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
155 #define XCB_REQUEST XCB_EVENT_ERROR_BAD_REQUEST
156 #define XCB_VALUE XCB_EVENT_ERROR_BAD_VALUE
157 #define XCB_WINDOW XCB_EVENT_ERROR_BAD_WINDOW
158 #define XCB_PIXMAP XCB_EVENT_ERROR_BAD_PIXMAP
159 #define XCB_ATOM XCB_EVENT_ERROR_BAD_ATOM
160 #define XCB CURSOR XCB_EVENT_ERROR_BAD_CURSOR
161 #define XCB_FONT XCB_EVENT_ERROR_BAD_FONT
162 #define XCB_MATCH XCB_EVENT_ERROR_BAD_MATCH
163 #define XCB_DRAWABLE XCB_EVENT_ERROR_BAD_DRAWABLE
164 #define XCB_ACCESS XCB_EVENT_ERROR_BAD_ACCESS
165 #define XCB_ALLOC XCB_EVENT_ERROR_BAD_ALLOC
166 #define XCB_COLORMAP XCB_EVENT_ERROR_BAD_COLOR
167 #define XCB_G_CONTEXT XCB_EVENT_ERROR_BAD_GC
168 #define XCB_ID_CHOICE XCB_EVENT_ERROR_BAD_ID_CHOICE
169 #define XCB_NAME XCB_EVENT_ERROR_BAD_NAME
170 #define XCB_LENGTH XCB_EVENT_ERROR_BAD_LENGTH
171 #define XCB_IMPLEMENTATION XCB_EVENT_ERROR_BAD_IMPLEMENTATION
176 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
177 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
178 #define SWM_D_MISC 0x0001
179 #define SWM_D_EVENT 0x0002
180 #define SWM_D_WS 0x0004
181 #define SWM_D_FOCUS 0x0008
182 #define SWM_D_MOVE 0x0010
183 #define SWM_D_STACK 0x0020
184 #define SWM_D_MOUSE 0x0040
185 #define SWM_D_PROP 0x0080
186 #define SWM_D_CLASS 0x0100
187 #define SWM_D_KEY 0x0200
188 #define SWM_D_QUIRK 0x0400
189 #define SWM_D_SPAWN 0x0800
190 #define SWM_D_EVENTQ 0x1000
191 #define SWM_D_CONF 0x2000
192 #define SWM_D_BAR 0x4000
194 u_int32_t swm_debug = 0
212 #define DPRINTF(x...)
213 #define DNPRINTF(n,x...)
216 #define LENGTH(x) (sizeof x / sizeof x[0])
217 #define MODKEY Mod1Mask
218 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
219 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
220 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
221 #define SWM_PROPLEN (16)
222 #define SWM_FUNCNAME_LEN (32)
223 #define SWM_KEYS_LEN (255)
224 #define SWM_QUIRK_LEN (64)
225 #define X(r) (r)->g.x
226 #define Y(r) (r)->g.y
227 #define WIDTH(r) (r)->g.w
228 #define HEIGHT(r) (r)->g.h
229 #define BORDER(w) (w->bordered ? border_width : 0)
230 #define MAX_X(r) ((r)->g.x + (r)->g.w)
231 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
232 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
233 #define SH_MIN_W(w) (w)->sh.min_width
234 #define SH_MIN_H(w) (w)->sh.min_height
235 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
236 #define SH_MAX_W(w) (w)->sh.max_width
237 #define SH_MAX_H(w) (w)->sh.max_height
238 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
239 #define SH_INC_W(w) (w)->sh.width_inc
240 #define SH_INC_H(w) (w)->sh.height_inc
241 #define SWM_MAX_FONT_STEPS (3)
242 #define WINID(w) ((w) ? (w)->id : 0)
243 #define YESNO(x) ((x) ? "yes" : "no")
245 #define SWM_FOCUS_DEFAULT (0)
246 #define SWM_FOCUS_SYNERGY (1)
247 #define SWM_FOCUS_FOLLOW (2)
249 #define SWM_CONF_DEFAULT (0)
250 #define SWM_CONF_KEYMAPPING (1)
253 #define SWM_LIB "/usr/local/lib/libswmhack.so"
260 xcb_atom_t takefocus;
262 xcb_atom_t a_netwmname;
263 xcb_atom_t a_utf8_string;
265 xcb_atom_t a_swm_iconic;
266 volatile sig_atomic_t running = 1;
267 volatile sig_atomic_t restart_wm = 0;
269 int last_focus_event = FocusOut;
270 int (*xerrorxlib)(Display *, XErrorEvent *);
274 int xrandr_eventbase;
275 unsigned int numlockmask = 0;
277 xcb_connection_t *conn;
278 xcb_key_symbols_t *syms;
281 int cycle_visible = 0;
283 int font_adjusted = 0;
284 unsigned int mod_key = MODKEY;
287 struct swm_region *search_r;
288 int select_list_pipe[2];
289 int select_resp_pipe[2];
291 volatile sig_atomic_t search_resp;
292 int search_resp_action;
294 struct search_window {
295 TAILQ_ENTRY(search_window) entry;
299 xcb_window_t indicator;
301 TAILQ_HEAD(search_winlist, search_window);
302 struct search_winlist search_wl;
307 SWM_SEARCH_UNICONIFY,
308 SWM_SEARCH_NAME_WORKSPACE,
309 SWM_SEARCH_SEARCH_WORKSPACE,
310 SWM_SEARCH_SEARCH_WINDOW
313 #define SWM_STACK_TOP (0)
314 #define SWM_STACK_BOTTOM (1)
315 #define SWM_STACK_ABOVE (2)
316 #define SWM_STACK_BELOW (3)
319 double dialog_ratio = 0.6;
321 #define SWM_BAR_MAX (256)
322 #define SWM_BAR_JUSTIFY_LEFT (0)
323 #define SWM_BAR_JUSTIFY_CENTER (1)
324 #define SWM_BAR_JUSTIFY_RIGHT (2)
325 #define SWM_BAR_OFFSET (4)
326 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
327 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
328 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
329 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
330 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
332 char *bar_argv[] = { NULL, NULL };
334 unsigned char bar_ext[SWM_BAR_MAX];
335 char bar_vertext[SWM_BAR_MAX];
337 sig_atomic_t bar_alarm = 0;
340 int bar_border_width = 1;
341 int bar_at_bottom = 0;
343 int bar_extra_running = 0;
346 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
347 char *bar_format = NULL;
348 int stack_enabled = 1;
349 int clock_enabled = 1;
350 int urgent_enabled = 0;
351 char *clock_format = NULL;
352 int title_name_enabled = 0;
353 int title_class_enabled = 0;
354 int window_name_enabled = 0;
355 int focus_mode = SWM_FOCUS_DEFAULT;
356 int focus_close = SWM_STACK_BELOW;
357 int focus_close_wrap = 1;
358 int focus_default = SWM_STACK_TOP;
359 int spawn_position = SWM_STACK_TOP;
360 int disable_border = 0;
361 int border_width = 1;
362 int verbose_layout = 0;
365 XFontSetExtents *bar_fs_extents;
369 #define SWM_MENU_FN (2)
370 #define SWM_MENU_NB (4)
371 #define SWM_MENU_NF (6)
372 #define SWM_MENU_SB (8)
373 #define SWM_MENU_SF (10)
375 /* layout manager data */
376 struct swm_geometry {
389 struct swm_geometry g;
392 /* virtual "screens" */
394 TAILQ_ENTRY(swm_region) entry;
395 struct swm_geometry g;
396 struct workspace *ws; /* current workspace on this region */
397 struct workspace *ws_prior; /* prior workspace on this region */
398 struct swm_screen *s; /* screen idx */
401 TAILQ_HEAD(swm_region_list, swm_region);
404 TAILQ_ENTRY(ws_win) entry;
406 xcb_window_t transient;
407 struct ws_win *child_trans; /* transient child window */
408 struct swm_geometry g; /* current geometry */
409 struct swm_geometry g_float; /* region coordinates */
410 int g_floatvalid; /* g_float geometry validity */
411 int floatmaxed; /* whether maxed by max_stack */
416 unsigned int ewmh_flags;
417 int font_size_boundary[SWM_MAX_FONT_STEPS];
423 unsigned long quirks;
424 struct workspace *ws; /* always valid */
425 struct swm_screen *s; /* always valid, never changes */
426 xcb_get_geometry_reply_t *wa;
428 xcb_icccm_get_wm_class_reply_t ch;
429 xcb_icccm_wm_hints_t hints;
431 TAILQ_HEAD(ws_win_list, ws_win);
435 TAILQ_ENTRY(pid_e) entry;
439 TAILQ_HEAD(pid_list, pid_e);
440 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
442 /* layout handlers */
444 void vertical_config(struct workspace *, int);
445 void vertical_stack(struct workspace *, struct swm_geometry *);
446 void horizontal_config(struct workspace *, int);
447 void horizontal_stack(struct workspace *, struct swm_geometry *);
448 void max_stack(struct workspace *, struct swm_geometry *);
449 void plain_stacker(struct workspace *);
450 void fancy_stacker(struct workspace *);
452 struct ws_win *find_window(xcb_window_t);
454 void grabbuttons(struct ws_win *, int);
455 void new_region(struct swm_screen *, int, int, int, int);
456 void unmanage_window(struct ws_win *);
457 uint16_t getstate(xcb_window_t);
459 int conf_load(char *, int);
462 void (*l_stack)(struct workspace *, struct swm_geometry *);
463 void (*l_config)(struct workspace *, int);
465 #define SWM_L_FOCUSPREV (1<<0)
466 #define SWM_L_MAPONFOCUS (1<<1)
467 void (*l_string)(struct workspace *);
469 /* stack, configure */
470 { vertical_stack, vertical_config, 0, plain_stacker },
471 { horizontal_stack, horizontal_config, 0, plain_stacker },
473 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
474 { NULL, NULL, 0, NULL },
477 /* position of max_stack mode in the layouts array, index into layouts! */
478 #define SWM_V_STACK (0)
479 #define SWM_H_STACK (1)
480 #define SWM_MAX_STACK (2)
482 #define SWM_H_SLICE (32)
483 #define SWM_V_SLICE (32)
485 /* define work spaces */
487 int idx; /* workspace index */
488 char *name; /* workspace name */
489 int always_raise; /* raise windows on focus */
490 struct layout *cur_layout; /* current layout handlers */
491 struct ws_win *focus; /* may be NULL */
492 struct ws_win *focus_prev; /* may be NULL */
493 struct swm_region *r; /* may be NULL */
494 struct swm_region *old_r; /* may be NULL */
495 struct ws_win_list winlist; /* list of windows in ws */
496 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
497 char stacker[10]; /* display stacker and layout */
501 int horizontal_msize;
503 int horizontal_stacks;
514 SWM_S_COLOR_BAR_BORDER,
515 SWM_S_COLOR_BAR_FONT,
521 /* physical screen mapping */
522 #define SWM_WS_MAX (22) /* hard limit */
523 int workspace_limit = 10; /* soft limit */
526 int idx; /* screen index */
527 struct swm_region_list rl; /* list of regions on this screen */
528 struct swm_region_list orl; /* list of old regions */
530 struct workspace ws[SWM_WS_MAX];
536 } c[SWM_S_COLOR_MAX];
538 xcb_gcontext_t bar_gc;
540 struct swm_screen *screens;
542 /* args to functions */
545 #define SWM_ARG_ID_FOCUSNEXT (0)
546 #define SWM_ARG_ID_FOCUSPREV (1)
547 #define SWM_ARG_ID_FOCUSMAIN (2)
548 #define SWM_ARG_ID_FOCUSCUR (4)
549 #define SWM_ARG_ID_SWAPNEXT (10)
550 #define SWM_ARG_ID_SWAPPREV (11)
551 #define SWM_ARG_ID_SWAPMAIN (12)
552 #define SWM_ARG_ID_MOVELAST (13)
553 #define SWM_ARG_ID_MASTERSHRINK (20)
554 #define SWM_ARG_ID_MASTERGROW (21)
555 #define SWM_ARG_ID_MASTERADD (22)
556 #define SWM_ARG_ID_MASTERDEL (23)
557 #define SWM_ARG_ID_FLIPLAYOUT (24)
558 #define SWM_ARG_ID_STACKRESET (30)
559 #define SWM_ARG_ID_STACKINIT (31)
560 #define SWM_ARG_ID_CYCLEWS_UP (40)
561 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
562 #define SWM_ARG_ID_CYCLESC_UP (42)
563 #define SWM_ARG_ID_CYCLESC_DOWN (43)
564 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
565 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
566 #define SWM_ARG_ID_STACKINC (50)
567 #define SWM_ARG_ID_STACKDEC (51)
568 #define SWM_ARG_ID_SS_ALL (60)
569 #define SWM_ARG_ID_SS_WINDOW (61)
570 #define SWM_ARG_ID_DONTCENTER (70)
571 #define SWM_ARG_ID_CENTER (71)
572 #define SWM_ARG_ID_KILLWINDOW (80)
573 #define SWM_ARG_ID_DELETEWINDOW (81)
574 #define SWM_ARG_ID_WIDTHGROW (90)
575 #define SWM_ARG_ID_WIDTHSHRINK (91)
576 #define SWM_ARG_ID_HEIGHTGROW (92)
577 #define SWM_ARG_ID_HEIGHTSHRINK (93)
578 #define SWM_ARG_ID_MOVEUP (100)
579 #define SWM_ARG_ID_MOVEDOWN (101)
580 #define SWM_ARG_ID_MOVELEFT (102)
581 #define SWM_ARG_ID_MOVERIGHT (103)
585 void focus(struct swm_region *, union arg *);
586 void focus_magic(struct ws_win *);
590 TAILQ_ENTRY(quirk) entry;
594 #define SWM_Q_FLOAT (1<<0) /* float this window */
595 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
596 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
597 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
598 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
599 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
601 TAILQ_HEAD(quirk_list, quirk);
602 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
605 * Supported EWMH hints should be added to
606 * both the enum and the ewmh array
611 _NET_MOVERESIZE_WINDOW,
612 _NET_WM_ACTION_CLOSE,
613 _NET_WM_ACTION_FULLSCREEN,
615 _NET_WM_ACTION_RESIZE,
616 _NET_WM_ALLOWED_ACTIONS,
619 _NET_WM_STATE_FULLSCREEN,
620 _NET_WM_STATE_HIDDEN,
621 _NET_WM_STATE_MAXIMIZED_HORZ,
622 _NET_WM_STATE_MAXIMIZED_VERT,
623 _NET_WM_STATE_SKIP_PAGER,
624 _NET_WM_STATE_SKIP_TASKBAR,
626 _NET_WM_WINDOW_TYPE_DIALOG,
627 _NET_WM_WINDOW_TYPE_DOCK,
628 _NET_WM_WINDOW_TYPE_NORMAL,
629 _NET_WM_WINDOW_TYPE_SPLASH,
630 _NET_WM_WINDOW_TYPE_TOOLBAR,
631 _NET_WM_WINDOW_TYPE_UTILITY,
632 _SWM_WM_STATE_MANUAL,
639 } ewmh[SWM_EWMH_HINT_MAX] = {
640 /* must be in same order as in the enum */
641 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
642 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
643 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
644 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
645 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
646 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
647 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
648 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
649 {"_NET_WM_STATE", XCB_ATOM_NONE},
650 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
651 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
652 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
653 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
654 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
655 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
656 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
657 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
658 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
659 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
660 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
661 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
662 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
663 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
664 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
667 void store_float_geom(struct ws_win *, struct swm_region *);
668 int floating_toggle_win(struct ws_win *);
669 void constrain_window(struct ws_win *, struct swm_region *, int);
670 void update_window(struct ws_win *);
671 void spawn_select(struct swm_region *, union arg *, char *, int *);
672 char *get_win_name(xcb_window_t);
673 xcb_atom_t get_atom_from_string(const char *);
674 void map_window_raised(xcb_window_t);
676 xcb_screen_t *get_screen(int);
677 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
678 void event_handle(xcb_generic_event_t *);
681 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
683 unsigned int tmpr, tmpg, tmpb;
685 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
696 get_screen(int screen)
698 xcb_screen_iterator_t i;
700 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
701 for (; i.rem; --screen, xcb_screen_next(&i))
711 xcb_get_input_focus_cookie_t c;
712 xcb_get_input_focus_reply_t *r;
715 c = xcb_get_input_focus(conn);
717 r = xcb_get_input_focus_reply(conn, c, NULL);
723 map_window_raised(xcb_window_t win)
725 uint32_t val = XCB_STACK_MODE_ABOVE;
727 xcb_configure_window(conn, win,
728 XCB_CONFIG_WINDOW_STACK_MODE, &val);
730 xcb_map_window(conn, win);
735 get_atom_from_string(const char *str)
737 xcb_intern_atom_cookie_t c;
738 xcb_intern_atom_reply_t *r;
741 c = xcb_intern_atom(conn, False, strlen(str), str);
742 r = xcb_intern_atom_reply(conn, c, NULL);
750 return (XCB_ATOM_NONE);
754 update_iconic(struct ws_win *win, int newv)
761 iprop = get_atom_from_string("_SWM_ICONIC");
762 if (iprop == XCB_ATOM_NONE)
766 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
767 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
769 xcb_delete_property(conn, win->id, iprop);
773 get_iconic(struct ws_win *win)
777 xcb_get_property_reply_t *pr = NULL;
779 iprop = get_atom_from_string("_SWM_ICONIC");
780 if (iprop == XCB_ATOM_NONE)
783 pr = xcb_get_property_reply(conn,
784 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
788 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
790 v = *((int32_t *)xcb_get_property_value(pr));
801 int i, j, num_screens;
803 sup_list = get_atom_from_string("_NET_SUPPORTED");
805 for (i = 0; i < LENGTH(ewmh); i++)
806 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
808 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
809 for (i = 0; i < num_screens; i++) {
810 /* Support check window will be created by workaround(). */
812 /* Report supported atoms */
813 xcb_delete_property(conn, screens[i].root, sup_list);
814 for (j = 0; j < LENGTH(ewmh); j++)
815 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
816 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
825 xcb_atom_t sup_check, sup_list;
827 xcb_get_property_cookie_t pc;
828 xcb_get_property_reply_t *pr;
830 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
831 sup_list = get_atom_from_string("_NET_SUPPORTED");
832 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
834 for (i = 0; i < num_screens; i++) {
835 /* Get the support check window and destroy it */
836 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
837 XCB_ATOM_WINDOW, 0, 1);
838 pr = xcb_get_property_reply(conn, pc, NULL);
840 id = *((xcb_window_t *)xcb_get_property_value(pr));
842 xcb_destroy_window(conn, id);
843 xcb_delete_property(conn, screens[i].root, sup_check);
844 xcb_delete_property(conn, screens[i].root, sup_list);
852 ewmh_autoquirk(struct ws_win *win)
858 xcb_get_property_cookie_t c;
859 xcb_get_property_reply_t *r;
861 c = xcb_get_property(conn, False, win->id,
862 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
863 r = xcb_get_property_reply(conn, c, NULL);
866 n = xcb_get_property_value_length(r);
868 for (i = 0; i < n; i++) {
869 type = *((xcb_atom_t *)xcb_get_property_value(r));
870 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
872 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
873 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
874 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
876 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
879 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
880 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
882 win->quirks = SWM_Q_FLOAT;
889 #define SWM_EWMH_ACTION_COUNT_MAX (6)
890 #define EWMH_F_FULLSCREEN (1<<0)
891 #define EWMH_F_ABOVE (1<<1)
892 #define EWMH_F_HIDDEN (1<<2)
893 #define EWMH_F_SKIP_PAGER (1<<3)
894 #define EWMH_F_SKIP_TASKBAR (1<<4)
895 #define SWM_F_MANUAL (1<<5)
898 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
906 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
907 "fullscreen %s\n", win->id, YESNO(fs));
910 if (!win->g_floatvalid)
911 store_float_geom(win, win->ws->r);
913 win->g = win->ws->r->g;
916 if (win->g_floatvalid) {
917 /* refloat at last floating relative position */
918 win->g = win->g_float;
919 X(win) += X(win->ws->r);
920 Y(win) += Y(win->ws->r);
928 ewmh_update_actions(struct ws_win *win)
930 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
936 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
939 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
940 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
943 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
944 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
947 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
948 #define _NET_WM_STATE_ADD 1 /* add/set property */
949 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
952 ewmh_update_win_state(struct ws_win *win, long state, long action)
954 unsigned int mask = 0;
955 unsigned int changed = 0;
956 unsigned int orig_flags;
961 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
962 mask = EWMH_F_FULLSCREEN;
963 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
965 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
967 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
968 mask = EWMH_F_SKIP_PAGER;
969 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
970 mask = EWMH_F_SKIP_TASKBAR;
973 orig_flags = win->ewmh_flags;
976 case _NET_WM_STATE_REMOVE:
977 win->ewmh_flags &= ~mask;
979 case _NET_WM_STATE_ADD:
980 win->ewmh_flags |= mask;
982 case _NET_WM_STATE_TOGGLE:
983 win->ewmh_flags ^= mask;
987 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
989 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
991 if (!floating_toggle_win(win))
992 win->ewmh_flags = orig_flags; /* revert */
993 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
995 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
996 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
998 if (!ewmh_set_win_fullscreen(win,
999 win->ewmh_flags & EWMH_F_FULLSCREEN))
1000 win->ewmh_flags = orig_flags; /* revert */
1002 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1004 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1005 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1006 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1007 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1008 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1009 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1010 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1011 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1012 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1013 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1014 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1015 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1016 if (win->ewmh_flags & EWMH_F_ABOVE)
1017 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1018 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1019 &ewmh[_NET_WM_STATE_ABOVE].atom);
1020 if (win->ewmh_flags & SWM_F_MANUAL)
1021 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1022 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1023 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1027 ewmh_get_win_state(struct ws_win *win)
1030 xcb_get_property_cookie_t c;
1031 xcb_get_property_reply_t *r;
1037 win->ewmh_flags = 0;
1039 win->ewmh_flags |= EWMH_F_ABOVE;
1041 win->ewmh_flags |= SWM_F_MANUAL;
1043 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1044 XCB_ATOM_ATOM, 0, (~0L));
1045 r = xcb_get_property_reply(conn, c, NULL);
1049 states = xcb_get_property_value(r);
1050 n = xcb_get_property_value_length(r);
1052 for (i = 0; i < n; i++)
1053 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1061 geteventname(xcb_generic_event_t *e)
1065 switch (XCB_EVENT_RESPONSE_TYPE(e)) {
1069 case XCB_KEY_RELEASE:
1070 name = "KeyRelease";
1072 case XCB_BUTTON_PRESS:
1073 name = "ButtonPress";
1075 case XCB_BUTTON_RELEASE:
1076 name = "ButtonRelease";
1078 case XCB_MOTION_NOTIFY:
1079 name = "MotionNotify";
1081 case XCB_ENTER_NOTIFY:
1082 name = "EnterNotify";
1084 case XCB_LEAVE_NOTIFY:
1085 name = "LeaveNotify";
1093 case XCB_KEYMAP_NOTIFY:
1094 name = "KeymapNotify";
1099 case XCB_GRAPHICS_EXPOSURE:
1100 name = "GraphicsExposure";
1102 case XCB_NO_EXPOSURE:
1103 name = "NoExposure";
1105 case XCB_VISIBILITY_NOTIFY:
1106 name = "VisibilityNotify";
1108 case XCB_CREATE_NOTIFY:
1109 name = "CreateNotify";
1111 case XCB_DESTROY_NOTIFY:
1112 name = "DestroyNotify";
1114 case XCB_UNMAP_NOTIFY:
1115 name = "UnmapNotify";
1117 case XCB_MAP_NOTIFY:
1120 case XCB_MAP_REQUEST:
1121 name = "MapRequest";
1123 case XCB_REPARENT_NOTIFY:
1124 name = "ReparentNotify";
1126 case XCB_CONFIGURE_NOTIFY:
1127 name = "ConfigureNotify";
1129 case XCB_CONFIGURE_REQUEST:
1130 name = "ConfigureRequest";
1132 case XCB_GRAVITY_NOTIFY:
1133 name = "GravityNotify";
1135 case XCB_RESIZE_REQUEST:
1136 name = "ResizeRequest";
1138 case XCB_CIRCULATE_NOTIFY:
1139 name = "CirculateNotify";
1141 case XCB_CIRCULATE_REQUEST:
1142 name = "CirculateRequest";
1144 case XCB_PROPERTY_NOTIFY:
1145 name = "PropertyNotify";
1147 case XCB_SELECTION_CLEAR:
1148 name = "SelectionClear";
1150 case XCB_SELECTION_REQUEST:
1151 name = "SelectionRequest";
1153 case XCB_SELECTION_NOTIFY:
1154 name = "SelectionNotify";
1156 case XCB_COLORMAP_NOTIFY:
1157 name = "ColormapNotify";
1159 case XCB_CLIENT_MESSAGE:
1160 name = "ClientMessage";
1162 case XCB_MAPPING_NOTIFY:
1163 name = "MappingNotify";
1174 dumpwins(struct swm_region *r, union arg *args)
1178 xcb_get_window_attributes_cookie_t c;
1179 xcb_get_window_attributes_reply_t *wa;
1181 if (r->ws == NULL) {
1182 warnx("dumpwins: invalid workspace");
1186 warnx("=== managed window list ws %02d ===", r->ws->idx);
1187 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1188 state = getstate(win->id);
1189 c = xcb_get_window_attributes(conn, win->id);
1190 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1192 warnx("window: 0x%x, map_state: %d, state: %u, "
1193 "transient: 0x%x", win->id, wa->map_state,
1194 state, win->transient);
1197 warnx("window: 0x%x, failed xcb_get_window_attributes",
1201 warnx("===== unmanaged window list =====");
1202 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1203 state = getstate(win->id);
1204 c = xcb_get_window_attributes(conn, win->id);
1205 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1207 warnx("window: 0x%x, map_state: %d, state: %u, "
1208 "transient: 0x%x", win->id, wa->map_state,
1209 state, win->transient);
1212 warnx("window: 0x%x, failed xcb_get_window_attributes",
1216 warnx("=================================");
1220 dumpwins(struct swm_region *r, union arg *args)
1223 #endif /* SWM_DEBUG */
1225 void event_error(xcb_generic_error_t *);
1226 void expose(xcb_expose_event_t *);
1227 void keypress(xcb_key_press_event_t *);
1228 void buttonpress(xcb_button_press_event_t *);
1229 void configurerequest(xcb_configure_request_event_t *);
1230 void configurenotify(xcb_configure_notify_event_t *);
1231 void destroynotify(xcb_destroy_notify_event_t *);
1232 void enternotify(xcb_enter_notify_event_t *);
1233 void mapnotify(xcb_map_notify_event_t *);
1234 void mappingnotify(xcb_mapping_notify_event_t *);
1235 void maprequest(xcb_map_request_event_t *);
1236 void propertynotify(xcb_property_notify_event_t *);
1237 void unmapnotify(xcb_unmap_notify_event_t *);
1238 void visibilitynotify(xcb_visibility_notify_event_t *);
1239 void clientmessage(xcb_client_message_event_t *);
1240 void screenchange(xcb_randr_screen_change_notify_event_t *);
1245 int saved_errno, status;
1248 saved_errno = errno;
1252 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1257 if (errno != ECHILD)
1258 warn("sighdlr: waitpid");
1259 #endif /* SWM_DEBUG */
1262 if (pid == searchpid)
1266 if (WIFEXITED(status)) {
1267 if (WEXITSTATUS(status) != 0)
1268 warnx("sighdlr: child exit status: %d",
1269 WEXITSTATUS(status));
1271 warnx("sighdlr: child is terminated "
1273 #endif /* SWM_DEBUG */
1287 errno = saved_errno;
1293 struct pid_e *p = NULL;
1295 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1300 TAILQ_FOREACH(p, &pidlist, entry) {
1309 name_to_color(const char *colorname)
1311 uint32_t result = 0;
1312 char cname[32] = "#";
1313 xcb_screen_t *screen;
1314 xcb_colormap_t cmap;
1315 xcb_alloc_color_reply_t *cr;
1316 xcb_alloc_named_color_reply_t *nr;
1317 uint16_t rr, gg, bb;
1319 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1320 cmap = screen->default_colormap;
1322 /* color is in format rgb://rr/gg/bb */
1323 if (strncmp(colorname, "rgb:", 4) == 0) {
1324 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1325 warnx("could not parse rgb %s", colorname);
1327 cr = xcb_alloc_color_reply(conn,
1328 xcb_alloc_color(conn, cmap, rr, gg, bb),
1334 warnx("color '%s' not found", colorname);
1337 nr = xcb_alloc_named_color_reply(conn,
1338 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1341 strlcat(cname, colorname + 2, sizeof cname - 1);
1342 nr = xcb_alloc_named_color_reply(conn,
1343 xcb_alloc_named_color(conn, cmap, strlen(cname),
1350 warnx("color '%s' not found", colorname);
1357 setscreencolor(char *val, int i, int c)
1361 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1362 if (i > 0 && i <= num_screens) {
1363 screens[i - 1].c[c].color = name_to_color(val);
1364 free(screens[i - 1].c[c].name);
1365 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1367 } else if (i == -1) {
1368 for (i = 0; i < num_screens; i++) {
1369 screens[i].c[c].color = name_to_color(val);
1370 free(screens[i].c[c].name);
1371 if ((screens[i].c[c].name = strdup(val)) == NULL)
1375 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1380 fancy_stacker(struct workspace *ws)
1382 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1383 if (ws->cur_layout->l_stack == vertical_stack)
1384 snprintf(ws->stacker, sizeof ws->stacker,
1385 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1386 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1387 if (ws->cur_layout->l_stack == horizontal_stack)
1388 snprintf(ws->stacker, sizeof ws->stacker,
1389 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1390 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1394 plain_stacker(struct workspace *ws)
1396 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1397 if (ws->cur_layout->l_stack == vertical_stack)
1398 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1399 sizeof ws->stacker);
1400 if (ws->cur_layout->l_stack == horizontal_stack)
1401 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1402 sizeof ws->stacker);
1406 custom_region(char *val)
1408 unsigned int sidx, x, y, w, h;
1410 xcb_screen_t *screen;
1412 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1413 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1414 errx(1, "invalid custom region, "
1415 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1416 if (sidx < 1 || sidx > num_screens)
1417 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1421 screen = get_screen(sidx);
1423 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1425 if (x > screen->width_in_pixels ||
1426 y > screen->height_in_pixels ||
1427 w + x > screen->width_in_pixels ||
1428 h + y > screen->height_in_pixels) {
1429 warnx("ignoring region %ux%u+%u+%u - not within screen "
1430 "boundaries (%ux%u)", w, h, x, y,
1431 screen->width_in_pixels, screen->height_in_pixels);
1435 new_region(&screens[sidx], x, y, w, h);
1439 socket_setnonblock(int fd)
1443 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1444 err(1, "fcntl F_GETFL");
1445 flags |= O_NONBLOCK;
1446 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1447 err(1, "fcntl F_SETFL");
1451 bar_print(struct swm_region *r, const char *s)
1455 xcb_rectangle_t rect;
1457 XRectangle ibox, lbox;
1460 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1462 switch (bar_justify) {
1463 case SWM_BAR_JUSTIFY_LEFT:
1466 case SWM_BAR_JUSTIFY_CENTER:
1467 x = (WIDTH(r) - lbox.width) / 2;
1469 case SWM_BAR_JUSTIFY_RIGHT:
1470 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1474 if (x < SWM_BAR_OFFSET)
1479 rect.width = WIDTH(r->bar);
1480 rect.height = HEIGHT(r->bar);
1482 /* clear back buffer */
1483 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1484 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1485 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1486 sizeof(rect), &rect);
1488 /* draw back buffer */
1489 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1490 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1491 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1492 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1493 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1494 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1498 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1499 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1503 bar_extra_stop(void)
1507 bzero(bar_pipe, sizeof bar_pipe);
1510 kill(bar_pid, SIGTERM);
1513 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1518 bar_class_name(char *s, size_t sz, struct swm_region *r)
1520 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1522 if (r->ws->focus->ch.class_name != NULL)
1523 strlcat(s, r->ws->focus->ch.class_name, sz);
1527 bar_title_name(char *s, size_t sz, struct swm_region *r)
1529 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1531 if (r->ws->focus->ch.instance_name != NULL)
1532 strlcat(s, r->ws->focus->ch.instance_name, sz);
1536 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1538 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1541 bar_class_name(s, sz, r);
1542 strlcat(s, ":", sz);
1543 bar_title_name(s, sz, r);
1547 bar_window_float(char *s, size_t sz, struct swm_region *r)
1549 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1551 if (r->ws->focus->floating)
1552 strlcat(s, "(f)", sz);
1556 bar_window_name(char *s, size_t sz, struct swm_region *r)
1560 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1562 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1565 strlcat(s, title, sz);
1569 int urgent[SWM_WS_MAX];
1571 bar_urgent(char *s, size_t sz)
1574 int i, j, num_screens;
1576 xcb_get_property_cookie_t c;
1577 xcb_icccm_wm_hints_t hints;
1579 for (i = 0; i < workspace_limit; i++)
1582 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1583 for (i = 0; i < num_screens; i++)
1584 for (j = 0; j < workspace_limit; j++)
1585 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1586 c = xcb_icccm_get_wm_hints(conn, win->id);
1587 if (xcb_icccm_get_wm_hints_reply(conn, c,
1590 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1594 for (i = 0; i < workspace_limit; i++) {
1596 snprintf(b, sizeof b, "%d ", i + 1);
1598 snprintf(b, sizeof b, "- ");
1604 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1606 if (r == NULL || r->ws == NULL)
1608 if (r->ws->name != NULL)
1609 strlcat(s, r->ws->name, sz);
1612 /* build the default bar format according to the defined enabled options */
1614 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1616 /* if format provided, just copy the buffers */
1617 if (bar_format != NULL) {
1618 strlcpy(fmtnew, fmtexp, sz);
1622 /* reset the output buffer */
1625 strlcat(fmtnew, "+N:+I ", sz);
1627 strlcat(fmtnew, "+S", sz);
1628 strlcat(fmtnew, " ", sz);
1630 /* only show the workspace name if there's actually one */
1631 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1632 strlcat(fmtnew, "<+D>", sz);
1633 strlcat(fmtnew, "+3<", sz);
1635 if (clock_enabled) {
1636 strlcat(fmtnew, fmtexp, sz);
1637 strlcat(fmtnew, "+4<", sz);
1640 /* bar_urgent already adds the space before the last asterisk */
1642 strlcat(fmtnew, "* +U*+4<", sz);
1644 if (title_class_enabled) {
1645 strlcat(fmtnew, "+C", sz);
1646 if (title_name_enabled == 0)
1647 strlcat(fmtnew, "+4<", sz);
1650 /* checks needed by the colon and floating strlcat(3) calls below */
1651 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1652 if (title_name_enabled) {
1653 if (title_class_enabled)
1654 strlcat(fmtnew, ":", sz);
1655 strlcat(fmtnew, "+T+4<", sz);
1657 if (window_name_enabled) {
1658 if (r->ws->focus->floating)
1659 strlcat(fmtnew, "+F ", sz);
1660 strlcat(fmtnew, "+64W ", sz);
1664 /* finally add the action script output and the version */
1665 strlcat(fmtnew, "+4<+A+4<+V", sz);
1669 bar_replace_pad(char *tmp, int *limit, size_t sz)
1671 /* special case; no limit given, pad one space, instead */
1672 if (*limit == sz - 1)
1674 snprintf(tmp, sz, "%*s", *limit, " ");
1677 /* replaces the bar format character sequences (like in tmux(1)) */
1679 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1683 char tmp[SWM_BAR_MAX];
1687 /* reset strlcat(3) buffer */
1690 /* get number, if any */
1693 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1694 limit = sizeof tmp - 1;
1695 if (limit <= 0 || limit >= sizeof tmp)
1696 limit = sizeof tmp - 1;
1698 /* there is nothing to replace (ie EOL) */
1705 bar_replace_pad(tmp, &limit, sizeof tmp);
1708 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1711 bar_class_name(tmp, sizeof tmp, r);
1714 bar_workspace_name(tmp, sizeof tmp, r);
1717 bar_window_float(tmp, sizeof tmp, r);
1720 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1723 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1726 bar_class_title_name(tmp, sizeof tmp, r);
1729 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1732 bar_title_name(tmp, sizeof tmp, r);
1735 bar_urgent(tmp, sizeof tmp);
1738 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1741 bar_window_name(tmp, sizeof tmp, r);
1744 /* unknown character sequence; copy as-is */
1745 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1753 while (limit-- > 0) {
1754 if (*offrep >= sz - 1)
1756 fmtrep[(*offrep)++] = *ptr++;
1764 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1769 while (*fmt != '\0') {
1771 /* skip ordinary characters */
1774 fmtrep[off++] = *fmt++;
1778 /* character sequence found; replace it */
1779 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1788 bar_fmt_expand(char *fmtexp, size_t sz)
1795 /* start by grabbing the current time and date */
1797 localtime_r(&tmt, &tm);
1799 /* figure out what to expand */
1800 if (bar_format != NULL)
1802 else if (bar_format == NULL && clock_enabled)
1804 /* if nothing to expand bail out */
1810 /* copy as-is, just in case the format shouldn't be expanded below */
1811 strlcpy(fmtexp, fmt, sz);
1812 /* finally pass the string through strftime(3) */
1813 #ifndef SWM_DENY_CLOCK_FORMAT
1814 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1815 warnx("format too long");
1823 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1824 char fmtrep[SWM_BAR_MAX];
1826 struct swm_region *r;
1828 /* expand the format by first passing it through strftime(3) */
1829 bar_fmt_expand(fmtexp, sizeof fmtexp);
1831 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1832 for (i = 0; i < num_screens; i++) {
1833 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1836 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1837 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1838 bar_print(r, fmtrep);
1849 if (bar_enabled == 0)
1851 if (bar_extra && bar_extra_running) {
1852 /* ignore short reads; it'll correct itself */
1853 while ((b = fgetln(stdin, &len)) != NULL)
1854 if (b && b[len - 1] == '\n') {
1856 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1858 if (b == NULL && errno != EAGAIN) {
1859 warn("bar_update: bar_extra failed");
1863 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1876 bar_toggle(struct swm_region *r, union arg *args)
1878 struct swm_region *tmpr;
1881 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1883 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1885 for (i = 0; i < num_screens; i++)
1886 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1888 xcb_unmap_window(conn, tmpr->bar->id);
1890 for (i = 0; i < num_screens; i++)
1891 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1893 map_window_raised(tmpr->bar->id);
1896 bar_enabled = !bar_enabled;
1899 /* must be after stack */
1906 struct swm_region *r;
1910 /* do this here because the conf file is in memory */
1911 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1912 /* launch external status app */
1913 bar_extra_running = 1;
1914 if (pipe(bar_pipe) == -1)
1915 err(1, "pipe error");
1916 socket_setnonblock(bar_pipe[0]);
1917 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1918 if (dup2(bar_pipe[0], 0) == -1)
1920 if (dup2(bar_pipe[1], 1) == -1)
1922 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1923 err(1, "could not disable SIGPIPE");
1924 switch (bar_pid = fork()) {
1926 err(1, "cannot fork");
1930 execvp(bar_argv[0], bar_argv);
1931 err(1, "%s external app failed", bar_argv[0]);
1933 default: /* parent */
1939 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1940 for (i = 0; i < num_screens; i++)
1941 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1944 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1945 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1946 xcb_change_window_attributes(conn, r->bar->id,
1947 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1953 bar_setup(struct swm_region *r)
1955 char *default_string;
1956 char **missing_charsets;
1957 int num_missing_charsets = 0;
1959 xcb_screen_t *screen = get_screen(r->s->idx);
1963 XFreeFontSet(display, bar_fs);
1967 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1968 err(1, "bar_setup: calloc: failed to allocate memory.");
1970 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1972 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1973 &num_missing_charsets, &default_string);
1975 if (num_missing_charsets > 0) {
1976 warnx("Unable to load charset(s):");
1978 for (i = 0; i < num_missing_charsets; ++i)
1979 warnx("%s", missing_charsets[i]);
1981 XFreeStringList(missing_charsets);
1983 if (strcmp(default_string, ""))
1984 warnx("Glyphs from those sets will be replaced "
1985 "by '%s'.", default_string);
1987 warnx("Glyphs from those sets won't be drawn.");
1991 errx(1, "Error creating font set structure.");
1993 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1995 bar_height = bar_fs_extents->max_logical_extent.height +
1996 2 * bar_border_width;
2002 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2003 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2004 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2006 r->bar->id = xcb_generate_id(conn);
2007 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
2008 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
2009 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2010 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2011 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2012 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
2014 r->bar->buffer = xcb_generate_id(conn);
2015 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2016 WIDTH(r->bar), HEIGHT(r->bar));
2018 xcb_randr_select_input(conn, r->bar->id,
2019 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2022 map_window_raised(r->bar->id);
2024 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2025 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2028 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2029 err(1, "could not install bar_signal");
2034 bar_cleanup(struct swm_region *r)
2038 xcb_destroy_window(conn, r->bar->id);
2039 xcb_free_pixmap(conn, r->bar->buffer);
2045 set_win_state(struct ws_win *win, uint16_t state)
2047 uint16_t data[2] = { state, XCB_ATOM_NONE };
2049 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2054 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
2055 astate, 32, 2, data);
2059 getstate(xcb_window_t w)
2061 uint16_t result = 0;
2062 xcb_get_property_cookie_t c;
2063 xcb_get_property_reply_t *r;
2065 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
2066 r = xcb_get_property_reply(conn, c, NULL);
2069 result = *((uint16_t *)xcb_get_property_value(r));
2073 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2079 version(struct swm_region *r, union arg *args)
2081 bar_version = !bar_version;
2083 snprintf(bar_vertext, sizeof bar_vertext,
2084 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2086 strlcpy(bar_vertext, "", sizeof bar_vertext);
2091 client_msg(struct ws_win *win, xcb_atom_t a)
2093 xcb_client_message_event_t ev;
2098 bzero(&ev, sizeof ev);
2099 ev.response_type = XCB_CLIENT_MESSAGE;
2100 ev.window = win->id;
2103 ev.data.data32[0] = a;
2104 ev.data.data32[1] = XCB_CURRENT_TIME;
2106 xcb_send_event(conn, False, win->id,
2107 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2110 /* synthetic response to a ConfigureRequest when not making a change */
2112 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2114 xcb_configure_notify_event_t ce;
2119 /* send notification of unchanged state. */
2120 bzero(&ce, sizeof(ce));
2121 ce.response_type = XCB_CONFIGURE_NOTIFY;
2124 ce.width = WIDTH(win);
2125 ce.height = HEIGHT(win);
2126 ce.override_redirect = False;
2131 ce.window = win->id;
2132 ce.border_width = BORDER(win);
2133 ce.above_sibling = XCB_WINDOW_NONE;
2136 ce.event = ev->window;
2137 ce.window = ev->window;
2139 /* make response appear more WM_SIZE_HINTS-compliant */
2141 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2142 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2143 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2144 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2145 SH_INC_W(win), SH_INC_H(win));
2149 /* the hint may be set... to 0! */
2150 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2151 ce.width = SH_MIN_W(win);
2152 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2153 ce.height = SH_MIN_H(win);
2158 /* may also be advertized as 0 */
2159 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2160 ce.width = SH_MAX_W(win);
2161 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2162 ce.height = SH_MAX_H(win);
2165 /* resize increment. */
2167 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2168 ce.width -= (ce.width - SH_MIN_W(win)) %
2170 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2171 ce.height -= (ce.height - SH_MIN_H(win)) %
2175 /* adjust x and y for requested border_width. */
2176 ce.x += BORDER(win) - ev->border_width;
2177 ce.y += BORDER(win) - ev->border_width;
2178 ce.border_width = ev->border_width;
2179 ce.above_sibling = ev->sibling;
2182 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2183 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2184 ce.y, ce.width, ce.height, ce.border_width);
2186 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2191 count_win(struct workspace *ws, int count_transient)
2196 TAILQ_FOREACH(win, &ws->winlist, entry) {
2197 if (count_transient == 0 && win->floating)
2199 if (count_transient == 0 && win->transient)
2205 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2211 quit(struct swm_region *r, union arg *args)
2213 DNPRINTF(SWM_D_MISC, "quit\n");
2218 unmap_window(struct ws_win *win)
2223 /* don't unmap again */
2224 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2227 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2229 xcb_unmap_window(conn, win->id);
2230 xcb_change_window_attributes(conn, win->id,
2231 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2238 int i, j, num_screens;
2240 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2241 for (i = 0; i < num_screens; i++)
2242 for (j = 0; j < workspace_limit; j++)
2243 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2248 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2250 xcb_key_press_event_t event;
2251 xcb_keycode_t *keycode;
2256 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2258 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2261 bzero(&event, sizeof(event));
2262 event.event = win->id;
2263 event.root = win->s->root;
2264 event.child = XCB_WINDOW_NONE;
2265 event.time = XCB_CURRENT_TIME;
2266 event.event_x = X(win);
2267 event.event_y = Y(win);
2270 event.same_screen = True;
2271 event.detail = *keycode;
2272 event.state = modifiers;
2274 event.response_type = XCB_KEY_PRESS;
2275 xcb_send_event(conn, True, win->id,
2276 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2278 event.response_type = XCB_KEY_RELEASE;
2279 xcb_send_event(conn, True, win->id,
2280 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2284 restart(struct swm_region *r, union arg *args)
2286 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2288 /* disable alarm because the following code may not be interrupted */
2290 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2291 err(1, "can't disable alarm");
2297 xcb_key_symbols_free(syms);
2299 xcb_disconnect(conn);
2301 execvp(start_argv[0], start_argv);
2302 warn("execvp failed");
2307 root_to_region(xcb_window_t root)
2309 struct swm_region *r = NULL;
2311 xcb_query_pointer_reply_t *qpr;
2313 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2315 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2316 for (i = 0; i < num_screens; i++)
2317 if (screens[i].root == root)
2320 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2321 screens[i].root), NULL);
2324 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2325 qpr->root_x, qpr->root_y);
2326 /* choose a region based on pointer location */
2327 TAILQ_FOREACH(r, &screens[i].rl, entry)
2328 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2329 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2335 r = TAILQ_FIRST(&screens[i].rl);
2341 find_unmanaged_window(xcb_window_t id)
2344 int i, j, num_screens;
2346 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2347 for (i = 0; i < num_screens; i++)
2348 for (j = 0; j < workspace_limit; j++)
2349 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2357 find_window(xcb_window_t id)
2360 int i, j, num_screens;
2361 xcb_query_tree_cookie_t c;
2362 xcb_query_tree_reply_t *r;
2364 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2365 for (i = 0; i < num_screens; i++)
2366 for (j = 0; j < workspace_limit; j++)
2367 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2371 c = xcb_query_tree(conn, id);
2372 r = xcb_query_tree_reply(conn, c, NULL);
2376 /* if we were looking for the parent return that window instead */
2377 if (r->parent == 0 || r->root == r->parent)
2380 /* look for parent */
2381 for (i = 0; i < num_screens; i++)
2382 for (j = 0; j < workspace_limit; j++)
2383 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2384 if (r->parent == win->id) {
2394 spawn(int ws_idx, union arg *args, int close_fd)
2399 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2402 close(xcb_get_file_descriptor(conn));
2404 setenv("LD_PRELOAD", SWM_LIB, 1);
2406 if (asprintf(&ret, "%d", ws_idx) == -1) {
2407 warn("spawn: asprintf SWM_WS");
2410 setenv("_SWM_WS", ret, 1);
2414 if (asprintf(&ret, "%d", getpid()) == -1) {
2415 warn("spawn: asprintf _SWM_PID");
2418 setenv("_SWM_PID", ret, 1);
2422 if (setsid() == -1) {
2423 warn("spawn: setsid");
2429 * close stdin and stdout to prevent interaction between apps
2430 * and the baraction script
2431 * leave stderr open to record errors
2433 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2434 warn("spawn: open");
2437 dup2(fd, STDIN_FILENO);
2438 dup2(fd, STDOUT_FILENO);
2443 execvp(args->argv[0], args->argv);
2445 warn("spawn: execvp");
2450 kill_refs(struct ws_win *win)
2452 int i, x, num_screens;
2453 struct swm_region *r;
2454 struct workspace *ws;
2459 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2460 for (i = 0; i < num_screens; i++)
2461 TAILQ_FOREACH(r, &screens[i].rl, entry)
2462 for (x = 0; x < workspace_limit; x++) {
2464 if (win == ws->focus)
2466 if (win == ws->focus_prev)
2467 ws->focus_prev = NULL;
2472 validate_win(struct ws_win *testwin)
2475 struct workspace *ws;
2476 struct swm_region *r;
2477 int i, x, num_screens;
2479 if (testwin == NULL)
2482 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2483 for (i = 0; i < num_screens; i++)
2484 TAILQ_FOREACH(r, &screens[i].rl, entry)
2485 for (x = 0; x < workspace_limit; x++) {
2487 TAILQ_FOREACH(win, &ws->winlist, entry)
2495 validate_ws(struct workspace *testws)
2497 struct swm_region *r;
2498 struct workspace *ws;
2499 int i, x, num_screens;
2501 /* validate all ws */
2502 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2503 for (i = 0; i < num_screens; i++)
2504 TAILQ_FOREACH(r, &screens[i].rl, entry)
2505 for (x = 0; x < workspace_limit; x++) {
2514 unfocus_win(struct ws_win *win)
2516 xcb_window_t none = XCB_WINDOW_NONE;
2518 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2522 if (win->ws == NULL)
2525 if (validate_ws(win->ws))
2526 return; /* XXX this gets hit with thunderbird, needs fixing */
2528 if (win->ws->r == NULL)
2531 if (validate_win(win)) {
2536 if (win->ws->focus == win) {
2537 win->ws->focus = NULL;
2538 win->ws->focus_prev = win;
2541 if (validate_win(win->ws->focus)) {
2542 kill_refs(win->ws->focus);
2543 win->ws->focus = NULL;
2545 if (validate_win(win->ws->focus_prev)) {
2546 kill_refs(win->ws->focus_prev);
2547 win->ws->focus_prev = NULL;
2550 grabbuttons(win, 0);
2551 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2552 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2553 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2554 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2561 int i, j, num_screens;
2563 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2565 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2566 for (i = 0; i < num_screens; i++)
2567 for (j = 0; j < workspace_limit; j++)
2568 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2573 focus_win(struct ws_win *win)
2575 struct ws_win *cfw = NULL;
2576 xcb_get_input_focus_cookie_t c;
2577 xcb_get_input_focus_reply_t *r;
2578 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2580 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2584 if (win->ws == NULL)
2587 if (validate_ws(win->ws))
2588 return; /* XXX this gets hit with thunderbird, needs fixing */
2590 if (validate_win(win)) {
2595 if (validate_win(win)) {
2600 c = xcb_get_input_focus(conn);
2601 r = xcb_get_input_focus_reply(conn, c, NULL);
2603 cur_focus = r->focus;
2606 if ((cfw = find_window(cur_focus)) != NULL)
2609 /* use larger hammer since the window was killed somehow */
2610 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2611 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2612 xcb_change_window_attributes(conn, cfw->id,
2613 XCB_CW_BORDER_PIXEL,
2614 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2617 win->ws->focus = win;
2619 if (win->ws->r != NULL) {
2621 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2622 win->id, XCB_CURRENT_TIME);
2623 grabbuttons(win, 1);
2624 xcb_change_window_attributes(conn, win->id,
2625 XCB_CW_BORDER_PIXEL,
2626 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2627 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2628 win->ws->always_raise)
2629 map_window_raised(win->id);
2631 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2632 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2640 switchws(struct swm_region *r, union arg *args)
2642 int wsid = args->id, unmap_old = 0;
2643 struct swm_region *this_r, *other_r;
2645 struct workspace *new_ws, *old_ws;
2651 if (wsid >= workspace_limit)
2655 old_ws = this_r->ws;
2656 new_ws = &this_r->s->ws[wsid];
2658 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2659 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2661 if (new_ws == NULL || old_ws == NULL)
2663 if (new_ws == old_ws)
2666 other_r = new_ws->r;
2667 if (other_r == NULL) {
2668 /* the other workspace is hidden, hide this one */
2672 /* the other ws is visible in another region, exchange them */
2673 other_r->ws_prior = new_ws;
2674 other_r->ws = old_ws;
2675 old_ws->r = other_r;
2677 this_r->ws_prior = old_ws;
2678 this_r->ws = new_ws;
2681 /* this is needed so that we can click on a window after a restart */
2685 a.id = SWM_ARG_ID_FOCUSCUR;
2686 focus(new_ws->r, &a);
2688 /* unmap old windows */
2690 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2695 cyclews(struct swm_region *r, union arg *args)
2698 struct swm_screen *s = r->s;
2701 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2702 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2707 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2710 case SWM_ARG_ID_CYCLEWS_UP:
2711 if (a.id < workspace_limit - 1)
2716 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2719 case SWM_ARG_ID_CYCLEWS_DOWN:
2723 a.id = workspace_limit - 1;
2730 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2732 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2736 } while (a.id != r->ws->idx);
2740 priorws(struct swm_region *r, union arg *args)
2744 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2745 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2747 if (r->ws_prior == NULL)
2750 a.id = r->ws_prior->idx;
2755 cyclescr(struct swm_region *r, union arg *args)
2757 struct swm_region *rr = NULL;
2759 int i, x, y, num_screens;
2761 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2762 /* do nothing if we don't have more than one screen */
2763 if (!(num_screens > 1 || outputs > 1))
2768 case SWM_ARG_ID_CYCLESC_UP:
2769 rr = TAILQ_NEXT(r, entry);
2771 rr = TAILQ_FIRST(&screens[i].rl);
2773 case SWM_ARG_ID_CYCLESC_DOWN:
2774 rr = TAILQ_PREV(r, swm_region_list, entry);
2776 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2784 /* move mouse to region */
2786 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2787 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2790 a.id = SWM_ARG_ID_FOCUSCUR;
2793 if (rr->ws->focus) {
2794 /* move to focus window */
2795 x = X(rr->ws->focus) + 1;
2796 y = Y(rr->ws->focus) + 1;
2797 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2803 sort_windows(struct ws_win_list *wl)
2805 struct ws_win *win, *parent, *nxt;
2810 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2811 nxt = TAILQ_NEXT(win, entry);
2812 if (win->transient) {
2813 parent = find_window(win->transient);
2814 if (parent == NULL) {
2815 warnx("not possible bug");
2818 TAILQ_REMOVE(wl, win, entry);
2819 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2826 swapwin(struct swm_region *r, union arg *args)
2828 struct ws_win *target, *source;
2829 struct ws_win *cur_focus;
2830 struct ws_win_list *wl;
2833 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2834 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2836 cur_focus = r->ws->focus;
2837 if (cur_focus == NULL)
2841 wl = &source->ws->winlist;
2844 case SWM_ARG_ID_SWAPPREV:
2845 if (source->transient)
2846 source = find_window(source->transient);
2847 target = TAILQ_PREV(source, ws_win_list, entry);
2848 if (target && target->transient)
2849 target = find_window(target->transient);
2850 TAILQ_REMOVE(wl, source, entry);
2852 TAILQ_INSERT_TAIL(wl, source, entry);
2854 TAILQ_INSERT_BEFORE(target, source, entry);
2856 case SWM_ARG_ID_SWAPNEXT:
2857 target = TAILQ_NEXT(source, entry);
2858 /* move the parent and let the sort handle the move */
2859 if (source->transient)
2860 source = find_window(source->transient);
2861 TAILQ_REMOVE(wl, source, entry);
2863 TAILQ_INSERT_HEAD(wl, source, entry);
2865 TAILQ_INSERT_AFTER(wl, target, source, entry);
2867 case SWM_ARG_ID_SWAPMAIN:
2868 target = TAILQ_FIRST(wl);
2869 if (target == source) {
2870 if (source->ws->focus_prev != NULL &&
2871 source->ws->focus_prev != target)
2872 source = source->ws->focus_prev;
2876 if (target == NULL || source == NULL)
2878 source->ws->focus_prev = target;
2879 TAILQ_REMOVE(wl, target, entry);
2880 TAILQ_INSERT_BEFORE(source, target, entry);
2881 TAILQ_REMOVE(wl, source, entry);
2882 TAILQ_INSERT_HEAD(wl, source, entry);
2884 case SWM_ARG_ID_MOVELAST:
2885 TAILQ_REMOVE(wl, source, entry);
2886 TAILQ_INSERT_TAIL(wl, source, entry);
2889 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2899 focus_prev(struct ws_win *win)
2901 struct ws_win *winfocus = NULL;
2902 struct ws_win *cur_focus = NULL;
2903 struct ws_win_list *wl = NULL;
2904 struct workspace *ws = NULL;
2906 if (!(win && win->ws))
2911 cur_focus = ws->focus;
2913 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2914 WINID(win), WINID(cur_focus));
2916 /* pickle, just focus on whatever */
2917 if (cur_focus == NULL) {
2918 /* use prev_focus if valid */
2919 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2920 find_window(WINID(ws->focus_prev)))
2921 winfocus = ws->focus_prev;
2925 /* if transient focus on parent */
2926 if (cur_focus->transient) {
2927 winfocus = find_window(cur_focus->transient);
2931 /* if in max_stack try harder */
2932 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2933 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2934 if (cur_focus != ws->focus_prev)
2935 winfocus = ws->focus_prev;
2937 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2942 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2944 if (winfocus == NULL || winfocus == win) {
2945 switch (focus_close) {
2946 case SWM_STACK_BOTTOM:
2947 winfocus = TAILQ_FIRST(wl);
2950 winfocus = TAILQ_LAST(wl, ws_win_list);
2952 case SWM_STACK_ABOVE:
2953 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2954 if (focus_close_wrap)
2955 winfocus = TAILQ_FIRST(wl);
2957 winfocus = TAILQ_PREV(cur_focus,
2958 ws_win_list, entry);
2961 case SWM_STACK_BELOW:
2962 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2964 if (focus_close_wrap)
2965 winfocus = TAILQ_LAST(wl, ws_win_list);
2967 winfocus = TAILQ_NEXT(cur_focus, entry);
2973 if (winfocus == NULL) {
2974 if (focus_default == SWM_STACK_TOP)
2975 winfocus = TAILQ_LAST(wl, ws_win_list);
2977 winfocus = TAILQ_FIRST(wl);
2981 focus_magic(winfocus);
2985 focus(struct swm_region *r, union arg *args)
2987 struct ws_win *winfocus = NULL, *head;
2988 struct ws_win *cur_focus = NULL;
2989 struct ws_win_list *wl = NULL;
2990 struct workspace *ws = NULL;
2996 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2998 /* treat FOCUS_CUR special */
2999 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3000 if (r->ws->focus && r->ws->focus->iconic == 0)
3001 winfocus = r->ws->focus;
3002 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3003 winfocus = r->ws->focus_prev;
3005 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3006 if (winfocus->iconic == 0)
3009 focus_magic(winfocus);
3013 if ((cur_focus = r->ws->focus) == NULL)
3017 if (TAILQ_EMPTY(wl))
3019 /* make sure there is at least one uniconified window */
3021 TAILQ_FOREACH(winfocus, wl, entry)
3022 if (winfocus->iconic == 0) {
3030 case SWM_ARG_ID_FOCUSPREV:
3031 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3033 head = TAILQ_LAST(wl, ws_win_list);
3035 if (WINID(winfocus) == cur_focus->transient) {
3036 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3038 head = TAILQ_LAST(wl, ws_win_list);
3043 if (winfocus && winfocus->iconic) {
3044 while (winfocus != cur_focus) {
3045 if (winfocus == NULL)
3046 winfocus = TAILQ_LAST(wl, ws_win_list);
3047 if (winfocus->iconic == 0)
3049 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3055 case SWM_ARG_ID_FOCUSNEXT:
3056 head = TAILQ_NEXT(cur_focus, entry);
3058 head = TAILQ_FIRST(wl);
3062 if (winfocus && winfocus->iconic) {
3063 while (winfocus != cur_focus) {
3064 if (winfocus == NULL)
3065 winfocus = TAILQ_FIRST(wl);
3066 if (winfocus->iconic == 0)
3068 winfocus = TAILQ_NEXT(winfocus, entry);
3073 case SWM_ARG_ID_FOCUSMAIN:
3074 winfocus = TAILQ_FIRST(wl);
3075 if (winfocus == cur_focus)
3076 winfocus = cur_focus->ws->focus_prev;
3083 focus_magic(winfocus);
3087 cycle_layout(struct swm_region *r, union arg *args)
3089 struct workspace *ws = r->ws;
3092 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3095 if (ws->cur_layout->l_stack == NULL)
3096 ws->cur_layout = &layouts[0];
3100 a.id = SWM_ARG_ID_FOCUSCUR;
3106 stack_config(struct swm_region *r, union arg *args)
3108 struct workspace *ws = r->ws;
3110 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3113 if (ws->cur_layout->l_config != NULL)
3114 ws->cur_layout->l_config(ws, args->id);
3116 if (args->id != SWM_ARG_ID_STACKINIT)
3123 struct swm_geometry g;
3124 struct swm_region *r;
3130 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3132 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3133 for (i = 0; i < num_screens; i++) {
3137 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3138 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3139 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3141 /* start with screen geometry, adjust for bar */
3143 g.w -= 2 * border_width;
3144 g.h -= 2 * border_width;
3150 r->ws->cur_layout->l_stack(r->ws, &g);
3151 r->ws->cur_layout->l_string(r->ws);
3152 /* save r so we can track region changes */
3159 DNPRINTF(SWM_D_STACK, "stack: end\n");
3163 store_float_geom(struct ws_win *win, struct swm_region *r)
3165 /* retain window geom and region geom */
3166 win->g_float = win->g;
3167 win->g_float.x -= X(r);
3168 win->g_float.y -= Y(r);
3169 win->g_floatvalid = 1;
3170 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3171 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3172 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3173 win->g_float.w, win->g_float.h);
3177 stack_floater(struct ws_win *win, struct swm_region *r)
3182 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3185 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3186 * geom on ws switches or return from max mode
3188 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3189 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3190 /* refloat at last floating relative position */
3191 win->g = win->g_float;
3196 win->floatmaxed = 0;
3199 * if set to fullscreen mode, configure window to maximum size.
3201 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3202 if (!win->g_floatvalid)
3203 store_float_geom(win, win->ws->r);
3205 win->g = win->ws->r->g;
3209 * remove border on fullscreen floater when in fullscreen mode or when
3210 * the quirk is present.
3212 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3213 ((win->quirks & SWM_Q_FULLSCREEN) &&
3214 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3215 if (win->bordered) {
3217 X(win) += border_width;
3218 Y(win) += border_width;
3220 } else if (!win->bordered) {
3222 X(win) -= border_width;
3223 Y(win) -= border_width;
3226 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3227 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3228 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3233 * floaters and transients are auto-centred unless moved
3236 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3237 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3240 /* keep window within region bounds */
3241 constrain_window(win, r, 0);
3247 * Send keystrokes to terminal to decrease/increase the font size as the
3248 * window size changes.
3251 adjust_font(struct ws_win *win)
3253 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3254 win->floating || win->transient)
3257 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3258 WIDTH(win) / win->sh.width_inc < term_width &&
3259 win->font_steps < SWM_MAX_FONT_STEPS) {
3260 win->font_size_boundary[win->font_steps] =
3261 (win->sh.width_inc * term_width) + win->sh.base_width;
3264 win->last_inc = win->sh.width_inc;
3265 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3266 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3267 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3270 win->last_inc = win->sh.width_inc;
3271 fake_keypress(win, XK_KP_Add, ShiftMask);
3275 #define SWAPXY(g) do { \
3277 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3278 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3281 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3283 struct swm_geometry win_g, r_g = *g;
3284 struct ws_win *win, *fs_win = NULL;
3285 int i, j, s, stacks;
3286 int w_inc = 1, h_inc, w_base = 1, h_base;
3287 int hrh, extra = 0, h_slice, last_h = 0;
3288 int split, colno, winno, mwin, msize, mscale;
3289 int remain, missing, v_slice, reconfigure;
3291 xcb_get_window_attributes_reply_t *war;
3293 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3294 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3296 winno = count_win(ws, 0);
3297 if (winno == 0 && count_win(ws, 1) == 0)
3300 TAILQ_FOREACH(win, &ws->winlist, entry)
3301 if (win->transient == 0 && win->floating == 0
3302 && win->iconic == 0)
3309 w_inc = win->sh.width_inc;
3310 w_base = win->sh.base_width;
3311 mwin = ws->l_state.horizontal_mwin;
3312 mscale = ws->l_state.horizontal_msize;
3313 stacks = ws->l_state.horizontal_stacks;
3316 w_inc = win->sh.height_inc;
3317 w_base = win->sh.base_height;
3318 mwin = ws->l_state.vertical_mwin;
3319 mscale = ws->l_state.vertical_msize;
3320 stacks = ws->l_state.vertical_stacks;
3324 if (stacks > winno - mwin)
3325 stacks = winno - mwin;
3329 h_slice = r_g.h / SWM_H_SLICE;
3330 if (mwin && winno > mwin) {
3331 v_slice = r_g.w / SWM_V_SLICE;
3335 win_g.w = v_slice * mscale;
3337 if (w_inc > 1 && w_inc < v_slice) {
3338 /* adjust for window's requested size increment */
3339 remain = (win_g.w - w_base) % w_inc;
3345 win_g.x += r_g.w - msize;
3348 colno = split = winno / stacks;
3349 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3350 2 * border_width) / stacks);
3352 hrh = r_g.h / colno;
3353 extra = r_g.h - (colno * hrh);
3354 win_g.h = hrh - 2 * border_width;
3356 /* stack all the tiled windows */
3357 i = j = 0, s = stacks;
3358 TAILQ_FOREACH(win, &ws->winlist, entry) {
3359 if (win->transient != 0 || win->floating != 0)
3361 if (win->iconic != 0)
3364 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3369 if (split && i == split) {
3370 colno = (winno - mwin) / stacks;
3371 if (s <= (winno - mwin) % stacks)
3373 split = split + colno;
3374 hrh = (r_g.h / colno);
3375 extra = r_g.h - (colno * hrh);
3379 win_g.x += win_g.w + 2 * border_width;
3380 win_g.w = (r_g.w - msize -
3381 (stacks * 2 * border_width)) / stacks;
3383 win_g.w += (r_g.w - msize -
3384 (stacks * 2 * border_width)) % stacks;
3388 win_g.h = hrh - 2 * border_width;
3390 h_inc = win->sh.width_inc;
3391 h_base = win->sh.base_width;
3393 h_inc = win->sh.height_inc;
3394 h_base = win->sh.base_height;
3396 if (j == colno - 1) {
3397 win_g.h = hrh + extra;
3398 } else if (h_inc > 1 && h_inc < h_slice) {
3399 /* adjust for window's requested size increment */
3400 remain = (win_g.h - h_base) % h_inc;
3401 missing = h_inc - remain;
3403 if (missing <= extra || j == 0) {
3415 win_g.y += last_h + 2 * border_width;
3417 if (disable_border && bar_enabled == 0 && winno == 1){
3419 win_g.w += 2 * border_width;
3420 win_g.h += 2 * border_width;
3425 if (X(win) != win_g.y || Y(win) != win_g.x ||
3426 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3430 WIDTH(win) = win_g.h;
3431 HEIGHT(win) = win_g.w;
3434 if (X(win) != win_g.x || Y(win) != win_g.y ||
3435 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3439 WIDTH(win) = win_g.w;
3440 HEIGHT(win) = win_g.h;
3444 if (bordered != win->bordered) {
3446 win->bordered = bordered;
3454 war = xcb_get_window_attributes_reply(conn,
3455 xcb_get_window_attributes(conn, win->id),
3458 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3459 map_window_raised(win->id);
3469 /* now, stack all the floaters and transients */
3470 TAILQ_FOREACH(win, &ws->winlist, entry) {
3471 if (win->transient == 0 && win->floating == 0)
3473 if (win->iconic == 1)
3475 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3480 stack_floater(win, ws->r);
3481 map_window_raised(win->id);
3485 stack_floater(fs_win, ws->r);
3486 map_window_raised(fs_win->id);
3491 vertical_config(struct workspace *ws, int id)
3493 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3497 case SWM_ARG_ID_STACKRESET:
3498 case SWM_ARG_ID_STACKINIT:
3499 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3500 ws->l_state.vertical_mwin = 1;
3501 ws->l_state.vertical_stacks = 1;
3503 case SWM_ARG_ID_MASTERSHRINK:
3504 if (ws->l_state.vertical_msize > 1)
3505 ws->l_state.vertical_msize--;
3507 case SWM_ARG_ID_MASTERGROW:
3508 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3509 ws->l_state.vertical_msize++;
3511 case SWM_ARG_ID_MASTERADD:
3512 ws->l_state.vertical_mwin++;
3514 case SWM_ARG_ID_MASTERDEL:
3515 if (ws->l_state.vertical_mwin > 0)
3516 ws->l_state.vertical_mwin--;
3518 case SWM_ARG_ID_STACKINC:
3519 ws->l_state.vertical_stacks++;
3521 case SWM_ARG_ID_STACKDEC:
3522 if (ws->l_state.vertical_stacks > 1)
3523 ws->l_state.vertical_stacks--;
3525 case SWM_ARG_ID_FLIPLAYOUT:
3526 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3534 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3536 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3538 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3542 horizontal_config(struct workspace *ws, int id)
3544 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3547 case SWM_ARG_ID_STACKRESET:
3548 case SWM_ARG_ID_STACKINIT:
3549 ws->l_state.horizontal_mwin = 1;
3550 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3551 ws->l_state.horizontal_stacks = 1;
3553 case SWM_ARG_ID_MASTERSHRINK:
3554 if (ws->l_state.horizontal_msize > 1)
3555 ws->l_state.horizontal_msize--;
3557 case SWM_ARG_ID_MASTERGROW:
3558 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3559 ws->l_state.horizontal_msize++;
3561 case SWM_ARG_ID_MASTERADD:
3562 ws->l_state.horizontal_mwin++;
3564 case SWM_ARG_ID_MASTERDEL:
3565 if (ws->l_state.horizontal_mwin > 0)
3566 ws->l_state.horizontal_mwin--;
3568 case SWM_ARG_ID_STACKINC:
3569 ws->l_state.horizontal_stacks++;
3571 case SWM_ARG_ID_STACKDEC:
3572 if (ws->l_state.horizontal_stacks > 1)
3573 ws->l_state.horizontal_stacks--;
3575 case SWM_ARG_ID_FLIPLAYOUT:
3576 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3584 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3586 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3588 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3591 /* fullscreen view */
3593 max_stack(struct workspace *ws, struct swm_geometry *g)
3595 struct swm_geometry gg = *g;
3596 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3597 int winno, num_screens;
3599 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3604 winno = count_win(ws, 0);
3605 if (winno == 0 && count_win(ws, 1) == 0)
3608 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3609 TAILQ_FOREACH(win, &ws->winlist, entry) {
3610 if (win->transient) {
3612 parent = find_window(win->transient);
3616 if (win->floating && win->floatmaxed == 0 ) {
3618 * retain geometry for retrieval on exit from
3621 store_float_geom(win, ws->r);
3622 win->floatmaxed = 1;
3625 /* only reconfigure if necessary */
3626 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3627 HEIGHT(win) != gg.h) {
3633 WIDTH(win) += 2 * border_width;
3634 HEIGHT(win) += 2 * border_width;
3639 /* unmap only if we don't have multi screen */
3640 if (win != ws->focus)
3641 if (!(num_screens > 1 || outputs > 1))
3645 /* put the last transient on top */
3648 map_window_raised(parent->id);
3649 stack_floater(wintrans, ws->r);
3650 focus_magic(wintrans);
3655 send_to_ws(struct swm_region *r, union arg *args)
3657 int wsid = args->id;
3658 struct ws_win *win = NULL, *parent;
3659 struct workspace *ws, *nws;
3660 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3661 char ws_idx_str[SWM_PROPLEN];
3664 if (wsid >= workspace_limit)
3667 if (r && r->ws && r->ws->focus)
3673 if (win->ws->idx == wsid)
3676 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3679 nws = &win->s->ws[wsid];
3681 a.id = SWM_ARG_ID_FOCUSPREV;
3683 if (win->transient) {
3684 parent = find_window(win->transient);
3686 unmap_window(parent);
3687 TAILQ_REMOVE(&ws->winlist, parent, entry);
3688 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3693 TAILQ_REMOVE(&ws->winlist, win, entry);
3694 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3695 if (TAILQ_EMPTY(&ws->winlist))
3696 r->ws->focus = NULL;
3699 /* Try to update the window's workspace property */
3700 ws_idx_atom = get_atom_from_string("_SWM_WS");
3702 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3704 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3706 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3707 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3716 pressbutton(struct swm_region *r, union arg *args)
3718 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3719 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3720 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3721 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3725 raise_toggle(struct swm_region *r, union arg *args)
3727 if (r == NULL || r->ws == NULL)
3730 r->ws->always_raise = !r->ws->always_raise;
3732 /* bring floaters back to top */
3733 if (r->ws->always_raise == 0)
3738 iconify(struct swm_region *r, union arg *args)
3742 if (r->ws->focus == NULL)
3744 unmap_window(r->ws->focus);
3745 update_iconic(r->ws->focus, 1);
3748 r->ws->focus = NULL;
3749 a.id = SWM_ARG_ID_FOCUSCUR;
3754 get_win_name(xcb_window_t win)
3757 xcb_get_property_cookie_t c;
3758 xcb_icccm_get_text_property_reply_t r;
3760 c = xcb_icccm_get_wm_name(conn, win);
3761 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3762 if (r.name_len > 0) {
3763 name = malloc(r.name_len + 1);
3765 xcb_icccm_get_text_property_reply_wipe(&r);
3768 memcpy(name, r.name, r.name_len);
3769 name[r.name_len] = '\0';
3771 xcb_icccm_get_text_property_reply_wipe(&r);
3778 uniconify(struct swm_region *r, union arg *args)
3785 DNPRINTF(SWM_D_MISC, "uniconify\n");
3787 if (r == NULL || r->ws == NULL)
3790 /* make sure we have anything to uniconify */
3791 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3792 if (win->ws == NULL)
3793 continue; /* should never happen */
3794 if (win->iconic == 0)
3802 search_resp_action = SWM_SEARCH_UNICONIFY;
3804 spawn_select(r, args, "search", &searchpid);
3806 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3809 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3810 if (win->ws == NULL)
3811 continue; /* should never happen */
3812 if (win->iconic == 0)
3815 name = get_win_name(win->id);
3818 fprintf(lfile, "%s.%u\n", name, win->id);
3826 name_workspace(struct swm_region *r, union arg *args)
3830 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3836 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3838 spawn_select(r, args, "name_workspace", &searchpid);
3840 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3843 fprintf(lfile, "%s", "");
3848 search_workspace(struct swm_region *r, union arg *args)
3851 struct workspace *ws;
3854 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3860 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3862 spawn_select(r, args, "search", &searchpid);
3864 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3867 for (i = 0; i < workspace_limit; i++) {
3871 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3872 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3879 search_win_cleanup(void)
3881 struct search_window *sw = NULL;
3883 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3884 xcb_destroy_window(conn, sw->indicator);
3885 xcb_free_gc(conn, sw->gc);
3886 TAILQ_REMOVE(&search_wl, sw, entry);
3892 search_win(struct swm_region *r, union arg *args)
3894 struct ws_win *win = NULL;
3895 struct search_window *sw = NULL;
3897 uint32_t gcv[3], wa[2];
3902 XRectangle ibox, lbox;
3903 DNPRINTF(SWM_D_MISC, "search_win\n");
3906 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3908 spawn_select(r, args, "search", &searchpid);
3910 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3913 TAILQ_INIT(&search_wl);
3916 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3917 if (win->iconic == 1)
3920 sw = calloc(1, sizeof(struct search_window));
3922 warn("search_win: calloc");
3924 search_win_cleanup();
3930 snprintf(s, sizeof s, "%d", i);
3933 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3935 w = xcb_generate_id(conn);
3936 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3937 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3938 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3939 lbox.width + 4, bar_fs_extents->max_logical_extent.height,
3940 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3941 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3944 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3946 sw->gc = xcb_generate_id(conn);
3947 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3948 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3950 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3951 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3952 map_window_raised(w);
3954 xcb_image_text_8(conn, len, w, sw->gc, 2,
3955 (bar_fs_extents->max_logical_extent.height -
3956 lbox.height) / 2 - lbox.y, s);
3958 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3960 fprintf(lfile, "%d\n", i);
3968 search_resp_uniconify(char *resp, unsigned long len)
3974 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3976 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3977 if (win->iconic == 0)
3979 name = get_win_name(win->id);
3982 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3987 if (strncmp(s, resp, len) == 0) {
3988 /* XXX this should be a callback to generalize */
3989 update_iconic(win, 0);
3998 search_resp_name_workspace(char *resp, unsigned long len)
4000 struct workspace *ws;
4002 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4004 if (search_r->ws == NULL)
4009 free(search_r->ws->name);
4010 search_r->ws->name = NULL;
4014 ws->name = strdup(resp);
4015 if (ws->name == NULL) {
4016 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4017 "strdup: %s", strerror(errno));
4024 search_resp_search_workspace(char *resp, unsigned long len)
4031 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4035 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4042 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
4044 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4051 switchws(search_r, &a);
4055 search_resp_search_window(char *resp, unsigned long len)
4060 struct search_window *sw;
4062 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4066 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4071 idx = strtonum(s, 1, INT_MAX, &errstr);
4073 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4080 TAILQ_FOREACH(sw, &search_wl, entry)
4081 if (idx == sw->idx) {
4087 #define MAX_RESP_LEN 1024
4090 search_do_resp(void)
4096 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4101 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4102 warn("search: calloc");
4106 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4108 warn("search: read error");
4111 resp[rbytes] = '\0';
4114 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4115 * newline, so work around that by sanitizing the resp now.
4117 resp[strcspn(resp, "\n")] = '\0';
4120 switch (search_resp_action) {
4121 case SWM_SEARCH_UNICONIFY:
4122 search_resp_uniconify(resp, len);
4124 case SWM_SEARCH_NAME_WORKSPACE:
4125 search_resp_name_workspace(resp, len);
4127 case SWM_SEARCH_SEARCH_WORKSPACE:
4128 search_resp_search_workspace(resp, len);
4130 case SWM_SEARCH_SEARCH_WINDOW:
4131 search_resp_search_window(resp, len);
4136 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4137 search_win_cleanup();
4139 search_resp_action = SWM_SEARCH_NONE;
4140 close(select_resp_pipe[0]);
4145 wkill(struct swm_region *r, union arg *args)
4147 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4149 if (r->ws->focus == NULL)
4152 if (args->id == SWM_ARG_ID_KILLWINDOW)
4153 xcb_kill_client(conn, r->ws->focus->id);
4155 if (r->ws->focus->can_delete)
4156 client_msg(r->ws->focus, adelete);
4161 floating_toggle_win(struct ws_win *win)
4163 struct swm_region *r;
4173 /* reject floating toggles in max stack mode */
4174 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4177 if (win->floating) {
4178 if (!win->floatmaxed) {
4179 /* retain position for refloat */
4180 store_float_geom(win, r);
4184 if (win->g_floatvalid) {
4185 /* refloat at last floating relative position */
4186 X(win) = win->g_float.x + X(r);
4187 Y(win) = win->g_float.y + Y(r);
4188 WIDTH(win) = win->g_float.w;
4189 HEIGHT(win) = win->g_float.h;
4194 ewmh_update_actions(win);
4200 floating_toggle(struct swm_region *r, union arg *args)
4202 struct ws_win *win = r->ws->focus;
4208 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4209 _NET_WM_STATE_TOGGLE);
4213 if (win == win->ws->focus) {
4214 a.id = SWM_ARG_ID_FOCUSCUR;
4215 focus(win->ws->r, &a);
4220 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4222 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4224 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4226 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4229 if (X(win) + BORDER(win) < X(r)) {
4231 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4233 X(win) = X(r) - BORDER(win);
4236 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4238 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4240 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4243 if (Y(win) + BORDER(win) < Y(r)) {
4245 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4247 Y(win) = Y(r) - BORDER(win);
4253 if (HEIGHT(win) < 1)
4259 update_window(struct ws_win *win)
4264 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4265 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4266 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4270 wc[3] = HEIGHT(win);
4271 wc[4] = BORDER(win);
4273 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4274 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4275 wc[3], YESNO(win->bordered));
4277 xcb_configure_window(conn, win->id, mask, wc);
4280 #define SWM_RESIZE_STEPS (50)
4283 resize(struct ws_win *win, union arg *args)
4285 xcb_timestamp_t time = 0;
4286 struct swm_region *r = NULL;
4287 int resize_step = 0;
4288 struct swm_geometry g;
4289 int top = 0, left = 0, buttonrelease;
4291 unsigned int shape; /* cursor style */
4292 xcb_cursor_t cursor;
4293 xcb_font_t cursor_font;
4294 xcb_grab_pointer_cookie_t gpc;
4295 xcb_grab_pointer_reply_t *gpr;
4296 xcb_query_pointer_reply_t *xpr;
4297 xcb_generic_event_t *evt;
4298 xcb_motion_notify_event_t *mne;
4304 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4307 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%x, floating: %s, "
4308 "transient: 0x%x\n", win->id, YESNO(win->floating),
4311 if (!(win->transient != 0 || win->floating != 0))
4314 /* reject resizes in max mode for floaters (transient ok) */
4315 if (win->floatmaxed)
4319 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4325 case SWM_ARG_ID_WIDTHSHRINK:
4326 WIDTH(win) -= SWM_RESIZE_STEPS;
4329 case SWM_ARG_ID_WIDTHGROW:
4330 WIDTH(win) += SWM_RESIZE_STEPS;
4333 case SWM_ARG_ID_HEIGHTSHRINK:
4334 HEIGHT(win) -= SWM_RESIZE_STEPS;
4337 case SWM_ARG_ID_HEIGHTGROW:
4338 HEIGHT(win) += SWM_RESIZE_STEPS;
4345 constrain_window(win, r, 1);
4347 store_float_geom(win,r);
4351 /* get cursor offset from window root */
4352 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4359 if (xpr->win_x < WIDTH(win) / 2)
4362 if (xpr->win_y < HEIGHT(win) / 2)
4365 if (args->id == SWM_ARG_ID_CENTER)
4368 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4370 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4372 cursor_font = xcb_generate_id(conn);
4373 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4375 cursor = xcb_generate_id(conn);
4376 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4377 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4379 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4380 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4382 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4384 xcb_free_cursor(conn, cursor);
4385 xcb_close_font(conn, cursor_font);
4391 while ((evt = xcb_poll_for_event(conn)) && buttonrelease != 1) {
4393 XMaskEvent(display, MOUSEMASK | ExposureMask |
4394 SubstructureRedirectMask, &ev);
4396 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4397 case XCB_BUTTON_RELEASE:
4400 case XCB_MOTION_NOTIFY:
4401 mne = (xcb_motion_notify_event_t *)evt;
4402 /* cursor offset/delta from start of the operation */
4403 dx = mne->root_x - xpr->root_x;
4404 dy = mne->root_y - xpr->root_y;
4410 if (args->id == SWM_ARG_ID_CENTER) {
4411 if (g.h / 2 + dy < 1)
4415 HEIGHT(win) = g.h + 2 * dy;
4423 HEIGHT(win) = g.h + dy;
4430 if (args->id == SWM_ARG_ID_CENTER) {
4431 if (g.w / 2 + dx < 1)
4435 WIDTH(win) = g.w + 2 * dx;
4443 WIDTH(win) = g.w + dx;
4446 constrain_window(win, r, 1);
4448 /* not free, don't sync more than 120 times / second */
4449 if ((mne->time - time) > (1000 / 120) ) {
4465 store_float_geom(win,r);
4467 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4468 xcb_free_cursor(conn, cursor);
4469 xcb_close_font(conn, cursor_font);
4475 resize_step(struct swm_region *r, union arg *args)
4477 struct ws_win *win = NULL;
4479 if (r && r->ws && r->ws->focus)
4487 #define SWM_MOVE_STEPS (50)
4490 move(struct ws_win *win, union arg *args)
4492 xcb_timestamp_t time = 0;
4493 int move_step = 0, buttonrelease;
4494 struct swm_region *r = NULL;
4495 xcb_font_t cursor_font;
4496 xcb_cursor_t cursor;
4497 xcb_grab_pointer_cookie_t gpc;
4498 xcb_grab_pointer_reply_t *gpr;
4499 xcb_query_pointer_reply_t *qpr;
4500 xcb_generic_event_t *evt;
4501 xcb_motion_notify_event_t *mne;
4507 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4510 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%x, floating: %s, transient: "
4511 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4513 /* in max_stack mode should only move transients */
4514 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4518 if (win->floating == 0 && !win->transient) {
4519 store_float_geom(win, r);
4520 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4523 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4530 case SWM_ARG_ID_MOVELEFT:
4531 X(win) -= (SWM_MOVE_STEPS - border_width);
4534 case SWM_ARG_ID_MOVERIGHT:
4535 X(win) += (SWM_MOVE_STEPS - border_width);
4538 case SWM_ARG_ID_MOVEUP:
4539 Y(win) -= (SWM_MOVE_STEPS - border_width);
4542 case SWM_ARG_ID_MOVEDOWN:
4543 Y(win) += (SWM_MOVE_STEPS - border_width);
4550 constrain_window(win, r, 0);
4552 store_float_geom(win, r);
4556 cursor_font = xcb_generate_id(conn);
4557 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4559 cursor = xcb_generate_id(conn);
4560 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4561 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4563 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4564 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4565 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4566 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4568 xcb_free_cursor(conn, cursor);
4569 xcb_close_font(conn, cursor_font);
4573 /* get cursor offset from window root */
4574 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4577 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4578 xcb_free_cursor(conn, cursor);
4579 xcb_close_font(conn, cursor_font);
4584 while ((evt = xcb_poll_for_event(conn)) && buttonrelease != 1) {
4586 XMaskEvent(display, MOUSEMASK | ExposureMask |
4587 SubstructureRedirectMask, &ev);
4589 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4590 case XCB_BUTTON_RELEASE:
4593 case XCB_MOTION_NOTIFY:
4594 mne = (xcb_motion_notify_event_t *)evt;
4595 X(win) = mne->root_x - qpr->win_x - border_width;
4596 Y(win) = mne->root_y - qpr->win_y - border_width;
4598 constrain_window(win, r, 0);
4600 /* not free, don't sync more than 120 times / second */
4601 if ((mne->time - time) > (1000 / 120) ) {
4617 store_float_geom(win, r);
4619 xcb_free_cursor(conn, cursor);
4620 xcb_close_font(conn, cursor_font);
4621 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4625 move_step(struct swm_region *r, union arg *args)
4627 struct ws_win *win = NULL;
4629 if (r && r->ws && r->ws->focus)
4634 if (!(win->transient != 0 || win->floating != 0))
4641 /* user/key callable function IDs */
4730 kf_search_workspace,
4732 kf_dumpwins, /* MUST BE LAST */
4736 /* key definitions */
4738 dummykeyfunc(struct swm_region *r, union arg *args)
4743 char name[SWM_FUNCNAME_LEN];
4744 void (*func)(struct swm_region *r, union arg *);
4746 } keyfuncs[kf_invalid + 1] = {
4747 /* name function argument */
4748 { "cycle_layout", cycle_layout, {0} },
4749 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4750 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4751 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4752 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4753 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4754 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4755 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4756 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4757 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4758 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4759 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4760 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4761 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4762 { "quit", quit, {0} },
4763 { "restart", restart, {0} },
4764 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4765 { "ws_1", switchws, {.id = 0} },
4766 { "ws_2", switchws, {.id = 1} },
4767 { "ws_3", switchws, {.id = 2} },
4768 { "ws_4", switchws, {.id = 3} },
4769 { "ws_5", switchws, {.id = 4} },
4770 { "ws_6", switchws, {.id = 5} },
4771 { "ws_7", switchws, {.id = 6} },
4772 { "ws_8", switchws, {.id = 7} },
4773 { "ws_9", switchws, {.id = 8} },
4774 { "ws_10", switchws, {.id = 9} },
4775 { "ws_11", switchws, {.id = 10} },
4776 { "ws_12", switchws, {.id = 11} },
4777 { "ws_13", switchws, {.id = 12} },
4778 { "ws_14", switchws, {.id = 13} },
4779 { "ws_15", switchws, {.id = 14} },
4780 { "ws_16", switchws, {.id = 15} },
4781 { "ws_17", switchws, {.id = 16} },
4782 { "ws_18", switchws, {.id = 17} },
4783 { "ws_19", switchws, {.id = 18} },
4784 { "ws_20", switchws, {.id = 19} },
4785 { "ws_21", switchws, {.id = 20} },
4786 { "ws_22", switchws, {.id = 21} },
4787 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4788 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4789 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4790 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4791 { "ws_prior", priorws, {0} },
4792 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4793 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4794 { "mvws_1", send_to_ws, {.id = 0} },
4795 { "mvws_2", send_to_ws, {.id = 1} },
4796 { "mvws_3", send_to_ws, {.id = 2} },
4797 { "mvws_4", send_to_ws, {.id = 3} },
4798 { "mvws_5", send_to_ws, {.id = 4} },
4799 { "mvws_6", send_to_ws, {.id = 5} },
4800 { "mvws_7", send_to_ws, {.id = 6} },
4801 { "mvws_8", send_to_ws, {.id = 7} },
4802 { "mvws_9", send_to_ws, {.id = 8} },
4803 { "mvws_10", send_to_ws, {.id = 9} },
4804 { "mvws_11", send_to_ws, {.id = 10} },
4805 { "mvws_12", send_to_ws, {.id = 11} },
4806 { "mvws_13", send_to_ws, {.id = 12} },
4807 { "mvws_14", send_to_ws, {.id = 13} },
4808 { "mvws_15", send_to_ws, {.id = 14} },
4809 { "mvws_16", send_to_ws, {.id = 15} },
4810 { "mvws_17", send_to_ws, {.id = 16} },
4811 { "mvws_18", send_to_ws, {.id = 17} },
4812 { "mvws_19", send_to_ws, {.id = 18} },
4813 { "mvws_20", send_to_ws, {.id = 19} },
4814 { "mvws_21", send_to_ws, {.id = 20} },
4815 { "mvws_22", send_to_ws, {.id = 21} },
4816 { "bar_toggle", bar_toggle, {0} },
4817 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4818 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4819 { "float_toggle", floating_toggle,{0} },
4820 { "version", version, {0} },
4821 { "spawn_custom", dummykeyfunc, {0} },
4822 { "iconify", iconify, {0} },
4823 { "uniconify", uniconify, {0} },
4824 { "raise_toggle", raise_toggle, {0} },
4825 { "button2", pressbutton, {2} },
4826 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4827 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4828 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4829 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4830 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4831 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4832 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4833 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4834 { "name_workspace", name_workspace, {0} },
4835 { "search_workspace", search_workspace, {0} },
4836 { "search_win", search_win, {0} },
4837 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4838 { "invalid key func", NULL, {0} },
4841 RB_ENTRY(key) entry;
4844 enum keyfuncid funcid;
4847 RB_HEAD(key_tree, key);
4850 key_cmp(struct key *kp1, struct key *kp2)
4852 if (kp1->keysym < kp2->keysym)
4854 if (kp1->keysym > kp2->keysym)
4857 if (kp1->mod < kp2->mod)
4859 if (kp1->mod > kp2->mod)
4865 RB_GENERATE(key_tree, key, entry, key_cmp);
4866 struct key_tree keys;
4869 enum { client_click, root_click };
4871 unsigned int action;
4873 unsigned int button;
4874 void (*func)(struct ws_win *, union arg *);
4877 /* action key mouse button func args */
4878 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4879 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4880 { client_click, MODKEY, Button1, move, {0} },
4884 update_modkey(unsigned int mod)
4890 RB_FOREACH(kp, key_tree, &keys)
4891 if (kp->mod & ShiftMask)
4892 kp->mod = mod | ShiftMask;
4896 for (i = 0; i < LENGTH(buttons); i++)
4897 if (buttons[i].mask & ShiftMask)
4898 buttons[i].mask = mod | ShiftMask;
4900 buttons[i].mask = mod;
4905 TAILQ_ENTRY(spawn_prog) entry;
4910 TAILQ_HEAD(spawn_list, spawn_prog);
4911 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4914 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4917 struct spawn_prog *prog = NULL;
4919 char *ap, **real_args;
4921 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4924 TAILQ_FOREACH(prog, &spawns, entry) {
4925 if (!strcasecmp(spawn_name, prog->name))
4929 warnx("spawn_custom: program %s not found", spawn_name);
4933 /* make room for expanded args */
4934 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4935 err(1, "spawn_custom: calloc real_args");
4937 /* expand spawn_args into real_args */
4938 for (i = 0; i < prog->argc; i++) {
4940 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4941 if (!strcasecmp(ap, "$bar_border")) {
4943 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4945 err(1, "spawn_custom border color");
4946 } else if (!strcasecmp(ap, "$bar_color")) {
4948 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4950 err(1, "spawn_custom bar color");
4951 } else if (!strcasecmp(ap, "$bar_font")) {
4952 if ((real_args[i] = strdup(bar_fonts))
4954 err(1, "spawn_custom bar fonts");
4955 } else if (!strcasecmp(ap, "$bar_font_color")) {
4957 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4959 err(1, "spawn_custom color font");
4960 } else if (!strcasecmp(ap, "$color_focus")) {
4962 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4964 err(1, "spawn_custom color focus");
4965 } else if (!strcasecmp(ap, "$color_unfocus")) {
4967 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4969 err(1, "spawn_custom color unfocus");
4971 /* no match --> copy as is */
4972 if ((real_args[i] = strdup(ap)) == NULL)
4973 err(1, "spawn_custom strdup(ap)");
4975 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4980 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4981 for (i = 0; i < prog->argc; i++)
4982 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4983 DNPRINTF(SWM_D_SPAWN, "\n");
4985 *ret_args = real_args;
4986 return (prog->argc);
4990 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4996 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5000 spawn(r->ws->idx, &a, 1);
5002 for (i = 0; i < spawn_argc; i++)
5008 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5014 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5018 if (pipe(select_list_pipe) == -1)
5019 err(1, "pipe error");
5020 if (pipe(select_resp_pipe) == -1)
5021 err(1, "pipe error");
5023 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5024 err(1, "could not disable SIGPIPE");
5025 switch (*pid = fork()) {
5027 err(1, "cannot fork");
5030 if (dup2(select_list_pipe[0], 0) == -1)
5032 if (dup2(select_resp_pipe[1], 1) == -1)
5034 close(select_list_pipe[1]);
5035 close(select_resp_pipe[0]);
5036 spawn(r->ws->idx, &a, 0);
5038 default: /* parent */
5039 close(select_list_pipe[0]);
5040 close(select_resp_pipe[1]);
5044 for (i = 0; i < spawn_argc; i++)
5050 spawn_insert(char *name, char *args)
5052 char *arg, *cp, *ptr;
5053 struct spawn_prog *sp;
5055 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5057 if ((sp = calloc(1, sizeof *sp)) == NULL)
5058 err(1, "spawn_insert: malloc");
5059 if ((sp->name = strdup(name)) == NULL)
5060 err(1, "spawn_insert: strdup");
5062 /* convert the arguments to an argument list */
5063 if ((ptr = cp = strdup(args)) == NULL)
5064 err(1, "spawn_insert: strdup");
5065 while ((arg = strsep(&ptr, " \t")) != NULL) {
5066 /* empty field; skip it */
5071 if ((sp->argv = realloc(sp->argv, sp->argc *
5072 sizeof *sp->argv)) == NULL)
5073 err(1, "spawn_insert: realloc");
5074 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5075 err(1, "spawn_insert: strdup");
5079 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5080 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5084 spawn_remove(struct spawn_prog *sp)
5088 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5090 TAILQ_REMOVE(&spawns, sp, entry);
5091 for (i = 0; i < sp->argc; i++)
5097 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5101 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5103 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5106 spawn_insert(name, args);
5108 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5112 setspawn(char *name, char *args)
5114 struct spawn_prog *sp;
5116 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5121 TAILQ_FOREACH(sp, &spawns, entry) {
5122 if (!strcmp(sp->name, name)) {
5126 spawn_replace(sp, name, args);
5127 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5131 if (*args == '\0') {
5132 warnx("error: setspawn: cannot find program: %s", name);
5136 spawn_insert(name, args);
5137 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5141 setconfspawn(char *selector, char *value, int flags)
5143 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5145 setspawn(selector, value);
5147 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5154 setconfspawn("term", "xterm", 0);
5155 setconfspawn("spawn_term", "xterm", 0);
5156 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5157 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5158 setconfspawn("lock", "xlock", 0);
5159 setconfspawn("initscr", "initscreen.sh", 0);
5160 setconfspawn("menu", "dmenu_run"
5163 " -nf $bar_font_color"
5165 " -sf $bar_color", 0);
5166 setconfspawn("search", "dmenu"
5170 " -nf $bar_font_color"
5172 " -sf $bar_color", 0);
5173 setconfspawn("name_workspace", "dmenu"
5177 " -nf $bar_font_color"
5179 " -sf $bar_color", 0);
5183 #define SWM_MODNAME_SIZE 32
5184 #define SWM_KEY_WS "\n+ \t"
5186 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5190 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5191 if (mod == NULL || ks == NULL) {
5192 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5195 if (keystr == NULL || strlen(keystr) == 0) {
5196 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5202 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5203 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5205 cp += (long)strspn(cp, SWM_KEY_WS);
5206 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5208 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5210 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5212 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5214 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5216 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5218 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5219 *mod |= ControlMask;
5221 *ks = XStringToKeysym(name);
5222 XConvertCase(*ks, ks, &uks);
5223 if (ks == NoSymbol) {
5225 "parsekeys: invalid key %s\n",
5231 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5236 strdupsafe(char *str)
5241 return (strdup(str));
5245 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5249 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5250 keyfuncs[kfid].name, spawn_name);
5252 if ((kp = malloc(sizeof *kp)) == NULL)
5253 err(1, "key_insert: malloc");
5258 kp->spawn_name = strdupsafe(spawn_name);
5259 RB_INSERT(key_tree, &keys, kp);
5261 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5265 key_lookup(unsigned int mod, KeySym ks)
5272 return (RB_FIND(key_tree, &keys, &kp));
5276 key_remove(struct key *kp)
5278 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5280 RB_REMOVE(key_tree, &keys, kp);
5281 free(kp->spawn_name);
5284 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5288 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5291 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5295 key_insert(mod, ks, kfid, spawn_name);
5297 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5301 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5306 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5307 keyfuncs[kfid].name, spawn_name);
5309 if ((kp = key_lookup(mod, ks)) != NULL) {
5310 if (kfid == kf_invalid)
5313 key_replace(kp, mod, ks, kfid, spawn_name);
5314 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5317 if (kfid == kf_invalid) {
5318 warnx("error: setkeybinding: cannot find mod/key combination");
5319 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5323 key_insert(mod, ks, kfid, spawn_name);
5324 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5328 setconfbinding(char *selector, char *value, int flags)
5330 enum keyfuncid kfid;
5333 struct spawn_prog *sp;
5334 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5335 if (selector == NULL) {
5336 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5337 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5339 setkeybinding(mod, ks, kfid, NULL);
5344 /* search by key function name */
5345 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5346 if (strncasecmp(selector, keyfuncs[kfid].name,
5347 SWM_FUNCNAME_LEN) == 0) {
5348 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5350 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5351 setkeybinding(mod, ks, kfid, NULL);
5357 /* search by custom spawn name */
5358 TAILQ_FOREACH(sp, &spawns, entry) {
5359 if (strcasecmp(selector, sp->name) == 0) {
5360 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5362 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5363 setkeybinding(mod, ks, kf_spawn_custom,
5370 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5377 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5380 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5381 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5382 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5383 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5384 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5385 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5386 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5387 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5388 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5389 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5390 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5391 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5392 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5393 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5394 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5395 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5396 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5397 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5398 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5399 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5400 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5401 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5402 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5403 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5404 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5405 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5406 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5407 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5408 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5409 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5410 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5411 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5412 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5413 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5414 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5415 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5416 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5417 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5418 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5419 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5420 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5421 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5422 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5424 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5425 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5426 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5427 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5428 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5429 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5430 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5431 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5432 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5433 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5434 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5435 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5436 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5437 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5438 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5439 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5440 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5441 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5442 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5443 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5444 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5445 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5446 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5447 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5448 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5449 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5450 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5451 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5452 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5453 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5454 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5455 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5456 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5457 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5458 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5459 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5460 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5461 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5462 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5463 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5464 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5465 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5466 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5467 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5468 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5469 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5470 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5471 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5472 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5474 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5483 while (RB_EMPTY(&keys) == 0) {
5484 kp = RB_ROOT(&keys);
5490 setkeymapping(char *selector, char *value, int flags)
5492 char keymapping_file[PATH_MAX];
5493 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5494 if (value[0] == '~')
5495 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5496 pwd->pw_dir, &value[1]);
5498 strlcpy(keymapping_file, value, sizeof keymapping_file);
5500 /* load new key bindings; if it fails, revert to default bindings */
5501 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5505 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5510 updatenumlockmask(void)
5513 xcb_get_modifier_mapping_reply_t *modmap_r;
5514 xcb_keycode_t *modmap, kc;
5516 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5519 modmap_r = xcb_get_modifier_mapping_reply(conn,
5520 xcb_get_modifier_mapping(conn),
5523 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5524 for (i = 0; i < 8; i++) {
5525 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5526 kc = modmap[i * modmap_r->keycodes_per_modifier
5529 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5531 numlockmask = (1 << i);
5543 xcb_keycode_t *code;
5544 unsigned int modifiers[] =
5545 { 0, LockMask, numlockmask, numlockmask | LockMask };
5548 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5549 updatenumlockmask();
5551 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5552 for (k = 0; k < num_screens; k++) {
5553 if (TAILQ_EMPTY(&screens[k].rl))
5555 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5557 RB_FOREACH(kp, key_tree, &keys) {
5558 if ((code = xcb_key_symbols_get_keycode(syms,
5560 for (j = 0; j < LENGTH(modifiers); j++)
5561 xcb_grab_key(conn, True,
5563 kp->mod | modifiers[j],
5564 *code, XCB_GRAB_MODE_ASYNC,
5565 XCB_GRAB_MODE_ASYNC);
5571 grabbuttons(struct ws_win *win, int focused)
5574 unsigned int modifiers[] =
5575 { 0, LockMask, numlockmask, numlockmask|LockMask };
5577 updatenumlockmask();
5578 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5579 XCB_BUTTON_MASK_ANY);
5581 for (i = 0; i < LENGTH(buttons); i++)
5582 if (buttons[i].action == client_click)
5583 for (j = 0; j < LENGTH(modifiers); j++)
5584 xcb_grab_button(conn, False, win->id,
5586 XCB_GRAB_MODE_ASYNC,
5593 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5594 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5595 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5598 const char *quirkname[] = {
5599 "NONE", /* config string for "no value" */
5608 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5609 #define SWM_Q_WS "\n|+ \t"
5611 parsequirks(char *qstr, unsigned long *quirk)
5621 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5623 cp += (long)strspn(cp, SWM_Q_WS);
5624 for (i = 0; i < LENGTH(quirkname); i++) {
5625 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5626 DNPRINTF(SWM_D_QUIRK,
5627 "parsequirks: %s\n", name);
5632 *quirk |= 1 << (i-1);
5636 if (i >= LENGTH(quirkname)) {
5637 DNPRINTF(SWM_D_QUIRK,
5638 "parsequirks: invalid quirk [%s]\n", name);
5646 quirk_insert(const char *class, const char *name, unsigned long quirk)
5650 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5653 if ((qp = malloc(sizeof *qp)) == NULL)
5654 err(1, "quirk_insert: malloc");
5655 if ((qp->class = strdup(class)) == NULL)
5656 err(1, "quirk_insert: strdup");
5657 if ((qp->name = strdup(name)) == NULL)
5658 err(1, "quirk_insert: strdup");
5661 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5663 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5667 quirk_remove(struct quirk *qp)
5669 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5670 qp->name, qp->quirk);
5672 TAILQ_REMOVE(&quirks, qp, entry);
5677 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5681 quirk_replace(struct quirk *qp, const char *class, const char *name,
5682 unsigned long quirk)
5684 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5685 qp->name, qp->quirk);
5688 quirk_insert(class, name, quirk);
5690 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5694 setquirk(const char *class, const char *name, unsigned long quirk)
5698 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5701 TAILQ_FOREACH(qp, &quirks, entry) {
5702 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5706 quirk_replace(qp, class, name, quirk);
5707 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5712 warnx("error: setquirk: cannot find class/name combination");
5716 quirk_insert(class, name, quirk);
5717 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5721 setconfquirk(char *selector, char *value, int flags)
5723 char *cp, *class, *name;
5725 unsigned long quirks;
5726 if (selector == NULL)
5728 if ((cp = strchr(selector, ':')) == NULL)
5733 if ((retval = parsequirks(value, &quirks)) == 0)
5734 setquirk(class, name, quirks);
5741 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5742 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5743 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5744 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5745 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5746 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5747 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5748 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5749 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5750 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5751 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5752 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5753 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5756 /* conf file stuff */
5757 #define SWM_CONF_FILE "spectrwm.conf"
5758 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5762 SWM_S_BAR_AT_BOTTOM,
5763 SWM_S_BAR_BORDER_WIDTH,
5770 SWM_S_CLOCK_ENABLED,
5773 SWM_S_CYCLE_VISIBLE,
5775 SWM_S_DISABLE_BORDER,
5777 SWM_S_FOCUS_CLOSE_WRAP,
5778 SWM_S_FOCUS_DEFAULT,
5784 SWM_S_STACK_ENABLED,
5786 SWM_S_TITLE_CLASS_ENABLED,
5787 SWM_S_TITLE_NAME_ENABLED,
5788 SWM_S_URGENT_ENABLED,
5789 SWM_S_VERBOSE_LAYOUT,
5790 SWM_S_WINDOW_NAME_ENABLED,
5791 SWM_S_WORKSPACE_LIMIT
5795 setconfvalue(char *selector, char *value, int flags)
5801 case SWM_S_BAR_ACTION:
5803 if ((bar_argv[0] = strdup(value)) == NULL)
5804 err(1, "setconfvalue: bar_action");
5806 case SWM_S_BAR_AT_BOTTOM:
5807 bar_at_bottom = atoi(value);
5809 case SWM_S_BAR_BORDER_WIDTH:
5810 bar_border_width = atoi(value);
5811 if (bar_border_width < 0)
5812 bar_border_width = 0;
5814 case SWM_S_BAR_DELAY:
5815 bar_delay = atoi(value);
5817 case SWM_S_BAR_ENABLED:
5818 bar_enabled = atoi(value);
5820 case SWM_S_BAR_FONT:
5822 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5823 err(1, "setconfvalue: asprintf: failed to allocate "
5824 "memory for bar_fonts.");
5828 case SWM_S_BAR_FORMAT:
5830 if ((bar_format = strdup(value)) == NULL)
5831 err(1, "setconfvalue: bar_format");
5833 case SWM_S_BAR_JUSTIFY:
5834 if (!strcmp(value, "left"))
5835 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5836 else if (!strcmp(value, "center"))
5837 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5838 else if (!strcmp(value, "right"))
5839 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5841 errx(1, "invalid bar_justify");
5843 case SWM_S_BORDER_WIDTH:
5844 border_width = atoi(value);
5845 if (border_width < 0)
5848 case SWM_S_CLOCK_ENABLED:
5849 clock_enabled = atoi(value);
5851 case SWM_S_CLOCK_FORMAT:
5852 #ifndef SWM_DENY_CLOCK_FORMAT
5854 if ((clock_format = strdup(value)) == NULL)
5855 err(1, "setconfvalue: clock_format");
5858 case SWM_S_CYCLE_EMPTY:
5859 cycle_empty = atoi(value);
5861 case SWM_S_CYCLE_VISIBLE:
5862 cycle_visible = atoi(value);
5864 case SWM_S_DIALOG_RATIO:
5865 dialog_ratio = atof(value);
5866 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5869 case SWM_S_DISABLE_BORDER:
5870 disable_border = atoi(value);
5872 case SWM_S_FOCUS_CLOSE:
5873 if (!strcmp(value, "first"))
5874 focus_close = SWM_STACK_BOTTOM;
5875 else if (!strcmp(value, "last"))
5876 focus_close = SWM_STACK_TOP;
5877 else if (!strcmp(value, "next"))
5878 focus_close = SWM_STACK_ABOVE;
5879 else if (!strcmp(value, "previous"))
5880 focus_close = SWM_STACK_BELOW;
5882 errx(1, "focus_close");
5884 case SWM_S_FOCUS_CLOSE_WRAP:
5885 focus_close_wrap = atoi(value);
5887 case SWM_S_FOCUS_DEFAULT:
5888 if (!strcmp(value, "last"))
5889 focus_default = SWM_STACK_TOP;
5890 else if (!strcmp(value, "first"))
5891 focus_default = SWM_STACK_BOTTOM;
5893 errx(1, "focus_default");
5895 case SWM_S_FOCUS_MODE:
5896 if (!strcmp(value, "default"))
5897 focus_mode = SWM_FOCUS_DEFAULT;
5898 else if (!strcmp(value, "follow_cursor"))
5899 focus_mode = SWM_FOCUS_FOLLOW;
5900 else if (!strcmp(value, "synergy"))
5901 focus_mode = SWM_FOCUS_SYNERGY;
5903 errx(1, "focus_mode");
5905 case SWM_S_SPAWN_ORDER:
5906 if (!strcmp(value, "first"))
5907 spawn_position = SWM_STACK_BOTTOM;
5908 else if (!strcmp(value, "last"))
5909 spawn_position = SWM_STACK_TOP;
5910 else if (!strcmp(value, "next"))
5911 spawn_position = SWM_STACK_ABOVE;
5912 else if (!strcmp(value, "previous"))
5913 spawn_position = SWM_STACK_BELOW;
5915 errx(1, "spawn_position");
5917 case SWM_S_SPAWN_TERM:
5918 setconfspawn("term", value, 0);
5919 setconfspawn("spawn_term", value, 0);
5923 case SWM_S_SS_ENABLED:
5924 ss_enabled = atoi(value);
5926 case SWM_S_STACK_ENABLED:
5927 stack_enabled = atoi(value);
5929 case SWM_S_TERM_WIDTH:
5930 term_width = atoi(value);
5934 case SWM_S_TITLE_CLASS_ENABLED:
5935 title_class_enabled = atoi(value);
5937 case SWM_S_TITLE_NAME_ENABLED:
5938 title_name_enabled = atoi(value);
5940 case SWM_S_URGENT_ENABLED:
5941 urgent_enabled = atoi(value);
5943 case SWM_S_VERBOSE_LAYOUT:
5944 verbose_layout = atoi(value);
5945 for (i = 0; layouts[i].l_stack != NULL; i++) {
5947 layouts[i].l_string = fancy_stacker;
5949 layouts[i].l_string = plain_stacker;
5952 case SWM_S_WINDOW_NAME_ENABLED:
5953 window_name_enabled = atoi(value);
5955 case SWM_S_WORKSPACE_LIMIT:
5956 workspace_limit = atoi(value);
5957 if (workspace_limit > SWM_WS_MAX)
5958 workspace_limit = SWM_WS_MAX;
5959 else if (workspace_limit < 1)
5960 workspace_limit = 1;
5969 setconfmodkey(char *selector, char *value, int flags)
5971 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5972 update_modkey(Mod1Mask);
5973 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5974 update_modkey(Mod2Mask);
5975 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5976 update_modkey(Mod3Mask);
5977 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5978 update_modkey(Mod4Mask);
5985 setconfcolor(char *selector, char *value, int flags)
5987 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5992 setconfregion(char *selector, char *value, int flags)
5994 custom_region(value);
5999 setautorun(char *selector, char *value, int flags)
6009 if (getenv("SWM_STARTED"))
6013 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6014 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6016 if (ws_id < 0 || ws_id >= workspace_limit)
6017 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6020 * This is a little intricate
6022 * If the pid already exists we simply reuse it because it means it was
6023 * used before AND not claimed by manage_window. We get away with
6024 * altering it in the parent after INSERT because this can not be a race
6027 while ((ap = strsep(&sp, " \t")) != NULL) {
6030 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6032 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6033 err(1, "setautorun: realloc");
6034 a.argv[argc - 1] = ap;
6037 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6038 err(1, "setautorun: realloc");
6039 a.argv[argc] = NULL;
6041 if ((pid = fork()) == 0) {
6042 spawn(ws_id, &a, 1);
6051 p = calloc(1, sizeof *p);
6054 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6064 setlayout(char *selector, char *value, int flags)
6066 int ws_id, i, x, mg, ma, si, raise, f = 0;
6067 int st = SWM_V_STACK, num_screens;
6069 struct workspace *ws;
6071 if (getenv("SWM_STARTED"))
6075 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6076 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6077 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6078 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6081 if (ws_id < 0 || ws_id >= workspace_limit)
6082 errx(1, "layout: invalid workspace %d", ws_id + 1);
6084 if (!strcasecmp(s, "vertical"))
6086 else if (!strcasecmp(s, "vertical_flip")) {
6089 } else if (!strcasecmp(s, "horizontal"))
6091 else if (!strcasecmp(s, "horizontal_flip")) {
6094 } else if (!strcasecmp(s, "fullscreen"))
6097 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6098 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6101 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6102 for (i = 0; i < num_screens; i++) {
6103 ws = (struct workspace *)&screens[i].ws;
6104 ws[ws_id].cur_layout = &layouts[st];
6106 ws[ws_id].always_raise = raise;
6107 if (st == SWM_MAX_STACK)
6111 for (x = 0; x < abs(mg); x++) {
6112 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6113 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6114 SWM_ARG_ID_MASTERSHRINK);
6118 for (x = 0; x < abs(ma); x++) {
6119 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6120 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6121 SWM_ARG_ID_MASTERDEL);
6125 for (x = 0; x < abs(si); x++) {
6126 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6127 si >= 0 ? SWM_ARG_ID_STACKINC :
6128 SWM_ARG_ID_STACKDEC);
6133 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6134 SWM_ARG_ID_FLIPLAYOUT);
6142 /* config options */
6143 struct config_option {
6145 int (*func)(char*, char*, int);
6148 struct config_option configopt[] = {
6149 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6150 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6151 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6152 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6153 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6154 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6155 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6156 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6157 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6158 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6159 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6160 { "keyboard_mapping", setkeymapping, 0 },
6161 { "bind", setconfbinding, 0 },
6162 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6163 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6164 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6165 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6166 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6167 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6168 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6169 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6170 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6171 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6172 { "modkey", setconfmodkey, 0 },
6173 { "program", setconfspawn, 0 },
6174 { "quirk", setconfquirk, 0 },
6175 { "region", setconfregion, 0 },
6176 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6177 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6178 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6179 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6180 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6181 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6182 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6183 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6184 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6185 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6186 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6187 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6188 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6189 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6190 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6191 { "autorun", setautorun, 0 },
6192 { "layout", setlayout, 0 },
6197 conf_load(char *filename, int keymapping)
6200 char *line, *cp, *optsub, *optval;
6201 size_t linelen, lineno = 0;
6202 int wordlen, i, optind;
6203 struct config_option *opt;
6205 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6207 if (filename == NULL) {
6208 warnx("conf_load: no filename");
6211 if ((config = fopen(filename, "r")) == NULL) {
6212 warn("conf_load: fopen: %s", filename);
6216 while (!feof(config)) {
6217 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6220 err(1, "%s", filename);
6225 cp += strspn(cp, " \t\n"); /* eat whitespace */
6226 if (cp[0] == '\0') {
6231 /* get config option */
6232 wordlen = strcspn(cp, "=[ \t\n");
6234 warnx("%s: line %zd: no option found",
6239 for (i = 0; i < LENGTH(configopt); i++) {
6240 opt = &configopt[i];
6241 if (!strncasecmp(cp, opt->optname, wordlen) &&
6242 strlen(opt->optname) == wordlen) {
6248 warnx("%s: line %zd: unknown option %.*s",
6249 filename, lineno, wordlen, cp);
6252 if (keymapping && strcmp(opt->optname, "bind")) {
6253 warnx("%s: line %zd: invalid option %.*s",
6254 filename, lineno, wordlen, cp);
6258 cp += strspn(cp, " \t\n"); /* eat whitespace */
6259 /* get [selector] if any */
6263 wordlen = strcspn(cp, "]");
6266 warnx("%s: line %zd: syntax error",
6271 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6273 warnx("%s: line %zd: unable to allocate"
6274 "memory for selector", filename,
6280 cp += strspn(cp, "] \t\n"); /* eat trailing */
6282 cp += strspn(cp, "= \t\n"); /* eat trailing */
6284 optval = strdup(cp);
6285 /* call function to deal with it all */
6286 if (configopt[optind].func(optsub, optval,
6287 configopt[optind].funcflags) != 0)
6288 errx(1, "%s: line %zd: invalid data for %s",
6289 filename, lineno, configopt[optind].optname);
6296 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6303 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6309 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6311 struct ws_win *parent, *w;
6312 struct swm_region *r;
6313 struct workspace *ws;
6314 xcb_icccm_wm_hints_t wmh;
6316 parent = find_window(win->transient);
6318 parent->child_trans = win;
6320 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6321 " for 0x%x trans 0x%x\n", win->id, win->transient);
6323 r = root_to_region(win->wa->root);
6325 /* parent doen't exist in our window list */
6326 TAILQ_FOREACH(w, &ws->winlist, entry) {
6327 if (xcb_icccm_get_wm_hints_reply(conn,
6328 xcb_icccm_get_wm_hints(conn, w->id),
6330 warnx("can't get hints for 0x%x", w->id);
6334 if (win->hints.window_group != wmh.window_group)
6337 w->child_trans = win;
6338 win->transient = w->id;
6340 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6341 "transient to 0x%x\n", win->transient);
6348 window_get_pid(xcb_window_t win)
6353 xcb_get_property_cookie_t pc;
6354 xcb_get_property_reply_t *pr;
6356 apid = get_atom_from_string("_NET_WM_PID");
6357 if (apid == XCB_ATOM_NONE)
6360 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6361 pr = xcb_get_property_reply(conn, pc, NULL);
6364 if (pr->type != XCB_ATOM_CARDINAL)
6367 ret = *(long *)xcb_get_property_value(pr);
6373 apid = get_atom_from_string("_SWM_PID");
6374 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6376 pr = xcb_get_property_reply(conn, pc, NULL);
6379 if (pr->type != XCB_ATOM_STRING)
6382 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6389 manage_window(xcb_window_t id)
6391 xcb_window_t trans = XCB_WINDOW_NONE;
6392 struct workspace *ws;
6393 struct ws_win *win, *ww;
6394 int i, ws_idx, border_me = 0;
6395 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6396 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6398 struct swm_region *r;
6402 uint32_t event_mask;
6404 xcb_get_property_reply_t *gpr;
6405 xcb_icccm_get_wm_protocols_reply_t wpr;
6407 if ((win = find_window(id)) != NULL)
6408 return (win); /* already being managed */
6410 /* see if we are on the unmanaged list */
6411 if ((win = find_unmanaged_window(id)) != NULL) {
6412 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6413 "window: 0x%x\n", win->id);
6414 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6416 set_child_transient(win, &trans);
6418 if (trans && (ww = find_window(trans)))
6419 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6420 else if ((ww = win->ws->focus) &&
6421 spawn_position == SWM_STACK_ABOVE)
6422 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6424 else if (ww && spawn_position == SWM_STACK_BELOW)
6425 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6426 else switch (spawn_position) {
6429 case SWM_STACK_ABOVE:
6430 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6432 case SWM_STACK_BOTTOM:
6433 case SWM_STACK_BELOW:
6434 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6437 ewmh_update_actions(win);
6441 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6442 err(1, "manage_window: calloc: failed to allocate memory for "
6448 /* see if we need to override the workspace */
6449 p = find_pid(window_get_pid(id));
6451 /* Get all the window data in one shot */
6452 ws_idx_atom = get_atom_from_string("_SWM_WS");
6454 gpr = xcb_get_property_reply(conn,
6455 xcb_get_property(conn, False, id, ws_idx_atom,
6456 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6459 proplen = xcb_get_property_value_length(gpr);
6461 prop = malloc(proplen + 1);
6464 xcb_get_property_value(gpr),
6466 prop[proplen] = '\0';
6472 win->wa = xcb_get_geometry_reply(conn,
6473 xcb_get_geometry(conn, id),
6475 xcb_icccm_get_wm_normal_hints_reply(conn,
6476 xcb_icccm_get_wm_normal_hints(conn, id),
6478 xcb_icccm_get_wm_hints_reply(conn,
6479 xcb_icccm_get_wm_hints(conn, id),
6481 xcb_icccm_get_wm_transient_for_reply(conn,
6482 xcb_icccm_get_wm_transient_for(conn, id),
6485 win->transient = trans;
6486 set_child_transient(win, &trans);
6487 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6488 "transient: 0x%x\n", win->id, win->transient);
6491 prot = get_atom_from_string("WM_PROTOCOLS");
6493 /* get supported protocols */
6494 if (xcb_icccm_get_wm_protocols_reply(conn,
6495 xcb_icccm_get_wm_protocols(conn, id, prot),
6497 for (i = 0; i < wpr.atoms_len; i++) {
6498 if (wpr.atoms[i] == takefocus)
6499 win->take_focus = 1;
6500 if (wpr.atoms[i] == adelete)
6501 win->can_delete = 1;
6503 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6506 win->iconic = get_iconic(win);
6509 * Figure out where to put the window. If it was previously assigned to
6510 * a workspace (either by spawn() or manually moving), and isn't
6511 * transient, * put it in the same workspace
6513 r = root_to_region(win->wa->root);
6515 ws = &r->s->ws[p->ws];
6516 TAILQ_REMOVE(&pidlist, p, entry);
6519 } else if (prop && win->transient == 0) {
6520 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6521 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6524 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6527 ws = &r->s->ws[ws_idx];
6530 /* this should launch transients in the same ws as parent */
6532 if ((ww = find_window(trans)) != NULL)
6538 warnx("manage_window: fix this "
6544 /* set up the window layout */
6547 win->s = r->s; /* this never changes */
6548 if (trans && (ww = find_window(trans)))
6549 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6550 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6551 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6553 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6554 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6555 else switch (spawn_position) {
6558 case SWM_STACK_ABOVE:
6559 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6561 case SWM_STACK_BOTTOM:
6562 case SWM_STACK_BELOW:
6563 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6566 /* ignore window border if there is one. */
6567 WIDTH(win) = win->wa->width;
6568 HEIGHT(win) = win->wa->height;
6569 X(win) = win->wa->x + win->wa->border_width;
6570 Y(win) = win->wa->y + win->wa->border_width;
6572 win->g_floatvalid = 0;
6573 win->floatmaxed = 0;
6574 win->ewmh_flags = 0;
6576 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6577 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6578 HEIGHT(win), ws->idx);
6580 constrain_window(win, r, 0);
6582 /* Set window properties so we can remember this after reincarnation */
6583 if (ws_idx_atom && prop == NULL &&
6584 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6586 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6588 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6589 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6595 ewmh_autoquirk(win);
6597 if (xcb_icccm_get_wm_class_reply(conn,
6598 xcb_icccm_get_wm_class(conn, win->id),
6600 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6601 win->ch.class_name, win->ch.instance_name);
6603 /* java is retarded so treat it special */
6604 if (strstr(win->ch.instance_name, "sun-awt")) {
6609 TAILQ_FOREACH(qp, &quirks, entry) {
6610 if (!strcmp(win->ch.class_name, qp->class) &&
6611 !strcmp(win->ch.instance_name, qp->name)) {
6612 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6613 "class: %s, name: %s\n", win->ch.class_name,
6614 win->ch.instance_name);
6615 if (qp->quirk & SWM_Q_FLOAT) {
6619 win->quirks = qp->quirk;
6624 /* alter window position if quirky */
6625 if (win->quirks & SWM_Q_ANYWHERE) {
6626 win->manual = 1; /* don't center the quirky windows */
6627 if (bar_enabled && Y(win) < bar_height)
6628 Y(win) = bar_height;
6629 if (WIDTH(win) + X(win) > WIDTH(r))
6630 X(win) = WIDTH(r) - WIDTH(win) - 2;
6634 /* Reset font sizes (the bruteforce way; no default keybinding). */
6635 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6636 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6637 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6638 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6639 fake_keypress(win, XK_KP_Add, ShiftMask);
6642 ewmh_get_win_state(win);
6643 ewmh_update_actions(win);
6644 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6649 X(win) -= border_width;
6650 Y(win) -= border_width;
6654 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6655 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6657 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6659 /* floaters need to be mapped if they are in the current workspace */
6660 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6661 map_window_raised(win->id);
6667 free_window(struct ws_win *win)
6669 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6674 /* needed for restart wm */
6675 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
6677 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6682 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6687 memset(win, 0xff, sizeof *win); /* XXX kill later */
6693 unmanage_window(struct ws_win *win)
6695 struct ws_win *parent;
6696 xcb_screen_t *screen;
6701 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6703 if (win->transient) {
6704 parent = find_window(win->transient);
6706 parent->child_trans = NULL;
6709 /* focus on root just in case */
6710 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6711 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6712 screen->root, XCB_CURRENT_TIME);
6716 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6717 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6721 focus_magic(struct ws_win *win)
6723 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6726 /* if there are no windows clear the status-bar */
6731 if (win->child_trans) {
6732 /* win = parent & has a transient so focus on that */
6734 focus_win(win->child_trans);
6735 if (win->child_trans->take_focus)
6736 client_msg(win, takefocus);
6738 /* make sure transient hasn't disappeared */
6739 if (validate_win(win->child_trans) == 0) {
6740 focus_win(win->child_trans);
6741 if (win->child_trans->take_focus)
6742 client_msg(win->child_trans, takefocus);
6744 win->child_trans = NULL;
6746 if (win->take_focus)
6747 client_msg(win, takefocus);
6753 if (win->take_focus)
6754 client_msg(win, takefocus);
6759 expose(xcb_expose_event_t *e)
6761 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6765 keypress(xcb_key_press_event_t *e)
6769 struct swm_region *r;
6771 keysym = XkbKeycodeToKeysym(display, (KeyCode)e->detail, 0, 0);
6773 DNPRINTF(SWM_D_EVENT, "keypress: %u\n", e->detail);
6775 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6777 if (keyfuncs[kp->funcid].func == NULL)
6780 r = root_to_region(e->root);
6781 if (kp->funcid == kf_spawn_custom)
6782 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6784 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6788 buttonpress(xcb_button_press_event_t *e)
6793 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x\n", e->event);
6795 if ((win = find_window(e->event)) == NULL)
6799 action = client_click;
6801 for (i = 0; i < LENGTH(buttons); i++)
6802 if (action == buttons[i].action && buttons[i].func &&
6803 buttons[i].button == e->detail &&
6804 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6805 buttons[i].func(win, &buttons[i].args);
6809 configurerequest(xcb_configure_request_event_t *e)
6814 uint32_t wc[7] = {0};
6816 if ((win = find_window(e->window)) == NULL)
6817 if ((win = find_unmanaged_window(e->window)) == NULL)
6821 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6822 mask |= XCB_CONFIG_WINDOW_X;
6825 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6826 mask |= XCB_CONFIG_WINDOW_Y;
6829 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6830 mask |= XCB_CONFIG_WINDOW_WIDTH;
6833 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6834 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6835 wc[i++] = e->height;
6837 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6838 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6839 wc[i++] = e->border_width;
6841 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6842 mask |= XCB_CONFIG_WINDOW_SIBLING;
6843 wc[i++] = e->sibling;
6845 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6846 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6847 wc[i++] = e->stack_mode;
6850 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6851 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e->window,
6852 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6854 xcb_configure_window(conn, e->window, mask, wc);
6855 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6856 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6857 win->g_float.x = e->x - X(win->ws->r);
6858 win->g_float.y = e->y - Y(win->ws->r);
6859 win->g_float.w = e->width;
6860 win->g_float.h = e->height;
6861 win->g_floatvalid = 1;
6863 if (win->floating) {
6864 win->g = win->g_float;
6865 win->g.x += X(win->ws->r);
6866 win->g.y += Y(win->ws->r);
6877 configurenotify(xcb_configure_notify_event_t *e)
6881 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6884 win = find_window(e->window);
6886 xcb_icccm_get_wm_normal_hints_reply(conn,
6887 xcb_icccm_get_wm_normal_hints(conn, win->id),
6896 destroynotify(xcb_destroy_notify_event_t *e)
6900 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6902 if ((win = find_window(e->window)) == NULL) {
6903 if ((win = find_unmanaged_window(e->window)) == NULL)
6909 /* make sure we focus on something */
6912 unmanage_window(win);
6918 enternotify(xcb_enter_notify_event_t *e)
6923 Window focus_return;
6924 int revert_to_return;
6926 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6927 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6928 "state: %d\n", e->event, e->mode, e->detail, e->root,
6929 e->child, YESNO(e->same_screen_focus), e->state);
6931 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6932 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6937 switch (focus_mode) {
6938 case SWM_FOCUS_DEFAULT:
6940 case SWM_FOCUS_FOLLOW:
6942 case SWM_FOCUS_SYNERGY:
6945 * all these checks need to be in this order because the
6946 * XCheckTypedWindowEvent relies on weeding out the previous events
6948 * making this code an option would enable a follow mouse for focus
6953 * state is set when we are switching workspaces and focus is set when
6954 * the window or a subwindow already has focus (occurs during restart).
6956 * Only honor the focus flag if last_focus_event is not FocusOut,
6957 * this allows spectrwm to continue to control focus when another
6958 * program is also playing with it.
6960 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6961 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6966 * happens when a window is created or destroyed and the border
6967 * crosses the mouse pointer and when switching ws
6969 * we need the subwindow test to see if we came from root in order
6970 * to give focus to floaters
6972 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6973 ev->subwindow == 0) {
6974 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6978 /* this window already has focus */
6979 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6980 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6984 /* this window is being deleted or moved to another ws */
6985 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6987 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6988 XPutBackEvent(display, &cne);
6992 if ((win = find_window(ev->window)) == NULL) {
6993 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6998 * In fullstack kill all enters unless they come from a different ws
6999 * (i.e. another region) or focus has been grabbed externally.
7001 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
7002 last_focus_event != FocusOut) {
7003 XGetInputFocus(display, &focus_return, &revert_to_return);
7004 if ((w = find_window(focus_return)) == NULL ||
7006 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
7014 if ((win = find_window(e->event)) == NULL) {
7015 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7022 /* lets us use one switch statement for arbitrary mode/detail combinations */
7023 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7026 mapnotify(xcb_map_notify_event_t *e)
7030 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7032 win = manage_window(e->window);
7034 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7037 * focus_win can only set input focus on a mapped window.
7038 * make sure the window really has focus since it is just being mapped.
7040 if (win->ws->focus == win)
7045 mappingnotify(xcb_mapping_notify_event_t *e)
7047 xcb_refresh_keyboard_mapping(syms, e);
7049 if (e->request == XCB_MAPPING_KEYBOARD)
7054 maprequest(xcb_map_request_event_t *e)
7057 struct swm_region *r;
7058 xcb_get_window_attributes_reply_t *war;
7060 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7063 war = xcb_get_window_attributes_reply(conn,
7064 xcb_get_window_attributes(conn, e->window),
7068 if (war->override_redirect) {
7074 win = manage_window(e->window);
7076 return; /* can't happen */
7081 /* make new win focused */
7082 r = root_to_region(win->wa->root);
7083 if (win->ws == r->ws)
7088 propertynotify(xcb_property_notify_event_t *e)
7094 xcb_get_atom_name_reply_t *r;
7096 r = xcb_get_atom_name_reply(conn,
7097 xcb_get_atom_name(conn, e->atom),
7100 len = xcb_get_atom_name_name_length(r);
7102 name = malloc(len + 1);
7104 memcpy(name, xcb_get_atom_name_name(r), len);
7107 DNPRINTF(SWM_D_EVENT,
7108 "propertynotify: window: 0x%x, atom: %s\n",
7117 win = find_window(e->window);
7121 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7122 update_iconic(win, 0);
7123 map_window_raised(win->id);
7131 case XCB_ATOM_WM_NORMAL_HINTS:
7132 xcb_icccm_get_wm_normal_hints(conn,
7133 xcb_iccom_get_wm_normal_hints(conn, win->id),
7135 warnx("normal hints: flag 0x%x", win->sh.flags);
7136 if (win->sh.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
7137 WIDTH(win) = win->sh.min_width;
7138 HEIGHT(win) = win->sh.min_height;
7139 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7141 XMoveResizeWindow(display, win->id,
7142 X(win), Y(win), WIDTH(win), HEIGHT(win));
7144 case XCB_ATOM_WM_CLASS:
7145 case XCB_ATOM_WM_NAME:
7154 unmapnotify(xcb_unmap_notify_event_t *e)
7158 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7160 /* determine if we need to help unmanage this window */
7161 win = find_window(e->window);
7165 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7166 unmanage_window(win);
7169 /* resend unmap because we ated it */
7170 xcb_unmap_window(conn, e->window);
7175 visibilitynotify(xcb_visibility_notify_event_t *e)
7178 struct swm_region *r;
7180 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7183 if (e->state == XCB_VISIBILITY_UNOBSCURED) {
7184 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7185 for (i = 0; i < num_screens; i++)
7186 TAILQ_FOREACH(r, &screens[i].rl, entry)
7187 if (e->window == WINID(r->bar))
7193 clientmessage(xcb_client_message_event_t *e)
7197 win = find_window(e->window);
7202 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, type: %u\n",
7203 e->window, e->response_type);
7205 if (e->response_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7206 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7209 if (e->response_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7210 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7211 if (win->can_delete)
7212 client_msg(win, adelete);
7214 xcb_kill_client(conn, win->id);
7216 if (e->response_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7217 DNPRINTF(SWM_D_EVENT,
7218 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7219 if (win->floating) {
7220 if (e->data.data32[0] & (1<<8)) /* x */
7221 X(win) = e->data.data32[1];
7222 if (e->data.data32[0] & (1<<9)) /* y */
7223 Y(win) = e->data.data32[2];
7224 if (e->data.data32[0] & (1<<10)) /* width */
7225 WIDTH(win) = e->data.data32[3];
7226 if (e->data.data32[0] & (1<<11)) /* height */
7227 HEIGHT(win) = e->data.data32[4];
7232 /* TODO: Change stack sizes */
7233 /* notify no change was made. */
7234 config_win(win, NULL);
7237 if (e->response_type == ewmh[_NET_WM_STATE].atom) {
7238 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7239 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7240 if (e->data.data32[2])
7241 ewmh_update_win_state(win, e->data.data32[2],
7249 xerror_start(Display *d, XErrorEvent *ee)
7256 xerror(Display *d, XErrorEvent *ee)
7258 /* warnx("error: %p %p", display, ee); */
7266 xerrorxlib = XSetErrorHandler(xerror_start);
7268 /* this causes an error if some other window manager is running */
7269 XSelectInput(display, DefaultRootWindow(display),
7270 SubstructureRedirectMask);
7275 XSetErrorHandler(xerror);
7281 new_region(struct swm_screen *s, int x, int y, int w, int h)
7283 struct swm_region *r, *n;
7284 struct workspace *ws = NULL;
7287 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7288 s->idx, w, h, x, y);
7290 /* remove any conflicting regions */
7291 n = TAILQ_FIRST(&s->rl);
7294 n = TAILQ_NEXT(r, entry);
7295 if (X(r) < (x + w) &&
7296 (X(r) + WIDTH(r)) > x &&
7298 (Y(r) + HEIGHT(r)) > y) {
7299 if (r->ws->r != NULL)
7300 r->ws->old_r = r->ws->r;
7303 TAILQ_REMOVE(&s->rl, r, entry);
7304 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7308 /* search old regions for one to reuse */
7310 /* size + location match */
7311 TAILQ_FOREACH(r, &s->orl, entry)
7312 if (X(r) == x && Y(r) == y &&
7313 HEIGHT(r) == h && WIDTH(r) == w)
7317 TAILQ_FOREACH(r, &s->orl, entry)
7318 if (HEIGHT(r) == h && WIDTH(r) == w)
7322 TAILQ_REMOVE(&s->orl, r, entry);
7323 /* try to use old region's workspace */
7324 if (r->ws->r == NULL)
7327 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7328 err(1, "new_region: calloc: failed to allocate memory "
7331 /* if we don't have a workspace already, find one */
7333 for (i = 0; i < workspace_limit; i++)
7334 if (s->ws[i].r == NULL) {
7341 errx(1, "new_region: no free workspaces");
7352 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7358 #ifdef SWM_XRR_HAS_CRTC
7361 #endif /* SWM_XRR_HAS_CRTC */
7362 struct swm_region *r;
7364 xcb_randr_get_screen_resources_current_cookie_t src;
7365 xcb_randr_get_screen_resources_current_reply_t *srr;
7366 xcb_randr_get_crtc_info_cookie_t cic;
7367 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7368 xcb_randr_crtc_t *crtc;
7369 xcb_screen_t *screen = get_screen(i);
7371 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7372 if (i >= num_screens)
7373 errx(1, "scan_xrandr: invalid screen");
7375 /* remove any old regions */
7376 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7377 r->ws->old_r = r->ws->r = NULL;
7379 TAILQ_REMOVE(&screens[i].rl, r, entry);
7380 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7384 /* map virtual screens onto physical screens */
7385 #ifdef SWM_XRR_HAS_CRTC
7386 if (xrandr_support) {
7387 src = xcb_randr_get_screen_resources_current(conn,
7389 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7392 new_region(&screens[i], 0, 0,
7393 screen->width_in_pixels,
7394 screen->height_in_pixels);
7397 ncrtc = srr->num_crtcs;
7398 for (c = 0; c < ncrtc; c++) {
7399 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7400 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7402 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7405 if (cir->num_outputs == 0) {
7411 new_region(&screens[i], 0, 0,
7412 screen->width_in_pixels,
7413 screen->height_in_pixels);
7415 new_region(&screens[i],
7416 cir->x, cir->y, cir->width, cir->height);
7421 #endif /* SWM_XRR_HAS_CRTC */
7423 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7424 screen->height_in_pixels);
7429 screenchange(xcb_randr_screen_change_notify_event_t *e)
7431 struct swm_region *r;
7434 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7436 if (e->rotation & (XCB_RANDR_ROTATION_ROTATE_90
7437 | XCB_RANDR_ROTATION_ROTATE_270))
7438 xcb_randr_set_screen_size(conn, e->root, e->height,
7439 e->width, e->mheight, e->mwidth);
7441 xcb_randr_set_screen_size(conn, e->root, e->width,
7442 e->height, e->mwidth, e->mheight);
7444 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7445 /* silly event doesn't include the screen index */
7446 for (i = 0; i < num_screens; i++)
7447 if (screens[i].root == e->root)
7449 if (i >= num_screens)
7450 errx(1, "screenchange: screen not found");
7452 /* brute force for now, just re-enumerate the regions */
7455 /* add bars to all regions */
7456 for (i = 0; i < num_screens; i++)
7457 TAILQ_FOREACH(r, &screens[i].rl, entry)
7465 xcb_window_t *wins = NULL;
7467 int i, j, num_screens;
7468 uint16_t state, manage;
7470 xcb_query_tree_cookie_t qtc;
7471 xcb_query_tree_reply_t *qtr;
7472 xcb_get_window_attributes_cookie_t c;
7473 xcb_get_window_attributes_reply_t *r;
7474 xcb_get_property_cookie_t pc;
7476 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7477 for (i = 0; i < num_screens; i++) {
7478 qtc = xcb_query_tree(conn, screens[i].root);
7479 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7482 wins = xcb_query_tree_children(qtr);
7483 no = xcb_query_tree_children_length(qtr);
7484 /* attach windows to a region */
7485 /* normal windows */
7486 for (j = 0; j < no; j++) {
7487 c = xcb_get_window_attributes(conn, wins[j]);
7488 r = xcb_get_window_attributes_reply(conn, c, NULL);
7491 if (r->override_redirect) {
7496 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7497 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7503 state = getstate(wins[j]);
7504 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7505 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7506 manage_window(wins[j]);
7509 /* transient windows */
7510 for (j = 0; j < no; j++) {
7511 c = xcb_get_window_attributes(conn, wins[j]);
7512 r = xcb_get_window_attributes_reply(conn, c, NULL);
7515 if (r->override_redirect) {
7521 state = getstate(wins[j]);
7522 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7523 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7524 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7525 &wins[j], NULL) && manage)
7526 manage_window(wins[j]);
7535 int i, j, k, num_screens;
7536 struct workspace *ws;
7537 uint32_t gcv[1], wa[1];
7538 const xcb_query_extension_reply_t *qep;
7539 xcb_cursor_t cursor;
7540 xcb_font_t cursor_font;
7541 xcb_randr_query_version_cookie_t c;
7542 xcb_randr_query_version_reply_t *r;
7544 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7545 if ((screens = calloc(num_screens,
7546 sizeof(struct swm_screen))) == NULL)
7547 err(1, "setup_screens: calloc: failed to allocate memory for "
7550 /* initial Xrandr setup */
7551 xrandr_support = False;
7552 c = xcb_randr_query_version(conn, True, True);
7553 r = xcb_randr_query_version_reply(conn, c, NULL);
7555 if (r->major_version >= 1)
7556 xrandr_support = True;
7559 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7560 xrandr_eventbase = qep->first_event;
7562 cursor_font = xcb_generate_id(conn);
7563 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7565 cursor = xcb_generate_id(conn);
7566 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7567 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7570 /* map physical screens */
7571 for (i = 0; i < num_screens; i++) {
7572 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7574 TAILQ_INIT(&screens[i].rl);
7575 TAILQ_INIT(&screens[i].orl);
7576 screens[i].root = RootWindow(display, i);
7578 /* set default colors */
7579 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7580 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7581 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7582 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7583 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7585 /* create graphics context on screen */
7586 screens[i].bar_gc = xcb_generate_id(conn);
7588 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7589 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7591 /* set default cursor */
7592 xcb_change_window_attributes(conn, screens[i].root,
7595 /* init all workspaces */
7596 /* XXX these should be dynamically allocated too */
7597 for (j = 0; j < SWM_WS_MAX; j++) {
7598 ws = &screens[i].ws[j];
7604 TAILQ_INIT(&ws->winlist);
7605 TAILQ_INIT(&ws->unmanagedlist);
7607 for (k = 0; layouts[k].l_stack != NULL; k++)
7608 if (layouts[k].l_config != NULL)
7609 layouts[k].l_config(ws,
7610 SWM_ARG_ID_STACKINIT);
7611 ws->cur_layout = &layouts[0];
7612 ws->cur_layout->l_string(ws);
7618 xcb_randr_select_input(conn, screens[i].root,
7619 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7621 xcb_free_cursor(conn, cursor);
7622 xcb_close_font(conn, cursor_font);
7628 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7629 err(1, "setup_globals: strdup: failed to allocate memory.");
7631 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7632 err(1, "setup_globals: strdup: failed to allocate memory.");
7639 xcb_atom_t netwmcheck, netwmname, utf8_string;
7640 xcb_window_t root, win;
7643 /* work around sun jdk bugs, code from wmname */
7644 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7645 netwmname = get_atom_from_string("_NET_WM_NAME");
7646 utf8_string = get_atom_from_string("UTF8_STRING");
7648 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7649 for (i = 0; i < num_screens; i++) {
7650 root = screens[i].root;
7652 win = xcb_generate_id(conn);
7653 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7654 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7655 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7656 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7657 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7659 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7660 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7661 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7662 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7663 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7664 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7669 event_error(xcb_generic_error_t *e)
7673 switch (e->error_code) {
7678 estr = "BadRequest";
7702 estr = "BadDrawable";
7717 estr = "BadIdChoice";
7725 case XCB_IMPLEMENTATION:
7726 estr = "BadImplementation";
7733 DNPRINTF(SWM_D_EVENT, "event_error: %s: response_type:%u error_code:%u "
7734 "sequence:%u resource_id:%u minor_code:%u major_code:%u\n", estr,
7735 e->response_type, e->error_code, e->sequence, e->resource_id,
7736 e->minor_code, e->major_code
7741 event_handle(xcb_generic_event_t *evt)
7743 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7745 #define EVENT(type, callback) case type: callback((void *)evt); return
7746 EVENT(0, event_error);
7747 EVENT(XCB_BUTTON_PRESS, buttonpress);
7748 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7749 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7750 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7751 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7752 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7753 EVENT(XCB_ENTER_NOTIFY, enternotify);
7754 EVENT(XCB_EXPOSE, expose);
7755 EVENT(XCB_KEY_PRESS, keypress);
7756 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7757 EVENT(XCB_MAP_NOTIFY, mapnotify);
7758 EVENT(XCB_MAP_REQUEST, maprequest);
7759 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7760 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7761 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7762 EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);
7765 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7766 screenchange((void *)evt);
7770 main(int argc, char *argv[])
7772 struct swm_region *r, *rr;
7773 struct ws_win *winfocus = NULL;
7776 char conf[PATH_MAX], *cfile = NULL;
7778 int xfd, i, num_screens;
7780 struct sigaction sact;
7781 xcb_generic_event_t *evt;
7784 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7785 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7787 warnx("no locale support");
7789 if (!X_HAVE_UTF8_STRING)
7790 warnx("no UTF-8 support");
7792 if (!(display = XOpenDisplay(0)))
7793 errx(1, "can not open display");
7795 /* handle some signals */
7796 bzero(&sact, sizeof(sact));
7797 sigemptyset(&sact.sa_mask);
7799 sact.sa_handler = sighdlr;
7800 sigaction(SIGINT, &sact, NULL);
7801 sigaction(SIGQUIT, &sact, NULL);
7802 sigaction(SIGTERM, &sact, NULL);
7803 sigaction(SIGHUP, &sact, NULL);
7805 sact.sa_handler = sighdlr;
7806 sact.sa_flags = SA_NOCLDSTOP;
7807 sigaction(SIGCHLD, &sact, NULL);
7809 conn = XGetXCBConnection(display);
7810 if (xcb_connection_has_error(conn))
7811 errx(1, "can not get XCB connection");
7813 xfd = xcb_get_file_descriptor(conn);
7814 syms = xcb_key_symbols_alloc(conn);
7816 errx(1, "unable to allocate key symbols");
7818 astate = get_atom_from_string("WM_STATE");
7819 aprot = get_atom_from_string("WM_PROTOCOLS");
7820 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7821 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7822 a_wmname = get_atom_from_string("WM_NAME");
7823 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7824 a_utf8_string = get_atom_from_string("UTF8_STRING");
7825 a_string = get_atom_from_string("STRING");
7826 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7828 /* look for local and global conf file */
7829 pwd = getpwuid(getuid());
7831 errx(1, "invalid user: %d", getuid());
7833 xcb_grab_server(conn);
7836 /* flush all events */
7837 while ((evt = xcb_poll_for_event(conn))) {
7838 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7845 errx(1, "other wm running");
7856 for (i = 0; ; i++) {
7861 snprintf(conf, sizeof conf, "%s/.%s",
7862 pwd->pw_dir, SWM_CONF_FILE);
7866 snprintf(conf, sizeof conf, "/etc/%s",
7871 snprintf(conf, sizeof conf, "%s/.%s",
7872 pwd->pw_dir, SWM_CONF_FILE_OLD);
7876 snprintf(conf, sizeof conf, "/etc/%s",
7883 if (strlen(conf) && stat(conf, &sb) != -1)
7884 if (S_ISREG(sb.st_mode)) {
7891 /* load conf (if any) */
7893 conf_load(cfile, SWM_CONF_DEFAULT);
7896 /* set some values to work around bad programs */
7898 /* grab existing windows (before we build the bars) */
7901 if (getenv("SWM_STARTED") == NULL)
7902 setenv("SWM_STARTED", "YES", 1);
7904 /* setup all bars */
7905 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7906 for (i = 0; i < num_screens; i++)
7907 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7908 if (winfocus == NULL)
7909 winfocus = TAILQ_FIRST(&r->ws->winlist);
7918 xcb_ungrab_server(conn);
7922 while ((evt = xcb_poll_for_event(conn))) {
7923 DNPRINTF(SWM_D_EVENT, "XCB Event: %s\n",
7931 /* if we are being restarted go focus on first window */
7933 rr = winfocus->ws->r;
7935 /* not a visible window */
7939 /* move pointer to first screen if multi screen */
7940 if (num_screens > 1 || outputs > 1)
7941 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7942 rr->s[0].root, 0, 0, 0, 0, X(rr),
7943 Y(rr) + (bar_enabled ? bar_height : 0));
7945 a.id = SWM_ARG_ID_FOCUSCUR;
7955 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7957 DNPRINTF(SWM_D_MISC, "select failed");
7958 if (restart_wm == 1)
7959 restart(NULL, NULL);
7960 if (search_resp == 1)
7973 for (i = 0; i < num_screens; ++i)
7974 if (screens[i].bar_gc != 0)
7975 xcb_free_gc(conn, screens[i].bar_gc);
7976 XFreeFontSet(display, bar_fs);
7978 xcb_key_symbols_free(syms);
7979 xcb_disconnect(conn);