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_icccm.h>
97 #include <xcb/xcb_keysyms.h>
98 #include <X11/Xproto.h>
99 #include <X11/Xutil.h>
100 #include <X11/extensions/Xrandr.h>
101 #include <X11/extensions/XTest.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr = SPECTRWM_BUILDSTR;
112 static const char *buildstr = SPECTRWM_VERSION;
116 # error XRandR versions less than 1.0 are not supported
121 #define SWM_XRR_HAS_CRTC
125 #if defined(__OpenBSD__)
126 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
128 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
129 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
130 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
131 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
132 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
133 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
134 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
135 #define xcb_icccm_get_wm_class xcb_get_wm_class
136 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
137 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
138 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
139 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
140 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
141 #define xcb_icccm_get_wm_name xcb_get_wm_name
142 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
143 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
144 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
145 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
146 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
147 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
148 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
149 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
150 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
151 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
154 /*#define SWM_DEBUG*/
156 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
157 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
158 #define SWM_D_MISC 0x0001
159 #define SWM_D_EVENT 0x0002
160 #define SWM_D_WS 0x0004
161 #define SWM_D_FOCUS 0x0008
162 #define SWM_D_MOVE 0x0010
163 #define SWM_D_STACK 0x0020
164 #define SWM_D_MOUSE 0x0040
165 #define SWM_D_PROP 0x0080
166 #define SWM_D_CLASS 0x0100
167 #define SWM_D_KEY 0x0200
168 #define SWM_D_QUIRK 0x0400
169 #define SWM_D_SPAWN 0x0800
170 #define SWM_D_EVENTQ 0x1000
171 #define SWM_D_CONF 0x2000
172 #define SWM_D_BAR 0x4000
174 u_int32_t swm_debug = 0
192 #define DPRINTF(x...)
193 #define DNPRINTF(n,x...)
196 #define LENGTH(x) (sizeof x / sizeof x[0])
197 #define MODKEY Mod1Mask
198 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
199 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
200 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
201 #define SWM_PROPLEN (16)
202 #define SWM_FUNCNAME_LEN (32)
203 #define SWM_KEYS_LEN (255)
204 #define SWM_QUIRK_LEN (64)
205 #define X(r) (r)->g.x
206 #define Y(r) (r)->g.y
207 #define WIDTH(r) (r)->g.w
208 #define HEIGHT(r) (r)->g.h
209 #define BORDER(w) (w->bordered ? border_width : 0)
210 #define MAX_X(r) ((r)->g.x + (r)->g.w)
211 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
212 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
213 #define SH_MIN_W(w) (w)->sh.min_width
214 #define SH_MIN_H(w) (w)->sh.min_height
215 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
216 #define SH_MAX_W(w) (w)->sh.max_width
217 #define SH_MAX_H(w) (w)->sh.max_height
218 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
219 #define SH_INC_W(w) (w)->sh.width_inc
220 #define SH_INC_H(w) (w)->sh.height_inc
221 #define SWM_MAX_FONT_STEPS (3)
222 #define WINID(w) ((w) ? (w)->id : 0)
223 #define YESNO(x) ((x) ? "yes" : "no")
225 #define SWM_FOCUS_DEFAULT (0)
226 #define SWM_FOCUS_SYNERGY (1)
227 #define SWM_FOCUS_FOLLOW (2)
229 #define SWM_CONF_DEFAULT (0)
230 #define SWM_CONF_KEYMAPPING (1)
233 #define SWM_LIB "/usr/local/lib/libswmhack.so"
240 xcb_atom_t takefocus;
242 xcb_atom_t a_netwmname;
243 xcb_atom_t a_utf8_string;
245 xcb_atom_t a_swm_iconic;
246 volatile sig_atomic_t running = 1;
247 volatile sig_atomic_t restart_wm = 0;
249 int last_focus_event = FocusOut;
250 int (*xerrorxlib)(Display *, XErrorEvent *);
254 int xrandr_eventbase;
255 unsigned int numlockmask = 0;
257 xcb_connection_t *conn;
258 xcb_key_symbols_t *syms;
261 int cycle_visible = 0;
263 int font_adjusted = 0;
264 unsigned int mod_key = MODKEY;
267 struct swm_region *search_r;
268 int select_list_pipe[2];
269 int select_resp_pipe[2];
271 volatile sig_atomic_t search_resp;
272 int search_resp_action;
274 struct search_window {
275 TAILQ_ENTRY(search_window) entry;
279 xcb_window_t indicator;
281 TAILQ_HEAD(search_winlist, search_window);
282 struct search_winlist search_wl;
287 SWM_SEARCH_UNICONIFY,
288 SWM_SEARCH_NAME_WORKSPACE,
289 SWM_SEARCH_SEARCH_WORKSPACE,
290 SWM_SEARCH_SEARCH_WINDOW
293 #define SWM_STACK_TOP (0)
294 #define SWM_STACK_BOTTOM (1)
295 #define SWM_STACK_ABOVE (2)
296 #define SWM_STACK_BELOW (3)
299 double dialog_ratio = 0.6;
301 #define SWM_BAR_MAX (256)
302 #define SWM_BAR_JUSTIFY_LEFT (0)
303 #define SWM_BAR_JUSTIFY_CENTER (1)
304 #define SWM_BAR_JUSTIFY_RIGHT (2)
305 #define SWM_BAR_OFFSET (4)
306 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
307 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
308 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
309 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
310 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
312 char *bar_argv[] = { NULL, NULL };
314 unsigned char bar_ext[SWM_BAR_MAX];
315 char bar_vertext[SWM_BAR_MAX];
317 sig_atomic_t bar_alarm = 0;
320 int bar_border_width = 1;
321 int bar_at_bottom = 0;
323 int bar_extra_running = 0;
326 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
327 char *bar_format = NULL;
328 int stack_enabled = 1;
329 int clock_enabled = 1;
330 int urgent_enabled = 0;
331 char *clock_format = NULL;
332 int title_name_enabled = 0;
333 int title_class_enabled = 0;
334 int window_name_enabled = 0;
335 int focus_mode = SWM_FOCUS_DEFAULT;
336 int focus_close = SWM_STACK_BELOW;
337 int focus_close_wrap = 1;
338 int focus_default = SWM_STACK_TOP;
339 int spawn_position = SWM_STACK_TOP;
340 int disable_border = 0;
341 int border_width = 1;
342 int verbose_layout = 0;
345 XFontSetExtents *bar_fs_extents;
349 #define SWM_MENU_FN (2)
350 #define SWM_MENU_NB (4)
351 #define SWM_MENU_NF (6)
352 #define SWM_MENU_SB (8)
353 #define SWM_MENU_SF (10)
355 /* layout manager data */
356 struct swm_geometry {
369 struct swm_geometry g;
372 /* virtual "screens" */
374 TAILQ_ENTRY(swm_region) entry;
375 struct swm_geometry g;
376 struct workspace *ws; /* current workspace on this region */
377 struct workspace *ws_prior; /* prior workspace on this region */
378 struct swm_screen *s; /* screen idx */
381 TAILQ_HEAD(swm_region_list, swm_region);
384 TAILQ_ENTRY(ws_win) entry;
386 xcb_window_t transient;
387 struct ws_win *child_trans; /* transient child window */
388 struct swm_geometry g; /* current geometry */
389 struct swm_geometry g_float; /* region coordinates */
390 int g_floatvalid; /* g_float geometry validity */
391 int floatmaxed; /* whether maxed by max_stack */
396 unsigned int ewmh_flags;
397 int font_size_boundary[SWM_MAX_FONT_STEPS];
403 unsigned long quirks;
404 struct workspace *ws; /* always valid */
405 struct swm_screen *s; /* always valid, never changes */
406 xcb_get_geometry_reply_t *wa;
408 xcb_icccm_get_wm_class_reply_t ch;
409 xcb_icccm_wm_hints_t hints;
411 TAILQ_HEAD(ws_win_list, ws_win);
415 TAILQ_ENTRY(pid_e) entry;
419 TAILQ_HEAD(pid_list, pid_e);
420 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
422 /* layout handlers */
424 void vertical_config(struct workspace *, int);
425 void vertical_stack(struct workspace *, struct swm_geometry *);
426 void horizontal_config(struct workspace *, int);
427 void horizontal_stack(struct workspace *, struct swm_geometry *);
428 void max_stack(struct workspace *, struct swm_geometry *);
429 void plain_stacker(struct workspace *);
430 void fancy_stacker(struct workspace *);
432 struct ws_win *find_window(xcb_window_t);
434 void grabbuttons(struct ws_win *, int);
435 void new_region(struct swm_screen *, int, int, int, int);
436 void unmanage_window(struct ws_win *);
437 uint16_t getstate(xcb_window_t);
439 int conf_load(char *, int);
442 void (*l_stack)(struct workspace *, struct swm_geometry *);
443 void (*l_config)(struct workspace *, int);
445 #define SWM_L_FOCUSPREV (1<<0)
446 #define SWM_L_MAPONFOCUS (1<<1)
447 void (*l_string)(struct workspace *);
449 /* stack, configure */
450 { vertical_stack, vertical_config, 0, plain_stacker },
451 { horizontal_stack, horizontal_config, 0, plain_stacker },
453 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
454 { NULL, NULL, 0, NULL },
457 /* position of max_stack mode in the layouts array, index into layouts! */
458 #define SWM_V_STACK (0)
459 #define SWM_H_STACK (1)
460 #define SWM_MAX_STACK (2)
462 #define SWM_H_SLICE (32)
463 #define SWM_V_SLICE (32)
465 /* define work spaces */
467 int idx; /* workspace index */
468 char *name; /* workspace name */
469 int always_raise; /* raise windows on focus */
470 struct layout *cur_layout; /* current layout handlers */
471 struct ws_win *focus; /* may be NULL */
472 struct ws_win *focus_prev; /* may be NULL */
473 struct swm_region *r; /* may be NULL */
474 struct swm_region *old_r; /* may be NULL */
475 struct ws_win_list winlist; /* list of windows in ws */
476 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
477 char stacker[10]; /* display stacker and layout */
481 int horizontal_msize;
483 int horizontal_stacks;
494 SWM_S_COLOR_BAR_BORDER,
495 SWM_S_COLOR_BAR_FONT,
501 /* physical screen mapping */
502 #define SWM_WS_MAX (22) /* hard limit */
503 int workspace_limit = 10; /* soft limit */
506 int idx; /* screen index */
507 struct swm_region_list rl; /* list of regions on this screen */
508 struct swm_region_list orl; /* list of old regions */
510 struct workspace ws[SWM_WS_MAX];
516 } c[SWM_S_COLOR_MAX];
518 xcb_gcontext_t bar_gc;
520 struct swm_screen *screens;
522 /* args to functions */
525 #define SWM_ARG_ID_FOCUSNEXT (0)
526 #define SWM_ARG_ID_FOCUSPREV (1)
527 #define SWM_ARG_ID_FOCUSMAIN (2)
528 #define SWM_ARG_ID_FOCUSCUR (4)
529 #define SWM_ARG_ID_SWAPNEXT (10)
530 #define SWM_ARG_ID_SWAPPREV (11)
531 #define SWM_ARG_ID_SWAPMAIN (12)
532 #define SWM_ARG_ID_MOVELAST (13)
533 #define SWM_ARG_ID_MASTERSHRINK (20)
534 #define SWM_ARG_ID_MASTERGROW (21)
535 #define SWM_ARG_ID_MASTERADD (22)
536 #define SWM_ARG_ID_MASTERDEL (23)
537 #define SWM_ARG_ID_FLIPLAYOUT (24)
538 #define SWM_ARG_ID_STACKRESET (30)
539 #define SWM_ARG_ID_STACKINIT (31)
540 #define SWM_ARG_ID_CYCLEWS_UP (40)
541 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
542 #define SWM_ARG_ID_CYCLESC_UP (42)
543 #define SWM_ARG_ID_CYCLESC_DOWN (43)
544 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
545 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
546 #define SWM_ARG_ID_STACKINC (50)
547 #define SWM_ARG_ID_STACKDEC (51)
548 #define SWM_ARG_ID_SS_ALL (60)
549 #define SWM_ARG_ID_SS_WINDOW (61)
550 #define SWM_ARG_ID_DONTCENTER (70)
551 #define SWM_ARG_ID_CENTER (71)
552 #define SWM_ARG_ID_KILLWINDOW (80)
553 #define SWM_ARG_ID_DELETEWINDOW (81)
554 #define SWM_ARG_ID_WIDTHGROW (90)
555 #define SWM_ARG_ID_WIDTHSHRINK (91)
556 #define SWM_ARG_ID_HEIGHTGROW (92)
557 #define SWM_ARG_ID_HEIGHTSHRINK (93)
558 #define SWM_ARG_ID_MOVEUP (100)
559 #define SWM_ARG_ID_MOVEDOWN (101)
560 #define SWM_ARG_ID_MOVELEFT (102)
561 #define SWM_ARG_ID_MOVERIGHT (103)
565 void focus(struct swm_region *, union arg *);
566 void focus_magic(struct ws_win *);
570 TAILQ_ENTRY(quirk) entry;
574 #define SWM_Q_FLOAT (1<<0) /* float this window */
575 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
576 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
577 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
578 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
579 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
581 TAILQ_HEAD(quirk_list, quirk);
582 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
585 * Supported EWMH hints should be added to
586 * both the enum and the ewmh array
591 _NET_MOVERESIZE_WINDOW,
592 _NET_WM_ACTION_CLOSE,
593 _NET_WM_ACTION_FULLSCREEN,
595 _NET_WM_ACTION_RESIZE,
596 _NET_WM_ALLOWED_ACTIONS,
599 _NET_WM_STATE_FULLSCREEN,
600 _NET_WM_STATE_HIDDEN,
601 _NET_WM_STATE_MAXIMIZED_HORZ,
602 _NET_WM_STATE_MAXIMIZED_VERT,
603 _NET_WM_STATE_SKIP_PAGER,
604 _NET_WM_STATE_SKIP_TASKBAR,
606 _NET_WM_WINDOW_TYPE_DIALOG,
607 _NET_WM_WINDOW_TYPE_DOCK,
608 _NET_WM_WINDOW_TYPE_NORMAL,
609 _NET_WM_WINDOW_TYPE_SPLASH,
610 _NET_WM_WINDOW_TYPE_TOOLBAR,
611 _NET_WM_WINDOW_TYPE_UTILITY,
612 _SWM_WM_STATE_MANUAL,
619 } ewmh[SWM_EWMH_HINT_MAX] = {
620 /* must be in same order as in the enum */
621 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
622 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
623 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
624 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
625 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
626 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
627 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
628 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
629 {"_NET_WM_STATE", XCB_ATOM_NONE},
630 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
631 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
632 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
637 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
638 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
639 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
644 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
647 void store_float_geom(struct ws_win *, struct swm_region *);
648 int floating_toggle_win(struct ws_win *);
649 void constrain_window(struct ws_win *, struct swm_region *, int);
650 void update_window(struct ws_win *);
651 void spawn_select(struct swm_region *, union arg *, char *, int *);
652 char *get_win_name(xcb_window_t);
653 xcb_atom_t get_atom_from_string(const char *);
654 void map_window_raised(xcb_window_t);
656 xcb_screen_t *get_screen(int);
657 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
660 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
662 unsigned int tmpr, tmpg, tmpb;
664 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
675 get_screen(int screen)
677 xcb_screen_iterator_t i;
679 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
680 for (; i.rem; --screen, xcb_screen_next(&i))
690 xcb_get_input_focus_cookie_t c;
691 xcb_get_input_focus_reply_t *r;
694 c = xcb_get_input_focus(conn);
696 r = xcb_get_input_focus_reply(conn, c, NULL);
702 map_window_raised(xcb_window_t win)
704 uint32_t val = XCB_STACK_MODE_ABOVE;
706 xcb_configure_window(conn, win,
707 XCB_CONFIG_WINDOW_STACK_MODE, &val);
709 xcb_map_window(conn, win);
714 get_atom_from_string(const char *str)
716 xcb_intern_atom_cookie_t c;
717 xcb_intern_atom_reply_t *r;
720 c = xcb_intern_atom(conn, False, strlen(str), str);
721 r = xcb_intern_atom_reply(conn, c, NULL);
729 return (XCB_ATOM_NONE);
733 update_iconic(struct ws_win *win, int newv)
740 iprop = get_atom_from_string("_SWM_ICONIC");
741 if (iprop == XCB_ATOM_NONE)
745 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
746 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
748 xcb_delete_property(conn, win->id, iprop);
752 get_iconic(struct ws_win *win)
756 xcb_get_property_reply_t *pr = NULL;
758 iprop = get_atom_from_string("_SWM_ICONIC");
759 if (iprop == XCB_ATOM_NONE)
762 pr = xcb_get_property_reply(conn,
763 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
768 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
770 v = *((int32_t *)xcb_get_property_value(pr));
781 int i, j, num_screens;
783 sup_list = get_atom_from_string("_NET_SUPPORTED");
785 for (i = 0; i < LENGTH(ewmh); i++)
786 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
788 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
789 for (i = 0; i < num_screens; i++) {
790 /* Support check window will be created by workaround(). */
792 /* Report supported atoms */
793 xcb_delete_property(conn, screens[i].root, sup_list);
794 for (j = 0; j < LENGTH(ewmh); j++)
795 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
796 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
805 xcb_atom_t sup_check, sup_list;
807 xcb_get_property_cookie_t pc;
808 xcb_get_property_reply_t *pr;
810 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
811 sup_list = get_atom_from_string("_NET_SUPPORTED");
812 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
814 for (i = 0; i < num_screens; i++) {
815 /* Get the support check window and destroy it */
816 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
817 XCB_ATOM_WINDOW, 0, 1);
818 pr = xcb_get_property_reply(conn, pc, NULL);
820 id = *((xcb_window_t *)xcb_get_property_value(pr));
822 xcb_destroy_window(conn, id);
823 xcb_delete_property(conn, screens[i].root, sup_check);
824 xcb_delete_property(conn, screens[i].root, sup_list);
832 ewmh_autoquirk(struct ws_win *win)
838 xcb_get_property_cookie_t c;
839 xcb_get_property_reply_t *r;
841 c = xcb_get_property(conn, False, win->id,
842 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
843 r = xcb_get_property_reply(conn, c, NULL);
846 n = xcb_get_property_value_length(r);
848 for (i = 0; i < n; i++) {
849 type = *((xcb_atom_t *)xcb_get_property_value(r));
850 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
852 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
853 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
854 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
856 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
859 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
860 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
862 win->quirks = SWM_Q_FLOAT;
869 #define SWM_EWMH_ACTION_COUNT_MAX (6)
870 #define EWMH_F_FULLSCREEN (1<<0)
871 #define EWMH_F_ABOVE (1<<1)
872 #define EWMH_F_HIDDEN (1<<2)
873 #define EWMH_F_SKIP_PAGER (1<<3)
874 #define EWMH_F_SKIP_TASKBAR (1<<4)
875 #define SWM_F_MANUAL (1<<5)
878 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
886 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
887 "fullscreen %s\n", win->id, YESNO(fs));
890 if (!win->g_floatvalid)
891 store_float_geom(win, win->ws->r);
893 win->g = win->ws->r->g;
896 if (win->g_floatvalid) {
897 /* refloat at last floating relative position */
898 win->g = win->g_float;
899 X(win) += X(win->ws->r);
900 Y(win) += Y(win->ws->r);
908 ewmh_update_actions(struct ws_win *win)
910 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
916 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
919 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
920 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
923 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
924 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1,
928 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
929 #define _NET_WM_STATE_ADD 1 /* add/set property */
930 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
933 ewmh_update_win_state(struct ws_win *win, long state, long action)
935 unsigned int mask = 0;
936 unsigned int changed = 0;
937 unsigned int orig_flags;
942 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
943 mask = EWMH_F_FULLSCREEN;
944 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
946 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
948 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
949 mask = EWMH_F_SKIP_PAGER;
950 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
951 mask = EWMH_F_SKIP_TASKBAR;
954 orig_flags = win->ewmh_flags;
957 case _NET_WM_STATE_REMOVE:
958 win->ewmh_flags &= ~mask;
960 case _NET_WM_STATE_ADD:
961 win->ewmh_flags |= mask;
963 case _NET_WM_STATE_TOGGLE:
964 win->ewmh_flags ^= mask;
968 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
970 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
972 if (!floating_toggle_win(win))
973 win->ewmh_flags = orig_flags; /* revert */
974 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
976 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
977 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
979 if (!ewmh_set_win_fullscreen(win,
980 win->ewmh_flags & EWMH_F_FULLSCREEN))
981 win->ewmh_flags = orig_flags; /* revert */
983 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
985 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
986 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
987 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
988 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
989 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
990 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
991 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
992 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
993 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
994 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
995 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
996 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
997 if (win->ewmh_flags & EWMH_F_ABOVE)
998 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
999 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1000 &ewmh[_NET_WM_STATE_ABOVE].atom);
1001 if (win->ewmh_flags & SWM_F_MANUAL)
1002 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1003 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1004 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1008 ewmh_get_win_state(struct ws_win *win)
1011 xcb_get_property_cookie_t c;
1012 xcb_get_property_reply_t *r;
1018 win->ewmh_flags = 0;
1020 win->ewmh_flags |= EWMH_F_ABOVE;
1022 win->ewmh_flags |= SWM_F_MANUAL;
1024 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1025 XCB_ATOM_ATOM, 0, (~0L));
1026 r = xcb_get_property_reply(conn, c, NULL);
1030 states = xcb_get_property_value(r);
1031 n = xcb_get_property_value_length(r);
1033 for (i = 0; i < n; i++)
1034 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1042 geteventname(XEvent *e)
1051 name = "KeyRelease";
1054 name = "ButtonPress";
1057 name = "ButtonRelease";
1060 name = "MotionNotify";
1063 name = "EnterNotify";
1066 name = "LeaveNotify";
1075 name = "KeymapNotify";
1080 case GraphicsExpose:
1081 name = "GraphicsExpose";
1086 case VisibilityNotify:
1087 name = "VisibilityNotify";
1090 name = "CreateNotify";
1093 name = "DestroyNotify";
1096 name = "UnmapNotify";
1102 name = "MapRequest";
1104 case ReparentNotify:
1105 name = "ReparentNotify";
1107 case ConfigureNotify:
1108 name = "ConfigureNotify";
1110 case ConfigureRequest:
1111 name = "ConfigureRequest";
1114 name = "GravityNotify";
1117 name = "ResizeRequest";
1119 case CirculateNotify:
1120 name = "CirculateNotify";
1122 case CirculateRequest:
1123 name = "CirculateRequest";
1125 case PropertyNotify:
1126 name = "PropertyNotify";
1128 case SelectionClear:
1129 name = "SelectionClear";
1131 case SelectionRequest:
1132 name = "SelectionRequest";
1134 case SelectionNotify:
1135 name = "SelectionNotify";
1137 case ColormapNotify:
1138 name = "ColormapNotify";
1141 name = "ClientMessage";
1144 name = "MappingNotify";
1154 xrandr_geteventname(XEvent *e)
1158 switch(e->type - xrandr_eventbase) {
1159 case RRScreenChangeNotify:
1160 name = "RRScreenChangeNotify";
1170 dumpwins(struct swm_region *r, union arg *args)
1174 xcb_get_window_attributes_cookie_t c;
1175 xcb_get_window_attributes_reply_t *wa;
1177 if (r->ws == NULL) {
1178 warnx("dumpwins: invalid workspace");
1182 warnx("=== managed window list ws %02d ===", r->ws->idx);
1183 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1184 state = getstate(win->id);
1185 c = xcb_get_window_attributes(conn, win->id);
1186 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1188 warnx("window: 0x%x, map_state: %d, state: %u, "
1189 "transient: 0x%x", win->id, wa->map_state,
1190 state, win->transient);
1193 warnx("window: 0x%x, failed xcb_get_window_attributes",
1197 warnx("===== unmanaged window list =====");
1198 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1199 state = getstate(win->id);
1200 c = xcb_get_window_attributes(conn, win->id);
1201 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1203 warnx("window: 0x%x, map_state: %d, state: %u, "
1204 "transient: 0x%x", win->id, wa->map_state,
1205 state, win->transient);
1208 warnx("window: 0x%x, failed xcb_get_window_attributes",
1212 warnx("=================================");
1216 dumpwins(struct swm_region *r, union arg *args)
1219 #endif /* SWM_DEBUG */
1221 void expose(XEvent *);
1222 void keypress(XEvent *);
1223 void buttonpress(XEvent *);
1224 void configurerequest(XEvent *);
1225 void configurenotify(XEvent *);
1226 void destroynotify(XEvent *);
1227 void enternotify(XEvent *);
1228 void focusevent(XEvent *);
1229 void mapnotify(XEvent *);
1230 void mappingnotify(XEvent *);
1231 void maprequest(XEvent *);
1232 void propertynotify(XEvent *);
1233 void unmapnotify(XEvent *);
1234 void visibilitynotify(XEvent *);
1235 void clientmessage(XEvent *);
1237 void (*handler[LASTEvent])(XEvent *) = {
1239 [KeyPress] = keypress,
1240 [ButtonPress] = buttonpress,
1241 [ConfigureRequest] = configurerequest,
1242 [ConfigureNotify] = configurenotify,
1243 [DestroyNotify] = destroynotify,
1244 [EnterNotify] = enternotify,
1245 [FocusIn] = focusevent,
1246 [FocusOut] = focusevent,
1247 [MapNotify] = mapnotify,
1248 [MappingNotify] = mappingnotify,
1249 [MapRequest] = maprequest,
1250 [PropertyNotify] = propertynotify,
1251 [UnmapNotify] = unmapnotify,
1252 [VisibilityNotify] = visibilitynotify,
1253 [ClientMessage] = clientmessage,
1259 int saved_errno, status;
1262 saved_errno = errno;
1266 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1271 if (errno != ECHILD)
1272 warn("sighdlr: waitpid");
1273 #endif /* SWM_DEBUG */
1276 if (pid == searchpid)
1280 if (WIFEXITED(status)) {
1281 if (WEXITSTATUS(status) != 0)
1282 warnx("sighdlr: child exit status: %d",
1283 WEXITSTATUS(status));
1285 warnx("sighdlr: child is terminated "
1287 #endif /* SWM_DEBUG */
1301 errno = saved_errno;
1307 struct pid_e *p = NULL;
1309 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1314 TAILQ_FOREACH(p, &pidlist, entry) {
1323 name_to_color(const char *colorname)
1325 uint32_t result = 0;
1326 char cname[32] = "#";
1327 xcb_screen_t *screen;
1328 xcb_colormap_t cmap;
1329 xcb_alloc_color_reply_t *cr;
1330 xcb_alloc_named_color_reply_t *nr;
1331 uint16_t rr, gg, bb;
1333 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1334 cmap = screen->default_colormap;
1336 /* color is in format rgb://rr/gg/bb */
1337 if (strncmp(colorname, "rgb:", 4) == 0) {
1338 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1339 warnx("could not parse rgb %s", colorname);
1341 cr = xcb_alloc_color_reply(conn,
1342 xcb_alloc_color(conn, cmap, rr, gg, bb),
1348 warnx("color '%s' not found", colorname);
1351 nr = xcb_alloc_named_color_reply(conn,
1352 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1356 strlcat(cname, colorname + 2, sizeof cname - 1);
1357 nr = xcb_alloc_named_color_reply(conn,
1358 xcb_alloc_named_color(conn, cmap, strlen(cname),
1366 warnx("color '%s' not found", colorname);
1373 setscreencolor(char *val, int i, int c)
1377 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1378 if (i > 0 && i <= num_screens) {
1379 screens[i - 1].c[c].color = name_to_color(val);
1380 free(screens[i - 1].c[c].name);
1381 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1383 } else if (i == -1) {
1384 for (i = 0; i < num_screens; i++) {
1385 screens[i].c[c].color = name_to_color(val);
1386 free(screens[i].c[c].name);
1387 if ((screens[i].c[c].name = strdup(val)) == NULL)
1391 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1396 fancy_stacker(struct workspace *ws)
1398 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1399 if (ws->cur_layout->l_stack == vertical_stack)
1400 snprintf(ws->stacker, sizeof ws->stacker,
1401 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1402 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1403 if (ws->cur_layout->l_stack == horizontal_stack)
1404 snprintf(ws->stacker, sizeof ws->stacker,
1405 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1406 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1410 plain_stacker(struct workspace *ws)
1412 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1413 if (ws->cur_layout->l_stack == vertical_stack)
1414 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1415 sizeof ws->stacker);
1416 if (ws->cur_layout->l_stack == horizontal_stack)
1417 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1418 sizeof ws->stacker);
1422 custom_region(char *val)
1424 unsigned int sidx, x, y, w, h;
1426 xcb_screen_t *screen;
1428 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1429 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1430 errx(1, "invalid custom region, "
1431 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1432 if (sidx < 1 || sidx > num_screens)
1433 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1437 screen = get_screen(sidx);
1439 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1441 if (x > screen->width_in_pixels ||
1442 y > screen->height_in_pixels ||
1443 w + x > screen->width_in_pixels ||
1444 h + y > screen->height_in_pixels) {
1445 warnx("ignoring region %ux%u+%u+%u - not within screen "
1446 "boundaries (%ux%u)", w, h, x, y,
1447 screen->width_in_pixels, screen->height_in_pixels);
1451 new_region(&screens[sidx], x, y, w, h);
1455 socket_setnonblock(int fd)
1459 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1460 err(1, "fcntl F_GETFL");
1461 flags |= O_NONBLOCK;
1462 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1463 err(1, "fcntl F_SETFL");
1467 bar_print(struct swm_region *r, const char *s)
1471 xcb_rectangle_t rect;
1473 XRectangle ibox, lbox;
1476 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1478 switch (bar_justify) {
1479 case SWM_BAR_JUSTIFY_LEFT:
1482 case SWM_BAR_JUSTIFY_CENTER:
1483 x = (WIDTH(r) - lbox.width) / 2;
1485 case SWM_BAR_JUSTIFY_RIGHT:
1486 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1490 if (x < SWM_BAR_OFFSET)
1495 rect.width = WIDTH(r->bar);
1496 rect.height = HEIGHT(r->bar);
1498 /* clear back buffer */
1499 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1500 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1501 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1502 sizeof(rect), &rect);
1504 /* draw back buffer */
1505 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1506 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1507 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1508 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1512 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1513 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1517 bar_extra_stop(void)
1521 bzero(bar_pipe, sizeof bar_pipe);
1524 kill(bar_pid, SIGTERM);
1527 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1532 bar_class_name(char *s, size_t sz, struct swm_region *r)
1534 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1536 if (r->ws->focus->ch.class_name != NULL)
1537 strlcat(s, r->ws->focus->ch.class_name, sz);
1541 bar_title_name(char *s, size_t sz, struct swm_region *r)
1543 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1545 if (r->ws->focus->ch.instance_name != NULL)
1546 strlcat(s, r->ws->focus->ch.instance_name, sz);
1550 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1552 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1555 bar_class_name(s, sz, r);
1556 strlcat(s, ":", sz);
1557 bar_title_name(s, sz, r);
1561 bar_window_float(char *s, size_t sz, struct swm_region *r)
1563 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1565 if (r->ws->focus->floating)
1566 strlcat(s, "(f)", sz);
1570 bar_window_name(char *s, size_t sz, struct swm_region *r)
1574 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1576 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1579 strlcat(s, title, sz);
1583 int urgent[SWM_WS_MAX];
1585 bar_urgent(char *s, size_t sz)
1588 int i, j, num_screens;
1590 xcb_get_property_cookie_t c;
1591 xcb_icccm_wm_hints_t hints;
1593 for (i = 0; i < workspace_limit; i++)
1596 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1597 for (i = 0; i < num_screens; i++)
1598 for (j = 0; j < workspace_limit; j++)
1599 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1600 c = xcb_icccm_get_wm_hints(conn, win->id);
1601 if (xcb_icccm_get_wm_hints_reply(conn, c,
1604 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1608 for (i = 0; i < workspace_limit; i++) {
1610 snprintf(b, sizeof b, "%d ", i + 1);
1612 snprintf(b, sizeof b, "- ");
1618 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1620 if (r == NULL || r->ws == NULL)
1622 if (r->ws->name != NULL)
1623 strlcat(s, r->ws->name, sz);
1626 /* build the default bar format according to the defined enabled options */
1628 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1630 /* if format provided, just copy the buffers */
1631 if (bar_format != NULL) {
1632 strlcpy(fmtnew, fmtexp, sz);
1636 /* reset the output buffer */
1639 strlcat(fmtnew, "+N:+I ", sz);
1641 strlcat(fmtnew, "+S", sz);
1642 strlcat(fmtnew, " ", sz);
1644 /* only show the workspace name if there's actually one */
1645 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1646 strlcat(fmtnew, "<+D>", sz);
1647 strlcat(fmtnew, "+3<", sz);
1649 if (clock_enabled) {
1650 strlcat(fmtnew, fmtexp, sz);
1651 strlcat(fmtnew, "+4<", sz);
1654 /* bar_urgent already adds the space before the last asterisk */
1656 strlcat(fmtnew, "* +U*+4<", sz);
1658 if (title_class_enabled) {
1659 strlcat(fmtnew, "+C", sz);
1660 if (title_name_enabled == 0)
1661 strlcat(fmtnew, "+4<", sz);
1664 /* checks needed by the colon and floating strlcat(3) calls below */
1665 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1666 if (title_name_enabled) {
1667 if (title_class_enabled)
1668 strlcat(fmtnew, ":", sz);
1669 strlcat(fmtnew, "+T+4<", sz);
1671 if (window_name_enabled) {
1672 if (r->ws->focus->floating)
1673 strlcat(fmtnew, "+F ", sz);
1674 strlcat(fmtnew, "+64W ", sz);
1678 /* finally add the action script output and the version */
1679 strlcat(fmtnew, "+4<+A+4<+V", sz);
1683 bar_replace_pad(char *tmp, int *limit, size_t sz)
1685 /* special case; no limit given, pad one space, instead */
1686 if (*limit == sz - 1)
1688 snprintf(tmp, sz, "%*s", *limit, " ");
1691 /* replaces the bar format character sequences (like in tmux(1)) */
1693 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1697 char tmp[SWM_BAR_MAX];
1701 /* reset strlcat(3) buffer */
1704 /* get number, if any */
1707 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1708 limit = sizeof tmp - 1;
1709 if (limit <= 0 || limit >= sizeof tmp)
1710 limit = sizeof tmp - 1;
1712 /* there is nothing to replace (ie EOL) */
1719 bar_replace_pad(tmp, &limit, sizeof tmp);
1722 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1725 bar_class_name(tmp, sizeof tmp, r);
1728 bar_workspace_name(tmp, sizeof tmp, r);
1731 bar_window_float(tmp, sizeof tmp, r);
1734 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1737 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1740 bar_class_title_name(tmp, sizeof tmp, r);
1743 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1746 bar_title_name(tmp, sizeof tmp, r);
1749 bar_urgent(tmp, sizeof tmp);
1752 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1755 bar_window_name(tmp, sizeof tmp, r);
1758 /* unknown character sequence; copy as-is */
1759 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1767 while (limit-- > 0) {
1768 if (*offrep >= sz - 1)
1770 fmtrep[(*offrep)++] = *ptr++;
1778 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1783 while (*fmt != '\0') {
1785 /* skip ordinary characters */
1788 fmtrep[off++] = *fmt++;
1792 /* character sequence found; replace it */
1793 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1802 bar_fmt_expand(char *fmtexp, size_t sz)
1809 /* start by grabbing the current time and date */
1811 localtime_r(&tmt, &tm);
1813 /* figure out what to expand */
1814 if (bar_format != NULL)
1816 else if (bar_format == NULL && clock_enabled)
1818 /* if nothing to expand bail out */
1824 /* copy as-is, just in case the format shouldn't be expanded below */
1825 strlcpy(fmtexp, fmt, sz);
1826 /* finally pass the string through strftime(3) */
1827 #ifndef SWM_DENY_CLOCK_FORMAT
1828 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1829 warnx("format too long");
1837 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1838 char fmtrep[SWM_BAR_MAX];
1840 struct swm_region *r;
1842 /* expand the format by first passing it through strftime(3) */
1843 bar_fmt_expand(fmtexp, sizeof fmtexp);
1845 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1846 for (i = 0; i < num_screens; i++) {
1847 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1850 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1851 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1852 bar_print(r, fmtrep);
1863 if (bar_enabled == 0)
1865 if (bar_extra && bar_extra_running) {
1866 /* ignore short reads; it'll correct itself */
1867 while ((b = fgetln(stdin, &len)) != NULL)
1868 if (b && b[len - 1] == '\n') {
1870 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1872 if (b == NULL && errno != EAGAIN) {
1873 warn("bar_update: bar_extra failed");
1877 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1890 bar_toggle(struct swm_region *r, union arg *args)
1892 struct swm_region *tmpr;
1895 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1897 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1899 for (i = 0; i < num_screens; i++)
1900 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1902 xcb_unmap_window(conn, tmpr->bar->id);
1904 for (i = 0; i < num_screens; i++)
1905 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1907 map_window_raised(tmpr->bar->id);
1910 bar_enabled = !bar_enabled;
1913 /* must be after stack */
1920 struct swm_region *r;
1924 /* do this here because the conf file is in memory */
1925 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1926 /* launch external status app */
1927 bar_extra_running = 1;
1928 if (pipe(bar_pipe) == -1)
1929 err(1, "pipe error");
1930 socket_setnonblock(bar_pipe[0]);
1931 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1932 if (dup2(bar_pipe[0], 0) == -1)
1934 if (dup2(bar_pipe[1], 1) == -1)
1936 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1937 err(1, "could not disable SIGPIPE");
1938 switch (bar_pid = fork()) {
1940 err(1, "cannot fork");
1944 execvp(bar_argv[0], bar_argv);
1945 err(1, "%s external app failed", bar_argv[0]);
1947 default: /* parent */
1953 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1954 for (i = 0; i < num_screens; i++)
1955 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1958 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1959 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1960 xcb_change_window_attributes(conn, r->bar->id,
1961 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1967 bar_setup(struct swm_region *r)
1969 char *default_string;
1970 char **missing_charsets;
1971 int num_missing_charsets = 0;
1973 xcb_screen_t *screen = get_screen(r->s->idx);
1977 XFreeFontSet(display, bar_fs);
1981 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1982 err(1, "bar_setup: calloc: failed to allocate memory.");
1984 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1986 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1987 &num_missing_charsets, &default_string);
1989 if (num_missing_charsets > 0) {
1990 warnx("Unable to load charset(s):");
1992 for (i = 0; i < num_missing_charsets; ++i)
1993 warnx("%s", missing_charsets[i]);
1995 XFreeStringList(missing_charsets);
1997 if (strcmp(default_string, ""))
1998 warnx("Glyphs from those sets will be replaced "
1999 "by '%s'.", default_string);
2001 warnx("Glyphs from those sets won't be drawn.");
2005 errx(1, "Error creating font set structure.");
2007 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2009 bar_height = bar_fs_extents->max_logical_extent.height +
2010 2 * bar_border_width;
2016 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2017 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2018 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2020 r->bar->id = xcb_generate_id(conn);
2021 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
2022 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
2023 xcb_create_window(conn, screen->root_depth, r->bar->id, r->s->root,
2024 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2025 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2026 screen->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL,
2029 r->bar->buffer = xcb_generate_id(conn);
2030 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2031 WIDTH(r->bar), HEIGHT(r->bar));
2033 xcb_randr_select_input(conn, r->bar->id,
2034 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2037 map_window_raised(r->bar->id);
2039 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2040 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2043 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2044 err(1, "could not install bar_signal");
2049 bar_cleanup(struct swm_region *r)
2053 xcb_destroy_window(conn, r->bar->id);
2054 xcb_free_pixmap(conn, r->bar->buffer);
2060 drain_enter_notify(void)
2065 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
2068 DNPRINTF(SWM_D_EVENT, "drain_enter_notify: drained: %d\n", i);
2072 set_win_state(struct ws_win *win, uint16_t state)
2074 uint16_t data[2] = { state, XCB_ATOM_NONE };
2076 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2081 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
2082 astate, 32, 2, data);
2086 getstate(xcb_window_t w)
2088 uint16_t result = 0;
2089 xcb_get_property_cookie_t c;
2090 xcb_get_property_reply_t *r;
2092 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
2093 r = xcb_get_property_reply(conn, c, NULL);
2096 result = *((uint16_t *)xcb_get_property_value(r));
2104 version(struct swm_region *r, union arg *args)
2106 bar_version = !bar_version;
2108 snprintf(bar_vertext, sizeof bar_vertext,
2109 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2111 strlcpy(bar_vertext, "", sizeof bar_vertext);
2116 client_msg(struct ws_win *win, xcb_atom_t a)
2118 xcb_client_message_event_t ev;
2123 bzero(&ev, sizeof ev);
2124 ev.response_type = XCB_CLIENT_MESSAGE;
2125 ev.window = win->id;
2128 ev.data.data32[0] = a;
2129 ev.data.data32[1] = XCB_CURRENT_TIME;
2131 xcb_send_event(conn, False, win->id,
2132 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2136 /* synthetic response to a ConfigureRequest when not making a change */
2138 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
2140 xcb_configure_notify_event_t ce;
2145 /* send notification of unchanged state. */
2146 bzero(&ce, sizeof(ce));
2147 ce.response_type = XCB_CONFIGURE_NOTIFY;
2150 ce.width = WIDTH(win);
2151 ce.height = HEIGHT(win);
2152 ce.override_redirect = False;
2157 ce.window = win->id;
2158 ce.border_width = BORDER(win);
2159 ce.above_sibling = XCB_WINDOW_NONE;
2162 ce.event = ev->window;
2163 ce.window = ev->window;
2165 /* make response appear more WM_SIZE_HINTS-compliant */
2167 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2168 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2169 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2170 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2171 SH_INC_W(win), SH_INC_H(win));
2175 /* the hint may be set... to 0! */
2176 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2177 ce.width = SH_MIN_W(win);
2178 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2179 ce.height = SH_MIN_H(win);
2184 /* may also be advertized as 0 */
2185 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2186 ce.width = SH_MAX_W(win);
2187 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2188 ce.height = SH_MAX_H(win);
2191 /* resize increment. */
2193 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2194 ce.width -= (ce.width - SH_MIN_W(win)) %
2196 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2197 ce.height -= (ce.height - SH_MIN_H(win)) %
2201 /* adjust x and y for requested border_width. */
2202 ce.x += BORDER(win) - ev->border_width;
2203 ce.y += BORDER(win) - ev->border_width;
2204 ce.border_width = ev->border_width;
2205 ce.above_sibling = ev->above;
2208 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2209 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2210 ce.y, ce.width, ce.height, ce.border_width);
2212 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2218 count_win(struct workspace *ws, int count_transient)
2223 TAILQ_FOREACH(win, &ws->winlist, entry) {
2224 if (count_transient == 0 && win->floating)
2226 if (count_transient == 0 && win->transient)
2232 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2238 quit(struct swm_region *r, union arg *args)
2240 DNPRINTF(SWM_D_MISC, "quit\n");
2245 unmap_window(struct ws_win *win)
2250 /* don't unmap again */
2251 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2254 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2256 xcb_unmap_window(conn, win->id);
2257 xcb_change_window_attributes(conn, win->id,
2258 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2265 int i, j, num_screens;
2267 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2268 for (i = 0; i < num_screens; i++)
2269 for (j = 0; j < workspace_limit; j++)
2270 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2275 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2277 xcb_key_press_event_t event;
2278 xcb_keycode_t *keycode;
2283 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2285 event.event = win->id;
2286 event.root = win->s->root;
2287 event.child = XCB_WINDOW_NONE;
2288 event.time = XCB_CURRENT_TIME;
2289 event.event_x = X(win);
2290 event.event_y = Y(win);
2293 event.same_screen = True;
2294 event.detail = *keycode;
2295 event.state = modifiers;
2297 event.response_type = XCB_KEY_PRESS;
2298 xcb_send_event(conn, True, win->id,
2299 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2301 event.response_type = XCB_KEY_RELEASE;
2302 xcb_send_event(conn, True, win->id,
2303 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2308 restart(struct swm_region *r, union arg *args)
2310 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2312 /* disable alarm because the following code may not be interrupted */
2314 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2315 err(1, "can't disable alarm");
2321 xcb_key_symbols_free(syms);
2323 xcb_disconnect(conn);
2325 execvp(start_argv[0], start_argv);
2326 warn("execvp failed");
2331 root_to_region(xcb_window_t root)
2333 struct swm_region *r = NULL;
2335 xcb_query_pointer_reply_t *qpr;
2337 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2339 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2340 for (i = 0; i < num_screens; i++)
2341 if (screens[i].root == root)
2344 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2345 screens[i].root), NULL);
2348 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2349 qpr->root_x, qpr->root_y);
2350 /* choose a region based on pointer location */
2351 TAILQ_FOREACH(r, &screens[i].rl, entry)
2352 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2353 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2359 r = TAILQ_FIRST(&screens[i].rl);
2365 find_unmanaged_window(xcb_window_t id)
2368 int i, j, num_screens;
2370 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2371 for (i = 0; i < num_screens; i++)
2372 for (j = 0; j < workspace_limit; j++)
2373 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2381 find_window(xcb_window_t id)
2384 int i, j, num_screens;
2385 xcb_query_tree_cookie_t c;
2386 xcb_query_tree_reply_t *r;
2388 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2389 for (i = 0; i < num_screens; i++)
2390 for (j = 0; j < workspace_limit; j++)
2391 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2395 c = xcb_query_tree(conn, id);
2396 r = xcb_query_tree_reply(conn, c, NULL);
2400 /* if we were looking for the parent return that window instead */
2401 if (r->parent == 0 || r->root == r->parent)
2404 /* look for parent */
2405 for (i = 0; i < num_screens; i++)
2406 for (j = 0; j < workspace_limit; j++)
2407 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2408 if (r->parent == win->id) {
2418 spawn(int ws_idx, union arg *args, int close_fd)
2423 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2426 close(xcb_get_file_descriptor(conn));
2428 setenv("LD_PRELOAD", SWM_LIB, 1);
2430 if (asprintf(&ret, "%d", ws_idx) == -1) {
2431 warn("spawn: asprintf SWM_WS");
2434 setenv("_SWM_WS", ret, 1);
2438 if (asprintf(&ret, "%d", getpid()) == -1) {
2439 warn("spawn: asprintf _SWM_PID");
2442 setenv("_SWM_PID", ret, 1);
2446 if (setsid() == -1) {
2447 warn("spawn: setsid");
2453 * close stdin and stdout to prevent interaction between apps
2454 * and the baraction script
2455 * leave stderr open to record errors
2457 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2458 warn("spawn: open");
2461 dup2(fd, STDIN_FILENO);
2462 dup2(fd, STDOUT_FILENO);
2467 execvp(args->argv[0], args->argv);
2469 warn("spawn: execvp");
2474 kill_refs(struct ws_win *win)
2476 int i, x, num_screens;
2477 struct swm_region *r;
2478 struct workspace *ws;
2483 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2484 for (i = 0; i < num_screens; i++)
2485 TAILQ_FOREACH(r, &screens[i].rl, entry)
2486 for (x = 0; x < workspace_limit; x++) {
2488 if (win == ws->focus)
2490 if (win == ws->focus_prev)
2491 ws->focus_prev = NULL;
2496 validate_win(struct ws_win *testwin)
2499 struct workspace *ws;
2500 struct swm_region *r;
2501 int i, x, num_screens;
2503 if (testwin == NULL)
2506 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2507 for (i = 0; i < num_screens; i++)
2508 TAILQ_FOREACH(r, &screens[i].rl, entry)
2509 for (x = 0; x < workspace_limit; x++) {
2511 TAILQ_FOREACH(win, &ws->winlist, entry)
2519 validate_ws(struct workspace *testws)
2521 struct swm_region *r;
2522 struct workspace *ws;
2523 int i, x, num_screens;
2525 /* validate all ws */
2526 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2527 for (i = 0; i < num_screens; i++)
2528 TAILQ_FOREACH(r, &screens[i].rl, entry)
2529 for (x = 0; x < workspace_limit; x++) {
2538 unfocus_win(struct ws_win *win)
2541 xcb_window_t none = XCB_WINDOW_NONE;
2543 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2547 if (win->ws == NULL)
2550 if (validate_ws(win->ws))
2551 return; /* XXX this gets hit with thunderbird, needs fixing */
2553 if (win->ws->r == NULL)
2556 if (validate_win(win)) {
2561 if (win->ws->focus == win) {
2562 win->ws->focus = NULL;
2563 win->ws->focus_prev = win;
2566 if (validate_win(win->ws->focus)) {
2567 kill_refs(win->ws->focus);
2568 win->ws->focus = NULL;
2570 if (validate_win(win->ws->focus_prev)) {
2571 kill_refs(win->ws->focus_prev);
2572 win->ws->focus_prev = NULL;
2575 /* drain all previous unfocus events */
2576 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2579 grabbuttons(win, 0);
2580 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2581 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2582 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2583 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2591 int i, j, num_screens;
2593 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2595 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2596 for (i = 0; i < num_screens; i++)
2597 for (j = 0; j < workspace_limit; j++)
2598 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2603 focus_win(struct ws_win *win)
2606 struct ws_win *cfw = NULL;
2607 xcb_get_input_focus_cookie_t c;
2608 xcb_get_input_focus_reply_t *r;
2609 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2611 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2615 if (win->ws == NULL)
2618 if (validate_ws(win->ws))
2619 return; /* XXX this gets hit with thunderbird, needs fixing */
2621 if (validate_win(win)) {
2626 if (validate_win(win)) {
2631 c = xcb_get_input_focus(conn);
2632 r = xcb_get_input_focus_reply(conn, c, NULL);
2634 cur_focus = r->focus;
2637 if ((cfw = find_window(cur_focus)) != NULL)
2640 /* use larger hammer since the window was killed somehow */
2641 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2642 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2643 xcb_change_window_attributes(conn, cfw->id,
2644 XCB_CW_BORDER_PIXEL,
2645 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2648 win->ws->focus = win;
2650 if (win->ws->r != NULL) {
2651 /* drain all previous focus events */
2652 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2656 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2657 win->id, XCB_CURRENT_TIME);
2658 grabbuttons(win, 1);
2659 xcb_change_window_attributes(conn, win->id,
2660 XCB_CW_BORDER_PIXEL,
2661 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2662 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2663 win->ws->always_raise)
2664 map_window_raised(win->id);
2666 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2667 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2675 switchws(struct swm_region *r, union arg *args)
2677 int wsid = args->id, unmap_old = 0;
2678 struct swm_region *this_r, *other_r;
2680 struct workspace *new_ws, *old_ws;
2686 if (wsid >= workspace_limit)
2690 old_ws = this_r->ws;
2691 new_ws = &this_r->s->ws[wsid];
2693 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2694 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2696 if (new_ws == NULL || old_ws == NULL)
2698 if (new_ws == old_ws)
2701 other_r = new_ws->r;
2702 if (other_r == NULL) {
2703 /* the other workspace is hidden, hide this one */
2707 /* the other ws is visible in another region, exchange them */
2708 other_r->ws_prior = new_ws;
2709 other_r->ws = old_ws;
2710 old_ws->r = other_r;
2712 this_r->ws_prior = old_ws;
2713 this_r->ws = new_ws;
2716 /* this is needed so that we can click on a window after a restart */
2720 a.id = SWM_ARG_ID_FOCUSCUR;
2721 focus(new_ws->r, &a);
2723 /* unmap old windows */
2725 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2728 if (focus_mode == SWM_FOCUS_DEFAULT)
2729 drain_enter_notify();
2733 cyclews(struct swm_region *r, union arg *args)
2736 struct swm_screen *s = r->s;
2739 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2740 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2745 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2748 case SWM_ARG_ID_CYCLEWS_UP:
2749 if (a.id < workspace_limit - 1)
2754 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2757 case SWM_ARG_ID_CYCLEWS_DOWN:
2761 a.id = workspace_limit - 1;
2768 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2770 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2774 } while (a.id != r->ws->idx);
2778 priorws(struct swm_region *r, union arg *args)
2782 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2783 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2785 if (r->ws_prior == NULL)
2788 a.id = r->ws_prior->idx;
2793 cyclescr(struct swm_region *r, union arg *args)
2795 struct swm_region *rr = NULL;
2797 int i, x, y, num_screens;
2799 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2800 /* do nothing if we don't have more than one screen */
2801 if (!(num_screens > 1 || outputs > 1))
2806 case SWM_ARG_ID_CYCLESC_UP:
2807 rr = TAILQ_NEXT(r, entry);
2809 rr = TAILQ_FIRST(&screens[i].rl);
2811 case SWM_ARG_ID_CYCLESC_DOWN:
2812 rr = TAILQ_PREV(r, swm_region_list, entry);
2814 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2822 /* move mouse to region */
2824 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2825 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2828 a.id = SWM_ARG_ID_FOCUSCUR;
2831 if (rr->ws->focus) {
2832 /* move to focus window */
2833 x = X(rr->ws->focus) + 1;
2834 y = Y(rr->ws->focus) + 1;
2835 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2841 sort_windows(struct ws_win_list *wl)
2843 struct ws_win *win, *parent, *nxt;
2848 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2849 nxt = TAILQ_NEXT(win, entry);
2850 if (win->transient) {
2851 parent = find_window(win->transient);
2852 if (parent == NULL) {
2853 warnx("not possible bug");
2856 TAILQ_REMOVE(wl, win, entry);
2857 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2864 swapwin(struct swm_region *r, union arg *args)
2866 struct ws_win *target, *source;
2867 struct ws_win *cur_focus;
2868 struct ws_win_list *wl;
2871 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2872 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2874 cur_focus = r->ws->focus;
2875 if (cur_focus == NULL)
2879 wl = &source->ws->winlist;
2882 case SWM_ARG_ID_SWAPPREV:
2883 if (source->transient)
2884 source = find_window(source->transient);
2885 target = TAILQ_PREV(source, ws_win_list, entry);
2886 if (target && target->transient)
2887 target = find_window(target->transient);
2888 TAILQ_REMOVE(wl, source, entry);
2890 TAILQ_INSERT_TAIL(wl, source, entry);
2892 TAILQ_INSERT_BEFORE(target, source, entry);
2894 case SWM_ARG_ID_SWAPNEXT:
2895 target = TAILQ_NEXT(source, entry);
2896 /* move the parent and let the sort handle the move */
2897 if (source->transient)
2898 source = find_window(source->transient);
2899 TAILQ_REMOVE(wl, source, entry);
2901 TAILQ_INSERT_HEAD(wl, source, entry);
2903 TAILQ_INSERT_AFTER(wl, target, source, entry);
2905 case SWM_ARG_ID_SWAPMAIN:
2906 target = TAILQ_FIRST(wl);
2907 if (target == source) {
2908 if (source->ws->focus_prev != NULL &&
2909 source->ws->focus_prev != target)
2910 source = source->ws->focus_prev;
2914 if (target == NULL || source == NULL)
2916 source->ws->focus_prev = target;
2917 TAILQ_REMOVE(wl, target, entry);
2918 TAILQ_INSERT_BEFORE(source, target, entry);
2919 TAILQ_REMOVE(wl, source, entry);
2920 TAILQ_INSERT_HEAD(wl, source, entry);
2922 case SWM_ARG_ID_MOVELAST:
2923 TAILQ_REMOVE(wl, source, entry);
2924 TAILQ_INSERT_TAIL(wl, source, entry);
2927 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2937 focus_prev(struct ws_win *win)
2939 struct ws_win *winfocus = NULL;
2940 struct ws_win *cur_focus = NULL;
2941 struct ws_win_list *wl = NULL;
2942 struct workspace *ws = NULL;
2944 if (!(win && win->ws))
2949 cur_focus = ws->focus;
2951 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2952 WINID(win), WINID(cur_focus));
2954 /* pickle, just focus on whatever */
2955 if (cur_focus == NULL) {
2956 /* use prev_focus if valid */
2957 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2958 find_window(WINID(ws->focus_prev)))
2959 winfocus = ws->focus_prev;
2963 /* if transient focus on parent */
2964 if (cur_focus->transient) {
2965 winfocus = find_window(cur_focus->transient);
2969 /* if in max_stack try harder */
2970 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2971 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2972 if (cur_focus != ws->focus_prev)
2973 winfocus = ws->focus_prev;
2975 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2980 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2982 if (winfocus == NULL || winfocus == win) {
2983 switch (focus_close) {
2984 case SWM_STACK_BOTTOM:
2985 winfocus = TAILQ_FIRST(wl);
2988 winfocus = TAILQ_LAST(wl, ws_win_list);
2990 case SWM_STACK_ABOVE:
2991 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2992 if (focus_close_wrap)
2993 winfocus = TAILQ_FIRST(wl);
2995 winfocus = TAILQ_PREV(cur_focus,
2996 ws_win_list, entry);
2999 case SWM_STACK_BELOW:
3000 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3002 if (focus_close_wrap)
3003 winfocus = TAILQ_LAST(wl, ws_win_list);
3005 winfocus = TAILQ_NEXT(cur_focus, entry);
3011 if (winfocus == NULL) {
3012 if (focus_default == SWM_STACK_TOP)
3013 winfocus = TAILQ_LAST(wl, ws_win_list);
3015 winfocus = TAILQ_FIRST(wl);
3019 focus_magic(winfocus);
3023 focus(struct swm_region *r, union arg *args)
3025 struct ws_win *winfocus = NULL, *head;
3026 struct ws_win *cur_focus = NULL;
3027 struct ws_win_list *wl = NULL;
3028 struct workspace *ws = NULL;
3034 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3036 /* treat FOCUS_CUR special */
3037 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3038 if (r->ws->focus && r->ws->focus->iconic == 0)
3039 winfocus = r->ws->focus;
3040 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3041 winfocus = r->ws->focus_prev;
3043 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3044 if (winfocus->iconic == 0)
3047 focus_magic(winfocus);
3051 if ((cur_focus = r->ws->focus) == NULL)
3055 if (TAILQ_EMPTY(wl))
3057 /* make sure there is at least one uniconified window */
3059 TAILQ_FOREACH(winfocus, wl, entry)
3060 if (winfocus->iconic == 0) {
3068 case SWM_ARG_ID_FOCUSPREV:
3069 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3071 head = TAILQ_LAST(wl, ws_win_list);
3073 if (WINID(winfocus) == cur_focus->transient) {
3074 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3076 head = TAILQ_LAST(wl, ws_win_list);
3081 if (winfocus && winfocus->iconic) {
3082 while (winfocus != cur_focus) {
3083 if (winfocus == NULL)
3084 winfocus = TAILQ_LAST(wl, ws_win_list);
3085 if (winfocus->iconic == 0)
3087 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3093 case SWM_ARG_ID_FOCUSNEXT:
3094 head = TAILQ_NEXT(cur_focus, entry);
3096 head = TAILQ_FIRST(wl);
3100 if (winfocus && winfocus->iconic) {
3101 while (winfocus != cur_focus) {
3102 if (winfocus == NULL)
3103 winfocus = TAILQ_FIRST(wl);
3104 if (winfocus->iconic == 0)
3106 winfocus = TAILQ_NEXT(winfocus, entry);
3111 case SWM_ARG_ID_FOCUSMAIN:
3112 winfocus = TAILQ_FIRST(wl);
3113 if (winfocus == cur_focus)
3114 winfocus = cur_focus->ws->focus_prev;
3121 focus_magic(winfocus);
3125 cycle_layout(struct swm_region *r, union arg *args)
3127 struct workspace *ws = r->ws;
3130 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3133 if (ws->cur_layout->l_stack == NULL)
3134 ws->cur_layout = &layouts[0];
3137 if (focus_mode == SWM_FOCUS_DEFAULT)
3138 drain_enter_notify();
3139 a.id = SWM_ARG_ID_FOCUSCUR;
3144 stack_config(struct swm_region *r, union arg *args)
3146 struct workspace *ws = r->ws;
3148 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3151 if (ws->cur_layout->l_config != NULL)
3152 ws->cur_layout->l_config(ws, args->id);
3154 if (args->id != SWM_ARG_ID_STACKINIT)
3161 struct swm_geometry g;
3162 struct swm_region *r;
3168 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3170 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3171 for (i = 0; i < num_screens; i++) {
3175 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3176 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3177 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3179 /* start with screen geometry, adjust for bar */
3181 g.w -= 2 * border_width;
3182 g.h -= 2 * border_width;
3188 r->ws->cur_layout->l_stack(r->ws, &g);
3189 r->ws->cur_layout->l_string(r->ws);
3190 /* save r so we can track region changes */
3197 if (focus_mode == SWM_FOCUS_DEFAULT)
3198 drain_enter_notify();
3200 DNPRINTF(SWM_D_STACK, "stack: end\n");
3204 store_float_geom(struct ws_win *win, struct swm_region *r)
3206 /* retain window geom and region geom */
3207 win->g_float = win->g;
3208 win->g_float.x -= X(r);
3209 win->g_float.y -= Y(r);
3210 win->g_floatvalid = 1;
3211 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3212 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3213 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3214 win->g_float.w, win->g_float.h);
3218 stack_floater(struct ws_win *win, struct swm_region *r)
3223 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3226 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3227 * geom on ws switches or return from max mode
3229 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3230 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3231 /* refloat at last floating relative position */
3232 win->g = win->g_float;
3237 win->floatmaxed = 0;
3240 * if set to fullscreen mode, configure window to maximum size.
3242 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3243 if (!win->g_floatvalid)
3244 store_float_geom(win, win->ws->r);
3246 win->g = win->ws->r->g;
3250 * remove border on fullscreen floater when in fullscreen mode or when
3251 * the quirk is present.
3253 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3254 ((win->quirks & SWM_Q_FULLSCREEN) &&
3255 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3256 if (win->bordered) {
3258 X(win) += border_width;
3259 Y(win) += border_width;
3261 } else if (!win->bordered) {
3263 X(win) -= border_width;
3264 Y(win) -= border_width;
3267 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3268 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3269 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3274 * floaters and transients are auto-centred unless moved
3277 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3278 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3281 /* keep window within region bounds */
3282 constrain_window(win, r, 0);
3288 * Send keystrokes to terminal to decrease/increase the font size as the
3289 * window size changes.
3292 adjust_font(struct ws_win *win)
3294 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3295 win->floating || win->transient)
3298 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3299 WIDTH(win) / win->sh.width_inc < term_width &&
3300 win->font_steps < SWM_MAX_FONT_STEPS) {
3301 win->font_size_boundary[win->font_steps] =
3302 (win->sh.width_inc * term_width) + win->sh.base_width;
3305 win->last_inc = win->sh.width_inc;
3306 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3307 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3308 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3311 win->last_inc = win->sh.width_inc;
3312 fake_keypress(win, XK_KP_Add, ShiftMask);
3316 #define SWAPXY(g) do { \
3318 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3319 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3322 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3324 struct swm_geometry win_g, r_g = *g;
3325 struct ws_win *win, *fs_win = NULL;
3326 int i, j, s, stacks;
3327 int w_inc = 1, h_inc, w_base = 1, h_base;
3328 int hrh, extra = 0, h_slice, last_h = 0;
3329 int split, colno, winno, mwin, msize, mscale;
3330 int remain, missing, v_slice, reconfigure;
3332 xcb_get_window_attributes_reply_t *war;
3334 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3335 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3337 winno = count_win(ws, 0);
3338 if (winno == 0 && count_win(ws, 1) == 0)
3341 TAILQ_FOREACH(win, &ws->winlist, entry)
3342 if (win->transient == 0 && win->floating == 0
3343 && win->iconic == 0)
3350 w_inc = win->sh.width_inc;
3351 w_base = win->sh.base_width;
3352 mwin = ws->l_state.horizontal_mwin;
3353 mscale = ws->l_state.horizontal_msize;
3354 stacks = ws->l_state.horizontal_stacks;
3357 w_inc = win->sh.height_inc;
3358 w_base = win->sh.base_height;
3359 mwin = ws->l_state.vertical_mwin;
3360 mscale = ws->l_state.vertical_msize;
3361 stacks = ws->l_state.vertical_stacks;
3365 if (stacks > winno - mwin)
3366 stacks = winno - mwin;
3370 h_slice = r_g.h / SWM_H_SLICE;
3371 if (mwin && winno > mwin) {
3372 v_slice = r_g.w / SWM_V_SLICE;
3376 win_g.w = v_slice * mscale;
3378 if (w_inc > 1 && w_inc < v_slice) {
3379 /* adjust for window's requested size increment */
3380 remain = (win_g.w - w_base) % w_inc;
3386 win_g.x += r_g.w - msize;
3389 colno = split = winno / stacks;
3390 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3391 2 * border_width) / stacks);
3393 hrh = r_g.h / colno;
3394 extra = r_g.h - (colno * hrh);
3395 win_g.h = hrh - 2 * border_width;
3397 /* stack all the tiled windows */
3398 i = j = 0, s = stacks;
3399 TAILQ_FOREACH(win, &ws->winlist, entry) {
3400 if (win->transient != 0 || win->floating != 0)
3402 if (win->iconic != 0)
3405 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3410 if (split && i == split) {
3411 colno = (winno - mwin) / stacks;
3412 if (s <= (winno - mwin) % stacks)
3414 split = split + colno;
3415 hrh = (r_g.h / colno);
3416 extra = r_g.h - (colno * hrh);
3420 win_g.x += win_g.w + 2 * border_width;
3421 win_g.w = (r_g.w - msize -
3422 (stacks * 2 * border_width)) / stacks;
3424 win_g.w += (r_g.w - msize -
3425 (stacks * 2 * border_width)) % stacks;
3429 win_g.h = hrh - 2 * border_width;
3431 h_inc = win->sh.width_inc;
3432 h_base = win->sh.base_width;
3434 h_inc = win->sh.height_inc;
3435 h_base = win->sh.base_height;
3437 if (j == colno - 1) {
3438 win_g.h = hrh + extra;
3439 } else if (h_inc > 1 && h_inc < h_slice) {
3440 /* adjust for window's requested size increment */
3441 remain = (win_g.h - h_base) % h_inc;
3442 missing = h_inc - remain;
3444 if (missing <= extra || j == 0) {
3456 win_g.y += last_h + 2 * border_width;
3458 if (disable_border && bar_enabled == 0 && winno == 1){
3460 win_g.w += 2 * border_width;
3461 win_g.h += 2 * border_width;
3466 if (X(win) != win_g.y || Y(win) != win_g.x ||
3467 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3471 WIDTH(win) = win_g.h;
3472 HEIGHT(win) = win_g.w;
3475 if (X(win) != win_g.x || Y(win) != win_g.y ||
3476 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3480 WIDTH(win) = win_g.w;
3481 HEIGHT(win) = win_g.h;
3485 if (bordered != win->bordered) {
3487 win->bordered = bordered;
3495 war = xcb_get_window_attributes_reply(conn,
3496 xcb_get_window_attributes(conn, win->id),
3499 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3500 map_window_raised(win->id);
3510 /* now, stack all the floaters and transients */
3511 TAILQ_FOREACH(win, &ws->winlist, entry) {
3512 if (win->transient == 0 && win->floating == 0)
3514 if (win->iconic == 1)
3516 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3521 stack_floater(win, ws->r);
3522 map_window_raised(win->id);
3526 stack_floater(fs_win, ws->r);
3527 map_window_raised(fs_win->id);
3532 vertical_config(struct workspace *ws, int id)
3534 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3538 case SWM_ARG_ID_STACKRESET:
3539 case SWM_ARG_ID_STACKINIT:
3540 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3541 ws->l_state.vertical_mwin = 1;
3542 ws->l_state.vertical_stacks = 1;
3544 case SWM_ARG_ID_MASTERSHRINK:
3545 if (ws->l_state.vertical_msize > 1)
3546 ws->l_state.vertical_msize--;
3548 case SWM_ARG_ID_MASTERGROW:
3549 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3550 ws->l_state.vertical_msize++;
3552 case SWM_ARG_ID_MASTERADD:
3553 ws->l_state.vertical_mwin++;
3555 case SWM_ARG_ID_MASTERDEL:
3556 if (ws->l_state.vertical_mwin > 0)
3557 ws->l_state.vertical_mwin--;
3559 case SWM_ARG_ID_STACKINC:
3560 ws->l_state.vertical_stacks++;
3562 case SWM_ARG_ID_STACKDEC:
3563 if (ws->l_state.vertical_stacks > 1)
3564 ws->l_state.vertical_stacks--;
3566 case SWM_ARG_ID_FLIPLAYOUT:
3567 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3575 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3577 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3579 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3583 horizontal_config(struct workspace *ws, int id)
3585 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3588 case SWM_ARG_ID_STACKRESET:
3589 case SWM_ARG_ID_STACKINIT:
3590 ws->l_state.horizontal_mwin = 1;
3591 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3592 ws->l_state.horizontal_stacks = 1;
3594 case SWM_ARG_ID_MASTERSHRINK:
3595 if (ws->l_state.horizontal_msize > 1)
3596 ws->l_state.horizontal_msize--;
3598 case SWM_ARG_ID_MASTERGROW:
3599 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3600 ws->l_state.horizontal_msize++;
3602 case SWM_ARG_ID_MASTERADD:
3603 ws->l_state.horizontal_mwin++;
3605 case SWM_ARG_ID_MASTERDEL:
3606 if (ws->l_state.horizontal_mwin > 0)
3607 ws->l_state.horizontal_mwin--;
3609 case SWM_ARG_ID_STACKINC:
3610 ws->l_state.horizontal_stacks++;
3612 case SWM_ARG_ID_STACKDEC:
3613 if (ws->l_state.horizontal_stacks > 1)
3614 ws->l_state.horizontal_stacks--;
3616 case SWM_ARG_ID_FLIPLAYOUT:
3617 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3625 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3627 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3629 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3632 /* fullscreen view */
3634 max_stack(struct workspace *ws, struct swm_geometry *g)
3636 struct swm_geometry gg = *g;
3637 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3638 int winno, num_screens;
3640 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3645 winno = count_win(ws, 0);
3646 if (winno == 0 && count_win(ws, 1) == 0)
3649 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3650 TAILQ_FOREACH(win, &ws->winlist, entry) {
3651 if (win->transient) {
3653 parent = find_window(win->transient);
3657 if (win->floating && win->floatmaxed == 0 ) {
3659 * retain geometry for retrieval on exit from
3662 store_float_geom(win, ws->r);
3663 win->floatmaxed = 1;
3666 /* only reconfigure if necessary */
3667 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3668 HEIGHT(win) != gg.h) {
3674 WIDTH(win) += 2 * border_width;
3675 HEIGHT(win) += 2 * border_width;
3680 /* unmap only if we don't have multi screen */
3681 if (win != ws->focus)
3682 if (!(num_screens > 1 || outputs > 1))
3686 /* put the last transient on top */
3689 map_window_raised(parent->id);
3690 stack_floater(wintrans, ws->r);
3691 focus_magic(wintrans);
3696 send_to_ws(struct swm_region *r, union arg *args)
3698 int wsid = args->id;
3699 struct ws_win *win = NULL, *parent;
3700 struct workspace *ws, *nws;
3701 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3702 char ws_idx_str[SWM_PROPLEN];
3705 if (wsid >= workspace_limit)
3708 if (r && r->ws && r->ws->focus)
3714 if (win->ws->idx == wsid)
3717 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3720 nws = &win->s->ws[wsid];
3722 a.id = SWM_ARG_ID_FOCUSPREV;
3724 if (win->transient) {
3725 parent = find_window(win->transient);
3727 unmap_window(parent);
3728 TAILQ_REMOVE(&ws->winlist, parent, entry);
3729 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3734 TAILQ_REMOVE(&ws->winlist, win, entry);
3735 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3736 if (TAILQ_EMPTY(&ws->winlist))
3737 r->ws->focus = NULL;
3740 /* Try to update the window's workspace property */
3741 ws_idx_atom = get_atom_from_string("_SWM_WS");
3743 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3745 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3747 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3748 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3757 pressbutton(struct swm_region *r, union arg *args)
3759 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3760 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3764 raise_toggle(struct swm_region *r, union arg *args)
3766 if (r == NULL || r->ws == NULL)
3769 r->ws->always_raise = !r->ws->always_raise;
3771 /* bring floaters back to top */
3772 if (r->ws->always_raise == 0)
3777 iconify(struct swm_region *r, union arg *args)
3781 if (r->ws->focus == NULL)
3783 unmap_window(r->ws->focus);
3784 update_iconic(r->ws->focus, 1);
3786 if (focus_mode == SWM_FOCUS_DEFAULT)
3787 drain_enter_notify();
3788 r->ws->focus = NULL;
3789 a.id = SWM_ARG_ID_FOCUSCUR;
3794 get_win_name(xcb_window_t win)
3797 xcb_get_property_cookie_t c;
3798 xcb_icccm_get_text_property_reply_t r;
3800 c = xcb_icccm_get_wm_name(conn, win);
3801 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3802 if (r.name_len > 0) {
3803 name = malloc(r.name_len + 1);
3805 xcb_icccm_get_text_property_reply_wipe(&r);
3808 memcpy(name, r.name, r.name_len);
3809 name[r.name_len] = '\0';
3811 xcb_icccm_get_text_property_reply_wipe(&r);
3818 uniconify(struct swm_region *r, union arg *args)
3825 DNPRINTF(SWM_D_MISC, "uniconify\n");
3827 if (r == NULL || r->ws == NULL)
3830 /* make sure we have anything to uniconify */
3831 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3832 if (win->ws == NULL)
3833 continue; /* should never happen */
3834 if (win->iconic == 0)
3842 search_resp_action = SWM_SEARCH_UNICONIFY;
3844 spawn_select(r, args, "search", &searchpid);
3846 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3849 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3850 if (win->ws == NULL)
3851 continue; /* should never happen */
3852 if (win->iconic == 0)
3855 name = get_win_name(win->id);
3858 fprintf(lfile, "%s.%u\n", name, win->id);
3866 name_workspace(struct swm_region *r, union arg *args)
3870 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3876 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3878 spawn_select(r, args, "name_workspace", &searchpid);
3880 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3883 fprintf(lfile, "%s", "");
3888 search_workspace(struct swm_region *r, union arg *args)
3891 struct workspace *ws;
3894 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3900 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3902 spawn_select(r, args, "search", &searchpid);
3904 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3907 for (i = 0; i < workspace_limit; i++) {
3911 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3912 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3919 search_win_cleanup(void)
3921 struct search_window *sw = NULL;
3923 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3924 xcb_destroy_window(conn, sw->indicator);
3925 xcb_free_gc(conn, sw->gc);
3926 TAILQ_REMOVE(&search_wl, sw, entry);
3932 search_win(struct swm_region *r, union arg *args)
3934 struct ws_win *win = NULL;
3935 struct search_window *sw = NULL;
3937 uint32_t gcv[1], wa[2];
3942 XRectangle ibox, lbox;
3943 xcb_screen_t *screen;
3944 DNPRINTF(SWM_D_MISC, "search_win\n");
3947 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3949 spawn_select(r, args, "search", &searchpid);
3951 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3954 TAILQ_INIT(&search_wl);
3957 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3958 if (win->iconic == 1)
3961 sw = calloc(1, sizeof(struct search_window));
3963 warn("search_win: calloc");
3965 search_win_cleanup();
3970 screen = get_screen(sw->idx);
3972 snprintf(s, sizeof s, "%d", i);
3975 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3977 w = xcb_generate_id(conn);
3978 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3979 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3980 xcb_create_window(conn, screen->root_depth, w, win->id,
3981 0, 0, lbox.width + 4,
3982 bar_fs_extents->max_logical_extent.height, 1,
3983 XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual,
3984 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3987 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3989 sw->gc = xcb_generate_id(conn);
3990 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3991 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND, gcv);
3992 map_window_raised(w);
3994 xcb_image_text_8(conn, len, w, sw->gc, 2,
3995 (bar_fs_extents->max_logical_extent.height -
3996 lbox.height) / 2 - lbox.y, s);
3998 fprintf(lfile, "%d\n", i);
4006 search_resp_uniconify(char *resp, unsigned long len)
4012 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4014 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4015 if (win->iconic == 0)
4017 name = get_win_name(win->id);
4020 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4025 if (strncmp(s, resp, len) == 0) {
4026 /* XXX this should be a callback to generalize */
4027 update_iconic(win, 0);
4036 search_resp_name_workspace(char *resp, unsigned long len)
4038 struct workspace *ws;
4040 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4042 if (search_r->ws == NULL)
4047 free(search_r->ws->name);
4048 search_r->ws->name = NULL;
4052 ws->name = strdup(resp);
4053 if (ws->name == NULL) {
4054 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4055 "strdup: %s", strerror(errno));
4062 search_resp_search_workspace(char *resp, unsigned long len)
4069 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4073 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4080 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
4082 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4089 switchws(search_r, &a);
4093 search_resp_search_window(char *resp, unsigned long len)
4098 struct search_window *sw;
4100 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4104 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4109 idx = strtonum(s, 1, INT_MAX, &errstr);
4111 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4118 TAILQ_FOREACH(sw, &search_wl, entry)
4119 if (idx == sw->idx) {
4125 #define MAX_RESP_LEN 1024
4128 search_do_resp(void)
4134 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4139 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4140 warn("search: calloc");
4144 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4146 warn("search: read error");
4149 resp[rbytes] = '\0';
4152 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4153 * newline, so work around that by sanitizing the resp now.
4155 resp[strcspn(resp, "\n")] = '\0';
4158 switch (search_resp_action) {
4159 case SWM_SEARCH_UNICONIFY:
4160 search_resp_uniconify(resp, len);
4162 case SWM_SEARCH_NAME_WORKSPACE:
4163 search_resp_name_workspace(resp, len);
4165 case SWM_SEARCH_SEARCH_WORKSPACE:
4166 search_resp_search_workspace(resp, len);
4168 case SWM_SEARCH_SEARCH_WINDOW:
4169 search_resp_search_window(resp, len);
4174 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4175 search_win_cleanup();
4177 search_resp_action = SWM_SEARCH_NONE;
4178 close(select_resp_pipe[0]);
4183 wkill(struct swm_region *r, union arg *args)
4185 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4187 if (r->ws->focus == NULL)
4190 if (args->id == SWM_ARG_ID_KILLWINDOW)
4191 xcb_kill_client(conn, r->ws->focus->id);
4193 if (r->ws->focus->can_delete)
4194 client_msg(r->ws->focus, adelete);
4199 floating_toggle_win(struct ws_win *win)
4201 struct swm_region *r;
4211 /* reject floating toggles in max stack mode */
4212 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4215 if (win->floating) {
4216 if (!win->floatmaxed) {
4217 /* retain position for refloat */
4218 store_float_geom(win, r);
4222 if (win->g_floatvalid) {
4223 /* refloat at last floating relative position */
4224 X(win) = win->g_float.x + X(r);
4225 Y(win) = win->g_float.y + Y(r);
4226 WIDTH(win) = win->g_float.w;
4227 HEIGHT(win) = win->g_float.h;
4232 ewmh_update_actions(win);
4238 floating_toggle(struct swm_region *r, union arg *args)
4240 struct ws_win *win = r->ws->focus;
4246 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4247 _NET_WM_STATE_TOGGLE);
4250 if (focus_mode == SWM_FOCUS_DEFAULT)
4251 drain_enter_notify();
4253 if (win == win->ws->focus) {
4254 a.id = SWM_ARG_ID_FOCUSCUR;
4255 focus(win->ws->r, &a);
4260 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4262 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4264 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4266 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4269 if (X(win) + BORDER(win) < X(r)) {
4271 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4273 X(win) = X(r) - BORDER(win);
4276 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4278 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4280 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4283 if (Y(win) + BORDER(win) < Y(r)) {
4285 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4287 Y(win) = Y(r) - BORDER(win);
4293 if (HEIGHT(win) < 1)
4299 update_window(struct ws_win *win)
4304 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4305 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4306 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4310 wc[3] = HEIGHT(win);
4311 wc[4] = BORDER(win);
4313 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4314 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4315 wc[3], YESNO(win->bordered));
4317 xcb_configure_window(conn, win->id, mask, wc);
4320 #define SWM_RESIZE_STEPS (50)
4323 resize(struct ws_win *win, union arg *args)
4327 struct swm_region *r = NULL;
4328 int resize_step = 0;
4329 struct swm_geometry g;
4330 int top = 0, left = 0;
4332 unsigned int shape; /* cursor style */
4333 xcb_cursor_t cursor;
4334 xcb_font_t cursor_font;
4335 xcb_grab_pointer_reply_t *gpr;
4336 xcb_query_pointer_reply_t *xpr;
4342 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4345 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%x, floating: %s, "
4346 "transient: 0x%x\n", win->id, YESNO(win->floating),
4349 if (!(win->transient != 0 || win->floating != 0))
4352 /* reject resizes in max mode for floaters (transient ok) */
4353 if (win->floatmaxed)
4357 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4363 case SWM_ARG_ID_WIDTHSHRINK:
4364 WIDTH(win) -= SWM_RESIZE_STEPS;
4367 case SWM_ARG_ID_WIDTHGROW:
4368 WIDTH(win) += SWM_RESIZE_STEPS;
4371 case SWM_ARG_ID_HEIGHTSHRINK:
4372 HEIGHT(win) -= SWM_RESIZE_STEPS;
4375 case SWM_ARG_ID_HEIGHTGROW:
4376 HEIGHT(win) += SWM_RESIZE_STEPS;
4383 constrain_window(win, r, 1);
4385 store_float_geom(win,r);
4389 if (focus_mode == SWM_FOCUS_DEFAULT)
4390 drain_enter_notify();
4392 /* get cursor offset from window root */
4393 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4400 if (xpr->win_x < WIDTH(win) / 2)
4403 if (xpr->win_y < HEIGHT(win) / 2)
4406 if (args->id == SWM_ARG_ID_CENTER)
4409 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4411 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4413 cursor_font = xcb_generate_id(conn);
4414 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4416 cursor = xcb_generate_id(conn);
4417 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4418 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4420 gpr = xcb_grab_pointer_reply(conn,
4421 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4422 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
4423 cursor, XCB_CURRENT_TIME),
4426 xcb_free_cursor(conn, cursor);
4427 xcb_close_font(conn, cursor_font);
4433 XMaskEvent(display, MOUSEMASK | ExposureMask |
4434 SubstructureRedirectMask, &ev);
4436 case ConfigureRequest:
4439 handler[ev.type](&ev);
4442 /* cursor offset/delta from start of the operation */
4443 dx = ev.xmotion.x_root - xpr->root_x;
4444 dy = ev.xmotion.y_root - xpr->root_y;
4450 if (args->id == SWM_ARG_ID_CENTER) {
4451 if (g.h / 2 + dy < 1)
4455 HEIGHT(win) = g.h + 2 * dy;
4463 HEIGHT(win) = g.h + dy;
4470 if (args->id == SWM_ARG_ID_CENTER) {
4471 if (g.w / 2 + dx < 1)
4475 WIDTH(win) = g.w + 2 * dx;
4483 WIDTH(win) = g.w + dx;
4486 constrain_window(win, r, 1);
4488 /* not free, don't sync more than 120 times / second */
4489 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4490 time = ev.xmotion.time;
4496 } while (ev.type != ButtonRelease);
4501 store_float_geom(win,r);
4503 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4504 xcb_free_cursor(conn, cursor);
4505 xcb_close_font(conn, cursor_font);
4510 drain_enter_notify();
4514 resize_step(struct swm_region *r, union arg *args)
4516 struct ws_win *win = NULL;
4518 if (r && r->ws && r->ws->focus)
4526 #define SWM_MOVE_STEPS (50)
4529 move(struct ws_win *win, union arg *args)
4534 struct swm_region *r = NULL;
4535 xcb_font_t cursor_font;
4536 xcb_cursor_t cursor;
4537 xcb_grab_pointer_reply_t *gpr;
4538 xcb_query_pointer_reply_t *qpr;
4544 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4547 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%x, floating: %s, transient: "
4548 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4550 /* in max_stack mode should only move transients */
4551 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4555 if (win->floating == 0 && !win->transient) {
4556 store_float_geom(win, r);
4557 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4560 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4567 case SWM_ARG_ID_MOVELEFT:
4568 X(win) -= (SWM_MOVE_STEPS - border_width);
4571 case SWM_ARG_ID_MOVERIGHT:
4572 X(win) += (SWM_MOVE_STEPS - border_width);
4575 case SWM_ARG_ID_MOVEUP:
4576 Y(win) -= (SWM_MOVE_STEPS - border_width);
4579 case SWM_ARG_ID_MOVEDOWN:
4580 Y(win) += (SWM_MOVE_STEPS - border_width);
4587 constrain_window(win, r, 0);
4589 store_float_geom(win, r);
4593 cursor_font = xcb_generate_id(conn);
4594 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4596 cursor = xcb_generate_id(conn);
4597 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4598 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4600 gpr = xcb_grab_pointer_reply(conn,
4601 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4602 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4603 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME),
4606 xcb_free_cursor(conn, cursor);
4607 xcb_close_font(conn, cursor_font);
4611 /* get cursor offset from window root */
4612 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4615 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4616 xcb_free_cursor(conn, cursor);
4617 xcb_close_font(conn, cursor_font);
4621 XMaskEvent(display, MOUSEMASK | ExposureMask |
4622 SubstructureRedirectMask, &ev);
4624 case ConfigureRequest:
4627 handler[ev.type](&ev);
4630 X(win) = ev.xmotion.x_root - qpr->win_x - border_width;
4631 Y(win) = ev.xmotion.y_root - qpr->win_y - border_width;
4633 constrain_window(win, r, 0);
4635 /* not free, don't sync more than 120 times / second */
4636 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4637 time = ev.xmotion.time;
4643 } while (ev.type != ButtonRelease);
4648 store_float_geom(win, r);
4650 xcb_free_cursor(conn, cursor);
4651 xcb_close_font(conn, cursor_font);
4652 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4655 drain_enter_notify();
4659 move_step(struct swm_region *r, union arg *args)
4661 struct ws_win *win = NULL;
4663 if (r && r->ws && r->ws->focus)
4668 if (!(win->transient != 0 || win->floating != 0))
4675 /* user/key callable function IDs */
4764 kf_search_workspace,
4766 kf_dumpwins, /* MUST BE LAST */
4770 /* key definitions */
4772 dummykeyfunc(struct swm_region *r, union arg *args)
4777 char name[SWM_FUNCNAME_LEN];
4778 void (*func)(struct swm_region *r, union arg *);
4780 } keyfuncs[kf_invalid + 1] = {
4781 /* name function argument */
4782 { "cycle_layout", cycle_layout, {0} },
4783 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4784 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4785 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4786 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4787 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4788 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4789 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4790 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4791 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4792 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4793 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4794 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4795 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4796 { "quit", quit, {0} },
4797 { "restart", restart, {0} },
4798 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4799 { "ws_1", switchws, {.id = 0} },
4800 { "ws_2", switchws, {.id = 1} },
4801 { "ws_3", switchws, {.id = 2} },
4802 { "ws_4", switchws, {.id = 3} },
4803 { "ws_5", switchws, {.id = 4} },
4804 { "ws_6", switchws, {.id = 5} },
4805 { "ws_7", switchws, {.id = 6} },
4806 { "ws_8", switchws, {.id = 7} },
4807 { "ws_9", switchws, {.id = 8} },
4808 { "ws_10", switchws, {.id = 9} },
4809 { "ws_11", switchws, {.id = 10} },
4810 { "ws_12", switchws, {.id = 11} },
4811 { "ws_13", switchws, {.id = 12} },
4812 { "ws_14", switchws, {.id = 13} },
4813 { "ws_15", switchws, {.id = 14} },
4814 { "ws_16", switchws, {.id = 15} },
4815 { "ws_17", switchws, {.id = 16} },
4816 { "ws_18", switchws, {.id = 17} },
4817 { "ws_19", switchws, {.id = 18} },
4818 { "ws_20", switchws, {.id = 19} },
4819 { "ws_21", switchws, {.id = 20} },
4820 { "ws_22", switchws, {.id = 21} },
4821 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4822 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4823 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4824 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4825 { "ws_prior", priorws, {0} },
4826 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4827 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4828 { "mvws_1", send_to_ws, {.id = 0} },
4829 { "mvws_2", send_to_ws, {.id = 1} },
4830 { "mvws_3", send_to_ws, {.id = 2} },
4831 { "mvws_4", send_to_ws, {.id = 3} },
4832 { "mvws_5", send_to_ws, {.id = 4} },
4833 { "mvws_6", send_to_ws, {.id = 5} },
4834 { "mvws_7", send_to_ws, {.id = 6} },
4835 { "mvws_8", send_to_ws, {.id = 7} },
4836 { "mvws_9", send_to_ws, {.id = 8} },
4837 { "mvws_10", send_to_ws, {.id = 9} },
4838 { "mvws_11", send_to_ws, {.id = 10} },
4839 { "mvws_12", send_to_ws, {.id = 11} },
4840 { "mvws_13", send_to_ws, {.id = 12} },
4841 { "mvws_14", send_to_ws, {.id = 13} },
4842 { "mvws_15", send_to_ws, {.id = 14} },
4843 { "mvws_16", send_to_ws, {.id = 15} },
4844 { "mvws_17", send_to_ws, {.id = 16} },
4845 { "mvws_18", send_to_ws, {.id = 17} },
4846 { "mvws_19", send_to_ws, {.id = 18} },
4847 { "mvws_20", send_to_ws, {.id = 19} },
4848 { "mvws_21", send_to_ws, {.id = 20} },
4849 { "mvws_22", send_to_ws, {.id = 21} },
4850 { "bar_toggle", bar_toggle, {0} },
4851 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4852 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4853 { "float_toggle", floating_toggle,{0} },
4854 { "version", version, {0} },
4855 { "spawn_custom", dummykeyfunc, {0} },
4856 { "iconify", iconify, {0} },
4857 { "uniconify", uniconify, {0} },
4858 { "raise_toggle", raise_toggle, {0} },
4859 { "button2", pressbutton, {2} },
4860 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4861 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4862 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4863 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4864 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4865 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4866 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4867 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4868 { "name_workspace", name_workspace, {0} },
4869 { "search_workspace", search_workspace, {0} },
4870 { "search_win", search_win, {0} },
4871 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4872 { "invalid key func", NULL, {0} },
4875 RB_ENTRY(key) entry;
4878 enum keyfuncid funcid;
4881 RB_HEAD(key_tree, key);
4884 key_cmp(struct key *kp1, struct key *kp2)
4886 if (kp1->keysym < kp2->keysym)
4888 if (kp1->keysym > kp2->keysym)
4891 if (kp1->mod < kp2->mod)
4893 if (kp1->mod > kp2->mod)
4899 RB_GENERATE(key_tree, key, entry, key_cmp);
4900 struct key_tree keys;
4903 enum { client_click, root_click };
4905 unsigned int action;
4907 unsigned int button;
4908 void (*func)(struct ws_win *, union arg *);
4911 /* action key mouse button func args */
4912 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4913 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4914 { client_click, MODKEY, Button1, move, {0} },
4918 update_modkey(unsigned int mod)
4924 RB_FOREACH(kp, key_tree, &keys)
4925 if (kp->mod & ShiftMask)
4926 kp->mod = mod | ShiftMask;
4930 for (i = 0; i < LENGTH(buttons); i++)
4931 if (buttons[i].mask & ShiftMask)
4932 buttons[i].mask = mod | ShiftMask;
4934 buttons[i].mask = mod;
4939 TAILQ_ENTRY(spawn_prog) entry;
4944 TAILQ_HEAD(spawn_list, spawn_prog);
4945 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4948 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4951 struct spawn_prog *prog = NULL;
4953 char *ap, **real_args;
4955 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4958 TAILQ_FOREACH(prog, &spawns, entry) {
4959 if (!strcasecmp(spawn_name, prog->name))
4963 warnx("spawn_custom: program %s not found", spawn_name);
4967 /* make room for expanded args */
4968 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4969 err(1, "spawn_custom: calloc real_args");
4971 /* expand spawn_args into real_args */
4972 for (i = 0; i < prog->argc; i++) {
4974 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4975 if (!strcasecmp(ap, "$bar_border")) {
4977 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4979 err(1, "spawn_custom border color");
4980 } else if (!strcasecmp(ap, "$bar_color")) {
4982 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4984 err(1, "spawn_custom bar color");
4985 } else if (!strcasecmp(ap, "$bar_font")) {
4986 if ((real_args[i] = strdup(bar_fonts))
4988 err(1, "spawn_custom bar fonts");
4989 } else if (!strcasecmp(ap, "$bar_font_color")) {
4991 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4993 err(1, "spawn_custom color font");
4994 } else if (!strcasecmp(ap, "$color_focus")) {
4996 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4998 err(1, "spawn_custom color focus");
4999 } else if (!strcasecmp(ap, "$color_unfocus")) {
5001 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5003 err(1, "spawn_custom color unfocus");
5005 /* no match --> copy as is */
5006 if ((real_args[i] = strdup(ap)) == NULL)
5007 err(1, "spawn_custom strdup(ap)");
5009 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5014 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5015 for (i = 0; i < prog->argc; i++)
5016 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5017 DNPRINTF(SWM_D_SPAWN, "\n");
5019 *ret_args = real_args;
5020 return (prog->argc);
5024 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5030 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5034 spawn(r->ws->idx, &a, 1);
5036 for (i = 0; i < spawn_argc; i++)
5042 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5048 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5052 if (pipe(select_list_pipe) == -1)
5053 err(1, "pipe error");
5054 if (pipe(select_resp_pipe) == -1)
5055 err(1, "pipe error");
5057 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5058 err(1, "could not disable SIGPIPE");
5059 switch (*pid = fork()) {
5061 err(1, "cannot fork");
5064 if (dup2(select_list_pipe[0], 0) == -1)
5066 if (dup2(select_resp_pipe[1], 1) == -1)
5068 close(select_list_pipe[1]);
5069 close(select_resp_pipe[0]);
5070 spawn(r->ws->idx, &a, 0);
5072 default: /* parent */
5073 close(select_list_pipe[0]);
5074 close(select_resp_pipe[1]);
5078 for (i = 0; i < spawn_argc; i++)
5084 spawn_insert(char *name, char *args)
5086 char *arg, *cp, *ptr;
5087 struct spawn_prog *sp;
5089 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5091 if ((sp = calloc(1, sizeof *sp)) == NULL)
5092 err(1, "spawn_insert: malloc");
5093 if ((sp->name = strdup(name)) == NULL)
5094 err(1, "spawn_insert: strdup");
5096 /* convert the arguments to an argument list */
5097 if ((ptr = cp = strdup(args)) == NULL)
5098 err(1, "spawn_insert: strdup");
5099 while ((arg = strsep(&ptr, " \t")) != NULL) {
5100 /* empty field; skip it */
5105 if ((sp->argv = realloc(sp->argv, sp->argc *
5106 sizeof *sp->argv)) == NULL)
5107 err(1, "spawn_insert: realloc");
5108 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5109 err(1, "spawn_insert: strdup");
5113 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5114 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5118 spawn_remove(struct spawn_prog *sp)
5122 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5124 TAILQ_REMOVE(&spawns, sp, entry);
5125 for (i = 0; i < sp->argc; i++)
5131 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5135 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5137 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5140 spawn_insert(name, args);
5142 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5146 setspawn(char *name, char *args)
5148 struct spawn_prog *sp;
5150 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5155 TAILQ_FOREACH(sp, &spawns, entry) {
5156 if (!strcmp(sp->name, name)) {
5160 spawn_replace(sp, name, args);
5161 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5165 if (*args == '\0') {
5166 warnx("error: setspawn: cannot find program: %s", name);
5170 spawn_insert(name, args);
5171 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5175 setconfspawn(char *selector, char *value, int flags)
5177 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5179 setspawn(selector, value);
5181 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5188 setconfspawn("term", "xterm", 0);
5189 setconfspawn("spawn_term", "xterm", 0);
5190 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5191 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5192 setconfspawn("lock", "xlock", 0);
5193 setconfspawn("initscr", "initscreen.sh", 0);
5194 setconfspawn("menu", "dmenu_run"
5197 " -nf $bar_font_color"
5199 " -sf $bar_color", 0);
5200 setconfspawn("search", "dmenu"
5204 " -nf $bar_font_color"
5206 " -sf $bar_color", 0);
5207 setconfspawn("name_workspace", "dmenu"
5211 " -nf $bar_font_color"
5213 " -sf $bar_color", 0);
5217 #define SWM_MODNAME_SIZE 32
5218 #define SWM_KEY_WS "\n+ \t"
5220 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5224 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5225 if (mod == NULL || ks == NULL) {
5226 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5229 if (keystr == NULL || strlen(keystr) == 0) {
5230 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5236 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5237 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5239 cp += (long)strspn(cp, SWM_KEY_WS);
5240 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5242 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5244 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5246 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5248 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5250 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5252 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5253 *mod |= ControlMask;
5255 *ks = XStringToKeysym(name);
5256 XConvertCase(*ks, ks, &uks);
5257 if (ks == NoSymbol) {
5259 "parsekeys: invalid key %s\n",
5265 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5270 strdupsafe(char *str)
5275 return (strdup(str));
5279 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5283 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5284 keyfuncs[kfid].name, spawn_name);
5286 if ((kp = malloc(sizeof *kp)) == NULL)
5287 err(1, "key_insert: malloc");
5292 kp->spawn_name = strdupsafe(spawn_name);
5293 RB_INSERT(key_tree, &keys, kp);
5295 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5299 key_lookup(unsigned int mod, KeySym ks)
5306 return (RB_FIND(key_tree, &keys, &kp));
5310 key_remove(struct key *kp)
5312 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5314 RB_REMOVE(key_tree, &keys, kp);
5315 free(kp->spawn_name);
5318 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5322 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5325 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5329 key_insert(mod, ks, kfid, spawn_name);
5331 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5335 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5340 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5341 keyfuncs[kfid].name, spawn_name);
5343 if ((kp = key_lookup(mod, ks)) != NULL) {
5344 if (kfid == kf_invalid)
5347 key_replace(kp, mod, ks, kfid, spawn_name);
5348 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5351 if (kfid == kf_invalid) {
5352 warnx("error: setkeybinding: cannot find mod/key combination");
5353 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5357 key_insert(mod, ks, kfid, spawn_name);
5358 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5362 setconfbinding(char *selector, char *value, int flags)
5364 enum keyfuncid kfid;
5367 struct spawn_prog *sp;
5368 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5369 if (selector == NULL) {
5370 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5371 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5373 setkeybinding(mod, ks, kfid, NULL);
5378 /* search by key function name */
5379 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5380 if (strncasecmp(selector, keyfuncs[kfid].name,
5381 SWM_FUNCNAME_LEN) == 0) {
5382 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5384 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5385 setkeybinding(mod, ks, kfid, NULL);
5391 /* search by custom spawn name */
5392 TAILQ_FOREACH(sp, &spawns, entry) {
5393 if (strcasecmp(selector, sp->name) == 0) {
5394 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5396 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5397 setkeybinding(mod, ks, kf_spawn_custom,
5404 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5411 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5412 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5414 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5415 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5416 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5417 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5419 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5420 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5421 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5422 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5424 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5425 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5426 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5427 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5428 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5429 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5430 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5431 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5432 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5433 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5434 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5435 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5436 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5437 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5438 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5439 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5440 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5441 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5442 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5443 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5444 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5445 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5446 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5447 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5448 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5449 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5450 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5451 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5452 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5453 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5454 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5455 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5456 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5457 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5458 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5459 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5460 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5461 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5462 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5463 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5464 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5465 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5466 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5467 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5468 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5469 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5470 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5471 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5472 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5473 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5474 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5475 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5476 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5477 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5478 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5479 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5480 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5481 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5482 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5483 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5484 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5485 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5486 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5487 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5488 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5489 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5490 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5491 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5492 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5493 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5494 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5495 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5496 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5497 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5498 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5499 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5500 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5501 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5502 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5503 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5504 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5505 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5506 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5508 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5517 while (RB_EMPTY(&keys) == 0) {
5518 kp = RB_ROOT(&keys);
5524 setkeymapping(char *selector, char *value, int flags)
5526 char keymapping_file[PATH_MAX];
5527 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5528 if (value[0] == '~')
5529 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5530 pwd->pw_dir, &value[1]);
5532 strlcpy(keymapping_file, value, sizeof keymapping_file);
5534 /* load new key bindings; if it fails, revert to default bindings */
5535 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5539 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5544 updatenumlockmask(void)
5547 xcb_get_modifier_mapping_reply_t *modmap_r;
5548 xcb_keycode_t *modmap, kc;
5550 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5553 modmap_r = xcb_get_modifier_mapping_reply(conn,
5554 xcb_get_modifier_mapping(conn),
5557 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5558 for (i = 0; i < 8; i++) {
5559 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5560 kc = modmap[i * modmap_r->keycodes_per_modifier
5563 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5565 numlockmask = (1 << i);
5577 xcb_keycode_t *code;
5578 unsigned int modifiers[] =
5579 { 0, LockMask, numlockmask, numlockmask | LockMask };
5582 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5583 updatenumlockmask();
5585 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5586 for (k = 0; k < num_screens; k++) {
5587 if (TAILQ_EMPTY(&screens[k].rl))
5589 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5591 RB_FOREACH(kp, key_tree, &keys) {
5592 if ((code = xcb_key_symbols_get_keycode(syms,
5594 for (j = 0; j < LENGTH(modifiers); j++)
5595 xcb_grab_key(conn, True,
5597 kp->mod | modifiers[j],
5598 *code, XCB_GRAB_MODE_ASYNC,
5599 XCB_GRAB_MODE_ASYNC);
5605 grabbuttons(struct ws_win *win, int focused)
5608 unsigned int modifiers[] =
5609 { 0, LockMask, numlockmask, numlockmask|LockMask };
5611 updatenumlockmask();
5612 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5613 XCB_BUTTON_MASK_ANY);
5615 for (i = 0; i < LENGTH(buttons); i++)
5616 if (buttons[i].action == client_click)
5617 for (j = 0; j < LENGTH(modifiers); j++)
5618 xcb_grab_button(conn, False, win->id,
5620 XCB_GRAB_MODE_ASYNC,
5627 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5628 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC,
5629 XCB_WINDOW_NONE, XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY,
5630 XCB_BUTTON_MASK_ANY);
5633 const char *quirkname[] = {
5634 "NONE", /* config string for "no value" */
5643 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5644 #define SWM_Q_WS "\n|+ \t"
5646 parsequirks(char *qstr, unsigned long *quirk)
5656 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5658 cp += (long)strspn(cp, SWM_Q_WS);
5659 for (i = 0; i < LENGTH(quirkname); i++) {
5660 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5661 DNPRINTF(SWM_D_QUIRK,
5662 "parsequirks: %s\n", name);
5667 *quirk |= 1 << (i-1);
5671 if (i >= LENGTH(quirkname)) {
5672 DNPRINTF(SWM_D_QUIRK,
5673 "parsequirks: invalid quirk [%s]\n", name);
5681 quirk_insert(const char *class, const char *name, unsigned long quirk)
5685 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5688 if ((qp = malloc(sizeof *qp)) == NULL)
5689 err(1, "quirk_insert: malloc");
5690 if ((qp->class = strdup(class)) == NULL)
5691 err(1, "quirk_insert: strdup");
5692 if ((qp->name = strdup(name)) == NULL)
5693 err(1, "quirk_insert: strdup");
5696 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5698 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5702 quirk_remove(struct quirk *qp)
5704 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5705 qp->name, qp->quirk);
5707 TAILQ_REMOVE(&quirks, qp, entry);
5712 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5716 quirk_replace(struct quirk *qp, const char *class, const char *name,
5717 unsigned long quirk)
5719 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5720 qp->name, qp->quirk);
5723 quirk_insert(class, name, quirk);
5725 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5729 setquirk(const char *class, const char *name, unsigned long quirk)
5733 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5736 TAILQ_FOREACH(qp, &quirks, entry) {
5737 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5741 quirk_replace(qp, class, name, quirk);
5742 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5747 warnx("error: setquirk: cannot find class/name combination");
5751 quirk_insert(class, name, quirk);
5752 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5756 setconfquirk(char *selector, char *value, int flags)
5758 char *cp, *class, *name;
5760 unsigned long quirks;
5761 if (selector == NULL)
5763 if ((cp = strchr(selector, ':')) == NULL)
5768 if ((retval = parsequirks(value, &quirks)) == 0)
5769 setquirk(class, name, quirks);
5776 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5777 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5778 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5779 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5780 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5781 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5782 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5783 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5784 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5785 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5786 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5787 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5788 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5791 /* conf file stuff */
5792 #define SWM_CONF_FILE "spectrwm.conf"
5793 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5797 SWM_S_BAR_AT_BOTTOM,
5798 SWM_S_BAR_BORDER_WIDTH,
5805 SWM_S_CLOCK_ENABLED,
5808 SWM_S_CYCLE_VISIBLE,
5810 SWM_S_DISABLE_BORDER,
5812 SWM_S_FOCUS_CLOSE_WRAP,
5813 SWM_S_FOCUS_DEFAULT,
5819 SWM_S_STACK_ENABLED,
5821 SWM_S_TITLE_CLASS_ENABLED,
5822 SWM_S_TITLE_NAME_ENABLED,
5823 SWM_S_URGENT_ENABLED,
5824 SWM_S_VERBOSE_LAYOUT,
5825 SWM_S_WINDOW_NAME_ENABLED,
5826 SWM_S_WORKSPACE_LIMIT
5830 setconfvalue(char *selector, char *value, int flags)
5836 case SWM_S_BAR_ACTION:
5838 if ((bar_argv[0] = strdup(value)) == NULL)
5839 err(1, "setconfvalue: bar_action");
5841 case SWM_S_BAR_AT_BOTTOM:
5842 bar_at_bottom = atoi(value);
5844 case SWM_S_BAR_BORDER_WIDTH:
5845 bar_border_width = atoi(value);
5846 if (bar_border_width < 0)
5847 bar_border_width = 0;
5849 case SWM_S_BAR_DELAY:
5850 bar_delay = atoi(value);
5852 case SWM_S_BAR_ENABLED:
5853 bar_enabled = atoi(value);
5855 case SWM_S_BAR_FONT:
5857 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5858 err(1, "setconfvalue: asprintf: failed to allocate "
5859 "memory for bar_fonts.");
5863 case SWM_S_BAR_FORMAT:
5865 if ((bar_format = strdup(value)) == NULL)
5866 err(1, "setconfvalue: bar_format");
5868 case SWM_S_BAR_JUSTIFY:
5869 if (!strcmp(value, "left"))
5870 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5871 else if (!strcmp(value, "center"))
5872 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5873 else if (!strcmp(value, "right"))
5874 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5876 errx(1, "invalid bar_justify");
5878 case SWM_S_BORDER_WIDTH:
5879 border_width = atoi(value);
5880 if (border_width < 0)
5883 case SWM_S_CLOCK_ENABLED:
5884 clock_enabled = atoi(value);
5886 case SWM_S_CLOCK_FORMAT:
5887 #ifndef SWM_DENY_CLOCK_FORMAT
5889 if ((clock_format = strdup(value)) == NULL)
5890 err(1, "setconfvalue: clock_format");
5893 case SWM_S_CYCLE_EMPTY:
5894 cycle_empty = atoi(value);
5896 case SWM_S_CYCLE_VISIBLE:
5897 cycle_visible = atoi(value);
5899 case SWM_S_DIALOG_RATIO:
5900 dialog_ratio = atof(value);
5901 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5904 case SWM_S_DISABLE_BORDER:
5905 disable_border = atoi(value);
5907 case SWM_S_FOCUS_CLOSE:
5908 if (!strcmp(value, "first"))
5909 focus_close = SWM_STACK_BOTTOM;
5910 else if (!strcmp(value, "last"))
5911 focus_close = SWM_STACK_TOP;
5912 else if (!strcmp(value, "next"))
5913 focus_close = SWM_STACK_ABOVE;
5914 else if (!strcmp(value, "previous"))
5915 focus_close = SWM_STACK_BELOW;
5917 errx(1, "focus_close");
5919 case SWM_S_FOCUS_CLOSE_WRAP:
5920 focus_close_wrap = atoi(value);
5922 case SWM_S_FOCUS_DEFAULT:
5923 if (!strcmp(value, "last"))
5924 focus_default = SWM_STACK_TOP;
5925 else if (!strcmp(value, "first"))
5926 focus_default = SWM_STACK_BOTTOM;
5928 errx(1, "focus_default");
5930 case SWM_S_FOCUS_MODE:
5931 if (!strcmp(value, "default"))
5932 focus_mode = SWM_FOCUS_DEFAULT;
5933 else if (!strcmp(value, "follow_cursor"))
5934 focus_mode = SWM_FOCUS_FOLLOW;
5935 else if (!strcmp(value, "synergy"))
5936 focus_mode = SWM_FOCUS_SYNERGY;
5938 errx(1, "focus_mode");
5940 case SWM_S_SPAWN_ORDER:
5941 if (!strcmp(value, "first"))
5942 spawn_position = SWM_STACK_BOTTOM;
5943 else if (!strcmp(value, "last"))
5944 spawn_position = SWM_STACK_TOP;
5945 else if (!strcmp(value, "next"))
5946 spawn_position = SWM_STACK_ABOVE;
5947 else if (!strcmp(value, "previous"))
5948 spawn_position = SWM_STACK_BELOW;
5950 errx(1, "spawn_position");
5952 case SWM_S_SPAWN_TERM:
5953 setconfspawn("term", value, 0);
5954 setconfspawn("spawn_term", value, 0);
5958 case SWM_S_SS_ENABLED:
5959 ss_enabled = atoi(value);
5961 case SWM_S_STACK_ENABLED:
5962 stack_enabled = atoi(value);
5964 case SWM_S_TERM_WIDTH:
5965 term_width = atoi(value);
5969 case SWM_S_TITLE_CLASS_ENABLED:
5970 title_class_enabled = atoi(value);
5972 case SWM_S_TITLE_NAME_ENABLED:
5973 title_name_enabled = atoi(value);
5975 case SWM_S_URGENT_ENABLED:
5976 urgent_enabled = atoi(value);
5978 case SWM_S_VERBOSE_LAYOUT:
5979 verbose_layout = atoi(value);
5980 for (i = 0; layouts[i].l_stack != NULL; i++) {
5982 layouts[i].l_string = fancy_stacker;
5984 layouts[i].l_string = plain_stacker;
5987 case SWM_S_WINDOW_NAME_ENABLED:
5988 window_name_enabled = atoi(value);
5990 case SWM_S_WORKSPACE_LIMIT:
5991 workspace_limit = atoi(value);
5992 if (workspace_limit > SWM_WS_MAX)
5993 workspace_limit = SWM_WS_MAX;
5994 else if (workspace_limit < 1)
5995 workspace_limit = 1;
6004 setconfmodkey(char *selector, char *value, int flags)
6006 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6007 update_modkey(Mod1Mask);
6008 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6009 update_modkey(Mod2Mask);
6010 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6011 update_modkey(Mod3Mask);
6012 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6013 update_modkey(Mod4Mask);
6020 setconfcolor(char *selector, char *value, int flags)
6022 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6027 setconfregion(char *selector, char *value, int flags)
6029 custom_region(value);
6034 setautorun(char *selector, char *value, int flags)
6044 if (getenv("SWM_STARTED"))
6048 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6049 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6051 if (ws_id < 0 || ws_id >= workspace_limit)
6052 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6055 * This is a little intricate
6057 * If the pid already exists we simply reuse it because it means it was
6058 * used before AND not claimed by manage_window. We get away with
6059 * altering it in the parent after INSERT because this can not be a race
6062 while ((ap = strsep(&sp, " \t")) != NULL) {
6065 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6067 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6068 err(1, "setautorun: realloc");
6069 a.argv[argc - 1] = ap;
6072 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6073 err(1, "setautorun: realloc");
6074 a.argv[argc] = NULL;
6076 if ((pid = fork()) == 0) {
6077 spawn(ws_id, &a, 1);
6086 p = calloc(1, sizeof *p);
6089 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6099 setlayout(char *selector, char *value, int flags)
6101 int ws_id, i, x, mg, ma, si, raise, f = 0;
6102 int st = SWM_V_STACK, num_screens;
6104 struct workspace *ws;
6106 if (getenv("SWM_STARTED"))
6110 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6111 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6112 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6113 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6116 if (ws_id < 0 || ws_id >= workspace_limit)
6117 errx(1, "layout: invalid workspace %d", ws_id + 1);
6119 if (!strcasecmp(s, "vertical"))
6121 else if (!strcasecmp(s, "vertical_flip")) {
6124 } else if (!strcasecmp(s, "horizontal"))
6126 else if (!strcasecmp(s, "horizontal_flip")) {
6129 } else if (!strcasecmp(s, "fullscreen"))
6132 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6133 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6136 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6137 for (i = 0; i < num_screens; i++) {
6138 ws = (struct workspace *)&screens[i].ws;
6139 ws[ws_id].cur_layout = &layouts[st];
6141 ws[ws_id].always_raise = raise;
6142 if (st == SWM_MAX_STACK)
6146 for (x = 0; x < abs(mg); x++) {
6147 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6148 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6149 SWM_ARG_ID_MASTERSHRINK);
6153 for (x = 0; x < abs(ma); x++) {
6154 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6155 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6156 SWM_ARG_ID_MASTERDEL);
6160 for (x = 0; x < abs(si); x++) {
6161 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6162 si >= 0 ? SWM_ARG_ID_STACKINC :
6163 SWM_ARG_ID_STACKDEC);
6168 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6169 SWM_ARG_ID_FLIPLAYOUT);
6177 /* config options */
6178 struct config_option {
6180 int (*func)(char*, char*, int);
6183 struct config_option configopt[] = {
6184 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6185 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6186 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6187 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6188 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6189 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6190 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6191 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6192 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6193 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6194 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6195 { "keyboard_mapping", setkeymapping, 0 },
6196 { "bind", setconfbinding, 0 },
6197 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6198 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6199 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6200 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6201 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6202 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6203 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6204 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6205 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6206 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6207 { "modkey", setconfmodkey, 0 },
6208 { "program", setconfspawn, 0 },
6209 { "quirk", setconfquirk, 0 },
6210 { "region", setconfregion, 0 },
6211 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6212 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6213 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6214 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6215 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6216 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6217 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6218 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6219 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6220 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6221 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6222 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6223 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6224 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6225 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6226 { "autorun", setautorun, 0 },
6227 { "layout", setlayout, 0 },
6232 conf_load(char *filename, int keymapping)
6235 char *line, *cp, *optsub, *optval;
6236 size_t linelen, lineno = 0;
6237 int wordlen, i, optind;
6238 struct config_option *opt;
6240 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6242 if (filename == NULL) {
6243 warnx("conf_load: no filename");
6246 if ((config = fopen(filename, "r")) == NULL) {
6247 warn("conf_load: fopen: %s", filename);
6251 while (!feof(config)) {
6252 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6255 err(1, "%s", filename);
6260 cp += strspn(cp, " \t\n"); /* eat whitespace */
6261 if (cp[0] == '\0') {
6266 /* get config option */
6267 wordlen = strcspn(cp, "=[ \t\n");
6269 warnx("%s: line %zd: no option found",
6274 for (i = 0; i < LENGTH(configopt); i++) {
6275 opt = &configopt[i];
6276 if (!strncasecmp(cp, opt->optname, wordlen) &&
6277 strlen(opt->optname) == wordlen) {
6283 warnx("%s: line %zd: unknown option %.*s",
6284 filename, lineno, wordlen, cp);
6287 if (keymapping && strcmp(opt->optname, "bind")) {
6288 warnx("%s: line %zd: invalid option %.*s",
6289 filename, lineno, wordlen, cp);
6293 cp += strspn(cp, " \t\n"); /* eat whitespace */
6294 /* get [selector] if any */
6298 wordlen = strcspn(cp, "]");
6301 warnx("%s: line %zd: syntax error",
6306 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6308 warnx("%s: line %zd: unable to allocate"
6309 "memory for selector", filename,
6315 cp += strspn(cp, "] \t\n"); /* eat trailing */
6317 cp += strspn(cp, "= \t\n"); /* eat trailing */
6319 optval = strdup(cp);
6320 /* call function to deal with it all */
6321 if (configopt[optind].func(optsub, optval,
6322 configopt[optind].funcflags) != 0)
6323 errx(1, "%s: line %zd: invalid data for %s",
6324 filename, lineno, configopt[optind].optname);
6331 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6338 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6344 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6346 struct ws_win *parent, *w;
6347 struct swm_region *r;
6348 struct workspace *ws;
6349 xcb_icccm_wm_hints_t wmh;
6351 parent = find_window(win->transient);
6353 parent->child_trans = win;
6355 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6356 " for 0x%x trans 0x%x\n", win->id, win->transient);
6358 r = root_to_region(win->wa->root);
6360 /* parent doen't exist in our window list */
6361 TAILQ_FOREACH(w, &ws->winlist, entry) {
6362 if (xcb_icccm_get_wm_hints_reply(conn,
6363 xcb_icccm_get_wm_hints(conn, w->id),
6365 warnx("can't get hints for 0x%x", w->id);
6369 if (win->hints.window_group != wmh.window_group)
6372 w->child_trans = win;
6373 win->transient = w->id;
6375 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6376 "transient to 0x%x\n", win->transient);
6383 window_get_pid(xcb_window_t win)
6388 xcb_get_property_cookie_t pc;
6389 xcb_get_property_reply_t *pr;
6391 apid = get_atom_from_string("_NET_WM_PID");
6392 if (apid == XCB_ATOM_NONE)
6395 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6396 pr = xcb_get_property_reply(conn, pc, NULL);
6399 if (pr->type != XCB_ATOM_CARDINAL)
6402 ret = *(long *)xcb_get_property_value(pr);
6408 apid = get_atom_from_string("_SWM_PID");
6409 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6411 pr = xcb_get_property_reply(conn, pc, NULL);
6414 if (pr->type != XCB_ATOM_STRING)
6417 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6424 manage_window(xcb_window_t id)
6426 xcb_window_t trans = XCB_WINDOW_NONE;
6427 struct workspace *ws;
6428 struct ws_win *win, *ww;
6429 int i, ws_idx, border_me = 0;
6430 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6431 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6433 struct swm_region *r;
6437 uint32_t event_mask;
6439 xcb_get_property_reply_t *gpr;
6440 xcb_icccm_get_wm_protocols_reply_t wpr;
6442 if ((win = find_window(id)) != NULL)
6443 return (win); /* already being managed */
6445 /* see if we are on the unmanaged list */
6446 if ((win = find_unmanaged_window(id)) != NULL) {
6447 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6448 "window: 0x%x\n", win->id);
6449 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6451 set_child_transient(win, &trans);
6453 if (trans && (ww = find_window(trans)))
6454 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6455 else if ((ww = win->ws->focus) &&
6456 spawn_position == SWM_STACK_ABOVE)
6457 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6459 else if (ww && spawn_position == SWM_STACK_BELOW)
6460 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6461 else switch (spawn_position) {
6464 case SWM_STACK_ABOVE:
6465 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6467 case SWM_STACK_BOTTOM:
6468 case SWM_STACK_BELOW:
6469 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6472 ewmh_update_actions(win);
6476 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6477 err(1, "manage_window: calloc: failed to allocate memory for "
6483 /* see if we need to override the workspace */
6484 p = find_pid(window_get_pid(id));
6486 /* Get all the window data in one shot */
6487 ws_idx_atom = get_atom_from_string("_SWM_WS");
6489 gpr = xcb_get_property_reply(conn,
6490 xcb_get_property(conn, False, id, ws_idx_atom,
6491 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6494 proplen = xcb_get_property_value_length(gpr);
6496 prop = malloc(proplen + 1);
6499 xcb_get_property_value(gpr),
6501 prop[proplen] = '\0';
6507 win->wa = xcb_get_geometry_reply(conn,
6508 xcb_get_geometry(conn, id),
6510 xcb_icccm_get_wm_normal_hints_reply(conn,
6511 xcb_icccm_get_wm_normal_hints(conn, id),
6513 xcb_icccm_get_wm_hints_reply(conn,
6514 xcb_icccm_get_wm_hints(conn, id),
6516 xcb_icccm_get_wm_transient_for_reply(conn,
6517 xcb_icccm_get_wm_transient_for(conn, id),
6520 win->transient = trans;
6521 set_child_transient(win, &trans);
6522 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6523 "transient: 0x%x\n", win->id, win->transient);
6526 prot = get_atom_from_string("WM_PROTOCOLS");
6528 /* get supported protocols */
6529 if (xcb_icccm_get_wm_protocols_reply(conn,
6530 xcb_icccm_get_wm_protocols(conn, id, prot),
6532 for (i = 0; i < wpr.atoms_len; i++) {
6533 if (wpr.atoms[i] == takefocus)
6534 win->take_focus = 1;
6535 if (wpr.atoms[i] == adelete)
6536 win->can_delete = 1;
6538 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6541 win->iconic = get_iconic(win);
6544 * Figure out where to put the window. If it was previously assigned to
6545 * a workspace (either by spawn() or manually moving), and isn't
6546 * transient, * put it in the same workspace
6548 r = root_to_region(win->wa->root);
6550 ws = &r->s->ws[p->ws];
6551 TAILQ_REMOVE(&pidlist, p, entry);
6554 } else if (prop && win->transient == 0) {
6555 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6556 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6559 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6562 ws = &r->s->ws[ws_idx];
6565 /* this should launch transients in the same ws as parent */
6567 if ((ww = find_window(trans)) != NULL)
6573 warnx("manage_window: fix this "
6579 /* set up the window layout */
6582 win->s = r->s; /* this never changes */
6583 if (trans && (ww = find_window(trans)))
6584 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6585 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6586 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6588 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6589 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6590 else switch (spawn_position) {
6593 case SWM_STACK_ABOVE:
6594 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6596 case SWM_STACK_BOTTOM:
6597 case SWM_STACK_BELOW:
6598 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6601 /* ignore window border if there is one. */
6602 WIDTH(win) = win->wa->width;
6603 HEIGHT(win) = win->wa->height;
6604 X(win) = win->wa->x + win->wa->border_width;
6605 Y(win) = win->wa->y + win->wa->border_width;
6607 win->g_floatvalid = 0;
6608 win->floatmaxed = 0;
6609 win->ewmh_flags = 0;
6611 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6612 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6613 HEIGHT(win), ws->idx);
6615 constrain_window(win, r, 0);
6617 /* Set window properties so we can remember this after reincarnation */
6618 if (ws_idx_atom && prop == NULL &&
6619 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6621 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6623 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6624 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6630 ewmh_autoquirk(win);
6632 if (xcb_icccm_get_wm_class_reply(conn,
6633 xcb_icccm_get_wm_class(conn, win->id),
6635 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6636 win->ch.class_name, win->ch.instance_name);
6638 /* java is retarded so treat it special */
6639 if (strstr(win->ch.instance_name, "sun-awt")) {
6644 TAILQ_FOREACH(qp, &quirks, entry) {
6645 if (!strcmp(win->ch.class_name, qp->class) &&
6646 !strcmp(win->ch.instance_name, qp->name)) {
6647 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6648 "class: %s, name: %s\n", win->ch.class_name,
6649 win->ch.instance_name);
6650 if (qp->quirk & SWM_Q_FLOAT) {
6654 win->quirks = qp->quirk;
6659 /* alter window position if quirky */
6660 if (win->quirks & SWM_Q_ANYWHERE) {
6661 win->manual = 1; /* don't center the quirky windows */
6662 if (bar_enabled && Y(win) < bar_height)
6663 Y(win) = bar_height;
6664 if (WIDTH(win) + X(win) > WIDTH(r))
6665 X(win) = WIDTH(r) - WIDTH(win) - 2;
6669 /* Reset font sizes (the bruteforce way; no default keybinding). */
6670 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6671 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6672 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6673 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6674 fake_keypress(win, XK_KP_Add, ShiftMask);
6677 ewmh_get_win_state(win);
6678 ewmh_update_actions(win);
6679 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6684 X(win) -= border_width;
6685 Y(win) -= border_width;
6689 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6690 XCB_EVENT_MASK_PROPERTY_CHANGE |
6691 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6693 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6695 /* floaters need to be mapped if they are in the current workspace */
6696 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6697 map_window_raised(win->id);
6703 free_window(struct ws_win *win)
6705 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6710 /* needed for restart wm */
6711 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
6713 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6718 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6723 memset(win, 0xff, sizeof *win); /* XXX kill later */
6729 unmanage_window(struct ws_win *win)
6731 struct ws_win *parent;
6732 xcb_screen_t *screen;
6737 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6739 if (win->transient) {
6740 parent = find_window(win->transient);
6742 parent->child_trans = NULL;
6745 /* focus on root just in case */
6746 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6747 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6748 screen->root, XCB_CURRENT_TIME);
6752 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6753 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6757 focus_magic(struct ws_win *win)
6759 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6762 /* if there are no windows clear the status-bar */
6767 if (win->child_trans) {
6768 /* win = parent & has a transient so focus on that */
6770 focus_win(win->child_trans);
6771 if (win->child_trans->take_focus)
6772 client_msg(win, takefocus);
6774 /* make sure transient hasn't disappeared */
6775 if (validate_win(win->child_trans) == 0) {
6776 focus_win(win->child_trans);
6777 if (win->child_trans->take_focus)
6778 client_msg(win->child_trans, takefocus);
6780 win->child_trans = NULL;
6782 if (win->take_focus)
6783 client_msg(win, takefocus);
6789 if (win->take_focus)
6790 client_msg(win, takefocus);
6797 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6804 XKeyEvent *ev = &e->xkey;
6806 struct swm_region *r;
6808 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6809 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6811 if (keyfuncs[kp->funcid].func == NULL)
6814 r = root_to_region(ev->root);
6815 if (kp->funcid == kf_spawn_custom)
6816 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6818 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6822 buttonpress(XEvent *e)
6826 XButtonPressedEvent *ev = &e->xbutton;
6828 if ((win = find_window(ev->window)) == NULL)
6832 action = client_click;
6834 for (i = 0; i < LENGTH(buttons); i++)
6835 if (action == buttons[i].action && buttons[i].func &&
6836 buttons[i].button == ev->button &&
6837 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6838 buttons[i].func(win, &buttons[i].args);
6842 configurerequest(XEvent *e)
6844 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6850 if ((win = find_window(ev->window)) == NULL)
6851 if ((win = find_unmanaged_window(ev->window)) == NULL)
6855 if (ev->value_mask & XCB_CONFIG_WINDOW_X) {
6856 mask |= XCB_CONFIG_WINDOW_X;
6859 if (ev->value_mask & XCB_CONFIG_WINDOW_Y) {
6860 mask |= XCB_CONFIG_WINDOW_Y;
6863 if (ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6864 mask |= XCB_CONFIG_WINDOW_WIDTH;
6865 wc[i++] = ev->width;
6867 if (ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6868 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6869 wc[i++] = ev->height;
6871 if (ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6872 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6873 wc[i++] = ev->border_width;
6875 if (ev->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6876 mask |= XCB_CONFIG_WINDOW_SIBLING;
6877 wc[i++] = ev->above;
6879 if (ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6880 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6881 wc[i++] = ev->detail;
6884 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%lx, "
6885 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev->window,
6886 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6888 xcb_configure_window(conn, ev->window, mask, wc);
6889 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6890 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6891 win->g_float.x = ev->x - X(win->ws->r);
6892 win->g_float.y = ev->y - Y(win->ws->r);
6893 win->g_float.w = ev->width;
6894 win->g_float.h = ev->height;
6895 win->g_floatvalid = 1;
6897 if (win->floating) {
6898 win->g = win->g_float;
6899 win->g.x += X(win->ws->r);
6900 win->g.y += Y(win->ws->r);
6903 config_win(win, ev);
6906 config_win(win, ev);
6911 configurenotify(XEvent *e)
6915 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6916 e->xconfigure.window);
6918 win = find_window(e->xconfigure.window);
6920 xcb_icccm_get_wm_normal_hints_reply(conn,
6921 xcb_icccm_get_wm_normal_hints(conn, win->id),
6926 if (focus_mode == SWM_FOCUS_DEFAULT)
6927 drain_enter_notify();
6932 destroynotify(XEvent *e)
6935 XDestroyWindowEvent *ev = &e->xdestroywindow;
6937 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6939 if ((win = find_window(ev->window)) == NULL) {
6940 if ((win = find_unmanaged_window(ev->window)) == NULL)
6946 /* make sure we focus on something */
6949 unmanage_window(win);
6951 if (focus_mode == SWM_FOCUS_DEFAULT)
6952 drain_enter_notify();
6957 enternotify(XEvent *e)
6959 XCrossingEvent *ev = &e->xcrossing;
6964 Window focus_return;
6965 int revert_to_return;
6967 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6968 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6969 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6970 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6972 if (ev->mode != NotifyNormal) {
6973 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6978 switch (focus_mode) {
6979 case SWM_FOCUS_DEFAULT:
6981 case SWM_FOCUS_FOLLOW:
6983 case SWM_FOCUS_SYNERGY:
6986 * all these checks need to be in this order because the
6987 * XCheckTypedWindowEvent relies on weeding out the previous events
6989 * making this code an option would enable a follow mouse for focus
6994 * state is set when we are switching workspaces and focus is set when
6995 * the window or a subwindow already has focus (occurs during restart).
6997 * Only honor the focus flag if last_focus_event is not FocusOut,
6998 * this allows spectrwm to continue to control focus when another
6999 * program is also playing with it.
7001 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
7002 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
7007 * happens when a window is created or destroyed and the border
7008 * crosses the mouse pointer and when switching ws
7010 * we need the subwindow test to see if we came from root in order
7011 * to give focus to floaters
7013 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
7014 ev->subwindow == 0) {
7015 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
7019 /* this window already has focus */
7020 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
7021 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
7025 /* this window is being deleted or moved to another ws */
7026 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
7028 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
7029 XPutBackEvent(display, &cne);
7033 if ((win = find_window(ev->window)) == NULL) {
7034 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
7039 * In fullstack kill all enters unless they come from a different ws
7040 * (i.e. another region) or focus has been grabbed externally.
7042 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
7043 last_focus_event != FocusOut) {
7044 XGetInputFocus(display, &focus_return, &revert_to_return);
7045 if ((w = find_window(focus_return)) == NULL ||
7047 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
7055 if ((win = find_window(ev->window)) == NULL) {
7056 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7061 * if we have more enternotifies let them handle it in due time
7063 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
7064 DNPRINTF(SWM_D_EVENT,
7065 "ignoring enternotify: got more enternotify\n");
7066 XPutBackEvent(display, &cne);
7073 /* lets us use one switch statement for arbitrary mode/detail combinations */
7074 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7077 focusevent(XEvent *e)
7081 u_int32_t mode_detail;
7082 XFocusChangeEvent *ev = &e->xfocus;
7084 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
7085 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
7086 ev->window, ev->mode, ev->detail);
7088 if (last_focus_event == ev->type) {
7089 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
7093 last_focus_event = ev->type;
7094 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
7096 switch (mode_detail) {
7097 /* synergy client focus operations */
7098 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
7099 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
7101 /* synergy server focus operations */
7102 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
7104 /* Entering applications like rdesktop that mangle the pointer */
7105 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
7107 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
7108 XSetWindowBorder(display, win->id,
7109 win->ws->r->s->c[ev->type == FocusIn ?
7110 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
7113 warnx("ignoring focusevent");
7114 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
7121 mapnotify(XEvent *e)
7124 XMapEvent *ev = &e->xmap;
7126 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
7128 win = manage_window(ev->window);
7130 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7133 * focus_win can only set input focus on a mapped window.
7134 * make sure the window really has focus since it is just being mapped.
7136 if (win->ws->focus == win)
7141 mappingnotify(XEvent *e)
7143 XMappingEvent *ev = &e->xmapping;
7145 XRefreshKeyboardMapping(ev);
7146 if (ev->request == MappingKeyboard)
7151 maprequest(XEvent *e)
7154 struct swm_region *r;
7155 XMapRequestEvent *ev = &e->xmaprequest;
7156 xcb_get_window_attributes_reply_t *war;
7158 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
7159 e->xmaprequest.window);
7161 war = xcb_get_window_attributes_reply(conn,
7162 xcb_get_window_attributes(conn, ev->window),
7166 if (war->override_redirect) {
7172 win = manage_window(e->xmaprequest.window);
7174 return; /* can't happen */
7179 /* make new win focused */
7180 r = root_to_region(win->wa->root);
7181 if (win->ws == r->ws)
7186 propertynotify(XEvent *e)
7189 XPropertyEvent *ev = &e->xproperty;
7193 xcb_get_atom_name_reply_t *r;
7195 r = xcb_get_atom_name_reply(conn,
7196 xcb_get_atom_name(conn, ev->atom),
7199 len = xcb_get_atom_name_name_length(r);
7201 name = malloc(len + 1);
7203 memcpy(name, xcb_get_atom_name_name(r), len);
7206 DNPRINTF(SWM_D_EVENT,
7207 "propertynotify: window: 0x%lx, "
7217 win = find_window(ev->window);
7221 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
7222 update_iconic(win, 0);
7223 map_window_raised(win->id);
7231 case XA_WM_NORMAL_HINTS:
7233 XGetWMNormalHints(display, win->id, &win->sh, &mask);
7234 warnx("normal hints: flag 0x%x", win->sh.flags);
7235 if (win->sh.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
7236 WIDTH(win) = win->sh.min_width;
7237 HEIGHT(win) = win->sh.min_height;
7238 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7240 XMoveResizeWindow(display, win->id,
7241 X(win), Y(win), WIDTH(win), HEIGHT(win));
7243 case XCB_ATOM_WM_CLASS:
7244 case XCB_ATOM_WM_NAME:
7253 unmapnotify(XEvent *e)
7257 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
7259 /* determine if we need to help unmanage this window */
7260 win = find_window(e->xunmap.window);
7264 if (getstate(e->xunmap.window) == XCB_ICCCM_WM_STATE_NORMAL) {
7265 unmanage_window(win);
7268 /* giant hack for apps that don't destroy transient windows */
7269 /* eat a bunch of events to prevent remanaging the window */
7271 while (XCheckWindowEvent(display, e->xunmap.window,
7272 EnterWindowMask, &cne))
7274 while (XCheckWindowEvent(display, e->xunmap.window,
7275 StructureNotifyMask, &cne))
7277 while (XCheckWindowEvent(display, e->xunmap.window,
7278 SubstructureNotifyMask, &cne))
7280 /* resend unmap because we ated it */
7281 xcb_unmap_window(conn, e->xunmap.window);
7284 if (focus_mode == SWM_FOCUS_DEFAULT)
7285 drain_enter_notify();
7289 visibilitynotify(XEvent *e)
7292 struct swm_region *r;
7294 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
7295 e->xvisibility.window);
7297 if (e->xvisibility.state == VisibilityUnobscured) {
7298 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7299 for (i = 0; i < num_screens; i++)
7300 TAILQ_FOREACH(r, &screens[i].rl, entry)
7301 if (e->xvisibility.window == WINID(r->bar))
7307 clientmessage(XEvent *e)
7309 XClientMessageEvent *ev;
7314 win = find_window(ev->window);
7316 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7317 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7318 "unmanaged window.\n");
7319 e->xmaprequest.window = ev->window;
7325 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
7326 ev->window, ev->message_type);
7328 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7329 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7332 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7333 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7334 if (win->can_delete)
7335 client_msg(win, adelete);
7337 xcb_kill_client(conn, win->id);
7339 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7340 DNPRINTF(SWM_D_EVENT,
7341 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7342 if (win->floating) {
7343 if (ev->data.l[0] & (1<<8)) /* x */
7344 X(win) = ev->data.l[1];
7345 if (ev->data.l[0] & (1<<9)) /* y */
7346 Y(win) = ev->data.l[2];
7347 if (ev->data.l[0] & (1<<10)) /* width */
7348 WIDTH(win) = ev->data.l[3];
7349 if (ev->data.l[0] & (1<<11)) /* height */
7350 HEIGHT(win) = ev->data.l[4];
7355 /* TODO: Change stack sizes */
7356 /* notify no change was made. */
7357 config_win(win, NULL);
7360 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
7361 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7362 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
7364 ewmh_update_win_state(win, ev->data.l[2],
7372 xerror_start(Display *d, XErrorEvent *ee)
7379 xerror(Display *d, XErrorEvent *ee)
7381 /* warnx("error: %p %p", display, ee); */
7389 xerrorxlib = XSetErrorHandler(xerror_start);
7391 /* this causes an error if some other window manager is running */
7392 XSelectInput(display, DefaultRootWindow(display),
7393 SubstructureRedirectMask);
7398 XSetErrorHandler(xerror);
7404 new_region(struct swm_screen *s, int x, int y, int w, int h)
7406 struct swm_region *r, *n;
7407 struct workspace *ws = NULL;
7410 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7411 s->idx, w, h, x, y);
7413 /* remove any conflicting regions */
7414 n = TAILQ_FIRST(&s->rl);
7417 n = TAILQ_NEXT(r, entry);
7418 if (X(r) < (x + w) &&
7419 (X(r) + WIDTH(r)) > x &&
7421 (Y(r) + HEIGHT(r)) > y) {
7422 if (r->ws->r != NULL)
7423 r->ws->old_r = r->ws->r;
7426 TAILQ_REMOVE(&s->rl, r, entry);
7427 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7431 /* search old regions for one to reuse */
7433 /* size + location match */
7434 TAILQ_FOREACH(r, &s->orl, entry)
7435 if (X(r) == x && Y(r) == y &&
7436 HEIGHT(r) == h && WIDTH(r) == w)
7440 TAILQ_FOREACH(r, &s->orl, entry)
7441 if (HEIGHT(r) == h && WIDTH(r) == w)
7445 TAILQ_REMOVE(&s->orl, r, entry);
7446 /* try to use old region's workspace */
7447 if (r->ws->r == NULL)
7450 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7451 err(1, "new_region: calloc: failed to allocate memory "
7454 /* if we don't have a workspace already, find one */
7456 for (i = 0; i < workspace_limit; i++)
7457 if (s->ws[i].r == NULL) {
7464 errx(1, "new_region: no free workspaces");
7475 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7481 #ifdef SWM_XRR_HAS_CRTC
7484 #endif /* SWM_XRR_HAS_CRTC */
7485 struct swm_region *r;
7487 xcb_randr_get_screen_resources_current_cookie_t src;
7488 xcb_randr_get_screen_resources_current_reply_t *srr;
7489 xcb_randr_get_crtc_info_cookie_t cic;
7490 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7491 xcb_randr_crtc_t *crtc;
7492 xcb_screen_t *screen = get_screen(i);
7494 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7495 if (i >= num_screens)
7496 errx(1, "scan_xrandr: invalid screen");
7498 /* remove any old regions */
7499 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7500 r->ws->old_r = r->ws->r = NULL;
7502 TAILQ_REMOVE(&screens[i].rl, r, entry);
7503 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7507 /* map virtual screens onto physical screens */
7508 #ifdef SWM_XRR_HAS_CRTC
7509 if (xrandr_support) {
7510 src = xcb_randr_get_screen_resources_current(conn,
7512 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7515 new_region(&screens[i], 0, 0,
7516 screen->width_in_pixels,
7517 screen->height_in_pixels);
7520 ncrtc = srr->num_crtcs;
7521 for (c = 0; c < ncrtc; c++) {
7522 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7523 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7525 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7528 if (cir->num_outputs == 0) {
7534 new_region(&screens[i], 0, 0,
7535 screen->width_in_pixels,
7536 screen->height_in_pixels);
7538 new_region(&screens[i],
7539 cir->x, cir->y, cir->width, cir->height);
7544 #endif /* SWM_XRR_HAS_CRTC */
7546 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7547 screen->height_in_pixels);
7552 screenchange(XEvent *e)
7554 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7555 struct swm_region *r;
7558 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7560 if (!XRRUpdateConfiguration(e))
7563 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7564 /* silly event doesn't include the screen index */
7565 for (i = 0; i < num_screens; i++)
7566 if (screens[i].root == xe->root)
7568 if (i >= num_screens)
7569 errx(1, "screenchange: screen not found");
7571 /* brute force for now, just re-enumerate the regions */
7574 /* add bars to all regions */
7575 for (i = 0; i < num_screens; i++)
7576 TAILQ_FOREACH(r, &screens[i].rl, entry)
7579 if (focus_mode == SWM_FOCUS_DEFAULT)
7580 drain_enter_notify();
7586 xcb_window_t *wins = NULL;
7588 int i, j, num_screens;
7589 uint16_t state, manage;
7591 xcb_query_tree_cookie_t qtc;
7592 xcb_query_tree_reply_t *qtr;
7593 xcb_get_window_attributes_cookie_t c;
7594 xcb_get_window_attributes_reply_t *r;
7595 xcb_get_property_cookie_t pc;
7597 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7598 for (i = 0; i < num_screens; i++) {
7599 qtc = xcb_query_tree(conn, screens[i].root);
7600 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7603 wins = xcb_query_tree_children(qtr);
7604 no = xcb_query_tree_children_length(qtr);
7605 /* attach windows to a region */
7606 /* normal windows */
7607 for (j = 0; j < no; j++) {
7608 c = xcb_get_window_attributes(conn, wins[j]);
7609 r = xcb_get_window_attributes_reply(conn, c, NULL);
7612 if (r->override_redirect) {
7617 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7618 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7624 state = getstate(wins[j]);
7625 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7626 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7627 manage_window(wins[j]);
7630 /* transient windows */
7631 for (j = 0; j < no; j++) {
7632 c = xcb_get_window_attributes(conn, wins[j]);
7633 r = xcb_get_window_attributes_reply(conn, c, NULL);
7636 if (r->override_redirect) {
7642 state = getstate(wins[j]);
7643 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7644 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7645 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7647 manage_window(wins[j]);
7656 int i, j, k, num_screens;
7657 struct workspace *ws;
7658 uint32_t gcv[1], wa[1];
7659 const xcb_query_extension_reply_t *qep;
7660 xcb_cursor_t cursor;
7661 xcb_font_t cursor_font;
7662 xcb_randr_query_version_cookie_t c;
7663 xcb_randr_query_version_reply_t *r;
7665 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7666 if ((screens = calloc(num_screens,
7667 sizeof(struct swm_screen))) == NULL)
7668 err(1, "setup_screens: calloc: failed to allocate memory for "
7671 /* initial Xrandr setup */
7672 xrandr_support = False;
7673 c = xcb_randr_query_version(conn, True, True);
7674 r = xcb_randr_query_version_reply(conn, c, NULL);
7676 if (r->major_version >= 1)
7677 xrandr_support = True;
7680 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7681 xrandr_eventbase = qep->first_event;
7683 cursor_font = xcb_generate_id(conn);
7684 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7686 cursor = xcb_generate_id(conn);
7687 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7688 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7691 /* map physical screens */
7692 for (i = 0; i < num_screens; i++) {
7693 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7695 TAILQ_INIT(&screens[i].rl);
7696 TAILQ_INIT(&screens[i].orl);
7697 screens[i].root = RootWindow(display, i);
7699 /* set default colors */
7700 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7701 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7702 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7703 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7704 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7706 /* create graphics context on screen */
7707 screens[i].bar_gc = xcb_generate_id(conn);
7709 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7710 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7712 /* set default cursor */
7713 xcb_change_window_attributes(conn, screens[i].root,
7716 /* init all workspaces */
7717 /* XXX these should be dynamically allocated too */
7718 for (j = 0; j < SWM_WS_MAX; j++) {
7719 ws = &screens[i].ws[j];
7725 TAILQ_INIT(&ws->winlist);
7726 TAILQ_INIT(&ws->unmanagedlist);
7728 for (k = 0; layouts[k].l_stack != NULL; k++)
7729 if (layouts[k].l_config != NULL)
7730 layouts[k].l_config(ws,
7731 SWM_ARG_ID_STACKINIT);
7732 ws->cur_layout = &layouts[0];
7733 ws->cur_layout->l_string(ws);
7739 xcb_randr_select_input(conn, screens[i].root,
7740 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7742 xcb_free_cursor(conn, cursor);
7743 xcb_close_font(conn, cursor_font);
7749 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7750 err(1, "setup_globals: strdup: failed to allocate memory.");
7752 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7753 err(1, "setup_globals: strdup: failed to allocate memory.");
7760 xcb_atom_t netwmcheck, netwmname, utf8_string;
7761 xcb_window_t root, win;
7762 xcb_screen_t *screen;
7765 /* work around sun jdk bugs, code from wmname */
7766 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7767 netwmname = get_atom_from_string("_NET_WM_NAME");
7768 utf8_string = get_atom_from_string("UTF8_STRING");
7770 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7771 for (i = 0; i < num_screens; i++) {
7772 root = screens[i].root;
7773 screen = get_screen(i);
7775 win = xcb_generate_id(conn);
7776 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7777 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7778 xcb_create_window(conn, screen->root_depth, win, 0,
7779 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
7780 screen->root_visual,
7781 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7783 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7784 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7785 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7786 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7787 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7788 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7793 main(int argc, char *argv[])
7795 struct swm_region *r, *rr;
7796 struct ws_win *winfocus = NULL;
7799 char conf[PATH_MAX], *cfile = NULL;
7802 int xfd, i, num_screens;
7804 struct sigaction sact;
7807 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7808 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7810 warnx("no locale support");
7812 if (!X_HAVE_UTF8_STRING)
7813 warnx("no UTF-8 support");
7815 if (!(display = XOpenDisplay(0)))
7816 errx(1, "can not open display");
7818 if (!(conn = XGetXCBConnection(display)))
7819 errx(1, "can not get XCB connection");
7822 errx(1, "other wm running");
7824 /* handle some signals */
7825 bzero(&sact, sizeof(sact));
7826 sigemptyset(&sact.sa_mask);
7828 sact.sa_handler = sighdlr;
7829 sigaction(SIGINT, &sact, NULL);
7830 sigaction(SIGQUIT, &sact, NULL);
7831 sigaction(SIGTERM, &sact, NULL);
7832 sigaction(SIGHUP, &sact, NULL);
7834 sact.sa_handler = sighdlr;
7835 sact.sa_flags = SA_NOCLDSTOP;
7836 sigaction(SIGCHLD, &sact, NULL);
7838 astate = get_atom_from_string("WM_STATE");
7839 aprot = get_atom_from_string("WM_PROTOCOLS");
7840 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7841 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7842 a_wmname = get_atom_from_string("WM_NAME");
7843 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7844 a_utf8_string = get_atom_from_string("UTF8_STRING");
7845 a_string = get_atom_from_string("STRING");
7846 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7848 /* look for local and global conf file */
7849 pwd = getpwuid(getuid());
7851 errx(1, "invalid user: %d", getuid());
7853 syms = xcb_key_symbols_alloc(conn);
7855 errx(1, "unable to allocate key symbols");
7864 for (i = 0; ; i++) {
7869 snprintf(conf, sizeof conf, "%s/.%s",
7870 pwd->pw_dir, SWM_CONF_FILE);
7874 snprintf(conf, sizeof conf, "/etc/%s",
7879 snprintf(conf, sizeof conf, "%s/.%s",
7880 pwd->pw_dir, SWM_CONF_FILE_OLD);
7884 snprintf(conf, sizeof conf, "/etc/%s",
7891 if (strlen(conf) && stat(conf, &sb) != -1)
7892 if (S_ISREG(sb.st_mode)) {
7899 /* load conf (if any) */
7901 conf_load(cfile, SWM_CONF_DEFAULT);
7904 /* set some values to work around bad programs */
7906 /* grab existing windows (before we build the bars) */
7909 if (getenv("SWM_STARTED") == NULL)
7910 setenv("SWM_STARTED", "YES", 1);
7912 /* setup all bars */
7913 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7914 for (i = 0; i < num_screens; i++)
7915 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7916 if (winfocus == NULL)
7917 winfocus = TAILQ_FIRST(&r->ws->winlist);
7925 if (focus_mode == SWM_FOCUS_DEFAULT)
7926 drain_enter_notify();
7928 xfd = xcb_get_file_descriptor(conn);
7930 while (XPending(display)) {
7931 XNextEvent(display, &e);
7934 if (e.type < LASTEvent) {
7935 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7936 "window: 0x%lx, type: %s (%d), %d remaining"
7937 "\n", YESNO(handler[e.type]),
7938 e.xany.window, geteventname(&e),
7939 e.type, QLength(display));
7941 if (handler[e.type])
7942 handler[e.type](&e);
7944 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7945 "0x%lx, type: %s (%d)\n", e.xany.window,
7946 xrandr_geteventname(&e), e.type);
7948 switch (e.type - xrandr_eventbase) {
7949 case XCB_RANDR_SCREEN_CHANGE_NOTIFY:
7958 /* if we are being restarted go focus on first window */
7960 rr = winfocus->ws->r;
7962 /* not a visible window */
7966 /* move pointer to first screen if multi screen */
7967 if (num_screens > 1 || outputs > 1)
7968 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7969 rr->s[0].root, 0, 0, 0, 0, X(rr),
7970 Y(rr) + (bar_enabled ? bar_height : 0));
7972 a.id = SWM_ARG_ID_FOCUSCUR;
7982 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7984 DNPRINTF(SWM_D_MISC, "select failed");
7985 if (restart_wm == 1)
7986 restart(NULL, NULL);
7987 if (search_resp == 1)
8000 for (i = 0; i < num_screens; ++i)
8001 if (screens[i].bar_gc != 0)
8002 xcb_free_gc(conn, screens[i].bar_gc);
8003 XFreeFontSet(display, bar_fs);
8005 xcb_key_symbols_free(syms);
8006 xcb_disconnect(conn);