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>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.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 <X11/Xproto.h>
102 #include <X11/Xutil.h>
103 #include <X11/extensions/Xrandr.h>
104 #include <X11/extensions/XTest.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr
= SPECTRWM_BUILDSTR
;
115 static const char *buildstr
= SPECTRWM_VERSION
;
119 # error XRandR versions less than 1.0 are not supported
124 #define SWM_XRR_HAS_CRTC
128 #if defined(__OpenBSD__)
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_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
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
185 u_int32_t swm_debug
= 0
203 #define DPRINTF(x...)
204 #define DNPRINTF(n,x...)
207 #define LENGTH(x) (sizeof x / sizeof x[0])
208 #define MODKEY Mod1Mask
209 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
210 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
211 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
212 #define SWM_PROPLEN (16)
213 #define SWM_FUNCNAME_LEN (32)
214 #define SWM_KEYS_LEN (255)
215 #define SWM_QUIRK_LEN (64)
216 #define X(r) (r)->g.x
217 #define Y(r) (r)->g.y
218 #define WIDTH(r) (r)->g.w
219 #define HEIGHT(r) (r)->g.h
220 #define BORDER(w) (w->bordered ? border_width : 0)
221 #define MAX_X(r) ((r)->g.x + (r)->g.w)
222 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
223 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
224 #define SH_MIN_W(w) (w)->sh.min_width
225 #define SH_MIN_H(w) (w)->sh.min_height
226 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
227 #define SH_MAX_W(w) (w)->sh.max_width
228 #define SH_MAX_H(w) (w)->sh.max_height
229 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
230 #define SH_INC_W(w) (w)->sh.width_inc
231 #define SH_INC_H(w) (w)->sh.height_inc
232 #define SWM_MAX_FONT_STEPS (3)
233 #define WINID(w) ((w) ? (w)->id : 0)
234 #define YESNO(x) ((x) ? "yes" : "no")
236 #define SWM_FOCUS_DEFAULT (0)
237 #define SWM_FOCUS_SYNERGY (1)
238 #define SWM_FOCUS_FOLLOW (2)
240 #define SWM_CONF_DEFAULT (0)
241 #define SWM_CONF_KEYMAPPING (1)
244 #define SWM_LIB "/usr/local/lib/libswmhack.so"
251 xcb_atom_t takefocus
;
253 xcb_atom_t a_netwmname
;
254 xcb_atom_t a_utf8_string
;
256 xcb_atom_t a_swm_iconic
;
257 volatile sig_atomic_t running
= 1;
258 volatile sig_atomic_t restart_wm
= 0;
260 /*int last_focus_event = FocusOut;*/
261 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
265 int xrandr_eventbase
;
266 unsigned int numlockmask
= 0;
268 xcb_connection_t
*conn
;
269 xcb_key_symbols_t
*syms
;
272 int cycle_visible
= 0;
274 int font_adjusted
= 0;
275 unsigned int mod_key
= MODKEY
;
278 struct swm_region
*search_r
;
279 int select_list_pipe
[2];
280 int select_resp_pipe
[2];
282 volatile sig_atomic_t search_resp
;
283 int search_resp_action
;
285 struct search_window
{
286 TAILQ_ENTRY(search_window
) entry
;
290 xcb_window_t indicator
;
292 TAILQ_HEAD(search_winlist
, search_window
);
293 struct search_winlist search_wl
;
298 SWM_SEARCH_UNICONIFY
,
299 SWM_SEARCH_NAME_WORKSPACE
,
300 SWM_SEARCH_SEARCH_WORKSPACE
,
301 SWM_SEARCH_SEARCH_WINDOW
304 #define SWM_STACK_TOP (0)
305 #define SWM_STACK_BOTTOM (1)
306 #define SWM_STACK_ABOVE (2)
307 #define SWM_STACK_BELOW (3)
310 double dialog_ratio
= 0.6;
312 #define SWM_BAR_MAX (256)
313 #define SWM_BAR_JUSTIFY_LEFT (0)
314 #define SWM_BAR_JUSTIFY_CENTER (1)
315 #define SWM_BAR_JUSTIFY_RIGHT (2)
316 #define SWM_BAR_OFFSET (4)
317 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
318 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
319 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
320 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
321 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
323 char *bar_argv
[] = { NULL
, NULL
};
325 unsigned char bar_ext
[SWM_BAR_MAX
];
326 char bar_vertext
[SWM_BAR_MAX
];
328 sig_atomic_t bar_alarm
= 0;
331 int bar_border_width
= 1;
332 int bar_at_bottom
= 0;
334 int bar_extra_running
= 0;
337 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
338 char *bar_format
= NULL
;
339 int stack_enabled
= 1;
340 int clock_enabled
= 1;
341 int urgent_enabled
= 0;
342 char *clock_format
= NULL
;
343 int title_name_enabled
= 0;
344 int title_class_enabled
= 0;
345 int window_name_enabled
= 0;
346 int focus_mode
= SWM_FOCUS_DEFAULT
;
347 int focus_close
= SWM_STACK_BELOW
;
348 int focus_close_wrap
= 1;
349 int focus_default
= SWM_STACK_TOP
;
350 int spawn_position
= SWM_STACK_TOP
;
351 int disable_border
= 0;
352 int border_width
= 1;
353 int verbose_layout
= 0;
357 XFontSetExtents
*bar_fs_extents
;
361 #define SWM_MENU_FN (2)
362 #define SWM_MENU_NB (4)
363 #define SWM_MENU_NF (6)
364 #define SWM_MENU_SB (8)
365 #define SWM_MENU_SF (10)
367 /* layout manager data */
368 struct swm_geometry
{
381 struct swm_geometry g
;
384 /* virtual "screens" */
386 TAILQ_ENTRY(swm_region
) entry
;
387 struct swm_geometry g
;
388 struct workspace
*ws
; /* current workspace on this region */
389 struct workspace
*ws_prior
; /* prior workspace on this region */
390 struct swm_screen
*s
; /* screen idx */
393 TAILQ_HEAD(swm_region_list
, swm_region
);
396 TAILQ_ENTRY(ws_win
) entry
;
398 xcb_window_t transient
;
399 struct ws_win
*child_trans
; /* transient child window */
400 struct swm_geometry g
; /* current geometry */
401 struct swm_geometry g_float
; /* region coordinates */
402 int g_floatvalid
; /* g_float geometry validity */
403 int floatmaxed
; /* whether maxed by max_stack */
408 unsigned int ewmh_flags
;
409 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
415 unsigned long quirks
;
416 struct workspace
*ws
; /* always valid */
417 struct swm_screen
*s
; /* always valid, never changes */
418 xcb_get_geometry_reply_t
*wa
;
420 xcb_icccm_get_wm_class_reply_t ch
;
421 xcb_icccm_wm_hints_t hints
;
423 TAILQ_HEAD(ws_win_list
, ws_win
);
427 TAILQ_ENTRY(pid_e
) entry
;
431 TAILQ_HEAD(pid_list
, pid_e
);
432 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
434 /* layout handlers */
436 void vertical_config(struct workspace
*, int);
437 void vertical_stack(struct workspace
*, struct swm_geometry
*);
438 void horizontal_config(struct workspace
*, int);
439 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
440 void max_stack(struct workspace
*, struct swm_geometry
*);
441 void plain_stacker(struct workspace
*);
442 void fancy_stacker(struct workspace
*);
444 struct ws_win
*find_window(xcb_window_t
);
446 void grabbuttons(struct ws_win
*, int);
447 void new_region(struct swm_screen
*, int, int, int, int);
448 void unmanage_window(struct ws_win
*);
449 uint16_t getstate(xcb_window_t
);
451 int conf_load(char *, int);
454 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
455 void (*l_config
)(struct workspace
*, int);
457 #define SWM_L_FOCUSPREV (1<<0)
458 #define SWM_L_MAPONFOCUS (1<<1)
459 void (*l_string
)(struct workspace
*);
461 /* stack, configure */
462 { vertical_stack
, vertical_config
, 0, plain_stacker
},
463 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
465 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
466 { NULL
, NULL
, 0, NULL
},
469 /* position of max_stack mode in the layouts array, index into layouts! */
470 #define SWM_V_STACK (0)
471 #define SWM_H_STACK (1)
472 #define SWM_MAX_STACK (2)
474 #define SWM_H_SLICE (32)
475 #define SWM_V_SLICE (32)
477 /* define work spaces */
479 int idx
; /* workspace index */
480 char *name
; /* workspace name */
481 int always_raise
; /* raise windows on focus */
482 struct layout
*cur_layout
; /* current layout handlers */
483 struct ws_win
*focus
; /* may be NULL */
484 struct ws_win
*focus_prev
; /* may be NULL */
485 struct swm_region
*r
; /* may be NULL */
486 struct swm_region
*old_r
; /* may be NULL */
487 struct ws_win_list winlist
; /* list of windows in ws */
488 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
489 char stacker
[10]; /* display stacker and layout */
493 int horizontal_msize
;
495 int horizontal_stacks
;
506 SWM_S_COLOR_BAR_BORDER
,
507 SWM_S_COLOR_BAR_FONT
,
513 /* physical screen mapping */
514 #define SWM_WS_MAX (22) /* hard limit */
515 int workspace_limit
= 10; /* soft limit */
518 int idx
; /* screen index */
519 struct swm_region_list rl
; /* list of regions on this screen */
520 struct swm_region_list orl
; /* list of old regions */
522 struct workspace ws
[SWM_WS_MAX
];
528 } c
[SWM_S_COLOR_MAX
];
530 xcb_gcontext_t bar_gc
;
532 struct swm_screen
*screens
;
534 /* args to functions */
537 #define SWM_ARG_ID_FOCUSNEXT (0)
538 #define SWM_ARG_ID_FOCUSPREV (1)
539 #define SWM_ARG_ID_FOCUSMAIN (2)
540 #define SWM_ARG_ID_FOCUSCUR (4)
541 #define SWM_ARG_ID_SWAPNEXT (10)
542 #define SWM_ARG_ID_SWAPPREV (11)
543 #define SWM_ARG_ID_SWAPMAIN (12)
544 #define SWM_ARG_ID_MOVELAST (13)
545 #define SWM_ARG_ID_MASTERSHRINK (20)
546 #define SWM_ARG_ID_MASTERGROW (21)
547 #define SWM_ARG_ID_MASTERADD (22)
548 #define SWM_ARG_ID_MASTERDEL (23)
549 #define SWM_ARG_ID_FLIPLAYOUT (24)
550 #define SWM_ARG_ID_STACKRESET (30)
551 #define SWM_ARG_ID_STACKINIT (31)
552 #define SWM_ARG_ID_CYCLEWS_UP (40)
553 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
554 #define SWM_ARG_ID_CYCLESC_UP (42)
555 #define SWM_ARG_ID_CYCLESC_DOWN (43)
556 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
557 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
558 #define SWM_ARG_ID_STACKINC (50)
559 #define SWM_ARG_ID_STACKDEC (51)
560 #define SWM_ARG_ID_SS_ALL (60)
561 #define SWM_ARG_ID_SS_WINDOW (61)
562 #define SWM_ARG_ID_DONTCENTER (70)
563 #define SWM_ARG_ID_CENTER (71)
564 #define SWM_ARG_ID_KILLWINDOW (80)
565 #define SWM_ARG_ID_DELETEWINDOW (81)
566 #define SWM_ARG_ID_WIDTHGROW (90)
567 #define SWM_ARG_ID_WIDTHSHRINK (91)
568 #define SWM_ARG_ID_HEIGHTGROW (92)
569 #define SWM_ARG_ID_HEIGHTSHRINK (93)
570 #define SWM_ARG_ID_MOVEUP (100)
571 #define SWM_ARG_ID_MOVEDOWN (101)
572 #define SWM_ARG_ID_MOVELEFT (102)
573 #define SWM_ARG_ID_MOVERIGHT (103)
577 void focus(struct swm_region
*, union arg
*);
578 void focus_magic(struct ws_win
*);
582 TAILQ_ENTRY(quirk
) entry
;
586 #define SWM_Q_FLOAT (1<<0) /* float this window */
587 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
588 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
589 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
590 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
591 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
593 TAILQ_HEAD(quirk_list
, quirk
);
594 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
597 * Supported EWMH hints should be added to
598 * both the enum and the ewmh array
603 _NET_MOVERESIZE_WINDOW
,
604 _NET_WM_ACTION_CLOSE
,
605 _NET_WM_ACTION_FULLSCREEN
,
607 _NET_WM_ACTION_RESIZE
,
608 _NET_WM_ALLOWED_ACTIONS
,
611 _NET_WM_STATE_FULLSCREEN
,
612 _NET_WM_STATE_HIDDEN
,
613 _NET_WM_STATE_MAXIMIZED_HORZ
,
614 _NET_WM_STATE_MAXIMIZED_VERT
,
615 _NET_WM_STATE_SKIP_PAGER
,
616 _NET_WM_STATE_SKIP_TASKBAR
,
618 _NET_WM_WINDOW_TYPE_DIALOG
,
619 _NET_WM_WINDOW_TYPE_DOCK
,
620 _NET_WM_WINDOW_TYPE_NORMAL
,
621 _NET_WM_WINDOW_TYPE_SPLASH
,
622 _NET_WM_WINDOW_TYPE_TOOLBAR
,
623 _NET_WM_WINDOW_TYPE_UTILITY
,
624 _SWM_WM_STATE_MANUAL
,
631 } ewmh
[SWM_EWMH_HINT_MAX
] = {
632 /* must be in same order as in the enum */
633 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
634 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
635 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
636 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
637 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
638 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
639 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
640 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
641 {"_NET_WM_STATE", XCB_ATOM_NONE
},
642 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
643 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
644 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
645 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
646 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
647 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
648 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
649 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
650 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
651 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
652 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
653 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
654 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
655 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
656 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
659 void store_float_geom(struct ws_win
*, struct swm_region
*);
660 int floating_toggle_win(struct ws_win
*);
661 void constrain_window(struct ws_win
*, struct swm_region
*, int);
662 void update_window(struct ws_win
*);
663 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
664 char *get_win_name(xcb_window_t
);
665 xcb_atom_t
get_atom_from_string(const char *);
666 void map_window_raised(xcb_window_t
);
668 xcb_screen_t
*get_screen(int);
669 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
670 void event_handle(xcb_generic_event_t
*);
673 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
675 unsigned int tmpr
, tmpg
, tmpb
;
677 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
688 get_screen(int screen
)
690 xcb_screen_iterator_t i
;
692 i
= xcb_setup_roots_iterator(xcb_get_setup(conn
));
693 for (; i
.rem
; --screen
, xcb_screen_next(&i
))
703 xcb_get_input_focus_cookie_t c
;
704 xcb_get_input_focus_reply_t
*r
;
707 c
= xcb_get_input_focus(conn
);
709 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
715 map_window_raised(xcb_window_t win
)
717 uint32_t val
= XCB_STACK_MODE_ABOVE
;
719 xcb_configure_window(conn
, win
,
720 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
722 xcb_map_window(conn
, win
);
726 get_atom_from_string(const char *str
)
728 xcb_intern_atom_cookie_t c
;
729 xcb_intern_atom_reply_t
*r
;
732 c
= xcb_intern_atom(conn
, False
, strlen(str
), str
);
733 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
741 return (XCB_ATOM_NONE
);
745 update_iconic(struct ws_win
*win
, int newv
)
752 iprop
= get_atom_from_string("_SWM_ICONIC");
753 if (iprop
== XCB_ATOM_NONE
)
757 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
758 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
760 xcb_delete_property(conn
, win
->id
, iprop
);
764 get_iconic(struct ws_win
*win
)
768 xcb_get_property_reply_t
*pr
= NULL
;
770 iprop
= get_atom_from_string("_SWM_ICONIC");
771 if (iprop
== XCB_ATOM_NONE
)
774 pr
= xcb_get_property_reply(conn
,
775 xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
779 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
781 v
= *((int32_t *)xcb_get_property_value(pr
));
792 int i
, j
, num_screens
;
794 sup_list
= get_atom_from_string("_NET_SUPPORTED");
796 for (i
= 0; i
< LENGTH(ewmh
); i
++)
797 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
799 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
800 for (i
= 0; i
< num_screens
; i
++) {
801 /* Support check window will be created by workaround(). */
803 /* Report supported atoms */
804 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
805 for (j
= 0; j
< LENGTH(ewmh
); j
++)
806 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
807 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
816 xcb_atom_t sup_check
, sup_list
;
818 xcb_get_property_cookie_t pc
;
819 xcb_get_property_reply_t
*pr
;
821 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
822 sup_list
= get_atom_from_string("_NET_SUPPORTED");
823 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
825 for (i
= 0; i
< num_screens
; i
++) {
826 /* Get the support check window and destroy it */
827 pc
= xcb_get_property(conn
, False
, screens
[i
].root
, sup_check
,
828 XCB_ATOM_WINDOW
, 0, 1);
829 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
831 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
833 xcb_destroy_window(conn
, id
);
834 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
835 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
843 ewmh_autoquirk(struct ws_win
*win
)
849 xcb_get_property_cookie_t c
;
850 xcb_get_property_reply_t
*r
;
852 c
= xcb_get_property(conn
, False
, win
->id
,
853 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, (~0L));
854 r
= xcb_get_property_reply(conn
, c
, NULL
);
857 n
= xcb_get_property_value_length(r
);
859 for (i
= 0; i
< n
; i
++) {
860 type
= *((xcb_atom_t
*)xcb_get_property_value(r
));
861 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
863 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
864 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
865 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
867 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
870 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
871 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
873 win
->quirks
= SWM_Q_FLOAT
;
880 #define SWM_EWMH_ACTION_COUNT_MAX (6)
881 #define EWMH_F_FULLSCREEN (1<<0)
882 #define EWMH_F_ABOVE (1<<1)
883 #define EWMH_F_HIDDEN (1<<2)
884 #define EWMH_F_SKIP_PAGER (1<<3)
885 #define EWMH_F_SKIP_TASKBAR (1<<4)
886 #define SWM_F_MANUAL (1<<5)
889 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
897 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
898 "fullscreen %s\n", win
->id
, YESNO(fs
));
901 if (!win
->g_floatvalid
)
902 store_float_geom(win
, win
->ws
->r
);
904 win
->g
= win
->ws
->r
->g
;
907 if (win
->g_floatvalid
) {
908 /* refloat at last floating relative position */
909 win
->g
= win
->g_float
;
910 X(win
) += X(win
->ws
->r
);
911 Y(win
) += Y(win
->ws
->r
);
919 ewmh_update_actions(struct ws_win
*win
)
921 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
927 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
930 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
931 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
934 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
935 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
938 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
939 #define _NET_WM_STATE_ADD 1 /* add/set property */
940 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
943 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
945 unsigned int mask
= 0;
946 unsigned int changed
= 0;
947 unsigned int orig_flags
;
952 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
953 mask
= EWMH_F_FULLSCREEN
;
954 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
956 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
958 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
959 mask
= EWMH_F_SKIP_PAGER
;
960 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
961 mask
= EWMH_F_SKIP_TASKBAR
;
964 orig_flags
= win
->ewmh_flags
;
967 case _NET_WM_STATE_REMOVE
:
968 win
->ewmh_flags
&= ~mask
;
970 case _NET_WM_STATE_ADD
:
971 win
->ewmh_flags
|= mask
;
973 case _NET_WM_STATE_TOGGLE
:
974 win
->ewmh_flags
^= mask
;
978 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
980 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
982 if (!floating_toggle_win(win
))
983 win
->ewmh_flags
= orig_flags
; /* revert */
984 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
986 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
987 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
989 if (!ewmh_set_win_fullscreen(win
,
990 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
991 win
->ewmh_flags
= orig_flags
; /* revert */
993 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
995 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
996 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
997 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
998 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
999 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1000 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1001 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1002 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1003 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1004 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1005 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1006 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1007 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1008 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1009 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1010 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1011 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1012 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1013 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1014 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1018 ewmh_get_win_state(struct ws_win
*win
)
1021 xcb_get_property_cookie_t c
;
1022 xcb_get_property_reply_t
*r
;
1028 win
->ewmh_flags
= 0;
1030 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1032 win
->ewmh_flags
|= SWM_F_MANUAL
;
1034 c
= xcb_get_property(conn
, False
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1035 XCB_ATOM_ATOM
, 0, (~0L));
1036 r
= xcb_get_property_reply(conn
, c
, NULL
);
1040 states
= xcb_get_property_value(r
);
1041 n
= xcb_get_property_value_length(r
);
1043 for (i
= 0; i
< n
; i
++)
1044 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1052 dumpwins(struct swm_region
*r
, union arg
*args
)
1056 xcb_get_window_attributes_cookie_t c
;
1057 xcb_get_window_attributes_reply_t
*wa
;
1059 if (r
->ws
== NULL
) {
1060 warnx("dumpwins: invalid workspace");
1064 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1065 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1066 state
= getstate(win
->id
);
1067 c
= xcb_get_window_attributes(conn
, win
->id
);
1068 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1070 warnx("window: 0x%x, map_state: %d, state: %u, "
1071 "transient: 0x%x", win
->id
, wa
->map_state
,
1072 state
, win
->transient
);
1075 warnx("window: 0x%x, failed xcb_get_window_attributes",
1079 warnx("===== unmanaged window list =====");
1080 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1081 state
= getstate(win
->id
);
1082 c
= xcb_get_window_attributes(conn
, win
->id
);
1083 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1085 warnx("window: 0x%x, map_state: %d, state: %u, "
1086 "transient: 0x%x", win
->id
, wa
->map_state
,
1087 state
, win
->transient
);
1090 warnx("window: 0x%x, failed xcb_get_window_attributes",
1094 warnx("=================================");
1098 dumpwins(struct swm_region
*r
, union arg
*args
)
1101 #endif /* SWM_DEBUG */
1103 void event_error(xcb_generic_error_t
*);
1104 void expose(xcb_expose_event_t
*);
1105 void keypress(xcb_key_press_event_t
*);
1106 void buttonpress(xcb_button_press_event_t
*);
1107 void configurerequest(xcb_configure_request_event_t
*);
1108 void configurenotify(xcb_configure_notify_event_t
*);
1109 void destroynotify(xcb_destroy_notify_event_t
*);
1110 void enternotify(xcb_enter_notify_event_t
*);
1111 void mapnotify(xcb_map_notify_event_t
*);
1112 void mappingnotify(xcb_mapping_notify_event_t
*);
1113 void maprequest(xcb_map_request_event_t
*);
1114 void propertynotify(xcb_property_notify_event_t
*);
1115 void unmapnotify(xcb_unmap_notify_event_t
*);
1116 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1117 void clientmessage(xcb_client_message_event_t
*);
1118 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1123 int saved_errno
, status
;
1126 saved_errno
= errno
;
1130 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1135 if (errno
!= ECHILD
)
1136 warn("sighdlr: waitpid");
1137 #endif /* SWM_DEBUG */
1140 if (pid
== searchpid
)
1144 if (WIFEXITED(status
)) {
1145 if (WEXITSTATUS(status
) != 0)
1146 warnx("sighdlr: child exit status: %d",
1147 WEXITSTATUS(status
));
1149 warnx("sighdlr: child is terminated "
1151 #endif /* SWM_DEBUG */
1165 errno
= saved_errno
;
1171 struct pid_e
*p
= NULL
;
1173 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1178 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1187 name_to_color(const char *colorname
)
1189 uint32_t result
= 0;
1190 char cname
[32] = "#";
1191 xcb_screen_t
*screen
;
1192 xcb_colormap_t cmap
;
1193 xcb_alloc_color_reply_t
*cr
;
1194 xcb_alloc_named_color_reply_t
*nr
;
1195 uint16_t rr
, gg
, bb
;
1197 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1198 cmap
= screen
->default_colormap
;
1200 /* color is in format rgb://rr/gg/bb */
1201 if (strncmp(colorname
, "rgb:", 4) == 0) {
1202 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1203 warnx("could not parse rgb %s", colorname
);
1205 cr
= xcb_alloc_color_reply(conn
,
1206 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1212 warnx("color '%s' not found", colorname
);
1215 nr
= xcb_alloc_named_color_reply(conn
,
1216 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1219 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1220 nr
= xcb_alloc_named_color_reply(conn
,
1221 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1228 warnx("color '%s' not found", colorname
);
1235 setscreencolor(char *val
, int i
, int c
)
1239 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1240 if (i
> 0 && i
<= num_screens
) {
1241 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1242 free(screens
[i
- 1].c
[c
].name
);
1243 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1245 } else if (i
== -1) {
1246 for (i
= 0; i
< num_screens
; i
++) {
1247 screens
[i
].c
[c
].color
= name_to_color(val
);
1248 free(screens
[i
].c
[c
].name
);
1249 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1253 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1258 fancy_stacker(struct workspace
*ws
)
1260 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1261 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1262 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1263 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1264 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1265 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1266 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1267 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1268 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1272 plain_stacker(struct workspace
*ws
)
1274 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1275 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1276 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1277 sizeof ws
->stacker
);
1278 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1279 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1280 sizeof ws
->stacker
);
1284 custom_region(char *val
)
1286 unsigned int sidx
, x
, y
, w
, h
;
1288 xcb_screen_t
*screen
;
1290 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1291 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1292 errx(1, "invalid custom region, "
1293 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1294 if (sidx
< 1 || sidx
> num_screens
)
1295 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1299 screen
= get_screen(sidx
);
1301 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1303 if (x
> screen
->width_in_pixels
||
1304 y
> screen
->height_in_pixels
||
1305 w
+ x
> screen
->width_in_pixels
||
1306 h
+ y
> screen
->height_in_pixels
) {
1307 warnx("ignoring region %ux%u+%u+%u - not within screen "
1308 "boundaries (%ux%u)", w
, h
, x
, y
,
1309 screen
->width_in_pixels
, screen
->height_in_pixels
);
1313 new_region(&screens
[sidx
], x
, y
, w
, h
);
1317 socket_setnonblock(int fd
)
1321 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1322 err(1, "fcntl F_GETFL");
1323 flags
|= O_NONBLOCK
;
1324 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1325 err(1, "fcntl F_SETFL");
1329 bar_print(struct swm_region
*r
, const char *s
)
1333 xcb_rectangle_t rect
;
1335 XRectangle ibox
, lbox
;
1338 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1340 switch (bar_justify
) {
1341 case SWM_BAR_JUSTIFY_LEFT
:
1344 case SWM_BAR_JUSTIFY_CENTER
:
1345 x
= (WIDTH(r
) - lbox
.width
) / 2;
1347 case SWM_BAR_JUSTIFY_RIGHT
:
1348 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1352 if (x
< SWM_BAR_OFFSET
)
1357 rect
.width
= WIDTH(r
->bar
);
1358 rect
.height
= HEIGHT(r
->bar
);
1360 /* clear back buffer */
1361 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1362 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1363 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1364 sizeof(rect
), &rect
);
1366 /* draw back buffer */
1367 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1368 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1369 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
;
1370 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1371 xcb_image_text_8(conn
, len
, r
->bar
->buffer
, r
->s
->bar_gc
, x
,
1372 (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1376 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1377 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1381 bar_extra_stop(void)
1385 bzero(bar_pipe
, sizeof bar_pipe
);
1388 kill(bar_pid
, SIGTERM
);
1391 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1396 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1398 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1400 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1401 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1405 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1407 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1409 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1410 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1414 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1416 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1419 bar_class_name(s
, sz
, r
);
1420 strlcat(s
, ":", sz
);
1421 bar_title_name(s
, sz
, r
);
1425 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1427 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1429 if (r
->ws
->focus
->floating
)
1430 strlcat(s
, "(f)", sz
);
1434 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1438 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1440 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1443 strlcat(s
, title
, sz
);
1447 int urgent
[SWM_WS_MAX
];
1449 bar_urgent(char *s
, size_t sz
)
1452 int i
, j
, num_screens
;
1454 xcb_get_property_cookie_t c
;
1455 xcb_icccm_wm_hints_t hints
;
1457 for (i
= 0; i
< workspace_limit
; i
++)
1460 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1461 for (i
= 0; i
< num_screens
; i
++)
1462 for (j
= 0; j
< workspace_limit
; j
++)
1463 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1464 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1465 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1468 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1472 for (i
= 0; i
< workspace_limit
; i
++) {
1474 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1476 snprintf(b
, sizeof b
, "- ");
1482 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1484 if (r
== NULL
|| r
->ws
== NULL
)
1486 if (r
->ws
->name
!= NULL
)
1487 strlcat(s
, r
->ws
->name
, sz
);
1490 /* build the default bar format according to the defined enabled options */
1492 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1494 /* if format provided, just copy the buffers */
1495 if (bar_format
!= NULL
) {
1496 strlcpy(fmtnew
, fmtexp
, sz
);
1500 /* reset the output buffer */
1503 strlcat(fmtnew
, "+N:+I ", sz
);
1505 strlcat(fmtnew
, "+S", sz
);
1506 strlcat(fmtnew
, " ", sz
);
1508 /* only show the workspace name if there's actually one */
1509 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1510 strlcat(fmtnew
, "<+D>", sz
);
1511 strlcat(fmtnew
, "+3<", sz
);
1513 if (clock_enabled
) {
1514 strlcat(fmtnew
, fmtexp
, sz
);
1515 strlcat(fmtnew
, "+4<", sz
);
1518 /* bar_urgent already adds the space before the last asterisk */
1520 strlcat(fmtnew
, "* +U*+4<", sz
);
1522 if (title_class_enabled
) {
1523 strlcat(fmtnew
, "+C", sz
);
1524 if (title_name_enabled
== 0)
1525 strlcat(fmtnew
, "+4<", sz
);
1528 /* checks needed by the colon and floating strlcat(3) calls below */
1529 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1530 if (title_name_enabled
) {
1531 if (title_class_enabled
)
1532 strlcat(fmtnew
, ":", sz
);
1533 strlcat(fmtnew
, "+T+4<", sz
);
1535 if (window_name_enabled
) {
1536 if (r
->ws
->focus
->floating
)
1537 strlcat(fmtnew
, "+F ", sz
);
1538 strlcat(fmtnew
, "+64W ", sz
);
1542 /* finally add the action script output and the version */
1543 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1547 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1549 /* special case; no limit given, pad one space, instead */
1550 if (*limit
== sz
- 1)
1552 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1555 /* replaces the bar format character sequences (like in tmux(1)) */
1557 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1561 char tmp
[SWM_BAR_MAX
];
1565 /* reset strlcat(3) buffer */
1568 /* get number, if any */
1571 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1572 limit
= sizeof tmp
- 1;
1573 if (limit
<= 0 || limit
>= sizeof tmp
)
1574 limit
= sizeof tmp
- 1;
1576 /* there is nothing to replace (ie EOL) */
1583 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1586 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1589 bar_class_name(tmp
, sizeof tmp
, r
);
1592 bar_workspace_name(tmp
, sizeof tmp
, r
);
1595 bar_window_float(tmp
, sizeof tmp
, r
);
1598 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1601 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1604 bar_class_title_name(tmp
, sizeof tmp
, r
);
1607 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1610 bar_title_name(tmp
, sizeof tmp
, r
);
1613 bar_urgent(tmp
, sizeof tmp
);
1616 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1619 bar_window_name(tmp
, sizeof tmp
, r
);
1622 /* unknown character sequence; copy as-is */
1623 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1631 while (limit
-- > 0) {
1632 if (*offrep
>= sz
- 1)
1634 fmtrep
[(*offrep
)++] = *ptr
++;
1642 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1647 while (*fmt
!= '\0') {
1649 /* skip ordinary characters */
1652 fmtrep
[off
++] = *fmt
++;
1656 /* character sequence found; replace it */
1657 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1666 bar_fmt_expand(char *fmtexp
, size_t sz
)
1673 /* start by grabbing the current time and date */
1675 localtime_r(&tmt
, &tm
);
1677 /* figure out what to expand */
1678 if (bar_format
!= NULL
)
1680 else if (bar_format
== NULL
&& clock_enabled
)
1682 /* if nothing to expand bail out */
1688 /* copy as-is, just in case the format shouldn't be expanded below */
1689 strlcpy(fmtexp
, fmt
, sz
);
1690 /* finally pass the string through strftime(3) */
1691 #ifndef SWM_DENY_CLOCK_FORMAT
1692 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1693 warnx("format too long");
1701 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1702 char fmtrep
[SWM_BAR_MAX
];
1704 struct swm_region
*r
;
1706 /* expand the format by first passing it through strftime(3) */
1707 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1709 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1710 for (i
= 0; i
< num_screens
; i
++) {
1711 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1714 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1715 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1716 bar_print(r
, fmtrep
);
1727 if (bar_enabled
== 0)
1729 if (bar_extra
&& bar_extra_running
) {
1730 /* ignore short reads; it'll correct itself */
1731 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1732 if (b
&& b
[len
- 1] == '\n') {
1734 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1736 if (b
== NULL
&& errno
!= EAGAIN
) {
1737 warn("bar_update: bar_extra failed");
1741 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1754 bar_toggle(struct swm_region
*r
, union arg
*args
)
1756 struct swm_region
*tmpr
;
1759 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1761 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1763 for (i
= 0; i
< num_screens
; i
++)
1764 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1766 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1768 for (i
= 0; i
< num_screens
; i
++)
1769 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1771 map_window_raised(tmpr
->bar
->id
);
1774 bar_enabled
= !bar_enabled
;
1777 /* must be after stack */
1786 struct swm_region
*r
;
1790 /* do this here because the conf file is in memory */
1791 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1792 /* launch external status app */
1793 bar_extra_running
= 1;
1794 if (pipe(bar_pipe
) == -1)
1795 err(1, "pipe error");
1796 socket_setnonblock(bar_pipe
[0]);
1797 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1798 if (dup2(bar_pipe
[0], 0) == -1)
1800 if (dup2(bar_pipe
[1], 1) == -1)
1802 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1803 err(1, "could not disable SIGPIPE");
1804 switch (bar_pid
= fork()) {
1806 err(1, "cannot fork");
1810 execvp(bar_argv
[0], bar_argv
);
1811 err(1, "%s external app failed", bar_argv
[0]);
1813 default: /* parent */
1819 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1820 for (i
= 0; i
< num_screens
; i
++)
1821 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1824 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1825 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1826 xcb_change_window_attributes(conn
, r
->bar
->id
,
1827 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1833 bar_setup(struct swm_region
*r
)
1835 char *default_string
;
1836 char **missing_charsets
;
1837 int num_missing_charsets
= 0;
1839 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1843 XFreeFontSet(display
, bar_fs
);
1847 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1848 err(1, "bar_setup: calloc: failed to allocate memory.");
1850 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1852 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1853 &num_missing_charsets
, &default_string
);
1855 if (num_missing_charsets
> 0) {
1856 warnx("Unable to load charset(s):");
1858 for (i
= 0; i
< num_missing_charsets
; ++i
)
1859 warnx("%s", missing_charsets
[i
]);
1861 XFreeStringList(missing_charsets
);
1863 if (strcmp(default_string
, ""))
1864 warnx("Glyphs from those sets will be replaced "
1865 "by '%s'.", default_string
);
1867 warnx("Glyphs from those sets won't be drawn.");
1871 errx(1, "Error creating font set structure.");
1873 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1875 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1876 2 * bar_border_width
;
1882 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1883 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1884 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1886 r
->bar
->id
= xcb_generate_id(conn
);
1887 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1888 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
;
1889 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
1890 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1891 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1892 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1893 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
1894 | XCB_CW_EVENT_MASK
, wa
);
1896 r
->bar
->buffer
= xcb_generate_id(conn
);
1897 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1898 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1900 xcb_randr_select_input(conn
, r
->bar
->id
,
1901 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1904 map_window_raised(r
->bar
->id
);
1906 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1907 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1910 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1911 err(1, "could not install bar_signal");
1916 bar_cleanup(struct swm_region
*r
)
1920 xcb_destroy_window(conn
, r
->bar
->id
);
1921 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1927 set_win_state(struct ws_win
*win
, uint16_t state
)
1929 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1931 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1936 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, astate
,
1937 astate
, 32, 2, data
);
1941 getstate(xcb_window_t w
)
1943 uint16_t result
= 0;
1944 xcb_get_property_cookie_t c
;
1945 xcb_get_property_reply_t
*r
;
1947 c
= xcb_get_property(conn
, False
, w
, astate
, astate
, 0L, 2L);
1948 r
= xcb_get_property_reply(conn
, c
, NULL
);
1951 result
= *((uint16_t *)xcb_get_property_value(r
));
1955 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
1961 version(struct swm_region
*r
, union arg
*args
)
1963 bar_version
= !bar_version
;
1965 snprintf(bar_vertext
, sizeof bar_vertext
,
1966 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1968 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1975 client_msg(struct ws_win
*win
, xcb_atom_t a
)
1977 xcb_client_message_event_t ev
;
1982 bzero(&ev
, sizeof ev
);
1983 ev
.response_type
= XCB_CLIENT_MESSAGE
;
1984 ev
.window
= win
->id
;
1987 ev
.data
.data32
[0] = a
;
1988 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
1990 xcb_send_event(conn
, False
, win
->id
,
1991 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
1994 /* synthetic response to a ConfigureRequest when not making a change */
1996 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
1998 xcb_configure_notify_event_t ce
;
2003 /* send notification of unchanged state. */
2004 bzero(&ce
, sizeof(ce
));
2005 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2008 ce
.width
= WIDTH(win
);
2009 ce
.height
= HEIGHT(win
);
2010 ce
.override_redirect
= False
;
2015 ce
.window
= win
->id
;
2016 ce
.border_width
= BORDER(win
);
2017 ce
.above_sibling
= XCB_WINDOW_NONE
;
2020 ce
.event
= ev
->window
;
2021 ce
.window
= ev
->window
;
2023 /* make response appear more WM_SIZE_HINTS-compliant */
2025 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2026 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2027 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2028 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2029 SH_INC_W(win
), SH_INC_H(win
));
2033 /* the hint may be set... to 0! */
2034 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2035 ce
.width
= SH_MIN_W(win
);
2036 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2037 ce
.height
= SH_MIN_H(win
);
2042 /* may also be advertized as 0 */
2043 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2044 ce
.width
= SH_MAX_W(win
);
2045 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2046 ce
.height
= SH_MAX_H(win
);
2049 /* resize increment. */
2051 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2052 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2054 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2055 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2059 /* adjust x and y for requested border_width. */
2060 ce
.x
+= BORDER(win
) - ev
->border_width
;
2061 ce
.y
+= BORDER(win
) - ev
->border_width
;
2062 ce
.border_width
= ev
->border_width
;
2063 ce
.above_sibling
= ev
->sibling
;
2066 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2067 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2068 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2070 xcb_send_event(conn
, False
, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2075 count_win(struct workspace
*ws
, int count_transient
)
2080 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2081 if (count_transient
== 0 && win
->floating
)
2083 if (count_transient
== 0 && win
->transient
)
2089 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2095 quit(struct swm_region
*r
, union arg
*args
)
2097 DNPRINTF(SWM_D_MISC
, "quit\n");
2102 unmap_window(struct ws_win
*win
)
2107 /* don't unmap again */
2108 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2111 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2113 xcb_unmap_window(conn
, win
->id
);
2114 xcb_change_window_attributes(conn
, win
->id
,
2115 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2122 int i
, j
, num_screens
;
2124 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2125 for (i
= 0; i
< num_screens
; i
++)
2126 for (j
= 0; j
< workspace_limit
; j
++)
2127 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2132 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2134 xcb_key_press_event_t event
;
2135 xcb_keycode_t
*keycode
;
2140 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2142 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2145 bzero(&event
, sizeof(event
));
2146 event
.event
= win
->id
;
2147 event
.root
= win
->s
->root
;
2148 event
.child
= XCB_WINDOW_NONE
;
2149 event
.time
= XCB_CURRENT_TIME
;
2150 event
.event_x
= X(win
);
2151 event
.event_y
= Y(win
);
2154 event
.same_screen
= True
;
2155 event
.detail
= *keycode
;
2156 event
.state
= modifiers
;
2158 event
.response_type
= XCB_KEY_PRESS
;
2159 xcb_send_event(conn
, True
, win
->id
,
2160 XCB_EVENT_MASK_KEY_PRESS
, (char *)&event
);
2162 event
.response_type
= XCB_KEY_RELEASE
;
2163 xcb_send_event(conn
, True
, win
->id
,
2164 XCB_EVENT_MASK_KEY_RELEASE
, (char *)&event
);
2168 restart(struct swm_region
*r
, union arg
*args
)
2170 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2172 /* disable alarm because the following code may not be interrupted */
2174 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2175 err(1, "can't disable alarm");
2181 xcb_key_symbols_free(syms
);
2183 xcb_disconnect(conn
);
2185 execvp(start_argv
[0], start_argv
);
2186 warn("execvp failed");
2191 root_to_region(xcb_window_t root
)
2193 struct swm_region
*r
= NULL
;
2195 xcb_query_pointer_reply_t
*qpr
;
2197 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2199 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2200 for (i
= 0; i
< num_screens
; i
++)
2201 if (screens
[i
].root
== root
)
2204 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2205 screens
[i
].root
), NULL
);
2208 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2209 qpr
->root_x
, qpr
->root_y
);
2210 /* choose a region based on pointer location */
2211 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2212 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2213 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2219 r
= TAILQ_FIRST(&screens
[i
].rl
);
2225 find_unmanaged_window(xcb_window_t id
)
2228 int i
, j
, num_screens
;
2230 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2231 for (i
= 0; i
< num_screens
; i
++)
2232 for (j
= 0; j
< workspace_limit
; j
++)
2233 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2241 find_window(xcb_window_t id
)
2244 int i
, j
, num_screens
;
2245 xcb_query_tree_cookie_t c
;
2246 xcb_query_tree_reply_t
*r
;
2248 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2249 for (i
= 0; i
< num_screens
; i
++)
2250 for (j
= 0; j
< workspace_limit
; j
++)
2251 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2255 c
= xcb_query_tree(conn
, id
);
2256 r
= xcb_query_tree_reply(conn
, c
, NULL
);
2260 /* if we were looking for the parent return that window instead */
2261 if (r
->parent
== 0 || r
->root
== r
->parent
)
2264 /* look for parent */
2265 for (i
= 0; i
< num_screens
; i
++)
2266 for (j
= 0; j
< workspace_limit
; j
++)
2267 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2268 if (r
->parent
== win
->id
) {
2278 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2283 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2286 close(xcb_get_file_descriptor(conn
));
2288 setenv("LD_PRELOAD", SWM_LIB
, 1);
2290 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2291 warn("spawn: asprintf SWM_WS");
2294 setenv("_SWM_WS", ret
, 1);
2298 if (asprintf(&ret
, "%d", getpid()) == -1) {
2299 warn("spawn: asprintf _SWM_PID");
2302 setenv("_SWM_PID", ret
, 1);
2306 if (setsid() == -1) {
2307 warn("spawn: setsid");
2313 * close stdin and stdout to prevent interaction between apps
2314 * and the baraction script
2315 * leave stderr open to record errors
2317 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2318 warn("spawn: open");
2321 dup2(fd
, STDIN_FILENO
);
2322 dup2(fd
, STDOUT_FILENO
);
2327 execvp(args
->argv
[0], args
->argv
);
2329 warn("spawn: execvp");
2334 kill_refs(struct ws_win
*win
)
2336 int i
, x
, num_screens
;
2337 struct swm_region
*r
;
2338 struct workspace
*ws
;
2343 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2344 for (i
= 0; i
< num_screens
; i
++)
2345 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2346 for (x
= 0; x
< workspace_limit
; x
++) {
2348 if (win
== ws
->focus
)
2350 if (win
== ws
->focus_prev
)
2351 ws
->focus_prev
= NULL
;
2356 validate_win(struct ws_win
*testwin
)
2359 struct workspace
*ws
;
2360 struct swm_region
*r
;
2361 int i
, x
, num_screens
;
2363 if (testwin
== NULL
)
2366 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2367 for (i
= 0; i
< num_screens
; i
++)
2368 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2369 for (x
= 0; x
< workspace_limit
; x
++) {
2371 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2379 validate_ws(struct workspace
*testws
)
2381 struct swm_region
*r
;
2382 struct workspace
*ws
;
2383 int i
, x
, num_screens
;
2385 /* validate all ws */
2386 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2387 for (i
= 0; i
< num_screens
; i
++)
2388 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2389 for (x
= 0; x
< workspace_limit
; x
++) {
2398 unfocus_win(struct ws_win
*win
)
2400 xcb_window_t none
= XCB_WINDOW_NONE
;
2402 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2406 if (win
->ws
== NULL
)
2409 if (validate_ws(win
->ws
))
2410 return; /* XXX this gets hit with thunderbird, needs fixing */
2412 if (win
->ws
->r
== NULL
)
2415 if (validate_win(win
)) {
2420 if (win
->ws
->focus
== win
) {
2421 win
->ws
->focus
= NULL
;
2422 win
->ws
->focus_prev
= win
;
2425 if (validate_win(win
->ws
->focus
)) {
2426 kill_refs(win
->ws
->focus
);
2427 win
->ws
->focus
= NULL
;
2429 if (validate_win(win
->ws
->focus_prev
)) {
2430 kill_refs(win
->ws
->focus_prev
);
2431 win
->ws
->focus_prev
= NULL
;
2434 grabbuttons(win
, 0);
2435 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2436 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2437 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2438 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2445 int i
, j
, num_screens
;
2447 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2449 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2450 for (i
= 0; i
< num_screens
; i
++)
2451 for (j
= 0; j
< workspace_limit
; j
++)
2452 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2457 focus_win(struct ws_win
*win
)
2459 struct ws_win
*cfw
= NULL
;
2460 xcb_get_input_focus_cookie_t c
;
2461 xcb_get_input_focus_reply_t
*r
;
2462 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2464 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2468 if (win
->ws
== NULL
)
2471 if (validate_ws(win
->ws
))
2472 return; /* XXX this gets hit with thunderbird, needs fixing */
2474 if (validate_win(win
)) {
2479 if (validate_win(win
)) {
2484 c
= xcb_get_input_focus(conn
);
2485 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
2487 cur_focus
= r
->focus
;
2490 if ((cfw
= find_window(cur_focus
)) != NULL
)
2493 /* use larger hammer since the window was killed somehow */
2494 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2495 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2496 xcb_change_window_attributes(conn, cfw->id,
2497 XCB_CW_BORDER_PIXEL,
2498 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2501 win
->ws
->focus
= win
;
2503 if (win
->ws
->r
!= NULL
) {
2505 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2506 win
->id
, XCB_CURRENT_TIME
);
2507 grabbuttons(win
, 1);
2508 xcb_change_window_attributes(conn
, win
->id
,
2509 XCB_CW_BORDER_PIXEL
,
2510 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2511 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2512 win
->ws
->always_raise
)
2513 map_window_raised(win
->id
);
2515 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2516 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2524 switchws(struct swm_region
*r
, union arg
*args
)
2526 int wsid
= args
->id
, unmap_old
= 0;
2527 struct swm_region
*this_r
, *other_r
;
2529 struct workspace
*new_ws
, *old_ws
;
2535 if (wsid
>= workspace_limit
)
2539 old_ws
= this_r
->ws
;
2540 new_ws
= &this_r
->s
->ws
[wsid
];
2542 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2543 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2545 if (new_ws
== NULL
|| old_ws
== NULL
)
2547 if (new_ws
== old_ws
)
2550 other_r
= new_ws
->r
;
2551 if (other_r
== NULL
) {
2552 /* the other workspace is hidden, hide this one */
2556 /* the other ws is visible in another region, exchange them */
2557 other_r
->ws_prior
= new_ws
;
2558 other_r
->ws
= old_ws
;
2559 old_ws
->r
= other_r
;
2561 this_r
->ws_prior
= old_ws
;
2562 this_r
->ws
= new_ws
;
2565 /* this is needed so that we can click on a window after a restart */
2569 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2570 focus(new_ws
->r
, &a
);
2572 /* unmap old windows */
2574 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2581 cyclews(struct swm_region
*r
, union arg
*args
)
2584 struct swm_screen
*s
= r
->s
;
2587 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2588 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2593 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2596 case SWM_ARG_ID_CYCLEWS_UP
:
2597 if (a
.id
< workspace_limit
- 1)
2602 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2605 case SWM_ARG_ID_CYCLEWS_DOWN
:
2609 a
.id
= workspace_limit
- 1;
2616 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2618 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2622 } while (a
.id
!= r
->ws
->idx
);
2626 priorws(struct swm_region
*r
, union arg
*args
)
2630 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2631 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2633 if (r
->ws_prior
== NULL
)
2636 a
.id
= r
->ws_prior
->idx
;
2641 cyclescr(struct swm_region
*r
, union arg
*args
)
2643 struct swm_region
*rr
= NULL
;
2645 int i
, x
, y
, num_screens
;
2647 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2648 /* do nothing if we don't have more than one screen */
2649 if (!(num_screens
> 1 || outputs
> 1))
2654 case SWM_ARG_ID_CYCLESC_UP
:
2655 rr
= TAILQ_NEXT(r
, entry
);
2657 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2659 case SWM_ARG_ID_CYCLESC_DOWN
:
2660 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2662 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2670 /* move mouse to region */
2672 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2673 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2676 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2679 if (rr
->ws
->focus
) {
2680 /* move to focus window */
2681 x
= X(rr
->ws
->focus
) + 1;
2682 y
= Y(rr
->ws
->focus
) + 1;
2683 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2689 sort_windows(struct ws_win_list
*wl
)
2691 struct ws_win
*win
, *parent
, *nxt
;
2696 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2697 nxt
= TAILQ_NEXT(win
, entry
);
2698 if (win
->transient
) {
2699 parent
= find_window(win
->transient
);
2700 if (parent
== NULL
) {
2701 warnx("not possible bug");
2704 TAILQ_REMOVE(wl
, win
, entry
);
2705 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2712 swapwin(struct swm_region
*r
, union arg
*args
)
2714 struct ws_win
*target
, *source
;
2715 struct ws_win
*cur_focus
;
2716 struct ws_win_list
*wl
;
2719 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2720 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2722 cur_focus
= r
->ws
->focus
;
2723 if (cur_focus
== NULL
)
2727 wl
= &source
->ws
->winlist
;
2730 case SWM_ARG_ID_SWAPPREV
:
2731 if (source
->transient
)
2732 source
= find_window(source
->transient
);
2733 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2734 if (target
&& target
->transient
)
2735 target
= find_window(target
->transient
);
2736 TAILQ_REMOVE(wl
, source
, entry
);
2738 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2740 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2742 case SWM_ARG_ID_SWAPNEXT
:
2743 target
= TAILQ_NEXT(source
, entry
);
2744 /* move the parent and let the sort handle the move */
2745 if (source
->transient
)
2746 source
= find_window(source
->transient
);
2747 TAILQ_REMOVE(wl
, source
, entry
);
2749 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2751 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2753 case SWM_ARG_ID_SWAPMAIN
:
2754 target
= TAILQ_FIRST(wl
);
2755 if (target
== source
) {
2756 if (source
->ws
->focus_prev
!= NULL
&&
2757 source
->ws
->focus_prev
!= target
)
2758 source
= source
->ws
->focus_prev
;
2762 if (target
== NULL
|| source
== NULL
)
2764 source
->ws
->focus_prev
= target
;
2765 TAILQ_REMOVE(wl
, target
, entry
);
2766 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2767 TAILQ_REMOVE(wl
, source
, entry
);
2768 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2770 case SWM_ARG_ID_MOVELAST
:
2771 TAILQ_REMOVE(wl
, source
, entry
);
2772 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2775 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2787 focus_prev(struct ws_win
*win
)
2789 struct ws_win
*winfocus
= NULL
;
2790 struct ws_win
*cur_focus
= NULL
;
2791 struct ws_win_list
*wl
= NULL
;
2792 struct workspace
*ws
= NULL
;
2794 if (!(win
&& win
->ws
))
2799 cur_focus
= ws
->focus
;
2801 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2802 WINID(win
), WINID(cur_focus
));
2804 /* pickle, just focus on whatever */
2805 if (cur_focus
== NULL
) {
2806 /* use prev_focus if valid */
2807 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2808 find_window(WINID(ws
->focus_prev
)))
2809 winfocus
= ws
->focus_prev
;
2813 /* if transient focus on parent */
2814 if (cur_focus
->transient
) {
2815 winfocus
= find_window(cur_focus
->transient
);
2819 /* if in max_stack try harder */
2820 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2821 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2822 if (cur_focus
!= ws
->focus_prev
)
2823 winfocus
= ws
->focus_prev
;
2825 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2830 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2832 if (winfocus
== NULL
|| winfocus
== win
) {
2833 switch (focus_close
) {
2834 case SWM_STACK_BOTTOM
:
2835 winfocus
= TAILQ_FIRST(wl
);
2838 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2840 case SWM_STACK_ABOVE
:
2841 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2842 if (focus_close_wrap
)
2843 winfocus
= TAILQ_FIRST(wl
);
2845 winfocus
= TAILQ_PREV(cur_focus
,
2846 ws_win_list
, entry
);
2849 case SWM_STACK_BELOW
:
2850 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2852 if (focus_close_wrap
)
2853 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2855 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2861 if (winfocus
== NULL
) {
2862 if (focus_default
== SWM_STACK_TOP
)
2863 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2865 winfocus
= TAILQ_FIRST(wl
);
2869 focus_magic(winfocus
);
2873 focus(struct swm_region
*r
, union arg
*args
)
2875 struct ws_win
*winfocus
= NULL
, *head
;
2876 struct ws_win
*cur_focus
= NULL
;
2877 struct ws_win_list
*wl
= NULL
;
2878 struct workspace
*ws
= NULL
;
2884 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2886 /* treat FOCUS_CUR special */
2887 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2888 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2889 winfocus
= r
->ws
->focus
;
2890 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2891 winfocus
= r
->ws
->focus_prev
;
2893 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2894 if (winfocus
->iconic
== 0)
2897 focus_magic(winfocus
);
2901 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2905 if (TAILQ_EMPTY(wl
))
2907 /* make sure there is at least one uniconified window */
2909 TAILQ_FOREACH(winfocus
, wl
, entry
)
2910 if (winfocus
->iconic
== 0) {
2918 case SWM_ARG_ID_FOCUSPREV
:
2919 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2921 head
= TAILQ_LAST(wl
, ws_win_list
);
2923 if (WINID(winfocus
) == cur_focus
->transient
) {
2924 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2926 head
= TAILQ_LAST(wl
, ws_win_list
);
2931 if (winfocus
&& winfocus
->iconic
) {
2932 while (winfocus
!= cur_focus
) {
2933 if (winfocus
== NULL
)
2934 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2935 if (winfocus
->iconic
== 0)
2937 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2943 case SWM_ARG_ID_FOCUSNEXT
:
2944 head
= TAILQ_NEXT(cur_focus
, entry
);
2946 head
= TAILQ_FIRST(wl
);
2950 if (winfocus
&& winfocus
->iconic
) {
2951 while (winfocus
!= cur_focus
) {
2952 if (winfocus
== NULL
)
2953 winfocus
= TAILQ_FIRST(wl
);
2954 if (winfocus
->iconic
== 0)
2956 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2961 case SWM_ARG_ID_FOCUSMAIN
:
2962 winfocus
= TAILQ_FIRST(wl
);
2963 if (winfocus
== cur_focus
)
2964 winfocus
= cur_focus
->ws
->focus_prev
;
2971 focus_magic(winfocus
);
2977 cycle_layout(struct swm_region
*r
, union arg
*args
)
2979 struct workspace
*ws
= r
->ws
;
2982 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2985 if (ws
->cur_layout
->l_stack
== NULL
)
2986 ws
->cur_layout
= &layouts
[0];
2990 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2996 stack_config(struct swm_region
*r
, union arg
*args
)
2998 struct workspace
*ws
= r
->ws
;
3000 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3003 if (ws
->cur_layout
->l_config
!= NULL
)
3004 ws
->cur_layout
->l_config(ws
, args
->id
);
3006 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3013 struct swm_geometry g
;
3014 struct swm_region
*r
;
3020 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3022 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3023 for (i
= 0; i
< num_screens
; i
++) {
3027 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3028 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3029 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3031 /* start with screen geometry, adjust for bar */
3033 g
.w
-= 2 * border_width
;
3034 g
.h
-= 2 * border_width
;
3040 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3041 r
->ws
->cur_layout
->l_string(r
->ws
);
3042 /* save r so we can track region changes */
3049 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3053 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3055 /* retain window geom and region geom */
3056 win
->g_float
= win
->g
;
3057 win
->g_float
.x
-= X(r
);
3058 win
->g_float
.y
-= Y(r
);
3059 win
->g_floatvalid
= 1;
3060 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3061 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3062 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3063 win
->g_float
.w
, win
->g_float
.h
);
3067 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3072 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3075 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3076 * geom on ws switches or return from max mode
3078 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3079 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3080 /* refloat at last floating relative position */
3081 win
->g
= win
->g_float
;
3086 win
->floatmaxed
= 0;
3089 * if set to fullscreen mode, configure window to maximum size.
3091 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3092 if (!win
->g_floatvalid
)
3093 store_float_geom(win
, win
->ws
->r
);
3095 win
->g
= win
->ws
->r
->g
;
3099 * remove border on fullscreen floater when in fullscreen mode or when
3100 * the quirk is present.
3102 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3103 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3104 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3105 if (win
->bordered
) {
3107 X(win
) += border_width
;
3108 Y(win
) += border_width
;
3110 } else if (!win
->bordered
) {
3112 X(win
) -= border_width
;
3113 Y(win
) -= border_width
;
3116 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3117 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3118 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3123 * floaters and transients are auto-centred unless moved
3126 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3127 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3130 /* keep window within region bounds */
3131 constrain_window(win
, r
, 0);
3137 * Send keystrokes to terminal to decrease/increase the font size as the
3138 * window size changes.
3141 adjust_font(struct ws_win
*win
)
3143 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3144 win
->floating
|| win
->transient
)
3147 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3148 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3149 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3150 win
->font_size_boundary
[win
->font_steps
] =
3151 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3154 win
->last_inc
= win
->sh
.width_inc
;
3155 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3156 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3157 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3160 win
->last_inc
= win
->sh
.width_inc
;
3161 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3165 #define SWAPXY(g) do { \
3167 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3168 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3171 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3173 struct swm_geometry win_g
, r_g
= *g
;
3174 struct ws_win
*win
, *fs_win
= NULL
;
3175 int i
, j
, s
, stacks
;
3176 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3177 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3178 int split
, colno
, winno
, mwin
, msize
, mscale
;
3179 int remain
, missing
, v_slice
, reconfigure
;
3181 xcb_get_window_attributes_reply_t
*war
;
3183 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3184 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3186 winno
= count_win(ws
, 0);
3187 if (winno
== 0 && count_win(ws
, 1) == 0)
3190 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3191 if (win
->transient
== 0 && win
->floating
== 0
3192 && win
->iconic
== 0)
3199 w_inc
= win
->sh
.width_inc
;
3200 w_base
= win
->sh
.base_width
;
3201 mwin
= ws
->l_state
.horizontal_mwin
;
3202 mscale
= ws
->l_state
.horizontal_msize
;
3203 stacks
= ws
->l_state
.horizontal_stacks
;
3206 w_inc
= win
->sh
.height_inc
;
3207 w_base
= win
->sh
.base_height
;
3208 mwin
= ws
->l_state
.vertical_mwin
;
3209 mscale
= ws
->l_state
.vertical_msize
;
3210 stacks
= ws
->l_state
.vertical_stacks
;
3214 if (stacks
> winno
- mwin
)
3215 stacks
= winno
- mwin
;
3219 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3220 if (mwin
&& winno
> mwin
) {
3221 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3225 win_g
.w
= v_slice
* mscale
;
3227 if (w_inc
> 1 && w_inc
< v_slice
) {
3228 /* adjust for window's requested size increment */
3229 remain
= (win_g
.w
- w_base
) % w_inc
;
3235 win_g
.x
+= r_g
.w
- msize
;
3238 colno
= split
= winno
/ stacks
;
3239 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3240 2 * border_width
) / stacks
);
3242 hrh
= r_g
.h
/ colno
;
3243 extra
= r_g
.h
- (colno
* hrh
);
3244 win_g
.h
= hrh
- 2 * border_width
;
3246 /* stack all the tiled windows */
3247 i
= j
= 0, s
= stacks
;
3248 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3249 if (win
->transient
!= 0 || win
->floating
!= 0)
3251 if (win
->iconic
!= 0)
3254 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3259 if (split
&& i
== split
) {
3260 colno
= (winno
- mwin
) / stacks
;
3261 if (s
<= (winno
- mwin
) % stacks
)
3263 split
= split
+ colno
;
3264 hrh
= (r_g
.h
/ colno
);
3265 extra
= r_g
.h
- (colno
* hrh
);
3269 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3270 win_g
.w
= (r_g
.w
- msize
-
3271 (stacks
* 2 * border_width
)) / stacks
;
3273 win_g
.w
+= (r_g
.w
- msize
-
3274 (stacks
* 2 * border_width
)) % stacks
;
3278 win_g
.h
= hrh
- 2 * border_width
;
3280 h_inc
= win
->sh
.width_inc
;
3281 h_base
= win
->sh
.base_width
;
3283 h_inc
= win
->sh
.height_inc
;
3284 h_base
= win
->sh
.base_height
;
3286 if (j
== colno
- 1) {
3287 win_g
.h
= hrh
+ extra
;
3288 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3289 /* adjust for window's requested size increment */
3290 remain
= (win_g
.h
- h_base
) % h_inc
;
3291 missing
= h_inc
- remain
;
3293 if (missing
<= extra
|| j
== 0) {
3305 win_g
.y
+= last_h
+ 2 * border_width
;
3307 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3309 win_g
.w
+= 2 * border_width
;
3310 win_g
.h
+= 2 * border_width
;
3315 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3316 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3320 WIDTH(win
) = win_g
.h
;
3321 HEIGHT(win
) = win_g
.w
;
3324 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3325 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3329 WIDTH(win
) = win_g
.w
;
3330 HEIGHT(win
) = win_g
.h
;
3334 if (bordered
!= win
->bordered
) {
3336 win
->bordered
= bordered
;
3344 war
= xcb_get_window_attributes_reply(conn
,
3345 xcb_get_window_attributes(conn
, win
->id
),
3348 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3349 map_window_raised(win
->id
);
3359 /* now, stack all the floaters and transients */
3360 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3361 if (win
->transient
== 0 && win
->floating
== 0)
3363 if (win
->iconic
== 1)
3365 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3370 stack_floater(win
, ws
->r
);
3371 map_window_raised(win
->id
);
3375 stack_floater(fs_win
, ws
->r
);
3376 map_window_raised(fs_win
->id
);
3381 vertical_config(struct workspace
*ws
, int id
)
3383 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3387 case SWM_ARG_ID_STACKRESET
:
3388 case SWM_ARG_ID_STACKINIT
:
3389 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3390 ws
->l_state
.vertical_mwin
= 1;
3391 ws
->l_state
.vertical_stacks
= 1;
3393 case SWM_ARG_ID_MASTERSHRINK
:
3394 if (ws
->l_state
.vertical_msize
> 1)
3395 ws
->l_state
.vertical_msize
--;
3397 case SWM_ARG_ID_MASTERGROW
:
3398 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3399 ws
->l_state
.vertical_msize
++;
3401 case SWM_ARG_ID_MASTERADD
:
3402 ws
->l_state
.vertical_mwin
++;
3404 case SWM_ARG_ID_MASTERDEL
:
3405 if (ws
->l_state
.vertical_mwin
> 0)
3406 ws
->l_state
.vertical_mwin
--;
3408 case SWM_ARG_ID_STACKINC
:
3409 ws
->l_state
.vertical_stacks
++;
3411 case SWM_ARG_ID_STACKDEC
:
3412 if (ws
->l_state
.vertical_stacks
> 1)
3413 ws
->l_state
.vertical_stacks
--;
3415 case SWM_ARG_ID_FLIPLAYOUT
:
3416 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3424 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3426 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3428 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3432 horizontal_config(struct workspace
*ws
, int id
)
3434 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3437 case SWM_ARG_ID_STACKRESET
:
3438 case SWM_ARG_ID_STACKINIT
:
3439 ws
->l_state
.horizontal_mwin
= 1;
3440 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3441 ws
->l_state
.horizontal_stacks
= 1;
3443 case SWM_ARG_ID_MASTERSHRINK
:
3444 if (ws
->l_state
.horizontal_msize
> 1)
3445 ws
->l_state
.horizontal_msize
--;
3447 case SWM_ARG_ID_MASTERGROW
:
3448 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3449 ws
->l_state
.horizontal_msize
++;
3451 case SWM_ARG_ID_MASTERADD
:
3452 ws
->l_state
.horizontal_mwin
++;
3454 case SWM_ARG_ID_MASTERDEL
:
3455 if (ws
->l_state
.horizontal_mwin
> 0)
3456 ws
->l_state
.horizontal_mwin
--;
3458 case SWM_ARG_ID_STACKINC
:
3459 ws
->l_state
.horizontal_stacks
++;
3461 case SWM_ARG_ID_STACKDEC
:
3462 if (ws
->l_state
.horizontal_stacks
> 1)
3463 ws
->l_state
.horizontal_stacks
--;
3465 case SWM_ARG_ID_FLIPLAYOUT
:
3466 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3474 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3476 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3478 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3481 /* fullscreen view */
3483 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3485 struct swm_geometry gg
= *g
;
3486 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3487 int winno
, num_screens
;
3489 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3494 winno
= count_win(ws
, 0);
3495 if (winno
== 0 && count_win(ws
, 1) == 0)
3498 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3499 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3500 if (win
->transient
) {
3502 parent
= find_window(win
->transient
);
3506 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3508 * retain geometry for retrieval on exit from
3511 store_float_geom(win
, ws
->r
);
3512 win
->floatmaxed
= 1;
3515 /* only reconfigure if necessary */
3516 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3517 HEIGHT(win
) != gg
.h
) {
3523 WIDTH(win
) += 2 * border_width
;
3524 HEIGHT(win
) += 2 * border_width
;
3529 /* unmap only if we don't have multi screen */
3530 if (win
!= ws
->focus
)
3531 if (!(num_screens
> 1 || outputs
> 1))
3535 /* put the last transient on top */
3538 map_window_raised(parent
->id
);
3539 stack_floater(wintrans
, ws
->r
);
3540 focus_magic(wintrans
);
3545 send_to_ws(struct swm_region
*r
, union arg
*args
)
3547 int wsid
= args
->id
;
3548 struct ws_win
*win
= NULL
, *parent
;
3549 struct workspace
*ws
, *nws
;
3550 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
3551 char ws_idx_str
[SWM_PROPLEN
];
3554 if (wsid
>= workspace_limit
)
3557 if (r
&& r
->ws
&& r
->ws
->focus
)
3563 if (win
->ws
->idx
== wsid
)
3566 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3569 nws
= &win
->s
->ws
[wsid
];
3571 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3573 if (win
->transient
) {
3574 parent
= find_window(win
->transient
);
3576 unmap_window(parent
);
3577 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3578 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3583 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3584 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3585 if (TAILQ_EMPTY(&ws
->winlist
))
3586 r
->ws
->focus
= NULL
;
3589 /* Try to update the window's workspace property */
3590 ws_idx_atom
= get_atom_from_string("_SWM_WS");
3592 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3594 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3596 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3597 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3608 pressbutton(struct swm_region
*r
, union arg
*args
)
3610 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3611 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3612 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3613 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3617 raise_toggle(struct swm_region
*r
, union arg
*args
)
3619 if (r
== NULL
|| r
->ws
== NULL
)
3622 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3624 /* bring floaters back to top */
3625 if (r
->ws
->always_raise
== 0)
3632 iconify(struct swm_region
*r
, union arg
*args
)
3636 if (r
->ws
->focus
== NULL
)
3638 unmap_window(r
->ws
->focus
);
3639 update_iconic(r
->ws
->focus
, 1);
3642 r
->ws
->focus
= NULL
;
3643 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3650 get_win_name(xcb_window_t win
)
3653 xcb_get_property_cookie_t c
;
3654 xcb_icccm_get_text_property_reply_t r
;
3656 c
= xcb_icccm_get_wm_name(conn
, win
);
3657 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3658 if (r
.name_len
> 0) {
3659 name
= malloc(r
.name_len
+ 1);
3661 xcb_icccm_get_text_property_reply_wipe(&r
);
3664 memcpy(name
, r
.name
, r
.name_len
);
3665 name
[r
.name_len
] = '\0';
3667 xcb_icccm_get_text_property_reply_wipe(&r
);
3674 uniconify(struct swm_region
*r
, union arg
*args
)
3681 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3683 if (r
== NULL
|| r
->ws
== NULL
)
3686 /* make sure we have anything to uniconify */
3687 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3688 if (win
->ws
== NULL
)
3689 continue; /* should never happen */
3690 if (win
->iconic
== 0)
3698 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3700 spawn_select(r
, args
, "search", &searchpid
);
3702 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3705 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3706 if (win
->ws
== NULL
)
3707 continue; /* should never happen */
3708 if (win
->iconic
== 0)
3711 name
= get_win_name(win
->id
);
3714 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3723 name_workspace(struct swm_region
*r
, union arg
*args
)
3727 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3733 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3735 spawn_select(r
, args
, "name_workspace", &searchpid
);
3737 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3740 fprintf(lfile
, "%s", "");
3745 search_workspace(struct swm_region
*r
, union arg
*args
)
3748 struct workspace
*ws
;
3751 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3757 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3759 spawn_select(r
, args
, "search", &searchpid
);
3761 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3764 for (i
= 0; i
< workspace_limit
; i
++) {
3768 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3769 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3776 search_win_cleanup(void)
3778 struct search_window
*sw
= NULL
;
3780 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3781 xcb_destroy_window(conn
, sw
->indicator
);
3782 xcb_free_gc(conn
, sw
->gc
);
3783 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3789 search_win(struct swm_region
*r
, union arg
*args
)
3791 struct ws_win
*win
= NULL
;
3792 struct search_window
*sw
= NULL
;
3794 uint32_t gcv
[3], wa
[2];
3799 XRectangle ibox
, lbox
;
3800 DNPRINTF(SWM_D_MISC
, "search_win\n");
3803 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3805 spawn_select(r
, args
, "search", &searchpid
);
3807 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3810 TAILQ_INIT(&search_wl
);
3813 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3814 if (win
->iconic
== 1)
3817 sw
= calloc(1, sizeof(struct search_window
));
3819 warn("search_win: calloc");
3821 search_win_cleanup();
3827 snprintf(s
, sizeof s
, "%d", i
);
3830 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3832 w
= xcb_generate_id(conn
);
3833 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3834 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
;
3835 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3836 lbox
.width
+ 4, bar_fs_extents
->max_logical_extent
.height
,
3837 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3838 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3841 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3843 sw
->gc
= xcb_generate_id(conn
);
3844 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
3845 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3847 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3848 XCB_GC_BACKGROUND
| XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
3849 map_window_raised(w
);
3851 xcb_image_text_8(conn
, len
, w
, sw
->gc
, 2,
3852 (bar_fs_extents
->max_logical_extent
.height
-
3853 lbox
.height
) / 2 - lbox
.y
, s
);
3855 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
3857 fprintf(lfile
, "%d\n", i
);
3867 search_resp_uniconify(char *resp
, unsigned long len
)
3873 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3875 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3876 if (win
->iconic
== 0)
3878 name
= get_win_name(win
->id
);
3881 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3886 if (strncmp(s
, resp
, len
) == 0) {
3887 /* XXX this should be a callback to generalize */
3888 update_iconic(win
, 0);
3897 search_resp_name_workspace(char *resp
, unsigned long len
)
3899 struct workspace
*ws
;
3901 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3903 if (search_r
->ws
== NULL
)
3908 free(search_r
->ws
->name
);
3909 search_r
->ws
->name
= NULL
;
3913 ws
->name
= strdup(resp
);
3914 if (ws
->name
== NULL
) {
3915 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3916 "strdup: %s", strerror(errno
));
3923 search_resp_search_workspace(char *resp
, unsigned long len
)
3930 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3934 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3941 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3943 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3950 switchws(search_r
, &a
);
3954 search_resp_search_window(char *resp
, unsigned long len
)
3959 struct search_window
*sw
;
3961 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3965 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3970 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3972 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3979 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3980 if (idx
== sw
->idx
) {
3986 #define MAX_RESP_LEN 1024
3989 search_do_resp(void)
3995 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4000 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4001 warn("search: calloc");
4005 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4007 warn("search: read error");
4010 resp
[rbytes
] = '\0';
4013 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4014 * newline, so work around that by sanitizing the resp now.
4016 resp
[strcspn(resp
, "\n")] = '\0';
4019 switch (search_resp_action
) {
4020 case SWM_SEARCH_UNICONIFY
:
4021 search_resp_uniconify(resp
, len
);
4023 case SWM_SEARCH_NAME_WORKSPACE
:
4024 search_resp_name_workspace(resp
, len
);
4026 case SWM_SEARCH_SEARCH_WORKSPACE
:
4027 search_resp_search_workspace(resp
, len
);
4029 case SWM_SEARCH_SEARCH_WINDOW
:
4030 search_resp_search_window(resp
, len
);
4035 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4036 search_win_cleanup();
4038 search_resp_action
= SWM_SEARCH_NONE
;
4039 close(select_resp_pipe
[0]);
4044 wkill(struct swm_region
*r
, union arg
*args
)
4046 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4048 if (r
->ws
->focus
== NULL
)
4051 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4052 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4054 if (r
->ws
->focus
->can_delete
)
4055 client_msg(r
->ws
->focus
, adelete
);
4062 floating_toggle_win(struct ws_win
*win
)
4064 struct swm_region
*r
;
4074 /* reject floating toggles in max stack mode */
4075 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4078 if (win
->floating
) {
4079 if (!win
->floatmaxed
) {
4080 /* retain position for refloat */
4081 store_float_geom(win
, r
);
4085 if (win
->g_floatvalid
) {
4086 /* refloat at last floating relative position */
4087 X(win
) = win
->g_float
.x
+ X(r
);
4088 Y(win
) = win
->g_float
.y
+ Y(r
);
4089 WIDTH(win
) = win
->g_float
.w
;
4090 HEIGHT(win
) = win
->g_float
.h
;
4095 ewmh_update_actions(win
);
4101 floating_toggle(struct swm_region
*r
, union arg
*args
)
4103 struct ws_win
*win
= r
->ws
->focus
;
4109 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4110 _NET_WM_STATE_TOGGLE
);
4114 if (win
== win
->ws
->focus
) {
4115 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4116 focus(win
->ws
->r
, &a
);
4123 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4125 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4127 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4129 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4132 if (X(win
) + BORDER(win
) < X(r
)) {
4134 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4136 X(win
) = X(r
) - BORDER(win
);
4139 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4141 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4143 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4146 if (Y(win
) + BORDER(win
) < Y(r
)) {
4148 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4150 Y(win
) = Y(r
) - BORDER(win
);
4156 if (HEIGHT(win
) < 1)
4162 update_window(struct ws_win
*win
)
4167 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4168 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4169 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4173 wc
[3] = HEIGHT(win
);
4174 wc
[4] = BORDER(win
);
4176 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4177 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4178 wc
[3], YESNO(win
->bordered
));
4180 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4183 #define SWM_RESIZE_STEPS (50)
4186 resize(struct ws_win
*win
, union arg
*args
)
4188 xcb_timestamp_t timestamp
= 0;
4189 struct swm_region
*r
= NULL
;
4190 int resize_step
= 0;
4191 struct swm_geometry g
;
4192 int top
= 0, left
= 0, resizing
;
4194 unsigned int shape
; /* cursor style */
4195 xcb_cursor_t cursor
;
4196 xcb_font_t cursor_font
;
4197 xcb_grab_pointer_cookie_t gpc
;
4198 xcb_grab_pointer_reply_t
*gpr
;
4199 xcb_query_pointer_reply_t
*xpr
;
4200 xcb_generic_event_t
*evt
;
4201 xcb_motion_notify_event_t
*mne
;
4207 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4210 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4211 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4214 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4217 /* reject resizes in max mode for floaters (transient ok) */
4218 if (win
->floatmaxed
)
4222 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4228 case SWM_ARG_ID_WIDTHSHRINK
:
4229 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4232 case SWM_ARG_ID_WIDTHGROW
:
4233 WIDTH(win
) += SWM_RESIZE_STEPS
;
4236 case SWM_ARG_ID_HEIGHTSHRINK
:
4237 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4240 case SWM_ARG_ID_HEIGHTGROW
:
4241 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4248 constrain_window(win
, r
, 1);
4250 store_float_geom(win
,r
);
4254 /* get cursor offset from window root */
4255 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4262 if (xpr
->win_x
< WIDTH(win
) / 2)
4265 if (xpr
->win_y
< HEIGHT(win
) / 2)
4268 if (args
->id
== SWM_ARG_ID_CENTER
)
4271 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4273 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4275 cursor_font
= xcb_generate_id(conn
);
4276 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4278 cursor
= xcb_generate_id(conn
);
4279 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4280 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4282 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4283 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4285 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4287 xcb_free_cursor(conn
, cursor
);
4288 xcb_close_font(conn
, cursor_font
);
4295 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4297 XMaskEvent(display, MOUSEMASK | ExposureMask |
4298 SubstructureRedirectMask, &ev);
4300 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4301 case XCB_BUTTON_RELEASE
:
4302 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4305 case XCB_MOTION_NOTIFY
:
4306 mne
= (xcb_motion_notify_event_t
*)evt
;
4307 /* cursor offset/delta from start of the operation */
4308 dx
= mne
->root_x
- xpr
->root_x
;
4309 dy
= mne
->root_y
- xpr
->root_y
;
4315 if (args
->id
== SWM_ARG_ID_CENTER
) {
4316 if (g
.h
/ 2 + dy
< 1)
4320 HEIGHT(win
) = g
.h
+ 2 * dy
;
4328 HEIGHT(win
) = g
.h
+ dy
;
4335 if (args
->id
== SWM_ARG_ID_CENTER
) {
4336 if (g
.w
/ 2 + dx
< 1)
4340 WIDTH(win
) = g
.w
+ 2 * dx
;
4348 WIDTH(win
) = g
.w
+ dx
;
4351 constrain_window(win
, r
, 1);
4353 /* not free, don't sync more than 120 times / second */
4354 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4355 timestamp
= mne
->time
;
4370 store_float_geom(win
,r
);
4372 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4373 xcb_free_cursor(conn
, cursor
);
4374 xcb_close_font(conn
, cursor_font
);
4377 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4381 resize_step(struct swm_region
*r
, union arg
*args
)
4383 struct ws_win
*win
= NULL
;
4385 if (r
&& r
->ws
&& r
->ws
->focus
)
4393 #define SWM_MOVE_STEPS (50)
4396 move(struct ws_win
*win
, union arg
*args
)
4398 xcb_timestamp_t timestamp
= 0;
4399 int move_step
= 0, moving
;
4400 struct swm_region
*r
= NULL
;
4401 xcb_font_t cursor_font
;
4402 xcb_cursor_t cursor
;
4403 xcb_grab_pointer_cookie_t gpc
;
4404 xcb_grab_pointer_reply_t
*gpr
;
4405 xcb_query_pointer_reply_t
*qpr
;
4406 xcb_generic_event_t
*evt
;
4407 xcb_motion_notify_event_t
*mne
;
4413 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4416 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4417 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4419 /* in max_stack mode should only move transients */
4420 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4424 if (win
->floating
== 0 && !win
->transient
) {
4425 store_float_geom(win
, r
);
4426 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4429 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4436 case SWM_ARG_ID_MOVELEFT
:
4437 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4440 case SWM_ARG_ID_MOVERIGHT
:
4441 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4444 case SWM_ARG_ID_MOVEUP
:
4445 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4448 case SWM_ARG_ID_MOVEDOWN
:
4449 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4456 constrain_window(win
, r
, 0);
4458 store_float_geom(win
, r
);
4462 cursor_font
= xcb_generate_id(conn
);
4463 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4465 cursor
= xcb_generate_id(conn
);
4466 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4467 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4469 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4470 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4471 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4472 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4474 xcb_free_cursor(conn
, cursor
);
4475 xcb_close_font(conn
, cursor_font
);
4479 /* get cursor offset from window root */
4480 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4483 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4484 xcb_free_cursor(conn
, cursor
);
4485 xcb_close_font(conn
, cursor_font
);
4491 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4492 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4493 case XCB_BUTTON_RELEASE
:
4494 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4497 case XCB_MOTION_NOTIFY
:
4498 mne
= (xcb_motion_notify_event_t
*)evt
;
4499 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4500 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4502 constrain_window(win
, r
, 0);
4504 /* not free, don't sync more than 120 times / second */
4505 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4506 timestamp
= mne
->time
;
4521 store_float_geom(win
, r
);
4523 xcb_free_cursor(conn
, cursor
);
4524 xcb_close_font(conn
, cursor_font
);
4525 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4526 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4530 move_step(struct swm_region
*r
, union arg
*args
)
4532 struct ws_win
*win
= NULL
;
4534 if (r
&& r
->ws
&& r
->ws
->focus
)
4539 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4546 /* user/key callable function IDs */
4635 kf_search_workspace
,
4637 kf_dumpwins
, /* MUST BE LAST */
4641 /* key definitions */
4643 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4648 char name
[SWM_FUNCNAME_LEN
];
4649 void (*func
)(struct swm_region
*r
, union arg
*);
4651 } keyfuncs
[kf_invalid
+ 1] = {
4652 /* name function argument */
4653 { "cycle_layout", cycle_layout
, {0} },
4654 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4655 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4656 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4657 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4658 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4659 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4660 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4661 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4662 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4663 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4664 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4665 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4666 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4667 { "quit", quit
, {0} },
4668 { "restart", restart
, {0} },
4669 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4670 { "ws_1", switchws
, {.id
= 0} },
4671 { "ws_2", switchws
, {.id
= 1} },
4672 { "ws_3", switchws
, {.id
= 2} },
4673 { "ws_4", switchws
, {.id
= 3} },
4674 { "ws_5", switchws
, {.id
= 4} },
4675 { "ws_6", switchws
, {.id
= 5} },
4676 { "ws_7", switchws
, {.id
= 6} },
4677 { "ws_8", switchws
, {.id
= 7} },
4678 { "ws_9", switchws
, {.id
= 8} },
4679 { "ws_10", switchws
, {.id
= 9} },
4680 { "ws_11", switchws
, {.id
= 10} },
4681 { "ws_12", switchws
, {.id
= 11} },
4682 { "ws_13", switchws
, {.id
= 12} },
4683 { "ws_14", switchws
, {.id
= 13} },
4684 { "ws_15", switchws
, {.id
= 14} },
4685 { "ws_16", switchws
, {.id
= 15} },
4686 { "ws_17", switchws
, {.id
= 16} },
4687 { "ws_18", switchws
, {.id
= 17} },
4688 { "ws_19", switchws
, {.id
= 18} },
4689 { "ws_20", switchws
, {.id
= 19} },
4690 { "ws_21", switchws
, {.id
= 20} },
4691 { "ws_22", switchws
, {.id
= 21} },
4692 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4693 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4694 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4695 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4696 { "ws_prior", priorws
, {0} },
4697 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4698 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4699 { "mvws_1", send_to_ws
, {.id
= 0} },
4700 { "mvws_2", send_to_ws
, {.id
= 1} },
4701 { "mvws_3", send_to_ws
, {.id
= 2} },
4702 { "mvws_4", send_to_ws
, {.id
= 3} },
4703 { "mvws_5", send_to_ws
, {.id
= 4} },
4704 { "mvws_6", send_to_ws
, {.id
= 5} },
4705 { "mvws_7", send_to_ws
, {.id
= 6} },
4706 { "mvws_8", send_to_ws
, {.id
= 7} },
4707 { "mvws_9", send_to_ws
, {.id
= 8} },
4708 { "mvws_10", send_to_ws
, {.id
= 9} },
4709 { "mvws_11", send_to_ws
, {.id
= 10} },
4710 { "mvws_12", send_to_ws
, {.id
= 11} },
4711 { "mvws_13", send_to_ws
, {.id
= 12} },
4712 { "mvws_14", send_to_ws
, {.id
= 13} },
4713 { "mvws_15", send_to_ws
, {.id
= 14} },
4714 { "mvws_16", send_to_ws
, {.id
= 15} },
4715 { "mvws_17", send_to_ws
, {.id
= 16} },
4716 { "mvws_18", send_to_ws
, {.id
= 17} },
4717 { "mvws_19", send_to_ws
, {.id
= 18} },
4718 { "mvws_20", send_to_ws
, {.id
= 19} },
4719 { "mvws_21", send_to_ws
, {.id
= 20} },
4720 { "mvws_22", send_to_ws
, {.id
= 21} },
4721 { "bar_toggle", bar_toggle
, {0} },
4722 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4723 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4724 { "float_toggle", floating_toggle
,{0} },
4725 { "version", version
, {0} },
4726 { "spawn_custom", dummykeyfunc
, {0} },
4727 { "iconify", iconify
, {0} },
4728 { "uniconify", uniconify
, {0} },
4729 { "raise_toggle", raise_toggle
, {0} },
4730 { "button2", pressbutton
, {2} },
4731 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4732 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4733 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4734 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4735 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4736 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4737 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4738 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4739 { "name_workspace", name_workspace
, {0} },
4740 { "search_workspace", search_workspace
, {0} },
4741 { "search_win", search_win
, {0} },
4742 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4743 { "invalid key func", NULL
, {0} },
4746 RB_ENTRY(key
) entry
;
4749 enum keyfuncid funcid
;
4752 RB_HEAD(key_tree
, key
);
4755 key_cmp(struct key
*kp1
, struct key
*kp2
)
4757 if (kp1
->keysym
< kp2
->keysym
)
4759 if (kp1
->keysym
> kp2
->keysym
)
4762 if (kp1
->mod
< kp2
->mod
)
4764 if (kp1
->mod
> kp2
->mod
)
4770 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4771 struct key_tree keys
;
4774 enum { client_click
, root_click
};
4776 unsigned int action
;
4778 unsigned int button
;
4779 void (*func
)(struct ws_win
*, union arg
*);
4782 /* action key mouse button func args */
4783 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4784 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4785 { client_click
, MODKEY
, Button1
, move
, {0} },
4789 update_modkey(unsigned int mod
)
4795 RB_FOREACH(kp
, key_tree
, &keys
)
4796 if (kp
->mod
& ShiftMask
)
4797 kp
->mod
= mod
| ShiftMask
;
4801 for (i
= 0; i
< LENGTH(buttons
); i
++)
4802 if (buttons
[i
].mask
& ShiftMask
)
4803 buttons
[i
].mask
= mod
| ShiftMask
;
4805 buttons
[i
].mask
= mod
;
4810 TAILQ_ENTRY(spawn_prog
) entry
;
4815 TAILQ_HEAD(spawn_list
, spawn_prog
);
4816 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4819 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4822 struct spawn_prog
*prog
= NULL
;
4824 char *ap
, **real_args
;
4826 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4829 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4830 if (!strcasecmp(spawn_name
, prog
->name
))
4834 warnx("spawn_custom: program %s not found", spawn_name
);
4838 /* make room for expanded args */
4839 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4840 err(1, "spawn_custom: calloc real_args");
4842 /* expand spawn_args into real_args */
4843 for (i
= 0; i
< prog
->argc
; i
++) {
4845 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4846 if (!strcasecmp(ap
, "$bar_border")) {
4848 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4850 err(1, "spawn_custom border color");
4851 } else if (!strcasecmp(ap
, "$bar_color")) {
4853 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4855 err(1, "spawn_custom bar color");
4856 } else if (!strcasecmp(ap
, "$bar_font")) {
4857 if ((real_args
[i
] = strdup(bar_fonts
))
4859 err(1, "spawn_custom bar fonts");
4860 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4862 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4864 err(1, "spawn_custom color font");
4865 } else if (!strcasecmp(ap
, "$color_focus")) {
4867 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4869 err(1, "spawn_custom color focus");
4870 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4872 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4874 err(1, "spawn_custom color unfocus");
4876 /* no match --> copy as is */
4877 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4878 err(1, "spawn_custom strdup(ap)");
4880 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4885 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4886 for (i
= 0; i
< prog
->argc
; i
++)
4887 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4888 DNPRINTF(SWM_D_SPAWN
, "\n");
4890 *ret_args
= real_args
;
4891 return (prog
->argc
);
4895 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4901 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4905 spawn(r
->ws
->idx
, &a
, 1);
4907 for (i
= 0; i
< spawn_argc
; i
++)
4913 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4919 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4923 if (pipe(select_list_pipe
) == -1)
4924 err(1, "pipe error");
4925 if (pipe(select_resp_pipe
) == -1)
4926 err(1, "pipe error");
4928 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4929 err(1, "could not disable SIGPIPE");
4930 switch (*pid
= fork()) {
4932 err(1, "cannot fork");
4935 if (dup2(select_list_pipe
[0], 0) == -1)
4937 if (dup2(select_resp_pipe
[1], 1) == -1)
4939 close(select_list_pipe
[1]);
4940 close(select_resp_pipe
[0]);
4941 spawn(r
->ws
->idx
, &a
, 0);
4943 default: /* parent */
4944 close(select_list_pipe
[0]);
4945 close(select_resp_pipe
[1]);
4949 for (i
= 0; i
< spawn_argc
; i
++)
4955 spawn_insert(char *name
, char *args
)
4957 char *arg
, *cp
, *ptr
;
4958 struct spawn_prog
*sp
;
4960 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4962 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4963 err(1, "spawn_insert: malloc");
4964 if ((sp
->name
= strdup(name
)) == NULL
)
4965 err(1, "spawn_insert: strdup");
4967 /* convert the arguments to an argument list */
4968 if ((ptr
= cp
= strdup(args
)) == NULL
)
4969 err(1, "spawn_insert: strdup");
4970 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4971 /* empty field; skip it */
4976 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4977 sizeof *sp
->argv
)) == NULL
)
4978 err(1, "spawn_insert: realloc");
4979 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4980 err(1, "spawn_insert: strdup");
4984 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4985 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4989 spawn_remove(struct spawn_prog
*sp
)
4993 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4995 TAILQ_REMOVE(&spawns
, sp
, entry
);
4996 for (i
= 0; i
< sp
->argc
; i
++)
5002 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5006 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5008 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5011 spawn_insert(name
, args
);
5013 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5017 setspawn(char *name
, char *args
)
5019 struct spawn_prog
*sp
;
5021 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5026 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5027 if (!strcmp(sp
->name
, name
)) {
5031 spawn_replace(sp
, name
, args
);
5032 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5036 if (*args
== '\0') {
5037 warnx("error: setspawn: cannot find program: %s", name
);
5041 spawn_insert(name
, args
);
5042 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5046 setconfspawn(char *selector
, char *value
, int flags
)
5048 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5050 setspawn(selector
, value
);
5052 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5059 setconfspawn("term", "xterm", 0);
5060 setconfspawn("spawn_term", "xterm", 0);
5061 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5062 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5063 setconfspawn("lock", "xlock", 0);
5064 setconfspawn("initscr", "initscreen.sh", 0);
5065 setconfspawn("menu", "dmenu_run"
5068 " -nf $bar_font_color"
5070 " -sf $bar_color", 0);
5071 setconfspawn("search", "dmenu"
5075 " -nf $bar_font_color"
5077 " -sf $bar_color", 0);
5078 setconfspawn("name_workspace", "dmenu"
5082 " -nf $bar_font_color"
5084 " -sf $bar_color", 0);
5088 #define SWM_MODNAME_SIZE 32
5089 #define SWM_KEY_WS "\n+ \t"
5091 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5095 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5096 if (mod
== NULL
|| ks
== NULL
) {
5097 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5100 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5101 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5107 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5108 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5110 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5111 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5113 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5115 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5117 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5119 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5121 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5123 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5124 *mod
|= ControlMask
;
5126 *ks
= XStringToKeysym(name
);
5127 XConvertCase(*ks
, ks
, &uks
);
5128 if (ks
== NoSymbol
) {
5130 "parsekeys: invalid key %s\n",
5136 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5141 strdupsafe(char *str
)
5146 return (strdup(str
));
5150 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5154 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5155 keyfuncs
[kfid
].name
, spawn_name
);
5157 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5158 err(1, "key_insert: malloc");
5163 kp
->spawn_name
= strdupsafe(spawn_name
);
5164 RB_INSERT(key_tree
, &keys
, kp
);
5166 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5170 key_lookup(unsigned int mod
, KeySym ks
)
5177 return (RB_FIND(key_tree
, &keys
, &kp
));
5181 key_remove(struct key
*kp
)
5183 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5185 RB_REMOVE(key_tree
, &keys
, kp
);
5186 free(kp
->spawn_name
);
5189 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5193 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5196 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5200 key_insert(mod
, ks
, kfid
, spawn_name
);
5202 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5206 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5211 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5212 keyfuncs
[kfid
].name
, spawn_name
);
5214 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5215 if (kfid
== kf_invalid
)
5218 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5219 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5222 if (kfid
== kf_invalid
) {
5223 warnx("error: setkeybinding: cannot find mod/key combination");
5224 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5228 key_insert(mod
, ks
, kfid
, spawn_name
);
5229 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5233 setconfbinding(char *selector
, char *value
, int flags
)
5235 enum keyfuncid kfid
;
5238 struct spawn_prog
*sp
;
5239 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5240 if (selector
== NULL
) {
5241 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5242 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5244 setkeybinding(mod
, ks
, kfid
, NULL
);
5249 /* search by key function name */
5250 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5251 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5252 SWM_FUNCNAME_LEN
) == 0) {
5253 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5255 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5256 setkeybinding(mod
, ks
, kfid
, NULL
);
5262 /* search by custom spawn name */
5263 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5264 if (strcasecmp(selector
, sp
->name
) == 0) {
5265 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5267 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5268 setkeybinding(mod
, ks
, kf_spawn_custom
,
5275 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5282 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5283 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5284 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5285 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5286 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5287 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5288 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5289 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5290 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5291 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5292 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5293 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5294 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5295 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5296 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5297 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5298 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5299 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5300 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5301 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5302 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5303 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5304 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5305 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5306 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5307 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5308 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5309 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5310 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5311 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5312 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5313 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5314 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5315 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5316 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5317 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5318 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5319 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5320 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5321 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5322 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5323 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5324 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5325 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5326 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5327 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5328 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5329 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5330 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5331 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5332 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5333 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5334 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5335 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5336 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5337 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5338 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5339 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5340 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5341 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5342 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5343 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5344 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5345 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5346 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5347 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5348 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5349 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5350 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5351 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5352 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5353 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5354 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5355 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5356 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5357 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5358 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5359 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5360 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5361 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5362 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5363 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5364 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5365 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5366 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5367 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5368 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5369 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5370 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5371 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5372 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5373 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5374 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5375 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5376 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5377 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5379 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5388 while (RB_EMPTY(&keys
) == 0) {
5389 kp
= RB_ROOT(&keys
);
5395 setkeymapping(char *selector
, char *value
, int flags
)
5397 char keymapping_file
[PATH_MAX
];
5398 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5399 if (value
[0] == '~')
5400 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5401 pwd
->pw_dir
, &value
[1]);
5403 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5405 /* load new key bindings; if it fails, revert to default bindings */
5406 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5410 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5415 updatenumlockmask(void)
5418 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5419 xcb_keycode_t
*modmap
, kc
;
5421 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5424 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5425 xcb_get_modifier_mapping(conn
),
5428 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5429 for (i
= 0; i
< 8; i
++) {
5430 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5431 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5434 if (kc
== *((xcb_keycode_t
*)xcb_key_symbols_get_keycode(syms
,
5436 numlockmask
= (1 << i
);
5448 xcb_keycode_t
*code
;
5449 unsigned int modifiers
[] =
5450 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5453 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5454 updatenumlockmask();
5456 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5457 for (k
= 0; k
< num_screens
; k
++) {
5458 if (TAILQ_EMPTY(&screens
[k
].rl
))
5460 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5462 RB_FOREACH(kp
, key_tree
, &keys
) {
5463 if ((code
= xcb_key_symbols_get_keycode(syms
,
5465 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5466 xcb_grab_key(conn
, True
,
5468 kp
->mod
| modifiers
[j
],
5469 *code
, XCB_GRAB_MODE_ASYNC
,
5470 XCB_GRAB_MODE_ASYNC
);
5476 grabbuttons(struct ws_win
*win
, int focused
)
5479 unsigned int modifiers
[] =
5480 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5482 updatenumlockmask();
5483 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5484 XCB_BUTTON_MASK_ANY
);
5486 for (i
= 0; i
< LENGTH(buttons
); i
++)
5487 if (buttons
[i
].action
== client_click
)
5488 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5489 xcb_grab_button(conn
, False
, win
->id
,
5491 XCB_GRAB_MODE_ASYNC
,
5498 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5499 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5500 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5503 const char *quirkname
[] = {
5504 "NONE", /* config string for "no value" */
5513 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5514 #define SWM_Q_WS "\n|+ \t"
5516 parsequirks(char *qstr
, unsigned long *quirk
)
5526 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5528 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5529 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5530 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5531 DNPRINTF(SWM_D_QUIRK
,
5532 "parsequirks: %s\n", name
);
5537 *quirk
|= 1 << (i
-1);
5541 if (i
>= LENGTH(quirkname
)) {
5542 DNPRINTF(SWM_D_QUIRK
,
5543 "parsequirks: invalid quirk [%s]\n", name
);
5551 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5555 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5558 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5559 err(1, "quirk_insert: malloc");
5560 if ((qp
->class = strdup(class)) == NULL
)
5561 err(1, "quirk_insert: strdup");
5562 if ((qp
->name
= strdup(name
)) == NULL
)
5563 err(1, "quirk_insert: strdup");
5566 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5568 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5572 quirk_remove(struct quirk
*qp
)
5574 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5575 qp
->name
, qp
->quirk
);
5577 TAILQ_REMOVE(&quirks
, qp
, entry
);
5582 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5586 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5587 unsigned long quirk
)
5589 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5590 qp
->name
, qp
->quirk
);
5593 quirk_insert(class, name
, quirk
);
5595 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5599 setquirk(const char *class, const char *name
, unsigned long quirk
)
5603 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5606 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5607 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5611 quirk_replace(qp
, class, name
, quirk
);
5612 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5617 warnx("error: setquirk: cannot find class/name combination");
5621 quirk_insert(class, name
, quirk
);
5622 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5626 setconfquirk(char *selector
, char *value
, int flags
)
5628 char *cp
, *class, *name
;
5630 unsigned long quirks
;
5631 if (selector
== NULL
)
5633 if ((cp
= strchr(selector
, ':')) == NULL
)
5638 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5639 setquirk(class, name
, quirks
);
5646 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5647 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5648 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5649 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5650 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5651 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5652 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5653 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5654 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5655 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5656 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5657 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5658 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5661 /* conf file stuff */
5662 #define SWM_CONF_FILE "spectrwm.conf"
5663 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5667 SWM_S_BAR_AT_BOTTOM
,
5668 SWM_S_BAR_BORDER_WIDTH
,
5675 SWM_S_CLOCK_ENABLED
,
5678 SWM_S_CYCLE_VISIBLE
,
5680 SWM_S_DISABLE_BORDER
,
5682 SWM_S_FOCUS_CLOSE_WRAP
,
5683 SWM_S_FOCUS_DEFAULT
,
5689 SWM_S_STACK_ENABLED
,
5691 SWM_S_TITLE_CLASS_ENABLED
,
5692 SWM_S_TITLE_NAME_ENABLED
,
5693 SWM_S_URGENT_ENABLED
,
5694 SWM_S_VERBOSE_LAYOUT
,
5695 SWM_S_WINDOW_NAME_ENABLED
,
5696 SWM_S_WORKSPACE_LIMIT
5700 setconfvalue(char *selector
, char *value
, int flags
)
5706 case SWM_S_BAR_ACTION
:
5708 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5709 err(1, "setconfvalue: bar_action");
5711 case SWM_S_BAR_AT_BOTTOM
:
5712 bar_at_bottom
= atoi(value
);
5714 case SWM_S_BAR_BORDER_WIDTH
:
5715 bar_border_width
= atoi(value
);
5716 if (bar_border_width
< 0)
5717 bar_border_width
= 0;
5719 case SWM_S_BAR_DELAY
:
5720 bar_delay
= atoi(value
);
5722 case SWM_S_BAR_ENABLED
:
5723 bar_enabled
= atoi(value
);
5725 case SWM_S_BAR_FONT
:
5727 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5728 err(1, "setconfvalue: asprintf: failed to allocate "
5729 "memory for bar_fonts.");
5733 case SWM_S_BAR_FORMAT
:
5735 if ((bar_format
= strdup(value
)) == NULL
)
5736 err(1, "setconfvalue: bar_format");
5738 case SWM_S_BAR_JUSTIFY
:
5739 if (!strcmp(value
, "left"))
5740 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5741 else if (!strcmp(value
, "center"))
5742 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5743 else if (!strcmp(value
, "right"))
5744 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5746 errx(1, "invalid bar_justify");
5748 case SWM_S_BORDER_WIDTH
:
5749 border_width
= atoi(value
);
5750 if (border_width
< 0)
5753 case SWM_S_CLOCK_ENABLED
:
5754 clock_enabled
= atoi(value
);
5756 case SWM_S_CLOCK_FORMAT
:
5757 #ifndef SWM_DENY_CLOCK_FORMAT
5759 if ((clock_format
= strdup(value
)) == NULL
)
5760 err(1, "setconfvalue: clock_format");
5763 case SWM_S_CYCLE_EMPTY
:
5764 cycle_empty
= atoi(value
);
5766 case SWM_S_CYCLE_VISIBLE
:
5767 cycle_visible
= atoi(value
);
5769 case SWM_S_DIALOG_RATIO
:
5770 dialog_ratio
= atof(value
);
5771 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5774 case SWM_S_DISABLE_BORDER
:
5775 disable_border
= atoi(value
);
5777 case SWM_S_FOCUS_CLOSE
:
5778 if (!strcmp(value
, "first"))
5779 focus_close
= SWM_STACK_BOTTOM
;
5780 else if (!strcmp(value
, "last"))
5781 focus_close
= SWM_STACK_TOP
;
5782 else if (!strcmp(value
, "next"))
5783 focus_close
= SWM_STACK_ABOVE
;
5784 else if (!strcmp(value
, "previous"))
5785 focus_close
= SWM_STACK_BELOW
;
5787 errx(1, "focus_close");
5789 case SWM_S_FOCUS_CLOSE_WRAP
:
5790 focus_close_wrap
= atoi(value
);
5792 case SWM_S_FOCUS_DEFAULT
:
5793 if (!strcmp(value
, "last"))
5794 focus_default
= SWM_STACK_TOP
;
5795 else if (!strcmp(value
, "first"))
5796 focus_default
= SWM_STACK_BOTTOM
;
5798 errx(1, "focus_default");
5800 case SWM_S_FOCUS_MODE
:
5801 if (!strcmp(value
, "default"))
5802 focus_mode
= SWM_FOCUS_DEFAULT
;
5803 else if (!strcmp(value
, "follow_cursor"))
5804 focus_mode
= SWM_FOCUS_FOLLOW
;
5805 else if (!strcmp(value
, "synergy"))
5806 focus_mode
= SWM_FOCUS_SYNERGY
;
5808 errx(1, "focus_mode");
5810 case SWM_S_SPAWN_ORDER
:
5811 if (!strcmp(value
, "first"))
5812 spawn_position
= SWM_STACK_BOTTOM
;
5813 else if (!strcmp(value
, "last"))
5814 spawn_position
= SWM_STACK_TOP
;
5815 else if (!strcmp(value
, "next"))
5816 spawn_position
= SWM_STACK_ABOVE
;
5817 else if (!strcmp(value
, "previous"))
5818 spawn_position
= SWM_STACK_BELOW
;
5820 errx(1, "spawn_position");
5822 case SWM_S_SPAWN_TERM
:
5823 setconfspawn("term", value
, 0);
5824 setconfspawn("spawn_term", value
, 0);
5828 case SWM_S_SS_ENABLED
:
5829 ss_enabled
= atoi(value
);
5831 case SWM_S_STACK_ENABLED
:
5832 stack_enabled
= atoi(value
);
5834 case SWM_S_TERM_WIDTH
:
5835 term_width
= atoi(value
);
5839 case SWM_S_TITLE_CLASS_ENABLED
:
5840 title_class_enabled
= atoi(value
);
5842 case SWM_S_TITLE_NAME_ENABLED
:
5843 title_name_enabled
= atoi(value
);
5845 case SWM_S_URGENT_ENABLED
:
5846 urgent_enabled
= atoi(value
);
5848 case SWM_S_VERBOSE_LAYOUT
:
5849 verbose_layout
= atoi(value
);
5850 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5852 layouts
[i
].l_string
= fancy_stacker
;
5854 layouts
[i
].l_string
= plain_stacker
;
5857 case SWM_S_WINDOW_NAME_ENABLED
:
5858 window_name_enabled
= atoi(value
);
5860 case SWM_S_WORKSPACE_LIMIT
:
5861 workspace_limit
= atoi(value
);
5862 if (workspace_limit
> SWM_WS_MAX
)
5863 workspace_limit
= SWM_WS_MAX
;
5864 else if (workspace_limit
< 1)
5865 workspace_limit
= 1;
5874 setconfmodkey(char *selector
, char *value
, int flags
)
5876 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5877 update_modkey(Mod1Mask
);
5878 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5879 update_modkey(Mod2Mask
);
5880 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5881 update_modkey(Mod3Mask
);
5882 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5883 update_modkey(Mod4Mask
);
5890 setconfcolor(char *selector
, char *value
, int flags
)
5892 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5897 setconfregion(char *selector
, char *value
, int flags
)
5899 custom_region(value
);
5904 setautorun(char *selector
, char *value
, int flags
)
5914 if (getenv("SWM_STARTED"))
5918 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5919 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5921 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5922 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5925 * This is a little intricate
5927 * If the pid already exists we simply reuse it because it means it was
5928 * used before AND not claimed by manage_window. We get away with
5929 * altering it in the parent after INSERT because this can not be a race
5932 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5935 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5937 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5938 err(1, "setautorun: realloc");
5939 a
.argv
[argc
- 1] = ap
;
5942 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5943 err(1, "setautorun: realloc");
5944 a
.argv
[argc
] = NULL
;
5946 if ((pid
= fork()) == 0) {
5947 spawn(ws_id
, &a
, 1);
5956 p
= calloc(1, sizeof *p
);
5959 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5969 setlayout(char *selector
, char *value
, int flags
)
5971 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5972 int st
= SWM_V_STACK
, num_screens
;
5974 struct workspace
*ws
;
5976 if (getenv("SWM_STARTED"))
5980 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5981 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5982 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5983 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5986 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5987 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5989 if (!strcasecmp(s
, "vertical"))
5991 else if (!strcasecmp(s
, "vertical_flip")) {
5994 } else if (!strcasecmp(s
, "horizontal"))
5996 else if (!strcasecmp(s
, "horizontal_flip")) {
5999 } else if (!strcasecmp(s
, "fullscreen"))
6002 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6003 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6006 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6007 for (i
= 0; i
< num_screens
; i
++) {
6008 ws
= (struct workspace
*)&screens
[i
].ws
;
6009 ws
[ws_id
].cur_layout
= &layouts
[st
];
6011 ws
[ws_id
].always_raise
= raise
;
6012 if (st
== SWM_MAX_STACK
)
6016 for (x
= 0; x
< abs(mg
); x
++) {
6017 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6018 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6019 SWM_ARG_ID_MASTERSHRINK
);
6023 for (x
= 0; x
< abs(ma
); x
++) {
6024 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6025 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6026 SWM_ARG_ID_MASTERDEL
);
6030 for (x
= 0; x
< abs(si
); x
++) {
6031 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6032 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6033 SWM_ARG_ID_STACKDEC
);
6038 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6039 SWM_ARG_ID_FLIPLAYOUT
);
6047 /* config options */
6048 struct config_option
{
6050 int (*func
)(char*, char*, int);
6053 struct config_option configopt
[] = {
6054 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6055 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6056 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6057 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6058 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6059 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6060 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6061 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6062 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6063 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6064 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6065 { "keyboard_mapping", setkeymapping
, 0 },
6066 { "bind", setconfbinding
, 0 },
6067 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6068 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6069 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6070 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6071 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6072 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6073 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6074 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6075 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6076 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6077 { "modkey", setconfmodkey
, 0 },
6078 { "program", setconfspawn
, 0 },
6079 { "quirk", setconfquirk
, 0 },
6080 { "region", setconfregion
, 0 },
6081 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6082 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6083 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6084 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6085 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6086 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6087 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6088 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6089 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6090 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6091 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6092 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6093 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6094 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6095 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6096 { "autorun", setautorun
, 0 },
6097 { "layout", setlayout
, 0 },
6102 conf_load(char *filename
, int keymapping
)
6105 char *line
, *cp
, *optsub
, *optval
;
6106 size_t linelen
, lineno
= 0;
6107 int wordlen
, i
, optind
;
6108 struct config_option
*opt
;
6110 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6112 if (filename
== NULL
) {
6113 warnx("conf_load: no filename");
6116 if ((config
= fopen(filename
, "r")) == NULL
) {
6117 warn("conf_load: fopen: %s", filename
);
6121 while (!feof(config
)) {
6122 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6125 err(1, "%s", filename
);
6130 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6131 if (cp
[0] == '\0') {
6136 /* get config option */
6137 wordlen
= strcspn(cp
, "=[ \t\n");
6139 warnx("%s: line %zd: no option found",
6144 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6145 opt
= &configopt
[i
];
6146 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6147 strlen(opt
->optname
) == wordlen
) {
6153 warnx("%s: line %zd: unknown option %.*s",
6154 filename
, lineno
, wordlen
, cp
);
6157 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6158 warnx("%s: line %zd: invalid option %.*s",
6159 filename
, lineno
, wordlen
, cp
);
6163 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6164 /* get [selector] if any */
6168 wordlen
= strcspn(cp
, "]");
6171 warnx("%s: line %zd: syntax error",
6176 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6178 warnx("%s: line %zd: unable to allocate"
6179 "memory for selector", filename
,
6185 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6187 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6189 optval
= strdup(cp
);
6190 /* call function to deal with it all */
6191 if (configopt
[optind
].func(optsub
, optval
,
6192 configopt
[optind
].funcflags
) != 0)
6193 errx(1, "%s: line %zd: invalid data for %s",
6194 filename
, lineno
, configopt
[optind
].optname
);
6201 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6208 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6214 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6216 struct ws_win
*parent
, *w
;
6217 struct swm_region
*r
;
6218 struct workspace
*ws
;
6219 xcb_icccm_wm_hints_t wmh
;
6221 parent
= find_window(win
->transient
);
6223 parent
->child_trans
= win
;
6225 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6226 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6228 r
= root_to_region(win
->wa
->root
);
6230 /* parent doen't exist in our window list */
6231 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6232 if (xcb_icccm_get_wm_hints_reply(conn
,
6233 xcb_icccm_get_wm_hints(conn
, w
->id
),
6235 warnx("can't get hints for 0x%x", w
->id
);
6239 if (win
->hints
.window_group
!= wmh
.window_group
)
6242 w
->child_trans
= win
;
6243 win
->transient
= w
->id
;
6245 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6246 "transient to 0x%x\n", win
->transient
);
6253 window_get_pid(xcb_window_t win
)
6258 xcb_get_property_cookie_t pc
;
6259 xcb_get_property_reply_t
*pr
;
6261 apid
= get_atom_from_string("_NET_WM_PID");
6262 if (apid
== XCB_ATOM_NONE
)
6265 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6266 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6269 if (pr
->type
!= XCB_ATOM_CARDINAL
)
6272 ret
= *(long *)xcb_get_property_value(pr
);
6278 apid
= get_atom_from_string("_SWM_PID");
6279 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6281 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6284 if (pr
->type
!= XCB_ATOM_STRING
)
6287 ret
= strtonum(xcb_get_property_value(pr
), 0, UINT_MAX
, &errstr
);
6294 manage_window(xcb_window_t id
)
6296 xcb_window_t trans
= XCB_WINDOW_NONE
;
6297 struct workspace
*ws
;
6298 struct ws_win
*win
, *ww
;
6299 int i
, ws_idx
, border_me
= 0;
6300 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
6301 char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6303 struct swm_region
*r
;
6307 uint32_t event_mask
;
6309 xcb_get_property_reply_t
*gpr
;
6310 xcb_icccm_get_wm_protocols_reply_t wpr
;
6312 if ((win
= find_window(id
)) != NULL
)
6313 return (win
); /* already being managed */
6315 /* see if we are on the unmanaged list */
6316 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6317 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6318 "window: 0x%x\n", win
->id
);
6319 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6321 set_child_transient(win
, &trans
);
6323 if (trans
&& (ww
= find_window(trans
)))
6324 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6325 else if ((ww
= win
->ws
->focus
) &&
6326 spawn_position
== SWM_STACK_ABOVE
)
6327 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6329 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6330 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6331 else switch (spawn_position
) {
6334 case SWM_STACK_ABOVE
:
6335 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6337 case SWM_STACK_BOTTOM
:
6338 case SWM_STACK_BELOW
:
6339 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6342 ewmh_update_actions(win
);
6346 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6347 err(1, "manage_window: calloc: failed to allocate memory for "
6353 /* see if we need to override the workspace */
6354 p
= find_pid(window_get_pid(id
));
6356 /* Get all the window data in one shot */
6357 ws_idx_atom
= get_atom_from_string("_SWM_WS");
6359 gpr
= xcb_get_property_reply(conn
,
6360 xcb_get_property(conn
, False
, id
, ws_idx_atom
,
6361 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6364 proplen
= xcb_get_property_value_length(gpr
);
6366 prop
= malloc(proplen
+ 1);
6369 xcb_get_property_value(gpr
),
6371 prop
[proplen
] = '\0';
6377 win
->wa
= xcb_get_geometry_reply(conn
,
6378 xcb_get_geometry(conn
, id
),
6380 xcb_icccm_get_wm_normal_hints_reply(conn
,
6381 xcb_icccm_get_wm_normal_hints(conn
, id
),
6383 xcb_icccm_get_wm_hints_reply(conn
,
6384 xcb_icccm_get_wm_hints(conn
, id
),
6386 xcb_icccm_get_wm_transient_for_reply(conn
,
6387 xcb_icccm_get_wm_transient_for(conn
, id
),
6390 win
->transient
= trans
;
6391 set_child_transient(win
, &trans
);
6392 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6393 "transient: 0x%x\n", win
->id
, win
->transient
);
6396 prot
= get_atom_from_string("WM_PROTOCOLS");
6398 /* get supported protocols */
6399 if (xcb_icccm_get_wm_protocols_reply(conn
,
6400 xcb_icccm_get_wm_protocols(conn
, id
, prot
),
6402 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6403 if (wpr
.atoms
[i
] == takefocus
)
6404 win
->take_focus
= 1;
6405 if (wpr
.atoms
[i
] == adelete
)
6406 win
->can_delete
= 1;
6408 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6411 win
->iconic
= get_iconic(win
);
6414 * Figure out where to put the window. If it was previously assigned to
6415 * a workspace (either by spawn() or manually moving), and isn't
6416 * transient, * put it in the same workspace
6418 r
= root_to_region(win
->wa
->root
);
6420 ws
= &r
->s
->ws
[p
->ws
];
6421 TAILQ_REMOVE(&pidlist
, p
, entry
);
6424 } else if (prop
&& win
->transient
== 0) {
6425 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6426 ws_idx
= strtonum(prop
, 0, workspace_limit
- 1,
6429 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6432 ws
= &r
->s
->ws
[ws_idx
];
6435 /* this should launch transients in the same ws as parent */
6437 if ((ww
= find_window(trans
)) != NULL
)
6443 warnx("manage_window: fix this "
6449 /* set up the window layout */
6452 win
->s
= r
->s
; /* this never changes */
6453 if (trans
&& (ww
= find_window(trans
)))
6454 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6455 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6456 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6458 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6459 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6460 else switch (spawn_position
) {
6463 case SWM_STACK_ABOVE
:
6464 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6466 case SWM_STACK_BOTTOM
:
6467 case SWM_STACK_BELOW
:
6468 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6471 /* ignore window border if there is one. */
6472 WIDTH(win
) = win
->wa
->width
;
6473 HEIGHT(win
) = win
->wa
->height
;
6474 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6475 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6477 win
->g_floatvalid
= 0;
6478 win
->floatmaxed
= 0;
6479 win
->ewmh_flags
= 0;
6481 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6482 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6483 HEIGHT(win
), ws
->idx
);
6485 constrain_window(win
, r
, 0);
6487 /* Set window properties so we can remember this after reincarnation */
6488 if (ws_idx_atom
&& prop
== NULL
&&
6489 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6491 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6493 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6494 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6500 ewmh_autoquirk(win
);
6502 if (xcb_icccm_get_wm_class_reply(conn
,
6503 xcb_icccm_get_wm_class(conn
, win
->id
),
6505 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6506 win
->ch
.class_name
, win
->ch
.instance_name
);
6508 /* java is retarded so treat it special */
6509 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6514 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6515 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6516 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6517 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6518 "class: %s, name: %s\n", win
->ch
.class_name
,
6519 win
->ch
.instance_name
);
6520 if (qp
->quirk
& SWM_Q_FLOAT
) {
6524 win
->quirks
= qp
->quirk
;
6529 /* alter window position if quirky */
6530 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6531 win
->manual
= 1; /* don't center the quirky windows */
6532 if (bar_enabled
&& Y(win
) < bar_height
)
6533 Y(win
) = bar_height
;
6534 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6535 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6539 /* Reset font sizes (the bruteforce way; no default keybinding). */
6540 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6541 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6542 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6543 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6544 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6547 ewmh_get_win_state(win
);
6548 ewmh_update_actions(win
);
6549 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6554 X(win
) -= border_width
;
6555 Y(win
) -= border_width
;
6559 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6560 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6562 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6564 /* floaters need to be mapped if they are in the current workspace */
6565 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6566 map_window_raised(win
->id
);
6572 free_window(struct ws_win
*win
)
6574 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6579 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6584 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6589 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6592 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6596 unmanage_window(struct ws_win
*win
)
6598 struct ws_win
*parent
;
6599 xcb_screen_t
*screen
;
6604 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6606 if (win
->transient
) {
6607 parent
= find_window(win
->transient
);
6609 parent
->child_trans
= NULL
;
6612 /* focus on root just in case */
6613 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6614 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6615 screen
->root
, XCB_CURRENT_TIME
);
6619 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6620 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6624 focus_magic(struct ws_win
*win
)
6626 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6629 /* if there are no windows clear the status-bar */
6634 if (win
->child_trans
) {
6635 /* win = parent & has a transient so focus on that */
6637 focus_win(win
->child_trans
);
6638 if (win
->child_trans
->take_focus
)
6639 client_msg(win
, takefocus
);
6641 /* make sure transient hasn't disappeared */
6642 if (validate_win(win
->child_trans
) == 0) {
6643 focus_win(win
->child_trans
);
6644 if (win
->child_trans
->take_focus
)
6645 client_msg(win
->child_trans
, takefocus
);
6647 win
->child_trans
= NULL
;
6649 if (win
->take_focus
)
6650 client_msg(win
, takefocus
);
6656 if (win
->take_focus
)
6657 client_msg(win
, takefocus
);
6662 expose(xcb_expose_event_t
*e
)
6665 struct swm_region
*r
;
6667 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6669 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6670 for (i
= 0; i
< num_screens
; i
++)
6671 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6672 if (e
->window
== WINID(r
->bar
))
6679 keypress(xcb_key_press_event_t
*e
)
6683 struct swm_region
*r
;
6685 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)e
->detail
, 0, 0);
6687 DNPRINTF(SWM_D_EVENT
, "keypress: %u\n", e
->detail
);
6689 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6691 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6694 r
= root_to_region(e
->root
);
6695 if (kp
->funcid
== kf_spawn_custom
)
6696 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6698 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6702 buttonpress(xcb_button_press_event_t
*e
)
6707 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
6708 e
->event
, e
->detail
);
6710 if ((win
= find_window(e
->event
)) == NULL
)
6714 action
= client_click
;
6716 for (i
= 0; i
< LENGTH(buttons
); i
++)
6717 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6718 buttons
[i
].button
== e
->detail
&&
6719 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6720 buttons
[i
].func(win
, &buttons
[i
].args
);
6724 configurerequest(xcb_configure_request_event_t
*e
)
6729 uint32_t wc
[7] = {0};
6731 if ((win
= find_window(e
->window
)) == NULL
)
6732 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6736 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6737 mask
|= XCB_CONFIG_WINDOW_X
;
6740 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6741 mask
|= XCB_CONFIG_WINDOW_Y
;
6744 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6745 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6748 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6749 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6750 wc
[i
++] = e
->height
;
6752 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6753 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6754 wc
[i
++] = e
->border_width
;
6756 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6757 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6758 wc
[i
++] = e
->sibling
;
6760 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6761 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6762 wc
[i
++] = e
->stack_mode
;
6765 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6766 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e
->window
,
6767 YESNO(new), wc
[0], wc
[1], wc
[2], wc
[3]);
6769 xcb_configure_window(conn
, e
->window
, mask
, wc
);
6770 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6771 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
6772 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
6773 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
6774 win
->g_float
.w
= e
->width
;
6775 win
->g_float
.h
= e
->height
;
6776 win
->g_floatvalid
= 1;
6778 if (win
->floating
) {
6779 win
->g
= win
->g_float
;
6780 win
->g
.x
+= X(win
->ws
->r
);
6781 win
->g
.y
+= Y(win
->ws
->r
);
6792 configurenotify(xcb_configure_notify_event_t
*e
)
6796 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
6799 win
= find_window(e
->window
);
6801 xcb_icccm_get_wm_normal_hints_reply(conn
,
6802 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6811 destroynotify(xcb_destroy_notify_event_t
*e
)
6815 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
6817 if ((win
= find_window(e
->window
)) == NULL
) {
6818 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6824 /* make sure we focus on something */
6827 unmanage_window(win
);
6833 enternotify(xcb_enter_notify_event_t
*e
)
6838 Window focus_return
;
6839 int revert_to_return
;
6841 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %d, detail: "
6842 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6843 "state: %d\n", e
->event
, e
->mode
, e
->detail
, e
->root
,
6844 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
6846 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
6847 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6852 switch (focus_mode
) {
6853 case SWM_FOCUS_DEFAULT
:
6855 case SWM_FOCUS_FOLLOW
:
6857 case SWM_FOCUS_SYNERGY
:
6860 * all these checks need to be in this order because the
6861 * XCheckTypedWindowEvent relies on weeding out the previous events
6863 * making this code an option would enable a follow mouse for focus
6868 * state is set when we are switching workspaces and focus is set when
6869 * the window or a subwindow already has focus (occurs during restart).
6871 * Only honor the focus flag if last_focus_event is not FocusOut,
6872 * this allows spectrwm to continue to control focus when another
6873 * program is also playing with it.
6875 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6876 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6881 * happens when a window is created or destroyed and the border
6882 * crosses the mouse pointer and when switching ws
6884 * we need the subwindow test to see if we came from root in order
6885 * to give focus to floaters
6887 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6888 ev
->subwindow
== 0) {
6889 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6893 /* this window already has focus */
6894 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6895 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6899 /* this window is being deleted or moved to another ws */
6900 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6902 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6903 XPutBackEvent(display
, &cne
);
6907 if ((win
= find_window(ev
->window
)) == NULL
) {
6908 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6913 * In fullstack kill all enters unless they come from a different ws
6914 * (i.e. another region) or focus has been grabbed externally.
6916 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6917 last_focus_event
!= FocusOut
) {
6918 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6919 if ((w
= find_window(focus_return
)) == NULL
||
6921 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6929 if ((win
= find_window(e
->event
)) == NULL
) {
6930 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6937 /* lets us use one switch statement for arbitrary mode/detail combinations */
6938 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6941 mapnotify(xcb_map_notify_event_t
*e
)
6945 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
6947 win
= manage_window(e
->window
);
6949 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
6952 * focus_win can only set input focus on a mapped window.
6953 * make sure the window really has focus since it is just being mapped.
6955 if (win
->ws
->focus
== win
)
6960 mappingnotify(xcb_mapping_notify_event_t
*e
)
6962 xcb_refresh_keyboard_mapping(syms
, e
);
6964 if (e
->request
== XCB_MAPPING_KEYBOARD
)
6969 maprequest(xcb_map_request_event_t
*e
)
6972 struct swm_region
*r
;
6973 xcb_get_window_attributes_reply_t
*war
;
6975 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
6978 war
= xcb_get_window_attributes_reply(conn
,
6979 xcb_get_window_attributes(conn
, e
->window
),
6983 if (war
->override_redirect
) {
6989 win
= manage_window(e
->window
);
6991 return; /* can't happen */
6996 /* make new win focused */
6997 r
= root_to_region(win
->wa
->root
);
6998 if (win
->ws
== r
->ws
)
7003 propertynotify(xcb_property_notify_event_t
*e
)
7009 xcb_get_atom_name_reply_t
*r
;
7011 r
= xcb_get_atom_name_reply(conn
,
7012 xcb_get_atom_name(conn
, e
->atom
),
7015 len
= xcb_get_atom_name_name_length(r
);
7017 name
= malloc(len
+ 1);
7019 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7022 DNPRINTF(SWM_D_EVENT
,
7023 "propertynotify: window: 0x%x, atom: %s\n",
7032 win
= find_window(e
->window
);
7036 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7037 update_iconic(win
, 0);
7038 map_window_raised(win
->id
);
7046 case XCB_ATOM_WM_NORMAL_HINTS
:
7047 xcb_icccm_get_wm_normal_hints(conn
,
7048 xcb_iccom_get_wm_normal_hints(conn
, win
->id
),
7050 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
7051 if (win
->sh
.flags
& XCB_SIZE_HINT_P_MIN_SIZE
) {
7052 WIDTH(win
) = win
->sh
.min_width
;
7053 HEIGHT(win
) = win
->sh
.min_height
;
7054 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
7056 XMoveResizeWindow(display
, win
->id
,
7057 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
7059 case XCB_ATOM_WM_CLASS
:
7060 case XCB_ATOM_WM_NAME
:
7069 unmapnotify(xcb_unmap_notify_event_t
*e
)
7073 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7075 /* determine if we need to help unmanage this window */
7076 win
= find_window(e
->window
);
7080 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7081 unmanage_window(win
);
7084 /* resend unmap because we ated it */
7085 xcb_unmap_window(conn
, e
->window
);
7091 visibilitynotify(xcb_visibility_notify_event_t *e)
7093 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7098 clientmessage(xcb_client_message_event_t
*e
)
7102 win
= find_window(e
->window
);
7107 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, type: %u\n",
7108 e
->window
, e
->type
);
7110 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7111 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7114 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7115 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7116 if (win
->can_delete
)
7117 client_msg(win
, adelete
);
7119 xcb_kill_client(conn
, win
->id
);
7121 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7122 DNPRINTF(SWM_D_EVENT
,
7123 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7124 if (win
->floating
) {
7125 if (e
->data
.data32
[0] & (1<<8)) /* x */
7126 X(win
) = e
->data
.data32
[1];
7127 if (e
->data
.data32
[0] & (1<<9)) /* y */
7128 Y(win
) = e
->data
.data32
[2];
7129 if (e
->data
.data32
[0] & (1<<10)) /* width */
7130 WIDTH(win
) = e
->data
.data32
[3];
7131 if (e
->data
.data32
[0] & (1<<11)) /* height */
7132 HEIGHT(win
) = e
->data
.data32
[4];
7137 /* TODO: Change stack sizes */
7138 /* notify no change was made. */
7139 config_win(win
, NULL
);
7142 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7143 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7144 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7145 if (e
->data
.data32
[2])
7146 ewmh_update_win_state(win
, e
->data
.data32
[2],
7154 xerror_start(Display
*d
, XErrorEvent
*ee
)
7161 xerror(Display
*d
, XErrorEvent
*ee
)
7163 /* warnx("error: %p %p", display, ee); */
7171 xerrorxlib
= XSetErrorHandler(xerror_start
);
7173 /* this causes an error if some other window manager is running */
7174 XSelectInput(display
, DefaultRootWindow(display
),
7175 SubstructureRedirectMask
);
7180 XSetErrorHandler(xerror
);
7186 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7188 struct swm_region
*r
, *n
;
7189 struct workspace
*ws
= NULL
;
7192 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7193 s
->idx
, w
, h
, x
, y
);
7195 /* remove any conflicting regions */
7196 n
= TAILQ_FIRST(&s
->rl
);
7199 n
= TAILQ_NEXT(r
, entry
);
7200 if (X(r
) < (x
+ w
) &&
7201 (X(r
) + WIDTH(r
)) > x
&&
7203 (Y(r
) + HEIGHT(r
)) > y
) {
7204 if (r
->ws
->r
!= NULL
)
7205 r
->ws
->old_r
= r
->ws
->r
;
7208 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7209 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7213 /* search old regions for one to reuse */
7215 /* size + location match */
7216 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7217 if (X(r
) == x
&& Y(r
) == y
&&
7218 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7222 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7223 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7227 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7228 /* try to use old region's workspace */
7229 if (r
->ws
->r
== NULL
)
7232 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7233 err(1, "new_region: calloc: failed to allocate memory "
7236 /* if we don't have a workspace already, find one */
7238 for (i
= 0; i
< workspace_limit
; i
++)
7239 if (s
->ws
[i
].r
== NULL
) {
7246 errx(1, "new_region: no free workspaces");
7257 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7263 #ifdef SWM_XRR_HAS_CRTC
7266 #endif /* SWM_XRR_HAS_CRTC */
7267 struct swm_region
*r
;
7269 xcb_randr_get_screen_resources_current_cookie_t src
;
7270 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7271 xcb_randr_get_crtc_info_cookie_t cic
;
7272 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7273 xcb_randr_crtc_t
*crtc
;
7274 xcb_screen_t
*screen
= get_screen(i
);
7276 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7277 if (i
>= num_screens
)
7278 errx(1, "scan_xrandr: invalid screen");
7280 /* remove any old regions */
7281 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7282 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7284 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7285 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7289 /* map virtual screens onto physical screens */
7290 #ifdef SWM_XRR_HAS_CRTC
7291 if (xrandr_support
) {
7292 src
= xcb_randr_get_screen_resources_current(conn
,
7294 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7297 new_region(&screens
[i
], 0, 0,
7298 screen
->width_in_pixels
,
7299 screen
->height_in_pixels
);
7302 ncrtc
= srr
->num_crtcs
;
7303 for (c
= 0; c
< ncrtc
; c
++) {
7304 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7305 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7307 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7310 if (cir
->num_outputs
== 0) {
7316 new_region(&screens
[i
], 0, 0,
7317 screen
->width_in_pixels
,
7318 screen
->height_in_pixels
);
7320 new_region(&screens
[i
],
7321 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7326 #endif /* SWM_XRR_HAS_CRTC */
7328 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7329 screen
->height_in_pixels
);
7334 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7336 struct swm_region
*r
;
7339 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7341 if (e
->rotation
& (XCB_RANDR_ROTATION_ROTATE_90
7342 | XCB_RANDR_ROTATION_ROTATE_270
))
7343 xcb_randr_set_screen_size(conn
, e
->root
, e
->height
,
7344 e
->width
, e
->mheight
, e
->mwidth
);
7346 xcb_randr_set_screen_size(conn
, e
->root
, e
->width
,
7347 e
->height
, e
->mwidth
, e
->mheight
);
7349 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7350 /* silly event doesn't include the screen index */
7351 for (i
= 0; i
< num_screens
; i
++)
7352 if (screens
[i
].root
== e
->root
)
7354 if (i
>= num_screens
)
7355 errx(1, "screenchange: screen not found");
7357 /* brute force for now, just re-enumerate the regions */
7360 /* add bars to all regions */
7361 for (i
= 0; i
< num_screens
; i
++)
7362 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7370 xcb_window_t
*wins
= NULL
;
7372 int i
, j
, num_screens
;
7373 uint16_t state
, manage
;
7375 xcb_query_tree_cookie_t qtc
;
7376 xcb_query_tree_reply_t
*qtr
;
7377 xcb_get_window_attributes_cookie_t c
;
7378 xcb_get_window_attributes_reply_t
*r
;
7379 xcb_get_property_cookie_t pc
;
7381 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7382 for (i
= 0; i
< num_screens
; i
++) {
7383 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7384 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7387 wins
= xcb_query_tree_children(qtr
);
7388 no
= xcb_query_tree_children_length(qtr
);
7389 /* attach windows to a region */
7390 /* normal windows */
7391 for (j
= 0; j
< no
; j
++) {
7392 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7393 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7396 if (r
->override_redirect
) {
7401 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7402 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7408 state
= getstate(wins
[j
]);
7409 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7410 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7411 manage_window(wins
[j
]);
7414 /* transient windows */
7415 for (j
= 0; j
< no
; j
++) {
7416 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7417 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7420 if (r
->override_redirect
) {
7426 state
= getstate(wins
[j
]);
7427 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7428 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7429 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7430 &wins
[j
], NULL
) && manage
)
7431 manage_window(wins
[j
]);
7440 int i
, j
, k
, num_screens
;
7441 struct workspace
*ws
;
7442 uint32_t gcv
[1], wa
[1];
7443 const xcb_query_extension_reply_t
*qep
;
7444 xcb_cursor_t cursor
;
7445 xcb_font_t cursor_font
;
7446 xcb_randr_query_version_cookie_t c
;
7447 xcb_randr_query_version_reply_t
*r
;
7449 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7450 if ((screens
= calloc(num_screens
,
7451 sizeof(struct swm_screen
))) == NULL
)
7452 err(1, "setup_screens: calloc: failed to allocate memory for "
7455 /* initial Xrandr setup */
7456 xrandr_support
= False
;
7457 c
= xcb_randr_query_version(conn
, True
, True
);
7458 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7460 if (r
->major_version
>= 1)
7461 xrandr_support
= True
;
7464 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7465 xrandr_eventbase
= qep
->first_event
;
7467 cursor_font
= xcb_generate_id(conn
);
7468 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7470 cursor
= xcb_generate_id(conn
);
7471 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7472 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7475 /* map physical screens */
7476 for (i
= 0; i
< num_screens
; i
++) {
7477 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7479 TAILQ_INIT(&screens
[i
].rl
);
7480 TAILQ_INIT(&screens
[i
].orl
);
7481 screens
[i
].root
= RootWindow(display
, i
);
7483 /* set default colors */
7484 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7485 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7486 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7487 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7488 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7490 /* create graphics context on screen */
7491 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7493 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7494 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7496 /* set default cursor */
7497 xcb_change_window_attributes(conn
, screens
[i
].root
,
7500 /* init all workspaces */
7501 /* XXX these should be dynamically allocated too */
7502 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7503 ws
= &screens
[i
].ws
[j
];
7509 TAILQ_INIT(&ws
->winlist
);
7510 TAILQ_INIT(&ws
->unmanagedlist
);
7512 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7513 if (layouts
[k
].l_config
!= NULL
)
7514 layouts
[k
].l_config(ws
,
7515 SWM_ARG_ID_STACKINIT
);
7516 ws
->cur_layout
= &layouts
[0];
7517 ws
->cur_layout
->l_string(ws
);
7523 xcb_randr_select_input(conn
, screens
[i
].root
,
7524 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7526 xcb_free_cursor(conn
, cursor
);
7527 xcb_close_font(conn
, cursor_font
);
7533 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7534 err(1, "setup_globals: strdup: failed to allocate memory.");
7536 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7537 err(1, "setup_globals: strdup: failed to allocate memory.");
7544 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7545 xcb_window_t root
, win
;
7548 /* work around sun jdk bugs, code from wmname */
7549 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7550 netwmname
= get_atom_from_string("_NET_WM_NAME");
7551 utf8_string
= get_atom_from_string("UTF8_STRING");
7553 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7554 for (i
= 0; i
< num_screens
; i
++) {
7555 root
= screens
[i
].root
;
7557 win
= xcb_generate_id(conn
);
7558 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7559 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7560 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7561 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7562 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7564 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7565 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7566 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7567 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7568 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7569 netwmname
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7574 event_error(xcb_generic_error_t
*e
)
7576 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7577 "resource_id: %u, minor_code: %u\n",
7578 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7579 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7580 e
->sequence
, e
->resource_id
, e
->minor_code
);
7584 event_handle(xcb_generic_event_t
*evt
)
7586 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7588 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7589 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7590 XCB_EVENT_RESPONSE_TYPE(evt
));
7593 #define EVENT(type, callback) case type: callback((void *)evt); return
7594 EVENT(0, event_error
);
7595 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7596 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7597 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7598 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7599 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7600 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7601 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7602 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7603 /*EVENT(XCB_CREATE_NOTIFY, );*/
7604 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7605 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7606 EVENT(XCB_EXPOSE
, expose
);
7607 /*EVENT(XCB_FOCUS_IN, );*/
7608 /*EVENT(XCB_FOCUS_OUT, );*/
7609 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7610 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7611 EVENT(XCB_KEY_PRESS
, keypress
);
7612 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7613 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7614 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7615 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7616 EVENT(XCB_MAP_REQUEST
, maprequest
);
7617 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7618 /*EVENT(XCB_MOTION_NOTIFY, );*/
7619 /*EVENT(XCB_NO_EXPOSURE, );*/
7620 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7621 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7622 /*EVENT(XCB_RESIZE_REQUEST, );*/
7623 /*EVENT(XCB_SELECTION_CLEAR, );*/
7624 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7625 /*EVENT(XCB_SELECTION_REQUEST, );*/
7626 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7627 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7630 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7631 screenchange((void *)evt
);
7635 main(int argc
, char *argv
[])
7637 struct swm_region
*r
, *rr
;
7638 struct ws_win
*winfocus
= NULL
;
7640 char conf
[PATH_MAX
], *cfile
= NULL
;
7642 int xfd
, i
, num_screens
;
7643 struct sigaction sact
;
7644 xcb_generic_event_t
*evt
;
7650 time_started
= time(NULL
);
7653 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7654 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7656 warnx("no locale support");
7658 if (!X_HAVE_UTF8_STRING
)
7659 warnx("no UTF-8 support");
7661 if (!(display
= XOpenDisplay(0)))
7662 errx(1, "can not open display");
7664 /* handle some signals */
7665 bzero(&sact
, sizeof(sact
));
7666 sigemptyset(&sact
.sa_mask
);
7668 sact
.sa_handler
= sighdlr
;
7669 sigaction(SIGINT
, &sact
, NULL
);
7670 sigaction(SIGQUIT
, &sact
, NULL
);
7671 sigaction(SIGTERM
, &sact
, NULL
);
7672 sigaction(SIGHUP
, &sact
, NULL
);
7674 sact
.sa_handler
= sighdlr
;
7675 sact
.sa_flags
= SA_NOCLDSTOP
;
7676 sigaction(SIGCHLD
, &sact
, NULL
);
7678 conn
= XGetXCBConnection(display
);
7679 if (xcb_connection_has_error(conn
))
7680 errx(1, "can not get XCB connection");
7682 xfd
= xcb_get_file_descriptor(conn
);
7683 syms
= xcb_key_symbols_alloc(conn
);
7685 errx(1, "unable to allocate key symbols");
7687 astate
= get_atom_from_string("WM_STATE");
7688 aprot
= get_atom_from_string("WM_PROTOCOLS");
7689 adelete
= get_atom_from_string("WM_DELETE_WINDOW");
7690 takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7691 a_wmname
= get_atom_from_string("WM_NAME");
7692 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7693 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7694 a_string
= get_atom_from_string("STRING");
7695 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7697 /* look for local and global conf file */
7698 pwd
= getpwuid(getuid());
7700 errx(1, "invalid user: %d", getuid());
7702 xcb_grab_server(conn
);
7705 /* flush all events */
7706 while ((evt
= xcb_poll_for_event(conn
))) {
7707 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
7713 errx(1, "other wm running");
7724 for (i
= 0; ; i
++) {
7729 snprintf(conf
, sizeof conf
, "%s/.%s",
7730 pwd
->pw_dir
, SWM_CONF_FILE
);
7734 snprintf(conf
, sizeof conf
, "/etc/%s",
7739 snprintf(conf
, sizeof conf
, "%s/.%s",
7740 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7744 snprintf(conf
, sizeof conf
, "/etc/%s",
7751 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7752 if (S_ISREG(sb
.st_mode
)) {
7759 /* load conf (if any) */
7761 conf_load(cfile
, SWM_CONF_DEFAULT
);
7764 /* set some values to work around bad programs */
7766 /* grab existing windows (before we build the bars) */
7769 if (getenv("SWM_STARTED") == NULL
)
7770 setenv("SWM_STARTED", "YES", 1);
7772 /* setup all bars */
7773 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7774 for (i
= 0; i
< num_screens
; i
++)
7775 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7776 if (winfocus
== NULL
)
7777 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7786 xcb_ungrab_server(conn
);
7790 while ((evt
= xcb_poll_for_event(conn
))) {
7797 /* if we are being restarted go focus on first window */
7799 rr
= winfocus
->ws
->r
;
7801 /* not a visible window */
7805 /* move pointer to first screen if multi screen */
7806 if (num_screens
> 1 || outputs
> 1)
7807 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7808 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7809 Y(rr
) + (bar_enabled
? bar_height
: 0));
7811 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7822 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7824 DNPRINTF(SWM_D_MISC
, "select failed");
7826 if (restart_wm
== 1)
7827 restart(NULL
, NULL
);
7828 if (search_resp
== 1)
7841 for (i
= 0; i
< num_screens
; ++i
)
7842 if (screens
[i
].bar_gc
!= 0)
7843 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7844 XFreeFontSet(display
, bar_fs
);
7846 xcb_key_symbols_free(syms
);
7847 xcb_disconnect(conn
);