2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <X11/Xproto.h>
102 #include <X11/Xutil.h>
103 #include <X11/extensions/Xrandr.h>
104 #include <X11/extensions/XTest.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr
= SPECTRWM_BUILDSTR
;
115 static const char *buildstr
= SPECTRWM_VERSION
;
119 # error XRandR versions less than 1.0 are not supported
124 #define SWM_XRR_HAS_CRTC
128 #if defined(__OpenBSD__)
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
133 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
134 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
135 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
136 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
137 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
138 #define xcb_icccm_get_wm_class xcb_get_wm_class
139 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
140 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
141 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
142 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
143 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
144 #define xcb_icccm_get_wm_name xcb_get_wm_name
145 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
146 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
147 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
148 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
149 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
150 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
151 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
152 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
153 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
154 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
159 #define DPRINTF(x...) do { \
161 fprintf(stderr, x); \
163 #define DNPRINTF(n,x...) do { \
164 if (swm_debug & n) { \
165 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
166 fprintf(stderr, x); \
169 #define SWM_D_MISC 0x0001
170 #define SWM_D_EVENT 0x0002
171 #define SWM_D_WS 0x0004
172 #define SWM_D_FOCUS 0x0008
173 #define SWM_D_MOVE 0x0010
174 #define SWM_D_STACK 0x0020
175 #define SWM_D_MOUSE 0x0040
176 #define SWM_D_PROP 0x0080
177 #define SWM_D_CLASS 0x0100
178 #define SWM_D_KEY 0x0200
179 #define SWM_D_QUIRK 0x0400
180 #define SWM_D_SPAWN 0x0800
181 #define SWM_D_EVENTQ 0x1000
182 #define SWM_D_CONF 0x2000
183 #define SWM_D_BAR 0x4000
185 u_int32_t swm_debug
= 0
203 #define DPRINTF(x...)
204 #define DNPRINTF(n,x...)
207 #define LENGTH(x) (sizeof x / sizeof x[0])
208 #define MODKEY Mod1Mask
209 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
210 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
211 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
212 #define SWM_PROPLEN (16)
213 #define SWM_FUNCNAME_LEN (32)
214 #define SWM_KEYS_LEN (255)
215 #define SWM_QUIRK_LEN (64)
216 #define X(r) (r)->g.x
217 #define Y(r) (r)->g.y
218 #define WIDTH(r) (r)->g.w
219 #define HEIGHT(r) (r)->g.h
220 #define BORDER(w) (w->bordered ? border_width : 0)
221 #define MAX_X(r) ((r)->g.x + (r)->g.w)
222 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
223 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
224 #define SH_MIN_W(w) (w)->sh.min_width
225 #define SH_MIN_H(w) (w)->sh.min_height
226 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
227 #define SH_MAX_W(w) (w)->sh.max_width
228 #define SH_MAX_H(w) (w)->sh.max_height
229 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
230 #define SH_INC_W(w) (w)->sh.width_inc
231 #define SH_INC_H(w) (w)->sh.height_inc
232 #define SWM_MAX_FONT_STEPS (3)
233 #define WINID(w) ((w) ? (w)->id : 0)
234 #define YESNO(x) ((x) ? "yes" : "no")
236 #define SWM_FOCUS_DEFAULT (0)
237 #define SWM_FOCUS_SYNERGY (1)
238 #define SWM_FOCUS_FOLLOW (2)
240 #define SWM_CONF_DEFAULT (0)
241 #define SWM_CONF_KEYMAPPING (1)
244 #define SWM_LIB "/usr/local/lib/libswmhack.so"
251 xcb_atom_t takefocus
;
253 xcb_atom_t a_netwmname
;
254 xcb_atom_t a_utf8_string
;
256 xcb_atom_t a_swm_iconic
;
257 volatile sig_atomic_t running
= 1;
258 volatile sig_atomic_t restart_wm
= 0;
260 /*int last_focus_event = FocusOut;*/
261 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
265 int xrandr_eventbase
;
266 unsigned int numlockmask
= 0;
268 xcb_connection_t
*conn
;
269 xcb_key_symbols_t
*syms
;
272 int cycle_visible
= 0;
274 int font_adjusted
= 0;
275 unsigned int mod_key
= MODKEY
;
278 struct swm_region
*search_r
;
279 int select_list_pipe
[2];
280 int select_resp_pipe
[2];
282 volatile sig_atomic_t search_resp
;
283 int search_resp_action
;
285 struct search_window
{
286 TAILQ_ENTRY(search_window
) entry
;
290 xcb_window_t indicator
;
292 TAILQ_HEAD(search_winlist
, search_window
);
293 struct search_winlist search_wl
;
298 SWM_SEARCH_UNICONIFY
,
299 SWM_SEARCH_NAME_WORKSPACE
,
300 SWM_SEARCH_SEARCH_WORKSPACE
,
301 SWM_SEARCH_SEARCH_WINDOW
304 #define SWM_STACK_TOP (0)
305 #define SWM_STACK_BOTTOM (1)
306 #define SWM_STACK_ABOVE (2)
307 #define SWM_STACK_BELOW (3)
310 double dialog_ratio
= 0.6;
312 #define SWM_BAR_MAX (256)
313 #define SWM_BAR_JUSTIFY_LEFT (0)
314 #define SWM_BAR_JUSTIFY_CENTER (1)
315 #define SWM_BAR_JUSTIFY_RIGHT (2)
316 #define SWM_BAR_OFFSET (4)
317 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
318 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
319 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
320 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
321 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
323 char *bar_argv
[] = { NULL
, NULL
};
325 unsigned char bar_ext
[SWM_BAR_MAX
];
326 char bar_vertext
[SWM_BAR_MAX
];
328 sig_atomic_t bar_alarm
= 0;
331 int bar_border_width
= 1;
332 int bar_at_bottom
= 0;
334 int bar_extra_running
= 0;
337 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
338 char *bar_format
= NULL
;
339 int stack_enabled
= 1;
340 int clock_enabled
= 1;
341 int urgent_enabled
= 0;
342 char *clock_format
= NULL
;
343 int title_name_enabled
= 0;
344 int title_class_enabled
= 0;
345 int window_name_enabled
= 0;
346 int focus_mode
= SWM_FOCUS_DEFAULT
;
347 int focus_close
= SWM_STACK_BELOW
;
348 int focus_close_wrap
= 1;
349 int focus_default
= SWM_STACK_TOP
;
350 int spawn_position
= SWM_STACK_TOP
;
351 int disable_border
= 0;
352 int border_width
= 1;
353 int verbose_layout
= 0;
357 XFontSetExtents
*bar_fs_extents
;
361 #define SWM_MENU_FN (2)
362 #define SWM_MENU_NB (4)
363 #define SWM_MENU_NF (6)
364 #define SWM_MENU_SB (8)
365 #define SWM_MENU_SF (10)
367 /* layout manager data */
368 struct swm_geometry
{
381 struct swm_geometry g
;
384 /* virtual "screens" */
386 TAILQ_ENTRY(swm_region
) entry
;
387 struct swm_geometry g
;
388 struct workspace
*ws
; /* current workspace on this region */
389 struct workspace
*ws_prior
; /* prior workspace on this region */
390 struct swm_screen
*s
; /* screen idx */
393 TAILQ_HEAD(swm_region_list
, swm_region
);
396 TAILQ_ENTRY(ws_win
) entry
;
398 xcb_window_t transient
;
399 struct ws_win
*child_trans
; /* transient child window */
400 struct swm_geometry g
; /* current geometry */
401 struct swm_geometry g_float
; /* region coordinates */
402 int g_floatvalid
; /* g_float geometry validity */
403 int floatmaxed
; /* whether maxed by max_stack */
408 unsigned int ewmh_flags
;
409 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
415 unsigned long quirks
;
416 struct workspace
*ws
; /* always valid */
417 struct swm_screen
*s
; /* always valid, never changes */
418 xcb_get_geometry_reply_t
*wa
;
420 xcb_icccm_get_wm_class_reply_t ch
;
421 xcb_icccm_wm_hints_t hints
;
423 TAILQ_HEAD(ws_win_list
, ws_win
);
427 TAILQ_ENTRY(pid_e
) entry
;
431 TAILQ_HEAD(pid_list
, pid_e
);
432 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
434 /* layout handlers */
436 void vertical_config(struct workspace
*, int);
437 void vertical_stack(struct workspace
*, struct swm_geometry
*);
438 void horizontal_config(struct workspace
*, int);
439 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
440 void max_stack(struct workspace
*, struct swm_geometry
*);
441 void plain_stacker(struct workspace
*);
442 void fancy_stacker(struct workspace
*);
444 struct ws_win
*find_window(xcb_window_t
);
446 void grabbuttons(struct ws_win
*, int);
447 void new_region(struct swm_screen
*, int, int, int, int);
448 void unmanage_window(struct ws_win
*);
449 uint16_t getstate(xcb_window_t
);
451 int conf_load(char *, int);
454 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
455 void (*l_config
)(struct workspace
*, int);
457 #define SWM_L_FOCUSPREV (1<<0)
458 #define SWM_L_MAPONFOCUS (1<<1)
459 void (*l_string
)(struct workspace
*);
461 /* stack, configure */
462 { vertical_stack
, vertical_config
, 0, plain_stacker
},
463 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
465 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
466 { NULL
, NULL
, 0, NULL
},
469 /* position of max_stack mode in the layouts array, index into layouts! */
470 #define SWM_V_STACK (0)
471 #define SWM_H_STACK (1)
472 #define SWM_MAX_STACK (2)
474 #define SWM_H_SLICE (32)
475 #define SWM_V_SLICE (32)
477 /* define work spaces */
479 int idx
; /* workspace index */
480 char *name
; /* workspace name */
481 int always_raise
; /* raise windows on focus */
482 struct layout
*cur_layout
; /* current layout handlers */
483 struct ws_win
*focus
; /* may be NULL */
484 struct ws_win
*focus_prev
; /* may be NULL */
485 struct swm_region
*r
; /* may be NULL */
486 struct swm_region
*old_r
; /* may be NULL */
487 struct ws_win_list winlist
; /* list of windows in ws */
488 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
489 char stacker
[10]; /* display stacker and layout */
493 int horizontal_msize
;
495 int horizontal_stacks
;
506 SWM_S_COLOR_BAR_BORDER
,
507 SWM_S_COLOR_BAR_FONT
,
513 /* physical screen mapping */
514 #define SWM_WS_MAX (22) /* hard limit */
515 int workspace_limit
= 10; /* soft limit */
518 int idx
; /* screen index */
519 struct swm_region_list rl
; /* list of regions on this screen */
520 struct swm_region_list orl
; /* list of old regions */
522 struct workspace ws
[SWM_WS_MAX
];
528 } c
[SWM_S_COLOR_MAX
];
530 xcb_gcontext_t bar_gc
;
532 struct swm_screen
*screens
;
534 /* args to functions */
537 #define SWM_ARG_ID_FOCUSNEXT (0)
538 #define SWM_ARG_ID_FOCUSPREV (1)
539 #define SWM_ARG_ID_FOCUSMAIN (2)
540 #define SWM_ARG_ID_FOCUSCUR (4)
541 #define SWM_ARG_ID_SWAPNEXT (10)
542 #define SWM_ARG_ID_SWAPPREV (11)
543 #define SWM_ARG_ID_SWAPMAIN (12)
544 #define SWM_ARG_ID_MOVELAST (13)
545 #define SWM_ARG_ID_MASTERSHRINK (20)
546 #define SWM_ARG_ID_MASTERGROW (21)
547 #define SWM_ARG_ID_MASTERADD (22)
548 #define SWM_ARG_ID_MASTERDEL (23)
549 #define SWM_ARG_ID_FLIPLAYOUT (24)
550 #define SWM_ARG_ID_STACKRESET (30)
551 #define SWM_ARG_ID_STACKINIT (31)
552 #define SWM_ARG_ID_CYCLEWS_UP (40)
553 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
554 #define SWM_ARG_ID_CYCLESC_UP (42)
555 #define SWM_ARG_ID_CYCLESC_DOWN (43)
556 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
557 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
558 #define SWM_ARG_ID_STACKINC (50)
559 #define SWM_ARG_ID_STACKDEC (51)
560 #define SWM_ARG_ID_SS_ALL (60)
561 #define SWM_ARG_ID_SS_WINDOW (61)
562 #define SWM_ARG_ID_DONTCENTER (70)
563 #define SWM_ARG_ID_CENTER (71)
564 #define SWM_ARG_ID_KILLWINDOW (80)
565 #define SWM_ARG_ID_DELETEWINDOW (81)
566 #define SWM_ARG_ID_WIDTHGROW (90)
567 #define SWM_ARG_ID_WIDTHSHRINK (91)
568 #define SWM_ARG_ID_HEIGHTGROW (92)
569 #define SWM_ARG_ID_HEIGHTSHRINK (93)
570 #define SWM_ARG_ID_MOVEUP (100)
571 #define SWM_ARG_ID_MOVEDOWN (101)
572 #define SWM_ARG_ID_MOVELEFT (102)
573 #define SWM_ARG_ID_MOVERIGHT (103)
577 void focus(struct swm_region
*, union arg
*);
578 void focus_magic(struct ws_win
*);
582 TAILQ_ENTRY(quirk
) entry
;
586 #define SWM_Q_FLOAT (1<<0) /* float this window */
587 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
588 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
589 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
590 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
591 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
593 TAILQ_HEAD(quirk_list
, quirk
);
594 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
597 * Supported EWMH hints should be added to
598 * both the enum and the ewmh array
603 _NET_MOVERESIZE_WINDOW
,
604 _NET_WM_ACTION_CLOSE
,
605 _NET_WM_ACTION_FULLSCREEN
,
607 _NET_WM_ACTION_RESIZE
,
608 _NET_WM_ALLOWED_ACTIONS
,
611 _NET_WM_STATE_FULLSCREEN
,
612 _NET_WM_STATE_HIDDEN
,
613 _NET_WM_STATE_MAXIMIZED_HORZ
,
614 _NET_WM_STATE_MAXIMIZED_VERT
,
615 _NET_WM_STATE_SKIP_PAGER
,
616 _NET_WM_STATE_SKIP_TASKBAR
,
618 _NET_WM_WINDOW_TYPE_DIALOG
,
619 _NET_WM_WINDOW_TYPE_DOCK
,
620 _NET_WM_WINDOW_TYPE_NORMAL
,
621 _NET_WM_WINDOW_TYPE_SPLASH
,
622 _NET_WM_WINDOW_TYPE_TOOLBAR
,
623 _NET_WM_WINDOW_TYPE_UTILITY
,
624 _SWM_WM_STATE_MANUAL
,
631 } ewmh
[SWM_EWMH_HINT_MAX
] = {
632 /* must be in same order as in the enum */
633 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
634 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
635 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
636 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
637 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
638 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
639 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
640 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
641 {"_NET_WM_STATE", XCB_ATOM_NONE
},
642 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
643 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
644 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
645 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
646 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
647 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
648 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
649 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
650 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
651 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
652 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
653 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
654 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
655 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
656 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
659 void store_float_geom(struct ws_win
*, struct swm_region
*);
660 int floating_toggle_win(struct ws_win
*);
661 void constrain_window(struct ws_win
*, struct swm_region
*, int);
662 void update_window(struct ws_win
*);
663 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
664 char *get_win_name(xcb_window_t
);
665 xcb_atom_t
get_atom_from_string(const char *);
666 void map_window_raised(xcb_window_t
);
668 xcb_screen_t
*get_screen(int);
669 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
670 void event_handle(xcb_generic_event_t
*);
673 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
675 unsigned int tmpr
, tmpg
, tmpb
;
677 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
688 get_screen(int screen
)
690 xcb_screen_iterator_t i
;
692 i
= xcb_setup_roots_iterator(xcb_get_setup(conn
));
693 for (; i
.rem
; --screen
, xcb_screen_next(&i
))
703 xcb_get_input_focus_cookie_t c
;
704 xcb_get_input_focus_reply_t
*r
;
707 c
= xcb_get_input_focus(conn
);
709 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
715 map_window_raised(xcb_window_t win
)
717 uint32_t val
= XCB_STACK_MODE_ABOVE
;
719 xcb_configure_window(conn
, win
,
720 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
722 xcb_map_window(conn
, win
);
726 get_atom_from_string(const char *str
)
728 xcb_intern_atom_cookie_t c
;
729 xcb_intern_atom_reply_t
*r
;
732 c
= xcb_intern_atom(conn
, False
, strlen(str
), str
);
733 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
741 return (XCB_ATOM_NONE
);
745 update_iconic(struct ws_win
*win
, int newv
)
752 iprop
= get_atom_from_string("_SWM_ICONIC");
753 if (iprop
== XCB_ATOM_NONE
)
757 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
758 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
760 xcb_delete_property(conn
, win
->id
, iprop
);
764 get_iconic(struct ws_win
*win
)
768 xcb_get_property_reply_t
*pr
= NULL
;
770 iprop
= get_atom_from_string("_SWM_ICONIC");
771 if (iprop
== XCB_ATOM_NONE
)
774 pr
= xcb_get_property_reply(conn
,
775 xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
779 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
781 v
= *((int32_t *)xcb_get_property_value(pr
));
792 int i
, j
, num_screens
;
794 sup_list
= get_atom_from_string("_NET_SUPPORTED");
796 for (i
= 0; i
< LENGTH(ewmh
); i
++)
797 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
799 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
800 for (i
= 0; i
< num_screens
; i
++) {
801 /* Support check window will be created by workaround(). */
803 /* Report supported atoms */
804 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
805 for (j
= 0; j
< LENGTH(ewmh
); j
++)
806 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
807 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
816 xcb_atom_t sup_check
, sup_list
;
818 xcb_get_property_cookie_t pc
;
819 xcb_get_property_reply_t
*pr
;
821 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
822 sup_list
= get_atom_from_string("_NET_SUPPORTED");
823 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
825 for (i
= 0; i
< num_screens
; i
++) {
826 /* Get the support check window and destroy it */
827 pc
= xcb_get_property(conn
, False
, screens
[i
].root
, sup_check
,
828 XCB_ATOM_WINDOW
, 0, 1);
829 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
831 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
833 xcb_destroy_window(conn
, id
);
834 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
835 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
843 ewmh_autoquirk(struct ws_win
*win
)
848 xcb_get_property_cookie_t c
;
849 xcb_get_property_reply_t
*r
;
851 c
= xcb_get_property(conn
, False
, win
->id
,
852 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
853 r
= xcb_get_property_reply(conn
, c
, NULL
);
857 n
= xcb_get_property_value_length(r
);
858 type
= xcb_get_property_value(r
);
860 for (i
= 0; i
< n
; i
++) {
861 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
863 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
864 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
865 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
867 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
870 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
871 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
873 win
->quirks
= SWM_Q_FLOAT
;
880 #define SWM_EWMH_ACTION_COUNT_MAX (6)
881 #define EWMH_F_FULLSCREEN (1<<0)
882 #define EWMH_F_ABOVE (1<<1)
883 #define EWMH_F_HIDDEN (1<<2)
884 #define EWMH_F_SKIP_PAGER (1<<3)
885 #define EWMH_F_SKIP_TASKBAR (1<<4)
886 #define SWM_F_MANUAL (1<<5)
889 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
897 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
898 "fullscreen %s\n", win
->id
, YESNO(fs
));
901 if (!win
->g_floatvalid
)
902 store_float_geom(win
, win
->ws
->r
);
904 win
->g
= win
->ws
->r
->g
;
907 if (win
->g_floatvalid
) {
908 /* refloat at last floating relative position */
909 win
->g
= win
->g_float
;
910 X(win
) += X(win
->ws
->r
);
911 Y(win
) += Y(win
->ws
->r
);
919 ewmh_update_actions(struct ws_win
*win
)
921 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
927 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
930 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
931 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
934 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
935 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
938 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
939 #define _NET_WM_STATE_ADD 1 /* add/set property */
940 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
943 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
945 unsigned int mask
= 0;
946 unsigned int changed
= 0;
947 unsigned int orig_flags
;
952 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
953 mask
= EWMH_F_FULLSCREEN
;
954 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
956 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
958 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
959 mask
= EWMH_F_SKIP_PAGER
;
960 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
961 mask
= EWMH_F_SKIP_TASKBAR
;
964 orig_flags
= win
->ewmh_flags
;
967 case _NET_WM_STATE_REMOVE
:
968 win
->ewmh_flags
&= ~mask
;
970 case _NET_WM_STATE_ADD
:
971 win
->ewmh_flags
|= mask
;
973 case _NET_WM_STATE_TOGGLE
:
974 win
->ewmh_flags
^= mask
;
978 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
980 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
982 if (!floating_toggle_win(win
))
983 win
->ewmh_flags
= orig_flags
; /* revert */
984 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
986 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
987 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
989 if (!ewmh_set_win_fullscreen(win
,
990 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
991 win
->ewmh_flags
= orig_flags
; /* revert */
993 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
995 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
996 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
997 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
998 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
999 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1000 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1001 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1002 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1003 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1004 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1005 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1006 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1007 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1008 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1009 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1010 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1011 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1012 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1013 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1014 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1018 ewmh_get_win_state(struct ws_win
*win
)
1021 xcb_get_property_cookie_t c
;
1022 xcb_get_property_reply_t
*r
;
1028 win
->ewmh_flags
= 0;
1030 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1032 win
->ewmh_flags
|= SWM_F_MANUAL
;
1034 c
= xcb_get_property(conn
, False
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1035 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1036 r
= xcb_get_property_reply(conn
, c
, NULL
);
1040 states
= xcb_get_property_value(r
);
1041 n
= xcb_get_property_value_length(r
);
1043 for (i
= 0; i
< n
; i
++)
1044 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1052 dumpwins(struct swm_region
*r
, union arg
*args
)
1056 xcb_get_window_attributes_cookie_t c
;
1057 xcb_get_window_attributes_reply_t
*wa
;
1059 if (r
->ws
== NULL
) {
1060 warnx("dumpwins: invalid workspace");
1064 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1065 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1066 state
= getstate(win
->id
);
1067 c
= xcb_get_window_attributes(conn
, win
->id
);
1068 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1070 warnx("window: 0x%x, map_state: %d, state: %u, "
1071 "transient: 0x%x", win
->id
, wa
->map_state
,
1072 state
, win
->transient
);
1075 warnx("window: 0x%x, failed xcb_get_window_attributes",
1079 warnx("===== unmanaged window list =====");
1080 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1081 state
= getstate(win
->id
);
1082 c
= xcb_get_window_attributes(conn
, win
->id
);
1083 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1085 warnx("window: 0x%x, map_state: %d, state: %u, "
1086 "transient: 0x%x", win
->id
, wa
->map_state
,
1087 state
, win
->transient
);
1090 warnx("window: 0x%x, failed xcb_get_window_attributes",
1094 warnx("=================================");
1098 dumpwins(struct swm_region
*r
, union arg
*args
)
1101 #endif /* SWM_DEBUG */
1103 void event_error(xcb_generic_error_t
*);
1104 void expose(xcb_expose_event_t
*);
1105 void keypress(xcb_key_press_event_t
*);
1106 void buttonpress(xcb_button_press_event_t
*);
1107 void configurerequest(xcb_configure_request_event_t
*);
1108 void configurenotify(xcb_configure_notify_event_t
*);
1109 void destroynotify(xcb_destroy_notify_event_t
*);
1110 void enternotify(xcb_enter_notify_event_t
*);
1111 void mapnotify(xcb_map_notify_event_t
*);
1112 void mappingnotify(xcb_mapping_notify_event_t
*);
1113 void maprequest(xcb_map_request_event_t
*);
1114 void propertynotify(xcb_property_notify_event_t
*);
1115 void unmapnotify(xcb_unmap_notify_event_t
*);
1116 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1117 void clientmessage(xcb_client_message_event_t
*);
1118 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1123 int saved_errno
, status
;
1126 saved_errno
= errno
;
1130 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1135 if (errno
!= ECHILD
)
1136 warn("sighdlr: waitpid");
1137 #endif /* SWM_DEBUG */
1140 if (pid
== searchpid
)
1144 if (WIFEXITED(status
)) {
1145 if (WEXITSTATUS(status
) != 0)
1146 warnx("sighdlr: child exit status: %d",
1147 WEXITSTATUS(status
));
1149 warnx("sighdlr: child is terminated "
1151 #endif /* SWM_DEBUG */
1165 errno
= saved_errno
;
1171 struct pid_e
*p
= NULL
;
1173 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1178 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1187 name_to_color(const char *colorname
)
1189 uint32_t result
= 0;
1190 char cname
[32] = "#";
1191 xcb_screen_t
*screen
;
1192 xcb_colormap_t cmap
;
1193 xcb_alloc_color_reply_t
*cr
;
1194 xcb_alloc_named_color_reply_t
*nr
;
1195 uint16_t rr
, gg
, bb
;
1197 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1198 cmap
= screen
->default_colormap
;
1200 /* color is in format rgb://rr/gg/bb */
1201 if (strncmp(colorname
, "rgb:", 4) == 0) {
1202 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1203 warnx("could not parse rgb %s", colorname
);
1205 cr
= xcb_alloc_color_reply(conn
,
1206 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1212 warnx("color '%s' not found", colorname
);
1215 nr
= xcb_alloc_named_color_reply(conn
,
1216 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1219 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1220 nr
= xcb_alloc_named_color_reply(conn
,
1221 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1228 warnx("color '%s' not found", colorname
);
1235 setscreencolor(char *val
, int i
, int c
)
1239 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1240 if (i
> 0 && i
<= num_screens
) {
1241 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1242 free(screens
[i
- 1].c
[c
].name
);
1243 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1245 } else if (i
== -1) {
1246 for (i
= 0; i
< num_screens
; i
++) {
1247 screens
[i
].c
[c
].color
= name_to_color(val
);
1248 free(screens
[i
].c
[c
].name
);
1249 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1253 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1258 fancy_stacker(struct workspace
*ws
)
1260 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1261 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1262 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1263 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1264 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1265 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1266 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1267 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1268 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1272 plain_stacker(struct workspace
*ws
)
1274 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1275 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1276 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1277 sizeof ws
->stacker
);
1278 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1279 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1280 sizeof ws
->stacker
);
1284 custom_region(char *val
)
1286 unsigned int sidx
, x
, y
, w
, h
;
1288 xcb_screen_t
*screen
;
1290 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1291 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1292 errx(1, "invalid custom region, "
1293 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1294 if (sidx
< 1 || sidx
> num_screens
)
1295 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1299 screen
= get_screen(sidx
);
1301 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1303 if (x
> screen
->width_in_pixels
||
1304 y
> screen
->height_in_pixels
||
1305 w
+ x
> screen
->width_in_pixels
||
1306 h
+ y
> screen
->height_in_pixels
) {
1307 warnx("ignoring region %ux%u+%u+%u - not within screen "
1308 "boundaries (%ux%u)", w
, h
, x
, y
,
1309 screen
->width_in_pixels
, screen
->height_in_pixels
);
1313 new_region(&screens
[sidx
], x
, y
, w
, h
);
1317 socket_setnonblock(int fd
)
1321 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1322 err(1, "fcntl F_GETFL");
1323 flags
|= O_NONBLOCK
;
1324 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1325 err(1, "fcntl F_SETFL");
1329 bar_print(struct swm_region
*r
, const char *s
)
1333 xcb_rectangle_t rect
;
1335 XRectangle ibox
, lbox
;
1338 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1340 switch (bar_justify
) {
1341 case SWM_BAR_JUSTIFY_LEFT
:
1344 case SWM_BAR_JUSTIFY_CENTER
:
1345 x
= (WIDTH(r
) - lbox
.width
) / 2;
1347 case SWM_BAR_JUSTIFY_RIGHT
:
1348 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1352 if (x
< SWM_BAR_OFFSET
)
1357 rect
.width
= WIDTH(r
->bar
);
1358 rect
.height
= HEIGHT(r
->bar
);
1360 /* clear back buffer */
1361 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1362 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1363 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1364 sizeof(rect
), &rect
);
1366 /* draw back buffer */
1367 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1368 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1369 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
;
1370 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1371 xcb_image_text_8(conn
, len
, r
->bar
->buffer
, r
->s
->bar_gc
, x
,
1372 (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1376 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1377 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1381 bar_extra_stop(void)
1385 bzero(bar_pipe
, sizeof bar_pipe
);
1388 kill(bar_pid
, SIGTERM
);
1391 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1396 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1398 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1400 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1401 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1405 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1407 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1409 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1410 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1414 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1416 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1419 bar_class_name(s
, sz
, r
);
1420 strlcat(s
, ":", sz
);
1421 bar_title_name(s
, sz
, r
);
1425 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1427 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1429 if (r
->ws
->focus
->floating
)
1430 strlcat(s
, "(f)", sz
);
1434 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1438 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1440 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1443 strlcat(s
, title
, sz
);
1447 int urgent
[SWM_WS_MAX
];
1449 bar_urgent(char *s
, size_t sz
)
1452 int i
, j
, num_screens
;
1454 xcb_get_property_cookie_t c
;
1455 xcb_icccm_wm_hints_t hints
;
1457 for (i
= 0; i
< workspace_limit
; i
++)
1460 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1461 for (i
= 0; i
< num_screens
; i
++)
1462 for (j
= 0; j
< workspace_limit
; j
++)
1463 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1464 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1465 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1468 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1472 for (i
= 0; i
< workspace_limit
; i
++) {
1474 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1476 snprintf(b
, sizeof b
, "- ");
1482 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1484 if (r
== NULL
|| r
->ws
== NULL
)
1486 if (r
->ws
->name
!= NULL
)
1487 strlcat(s
, r
->ws
->name
, sz
);
1490 /* build the default bar format according to the defined enabled options */
1492 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1494 /* if format provided, just copy the buffers */
1495 if (bar_format
!= NULL
) {
1496 strlcpy(fmtnew
, fmtexp
, sz
);
1500 /* reset the output buffer */
1503 strlcat(fmtnew
, "+N:+I ", sz
);
1505 strlcat(fmtnew
, "+S", sz
);
1506 strlcat(fmtnew
, " ", sz
);
1508 /* only show the workspace name if there's actually one */
1509 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1510 strlcat(fmtnew
, "<+D>", sz
);
1511 strlcat(fmtnew
, "+3<", sz
);
1513 if (clock_enabled
) {
1514 strlcat(fmtnew
, fmtexp
, sz
);
1515 strlcat(fmtnew
, "+4<", sz
);
1518 /* bar_urgent already adds the space before the last asterisk */
1520 strlcat(fmtnew
, "* +U*+4<", sz
);
1522 if (title_class_enabled
) {
1523 strlcat(fmtnew
, "+C", sz
);
1524 if (title_name_enabled
== 0)
1525 strlcat(fmtnew
, "+4<", sz
);
1528 /* checks needed by the colon and floating strlcat(3) calls below */
1529 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1530 if (title_name_enabled
) {
1531 if (title_class_enabled
)
1532 strlcat(fmtnew
, ":", sz
);
1533 strlcat(fmtnew
, "+T+4<", sz
);
1535 if (window_name_enabled
) {
1536 if (r
->ws
->focus
->floating
)
1537 strlcat(fmtnew
, "+F ", sz
);
1538 strlcat(fmtnew
, "+64W ", sz
);
1542 /* finally add the action script output and the version */
1543 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1547 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1549 /* special case; no limit given, pad one space, instead */
1550 if (*limit
== sz
- 1)
1552 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1555 /* replaces the bar format character sequences (like in tmux(1)) */
1557 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1561 char tmp
[SWM_BAR_MAX
];
1565 /* reset strlcat(3) buffer */
1568 /* get number, if any */
1571 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1572 limit
= sizeof tmp
- 1;
1573 if (limit
<= 0 || limit
>= sizeof tmp
)
1574 limit
= sizeof tmp
- 1;
1576 /* there is nothing to replace (ie EOL) */
1583 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1586 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1589 bar_class_name(tmp
, sizeof tmp
, r
);
1592 bar_workspace_name(tmp
, sizeof tmp
, r
);
1595 bar_window_float(tmp
, sizeof tmp
, r
);
1598 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1601 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1604 bar_class_title_name(tmp
, sizeof tmp
, r
);
1607 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1610 bar_title_name(tmp
, sizeof tmp
, r
);
1613 bar_urgent(tmp
, sizeof tmp
);
1616 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1619 bar_window_name(tmp
, sizeof tmp
, r
);
1622 /* unknown character sequence; copy as-is */
1623 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1631 while (limit
-- > 0) {
1632 if (*offrep
>= sz
- 1)
1634 fmtrep
[(*offrep
)++] = *ptr
++;
1642 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1647 while (*fmt
!= '\0') {
1649 /* skip ordinary characters */
1652 fmtrep
[off
++] = *fmt
++;
1656 /* character sequence found; replace it */
1657 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1666 bar_fmt_expand(char *fmtexp
, size_t sz
)
1673 /* start by grabbing the current time and date */
1675 localtime_r(&tmt
, &tm
);
1677 /* figure out what to expand */
1678 if (bar_format
!= NULL
)
1680 else if (bar_format
== NULL
&& clock_enabled
)
1682 /* if nothing to expand bail out */
1688 /* copy as-is, just in case the format shouldn't be expanded below */
1689 strlcpy(fmtexp
, fmt
, sz
);
1690 /* finally pass the string through strftime(3) */
1691 #ifndef SWM_DENY_CLOCK_FORMAT
1692 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1693 warnx("format too long");
1701 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1702 char fmtrep
[SWM_BAR_MAX
];
1704 struct swm_region
*r
;
1706 /* expand the format by first passing it through strftime(3) */
1707 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1709 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1710 for (i
= 0; i
< num_screens
; i
++) {
1711 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1714 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1715 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1716 bar_print(r
, fmtrep
);
1727 if (bar_enabled
== 0)
1729 if (bar_extra
&& bar_extra_running
) {
1730 /* ignore short reads; it'll correct itself */
1731 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1732 if (b
&& b
[len
- 1] == '\n') {
1734 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1736 if (b
== NULL
&& errno
!= EAGAIN
) {
1737 warn("bar_update: bar_extra failed");
1741 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1754 bar_toggle(struct swm_region
*r
, union arg
*args
)
1756 struct swm_region
*tmpr
;
1759 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1761 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1763 for (i
= 0; i
< num_screens
; i
++)
1764 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1766 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1768 for (i
= 0; i
< num_screens
; i
++)
1769 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1771 map_window_raised(tmpr
->bar
->id
);
1774 bar_enabled
= !bar_enabled
;
1777 /* must be after stack */
1786 struct swm_region
*r
;
1790 /* do this here because the conf file is in memory */
1791 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1792 /* launch external status app */
1793 bar_extra_running
= 1;
1794 if (pipe(bar_pipe
) == -1)
1795 err(1, "pipe error");
1796 socket_setnonblock(bar_pipe
[0]);
1797 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1798 if (dup2(bar_pipe
[0], 0) == -1)
1800 if (dup2(bar_pipe
[1], 1) == -1)
1802 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1803 err(1, "could not disable SIGPIPE");
1804 switch (bar_pid
= fork()) {
1806 err(1, "cannot fork");
1810 execvp(bar_argv
[0], bar_argv
);
1811 err(1, "%s external app failed", bar_argv
[0]);
1813 default: /* parent */
1819 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1820 for (i
= 0; i
< num_screens
; i
++)
1821 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1824 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1825 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1826 xcb_change_window_attributes(conn
, r
->bar
->id
,
1827 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1833 bar_setup(struct swm_region
*r
)
1835 char *default_string
;
1836 char **missing_charsets
;
1837 int num_missing_charsets
= 0;
1839 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1843 XFreeFontSet(display
, bar_fs
);
1847 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1848 err(1, "bar_setup: calloc: failed to allocate memory.");
1850 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1852 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1853 &num_missing_charsets
, &default_string
);
1855 if (num_missing_charsets
> 0) {
1856 warnx("Unable to load charset(s):");
1858 for (i
= 0; i
< num_missing_charsets
; ++i
)
1859 warnx("%s", missing_charsets
[i
]);
1861 XFreeStringList(missing_charsets
);
1863 if (strcmp(default_string
, ""))
1864 warnx("Glyphs from those sets will be replaced "
1865 "by '%s'.", default_string
);
1867 warnx("Glyphs from those sets won't be drawn.");
1871 errx(1, "Error creating font set structure.");
1873 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1875 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1876 2 * bar_border_width
;
1882 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1883 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1884 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1886 r
->bar
->id
= xcb_generate_id(conn
);
1887 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1888 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
;
1889 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
1890 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1891 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1892 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1893 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
1894 | XCB_CW_EVENT_MASK
, wa
);
1896 r
->bar
->buffer
= xcb_generate_id(conn
);
1897 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1898 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1900 xcb_randr_select_input(conn
, r
->bar
->id
,
1901 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1904 map_window_raised(r
->bar
->id
);
1906 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1907 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1910 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1911 err(1, "could not install bar_signal");
1916 bar_cleanup(struct swm_region
*r
)
1920 xcb_destroy_window(conn
, r
->bar
->id
);
1921 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1927 set_win_state(struct ws_win
*win
, uint16_t state
)
1929 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1931 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1936 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, astate
,
1937 astate
, 32, 2, data
);
1941 getstate(xcb_window_t w
)
1943 uint16_t result
= 0;
1944 xcb_get_property_cookie_t c
;
1945 xcb_get_property_reply_t
*r
;
1947 c
= xcb_get_property(conn
, False
, w
, astate
, astate
, 0L, 2L);
1948 r
= xcb_get_property_reply(conn
, c
, NULL
);
1951 result
= *((uint16_t *)xcb_get_property_value(r
));
1955 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
1961 version(struct swm_region
*r
, union arg
*args
)
1963 bar_version
= !bar_version
;
1965 snprintf(bar_vertext
, sizeof bar_vertext
,
1966 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1968 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1975 client_msg(struct ws_win
*win
, xcb_atom_t a
)
1977 xcb_client_message_event_t ev
;
1982 bzero(&ev
, sizeof ev
);
1983 ev
.response_type
= XCB_CLIENT_MESSAGE
;
1984 ev
.window
= win
->id
;
1987 ev
.data
.data32
[0] = a
;
1988 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
1990 xcb_send_event(conn
, False
, win
->id
,
1991 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
1994 /* synthetic response to a ConfigureRequest when not making a change */
1996 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
1998 xcb_configure_notify_event_t ce
;
2003 /* send notification of unchanged state. */
2004 bzero(&ce
, sizeof(ce
));
2005 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2008 ce
.width
= WIDTH(win
);
2009 ce
.height
= HEIGHT(win
);
2010 ce
.override_redirect
= False
;
2015 ce
.window
= win
->id
;
2016 ce
.border_width
= BORDER(win
);
2017 ce
.above_sibling
= XCB_WINDOW_NONE
;
2020 ce
.event
= ev
->window
;
2021 ce
.window
= ev
->window
;
2023 /* make response appear more WM_SIZE_HINTS-compliant */
2025 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2026 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2027 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2028 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2029 SH_INC_W(win
), SH_INC_H(win
));
2033 /* the hint may be set... to 0! */
2034 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2035 ce
.width
= SH_MIN_W(win
);
2036 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2037 ce
.height
= SH_MIN_H(win
);
2042 /* may also be advertized as 0 */
2043 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2044 ce
.width
= SH_MAX_W(win
);
2045 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2046 ce
.height
= SH_MAX_H(win
);
2049 /* resize increment. */
2051 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2052 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2054 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2055 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2059 /* adjust x and y for requested border_width. */
2060 ce
.x
+= BORDER(win
) - ev
->border_width
;
2061 ce
.y
+= BORDER(win
) - ev
->border_width
;
2062 ce
.border_width
= ev
->border_width
;
2063 ce
.above_sibling
= ev
->sibling
;
2066 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2067 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2068 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2070 xcb_send_event(conn
, False
, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2075 count_win(struct workspace
*ws
, int count_transient
)
2080 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2081 if (count_transient
== 0 && win
->floating
)
2083 if (count_transient
== 0 && win
->transient
)
2089 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2095 quit(struct swm_region
*r
, union arg
*args
)
2097 DNPRINTF(SWM_D_MISC
, "quit\n");
2102 unmap_window(struct ws_win
*win
)
2107 /* don't unmap again */
2108 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2111 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2113 xcb_unmap_window(conn
, win
->id
);
2114 xcb_change_window_attributes(conn
, win
->id
,
2115 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2122 int i
, j
, num_screens
;
2124 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2125 for (i
= 0; i
< num_screens
; i
++)
2126 for (j
= 0; j
< workspace_limit
; j
++)
2127 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2132 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2134 xcb_key_press_event_t event
;
2135 xcb_keycode_t
*keycode
;
2140 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2142 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2145 bzero(&event
, sizeof(event
));
2146 event
.event
= win
->id
;
2147 event
.root
= win
->s
->root
;
2148 event
.child
= XCB_WINDOW_NONE
;
2149 event
.time
= XCB_CURRENT_TIME
;
2150 event
.event_x
= X(win
);
2151 event
.event_y
= Y(win
);
2154 event
.same_screen
= True
;
2155 event
.detail
= *keycode
;
2156 event
.state
= modifiers
;
2158 event
.response_type
= XCB_KEY_PRESS
;
2159 xcb_send_event(conn
, True
, win
->id
,
2160 XCB_EVENT_MASK_KEY_PRESS
, (char *)&event
);
2162 event
.response_type
= XCB_KEY_RELEASE
;
2163 xcb_send_event(conn
, True
, win
->id
,
2164 XCB_EVENT_MASK_KEY_RELEASE
, (char *)&event
);
2168 restart(struct swm_region
*r
, union arg
*args
)
2170 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2172 /* disable alarm because the following code may not be interrupted */
2174 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2175 err(1, "can't disable alarm");
2181 xcb_key_symbols_free(syms
);
2183 xcb_disconnect(conn
);
2185 execvp(start_argv
[0], start_argv
);
2186 warn("execvp failed");
2191 root_to_region(xcb_window_t root
)
2193 struct swm_region
*r
= NULL
;
2195 xcb_query_pointer_reply_t
*qpr
;
2197 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2199 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2200 for (i
= 0; i
< num_screens
; i
++)
2201 if (screens
[i
].root
== root
)
2204 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2205 screens
[i
].root
), NULL
);
2208 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2209 qpr
->root_x
, qpr
->root_y
);
2210 /* choose a region based on pointer location */
2211 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2212 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2213 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2219 r
= TAILQ_FIRST(&screens
[i
].rl
);
2225 find_unmanaged_window(xcb_window_t id
)
2228 int i
, j
, num_screens
;
2230 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2231 for (i
= 0; i
< num_screens
; i
++)
2232 for (j
= 0; j
< workspace_limit
; j
++)
2233 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2241 find_window(xcb_window_t id
)
2244 int i
, j
, num_screens
;
2245 xcb_query_tree_cookie_t c
;
2246 xcb_query_tree_reply_t
*r
;
2248 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2249 for (i
= 0; i
< num_screens
; i
++)
2250 for (j
= 0; j
< workspace_limit
; j
++)
2251 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2255 c
= xcb_query_tree(conn
, id
);
2256 r
= xcb_query_tree_reply(conn
, c
, NULL
);
2260 /* if we were looking for the parent return that window instead */
2261 if (r
->parent
== 0 || r
->root
== r
->parent
)
2264 /* look for parent */
2265 for (i
= 0; i
< num_screens
; i
++)
2266 for (j
= 0; j
< workspace_limit
; j
++)
2267 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2268 if (r
->parent
== win
->id
) {
2278 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2283 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2286 close(xcb_get_file_descriptor(conn
));
2288 setenv("LD_PRELOAD", SWM_LIB
, 1);
2290 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2291 warn("spawn: asprintf SWM_WS");
2294 setenv("_SWM_WS", ret
, 1);
2298 if (asprintf(&ret
, "%d", getpid()) == -1) {
2299 warn("spawn: asprintf _SWM_PID");
2302 setenv("_SWM_PID", ret
, 1);
2306 if (setsid() == -1) {
2307 warn("spawn: setsid");
2313 * close stdin and stdout to prevent interaction between apps
2314 * and the baraction script
2315 * leave stderr open to record errors
2317 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2318 warn("spawn: open");
2321 dup2(fd
, STDIN_FILENO
);
2322 dup2(fd
, STDOUT_FILENO
);
2327 execvp(args
->argv
[0], args
->argv
);
2329 warn("spawn: execvp");
2334 kill_refs(struct ws_win
*win
)
2336 int i
, x
, num_screens
;
2337 struct swm_region
*r
;
2338 struct workspace
*ws
;
2343 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2344 for (i
= 0; i
< num_screens
; i
++)
2345 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2346 for (x
= 0; x
< workspace_limit
; x
++) {
2348 if (win
== ws
->focus
)
2350 if (win
== ws
->focus_prev
)
2351 ws
->focus_prev
= NULL
;
2356 validate_win(struct ws_win
*testwin
)
2359 struct workspace
*ws
;
2360 struct swm_region
*r
;
2361 int i
, x
, num_screens
;
2363 if (testwin
== NULL
)
2366 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2367 for (i
= 0; i
< num_screens
; i
++)
2368 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2369 for (x
= 0; x
< workspace_limit
; x
++) {
2371 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2379 validate_ws(struct workspace
*testws
)
2381 struct swm_region
*r
;
2382 struct workspace
*ws
;
2383 int i
, x
, num_screens
;
2385 /* validate all ws */
2386 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2387 for (i
= 0; i
< num_screens
; i
++)
2388 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2389 for (x
= 0; x
< workspace_limit
; x
++) {
2398 unfocus_win(struct ws_win
*win
)
2400 xcb_window_t none
= XCB_WINDOW_NONE
;
2402 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2406 if (win
->ws
== NULL
)
2409 if (validate_ws(win
->ws
))
2410 return; /* XXX this gets hit with thunderbird, needs fixing */
2412 if (win
->ws
->r
== NULL
)
2415 if (validate_win(win
)) {
2420 if (win
->ws
->focus
== win
) {
2421 win
->ws
->focus
= NULL
;
2422 win
->ws
->focus_prev
= win
;
2425 if (validate_win(win
->ws
->focus
)) {
2426 kill_refs(win
->ws
->focus
);
2427 win
->ws
->focus
= NULL
;
2429 if (validate_win(win
->ws
->focus_prev
)) {
2430 kill_refs(win
->ws
->focus_prev
);
2431 win
->ws
->focus_prev
= NULL
;
2434 grabbuttons(win
, 0);
2435 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2436 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2437 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2438 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2445 int i
, j
, num_screens
;
2447 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2449 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2450 for (i
= 0; i
< num_screens
; i
++)
2451 for (j
= 0; j
< workspace_limit
; j
++)
2452 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2457 focus_win(struct ws_win
*win
)
2459 struct ws_win
*cfw
= NULL
;
2460 xcb_get_input_focus_cookie_t c
;
2461 xcb_get_input_focus_reply_t
*r
;
2462 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2464 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2468 if (win
->ws
== NULL
)
2471 if (validate_ws(win
->ws
))
2472 return; /* XXX this gets hit with thunderbird, needs fixing */
2474 if (validate_win(win
)) {
2479 if (validate_win(win
)) {
2484 c
= xcb_get_input_focus(conn
);
2485 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
2487 cur_focus
= r
->focus
;
2490 if ((cfw
= find_window(cur_focus
)) != NULL
)
2493 /* use larger hammer since the window was killed somehow */
2494 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2495 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2496 xcb_change_window_attributes(conn, cfw->id,
2497 XCB_CW_BORDER_PIXEL,
2498 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2501 win
->ws
->focus
= win
;
2503 if (win
->ws
->r
!= NULL
) {
2505 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2506 win
->id
, XCB_CURRENT_TIME
);
2507 grabbuttons(win
, 1);
2508 xcb_change_window_attributes(conn
, win
->id
,
2509 XCB_CW_BORDER_PIXEL
,
2510 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2511 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2512 win
->ws
->always_raise
)
2513 map_window_raised(win
->id
);
2515 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2516 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2524 switchws(struct swm_region
*r
, union arg
*args
)
2526 int wsid
= args
->id
, unmap_old
= 0;
2527 struct swm_region
*this_r
, *other_r
;
2529 struct workspace
*new_ws
, *old_ws
;
2535 if (wsid
>= workspace_limit
)
2539 old_ws
= this_r
->ws
;
2540 new_ws
= &this_r
->s
->ws
[wsid
];
2542 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2543 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2545 if (new_ws
== NULL
|| old_ws
== NULL
)
2547 if (new_ws
== old_ws
)
2550 other_r
= new_ws
->r
;
2551 if (other_r
== NULL
) {
2552 /* the other workspace is hidden, hide this one */
2556 /* the other ws is visible in another region, exchange them */
2557 other_r
->ws_prior
= new_ws
;
2558 other_r
->ws
= old_ws
;
2559 old_ws
->r
= other_r
;
2561 this_r
->ws_prior
= old_ws
;
2562 this_r
->ws
= new_ws
;
2565 /* this is needed so that we can click on a window after a restart */
2569 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2570 focus(new_ws
->r
, &a
);
2572 /* unmap old windows */
2574 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2581 cyclews(struct swm_region
*r
, union arg
*args
)
2584 struct swm_screen
*s
= r
->s
;
2587 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2588 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2593 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2596 case SWM_ARG_ID_CYCLEWS_UP
:
2597 if (a
.id
< workspace_limit
- 1)
2602 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2605 case SWM_ARG_ID_CYCLEWS_DOWN
:
2609 a
.id
= workspace_limit
- 1;
2616 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2618 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2622 } while (a
.id
!= r
->ws
->idx
);
2626 priorws(struct swm_region
*r
, union arg
*args
)
2630 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2631 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2633 if (r
->ws_prior
== NULL
)
2636 a
.id
= r
->ws_prior
->idx
;
2641 cyclescr(struct swm_region
*r
, union arg
*args
)
2643 struct swm_region
*rr
= NULL
;
2645 int i
, x
, y
, num_screens
;
2647 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2648 /* do nothing if we don't have more than one screen */
2649 if (!(num_screens
> 1 || outputs
> 1))
2654 case SWM_ARG_ID_CYCLESC_UP
:
2655 rr
= TAILQ_NEXT(r
, entry
);
2657 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2659 case SWM_ARG_ID_CYCLESC_DOWN
:
2660 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2662 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2670 /* move mouse to region */
2672 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2673 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2676 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2679 if (rr
->ws
->focus
) {
2680 /* move to focus window */
2681 x
= X(rr
->ws
->focus
) + 1;
2682 y
= Y(rr
->ws
->focus
) + 1;
2683 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2689 sort_windows(struct ws_win_list
*wl
)
2691 struct ws_win
*win
, *parent
, *nxt
;
2696 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2697 nxt
= TAILQ_NEXT(win
, entry
);
2698 if (win
->transient
) {
2699 parent
= find_window(win
->transient
);
2700 if (parent
== NULL
) {
2701 warnx("not possible bug");
2704 TAILQ_REMOVE(wl
, win
, entry
);
2705 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2712 swapwin(struct swm_region
*r
, union arg
*args
)
2714 struct ws_win
*target
, *source
;
2715 struct ws_win
*cur_focus
;
2716 struct ws_win_list
*wl
;
2719 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2720 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2722 cur_focus
= r
->ws
->focus
;
2723 if (cur_focus
== NULL
)
2727 wl
= &source
->ws
->winlist
;
2730 case SWM_ARG_ID_SWAPPREV
:
2731 if (source
->transient
)
2732 source
= find_window(source
->transient
);
2733 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2734 if (target
&& target
->transient
)
2735 target
= find_window(target
->transient
);
2736 TAILQ_REMOVE(wl
, source
, entry
);
2738 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2740 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2742 case SWM_ARG_ID_SWAPNEXT
:
2743 target
= TAILQ_NEXT(source
, entry
);
2744 /* move the parent and let the sort handle the move */
2745 if (source
->transient
)
2746 source
= find_window(source
->transient
);
2747 TAILQ_REMOVE(wl
, source
, entry
);
2749 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2751 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2753 case SWM_ARG_ID_SWAPMAIN
:
2754 target
= TAILQ_FIRST(wl
);
2755 if (target
== source
) {
2756 if (source
->ws
->focus_prev
!= NULL
&&
2757 source
->ws
->focus_prev
!= target
)
2758 source
= source
->ws
->focus_prev
;
2762 if (target
== NULL
|| source
== NULL
)
2764 source
->ws
->focus_prev
= target
;
2765 TAILQ_REMOVE(wl
, target
, entry
);
2766 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2767 TAILQ_REMOVE(wl
, source
, entry
);
2768 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2770 case SWM_ARG_ID_MOVELAST
:
2771 TAILQ_REMOVE(wl
, source
, entry
);
2772 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2775 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2787 focus_prev(struct ws_win
*win
)
2789 struct ws_win
*winfocus
= NULL
;
2790 struct ws_win
*cur_focus
= NULL
;
2791 struct ws_win_list
*wl
= NULL
;
2792 struct workspace
*ws
= NULL
;
2794 if (!(win
&& win
->ws
))
2799 cur_focus
= ws
->focus
;
2801 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2802 WINID(win
), WINID(cur_focus
));
2804 /* pickle, just focus on whatever */
2805 if (cur_focus
== NULL
) {
2806 /* use prev_focus if valid */
2807 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2808 find_window(WINID(ws
->focus_prev
)))
2809 winfocus
= ws
->focus_prev
;
2813 /* if transient focus on parent */
2814 if (cur_focus
->transient
) {
2815 winfocus
= find_window(cur_focus
->transient
);
2819 /* if in max_stack try harder */
2820 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2821 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2822 if (cur_focus
!= ws
->focus_prev
)
2823 winfocus
= ws
->focus_prev
;
2825 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2830 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2832 if (winfocus
== NULL
|| winfocus
== win
) {
2833 switch (focus_close
) {
2834 case SWM_STACK_BOTTOM
:
2835 winfocus
= TAILQ_FIRST(wl
);
2838 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2840 case SWM_STACK_ABOVE
:
2841 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2842 if (focus_close_wrap
)
2843 winfocus
= TAILQ_FIRST(wl
);
2845 winfocus
= TAILQ_PREV(cur_focus
,
2846 ws_win_list
, entry
);
2849 case SWM_STACK_BELOW
:
2850 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2852 if (focus_close_wrap
)
2853 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2855 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2861 if (winfocus
== NULL
) {
2862 if (focus_default
== SWM_STACK_TOP
)
2863 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2865 winfocus
= TAILQ_FIRST(wl
);
2869 focus_magic(winfocus
);
2873 focus(struct swm_region
*r
, union arg
*args
)
2875 struct ws_win
*winfocus
= NULL
, *head
;
2876 struct ws_win
*cur_focus
= NULL
;
2877 struct ws_win_list
*wl
= NULL
;
2878 struct workspace
*ws
= NULL
;
2884 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2886 /* treat FOCUS_CUR special */
2887 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2888 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2889 winfocus
= r
->ws
->focus
;
2890 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2891 winfocus
= r
->ws
->focus_prev
;
2893 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2894 if (winfocus
->iconic
== 0)
2897 focus_magic(winfocus
);
2901 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2905 if (TAILQ_EMPTY(wl
))
2907 /* make sure there is at least one uniconified window */
2909 TAILQ_FOREACH(winfocus
, wl
, entry
)
2910 if (winfocus
->iconic
== 0) {
2918 case SWM_ARG_ID_FOCUSPREV
:
2919 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2921 head
= TAILQ_LAST(wl
, ws_win_list
);
2923 if (WINID(winfocus
) == cur_focus
->transient
) {
2924 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2926 head
= TAILQ_LAST(wl
, ws_win_list
);
2931 if (winfocus
&& winfocus
->iconic
) {
2932 while (winfocus
!= cur_focus
) {
2933 if (winfocus
== NULL
)
2934 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2935 if (winfocus
->iconic
== 0)
2937 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2943 case SWM_ARG_ID_FOCUSNEXT
:
2944 head
= TAILQ_NEXT(cur_focus
, entry
);
2946 head
= TAILQ_FIRST(wl
);
2950 if (winfocus
&& winfocus
->iconic
) {
2951 while (winfocus
!= cur_focus
) {
2952 if (winfocus
== NULL
)
2953 winfocus
= TAILQ_FIRST(wl
);
2954 if (winfocus
->iconic
== 0)
2956 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2961 case SWM_ARG_ID_FOCUSMAIN
:
2962 winfocus
= TAILQ_FIRST(wl
);
2963 if (winfocus
== cur_focus
)
2964 winfocus
= cur_focus
->ws
->focus_prev
;
2971 focus_magic(winfocus
);
2977 cycle_layout(struct swm_region
*r
, union arg
*args
)
2979 struct workspace
*ws
= r
->ws
;
2982 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2985 if (ws
->cur_layout
->l_stack
== NULL
)
2986 ws
->cur_layout
= &layouts
[0];
2990 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2996 stack_config(struct swm_region
*r
, union arg
*args
)
2998 struct workspace
*ws
= r
->ws
;
3000 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3003 if (ws
->cur_layout
->l_config
!= NULL
)
3004 ws
->cur_layout
->l_config(ws
, args
->id
);
3006 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3013 struct swm_geometry g
;
3014 struct swm_region
*r
;
3020 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3022 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3023 for (i
= 0; i
< num_screens
; i
++) {
3027 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3028 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3029 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3031 /* start with screen geometry, adjust for bar */
3033 g
.w
-= 2 * border_width
;
3034 g
.h
-= 2 * border_width
;
3040 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3041 r
->ws
->cur_layout
->l_string(r
->ws
);
3042 /* save r so we can track region changes */
3049 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3053 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3055 /* retain window geom and region geom */
3056 win
->g_float
= win
->g
;
3057 win
->g_float
.x
-= X(r
);
3058 win
->g_float
.y
-= Y(r
);
3059 win
->g_floatvalid
= 1;
3060 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3061 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3062 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3063 win
->g_float
.w
, win
->g_float
.h
);
3067 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3072 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3075 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3076 * geom on ws switches or return from max mode
3078 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3079 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3080 /* refloat at last floating relative position */
3081 win
->g
= win
->g_float
;
3086 win
->floatmaxed
= 0;
3089 * if set to fullscreen mode, configure window to maximum size.
3091 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3092 if (!win
->g_floatvalid
)
3093 store_float_geom(win
, win
->ws
->r
);
3095 win
->g
= win
->ws
->r
->g
;
3099 * remove border on fullscreen floater when in fullscreen mode or when
3100 * the quirk is present.
3102 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3103 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3104 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3105 if (win
->bordered
) {
3107 X(win
) += border_width
;
3108 Y(win
) += border_width
;
3110 } else if (!win
->bordered
) {
3112 X(win
) -= border_width
;
3113 Y(win
) -= border_width
;
3116 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3117 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3118 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3123 * floaters and transients are auto-centred unless moved
3126 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3127 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3130 /* keep window within region bounds */
3131 constrain_window(win
, r
, 0);
3137 * Send keystrokes to terminal to decrease/increase the font size as the
3138 * window size changes.
3141 adjust_font(struct ws_win
*win
)
3143 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3144 win
->floating
|| win
->transient
)
3147 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3148 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3149 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3150 win
->font_size_boundary
[win
->font_steps
] =
3151 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3154 win
->last_inc
= win
->sh
.width_inc
;
3155 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3156 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3157 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3160 win
->last_inc
= win
->sh
.width_inc
;
3161 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3165 #define SWAPXY(g) do { \
3167 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3168 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3171 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3173 struct swm_geometry win_g
, r_g
= *g
;
3174 struct ws_win
*win
, *fs_win
= NULL
;
3175 int i
, j
, s
, stacks
;
3176 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3177 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3178 int split
, colno
, winno
, mwin
, msize
, mscale
;
3179 int remain
, missing
, v_slice
, reconfigure
;
3181 xcb_get_window_attributes_reply_t
*war
;
3183 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3184 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3186 winno
= count_win(ws
, 0);
3187 if (winno
== 0 && count_win(ws
, 1) == 0)
3190 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3191 if (win
->transient
== 0 && win
->floating
== 0
3192 && win
->iconic
== 0)
3199 w_inc
= win
->sh
.width_inc
;
3200 w_base
= win
->sh
.base_width
;
3201 mwin
= ws
->l_state
.horizontal_mwin
;
3202 mscale
= ws
->l_state
.horizontal_msize
;
3203 stacks
= ws
->l_state
.horizontal_stacks
;
3206 w_inc
= win
->sh
.height_inc
;
3207 w_base
= win
->sh
.base_height
;
3208 mwin
= ws
->l_state
.vertical_mwin
;
3209 mscale
= ws
->l_state
.vertical_msize
;
3210 stacks
= ws
->l_state
.vertical_stacks
;
3214 if (stacks
> winno
- mwin
)
3215 stacks
= winno
- mwin
;
3219 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3220 if (mwin
&& winno
> mwin
) {
3221 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3225 win_g
.w
= v_slice
* mscale
;
3227 if (w_inc
> 1 && w_inc
< v_slice
) {
3228 /* adjust for window's requested size increment */
3229 remain
= (win_g
.w
- w_base
) % w_inc
;
3235 win_g
.x
+= r_g
.w
- msize
;
3238 colno
= split
= winno
/ stacks
;
3239 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3240 2 * border_width
) / stacks
);
3242 hrh
= r_g
.h
/ colno
;
3243 extra
= r_g
.h
- (colno
* hrh
);
3244 win_g
.h
= hrh
- 2 * border_width
;
3246 /* stack all the tiled windows */
3247 i
= j
= 0, s
= stacks
;
3248 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3249 if (win
->transient
!= 0 || win
->floating
!= 0)
3251 if (win
->iconic
!= 0)
3254 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3259 if (split
&& i
== split
) {
3260 colno
= (winno
- mwin
) / stacks
;
3261 if (s
<= (winno
- mwin
) % stacks
)
3263 split
= split
+ colno
;
3264 hrh
= (r_g
.h
/ colno
);
3265 extra
= r_g
.h
- (colno
* hrh
);
3269 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3270 win_g
.w
= (r_g
.w
- msize
-
3271 (stacks
* 2 * border_width
)) / stacks
;
3273 win_g
.w
+= (r_g
.w
- msize
-
3274 (stacks
* 2 * border_width
)) % stacks
;
3278 win_g
.h
= hrh
- 2 * border_width
;
3280 h_inc
= win
->sh
.width_inc
;
3281 h_base
= win
->sh
.base_width
;
3283 h_inc
= win
->sh
.height_inc
;
3284 h_base
= win
->sh
.base_height
;
3286 if (j
== colno
- 1) {
3287 win_g
.h
= hrh
+ extra
;
3288 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3289 /* adjust for window's requested size increment */
3290 remain
= (win_g
.h
- h_base
) % h_inc
;
3291 missing
= h_inc
- remain
;
3293 if (missing
<= extra
|| j
== 0) {
3305 win_g
.y
+= last_h
+ 2 * border_width
;
3307 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3309 win_g
.w
+= 2 * border_width
;
3310 win_g
.h
+= 2 * border_width
;
3315 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3316 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3320 WIDTH(win
) = win_g
.h
;
3321 HEIGHT(win
) = win_g
.w
;
3324 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3325 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3329 WIDTH(win
) = win_g
.w
;
3330 HEIGHT(win
) = win_g
.h
;
3334 if (bordered
!= win
->bordered
) {
3336 win
->bordered
= bordered
;
3344 war
= xcb_get_window_attributes_reply(conn
,
3345 xcb_get_window_attributes(conn
, win
->id
),
3348 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3349 map_window_raised(win
->id
);
3359 /* now, stack all the floaters and transients */
3360 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3361 if (win
->transient
== 0 && win
->floating
== 0)
3363 if (win
->iconic
== 1)
3365 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3370 stack_floater(win
, ws
->r
);
3371 map_window_raised(win
->id
);
3375 stack_floater(fs_win
, ws
->r
);
3376 map_window_raised(fs_win
->id
);
3381 vertical_config(struct workspace
*ws
, int id
)
3383 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3387 case SWM_ARG_ID_STACKRESET
:
3388 case SWM_ARG_ID_STACKINIT
:
3389 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3390 ws
->l_state
.vertical_mwin
= 1;
3391 ws
->l_state
.vertical_stacks
= 1;
3393 case SWM_ARG_ID_MASTERSHRINK
:
3394 if (ws
->l_state
.vertical_msize
> 1)
3395 ws
->l_state
.vertical_msize
--;
3397 case SWM_ARG_ID_MASTERGROW
:
3398 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3399 ws
->l_state
.vertical_msize
++;
3401 case SWM_ARG_ID_MASTERADD
:
3402 ws
->l_state
.vertical_mwin
++;
3404 case SWM_ARG_ID_MASTERDEL
:
3405 if (ws
->l_state
.vertical_mwin
> 0)
3406 ws
->l_state
.vertical_mwin
--;
3408 case SWM_ARG_ID_STACKINC
:
3409 ws
->l_state
.vertical_stacks
++;
3411 case SWM_ARG_ID_STACKDEC
:
3412 if (ws
->l_state
.vertical_stacks
> 1)
3413 ws
->l_state
.vertical_stacks
--;
3415 case SWM_ARG_ID_FLIPLAYOUT
:
3416 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3424 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3426 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3428 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3432 horizontal_config(struct workspace
*ws
, int id
)
3434 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3437 case SWM_ARG_ID_STACKRESET
:
3438 case SWM_ARG_ID_STACKINIT
:
3439 ws
->l_state
.horizontal_mwin
= 1;
3440 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3441 ws
->l_state
.horizontal_stacks
= 1;
3443 case SWM_ARG_ID_MASTERSHRINK
:
3444 if (ws
->l_state
.horizontal_msize
> 1)
3445 ws
->l_state
.horizontal_msize
--;
3447 case SWM_ARG_ID_MASTERGROW
:
3448 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3449 ws
->l_state
.horizontal_msize
++;
3451 case SWM_ARG_ID_MASTERADD
:
3452 ws
->l_state
.horizontal_mwin
++;
3454 case SWM_ARG_ID_MASTERDEL
:
3455 if (ws
->l_state
.horizontal_mwin
> 0)
3456 ws
->l_state
.horizontal_mwin
--;
3458 case SWM_ARG_ID_STACKINC
:
3459 ws
->l_state
.horizontal_stacks
++;
3461 case SWM_ARG_ID_STACKDEC
:
3462 if (ws
->l_state
.horizontal_stacks
> 1)
3463 ws
->l_state
.horizontal_stacks
--;
3465 case SWM_ARG_ID_FLIPLAYOUT
:
3466 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3474 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3476 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3478 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3481 /* fullscreen view */
3483 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3485 struct swm_geometry gg
= *g
;
3486 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3487 int winno
, num_screens
;
3489 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3494 winno
= count_win(ws
, 0);
3495 if (winno
== 0 && count_win(ws
, 1) == 0)
3498 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3499 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3500 if (win
->transient
) {
3502 parent
= find_window(win
->transient
);
3506 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3508 * retain geometry for retrieval on exit from
3511 store_float_geom(win
, ws
->r
);
3512 win
->floatmaxed
= 1;
3515 /* only reconfigure if necessary */
3516 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3517 HEIGHT(win
) != gg
.h
) {
3523 WIDTH(win
) += 2 * border_width
;
3524 HEIGHT(win
) += 2 * border_width
;
3529 /* unmap only if we don't have multi screen */
3530 if (win
!= ws
->focus
)
3531 if (!(num_screens
> 1 || outputs
> 1))
3535 /* put the last transient on top */
3538 map_window_raised(parent
->id
);
3539 stack_floater(wintrans
, ws
->r
);
3540 focus_magic(wintrans
);
3545 send_to_ws(struct swm_region
*r
, union arg
*args
)
3547 int wsid
= args
->id
;
3548 struct ws_win
*win
= NULL
, *parent
;
3549 struct workspace
*ws
, *nws
;
3550 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
3551 char ws_idx_str
[SWM_PROPLEN
];
3554 if (wsid
>= workspace_limit
)
3557 if (r
&& r
->ws
&& r
->ws
->focus
)
3563 if (win
->ws
->idx
== wsid
)
3566 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3569 nws
= &win
->s
->ws
[wsid
];
3571 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3573 if (win
->transient
) {
3574 parent
= find_window(win
->transient
);
3576 unmap_window(parent
);
3577 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3578 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3583 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3584 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3585 if (TAILQ_EMPTY(&ws
->winlist
))
3586 r
->ws
->focus
= NULL
;
3589 /* Try to update the window's workspace property */
3590 ws_idx_atom
= get_atom_from_string("_SWM_WS");
3592 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3594 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3596 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3597 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3608 pressbutton(struct swm_region
*r
, union arg
*args
)
3610 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3611 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3612 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3613 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3617 raise_toggle(struct swm_region
*r
, union arg
*args
)
3619 if (r
== NULL
|| r
->ws
== NULL
)
3622 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3624 /* bring floaters back to top */
3625 if (r
->ws
->always_raise
== 0)
3632 iconify(struct swm_region
*r
, union arg
*args
)
3636 if (r
->ws
->focus
== NULL
)
3638 unmap_window(r
->ws
->focus
);
3639 update_iconic(r
->ws
->focus
, 1);
3642 r
->ws
->focus
= NULL
;
3643 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3650 get_win_name(xcb_window_t win
)
3653 xcb_get_property_cookie_t c
;
3654 xcb_icccm_get_text_property_reply_t r
;
3656 c
= xcb_icccm_get_wm_name(conn
, win
);
3657 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3658 if (r
.name_len
> 0) {
3659 name
= malloc(r
.name_len
+ 1);
3661 xcb_icccm_get_text_property_reply_wipe(&r
);
3664 memcpy(name
, r
.name
, r
.name_len
);
3665 name
[r
.name_len
] = '\0';
3667 xcb_icccm_get_text_property_reply_wipe(&r
);
3674 uniconify(struct swm_region
*r
, union arg
*args
)
3681 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3683 if (r
== NULL
|| r
->ws
== NULL
)
3686 /* make sure we have anything to uniconify */
3687 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3688 if (win
->ws
== NULL
)
3689 continue; /* should never happen */
3690 if (win
->iconic
== 0)
3698 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3700 spawn_select(r
, args
, "search", &searchpid
);
3702 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3705 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3706 if (win
->ws
== NULL
)
3707 continue; /* should never happen */
3708 if (win
->iconic
== 0)
3711 name
= get_win_name(win
->id
);
3714 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3724 name_workspace(struct swm_region
*r
, union arg
*args
)
3728 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3734 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3736 spawn_select(r
, args
, "name_workspace", &searchpid
);
3738 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3741 fprintf(lfile
, "%s", "");
3746 search_workspace(struct swm_region
*r
, union arg
*args
)
3749 struct workspace
*ws
;
3752 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3758 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3760 spawn_select(r
, args
, "search", &searchpid
);
3762 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3765 for (i
= 0; i
< workspace_limit
; i
++) {
3769 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3770 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3777 search_win_cleanup(void)
3779 struct search_window
*sw
= NULL
;
3781 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3782 xcb_destroy_window(conn
, sw
->indicator
);
3783 xcb_free_gc(conn
, sw
->gc
);
3784 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3790 search_win(struct swm_region
*r
, union arg
*args
)
3792 struct ws_win
*win
= NULL
;
3793 struct search_window
*sw
= NULL
;
3795 uint32_t gcv
[3], wa
[2];
3800 XRectangle ibox
, lbox
;
3801 DNPRINTF(SWM_D_MISC
, "search_win\n");
3804 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3806 spawn_select(r
, args
, "search", &searchpid
);
3808 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3811 TAILQ_INIT(&search_wl
);
3814 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3815 if (win
->iconic
== 1)
3818 sw
= calloc(1, sizeof(struct search_window
));
3820 warn("search_win: calloc");
3822 search_win_cleanup();
3828 snprintf(s
, sizeof s
, "%d", i
);
3831 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3833 w
= xcb_generate_id(conn
);
3834 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3835 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
;
3836 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3837 lbox
.width
+ 4, bar_fs_extents
->max_logical_extent
.height
,
3838 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3839 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3842 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3844 sw
->gc
= xcb_generate_id(conn
);
3845 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
3846 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3848 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3849 XCB_GC_BACKGROUND
| XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
3850 map_window_raised(w
);
3852 xcb_image_text_8(conn
, len
, w
, sw
->gc
, 2,
3853 (bar_fs_extents
->max_logical_extent
.height
-
3854 lbox
.height
) / 2 - lbox
.y
, s
);
3856 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
3858 fprintf(lfile
, "%d\n", i
);
3868 search_resp_uniconify(char *resp
, unsigned long len
)
3874 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3876 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3877 if (win
->iconic
== 0)
3879 name
= get_win_name(win
->id
);
3882 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3887 if (strncmp(s
, resp
, len
) == 0) {
3888 /* XXX this should be a callback to generalize */
3889 update_iconic(win
, 0);
3898 search_resp_name_workspace(char *resp
, unsigned long len
)
3900 struct workspace
*ws
;
3902 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3904 if (search_r
->ws
== NULL
)
3909 free(search_r
->ws
->name
);
3910 search_r
->ws
->name
= NULL
;
3914 ws
->name
= strdup(resp
);
3915 if (ws
->name
== NULL
) {
3916 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3917 "strdup: %s", strerror(errno
));
3924 search_resp_search_workspace(char *resp
, unsigned long len
)
3931 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3935 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3942 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3944 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3951 switchws(search_r
, &a
);
3955 search_resp_search_window(char *resp
, unsigned long len
)
3960 struct search_window
*sw
;
3962 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3966 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3971 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3973 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3980 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3981 if (idx
== sw
->idx
) {
3987 #define MAX_RESP_LEN 1024
3990 search_do_resp(void)
3996 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4001 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4002 warn("search: calloc");
4006 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4008 warn("search: read error");
4011 resp
[rbytes
] = '\0';
4014 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4015 * newline, so work around that by sanitizing the resp now.
4017 resp
[strcspn(resp
, "\n")] = '\0';
4020 switch (search_resp_action
) {
4021 case SWM_SEARCH_UNICONIFY
:
4022 search_resp_uniconify(resp
, len
);
4024 case SWM_SEARCH_NAME_WORKSPACE
:
4025 search_resp_name_workspace(resp
, len
);
4027 case SWM_SEARCH_SEARCH_WORKSPACE
:
4028 search_resp_search_workspace(resp
, len
);
4030 case SWM_SEARCH_SEARCH_WINDOW
:
4031 search_resp_search_window(resp
, len
);
4036 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4037 search_win_cleanup();
4039 search_resp_action
= SWM_SEARCH_NONE
;
4040 close(select_resp_pipe
[0]);
4045 wkill(struct swm_region
*r
, union arg
*args
)
4047 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4049 if (r
->ws
->focus
== NULL
)
4052 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4053 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4055 if (r
->ws
->focus
->can_delete
)
4056 client_msg(r
->ws
->focus
, adelete
);
4063 floating_toggle_win(struct ws_win
*win
)
4065 struct swm_region
*r
;
4075 /* reject floating toggles in max stack mode */
4076 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4079 if (win
->floating
) {
4080 if (!win
->floatmaxed
) {
4081 /* retain position for refloat */
4082 store_float_geom(win
, r
);
4086 if (win
->g_floatvalid
) {
4087 /* refloat at last floating relative position */
4088 X(win
) = win
->g_float
.x
+ X(r
);
4089 Y(win
) = win
->g_float
.y
+ Y(r
);
4090 WIDTH(win
) = win
->g_float
.w
;
4091 HEIGHT(win
) = win
->g_float
.h
;
4096 ewmh_update_actions(win
);
4102 floating_toggle(struct swm_region
*r
, union arg
*args
)
4104 struct ws_win
*win
= r
->ws
->focus
;
4110 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4111 _NET_WM_STATE_TOGGLE
);
4115 if (win
== win
->ws
->focus
) {
4116 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4117 focus(win
->ws
->r
, &a
);
4124 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4126 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4128 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4130 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4133 if (X(win
) + BORDER(win
) < X(r
)) {
4135 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4137 X(win
) = X(r
) - BORDER(win
);
4140 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4142 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4144 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4147 if (Y(win
) + BORDER(win
) < Y(r
)) {
4149 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4151 Y(win
) = Y(r
) - BORDER(win
);
4157 if (HEIGHT(win
) < 1)
4163 update_window(struct ws_win
*win
)
4168 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4169 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4170 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4174 wc
[3] = HEIGHT(win
);
4175 wc
[4] = BORDER(win
);
4177 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4178 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4179 wc
[3], YESNO(win
->bordered
));
4181 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4184 #define SWM_RESIZE_STEPS (50)
4187 resize(struct ws_win
*win
, union arg
*args
)
4189 xcb_timestamp_t timestamp
= 0;
4190 struct swm_region
*r
= NULL
;
4191 int resize_step
= 0;
4192 struct swm_geometry g
;
4193 int top
= 0, left
= 0, resizing
;
4195 unsigned int shape
; /* cursor style */
4196 xcb_cursor_t cursor
;
4197 xcb_font_t cursor_font
;
4198 xcb_grab_pointer_cookie_t gpc
;
4199 xcb_grab_pointer_reply_t
*gpr
;
4200 xcb_query_pointer_reply_t
*xpr
;
4201 xcb_generic_event_t
*evt
;
4202 xcb_motion_notify_event_t
*mne
;
4208 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4211 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4212 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4215 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4218 /* reject resizes in max mode for floaters (transient ok) */
4219 if (win
->floatmaxed
)
4223 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4229 case SWM_ARG_ID_WIDTHSHRINK
:
4230 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4233 case SWM_ARG_ID_WIDTHGROW
:
4234 WIDTH(win
) += SWM_RESIZE_STEPS
;
4237 case SWM_ARG_ID_HEIGHTSHRINK
:
4238 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4241 case SWM_ARG_ID_HEIGHTGROW
:
4242 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4249 constrain_window(win
, r
, 1);
4251 store_float_geom(win
,r
);
4255 /* get cursor offset from window root */
4256 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4263 if (xpr
->win_x
< WIDTH(win
) / 2)
4266 if (xpr
->win_y
< HEIGHT(win
) / 2)
4269 if (args
->id
== SWM_ARG_ID_CENTER
)
4272 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4274 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4276 cursor_font
= xcb_generate_id(conn
);
4277 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4279 cursor
= xcb_generate_id(conn
);
4280 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4281 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4283 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4284 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4286 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4288 xcb_free_cursor(conn
, cursor
);
4289 xcb_close_font(conn
, cursor_font
);
4296 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4298 XMaskEvent(display, MOUSEMASK | ExposureMask |
4299 SubstructureRedirectMask, &ev);
4301 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4302 case XCB_BUTTON_RELEASE
:
4303 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4306 case XCB_MOTION_NOTIFY
:
4307 mne
= (xcb_motion_notify_event_t
*)evt
;
4308 /* cursor offset/delta from start of the operation */
4309 dx
= mne
->root_x
- xpr
->root_x
;
4310 dy
= mne
->root_y
- xpr
->root_y
;
4316 if (args
->id
== SWM_ARG_ID_CENTER
) {
4317 if (g
.h
/ 2 + dy
< 1)
4321 HEIGHT(win
) = g
.h
+ 2 * dy
;
4329 HEIGHT(win
) = g
.h
+ dy
;
4336 if (args
->id
== SWM_ARG_ID_CENTER
) {
4337 if (g
.w
/ 2 + dx
< 1)
4341 WIDTH(win
) = g
.w
+ 2 * dx
;
4349 WIDTH(win
) = g
.w
+ dx
;
4352 constrain_window(win
, r
, 1);
4354 /* not free, don't sync more than 120 times / second */
4355 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4356 timestamp
= mne
->time
;
4371 store_float_geom(win
,r
);
4373 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4374 xcb_free_cursor(conn
, cursor
);
4375 xcb_close_font(conn
, cursor_font
);
4378 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4382 resize_step(struct swm_region
*r
, union arg
*args
)
4384 struct ws_win
*win
= NULL
;
4386 if (r
&& r
->ws
&& r
->ws
->focus
)
4394 #define SWM_MOVE_STEPS (50)
4397 move(struct ws_win
*win
, union arg
*args
)
4399 xcb_timestamp_t timestamp
= 0;
4400 int move_step
= 0, moving
;
4401 struct swm_region
*r
= NULL
;
4402 xcb_font_t cursor_font
;
4403 xcb_cursor_t cursor
;
4404 xcb_grab_pointer_cookie_t gpc
;
4405 xcb_grab_pointer_reply_t
*gpr
;
4406 xcb_query_pointer_reply_t
*qpr
;
4407 xcb_generic_event_t
*evt
;
4408 xcb_motion_notify_event_t
*mne
;
4414 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4417 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4418 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4420 /* in max_stack mode should only move transients */
4421 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4425 if (win
->floating
== 0 && !win
->transient
) {
4426 store_float_geom(win
, r
);
4427 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4430 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4437 case SWM_ARG_ID_MOVELEFT
:
4438 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4441 case SWM_ARG_ID_MOVERIGHT
:
4442 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4445 case SWM_ARG_ID_MOVEUP
:
4446 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4449 case SWM_ARG_ID_MOVEDOWN
:
4450 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4457 constrain_window(win
, r
, 0);
4459 store_float_geom(win
, r
);
4463 cursor_font
= xcb_generate_id(conn
);
4464 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4466 cursor
= xcb_generate_id(conn
);
4467 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4468 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4470 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4471 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4472 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4473 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4475 xcb_free_cursor(conn
, cursor
);
4476 xcb_close_font(conn
, cursor_font
);
4480 /* get cursor offset from window root */
4481 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4484 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4485 xcb_free_cursor(conn
, cursor
);
4486 xcb_close_font(conn
, cursor_font
);
4492 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4493 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4494 case XCB_BUTTON_RELEASE
:
4495 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4498 case XCB_MOTION_NOTIFY
:
4499 mne
= (xcb_motion_notify_event_t
*)evt
;
4500 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4501 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4503 constrain_window(win
, r
, 0);
4505 /* not free, don't sync more than 120 times / second */
4506 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4507 timestamp
= mne
->time
;
4522 store_float_geom(win
, r
);
4524 xcb_free_cursor(conn
, cursor
);
4525 xcb_close_font(conn
, cursor_font
);
4526 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4527 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4531 move_step(struct swm_region
*r
, union arg
*args
)
4533 struct ws_win
*win
= NULL
;
4535 if (r
&& r
->ws
&& r
->ws
->focus
)
4540 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4547 /* user/key callable function IDs */
4636 kf_search_workspace
,
4638 kf_dumpwins
, /* MUST BE LAST */
4642 /* key definitions */
4644 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4649 char name
[SWM_FUNCNAME_LEN
];
4650 void (*func
)(struct swm_region
*r
, union arg
*);
4652 } keyfuncs
[kf_invalid
+ 1] = {
4653 /* name function argument */
4654 { "cycle_layout", cycle_layout
, {0} },
4655 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4656 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4657 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4658 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4659 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4660 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4661 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4662 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4663 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4664 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4665 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4666 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4667 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4668 { "quit", quit
, {0} },
4669 { "restart", restart
, {0} },
4670 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4671 { "ws_1", switchws
, {.id
= 0} },
4672 { "ws_2", switchws
, {.id
= 1} },
4673 { "ws_3", switchws
, {.id
= 2} },
4674 { "ws_4", switchws
, {.id
= 3} },
4675 { "ws_5", switchws
, {.id
= 4} },
4676 { "ws_6", switchws
, {.id
= 5} },
4677 { "ws_7", switchws
, {.id
= 6} },
4678 { "ws_8", switchws
, {.id
= 7} },
4679 { "ws_9", switchws
, {.id
= 8} },
4680 { "ws_10", switchws
, {.id
= 9} },
4681 { "ws_11", switchws
, {.id
= 10} },
4682 { "ws_12", switchws
, {.id
= 11} },
4683 { "ws_13", switchws
, {.id
= 12} },
4684 { "ws_14", switchws
, {.id
= 13} },
4685 { "ws_15", switchws
, {.id
= 14} },
4686 { "ws_16", switchws
, {.id
= 15} },
4687 { "ws_17", switchws
, {.id
= 16} },
4688 { "ws_18", switchws
, {.id
= 17} },
4689 { "ws_19", switchws
, {.id
= 18} },
4690 { "ws_20", switchws
, {.id
= 19} },
4691 { "ws_21", switchws
, {.id
= 20} },
4692 { "ws_22", switchws
, {.id
= 21} },
4693 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4694 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4695 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4696 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4697 { "ws_prior", priorws
, {0} },
4698 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4699 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4700 { "mvws_1", send_to_ws
, {.id
= 0} },
4701 { "mvws_2", send_to_ws
, {.id
= 1} },
4702 { "mvws_3", send_to_ws
, {.id
= 2} },
4703 { "mvws_4", send_to_ws
, {.id
= 3} },
4704 { "mvws_5", send_to_ws
, {.id
= 4} },
4705 { "mvws_6", send_to_ws
, {.id
= 5} },
4706 { "mvws_7", send_to_ws
, {.id
= 6} },
4707 { "mvws_8", send_to_ws
, {.id
= 7} },
4708 { "mvws_9", send_to_ws
, {.id
= 8} },
4709 { "mvws_10", send_to_ws
, {.id
= 9} },
4710 { "mvws_11", send_to_ws
, {.id
= 10} },
4711 { "mvws_12", send_to_ws
, {.id
= 11} },
4712 { "mvws_13", send_to_ws
, {.id
= 12} },
4713 { "mvws_14", send_to_ws
, {.id
= 13} },
4714 { "mvws_15", send_to_ws
, {.id
= 14} },
4715 { "mvws_16", send_to_ws
, {.id
= 15} },
4716 { "mvws_17", send_to_ws
, {.id
= 16} },
4717 { "mvws_18", send_to_ws
, {.id
= 17} },
4718 { "mvws_19", send_to_ws
, {.id
= 18} },
4719 { "mvws_20", send_to_ws
, {.id
= 19} },
4720 { "mvws_21", send_to_ws
, {.id
= 20} },
4721 { "mvws_22", send_to_ws
, {.id
= 21} },
4722 { "bar_toggle", bar_toggle
, {0} },
4723 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4724 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4725 { "float_toggle", floating_toggle
,{0} },
4726 { "version", version
, {0} },
4727 { "spawn_custom", dummykeyfunc
, {0} },
4728 { "iconify", iconify
, {0} },
4729 { "uniconify", uniconify
, {0} },
4730 { "raise_toggle", raise_toggle
, {0} },
4731 { "button2", pressbutton
, {2} },
4732 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4733 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4734 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4735 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4736 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4737 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4738 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4739 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4740 { "name_workspace", name_workspace
, {0} },
4741 { "search_workspace", search_workspace
, {0} },
4742 { "search_win", search_win
, {0} },
4743 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4744 { "invalid key func", NULL
, {0} },
4747 RB_ENTRY(key
) entry
;
4750 enum keyfuncid funcid
;
4753 RB_HEAD(key_tree
, key
);
4756 key_cmp(struct key
*kp1
, struct key
*kp2
)
4758 if (kp1
->keysym
< kp2
->keysym
)
4760 if (kp1
->keysym
> kp2
->keysym
)
4763 if (kp1
->mod
< kp2
->mod
)
4765 if (kp1
->mod
> kp2
->mod
)
4771 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4772 struct key_tree keys
;
4775 enum { client_click
, root_click
};
4777 unsigned int action
;
4779 unsigned int button
;
4780 void (*func
)(struct ws_win
*, union arg
*);
4783 /* action key mouse button func args */
4784 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4785 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4786 { client_click
, MODKEY
, Button1
, move
, {0} },
4790 update_modkey(unsigned int mod
)
4796 RB_FOREACH(kp
, key_tree
, &keys
)
4797 if (kp
->mod
& ShiftMask
)
4798 kp
->mod
= mod
| ShiftMask
;
4802 for (i
= 0; i
< LENGTH(buttons
); i
++)
4803 if (buttons
[i
].mask
& ShiftMask
)
4804 buttons
[i
].mask
= mod
| ShiftMask
;
4806 buttons
[i
].mask
= mod
;
4811 TAILQ_ENTRY(spawn_prog
) entry
;
4816 TAILQ_HEAD(spawn_list
, spawn_prog
);
4817 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4820 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4823 struct spawn_prog
*prog
= NULL
;
4825 char *ap
, **real_args
;
4827 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4830 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4831 if (!strcasecmp(spawn_name
, prog
->name
))
4835 warnx("spawn_custom: program %s not found", spawn_name
);
4839 /* make room for expanded args */
4840 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4841 err(1, "spawn_custom: calloc real_args");
4843 /* expand spawn_args into real_args */
4844 for (i
= 0; i
< prog
->argc
; i
++) {
4846 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4847 if (!strcasecmp(ap
, "$bar_border")) {
4849 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4851 err(1, "spawn_custom border color");
4852 } else if (!strcasecmp(ap
, "$bar_color")) {
4854 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4856 err(1, "spawn_custom bar color");
4857 } else if (!strcasecmp(ap
, "$bar_font")) {
4858 if ((real_args
[i
] = strdup(bar_fonts
))
4860 err(1, "spawn_custom bar fonts");
4861 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4863 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4865 err(1, "spawn_custom color font");
4866 } else if (!strcasecmp(ap
, "$color_focus")) {
4868 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4870 err(1, "spawn_custom color focus");
4871 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4873 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4875 err(1, "spawn_custom color unfocus");
4877 /* no match --> copy as is */
4878 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4879 err(1, "spawn_custom strdup(ap)");
4881 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4886 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4887 for (i
= 0; i
< prog
->argc
; i
++)
4888 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4889 DNPRINTF(SWM_D_SPAWN
, "\n");
4891 *ret_args
= real_args
;
4892 return (prog
->argc
);
4896 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4902 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4906 spawn(r
->ws
->idx
, &a
, 1);
4908 for (i
= 0; i
< spawn_argc
; i
++)
4914 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4920 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4924 if (pipe(select_list_pipe
) == -1)
4925 err(1, "pipe error");
4926 if (pipe(select_resp_pipe
) == -1)
4927 err(1, "pipe error");
4929 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4930 err(1, "could not disable SIGPIPE");
4931 switch (*pid
= fork()) {
4933 err(1, "cannot fork");
4936 if (dup2(select_list_pipe
[0], 0) == -1)
4938 if (dup2(select_resp_pipe
[1], 1) == -1)
4940 close(select_list_pipe
[1]);
4941 close(select_resp_pipe
[0]);
4942 spawn(r
->ws
->idx
, &a
, 0);
4944 default: /* parent */
4945 close(select_list_pipe
[0]);
4946 close(select_resp_pipe
[1]);
4950 for (i
= 0; i
< spawn_argc
; i
++)
4956 spawn_insert(char *name
, char *args
)
4958 char *arg
, *cp
, *ptr
;
4959 struct spawn_prog
*sp
;
4961 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4963 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4964 err(1, "spawn_insert: malloc");
4965 if ((sp
->name
= strdup(name
)) == NULL
)
4966 err(1, "spawn_insert: strdup");
4968 /* convert the arguments to an argument list */
4969 if ((ptr
= cp
= strdup(args
)) == NULL
)
4970 err(1, "spawn_insert: strdup");
4971 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4972 /* empty field; skip it */
4977 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4978 sizeof *sp
->argv
)) == NULL
)
4979 err(1, "spawn_insert: realloc");
4980 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4981 err(1, "spawn_insert: strdup");
4985 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4986 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4990 spawn_remove(struct spawn_prog
*sp
)
4994 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4996 TAILQ_REMOVE(&spawns
, sp
, entry
);
4997 for (i
= 0; i
< sp
->argc
; i
++)
5003 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5007 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5009 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5012 spawn_insert(name
, args
);
5014 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5018 setspawn(char *name
, char *args
)
5020 struct spawn_prog
*sp
;
5022 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5027 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5028 if (!strcmp(sp
->name
, name
)) {
5032 spawn_replace(sp
, name
, args
);
5033 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5037 if (*args
== '\0') {
5038 warnx("error: setspawn: cannot find program: %s", name
);
5042 spawn_insert(name
, args
);
5043 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5047 setconfspawn(char *selector
, char *value
, int flags
)
5049 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5051 setspawn(selector
, value
);
5053 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5060 setconfspawn("term", "xterm", 0);
5061 setconfspawn("spawn_term", "xterm", 0);
5062 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5063 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5064 setconfspawn("lock", "xlock", 0);
5065 setconfspawn("initscr", "initscreen.sh", 0);
5066 setconfspawn("menu", "dmenu_run"
5069 " -nf $bar_font_color"
5071 " -sf $bar_color", 0);
5072 setconfspawn("search", "dmenu"
5076 " -nf $bar_font_color"
5078 " -sf $bar_color", 0);
5079 setconfspawn("name_workspace", "dmenu"
5083 " -nf $bar_font_color"
5085 " -sf $bar_color", 0);
5089 #define SWM_MODNAME_SIZE 32
5090 #define SWM_KEY_WS "\n+ \t"
5092 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5096 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5097 if (mod
== NULL
|| ks
== NULL
) {
5098 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5101 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5102 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5108 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5109 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5111 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5112 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5114 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5116 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5118 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5120 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5122 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5124 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5125 *mod
|= ControlMask
;
5127 *ks
= XStringToKeysym(name
);
5128 XConvertCase(*ks
, ks
, &uks
);
5129 if (ks
== NoSymbol
) {
5131 "parsekeys: invalid key %s\n",
5137 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5142 strdupsafe(char *str
)
5147 return (strdup(str
));
5151 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5155 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5156 keyfuncs
[kfid
].name
, spawn_name
);
5158 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5159 err(1, "key_insert: malloc");
5164 kp
->spawn_name
= strdupsafe(spawn_name
);
5165 RB_INSERT(key_tree
, &keys
, kp
);
5167 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5171 key_lookup(unsigned int mod
, KeySym ks
)
5178 return (RB_FIND(key_tree
, &keys
, &kp
));
5182 key_remove(struct key
*kp
)
5184 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5186 RB_REMOVE(key_tree
, &keys
, kp
);
5187 free(kp
->spawn_name
);
5190 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5194 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5197 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5201 key_insert(mod
, ks
, kfid
, spawn_name
);
5203 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5207 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5212 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5213 keyfuncs
[kfid
].name
, spawn_name
);
5215 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5216 if (kfid
== kf_invalid
)
5219 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5220 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5223 if (kfid
== kf_invalid
) {
5224 warnx("error: setkeybinding: cannot find mod/key combination");
5225 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5229 key_insert(mod
, ks
, kfid
, spawn_name
);
5230 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5234 setconfbinding(char *selector
, char *value
, int flags
)
5236 enum keyfuncid kfid
;
5239 struct spawn_prog
*sp
;
5240 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5241 if (selector
== NULL
) {
5242 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5243 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5245 setkeybinding(mod
, ks
, kfid
, NULL
);
5250 /* search by key function name */
5251 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5252 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5253 SWM_FUNCNAME_LEN
) == 0) {
5254 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5256 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5257 setkeybinding(mod
, ks
, kfid
, NULL
);
5263 /* search by custom spawn name */
5264 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5265 if (strcasecmp(selector
, sp
->name
) == 0) {
5266 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5268 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5269 setkeybinding(mod
, ks
, kf_spawn_custom
,
5276 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5283 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5284 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5285 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5286 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5287 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5288 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5289 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5290 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5291 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5292 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5293 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5294 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5295 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5296 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5297 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5298 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5299 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5300 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5301 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5302 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5303 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5304 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5305 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5306 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5307 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5308 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5309 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5310 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5311 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5312 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5313 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5314 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5315 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5316 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5317 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5318 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5319 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5320 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5321 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5322 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5323 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5324 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5325 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5326 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5327 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5328 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5329 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5330 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5331 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5332 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5333 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5334 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5335 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5336 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5337 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5338 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5339 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5340 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5341 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5342 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5343 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5344 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5345 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5346 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5347 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5348 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5349 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5350 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5351 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5352 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5353 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5354 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5355 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5356 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5357 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5358 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5359 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5360 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5361 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5362 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5363 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5364 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5365 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5366 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5367 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5368 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5369 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5370 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5371 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5372 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5373 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5374 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5375 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5376 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5377 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5378 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5380 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5389 while (RB_EMPTY(&keys
) == 0) {
5390 kp
= RB_ROOT(&keys
);
5396 setkeymapping(char *selector
, char *value
, int flags
)
5398 char keymapping_file
[PATH_MAX
];
5399 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5400 if (value
[0] == '~')
5401 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5402 pwd
->pw_dir
, &value
[1]);
5404 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5406 /* load new key bindings; if it fails, revert to default bindings */
5407 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5411 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5416 updatenumlockmask(void)
5419 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5420 xcb_keycode_t
*modmap
, kc
;
5422 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5425 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5426 xcb_get_modifier_mapping(conn
),
5429 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5430 for (i
= 0; i
< 8; i
++) {
5431 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5432 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5435 if (kc
== *((xcb_keycode_t
*)xcb_key_symbols_get_keycode(syms
,
5437 numlockmask
= (1 << i
);
5449 xcb_keycode_t
*code
;
5450 unsigned int modifiers
[] =
5451 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5454 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5455 updatenumlockmask();
5457 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5458 for (k
= 0; k
< num_screens
; k
++) {
5459 if (TAILQ_EMPTY(&screens
[k
].rl
))
5461 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5463 RB_FOREACH(kp
, key_tree
, &keys
) {
5464 if ((code
= xcb_key_symbols_get_keycode(syms
,
5466 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5467 xcb_grab_key(conn
, True
,
5469 kp
->mod
| modifiers
[j
],
5470 *code
, XCB_GRAB_MODE_ASYNC
,
5471 XCB_GRAB_MODE_ASYNC
);
5477 grabbuttons(struct ws_win
*win
, int focused
)
5480 unsigned int modifiers
[] =
5481 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5483 updatenumlockmask();
5484 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5485 XCB_BUTTON_MASK_ANY
);
5487 for (i
= 0; i
< LENGTH(buttons
); i
++)
5488 if (buttons
[i
].action
== client_click
)
5489 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5490 xcb_grab_button(conn
, False
, win
->id
,
5492 XCB_GRAB_MODE_ASYNC
,
5499 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5500 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5501 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5504 const char *quirkname
[] = {
5505 "NONE", /* config string for "no value" */
5514 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5515 #define SWM_Q_WS "\n|+ \t"
5517 parsequirks(char *qstr
, unsigned long *quirk
)
5527 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5529 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5530 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5531 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5532 DNPRINTF(SWM_D_QUIRK
,
5533 "parsequirks: %s\n", name
);
5538 *quirk
|= 1 << (i
-1);
5542 if (i
>= LENGTH(quirkname
)) {
5543 DNPRINTF(SWM_D_QUIRK
,
5544 "parsequirks: invalid quirk [%s]\n", name
);
5552 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5556 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5559 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5560 err(1, "quirk_insert: malloc");
5561 if ((qp
->class = strdup(class)) == NULL
)
5562 err(1, "quirk_insert: strdup");
5563 if ((qp
->name
= strdup(name
)) == NULL
)
5564 err(1, "quirk_insert: strdup");
5567 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5569 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5573 quirk_remove(struct quirk
*qp
)
5575 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5576 qp
->name
, qp
->quirk
);
5578 TAILQ_REMOVE(&quirks
, qp
, entry
);
5583 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5587 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5588 unsigned long quirk
)
5590 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5591 qp
->name
, qp
->quirk
);
5594 quirk_insert(class, name
, quirk
);
5596 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5600 setquirk(const char *class, const char *name
, unsigned long quirk
)
5604 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5607 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5608 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5612 quirk_replace(qp
, class, name
, quirk
);
5613 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5618 warnx("error: setquirk: cannot find class/name combination");
5622 quirk_insert(class, name
, quirk
);
5623 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5627 setconfquirk(char *selector
, char *value
, int flags
)
5629 char *cp
, *class, *name
;
5631 unsigned long quirks
;
5632 if (selector
== NULL
)
5634 if ((cp
= strchr(selector
, ':')) == NULL
)
5639 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5640 setquirk(class, name
, quirks
);
5647 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5648 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5649 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5650 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5651 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5652 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5653 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5654 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5655 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5656 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5657 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5658 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5659 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5662 /* conf file stuff */
5663 #define SWM_CONF_FILE "spectrwm.conf"
5664 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5668 SWM_S_BAR_AT_BOTTOM
,
5669 SWM_S_BAR_BORDER_WIDTH
,
5676 SWM_S_CLOCK_ENABLED
,
5679 SWM_S_CYCLE_VISIBLE
,
5681 SWM_S_DISABLE_BORDER
,
5683 SWM_S_FOCUS_CLOSE_WRAP
,
5684 SWM_S_FOCUS_DEFAULT
,
5690 SWM_S_STACK_ENABLED
,
5692 SWM_S_TITLE_CLASS_ENABLED
,
5693 SWM_S_TITLE_NAME_ENABLED
,
5694 SWM_S_URGENT_ENABLED
,
5695 SWM_S_VERBOSE_LAYOUT
,
5696 SWM_S_WINDOW_NAME_ENABLED
,
5697 SWM_S_WORKSPACE_LIMIT
5701 setconfvalue(char *selector
, char *value
, int flags
)
5707 case SWM_S_BAR_ACTION
:
5709 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5710 err(1, "setconfvalue: bar_action");
5712 case SWM_S_BAR_AT_BOTTOM
:
5713 bar_at_bottom
= atoi(value
);
5715 case SWM_S_BAR_BORDER_WIDTH
:
5716 bar_border_width
= atoi(value
);
5717 if (bar_border_width
< 0)
5718 bar_border_width
= 0;
5720 case SWM_S_BAR_DELAY
:
5721 bar_delay
= atoi(value
);
5723 case SWM_S_BAR_ENABLED
:
5724 bar_enabled
= atoi(value
);
5726 case SWM_S_BAR_FONT
:
5728 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5729 err(1, "setconfvalue: asprintf: failed to allocate "
5730 "memory for bar_fonts.");
5734 case SWM_S_BAR_FORMAT
:
5736 if ((bar_format
= strdup(value
)) == NULL
)
5737 err(1, "setconfvalue: bar_format");
5739 case SWM_S_BAR_JUSTIFY
:
5740 if (!strcmp(value
, "left"))
5741 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5742 else if (!strcmp(value
, "center"))
5743 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5744 else if (!strcmp(value
, "right"))
5745 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5747 errx(1, "invalid bar_justify");
5749 case SWM_S_BORDER_WIDTH
:
5750 border_width
= atoi(value
);
5751 if (border_width
< 0)
5754 case SWM_S_CLOCK_ENABLED
:
5755 clock_enabled
= atoi(value
);
5757 case SWM_S_CLOCK_FORMAT
:
5758 #ifndef SWM_DENY_CLOCK_FORMAT
5760 if ((clock_format
= strdup(value
)) == NULL
)
5761 err(1, "setconfvalue: clock_format");
5764 case SWM_S_CYCLE_EMPTY
:
5765 cycle_empty
= atoi(value
);
5767 case SWM_S_CYCLE_VISIBLE
:
5768 cycle_visible
= atoi(value
);
5770 case SWM_S_DIALOG_RATIO
:
5771 dialog_ratio
= atof(value
);
5772 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5775 case SWM_S_DISABLE_BORDER
:
5776 disable_border
= atoi(value
);
5778 case SWM_S_FOCUS_CLOSE
:
5779 if (!strcmp(value
, "first"))
5780 focus_close
= SWM_STACK_BOTTOM
;
5781 else if (!strcmp(value
, "last"))
5782 focus_close
= SWM_STACK_TOP
;
5783 else if (!strcmp(value
, "next"))
5784 focus_close
= SWM_STACK_ABOVE
;
5785 else if (!strcmp(value
, "previous"))
5786 focus_close
= SWM_STACK_BELOW
;
5788 errx(1, "focus_close");
5790 case SWM_S_FOCUS_CLOSE_WRAP
:
5791 focus_close_wrap
= atoi(value
);
5793 case SWM_S_FOCUS_DEFAULT
:
5794 if (!strcmp(value
, "last"))
5795 focus_default
= SWM_STACK_TOP
;
5796 else if (!strcmp(value
, "first"))
5797 focus_default
= SWM_STACK_BOTTOM
;
5799 errx(1, "focus_default");
5801 case SWM_S_FOCUS_MODE
:
5802 if (!strcmp(value
, "default"))
5803 focus_mode
= SWM_FOCUS_DEFAULT
;
5804 else if (!strcmp(value
, "follow_cursor"))
5805 focus_mode
= SWM_FOCUS_FOLLOW
;
5806 else if (!strcmp(value
, "synergy"))
5807 focus_mode
= SWM_FOCUS_SYNERGY
;
5809 errx(1, "focus_mode");
5811 case SWM_S_SPAWN_ORDER
:
5812 if (!strcmp(value
, "first"))
5813 spawn_position
= SWM_STACK_BOTTOM
;
5814 else if (!strcmp(value
, "last"))
5815 spawn_position
= SWM_STACK_TOP
;
5816 else if (!strcmp(value
, "next"))
5817 spawn_position
= SWM_STACK_ABOVE
;
5818 else if (!strcmp(value
, "previous"))
5819 spawn_position
= SWM_STACK_BELOW
;
5821 errx(1, "spawn_position");
5823 case SWM_S_SPAWN_TERM
:
5824 setconfspawn("term", value
, 0);
5825 setconfspawn("spawn_term", value
, 0);
5829 case SWM_S_SS_ENABLED
:
5830 ss_enabled
= atoi(value
);
5832 case SWM_S_STACK_ENABLED
:
5833 stack_enabled
= atoi(value
);
5835 case SWM_S_TERM_WIDTH
:
5836 term_width
= atoi(value
);
5840 case SWM_S_TITLE_CLASS_ENABLED
:
5841 title_class_enabled
= atoi(value
);
5843 case SWM_S_TITLE_NAME_ENABLED
:
5844 title_name_enabled
= atoi(value
);
5846 case SWM_S_URGENT_ENABLED
:
5847 urgent_enabled
= atoi(value
);
5849 case SWM_S_VERBOSE_LAYOUT
:
5850 verbose_layout
= atoi(value
);
5851 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5853 layouts
[i
].l_string
= fancy_stacker
;
5855 layouts
[i
].l_string
= plain_stacker
;
5858 case SWM_S_WINDOW_NAME_ENABLED
:
5859 window_name_enabled
= atoi(value
);
5861 case SWM_S_WORKSPACE_LIMIT
:
5862 workspace_limit
= atoi(value
);
5863 if (workspace_limit
> SWM_WS_MAX
)
5864 workspace_limit
= SWM_WS_MAX
;
5865 else if (workspace_limit
< 1)
5866 workspace_limit
= 1;
5875 setconfmodkey(char *selector
, char *value
, int flags
)
5877 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5878 update_modkey(Mod1Mask
);
5879 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5880 update_modkey(Mod2Mask
);
5881 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5882 update_modkey(Mod3Mask
);
5883 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5884 update_modkey(Mod4Mask
);
5891 setconfcolor(char *selector
, char *value
, int flags
)
5893 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5898 setconfregion(char *selector
, char *value
, int flags
)
5900 custom_region(value
);
5905 setautorun(char *selector
, char *value
, int flags
)
5915 if (getenv("SWM_STARTED"))
5919 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5920 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5922 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5923 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5926 * This is a little intricate
5928 * If the pid already exists we simply reuse it because it means it was
5929 * used before AND not claimed by manage_window. We get away with
5930 * altering it in the parent after INSERT because this can not be a race
5933 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5936 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5938 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5939 err(1, "setautorun: realloc");
5940 a
.argv
[argc
- 1] = ap
;
5943 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5944 err(1, "setautorun: realloc");
5945 a
.argv
[argc
] = NULL
;
5947 if ((pid
= fork()) == 0) {
5948 spawn(ws_id
, &a
, 1);
5957 p
= calloc(1, sizeof *p
);
5960 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5970 setlayout(char *selector
, char *value
, int flags
)
5972 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5973 int st
= SWM_V_STACK
, num_screens
;
5975 struct workspace
*ws
;
5977 if (getenv("SWM_STARTED"))
5981 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5982 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5983 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5984 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5987 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5988 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5990 if (!strcasecmp(s
, "vertical"))
5992 else if (!strcasecmp(s
, "vertical_flip")) {
5995 } else if (!strcasecmp(s
, "horizontal"))
5997 else if (!strcasecmp(s
, "horizontal_flip")) {
6000 } else if (!strcasecmp(s
, "fullscreen"))
6003 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6004 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6007 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6008 for (i
= 0; i
< num_screens
; i
++) {
6009 ws
= (struct workspace
*)&screens
[i
].ws
;
6010 ws
[ws_id
].cur_layout
= &layouts
[st
];
6012 ws
[ws_id
].always_raise
= raise
;
6013 if (st
== SWM_MAX_STACK
)
6017 for (x
= 0; x
< abs(mg
); x
++) {
6018 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6019 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6020 SWM_ARG_ID_MASTERSHRINK
);
6024 for (x
= 0; x
< abs(ma
); x
++) {
6025 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6026 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6027 SWM_ARG_ID_MASTERDEL
);
6031 for (x
= 0; x
< abs(si
); x
++) {
6032 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6033 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6034 SWM_ARG_ID_STACKDEC
);
6039 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6040 SWM_ARG_ID_FLIPLAYOUT
);
6048 /* config options */
6049 struct config_option
{
6051 int (*func
)(char*, char*, int);
6054 struct config_option configopt
[] = {
6055 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6056 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6057 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6058 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6059 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6060 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6061 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6062 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6063 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6064 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6065 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6066 { "keyboard_mapping", setkeymapping
, 0 },
6067 { "bind", setconfbinding
, 0 },
6068 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6069 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6070 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6071 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6072 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6073 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6074 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6075 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6076 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6077 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6078 { "modkey", setconfmodkey
, 0 },
6079 { "program", setconfspawn
, 0 },
6080 { "quirk", setconfquirk
, 0 },
6081 { "region", setconfregion
, 0 },
6082 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6083 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6084 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6085 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6086 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6087 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6088 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6089 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6090 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6091 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6092 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6093 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6094 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6095 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6096 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6097 { "autorun", setautorun
, 0 },
6098 { "layout", setlayout
, 0 },
6103 conf_load(char *filename
, int keymapping
)
6106 char *line
, *cp
, *optsub
, *optval
;
6107 size_t linelen
, lineno
= 0;
6108 int wordlen
, i
, optind
;
6109 struct config_option
*opt
;
6111 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6113 if (filename
== NULL
) {
6114 warnx("conf_load: no filename");
6117 if ((config
= fopen(filename
, "r")) == NULL
) {
6118 warn("conf_load: fopen: %s", filename
);
6122 while (!feof(config
)) {
6123 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6126 err(1, "%s", filename
);
6131 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6132 if (cp
[0] == '\0') {
6137 /* get config option */
6138 wordlen
= strcspn(cp
, "=[ \t\n");
6140 warnx("%s: line %zd: no option found",
6145 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6146 opt
= &configopt
[i
];
6147 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6148 strlen(opt
->optname
) == wordlen
) {
6154 warnx("%s: line %zd: unknown option %.*s",
6155 filename
, lineno
, wordlen
, cp
);
6158 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6159 warnx("%s: line %zd: invalid option %.*s",
6160 filename
, lineno
, wordlen
, cp
);
6164 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6165 /* get [selector] if any */
6169 wordlen
= strcspn(cp
, "]");
6172 warnx("%s: line %zd: syntax error",
6177 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6179 warnx("%s: line %zd: unable to allocate"
6180 "memory for selector", filename
,
6186 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6188 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6190 optval
= strdup(cp
);
6191 /* call function to deal with it all */
6192 if (configopt
[optind
].func(optsub
, optval
,
6193 configopt
[optind
].funcflags
) != 0)
6194 errx(1, "%s: line %zd: invalid data for %s",
6195 filename
, lineno
, configopt
[optind
].optname
);
6202 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6209 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6215 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6217 struct ws_win
*parent
, *w
;
6218 struct swm_region
*r
;
6219 struct workspace
*ws
;
6220 xcb_icccm_wm_hints_t wmh
;
6222 parent
= find_window(win
->transient
);
6224 parent
->child_trans
= win
;
6226 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6227 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6229 r
= root_to_region(win
->wa
->root
);
6231 /* parent doen't exist in our window list */
6232 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6233 if (xcb_icccm_get_wm_hints_reply(conn
,
6234 xcb_icccm_get_wm_hints(conn
, w
->id
),
6236 warnx("can't get hints for 0x%x", w
->id
);
6240 if (win
->hints
.window_group
!= wmh
.window_group
)
6243 w
->child_trans
= win
;
6244 win
->transient
= w
->id
;
6246 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6247 "transient to 0x%x\n", win
->transient
);
6254 window_get_pid(xcb_window_t win
)
6259 xcb_get_property_cookie_t pc
;
6260 xcb_get_property_reply_t
*pr
;
6262 apid
= get_atom_from_string("_NET_WM_PID");
6263 if (apid
== XCB_ATOM_NONE
)
6266 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6267 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6270 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6275 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6281 apid
= get_atom_from_string("_SWM_PID");
6282 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6284 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6287 if (pr
->type
!= XCB_ATOM_STRING
) {
6292 ret
= strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6299 manage_window(xcb_window_t id
)
6301 xcb_window_t trans
= XCB_WINDOW_NONE
;
6302 struct workspace
*ws
;
6303 struct ws_win
*win
, *ww
;
6304 int i
, ws_idx
, border_me
= 0;
6305 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
6306 char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6308 struct swm_region
*r
;
6312 uint32_t event_mask
;
6314 xcb_get_property_reply_t
*gpr
;
6315 xcb_icccm_get_wm_protocols_reply_t wpr
;
6317 if ((win
= find_window(id
)) != NULL
)
6318 return (win
); /* already being managed */
6320 /* see if we are on the unmanaged list */
6321 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6322 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6323 "window: 0x%x\n", win
->id
);
6324 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6326 set_child_transient(win
, &trans
);
6328 if (trans
&& (ww
= find_window(trans
)))
6329 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6330 else if ((ww
= win
->ws
->focus
) &&
6331 spawn_position
== SWM_STACK_ABOVE
)
6332 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6334 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6335 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6336 else switch (spawn_position
) {
6339 case SWM_STACK_ABOVE
:
6340 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6342 case SWM_STACK_BOTTOM
:
6343 case SWM_STACK_BELOW
:
6344 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6347 ewmh_update_actions(win
);
6351 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6352 err(1, "manage_window: calloc: failed to allocate memory for "
6358 /* see if we need to override the workspace */
6359 p
= find_pid(window_get_pid(id
));
6361 /* Get all the window data in one shot */
6362 ws_idx_atom
= get_atom_from_string("_SWM_WS");
6364 gpr
= xcb_get_property_reply(conn
,
6365 xcb_get_property(conn
, False
, id
, ws_idx_atom
,
6366 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6369 proplen
= xcb_get_property_value_length(gpr
);
6371 prop
= malloc(proplen
+ 1);
6374 xcb_get_property_value(gpr
),
6376 prop
[proplen
] = '\0';
6382 win
->wa
= xcb_get_geometry_reply(conn
,
6383 xcb_get_geometry(conn
, id
),
6385 xcb_icccm_get_wm_normal_hints_reply(conn
,
6386 xcb_icccm_get_wm_normal_hints(conn
, id
),
6388 xcb_icccm_get_wm_hints_reply(conn
,
6389 xcb_icccm_get_wm_hints(conn
, id
),
6391 xcb_icccm_get_wm_transient_for_reply(conn
,
6392 xcb_icccm_get_wm_transient_for(conn
, id
),
6395 win
->transient
= trans
;
6396 set_child_transient(win
, &trans
);
6397 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6398 "transient: 0x%x\n", win
->id
, win
->transient
);
6401 prot
= get_atom_from_string("WM_PROTOCOLS");
6403 /* get supported protocols */
6404 if (xcb_icccm_get_wm_protocols_reply(conn
,
6405 xcb_icccm_get_wm_protocols(conn
, id
, prot
),
6407 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6408 if (wpr
.atoms
[i
] == takefocus
)
6409 win
->take_focus
= 1;
6410 if (wpr
.atoms
[i
] == adelete
)
6411 win
->can_delete
= 1;
6413 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6416 win
->iconic
= get_iconic(win
);
6419 * Figure out where to put the window. If it was previously assigned to
6420 * a workspace (either by spawn() or manually moving), and isn't
6421 * transient, * put it in the same workspace
6423 r
= root_to_region(win
->wa
->root
);
6425 ws
= &r
->s
->ws
[p
->ws
];
6426 TAILQ_REMOVE(&pidlist
, p
, entry
);
6429 } else if (prop
&& win
->transient
== 0) {
6430 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6431 ws_idx
= strtonum(prop
, 0, workspace_limit
- 1,
6434 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6437 ws
= &r
->s
->ws
[ws_idx
];
6440 /* this should launch transients in the same ws as parent */
6442 if ((ww
= find_window(trans
)) != NULL
)
6448 warnx("manage_window: fix this "
6454 /* set up the window layout */
6457 win
->s
= r
->s
; /* this never changes */
6458 if (trans
&& (ww
= find_window(trans
)))
6459 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6460 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6461 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6463 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6464 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6465 else switch (spawn_position
) {
6468 case SWM_STACK_ABOVE
:
6469 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6471 case SWM_STACK_BOTTOM
:
6472 case SWM_STACK_BELOW
:
6473 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6476 /* ignore window border if there is one. */
6477 WIDTH(win
) = win
->wa
->width
;
6478 HEIGHT(win
) = win
->wa
->height
;
6479 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6480 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6482 win
->g_floatvalid
= 0;
6483 win
->floatmaxed
= 0;
6484 win
->ewmh_flags
= 0;
6486 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6487 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6488 HEIGHT(win
), ws
->idx
);
6490 constrain_window(win
, r
, 0);
6492 /* Set window properties so we can remember this after reincarnation */
6493 if (ws_idx_atom
&& prop
== NULL
&&
6494 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6496 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6498 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6499 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6505 ewmh_autoquirk(win
);
6507 if (xcb_icccm_get_wm_class_reply(conn
,
6508 xcb_icccm_get_wm_class(conn
, win
->id
),
6510 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6511 win
->ch
.class_name
, win
->ch
.instance_name
);
6513 /* java is retarded so treat it special */
6514 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6519 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6520 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6521 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6522 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6523 "class: %s, name: %s\n", win
->ch
.class_name
,
6524 win
->ch
.instance_name
);
6525 if (qp
->quirk
& SWM_Q_FLOAT
) {
6529 win
->quirks
= qp
->quirk
;
6534 /* alter window position if quirky */
6535 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6536 win
->manual
= 1; /* don't center the quirky windows */
6537 if (bar_enabled
&& Y(win
) < bar_height
)
6538 Y(win
) = bar_height
;
6539 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6540 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6544 /* Reset font sizes (the bruteforce way; no default keybinding). */
6545 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6546 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6547 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6548 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6549 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6552 ewmh_get_win_state(win
);
6553 ewmh_update_actions(win
);
6554 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6559 X(win
) -= border_width
;
6560 Y(win
) -= border_width
;
6564 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6565 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6567 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6569 /* floaters need to be mapped if they are in the current workspace */
6570 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6571 map_window_raised(win
->id
);
6577 free_window(struct ws_win
*win
)
6579 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6584 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6589 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6594 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6597 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6601 unmanage_window(struct ws_win
*win
)
6603 struct ws_win
*parent
;
6604 xcb_screen_t
*screen
;
6609 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6611 if (win
->transient
) {
6612 parent
= find_window(win
->transient
);
6614 parent
->child_trans
= NULL
;
6617 /* focus on root just in case */
6618 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6619 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6620 screen
->root
, XCB_CURRENT_TIME
);
6624 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6625 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6629 focus_magic(struct ws_win
*win
)
6631 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6634 /* if there are no windows clear the status-bar */
6639 if (win
->child_trans
) {
6640 /* win = parent & has a transient so focus on that */
6642 focus_win(win
->child_trans
);
6643 if (win
->child_trans
->take_focus
)
6644 client_msg(win
, takefocus
);
6646 /* make sure transient hasn't disappeared */
6647 if (validate_win(win
->child_trans
) == 0) {
6648 focus_win(win
->child_trans
);
6649 if (win
->child_trans
->take_focus
)
6650 client_msg(win
->child_trans
, takefocus
);
6652 win
->child_trans
= NULL
;
6654 if (win
->take_focus
)
6655 client_msg(win
, takefocus
);
6661 if (win
->take_focus
)
6662 client_msg(win
, takefocus
);
6667 expose(xcb_expose_event_t
*e
)
6670 struct swm_region
*r
;
6672 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6674 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6675 for (i
= 0; i
< num_screens
; i
++)
6676 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6677 if (e
->window
== WINID(r
->bar
))
6684 keypress(xcb_key_press_event_t
*e
)
6688 struct swm_region
*r
;
6690 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)e
->detail
, 0, 0);
6692 DNPRINTF(SWM_D_EVENT
, "keypress: %u\n", e
->detail
);
6694 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6696 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6699 r
= root_to_region(e
->root
);
6700 if (kp
->funcid
== kf_spawn_custom
)
6701 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6703 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6707 buttonpress(xcb_button_press_event_t
*e
)
6712 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
6713 e
->event
, e
->detail
);
6715 if ((win
= find_window(e
->event
)) == NULL
)
6719 action
= client_click
;
6721 for (i
= 0; i
< LENGTH(buttons
); i
++)
6722 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6723 buttons
[i
].button
== e
->detail
&&
6724 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6725 buttons
[i
].func(win
, &buttons
[i
].args
);
6731 configurerequest(xcb_configure_request_event_t
*e
)
6736 uint32_t wc
[7] = {0};
6738 if ((win
= find_window(e
->window
)) == NULL
)
6739 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6743 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6744 mask
|= XCB_CONFIG_WINDOW_X
;
6747 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6748 mask
|= XCB_CONFIG_WINDOW_Y
;
6751 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6752 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6755 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6756 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6757 wc
[i
++] = e
->height
;
6759 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6760 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6761 wc
[i
++] = e
->border_width
;
6763 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6764 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6765 wc
[i
++] = e
->sibling
;
6767 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6768 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6769 wc
[i
++] = e
->stack_mode
;
6772 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6773 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e
->window
,
6774 YESNO(new), wc
[0], wc
[1], wc
[2], wc
[3]);
6776 xcb_configure_window(conn
, e
->window
, mask
, wc
);
6777 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6778 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
6779 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
6780 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
6781 win
->g_float
.w
= e
->width
;
6782 win
->g_float
.h
= e
->height
;
6783 win
->g_floatvalid
= 1;
6785 if (win
->floating
) {
6786 win
->g
= win
->g_float
;
6787 win
->g
.x
+= X(win
->ws
->r
);
6788 win
->g
.y
+= Y(win
->ws
->r
);
6801 configurenotify(xcb_configure_notify_event_t
*e
)
6805 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
6808 win
= find_window(e
->window
);
6810 xcb_icccm_get_wm_normal_hints_reply(conn
,
6811 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6820 destroynotify(xcb_destroy_notify_event_t
*e
)
6824 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
6826 if ((win
= find_window(e
->window
)) == NULL
) {
6827 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6833 /* make sure we focus on something */
6836 unmanage_window(win
);
6842 enternotify(xcb_enter_notify_event_t
*e
)
6847 Window focus_return
;
6848 int revert_to_return
;
6850 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %d, detail: "
6851 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6852 "state: %d\n", e
->event
, e
->mode
, e
->detail
, e
->root
,
6853 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
6855 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
6856 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6861 switch (focus_mode
) {
6862 case SWM_FOCUS_DEFAULT
:
6864 case SWM_FOCUS_FOLLOW
:
6866 case SWM_FOCUS_SYNERGY
:
6869 * all these checks need to be in this order because the
6870 * XCheckTypedWindowEvent relies on weeding out the previous events
6872 * making this code an option would enable a follow mouse for focus
6877 * state is set when we are switching workspaces and focus is set when
6878 * the window or a subwindow already has focus (occurs during restart).
6880 * Only honor the focus flag if last_focus_event is not FocusOut,
6881 * this allows spectrwm to continue to control focus when another
6882 * program is also playing with it.
6884 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6885 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6890 * happens when a window is created or destroyed and the border
6891 * crosses the mouse pointer and when switching ws
6893 * we need the subwindow test to see if we came from root in order
6894 * to give focus to floaters
6896 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6897 ev
->subwindow
== 0) {
6898 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6902 /* this window already has focus */
6903 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6904 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6908 /* this window is being deleted or moved to another ws */
6909 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6911 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6912 XPutBackEvent(display
, &cne
);
6916 if ((win
= find_window(ev
->window
)) == NULL
) {
6917 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6922 * In fullstack kill all enters unless they come from a different ws
6923 * (i.e. another region) or focus has been grabbed externally.
6925 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6926 last_focus_event
!= FocusOut
) {
6927 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6928 if ((w
= find_window(focus_return
)) == NULL
||
6930 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6938 if ((win
= find_window(e
->event
)) == NULL
) {
6939 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6948 /* lets us use one switch statement for arbitrary mode/detail combinations */
6949 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6952 mapnotify(xcb_map_notify_event_t
*e
)
6956 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
6958 win
= manage_window(e
->window
);
6960 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
6963 * focus_win can only set input focus on a mapped window.
6964 * make sure the window really has focus since it is just being mapped.
6966 if (win
->ws
->focus
== win
)
6971 mappingnotify(xcb_mapping_notify_event_t
*e
)
6973 xcb_refresh_keyboard_mapping(syms
, e
);
6975 if (e
->request
== XCB_MAPPING_KEYBOARD
)
6980 maprequest(xcb_map_request_event_t
*e
)
6983 struct swm_region
*r
;
6984 xcb_get_window_attributes_reply_t
*war
;
6986 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
6989 war
= xcb_get_window_attributes_reply(conn
,
6990 xcb_get_window_attributes(conn
, e
->window
),
6994 if (war
->override_redirect
) {
7000 win
= manage_window(e
->window
);
7002 return; /* can't happen */
7007 /* make new win focused */
7008 r
= root_to_region(win
->wa
->root
);
7009 if (win
->ws
== r
->ws
)
7014 propertynotify(xcb_property_notify_event_t
*e
)
7020 xcb_get_atom_name_reply_t
*r
;
7022 r
= xcb_get_atom_name_reply(conn
,
7023 xcb_get_atom_name(conn
, e
->atom
),
7026 len
= xcb_get_atom_name_name_length(r
);
7028 name
= malloc(len
+ 1);
7030 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7033 DNPRINTF(SWM_D_EVENT
,
7034 "propertynotify: window: 0x%x, atom: %s\n",
7043 win
= find_window(e
->window
);
7047 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7048 update_iconic(win
, 0);
7049 map_window_raised(win
->id
);
7057 case XCB_ATOM_WM_NORMAL_HINTS
:
7058 xcb_icccm_get_wm_normal_hints(conn
,
7059 xcb_iccom_get_wm_normal_hints(conn
, win
->id
),
7061 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
7062 if (win
->sh
.flags
& XCB_SIZE_HINT_P_MIN_SIZE
) {
7063 WIDTH(win
) = win
->sh
.min_width
;
7064 HEIGHT(win
) = win
->sh
.min_height
;
7065 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
7067 XMoveResizeWindow(display
, win
->id
,
7068 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
7070 case XCB_ATOM_WM_CLASS
:
7071 case XCB_ATOM_WM_NAME
:
7080 unmapnotify(xcb_unmap_notify_event_t
*e
)
7084 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7086 /* determine if we need to help unmanage this window */
7087 win
= find_window(e
->window
);
7091 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7092 unmanage_window(win
);
7095 /* resend unmap because we ated it */
7096 xcb_unmap_window(conn
, e
->window
);
7102 visibilitynotify(xcb_visibility_notify_event_t *e)
7104 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7109 clientmessage(xcb_client_message_event_t
*e
)
7113 win
= find_window(e
->window
);
7118 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, type: %u\n",
7119 e
->window
, e
->type
);
7121 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7122 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7125 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7126 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7127 if (win
->can_delete
)
7128 client_msg(win
, adelete
);
7130 xcb_kill_client(conn
, win
->id
);
7132 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7133 DNPRINTF(SWM_D_EVENT
,
7134 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7135 if (win
->floating
) {
7136 if (e
->data
.data32
[0] & (1<<8)) /* x */
7137 X(win
) = e
->data
.data32
[1];
7138 if (e
->data
.data32
[0] & (1<<9)) /* y */
7139 Y(win
) = e
->data
.data32
[2];
7140 if (e
->data
.data32
[0] & (1<<10)) /* width */
7141 WIDTH(win
) = e
->data
.data32
[3];
7142 if (e
->data
.data32
[0] & (1<<11)) /* height */
7143 HEIGHT(win
) = e
->data
.data32
[4];
7148 /* TODO: Change stack sizes */
7149 /* notify no change was made. */
7150 config_win(win
, NULL
);
7153 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7154 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7155 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7156 if (e
->data
.data32
[2])
7157 ewmh_update_win_state(win
, e
->data
.data32
[2],
7167 xerror_start(Display
*d
, XErrorEvent
*ee
)
7174 xerror(Display
*d
, XErrorEvent
*ee
)
7176 /* warnx("error: %p %p", display, ee); */
7184 xerrorxlib
= XSetErrorHandler(xerror_start
);
7186 /* this causes an error if some other window manager is running */
7187 XSelectInput(display
, DefaultRootWindow(display
),
7188 SubstructureRedirectMask
);
7193 XSetErrorHandler(xerror
);
7199 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7201 struct swm_region
*r
, *n
;
7202 struct workspace
*ws
= NULL
;
7205 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7206 s
->idx
, w
, h
, x
, y
);
7208 /* remove any conflicting regions */
7209 n
= TAILQ_FIRST(&s
->rl
);
7212 n
= TAILQ_NEXT(r
, entry
);
7213 if (X(r
) < (x
+ w
) &&
7214 (X(r
) + WIDTH(r
)) > x
&&
7216 (Y(r
) + HEIGHT(r
)) > y
) {
7217 if (r
->ws
->r
!= NULL
)
7218 r
->ws
->old_r
= r
->ws
->r
;
7221 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7222 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7226 /* search old regions for one to reuse */
7228 /* size + location match */
7229 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7230 if (X(r
) == x
&& Y(r
) == y
&&
7231 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7235 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7236 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7240 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7241 /* try to use old region's workspace */
7242 if (r
->ws
->r
== NULL
)
7245 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7246 err(1, "new_region: calloc: failed to allocate memory "
7249 /* if we don't have a workspace already, find one */
7251 for (i
= 0; i
< workspace_limit
; i
++)
7252 if (s
->ws
[i
].r
== NULL
) {
7259 errx(1, "new_region: no free workspaces");
7270 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7276 #ifdef SWM_XRR_HAS_CRTC
7279 #endif /* SWM_XRR_HAS_CRTC */
7280 struct swm_region
*r
;
7282 xcb_randr_get_screen_resources_current_cookie_t src
;
7283 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7284 xcb_randr_get_crtc_info_cookie_t cic
;
7285 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7286 xcb_randr_crtc_t
*crtc
;
7287 xcb_screen_t
*screen
= get_screen(i
);
7289 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7290 if (i
>= num_screens
)
7291 errx(1, "scan_xrandr: invalid screen");
7293 /* remove any old regions */
7294 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7295 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7297 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7298 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7302 /* map virtual screens onto physical screens */
7303 #ifdef SWM_XRR_HAS_CRTC
7304 if (xrandr_support
) {
7305 src
= xcb_randr_get_screen_resources_current(conn
,
7307 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7310 new_region(&screens
[i
], 0, 0,
7311 screen
->width_in_pixels
,
7312 screen
->height_in_pixels
);
7315 ncrtc
= srr
->num_crtcs
;
7316 for (c
= 0; c
< ncrtc
; c
++) {
7317 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7318 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7320 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7323 if (cir
->num_outputs
== 0) {
7329 new_region(&screens
[i
], 0, 0,
7330 screen
->width_in_pixels
,
7331 screen
->height_in_pixels
);
7333 new_region(&screens
[i
],
7334 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7339 #endif /* SWM_XRR_HAS_CRTC */
7341 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7342 screen
->height_in_pixels
);
7347 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7349 struct swm_region
*r
;
7352 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7354 if (e
->rotation
& (XCB_RANDR_ROTATION_ROTATE_90
7355 | XCB_RANDR_ROTATION_ROTATE_270
))
7356 xcb_randr_set_screen_size(conn
, e
->root
, e
->height
,
7357 e
->width
, e
->mheight
, e
->mwidth
);
7359 xcb_randr_set_screen_size(conn
, e
->root
, e
->width
,
7360 e
->height
, e
->mwidth
, e
->mheight
);
7362 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7363 /* silly event doesn't include the screen index */
7364 for (i
= 0; i
< num_screens
; i
++)
7365 if (screens
[i
].root
== e
->root
)
7367 if (i
>= num_screens
)
7368 errx(1, "screenchange: screen not found");
7370 /* brute force for now, just re-enumerate the regions */
7373 /* add bars to all regions */
7374 for (i
= 0; i
< num_screens
; i
++)
7375 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7383 xcb_window_t
*wins
= NULL
;
7385 int i
, j
, num_screens
;
7386 uint16_t state
, manage
;
7388 xcb_query_tree_cookie_t qtc
;
7389 xcb_query_tree_reply_t
*qtr
;
7390 xcb_get_window_attributes_cookie_t c
;
7391 xcb_get_window_attributes_reply_t
*r
;
7392 xcb_get_property_cookie_t pc
;
7394 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7395 for (i
= 0; i
< num_screens
; i
++) {
7396 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7397 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7400 wins
= xcb_query_tree_children(qtr
);
7401 no
= xcb_query_tree_children_length(qtr
);
7402 /* attach windows to a region */
7403 /* normal windows */
7404 for (j
= 0; j
< no
; j
++) {
7405 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7406 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7409 if (r
->override_redirect
) {
7414 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7415 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7421 state
= getstate(wins
[j
]);
7422 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7423 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7424 manage_window(wins
[j
]);
7427 /* transient windows */
7428 for (j
= 0; j
< no
; j
++) {
7429 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7430 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7433 if (r
->override_redirect
) {
7439 state
= getstate(wins
[j
]);
7440 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7441 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7442 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7443 &wins
[j
], NULL
) && manage
)
7444 manage_window(wins
[j
]);
7453 int i
, j
, k
, num_screens
;
7454 struct workspace
*ws
;
7455 uint32_t gcv
[1], wa
[1];
7456 const xcb_query_extension_reply_t
*qep
;
7457 xcb_cursor_t cursor
;
7458 xcb_font_t cursor_font
;
7459 xcb_randr_query_version_cookie_t c
;
7460 xcb_randr_query_version_reply_t
*r
;
7462 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7463 if ((screens
= calloc(num_screens
,
7464 sizeof(struct swm_screen
))) == NULL
)
7465 err(1, "setup_screens: calloc: failed to allocate memory for "
7468 /* initial Xrandr setup */
7469 xrandr_support
= False
;
7470 c
= xcb_randr_query_version(conn
, True
, True
);
7471 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7473 if (r
->major_version
>= 1)
7474 xrandr_support
= True
;
7477 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7478 xrandr_eventbase
= qep
->first_event
;
7480 cursor_font
= xcb_generate_id(conn
);
7481 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7483 cursor
= xcb_generate_id(conn
);
7484 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7485 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7488 /* map physical screens */
7489 for (i
= 0; i
< num_screens
; i
++) {
7490 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7492 TAILQ_INIT(&screens
[i
].rl
);
7493 TAILQ_INIT(&screens
[i
].orl
);
7494 screens
[i
].root
= RootWindow(display
, i
);
7496 /* set default colors */
7497 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7498 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7499 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7500 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7501 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7503 /* create graphics context on screen */
7504 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7506 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7507 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7509 /* set default cursor */
7510 xcb_change_window_attributes(conn
, screens
[i
].root
,
7513 /* init all workspaces */
7514 /* XXX these should be dynamically allocated too */
7515 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7516 ws
= &screens
[i
].ws
[j
];
7522 TAILQ_INIT(&ws
->winlist
);
7523 TAILQ_INIT(&ws
->unmanagedlist
);
7525 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7526 if (layouts
[k
].l_config
!= NULL
)
7527 layouts
[k
].l_config(ws
,
7528 SWM_ARG_ID_STACKINIT
);
7529 ws
->cur_layout
= &layouts
[0];
7530 ws
->cur_layout
->l_string(ws
);
7536 xcb_randr_select_input(conn
, screens
[i
].root
,
7537 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7539 xcb_free_cursor(conn
, cursor
);
7540 xcb_close_font(conn
, cursor_font
);
7546 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7547 err(1, "setup_globals: strdup: failed to allocate memory.");
7549 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7550 err(1, "setup_globals: strdup: failed to allocate memory.");
7557 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7558 xcb_window_t root
, win
;
7561 /* work around sun jdk bugs, code from wmname */
7562 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7563 netwmname
= get_atom_from_string("_NET_WM_NAME");
7564 utf8_string
= get_atom_from_string("UTF8_STRING");
7566 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7567 for (i
= 0; i
< num_screens
; i
++) {
7568 root
= screens
[i
].root
;
7570 win
= xcb_generate_id(conn
);
7571 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7572 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7573 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7574 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7575 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7577 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7578 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7579 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7580 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7581 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7582 netwmname
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7587 event_error(xcb_generic_error_t
*e
)
7589 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7590 "resource_id: %u, minor_code: %u\n",
7591 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7592 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7593 e
->sequence
, e
->resource_id
, e
->minor_code
);
7597 event_handle(xcb_generic_event_t
*evt
)
7599 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7601 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7602 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7603 XCB_EVENT_RESPONSE_TYPE(evt
));
7606 #define EVENT(type, callback) case type: callback((void *)evt); return
7607 EVENT(0, event_error
);
7608 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7609 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7610 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7611 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7612 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7613 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7614 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7615 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7616 /*EVENT(XCB_CREATE_NOTIFY, );*/
7617 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7618 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7619 EVENT(XCB_EXPOSE
, expose
);
7620 /*EVENT(XCB_FOCUS_IN, );*/
7621 /*EVENT(XCB_FOCUS_OUT, );*/
7622 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7623 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7624 EVENT(XCB_KEY_PRESS
, keypress
);
7625 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7626 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7627 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7628 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7629 EVENT(XCB_MAP_REQUEST
, maprequest
);
7630 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7631 /*EVENT(XCB_MOTION_NOTIFY, );*/
7632 /*EVENT(XCB_NO_EXPOSURE, );*/
7633 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7634 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7635 /*EVENT(XCB_RESIZE_REQUEST, );*/
7636 /*EVENT(XCB_SELECTION_CLEAR, );*/
7637 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7638 /*EVENT(XCB_SELECTION_REQUEST, );*/
7639 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7640 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7643 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7644 screenchange((void *)evt
);
7648 main(int argc
, char *argv
[])
7650 struct swm_region
*r
, *rr
;
7651 struct ws_win
*winfocus
= NULL
;
7653 char conf
[PATH_MAX
], *cfile
= NULL
;
7655 int xfd
, i
, num_screens
;
7656 struct sigaction sact
;
7657 xcb_generic_event_t
*evt
;
7661 time_started
= time(NULL
);
7664 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7665 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7667 warnx("no locale support");
7669 if (!X_HAVE_UTF8_STRING
)
7670 warnx("no UTF-8 support");
7672 if (!(display
= XOpenDisplay(0)))
7673 errx(1, "can not open display");
7675 /* handle some signals */
7676 bzero(&sact
, sizeof(sact
));
7677 sigemptyset(&sact
.sa_mask
);
7679 sact
.sa_handler
= sighdlr
;
7680 sigaction(SIGINT
, &sact
, NULL
);
7681 sigaction(SIGQUIT
, &sact
, NULL
);
7682 sigaction(SIGTERM
, &sact
, NULL
);
7683 sigaction(SIGHUP
, &sact
, NULL
);
7685 sact
.sa_handler
= sighdlr
;
7686 sact
.sa_flags
= SA_NOCLDSTOP
;
7687 sigaction(SIGCHLD
, &sact
, NULL
);
7689 conn
= XGetXCBConnection(display
);
7690 if (xcb_connection_has_error(conn
))
7691 errx(1, "can not get XCB connection");
7693 xfd
= xcb_get_file_descriptor(conn
);
7694 syms
= xcb_key_symbols_alloc(conn
);
7696 errx(1, "unable to allocate key symbols");
7698 astate
= get_atom_from_string("WM_STATE");
7699 aprot
= get_atom_from_string("WM_PROTOCOLS");
7700 adelete
= get_atom_from_string("WM_DELETE_WINDOW");
7701 takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7702 a_wmname
= get_atom_from_string("WM_NAME");
7703 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7704 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7705 a_string
= get_atom_from_string("STRING");
7706 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7708 /* look for local and global conf file */
7709 pwd
= getpwuid(getuid());
7711 errx(1, "invalid user: %d", getuid());
7713 xcb_grab_server(conn
);
7716 /* flush all events */
7717 while ((evt
= xcb_poll_for_event(conn
))) {
7718 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
7724 errx(1, "other wm running");
7735 for (i
= 0; ; i
++) {
7740 snprintf(conf
, sizeof conf
, "%s/.%s",
7741 pwd
->pw_dir
, SWM_CONF_FILE
);
7745 snprintf(conf
, sizeof conf
, "/etc/%s",
7750 snprintf(conf
, sizeof conf
, "%s/.%s",
7751 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7755 snprintf(conf
, sizeof conf
, "/etc/%s",
7762 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7763 if (S_ISREG(sb
.st_mode
)) {
7770 /* load conf (if any) */
7772 conf_load(cfile
, SWM_CONF_DEFAULT
);
7775 /* set some values to work around bad programs */
7777 /* grab existing windows (before we build the bars) */
7780 if (getenv("SWM_STARTED") == NULL
)
7781 setenv("SWM_STARTED", "YES", 1);
7783 /* setup all bars */
7784 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7785 for (i
= 0; i
< num_screens
; i
++)
7786 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7787 if (winfocus
== NULL
)
7788 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7797 xcb_ungrab_server(conn
);
7801 while ((evt
= xcb_poll_for_event(conn
))) {
7808 /* if we are being restarted go focus on first window */
7810 rr
= winfocus
->ws
->r
;
7812 /* not a visible window */
7816 /* move pointer to first screen if multi screen */
7817 if (num_screens
> 1 || outputs
> 1)
7818 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7819 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7820 Y(rr
) + (bar_enabled
? bar_height
: 0));
7822 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7832 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7834 DNPRINTF(SWM_D_MISC
, "select failed");
7835 if (restart_wm
== 1)
7836 restart(NULL
, NULL
);
7837 if (search_resp
== 1)
7850 for (i
= 0; i
< num_screens
; ++i
)
7851 if (screens
[i
].bar_gc
!= 0)
7852 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7853 XFreeFontSet(display
, bar_fs
);
7855 xcb_key_symbols_free(syms
);
7856 xcb_disconnect(conn
);