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
);
727 get_atom_from_string(const char *str
)
729 xcb_intern_atom_cookie_t c
;
730 xcb_intern_atom_reply_t
*r
;
733 c
= xcb_intern_atom(conn
, False
, strlen(str
), str
);
734 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
742 return (XCB_ATOM_NONE
);
746 update_iconic(struct ws_win
*win
, int newv
)
753 iprop
= get_atom_from_string("_SWM_ICONIC");
754 if (iprop
== XCB_ATOM_NONE
)
758 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
759 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
761 xcb_delete_property(conn
, win
->id
, iprop
);
765 get_iconic(struct ws_win
*win
)
769 xcb_get_property_reply_t
*pr
= NULL
;
771 iprop
= get_atom_from_string("_SWM_ICONIC");
772 if (iprop
== XCB_ATOM_NONE
)
775 pr
= xcb_get_property_reply(conn
,
776 xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
780 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
782 v
= *((int32_t *)xcb_get_property_value(pr
));
793 int i
, j
, num_screens
;
795 sup_list
= get_atom_from_string("_NET_SUPPORTED");
797 for (i
= 0; i
< LENGTH(ewmh
); i
++)
798 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
800 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
801 for (i
= 0; i
< num_screens
; i
++) {
802 /* Support check window will be created by workaround(). */
804 /* Report supported atoms */
805 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
806 for (j
= 0; j
< LENGTH(ewmh
); j
++)
807 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
808 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
817 xcb_atom_t sup_check
, sup_list
;
819 xcb_get_property_cookie_t pc
;
820 xcb_get_property_reply_t
*pr
;
822 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
823 sup_list
= get_atom_from_string("_NET_SUPPORTED");
824 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
826 for (i
= 0; i
< num_screens
; i
++) {
827 /* Get the support check window and destroy it */
828 pc
= xcb_get_property(conn
, False
, screens
[i
].root
, sup_check
,
829 XCB_ATOM_WINDOW
, 0, 1);
830 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
832 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
834 xcb_destroy_window(conn
, id
);
835 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
836 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
844 ewmh_autoquirk(struct ws_win
*win
)
850 xcb_get_property_cookie_t c
;
851 xcb_get_property_reply_t
*r
;
853 c
= xcb_get_property(conn
, False
, win
->id
,
854 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, (~0L));
855 r
= xcb_get_property_reply(conn
, c
, NULL
);
858 n
= xcb_get_property_value_length(r
);
860 for (i
= 0; i
< n
; i
++) {
861 type
= *((xcb_atom_t
*)xcb_get_property_value(r
));
862 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
864 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
865 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
866 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
868 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
871 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
872 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
874 win
->quirks
= SWM_Q_FLOAT
;
881 #define SWM_EWMH_ACTION_COUNT_MAX (6)
882 #define EWMH_F_FULLSCREEN (1<<0)
883 #define EWMH_F_ABOVE (1<<1)
884 #define EWMH_F_HIDDEN (1<<2)
885 #define EWMH_F_SKIP_PAGER (1<<3)
886 #define EWMH_F_SKIP_TASKBAR (1<<4)
887 #define SWM_F_MANUAL (1<<5)
890 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
898 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
899 "fullscreen %s\n", win
->id
, YESNO(fs
));
902 if (!win
->g_floatvalid
)
903 store_float_geom(win
, win
->ws
->r
);
905 win
->g
= win
->ws
->r
->g
;
908 if (win
->g_floatvalid
) {
909 /* refloat at last floating relative position */
910 win
->g
= win
->g_float
;
911 X(win
) += X(win
->ws
->r
);
912 Y(win
) += Y(win
->ws
->r
);
920 ewmh_update_actions(struct ws_win
*win
)
922 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
928 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
931 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
932 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
935 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
936 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
939 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
940 #define _NET_WM_STATE_ADD 1 /* add/set property */
941 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
944 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
946 unsigned int mask
= 0;
947 unsigned int changed
= 0;
948 unsigned int orig_flags
;
953 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
954 mask
= EWMH_F_FULLSCREEN
;
955 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
957 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
959 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
960 mask
= EWMH_F_SKIP_PAGER
;
961 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
962 mask
= EWMH_F_SKIP_TASKBAR
;
965 orig_flags
= win
->ewmh_flags
;
968 case _NET_WM_STATE_REMOVE
:
969 win
->ewmh_flags
&= ~mask
;
971 case _NET_WM_STATE_ADD
:
972 win
->ewmh_flags
|= mask
;
974 case _NET_WM_STATE_TOGGLE
:
975 win
->ewmh_flags
^= mask
;
979 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
981 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
983 if (!floating_toggle_win(win
))
984 win
->ewmh_flags
= orig_flags
; /* revert */
985 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
987 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
988 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
990 if (!ewmh_set_win_fullscreen(win
,
991 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
992 win
->ewmh_flags
= orig_flags
; /* revert */
994 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
996 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
997 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
998 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
999 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1000 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1001 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1002 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1003 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1004 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1005 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1006 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1007 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1008 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1009 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1010 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1011 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1012 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1013 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1014 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1015 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1019 ewmh_get_win_state(struct ws_win
*win
)
1022 xcb_get_property_cookie_t c
;
1023 xcb_get_property_reply_t
*r
;
1029 win
->ewmh_flags
= 0;
1031 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1033 win
->ewmh_flags
|= SWM_F_MANUAL
;
1035 c
= xcb_get_property(conn
, False
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1036 XCB_ATOM_ATOM
, 0, (~0L));
1037 r
= xcb_get_property_reply(conn
, c
, NULL
);
1041 states
= xcb_get_property_value(r
);
1042 n
= xcb_get_property_value_length(r
);
1044 for (i
= 0; i
< n
; i
++)
1045 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1053 dumpwins(struct swm_region
*r
, union arg
*args
)
1057 xcb_get_window_attributes_cookie_t c
;
1058 xcb_get_window_attributes_reply_t
*wa
;
1060 if (r
->ws
== NULL
) {
1061 warnx("dumpwins: invalid workspace");
1065 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1066 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1067 state
= getstate(win
->id
);
1068 c
= xcb_get_window_attributes(conn
, win
->id
);
1069 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1071 warnx("window: 0x%x, map_state: %d, state: %u, "
1072 "transient: 0x%x", win
->id
, wa
->map_state
,
1073 state
, win
->transient
);
1076 warnx("window: 0x%x, failed xcb_get_window_attributes",
1080 warnx("===== unmanaged window list =====");
1081 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1082 state
= getstate(win
->id
);
1083 c
= xcb_get_window_attributes(conn
, win
->id
);
1084 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1086 warnx("window: 0x%x, map_state: %d, state: %u, "
1087 "transient: 0x%x", win
->id
, wa
->map_state
,
1088 state
, win
->transient
);
1091 warnx("window: 0x%x, failed xcb_get_window_attributes",
1095 warnx("=================================");
1099 dumpwins(struct swm_region
*r
, union arg
*args
)
1102 #endif /* SWM_DEBUG */
1104 void event_error(xcb_generic_error_t
*);
1105 void expose(xcb_expose_event_t
*);
1106 void keypress(xcb_key_press_event_t
*);
1107 void buttonpress(xcb_button_press_event_t
*);
1108 void configurerequest(xcb_configure_request_event_t
*);
1109 void configurenotify(xcb_configure_notify_event_t
*);
1110 void destroynotify(xcb_destroy_notify_event_t
*);
1111 void enternotify(xcb_enter_notify_event_t
*);
1112 void mapnotify(xcb_map_notify_event_t
*);
1113 void mappingnotify(xcb_mapping_notify_event_t
*);
1114 void maprequest(xcb_map_request_event_t
*);
1115 void propertynotify(xcb_property_notify_event_t
*);
1116 void unmapnotify(xcb_unmap_notify_event_t
*);
1117 void visibilitynotify(xcb_visibility_notify_event_t
*);
1118 void clientmessage(xcb_client_message_event_t
*);
1119 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1124 int saved_errno
, status
;
1127 saved_errno
= errno
;
1131 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1136 if (errno
!= ECHILD
)
1137 warn("sighdlr: waitpid");
1138 #endif /* SWM_DEBUG */
1141 if (pid
== searchpid
)
1145 if (WIFEXITED(status
)) {
1146 if (WEXITSTATUS(status
) != 0)
1147 warnx("sighdlr: child exit status: %d",
1148 WEXITSTATUS(status
));
1150 warnx("sighdlr: child is terminated "
1152 #endif /* SWM_DEBUG */
1166 errno
= saved_errno
;
1172 struct pid_e
*p
= NULL
;
1174 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1179 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1188 name_to_color(const char *colorname
)
1190 uint32_t result
= 0;
1191 char cname
[32] = "#";
1192 xcb_screen_t
*screen
;
1193 xcb_colormap_t cmap
;
1194 xcb_alloc_color_reply_t
*cr
;
1195 xcb_alloc_named_color_reply_t
*nr
;
1196 uint16_t rr
, gg
, bb
;
1198 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1199 cmap
= screen
->default_colormap
;
1201 /* color is in format rgb://rr/gg/bb */
1202 if (strncmp(colorname
, "rgb:", 4) == 0) {
1203 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1204 warnx("could not parse rgb %s", colorname
);
1206 cr
= xcb_alloc_color_reply(conn
,
1207 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1213 warnx("color '%s' not found", colorname
);
1216 nr
= xcb_alloc_named_color_reply(conn
,
1217 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1220 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1221 nr
= xcb_alloc_named_color_reply(conn
,
1222 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1229 warnx("color '%s' not found", colorname
);
1236 setscreencolor(char *val
, int i
, int c
)
1240 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1241 if (i
> 0 && i
<= num_screens
) {
1242 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1243 free(screens
[i
- 1].c
[c
].name
);
1244 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1246 } else if (i
== -1) {
1247 for (i
= 0; i
< num_screens
; i
++) {
1248 screens
[i
].c
[c
].color
= name_to_color(val
);
1249 free(screens
[i
].c
[c
].name
);
1250 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1254 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1259 fancy_stacker(struct workspace
*ws
)
1261 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1262 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1263 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1264 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1265 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1266 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1267 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1268 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1269 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1273 plain_stacker(struct workspace
*ws
)
1275 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1276 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1277 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1278 sizeof ws
->stacker
);
1279 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1280 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1281 sizeof ws
->stacker
);
1285 custom_region(char *val
)
1287 unsigned int sidx
, x
, y
, w
, h
;
1289 xcb_screen_t
*screen
;
1291 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1292 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1293 errx(1, "invalid custom region, "
1294 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1295 if (sidx
< 1 || sidx
> num_screens
)
1296 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1300 screen
= get_screen(sidx
);
1302 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1304 if (x
> screen
->width_in_pixels
||
1305 y
> screen
->height_in_pixels
||
1306 w
+ x
> screen
->width_in_pixels
||
1307 h
+ y
> screen
->height_in_pixels
) {
1308 warnx("ignoring region %ux%u+%u+%u - not within screen "
1309 "boundaries (%ux%u)", w
, h
, x
, y
,
1310 screen
->width_in_pixels
, screen
->height_in_pixels
);
1314 new_region(&screens
[sidx
], x
, y
, w
, h
);
1318 socket_setnonblock(int fd
)
1322 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1323 err(1, "fcntl F_GETFL");
1324 flags
|= O_NONBLOCK
;
1325 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1326 err(1, "fcntl F_SETFL");
1330 bar_print(struct swm_region
*r
, const char *s
)
1334 xcb_rectangle_t rect
;
1336 XRectangle ibox
, lbox
;
1339 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1341 switch (bar_justify
) {
1342 case SWM_BAR_JUSTIFY_LEFT
:
1345 case SWM_BAR_JUSTIFY_CENTER
:
1346 x
= (WIDTH(r
) - lbox
.width
) / 2;
1348 case SWM_BAR_JUSTIFY_RIGHT
:
1349 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1353 if (x
< SWM_BAR_OFFSET
)
1358 rect
.width
= WIDTH(r
->bar
);
1359 rect
.height
= HEIGHT(r
->bar
);
1361 /* clear back buffer */
1362 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1363 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1364 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1365 sizeof(rect
), &rect
);
1367 /* draw back buffer */
1368 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1369 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1370 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
;
1371 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1372 xcb_image_text_8(conn
, len
, r
->bar
->buffer
, r
->s
->bar_gc
, x
,
1373 (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1377 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1378 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1382 bar_extra_stop(void)
1386 bzero(bar_pipe
, sizeof bar_pipe
);
1389 kill(bar_pid
, SIGTERM
);
1392 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1397 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1399 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1401 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1402 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1406 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1408 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1410 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1411 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1415 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1417 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1420 bar_class_name(s
, sz
, r
);
1421 strlcat(s
, ":", sz
);
1422 bar_title_name(s
, sz
, r
);
1426 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1428 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1430 if (r
->ws
->focus
->floating
)
1431 strlcat(s
, "(f)", sz
);
1435 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1439 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1441 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1444 strlcat(s
, title
, sz
);
1448 int urgent
[SWM_WS_MAX
];
1450 bar_urgent(char *s
, size_t sz
)
1453 int i
, j
, num_screens
;
1455 xcb_get_property_cookie_t c
;
1456 xcb_icccm_wm_hints_t hints
;
1458 for (i
= 0; i
< workspace_limit
; i
++)
1461 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1462 for (i
= 0; i
< num_screens
; i
++)
1463 for (j
= 0; j
< workspace_limit
; j
++)
1464 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1465 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1466 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1469 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1473 for (i
= 0; i
< workspace_limit
; i
++) {
1475 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1477 snprintf(b
, sizeof b
, "- ");
1483 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1485 if (r
== NULL
|| r
->ws
== NULL
)
1487 if (r
->ws
->name
!= NULL
)
1488 strlcat(s
, r
->ws
->name
, sz
);
1491 /* build the default bar format according to the defined enabled options */
1493 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1495 /* if format provided, just copy the buffers */
1496 if (bar_format
!= NULL
) {
1497 strlcpy(fmtnew
, fmtexp
, sz
);
1501 /* reset the output buffer */
1504 strlcat(fmtnew
, "+N:+I ", sz
);
1506 strlcat(fmtnew
, "+S", sz
);
1507 strlcat(fmtnew
, " ", sz
);
1509 /* only show the workspace name if there's actually one */
1510 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1511 strlcat(fmtnew
, "<+D>", sz
);
1512 strlcat(fmtnew
, "+3<", sz
);
1514 if (clock_enabled
) {
1515 strlcat(fmtnew
, fmtexp
, sz
);
1516 strlcat(fmtnew
, "+4<", sz
);
1519 /* bar_urgent already adds the space before the last asterisk */
1521 strlcat(fmtnew
, "* +U*+4<", sz
);
1523 if (title_class_enabled
) {
1524 strlcat(fmtnew
, "+C", sz
);
1525 if (title_name_enabled
== 0)
1526 strlcat(fmtnew
, "+4<", sz
);
1529 /* checks needed by the colon and floating strlcat(3) calls below */
1530 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1531 if (title_name_enabled
) {
1532 if (title_class_enabled
)
1533 strlcat(fmtnew
, ":", sz
);
1534 strlcat(fmtnew
, "+T+4<", sz
);
1536 if (window_name_enabled
) {
1537 if (r
->ws
->focus
->floating
)
1538 strlcat(fmtnew
, "+F ", sz
);
1539 strlcat(fmtnew
, "+64W ", sz
);
1543 /* finally add the action script output and the version */
1544 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1548 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1550 /* special case; no limit given, pad one space, instead */
1551 if (*limit
== sz
- 1)
1553 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1556 /* replaces the bar format character sequences (like in tmux(1)) */
1558 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1562 char tmp
[SWM_BAR_MAX
];
1566 /* reset strlcat(3) buffer */
1569 /* get number, if any */
1572 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1573 limit
= sizeof tmp
- 1;
1574 if (limit
<= 0 || limit
>= sizeof tmp
)
1575 limit
= sizeof tmp
- 1;
1577 /* there is nothing to replace (ie EOL) */
1584 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1587 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1590 bar_class_name(tmp
, sizeof tmp
, r
);
1593 bar_workspace_name(tmp
, sizeof tmp
, r
);
1596 bar_window_float(tmp
, sizeof tmp
, r
);
1599 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1602 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1605 bar_class_title_name(tmp
, sizeof tmp
, r
);
1608 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1611 bar_title_name(tmp
, sizeof tmp
, r
);
1614 bar_urgent(tmp
, sizeof tmp
);
1617 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1620 bar_window_name(tmp
, sizeof tmp
, r
);
1623 /* unknown character sequence; copy as-is */
1624 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1632 while (limit
-- > 0) {
1633 if (*offrep
>= sz
- 1)
1635 fmtrep
[(*offrep
)++] = *ptr
++;
1643 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1648 while (*fmt
!= '\0') {
1650 /* skip ordinary characters */
1653 fmtrep
[off
++] = *fmt
++;
1657 /* character sequence found; replace it */
1658 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1667 bar_fmt_expand(char *fmtexp
, size_t sz
)
1674 /* start by grabbing the current time and date */
1676 localtime_r(&tmt
, &tm
);
1678 /* figure out what to expand */
1679 if (bar_format
!= NULL
)
1681 else if (bar_format
== NULL
&& clock_enabled
)
1683 /* if nothing to expand bail out */
1689 /* copy as-is, just in case the format shouldn't be expanded below */
1690 strlcpy(fmtexp
, fmt
, sz
);
1691 /* finally pass the string through strftime(3) */
1692 #ifndef SWM_DENY_CLOCK_FORMAT
1693 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1694 warnx("format too long");
1702 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1703 char fmtrep
[SWM_BAR_MAX
];
1705 struct swm_region
*r
;
1707 /* expand the format by first passing it through strftime(3) */
1708 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1710 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1711 for (i
= 0; i
< num_screens
; i
++) {
1712 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1715 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1716 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1717 bar_print(r
, fmtrep
);
1728 if (bar_enabled
== 0)
1730 if (bar_extra
&& bar_extra_running
) {
1731 /* ignore short reads; it'll correct itself */
1732 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1733 if (b
&& b
[len
- 1] == '\n') {
1735 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1737 if (b
== NULL
&& errno
!= EAGAIN
) {
1738 warn("bar_update: bar_extra failed");
1742 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1755 bar_toggle(struct swm_region
*r
, union arg
*args
)
1757 struct swm_region
*tmpr
;
1760 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1762 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1764 for (i
= 0; i
< num_screens
; i
++)
1765 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1767 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1769 for (i
= 0; i
< num_screens
; i
++)
1770 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1772 map_window_raised(tmpr
->bar
->id
);
1775 bar_enabled
= !bar_enabled
;
1778 /* must be after stack */
1785 struct swm_region
*r
;
1789 /* do this here because the conf file is in memory */
1790 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1791 /* launch external status app */
1792 bar_extra_running
= 1;
1793 if (pipe(bar_pipe
) == -1)
1794 err(1, "pipe error");
1795 socket_setnonblock(bar_pipe
[0]);
1796 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1797 if (dup2(bar_pipe
[0], 0) == -1)
1799 if (dup2(bar_pipe
[1], 1) == -1)
1801 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1802 err(1, "could not disable SIGPIPE");
1803 switch (bar_pid
= fork()) {
1805 err(1, "cannot fork");
1809 execvp(bar_argv
[0], bar_argv
);
1810 err(1, "%s external app failed", bar_argv
[0]);
1812 default: /* parent */
1818 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1819 for (i
= 0; i
< num_screens
; i
++)
1820 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1823 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1824 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1825 xcb_change_window_attributes(conn
, r
->bar
->id
,
1826 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1832 bar_setup(struct swm_region
*r
)
1834 char *default_string
;
1835 char **missing_charsets
;
1836 int num_missing_charsets
= 0;
1838 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1842 XFreeFontSet(display
, bar_fs
);
1846 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1847 err(1, "bar_setup: calloc: failed to allocate memory.");
1849 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1851 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1852 &num_missing_charsets
, &default_string
);
1854 if (num_missing_charsets
> 0) {
1855 warnx("Unable to load charset(s):");
1857 for (i
= 0; i
< num_missing_charsets
; ++i
)
1858 warnx("%s", missing_charsets
[i
]);
1860 XFreeStringList(missing_charsets
);
1862 if (strcmp(default_string
, ""))
1863 warnx("Glyphs from those sets will be replaced "
1864 "by '%s'.", default_string
);
1866 warnx("Glyphs from those sets won't be drawn.");
1870 errx(1, "Error creating font set structure.");
1872 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1874 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1875 2 * bar_border_width
;
1881 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1882 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1883 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1885 r
->bar
->id
= xcb_generate_id(conn
);
1886 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1887 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
;
1888 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1889 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1890 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1891 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1893 r
->bar
->buffer
= xcb_generate_id(conn
);
1894 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1895 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1897 xcb_randr_select_input(conn
, r
->bar
->id
,
1898 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1901 map_window_raised(r
->bar
->id
);
1903 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1904 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1907 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1908 err(1, "could not install bar_signal");
1913 bar_cleanup(struct swm_region
*r
)
1917 xcb_destroy_window(conn
, r
->bar
->id
);
1918 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1924 set_win_state(struct ws_win
*win
, uint16_t state
)
1926 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1928 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1933 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, astate
,
1934 astate
, 32, 2, data
);
1938 getstate(xcb_window_t w
)
1940 uint16_t result
= 0;
1941 xcb_get_property_cookie_t c
;
1942 xcb_get_property_reply_t
*r
;
1944 c
= xcb_get_property(conn
, False
, w
, astate
, astate
, 0L, 2L);
1945 r
= xcb_get_property_reply(conn
, c
, NULL
);
1948 result
= *((uint16_t *)xcb_get_property_value(r
));
1952 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
1958 version(struct swm_region
*r
, union arg
*args
)
1960 bar_version
= !bar_version
;
1962 snprintf(bar_vertext
, sizeof bar_vertext
,
1963 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1965 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1970 client_msg(struct ws_win
*win
, xcb_atom_t a
)
1972 xcb_client_message_event_t ev
;
1977 bzero(&ev
, sizeof ev
);
1978 ev
.response_type
= XCB_CLIENT_MESSAGE
;
1979 ev
.window
= win
->id
;
1982 ev
.data
.data32
[0] = a
;
1983 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
1985 xcb_send_event(conn
, False
, win
->id
,
1986 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
1989 /* synthetic response to a ConfigureRequest when not making a change */
1991 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
1993 xcb_configure_notify_event_t ce
;
1998 /* send notification of unchanged state. */
1999 bzero(&ce
, sizeof(ce
));
2000 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2003 ce
.width
= WIDTH(win
);
2004 ce
.height
= HEIGHT(win
);
2005 ce
.override_redirect
= False
;
2010 ce
.window
= win
->id
;
2011 ce
.border_width
= BORDER(win
);
2012 ce
.above_sibling
= XCB_WINDOW_NONE
;
2015 ce
.event
= ev
->window
;
2016 ce
.window
= ev
->window
;
2018 /* make response appear more WM_SIZE_HINTS-compliant */
2020 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2021 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2022 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2023 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2024 SH_INC_W(win
), SH_INC_H(win
));
2028 /* the hint may be set... to 0! */
2029 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2030 ce
.width
= SH_MIN_W(win
);
2031 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2032 ce
.height
= SH_MIN_H(win
);
2037 /* may also be advertized as 0 */
2038 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2039 ce
.width
= SH_MAX_W(win
);
2040 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2041 ce
.height
= SH_MAX_H(win
);
2044 /* resize increment. */
2046 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2047 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2049 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2050 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2054 /* adjust x and y for requested border_width. */
2055 ce
.x
+= BORDER(win
) - ev
->border_width
;
2056 ce
.y
+= BORDER(win
) - ev
->border_width
;
2057 ce
.border_width
= ev
->border_width
;
2058 ce
.above_sibling
= ev
->sibling
;
2061 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2062 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2063 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2065 xcb_send_event(conn
, False
, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2070 count_win(struct workspace
*ws
, int count_transient
)
2075 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2076 if (count_transient
== 0 && win
->floating
)
2078 if (count_transient
== 0 && win
->transient
)
2084 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2090 quit(struct swm_region
*r
, union arg
*args
)
2092 DNPRINTF(SWM_D_MISC
, "quit\n");
2097 unmap_window(struct ws_win
*win
)
2102 /* don't unmap again */
2103 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2106 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2108 xcb_unmap_window(conn
, win
->id
);
2109 xcb_change_window_attributes(conn
, win
->id
,
2110 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2117 int i
, j
, num_screens
;
2119 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2120 for (i
= 0; i
< num_screens
; i
++)
2121 for (j
= 0; j
< workspace_limit
; j
++)
2122 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2127 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2129 xcb_key_press_event_t event
;
2130 xcb_keycode_t
*keycode
;
2135 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2137 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2140 bzero(&event
, sizeof(event
));
2141 event
.event
= win
->id
;
2142 event
.root
= win
->s
->root
;
2143 event
.child
= XCB_WINDOW_NONE
;
2144 event
.time
= XCB_CURRENT_TIME
;
2145 event
.event_x
= X(win
);
2146 event
.event_y
= Y(win
);
2149 event
.same_screen
= True
;
2150 event
.detail
= *keycode
;
2151 event
.state
= modifiers
;
2153 event
.response_type
= XCB_KEY_PRESS
;
2154 xcb_send_event(conn
, True
, win
->id
,
2155 XCB_EVENT_MASK_KEY_PRESS
, (char *)&event
);
2157 event
.response_type
= XCB_KEY_RELEASE
;
2158 xcb_send_event(conn
, True
, win
->id
,
2159 XCB_EVENT_MASK_KEY_RELEASE
, (char *)&event
);
2163 restart(struct swm_region
*r
, union arg
*args
)
2165 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2167 /* disable alarm because the following code may not be interrupted */
2169 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2170 err(1, "can't disable alarm");
2176 xcb_key_symbols_free(syms
);
2178 xcb_disconnect(conn
);
2180 execvp(start_argv
[0], start_argv
);
2181 warn("execvp failed");
2186 root_to_region(xcb_window_t root
)
2188 struct swm_region
*r
= NULL
;
2190 xcb_query_pointer_reply_t
*qpr
;
2192 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2194 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2195 for (i
= 0; i
< num_screens
; i
++)
2196 if (screens
[i
].root
== root
)
2199 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2200 screens
[i
].root
), NULL
);
2203 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2204 qpr
->root_x
, qpr
->root_y
);
2205 /* choose a region based on pointer location */
2206 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2207 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2208 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2214 r
= TAILQ_FIRST(&screens
[i
].rl
);
2220 find_unmanaged_window(xcb_window_t id
)
2223 int i
, j
, num_screens
;
2225 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2226 for (i
= 0; i
< num_screens
; i
++)
2227 for (j
= 0; j
< workspace_limit
; j
++)
2228 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2236 find_window(xcb_window_t id
)
2239 int i
, j
, num_screens
;
2240 xcb_query_tree_cookie_t c
;
2241 xcb_query_tree_reply_t
*r
;
2243 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2244 for (i
= 0; i
< num_screens
; i
++)
2245 for (j
= 0; j
< workspace_limit
; j
++)
2246 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2250 c
= xcb_query_tree(conn
, id
);
2251 r
= xcb_query_tree_reply(conn
, c
, NULL
);
2255 /* if we were looking for the parent return that window instead */
2256 if (r
->parent
== 0 || r
->root
== r
->parent
)
2259 /* look for parent */
2260 for (i
= 0; i
< num_screens
; i
++)
2261 for (j
= 0; j
< workspace_limit
; j
++)
2262 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2263 if (r
->parent
== win
->id
) {
2273 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2278 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2281 close(xcb_get_file_descriptor(conn
));
2283 setenv("LD_PRELOAD", SWM_LIB
, 1);
2285 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2286 warn("spawn: asprintf SWM_WS");
2289 setenv("_SWM_WS", ret
, 1);
2293 if (asprintf(&ret
, "%d", getpid()) == -1) {
2294 warn("spawn: asprintf _SWM_PID");
2297 setenv("_SWM_PID", ret
, 1);
2301 if (setsid() == -1) {
2302 warn("spawn: setsid");
2308 * close stdin and stdout to prevent interaction between apps
2309 * and the baraction script
2310 * leave stderr open to record errors
2312 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2313 warn("spawn: open");
2316 dup2(fd
, STDIN_FILENO
);
2317 dup2(fd
, STDOUT_FILENO
);
2322 execvp(args
->argv
[0], args
->argv
);
2324 warn("spawn: execvp");
2329 kill_refs(struct ws_win
*win
)
2331 int i
, x
, num_screens
;
2332 struct swm_region
*r
;
2333 struct workspace
*ws
;
2338 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2339 for (i
= 0; i
< num_screens
; i
++)
2340 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2341 for (x
= 0; x
< workspace_limit
; x
++) {
2343 if (win
== ws
->focus
)
2345 if (win
== ws
->focus_prev
)
2346 ws
->focus_prev
= NULL
;
2351 validate_win(struct ws_win
*testwin
)
2354 struct workspace
*ws
;
2355 struct swm_region
*r
;
2356 int i
, x
, num_screens
;
2358 if (testwin
== NULL
)
2361 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2362 for (i
= 0; i
< num_screens
; i
++)
2363 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2364 for (x
= 0; x
< workspace_limit
; x
++) {
2366 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2374 validate_ws(struct workspace
*testws
)
2376 struct swm_region
*r
;
2377 struct workspace
*ws
;
2378 int i
, x
, num_screens
;
2380 /* validate all ws */
2381 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2382 for (i
= 0; i
< num_screens
; i
++)
2383 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2384 for (x
= 0; x
< workspace_limit
; x
++) {
2393 unfocus_win(struct ws_win
*win
)
2395 xcb_window_t none
= XCB_WINDOW_NONE
;
2397 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2401 if (win
->ws
== NULL
)
2404 if (validate_ws(win
->ws
))
2405 return; /* XXX this gets hit with thunderbird, needs fixing */
2407 if (win
->ws
->r
== NULL
)
2410 if (validate_win(win
)) {
2415 if (win
->ws
->focus
== win
) {
2416 win
->ws
->focus
= NULL
;
2417 win
->ws
->focus_prev
= win
;
2420 if (validate_win(win
->ws
->focus
)) {
2421 kill_refs(win
->ws
->focus
);
2422 win
->ws
->focus
= NULL
;
2424 if (validate_win(win
->ws
->focus_prev
)) {
2425 kill_refs(win
->ws
->focus_prev
);
2426 win
->ws
->focus_prev
= NULL
;
2429 grabbuttons(win
, 0);
2430 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2431 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2432 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2433 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2440 int i
, j
, num_screens
;
2442 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2444 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2445 for (i
= 0; i
< num_screens
; i
++)
2446 for (j
= 0; j
< workspace_limit
; j
++)
2447 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2452 focus_win(struct ws_win
*win
)
2454 struct ws_win
*cfw
= NULL
;
2455 xcb_get_input_focus_cookie_t c
;
2456 xcb_get_input_focus_reply_t
*r
;
2457 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2459 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2463 if (win
->ws
== NULL
)
2466 if (validate_ws(win
->ws
))
2467 return; /* XXX this gets hit with thunderbird, needs fixing */
2469 if (validate_win(win
)) {
2474 if (validate_win(win
)) {
2479 c
= xcb_get_input_focus(conn
);
2480 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
2482 cur_focus
= r
->focus
;
2485 if ((cfw
= find_window(cur_focus
)) != NULL
)
2488 /* use larger hammer since the window was killed somehow */
2489 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2490 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2491 xcb_change_window_attributes(conn, cfw->id,
2492 XCB_CW_BORDER_PIXEL,
2493 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2496 win
->ws
->focus
= win
;
2498 if (win
->ws
->r
!= NULL
) {
2500 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2501 win
->id
, XCB_CURRENT_TIME
);
2502 grabbuttons(win
, 1);
2503 xcb_change_window_attributes(conn
, win
->id
,
2504 XCB_CW_BORDER_PIXEL
,
2505 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2506 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2507 win
->ws
->always_raise
)
2508 map_window_raised(win
->id
);
2510 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2511 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2519 switchws(struct swm_region
*r
, union arg
*args
)
2521 int wsid
= args
->id
, unmap_old
= 0;
2522 struct swm_region
*this_r
, *other_r
;
2524 struct workspace
*new_ws
, *old_ws
;
2530 if (wsid
>= workspace_limit
)
2534 old_ws
= this_r
->ws
;
2535 new_ws
= &this_r
->s
->ws
[wsid
];
2537 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2538 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2540 if (new_ws
== NULL
|| old_ws
== NULL
)
2542 if (new_ws
== old_ws
)
2545 other_r
= new_ws
->r
;
2546 if (other_r
== NULL
) {
2547 /* the other workspace is hidden, hide this one */
2551 /* the other ws is visible in another region, exchange them */
2552 other_r
->ws_prior
= new_ws
;
2553 other_r
->ws
= old_ws
;
2554 old_ws
->r
= other_r
;
2556 this_r
->ws_prior
= old_ws
;
2557 this_r
->ws
= new_ws
;
2560 /* this is needed so that we can click on a window after a restart */
2564 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2565 focus(new_ws
->r
, &a
);
2567 /* unmap old windows */
2569 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2574 cyclews(struct swm_region
*r
, union arg
*args
)
2577 struct swm_screen
*s
= r
->s
;
2580 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2581 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2586 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2589 case SWM_ARG_ID_CYCLEWS_UP
:
2590 if (a
.id
< workspace_limit
- 1)
2595 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2598 case SWM_ARG_ID_CYCLEWS_DOWN
:
2602 a
.id
= workspace_limit
- 1;
2609 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2611 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2615 } while (a
.id
!= r
->ws
->idx
);
2619 priorws(struct swm_region
*r
, union arg
*args
)
2623 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2624 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2626 if (r
->ws_prior
== NULL
)
2629 a
.id
= r
->ws_prior
->idx
;
2634 cyclescr(struct swm_region
*r
, union arg
*args
)
2636 struct swm_region
*rr
= NULL
;
2638 int i
, x
, y
, num_screens
;
2640 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2641 /* do nothing if we don't have more than one screen */
2642 if (!(num_screens
> 1 || outputs
> 1))
2647 case SWM_ARG_ID_CYCLESC_UP
:
2648 rr
= TAILQ_NEXT(r
, entry
);
2650 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2652 case SWM_ARG_ID_CYCLESC_DOWN
:
2653 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2655 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2663 /* move mouse to region */
2665 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2666 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2669 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2672 if (rr
->ws
->focus
) {
2673 /* move to focus window */
2674 x
= X(rr
->ws
->focus
) + 1;
2675 y
= Y(rr
->ws
->focus
) + 1;
2676 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2682 sort_windows(struct ws_win_list
*wl
)
2684 struct ws_win
*win
, *parent
, *nxt
;
2689 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2690 nxt
= TAILQ_NEXT(win
, entry
);
2691 if (win
->transient
) {
2692 parent
= find_window(win
->transient
);
2693 if (parent
== NULL
) {
2694 warnx("not possible bug");
2697 TAILQ_REMOVE(wl
, win
, entry
);
2698 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2705 swapwin(struct swm_region
*r
, union arg
*args
)
2707 struct ws_win
*target
, *source
;
2708 struct ws_win
*cur_focus
;
2709 struct ws_win_list
*wl
;
2712 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2713 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2715 cur_focus
= r
->ws
->focus
;
2716 if (cur_focus
== NULL
)
2720 wl
= &source
->ws
->winlist
;
2723 case SWM_ARG_ID_SWAPPREV
:
2724 if (source
->transient
)
2725 source
= find_window(source
->transient
);
2726 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2727 if (target
&& target
->transient
)
2728 target
= find_window(target
->transient
);
2729 TAILQ_REMOVE(wl
, source
, entry
);
2731 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2733 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2735 case SWM_ARG_ID_SWAPNEXT
:
2736 target
= TAILQ_NEXT(source
, entry
);
2737 /* move the parent and let the sort handle the move */
2738 if (source
->transient
)
2739 source
= find_window(source
->transient
);
2740 TAILQ_REMOVE(wl
, source
, entry
);
2742 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2744 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2746 case SWM_ARG_ID_SWAPMAIN
:
2747 target
= TAILQ_FIRST(wl
);
2748 if (target
== source
) {
2749 if (source
->ws
->focus_prev
!= NULL
&&
2750 source
->ws
->focus_prev
!= target
)
2751 source
= source
->ws
->focus_prev
;
2755 if (target
== NULL
|| source
== NULL
)
2757 source
->ws
->focus_prev
= target
;
2758 TAILQ_REMOVE(wl
, target
, entry
);
2759 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2760 TAILQ_REMOVE(wl
, source
, entry
);
2761 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2763 case SWM_ARG_ID_MOVELAST
:
2764 TAILQ_REMOVE(wl
, source
, entry
);
2765 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2768 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2778 focus_prev(struct ws_win
*win
)
2780 struct ws_win
*winfocus
= NULL
;
2781 struct ws_win
*cur_focus
= NULL
;
2782 struct ws_win_list
*wl
= NULL
;
2783 struct workspace
*ws
= NULL
;
2785 if (!(win
&& win
->ws
))
2790 cur_focus
= ws
->focus
;
2792 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2793 WINID(win
), WINID(cur_focus
));
2795 /* pickle, just focus on whatever */
2796 if (cur_focus
== NULL
) {
2797 /* use prev_focus if valid */
2798 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2799 find_window(WINID(ws
->focus_prev
)))
2800 winfocus
= ws
->focus_prev
;
2804 /* if transient focus on parent */
2805 if (cur_focus
->transient
) {
2806 winfocus
= find_window(cur_focus
->transient
);
2810 /* if in max_stack try harder */
2811 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2812 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2813 if (cur_focus
!= ws
->focus_prev
)
2814 winfocus
= ws
->focus_prev
;
2816 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2821 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2823 if (winfocus
== NULL
|| winfocus
== win
) {
2824 switch (focus_close
) {
2825 case SWM_STACK_BOTTOM
:
2826 winfocus
= TAILQ_FIRST(wl
);
2829 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2831 case SWM_STACK_ABOVE
:
2832 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2833 if (focus_close_wrap
)
2834 winfocus
= TAILQ_FIRST(wl
);
2836 winfocus
= TAILQ_PREV(cur_focus
,
2837 ws_win_list
, entry
);
2840 case SWM_STACK_BELOW
:
2841 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2843 if (focus_close_wrap
)
2844 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2846 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2852 if (winfocus
== NULL
) {
2853 if (focus_default
== SWM_STACK_TOP
)
2854 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2856 winfocus
= TAILQ_FIRST(wl
);
2860 focus_magic(winfocus
);
2864 focus(struct swm_region
*r
, union arg
*args
)
2866 struct ws_win
*winfocus
= NULL
, *head
;
2867 struct ws_win
*cur_focus
= NULL
;
2868 struct ws_win_list
*wl
= NULL
;
2869 struct workspace
*ws
= NULL
;
2875 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2877 /* treat FOCUS_CUR special */
2878 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2879 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2880 winfocus
= r
->ws
->focus
;
2881 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2882 winfocus
= r
->ws
->focus_prev
;
2884 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2885 if (winfocus
->iconic
== 0)
2888 focus_magic(winfocus
);
2892 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2896 if (TAILQ_EMPTY(wl
))
2898 /* make sure there is at least one uniconified window */
2900 TAILQ_FOREACH(winfocus
, wl
, entry
)
2901 if (winfocus
->iconic
== 0) {
2909 case SWM_ARG_ID_FOCUSPREV
:
2910 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2912 head
= TAILQ_LAST(wl
, ws_win_list
);
2914 if (WINID(winfocus
) == cur_focus
->transient
) {
2915 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2917 head
= TAILQ_LAST(wl
, ws_win_list
);
2922 if (winfocus
&& winfocus
->iconic
) {
2923 while (winfocus
!= cur_focus
) {
2924 if (winfocus
== NULL
)
2925 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2926 if (winfocus
->iconic
== 0)
2928 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2934 case SWM_ARG_ID_FOCUSNEXT
:
2935 head
= TAILQ_NEXT(cur_focus
, entry
);
2937 head
= TAILQ_FIRST(wl
);
2941 if (winfocus
&& winfocus
->iconic
) {
2942 while (winfocus
!= cur_focus
) {
2943 if (winfocus
== NULL
)
2944 winfocus
= TAILQ_FIRST(wl
);
2945 if (winfocus
->iconic
== 0)
2947 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2952 case SWM_ARG_ID_FOCUSMAIN
:
2953 winfocus
= TAILQ_FIRST(wl
);
2954 if (winfocus
== cur_focus
)
2955 winfocus
= cur_focus
->ws
->focus_prev
;
2962 focus_magic(winfocus
);
2966 cycle_layout(struct swm_region
*r
, union arg
*args
)
2968 struct workspace
*ws
= r
->ws
;
2971 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2974 if (ws
->cur_layout
->l_stack
== NULL
)
2975 ws
->cur_layout
= &layouts
[0];
2979 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2985 stack_config(struct swm_region
*r
, union arg
*args
)
2987 struct workspace
*ws
= r
->ws
;
2989 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2992 if (ws
->cur_layout
->l_config
!= NULL
)
2993 ws
->cur_layout
->l_config(ws
, args
->id
);
2995 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3002 struct swm_geometry g
;
3003 struct swm_region
*r
;
3009 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3011 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3012 for (i
= 0; i
< num_screens
; i
++) {
3016 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3017 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3018 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3020 /* start with screen geometry, adjust for bar */
3022 g
.w
-= 2 * border_width
;
3023 g
.h
-= 2 * border_width
;
3029 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3030 r
->ws
->cur_layout
->l_string(r
->ws
);
3031 /* save r so we can track region changes */
3038 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3042 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3044 /* retain window geom and region geom */
3045 win
->g_float
= win
->g
;
3046 win
->g_float
.x
-= X(r
);
3047 win
->g_float
.y
-= Y(r
);
3048 win
->g_floatvalid
= 1;
3049 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3050 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3051 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3052 win
->g_float
.w
, win
->g_float
.h
);
3056 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3061 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3064 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3065 * geom on ws switches or return from max mode
3067 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3068 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3069 /* refloat at last floating relative position */
3070 win
->g
= win
->g_float
;
3075 win
->floatmaxed
= 0;
3078 * if set to fullscreen mode, configure window to maximum size.
3080 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3081 if (!win
->g_floatvalid
)
3082 store_float_geom(win
, win
->ws
->r
);
3084 win
->g
= win
->ws
->r
->g
;
3088 * remove border on fullscreen floater when in fullscreen mode or when
3089 * the quirk is present.
3091 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3092 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3093 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3094 if (win
->bordered
) {
3096 X(win
) += border_width
;
3097 Y(win
) += border_width
;
3099 } else if (!win
->bordered
) {
3101 X(win
) -= border_width
;
3102 Y(win
) -= border_width
;
3105 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3106 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3107 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3112 * floaters and transients are auto-centred unless moved
3115 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3116 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3119 /* keep window within region bounds */
3120 constrain_window(win
, r
, 0);
3126 * Send keystrokes to terminal to decrease/increase the font size as the
3127 * window size changes.
3130 adjust_font(struct ws_win
*win
)
3132 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3133 win
->floating
|| win
->transient
)
3136 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3137 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3138 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3139 win
->font_size_boundary
[win
->font_steps
] =
3140 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3143 win
->last_inc
= win
->sh
.width_inc
;
3144 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3145 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3146 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3149 win
->last_inc
= win
->sh
.width_inc
;
3150 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3154 #define SWAPXY(g) do { \
3156 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3157 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3160 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3162 struct swm_geometry win_g
, r_g
= *g
;
3163 struct ws_win
*win
, *fs_win
= NULL
;
3164 int i
, j
, s
, stacks
;
3165 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3166 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3167 int split
, colno
, winno
, mwin
, msize
, mscale
;
3168 int remain
, missing
, v_slice
, reconfigure
;
3170 xcb_get_window_attributes_reply_t
*war
;
3172 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3173 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3175 winno
= count_win(ws
, 0);
3176 if (winno
== 0 && count_win(ws
, 1) == 0)
3179 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3180 if (win
->transient
== 0 && win
->floating
== 0
3181 && win
->iconic
== 0)
3188 w_inc
= win
->sh
.width_inc
;
3189 w_base
= win
->sh
.base_width
;
3190 mwin
= ws
->l_state
.horizontal_mwin
;
3191 mscale
= ws
->l_state
.horizontal_msize
;
3192 stacks
= ws
->l_state
.horizontal_stacks
;
3195 w_inc
= win
->sh
.height_inc
;
3196 w_base
= win
->sh
.base_height
;
3197 mwin
= ws
->l_state
.vertical_mwin
;
3198 mscale
= ws
->l_state
.vertical_msize
;
3199 stacks
= ws
->l_state
.vertical_stacks
;
3203 if (stacks
> winno
- mwin
)
3204 stacks
= winno
- mwin
;
3208 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3209 if (mwin
&& winno
> mwin
) {
3210 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3214 win_g
.w
= v_slice
* mscale
;
3216 if (w_inc
> 1 && w_inc
< v_slice
) {
3217 /* adjust for window's requested size increment */
3218 remain
= (win_g
.w
- w_base
) % w_inc
;
3224 win_g
.x
+= r_g
.w
- msize
;
3227 colno
= split
= winno
/ stacks
;
3228 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3229 2 * border_width
) / stacks
);
3231 hrh
= r_g
.h
/ colno
;
3232 extra
= r_g
.h
- (colno
* hrh
);
3233 win_g
.h
= hrh
- 2 * border_width
;
3235 /* stack all the tiled windows */
3236 i
= j
= 0, s
= stacks
;
3237 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3238 if (win
->transient
!= 0 || win
->floating
!= 0)
3240 if (win
->iconic
!= 0)
3243 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3248 if (split
&& i
== split
) {
3249 colno
= (winno
- mwin
) / stacks
;
3250 if (s
<= (winno
- mwin
) % stacks
)
3252 split
= split
+ colno
;
3253 hrh
= (r_g
.h
/ colno
);
3254 extra
= r_g
.h
- (colno
* hrh
);
3258 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3259 win_g
.w
= (r_g
.w
- msize
-
3260 (stacks
* 2 * border_width
)) / stacks
;
3262 win_g
.w
+= (r_g
.w
- msize
-
3263 (stacks
* 2 * border_width
)) % stacks
;
3267 win_g
.h
= hrh
- 2 * border_width
;
3269 h_inc
= win
->sh
.width_inc
;
3270 h_base
= win
->sh
.base_width
;
3272 h_inc
= win
->sh
.height_inc
;
3273 h_base
= win
->sh
.base_height
;
3275 if (j
== colno
- 1) {
3276 win_g
.h
= hrh
+ extra
;
3277 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3278 /* adjust for window's requested size increment */
3279 remain
= (win_g
.h
- h_base
) % h_inc
;
3280 missing
= h_inc
- remain
;
3282 if (missing
<= extra
|| j
== 0) {
3294 win_g
.y
+= last_h
+ 2 * border_width
;
3296 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3298 win_g
.w
+= 2 * border_width
;
3299 win_g
.h
+= 2 * border_width
;
3304 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3305 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3309 WIDTH(win
) = win_g
.h
;
3310 HEIGHT(win
) = win_g
.w
;
3313 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3314 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3318 WIDTH(win
) = win_g
.w
;
3319 HEIGHT(win
) = win_g
.h
;
3323 if (bordered
!= win
->bordered
) {
3325 win
->bordered
= bordered
;
3333 war
= xcb_get_window_attributes_reply(conn
,
3334 xcb_get_window_attributes(conn
, win
->id
),
3337 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3338 map_window_raised(win
->id
);
3348 /* now, stack all the floaters and transients */
3349 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3350 if (win
->transient
== 0 && win
->floating
== 0)
3352 if (win
->iconic
== 1)
3354 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3359 stack_floater(win
, ws
->r
);
3360 map_window_raised(win
->id
);
3364 stack_floater(fs_win
, ws
->r
);
3365 map_window_raised(fs_win
->id
);
3370 vertical_config(struct workspace
*ws
, int id
)
3372 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3376 case SWM_ARG_ID_STACKRESET
:
3377 case SWM_ARG_ID_STACKINIT
:
3378 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3379 ws
->l_state
.vertical_mwin
= 1;
3380 ws
->l_state
.vertical_stacks
= 1;
3382 case SWM_ARG_ID_MASTERSHRINK
:
3383 if (ws
->l_state
.vertical_msize
> 1)
3384 ws
->l_state
.vertical_msize
--;
3386 case SWM_ARG_ID_MASTERGROW
:
3387 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3388 ws
->l_state
.vertical_msize
++;
3390 case SWM_ARG_ID_MASTERADD
:
3391 ws
->l_state
.vertical_mwin
++;
3393 case SWM_ARG_ID_MASTERDEL
:
3394 if (ws
->l_state
.vertical_mwin
> 0)
3395 ws
->l_state
.vertical_mwin
--;
3397 case SWM_ARG_ID_STACKINC
:
3398 ws
->l_state
.vertical_stacks
++;
3400 case SWM_ARG_ID_STACKDEC
:
3401 if (ws
->l_state
.vertical_stacks
> 1)
3402 ws
->l_state
.vertical_stacks
--;
3404 case SWM_ARG_ID_FLIPLAYOUT
:
3405 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3413 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3415 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3417 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3421 horizontal_config(struct workspace
*ws
, int id
)
3423 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3426 case SWM_ARG_ID_STACKRESET
:
3427 case SWM_ARG_ID_STACKINIT
:
3428 ws
->l_state
.horizontal_mwin
= 1;
3429 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3430 ws
->l_state
.horizontal_stacks
= 1;
3432 case SWM_ARG_ID_MASTERSHRINK
:
3433 if (ws
->l_state
.horizontal_msize
> 1)
3434 ws
->l_state
.horizontal_msize
--;
3436 case SWM_ARG_ID_MASTERGROW
:
3437 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3438 ws
->l_state
.horizontal_msize
++;
3440 case SWM_ARG_ID_MASTERADD
:
3441 ws
->l_state
.horizontal_mwin
++;
3443 case SWM_ARG_ID_MASTERDEL
:
3444 if (ws
->l_state
.horizontal_mwin
> 0)
3445 ws
->l_state
.horizontal_mwin
--;
3447 case SWM_ARG_ID_STACKINC
:
3448 ws
->l_state
.horizontal_stacks
++;
3450 case SWM_ARG_ID_STACKDEC
:
3451 if (ws
->l_state
.horizontal_stacks
> 1)
3452 ws
->l_state
.horizontal_stacks
--;
3454 case SWM_ARG_ID_FLIPLAYOUT
:
3455 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3463 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3465 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3467 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3470 /* fullscreen view */
3472 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3474 struct swm_geometry gg
= *g
;
3475 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3476 int winno
, num_screens
;
3478 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3483 winno
= count_win(ws
, 0);
3484 if (winno
== 0 && count_win(ws
, 1) == 0)
3487 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3488 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3489 if (win
->transient
) {
3491 parent
= find_window(win
->transient
);
3495 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3497 * retain geometry for retrieval on exit from
3500 store_float_geom(win
, ws
->r
);
3501 win
->floatmaxed
= 1;
3504 /* only reconfigure if necessary */
3505 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3506 HEIGHT(win
) != gg
.h
) {
3512 WIDTH(win
) += 2 * border_width
;
3513 HEIGHT(win
) += 2 * border_width
;
3518 /* unmap only if we don't have multi screen */
3519 if (win
!= ws
->focus
)
3520 if (!(num_screens
> 1 || outputs
> 1))
3524 /* put the last transient on top */
3527 map_window_raised(parent
->id
);
3528 stack_floater(wintrans
, ws
->r
);
3529 focus_magic(wintrans
);
3534 send_to_ws(struct swm_region
*r
, union arg
*args
)
3536 int wsid
= args
->id
;
3537 struct ws_win
*win
= NULL
, *parent
;
3538 struct workspace
*ws
, *nws
;
3539 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
3540 char ws_idx_str
[SWM_PROPLEN
];
3543 if (wsid
>= workspace_limit
)
3546 if (r
&& r
->ws
&& r
->ws
->focus
)
3552 if (win
->ws
->idx
== wsid
)
3555 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3558 nws
= &win
->s
->ws
[wsid
];
3560 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3562 if (win
->transient
) {
3563 parent
= find_window(win
->transient
);
3565 unmap_window(parent
);
3566 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3567 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3572 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3573 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3574 if (TAILQ_EMPTY(&ws
->winlist
))
3575 r
->ws
->focus
= NULL
;
3578 /* Try to update the window's workspace property */
3579 ws_idx_atom
= get_atom_from_string("_SWM_WS");
3581 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3583 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3585 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3586 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3595 pressbutton(struct swm_region
*r
, union arg
*args
)
3597 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3598 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3599 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3600 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3604 raise_toggle(struct swm_region
*r
, union arg
*args
)
3606 if (r
== NULL
|| r
->ws
== NULL
)
3609 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3611 /* bring floaters back to top */
3612 if (r
->ws
->always_raise
== 0)
3617 iconify(struct swm_region
*r
, union arg
*args
)
3621 if (r
->ws
->focus
== NULL
)
3623 unmap_window(r
->ws
->focus
);
3624 update_iconic(r
->ws
->focus
, 1);
3627 r
->ws
->focus
= NULL
;
3628 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3633 get_win_name(xcb_window_t win
)
3636 xcb_get_property_cookie_t c
;
3637 xcb_icccm_get_text_property_reply_t r
;
3639 c
= xcb_icccm_get_wm_name(conn
, win
);
3640 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3641 if (r
.name_len
> 0) {
3642 name
= malloc(r
.name_len
+ 1);
3644 xcb_icccm_get_text_property_reply_wipe(&r
);
3647 memcpy(name
, r
.name
, r
.name_len
);
3648 name
[r
.name_len
] = '\0';
3650 xcb_icccm_get_text_property_reply_wipe(&r
);
3657 uniconify(struct swm_region
*r
, union arg
*args
)
3664 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3666 if (r
== NULL
|| r
->ws
== NULL
)
3669 /* make sure we have anything to uniconify */
3670 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3671 if (win
->ws
== NULL
)
3672 continue; /* should never happen */
3673 if (win
->iconic
== 0)
3681 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3683 spawn_select(r
, args
, "search", &searchpid
);
3685 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3688 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3689 if (win
->ws
== NULL
)
3690 continue; /* should never happen */
3691 if (win
->iconic
== 0)
3694 name
= get_win_name(win
->id
);
3697 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3705 name_workspace(struct swm_region
*r
, union arg
*args
)
3709 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3715 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3717 spawn_select(r
, args
, "name_workspace", &searchpid
);
3719 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3722 fprintf(lfile
, "%s", "");
3727 search_workspace(struct swm_region
*r
, union arg
*args
)
3730 struct workspace
*ws
;
3733 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3739 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3741 spawn_select(r
, args
, "search", &searchpid
);
3743 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3746 for (i
= 0; i
< workspace_limit
; i
++) {
3750 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3751 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3758 search_win_cleanup(void)
3760 struct search_window
*sw
= NULL
;
3762 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3763 xcb_destroy_window(conn
, sw
->indicator
);
3764 xcb_free_gc(conn
, sw
->gc
);
3765 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3771 search_win(struct swm_region
*r
, union arg
*args
)
3773 struct ws_win
*win
= NULL
;
3774 struct search_window
*sw
= NULL
;
3776 uint32_t gcv
[3], wa
[2];
3781 XRectangle ibox
, lbox
;
3782 DNPRINTF(SWM_D_MISC
, "search_win\n");
3785 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3787 spawn_select(r
, args
, "search", &searchpid
);
3789 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3792 TAILQ_INIT(&search_wl
);
3795 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3796 if (win
->iconic
== 1)
3799 sw
= calloc(1, sizeof(struct search_window
));
3801 warn("search_win: calloc");
3803 search_win_cleanup();
3809 snprintf(s
, sizeof s
, "%d", i
);
3812 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3814 w
= xcb_generate_id(conn
);
3815 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3816 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
;
3817 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3818 lbox
.width
+ 4, bar_fs_extents
->max_logical_extent
.height
,
3819 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3820 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3823 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3825 sw
->gc
= xcb_generate_id(conn
);
3826 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
3827 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3829 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3830 XCB_GC_BACKGROUND
| XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
3831 map_window_raised(w
);
3833 xcb_image_text_8(conn
, len
, w
, sw
->gc
, 2,
3834 (bar_fs_extents
->max_logical_extent
.height
-
3835 lbox
.height
) / 2 - lbox
.y
, s
);
3837 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
3839 fprintf(lfile
, "%d\n", i
);
3847 search_resp_uniconify(char *resp
, unsigned long len
)
3853 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3855 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3856 if (win
->iconic
== 0)
3858 name
= get_win_name(win
->id
);
3861 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3866 if (strncmp(s
, resp
, len
) == 0) {
3867 /* XXX this should be a callback to generalize */
3868 update_iconic(win
, 0);
3877 search_resp_name_workspace(char *resp
, unsigned long len
)
3879 struct workspace
*ws
;
3881 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3883 if (search_r
->ws
== NULL
)
3888 free(search_r
->ws
->name
);
3889 search_r
->ws
->name
= NULL
;
3893 ws
->name
= strdup(resp
);
3894 if (ws
->name
== NULL
) {
3895 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3896 "strdup: %s", strerror(errno
));
3903 search_resp_search_workspace(char *resp
, unsigned long len
)
3910 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3914 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3921 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3923 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3930 switchws(search_r
, &a
);
3934 search_resp_search_window(char *resp
, unsigned long len
)
3939 struct search_window
*sw
;
3941 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3945 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3950 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3952 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3959 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3960 if (idx
== sw
->idx
) {
3966 #define MAX_RESP_LEN 1024
3969 search_do_resp(void)
3975 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3980 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3981 warn("search: calloc");
3985 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3987 warn("search: read error");
3990 resp
[rbytes
] = '\0';
3993 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3994 * newline, so work around that by sanitizing the resp now.
3996 resp
[strcspn(resp
, "\n")] = '\0';
3999 switch (search_resp_action
) {
4000 case SWM_SEARCH_UNICONIFY
:
4001 search_resp_uniconify(resp
, len
);
4003 case SWM_SEARCH_NAME_WORKSPACE
:
4004 search_resp_name_workspace(resp
, len
);
4006 case SWM_SEARCH_SEARCH_WORKSPACE
:
4007 search_resp_search_workspace(resp
, len
);
4009 case SWM_SEARCH_SEARCH_WINDOW
:
4010 search_resp_search_window(resp
, len
);
4015 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4016 search_win_cleanup();
4018 search_resp_action
= SWM_SEARCH_NONE
;
4019 close(select_resp_pipe
[0]);
4024 wkill(struct swm_region
*r
, union arg
*args
)
4026 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4028 if (r
->ws
->focus
== NULL
)
4031 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4032 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4034 if (r
->ws
->focus
->can_delete
)
4035 client_msg(r
->ws
->focus
, adelete
);
4040 floating_toggle_win(struct ws_win
*win
)
4042 struct swm_region
*r
;
4052 /* reject floating toggles in max stack mode */
4053 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4056 if (win
->floating
) {
4057 if (!win
->floatmaxed
) {
4058 /* retain position for refloat */
4059 store_float_geom(win
, r
);
4063 if (win
->g_floatvalid
) {
4064 /* refloat at last floating relative position */
4065 X(win
) = win
->g_float
.x
+ X(r
);
4066 Y(win
) = win
->g_float
.y
+ Y(r
);
4067 WIDTH(win
) = win
->g_float
.w
;
4068 HEIGHT(win
) = win
->g_float
.h
;
4073 ewmh_update_actions(win
);
4079 floating_toggle(struct swm_region
*r
, union arg
*args
)
4081 struct ws_win
*win
= r
->ws
->focus
;
4087 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4088 _NET_WM_STATE_TOGGLE
);
4092 if (win
== win
->ws
->focus
) {
4093 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4094 focus(win
->ws
->r
, &a
);
4099 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4101 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4103 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4105 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4108 if (X(win
) + BORDER(win
) < X(r
)) {
4110 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4112 X(win
) = X(r
) - BORDER(win
);
4115 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4117 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4119 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4122 if (Y(win
) + BORDER(win
) < Y(r
)) {
4124 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4126 Y(win
) = Y(r
) - BORDER(win
);
4132 if (HEIGHT(win
) < 1)
4138 update_window(struct ws_win
*win
)
4143 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4144 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4145 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4149 wc
[3] = HEIGHT(win
);
4150 wc
[4] = BORDER(win
);
4152 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4153 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4154 wc
[3], YESNO(win
->bordered
));
4156 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4159 #define SWM_RESIZE_STEPS (50)
4162 resize(struct ws_win
*win
, union arg
*args
)
4164 xcb_timestamp_t timestamp
= 0;
4165 struct swm_region
*r
= NULL
;
4166 int resize_step
= 0;
4167 struct swm_geometry g
;
4168 int top
= 0, left
= 0, buttonrelease
;
4170 unsigned int shape
; /* cursor style */
4171 xcb_cursor_t cursor
;
4172 xcb_font_t cursor_font
;
4173 xcb_grab_pointer_cookie_t gpc
;
4174 xcb_grab_pointer_reply_t
*gpr
;
4175 xcb_query_pointer_reply_t
*xpr
;
4176 xcb_generic_event_t
*evt
;
4177 xcb_motion_notify_event_t
*mne
;
4183 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4186 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%x, floating: %s, "
4187 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4190 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4193 /* reject resizes in max mode for floaters (transient ok) */
4194 if (win
->floatmaxed
)
4198 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4204 case SWM_ARG_ID_WIDTHSHRINK
:
4205 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4208 case SWM_ARG_ID_WIDTHGROW
:
4209 WIDTH(win
) += SWM_RESIZE_STEPS
;
4212 case SWM_ARG_ID_HEIGHTSHRINK
:
4213 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4216 case SWM_ARG_ID_HEIGHTGROW
:
4217 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4224 constrain_window(win
, r
, 1);
4226 store_float_geom(win
,r
);
4230 /* get cursor offset from window root */
4231 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4238 if (xpr
->win_x
< WIDTH(win
) / 2)
4241 if (xpr
->win_y
< HEIGHT(win
) / 2)
4244 if (args
->id
== SWM_ARG_ID_CENTER
)
4247 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4249 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4251 cursor_font
= xcb_generate_id(conn
);
4252 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4254 cursor
= xcb_generate_id(conn
);
4255 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4256 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4258 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4259 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4261 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4263 xcb_free_cursor(conn
, cursor
);
4264 xcb_close_font(conn
, cursor_font
);
4270 while ((evt
= xcb_poll_for_event(conn
)) && buttonrelease
!= 1) {
4272 XMaskEvent(display, MOUSEMASK | ExposureMask |
4273 SubstructureRedirectMask, &ev);
4275 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4276 case XCB_BUTTON_RELEASE
:
4279 case XCB_MOTION_NOTIFY
:
4280 mne
= (xcb_motion_notify_event_t
*)evt
;
4281 /* cursor offset/delta from start of the operation */
4282 dx
= mne
->root_x
- xpr
->root_x
;
4283 dy
= mne
->root_y
- xpr
->root_y
;
4289 if (args
->id
== SWM_ARG_ID_CENTER
) {
4290 if (g
.h
/ 2 + dy
< 1)
4294 HEIGHT(win
) = g
.h
+ 2 * dy
;
4302 HEIGHT(win
) = g
.h
+ dy
;
4309 if (args
->id
== SWM_ARG_ID_CENTER
) {
4310 if (g
.w
/ 2 + dx
< 1)
4314 WIDTH(win
) = g
.w
+ 2 * dx
;
4322 WIDTH(win
) = g
.w
+ dx
;
4325 constrain_window(win
, r
, 1);
4327 /* not free, don't sync more than 120 times / second */
4328 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4329 timestamp
= mne
->time
;
4344 store_float_geom(win
,r
);
4346 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4347 xcb_free_cursor(conn
, cursor
);
4348 xcb_close_font(conn
, cursor_font
);
4354 resize_step(struct swm_region
*r
, union arg
*args
)
4356 struct ws_win
*win
= NULL
;
4358 if (r
&& r
->ws
&& r
->ws
->focus
)
4366 #define SWM_MOVE_STEPS (50)
4369 move(struct ws_win
*win
, union arg
*args
)
4371 xcb_timestamp_t timestamp
= 0;
4372 int move_step
= 0, buttonrelease
;
4373 struct swm_region
*r
= NULL
;
4374 xcb_font_t cursor_font
;
4375 xcb_cursor_t cursor
;
4376 xcb_grab_pointer_cookie_t gpc
;
4377 xcb_grab_pointer_reply_t
*gpr
;
4378 xcb_query_pointer_reply_t
*qpr
;
4379 xcb_generic_event_t
*evt
;
4380 xcb_motion_notify_event_t
*mne
;
4386 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4389 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%x, floating: %s, transient: "
4390 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4392 /* in max_stack mode should only move transients */
4393 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4397 if (win
->floating
== 0 && !win
->transient
) {
4398 store_float_geom(win
, r
);
4399 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4402 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4409 case SWM_ARG_ID_MOVELEFT
:
4410 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4413 case SWM_ARG_ID_MOVERIGHT
:
4414 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4417 case SWM_ARG_ID_MOVEUP
:
4418 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4421 case SWM_ARG_ID_MOVEDOWN
:
4422 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4429 constrain_window(win
, r
, 0);
4431 store_float_geom(win
, r
);
4435 cursor_font
= xcb_generate_id(conn
);
4436 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4438 cursor
= xcb_generate_id(conn
);
4439 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4440 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4442 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4443 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4444 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4445 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4447 xcb_free_cursor(conn
, cursor
);
4448 xcb_close_font(conn
, cursor_font
);
4452 /* get cursor offset from window root */
4453 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4456 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4457 xcb_free_cursor(conn
, cursor
);
4458 xcb_close_font(conn
, cursor_font
);
4463 while ((evt
= xcb_poll_for_event(conn
)) && buttonrelease
!= 1) {
4465 XMaskEvent(display, MOUSEMASK | ExposureMask |
4466 SubstructureRedirectMask, &ev);
4468 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4469 case XCB_BUTTON_RELEASE
:
4472 case XCB_MOTION_NOTIFY
:
4473 mne
= (xcb_motion_notify_event_t
*)evt
;
4474 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4475 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4477 constrain_window(win
, r
, 0);
4479 /* not free, don't sync more than 120 times / second */
4480 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4481 timestamp
= mne
->time
;
4496 store_float_geom(win
, r
);
4498 xcb_free_cursor(conn
, cursor
);
4499 xcb_close_font(conn
, cursor_font
);
4500 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4504 move_step(struct swm_region
*r
, union arg
*args
)
4506 struct ws_win
*win
= NULL
;
4508 if (r
&& r
->ws
&& r
->ws
->focus
)
4513 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4520 /* user/key callable function IDs */
4609 kf_search_workspace
,
4611 kf_dumpwins
, /* MUST BE LAST */
4615 /* key definitions */
4617 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4622 char name
[SWM_FUNCNAME_LEN
];
4623 void (*func
)(struct swm_region
*r
, union arg
*);
4625 } keyfuncs
[kf_invalid
+ 1] = {
4626 /* name function argument */
4627 { "cycle_layout", cycle_layout
, {0} },
4628 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4629 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4630 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4631 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4632 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4633 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4634 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4635 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4636 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4637 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4638 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4639 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4640 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4641 { "quit", quit
, {0} },
4642 { "restart", restart
, {0} },
4643 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4644 { "ws_1", switchws
, {.id
= 0} },
4645 { "ws_2", switchws
, {.id
= 1} },
4646 { "ws_3", switchws
, {.id
= 2} },
4647 { "ws_4", switchws
, {.id
= 3} },
4648 { "ws_5", switchws
, {.id
= 4} },
4649 { "ws_6", switchws
, {.id
= 5} },
4650 { "ws_7", switchws
, {.id
= 6} },
4651 { "ws_8", switchws
, {.id
= 7} },
4652 { "ws_9", switchws
, {.id
= 8} },
4653 { "ws_10", switchws
, {.id
= 9} },
4654 { "ws_11", switchws
, {.id
= 10} },
4655 { "ws_12", switchws
, {.id
= 11} },
4656 { "ws_13", switchws
, {.id
= 12} },
4657 { "ws_14", switchws
, {.id
= 13} },
4658 { "ws_15", switchws
, {.id
= 14} },
4659 { "ws_16", switchws
, {.id
= 15} },
4660 { "ws_17", switchws
, {.id
= 16} },
4661 { "ws_18", switchws
, {.id
= 17} },
4662 { "ws_19", switchws
, {.id
= 18} },
4663 { "ws_20", switchws
, {.id
= 19} },
4664 { "ws_21", switchws
, {.id
= 20} },
4665 { "ws_22", switchws
, {.id
= 21} },
4666 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4667 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4668 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4669 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4670 { "ws_prior", priorws
, {0} },
4671 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4672 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4673 { "mvws_1", send_to_ws
, {.id
= 0} },
4674 { "mvws_2", send_to_ws
, {.id
= 1} },
4675 { "mvws_3", send_to_ws
, {.id
= 2} },
4676 { "mvws_4", send_to_ws
, {.id
= 3} },
4677 { "mvws_5", send_to_ws
, {.id
= 4} },
4678 { "mvws_6", send_to_ws
, {.id
= 5} },
4679 { "mvws_7", send_to_ws
, {.id
= 6} },
4680 { "mvws_8", send_to_ws
, {.id
= 7} },
4681 { "mvws_9", send_to_ws
, {.id
= 8} },
4682 { "mvws_10", send_to_ws
, {.id
= 9} },
4683 { "mvws_11", send_to_ws
, {.id
= 10} },
4684 { "mvws_12", send_to_ws
, {.id
= 11} },
4685 { "mvws_13", send_to_ws
, {.id
= 12} },
4686 { "mvws_14", send_to_ws
, {.id
= 13} },
4687 { "mvws_15", send_to_ws
, {.id
= 14} },
4688 { "mvws_16", send_to_ws
, {.id
= 15} },
4689 { "mvws_17", send_to_ws
, {.id
= 16} },
4690 { "mvws_18", send_to_ws
, {.id
= 17} },
4691 { "mvws_19", send_to_ws
, {.id
= 18} },
4692 { "mvws_20", send_to_ws
, {.id
= 19} },
4693 { "mvws_21", send_to_ws
, {.id
= 20} },
4694 { "mvws_22", send_to_ws
, {.id
= 21} },
4695 { "bar_toggle", bar_toggle
, {0} },
4696 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4697 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4698 { "float_toggle", floating_toggle
,{0} },
4699 { "version", version
, {0} },
4700 { "spawn_custom", dummykeyfunc
, {0} },
4701 { "iconify", iconify
, {0} },
4702 { "uniconify", uniconify
, {0} },
4703 { "raise_toggle", raise_toggle
, {0} },
4704 { "button2", pressbutton
, {2} },
4705 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4706 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4707 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4708 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4709 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4710 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4711 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4712 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4713 { "name_workspace", name_workspace
, {0} },
4714 { "search_workspace", search_workspace
, {0} },
4715 { "search_win", search_win
, {0} },
4716 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4717 { "invalid key func", NULL
, {0} },
4720 RB_ENTRY(key
) entry
;
4723 enum keyfuncid funcid
;
4726 RB_HEAD(key_tree
, key
);
4729 key_cmp(struct key
*kp1
, struct key
*kp2
)
4731 if (kp1
->keysym
< kp2
->keysym
)
4733 if (kp1
->keysym
> kp2
->keysym
)
4736 if (kp1
->mod
< kp2
->mod
)
4738 if (kp1
->mod
> kp2
->mod
)
4744 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4745 struct key_tree keys
;
4748 enum { client_click
, root_click
};
4750 unsigned int action
;
4752 unsigned int button
;
4753 void (*func
)(struct ws_win
*, union arg
*);
4756 /* action key mouse button func args */
4757 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4758 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4759 { client_click
, MODKEY
, Button1
, move
, {0} },
4763 update_modkey(unsigned int mod
)
4769 RB_FOREACH(kp
, key_tree
, &keys
)
4770 if (kp
->mod
& ShiftMask
)
4771 kp
->mod
= mod
| ShiftMask
;
4775 for (i
= 0; i
< LENGTH(buttons
); i
++)
4776 if (buttons
[i
].mask
& ShiftMask
)
4777 buttons
[i
].mask
= mod
| ShiftMask
;
4779 buttons
[i
].mask
= mod
;
4784 TAILQ_ENTRY(spawn_prog
) entry
;
4789 TAILQ_HEAD(spawn_list
, spawn_prog
);
4790 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4793 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4796 struct spawn_prog
*prog
= NULL
;
4798 char *ap
, **real_args
;
4800 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4803 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4804 if (!strcasecmp(spawn_name
, prog
->name
))
4808 warnx("spawn_custom: program %s not found", spawn_name
);
4812 /* make room for expanded args */
4813 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4814 err(1, "spawn_custom: calloc real_args");
4816 /* expand spawn_args into real_args */
4817 for (i
= 0; i
< prog
->argc
; i
++) {
4819 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4820 if (!strcasecmp(ap
, "$bar_border")) {
4822 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4824 err(1, "spawn_custom border color");
4825 } else if (!strcasecmp(ap
, "$bar_color")) {
4827 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4829 err(1, "spawn_custom bar color");
4830 } else if (!strcasecmp(ap
, "$bar_font")) {
4831 if ((real_args
[i
] = strdup(bar_fonts
))
4833 err(1, "spawn_custom bar fonts");
4834 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4836 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4838 err(1, "spawn_custom color font");
4839 } else if (!strcasecmp(ap
, "$color_focus")) {
4841 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4843 err(1, "spawn_custom color focus");
4844 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4846 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4848 err(1, "spawn_custom color unfocus");
4850 /* no match --> copy as is */
4851 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4852 err(1, "spawn_custom strdup(ap)");
4854 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4859 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4860 for (i
= 0; i
< prog
->argc
; i
++)
4861 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4862 DNPRINTF(SWM_D_SPAWN
, "\n");
4864 *ret_args
= real_args
;
4865 return (prog
->argc
);
4869 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4875 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4879 spawn(r
->ws
->idx
, &a
, 1);
4881 for (i
= 0; i
< spawn_argc
; i
++)
4887 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4893 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4897 if (pipe(select_list_pipe
) == -1)
4898 err(1, "pipe error");
4899 if (pipe(select_resp_pipe
) == -1)
4900 err(1, "pipe error");
4902 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4903 err(1, "could not disable SIGPIPE");
4904 switch (*pid
= fork()) {
4906 err(1, "cannot fork");
4909 if (dup2(select_list_pipe
[0], 0) == -1)
4911 if (dup2(select_resp_pipe
[1], 1) == -1)
4913 close(select_list_pipe
[1]);
4914 close(select_resp_pipe
[0]);
4915 spawn(r
->ws
->idx
, &a
, 0);
4917 default: /* parent */
4918 close(select_list_pipe
[0]);
4919 close(select_resp_pipe
[1]);
4923 for (i
= 0; i
< spawn_argc
; i
++)
4929 spawn_insert(char *name
, char *args
)
4931 char *arg
, *cp
, *ptr
;
4932 struct spawn_prog
*sp
;
4934 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4936 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4937 err(1, "spawn_insert: malloc");
4938 if ((sp
->name
= strdup(name
)) == NULL
)
4939 err(1, "spawn_insert: strdup");
4941 /* convert the arguments to an argument list */
4942 if ((ptr
= cp
= strdup(args
)) == NULL
)
4943 err(1, "spawn_insert: strdup");
4944 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4945 /* empty field; skip it */
4950 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4951 sizeof *sp
->argv
)) == NULL
)
4952 err(1, "spawn_insert: realloc");
4953 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4954 err(1, "spawn_insert: strdup");
4958 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4959 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4963 spawn_remove(struct spawn_prog
*sp
)
4967 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4969 TAILQ_REMOVE(&spawns
, sp
, entry
);
4970 for (i
= 0; i
< sp
->argc
; i
++)
4976 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4980 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4982 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4985 spawn_insert(name
, args
);
4987 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4991 setspawn(char *name
, char *args
)
4993 struct spawn_prog
*sp
;
4995 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5000 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5001 if (!strcmp(sp
->name
, name
)) {
5005 spawn_replace(sp
, name
, args
);
5006 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5010 if (*args
== '\0') {
5011 warnx("error: setspawn: cannot find program: %s", name
);
5015 spawn_insert(name
, args
);
5016 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5020 setconfspawn(char *selector
, char *value
, int flags
)
5022 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5024 setspawn(selector
, value
);
5026 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5033 setconfspawn("term", "xterm", 0);
5034 setconfspawn("spawn_term", "xterm", 0);
5035 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5036 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5037 setconfspawn("lock", "xlock", 0);
5038 setconfspawn("initscr", "initscreen.sh", 0);
5039 setconfspawn("menu", "dmenu_run"
5042 " -nf $bar_font_color"
5044 " -sf $bar_color", 0);
5045 setconfspawn("search", "dmenu"
5049 " -nf $bar_font_color"
5051 " -sf $bar_color", 0);
5052 setconfspawn("name_workspace", "dmenu"
5056 " -nf $bar_font_color"
5058 " -sf $bar_color", 0);
5062 #define SWM_MODNAME_SIZE 32
5063 #define SWM_KEY_WS "\n+ \t"
5065 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5069 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5070 if (mod
== NULL
|| ks
== NULL
) {
5071 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5074 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5075 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5081 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5082 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5084 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5085 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5087 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5089 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5091 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5093 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5095 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5097 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5098 *mod
|= ControlMask
;
5100 *ks
= XStringToKeysym(name
);
5101 XConvertCase(*ks
, ks
, &uks
);
5102 if (ks
== NoSymbol
) {
5104 "parsekeys: invalid key %s\n",
5110 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5115 strdupsafe(char *str
)
5120 return (strdup(str
));
5124 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5128 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5129 keyfuncs
[kfid
].name
, spawn_name
);
5131 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5132 err(1, "key_insert: malloc");
5137 kp
->spawn_name
= strdupsafe(spawn_name
);
5138 RB_INSERT(key_tree
, &keys
, kp
);
5140 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5144 key_lookup(unsigned int mod
, KeySym ks
)
5151 return (RB_FIND(key_tree
, &keys
, &kp
));
5155 key_remove(struct key
*kp
)
5157 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5159 RB_REMOVE(key_tree
, &keys
, kp
);
5160 free(kp
->spawn_name
);
5163 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5167 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5170 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5174 key_insert(mod
, ks
, kfid
, spawn_name
);
5176 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5180 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5185 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5186 keyfuncs
[kfid
].name
, spawn_name
);
5188 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5189 if (kfid
== kf_invalid
)
5192 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5193 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5196 if (kfid
== kf_invalid
) {
5197 warnx("error: setkeybinding: cannot find mod/key combination");
5198 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5202 key_insert(mod
, ks
, kfid
, spawn_name
);
5203 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5207 setconfbinding(char *selector
, char *value
, int flags
)
5209 enum keyfuncid kfid
;
5212 struct spawn_prog
*sp
;
5213 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5214 if (selector
== NULL
) {
5215 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5216 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5218 setkeybinding(mod
, ks
, kfid
, NULL
);
5223 /* search by key function name */
5224 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5225 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5226 SWM_FUNCNAME_LEN
) == 0) {
5227 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5229 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5230 setkeybinding(mod
, ks
, kfid
, NULL
);
5236 /* search by custom spawn name */
5237 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5238 if (strcasecmp(selector
, sp
->name
) == 0) {
5239 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5241 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5242 setkeybinding(mod
, ks
, kf_spawn_custom
,
5249 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5256 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5257 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5258 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5259 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5260 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5261 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5262 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5263 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5264 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5265 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5266 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5267 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5268 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5269 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5270 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5271 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5272 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5273 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5274 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5275 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5276 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5277 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5278 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5279 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5280 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5281 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5282 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5283 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5284 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5285 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5286 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5287 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5288 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5289 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5290 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5291 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5292 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5293 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5294 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5295 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5296 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5297 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5298 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5299 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5300 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5301 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5302 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5303 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5304 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5305 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5306 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5307 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5308 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5309 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5310 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5311 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5312 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5313 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5314 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5315 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5316 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5317 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5318 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5319 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5320 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5321 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5322 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5323 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5324 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5325 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5326 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5327 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5328 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5329 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5330 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5331 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5332 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5333 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5334 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5335 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5336 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5337 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5338 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5339 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5340 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5341 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5342 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5343 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5344 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5345 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5346 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5347 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5348 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5349 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5350 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5351 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5353 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5362 while (RB_EMPTY(&keys
) == 0) {
5363 kp
= RB_ROOT(&keys
);
5369 setkeymapping(char *selector
, char *value
, int flags
)
5371 char keymapping_file
[PATH_MAX
];
5372 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5373 if (value
[0] == '~')
5374 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5375 pwd
->pw_dir
, &value
[1]);
5377 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5379 /* load new key bindings; if it fails, revert to default bindings */
5380 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5384 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5389 updatenumlockmask(void)
5392 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5393 xcb_keycode_t
*modmap
, kc
;
5395 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5398 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5399 xcb_get_modifier_mapping(conn
),
5402 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5403 for (i
= 0; i
< 8; i
++) {
5404 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5405 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5408 if (kc
== *((xcb_keycode_t
*)xcb_key_symbols_get_keycode(syms
,
5410 numlockmask
= (1 << i
);
5422 xcb_keycode_t
*code
;
5423 unsigned int modifiers
[] =
5424 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5427 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5428 updatenumlockmask();
5430 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5431 for (k
= 0; k
< num_screens
; k
++) {
5432 if (TAILQ_EMPTY(&screens
[k
].rl
))
5434 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5436 RB_FOREACH(kp
, key_tree
, &keys
) {
5437 if ((code
= xcb_key_symbols_get_keycode(syms
,
5439 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5440 xcb_grab_key(conn
, True
,
5442 kp
->mod
| modifiers
[j
],
5443 *code
, XCB_GRAB_MODE_ASYNC
,
5444 XCB_GRAB_MODE_ASYNC
);
5450 grabbuttons(struct ws_win
*win
, int focused
)
5453 unsigned int modifiers
[] =
5454 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5456 updatenumlockmask();
5457 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5458 XCB_BUTTON_MASK_ANY
);
5460 for (i
= 0; i
< LENGTH(buttons
); i
++)
5461 if (buttons
[i
].action
== client_click
)
5462 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5463 xcb_grab_button(conn
, False
, win
->id
,
5465 XCB_GRAB_MODE_ASYNC
,
5472 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5473 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5474 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5477 const char *quirkname
[] = {
5478 "NONE", /* config string for "no value" */
5487 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5488 #define SWM_Q_WS "\n|+ \t"
5490 parsequirks(char *qstr
, unsigned long *quirk
)
5500 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5502 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5503 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5504 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5505 DNPRINTF(SWM_D_QUIRK
,
5506 "parsequirks: %s\n", name
);
5511 *quirk
|= 1 << (i
-1);
5515 if (i
>= LENGTH(quirkname
)) {
5516 DNPRINTF(SWM_D_QUIRK
,
5517 "parsequirks: invalid quirk [%s]\n", name
);
5525 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5529 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5532 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5533 err(1, "quirk_insert: malloc");
5534 if ((qp
->class = strdup(class)) == NULL
)
5535 err(1, "quirk_insert: strdup");
5536 if ((qp
->name
= strdup(name
)) == NULL
)
5537 err(1, "quirk_insert: strdup");
5540 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5542 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5546 quirk_remove(struct quirk
*qp
)
5548 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5549 qp
->name
, qp
->quirk
);
5551 TAILQ_REMOVE(&quirks
, qp
, entry
);
5556 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5560 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5561 unsigned long quirk
)
5563 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5564 qp
->name
, qp
->quirk
);
5567 quirk_insert(class, name
, quirk
);
5569 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5573 setquirk(const char *class, const char *name
, unsigned long quirk
)
5577 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5580 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5581 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5585 quirk_replace(qp
, class, name
, quirk
);
5586 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5591 warnx("error: setquirk: cannot find class/name combination");
5595 quirk_insert(class, name
, quirk
);
5596 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5600 setconfquirk(char *selector
, char *value
, int flags
)
5602 char *cp
, *class, *name
;
5604 unsigned long quirks
;
5605 if (selector
== NULL
)
5607 if ((cp
= strchr(selector
, ':')) == NULL
)
5612 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5613 setquirk(class, name
, quirks
);
5620 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5621 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5622 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5623 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5624 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5625 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5626 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5627 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5628 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5629 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5630 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5631 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5632 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5635 /* conf file stuff */
5636 #define SWM_CONF_FILE "spectrwm.conf"
5637 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5641 SWM_S_BAR_AT_BOTTOM
,
5642 SWM_S_BAR_BORDER_WIDTH
,
5649 SWM_S_CLOCK_ENABLED
,
5652 SWM_S_CYCLE_VISIBLE
,
5654 SWM_S_DISABLE_BORDER
,
5656 SWM_S_FOCUS_CLOSE_WRAP
,
5657 SWM_S_FOCUS_DEFAULT
,
5663 SWM_S_STACK_ENABLED
,
5665 SWM_S_TITLE_CLASS_ENABLED
,
5666 SWM_S_TITLE_NAME_ENABLED
,
5667 SWM_S_URGENT_ENABLED
,
5668 SWM_S_VERBOSE_LAYOUT
,
5669 SWM_S_WINDOW_NAME_ENABLED
,
5670 SWM_S_WORKSPACE_LIMIT
5674 setconfvalue(char *selector
, char *value
, int flags
)
5680 case SWM_S_BAR_ACTION
:
5682 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5683 err(1, "setconfvalue: bar_action");
5685 case SWM_S_BAR_AT_BOTTOM
:
5686 bar_at_bottom
= atoi(value
);
5688 case SWM_S_BAR_BORDER_WIDTH
:
5689 bar_border_width
= atoi(value
);
5690 if (bar_border_width
< 0)
5691 bar_border_width
= 0;
5693 case SWM_S_BAR_DELAY
:
5694 bar_delay
= atoi(value
);
5696 case SWM_S_BAR_ENABLED
:
5697 bar_enabled
= atoi(value
);
5699 case SWM_S_BAR_FONT
:
5701 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5702 err(1, "setconfvalue: asprintf: failed to allocate "
5703 "memory for bar_fonts.");
5707 case SWM_S_BAR_FORMAT
:
5709 if ((bar_format
= strdup(value
)) == NULL
)
5710 err(1, "setconfvalue: bar_format");
5712 case SWM_S_BAR_JUSTIFY
:
5713 if (!strcmp(value
, "left"))
5714 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5715 else if (!strcmp(value
, "center"))
5716 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5717 else if (!strcmp(value
, "right"))
5718 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5720 errx(1, "invalid bar_justify");
5722 case SWM_S_BORDER_WIDTH
:
5723 border_width
= atoi(value
);
5724 if (border_width
< 0)
5727 case SWM_S_CLOCK_ENABLED
:
5728 clock_enabled
= atoi(value
);
5730 case SWM_S_CLOCK_FORMAT
:
5731 #ifndef SWM_DENY_CLOCK_FORMAT
5733 if ((clock_format
= strdup(value
)) == NULL
)
5734 err(1, "setconfvalue: clock_format");
5737 case SWM_S_CYCLE_EMPTY
:
5738 cycle_empty
= atoi(value
);
5740 case SWM_S_CYCLE_VISIBLE
:
5741 cycle_visible
= atoi(value
);
5743 case SWM_S_DIALOG_RATIO
:
5744 dialog_ratio
= atof(value
);
5745 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5748 case SWM_S_DISABLE_BORDER
:
5749 disable_border
= atoi(value
);
5751 case SWM_S_FOCUS_CLOSE
:
5752 if (!strcmp(value
, "first"))
5753 focus_close
= SWM_STACK_BOTTOM
;
5754 else if (!strcmp(value
, "last"))
5755 focus_close
= SWM_STACK_TOP
;
5756 else if (!strcmp(value
, "next"))
5757 focus_close
= SWM_STACK_ABOVE
;
5758 else if (!strcmp(value
, "previous"))
5759 focus_close
= SWM_STACK_BELOW
;
5761 errx(1, "focus_close");
5763 case SWM_S_FOCUS_CLOSE_WRAP
:
5764 focus_close_wrap
= atoi(value
);
5766 case SWM_S_FOCUS_DEFAULT
:
5767 if (!strcmp(value
, "last"))
5768 focus_default
= SWM_STACK_TOP
;
5769 else if (!strcmp(value
, "first"))
5770 focus_default
= SWM_STACK_BOTTOM
;
5772 errx(1, "focus_default");
5774 case SWM_S_FOCUS_MODE
:
5775 if (!strcmp(value
, "default"))
5776 focus_mode
= SWM_FOCUS_DEFAULT
;
5777 else if (!strcmp(value
, "follow_cursor"))
5778 focus_mode
= SWM_FOCUS_FOLLOW
;
5779 else if (!strcmp(value
, "synergy"))
5780 focus_mode
= SWM_FOCUS_SYNERGY
;
5782 errx(1, "focus_mode");
5784 case SWM_S_SPAWN_ORDER
:
5785 if (!strcmp(value
, "first"))
5786 spawn_position
= SWM_STACK_BOTTOM
;
5787 else if (!strcmp(value
, "last"))
5788 spawn_position
= SWM_STACK_TOP
;
5789 else if (!strcmp(value
, "next"))
5790 spawn_position
= SWM_STACK_ABOVE
;
5791 else if (!strcmp(value
, "previous"))
5792 spawn_position
= SWM_STACK_BELOW
;
5794 errx(1, "spawn_position");
5796 case SWM_S_SPAWN_TERM
:
5797 setconfspawn("term", value
, 0);
5798 setconfspawn("spawn_term", value
, 0);
5802 case SWM_S_SS_ENABLED
:
5803 ss_enabled
= atoi(value
);
5805 case SWM_S_STACK_ENABLED
:
5806 stack_enabled
= atoi(value
);
5808 case SWM_S_TERM_WIDTH
:
5809 term_width
= atoi(value
);
5813 case SWM_S_TITLE_CLASS_ENABLED
:
5814 title_class_enabled
= atoi(value
);
5816 case SWM_S_TITLE_NAME_ENABLED
:
5817 title_name_enabled
= atoi(value
);
5819 case SWM_S_URGENT_ENABLED
:
5820 urgent_enabled
= atoi(value
);
5822 case SWM_S_VERBOSE_LAYOUT
:
5823 verbose_layout
= atoi(value
);
5824 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5826 layouts
[i
].l_string
= fancy_stacker
;
5828 layouts
[i
].l_string
= plain_stacker
;
5831 case SWM_S_WINDOW_NAME_ENABLED
:
5832 window_name_enabled
= atoi(value
);
5834 case SWM_S_WORKSPACE_LIMIT
:
5835 workspace_limit
= atoi(value
);
5836 if (workspace_limit
> SWM_WS_MAX
)
5837 workspace_limit
= SWM_WS_MAX
;
5838 else if (workspace_limit
< 1)
5839 workspace_limit
= 1;
5848 setconfmodkey(char *selector
, char *value
, int flags
)
5850 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5851 update_modkey(Mod1Mask
);
5852 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5853 update_modkey(Mod2Mask
);
5854 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5855 update_modkey(Mod3Mask
);
5856 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5857 update_modkey(Mod4Mask
);
5864 setconfcolor(char *selector
, char *value
, int flags
)
5866 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5871 setconfregion(char *selector
, char *value
, int flags
)
5873 custom_region(value
);
5878 setautorun(char *selector
, char *value
, int flags
)
5888 if (getenv("SWM_STARTED"))
5892 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5893 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5895 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5896 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5899 * This is a little intricate
5901 * If the pid already exists we simply reuse it because it means it was
5902 * used before AND not claimed by manage_window. We get away with
5903 * altering it in the parent after INSERT because this can not be a race
5906 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5909 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5911 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5912 err(1, "setautorun: realloc");
5913 a
.argv
[argc
- 1] = ap
;
5916 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5917 err(1, "setautorun: realloc");
5918 a
.argv
[argc
] = NULL
;
5920 if ((pid
= fork()) == 0) {
5921 spawn(ws_id
, &a
, 1);
5930 p
= calloc(1, sizeof *p
);
5933 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5943 setlayout(char *selector
, char *value
, int flags
)
5945 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5946 int st
= SWM_V_STACK
, num_screens
;
5948 struct workspace
*ws
;
5950 if (getenv("SWM_STARTED"))
5954 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5955 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5956 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5957 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5960 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5961 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5963 if (!strcasecmp(s
, "vertical"))
5965 else if (!strcasecmp(s
, "vertical_flip")) {
5968 } else if (!strcasecmp(s
, "horizontal"))
5970 else if (!strcasecmp(s
, "horizontal_flip")) {
5973 } else if (!strcasecmp(s
, "fullscreen"))
5976 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5977 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5980 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5981 for (i
= 0; i
< num_screens
; i
++) {
5982 ws
= (struct workspace
*)&screens
[i
].ws
;
5983 ws
[ws_id
].cur_layout
= &layouts
[st
];
5985 ws
[ws_id
].always_raise
= raise
;
5986 if (st
== SWM_MAX_STACK
)
5990 for (x
= 0; x
< abs(mg
); x
++) {
5991 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5992 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5993 SWM_ARG_ID_MASTERSHRINK
);
5997 for (x
= 0; x
< abs(ma
); x
++) {
5998 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5999 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6000 SWM_ARG_ID_MASTERDEL
);
6004 for (x
= 0; x
< abs(si
); x
++) {
6005 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6006 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6007 SWM_ARG_ID_STACKDEC
);
6012 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6013 SWM_ARG_ID_FLIPLAYOUT
);
6021 /* config options */
6022 struct config_option
{
6024 int (*func
)(char*, char*, int);
6027 struct config_option configopt
[] = {
6028 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6029 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6030 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6031 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6032 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6033 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6034 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6035 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6036 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6037 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6038 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6039 { "keyboard_mapping", setkeymapping
, 0 },
6040 { "bind", setconfbinding
, 0 },
6041 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6042 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6043 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6044 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6045 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6046 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6047 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6048 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6049 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6050 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6051 { "modkey", setconfmodkey
, 0 },
6052 { "program", setconfspawn
, 0 },
6053 { "quirk", setconfquirk
, 0 },
6054 { "region", setconfregion
, 0 },
6055 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6056 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6057 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6058 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6059 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6060 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6061 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6062 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6063 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6064 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6065 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6066 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6067 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6068 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6069 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6070 { "autorun", setautorun
, 0 },
6071 { "layout", setlayout
, 0 },
6076 conf_load(char *filename
, int keymapping
)
6079 char *line
, *cp
, *optsub
, *optval
;
6080 size_t linelen
, lineno
= 0;
6081 int wordlen
, i
, optind
;
6082 struct config_option
*opt
;
6084 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6086 if (filename
== NULL
) {
6087 warnx("conf_load: no filename");
6090 if ((config
= fopen(filename
, "r")) == NULL
) {
6091 warn("conf_load: fopen: %s", filename
);
6095 while (!feof(config
)) {
6096 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6099 err(1, "%s", filename
);
6104 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6105 if (cp
[0] == '\0') {
6110 /* get config option */
6111 wordlen
= strcspn(cp
, "=[ \t\n");
6113 warnx("%s: line %zd: no option found",
6118 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6119 opt
= &configopt
[i
];
6120 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6121 strlen(opt
->optname
) == wordlen
) {
6127 warnx("%s: line %zd: unknown option %.*s",
6128 filename
, lineno
, wordlen
, cp
);
6131 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6132 warnx("%s: line %zd: invalid option %.*s",
6133 filename
, lineno
, wordlen
, cp
);
6137 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6138 /* get [selector] if any */
6142 wordlen
= strcspn(cp
, "]");
6145 warnx("%s: line %zd: syntax error",
6150 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6152 warnx("%s: line %zd: unable to allocate"
6153 "memory for selector", filename
,
6159 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6161 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6163 optval
= strdup(cp
);
6164 /* call function to deal with it all */
6165 if (configopt
[optind
].func(optsub
, optval
,
6166 configopt
[optind
].funcflags
) != 0)
6167 errx(1, "%s: line %zd: invalid data for %s",
6168 filename
, lineno
, configopt
[optind
].optname
);
6175 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6182 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6188 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6190 struct ws_win
*parent
, *w
;
6191 struct swm_region
*r
;
6192 struct workspace
*ws
;
6193 xcb_icccm_wm_hints_t wmh
;
6195 parent
= find_window(win
->transient
);
6197 parent
->child_trans
= win
;
6199 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6200 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6202 r
= root_to_region(win
->wa
->root
);
6204 /* parent doen't exist in our window list */
6205 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6206 if (xcb_icccm_get_wm_hints_reply(conn
,
6207 xcb_icccm_get_wm_hints(conn
, w
->id
),
6209 warnx("can't get hints for 0x%x", w
->id
);
6213 if (win
->hints
.window_group
!= wmh
.window_group
)
6216 w
->child_trans
= win
;
6217 win
->transient
= w
->id
;
6219 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6220 "transient to 0x%x\n", win
->transient
);
6227 window_get_pid(xcb_window_t win
)
6232 xcb_get_property_cookie_t pc
;
6233 xcb_get_property_reply_t
*pr
;
6235 apid
= get_atom_from_string("_NET_WM_PID");
6236 if (apid
== XCB_ATOM_NONE
)
6239 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6240 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6243 if (pr
->type
!= XCB_ATOM_CARDINAL
)
6246 ret
= *(long *)xcb_get_property_value(pr
);
6252 apid
= get_atom_from_string("_SWM_PID");
6253 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6255 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6258 if (pr
->type
!= XCB_ATOM_STRING
)
6261 ret
= strtonum(xcb_get_property_value(pr
), 0, UINT_MAX
, &errstr
);
6268 manage_window(xcb_window_t id
)
6270 xcb_window_t trans
= XCB_WINDOW_NONE
;
6271 struct workspace
*ws
;
6272 struct ws_win
*win
, *ww
;
6273 int i
, ws_idx
, border_me
= 0;
6274 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
6275 char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6277 struct swm_region
*r
;
6281 uint32_t event_mask
;
6283 xcb_get_property_reply_t
*gpr
;
6284 xcb_icccm_get_wm_protocols_reply_t wpr
;
6286 if ((win
= find_window(id
)) != NULL
)
6287 return (win
); /* already being managed */
6289 /* see if we are on the unmanaged list */
6290 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6291 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6292 "window: 0x%x\n", win
->id
);
6293 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6295 set_child_transient(win
, &trans
);
6297 if (trans
&& (ww
= find_window(trans
)))
6298 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6299 else if ((ww
= win
->ws
->focus
) &&
6300 spawn_position
== SWM_STACK_ABOVE
)
6301 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6303 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6304 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6305 else switch (spawn_position
) {
6308 case SWM_STACK_ABOVE
:
6309 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6311 case SWM_STACK_BOTTOM
:
6312 case SWM_STACK_BELOW
:
6313 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6316 ewmh_update_actions(win
);
6320 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6321 err(1, "manage_window: calloc: failed to allocate memory for "
6327 /* see if we need to override the workspace */
6328 p
= find_pid(window_get_pid(id
));
6330 /* Get all the window data in one shot */
6331 ws_idx_atom
= get_atom_from_string("_SWM_WS");
6333 gpr
= xcb_get_property_reply(conn
,
6334 xcb_get_property(conn
, False
, id
, ws_idx_atom
,
6335 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6338 proplen
= xcb_get_property_value_length(gpr
);
6340 prop
= malloc(proplen
+ 1);
6343 xcb_get_property_value(gpr
),
6345 prop
[proplen
] = '\0';
6351 win
->wa
= xcb_get_geometry_reply(conn
,
6352 xcb_get_geometry(conn
, id
),
6354 xcb_icccm_get_wm_normal_hints_reply(conn
,
6355 xcb_icccm_get_wm_normal_hints(conn
, id
),
6357 xcb_icccm_get_wm_hints_reply(conn
,
6358 xcb_icccm_get_wm_hints(conn
, id
),
6360 xcb_icccm_get_wm_transient_for_reply(conn
,
6361 xcb_icccm_get_wm_transient_for(conn
, id
),
6364 win
->transient
= trans
;
6365 set_child_transient(win
, &trans
);
6366 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6367 "transient: 0x%x\n", win
->id
, win
->transient
);
6370 prot
= get_atom_from_string("WM_PROTOCOLS");
6372 /* get supported protocols */
6373 if (xcb_icccm_get_wm_protocols_reply(conn
,
6374 xcb_icccm_get_wm_protocols(conn
, id
, prot
),
6376 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6377 if (wpr
.atoms
[i
] == takefocus
)
6378 win
->take_focus
= 1;
6379 if (wpr
.atoms
[i
] == adelete
)
6380 win
->can_delete
= 1;
6382 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6385 win
->iconic
= get_iconic(win
);
6388 * Figure out where to put the window. If it was previously assigned to
6389 * a workspace (either by spawn() or manually moving), and isn't
6390 * transient, * put it in the same workspace
6392 r
= root_to_region(win
->wa
->root
);
6394 ws
= &r
->s
->ws
[p
->ws
];
6395 TAILQ_REMOVE(&pidlist
, p
, entry
);
6398 } else if (prop
&& win
->transient
== 0) {
6399 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6400 ws_idx
= strtonum(prop
, 0, workspace_limit
- 1,
6403 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6406 ws
= &r
->s
->ws
[ws_idx
];
6409 /* this should launch transients in the same ws as parent */
6411 if ((ww
= find_window(trans
)) != NULL
)
6417 warnx("manage_window: fix this "
6423 /* set up the window layout */
6426 win
->s
= r
->s
; /* this never changes */
6427 if (trans
&& (ww
= find_window(trans
)))
6428 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6429 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6430 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6432 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6433 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6434 else switch (spawn_position
) {
6437 case SWM_STACK_ABOVE
:
6438 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6440 case SWM_STACK_BOTTOM
:
6441 case SWM_STACK_BELOW
:
6442 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6445 /* ignore window border if there is one. */
6446 WIDTH(win
) = win
->wa
->width
;
6447 HEIGHT(win
) = win
->wa
->height
;
6448 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6449 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6451 win
->g_floatvalid
= 0;
6452 win
->floatmaxed
= 0;
6453 win
->ewmh_flags
= 0;
6455 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6456 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6457 HEIGHT(win
), ws
->idx
);
6459 constrain_window(win
, r
, 0);
6461 /* Set window properties so we can remember this after reincarnation */
6462 if (ws_idx_atom
&& prop
== NULL
&&
6463 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6465 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6467 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6468 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6474 ewmh_autoquirk(win
);
6476 if (xcb_icccm_get_wm_class_reply(conn
,
6477 xcb_icccm_get_wm_class(conn
, win
->id
),
6479 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6480 win
->ch
.class_name
, win
->ch
.instance_name
);
6482 /* java is retarded so treat it special */
6483 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6488 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6489 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6490 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6491 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6492 "class: %s, name: %s\n", win
->ch
.class_name
,
6493 win
->ch
.instance_name
);
6494 if (qp
->quirk
& SWM_Q_FLOAT
) {
6498 win
->quirks
= qp
->quirk
;
6503 /* alter window position if quirky */
6504 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6505 win
->manual
= 1; /* don't center the quirky windows */
6506 if (bar_enabled
&& Y(win
) < bar_height
)
6507 Y(win
) = bar_height
;
6508 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6509 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6513 /* Reset font sizes (the bruteforce way; no default keybinding). */
6514 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6515 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6516 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6517 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6518 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6521 ewmh_get_win_state(win
);
6522 ewmh_update_actions(win
);
6523 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6528 X(win
) -= border_width
;
6529 Y(win
) -= border_width
;
6533 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6534 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6536 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6538 /* floaters need to be mapped if they are in the current workspace */
6539 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6540 map_window_raised(win
->id
);
6546 free_window(struct ws_win
*win
)
6548 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6553 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6558 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6563 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6566 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6570 unmanage_window(struct ws_win
*win
)
6572 struct ws_win
*parent
;
6573 xcb_screen_t
*screen
;
6578 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6580 if (win
->transient
) {
6581 parent
= find_window(win
->transient
);
6583 parent
->child_trans
= NULL
;
6586 /* focus on root just in case */
6587 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6588 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6589 screen
->root
, XCB_CURRENT_TIME
);
6593 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6594 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6598 focus_magic(struct ws_win
*win
)
6600 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6603 /* if there are no windows clear the status-bar */
6608 if (win
->child_trans
) {
6609 /* win = parent & has a transient so focus on that */
6611 focus_win(win
->child_trans
);
6612 if (win
->child_trans
->take_focus
)
6613 client_msg(win
, takefocus
);
6615 /* make sure transient hasn't disappeared */
6616 if (validate_win(win
->child_trans
) == 0) {
6617 focus_win(win
->child_trans
);
6618 if (win
->child_trans
->take_focus
)
6619 client_msg(win
->child_trans
, takefocus
);
6621 win
->child_trans
= NULL
;
6623 if (win
->take_focus
)
6624 client_msg(win
, takefocus
);
6630 if (win
->take_focus
)
6631 client_msg(win
, takefocus
);
6636 expose(xcb_expose_event_t
*e
)
6638 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6642 keypress(xcb_key_press_event_t
*e
)
6646 struct swm_region
*r
;
6648 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)e
->detail
, 0, 0);
6650 DNPRINTF(SWM_D_EVENT
, "keypress: %u\n", e
->detail
);
6652 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6654 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6657 r
= root_to_region(e
->root
);
6658 if (kp
->funcid
== kf_spawn_custom
)
6659 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6661 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6665 buttonpress(xcb_button_press_event_t
*e
)
6670 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x\n", e
->event
);
6672 if ((win
= find_window(e
->event
)) == NULL
)
6676 action
= client_click
;
6678 for (i
= 0; i
< LENGTH(buttons
); i
++)
6679 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6680 buttons
[i
].button
== e
->detail
&&
6681 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6682 buttons
[i
].func(win
, &buttons
[i
].args
);
6686 configurerequest(xcb_configure_request_event_t
*e
)
6691 uint32_t wc
[7] = {0};
6693 if ((win
= find_window(e
->window
)) == NULL
)
6694 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6698 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6699 mask
|= XCB_CONFIG_WINDOW_X
;
6702 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6703 mask
|= XCB_CONFIG_WINDOW_Y
;
6706 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6707 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6710 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6711 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6712 wc
[i
++] = e
->height
;
6714 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6715 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6716 wc
[i
++] = e
->border_width
;
6718 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6719 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6720 wc
[i
++] = e
->sibling
;
6722 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6723 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6724 wc
[i
++] = e
->stack_mode
;
6727 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6728 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e
->window
,
6729 YESNO(new), wc
[0], wc
[1], wc
[2], wc
[3]);
6731 xcb_configure_window(conn
, e
->window
, mask
, wc
);
6732 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6733 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
6734 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
6735 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
6736 win
->g_float
.w
= e
->width
;
6737 win
->g_float
.h
= e
->height
;
6738 win
->g_floatvalid
= 1;
6740 if (win
->floating
) {
6741 win
->g
= win
->g_float
;
6742 win
->g
.x
+= X(win
->ws
->r
);
6743 win
->g
.y
+= Y(win
->ws
->r
);
6754 configurenotify(xcb_configure_notify_event_t
*e
)
6758 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
6761 win
= find_window(e
->window
);
6763 xcb_icccm_get_wm_normal_hints_reply(conn
,
6764 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6773 destroynotify(xcb_destroy_notify_event_t
*e
)
6777 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
6779 if ((win
= find_window(e
->window
)) == NULL
) {
6780 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6786 /* make sure we focus on something */
6789 unmanage_window(win
);
6795 enternotify(xcb_enter_notify_event_t
*e
)
6800 Window focus_return
;
6801 int revert_to_return
;
6803 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %d, detail: "
6804 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6805 "state: %d\n", e
->event
, e
->mode
, e
->detail
, e
->root
,
6806 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
6808 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
6809 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6814 switch (focus_mode
) {
6815 case SWM_FOCUS_DEFAULT
:
6817 case SWM_FOCUS_FOLLOW
:
6819 case SWM_FOCUS_SYNERGY
:
6822 * all these checks need to be in this order because the
6823 * XCheckTypedWindowEvent relies on weeding out the previous events
6825 * making this code an option would enable a follow mouse for focus
6830 * state is set when we are switching workspaces and focus is set when
6831 * the window or a subwindow already has focus (occurs during restart).
6833 * Only honor the focus flag if last_focus_event is not FocusOut,
6834 * this allows spectrwm to continue to control focus when another
6835 * program is also playing with it.
6837 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6838 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6843 * happens when a window is created or destroyed and the border
6844 * crosses the mouse pointer and when switching ws
6846 * we need the subwindow test to see if we came from root in order
6847 * to give focus to floaters
6849 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6850 ev
->subwindow
== 0) {
6851 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6855 /* this window already has focus */
6856 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6857 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6861 /* this window is being deleted or moved to another ws */
6862 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6864 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6865 XPutBackEvent(display
, &cne
);
6869 if ((win
= find_window(ev
->window
)) == NULL
) {
6870 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6875 * In fullstack kill all enters unless they come from a different ws
6876 * (i.e. another region) or focus has been grabbed externally.
6878 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6879 last_focus_event
!= FocusOut
) {
6880 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6881 if ((w
= find_window(focus_return
)) == NULL
||
6883 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6891 if ((win
= find_window(e
->event
)) == NULL
) {
6892 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6899 /* lets us use one switch statement for arbitrary mode/detail combinations */
6900 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6903 mapnotify(xcb_map_notify_event_t
*e
)
6907 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
6909 win
= manage_window(e
->window
);
6911 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
6914 * focus_win can only set input focus on a mapped window.
6915 * make sure the window really has focus since it is just being mapped.
6917 if (win
->ws
->focus
== win
)
6922 mappingnotify(xcb_mapping_notify_event_t
*e
)
6924 xcb_refresh_keyboard_mapping(syms
, e
);
6926 if (e
->request
== XCB_MAPPING_KEYBOARD
)
6931 maprequest(xcb_map_request_event_t
*e
)
6934 struct swm_region
*r
;
6935 xcb_get_window_attributes_reply_t
*war
;
6937 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
6940 war
= xcb_get_window_attributes_reply(conn
,
6941 xcb_get_window_attributes(conn
, e
->window
),
6945 if (war
->override_redirect
) {
6951 win
= manage_window(e
->window
);
6953 return; /* can't happen */
6958 /* make new win focused */
6959 r
= root_to_region(win
->wa
->root
);
6960 if (win
->ws
== r
->ws
)
6965 propertynotify(xcb_property_notify_event_t
*e
)
6971 xcb_get_atom_name_reply_t
*r
;
6973 r
= xcb_get_atom_name_reply(conn
,
6974 xcb_get_atom_name(conn
, e
->atom
),
6977 len
= xcb_get_atom_name_name_length(r
);
6979 name
= malloc(len
+ 1);
6981 memcpy(name
, xcb_get_atom_name_name(r
), len
);
6984 DNPRINTF(SWM_D_EVENT
,
6985 "propertynotify: window: 0x%x, atom: %s\n",
6994 win
= find_window(e
->window
);
6998 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
6999 update_iconic(win
, 0);
7000 map_window_raised(win
->id
);
7008 case XCB_ATOM_WM_NORMAL_HINTS
:
7009 xcb_icccm_get_wm_normal_hints(conn
,
7010 xcb_iccom_get_wm_normal_hints(conn
, win
->id
),
7012 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
7013 if (win
->sh
.flags
& XCB_SIZE_HINT_P_MIN_SIZE
) {
7014 WIDTH(win
) = win
->sh
.min_width
;
7015 HEIGHT(win
) = win
->sh
.min_height
;
7016 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
7018 XMoveResizeWindow(display
, win
->id
,
7019 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
7021 case XCB_ATOM_WM_CLASS
:
7022 case XCB_ATOM_WM_NAME
:
7031 unmapnotify(xcb_unmap_notify_event_t
*e
)
7035 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7037 /* determine if we need to help unmanage this window */
7038 win
= find_window(e
->window
);
7042 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7043 unmanage_window(win
);
7046 /* resend unmap because we ated it */
7047 xcb_unmap_window(conn
, e
->window
);
7052 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7055 struct swm_region
*r
;
7057 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7060 if (e
->state
== XCB_VISIBILITY_UNOBSCURED
) {
7061 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7062 for (i
= 0; i
< num_screens
; i
++)
7063 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7064 if (e
->window
== WINID(r
->bar
))
7070 clientmessage(xcb_client_message_event_t
*e
)
7074 win
= find_window(e
->window
);
7079 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, type: %u\n",
7080 e
->window
, e
->response_type
);
7082 if (e
->response_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7083 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7086 if (e
->response_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7087 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7088 if (win
->can_delete
)
7089 client_msg(win
, adelete
);
7091 xcb_kill_client(conn
, win
->id
);
7093 if (e
->response_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7094 DNPRINTF(SWM_D_EVENT
,
7095 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7096 if (win
->floating
) {
7097 if (e
->data
.data32
[0] & (1<<8)) /* x */
7098 X(win
) = e
->data
.data32
[1];
7099 if (e
->data
.data32
[0] & (1<<9)) /* y */
7100 Y(win
) = e
->data
.data32
[2];
7101 if (e
->data
.data32
[0] & (1<<10)) /* width */
7102 WIDTH(win
) = e
->data
.data32
[3];
7103 if (e
->data
.data32
[0] & (1<<11)) /* height */
7104 HEIGHT(win
) = e
->data
.data32
[4];
7109 /* TODO: Change stack sizes */
7110 /* notify no change was made. */
7111 config_win(win
, NULL
);
7114 if (e
->response_type
== ewmh
[_NET_WM_STATE
].atom
) {
7115 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7116 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7117 if (e
->data
.data32
[2])
7118 ewmh_update_win_state(win
, e
->data
.data32
[2],
7126 xerror_start(Display
*d
, XErrorEvent
*ee
)
7133 xerror(Display
*d
, XErrorEvent
*ee
)
7135 /* warnx("error: %p %p", display, ee); */
7143 xerrorxlib
= XSetErrorHandler(xerror_start
);
7145 /* this causes an error if some other window manager is running */
7146 XSelectInput(display
, DefaultRootWindow(display
),
7147 SubstructureRedirectMask
);
7152 XSetErrorHandler(xerror
);
7158 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7160 struct swm_region
*r
, *n
;
7161 struct workspace
*ws
= NULL
;
7164 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7165 s
->idx
, w
, h
, x
, y
);
7167 /* remove any conflicting regions */
7168 n
= TAILQ_FIRST(&s
->rl
);
7171 n
= TAILQ_NEXT(r
, entry
);
7172 if (X(r
) < (x
+ w
) &&
7173 (X(r
) + WIDTH(r
)) > x
&&
7175 (Y(r
) + HEIGHT(r
)) > y
) {
7176 if (r
->ws
->r
!= NULL
)
7177 r
->ws
->old_r
= r
->ws
->r
;
7180 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7181 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7185 /* search old regions for one to reuse */
7187 /* size + location match */
7188 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7189 if (X(r
) == x
&& Y(r
) == y
&&
7190 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7194 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7195 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7199 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7200 /* try to use old region's workspace */
7201 if (r
->ws
->r
== NULL
)
7204 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7205 err(1, "new_region: calloc: failed to allocate memory "
7208 /* if we don't have a workspace already, find one */
7210 for (i
= 0; i
< workspace_limit
; i
++)
7211 if (s
->ws
[i
].r
== NULL
) {
7218 errx(1, "new_region: no free workspaces");
7229 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7235 #ifdef SWM_XRR_HAS_CRTC
7238 #endif /* SWM_XRR_HAS_CRTC */
7239 struct swm_region
*r
;
7241 xcb_randr_get_screen_resources_current_cookie_t src
;
7242 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7243 xcb_randr_get_crtc_info_cookie_t cic
;
7244 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7245 xcb_randr_crtc_t
*crtc
;
7246 xcb_screen_t
*screen
= get_screen(i
);
7248 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7249 if (i
>= num_screens
)
7250 errx(1, "scan_xrandr: invalid screen");
7252 /* remove any old regions */
7253 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7254 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7256 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7257 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7261 /* map virtual screens onto physical screens */
7262 #ifdef SWM_XRR_HAS_CRTC
7263 if (xrandr_support
) {
7264 src
= xcb_randr_get_screen_resources_current(conn
,
7266 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7269 new_region(&screens
[i
], 0, 0,
7270 screen
->width_in_pixels
,
7271 screen
->height_in_pixels
);
7274 ncrtc
= srr
->num_crtcs
;
7275 for (c
= 0; c
< ncrtc
; c
++) {
7276 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7277 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7279 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7282 if (cir
->num_outputs
== 0) {
7288 new_region(&screens
[i
], 0, 0,
7289 screen
->width_in_pixels
,
7290 screen
->height_in_pixels
);
7292 new_region(&screens
[i
],
7293 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7298 #endif /* SWM_XRR_HAS_CRTC */
7300 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7301 screen
->height_in_pixels
);
7306 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7308 struct swm_region
*r
;
7311 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7313 if (e
->rotation
& (XCB_RANDR_ROTATION_ROTATE_90
7314 | XCB_RANDR_ROTATION_ROTATE_270
))
7315 xcb_randr_set_screen_size(conn
, e
->root
, e
->height
,
7316 e
->width
, e
->mheight
, e
->mwidth
);
7318 xcb_randr_set_screen_size(conn
, e
->root
, e
->width
,
7319 e
->height
, e
->mwidth
, e
->mheight
);
7321 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7322 /* silly event doesn't include the screen index */
7323 for (i
= 0; i
< num_screens
; i
++)
7324 if (screens
[i
].root
== e
->root
)
7326 if (i
>= num_screens
)
7327 errx(1, "screenchange: screen not found");
7329 /* brute force for now, just re-enumerate the regions */
7332 /* add bars to all regions */
7333 for (i
= 0; i
< num_screens
; i
++)
7334 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7342 xcb_window_t
*wins
= NULL
;
7344 int i
, j
, num_screens
;
7345 uint16_t state
, manage
;
7347 xcb_query_tree_cookie_t qtc
;
7348 xcb_query_tree_reply_t
*qtr
;
7349 xcb_get_window_attributes_cookie_t c
;
7350 xcb_get_window_attributes_reply_t
*r
;
7351 xcb_get_property_cookie_t pc
;
7353 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7354 for (i
= 0; i
< num_screens
; i
++) {
7355 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7356 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7359 wins
= xcb_query_tree_children(qtr
);
7360 no
= xcb_query_tree_children_length(qtr
);
7361 /* attach windows to a region */
7362 /* normal windows */
7363 for (j
= 0; j
< no
; j
++) {
7364 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7365 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7368 if (r
->override_redirect
) {
7373 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7374 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7380 state
= getstate(wins
[j
]);
7381 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7382 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7383 manage_window(wins
[j
]);
7386 /* transient windows */
7387 for (j
= 0; j
< no
; j
++) {
7388 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7389 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7392 if (r
->override_redirect
) {
7398 state
= getstate(wins
[j
]);
7399 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7400 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7401 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7402 &wins
[j
], NULL
) && manage
)
7403 manage_window(wins
[j
]);
7412 int i
, j
, k
, num_screens
;
7413 struct workspace
*ws
;
7414 uint32_t gcv
[1], wa
[1];
7415 const xcb_query_extension_reply_t
*qep
;
7416 xcb_cursor_t cursor
;
7417 xcb_font_t cursor_font
;
7418 xcb_randr_query_version_cookie_t c
;
7419 xcb_randr_query_version_reply_t
*r
;
7421 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7422 if ((screens
= calloc(num_screens
,
7423 sizeof(struct swm_screen
))) == NULL
)
7424 err(1, "setup_screens: calloc: failed to allocate memory for "
7427 /* initial Xrandr setup */
7428 xrandr_support
= False
;
7429 c
= xcb_randr_query_version(conn
, True
, True
);
7430 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7432 if (r
->major_version
>= 1)
7433 xrandr_support
= True
;
7436 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7437 xrandr_eventbase
= qep
->first_event
;
7439 cursor_font
= xcb_generate_id(conn
);
7440 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7442 cursor
= xcb_generate_id(conn
);
7443 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7444 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7447 /* map physical screens */
7448 for (i
= 0; i
< num_screens
; i
++) {
7449 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7451 TAILQ_INIT(&screens
[i
].rl
);
7452 TAILQ_INIT(&screens
[i
].orl
);
7453 screens
[i
].root
= RootWindow(display
, i
);
7455 /* set default colors */
7456 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7457 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7458 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7459 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7460 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7462 /* create graphics context on screen */
7463 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7465 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7466 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7468 /* set default cursor */
7469 xcb_change_window_attributes(conn
, screens
[i
].root
,
7472 /* init all workspaces */
7473 /* XXX these should be dynamically allocated too */
7474 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7475 ws
= &screens
[i
].ws
[j
];
7481 TAILQ_INIT(&ws
->winlist
);
7482 TAILQ_INIT(&ws
->unmanagedlist
);
7484 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7485 if (layouts
[k
].l_config
!= NULL
)
7486 layouts
[k
].l_config(ws
,
7487 SWM_ARG_ID_STACKINIT
);
7488 ws
->cur_layout
= &layouts
[0];
7489 ws
->cur_layout
->l_string(ws
);
7495 xcb_randr_select_input(conn
, screens
[i
].root
,
7496 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7498 xcb_free_cursor(conn
, cursor
);
7499 xcb_close_font(conn
, cursor_font
);
7505 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7506 err(1, "setup_globals: strdup: failed to allocate memory.");
7508 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7509 err(1, "setup_globals: strdup: failed to allocate memory.");
7516 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7517 xcb_window_t root
, win
;
7520 /* work around sun jdk bugs, code from wmname */
7521 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7522 netwmname
= get_atom_from_string("_NET_WM_NAME");
7523 utf8_string
= get_atom_from_string("UTF8_STRING");
7525 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7526 for (i
= 0; i
< num_screens
; i
++) {
7527 root
= screens
[i
].root
;
7529 win
= xcb_generate_id(conn
);
7530 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7531 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7532 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7533 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7534 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7536 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7537 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7538 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7539 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7540 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7541 netwmname
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7546 event_error(xcb_generic_error_t
*e
)
7548 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7549 "resource_id: %u, minor_code: %u\n",
7550 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7551 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7552 e
->sequence
, e
->resource_id
, e
->minor_code
);
7556 event_handle(xcb_generic_event_t
*evt
)
7558 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7560 #define EVENT(type, callback) case type: callback((void *)evt); return
7561 EVENT(0, event_error
);
7562 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7563 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7564 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7565 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7566 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7567 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7568 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7569 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7570 /*EVENT(XCB_CREATE_NOTIFY, );*/
7571 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7572 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7573 EVENT(XCB_EXPOSE
, expose
);
7574 /*EVENT(XCB_FOCUS_IN, );*/
7575 /*EVENT(XCB_FOCUS_OUT, );*/
7576 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7577 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7578 EVENT(XCB_KEY_PRESS
, keypress
);
7579 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7580 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7581 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7582 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7583 EVENT(XCB_MAP_REQUEST
, maprequest
);
7584 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7585 /*EVENT(XCB_MOTION_NOTIFY, );*/
7586 /*EVENT(XCB_NO_EXPOSURE, );*/
7587 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7588 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7589 /*EVENT(XCB_RESIZE_REQUEST, );*/
7590 /*EVENT(XCB_SELECTION_CLEAR, );*/
7591 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7592 /*EVENT(XCB_SELECTION_REQUEST, );*/
7593 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7594 EVENT(XCB_VISIBILITY_NOTIFY
, visibilitynotify
);
7597 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7598 screenchange((void *)evt
);
7602 main(int argc
, char *argv
[])
7604 struct swm_region
*r
, *rr
;
7605 struct ws_win
*winfocus
= NULL
;
7608 char conf
[PATH_MAX
], *cfile
= NULL
;
7610 int xfd
, i
, num_screens
;
7612 struct sigaction sact
;
7613 xcb_generic_event_t
*evt
;
7615 time_started
= time(NULL
);
7618 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7619 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7621 warnx("no locale support");
7623 if (!X_HAVE_UTF8_STRING
)
7624 warnx("no UTF-8 support");
7626 if (!(display
= XOpenDisplay(0)))
7627 errx(1, "can not open display");
7629 /* handle some signals */
7630 bzero(&sact
, sizeof(sact
));
7631 sigemptyset(&sact
.sa_mask
);
7633 sact
.sa_handler
= sighdlr
;
7634 sigaction(SIGINT
, &sact
, NULL
);
7635 sigaction(SIGQUIT
, &sact
, NULL
);
7636 sigaction(SIGTERM
, &sact
, NULL
);
7637 sigaction(SIGHUP
, &sact
, NULL
);
7639 sact
.sa_handler
= sighdlr
;
7640 sact
.sa_flags
= SA_NOCLDSTOP
;
7641 sigaction(SIGCHLD
, &sact
, NULL
);
7643 conn
= XGetXCBConnection(display
);
7644 if (xcb_connection_has_error(conn
))
7645 errx(1, "can not get XCB connection");
7647 xfd
= xcb_get_file_descriptor(conn
);
7648 syms
= xcb_key_symbols_alloc(conn
);
7650 errx(1, "unable to allocate key symbols");
7652 astate
= get_atom_from_string("WM_STATE");
7653 aprot
= get_atom_from_string("WM_PROTOCOLS");
7654 adelete
= get_atom_from_string("WM_DELETE_WINDOW");
7655 takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7656 a_wmname
= get_atom_from_string("WM_NAME");
7657 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7658 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7659 a_string
= get_atom_from_string("STRING");
7660 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7662 /* look for local and global conf file */
7663 pwd
= getpwuid(getuid());
7665 errx(1, "invalid user: %d", getuid());
7667 xcb_grab_server(conn
);
7670 /* flush all events */
7671 while ((evt
= xcb_poll_for_event(conn
))) {
7672 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7679 errx(1, "other wm running");
7690 for (i
= 0; ; i
++) {
7695 snprintf(conf
, sizeof conf
, "%s/.%s",
7696 pwd
->pw_dir
, SWM_CONF_FILE
);
7700 snprintf(conf
, sizeof conf
, "/etc/%s",
7705 snprintf(conf
, sizeof conf
, "%s/.%s",
7706 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7710 snprintf(conf
, sizeof conf
, "/etc/%s",
7717 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7718 if (S_ISREG(sb
.st_mode
)) {
7725 /* load conf (if any) */
7727 conf_load(cfile
, SWM_CONF_DEFAULT
);
7730 /* set some values to work around bad programs */
7732 /* grab existing windows (before we build the bars) */
7735 if (getenv("SWM_STARTED") == NULL
)
7736 setenv("SWM_STARTED", "YES", 1);
7738 /* setup all bars */
7739 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7740 for (i
= 0; i
< num_screens
; i
++)
7741 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7742 if (winfocus
== NULL
)
7743 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7752 xcb_ungrab_server(conn
);
7756 while ((evt
= xcb_poll_for_event(conn
))) {
7757 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7758 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7759 XCB_EVENT_RESPONSE_TYPE(evt
));
7766 /* if we are being restarted go focus on first window */
7768 rr
= winfocus
->ws
->r
;
7770 /* not a visible window */
7774 /* move pointer to first screen if multi screen */
7775 if (num_screens
> 1 || outputs
> 1)
7776 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7777 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7778 Y(rr
) + (bar_enabled
? bar_height
: 0));
7780 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7790 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7792 DNPRINTF(SWM_D_MISC
, "select failed");
7793 if (restart_wm
== 1)
7794 restart(NULL
, NULL
);
7795 if (search_resp
== 1)
7808 for (i
= 0; i
< num_screens
; ++i
)
7809 if (screens
[i
].bar_gc
!= 0)
7810 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7811 XFreeFontSet(display
, bar_fs
);
7813 xcb_key_symbols_free(syms
);
7814 xcb_disconnect(conn
);