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.
58 #include <sys/types.h>
62 #include <sys/queue.h>
63 #include <sys/param.h>
64 #include <sys/select.h>
65 #if defined(__linux__)
67 #elif defined(__OpenBSD__)
69 #elif defined(__FreeBSD__)
90 #include <X11/cursorfont.h>
91 #include <X11/keysym.h>
92 #include <X11/Xatom.h>
93 #include <X11/XKBlib.h>
94 #include <X11/Xlib-xcb.h>
95 #include <X11/Xproto.h>
96 #include <X11/Xutil.h>
97 #include <X11/extensions/Xrandr.h>
98 #include <X11/extensions/XTest.h>
99 #include <xcb/randr.h>
100 #include <xcb/xcb_atom.h>
101 #include <xcb/xcb_aux.h>
102 #include <xcb/xcb_event.h>
103 #include <xcb/xcb_icccm.h>
104 #include <xcb/xcb_keysyms.h>
105 #include <xcb/xtest.h>
113 #ifdef SPECTRWM_BUILDSTR
114 static const char *buildstr = SPECTRWM_BUILDSTR;
116 static const char *buildstr = SPECTRWM_VERSION;
120 # error XRandR versions less than 1.0 are not supported
125 #define SWM_XRR_HAS_CRTC
129 #if defined(__OpenBSD__)
130 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
186 u_int32_t swm_debug = 0
204 #define DPRINTF(x...)
205 #define DNPRINTF(n,x...)
208 #define LENGTH(x) (int)(sizeof x / sizeof x[0])
209 #define MODKEY Mod1Mask
210 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
211 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
212 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
213 #define SWM_PROPLEN (16)
214 #define SWM_FUNCNAME_LEN (32)
215 #define SWM_KEYS_LEN (255)
216 #define SWM_QUIRK_LEN (64)
217 #define X(r) (r)->g.x
218 #define Y(r) (r)->g.y
219 #define WIDTH(r) (r)->g.w
220 #define HEIGHT(r) (r)->g.h
221 #define BORDER(w) (w->bordered ? border_width : 0)
222 #define MAX_X(r) ((r)->g.x + (r)->g.w)
223 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
224 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
225 #define SH_MIN_W(w) (w)->sh.min_width
226 #define SH_MIN_H(w) (w)->sh.min_height
227 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
228 #define SH_MAX_W(w) (w)->sh.max_width
229 #define SH_MAX_H(w) (w)->sh.max_height
230 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
231 #define SH_INC_W(w) (w)->sh.width_inc
232 #define SH_INC_H(w) (w)->sh.height_inc
233 #define SWM_MAX_FONT_STEPS (3)
234 #define WINID(w) ((w) ? (w)->id : 0)
235 #define YESNO(x) ((x) ? "yes" : "no")
237 #define SWM_FOCUS_DEFAULT (0)
238 #define SWM_FOCUS_SYNERGY (1)
239 #define SWM_FOCUS_FOLLOW (2)
241 #define SWM_CONF_DEFAULT (0)
242 #define SWM_CONF_KEYMAPPING (1)
245 #define SWM_LIB "/usr/local/lib/libswmhack.so"
252 xcb_atom_t a_takefocus;
254 xcb_atom_t a_netwmname;
255 xcb_atom_t a_utf8_string;
257 xcb_atom_t a_swm_iconic;
258 volatile sig_atomic_t running = 1;
259 volatile sig_atomic_t restart_wm = 0;
261 /*int last_focus_event = FocusOut;*/
262 int (*xerrorxlib)(Display *, XErrorEvent *);
266 int xrandr_eventbase;
267 unsigned int numlockmask = 0;
269 xcb_connection_t *conn;
270 xcb_key_symbols_t *syms;
273 int cycle_visible = 0;
275 int font_adjusted = 0;
276 unsigned int mod_key = MODKEY;
279 struct swm_region *search_r;
280 int select_list_pipe[2];
281 int select_resp_pipe[2];
283 volatile sig_atomic_t search_resp;
284 int search_resp_action;
286 struct search_window {
287 TAILQ_ENTRY(search_window) entry;
291 xcb_window_t indicator;
293 TAILQ_HEAD(search_winlist, search_window);
294 struct search_winlist search_wl;
299 SWM_SEARCH_UNICONIFY,
300 SWM_SEARCH_NAME_WORKSPACE,
301 SWM_SEARCH_SEARCH_WORKSPACE,
302 SWM_SEARCH_SEARCH_WINDOW
305 #define SWM_STACK_TOP (0)
306 #define SWM_STACK_BOTTOM (1)
307 #define SWM_STACK_ABOVE (2)
308 #define SWM_STACK_BELOW (3)
311 double dialog_ratio = 0.6;
313 #define SWM_BAR_MAX (256)
314 #define SWM_BAR_JUSTIFY_LEFT (0)
315 #define SWM_BAR_JUSTIFY_CENTER (1)
316 #define SWM_BAR_JUSTIFY_RIGHT (2)
317 #define SWM_BAR_OFFSET (4)
318 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
319 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
320 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
321 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
322 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
324 char *bar_argv[] = { NULL, NULL };
326 unsigned char bar_ext[SWM_BAR_MAX];
327 char bar_vertext[SWM_BAR_MAX];
329 sig_atomic_t bar_alarm = 0;
332 int bar_border_width = 1;
333 int bar_at_bottom = 0;
335 int bar_extra_running = 0;
338 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
339 char *bar_format = NULL;
340 int stack_enabled = 1;
341 int clock_enabled = 1;
342 int urgent_enabled = 0;
343 char *clock_format = NULL;
344 int title_name_enabled = 0;
345 int title_class_enabled = 0;
346 int window_name_enabled = 0;
347 int focus_mode = SWM_FOCUS_DEFAULT;
348 int focus_close = SWM_STACK_BELOW;
349 int focus_close_wrap = 1;
350 int focus_default = SWM_STACK_TOP;
351 int spawn_position = SWM_STACK_TOP;
352 int disable_border = 0;
353 int border_width = 1;
354 int verbose_layout = 0;
359 XFontSetExtents *bar_fs_extents;
364 #define SWM_MENU_FN (2)
365 #define SWM_MENU_NB (4)
366 #define SWM_MENU_NF (6)
367 #define SWM_MENU_SB (8)
368 #define SWM_MENU_SF (10)
370 /* layout manager data */
371 struct swm_geometry {
384 struct swm_geometry g;
387 /* virtual "screens" */
389 TAILQ_ENTRY(swm_region) entry;
390 struct swm_geometry g;
391 struct workspace *ws; /* current workspace on this region */
392 struct workspace *ws_prior; /* prior workspace on this region */
393 struct swm_screen *s; /* screen idx */
396 TAILQ_HEAD(swm_region_list, swm_region);
399 TAILQ_ENTRY(ws_win) entry;
401 xcb_window_t transient;
402 struct ws_win *child_trans; /* transient child window */
403 struct swm_geometry g; /* current geometry */
404 struct swm_geometry g_float; /* region coordinates */
405 int g_floatvalid; /* g_float geometry validity */
406 int floatmaxed; /* whether maxed by max_stack */
411 unsigned int ewmh_flags;
412 int font_size_boundary[SWM_MAX_FONT_STEPS];
418 unsigned long quirks;
419 struct workspace *ws; /* always valid */
420 struct swm_screen *s; /* always valid, never changes */
421 xcb_get_geometry_reply_t *wa;
423 xcb_icccm_get_wm_class_reply_t ch;
424 xcb_icccm_wm_hints_t hints;
426 TAILQ_HEAD(ws_win_list, ws_win);
430 TAILQ_ENTRY(pid_e) entry;
434 TAILQ_HEAD(pid_list, pid_e);
435 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
437 /* layout handlers */
439 void vertical_config(struct workspace *, int);
440 void vertical_stack(struct workspace *, struct swm_geometry *);
441 void horizontal_config(struct workspace *, int);
442 void horizontal_stack(struct workspace *, struct swm_geometry *);
443 void max_stack(struct workspace *, struct swm_geometry *);
444 void plain_stacker(struct workspace *);
445 void fancy_stacker(struct workspace *);
448 void (*l_stack)(struct workspace *, struct swm_geometry *);
449 void (*l_config)(struct workspace *, int);
451 #define SWM_L_FOCUSPREV (1<<0)
452 #define SWM_L_MAPONFOCUS (1<<1)
453 void (*l_string)(struct workspace *);
455 /* stack, configure */
456 { vertical_stack, vertical_config, 0, plain_stacker },
457 { horizontal_stack, horizontal_config, 0, plain_stacker },
459 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
460 { NULL, NULL, 0, NULL },
463 /* position of max_stack mode in the layouts array, index into layouts! */
464 #define SWM_V_STACK (0)
465 #define SWM_H_STACK (1)
466 #define SWM_MAX_STACK (2)
468 #define SWM_H_SLICE (32)
469 #define SWM_V_SLICE (32)
471 /* define work spaces */
473 int idx; /* workspace index */
474 char *name; /* workspace name */
475 int always_raise; /* raise windows on focus */
476 struct layout *cur_layout; /* current layout handlers */
477 struct ws_win *focus; /* may be NULL */
478 struct ws_win *focus_prev; /* may be NULL */
479 struct swm_region *r; /* may be NULL */
480 struct swm_region *old_r; /* may be NULL */
481 struct ws_win_list winlist; /* list of windows in ws */
482 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
483 char stacker[10]; /* display stacker and layout */
487 int horizontal_msize;
489 int horizontal_stacks;
500 SWM_S_COLOR_BAR_BORDER,
501 SWM_S_COLOR_BAR_FONT,
507 /* physical screen mapping */
508 #define SWM_WS_MAX (22) /* hard limit */
509 int workspace_limit = 10; /* soft limit */
512 int idx; /* screen index */
513 struct swm_region_list rl; /* list of regions on this screen */
514 struct swm_region_list orl; /* list of old regions */
516 struct workspace ws[SWM_WS_MAX];
522 } c[SWM_S_COLOR_MAX];
524 xcb_gcontext_t bar_gc;
526 struct swm_screen *screens;
528 /* args to functions */
531 #define SWM_ARG_ID_FOCUSNEXT (0)
532 #define SWM_ARG_ID_FOCUSPREV (1)
533 #define SWM_ARG_ID_FOCUSMAIN (2)
534 #define SWM_ARG_ID_FOCUSCUR (4)
535 #define SWM_ARG_ID_SWAPNEXT (10)
536 #define SWM_ARG_ID_SWAPPREV (11)
537 #define SWM_ARG_ID_SWAPMAIN (12)
538 #define SWM_ARG_ID_MOVELAST (13)
539 #define SWM_ARG_ID_MASTERSHRINK (20)
540 #define SWM_ARG_ID_MASTERGROW (21)
541 #define SWM_ARG_ID_MASTERADD (22)
542 #define SWM_ARG_ID_MASTERDEL (23)
543 #define SWM_ARG_ID_FLIPLAYOUT (24)
544 #define SWM_ARG_ID_STACKRESET (30)
545 #define SWM_ARG_ID_STACKINIT (31)
546 #define SWM_ARG_ID_CYCLEWS_UP (40)
547 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
548 #define SWM_ARG_ID_CYCLESC_UP (42)
549 #define SWM_ARG_ID_CYCLESC_DOWN (43)
550 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
551 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
552 #define SWM_ARG_ID_STACKINC (50)
553 #define SWM_ARG_ID_STACKDEC (51)
554 #define SWM_ARG_ID_SS_ALL (60)
555 #define SWM_ARG_ID_SS_WINDOW (61)
556 #define SWM_ARG_ID_DONTCENTER (70)
557 #define SWM_ARG_ID_CENTER (71)
558 #define SWM_ARG_ID_KILLWINDOW (80)
559 #define SWM_ARG_ID_DELETEWINDOW (81)
560 #define SWM_ARG_ID_WIDTHGROW (90)
561 #define SWM_ARG_ID_WIDTHSHRINK (91)
562 #define SWM_ARG_ID_HEIGHTGROW (92)
563 #define SWM_ARG_ID_HEIGHTSHRINK (93)
564 #define SWM_ARG_ID_MOVEUP (100)
565 #define SWM_ARG_ID_MOVEDOWN (101)
566 #define SWM_ARG_ID_MOVELEFT (102)
567 #define SWM_ARG_ID_MOVERIGHT (103)
573 TAILQ_ENTRY(quirk) entry;
577 #define SWM_Q_FLOAT (1<<0) /* float this window */
578 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
579 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
580 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
581 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
582 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
584 TAILQ_HEAD(quirk_list, quirk);
585 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
588 * Supported EWMH hints should be added to
589 * both the enum and the ewmh array
594 _NET_MOVERESIZE_WINDOW,
595 _NET_WM_ACTION_CLOSE,
596 _NET_WM_ACTION_FULLSCREEN,
598 _NET_WM_ACTION_RESIZE,
599 _NET_WM_ALLOWED_ACTIONS,
602 _NET_WM_STATE_FULLSCREEN,
603 _NET_WM_STATE_HIDDEN,
604 _NET_WM_STATE_MAXIMIZED_HORZ,
605 _NET_WM_STATE_MAXIMIZED_VERT,
606 _NET_WM_STATE_SKIP_PAGER,
607 _NET_WM_STATE_SKIP_TASKBAR,
609 _NET_WM_WINDOW_TYPE_DIALOG,
610 _NET_WM_WINDOW_TYPE_DOCK,
611 _NET_WM_WINDOW_TYPE_NORMAL,
612 _NET_WM_WINDOW_TYPE_SPLASH,
613 _NET_WM_WINDOW_TYPE_TOOLBAR,
614 _NET_WM_WINDOW_TYPE_UTILITY,
615 _SWM_WM_STATE_MANUAL,
622 } ewmh[SWM_EWMH_HINT_MAX] = {
623 /* must be in same order as in the enum */
624 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
625 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
626 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
627 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
628 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
629 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
630 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
631 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
632 {"_NET_WM_STATE", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
637 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
638 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
639 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
644 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
645 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
646 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
647 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
650 /* function prototypes */
651 int conf_load(char *, int);
652 void constrain_window(struct ws_win *, struct swm_region *, int);
654 void event_handle(xcb_generic_event_t *);
655 struct ws_win *find_window(xcb_window_t);
656 int floating_toggle_win(struct ws_win *);
657 void focus(struct swm_region *, union arg *);
658 void focus_magic(struct ws_win *);
659 xcb_atom_t get_atom_from_string(const char *);
660 xcb_screen_t *get_screen(int);
661 char *get_win_name(xcb_window_t);
662 uint16_t getstate(xcb_window_t);
663 void grabbuttons(struct ws_win *, int);
664 void map_window_raised(xcb_window_t);
665 void new_region(struct swm_screen *, int, int, int, int);
666 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
667 void spawn_select(struct swm_region *, union arg *, char *, int *);
668 void store_float_geom(struct ws_win *, struct swm_region *);
669 void unmanage_window(struct ws_win *);
670 void update_window(struct ws_win *);
672 /* function definitions */
674 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
676 unsigned int tmpr, tmpg, tmpb;
678 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
689 get_screen(int screen)
691 xcb_screen_iterator_t i;
693 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
694 for (; i.rem; --screen, xcb_screen_next(&i))
704 xcb_get_input_focus_cookie_t c;
705 xcb_get_input_focus_reply_t *r;
708 c = xcb_get_input_focus(conn);
710 r = xcb_get_input_focus_reply(conn, c, NULL);
716 map_window_raised(xcb_window_t win)
718 uint32_t val = XCB_STACK_MODE_ABOVE;
720 xcb_configure_window(conn, win,
721 XCB_CONFIG_WINDOW_STACK_MODE, &val);
723 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)
848 xcb_get_property_cookie_t c;
849 xcb_get_property_reply_t *r;
851 c = xcb_get_property(conn, False, win->id,
852 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
853 r = xcb_get_property_reply(conn, c, NULL);
857 n = xcb_get_property_value_length(r);
858 type = xcb_get_property_value(r);
860 for (i = 0; i < n; i++) {
861 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
863 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
864 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
865 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
867 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
870 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
871 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
873 win->quirks = SWM_Q_FLOAT;
880 #define SWM_EWMH_ACTION_COUNT_MAX (6)
881 #define EWMH_F_FULLSCREEN (1<<0)
882 #define EWMH_F_ABOVE (1<<1)
883 #define EWMH_F_HIDDEN (1<<2)
884 #define EWMH_F_SKIP_PAGER (1<<3)
885 #define EWMH_F_SKIP_TASKBAR (1<<4)
886 #define SWM_F_MANUAL (1<<5)
889 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
897 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
898 "fullscreen %s\n", win->id, YESNO(fs));
901 if (!win->g_floatvalid)
902 store_float_geom(win, win->ws->r);
904 win->g = win->ws->r->g;
907 if (win->g_floatvalid) {
908 /* refloat at last floating relative position */
909 win->g = win->g_float;
910 X(win) += X(win->ws->r);
911 Y(win) += Y(win->ws->r);
919 ewmh_update_actions(struct ws_win *win)
921 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
927 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
930 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
931 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
934 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
935 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
938 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
939 #define _NET_WM_STATE_ADD 1 /* add/set property */
940 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
943 ewmh_update_win_state(struct ws_win *win, long state, long action)
945 unsigned int mask = 0;
946 unsigned int changed = 0;
947 unsigned int orig_flags;
952 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
953 mask = EWMH_F_FULLSCREEN;
954 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
956 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
958 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
959 mask = EWMH_F_SKIP_PAGER;
960 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
961 mask = EWMH_F_SKIP_TASKBAR;
964 orig_flags = win->ewmh_flags;
967 case _NET_WM_STATE_REMOVE:
968 win->ewmh_flags &= ~mask;
970 case _NET_WM_STATE_ADD:
971 win->ewmh_flags |= mask;
973 case _NET_WM_STATE_TOGGLE:
974 win->ewmh_flags ^= mask;
978 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
980 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
982 if (!floating_toggle_win(win))
983 win->ewmh_flags = orig_flags; /* revert */
984 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
986 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
987 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
989 if (!ewmh_set_win_fullscreen(win,
990 win->ewmh_flags & EWMH_F_FULLSCREEN))
991 win->ewmh_flags = orig_flags; /* revert */
993 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
995 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
996 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
997 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
998 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
999 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1000 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1001 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1002 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1003 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1004 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1005 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1006 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1007 if (win->ewmh_flags & EWMH_F_ABOVE)
1008 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1009 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1010 &ewmh[_NET_WM_STATE_ABOVE].atom);
1011 if (win->ewmh_flags & SWM_F_MANUAL)
1012 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1013 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1014 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1018 ewmh_get_win_state(struct ws_win *win)
1021 xcb_get_property_cookie_t c;
1022 xcb_get_property_reply_t *r;
1028 win->ewmh_flags = 0;
1030 win->ewmh_flags |= EWMH_F_ABOVE;
1032 win->ewmh_flags |= SWM_F_MANUAL;
1034 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1035 XCB_ATOM_ATOM, 0, UINT32_MAX);
1036 r = xcb_get_property_reply(conn, c, NULL);
1040 states = xcb_get_property_value(r);
1041 n = xcb_get_property_value_length(r);
1043 for (i = 0; i < n; i++)
1044 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1052 dumpwins(struct swm_region *r, union arg *args)
1056 xcb_get_window_attributes_cookie_t c;
1057 xcb_get_window_attributes_reply_t *wa;
1059 if (r->ws == NULL) {
1060 warnx("dumpwins: invalid workspace");
1064 warnx("=== managed window list ws %02d ===", r->ws->idx);
1065 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1066 state = getstate(win->id);
1067 c = xcb_get_window_attributes(conn, win->id);
1068 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1070 warnx("window: 0x%x, map_state: %d, state: %u, "
1071 "transient: 0x%x", win->id, wa->map_state,
1072 state, win->transient);
1075 warnx("window: 0x%x, failed xcb_get_window_attributes",
1079 warnx("===== unmanaged window list =====");
1080 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1081 state = getstate(win->id);
1082 c = xcb_get_window_attributes(conn, win->id);
1083 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1085 warnx("window: 0x%x, map_state: %d, state: %u, "
1086 "transient: 0x%x", win->id, wa->map_state,
1087 state, win->transient);
1090 warnx("window: 0x%x, failed xcb_get_window_attributes",
1094 warnx("=================================");
1098 dumpwins(struct swm_region *r, union arg *args)
1101 #endif /* SWM_DEBUG */
1103 void event_error(xcb_generic_error_t *);
1104 void expose(xcb_expose_event_t *);
1105 void keypress(xcb_key_press_event_t *);
1106 void buttonpress(xcb_button_press_event_t *);
1107 void configurerequest(xcb_configure_request_event_t *);
1108 void configurenotify(xcb_configure_notify_event_t *);
1109 void destroynotify(xcb_destroy_notify_event_t *);
1110 void enternotify(xcb_enter_notify_event_t *);
1111 void mapnotify(xcb_map_notify_event_t *);
1112 void mappingnotify(xcb_mapping_notify_event_t *);
1113 void maprequest(xcb_map_request_event_t *);
1114 void propertynotify(xcb_property_notify_event_t *);
1115 void unmapnotify(xcb_unmap_notify_event_t *);
1116 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1117 void clientmessage(xcb_client_message_event_t *);
1118 void screenchange(xcb_randr_screen_change_notify_event_t *);
1123 int saved_errno, status;
1126 saved_errno = errno;
1130 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1135 if (errno != ECHILD)
1136 warn("sighdlr: waitpid");
1137 #endif /* SWM_DEBUG */
1140 if (pid == searchpid)
1144 if (WIFEXITED(status)) {
1145 if (WEXITSTATUS(status) != 0)
1146 warnx("sighdlr: child exit status: %d",
1147 WEXITSTATUS(status));
1149 warnx("sighdlr: child is terminated "
1151 #endif /* SWM_DEBUG */
1165 errno = saved_errno;
1171 struct pid_e *p = NULL;
1173 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1178 TAILQ_FOREACH(p, &pidlist, entry) {
1187 name_to_color(const char *colorname)
1189 uint32_t result = 0;
1190 char cname[32] = "#";
1191 xcb_screen_t *screen;
1192 xcb_colormap_t cmap;
1193 xcb_alloc_color_reply_t *cr;
1194 xcb_alloc_named_color_reply_t *nr;
1195 uint16_t rr, gg, bb;
1197 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1198 cmap = screen->default_colormap;
1200 /* color is in format rgb://rr/gg/bb */
1201 if (strncmp(colorname, "rgb:", 4) == 0) {
1202 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1203 warnx("could not parse rgb %s", colorname);
1205 cr = xcb_alloc_color_reply(conn,
1206 xcb_alloc_color(conn, cmap, rr, gg, bb),
1212 warnx("color '%s' not found", colorname);
1215 nr = xcb_alloc_named_color_reply(conn,
1216 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1219 strlcat(cname, colorname + 2, sizeof cname - 1);
1220 nr = xcb_alloc_named_color_reply(conn,
1221 xcb_alloc_named_color(conn, cmap, strlen(cname),
1228 warnx("color '%s' not found", colorname);
1235 setscreencolor(char *val, int i, int c)
1239 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1240 if (i > 0 && i <= num_screens) {
1241 screens[i - 1].c[c].color = name_to_color(val);
1242 free(screens[i - 1].c[c].name);
1243 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1245 } else if (i == -1) {
1246 for (i = 0; i < num_screens; i++) {
1247 screens[i].c[c].color = name_to_color(val);
1248 free(screens[i].c[c].name);
1249 if ((screens[i].c[c].name = strdup(val)) == NULL)
1253 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1258 fancy_stacker(struct workspace *ws)
1260 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1261 if (ws->cur_layout->l_stack == vertical_stack)
1262 snprintf(ws->stacker, sizeof ws->stacker,
1263 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1264 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1265 if (ws->cur_layout->l_stack == horizontal_stack)
1266 snprintf(ws->stacker, sizeof ws->stacker,
1267 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1268 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1272 plain_stacker(struct workspace *ws)
1274 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1275 if (ws->cur_layout->l_stack == vertical_stack)
1276 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1277 sizeof ws->stacker);
1278 if (ws->cur_layout->l_stack == horizontal_stack)
1279 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1280 sizeof ws->stacker);
1284 custom_region(char *val)
1286 unsigned int sidx, x, y, w, h;
1288 xcb_screen_t *screen;
1290 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1291 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1292 errx(1, "invalid custom region, "
1293 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1294 if (sidx < 1 || sidx > num_screens)
1295 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1299 screen = get_screen(sidx);
1301 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1303 if (x > screen->width_in_pixels ||
1304 y > screen->height_in_pixels ||
1305 w + x > screen->width_in_pixels ||
1306 h + y > screen->height_in_pixels) {
1307 warnx("ignoring region %ux%u+%u+%u - not within screen "
1308 "boundaries (%ux%u)", w, h, x, y,
1309 screen->width_in_pixels, screen->height_in_pixels);
1313 new_region(&screens[sidx], x, y, w, h);
1317 socket_setnonblock(int fd)
1321 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1322 err(1, "fcntl F_GETFL");
1323 flags |= O_NONBLOCK;
1324 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1325 err(1, "fcntl F_SETFL");
1329 bar_print(struct swm_region *r, const char *s)
1333 xcb_rectangle_t rect;
1335 XRectangle ibox, lbox;
1338 /* FIXME fix bar font position calculations */
1340 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1342 switch (bar_justify) {
1343 case SWM_BAR_JUSTIFY_LEFT:
1346 case SWM_BAR_JUSTIFY_CENTER:
1347 x = (WIDTH(r) - lbox.width) / 2;
1349 case SWM_BAR_JUSTIFY_RIGHT:
1350 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1354 if (x < SWM_BAR_OFFSET)
1360 rect.width = WIDTH(r->bar);
1361 rect.height = HEIGHT(r->bar);
1363 /* clear back buffer */
1364 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1365 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1366 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1367 sizeof(rect), &rect);
1369 /* draw back buffer */
1370 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1371 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1372 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1373 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1375 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1376 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1380 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, 4, 14, s);
1384 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1385 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1389 bar_extra_stop(void)
1393 bzero(bar_pipe, sizeof bar_pipe);
1396 kill(bar_pid, SIGTERM);
1399 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1404 bar_class_name(char *s, size_t sz, struct swm_region *r)
1406 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1408 if (r->ws->focus->ch.class_name != NULL)
1409 strlcat(s, r->ws->focus->ch.class_name, sz);
1413 bar_title_name(char *s, size_t sz, struct swm_region *r)
1415 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1417 if (r->ws->focus->ch.instance_name != NULL)
1418 strlcat(s, r->ws->focus->ch.instance_name, sz);
1422 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1424 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1427 bar_class_name(s, sz, r);
1428 strlcat(s, ":", sz);
1429 bar_title_name(s, sz, r);
1433 bar_window_float(char *s, size_t sz, struct swm_region *r)
1435 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1437 if (r->ws->focus->floating)
1438 strlcat(s, "(f)", sz);
1442 bar_window_name(char *s, size_t sz, struct swm_region *r)
1446 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1448 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1451 strlcat(s, title, sz);
1455 int urgent[SWM_WS_MAX];
1457 bar_urgent(char *s, size_t sz)
1460 int i, j, num_screens;
1462 xcb_get_property_cookie_t c;
1463 xcb_icccm_wm_hints_t hints;
1465 for (i = 0; i < workspace_limit; i++)
1468 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1469 for (i = 0; i < num_screens; i++)
1470 for (j = 0; j < workspace_limit; j++)
1471 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1472 c = xcb_icccm_get_wm_hints(conn, win->id);
1473 if (xcb_icccm_get_wm_hints_reply(conn, c,
1476 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1480 for (i = 0; i < workspace_limit; i++) {
1482 snprintf(b, sizeof b, "%d ", i + 1);
1484 snprintf(b, sizeof b, "- ");
1490 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1492 if (r == NULL || r->ws == NULL)
1494 if (r->ws->name != NULL)
1495 strlcat(s, r->ws->name, sz);
1498 /* build the default bar format according to the defined enabled options */
1500 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1502 /* if format provided, just copy the buffers */
1503 if (bar_format != NULL) {
1504 strlcpy(fmtnew, fmtexp, sz);
1508 /* reset the output buffer */
1511 strlcat(fmtnew, "+N:+I ", sz);
1513 strlcat(fmtnew, "+S", sz);
1514 strlcat(fmtnew, " ", sz);
1516 /* only show the workspace name if there's actually one */
1517 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1518 strlcat(fmtnew, "<+D>", sz);
1519 strlcat(fmtnew, "+3<", sz);
1521 if (clock_enabled) {
1522 strlcat(fmtnew, fmtexp, sz);
1523 strlcat(fmtnew, "+4<", sz);
1526 /* bar_urgent already adds the space before the last asterisk */
1528 strlcat(fmtnew, "* +U*+4<", sz);
1530 if (title_class_enabled) {
1531 strlcat(fmtnew, "+C", sz);
1532 if (title_name_enabled == 0)
1533 strlcat(fmtnew, "+4<", sz);
1536 /* checks needed by the colon and floating strlcat(3) calls below */
1537 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1538 if (title_name_enabled) {
1539 if (title_class_enabled)
1540 strlcat(fmtnew, ":", sz);
1541 strlcat(fmtnew, "+T+4<", sz);
1543 if (window_name_enabled) {
1544 if (r->ws->focus->floating)
1545 strlcat(fmtnew, "+F ", sz);
1546 strlcat(fmtnew, "+64W ", sz);
1550 /* finally add the action script output and the version */
1551 strlcat(fmtnew, "+4<+A+4<+V", sz);
1555 bar_replace_pad(char *tmp, int *limit, size_t sz)
1557 /* special case; no limit given, pad one space, instead */
1558 if (*limit == sz - 1)
1560 snprintf(tmp, sz, "%*s", *limit, " ");
1563 /* replaces the bar format character sequences (like in tmux(1)) */
1565 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1569 char tmp[SWM_BAR_MAX];
1573 /* reset strlcat(3) buffer */
1576 /* get number, if any */
1579 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1580 limit = sizeof tmp - 1;
1581 if (limit <= 0 || limit >= sizeof tmp)
1582 limit = sizeof tmp - 1;
1584 /* there is nothing to replace (ie EOL) */
1591 bar_replace_pad(tmp, &limit, sizeof tmp);
1594 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1597 bar_class_name(tmp, sizeof tmp, r);
1600 bar_workspace_name(tmp, sizeof tmp, r);
1603 bar_window_float(tmp, sizeof tmp, r);
1606 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1609 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1612 bar_class_title_name(tmp, sizeof tmp, r);
1615 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1618 bar_title_name(tmp, sizeof tmp, r);
1621 bar_urgent(tmp, sizeof tmp);
1624 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1627 bar_window_name(tmp, sizeof tmp, r);
1630 /* unknown character sequence; copy as-is */
1631 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1639 while (limit-- > 0) {
1640 if (*offrep >= sz - 1)
1642 fmtrep[(*offrep)++] = *ptr++;
1650 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1655 while (*fmt != '\0') {
1657 /* skip ordinary characters */
1660 fmtrep[off++] = *fmt++;
1664 /* character sequence found; replace it */
1665 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1674 bar_fmt_expand(char *fmtexp, size_t sz)
1681 /* start by grabbing the current time and date */
1683 localtime_r(&tmt, &tm);
1685 /* figure out what to expand */
1686 if (bar_format != NULL)
1688 else if (bar_format == NULL && clock_enabled)
1690 /* if nothing to expand bail out */
1696 /* copy as-is, just in case the format shouldn't be expanded below */
1697 strlcpy(fmtexp, fmt, sz);
1698 /* finally pass the string through strftime(3) */
1699 #ifndef SWM_DENY_CLOCK_FORMAT
1700 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1701 warnx("format too long");
1709 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1710 char fmtrep[SWM_BAR_MAX];
1712 struct swm_region *r;
1714 /* expand the format by first passing it through strftime(3) */
1715 bar_fmt_expand(fmtexp, sizeof fmtexp);
1717 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1718 for (i = 0; i < num_screens; i++) {
1719 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1722 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1723 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1724 bar_print(r, fmtrep);
1735 if (bar_enabled == 0)
1737 if (bar_extra && bar_extra_running) {
1738 /* ignore short reads; it'll correct itself */
1739 while ((b = fgetln(stdin, &len)) != NULL)
1740 if (b && b[len - 1] == '\n') {
1742 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1744 if (b == NULL && errno != EAGAIN) {
1745 warn("bar_update: bar_extra failed");
1749 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1762 bar_toggle(struct swm_region *r, union arg *args)
1764 struct swm_region *tmpr;
1767 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1769 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1771 for (i = 0; i < num_screens; i++)
1772 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1774 xcb_unmap_window(conn, tmpr->bar->id);
1776 for (i = 0; i < num_screens; i++)
1777 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1779 map_window_raised(tmpr->bar->id);
1782 bar_enabled = !bar_enabled;
1785 /* must be after stack */
1794 struct swm_region *r;
1798 /* do this here because the conf file is in memory */
1799 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1800 /* launch external status app */
1801 bar_extra_running = 1;
1802 if (pipe(bar_pipe) == -1)
1803 err(1, "pipe error");
1804 socket_setnonblock(bar_pipe[0]);
1805 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1806 if (dup2(bar_pipe[0], 0) == -1)
1808 if (dup2(bar_pipe[1], 1) == -1)
1810 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1811 err(1, "could not disable SIGPIPE");
1812 switch (bar_pid = fork()) {
1814 err(1, "cannot fork");
1818 execvp(bar_argv[0], bar_argv);
1819 err(1, "%s external app failed", bar_argv[0]);
1821 default: /* parent */
1827 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1828 for (i = 0; i < num_screens; i++)
1829 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1832 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1833 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1834 xcb_change_window_attributes(conn, r->bar->id,
1835 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1841 bar_setup(struct swm_region *r)
1843 char *default_string;
1844 char **missing_charsets;
1845 int num_missing_charsets = 0;
1847 xcb_screen_t *screen = get_screen(r->s->idx);
1852 XFreeFontSet(display, bar_fs);
1857 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1858 err(1, "bar_setup: calloc: failed to allocate memory.");
1860 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1862 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1863 &num_missing_charsets, &default_string);*/
1865 if (num_missing_charsets > 0) {
1866 warnx("Unable to load charset(s):");
1868 for (i = 0; i < num_missing_charsets; ++i)
1869 warnx("%s", missing_charsets[i]);
1871 XFreeStringList(missing_charsets);
1873 if (strcmp(default_string, ""))
1874 warnx("Glyphs from those sets will be replaced "
1875 "by '%s'.", default_string);
1877 warnx("Glyphs from those sets won't be drawn.");
1881 errx(1, "Error creating font set structure.");
1883 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1885 bar_height = bar_fs_extents->max_logical_extent.height +
1886 2 * bar_border_width;
1895 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1896 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1897 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1899 r->bar->id = xcb_generate_id(conn);
1900 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1901 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1902 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1903 DNPRINTF(SWM_D_BAR, "bar_setup: create_window: (x,y) w x h: (%d,%d) "
1904 "%d x %d\n", X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar));
1905 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1906 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1907 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1908 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1909 | XCB_CW_EVENT_MASK, wa);
1911 r->bar->buffer = xcb_generate_id(conn);
1912 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1913 WIDTH(r->bar), HEIGHT(r->bar));
1915 xcb_randr_select_input(conn, r->bar->id,
1916 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1919 map_window_raised(r->bar->id);
1921 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1922 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1925 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1926 err(1, "could not install bar_signal");
1931 bar_cleanup(struct swm_region *r)
1935 xcb_destroy_window(conn, r->bar->id);
1936 xcb_free_pixmap(conn, r->bar->buffer);
1942 set_win_state(struct ws_win *win, uint16_t state)
1944 uint16_t data[2] = { state, XCB_ATOM_NONE };
1946 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
1951 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
1952 a_state, 32, 2, data);
1956 getstate(xcb_window_t w)
1958 uint16_t result = 0;
1959 xcb_get_property_cookie_t c;
1960 xcb_get_property_reply_t *r;
1962 c = xcb_get_property(conn, False, w, a_state, a_state, 0L, 2L);
1963 r = xcb_get_property_reply(conn, c, NULL);
1966 result = *((uint16_t *)xcb_get_property_value(r));
1970 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
1976 version(struct swm_region *r, union arg *args)
1978 bar_version = !bar_version;
1980 snprintf(bar_vertext, sizeof bar_vertext,
1981 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1983 strlcpy(bar_vertext, "", sizeof bar_vertext);
1990 client_msg(struct ws_win *win, xcb_atom_t a)
1992 xcb_client_message_event_t ev;
1997 bzero(&ev, sizeof ev);
1998 ev.response_type = XCB_CLIENT_MESSAGE;
1999 ev.window = win->id;
2002 ev.data.data32[0] = a;
2003 ev.data.data32[1] = XCB_CURRENT_TIME;
2005 xcb_send_event(conn, False, win->id,
2006 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2009 /* synthetic response to a ConfigureRequest when not making a change */
2011 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2013 xcb_configure_notify_event_t ce;
2018 /* send notification of unchanged state. */
2019 bzero(&ce, sizeof(ce));
2020 ce.response_type = XCB_CONFIGURE_NOTIFY;
2023 ce.width = WIDTH(win);
2024 ce.height = HEIGHT(win);
2025 ce.override_redirect = False;
2030 ce.window = win->id;
2031 ce.border_width = BORDER(win);
2032 ce.above_sibling = XCB_WINDOW_NONE;
2035 ce.event = ev->window;
2036 ce.window = ev->window;
2038 /* make response appear more WM_SIZE_HINTS-compliant */
2040 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2041 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2042 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2043 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2044 SH_INC_W(win), SH_INC_H(win));
2048 /* the hint may be set... to 0! */
2049 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2050 ce.width = SH_MIN_W(win);
2051 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2052 ce.height = SH_MIN_H(win);
2057 /* may also be advertized as 0 */
2058 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2059 ce.width = SH_MAX_W(win);
2060 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2061 ce.height = SH_MAX_H(win);
2064 /* resize increment. */
2066 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2067 ce.width -= (ce.width - SH_MIN_W(win)) %
2069 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2070 ce.height -= (ce.height - SH_MIN_H(win)) %
2074 /* adjust x and y for requested border_width. */
2075 ce.x += BORDER(win) - ev->border_width;
2076 ce.y += BORDER(win) - ev->border_width;
2077 ce.border_width = ev->border_width;
2078 ce.above_sibling = ev->sibling;
2081 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2082 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2083 ce.y, ce.width, ce.height, ce.border_width);
2085 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2090 count_win(struct workspace *ws, int count_transient)
2095 TAILQ_FOREACH(win, &ws->winlist, entry) {
2096 if (count_transient == 0 && win->floating)
2098 if (count_transient == 0 && win->transient)
2104 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2110 quit(struct swm_region *r, union arg *args)
2112 DNPRINTF(SWM_D_MISC, "quit\n");
2117 unmap_window(struct ws_win *win)
2122 /* don't unmap again */
2123 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2126 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2128 xcb_unmap_window(conn, win->id);
2129 xcb_change_window_attributes(conn, win->id,
2130 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2137 int i, j, num_screens;
2139 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2140 for (i = 0; i < num_screens; i++)
2141 for (j = 0; j < workspace_limit; j++)
2142 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2147 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2149 xcb_key_press_event_t event;
2150 xcb_keycode_t *keycode;
2155 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2157 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2160 bzero(&event, sizeof(event));
2161 event.event = win->id;
2162 event.root = win->s->root;
2163 event.child = XCB_WINDOW_NONE;
2164 event.time = XCB_CURRENT_TIME;
2165 event.event_x = X(win);
2166 event.event_y = Y(win);
2169 event.same_screen = True;
2170 event.detail = *keycode;
2171 event.state = modifiers;
2173 event.response_type = XCB_KEY_PRESS;
2174 xcb_send_event(conn, True, win->id,
2175 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2177 event.response_type = XCB_KEY_RELEASE;
2178 xcb_send_event(conn, True, win->id,
2179 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2183 restart(struct swm_region *r, union arg *args)
2185 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2187 /* disable alarm because the following code may not be interrupted */
2189 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2190 err(1, "can't disable alarm");
2196 xcb_key_symbols_free(syms);
2198 xcb_disconnect(conn);
2200 execvp(start_argv[0], start_argv);
2201 warn("execvp failed");
2206 root_to_region(xcb_window_t root)
2208 struct swm_region *r = NULL;
2210 xcb_query_pointer_reply_t *qpr;
2212 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2214 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2215 for (i = 0; i < num_screens; i++)
2216 if (screens[i].root == root)
2219 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2220 screens[i].root), NULL);
2223 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2224 qpr->root_x, qpr->root_y);
2225 /* choose a region based on pointer location */
2226 TAILQ_FOREACH(r, &screens[i].rl, entry)
2227 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2228 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2234 r = TAILQ_FIRST(&screens[i].rl);
2240 find_unmanaged_window(xcb_window_t id)
2243 int i, j, num_screens;
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].unmanagedlist,
2256 find_window(xcb_window_t id)
2259 int i, j, num_screens;
2260 xcb_query_tree_reply_t *r;
2262 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2263 for (i = 0; i < num_screens; i++)
2264 for (j = 0; j < workspace_limit; j++)
2265 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2269 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2273 /* if we were looking for the parent return that window instead */
2274 if (r->parent == 0 || r->root == r->parent)
2277 /* look for parent */
2278 for (i = 0; i < num_screens; i++)
2279 for (j = 0; j < workspace_limit; j++)
2280 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2281 if (r->parent == win->id) {
2291 spawn(int ws_idx, union arg *args, int close_fd)
2296 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2299 close(xcb_get_file_descriptor(conn));
2301 setenv("LD_PRELOAD", SWM_LIB, 1);
2303 if (asprintf(&ret, "%d", ws_idx) == -1) {
2304 warn("spawn: asprintf SWM_WS");
2307 setenv("_SWM_WS", ret, 1);
2311 if (asprintf(&ret, "%d", getpid()) == -1) {
2312 warn("spawn: asprintf _SWM_PID");
2315 setenv("_SWM_PID", ret, 1);
2319 if (setsid() == -1) {
2320 warn("spawn: setsid");
2326 * close stdin and stdout to prevent interaction between apps
2327 * and the baraction script
2328 * leave stderr open to record errors
2330 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2331 warn("spawn: open");
2334 dup2(fd, STDIN_FILENO);
2335 dup2(fd, STDOUT_FILENO);
2340 execvp(args->argv[0], args->argv);
2342 warn("spawn: execvp");
2347 kill_refs(struct ws_win *win)
2349 int i, x, num_screens;
2350 struct swm_region *r;
2351 struct workspace *ws;
2356 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2357 for (i = 0; i < num_screens; i++)
2358 TAILQ_FOREACH(r, &screens[i].rl, entry)
2359 for (x = 0; x < workspace_limit; x++) {
2361 if (win == ws->focus)
2363 if (win == ws->focus_prev)
2364 ws->focus_prev = NULL;
2369 validate_win(struct ws_win *testwin)
2372 struct workspace *ws;
2373 struct swm_region *r;
2374 int i, x, num_screens;
2376 if (testwin == NULL)
2379 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2380 for (i = 0; i < num_screens; i++)
2381 TAILQ_FOREACH(r, &screens[i].rl, entry)
2382 for (x = 0; x < workspace_limit; x++) {
2384 TAILQ_FOREACH(win, &ws->winlist, entry)
2392 validate_ws(struct workspace *testws)
2394 struct swm_region *r;
2395 struct workspace *ws;
2396 int i, x, num_screens;
2398 /* validate all ws */
2399 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2400 for (i = 0; i < num_screens; i++)
2401 TAILQ_FOREACH(r, &screens[i].rl, entry)
2402 for (x = 0; x < workspace_limit; x++) {
2411 unfocus_win(struct ws_win *win)
2413 xcb_window_t none = XCB_WINDOW_NONE;
2415 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2419 if (win->ws == NULL)
2422 if (validate_ws(win->ws))
2423 return; /* XXX this gets hit with thunderbird, needs fixing */
2425 if (win->ws->r == NULL)
2428 if (validate_win(win)) {
2433 if (win->ws->focus == win) {
2434 win->ws->focus = NULL;
2435 win->ws->focus_prev = win;
2438 if (validate_win(win->ws->focus)) {
2439 kill_refs(win->ws->focus);
2440 win->ws->focus = NULL;
2442 if (validate_win(win->ws->focus_prev)) {
2443 kill_refs(win->ws->focus_prev);
2444 win->ws->focus_prev = NULL;
2447 grabbuttons(win, 0);
2448 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2449 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2450 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2451 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2458 int i, j, num_screens;
2460 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2462 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2463 for (i = 0; i < num_screens; i++)
2464 for (j = 0; j < workspace_limit; j++)
2465 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2470 focus_win(struct ws_win *win)
2472 struct ws_win *cfw = NULL;
2473 xcb_get_input_focus_reply_t *r;
2474 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2476 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2480 if (win->ws == NULL)
2483 if (validate_ws(win->ws))
2484 return; /* XXX this gets hit with thunderbird, needs fixing */
2486 if (validate_win(win)) {
2491 if (validate_win(win)) {
2496 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2498 cur_focus = r->focus;
2501 if ((cfw = find_window(cur_focus)) != NULL)
2504 /* use larger hammer since the window was killed somehow */
2505 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2506 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2507 xcb_change_window_attributes(conn, cfw->id,
2508 XCB_CW_BORDER_PIXEL,
2509 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2512 win->ws->focus = win;
2514 if (win->ws->r != NULL) {
2516 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2517 win->id, XCB_CURRENT_TIME);
2518 grabbuttons(win, 1);
2519 xcb_change_window_attributes(conn, win->id,
2520 XCB_CW_BORDER_PIXEL,
2521 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2522 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2523 win->ws->always_raise)
2524 map_window_raised(win->id);
2526 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2527 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2535 event_drain(uint8_t rt)
2537 xcb_generic_event_t *evt;
2540 while ((evt = xcb_poll_for_event(conn))) {
2541 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2549 switchws(struct swm_region *r, union arg *args)
2551 int wsid = args->id, unmap_old = 0;
2552 struct swm_region *this_r, *other_r;
2554 struct workspace *new_ws, *old_ws;
2560 if (wsid >= workspace_limit)
2564 old_ws = this_r->ws;
2565 new_ws = &this_r->s->ws[wsid];
2567 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2568 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2570 if (new_ws == NULL || old_ws == NULL)
2572 if (new_ws == old_ws)
2575 other_r = new_ws->r;
2576 if (other_r == NULL) {
2577 /* the other workspace is hidden, hide this one */
2581 /* the other ws is visible in another region, exchange them */
2582 other_r->ws_prior = new_ws;
2583 other_r->ws = old_ws;
2584 old_ws->r = other_r;
2586 this_r->ws_prior = old_ws;
2587 this_r->ws = new_ws;
2590 /* this is needed so that we can click on a window after a restart */
2594 a.id = SWM_ARG_ID_FOCUSCUR;
2595 focus(new_ws->r, &a);
2597 /* unmap old windows */
2599 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2602 if (focus_mode == SWM_FOCUS_DEFAULT)
2603 event_drain(XCB_ENTER_NOTIFY);
2609 cyclews(struct swm_region *r, union arg *args)
2612 struct swm_screen *s = r->s;
2615 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2616 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2621 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2624 case SWM_ARG_ID_CYCLEWS_UP:
2625 if (a.id < workspace_limit - 1)
2630 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2633 case SWM_ARG_ID_CYCLEWS_DOWN:
2637 a.id = workspace_limit - 1;
2644 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2646 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2650 } while (a.id != r->ws->idx);
2654 priorws(struct swm_region *r, union arg *args)
2658 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2659 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2661 if (r->ws_prior == NULL)
2664 a.id = r->ws_prior->idx;
2669 cyclescr(struct swm_region *r, union arg *args)
2671 struct swm_region *rr = NULL;
2673 int i, x, y, num_screens;
2675 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2676 /* do nothing if we don't have more than one screen */
2677 if (!(num_screens > 1 || outputs > 1))
2682 case SWM_ARG_ID_CYCLESC_UP:
2683 rr = TAILQ_NEXT(r, entry);
2685 rr = TAILQ_FIRST(&screens[i].rl);
2687 case SWM_ARG_ID_CYCLESC_DOWN:
2688 rr = TAILQ_PREV(r, swm_region_list, entry);
2690 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2698 /* move mouse to region */
2700 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2701 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2704 a.id = SWM_ARG_ID_FOCUSCUR;
2707 if (rr->ws->focus) {
2708 /* move to focus window */
2709 x = X(rr->ws->focus) + 1;
2710 y = Y(rr->ws->focus) + 1;
2711 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2717 sort_windows(struct ws_win_list *wl)
2719 struct ws_win *win, *parent, *nxt;
2724 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2725 nxt = TAILQ_NEXT(win, entry);
2726 if (win->transient) {
2727 parent = find_window(win->transient);
2728 if (parent == NULL) {
2729 warnx("not possible bug");
2732 TAILQ_REMOVE(wl, win, entry);
2733 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2740 swapwin(struct swm_region *r, union arg *args)
2742 struct ws_win *target, *source;
2743 struct ws_win *cur_focus;
2744 struct ws_win_list *wl;
2747 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2748 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2750 cur_focus = r->ws->focus;
2751 if (cur_focus == NULL)
2755 wl = &source->ws->winlist;
2758 case SWM_ARG_ID_SWAPPREV:
2759 if (source->transient)
2760 source = find_window(source->transient);
2761 target = TAILQ_PREV(source, ws_win_list, entry);
2762 if (target && target->transient)
2763 target = find_window(target->transient);
2764 TAILQ_REMOVE(wl, source, entry);
2766 TAILQ_INSERT_TAIL(wl, source, entry);
2768 TAILQ_INSERT_BEFORE(target, source, entry);
2770 case SWM_ARG_ID_SWAPNEXT:
2771 target = TAILQ_NEXT(source, entry);
2772 /* move the parent and let the sort handle the move */
2773 if (source->transient)
2774 source = find_window(source->transient);
2775 TAILQ_REMOVE(wl, source, entry);
2777 TAILQ_INSERT_HEAD(wl, source, entry);
2779 TAILQ_INSERT_AFTER(wl, target, source, entry);
2781 case SWM_ARG_ID_SWAPMAIN:
2782 target = TAILQ_FIRST(wl);
2783 if (target == source) {
2784 if (source->ws->focus_prev != NULL &&
2785 source->ws->focus_prev != target)
2786 source = source->ws->focus_prev;
2790 if (target == NULL || source == NULL)
2792 source->ws->focus_prev = target;
2793 TAILQ_REMOVE(wl, target, entry);
2794 TAILQ_INSERT_BEFORE(source, target, entry);
2795 TAILQ_REMOVE(wl, source, entry);
2796 TAILQ_INSERT_HEAD(wl, source, entry);
2798 case SWM_ARG_ID_MOVELAST:
2799 TAILQ_REMOVE(wl, source, entry);
2800 TAILQ_INSERT_TAIL(wl, source, entry);
2803 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2815 focus_prev(struct ws_win *win)
2817 struct ws_win *winfocus = NULL;
2818 struct ws_win *cur_focus = NULL;
2819 struct ws_win_list *wl = NULL;
2820 struct workspace *ws = NULL;
2822 if (!(win && win->ws))
2827 cur_focus = ws->focus;
2829 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2830 WINID(win), WINID(cur_focus));
2832 /* pickle, just focus on whatever */
2833 if (cur_focus == NULL) {
2834 /* use prev_focus if valid */
2835 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2836 find_window(WINID(ws->focus_prev)))
2837 winfocus = ws->focus_prev;
2841 /* if transient focus on parent */
2842 if (cur_focus->transient) {
2843 winfocus = find_window(cur_focus->transient);
2847 /* if in max_stack try harder */
2848 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2849 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2850 if (cur_focus != ws->focus_prev)
2851 winfocus = ws->focus_prev;
2853 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2858 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2860 if (winfocus == NULL || winfocus == win) {
2861 switch (focus_close) {
2862 case SWM_STACK_BOTTOM:
2863 winfocus = TAILQ_FIRST(wl);
2866 winfocus = TAILQ_LAST(wl, ws_win_list);
2868 case SWM_STACK_ABOVE:
2869 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2870 if (focus_close_wrap)
2871 winfocus = TAILQ_FIRST(wl);
2873 winfocus = TAILQ_PREV(cur_focus,
2874 ws_win_list, entry);
2877 case SWM_STACK_BELOW:
2878 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2880 if (focus_close_wrap)
2881 winfocus = TAILQ_LAST(wl, ws_win_list);
2883 winfocus = TAILQ_NEXT(cur_focus, entry);
2889 if (winfocus == NULL) {
2890 if (focus_default == SWM_STACK_TOP)
2891 winfocus = TAILQ_LAST(wl, ws_win_list);
2893 winfocus = TAILQ_FIRST(wl);
2897 focus_magic(winfocus);
2901 focus(struct swm_region *r, union arg *args)
2903 struct ws_win *winfocus = NULL, *head;
2904 struct ws_win *cur_focus = NULL;
2905 struct ws_win_list *wl = NULL;
2906 struct workspace *ws = NULL;
2912 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2914 /* treat FOCUS_CUR special */
2915 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2916 if (r->ws->focus && r->ws->focus->iconic == 0)
2917 winfocus = r->ws->focus;
2918 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2919 winfocus = r->ws->focus_prev;
2921 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2922 if (winfocus->iconic == 0)
2925 focus_magic(winfocus);
2929 if ((cur_focus = r->ws->focus) == NULL)
2933 if (TAILQ_EMPTY(wl))
2935 /* make sure there is at least one uniconified window */
2937 TAILQ_FOREACH(winfocus, wl, entry)
2938 if (winfocus->iconic == 0) {
2946 case SWM_ARG_ID_FOCUSPREV:
2947 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2949 head = TAILQ_LAST(wl, ws_win_list);
2951 if (WINID(winfocus) == cur_focus->transient) {
2952 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2954 head = TAILQ_LAST(wl, ws_win_list);
2959 if (winfocus && winfocus->iconic) {
2960 while (winfocus != cur_focus) {
2961 if (winfocus == NULL)
2962 winfocus = TAILQ_LAST(wl, ws_win_list);
2963 if (winfocus->iconic == 0)
2965 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2971 case SWM_ARG_ID_FOCUSNEXT:
2972 head = TAILQ_NEXT(cur_focus, entry);
2974 head = TAILQ_FIRST(wl);
2978 if (winfocus && winfocus->iconic) {
2979 while (winfocus != cur_focus) {
2980 if (winfocus == NULL)
2981 winfocus = TAILQ_FIRST(wl);
2982 if (winfocus->iconic == 0)
2984 winfocus = TAILQ_NEXT(winfocus, entry);
2989 case SWM_ARG_ID_FOCUSMAIN:
2990 winfocus = TAILQ_FIRST(wl);
2991 if (winfocus == cur_focus)
2992 winfocus = cur_focus->ws->focus_prev;
2999 focus_magic(winfocus);
3005 cycle_layout(struct swm_region *r, union arg *args)
3007 struct workspace *ws = r->ws;
3010 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3013 if (ws->cur_layout->l_stack == NULL)
3014 ws->cur_layout = &layouts[0];
3019 if (focus_mode == SWM_FOCUS_DEFAULT)
3020 event_drain(XCB_ENTER_NOTIFY);
3022 a.id = SWM_ARG_ID_FOCUSCUR;
3027 stack_config(struct swm_region *r, union arg *args)
3029 struct workspace *ws = r->ws;
3031 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3034 if (ws->cur_layout->l_config != NULL)
3035 ws->cur_layout->l_config(ws, args->id);
3037 if (args->id != SWM_ARG_ID_STACKINIT)
3044 struct swm_geometry g;
3045 struct swm_region *r;
3051 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3053 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3054 for (i = 0; i < num_screens; i++) {
3058 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3059 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3060 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3062 /* start with screen geometry, adjust for bar */
3064 g.w -= 2 * border_width;
3065 g.h -= 2 * border_width;
3071 r->ws->cur_layout->l_stack(r->ws, &g);
3072 r->ws->cur_layout->l_string(r->ws);
3073 /* save r so we can track region changes */
3080 if (focus_mode == SWM_FOCUS_DEFAULT)
3081 event_drain(XCB_ENTER_NOTIFY);
3083 DNPRINTF(SWM_D_STACK, "stack: end\n");
3087 store_float_geom(struct ws_win *win, struct swm_region *r)
3089 /* retain window geom and region geom */
3090 win->g_float = win->g;
3091 win->g_float.x -= X(r);
3092 win->g_float.y -= Y(r);
3093 win->g_floatvalid = 1;
3094 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3095 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3096 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3097 win->g_float.w, win->g_float.h);
3101 stack_floater(struct ws_win *win, struct swm_region *r)
3106 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3109 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3110 * geom on ws switches or return from max mode
3112 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3113 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3114 /* refloat at last floating relative position */
3115 win->g = win->g_float;
3120 win->floatmaxed = 0;
3123 * if set to fullscreen mode, configure window to maximum size.
3125 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3126 if (!win->g_floatvalid)
3127 store_float_geom(win, win->ws->r);
3129 win->g = win->ws->r->g;
3133 * remove border on fullscreen floater when in fullscreen mode or when
3134 * the quirk is present.
3136 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3137 ((win->quirks & SWM_Q_FULLSCREEN) &&
3138 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3139 if (win->bordered) {
3141 X(win) += border_width;
3142 Y(win) += border_width;
3144 } else if (!win->bordered) {
3146 X(win) -= border_width;
3147 Y(win) -= border_width;
3150 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3151 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3152 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3157 * floaters and transients are auto-centred unless moved
3160 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3161 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3164 /* keep window within region bounds */
3165 constrain_window(win, r, 0);
3171 * Send keystrokes to terminal to decrease/increase the font size as the
3172 * window size changes.
3175 adjust_font(struct ws_win *win)
3177 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3178 win->floating || win->transient)
3181 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3182 WIDTH(win) / win->sh.width_inc < term_width &&
3183 win->font_steps < SWM_MAX_FONT_STEPS) {
3184 win->font_size_boundary[win->font_steps] =
3185 (win->sh.width_inc * term_width) + win->sh.base_width;
3188 win->last_inc = win->sh.width_inc;
3189 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3190 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3191 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3194 win->last_inc = win->sh.width_inc;
3195 fake_keypress(win, XK_KP_Add, ShiftMask);
3199 #define SWAPXY(g) do { \
3201 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3202 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3205 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3207 struct swm_geometry win_g, r_g = *g;
3208 struct ws_win *win, *fs_win = NULL;
3209 int i, j, s, stacks;
3210 int w_inc = 1, h_inc, w_base = 1, h_base;
3211 int hrh, extra = 0, h_slice, last_h = 0;
3212 int split, colno, winno, mwin, msize, mscale;
3213 int remain, missing, v_slice, reconfigure;
3215 xcb_get_window_attributes_reply_t *war;
3217 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3218 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3220 winno = count_win(ws, 0);
3221 if (winno == 0 && count_win(ws, 1) == 0)
3224 TAILQ_FOREACH(win, &ws->winlist, entry)
3225 if (win->transient == 0 && win->floating == 0
3226 && win->iconic == 0)
3233 w_inc = win->sh.width_inc;
3234 w_base = win->sh.base_width;
3235 mwin = ws->l_state.horizontal_mwin;
3236 mscale = ws->l_state.horizontal_msize;
3237 stacks = ws->l_state.horizontal_stacks;
3240 w_inc = win->sh.height_inc;
3241 w_base = win->sh.base_height;
3242 mwin = ws->l_state.vertical_mwin;
3243 mscale = ws->l_state.vertical_msize;
3244 stacks = ws->l_state.vertical_stacks;
3248 if (stacks > winno - mwin)
3249 stacks = winno - mwin;
3253 h_slice = r_g.h / SWM_H_SLICE;
3254 if (mwin && winno > mwin) {
3255 v_slice = r_g.w / SWM_V_SLICE;
3259 win_g.w = v_slice * mscale;
3261 if (w_inc > 1 && w_inc < v_slice) {
3262 /* adjust for window's requested size increment */
3263 remain = (win_g.w - w_base) % w_inc;
3269 win_g.x += r_g.w - msize;
3272 colno = split = winno / stacks;
3273 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3274 2 * border_width) / stacks);
3276 hrh = r_g.h / colno;
3277 extra = r_g.h - (colno * hrh);
3278 win_g.h = hrh - 2 * border_width;
3280 /* stack all the tiled windows */
3281 i = j = 0, s = stacks;
3282 TAILQ_FOREACH(win, &ws->winlist, entry) {
3283 if (win->transient != 0 || win->floating != 0)
3285 if (win->iconic != 0)
3288 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3293 if (split && i == split) {
3294 colno = (winno - mwin) / stacks;
3295 if (s <= (winno - mwin) % stacks)
3297 split = split + colno;
3298 hrh = (r_g.h / colno);
3299 extra = r_g.h - (colno * hrh);
3303 win_g.x += win_g.w + 2 * border_width;
3304 win_g.w = (r_g.w - msize -
3305 (stacks * 2 * border_width)) / stacks;
3307 win_g.w += (r_g.w - msize -
3308 (stacks * 2 * border_width)) % stacks;
3312 win_g.h = hrh - 2 * border_width;
3314 h_inc = win->sh.width_inc;
3315 h_base = win->sh.base_width;
3317 h_inc = win->sh.height_inc;
3318 h_base = win->sh.base_height;
3320 if (j == colno - 1) {
3321 win_g.h = hrh + extra;
3322 } else if (h_inc > 1 && h_inc < h_slice) {
3323 /* adjust for window's requested size increment */
3324 remain = (win_g.h - h_base) % h_inc;
3325 missing = h_inc - remain;
3327 if (missing <= extra || j == 0) {
3339 win_g.y += last_h + 2 * border_width;
3341 if (disable_border && bar_enabled == 0 && winno == 1){
3343 win_g.w += 2 * border_width;
3344 win_g.h += 2 * border_width;
3349 if (X(win) != win_g.y || Y(win) != win_g.x ||
3350 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3354 WIDTH(win) = win_g.h;
3355 HEIGHT(win) = win_g.w;
3358 if (X(win) != win_g.x || Y(win) != win_g.y ||
3359 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3363 WIDTH(win) = win_g.w;
3364 HEIGHT(win) = win_g.h;
3368 if (bordered != win->bordered) {
3370 win->bordered = bordered;
3378 war = xcb_get_window_attributes_reply(conn,
3379 xcb_get_window_attributes(conn, win->id),
3382 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3383 map_window_raised(win->id);
3393 /* now, stack all the floaters and transients */
3394 TAILQ_FOREACH(win, &ws->winlist, entry) {
3395 if (win->transient == 0 && win->floating == 0)
3397 if (win->iconic == 1)
3399 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3404 stack_floater(win, ws->r);
3405 map_window_raised(win->id);
3409 stack_floater(fs_win, ws->r);
3410 map_window_raised(fs_win->id);
3415 vertical_config(struct workspace *ws, int id)
3417 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3421 case SWM_ARG_ID_STACKRESET:
3422 case SWM_ARG_ID_STACKINIT:
3423 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3424 ws->l_state.vertical_mwin = 1;
3425 ws->l_state.vertical_stacks = 1;
3427 case SWM_ARG_ID_MASTERSHRINK:
3428 if (ws->l_state.vertical_msize > 1)
3429 ws->l_state.vertical_msize--;
3431 case SWM_ARG_ID_MASTERGROW:
3432 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3433 ws->l_state.vertical_msize++;
3435 case SWM_ARG_ID_MASTERADD:
3436 ws->l_state.vertical_mwin++;
3438 case SWM_ARG_ID_MASTERDEL:
3439 if (ws->l_state.vertical_mwin > 0)
3440 ws->l_state.vertical_mwin--;
3442 case SWM_ARG_ID_STACKINC:
3443 ws->l_state.vertical_stacks++;
3445 case SWM_ARG_ID_STACKDEC:
3446 if (ws->l_state.vertical_stacks > 1)
3447 ws->l_state.vertical_stacks--;
3449 case SWM_ARG_ID_FLIPLAYOUT:
3450 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3458 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3460 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3462 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3466 horizontal_config(struct workspace *ws, int id)
3468 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3471 case SWM_ARG_ID_STACKRESET:
3472 case SWM_ARG_ID_STACKINIT:
3473 ws->l_state.horizontal_mwin = 1;
3474 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3475 ws->l_state.horizontal_stacks = 1;
3477 case SWM_ARG_ID_MASTERSHRINK:
3478 if (ws->l_state.horizontal_msize > 1)
3479 ws->l_state.horizontal_msize--;
3481 case SWM_ARG_ID_MASTERGROW:
3482 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3483 ws->l_state.horizontal_msize++;
3485 case SWM_ARG_ID_MASTERADD:
3486 ws->l_state.horizontal_mwin++;
3488 case SWM_ARG_ID_MASTERDEL:
3489 if (ws->l_state.horizontal_mwin > 0)
3490 ws->l_state.horizontal_mwin--;
3492 case SWM_ARG_ID_STACKINC:
3493 ws->l_state.horizontal_stacks++;
3495 case SWM_ARG_ID_STACKDEC:
3496 if (ws->l_state.horizontal_stacks > 1)
3497 ws->l_state.horizontal_stacks--;
3499 case SWM_ARG_ID_FLIPLAYOUT:
3500 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3508 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3510 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3512 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3515 /* fullscreen view */
3517 max_stack(struct workspace *ws, struct swm_geometry *g)
3519 struct swm_geometry gg = *g;
3520 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3521 int winno, num_screens;
3523 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3528 winno = count_win(ws, 0);
3529 if (winno == 0 && count_win(ws, 1) == 0)
3532 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3533 TAILQ_FOREACH(win, &ws->winlist, entry) {
3534 if (win->transient) {
3536 parent = find_window(win->transient);
3540 if (win->floating && win->floatmaxed == 0 ) {
3542 * retain geometry for retrieval on exit from
3545 store_float_geom(win, ws->r);
3546 win->floatmaxed = 1;
3549 /* only reconfigure if necessary */
3550 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3551 HEIGHT(win) != gg.h) {
3557 WIDTH(win) += 2 * border_width;
3558 HEIGHT(win) += 2 * border_width;
3563 /* unmap only if we don't have multi screen */
3564 if (win != ws->focus)
3565 if (!(num_screens > 1 || outputs > 1))
3569 /* put the last transient on top */
3572 map_window_raised(parent->id);
3573 stack_floater(wintrans, ws->r);
3574 focus_magic(wintrans);
3579 send_to_ws(struct swm_region *r, union arg *args)
3581 int wsid = args->id;
3582 struct ws_win *win = NULL, *parent;
3583 struct workspace *ws, *nws;
3584 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3585 char ws_idx_str[SWM_PROPLEN];
3588 if (wsid >= workspace_limit)
3591 if (r && r->ws && r->ws->focus)
3597 if (win->ws->idx == wsid)
3600 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3603 nws = &win->s->ws[wsid];
3605 a.id = SWM_ARG_ID_FOCUSPREV;
3607 if (win->transient) {
3608 parent = find_window(win->transient);
3610 unmap_window(parent);
3611 TAILQ_REMOVE(&ws->winlist, parent, entry);
3612 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3617 TAILQ_REMOVE(&ws->winlist, win, entry);
3618 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3619 if (TAILQ_EMPTY(&ws->winlist))
3620 r->ws->focus = NULL;
3623 /* Try to update the window's workspace property */
3624 ws_idx_atom = get_atom_from_string("_SWM_WS");
3626 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3628 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3630 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3631 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3642 pressbutton(struct swm_region *r, union arg *args)
3644 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3645 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3646 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3647 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3651 raise_toggle(struct swm_region *r, union arg *args)
3653 if (r == NULL || r->ws == NULL)
3656 r->ws->always_raise = !r->ws->always_raise;
3658 /* bring floaters back to top */
3659 if (r->ws->always_raise == 0)
3666 iconify(struct swm_region *r, union arg *args)
3670 if (r->ws->focus == NULL)
3672 unmap_window(r->ws->focus);
3673 update_iconic(r->ws->focus, 1);
3676 r->ws->focus = NULL;
3677 a.id = SWM_ARG_ID_FOCUSCUR;
3684 get_win_name(xcb_window_t win)
3687 xcb_get_property_cookie_t c;
3688 xcb_icccm_get_text_property_reply_t r;
3690 c = xcb_icccm_get_wm_name(conn, win);
3691 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3692 if (r.name_len > 0) {
3693 name = malloc(r.name_len + 1);
3695 xcb_icccm_get_text_property_reply_wipe(&r);
3698 memcpy(name, r.name, r.name_len);
3699 name[r.name_len] = '\0';
3701 xcb_icccm_get_text_property_reply_wipe(&r);
3708 uniconify(struct swm_region *r, union arg *args)
3715 DNPRINTF(SWM_D_MISC, "uniconify\n");
3717 if (r == NULL || r->ws == NULL)
3720 /* make sure we have anything to uniconify */
3721 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3722 if (win->ws == NULL)
3723 continue; /* should never happen */
3724 if (win->iconic == 0)
3732 search_resp_action = SWM_SEARCH_UNICONIFY;
3734 spawn_select(r, args, "search", &searchpid);
3736 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3739 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3740 if (win->ws == NULL)
3741 continue; /* should never happen */
3742 if (win->iconic == 0)
3745 name = get_win_name(win->id);
3748 fprintf(lfile, "%s.%u\n", name, win->id);
3758 name_workspace(struct swm_region *r, union arg *args)
3762 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3768 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3770 spawn_select(r, args, "name_workspace", &searchpid);
3772 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3775 fprintf(lfile, "%s", "");
3780 search_workspace(struct swm_region *r, union arg *args)
3783 struct workspace *ws;
3786 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3792 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3794 spawn_select(r, args, "search", &searchpid);
3796 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3799 for (i = 0; i < workspace_limit; i++) {
3803 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3804 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3811 search_win_cleanup(void)
3813 struct search_window *sw = NULL;
3815 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3816 xcb_destroy_window(conn, sw->indicator);
3817 xcb_free_gc(conn, sw->gc);
3818 TAILQ_REMOVE(&search_wl, sw, entry);
3824 search_win(struct swm_region *r, union arg *args)
3826 struct ws_win *win = NULL;
3827 struct search_window *sw = NULL;
3829 uint32_t gcv[3], wa[2];
3834 XRectangle ibox, lbox;
3836 DNPRINTF(SWM_D_MISC, "search_win\n");
3839 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3841 spawn_select(r, args, "search", &searchpid);
3843 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3846 TAILQ_INIT(&search_wl);
3849 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3850 if (win->iconic == 1)
3853 sw = calloc(1, sizeof(struct search_window));
3855 warn("search_win: calloc");
3857 search_win_cleanup();
3863 snprintf(s, sizeof s, "%d", i);
3866 /* FIXME fix calculations */
3868 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3871 w = xcb_generate_id(conn);
3872 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3873 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3875 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3876 lbox.width + 4, bar_fs_extents->max_logical_extent.height,
3877 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3878 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3881 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3882 22, 20, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
3883 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
3884 XCB_CW_BORDER_PIXEL, wa);
3888 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3890 sw->gc = xcb_generate_id(conn);
3891 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3892 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3894 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3895 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3896 map_window_raised(w);
3899 xcb_image_text_8(conn, len, w, sw->gc, 2,
3900 (bar_fs_extents->max_logical_extent.height -
3901 lbox.height) / 2 - lbox.y, s);
3904 xcb_image_text_8(conn, len, w, sw->gc, 6, 14, s);
3907 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3909 fprintf(lfile, "%d\n", i);
3919 search_resp_uniconify(char *resp, unsigned long len)
3925 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3927 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3928 if (win->iconic == 0)
3930 name = get_win_name(win->id);
3933 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3938 if (strncmp(s, resp, len) == 0) {
3939 /* XXX this should be a callback to generalize */
3940 update_iconic(win, 0);
3949 search_resp_name_workspace(char *resp, unsigned long len)
3951 struct workspace *ws;
3953 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3955 if (search_r->ws == NULL)
3960 free(search_r->ws->name);
3961 search_r->ws->name = NULL;
3965 ws->name = strdup(resp);
3966 if (ws->name == NULL) {
3967 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3968 "strdup: %s", strerror(errno));
3975 search_resp_search_workspace(char *resp, unsigned long len)
3982 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3986 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3993 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3995 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4002 switchws(search_r, &a);
4006 search_resp_search_window(char *resp, unsigned long len)
4011 struct search_window *sw;
4013 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4017 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4022 idx = strtonum(s, 1, INT_MAX, &errstr);
4024 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4031 TAILQ_FOREACH(sw, &search_wl, entry)
4032 if (idx == sw->idx) {
4038 #define MAX_RESP_LEN 1024
4041 search_do_resp(void)
4047 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4052 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4053 warn("search: calloc");
4057 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4059 warn("search: read error");
4062 resp[rbytes] = '\0';
4065 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4066 * newline, so work around that by sanitizing the resp now.
4068 resp[strcspn(resp, "\n")] = '\0';
4071 switch (search_resp_action) {
4072 case SWM_SEARCH_UNICONIFY:
4073 search_resp_uniconify(resp, len);
4075 case SWM_SEARCH_NAME_WORKSPACE:
4076 search_resp_name_workspace(resp, len);
4078 case SWM_SEARCH_SEARCH_WORKSPACE:
4079 search_resp_search_workspace(resp, len);
4081 case SWM_SEARCH_SEARCH_WINDOW:
4082 search_resp_search_window(resp, len);
4087 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4088 search_win_cleanup();
4090 search_resp_action = SWM_SEARCH_NONE;
4091 close(select_resp_pipe[0]);
4096 wkill(struct swm_region *r, union arg *args)
4098 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4100 if (r->ws->focus == NULL)
4103 if (args->id == SWM_ARG_ID_KILLWINDOW)
4104 xcb_kill_client(conn, r->ws->focus->id);
4106 if (r->ws->focus->can_delete)
4107 client_msg(r->ws->focus, a_delete);
4114 floating_toggle_win(struct ws_win *win)
4116 struct swm_region *r;
4126 /* reject floating toggles in max stack mode */
4127 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4130 if (win->floating) {
4131 if (!win->floatmaxed) {
4132 /* retain position for refloat */
4133 store_float_geom(win, r);
4137 if (win->g_floatvalid) {
4138 /* refloat at last floating relative position */
4139 X(win) = win->g_float.x + X(r);
4140 Y(win) = win->g_float.y + Y(r);
4141 WIDTH(win) = win->g_float.w;
4142 HEIGHT(win) = win->g_float.h;
4147 ewmh_update_actions(win);
4153 floating_toggle(struct swm_region *r, union arg *args)
4155 struct ws_win *win = r->ws->focus;
4161 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4162 _NET_WM_STATE_TOGGLE);
4166 if (win == win->ws->focus) {
4167 a.id = SWM_ARG_ID_FOCUSCUR;
4168 focus(win->ws->r, &a);
4172 event_drain(XCB_ENTER_NOTIFY);
4176 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4178 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4180 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4182 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4185 if (X(win) + BORDER(win) < X(r)) {
4187 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4189 X(win) = X(r) - BORDER(win);
4192 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4194 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4196 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4199 if (Y(win) + BORDER(win) < Y(r)) {
4201 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4203 Y(win) = Y(r) - BORDER(win);
4209 if (HEIGHT(win) < 1)
4215 update_window(struct ws_win *win)
4220 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4221 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4222 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4226 wc[3] = HEIGHT(win);
4227 wc[4] = BORDER(win);
4229 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4230 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4231 wc[3], YESNO(win->bordered));
4233 xcb_configure_window(conn, win->id, mask, wc);
4236 #define SWM_RESIZE_STEPS (50)
4239 resize(struct ws_win *win, union arg *args)
4241 xcb_timestamp_t timestamp = 0;
4242 struct swm_region *r = NULL;
4243 int resize_step = 0;
4244 struct swm_geometry g;
4245 int top = 0, left = 0, resizing;
4247 unsigned int shape; /* cursor style */
4248 xcb_cursor_t cursor;
4249 xcb_font_t cursor_font;
4250 xcb_grab_pointer_cookie_t gpc;
4251 xcb_grab_pointer_reply_t *gpr;
4252 xcb_query_pointer_reply_t *xpr;
4253 xcb_generic_event_t *evt;
4254 xcb_motion_notify_event_t *mne;
4260 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4263 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4264 "transient: 0x%x\n", win->id, YESNO(win->floating),
4267 if (!(win->transient != 0 || win->floating != 0))
4270 /* reject resizes in max mode for floaters (transient ok) */
4271 if (win->floatmaxed)
4275 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4281 case SWM_ARG_ID_WIDTHSHRINK:
4282 WIDTH(win) -= SWM_RESIZE_STEPS;
4285 case SWM_ARG_ID_WIDTHGROW:
4286 WIDTH(win) += SWM_RESIZE_STEPS;
4289 case SWM_ARG_ID_HEIGHTSHRINK:
4290 HEIGHT(win) -= SWM_RESIZE_STEPS;
4293 case SWM_ARG_ID_HEIGHTGROW:
4294 HEIGHT(win) += SWM_RESIZE_STEPS;
4301 constrain_window(win, r, 1);
4303 store_float_geom(win,r);
4307 /* get cursor offset from window root */
4308 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4315 if (xpr->win_x < WIDTH(win) / 2)
4318 if (xpr->win_y < HEIGHT(win) / 2)
4321 if (args->id == SWM_ARG_ID_CENTER)
4324 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4326 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4328 cursor_font = xcb_generate_id(conn);
4329 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4331 cursor = xcb_generate_id(conn);
4332 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4333 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4335 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4336 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4338 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4340 xcb_free_cursor(conn, cursor);
4341 xcb_close_font(conn, cursor_font);
4348 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4349 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4350 case XCB_BUTTON_RELEASE:
4351 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4354 case XCB_MOTION_NOTIFY:
4355 mne = (xcb_motion_notify_event_t *)evt;
4356 /* cursor offset/delta from start of the operation */
4357 dx = mne->root_x - xpr->root_x;
4358 dy = mne->root_y - xpr->root_y;
4364 if (args->id == SWM_ARG_ID_CENTER) {
4365 if (g.h / 2 + dy < 1)
4369 HEIGHT(win) = g.h + 2 * dy;
4377 HEIGHT(win) = g.h + dy;
4384 if (args->id == SWM_ARG_ID_CENTER) {
4385 if (g.w / 2 + dx < 1)
4389 WIDTH(win) = g.w + 2 * dx;
4397 WIDTH(win) = g.w + dx;
4400 constrain_window(win, r, 1);
4402 /* not free, don't sync more than 120 times / second */
4403 if ((mne->time - timestamp) > (1000 / 120) ) {
4404 timestamp = mne->time;
4419 store_float_geom(win,r);
4421 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4422 xcb_free_cursor(conn, cursor);
4423 xcb_close_font(conn, cursor_font);
4426 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4430 resize_step(struct swm_region *r, union arg *args)
4432 struct ws_win *win = NULL;
4434 if (r && r->ws && r->ws->focus)
4442 #define SWM_MOVE_STEPS (50)
4445 move(struct ws_win *win, union arg *args)
4447 xcb_timestamp_t timestamp = 0;
4448 int move_step = 0, moving;
4449 struct swm_region *r = NULL;
4450 xcb_font_t cursor_font;
4451 xcb_cursor_t cursor;
4452 xcb_grab_pointer_cookie_t gpc;
4453 xcb_grab_pointer_reply_t *gpr;
4454 xcb_query_pointer_reply_t *qpr;
4455 xcb_generic_event_t *evt;
4456 xcb_motion_notify_event_t *mne;
4462 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4465 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4466 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4468 /* in max_stack mode should only move transients */
4469 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4473 if (win->floating == 0 && !win->transient) {
4474 store_float_geom(win, r);
4475 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4478 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4485 case SWM_ARG_ID_MOVELEFT:
4486 X(win) -= (SWM_MOVE_STEPS - border_width);
4489 case SWM_ARG_ID_MOVERIGHT:
4490 X(win) += (SWM_MOVE_STEPS - border_width);
4493 case SWM_ARG_ID_MOVEUP:
4494 Y(win) -= (SWM_MOVE_STEPS - border_width);
4497 case SWM_ARG_ID_MOVEDOWN:
4498 Y(win) += (SWM_MOVE_STEPS - border_width);
4505 constrain_window(win, r, 0);
4507 store_float_geom(win, r);
4511 cursor_font = xcb_generate_id(conn);
4512 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4514 cursor = xcb_generate_id(conn);
4515 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4516 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4518 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4519 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4520 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4521 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4523 xcb_free_cursor(conn, cursor);
4524 xcb_close_font(conn, cursor_font);
4528 /* get cursor offset from window root */
4529 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4532 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4533 xcb_free_cursor(conn, cursor);
4534 xcb_close_font(conn, cursor_font);
4540 while ((evt = xcb_wait_for_event(conn)) && moving) {
4541 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4542 case XCB_BUTTON_RELEASE:
4543 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4546 case XCB_MOTION_NOTIFY:
4547 mne = (xcb_motion_notify_event_t *)evt;
4548 X(win) = mne->root_x - qpr->win_x - border_width;
4549 Y(win) = mne->root_y - qpr->win_y - border_width;
4551 constrain_window(win, r, 0);
4553 /* not free, don't sync more than 120 times / second */
4554 if ((mne->time - timestamp) > (1000 / 120) ) {
4555 timestamp = mne->time;
4570 store_float_geom(win, r);
4572 xcb_free_cursor(conn, cursor);
4573 xcb_close_font(conn, cursor_font);
4574 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4575 DNPRINTF(SWM_D_EVENT, "move: done\n");
4579 move_step(struct swm_region *r, union arg *args)
4581 struct ws_win *win = NULL;
4583 if (r && r->ws && r->ws->focus)
4588 if (!(win->transient != 0 || win->floating != 0))
4595 /* user/key callable function IDs */
4645 KF_SEARCH_WORKSPACE,
4686 KF_DUMPWINS, /* MUST BE LAST */
4690 /* key definitions */
4692 char name[SWM_FUNCNAME_LEN];
4693 void (*func)(struct swm_region *r, union arg *);
4695 } keyfuncs[KF_INVALID + 1] = {
4696 /* name function argument */
4697 { "bar_toggle", bar_toggle, {0} },
4698 { "button2", pressbutton, {2} },
4699 { "cycle_layout", cycle_layout, {0} },
4700 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4701 { "float_toggle", floating_toggle,{0} },
4702 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4703 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4704 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4705 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4706 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4707 { "iconify", iconify, {0} },
4708 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4709 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4710 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4711 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4712 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4713 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4714 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4715 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4716 { "mvws_1", send_to_ws, {.id = 0} },
4717 { "mvws_2", send_to_ws, {.id = 1} },
4718 { "mvws_3", send_to_ws, {.id = 2} },
4719 { "mvws_4", send_to_ws, {.id = 3} },
4720 { "mvws_5", send_to_ws, {.id = 4} },
4721 { "mvws_6", send_to_ws, {.id = 5} },
4722 { "mvws_7", send_to_ws, {.id = 6} },
4723 { "mvws_8", send_to_ws, {.id = 7} },
4724 { "mvws_9", send_to_ws, {.id = 8} },
4725 { "mvws_10", send_to_ws, {.id = 9} },
4726 { "mvws_11", send_to_ws, {.id = 10} },
4727 { "mvws_12", send_to_ws, {.id = 11} },
4728 { "mvws_13", send_to_ws, {.id = 12} },
4729 { "mvws_14", send_to_ws, {.id = 13} },
4730 { "mvws_15", send_to_ws, {.id = 14} },
4731 { "mvws_16", send_to_ws, {.id = 15} },
4732 { "mvws_17", send_to_ws, {.id = 16} },
4733 { "mvws_18", send_to_ws, {.id = 17} },
4734 { "mvws_19", send_to_ws, {.id = 18} },
4735 { "mvws_20", send_to_ws, {.id = 19} },
4736 { "mvws_21", send_to_ws, {.id = 20} },
4737 { "mvws_22", send_to_ws, {.id = 21} },
4738 { "name_workspace", name_workspace, {0} },
4739 { "quit", quit, {0} },
4740 { "raise_toggle", raise_toggle, {0} },
4741 { "restart", restart, {0} },
4742 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4743 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4744 { "search_win", search_win, {0} },
4745 { "search_workspace", search_workspace, {0} },
4746 { "spawn_custom", NULL, {0} },
4747 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4748 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4749 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4750 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4751 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4752 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4753 { "uniconify", uniconify, {0} },
4754 { "version", version, {0} },
4755 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4756 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4757 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4758 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4759 { "ws_1", switchws, {.id = 0} },
4760 { "ws_2", switchws, {.id = 1} },
4761 { "ws_3", switchws, {.id = 2} },
4762 { "ws_4", switchws, {.id = 3} },
4763 { "ws_5", switchws, {.id = 4} },
4764 { "ws_6", switchws, {.id = 5} },
4765 { "ws_7", switchws, {.id = 6} },
4766 { "ws_8", switchws, {.id = 7} },
4767 { "ws_9", switchws, {.id = 8} },
4768 { "ws_10", switchws, {.id = 9} },
4769 { "ws_11", switchws, {.id = 10} },
4770 { "ws_12", switchws, {.id = 11} },
4771 { "ws_13", switchws, {.id = 12} },
4772 { "ws_14", switchws, {.id = 13} },
4773 { "ws_15", switchws, {.id = 14} },
4774 { "ws_16", switchws, {.id = 15} },
4775 { "ws_17", switchws, {.id = 16} },
4776 { "ws_18", switchws, {.id = 17} },
4777 { "ws_19", switchws, {.id = 18} },
4778 { "ws_20", switchws, {.id = 19} },
4779 { "ws_21", switchws, {.id = 20} },
4780 { "ws_22", switchws, {.id = 21} },
4781 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4782 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4783 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4784 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4785 { "ws_prior", priorws, {0} },
4786 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4787 { "invalid key func", NULL, {0} },
4790 RB_ENTRY(key) entry;
4793 enum keyfuncid funcid;
4796 RB_HEAD(key_tree, key);
4799 key_cmp(struct key *kp1, struct key *kp2)
4801 if (kp1->keysym < kp2->keysym)
4803 if (kp1->keysym > kp2->keysym)
4806 if (kp1->mod < kp2->mod)
4808 if (kp1->mod > kp2->mod)
4814 RB_GENERATE(key_tree, key, entry, key_cmp);
4815 struct key_tree keys;
4818 enum { client_click, root_click };
4820 unsigned int action;
4822 unsigned int button;
4823 void (*func)(struct ws_win *, union arg *);
4826 /* action key mouse button func args */
4827 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4828 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4829 { client_click, MODKEY, Button1, move, {0} },
4833 update_modkey(unsigned int mod)
4839 RB_FOREACH(kp, key_tree, &keys)
4840 if (kp->mod & ShiftMask)
4841 kp->mod = mod | ShiftMask;
4845 for (i = 0; i < LENGTH(buttons); i++)
4846 if (buttons[i].mask & ShiftMask)
4847 buttons[i].mask = mod | ShiftMask;
4849 buttons[i].mask = mod;
4854 TAILQ_ENTRY(spawn_prog) entry;
4859 TAILQ_HEAD(spawn_list, spawn_prog);
4860 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4863 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4866 struct spawn_prog *prog = NULL;
4868 char *ap, **real_args;
4870 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4873 TAILQ_FOREACH(prog, &spawns, entry) {
4874 if (!strcasecmp(spawn_name, prog->name))
4878 warnx("spawn_custom: program %s not found", spawn_name);
4882 /* make room for expanded args */
4883 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4884 err(1, "spawn_custom: calloc real_args");
4886 /* expand spawn_args into real_args */
4887 for (i = 0; i < prog->argc; i++) {
4889 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4890 if (!strcasecmp(ap, "$bar_border")) {
4892 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4894 err(1, "spawn_custom border color");
4895 } else if (!strcasecmp(ap, "$bar_color")) {
4897 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4899 err(1, "spawn_custom bar color");
4900 } else if (!strcasecmp(ap, "$bar_font")) {
4901 if ((real_args[i] = strdup(bar_fonts))
4903 err(1, "spawn_custom bar fonts");
4904 } else if (!strcasecmp(ap, "$bar_font_color")) {
4906 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4908 err(1, "spawn_custom color font");
4909 } else if (!strcasecmp(ap, "$color_focus")) {
4911 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4913 err(1, "spawn_custom color focus");
4914 } else if (!strcasecmp(ap, "$color_unfocus")) {
4916 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4918 err(1, "spawn_custom color unfocus");
4920 /* no match --> copy as is */
4921 if ((real_args[i] = strdup(ap)) == NULL)
4922 err(1, "spawn_custom strdup(ap)");
4924 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4929 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4930 for (i = 0; i < prog->argc; i++)
4931 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4932 DNPRINTF(SWM_D_SPAWN, "\n");
4934 *ret_args = real_args;
4935 return (prog->argc);
4939 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4945 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4949 spawn(r->ws->idx, &a, 1);
4951 for (i = 0; i < spawn_argc; i++)
4957 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4963 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4967 if (pipe(select_list_pipe) == -1)
4968 err(1, "pipe error");
4969 if (pipe(select_resp_pipe) == -1)
4970 err(1, "pipe error");
4972 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4973 err(1, "could not disable SIGPIPE");
4974 switch (*pid = fork()) {
4976 err(1, "cannot fork");
4979 if (dup2(select_list_pipe[0], 0) == -1)
4981 if (dup2(select_resp_pipe[1], 1) == -1)
4983 close(select_list_pipe[1]);
4984 close(select_resp_pipe[0]);
4985 spawn(r->ws->idx, &a, 0);
4987 default: /* parent */
4988 close(select_list_pipe[0]);
4989 close(select_resp_pipe[1]);
4993 for (i = 0; i < spawn_argc; i++)
4999 spawn_insert(char *name, char *args)
5001 char *arg, *cp, *ptr;
5002 struct spawn_prog *sp;
5004 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5006 if ((sp = calloc(1, sizeof *sp)) == NULL)
5007 err(1, "spawn_insert: malloc");
5008 if ((sp->name = strdup(name)) == NULL)
5009 err(1, "spawn_insert: strdup");
5011 /* convert the arguments to an argument list */
5012 if ((ptr = cp = strdup(args)) == NULL)
5013 err(1, "spawn_insert: strdup");
5014 while ((arg = strsep(&ptr, " \t")) != NULL) {
5015 /* empty field; skip it */
5020 if ((sp->argv = realloc(sp->argv, sp->argc *
5021 sizeof *sp->argv)) == NULL)
5022 err(1, "spawn_insert: realloc");
5023 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5024 err(1, "spawn_insert: strdup");
5028 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5029 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5033 spawn_remove(struct spawn_prog *sp)
5037 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5039 TAILQ_REMOVE(&spawns, sp, entry);
5040 for (i = 0; i < sp->argc; i++)
5046 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5050 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5052 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5055 spawn_insert(name, args);
5057 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5061 setspawn(char *name, char *args)
5063 struct spawn_prog *sp;
5065 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5070 TAILQ_FOREACH(sp, &spawns, entry) {
5071 if (!strcmp(sp->name, name)) {
5075 spawn_replace(sp, name, args);
5076 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5080 if (*args == '\0') {
5081 warnx("error: setspawn: cannot find program: %s", name);
5085 spawn_insert(name, args);
5086 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5090 setconfspawn(char *selector, char *value, int flags)
5092 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5094 setspawn(selector, value);
5096 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5103 setconfspawn("term", "xterm", 0);
5104 setconfspawn("spawn_term", "xterm", 0);
5105 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5106 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5107 setconfspawn("lock", "xlock", 0);
5108 setconfspawn("initscr", "initscreen.sh", 0);
5109 setconfspawn("menu", "dmenu_run"
5112 " -nf $bar_font_color"
5114 " -sf $bar_color", 0);
5115 setconfspawn("search", "dmenu"
5119 " -nf $bar_font_color"
5121 " -sf $bar_color", 0);
5122 setconfspawn("name_workspace", "dmenu"
5126 " -nf $bar_font_color"
5128 " -sf $bar_color", 0);
5132 #define SWM_MODNAME_SIZE 32
5133 #define SWM_KEY_WS "\n+ \t"
5135 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5139 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5140 if (mod == NULL || ks == NULL) {
5141 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5144 if (keystr == NULL || strlen(keystr) == 0) {
5145 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5149 *ks = XCB_NO_SYMBOL;
5151 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5152 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5154 cp += (long)strspn(cp, SWM_KEY_WS);
5155 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5157 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5159 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5161 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5163 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5165 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5167 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5168 *mod |= ControlMask;
5170 *ks = XStringToKeysym(name);
5171 XConvertCase(*ks, ks, &uks);
5172 if (ks == XCB_NO_SYMBOL) {
5174 "parsekeys: invalid key %s\n",
5180 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5185 strdupsafe(char *str)
5190 return (strdup(str));
5194 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5198 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5199 keyfuncs[kfid].name, spawn_name);
5201 if ((kp = malloc(sizeof *kp)) == NULL)
5202 err(1, "key_insert: malloc");
5207 kp->spawn_name = strdupsafe(spawn_name);
5208 RB_INSERT(key_tree, &keys, kp);
5210 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5214 key_lookup(unsigned int mod, KeySym ks)
5221 return (RB_FIND(key_tree, &keys, &kp));
5225 key_remove(struct key *kp)
5227 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5229 RB_REMOVE(key_tree, &keys, kp);
5230 free(kp->spawn_name);
5233 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5237 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5240 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5244 key_insert(mod, ks, kfid, spawn_name);
5246 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5250 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5255 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5256 keyfuncs[kfid].name, spawn_name);
5258 if ((kp = key_lookup(mod, ks)) != NULL) {
5259 if (kfid == KF_INVALID)
5262 key_replace(kp, mod, ks, kfid, spawn_name);
5263 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5266 if (kfid == KF_INVALID) {
5267 warnx("error: setkeybinding: cannot find mod/key combination");
5268 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5272 key_insert(mod, ks, kfid, spawn_name);
5273 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5277 setconfbinding(char *selector, char *value, int flags)
5279 enum keyfuncid kfid;
5282 struct spawn_prog *sp;
5283 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5284 if (selector == NULL) {
5285 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5286 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5288 setkeybinding(mod, ks, kfid, NULL);
5293 /* search by key function name */
5294 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5295 if (strncasecmp(selector, keyfuncs[kfid].name,
5296 SWM_FUNCNAME_LEN) == 0) {
5297 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5299 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5300 setkeybinding(mod, ks, kfid, NULL);
5306 /* search by custom spawn name */
5307 TAILQ_FOREACH(sp, &spawns, entry) {
5308 if (strcasecmp(selector, sp->name) == 0) {
5309 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5311 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5312 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5319 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5326 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5327 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5328 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5329 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5330 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5331 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5332 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5333 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5334 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5335 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5336 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5337 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5338 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5339 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5340 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5341 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5342 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5343 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5344 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5345 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5346 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5347 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5348 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5349 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5350 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5351 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5352 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5353 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5354 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5355 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5356 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5357 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5358 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5359 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5360 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5361 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5362 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5363 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5364 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5365 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5366 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5367 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5368 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5369 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5370 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5371 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5372 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5374 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5375 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5376 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5377 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5380 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5381 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5382 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5383 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5384 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5385 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5386 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5387 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5388 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5389 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5390 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5391 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5392 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5393 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5394 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5395 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5396 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5397 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5398 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5399 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5400 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5401 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5402 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5403 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5404 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5405 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5406 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5407 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5408 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5409 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5410 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5411 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5412 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5414 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5415 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5416 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5419 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5420 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5421 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5432 while (RB_EMPTY(&keys) == 0) {
5433 kp = RB_ROOT(&keys);
5439 setkeymapping(char *selector, char *value, int flags)
5441 char keymapping_file[PATH_MAX];
5442 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5443 if (value[0] == '~')
5444 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5445 pwd->pw_dir, &value[1]);
5447 strlcpy(keymapping_file, value, sizeof keymapping_file);
5449 /* load new key bindings; if it fails, revert to default bindings */
5450 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5454 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5459 updatenumlockmask(void)
5462 xcb_get_modifier_mapping_reply_t *modmap_r;
5463 xcb_keycode_t *modmap, kc;
5465 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5468 modmap_r = xcb_get_modifier_mapping_reply(conn,
5469 xcb_get_modifier_mapping(conn),
5472 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5473 for (i = 0; i < 8; i++) {
5474 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5475 kc = modmap[i * modmap_r->keycodes_per_modifier
5478 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5480 numlockmask = (1 << i);
5492 xcb_keycode_t *code;
5493 unsigned int modifiers[] =
5494 { 0, LockMask, numlockmask, numlockmask | LockMask };
5497 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5498 updatenumlockmask();
5500 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5501 for (k = 0; k < num_screens; k++) {
5502 if (TAILQ_EMPTY(&screens[k].rl))
5504 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5506 RB_FOREACH(kp, key_tree, &keys) {
5507 if ((code = xcb_key_symbols_get_keycode(syms,
5509 for (j = 0; j < LENGTH(modifiers); j++)
5510 xcb_grab_key(conn, True,
5512 kp->mod | modifiers[j],
5513 *code, XCB_GRAB_MODE_ASYNC,
5514 XCB_GRAB_MODE_ASYNC);
5520 grabbuttons(struct ws_win *win, int focused)
5523 unsigned int modifiers[] =
5524 { 0, LockMask, numlockmask, numlockmask|LockMask };
5526 updatenumlockmask();
5527 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5528 XCB_BUTTON_MASK_ANY);
5530 for (i = 0; i < LENGTH(buttons); i++)
5531 if (buttons[i].action == client_click)
5532 for (j = 0; j < LENGTH(modifiers); j++)
5533 xcb_grab_button(conn, False, win->id,
5535 XCB_GRAB_MODE_ASYNC,
5542 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5543 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5544 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5547 const char *quirkname[] = {
5548 "NONE", /* config string for "no value" */
5557 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5558 #define SWM_Q_WS "\n|+ \t"
5560 parsequirks(char *qstr, unsigned long *quirk)
5570 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5572 cp += (long)strspn(cp, SWM_Q_WS);
5573 for (i = 0; i < LENGTH(quirkname); i++) {
5574 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5575 DNPRINTF(SWM_D_QUIRK,
5576 "parsequirks: %s\n", name);
5581 *quirk |= 1 << (i-1);
5585 if (i >= LENGTH(quirkname)) {
5586 DNPRINTF(SWM_D_QUIRK,
5587 "parsequirks: invalid quirk [%s]\n", name);
5595 quirk_insert(const char *class, const char *name, unsigned long quirk)
5599 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5602 if ((qp = malloc(sizeof *qp)) == NULL)
5603 err(1, "quirk_insert: malloc");
5604 if ((qp->class = strdup(class)) == NULL)
5605 err(1, "quirk_insert: strdup");
5606 if ((qp->name = strdup(name)) == NULL)
5607 err(1, "quirk_insert: strdup");
5610 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5612 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5616 quirk_remove(struct quirk *qp)
5618 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5619 qp->name, qp->quirk);
5621 TAILQ_REMOVE(&quirks, qp, entry);
5626 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5630 quirk_replace(struct quirk *qp, const char *class, const char *name,
5631 unsigned long quirk)
5633 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5634 qp->name, qp->quirk);
5637 quirk_insert(class, name, quirk);
5639 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5643 setquirk(const char *class, const char *name, unsigned long quirk)
5647 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5650 TAILQ_FOREACH(qp, &quirks, entry) {
5651 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5655 quirk_replace(qp, class, name, quirk);
5656 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5661 warnx("error: setquirk: cannot find class/name combination");
5665 quirk_insert(class, name, quirk);
5666 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5670 setconfquirk(char *selector, char *value, int flags)
5672 char *cp, *class, *name;
5674 unsigned long quirks;
5675 if (selector == NULL)
5677 if ((cp = strchr(selector, ':')) == NULL)
5682 if ((retval = parsequirks(value, &quirks)) == 0)
5683 setquirk(class, name, quirks);
5690 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5691 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5692 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5693 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5694 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5695 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5696 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5697 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5698 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5699 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5700 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5701 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5702 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5705 /* conf file stuff */
5706 #define SWM_CONF_FILE "spectrwm.conf"
5707 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5711 SWM_S_BAR_AT_BOTTOM,
5712 SWM_S_BAR_BORDER_WIDTH,
5719 SWM_S_CLOCK_ENABLED,
5722 SWM_S_CYCLE_VISIBLE,
5724 SWM_S_DISABLE_BORDER,
5726 SWM_S_FOCUS_CLOSE_WRAP,
5727 SWM_S_FOCUS_DEFAULT,
5733 SWM_S_STACK_ENABLED,
5735 SWM_S_TITLE_CLASS_ENABLED,
5736 SWM_S_TITLE_NAME_ENABLED,
5737 SWM_S_URGENT_ENABLED,
5738 SWM_S_VERBOSE_LAYOUT,
5739 SWM_S_WINDOW_NAME_ENABLED,
5740 SWM_S_WORKSPACE_LIMIT
5744 setconfvalue(char *selector, char *value, int flags)
5750 case SWM_S_BAR_ACTION:
5752 if ((bar_argv[0] = strdup(value)) == NULL)
5753 err(1, "setconfvalue: bar_action");
5755 case SWM_S_BAR_AT_BOTTOM:
5756 bar_at_bottom = atoi(value);
5758 case SWM_S_BAR_BORDER_WIDTH:
5759 bar_border_width = atoi(value);
5760 if (bar_border_width < 0)
5761 bar_border_width = 0;
5763 case SWM_S_BAR_DELAY:
5764 bar_delay = atoi(value);
5766 case SWM_S_BAR_ENABLED:
5767 bar_enabled = atoi(value);
5769 case SWM_S_BAR_FONT:
5771 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5772 err(1, "setconfvalue: asprintf: failed to allocate "
5773 "memory for bar_fonts.");
5777 case SWM_S_BAR_FORMAT:
5779 if ((bar_format = strdup(value)) == NULL)
5780 err(1, "setconfvalue: bar_format");
5782 case SWM_S_BAR_JUSTIFY:
5783 if (!strcmp(value, "left"))
5784 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5785 else if (!strcmp(value, "center"))
5786 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5787 else if (!strcmp(value, "right"))
5788 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5790 errx(1, "invalid bar_justify");
5792 case SWM_S_BORDER_WIDTH:
5793 border_width = atoi(value);
5794 if (border_width < 0)
5797 case SWM_S_CLOCK_ENABLED:
5798 clock_enabled = atoi(value);
5800 case SWM_S_CLOCK_FORMAT:
5801 #ifndef SWM_DENY_CLOCK_FORMAT
5803 if ((clock_format = strdup(value)) == NULL)
5804 err(1, "setconfvalue: clock_format");
5807 case SWM_S_CYCLE_EMPTY:
5808 cycle_empty = atoi(value);
5810 case SWM_S_CYCLE_VISIBLE:
5811 cycle_visible = atoi(value);
5813 case SWM_S_DIALOG_RATIO:
5814 dialog_ratio = atof(value);
5815 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5818 case SWM_S_DISABLE_BORDER:
5819 disable_border = atoi(value);
5821 case SWM_S_FOCUS_CLOSE:
5822 if (!strcmp(value, "first"))
5823 focus_close = SWM_STACK_BOTTOM;
5824 else if (!strcmp(value, "last"))
5825 focus_close = SWM_STACK_TOP;
5826 else if (!strcmp(value, "next"))
5827 focus_close = SWM_STACK_ABOVE;
5828 else if (!strcmp(value, "previous"))
5829 focus_close = SWM_STACK_BELOW;
5831 errx(1, "focus_close");
5833 case SWM_S_FOCUS_CLOSE_WRAP:
5834 focus_close_wrap = atoi(value);
5836 case SWM_S_FOCUS_DEFAULT:
5837 if (!strcmp(value, "last"))
5838 focus_default = SWM_STACK_TOP;
5839 else if (!strcmp(value, "first"))
5840 focus_default = SWM_STACK_BOTTOM;
5842 errx(1, "focus_default");
5844 case SWM_S_FOCUS_MODE:
5845 if (!strcmp(value, "default"))
5846 focus_mode = SWM_FOCUS_DEFAULT;
5847 else if (!strcmp(value, "follow_cursor"))
5848 focus_mode = SWM_FOCUS_FOLLOW;
5849 else if (!strcmp(value, "synergy"))
5850 focus_mode = SWM_FOCUS_SYNERGY;
5852 errx(1, "focus_mode");
5854 case SWM_S_SPAWN_ORDER:
5855 if (!strcmp(value, "first"))
5856 spawn_position = SWM_STACK_BOTTOM;
5857 else if (!strcmp(value, "last"))
5858 spawn_position = SWM_STACK_TOP;
5859 else if (!strcmp(value, "next"))
5860 spawn_position = SWM_STACK_ABOVE;
5861 else if (!strcmp(value, "previous"))
5862 spawn_position = SWM_STACK_BELOW;
5864 errx(1, "spawn_position");
5866 case SWM_S_SPAWN_TERM:
5867 setconfspawn("term", value, 0);
5868 setconfspawn("spawn_term", value, 0);
5872 case SWM_S_SS_ENABLED:
5873 ss_enabled = atoi(value);
5875 case SWM_S_STACK_ENABLED:
5876 stack_enabled = atoi(value);
5878 case SWM_S_TERM_WIDTH:
5879 term_width = atoi(value);
5883 case SWM_S_TITLE_CLASS_ENABLED:
5884 title_class_enabled = atoi(value);
5886 case SWM_S_TITLE_NAME_ENABLED:
5887 title_name_enabled = atoi(value);
5889 case SWM_S_URGENT_ENABLED:
5890 urgent_enabled = atoi(value);
5892 case SWM_S_VERBOSE_LAYOUT:
5893 verbose_layout = atoi(value);
5894 for (i = 0; layouts[i].l_stack != NULL; i++) {
5896 layouts[i].l_string = fancy_stacker;
5898 layouts[i].l_string = plain_stacker;
5901 case SWM_S_WINDOW_NAME_ENABLED:
5902 window_name_enabled = atoi(value);
5904 case SWM_S_WORKSPACE_LIMIT:
5905 workspace_limit = atoi(value);
5906 if (workspace_limit > SWM_WS_MAX)
5907 workspace_limit = SWM_WS_MAX;
5908 else if (workspace_limit < 1)
5909 workspace_limit = 1;
5918 setconfmodkey(char *selector, char *value, int flags)
5920 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5921 update_modkey(Mod1Mask);
5922 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5923 update_modkey(Mod2Mask);
5924 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5925 update_modkey(Mod3Mask);
5926 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5927 update_modkey(Mod4Mask);
5934 setconfcolor(char *selector, char *value, int flags)
5936 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5941 setconfregion(char *selector, char *value, int flags)
5943 custom_region(value);
5948 setautorun(char *selector, char *value, int flags)
5958 if (getenv("SWM_STARTED"))
5962 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5963 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5965 if (ws_id < 0 || ws_id >= workspace_limit)
5966 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5969 * This is a little intricate
5971 * If the pid already exists we simply reuse it because it means it was
5972 * used before AND not claimed by manage_window. We get away with
5973 * altering it in the parent after INSERT because this can not be a race
5976 while ((ap = strsep(&sp, " \t")) != NULL) {
5979 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5981 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5982 err(1, "setautorun: realloc");
5983 a.argv[argc - 1] = ap;
5986 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5987 err(1, "setautorun: realloc");
5988 a.argv[argc] = NULL;
5990 if ((pid = fork()) == 0) {
5991 spawn(ws_id, &a, 1);
6000 p = calloc(1, sizeof *p);
6003 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6013 setlayout(char *selector, char *value, int flags)
6015 int ws_id, i, x, mg, ma, si, raise, f = 0;
6016 int st = SWM_V_STACK, num_screens;
6018 struct workspace *ws;
6020 if (getenv("SWM_STARTED"))
6024 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6025 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6026 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6027 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6030 if (ws_id < 0 || ws_id >= workspace_limit)
6031 errx(1, "layout: invalid workspace %d", ws_id + 1);
6033 if (!strcasecmp(s, "vertical"))
6035 else if (!strcasecmp(s, "vertical_flip")) {
6038 } else if (!strcasecmp(s, "horizontal"))
6040 else if (!strcasecmp(s, "horizontal_flip")) {
6043 } else if (!strcasecmp(s, "fullscreen"))
6046 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6047 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6050 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6051 for (i = 0; i < num_screens; i++) {
6052 ws = (struct workspace *)&screens[i].ws;
6053 ws[ws_id].cur_layout = &layouts[st];
6055 ws[ws_id].always_raise = raise;
6056 if (st == SWM_MAX_STACK)
6060 for (x = 0; x < abs(mg); x++) {
6061 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6062 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6063 SWM_ARG_ID_MASTERSHRINK);
6067 for (x = 0; x < abs(ma); x++) {
6068 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6069 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6070 SWM_ARG_ID_MASTERDEL);
6074 for (x = 0; x < abs(si); x++) {
6075 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6076 si >= 0 ? SWM_ARG_ID_STACKINC :
6077 SWM_ARG_ID_STACKDEC);
6082 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6083 SWM_ARG_ID_FLIPLAYOUT);
6091 /* config options */
6092 struct config_option {
6094 int (*func)(char*, char*, int);
6097 struct config_option configopt[] = {
6098 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6099 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6100 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6101 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6102 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6103 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6104 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6105 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6106 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6107 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6108 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6109 { "keyboard_mapping", setkeymapping, 0 },
6110 { "bind", setconfbinding, 0 },
6111 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6112 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6113 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6114 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6115 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6116 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6117 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6118 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6119 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6120 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6121 { "modkey", setconfmodkey, 0 },
6122 { "program", setconfspawn, 0 },
6123 { "quirk", setconfquirk, 0 },
6124 { "region", setconfregion, 0 },
6125 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6126 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6127 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6128 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6129 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6130 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6131 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6132 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6133 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6134 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6135 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6136 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6137 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6138 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6139 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6140 { "autorun", setautorun, 0 },
6141 { "layout", setlayout, 0 },
6146 conf_load(char *filename, int keymapping)
6149 char *line, *cp, *optsub, *optval;
6150 size_t linelen, lineno = 0;
6151 int wordlen, i, optind;
6152 struct config_option *opt;
6154 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6156 if (filename == NULL) {
6157 warnx("conf_load: no filename");
6160 if ((config = fopen(filename, "r")) == NULL) {
6161 warn("conf_load: fopen: %s", filename);
6165 while (!feof(config)) {
6166 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6169 err(1, "%s", filename);
6174 cp += strspn(cp, " \t\n"); /* eat whitespace */
6175 if (cp[0] == '\0') {
6180 /* get config option */
6181 wordlen = strcspn(cp, "=[ \t\n");
6183 warnx("%s: line %zd: no option found",
6188 for (i = 0; i < LENGTH(configopt); i++) {
6189 opt = &configopt[i];
6190 if (!strncasecmp(cp, opt->optname, wordlen) &&
6191 strlen(opt->optname) == wordlen) {
6197 warnx("%s: line %zd: unknown option %.*s",
6198 filename, lineno, wordlen, cp);
6201 if (keymapping && strcmp(opt->optname, "bind")) {
6202 warnx("%s: line %zd: invalid option %.*s",
6203 filename, lineno, wordlen, cp);
6207 cp += strspn(cp, " \t\n"); /* eat whitespace */
6208 /* get [selector] if any */
6212 wordlen = strcspn(cp, "]");
6215 warnx("%s: line %zd: syntax error",
6220 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6222 warnx("%s: line %zd: unable to allocate"
6223 "memory for selector", filename,
6229 cp += strspn(cp, "] \t\n"); /* eat trailing */
6231 cp += strspn(cp, "= \t\n"); /* eat trailing */
6233 optval = strdup(cp);
6234 /* call function to deal with it all */
6235 if (configopt[optind].func(optsub, optval,
6236 configopt[optind].funcflags) != 0)
6237 errx(1, "%s: line %zd: invalid data for %s",
6238 filename, lineno, configopt[optind].optname);
6245 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6252 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6258 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6260 struct ws_win *parent, *w;
6261 struct swm_region *r;
6262 struct workspace *ws;
6263 xcb_icccm_wm_hints_t wmh;
6265 parent = find_window(win->transient);
6267 parent->child_trans = win;
6269 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6270 " for 0x%x trans 0x%x\n", win->id, win->transient);
6272 r = root_to_region(win->wa->root);
6274 /* parent doen't exist in our window list */
6275 TAILQ_FOREACH(w, &ws->winlist, entry) {
6276 if (xcb_icccm_get_wm_hints_reply(conn,
6277 xcb_icccm_get_wm_hints(conn, w->id),
6279 warnx("can't get hints for 0x%x", w->id);
6283 if (win->hints.window_group != wmh.window_group)
6286 w->child_trans = win;
6287 win->transient = w->id;
6289 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6290 "transient to 0x%x\n", win->transient);
6297 window_get_pid(xcb_window_t win)
6302 xcb_get_property_cookie_t pc;
6303 xcb_get_property_reply_t *pr;
6305 apid = get_atom_from_string("_NET_WM_PID");
6306 if (apid == XCB_ATOM_NONE)
6309 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6310 pr = xcb_get_property_reply(conn, pc, NULL);
6313 if (pr->type != XCB_ATOM_CARDINAL) {
6318 ret = *((pid_t *)xcb_get_property_value(pr));
6324 apid = get_atom_from_string("_SWM_PID");
6325 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6327 pr = xcb_get_property_reply(conn, pc, NULL);
6330 if (pr->type != XCB_ATOM_STRING) {
6335 ret = strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6342 manage_window(xcb_window_t id)
6344 xcb_window_t trans = XCB_WINDOW_NONE;
6345 struct workspace *ws;
6346 struct ws_win *win, *ww;
6347 int i, ws_idx, border_me = 0;
6348 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6349 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6351 struct swm_region *r;
6355 uint32_t event_mask;
6357 xcb_get_property_reply_t *gpr;
6358 xcb_icccm_get_wm_protocols_reply_t wpr;
6360 if ((win = find_window(id)) != NULL)
6361 return (win); /* already being managed */
6363 /* see if we are on the unmanaged list */
6364 if ((win = find_unmanaged_window(id)) != NULL) {
6365 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6366 "window: 0x%x\n", win->id);
6367 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6369 set_child_transient(win, &trans);
6371 if (trans && (ww = find_window(trans)))
6372 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6373 else if ((ww = win->ws->focus) &&
6374 spawn_position == SWM_STACK_ABOVE)
6375 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6377 else if (ww && spawn_position == SWM_STACK_BELOW)
6378 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6379 else switch (spawn_position) {
6382 case SWM_STACK_ABOVE:
6383 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6385 case SWM_STACK_BOTTOM:
6386 case SWM_STACK_BELOW:
6387 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6390 ewmh_update_actions(win);
6394 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6395 err(1, "manage_window: calloc: failed to allocate memory for "
6401 /* see if we need to override the workspace */
6402 p = find_pid(window_get_pid(id));
6404 /* Get all the window data in one shot */
6405 ws_idx_atom = get_atom_from_string("_SWM_WS");
6407 gpr = xcb_get_property_reply(conn,
6408 xcb_get_property(conn, False, id, ws_idx_atom,
6409 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6412 proplen = xcb_get_property_value_length(gpr);
6414 prop = malloc(proplen + 1);
6417 xcb_get_property_value(gpr),
6419 prop[proplen] = '\0';
6425 win->wa = xcb_get_geometry_reply(conn,
6426 xcb_get_geometry(conn, id),
6428 xcb_icccm_get_wm_normal_hints_reply(conn,
6429 xcb_icccm_get_wm_normal_hints(conn, id),
6431 xcb_icccm_get_wm_hints_reply(conn,
6432 xcb_icccm_get_wm_hints(conn, id),
6434 xcb_icccm_get_wm_transient_for_reply(conn,
6435 xcb_icccm_get_wm_transient_for(conn, id),
6438 win->transient = trans;
6439 set_child_transient(win, &trans);
6440 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6441 "transient: 0x%x\n", win->id, win->transient);
6444 prot = get_atom_from_string("WM_PROTOCOLS");
6446 /* get supported protocols */
6447 if (xcb_icccm_get_wm_protocols_reply(conn,
6448 xcb_icccm_get_wm_protocols(conn, id, prot),
6450 for (i = 0; i < wpr.atoms_len; i++) {
6451 if (wpr.atoms[i] == a_takefocus)
6452 win->take_focus = 1;
6453 if (wpr.atoms[i] == a_delete)
6454 win->can_delete = 1;
6456 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6459 win->iconic = get_iconic(win);
6462 * Figure out where to put the window. If it was previously assigned to
6463 * a workspace (either by spawn() or manually moving), and isn't
6464 * transient, * put it in the same workspace
6466 r = root_to_region(win->wa->root);
6468 ws = &r->s->ws[p->ws];
6469 TAILQ_REMOVE(&pidlist, p, entry);
6472 } else if (prop && win->transient == 0) {
6473 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6474 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6477 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6480 ws = &r->s->ws[ws_idx];
6483 /* this should launch transients in the same ws as parent */
6485 if ((ww = find_window(trans)) != NULL)
6491 warnx("manage_window: fix this "
6497 /* set up the window layout */
6500 win->s = r->s; /* this never changes */
6501 if (trans && (ww = find_window(trans)))
6502 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6503 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6504 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6506 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6507 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6508 else switch (spawn_position) {
6511 case SWM_STACK_ABOVE:
6512 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6514 case SWM_STACK_BOTTOM:
6515 case SWM_STACK_BELOW:
6516 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6519 /* ignore window border if there is one. */
6520 WIDTH(win) = win->wa->width;
6521 HEIGHT(win) = win->wa->height;
6522 X(win) = win->wa->x + win->wa->border_width;
6523 Y(win) = win->wa->y + win->wa->border_width;
6525 win->g_floatvalid = 0;
6526 win->floatmaxed = 0;
6527 win->ewmh_flags = 0;
6529 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6530 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6531 HEIGHT(win), ws->idx);
6533 constrain_window(win, r, 0);
6535 /* Set window properties so we can remember this after reincarnation */
6536 if (ws_idx_atom && prop == NULL &&
6537 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6539 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6541 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6542 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6548 ewmh_autoquirk(win);
6550 if (xcb_icccm_get_wm_class_reply(conn,
6551 xcb_icccm_get_wm_class(conn, win->id),
6553 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6554 win->ch.class_name, win->ch.instance_name);
6556 /* java is retarded so treat it special */
6557 if (strstr(win->ch.instance_name, "sun-awt")) {
6562 TAILQ_FOREACH(qp, &quirks, entry) {
6563 if (!strcmp(win->ch.class_name, qp->class) &&
6564 !strcmp(win->ch.instance_name, qp->name)) {
6565 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6566 "class: %s, name: %s\n", win->ch.class_name,
6567 win->ch.instance_name);
6568 if (qp->quirk & SWM_Q_FLOAT) {
6572 win->quirks = qp->quirk;
6577 /* alter window position if quirky */
6578 if (win->quirks & SWM_Q_ANYWHERE) {
6579 win->manual = 1; /* don't center the quirky windows */
6580 if (bar_enabled && Y(win) < bar_height)
6581 Y(win) = bar_height;
6582 if (WIDTH(win) + X(win) > WIDTH(r))
6583 X(win) = WIDTH(r) - WIDTH(win) - 2;
6587 /* Reset font sizes (the bruteforce way; no default keybinding). */
6588 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6589 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6590 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6591 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6592 fake_keypress(win, XK_KP_Add, ShiftMask);
6595 ewmh_get_win_state(win);
6596 ewmh_update_actions(win);
6597 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6602 X(win) -= border_width;
6603 Y(win) -= border_width;
6607 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6608 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6610 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6612 /* floaters need to be mapped if they are in the current workspace */
6613 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6614 map_window_raised(win->id);
6620 free_window(struct ws_win *win)
6622 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6627 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6632 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6637 memset(win, 0xff, sizeof *win); /* XXX kill later */
6640 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6644 unmanage_window(struct ws_win *win)
6646 struct ws_win *parent;
6647 xcb_screen_t *screen;
6652 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6654 if (win->transient) {
6655 parent = find_window(win->transient);
6657 parent->child_trans = NULL;
6660 /* focus on root just in case */
6661 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6662 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6663 screen->root, XCB_CURRENT_TIME);
6667 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6668 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6672 focus_magic(struct ws_win *win)
6674 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6677 /* if there are no windows clear the status-bar */
6682 if (win->child_trans) {
6683 /* win = parent & has a transient so focus on that */
6685 focus_win(win->child_trans);
6686 if (win->child_trans->take_focus)
6687 client_msg(win, a_takefocus);
6689 /* make sure transient hasn't disappeared */
6690 if (validate_win(win->child_trans) == 0) {
6691 focus_win(win->child_trans);
6692 if (win->child_trans->take_focus)
6693 client_msg(win->child_trans,
6696 win->child_trans = NULL;
6698 if (win->take_focus)
6699 client_msg(win, a_takefocus);
6705 if (win->take_focus)
6706 client_msg(win, a_takefocus);
6711 expose(xcb_expose_event_t *e)
6714 struct swm_region *r;
6716 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6718 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6719 for (i = 0; i < num_screens; i++)
6720 TAILQ_FOREACH(r, &screens[i].rl, entry)
6721 if (e->window == WINID(r->bar))
6728 keypress(xcb_key_press_event_t *e)
6730 xcb_keysym_t keysym;
6733 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6735 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6737 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6740 if (kp->funcid == KF_SPAWN_CUSTOM)
6741 spawn_custom(root_to_region(e->root),
6742 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6743 else if (keyfuncs[kp->funcid].func)
6744 keyfuncs[kp->funcid].func(root_to_region(e->root),
6745 &(keyfuncs[kp->funcid].args));
6749 buttonpress(xcb_button_press_event_t *e)
6754 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6755 e->event, e->detail);
6757 if ((win = find_window(e->event)) == NULL)
6761 action = client_click;
6763 for (i = 0; i < LENGTH(buttons); i++)
6764 if (action == buttons[i].action && buttons[i].func &&
6765 buttons[i].button == e->detail &&
6766 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6767 buttons[i].func(win, &buttons[i].args);
6773 configurerequest(xcb_configure_request_event_t *e)
6778 uint32_t wc[7] = {0};
6780 if ((win = find_window(e->window)) == NULL)
6781 if ((win = find_unmanaged_window(e->window)) == NULL)
6785 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6786 mask |= XCB_CONFIG_WINDOW_X;
6789 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6790 mask |= XCB_CONFIG_WINDOW_Y;
6793 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6794 mask |= XCB_CONFIG_WINDOW_WIDTH;
6797 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6798 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6799 wc[i++] = e->height;
6801 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6802 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6803 wc[i++] = e->border_width;
6805 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6806 mask |= XCB_CONFIG_WINDOW_SIBLING;
6807 wc[i++] = e->sibling;
6809 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6810 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6811 wc[i++] = e->stack_mode;
6814 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6815 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e->window,
6816 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6818 xcb_configure_window(conn, e->window, mask, wc);
6819 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6820 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6821 win->g_float.x = e->x - X(win->ws->r);
6822 win->g_float.y = e->y - Y(win->ws->r);
6823 win->g_float.w = e->width;
6824 win->g_float.h = e->height;
6825 win->g_floatvalid = 1;
6827 if (win->floating) {
6828 win->g = win->g_float;
6829 win->g.x += X(win->ws->r);
6830 win->g.y += Y(win->ws->r);
6843 configurenotify(xcb_configure_notify_event_t *e)
6847 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6850 win = find_window(e->window);
6852 xcb_icccm_get_wm_normal_hints_reply(conn,
6853 xcb_icccm_get_wm_normal_hints(conn, win->id),
6862 destroynotify(xcb_destroy_notify_event_t *e)
6866 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6868 if ((win = find_window(e->window)) == NULL) {
6869 if ((win = find_unmanaged_window(e->window)) == NULL)
6875 /* make sure we focus on something */
6878 unmanage_window(win);
6884 enternotify(xcb_enter_notify_event_t *e)
6887 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6888 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6889 "state: %d\n", e->event, e->mode, e->detail, e->root,
6890 e->child, YESNO(e->same_screen_focus), e->state);
6892 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6893 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6898 switch (focus_mode) {
6899 case SWM_FOCUS_DEFAULT:
6901 case SWM_FOCUS_FOLLOW:
6903 case SWM_FOCUS_SYNERGY:
6907 if ((win = find_window(e->event)) == NULL) {
6908 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);
7025 case XCB_ATOM_WM_CLASS:
7026 case XCB_ATOM_WM_NAME:
7035 unmapnotify(xcb_unmap_notify_event_t *e)
7039 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7041 /* determine if we need to help unmanage this window */
7042 win = find_window(e->window);
7046 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7047 unmanage_window(win);
7050 /* resend unmap because we ated it */
7051 xcb_unmap_window(conn, e->window);
7057 visibilitynotify(xcb_visibility_notify_event_t *e)
7059 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7064 clientmessage(xcb_client_message_event_t *e)
7068 win = find_window(e->window);
7073 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, type: %u\n",
7074 e->window, e->type);
7076 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7077 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7080 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7081 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7082 if (win->can_delete)
7083 client_msg(win, a_delete);
7085 xcb_kill_client(conn, win->id);
7087 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7088 DNPRINTF(SWM_D_EVENT,
7089 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7090 if (win->floating) {
7091 if (e->data.data32[0] & (1<<8)) /* x */
7092 X(win) = e->data.data32[1];
7093 if (e->data.data32[0] & (1<<9)) /* y */
7094 Y(win) = e->data.data32[2];
7095 if (e->data.data32[0] & (1<<10)) /* width */
7096 WIDTH(win) = e->data.data32[3];
7097 if (e->data.data32[0] & (1<<11)) /* height */
7098 HEIGHT(win) = e->data.data32[4];
7103 /* TODO: Change stack sizes */
7104 /* notify no change was made. */
7105 config_win(win, NULL);
7108 if (e->type == ewmh[_NET_WM_STATE].atom) {
7109 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7110 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7111 if (e->data.data32[2])
7112 ewmh_update_win_state(win, e->data.data32[2],
7122 xerror_start(Display *d, XErrorEvent *ee)
7129 xerror(Display *d, XErrorEvent *ee)
7131 /* warnx("error: %p %p", display, ee); */
7139 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7142 /* this causes an error if some other window manager is running */
7143 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7144 for (i = 0; i < num_screens; i++) {
7146 xcb_change_window_attributes(conn, sc->root,
7147 XCB_CW_EVENT_MASK, &val);
7152 new_region(struct swm_screen *s, int x, int y, int w, int h)
7154 struct swm_region *r, *n;
7155 struct workspace *ws = NULL;
7158 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7159 s->idx, w, h, x, y);
7161 /* remove any conflicting regions */
7162 n = TAILQ_FIRST(&s->rl);
7165 n = TAILQ_NEXT(r, entry);
7166 if (X(r) < (x + w) &&
7167 (X(r) + WIDTH(r)) > x &&
7169 (Y(r) + HEIGHT(r)) > y) {
7170 if (r->ws->r != NULL)
7171 r->ws->old_r = r->ws->r;
7174 TAILQ_REMOVE(&s->rl, r, entry);
7175 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7179 /* search old regions for one to reuse */
7181 /* size + location match */
7182 TAILQ_FOREACH(r, &s->orl, entry)
7183 if (X(r) == x && Y(r) == y &&
7184 HEIGHT(r) == h && WIDTH(r) == w)
7188 TAILQ_FOREACH(r, &s->orl, entry)
7189 if (HEIGHT(r) == h && WIDTH(r) == w)
7193 TAILQ_REMOVE(&s->orl, r, entry);
7194 /* try to use old region's workspace */
7195 if (r->ws->r == NULL)
7198 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7199 err(1, "new_region: calloc: failed to allocate memory "
7202 /* if we don't have a workspace already, find one */
7204 for (i = 0; i < workspace_limit; i++)
7205 if (s->ws[i].r == NULL) {
7212 errx(1, "new_region: no free workspaces");
7223 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7229 #ifdef SWM_XRR_HAS_CRTC
7232 #endif /* SWM_XRR_HAS_CRTC */
7233 struct swm_region *r;
7235 xcb_randr_get_screen_resources_current_cookie_t src;
7236 xcb_randr_get_screen_resources_current_reply_t *srr;
7237 xcb_randr_get_crtc_info_cookie_t cic;
7238 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7239 xcb_randr_crtc_t *crtc;
7240 xcb_screen_t *screen = get_screen(i);
7242 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7243 if (i >= num_screens)
7244 errx(1, "scan_xrandr: invalid screen");
7246 /* remove any old regions */
7247 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7248 r->ws->old_r = r->ws->r = NULL;
7250 TAILQ_REMOVE(&screens[i].rl, r, entry);
7251 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7255 /* map virtual screens onto physical screens */
7256 #ifdef SWM_XRR_HAS_CRTC
7257 if (xrandr_support) {
7258 src = xcb_randr_get_screen_resources_current(conn,
7260 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7263 new_region(&screens[i], 0, 0,
7264 screen->width_in_pixels,
7265 screen->height_in_pixels);
7268 ncrtc = srr->num_crtcs;
7269 for (c = 0; c < ncrtc; c++) {
7270 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7271 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7273 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7276 if (cir->num_outputs == 0) {
7282 new_region(&screens[i], 0, 0,
7283 screen->width_in_pixels,
7284 screen->height_in_pixels);
7286 new_region(&screens[i],
7287 cir->x, cir->y, cir->width, cir->height);
7292 #endif /* SWM_XRR_HAS_CRTC */
7294 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7295 screen->height_in_pixels);
7300 screenchange(xcb_randr_screen_change_notify_event_t *e)
7302 struct swm_region *r;
7305 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7307 if (e->rotation & (XCB_RANDR_ROTATION_ROTATE_90
7308 | XCB_RANDR_ROTATION_ROTATE_270))
7309 xcb_randr_set_screen_size(conn, e->root, e->height,
7310 e->width, e->mheight, e->mwidth);
7312 xcb_randr_set_screen_size(conn, e->root, e->width,
7313 e->height, e->mwidth, e->mheight);
7315 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7316 /* silly event doesn't include the screen index */
7317 for (i = 0; i < num_screens; i++)
7318 if (screens[i].root == e->root)
7320 if (i >= num_screens)
7321 errx(1, "screenchange: screen not found");
7323 /* brute force for now, just re-enumerate the regions */
7326 /* add bars to all regions */
7327 for (i = 0; i < num_screens; i++)
7328 TAILQ_FOREACH(r, &screens[i].rl, entry)
7336 xcb_window_t *wins = NULL;
7338 int i, j, num_screens;
7339 uint16_t state, manage;
7341 xcb_query_tree_cookie_t qtc;
7342 xcb_query_tree_reply_t *qtr;
7343 xcb_get_window_attributes_cookie_t c;
7344 xcb_get_window_attributes_reply_t *r;
7345 xcb_get_property_cookie_t pc;
7347 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7348 for (i = 0; i < num_screens; i++) {
7349 qtc = xcb_query_tree(conn, screens[i].root);
7350 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7353 wins = xcb_query_tree_children(qtr);
7354 no = xcb_query_tree_children_length(qtr);
7355 /* attach windows to a region */
7356 /* normal windows */
7357 for (j = 0; j < no; j++) {
7358 c = xcb_get_window_attributes(conn, wins[j]);
7359 r = xcb_get_window_attributes_reply(conn, c, NULL);
7362 if (r->override_redirect) {
7367 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7368 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7374 state = getstate(wins[j]);
7375 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7376 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7377 manage_window(wins[j]);
7380 /* transient windows */
7381 for (j = 0; j < no; j++) {
7382 c = xcb_get_window_attributes(conn, wins[j]);
7383 r = xcb_get_window_attributes_reply(conn, c, NULL);
7386 if (r->override_redirect) {
7392 state = getstate(wins[j]);
7393 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7394 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7395 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7396 &wins[j], NULL) && manage)
7397 manage_window(wins[j]);
7406 int i, j, k, num_screens;
7407 struct workspace *ws;
7408 uint32_t gcv[1], wa[1];
7409 const xcb_query_extension_reply_t *qep;
7410 xcb_cursor_t cursor;
7411 xcb_font_t cursor_font;
7412 xcb_randr_query_version_cookie_t c;
7413 xcb_randr_query_version_reply_t *r;
7415 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7416 if ((screens = calloc(num_screens,
7417 sizeof(struct swm_screen))) == NULL)
7418 err(1, "setup_screens: calloc: failed to allocate memory for "
7421 /* initial Xrandr setup */
7422 xrandr_support = False;
7423 c = xcb_randr_query_version(conn, True, True);
7424 r = xcb_randr_query_version_reply(conn, c, NULL);
7426 if (r->major_version >= 1)
7427 xrandr_support = True;
7430 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7431 xrandr_eventbase = qep->first_event;
7433 cursor_font = xcb_generate_id(conn);
7434 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7436 cursor = xcb_generate_id(conn);
7437 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7438 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7441 /* map physical screens */
7442 for (i = 0; i < num_screens; i++) {
7443 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7445 TAILQ_INIT(&screens[i].rl);
7446 TAILQ_INIT(&screens[i].orl);
7447 screens[i].root = get_screen(i)->root;
7449 /* set default colors */
7450 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7451 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7452 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7453 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7454 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7456 /* create graphics context on screen */
7457 screens[i].bar_gc = xcb_generate_id(conn);
7459 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7460 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7462 /* set default cursor */
7463 xcb_change_window_attributes(conn, screens[i].root,
7466 /* init all workspaces */
7467 /* XXX these should be dynamically allocated too */
7468 for (j = 0; j < SWM_WS_MAX; j++) {
7469 ws = &screens[i].ws[j];
7475 TAILQ_INIT(&ws->winlist);
7476 TAILQ_INIT(&ws->unmanagedlist);
7478 for (k = 0; layouts[k].l_stack != NULL; k++)
7479 if (layouts[k].l_config != NULL)
7480 layouts[k].l_config(ws,
7481 SWM_ARG_ID_STACKINIT);
7482 ws->cur_layout = &layouts[0];
7483 ws->cur_layout->l_string(ws);
7489 xcb_randr_select_input(conn, screens[i].root,
7490 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7492 xcb_free_cursor(conn, cursor);
7493 xcb_close_font(conn, cursor_font);
7499 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7500 err(1, "setup_globals: strdup: failed to allocate memory.");
7502 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7503 err(1, "setup_globals: strdup: failed to allocate memory.");
7505 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7506 errx(1, "unable to allocate key symbols");
7508 a_state = get_atom_from_string("WM_STATE");
7509 a_prot = get_atom_from_string("WM_PROTOCOLS");
7510 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7511 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7512 a_wmname = get_atom_from_string("WM_NAME");
7513 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7514 a_utf8_string = get_atom_from_string("UTF8_STRING");
7515 a_string = get_atom_from_string("STRING");
7516 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
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;
7627 time_started = time(NULL);
7630 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7631 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7633 warnx("no locale support");
7635 if (!X_HAVE_UTF8_STRING)
7636 warnx("no UTF-8 support");
7638 if (!(display = XOpenDisplay(0)))
7639 errx(1, "can not open display");
7641 /* handle some signals */
7642 bzero(&sact, sizeof(sact));
7643 sigemptyset(&sact.sa_mask);
7645 sact.sa_handler = sighdlr;
7646 sigaction(SIGINT, &sact, NULL);
7647 sigaction(SIGQUIT, &sact, NULL);
7648 sigaction(SIGTERM, &sact, NULL);
7649 sigaction(SIGHUP, &sact, NULL);
7651 sact.sa_handler = sighdlr;
7652 sact.sa_flags = SA_NOCLDSTOP;
7653 sigaction(SIGCHLD, &sact, NULL);
7655 conn = XGetXCBConnection(display);
7656 if (xcb_connection_has_error(conn))
7657 errx(1, "can not get XCB connection");
7659 xfd = xcb_get_file_descriptor(conn);
7661 /* look for local and global conf file */
7662 pwd = getpwuid(getuid());
7664 errx(1, "invalid user: %d", getuid());
7666 xcb_grab_server(conn);
7669 /* flush all events */
7670 while ((evt = xcb_poll_for_event(conn))) {
7671 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7679 if (xcb_poll_for_event(conn) != NULL)
7680 errx(1, "another window manager is currently running");
7689 for (i = 0; ; i++) {
7694 snprintf(conf, sizeof conf, "%s/.%s",
7695 pwd->pw_dir, SWM_CONF_FILE);
7699 snprintf(conf, sizeof conf, "/etc/%s",
7704 snprintf(conf, sizeof conf, "%s/.%s",
7705 pwd->pw_dir, SWM_CONF_FILE_OLD);
7709 snprintf(conf, sizeof conf, "/etc/%s",
7716 if (strlen(conf) && stat(conf, &sb) != -1)
7717 if (S_ISREG(sb.st_mode)) {
7724 /* load conf (if any) */
7726 conf_load(cfile, SWM_CONF_DEFAULT);
7729 /* set some values to work around bad programs */
7731 /* grab existing windows (before we build the bars) */
7734 if (getenv("SWM_STARTED") == NULL)
7735 setenv("SWM_STARTED", "YES", 1);
7737 /* setup all bars */
7738 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7739 for (i = 0; i < num_screens; i++)
7740 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7741 if (winfocus == NULL)
7742 winfocus = TAILQ_FIRST(&r->ws->winlist);
7751 xcb_ungrab_server(conn);
7755 while ((evt = xcb_poll_for_event(conn))) {
7762 /* if we are being restarted go focus on first window */
7764 rr = winfocus->ws->r;
7766 /* not a visible window */
7770 /* move pointer to first screen if multi screen */
7771 if (num_screens > 1 || outputs > 1)
7772 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7773 rr->s[0].root, 0, 0, 0, 0, X(rr),
7774 Y(rr) + (bar_enabled ? bar_height : 0));
7776 a.id = SWM_ARG_ID_FOCUSCUR;
7786 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7788 DNPRINTF(SWM_D_MISC, "select failed");
7789 if (restart_wm == 1)
7790 restart(NULL, NULL);
7791 if (search_resp == 1)
7804 for (i = 0; i < num_screens; ++i)
7805 if (screens[i].bar_gc != 0)
7806 xcb_free_gc(conn, screens[i].bar_gc);
7808 XFreeFontSet(display, bar_fs);
7810 xcb_key_symbols_free(syms);
7811 xcb_disconnect(conn);