2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <X11/Xproto.h>
102 #include <X11/Xutil.h>
103 #include <X11/extensions/Xrandr.h>
104 #include <X11/extensions/XTest.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr = SPECTRWM_BUILDSTR;
115 static const char *buildstr = SPECTRWM_VERSION;
119 # error XRandR versions less than 1.0 are not supported
124 #define SWM_XRR_HAS_CRTC
128 #if defined(__OpenBSD__)
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
133 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
134 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
135 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
136 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
137 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
138 #define xcb_icccm_get_wm_class xcb_get_wm_class
139 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
140 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
141 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
142 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
143 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
144 #define xcb_icccm_get_wm_name xcb_get_wm_name
145 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
146 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
147 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
148 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
149 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
150 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
151 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
152 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
153 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
154 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
159 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
160 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
161 #define SWM_D_MISC 0x0001
162 #define SWM_D_EVENT 0x0002
163 #define SWM_D_WS 0x0004
164 #define SWM_D_FOCUS 0x0008
165 #define SWM_D_MOVE 0x0010
166 #define SWM_D_STACK 0x0020
167 #define SWM_D_MOUSE 0x0040
168 #define SWM_D_PROP 0x0080
169 #define SWM_D_CLASS 0x0100
170 #define SWM_D_KEY 0x0200
171 #define SWM_D_QUIRK 0x0400
172 #define SWM_D_SPAWN 0x0800
173 #define SWM_D_EVENTQ 0x1000
174 #define SWM_D_CONF 0x2000
175 #define SWM_D_BAR 0x4000
177 u_int32_t swm_debug = 0
195 #define DPRINTF(x...)
196 #define DNPRINTF(n,x...)
199 #define LENGTH(x) (sizeof x / sizeof x[0])
200 #define MODKEY Mod1Mask
201 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
202 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
203 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
204 #define SWM_PROPLEN (16)
205 #define SWM_FUNCNAME_LEN (32)
206 #define SWM_KEYS_LEN (255)
207 #define SWM_QUIRK_LEN (64)
208 #define X(r) (r)->g.x
209 #define Y(r) (r)->g.y
210 #define WIDTH(r) (r)->g.w
211 #define HEIGHT(r) (r)->g.h
212 #define BORDER(w) (w->bordered ? border_width : 0)
213 #define MAX_X(r) ((r)->g.x + (r)->g.w)
214 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
215 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
216 #define SH_MIN_W(w) (w)->sh.min_width
217 #define SH_MIN_H(w) (w)->sh.min_height
218 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
219 #define SH_MAX_W(w) (w)->sh.max_width
220 #define SH_MAX_H(w) (w)->sh.max_height
221 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
222 #define SH_INC_W(w) (w)->sh.width_inc
223 #define SH_INC_H(w) (w)->sh.height_inc
224 #define SWM_MAX_FONT_STEPS (3)
225 #define WINID(w) ((w) ? (w)->id : 0)
226 #define YESNO(x) ((x) ? "yes" : "no")
228 #define SWM_FOCUS_DEFAULT (0)
229 #define SWM_FOCUS_SYNERGY (1)
230 #define SWM_FOCUS_FOLLOW (2)
232 #define SWM_CONF_DEFAULT (0)
233 #define SWM_CONF_KEYMAPPING (1)
236 #define SWM_LIB "/usr/local/lib/libswmhack.so"
243 xcb_atom_t takefocus;
245 xcb_atom_t a_netwmname;
246 xcb_atom_t a_utf8_string;
248 xcb_atom_t a_swm_iconic;
249 volatile sig_atomic_t running = 1;
250 volatile sig_atomic_t restart_wm = 0;
252 int last_focus_event = FocusOut;
253 int (*xerrorxlib)(Display *, XErrorEvent *);
257 int xrandr_eventbase;
258 unsigned int numlockmask = 0;
260 xcb_connection_t *conn;
261 xcb_key_symbols_t *syms;
264 int cycle_visible = 0;
266 int font_adjusted = 0;
267 unsigned int mod_key = MODKEY;
270 struct swm_region *search_r;
271 int select_list_pipe[2];
272 int select_resp_pipe[2];
274 volatile sig_atomic_t search_resp;
275 int search_resp_action;
277 struct search_window {
278 TAILQ_ENTRY(search_window) entry;
282 xcb_window_t indicator;
284 TAILQ_HEAD(search_winlist, search_window);
285 struct search_winlist search_wl;
290 SWM_SEARCH_UNICONIFY,
291 SWM_SEARCH_NAME_WORKSPACE,
292 SWM_SEARCH_SEARCH_WORKSPACE,
293 SWM_SEARCH_SEARCH_WINDOW
296 #define SWM_STACK_TOP (0)
297 #define SWM_STACK_BOTTOM (1)
298 #define SWM_STACK_ABOVE (2)
299 #define SWM_STACK_BELOW (3)
302 double dialog_ratio = 0.6;
304 #define SWM_BAR_MAX (256)
305 #define SWM_BAR_JUSTIFY_LEFT (0)
306 #define SWM_BAR_JUSTIFY_CENTER (1)
307 #define SWM_BAR_JUSTIFY_RIGHT (2)
308 #define SWM_BAR_OFFSET (4)
309 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
310 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
311 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
312 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
313 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
315 char *bar_argv[] = { NULL, NULL };
317 unsigned char bar_ext[SWM_BAR_MAX];
318 char bar_vertext[SWM_BAR_MAX];
320 sig_atomic_t bar_alarm = 0;
323 int bar_border_width = 1;
324 int bar_at_bottom = 0;
326 int bar_extra_running = 0;
329 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
330 char *bar_format = NULL;
331 int stack_enabled = 1;
332 int clock_enabled = 1;
333 int urgent_enabled = 0;
334 char *clock_format = NULL;
335 int title_name_enabled = 0;
336 int title_class_enabled = 0;
337 int window_name_enabled = 0;
338 int focus_mode = SWM_FOCUS_DEFAULT;
339 int focus_close = SWM_STACK_BELOW;
340 int focus_close_wrap = 1;
341 int focus_default = SWM_STACK_TOP;
342 int spawn_position = SWM_STACK_TOP;
343 int disable_border = 0;
344 int border_width = 1;
345 int verbose_layout = 0;
348 XFontSetExtents *bar_fs_extents;
352 #define SWM_MENU_FN (2)
353 #define SWM_MENU_NB (4)
354 #define SWM_MENU_NF (6)
355 #define SWM_MENU_SB (8)
356 #define SWM_MENU_SF (10)
358 /* layout manager data */
359 struct swm_geometry {
372 struct swm_geometry g;
375 /* virtual "screens" */
377 TAILQ_ENTRY(swm_region) entry;
378 struct swm_geometry g;
379 struct workspace *ws; /* current workspace on this region */
380 struct workspace *ws_prior; /* prior workspace on this region */
381 struct swm_screen *s; /* screen idx */
384 TAILQ_HEAD(swm_region_list, swm_region);
387 TAILQ_ENTRY(ws_win) entry;
389 xcb_window_t transient;
390 struct ws_win *child_trans; /* transient child window */
391 struct swm_geometry g; /* current geometry */
392 struct swm_geometry g_float; /* region coordinates */
393 int g_floatvalid; /* g_float geometry validity */
394 int floatmaxed; /* whether maxed by max_stack */
399 unsigned int ewmh_flags;
400 int font_size_boundary[SWM_MAX_FONT_STEPS];
406 unsigned long quirks;
407 struct workspace *ws; /* always valid */
408 struct swm_screen *s; /* always valid, never changes */
409 xcb_get_geometry_reply_t *wa;
411 xcb_icccm_get_wm_class_reply_t ch;
412 xcb_icccm_wm_hints_t hints;
414 TAILQ_HEAD(ws_win_list, ws_win);
418 TAILQ_ENTRY(pid_e) entry;
422 TAILQ_HEAD(pid_list, pid_e);
423 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
425 /* layout handlers */
427 void vertical_config(struct workspace *, int);
428 void vertical_stack(struct workspace *, struct swm_geometry *);
429 void horizontal_config(struct workspace *, int);
430 void horizontal_stack(struct workspace *, struct swm_geometry *);
431 void max_stack(struct workspace *, struct swm_geometry *);
432 void plain_stacker(struct workspace *);
433 void fancy_stacker(struct workspace *);
435 struct ws_win *find_window(xcb_window_t);
437 void grabbuttons(struct ws_win *, int);
438 void new_region(struct swm_screen *, int, int, int, int);
439 void unmanage_window(struct ws_win *);
440 uint16_t getstate(xcb_window_t);
442 int conf_load(char *, int);
445 void (*l_stack)(struct workspace *, struct swm_geometry *);
446 void (*l_config)(struct workspace *, int);
448 #define SWM_L_FOCUSPREV (1<<0)
449 #define SWM_L_MAPONFOCUS (1<<1)
450 void (*l_string)(struct workspace *);
452 /* stack, configure */
453 { vertical_stack, vertical_config, 0, plain_stacker },
454 { horizontal_stack, horizontal_config, 0, plain_stacker },
456 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
457 { NULL, NULL, 0, NULL },
460 /* position of max_stack mode in the layouts array, index into layouts! */
461 #define SWM_V_STACK (0)
462 #define SWM_H_STACK (1)
463 #define SWM_MAX_STACK (2)
465 #define SWM_H_SLICE (32)
466 #define SWM_V_SLICE (32)
468 /* define work spaces */
470 int idx; /* workspace index */
471 char *name; /* workspace name */
472 int always_raise; /* raise windows on focus */
473 struct layout *cur_layout; /* current layout handlers */
474 struct ws_win *focus; /* may be NULL */
475 struct ws_win *focus_prev; /* may be NULL */
476 struct swm_region *r; /* may be NULL */
477 struct swm_region *old_r; /* may be NULL */
478 struct ws_win_list winlist; /* list of windows in ws */
479 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
480 char stacker[10]; /* display stacker and layout */
484 int horizontal_msize;
486 int horizontal_stacks;
497 SWM_S_COLOR_BAR_BORDER,
498 SWM_S_COLOR_BAR_FONT,
504 /* physical screen mapping */
505 #define SWM_WS_MAX (22) /* hard limit */
506 int workspace_limit = 10; /* soft limit */
509 int idx; /* screen index */
510 struct swm_region_list rl; /* list of regions on this screen */
511 struct swm_region_list orl; /* list of old regions */
513 struct workspace ws[SWM_WS_MAX];
519 } c[SWM_S_COLOR_MAX];
521 xcb_gcontext_t bar_gc;
523 struct swm_screen *screens;
525 /* args to functions */
528 #define SWM_ARG_ID_FOCUSNEXT (0)
529 #define SWM_ARG_ID_FOCUSPREV (1)
530 #define SWM_ARG_ID_FOCUSMAIN (2)
531 #define SWM_ARG_ID_FOCUSCUR (4)
532 #define SWM_ARG_ID_SWAPNEXT (10)
533 #define SWM_ARG_ID_SWAPPREV (11)
534 #define SWM_ARG_ID_SWAPMAIN (12)
535 #define SWM_ARG_ID_MOVELAST (13)
536 #define SWM_ARG_ID_MASTERSHRINK (20)
537 #define SWM_ARG_ID_MASTERGROW (21)
538 #define SWM_ARG_ID_MASTERADD (22)
539 #define SWM_ARG_ID_MASTERDEL (23)
540 #define SWM_ARG_ID_FLIPLAYOUT (24)
541 #define SWM_ARG_ID_STACKRESET (30)
542 #define SWM_ARG_ID_STACKINIT (31)
543 #define SWM_ARG_ID_CYCLEWS_UP (40)
544 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
545 #define SWM_ARG_ID_CYCLESC_UP (42)
546 #define SWM_ARG_ID_CYCLESC_DOWN (43)
547 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
548 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
549 #define SWM_ARG_ID_STACKINC (50)
550 #define SWM_ARG_ID_STACKDEC (51)
551 #define SWM_ARG_ID_SS_ALL (60)
552 #define SWM_ARG_ID_SS_WINDOW (61)
553 #define SWM_ARG_ID_DONTCENTER (70)
554 #define SWM_ARG_ID_CENTER (71)
555 #define SWM_ARG_ID_KILLWINDOW (80)
556 #define SWM_ARG_ID_DELETEWINDOW (81)
557 #define SWM_ARG_ID_WIDTHGROW (90)
558 #define SWM_ARG_ID_WIDTHSHRINK (91)
559 #define SWM_ARG_ID_HEIGHTGROW (92)
560 #define SWM_ARG_ID_HEIGHTSHRINK (93)
561 #define SWM_ARG_ID_MOVEUP (100)
562 #define SWM_ARG_ID_MOVEDOWN (101)
563 #define SWM_ARG_ID_MOVELEFT (102)
564 #define SWM_ARG_ID_MOVERIGHT (103)
568 void focus(struct swm_region *, union arg *);
569 void focus_magic(struct ws_win *);
573 TAILQ_ENTRY(quirk) entry;
577 #define SWM_Q_FLOAT (1<<0) /* float this window */
578 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
579 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
580 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
581 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
582 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
584 TAILQ_HEAD(quirk_list, quirk);
585 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
588 * Supported EWMH hints should be added to
589 * both the enum and the ewmh array
594 _NET_MOVERESIZE_WINDOW,
595 _NET_WM_ACTION_CLOSE,
596 _NET_WM_ACTION_FULLSCREEN,
598 _NET_WM_ACTION_RESIZE,
599 _NET_WM_ALLOWED_ACTIONS,
602 _NET_WM_STATE_FULLSCREEN,
603 _NET_WM_STATE_HIDDEN,
604 _NET_WM_STATE_MAXIMIZED_HORZ,
605 _NET_WM_STATE_MAXIMIZED_VERT,
606 _NET_WM_STATE_SKIP_PAGER,
607 _NET_WM_STATE_SKIP_TASKBAR,
609 _NET_WM_WINDOW_TYPE_DIALOG,
610 _NET_WM_WINDOW_TYPE_DOCK,
611 _NET_WM_WINDOW_TYPE_NORMAL,
612 _NET_WM_WINDOW_TYPE_SPLASH,
613 _NET_WM_WINDOW_TYPE_TOOLBAR,
614 _NET_WM_WINDOW_TYPE_UTILITY,
615 _SWM_WM_STATE_MANUAL,
622 } ewmh[SWM_EWMH_HINT_MAX] = {
623 /* must be in same order as in the enum */
624 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
625 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
626 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
627 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
628 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
629 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
630 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
631 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
632 {"_NET_WM_STATE", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
637 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
638 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
639 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
644 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
645 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
646 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
647 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
650 void store_float_geom(struct ws_win *, struct swm_region *);
651 int floating_toggle_win(struct ws_win *);
652 void constrain_window(struct ws_win *, struct swm_region *, int);
653 void update_window(struct ws_win *);
654 void spawn_select(struct swm_region *, union arg *, char *, int *);
655 char *get_win_name(xcb_window_t);
656 xcb_atom_t get_atom_from_string(const char *);
657 void map_window_raised(xcb_window_t);
659 xcb_screen_t *get_screen(int);
660 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
661 void event_handle(xcb_generic_event_t *);
664 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
666 unsigned int tmpr, tmpg, tmpb;
668 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
679 get_screen(int screen)
681 xcb_screen_iterator_t i;
683 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
684 for (; i.rem; --screen, xcb_screen_next(&i))
694 xcb_get_input_focus_cookie_t c;
695 xcb_get_input_focus_reply_t *r;
698 c = xcb_get_input_focus(conn);
700 r = xcb_get_input_focus_reply(conn, c, NULL);
706 map_window_raised(xcb_window_t win)
708 uint32_t val = XCB_STACK_MODE_ABOVE;
710 xcb_configure_window(conn, win,
711 XCB_CONFIG_WINDOW_STACK_MODE, &val);
713 xcb_map_window(conn, win);
718 get_atom_from_string(const char *str)
720 xcb_intern_atom_cookie_t c;
721 xcb_intern_atom_reply_t *r;
724 c = xcb_intern_atom(conn, False, strlen(str), str);
725 r = xcb_intern_atom_reply(conn, c, NULL);
733 return (XCB_ATOM_NONE);
737 update_iconic(struct ws_win *win, int newv)
744 iprop = get_atom_from_string("_SWM_ICONIC");
745 if (iprop == XCB_ATOM_NONE)
749 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
750 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
752 xcb_delete_property(conn, win->id, iprop);
756 get_iconic(struct ws_win *win)
760 xcb_get_property_reply_t *pr = NULL;
762 iprop = get_atom_from_string("_SWM_ICONIC");
763 if (iprop == XCB_ATOM_NONE)
766 pr = xcb_get_property_reply(conn,
767 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
772 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
774 v = *((int32_t *)xcb_get_property_value(pr));
785 int i, j, num_screens;
787 sup_list = get_atom_from_string("_NET_SUPPORTED");
789 for (i = 0; i < LENGTH(ewmh); i++)
790 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
792 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
793 for (i = 0; i < num_screens; i++) {
794 /* Support check window will be created by workaround(). */
796 /* Report supported atoms */
797 xcb_delete_property(conn, screens[i].root, sup_list);
798 for (j = 0; j < LENGTH(ewmh); j++)
799 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
800 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
809 xcb_atom_t sup_check, sup_list;
811 xcb_get_property_cookie_t pc;
812 xcb_get_property_reply_t *pr;
814 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
815 sup_list = get_atom_from_string("_NET_SUPPORTED");
816 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
818 for (i = 0; i < num_screens; i++) {
819 /* Get the support check window and destroy it */
820 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
821 XCB_ATOM_WINDOW, 0, 1);
822 pr = xcb_get_property_reply(conn, pc, NULL);
824 id = *((xcb_window_t *)xcb_get_property_value(pr));
826 xcb_destroy_window(conn, id);
827 xcb_delete_property(conn, screens[i].root, sup_check);
828 xcb_delete_property(conn, screens[i].root, sup_list);
836 ewmh_autoquirk(struct ws_win *win)
842 xcb_get_property_cookie_t c;
843 xcb_get_property_reply_t *r;
845 c = xcb_get_property(conn, False, win->id,
846 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
847 r = xcb_get_property_reply(conn, c, NULL);
850 n = xcb_get_property_value_length(r);
852 for (i = 0; i < n; i++) {
853 type = *((xcb_atom_t *)xcb_get_property_value(r));
854 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
856 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
857 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
858 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
860 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
863 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
864 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
866 win->quirks = SWM_Q_FLOAT;
873 #define SWM_EWMH_ACTION_COUNT_MAX (6)
874 #define EWMH_F_FULLSCREEN (1<<0)
875 #define EWMH_F_ABOVE (1<<1)
876 #define EWMH_F_HIDDEN (1<<2)
877 #define EWMH_F_SKIP_PAGER (1<<3)
878 #define EWMH_F_SKIP_TASKBAR (1<<4)
879 #define SWM_F_MANUAL (1<<5)
882 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
890 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
891 "fullscreen %s\n", win->id, YESNO(fs));
894 if (!win->g_floatvalid)
895 store_float_geom(win, win->ws->r);
897 win->g = win->ws->r->g;
900 if (win->g_floatvalid) {
901 /* refloat at last floating relative position */
902 win->g = win->g_float;
903 X(win) += X(win->ws->r);
904 Y(win) += Y(win->ws->r);
912 ewmh_update_actions(struct ws_win *win)
914 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
920 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
923 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
924 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
927 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
928 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1,
932 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
933 #define _NET_WM_STATE_ADD 1 /* add/set property */
934 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
937 ewmh_update_win_state(struct ws_win *win, long state, long action)
939 unsigned int mask = 0;
940 unsigned int changed = 0;
941 unsigned int orig_flags;
946 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
947 mask = EWMH_F_FULLSCREEN;
948 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
950 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
952 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
953 mask = EWMH_F_SKIP_PAGER;
954 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
955 mask = EWMH_F_SKIP_TASKBAR;
958 orig_flags = win->ewmh_flags;
961 case _NET_WM_STATE_REMOVE:
962 win->ewmh_flags &= ~mask;
964 case _NET_WM_STATE_ADD:
965 win->ewmh_flags |= mask;
967 case _NET_WM_STATE_TOGGLE:
968 win->ewmh_flags ^= mask;
972 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
974 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
976 if (!floating_toggle_win(win))
977 win->ewmh_flags = orig_flags; /* revert */
978 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
980 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
981 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
983 if (!ewmh_set_win_fullscreen(win,
984 win->ewmh_flags & EWMH_F_FULLSCREEN))
985 win->ewmh_flags = orig_flags; /* revert */
987 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
989 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
990 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
991 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
992 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
993 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
994 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
995 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
996 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
997 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
998 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
999 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1000 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1001 if (win->ewmh_flags & EWMH_F_ABOVE)
1002 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1003 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1004 &ewmh[_NET_WM_STATE_ABOVE].atom);
1005 if (win->ewmh_flags & SWM_F_MANUAL)
1006 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1007 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1008 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1012 ewmh_get_win_state(struct ws_win *win)
1015 xcb_get_property_cookie_t c;
1016 xcb_get_property_reply_t *r;
1022 win->ewmh_flags = 0;
1024 win->ewmh_flags |= EWMH_F_ABOVE;
1026 win->ewmh_flags |= SWM_F_MANUAL;
1028 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1029 XCB_ATOM_ATOM, 0, (~0L));
1030 r = xcb_get_property_reply(conn, c, NULL);
1034 states = xcb_get_property_value(r);
1035 n = xcb_get_property_value_length(r);
1037 for (i = 0; i < n; i++)
1038 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1046 geteventname(xcb_generic_event_t *e)
1050 switch (XCB_EVENT_RESPONSE_TYPE(e)) {
1054 case XCB_KEY_RELEASE:
1055 name = "KeyRelease";
1057 case XCB_BUTTON_PRESS:
1058 name = "ButtonPress";
1060 case XCB_BUTTON_RELEASE:
1061 name = "ButtonRelease";
1063 case XCB_MOTION_NOTIFY:
1064 name = "MotionNotify";
1066 case XCB_ENTER_NOTIFY:
1067 name = "EnterNotify";
1069 case XCB_LEAVE_NOTIFY:
1070 name = "LeaveNotify";
1078 case XCB_KEYMAP_NOTIFY:
1079 name = "KeymapNotify";
1084 case XCB_GRAPHICS_EXPOSURE:
1085 name = "GraphicsExposure";
1087 case XCB_NO_EXPOSURE:
1088 name = "NoExposure";
1090 case XCB_VISIBILITY_NOTIFY:
1091 name = "VisibilityNotify";
1093 case XCB_CREATE_NOTIFY:
1094 name = "CreateNotify";
1096 case XCB_DESTROY_NOTIFY:
1097 name = "DestroyNotify";
1099 case XCB_UNMAP_NOTIFY:
1100 name = "UnmapNotify";
1102 case XCB_MAP_NOTIFY:
1105 case XCB_MAP_REQUEST:
1106 name = "MapRequest";
1108 case XCB_REPARENT_NOTIFY:
1109 name = "ReparentNotify";
1111 case XCB_CONFIGURE_NOTIFY:
1112 name = "ConfigureNotify";
1114 case XCB_CONFIGURE_REQUEST:
1115 name = "ConfigureRequest";
1117 case XCB_GRAVITY_NOTIFY:
1118 name = "GravityNotify";
1120 case XCB_RESIZE_REQUEST:
1121 name = "ResizeRequest";
1123 case XCB_CIRCULATE_NOTIFY:
1124 name = "CirculateNotify";
1126 case XCB_CIRCULATE_REQUEST:
1127 name = "CirculateRequest";
1129 case XCB_PROPERTY_NOTIFY:
1130 name = "PropertyNotify";
1132 case XCB_SELECTION_CLEAR:
1133 name = "SelectionClear";
1135 case XCB_SELECTION_REQUEST:
1136 name = "SelectionRequest";
1138 case XCB_SELECTION_NOTIFY:
1139 name = "SelectionNotify";
1141 case XCB_COLORMAP_NOTIFY:
1142 name = "ColormapNotify";
1144 case XCB_CLIENT_MESSAGE:
1145 name = "ClientMessage";
1147 case XCB_MAPPING_NOTIFY:
1148 name = "MappingNotify";
1158 dumpwins(struct swm_region *r, union arg *args)
1162 xcb_get_window_attributes_cookie_t c;
1163 xcb_get_window_attributes_reply_t *wa;
1165 if (r->ws == NULL) {
1166 warnx("dumpwins: invalid workspace");
1170 warnx("=== managed window list ws %02d ===", r->ws->idx);
1171 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1172 state = getstate(win->id);
1173 c = xcb_get_window_attributes(conn, win->id);
1174 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1176 warnx("window: 0x%x, map_state: %d, state: %u, "
1177 "transient: 0x%x", win->id, wa->map_state,
1178 state, win->transient);
1181 warnx("window: 0x%x, failed xcb_get_window_attributes",
1185 warnx("===== unmanaged window list =====");
1186 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1187 state = getstate(win->id);
1188 c = xcb_get_window_attributes(conn, win->id);
1189 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1191 warnx("window: 0x%x, map_state: %d, state: %u, "
1192 "transient: 0x%x", win->id, wa->map_state,
1193 state, win->transient);
1196 warnx("window: 0x%x, failed xcb_get_window_attributes",
1200 warnx("=================================");
1204 dumpwins(struct swm_region *r, union arg *args)
1207 #endif /* SWM_DEBUG */
1209 void expose(xcb_expose_event_t *);
1210 void keypress(xcb_key_press_event_t *);
1211 void buttonpress(xcb_button_press_event_t *);
1212 void configurerequest(xcb_configure_request_event_t *);
1213 void configurenotify(xcb_configure_notify_event_t *);
1214 void destroynotify(xcb_destroy_notify_event_t *);
1215 void enternotify(xcb_enter_notify_event_t *);
1216 void mapnotify(xcb_map_notify_event_t *);
1217 void mappingnotify(xcb_mapping_notify_event_t *);
1218 void maprequest(xcb_map_request_event_t *);
1219 void propertynotify(xcb_property_notify_event_t *);
1220 void unmapnotify(xcb_unmap_notify_event_t *);
1221 void visibilitynotify(xcb_visibility_notify_event_t *);
1222 void clientmessage(xcb_client_message_event_t *);
1223 void screenchange(xcb_randr_screen_change_notify_event_t *);
1228 int saved_errno, status;
1231 saved_errno = errno;
1235 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1240 if (errno != ECHILD)
1241 warn("sighdlr: waitpid");
1242 #endif /* SWM_DEBUG */
1245 if (pid == searchpid)
1249 if (WIFEXITED(status)) {
1250 if (WEXITSTATUS(status) != 0)
1251 warnx("sighdlr: child exit status: %d",
1252 WEXITSTATUS(status));
1254 warnx("sighdlr: child is terminated "
1256 #endif /* SWM_DEBUG */
1270 errno = saved_errno;
1276 struct pid_e *p = NULL;
1278 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1283 TAILQ_FOREACH(p, &pidlist, entry) {
1292 name_to_color(const char *colorname)
1294 uint32_t result = 0;
1295 char cname[32] = "#";
1296 xcb_screen_t *screen;
1297 xcb_colormap_t cmap;
1298 xcb_alloc_color_reply_t *cr;
1299 xcb_alloc_named_color_reply_t *nr;
1300 uint16_t rr, gg, bb;
1302 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1303 cmap = screen->default_colormap;
1305 /* color is in format rgb://rr/gg/bb */
1306 if (strncmp(colorname, "rgb:", 4) == 0) {
1307 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1308 warnx("could not parse rgb %s", colorname);
1310 cr = xcb_alloc_color_reply(conn,
1311 xcb_alloc_color(conn, cmap, rr, gg, bb),
1317 warnx("color '%s' not found", colorname);
1320 nr = xcb_alloc_named_color_reply(conn,
1321 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1325 strlcat(cname, colorname + 2, sizeof cname - 1);
1326 nr = xcb_alloc_named_color_reply(conn,
1327 xcb_alloc_named_color(conn, cmap, strlen(cname),
1335 warnx("color '%s' not found", colorname);
1342 setscreencolor(char *val, int i, int c)
1346 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1347 if (i > 0 && i <= num_screens) {
1348 screens[i - 1].c[c].color = name_to_color(val);
1349 free(screens[i - 1].c[c].name);
1350 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1352 } else if (i == -1) {
1353 for (i = 0; i < num_screens; i++) {
1354 screens[i].c[c].color = name_to_color(val);
1355 free(screens[i].c[c].name);
1356 if ((screens[i].c[c].name = strdup(val)) == NULL)
1360 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1365 fancy_stacker(struct workspace *ws)
1367 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1368 if (ws->cur_layout->l_stack == vertical_stack)
1369 snprintf(ws->stacker, sizeof ws->stacker,
1370 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1371 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1372 if (ws->cur_layout->l_stack == horizontal_stack)
1373 snprintf(ws->stacker, sizeof ws->stacker,
1374 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1375 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1379 plain_stacker(struct workspace *ws)
1381 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1382 if (ws->cur_layout->l_stack == vertical_stack)
1383 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1384 sizeof ws->stacker);
1385 if (ws->cur_layout->l_stack == horizontal_stack)
1386 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1387 sizeof ws->stacker);
1391 custom_region(char *val)
1393 unsigned int sidx, x, y, w, h;
1395 xcb_screen_t *screen;
1397 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1398 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1399 errx(1, "invalid custom region, "
1400 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1401 if (sidx < 1 || sidx > num_screens)
1402 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1406 screen = get_screen(sidx);
1408 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1410 if (x > screen->width_in_pixels ||
1411 y > screen->height_in_pixels ||
1412 w + x > screen->width_in_pixels ||
1413 h + y > screen->height_in_pixels) {
1414 warnx("ignoring region %ux%u+%u+%u - not within screen "
1415 "boundaries (%ux%u)", w, h, x, y,
1416 screen->width_in_pixels, screen->height_in_pixels);
1420 new_region(&screens[sidx], x, y, w, h);
1424 socket_setnonblock(int fd)
1428 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1429 err(1, "fcntl F_GETFL");
1430 flags |= O_NONBLOCK;
1431 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1432 err(1, "fcntl F_SETFL");
1436 bar_print(struct swm_region *r, const char *s)
1440 xcb_rectangle_t rect;
1442 XRectangle ibox, lbox;
1445 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1447 switch (bar_justify) {
1448 case SWM_BAR_JUSTIFY_LEFT:
1451 case SWM_BAR_JUSTIFY_CENTER:
1452 x = (WIDTH(r) - lbox.width) / 2;
1454 case SWM_BAR_JUSTIFY_RIGHT:
1455 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1459 if (x < SWM_BAR_OFFSET)
1464 rect.width = WIDTH(r->bar);
1465 rect.height = HEIGHT(r->bar);
1467 /* clear back buffer */
1468 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1469 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1470 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1471 sizeof(rect), &rect);
1473 /* draw back buffer */
1474 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1475 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1476 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1477 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1478 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1479 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1483 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1484 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1488 bar_extra_stop(void)
1492 bzero(bar_pipe, sizeof bar_pipe);
1495 kill(bar_pid, SIGTERM);
1498 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1503 bar_class_name(char *s, size_t sz, struct swm_region *r)
1505 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1507 if (r->ws->focus->ch.class_name != NULL)
1508 strlcat(s, r->ws->focus->ch.class_name, sz);
1512 bar_title_name(char *s, size_t sz, struct swm_region *r)
1514 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1516 if (r->ws->focus->ch.instance_name != NULL)
1517 strlcat(s, r->ws->focus->ch.instance_name, sz);
1521 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1523 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1526 bar_class_name(s, sz, r);
1527 strlcat(s, ":", sz);
1528 bar_title_name(s, sz, r);
1532 bar_window_float(char *s, size_t sz, struct swm_region *r)
1534 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1536 if (r->ws->focus->floating)
1537 strlcat(s, "(f)", sz);
1541 bar_window_name(char *s, size_t sz, struct swm_region *r)
1545 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1547 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1550 strlcat(s, title, sz);
1554 int urgent[SWM_WS_MAX];
1556 bar_urgent(char *s, size_t sz)
1559 int i, j, num_screens;
1561 xcb_get_property_cookie_t c;
1562 xcb_icccm_wm_hints_t hints;
1564 for (i = 0; i < workspace_limit; i++)
1567 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1568 for (i = 0; i < num_screens; i++)
1569 for (j = 0; j < workspace_limit; j++)
1570 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1571 c = xcb_icccm_get_wm_hints(conn, win->id);
1572 if (xcb_icccm_get_wm_hints_reply(conn, c,
1575 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1579 for (i = 0; i < workspace_limit; i++) {
1581 snprintf(b, sizeof b, "%d ", i + 1);
1583 snprintf(b, sizeof b, "- ");
1589 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1591 if (r == NULL || r->ws == NULL)
1593 if (r->ws->name != NULL)
1594 strlcat(s, r->ws->name, sz);
1597 /* build the default bar format according to the defined enabled options */
1599 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1601 /* if format provided, just copy the buffers */
1602 if (bar_format != NULL) {
1603 strlcpy(fmtnew, fmtexp, sz);
1607 /* reset the output buffer */
1610 strlcat(fmtnew, "+N:+I ", sz);
1612 strlcat(fmtnew, "+S", sz);
1613 strlcat(fmtnew, " ", sz);
1615 /* only show the workspace name if there's actually one */
1616 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1617 strlcat(fmtnew, "<+D>", sz);
1618 strlcat(fmtnew, "+3<", sz);
1620 if (clock_enabled) {
1621 strlcat(fmtnew, fmtexp, sz);
1622 strlcat(fmtnew, "+4<", sz);
1625 /* bar_urgent already adds the space before the last asterisk */
1627 strlcat(fmtnew, "* +U*+4<", sz);
1629 if (title_class_enabled) {
1630 strlcat(fmtnew, "+C", sz);
1631 if (title_name_enabled == 0)
1632 strlcat(fmtnew, "+4<", sz);
1635 /* checks needed by the colon and floating strlcat(3) calls below */
1636 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1637 if (title_name_enabled) {
1638 if (title_class_enabled)
1639 strlcat(fmtnew, ":", sz);
1640 strlcat(fmtnew, "+T+4<", sz);
1642 if (window_name_enabled) {
1643 if (r->ws->focus->floating)
1644 strlcat(fmtnew, "+F ", sz);
1645 strlcat(fmtnew, "+64W ", sz);
1649 /* finally add the action script output and the version */
1650 strlcat(fmtnew, "+4<+A+4<+V", sz);
1654 bar_replace_pad(char *tmp, int *limit, size_t sz)
1656 /* special case; no limit given, pad one space, instead */
1657 if (*limit == sz - 1)
1659 snprintf(tmp, sz, "%*s", *limit, " ");
1662 /* replaces the bar format character sequences (like in tmux(1)) */
1664 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1668 char tmp[SWM_BAR_MAX];
1672 /* reset strlcat(3) buffer */
1675 /* get number, if any */
1678 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1679 limit = sizeof tmp - 1;
1680 if (limit <= 0 || limit >= sizeof tmp)
1681 limit = sizeof tmp - 1;
1683 /* there is nothing to replace (ie EOL) */
1690 bar_replace_pad(tmp, &limit, sizeof tmp);
1693 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1696 bar_class_name(tmp, sizeof tmp, r);
1699 bar_workspace_name(tmp, sizeof tmp, r);
1702 bar_window_float(tmp, sizeof tmp, r);
1705 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1708 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1711 bar_class_title_name(tmp, sizeof tmp, r);
1714 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1717 bar_title_name(tmp, sizeof tmp, r);
1720 bar_urgent(tmp, sizeof tmp);
1723 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1726 bar_window_name(tmp, sizeof tmp, r);
1729 /* unknown character sequence; copy as-is */
1730 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1738 while (limit-- > 0) {
1739 if (*offrep >= sz - 1)
1741 fmtrep[(*offrep)++] = *ptr++;
1749 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1754 while (*fmt != '\0') {
1756 /* skip ordinary characters */
1759 fmtrep[off++] = *fmt++;
1763 /* character sequence found; replace it */
1764 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1773 bar_fmt_expand(char *fmtexp, size_t sz)
1780 /* start by grabbing the current time and date */
1782 localtime_r(&tmt, &tm);
1784 /* figure out what to expand */
1785 if (bar_format != NULL)
1787 else if (bar_format == NULL && clock_enabled)
1789 /* if nothing to expand bail out */
1795 /* copy as-is, just in case the format shouldn't be expanded below */
1796 strlcpy(fmtexp, fmt, sz);
1797 /* finally pass the string through strftime(3) */
1798 #ifndef SWM_DENY_CLOCK_FORMAT
1799 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1800 warnx("format too long");
1808 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1809 char fmtrep[SWM_BAR_MAX];
1811 struct swm_region *r;
1813 /* expand the format by first passing it through strftime(3) */
1814 bar_fmt_expand(fmtexp, sizeof fmtexp);
1816 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1817 for (i = 0; i < num_screens; i++) {
1818 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1821 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1822 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1823 bar_print(r, fmtrep);
1834 if (bar_enabled == 0)
1836 if (bar_extra && bar_extra_running) {
1837 /* ignore short reads; it'll correct itself */
1838 while ((b = fgetln(stdin, &len)) != NULL)
1839 if (b && b[len - 1] == '\n') {
1841 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1843 if (b == NULL && errno != EAGAIN) {
1844 warn("bar_update: bar_extra failed");
1848 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1861 bar_toggle(struct swm_region *r, union arg *args)
1863 struct swm_region *tmpr;
1866 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1868 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1870 for (i = 0; i < num_screens; i++)
1871 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1873 xcb_unmap_window(conn, tmpr->bar->id);
1875 for (i = 0; i < num_screens; i++)
1876 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1878 map_window_raised(tmpr->bar->id);
1881 bar_enabled = !bar_enabled;
1884 /* must be after stack */
1891 struct swm_region *r;
1895 /* do this here because the conf file is in memory */
1896 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1897 /* launch external status app */
1898 bar_extra_running = 1;
1899 if (pipe(bar_pipe) == -1)
1900 err(1, "pipe error");
1901 socket_setnonblock(bar_pipe[0]);
1902 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1903 if (dup2(bar_pipe[0], 0) == -1)
1905 if (dup2(bar_pipe[1], 1) == -1)
1907 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1908 err(1, "could not disable SIGPIPE");
1909 switch (bar_pid = fork()) {
1911 err(1, "cannot fork");
1915 execvp(bar_argv[0], bar_argv);
1916 err(1, "%s external app failed", bar_argv[0]);
1918 default: /* parent */
1924 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1925 for (i = 0; i < num_screens; i++)
1926 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1929 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1930 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1931 xcb_change_window_attributes(conn, r->bar->id,
1932 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1938 bar_setup(struct swm_region *r)
1940 char *default_string;
1941 char **missing_charsets;
1942 int num_missing_charsets = 0;
1944 xcb_screen_t *screen = get_screen(r->s->idx);
1948 XFreeFontSet(display, bar_fs);
1952 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1953 err(1, "bar_setup: calloc: failed to allocate memory.");
1955 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1957 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1958 &num_missing_charsets, &default_string);
1960 if (num_missing_charsets > 0) {
1961 warnx("Unable to load charset(s):");
1963 for (i = 0; i < num_missing_charsets; ++i)
1964 warnx("%s", missing_charsets[i]);
1966 XFreeStringList(missing_charsets);
1968 if (strcmp(default_string, ""))
1969 warnx("Glyphs from those sets will be replaced "
1970 "by '%s'.", default_string);
1972 warnx("Glyphs from those sets won't be drawn.");
1976 errx(1, "Error creating font set structure.");
1978 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1980 bar_height = bar_fs_extents->max_logical_extent.height +
1981 2 * bar_border_width;
1987 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1988 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1989 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1991 r->bar->id = xcb_generate_id(conn);
1992 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1993 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1994 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1995 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1996 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1997 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1999 r->bar->buffer = xcb_generate_id(conn);
2000 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2001 WIDTH(r->bar), HEIGHT(r->bar));
2003 xcb_randr_select_input(conn, r->bar->id,
2004 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2007 map_window_raised(r->bar->id);
2009 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2010 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2013 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2014 err(1, "could not install bar_signal");
2019 bar_cleanup(struct swm_region *r)
2023 xcb_destroy_window(conn, r->bar->id);
2024 xcb_free_pixmap(conn, r->bar->buffer);
2030 set_win_state(struct ws_win *win, uint16_t state)
2032 uint16_t data[2] = { state, XCB_ATOM_NONE };
2034 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2039 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
2040 astate, 32, 2, data);
2044 getstate(xcb_window_t w)
2046 uint16_t result = 0;
2047 xcb_get_property_cookie_t c;
2048 xcb_get_property_reply_t *r;
2050 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
2051 r = xcb_get_property_reply(conn, c, NULL);
2054 result = *((uint16_t *)xcb_get_property_value(r));
2058 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2064 version(struct swm_region *r, union arg *args)
2066 bar_version = !bar_version;
2068 snprintf(bar_vertext, sizeof bar_vertext,
2069 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2071 strlcpy(bar_vertext, "", sizeof bar_vertext);
2076 client_msg(struct ws_win *win, xcb_atom_t a)
2078 xcb_client_message_event_t ev;
2083 bzero(&ev, sizeof ev);
2084 ev.response_type = XCB_CLIENT_MESSAGE;
2085 ev.window = win->id;
2088 ev.data.data32[0] = a;
2089 ev.data.data32[1] = XCB_CURRENT_TIME;
2091 xcb_send_event(conn, False, win->id,
2092 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2095 /* synthetic response to a ConfigureRequest when not making a change */
2097 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2099 xcb_configure_notify_event_t ce;
2104 /* send notification of unchanged state. */
2105 bzero(&ce, sizeof(ce));
2106 ce.response_type = XCB_CONFIGURE_NOTIFY;
2109 ce.width = WIDTH(win);
2110 ce.height = HEIGHT(win);
2111 ce.override_redirect = False;
2116 ce.window = win->id;
2117 ce.border_width = BORDER(win);
2118 ce.above_sibling = XCB_WINDOW_NONE;
2121 ce.event = ev->window;
2122 ce.window = ev->window;
2124 /* make response appear more WM_SIZE_HINTS-compliant */
2126 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2127 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2128 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2129 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2130 SH_INC_W(win), SH_INC_H(win));
2134 /* the hint may be set... to 0! */
2135 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2136 ce.width = SH_MIN_W(win);
2137 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2138 ce.height = SH_MIN_H(win);
2143 /* may also be advertized as 0 */
2144 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2145 ce.width = SH_MAX_W(win);
2146 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2147 ce.height = SH_MAX_H(win);
2150 /* resize increment. */
2152 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2153 ce.width -= (ce.width - SH_MIN_W(win)) %
2155 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2156 ce.height -= (ce.height - SH_MIN_H(win)) %
2160 /* adjust x and y for requested border_width. */
2161 ce.x += BORDER(win) - ev->border_width;
2162 ce.y += BORDER(win) - ev->border_width;
2163 ce.border_width = ev->border_width;
2164 ce.above_sibling = ev->sibling;
2167 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2168 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2169 ce.y, ce.width, ce.height, ce.border_width);
2171 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2176 count_win(struct workspace *ws, int count_transient)
2181 TAILQ_FOREACH(win, &ws->winlist, entry) {
2182 if (count_transient == 0 && win->floating)
2184 if (count_transient == 0 && win->transient)
2190 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2196 quit(struct swm_region *r, union arg *args)
2198 DNPRINTF(SWM_D_MISC, "quit\n");
2203 unmap_window(struct ws_win *win)
2208 /* don't unmap again */
2209 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2212 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2214 xcb_unmap_window(conn, win->id);
2215 xcb_change_window_attributes(conn, win->id,
2216 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2223 int i, j, num_screens;
2225 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2226 for (i = 0; i < num_screens; i++)
2227 for (j = 0; j < workspace_limit; j++)
2228 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2233 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2235 xcb_key_press_event_t event;
2236 xcb_keycode_t *keycode;
2241 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2243 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2246 bzero(&event, sizeof(event));
2247 event.event = win->id;
2248 event.root = win->s->root;
2249 event.child = XCB_WINDOW_NONE;
2250 event.time = XCB_CURRENT_TIME;
2251 event.event_x = X(win);
2252 event.event_y = Y(win);
2255 event.same_screen = True;
2256 event.detail = *keycode;
2257 event.state = modifiers;
2259 event.response_type = XCB_KEY_PRESS;
2260 xcb_send_event(conn, True, win->id,
2261 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2263 event.response_type = XCB_KEY_RELEASE;
2264 xcb_send_event(conn, True, win->id,
2265 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2269 restart(struct swm_region *r, union arg *args)
2271 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2273 /* disable alarm because the following code may not be interrupted */
2275 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2276 err(1, "can't disable alarm");
2282 xcb_key_symbols_free(syms);
2284 xcb_disconnect(conn);
2286 execvp(start_argv[0], start_argv);
2287 warn("execvp failed");
2292 root_to_region(xcb_window_t root)
2294 struct swm_region *r = NULL;
2296 xcb_query_pointer_reply_t *qpr;
2298 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2300 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2301 for (i = 0; i < num_screens; i++)
2302 if (screens[i].root == root)
2305 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2306 screens[i].root), NULL);
2309 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2310 qpr->root_x, qpr->root_y);
2311 /* choose a region based on pointer location */
2312 TAILQ_FOREACH(r, &screens[i].rl, entry)
2313 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2314 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2320 r = TAILQ_FIRST(&screens[i].rl);
2326 find_unmanaged_window(xcb_window_t id)
2329 int i, j, num_screens;
2331 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2332 for (i = 0; i < num_screens; i++)
2333 for (j = 0; j < workspace_limit; j++)
2334 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2342 find_window(xcb_window_t id)
2345 int i, j, num_screens;
2346 xcb_query_tree_cookie_t c;
2347 xcb_query_tree_reply_t *r;
2349 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2350 for (i = 0; i < num_screens; i++)
2351 for (j = 0; j < workspace_limit; j++)
2352 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2356 c = xcb_query_tree(conn, id);
2357 r = xcb_query_tree_reply(conn, c, NULL);
2361 /* if we were looking for the parent return that window instead */
2362 if (r->parent == 0 || r->root == r->parent)
2365 /* look for parent */
2366 for (i = 0; i < num_screens; i++)
2367 for (j = 0; j < workspace_limit; j++)
2368 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2369 if (r->parent == win->id) {
2379 spawn(int ws_idx, union arg *args, int close_fd)
2384 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2387 close(xcb_get_file_descriptor(conn));
2389 setenv("LD_PRELOAD", SWM_LIB, 1);
2391 if (asprintf(&ret, "%d", ws_idx) == -1) {
2392 warn("spawn: asprintf SWM_WS");
2395 setenv("_SWM_WS", ret, 1);
2399 if (asprintf(&ret, "%d", getpid()) == -1) {
2400 warn("spawn: asprintf _SWM_PID");
2403 setenv("_SWM_PID", ret, 1);
2407 if (setsid() == -1) {
2408 warn("spawn: setsid");
2414 * close stdin and stdout to prevent interaction between apps
2415 * and the baraction script
2416 * leave stderr open to record errors
2418 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2419 warn("spawn: open");
2422 dup2(fd, STDIN_FILENO);
2423 dup2(fd, STDOUT_FILENO);
2428 execvp(args->argv[0], args->argv);
2430 warn("spawn: execvp");
2435 kill_refs(struct ws_win *win)
2437 int i, x, num_screens;
2438 struct swm_region *r;
2439 struct workspace *ws;
2444 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2445 for (i = 0; i < num_screens; i++)
2446 TAILQ_FOREACH(r, &screens[i].rl, entry)
2447 for (x = 0; x < workspace_limit; x++) {
2449 if (win == ws->focus)
2451 if (win == ws->focus_prev)
2452 ws->focus_prev = NULL;
2457 validate_win(struct ws_win *testwin)
2460 struct workspace *ws;
2461 struct swm_region *r;
2462 int i, x, num_screens;
2464 if (testwin == NULL)
2467 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2468 for (i = 0; i < num_screens; i++)
2469 TAILQ_FOREACH(r, &screens[i].rl, entry)
2470 for (x = 0; x < workspace_limit; x++) {
2472 TAILQ_FOREACH(win, &ws->winlist, entry)
2480 validate_ws(struct workspace *testws)
2482 struct swm_region *r;
2483 struct workspace *ws;
2484 int i, x, num_screens;
2486 /* validate all ws */
2487 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2488 for (i = 0; i < num_screens; i++)
2489 TAILQ_FOREACH(r, &screens[i].rl, entry)
2490 for (x = 0; x < workspace_limit; x++) {
2499 unfocus_win(struct ws_win *win)
2501 xcb_window_t none = XCB_WINDOW_NONE;
2503 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2507 if (win->ws == NULL)
2510 if (validate_ws(win->ws))
2511 return; /* XXX this gets hit with thunderbird, needs fixing */
2513 if (win->ws->r == NULL)
2516 if (validate_win(win)) {
2521 if (win->ws->focus == win) {
2522 win->ws->focus = NULL;
2523 win->ws->focus_prev = win;
2526 if (validate_win(win->ws->focus)) {
2527 kill_refs(win->ws->focus);
2528 win->ws->focus = NULL;
2530 if (validate_win(win->ws->focus_prev)) {
2531 kill_refs(win->ws->focus_prev);
2532 win->ws->focus_prev = NULL;
2535 grabbuttons(win, 0);
2536 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2537 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2538 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2539 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2547 int i, j, num_screens;
2549 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2551 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2552 for (i = 0; i < num_screens; i++)
2553 for (j = 0; j < workspace_limit; j++)
2554 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2559 focus_win(struct ws_win *win)
2561 struct ws_win *cfw = NULL;
2562 xcb_get_input_focus_cookie_t c;
2563 xcb_get_input_focus_reply_t *r;
2564 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2566 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2570 if (win->ws == NULL)
2573 if (validate_ws(win->ws))
2574 return; /* XXX this gets hit with thunderbird, needs fixing */
2576 if (validate_win(win)) {
2581 if (validate_win(win)) {
2586 c = xcb_get_input_focus(conn);
2587 r = xcb_get_input_focus_reply(conn, c, NULL);
2589 cur_focus = r->focus;
2592 if ((cfw = find_window(cur_focus)) != NULL)
2595 /* use larger hammer since the window was killed somehow */
2596 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2597 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2598 xcb_change_window_attributes(conn, cfw->id,
2599 XCB_CW_BORDER_PIXEL,
2600 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2603 win->ws->focus = win;
2605 if (win->ws->r != NULL) {
2607 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2608 win->id, XCB_CURRENT_TIME);
2609 grabbuttons(win, 1);
2610 xcb_change_window_attributes(conn, win->id,
2611 XCB_CW_BORDER_PIXEL,
2612 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2613 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2614 win->ws->always_raise)
2615 map_window_raised(win->id);
2617 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2618 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2626 switchws(struct swm_region *r, union arg *args)
2628 int wsid = args->id, unmap_old = 0;
2629 struct swm_region *this_r, *other_r;
2631 struct workspace *new_ws, *old_ws;
2637 if (wsid >= workspace_limit)
2641 old_ws = this_r->ws;
2642 new_ws = &this_r->s->ws[wsid];
2644 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2645 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2647 if (new_ws == NULL || old_ws == NULL)
2649 if (new_ws == old_ws)
2652 other_r = new_ws->r;
2653 if (other_r == NULL) {
2654 /* the other workspace is hidden, hide this one */
2658 /* the other ws is visible in another region, exchange them */
2659 other_r->ws_prior = new_ws;
2660 other_r->ws = old_ws;
2661 old_ws->r = other_r;
2663 this_r->ws_prior = old_ws;
2664 this_r->ws = new_ws;
2667 /* this is needed so that we can click on a window after a restart */
2671 a.id = SWM_ARG_ID_FOCUSCUR;
2672 focus(new_ws->r, &a);
2674 /* unmap old windows */
2676 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2681 cyclews(struct swm_region *r, union arg *args)
2684 struct swm_screen *s = r->s;
2687 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2688 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2693 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2696 case SWM_ARG_ID_CYCLEWS_UP:
2697 if (a.id < workspace_limit - 1)
2702 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2705 case SWM_ARG_ID_CYCLEWS_DOWN:
2709 a.id = workspace_limit - 1;
2716 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2718 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2722 } while (a.id != r->ws->idx);
2726 priorws(struct swm_region *r, union arg *args)
2730 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2731 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2733 if (r->ws_prior == NULL)
2736 a.id = r->ws_prior->idx;
2741 cyclescr(struct swm_region *r, union arg *args)
2743 struct swm_region *rr = NULL;
2745 int i, x, y, num_screens;
2747 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2748 /* do nothing if we don't have more than one screen */
2749 if (!(num_screens > 1 || outputs > 1))
2754 case SWM_ARG_ID_CYCLESC_UP:
2755 rr = TAILQ_NEXT(r, entry);
2757 rr = TAILQ_FIRST(&screens[i].rl);
2759 case SWM_ARG_ID_CYCLESC_DOWN:
2760 rr = TAILQ_PREV(r, swm_region_list, entry);
2762 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2770 /* move mouse to region */
2772 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2773 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2776 a.id = SWM_ARG_ID_FOCUSCUR;
2779 if (rr->ws->focus) {
2780 /* move to focus window */
2781 x = X(rr->ws->focus) + 1;
2782 y = Y(rr->ws->focus) + 1;
2783 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2789 sort_windows(struct ws_win_list *wl)
2791 struct ws_win *win, *parent, *nxt;
2796 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2797 nxt = TAILQ_NEXT(win, entry);
2798 if (win->transient) {
2799 parent = find_window(win->transient);
2800 if (parent == NULL) {
2801 warnx("not possible bug");
2804 TAILQ_REMOVE(wl, win, entry);
2805 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2812 swapwin(struct swm_region *r, union arg *args)
2814 struct ws_win *target, *source;
2815 struct ws_win *cur_focus;
2816 struct ws_win_list *wl;
2819 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2820 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2822 cur_focus = r->ws->focus;
2823 if (cur_focus == NULL)
2827 wl = &source->ws->winlist;
2830 case SWM_ARG_ID_SWAPPREV:
2831 if (source->transient)
2832 source = find_window(source->transient);
2833 target = TAILQ_PREV(source, ws_win_list, entry);
2834 if (target && target->transient)
2835 target = find_window(target->transient);
2836 TAILQ_REMOVE(wl, source, entry);
2838 TAILQ_INSERT_TAIL(wl, source, entry);
2840 TAILQ_INSERT_BEFORE(target, source, entry);
2842 case SWM_ARG_ID_SWAPNEXT:
2843 target = TAILQ_NEXT(source, entry);
2844 /* move the parent and let the sort handle the move */
2845 if (source->transient)
2846 source = find_window(source->transient);
2847 TAILQ_REMOVE(wl, source, entry);
2849 TAILQ_INSERT_HEAD(wl, source, entry);
2851 TAILQ_INSERT_AFTER(wl, target, source, entry);
2853 case SWM_ARG_ID_SWAPMAIN:
2854 target = TAILQ_FIRST(wl);
2855 if (target == source) {
2856 if (source->ws->focus_prev != NULL &&
2857 source->ws->focus_prev != target)
2858 source = source->ws->focus_prev;
2862 if (target == NULL || source == NULL)
2864 source->ws->focus_prev = target;
2865 TAILQ_REMOVE(wl, target, entry);
2866 TAILQ_INSERT_BEFORE(source, target, entry);
2867 TAILQ_REMOVE(wl, source, entry);
2868 TAILQ_INSERT_HEAD(wl, source, entry);
2870 case SWM_ARG_ID_MOVELAST:
2871 TAILQ_REMOVE(wl, source, entry);
2872 TAILQ_INSERT_TAIL(wl, source, entry);
2875 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2885 focus_prev(struct ws_win *win)
2887 struct ws_win *winfocus = NULL;
2888 struct ws_win *cur_focus = NULL;
2889 struct ws_win_list *wl = NULL;
2890 struct workspace *ws = NULL;
2892 if (!(win && win->ws))
2897 cur_focus = ws->focus;
2899 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2900 WINID(win), WINID(cur_focus));
2902 /* pickle, just focus on whatever */
2903 if (cur_focus == NULL) {
2904 /* use prev_focus if valid */
2905 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2906 find_window(WINID(ws->focus_prev)))
2907 winfocus = ws->focus_prev;
2911 /* if transient focus on parent */
2912 if (cur_focus->transient) {
2913 winfocus = find_window(cur_focus->transient);
2917 /* if in max_stack try harder */
2918 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2919 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2920 if (cur_focus != ws->focus_prev)
2921 winfocus = ws->focus_prev;
2923 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2928 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2930 if (winfocus == NULL || winfocus == win) {
2931 switch (focus_close) {
2932 case SWM_STACK_BOTTOM:
2933 winfocus = TAILQ_FIRST(wl);
2936 winfocus = TAILQ_LAST(wl, ws_win_list);
2938 case SWM_STACK_ABOVE:
2939 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2940 if (focus_close_wrap)
2941 winfocus = TAILQ_FIRST(wl);
2943 winfocus = TAILQ_PREV(cur_focus,
2944 ws_win_list, entry);
2947 case SWM_STACK_BELOW:
2948 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2950 if (focus_close_wrap)
2951 winfocus = TAILQ_LAST(wl, ws_win_list);
2953 winfocus = TAILQ_NEXT(cur_focus, entry);
2959 if (winfocus == NULL) {
2960 if (focus_default == SWM_STACK_TOP)
2961 winfocus = TAILQ_LAST(wl, ws_win_list);
2963 winfocus = TAILQ_FIRST(wl);
2967 focus_magic(winfocus);
2971 focus(struct swm_region *r, union arg *args)
2973 struct ws_win *winfocus = NULL, *head;
2974 struct ws_win *cur_focus = NULL;
2975 struct ws_win_list *wl = NULL;
2976 struct workspace *ws = NULL;
2982 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2984 /* treat FOCUS_CUR special */
2985 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2986 if (r->ws->focus && r->ws->focus->iconic == 0)
2987 winfocus = r->ws->focus;
2988 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2989 winfocus = r->ws->focus_prev;
2991 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2992 if (winfocus->iconic == 0)
2995 focus_magic(winfocus);
2999 if ((cur_focus = r->ws->focus) == NULL)
3003 if (TAILQ_EMPTY(wl))
3005 /* make sure there is at least one uniconified window */
3007 TAILQ_FOREACH(winfocus, wl, entry)
3008 if (winfocus->iconic == 0) {
3016 case SWM_ARG_ID_FOCUSPREV:
3017 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3019 head = TAILQ_LAST(wl, ws_win_list);
3021 if (WINID(winfocus) == cur_focus->transient) {
3022 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3024 head = TAILQ_LAST(wl, ws_win_list);
3029 if (winfocus && winfocus->iconic) {
3030 while (winfocus != cur_focus) {
3031 if (winfocus == NULL)
3032 winfocus = TAILQ_LAST(wl, ws_win_list);
3033 if (winfocus->iconic == 0)
3035 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3041 case SWM_ARG_ID_FOCUSNEXT:
3042 head = TAILQ_NEXT(cur_focus, entry);
3044 head = TAILQ_FIRST(wl);
3048 if (winfocus && winfocus->iconic) {
3049 while (winfocus != cur_focus) {
3050 if (winfocus == NULL)
3051 winfocus = TAILQ_FIRST(wl);
3052 if (winfocus->iconic == 0)
3054 winfocus = TAILQ_NEXT(winfocus, entry);
3059 case SWM_ARG_ID_FOCUSMAIN:
3060 winfocus = TAILQ_FIRST(wl);
3061 if (winfocus == cur_focus)
3062 winfocus = cur_focus->ws->focus_prev;
3069 focus_magic(winfocus);
3073 cycle_layout(struct swm_region *r, union arg *args)
3075 struct workspace *ws = r->ws;
3078 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3081 if (ws->cur_layout->l_stack == NULL)
3082 ws->cur_layout = &layouts[0];
3086 a.id = SWM_ARG_ID_FOCUSCUR;
3092 stack_config(struct swm_region *r, union arg *args)
3094 struct workspace *ws = r->ws;
3096 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3099 if (ws->cur_layout->l_config != NULL)
3100 ws->cur_layout->l_config(ws, args->id);
3102 if (args->id != SWM_ARG_ID_STACKINIT)
3109 struct swm_geometry g;
3110 struct swm_region *r;
3116 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3118 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3119 for (i = 0; i < num_screens; i++) {
3123 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3124 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3125 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3127 /* start with screen geometry, adjust for bar */
3129 g.w -= 2 * border_width;
3130 g.h -= 2 * border_width;
3136 r->ws->cur_layout->l_stack(r->ws, &g);
3137 r->ws->cur_layout->l_string(r->ws);
3138 /* save r so we can track region changes */
3145 DNPRINTF(SWM_D_STACK, "stack: end\n");
3149 store_float_geom(struct ws_win *win, struct swm_region *r)
3151 /* retain window geom and region geom */
3152 win->g_float = win->g;
3153 win->g_float.x -= X(r);
3154 win->g_float.y -= Y(r);
3155 win->g_floatvalid = 1;
3156 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3157 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3158 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3159 win->g_float.w, win->g_float.h);
3163 stack_floater(struct ws_win *win, struct swm_region *r)
3168 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3171 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3172 * geom on ws switches or return from max mode
3174 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3175 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3176 /* refloat at last floating relative position */
3177 win->g = win->g_float;
3182 win->floatmaxed = 0;
3185 * if set to fullscreen mode, configure window to maximum size.
3187 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3188 if (!win->g_floatvalid)
3189 store_float_geom(win, win->ws->r);
3191 win->g = win->ws->r->g;
3195 * remove border on fullscreen floater when in fullscreen mode or when
3196 * the quirk is present.
3198 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3199 ((win->quirks & SWM_Q_FULLSCREEN) &&
3200 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3201 if (win->bordered) {
3203 X(win) += border_width;
3204 Y(win) += border_width;
3206 } else if (!win->bordered) {
3208 X(win) -= border_width;
3209 Y(win) -= border_width;
3212 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3213 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3214 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3219 * floaters and transients are auto-centred unless moved
3222 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3223 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3226 /* keep window within region bounds */
3227 constrain_window(win, r, 0);
3233 * Send keystrokes to terminal to decrease/increase the font size as the
3234 * window size changes.
3237 adjust_font(struct ws_win *win)
3239 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3240 win->floating || win->transient)
3243 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3244 WIDTH(win) / win->sh.width_inc < term_width &&
3245 win->font_steps < SWM_MAX_FONT_STEPS) {
3246 win->font_size_boundary[win->font_steps] =
3247 (win->sh.width_inc * term_width) + win->sh.base_width;
3250 win->last_inc = win->sh.width_inc;
3251 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3252 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3253 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3256 win->last_inc = win->sh.width_inc;
3257 fake_keypress(win, XK_KP_Add, ShiftMask);
3261 #define SWAPXY(g) do { \
3263 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3264 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3267 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3269 struct swm_geometry win_g, r_g = *g;
3270 struct ws_win *win, *fs_win = NULL;
3271 int i, j, s, stacks;
3272 int w_inc = 1, h_inc, w_base = 1, h_base;
3273 int hrh, extra = 0, h_slice, last_h = 0;
3274 int split, colno, winno, mwin, msize, mscale;
3275 int remain, missing, v_slice, reconfigure;
3277 xcb_get_window_attributes_reply_t *war;
3279 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3280 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3282 winno = count_win(ws, 0);
3283 if (winno == 0 && count_win(ws, 1) == 0)
3286 TAILQ_FOREACH(win, &ws->winlist, entry)
3287 if (win->transient == 0 && win->floating == 0
3288 && win->iconic == 0)
3295 w_inc = win->sh.width_inc;
3296 w_base = win->sh.base_width;
3297 mwin = ws->l_state.horizontal_mwin;
3298 mscale = ws->l_state.horizontal_msize;
3299 stacks = ws->l_state.horizontal_stacks;
3302 w_inc = win->sh.height_inc;
3303 w_base = win->sh.base_height;
3304 mwin = ws->l_state.vertical_mwin;
3305 mscale = ws->l_state.vertical_msize;
3306 stacks = ws->l_state.vertical_stacks;
3310 if (stacks > winno - mwin)
3311 stacks = winno - mwin;
3315 h_slice = r_g.h / SWM_H_SLICE;
3316 if (mwin && winno > mwin) {
3317 v_slice = r_g.w / SWM_V_SLICE;
3321 win_g.w = v_slice * mscale;
3323 if (w_inc > 1 && w_inc < v_slice) {
3324 /* adjust for window's requested size increment */
3325 remain = (win_g.w - w_base) % w_inc;
3331 win_g.x += r_g.w - msize;
3334 colno = split = winno / stacks;
3335 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3336 2 * border_width) / stacks);
3338 hrh = r_g.h / colno;
3339 extra = r_g.h - (colno * hrh);
3340 win_g.h = hrh - 2 * border_width;
3342 /* stack all the tiled windows */
3343 i = j = 0, s = stacks;
3344 TAILQ_FOREACH(win, &ws->winlist, entry) {
3345 if (win->transient != 0 || win->floating != 0)
3347 if (win->iconic != 0)
3350 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3355 if (split && i == split) {
3356 colno = (winno - mwin) / stacks;
3357 if (s <= (winno - mwin) % stacks)
3359 split = split + colno;
3360 hrh = (r_g.h / colno);
3361 extra = r_g.h - (colno * hrh);
3365 win_g.x += win_g.w + 2 * border_width;
3366 win_g.w = (r_g.w - msize -
3367 (stacks * 2 * border_width)) / stacks;
3369 win_g.w += (r_g.w - msize -
3370 (stacks * 2 * border_width)) % stacks;
3374 win_g.h = hrh - 2 * border_width;
3376 h_inc = win->sh.width_inc;
3377 h_base = win->sh.base_width;
3379 h_inc = win->sh.height_inc;
3380 h_base = win->sh.base_height;
3382 if (j == colno - 1) {
3383 win_g.h = hrh + extra;
3384 } else if (h_inc > 1 && h_inc < h_slice) {
3385 /* adjust for window's requested size increment */
3386 remain = (win_g.h - h_base) % h_inc;
3387 missing = h_inc - remain;
3389 if (missing <= extra || j == 0) {
3401 win_g.y += last_h + 2 * border_width;
3403 if (disable_border && bar_enabled == 0 && winno == 1){
3405 win_g.w += 2 * border_width;
3406 win_g.h += 2 * border_width;
3411 if (X(win) != win_g.y || Y(win) != win_g.x ||
3412 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3416 WIDTH(win) = win_g.h;
3417 HEIGHT(win) = win_g.w;
3420 if (X(win) != win_g.x || Y(win) != win_g.y ||
3421 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3425 WIDTH(win) = win_g.w;
3426 HEIGHT(win) = win_g.h;
3430 if (bordered != win->bordered) {
3432 win->bordered = bordered;
3440 war = xcb_get_window_attributes_reply(conn,
3441 xcb_get_window_attributes(conn, win->id),
3444 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3445 map_window_raised(win->id);
3455 /* now, stack all the floaters and transients */
3456 TAILQ_FOREACH(win, &ws->winlist, entry) {
3457 if (win->transient == 0 && win->floating == 0)
3459 if (win->iconic == 1)
3461 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3466 stack_floater(win, ws->r);
3467 map_window_raised(win->id);
3471 stack_floater(fs_win, ws->r);
3472 map_window_raised(fs_win->id);
3477 vertical_config(struct workspace *ws, int id)
3479 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3483 case SWM_ARG_ID_STACKRESET:
3484 case SWM_ARG_ID_STACKINIT:
3485 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3486 ws->l_state.vertical_mwin = 1;
3487 ws->l_state.vertical_stacks = 1;
3489 case SWM_ARG_ID_MASTERSHRINK:
3490 if (ws->l_state.vertical_msize > 1)
3491 ws->l_state.vertical_msize--;
3493 case SWM_ARG_ID_MASTERGROW:
3494 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3495 ws->l_state.vertical_msize++;
3497 case SWM_ARG_ID_MASTERADD:
3498 ws->l_state.vertical_mwin++;
3500 case SWM_ARG_ID_MASTERDEL:
3501 if (ws->l_state.vertical_mwin > 0)
3502 ws->l_state.vertical_mwin--;
3504 case SWM_ARG_ID_STACKINC:
3505 ws->l_state.vertical_stacks++;
3507 case SWM_ARG_ID_STACKDEC:
3508 if (ws->l_state.vertical_stacks > 1)
3509 ws->l_state.vertical_stacks--;
3511 case SWM_ARG_ID_FLIPLAYOUT:
3512 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3520 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3522 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3524 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3528 horizontal_config(struct workspace *ws, int id)
3530 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3533 case SWM_ARG_ID_STACKRESET:
3534 case SWM_ARG_ID_STACKINIT:
3535 ws->l_state.horizontal_mwin = 1;
3536 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3537 ws->l_state.horizontal_stacks = 1;
3539 case SWM_ARG_ID_MASTERSHRINK:
3540 if (ws->l_state.horizontal_msize > 1)
3541 ws->l_state.horizontal_msize--;
3543 case SWM_ARG_ID_MASTERGROW:
3544 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3545 ws->l_state.horizontal_msize++;
3547 case SWM_ARG_ID_MASTERADD:
3548 ws->l_state.horizontal_mwin++;
3550 case SWM_ARG_ID_MASTERDEL:
3551 if (ws->l_state.horizontal_mwin > 0)
3552 ws->l_state.horizontal_mwin--;
3554 case SWM_ARG_ID_STACKINC:
3555 ws->l_state.horizontal_stacks++;
3557 case SWM_ARG_ID_STACKDEC:
3558 if (ws->l_state.horizontal_stacks > 1)
3559 ws->l_state.horizontal_stacks--;
3561 case SWM_ARG_ID_FLIPLAYOUT:
3562 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3570 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3572 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3574 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3577 /* fullscreen view */
3579 max_stack(struct workspace *ws, struct swm_geometry *g)
3581 struct swm_geometry gg = *g;
3582 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3583 int winno, num_screens;
3585 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3590 winno = count_win(ws, 0);
3591 if (winno == 0 && count_win(ws, 1) == 0)
3594 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3595 TAILQ_FOREACH(win, &ws->winlist, entry) {
3596 if (win->transient) {
3598 parent = find_window(win->transient);
3602 if (win->floating && win->floatmaxed == 0 ) {
3604 * retain geometry for retrieval on exit from
3607 store_float_geom(win, ws->r);
3608 win->floatmaxed = 1;
3611 /* only reconfigure if necessary */
3612 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3613 HEIGHT(win) != gg.h) {
3619 WIDTH(win) += 2 * border_width;
3620 HEIGHT(win) += 2 * border_width;
3625 /* unmap only if we don't have multi screen */
3626 if (win != ws->focus)
3627 if (!(num_screens > 1 || outputs > 1))
3631 /* put the last transient on top */
3634 map_window_raised(parent->id);
3635 stack_floater(wintrans, ws->r);
3636 focus_magic(wintrans);
3641 send_to_ws(struct swm_region *r, union arg *args)
3643 int wsid = args->id;
3644 struct ws_win *win = NULL, *parent;
3645 struct workspace *ws, *nws;
3646 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3647 char ws_idx_str[SWM_PROPLEN];
3650 if (wsid >= workspace_limit)
3653 if (r && r->ws && r->ws->focus)
3659 if (win->ws->idx == wsid)
3662 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3665 nws = &win->s->ws[wsid];
3667 a.id = SWM_ARG_ID_FOCUSPREV;
3669 if (win->transient) {
3670 parent = find_window(win->transient);
3672 unmap_window(parent);
3673 TAILQ_REMOVE(&ws->winlist, parent, entry);
3674 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3679 TAILQ_REMOVE(&ws->winlist, win, entry);
3680 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3681 if (TAILQ_EMPTY(&ws->winlist))
3682 r->ws->focus = NULL;
3685 /* Try to update the window's workspace property */
3686 ws_idx_atom = get_atom_from_string("_SWM_WS");
3688 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3690 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3692 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3693 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3702 pressbutton(struct swm_region *r, union arg *args)
3704 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3705 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3706 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3707 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3711 raise_toggle(struct swm_region *r, union arg *args)
3713 if (r == NULL || r->ws == NULL)
3716 r->ws->always_raise = !r->ws->always_raise;
3718 /* bring floaters back to top */
3719 if (r->ws->always_raise == 0)
3724 iconify(struct swm_region *r, union arg *args)
3728 if (r->ws->focus == NULL)
3730 unmap_window(r->ws->focus);
3731 update_iconic(r->ws->focus, 1);
3734 r->ws->focus = NULL;
3735 a.id = SWM_ARG_ID_FOCUSCUR;
3740 get_win_name(xcb_window_t win)
3743 xcb_get_property_cookie_t c;
3744 xcb_icccm_get_text_property_reply_t r;
3746 c = xcb_icccm_get_wm_name(conn, win);
3747 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3748 if (r.name_len > 0) {
3749 name = malloc(r.name_len + 1);
3751 xcb_icccm_get_text_property_reply_wipe(&r);
3754 memcpy(name, r.name, r.name_len);
3755 name[r.name_len] = '\0';
3757 xcb_icccm_get_text_property_reply_wipe(&r);
3764 uniconify(struct swm_region *r, union arg *args)
3771 DNPRINTF(SWM_D_MISC, "uniconify\n");
3773 if (r == NULL || r->ws == NULL)
3776 /* make sure we have anything to uniconify */
3777 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3778 if (win->ws == NULL)
3779 continue; /* should never happen */
3780 if (win->iconic == 0)
3788 search_resp_action = SWM_SEARCH_UNICONIFY;
3790 spawn_select(r, args, "search", &searchpid);
3792 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3795 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3796 if (win->ws == NULL)
3797 continue; /* should never happen */
3798 if (win->iconic == 0)
3801 name = get_win_name(win->id);
3804 fprintf(lfile, "%s.%u\n", name, win->id);
3812 name_workspace(struct swm_region *r, union arg *args)
3816 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3822 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3824 spawn_select(r, args, "name_workspace", &searchpid);
3826 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3829 fprintf(lfile, "%s", "");
3834 search_workspace(struct swm_region *r, union arg *args)
3837 struct workspace *ws;
3840 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3846 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3848 spawn_select(r, args, "search", &searchpid);
3850 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3853 for (i = 0; i < workspace_limit; i++) {
3857 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3858 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3865 search_win_cleanup(void)
3867 struct search_window *sw = NULL;
3869 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3870 xcb_destroy_window(conn, sw->indicator);
3871 xcb_free_gc(conn, sw->gc);
3872 TAILQ_REMOVE(&search_wl, sw, entry);
3878 search_win(struct swm_region *r, union arg *args)
3880 struct ws_win *win = NULL;
3881 struct search_window *sw = NULL;
3883 uint32_t gcv[3], wa[2];
3888 XRectangle ibox, lbox;
3889 DNPRINTF(SWM_D_MISC, "search_win\n");
3892 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3894 spawn_select(r, args, "search", &searchpid);
3896 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3899 TAILQ_INIT(&search_wl);
3902 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3903 if (win->iconic == 1)
3906 sw = calloc(1, sizeof(struct search_window));
3908 warn("search_win: calloc");
3910 search_win_cleanup();
3916 snprintf(s, sizeof s, "%d", i);
3919 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3921 w = xcb_generate_id(conn);
3922 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3923 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3924 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3925 lbox.width + 4, bar_fs_extents->max_logical_extent.height,
3926 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3927 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3930 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3932 sw->gc = xcb_generate_id(conn);
3933 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3934 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3936 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3937 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3938 map_window_raised(w);
3940 xcb_image_text_8(conn, len, w, sw->gc, 2,
3941 (bar_fs_extents->max_logical_extent.height -
3942 lbox.height) / 2 - lbox.y, s);
3944 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3946 fprintf(lfile, "%d\n", i);
3954 search_resp_uniconify(char *resp, unsigned long len)
3960 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3962 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3963 if (win->iconic == 0)
3965 name = get_win_name(win->id);
3968 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3973 if (strncmp(s, resp, len) == 0) {
3974 /* XXX this should be a callback to generalize */
3975 update_iconic(win, 0);
3984 search_resp_name_workspace(char *resp, unsigned long len)
3986 struct workspace *ws;
3988 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3990 if (search_r->ws == NULL)
3995 free(search_r->ws->name);
3996 search_r->ws->name = NULL;
4000 ws->name = strdup(resp);
4001 if (ws->name == NULL) {
4002 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4003 "strdup: %s", strerror(errno));
4010 search_resp_search_workspace(char *resp, unsigned long len)
4017 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4021 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4028 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
4030 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4037 switchws(search_r, &a);
4041 search_resp_search_window(char *resp, unsigned long len)
4046 struct search_window *sw;
4048 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4052 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4057 idx = strtonum(s, 1, INT_MAX, &errstr);
4059 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4066 TAILQ_FOREACH(sw, &search_wl, entry)
4067 if (idx == sw->idx) {
4073 #define MAX_RESP_LEN 1024
4076 search_do_resp(void)
4082 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4087 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4088 warn("search: calloc");
4092 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4094 warn("search: read error");
4097 resp[rbytes] = '\0';
4100 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4101 * newline, so work around that by sanitizing the resp now.
4103 resp[strcspn(resp, "\n")] = '\0';
4106 switch (search_resp_action) {
4107 case SWM_SEARCH_UNICONIFY:
4108 search_resp_uniconify(resp, len);
4110 case SWM_SEARCH_NAME_WORKSPACE:
4111 search_resp_name_workspace(resp, len);
4113 case SWM_SEARCH_SEARCH_WORKSPACE:
4114 search_resp_search_workspace(resp, len);
4116 case SWM_SEARCH_SEARCH_WINDOW:
4117 search_resp_search_window(resp, len);
4122 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4123 search_win_cleanup();
4125 search_resp_action = SWM_SEARCH_NONE;
4126 close(select_resp_pipe[0]);
4131 wkill(struct swm_region *r, union arg *args)
4133 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4135 if (r->ws->focus == NULL)
4138 if (args->id == SWM_ARG_ID_KILLWINDOW)
4139 xcb_kill_client(conn, r->ws->focus->id);
4141 if (r->ws->focus->can_delete)
4142 client_msg(r->ws->focus, adelete);
4147 floating_toggle_win(struct ws_win *win)
4149 struct swm_region *r;
4159 /* reject floating toggles in max stack mode */
4160 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4163 if (win->floating) {
4164 if (!win->floatmaxed) {
4165 /* retain position for refloat */
4166 store_float_geom(win, r);
4170 if (win->g_floatvalid) {
4171 /* refloat at last floating relative position */
4172 X(win) = win->g_float.x + X(r);
4173 Y(win) = win->g_float.y + Y(r);
4174 WIDTH(win) = win->g_float.w;
4175 HEIGHT(win) = win->g_float.h;
4180 ewmh_update_actions(win);
4186 floating_toggle(struct swm_region *r, union arg *args)
4188 struct ws_win *win = r->ws->focus;
4194 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4195 _NET_WM_STATE_TOGGLE);
4199 if (win == win->ws->focus) {
4200 a.id = SWM_ARG_ID_FOCUSCUR;
4201 focus(win->ws->r, &a);
4206 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4208 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4210 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4212 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4215 if (X(win) + BORDER(win) < X(r)) {
4217 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4219 X(win) = X(r) - BORDER(win);
4222 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4224 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4226 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4229 if (Y(win) + BORDER(win) < Y(r)) {
4231 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4233 Y(win) = Y(r) - BORDER(win);
4239 if (HEIGHT(win) < 1)
4245 update_window(struct ws_win *win)
4250 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4251 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4252 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4256 wc[3] = HEIGHT(win);
4257 wc[4] = BORDER(win);
4259 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4260 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4261 wc[3], YESNO(win->bordered));
4263 xcb_configure_window(conn, win->id, mask, wc);
4266 #define SWM_RESIZE_STEPS (50)
4269 resize(struct ws_win *win, union arg *args)
4271 xcb_timestamp_t time = 0;
4272 struct swm_region *r = NULL;
4273 int resize_step = 0;
4274 struct swm_geometry g;
4275 int top = 0, left = 0, buttonrelease;
4277 unsigned int shape; /* cursor style */
4278 xcb_cursor_t cursor;
4279 xcb_font_t cursor_font;
4280 xcb_grab_pointer_reply_t *gpr;
4281 xcb_query_pointer_reply_t *xpr;
4282 xcb_generic_event_t *evt;
4283 xcb_motion_notify_event_t *mne;
4289 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4292 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%x, floating: %s, "
4293 "transient: 0x%x\n", win->id, YESNO(win->floating),
4296 if (!(win->transient != 0 || win->floating != 0))
4299 /* reject resizes in max mode for floaters (transient ok) */
4300 if (win->floatmaxed)
4304 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4310 case SWM_ARG_ID_WIDTHSHRINK:
4311 WIDTH(win) -= SWM_RESIZE_STEPS;
4314 case SWM_ARG_ID_WIDTHGROW:
4315 WIDTH(win) += SWM_RESIZE_STEPS;
4318 case SWM_ARG_ID_HEIGHTSHRINK:
4319 HEIGHT(win) -= SWM_RESIZE_STEPS;
4322 case SWM_ARG_ID_HEIGHTGROW:
4323 HEIGHT(win) += SWM_RESIZE_STEPS;
4330 constrain_window(win, r, 1);
4332 store_float_geom(win,r);
4336 /* get cursor offset from window root */
4337 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4344 if (xpr->win_x < WIDTH(win) / 2)
4347 if (xpr->win_y < HEIGHT(win) / 2)
4350 if (args->id == SWM_ARG_ID_CENTER)
4353 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4355 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4357 cursor_font = xcb_generate_id(conn);
4358 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4360 cursor = xcb_generate_id(conn);
4361 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4362 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4364 gpr = xcb_grab_pointer_reply(conn,
4365 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4366 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
4367 cursor, XCB_CURRENT_TIME),
4370 xcb_free_cursor(conn, cursor);
4371 xcb_close_font(conn, cursor_font);
4377 while ((evt = xcb_poll_for_event(conn)) && buttonrelease != 1) {
4379 XMaskEvent(display, MOUSEMASK | ExposureMask |
4380 SubstructureRedirectMask, &ev);
4382 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4383 case XCB_BUTTON_RELEASE:
4386 case XCB_MOTION_NOTIFY:
4387 mne = (xcb_motion_notify_event_t *)evt;
4388 /* cursor offset/delta from start of the operation */
4389 dx = mne->root_x - xpr->root_x;
4390 dy = mne->root_y - xpr->root_y;
4396 if (args->id == SWM_ARG_ID_CENTER) {
4397 if (g.h / 2 + dy < 1)
4401 HEIGHT(win) = g.h + 2 * dy;
4409 HEIGHT(win) = g.h + dy;
4416 if (args->id == SWM_ARG_ID_CENTER) {
4417 if (g.w / 2 + dx < 1)
4421 WIDTH(win) = g.w + 2 * dx;
4429 WIDTH(win) = g.w + dx;
4432 constrain_window(win, r, 1);
4434 /* not free, don't sync more than 120 times / second */
4435 if ((mne->time - time) > (1000 / 120) ) {
4451 store_float_geom(win,r);
4453 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4454 xcb_free_cursor(conn, cursor);
4455 xcb_close_font(conn, cursor_font);
4461 resize_step(struct swm_region *r, union arg *args)
4463 struct ws_win *win = NULL;
4465 if (r && r->ws && r->ws->focus)
4473 #define SWM_MOVE_STEPS (50)
4476 move(struct ws_win *win, union arg *args)
4478 xcb_timestamp_t time = 0;
4479 int move_step = 0, buttonrelease;
4480 struct swm_region *r = NULL;
4481 xcb_font_t cursor_font;
4482 xcb_cursor_t cursor;
4483 xcb_grab_pointer_reply_t *gpr;
4484 xcb_query_pointer_reply_t *qpr;
4485 xcb_generic_event_t *evt;
4486 xcb_motion_notify_event_t *mne;
4492 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4495 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%x, floating: %s, transient: "
4496 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4498 /* in max_stack mode should only move transients */
4499 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4503 if (win->floating == 0 && !win->transient) {
4504 store_float_geom(win, r);
4505 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4508 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4515 case SWM_ARG_ID_MOVELEFT:
4516 X(win) -= (SWM_MOVE_STEPS - border_width);
4519 case SWM_ARG_ID_MOVERIGHT:
4520 X(win) += (SWM_MOVE_STEPS - border_width);
4523 case SWM_ARG_ID_MOVEUP:
4524 Y(win) -= (SWM_MOVE_STEPS - border_width);
4527 case SWM_ARG_ID_MOVEDOWN:
4528 Y(win) += (SWM_MOVE_STEPS - border_width);
4535 constrain_window(win, r, 0);
4537 store_float_geom(win, r);
4541 cursor_font = xcb_generate_id(conn);
4542 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4544 cursor = xcb_generate_id(conn);
4545 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4546 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4548 gpr = xcb_grab_pointer_reply(conn,
4549 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4550 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4551 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME),
4554 xcb_free_cursor(conn, cursor);
4555 xcb_close_font(conn, cursor_font);
4559 /* get cursor offset from window root */
4560 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4563 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4564 xcb_free_cursor(conn, cursor);
4565 xcb_close_font(conn, cursor_font);
4570 while ((evt = xcb_poll_for_event(conn)) && buttonrelease != 1) {
4572 XMaskEvent(display, MOUSEMASK | ExposureMask |
4573 SubstructureRedirectMask, &ev);
4575 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4576 case XCB_BUTTON_RELEASE:
4579 case XCB_MOTION_NOTIFY:
4580 mne = (xcb_motion_notify_event_t *)evt;
4581 X(win) = mne->root_x - qpr->win_x - border_width;
4582 Y(win) = mne->root_y - qpr->win_y - border_width;
4584 constrain_window(win, r, 0);
4586 /* not free, don't sync more than 120 times / second */
4587 if ((mne->time - time) > (1000 / 120) ) {
4603 store_float_geom(win, r);
4605 xcb_free_cursor(conn, cursor);
4606 xcb_close_font(conn, cursor_font);
4607 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4611 move_step(struct swm_region *r, union arg *args)
4613 struct ws_win *win = NULL;
4615 if (r && r->ws && r->ws->focus)
4620 if (!(win->transient != 0 || win->floating != 0))
4627 /* user/key callable function IDs */
4716 kf_search_workspace,
4718 kf_dumpwins, /* MUST BE LAST */
4722 /* key definitions */
4724 dummykeyfunc(struct swm_region *r, union arg *args)
4729 char name[SWM_FUNCNAME_LEN];
4730 void (*func)(struct swm_region *r, union arg *);
4732 } keyfuncs[kf_invalid + 1] = {
4733 /* name function argument */
4734 { "cycle_layout", cycle_layout, {0} },
4735 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4736 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4737 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4738 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4739 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4740 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4741 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4742 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4743 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4744 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4745 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4746 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4747 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4748 { "quit", quit, {0} },
4749 { "restart", restart, {0} },
4750 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4751 { "ws_1", switchws, {.id = 0} },
4752 { "ws_2", switchws, {.id = 1} },
4753 { "ws_3", switchws, {.id = 2} },
4754 { "ws_4", switchws, {.id = 3} },
4755 { "ws_5", switchws, {.id = 4} },
4756 { "ws_6", switchws, {.id = 5} },
4757 { "ws_7", switchws, {.id = 6} },
4758 { "ws_8", switchws, {.id = 7} },
4759 { "ws_9", switchws, {.id = 8} },
4760 { "ws_10", switchws, {.id = 9} },
4761 { "ws_11", switchws, {.id = 10} },
4762 { "ws_12", switchws, {.id = 11} },
4763 { "ws_13", switchws, {.id = 12} },
4764 { "ws_14", switchws, {.id = 13} },
4765 { "ws_15", switchws, {.id = 14} },
4766 { "ws_16", switchws, {.id = 15} },
4767 { "ws_17", switchws, {.id = 16} },
4768 { "ws_18", switchws, {.id = 17} },
4769 { "ws_19", switchws, {.id = 18} },
4770 { "ws_20", switchws, {.id = 19} },
4771 { "ws_21", switchws, {.id = 20} },
4772 { "ws_22", switchws, {.id = 21} },
4773 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4774 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4775 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4776 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4777 { "ws_prior", priorws, {0} },
4778 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4779 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4780 { "mvws_1", send_to_ws, {.id = 0} },
4781 { "mvws_2", send_to_ws, {.id = 1} },
4782 { "mvws_3", send_to_ws, {.id = 2} },
4783 { "mvws_4", send_to_ws, {.id = 3} },
4784 { "mvws_5", send_to_ws, {.id = 4} },
4785 { "mvws_6", send_to_ws, {.id = 5} },
4786 { "mvws_7", send_to_ws, {.id = 6} },
4787 { "mvws_8", send_to_ws, {.id = 7} },
4788 { "mvws_9", send_to_ws, {.id = 8} },
4789 { "mvws_10", send_to_ws, {.id = 9} },
4790 { "mvws_11", send_to_ws, {.id = 10} },
4791 { "mvws_12", send_to_ws, {.id = 11} },
4792 { "mvws_13", send_to_ws, {.id = 12} },
4793 { "mvws_14", send_to_ws, {.id = 13} },
4794 { "mvws_15", send_to_ws, {.id = 14} },
4795 { "mvws_16", send_to_ws, {.id = 15} },
4796 { "mvws_17", send_to_ws, {.id = 16} },
4797 { "mvws_18", send_to_ws, {.id = 17} },
4798 { "mvws_19", send_to_ws, {.id = 18} },
4799 { "mvws_20", send_to_ws, {.id = 19} },
4800 { "mvws_21", send_to_ws, {.id = 20} },
4801 { "mvws_22", send_to_ws, {.id = 21} },
4802 { "bar_toggle", bar_toggle, {0} },
4803 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4804 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4805 { "float_toggle", floating_toggle,{0} },
4806 { "version", version, {0} },
4807 { "spawn_custom", dummykeyfunc, {0} },
4808 { "iconify", iconify, {0} },
4809 { "uniconify", uniconify, {0} },
4810 { "raise_toggle", raise_toggle, {0} },
4811 { "button2", pressbutton, {2} },
4812 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4813 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4814 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4815 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4816 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4817 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4818 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4819 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4820 { "name_workspace", name_workspace, {0} },
4821 { "search_workspace", search_workspace, {0} },
4822 { "search_win", search_win, {0} },
4823 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4824 { "invalid key func", NULL, {0} },
4827 RB_ENTRY(key) entry;
4830 enum keyfuncid funcid;
4833 RB_HEAD(key_tree, key);
4836 key_cmp(struct key *kp1, struct key *kp2)
4838 if (kp1->keysym < kp2->keysym)
4840 if (kp1->keysym > kp2->keysym)
4843 if (kp1->mod < kp2->mod)
4845 if (kp1->mod > kp2->mod)
4851 RB_GENERATE(key_tree, key, entry, key_cmp);
4852 struct key_tree keys;
4855 enum { client_click, root_click };
4857 unsigned int action;
4859 unsigned int button;
4860 void (*func)(struct ws_win *, union arg *);
4863 /* action key mouse button func args */
4864 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4865 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4866 { client_click, MODKEY, Button1, move, {0} },
4870 update_modkey(unsigned int mod)
4876 RB_FOREACH(kp, key_tree, &keys)
4877 if (kp->mod & ShiftMask)
4878 kp->mod = mod | ShiftMask;
4882 for (i = 0; i < LENGTH(buttons); i++)
4883 if (buttons[i].mask & ShiftMask)
4884 buttons[i].mask = mod | ShiftMask;
4886 buttons[i].mask = mod;
4891 TAILQ_ENTRY(spawn_prog) entry;
4896 TAILQ_HEAD(spawn_list, spawn_prog);
4897 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4900 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4903 struct spawn_prog *prog = NULL;
4905 char *ap, **real_args;
4907 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4910 TAILQ_FOREACH(prog, &spawns, entry) {
4911 if (!strcasecmp(spawn_name, prog->name))
4915 warnx("spawn_custom: program %s not found", spawn_name);
4919 /* make room for expanded args */
4920 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4921 err(1, "spawn_custom: calloc real_args");
4923 /* expand spawn_args into real_args */
4924 for (i = 0; i < prog->argc; i++) {
4926 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4927 if (!strcasecmp(ap, "$bar_border")) {
4929 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4931 err(1, "spawn_custom border color");
4932 } else if (!strcasecmp(ap, "$bar_color")) {
4934 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4936 err(1, "spawn_custom bar color");
4937 } else if (!strcasecmp(ap, "$bar_font")) {
4938 if ((real_args[i] = strdup(bar_fonts))
4940 err(1, "spawn_custom bar fonts");
4941 } else if (!strcasecmp(ap, "$bar_font_color")) {
4943 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4945 err(1, "spawn_custom color font");
4946 } else if (!strcasecmp(ap, "$color_focus")) {
4948 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4950 err(1, "spawn_custom color focus");
4951 } else if (!strcasecmp(ap, "$color_unfocus")) {
4953 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4955 err(1, "spawn_custom color unfocus");
4957 /* no match --> copy as is */
4958 if ((real_args[i] = strdup(ap)) == NULL)
4959 err(1, "spawn_custom strdup(ap)");
4961 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4966 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4967 for (i = 0; i < prog->argc; i++)
4968 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4969 DNPRINTF(SWM_D_SPAWN, "\n");
4971 *ret_args = real_args;
4972 return (prog->argc);
4976 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4982 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4986 spawn(r->ws->idx, &a, 1);
4988 for (i = 0; i < spawn_argc; i++)
4994 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5000 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5004 if (pipe(select_list_pipe) == -1)
5005 err(1, "pipe error");
5006 if (pipe(select_resp_pipe) == -1)
5007 err(1, "pipe error");
5009 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5010 err(1, "could not disable SIGPIPE");
5011 switch (*pid = fork()) {
5013 err(1, "cannot fork");
5016 if (dup2(select_list_pipe[0], 0) == -1)
5018 if (dup2(select_resp_pipe[1], 1) == -1)
5020 close(select_list_pipe[1]);
5021 close(select_resp_pipe[0]);
5022 spawn(r->ws->idx, &a, 0);
5024 default: /* parent */
5025 close(select_list_pipe[0]);
5026 close(select_resp_pipe[1]);
5030 for (i = 0; i < spawn_argc; i++)
5036 spawn_insert(char *name, char *args)
5038 char *arg, *cp, *ptr;
5039 struct spawn_prog *sp;
5041 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5043 if ((sp = calloc(1, sizeof *sp)) == NULL)
5044 err(1, "spawn_insert: malloc");
5045 if ((sp->name = strdup(name)) == NULL)
5046 err(1, "spawn_insert: strdup");
5048 /* convert the arguments to an argument list */
5049 if ((ptr = cp = strdup(args)) == NULL)
5050 err(1, "spawn_insert: strdup");
5051 while ((arg = strsep(&ptr, " \t")) != NULL) {
5052 /* empty field; skip it */
5057 if ((sp->argv = realloc(sp->argv, sp->argc *
5058 sizeof *sp->argv)) == NULL)
5059 err(1, "spawn_insert: realloc");
5060 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5061 err(1, "spawn_insert: strdup");
5065 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5066 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5070 spawn_remove(struct spawn_prog *sp)
5074 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5076 TAILQ_REMOVE(&spawns, sp, entry);
5077 for (i = 0; i < sp->argc; i++)
5083 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5087 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5089 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5092 spawn_insert(name, args);
5094 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5098 setspawn(char *name, char *args)
5100 struct spawn_prog *sp;
5102 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5107 TAILQ_FOREACH(sp, &spawns, entry) {
5108 if (!strcmp(sp->name, name)) {
5112 spawn_replace(sp, name, args);
5113 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5117 if (*args == '\0') {
5118 warnx("error: setspawn: cannot find program: %s", name);
5122 spawn_insert(name, args);
5123 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5127 setconfspawn(char *selector, char *value, int flags)
5129 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5131 setspawn(selector, value);
5133 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5140 setconfspawn("term", "xterm", 0);
5141 setconfspawn("spawn_term", "xterm", 0);
5142 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5143 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5144 setconfspawn("lock", "xlock", 0);
5145 setconfspawn("initscr", "initscreen.sh", 0);
5146 setconfspawn("menu", "dmenu_run"
5149 " -nf $bar_font_color"
5151 " -sf $bar_color", 0);
5152 setconfspawn("search", "dmenu"
5156 " -nf $bar_font_color"
5158 " -sf $bar_color", 0);
5159 setconfspawn("name_workspace", "dmenu"
5163 " -nf $bar_font_color"
5165 " -sf $bar_color", 0);
5169 #define SWM_MODNAME_SIZE 32
5170 #define SWM_KEY_WS "\n+ \t"
5172 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5176 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5177 if (mod == NULL || ks == NULL) {
5178 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5181 if (keystr == NULL || strlen(keystr) == 0) {
5182 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5188 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5189 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5191 cp += (long)strspn(cp, SWM_KEY_WS);
5192 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5194 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5196 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5198 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5200 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5202 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5204 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5205 *mod |= ControlMask;
5207 *ks = XStringToKeysym(name);
5208 XConvertCase(*ks, ks, &uks);
5209 if (ks == NoSymbol) {
5211 "parsekeys: invalid key %s\n",
5217 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5222 strdupsafe(char *str)
5227 return (strdup(str));
5231 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5235 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5236 keyfuncs[kfid].name, spawn_name);
5238 if ((kp = malloc(sizeof *kp)) == NULL)
5239 err(1, "key_insert: malloc");
5244 kp->spawn_name = strdupsafe(spawn_name);
5245 RB_INSERT(key_tree, &keys, kp);
5247 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5251 key_lookup(unsigned int mod, KeySym ks)
5258 return (RB_FIND(key_tree, &keys, &kp));
5262 key_remove(struct key *kp)
5264 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5266 RB_REMOVE(key_tree, &keys, kp);
5267 free(kp->spawn_name);
5270 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5274 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5277 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5281 key_insert(mod, ks, kfid, spawn_name);
5283 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5287 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5292 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5293 keyfuncs[kfid].name, spawn_name);
5295 if ((kp = key_lookup(mod, ks)) != NULL) {
5296 if (kfid == kf_invalid)
5299 key_replace(kp, mod, ks, kfid, spawn_name);
5300 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5303 if (kfid == kf_invalid) {
5304 warnx("error: setkeybinding: cannot find mod/key combination");
5305 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5309 key_insert(mod, ks, kfid, spawn_name);
5310 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5314 setconfbinding(char *selector, char *value, int flags)
5316 enum keyfuncid kfid;
5319 struct spawn_prog *sp;
5320 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5321 if (selector == NULL) {
5322 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5323 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5325 setkeybinding(mod, ks, kfid, NULL);
5330 /* search by key function name */
5331 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5332 if (strncasecmp(selector, keyfuncs[kfid].name,
5333 SWM_FUNCNAME_LEN) == 0) {
5334 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5336 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5337 setkeybinding(mod, ks, kfid, NULL);
5343 /* search by custom spawn name */
5344 TAILQ_FOREACH(sp, &spawns, entry) {
5345 if (strcasecmp(selector, sp->name) == 0) {
5346 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5348 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5349 setkeybinding(mod, ks, kf_spawn_custom,
5356 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5363 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5364 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5365 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5366 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5367 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5368 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5369 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5370 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5371 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5372 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5373 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5374 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5375 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5376 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5377 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5378 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5379 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5380 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5381 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5382 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5383 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5384 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5385 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5386 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5387 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5388 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5389 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5390 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5391 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5392 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5393 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5394 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5395 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5396 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5397 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5398 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5399 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5400 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5401 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5402 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5403 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5404 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5405 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5406 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5407 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5408 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5409 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5410 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5411 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5412 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5414 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5415 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5416 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5419 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5420 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5421 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5422 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5424 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5425 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5426 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5427 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5428 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5429 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5430 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5431 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5432 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5433 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5434 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5435 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5436 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5437 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5438 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5439 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5440 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5441 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5442 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5443 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5444 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5445 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5446 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5447 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5448 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5449 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5450 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5451 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5452 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5453 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5454 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5455 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5456 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5457 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5458 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5460 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5469 while (RB_EMPTY(&keys) == 0) {
5470 kp = RB_ROOT(&keys);
5476 setkeymapping(char *selector, char *value, int flags)
5478 char keymapping_file[PATH_MAX];
5479 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5480 if (value[0] == '~')
5481 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5482 pwd->pw_dir, &value[1]);
5484 strlcpy(keymapping_file, value, sizeof keymapping_file);
5486 /* load new key bindings; if it fails, revert to default bindings */
5487 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5491 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5496 updatenumlockmask(void)
5499 xcb_get_modifier_mapping_reply_t *modmap_r;
5500 xcb_keycode_t *modmap, kc;
5502 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5505 modmap_r = xcb_get_modifier_mapping_reply(conn,
5506 xcb_get_modifier_mapping(conn),
5509 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5510 for (i = 0; i < 8; i++) {
5511 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5512 kc = modmap[i * modmap_r->keycodes_per_modifier
5515 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5517 numlockmask = (1 << i);
5529 xcb_keycode_t *code;
5530 unsigned int modifiers[] =
5531 { 0, LockMask, numlockmask, numlockmask | LockMask };
5534 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5535 updatenumlockmask();
5537 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5538 for (k = 0; k < num_screens; k++) {
5539 if (TAILQ_EMPTY(&screens[k].rl))
5541 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5543 RB_FOREACH(kp, key_tree, &keys) {
5544 if ((code = xcb_key_symbols_get_keycode(syms,
5546 for (j = 0; j < LENGTH(modifiers); j++)
5547 xcb_grab_key(conn, True,
5549 kp->mod | modifiers[j],
5550 *code, XCB_GRAB_MODE_ASYNC,
5551 XCB_GRAB_MODE_ASYNC);
5557 grabbuttons(struct ws_win *win, int focused)
5560 unsigned int modifiers[] =
5561 { 0, LockMask, numlockmask, numlockmask|LockMask };
5563 updatenumlockmask();
5564 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5565 XCB_BUTTON_MASK_ANY);
5567 for (i = 0; i < LENGTH(buttons); i++)
5568 if (buttons[i].action == client_click)
5569 for (j = 0; j < LENGTH(modifiers); j++)
5570 xcb_grab_button(conn, False, win->id,
5572 XCB_GRAB_MODE_ASYNC,
5579 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5580 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC,
5581 XCB_WINDOW_NONE, XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY,
5582 XCB_BUTTON_MASK_ANY);
5585 const char *quirkname[] = {
5586 "NONE", /* config string for "no value" */
5595 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5596 #define SWM_Q_WS "\n|+ \t"
5598 parsequirks(char *qstr, unsigned long *quirk)
5608 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5610 cp += (long)strspn(cp, SWM_Q_WS);
5611 for (i = 0; i < LENGTH(quirkname); i++) {
5612 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5613 DNPRINTF(SWM_D_QUIRK,
5614 "parsequirks: %s\n", name);
5619 *quirk |= 1 << (i-1);
5623 if (i >= LENGTH(quirkname)) {
5624 DNPRINTF(SWM_D_QUIRK,
5625 "parsequirks: invalid quirk [%s]\n", name);
5633 quirk_insert(const char *class, const char *name, unsigned long quirk)
5637 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5640 if ((qp = malloc(sizeof *qp)) == NULL)
5641 err(1, "quirk_insert: malloc");
5642 if ((qp->class = strdup(class)) == NULL)
5643 err(1, "quirk_insert: strdup");
5644 if ((qp->name = strdup(name)) == NULL)
5645 err(1, "quirk_insert: strdup");
5648 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5650 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5654 quirk_remove(struct quirk *qp)
5656 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5657 qp->name, qp->quirk);
5659 TAILQ_REMOVE(&quirks, qp, entry);
5664 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5668 quirk_replace(struct quirk *qp, const char *class, const char *name,
5669 unsigned long quirk)
5671 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5672 qp->name, qp->quirk);
5675 quirk_insert(class, name, quirk);
5677 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5681 setquirk(const char *class, const char *name, unsigned long quirk)
5685 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5688 TAILQ_FOREACH(qp, &quirks, entry) {
5689 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5693 quirk_replace(qp, class, name, quirk);
5694 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5699 warnx("error: setquirk: cannot find class/name combination");
5703 quirk_insert(class, name, quirk);
5704 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5708 setconfquirk(char *selector, char *value, int flags)
5710 char *cp, *class, *name;
5712 unsigned long quirks;
5713 if (selector == NULL)
5715 if ((cp = strchr(selector, ':')) == NULL)
5720 if ((retval = parsequirks(value, &quirks)) == 0)
5721 setquirk(class, name, quirks);
5728 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5729 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5730 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5731 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5732 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5733 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5734 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5735 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5736 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5737 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5738 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5739 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5740 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5743 /* conf file stuff */
5744 #define SWM_CONF_FILE "spectrwm.conf"
5745 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5749 SWM_S_BAR_AT_BOTTOM,
5750 SWM_S_BAR_BORDER_WIDTH,
5757 SWM_S_CLOCK_ENABLED,
5760 SWM_S_CYCLE_VISIBLE,
5762 SWM_S_DISABLE_BORDER,
5764 SWM_S_FOCUS_CLOSE_WRAP,
5765 SWM_S_FOCUS_DEFAULT,
5771 SWM_S_STACK_ENABLED,
5773 SWM_S_TITLE_CLASS_ENABLED,
5774 SWM_S_TITLE_NAME_ENABLED,
5775 SWM_S_URGENT_ENABLED,
5776 SWM_S_VERBOSE_LAYOUT,
5777 SWM_S_WINDOW_NAME_ENABLED,
5778 SWM_S_WORKSPACE_LIMIT
5782 setconfvalue(char *selector, char *value, int flags)
5788 case SWM_S_BAR_ACTION:
5790 if ((bar_argv[0] = strdup(value)) == NULL)
5791 err(1, "setconfvalue: bar_action");
5793 case SWM_S_BAR_AT_BOTTOM:
5794 bar_at_bottom = atoi(value);
5796 case SWM_S_BAR_BORDER_WIDTH:
5797 bar_border_width = atoi(value);
5798 if (bar_border_width < 0)
5799 bar_border_width = 0;
5801 case SWM_S_BAR_DELAY:
5802 bar_delay = atoi(value);
5804 case SWM_S_BAR_ENABLED:
5805 bar_enabled = atoi(value);
5807 case SWM_S_BAR_FONT:
5809 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5810 err(1, "setconfvalue: asprintf: failed to allocate "
5811 "memory for bar_fonts.");
5815 case SWM_S_BAR_FORMAT:
5817 if ((bar_format = strdup(value)) == NULL)
5818 err(1, "setconfvalue: bar_format");
5820 case SWM_S_BAR_JUSTIFY:
5821 if (!strcmp(value, "left"))
5822 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5823 else if (!strcmp(value, "center"))
5824 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5825 else if (!strcmp(value, "right"))
5826 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5828 errx(1, "invalid bar_justify");
5830 case SWM_S_BORDER_WIDTH:
5831 border_width = atoi(value);
5832 if (border_width < 0)
5835 case SWM_S_CLOCK_ENABLED:
5836 clock_enabled = atoi(value);
5838 case SWM_S_CLOCK_FORMAT:
5839 #ifndef SWM_DENY_CLOCK_FORMAT
5841 if ((clock_format = strdup(value)) == NULL)
5842 err(1, "setconfvalue: clock_format");
5845 case SWM_S_CYCLE_EMPTY:
5846 cycle_empty = atoi(value);
5848 case SWM_S_CYCLE_VISIBLE:
5849 cycle_visible = atoi(value);
5851 case SWM_S_DIALOG_RATIO:
5852 dialog_ratio = atof(value);
5853 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5856 case SWM_S_DISABLE_BORDER:
5857 disable_border = atoi(value);
5859 case SWM_S_FOCUS_CLOSE:
5860 if (!strcmp(value, "first"))
5861 focus_close = SWM_STACK_BOTTOM;
5862 else if (!strcmp(value, "last"))
5863 focus_close = SWM_STACK_TOP;
5864 else if (!strcmp(value, "next"))
5865 focus_close = SWM_STACK_ABOVE;
5866 else if (!strcmp(value, "previous"))
5867 focus_close = SWM_STACK_BELOW;
5869 errx(1, "focus_close");
5871 case SWM_S_FOCUS_CLOSE_WRAP:
5872 focus_close_wrap = atoi(value);
5874 case SWM_S_FOCUS_DEFAULT:
5875 if (!strcmp(value, "last"))
5876 focus_default = SWM_STACK_TOP;
5877 else if (!strcmp(value, "first"))
5878 focus_default = SWM_STACK_BOTTOM;
5880 errx(1, "focus_default");
5882 case SWM_S_FOCUS_MODE:
5883 if (!strcmp(value, "default"))
5884 focus_mode = SWM_FOCUS_DEFAULT;
5885 else if (!strcmp(value, "follow_cursor"))
5886 focus_mode = SWM_FOCUS_FOLLOW;
5887 else if (!strcmp(value, "synergy"))
5888 focus_mode = SWM_FOCUS_SYNERGY;
5890 errx(1, "focus_mode");
5892 case SWM_S_SPAWN_ORDER:
5893 if (!strcmp(value, "first"))
5894 spawn_position = SWM_STACK_BOTTOM;
5895 else if (!strcmp(value, "last"))
5896 spawn_position = SWM_STACK_TOP;
5897 else if (!strcmp(value, "next"))
5898 spawn_position = SWM_STACK_ABOVE;
5899 else if (!strcmp(value, "previous"))
5900 spawn_position = SWM_STACK_BELOW;
5902 errx(1, "spawn_position");
5904 case SWM_S_SPAWN_TERM:
5905 setconfspawn("term", value, 0);
5906 setconfspawn("spawn_term", value, 0);
5910 case SWM_S_SS_ENABLED:
5911 ss_enabled = atoi(value);
5913 case SWM_S_STACK_ENABLED:
5914 stack_enabled = atoi(value);
5916 case SWM_S_TERM_WIDTH:
5917 term_width = atoi(value);
5921 case SWM_S_TITLE_CLASS_ENABLED:
5922 title_class_enabled = atoi(value);
5924 case SWM_S_TITLE_NAME_ENABLED:
5925 title_name_enabled = atoi(value);
5927 case SWM_S_URGENT_ENABLED:
5928 urgent_enabled = atoi(value);
5930 case SWM_S_VERBOSE_LAYOUT:
5931 verbose_layout = atoi(value);
5932 for (i = 0; layouts[i].l_stack != NULL; i++) {
5934 layouts[i].l_string = fancy_stacker;
5936 layouts[i].l_string = plain_stacker;
5939 case SWM_S_WINDOW_NAME_ENABLED:
5940 window_name_enabled = atoi(value);
5942 case SWM_S_WORKSPACE_LIMIT:
5943 workspace_limit = atoi(value);
5944 if (workspace_limit > SWM_WS_MAX)
5945 workspace_limit = SWM_WS_MAX;
5946 else if (workspace_limit < 1)
5947 workspace_limit = 1;
5956 setconfmodkey(char *selector, char *value, int flags)
5958 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5959 update_modkey(Mod1Mask);
5960 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5961 update_modkey(Mod2Mask);
5962 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5963 update_modkey(Mod3Mask);
5964 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5965 update_modkey(Mod4Mask);
5972 setconfcolor(char *selector, char *value, int flags)
5974 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5979 setconfregion(char *selector, char *value, int flags)
5981 custom_region(value);
5986 setautorun(char *selector, char *value, int flags)
5996 if (getenv("SWM_STARTED"))
6000 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6001 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6003 if (ws_id < 0 || ws_id >= workspace_limit)
6004 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6007 * This is a little intricate
6009 * If the pid already exists we simply reuse it because it means it was
6010 * used before AND not claimed by manage_window. We get away with
6011 * altering it in the parent after INSERT because this can not be a race
6014 while ((ap = strsep(&sp, " \t")) != NULL) {
6017 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6019 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6020 err(1, "setautorun: realloc");
6021 a.argv[argc - 1] = ap;
6024 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6025 err(1, "setautorun: realloc");
6026 a.argv[argc] = NULL;
6028 if ((pid = fork()) == 0) {
6029 spawn(ws_id, &a, 1);
6038 p = calloc(1, sizeof *p);
6041 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6051 setlayout(char *selector, char *value, int flags)
6053 int ws_id, i, x, mg, ma, si, raise, f = 0;
6054 int st = SWM_V_STACK, num_screens;
6056 struct workspace *ws;
6058 if (getenv("SWM_STARTED"))
6062 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6063 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6064 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6065 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6068 if (ws_id < 0 || ws_id >= workspace_limit)
6069 errx(1, "layout: invalid workspace %d", ws_id + 1);
6071 if (!strcasecmp(s, "vertical"))
6073 else if (!strcasecmp(s, "vertical_flip")) {
6076 } else if (!strcasecmp(s, "horizontal"))
6078 else if (!strcasecmp(s, "horizontal_flip")) {
6081 } else if (!strcasecmp(s, "fullscreen"))
6084 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6085 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6088 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6089 for (i = 0; i < num_screens; i++) {
6090 ws = (struct workspace *)&screens[i].ws;
6091 ws[ws_id].cur_layout = &layouts[st];
6093 ws[ws_id].always_raise = raise;
6094 if (st == SWM_MAX_STACK)
6098 for (x = 0; x < abs(mg); x++) {
6099 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6100 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6101 SWM_ARG_ID_MASTERSHRINK);
6105 for (x = 0; x < abs(ma); x++) {
6106 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6107 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6108 SWM_ARG_ID_MASTERDEL);
6112 for (x = 0; x < abs(si); x++) {
6113 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6114 si >= 0 ? SWM_ARG_ID_STACKINC :
6115 SWM_ARG_ID_STACKDEC);
6120 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6121 SWM_ARG_ID_FLIPLAYOUT);
6129 /* config options */
6130 struct config_option {
6132 int (*func)(char*, char*, int);
6135 struct config_option configopt[] = {
6136 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6137 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6138 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6139 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6140 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6141 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6142 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6143 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6144 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6145 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6146 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6147 { "keyboard_mapping", setkeymapping, 0 },
6148 { "bind", setconfbinding, 0 },
6149 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6150 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6151 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6152 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6153 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6154 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6155 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6156 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6157 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6158 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6159 { "modkey", setconfmodkey, 0 },
6160 { "program", setconfspawn, 0 },
6161 { "quirk", setconfquirk, 0 },
6162 { "region", setconfregion, 0 },
6163 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6164 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6165 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6166 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6167 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6168 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6169 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6170 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6171 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6172 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6173 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6174 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6175 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6176 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6177 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6178 { "autorun", setautorun, 0 },
6179 { "layout", setlayout, 0 },
6184 conf_load(char *filename, int keymapping)
6187 char *line, *cp, *optsub, *optval;
6188 size_t linelen, lineno = 0;
6189 int wordlen, i, optind;
6190 struct config_option *opt;
6192 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6194 if (filename == NULL) {
6195 warnx("conf_load: no filename");
6198 if ((config = fopen(filename, "r")) == NULL) {
6199 warn("conf_load: fopen: %s", filename);
6203 while (!feof(config)) {
6204 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6207 err(1, "%s", filename);
6212 cp += strspn(cp, " \t\n"); /* eat whitespace */
6213 if (cp[0] == '\0') {
6218 /* get config option */
6219 wordlen = strcspn(cp, "=[ \t\n");
6221 warnx("%s: line %zd: no option found",
6226 for (i = 0; i < LENGTH(configopt); i++) {
6227 opt = &configopt[i];
6228 if (!strncasecmp(cp, opt->optname, wordlen) &&
6229 strlen(opt->optname) == wordlen) {
6235 warnx("%s: line %zd: unknown option %.*s",
6236 filename, lineno, wordlen, cp);
6239 if (keymapping && strcmp(opt->optname, "bind")) {
6240 warnx("%s: line %zd: invalid option %.*s",
6241 filename, lineno, wordlen, cp);
6245 cp += strspn(cp, " \t\n"); /* eat whitespace */
6246 /* get [selector] if any */
6250 wordlen = strcspn(cp, "]");
6253 warnx("%s: line %zd: syntax error",
6258 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6260 warnx("%s: line %zd: unable to allocate"
6261 "memory for selector", filename,
6267 cp += strspn(cp, "] \t\n"); /* eat trailing */
6269 cp += strspn(cp, "= \t\n"); /* eat trailing */
6271 optval = strdup(cp);
6272 /* call function to deal with it all */
6273 if (configopt[optind].func(optsub, optval,
6274 configopt[optind].funcflags) != 0)
6275 errx(1, "%s: line %zd: invalid data for %s",
6276 filename, lineno, configopt[optind].optname);
6283 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6290 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6296 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6298 struct ws_win *parent, *w;
6299 struct swm_region *r;
6300 struct workspace *ws;
6301 xcb_icccm_wm_hints_t wmh;
6303 parent = find_window(win->transient);
6305 parent->child_trans = win;
6307 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6308 " for 0x%x trans 0x%x\n", win->id, win->transient);
6310 r = root_to_region(win->wa->root);
6312 /* parent doen't exist in our window list */
6313 TAILQ_FOREACH(w, &ws->winlist, entry) {
6314 if (xcb_icccm_get_wm_hints_reply(conn,
6315 xcb_icccm_get_wm_hints(conn, w->id),
6317 warnx("can't get hints for 0x%x", w->id);
6321 if (win->hints.window_group != wmh.window_group)
6324 w->child_trans = win;
6325 win->transient = w->id;
6327 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6328 "transient to 0x%x\n", win->transient);
6335 window_get_pid(xcb_window_t win)
6340 xcb_get_property_cookie_t pc;
6341 xcb_get_property_reply_t *pr;
6343 apid = get_atom_from_string("_NET_WM_PID");
6344 if (apid == XCB_ATOM_NONE)
6347 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6348 pr = xcb_get_property_reply(conn, pc, NULL);
6351 if (pr->type != XCB_ATOM_CARDINAL)
6354 ret = *(long *)xcb_get_property_value(pr);
6360 apid = get_atom_from_string("_SWM_PID");
6361 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6363 pr = xcb_get_property_reply(conn, pc, NULL);
6366 if (pr->type != XCB_ATOM_STRING)
6369 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6376 manage_window(xcb_window_t id)
6378 xcb_window_t trans = XCB_WINDOW_NONE;
6379 struct workspace *ws;
6380 struct ws_win *win, *ww;
6381 int i, ws_idx, border_me = 0;
6382 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6383 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6385 struct swm_region *r;
6389 uint32_t event_mask;
6391 xcb_get_property_reply_t *gpr;
6392 xcb_icccm_get_wm_protocols_reply_t wpr;
6394 if ((win = find_window(id)) != NULL)
6395 return (win); /* already being managed */
6397 /* see if we are on the unmanaged list */
6398 if ((win = find_unmanaged_window(id)) != NULL) {
6399 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6400 "window: 0x%x\n", win->id);
6401 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6403 set_child_transient(win, &trans);
6405 if (trans && (ww = find_window(trans)))
6406 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6407 else if ((ww = win->ws->focus) &&
6408 spawn_position == SWM_STACK_ABOVE)
6409 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6411 else if (ww && spawn_position == SWM_STACK_BELOW)
6412 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6413 else switch (spawn_position) {
6416 case SWM_STACK_ABOVE:
6417 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6419 case SWM_STACK_BOTTOM:
6420 case SWM_STACK_BELOW:
6421 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6424 ewmh_update_actions(win);
6428 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6429 err(1, "manage_window: calloc: failed to allocate memory for "
6435 /* see if we need to override the workspace */
6436 p = find_pid(window_get_pid(id));
6438 /* Get all the window data in one shot */
6439 ws_idx_atom = get_atom_from_string("_SWM_WS");
6441 gpr = xcb_get_property_reply(conn,
6442 xcb_get_property(conn, False, id, ws_idx_atom,
6443 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6446 proplen = xcb_get_property_value_length(gpr);
6448 prop = malloc(proplen + 1);
6451 xcb_get_property_value(gpr),
6453 prop[proplen] = '\0';
6459 win->wa = xcb_get_geometry_reply(conn,
6460 xcb_get_geometry(conn, id),
6462 xcb_icccm_get_wm_normal_hints_reply(conn,
6463 xcb_icccm_get_wm_normal_hints(conn, id),
6465 xcb_icccm_get_wm_hints_reply(conn,
6466 xcb_icccm_get_wm_hints(conn, id),
6468 xcb_icccm_get_wm_transient_for_reply(conn,
6469 xcb_icccm_get_wm_transient_for(conn, id),
6472 win->transient = trans;
6473 set_child_transient(win, &trans);
6474 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6475 "transient: 0x%x\n", win->id, win->transient);
6478 prot = get_atom_from_string("WM_PROTOCOLS");
6480 /* get supported protocols */
6481 if (xcb_icccm_get_wm_protocols_reply(conn,
6482 xcb_icccm_get_wm_protocols(conn, id, prot),
6484 for (i = 0; i < wpr.atoms_len; i++) {
6485 if (wpr.atoms[i] == takefocus)
6486 win->take_focus = 1;
6487 if (wpr.atoms[i] == adelete)
6488 win->can_delete = 1;
6490 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6493 win->iconic = get_iconic(win);
6496 * Figure out where to put the window. If it was previously assigned to
6497 * a workspace (either by spawn() or manually moving), and isn't
6498 * transient, * put it in the same workspace
6500 r = root_to_region(win->wa->root);
6502 ws = &r->s->ws[p->ws];
6503 TAILQ_REMOVE(&pidlist, p, entry);
6506 } else if (prop && win->transient == 0) {
6507 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6508 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6511 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6514 ws = &r->s->ws[ws_idx];
6517 /* this should launch transients in the same ws as parent */
6519 if ((ww = find_window(trans)) != NULL)
6525 warnx("manage_window: fix this "
6531 /* set up the window layout */
6534 win->s = r->s; /* this never changes */
6535 if (trans && (ww = find_window(trans)))
6536 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6537 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6538 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6540 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6541 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6542 else switch (spawn_position) {
6545 case SWM_STACK_ABOVE:
6546 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6548 case SWM_STACK_BOTTOM:
6549 case SWM_STACK_BELOW:
6550 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6553 /* ignore window border if there is one. */
6554 WIDTH(win) = win->wa->width;
6555 HEIGHT(win) = win->wa->height;
6556 X(win) = win->wa->x + win->wa->border_width;
6557 Y(win) = win->wa->y + win->wa->border_width;
6559 win->g_floatvalid = 0;
6560 win->floatmaxed = 0;
6561 win->ewmh_flags = 0;
6563 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6564 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6565 HEIGHT(win), ws->idx);
6567 constrain_window(win, r, 0);
6569 /* Set window properties so we can remember this after reincarnation */
6570 if (ws_idx_atom && prop == NULL &&
6571 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6573 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6575 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6576 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6582 ewmh_autoquirk(win);
6584 if (xcb_icccm_get_wm_class_reply(conn,
6585 xcb_icccm_get_wm_class(conn, win->id),
6587 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6588 win->ch.class_name, win->ch.instance_name);
6590 /* java is retarded so treat it special */
6591 if (strstr(win->ch.instance_name, "sun-awt")) {
6596 TAILQ_FOREACH(qp, &quirks, entry) {
6597 if (!strcmp(win->ch.class_name, qp->class) &&
6598 !strcmp(win->ch.instance_name, qp->name)) {
6599 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6600 "class: %s, name: %s\n", win->ch.class_name,
6601 win->ch.instance_name);
6602 if (qp->quirk & SWM_Q_FLOAT) {
6606 win->quirks = qp->quirk;
6611 /* alter window position if quirky */
6612 if (win->quirks & SWM_Q_ANYWHERE) {
6613 win->manual = 1; /* don't center the quirky windows */
6614 if (bar_enabled && Y(win) < bar_height)
6615 Y(win) = bar_height;
6616 if (WIDTH(win) + X(win) > WIDTH(r))
6617 X(win) = WIDTH(r) - WIDTH(win) - 2;
6621 /* Reset font sizes (the bruteforce way; no default keybinding). */
6622 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6623 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6624 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6625 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6626 fake_keypress(win, XK_KP_Add, ShiftMask);
6629 ewmh_get_win_state(win);
6630 ewmh_update_actions(win);
6631 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6636 X(win) -= border_width;
6637 Y(win) -= border_width;
6641 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6642 XCB_EVENT_MASK_PROPERTY_CHANGE |
6643 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6645 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6647 /* floaters need to be mapped if they are in the current workspace */
6648 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6649 map_window_raised(win->id);
6655 free_window(struct ws_win *win)
6657 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6662 /* needed for restart wm */
6663 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
6665 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6670 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6675 memset(win, 0xff, sizeof *win); /* XXX kill later */
6681 unmanage_window(struct ws_win *win)
6683 struct ws_win *parent;
6684 xcb_screen_t *screen;
6689 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6691 if (win->transient) {
6692 parent = find_window(win->transient);
6694 parent->child_trans = NULL;
6697 /* focus on root just in case */
6698 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6699 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6700 screen->root, XCB_CURRENT_TIME);
6704 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6705 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6709 focus_magic(struct ws_win *win)
6711 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6714 /* if there are no windows clear the status-bar */
6719 if (win->child_trans) {
6720 /* win = parent & has a transient so focus on that */
6722 focus_win(win->child_trans);
6723 if (win->child_trans->take_focus)
6724 client_msg(win, takefocus);
6726 /* make sure transient hasn't disappeared */
6727 if (validate_win(win->child_trans) == 0) {
6728 focus_win(win->child_trans);
6729 if (win->child_trans->take_focus)
6730 client_msg(win->child_trans, takefocus);
6732 win->child_trans = NULL;
6734 if (win->take_focus)
6735 client_msg(win, takefocus);
6741 if (win->take_focus)
6742 client_msg(win, takefocus);
6747 expose(xcb_expose_event_t *e)
6749 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6753 keypress(xcb_key_press_event_t *e)
6757 struct swm_region *r;
6759 keysym = XkbKeycodeToKeysym(display, (KeyCode)e->detail, 0, 0);
6761 DNPRINTF(SWM_D_EVENT, "keypress: %u\n", e->detail);
6763 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6765 if (keyfuncs[kp->funcid].func == NULL)
6768 r = root_to_region(e->root);
6769 if (kp->funcid == kf_spawn_custom)
6770 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6772 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6776 buttonpress(xcb_button_press_event_t *e)
6781 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x\n", e->event);
6783 if ((win = find_window(e->event)) == NULL)
6787 action = client_click;
6789 for (i = 0; i < LENGTH(buttons); i++)
6790 if (action == buttons[i].action && buttons[i].func &&
6791 buttons[i].button == e->detail &&
6792 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6793 buttons[i].func(win, &buttons[i].args);
6797 configurerequest(xcb_configure_request_event_t *e)
6802 uint32_t wc[7] = {0};
6804 if ((win = find_window(e->window)) == NULL)
6805 if ((win = find_unmanaged_window(e->window)) == NULL)
6809 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6810 mask |= XCB_CONFIG_WINDOW_X;
6813 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6814 mask |= XCB_CONFIG_WINDOW_Y;
6817 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6818 mask |= XCB_CONFIG_WINDOW_WIDTH;
6821 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6822 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6823 wc[i++] = e->height;
6825 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6826 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6827 wc[i++] = e->border_width;
6829 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6830 mask |= XCB_CONFIG_WINDOW_SIBLING;
6831 wc[i++] = e->sibling;
6833 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6834 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6835 wc[i++] = e->stack_mode;
6838 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6839 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e->window,
6840 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6842 xcb_configure_window(conn, e->window, mask, wc);
6843 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6844 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6845 win->g_float.x = e->x - X(win->ws->r);
6846 win->g_float.y = e->y - Y(win->ws->r);
6847 win->g_float.w = e->width;
6848 win->g_float.h = e->height;
6849 win->g_floatvalid = 1;
6851 if (win->floating) {
6852 win->g = win->g_float;
6853 win->g.x += X(win->ws->r);
6854 win->g.y += Y(win->ws->r);
6865 configurenotify(xcb_configure_notify_event_t *e)
6869 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6872 win = find_window(e->window);
6874 xcb_icccm_get_wm_normal_hints_reply(conn,
6875 xcb_icccm_get_wm_normal_hints(conn, win->id),
6884 destroynotify(xcb_destroy_notify_event_t *e)
6888 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6890 if ((win = find_window(e->window)) == NULL) {
6891 if ((win = find_unmanaged_window(e->window)) == NULL)
6897 /* make sure we focus on something */
6900 unmanage_window(win);
6906 enternotify(xcb_enter_notify_event_t *e)
6911 Window focus_return;
6912 int revert_to_return;
6914 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6915 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6916 "state: %d\n", e->event, e->mode, e->detail, e->root,
6917 e->child, YESNO(e->same_screen_focus), e->state);
6919 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6920 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6925 switch (focus_mode) {
6926 case SWM_FOCUS_DEFAULT:
6928 case SWM_FOCUS_FOLLOW:
6930 case SWM_FOCUS_SYNERGY:
6933 * all these checks need to be in this order because the
6934 * XCheckTypedWindowEvent relies on weeding out the previous events
6936 * making this code an option would enable a follow mouse for focus
6941 * state is set when we are switching workspaces and focus is set when
6942 * the window or a subwindow already has focus (occurs during restart).
6944 * Only honor the focus flag if last_focus_event is not FocusOut,
6945 * this allows spectrwm to continue to control focus when another
6946 * program is also playing with it.
6948 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6949 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6954 * happens when a window is created or destroyed and the border
6955 * crosses the mouse pointer and when switching ws
6957 * we need the subwindow test to see if we came from root in order
6958 * to give focus to floaters
6960 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6961 ev->subwindow == 0) {
6962 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6966 /* this window already has focus */
6967 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6968 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6972 /* this window is being deleted or moved to another ws */
6973 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6975 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6976 XPutBackEvent(display, &cne);
6980 if ((win = find_window(ev->window)) == NULL) {
6981 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6986 * In fullstack kill all enters unless they come from a different ws
6987 * (i.e. another region) or focus has been grabbed externally.
6989 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6990 last_focus_event != FocusOut) {
6991 XGetInputFocus(display, &focus_return, &revert_to_return);
6992 if ((w = find_window(focus_return)) == NULL ||
6994 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
7002 if ((win = find_window(e->event)) == NULL) {
7003 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7010 /* lets us use one switch statement for arbitrary mode/detail combinations */
7011 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7014 mapnotify(xcb_map_notify_event_t *e)
7018 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7020 win = manage_window(e->window);
7022 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7025 * focus_win can only set input focus on a mapped window.
7026 * make sure the window really has focus since it is just being mapped.
7028 if (win->ws->focus == win)
7033 mappingnotify(xcb_mapping_notify_event_t *e)
7035 xcb_refresh_keyboard_mapping(syms, e);
7037 if (e->request == XCB_MAPPING_KEYBOARD)
7042 maprequest(xcb_map_request_event_t *e)
7045 struct swm_region *r;
7046 xcb_get_window_attributes_reply_t *war;
7048 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7051 war = xcb_get_window_attributes_reply(conn,
7052 xcb_get_window_attributes(conn, e->window),
7056 if (war->override_redirect) {
7062 win = manage_window(e->window);
7064 return; /* can't happen */
7069 /* make new win focused */
7070 r = root_to_region(win->wa->root);
7071 if (win->ws == r->ws)
7076 propertynotify(xcb_property_notify_event_t *e)
7082 xcb_get_atom_name_reply_t *r;
7084 r = xcb_get_atom_name_reply(conn,
7085 xcb_get_atom_name(conn, e->atom),
7088 len = xcb_get_atom_name_name_length(r);
7090 name = malloc(len + 1);
7092 memcpy(name, xcb_get_atom_name_name(r), len);
7095 DNPRINTF(SWM_D_EVENT,
7096 "propertynotify: window: 0x%x, "
7106 win = find_window(e->window);
7110 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7111 update_iconic(win, 0);
7112 map_window_raised(win->id);
7120 case XCB_ATOM_WM_NORMAL_HINTS:
7121 xcb_icccm_get_wm_normal_hints(conn,
7122 xcb_iccom_get_wm_normal_hints(conn, win->id),
7124 warnx("normal hints: flag 0x%x", win->sh.flags);
7125 if (win->sh.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
7126 WIDTH(win) = win->sh.min_width;
7127 HEIGHT(win) = win->sh.min_height;
7128 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7130 XMoveResizeWindow(display, win->id,
7131 X(win), Y(win), WIDTH(win), HEIGHT(win));
7133 case XCB_ATOM_WM_CLASS:
7134 case XCB_ATOM_WM_NAME:
7143 unmapnotify(xcb_unmap_notify_event_t *e)
7147 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7149 /* determine if we need to help unmanage this window */
7150 win = find_window(e->window);
7154 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7155 unmanage_window(win);
7158 /* resend unmap because we ated it */
7159 xcb_unmap_window(conn, e->window);
7164 visibilitynotify(xcb_visibility_notify_event_t *e)
7167 struct swm_region *r;
7169 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7172 if (e->state == XCB_VISIBILITY_UNOBSCURED) {
7173 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7174 for (i = 0; i < num_screens; i++)
7175 TAILQ_FOREACH(r, &screens[i].rl, entry)
7176 if (e->window == WINID(r->bar))
7182 clientmessage(xcb_client_message_event_t *e)
7186 win = find_window(e->window);
7191 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, type: %u\n",
7192 e->window, e->response_type);
7194 if (e->response_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7195 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7198 if (e->response_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7199 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7200 if (win->can_delete)
7201 client_msg(win, adelete);
7203 xcb_kill_client(conn, win->id);
7205 if (e->response_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7206 DNPRINTF(SWM_D_EVENT,
7207 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7208 if (win->floating) {
7209 if (e->data.data32[0] & (1<<8)) /* x */
7210 X(win) = e->data.data32[1];
7211 if (e->data.data32[0] & (1<<9)) /* y */
7212 Y(win) = e->data.data32[2];
7213 if (e->data.data32[0] & (1<<10)) /* width */
7214 WIDTH(win) = e->data.data32[3];
7215 if (e->data.data32[0] & (1<<11)) /* height */
7216 HEIGHT(win) = e->data.data32[4];
7221 /* TODO: Change stack sizes */
7222 /* notify no change was made. */
7223 config_win(win, NULL);
7226 if (e->response_type == ewmh[_NET_WM_STATE].atom) {
7227 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7228 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7229 if (e->data.data32[2])
7230 ewmh_update_win_state(win, e->data.data32[2],
7238 xerror_start(Display *d, XErrorEvent *ee)
7245 xerror(Display *d, XErrorEvent *ee)
7247 /* warnx("error: %p %p", display, ee); */
7255 xerrorxlib = XSetErrorHandler(xerror_start);
7257 /* this causes an error if some other window manager is running */
7258 XSelectInput(display, DefaultRootWindow(display),
7259 SubstructureRedirectMask);
7264 XSetErrorHandler(xerror);
7270 new_region(struct swm_screen *s, int x, int y, int w, int h)
7272 struct swm_region *r, *n;
7273 struct workspace *ws = NULL;
7276 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7277 s->idx, w, h, x, y);
7279 /* remove any conflicting regions */
7280 n = TAILQ_FIRST(&s->rl);
7283 n = TAILQ_NEXT(r, entry);
7284 if (X(r) < (x + w) &&
7285 (X(r) + WIDTH(r)) > x &&
7287 (Y(r) + HEIGHT(r)) > y) {
7288 if (r->ws->r != NULL)
7289 r->ws->old_r = r->ws->r;
7292 TAILQ_REMOVE(&s->rl, r, entry);
7293 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7297 /* search old regions for one to reuse */
7299 /* size + location match */
7300 TAILQ_FOREACH(r, &s->orl, entry)
7301 if (X(r) == x && Y(r) == y &&
7302 HEIGHT(r) == h && WIDTH(r) == w)
7306 TAILQ_FOREACH(r, &s->orl, entry)
7307 if (HEIGHT(r) == h && WIDTH(r) == w)
7311 TAILQ_REMOVE(&s->orl, r, entry);
7312 /* try to use old region's workspace */
7313 if (r->ws->r == NULL)
7316 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7317 err(1, "new_region: calloc: failed to allocate memory "
7320 /* if we don't have a workspace already, find one */
7322 for (i = 0; i < workspace_limit; i++)
7323 if (s->ws[i].r == NULL) {
7330 errx(1, "new_region: no free workspaces");
7341 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7347 #ifdef SWM_XRR_HAS_CRTC
7350 #endif /* SWM_XRR_HAS_CRTC */
7351 struct swm_region *r;
7353 xcb_randr_get_screen_resources_current_cookie_t src;
7354 xcb_randr_get_screen_resources_current_reply_t *srr;
7355 xcb_randr_get_crtc_info_cookie_t cic;
7356 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7357 xcb_randr_crtc_t *crtc;
7358 xcb_screen_t *screen = get_screen(i);
7360 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7361 if (i >= num_screens)
7362 errx(1, "scan_xrandr: invalid screen");
7364 /* remove any old regions */
7365 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7366 r->ws->old_r = r->ws->r = NULL;
7368 TAILQ_REMOVE(&screens[i].rl, r, entry);
7369 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7373 /* map virtual screens onto physical screens */
7374 #ifdef SWM_XRR_HAS_CRTC
7375 if (xrandr_support) {
7376 src = xcb_randr_get_screen_resources_current(conn,
7378 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7381 new_region(&screens[i], 0, 0,
7382 screen->width_in_pixels,
7383 screen->height_in_pixels);
7386 ncrtc = srr->num_crtcs;
7387 for (c = 0; c < ncrtc; c++) {
7388 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7389 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7391 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7394 if (cir->num_outputs == 0) {
7400 new_region(&screens[i], 0, 0,
7401 screen->width_in_pixels,
7402 screen->height_in_pixels);
7404 new_region(&screens[i],
7405 cir->x, cir->y, cir->width, cir->height);
7410 #endif /* SWM_XRR_HAS_CRTC */
7412 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7413 screen->height_in_pixels);
7418 screenchange(xcb_randr_screen_change_notify_event_t *e)
7420 struct swm_region *r;
7423 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7425 if (e->rotation & (XCB_RANDR_ROTATION_ROTATE_90
7426 | XCB_RANDR_ROTATION_ROTATE_270))
7427 xcb_randr_set_screen_size(conn, e->root, e->height,
7428 e->width, e->mheight, e->mwidth);
7430 xcb_randr_set_screen_size(conn, e->root, e->width,
7431 e->height, e->mwidth, e->mheight);
7433 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7434 /* silly event doesn't include the screen index */
7435 for (i = 0; i < num_screens; i++)
7436 if (screens[i].root == e->root)
7438 if (i >= num_screens)
7439 errx(1, "screenchange: screen not found");
7441 /* brute force for now, just re-enumerate the regions */
7444 /* add bars to all regions */
7445 for (i = 0; i < num_screens; i++)
7446 TAILQ_FOREACH(r, &screens[i].rl, entry)
7454 xcb_window_t *wins = NULL;
7456 int i, j, num_screens;
7457 uint16_t state, manage;
7459 xcb_query_tree_cookie_t qtc;
7460 xcb_query_tree_reply_t *qtr;
7461 xcb_get_window_attributes_cookie_t c;
7462 xcb_get_window_attributes_reply_t *r;
7463 xcb_get_property_cookie_t pc;
7465 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7466 for (i = 0; i < num_screens; i++) {
7467 qtc = xcb_query_tree(conn, screens[i].root);
7468 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7471 wins = xcb_query_tree_children(qtr);
7472 no = xcb_query_tree_children_length(qtr);
7473 /* attach windows to a region */
7474 /* normal windows */
7475 for (j = 0; j < no; j++) {
7476 c = xcb_get_window_attributes(conn, wins[j]);
7477 r = xcb_get_window_attributes_reply(conn, c, NULL);
7480 if (r->override_redirect) {
7485 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7486 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7492 state = getstate(wins[j]);
7493 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7494 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7495 manage_window(wins[j]);
7498 /* transient windows */
7499 for (j = 0; j < no; j++) {
7500 c = xcb_get_window_attributes(conn, wins[j]);
7501 r = xcb_get_window_attributes_reply(conn, c, NULL);
7504 if (r->override_redirect) {
7510 state = getstate(wins[j]);
7511 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7512 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7513 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7515 manage_window(wins[j]);
7524 int i, j, k, num_screens;
7525 struct workspace *ws;
7526 uint32_t gcv[1], wa[1];
7527 const xcb_query_extension_reply_t *qep;
7528 xcb_cursor_t cursor;
7529 xcb_font_t cursor_font;
7530 xcb_randr_query_version_cookie_t c;
7531 xcb_randr_query_version_reply_t *r;
7533 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7534 if ((screens = calloc(num_screens,
7535 sizeof(struct swm_screen))) == NULL)
7536 err(1, "setup_screens: calloc: failed to allocate memory for "
7539 /* initial Xrandr setup */
7540 xrandr_support = False;
7541 c = xcb_randr_query_version(conn, True, True);
7542 r = xcb_randr_query_version_reply(conn, c, NULL);
7544 if (r->major_version >= 1)
7545 xrandr_support = True;
7548 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7549 xrandr_eventbase = qep->first_event;
7551 cursor_font = xcb_generate_id(conn);
7552 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7554 cursor = xcb_generate_id(conn);
7555 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7556 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7559 /* map physical screens */
7560 for (i = 0; i < num_screens; i++) {
7561 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7563 TAILQ_INIT(&screens[i].rl);
7564 TAILQ_INIT(&screens[i].orl);
7565 screens[i].root = RootWindow(display, i);
7567 /* set default colors */
7568 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7569 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7570 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7571 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7572 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7574 /* create graphics context on screen */
7575 screens[i].bar_gc = xcb_generate_id(conn);
7577 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7578 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7580 /* set default cursor */
7581 xcb_change_window_attributes(conn, screens[i].root,
7584 /* init all workspaces */
7585 /* XXX these should be dynamically allocated too */
7586 for (j = 0; j < SWM_WS_MAX; j++) {
7587 ws = &screens[i].ws[j];
7593 TAILQ_INIT(&ws->winlist);
7594 TAILQ_INIT(&ws->unmanagedlist);
7596 for (k = 0; layouts[k].l_stack != NULL; k++)
7597 if (layouts[k].l_config != NULL)
7598 layouts[k].l_config(ws,
7599 SWM_ARG_ID_STACKINIT);
7600 ws->cur_layout = &layouts[0];
7601 ws->cur_layout->l_string(ws);
7607 xcb_randr_select_input(conn, screens[i].root,
7608 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7610 xcb_free_cursor(conn, cursor);
7611 xcb_close_font(conn, cursor_font);
7617 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7618 err(1, "setup_globals: strdup: failed to allocate memory.");
7620 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7621 err(1, "setup_globals: strdup: failed to allocate memory.");
7628 xcb_atom_t netwmcheck, netwmname, utf8_string;
7629 xcb_window_t root, win;
7632 /* work around sun jdk bugs, code from wmname */
7633 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7634 netwmname = get_atom_from_string("_NET_WM_NAME");
7635 utf8_string = get_atom_from_string("UTF8_STRING");
7637 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7638 for (i = 0; i < num_screens; i++) {
7639 root = screens[i].root;
7641 win = xcb_generate_id(conn);
7642 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7643 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7644 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7645 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7646 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7648 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7649 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7650 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7651 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7652 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7653 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7658 event_handle(xcb_generic_event_t *evt)
7660 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7664 /* XXX - handle error */
7669 #define EVENT(type, callback) case type: callback((void *)evt); return
7670 EVENT(XCB_BUTTON_PRESS, buttonpress);
7671 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7672 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7673 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7674 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7675 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7676 EVENT(XCB_ENTER_NOTIFY, enternotify);
7677 EVENT(XCB_EXPOSE, expose);
7678 EVENT(XCB_KEY_PRESS, keypress);
7679 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7680 EVENT(XCB_MAP_NOTIFY, mapnotify);
7681 EVENT(XCB_MAP_REQUEST, maprequest);
7682 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7683 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7684 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7685 EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);
7688 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7689 screenchange((void *)evt);
7693 main(int argc, char *argv[])
7695 struct swm_region *r, *rr;
7696 struct ws_win *winfocus = NULL;
7699 char conf[PATH_MAX], *cfile = NULL;
7701 int xfd, i, num_screens;
7703 struct sigaction sact;
7704 xcb_generic_event_t *evt;
7707 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7708 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7710 warnx("no locale support");
7712 if (!X_HAVE_UTF8_STRING)
7713 warnx("no UTF-8 support");
7715 if (!(display = XOpenDisplay(0)))
7716 errx(1, "can not open display");
7718 /* handle some signals */
7719 bzero(&sact, sizeof(sact));
7720 sigemptyset(&sact.sa_mask);
7722 sact.sa_handler = sighdlr;
7723 sigaction(SIGINT, &sact, NULL);
7724 sigaction(SIGQUIT, &sact, NULL);
7725 sigaction(SIGTERM, &sact, NULL);
7726 sigaction(SIGHUP, &sact, NULL);
7728 sact.sa_handler = sighdlr;
7729 sact.sa_flags = SA_NOCLDSTOP;
7730 sigaction(SIGCHLD, &sact, NULL);
7732 conn = XGetXCBConnection(display);
7733 if (xcb_connection_has_error(conn))
7734 errx(1, "can not get XCB connection");
7736 xfd = xcb_get_file_descriptor(conn);
7737 syms = xcb_key_symbols_alloc(conn);
7739 errx(1, "unable to allocate key symbols");
7741 astate = get_atom_from_string("WM_STATE");
7742 aprot = get_atom_from_string("WM_PROTOCOLS");
7743 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7744 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7745 a_wmname = get_atom_from_string("WM_NAME");
7746 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7747 a_utf8_string = get_atom_from_string("UTF8_STRING");
7748 a_string = get_atom_from_string("STRING");
7749 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7751 /* look for local and global conf file */
7752 pwd = getpwuid(getuid());
7754 errx(1, "invalid user: %d", getuid());
7756 xcb_grab_server(conn);
7759 /* flush all events */
7760 while ((evt = xcb_poll_for_event(conn))) {
7761 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7768 errx(1, "other wm running");
7779 for (i = 0; ; i++) {
7784 snprintf(conf, sizeof conf, "%s/.%s",
7785 pwd->pw_dir, SWM_CONF_FILE);
7789 snprintf(conf, sizeof conf, "/etc/%s",
7794 snprintf(conf, sizeof conf, "%s/.%s",
7795 pwd->pw_dir, SWM_CONF_FILE_OLD);
7799 snprintf(conf, sizeof conf, "/etc/%s",
7806 if (strlen(conf) && stat(conf, &sb) != -1)
7807 if (S_ISREG(sb.st_mode)) {
7814 /* load conf (if any) */
7816 conf_load(cfile, SWM_CONF_DEFAULT);
7819 /* set some values to work around bad programs */
7821 /* grab existing windows (before we build the bars) */
7824 if (getenv("SWM_STARTED") == NULL)
7825 setenv("SWM_STARTED", "YES", 1);
7827 /* setup all bars */
7828 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7829 for (i = 0; i < num_screens; i++)
7830 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7831 if (winfocus == NULL)
7832 winfocus = TAILQ_FIRST(&r->ws->winlist);
7841 xcb_ungrab_server(conn);
7845 while ((evt = xcb_poll_for_event(conn))) {
7846 DNPRINTF(SWM_D_EVENT, "XCB Event: %s\n",
7854 /* if we are being restarted go focus on first window */
7856 rr = winfocus->ws->r;
7858 /* not a visible window */
7862 /* move pointer to first screen if multi screen */
7863 if (num_screens > 1 || outputs > 1)
7864 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7865 rr->s[0].root, 0, 0, 0, 0, X(rr),
7866 Y(rr) + (bar_enabled ? bar_height : 0));
7868 a.id = SWM_ARG_ID_FOCUSCUR;
7878 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7880 DNPRINTF(SWM_D_MISC, "select failed");
7881 if (restart_wm == 1)
7882 restart(NULL, NULL);
7883 if (search_resp == 1)
7896 for (i = 0; i < num_screens; ++i)
7897 if (screens[i].bar_gc != 0)
7898 xcb_free_gc(conn, screens[i].bar_gc);
7899 XFreeFontSet(display, bar_fs);
7901 xcb_key_symbols_free(syms);
7902 xcb_disconnect(conn);