2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
58 #include <sys/types.h>
62 #include <sys/queue.h>
63 #include <sys/param.h>
64 #include <sys/select.h>
65 #if defined(__linux__)
67 #elif defined(__OpenBSD__)
69 #elif defined(__FreeBSD__)
90 #include <X11/cursorfont.h>
91 #include <X11/keysym.h>
93 #include <X11/extensions/Xrandr.h>
94 #include <X11/extensions/XTest.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr = SPECTRWM_BUILDSTR;
113 static const char *buildstr = SPECTRWM_VERSION;
117 # error XRandR versions less than 1.0 are not supported
122 #define SWM_XRR_HAS_CRTC
126 #if defined(__OpenBSD__)
127 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
128 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
129 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
130 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
131 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
132 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
133 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
134 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
135 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
136 #define xcb_icccm_get_wm_class xcb_get_wm_class
137 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
138 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
139 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
140 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
141 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
142 #define xcb_icccm_get_wm_name xcb_get_wm_name
143 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
144 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
145 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
146 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
147 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
148 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
149 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
150 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
151 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
152 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
157 #define DPRINTF(x...) do { \
159 fprintf(stderr, x); \
161 #define DNPRINTF(n,x...) do { \
162 if (swm_debug & n) { \
163 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
164 fprintf(stderr, x); \
167 #define SWM_D_MISC 0x0001
168 #define SWM_D_EVENT 0x0002
169 #define SWM_D_WS 0x0004
170 #define SWM_D_FOCUS 0x0008
171 #define SWM_D_MOVE 0x0010
172 #define SWM_D_STACK 0x0020
173 #define SWM_D_MOUSE 0x0040
174 #define SWM_D_PROP 0x0080
175 #define SWM_D_CLASS 0x0100
176 #define SWM_D_KEY 0x0200
177 #define SWM_D_QUIRK 0x0400
178 #define SWM_D_SPAWN 0x0800
179 #define SWM_D_EVENTQ 0x1000
180 #define SWM_D_CONF 0x2000
181 #define SWM_D_BAR 0x4000
183 u_int32_t swm_debug = 0
201 #define DPRINTF(x...)
202 #define DNPRINTF(n,x...)
205 #define LENGTH(x) (int)(sizeof x / sizeof x[0])
206 #define MODKEY Mod1Mask
207 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
208 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
209 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
210 #define SWM_PROPLEN (16)
211 #define SWM_FUNCNAME_LEN (32)
212 #define SWM_KEYS_LEN (255)
213 #define SWM_QUIRK_LEN (64)
214 #define X(r) (r)->g.x
215 #define Y(r) (r)->g.y
216 #define WIDTH(r) (r)->g.w
217 #define HEIGHT(r) (r)->g.h
218 #define BORDER(w) (w->bordered ? border_width : 0)
219 #define MAX_X(r) ((r)->g.x + (r)->g.w)
220 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
221 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
222 #define SH_MIN_W(w) (w)->sh.min_width
223 #define SH_MIN_H(w) (w)->sh.min_height
224 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
225 #define SH_MAX_W(w) (w)->sh.max_width
226 #define SH_MAX_H(w) (w)->sh.max_height
227 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
228 #define SH_INC_W(w) (w)->sh.width_inc
229 #define SH_INC_H(w) (w)->sh.height_inc
230 #define SWM_MAX_FONT_STEPS (3)
231 #define WINID(w) ((w) ? (w)->id : 0)
232 #define YESNO(x) ((x) ? "yes" : "no")
234 #define SWM_FOCUS_DEFAULT (0)
235 #define SWM_FOCUS_SYNERGY (1)
236 #define SWM_FOCUS_FOLLOW (2)
238 #define SWM_CONF_DEFAULT (0)
239 #define SWM_CONF_KEYMAPPING (1)
242 #define SWM_LIB "/usr/local/lib/libswmhack.so"
249 xcb_atom_t a_takefocus;
251 xcb_atom_t a_netwmname;
252 xcb_atom_t a_utf8_string;
254 xcb_atom_t a_swm_iconic;
255 volatile sig_atomic_t running = 1;
256 volatile sig_atomic_t restart_wm = 0;
258 /*int last_focus_event = FocusOut;*/
262 int xrandr_eventbase;
263 unsigned int numlockmask = 0;
264 xcb_connection_t *conn;
265 xcb_key_symbols_t *syms;
268 int cycle_visible = 0;
270 int font_adjusted = 0;
271 unsigned int mod_key = MODKEY;
274 struct swm_region *search_r;
275 int select_list_pipe[2];
276 int select_resp_pipe[2];
278 volatile sig_atomic_t search_resp;
279 int search_resp_action;
281 struct search_window {
282 TAILQ_ENTRY(search_window) entry;
286 xcb_window_t indicator;
288 TAILQ_HEAD(search_winlist, search_window);
289 struct search_winlist search_wl;
294 SWM_SEARCH_UNICONIFY,
295 SWM_SEARCH_NAME_WORKSPACE,
296 SWM_SEARCH_SEARCH_WORKSPACE,
297 SWM_SEARCH_SEARCH_WINDOW
300 #define SWM_STACK_TOP (0)
301 #define SWM_STACK_BOTTOM (1)
302 #define SWM_STACK_ABOVE (2)
303 #define SWM_STACK_BELOW (3)
306 double dialog_ratio = 0.6;
308 #define SWM_BAR_MAX (256)
309 #define SWM_BAR_JUSTIFY_LEFT (0)
310 #define SWM_BAR_JUSTIFY_CENTER (1)
311 #define SWM_BAR_JUSTIFY_RIGHT (2)
312 #define SWM_BAR_OFFSET (4)
313 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
314 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
315 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
316 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
317 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
319 char *bar_argv[] = { NULL, NULL };
321 unsigned char bar_ext[SWM_BAR_MAX];
322 char bar_vertext[SWM_BAR_MAX];
324 sig_atomic_t bar_alarm = 0;
327 int bar_border_width = 1;
328 int bar_at_bottom = 0;
330 int bar_extra_running = 0;
333 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
334 char *bar_format = NULL;
335 int stack_enabled = 1;
336 int clock_enabled = 1;
337 int urgent_enabled = 0;
338 char *clock_format = NULL;
339 int title_name_enabled = 0;
340 int title_class_enabled = 0;
341 int window_name_enabled = 0;
342 int focus_mode = SWM_FOCUS_DEFAULT;
343 int focus_close = SWM_STACK_BELOW;
344 int focus_close_wrap = 1;
345 int focus_default = SWM_STACK_TOP;
346 int spawn_position = SWM_STACK_TOP;
347 int disable_border = 0;
348 int border_width = 1;
349 int verbose_layout = 0;
354 XFontSetExtents *bar_fs_extents;
359 #define SWM_MENU_FN (2)
360 #define SWM_MENU_NB (4)
361 #define SWM_MENU_NF (6)
362 #define SWM_MENU_SB (8)
363 #define SWM_MENU_SF (10)
365 /* layout manager data */
366 struct swm_geometry {
379 struct swm_geometry g;
382 /* virtual "screens" */
384 TAILQ_ENTRY(swm_region) entry;
385 struct swm_geometry g;
386 struct workspace *ws; /* current workspace on this region */
387 struct workspace *ws_prior; /* prior workspace on this region */
388 struct swm_screen *s; /* screen idx */
391 TAILQ_HEAD(swm_region_list, swm_region);
394 TAILQ_ENTRY(ws_win) entry;
396 xcb_window_t transient;
397 struct ws_win *child_trans; /* transient child window */
398 struct swm_geometry g; /* current geometry */
399 struct swm_geometry g_float; /* region coordinates */
400 int g_floatvalid; /* g_float geometry validity */
401 int floatmaxed; /* whether maxed by max_stack */
406 unsigned int ewmh_flags;
407 int font_size_boundary[SWM_MAX_FONT_STEPS];
413 unsigned long quirks;
414 struct workspace *ws; /* always valid */
415 struct swm_screen *s; /* always valid, never changes */
416 xcb_get_geometry_reply_t *wa;
418 xcb_icccm_get_wm_class_reply_t ch;
419 xcb_icccm_wm_hints_t hints;
421 TAILQ_HEAD(ws_win_list, ws_win);
425 TAILQ_ENTRY(pid_e) entry;
429 TAILQ_HEAD(pid_list, pid_e);
430 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
432 /* layout handlers */
434 void vertical_config(struct workspace *, int);
435 void vertical_stack(struct workspace *, struct swm_geometry *);
436 void horizontal_config(struct workspace *, int);
437 void horizontal_stack(struct workspace *, struct swm_geometry *);
438 void max_stack(struct workspace *, struct swm_geometry *);
439 void plain_stacker(struct workspace *);
440 void fancy_stacker(struct workspace *);
443 void (*l_stack)(struct workspace *, struct swm_geometry *);
444 void (*l_config)(struct workspace *, int);
446 #define SWM_L_FOCUSPREV (1<<0)
447 #define SWM_L_MAPONFOCUS (1<<1)
448 void (*l_string)(struct workspace *);
450 /* stack, configure */
451 { vertical_stack, vertical_config, 0, plain_stacker },
452 { horizontal_stack, horizontal_config, 0, plain_stacker },
454 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
455 { NULL, NULL, 0, NULL },
458 /* position of max_stack mode in the layouts array, index into layouts! */
459 #define SWM_V_STACK (0)
460 #define SWM_H_STACK (1)
461 #define SWM_MAX_STACK (2)
463 #define SWM_H_SLICE (32)
464 #define SWM_V_SLICE (32)
466 /* define work spaces */
468 int idx; /* workspace index */
469 char *name; /* workspace name */
470 int always_raise; /* raise windows on focus */
471 struct layout *cur_layout; /* current layout handlers */
472 struct ws_win *focus; /* may be NULL */
473 struct ws_win *focus_prev; /* may be NULL */
474 struct swm_region *r; /* may be NULL */
475 struct swm_region *old_r; /* may be NULL */
476 struct ws_win_list winlist; /* list of windows in ws */
477 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
478 char stacker[10]; /* display stacker and layout */
482 int horizontal_msize;
484 int horizontal_stacks;
495 SWM_S_COLOR_BAR_BORDER,
496 SWM_S_COLOR_BAR_FONT,
502 /* physical screen mapping */
503 #define SWM_WS_MAX (22) /* hard limit */
504 int workspace_limit = 10; /* soft limit */
507 int idx; /* screen index */
508 struct swm_region_list rl; /* list of regions on this screen */
509 struct swm_region_list orl; /* list of old regions */
511 struct workspace ws[SWM_WS_MAX];
517 } c[SWM_S_COLOR_MAX];
519 xcb_gcontext_t bar_gc;
521 struct swm_screen *screens;
523 /* args to functions */
526 #define SWM_ARG_ID_FOCUSNEXT (0)
527 #define SWM_ARG_ID_FOCUSPREV (1)
528 #define SWM_ARG_ID_FOCUSMAIN (2)
529 #define SWM_ARG_ID_FOCUSCUR (4)
530 #define SWM_ARG_ID_SWAPNEXT (10)
531 #define SWM_ARG_ID_SWAPPREV (11)
532 #define SWM_ARG_ID_SWAPMAIN (12)
533 #define SWM_ARG_ID_MOVELAST (13)
534 #define SWM_ARG_ID_MASTERSHRINK (20)
535 #define SWM_ARG_ID_MASTERGROW (21)
536 #define SWM_ARG_ID_MASTERADD (22)
537 #define SWM_ARG_ID_MASTERDEL (23)
538 #define SWM_ARG_ID_FLIPLAYOUT (24)
539 #define SWM_ARG_ID_STACKRESET (30)
540 #define SWM_ARG_ID_STACKINIT (31)
541 #define SWM_ARG_ID_CYCLEWS_UP (40)
542 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
543 #define SWM_ARG_ID_CYCLESC_UP (42)
544 #define SWM_ARG_ID_CYCLESC_DOWN (43)
545 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
546 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
547 #define SWM_ARG_ID_STACKINC (50)
548 #define SWM_ARG_ID_STACKDEC (51)
549 #define SWM_ARG_ID_SS_ALL (60)
550 #define SWM_ARG_ID_SS_WINDOW (61)
551 #define SWM_ARG_ID_DONTCENTER (70)
552 #define SWM_ARG_ID_CENTER (71)
553 #define SWM_ARG_ID_KILLWINDOW (80)
554 #define SWM_ARG_ID_DELETEWINDOW (81)
555 #define SWM_ARG_ID_WIDTHGROW (90)
556 #define SWM_ARG_ID_WIDTHSHRINK (91)
557 #define SWM_ARG_ID_HEIGHTGROW (92)
558 #define SWM_ARG_ID_HEIGHTSHRINK (93)
559 #define SWM_ARG_ID_MOVEUP (100)
560 #define SWM_ARG_ID_MOVEDOWN (101)
561 #define SWM_ARG_ID_MOVELEFT (102)
562 #define SWM_ARG_ID_MOVERIGHT (103)
568 TAILQ_ENTRY(quirk) entry;
572 #define SWM_Q_FLOAT (1<<0) /* float this window */
573 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
574 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
575 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
576 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
577 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
579 TAILQ_HEAD(quirk_list, quirk);
580 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
583 * Supported EWMH hints should be added to
584 * both the enum and the ewmh array
589 _NET_MOVERESIZE_WINDOW,
590 _NET_WM_ACTION_CLOSE,
591 _NET_WM_ACTION_FULLSCREEN,
593 _NET_WM_ACTION_RESIZE,
594 _NET_WM_ALLOWED_ACTIONS,
597 _NET_WM_STATE_FULLSCREEN,
598 _NET_WM_STATE_HIDDEN,
599 _NET_WM_STATE_MAXIMIZED_HORZ,
600 _NET_WM_STATE_MAXIMIZED_VERT,
601 _NET_WM_STATE_SKIP_PAGER,
602 _NET_WM_STATE_SKIP_TASKBAR,
604 _NET_WM_WINDOW_TYPE_DIALOG,
605 _NET_WM_WINDOW_TYPE_DOCK,
606 _NET_WM_WINDOW_TYPE_NORMAL,
607 _NET_WM_WINDOW_TYPE_SPLASH,
608 _NET_WM_WINDOW_TYPE_TOOLBAR,
609 _NET_WM_WINDOW_TYPE_UTILITY,
610 _SWM_WM_STATE_MANUAL,
617 } ewmh[SWM_EWMH_HINT_MAX] = {
618 /* must be in same order as in the enum */
619 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
620 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
621 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
622 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
623 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
624 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
625 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
626 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
627 {"_NET_WM_STATE", XCB_ATOM_NONE},
628 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
629 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
630 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
631 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
632 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
635 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
636 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
637 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
638 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
639 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
642 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
645 /* function prototypes */
646 int conf_load(char *, int);
647 void constrain_window(struct ws_win *, struct swm_region *, int);
649 void event_handle(xcb_generic_event_t *);
650 struct ws_win *find_window(xcb_window_t);
651 int floating_toggle_win(struct ws_win *);
652 void focus(struct swm_region *, union arg *);
653 void focus_magic(struct ws_win *);
654 xcb_atom_t get_atom_from_string(const char *);
655 xcb_screen_t *get_screen(int);
656 char *get_win_name(xcb_window_t);
657 uint16_t getstate(xcb_window_t);
658 void grabbuttons(struct ws_win *, int);
659 void map_window_raised(xcb_window_t);
660 void new_region(struct swm_screen *, int, int, int, int);
661 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
662 void spawn_select(struct swm_region *, union arg *, char *, int *);
663 void store_float_geom(struct ws_win *, struct swm_region *);
664 void unmanage_window(struct ws_win *);
665 void update_window(struct ws_win *);
667 /* function definitions */
669 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
671 unsigned int tmpr, tmpg, tmpb;
673 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
684 get_screen(int screen)
686 xcb_screen_iterator_t i;
688 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
689 for (; i.rem; --screen, xcb_screen_next(&i))
699 xcb_get_input_focus_cookie_t c;
700 xcb_get_input_focus_reply_t *r;
703 c = xcb_get_input_focus(conn);
705 r = xcb_get_input_focus_reply(conn, c, NULL);
711 map_window_raised(xcb_window_t win)
713 uint32_t val = XCB_STACK_MODE_ABOVE;
715 xcb_configure_window(conn, win,
716 XCB_CONFIG_WINDOW_STACK_MODE, &val);
718 xcb_map_window(conn, win);
722 get_atom_from_string(const char *str)
724 xcb_intern_atom_cookie_t c;
725 xcb_intern_atom_reply_t *r;
728 c = xcb_intern_atom(conn, False, strlen(str), str);
729 r = xcb_intern_atom_reply(conn, c, NULL);
737 return (XCB_ATOM_NONE);
741 update_iconic(struct ws_win *win, int newv)
748 iprop = get_atom_from_string("_SWM_ICONIC");
749 if (iprop == XCB_ATOM_NONE)
753 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
754 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
756 xcb_delete_property(conn, win->id, iprop);
760 get_iconic(struct ws_win *win)
764 xcb_get_property_reply_t *pr = NULL;
766 iprop = get_atom_from_string("_SWM_ICONIC");
767 if (iprop == XCB_ATOM_NONE)
770 pr = xcb_get_property_reply(conn,
771 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
775 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
777 v = *((int32_t *)xcb_get_property_value(pr));
788 int i, j, num_screens;
790 sup_list = get_atom_from_string("_NET_SUPPORTED");
792 for (i = 0; i < LENGTH(ewmh); i++)
793 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
795 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
796 for (i = 0; i < num_screens; i++) {
797 /* Support check window will be created by workaround(). */
799 /* Report supported atoms */
800 xcb_delete_property(conn, screens[i].root, sup_list);
801 for (j = 0; j < LENGTH(ewmh); j++)
802 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
803 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
812 xcb_atom_t sup_check, sup_list;
814 xcb_get_property_cookie_t pc;
815 xcb_get_property_reply_t *pr;
817 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
818 sup_list = get_atom_from_string("_NET_SUPPORTED");
819 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
821 for (i = 0; i < num_screens; i++) {
822 /* Get the support check window and destroy it */
823 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
824 XCB_ATOM_WINDOW, 0, 1);
825 pr = xcb_get_property_reply(conn, pc, NULL);
827 id = *((xcb_window_t *)xcb_get_property_value(pr));
829 xcb_destroy_window(conn, id);
830 xcb_delete_property(conn, screens[i].root, sup_check);
831 xcb_delete_property(conn, screens[i].root, sup_list);
839 ewmh_autoquirk(struct ws_win *win)
843 xcb_get_property_cookie_t c;
844 xcb_get_property_reply_t *r;
846 c = xcb_get_property(conn, False, win->id,
847 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
848 r = xcb_get_property_reply(conn, c, NULL);
852 n = xcb_get_property_value_length(r);
853 type = xcb_get_property_value(r);
855 for (i = 0; i < n; i++) {
856 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
858 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
859 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
860 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
862 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
865 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
866 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
868 win->quirks = SWM_Q_FLOAT;
875 #define SWM_EWMH_ACTION_COUNT_MAX (6)
876 #define EWMH_F_FULLSCREEN (1<<0)
877 #define EWMH_F_ABOVE (1<<1)
878 #define EWMH_F_HIDDEN (1<<2)
879 #define EWMH_F_SKIP_PAGER (1<<3)
880 #define EWMH_F_SKIP_TASKBAR (1<<4)
881 #define SWM_F_MANUAL (1<<5)
884 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
892 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
893 "fullscreen %s\n", win->id, YESNO(fs));
896 if (!win->g_floatvalid)
897 store_float_geom(win, win->ws->r);
899 win->g = win->ws->r->g;
902 if (win->g_floatvalid) {
903 /* refloat at last floating relative position */
904 win->g = win->g_float;
905 X(win) += X(win->ws->r);
906 Y(win) += Y(win->ws->r);
914 ewmh_update_actions(struct ws_win *win)
916 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
922 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
925 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
926 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
929 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
930 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
933 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
934 #define _NET_WM_STATE_ADD 1 /* add/set property */
935 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
938 ewmh_update_win_state(struct ws_win *win, long state, long action)
940 unsigned int mask = 0;
941 unsigned int changed = 0;
942 unsigned int orig_flags;
947 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
948 mask = EWMH_F_FULLSCREEN;
949 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
951 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
953 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
954 mask = EWMH_F_SKIP_PAGER;
955 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
956 mask = EWMH_F_SKIP_TASKBAR;
959 orig_flags = win->ewmh_flags;
962 case _NET_WM_STATE_REMOVE:
963 win->ewmh_flags &= ~mask;
965 case _NET_WM_STATE_ADD:
966 win->ewmh_flags |= mask;
968 case _NET_WM_STATE_TOGGLE:
969 win->ewmh_flags ^= mask;
973 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
975 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
977 if (!floating_toggle_win(win))
978 win->ewmh_flags = orig_flags; /* revert */
979 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
981 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
982 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
984 if (!ewmh_set_win_fullscreen(win,
985 win->ewmh_flags & EWMH_F_FULLSCREEN))
986 win->ewmh_flags = orig_flags; /* revert */
988 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
990 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
991 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
992 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
993 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
994 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
995 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
996 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
997 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
998 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
999 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1000 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1001 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1002 if (win->ewmh_flags & EWMH_F_ABOVE)
1003 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1004 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1005 &ewmh[_NET_WM_STATE_ABOVE].atom);
1006 if (win->ewmh_flags & SWM_F_MANUAL)
1007 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1008 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1009 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1013 ewmh_get_win_state(struct ws_win *win)
1016 xcb_get_property_cookie_t c;
1017 xcb_get_property_reply_t *r;
1023 win->ewmh_flags = 0;
1025 win->ewmh_flags |= EWMH_F_ABOVE;
1027 win->ewmh_flags |= SWM_F_MANUAL;
1029 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1030 XCB_ATOM_ATOM, 0, UINT32_MAX);
1031 r = xcb_get_property_reply(conn, c, NULL);
1035 states = xcb_get_property_value(r);
1036 n = xcb_get_property_value_length(r);
1038 for (i = 0; i < n; i++)
1039 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1047 dumpwins(struct swm_region *r, union arg *args)
1051 xcb_get_window_attributes_cookie_t c;
1052 xcb_get_window_attributes_reply_t *wa;
1054 if (r->ws == NULL) {
1055 warnx("dumpwins: invalid workspace");
1059 warnx("=== managed window list ws %02d ===", r->ws->idx);
1060 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1061 state = getstate(win->id);
1062 c = xcb_get_window_attributes(conn, win->id);
1063 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1065 warnx("window: 0x%x, map_state: %d, state: %u, "
1066 "transient: 0x%x", win->id, wa->map_state,
1067 state, win->transient);
1070 warnx("window: 0x%x, failed xcb_get_window_attributes",
1074 warnx("===== unmanaged window list =====");
1075 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1076 state = getstate(win->id);
1077 c = xcb_get_window_attributes(conn, win->id);
1078 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1080 warnx("window: 0x%x, map_state: %d, state: %u, "
1081 "transient: 0x%x", win->id, wa->map_state,
1082 state, win->transient);
1085 warnx("window: 0x%x, failed xcb_get_window_attributes",
1089 warnx("=================================");
1093 dumpwins(struct swm_region *r, union arg *args)
1096 #endif /* SWM_DEBUG */
1098 void event_error(xcb_generic_error_t *);
1099 void expose(xcb_expose_event_t *);
1100 void keypress(xcb_key_press_event_t *);
1101 void buttonpress(xcb_button_press_event_t *);
1102 void configurerequest(xcb_configure_request_event_t *);
1103 void configurenotify(xcb_configure_notify_event_t *);
1104 void destroynotify(xcb_destroy_notify_event_t *);
1105 void enternotify(xcb_enter_notify_event_t *);
1106 void mapnotify(xcb_map_notify_event_t *);
1107 void mappingnotify(xcb_mapping_notify_event_t *);
1108 void maprequest(xcb_map_request_event_t *);
1109 void propertynotify(xcb_property_notify_event_t *);
1110 void unmapnotify(xcb_unmap_notify_event_t *);
1111 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1112 void clientmessage(xcb_client_message_event_t *);
1113 void screenchange(xcb_randr_screen_change_notify_event_t *);
1118 int saved_errno, status;
1121 saved_errno = errno;
1125 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1130 if (errno != ECHILD)
1131 warn("sighdlr: waitpid");
1132 #endif /* SWM_DEBUG */
1135 if (pid == searchpid)
1139 if (WIFEXITED(status)) {
1140 if (WEXITSTATUS(status) != 0)
1141 warnx("sighdlr: child exit status: %d",
1142 WEXITSTATUS(status));
1144 warnx("sighdlr: child is terminated "
1146 #endif /* SWM_DEBUG */
1160 errno = saved_errno;
1166 struct pid_e *p = NULL;
1168 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1173 TAILQ_FOREACH(p, &pidlist, entry) {
1182 name_to_color(const char *colorname)
1184 uint32_t result = 0;
1185 char cname[32] = "#";
1186 xcb_screen_t *screen;
1187 xcb_colormap_t cmap;
1188 xcb_alloc_color_reply_t *cr;
1189 xcb_alloc_named_color_reply_t *nr;
1190 uint16_t rr, gg, bb;
1192 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1193 cmap = screen->default_colormap;
1195 /* color is in format rgb://rr/gg/bb */
1196 if (strncmp(colorname, "rgb:", 4) == 0) {
1197 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1198 warnx("could not parse rgb %s", colorname);
1200 cr = xcb_alloc_color_reply(conn,
1201 xcb_alloc_color(conn, cmap, rr, gg, bb),
1207 warnx("color '%s' not found", colorname);
1210 nr = xcb_alloc_named_color_reply(conn,
1211 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1214 strlcat(cname, colorname + 2, sizeof cname - 1);
1215 nr = xcb_alloc_named_color_reply(conn,
1216 xcb_alloc_named_color(conn, cmap, strlen(cname),
1223 warnx("color '%s' not found", colorname);
1230 setscreencolor(char *val, int i, int c)
1234 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1235 if (i > 0 && i <= num_screens) {
1236 screens[i - 1].c[c].color = name_to_color(val);
1237 free(screens[i - 1].c[c].name);
1238 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1240 } else if (i == -1) {
1241 for (i = 0; i < num_screens; i++) {
1242 screens[i].c[c].color = name_to_color(val);
1243 free(screens[i].c[c].name);
1244 if ((screens[i].c[c].name = strdup(val)) == NULL)
1248 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1253 fancy_stacker(struct workspace *ws)
1255 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1256 if (ws->cur_layout->l_stack == vertical_stack)
1257 snprintf(ws->stacker, sizeof ws->stacker,
1258 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1259 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1260 if (ws->cur_layout->l_stack == horizontal_stack)
1261 snprintf(ws->stacker, sizeof ws->stacker,
1262 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1263 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1267 plain_stacker(struct workspace *ws)
1269 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1270 if (ws->cur_layout->l_stack == vertical_stack)
1271 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1272 sizeof ws->stacker);
1273 if (ws->cur_layout->l_stack == horizontal_stack)
1274 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1275 sizeof ws->stacker);
1279 custom_region(char *val)
1281 unsigned int sidx, x, y, w, h;
1283 xcb_screen_t *screen;
1285 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1286 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1287 errx(1, "invalid custom region, "
1288 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1289 if (sidx < 1 || sidx > num_screens)
1290 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1294 screen = get_screen(sidx);
1296 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1298 if (x > screen->width_in_pixels ||
1299 y > screen->height_in_pixels ||
1300 w + x > screen->width_in_pixels ||
1301 h + y > screen->height_in_pixels) {
1302 warnx("ignoring region %ux%u+%u+%u - not within screen "
1303 "boundaries (%ux%u)", w, h, x, y,
1304 screen->width_in_pixels, screen->height_in_pixels);
1308 new_region(&screens[sidx], x, y, w, h);
1312 socket_setnonblock(int fd)
1316 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1317 err(1, "fcntl F_GETFL");
1318 flags |= O_NONBLOCK;
1319 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1320 err(1, "fcntl F_SETFL");
1324 bar_print(struct swm_region *r, const char *s)
1328 xcb_rectangle_t rect;
1330 XRectangle ibox, lbox;
1333 /* FIXME fix bar font position calculations */
1335 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1337 switch (bar_justify) {
1338 case SWM_BAR_JUSTIFY_LEFT:
1341 case SWM_BAR_JUSTIFY_CENTER:
1342 x = (WIDTH(r) - lbox.width) / 2;
1344 case SWM_BAR_JUSTIFY_RIGHT:
1345 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1349 if (x < SWM_BAR_OFFSET)
1355 rect.width = WIDTH(r->bar);
1356 rect.height = HEIGHT(r->bar);
1358 /* clear back buffer */
1359 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1360 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1361 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1362 sizeof(rect), &rect);
1364 /* draw back buffer */
1365 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1366 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1367 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1368 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1370 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1371 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1375 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, 4, 14, s);
1379 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1380 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1384 bar_extra_stop(void)
1388 bzero(bar_pipe, sizeof bar_pipe);
1391 kill(bar_pid, SIGTERM);
1394 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1399 bar_class_name(char *s, size_t sz, struct swm_region *r)
1401 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1403 if (r->ws->focus->ch.class_name != NULL)
1404 strlcat(s, r->ws->focus->ch.class_name, sz);
1408 bar_title_name(char *s, size_t sz, struct swm_region *r)
1410 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1412 if (r->ws->focus->ch.instance_name != NULL)
1413 strlcat(s, r->ws->focus->ch.instance_name, sz);
1417 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1419 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1422 bar_class_name(s, sz, r);
1423 strlcat(s, ":", sz);
1424 bar_title_name(s, sz, r);
1428 bar_window_float(char *s, size_t sz, struct swm_region *r)
1430 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1432 if (r->ws->focus->floating)
1433 strlcat(s, "(f)", sz);
1437 bar_window_name(char *s, size_t sz, struct swm_region *r)
1441 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1443 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1446 strlcat(s, title, sz);
1450 int urgent[SWM_WS_MAX];
1452 bar_urgent(char *s, size_t sz)
1455 int i, j, num_screens;
1457 xcb_get_property_cookie_t c;
1458 xcb_icccm_wm_hints_t hints;
1460 for (i = 0; i < workspace_limit; i++)
1463 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1464 for (i = 0; i < num_screens; i++)
1465 for (j = 0; j < workspace_limit; j++)
1466 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1467 c = xcb_icccm_get_wm_hints(conn, win->id);
1468 if (xcb_icccm_get_wm_hints_reply(conn, c,
1471 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1475 for (i = 0; i < workspace_limit; i++) {
1477 snprintf(b, sizeof b, "%d ", i + 1);
1479 snprintf(b, sizeof b, "- ");
1485 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1487 if (r == NULL || r->ws == NULL)
1489 if (r->ws->name != NULL)
1490 strlcat(s, r->ws->name, sz);
1493 /* build the default bar format according to the defined enabled options */
1495 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1497 /* if format provided, just copy the buffers */
1498 if (bar_format != NULL) {
1499 strlcpy(fmtnew, fmtexp, sz);
1503 /* reset the output buffer */
1506 strlcat(fmtnew, "+N:+I ", sz);
1508 strlcat(fmtnew, "+S", sz);
1509 strlcat(fmtnew, " ", sz);
1511 /* only show the workspace name if there's actually one */
1512 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1513 strlcat(fmtnew, "<+D>", sz);
1514 strlcat(fmtnew, "+3<", sz);
1516 if (clock_enabled) {
1517 strlcat(fmtnew, fmtexp, sz);
1518 strlcat(fmtnew, "+4<", sz);
1521 /* bar_urgent already adds the space before the last asterisk */
1523 strlcat(fmtnew, "* +U*+4<", sz);
1525 if (title_class_enabled) {
1526 strlcat(fmtnew, "+C", sz);
1527 if (title_name_enabled == 0)
1528 strlcat(fmtnew, "+4<", sz);
1531 /* checks needed by the colon and floating strlcat(3) calls below */
1532 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1533 if (title_name_enabled) {
1534 if (title_class_enabled)
1535 strlcat(fmtnew, ":", sz);
1536 strlcat(fmtnew, "+T+4<", sz);
1538 if (window_name_enabled) {
1539 if (r->ws->focus->floating)
1540 strlcat(fmtnew, "+F ", sz);
1541 strlcat(fmtnew, "+64W ", sz);
1545 /* finally add the action script output and the version */
1546 strlcat(fmtnew, "+4<+A+4<+V", sz);
1550 bar_replace_pad(char *tmp, int *limit, size_t sz)
1552 /* special case; no limit given, pad one space, instead */
1553 if (*limit == sz - 1)
1555 snprintf(tmp, sz, "%*s", *limit, " ");
1558 /* replaces the bar format character sequences (like in tmux(1)) */
1560 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1564 char tmp[SWM_BAR_MAX];
1568 /* reset strlcat(3) buffer */
1571 /* get number, if any */
1574 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1575 limit = sizeof tmp - 1;
1576 if (limit <= 0 || limit >= sizeof tmp)
1577 limit = sizeof tmp - 1;
1579 /* there is nothing to replace (ie EOL) */
1586 bar_replace_pad(tmp, &limit, sizeof tmp);
1589 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1592 bar_class_name(tmp, sizeof tmp, r);
1595 bar_workspace_name(tmp, sizeof tmp, r);
1598 bar_window_float(tmp, sizeof tmp, r);
1601 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1604 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1607 bar_class_title_name(tmp, sizeof tmp, r);
1610 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1613 bar_title_name(tmp, sizeof tmp, r);
1616 bar_urgent(tmp, sizeof tmp);
1619 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1622 bar_window_name(tmp, sizeof tmp, r);
1625 /* unknown character sequence; copy as-is */
1626 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1634 while (limit-- > 0) {
1635 if (*offrep >= sz - 1)
1637 fmtrep[(*offrep)++] = *ptr++;
1645 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1650 while (*fmt != '\0') {
1652 /* skip ordinary characters */
1655 fmtrep[off++] = *fmt++;
1659 /* character sequence found; replace it */
1660 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1669 bar_fmt_expand(char *fmtexp, size_t sz)
1676 /* start by grabbing the current time and date */
1678 localtime_r(&tmt, &tm);
1680 /* figure out what to expand */
1681 if (bar_format != NULL)
1683 else if (bar_format == NULL && clock_enabled)
1685 /* if nothing to expand bail out */
1691 /* copy as-is, just in case the format shouldn't be expanded below */
1692 strlcpy(fmtexp, fmt, sz);
1693 /* finally pass the string through strftime(3) */
1694 #ifndef SWM_DENY_CLOCK_FORMAT
1695 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1696 warnx("format too long");
1704 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1705 char fmtrep[SWM_BAR_MAX];
1707 struct swm_region *r;
1709 /* expand the format by first passing it through strftime(3) */
1710 bar_fmt_expand(fmtexp, sizeof fmtexp);
1712 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1713 for (i = 0; i < num_screens; i++) {
1714 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1717 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1718 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1719 bar_print(r, fmtrep);
1730 if (bar_enabled == 0)
1732 if (bar_extra && bar_extra_running) {
1733 /* ignore short reads; it'll correct itself */
1734 while ((b = fgetln(stdin, &len)) != NULL)
1735 if (b && b[len - 1] == '\n') {
1737 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1739 if (b == NULL && errno != EAGAIN) {
1740 warn("bar_update: bar_extra failed");
1744 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1757 bar_toggle(struct swm_region *r, union arg *args)
1759 struct swm_region *tmpr;
1762 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1764 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1766 for (i = 0; i < num_screens; i++)
1767 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1769 xcb_unmap_window(conn, tmpr->bar->id);
1771 for (i = 0; i < num_screens; i++)
1772 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1774 map_window_raised(tmpr->bar->id);
1777 bar_enabled = !bar_enabled;
1780 /* must be after stack */
1789 struct swm_region *r;
1793 /* do this here because the conf file is in memory */
1794 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1795 /* launch external status app */
1796 bar_extra_running = 1;
1797 if (pipe(bar_pipe) == -1)
1798 err(1, "pipe error");
1799 socket_setnonblock(bar_pipe[0]);
1800 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1801 if (dup2(bar_pipe[0], 0) == -1)
1803 if (dup2(bar_pipe[1], 1) == -1)
1805 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1806 err(1, "could not disable SIGPIPE");
1807 switch (bar_pid = fork()) {
1809 err(1, "cannot fork");
1813 execvp(bar_argv[0], bar_argv);
1814 err(1, "%s external app failed", bar_argv[0]);
1816 default: /* parent */
1822 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1823 for (i = 0; i < num_screens; i++)
1824 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1827 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1828 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1829 xcb_change_window_attributes(conn, r->bar->id,
1830 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1836 bar_setup(struct swm_region *r)
1838 char *default_string;
1839 char **missing_charsets;
1840 int num_missing_charsets = 0;
1842 xcb_screen_t *screen = get_screen(r->s->idx);
1847 XFreeFontSet(display, bar_fs);
1852 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1853 err(1, "bar_setup: calloc: failed to allocate memory.");
1855 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1857 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1858 &num_missing_charsets, &default_string);*/
1860 if (num_missing_charsets > 0) {
1861 warnx("Unable to load charset(s):");
1863 for (i = 0; i < num_missing_charsets; ++i)
1864 warnx("%s", missing_charsets[i]);
1866 XFreeStringList(missing_charsets);
1868 if (strcmp(default_string, ""))
1869 warnx("Glyphs from those sets will be replaced "
1870 "by '%s'.", default_string);
1872 warnx("Glyphs from those sets won't be drawn.");
1876 errx(1, "Error creating font set structure.");
1878 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1880 bar_height = bar_fs_extents->max_logical_extent.height +
1881 2 * bar_border_width;
1890 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1891 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1892 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1894 r->bar->id = xcb_generate_id(conn);
1895 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1896 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1897 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1898 DNPRINTF(SWM_D_BAR, "bar_setup: create_window: (x,y) w x h: (%d,%d) "
1899 "%d x %d\n", X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar));
1900 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1901 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1902 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1903 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1904 | XCB_CW_EVENT_MASK, wa);
1906 r->bar->buffer = xcb_generate_id(conn);
1907 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1908 WIDTH(r->bar), HEIGHT(r->bar));
1910 xcb_randr_select_input(conn, r->bar->id,
1911 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1914 map_window_raised(r->bar->id);
1916 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1917 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1920 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1921 err(1, "could not install bar_signal");
1926 bar_cleanup(struct swm_region *r)
1930 xcb_destroy_window(conn, r->bar->id);
1931 xcb_free_pixmap(conn, r->bar->buffer);
1937 set_win_state(struct ws_win *win, uint16_t state)
1939 uint16_t data[2] = { state, XCB_ATOM_NONE };
1941 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
1946 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
1947 a_state, 32, 2, data);
1951 getstate(xcb_window_t w)
1953 uint16_t result = 0;
1954 xcb_get_property_cookie_t c;
1955 xcb_get_property_reply_t *r;
1957 c = xcb_get_property(conn, False, w, a_state, a_state, 0L, 2L);
1958 r = xcb_get_property_reply(conn, c, NULL);
1961 result = *((uint16_t *)xcb_get_property_value(r));
1965 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
1971 version(struct swm_region *r, union arg *args)
1973 bar_version = !bar_version;
1975 snprintf(bar_vertext, sizeof bar_vertext,
1976 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1978 strlcpy(bar_vertext, "", sizeof bar_vertext);
1985 client_msg(struct ws_win *win, xcb_atom_t a)
1987 xcb_client_message_event_t ev;
1992 bzero(&ev, sizeof ev);
1993 ev.response_type = XCB_CLIENT_MESSAGE;
1994 ev.window = win->id;
1997 ev.data.data32[0] = a;
1998 ev.data.data32[1] = XCB_CURRENT_TIME;
2000 xcb_send_event(conn, False, win->id,
2001 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2004 /* synthetic response to a ConfigureRequest when not making a change */
2006 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2008 xcb_configure_notify_event_t ce;
2013 /* send notification of unchanged state. */
2014 bzero(&ce, sizeof(ce));
2015 ce.response_type = XCB_CONFIGURE_NOTIFY;
2018 ce.width = WIDTH(win);
2019 ce.height = HEIGHT(win);
2020 ce.override_redirect = False;
2025 ce.window = win->id;
2026 ce.border_width = BORDER(win);
2027 ce.above_sibling = XCB_WINDOW_NONE;
2030 ce.event = ev->window;
2031 ce.window = ev->window;
2033 /* make response appear more WM_SIZE_HINTS-compliant */
2035 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2036 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2037 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2038 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2039 SH_INC_W(win), SH_INC_H(win));
2043 /* the hint may be set... to 0! */
2044 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2045 ce.width = SH_MIN_W(win);
2046 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2047 ce.height = SH_MIN_H(win);
2052 /* may also be advertized as 0 */
2053 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2054 ce.width = SH_MAX_W(win);
2055 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2056 ce.height = SH_MAX_H(win);
2059 /* resize increment. */
2061 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2062 ce.width -= (ce.width - SH_MIN_W(win)) %
2064 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2065 ce.height -= (ce.height - SH_MIN_H(win)) %
2069 /* adjust x and y for requested border_width. */
2070 ce.x += BORDER(win) - ev->border_width;
2071 ce.y += BORDER(win) - ev->border_width;
2072 ce.border_width = ev->border_width;
2073 ce.above_sibling = ev->sibling;
2076 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2077 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2078 ce.y, ce.width, ce.height, ce.border_width);
2080 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2085 count_win(struct workspace *ws, int count_transient)
2090 TAILQ_FOREACH(win, &ws->winlist, entry) {
2091 if (count_transient == 0 && win->floating)
2093 if (count_transient == 0 && win->transient)
2099 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2105 quit(struct swm_region *r, union arg *args)
2107 DNPRINTF(SWM_D_MISC, "quit\n");
2112 unmap_window(struct ws_win *win)
2117 /* don't unmap again */
2118 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2121 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2123 xcb_unmap_window(conn, win->id);
2124 xcb_change_window_attributes(conn, win->id,
2125 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2132 int i, j, num_screens;
2134 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2135 for (i = 0; i < num_screens; i++)
2136 for (j = 0; j < workspace_limit; j++)
2137 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2142 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2144 xcb_key_press_event_t event;
2145 xcb_keycode_t *keycode;
2150 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2152 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2155 bzero(&event, sizeof(event));
2156 event.event = win->id;
2157 event.root = win->s->root;
2158 event.child = XCB_WINDOW_NONE;
2159 event.time = XCB_CURRENT_TIME;
2160 event.event_x = X(win);
2161 event.event_y = Y(win);
2164 event.same_screen = True;
2165 event.detail = *keycode;
2166 event.state = modifiers;
2168 event.response_type = XCB_KEY_PRESS;
2169 xcb_send_event(conn, True, win->id,
2170 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2172 event.response_type = XCB_KEY_RELEASE;
2173 xcb_send_event(conn, True, win->id,
2174 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2178 restart(struct swm_region *r, union arg *args)
2180 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2182 /* disable alarm because the following code may not be interrupted */
2184 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2185 err(1, "can't disable alarm");
2191 xcb_key_symbols_free(syms);
2193 xcb_disconnect(conn);
2195 execvp(start_argv[0], start_argv);
2196 warn("execvp failed");
2201 root_to_region(xcb_window_t root)
2203 struct swm_region *r = NULL;
2205 xcb_query_pointer_reply_t *qpr;
2207 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2209 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2210 for (i = 0; i < num_screens; i++)
2211 if (screens[i].root == root)
2214 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2215 screens[i].root), NULL);
2218 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2219 qpr->root_x, qpr->root_y);
2220 /* choose a region based on pointer location */
2221 TAILQ_FOREACH(r, &screens[i].rl, entry)
2222 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2223 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2229 r = TAILQ_FIRST(&screens[i].rl);
2235 find_unmanaged_window(xcb_window_t id)
2238 int i, j, num_screens;
2240 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2241 for (i = 0; i < num_screens; i++)
2242 for (j = 0; j < workspace_limit; j++)
2243 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2251 find_window(xcb_window_t id)
2254 int i, j, num_screens;
2255 xcb_query_tree_reply_t *r;
2257 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2258 for (i = 0; i < num_screens; i++)
2259 for (j = 0; j < workspace_limit; j++)
2260 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2264 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2268 /* if we were looking for the parent return that window instead */
2269 if (r->parent == 0 || r->root == r->parent)
2272 /* look for parent */
2273 for (i = 0; i < num_screens; i++)
2274 for (j = 0; j < workspace_limit; j++)
2275 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2276 if (r->parent == win->id) {
2286 spawn(int ws_idx, union arg *args, int close_fd)
2291 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2293 close(xcb_get_file_descriptor(conn));
2295 setenv("LD_PRELOAD", SWM_LIB, 1);
2297 if (asprintf(&ret, "%d", ws_idx) == -1) {
2298 warn("spawn: asprintf SWM_WS");
2301 setenv("_SWM_WS", ret, 1);
2305 if (asprintf(&ret, "%d", getpid()) == -1) {
2306 warn("spawn: asprintf _SWM_PID");
2309 setenv("_SWM_PID", ret, 1);
2313 if (setsid() == -1) {
2314 warn("spawn: setsid");
2320 * close stdin and stdout to prevent interaction between apps
2321 * and the baraction script
2322 * leave stderr open to record errors
2324 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2325 warn("spawn: open");
2328 dup2(fd, STDIN_FILENO);
2329 dup2(fd, STDOUT_FILENO);
2334 execvp(args->argv[0], args->argv);
2336 warn("spawn: execvp");
2341 kill_refs(struct ws_win *win)
2343 int i, x, num_screens;
2344 struct swm_region *r;
2345 struct workspace *ws;
2350 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2351 for (i = 0; i < num_screens; i++)
2352 TAILQ_FOREACH(r, &screens[i].rl, entry)
2353 for (x = 0; x < workspace_limit; x++) {
2355 if (win == ws->focus)
2357 if (win == ws->focus_prev)
2358 ws->focus_prev = NULL;
2363 validate_win(struct ws_win *testwin)
2366 struct workspace *ws;
2367 struct swm_region *r;
2368 int i, x, num_screens;
2370 if (testwin == NULL)
2373 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2374 for (i = 0; i < num_screens; i++)
2375 TAILQ_FOREACH(r, &screens[i].rl, entry)
2376 for (x = 0; x < workspace_limit; x++) {
2378 TAILQ_FOREACH(win, &ws->winlist, entry)
2386 validate_ws(struct workspace *testws)
2388 struct swm_region *r;
2389 struct workspace *ws;
2390 int i, x, num_screens;
2392 /* validate all ws */
2393 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2394 for (i = 0; i < num_screens; i++)
2395 TAILQ_FOREACH(r, &screens[i].rl, entry)
2396 for (x = 0; x < workspace_limit; x++) {
2405 unfocus_win(struct ws_win *win)
2407 xcb_window_t none = XCB_WINDOW_NONE;
2409 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2413 if (win->ws == NULL)
2416 if (validate_ws(win->ws))
2417 return; /* XXX this gets hit with thunderbird, needs fixing */
2419 if (win->ws->r == NULL)
2422 if (validate_win(win)) {
2427 if (win->ws->focus == win) {
2428 win->ws->focus = NULL;
2429 win->ws->focus_prev = win;
2432 if (validate_win(win->ws->focus)) {
2433 kill_refs(win->ws->focus);
2434 win->ws->focus = NULL;
2436 if (validate_win(win->ws->focus_prev)) {
2437 kill_refs(win->ws->focus_prev);
2438 win->ws->focus_prev = NULL;
2441 grabbuttons(win, 0);
2442 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2443 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2444 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2445 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2452 int i, j, num_screens;
2454 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2456 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2457 for (i = 0; i < num_screens; i++)
2458 for (j = 0; j < workspace_limit; j++)
2459 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2464 focus_win(struct ws_win *win)
2466 struct ws_win *cfw = NULL;
2467 xcb_get_input_focus_reply_t *r;
2468 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2470 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2474 if (win->ws == NULL)
2477 if (validate_ws(win->ws))
2478 return; /* XXX this gets hit with thunderbird, needs fixing */
2480 if (validate_win(win)) {
2485 if (validate_win(win)) {
2490 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2492 cur_focus = r->focus;
2495 if ((cfw = find_window(cur_focus)) != NULL)
2498 /* use larger hammer since the window was killed somehow */
2499 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2500 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2501 xcb_change_window_attributes(conn, cfw->id,
2502 XCB_CW_BORDER_PIXEL,
2503 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2506 win->ws->focus = win;
2508 if (win->ws->r != NULL) {
2510 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2511 win->id, XCB_CURRENT_TIME);
2512 grabbuttons(win, 1);
2513 xcb_change_window_attributes(conn, win->id,
2514 XCB_CW_BORDER_PIXEL,
2515 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2516 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2517 win->ws->always_raise)
2518 map_window_raised(win->id);
2520 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2521 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2529 event_drain(uint8_t rt)
2531 xcb_generic_event_t *evt;
2534 while ((evt = xcb_poll_for_event(conn))) {
2535 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2543 switchws(struct swm_region *r, union arg *args)
2545 int wsid = args->id, unmap_old = 0;
2546 struct swm_region *this_r, *other_r;
2548 struct workspace *new_ws, *old_ws;
2554 if (wsid >= workspace_limit)
2558 old_ws = this_r->ws;
2559 new_ws = &this_r->s->ws[wsid];
2561 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2562 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2564 if (new_ws == NULL || old_ws == NULL)
2566 if (new_ws == old_ws)
2569 other_r = new_ws->r;
2570 if (other_r == NULL) {
2571 /* the other workspace is hidden, hide this one */
2575 /* the other ws is visible in another region, exchange them */
2576 other_r->ws_prior = new_ws;
2577 other_r->ws = old_ws;
2578 old_ws->r = other_r;
2580 this_r->ws_prior = old_ws;
2581 this_r->ws = new_ws;
2584 /* this is needed so that we can click on a window after a restart */
2588 a.id = SWM_ARG_ID_FOCUSCUR;
2589 focus(new_ws->r, &a);
2591 /* unmap old windows */
2593 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2596 if (focus_mode == SWM_FOCUS_DEFAULT)
2597 event_drain(XCB_ENTER_NOTIFY);
2603 cyclews(struct swm_region *r, union arg *args)
2606 struct swm_screen *s = r->s;
2609 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2610 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2615 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2618 case SWM_ARG_ID_CYCLEWS_UP:
2619 if (a.id < workspace_limit - 1)
2624 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2627 case SWM_ARG_ID_CYCLEWS_DOWN:
2631 a.id = workspace_limit - 1;
2638 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2640 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2644 } while (a.id != r->ws->idx);
2648 priorws(struct swm_region *r, union arg *args)
2652 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2653 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2655 if (r->ws_prior == NULL)
2658 a.id = r->ws_prior->idx;
2663 cyclescr(struct swm_region *r, union arg *args)
2665 struct swm_region *rr = NULL;
2667 int i, x, y, num_screens;
2669 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2670 /* do nothing if we don't have more than one screen */
2671 if (!(num_screens > 1 || outputs > 1))
2676 case SWM_ARG_ID_CYCLESC_UP:
2677 rr = TAILQ_NEXT(r, entry);
2679 rr = TAILQ_FIRST(&screens[i].rl);
2681 case SWM_ARG_ID_CYCLESC_DOWN:
2682 rr = TAILQ_PREV(r, swm_region_list, entry);
2684 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2692 /* move mouse to region */
2694 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2695 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2698 a.id = SWM_ARG_ID_FOCUSCUR;
2701 if (rr->ws->focus) {
2702 /* move to focus window */
2703 x = X(rr->ws->focus) + 1;
2704 y = Y(rr->ws->focus) + 1;
2705 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2711 sort_windows(struct ws_win_list *wl)
2713 struct ws_win *win, *parent, *nxt;
2718 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2719 nxt = TAILQ_NEXT(win, entry);
2720 if (win->transient) {
2721 parent = find_window(win->transient);
2722 if (parent == NULL) {
2723 warnx("not possible bug");
2726 TAILQ_REMOVE(wl, win, entry);
2727 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2734 swapwin(struct swm_region *r, union arg *args)
2736 struct ws_win *target, *source;
2737 struct ws_win *cur_focus;
2738 struct ws_win_list *wl;
2741 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2742 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2744 cur_focus = r->ws->focus;
2745 if (cur_focus == NULL)
2749 wl = &source->ws->winlist;
2752 case SWM_ARG_ID_SWAPPREV:
2753 if (source->transient)
2754 source = find_window(source->transient);
2755 target = TAILQ_PREV(source, ws_win_list, entry);
2756 if (target && target->transient)
2757 target = find_window(target->transient);
2758 TAILQ_REMOVE(wl, source, entry);
2760 TAILQ_INSERT_TAIL(wl, source, entry);
2762 TAILQ_INSERT_BEFORE(target, source, entry);
2764 case SWM_ARG_ID_SWAPNEXT:
2765 target = TAILQ_NEXT(source, entry);
2766 /* move the parent and let the sort handle the move */
2767 if (source->transient)
2768 source = find_window(source->transient);
2769 TAILQ_REMOVE(wl, source, entry);
2771 TAILQ_INSERT_HEAD(wl, source, entry);
2773 TAILQ_INSERT_AFTER(wl, target, source, entry);
2775 case SWM_ARG_ID_SWAPMAIN:
2776 target = TAILQ_FIRST(wl);
2777 if (target == source) {
2778 if (source->ws->focus_prev != NULL &&
2779 source->ws->focus_prev != target)
2780 source = source->ws->focus_prev;
2784 if (target == NULL || source == NULL)
2786 source->ws->focus_prev = target;
2787 TAILQ_REMOVE(wl, target, entry);
2788 TAILQ_INSERT_BEFORE(source, target, entry);
2789 TAILQ_REMOVE(wl, source, entry);
2790 TAILQ_INSERT_HEAD(wl, source, entry);
2792 case SWM_ARG_ID_MOVELAST:
2793 TAILQ_REMOVE(wl, source, entry);
2794 TAILQ_INSERT_TAIL(wl, source, entry);
2797 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2809 focus_prev(struct ws_win *win)
2811 struct ws_win *winfocus = NULL;
2812 struct ws_win *cur_focus = NULL;
2813 struct ws_win_list *wl = NULL;
2814 struct workspace *ws = NULL;
2816 if (!(win && win->ws))
2821 cur_focus = ws->focus;
2823 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2824 WINID(win), WINID(cur_focus));
2826 /* pickle, just focus on whatever */
2827 if (cur_focus == NULL) {
2828 /* use prev_focus if valid */
2829 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2830 find_window(WINID(ws->focus_prev)))
2831 winfocus = ws->focus_prev;
2835 /* if transient focus on parent */
2836 if (cur_focus->transient) {
2837 winfocus = find_window(cur_focus->transient);
2841 /* if in max_stack try harder */
2842 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2843 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2844 if (cur_focus != ws->focus_prev)
2845 winfocus = ws->focus_prev;
2847 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2852 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2854 if (winfocus == NULL || winfocus == win) {
2855 switch (focus_close) {
2856 case SWM_STACK_BOTTOM:
2857 winfocus = TAILQ_FIRST(wl);
2860 winfocus = TAILQ_LAST(wl, ws_win_list);
2862 case SWM_STACK_ABOVE:
2863 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2864 if (focus_close_wrap)
2865 winfocus = TAILQ_FIRST(wl);
2867 winfocus = TAILQ_PREV(cur_focus,
2868 ws_win_list, entry);
2871 case SWM_STACK_BELOW:
2872 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2874 if (focus_close_wrap)
2875 winfocus = TAILQ_LAST(wl, ws_win_list);
2877 winfocus = TAILQ_NEXT(cur_focus, entry);
2883 if (winfocus == NULL) {
2884 if (focus_default == SWM_STACK_TOP)
2885 winfocus = TAILQ_LAST(wl, ws_win_list);
2887 winfocus = TAILQ_FIRST(wl);
2891 focus_magic(winfocus);
2895 focus(struct swm_region *r, union arg *args)
2897 struct ws_win *winfocus = NULL, *head;
2898 struct ws_win *cur_focus = NULL;
2899 struct ws_win_list *wl = NULL;
2900 struct workspace *ws = NULL;
2906 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2908 /* treat FOCUS_CUR special */
2909 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2910 if (r->ws->focus && r->ws->focus->iconic == 0)
2911 winfocus = r->ws->focus;
2912 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2913 winfocus = r->ws->focus_prev;
2915 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2916 if (winfocus->iconic == 0)
2919 focus_magic(winfocus);
2923 if ((cur_focus = r->ws->focus) == NULL)
2927 if (TAILQ_EMPTY(wl))
2929 /* make sure there is at least one uniconified window */
2931 TAILQ_FOREACH(winfocus, wl, entry)
2932 if (winfocus->iconic == 0) {
2940 case SWM_ARG_ID_FOCUSPREV:
2941 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2943 head = TAILQ_LAST(wl, ws_win_list);
2945 if (WINID(winfocus) == cur_focus->transient) {
2946 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2948 head = TAILQ_LAST(wl, ws_win_list);
2953 if (winfocus && winfocus->iconic) {
2954 while (winfocus != cur_focus) {
2955 if (winfocus == NULL)
2956 winfocus = TAILQ_LAST(wl, ws_win_list);
2957 if (winfocus->iconic == 0)
2959 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2965 case SWM_ARG_ID_FOCUSNEXT:
2966 head = TAILQ_NEXT(cur_focus, entry);
2968 head = TAILQ_FIRST(wl);
2972 if (winfocus && winfocus->iconic) {
2973 while (winfocus != cur_focus) {
2974 if (winfocus == NULL)
2975 winfocus = TAILQ_FIRST(wl);
2976 if (winfocus->iconic == 0)
2978 winfocus = TAILQ_NEXT(winfocus, entry);
2983 case SWM_ARG_ID_FOCUSMAIN:
2984 winfocus = TAILQ_FIRST(wl);
2985 if (winfocus == cur_focus)
2986 winfocus = cur_focus->ws->focus_prev;
2993 focus_magic(winfocus);
2999 cycle_layout(struct swm_region *r, union arg *args)
3001 struct workspace *ws = r->ws;
3004 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3007 if (ws->cur_layout->l_stack == NULL)
3008 ws->cur_layout = &layouts[0];
3013 if (focus_mode == SWM_FOCUS_DEFAULT)
3014 event_drain(XCB_ENTER_NOTIFY);
3016 a.id = SWM_ARG_ID_FOCUSCUR;
3021 stack_config(struct swm_region *r, union arg *args)
3023 struct workspace *ws = r->ws;
3025 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3028 if (ws->cur_layout->l_config != NULL)
3029 ws->cur_layout->l_config(ws, args->id);
3031 if (args->id != SWM_ARG_ID_STACKINIT)
3038 struct swm_geometry g;
3039 struct swm_region *r;
3045 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3047 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3048 for (i = 0; i < num_screens; i++) {
3052 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3053 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3054 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3056 /* start with screen geometry, adjust for bar */
3058 g.w -= 2 * border_width;
3059 g.h -= 2 * border_width;
3065 r->ws->cur_layout->l_stack(r->ws, &g);
3066 r->ws->cur_layout->l_string(r->ws);
3067 /* save r so we can track region changes */
3074 if (focus_mode == SWM_FOCUS_DEFAULT)
3075 event_drain(XCB_ENTER_NOTIFY);
3077 DNPRINTF(SWM_D_STACK, "stack: end\n");
3081 store_float_geom(struct ws_win *win, struct swm_region *r)
3083 /* retain window geom and region geom */
3084 win->g_float = win->g;
3085 win->g_float.x -= X(r);
3086 win->g_float.y -= Y(r);
3087 win->g_floatvalid = 1;
3088 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3089 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3090 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3091 win->g_float.w, win->g_float.h);
3095 stack_floater(struct ws_win *win, struct swm_region *r)
3100 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3103 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3104 * geom on ws switches or return from max mode
3106 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3107 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3108 /* refloat at last floating relative position */
3109 win->g = win->g_float;
3114 win->floatmaxed = 0;
3117 * if set to fullscreen mode, configure window to maximum size.
3119 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3120 if (!win->g_floatvalid)
3121 store_float_geom(win, win->ws->r);
3123 win->g = win->ws->r->g;
3127 * remove border on fullscreen floater when in fullscreen mode or when
3128 * the quirk is present.
3130 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3131 ((win->quirks & SWM_Q_FULLSCREEN) &&
3132 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3133 if (win->bordered) {
3135 X(win) += border_width;
3136 Y(win) += border_width;
3138 } else if (!win->bordered) {
3140 X(win) -= border_width;
3141 Y(win) -= border_width;
3144 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3145 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3146 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3151 * floaters and transients are auto-centred unless moved
3154 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3155 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3158 /* keep window within region bounds */
3159 constrain_window(win, r, 0);
3165 * Send keystrokes to terminal to decrease/increase the font size as the
3166 * window size changes.
3169 adjust_font(struct ws_win *win)
3171 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3172 win->floating || win->transient)
3175 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3176 WIDTH(win) / win->sh.width_inc < term_width &&
3177 win->font_steps < SWM_MAX_FONT_STEPS) {
3178 win->font_size_boundary[win->font_steps] =
3179 (win->sh.width_inc * term_width) + win->sh.base_width;
3182 win->last_inc = win->sh.width_inc;
3183 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3184 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3185 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3188 win->last_inc = win->sh.width_inc;
3189 fake_keypress(win, XK_KP_Add, ShiftMask);
3193 #define SWAPXY(g) do { \
3195 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3196 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3199 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3201 struct swm_geometry win_g, r_g = *g;
3202 struct ws_win *win, *fs_win = NULL;
3203 int i, j, s, stacks;
3204 int w_inc = 1, h_inc, w_base = 1, h_base;
3205 int hrh, extra = 0, h_slice, last_h = 0;
3206 int split, colno, winno, mwin, msize, mscale;
3207 int remain, missing, v_slice, reconfigure;
3209 xcb_get_window_attributes_reply_t *war;
3211 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3212 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3214 winno = count_win(ws, 0);
3215 if (winno == 0 && count_win(ws, 1) == 0)
3218 TAILQ_FOREACH(win, &ws->winlist, entry)
3219 if (win->transient == 0 && win->floating == 0
3220 && win->iconic == 0)
3227 w_inc = win->sh.width_inc;
3228 w_base = win->sh.base_width;
3229 mwin = ws->l_state.horizontal_mwin;
3230 mscale = ws->l_state.horizontal_msize;
3231 stacks = ws->l_state.horizontal_stacks;
3234 w_inc = win->sh.height_inc;
3235 w_base = win->sh.base_height;
3236 mwin = ws->l_state.vertical_mwin;
3237 mscale = ws->l_state.vertical_msize;
3238 stacks = ws->l_state.vertical_stacks;
3242 if (stacks > winno - mwin)
3243 stacks = winno - mwin;
3247 h_slice = r_g.h / SWM_H_SLICE;
3248 if (mwin && winno > mwin) {
3249 v_slice = r_g.w / SWM_V_SLICE;
3253 win_g.w = v_slice * mscale;
3255 if (w_inc > 1 && w_inc < v_slice) {
3256 /* adjust for window's requested size increment */
3257 remain = (win_g.w - w_base) % w_inc;
3263 win_g.x += r_g.w - msize;
3266 colno = split = winno / stacks;
3267 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3268 2 * border_width) / stacks);
3270 hrh = r_g.h / colno;
3271 extra = r_g.h - (colno * hrh);
3272 win_g.h = hrh - 2 * border_width;
3274 /* stack all the tiled windows */
3275 i = j = 0, s = stacks;
3276 TAILQ_FOREACH(win, &ws->winlist, entry) {
3277 if (win->transient != 0 || win->floating != 0)
3279 if (win->iconic != 0)
3282 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3287 if (split && i == split) {
3288 colno = (winno - mwin) / stacks;
3289 if (s <= (winno - mwin) % stacks)
3291 split = split + colno;
3292 hrh = (r_g.h / colno);
3293 extra = r_g.h - (colno * hrh);
3297 win_g.x += win_g.w + 2 * border_width;
3298 win_g.w = (r_g.w - msize -
3299 (stacks * 2 * border_width)) / stacks;
3301 win_g.w += (r_g.w - msize -
3302 (stacks * 2 * border_width)) % stacks;
3306 win_g.h = hrh - 2 * border_width;
3308 h_inc = win->sh.width_inc;
3309 h_base = win->sh.base_width;
3311 h_inc = win->sh.height_inc;
3312 h_base = win->sh.base_height;
3314 if (j == colno - 1) {
3315 win_g.h = hrh + extra;
3316 } else if (h_inc > 1 && h_inc < h_slice) {
3317 /* adjust for window's requested size increment */
3318 remain = (win_g.h - h_base) % h_inc;
3319 missing = h_inc - remain;
3321 if (missing <= extra || j == 0) {
3333 win_g.y += last_h + 2 * border_width;
3335 if (disable_border && bar_enabled == 0 && winno == 1){
3337 win_g.w += 2 * border_width;
3338 win_g.h += 2 * border_width;
3343 if (X(win) != win_g.y || Y(win) != win_g.x ||
3344 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3348 WIDTH(win) = win_g.h;
3349 HEIGHT(win) = win_g.w;
3352 if (X(win) != win_g.x || Y(win) != win_g.y ||
3353 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3357 WIDTH(win) = win_g.w;
3358 HEIGHT(win) = win_g.h;
3362 if (bordered != win->bordered) {
3364 win->bordered = bordered;
3372 war = xcb_get_window_attributes_reply(conn,
3373 xcb_get_window_attributes(conn, win->id),
3376 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3377 map_window_raised(win->id);
3387 /* now, stack all the floaters and transients */
3388 TAILQ_FOREACH(win, &ws->winlist, entry) {
3389 if (win->transient == 0 && win->floating == 0)
3391 if (win->iconic == 1)
3393 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3398 stack_floater(win, ws->r);
3399 map_window_raised(win->id);
3403 stack_floater(fs_win, ws->r);
3404 map_window_raised(fs_win->id);
3409 vertical_config(struct workspace *ws, int id)
3411 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3415 case SWM_ARG_ID_STACKRESET:
3416 case SWM_ARG_ID_STACKINIT:
3417 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3418 ws->l_state.vertical_mwin = 1;
3419 ws->l_state.vertical_stacks = 1;
3421 case SWM_ARG_ID_MASTERSHRINK:
3422 if (ws->l_state.vertical_msize > 1)
3423 ws->l_state.vertical_msize--;
3425 case SWM_ARG_ID_MASTERGROW:
3426 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3427 ws->l_state.vertical_msize++;
3429 case SWM_ARG_ID_MASTERADD:
3430 ws->l_state.vertical_mwin++;
3432 case SWM_ARG_ID_MASTERDEL:
3433 if (ws->l_state.vertical_mwin > 0)
3434 ws->l_state.vertical_mwin--;
3436 case SWM_ARG_ID_STACKINC:
3437 ws->l_state.vertical_stacks++;
3439 case SWM_ARG_ID_STACKDEC:
3440 if (ws->l_state.vertical_stacks > 1)
3441 ws->l_state.vertical_stacks--;
3443 case SWM_ARG_ID_FLIPLAYOUT:
3444 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3452 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3454 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3456 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3460 horizontal_config(struct workspace *ws, int id)
3462 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3465 case SWM_ARG_ID_STACKRESET:
3466 case SWM_ARG_ID_STACKINIT:
3467 ws->l_state.horizontal_mwin = 1;
3468 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3469 ws->l_state.horizontal_stacks = 1;
3471 case SWM_ARG_ID_MASTERSHRINK:
3472 if (ws->l_state.horizontal_msize > 1)
3473 ws->l_state.horizontal_msize--;
3475 case SWM_ARG_ID_MASTERGROW:
3476 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3477 ws->l_state.horizontal_msize++;
3479 case SWM_ARG_ID_MASTERADD:
3480 ws->l_state.horizontal_mwin++;
3482 case SWM_ARG_ID_MASTERDEL:
3483 if (ws->l_state.horizontal_mwin > 0)
3484 ws->l_state.horizontal_mwin--;
3486 case SWM_ARG_ID_STACKINC:
3487 ws->l_state.horizontal_stacks++;
3489 case SWM_ARG_ID_STACKDEC:
3490 if (ws->l_state.horizontal_stacks > 1)
3491 ws->l_state.horizontal_stacks--;
3493 case SWM_ARG_ID_FLIPLAYOUT:
3494 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3502 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3504 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3506 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3509 /* fullscreen view */
3511 max_stack(struct workspace *ws, struct swm_geometry *g)
3513 struct swm_geometry gg = *g;
3514 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3515 int winno, num_screens;
3517 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3522 winno = count_win(ws, 0);
3523 if (winno == 0 && count_win(ws, 1) == 0)
3526 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3527 TAILQ_FOREACH(win, &ws->winlist, entry) {
3528 if (win->transient) {
3530 parent = find_window(win->transient);
3534 if (win->floating && win->floatmaxed == 0 ) {
3536 * retain geometry for retrieval on exit from
3539 store_float_geom(win, ws->r);
3540 win->floatmaxed = 1;
3543 /* only reconfigure if necessary */
3544 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3545 HEIGHT(win) != gg.h) {
3551 WIDTH(win) += 2 * border_width;
3552 HEIGHT(win) += 2 * border_width;
3557 /* unmap only if we don't have multi screen */
3558 if (win != ws->focus)
3559 if (!(num_screens > 1 || outputs > 1))
3563 /* put the last transient on top */
3566 map_window_raised(parent->id);
3567 stack_floater(wintrans, ws->r);
3568 focus_magic(wintrans);
3573 send_to_ws(struct swm_region *r, union arg *args)
3575 int wsid = args->id;
3576 struct ws_win *win = NULL, *parent;
3577 struct workspace *ws, *nws;
3578 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3579 char ws_idx_str[SWM_PROPLEN];
3582 if (wsid >= workspace_limit)
3585 if (r && r->ws && r->ws->focus)
3591 if (win->ws->idx == wsid)
3594 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3597 nws = &win->s->ws[wsid];
3599 a.id = SWM_ARG_ID_FOCUSPREV;
3601 if (win->transient) {
3602 parent = find_window(win->transient);
3604 unmap_window(parent);
3605 TAILQ_REMOVE(&ws->winlist, parent, entry);
3606 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3611 TAILQ_REMOVE(&ws->winlist, win, entry);
3612 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3613 if (TAILQ_EMPTY(&ws->winlist))
3614 r->ws->focus = NULL;
3617 /* Try to update the window's workspace property */
3618 ws_idx_atom = get_atom_from_string("_SWM_WS");
3620 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3622 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3624 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3625 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3636 pressbutton(struct swm_region *r, union arg *args)
3638 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3639 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3640 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3641 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3645 raise_toggle(struct swm_region *r, union arg *args)
3647 if (r == NULL || r->ws == NULL)
3650 r->ws->always_raise = !r->ws->always_raise;
3652 /* bring floaters back to top */
3653 if (r->ws->always_raise == 0)
3660 iconify(struct swm_region *r, union arg *args)
3664 if (r->ws->focus == NULL)
3666 unmap_window(r->ws->focus);
3667 update_iconic(r->ws->focus, 1);
3670 r->ws->focus = NULL;
3671 a.id = SWM_ARG_ID_FOCUSCUR;
3678 get_win_name(xcb_window_t win)
3681 xcb_get_property_cookie_t c;
3682 xcb_icccm_get_text_property_reply_t r;
3684 c = xcb_icccm_get_wm_name(conn, win);
3685 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3686 if (r.name_len > 0) {
3687 name = malloc(r.name_len + 1);
3689 xcb_icccm_get_text_property_reply_wipe(&r);
3692 memcpy(name, r.name, r.name_len);
3693 name[r.name_len] = '\0';
3695 xcb_icccm_get_text_property_reply_wipe(&r);
3702 uniconify(struct swm_region *r, union arg *args)
3709 DNPRINTF(SWM_D_MISC, "uniconify\n");
3711 if (r == NULL || r->ws == NULL)
3714 /* make sure we have anything to uniconify */
3715 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3716 if (win->ws == NULL)
3717 continue; /* should never happen */
3718 if (win->iconic == 0)
3726 search_resp_action = SWM_SEARCH_UNICONIFY;
3728 spawn_select(r, args, "search", &searchpid);
3730 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3733 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3734 if (win->ws == NULL)
3735 continue; /* should never happen */
3736 if (win->iconic == 0)
3739 name = get_win_name(win->id);
3742 fprintf(lfile, "%s.%u\n", name, win->id);
3752 name_workspace(struct swm_region *r, union arg *args)
3756 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3762 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3764 spawn_select(r, args, "name_workspace", &searchpid);
3766 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3769 fprintf(lfile, "%s", "");
3774 search_workspace(struct swm_region *r, union arg *args)
3777 struct workspace *ws;
3780 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3786 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3788 spawn_select(r, args, "search", &searchpid);
3790 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3793 for (i = 0; i < workspace_limit; i++) {
3797 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3798 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3805 search_win_cleanup(void)
3807 struct search_window *sw = NULL;
3809 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3810 xcb_destroy_window(conn, sw->indicator);
3811 xcb_free_gc(conn, sw->gc);
3812 TAILQ_REMOVE(&search_wl, sw, entry);
3818 search_win(struct swm_region *r, union arg *args)
3820 struct ws_win *win = NULL;
3821 struct search_window *sw = NULL;
3823 uint32_t gcv[3], wa[2];
3828 XRectangle ibox, lbox;
3830 DNPRINTF(SWM_D_MISC, "search_win\n");
3833 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3835 spawn_select(r, args, "search", &searchpid);
3837 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3840 TAILQ_INIT(&search_wl);
3843 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3844 if (win->iconic == 1)
3847 sw = calloc(1, sizeof(struct search_window));
3849 warn("search_win: calloc");
3851 search_win_cleanup();
3857 snprintf(s, sizeof s, "%d", i);
3860 /* FIXME fix calculations */
3862 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3865 w = xcb_generate_id(conn);
3866 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3867 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3869 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3870 lbox.width + 4, bar_fs_extents->max_logical_extent.height,
3871 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3872 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3875 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3876 22, 20, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
3877 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
3878 XCB_CW_BORDER_PIXEL, wa);
3882 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3884 sw->gc = xcb_generate_id(conn);
3885 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3886 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3888 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3889 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3890 map_window_raised(w);
3893 xcb_image_text_8(conn, len, w, sw->gc, 2,
3894 (bar_fs_extents->max_logical_extent.height -
3895 lbox.height) / 2 - lbox.y, s);
3898 xcb_image_text_8(conn, len, w, sw->gc, 6, 14, s);
3901 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3903 fprintf(lfile, "%d\n", i);
3913 search_resp_uniconify(char *resp, unsigned long len)
3919 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3921 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3922 if (win->iconic == 0)
3924 name = get_win_name(win->id);
3927 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3932 if (strncmp(s, resp, len) == 0) {
3933 /* XXX this should be a callback to generalize */
3934 update_iconic(win, 0);
3943 search_resp_name_workspace(char *resp, unsigned long len)
3945 struct workspace *ws;
3947 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3949 if (search_r->ws == NULL)
3954 free(search_r->ws->name);
3955 search_r->ws->name = NULL;
3959 ws->name = strdup(resp);
3960 if (ws->name == NULL) {
3961 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3962 "strdup: %s", strerror(errno));
3969 search_resp_search_workspace(char *resp, unsigned long len)
3976 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3980 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3987 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3989 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3996 switchws(search_r, &a);
4000 search_resp_search_window(char *resp, unsigned long len)
4005 struct search_window *sw;
4007 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4011 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4016 idx = strtonum(s, 1, INT_MAX, &errstr);
4018 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4025 TAILQ_FOREACH(sw, &search_wl, entry)
4026 if (idx == sw->idx) {
4032 #define MAX_RESP_LEN 1024
4035 search_do_resp(void)
4041 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4046 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4047 warn("search: calloc");
4051 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4053 warn("search: read error");
4056 resp[rbytes] = '\0';
4059 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4060 * newline, so work around that by sanitizing the resp now.
4062 resp[strcspn(resp, "\n")] = '\0';
4065 switch (search_resp_action) {
4066 case SWM_SEARCH_UNICONIFY:
4067 search_resp_uniconify(resp, len);
4069 case SWM_SEARCH_NAME_WORKSPACE:
4070 search_resp_name_workspace(resp, len);
4072 case SWM_SEARCH_SEARCH_WORKSPACE:
4073 search_resp_search_workspace(resp, len);
4075 case SWM_SEARCH_SEARCH_WINDOW:
4076 search_resp_search_window(resp, len);
4081 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4082 search_win_cleanup();
4084 search_resp_action = SWM_SEARCH_NONE;
4085 close(select_resp_pipe[0]);
4090 wkill(struct swm_region *r, union arg *args)
4092 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4094 if (r->ws->focus == NULL)
4097 if (args->id == SWM_ARG_ID_KILLWINDOW)
4098 xcb_kill_client(conn, r->ws->focus->id);
4100 if (r->ws->focus->can_delete)
4101 client_msg(r->ws->focus, a_delete);
4108 floating_toggle_win(struct ws_win *win)
4110 struct swm_region *r;
4120 /* reject floating toggles in max stack mode */
4121 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4124 if (win->floating) {
4125 if (!win->floatmaxed) {
4126 /* retain position for refloat */
4127 store_float_geom(win, r);
4131 if (win->g_floatvalid) {
4132 /* refloat at last floating relative position */
4133 X(win) = win->g_float.x + X(r);
4134 Y(win) = win->g_float.y + Y(r);
4135 WIDTH(win) = win->g_float.w;
4136 HEIGHT(win) = win->g_float.h;
4141 ewmh_update_actions(win);
4147 floating_toggle(struct swm_region *r, union arg *args)
4149 struct ws_win *win = r->ws->focus;
4155 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4156 _NET_WM_STATE_TOGGLE);
4160 if (win == win->ws->focus) {
4161 a.id = SWM_ARG_ID_FOCUSCUR;
4162 focus(win->ws->r, &a);
4166 event_drain(XCB_ENTER_NOTIFY);
4170 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4172 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4174 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4176 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4179 if (X(win) + BORDER(win) < X(r)) {
4181 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4183 X(win) = X(r) - BORDER(win);
4186 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4188 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4190 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4193 if (Y(win) + BORDER(win) < Y(r)) {
4195 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4197 Y(win) = Y(r) - BORDER(win);
4203 if (HEIGHT(win) < 1)
4209 update_window(struct ws_win *win)
4214 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4215 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4216 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4220 wc[3] = HEIGHT(win);
4221 wc[4] = BORDER(win);
4223 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4224 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4225 wc[3], YESNO(win->bordered));
4227 xcb_configure_window(conn, win->id, mask, wc);
4230 #define SWM_RESIZE_STEPS (50)
4233 resize(struct ws_win *win, union arg *args)
4235 xcb_timestamp_t timestamp = 0;
4236 struct swm_region *r = NULL;
4237 int resize_step = 0;
4238 struct swm_geometry g;
4239 int top = 0, left = 0, resizing;
4241 unsigned int shape; /* cursor style */
4242 xcb_cursor_t cursor;
4243 xcb_font_t cursor_font;
4244 xcb_grab_pointer_cookie_t gpc;
4245 xcb_grab_pointer_reply_t *gpr;
4246 xcb_query_pointer_reply_t *xpr;
4247 xcb_generic_event_t *evt;
4248 xcb_motion_notify_event_t *mne;
4254 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4257 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4258 "transient: 0x%x\n", win->id, YESNO(win->floating),
4261 if (!(win->transient != 0 || win->floating != 0))
4264 /* reject resizes in max mode for floaters (transient ok) */
4265 if (win->floatmaxed)
4269 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4275 case SWM_ARG_ID_WIDTHSHRINK:
4276 WIDTH(win) -= SWM_RESIZE_STEPS;
4279 case SWM_ARG_ID_WIDTHGROW:
4280 WIDTH(win) += SWM_RESIZE_STEPS;
4283 case SWM_ARG_ID_HEIGHTSHRINK:
4284 HEIGHT(win) -= SWM_RESIZE_STEPS;
4287 case SWM_ARG_ID_HEIGHTGROW:
4288 HEIGHT(win) += SWM_RESIZE_STEPS;
4295 constrain_window(win, r, 1);
4297 store_float_geom(win,r);
4301 /* get cursor offset from window root */
4302 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4309 if (xpr->win_x < WIDTH(win) / 2)
4312 if (xpr->win_y < HEIGHT(win) / 2)
4315 if (args->id == SWM_ARG_ID_CENTER)
4318 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4320 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4322 cursor_font = xcb_generate_id(conn);
4323 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4325 cursor = xcb_generate_id(conn);
4326 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4327 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4329 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4330 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4332 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4334 xcb_free_cursor(conn, cursor);
4335 xcb_close_font(conn, cursor_font);
4342 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4343 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4344 case XCB_BUTTON_RELEASE:
4345 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4348 case XCB_MOTION_NOTIFY:
4349 mne = (xcb_motion_notify_event_t *)evt;
4350 /* cursor offset/delta from start of the operation */
4351 dx = mne->root_x - xpr->root_x;
4352 dy = mne->root_y - xpr->root_y;
4358 if (args->id == SWM_ARG_ID_CENTER) {
4359 if (g.h / 2 + dy < 1)
4363 HEIGHT(win) = g.h + 2 * dy;
4371 HEIGHT(win) = g.h + dy;
4378 if (args->id == SWM_ARG_ID_CENTER) {
4379 if (g.w / 2 + dx < 1)
4383 WIDTH(win) = g.w + 2 * dx;
4391 WIDTH(win) = g.w + dx;
4394 constrain_window(win, r, 1);
4396 /* not free, don't sync more than 120 times / second */
4397 if ((mne->time - timestamp) > (1000 / 120) ) {
4398 timestamp = mne->time;
4413 store_float_geom(win,r);
4415 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4416 xcb_free_cursor(conn, cursor);
4417 xcb_close_font(conn, cursor_font);
4420 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4424 resize_step(struct swm_region *r, union arg *args)
4426 struct ws_win *win = NULL;
4428 if (r && r->ws && r->ws->focus)
4436 #define SWM_MOVE_STEPS (50)
4439 move(struct ws_win *win, union arg *args)
4441 xcb_timestamp_t timestamp = 0;
4442 int move_step = 0, moving;
4443 struct swm_region *r = NULL;
4444 xcb_font_t cursor_font;
4445 xcb_cursor_t cursor;
4446 xcb_grab_pointer_cookie_t gpc;
4447 xcb_grab_pointer_reply_t *gpr;
4448 xcb_query_pointer_reply_t *qpr;
4449 xcb_generic_event_t *evt;
4450 xcb_motion_notify_event_t *mne;
4456 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4459 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4460 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4462 /* in max_stack mode should only move transients */
4463 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4467 if (win->floating == 0 && !win->transient) {
4468 store_float_geom(win, r);
4469 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4472 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4479 case SWM_ARG_ID_MOVELEFT:
4480 X(win) -= (SWM_MOVE_STEPS - border_width);
4483 case SWM_ARG_ID_MOVERIGHT:
4484 X(win) += (SWM_MOVE_STEPS - border_width);
4487 case SWM_ARG_ID_MOVEUP:
4488 Y(win) -= (SWM_MOVE_STEPS - border_width);
4491 case SWM_ARG_ID_MOVEDOWN:
4492 Y(win) += (SWM_MOVE_STEPS - border_width);
4499 constrain_window(win, r, 0);
4501 store_float_geom(win, r);
4505 cursor_font = xcb_generate_id(conn);
4506 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4508 cursor = xcb_generate_id(conn);
4509 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4510 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4512 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4513 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4514 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4515 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4517 xcb_free_cursor(conn, cursor);
4518 xcb_close_font(conn, cursor_font);
4522 /* get cursor offset from window root */
4523 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4526 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4527 xcb_free_cursor(conn, cursor);
4528 xcb_close_font(conn, cursor_font);
4534 while ((evt = xcb_wait_for_event(conn)) && moving) {
4535 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4536 case XCB_BUTTON_RELEASE:
4537 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4540 case XCB_MOTION_NOTIFY:
4541 mne = (xcb_motion_notify_event_t *)evt;
4542 X(win) = mne->root_x - qpr->win_x - border_width;
4543 Y(win) = mne->root_y - qpr->win_y - border_width;
4545 constrain_window(win, r, 0);
4547 /* not free, don't sync more than 120 times / second */
4548 if ((mne->time - timestamp) > (1000 / 120) ) {
4549 timestamp = mne->time;
4564 store_float_geom(win, r);
4566 xcb_free_cursor(conn, cursor);
4567 xcb_close_font(conn, cursor_font);
4568 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4569 DNPRINTF(SWM_D_EVENT, "move: done\n");
4573 move_step(struct swm_region *r, union arg *args)
4575 struct ws_win *win = NULL;
4577 if (r && r->ws && r->ws->focus)
4582 if (!(win->transient != 0 || win->floating != 0))
4589 /* user/key callable function IDs */
4639 KF_SEARCH_WORKSPACE,
4680 KF_DUMPWINS, /* MUST BE LAST */
4684 /* key definitions */
4686 char name[SWM_FUNCNAME_LEN];
4687 void (*func)(struct swm_region *r, union arg *);
4689 } keyfuncs[KF_INVALID + 1] = {
4690 /* name function argument */
4691 { "bar_toggle", bar_toggle, {0} },
4692 { "button2", pressbutton, {2} },
4693 { "cycle_layout", cycle_layout, {0} },
4694 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4695 { "float_toggle", floating_toggle,{0} },
4696 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4697 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4698 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4699 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4700 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4701 { "iconify", iconify, {0} },
4702 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4703 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4704 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4705 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4706 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4707 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4708 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4709 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4710 { "mvws_1", send_to_ws, {.id = 0} },
4711 { "mvws_2", send_to_ws, {.id = 1} },
4712 { "mvws_3", send_to_ws, {.id = 2} },
4713 { "mvws_4", send_to_ws, {.id = 3} },
4714 { "mvws_5", send_to_ws, {.id = 4} },
4715 { "mvws_6", send_to_ws, {.id = 5} },
4716 { "mvws_7", send_to_ws, {.id = 6} },
4717 { "mvws_8", send_to_ws, {.id = 7} },
4718 { "mvws_9", send_to_ws, {.id = 8} },
4719 { "mvws_10", send_to_ws, {.id = 9} },
4720 { "mvws_11", send_to_ws, {.id = 10} },
4721 { "mvws_12", send_to_ws, {.id = 11} },
4722 { "mvws_13", send_to_ws, {.id = 12} },
4723 { "mvws_14", send_to_ws, {.id = 13} },
4724 { "mvws_15", send_to_ws, {.id = 14} },
4725 { "mvws_16", send_to_ws, {.id = 15} },
4726 { "mvws_17", send_to_ws, {.id = 16} },
4727 { "mvws_18", send_to_ws, {.id = 17} },
4728 { "mvws_19", send_to_ws, {.id = 18} },
4729 { "mvws_20", send_to_ws, {.id = 19} },
4730 { "mvws_21", send_to_ws, {.id = 20} },
4731 { "mvws_22", send_to_ws, {.id = 21} },
4732 { "name_workspace", name_workspace, {0} },
4733 { "quit", quit, {0} },
4734 { "raise_toggle", raise_toggle, {0} },
4735 { "restart", restart, {0} },
4736 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4737 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4738 { "search_win", search_win, {0} },
4739 { "search_workspace", search_workspace, {0} },
4740 { "spawn_custom", NULL, {0} },
4741 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4742 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4743 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4744 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4745 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4746 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4747 { "uniconify", uniconify, {0} },
4748 { "version", version, {0} },
4749 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4750 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4751 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4752 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4753 { "ws_1", switchws, {.id = 0} },
4754 { "ws_2", switchws, {.id = 1} },
4755 { "ws_3", switchws, {.id = 2} },
4756 { "ws_4", switchws, {.id = 3} },
4757 { "ws_5", switchws, {.id = 4} },
4758 { "ws_6", switchws, {.id = 5} },
4759 { "ws_7", switchws, {.id = 6} },
4760 { "ws_8", switchws, {.id = 7} },
4761 { "ws_9", switchws, {.id = 8} },
4762 { "ws_10", switchws, {.id = 9} },
4763 { "ws_11", switchws, {.id = 10} },
4764 { "ws_12", switchws, {.id = 11} },
4765 { "ws_13", switchws, {.id = 12} },
4766 { "ws_14", switchws, {.id = 13} },
4767 { "ws_15", switchws, {.id = 14} },
4768 { "ws_16", switchws, {.id = 15} },
4769 { "ws_17", switchws, {.id = 16} },
4770 { "ws_18", switchws, {.id = 17} },
4771 { "ws_19", switchws, {.id = 18} },
4772 { "ws_20", switchws, {.id = 19} },
4773 { "ws_21", switchws, {.id = 20} },
4774 { "ws_22", switchws, {.id = 21} },
4775 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4776 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4777 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4778 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4779 { "ws_prior", priorws, {0} },
4780 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4781 { "invalid key func", NULL, {0} },
4784 RB_ENTRY(key) entry;
4787 enum keyfuncid funcid;
4790 RB_HEAD(key_tree, key);
4793 key_cmp(struct key *kp1, struct key *kp2)
4795 if (kp1->keysym < kp2->keysym)
4797 if (kp1->keysym > kp2->keysym)
4800 if (kp1->mod < kp2->mod)
4802 if (kp1->mod > kp2->mod)
4808 RB_GENERATE(key_tree, key, entry, key_cmp);
4809 struct key_tree keys;
4812 enum { client_click, root_click };
4814 unsigned int action;
4816 unsigned int button;
4817 void (*func)(struct ws_win *, union arg *);
4820 /* action key mouse button func args */
4821 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4822 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4823 { client_click, MODKEY, Button1, move, {0} },
4827 update_modkey(unsigned int mod)
4833 RB_FOREACH(kp, key_tree, &keys)
4834 if (kp->mod & ShiftMask)
4835 kp->mod = mod | ShiftMask;
4839 for (i = 0; i < LENGTH(buttons); i++)
4840 if (buttons[i].mask & ShiftMask)
4841 buttons[i].mask = mod | ShiftMask;
4843 buttons[i].mask = mod;
4848 TAILQ_ENTRY(spawn_prog) entry;
4853 TAILQ_HEAD(spawn_list, spawn_prog);
4854 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4857 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4860 struct spawn_prog *prog = NULL;
4862 char *ap, **real_args;
4864 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4867 TAILQ_FOREACH(prog, &spawns, entry) {
4868 if (!strcasecmp(spawn_name, prog->name))
4872 warnx("spawn_custom: program %s not found", spawn_name);
4876 /* make room for expanded args */
4877 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4878 err(1, "spawn_custom: calloc real_args");
4880 /* expand spawn_args into real_args */
4881 for (i = 0; i < prog->argc; i++) {
4883 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4884 if (!strcasecmp(ap, "$bar_border")) {
4886 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4888 err(1, "spawn_custom border color");
4889 } else if (!strcasecmp(ap, "$bar_color")) {
4891 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4893 err(1, "spawn_custom bar color");
4894 } else if (!strcasecmp(ap, "$bar_font")) {
4895 if ((real_args[i] = strdup(bar_fonts))
4897 err(1, "spawn_custom bar fonts");
4898 } else if (!strcasecmp(ap, "$bar_font_color")) {
4900 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4902 err(1, "spawn_custom color font");
4903 } else if (!strcasecmp(ap, "$color_focus")) {
4905 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4907 err(1, "spawn_custom color focus");
4908 } else if (!strcasecmp(ap, "$color_unfocus")) {
4910 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4912 err(1, "spawn_custom color unfocus");
4914 /* no match --> copy as is */
4915 if ((real_args[i] = strdup(ap)) == NULL)
4916 err(1, "spawn_custom strdup(ap)");
4918 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4923 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4924 for (i = 0; i < prog->argc; i++)
4925 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4926 DNPRINTF(SWM_D_SPAWN, "\n");
4928 *ret_args = real_args;
4929 return (prog->argc);
4933 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4939 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4943 spawn(r->ws->idx, &a, 1);
4945 for (i = 0; i < spawn_argc; i++)
4951 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4957 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4961 if (pipe(select_list_pipe) == -1)
4962 err(1, "pipe error");
4963 if (pipe(select_resp_pipe) == -1)
4964 err(1, "pipe error");
4966 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4967 err(1, "could not disable SIGPIPE");
4968 switch (*pid = fork()) {
4970 err(1, "cannot fork");
4973 if (dup2(select_list_pipe[0], 0) == -1)
4975 if (dup2(select_resp_pipe[1], 1) == -1)
4977 close(select_list_pipe[1]);
4978 close(select_resp_pipe[0]);
4979 spawn(r->ws->idx, &a, 0);
4981 default: /* parent */
4982 close(select_list_pipe[0]);
4983 close(select_resp_pipe[1]);
4987 for (i = 0; i < spawn_argc; i++)
4993 spawn_insert(char *name, char *args)
4995 char *arg, *cp, *ptr;
4996 struct spawn_prog *sp;
4998 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5000 if ((sp = calloc(1, sizeof *sp)) == NULL)
5001 err(1, "spawn_insert: malloc");
5002 if ((sp->name = strdup(name)) == NULL)
5003 err(1, "spawn_insert: strdup");
5005 /* convert the arguments to an argument list */
5006 if ((ptr = cp = strdup(args)) == NULL)
5007 err(1, "spawn_insert: strdup");
5008 while ((arg = strsep(&ptr, " \t")) != NULL) {
5009 /* empty field; skip it */
5014 if ((sp->argv = realloc(sp->argv, sp->argc *
5015 sizeof *sp->argv)) == NULL)
5016 err(1, "spawn_insert: realloc");
5017 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5018 err(1, "spawn_insert: strdup");
5022 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5023 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5027 spawn_remove(struct spawn_prog *sp)
5031 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5033 TAILQ_REMOVE(&spawns, sp, entry);
5034 for (i = 0; i < sp->argc; i++)
5040 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5044 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5046 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5049 spawn_insert(name, args);
5051 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5055 setspawn(char *name, char *args)
5057 struct spawn_prog *sp;
5059 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5064 TAILQ_FOREACH(sp, &spawns, entry) {
5065 if (!strcmp(sp->name, name)) {
5069 spawn_replace(sp, name, args);
5070 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5074 if (*args == '\0') {
5075 warnx("error: setspawn: cannot find program: %s", name);
5079 spawn_insert(name, args);
5080 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5084 setconfspawn(char *selector, char *value, int flags)
5086 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5088 setspawn(selector, value);
5090 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5097 setconfspawn("term", "xterm", 0);
5098 setconfspawn("spawn_term", "xterm", 0);
5099 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5100 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5101 setconfspawn("lock", "xlock", 0);
5102 setconfspawn("initscr", "initscreen.sh", 0);
5103 setconfspawn("menu", "dmenu_run"
5106 " -nf $bar_font_color"
5108 " -sf $bar_color", 0);
5109 setconfspawn("search", "dmenu"
5113 " -nf $bar_font_color"
5115 " -sf $bar_color", 0);
5116 setconfspawn("name_workspace", "dmenu"
5120 " -nf $bar_font_color"
5122 " -sf $bar_color", 0);
5126 #define SWM_MODNAME_SIZE 32
5127 #define SWM_KEY_WS "\n+ \t"
5129 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5133 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5134 if (mod == NULL || ks == NULL) {
5135 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5138 if (keystr == NULL || strlen(keystr) == 0) {
5139 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5143 *ks = XCB_NO_SYMBOL;
5145 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5146 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5148 cp += (long)strspn(cp, SWM_KEY_WS);
5149 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5151 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5153 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5155 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5157 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5159 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5161 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5162 *mod |= ControlMask;
5164 *ks = XStringToKeysym(name);
5165 XConvertCase(*ks, ks, &uks);
5166 if (ks == XCB_NO_SYMBOL) {
5168 "parsekeys: invalid key %s\n",
5174 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5179 strdupsafe(char *str)
5184 return (strdup(str));
5188 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5192 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5193 keyfuncs[kfid].name, spawn_name);
5195 if ((kp = malloc(sizeof *kp)) == NULL)
5196 err(1, "key_insert: malloc");
5201 kp->spawn_name = strdupsafe(spawn_name);
5202 RB_INSERT(key_tree, &keys, kp);
5204 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5208 key_lookup(unsigned int mod, KeySym ks)
5215 return (RB_FIND(key_tree, &keys, &kp));
5219 key_remove(struct key *kp)
5221 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5223 RB_REMOVE(key_tree, &keys, kp);
5224 free(kp->spawn_name);
5227 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5231 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5234 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5238 key_insert(mod, ks, kfid, spawn_name);
5240 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5244 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5249 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5250 keyfuncs[kfid].name, spawn_name);
5252 if ((kp = key_lookup(mod, ks)) != NULL) {
5253 if (kfid == KF_INVALID)
5256 key_replace(kp, mod, ks, kfid, spawn_name);
5257 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5260 if (kfid == KF_INVALID) {
5261 warnx("error: setkeybinding: cannot find mod/key combination");
5262 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5266 key_insert(mod, ks, kfid, spawn_name);
5267 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5271 setconfbinding(char *selector, char *value, int flags)
5273 enum keyfuncid kfid;
5276 struct spawn_prog *sp;
5277 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5278 if (selector == NULL) {
5279 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5280 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5282 setkeybinding(mod, ks, kfid, NULL);
5287 /* search by key function name */
5288 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5289 if (strncasecmp(selector, keyfuncs[kfid].name,
5290 SWM_FUNCNAME_LEN) == 0) {
5291 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5293 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5294 setkeybinding(mod, ks, kfid, NULL);
5300 /* search by custom spawn name */
5301 TAILQ_FOREACH(sp, &spawns, entry) {
5302 if (strcasecmp(selector, sp->name) == 0) {
5303 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5305 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5306 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5313 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5320 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5321 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5322 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5323 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5324 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5325 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5326 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5327 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5328 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5329 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5330 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5331 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5332 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5333 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5334 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5335 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5336 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5337 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5338 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5339 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5340 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5341 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5342 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5343 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5344 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5345 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5346 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5347 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5348 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5349 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5350 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5351 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5352 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5353 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5354 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5355 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5356 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5357 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5358 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5359 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5360 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5361 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5362 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5363 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5364 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5365 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5366 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5367 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5368 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5369 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5370 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5371 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5372 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5374 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5375 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5376 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5377 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5380 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5381 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5382 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5383 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5384 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5385 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5386 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5387 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5388 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5389 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5390 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5391 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5392 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5393 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5394 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5395 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5396 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5397 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5398 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5399 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5400 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5401 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5402 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5403 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5404 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5405 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5406 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5407 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5408 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5409 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5410 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5411 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5412 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5414 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5415 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5426 while (RB_EMPTY(&keys) == 0) {
5427 kp = RB_ROOT(&keys);
5433 setkeymapping(char *selector, char *value, int flags)
5435 char keymapping_file[PATH_MAX];
5436 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5437 if (value[0] == '~')
5438 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5439 pwd->pw_dir, &value[1]);
5441 strlcpy(keymapping_file, value, sizeof keymapping_file);
5443 /* load new key bindings; if it fails, revert to default bindings */
5444 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5448 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5453 updatenumlockmask(void)
5456 xcb_get_modifier_mapping_reply_t *modmap_r;
5457 xcb_keycode_t *modmap, kc;
5459 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5462 modmap_r = xcb_get_modifier_mapping_reply(conn,
5463 xcb_get_modifier_mapping(conn),
5466 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5467 for (i = 0; i < 8; i++) {
5468 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5469 kc = modmap[i * modmap_r->keycodes_per_modifier
5472 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5474 numlockmask = (1 << i);
5486 xcb_keycode_t *code;
5487 unsigned int modifiers[] =
5488 { 0, LockMask, numlockmask, numlockmask | LockMask };
5491 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5492 updatenumlockmask();
5494 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5495 for (k = 0; k < num_screens; k++) {
5496 if (TAILQ_EMPTY(&screens[k].rl))
5498 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5500 RB_FOREACH(kp, key_tree, &keys) {
5501 if ((code = xcb_key_symbols_get_keycode(syms,
5503 for (j = 0; j < LENGTH(modifiers); j++)
5504 xcb_grab_key(conn, True,
5506 kp->mod | modifiers[j],
5507 *code, XCB_GRAB_MODE_ASYNC,
5508 XCB_GRAB_MODE_ASYNC);
5514 grabbuttons(struct ws_win *win, int focused)
5517 unsigned int modifiers[] =
5518 { 0, LockMask, numlockmask, numlockmask|LockMask };
5520 updatenumlockmask();
5521 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5522 XCB_BUTTON_MASK_ANY);
5524 for (i = 0; i < LENGTH(buttons); i++)
5525 if (buttons[i].action == client_click)
5526 for (j = 0; j < LENGTH(modifiers); j++)
5527 xcb_grab_button(conn, False, win->id,
5529 XCB_GRAB_MODE_ASYNC,
5536 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5537 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5538 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5541 const char *quirkname[] = {
5542 "NONE", /* config string for "no value" */
5551 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5552 #define SWM_Q_WS "\n|+ \t"
5554 parsequirks(char *qstr, unsigned long *quirk)
5564 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5566 cp += (long)strspn(cp, SWM_Q_WS);
5567 for (i = 0; i < LENGTH(quirkname); i++) {
5568 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5569 DNPRINTF(SWM_D_QUIRK,
5570 "parsequirks: %s\n", name);
5575 *quirk |= 1 << (i-1);
5579 if (i >= LENGTH(quirkname)) {
5580 DNPRINTF(SWM_D_QUIRK,
5581 "parsequirks: invalid quirk [%s]\n", name);
5589 quirk_insert(const char *class, const char *name, unsigned long quirk)
5593 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5596 if ((qp = malloc(sizeof *qp)) == NULL)
5597 err(1, "quirk_insert: malloc");
5598 if ((qp->class = strdup(class)) == NULL)
5599 err(1, "quirk_insert: strdup");
5600 if ((qp->name = strdup(name)) == NULL)
5601 err(1, "quirk_insert: strdup");
5604 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5606 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5610 quirk_remove(struct quirk *qp)
5612 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5613 qp->name, qp->quirk);
5615 TAILQ_REMOVE(&quirks, qp, entry);
5620 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5624 quirk_replace(struct quirk *qp, const char *class, const char *name,
5625 unsigned long quirk)
5627 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5628 qp->name, qp->quirk);
5631 quirk_insert(class, name, quirk);
5633 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5637 setquirk(const char *class, const char *name, unsigned long quirk)
5641 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5644 TAILQ_FOREACH(qp, &quirks, entry) {
5645 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5649 quirk_replace(qp, class, name, quirk);
5650 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5655 warnx("error: setquirk: cannot find class/name combination");
5659 quirk_insert(class, name, quirk);
5660 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5664 setconfquirk(char *selector, char *value, int flags)
5666 char *cp, *class, *name;
5668 unsigned long quirks;
5669 if (selector == NULL)
5671 if ((cp = strchr(selector, ':')) == NULL)
5676 if ((retval = parsequirks(value, &quirks)) == 0)
5677 setquirk(class, name, quirks);
5684 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5685 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5686 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5687 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5688 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5689 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5690 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5691 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5692 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5693 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5694 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5695 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5696 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5699 /* conf file stuff */
5700 #define SWM_CONF_FILE "spectrwm.conf"
5701 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5705 SWM_S_BAR_AT_BOTTOM,
5706 SWM_S_BAR_BORDER_WIDTH,
5713 SWM_S_CLOCK_ENABLED,
5716 SWM_S_CYCLE_VISIBLE,
5718 SWM_S_DISABLE_BORDER,
5720 SWM_S_FOCUS_CLOSE_WRAP,
5721 SWM_S_FOCUS_DEFAULT,
5727 SWM_S_STACK_ENABLED,
5729 SWM_S_TITLE_CLASS_ENABLED,
5730 SWM_S_TITLE_NAME_ENABLED,
5731 SWM_S_URGENT_ENABLED,
5732 SWM_S_VERBOSE_LAYOUT,
5733 SWM_S_WINDOW_NAME_ENABLED,
5734 SWM_S_WORKSPACE_LIMIT
5738 setconfvalue(char *selector, char *value, int flags)
5744 case SWM_S_BAR_ACTION:
5746 if ((bar_argv[0] = strdup(value)) == NULL)
5747 err(1, "setconfvalue: bar_action");
5749 case SWM_S_BAR_AT_BOTTOM:
5750 bar_at_bottom = atoi(value);
5752 case SWM_S_BAR_BORDER_WIDTH:
5753 bar_border_width = atoi(value);
5754 if (bar_border_width < 0)
5755 bar_border_width = 0;
5757 case SWM_S_BAR_DELAY:
5758 bar_delay = atoi(value);
5760 case SWM_S_BAR_ENABLED:
5761 bar_enabled = atoi(value);
5763 case SWM_S_BAR_FONT:
5765 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5766 err(1, "setconfvalue: asprintf: failed to allocate "
5767 "memory for bar_fonts.");
5771 case SWM_S_BAR_FORMAT:
5773 if ((bar_format = strdup(value)) == NULL)
5774 err(1, "setconfvalue: bar_format");
5776 case SWM_S_BAR_JUSTIFY:
5777 if (!strcmp(value, "left"))
5778 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5779 else if (!strcmp(value, "center"))
5780 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5781 else if (!strcmp(value, "right"))
5782 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5784 errx(1, "invalid bar_justify");
5786 case SWM_S_BORDER_WIDTH:
5787 border_width = atoi(value);
5788 if (border_width < 0)
5791 case SWM_S_CLOCK_ENABLED:
5792 clock_enabled = atoi(value);
5794 case SWM_S_CLOCK_FORMAT:
5795 #ifndef SWM_DENY_CLOCK_FORMAT
5797 if ((clock_format = strdup(value)) == NULL)
5798 err(1, "setconfvalue: clock_format");
5801 case SWM_S_CYCLE_EMPTY:
5802 cycle_empty = atoi(value);
5804 case SWM_S_CYCLE_VISIBLE:
5805 cycle_visible = atoi(value);
5807 case SWM_S_DIALOG_RATIO:
5808 dialog_ratio = atof(value);
5809 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5812 case SWM_S_DISABLE_BORDER:
5813 disable_border = atoi(value);
5815 case SWM_S_FOCUS_CLOSE:
5816 if (!strcmp(value, "first"))
5817 focus_close = SWM_STACK_BOTTOM;
5818 else if (!strcmp(value, "last"))
5819 focus_close = SWM_STACK_TOP;
5820 else if (!strcmp(value, "next"))
5821 focus_close = SWM_STACK_ABOVE;
5822 else if (!strcmp(value, "previous"))
5823 focus_close = SWM_STACK_BELOW;
5825 errx(1, "focus_close");
5827 case SWM_S_FOCUS_CLOSE_WRAP:
5828 focus_close_wrap = atoi(value);
5830 case SWM_S_FOCUS_DEFAULT:
5831 if (!strcmp(value, "last"))
5832 focus_default = SWM_STACK_TOP;
5833 else if (!strcmp(value, "first"))
5834 focus_default = SWM_STACK_BOTTOM;
5836 errx(1, "focus_default");
5838 case SWM_S_FOCUS_MODE:
5839 if (!strcmp(value, "default"))
5840 focus_mode = SWM_FOCUS_DEFAULT;
5841 else if (!strcmp(value, "follow_cursor"))
5842 focus_mode = SWM_FOCUS_FOLLOW;
5843 else if (!strcmp(value, "synergy"))
5844 focus_mode = SWM_FOCUS_SYNERGY;
5846 errx(1, "focus_mode");
5848 case SWM_S_SPAWN_ORDER:
5849 if (!strcmp(value, "first"))
5850 spawn_position = SWM_STACK_BOTTOM;
5851 else if (!strcmp(value, "last"))
5852 spawn_position = SWM_STACK_TOP;
5853 else if (!strcmp(value, "next"))
5854 spawn_position = SWM_STACK_ABOVE;
5855 else if (!strcmp(value, "previous"))
5856 spawn_position = SWM_STACK_BELOW;
5858 errx(1, "spawn_position");
5860 case SWM_S_SPAWN_TERM:
5861 setconfspawn("term", value, 0);
5862 setconfspawn("spawn_term", value, 0);
5866 case SWM_S_SS_ENABLED:
5867 ss_enabled = atoi(value);
5869 case SWM_S_STACK_ENABLED:
5870 stack_enabled = atoi(value);
5872 case SWM_S_TERM_WIDTH:
5873 term_width = atoi(value);
5877 case SWM_S_TITLE_CLASS_ENABLED:
5878 title_class_enabled = atoi(value);
5880 case SWM_S_TITLE_NAME_ENABLED:
5881 title_name_enabled = atoi(value);
5883 case SWM_S_URGENT_ENABLED:
5884 urgent_enabled = atoi(value);
5886 case SWM_S_VERBOSE_LAYOUT:
5887 verbose_layout = atoi(value);
5888 for (i = 0; layouts[i].l_stack != NULL; i++) {
5890 layouts[i].l_string = fancy_stacker;
5892 layouts[i].l_string = plain_stacker;
5895 case SWM_S_WINDOW_NAME_ENABLED:
5896 window_name_enabled = atoi(value);
5898 case SWM_S_WORKSPACE_LIMIT:
5899 workspace_limit = atoi(value);
5900 if (workspace_limit > SWM_WS_MAX)
5901 workspace_limit = SWM_WS_MAX;
5902 else if (workspace_limit < 1)
5903 workspace_limit = 1;
5912 setconfmodkey(char *selector, char *value, int flags)
5914 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5915 update_modkey(Mod1Mask);
5916 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5917 update_modkey(Mod2Mask);
5918 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5919 update_modkey(Mod3Mask);
5920 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5921 update_modkey(Mod4Mask);
5928 setconfcolor(char *selector, char *value, int flags)
5930 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5935 setconfregion(char *selector, char *value, int flags)
5937 custom_region(value);
5942 setautorun(char *selector, char *value, int flags)
5952 if (getenv("SWM_STARTED"))
5956 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5957 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5959 if (ws_id < 0 || ws_id >= workspace_limit)
5960 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5963 * This is a little intricate
5965 * If the pid already exists we simply reuse it because it means it was
5966 * used before AND not claimed by manage_window. We get away with
5967 * altering it in the parent after INSERT because this can not be a race
5970 while ((ap = strsep(&sp, " \t")) != NULL) {
5973 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5975 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5976 err(1, "setautorun: realloc");
5977 a.argv[argc - 1] = ap;
5980 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5981 err(1, "setautorun: realloc");
5982 a.argv[argc] = NULL;
5984 if ((pid = fork()) == 0) {
5985 spawn(ws_id, &a, 1);
5994 p = calloc(1, sizeof *p);
5997 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6007 setlayout(char *selector, char *value, int flags)
6009 int ws_id, i, x, mg, ma, si, raise, f = 0;
6010 int st = SWM_V_STACK, num_screens;
6012 struct workspace *ws;
6014 if (getenv("SWM_STARTED"))
6018 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6019 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6020 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6021 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6024 if (ws_id < 0 || ws_id >= workspace_limit)
6025 errx(1, "layout: invalid workspace %d", ws_id + 1);
6027 if (!strcasecmp(s, "vertical"))
6029 else if (!strcasecmp(s, "vertical_flip")) {
6032 } else if (!strcasecmp(s, "horizontal"))
6034 else if (!strcasecmp(s, "horizontal_flip")) {
6037 } else if (!strcasecmp(s, "fullscreen"))
6040 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6041 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6044 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6045 for (i = 0; i < num_screens; i++) {
6046 ws = (struct workspace *)&screens[i].ws;
6047 ws[ws_id].cur_layout = &layouts[st];
6049 ws[ws_id].always_raise = raise;
6050 if (st == SWM_MAX_STACK)
6054 for (x = 0; x < abs(mg); x++) {
6055 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6056 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6057 SWM_ARG_ID_MASTERSHRINK);
6061 for (x = 0; x < abs(ma); x++) {
6062 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6063 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6064 SWM_ARG_ID_MASTERDEL);
6068 for (x = 0; x < abs(si); x++) {
6069 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6070 si >= 0 ? SWM_ARG_ID_STACKINC :
6071 SWM_ARG_ID_STACKDEC);
6076 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6077 SWM_ARG_ID_FLIPLAYOUT);
6085 /* config options */
6086 struct config_option {
6088 int (*func)(char*, char*, int);
6091 struct config_option configopt[] = {
6092 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6093 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6094 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6095 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6096 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6097 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6098 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6099 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6100 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6101 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6102 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6103 { "keyboard_mapping", setkeymapping, 0 },
6104 { "bind", setconfbinding, 0 },
6105 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6106 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6107 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6108 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6109 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6110 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6111 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6112 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6113 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6114 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6115 { "modkey", setconfmodkey, 0 },
6116 { "program", setconfspawn, 0 },
6117 { "quirk", setconfquirk, 0 },
6118 { "region", setconfregion, 0 },
6119 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6120 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6121 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6122 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6123 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6124 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6125 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6126 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6127 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6128 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6129 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6130 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6131 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6132 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6133 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6134 { "autorun", setautorun, 0 },
6135 { "layout", setlayout, 0 },
6140 conf_load(char *filename, int keymapping)
6143 char *line, *cp, *optsub, *optval;
6144 size_t linelen, lineno = 0;
6145 int wordlen, i, optind;
6146 struct config_option *opt;
6148 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6150 if (filename == NULL) {
6151 warnx("conf_load: no filename");
6154 if ((config = fopen(filename, "r")) == NULL) {
6155 warn("conf_load: fopen: %s", filename);
6159 while (!feof(config)) {
6160 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6163 err(1, "%s", filename);
6168 cp += strspn(cp, " \t\n"); /* eat whitespace */
6169 if (cp[0] == '\0') {
6174 /* get config option */
6175 wordlen = strcspn(cp, "=[ \t\n");
6177 warnx("%s: line %zd: no option found",
6182 for (i = 0; i < LENGTH(configopt); i++) {
6183 opt = &configopt[i];
6184 if (!strncasecmp(cp, opt->optname, wordlen) &&
6185 strlen(opt->optname) == wordlen) {
6191 warnx("%s: line %zd: unknown option %.*s",
6192 filename, lineno, wordlen, cp);
6195 if (keymapping && strcmp(opt->optname, "bind")) {
6196 warnx("%s: line %zd: invalid option %.*s",
6197 filename, lineno, wordlen, cp);
6201 cp += strspn(cp, " \t\n"); /* eat whitespace */
6202 /* get [selector] if any */
6206 wordlen = strcspn(cp, "]");
6209 warnx("%s: line %zd: syntax error",
6214 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6216 warnx("%s: line %zd: unable to allocate"
6217 "memory for selector", filename,
6223 cp += strspn(cp, "] \t\n"); /* eat trailing */
6225 cp += strspn(cp, "= \t\n"); /* eat trailing */
6227 optval = strdup(cp);
6228 /* call function to deal with it all */
6229 if (configopt[optind].func(optsub, optval,
6230 configopt[optind].funcflags) != 0)
6231 errx(1, "%s: line %zd: invalid data for %s",
6232 filename, lineno, configopt[optind].optname);
6239 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6246 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6252 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6254 struct ws_win *parent, *w;
6255 struct swm_region *r;
6256 struct workspace *ws;
6257 xcb_icccm_wm_hints_t wmh;
6259 parent = find_window(win->transient);
6261 parent->child_trans = win;
6263 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6264 " for 0x%x trans 0x%x\n", win->id, win->transient);
6266 r = root_to_region(win->wa->root);
6268 /* parent doen't exist in our window list */
6269 TAILQ_FOREACH(w, &ws->winlist, entry) {
6270 if (xcb_icccm_get_wm_hints_reply(conn,
6271 xcb_icccm_get_wm_hints(conn, w->id),
6273 warnx("can't get hints for 0x%x", w->id);
6277 if (win->hints.window_group != wmh.window_group)
6280 w->child_trans = win;
6281 win->transient = w->id;
6283 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6284 "transient to 0x%x\n", win->transient);
6291 window_get_pid(xcb_window_t win)
6296 xcb_get_property_cookie_t pc;
6297 xcb_get_property_reply_t *pr;
6299 apid = get_atom_from_string("_NET_WM_PID");
6300 if (apid == XCB_ATOM_NONE)
6303 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6304 pr = xcb_get_property_reply(conn, pc, NULL);
6307 if (pr->type != XCB_ATOM_CARDINAL) {
6312 ret = *((pid_t *)xcb_get_property_value(pr));
6318 apid = get_atom_from_string("_SWM_PID");
6319 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6321 pr = xcb_get_property_reply(conn, pc, NULL);
6324 if (pr->type != XCB_ATOM_STRING) {
6329 ret = strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6336 manage_window(xcb_window_t id)
6338 xcb_window_t trans = XCB_WINDOW_NONE;
6339 struct workspace *ws;
6340 struct ws_win *win, *ww;
6341 int i, ws_idx, border_me = 0;
6342 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6343 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6345 struct swm_region *r;
6349 uint32_t event_mask;
6351 xcb_get_property_reply_t *gpr;
6352 xcb_icccm_get_wm_protocols_reply_t wpr;
6354 if ((win = find_window(id)) != NULL)
6355 return (win); /* already being managed */
6357 /* see if we are on the unmanaged list */
6358 if ((win = find_unmanaged_window(id)) != NULL) {
6359 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6360 "window: 0x%x\n", win->id);
6361 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6363 set_child_transient(win, &trans);
6365 if (trans && (ww = find_window(trans)))
6366 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6367 else if ((ww = win->ws->focus) &&
6368 spawn_position == SWM_STACK_ABOVE)
6369 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6371 else if (ww && spawn_position == SWM_STACK_BELOW)
6372 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6373 else switch (spawn_position) {
6376 case SWM_STACK_ABOVE:
6377 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6379 case SWM_STACK_BOTTOM:
6380 case SWM_STACK_BELOW:
6381 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6384 ewmh_update_actions(win);
6388 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6389 err(1, "manage_window: calloc: failed to allocate memory for "
6395 /* see if we need to override the workspace */
6396 p = find_pid(window_get_pid(id));
6398 /* Get all the window data in one shot */
6399 ws_idx_atom = get_atom_from_string("_SWM_WS");
6401 gpr = xcb_get_property_reply(conn,
6402 xcb_get_property(conn, False, id, ws_idx_atom,
6403 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6406 proplen = xcb_get_property_value_length(gpr);
6408 prop = malloc(proplen + 1);
6411 xcb_get_property_value(gpr),
6413 prop[proplen] = '\0';
6419 win->wa = xcb_get_geometry_reply(conn,
6420 xcb_get_geometry(conn, id),
6422 xcb_icccm_get_wm_normal_hints_reply(conn,
6423 xcb_icccm_get_wm_normal_hints(conn, id),
6425 xcb_icccm_get_wm_hints_reply(conn,
6426 xcb_icccm_get_wm_hints(conn, id),
6428 xcb_icccm_get_wm_transient_for_reply(conn,
6429 xcb_icccm_get_wm_transient_for(conn, id),
6432 win->transient = trans;
6433 set_child_transient(win, &trans);
6434 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6435 "transient: 0x%x\n", win->id, win->transient);
6438 prot = get_atom_from_string("WM_PROTOCOLS");
6440 /* get supported protocols */
6441 if (xcb_icccm_get_wm_protocols_reply(conn,
6442 xcb_icccm_get_wm_protocols(conn, id, prot),
6444 for (i = 0; i < wpr.atoms_len; i++) {
6445 if (wpr.atoms[i] == a_takefocus)
6446 win->take_focus = 1;
6447 if (wpr.atoms[i] == a_delete)
6448 win->can_delete = 1;
6450 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6453 win->iconic = get_iconic(win);
6456 * Figure out where to put the window. If it was previously assigned to
6457 * a workspace (either by spawn() or manually moving), and isn't
6458 * transient, * put it in the same workspace
6460 r = root_to_region(win->wa->root);
6462 ws = &r->s->ws[p->ws];
6463 TAILQ_REMOVE(&pidlist, p, entry);
6466 } else if (prop && win->transient == 0) {
6467 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6468 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6471 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6474 ws = &r->s->ws[ws_idx];
6477 /* this should launch transients in the same ws as parent */
6479 if ((ww = find_window(trans)) != NULL)
6485 warnx("manage_window: fix this "
6491 /* set up the window layout */
6494 win->s = r->s; /* this never changes */
6495 if (trans && (ww = find_window(trans)))
6496 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6497 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6498 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6500 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6501 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6502 else switch (spawn_position) {
6505 case SWM_STACK_ABOVE:
6506 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6508 case SWM_STACK_BOTTOM:
6509 case SWM_STACK_BELOW:
6510 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6513 /* ignore window border if there is one. */
6514 WIDTH(win) = win->wa->width;
6515 HEIGHT(win) = win->wa->height;
6516 X(win) = win->wa->x + win->wa->border_width;
6517 Y(win) = win->wa->y + win->wa->border_width;
6519 win->g_floatvalid = 0;
6520 win->floatmaxed = 0;
6521 win->ewmh_flags = 0;
6523 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6524 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6525 HEIGHT(win), ws->idx);
6527 constrain_window(win, r, 0);
6529 /* Set window properties so we can remember this after reincarnation */
6530 if (ws_idx_atom && prop == NULL &&
6531 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6533 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6535 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6536 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6542 ewmh_autoquirk(win);
6544 if (xcb_icccm_get_wm_class_reply(conn,
6545 xcb_icccm_get_wm_class(conn, win->id),
6547 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6548 win->ch.class_name, win->ch.instance_name);
6550 /* java is retarded so treat it special */
6551 if (strstr(win->ch.instance_name, "sun-awt")) {
6556 TAILQ_FOREACH(qp, &quirks, entry) {
6557 if (!strcmp(win->ch.class_name, qp->class) &&
6558 !strcmp(win->ch.instance_name, qp->name)) {
6559 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6560 "class: %s, name: %s\n", win->ch.class_name,
6561 win->ch.instance_name);
6562 if (qp->quirk & SWM_Q_FLOAT) {
6566 win->quirks = qp->quirk;
6571 /* alter window position if quirky */
6572 if (win->quirks & SWM_Q_ANYWHERE) {
6573 win->manual = 1; /* don't center the quirky windows */
6574 if (bar_enabled && Y(win) < bar_height)
6575 Y(win) = bar_height;
6576 if (WIDTH(win) + X(win) > WIDTH(r))
6577 X(win) = WIDTH(r) - WIDTH(win) - 2;
6581 /* Reset font sizes (the bruteforce way; no default keybinding). */
6582 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6583 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6584 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6585 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6586 fake_keypress(win, XK_KP_Add, ShiftMask);
6589 ewmh_get_win_state(win);
6590 ewmh_update_actions(win);
6591 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6596 X(win) -= border_width;
6597 Y(win) -= border_width;
6601 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6602 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6604 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6606 /* floaters need to be mapped if they are in the current workspace */
6607 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6608 map_window_raised(win->id);
6614 free_window(struct ws_win *win)
6616 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6621 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6626 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6631 memset(win, 0xff, sizeof *win); /* XXX kill later */
6634 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6638 unmanage_window(struct ws_win *win)
6640 struct ws_win *parent;
6641 xcb_screen_t *screen;
6646 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6648 if (win->transient) {
6649 parent = find_window(win->transient);
6651 parent->child_trans = NULL;
6654 /* focus on root just in case */
6655 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6656 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6657 screen->root, XCB_CURRENT_TIME);
6661 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6662 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6666 focus_magic(struct ws_win *win)
6668 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6671 /* if there are no windows clear the status-bar */
6676 if (win->child_trans) {
6677 /* win = parent & has a transient so focus on that */
6679 focus_win(win->child_trans);
6680 if (win->child_trans->take_focus)
6681 client_msg(win, a_takefocus);
6683 /* make sure transient hasn't disappeared */
6684 if (validate_win(win->child_trans) == 0) {
6685 focus_win(win->child_trans);
6686 if (win->child_trans->take_focus)
6687 client_msg(win->child_trans,
6690 win->child_trans = NULL;
6692 if (win->take_focus)
6693 client_msg(win, a_takefocus);
6699 if (win->take_focus)
6700 client_msg(win, a_takefocus);
6705 expose(xcb_expose_event_t *e)
6708 struct swm_region *r;
6710 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6712 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6713 for (i = 0; i < num_screens; i++)
6714 TAILQ_FOREACH(r, &screens[i].rl, entry)
6715 if (e->window == WINID(r->bar))
6722 keypress(xcb_key_press_event_t *e)
6724 xcb_keysym_t keysym;
6727 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6729 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6731 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6734 if (kp->funcid == KF_SPAWN_CUSTOM)
6735 spawn_custom(root_to_region(e->root),
6736 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6737 else if (keyfuncs[kp->funcid].func)
6738 keyfuncs[kp->funcid].func(root_to_region(e->root),
6739 &(keyfuncs[kp->funcid].args));
6743 buttonpress(xcb_button_press_event_t *e)
6748 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6749 e->event, e->detail);
6751 if ((win = find_window(e->event)) == NULL)
6755 action = client_click;
6757 for (i = 0; i < LENGTH(buttons); i++)
6758 if (action == buttons[i].action && buttons[i].func &&
6759 buttons[i].button == e->detail &&
6760 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6761 buttons[i].func(win, &buttons[i].args);
6767 configurerequest(xcb_configure_request_event_t *e)
6772 uint32_t wc[7] = {0};
6774 if ((win = find_window(e->window)) == NULL)
6775 if ((win = find_unmanaged_window(e->window)) == NULL)
6779 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6780 mask |= XCB_CONFIG_WINDOW_X;
6783 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6784 mask |= XCB_CONFIG_WINDOW_Y;
6787 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6788 mask |= XCB_CONFIG_WINDOW_WIDTH;
6791 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6792 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6793 wc[i++] = e->height;
6795 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6796 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6797 wc[i++] = e->border_width;
6799 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6800 mask |= XCB_CONFIG_WINDOW_SIBLING;
6801 wc[i++] = e->sibling;
6803 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6804 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6805 wc[i++] = e->stack_mode;
6808 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6809 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", e->window,
6810 YESNO(new), wc[0], wc[1], wc[2], wc[3]);
6812 xcb_configure_window(conn, e->window, mask, wc);
6813 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6814 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6815 win->g_float.x = e->x - X(win->ws->r);
6816 win->g_float.y = e->y - Y(win->ws->r);
6817 win->g_float.w = e->width;
6818 win->g_float.h = e->height;
6819 win->g_floatvalid = 1;
6821 if (win->floating) {
6822 win->g = win->g_float;
6823 win->g.x += X(win->ws->r);
6824 win->g.y += Y(win->ws->r);
6837 configurenotify(xcb_configure_notify_event_t *e)
6841 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6844 win = find_window(e->window);
6846 xcb_icccm_get_wm_normal_hints_reply(conn,
6847 xcb_icccm_get_wm_normal_hints(conn, win->id),
6856 destroynotify(xcb_destroy_notify_event_t *e)
6860 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6862 if ((win = find_window(e->window)) == NULL) {
6863 if ((win = find_unmanaged_window(e->window)) == NULL)
6869 /* make sure we focus on something */
6872 unmanage_window(win);
6878 enternotify(xcb_enter_notify_event_t *e)
6881 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6882 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6883 "state: %d\n", e->event, e->mode, e->detail, e->root,
6884 e->child, YESNO(e->same_screen_focus), e->state);
6886 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6887 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6892 switch (focus_mode) {
6893 case SWM_FOCUS_DEFAULT:
6895 case SWM_FOCUS_FOLLOW:
6897 case SWM_FOCUS_SYNERGY:
6901 if ((win = find_window(e->event)) == NULL) {
6902 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6911 /* lets us use one switch statement for arbitrary mode/detail combinations */
6912 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6915 mapnotify(xcb_map_notify_event_t *e)
6919 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
6921 win = manage_window(e->window);
6923 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
6926 * focus_win can only set input focus on a mapped window.
6927 * make sure the window really has focus since it is just being mapped.
6929 if (win->ws->focus == win)
6934 mappingnotify(xcb_mapping_notify_event_t *e)
6936 xcb_refresh_keyboard_mapping(syms, e);
6938 if (e->request == XCB_MAPPING_KEYBOARD)
6943 maprequest(xcb_map_request_event_t *e)
6946 struct swm_region *r;
6947 xcb_get_window_attributes_reply_t *war;
6949 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
6952 war = xcb_get_window_attributes_reply(conn,
6953 xcb_get_window_attributes(conn, e->window),
6957 if (war->override_redirect) {
6963 win = manage_window(e->window);
6965 return; /* can't happen */
6970 /* make new win focused */
6971 r = root_to_region(win->wa->root);
6972 if (win->ws == r->ws)
6977 propertynotify(xcb_property_notify_event_t *e)
6983 xcb_get_atom_name_reply_t *r;
6985 r = xcb_get_atom_name_reply(conn,
6986 xcb_get_atom_name(conn, e->atom),
6989 len = xcb_get_atom_name_name_length(r);
6991 name = malloc(len + 1);
6993 memcpy(name, xcb_get_atom_name_name(r), len);
6996 DNPRINTF(SWM_D_EVENT,
6997 "propertynotify: window: 0x%x, atom: %s\n",
7006 win = find_window(e->window);
7010 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7011 update_iconic(win, 0);
7012 map_window_raised(win->id);
7019 case XCB_ATOM_WM_CLASS:
7020 case XCB_ATOM_WM_NAME:
7029 unmapnotify(xcb_unmap_notify_event_t *e)
7033 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7035 /* determine if we need to help unmanage this window */
7036 win = find_window(e->window);
7040 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7041 unmanage_window(win);
7044 /* resend unmap because we ated it */
7045 xcb_unmap_window(conn, e->window);
7051 visibilitynotify(xcb_visibility_notify_event_t *e)
7053 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7058 clientmessage(xcb_client_message_event_t *e)
7062 win = find_window(e->window);
7067 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, type: %u\n",
7068 e->window, e->type);
7070 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7071 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7074 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7075 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7076 if (win->can_delete)
7077 client_msg(win, a_delete);
7079 xcb_kill_client(conn, win->id);
7081 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7082 DNPRINTF(SWM_D_EVENT,
7083 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7084 if (win->floating) {
7085 if (e->data.data32[0] & (1<<8)) /* x */
7086 X(win) = e->data.data32[1];
7087 if (e->data.data32[0] & (1<<9)) /* y */
7088 Y(win) = e->data.data32[2];
7089 if (e->data.data32[0] & (1<<10)) /* width */
7090 WIDTH(win) = e->data.data32[3];
7091 if (e->data.data32[0] & (1<<11)) /* height */
7092 HEIGHT(win) = e->data.data32[4];
7097 /* TODO: Change stack sizes */
7098 /* notify no change was made. */
7099 config_win(win, NULL);
7102 if (e->type == ewmh[_NET_WM_STATE].atom) {
7103 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7104 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7105 if (e->data.data32[2])
7106 ewmh_update_win_state(win, e->data.data32[2],
7119 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7122 /* this causes an error if some other window manager is running */
7123 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7124 for (i = 0; i < num_screens; i++) {
7126 xcb_change_window_attributes(conn, sc->root,
7127 XCB_CW_EVENT_MASK, &val);
7132 new_region(struct swm_screen *s, int x, int y, int w, int h)
7134 struct swm_region *r, *n;
7135 struct workspace *ws = NULL;
7138 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7139 s->idx, w, h, x, y);
7141 /* remove any conflicting regions */
7142 n = TAILQ_FIRST(&s->rl);
7145 n = TAILQ_NEXT(r, entry);
7146 if (X(r) < (x + w) &&
7147 (X(r) + WIDTH(r)) > x &&
7149 (Y(r) + HEIGHT(r)) > y) {
7150 if (r->ws->r != NULL)
7151 r->ws->old_r = r->ws->r;
7154 TAILQ_REMOVE(&s->rl, r, entry);
7155 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7159 /* search old regions for one to reuse */
7161 /* size + location match */
7162 TAILQ_FOREACH(r, &s->orl, entry)
7163 if (X(r) == x && Y(r) == y &&
7164 HEIGHT(r) == h && WIDTH(r) == w)
7168 TAILQ_FOREACH(r, &s->orl, entry)
7169 if (HEIGHT(r) == h && WIDTH(r) == w)
7173 TAILQ_REMOVE(&s->orl, r, entry);
7174 /* try to use old region's workspace */
7175 if (r->ws->r == NULL)
7178 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7179 err(1, "new_region: calloc: failed to allocate memory "
7182 /* if we don't have a workspace already, find one */
7184 for (i = 0; i < workspace_limit; i++)
7185 if (s->ws[i].r == NULL) {
7192 errx(1, "new_region: no free workspaces");
7203 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7209 #ifdef SWM_XRR_HAS_CRTC
7212 #endif /* SWM_XRR_HAS_CRTC */
7213 struct swm_region *r;
7215 xcb_randr_get_screen_resources_current_cookie_t src;
7216 xcb_randr_get_screen_resources_current_reply_t *srr;
7217 xcb_randr_get_crtc_info_cookie_t cic;
7218 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7219 xcb_randr_crtc_t *crtc;
7220 xcb_screen_t *screen = get_screen(i);
7222 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7223 if (i >= num_screens)
7224 errx(1, "scan_xrandr: invalid screen");
7226 /* remove any old regions */
7227 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7228 r->ws->old_r = r->ws->r = NULL;
7230 TAILQ_REMOVE(&screens[i].rl, r, entry);
7231 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7235 /* map virtual screens onto physical screens */
7236 #ifdef SWM_XRR_HAS_CRTC
7237 if (xrandr_support) {
7238 src = xcb_randr_get_screen_resources_current(conn,
7240 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7243 new_region(&screens[i], 0, 0,
7244 screen->width_in_pixels,
7245 screen->height_in_pixels);
7248 ncrtc = srr->num_crtcs;
7249 for (c = 0; c < ncrtc; c++) {
7250 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7251 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7253 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7256 if (cir->num_outputs == 0) {
7262 new_region(&screens[i], 0, 0,
7263 screen->width_in_pixels,
7264 screen->height_in_pixels);
7266 new_region(&screens[i],
7267 cir->x, cir->y, cir->width, cir->height);
7272 #endif /* SWM_XRR_HAS_CRTC */
7274 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7275 screen->height_in_pixels);
7280 screenchange(xcb_randr_screen_change_notify_event_t *e)
7282 struct swm_region *r;
7285 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7287 if (e->rotation & (XCB_RANDR_ROTATION_ROTATE_90
7288 | XCB_RANDR_ROTATION_ROTATE_270))
7289 xcb_randr_set_screen_size(conn, e->root, e->height,
7290 e->width, e->mheight, e->mwidth);
7292 xcb_randr_set_screen_size(conn, e->root, e->width,
7293 e->height, e->mwidth, e->mheight);
7295 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7296 /* silly event doesn't include the screen index */
7297 for (i = 0; i < num_screens; i++)
7298 if (screens[i].root == e->root)
7300 if (i >= num_screens)
7301 errx(1, "screenchange: screen not found");
7303 /* brute force for now, just re-enumerate the regions */
7306 /* add bars to all regions */
7307 for (i = 0; i < num_screens; i++)
7308 TAILQ_FOREACH(r, &screens[i].rl, entry)
7316 xcb_window_t *wins = NULL;
7318 int i, j, num_screens;
7319 uint16_t state, manage;
7321 xcb_query_tree_cookie_t qtc;
7322 xcb_query_tree_reply_t *qtr;
7323 xcb_get_window_attributes_cookie_t c;
7324 xcb_get_window_attributes_reply_t *r;
7325 xcb_get_property_cookie_t pc;
7327 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7328 for (i = 0; i < num_screens; i++) {
7329 qtc = xcb_query_tree(conn, screens[i].root);
7330 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7333 wins = xcb_query_tree_children(qtr);
7334 no = xcb_query_tree_children_length(qtr);
7335 /* attach windows to a region */
7336 /* normal windows */
7337 for (j = 0; j < no; j++) {
7338 c = xcb_get_window_attributes(conn, wins[j]);
7339 r = xcb_get_window_attributes_reply(conn, c, NULL);
7342 if (r->override_redirect) {
7347 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7348 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7354 state = getstate(wins[j]);
7355 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7356 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7357 manage_window(wins[j]);
7360 /* transient windows */
7361 for (j = 0; j < no; j++) {
7362 c = xcb_get_window_attributes(conn, wins[j]);
7363 r = xcb_get_window_attributes_reply(conn, c, NULL);
7366 if (r->override_redirect) {
7372 state = getstate(wins[j]);
7373 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7374 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7375 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7376 &wins[j], NULL) && manage)
7377 manage_window(wins[j]);
7386 int i, j, k, num_screens;
7387 struct workspace *ws;
7388 uint32_t gcv[1], wa[1];
7389 const xcb_query_extension_reply_t *qep;
7390 xcb_cursor_t cursor;
7391 xcb_font_t cursor_font;
7392 xcb_randr_query_version_cookie_t c;
7393 xcb_randr_query_version_reply_t *r;
7395 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7396 if ((screens = calloc(num_screens,
7397 sizeof(struct swm_screen))) == NULL)
7398 err(1, "setup_screens: calloc: failed to allocate memory for "
7401 /* initial Xrandr setup */
7402 xrandr_support = False;
7403 c = xcb_randr_query_version(conn, True, True);
7404 r = xcb_randr_query_version_reply(conn, c, NULL);
7406 if (r->major_version >= 1)
7407 xrandr_support = True;
7410 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7411 xrandr_eventbase = qep->first_event;
7413 cursor_font = xcb_generate_id(conn);
7414 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7416 cursor = xcb_generate_id(conn);
7417 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7418 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7421 /* map physical screens */
7422 for (i = 0; i < num_screens; i++) {
7423 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7425 TAILQ_INIT(&screens[i].rl);
7426 TAILQ_INIT(&screens[i].orl);
7427 screens[i].root = get_screen(i)->root;
7429 /* set default colors */
7430 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7431 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7432 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7433 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7434 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7436 /* create graphics context on screen */
7437 screens[i].bar_gc = xcb_generate_id(conn);
7439 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7440 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7442 /* set default cursor */
7443 xcb_change_window_attributes(conn, screens[i].root,
7446 /* init all workspaces */
7447 /* XXX these should be dynamically allocated too */
7448 for (j = 0; j < SWM_WS_MAX; j++) {
7449 ws = &screens[i].ws[j];
7455 TAILQ_INIT(&ws->winlist);
7456 TAILQ_INIT(&ws->unmanagedlist);
7458 for (k = 0; layouts[k].l_stack != NULL; k++)
7459 if (layouts[k].l_config != NULL)
7460 layouts[k].l_config(ws,
7461 SWM_ARG_ID_STACKINIT);
7462 ws->cur_layout = &layouts[0];
7463 ws->cur_layout->l_string(ws);
7469 xcb_randr_select_input(conn, screens[i].root,
7470 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7472 xcb_free_cursor(conn, cursor);
7473 xcb_close_font(conn, cursor_font);
7479 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7480 err(1, "setup_globals: strdup: failed to allocate memory.");
7482 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7483 err(1, "setup_globals: strdup: failed to allocate memory.");
7485 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7486 errx(1, "unable to allocate key symbols");
7488 a_state = get_atom_from_string("WM_STATE");
7489 a_prot = get_atom_from_string("WM_PROTOCOLS");
7490 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7491 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7492 a_wmname = get_atom_from_string("WM_NAME");
7493 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7494 a_utf8_string = get_atom_from_string("UTF8_STRING");
7495 a_string = get_atom_from_string("STRING");
7496 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7503 xcb_atom_t netwmcheck, netwmname, utf8_string;
7504 xcb_window_t root, win;
7507 /* work around sun jdk bugs, code from wmname */
7508 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7509 netwmname = get_atom_from_string("_NET_WM_NAME");
7510 utf8_string = get_atom_from_string("UTF8_STRING");
7512 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7513 for (i = 0; i < num_screens; i++) {
7514 root = screens[i].root;
7516 win = xcb_generate_id(conn);
7517 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7518 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7519 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7520 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7521 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7523 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7524 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7525 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7526 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7527 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7528 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7533 event_error(xcb_generic_error_t *e)
7535 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7536 "resource_id: %u, minor_code: %u\n",
7537 xcb_event_get_error_label(e->error_code), e->error_code,
7538 xcb_event_get_request_label(e->major_code), e->major_code,
7539 e->sequence, e->resource_id, e->minor_code);
7543 event_handle(xcb_generic_event_t *evt)
7545 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7547 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7548 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7549 XCB_EVENT_RESPONSE_TYPE(evt));
7552 #define EVENT(type, callback) case type: callback((void *)evt); return
7553 EVENT(0, event_error);
7554 EVENT(XCB_BUTTON_PRESS, buttonpress);
7555 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7556 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7557 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7558 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7559 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7560 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7561 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7562 /*EVENT(XCB_CREATE_NOTIFY, );*/
7563 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7564 EVENT(XCB_ENTER_NOTIFY, enternotify);
7565 EVENT(XCB_EXPOSE, expose);
7566 /*EVENT(XCB_FOCUS_IN, );*/
7567 /*EVENT(XCB_FOCUS_OUT, );*/
7568 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7569 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7570 EVENT(XCB_KEY_PRESS, keypress);
7571 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7572 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7573 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7574 EVENT(XCB_MAP_NOTIFY, mapnotify);
7575 EVENT(XCB_MAP_REQUEST, maprequest);
7576 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7577 /*EVENT(XCB_MOTION_NOTIFY, );*/
7578 /*EVENT(XCB_NO_EXPOSURE, );*/
7579 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7580 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7581 /*EVENT(XCB_RESIZE_REQUEST, );*/
7582 /*EVENT(XCB_SELECTION_CLEAR, );*/
7583 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7584 /*EVENT(XCB_SELECTION_REQUEST, );*/
7585 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7586 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7589 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7590 screenchange((void *)evt);
7594 main(int argc, char *argv[])
7596 struct swm_region *r, *rr;
7597 struct ws_win *winfocus = NULL;
7599 char conf[PATH_MAX], *cfile = NULL;
7601 int xfd, i, num_screens;
7602 struct sigaction sact;
7603 xcb_generic_event_t *evt;
7607 time_started = time(NULL);
7610 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7611 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
7612 warnx("no locale support");
7614 /* handle some signals */
7615 bzero(&sact, sizeof(sact));
7616 sigemptyset(&sact.sa_mask);
7618 sact.sa_handler = sighdlr;
7619 sigaction(SIGINT, &sact, NULL);
7620 sigaction(SIGQUIT, &sact, NULL);
7621 sigaction(SIGTERM, &sact, NULL);
7622 sigaction(SIGHUP, &sact, NULL);
7624 sact.sa_handler = sighdlr;
7625 sact.sa_flags = SA_NOCLDSTOP;
7626 sigaction(SIGCHLD, &sact, NULL);
7628 conn = xcb_connect(NULL, NULL);
7629 if (xcb_connection_has_error(conn))
7630 errx(1, "can not get XCB connection");
7632 xcb_prefetch_extension_data(conn, &xcb_randr_id);
7633 xfd = xcb_get_file_descriptor(conn);
7635 /* look for local and global conf file */
7636 pwd = getpwuid(getuid());
7638 errx(1, "invalid user: %d", getuid());
7640 xcb_grab_server(conn);
7643 /* flush all events */
7644 while ((evt = xcb_poll_for_event(conn))) {
7645 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7653 if (xcb_poll_for_event(conn) != NULL)
7654 errx(1, "another window manager is currently running");
7663 for (i = 0; ; i++) {
7668 snprintf(conf, sizeof conf, "%s/.%s",
7669 pwd->pw_dir, SWM_CONF_FILE);
7673 snprintf(conf, sizeof conf, "/etc/%s",
7678 snprintf(conf, sizeof conf, "%s/.%s",
7679 pwd->pw_dir, SWM_CONF_FILE_OLD);
7683 snprintf(conf, sizeof conf, "/etc/%s",
7690 if (strlen(conf) && stat(conf, &sb) != -1)
7691 if (S_ISREG(sb.st_mode)) {
7698 /* load conf (if any) */
7700 conf_load(cfile, SWM_CONF_DEFAULT);
7703 /* set some values to work around bad programs */
7705 /* grab existing windows (before we build the bars) */
7708 if (getenv("SWM_STARTED") == NULL)
7709 setenv("SWM_STARTED", "YES", 1);
7711 /* setup all bars */
7712 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7713 for (i = 0; i < num_screens; i++)
7714 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7715 if (winfocus == NULL)
7716 winfocus = TAILQ_FIRST(&r->ws->winlist);
7725 xcb_ungrab_server(conn);
7729 while ((evt = xcb_poll_for_event(conn))) {
7736 /* if we are being restarted go focus on first window */
7738 rr = winfocus->ws->r;
7740 /* not a visible window */
7744 /* move pointer to first screen if multi screen */
7745 if (num_screens > 1 || outputs > 1)
7746 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7747 rr->s[0].root, 0, 0, 0, 0, X(rr),
7748 Y(rr) + (bar_enabled ? bar_height : 0));
7750 a.id = SWM_ARG_ID_FOCUSCUR;
7760 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7762 DNPRINTF(SWM_D_MISC, "select failed");
7763 if (restart_wm == 1)
7764 restart(NULL, NULL);
7765 if (search_resp == 1)
7778 for (i = 0; i < num_screens; ++i)
7779 if (screens[i].bar_gc != 0)
7780 xcb_free_gc(conn, screens[i].bar_gc);
7782 XFreeFontSet(display, bar_fs);
7784 xcb_key_symbols_free(syms);
7785 xcb_disconnect(conn);