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 { if (swm_debug) fprintf(stderr, x); } while (0)
160 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
161 #define SWM_D_MISC 0x0001
162 #define SWM_D_EVENT 0x0002
163 #define SWM_D_WS 0x0004
164 #define SWM_D_FOCUS 0x0008
165 #define SWM_D_MOVE 0x0010
166 #define SWM_D_STACK 0x0020
167 #define SWM_D_MOUSE 0x0040
168 #define SWM_D_PROP 0x0080
169 #define SWM_D_CLASS 0x0100
170 #define SWM_D_KEY 0x0200
171 #define SWM_D_QUIRK 0x0400
172 #define SWM_D_SPAWN 0x0800
173 #define SWM_D_EVENTQ 0x1000
174 #define SWM_D_CONF 0x2000
175 #define SWM_D_BAR 0x4000
177 u_int32_t swm_debug = 0
195 #define DPRINTF(x...)
196 #define DNPRINTF(n,x...)
199 #define LENGTH(x) (sizeof x / sizeof x[0])
200 #define MODKEY Mod1Mask
201 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
202 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
203 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
204 #define SWM_PROPLEN (16)
205 #define SWM_FUNCNAME_LEN (32)
206 #define SWM_KEYS_LEN (255)
207 #define SWM_QUIRK_LEN (64)
208 #define X(r) (r)->g.x
209 #define Y(r) (r)->g.y
210 #define WIDTH(r) (r)->g.w
211 #define HEIGHT(r) (r)->g.h
212 #define BORDER(w) (w->bordered ? border_width : 0)
213 #define MAX_X(r) ((r)->g.x + (r)->g.w)
214 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
215 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
216 #define SH_MIN_W(w) (w)->sh.min_width
217 #define SH_MIN_H(w) (w)->sh.min_height
218 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
219 #define SH_MAX_W(w) (w)->sh.max_width
220 #define SH_MAX_H(w) (w)->sh.max_height
221 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
222 #define SH_INC_W(w) (w)->sh.width_inc
223 #define SH_INC_H(w) (w)->sh.height_inc
224 #define SWM_MAX_FONT_STEPS (3)
225 #define WINID(w) ((w) ? (w)->id : 0)
226 #define YESNO(x) ((x) ? "yes" : "no")
228 #define SWM_FOCUS_DEFAULT (0)
229 #define SWM_FOCUS_SYNERGY (1)
230 #define SWM_FOCUS_FOLLOW (2)
232 #define SWM_CONF_DEFAULT (0)
233 #define SWM_CONF_KEYMAPPING (1)
236 #define SWM_LIB "/usr/local/lib/libswmhack.so"
243 xcb_atom_t takefocus;
245 xcb_atom_t a_netwmname;
246 xcb_atom_t a_utf8_string;
248 xcb_atom_t a_swm_iconic;
249 volatile sig_atomic_t running = 1;
250 volatile sig_atomic_t restart_wm = 0;
252 int last_focus_event = FocusOut;
253 int (*xerrorxlib)(Display *, XErrorEvent *);
257 int xrandr_eventbase;
258 unsigned int numlockmask = 0;
260 xcb_connection_t *conn;
261 xcb_key_symbols_t *syms;
264 int cycle_visible = 0;
266 int font_adjusted = 0;
267 unsigned int mod_key = MODKEY;
270 struct swm_region *search_r;
271 int select_list_pipe[2];
272 int select_resp_pipe[2];
274 volatile sig_atomic_t search_resp;
275 int search_resp_action;
277 struct search_window {
278 TAILQ_ENTRY(search_window) entry;
282 xcb_window_t indicator;
284 TAILQ_HEAD(search_winlist, search_window);
285 struct search_winlist search_wl;
290 SWM_SEARCH_UNICONIFY,
291 SWM_SEARCH_NAME_WORKSPACE,
292 SWM_SEARCH_SEARCH_WORKSPACE,
293 SWM_SEARCH_SEARCH_WINDOW
296 #define SWM_STACK_TOP (0)
297 #define SWM_STACK_BOTTOM (1)
298 #define SWM_STACK_ABOVE (2)
299 #define SWM_STACK_BELOW (3)
302 double dialog_ratio = 0.6;
304 #define SWM_BAR_MAX (256)
305 #define SWM_BAR_JUSTIFY_LEFT (0)
306 #define SWM_BAR_JUSTIFY_CENTER (1)
307 #define SWM_BAR_JUSTIFY_RIGHT (2)
308 #define SWM_BAR_OFFSET (4)
309 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
310 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
311 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
312 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
313 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
315 char *bar_argv[] = { NULL, NULL };
317 unsigned char bar_ext[SWM_BAR_MAX];
318 char bar_vertext[SWM_BAR_MAX];
320 sig_atomic_t bar_alarm = 0;
323 int bar_border_width = 1;
324 int bar_at_bottom = 0;
326 int bar_extra_running = 0;
329 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
330 char *bar_format = NULL;
331 int stack_enabled = 1;
332 int clock_enabled = 1;
333 int urgent_enabled = 0;
334 char *clock_format = NULL;
335 int title_name_enabled = 0;
336 int title_class_enabled = 0;
337 int window_name_enabled = 0;
338 int focus_mode = SWM_FOCUS_DEFAULT;
339 int focus_close = SWM_STACK_BELOW;
340 int focus_close_wrap = 1;
341 int focus_default = SWM_STACK_TOP;
342 int spawn_position = SWM_STACK_TOP;
343 int disable_border = 0;
344 int border_width = 1;
345 int verbose_layout = 0;
348 XFontSetExtents *bar_fs_extents;
352 #define SWM_MENU_FN (2)
353 #define SWM_MENU_NB (4)
354 #define SWM_MENU_NF (6)
355 #define SWM_MENU_SB (8)
356 #define SWM_MENU_SF (10)
358 /* layout manager data */
359 struct swm_geometry {
372 struct swm_geometry g;
375 /* virtual "screens" */
377 TAILQ_ENTRY(swm_region) entry;
378 struct swm_geometry g;
379 struct workspace *ws; /* current workspace on this region */
380 struct workspace *ws_prior; /* prior workspace on this region */
381 struct swm_screen *s; /* screen idx */
384 TAILQ_HEAD(swm_region_list, swm_region);
387 TAILQ_ENTRY(ws_win) entry;
389 xcb_window_t transient;
390 struct ws_win *child_trans; /* transient child window */
391 struct swm_geometry g; /* current geometry */
392 struct swm_geometry g_float; /* region coordinates */
393 int g_floatvalid; /* g_float geometry validity */
394 int floatmaxed; /* whether maxed by max_stack */
399 unsigned int ewmh_flags;
400 int font_size_boundary[SWM_MAX_FONT_STEPS];
406 unsigned long quirks;
407 struct workspace *ws; /* always valid */
408 struct swm_screen *s; /* always valid, never changes */
409 xcb_get_geometry_reply_t *wa;
411 xcb_icccm_get_wm_class_reply_t ch;
412 xcb_icccm_wm_hints_t hints;
414 TAILQ_HEAD(ws_win_list, ws_win);
418 TAILQ_ENTRY(pid_e) entry;
422 TAILQ_HEAD(pid_list, pid_e);
423 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
425 /* layout handlers */
427 void vertical_config(struct workspace *, int);
428 void vertical_stack(struct workspace *, struct swm_geometry *);
429 void horizontal_config(struct workspace *, int);
430 void horizontal_stack(struct workspace *, struct swm_geometry *);
431 void max_stack(struct workspace *, struct swm_geometry *);
432 void plain_stacker(struct workspace *);
433 void fancy_stacker(struct workspace *);
435 struct ws_win *find_window(xcb_window_t);
437 void grabbuttons(struct ws_win *, int);
438 void new_region(struct swm_screen *, int, int, int, int);
439 void unmanage_window(struct ws_win *);
440 uint16_t getstate(xcb_window_t);
442 int conf_load(char *, int);
445 void (*l_stack)(struct workspace *, struct swm_geometry *);
446 void (*l_config)(struct workspace *, int);
448 #define SWM_L_FOCUSPREV (1<<0)
449 #define SWM_L_MAPONFOCUS (1<<1)
450 void (*l_string)(struct workspace *);
452 /* stack, configure */
453 { vertical_stack, vertical_config, 0, plain_stacker },
454 { horizontal_stack, horizontal_config, 0, plain_stacker },
456 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
457 { NULL, NULL, 0, NULL },
460 /* position of max_stack mode in the layouts array, index into layouts! */
461 #define SWM_V_STACK (0)
462 #define SWM_H_STACK (1)
463 #define SWM_MAX_STACK (2)
465 #define SWM_H_SLICE (32)
466 #define SWM_V_SLICE (32)
468 /* define work spaces */
470 int idx; /* workspace index */
471 char *name; /* workspace name */
472 int always_raise; /* raise windows on focus */
473 struct layout *cur_layout; /* current layout handlers */
474 struct ws_win *focus; /* may be NULL */
475 struct ws_win *focus_prev; /* may be NULL */
476 struct swm_region *r; /* may be NULL */
477 struct swm_region *old_r; /* may be NULL */
478 struct ws_win_list winlist; /* list of windows in ws */
479 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
480 char stacker[10]; /* display stacker and layout */
484 int horizontal_msize;
486 int horizontal_stacks;
497 SWM_S_COLOR_BAR_BORDER,
498 SWM_S_COLOR_BAR_FONT,
504 /* physical screen mapping */
505 #define SWM_WS_MAX (22) /* hard limit */
506 int workspace_limit = 10; /* soft limit */
509 int idx; /* screen index */
510 struct swm_region_list rl; /* list of regions on this screen */
511 struct swm_region_list orl; /* list of old regions */
513 struct workspace ws[SWM_WS_MAX];
519 } c[SWM_S_COLOR_MAX];
521 xcb_gcontext_t bar_gc;
523 struct swm_screen *screens;
525 /* args to functions */
528 #define SWM_ARG_ID_FOCUSNEXT (0)
529 #define SWM_ARG_ID_FOCUSPREV (1)
530 #define SWM_ARG_ID_FOCUSMAIN (2)
531 #define SWM_ARG_ID_FOCUSCUR (4)
532 #define SWM_ARG_ID_SWAPNEXT (10)
533 #define SWM_ARG_ID_SWAPPREV (11)
534 #define SWM_ARG_ID_SWAPMAIN (12)
535 #define SWM_ARG_ID_MOVELAST (13)
536 #define SWM_ARG_ID_MASTERSHRINK (20)
537 #define SWM_ARG_ID_MASTERGROW (21)
538 #define SWM_ARG_ID_MASTERADD (22)
539 #define SWM_ARG_ID_MASTERDEL (23)
540 #define SWM_ARG_ID_FLIPLAYOUT (24)
541 #define SWM_ARG_ID_STACKRESET (30)
542 #define SWM_ARG_ID_STACKINIT (31)
543 #define SWM_ARG_ID_CYCLEWS_UP (40)
544 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
545 #define SWM_ARG_ID_CYCLESC_UP (42)
546 #define SWM_ARG_ID_CYCLESC_DOWN (43)
547 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
548 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
549 #define SWM_ARG_ID_STACKINC (50)
550 #define SWM_ARG_ID_STACKDEC (51)
551 #define SWM_ARG_ID_SS_ALL (60)
552 #define SWM_ARG_ID_SS_WINDOW (61)
553 #define SWM_ARG_ID_DONTCENTER (70)
554 #define SWM_ARG_ID_CENTER (71)
555 #define SWM_ARG_ID_KILLWINDOW (80)
556 #define SWM_ARG_ID_DELETEWINDOW (81)
557 #define SWM_ARG_ID_WIDTHGROW (90)
558 #define SWM_ARG_ID_WIDTHSHRINK (91)
559 #define SWM_ARG_ID_HEIGHTGROW (92)
560 #define SWM_ARG_ID_HEIGHTSHRINK (93)
561 #define SWM_ARG_ID_MOVEUP (100)
562 #define SWM_ARG_ID_MOVEDOWN (101)
563 #define SWM_ARG_ID_MOVELEFT (102)
564 #define SWM_ARG_ID_MOVERIGHT (103)
568 void focus(struct swm_region *, union arg *);
569 void focus_magic(struct ws_win *);
573 TAILQ_ENTRY(quirk) entry;
577 #define SWM_Q_FLOAT (1<<0) /* float this window */
578 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
579 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
580 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
581 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
582 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
584 TAILQ_HEAD(quirk_list, quirk);
585 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
588 * Supported EWMH hints should be added to
589 * both the enum and the ewmh array
594 _NET_MOVERESIZE_WINDOW,
595 _NET_WM_ACTION_CLOSE,
596 _NET_WM_ACTION_FULLSCREEN,
598 _NET_WM_ACTION_RESIZE,
599 _NET_WM_ALLOWED_ACTIONS,
602 _NET_WM_STATE_FULLSCREEN,
603 _NET_WM_STATE_HIDDEN,
604 _NET_WM_STATE_MAXIMIZED_HORZ,
605 _NET_WM_STATE_MAXIMIZED_VERT,
606 _NET_WM_STATE_SKIP_PAGER,
607 _NET_WM_STATE_SKIP_TASKBAR,
609 _NET_WM_WINDOW_TYPE_DIALOG,
610 _NET_WM_WINDOW_TYPE_DOCK,
611 _NET_WM_WINDOW_TYPE_NORMAL,
612 _NET_WM_WINDOW_TYPE_SPLASH,
613 _NET_WM_WINDOW_TYPE_TOOLBAR,
614 _NET_WM_WINDOW_TYPE_UTILITY,
615 _SWM_WM_STATE_MANUAL,
622 } ewmh[SWM_EWMH_HINT_MAX] = {
623 /* must be in same order as in the enum */
624 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
625 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
626 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
627 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
628 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
629 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
630 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
631 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
632 {"_NET_WM_STATE", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
637 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
638 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
639 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
644 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
645 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
646 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
647 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
650 void store_float_geom(struct ws_win *, struct swm_region *);
651 int floating_toggle_win(struct ws_win *);
652 void constrain_window(struct ws_win *, struct swm_region *, int);
653 void update_window(struct ws_win *);
654 void spawn_select(struct swm_region *, union arg *, char *, int *);
655 char *get_win_name(xcb_window_t);
656 xcb_atom_t get_atom_from_string(const char *);
657 void map_window_raised(xcb_window_t);
659 xcb_screen_t *get_screen(int);
660 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
661 void event_handle(xcb_generic_event_t *);
664 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
666 unsigned int tmpr, tmpg, tmpb;
668 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
679 get_screen(int screen)
681 xcb_screen_iterator_t i;
683 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
684 for (; i.rem; --screen, xcb_screen_next(&i))
694 xcb_get_input_focus_cookie_t c;
695 xcb_get_input_focus_reply_t *r;
698 c = xcb_get_input_focus(conn);
700 r = xcb_get_input_focus_reply(conn, c, NULL);
706 map_window_raised(xcb_window_t win)
708 uint32_t val = XCB_STACK_MODE_ABOVE;
710 xcb_configure_window(conn, win,
711 XCB_CONFIG_WINDOW_STACK_MODE, &val);
713 xcb_map_window(conn, win);
718 get_atom_from_string(const char *str)
720 xcb_intern_atom_cookie_t c;
721 xcb_intern_atom_reply_t *r;
724 c = xcb_intern_atom(conn, False, strlen(str), str);
725 r = xcb_intern_atom_reply(conn, c, NULL);
733 return (XCB_ATOM_NONE);
737 update_iconic(struct ws_win *win, int newv)
744 iprop = get_atom_from_string("_SWM_ICONIC");
745 if (iprop == XCB_ATOM_NONE)
749 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
750 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
752 xcb_delete_property(conn, win->id, iprop);
756 get_iconic(struct ws_win *win)
760 xcb_get_property_reply_t *pr = NULL;
762 iprop = get_atom_from_string("_SWM_ICONIC");
763 if (iprop == XCB_ATOM_NONE)
766 pr = xcb_get_property_reply(conn,
767 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
772 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
774 v = *((int32_t *)xcb_get_property_value(pr));
785 int i, j, num_screens;
787 sup_list = get_atom_from_string("_NET_SUPPORTED");
789 for (i = 0; i < LENGTH(ewmh); i++)
790 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
792 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
793 for (i = 0; i < num_screens; i++) {
794 /* Support check window will be created by workaround(). */
796 /* Report supported atoms */
797 xcb_delete_property(conn, screens[i].root, sup_list);
798 for (j = 0; j < LENGTH(ewmh); j++)
799 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
800 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
809 xcb_atom_t sup_check, sup_list;
811 xcb_get_property_cookie_t pc;
812 xcb_get_property_reply_t *pr;
814 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
815 sup_list = get_atom_from_string("_NET_SUPPORTED");
816 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
818 for (i = 0; i < num_screens; i++) {
819 /* Get the support check window and destroy it */
820 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
821 XCB_ATOM_WINDOW, 0, 1);
822 pr = xcb_get_property_reply(conn, pc, NULL);
824 id = *((xcb_window_t *)xcb_get_property_value(pr));
826 xcb_destroy_window(conn, id);
827 xcb_delete_property(conn, screens[i].root, sup_check);
828 xcb_delete_property(conn, screens[i].root, sup_list);
836 ewmh_autoquirk(struct ws_win *win)
842 xcb_get_property_cookie_t c;
843 xcb_get_property_reply_t *r;
845 c = xcb_get_property(conn, False, win->id,
846 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
847 r = xcb_get_property_reply(conn, c, NULL);
850 n = xcb_get_property_value_length(r);
852 for (i = 0; i < n; i++) {
853 type = *((xcb_atom_t *)xcb_get_property_value(r));
854 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
856 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
857 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
858 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
860 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
863 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
864 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
866 win->quirks = SWM_Q_FLOAT;
873 #define SWM_EWMH_ACTION_COUNT_MAX (6)
874 #define EWMH_F_FULLSCREEN (1<<0)
875 #define EWMH_F_ABOVE (1<<1)
876 #define EWMH_F_HIDDEN (1<<2)
877 #define EWMH_F_SKIP_PAGER (1<<3)
878 #define EWMH_F_SKIP_TASKBAR (1<<4)
879 #define SWM_F_MANUAL (1<<5)
882 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
890 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
891 "fullscreen %s\n", win->id, YESNO(fs));
894 if (!win->g_floatvalid)
895 store_float_geom(win, win->ws->r);
897 win->g = win->ws->r->g;
900 if (win->g_floatvalid) {
901 /* refloat at last floating relative position */
902 win->g = win->g_float;
903 X(win) += X(win->ws->r);
904 Y(win) += Y(win->ws->r);
912 ewmh_update_actions(struct ws_win *win)
914 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
920 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
923 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
924 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
927 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
928 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1,
932 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
933 #define _NET_WM_STATE_ADD 1 /* add/set property */
934 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
937 ewmh_update_win_state(struct ws_win *win, long state, long action)
939 unsigned int mask = 0;
940 unsigned int changed = 0;
941 unsigned int orig_flags;
946 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
947 mask = EWMH_F_FULLSCREEN;
948 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
950 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
952 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
953 mask = EWMH_F_SKIP_PAGER;
954 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
955 mask = EWMH_F_SKIP_TASKBAR;
958 orig_flags = win->ewmh_flags;
961 case _NET_WM_STATE_REMOVE:
962 win->ewmh_flags &= ~mask;
964 case _NET_WM_STATE_ADD:
965 win->ewmh_flags |= mask;
967 case _NET_WM_STATE_TOGGLE:
968 win->ewmh_flags ^= mask;
972 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
974 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
976 if (!floating_toggle_win(win))
977 win->ewmh_flags = orig_flags; /* revert */
978 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
980 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
981 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
983 if (!ewmh_set_win_fullscreen(win,
984 win->ewmh_flags & EWMH_F_FULLSCREEN))
985 win->ewmh_flags = orig_flags; /* revert */
987 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
989 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
990 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
991 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
992 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
993 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
994 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
995 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
996 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
997 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
998 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
999 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1000 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1001 if (win->ewmh_flags & EWMH_F_ABOVE)
1002 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1003 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1004 &ewmh[_NET_WM_STATE_ABOVE].atom);
1005 if (win->ewmh_flags & SWM_F_MANUAL)
1006 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1007 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1008 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1012 ewmh_get_win_state(struct ws_win *win)
1015 xcb_get_property_cookie_t c;
1016 xcb_get_property_reply_t *r;
1022 win->ewmh_flags = 0;
1024 win->ewmh_flags |= EWMH_F_ABOVE;
1026 win->ewmh_flags |= SWM_F_MANUAL;
1028 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1029 XCB_ATOM_ATOM, 0, (~0L));
1030 r = xcb_get_property_reply(conn, c, NULL);
1034 states = xcb_get_property_value(r);
1035 n = xcb_get_property_value_length(r);
1037 for (i = 0; i < n; i++)
1038 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1046 geteventname(xcb_generic_event_t *e)
1050 switch (XCB_EVENT_RESPONSE_TYPE(e)) {
1054 case XCB_KEY_RELEASE:
1055 name = "KeyRelease";
1057 case XCB_BUTTON_PRESS:
1058 name = "ButtonPress";
1060 case XCB_BUTTON_RELEASE:
1061 name = "ButtonRelease";
1063 case XCB_MOTION_NOTIFY:
1064 name = "MotionNotify";
1066 case XCB_ENTER_NOTIFY:
1067 name = "EnterNotify";
1069 case XCB_LEAVE_NOTIFY:
1070 name = "LeaveNotify";
1078 case XCB_KEYMAP_NOTIFY:
1079 name = "KeymapNotify";
1084 case XCB_GRAPHICS_EXPOSURE:
1085 name = "GraphicsExposure";
1087 case XCB_NO_EXPOSURE:
1088 name = "NoExposure";
1090 case XCB_VISIBILITY_NOTIFY:
1091 name = "VisibilityNotify";
1093 case XCB_CREATE_NOTIFY:
1094 name = "CreateNotify";
1096 case XCB_DESTROY_NOTIFY:
1097 name = "DestroyNotify";
1099 case XCB_UNMAP_NOTIFY:
1100 name = "UnmapNotify";
1102 case XCB_MAP_NOTIFY:
1105 case XCB_MAP_REQUEST:
1106 name = "MapRequest";
1108 case XCB_REPARENT_NOTIFY:
1109 name = "ReparentNotify";
1111 case XCB_CONFIGURE_NOTIFY:
1112 name = "ConfigureNotify";
1114 case XCB_CONFIGURE_REQUEST:
1115 name = "ConfigureRequest";
1117 case XCB_GRAVITY_NOTIFY:
1118 name = "GravityNotify";
1120 case XCB_RESIZE_REQUEST:
1121 name = "ResizeRequest";
1123 case XCB_CIRCULATE_NOTIFY:
1124 name = "CirculateNotify";
1126 case XCB_CIRCULATE_REQUEST:
1127 name = "CirculateRequest";
1129 case XCB_PROPERTY_NOTIFY:
1130 name = "PropertyNotify";
1132 case XCB_SELECTION_CLEAR:
1133 name = "SelectionClear";
1135 case XCB_SELECTION_REQUEST:
1136 name = "SelectionRequest";
1138 case XCB_SELECTION_NOTIFY:
1139 name = "SelectionNotify";
1141 case XCB_COLORMAP_NOTIFY:
1142 name = "ColormapNotify";
1144 case XCB_CLIENT_MESSAGE:
1145 name = "ClientMessage";
1147 case XCB_MAPPING_NOTIFY:
1148 name = "MappingNotify";
1159 dumpwins(struct swm_region *r, union arg *args)
1163 xcb_get_window_attributes_cookie_t c;
1164 xcb_get_window_attributes_reply_t *wa;
1166 if (r->ws == NULL) {
1167 warnx("dumpwins: invalid workspace");
1171 warnx("=== managed window list ws %02d ===", r->ws->idx);
1172 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1173 state = getstate(win->id);
1174 c = xcb_get_window_attributes(conn, win->id);
1175 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1177 warnx("window: 0x%x, map_state: %d, state: %u, "
1178 "transient: 0x%x", win->id, wa->map_state,
1179 state, win->transient);
1182 warnx("window: 0x%x, failed xcb_get_window_attributes",
1186 warnx("===== unmanaged window list =====");
1187 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1188 state = getstate(win->id);
1189 c = xcb_get_window_attributes(conn, win->id);
1190 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1192 warnx("window: 0x%x, map_state: %d, state: %u, "
1193 "transient: 0x%x", win->id, wa->map_state,
1194 state, win->transient);
1197 warnx("window: 0x%x, failed xcb_get_window_attributes",
1201 warnx("=================================");
1205 dumpwins(struct swm_region *r, union arg *args)
1208 #endif /* SWM_DEBUG */
1210 void event_error(xcb_generic_error_t *);
1211 void expose(xcb_expose_event_t *);
1212 void keypress(xcb_key_press_event_t *);
1213 void buttonpress(xcb_button_press_event_t *);
1214 void configurerequest(xcb_configure_request_event_t *);
1215 void configurenotify(xcb_configure_notify_event_t *);
1216 void destroynotify(xcb_destroy_notify_event_t *);
1217 void enternotify(xcb_enter_notify_event_t *);
1218 void mapnotify(xcb_map_notify_event_t *);
1219 void mappingnotify(xcb_mapping_notify_event_t *);
1220 void maprequest(xcb_map_request_event_t *);
1221 void propertynotify(xcb_property_notify_event_t *);
1222 void unmapnotify(xcb_unmap_notify_event_t *);
1223 void visibilitynotify(xcb_visibility_notify_event_t *);
1224 void clientmessage(xcb_client_message_event_t *);
1225 void screenchange(xcb_randr_screen_change_notify_event_t *);
1230 int saved_errno, status;
1233 saved_errno = errno;
1237 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1242 if (errno != ECHILD)
1243 warn("sighdlr: waitpid");
1244 #endif /* SWM_DEBUG */
1247 if (pid == searchpid)
1251 if (WIFEXITED(status)) {
1252 if (WEXITSTATUS(status) != 0)
1253 warnx("sighdlr: child exit status: %d",
1254 WEXITSTATUS(status));
1256 warnx("sighdlr: child is terminated "
1258 #endif /* SWM_DEBUG */
1272 errno = saved_errno;
1278 struct pid_e *p = NULL;
1280 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1285 TAILQ_FOREACH(p, &pidlist, entry) {
1294 name_to_color(const char *colorname)
1296 uint32_t result = 0;
1297 char cname[32] = "#";
1298 xcb_screen_t *screen;
1299 xcb_colormap_t cmap;
1300 xcb_alloc_color_reply_t *cr;
1301 xcb_alloc_named_color_reply_t *nr;
1302 uint16_t rr, gg, bb;
1304 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1305 cmap = screen->default_colormap;
1307 /* color is in format rgb://rr/gg/bb */
1308 if (strncmp(colorname, "rgb:", 4) == 0) {
1309 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1310 warnx("could not parse rgb %s", colorname);
1312 cr = xcb_alloc_color_reply(conn,
1313 xcb_alloc_color(conn, cmap, rr, gg, bb),
1319 warnx("color '%s' not found", colorname);
1322 nr = xcb_alloc_named_color_reply(conn,
1323 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1327 strlcat(cname, colorname + 2, sizeof cname - 1);
1328 nr = xcb_alloc_named_color_reply(conn,
1329 xcb_alloc_named_color(conn, cmap, strlen(cname),
1337 warnx("color '%s' not found", colorname);
1344 setscreencolor(char *val, int i, int c)
1348 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1349 if (i > 0 && i <= num_screens) {
1350 screens[i - 1].c[c].color = name_to_color(val);
1351 free(screens[i - 1].c[c].name);
1352 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1354 } else if (i == -1) {
1355 for (i = 0; i < num_screens; i++) {
1356 screens[i].c[c].color = name_to_color(val);
1357 free(screens[i].c[c].name);
1358 if ((screens[i].c[c].name = strdup(val)) == NULL)
1362 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1367 fancy_stacker(struct workspace *ws)
1369 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1370 if (ws->cur_layout->l_stack == vertical_stack)
1371 snprintf(ws->stacker, sizeof ws->stacker,
1372 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1373 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1374 if (ws->cur_layout->l_stack == horizontal_stack)
1375 snprintf(ws->stacker, sizeof ws->stacker,
1376 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1377 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1381 plain_stacker(struct workspace *ws)
1383 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1384 if (ws->cur_layout->l_stack == vertical_stack)
1385 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1386 sizeof ws->stacker);
1387 if (ws->cur_layout->l_stack == horizontal_stack)
1388 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1389 sizeof ws->stacker);
1393 custom_region(char *val)
1395 unsigned int sidx, x, y, w, h;
1397 xcb_screen_t *screen;
1399 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1400 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1401 errx(1, "invalid custom region, "
1402 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1403 if (sidx < 1 || sidx > num_screens)
1404 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1408 screen = get_screen(sidx);
1410 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1412 if (x > screen->width_in_pixels ||
1413 y > screen->height_in_pixels ||
1414 w + x > screen->width_in_pixels ||
1415 h + y > screen->height_in_pixels) {
1416 warnx("ignoring region %ux%u+%u+%u - not within screen "
1417 "boundaries (%ux%u)", w, h, x, y,
1418 screen->width_in_pixels, screen->height_in_pixels);
1422 new_region(&screens[sidx], x, y, w, h);
1426 socket_setnonblock(int fd)
1430 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1431 err(1, "fcntl F_GETFL");
1432 flags |= O_NONBLOCK;
1433 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1434 err(1, "fcntl F_SETFL");
1438 bar_print(struct swm_region *r, const char *s)
1442 xcb_rectangle_t rect;
1444 XRectangle ibox, lbox;
1447 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1449 switch (bar_justify) {
1450 case SWM_BAR_JUSTIFY_LEFT:
1453 case SWM_BAR_JUSTIFY_CENTER:
1454 x = (WIDTH(r) - lbox.width) / 2;
1456 case SWM_BAR_JUSTIFY_RIGHT:
1457 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1461 if (x < SWM_BAR_OFFSET)
1466 rect.width = WIDTH(r->bar);
1467 rect.height = HEIGHT(r->bar);
1469 /* clear back buffer */
1470 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1471 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1472 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1473 sizeof(rect), &rect);
1475 /* draw back buffer */
1476 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1477 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1478 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1479 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1480 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1481 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1485 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1486 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1490 bar_extra_stop(void)
1494 bzero(bar_pipe, sizeof bar_pipe);
1497 kill(bar_pid, SIGTERM);
1500 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1505 bar_class_name(char *s, size_t sz, struct swm_region *r)
1507 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1509 if (r->ws->focus->ch.class_name != NULL)
1510 strlcat(s, r->ws->focus->ch.class_name, sz);
1514 bar_title_name(char *s, size_t sz, struct swm_region *r)
1516 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1518 if (r->ws->focus->ch.instance_name != NULL)
1519 strlcat(s, r->ws->focus->ch.instance_name, sz);
1523 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1525 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1528 bar_class_name(s, sz, r);
1529 strlcat(s, ":", sz);
1530 bar_title_name(s, sz, r);
1534 bar_window_float(char *s, size_t sz, struct swm_region *r)
1536 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1538 if (r->ws->focus->floating)
1539 strlcat(s, "(f)", sz);
1543 bar_window_name(char *s, size_t sz, struct swm_region *r)
1547 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1549 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1552 strlcat(s, title, sz);
1556 int urgent[SWM_WS_MAX];
1558 bar_urgent(char *s, size_t sz)
1561 int i, j, num_screens;
1563 xcb_get_property_cookie_t c;
1564 xcb_icccm_wm_hints_t hints;
1566 for (i = 0; i < workspace_limit; i++)
1569 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1570 for (i = 0; i < num_screens; i++)
1571 for (j = 0; j < workspace_limit; j++)
1572 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1573 c = xcb_icccm_get_wm_hints(conn, win->id);
1574 if (xcb_icccm_get_wm_hints_reply(conn, c,
1577 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1581 for (i = 0; i < workspace_limit; i++) {
1583 snprintf(b, sizeof b, "%d ", i + 1);
1585 snprintf(b, sizeof b, "- ");
1591 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1593 if (r == NULL || r->ws == NULL)
1595 if (r->ws->name != NULL)
1596 strlcat(s, r->ws->name, sz);
1599 /* build the default bar format according to the defined enabled options */
1601 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1603 /* if format provided, just copy the buffers */
1604 if (bar_format != NULL) {
1605 strlcpy(fmtnew, fmtexp, sz);
1609 /* reset the output buffer */
1612 strlcat(fmtnew, "+N:+I ", sz);
1614 strlcat(fmtnew, "+S", sz);
1615 strlcat(fmtnew, " ", sz);
1617 /* only show the workspace name if there's actually one */
1618 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1619 strlcat(fmtnew, "<+D>", sz);
1620 strlcat(fmtnew, "+3<", sz);
1622 if (clock_enabled) {
1623 strlcat(fmtnew, fmtexp, sz);
1624 strlcat(fmtnew, "+4<", sz);
1627 /* bar_urgent already adds the space before the last asterisk */
1629 strlcat(fmtnew, "* +U*+4<", sz);
1631 if (title_class_enabled) {
1632 strlcat(fmtnew, "+C", sz);
1633 if (title_name_enabled == 0)
1634 strlcat(fmtnew, "+4<", sz);
1637 /* checks needed by the colon and floating strlcat(3) calls below */
1638 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1639 if (title_name_enabled) {
1640 if (title_class_enabled)
1641 strlcat(fmtnew, ":", sz);
1642 strlcat(fmtnew, "+T+4<", sz);
1644 if (window_name_enabled) {
1645 if (r->ws->focus->floating)
1646 strlcat(fmtnew, "+F ", sz);
1647 strlcat(fmtnew, "+64W ", sz);
1651 /* finally add the action script output and the version */
1652 strlcat(fmtnew, "+4<+A+4<+V", sz);
1656 bar_replace_pad(char *tmp, int *limit, size_t sz)
1658 /* special case; no limit given, pad one space, instead */
1659 if (*limit == sz - 1)
1661 snprintf(tmp, sz, "%*s", *limit, " ");
1664 /* replaces the bar format character sequences (like in tmux(1)) */
1666 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1670 char tmp[SWM_BAR_MAX];
1674 /* reset strlcat(3) buffer */
1677 /* get number, if any */
1680 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1681 limit = sizeof tmp - 1;
1682 if (limit <= 0 || limit >= sizeof tmp)
1683 limit = sizeof tmp - 1;
1685 /* there is nothing to replace (ie EOL) */
1692 bar_replace_pad(tmp, &limit, sizeof tmp);
1695 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1698 bar_class_name(tmp, sizeof tmp, r);
1701 bar_workspace_name(tmp, sizeof tmp, r);
1704 bar_window_float(tmp, sizeof tmp, r);
1707 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1710 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1713 bar_class_title_name(tmp, sizeof tmp, r);
1716 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1719 bar_title_name(tmp, sizeof tmp, r);
1722 bar_urgent(tmp, sizeof tmp);
1725 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1728 bar_window_name(tmp, sizeof tmp, r);
1731 /* unknown character sequence; copy as-is */
1732 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1740 while (limit-- > 0) {
1741 if (*offrep >= sz - 1)
1743 fmtrep[(*offrep)++] = *ptr++;
1751 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1756 while (*fmt != '\0') {
1758 /* skip ordinary characters */
1761 fmtrep[off++] = *fmt++;
1765 /* character sequence found; replace it */
1766 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1775 bar_fmt_expand(char *fmtexp, size_t sz)
1782 /* start by grabbing the current time and date */
1784 localtime_r(&tmt, &tm);
1786 /* figure out what to expand */
1787 if (bar_format != NULL)
1789 else if (bar_format == NULL && clock_enabled)
1791 /* if nothing to expand bail out */
1797 /* copy as-is, just in case the format shouldn't be expanded below */
1798 strlcpy(fmtexp, fmt, sz);
1799 /* finally pass the string through strftime(3) */
1800 #ifndef SWM_DENY_CLOCK_FORMAT
1801 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1802 warnx("format too long");
1810 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1811 char fmtrep[SWM_BAR_MAX];
1813 struct swm_region *r;
1815 /* expand the format by first passing it through strftime(3) */
1816 bar_fmt_expand(fmtexp, sizeof fmtexp);
1818 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1819 for (i = 0; i < num_screens; i++) {
1820 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1823 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1824 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1825 bar_print(r, fmtrep);
1836 if (bar_enabled == 0)
1838 if (bar_extra && bar_extra_running) {
1839 /* ignore short reads; it'll correct itself */
1840 while ((b = fgetln(stdin, &len)) != NULL)
1841 if (b && b[len - 1] == '\n') {
1843 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1845 if (b == NULL && errno != EAGAIN) {
1846 warn("bar_update: bar_extra failed");
1850 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1863 bar_toggle(struct swm_region *r, union arg *args)
1865 struct swm_region *tmpr;
1868 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1870 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1872 for (i = 0; i < num_screens; i++)
1873 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1875 xcb_unmap_window(conn, tmpr->bar->id);
1877 for (i = 0; i < num_screens; i++)
1878 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1880 map_window_raised(tmpr->bar->id);
1883 bar_enabled = !bar_enabled;
1886 /* must be after stack */
1893 struct swm_region *r;
1897 /* do this here because the conf file is in memory */
1898 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1899 /* launch external status app */
1900 bar_extra_running = 1;
1901 if (pipe(bar_pipe) == -1)
1902 err(1, "pipe error");
1903 socket_setnonblock(bar_pipe[0]);
1904 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1905 if (dup2(bar_pipe[0], 0) == -1)
1907 if (dup2(bar_pipe[1], 1) == -1)
1909 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1910 err(1, "could not disable SIGPIPE");
1911 switch (bar_pid = fork()) {
1913 err(1, "cannot fork");
1917 execvp(bar_argv[0], bar_argv);
1918 err(1, "%s external app failed", bar_argv[0]);
1920 default: /* parent */
1926 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1927 for (i = 0; i < num_screens; i++)
1928 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1931 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1932 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1933 xcb_change_window_attributes(conn, r->bar->id,
1934 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1940 bar_setup(struct swm_region *r)
1942 char *default_string;
1943 char **missing_charsets;
1944 int num_missing_charsets = 0;
1946 xcb_screen_t *screen = get_screen(r->s->idx);
1950 XFreeFontSet(display, bar_fs);
1954 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1955 err(1, "bar_setup: calloc: failed to allocate memory.");
1957 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1959 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1960 &num_missing_charsets, &default_string);
1962 if (num_missing_charsets > 0) {
1963 warnx("Unable to load charset(s):");
1965 for (i = 0; i < num_missing_charsets; ++i)
1966 warnx("%s", missing_charsets[i]);
1968 XFreeStringList(missing_charsets);
1970 if (strcmp(default_string, ""))
1971 warnx("Glyphs from those sets will be replaced "
1972 "by '%s'.", default_string);
1974 warnx("Glyphs from those sets won't be drawn.");
1978 errx(1, "Error creating font set structure.");
1980 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1982 bar_height = bar_fs_extents->max_logical_extent.height +
1983 2 * bar_border_width;
1989 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1990 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1991 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1993 r->bar->id = xcb_generate_id(conn);
1994 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1995 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1996 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1997 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1998 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1999 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
2001 r->bar->buffer = xcb_generate_id(conn);
2002 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2003 WIDTH(r->bar), HEIGHT(r->bar));
2005 xcb_randr_select_input(conn, r->bar->id,
2006 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2009 map_window_raised(r->bar->id);
2011 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2012 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2015 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2016 err(1, "could not install bar_signal");
2021 bar_cleanup(struct swm_region *r)
2025 xcb_destroy_window(conn, r->bar->id);
2026 xcb_free_pixmap(conn, r->bar->buffer);
2032 set_win_state(struct ws_win *win, uint16_t state)
2034 uint16_t data[2] = { state, XCB_ATOM_NONE };
2036 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2041 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
2042 astate, 32, 2, data);
2046 getstate(xcb_window_t w)
2048 uint16_t result = 0;
2049 xcb_get_property_cookie_t c;
2050 xcb_get_property_reply_t *r;
2052 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
2053 r = xcb_get_property_reply(conn, c, NULL);
2056 result = *((uint16_t *)xcb_get_property_value(r));
2060 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2066 version(struct swm_region *r, union arg *args)
2068 bar_version = !bar_version;
2070 snprintf(bar_vertext, sizeof bar_vertext,
2071 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2073 strlcpy(bar_vertext, "", sizeof bar_vertext);
2078 client_msg(struct ws_win *win, xcb_atom_t a)
2080 xcb_client_message_event_t ev;
2085 bzero(&ev, sizeof ev);
2086 ev.response_type = XCB_CLIENT_MESSAGE;
2087 ev.window = win->id;
2090 ev.data.data32[0] = a;
2091 ev.data.data32[1] = XCB_CURRENT_TIME;
2093 xcb_send_event(conn, False, win->id,
2094 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2097 /* synthetic response to a ConfigureRequest when not making a change */
2099 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2101 xcb_configure_notify_event_t ce;
2106 /* send notification of unchanged state. */
2107 bzero(&ce, sizeof(ce));
2108 ce.response_type = XCB_CONFIGURE_NOTIFY;
2111 ce.width = WIDTH(win);
2112 ce.height = HEIGHT(win);
2113 ce.override_redirect = False;
2118 ce.window = win->id;
2119 ce.border_width = BORDER(win);
2120 ce.above_sibling = XCB_WINDOW_NONE;
2123 ce.event = ev->window;
2124 ce.window = ev->window;
2126 /* make response appear more WM_SIZE_HINTS-compliant */
2128 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2129 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2130 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2131 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2132 SH_INC_W(win), SH_INC_H(win));
2136 /* the hint may be set... to 0! */
2137 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2138 ce.width = SH_MIN_W(win);
2139 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2140 ce.height = SH_MIN_H(win);
2145 /* may also be advertized as 0 */
2146 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2147 ce.width = SH_MAX_W(win);
2148 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2149 ce.height = SH_MAX_H(win);
2152 /* resize increment. */
2154 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2155 ce.width -= (ce.width - SH_MIN_W(win)) %
2157 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2158 ce.height -= (ce.height - SH_MIN_H(win)) %
2162 /* adjust x and y for requested border_width. */
2163 ce.x += BORDER(win) - ev->border_width;
2164 ce.y += BORDER(win) - ev->border_width;
2165 ce.border_width = ev->border_width;
2166 ce.above_sibling = ev->sibling;
2169 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2170 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2171 ce.y, ce.width, ce.height, ce.border_width);
2173 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2178 count_win(struct workspace *ws, int count_transient)
2183 TAILQ_FOREACH(win, &ws->winlist, entry) {
2184 if (count_transient == 0 && win->floating)
2186 if (count_transient == 0 && win->transient)
2192 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2198 quit(struct swm_region *r, union arg *args)
2200 DNPRINTF(SWM_D_MISC, "quit\n");
2205 unmap_window(struct ws_win *win)
2210 /* don't unmap again */
2211 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2214 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2216 xcb_unmap_window(conn, win->id);
2217 xcb_change_window_attributes(conn, win->id,
2218 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2225 int i, j, num_screens;
2227 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2228 for (i = 0; i < num_screens; i++)
2229 for (j = 0; j < workspace_limit; j++)
2230 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2235 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2237 xcb_key_press_event_t event;
2238 xcb_keycode_t *keycode;
2243 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2245 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2248 bzero(&event, sizeof(event));
2249 event.event = win->id;
2250 event.root = win->s->root;
2251 event.child = XCB_WINDOW_NONE;
2252 event.time = XCB_CURRENT_TIME;
2253 event.event_x = X(win);
2254 event.event_y = Y(win);
2257 event.same_screen = True;
2258 event.detail = *keycode;
2259 event.state = modifiers;
2261 event.response_type = XCB_KEY_PRESS;
2262 xcb_send_event(conn, True, win->id,
2263 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2265 event.response_type = XCB_KEY_RELEASE;
2266 xcb_send_event(conn, True, win->id,
2267 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2271 restart(struct swm_region *r, union arg *args)
2273 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2275 /* disable alarm because the following code may not be interrupted */
2277 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2278 err(1, "can't disable alarm");
2284 xcb_key_symbols_free(syms);
2286 xcb_disconnect(conn);
2288 execvp(start_argv[0], start_argv);
2289 warn("execvp failed");
2294 root_to_region(xcb_window_t root)
2296 struct swm_region *r = NULL;
2298 xcb_query_pointer_reply_t *qpr;
2300 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2302 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2303 for (i = 0; i < num_screens; i++)
2304 if (screens[i].root == root)
2307 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2308 screens[i].root), NULL);
2311 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2312 qpr->root_x, qpr->root_y);
2313 /* choose a region based on pointer location */
2314 TAILQ_FOREACH(r, &screens[i].rl, entry)
2315 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2316 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2322 r = TAILQ_FIRST(&screens[i].rl);
2328 find_unmanaged_window(xcb_window_t id)
2331 int i, j, num_screens;
2333 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2334 for (i = 0; i < num_screens; i++)
2335 for (j = 0; j < workspace_limit; j++)
2336 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2344 find_window(xcb_window_t id)
2347 int i, j, num_screens;
2348 xcb_query_tree_cookie_t c;
2349 xcb_query_tree_reply_t *r;
2351 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2352 for (i = 0; i < num_screens; i++)
2353 for (j = 0; j < workspace_limit; j++)
2354 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2358 c = xcb_query_tree(conn, id);
2359 r = xcb_query_tree_reply(conn, c, NULL);
2363 /* if we were looking for the parent return that window instead */
2364 if (r->parent == 0 || r->root == r->parent)
2367 /* look for parent */
2368 for (i = 0; i < num_screens; i++)
2369 for (j = 0; j < workspace_limit; j++)
2370 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2371 if (r->parent == win->id) {
2381 spawn(int ws_idx, union arg *args, int close_fd)
2386 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2389 close(xcb_get_file_descriptor(conn));
2391 setenv("LD_PRELOAD", SWM_LIB, 1);
2393 if (asprintf(&ret, "%d", ws_idx) == -1) {
2394 warn("spawn: asprintf SWM_WS");
2397 setenv("_SWM_WS", ret, 1);
2401 if (asprintf(&ret, "%d", getpid()) == -1) {
2402 warn("spawn: asprintf _SWM_PID");
2405 setenv("_SWM_PID", ret, 1);
2409 if (setsid() == -1) {
2410 warn("spawn: setsid");
2416 * close stdin and stdout to prevent interaction between apps
2417 * and the baraction script
2418 * leave stderr open to record errors
2420 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2421 warn("spawn: open");
2424 dup2(fd, STDIN_FILENO);
2425 dup2(fd, STDOUT_FILENO);
2430 execvp(args->argv[0], args->argv);
2432 warn("spawn: execvp");
2437 kill_refs(struct ws_win *win)
2439 int i, x, num_screens;
2440 struct swm_region *r;
2441 struct workspace *ws;
2446 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2447 for (i = 0; i < num_screens; i++)
2448 TAILQ_FOREACH(r, &screens[i].rl, entry)
2449 for (x = 0; x < workspace_limit; x++) {
2451 if (win == ws->focus)
2453 if (win == ws->focus_prev)
2454 ws->focus_prev = NULL;
2459 validate_win(struct ws_win *testwin)
2462 struct workspace *ws;
2463 struct swm_region *r;
2464 int i, x, num_screens;
2466 if (testwin == NULL)
2469 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2470 for (i = 0; i < num_screens; i++)
2471 TAILQ_FOREACH(r, &screens[i].rl, entry)
2472 for (x = 0; x < workspace_limit; x++) {
2474 TAILQ_FOREACH(win, &ws->winlist, entry)
2482 validate_ws(struct workspace *testws)
2484 struct swm_region *r;
2485 struct workspace *ws;
2486 int i, x, num_screens;
2488 /* validate all ws */
2489 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2490 for (i = 0; i < num_screens; i++)
2491 TAILQ_FOREACH(r, &screens[i].rl, entry)
2492 for (x = 0; x < workspace_limit; x++) {
2501 unfocus_win(struct ws_win *win)
2503 xcb_window_t none = XCB_WINDOW_NONE;
2505 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2509 if (win->ws == NULL)
2512 if (validate_ws(win->ws))
2513 return; /* XXX this gets hit with thunderbird, needs fixing */
2515 if (win->ws->r == NULL)
2518 if (validate_win(win)) {
2523 if (win->ws->focus == win) {
2524 win->ws->focus = NULL;
2525 win->ws->focus_prev = win;
2528 if (validate_win(win->ws->focus)) {
2529 kill_refs(win->ws->focus);
2530 win->ws->focus = NULL;
2532 if (validate_win(win->ws->focus_prev)) {
2533 kill_refs(win->ws->focus_prev);
2534 win->ws->focus_prev = NULL;
2537 grabbuttons(win, 0);
2538 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2539 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2540 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2541 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2549 int i, j, num_screens;
2551 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2553 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2554 for (i = 0; i < num_screens; i++)
2555 for (j = 0; j < workspace_limit; j++)
2556 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2561 focus_win(struct ws_win *win)
2563 struct ws_win *cfw = NULL;
2564 xcb_get_input_focus_cookie_t c;
2565 xcb_get_input_focus_reply_t *r;
2566 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2568 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2572 if (win->ws == NULL)
2575 if (validate_ws(win->ws))
2576 return; /* XXX this gets hit with thunderbird, needs fixing */
2578 if (validate_win(win)) {
2583 if (validate_win(win)) {
2588 c = xcb_get_input_focus(conn);
2589 r = xcb_get_input_focus_reply(conn, c, NULL);
2591 cur_focus = r->focus;
2594 if ((cfw = find_window(cur_focus)) != NULL)
2597 /* use larger hammer since the window was killed somehow */
2598 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2599 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2600 xcb_change_window_attributes(conn, cfw->id,
2601 XCB_CW_BORDER_PIXEL,
2602 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2605 win->ws->focus = win;
2607 if (win->ws->r != NULL) {
2609 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2610 win->id, XCB_CURRENT_TIME);
2611 grabbuttons(win, 1);
2612 xcb_change_window_attributes(conn, win->id,
2613 XCB_CW_BORDER_PIXEL,
2614 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2615 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2616 win->ws->always_raise)
2617 map_window_raised(win->id);
2619 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2620 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2628 switchws(struct swm_region *r, union arg *args)
2630 int wsid = args->id, unmap_old = 0;
2631 struct swm_region *this_r, *other_r;
2633 struct workspace *new_ws, *old_ws;
2639 if (wsid >= workspace_limit)
2643 old_ws = this_r->ws;
2644 new_ws = &this_r->s->ws[wsid];
2646 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2647 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2649 if (new_ws == NULL || old_ws == NULL)
2651 if (new_ws == old_ws)
2654 other_r = new_ws->r;
2655 if (other_r == NULL) {
2656 /* the other workspace is hidden, hide this one */
2660 /* the other ws is visible in another region, exchange them */
2661 other_r->ws_prior = new_ws;
2662 other_r->ws = old_ws;
2663 old_ws->r = other_r;
2665 this_r->ws_prior = old_ws;
2666 this_r->ws = new_ws;
2669 /* this is needed so that we can click on a window after a restart */
2673 a.id = SWM_ARG_ID_FOCUSCUR;
2674 focus(new_ws->r, &a);
2676 /* unmap old windows */
2678 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2683 cyclews(struct swm_region *r, union arg *args)
2686 struct swm_screen *s = r->s;
2689 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2690 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2695 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2698 case SWM_ARG_ID_CYCLEWS_UP:
2699 if (a.id < workspace_limit - 1)
2704 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2707 case SWM_ARG_ID_CYCLEWS_DOWN:
2711 a.id = workspace_limit - 1;
2718 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2720 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2724 } while (a.id != r->ws->idx);
2728 priorws(struct swm_region *r, union arg *args)
2732 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2733 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2735 if (r->ws_prior == NULL)
2738 a.id = r->ws_prior->idx;
2743 cyclescr(struct swm_region *r, union arg *args)
2745 struct swm_region *rr = NULL;
2747 int i, x, y, num_screens;
2749 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2750 /* do nothing if we don't have more than one screen */
2751 if (!(num_screens > 1 || outputs > 1))
2756 case SWM_ARG_ID_CYCLESC_UP:
2757 rr = TAILQ_NEXT(r, entry);
2759 rr = TAILQ_FIRST(&screens[i].rl);
2761 case SWM_ARG_ID_CYCLESC_DOWN:
2762 rr = TAILQ_PREV(r, swm_region_list, entry);
2764 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2772 /* move mouse to region */
2774 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2775 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2778 a.id = SWM_ARG_ID_FOCUSCUR;
2781 if (rr->ws->focus) {
2782 /* move to focus window */
2783 x = X(rr->ws->focus) + 1;
2784 y = Y(rr->ws->focus) + 1;
2785 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2791 sort_windows(struct ws_win_list *wl)
2793 struct ws_win *win, *parent, *nxt;
2798 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2799 nxt = TAILQ_NEXT(win, entry);
2800 if (win->transient) {
2801 parent = find_window(win->transient);
2802 if (parent == NULL) {
2803 warnx("not possible bug");
2806 TAILQ_REMOVE(wl, win, entry);
2807 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2814 swapwin(struct swm_region *r, union arg *args)
2816 struct ws_win *target, *source;
2817 struct ws_win *cur_focus;
2818 struct ws_win_list *wl;
2821 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2822 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2824 cur_focus = r->ws->focus;
2825 if (cur_focus == NULL)
2829 wl = &source->ws->winlist;
2832 case SWM_ARG_ID_SWAPPREV:
2833 if (source->transient)
2834 source = find_window(source->transient);
2835 target = TAILQ_PREV(source, ws_win_list, entry);
2836 if (target && target->transient)
2837 target = find_window(target->transient);
2838 TAILQ_REMOVE(wl, source, entry);
2840 TAILQ_INSERT_TAIL(wl, source, entry);
2842 TAILQ_INSERT_BEFORE(target, source, entry);
2844 case SWM_ARG_ID_SWAPNEXT:
2845 target = TAILQ_NEXT(source, entry);
2846 /* move the parent and let the sort handle the move */
2847 if (source->transient)
2848 source = find_window(source->transient);
2849 TAILQ_REMOVE(wl, source, entry);
2851 TAILQ_INSERT_HEAD(wl, source, entry);
2853 TAILQ_INSERT_AFTER(wl, target, source, entry);
2855 case SWM_ARG_ID_SWAPMAIN:
2856 target = TAILQ_FIRST(wl);
2857 if (target == source) {
2858 if (source->ws->focus_prev != NULL &&
2859 source->ws->focus_prev != target)
2860 source = source->ws->focus_prev;
2864 if (target == NULL || source == NULL)
2866 source->ws->focus_prev = target;
2867 TAILQ_REMOVE(wl, target, entry);
2868 TAILQ_INSERT_BEFORE(source, target, entry);
2869 TAILQ_REMOVE(wl, source, entry);
2870 TAILQ_INSERT_HEAD(wl, source, entry);
2872 case SWM_ARG_ID_MOVELAST:
2873 TAILQ_REMOVE(wl, source, entry);
2874 TAILQ_INSERT_TAIL(wl, source, entry);
2877 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2887 focus_prev(struct ws_win *win)
2889 struct ws_win *winfocus = NULL;
2890 struct ws_win *cur_focus = NULL;
2891 struct ws_win_list *wl = NULL;
2892 struct workspace *ws = NULL;
2894 if (!(win && win->ws))
2899 cur_focus = ws->focus;
2901 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2902 WINID(win), WINID(cur_focus));
2904 /* pickle, just focus on whatever */
2905 if (cur_focus == NULL) {
2906 /* use prev_focus if valid */
2907 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2908 find_window(WINID(ws->focus_prev)))
2909 winfocus = ws->focus_prev;
2913 /* if transient focus on parent */
2914 if (cur_focus->transient) {
2915 winfocus = find_window(cur_focus->transient);
2919 /* if in max_stack try harder */
2920 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2921 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2922 if (cur_focus != ws->focus_prev)
2923 winfocus = ws->focus_prev;
2925 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2930 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2932 if (winfocus == NULL || winfocus == win) {
2933 switch (focus_close) {
2934 case SWM_STACK_BOTTOM:
2935 winfocus = TAILQ_FIRST(wl);
2938 winfocus = TAILQ_LAST(wl, ws_win_list);
2940 case SWM_STACK_ABOVE:
2941 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2942 if (focus_close_wrap)
2943 winfocus = TAILQ_FIRST(wl);
2945 winfocus = TAILQ_PREV(cur_focus,
2946 ws_win_list, entry);
2949 case SWM_STACK_BELOW:
2950 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2952 if (focus_close_wrap)
2953 winfocus = TAILQ_LAST(wl, ws_win_list);
2955 winfocus = TAILQ_NEXT(cur_focus, entry);
2961 if (winfocus == NULL) {
2962 if (focus_default == SWM_STACK_TOP)
2963 winfocus = TAILQ_LAST(wl, ws_win_list);
2965 winfocus = TAILQ_FIRST(wl);
2969 focus_magic(winfocus);
2973 focus(struct swm_region *r, union arg *args)
2975 struct ws_win *winfocus = NULL, *head;
2976 struct ws_win *cur_focus = NULL;
2977 struct ws_win_list *wl = NULL;
2978 struct workspace *ws = NULL;
2984 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2986 /* treat FOCUS_CUR special */
2987 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2988 if (r->ws->focus && r->ws->focus->iconic == 0)
2989 winfocus = r->ws->focus;
2990 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2991 winfocus = r->ws->focus_prev;
2993 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2994 if (winfocus->iconic == 0)
2997 focus_magic(winfocus);
3001 if ((cur_focus = r->ws->focus) == NULL)
3005 if (TAILQ_EMPTY(wl))
3007 /* make sure there is at least one uniconified window */
3009 TAILQ_FOREACH(winfocus, wl, entry)
3010 if (winfocus->iconic == 0) {
3018 case SWM_ARG_ID_FOCUSPREV:
3019 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3021 head = TAILQ_LAST(wl, ws_win_list);
3023 if (WINID(winfocus) == cur_focus->transient) {
3024 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3026 head = TAILQ_LAST(wl, ws_win_list);
3031 if (winfocus && winfocus->iconic) {
3032 while (winfocus != cur_focus) {
3033 if (winfocus == NULL)
3034 winfocus = TAILQ_LAST(wl, ws_win_list);
3035 if (winfocus->iconic == 0)
3037 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3043 case SWM_ARG_ID_FOCUSNEXT:
3044 head = TAILQ_NEXT(cur_focus, entry);
3046 head = TAILQ_FIRST(wl);
3050 if (winfocus && winfocus->iconic) {
3051 while (winfocus != cur_focus) {
3052 if (winfocus == NULL)
3053 winfocus = TAILQ_FIRST(wl);
3054 if (winfocus->iconic == 0)
3056 winfocus = TAILQ_NEXT(winfocus, entry);
3061 case SWM_ARG_ID_FOCUSMAIN:
3062 winfocus = TAILQ_FIRST(wl);
3063 if (winfocus == cur_focus)
3064 winfocus = cur_focus->ws->focus_prev;
3071 focus_magic(winfocus);
3075 cycle_layout(struct swm_region *r, union arg *args)
3077 struct workspace *ws = r->ws;
3080 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3083 if (ws->cur_layout->l_stack == NULL)
3084 ws->cur_layout = &layouts[0];
3088 a.id = SWM_ARG_ID_FOCUSCUR;
3094 stack_config(struct swm_region *r, union arg *args)
3096 struct workspace *ws = r->ws;
3098 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3101 if (ws->cur_layout->l_config != NULL)
3102 ws->cur_layout->l_config(ws, args->id);
3104 if (args->id != SWM_ARG_ID_STACKINIT)
3111 struct swm_geometry g;
3112 struct swm_region *r;
3118 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3120 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3121 for (i = 0; i < num_screens; i++) {
3125 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3126 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3127 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3129 /* start with screen geometry, adjust for bar */
3131 g.w -= 2 * border_width;
3132 g.h -= 2 * border_width;
3138 r->ws->cur_layout->l_stack(r->ws, &g);
3139 r->ws->cur_layout->l_string(r->ws);
3140 /* save r so we can track region changes */
3147 DNPRINTF(SWM_D_STACK, "stack: end\n");
3151 store_float_geom(struct ws_win *win, struct swm_region *r)
3153 /* retain window geom and region geom */
3154 win->g_float = win->g;
3155 win->g_float.x -= X(r);
3156 win->g_float.y -= Y(r);
3157 win->g_floatvalid = 1;
3158 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3159 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3160 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3161 win->g_float.w, win->g_float.h);
3165 stack_floater(struct ws_win *win, struct swm_region *r)
3170 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3173 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3174 * geom on ws switches or return from max mode
3176 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3177 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3178 /* refloat at last floating relative position */
3179 win->g = win->g_float;
3184 win->floatmaxed = 0;
3187 * if set to fullscreen mode, configure window to maximum size.
3189 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3190 if (!win->g_floatvalid)
3191 store_float_geom(win, win->ws->r);
3193 win->g = win->ws->r->g;
3197 * remove border on fullscreen floater when in fullscreen mode or when
3198 * the quirk is present.
3200 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3201 ((win->quirks & SWM_Q_FULLSCREEN) &&
3202 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3203 if (win->bordered) {
3205 X(win) += border_width;
3206 Y(win) += border_width;
3208 } else if (!win->bordered) {
3210 X(win) -= border_width;
3211 Y(win) -= border_width;
3214 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3215 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3216 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3221 * floaters and transients are auto-centred unless moved
3224 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3225 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3228 /* keep window within region bounds */
3229 constrain_window(win, r, 0);
3235 * Send keystrokes to terminal to decrease/increase the font size as the
3236 * window size changes.
3239 adjust_font(struct ws_win *win)
3241 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3242 win->floating || win->transient)
3245 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3246 WIDTH(win) / win->sh.width_inc < term_width &&
3247 win->font_steps < SWM_MAX_FONT_STEPS) {
3248 win->font_size_boundary[win->font_steps] =
3249 (win->sh.width_inc * term_width) + win->sh.base_width;
3252 win->last_inc = win->sh.width_inc;
3253 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3254 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3255 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3258 win->last_inc = win->sh.width_inc;
3259 fake_keypress(win, XK_KP_Add, ShiftMask);
3263 #define SWAPXY(g) do { \
3265 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3266 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3269 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3271 struct swm_geometry win_g, r_g = *g;
3272 struct ws_win *win, *fs_win = NULL;
3273 int i, j, s, stacks;
3274 int w_inc = 1, h_inc, w_base = 1, h_base;
3275 int hrh, extra = 0, h_slice, last_h = 0;
3276 int split, colno, winno, mwin, msize, mscale;
3277 int remain, missing, v_slice, reconfigure;
3279 xcb_get_window_attributes_reply_t *war;
3281 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3282 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3284 winno = count_win(ws, 0);
3285 if (winno == 0 && count_win(ws, 1) == 0)
3288 TAILQ_FOREACH(win, &ws->winlist, entry)
3289 if (win->transient == 0 && win->floating == 0
3290 && win->iconic == 0)
3297 w_inc = win->sh.width_inc;
3298 w_base = win->sh.base_width;
3299 mwin = ws->l_state.horizontal_mwin;
3300 mscale = ws->l_state.horizontal_msize;
3301 stacks = ws->l_state.horizontal_stacks;
3304 w_inc = win->sh.height_inc;
3305 w_base = win->sh.base_height;
3306 mwin = ws->l_state.vertical_mwin;
3307 mscale = ws->l_state.vertical_msize;
3308 stacks = ws->l_state.vertical_stacks;
3312 if (stacks > winno - mwin)
3313 stacks = winno - mwin;
3317 h_slice = r_g.h / SWM_H_SLICE;
3318 if (mwin && winno > mwin) {
3319 v_slice = r_g.w / SWM_V_SLICE;
3323 win_g.w = v_slice * mscale;
3325 if (w_inc > 1 && w_inc < v_slice) {
3326 /* adjust for window's requested size increment */
3327 remain = (win_g.w - w_base) % w_inc;
3333 win_g.x += r_g.w - msize;
3336 colno = split = winno / stacks;
3337 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3338 2 * border_width) / stacks);
3340 hrh = r_g.h / colno;
3341 extra = r_g.h - (colno * hrh);
3342 win_g.h = hrh - 2 * border_width;
3344 /* stack all the tiled windows */
3345 i = j = 0, s = stacks;
3346 TAILQ_FOREACH(win, &ws->winlist, entry) {
3347 if (win->transient != 0 || win->floating != 0)
3349 if (win->iconic != 0)
3352 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3357 if (split && i == split) {
3358 colno = (winno - mwin) / stacks;
3359 if (s <= (winno - mwin) % stacks)
3361 split = split + colno;
3362 hrh = (r_g.h / colno);
3363 extra = r_g.h - (colno * hrh);
3367 win_g.x += win_g.w + 2 * border_width;
3368 win_g.w = (r_g.w - msize -
3369 (stacks * 2 * border_width)) / stacks;
3371 win_g.w += (r_g.w - msize -
3372 (stacks * 2 * border_width)) % stacks;
3376 win_g.h = hrh - 2 * border_width;
3378 h_inc = win->sh.width_inc;
3379 h_base = win->sh.base_width;
3381 h_inc = win->sh.height_inc;
3382 h_base = win->sh.base_height;
3384 if (j == colno - 1) {
3385 win_g.h = hrh + extra;
3386 } else if (h_inc > 1 && h_inc < h_slice) {
3387 /* adjust for window's requested size increment */
3388 remain = (win_g.h - h_base) % h_inc;
3389 missing = h_inc - remain;
3391 if (missing <= extra || j == 0) {
3403 win_g.y += last_h + 2 * border_width;
3405 if (disable_border && bar_enabled == 0 && winno == 1){
3407 win_g.w += 2 * border_width;
3408 win_g.h += 2 * border_width;
3413 if (X(win) != win_g.y || Y(win) != win_g.x ||
3414 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3418 WIDTH(win) = win_g.h;
3419 HEIGHT(win) = win_g.w;
3422 if (X(win) != win_g.x || Y(win) != win_g.y ||
3423 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3427 WIDTH(win) = win_g.w;
3428 HEIGHT(win) = win_g.h;
3432 if (bordered != win->bordered) {
3434 win->bordered = bordered;
3442 war = xcb_get_window_attributes_reply(conn,
3443 xcb_get_window_attributes(conn, win->id),
3446 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3447 map_window_raised(win->id);
3457 /* now, stack all the floaters and transients */
3458 TAILQ_FOREACH(win, &ws->winlist, entry) {
3459 if (win->transient == 0 && win->floating == 0)
3461 if (win->iconic == 1)
3463 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3468 stack_floater(win, ws->r);
3469 map_window_raised(win->id);
3473 stack_floater(fs_win, ws->r);
3474 map_window_raised(fs_win->id);
3479 vertical_config(struct workspace *ws, int id)
3481 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3485 case SWM_ARG_ID_STACKRESET:
3486 case SWM_ARG_ID_STACKINIT:
3487 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3488 ws->l_state.vertical_mwin = 1;
3489 ws->l_state.vertical_stacks = 1;
3491 case SWM_ARG_ID_MASTERSHRINK:
3492 if (ws->l_state.vertical_msize > 1)
3493 ws->l_state.vertical_msize--;
3495 case SWM_ARG_ID_MASTERGROW:
3496 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3497 ws->l_state.vertical_msize++;
3499 case SWM_ARG_ID_MASTERADD:
3500 ws->l_state.vertical_mwin++;
3502 case SWM_ARG_ID_MASTERDEL:
3503 if (ws->l_state.vertical_mwin > 0)
3504 ws->l_state.vertical_mwin--;
3506 case SWM_ARG_ID_STACKINC:
3507 ws->l_state.vertical_stacks++;
3509 case SWM_ARG_ID_STACKDEC:
3510 if (ws->l_state.vertical_stacks > 1)
3511 ws->l_state.vertical_stacks--;
3513 case SWM_ARG_ID_FLIPLAYOUT:
3514 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3522 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3524 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3526 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3530 horizontal_config(struct workspace *ws, int id)
3532 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3535 case SWM_ARG_ID_STACKRESET:
3536 case SWM_ARG_ID_STACKINIT:
3537 ws->l_state.horizontal_mwin = 1;
3538 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3539 ws->l_state.horizontal_stacks = 1;
3541 case SWM_ARG_ID_MASTERSHRINK:
3542 if (ws->l_state.horizontal_msize > 1)
3543 ws->l_state.horizontal_msize--;
3545 case SWM_ARG_ID_MASTERGROW:
3546 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3547 ws->l_state.horizontal_msize++;
3549 case SWM_ARG_ID_MASTERADD:
3550 ws->l_state.horizontal_mwin++;
3552 case SWM_ARG_ID_MASTERDEL:
3553 if (ws->l_state.horizontal_mwin > 0)
3554 ws->l_state.horizontal_mwin--;
3556 case SWM_ARG_ID_STACKINC:
3557 ws->l_state.horizontal_stacks++;
3559 case SWM_ARG_ID_STACKDEC:
3560 if (ws->l_state.horizontal_stacks > 1)
3561 ws->l_state.horizontal_stacks--;
3563 case SWM_ARG_ID_FLIPLAYOUT:
3564 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3572 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3574 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3576 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3579 /* fullscreen view */
3581 max_stack(struct workspace *ws, struct swm_geometry *g)
3583 struct swm_geometry gg = *g;
3584 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3585 int winno, num_screens;
3587 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3592 winno = count_win(ws, 0);
3593 if (winno == 0 && count_win(ws, 1) == 0)
3596 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3597 TAILQ_FOREACH(win, &ws->winlist, entry) {
3598 if (win->transient) {
3600 parent = find_window(win->transient);
3604 if (win->floating && win->floatmaxed == 0 ) {
3606 * retain geometry for retrieval on exit from
3609 store_float_geom(win, ws->r);
3610 win->floatmaxed = 1;
3613 /* only reconfigure if necessary */
3614 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3615 HEIGHT(win) != gg.h) {
3621 WIDTH(win) += 2 * border_width;
3622 HEIGHT(win) += 2 * border_width;
3627 /* unmap only if we don't have multi screen */
3628 if (win != ws->focus)
3629 if (!(num_screens > 1 || outputs > 1))
3633 /* put the last transient on top */
3636 map_window_raised(parent->id);
3637 stack_floater(wintrans, ws->r);
3638 focus_magic(wintrans);
3643 send_to_ws(struct swm_region *r, union arg *args)
3645 int wsid = args->id;
3646 struct ws_win *win = NULL, *parent;
3647 struct workspace *ws, *nws;
3648 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3649 char ws_idx_str[SWM_PROPLEN];
3652 if (wsid >= workspace_limit)
3655 if (r && r->ws && r->ws->focus)
3661 if (win->ws->idx == wsid)
3664 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3667 nws = &win->s->ws[wsid];
3669 a.id = SWM_ARG_ID_FOCUSPREV;
3671 if (win->transient) {
3672 parent = find_window(win->transient);
3674 unmap_window(parent);
3675 TAILQ_REMOVE(&ws->winlist, parent, entry);
3676 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3681 TAILQ_REMOVE(&ws->winlist, win, entry);
3682 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3683 if (TAILQ_EMPTY(&ws->winlist))
3684 r->ws->focus = NULL;
3687 /* Try to update the window's workspace property */
3688 ws_idx_atom = get_atom_from_string("_SWM_WS");
3690 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3692 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3694 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3695 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3704 pressbutton(struct swm_region *r, union arg *args)
3706 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3707 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3708 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3709 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3713 raise_toggle(struct swm_region *r, union arg *args)
3715 if (r == NULL || r->ws == NULL)
3718 r->ws->always_raise = !r->ws->always_raise;
3720 /* bring floaters back to top */
3721 if (r->ws->always_raise == 0)
3726 iconify(struct swm_region *r, union arg *args)
3730 if (r->ws->focus == NULL)
3732 unmap_window(r->ws->focus);
3733 update_iconic(r->ws->focus, 1);
3736 r->ws->focus = NULL;
3737 a.id = SWM_ARG_ID_FOCUSCUR;
3742 get_win_name(xcb_window_t win)
3745 xcb_get_property_cookie_t c;
3746 xcb_icccm_get_text_property_reply_t r;
3748 c = xcb_icccm_get_wm_name(conn, win);
3749 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3750 if (r.name_len > 0) {
3751 name = malloc(r.name_len + 1);
3753 xcb_icccm_get_text_property_reply_wipe(&r);
3756 memcpy(name, r.name, r.name_len);
3757 name[r.name_len] = '\0';
3759 xcb_icccm_get_text_property_reply_wipe(&r);
3766 uniconify(struct swm_region *r, union arg *args)
3773 DNPRINTF(SWM_D_MISC, "uniconify\n");
3775 if (r == NULL || r->ws == NULL)
3778 /* make sure we have anything to uniconify */
3779 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3780 if (win->ws == NULL)
3781 continue; /* should never happen */
3782 if (win->iconic == 0)
3790 search_resp_action = SWM_SEARCH_UNICONIFY;
3792 spawn_select(r, args, "search", &searchpid);
3794 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3797 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3798 if (win->ws == NULL)
3799 continue; /* should never happen */
3800 if (win->iconic == 0)
3803 name = get_win_name(win->id);
3806 fprintf(lfile, "%s.%u\n", name, win->id);
3814 name_workspace(struct swm_region *r, union arg *args)
3818 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3824 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3826 spawn_select(r, args, "name_workspace", &searchpid);
3828 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3831 fprintf(lfile, "%s", "");
3836 search_workspace(struct swm_region *r, union arg *args)
3839 struct workspace *ws;
3842 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3848 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3850 spawn_select(r, args, "search", &searchpid);
3852 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3855 for (i = 0; i < workspace_limit; i++) {
3859 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3860 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3867 search_win_cleanup(void)
3869 struct search_window *sw = NULL;
3871 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3872 xcb_destroy_window(conn, sw->indicator);
3873 xcb_free_gc(conn, sw->gc);
3874 TAILQ_REMOVE(&search_wl, sw, entry);
3880 search_win(struct swm_region *r, union arg *args)
3882 struct ws_win *win = NULL;
3883 struct search_window *sw = NULL;
3885 uint32_t gcv[3], wa[2];
3890 XRectangle ibox, lbox;
3891 DNPRINTF(SWM_D_MISC, "search_win\n");
3894 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3896 spawn_select(r, args, "search", &searchpid);
3898 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3901 TAILQ_INIT(&search_wl);
3904 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3905 if (win->iconic == 1)
3908 sw = calloc(1, sizeof(struct search_window));
3910 warn("search_win: calloc");
3912 search_win_cleanup();
3918 snprintf(s, sizeof s, "%d", i);
3921 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3923 w = xcb_generate_id(conn);
3924 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3925 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3926 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3927 lbox.width + 4, bar_fs_extents->max_logical_extent.height,
3928 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3929 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3932 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3934 sw->gc = xcb_generate_id(conn);
3935 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3936 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3938 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3939 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3940 map_window_raised(w);
3942 xcb_image_text_8(conn, len, w, sw->gc, 2,
3943 (bar_fs_extents->max_logical_extent.height -
3944 lbox.height) / 2 - lbox.y, s);
3946 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3948 fprintf(lfile, "%d\n", i);
3956 search_resp_uniconify(char *resp, unsigned long len)
3962 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3964 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3965 if (win->iconic == 0)
3967 name = get_win_name(win->id);
3970 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3975 if (strncmp(s, resp, len) == 0) {
3976 /* XXX this should be a callback to generalize */
3977 update_iconic(win, 0);
3986 search_resp_name_workspace(char *resp, unsigned long len)
3988 struct workspace *ws;
3990 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3992 if (search_r->ws == NULL)
3997 free(search_r->ws->name);
3998 search_r->ws->name = NULL;
4002 ws->name = strdup(resp);
4003 if (ws->name == NULL) {
4004 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4005 "strdup: %s", strerror(errno));
4012 search_resp_search_workspace(char *resp, unsigned long len)
4019 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4023 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4030 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
4032 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4039 switchws(search_r, &a);
4043 search_resp_search_window(char *resp, unsigned long len)
4048 struct search_window *sw;
4050 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4054 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4059 idx = strtonum(s, 1, INT_MAX, &errstr);
4061 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4068 TAILQ_FOREACH(sw, &search_wl, entry)
4069 if (idx == sw->idx) {
4075 #define MAX_RESP_LEN 1024
4078 search_do_resp(void)
4084 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4089 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4090 warn("search: calloc");
4094 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4096 warn("search: read error");
4099 resp[rbytes] = '\0';
4102 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4103 * newline, so work around that by sanitizing the resp now.
4105 resp[strcspn(resp, "\n")] = '\0';
4108 switch (search_resp_action) {
4109 case SWM_SEARCH_UNICONIFY:
4110 search_resp_uniconify(resp, len);
4112 case SWM_SEARCH_NAME_WORKSPACE:
4113 search_resp_name_workspace(resp, len);
4115 case SWM_SEARCH_SEARCH_WORKSPACE:
4116 search_resp_search_workspace(resp, len);
4118 case SWM_SEARCH_SEARCH_WINDOW:
4119 search_resp_search_window(resp, len);
4124 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4125 search_win_cleanup();
4127 search_resp_action = SWM_SEARCH_NONE;
4128 close(select_resp_pipe[0]);
4133 wkill(struct swm_region *r, union arg *args)
4135 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4137 if (r->ws->focus == NULL)
4140 if (args->id == SWM_ARG_ID_KILLWINDOW)
4141 xcb_kill_client(conn, r->ws->focus->id);
4143 if (r->ws->focus->can_delete)
4144 client_msg(r->ws->focus, adelete);
4149 floating_toggle_win(struct ws_win *win)
4151 struct swm_region *r;
4161 /* reject floating toggles in max stack mode */
4162 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4165 if (win->floating) {
4166 if (!win->floatmaxed) {
4167 /* retain position for refloat */
4168 store_float_geom(win, r);
4172 if (win->g_floatvalid) {
4173 /* refloat at last floating relative position */
4174 X(win) = win->g_float.x + X(r);
4175 Y(win) = win->g_float.y + Y(r);
4176 WIDTH(win) = win->g_float.w;
4177 HEIGHT(win) = win->g_float.h;
4182 ewmh_update_actions(win);
4188 floating_toggle(struct swm_region *r, union arg *args)
4190 struct ws_win *win = r->ws->focus;
4196 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4197 _NET_WM_STATE_TOGGLE);
4201 if (win == win->ws->focus) {
4202 a.id = SWM_ARG_ID_FOCUSCUR;
4203 focus(win->ws->r, &a);
4208 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4210 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4212 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4214 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4217 if (X(win) + BORDER(win) < X(r)) {
4219 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4221 X(win) = X(r) - BORDER(win);
4224 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4226 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4228 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4231 if (Y(win) + BORDER(win) < Y(r)) {
4233 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4235 Y(win) = Y(r) - BORDER(win);
4241 if (HEIGHT(win) < 1)
4247 update_window(struct ws_win *win)
4252 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4253 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4254 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4258 wc[3] = HEIGHT(win);
4259 wc[4] = BORDER(win);
4261 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4262 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4263 wc[3], YESNO(win->bordered));
4265 xcb_configure_window(conn, win->id, mask, wc);
4268 #define SWM_RESIZE_STEPS (50)
4271 resize(struct ws_win *win, union arg *args)
4273 xcb_timestamp_t time = 0;
4274 struct swm_region *r = NULL;
4275 int resize_step = 0;
4276 struct swm_geometry g;
4277 int top = 0, left = 0, buttonrelease;
4279 unsigned int shape; /* cursor style */
4280 xcb_cursor_t cursor;
4281 xcb_font_t cursor_font;
4282 xcb_grab_pointer_reply_t *gpr;
4283 xcb_query_pointer_reply_t *xpr;
4284 xcb_generic_event_t *evt;
4285 xcb_motion_notify_event_t *mne;
4291 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4294 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%x, floating: %s, "
4295 "transient: 0x%x\n", win->id, YESNO(win->floating),
4298 if (!(win->transient != 0 || win->floating != 0))
4301 /* reject resizes in max mode for floaters (transient ok) */
4302 if (win->floatmaxed)
4306 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4312 case SWM_ARG_ID_WIDTHSHRINK:
4313 WIDTH(win) -= SWM_RESIZE_STEPS;
4316 case SWM_ARG_ID_WIDTHGROW:
4317 WIDTH(win) += SWM_RESIZE_STEPS;
4320 case SWM_ARG_ID_HEIGHTSHRINK:
4321 HEIGHT(win) -= SWM_RESIZE_STEPS;
4324 case SWM_ARG_ID_HEIGHTGROW:
4325 HEIGHT(win) += SWM_RESIZE_STEPS;
4332 constrain_window(win, r, 1);
4334 store_float_geom(win,r);
4338 /* get cursor offset from window root */
4339 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4346 if (xpr->win_x < WIDTH(win) / 2)
4349 if (xpr->win_y < HEIGHT(win) / 2)
4352 if (args->id == SWM_ARG_ID_CENTER)
4355 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4357 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4359 cursor_font = xcb_generate_id(conn);
4360 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4362 cursor = xcb_generate_id(conn);
4363 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4364 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4366 gpr = xcb_grab_pointer_reply(conn,
4367 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4368 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
4369 cursor, XCB_CURRENT_TIME),
4372 xcb_free_cursor(conn, cursor);
4373 xcb_close_font(conn, cursor_font);
4379 while ((evt = xcb_poll_for_event(conn)) && buttonrelease != 1) {
4381 XMaskEvent(display, MOUSEMASK | ExposureMask |
4382 SubstructureRedirectMask, &ev);
4384 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4385 case XCB_BUTTON_RELEASE:
4388 case XCB_MOTION_NOTIFY:
4389 mne = (xcb_motion_notify_event_t *)evt;
4390 /* cursor offset/delta from start of the operation */
4391 dx = mne->root_x - xpr->root_x;
4392 dy = mne->root_y - xpr->root_y;
4398 if (args->id == SWM_ARG_ID_CENTER) {
4399 if (g.h / 2 + dy < 1)
4403 HEIGHT(win) = g.h + 2 * dy;
4411 HEIGHT(win) = g.h + dy;
4418 if (args->id == SWM_ARG_ID_CENTER) {
4419 if (g.w / 2 + dx < 1)
4423 WIDTH(win) = g.w + 2 * dx;
4431 WIDTH(win) = g.w + dx;
4434 constrain_window(win, r, 1);
4436 /* not free, don't sync more than 120 times / second */
4437 if ((mne->time - time) > (1000 / 120) ) {
4453 store_float_geom(win,r);
4455 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4456 xcb_free_cursor(conn, cursor);
4457 xcb_close_font(conn, cursor_font);
4463 resize_step(struct swm_region *r, union arg *args)
4465 struct ws_win *win = NULL;
4467 if (r && r->ws && r->ws->focus)
4475 #define SWM_MOVE_STEPS (50)
4478 move(struct ws_win *win, union arg *args)
4480 xcb_timestamp_t time = 0;
4481 int move_step = 0, buttonrelease;
4482 struct swm_region *r = NULL;
4483 xcb_font_t cursor_font;
4484 xcb_cursor_t cursor;
4485 xcb_grab_pointer_reply_t *gpr;
4486 xcb_query_pointer_reply_t *qpr;
4487 xcb_generic_event_t *evt;
4488 xcb_motion_notify_event_t *mne;
4494 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4497 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%x, floating: %s, transient: "
4498 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4500 /* in max_stack mode should only move transients */
4501 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4505 if (win->floating == 0 && !win->transient) {
4506 store_float_geom(win, r);
4507 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4510 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4517 case SWM_ARG_ID_MOVELEFT:
4518 X(win) -= (SWM_MOVE_STEPS - border_width);
4521 case SWM_ARG_ID_MOVERIGHT:
4522 X(win) += (SWM_MOVE_STEPS - border_width);
4525 case SWM_ARG_ID_MOVEUP:
4526 Y(win) -= (SWM_MOVE_STEPS - border_width);
4529 case SWM_ARG_ID_MOVEDOWN:
4530 Y(win) += (SWM_MOVE_STEPS - border_width);
4537 constrain_window(win, r, 0);
4539 store_float_geom(win, r);
4543 cursor_font = xcb_generate_id(conn);
4544 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4546 cursor = xcb_generate_id(conn);
4547 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4548 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4550 gpr = xcb_grab_pointer_reply(conn,
4551 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4552 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4553 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME),
4556 xcb_free_cursor(conn, cursor);
4557 xcb_close_font(conn, cursor_font);
4561 /* get cursor offset from window root */
4562 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4565 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4566 xcb_free_cursor(conn, cursor);
4567 xcb_close_font(conn, cursor_font);
4572 while ((evt = xcb_poll_for_event(conn)) && buttonrelease != 1) {
4574 XMaskEvent(display, MOUSEMASK | ExposureMask |
4575 SubstructureRedirectMask, &ev);
4577 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4578 case XCB_BUTTON_RELEASE:
4581 case XCB_MOTION_NOTIFY:
4582 mne = (xcb_motion_notify_event_t *)evt;
4583 X(win) = mne->root_x - qpr->win_x - border_width;
4584 Y(win) = mne->root_y - qpr->win_y - border_width;
4586 constrain_window(win, r, 0);
4588 /* not free, don't sync more than 120 times / second */
4589 if ((mne->time - time) > (1000 / 120) ) {
4605 store_float_geom(win, r);
4607 xcb_free_cursor(conn, cursor);
4608 xcb_close_font(conn, cursor_font);
4609 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4613 move_step(struct swm_region *r, union arg *args)
4615 struct ws_win *win = NULL;
4617 if (r && r->ws && r->ws->focus)
4622 if (!(win->transient != 0 || win->floating != 0))
4629 /* user/key callable function IDs */
4718 kf_search_workspace,
4720 kf_dumpwins, /* MUST BE LAST */
4724 /* key definitions */
4726 dummykeyfunc(struct swm_region *r, union arg *args)
4731 char name[SWM_FUNCNAME_LEN];
4732 void (*func)(struct swm_region *r, union arg *);
4734 } keyfuncs[kf_invalid + 1] = {
4735 /* name function argument */
4736 { "cycle_layout", cycle_layout, {0} },
4737 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4738 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4739 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4740 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4741 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4742 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4743 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4744 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4745 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4746 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4747 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4748 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4749 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4750 { "quit", quit, {0} },
4751 { "restart", restart, {0} },
4752 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4753 { "ws_1", switchws, {.id = 0} },
4754 { "ws_2", switchws, {.id = 1} },
4755 { "ws_3", switchws, {.id = 2} },
4756 { "ws_4", switchws, {.id = 3} },
4757 { "ws_5", switchws, {.id = 4} },
4758 { "ws_6", switchws, {.id = 5} },
4759 { "ws_7", switchws, {.id = 6} },
4760 { "ws_8", switchws, {.id = 7} },
4761 { "ws_9", switchws, {.id = 8} },
4762 { "ws_10", switchws, {.id = 9} },
4763 { "ws_11", switchws, {.id = 10} },
4764 { "ws_12", switchws, {.id = 11} },
4765 { "ws_13", switchws, {.id = 12} },
4766 { "ws_14", switchws, {.id = 13} },
4767 { "ws_15", switchws, {.id = 14} },
4768 { "ws_16", switchws, {.id = 15} },
4769 { "ws_17", switchws, {.id = 16} },
4770 { "ws_18", switchws, {.id = 17} },
4771 { "ws_19", switchws, {.id = 18} },
4772 { "ws_20", switchws, {.id = 19} },
4773 { "ws_21", switchws, {.id = 20} },
4774 { "ws_22", switchws, {.id = 21} },
4775 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4776 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4777 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4778 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4779 { "ws_prior", priorws, {0} },
4780 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4781 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4782 { "mvws_1", send_to_ws, {.id = 0} },
4783 { "mvws_2", send_to_ws, {.id = 1} },
4784 { "mvws_3", send_to_ws, {.id = 2} },
4785 { "mvws_4", send_to_ws, {.id = 3} },
4786 { "mvws_5", send_to_ws, {.id = 4} },
4787 { "mvws_6", send_to_ws, {.id = 5} },
4788 { "mvws_7", send_to_ws, {.id = 6} },
4789 { "mvws_8", send_to_ws, {.id = 7} },
4790 { "mvws_9", send_to_ws, {.id = 8} },
4791 { "mvws_10", send_to_ws, {.id = 9} },
4792 { "mvws_11", send_to_ws, {.id = 10} },
4793 { "mvws_12", send_to_ws, {.id = 11} },
4794 { "mvws_13", send_to_ws, {.id = 12} },
4795 { "mvws_14", send_to_ws, {.id = 13} },
4796 { "mvws_15", send_to_ws, {.id = 14} },
4797 { "mvws_16", send_to_ws, {.id = 15} },
4798 { "mvws_17", send_to_ws, {.id = 16} },
4799 { "mvws_18", send_to_ws, {.id = 17} },
4800 { "mvws_19", send_to_ws, {.id = 18} },
4801 { "mvws_20", send_to_ws, {.id = 19} },
4802 { "mvws_21", send_to_ws, {.id = 20} },
4803 { "mvws_22", send_to_ws, {.id = 21} },
4804 { "bar_toggle", bar_toggle, {0} },
4805 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4806 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4807 { "float_toggle", floating_toggle,{0} },
4808 { "version", version, {0} },
4809 { "spawn_custom", dummykeyfunc, {0} },
4810 { "iconify", iconify, {0} },
4811 { "uniconify", uniconify, {0} },
4812 { "raise_toggle", raise_toggle, {0} },
4813 { "button2", pressbutton, {2} },
4814 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4815 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4816 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4817 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4818 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4819 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4820 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4821 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4822 { "name_workspace", name_workspace, {0} },
4823 { "search_workspace", search_workspace, {0} },
4824 { "search_win", search_win, {0} },
4825 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4826 { "invalid key func", NULL, {0} },
4829 RB_ENTRY(key) entry;
4832 enum keyfuncid funcid;
4835 RB_HEAD(key_tree, key);
4838 key_cmp(struct key *kp1, struct key *kp2)
4840 if (kp1->keysym < kp2->keysym)
4842 if (kp1->keysym > kp2->keysym)
4845 if (kp1->mod < kp2->mod)
4847 if (kp1->mod > kp2->mod)
4853 RB_GENERATE(key_tree, key, entry, key_cmp);
4854 struct key_tree keys;
4857 enum { client_click, root_click };
4859 unsigned int action;
4861 unsigned int button;
4862 void (*func)(struct ws_win *, union arg *);
4865 /* action key mouse button func args */
4866 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4867 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4868 { client_click, MODKEY, Button1, move, {0} },
4872 update_modkey(unsigned int mod)
4878 RB_FOREACH(kp, key_tree, &keys)
4879 if (kp->mod & ShiftMask)
4880 kp->mod = mod | ShiftMask;
4884 for (i = 0; i < LENGTH(buttons); i++)
4885 if (buttons[i].mask & ShiftMask)
4886 buttons[i].mask = mod | ShiftMask;
4888 buttons[i].mask = mod;
4893 TAILQ_ENTRY(spawn_prog) entry;
4898 TAILQ_HEAD(spawn_list, spawn_prog);
4899 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4902 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4905 struct spawn_prog *prog = NULL;
4907 char *ap, **real_args;
4909 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4912 TAILQ_FOREACH(prog, &spawns, entry) {
4913 if (!strcasecmp(spawn_name, prog->name))
4917 warnx("spawn_custom: program %s not found", spawn_name);
4921 /* make room for expanded args */
4922 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4923 err(1, "spawn_custom: calloc real_args");
4925 /* expand spawn_args into real_args */
4926 for (i = 0; i < prog->argc; i++) {
4928 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4929 if (!strcasecmp(ap, "$bar_border")) {
4931 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4933 err(1, "spawn_custom border color");
4934 } else if (!strcasecmp(ap, "$bar_color")) {
4936 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4938 err(1, "spawn_custom bar color");
4939 } else if (!strcasecmp(ap, "$bar_font")) {
4940 if ((real_args[i] = strdup(bar_fonts))
4942 err(1, "spawn_custom bar fonts");
4943 } else if (!strcasecmp(ap, "$bar_font_color")) {
4945 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4947 err(1, "spawn_custom color font");
4948 } else if (!strcasecmp(ap, "$color_focus")) {
4950 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4952 err(1, "spawn_custom color focus");
4953 } else if (!strcasecmp(ap, "$color_unfocus")) {
4955 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4957 err(1, "spawn_custom color unfocus");
4959 /* no match --> copy as is */
4960 if ((real_args[i] = strdup(ap)) == NULL)
4961 err(1, "spawn_custom strdup(ap)");
4963 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4968 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4969 for (i = 0; i < prog->argc; i++)
4970 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4971 DNPRINTF(SWM_D_SPAWN, "\n");
4973 *ret_args = real_args;
4974 return (prog->argc);
4978 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4984 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4988 spawn(r->ws->idx, &a, 1);
4990 for (i = 0; i < spawn_argc; i++)
4996 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5002 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5006 if (pipe(select_list_pipe) == -1)
5007 err(1, "pipe error");
5008 if (pipe(select_resp_pipe) == -1)
5009 err(1, "pipe error");
5011 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5012 err(1, "could not disable SIGPIPE");
5013 switch (*pid = fork()) {
5015 err(1, "cannot fork");
5018 if (dup2(select_list_pipe[0], 0) == -1)
5020 if (dup2(select_resp_pipe[1], 1) == -1)
5022 close(select_list_pipe[1]);
5023 close(select_resp_pipe[0]);
5024 spawn(r->ws->idx, &a, 0);
5026 default: /* parent */
5027 close(select_list_pipe[0]);
5028 close(select_resp_pipe[1]);
5032 for (i = 0; i < spawn_argc; i++)
5038 spawn_insert(char *name, char *args)
5040 char *arg, *cp, *ptr;
5041 struct spawn_prog *sp;
5043 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5045 if ((sp = calloc(1, sizeof *sp)) == NULL)
5046 err(1, "spawn_insert: malloc");
5047 if ((sp->name = strdup(name)) == NULL)
5048 err(1, "spawn_insert: strdup");
5050 /* convert the arguments to an argument list */
5051 if ((ptr = cp = strdup(args)) == NULL)
5052 err(1, "spawn_insert: strdup");
5053 while ((arg = strsep(&ptr, " \t")) != NULL) {
5054 /* empty field; skip it */
5059 if ((sp->argv = realloc(sp->argv, sp->argc *
5060 sizeof *sp->argv)) == NULL)
5061 err(1, "spawn_insert: realloc");
5062 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5063 err(1, "spawn_insert: strdup");
5067 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5068 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5072 spawn_remove(struct spawn_prog *sp)
5076 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5078 TAILQ_REMOVE(&spawns, sp, entry);
5079 for (i = 0; i < sp->argc; i++)
5085 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5089 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5091 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5094 spawn_insert(name, args);
5096 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5100 setspawn(char *name, char *args)
5102 struct spawn_prog *sp;
5104 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5109 TAILQ_FOREACH(sp, &spawns, entry) {
5110 if (!strcmp(sp->name, name)) {
5114 spawn_replace(sp, name, args);
5115 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5119 if (*args == '\0') {
5120 warnx("error: setspawn: cannot find program: %s", name);
5124 spawn_insert(name, args);
5125 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5129 setconfspawn(char *selector, char *value, int flags)
5131 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5133 setspawn(selector, value);
5135 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5142 setconfspawn("term", "xterm", 0);
5143 setconfspawn("spawn_term", "xterm", 0);
5144 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5145 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5146 setconfspawn("lock", "xlock", 0);
5147 setconfspawn("initscr", "initscreen.sh", 0);
5148 setconfspawn("menu", "dmenu_run"
5151 " -nf $bar_font_color"
5153 " -sf $bar_color", 0);
5154 setconfspawn("search", "dmenu"
5158 " -nf $bar_font_color"
5160 " -sf $bar_color", 0);
5161 setconfspawn("name_workspace", "dmenu"
5165 " -nf $bar_font_color"
5167 " -sf $bar_color", 0);
5171 #define SWM_MODNAME_SIZE 32
5172 #define SWM_KEY_WS "\n+ \t"
5174 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5178 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5179 if (mod == NULL || ks == NULL) {
5180 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5183 if (keystr == NULL || strlen(keystr) == 0) {
5184 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5190 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5191 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5193 cp += (long)strspn(cp, SWM_KEY_WS);
5194 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5196 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5198 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5200 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5202 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5204 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5206 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5207 *mod |= ControlMask;
5209 *ks = XStringToKeysym(name);
5210 XConvertCase(*ks, ks, &uks);
5211 if (ks == NoSymbol) {
5213 "parsekeys: invalid key %s\n",
5219 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5224 strdupsafe(char *str)
5229 return (strdup(str));
5233 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5237 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5238 keyfuncs[kfid].name, spawn_name);
5240 if ((kp = malloc(sizeof *kp)) == NULL)
5241 err(1, "key_insert: malloc");
5246 kp->spawn_name = strdupsafe(spawn_name);
5247 RB_INSERT(key_tree, &keys, kp);
5249 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5253 key_lookup(unsigned int mod, KeySym ks)
5260 return (RB_FIND(key_tree, &keys, &kp));
5264 key_remove(struct key *kp)
5266 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5268 RB_REMOVE(key_tree, &keys, kp);
5269 free(kp->spawn_name);
5272 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5276 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5279 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5283 key_insert(mod, ks, kfid, spawn_name);
5285 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5289 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5294 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5295 keyfuncs[kfid].name, spawn_name);
5297 if ((kp = key_lookup(mod, ks)) != NULL) {
5298 if (kfid == kf_invalid)
5301 key_replace(kp, mod, ks, kfid, spawn_name);
5302 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5305 if (kfid == kf_invalid) {
5306 warnx("error: setkeybinding: cannot find mod/key combination");
5307 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5311 key_insert(mod, ks, kfid, spawn_name);
5312 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5316 setconfbinding(char *selector, char *value, int flags)
5318 enum keyfuncid kfid;
5321 struct spawn_prog *sp;
5322 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5323 if (selector == NULL) {
5324 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5325 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5327 setkeybinding(mod, ks, kfid, NULL);
5332 /* search by key function name */
5333 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5334 if (strncasecmp(selector, keyfuncs[kfid].name,
5335 SWM_FUNCNAME_LEN) == 0) {
5336 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5338 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5339 setkeybinding(mod, ks, kfid, NULL);
5345 /* search by custom spawn name */
5346 TAILQ_FOREACH(sp, &spawns, entry) {
5347 if (strcasecmp(selector, sp->name) == 0) {
5348 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5350 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5351 setkeybinding(mod, ks, kf_spawn_custom,
5358 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5365 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5366 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5367 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5368 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5369 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5370 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5371 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5372 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5374 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5375 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5376 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5377 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5380 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5381 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5382 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5383 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5384 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5385 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5386 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5387 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5388 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5389 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5390 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5391 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5392 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5393 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5394 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5395 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5396 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5397 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5398 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5399 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5400 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5401 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5402 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5403 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5404 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5405 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5406 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5407 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5408 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5409 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5410 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5411 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5412 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5414 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5415 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5416 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5419 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5420 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5421 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5422 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5424 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5425 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5426 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5427 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5428 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5429 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5430 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5431 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5432 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5433 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5434 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5435 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5436 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5437 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5438 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5439 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5440 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5441 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5442 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5443 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5444 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5445 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5446 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5447 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5448 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5449 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5450 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5451 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5452 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5453 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5454 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5455 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5456 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5457 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5458 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5459 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5460 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5462 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5471 while (RB_EMPTY(&keys) == 0) {
5472 kp = RB_ROOT(&keys);
5478 setkeymapping(char *selector, char *value, int flags)
5480 char keymapping_file[PATH_MAX];
5481 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5482 if (value[0] == '~')
5483 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5484 pwd->pw_dir, &value[1]);
5486 strlcpy(keymapping_file, value, sizeof keymapping_file);
5488 /* load new key bindings; if it fails, revert to default bindings */
5489 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5493 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5498 updatenumlockmask(void)
5501 xcb_get_modifier_mapping_reply_t *modmap_r;
5502 xcb_keycode_t *modmap, kc;
5504 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5507 modmap_r = xcb_get_modifier_mapping_reply(conn,
5508 xcb_get_modifier_mapping(conn),
5511 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5512 for (i = 0; i < 8; i++) {
5513 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5514 kc = modmap[i * modmap_r->keycodes_per_modifier
5517 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5519 numlockmask = (1 << i);
5531 xcb_keycode_t *code;
5532 unsigned int modifiers[] =
5533 { 0, LockMask, numlockmask, numlockmask | LockMask };
5536 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5537 updatenumlockmask();
5539 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5540 for (k = 0; k < num_screens; k++) {
5541 if (TAILQ_EMPTY(&screens[k].rl))
5543 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5545 RB_FOREACH(kp, key_tree, &keys) {
5546 if ((code = xcb_key_symbols_get_keycode(syms,
5548 for (j = 0; j < LENGTH(modifiers); j++)
5549 xcb_grab_key(conn, True,
5551 kp->mod | modifiers[j],
5552 *code, XCB_GRAB_MODE_ASYNC,
5553 XCB_GRAB_MODE_ASYNC);
5559 grabbuttons(struct ws_win *win, int focused)
5562 unsigned int modifiers[] =
5563 { 0, LockMask, numlockmask, numlockmask|LockMask };
5565 updatenumlockmask();
5566 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5567 XCB_BUTTON_MASK_ANY);
5569 for (i = 0; i < LENGTH(buttons); i++)
5570 if (buttons[i].action == client_click)
5571 for (j = 0; j < LENGTH(modifiers); j++)
5572 xcb_grab_button(conn, False, win->id,
5574 XCB_GRAB_MODE_ASYNC,
5581 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5582 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC,
5583 XCB_WINDOW_NONE, XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY,
5584 XCB_BUTTON_MASK_ANY);
5587 const char *quirkname[] = {
5588 "NONE", /* config string for "no value" */
5597 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5598 #define SWM_Q_WS "\n|+ \t"
5600 parsequirks(char *qstr, unsigned long *quirk)
5610 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5612 cp += (long)strspn(cp, SWM_Q_WS);
5613 for (i = 0; i < LENGTH(quirkname); i++) {
5614 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5615 DNPRINTF(SWM_D_QUIRK,
5616 "parsequirks: %s\n", name);
5621 *quirk |= 1 << (i-1);
5625 if (i >= LENGTH(quirkname)) {
5626 DNPRINTF(SWM_D_QUIRK,
5627 "parsequirks: invalid quirk [%s]\n", name);
5635 quirk_insert(const char *class, const char *name, unsigned long quirk)
5639 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5642 if ((qp = malloc(sizeof *qp)) == NULL)
5643 err(1, "quirk_insert: malloc");
5644 if ((qp->class = strdup(class)) == NULL)
5645 err(1, "quirk_insert: strdup");
5646 if ((qp->name = strdup(name)) == NULL)
5647 err(1, "quirk_insert: strdup");
5650 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5652 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5656 quirk_remove(struct quirk *qp)
5658 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5659 qp->name, qp->quirk);
5661 TAILQ_REMOVE(&quirks, qp, entry);
5666 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5670 quirk_replace(struct quirk *qp, const char *class, const char *name,
5671 unsigned long quirk)
5673 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5674 qp->name, qp->quirk);
5677 quirk_insert(class, name, quirk);
5679 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5683 setquirk(const char *class, const char *name, unsigned long quirk)
5687 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5690 TAILQ_FOREACH(qp, &quirks, entry) {
5691 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5695 quirk_replace(qp, class, name, quirk);
5696 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5701 warnx("error: setquirk: cannot find class/name combination");
5705 quirk_insert(class, name, quirk);
5706 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5710 setconfquirk(char *selector, char *value, int flags)
5712 char *cp, *class, *name;
5714 unsigned long quirks;
5715 if (selector == NULL)
5717 if ((cp = strchr(selector, ':')) == NULL)
5722 if ((retval = parsequirks(value, &quirks)) == 0)
5723 setquirk(class, name, quirks);
5730 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5731 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5732 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5733 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5734 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5735 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5736 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5737 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5738 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5739 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5740 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5741 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5742 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5745 /* conf file stuff */
5746 #define SWM_CONF_FILE "spectrwm.conf"
5747 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5751 SWM_S_BAR_AT_BOTTOM,
5752 SWM_S_BAR_BORDER_WIDTH,
5759 SWM_S_CLOCK_ENABLED,
5762 SWM_S_CYCLE_VISIBLE,
5764 SWM_S_DISABLE_BORDER,
5766 SWM_S_FOCUS_CLOSE_WRAP,
5767 SWM_S_FOCUS_DEFAULT,
5773 SWM_S_STACK_ENABLED,
5775 SWM_S_TITLE_CLASS_ENABLED,
5776 SWM_S_TITLE_NAME_ENABLED,
5777 SWM_S_URGENT_ENABLED,
5778 SWM_S_VERBOSE_LAYOUT,
5779 SWM_S_WINDOW_NAME_ENABLED,
5780 SWM_S_WORKSPACE_LIMIT
5784 setconfvalue(char *selector, char *value, int flags)
5790 case SWM_S_BAR_ACTION:
5792 if ((bar_argv[0] = strdup(value)) == NULL)
5793 err(1, "setconfvalue: bar_action");
5795 case SWM_S_BAR_AT_BOTTOM:
5796 bar_at_bottom = atoi(value);
5798 case SWM_S_BAR_BORDER_WIDTH:
5799 bar_border_width = atoi(value);
5800 if (bar_border_width < 0)
5801 bar_border_width = 0;
5803 case SWM_S_BAR_DELAY:
5804 bar_delay = atoi(value);
5806 case SWM_S_BAR_ENABLED:
5807 bar_enabled = atoi(value);
5809 case SWM_S_BAR_FONT:
5811 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5812 err(1, "setconfvalue: asprintf: failed to allocate "
5813 "memory for bar_fonts.");
5817 case SWM_S_BAR_FORMAT:
5819 if ((bar_format = strdup(value)) == NULL)
5820 err(1, "setconfvalue: bar_format");
5822 case SWM_S_BAR_JUSTIFY:
5823 if (!strcmp(value, "left"))
5824 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5825 else if (!strcmp(value, "center"))
5826 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5827 else if (!strcmp(value, "right"))
5828 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5830 errx(1, "invalid bar_justify");
5832 case SWM_S_BORDER_WIDTH:
5833 border_width = atoi(value);
5834 if (border_width < 0)
5837 case SWM_S_CLOCK_ENABLED:
5838 clock_enabled = atoi(value);
5840 case SWM_S_CLOCK_FORMAT:
5841 #ifndef SWM_DENY_CLOCK_FORMAT
5843 if ((clock_format = strdup(value)) == NULL)
5844 err(1, "setconfvalue: clock_format");
5847 case SWM_S_CYCLE_EMPTY:
5848 cycle_empty = atoi(value);
5850 case SWM_S_CYCLE_VISIBLE:
5851 cycle_visible = atoi(value);
5853 case SWM_S_DIALOG_RATIO:
5854 dialog_ratio = atof(value);
5855 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5858 case SWM_S_DISABLE_BORDER:
5859 disable_border = atoi(value);
5861 case SWM_S_FOCUS_CLOSE:
5862 if (!strcmp(value, "first"))
5863 focus_close = SWM_STACK_BOTTOM;
5864 else if (!strcmp(value, "last"))
5865 focus_close = SWM_STACK_TOP;
5866 else if (!strcmp(value, "next"))
5867 focus_close = SWM_STACK_ABOVE;
5868 else if (!strcmp(value, "previous"))
5869 focus_close = SWM_STACK_BELOW;
5871 errx(1, "focus_close");
5873 case SWM_S_FOCUS_CLOSE_WRAP:
5874 focus_close_wrap = atoi(value);
5876 case SWM_S_FOCUS_DEFAULT:
5877 if (!strcmp(value, "last"))
5878 focus_default = SWM_STACK_TOP;
5879 else if (!strcmp(value, "first"))
5880 focus_default = SWM_STACK_BOTTOM;
5882 errx(1, "focus_default");
5884 case SWM_S_FOCUS_MODE:
5885 if (!strcmp(value, "default"))
5886 focus_mode = SWM_FOCUS_DEFAULT;
5887 else if (!strcmp(value, "follow_cursor"))
5888 focus_mode = SWM_FOCUS_FOLLOW;
5889 else if (!strcmp(value, "synergy"))
5890 focus_mode = SWM_FOCUS_SYNERGY;
5892 errx(1, "focus_mode");
5894 case SWM_S_SPAWN_ORDER:
5895 if (!strcmp(value, "first"))
5896 spawn_position = SWM_STACK_BOTTOM;
5897 else if (!strcmp(value, "last"))
5898 spawn_position = SWM_STACK_TOP;
5899 else if (!strcmp(value, "next"))
5900 spawn_position = SWM_STACK_ABOVE;
5901 else if (!strcmp(value, "previous"))
5902 spawn_position = SWM_STACK_BELOW;
5904 errx(1, "spawn_position");
5906 case SWM_S_SPAWN_TERM:
5907 setconfspawn("term", value, 0);
5908 setconfspawn("spawn_term", value, 0);
5912 case SWM_S_SS_ENABLED:
5913 ss_enabled = atoi(value);
5915 case SWM_S_STACK_ENABLED:
5916 stack_enabled = atoi(value);
5918 case SWM_S_TERM_WIDTH:
5919 term_width = atoi(value);
5923 case SWM_S_TITLE_CLASS_ENABLED:
5924 title_class_enabled = atoi(value);
5926 case SWM_S_TITLE_NAME_ENABLED:
5927 title_name_enabled = atoi(value);
5929 case SWM_S_URGENT_ENABLED:
5930 urgent_enabled = atoi(value);
5932 case SWM_S_VERBOSE_LAYOUT:
5933 verbose_layout = atoi(value);
5934 for (i = 0; layouts[i].l_stack != NULL; i++) {
5936 layouts[i].l_string = fancy_stacker;
5938 layouts[i].l_string = plain_stacker;
5941 case SWM_S_WINDOW_NAME_ENABLED:
5942 window_name_enabled = atoi(value);
5944 case SWM_S_WORKSPACE_LIMIT:
5945 workspace_limit = atoi(value);
5946 if (workspace_limit > SWM_WS_MAX)
5947 workspace_limit = SWM_WS_MAX;
5948 else if (workspace_limit < 1)
5949 workspace_limit = 1;
5958 setconfmodkey(char *selector, char *value, int flags)
5960 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5961 update_modkey(Mod1Mask);
5962 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5963 update_modkey(Mod2Mask);
5964 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5965 update_modkey(Mod3Mask);
5966 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5967 update_modkey(Mod4Mask);
5974 setconfcolor(char *selector, char *value, int flags)
5976 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5981 setconfregion(char *selector, char *value, int flags)
5983 custom_region(value);
5988 setautorun(char *selector, char *value, int flags)
5998 if (getenv("SWM_STARTED"))
6002 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6003 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6005 if (ws_id < 0 || ws_id >= workspace_limit)
6006 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6009 * This is a little intricate
6011 * If the pid already exists we simply reuse it because it means it was
6012 * used before AND not claimed by manage_window. We get away with
6013 * altering it in the parent after INSERT because this can not be a race
6016 while ((ap = strsep(&sp, " \t")) != NULL) {
6019 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6021 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6022 err(1, "setautorun: realloc");
6023 a.argv[argc - 1] = ap;
6026 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6027 err(1, "setautorun: realloc");
6028 a.argv[argc] = NULL;
6030 if ((pid = fork()) == 0) {
6031 spawn(ws_id, &a, 1);
6040 p = calloc(1, sizeof *p);
6043 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6053 setlayout(char *selector, char *value, int flags)
6055 int ws_id, i, x, mg, ma, si, raise, f = 0;
6056 int st = SWM_V_STACK, num_screens;
6058 struct workspace *ws;
6060 if (getenv("SWM_STARTED"))
6064 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6065 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6066 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6067 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6070 if (ws_id < 0 || ws_id >= workspace_limit)
6071 errx(1, "layout: invalid workspace %d", ws_id + 1);
6073 if (!strcasecmp(s, "vertical"))
6075 else if (!strcasecmp(s, "vertical_flip")) {
6078 } else if (!strcasecmp(s, "horizontal"))
6080 else if (!strcasecmp(s, "horizontal_flip")) {
6083 } else if (!strcasecmp(s, "fullscreen"))
6086 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6087 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6090 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6091 for (i = 0; i < num_screens; i++) {
6092 ws = (struct workspace *)&screens[i].ws;
6093 ws[ws_id].cur_layout = &layouts[st];
6095 ws[ws_id].always_raise = raise;
6096 if (st == SWM_MAX_STACK)
6100 for (x = 0; x < abs(mg); x++) {
6101 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6102 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6103 SWM_ARG_ID_MASTERSHRINK);
6107 for (x = 0; x < abs(ma); x++) {
6108 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6109 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6110 SWM_ARG_ID_MASTERDEL);
6114 for (x = 0; x < abs(si); x++) {
6115 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6116 si >= 0 ? SWM_ARG_ID_STACKINC :
6117 SWM_ARG_ID_STACKDEC);
6122 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6123 SWM_ARG_ID_FLIPLAYOUT);
6131 /* config options */
6132 struct config_option {
6134 int (*func)(char*, char*, int);
6137 struct config_option configopt[] = {
6138 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6139 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6140 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6141 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6142 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6143 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6144 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6145 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6146 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6147 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6148 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6149 { "keyboard_mapping", setkeymapping, 0 },
6150 { "bind", setconfbinding, 0 },
6151 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6152 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6153 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6154 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6155 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6156 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6157 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6158 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6159 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6160 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6161 { "modkey", setconfmodkey, 0 },
6162 { "program", setconfspawn, 0 },
6163 { "quirk", setconfquirk, 0 },
6164 { "region", setconfregion, 0 },
6165 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6166 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6167 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6168 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6169 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6170 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6171 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6172 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6173 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6174 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6175 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6176 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6177 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6178 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6179 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6180 { "autorun", setautorun, 0 },
6181 { "layout", setlayout, 0 },
6186 conf_load(char *filename, int keymapping)
6189 char *line, *cp, *optsub, *optval;
6190 size_t linelen, lineno = 0;
6191 int wordlen, i, optind;
6192 struct config_option *opt;
6194 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6196 if (filename == NULL) {
6197 warnx("conf_load: no filename");
6200 if ((config = fopen(filename, "r")) == NULL) {
6201 warn("conf_load: fopen: %s", filename);
6205 while (!feof(config)) {
6206 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6209 err(1, "%s", filename);
6214 cp += strspn(cp, " \t\n"); /* eat whitespace */
6215 if (cp[0] == '\0') {
6220 /* get config option */
6221 wordlen = strcspn(cp, "=[ \t\n");
6223 warnx("%s: line %zd: no option found",
6228 for (i = 0; i < LENGTH(configopt); i++) {
6229 opt = &configopt[i];
6230 if (!strncasecmp(cp, opt->optname, wordlen) &&
6231 strlen(opt->optname) == wordlen) {
6237 warnx("%s: line %zd: unknown option %.*s",
6238 filename, lineno, wordlen, cp);
6241 if (keymapping && strcmp(opt->optname, "bind")) {
6242 warnx("%s: line %zd: invalid option %.*s",
6243 filename, lineno, wordlen, cp);
6247 cp += strspn(cp, " \t\n"); /* eat whitespace */
6248 /* get [selector] if any */
6252 wordlen = strcspn(cp, "]");
6255 warnx("%s: line %zd: syntax error",
6260 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6262 warnx("%s: line %zd: unable to allocate"
6263 "memory for selector", filename,
6269 cp += strspn(cp, "] \t\n"); /* eat trailing */
6271 cp += strspn(cp, "= \t\n"); /* eat trailing */
6273 optval = strdup(cp);
6274 /* call function to deal with it all */
6275 if (configopt[optind].func(optsub, optval,
6276 configopt[optind].funcflags) != 0)
6277 errx(1, "%s: line %zd: invalid data for %s",
6278 filename, lineno, configopt[optind].optname);
6285 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6292 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6298 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6300 struct ws_win *parent, *w;
6301 struct swm_region *r;
6302 struct workspace *ws;
6303 xcb_icccm_wm_hints_t wmh;
6305 parent = find_window(win->transient);
6307 parent->child_trans = win;
6309 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6310 " for 0x%x trans 0x%x\n", win->id, win->transient);
6312 r = root_to_region(win->wa->root);
6314 /* parent doen't exist in our window list */
6315 TAILQ_FOREACH(w, &ws->winlist, entry) {
6316 if (xcb_icccm_get_wm_hints_reply(conn,
6317 xcb_icccm_get_wm_hints(conn, w->id),
6319 warnx("can't get hints for 0x%x", w->id);
6323 if (win->hints.window_group != wmh.window_group)
6326 w->child_trans = win;
6327 win->transient = w->id;
6329 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6330 "transient to 0x%x\n", win->transient);
6337 window_get_pid(xcb_window_t win)
6342 xcb_get_property_cookie_t pc;
6343 xcb_get_property_reply_t *pr;
6345 apid = get_atom_from_string("_NET_WM_PID");
6346 if (apid == XCB_ATOM_NONE)
6349 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6350 pr = xcb_get_property_reply(conn, pc, NULL);
6353 if (pr->type != XCB_ATOM_CARDINAL)
6356 ret = *(long *)xcb_get_property_value(pr);
6362 apid = get_atom_from_string("_SWM_PID");
6363 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6365 pr = xcb_get_property_reply(conn, pc, NULL);
6368 if (pr->type != XCB_ATOM_STRING)
6371 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6378 manage_window(xcb_window_t id)
6380 xcb_window_t trans = XCB_WINDOW_NONE;
6381 struct workspace *ws;
6382 struct ws_win *win, *ww;
6383 int i, ws_idx, border_me = 0;
6384 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6385 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6387 struct swm_region *r;
6391 uint32_t event_mask;
6393 xcb_get_property_reply_t *gpr;
6394 xcb_icccm_get_wm_protocols_reply_t wpr;
6396 if ((win = find_window(id)) != NULL)
6397 return (win); /* already being managed */
6399 /* see if we are on the unmanaged list */
6400 if ((win = find_unmanaged_window(id)) != NULL) {
6401 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6402 "window: 0x%x\n", win->id);
6403 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6405 set_child_transient(win, &trans);
6407 if (trans && (ww = find_window(trans)))
6408 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6409 else if ((ww = win->ws->focus) &&
6410 spawn_position == SWM_STACK_ABOVE)
6411 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6413 else if (ww && spawn_position == SWM_STACK_BELOW)
6414 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6415 else switch (spawn_position) {
6418 case SWM_STACK_ABOVE:
6419 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6421 case SWM_STACK_BOTTOM:
6422 case SWM_STACK_BELOW:
6423 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6426 ewmh_update_actions(win);
6430 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6431 err(1, "manage_window: calloc: failed to allocate memory for "
6437 /* see if we need to override the workspace */
6438 p = find_pid(window_get_pid(id));
6440 /* Get all the window data in one shot */
6441 ws_idx_atom = get_atom_from_string("_SWM_WS");
6443 gpr = xcb_get_property_reply(conn,
6444 xcb_get_property(conn, False, id, ws_idx_atom,
6445 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6448 proplen = xcb_get_property_value_length(gpr);
6450 prop = malloc(proplen + 1);
6453 xcb_get_property_value(gpr),
6455 prop[proplen] = '\0';
6461 win->wa = xcb_get_geometry_reply(conn,
6462 xcb_get_geometry(conn, id),
6464 xcb_icccm_get_wm_normal_hints_reply(conn,
6465 xcb_icccm_get_wm_normal_hints(conn, id),
6467 xcb_icccm_get_wm_hints_reply(conn,
6468 xcb_icccm_get_wm_hints(conn, id),
6470 xcb_icccm_get_wm_transient_for_reply(conn,
6471 xcb_icccm_get_wm_transient_for(conn, id),
6474 win->transient = trans;
6475 set_child_transient(win, &trans);
6476 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6477 "transient: 0x%x\n", win->id, win->transient);
6480 prot = get_atom_from_string("WM_PROTOCOLS");
6482 /* get supported protocols */
6483 if (xcb_icccm_get_wm_protocols_reply(conn,
6484 xcb_icccm_get_wm_protocols(conn, id, prot),
6486 for (i = 0; i < wpr.atoms_len; i++) {
6487 if (wpr.atoms[i] == takefocus)
6488 win->take_focus = 1;
6489 if (wpr.atoms[i] == adelete)
6490 win->can_delete = 1;
6492 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6495 win->iconic = get_iconic(win);
6498 * Figure out where to put the window. If it was previously assigned to
6499 * a workspace (either by spawn() or manually moving), and isn't
6500 * transient, * put it in the same workspace
6502 r = root_to_region(win->wa->root);
6504 ws = &r->s->ws[p->ws];
6505 TAILQ_REMOVE(&pidlist, p, entry);
6508 } else if (prop && win->transient == 0) {
6509 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6510 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6513 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6516 ws = &r->s->ws[ws_idx];
6519 /* this should launch transients in the same ws as parent */
6521 if ((ww = find_window(trans)) != NULL)
6527 warnx("manage_window: fix this "
6533 /* set up the window layout */
6536 win->s = r->s; /* this never changes */
6537 if (trans && (ww = find_window(trans)))
6538 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6539 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6540 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6542 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6543 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6544 else switch (spawn_position) {
6547 case SWM_STACK_ABOVE:
6548 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6550 case SWM_STACK_BOTTOM:
6551 case SWM_STACK_BELOW:
6552 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6555 /* ignore window border if there is one. */
6556 WIDTH(win) = win->wa->width;
6557 HEIGHT(win) = win->wa->height;
6558 X(win) = win->wa->x + win->wa->border_width;
6559 Y(win) = win->wa->y + win->wa->border_width;
6561 win->g_floatvalid = 0;
6562 win->floatmaxed = 0;
6563 win->ewmh_flags = 0;
6565 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6566 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6567 HEIGHT(win), ws->idx);
6569 constrain_window(win, r, 0);
6571 /* Set window properties so we can remember this after reincarnation */
6572 if (ws_idx_atom && prop == NULL &&
6573 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6575 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6577 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6578 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6584 ewmh_autoquirk(win);
6586 if (xcb_icccm_get_wm_class_reply(conn,
6587 xcb_icccm_get_wm_class(conn, win->id),
6589 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6590 win->ch.class_name, win->ch.instance_name);
6592 /* java is retarded so treat it special */
6593 if (strstr(win->ch.instance_name, "sun-awt")) {
6598 TAILQ_FOREACH(qp, &quirks, entry) {
6599 if (!strcmp(win->ch.class_name, qp->class) &&
6600 !strcmp(win->ch.instance_name, qp->name)) {
6601 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6602 "class: %s, name: %s\n", win->ch.class_name,
6603 win->ch.instance_name);
6604 if (qp->quirk & SWM_Q_FLOAT) {
6608 win->quirks = qp->quirk;
6613 /* alter window position if quirky */
6614 if (win->quirks & SWM_Q_ANYWHERE) {
6615 win->manual = 1; /* don't center the quirky windows */
6616 if (bar_enabled && Y(win) < bar_height)
6617 Y(win) = bar_height;
6618 if (WIDTH(win) + X(win) > WIDTH(r))
6619 X(win) = WIDTH(r) - WIDTH(win) - 2;
6623 /* Reset font sizes (the bruteforce way; no default keybinding). */
6624 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6625 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6626 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6627 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6628 fake_keypress(win, XK_KP_Add, ShiftMask);
6631 ewmh_get_win_state(win);
6632 ewmh_update_actions(win);
6633 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6638 X(win) -= border_width;
6639 Y(win) -= border_width;
6643 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6644 XCB_EVENT_MASK_PROPERTY_CHANGE |
6645 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6647 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6649 /* floaters need to be mapped if they are in the current workspace */
6650 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6651 map_window_raised(win->id);
6657 free_window(struct ws_win *win)
6659 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6664 /* needed for restart wm */
6665 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
6667 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6672 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6677 memset(win, 0xff, sizeof *win); /* XXX kill later */
6683 unmanage_window(struct ws_win *win)
6685 struct ws_win *parent;
6686 xcb_screen_t *screen;
6691 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6693 if (win->transient) {
6694 parent = find_window(win->transient);
6696 parent->child_trans = NULL;
6699 /* focus on root just in case */
6700 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6701 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6702 screen->root, XCB_CURRENT_TIME);
6706 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6707 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6711 focus_magic(struct ws_win *win)
6713 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6716 /* if there are no windows clear the status-bar */
6721 if (win->child_trans) {
6722 /* win = parent & has a transient so focus on that */
6724 focus_win(win->child_trans);
6725 if (win->child_trans->take_focus)
6726 client_msg(win, takefocus);
6728 /* make sure transient hasn't disappeared */
6729 if (validate_win(win->child_trans) == 0) {
6730 focus_win(win->child_trans);
6731 if (win->child_trans->take_focus)
6732 client_msg(win->child_trans, takefocus);
6734 win->child_trans = NULL;
6736 if (win->take_focus)
6737 client_msg(win, takefocus);
6743 if (win->take_focus)
6744 client_msg(win, takefocus);
6749 expose(xcb_expose_event_t *e)
6751 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6755 keypress(xcb_key_press_event_t *e)
6759 struct swm_region *r;
6761 keysym = XkbKeycodeToKeysym(display, (KeyCode)e->detail, 0, 0);
6763 DNPRINTF(SWM_D_EVENT, "keypress: %u\n", e->detail);
6765 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6767 if (keyfuncs[kp->funcid].func == NULL)
6770 r = root_to_region(e->root);
6771 if (kp->funcid == kf_spawn_custom)
6772 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6774 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6778 buttonpress(xcb_button_press_event_t *e)
6783 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x\n", e->event);
6785 if ((win = find_window(e->event)) == NULL)
6789 action = client_click;
6791 for (i = 0; i < LENGTH(buttons); i++)
6792 if (action == buttons[i].action && buttons[i].func &&
6793 buttons[i].button == e->detail &&
6794 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6795 buttons[i].func(win, &buttons[i].args);
6799 configurerequest(xcb_configure_request_event_t *e)
6804 uint32_t wc[7] = {0};
6806 if ((win = find_window(e->window)) == NULL)
6807 if ((win = find_unmanaged_window(e->window)) == NULL)
6811 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6812 mask |= XCB_CONFIG_WINDOW_X;
6815 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6816 mask |= XCB_CONFIG_WINDOW_Y;
6819 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6820 mask |= XCB_CONFIG_WINDOW_WIDTH;
6823 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6824 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6825 wc[i++] = e->height;
6827 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6828 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6829 wc[i++] = e->border_width;
6831 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6832 mask |= XCB_CONFIG_WINDOW_SIBLING;
6833 wc[i++] = e->sibling;
6835 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6836 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6837 wc[i++] = e->stack_mode;
6840 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6841 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e->window,
6842 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6844 xcb_configure_window(conn, e->window, mask, wc);
6845 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6846 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6847 win->g_float.x = e->x - X(win->ws->r);
6848 win->g_float.y = e->y - Y(win->ws->r);
6849 win->g_float.w = e->width;
6850 win->g_float.h = e->height;
6851 win->g_floatvalid = 1;
6853 if (win->floating) {
6854 win->g = win->g_float;
6855 win->g.x += X(win->ws->r);
6856 win->g.y += Y(win->ws->r);
6867 configurenotify(xcb_configure_notify_event_t *e)
6871 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6874 win = find_window(e->window);
6876 xcb_icccm_get_wm_normal_hints_reply(conn,
6877 xcb_icccm_get_wm_normal_hints(conn, win->id),
6886 destroynotify(xcb_destroy_notify_event_t *e)
6890 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6892 if ((win = find_window(e->window)) == NULL) {
6893 if ((win = find_unmanaged_window(e->window)) == NULL)
6899 /* make sure we focus on something */
6902 unmanage_window(win);
6908 enternotify(xcb_enter_notify_event_t *e)
6913 Window focus_return;
6914 int revert_to_return;
6916 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6917 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6918 "state: %d\n", e->event, e->mode, e->detail, e->root,
6919 e->child, YESNO(e->same_screen_focus), e->state);
6921 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6922 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6927 switch (focus_mode) {
6928 case SWM_FOCUS_DEFAULT:
6930 case SWM_FOCUS_FOLLOW:
6932 case SWM_FOCUS_SYNERGY:
6935 * all these checks need to be in this order because the
6936 * XCheckTypedWindowEvent relies on weeding out the previous events
6938 * making this code an option would enable a follow mouse for focus
6943 * state is set when we are switching workspaces and focus is set when
6944 * the window or a subwindow already has focus (occurs during restart).
6946 * Only honor the focus flag if last_focus_event is not FocusOut,
6947 * this allows spectrwm to continue to control focus when another
6948 * program is also playing with it.
6950 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6951 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6956 * happens when a window is created or destroyed and the border
6957 * crosses the mouse pointer and when switching ws
6959 * we need the subwindow test to see if we came from root in order
6960 * to give focus to floaters
6962 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6963 ev->subwindow == 0) {
6964 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6968 /* this window already has focus */
6969 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6970 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6974 /* this window is being deleted or moved to another ws */
6975 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6977 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6978 XPutBackEvent(display, &cne);
6982 if ((win = find_window(ev->window)) == NULL) {
6983 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6988 * In fullstack kill all enters unless they come from a different ws
6989 * (i.e. another region) or focus has been grabbed externally.
6991 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6992 last_focus_event != FocusOut) {
6993 XGetInputFocus(display, &focus_return, &revert_to_return);
6994 if ((w = find_window(focus_return)) == NULL ||
6996 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
7004 if ((win = find_window(e->event)) == NULL) {
7005 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7012 /* lets us use one switch statement for arbitrary mode/detail combinations */
7013 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7016 mapnotify(xcb_map_notify_event_t *e)
7020 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7022 win = manage_window(e->window);
7024 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7027 * focus_win can only set input focus on a mapped window.
7028 * make sure the window really has focus since it is just being mapped.
7030 if (win->ws->focus == win)
7035 mappingnotify(xcb_mapping_notify_event_t *e)
7037 xcb_refresh_keyboard_mapping(syms, e);
7039 if (e->request == XCB_MAPPING_KEYBOARD)
7044 maprequest(xcb_map_request_event_t *e)
7047 struct swm_region *r;
7048 xcb_get_window_attributes_reply_t *war;
7050 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7053 war = xcb_get_window_attributes_reply(conn,
7054 xcb_get_window_attributes(conn, e->window),
7058 if (war->override_redirect) {
7064 win = manage_window(e->window);
7066 return; /* can't happen */
7071 /* make new win focused */
7072 r = root_to_region(win->wa->root);
7073 if (win->ws == r->ws)
7078 propertynotify(xcb_property_notify_event_t *e)
7084 xcb_get_atom_name_reply_t *r;
7086 r = xcb_get_atom_name_reply(conn,
7087 xcb_get_atom_name(conn, e->atom),
7090 len = xcb_get_atom_name_name_length(r);
7092 name = malloc(len + 1);
7094 memcpy(name, xcb_get_atom_name_name(r), len);
7097 DNPRINTF(SWM_D_EVENT,
7098 "propertynotify: window: 0x%x, "
7108 win = find_window(e->window);
7112 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7113 update_iconic(win, 0);
7114 map_window_raised(win->id);
7122 case XCB_ATOM_WM_NORMAL_HINTS:
7123 xcb_icccm_get_wm_normal_hints(conn,
7124 xcb_iccom_get_wm_normal_hints(conn, win->id),
7126 warnx("normal hints: flag 0x%x", win->sh.flags);
7127 if (win->sh.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
7128 WIDTH(win) = win->sh.min_width;
7129 HEIGHT(win) = win->sh.min_height;
7130 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7132 XMoveResizeWindow(display, win->id,
7133 X(win), Y(win), WIDTH(win), HEIGHT(win));
7135 case XCB_ATOM_WM_CLASS:
7136 case XCB_ATOM_WM_NAME:
7145 unmapnotify(xcb_unmap_notify_event_t *e)
7149 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7151 /* determine if we need to help unmanage this window */
7152 win = find_window(e->window);
7156 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7157 unmanage_window(win);
7160 /* resend unmap because we ated it */
7161 xcb_unmap_window(conn, e->window);
7166 visibilitynotify(xcb_visibility_notify_event_t *e)
7169 struct swm_region *r;
7171 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7174 if (e->state == XCB_VISIBILITY_UNOBSCURED) {
7175 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7176 for (i = 0; i < num_screens; i++)
7177 TAILQ_FOREACH(r, &screens[i].rl, entry)
7178 if (e->window == WINID(r->bar))
7184 clientmessage(xcb_client_message_event_t *e)
7188 win = find_window(e->window);
7193 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, type: %u\n",
7194 e->window, e->response_type);
7196 if (e->response_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7197 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7200 if (e->response_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7201 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7202 if (win->can_delete)
7203 client_msg(win, adelete);
7205 xcb_kill_client(conn, win->id);
7207 if (e->response_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7208 DNPRINTF(SWM_D_EVENT,
7209 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7210 if (win->floating) {
7211 if (e->data.data32[0] & (1<<8)) /* x */
7212 X(win) = e->data.data32[1];
7213 if (e->data.data32[0] & (1<<9)) /* y */
7214 Y(win) = e->data.data32[2];
7215 if (e->data.data32[0] & (1<<10)) /* width */
7216 WIDTH(win) = e->data.data32[3];
7217 if (e->data.data32[0] & (1<<11)) /* height */
7218 HEIGHT(win) = e->data.data32[4];
7223 /* TODO: Change stack sizes */
7224 /* notify no change was made. */
7225 config_win(win, NULL);
7228 if (e->response_type == ewmh[_NET_WM_STATE].atom) {
7229 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7230 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7231 if (e->data.data32[2])
7232 ewmh_update_win_state(win, e->data.data32[2],
7240 xerror_start(Display *d, XErrorEvent *ee)
7247 xerror(Display *d, XErrorEvent *ee)
7249 /* warnx("error: %p %p", display, ee); */
7257 xerrorxlib = XSetErrorHandler(xerror_start);
7259 /* this causes an error if some other window manager is running */
7260 XSelectInput(display, DefaultRootWindow(display),
7261 SubstructureRedirectMask);
7266 XSetErrorHandler(xerror);
7272 new_region(struct swm_screen *s, int x, int y, int w, int h)
7274 struct swm_region *r, *n;
7275 struct workspace *ws = NULL;
7278 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7279 s->idx, w, h, x, y);
7281 /* remove any conflicting regions */
7282 n = TAILQ_FIRST(&s->rl);
7285 n = TAILQ_NEXT(r, entry);
7286 if (X(r) < (x + w) &&
7287 (X(r) + WIDTH(r)) > x &&
7289 (Y(r) + HEIGHT(r)) > y) {
7290 if (r->ws->r != NULL)
7291 r->ws->old_r = r->ws->r;
7294 TAILQ_REMOVE(&s->rl, r, entry);
7295 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7299 /* search old regions for one to reuse */
7301 /* size + location match */
7302 TAILQ_FOREACH(r, &s->orl, entry)
7303 if (X(r) == x && Y(r) == y &&
7304 HEIGHT(r) == h && WIDTH(r) == w)
7308 TAILQ_FOREACH(r, &s->orl, entry)
7309 if (HEIGHT(r) == h && WIDTH(r) == w)
7313 TAILQ_REMOVE(&s->orl, r, entry);
7314 /* try to use old region's workspace */
7315 if (r->ws->r == NULL)
7318 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7319 err(1, "new_region: calloc: failed to allocate memory "
7322 /* if we don't have a workspace already, find one */
7324 for (i = 0; i < workspace_limit; i++)
7325 if (s->ws[i].r == NULL) {
7332 errx(1, "new_region: no free workspaces");
7343 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7349 #ifdef SWM_XRR_HAS_CRTC
7352 #endif /* SWM_XRR_HAS_CRTC */
7353 struct swm_region *r;
7355 xcb_randr_get_screen_resources_current_cookie_t src;
7356 xcb_randr_get_screen_resources_current_reply_t *srr;
7357 xcb_randr_get_crtc_info_cookie_t cic;
7358 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7359 xcb_randr_crtc_t *crtc;
7360 xcb_screen_t *screen = get_screen(i);
7362 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7363 if (i >= num_screens)
7364 errx(1, "scan_xrandr: invalid screen");
7366 /* remove any old regions */
7367 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7368 r->ws->old_r = r->ws->r = NULL;
7370 TAILQ_REMOVE(&screens[i].rl, r, entry);
7371 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7375 /* map virtual screens onto physical screens */
7376 #ifdef SWM_XRR_HAS_CRTC
7377 if (xrandr_support) {
7378 src = xcb_randr_get_screen_resources_current(conn,
7380 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7383 new_region(&screens[i], 0, 0,
7384 screen->width_in_pixels,
7385 screen->height_in_pixels);
7388 ncrtc = srr->num_crtcs;
7389 for (c = 0; c < ncrtc; c++) {
7390 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7391 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7393 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7396 if (cir->num_outputs == 0) {
7402 new_region(&screens[i], 0, 0,
7403 screen->width_in_pixels,
7404 screen->height_in_pixels);
7406 new_region(&screens[i],
7407 cir->x, cir->y, cir->width, cir->height);
7412 #endif /* SWM_XRR_HAS_CRTC */
7414 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7415 screen->height_in_pixels);
7420 screenchange(xcb_randr_screen_change_notify_event_t *e)
7422 struct swm_region *r;
7425 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7427 if (e->rotation & (XCB_RANDR_ROTATION_ROTATE_90
7428 | XCB_RANDR_ROTATION_ROTATE_270))
7429 xcb_randr_set_screen_size(conn, e->root, e->height,
7430 e->width, e->mheight, e->mwidth);
7432 xcb_randr_set_screen_size(conn, e->root, e->width,
7433 e->height, e->mwidth, e->mheight);
7435 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7436 /* silly event doesn't include the screen index */
7437 for (i = 0; i < num_screens; i++)
7438 if (screens[i].root == e->root)
7440 if (i >= num_screens)
7441 errx(1, "screenchange: screen not found");
7443 /* brute force for now, just re-enumerate the regions */
7446 /* add bars to all regions */
7447 for (i = 0; i < num_screens; i++)
7448 TAILQ_FOREACH(r, &screens[i].rl, entry)
7456 xcb_window_t *wins = NULL;
7458 int i, j, num_screens;
7459 uint16_t state, manage;
7461 xcb_query_tree_cookie_t qtc;
7462 xcb_query_tree_reply_t *qtr;
7463 xcb_get_window_attributes_cookie_t c;
7464 xcb_get_window_attributes_reply_t *r;
7465 xcb_get_property_cookie_t pc;
7467 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7468 for (i = 0; i < num_screens; i++) {
7469 qtc = xcb_query_tree(conn, screens[i].root);
7470 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7473 wins = xcb_query_tree_children(qtr);
7474 no = xcb_query_tree_children_length(qtr);
7475 /* attach windows to a region */
7476 /* normal windows */
7477 for (j = 0; j < no; j++) {
7478 c = xcb_get_window_attributes(conn, wins[j]);
7479 r = xcb_get_window_attributes_reply(conn, c, NULL);
7482 if (r->override_redirect) {
7487 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7488 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7494 state = getstate(wins[j]);
7495 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7496 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7497 manage_window(wins[j]);
7500 /* transient windows */
7501 for (j = 0; j < no; j++) {
7502 c = xcb_get_window_attributes(conn, wins[j]);
7503 r = xcb_get_window_attributes_reply(conn, c, NULL);
7506 if (r->override_redirect) {
7512 state = getstate(wins[j]);
7513 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7514 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7515 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7517 manage_window(wins[j]);
7526 int i, j, k, num_screens;
7527 struct workspace *ws;
7528 uint32_t gcv[1], wa[1];
7529 const xcb_query_extension_reply_t *qep;
7530 xcb_cursor_t cursor;
7531 xcb_font_t cursor_font;
7532 xcb_randr_query_version_cookie_t c;
7533 xcb_randr_query_version_reply_t *r;
7535 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7536 if ((screens = calloc(num_screens,
7537 sizeof(struct swm_screen))) == NULL)
7538 err(1, "setup_screens: calloc: failed to allocate memory for "
7541 /* initial Xrandr setup */
7542 xrandr_support = False;
7543 c = xcb_randr_query_version(conn, True, True);
7544 r = xcb_randr_query_version_reply(conn, c, NULL);
7546 if (r->major_version >= 1)
7547 xrandr_support = True;
7550 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7551 xrandr_eventbase = qep->first_event;
7553 cursor_font = xcb_generate_id(conn);
7554 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7556 cursor = xcb_generate_id(conn);
7557 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7558 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7561 /* map physical screens */
7562 for (i = 0; i < num_screens; i++) {
7563 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7565 TAILQ_INIT(&screens[i].rl);
7566 TAILQ_INIT(&screens[i].orl);
7567 screens[i].root = RootWindow(display, i);
7569 /* set default colors */
7570 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7571 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7572 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7573 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7574 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7576 /* create graphics context on screen */
7577 screens[i].bar_gc = xcb_generate_id(conn);
7579 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7580 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7582 /* set default cursor */
7583 xcb_change_window_attributes(conn, screens[i].root,
7586 /* init all workspaces */
7587 /* XXX these should be dynamically allocated too */
7588 for (j = 0; j < SWM_WS_MAX; j++) {
7589 ws = &screens[i].ws[j];
7595 TAILQ_INIT(&ws->winlist);
7596 TAILQ_INIT(&ws->unmanagedlist);
7598 for (k = 0; layouts[k].l_stack != NULL; k++)
7599 if (layouts[k].l_config != NULL)
7600 layouts[k].l_config(ws,
7601 SWM_ARG_ID_STACKINIT);
7602 ws->cur_layout = &layouts[0];
7603 ws->cur_layout->l_string(ws);
7609 xcb_randr_select_input(conn, screens[i].root,
7610 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7612 xcb_free_cursor(conn, cursor);
7613 xcb_close_font(conn, cursor_font);
7619 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7620 err(1, "setup_globals: strdup: failed to allocate memory.");
7622 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7623 err(1, "setup_globals: strdup: failed to allocate memory.");
7630 xcb_atom_t netwmcheck, netwmname, utf8_string;
7631 xcb_window_t root, win;
7634 /* work around sun jdk bugs, code from wmname */
7635 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7636 netwmname = get_atom_from_string("_NET_WM_NAME");
7637 utf8_string = get_atom_from_string("UTF8_STRING");
7639 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7640 for (i = 0; i < num_screens; i++) {
7641 root = screens[i].root;
7643 win = xcb_generate_id(conn);
7644 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7645 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7646 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7647 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7648 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7650 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7651 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7652 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7653 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7654 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7655 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7660 event_error(xcb_generic_error_t *e)
7664 switch (e->error_code) {
7665 case XCB_EVENT_ERROR_SUCESS:
7668 case XCB_EVENT_ERROR_BAD_REQUEST:
7669 estr = "BadRequest";
7671 case XCB_EVENT_ERROR_BAD_VALUE:
7674 case XCB_EVENT_ERROR_BAD_WINDOW:
7677 case XCB_EVENT_ERROR_BAD_PIXMAP:
7680 case XCB_EVENT_ERROR_BAD_ATOM:
7683 case XCB_EVENT_ERROR_BAD_CURSOR:
7686 case XCB_EVENT_ERROR_BAD_FONT:
7689 case XCB_EVENT_ERROR_BAD_MATCH:
7692 case XCB_EVENT_ERROR_BAD_DRAWABLE:
7693 estr = "BadDrawable";
7695 case XCB_EVENT_ERROR_BAD_ACCESS:
7698 case XCB_EVENT_ERROR_BAD_ALLOC:
7701 case XCB_EVENT_ERROR_BAD_COLOR:
7704 case XCB_EVENT_ERROR_BAD_GC:
7707 case XCB_EVENT_ERROR_BAD_ID_CHOICE:
7708 estr = "BadIdChoice";
7710 case XCB_EVENT_ERROR_BAD_NAME:
7713 case XCB_EVENT_ERROR_BAD_LENGTH:
7716 case XCB_EVENT_ERROR_BAD_IMPLEMENTATION:
7717 estr = "BadImplementation";
7724 DNPRINTF(SWM_D_EVENT,
7725 "event_error: %s: response_type:%u "
7726 "error_code:%u sequence:%u resource_id:%u "
7727 "minor_code:%u major_code:%u\n", estr,
7728 e->response_type, e->error_code, e->sequence,
7729 e->resource_id, e->minor_code, e->major_code
7734 event_handle(xcb_generic_event_t *evt)
7736 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7738 #define EVENT(type, callback) case type: callback((void *)evt); return
7739 EVENT(0, event_error);
7740 EVENT(XCB_BUTTON_PRESS, buttonpress);
7741 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7742 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7743 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7744 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7745 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7746 EVENT(XCB_ENTER_NOTIFY, enternotify);
7747 EVENT(XCB_EXPOSE, expose);
7748 EVENT(XCB_KEY_PRESS, keypress);
7749 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7750 EVENT(XCB_MAP_NOTIFY, mapnotify);
7751 EVENT(XCB_MAP_REQUEST, maprequest);
7752 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7753 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7754 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7755 EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);
7758 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7759 screenchange((void *)evt);
7763 main(int argc, char *argv[])
7765 struct swm_region *r, *rr;
7766 struct ws_win *winfocus = NULL;
7769 char conf[PATH_MAX], *cfile = NULL;
7771 int xfd, i, num_screens;
7773 struct sigaction sact;
7774 xcb_generic_event_t *evt;
7777 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7778 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7780 warnx("no locale support");
7782 if (!X_HAVE_UTF8_STRING)
7783 warnx("no UTF-8 support");
7785 if (!(display = XOpenDisplay(0)))
7786 errx(1, "can not open display");
7788 /* handle some signals */
7789 bzero(&sact, sizeof(sact));
7790 sigemptyset(&sact.sa_mask);
7792 sact.sa_handler = sighdlr;
7793 sigaction(SIGINT, &sact, NULL);
7794 sigaction(SIGQUIT, &sact, NULL);
7795 sigaction(SIGTERM, &sact, NULL);
7796 sigaction(SIGHUP, &sact, NULL);
7798 sact.sa_handler = sighdlr;
7799 sact.sa_flags = SA_NOCLDSTOP;
7800 sigaction(SIGCHLD, &sact, NULL);
7802 conn = XGetXCBConnection(display);
7803 if (xcb_connection_has_error(conn))
7804 errx(1, "can not get XCB connection");
7806 xfd = xcb_get_file_descriptor(conn);
7807 syms = xcb_key_symbols_alloc(conn);
7809 errx(1, "unable to allocate key symbols");
7811 astate = get_atom_from_string("WM_STATE");
7812 aprot = get_atom_from_string("WM_PROTOCOLS");
7813 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7814 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7815 a_wmname = get_atom_from_string("WM_NAME");
7816 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7817 a_utf8_string = get_atom_from_string("UTF8_STRING");
7818 a_string = get_atom_from_string("STRING");
7819 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7821 /* look for local and global conf file */
7822 pwd = getpwuid(getuid());
7824 errx(1, "invalid user: %d", getuid());
7826 xcb_grab_server(conn);
7829 /* flush all events */
7830 while ((evt = xcb_poll_for_event(conn))) {
7831 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7838 errx(1, "other wm running");
7849 for (i = 0; ; i++) {
7854 snprintf(conf, sizeof conf, "%s/.%s",
7855 pwd->pw_dir, SWM_CONF_FILE);
7859 snprintf(conf, sizeof conf, "/etc/%s",
7864 snprintf(conf, sizeof conf, "%s/.%s",
7865 pwd->pw_dir, SWM_CONF_FILE_OLD);
7869 snprintf(conf, sizeof conf, "/etc/%s",
7876 if (strlen(conf) && stat(conf, &sb) != -1)
7877 if (S_ISREG(sb.st_mode)) {
7884 /* load conf (if any) */
7886 conf_load(cfile, SWM_CONF_DEFAULT);
7889 /* set some values to work around bad programs */
7891 /* grab existing windows (before we build the bars) */
7894 if (getenv("SWM_STARTED") == NULL)
7895 setenv("SWM_STARTED", "YES", 1);
7897 /* setup all bars */
7898 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7899 for (i = 0; i < num_screens; i++)
7900 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7901 if (winfocus == NULL)
7902 winfocus = TAILQ_FIRST(&r->ws->winlist);
7911 xcb_ungrab_server(conn);
7915 while ((evt = xcb_poll_for_event(conn))) {
7916 DNPRINTF(SWM_D_EVENT, "XCB Event: %s\n",
7924 /* if we are being restarted go focus on first window */
7926 rr = winfocus->ws->r;
7928 /* not a visible window */
7932 /* move pointer to first screen if multi screen */
7933 if (num_screens > 1 || outputs > 1)
7934 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7935 rr->s[0].root, 0, 0, 0, 0, X(rr),
7936 Y(rr) + (bar_enabled ? bar_height : 0));
7938 a.id = SWM_ARG_ID_FOCUSCUR;
7948 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7950 DNPRINTF(SWM_D_MISC, "select failed");
7951 if (restart_wm == 1)
7952 restart(NULL, NULL);
7953 if (search_resp == 1)
7966 for (i = 0; i < num_screens; ++i)
7967 if (screens[i].bar_gc != 0)
7968 xcb_free_gc(conn, screens[i].bar_gc);
7969 XFreeFontSet(display, bar_fs);
7971 xcb_key_symbols_free(syms);
7972 xcb_disconnect(conn);