2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <X11/Xproto.h>
102 #include <X11/Xutil.h>
103 #include <X11/extensions/Xrandr.h>
104 #include <X11/extensions/XTest.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr = SPECTRWM_BUILDSTR;
115 static const char *buildstr = SPECTRWM_VERSION;
119 # error XRandR versions less than 1.0 are not supported
124 #define SWM_XRR_HAS_CRTC
128 #if defined(__OpenBSD__)
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
133 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
134 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
135 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
136 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
137 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
138 #define xcb_icccm_get_wm_class xcb_get_wm_class
139 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
140 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
141 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
142 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
143 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
144 #define xcb_icccm_get_wm_name xcb_get_wm_name
145 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
146 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
147 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
148 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
149 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
150 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
151 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
152 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
153 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
154 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
159 #define DPRINTF(x...) do { \
161 fprintf(stderr, x); \
163 #define DNPRINTF(n,x...) do { \
164 if (swm_debug & n) { \
165 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
166 fprintf(stderr, x); \
169 #define SWM_D_MISC 0x0001
170 #define SWM_D_EVENT 0x0002
171 #define SWM_D_WS 0x0004
172 #define SWM_D_FOCUS 0x0008
173 #define SWM_D_MOVE 0x0010
174 #define SWM_D_STACK 0x0020
175 #define SWM_D_MOUSE 0x0040
176 #define SWM_D_PROP 0x0080
177 #define SWM_D_CLASS 0x0100
178 #define SWM_D_KEY 0x0200
179 #define SWM_D_QUIRK 0x0400
180 #define SWM_D_SPAWN 0x0800
181 #define SWM_D_EVENTQ 0x1000
182 #define SWM_D_CONF 0x2000
183 #define SWM_D_BAR 0x4000
185 u_int32_t swm_debug = 0
203 #define DPRINTF(x...)
204 #define DNPRINTF(n,x...)
207 #define LENGTH(x) (sizeof x / sizeof x[0])
208 #define MODKEY Mod1Mask
209 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
210 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
211 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
212 #define SWM_PROPLEN (16)
213 #define SWM_FUNCNAME_LEN (32)
214 #define SWM_KEYS_LEN (255)
215 #define SWM_QUIRK_LEN (64)
216 #define X(r) (r)->g.x
217 #define Y(r) (r)->g.y
218 #define WIDTH(r) (r)->g.w
219 #define HEIGHT(r) (r)->g.h
220 #define BORDER(w) (w->bordered ? border_width : 0)
221 #define MAX_X(r) ((r)->g.x + (r)->g.w)
222 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
223 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
224 #define SH_MIN_W(w) (w)->sh.min_width
225 #define SH_MIN_H(w) (w)->sh.min_height
226 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
227 #define SH_MAX_W(w) (w)->sh.max_width
228 #define SH_MAX_H(w) (w)->sh.max_height
229 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
230 #define SH_INC_W(w) (w)->sh.width_inc
231 #define SH_INC_H(w) (w)->sh.height_inc
232 #define SWM_MAX_FONT_STEPS (3)
233 #define WINID(w) ((w) ? (w)->id : 0)
234 #define YESNO(x) ((x) ? "yes" : "no")
236 #define SWM_FOCUS_DEFAULT (0)
237 #define SWM_FOCUS_SYNERGY (1)
238 #define SWM_FOCUS_FOLLOW (2)
240 #define SWM_CONF_DEFAULT (0)
241 #define SWM_CONF_KEYMAPPING (1)
244 #define SWM_LIB "/usr/local/lib/libswmhack.so"
251 xcb_atom_t takefocus;
253 xcb_atom_t a_netwmname;
254 xcb_atom_t a_utf8_string;
256 xcb_atom_t a_swm_iconic;
257 volatile sig_atomic_t running = 1;
258 volatile sig_atomic_t restart_wm = 0;
260 /*int last_focus_event = FocusOut;*/
261 int (*xerrorxlib)(Display *, XErrorEvent *);
265 int xrandr_eventbase;
266 unsigned int numlockmask = 0;
268 xcb_connection_t *conn;
269 xcb_key_symbols_t *syms;
272 int cycle_visible = 0;
274 int font_adjusted = 0;
275 unsigned int mod_key = MODKEY;
278 struct swm_region *search_r;
279 int select_list_pipe[2];
280 int select_resp_pipe[2];
282 volatile sig_atomic_t search_resp;
283 int search_resp_action;
285 struct search_window {
286 TAILQ_ENTRY(search_window) entry;
290 xcb_window_t indicator;
292 TAILQ_HEAD(search_winlist, search_window);
293 struct search_winlist search_wl;
298 SWM_SEARCH_UNICONIFY,
299 SWM_SEARCH_NAME_WORKSPACE,
300 SWM_SEARCH_SEARCH_WORKSPACE,
301 SWM_SEARCH_SEARCH_WINDOW
304 #define SWM_STACK_TOP (0)
305 #define SWM_STACK_BOTTOM (1)
306 #define SWM_STACK_ABOVE (2)
307 #define SWM_STACK_BELOW (3)
310 double dialog_ratio = 0.6;
312 #define SWM_BAR_MAX (256)
313 #define SWM_BAR_JUSTIFY_LEFT (0)
314 #define SWM_BAR_JUSTIFY_CENTER (1)
315 #define SWM_BAR_JUSTIFY_RIGHT (2)
316 #define SWM_BAR_OFFSET (4)
317 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
318 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
319 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
320 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
321 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
323 char *bar_argv[] = { NULL, NULL };
325 unsigned char bar_ext[SWM_BAR_MAX];
326 char bar_vertext[SWM_BAR_MAX];
328 sig_atomic_t bar_alarm = 0;
331 int bar_border_width = 1;
332 int bar_at_bottom = 0;
334 int bar_extra_running = 0;
337 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
338 char *bar_format = NULL;
339 int stack_enabled = 1;
340 int clock_enabled = 1;
341 int urgent_enabled = 0;
342 char *clock_format = NULL;
343 int title_name_enabled = 0;
344 int title_class_enabled = 0;
345 int window_name_enabled = 0;
346 int focus_mode = SWM_FOCUS_DEFAULT;
347 int focus_close = SWM_STACK_BELOW;
348 int focus_close_wrap = 1;
349 int focus_default = SWM_STACK_TOP;
350 int spawn_position = SWM_STACK_TOP;
351 int disable_border = 0;
352 int border_width = 1;
353 int verbose_layout = 0;
357 XFontSetExtents *bar_fs_extents;
361 #define SWM_MENU_FN (2)
362 #define SWM_MENU_NB (4)
363 #define SWM_MENU_NF (6)
364 #define SWM_MENU_SB (8)
365 #define SWM_MENU_SF (10)
367 /* layout manager data */
368 struct swm_geometry {
381 struct swm_geometry g;
384 /* virtual "screens" */
386 TAILQ_ENTRY(swm_region) entry;
387 struct swm_geometry g;
388 struct workspace *ws; /* current workspace on this region */
389 struct workspace *ws_prior; /* prior workspace on this region */
390 struct swm_screen *s; /* screen idx */
393 TAILQ_HEAD(swm_region_list, swm_region);
396 TAILQ_ENTRY(ws_win) entry;
398 xcb_window_t transient;
399 struct ws_win *child_trans; /* transient child window */
400 struct swm_geometry g; /* current geometry */
401 struct swm_geometry g_float; /* region coordinates */
402 int g_floatvalid; /* g_float geometry validity */
403 int floatmaxed; /* whether maxed by max_stack */
408 unsigned int ewmh_flags;
409 int font_size_boundary[SWM_MAX_FONT_STEPS];
415 unsigned long quirks;
416 struct workspace *ws; /* always valid */
417 struct swm_screen *s; /* always valid, never changes */
418 xcb_get_geometry_reply_t *wa;
420 xcb_icccm_get_wm_class_reply_t ch;
421 xcb_icccm_wm_hints_t hints;
423 TAILQ_HEAD(ws_win_list, ws_win);
427 TAILQ_ENTRY(pid_e) entry;
431 TAILQ_HEAD(pid_list, pid_e);
432 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
434 /* layout handlers */
436 void vertical_config(struct workspace *, int);
437 void vertical_stack(struct workspace *, struct swm_geometry *);
438 void horizontal_config(struct workspace *, int);
439 void horizontal_stack(struct workspace *, struct swm_geometry *);
440 void max_stack(struct workspace *, struct swm_geometry *);
441 void plain_stacker(struct workspace *);
442 void fancy_stacker(struct workspace *);
444 struct ws_win *find_window(xcb_window_t);
446 void grabbuttons(struct ws_win *, int);
447 void new_region(struct swm_screen *, int, int, int, int);
448 void unmanage_window(struct ws_win *);
449 uint16_t getstate(xcb_window_t);
451 int conf_load(char *, int);
454 void (*l_stack)(struct workspace *, struct swm_geometry *);
455 void (*l_config)(struct workspace *, int);
457 #define SWM_L_FOCUSPREV (1<<0)
458 #define SWM_L_MAPONFOCUS (1<<1)
459 void (*l_string)(struct workspace *);
461 /* stack, configure */
462 { vertical_stack, vertical_config, 0, plain_stacker },
463 { horizontal_stack, horizontal_config, 0, plain_stacker },
465 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
466 { NULL, NULL, 0, NULL },
469 /* position of max_stack mode in the layouts array, index into layouts! */
470 #define SWM_V_STACK (0)
471 #define SWM_H_STACK (1)
472 #define SWM_MAX_STACK (2)
474 #define SWM_H_SLICE (32)
475 #define SWM_V_SLICE (32)
477 /* define work spaces */
479 int idx; /* workspace index */
480 char *name; /* workspace name */
481 int always_raise; /* raise windows on focus */
482 struct layout *cur_layout; /* current layout handlers */
483 struct ws_win *focus; /* may be NULL */
484 struct ws_win *focus_prev; /* may be NULL */
485 struct swm_region *r; /* may be NULL */
486 struct swm_region *old_r; /* may be NULL */
487 struct ws_win_list winlist; /* list of windows in ws */
488 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
489 char stacker[10]; /* display stacker and layout */
493 int horizontal_msize;
495 int horizontal_stacks;
506 SWM_S_COLOR_BAR_BORDER,
507 SWM_S_COLOR_BAR_FONT,
513 /* physical screen mapping */
514 #define SWM_WS_MAX (22) /* hard limit */
515 int workspace_limit = 10; /* soft limit */
518 int idx; /* screen index */
519 struct swm_region_list rl; /* list of regions on this screen */
520 struct swm_region_list orl; /* list of old regions */
522 struct workspace ws[SWM_WS_MAX];
528 } c[SWM_S_COLOR_MAX];
530 xcb_gcontext_t bar_gc;
532 struct swm_screen *screens;
534 /* args to functions */
537 #define SWM_ARG_ID_FOCUSNEXT (0)
538 #define SWM_ARG_ID_FOCUSPREV (1)
539 #define SWM_ARG_ID_FOCUSMAIN (2)
540 #define SWM_ARG_ID_FOCUSCUR (4)
541 #define SWM_ARG_ID_SWAPNEXT (10)
542 #define SWM_ARG_ID_SWAPPREV (11)
543 #define SWM_ARG_ID_SWAPMAIN (12)
544 #define SWM_ARG_ID_MOVELAST (13)
545 #define SWM_ARG_ID_MASTERSHRINK (20)
546 #define SWM_ARG_ID_MASTERGROW (21)
547 #define SWM_ARG_ID_MASTERADD (22)
548 #define SWM_ARG_ID_MASTERDEL (23)
549 #define SWM_ARG_ID_FLIPLAYOUT (24)
550 #define SWM_ARG_ID_STACKRESET (30)
551 #define SWM_ARG_ID_STACKINIT (31)
552 #define SWM_ARG_ID_CYCLEWS_UP (40)
553 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
554 #define SWM_ARG_ID_CYCLESC_UP (42)
555 #define SWM_ARG_ID_CYCLESC_DOWN (43)
556 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
557 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
558 #define SWM_ARG_ID_STACKINC (50)
559 #define SWM_ARG_ID_STACKDEC (51)
560 #define SWM_ARG_ID_SS_ALL (60)
561 #define SWM_ARG_ID_SS_WINDOW (61)
562 #define SWM_ARG_ID_DONTCENTER (70)
563 #define SWM_ARG_ID_CENTER (71)
564 #define SWM_ARG_ID_KILLWINDOW (80)
565 #define SWM_ARG_ID_DELETEWINDOW (81)
566 #define SWM_ARG_ID_WIDTHGROW (90)
567 #define SWM_ARG_ID_WIDTHSHRINK (91)
568 #define SWM_ARG_ID_HEIGHTGROW (92)
569 #define SWM_ARG_ID_HEIGHTSHRINK (93)
570 #define SWM_ARG_ID_MOVEUP (100)
571 #define SWM_ARG_ID_MOVEDOWN (101)
572 #define SWM_ARG_ID_MOVELEFT (102)
573 #define SWM_ARG_ID_MOVERIGHT (103)
577 void focus(struct swm_region *, union arg *);
578 void focus_magic(struct ws_win *);
582 TAILQ_ENTRY(quirk) entry;
586 #define SWM_Q_FLOAT (1<<0) /* float this window */
587 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
588 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
589 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
590 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
591 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
593 TAILQ_HEAD(quirk_list, quirk);
594 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
597 * Supported EWMH hints should be added to
598 * both the enum and the ewmh array
603 _NET_MOVERESIZE_WINDOW,
604 _NET_WM_ACTION_CLOSE,
605 _NET_WM_ACTION_FULLSCREEN,
607 _NET_WM_ACTION_RESIZE,
608 _NET_WM_ALLOWED_ACTIONS,
611 _NET_WM_STATE_FULLSCREEN,
612 _NET_WM_STATE_HIDDEN,
613 _NET_WM_STATE_MAXIMIZED_HORZ,
614 _NET_WM_STATE_MAXIMIZED_VERT,
615 _NET_WM_STATE_SKIP_PAGER,
616 _NET_WM_STATE_SKIP_TASKBAR,
618 _NET_WM_WINDOW_TYPE_DIALOG,
619 _NET_WM_WINDOW_TYPE_DOCK,
620 _NET_WM_WINDOW_TYPE_NORMAL,
621 _NET_WM_WINDOW_TYPE_SPLASH,
622 _NET_WM_WINDOW_TYPE_TOOLBAR,
623 _NET_WM_WINDOW_TYPE_UTILITY,
624 _SWM_WM_STATE_MANUAL,
631 } ewmh[SWM_EWMH_HINT_MAX] = {
632 /* must be in same order as in the enum */
633 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
634 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
635 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
636 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
637 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
638 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
639 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
640 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
641 {"_NET_WM_STATE", XCB_ATOM_NONE},
642 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
643 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
644 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
645 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
646 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
647 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
648 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
649 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
650 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
651 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
652 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
653 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
654 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
655 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
656 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
659 void store_float_geom(struct ws_win *, struct swm_region *);
660 int floating_toggle_win(struct ws_win *);
661 void constrain_window(struct ws_win *, struct swm_region *, int);
662 void update_window(struct ws_win *);
663 void spawn_select(struct swm_region *, union arg *, char *, int *);
664 char *get_win_name(xcb_window_t);
665 xcb_atom_t get_atom_from_string(const char *);
666 void map_window_raised(xcb_window_t);
668 xcb_screen_t *get_screen(int);
669 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
670 void event_handle(xcb_generic_event_t *);
673 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
675 unsigned int tmpr, tmpg, tmpb;
677 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
688 get_screen(int screen)
690 xcb_screen_iterator_t i;
692 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
693 for (; i.rem; --screen, xcb_screen_next(&i))
703 xcb_get_input_focus_cookie_t c;
704 xcb_get_input_focus_reply_t *r;
707 c = xcb_get_input_focus(conn);
709 r = xcb_get_input_focus_reply(conn, c, NULL);
715 map_window_raised(xcb_window_t win)
717 uint32_t val = XCB_STACK_MODE_ABOVE;
719 xcb_configure_window(conn, win,
720 XCB_CONFIG_WINDOW_STACK_MODE, &val);
722 xcb_map_window(conn, win);
727 get_atom_from_string(const char *str)
729 xcb_intern_atom_cookie_t c;
730 xcb_intern_atom_reply_t *r;
733 c = xcb_intern_atom(conn, False, strlen(str), str);
734 r = xcb_intern_atom_reply(conn, c, NULL);
742 return (XCB_ATOM_NONE);
746 update_iconic(struct ws_win *win, int newv)
753 iprop = get_atom_from_string("_SWM_ICONIC");
754 if (iprop == XCB_ATOM_NONE)
758 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
759 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
761 xcb_delete_property(conn, win->id, iprop);
765 get_iconic(struct ws_win *win)
769 xcb_get_property_reply_t *pr = NULL;
771 iprop = get_atom_from_string("_SWM_ICONIC");
772 if (iprop == XCB_ATOM_NONE)
775 pr = xcb_get_property_reply(conn,
776 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
780 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
782 v = *((int32_t *)xcb_get_property_value(pr));
793 int i, j, num_screens;
795 sup_list = get_atom_from_string("_NET_SUPPORTED");
797 for (i = 0; i < LENGTH(ewmh); i++)
798 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
800 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
801 for (i = 0; i < num_screens; i++) {
802 /* Support check window will be created by workaround(). */
804 /* Report supported atoms */
805 xcb_delete_property(conn, screens[i].root, sup_list);
806 for (j = 0; j < LENGTH(ewmh); j++)
807 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
808 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
817 xcb_atom_t sup_check, sup_list;
819 xcb_get_property_cookie_t pc;
820 xcb_get_property_reply_t *pr;
822 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
823 sup_list = get_atom_from_string("_NET_SUPPORTED");
824 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
826 for (i = 0; i < num_screens; i++) {
827 /* Get the support check window and destroy it */
828 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
829 XCB_ATOM_WINDOW, 0, 1);
830 pr = xcb_get_property_reply(conn, pc, NULL);
832 id = *((xcb_window_t *)xcb_get_property_value(pr));
834 xcb_destroy_window(conn, id);
835 xcb_delete_property(conn, screens[i].root, sup_check);
836 xcb_delete_property(conn, screens[i].root, sup_list);
844 ewmh_autoquirk(struct ws_win *win)
850 xcb_get_property_cookie_t c;
851 xcb_get_property_reply_t *r;
853 c = xcb_get_property(conn, False, win->id,
854 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
855 r = xcb_get_property_reply(conn, c, NULL);
858 n = xcb_get_property_value_length(r);
860 for (i = 0; i < n; i++) {
861 type = *((xcb_atom_t *)xcb_get_property_value(r));
862 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
864 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
865 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
866 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
868 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
871 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
872 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
874 win->quirks = SWM_Q_FLOAT;
881 #define SWM_EWMH_ACTION_COUNT_MAX (6)
882 #define EWMH_F_FULLSCREEN (1<<0)
883 #define EWMH_F_ABOVE (1<<1)
884 #define EWMH_F_HIDDEN (1<<2)
885 #define EWMH_F_SKIP_PAGER (1<<3)
886 #define EWMH_F_SKIP_TASKBAR (1<<4)
887 #define SWM_F_MANUAL (1<<5)
890 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
898 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
899 "fullscreen %s\n", win->id, YESNO(fs));
902 if (!win->g_floatvalid)
903 store_float_geom(win, win->ws->r);
905 win->g = win->ws->r->g;
908 if (win->g_floatvalid) {
909 /* refloat at last floating relative position */
910 win->g = win->g_float;
911 X(win) += X(win->ws->r);
912 Y(win) += Y(win->ws->r);
920 ewmh_update_actions(struct ws_win *win)
922 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
928 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
931 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
932 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
935 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
936 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
939 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
940 #define _NET_WM_STATE_ADD 1 /* add/set property */
941 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
944 ewmh_update_win_state(struct ws_win *win, long state, long action)
946 unsigned int mask = 0;
947 unsigned int changed = 0;
948 unsigned int orig_flags;
953 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
954 mask = EWMH_F_FULLSCREEN;
955 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
957 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
959 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
960 mask = EWMH_F_SKIP_PAGER;
961 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
962 mask = EWMH_F_SKIP_TASKBAR;
965 orig_flags = win->ewmh_flags;
968 case _NET_WM_STATE_REMOVE:
969 win->ewmh_flags &= ~mask;
971 case _NET_WM_STATE_ADD:
972 win->ewmh_flags |= mask;
974 case _NET_WM_STATE_TOGGLE:
975 win->ewmh_flags ^= mask;
979 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
981 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
983 if (!floating_toggle_win(win))
984 win->ewmh_flags = orig_flags; /* revert */
985 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
987 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
988 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
990 if (!ewmh_set_win_fullscreen(win,
991 win->ewmh_flags & EWMH_F_FULLSCREEN))
992 win->ewmh_flags = orig_flags; /* revert */
994 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
996 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
997 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
998 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
999 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1000 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1001 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1002 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1003 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1004 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1005 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1006 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1007 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1008 if (win->ewmh_flags & EWMH_F_ABOVE)
1009 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1010 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1011 &ewmh[_NET_WM_STATE_ABOVE].atom);
1012 if (win->ewmh_flags & SWM_F_MANUAL)
1013 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1014 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1015 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1019 ewmh_get_win_state(struct ws_win *win)
1022 xcb_get_property_cookie_t c;
1023 xcb_get_property_reply_t *r;
1029 win->ewmh_flags = 0;
1031 win->ewmh_flags |= EWMH_F_ABOVE;
1033 win->ewmh_flags |= SWM_F_MANUAL;
1035 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1036 XCB_ATOM_ATOM, 0, (~0L));
1037 r = xcb_get_property_reply(conn, c, NULL);
1041 states = xcb_get_property_value(r);
1042 n = xcb_get_property_value_length(r);
1044 for (i = 0; i < n; i++)
1045 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1053 dumpwins(struct swm_region *r, union arg *args)
1057 xcb_get_window_attributes_cookie_t c;
1058 xcb_get_window_attributes_reply_t *wa;
1060 if (r->ws == NULL) {
1061 warnx("dumpwins: invalid workspace");
1065 warnx("=== managed window list ws %02d ===", r->ws->idx);
1066 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1067 state = getstate(win->id);
1068 c = xcb_get_window_attributes(conn, win->id);
1069 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1071 warnx("window: 0x%x, map_state: %d, state: %u, "
1072 "transient: 0x%x", win->id, wa->map_state,
1073 state, win->transient);
1076 warnx("window: 0x%x, failed xcb_get_window_attributes",
1080 warnx("===== unmanaged window list =====");
1081 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1082 state = getstate(win->id);
1083 c = xcb_get_window_attributes(conn, win->id);
1084 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1086 warnx("window: 0x%x, map_state: %d, state: %u, "
1087 "transient: 0x%x", win->id, wa->map_state,
1088 state, win->transient);
1091 warnx("window: 0x%x, failed xcb_get_window_attributes",
1095 warnx("=================================");
1099 dumpwins(struct swm_region *r, union arg *args)
1102 #endif /* SWM_DEBUG */
1104 void event_error(xcb_generic_error_t *);
1105 void expose(xcb_expose_event_t *);
1106 void keypress(xcb_key_press_event_t *);
1107 void buttonpress(xcb_button_press_event_t *);
1108 void configurerequest(xcb_configure_request_event_t *);
1109 void configurenotify(xcb_configure_notify_event_t *);
1110 void destroynotify(xcb_destroy_notify_event_t *);
1111 void enternotify(xcb_enter_notify_event_t *);
1112 void mapnotify(xcb_map_notify_event_t *);
1113 void mappingnotify(xcb_mapping_notify_event_t *);
1114 void maprequest(xcb_map_request_event_t *);
1115 void propertynotify(xcb_property_notify_event_t *);
1116 void unmapnotify(xcb_unmap_notify_event_t *);
1117 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1118 void clientmessage(xcb_client_message_event_t *);
1119 void screenchange(xcb_randr_screen_change_notify_event_t *);
1124 int saved_errno, status;
1127 saved_errno = errno;
1131 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1136 if (errno != ECHILD)
1137 warn("sighdlr: waitpid");
1138 #endif /* SWM_DEBUG */
1141 if (pid == searchpid)
1145 if (WIFEXITED(status)) {
1146 if (WEXITSTATUS(status) != 0)
1147 warnx("sighdlr: child exit status: %d",
1148 WEXITSTATUS(status));
1150 warnx("sighdlr: child is terminated "
1152 #endif /* SWM_DEBUG */
1166 errno = saved_errno;
1172 struct pid_e *p = NULL;
1174 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1179 TAILQ_FOREACH(p, &pidlist, entry) {
1188 name_to_color(const char *colorname)
1190 uint32_t result = 0;
1191 char cname[32] = "#";
1192 xcb_screen_t *screen;
1193 xcb_colormap_t cmap;
1194 xcb_alloc_color_reply_t *cr;
1195 xcb_alloc_named_color_reply_t *nr;
1196 uint16_t rr, gg, bb;
1198 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1199 cmap = screen->default_colormap;
1201 /* color is in format rgb://rr/gg/bb */
1202 if (strncmp(colorname, "rgb:", 4) == 0) {
1203 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1204 warnx("could not parse rgb %s", colorname);
1206 cr = xcb_alloc_color_reply(conn,
1207 xcb_alloc_color(conn, cmap, rr, gg, bb),
1213 warnx("color '%s' not found", colorname);
1216 nr = xcb_alloc_named_color_reply(conn,
1217 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1220 strlcat(cname, colorname + 2, sizeof cname - 1);
1221 nr = xcb_alloc_named_color_reply(conn,
1222 xcb_alloc_named_color(conn, cmap, strlen(cname),
1229 warnx("color '%s' not found", colorname);
1236 setscreencolor(char *val, int i, int c)
1240 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1241 if (i > 0 && i <= num_screens) {
1242 screens[i - 1].c[c].color = name_to_color(val);
1243 free(screens[i - 1].c[c].name);
1244 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1246 } else if (i == -1) {
1247 for (i = 0; i < num_screens; i++) {
1248 screens[i].c[c].color = name_to_color(val);
1249 free(screens[i].c[c].name);
1250 if ((screens[i].c[c].name = strdup(val)) == NULL)
1254 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1259 fancy_stacker(struct workspace *ws)
1261 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1262 if (ws->cur_layout->l_stack == vertical_stack)
1263 snprintf(ws->stacker, sizeof ws->stacker,
1264 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1265 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1266 if (ws->cur_layout->l_stack == horizontal_stack)
1267 snprintf(ws->stacker, sizeof ws->stacker,
1268 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1269 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1273 plain_stacker(struct workspace *ws)
1275 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1276 if (ws->cur_layout->l_stack == vertical_stack)
1277 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1278 sizeof ws->stacker);
1279 if (ws->cur_layout->l_stack == horizontal_stack)
1280 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1281 sizeof ws->stacker);
1285 custom_region(char *val)
1287 unsigned int sidx, x, y, w, h;
1289 xcb_screen_t *screen;
1291 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1292 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1293 errx(1, "invalid custom region, "
1294 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1295 if (sidx < 1 || sidx > num_screens)
1296 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1300 screen = get_screen(sidx);
1302 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1304 if (x > screen->width_in_pixels ||
1305 y > screen->height_in_pixels ||
1306 w + x > screen->width_in_pixels ||
1307 h + y > screen->height_in_pixels) {
1308 warnx("ignoring region %ux%u+%u+%u - not within screen "
1309 "boundaries (%ux%u)", w, h, x, y,
1310 screen->width_in_pixels, screen->height_in_pixels);
1314 new_region(&screens[sidx], x, y, w, h);
1318 socket_setnonblock(int fd)
1322 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1323 err(1, "fcntl F_GETFL");
1324 flags |= O_NONBLOCK;
1325 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1326 err(1, "fcntl F_SETFL");
1330 bar_print(struct swm_region *r, const char *s)
1334 xcb_rectangle_t rect;
1336 XRectangle ibox, lbox;
1339 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1341 switch (bar_justify) {
1342 case SWM_BAR_JUSTIFY_LEFT:
1345 case SWM_BAR_JUSTIFY_CENTER:
1346 x = (WIDTH(r) - lbox.width) / 2;
1348 case SWM_BAR_JUSTIFY_RIGHT:
1349 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1353 if (x < SWM_BAR_OFFSET)
1358 rect.width = WIDTH(r->bar);
1359 rect.height = HEIGHT(r->bar);
1361 /* clear back buffer */
1362 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1363 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1364 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1365 sizeof(rect), &rect);
1367 /* draw back buffer */
1368 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1369 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1370 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1371 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1372 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1373 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1377 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1378 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1382 bar_extra_stop(void)
1386 bzero(bar_pipe, sizeof bar_pipe);
1389 kill(bar_pid, SIGTERM);
1392 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1397 bar_class_name(char *s, size_t sz, struct swm_region *r)
1399 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1401 if (r->ws->focus->ch.class_name != NULL)
1402 strlcat(s, r->ws->focus->ch.class_name, sz);
1406 bar_title_name(char *s, size_t sz, struct swm_region *r)
1408 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1410 if (r->ws->focus->ch.instance_name != NULL)
1411 strlcat(s, r->ws->focus->ch.instance_name, sz);
1415 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1417 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1420 bar_class_name(s, sz, r);
1421 strlcat(s, ":", sz);
1422 bar_title_name(s, sz, r);
1426 bar_window_float(char *s, size_t sz, struct swm_region *r)
1428 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1430 if (r->ws->focus->floating)
1431 strlcat(s, "(f)", sz);
1435 bar_window_name(char *s, size_t sz, struct swm_region *r)
1439 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1441 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1444 strlcat(s, title, sz);
1448 int urgent[SWM_WS_MAX];
1450 bar_urgent(char *s, size_t sz)
1453 int i, j, num_screens;
1455 xcb_get_property_cookie_t c;
1456 xcb_icccm_wm_hints_t hints;
1458 for (i = 0; i < workspace_limit; i++)
1461 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1462 for (i = 0; i < num_screens; i++)
1463 for (j = 0; j < workspace_limit; j++)
1464 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1465 c = xcb_icccm_get_wm_hints(conn, win->id);
1466 if (xcb_icccm_get_wm_hints_reply(conn, c,
1469 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1473 for (i = 0; i < workspace_limit; i++) {
1475 snprintf(b, sizeof b, "%d ", i + 1);
1477 snprintf(b, sizeof b, "- ");
1483 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1485 if (r == NULL || r->ws == NULL)
1487 if (r->ws->name != NULL)
1488 strlcat(s, r->ws->name, sz);
1491 /* build the default bar format according to the defined enabled options */
1493 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1495 /* if format provided, just copy the buffers */
1496 if (bar_format != NULL) {
1497 strlcpy(fmtnew, fmtexp, sz);
1501 /* reset the output buffer */
1504 strlcat(fmtnew, "+N:+I ", sz);
1506 strlcat(fmtnew, "+S", sz);
1507 strlcat(fmtnew, " ", sz);
1509 /* only show the workspace name if there's actually one */
1510 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1511 strlcat(fmtnew, "<+D>", sz);
1512 strlcat(fmtnew, "+3<", sz);
1514 if (clock_enabled) {
1515 strlcat(fmtnew, fmtexp, sz);
1516 strlcat(fmtnew, "+4<", sz);
1519 /* bar_urgent already adds the space before the last asterisk */
1521 strlcat(fmtnew, "* +U*+4<", sz);
1523 if (title_class_enabled) {
1524 strlcat(fmtnew, "+C", sz);
1525 if (title_name_enabled == 0)
1526 strlcat(fmtnew, "+4<", sz);
1529 /* checks needed by the colon and floating strlcat(3) calls below */
1530 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1531 if (title_name_enabled) {
1532 if (title_class_enabled)
1533 strlcat(fmtnew, ":", sz);
1534 strlcat(fmtnew, "+T+4<", sz);
1536 if (window_name_enabled) {
1537 if (r->ws->focus->floating)
1538 strlcat(fmtnew, "+F ", sz);
1539 strlcat(fmtnew, "+64W ", sz);
1543 /* finally add the action script output and the version */
1544 strlcat(fmtnew, "+4<+A+4<+V", sz);
1548 bar_replace_pad(char *tmp, int *limit, size_t sz)
1550 /* special case; no limit given, pad one space, instead */
1551 if (*limit == sz - 1)
1553 snprintf(tmp, sz, "%*s", *limit, " ");
1556 /* replaces the bar format character sequences (like in tmux(1)) */
1558 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1562 char tmp[SWM_BAR_MAX];
1566 /* reset strlcat(3) buffer */
1569 /* get number, if any */
1572 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1573 limit = sizeof tmp - 1;
1574 if (limit <= 0 || limit >= sizeof tmp)
1575 limit = sizeof tmp - 1;
1577 /* there is nothing to replace (ie EOL) */
1584 bar_replace_pad(tmp, &limit, sizeof tmp);
1587 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1590 bar_class_name(tmp, sizeof tmp, r);
1593 bar_workspace_name(tmp, sizeof tmp, r);
1596 bar_window_float(tmp, sizeof tmp, r);
1599 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1602 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1605 bar_class_title_name(tmp, sizeof tmp, r);
1608 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1611 bar_title_name(tmp, sizeof tmp, r);
1614 bar_urgent(tmp, sizeof tmp);
1617 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1620 bar_window_name(tmp, sizeof tmp, r);
1623 /* unknown character sequence; copy as-is */
1624 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1632 while (limit-- > 0) {
1633 if (*offrep >= sz - 1)
1635 fmtrep[(*offrep)++] = *ptr++;
1643 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1648 while (*fmt != '\0') {
1650 /* skip ordinary characters */
1653 fmtrep[off++] = *fmt++;
1657 /* character sequence found; replace it */
1658 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1667 bar_fmt_expand(char *fmtexp, size_t sz)
1674 /* start by grabbing the current time and date */
1676 localtime_r(&tmt, &tm);
1678 /* figure out what to expand */
1679 if (bar_format != NULL)
1681 else if (bar_format == NULL && clock_enabled)
1683 /* if nothing to expand bail out */
1689 /* copy as-is, just in case the format shouldn't be expanded below */
1690 strlcpy(fmtexp, fmt, sz);
1691 /* finally pass the string through strftime(3) */
1692 #ifndef SWM_DENY_CLOCK_FORMAT
1693 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1694 warnx("format too long");
1702 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1703 char fmtrep[SWM_BAR_MAX];
1705 struct swm_region *r;
1707 /* expand the format by first passing it through strftime(3) */
1708 bar_fmt_expand(fmtexp, sizeof fmtexp);
1710 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1711 for (i = 0; i < num_screens; i++) {
1712 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1715 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1716 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1717 bar_print(r, fmtrep);
1728 if (bar_enabled == 0)
1730 if (bar_extra && bar_extra_running) {
1731 /* ignore short reads; it'll correct itself */
1732 while ((b = fgetln(stdin, &len)) != NULL)
1733 if (b && b[len - 1] == '\n') {
1735 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1737 if (b == NULL && errno != EAGAIN) {
1738 warn("bar_update: bar_extra failed");
1742 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1755 bar_toggle(struct swm_region *r, union arg *args)
1757 struct swm_region *tmpr;
1760 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1762 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1764 for (i = 0; i < num_screens; i++)
1765 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1767 xcb_unmap_window(conn, tmpr->bar->id);
1769 for (i = 0; i < num_screens; i++)
1770 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1772 map_window_raised(tmpr->bar->id);
1775 bar_enabled = !bar_enabled;
1778 /* must be after stack */
1785 struct swm_region *r;
1789 /* do this here because the conf file is in memory */
1790 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1791 /* launch external status app */
1792 bar_extra_running = 1;
1793 if (pipe(bar_pipe) == -1)
1794 err(1, "pipe error");
1795 socket_setnonblock(bar_pipe[0]);
1796 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1797 if (dup2(bar_pipe[0], 0) == -1)
1799 if (dup2(bar_pipe[1], 1) == -1)
1801 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1802 err(1, "could not disable SIGPIPE");
1803 switch (bar_pid = fork()) {
1805 err(1, "cannot fork");
1809 execvp(bar_argv[0], bar_argv);
1810 err(1, "%s external app failed", bar_argv[0]);
1812 default: /* parent */
1818 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1819 for (i = 0; i < num_screens; i++)
1820 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1823 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1824 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1825 xcb_change_window_attributes(conn, r->bar->id,
1826 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1832 bar_setup(struct swm_region *r)
1834 char *default_string;
1835 char **missing_charsets;
1836 int num_missing_charsets = 0;
1838 xcb_screen_t *screen = get_screen(r->s->idx);
1842 XFreeFontSet(display, bar_fs);
1846 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1847 err(1, "bar_setup: calloc: failed to allocate memory.");
1849 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1851 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1852 &num_missing_charsets, &default_string);
1854 if (num_missing_charsets > 0) {
1855 warnx("Unable to load charset(s):");
1857 for (i = 0; i < num_missing_charsets; ++i)
1858 warnx("%s", missing_charsets[i]);
1860 XFreeStringList(missing_charsets);
1862 if (strcmp(default_string, ""))
1863 warnx("Glyphs from those sets will be replaced "
1864 "by '%s'.", default_string);
1866 warnx("Glyphs from those sets won't be drawn.");
1870 errx(1, "Error creating font set structure.");
1872 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1874 bar_height = bar_fs_extents->max_logical_extent.height +
1875 2 * bar_border_width;
1881 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1882 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1883 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1885 r->bar->id = xcb_generate_id(conn);
1886 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1887 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1888 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1889 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1890 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1891 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1892 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1893 | XCB_CW_EVENT_MASK, wa);
1895 r->bar->buffer = xcb_generate_id(conn);
1896 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1897 WIDTH(r->bar), HEIGHT(r->bar));
1899 xcb_randr_select_input(conn, r->bar->id,
1900 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1903 map_window_raised(r->bar->id);
1905 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1906 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1909 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1910 err(1, "could not install bar_signal");
1915 bar_cleanup(struct swm_region *r)
1919 xcb_destroy_window(conn, r->bar->id);
1920 xcb_free_pixmap(conn, r->bar->buffer);
1926 set_win_state(struct ws_win *win, uint16_t state)
1928 uint16_t data[2] = { state, XCB_ATOM_NONE };
1930 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
1935 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
1936 astate, 32, 2, data);
1940 getstate(xcb_window_t w)
1942 uint16_t result = 0;
1943 xcb_get_property_cookie_t c;
1944 xcb_get_property_reply_t *r;
1946 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
1947 r = xcb_get_property_reply(conn, c, NULL);
1950 result = *((uint16_t *)xcb_get_property_value(r));
1954 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
1960 version(struct swm_region *r, union arg *args)
1962 bar_version = !bar_version;
1964 snprintf(bar_vertext, sizeof bar_vertext,
1965 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1967 strlcpy(bar_vertext, "", sizeof bar_vertext);
1972 client_msg(struct ws_win *win, xcb_atom_t a)
1974 xcb_client_message_event_t ev;
1979 bzero(&ev, sizeof ev);
1980 ev.response_type = XCB_CLIENT_MESSAGE;
1981 ev.window = win->id;
1984 ev.data.data32[0] = a;
1985 ev.data.data32[1] = XCB_CURRENT_TIME;
1987 xcb_send_event(conn, False, win->id,
1988 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
1991 /* synthetic response to a ConfigureRequest when not making a change */
1993 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
1995 xcb_configure_notify_event_t ce;
2000 /* send notification of unchanged state. */
2001 bzero(&ce, sizeof(ce));
2002 ce.response_type = XCB_CONFIGURE_NOTIFY;
2005 ce.width = WIDTH(win);
2006 ce.height = HEIGHT(win);
2007 ce.override_redirect = False;
2012 ce.window = win->id;
2013 ce.border_width = BORDER(win);
2014 ce.above_sibling = XCB_WINDOW_NONE;
2017 ce.event = ev->window;
2018 ce.window = ev->window;
2020 /* make response appear more WM_SIZE_HINTS-compliant */
2022 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2023 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2024 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2025 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2026 SH_INC_W(win), SH_INC_H(win));
2030 /* the hint may be set... to 0! */
2031 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2032 ce.width = SH_MIN_W(win);
2033 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2034 ce.height = SH_MIN_H(win);
2039 /* may also be advertized as 0 */
2040 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2041 ce.width = SH_MAX_W(win);
2042 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2043 ce.height = SH_MAX_H(win);
2046 /* resize increment. */
2048 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2049 ce.width -= (ce.width - SH_MIN_W(win)) %
2051 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2052 ce.height -= (ce.height - SH_MIN_H(win)) %
2056 /* adjust x and y for requested border_width. */
2057 ce.x += BORDER(win) - ev->border_width;
2058 ce.y += BORDER(win) - ev->border_width;
2059 ce.border_width = ev->border_width;
2060 ce.above_sibling = ev->sibling;
2063 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2064 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2065 ce.y, ce.width, ce.height, ce.border_width);
2067 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2072 count_win(struct workspace *ws, int count_transient)
2077 TAILQ_FOREACH(win, &ws->winlist, entry) {
2078 if (count_transient == 0 && win->floating)
2080 if (count_transient == 0 && win->transient)
2086 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2092 quit(struct swm_region *r, union arg *args)
2094 DNPRINTF(SWM_D_MISC, "quit\n");
2099 unmap_window(struct ws_win *win)
2104 /* don't unmap again */
2105 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2108 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2110 xcb_unmap_window(conn, win->id);
2111 xcb_change_window_attributes(conn, win->id,
2112 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2119 int i, j, num_screens;
2121 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2122 for (i = 0; i < num_screens; i++)
2123 for (j = 0; j < workspace_limit; j++)
2124 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2129 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2131 xcb_key_press_event_t event;
2132 xcb_keycode_t *keycode;
2137 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2139 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2142 bzero(&event, sizeof(event));
2143 event.event = win->id;
2144 event.root = win->s->root;
2145 event.child = XCB_WINDOW_NONE;
2146 event.time = XCB_CURRENT_TIME;
2147 event.event_x = X(win);
2148 event.event_y = Y(win);
2151 event.same_screen = True;
2152 event.detail = *keycode;
2153 event.state = modifiers;
2155 event.response_type = XCB_KEY_PRESS;
2156 xcb_send_event(conn, True, win->id,
2157 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2159 event.response_type = XCB_KEY_RELEASE;
2160 xcb_send_event(conn, True, win->id,
2161 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2165 restart(struct swm_region *r, union arg *args)
2167 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2169 /* disable alarm because the following code may not be interrupted */
2171 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2172 err(1, "can't disable alarm");
2178 xcb_key_symbols_free(syms);
2180 xcb_disconnect(conn);
2182 execvp(start_argv[0], start_argv);
2183 warn("execvp failed");
2188 root_to_region(xcb_window_t root)
2190 struct swm_region *r = NULL;
2192 xcb_query_pointer_reply_t *qpr;
2194 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2196 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2197 for (i = 0; i < num_screens; i++)
2198 if (screens[i].root == root)
2201 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2202 screens[i].root), NULL);
2205 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2206 qpr->root_x, qpr->root_y);
2207 /* choose a region based on pointer location */
2208 TAILQ_FOREACH(r, &screens[i].rl, entry)
2209 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2210 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2216 r = TAILQ_FIRST(&screens[i].rl);
2222 find_unmanaged_window(xcb_window_t id)
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].unmanagedlist,
2238 find_window(xcb_window_t id)
2241 int i, j, num_screens;
2242 xcb_query_tree_cookie_t c;
2243 xcb_query_tree_reply_t *r;
2245 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2246 for (i = 0; i < num_screens; i++)
2247 for (j = 0; j < workspace_limit; j++)
2248 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2252 c = xcb_query_tree(conn, id);
2253 r = xcb_query_tree_reply(conn, c, NULL);
2257 /* if we were looking for the parent return that window instead */
2258 if (r->parent == 0 || r->root == r->parent)
2261 /* look for parent */
2262 for (i = 0; i < num_screens; i++)
2263 for (j = 0; j < workspace_limit; j++)
2264 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2265 if (r->parent == win->id) {
2275 spawn(int ws_idx, union arg *args, int close_fd)
2280 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2283 close(xcb_get_file_descriptor(conn));
2285 setenv("LD_PRELOAD", SWM_LIB, 1);
2287 if (asprintf(&ret, "%d", ws_idx) == -1) {
2288 warn("spawn: asprintf SWM_WS");
2291 setenv("_SWM_WS", ret, 1);
2295 if (asprintf(&ret, "%d", getpid()) == -1) {
2296 warn("spawn: asprintf _SWM_PID");
2299 setenv("_SWM_PID", ret, 1);
2303 if (setsid() == -1) {
2304 warn("spawn: setsid");
2310 * close stdin and stdout to prevent interaction between apps
2311 * and the baraction script
2312 * leave stderr open to record errors
2314 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2315 warn("spawn: open");
2318 dup2(fd, STDIN_FILENO);
2319 dup2(fd, STDOUT_FILENO);
2324 execvp(args->argv[0], args->argv);
2326 warn("spawn: execvp");
2331 kill_refs(struct ws_win *win)
2333 int i, x, num_screens;
2334 struct swm_region *r;
2335 struct workspace *ws;
2340 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2341 for (i = 0; i < num_screens; i++)
2342 TAILQ_FOREACH(r, &screens[i].rl, entry)
2343 for (x = 0; x < workspace_limit; x++) {
2345 if (win == ws->focus)
2347 if (win == ws->focus_prev)
2348 ws->focus_prev = NULL;
2353 validate_win(struct ws_win *testwin)
2356 struct workspace *ws;
2357 struct swm_region *r;
2358 int i, x, num_screens;
2360 if (testwin == NULL)
2363 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2364 for (i = 0; i < num_screens; i++)
2365 TAILQ_FOREACH(r, &screens[i].rl, entry)
2366 for (x = 0; x < workspace_limit; x++) {
2368 TAILQ_FOREACH(win, &ws->winlist, entry)
2376 validate_ws(struct workspace *testws)
2378 struct swm_region *r;
2379 struct workspace *ws;
2380 int i, x, num_screens;
2382 /* validate all ws */
2383 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2384 for (i = 0; i < num_screens; i++)
2385 TAILQ_FOREACH(r, &screens[i].rl, entry)
2386 for (x = 0; x < workspace_limit; x++) {
2395 unfocus_win(struct ws_win *win)
2397 xcb_window_t none = XCB_WINDOW_NONE;
2399 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2403 if (win->ws == NULL)
2406 if (validate_ws(win->ws))
2407 return; /* XXX this gets hit with thunderbird, needs fixing */
2409 if (win->ws->r == NULL)
2412 if (validate_win(win)) {
2417 if (win->ws->focus == win) {
2418 win->ws->focus = NULL;
2419 win->ws->focus_prev = win;
2422 if (validate_win(win->ws->focus)) {
2423 kill_refs(win->ws->focus);
2424 win->ws->focus = NULL;
2426 if (validate_win(win->ws->focus_prev)) {
2427 kill_refs(win->ws->focus_prev);
2428 win->ws->focus_prev = NULL;
2431 grabbuttons(win, 0);
2432 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2433 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2434 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2435 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2442 int i, j, num_screens;
2444 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2446 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2447 for (i = 0; i < num_screens; i++)
2448 for (j = 0; j < workspace_limit; j++)
2449 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2454 focus_win(struct ws_win *win)
2456 struct ws_win *cfw = NULL;
2457 xcb_get_input_focus_cookie_t c;
2458 xcb_get_input_focus_reply_t *r;
2459 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2461 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2465 if (win->ws == NULL)
2468 if (validate_ws(win->ws))
2469 return; /* XXX this gets hit with thunderbird, needs fixing */
2471 if (validate_win(win)) {
2476 if (validate_win(win)) {
2481 c = xcb_get_input_focus(conn);
2482 r = xcb_get_input_focus_reply(conn, c, NULL);
2484 cur_focus = r->focus;
2487 if ((cfw = find_window(cur_focus)) != NULL)
2490 /* use larger hammer since the window was killed somehow */
2491 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2492 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2493 xcb_change_window_attributes(conn, cfw->id,
2494 XCB_CW_BORDER_PIXEL,
2495 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2498 win->ws->focus = win;
2500 if (win->ws->r != NULL) {
2502 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2503 win->id, XCB_CURRENT_TIME);
2504 grabbuttons(win, 1);
2505 xcb_change_window_attributes(conn, win->id,
2506 XCB_CW_BORDER_PIXEL,
2507 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2508 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2509 win->ws->always_raise)
2510 map_window_raised(win->id);
2512 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2513 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2521 switchws(struct swm_region *r, union arg *args)
2523 int wsid = args->id, unmap_old = 0;
2524 struct swm_region *this_r, *other_r;
2526 struct workspace *new_ws, *old_ws;
2532 if (wsid >= workspace_limit)
2536 old_ws = this_r->ws;
2537 new_ws = &this_r->s->ws[wsid];
2539 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2540 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2542 if (new_ws == NULL || old_ws == NULL)
2544 if (new_ws == old_ws)
2547 other_r = new_ws->r;
2548 if (other_r == NULL) {
2549 /* the other workspace is hidden, hide this one */
2553 /* the other ws is visible in another region, exchange them */
2554 other_r->ws_prior = new_ws;
2555 other_r->ws = old_ws;
2556 old_ws->r = other_r;
2558 this_r->ws_prior = old_ws;
2559 this_r->ws = new_ws;
2562 /* this is needed so that we can click on a window after a restart */
2566 a.id = SWM_ARG_ID_FOCUSCUR;
2567 focus(new_ws->r, &a);
2569 /* unmap old windows */
2571 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2576 cyclews(struct swm_region *r, union arg *args)
2579 struct swm_screen *s = r->s;
2582 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2583 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2588 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2591 case SWM_ARG_ID_CYCLEWS_UP:
2592 if (a.id < workspace_limit - 1)
2597 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2600 case SWM_ARG_ID_CYCLEWS_DOWN:
2604 a.id = workspace_limit - 1;
2611 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2613 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2617 } while (a.id != r->ws->idx);
2621 priorws(struct swm_region *r, union arg *args)
2625 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2626 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2628 if (r->ws_prior == NULL)
2631 a.id = r->ws_prior->idx;
2636 cyclescr(struct swm_region *r, union arg *args)
2638 struct swm_region *rr = NULL;
2640 int i, x, y, num_screens;
2642 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2643 /* do nothing if we don't have more than one screen */
2644 if (!(num_screens > 1 || outputs > 1))
2649 case SWM_ARG_ID_CYCLESC_UP:
2650 rr = TAILQ_NEXT(r, entry);
2652 rr = TAILQ_FIRST(&screens[i].rl);
2654 case SWM_ARG_ID_CYCLESC_DOWN:
2655 rr = TAILQ_PREV(r, swm_region_list, entry);
2657 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2665 /* move mouse to region */
2667 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2668 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2671 a.id = SWM_ARG_ID_FOCUSCUR;
2674 if (rr->ws->focus) {
2675 /* move to focus window */
2676 x = X(rr->ws->focus) + 1;
2677 y = Y(rr->ws->focus) + 1;
2678 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2684 sort_windows(struct ws_win_list *wl)
2686 struct ws_win *win, *parent, *nxt;
2691 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2692 nxt = TAILQ_NEXT(win, entry);
2693 if (win->transient) {
2694 parent = find_window(win->transient);
2695 if (parent == NULL) {
2696 warnx("not possible bug");
2699 TAILQ_REMOVE(wl, win, entry);
2700 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2707 swapwin(struct swm_region *r, union arg *args)
2709 struct ws_win *target, *source;
2710 struct ws_win *cur_focus;
2711 struct ws_win_list *wl;
2714 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2715 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2717 cur_focus = r->ws->focus;
2718 if (cur_focus == NULL)
2722 wl = &source->ws->winlist;
2725 case SWM_ARG_ID_SWAPPREV:
2726 if (source->transient)
2727 source = find_window(source->transient);
2728 target = TAILQ_PREV(source, ws_win_list, entry);
2729 if (target && target->transient)
2730 target = find_window(target->transient);
2731 TAILQ_REMOVE(wl, source, entry);
2733 TAILQ_INSERT_TAIL(wl, source, entry);
2735 TAILQ_INSERT_BEFORE(target, source, entry);
2737 case SWM_ARG_ID_SWAPNEXT:
2738 target = TAILQ_NEXT(source, entry);
2739 /* move the parent and let the sort handle the move */
2740 if (source->transient)
2741 source = find_window(source->transient);
2742 TAILQ_REMOVE(wl, source, entry);
2744 TAILQ_INSERT_HEAD(wl, source, entry);
2746 TAILQ_INSERT_AFTER(wl, target, source, entry);
2748 case SWM_ARG_ID_SWAPMAIN:
2749 target = TAILQ_FIRST(wl);
2750 if (target == source) {
2751 if (source->ws->focus_prev != NULL &&
2752 source->ws->focus_prev != target)
2753 source = source->ws->focus_prev;
2757 if (target == NULL || source == NULL)
2759 source->ws->focus_prev = target;
2760 TAILQ_REMOVE(wl, target, entry);
2761 TAILQ_INSERT_BEFORE(source, target, entry);
2762 TAILQ_REMOVE(wl, source, entry);
2763 TAILQ_INSERT_HEAD(wl, source, entry);
2765 case SWM_ARG_ID_MOVELAST:
2766 TAILQ_REMOVE(wl, source, entry);
2767 TAILQ_INSERT_TAIL(wl, source, entry);
2770 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2780 focus_prev(struct ws_win *win)
2782 struct ws_win *winfocus = NULL;
2783 struct ws_win *cur_focus = NULL;
2784 struct ws_win_list *wl = NULL;
2785 struct workspace *ws = NULL;
2787 if (!(win && win->ws))
2792 cur_focus = ws->focus;
2794 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2795 WINID(win), WINID(cur_focus));
2797 /* pickle, just focus on whatever */
2798 if (cur_focus == NULL) {
2799 /* use prev_focus if valid */
2800 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2801 find_window(WINID(ws->focus_prev)))
2802 winfocus = ws->focus_prev;
2806 /* if transient focus on parent */
2807 if (cur_focus->transient) {
2808 winfocus = find_window(cur_focus->transient);
2812 /* if in max_stack try harder */
2813 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2814 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2815 if (cur_focus != ws->focus_prev)
2816 winfocus = ws->focus_prev;
2818 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2823 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2825 if (winfocus == NULL || winfocus == win) {
2826 switch (focus_close) {
2827 case SWM_STACK_BOTTOM:
2828 winfocus = TAILQ_FIRST(wl);
2831 winfocus = TAILQ_LAST(wl, ws_win_list);
2833 case SWM_STACK_ABOVE:
2834 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2835 if (focus_close_wrap)
2836 winfocus = TAILQ_FIRST(wl);
2838 winfocus = TAILQ_PREV(cur_focus,
2839 ws_win_list, entry);
2842 case SWM_STACK_BELOW:
2843 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2845 if (focus_close_wrap)
2846 winfocus = TAILQ_LAST(wl, ws_win_list);
2848 winfocus = TAILQ_NEXT(cur_focus, entry);
2854 if (winfocus == NULL) {
2855 if (focus_default == SWM_STACK_TOP)
2856 winfocus = TAILQ_LAST(wl, ws_win_list);
2858 winfocus = TAILQ_FIRST(wl);
2862 focus_magic(winfocus);
2866 focus(struct swm_region *r, union arg *args)
2868 struct ws_win *winfocus = NULL, *head;
2869 struct ws_win *cur_focus = NULL;
2870 struct ws_win_list *wl = NULL;
2871 struct workspace *ws = NULL;
2877 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2879 /* treat FOCUS_CUR special */
2880 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2881 if (r->ws->focus && r->ws->focus->iconic == 0)
2882 winfocus = r->ws->focus;
2883 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2884 winfocus = r->ws->focus_prev;
2886 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2887 if (winfocus->iconic == 0)
2890 focus_magic(winfocus);
2894 if ((cur_focus = r->ws->focus) == NULL)
2898 if (TAILQ_EMPTY(wl))
2900 /* make sure there is at least one uniconified window */
2902 TAILQ_FOREACH(winfocus, wl, entry)
2903 if (winfocus->iconic == 0) {
2911 case SWM_ARG_ID_FOCUSPREV:
2912 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2914 head = TAILQ_LAST(wl, ws_win_list);
2916 if (WINID(winfocus) == cur_focus->transient) {
2917 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2919 head = TAILQ_LAST(wl, ws_win_list);
2924 if (winfocus && winfocus->iconic) {
2925 while (winfocus != cur_focus) {
2926 if (winfocus == NULL)
2927 winfocus = TAILQ_LAST(wl, ws_win_list);
2928 if (winfocus->iconic == 0)
2930 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2936 case SWM_ARG_ID_FOCUSNEXT:
2937 head = TAILQ_NEXT(cur_focus, entry);
2939 head = TAILQ_FIRST(wl);
2943 if (winfocus && winfocus->iconic) {
2944 while (winfocus != cur_focus) {
2945 if (winfocus == NULL)
2946 winfocus = TAILQ_FIRST(wl);
2947 if (winfocus->iconic == 0)
2949 winfocus = TAILQ_NEXT(winfocus, entry);
2954 case SWM_ARG_ID_FOCUSMAIN:
2955 winfocus = TAILQ_FIRST(wl);
2956 if (winfocus == cur_focus)
2957 winfocus = cur_focus->ws->focus_prev;
2964 focus_magic(winfocus);
2968 cycle_layout(struct swm_region *r, union arg *args)
2970 struct workspace *ws = r->ws;
2973 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2976 if (ws->cur_layout->l_stack == NULL)
2977 ws->cur_layout = &layouts[0];
2981 a.id = SWM_ARG_ID_FOCUSCUR;
2987 stack_config(struct swm_region *r, union arg *args)
2989 struct workspace *ws = r->ws;
2991 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2994 if (ws->cur_layout->l_config != NULL)
2995 ws->cur_layout->l_config(ws, args->id);
2997 if (args->id != SWM_ARG_ID_STACKINIT)
3004 struct swm_geometry g;
3005 struct swm_region *r;
3011 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3013 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3014 for (i = 0; i < num_screens; i++) {
3018 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3019 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3020 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3022 /* start with screen geometry, adjust for bar */
3024 g.w -= 2 * border_width;
3025 g.h -= 2 * border_width;
3031 r->ws->cur_layout->l_stack(r->ws, &g);
3032 r->ws->cur_layout->l_string(r->ws);
3033 /* save r so we can track region changes */
3040 DNPRINTF(SWM_D_STACK, "stack: end\n");
3044 store_float_geom(struct ws_win *win, struct swm_region *r)
3046 /* retain window geom and region geom */
3047 win->g_float = win->g;
3048 win->g_float.x -= X(r);
3049 win->g_float.y -= Y(r);
3050 win->g_floatvalid = 1;
3051 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3052 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3053 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3054 win->g_float.w, win->g_float.h);
3058 stack_floater(struct ws_win *win, struct swm_region *r)
3063 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3066 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3067 * geom on ws switches or return from max mode
3069 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3070 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3071 /* refloat at last floating relative position */
3072 win->g = win->g_float;
3077 win->floatmaxed = 0;
3080 * if set to fullscreen mode, configure window to maximum size.
3082 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3083 if (!win->g_floatvalid)
3084 store_float_geom(win, win->ws->r);
3086 win->g = win->ws->r->g;
3090 * remove border on fullscreen floater when in fullscreen mode or when
3091 * the quirk is present.
3093 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3094 ((win->quirks & SWM_Q_FULLSCREEN) &&
3095 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3096 if (win->bordered) {
3098 X(win) += border_width;
3099 Y(win) += border_width;
3101 } else if (!win->bordered) {
3103 X(win) -= border_width;
3104 Y(win) -= border_width;
3107 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3108 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3109 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3114 * floaters and transients are auto-centred unless moved
3117 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3118 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3121 /* keep window within region bounds */
3122 constrain_window(win, r, 0);
3128 * Send keystrokes to terminal to decrease/increase the font size as the
3129 * window size changes.
3132 adjust_font(struct ws_win *win)
3134 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3135 win->floating || win->transient)
3138 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3139 WIDTH(win) / win->sh.width_inc < term_width &&
3140 win->font_steps < SWM_MAX_FONT_STEPS) {
3141 win->font_size_boundary[win->font_steps] =
3142 (win->sh.width_inc * term_width) + win->sh.base_width;
3145 win->last_inc = win->sh.width_inc;
3146 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3147 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3148 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3151 win->last_inc = win->sh.width_inc;
3152 fake_keypress(win, XK_KP_Add, ShiftMask);
3156 #define SWAPXY(g) do { \
3158 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3159 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3162 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3164 struct swm_geometry win_g, r_g = *g;
3165 struct ws_win *win, *fs_win = NULL;
3166 int i, j, s, stacks;
3167 int w_inc = 1, h_inc, w_base = 1, h_base;
3168 int hrh, extra = 0, h_slice, last_h = 0;
3169 int split, colno, winno, mwin, msize, mscale;
3170 int remain, missing, v_slice, reconfigure;
3172 xcb_get_window_attributes_reply_t *war;
3174 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3175 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3177 winno = count_win(ws, 0);
3178 if (winno == 0 && count_win(ws, 1) == 0)
3181 TAILQ_FOREACH(win, &ws->winlist, entry)
3182 if (win->transient == 0 && win->floating == 0
3183 && win->iconic == 0)
3190 w_inc = win->sh.width_inc;
3191 w_base = win->sh.base_width;
3192 mwin = ws->l_state.horizontal_mwin;
3193 mscale = ws->l_state.horizontal_msize;
3194 stacks = ws->l_state.horizontal_stacks;
3197 w_inc = win->sh.height_inc;
3198 w_base = win->sh.base_height;
3199 mwin = ws->l_state.vertical_mwin;
3200 mscale = ws->l_state.vertical_msize;
3201 stacks = ws->l_state.vertical_stacks;
3205 if (stacks > winno - mwin)
3206 stacks = winno - mwin;
3210 h_slice = r_g.h / SWM_H_SLICE;
3211 if (mwin && winno > mwin) {
3212 v_slice = r_g.w / SWM_V_SLICE;
3216 win_g.w = v_slice * mscale;
3218 if (w_inc > 1 && w_inc < v_slice) {
3219 /* adjust for window's requested size increment */
3220 remain = (win_g.w - w_base) % w_inc;
3226 win_g.x += r_g.w - msize;
3229 colno = split = winno / stacks;
3230 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3231 2 * border_width) / stacks);
3233 hrh = r_g.h / colno;
3234 extra = r_g.h - (colno * hrh);
3235 win_g.h = hrh - 2 * border_width;
3237 /* stack all the tiled windows */
3238 i = j = 0, s = stacks;
3239 TAILQ_FOREACH(win, &ws->winlist, entry) {
3240 if (win->transient != 0 || win->floating != 0)
3242 if (win->iconic != 0)
3245 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3250 if (split && i == split) {
3251 colno = (winno - mwin) / stacks;
3252 if (s <= (winno - mwin) % stacks)
3254 split = split + colno;
3255 hrh = (r_g.h / colno);
3256 extra = r_g.h - (colno * hrh);
3260 win_g.x += win_g.w + 2 * border_width;
3261 win_g.w = (r_g.w - msize -
3262 (stacks * 2 * border_width)) / stacks;
3264 win_g.w += (r_g.w - msize -
3265 (stacks * 2 * border_width)) % stacks;
3269 win_g.h = hrh - 2 * border_width;
3271 h_inc = win->sh.width_inc;
3272 h_base = win->sh.base_width;
3274 h_inc = win->sh.height_inc;
3275 h_base = win->sh.base_height;
3277 if (j == colno - 1) {
3278 win_g.h = hrh + extra;
3279 } else if (h_inc > 1 && h_inc < h_slice) {
3280 /* adjust for window's requested size increment */
3281 remain = (win_g.h - h_base) % h_inc;
3282 missing = h_inc - remain;
3284 if (missing <= extra || j == 0) {
3296 win_g.y += last_h + 2 * border_width;
3298 if (disable_border && bar_enabled == 0 && winno == 1){
3300 win_g.w += 2 * border_width;
3301 win_g.h += 2 * border_width;
3306 if (X(win) != win_g.y || Y(win) != win_g.x ||
3307 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3311 WIDTH(win) = win_g.h;
3312 HEIGHT(win) = win_g.w;
3315 if (X(win) != win_g.x || Y(win) != win_g.y ||
3316 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3320 WIDTH(win) = win_g.w;
3321 HEIGHT(win) = win_g.h;
3325 if (bordered != win->bordered) {
3327 win->bordered = bordered;
3335 war = xcb_get_window_attributes_reply(conn,
3336 xcb_get_window_attributes(conn, win->id),
3339 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3340 map_window_raised(win->id);
3350 /* now, stack all the floaters and transients */
3351 TAILQ_FOREACH(win, &ws->winlist, entry) {
3352 if (win->transient == 0 && win->floating == 0)
3354 if (win->iconic == 1)
3356 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3361 stack_floater(win, ws->r);
3362 map_window_raised(win->id);
3366 stack_floater(fs_win, ws->r);
3367 map_window_raised(fs_win->id);
3372 vertical_config(struct workspace *ws, int id)
3374 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3378 case SWM_ARG_ID_STACKRESET:
3379 case SWM_ARG_ID_STACKINIT:
3380 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3381 ws->l_state.vertical_mwin = 1;
3382 ws->l_state.vertical_stacks = 1;
3384 case SWM_ARG_ID_MASTERSHRINK:
3385 if (ws->l_state.vertical_msize > 1)
3386 ws->l_state.vertical_msize--;
3388 case SWM_ARG_ID_MASTERGROW:
3389 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3390 ws->l_state.vertical_msize++;
3392 case SWM_ARG_ID_MASTERADD:
3393 ws->l_state.vertical_mwin++;
3395 case SWM_ARG_ID_MASTERDEL:
3396 if (ws->l_state.vertical_mwin > 0)
3397 ws->l_state.vertical_mwin--;
3399 case SWM_ARG_ID_STACKINC:
3400 ws->l_state.vertical_stacks++;
3402 case SWM_ARG_ID_STACKDEC:
3403 if (ws->l_state.vertical_stacks > 1)
3404 ws->l_state.vertical_stacks--;
3406 case SWM_ARG_ID_FLIPLAYOUT:
3407 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3415 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3417 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3419 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3423 horizontal_config(struct workspace *ws, int id)
3425 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3428 case SWM_ARG_ID_STACKRESET:
3429 case SWM_ARG_ID_STACKINIT:
3430 ws->l_state.horizontal_mwin = 1;
3431 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3432 ws->l_state.horizontal_stacks = 1;
3434 case SWM_ARG_ID_MASTERSHRINK:
3435 if (ws->l_state.horizontal_msize > 1)
3436 ws->l_state.horizontal_msize--;
3438 case SWM_ARG_ID_MASTERGROW:
3439 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3440 ws->l_state.horizontal_msize++;
3442 case SWM_ARG_ID_MASTERADD:
3443 ws->l_state.horizontal_mwin++;
3445 case SWM_ARG_ID_MASTERDEL:
3446 if (ws->l_state.horizontal_mwin > 0)
3447 ws->l_state.horizontal_mwin--;
3449 case SWM_ARG_ID_STACKINC:
3450 ws->l_state.horizontal_stacks++;
3452 case SWM_ARG_ID_STACKDEC:
3453 if (ws->l_state.horizontal_stacks > 1)
3454 ws->l_state.horizontal_stacks--;
3456 case SWM_ARG_ID_FLIPLAYOUT:
3457 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3465 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3467 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3469 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3472 /* fullscreen view */
3474 max_stack(struct workspace *ws, struct swm_geometry *g)
3476 struct swm_geometry gg = *g;
3477 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3478 int winno, num_screens;
3480 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3485 winno = count_win(ws, 0);
3486 if (winno == 0 && count_win(ws, 1) == 0)
3489 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3490 TAILQ_FOREACH(win, &ws->winlist, entry) {
3491 if (win->transient) {
3493 parent = find_window(win->transient);
3497 if (win->floating && win->floatmaxed == 0 ) {
3499 * retain geometry for retrieval on exit from
3502 store_float_geom(win, ws->r);
3503 win->floatmaxed = 1;
3506 /* only reconfigure if necessary */
3507 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3508 HEIGHT(win) != gg.h) {
3514 WIDTH(win) += 2 * border_width;
3515 HEIGHT(win) += 2 * border_width;
3520 /* unmap only if we don't have multi screen */
3521 if (win != ws->focus)
3522 if (!(num_screens > 1 || outputs > 1))
3526 /* put the last transient on top */
3529 map_window_raised(parent->id);
3530 stack_floater(wintrans, ws->r);
3531 focus_magic(wintrans);
3536 send_to_ws(struct swm_region *r, union arg *args)
3538 int wsid = args->id;
3539 struct ws_win *win = NULL, *parent;
3540 struct workspace *ws, *nws;
3541 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3542 char ws_idx_str[SWM_PROPLEN];
3545 if (wsid >= workspace_limit)
3548 if (r && r->ws && r->ws->focus)
3554 if (win->ws->idx == wsid)
3557 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3560 nws = &win->s->ws[wsid];
3562 a.id = SWM_ARG_ID_FOCUSPREV;
3564 if (win->transient) {
3565 parent = find_window(win->transient);
3567 unmap_window(parent);
3568 TAILQ_REMOVE(&ws->winlist, parent, entry);
3569 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3574 TAILQ_REMOVE(&ws->winlist, win, entry);
3575 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3576 if (TAILQ_EMPTY(&ws->winlist))
3577 r->ws->focus = NULL;
3580 /* Try to update the window's workspace property */
3581 ws_idx_atom = get_atom_from_string("_SWM_WS");
3583 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3585 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3587 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3588 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3597 pressbutton(struct swm_region *r, union arg *args)
3599 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3600 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3601 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3602 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3606 raise_toggle(struct swm_region *r, union arg *args)
3608 if (r == NULL || r->ws == NULL)
3611 r->ws->always_raise = !r->ws->always_raise;
3613 /* bring floaters back to top */
3614 if (r->ws->always_raise == 0)
3619 iconify(struct swm_region *r, union arg *args)
3623 if (r->ws->focus == NULL)
3625 unmap_window(r->ws->focus);
3626 update_iconic(r->ws->focus, 1);
3629 r->ws->focus = NULL;
3630 a.id = SWM_ARG_ID_FOCUSCUR;
3635 get_win_name(xcb_window_t win)
3638 xcb_get_property_cookie_t c;
3639 xcb_icccm_get_text_property_reply_t r;
3641 c = xcb_icccm_get_wm_name(conn, win);
3642 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3643 if (r.name_len > 0) {
3644 name = malloc(r.name_len + 1);
3646 xcb_icccm_get_text_property_reply_wipe(&r);
3649 memcpy(name, r.name, r.name_len);
3650 name[r.name_len] = '\0';
3652 xcb_icccm_get_text_property_reply_wipe(&r);
3659 uniconify(struct swm_region *r, union arg *args)
3666 DNPRINTF(SWM_D_MISC, "uniconify\n");
3668 if (r == NULL || r->ws == NULL)
3671 /* make sure we have anything to uniconify */
3672 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3673 if (win->ws == NULL)
3674 continue; /* should never happen */
3675 if (win->iconic == 0)
3683 search_resp_action = SWM_SEARCH_UNICONIFY;
3685 spawn_select(r, args, "search", &searchpid);
3687 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3690 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3691 if (win->ws == NULL)
3692 continue; /* should never happen */
3693 if (win->iconic == 0)
3696 name = get_win_name(win->id);
3699 fprintf(lfile, "%s.%u\n", name, win->id);
3707 name_workspace(struct swm_region *r, union arg *args)
3711 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3717 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3719 spawn_select(r, args, "name_workspace", &searchpid);
3721 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3724 fprintf(lfile, "%s", "");
3729 search_workspace(struct swm_region *r, union arg *args)
3732 struct workspace *ws;
3735 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3741 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3743 spawn_select(r, args, "search", &searchpid);
3745 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3748 for (i = 0; i < workspace_limit; i++) {
3752 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3753 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3760 search_win_cleanup(void)
3762 struct search_window *sw = NULL;
3764 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3765 xcb_destroy_window(conn, sw->indicator);
3766 xcb_free_gc(conn, sw->gc);
3767 TAILQ_REMOVE(&search_wl, sw, entry);
3773 search_win(struct swm_region *r, union arg *args)
3775 struct ws_win *win = NULL;
3776 struct search_window *sw = NULL;
3778 uint32_t gcv[3], wa[2];
3783 XRectangle ibox, lbox;
3784 DNPRINTF(SWM_D_MISC, "search_win\n");
3787 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3789 spawn_select(r, args, "search", &searchpid);
3791 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3794 TAILQ_INIT(&search_wl);
3797 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3798 if (win->iconic == 1)
3801 sw = calloc(1, sizeof(struct search_window));
3803 warn("search_win: calloc");
3805 search_win_cleanup();
3811 snprintf(s, sizeof s, "%d", i);
3814 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3816 w = xcb_generate_id(conn);
3817 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3818 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3819 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3820 lbox.width + 4, bar_fs_extents->max_logical_extent.height,
3821 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3822 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3825 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3827 sw->gc = xcb_generate_id(conn);
3828 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3829 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3831 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3832 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3833 map_window_raised(w);
3835 xcb_image_text_8(conn, len, w, sw->gc, 2,
3836 (bar_fs_extents->max_logical_extent.height -
3837 lbox.height) / 2 - lbox.y, s);
3839 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3841 fprintf(lfile, "%d\n", i);
3849 search_resp_uniconify(char *resp, unsigned long len)
3855 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3857 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3858 if (win->iconic == 0)
3860 name = get_win_name(win->id);
3863 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3868 if (strncmp(s, resp, len) == 0) {
3869 /* XXX this should be a callback to generalize */
3870 update_iconic(win, 0);
3879 search_resp_name_workspace(char *resp, unsigned long len)
3881 struct workspace *ws;
3883 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3885 if (search_r->ws == NULL)
3890 free(search_r->ws->name);
3891 search_r->ws->name = NULL;
3895 ws->name = strdup(resp);
3896 if (ws->name == NULL) {
3897 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3898 "strdup: %s", strerror(errno));
3905 search_resp_search_workspace(char *resp, unsigned long len)
3912 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3916 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3923 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3925 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3932 switchws(search_r, &a);
3936 search_resp_search_window(char *resp, unsigned long len)
3941 struct search_window *sw;
3943 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3947 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3952 idx = strtonum(s, 1, INT_MAX, &errstr);
3954 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3961 TAILQ_FOREACH(sw, &search_wl, entry)
3962 if (idx == sw->idx) {
3968 #define MAX_RESP_LEN 1024
3971 search_do_resp(void)
3977 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3982 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3983 warn("search: calloc");
3987 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3989 warn("search: read error");
3992 resp[rbytes] = '\0';
3995 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3996 * newline, so work around that by sanitizing the resp now.
3998 resp[strcspn(resp, "\n")] = '\0';
4001 switch (search_resp_action) {
4002 case SWM_SEARCH_UNICONIFY:
4003 search_resp_uniconify(resp, len);
4005 case SWM_SEARCH_NAME_WORKSPACE:
4006 search_resp_name_workspace(resp, len);
4008 case SWM_SEARCH_SEARCH_WORKSPACE:
4009 search_resp_search_workspace(resp, len);
4011 case SWM_SEARCH_SEARCH_WINDOW:
4012 search_resp_search_window(resp, len);
4017 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4018 search_win_cleanup();
4020 search_resp_action = SWM_SEARCH_NONE;
4021 close(select_resp_pipe[0]);
4026 wkill(struct swm_region *r, union arg *args)
4028 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4030 if (r->ws->focus == NULL)
4033 if (args->id == SWM_ARG_ID_KILLWINDOW)
4034 xcb_kill_client(conn, r->ws->focus->id);
4036 if (r->ws->focus->can_delete)
4037 client_msg(r->ws->focus, adelete);
4044 floating_toggle_win(struct ws_win *win)
4046 struct swm_region *r;
4056 /* reject floating toggles in max stack mode */
4057 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4060 if (win->floating) {
4061 if (!win->floatmaxed) {
4062 /* retain position for refloat */
4063 store_float_geom(win, r);
4067 if (win->g_floatvalid) {
4068 /* refloat at last floating relative position */
4069 X(win) = win->g_float.x + X(r);
4070 Y(win) = win->g_float.y + Y(r);
4071 WIDTH(win) = win->g_float.w;
4072 HEIGHT(win) = win->g_float.h;
4077 ewmh_update_actions(win);
4083 floating_toggle(struct swm_region *r, union arg *args)
4085 struct ws_win *win = r->ws->focus;
4091 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4092 _NET_WM_STATE_TOGGLE);
4096 if (win == win->ws->focus) {
4097 a.id = SWM_ARG_ID_FOCUSCUR;
4098 focus(win->ws->r, &a);
4103 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4105 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4107 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4109 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4112 if (X(win) + BORDER(win) < X(r)) {
4114 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4116 X(win) = X(r) - BORDER(win);
4119 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4121 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4123 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4126 if (Y(win) + BORDER(win) < Y(r)) {
4128 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4130 Y(win) = Y(r) - BORDER(win);
4136 if (HEIGHT(win) < 1)
4142 update_window(struct ws_win *win)
4147 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4148 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4149 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4153 wc[3] = HEIGHT(win);
4154 wc[4] = BORDER(win);
4156 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4157 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4158 wc[3], YESNO(win->bordered));
4160 xcb_configure_window(conn, win->id, mask, wc);
4163 #define SWM_RESIZE_STEPS (50)
4166 resize(struct ws_win *win, union arg *args)
4168 xcb_timestamp_t timestamp = 0;
4169 struct swm_region *r = NULL;
4170 int resize_step = 0;
4171 struct swm_geometry g;
4172 int top = 0, left = 0, resizing;
4174 unsigned int shape; /* cursor style */
4175 xcb_cursor_t cursor;
4176 xcb_font_t cursor_font;
4177 xcb_grab_pointer_cookie_t gpc;
4178 xcb_grab_pointer_reply_t *gpr;
4179 xcb_query_pointer_reply_t *xpr;
4180 xcb_generic_event_t *evt;
4181 xcb_motion_notify_event_t *mne;
4187 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4190 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4191 "transient: 0x%x\n", win->id, YESNO(win->floating),
4194 if (!(win->transient != 0 || win->floating != 0))
4197 /* reject resizes in max mode for floaters (transient ok) */
4198 if (win->floatmaxed)
4202 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4208 case SWM_ARG_ID_WIDTHSHRINK:
4209 WIDTH(win) -= SWM_RESIZE_STEPS;
4212 case SWM_ARG_ID_WIDTHGROW:
4213 WIDTH(win) += SWM_RESIZE_STEPS;
4216 case SWM_ARG_ID_HEIGHTSHRINK:
4217 HEIGHT(win) -= SWM_RESIZE_STEPS;
4220 case SWM_ARG_ID_HEIGHTGROW:
4221 HEIGHT(win) += SWM_RESIZE_STEPS;
4228 constrain_window(win, r, 1);
4230 store_float_geom(win,r);
4234 /* get cursor offset from window root */
4235 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4242 if (xpr->win_x < WIDTH(win) / 2)
4245 if (xpr->win_y < HEIGHT(win) / 2)
4248 if (args->id == SWM_ARG_ID_CENTER)
4251 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4253 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4255 cursor_font = xcb_generate_id(conn);
4256 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4258 cursor = xcb_generate_id(conn);
4259 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4260 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4262 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4263 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4265 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4267 xcb_free_cursor(conn, cursor);
4268 xcb_close_font(conn, cursor_font);
4275 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4277 XMaskEvent(display, MOUSEMASK | ExposureMask |
4278 SubstructureRedirectMask, &ev);
4280 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4281 case XCB_BUTTON_RELEASE:
4282 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4285 case XCB_MOTION_NOTIFY:
4286 mne = (xcb_motion_notify_event_t *)evt;
4287 /* cursor offset/delta from start of the operation */
4288 dx = mne->root_x - xpr->root_x;
4289 dy = mne->root_y - xpr->root_y;
4295 if (args->id == SWM_ARG_ID_CENTER) {
4296 if (g.h / 2 + dy < 1)
4300 HEIGHT(win) = g.h + 2 * dy;
4308 HEIGHT(win) = g.h + dy;
4315 if (args->id == SWM_ARG_ID_CENTER) {
4316 if (g.w / 2 + dx < 1)
4320 WIDTH(win) = g.w + 2 * dx;
4328 WIDTH(win) = g.w + dx;
4331 constrain_window(win, r, 1);
4333 /* not free, don't sync more than 120 times / second */
4334 if ((mne->time - timestamp) > (1000 / 120) ) {
4335 timestamp = mne->time;
4350 store_float_geom(win,r);
4352 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4353 xcb_free_cursor(conn, cursor);
4354 xcb_close_font(conn, cursor_font);
4357 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4361 resize_step(struct swm_region *r, union arg *args)
4363 struct ws_win *win = NULL;
4365 if (r && r->ws && r->ws->focus)
4373 #define SWM_MOVE_STEPS (50)
4376 move(struct ws_win *win, union arg *args)
4378 xcb_timestamp_t timestamp = 0;
4379 int move_step = 0, moving;
4380 struct swm_region *r = NULL;
4381 xcb_font_t cursor_font;
4382 xcb_cursor_t cursor;
4383 xcb_grab_pointer_cookie_t gpc;
4384 xcb_grab_pointer_reply_t *gpr;
4385 xcb_query_pointer_reply_t *qpr;
4386 xcb_generic_event_t *evt;
4387 xcb_motion_notify_event_t *mne;
4393 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4396 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4397 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4399 /* in max_stack mode should only move transients */
4400 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4404 if (win->floating == 0 && !win->transient) {
4405 store_float_geom(win, r);
4406 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4409 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4416 case SWM_ARG_ID_MOVELEFT:
4417 X(win) -= (SWM_MOVE_STEPS - border_width);
4420 case SWM_ARG_ID_MOVERIGHT:
4421 X(win) += (SWM_MOVE_STEPS - border_width);
4424 case SWM_ARG_ID_MOVEUP:
4425 Y(win) -= (SWM_MOVE_STEPS - border_width);
4428 case SWM_ARG_ID_MOVEDOWN:
4429 Y(win) += (SWM_MOVE_STEPS - border_width);
4436 constrain_window(win, r, 0);
4438 store_float_geom(win, r);
4442 cursor_font = xcb_generate_id(conn);
4443 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4445 cursor = xcb_generate_id(conn);
4446 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4447 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4449 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4450 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4451 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4452 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4454 xcb_free_cursor(conn, cursor);
4455 xcb_close_font(conn, cursor_font);
4459 /* get cursor offset from window root */
4460 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4463 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4464 xcb_free_cursor(conn, cursor);
4465 xcb_close_font(conn, cursor_font);
4471 while ((evt = xcb_wait_for_event(conn)) && moving) {
4472 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4473 case XCB_BUTTON_RELEASE:
4474 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4477 case XCB_MOTION_NOTIFY:
4478 mne = (xcb_motion_notify_event_t *)evt;
4479 X(win) = mne->root_x - qpr->win_x - border_width;
4480 Y(win) = mne->root_y - qpr->win_y - border_width;
4482 constrain_window(win, r, 0);
4484 /* not free, don't sync more than 120 times / second */
4485 if ((mne->time - timestamp) > (1000 / 120) ) {
4486 timestamp = mne->time;
4501 store_float_geom(win, r);
4503 xcb_free_cursor(conn, cursor);
4504 xcb_close_font(conn, cursor_font);
4505 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4506 DNPRINTF(SWM_D_EVENT, "move: done\n");
4510 move_step(struct swm_region *r, union arg *args)
4512 struct ws_win *win = NULL;
4514 if (r && r->ws && r->ws->focus)
4519 if (!(win->transient != 0 || win->floating != 0))
4526 /* user/key callable function IDs */
4615 kf_search_workspace,
4617 kf_dumpwins, /* MUST BE LAST */
4621 /* key definitions */
4623 dummykeyfunc(struct swm_region *r, union arg *args)
4628 char name[SWM_FUNCNAME_LEN];
4629 void (*func)(struct swm_region *r, union arg *);
4631 } keyfuncs[kf_invalid + 1] = {
4632 /* name function argument */
4633 { "cycle_layout", cycle_layout, {0} },
4634 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4635 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4636 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4637 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4638 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4639 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4640 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4641 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4642 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4643 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4644 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4645 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4646 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4647 { "quit", quit, {0} },
4648 { "restart", restart, {0} },
4649 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4650 { "ws_1", switchws, {.id = 0} },
4651 { "ws_2", switchws, {.id = 1} },
4652 { "ws_3", switchws, {.id = 2} },
4653 { "ws_4", switchws, {.id = 3} },
4654 { "ws_5", switchws, {.id = 4} },
4655 { "ws_6", switchws, {.id = 5} },
4656 { "ws_7", switchws, {.id = 6} },
4657 { "ws_8", switchws, {.id = 7} },
4658 { "ws_9", switchws, {.id = 8} },
4659 { "ws_10", switchws, {.id = 9} },
4660 { "ws_11", switchws, {.id = 10} },
4661 { "ws_12", switchws, {.id = 11} },
4662 { "ws_13", switchws, {.id = 12} },
4663 { "ws_14", switchws, {.id = 13} },
4664 { "ws_15", switchws, {.id = 14} },
4665 { "ws_16", switchws, {.id = 15} },
4666 { "ws_17", switchws, {.id = 16} },
4667 { "ws_18", switchws, {.id = 17} },
4668 { "ws_19", switchws, {.id = 18} },
4669 { "ws_20", switchws, {.id = 19} },
4670 { "ws_21", switchws, {.id = 20} },
4671 { "ws_22", switchws, {.id = 21} },
4672 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4673 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4674 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4675 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4676 { "ws_prior", priorws, {0} },
4677 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4678 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4679 { "mvws_1", send_to_ws, {.id = 0} },
4680 { "mvws_2", send_to_ws, {.id = 1} },
4681 { "mvws_3", send_to_ws, {.id = 2} },
4682 { "mvws_4", send_to_ws, {.id = 3} },
4683 { "mvws_5", send_to_ws, {.id = 4} },
4684 { "mvws_6", send_to_ws, {.id = 5} },
4685 { "mvws_7", send_to_ws, {.id = 6} },
4686 { "mvws_8", send_to_ws, {.id = 7} },
4687 { "mvws_9", send_to_ws, {.id = 8} },
4688 { "mvws_10", send_to_ws, {.id = 9} },
4689 { "mvws_11", send_to_ws, {.id = 10} },
4690 { "mvws_12", send_to_ws, {.id = 11} },
4691 { "mvws_13", send_to_ws, {.id = 12} },
4692 { "mvws_14", send_to_ws, {.id = 13} },
4693 { "mvws_15", send_to_ws, {.id = 14} },
4694 { "mvws_16", send_to_ws, {.id = 15} },
4695 { "mvws_17", send_to_ws, {.id = 16} },
4696 { "mvws_18", send_to_ws, {.id = 17} },
4697 { "mvws_19", send_to_ws, {.id = 18} },
4698 { "mvws_20", send_to_ws, {.id = 19} },
4699 { "mvws_21", send_to_ws, {.id = 20} },
4700 { "mvws_22", send_to_ws, {.id = 21} },
4701 { "bar_toggle", bar_toggle, {0} },
4702 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4703 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4704 { "float_toggle", floating_toggle,{0} },
4705 { "version", version, {0} },
4706 { "spawn_custom", dummykeyfunc, {0} },
4707 { "iconify", iconify, {0} },
4708 { "uniconify", uniconify, {0} },
4709 { "raise_toggle", raise_toggle, {0} },
4710 { "button2", pressbutton, {2} },
4711 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4712 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4713 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4714 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4715 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4716 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4717 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4718 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4719 { "name_workspace", name_workspace, {0} },
4720 { "search_workspace", search_workspace, {0} },
4721 { "search_win", search_win, {0} },
4722 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4723 { "invalid key func", NULL, {0} },
4726 RB_ENTRY(key) entry;
4729 enum keyfuncid funcid;
4732 RB_HEAD(key_tree, key);
4735 key_cmp(struct key *kp1, struct key *kp2)
4737 if (kp1->keysym < kp2->keysym)
4739 if (kp1->keysym > kp2->keysym)
4742 if (kp1->mod < kp2->mod)
4744 if (kp1->mod > kp2->mod)
4750 RB_GENERATE(key_tree, key, entry, key_cmp);
4751 struct key_tree keys;
4754 enum { client_click, root_click };
4756 unsigned int action;
4758 unsigned int button;
4759 void (*func)(struct ws_win *, union arg *);
4762 /* action key mouse button func args */
4763 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4764 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4765 { client_click, MODKEY, Button1, move, {0} },
4769 update_modkey(unsigned int mod)
4775 RB_FOREACH(kp, key_tree, &keys)
4776 if (kp->mod & ShiftMask)
4777 kp->mod = mod | ShiftMask;
4781 for (i = 0; i < LENGTH(buttons); i++)
4782 if (buttons[i].mask & ShiftMask)
4783 buttons[i].mask = mod | ShiftMask;
4785 buttons[i].mask = mod;
4790 TAILQ_ENTRY(spawn_prog) entry;
4795 TAILQ_HEAD(spawn_list, spawn_prog);
4796 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4799 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4802 struct spawn_prog *prog = NULL;
4804 char *ap, **real_args;
4806 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4809 TAILQ_FOREACH(prog, &spawns, entry) {
4810 if (!strcasecmp(spawn_name, prog->name))
4814 warnx("spawn_custom: program %s not found", spawn_name);
4818 /* make room for expanded args */
4819 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4820 err(1, "spawn_custom: calloc real_args");
4822 /* expand spawn_args into real_args */
4823 for (i = 0; i < prog->argc; i++) {
4825 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4826 if (!strcasecmp(ap, "$bar_border")) {
4828 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4830 err(1, "spawn_custom border color");
4831 } else if (!strcasecmp(ap, "$bar_color")) {
4833 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4835 err(1, "spawn_custom bar color");
4836 } else if (!strcasecmp(ap, "$bar_font")) {
4837 if ((real_args[i] = strdup(bar_fonts))
4839 err(1, "spawn_custom bar fonts");
4840 } else if (!strcasecmp(ap, "$bar_font_color")) {
4842 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4844 err(1, "spawn_custom color font");
4845 } else if (!strcasecmp(ap, "$color_focus")) {
4847 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4849 err(1, "spawn_custom color focus");
4850 } else if (!strcasecmp(ap, "$color_unfocus")) {
4852 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4854 err(1, "spawn_custom color unfocus");
4856 /* no match --> copy as is */
4857 if ((real_args[i] = strdup(ap)) == NULL)
4858 err(1, "spawn_custom strdup(ap)");
4860 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4865 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4866 for (i = 0; i < prog->argc; i++)
4867 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4868 DNPRINTF(SWM_D_SPAWN, "\n");
4870 *ret_args = real_args;
4871 return (prog->argc);
4875 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4881 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4885 spawn(r->ws->idx, &a, 1);
4887 for (i = 0; i < spawn_argc; i++)
4893 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4899 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4903 if (pipe(select_list_pipe) == -1)
4904 err(1, "pipe error");
4905 if (pipe(select_resp_pipe) == -1)
4906 err(1, "pipe error");
4908 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4909 err(1, "could not disable SIGPIPE");
4910 switch (*pid = fork()) {
4912 err(1, "cannot fork");
4915 if (dup2(select_list_pipe[0], 0) == -1)
4917 if (dup2(select_resp_pipe[1], 1) == -1)
4919 close(select_list_pipe[1]);
4920 close(select_resp_pipe[0]);
4921 spawn(r->ws->idx, &a, 0);
4923 default: /* parent */
4924 close(select_list_pipe[0]);
4925 close(select_resp_pipe[1]);
4929 for (i = 0; i < spawn_argc; i++)
4935 spawn_insert(char *name, char *args)
4937 char *arg, *cp, *ptr;
4938 struct spawn_prog *sp;
4940 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4942 if ((sp = calloc(1, sizeof *sp)) == NULL)
4943 err(1, "spawn_insert: malloc");
4944 if ((sp->name = strdup(name)) == NULL)
4945 err(1, "spawn_insert: strdup");
4947 /* convert the arguments to an argument list */
4948 if ((ptr = cp = strdup(args)) == NULL)
4949 err(1, "spawn_insert: strdup");
4950 while ((arg = strsep(&ptr, " \t")) != NULL) {
4951 /* empty field; skip it */
4956 if ((sp->argv = realloc(sp->argv, sp->argc *
4957 sizeof *sp->argv)) == NULL)
4958 err(1, "spawn_insert: realloc");
4959 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4960 err(1, "spawn_insert: strdup");
4964 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4965 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4969 spawn_remove(struct spawn_prog *sp)
4973 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4975 TAILQ_REMOVE(&spawns, sp, entry);
4976 for (i = 0; i < sp->argc; i++)
4982 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4986 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4988 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4991 spawn_insert(name, args);
4993 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4997 setspawn(char *name, char *args)
4999 struct spawn_prog *sp;
5001 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5006 TAILQ_FOREACH(sp, &spawns, entry) {
5007 if (!strcmp(sp->name, name)) {
5011 spawn_replace(sp, name, args);
5012 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5016 if (*args == '\0') {
5017 warnx("error: setspawn: cannot find program: %s", name);
5021 spawn_insert(name, args);
5022 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5026 setconfspawn(char *selector, char *value, int flags)
5028 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5030 setspawn(selector, value);
5032 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5039 setconfspawn("term", "xterm", 0);
5040 setconfspawn("spawn_term", "xterm", 0);
5041 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5042 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5043 setconfspawn("lock", "xlock", 0);
5044 setconfspawn("initscr", "initscreen.sh", 0);
5045 setconfspawn("menu", "dmenu_run"
5048 " -nf $bar_font_color"
5050 " -sf $bar_color", 0);
5051 setconfspawn("search", "dmenu"
5055 " -nf $bar_font_color"
5057 " -sf $bar_color", 0);
5058 setconfspawn("name_workspace", "dmenu"
5062 " -nf $bar_font_color"
5064 " -sf $bar_color", 0);
5068 #define SWM_MODNAME_SIZE 32
5069 #define SWM_KEY_WS "\n+ \t"
5071 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5075 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5076 if (mod == NULL || ks == NULL) {
5077 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5080 if (keystr == NULL || strlen(keystr) == 0) {
5081 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5087 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5088 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5090 cp += (long)strspn(cp, SWM_KEY_WS);
5091 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5093 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5095 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5097 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5099 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5101 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5103 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5104 *mod |= ControlMask;
5106 *ks = XStringToKeysym(name);
5107 XConvertCase(*ks, ks, &uks);
5108 if (ks == NoSymbol) {
5110 "parsekeys: invalid key %s\n",
5116 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5121 strdupsafe(char *str)
5126 return (strdup(str));
5130 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5134 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5135 keyfuncs[kfid].name, spawn_name);
5137 if ((kp = malloc(sizeof *kp)) == NULL)
5138 err(1, "key_insert: malloc");
5143 kp->spawn_name = strdupsafe(spawn_name);
5144 RB_INSERT(key_tree, &keys, kp);
5146 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5150 key_lookup(unsigned int mod, KeySym ks)
5157 return (RB_FIND(key_tree, &keys, &kp));
5161 key_remove(struct key *kp)
5163 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5165 RB_REMOVE(key_tree, &keys, kp);
5166 free(kp->spawn_name);
5169 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5173 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5176 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5180 key_insert(mod, ks, kfid, spawn_name);
5182 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5186 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5191 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5192 keyfuncs[kfid].name, spawn_name);
5194 if ((kp = key_lookup(mod, ks)) != NULL) {
5195 if (kfid == kf_invalid)
5198 key_replace(kp, mod, ks, kfid, spawn_name);
5199 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5202 if (kfid == kf_invalid) {
5203 warnx("error: setkeybinding: cannot find mod/key combination");
5204 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5208 key_insert(mod, ks, kfid, spawn_name);
5209 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5213 setconfbinding(char *selector, char *value, int flags)
5215 enum keyfuncid kfid;
5218 struct spawn_prog *sp;
5219 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5220 if (selector == NULL) {
5221 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5222 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5224 setkeybinding(mod, ks, kfid, NULL);
5229 /* search by key function name */
5230 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5231 if (strncasecmp(selector, keyfuncs[kfid].name,
5232 SWM_FUNCNAME_LEN) == 0) {
5233 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5235 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5236 setkeybinding(mod, ks, kfid, NULL);
5242 /* search by custom spawn name */
5243 TAILQ_FOREACH(sp, &spawns, entry) {
5244 if (strcasecmp(selector, sp->name) == 0) {
5245 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5247 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5248 setkeybinding(mod, ks, kf_spawn_custom,
5255 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5262 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5263 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5264 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5265 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5266 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5267 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5268 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5269 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5270 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5271 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5272 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5273 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5274 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5275 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5276 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5277 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5278 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5279 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5280 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5281 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5282 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5283 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5284 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5285 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5286 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5287 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5288 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5289 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5290 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5291 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5292 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5293 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5294 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5295 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5296 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5297 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5298 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5299 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5300 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5301 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5302 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5303 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5304 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5305 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5306 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5307 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5308 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5309 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5310 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5311 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5312 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5313 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5314 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5315 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5316 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5317 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5318 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5319 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5320 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5321 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5322 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5323 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5324 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5325 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5326 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5327 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5328 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5329 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5330 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5331 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5332 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5333 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5334 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5335 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5336 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5337 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5338 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5339 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5340 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5341 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5342 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5343 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5344 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5345 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5346 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5347 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5348 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5349 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5350 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5351 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5352 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5353 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5354 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5355 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5356 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5357 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5359 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5368 while (RB_EMPTY(&keys) == 0) {
5369 kp = RB_ROOT(&keys);
5375 setkeymapping(char *selector, char *value, int flags)
5377 char keymapping_file[PATH_MAX];
5378 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5379 if (value[0] == '~')
5380 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5381 pwd->pw_dir, &value[1]);
5383 strlcpy(keymapping_file, value, sizeof keymapping_file);
5385 /* load new key bindings; if it fails, revert to default bindings */
5386 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5390 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5395 updatenumlockmask(void)
5398 xcb_get_modifier_mapping_reply_t *modmap_r;
5399 xcb_keycode_t *modmap, kc;
5401 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5404 modmap_r = xcb_get_modifier_mapping_reply(conn,
5405 xcb_get_modifier_mapping(conn),
5408 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5409 for (i = 0; i < 8; i++) {
5410 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5411 kc = modmap[i * modmap_r->keycodes_per_modifier
5414 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5416 numlockmask = (1 << i);
5428 xcb_keycode_t *code;
5429 unsigned int modifiers[] =
5430 { 0, LockMask, numlockmask, numlockmask | LockMask };
5433 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5434 updatenumlockmask();
5436 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5437 for (k = 0; k < num_screens; k++) {
5438 if (TAILQ_EMPTY(&screens[k].rl))
5440 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5442 RB_FOREACH(kp, key_tree, &keys) {
5443 if ((code = xcb_key_symbols_get_keycode(syms,
5445 for (j = 0; j < LENGTH(modifiers); j++)
5446 xcb_grab_key(conn, True,
5448 kp->mod | modifiers[j],
5449 *code, XCB_GRAB_MODE_ASYNC,
5450 XCB_GRAB_MODE_ASYNC);
5456 grabbuttons(struct ws_win *win, int focused)
5459 unsigned int modifiers[] =
5460 { 0, LockMask, numlockmask, numlockmask|LockMask };
5462 updatenumlockmask();
5463 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5464 XCB_BUTTON_MASK_ANY);
5466 for (i = 0; i < LENGTH(buttons); i++)
5467 if (buttons[i].action == client_click)
5468 for (j = 0; j < LENGTH(modifiers); j++)
5469 xcb_grab_button(conn, False, win->id,
5471 XCB_GRAB_MODE_ASYNC,
5478 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5479 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5480 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5483 const char *quirkname[] = {
5484 "NONE", /* config string for "no value" */
5493 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5494 #define SWM_Q_WS "\n|+ \t"
5496 parsequirks(char *qstr, unsigned long *quirk)
5506 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5508 cp += (long)strspn(cp, SWM_Q_WS);
5509 for (i = 0; i < LENGTH(quirkname); i++) {
5510 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5511 DNPRINTF(SWM_D_QUIRK,
5512 "parsequirks: %s\n", name);
5517 *quirk |= 1 << (i-1);
5521 if (i >= LENGTH(quirkname)) {
5522 DNPRINTF(SWM_D_QUIRK,
5523 "parsequirks: invalid quirk [%s]\n", name);
5531 quirk_insert(const char *class, const char *name, unsigned long quirk)
5535 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5538 if ((qp = malloc(sizeof *qp)) == NULL)
5539 err(1, "quirk_insert: malloc");
5540 if ((qp->class = strdup(class)) == NULL)
5541 err(1, "quirk_insert: strdup");
5542 if ((qp->name = strdup(name)) == NULL)
5543 err(1, "quirk_insert: strdup");
5546 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5548 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5552 quirk_remove(struct quirk *qp)
5554 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5555 qp->name, qp->quirk);
5557 TAILQ_REMOVE(&quirks, qp, entry);
5562 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5566 quirk_replace(struct quirk *qp, const char *class, const char *name,
5567 unsigned long quirk)
5569 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5570 qp->name, qp->quirk);
5573 quirk_insert(class, name, quirk);
5575 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5579 setquirk(const char *class, const char *name, unsigned long quirk)
5583 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5586 TAILQ_FOREACH(qp, &quirks, entry) {
5587 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5591 quirk_replace(qp, class, name, quirk);
5592 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5597 warnx("error: setquirk: cannot find class/name combination");
5601 quirk_insert(class, name, quirk);
5602 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5606 setconfquirk(char *selector, char *value, int flags)
5608 char *cp, *class, *name;
5610 unsigned long quirks;
5611 if (selector == NULL)
5613 if ((cp = strchr(selector, ':')) == NULL)
5618 if ((retval = parsequirks(value, &quirks)) == 0)
5619 setquirk(class, name, quirks);
5626 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5627 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5628 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5629 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5630 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5631 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5632 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5633 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5634 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5635 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5636 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5637 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5638 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5641 /* conf file stuff */
5642 #define SWM_CONF_FILE "spectrwm.conf"
5643 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5647 SWM_S_BAR_AT_BOTTOM,
5648 SWM_S_BAR_BORDER_WIDTH,
5655 SWM_S_CLOCK_ENABLED,
5658 SWM_S_CYCLE_VISIBLE,
5660 SWM_S_DISABLE_BORDER,
5662 SWM_S_FOCUS_CLOSE_WRAP,
5663 SWM_S_FOCUS_DEFAULT,
5669 SWM_S_STACK_ENABLED,
5671 SWM_S_TITLE_CLASS_ENABLED,
5672 SWM_S_TITLE_NAME_ENABLED,
5673 SWM_S_URGENT_ENABLED,
5674 SWM_S_VERBOSE_LAYOUT,
5675 SWM_S_WINDOW_NAME_ENABLED,
5676 SWM_S_WORKSPACE_LIMIT
5680 setconfvalue(char *selector, char *value, int flags)
5686 case SWM_S_BAR_ACTION:
5688 if ((bar_argv[0] = strdup(value)) == NULL)
5689 err(1, "setconfvalue: bar_action");
5691 case SWM_S_BAR_AT_BOTTOM:
5692 bar_at_bottom = atoi(value);
5694 case SWM_S_BAR_BORDER_WIDTH:
5695 bar_border_width = atoi(value);
5696 if (bar_border_width < 0)
5697 bar_border_width = 0;
5699 case SWM_S_BAR_DELAY:
5700 bar_delay = atoi(value);
5702 case SWM_S_BAR_ENABLED:
5703 bar_enabled = atoi(value);
5705 case SWM_S_BAR_FONT:
5707 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5708 err(1, "setconfvalue: asprintf: failed to allocate "
5709 "memory for bar_fonts.");
5713 case SWM_S_BAR_FORMAT:
5715 if ((bar_format = strdup(value)) == NULL)
5716 err(1, "setconfvalue: bar_format");
5718 case SWM_S_BAR_JUSTIFY:
5719 if (!strcmp(value, "left"))
5720 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5721 else if (!strcmp(value, "center"))
5722 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5723 else if (!strcmp(value, "right"))
5724 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5726 errx(1, "invalid bar_justify");
5728 case SWM_S_BORDER_WIDTH:
5729 border_width = atoi(value);
5730 if (border_width < 0)
5733 case SWM_S_CLOCK_ENABLED:
5734 clock_enabled = atoi(value);
5736 case SWM_S_CLOCK_FORMAT:
5737 #ifndef SWM_DENY_CLOCK_FORMAT
5739 if ((clock_format = strdup(value)) == NULL)
5740 err(1, "setconfvalue: clock_format");
5743 case SWM_S_CYCLE_EMPTY:
5744 cycle_empty = atoi(value);
5746 case SWM_S_CYCLE_VISIBLE:
5747 cycle_visible = atoi(value);
5749 case SWM_S_DIALOG_RATIO:
5750 dialog_ratio = atof(value);
5751 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5754 case SWM_S_DISABLE_BORDER:
5755 disable_border = atoi(value);
5757 case SWM_S_FOCUS_CLOSE:
5758 if (!strcmp(value, "first"))
5759 focus_close = SWM_STACK_BOTTOM;
5760 else if (!strcmp(value, "last"))
5761 focus_close = SWM_STACK_TOP;
5762 else if (!strcmp(value, "next"))
5763 focus_close = SWM_STACK_ABOVE;
5764 else if (!strcmp(value, "previous"))
5765 focus_close = SWM_STACK_BELOW;
5767 errx(1, "focus_close");
5769 case SWM_S_FOCUS_CLOSE_WRAP:
5770 focus_close_wrap = atoi(value);
5772 case SWM_S_FOCUS_DEFAULT:
5773 if (!strcmp(value, "last"))
5774 focus_default = SWM_STACK_TOP;
5775 else if (!strcmp(value, "first"))
5776 focus_default = SWM_STACK_BOTTOM;
5778 errx(1, "focus_default");
5780 case SWM_S_FOCUS_MODE:
5781 if (!strcmp(value, "default"))
5782 focus_mode = SWM_FOCUS_DEFAULT;
5783 else if (!strcmp(value, "follow_cursor"))
5784 focus_mode = SWM_FOCUS_FOLLOW;
5785 else if (!strcmp(value, "synergy"))
5786 focus_mode = SWM_FOCUS_SYNERGY;
5788 errx(1, "focus_mode");
5790 case SWM_S_SPAWN_ORDER:
5791 if (!strcmp(value, "first"))
5792 spawn_position = SWM_STACK_BOTTOM;
5793 else if (!strcmp(value, "last"))
5794 spawn_position = SWM_STACK_TOP;
5795 else if (!strcmp(value, "next"))
5796 spawn_position = SWM_STACK_ABOVE;
5797 else if (!strcmp(value, "previous"))
5798 spawn_position = SWM_STACK_BELOW;
5800 errx(1, "spawn_position");
5802 case SWM_S_SPAWN_TERM:
5803 setconfspawn("term", value, 0);
5804 setconfspawn("spawn_term", value, 0);
5808 case SWM_S_SS_ENABLED:
5809 ss_enabled = atoi(value);
5811 case SWM_S_STACK_ENABLED:
5812 stack_enabled = atoi(value);
5814 case SWM_S_TERM_WIDTH:
5815 term_width = atoi(value);
5819 case SWM_S_TITLE_CLASS_ENABLED:
5820 title_class_enabled = atoi(value);
5822 case SWM_S_TITLE_NAME_ENABLED:
5823 title_name_enabled = atoi(value);
5825 case SWM_S_URGENT_ENABLED:
5826 urgent_enabled = atoi(value);
5828 case SWM_S_VERBOSE_LAYOUT:
5829 verbose_layout = atoi(value);
5830 for (i = 0; layouts[i].l_stack != NULL; i++) {
5832 layouts[i].l_string = fancy_stacker;
5834 layouts[i].l_string = plain_stacker;
5837 case SWM_S_WINDOW_NAME_ENABLED:
5838 window_name_enabled = atoi(value);
5840 case SWM_S_WORKSPACE_LIMIT:
5841 workspace_limit = atoi(value);
5842 if (workspace_limit > SWM_WS_MAX)
5843 workspace_limit = SWM_WS_MAX;
5844 else if (workspace_limit < 1)
5845 workspace_limit = 1;
5854 setconfmodkey(char *selector, char *value, int flags)
5856 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5857 update_modkey(Mod1Mask);
5858 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5859 update_modkey(Mod2Mask);
5860 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5861 update_modkey(Mod3Mask);
5862 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5863 update_modkey(Mod4Mask);
5870 setconfcolor(char *selector, char *value, int flags)
5872 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5877 setconfregion(char *selector, char *value, int flags)
5879 custom_region(value);
5884 setautorun(char *selector, char *value, int flags)
5894 if (getenv("SWM_STARTED"))
5898 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5899 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5901 if (ws_id < 0 || ws_id >= workspace_limit)
5902 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5905 * This is a little intricate
5907 * If the pid already exists we simply reuse it because it means it was
5908 * used before AND not claimed by manage_window. We get away with
5909 * altering it in the parent after INSERT because this can not be a race
5912 while ((ap = strsep(&sp, " \t")) != NULL) {
5915 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5917 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5918 err(1, "setautorun: realloc");
5919 a.argv[argc - 1] = ap;
5922 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5923 err(1, "setautorun: realloc");
5924 a.argv[argc] = NULL;
5926 if ((pid = fork()) == 0) {
5927 spawn(ws_id, &a, 1);
5936 p = calloc(1, sizeof *p);
5939 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5949 setlayout(char *selector, char *value, int flags)
5951 int ws_id, i, x, mg, ma, si, raise, f = 0;
5952 int st = SWM_V_STACK, num_screens;
5954 struct workspace *ws;
5956 if (getenv("SWM_STARTED"))
5960 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5961 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5962 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5963 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5966 if (ws_id < 0 || ws_id >= workspace_limit)
5967 errx(1, "layout: invalid workspace %d", ws_id + 1);
5969 if (!strcasecmp(s, "vertical"))
5971 else if (!strcasecmp(s, "vertical_flip")) {
5974 } else if (!strcasecmp(s, "horizontal"))
5976 else if (!strcasecmp(s, "horizontal_flip")) {
5979 } else if (!strcasecmp(s, "fullscreen"))
5982 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5983 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5986 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5987 for (i = 0; i < num_screens; i++) {
5988 ws = (struct workspace *)&screens[i].ws;
5989 ws[ws_id].cur_layout = &layouts[st];
5991 ws[ws_id].always_raise = raise;
5992 if (st == SWM_MAX_STACK)
5996 for (x = 0; x < abs(mg); x++) {
5997 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5998 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5999 SWM_ARG_ID_MASTERSHRINK);
6003 for (x = 0; x < abs(ma); x++) {
6004 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6005 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6006 SWM_ARG_ID_MASTERDEL);
6010 for (x = 0; x < abs(si); x++) {
6011 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6012 si >= 0 ? SWM_ARG_ID_STACKINC :
6013 SWM_ARG_ID_STACKDEC);
6018 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6019 SWM_ARG_ID_FLIPLAYOUT);
6027 /* config options */
6028 struct config_option {
6030 int (*func)(char*, char*, int);
6033 struct config_option configopt[] = {
6034 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6035 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6036 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6037 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6038 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6039 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6040 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6041 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6042 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6043 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6044 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6045 { "keyboard_mapping", setkeymapping, 0 },
6046 { "bind", setconfbinding, 0 },
6047 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6048 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6049 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6050 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6051 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6052 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6053 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6054 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6055 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6056 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6057 { "modkey", setconfmodkey, 0 },
6058 { "program", setconfspawn, 0 },
6059 { "quirk", setconfquirk, 0 },
6060 { "region", setconfregion, 0 },
6061 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6062 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6063 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6064 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6065 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6066 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6067 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6068 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6069 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6070 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6071 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6072 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6073 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6074 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6075 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6076 { "autorun", setautorun, 0 },
6077 { "layout", setlayout, 0 },
6082 conf_load(char *filename, int keymapping)
6085 char *line, *cp, *optsub, *optval;
6086 size_t linelen, lineno = 0;
6087 int wordlen, i, optind;
6088 struct config_option *opt;
6090 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6092 if (filename == NULL) {
6093 warnx("conf_load: no filename");
6096 if ((config = fopen(filename, "r")) == NULL) {
6097 warn("conf_load: fopen: %s", filename);
6101 while (!feof(config)) {
6102 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6105 err(1, "%s", filename);
6110 cp += strspn(cp, " \t\n"); /* eat whitespace */
6111 if (cp[0] == '\0') {
6116 /* get config option */
6117 wordlen = strcspn(cp, "=[ \t\n");
6119 warnx("%s: line %zd: no option found",
6124 for (i = 0; i < LENGTH(configopt); i++) {
6125 opt = &configopt[i];
6126 if (!strncasecmp(cp, opt->optname, wordlen) &&
6127 strlen(opt->optname) == wordlen) {
6133 warnx("%s: line %zd: unknown option %.*s",
6134 filename, lineno, wordlen, cp);
6137 if (keymapping && strcmp(opt->optname, "bind")) {
6138 warnx("%s: line %zd: invalid option %.*s",
6139 filename, lineno, wordlen, cp);
6143 cp += strspn(cp, " \t\n"); /* eat whitespace */
6144 /* get [selector] if any */
6148 wordlen = strcspn(cp, "]");
6151 warnx("%s: line %zd: syntax error",
6156 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6158 warnx("%s: line %zd: unable to allocate"
6159 "memory for selector", filename,
6165 cp += strspn(cp, "] \t\n"); /* eat trailing */
6167 cp += strspn(cp, "= \t\n"); /* eat trailing */
6169 optval = strdup(cp);
6170 /* call function to deal with it all */
6171 if (configopt[optind].func(optsub, optval,
6172 configopt[optind].funcflags) != 0)
6173 errx(1, "%s: line %zd: invalid data for %s",
6174 filename, lineno, configopt[optind].optname);
6181 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6188 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6194 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6196 struct ws_win *parent, *w;
6197 struct swm_region *r;
6198 struct workspace *ws;
6199 xcb_icccm_wm_hints_t wmh;
6201 parent = find_window(win->transient);
6203 parent->child_trans = win;
6205 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6206 " for 0x%x trans 0x%x\n", win->id, win->transient);
6208 r = root_to_region(win->wa->root);
6210 /* parent doen't exist in our window list */
6211 TAILQ_FOREACH(w, &ws->winlist, entry) {
6212 if (xcb_icccm_get_wm_hints_reply(conn,
6213 xcb_icccm_get_wm_hints(conn, w->id),
6215 warnx("can't get hints for 0x%x", w->id);
6219 if (win->hints.window_group != wmh.window_group)
6222 w->child_trans = win;
6223 win->transient = w->id;
6225 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6226 "transient to 0x%x\n", win->transient);
6233 window_get_pid(xcb_window_t win)
6238 xcb_get_property_cookie_t pc;
6239 xcb_get_property_reply_t *pr;
6241 apid = get_atom_from_string("_NET_WM_PID");
6242 if (apid == XCB_ATOM_NONE)
6245 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6246 pr = xcb_get_property_reply(conn, pc, NULL);
6249 if (pr->type != XCB_ATOM_CARDINAL)
6252 ret = *(long *)xcb_get_property_value(pr);
6258 apid = get_atom_from_string("_SWM_PID");
6259 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6261 pr = xcb_get_property_reply(conn, pc, NULL);
6264 if (pr->type != XCB_ATOM_STRING)
6267 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6274 manage_window(xcb_window_t id)
6276 xcb_window_t trans = XCB_WINDOW_NONE;
6277 struct workspace *ws;
6278 struct ws_win *win, *ww;
6279 int i, ws_idx, border_me = 0;
6280 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6281 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6283 struct swm_region *r;
6287 uint32_t event_mask;
6289 xcb_get_property_reply_t *gpr;
6290 xcb_icccm_get_wm_protocols_reply_t wpr;
6292 if ((win = find_window(id)) != NULL)
6293 return (win); /* already being managed */
6295 /* see if we are on the unmanaged list */
6296 if ((win = find_unmanaged_window(id)) != NULL) {
6297 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6298 "window: 0x%x\n", win->id);
6299 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6301 set_child_transient(win, &trans);
6303 if (trans && (ww = find_window(trans)))
6304 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6305 else if ((ww = win->ws->focus) &&
6306 spawn_position == SWM_STACK_ABOVE)
6307 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6309 else if (ww && spawn_position == SWM_STACK_BELOW)
6310 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6311 else switch (spawn_position) {
6314 case SWM_STACK_ABOVE:
6315 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6317 case SWM_STACK_BOTTOM:
6318 case SWM_STACK_BELOW:
6319 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6322 ewmh_update_actions(win);
6326 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6327 err(1, "manage_window: calloc: failed to allocate memory for "
6333 /* see if we need to override the workspace */
6334 p = find_pid(window_get_pid(id));
6336 /* Get all the window data in one shot */
6337 ws_idx_atom = get_atom_from_string("_SWM_WS");
6339 gpr = xcb_get_property_reply(conn,
6340 xcb_get_property(conn, False, id, ws_idx_atom,
6341 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6344 proplen = xcb_get_property_value_length(gpr);
6346 prop = malloc(proplen + 1);
6349 xcb_get_property_value(gpr),
6351 prop[proplen] = '\0';
6357 win->wa = xcb_get_geometry_reply(conn,
6358 xcb_get_geometry(conn, id),
6360 xcb_icccm_get_wm_normal_hints_reply(conn,
6361 xcb_icccm_get_wm_normal_hints(conn, id),
6363 xcb_icccm_get_wm_hints_reply(conn,
6364 xcb_icccm_get_wm_hints(conn, id),
6366 xcb_icccm_get_wm_transient_for_reply(conn,
6367 xcb_icccm_get_wm_transient_for(conn, id),
6370 win->transient = trans;
6371 set_child_transient(win, &trans);
6372 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6373 "transient: 0x%x\n", win->id, win->transient);
6376 prot = get_atom_from_string("WM_PROTOCOLS");
6378 /* get supported protocols */
6379 if (xcb_icccm_get_wm_protocols_reply(conn,
6380 xcb_icccm_get_wm_protocols(conn, id, prot),
6382 for (i = 0; i < wpr.atoms_len; i++) {
6383 if (wpr.atoms[i] == takefocus)
6384 win->take_focus = 1;
6385 if (wpr.atoms[i] == adelete)
6386 win->can_delete = 1;
6388 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6391 win->iconic = get_iconic(win);
6394 * Figure out where to put the window. If it was previously assigned to
6395 * a workspace (either by spawn() or manually moving), and isn't
6396 * transient, * put it in the same workspace
6398 r = root_to_region(win->wa->root);
6400 ws = &r->s->ws[p->ws];
6401 TAILQ_REMOVE(&pidlist, p, entry);
6404 } else if (prop && win->transient == 0) {
6405 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6406 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6409 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6412 ws = &r->s->ws[ws_idx];
6415 /* this should launch transients in the same ws as parent */
6417 if ((ww = find_window(trans)) != NULL)
6423 warnx("manage_window: fix this "
6429 /* set up the window layout */
6432 win->s = r->s; /* this never changes */
6433 if (trans && (ww = find_window(trans)))
6434 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6435 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6436 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6438 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6439 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6440 else switch (spawn_position) {
6443 case SWM_STACK_ABOVE:
6444 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6446 case SWM_STACK_BOTTOM:
6447 case SWM_STACK_BELOW:
6448 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6451 /* ignore window border if there is one. */
6452 WIDTH(win) = win->wa->width;
6453 HEIGHT(win) = win->wa->height;
6454 X(win) = win->wa->x + win->wa->border_width;
6455 Y(win) = win->wa->y + win->wa->border_width;
6457 win->g_floatvalid = 0;
6458 win->floatmaxed = 0;
6459 win->ewmh_flags = 0;
6461 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6462 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6463 HEIGHT(win), ws->idx);
6465 constrain_window(win, r, 0);
6467 /* Set window properties so we can remember this after reincarnation */
6468 if (ws_idx_atom && prop == NULL &&
6469 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6471 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6473 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6474 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6480 ewmh_autoquirk(win);
6482 if (xcb_icccm_get_wm_class_reply(conn,
6483 xcb_icccm_get_wm_class(conn, win->id),
6485 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6486 win->ch.class_name, win->ch.instance_name);
6488 /* java is retarded so treat it special */
6489 if (strstr(win->ch.instance_name, "sun-awt")) {
6494 TAILQ_FOREACH(qp, &quirks, entry) {
6495 if (!strcmp(win->ch.class_name, qp->class) &&
6496 !strcmp(win->ch.instance_name, qp->name)) {
6497 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6498 "class: %s, name: %s\n", win->ch.class_name,
6499 win->ch.instance_name);
6500 if (qp->quirk & SWM_Q_FLOAT) {
6504 win->quirks = qp->quirk;
6509 /* alter window position if quirky */
6510 if (win->quirks & SWM_Q_ANYWHERE) {
6511 win->manual = 1; /* don't center the quirky windows */
6512 if (bar_enabled && Y(win) < bar_height)
6513 Y(win) = bar_height;
6514 if (WIDTH(win) + X(win) > WIDTH(r))
6515 X(win) = WIDTH(r) - WIDTH(win) - 2;
6519 /* Reset font sizes (the bruteforce way; no default keybinding). */
6520 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6521 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6522 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6523 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6524 fake_keypress(win, XK_KP_Add, ShiftMask);
6527 ewmh_get_win_state(win);
6528 ewmh_update_actions(win);
6529 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6534 X(win) -= border_width;
6535 Y(win) -= border_width;
6539 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6540 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6542 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6544 /* floaters need to be mapped if they are in the current workspace */
6545 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6546 map_window_raised(win->id);
6552 free_window(struct ws_win *win)
6554 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6559 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6564 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6569 memset(win, 0xff, sizeof *win); /* XXX kill later */
6572 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6576 unmanage_window(struct ws_win *win)
6578 struct ws_win *parent;
6579 xcb_screen_t *screen;
6584 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6586 if (win->transient) {
6587 parent = find_window(win->transient);
6589 parent->child_trans = NULL;
6592 /* focus on root just in case */
6593 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6594 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6595 screen->root, XCB_CURRENT_TIME);
6599 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6600 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6604 focus_magic(struct ws_win *win)
6606 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6609 /* if there are no windows clear the status-bar */
6614 if (win->child_trans) {
6615 /* win = parent & has a transient so focus on that */
6617 focus_win(win->child_trans);
6618 if (win->child_trans->take_focus)
6619 client_msg(win, takefocus);
6621 /* make sure transient hasn't disappeared */
6622 if (validate_win(win->child_trans) == 0) {
6623 focus_win(win->child_trans);
6624 if (win->child_trans->take_focus)
6625 client_msg(win->child_trans, takefocus);
6627 win->child_trans = NULL;
6629 if (win->take_focus)
6630 client_msg(win, takefocus);
6636 if (win->take_focus)
6637 client_msg(win, takefocus);
6642 expose(xcb_expose_event_t *e)
6645 struct swm_region *r;
6647 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6649 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6650 for (i = 0; i < num_screens; i++)
6651 TAILQ_FOREACH(r, &screens[i].rl, entry)
6652 if (e->window == WINID(r->bar))
6659 keypress(xcb_key_press_event_t *e)
6663 struct swm_region *r;
6665 keysym = XkbKeycodeToKeysym(display, (KeyCode)e->detail, 0, 0);
6667 DNPRINTF(SWM_D_EVENT, "keypress: %u\n", e->detail);
6669 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6671 if (keyfuncs[kp->funcid].func == NULL)
6674 r = root_to_region(e->root);
6675 if (kp->funcid == kf_spawn_custom)
6676 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6678 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6682 buttonpress(xcb_button_press_event_t *e)
6687 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6688 e->event, e->detail);
6690 if ((win = find_window(e->event)) == NULL)
6694 action = client_click;
6696 for (i = 0; i < LENGTH(buttons); i++)
6697 if (action == buttons[i].action && buttons[i].func &&
6698 buttons[i].button == e->detail &&
6699 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6700 buttons[i].func(win, &buttons[i].args);
6704 configurerequest(xcb_configure_request_event_t *e)
6709 uint32_t wc[7] = {0};
6711 if ((win = find_window(e->window)) == NULL)
6712 if ((win = find_unmanaged_window(e->window)) == NULL)
6716 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6717 mask |= XCB_CONFIG_WINDOW_X;
6720 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6721 mask |= XCB_CONFIG_WINDOW_Y;
6724 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6725 mask |= XCB_CONFIG_WINDOW_WIDTH;
6728 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6729 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6730 wc[i++] = e->height;
6732 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6733 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6734 wc[i++] = e->border_width;
6736 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6737 mask |= XCB_CONFIG_WINDOW_SIBLING;
6738 wc[i++] = e->sibling;
6740 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6741 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6742 wc[i++] = e->stack_mode;
6745 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6746 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e->window,
6747 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6749 xcb_configure_window(conn, e->window, mask, wc);
6750 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6751 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6752 win->g_float.x = e->x - X(win->ws->r);
6753 win->g_float.y = e->y - Y(win->ws->r);
6754 win->g_float.w = e->width;
6755 win->g_float.h = e->height;
6756 win->g_floatvalid = 1;
6758 if (win->floating) {
6759 win->g = win->g_float;
6760 win->g.x += X(win->ws->r);
6761 win->g.y += Y(win->ws->r);
6772 configurenotify(xcb_configure_notify_event_t *e)
6776 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6779 win = find_window(e->window);
6781 xcb_icccm_get_wm_normal_hints_reply(conn,
6782 xcb_icccm_get_wm_normal_hints(conn, win->id),
6791 destroynotify(xcb_destroy_notify_event_t *e)
6795 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6797 if ((win = find_window(e->window)) == NULL) {
6798 if ((win = find_unmanaged_window(e->window)) == NULL)
6804 /* make sure we focus on something */
6807 unmanage_window(win);
6813 enternotify(xcb_enter_notify_event_t *e)
6818 Window focus_return;
6819 int revert_to_return;
6821 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6822 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6823 "state: %d\n", e->event, e->mode, e->detail, e->root,
6824 e->child, YESNO(e->same_screen_focus), e->state);
6826 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6827 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6832 switch (focus_mode) {
6833 case SWM_FOCUS_DEFAULT:
6835 case SWM_FOCUS_FOLLOW:
6837 case SWM_FOCUS_SYNERGY:
6840 * all these checks need to be in this order because the
6841 * XCheckTypedWindowEvent relies on weeding out the previous events
6843 * making this code an option would enable a follow mouse for focus
6848 * state is set when we are switching workspaces and focus is set when
6849 * the window or a subwindow already has focus (occurs during restart).
6851 * Only honor the focus flag if last_focus_event is not FocusOut,
6852 * this allows spectrwm to continue to control focus when another
6853 * program is also playing with it.
6855 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6856 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6861 * happens when a window is created or destroyed and the border
6862 * crosses the mouse pointer and when switching ws
6864 * we need the subwindow test to see if we came from root in order
6865 * to give focus to floaters
6867 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6868 ev->subwindow == 0) {
6869 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6873 /* this window already has focus */
6874 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6875 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6879 /* this window is being deleted or moved to another ws */
6880 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6882 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6883 XPutBackEvent(display, &cne);
6887 if ((win = find_window(ev->window)) == NULL) {
6888 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6893 * In fullstack kill all enters unless they come from a different ws
6894 * (i.e. another region) or focus has been grabbed externally.
6896 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6897 last_focus_event != FocusOut) {
6898 XGetInputFocus(display, &focus_return, &revert_to_return);
6899 if ((w = find_window(focus_return)) == NULL ||
6901 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6909 if ((win = find_window(e->event)) == NULL) {
6910 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6917 /* lets us use one switch statement for arbitrary mode/detail combinations */
6918 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6921 mapnotify(xcb_map_notify_event_t *e)
6925 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
6927 win = manage_window(e->window);
6929 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
6932 * focus_win can only set input focus on a mapped window.
6933 * make sure the window really has focus since it is just being mapped.
6935 if (win->ws->focus == win)
6940 mappingnotify(xcb_mapping_notify_event_t *e)
6942 xcb_refresh_keyboard_mapping(syms, e);
6944 if (e->request == XCB_MAPPING_KEYBOARD)
6949 maprequest(xcb_map_request_event_t *e)
6952 struct swm_region *r;
6953 xcb_get_window_attributes_reply_t *war;
6955 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
6958 war = xcb_get_window_attributes_reply(conn,
6959 xcb_get_window_attributes(conn, e->window),
6963 if (war->override_redirect) {
6969 win = manage_window(e->window);
6971 return; /* can't happen */
6976 /* make new win focused */
6977 r = root_to_region(win->wa->root);
6978 if (win->ws == r->ws)
6983 propertynotify(xcb_property_notify_event_t *e)
6989 xcb_get_atom_name_reply_t *r;
6991 r = xcb_get_atom_name_reply(conn,
6992 xcb_get_atom_name(conn, e->atom),
6995 len = xcb_get_atom_name_name_length(r);
6997 name = malloc(len + 1);
6999 memcpy(name, xcb_get_atom_name_name(r), len);
7002 DNPRINTF(SWM_D_EVENT,
7003 "propertynotify: window: 0x%x, atom: %s\n",
7012 win = find_window(e->window);
7016 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7017 update_iconic(win, 0);
7018 map_window_raised(win->id);
7026 case XCB_ATOM_WM_NORMAL_HINTS:
7027 xcb_icccm_get_wm_normal_hints(conn,
7028 xcb_iccom_get_wm_normal_hints(conn, win->id),
7030 warnx("normal hints: flag 0x%x", win->sh.flags);
7031 if (win->sh.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
7032 WIDTH(win) = win->sh.min_width;
7033 HEIGHT(win) = win->sh.min_height;
7034 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7036 XMoveResizeWindow(display, win->id,
7037 X(win), Y(win), WIDTH(win), HEIGHT(win));
7039 case XCB_ATOM_WM_CLASS:
7040 case XCB_ATOM_WM_NAME:
7049 unmapnotify(xcb_unmap_notify_event_t *e)
7053 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7055 /* determine if we need to help unmanage this window */
7056 win = find_window(e->window);
7060 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7061 unmanage_window(win);
7064 /* resend unmap because we ated it */
7065 xcb_unmap_window(conn, e->window);
7070 visibilitynotify(xcb_visibility_notify_event_t *e)
7072 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7077 clientmessage(xcb_client_message_event_t *e)
7081 win = find_window(e->window);
7086 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, type: %u\n",
7087 e->window, e->type);
7089 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7090 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7093 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7094 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7095 if (win->can_delete)
7096 client_msg(win, adelete);
7098 xcb_kill_client(conn, win->id);
7100 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7101 DNPRINTF(SWM_D_EVENT,
7102 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7103 if (win->floating) {
7104 if (e->data.data32[0] & (1<<8)) /* x */
7105 X(win) = e->data.data32[1];
7106 if (e->data.data32[0] & (1<<9)) /* y */
7107 Y(win) = e->data.data32[2];
7108 if (e->data.data32[0] & (1<<10)) /* width */
7109 WIDTH(win) = e->data.data32[3];
7110 if (e->data.data32[0] & (1<<11)) /* height */
7111 HEIGHT(win) = e->data.data32[4];
7116 /* TODO: Change stack sizes */
7117 /* notify no change was made. */
7118 config_win(win, NULL);
7121 if (e->type == ewmh[_NET_WM_STATE].atom) {
7122 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7123 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7124 if (e->data.data32[2])
7125 ewmh_update_win_state(win, e->data.data32[2],
7133 xerror_start(Display *d, XErrorEvent *ee)
7140 xerror(Display *d, XErrorEvent *ee)
7142 /* warnx("error: %p %p", display, ee); */
7150 xerrorxlib = XSetErrorHandler(xerror_start);
7152 /* this causes an error if some other window manager is running */
7153 XSelectInput(display, DefaultRootWindow(display),
7154 SubstructureRedirectMask);
7159 XSetErrorHandler(xerror);
7165 new_region(struct swm_screen *s, int x, int y, int w, int h)
7167 struct swm_region *r, *n;
7168 struct workspace *ws = NULL;
7171 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7172 s->idx, w, h, x, y);
7174 /* remove any conflicting regions */
7175 n = TAILQ_FIRST(&s->rl);
7178 n = TAILQ_NEXT(r, entry);
7179 if (X(r) < (x + w) &&
7180 (X(r) + WIDTH(r)) > x &&
7182 (Y(r) + HEIGHT(r)) > y) {
7183 if (r->ws->r != NULL)
7184 r->ws->old_r = r->ws->r;
7187 TAILQ_REMOVE(&s->rl, r, entry);
7188 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7192 /* search old regions for one to reuse */
7194 /* size + location match */
7195 TAILQ_FOREACH(r, &s->orl, entry)
7196 if (X(r) == x && Y(r) == y &&
7197 HEIGHT(r) == h && WIDTH(r) == w)
7201 TAILQ_FOREACH(r, &s->orl, entry)
7202 if (HEIGHT(r) == h && WIDTH(r) == w)
7206 TAILQ_REMOVE(&s->orl, r, entry);
7207 /* try to use old region's workspace */
7208 if (r->ws->r == NULL)
7211 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7212 err(1, "new_region: calloc: failed to allocate memory "
7215 /* if we don't have a workspace already, find one */
7217 for (i = 0; i < workspace_limit; i++)
7218 if (s->ws[i].r == NULL) {
7225 errx(1, "new_region: no free workspaces");
7236 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7242 #ifdef SWM_XRR_HAS_CRTC
7245 #endif /* SWM_XRR_HAS_CRTC */
7246 struct swm_region *r;
7248 xcb_randr_get_screen_resources_current_cookie_t src;
7249 xcb_randr_get_screen_resources_current_reply_t *srr;
7250 xcb_randr_get_crtc_info_cookie_t cic;
7251 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7252 xcb_randr_crtc_t *crtc;
7253 xcb_screen_t *screen = get_screen(i);
7255 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7256 if (i >= num_screens)
7257 errx(1, "scan_xrandr: invalid screen");
7259 /* remove any old regions */
7260 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7261 r->ws->old_r = r->ws->r = NULL;
7263 TAILQ_REMOVE(&screens[i].rl, r, entry);
7264 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7268 /* map virtual screens onto physical screens */
7269 #ifdef SWM_XRR_HAS_CRTC
7270 if (xrandr_support) {
7271 src = xcb_randr_get_screen_resources_current(conn,
7273 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7276 new_region(&screens[i], 0, 0,
7277 screen->width_in_pixels,
7278 screen->height_in_pixels);
7281 ncrtc = srr->num_crtcs;
7282 for (c = 0; c < ncrtc; c++) {
7283 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7284 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7286 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7289 if (cir->num_outputs == 0) {
7295 new_region(&screens[i], 0, 0,
7296 screen->width_in_pixels,
7297 screen->height_in_pixels);
7299 new_region(&screens[i],
7300 cir->x, cir->y, cir->width, cir->height);
7305 #endif /* SWM_XRR_HAS_CRTC */
7307 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7308 screen->height_in_pixels);
7313 screenchange(xcb_randr_screen_change_notify_event_t *e)
7315 struct swm_region *r;
7318 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7320 if (e->rotation & (XCB_RANDR_ROTATION_ROTATE_90
7321 | XCB_RANDR_ROTATION_ROTATE_270))
7322 xcb_randr_set_screen_size(conn, e->root, e->height,
7323 e->width, e->mheight, e->mwidth);
7325 xcb_randr_set_screen_size(conn, e->root, e->width,
7326 e->height, e->mwidth, e->mheight);
7328 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7329 /* silly event doesn't include the screen index */
7330 for (i = 0; i < num_screens; i++)
7331 if (screens[i].root == e->root)
7333 if (i >= num_screens)
7334 errx(1, "screenchange: screen not found");
7336 /* brute force for now, just re-enumerate the regions */
7339 /* add bars to all regions */
7340 for (i = 0; i < num_screens; i++)
7341 TAILQ_FOREACH(r, &screens[i].rl, entry)
7349 xcb_window_t *wins = NULL;
7351 int i, j, num_screens;
7352 uint16_t state, manage;
7354 xcb_query_tree_cookie_t qtc;
7355 xcb_query_tree_reply_t *qtr;
7356 xcb_get_window_attributes_cookie_t c;
7357 xcb_get_window_attributes_reply_t *r;
7358 xcb_get_property_cookie_t pc;
7360 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7361 for (i = 0; i < num_screens; i++) {
7362 qtc = xcb_query_tree(conn, screens[i].root);
7363 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7366 wins = xcb_query_tree_children(qtr);
7367 no = xcb_query_tree_children_length(qtr);
7368 /* attach windows to a region */
7369 /* normal windows */
7370 for (j = 0; j < no; j++) {
7371 c = xcb_get_window_attributes(conn, wins[j]);
7372 r = xcb_get_window_attributes_reply(conn, c, NULL);
7375 if (r->override_redirect) {
7380 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7381 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7387 state = getstate(wins[j]);
7388 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7389 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7390 manage_window(wins[j]);
7393 /* transient windows */
7394 for (j = 0; j < no; j++) {
7395 c = xcb_get_window_attributes(conn, wins[j]);
7396 r = xcb_get_window_attributes_reply(conn, c, NULL);
7399 if (r->override_redirect) {
7405 state = getstate(wins[j]);
7406 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7407 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7408 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7409 &wins[j], NULL) && manage)
7410 manage_window(wins[j]);
7419 int i, j, k, num_screens;
7420 struct workspace *ws;
7421 uint32_t gcv[1], wa[1];
7422 const xcb_query_extension_reply_t *qep;
7423 xcb_cursor_t cursor;
7424 xcb_font_t cursor_font;
7425 xcb_randr_query_version_cookie_t c;
7426 xcb_randr_query_version_reply_t *r;
7428 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7429 if ((screens = calloc(num_screens,
7430 sizeof(struct swm_screen))) == NULL)
7431 err(1, "setup_screens: calloc: failed to allocate memory for "
7434 /* initial Xrandr setup */
7435 xrandr_support = False;
7436 c = xcb_randr_query_version(conn, True, True);
7437 r = xcb_randr_query_version_reply(conn, c, NULL);
7439 if (r->major_version >= 1)
7440 xrandr_support = True;
7443 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7444 xrandr_eventbase = qep->first_event;
7446 cursor_font = xcb_generate_id(conn);
7447 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7449 cursor = xcb_generate_id(conn);
7450 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7451 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7454 /* map physical screens */
7455 for (i = 0; i < num_screens; i++) {
7456 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7458 TAILQ_INIT(&screens[i].rl);
7459 TAILQ_INIT(&screens[i].orl);
7460 screens[i].root = RootWindow(display, i);
7462 /* set default colors */
7463 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7464 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7465 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7466 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7467 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7469 /* create graphics context on screen */
7470 screens[i].bar_gc = xcb_generate_id(conn);
7472 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7473 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7475 /* set default cursor */
7476 xcb_change_window_attributes(conn, screens[i].root,
7479 /* init all workspaces */
7480 /* XXX these should be dynamically allocated too */
7481 for (j = 0; j < SWM_WS_MAX; j++) {
7482 ws = &screens[i].ws[j];
7488 TAILQ_INIT(&ws->winlist);
7489 TAILQ_INIT(&ws->unmanagedlist);
7491 for (k = 0; layouts[k].l_stack != NULL; k++)
7492 if (layouts[k].l_config != NULL)
7493 layouts[k].l_config(ws,
7494 SWM_ARG_ID_STACKINIT);
7495 ws->cur_layout = &layouts[0];
7496 ws->cur_layout->l_string(ws);
7502 xcb_randr_select_input(conn, screens[i].root,
7503 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7505 xcb_free_cursor(conn, cursor);
7506 xcb_close_font(conn, cursor_font);
7512 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7513 err(1, "setup_globals: strdup: failed to allocate memory.");
7515 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7516 err(1, "setup_globals: strdup: failed to allocate memory.");
7523 xcb_atom_t netwmcheck, netwmname, utf8_string;
7524 xcb_window_t root, win;
7527 /* work around sun jdk bugs, code from wmname */
7528 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7529 netwmname = get_atom_from_string("_NET_WM_NAME");
7530 utf8_string = get_atom_from_string("UTF8_STRING");
7532 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7533 for (i = 0; i < num_screens; i++) {
7534 root = screens[i].root;
7536 win = xcb_generate_id(conn);
7537 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7538 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7539 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7540 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7541 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7543 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7544 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7545 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7546 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7547 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7548 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7553 event_error(xcb_generic_error_t *e)
7555 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7556 "resource_id: %u, minor_code: %u\n",
7557 xcb_event_get_error_label(e->error_code), e->error_code,
7558 xcb_event_get_request_label(e->major_code), e->major_code,
7559 e->sequence, e->resource_id, e->minor_code);
7563 event_handle(xcb_generic_event_t *evt)
7565 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7567 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7568 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7569 XCB_EVENT_RESPONSE_TYPE(evt));
7572 #define EVENT(type, callback) case type: callback((void *)evt); return
7573 EVENT(0, event_error);
7574 EVENT(XCB_BUTTON_PRESS, buttonpress);
7575 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7576 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7577 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7578 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7579 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7580 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7581 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7582 /*EVENT(XCB_CREATE_NOTIFY, );*/
7583 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7584 EVENT(XCB_ENTER_NOTIFY, enternotify);
7585 EVENT(XCB_EXPOSE, expose);
7586 /*EVENT(XCB_FOCUS_IN, );*/
7587 /*EVENT(XCB_FOCUS_OUT, );*/
7588 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7589 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7590 EVENT(XCB_KEY_PRESS, keypress);
7591 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7592 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7593 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7594 EVENT(XCB_MAP_NOTIFY, mapnotify);
7595 EVENT(XCB_MAP_REQUEST, maprequest);
7596 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7597 /*EVENT(XCB_MOTION_NOTIFY, );*/
7598 /*EVENT(XCB_NO_EXPOSURE, );*/
7599 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7600 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7601 /*EVENT(XCB_RESIZE_REQUEST, );*/
7602 /*EVENT(XCB_SELECTION_CLEAR, );*/
7603 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7604 /*EVENT(XCB_SELECTION_REQUEST, );*/
7605 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7606 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7609 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7610 screenchange((void *)evt);
7614 main(int argc, char *argv[])
7616 struct swm_region *r, *rr;
7617 struct ws_win *winfocus = NULL;
7619 char conf[PATH_MAX], *cfile = NULL;
7621 int xfd, i, num_screens;
7622 struct sigaction sact;
7623 xcb_generic_event_t *evt;
7629 time_started = time(NULL);
7632 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7633 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7635 warnx("no locale support");
7637 if (!X_HAVE_UTF8_STRING)
7638 warnx("no UTF-8 support");
7640 if (!(display = XOpenDisplay(0)))
7641 errx(1, "can not open display");
7643 /* handle some signals */
7644 bzero(&sact, sizeof(sact));
7645 sigemptyset(&sact.sa_mask);
7647 sact.sa_handler = sighdlr;
7648 sigaction(SIGINT, &sact, NULL);
7649 sigaction(SIGQUIT, &sact, NULL);
7650 sigaction(SIGTERM, &sact, NULL);
7651 sigaction(SIGHUP, &sact, NULL);
7653 sact.sa_handler = sighdlr;
7654 sact.sa_flags = SA_NOCLDSTOP;
7655 sigaction(SIGCHLD, &sact, NULL);
7657 conn = XGetXCBConnection(display);
7658 if (xcb_connection_has_error(conn))
7659 errx(1, "can not get XCB connection");
7661 xfd = xcb_get_file_descriptor(conn);
7662 syms = xcb_key_symbols_alloc(conn);
7664 errx(1, "unable to allocate key symbols");
7666 astate = get_atom_from_string("WM_STATE");
7667 aprot = get_atom_from_string("WM_PROTOCOLS");
7668 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7669 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7670 a_wmname = get_atom_from_string("WM_NAME");
7671 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7672 a_utf8_string = get_atom_from_string("UTF8_STRING");
7673 a_string = get_atom_from_string("STRING");
7674 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7676 /* look for local and global conf file */
7677 pwd = getpwuid(getuid());
7679 errx(1, "invalid user: %d", getuid());
7681 xcb_grab_server(conn);
7684 /* flush all events */
7685 while ((evt = xcb_poll_for_event(conn))) {
7686 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7692 errx(1, "other wm running");
7703 for (i = 0; ; i++) {
7708 snprintf(conf, sizeof conf, "%s/.%s",
7709 pwd->pw_dir, SWM_CONF_FILE);
7713 snprintf(conf, sizeof conf, "/etc/%s",
7718 snprintf(conf, sizeof conf, "%s/.%s",
7719 pwd->pw_dir, SWM_CONF_FILE_OLD);
7723 snprintf(conf, sizeof conf, "/etc/%s",
7730 if (strlen(conf) && stat(conf, &sb) != -1)
7731 if (S_ISREG(sb.st_mode)) {
7738 /* load conf (if any) */
7740 conf_load(cfile, SWM_CONF_DEFAULT);
7743 /* set some values to work around bad programs */
7745 /* grab existing windows (before we build the bars) */
7748 if (getenv("SWM_STARTED") == NULL)
7749 setenv("SWM_STARTED", "YES", 1);
7751 /* setup all bars */
7752 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7753 for (i = 0; i < num_screens; i++)
7754 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7755 if (winfocus == NULL)
7756 winfocus = TAILQ_FIRST(&r->ws->winlist);
7765 xcb_ungrab_server(conn);
7769 while ((evt = xcb_poll_for_event(conn))) {
7776 /* if we are being restarted go focus on first window */
7778 rr = winfocus->ws->r;
7780 /* not a visible window */
7784 /* move pointer to first screen if multi screen */
7785 if (num_screens > 1 || outputs > 1)
7786 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7787 rr->s[0].root, 0, 0, 0, 0, X(rr),
7788 Y(rr) + (bar_enabled ? bar_height : 0));
7790 a.id = SWM_ARG_ID_FOCUSCUR;
7801 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7803 DNPRINTF(SWM_D_MISC, "select failed");
7805 if (restart_wm == 1)
7806 restart(NULL, NULL);
7807 if (search_resp == 1)
7820 for (i = 0; i < num_screens; ++i)
7821 if (screens[i].bar_gc != 0)
7822 xcb_free_gc(conn, screens[i].bar_gc);
7823 XFreeFontSet(display, bar_fs);
7825 xcb_key_symbols_free(syms);
7826 xcb_disconnect(conn);