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_INPUT XCB_WM_HINT_INPUT
132 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
133 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
134 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
135 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
136 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
137 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
138 #define xcb_icccm_get_wm_class xcb_get_wm_class
139 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
140 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
141 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
142 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
143 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
144 #define xcb_icccm_get_wm_name xcb_get_wm_name
145 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
146 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
147 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
148 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
149 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
150 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
151 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
152 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
153 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
154 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
157 /*#define SWM_DEBUG*/
159 #define DPRINTF(x...) do { \
161 fprintf(stderr, x); \
163 #define DNPRINTF(n,x...) do { \
164 if (swm_debug & n) { \
165 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
166 fprintf(stderr, x); \
169 #define SWM_D_MISC 0x0001
170 #define SWM_D_EVENT 0x0002
171 #define SWM_D_WS 0x0004
172 #define SWM_D_FOCUS 0x0008
173 #define SWM_D_MOVE 0x0010
174 #define SWM_D_STACK 0x0020
175 #define SWM_D_MOUSE 0x0040
176 #define SWM_D_PROP 0x0080
177 #define SWM_D_CLASS 0x0100
178 #define SWM_D_KEY 0x0200
179 #define SWM_D_QUIRK 0x0400
180 #define SWM_D_SPAWN 0x0800
181 #define SWM_D_EVENTQ 0x1000
182 #define SWM_D_CONF 0x2000
183 #define SWM_D_BAR 0x4000
184 #define SWM_D_INIT 0x8000
186 u_int32_t swm_debug
= 0
205 #define DPRINTF(x...)
206 #define DNPRINTF(n,x...)
209 /* convert 8-bit to 16-bit */
210 #define RGB_8_TO_16(col) ((col) << 8) + (col)
212 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
213 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
214 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
215 xrc.blue = RGB_8_TO_16((px) & 0xff); \
218 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
219 #define MODKEY XCB_MOD_MASK_1
220 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
221 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
222 XCB_EVENT_MASK_BUTTON_RELEASE)
223 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
224 #define SWM_PROPLEN (16)
225 #define SWM_FUNCNAME_LEN (32)
226 #define SWM_KEYS_LEN (255)
227 #define SWM_QUIRK_LEN (64)
228 #define X(r) (r)->g.x
229 #define Y(r) (r)->g.y
230 #define WIDTH(r) (r)->g.w
231 #define HEIGHT(r) (r)->g.h
232 #define BORDER(w) ((w)->bordered ? border_width : 0)
233 #define MAX_X(r) ((r)->g.x + (r)->g.w)
234 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
235 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
236 #define SH_MIN_W(w) (w)->sh.min_width
237 #define SH_MIN_H(w) (w)->sh.min_height
238 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
239 #define SH_MAX_W(w) (w)->sh.max_width
240 #define SH_MAX_H(w) (w)->sh.max_height
241 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
242 #define SH_INC_W(w) (w)->sh.width_inc
243 #define SH_INC_H(w) (w)->sh.height_inc
244 #define SWM_MAX_FONT_STEPS (3)
245 #define WINID(w) ((w) ? (w)->id : 0)
246 #define YESNO(x) ((x) ? "yes" : "no")
248 #define SWM_FOCUS_DEFAULT (0)
249 #define SWM_FOCUS_FOLLOW (1)
250 #define SWM_FOCUS_MANUAL (2)
252 #define SWM_CK_NONE 0
253 #define SWM_CK_ALL 0x7
254 #define SWM_CK_FOCUS 0x1
255 #define SWM_CK_POINTER 0x2
256 #define SWM_CK_FALLBACK 0x4
258 #define SWM_CONF_DEFAULT (0)
259 #define SWM_CONF_KEYMAPPING (1)
262 #define SWM_LIB "/usr/local/lib/libswmhack.so"
269 xcb_atom_t a_takefocus
;
271 xcb_atom_t a_netwmname
;
272 xcb_atom_t a_utf8_string
;
274 xcb_atom_t a_swm_iconic
;
276 volatile sig_atomic_t running
= 1;
277 volatile sig_atomic_t restart_wm
= 0;
278 xcb_timestamp_t last_event_time
= 0;
283 int xrandr_eventbase
;
284 unsigned int numlockmask
= 0;
287 xcb_connection_t
*conn
;
288 xcb_key_symbols_t
*syms
;
291 int cycle_visible
= 0;
293 int font_adjusted
= 0;
294 unsigned int mod_key
= MODKEY
;
297 struct swm_region
*search_r
;
298 int select_list_pipe
[2];
299 int select_resp_pipe
[2];
301 volatile sig_atomic_t search_resp
;
302 int search_resp_action
;
304 struct search_window
{
305 TAILQ_ENTRY(search_window
) entry
;
309 xcb_window_t indicator
;
311 TAILQ_HEAD(search_winlist
, search_window
);
312 struct search_winlist search_wl
;
317 SWM_SEARCH_UNICONIFY
,
318 SWM_SEARCH_NAME_WORKSPACE
,
319 SWM_SEARCH_SEARCH_WORKSPACE
,
320 SWM_SEARCH_SEARCH_WINDOW
323 #define SWM_STACK_TOP (0)
324 #define SWM_STACK_BOTTOM (1)
325 #define SWM_STACK_ABOVE (2)
326 #define SWM_STACK_BELOW (3)
329 double dialog_ratio
= 0.6;
331 #define SWM_BAR_MAX (256)
332 #define SWM_BAR_JUSTIFY_LEFT (0)
333 #define SWM_BAR_JUSTIFY_CENTER (1)
334 #define SWM_BAR_JUSTIFY_RIGHT (2)
335 #define SWM_BAR_OFFSET (4)
336 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
337 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
339 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
342 #ifdef X_HAVE_UTF8_STRING
343 #define DRAWSTRING(x...) Xutf8DrawString(x)
345 #define DRAWSTRING(x...) XmbDrawString(x)
348 char *bar_argv
[] = { NULL
, NULL
};
350 unsigned char bar_ext
[SWM_BAR_MAX
];
351 char bar_vertext
[SWM_BAR_MAX
];
353 sig_atomic_t bar_alarm
= 0;
356 int bar_border_width
= 1;
357 int bar_at_bottom
= 0;
359 int bar_extra_running
= 0;
362 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
363 char *bar_format
= NULL
;
364 int stack_enabled
= 1;
365 int clock_enabled
= 1;
366 int urgent_enabled
= 0;
367 char *clock_format
= NULL
;
368 int title_name_enabled
= 0;
369 int title_class_enabled
= 0;
370 int window_name_enabled
= 0;
371 int focus_mode
= SWM_FOCUS_DEFAULT
;
372 int focus_close
= SWM_STACK_BELOW
;
373 int focus_close_wrap
= 1;
374 int focus_default
= SWM_STACK_TOP
;
375 int spawn_position
= SWM_STACK_TOP
;
376 int disable_border
= 0;
377 int border_width
= 1;
378 int verbose_layout
= 0;
382 XFontSetExtents
*bar_fs_extents
;
384 int bar_font_legacy
= 1;
386 XftColor bar_font_color
;
389 /* layout manager data */
390 struct swm_geometry
{
403 struct swm_geometry g
;
406 /* virtual "screens" */
408 TAILQ_ENTRY(swm_region
) entry
;
409 struct swm_geometry g
;
410 struct workspace
*ws
; /* current workspace on this region */
411 struct workspace
*ws_prior
; /* prior workspace on this region */
412 struct swm_screen
*s
; /* screen idx */
415 TAILQ_HEAD(swm_region_list
, swm_region
);
418 TAILQ_ENTRY(ws_win
) entry
;
420 xcb_window_t transient
;
421 struct ws_win
*focus_child
; /* focus on child transient */
422 struct swm_geometry g
; /* current geometry */
423 struct swm_geometry g_float
; /* region coordinates */
424 int g_floatvalid
; /* g_float geometry validity */
425 int floatmaxed
; /* whether maxed by max_stack */
431 unsigned int ewmh_flags
;
432 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
438 unsigned long quirks
;
439 struct workspace
*ws
; /* always valid */
440 struct swm_screen
*s
; /* always valid, never changes */
441 xcb_get_geometry_reply_t
*wa
;
443 xcb_icccm_get_wm_class_reply_t ch
;
444 xcb_icccm_wm_hints_t hints
;
446 TAILQ_HEAD(ws_win_list
, ws_win
);
450 TAILQ_ENTRY(pid_e
) entry
;
454 TAILQ_HEAD(pid_list
, pid_e
);
455 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
457 /* layout handlers */
459 void vertical_config(struct workspace
*, int);
460 void vertical_stack(struct workspace
*, struct swm_geometry
*);
461 void horizontal_config(struct workspace
*, int);
462 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
463 void max_stack(struct workspace
*, struct swm_geometry
*);
464 void plain_stacker(struct workspace
*);
465 void fancy_stacker(struct workspace
*);
468 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
469 void (*l_config
)(struct workspace
*, int);
471 #define SWM_L_FOCUSPREV (1<<0)
472 #define SWM_L_MAPONFOCUS (1<<1)
473 void (*l_string
)(struct workspace
*);
475 /* stack, configure */
476 { vertical_stack
, vertical_config
, 0, plain_stacker
},
477 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
479 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
480 { NULL
, NULL
, 0, NULL
},
483 /* position of max_stack mode in the layouts array, index into layouts! */
484 #define SWM_V_STACK (0)
485 #define SWM_H_STACK (1)
486 #define SWM_MAX_STACK (2)
488 #define SWM_H_SLICE (32)
489 #define SWM_V_SLICE (32)
491 /* define work spaces */
493 int idx
; /* workspace index */
494 char *name
; /* workspace name */
495 int always_raise
; /* raise windows on focus */
496 struct layout
*cur_layout
; /* current layout handlers */
497 struct ws_win
*focus
; /* may be NULL */
498 struct ws_win
*focus_prev
; /* may be NULL */
499 struct ws_win
*focus_pending
; /* may be NULL */
500 struct swm_region
*r
; /* may be NULL */
501 struct swm_region
*old_r
; /* may be NULL */
502 struct ws_win_list winlist
; /* list of windows in ws */
503 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
504 char stacker
[10]; /* display stacker and layout */
508 int horizontal_msize
;
510 int horizontal_stacks
;
521 SWM_S_COLOR_BAR_BORDER
,
522 SWM_S_COLOR_BAR_FONT
,
528 /* physical screen mapping */
529 #define SWM_WS_MAX (22) /* hard limit */
530 int workspace_limit
= 10; /* soft limit */
533 int idx
; /* screen index */
534 struct swm_region_list rl
; /* list of regions on this screen */
535 struct swm_region_list orl
; /* list of old regions */
537 struct workspace ws
[SWM_WS_MAX
];
543 } c
[SWM_S_COLOR_MAX
];
545 xcb_gcontext_t bar_gc
;
548 struct swm_screen
*screens
;
550 /* args to functions */
553 #define SWM_ARG_ID_FOCUSNEXT (0)
554 #define SWM_ARG_ID_FOCUSPREV (1)
555 #define SWM_ARG_ID_FOCUSMAIN (2)
556 #define SWM_ARG_ID_SWAPNEXT (10)
557 #define SWM_ARG_ID_SWAPPREV (11)
558 #define SWM_ARG_ID_SWAPMAIN (12)
559 #define SWM_ARG_ID_MOVELAST (13)
560 #define SWM_ARG_ID_MASTERSHRINK (20)
561 #define SWM_ARG_ID_MASTERGROW (21)
562 #define SWM_ARG_ID_MASTERADD (22)
563 #define SWM_ARG_ID_MASTERDEL (23)
564 #define SWM_ARG_ID_FLIPLAYOUT (24)
565 #define SWM_ARG_ID_STACKRESET (30)
566 #define SWM_ARG_ID_STACKINIT (31)
567 #define SWM_ARG_ID_CYCLEWS_UP (40)
568 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
569 #define SWM_ARG_ID_CYCLESC_UP (42)
570 #define SWM_ARG_ID_CYCLESC_DOWN (43)
571 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
572 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
573 #define SWM_ARG_ID_STACKINC (50)
574 #define SWM_ARG_ID_STACKDEC (51)
575 #define SWM_ARG_ID_SS_ALL (60)
576 #define SWM_ARG_ID_SS_WINDOW (61)
577 #define SWM_ARG_ID_DONTCENTER (70)
578 #define SWM_ARG_ID_CENTER (71)
579 #define SWM_ARG_ID_KILLWINDOW (80)
580 #define SWM_ARG_ID_DELETEWINDOW (81)
581 #define SWM_ARG_ID_WIDTHGROW (90)
582 #define SWM_ARG_ID_WIDTHSHRINK (91)
583 #define SWM_ARG_ID_HEIGHTGROW (92)
584 #define SWM_ARG_ID_HEIGHTSHRINK (93)
585 #define SWM_ARG_ID_MOVEUP (100)
586 #define SWM_ARG_ID_MOVEDOWN (101)
587 #define SWM_ARG_ID_MOVELEFT (102)
588 #define SWM_ARG_ID_MOVERIGHT (103)
594 TAILQ_ENTRY(quirk
) entry
;
598 #define SWM_Q_FLOAT (1<<0) /* float this window */
599 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
600 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
601 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
602 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
603 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
605 TAILQ_HEAD(quirk_list
, quirk
);
606 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
609 * Supported EWMH hints should be added to
610 * both the enum and the ewmh array
615 _NET_MOVERESIZE_WINDOW
,
616 _NET_WM_ACTION_ABOVE
,
617 _NET_WM_ACTION_CLOSE
,
618 _NET_WM_ACTION_FULLSCREEN
,
620 _NET_WM_ACTION_RESIZE
,
621 _NET_WM_ALLOWED_ACTIONS
,
625 _NET_WM_STATE_FULLSCREEN
,
626 _NET_WM_STATE_HIDDEN
,
627 _NET_WM_STATE_MAXIMIZED_HORZ
,
628 _NET_WM_STATE_MAXIMIZED_VERT
,
629 _NET_WM_STATE_SKIP_PAGER
,
630 _NET_WM_STATE_SKIP_TASKBAR
,
632 _NET_WM_WINDOW_TYPE_DIALOG
,
633 _NET_WM_WINDOW_TYPE_DOCK
,
634 _NET_WM_WINDOW_TYPE_NORMAL
,
635 _NET_WM_WINDOW_TYPE_SPLASH
,
636 _NET_WM_WINDOW_TYPE_TOOLBAR
,
637 _NET_WM_WINDOW_TYPE_UTILITY
,
638 _SWM_WM_STATE_MANUAL
,
645 } ewmh
[SWM_EWMH_HINT_MAX
] = {
646 /* must be in same order as in the enum */
647 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
648 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
649 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
650 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
651 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
652 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
653 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
654 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
655 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
656 {"_NET_WM_NAME", XCB_ATOM_NONE
},
657 {"_NET_WM_STATE", XCB_ATOM_NONE
},
658 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
659 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
660 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
661 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
662 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
663 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
664 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
665 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
666 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
667 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
668 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
669 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
670 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
671 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
672 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
675 /* function prototypes */
676 void buttonpress(xcb_button_press_event_t
*);
677 void check_conn(void);
678 void clientmessage(xcb_client_message_event_t
*);
679 int conf_load(char *, int);
680 void configurenotify(xcb_configure_notify_event_t
*);
681 void configurerequest(xcb_configure_request_event_t
*);
682 void constrain_window(struct ws_win
*, struct swm_region
*, int);
683 void destroynotify(xcb_destroy_notify_event_t
*);
684 void enternotify(xcb_enter_notify_event_t
*);
685 void event_drain(uint8_t);
686 void event_error(xcb_generic_error_t
*);
687 void event_handle(xcb_generic_event_t
*);
688 char *expand_tilde(char *);
689 void expose(xcb_expose_event_t
*);
690 struct ws_win
*find_window(xcb_window_t
);
691 int floating_toggle_win(struct ws_win
*);
692 void focus(struct swm_region
*, union arg
*);
693 void focus_flush(void);
694 struct ws_win
*get_focus_magic(struct ws_win
*);
695 struct ws_win
*get_focus_prev(struct ws_win
*);
696 void focus_win(struct ws_win
*);
698 void focusin(xcb_focus_in_event_t
*);
700 xcb_atom_t
get_atom_from_string(const char *);
702 char *get_atom_name(xcb_atom_t
);
703 char *get_notify_detail_label(uint8_t);
704 char *get_notify_mode_label(uint8_t);
706 struct ws_win
*get_region_focus(struct swm_region
*);
707 xcb_screen_t
*get_screen(int);
708 char *get_win_name(xcb_window_t
);
709 uint32_t getstate(xcb_window_t
);
710 void grabbuttons(struct ws_win
*);
711 void keypress(xcb_key_press_event_t
*);
713 void leavenotify(xcb_leave_notify_event_t
*);
715 void load_float_geom(struct ws_win
*, struct swm_region
*);
716 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
717 void map_window(struct ws_win
*);
718 void mapnotify(xcb_map_notify_event_t
*);
719 void mappingnotify(xcb_mapping_notify_event_t
*);
720 void maprequest(xcb_map_request_event_t
*);
721 void new_region(struct swm_screen
*, int, int, int, int);
722 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
723 void propertynotify(xcb_property_notify_event_t
*);
724 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
725 void screenchange(xcb_randr_screen_change_notify_event_t
*);
726 void shutdown_cleanup(void);
727 void store_float_geom(struct ws_win
*, struct swm_region
*);
728 void unmanage_window(struct ws_win
*);
729 void unmapnotify(xcb_unmap_notify_event_t
*);
730 void unfocus_win(struct ws_win
*);
731 void update_window(struct ws_win
*);
732 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
735 expand_tilde(char *s
)
744 errx(1, "expand_tilde: NULL string.");
753 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
754 errx(1, "expand_tilde: sysconf");
756 if ((user
= calloc(1, max
+ 1)) == NULL
)
757 errx(1, "expand_tilde: calloc");
759 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
764 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
768 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
772 errx(1, "expand_tilde: failed to allocate memory.");
778 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
780 unsigned int tmpr
, tmpg
, tmpb
;
782 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
785 *rr
= RGB_8_TO_16(tmpr
);
786 *gg
= RGB_8_TO_16(tmpg
);
787 *bb
= RGB_8_TO_16(tmpb
);
793 get_screen(int screen
)
795 const xcb_setup_t
*r
;
796 xcb_screen_iterator_t iter
;
798 if ((r
= xcb_get_setup(conn
)) == NULL
) {
799 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
803 iter
= xcb_setup_roots_iterator(r
);
804 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
814 if (focus_mode
== SWM_FOCUS_DEFAULT
)
815 event_drain(XCB_ENTER_NOTIFY
);
821 get_atom_from_string(const char *str
)
823 xcb_intern_atom_cookie_t c
;
824 xcb_intern_atom_reply_t
*r
;
827 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
828 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
836 return (XCB_ATOM_NONE
);
840 set_swm_iconic(struct ws_win
*win
, int newv
)
847 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
848 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
850 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
854 get_swm_iconic(struct ws_win
*win
)
857 xcb_get_property_reply_t
*pr
= NULL
;
859 pr
= xcb_get_property_reply(conn
,
860 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
861 XCB_ATOM_INTEGER
, 0, 1), NULL
);
864 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
866 v
= *((int32_t *)xcb_get_property_value(pr
));
877 int i
, j
, num_screens
;
879 sup_list
= get_atom_from_string("_NET_SUPPORTED");
881 for (i
= 0; i
< LENGTH(ewmh
); i
++)
882 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
884 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
885 for (i
= 0; i
< num_screens
; i
++) {
886 /* Support check window will be created by workaround(). */
888 /* Report supported atoms */
889 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
890 for (j
= 0; j
< LENGTH(ewmh
); j
++)
891 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
892 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
901 xcb_atom_t sup_check
, sup_list
;
903 xcb_get_property_cookie_t pc
;
904 xcb_get_property_reply_t
*pr
;
906 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
907 sup_list
= get_atom_from_string("_NET_SUPPORTED");
908 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
910 for (i
= 0; i
< num_screens
; i
++) {
911 /* Get the support check window and destroy it */
912 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
913 XCB_ATOM_WINDOW
, 0, 1);
914 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
917 if (pr
->format
== sup_check
) {
918 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
920 xcb_destroy_window(conn
, id
);
921 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
922 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
929 ewmh_autoquirk(struct ws_win
*win
)
933 xcb_get_property_cookie_t c
;
934 xcb_get_property_reply_t
*r
;
936 c
= xcb_get_property(conn
, 0, win
->id
,
937 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
938 r
= xcb_get_property_reply(conn
, c
, NULL
);
942 n
= xcb_get_property_value_length(r
);
943 type
= xcb_get_property_value(r
);
945 for (i
= 0; i
< n
; i
++) {
946 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
948 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
949 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
950 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
952 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
955 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
956 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
958 win
->quirks
= SWM_Q_FLOAT
;
965 #define SWM_EWMH_ACTION_COUNT_MAX (6)
966 #define EWMH_F_FULLSCREEN (1<<0)
967 #define EWMH_F_ABOVE (1<<1)
968 #define EWMH_F_HIDDEN (1<<2)
969 #define EWMH_F_SKIP_PAGER (1<<3)
970 #define EWMH_F_SKIP_TASKBAR (1<<4)
971 #define SWM_F_MANUAL (1<<5)
974 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
982 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
983 "fullscreen %s\n", win
->id
, YESNO(fs
));
986 if (!win
->g_floatvalid
)
987 store_float_geom(win
, win
->ws
->r
);
989 win
->g
= win
->ws
->r
->g
;
992 load_float_geom(win
, win
->ws
->r
);
999 ewmh_update_actions(struct ws_win
*win
)
1001 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1007 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1009 if (win
->floating
) {
1010 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1011 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1012 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1015 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1016 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1019 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1020 #define _NET_WM_STATE_ADD 1 /* add/set property */
1021 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1024 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1026 unsigned int mask
= 0;
1027 unsigned int changed
= 0;
1028 unsigned int orig_flags
;
1033 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1034 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1036 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1037 mask
= EWMH_F_FULLSCREEN
;
1038 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1039 mask
= EWMH_F_ABOVE
;
1040 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1041 mask
= SWM_F_MANUAL
;
1042 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1043 mask
= EWMH_F_SKIP_PAGER
;
1044 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1045 mask
= EWMH_F_SKIP_TASKBAR
;
1047 orig_flags
= win
->ewmh_flags
;
1050 case _NET_WM_STATE_REMOVE
:
1051 win
->ewmh_flags
&= ~mask
;
1053 case _NET_WM_STATE_ADD
:
1054 win
->ewmh_flags
|= mask
;
1056 case _NET_WM_STATE_TOGGLE
:
1057 win
->ewmh_flags
^= mask
;
1061 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1063 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1064 if (changed
&& !floating_toggle_win(win
))
1065 win
->ewmh_flags
= orig_flags
; /* revert */
1066 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1068 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1069 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1070 if (changed
&& !ewmh_set_win_fullscreen(win
,
1071 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1072 win
->ewmh_flags
= orig_flags
; /* revert */
1075 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1077 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1078 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1079 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1080 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1081 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1082 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1083 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1084 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1085 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1086 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1087 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1088 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1089 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1090 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1091 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1092 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1093 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1094 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1095 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1096 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1100 ewmh_get_win_state(struct ws_win
*win
)
1103 xcb_get_property_cookie_t c
;
1104 xcb_get_property_reply_t
*r
;
1110 win
->ewmh_flags
= 0;
1112 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1114 win
->ewmh_flags
|= SWM_F_MANUAL
;
1116 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1117 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1118 r
= xcb_get_property_reply(conn
, c
, NULL
);
1122 states
= xcb_get_property_value(r
);
1123 n
= xcb_get_property_value_length(r
);
1125 for (i
= 0; i
< n
; i
++)
1126 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1134 dumpwins(struct swm_region
*r
, union arg
*args
)
1138 xcb_get_window_attributes_cookie_t c
;
1139 xcb_get_window_attributes_reply_t
*wa
;
1141 /* suppress unused warning since var is needed */
1144 if (r
->ws
== NULL
) {
1145 warnx("dumpwins: invalid workspace");
1149 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1150 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1151 state
= getstate(win
->id
);
1152 c
= xcb_get_window_attributes(conn
, win
->id
);
1153 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1155 warnx("window: 0x%x, map_state: %d, state: %u, "
1156 "transient: 0x%x", win
->id
, wa
->map_state
,
1157 state
, win
->transient
);
1160 warnx("window: 0x%x, failed xcb_get_window_attributes",
1164 warnx("===== unmanaged window list =====");
1165 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1166 state
= getstate(win
->id
);
1167 c
= xcb_get_window_attributes(conn
, win
->id
);
1168 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1170 warnx("window: 0x%x, map_state: %d, state: %u, "
1171 "transient: 0x%x", win
->id
, wa
->map_state
,
1172 state
, win
->transient
);
1175 warnx("window: 0x%x, failed xcb_get_window_attributes",
1179 warnx("=================================");
1183 dumpwins(struct swm_region
*r
, union arg
*s
)
1188 #endif /* SWM_DEBUG */
1193 int saved_errno
, status
;
1196 saved_errno
= errno
;
1200 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1205 if (errno
!= ECHILD
)
1206 warn("sighdlr: waitpid");
1207 #endif /* SWM_DEBUG */
1210 if (pid
== searchpid
)
1214 if (WIFEXITED(status
)) {
1215 if (WEXITSTATUS(status
) != 0)
1216 warnx("sighdlr: child exit status: %d",
1217 WEXITSTATUS(status
));
1219 warnx("sighdlr: child is terminated "
1221 #endif /* SWM_DEBUG */
1235 errno
= saved_errno
;
1241 struct pid_e
*p
= NULL
;
1243 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1248 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1257 name_to_pixel(const char *colorname
)
1259 uint32_t result
= 0;
1260 char cname
[32] = "#";
1261 xcb_screen_t
*screen
;
1262 xcb_colormap_t cmap
;
1263 xcb_alloc_color_reply_t
*cr
;
1264 xcb_alloc_named_color_reply_t
*nr
;
1265 uint16_t rr
, gg
, bb
;
1267 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1268 cmap
= screen
->default_colormap
;
1270 /* color is in format rgb://rr/gg/bb */
1271 if (strncmp(colorname
, "rgb:", 4) == 0) {
1272 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1273 warnx("could not parse rgb %s", colorname
);
1275 cr
= xcb_alloc_color_reply(conn
,
1276 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1282 warnx("color '%s' not found", colorname
);
1285 nr
= xcb_alloc_named_color_reply(conn
,
1286 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1289 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1290 nr
= xcb_alloc_named_color_reply(conn
,
1291 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1298 warnx("color '%s' not found", colorname
);
1305 setscreencolor(char *val
, int i
, int c
)
1309 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1310 if (i
> 0 && i
<= num_screens
) {
1311 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1312 free(screens
[i
- 1].c
[c
].name
);
1313 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1315 } else if (i
== -1) {
1316 for (i
= 0; i
< num_screens
; i
++) {
1317 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1318 free(screens
[i
].c
[c
].name
);
1319 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1323 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1328 fancy_stacker(struct workspace
*ws
)
1330 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1331 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1332 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1333 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1334 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1335 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1336 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1337 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1338 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1342 plain_stacker(struct workspace
*ws
)
1344 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1345 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1346 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1347 sizeof ws
->stacker
);
1348 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1349 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1350 sizeof ws
->stacker
);
1354 custom_region(char *val
)
1356 unsigned int x
, y
, w
, h
;
1357 int sidx
, num_screens
;
1358 xcb_screen_t
*screen
;
1360 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1361 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1362 errx(1, "invalid custom region, "
1363 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1364 if (sidx
< 1 || sidx
> num_screens
)
1365 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1369 if ((screen
= get_screen(sidx
)) == NULL
)
1370 errx(1, "ERROR: can't get screen %d.", sidx
);
1373 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1375 if (x
> screen
->width_in_pixels
||
1376 y
> screen
->height_in_pixels
||
1377 w
+ x
> screen
->width_in_pixels
||
1378 h
+ y
> screen
->height_in_pixels
) {
1379 warnx("ignoring region %ux%u+%u+%u - not within screen "
1380 "boundaries (%ux%u)", w
, h
, x
, y
,
1381 screen
->width_in_pixels
, screen
->height_in_pixels
);
1385 new_region(&screens
[sidx
], x
, y
, w
, h
);
1389 socket_setnonblock(int fd
)
1393 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1394 err(1, "fcntl F_GETFL");
1395 flags
|= O_NONBLOCK
;
1396 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1397 err(1, "fcntl F_SETFL");
1401 bar_print_legacy(struct swm_region
*r
, const char *s
)
1403 xcb_rectangle_t rect
;
1408 XRectangle ibox
, lbox
;
1412 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1414 switch (bar_justify
) {
1415 case SWM_BAR_JUSTIFY_LEFT
:
1418 case SWM_BAR_JUSTIFY_CENTER
:
1419 x
= (WIDTH(r
) - lbox
.width
) / 2;
1421 case SWM_BAR_JUSTIFY_RIGHT
:
1422 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1426 if (x
< SWM_BAR_OFFSET
)
1431 rect
.width
= WIDTH(r
->bar
);
1432 rect
.height
= HEIGHT(r
->bar
);
1434 /* clear back buffer */
1435 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1436 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1437 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1439 /* draw back buffer */
1440 gcvd
.graphics_exposures
= 0;
1441 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1442 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1443 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1444 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1446 XFreeGC(display
, draw
);
1449 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1450 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1454 bar_print(struct swm_region
*r
, const char *s
)
1457 xcb_rectangle_t rect
;
1465 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1467 switch (bar_justify
) {
1468 case SWM_BAR_JUSTIFY_LEFT
:
1471 case SWM_BAR_JUSTIFY_CENTER
:
1472 x
= (WIDTH(r
) - info
.width
) / 2;
1474 case SWM_BAR_JUSTIFY_RIGHT
:
1475 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1479 if (x
< SWM_BAR_OFFSET
)
1484 rect
.width
= WIDTH(r
->bar
);
1485 rect
.height
= HEIGHT(r
->bar
);
1487 /* clear back buffer */
1488 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1489 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1490 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1492 /* draw back buffer */
1493 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1494 DefaultVisual(display
, r
->s
->idx
),
1495 DefaultColormap(display
, r
->s
->idx
));
1497 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1498 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1501 XftDrawDestroy(draw
);
1504 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1505 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1509 bar_extra_stop(void)
1513 bzero(bar_pipe
, sizeof bar_pipe
);
1516 kill(bar_pid
, SIGTERM
);
1519 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1524 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1526 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1528 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1529 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1533 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1535 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1537 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1538 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1542 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1544 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1547 bar_class_name(s
, sz
, r
);
1548 strlcat(s
, ":", sz
);
1549 bar_title_name(s
, sz
, r
);
1553 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1555 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1557 if (r
->ws
->focus
->floating
)
1558 strlcat(s
, "(f)", sz
);
1562 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1566 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1568 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1571 strlcat(s
, title
, sz
);
1575 int urgent
[SWM_WS_MAX
];
1577 bar_urgent(char *s
, size_t sz
)
1580 int i
, j
, num_screens
;
1582 xcb_get_property_cookie_t c
;
1583 xcb_icccm_wm_hints_t hints
;
1585 for (i
= 0; i
< workspace_limit
; i
++)
1588 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1589 for (i
= 0; i
< num_screens
; i
++)
1590 for (j
= 0; j
< workspace_limit
; j
++)
1591 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1592 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1593 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1596 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1600 for (i
= 0; i
< workspace_limit
; i
++) {
1602 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1604 snprintf(b
, sizeof b
, "- ");
1610 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1612 if (r
== NULL
|| r
->ws
== NULL
)
1614 if (r
->ws
->name
!= NULL
)
1615 strlcat(s
, r
->ws
->name
, sz
);
1618 /* build the default bar format according to the defined enabled options */
1620 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1622 /* if format provided, just copy the buffers */
1623 if (bar_format
!= NULL
) {
1624 strlcpy(fmtnew
, fmtexp
, sz
);
1628 /* reset the output buffer */
1631 strlcat(fmtnew
, "+N:+I ", sz
);
1633 strlcat(fmtnew
, "+S", sz
);
1634 strlcat(fmtnew
, " ", sz
);
1636 /* only show the workspace name if there's actually one */
1637 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1638 strlcat(fmtnew
, "<+D>", sz
);
1639 strlcat(fmtnew
, "+3<", sz
);
1641 if (clock_enabled
) {
1642 strlcat(fmtnew
, fmtexp
, sz
);
1643 strlcat(fmtnew
, "+4<", sz
);
1646 /* bar_urgent already adds the space before the last asterisk */
1648 strlcat(fmtnew
, "* +U*+4<", sz
);
1650 if (title_class_enabled
) {
1651 strlcat(fmtnew
, "+C", sz
);
1652 if (!title_name_enabled
)
1653 strlcat(fmtnew
, "+4<", sz
);
1656 /* checks needed by the colon and floating strlcat(3) calls below */
1657 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1658 if (title_name_enabled
) {
1659 if (title_class_enabled
)
1660 strlcat(fmtnew
, ":", sz
);
1661 strlcat(fmtnew
, "+T+4<", sz
);
1663 if (window_name_enabled
) {
1664 if (r
->ws
->focus
->floating
)
1665 strlcat(fmtnew
, "+F ", sz
);
1666 strlcat(fmtnew
, "+64W ", sz
);
1670 /* finally add the action script output and the version */
1671 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1675 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1677 /* special case; no limit given, pad one space, instead */
1678 if (*limit
== (int)sz
- 1)
1680 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1683 /* replaces the bar format character sequences (like in tmux(1)) */
1685 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1689 char tmp
[SWM_BAR_MAX
];
1693 /* reset strlcat(3) buffer */
1696 /* get number, if any */
1699 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1700 limit
= sizeof tmp
- 1;
1701 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1702 limit
= sizeof tmp
- 1;
1704 /* there is nothing to replace (ie EOL) */
1711 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1714 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1717 bar_class_name(tmp
, sizeof tmp
, r
);
1720 bar_workspace_name(tmp
, sizeof tmp
, r
);
1723 bar_window_float(tmp
, sizeof tmp
, r
);
1726 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1729 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1732 bar_class_title_name(tmp
, sizeof tmp
, r
);
1735 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1738 bar_title_name(tmp
, sizeof tmp
, r
);
1741 bar_urgent(tmp
, sizeof tmp
);
1744 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1747 bar_window_name(tmp
, sizeof tmp
, r
);
1750 /* unknown character sequence; copy as-is */
1751 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1757 if ((int)len
< limit
)
1759 while (limit
-- > 0) {
1760 if (*offrep
>= sz
- 1)
1762 fmtrep
[(*offrep
)++] = *ptr
++;
1770 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1775 while (*fmt
!= '\0') {
1777 /* skip ordinary characters */
1780 fmtrep
[off
++] = *fmt
++;
1784 /* character sequence found; replace it */
1785 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1794 bar_fmt_expand(char *fmtexp
, size_t sz
)
1801 /* start by grabbing the current time and date */
1803 localtime_r(&tmt
, &tm
);
1805 /* figure out what to expand */
1806 if (bar_format
!= NULL
)
1808 else if (bar_format
== NULL
&& clock_enabled
)
1810 /* if nothing to expand bail out */
1816 /* copy as-is, just in case the format shouldn't be expanded below */
1817 strlcpy(fmtexp
, fmt
, sz
);
1818 /* finally pass the string through strftime(3) */
1819 #ifndef SWM_DENY_CLOCK_FORMAT
1820 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1821 warnx("format too long");
1829 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1830 char fmtrep
[SWM_BAR_MAX
];
1832 struct swm_region
*r
;
1834 /* expand the format by first passing it through strftime(3) */
1835 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1837 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1838 for (i
= 0; i
< num_screens
; i
++) {
1839 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1842 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1843 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1844 if (bar_font_legacy
)
1845 bar_print_legacy(r
, fmtrep
);
1847 bar_print(r
, fmtrep
);
1860 if (bar_extra
&& bar_extra_running
) {
1861 /* ignore short reads; it'll correct itself */
1862 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1863 if (b
&& b
[len
- 1] == '\n') {
1865 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1867 if (b
== NULL
&& errno
!= EAGAIN
) {
1868 warn("bar_update: bar_extra failed");
1872 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1881 /* suppress unused warning since var is needed */
1888 bar_toggle(struct swm_region
*r
, union arg
*args
)
1890 struct swm_region
*tmpr
;
1893 /* suppress unused warnings since vars are needed */
1897 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1899 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1901 for (i
= 0; i
< num_screens
; i
++)
1902 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1904 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1906 for (i
= 0; i
< num_screens
; i
++)
1907 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1909 xcb_map_window(conn
, tmpr
->bar
->id
);
1912 bar_enabled
= !bar_enabled
;
1916 /* must be after stack */
1925 struct swm_region
*r
;
1929 /* do this here because the conf file is in memory */
1930 if (bar_extra
&& !bar_extra_running
&& bar_argv
[0]) {
1931 /* launch external status app */
1932 bar_extra_running
= 1;
1933 if (pipe(bar_pipe
) == -1)
1934 err(1, "pipe error");
1935 socket_setnonblock(bar_pipe
[0]);
1936 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1937 if (dup2(bar_pipe
[0], 0) == -1)
1939 if (dup2(bar_pipe
[1], 1) == -1)
1941 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1942 err(1, "could not disable SIGPIPE");
1943 switch (bar_pid
= fork()) {
1945 err(1, "cannot fork");
1949 execvp(bar_argv
[0], bar_argv
);
1950 err(1, "%s external app failed", bar_argv
[0]);
1952 default: /* parent */
1958 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1959 for (i
= 0; i
< num_screens
; i
++)
1960 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1963 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
1964 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1965 xcb_change_window_attributes(conn
, r
->bar
->id
,
1966 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1976 while ((s
= index(s
, '-'))) {
1981 return (count
== 14);
1987 char *default_string
;
1988 char **missing_charsets
;
1989 int num_missing_charsets
= 0;
1993 XFreeFontSet(display
, bar_fs
);
1997 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
1999 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2000 &num_missing_charsets
, &default_string
);
2002 if (num_missing_charsets
> 0) {
2003 warnx("Unable to load charset(s):");
2005 for (i
= 0; i
< num_missing_charsets
; ++i
)
2006 warnx("%s", missing_charsets
[i
]);
2008 XFreeStringList(missing_charsets
);
2010 if (strcmp(default_string
, ""))
2011 warnx("Glyphs from those sets will be replaced "
2012 "by '%s'.", default_string
);
2014 warnx("Glyphs from those sets won't be drawn.");
2018 errx(1, "Error creating font set structure.");
2020 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2022 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2023 2 * bar_border_width
;
2030 xft_init(struct swm_region
*r
)
2032 char *font
, *d
, *search
;
2035 if (bar_font
== NULL
) {
2036 if ((d
= strdup(bar_fonts
)) == NULL
)
2037 errx(1, "insufficient memory.");
2039 while ((font
= strsep(&search
, ",")) != NULL
) {
2043 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2046 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2049 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2054 warnx("unable to load font %s", font
);
2057 DNPRINTF(SWM_D_INIT
, "successfully opened "
2065 if (bar_font
== NULL
)
2066 errx(1, "unable to open a font");
2068 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2070 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2071 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2072 warn("unable to allocate Xft color");
2074 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2081 bar_setup(struct swm_region
*r
)
2083 xcb_screen_t
*screen
;
2086 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2089 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2090 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2095 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2096 err(1, "bar_setup: calloc: failed to allocate memory.");
2098 if (bar_font_legacy
)
2104 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2105 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2106 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2108 r
->bar
->id
= xcb_generate_id(conn
);
2109 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2110 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2111 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2113 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2114 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2115 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2116 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2117 | XCB_CW_EVENT_MASK
, wa
);
2119 r
->bar
->buffer
= xcb_generate_id(conn
);
2120 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2121 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2124 xcb_randr_select_input(conn
, r
->bar
->id
,
2125 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2128 xcb_map_window(conn
, r
->bar
->id
);
2130 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2131 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2134 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2135 err(1, "could not install bar_signal");
2140 bar_cleanup(struct swm_region
*r
)
2144 xcb_destroy_window(conn
, r
->bar
->id
);
2145 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2151 set_win_state(struct ws_win
*win
, uint16_t state
)
2153 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2155 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2161 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2162 a_state
, 32, 2, data
);
2166 getstate(xcb_window_t w
)
2168 uint32_t result
= 0;
2169 xcb_get_property_cookie_t c
;
2170 xcb_get_property_reply_t
*r
;
2172 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2173 r
= xcb_get_property_reply(conn
, c
, NULL
);
2176 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2177 result
= *((uint32_t *)xcb_get_property_value(r
));
2181 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2187 version(struct swm_region
*r
, union arg
*args
)
2189 /* suppress unused warnings since vars are needed */
2193 bar_version
= !bar_version
;
2195 snprintf(bar_vertext
, sizeof bar_vertext
,
2196 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2198 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2205 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2207 xcb_client_message_event_t ev
;
2215 name
= get_atom_name(a
);
2216 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2218 win
->id
, name
, a
, t
);
2222 bzero(&ev
, sizeof ev
);
2223 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2224 ev
.window
= win
->id
;
2227 ev
.data
.data32
[0] = a
;
2228 ev
.data
.data32
[1] = t
;
2230 xcb_send_event(conn
, 0, win
->id
,
2231 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2234 /* synthetic response to a ConfigureRequest when not making a change */
2236 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2238 xcb_configure_notify_event_t ce
;
2243 /* send notification of unchanged state. */
2244 bzero(&ce
, sizeof(ce
));
2245 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2248 ce
.width
= WIDTH(win
);
2249 ce
.height
= HEIGHT(win
);
2250 ce
.override_redirect
= 0;
2255 ce
.window
= win
->id
;
2256 ce
.border_width
= BORDER(win
);
2257 ce
.above_sibling
= XCB_WINDOW_NONE
;
2260 ce
.event
= ev
->window
;
2261 ce
.window
= ev
->window
;
2263 /* make response appear more WM_SIZE_HINTS-compliant */
2264 if (win
->sh
.flags
) {
2265 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2266 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2267 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2268 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2269 SH_INC_W(win
), SH_INC_H(win
));
2274 /* the hint may be set... to 0! */
2275 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2276 ce
.width
= SH_MIN_W(win
);
2277 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2278 ce
.height
= SH_MIN_H(win
);
2283 /* may also be advertized as 0 */
2284 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2285 ce
.width
= SH_MAX_W(win
);
2286 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2287 ce
.height
= SH_MAX_H(win
);
2290 /* resize increment. */
2292 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2293 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2295 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2296 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2300 /* adjust x and y for requested border_width. */
2301 ce
.x
+= BORDER(win
) - ev
->border_width
;
2302 ce
.y
+= BORDER(win
) - ev
->border_width
;
2303 ce
.border_width
= ev
->border_width
;
2304 ce
.above_sibling
= ev
->sibling
;
2307 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2308 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2309 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2311 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2316 count_win(struct workspace
*ws
, int count_transient
)
2321 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2322 if (!count_transient
&& win
->floating
)
2324 if (!count_transient
&& win
->transient
)
2330 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2336 quit(struct swm_region
*r
, union arg
*args
)
2338 /* suppress unused warnings since vars are needed */
2342 DNPRINTF(SWM_D_MISC
, "quit\n");
2347 map_window(struct ws_win
*win
)
2349 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2354 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2355 YESNO(win
->mapped
));
2357 xcb_configure_window(conn
, win
->id
,
2358 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2363 xcb_map_window(conn
, win
->id
);
2364 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2369 unmap_window(struct ws_win
*win
)
2374 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2375 YESNO(win
->mapped
));
2380 xcb_unmap_window(conn
, win
->id
);
2381 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2389 int i
, j
, num_screens
;
2391 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2392 for (i
= 0; i
< num_screens
; i
++)
2393 for (j
= 0; j
< workspace_limit
; j
++)
2394 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2399 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2401 xcb_key_press_event_t event
;
2402 xcb_keycode_t
*keycode
;
2407 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2409 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2412 bzero(&event
, sizeof(event
));
2413 event
.event
= win
->id
;
2414 event
.root
= win
->s
->root
;
2415 event
.child
= XCB_WINDOW_NONE
;
2416 event
.time
= XCB_CURRENT_TIME
;
2417 event
.event_x
= X(win
);
2418 event
.event_y
= Y(win
);
2421 event
.same_screen
= 1;
2422 event
.detail
= *keycode
;
2423 event
.state
= modifiers
;
2425 event
.response_type
= XCB_KEY_PRESS
;
2426 xcb_send_event(conn
, 1, win
->id
,
2427 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2429 event
.response_type
= XCB_KEY_RELEASE
;
2430 xcb_send_event(conn
, 1, win
->id
,
2431 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2437 restart(struct swm_region
*r
, union arg
*args
)
2439 /* suppress unused warning since var is needed */
2443 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2447 execvp(start_argv
[0], start_argv
);
2448 warn("execvp failed");
2453 root_to_region(xcb_window_t root
, int check
)
2456 struct swm_region
*r
= NULL
;
2458 xcb_query_pointer_reply_t
*qpr
;
2459 xcb_get_input_focus_reply_t
*gifr
;
2461 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2463 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2464 for (i
= 0; i
< num_screens
; i
++)
2465 if (screens
[i
].root
== root
)
2468 if (check
& SWM_CK_FOCUS
) {
2469 /* Try to find an actively focused window */
2470 gifr
= xcb_get_input_focus_reply(conn
,
2471 xcb_get_input_focus(conn
), NULL
);
2473 cfw
= find_window(gifr
->focus
);
2474 if (cfw
&& cfw
->ws
->r
)
2481 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2482 /* No region with an active focus; try to use pointer. */
2483 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2484 screens
[i
].root
), NULL
);
2487 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2488 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2489 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2490 if (X(r
) <= qpr
->root_x
&&
2491 qpr
->root_x
< MAX_X(r
) &&
2492 Y(r
) <= qpr
->root_y
&&
2493 qpr
->root_y
< MAX_Y(r
))
2500 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2501 r
= TAILQ_FIRST(&screens
[i
].rl
);
2507 find_unmanaged_window(xcb_window_t id
)
2510 int i
, j
, num_screens
;
2512 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2513 for (i
= 0; i
< num_screens
; i
++)
2514 for (j
= 0; j
< workspace_limit
; j
++)
2515 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2523 find_window(xcb_window_t id
)
2526 int i
, j
, num_screens
;
2527 xcb_query_tree_reply_t
*r
;
2529 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2530 for (i
= 0; i
< num_screens
; i
++)
2531 for (j
= 0; j
< workspace_limit
; j
++)
2532 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2536 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2540 /* if we were looking for the parent return that window instead */
2541 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2546 /* look for parent */
2547 for (i
= 0; i
< num_screens
; i
++)
2548 for (j
= 0; j
< workspace_limit
; j
++)
2549 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2550 if (r
->parent
== win
->id
) {
2560 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2565 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2567 close(xcb_get_file_descriptor(conn
));
2569 setenv("LD_PRELOAD", SWM_LIB
, 1);
2571 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2572 warn("spawn: asprintf SWM_WS");
2575 setenv("_SWM_WS", ret
, 1);
2579 if (asprintf(&ret
, "%d", getpid()) == -1) {
2580 warn("spawn: asprintf _SWM_PID");
2583 setenv("_SWM_PID", ret
, 1);
2587 if (setsid() == -1) {
2588 warn("spawn: setsid");
2594 * close stdin and stdout to prevent interaction between apps
2595 * and the baraction script
2596 * leave stderr open to record errors
2598 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2599 warn("spawn: open");
2602 dup2(fd
, STDIN_FILENO
);
2603 dup2(fd
, STDOUT_FILENO
);
2608 execvp(args
->argv
[0], args
->argv
);
2610 warn("spawn: execvp");
2615 kill_refs(struct ws_win
*win
)
2617 int i
, x
, num_screens
;
2618 struct swm_region
*r
;
2619 struct workspace
*ws
;
2624 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2625 for (i
= 0; i
< num_screens
; i
++)
2626 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2627 for (x
= 0; x
< workspace_limit
; x
++) {
2629 if (win
== ws
->focus
)
2631 if (win
== ws
->focus_prev
)
2632 ws
->focus_prev
= NULL
;
2637 validate_win(struct ws_win
*testwin
)
2640 struct workspace
*ws
;
2641 struct swm_region
*r
;
2642 int i
, x
, num_screens
;
2644 if (testwin
== NULL
)
2647 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2648 for (i
= 0; i
< num_screens
; i
++)
2649 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2650 for (x
= 0; x
< workspace_limit
; x
++) {
2652 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2660 validate_ws(struct workspace
*testws
)
2662 struct swm_region
*r
;
2663 struct workspace
*ws
;
2664 int i
, x
, num_screens
;
2666 /* validate all ws */
2667 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2668 for (i
= 0; i
< num_screens
; i
++)
2669 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2670 for (x
= 0; x
< workspace_limit
; x
++) {
2679 unfocus_win(struct ws_win
*win
)
2681 xcb_window_t none
= XCB_WINDOW_NONE
;
2683 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2687 if (win
->ws
== NULL
) {
2688 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
2692 if (validate_ws(win
->ws
)) {
2693 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
2697 if (win
->ws
->r
== NULL
) {
2698 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
2702 if (validate_win(win
)) {
2703 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
2708 if (win
->ws
->focus
== win
) {
2709 win
->ws
->focus
= NULL
;
2710 win
->ws
->focus_prev
= win
;
2713 if (validate_win(win
->ws
->focus
)) {
2714 kill_refs(win
->ws
->focus
);
2715 win
->ws
->focus
= NULL
;
2717 if (validate_win(win
->ws
->focus_prev
)) {
2718 kill_refs(win
->ws
->focus_prev
);
2719 win
->ws
->focus_prev
= NULL
;
2722 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2723 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2725 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2726 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2728 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
2732 focus_win(struct ws_win
*win
)
2734 struct ws_win
*cfw
= NULL
;
2735 xcb_get_input_focus_reply_t
*r
;
2737 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2742 if (win
->ws
== NULL
)
2745 if (validate_ws(win
->ws
))
2748 if (validate_win(win
)) {
2753 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2755 cfw
= find_window(r
->focus
);
2761 if (win
->ws
->focus
!= win
) {
2762 if (win
->ws
->focus
&& win
->ws
->focus
!= cfw
)
2763 unfocus_win(win
->ws
->focus
);
2764 win
->ws
->focus
= win
;
2767 if (cfw
!= win
&& win
->ws
->r
!= NULL
) {
2768 /* Set input focus if no input hint, or indicated by hint. */
2769 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
2770 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
2772 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2773 win
->id
, last_event_time
);
2775 /* Tell app it can adjust focus to a specific window. */
2776 if (win
->take_focus
) {
2777 /* java is special; always tell parent */
2778 if (win
->transient
&& win
->java
)
2779 client_msg(find_window(win
->transient
),
2780 a_takefocus
, last_event_time
);
2782 client_msg(win
, a_takefocus
, last_event_time
);
2785 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2786 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2788 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2789 win
->ws
->always_raise
)
2792 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2793 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2800 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
2803 /* If a child window should have focus instead, return it. */
2805 get_focus_magic(struct ws_win
*win
)
2807 struct ws_win
*parent
= NULL
;
2808 struct ws_win
*child
= NULL
;
2810 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
2814 if (win
->transient
) {
2815 parent
= find_window(win
->transient
);
2817 /* If parent prefers focus elsewhere, then try to do so. */
2818 if (parent
&& (child
= parent
->focus_child
)) {
2819 if (validate_win(child
) == 0 && child
->mapped
)
2822 parent
->focus_child
= NULL
;
2826 /* If this window prefers focus elsewhere, then try to do so. */
2827 if ((child
= win
->focus_child
)) {
2828 if (validate_win(child
) == 0 && child
->mapped
)
2831 win
->focus_child
= NULL
;
2838 event_drain(uint8_t rt
)
2840 xcb_generic_event_t
*evt
;
2842 /* ensure all pending requests have been processed before filtering. */
2844 while ((evt
= xcb_poll_for_event(conn
))) {
2845 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2853 switchws(struct swm_region
*r
, union arg
*args
)
2855 int wsid
= args
->id
, unmap_old
= 0;
2856 struct swm_region
*this_r
, *other_r
;
2858 struct workspace
*new_ws
, *old_ws
;
2863 if (wsid
>= workspace_limit
)
2867 old_ws
= this_r
->ws
;
2868 new_ws
= &this_r
->s
->ws
[wsid
];
2870 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2871 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2873 if (new_ws
== NULL
|| old_ws
== NULL
)
2875 if (new_ws
== old_ws
)
2878 unfocus_win(old_ws
->focus
);
2880 other_r
= new_ws
->r
;
2881 if (other_r
== NULL
) {
2882 /* the other workspace is hidden, hide this one */
2886 /* the other ws is visible in another region, exchange them */
2887 other_r
->ws_prior
= new_ws
;
2888 other_r
->ws
= old_ws
;
2889 old_ws
->r
= other_r
;
2891 this_r
->ws_prior
= old_ws
;
2892 this_r
->ws
= new_ws
;
2897 /* unmap old windows */
2899 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2902 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
2903 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
2905 /* if workspaces were swapped, then don't wait to set focus */
2907 if (new_ws
->focus_pending
) {
2908 focus_win(new_ws
->focus_pending
);
2910 /* Empty region, focus on root. */
2911 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2912 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
2918 /* Clear bar if new ws is empty. */
2919 if (new_ws
->focus_pending
== NULL
)
2924 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
2928 cyclews(struct swm_region
*r
, union arg
*args
)
2931 struct swm_screen
*s
= r
->s
;
2934 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2935 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2940 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2943 case SWM_ARG_ID_CYCLEWS_UP
:
2944 if (a
.id
< workspace_limit
- 1)
2949 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2952 case SWM_ARG_ID_CYCLEWS_DOWN
:
2956 a
.id
= workspace_limit
- 1;
2963 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2965 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
2969 } while (a
.id
!= r
->ws
->idx
);
2973 priorws(struct swm_region
*r
, union arg
*args
)
2979 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2980 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2982 if (r
->ws_prior
== NULL
)
2985 a
.id
= r
->ws_prior
->idx
;
2990 cyclescr(struct swm_region
*r
, union arg
*args
)
2993 struct swm_region
*rr
= NULL
;
2996 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2997 /* do nothing if we don't have more than one screen */
2998 if (!(num_screens
> 1 || outputs
> 1))
3003 case SWM_ARG_ID_CYCLESC_UP
:
3004 rr
= TAILQ_NEXT(r
, entry
);
3006 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3008 case SWM_ARG_ID_CYCLESC_DOWN
:
3009 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3011 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3019 nfw
= get_region_focus(rr
);
3023 /* New region is empty; unfocus old region and warp pointer. */
3024 unfocus_win(r
->ws
->focus
);
3025 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3026 rr
->s
[i
].root
, XCB_CURRENT_TIME
);
3028 /* Clear bar since empty. */
3036 sort_windows(struct ws_win_list
*wl
)
3038 struct ws_win
*win
, *parent
, *nxt
;
3043 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3044 nxt
= TAILQ_NEXT(win
, entry
);
3045 if (win
->transient
) {
3046 parent
= find_window(win
->transient
);
3047 if (parent
== NULL
) {
3048 warnx("not possible bug");
3051 TAILQ_REMOVE(wl
, win
, entry
);
3052 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3059 swapwin(struct swm_region
*r
, union arg
*args
)
3061 struct ws_win
*target
, *source
;
3062 struct ws_win
*cur_focus
;
3063 struct ws_win_list
*wl
;
3065 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3066 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3068 cur_focus
= r
->ws
->focus
;
3069 if (cur_focus
== NULL
)
3073 wl
= &source
->ws
->winlist
;
3076 case SWM_ARG_ID_SWAPPREV
:
3077 if (source
->transient
)
3078 source
= find_window(source
->transient
);
3079 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3080 if (target
&& target
->transient
)
3081 target
= find_window(target
->transient
);
3082 TAILQ_REMOVE(wl
, source
, entry
);
3084 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3086 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3088 case SWM_ARG_ID_SWAPNEXT
:
3089 target
= TAILQ_NEXT(source
, entry
);
3090 /* move the parent and let the sort handle the move */
3091 if (source
->transient
)
3092 source
= find_window(source
->transient
);
3093 TAILQ_REMOVE(wl
, source
, entry
);
3095 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3097 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3099 case SWM_ARG_ID_SWAPMAIN
:
3100 target
= TAILQ_FIRST(wl
);
3101 if (target
== source
) {
3102 if (source
->ws
->focus_prev
!= NULL
&&
3103 source
->ws
->focus_prev
!= target
)
3104 source
= source
->ws
->focus_prev
;
3108 if (target
== NULL
|| source
== NULL
)
3110 source
->ws
->focus_prev
= target
;
3111 TAILQ_REMOVE(wl
, target
, entry
);
3112 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3113 TAILQ_REMOVE(wl
, source
, entry
);
3114 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3116 case SWM_ARG_ID_MOVELAST
:
3117 TAILQ_REMOVE(wl
, source
, entry
);
3118 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3121 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3133 get_focus_prev(struct ws_win
*win
)
3135 struct ws_win
*winfocus
= NULL
;
3136 struct ws_win
*cur_focus
= NULL
;
3137 struct ws_win_list
*wl
= NULL
;
3138 struct workspace
*ws
= NULL
;
3140 if (!(win
&& win
->ws
))
3145 cur_focus
= ws
->focus
;
3147 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3148 WINID(win
), WINID(cur_focus
));
3150 /* pickle, just focus on whatever */
3151 if (cur_focus
== NULL
) {
3152 /* use prev_focus if valid */
3153 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3154 find_window(WINID(ws
->focus_prev
)))
3155 winfocus
= ws
->focus_prev
;
3159 /* if transient focus on parent */
3160 if (cur_focus
->transient
) {
3161 winfocus
= find_window(cur_focus
->transient
);
3165 /* if in max_stack try harder */
3166 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3167 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3168 if (cur_focus
!= ws
->focus_prev
)
3169 winfocus
= ws
->focus_prev
;
3171 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3176 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3178 if (winfocus
== NULL
|| winfocus
== win
) {
3179 switch (focus_close
) {
3180 case SWM_STACK_BOTTOM
:
3181 winfocus
= TAILQ_FIRST(wl
);
3184 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3186 case SWM_STACK_ABOVE
:
3187 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3188 if (focus_close_wrap
)
3189 winfocus
= TAILQ_FIRST(wl
);
3191 winfocus
= TAILQ_PREV(cur_focus
,
3192 ws_win_list
, entry
);
3195 case SWM_STACK_BELOW
:
3196 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3198 if (focus_close_wrap
)
3199 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3201 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3207 if (winfocus
== NULL
) {
3208 if (focus_default
== SWM_STACK_TOP
)
3209 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3211 winfocus
= TAILQ_FIRST(wl
);
3216 return get_focus_magic(winfocus
);
3220 get_region_focus(struct swm_region
*r
)
3222 struct ws_win
*winfocus
= NULL
;
3227 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3228 winfocus
= r
->ws
->focus
;
3229 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3230 winfocus
= r
->ws
->focus_prev
;
3232 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3233 if (!winfocus
->iconic
)
3236 return get_focus_magic(winfocus
);
3240 focus(struct swm_region
*r
, union arg
*args
)
3242 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3243 struct ws_win_list
*wl
= NULL
;
3244 struct workspace
*ws
= NULL
;
3250 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3252 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3256 if (TAILQ_EMPTY(wl
))
3258 /* make sure there is at least one uniconified window */
3260 TAILQ_FOREACH(winfocus
, wl
, entry
)
3261 if (!winfocus
->iconic
) {
3269 case SWM_ARG_ID_FOCUSPREV
:
3270 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3272 head
= TAILQ_LAST(wl
, ws_win_list
);
3274 if (WINID(winfocus
) == cur_focus
->transient
) {
3275 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3277 head
= TAILQ_LAST(wl
, ws_win_list
);
3282 if (winfocus
&& winfocus
->iconic
) {
3283 while (winfocus
!= cur_focus
) {
3284 if (winfocus
== NULL
)
3285 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3286 if (!winfocus
->iconic
)
3288 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3294 case SWM_ARG_ID_FOCUSNEXT
:
3295 head
= TAILQ_NEXT(cur_focus
, entry
);
3297 head
= TAILQ_FIRST(wl
);
3301 if (winfocus
&& winfocus
->iconic
) {
3302 while (winfocus
!= cur_focus
) {
3303 if (winfocus
== NULL
)
3304 winfocus
= TAILQ_FIRST(wl
);
3305 if (!winfocus
->iconic
)
3307 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3312 case SWM_ARG_ID_FOCUSMAIN
:
3313 winfocus
= TAILQ_FIRST(wl
);
3314 if (winfocus
== cur_focus
)
3315 winfocus
= cur_focus
->ws
->focus_prev
;
3322 focus_win(get_focus_magic(winfocus
));
3328 cycle_layout(struct swm_region
*r
, union arg
*args
)
3330 struct workspace
*ws
= r
->ws
;
3332 /* suppress unused warning since var is needed */
3335 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3338 if (ws
->cur_layout
->l_stack
== NULL
)
3339 ws
->cur_layout
= &layouts
[0];
3344 focus_win(get_region_focus(r
));
3350 stack_config(struct swm_region
*r
, union arg
*args
)
3352 struct workspace
*ws
= r
->ws
;
3354 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3357 if (ws
->cur_layout
->l_config
!= NULL
)
3358 ws
->cur_layout
->l_config(ws
, args
->id
);
3360 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3369 struct swm_geometry g
;
3370 struct swm_region
*r
;
3376 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3378 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3379 for (i
= 0; i
< num_screens
; i
++) {
3383 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3384 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3385 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3387 /* start with screen geometry, adjust for bar */
3389 g
.w
-= 2 * border_width
;
3390 g
.h
-= 2 * border_width
;
3396 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3397 r
->ws
->cur_layout
->l_string(r
->ws
);
3398 /* save r so we can track region changes */
3405 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3409 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3411 if (win
== NULL
|| r
== NULL
)
3414 /* retain window geom and region geom */
3415 win
->g_float
= win
->g
;
3416 win
->g_float
.x
-= X(r
);
3417 win
->g_float
.y
-= Y(r
);
3418 win
->g_floatvalid
= 1;
3419 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3420 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3421 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3422 win
->g_float
.w
, win
->g_float
.h
);
3426 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3428 if (win
== NULL
|| r
== NULL
)
3431 if (win
->g_floatvalid
) {
3432 win
->g
= win
->g_float
;
3435 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3436 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3439 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3440 "is not set.\n", win
->id
);
3445 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3450 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3453 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3454 * geom on ws switches or return from max mode
3456 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3457 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3458 /* update geometry for the new region */
3459 load_float_geom(win
, r
);
3462 win
->floatmaxed
= 0;
3465 * if set to fullscreen mode, configure window to maximum size.
3467 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3468 if (!win
->g_floatvalid
)
3469 store_float_geom(win
, win
->ws
->r
);
3475 * remove border on fullscreen floater when in fullscreen mode or when
3476 * the quirk is present.
3478 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3479 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3480 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3481 if (win
->bordered
) {
3483 X(win
) += border_width
;
3484 Y(win
) += border_width
;
3486 } else if (!win
->bordered
) {
3488 X(win
) -= border_width
;
3489 Y(win
) -= border_width
;
3492 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3493 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3494 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3497 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3498 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3500 * floaters and transients are auto-centred unless moved,
3501 * resized or ANYWHERE quirk is set.
3503 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3504 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3506 store_float_geom(win
, r
);
3509 /* keep window within region bounds */
3510 constrain_window(win
, r
, 0);
3516 * Send keystrokes to terminal to decrease/increase the font size as the
3517 * window size changes.
3520 adjust_font(struct ws_win
*win
)
3522 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3523 win
->floating
|| win
->transient
)
3526 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3527 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3528 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3529 win
->font_size_boundary
[win
->font_steps
] =
3530 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3533 win
->last_inc
= win
->sh
.width_inc
;
3534 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3535 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3536 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3539 win
->last_inc
= win
->sh
.width_inc
;
3540 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3544 #define SWAPXY(g) do { \
3546 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3547 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3550 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3552 struct swm_geometry win_g
, r_g
= *g
;
3553 struct ws_win
*win
, *fs_win
= NULL
;
3554 int i
, j
, s
, stacks
;
3555 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3556 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3557 int split
, colno
, winno
, mwin
, msize
, mscale
;
3558 int remain
, missing
, v_slice
, reconfigure
= 0;
3561 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3562 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3564 winno
= count_win(ws
, 0);
3565 if (winno
== 0 && count_win(ws
, 1) == 0)
3568 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3569 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
3576 w_inc
= win
->sh
.width_inc
;
3577 w_base
= win
->sh
.base_width
;
3578 mwin
= ws
->l_state
.horizontal_mwin
;
3579 mscale
= ws
->l_state
.horizontal_msize
;
3580 stacks
= ws
->l_state
.horizontal_stacks
;
3583 w_inc
= win
->sh
.height_inc
;
3584 w_base
= win
->sh
.base_height
;
3585 mwin
= ws
->l_state
.vertical_mwin
;
3586 mscale
= ws
->l_state
.vertical_msize
;
3587 stacks
= ws
->l_state
.vertical_stacks
;
3591 if (stacks
> winno
- mwin
)
3592 stacks
= winno
- mwin
;
3596 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3597 if (mwin
&& winno
> mwin
) {
3598 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3602 win_g
.w
= v_slice
* mscale
;
3604 if (w_inc
> 1 && w_inc
< v_slice
) {
3605 /* adjust for window's requested size increment */
3606 remain
= (win_g
.w
- w_base
) % w_inc
;
3612 win_g
.x
+= r_g
.w
- msize
;
3615 colno
= split
= winno
/ stacks
;
3616 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3617 2 * border_width
) / stacks
);
3619 hrh
= r_g
.h
/ colno
;
3620 extra
= r_g
.h
- (colno
* hrh
);
3621 win_g
.h
= hrh
- 2 * border_width
;
3623 /* stack all the tiled windows */
3624 i
= j
= 0, s
= stacks
;
3625 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3626 if (win
->transient
|| win
->floating
)
3631 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3636 if (split
&& i
== split
) {
3637 colno
= (winno
- mwin
) / stacks
;
3638 if (s
<= (winno
- mwin
) % stacks
)
3640 split
= split
+ colno
;
3641 hrh
= (r_g
.h
/ colno
);
3642 extra
= r_g
.h
- (colno
* hrh
);
3646 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3647 win_g
.w
= (r_g
.w
- msize
-
3648 (stacks
* 2 * border_width
)) / stacks
;
3650 win_g
.w
+= (r_g
.w
- msize
-
3651 (stacks
* 2 * border_width
)) % stacks
;
3655 win_g
.h
= hrh
- 2 * border_width
;
3657 h_inc
= win
->sh
.width_inc
;
3658 h_base
= win
->sh
.base_width
;
3660 h_inc
= win
->sh
.height_inc
;
3661 h_base
= win
->sh
.base_height
;
3663 if (j
== colno
- 1) {
3664 win_g
.h
= hrh
+ extra
;
3665 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3666 /* adjust for window's requested size increment */
3667 remain
= (win_g
.h
- h_base
) % h_inc
;
3668 missing
= h_inc
- remain
;
3670 if (missing
<= extra
|| j
== 0) {
3682 win_g
.y
+= last_h
+ 2 * border_width
;
3684 if (disable_border
&& !bar_enabled
&& winno
== 1){
3686 win_g
.w
+= 2 * border_width
;
3687 win_g
.h
+= 2 * border_width
;
3692 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3693 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3697 WIDTH(win
) = win_g
.h
;
3698 HEIGHT(win
) = win_g
.w
;
3701 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3702 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3706 WIDTH(win
) = win_g
.w
;
3707 HEIGHT(win
) = win_g
.h
;
3711 if (bordered
!= win
->bordered
) {
3713 win
->bordered
= bordered
;
3729 /* now, stack all the floaters and transients */
3730 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3731 if (!win
->transient
&& !win
->floating
)
3735 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3740 stack_floater(win
, ws
->r
);
3745 stack_floater(fs_win
, ws
->r
);
3751 vertical_config(struct workspace
*ws
, int id
)
3753 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3757 case SWM_ARG_ID_STACKRESET
:
3758 case SWM_ARG_ID_STACKINIT
:
3759 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3760 ws
->l_state
.vertical_mwin
= 1;
3761 ws
->l_state
.vertical_stacks
= 1;
3763 case SWM_ARG_ID_MASTERSHRINK
:
3764 if (ws
->l_state
.vertical_msize
> 1)
3765 ws
->l_state
.vertical_msize
--;
3767 case SWM_ARG_ID_MASTERGROW
:
3768 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3769 ws
->l_state
.vertical_msize
++;
3771 case SWM_ARG_ID_MASTERADD
:
3772 ws
->l_state
.vertical_mwin
++;
3774 case SWM_ARG_ID_MASTERDEL
:
3775 if (ws
->l_state
.vertical_mwin
> 0)
3776 ws
->l_state
.vertical_mwin
--;
3778 case SWM_ARG_ID_STACKINC
:
3779 ws
->l_state
.vertical_stacks
++;
3781 case SWM_ARG_ID_STACKDEC
:
3782 if (ws
->l_state
.vertical_stacks
> 1)
3783 ws
->l_state
.vertical_stacks
--;
3785 case SWM_ARG_ID_FLIPLAYOUT
:
3786 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3794 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3796 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3798 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3802 horizontal_config(struct workspace
*ws
, int id
)
3804 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3807 case SWM_ARG_ID_STACKRESET
:
3808 case SWM_ARG_ID_STACKINIT
:
3809 ws
->l_state
.horizontal_mwin
= 1;
3810 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3811 ws
->l_state
.horizontal_stacks
= 1;
3813 case SWM_ARG_ID_MASTERSHRINK
:
3814 if (ws
->l_state
.horizontal_msize
> 1)
3815 ws
->l_state
.horizontal_msize
--;
3817 case SWM_ARG_ID_MASTERGROW
:
3818 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3819 ws
->l_state
.horizontal_msize
++;
3821 case SWM_ARG_ID_MASTERADD
:
3822 ws
->l_state
.horizontal_mwin
++;
3824 case SWM_ARG_ID_MASTERDEL
:
3825 if (ws
->l_state
.horizontal_mwin
> 0)
3826 ws
->l_state
.horizontal_mwin
--;
3828 case SWM_ARG_ID_STACKINC
:
3829 ws
->l_state
.horizontal_stacks
++;
3831 case SWM_ARG_ID_STACKDEC
:
3832 if (ws
->l_state
.horizontal_stacks
> 1)
3833 ws
->l_state
.horizontal_stacks
--;
3835 case SWM_ARG_ID_FLIPLAYOUT
:
3836 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3844 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3846 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3848 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3851 /* fullscreen view */
3853 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3855 struct swm_geometry gg
= *g
;
3856 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3857 int winno
, num_screens
;
3859 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3864 winno
= count_win(ws
, 0);
3865 if (winno
== 0 && count_win(ws
, 1) == 0)
3868 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3869 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3870 if (win
->transient
) {
3872 parent
= find_window(win
->transient
);
3876 if (win
->floating
&& !win
->floatmaxed
) {
3878 * retain geometry for retrieval on exit from
3881 store_float_geom(win
, ws
->r
);
3882 win
->floatmaxed
= 1;
3885 /* only reconfigure if necessary */
3886 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3887 HEIGHT(win
) != gg
.h
) {
3893 WIDTH(win
) += 2 * border_width
;
3894 HEIGHT(win
) += 2 * border_width
;
3899 /* unmap only if we don't have multi screen */
3900 if (win
!= ws
->focus
)
3901 if (!(num_screens
> 1 || outputs
> 1))
3905 /* put the last transient on top */
3909 stack_floater(wintrans
, ws
->r
);
3910 ws
->focus
= get_focus_magic(wintrans
);
3915 send_to_ws(struct swm_region
*r
, union arg
*args
)
3917 int wsid
= args
->id
;
3918 struct ws_win
*win
= NULL
, *parent
;
3919 struct workspace
*ws
, *nws
, *pws
;
3920 char ws_idx_str
[SWM_PROPLEN
];
3922 if (wsid
>= workspace_limit
)
3925 if (r
&& r
->ws
&& r
->ws
->focus
)
3930 if (win
->ws
->idx
== wsid
)
3933 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
3934 win
->ws
->idx
, wsid
);
3937 nws
= &win
->s
->ws
[wsid
];
3939 /* Update the window's workspace property: _SWM_WS */
3940 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
3941 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3942 ws
->focus_pending
= get_focus_prev(win
);
3944 /* Move the parent if this is a transient window. */
3945 if (win
->transient
) {
3946 parent
= find_window(win
->transient
);
3949 /* Set new focus in parent's ws if needed. */
3950 if (pws
->focus
== parent
) {
3951 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3952 pws
->focus_pending
=
3953 get_focus_prev(parent
);
3955 unfocus_win(parent
);
3957 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3958 pws
->focus
= pws
->focus_pending
;
3960 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3961 pws
->focus_pending
= NULL
;
3964 /* Don't unmap parent if new ws is visible */
3966 unmap_window(parent
);
3969 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3970 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3973 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
3974 "property: _SWM_WS: %s\n", ws_idx_str
);
3975 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
3976 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
3977 strlen(ws_idx_str
), ws_idx_str
);
3983 /* Don't unmap if new ws is visible */
3988 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3989 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3992 /* Set focus on new ws. */
3993 unfocus_win(nws
->focus
);
3996 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3998 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3999 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4002 /* Restack and set new focus. */
4005 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4006 focus_win(ws
->focus_pending
);
4007 ws
->focus_pending
= NULL
;
4013 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4017 pressbutton(struct swm_region
*r
, union arg
*args
)
4019 /* suppress unused warning since var is needed */
4022 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4023 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4024 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4025 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4029 raise_toggle(struct swm_region
*r
, union arg
*args
)
4031 /* suppress unused warning since var is needed */
4034 if (r
== NULL
|| r
->ws
== NULL
)
4037 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4039 /* bring floaters back to top */
4040 if (!r
->ws
->always_raise
)
4047 iconify(struct swm_region
*r
, union arg
*args
)
4049 /* suppress unused warning since var is needed */
4052 if (r
->ws
->focus
== NULL
)
4055 set_swm_iconic(r
->ws
->focus
, 1);
4061 get_win_name(xcb_window_t win
)
4064 xcb_get_property_cookie_t c
;
4065 xcb_get_property_reply_t
*r
;
4067 /* First try _NET_WM_NAME for UTF-8. */
4068 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4069 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4070 r
= xcb_get_property_reply(conn
, c
, NULL
);
4072 if (!r
|| r
->type
== XCB_NONE
) {
4074 /* Use WM_NAME instead; no UTF-8. */
4075 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4076 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4077 r
= xcb_get_property_reply(conn
, c
, NULL
);
4079 if(!r
|| r
->type
== XCB_NONE
) {
4086 name
= strndup(xcb_get_property_value(r
),
4087 xcb_get_property_value_length(r
));
4094 uniconify(struct swm_region
*r
, union arg
*args
)
4101 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4103 if (r
== NULL
|| r
->ws
== NULL
)
4106 /* make sure we have anything to uniconify */
4107 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4108 if (win
->ws
== NULL
)
4109 continue; /* should never happen */
4118 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4120 spawn_select(r
, args
, "search", &searchpid
);
4122 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4125 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4126 if (win
->ws
== NULL
)
4127 continue; /* should never happen */
4131 name
= get_win_name(win
->id
);
4134 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4142 name_workspace(struct swm_region
*r
, union arg
*args
)
4146 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4152 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4154 spawn_select(r
, args
, "name_workspace", &searchpid
);
4156 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4159 fprintf(lfile
, "%s", "");
4164 search_workspace(struct swm_region
*r
, union arg
*args
)
4167 struct workspace
*ws
;
4170 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4176 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4178 spawn_select(r
, args
, "search", &searchpid
);
4180 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4183 for (i
= 0; i
< workspace_limit
; i
++) {
4187 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4188 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4195 search_win_cleanup(void)
4197 struct search_window
*sw
= NULL
;
4199 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4200 xcb_destroy_window(conn
, sw
->indicator
);
4201 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4207 search_win(struct swm_region
*r
, union arg
*args
)
4209 struct ws_win
*win
= NULL
;
4210 struct search_window
*sw
= NULL
;
4213 int i
, width
, height
;
4221 XRectangle l_ibox
, l_lbox
;
4223 DNPRINTF(SWM_D_MISC
, "search_win\n");
4226 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4228 spawn_select(r
, args
, "search", &searchpid
);
4230 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4233 TAILQ_INIT(&search_wl
);
4236 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4240 sw
= calloc(1, sizeof(struct search_window
));
4242 warn("search_win: calloc");
4244 search_win_cleanup();
4250 snprintf(s
, sizeof s
, "%d", i
);
4253 w
= xcb_generate_id(conn
);
4254 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4255 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4257 if (bar_font_legacy
) {
4258 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4259 width
= l_lbox
.width
+ 4;
4260 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4262 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4264 width
= info
.width
+ 4;
4265 height
= bar_font
->height
+ 4;
4268 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4269 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4270 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4271 XCB_CW_BORDER_PIXEL
, wa
);
4273 xcb_map_window(conn
, w
);
4276 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4278 if (bar_font_legacy
) {
4279 l_gcv
.graphics_exposures
= 0;
4280 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4282 XSetForeground(display
, l_draw
,
4283 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4285 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4286 (bar_fs_extents
->max_logical_extent
.height
-
4287 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4289 XFreeGC(display
, l_draw
);
4292 draw
= XftDrawCreate(display
, w
,
4293 DefaultVisual(display
, r
->s
->idx
),
4294 DefaultColormap(display
, r
->s
->idx
));
4296 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4297 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4298 bar_font
->descent
, (FcChar8
*)s
, len
);
4300 XftDrawDestroy(draw
);
4303 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4305 fprintf(lfile
, "%d\n", i
);
4315 search_resp_uniconify(char *resp
, unsigned long len
)
4321 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4323 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4326 name
= get_win_name(win
->id
);
4329 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4334 if (strncmp(s
, resp
, len
) == 0) {
4335 /* XXX this should be a callback to generalize */
4336 set_swm_iconic(win
, 0);
4346 search_resp_name_workspace(char *resp
, unsigned long len
)
4348 struct workspace
*ws
;
4350 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4352 if (search_r
->ws
== NULL
)
4357 free(search_r
->ws
->name
);
4358 search_r
->ws
->name
= NULL
;
4362 ws
->name
= strdup(resp
);
4363 if (ws
->name
== NULL
) {
4364 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4365 "strdup: %s", strerror(errno
));
4372 search_resp_search_workspace(char *resp
)
4379 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4383 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4390 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4392 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4399 switchws(search_r
, &a
);
4403 search_resp_search_window(char *resp
)
4408 struct search_window
*sw
;
4410 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4414 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4419 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4421 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4428 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4429 if (idx
== sw
->idx
) {
4435 #define MAX_RESP_LEN 1024
4438 search_do_resp(void)
4444 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4449 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4450 warn("search: calloc");
4454 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4456 warn("search: read error");
4459 resp
[rbytes
] = '\0';
4462 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4463 * newline, so work around that by sanitizing the resp now.
4465 resp
[strcspn(resp
, "\n")] = '\0';
4468 switch (search_resp_action
) {
4469 case SWM_SEARCH_UNICONIFY
:
4470 search_resp_uniconify(resp
, len
);
4472 case SWM_SEARCH_NAME_WORKSPACE
:
4473 search_resp_name_workspace(resp
, len
);
4475 case SWM_SEARCH_SEARCH_WORKSPACE
:
4476 search_resp_search_workspace(resp
);
4478 case SWM_SEARCH_SEARCH_WINDOW
:
4479 search_resp_search_window(resp
);
4484 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4485 search_win_cleanup();
4487 search_resp_action
= SWM_SEARCH_NONE
;
4488 close(select_resp_pipe
[0]);
4495 wkill(struct swm_region
*r
, union arg
*args
)
4497 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4499 if (r
->ws
->focus
== NULL
)
4502 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4503 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4505 if (r
->ws
->focus
->can_delete
)
4506 client_msg(r
->ws
->focus
, a_delete
, 0);
4512 floating_toggle_win(struct ws_win
*win
)
4514 struct swm_region
*r
;
4524 /* reject floating toggles in max stack mode */
4525 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4528 if (win
->floating
) {
4529 if (!win
->floatmaxed
) {
4530 /* retain position for refloat */
4531 store_float_geom(win
, r
);
4535 load_float_geom(win
, r
);
4539 ewmh_update_actions(win
);
4545 floating_toggle(struct swm_region
*r
, union arg
*args
)
4547 struct ws_win
*win
= r
->ws
->focus
;
4549 /* suppress unused warning since var is needed */
4555 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4558 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4559 _NET_WM_STATE_TOGGLE
);
4563 if (win
== win
->ws
->focus
)
4570 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4572 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4574 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4576 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4579 if (X(win
) + BORDER(win
) < X(r
)) {
4581 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4583 X(win
) = X(r
) - BORDER(win
);
4586 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4588 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4590 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4593 if (Y(win
) + BORDER(win
) < Y(r
)) {
4595 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4597 Y(win
) = Y(r
) - BORDER(win
);
4603 if (HEIGHT(win
) < 1)
4609 update_window(struct ws_win
*win
)
4614 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4615 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4616 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4620 wc
[3] = HEIGHT(win
);
4621 wc
[4] = BORDER(win
);
4623 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4624 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4625 wc
[3], YESNO(win
->bordered
));
4627 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4630 #define SWM_RESIZE_STEPS (50)
4633 resize(struct ws_win
*win
, union arg
*args
)
4635 xcb_timestamp_t timestamp
= 0;
4636 struct swm_region
*r
= NULL
;
4637 int resize_step
= 0;
4638 struct swm_geometry g
;
4639 int top
= 0, left
= 0, resizing
;
4641 unsigned int shape
; /* cursor style */
4642 xcb_cursor_t cursor
;
4643 xcb_font_t cursor_font
;
4644 xcb_query_pointer_reply_t
*xpr
;
4645 xcb_generic_event_t
*evt
;
4646 xcb_motion_notify_event_t
*mne
;
4652 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4655 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4656 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4659 if (!win
->transient
&& !win
->floating
)
4662 /* reject resizes in max mode for floaters (transient ok) */
4663 if (win
->floatmaxed
)
4667 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4675 case SWM_ARG_ID_WIDTHSHRINK
:
4676 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4679 case SWM_ARG_ID_WIDTHGROW
:
4680 WIDTH(win
) += SWM_RESIZE_STEPS
;
4683 case SWM_ARG_ID_HEIGHTSHRINK
:
4684 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4687 case SWM_ARG_ID_HEIGHTGROW
:
4688 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4695 constrain_window(win
, r
, 1);
4697 store_float_geom(win
,r
);
4701 /* get cursor offset from window root */
4702 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4709 if (xpr
->win_x
< WIDTH(win
) / 2)
4712 if (xpr
->win_y
< HEIGHT(win
) / 2)
4715 if (args
->id
== SWM_ARG_ID_CENTER
)
4718 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4720 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4722 cursor_font
= xcb_generate_id(conn
);
4723 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4725 cursor
= xcb_generate_id(conn
);
4726 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4727 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4729 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4730 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4735 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4736 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4737 case XCB_BUTTON_RELEASE
:
4738 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4741 case XCB_MOTION_NOTIFY
:
4742 mne
= (xcb_motion_notify_event_t
*)evt
;
4743 /* cursor offset/delta from start of the operation */
4744 dx
= mne
->root_x
- xpr
->root_x
;
4745 dy
= mne
->root_y
- xpr
->root_y
;
4751 if (args
->id
== SWM_ARG_ID_CENTER
) {
4752 if (g
.h
/ 2 + dy
< 1)
4756 HEIGHT(win
) = g
.h
+ 2 * dy
;
4764 HEIGHT(win
) = g
.h
+ dy
;
4771 if (args
->id
== SWM_ARG_ID_CENTER
) {
4772 if (g
.w
/ 2 + dx
< 1)
4776 WIDTH(win
) = g
.w
+ 2 * dx
;
4784 WIDTH(win
) = g
.w
+ dx
;
4787 constrain_window(win
, r
, 1);
4789 /* not free, don't sync more than 120 times / second */
4790 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4791 timestamp
= mne
->time
;
4806 store_float_geom(win
,r
);
4808 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4809 xcb_free_cursor(conn
, cursor
);
4810 xcb_close_font(conn
, cursor_font
);
4812 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
4816 resize_step(struct swm_region
*r
, union arg
*args
)
4818 struct ws_win
*win
= NULL
;
4820 if (r
&& r
->ws
&& r
->ws
->focus
)
4828 #define SWM_MOVE_STEPS (50)
4831 move(struct ws_win
*win
, union arg
*args
)
4833 xcb_timestamp_t timestamp
= 0;
4834 int move_step
= 0, moving
;
4835 struct swm_region
*r
= NULL
;
4836 xcb_font_t cursor_font
;
4837 xcb_cursor_t cursor
;
4838 xcb_query_pointer_reply_t
*qpr
;
4839 xcb_generic_event_t
*evt
;
4840 xcb_motion_notify_event_t
*mne
;
4846 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4849 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4850 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4852 /* in max_stack mode should only move transients */
4853 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4857 if (!win
->floating
&& !win
->transient
) {
4858 store_float_geom(win
, r
);
4859 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4862 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4871 case SWM_ARG_ID_MOVELEFT
:
4872 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4875 case SWM_ARG_ID_MOVERIGHT
:
4876 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4879 case SWM_ARG_ID_MOVEUP
:
4880 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4883 case SWM_ARG_ID_MOVEDOWN
:
4884 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4891 constrain_window(win
, r
, 0);
4893 store_float_geom(win
, r
);
4897 cursor_font
= xcb_generate_id(conn
);
4898 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4900 cursor
= xcb_generate_id(conn
);
4901 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4902 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4904 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4905 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4906 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4908 /* get cursor offset from window root */
4909 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4912 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4913 xcb_free_cursor(conn
, cursor
);
4914 xcb_close_font(conn
, cursor_font
);
4920 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4921 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4922 case XCB_BUTTON_RELEASE
:
4923 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4926 case XCB_MOTION_NOTIFY
:
4927 mne
= (xcb_motion_notify_event_t
*)evt
;
4928 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4929 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4931 constrain_window(win
, r
, 0);
4933 /* not free, don't sync more than 120 times / second */
4934 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4935 timestamp
= mne
->time
;
4950 store_float_geom(win
, r
);
4952 xcb_free_cursor(conn
, cursor
);
4953 xcb_close_font(conn
, cursor_font
);
4954 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4955 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
4959 move_step(struct swm_region
*r
, union arg
*args
)
4961 struct ws_win
*win
= NULL
;
4963 if (r
&& r
->ws
&& r
->ws
->focus
)
4968 if (!win
->transient
&& !win
->floating
)
4974 /* user/key callable function IDs */
5024 KF_SEARCH_WORKSPACE
,
5065 KF_DUMPWINS
, /* MUST BE LAST */
5069 /* key definitions */
5071 char name
[SWM_FUNCNAME_LEN
];
5072 void (*func
)(struct swm_region
*r
, union arg
*);
5074 } keyfuncs
[KF_INVALID
+ 1] = {
5075 /* name function argument */
5076 { "bar_toggle", bar_toggle
, {0} },
5077 { "button2", pressbutton
, {2} },
5078 { "cycle_layout", cycle_layout
, {0} },
5079 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5080 { "float_toggle", floating_toggle
,{0} },
5081 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5082 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5083 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5084 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5085 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5086 { "iconify", iconify
, {0} },
5087 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5088 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5089 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5090 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5091 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5092 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5093 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5094 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5095 { "mvws_1", send_to_ws
, {.id
= 0} },
5096 { "mvws_2", send_to_ws
, {.id
= 1} },
5097 { "mvws_3", send_to_ws
, {.id
= 2} },
5098 { "mvws_4", send_to_ws
, {.id
= 3} },
5099 { "mvws_5", send_to_ws
, {.id
= 4} },
5100 { "mvws_6", send_to_ws
, {.id
= 5} },
5101 { "mvws_7", send_to_ws
, {.id
= 6} },
5102 { "mvws_8", send_to_ws
, {.id
= 7} },
5103 { "mvws_9", send_to_ws
, {.id
= 8} },
5104 { "mvws_10", send_to_ws
, {.id
= 9} },
5105 { "mvws_11", send_to_ws
, {.id
= 10} },
5106 { "mvws_12", send_to_ws
, {.id
= 11} },
5107 { "mvws_13", send_to_ws
, {.id
= 12} },
5108 { "mvws_14", send_to_ws
, {.id
= 13} },
5109 { "mvws_15", send_to_ws
, {.id
= 14} },
5110 { "mvws_16", send_to_ws
, {.id
= 15} },
5111 { "mvws_17", send_to_ws
, {.id
= 16} },
5112 { "mvws_18", send_to_ws
, {.id
= 17} },
5113 { "mvws_19", send_to_ws
, {.id
= 18} },
5114 { "mvws_20", send_to_ws
, {.id
= 19} },
5115 { "mvws_21", send_to_ws
, {.id
= 20} },
5116 { "mvws_22", send_to_ws
, {.id
= 21} },
5117 { "name_workspace", name_workspace
, {0} },
5118 { "quit", quit
, {0} },
5119 { "raise_toggle", raise_toggle
, {0} },
5120 { "restart", restart
, {0} },
5121 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5122 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5123 { "search_win", search_win
, {0} },
5124 { "search_workspace", search_workspace
, {0} },
5125 { "spawn_custom", NULL
, {0} },
5126 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5127 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5128 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5129 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5130 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5131 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5132 { "uniconify", uniconify
, {0} },
5133 { "version", version
, {0} },
5134 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5135 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5136 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5137 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5138 { "ws_1", switchws
, {.id
= 0} },
5139 { "ws_2", switchws
, {.id
= 1} },
5140 { "ws_3", switchws
, {.id
= 2} },
5141 { "ws_4", switchws
, {.id
= 3} },
5142 { "ws_5", switchws
, {.id
= 4} },
5143 { "ws_6", switchws
, {.id
= 5} },
5144 { "ws_7", switchws
, {.id
= 6} },
5145 { "ws_8", switchws
, {.id
= 7} },
5146 { "ws_9", switchws
, {.id
= 8} },
5147 { "ws_10", switchws
, {.id
= 9} },
5148 { "ws_11", switchws
, {.id
= 10} },
5149 { "ws_12", switchws
, {.id
= 11} },
5150 { "ws_13", switchws
, {.id
= 12} },
5151 { "ws_14", switchws
, {.id
= 13} },
5152 { "ws_15", switchws
, {.id
= 14} },
5153 { "ws_16", switchws
, {.id
= 15} },
5154 { "ws_17", switchws
, {.id
= 16} },
5155 { "ws_18", switchws
, {.id
= 17} },
5156 { "ws_19", switchws
, {.id
= 18} },
5157 { "ws_20", switchws
, {.id
= 19} },
5158 { "ws_21", switchws
, {.id
= 20} },
5159 { "ws_22", switchws
, {.id
= 21} },
5160 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5161 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5162 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5163 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5164 { "ws_prior", priorws
, {0} },
5165 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5166 { "invalid key func", NULL
, {0} },
5169 RB_ENTRY(key
) entry
;
5172 enum keyfuncid funcid
;
5175 RB_HEAD(key_tree
, key
);
5178 key_cmp(struct key
*kp1
, struct key
*kp2
)
5180 if (kp1
->keysym
< kp2
->keysym
)
5182 if (kp1
->keysym
> kp2
->keysym
)
5185 if (kp1
->mod
< kp2
->mod
)
5187 if (kp1
->mod
> kp2
->mod
)
5193 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
5194 struct key_tree keys
;
5197 enum { client_click
, root_click
};
5199 unsigned int action
;
5201 unsigned int button
;
5202 void (*func
)(struct ws_win
*, union arg
*);
5205 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5206 /* action key mouse button func args */
5207 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5208 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5209 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5214 update_modkey(unsigned int mod
)
5220 RB_FOREACH(kp
, key_tree
, &keys
)
5221 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5222 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5226 for (i
= 0; i
< LENGTH(buttons
); i
++)
5227 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5228 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5230 buttons
[i
].mask
= mod
;
5235 TAILQ_ENTRY(spawn_prog
) entry
;
5240 TAILQ_HEAD(spawn_list
, spawn_prog
);
5241 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
5244 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
5247 struct spawn_prog
*prog
= NULL
;
5249 char *ap
, **real_args
;
5251 /* suppress unused warning since var is needed */
5254 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5257 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5258 if (!strcasecmp(spawn_name
, prog
->name
))
5262 warnx("spawn_custom: program %s not found", spawn_name
);
5266 /* make room for expanded args */
5267 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5268 err(1, "spawn_custom: calloc real_args");
5270 /* expand spawn_args into real_args */
5271 for (i
= 0; i
< prog
->argc
; i
++) {
5273 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5274 if (!strcasecmp(ap
, "$bar_border")) {
5276 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5278 err(1, "spawn_custom border color");
5279 } else if (!strcasecmp(ap
, "$bar_color")) {
5281 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5283 err(1, "spawn_custom bar color");
5284 } else if (!strcasecmp(ap
, "$bar_font")) {
5285 if ((real_args
[i
] = strdup(bar_fonts
))
5287 err(1, "spawn_custom bar fonts");
5288 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5290 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5292 err(1, "spawn_custom color font");
5293 } else if (!strcasecmp(ap
, "$color_focus")) {
5295 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5297 err(1, "spawn_custom color focus");
5298 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5300 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5302 err(1, "spawn_custom color unfocus");
5304 /* no match --> copy as is */
5305 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5306 err(1, "spawn_custom strdup(ap)");
5308 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5313 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5314 for (i
= 0; i
< prog
->argc
; i
++)
5315 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5316 DNPRINTF(SWM_D_SPAWN
, "\n");
5318 *ret_args
= real_args
;
5319 return (prog
->argc
);
5323 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
5329 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5333 spawn(r
->ws
->idx
, &a
, 1);
5335 for (i
= 0; i
< spawn_argc
; i
++)
5341 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5347 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5351 if (pipe(select_list_pipe
) == -1)
5352 err(1, "pipe error");
5353 if (pipe(select_resp_pipe
) == -1)
5354 err(1, "pipe error");
5356 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5357 err(1, "could not disable SIGPIPE");
5358 switch (*pid
= fork()) {
5360 err(1, "cannot fork");
5363 if (dup2(select_list_pipe
[0], 0) == -1)
5365 if (dup2(select_resp_pipe
[1], 1) == -1)
5367 close(select_list_pipe
[1]);
5368 close(select_resp_pipe
[0]);
5369 spawn(r
->ws
->idx
, &a
, 0);
5371 default: /* parent */
5372 close(select_list_pipe
[0]);
5373 close(select_resp_pipe
[1]);
5377 for (i
= 0; i
< spawn_argc
; i
++)
5383 spawn_insert(char *name
, char *args
)
5385 char *arg
, *cp
, *ptr
;
5386 struct spawn_prog
*sp
;
5388 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5390 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5391 err(1, "spawn_insert: calloc");
5392 if ((sp
->name
= strdup(name
)) == NULL
)
5393 err(1, "spawn_insert: strdup");
5395 /* convert the arguments to an argument list */
5396 if ((ptr
= cp
= strdup(args
)) == NULL
)
5397 err(1, "spawn_insert: strdup");
5398 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5399 /* empty field; skip it */
5404 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5405 sizeof *sp
->argv
)) == NULL
)
5406 err(1, "spawn_insert: realloc");
5407 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5408 err(1, "spawn_insert: strdup");
5412 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5413 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5417 spawn_remove(struct spawn_prog
*sp
)
5421 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5423 TAILQ_REMOVE(&spawns
, sp
, entry
);
5424 for (i
= 0; i
< sp
->argc
; i
++)
5430 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5434 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5436 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5439 spawn_insert(name
, args
);
5441 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5445 setspawn(char *name
, char *args
)
5447 struct spawn_prog
*sp
;
5449 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5454 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5455 if (!strcmp(sp
->name
, name
)) {
5459 spawn_replace(sp
, name
, args
);
5460 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5464 if (*args
== '\0') {
5465 warnx("error: setspawn: cannot find program: %s", name
);
5469 spawn_insert(name
, args
);
5470 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5474 setconfspawn(char *selector
, char *value
, int flags
)
5478 /* suppress unused warning since var is needed */
5481 args
= expand_tilde(value
);
5483 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5485 setspawn(selector
, args
);
5488 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
5495 setconfspawn("term", "xterm", 0);
5496 setconfspawn("spawn_term", "xterm", 0);
5497 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5498 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5499 setconfspawn("lock", "xlock", 0);
5500 setconfspawn("initscr", "initscreen.sh", 0);
5501 setconfspawn("menu", "dmenu_run"
5504 " -nf $bar_font_color"
5506 " -sf $bar_color", 0);
5507 setconfspawn("search", "dmenu"
5511 " -nf $bar_font_color"
5513 " -sf $bar_color", 0);
5514 setconfspawn("name_workspace", "dmenu"
5518 " -nf $bar_font_color"
5520 " -sf $bar_color", 0);
5524 #define SWM_MODNAME_SIZE 32
5525 #define SWM_KEY_WS "\n+ \t"
5527 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5531 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5532 if (mod
== NULL
|| ks
== NULL
) {
5533 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5536 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5537 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5541 *ks
= XCB_NO_SYMBOL
;
5543 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5544 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5546 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5547 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5549 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5550 *mod
|= XCB_MOD_MASK_1
;
5551 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5552 *mod
+= XCB_MOD_MASK_2
;
5553 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5554 *mod
|= XCB_MOD_MASK_3
;
5555 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5556 *mod
|= XCB_MOD_MASK_4
;
5557 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5558 *mod
|= XCB_MOD_MASK_SHIFT
;
5559 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5560 *mod
|= XCB_MOD_MASK_CONTROL
;
5562 *ks
= XStringToKeysym(name
);
5563 XConvertCase(*ks
, ks
, &uks
);
5564 if (ks
== XCB_NO_SYMBOL
) {
5566 "parsekeys: invalid key %s\n",
5572 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5577 strdupsafe(char *str
)
5582 return (strdup(str
));
5586 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5590 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5591 keyfuncs
[kfid
].name
, spawn_name
);
5593 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5594 err(1, "key_insert: malloc");
5599 kp
->spawn_name
= strdupsafe(spawn_name
);
5600 RB_INSERT(key_tree
, &keys
, kp
);
5602 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5606 key_lookup(unsigned int mod
, KeySym ks
)
5613 return (RB_FIND(key_tree
, &keys
, &kp
));
5617 key_remove(struct key
*kp
)
5619 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5621 RB_REMOVE(key_tree
, &keys
, kp
);
5622 free(kp
->spawn_name
);
5625 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5629 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5632 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5636 key_insert(mod
, ks
, kfid
, spawn_name
);
5638 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5642 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5647 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5648 keyfuncs
[kfid
].name
, spawn_name
);
5650 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5651 if (kfid
== KF_INVALID
)
5654 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5655 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5658 if (kfid
== KF_INVALID
) {
5659 warnx("error: setkeybinding: cannot find mod/key combination");
5660 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5664 key_insert(mod
, ks
, kfid
, spawn_name
);
5665 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5669 setconfbinding(char *selector
, char *value
, int flags
)
5671 enum keyfuncid kfid
;
5674 struct spawn_prog
*sp
;
5676 /* suppress unused warning since var is needed */
5679 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5680 if (selector
== NULL
) {
5681 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5682 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5684 setkeybinding(mod
, ks
, kfid
, NULL
);
5689 /* search by key function name */
5690 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5691 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5692 SWM_FUNCNAME_LEN
) == 0) {
5693 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5695 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5696 setkeybinding(mod
, ks
, kfid
, NULL
);
5702 /* search by custom spawn name */
5703 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5704 if (strcasecmp(selector
, sp
->name
) == 0) {
5705 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5707 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5708 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5715 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5722 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5723 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5724 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5725 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
5726 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5727 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5728 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5729 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5730 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
5731 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
5732 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5733 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5734 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5735 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
5736 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
5737 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5738 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5739 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
5740 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5741 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5742 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5743 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5744 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5745 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5746 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5747 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5748 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5749 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5750 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5751 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5752 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5753 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5754 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5755 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5756 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5757 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5758 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5759 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5760 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5761 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5762 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5763 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5764 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5765 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5766 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5767 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5768 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5769 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5770 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5771 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
5772 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
5773 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
5774 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
5775 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
5776 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
5777 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
5778 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
5779 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
5780 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
5781 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
5782 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
5783 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
5784 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
5785 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
5786 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
5787 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
5788 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
5789 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
5790 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
5791 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
5792 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
5793 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5794 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5795 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5796 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
5797 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5798 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5799 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5800 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5801 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
5802 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5803 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5804 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5805 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
5806 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5807 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5808 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5809 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5810 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5811 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5812 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5813 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5814 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5815 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5816 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5817 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5818 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5820 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
5830 while (RB_EMPTY(&keys
) == 0) {
5831 kp
= RB_ROOT(&keys
);
5837 setkeymapping(char *selector
, char *value
, int flags
)
5839 char keymapping_file
[PATH_MAX
];
5841 /* suppress unused warnings since vars are needed */
5845 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5846 if (value
[0] == '~')
5847 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5848 pwd
->pw_dir
, &value
[1]);
5850 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5852 /* load new key bindings; if it fails, revert to default bindings */
5853 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5857 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5862 updatenumlockmask(void)
5865 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5866 xcb_keycode_t
*modmap
, kc
, *keycode
;
5870 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5871 xcb_get_modifier_mapping(conn
),
5874 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5875 for (i
= 0; i
< 8; i
++) {
5876 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5877 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5879 keycode
= xcb_key_symbols_get_keycode(syms
,
5882 numlockmask
= (1 << i
);
5888 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
5895 int num_screens
, k
, j
;
5896 unsigned int modifiers
[3];
5897 xcb_keycode_t
*code
;
5899 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5900 updatenumlockmask();
5903 modifiers
[1] = numlockmask
;
5904 modifiers
[2] = numlockmask
| XCB_MOD_MASK_LOCK
;
5906 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5907 for (k
= 0; k
< num_screens
; k
++) {
5908 if (TAILQ_EMPTY(&screens
[k
].rl
))
5910 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5912 RB_FOREACH(kp
, key_tree
, &keys
) {
5913 if ((code
= xcb_key_symbols_get_keycode(syms
,
5915 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5916 xcb_grab_key(conn
, 1,
5918 kp
->mod
| modifiers
[j
],
5919 *code
, XCB_GRAB_MODE_SYNC
,
5920 XCB_GRAB_MODE_ASYNC
);
5928 grabbuttons(struct ws_win
*win
)
5932 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
5934 for (i
= 0; i
< LENGTH(buttons
); i
++)
5935 if (buttons
[i
].action
== client_click
)
5936 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
5937 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
5938 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
5939 buttons
[i
].button
, buttons
[i
].mask
);
5942 const char *quirkname
[] = {
5943 "NONE", /* config string for "no value" */
5952 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5953 #define SWM_Q_WS "\n|+ \t"
5955 parsequirks(char *qstr
, unsigned long *quirk
)
5965 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5967 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5968 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5969 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5970 DNPRINTF(SWM_D_QUIRK
,
5971 "parsequirks: %s\n", name
);
5976 *quirk
|= 1 << (i
-1);
5980 if (i
>= LENGTH(quirkname
)) {
5981 DNPRINTF(SWM_D_QUIRK
,
5982 "parsequirks: invalid quirk [%s]\n", name
);
5990 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5994 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5997 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5998 err(1, "quirk_insert: malloc");
5999 if ((qp
->class = strdup(class)) == NULL
)
6000 err(1, "quirk_insert: strdup");
6001 if ((qp
->name
= strdup(name
)) == NULL
)
6002 err(1, "quirk_insert: strdup");
6005 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6007 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6011 quirk_remove(struct quirk
*qp
)
6013 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6014 qp
->name
, qp
->quirk
);
6016 TAILQ_REMOVE(&quirks
, qp
, entry
);
6021 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6025 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6026 unsigned long quirk
)
6028 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6029 qp
->name
, qp
->quirk
);
6032 quirk_insert(class, name
, quirk
);
6034 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6038 setquirk(const char *class, const char *name
, unsigned long quirk
)
6042 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6045 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6046 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6050 quirk_replace(qp
, class, name
, quirk
);
6051 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6056 warnx("error: setquirk: cannot find class/name combination");
6060 quirk_insert(class, name
, quirk
);
6061 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6065 setconfquirk(char *selector
, char *value
, int flags
)
6067 char *cp
, *class, *name
;
6071 /* suppress unused warning since var is needed */
6074 if (selector
== NULL
)
6076 if ((cp
= strchr(selector
, ':')) == NULL
)
6081 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6082 setquirk(class, name
, qrks
);
6089 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6090 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6091 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6092 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6093 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6094 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6095 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6096 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6097 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6098 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6099 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6100 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6101 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6104 /* conf file stuff */
6105 #define SWM_CONF_FILE "spectrwm.conf"
6106 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6110 SWM_S_BAR_AT_BOTTOM
,
6111 SWM_S_BAR_BORDER_WIDTH
,
6118 SWM_S_CLOCK_ENABLED
,
6121 SWM_S_CYCLE_VISIBLE
,
6123 SWM_S_DISABLE_BORDER
,
6125 SWM_S_FOCUS_CLOSE_WRAP
,
6126 SWM_S_FOCUS_DEFAULT
,
6132 SWM_S_STACK_ENABLED
,
6134 SWM_S_TITLE_CLASS_ENABLED
,
6135 SWM_S_TITLE_NAME_ENABLED
,
6136 SWM_S_URGENT_ENABLED
,
6137 SWM_S_VERBOSE_LAYOUT
,
6138 SWM_S_WINDOW_NAME_ENABLED
,
6139 SWM_S_WORKSPACE_LIMIT
6143 setconfvalue(char *selector
, char *value
, int flags
)
6148 /* suppress unused warning since var is needed */
6152 case SWM_S_BAR_ACTION
:
6154 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6155 err(1, "setconfvalue: bar_action");
6157 case SWM_S_BAR_AT_BOTTOM
:
6158 bar_at_bottom
= atoi(value
);
6160 case SWM_S_BAR_BORDER_WIDTH
:
6161 bar_border_width
= atoi(value
);
6162 if (bar_border_width
< 0)
6163 bar_border_width
= 0;
6165 case SWM_S_BAR_DELAY
:
6166 bar_delay
= atoi(value
);
6168 case SWM_S_BAR_ENABLED
:
6169 bar_enabled
= atoi(value
);
6171 case SWM_S_BAR_FONT
:
6173 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6174 err(1, "setconfvalue: asprintf: failed to allocate "
6175 "memory for bar_fonts.");
6178 /* If already in xft mode, then we are done. */
6179 if (!bar_font_legacy
)
6182 /* If there are any non-XLFD entries, switch to Xft mode. */
6183 while ((b
= strsep(&value
, ",")) != NULL
) {
6187 bar_font_legacy
= 0;
6192 case SWM_S_BAR_FORMAT
:
6194 if ((bar_format
= strdup(value
)) == NULL
)
6195 err(1, "setconfvalue: bar_format");
6197 case SWM_S_BAR_JUSTIFY
:
6198 if (!strcmp(value
, "left"))
6199 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6200 else if (!strcmp(value
, "center"))
6201 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6202 else if (!strcmp(value
, "right"))
6203 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6205 errx(1, "invalid bar_justify");
6207 case SWM_S_BORDER_WIDTH
:
6208 border_width
= atoi(value
);
6209 if (border_width
< 0)
6212 case SWM_S_CLOCK_ENABLED
:
6213 clock_enabled
= atoi(value
);
6215 case SWM_S_CLOCK_FORMAT
:
6216 #ifndef SWM_DENY_CLOCK_FORMAT
6218 if ((clock_format
= strdup(value
)) == NULL
)
6219 err(1, "setconfvalue: clock_format");
6222 case SWM_S_CYCLE_EMPTY
:
6223 cycle_empty
= atoi(value
);
6225 case SWM_S_CYCLE_VISIBLE
:
6226 cycle_visible
= atoi(value
);
6228 case SWM_S_DIALOG_RATIO
:
6229 dialog_ratio
= atof(value
);
6230 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6233 case SWM_S_DISABLE_BORDER
:
6234 disable_border
= atoi(value
);
6236 case SWM_S_FOCUS_CLOSE
:
6237 if (!strcmp(value
, "first"))
6238 focus_close
= SWM_STACK_BOTTOM
;
6239 else if (!strcmp(value
, "last"))
6240 focus_close
= SWM_STACK_TOP
;
6241 else if (!strcmp(value
, "next"))
6242 focus_close
= SWM_STACK_ABOVE
;
6243 else if (!strcmp(value
, "previous"))
6244 focus_close
= SWM_STACK_BELOW
;
6246 errx(1, "focus_close");
6248 case SWM_S_FOCUS_CLOSE_WRAP
:
6249 focus_close_wrap
= atoi(value
);
6251 case SWM_S_FOCUS_DEFAULT
:
6252 if (!strcmp(value
, "last"))
6253 focus_default
= SWM_STACK_TOP
;
6254 else if (!strcmp(value
, "first"))
6255 focus_default
= SWM_STACK_BOTTOM
;
6257 errx(1, "focus_default");
6259 case SWM_S_FOCUS_MODE
:
6260 if (!strcmp(value
, "default"))
6261 focus_mode
= SWM_FOCUS_DEFAULT
;
6262 else if (!strcmp(value
, "follow") ||
6263 !strcmp(value
, "follow_cursor"))
6264 focus_mode
= SWM_FOCUS_FOLLOW
;
6265 else if (!strcmp(value
, "manual"))
6266 focus_mode
= SWM_FOCUS_MANUAL
;
6268 errx(1, "focus_mode");
6270 case SWM_S_SPAWN_ORDER
:
6271 if (!strcmp(value
, "first"))
6272 spawn_position
= SWM_STACK_BOTTOM
;
6273 else if (!strcmp(value
, "last"))
6274 spawn_position
= SWM_STACK_TOP
;
6275 else if (!strcmp(value
, "next"))
6276 spawn_position
= SWM_STACK_ABOVE
;
6277 else if (!strcmp(value
, "previous"))
6278 spawn_position
= SWM_STACK_BELOW
;
6280 errx(1, "spawn_position");
6282 case SWM_S_SPAWN_TERM
:
6283 setconfspawn("term", value
, 0);
6284 setconfspawn("spawn_term", value
, 0);
6288 case SWM_S_SS_ENABLED
:
6289 ss_enabled
= atoi(value
);
6291 case SWM_S_STACK_ENABLED
:
6292 stack_enabled
= atoi(value
);
6294 case SWM_S_TERM_WIDTH
:
6295 term_width
= atoi(value
);
6299 case SWM_S_TITLE_CLASS_ENABLED
:
6300 title_class_enabled
= atoi(value
);
6302 case SWM_S_TITLE_NAME_ENABLED
:
6303 title_name_enabled
= atoi(value
);
6305 case SWM_S_URGENT_ENABLED
:
6306 urgent_enabled
= atoi(value
);
6308 case SWM_S_VERBOSE_LAYOUT
:
6309 verbose_layout
= atoi(value
);
6310 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6312 layouts
[i
].l_string
= fancy_stacker
;
6314 layouts
[i
].l_string
= plain_stacker
;
6317 case SWM_S_WINDOW_NAME_ENABLED
:
6318 window_name_enabled
= atoi(value
);
6320 case SWM_S_WORKSPACE_LIMIT
:
6321 workspace_limit
= atoi(value
);
6322 if (workspace_limit
> SWM_WS_MAX
)
6323 workspace_limit
= SWM_WS_MAX
;
6324 else if (workspace_limit
< 1)
6325 workspace_limit
= 1;
6334 setconfmodkey(char *selector
, char *value
, int flags
)
6336 /* suppress unused warnings since vars are needed */
6340 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6341 update_modkey(XCB_MOD_MASK_1
);
6342 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6343 update_modkey(XCB_MOD_MASK_2
);
6344 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6345 update_modkey(XCB_MOD_MASK_3
);
6346 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6347 update_modkey(XCB_MOD_MASK_4
);
6354 setconfcolor(char *selector
, char *value
, int flags
)
6356 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6361 setconfregion(char *selector
, char *value
, int flags
)
6363 /* suppress unused warnings since vars are needed */
6367 custom_region(value
);
6372 setautorun(char *selector
, char *value
, int flags
)
6382 /* suppress unused warnings since vars are needed */
6386 if (getenv("SWM_STARTED"))
6390 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6391 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6393 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6394 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6397 * This is a little intricate
6399 * If the pid already exists we simply reuse it because it means it was
6400 * used before AND not claimed by manage_window. We get away with
6401 * altering it in the parent after INSERT because this can not be a race
6404 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6407 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6409 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6410 err(1, "setautorun: realloc");
6411 a
.argv
[argc
- 1] = ap
;
6414 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6415 err(1, "setautorun: realloc");
6416 a
.argv
[argc
] = NULL
;
6418 if ((pid
= fork()) == 0) {
6419 spawn(ws_id
, &a
, 1);
6428 p
= calloc(1, sizeof *p
);
6431 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6441 setlayout(char *selector
, char *value
, int flags
)
6443 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
6444 int st
= SWM_V_STACK
, num_screens
;
6446 struct workspace
*ws
;
6448 /* suppress unused warnings since vars are needed */
6452 if (getenv("SWM_STARTED"))
6456 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6457 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
6458 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6459 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6462 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6463 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6465 if (!strcasecmp(s
, "vertical"))
6467 else if (!strcasecmp(s
, "vertical_flip")) {
6470 } else if (!strcasecmp(s
, "horizontal"))
6472 else if (!strcasecmp(s
, "horizontal_flip")) {
6475 } else if (!strcasecmp(s
, "fullscreen"))
6478 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6479 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6482 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6483 for (i
= 0; i
< num_screens
; i
++) {
6484 ws
= (struct workspace
*)&screens
[i
].ws
;
6485 ws
[ws_id
].cur_layout
= &layouts
[st
];
6487 ws
[ws_id
].always_raise
= ar
;
6488 if (st
== SWM_MAX_STACK
)
6492 for (x
= 0; x
< abs(mg
); x
++) {
6493 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6494 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6495 SWM_ARG_ID_MASTERSHRINK
);
6499 for (x
= 0; x
< abs(ma
); x
++) {
6500 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6501 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6502 SWM_ARG_ID_MASTERDEL
);
6506 for (x
= 0; x
< abs(si
); x
++) {
6507 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6508 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6509 SWM_ARG_ID_STACKDEC
);
6514 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6515 SWM_ARG_ID_FLIPLAYOUT
);
6525 /* config options */
6526 struct config_option
{
6528 int (*func
)(char*, char*, int);
6531 struct config_option configopt
[] = {
6532 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6533 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6534 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6535 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6536 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6537 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6538 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6539 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6540 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6541 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6542 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6543 { "keyboard_mapping", setkeymapping
, 0 },
6544 { "bind", setconfbinding
, 0 },
6545 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6546 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6547 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6548 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6549 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6550 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6551 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6552 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6553 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6554 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6555 { "modkey", setconfmodkey
, 0 },
6556 { "program", setconfspawn
, 0 },
6557 { "quirk", setconfquirk
, 0 },
6558 { "region", setconfregion
, 0 },
6559 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6560 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6561 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6562 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6563 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6564 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6565 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6566 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6567 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6568 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6569 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6570 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6571 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6572 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6573 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6574 { "autorun", setautorun
, 0 },
6575 { "layout", setlayout
, 0 },
6579 conf_load(char *filename
, int keymapping
)
6582 char *line
, *cp
, *optsub
, *optval
;
6583 size_t linelen
, lineno
= 0;
6584 int wordlen
, i
, optidx
;
6585 struct config_option
*opt
= NULL
;
6587 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6589 if (filename
== NULL
) {
6590 warnx("conf_load: no filename");
6593 if ((config
= fopen(filename
, "r")) == NULL
) {
6594 warn("conf_load: fopen: %s", filename
);
6598 while (!feof(config
)) {
6599 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6602 err(1, "%s", filename
);
6607 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6608 if (cp
[0] == '\0') {
6613 /* get config option */
6614 wordlen
= strcspn(cp
, "=[ \t\n");
6616 warnx("%s: line %zd: no option found",
6621 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6622 opt
= &configopt
[i
];
6623 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6624 (int)strlen(opt
->optname
) == wordlen
) {
6630 warnx("%s: line %zd: unknown option %.*s",
6631 filename
, lineno
, wordlen
, cp
);
6634 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
6635 warnx("%s: line %zd: invalid option %.*s",
6636 filename
, lineno
, wordlen
, cp
);
6640 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6641 /* get [selector] if any */
6645 wordlen
= strcspn(cp
, "]");
6648 warnx("%s: line %zd: syntax error",
6653 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6655 warnx("%s: line %zd: unable to allocate"
6656 "memory for selector", filename
,
6662 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6664 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6666 optval
= strdup(cp
);
6667 /* call function to deal with it all */
6668 if (configopt
[optidx
].func(optsub
, optval
,
6669 configopt
[optidx
].funcflags
) != 0)
6670 errx(1, "%s: line %zd: invalid data for %s",
6671 filename
, lineno
, configopt
[optidx
].optname
);
6678 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6685 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6691 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6693 struct ws_win
*parent
, *w
;
6694 struct swm_region
*r
;
6695 struct workspace
*ws
;
6696 xcb_icccm_wm_hints_t wmh
;
6698 parent
= find_window(win
->transient
);
6700 parent
->focus_child
= win
;
6702 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6703 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6705 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6707 /* parent doen't exist in our window list */
6708 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6709 if (xcb_icccm_get_wm_hints_reply(conn
,
6710 xcb_icccm_get_wm_hints(conn
, w
->id
),
6712 warnx("can't get hints for 0x%x", w
->id
);
6716 if (win
->hints
.window_group
!= wmh
.window_group
)
6719 w
->focus_child
= win
;
6720 win
->transient
= w
->id
;
6722 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6723 "transient to 0x%x\n", win
->transient
);
6730 window_get_pid(xcb_window_t win
)
6735 xcb_get_property_cookie_t pc
;
6736 xcb_get_property_reply_t
*pr
;
6738 apid
= get_atom_from_string("_NET_WM_PID");
6739 if (apid
== XCB_ATOM_NONE
)
6742 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6743 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6746 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6751 if (pr
->type
== apid
&& pr
->format
== 32)
6752 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6758 apid
= get_atom_from_string("_SWM_PID");
6759 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6761 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6764 if (pr
->type
!= apid
) {
6769 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6776 get_ws_idx(xcb_window_t id
)
6782 xcb_get_property_reply_t
*gpr
;
6784 gpr
= xcb_get_property_reply(conn
,
6785 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6786 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6791 proplen
= xcb_get_property_value_length(gpr
);
6793 prop
= malloc(proplen
+ 1);
6796 xcb_get_property_value(gpr
),
6798 prop
[proplen
] = '\0';
6805 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6806 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6808 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
6818 manage_window(xcb_window_t id
, uint16_t mapped
)
6820 xcb_window_t trans
= XCB_WINDOW_NONE
;
6821 struct ws_win
*win
, *ww
;
6823 char ws_idx_str
[SWM_PROPLEN
];
6824 struct swm_region
*r
;
6828 xcb_icccm_get_wm_protocols_reply_t wpr
;
6830 if ((win
= find_window(id
)) != NULL
) {
6831 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
6832 "managed; skipping.)\n", id
);
6833 return (win
); /* Already managed. */
6836 /* See if window is on the unmanaged list. */
6837 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6838 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
6839 "unmanaged list.\n", id
);
6840 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6843 set_child_transient(win
, &trans
);
6847 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
6850 /* Create and initialize ws_win object. */
6851 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6852 err(1, "manage_window: calloc: failed to allocate memory for "
6857 /* Get window geometry. */
6858 win
->wa
= xcb_get_geometry_reply(conn
,
6859 xcb_get_geometry(conn
, win
->id
),
6862 /* Figure out which region the window belongs to. */
6863 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6865 /* Ignore window border if there is one. */
6866 WIDTH(win
) = win
->wa
->width
;
6867 HEIGHT(win
) = win
->wa
->height
;
6868 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
6869 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
6871 win
->mapped
= mapped
;
6872 win
->floatmaxed
= 0;
6873 win
->ewmh_flags
= 0;
6874 win
->s
= r
->s
; /* this never changes */
6876 store_float_geom(win
, r
);
6878 /* Get WM_SIZE_HINTS. */
6879 xcb_icccm_get_wm_normal_hints_reply(conn
,
6880 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6884 xcb_icccm_get_wm_hints_reply(conn
,
6885 xcb_icccm_get_wm_hints(conn
, win
->id
),
6888 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
6889 xcb_icccm_get_wm_transient_for_reply(conn
,
6890 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
6893 win
->transient
= trans
;
6894 set_child_transient(win
, &win
->transient
);
6897 /* Get supported protocols. */
6898 if (xcb_icccm_get_wm_protocols_reply(conn
,
6899 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
6901 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6902 if (wpr
.atoms
[i
] == a_takefocus
)
6903 win
->take_focus
= 1;
6904 if (wpr
.atoms
[i
] == a_delete
)
6905 win
->can_delete
= 1;
6907 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6910 win
->iconic
= get_swm_iconic(win
);
6912 /* Figure out which workspace the window belongs to. */
6913 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
6914 win
->ws
= &r
->s
->ws
[p
->ws
];
6915 TAILQ_REMOVE(&pidlist
, p
, entry
);
6918 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
6920 /* _SWM_WS is set; use that. */
6921 win
->ws
= &r
->s
->ws
[ws_idx
];
6922 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
6923 /* Launch transients in the same ws as parent. */
6929 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
6930 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
6932 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6934 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6935 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6940 ewmh_autoquirk(win
);
6942 /* Determine initial quirks. */
6943 if (xcb_icccm_get_wm_class_reply(conn
,
6944 xcb_icccm_get_wm_class(conn
, win
->id
),
6946 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6947 win
->ch
.class_name
, win
->ch
.instance_name
);
6949 /* java is retarded so treat it special */
6950 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6951 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
6956 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6957 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6958 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6959 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
6960 "list; mask: 0x%lx\n", qp
->quirk
);
6961 if (qp
->quirk
& SWM_Q_FLOAT
)
6963 win
->quirks
= qp
->quirk
;
6968 /* Alter window position if quirky */
6969 if (win
->quirks
& SWM_Q_ANYWHERE
)
6972 /* Reset font sizes (the bruteforce way; no default keybinding). */
6973 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6974 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6975 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
6976 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6977 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
6980 /* Make sure window is positioned inside its region, if its active. */
6982 constrain_window(win
, win
->ws
->r
, 0);
6986 /* Select which X events to monitor and set border pixel color. */
6987 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6988 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
6989 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6991 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
6994 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
6995 XCB_CW_EVENT_MASK
, wa
);
6998 /* Figure out where to stack the window in the workspace. */
6999 if (trans
&& (ww
= find_window(trans
)))
7000 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7001 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7002 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7004 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7005 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7006 else switch (spawn_position
) {
7009 case SWM_STACK_ABOVE
:
7010 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7012 case SWM_STACK_BOTTOM
:
7013 case SWM_STACK_BELOW
:
7014 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7017 /* Get initial _NET_WM_STATE */
7018 ewmh_get_win_state(win
);
7019 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7020 ewmh_update_actions(win
);
7024 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7025 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7026 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7027 YESNO(win
->iconic
), win
->transient
);
7033 free_window(struct ws_win
*win
)
7035 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7040 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7045 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7050 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7053 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7057 unmanage_window(struct ws_win
*win
)
7059 struct ws_win
*parent
;
7064 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7066 if (win
->transient
) {
7067 parent
= find_window(win
->transient
);
7069 parent
->focus_child
= NULL
;
7072 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7073 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7077 expose(xcb_expose_event_t
*e
)
7080 struct swm_region
*r
;
7082 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7084 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7085 for (i
= 0; i
< num_screens
; i
++)
7086 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7087 if (e
->window
== WINID(r
->bar
))
7095 focusin(xcb_focus_in_event_t
*e
)
7097 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7098 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7099 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7103 focusout(xcb_focus_out_event_t
*e
)
7105 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7106 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7107 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7112 keypress(xcb_key_press_event_t
*e
)
7114 xcb_keysym_t keysym
;
7117 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7119 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7121 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7124 last_event_time
= e
->time
;
7126 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7127 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7128 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7129 else if (keyfuncs
[kp
->funcid
].func
)
7130 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7131 &(keyfuncs
[kp
->funcid
].args
));
7135 buttonpress(xcb_button_press_event_t
*e
)
7138 struct swm_region
*r
, *old_r
;
7142 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7143 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7144 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7145 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7146 e
->state
, YESNO(e
->same_screen
));
7148 if (e
->event
== e
->root
) {
7149 if (e
->child
!= 0) {
7150 win
= find_window(e
->child
);
7151 /* Pass ButtonPress to window if it isn't managed. */
7155 /* Focus on empty region */
7156 /* If no windows on region if its empty. */
7157 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7158 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7159 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7160 if (old_r
&& old_r
!= r
)
7161 unfocus_win(old_r
->ws
->focus
);
7163 xcb_set_input_focus(conn
,
7164 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7166 /* Clear bar since empty. */
7174 win
= find_window(e
->event
);
7180 last_event_time
= e
->time
;
7182 focus_win(get_focus_magic(win
));
7184 for (i
= 0; i
< LENGTH(buttons
); i
++)
7185 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7186 buttons
[i
].button
== e
->detail
&&
7187 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7188 buttons
[i
].func(win
, &buttons
[i
].args
);
7194 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7195 /* Replay event to event window */
7196 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7198 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7199 /* Unfreeze grab events. */
7200 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7208 print_win_geom(xcb_window_t w
)
7210 xcb_get_geometry_reply_t
*wa
;
7212 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7215 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7220 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7221 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7222 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7231 get_stack_mode_name(uint8_t mode
)
7236 case XCB_STACK_MODE_ABOVE
:
7239 case XCB_STACK_MODE_BELOW
:
7242 case XCB_STACK_MODE_TOP_IF
:
7245 case XCB_STACK_MODE_BOTTOM_IF
:
7248 case XCB_STACK_MODE_OPPOSITE
:
7260 configurerequest(xcb_configure_request_event_t
*e
)
7263 struct swm_region
*r
= NULL
;
7266 uint32_t wc
[7] = {0};
7268 if ((win
= find_window(e
->window
)) == NULL
)
7269 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7273 if (swm_debug
& SWM_D_EVENT
) {
7274 print_win_geom(e
->window
);
7276 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7277 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7278 e
->parent
, YESNO(new), e
->value_mask
);
7279 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7280 DPRINTF("X: %d ", e
->x
);
7281 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7282 DPRINTF("Y: %d ", e
->y
);
7283 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7284 DPRINTF("W: %u ", e
->width
);
7285 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7286 DPRINTF("H: %u ", e
->height
);
7287 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7288 DPRINTF("Border: %u ", e
->border_width
);
7289 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7290 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7291 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7292 DPRINTF("StackMode: %s(%u) ",
7293 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7299 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7300 mask
|= XCB_CONFIG_WINDOW_X
;
7303 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7304 mask
|= XCB_CONFIG_WINDOW_Y
;
7307 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7308 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7311 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7312 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7313 wc
[i
++] = e
->height
;
7315 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7316 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7317 wc
[i
++] = e
->border_width
;
7319 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7320 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7321 wc
[i
++] = e
->sibling
;
7323 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7324 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7325 wc
[i
++] = e
->stack_mode
;
7329 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7332 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7333 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7336 else if (win
->ws
->old_r
)
7339 /* windows are centered unless ANYWHERE quirk is set. */
7340 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7341 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7342 win
->g_float
.x
= e
->x
;
7344 win
->g_float
.x
-= X(r
);
7347 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7348 win
->g_float
.y
= e
->y
;
7350 win
->g_float
.y
-= Y(r
);
7354 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7355 win
->g_float
.w
= e
->width
;
7357 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7358 win
->g_float
.h
= e
->height
;
7360 win
->g_floatvalid
= 1;
7362 if (win
->floating
&& r
) {
7363 WIDTH(win
) = win
->g_float
.w
;
7364 HEIGHT(win
) = win
->g_float
.h
;
7366 stack_floater(win
, win
->ws
->r
);
7380 configurenotify(xcb_configure_notify_event_t
*e
)
7384 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
7385 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7386 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
7387 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
7388 YESNO(e
->override_redirect
));
7390 win
= find_window(e
->window
);
7392 xcb_icccm_get_wm_normal_hints_reply(conn
,
7393 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7396 if (font_adjusted
) {
7404 destroynotify(xcb_destroy_notify_event_t
*e
)
7408 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7410 if ((win
= find_window(e
->window
)) == NULL
) {
7411 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7417 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7418 /* If we were focused, make sure we focus on something else. */
7419 if (win
== win
->ws
->focus
)
7420 win
->ws
->focus_pending
= get_focus_prev(win
);
7423 unmanage_window(win
);
7426 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7427 if (win
->ws
->focus_pending
) {
7428 focus_win(win
->ws
->focus_pending
);
7429 win
->ws
->focus_pending
= NULL
;
7440 get_notify_detail_label(uint8_t detail
)
7445 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7448 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7451 case XCB_NOTIFY_DETAIL_INFERIOR
:
7454 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7455 label
= "Nonlinear";
7457 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7458 label
= "NonlinearVirtual";
7460 case XCB_NOTIFY_DETAIL_POINTER
:
7463 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7464 label
= "PointerRoot";
7466 case XCB_NOTIFY_DETAIL_NONE
:
7477 get_notify_mode_label(uint8_t mode
)
7482 case XCB_NOTIFY_MODE_NORMAL
:
7485 case XCB_NOTIFY_MODE_GRAB
:
7488 case XCB_NOTIFY_MODE_UNGRAB
:
7491 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7492 label
= "WhileGrabbed";
7503 enternotify(xcb_enter_notify_event_t
*e
)
7506 struct swm_region
*old_r
, *r
;
7508 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
7509 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7510 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7511 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7512 get_notify_mode_label(e
->mode
), e
->mode
,
7513 get_notify_detail_label(e
->detail
), e
->detail
,
7514 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7515 YESNO(e
->same_screen_focus
), e
->state
);
7517 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7518 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7519 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7523 last_event_time
= e
->time
;
7525 if ((win
= find_window(e
->event
)) == NULL
) {
7526 if (e
->event
== e
->root
) {
7527 /* If no windows on pointer region, then focus root. */
7528 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7529 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7530 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7531 if (old_r
&& old_r
!= r
)
7532 unfocus_win(old_r
->ws
->focus
);
7534 xcb_set_input_focus(conn
,
7535 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7537 /* Clear bar since empty. */
7543 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
7549 focus_win(get_focus_magic(win
));
7556 leavenotify(xcb_leave_notify_event_t
*e
)
7558 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
7559 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7560 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7561 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7562 get_notify_mode_label(e
->mode
), e
->mode
,
7563 get_notify_detail_label(e
->detail
), e
->detail
,
7564 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7565 YESNO(e
->same_screen_focus
), e
->state
);
7569 /* lets us use one switch statement for arbitrary mode/detail combinations */
7570 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7573 mapnotify(xcb_map_notify_event_t
*e
)
7577 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7579 if ((win
= find_window(e
->window
)) == NULL
)
7580 win
= manage_window(e
->window
, 1);
7583 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7585 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7586 if (win
->ws
->focus_pending
== win
) {
7588 win
->ws
->focus_pending
= NULL
;
7596 mappingnotify(xcb_mapping_notify_event_t
*e
)
7598 xcb_refresh_keyboard_mapping(syms
, e
);
7600 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7605 maprequest(xcb_map_request_event_t
*e
)
7608 xcb_get_window_attributes_reply_t
*war
;
7610 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7613 war
= xcb_get_window_attributes_reply(conn
,
7614 xcb_get_window_attributes(conn
, e
->window
),
7617 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7621 if (war
->override_redirect
) {
7622 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7627 win
= manage_window(e
->window
,
7628 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
7630 /* All windows need to be mapped if they are in the current workspace.*/
7634 /* The new window should get focus. */
7635 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7636 win
->ws
->focus_pending
= get_focus_magic(win
);
7638 /* Ignore EnterNotify to handle the mapnotify without interference. */
7639 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7640 event_drain(XCB_ENTER_NOTIFY
);
7643 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7648 get_atom_name(xcb_atom_t atom
)
7653 * This should be disabled during most debugging since
7654 * xcb_get_* causes an xcb_flush.
7657 xcb_get_atom_name_reply_t
*r
;
7659 r
= xcb_get_atom_name_reply(conn
,
7660 xcb_get_atom_name(conn
, atom
),
7663 len
= xcb_get_atom_name_name_length(r
);
7665 name
= malloc(len
+ 1);
7667 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7681 propertynotify(xcb_property_notify_event_t
*e
)
7687 name
= get_atom_name(e
->atom
);
7688 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
7689 "time: %#x\n", e
->window
, name
, e
->atom
, e
->time
);
7692 win
= find_window(e
->window
);
7696 last_event_time
= e
->time
;
7698 if (e
->atom
== a_swm_iconic
) {
7699 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7700 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7701 win
->ws
->focus_pending
= get_focus_prev(win
);
7708 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7709 focus_win(win
->ws
->focus_pending
);
7710 win
->ws
->focus_pending
= NULL
;
7714 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
7715 /* The window is no longer iconic, restack ws. */
7716 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7717 win
->ws
->focus_pending
= get_focus_magic(win
);
7721 /* Flush EnterNotify for mapnotify, if needed. */
7724 } else if (e
->atom
== a_state
) {
7725 /* State just changed, make sure it gets focused if mapped. */
7726 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7727 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7729 win
->ws
->focus_pending
== win
) {
7730 focus_win(win
->ws
->focus_pending
);
7731 win
->ws
->focus_pending
= NULL
;
7735 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
7736 e
->atom
== XCB_ATOM_WM_NAME
) {
7744 unmapnotify(xcb_unmap_notify_event_t
*e
)
7747 struct workspace
*ws
;
7749 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7751 /* determine if we need to help unmanage this window */
7752 win
= find_window(e
->window
);
7758 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7760 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
7762 /* If we were focused, make sure we focus on something else. */
7763 if (win
== ws
->focus
)
7764 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7765 ws
->focus_pending
= get_focus_prev(win
);
7768 unmanage_window(win
);
7771 DNPRINTF(SWM_D_EVENT
, "unmapnotify: focus_pending: 0x%x\n",
7772 ws
->focus_pending
->id
);
7774 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7775 if (ws
->focus_pending
) {
7776 focus_win(ws
->focus_pending
);
7777 ws
->focus_pending
= NULL
;
7787 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7789 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7795 clientmessage(xcb_client_message_event_t
*e
)
7798 xcb_map_request_event_t mre
;
7802 name
= get_atom_name(e
->type
);
7803 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7804 e
->window
, name
, e
->type
);
7807 win
= find_window(e
->window
);
7810 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7811 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7812 "unmanaged window.\n");
7813 mre
.window
= e
->window
;
7819 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7820 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7823 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7824 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7825 if (win
->can_delete
)
7826 client_msg(win
, a_delete
, 0);
7828 xcb_kill_client(conn
, win
->id
);
7830 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7831 DNPRINTF(SWM_D_EVENT
,
7832 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7833 if (win
->floating
) {
7834 if (e
->data
.data32
[0] & (1<<8)) /* x */
7835 X(win
) = e
->data
.data32
[1];
7836 if (e
->data
.data32
[0] & (1<<9)) /* y */
7837 Y(win
) = e
->data
.data32
[2];
7838 if (e
->data
.data32
[0] & (1<<10)) /* width */
7839 WIDTH(win
) = e
->data
.data32
[3];
7840 if (e
->data
.data32
[0] & (1<<11)) /* height */
7841 HEIGHT(win
) = e
->data
.data32
[4];
7846 /* TODO: Change stack sizes */
7847 /* notify no change was made. */
7848 config_win(win
, NULL
);
7851 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7852 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7853 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7854 if (e
->data
.data32
[2])
7855 ewmh_update_win_state(win
, e
->data
.data32
[2],
7867 int errcode
= xcb_connection_has_error(conn
);
7868 #ifdef XCB_CONN_ERROR
7871 case XCB_CONN_ERROR
:
7872 s
= "Socket error, pipe error or other stream error.";
7874 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
7875 s
= "Extension not supported.";
7877 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
7878 s
= "Insufficient memory.";
7880 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
7881 s
= "Request length was exceeded.";
7883 case XCB_CONN_CLOSED_PARSE_ERR
:
7884 s
= "Error parsing display string.";
7887 s
= "Unknown error.";
7890 errx(errcode
, "X CONNECTION ERROR: %s", s
);
7893 errx(errcode
, "X CONNECTION ERROR");
7901 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
7902 XCB_EVENT_MASK_ENTER_WINDOW
;
7904 xcb_void_cookie_t wac
;
7905 xcb_generic_error_t
*error
;
7907 /* this causes an error if some other window manager is running */
7908 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7909 for (i
= 0; i
< num_screens
; i
++) {
7910 if ((sc
= get_screen(i
)) == NULL
)
7911 errx(1, "ERROR: can't get screen %d.", i
);
7912 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7914 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7915 XCB_CW_EVENT_MASK
, &val
);
7916 if ((error
= xcb_request_check(conn
, wac
))) {
7917 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7923 /* click to focus on empty region */
7924 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
7925 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
7926 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
7933 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7935 struct swm_region
*r
, *n
;
7936 struct workspace
*ws
= NULL
;
7939 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7940 s
->idx
, w
, h
, x
, y
);
7942 /* remove any conflicting regions */
7943 n
= TAILQ_FIRST(&s
->rl
);
7946 n
= TAILQ_NEXT(r
, entry
);
7947 if (X(r
) < (x
+ w
) &&
7948 (X(r
) + WIDTH(r
)) > x
&&
7950 (Y(r
) + HEIGHT(r
)) > y
) {
7951 if (r
->ws
->r
!= NULL
)
7952 r
->ws
->old_r
= r
->ws
->r
;
7955 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7956 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7960 /* search old regions for one to reuse */
7962 /* size + location match */
7963 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7964 if (X(r
) == x
&& Y(r
) == y
&&
7965 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7969 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7970 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7974 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7975 /* try to use old region's workspace */
7976 if (r
->ws
->r
== NULL
)
7979 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7980 err(1, "new_region: calloc: failed to allocate memory "
7983 /* if we don't have a workspace already, find one */
7985 for (i
= 0; i
< workspace_limit
; i
++)
7986 if (s
->ws
[i
].r
== NULL
) {
7993 errx(1, "new_region: no free workspaces");
8004 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8010 #ifdef SWM_XRR_HAS_CRTC
8013 #endif /* SWM_XRR_HAS_CRTC */
8014 struct swm_region
*r
;
8016 xcb_randr_get_screen_resources_current_cookie_t src
;
8017 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8018 xcb_randr_get_crtc_info_cookie_t cic
;
8019 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8020 xcb_randr_crtc_t
*crtc
;
8021 xcb_screen_t
*screen
;
8023 if ((screen
= get_screen(i
)) == NULL
)
8024 errx(1, "ERROR: can't get screen %d.", i
);
8026 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8027 if (i
>= num_screens
)
8028 errx(1, "scan_xrandr: invalid screen");
8030 /* remove any old regions */
8031 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8032 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8034 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8035 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8039 /* map virtual screens onto physical screens */
8040 #ifdef SWM_XRR_HAS_CRTC
8041 if (xrandr_support
) {
8042 src
= xcb_randr_get_screen_resources_current(conn
,
8044 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8047 new_region(&screens
[i
], 0, 0,
8048 screen
->width_in_pixels
,
8049 screen
->height_in_pixels
);
8052 ncrtc
= srr
->num_crtcs
;
8054 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8055 for (c
= 0; c
< ncrtc
; c
++) {
8056 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8058 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8061 if (cir
->num_outputs
== 0) {
8067 new_region(&screens
[i
], 0, 0,
8068 screen
->width_in_pixels
,
8069 screen
->height_in_pixels
);
8071 new_region(&screens
[i
],
8072 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8077 #endif /* SWM_XRR_HAS_CRTC */
8079 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8080 screen
->height_in_pixels
);
8085 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8087 struct swm_region
*r
;
8090 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8092 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8093 /* silly event doesn't include the screen index */
8094 for (i
= 0; i
< num_screens
; i
++)
8095 if (screens
[i
].root
== e
->root
)
8097 if (i
>= num_screens
)
8098 errx(1, "screenchange: screen not found");
8100 /* brute force for now, just re-enumerate the regions */
8104 print_win_geom(e
->root
);
8106 /* add bars to all regions */
8107 for (i
= 0; i
< num_screens
; i
++)
8108 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8116 xcb_window_t
*wins
= NULL
;
8118 int i
, j
, num_screens
;
8119 uint16_t state
, manage
, mapped
;
8121 xcb_query_tree_cookie_t qtc
;
8122 xcb_query_tree_reply_t
*qtr
;
8123 xcb_get_window_attributes_cookie_t c
;
8124 xcb_get_window_attributes_reply_t
*r
;
8125 xcb_get_property_cookie_t pc
;
8127 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8128 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8129 for (i
= 0; i
< num_screens
; i
++) {
8130 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8131 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8134 wins
= xcb_query_tree_children(qtr
);
8135 no
= xcb_query_tree_children_length(qtr
);
8136 /* attach windows to a region */
8137 /* normal windows */
8138 for (j
= 0; j
< no
; j
++) {
8139 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8140 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8143 if (r
->override_redirect
) {
8148 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8149 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8155 state
= getstate(wins
[j
]);
8156 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
8157 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8158 if (mapped
|| manage
)
8159 manage_window(wins
[j
], mapped
);
8162 /* transient windows */
8163 for (j
= 0; j
< no
; j
++) {
8164 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8165 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8168 if (r
->override_redirect
) {
8174 state
= getstate(wins
[j
]);
8175 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
8176 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8177 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8178 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8179 &wins
[j
], NULL
) && manage
)
8180 manage_window(wins
[j
], mapped
);
8184 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8190 int i
, j
, k
, num_screens
;
8191 struct workspace
*ws
;
8192 uint32_t gcv
[1], wa
[1];
8193 const xcb_query_extension_reply_t
*qep
;
8194 xcb_screen_t
*screen
;
8195 xcb_cursor_t cursor
;
8196 xcb_font_t cursor_font
;
8197 xcb_randr_query_version_cookie_t c
;
8198 xcb_randr_query_version_reply_t
*r
;
8200 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8201 if ((screens
= calloc(num_screens
,
8202 sizeof(struct swm_screen
))) == NULL
)
8203 err(1, "setup_screens: calloc: failed to allocate memory for "
8206 /* initial Xrandr setup */
8208 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8210 c
= xcb_randr_query_version(conn
, 1, 1);
8211 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8213 if (r
->major_version
>= 1) {
8215 xrandr_eventbase
= qep
->first_event
;
8221 cursor_font
= xcb_generate_id(conn
);
8222 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
8224 cursor
= xcb_generate_id(conn
);
8225 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
8226 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
8229 /* map physical screens */
8230 for (i
= 0; i
< num_screens
; i
++) {
8231 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8233 TAILQ_INIT(&screens
[i
].rl
);
8234 TAILQ_INIT(&screens
[i
].orl
);
8235 if ((screen
= get_screen(i
)) == NULL
)
8236 errx(1, "ERROR: can't get screen %d.", i
);
8237 screens
[i
].root
= screen
->root
;
8239 /* set default colors */
8240 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8241 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8242 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8243 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8244 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8246 /* create graphics context on screen */
8247 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8249 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8250 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8252 /* set default cursor */
8253 xcb_change_window_attributes(conn
, screens
[i
].root
,
8256 /* init all workspaces */
8257 /* XXX these should be dynamically allocated too */
8258 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8259 ws
= &screens
[i
].ws
[j
];
8263 ws
->focus_prev
= NULL
;
8264 ws
->focus_pending
= NULL
;
8267 TAILQ_INIT(&ws
->winlist
);
8268 TAILQ_INIT(&ws
->unmanagedlist
);
8270 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8271 if (layouts
[k
].l_config
!= NULL
)
8272 layouts
[k
].l_config(ws
,
8273 SWM_ARG_ID_STACKINIT
);
8274 ws
->cur_layout
= &layouts
[0];
8275 ws
->cur_layout
->l_string(ws
);
8281 xcb_randr_select_input(conn
, screens
[i
].root
,
8282 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8284 xcb_free_cursor(conn
, cursor
);
8285 xcb_close_font(conn
, cursor_font
);
8291 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8292 err(1, "setup_globals: strdup: failed to allocate memory.");
8294 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8295 err(1, "setup_globals: strdup: failed to allocate memory.");
8297 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8298 errx(1, "unable to allocate key symbols");
8300 a_state
= get_atom_from_string("WM_STATE");
8301 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8302 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8303 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8304 a_wmname
= get_atom_from_string("WM_NAME");
8305 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8306 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8307 a_string
= get_atom_from_string("STRING");
8308 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8309 a_swm_ws
= get_atom_from_string("_SWM_WS");
8316 xcb_atom_t netwmcheck
;
8317 xcb_window_t root
, win
;
8319 /* work around sun jdk bugs, code from wmname */
8320 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8322 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8323 for (i
= 0; i
< num_screens
; i
++) {
8324 root
= screens
[i
].root
;
8326 win
= xcb_generate_id(conn
);
8327 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8328 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8329 XCB_COPY_FROM_PARENT
, 0, NULL
);
8331 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8332 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8333 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8334 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8335 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8336 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8341 shutdown_cleanup(void)
8345 /* disable alarm because the following code may not be interrupted */
8347 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8348 err(1, "can't disable alarm");
8356 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8357 for (i
= 0; i
< num_screens
; ++i
) {
8358 if (screens
[i
].bar_gc
!= 0)
8359 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8360 if (!bar_font_legacy
)
8361 XftColorFree(display
, DefaultVisual(display
, i
),
8362 DefaultColormap(display
, i
), &bar_font_color
);
8365 if (bar_font_legacy
)
8366 XFreeFontSet(display
, bar_fs
);
8368 XftFontClose(display
, bar_font
);
8371 xcb_key_symbols_free(syms
);
8373 xcb_disconnect(conn
);
8377 event_error(xcb_generic_error_t
*e
)
8381 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8382 "resource_id: %u, minor_code: %u\n",
8383 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8384 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8385 e
->sequence
, e
->resource_id
, e
->minor_code
);
8389 event_handle(xcb_generic_event_t
*evt
)
8391 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8393 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
8394 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8395 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
8398 #define EVENT(type, callback) case type: callback((void *)evt); return
8399 EVENT(0, event_error
);
8400 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8401 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8402 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8403 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8404 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8405 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8406 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8407 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8408 /*EVENT(XCB_CREATE_NOTIFY, );*/
8409 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8410 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8411 EVENT(XCB_EXPOSE
, expose
);
8413 EVENT(XCB_FOCUS_IN
, focusin
);
8414 EVENT(XCB_FOCUS_OUT
, focusout
);
8416 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8417 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8418 EVENT(XCB_KEY_PRESS
, keypress
);
8419 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8420 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8422 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8424 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8425 EVENT(XCB_MAP_REQUEST
, maprequest
);
8426 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8427 /*EVENT(XCB_MOTION_NOTIFY, );*/
8428 /*EVENT(XCB_NO_EXPOSURE, );*/
8429 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8430 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8431 /*EVENT(XCB_RESIZE_REQUEST, );*/
8432 /*EVENT(XCB_SELECTION_CLEAR, );*/
8433 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8434 /*EVENT(XCB_SELECTION_REQUEST, );*/
8435 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8436 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8439 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8440 screenchange((void *)evt
);
8444 main(int argc
, char *argv
[])
8446 struct swm_region
*r
, *rr
;
8447 struct ws_win
*winfocus
= NULL
;
8448 char conf
[PATH_MAX
], *cfile
= NULL
;
8450 int xfd
, i
, num_screens
;
8451 struct sigaction sact
;
8452 xcb_generic_event_t
*evt
;
8456 /* suppress unused warning since var is needed */
8459 time_started
= time(NULL
);
8462 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8463 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8464 warnx("no locale support");
8466 /* handle some signals */
8467 bzero(&sact
, sizeof(sact
));
8468 sigemptyset(&sact
.sa_mask
);
8470 sact
.sa_handler
= sighdlr
;
8471 sigaction(SIGINT
, &sact
, NULL
);
8472 sigaction(SIGQUIT
, &sact
, NULL
);
8473 sigaction(SIGTERM
, &sact
, NULL
);
8474 sigaction(SIGHUP
, &sact
, NULL
);
8476 sact
.sa_handler
= sighdlr
;
8477 sact
.sa_flags
= SA_NOCLDSTOP
;
8478 sigaction(SIGCHLD
, &sact
, NULL
);
8480 if (!(display
= XOpenDisplay(0)))
8481 errx(1, "can not open display");
8483 conn
= XGetXCBConnection(display
);
8484 if (xcb_connection_has_error(conn
))
8485 errx(1, "can not get XCB connection");
8487 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8489 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8490 xfd
= xcb_get_file_descriptor(conn
);
8492 /* look for local and global conf file */
8493 pwd
= getpwuid(getuid());
8495 errx(1, "invalid user: %d", getuid());
8497 xcb_grab_server(conn
);
8500 /* flush all events */
8501 while ((evt
= xcb_poll_for_event(conn
))) {
8502 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8507 if (enable_wm() != 0)
8508 errx(1, "another window manager is currently running");
8519 for (i
= 0; ; i
++) {
8524 snprintf(conf
, sizeof conf
, "%s/.%s",
8525 pwd
->pw_dir
, SWM_CONF_FILE
);
8529 snprintf(conf
, sizeof conf
, "/etc/%s",
8534 snprintf(conf
, sizeof conf
, "%s/.%s",
8535 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8539 snprintf(conf
, sizeof conf
, "/etc/%s",
8546 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8547 if (S_ISREG(sb
.st_mode
)) {
8554 /* load conf (if any) */
8556 conf_load(cfile
, SWM_CONF_DEFAULT
);
8559 /* set some values to work around bad programs */
8561 /* grab existing windows (before we build the bars) */
8564 if (getenv("SWM_STARTED") == NULL
)
8565 setenv("SWM_STARTED", "YES", 1);
8567 /* setup all bars */
8568 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8569 for (i
= 0; i
< num_screens
; i
++)
8570 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8571 if (winfocus
== NULL
)
8572 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8579 xcb_ungrab_server(conn
);
8583 while ((evt
= xcb_poll_for_event(conn
))) {
8590 /* if we are being restarted go focus on first window */
8592 rr
= winfocus
->ws
->r
;
8594 /* not a visible window */
8599 focus_win(get_region_focus(rr
));
8609 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8610 if (errno
!= EINTR
) {
8611 DNPRINTF(SWM_D_MISC
, "select failed");
8614 restart(NULL
, NULL
);