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/extensions/Xrandr.h>
92 #include <X11/Xft/Xft.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/xcb_atom.h>
95 #include <xcb/xcb_aux.h>
96 #include <xcb/xcb_event.h>
97 #include <xcb/xcb_icccm.h>
98 #include <xcb/xcb_keysyms.h>
99 #include <xcb/xtest.h>
100 #include <xcb/randr.h>
108 #ifdef SPECTRWM_BUILDSTR
109 static const char *buildstr = SPECTRWM_BUILDSTR;
111 static const char *buildstr = SPECTRWM_VERSION;
115 # error XRandR versions less than 1.0 are not supported
120 #define SWM_XRR_HAS_CRTC
124 #if defined(__OpenBSD__)
125 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
126 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
128 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
129 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
130 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
131 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
132 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
133 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
134 #define xcb_icccm_get_wm_class xcb_get_wm_class
135 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
136 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
137 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
138 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
139 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
140 #define xcb_icccm_get_wm_name xcb_get_wm_name
141 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
142 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
143 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
144 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
145 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
146 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
147 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
148 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
149 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
150 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
155 #define DPRINTF(x...) do { \
157 fprintf(stderr, x); \
159 #define DNPRINTF(n,x...) do { \
160 if (swm_debug & n) { \
161 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
162 fprintf(stderr, x); \
165 #define SWM_D_MISC 0x0001
166 #define SWM_D_EVENT 0x0002
167 #define SWM_D_WS 0x0004
168 #define SWM_D_FOCUS 0x0008
169 #define SWM_D_MOVE 0x0010
170 #define SWM_D_STACK 0x0020
171 #define SWM_D_MOUSE 0x0040
172 #define SWM_D_PROP 0x0080
173 #define SWM_D_CLASS 0x0100
174 #define SWM_D_KEY 0x0200
175 #define SWM_D_QUIRK 0x0400
176 #define SWM_D_SPAWN 0x0800
177 #define SWM_D_EVENTQ 0x1000
178 #define SWM_D_CONF 0x2000
179 #define SWM_D_BAR 0x4000
180 #define SWM_D_INIT 0x8000
182 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;
266 xcb_connection_t *conn;
267 xcb_key_symbols_t *syms;
270 int cycle_visible = 0;
272 int font_adjusted = 0;
273 unsigned int mod_key = MODKEY;
276 struct swm_region *search_r;
277 int select_list_pipe[2];
278 int select_resp_pipe[2];
280 volatile sig_atomic_t search_resp;
281 int search_resp_action;
283 struct search_window {
284 TAILQ_ENTRY(search_window) entry;
288 xcb_window_t indicator;
290 TAILQ_HEAD(search_winlist, search_window);
291 struct search_winlist search_wl;
296 SWM_SEARCH_UNICONIFY,
297 SWM_SEARCH_NAME_WORKSPACE,
298 SWM_SEARCH_SEARCH_WORKSPACE,
299 SWM_SEARCH_SEARCH_WINDOW
302 #define SWM_STACK_TOP (0)
303 #define SWM_STACK_BOTTOM (1)
304 #define SWM_STACK_ABOVE (2)
305 #define SWM_STACK_BELOW (3)
308 double dialog_ratio = 0.6;
310 #define SWM_BAR_MAX (256)
311 #define SWM_BAR_JUSTIFY_LEFT (0)
312 #define SWM_BAR_JUSTIFY_CENTER (1)
313 #define SWM_BAR_JUSTIFY_RIGHT (2)
314 #define SWM_BAR_OFFSET (4)
315 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
316 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
317 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
318 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
319 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
321 char *bar_argv[] = { NULL, NULL };
323 unsigned char bar_ext[SWM_BAR_MAX];
324 char bar_vertext[SWM_BAR_MAX];
326 sig_atomic_t bar_alarm = 0;
329 int bar_border_width = 1;
330 int bar_at_bottom = 0;
332 int bar_extra_running = 0;
335 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
336 char *bar_format = NULL;
337 int stack_enabled = 1;
338 int clock_enabled = 1;
339 int urgent_enabled = 0;
340 char *clock_format = NULL;
341 int title_name_enabled = 0;
342 int title_class_enabled = 0;
343 int window_name_enabled = 0;
344 int focus_mode = SWM_FOCUS_DEFAULT;
345 int focus_close = SWM_STACK_BELOW;
346 int focus_close_wrap = 1;
347 int focus_default = SWM_STACK_TOP;
348 int spawn_position = SWM_STACK_TOP;
349 int disable_border = 0;
350 int border_width = 1;
351 int verbose_layout = 0;
356 XftColor bar_font_color;
359 /* layout manager data */
360 struct swm_geometry {
373 struct swm_geometry g;
376 /* virtual "screens" */
378 TAILQ_ENTRY(swm_region) entry;
379 struct swm_geometry g;
380 struct workspace *ws; /* current workspace on this region */
381 struct workspace *ws_prior; /* prior workspace on this region */
382 struct swm_screen *s; /* screen idx */
385 TAILQ_HEAD(swm_region_list, swm_region);
388 TAILQ_ENTRY(ws_win) entry;
390 xcb_window_t transient;
391 struct ws_win *child_trans; /* transient child window */
392 struct swm_geometry g; /* current geometry */
393 struct swm_geometry g_float; /* region coordinates */
394 int g_floatvalid; /* g_float geometry validity */
395 int floatmaxed; /* whether maxed by max_stack */
400 unsigned int ewmh_flags;
401 int font_size_boundary[SWM_MAX_FONT_STEPS];
407 unsigned long quirks;
408 struct workspace *ws; /* always valid */
409 struct swm_screen *s; /* always valid, never changes */
410 xcb_get_geometry_reply_t *wa;
412 xcb_icccm_get_wm_class_reply_t ch;
413 xcb_icccm_wm_hints_t hints;
415 TAILQ_HEAD(ws_win_list, ws_win);
419 TAILQ_ENTRY(pid_e) entry;
423 TAILQ_HEAD(pid_list, pid_e);
424 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
426 /* layout handlers */
428 void vertical_config(struct workspace *, int);
429 void vertical_stack(struct workspace *, struct swm_geometry *);
430 void horizontal_config(struct workspace *, int);
431 void horizontal_stack(struct workspace *, struct swm_geometry *);
432 void max_stack(struct workspace *, struct swm_geometry *);
433 void plain_stacker(struct workspace *);
434 void fancy_stacker(struct workspace *);
437 void (*l_stack)(struct workspace *, struct swm_geometry *);
438 void (*l_config)(struct workspace *, int);
440 #define SWM_L_FOCUSPREV (1<<0)
441 #define SWM_L_MAPONFOCUS (1<<1)
442 void (*l_string)(struct workspace *);
444 /* stack, configure */
445 { vertical_stack, vertical_config, 0, plain_stacker },
446 { horizontal_stack, horizontal_config, 0, plain_stacker },
448 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
449 { NULL, NULL, 0, NULL },
452 /* position of max_stack mode in the layouts array, index into layouts! */
453 #define SWM_V_STACK (0)
454 #define SWM_H_STACK (1)
455 #define SWM_MAX_STACK (2)
457 #define SWM_H_SLICE (32)
458 #define SWM_V_SLICE (32)
460 /* define work spaces */
462 int idx; /* workspace index */
463 char *name; /* workspace name */
464 int always_raise; /* raise windows on focus */
465 struct layout *cur_layout; /* current layout handlers */
466 struct ws_win *focus; /* may be NULL */
467 struct ws_win *focus_prev; /* may be NULL */
468 struct swm_region *r; /* may be NULL */
469 struct swm_region *old_r; /* may be NULL */
470 struct ws_win_list winlist; /* list of windows in ws */
471 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
472 char stacker[10]; /* display stacker and layout */
476 int horizontal_msize;
478 int horizontal_stacks;
489 SWM_S_COLOR_BAR_BORDER,
490 SWM_S_COLOR_BAR_FONT,
496 /* physical screen mapping */
497 #define SWM_WS_MAX (22) /* hard limit */
498 int workspace_limit = 10; /* soft limit */
501 int idx; /* screen index */
502 struct swm_region_list rl; /* list of regions on this screen */
503 struct swm_region_list orl; /* list of old regions */
505 struct workspace ws[SWM_WS_MAX];
511 } c[SWM_S_COLOR_MAX];
513 xcb_gcontext_t bar_gc;
515 struct swm_screen *screens;
517 /* args to functions */
520 #define SWM_ARG_ID_FOCUSNEXT (0)
521 #define SWM_ARG_ID_FOCUSPREV (1)
522 #define SWM_ARG_ID_FOCUSMAIN (2)
523 #define SWM_ARG_ID_FOCUSCUR (4)
524 #define SWM_ARG_ID_SWAPNEXT (10)
525 #define SWM_ARG_ID_SWAPPREV (11)
526 #define SWM_ARG_ID_SWAPMAIN (12)
527 #define SWM_ARG_ID_MOVELAST (13)
528 #define SWM_ARG_ID_MASTERSHRINK (20)
529 #define SWM_ARG_ID_MASTERGROW (21)
530 #define SWM_ARG_ID_MASTERADD (22)
531 #define SWM_ARG_ID_MASTERDEL (23)
532 #define SWM_ARG_ID_FLIPLAYOUT (24)
533 #define SWM_ARG_ID_STACKRESET (30)
534 #define SWM_ARG_ID_STACKINIT (31)
535 #define SWM_ARG_ID_CYCLEWS_UP (40)
536 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
537 #define SWM_ARG_ID_CYCLESC_UP (42)
538 #define SWM_ARG_ID_CYCLESC_DOWN (43)
539 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
540 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
541 #define SWM_ARG_ID_STACKINC (50)
542 #define SWM_ARG_ID_STACKDEC (51)
543 #define SWM_ARG_ID_SS_ALL (60)
544 #define SWM_ARG_ID_SS_WINDOW (61)
545 #define SWM_ARG_ID_DONTCENTER (70)
546 #define SWM_ARG_ID_CENTER (71)
547 #define SWM_ARG_ID_KILLWINDOW (80)
548 #define SWM_ARG_ID_DELETEWINDOW (81)
549 #define SWM_ARG_ID_WIDTHGROW (90)
550 #define SWM_ARG_ID_WIDTHSHRINK (91)
551 #define SWM_ARG_ID_HEIGHTGROW (92)
552 #define SWM_ARG_ID_HEIGHTSHRINK (93)
553 #define SWM_ARG_ID_MOVEUP (100)
554 #define SWM_ARG_ID_MOVEDOWN (101)
555 #define SWM_ARG_ID_MOVELEFT (102)
556 #define SWM_ARG_ID_MOVERIGHT (103)
562 TAILQ_ENTRY(quirk) entry;
566 #define SWM_Q_FLOAT (1<<0) /* float this window */
567 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
568 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
569 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
570 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
571 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
573 TAILQ_HEAD(quirk_list, quirk);
574 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
577 * Supported EWMH hints should be added to
578 * both the enum and the ewmh array
583 _NET_MOVERESIZE_WINDOW,
584 _NET_WM_ACTION_CLOSE,
585 _NET_WM_ACTION_FULLSCREEN,
587 _NET_WM_ACTION_RESIZE,
588 _NET_WM_ALLOWED_ACTIONS,
591 _NET_WM_STATE_FULLSCREEN,
592 _NET_WM_STATE_HIDDEN,
593 _NET_WM_STATE_MAXIMIZED_HORZ,
594 _NET_WM_STATE_MAXIMIZED_VERT,
595 _NET_WM_STATE_SKIP_PAGER,
596 _NET_WM_STATE_SKIP_TASKBAR,
598 _NET_WM_WINDOW_TYPE_DIALOG,
599 _NET_WM_WINDOW_TYPE_DOCK,
600 _NET_WM_WINDOW_TYPE_NORMAL,
601 _NET_WM_WINDOW_TYPE_SPLASH,
602 _NET_WM_WINDOW_TYPE_TOOLBAR,
603 _NET_WM_WINDOW_TYPE_UTILITY,
604 _SWM_WM_STATE_MANUAL,
611 } ewmh[SWM_EWMH_HINT_MAX] = {
612 /* must be in same order as in the enum */
613 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
614 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
615 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
616 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
617 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
618 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
619 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
620 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
621 {"_NET_WM_STATE", XCB_ATOM_NONE},
622 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
623 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
624 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
625 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
626 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
627 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
628 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
629 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
630 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
631 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
632 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
633 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
634 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
635 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
636 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
639 /* function prototypes */
640 xcb_char2b_t *char2b(const char *);
641 int conf_load(char *, int);
642 void constrain_window(struct ws_win *, struct swm_region *, int);
644 void event_handle(xcb_generic_event_t *);
645 struct ws_win *find_window(xcb_window_t);
646 int floating_toggle_win(struct ws_win *);
647 void focus(struct swm_region *, union arg *);
648 void focus_magic(struct ws_win *);
649 xcb_atom_t get_atom_from_string(const char *);
650 xcb_screen_t *get_screen(int);
651 char *get_win_name(xcb_window_t);
652 uint16_t getstate(xcb_window_t);
653 void grabbuttons(struct ws_win *, int);
654 void map_window_raised(xcb_window_t);
655 void new_region(struct swm_screen *, int, int, int, int);
656 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
657 void spawn_select(struct swm_region *, union arg *, char *, int *);
658 void store_float_geom(struct ws_win *, struct swm_region *);
659 void unmanage_window(struct ws_win *);
660 void update_window(struct ws_win *);
662 char *get_atom_name(xcb_atom_t);
665 /* function definitions */
667 char2b(const char *str)
673 s = malloc(len * sizeof(xcb_char2b_t));
677 for (i = 0; i < len; i++) {
686 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
688 unsigned int tmpr, tmpg, tmpb;
690 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
701 get_screen(int screen)
703 xcb_screen_iterator_t i;
705 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
706 for (; i.rem; --screen, xcb_screen_next(&i))
716 xcb_get_input_focus_cookie_t c;
717 xcb_get_input_focus_reply_t *r;
720 c = xcb_get_input_focus(conn);
722 r = xcb_get_input_focus_reply(conn, c, NULL);
728 map_window_raised(xcb_window_t win)
730 uint32_t val = XCB_STACK_MODE_ABOVE;
732 xcb_configure_window(conn, win,
733 XCB_CONFIG_WINDOW_STACK_MODE, &val);
735 xcb_map_window(conn, win);
739 get_atom_from_string(const char *str)
741 xcb_intern_atom_cookie_t c;
742 xcb_intern_atom_reply_t *r;
745 c = xcb_intern_atom(conn, False, strlen(str), str);
746 r = xcb_intern_atom_reply(conn, c, NULL);
754 return (XCB_ATOM_NONE);
758 update_iconic(struct ws_win *win, int newv)
765 iprop = get_atom_from_string("_SWM_ICONIC");
766 if (iprop == XCB_ATOM_NONE)
770 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
771 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
773 xcb_delete_property(conn, win->id, iprop);
777 get_iconic(struct ws_win *win)
781 xcb_get_property_reply_t *pr = NULL;
783 iprop = get_atom_from_string("_SWM_ICONIC");
784 if (iprop == XCB_ATOM_NONE)
787 pr = xcb_get_property_reply(conn,
788 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
792 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
794 v = *((int32_t *)xcb_get_property_value(pr));
805 int i, j, num_screens;
807 sup_list = get_atom_from_string("_NET_SUPPORTED");
809 for (i = 0; i < LENGTH(ewmh); i++)
810 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
812 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
813 for (i = 0; i < num_screens; i++) {
814 /* Support check window will be created by workaround(). */
816 /* Report supported atoms */
817 xcb_delete_property(conn, screens[i].root, sup_list);
818 for (j = 0; j < LENGTH(ewmh); j++)
819 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
820 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
829 xcb_atom_t sup_check, sup_list;
831 xcb_get_property_cookie_t pc;
832 xcb_get_property_reply_t *pr;
834 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
835 sup_list = get_atom_from_string("_NET_SUPPORTED");
836 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
838 for (i = 0; i < num_screens; i++) {
839 /* Get the support check window and destroy it */
840 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
841 XCB_ATOM_WINDOW, 0, 1);
842 pr = xcb_get_property_reply(conn, pc, NULL);
844 id = *((xcb_window_t *)xcb_get_property_value(pr));
846 xcb_destroy_window(conn, id);
847 xcb_delete_property(conn, screens[i].root, sup_check);
848 xcb_delete_property(conn, screens[i].root, sup_list);
856 ewmh_autoquirk(struct ws_win *win)
860 xcb_get_property_cookie_t c;
861 xcb_get_property_reply_t *r;
863 c = xcb_get_property(conn, False, win->id,
864 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
865 r = xcb_get_property_reply(conn, c, NULL);
869 n = xcb_get_property_value_length(r);
870 type = xcb_get_property_value(r);
872 for (i = 0; i < n; i++) {
873 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
875 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
876 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
877 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
879 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
882 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
883 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
885 win->quirks = SWM_Q_FLOAT;
892 #define SWM_EWMH_ACTION_COUNT_MAX (6)
893 #define EWMH_F_FULLSCREEN (1<<0)
894 #define EWMH_F_ABOVE (1<<1)
895 #define EWMH_F_HIDDEN (1<<2)
896 #define EWMH_F_SKIP_PAGER (1<<3)
897 #define EWMH_F_SKIP_TASKBAR (1<<4)
898 #define SWM_F_MANUAL (1<<5)
901 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
909 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
910 "fullscreen %s\n", win->id, YESNO(fs));
913 if (!win->g_floatvalid)
914 store_float_geom(win, win->ws->r);
916 win->g = win->ws->r->g;
919 if (win->g_floatvalid) {
920 /* refloat at last floating relative position */
921 win->g = win->g_float;
922 X(win) += X(win->ws->r);
923 Y(win) += Y(win->ws->r);
931 ewmh_update_actions(struct ws_win *win)
933 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
939 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
942 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
943 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
946 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
947 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
950 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
951 #define _NET_WM_STATE_ADD 1 /* add/set property */
952 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
955 ewmh_update_win_state(struct ws_win *win, long state, long action)
957 unsigned int mask = 0;
958 unsigned int changed = 0;
959 unsigned int orig_flags;
964 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
965 "action: %ld\n", win->id, state, action);
967 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
968 mask = EWMH_F_FULLSCREEN;
969 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
971 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
973 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
974 mask = EWMH_F_SKIP_PAGER;
975 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
976 mask = EWMH_F_SKIP_TASKBAR;
979 orig_flags = win->ewmh_flags;
982 case _NET_WM_STATE_REMOVE:
983 win->ewmh_flags &= ~mask;
985 case _NET_WM_STATE_ADD:
986 win->ewmh_flags |= mask;
988 case _NET_WM_STATE_TOGGLE:
989 win->ewmh_flags ^= mask;
993 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
995 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
997 if (!floating_toggle_win(win))
998 win->ewmh_flags = orig_flags; /* revert */
999 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1001 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1002 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1004 if (!ewmh_set_win_fullscreen(win,
1005 win->ewmh_flags & EWMH_F_FULLSCREEN))
1006 win->ewmh_flags = orig_flags; /* revert */
1008 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1010 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1011 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1012 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1013 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1014 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1015 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1016 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1017 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1018 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1019 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1020 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1021 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1022 if (win->ewmh_flags & EWMH_F_ABOVE)
1023 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1024 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1025 &ewmh[_NET_WM_STATE_ABOVE].atom);
1026 if (win->ewmh_flags & SWM_F_MANUAL)
1027 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1028 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1029 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1033 ewmh_get_win_state(struct ws_win *win)
1036 xcb_get_property_cookie_t c;
1037 xcb_get_property_reply_t *r;
1043 win->ewmh_flags = 0;
1045 win->ewmh_flags |= EWMH_F_ABOVE;
1047 win->ewmh_flags |= SWM_F_MANUAL;
1049 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1050 XCB_ATOM_ATOM, 0, UINT32_MAX);
1051 r = xcb_get_property_reply(conn, c, NULL);
1055 states = xcb_get_property_value(r);
1056 n = xcb_get_property_value_length(r);
1058 for (i = 0; i < n; i++)
1059 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1067 dumpwins(struct swm_region *r, union arg *args)
1071 xcb_get_window_attributes_cookie_t c;
1072 xcb_get_window_attributes_reply_t *wa;
1074 if (r->ws == NULL) {
1075 warnx("dumpwins: invalid workspace");
1079 warnx("=== managed window list ws %02d ===", r->ws->idx);
1080 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1081 state = getstate(win->id);
1082 c = xcb_get_window_attributes(conn, win->id);
1083 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1085 warnx("window: 0x%x, map_state: %d, state: %u, "
1086 "transient: 0x%x", win->id, wa->map_state,
1087 state, win->transient);
1090 warnx("window: 0x%x, failed xcb_get_window_attributes",
1094 warnx("===== unmanaged window list =====");
1095 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1096 state = getstate(win->id);
1097 c = xcb_get_window_attributes(conn, win->id);
1098 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1100 warnx("window: 0x%x, map_state: %d, state: %u, "
1101 "transient: 0x%x", win->id, wa->map_state,
1102 state, win->transient);
1105 warnx("window: 0x%x, failed xcb_get_window_attributes",
1109 warnx("=================================");
1113 dumpwins(struct swm_region *r, union arg *args)
1116 #endif /* SWM_DEBUG */
1118 void event_error(xcb_generic_error_t *);
1119 void expose(xcb_expose_event_t *);
1120 void keypress(xcb_key_press_event_t *);
1121 void buttonpress(xcb_button_press_event_t *);
1122 void configurerequest(xcb_configure_request_event_t *);
1123 void configurenotify(xcb_configure_notify_event_t *);
1124 void destroynotify(xcb_destroy_notify_event_t *);
1125 void enternotify(xcb_enter_notify_event_t *);
1126 void mapnotify(xcb_map_notify_event_t *);
1127 void mappingnotify(xcb_mapping_notify_event_t *);
1128 void maprequest(xcb_map_request_event_t *);
1129 void propertynotify(xcb_property_notify_event_t *);
1130 void unmapnotify(xcb_unmap_notify_event_t *);
1131 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1132 void clientmessage(xcb_client_message_event_t *);
1133 void screenchange(xcb_randr_screen_change_notify_event_t *);
1138 int saved_errno, status;
1141 saved_errno = errno;
1145 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1150 if (errno != ECHILD)
1151 warn("sighdlr: waitpid");
1152 #endif /* SWM_DEBUG */
1155 if (pid == searchpid)
1159 if (WIFEXITED(status)) {
1160 if (WEXITSTATUS(status) != 0)
1161 warnx("sighdlr: child exit status: %d",
1162 WEXITSTATUS(status));
1164 warnx("sighdlr: child is terminated "
1166 #endif /* SWM_DEBUG */
1180 errno = saved_errno;
1186 struct pid_e *p = NULL;
1188 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1193 TAILQ_FOREACH(p, &pidlist, entry) {
1202 name_to_color(const char *colorname)
1204 uint32_t result = 0;
1205 char cname[32] = "#";
1206 xcb_screen_t *screen;
1207 xcb_colormap_t cmap;
1208 xcb_alloc_color_reply_t *cr;
1209 xcb_alloc_named_color_reply_t *nr;
1210 uint16_t rr, gg, bb;
1212 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1213 cmap = screen->default_colormap;
1215 /* color is in format rgb://rr/gg/bb */
1216 if (strncmp(colorname, "rgb:", 4) == 0) {
1217 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1218 warnx("could not parse rgb %s", colorname);
1220 cr = xcb_alloc_color_reply(conn,
1221 xcb_alloc_color(conn, cmap, rr, gg, bb),
1227 warnx("color '%s' not found", colorname);
1230 nr = xcb_alloc_named_color_reply(conn,
1231 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1234 strlcat(cname, colorname + 2, sizeof cname - 1);
1235 nr = xcb_alloc_named_color_reply(conn,
1236 xcb_alloc_named_color(conn, cmap, strlen(cname),
1243 warnx("color '%s' not found", colorname);
1250 setscreencolor(char *val, int i, int c)
1254 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1255 if (i > 0 && i <= num_screens) {
1256 screens[i - 1].c[c].color = name_to_color(val);
1257 free(screens[i - 1].c[c].name);
1258 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1260 } else if (i == -1) {
1261 for (i = 0; i < num_screens; i++) {
1262 screens[i].c[c].color = name_to_color(val);
1263 free(screens[i].c[c].name);
1264 if ((screens[i].c[c].name = strdup(val)) == NULL)
1268 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1273 fancy_stacker(struct workspace *ws)
1275 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1276 if (ws->cur_layout->l_stack == vertical_stack)
1277 snprintf(ws->stacker, sizeof ws->stacker,
1278 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1279 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1280 if (ws->cur_layout->l_stack == horizontal_stack)
1281 snprintf(ws->stacker, sizeof ws->stacker,
1282 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1283 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1287 plain_stacker(struct workspace *ws)
1289 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1290 if (ws->cur_layout->l_stack == vertical_stack)
1291 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1292 sizeof ws->stacker);
1293 if (ws->cur_layout->l_stack == horizontal_stack)
1294 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1295 sizeof ws->stacker);
1299 custom_region(char *val)
1301 unsigned int x, y, w, h;
1302 int sidx, num_screens;
1303 xcb_screen_t *screen;
1305 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1306 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1307 errx(1, "invalid custom region, "
1308 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1309 if (sidx < 1 || sidx > num_screens)
1310 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1314 screen = get_screen(sidx);
1316 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1318 if (x > screen->width_in_pixels ||
1319 y > screen->height_in_pixels ||
1320 w + x > screen->width_in_pixels ||
1321 h + y > screen->height_in_pixels) {
1322 warnx("ignoring region %ux%u+%u+%u - not within screen "
1323 "boundaries (%ux%u)", w, h, x, y,
1324 screen->width_in_pixels, screen->height_in_pixels);
1328 new_region(&screens[sidx], x, y, w, h);
1332 socket_setnonblock(int fd)
1336 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1337 err(1, "fcntl F_GETFL");
1338 flags |= O_NONBLOCK;
1339 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1340 err(1, "fcntl F_SETFL");
1344 bar_print(struct swm_region *r, const char *s)
1347 xcb_rectangle_t rect;
1355 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1357 switch (bar_justify) {
1358 case SWM_BAR_JUSTIFY_LEFT:
1361 case SWM_BAR_JUSTIFY_CENTER:
1362 x = (WIDTH(r) - info.width) / 2;
1364 case SWM_BAR_JUSTIFY_RIGHT:
1365 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1369 if (x < SWM_BAR_OFFSET)
1374 rect.width = WIDTH(r->bar);
1375 rect.height = HEIGHT(r->bar);
1377 /* clear back buffer */
1378 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1379 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1380 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1381 sizeof(rect), &rect);
1383 /* draw back buffer */
1384 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1385 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1386 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1387 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1389 draw = XftDrawCreate(display, r->bar->buffer,
1390 DefaultVisual(display, r->s->idx),
1391 DefaultColormap(display, r->s->idx));
1393 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x, bar_font->height,
1396 XftDrawDestroy(draw);
1399 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1400 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1404 bar_extra_stop(void)
1408 bzero(bar_pipe, sizeof bar_pipe);
1411 kill(bar_pid, SIGTERM);
1414 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1419 bar_class_name(char *s, size_t sz, struct swm_region *r)
1421 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1423 if (r->ws->focus->ch.class_name != NULL)
1424 strlcat(s, r->ws->focus->ch.class_name, sz);
1428 bar_title_name(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->ch.instance_name != NULL)
1433 strlcat(s, r->ws->focus->ch.instance_name, sz);
1437 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1439 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1442 bar_class_name(s, sz, r);
1443 strlcat(s, ":", sz);
1444 bar_title_name(s, sz, r);
1448 bar_window_float(char *s, size_t sz, struct swm_region *r)
1450 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1452 if (r->ws->focus->floating)
1453 strlcat(s, "(f)", sz);
1457 bar_window_name(char *s, size_t sz, struct swm_region *r)
1461 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1463 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1466 strlcat(s, title, sz);
1470 int urgent[SWM_WS_MAX];
1472 bar_urgent(char *s, size_t sz)
1475 int i, j, num_screens;
1477 xcb_get_property_cookie_t c;
1478 xcb_icccm_wm_hints_t hints;
1480 for (i = 0; i < workspace_limit; i++)
1483 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1484 for (i = 0; i < num_screens; i++)
1485 for (j = 0; j < workspace_limit; j++)
1486 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1487 c = xcb_icccm_get_wm_hints(conn, win->id);
1488 if (xcb_icccm_get_wm_hints_reply(conn, c,
1491 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1495 for (i = 0; i < workspace_limit; i++) {
1497 snprintf(b, sizeof b, "%d ", i + 1);
1499 snprintf(b, sizeof b, "- ");
1505 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1507 if (r == NULL || r->ws == NULL)
1509 if (r->ws->name != NULL)
1510 strlcat(s, r->ws->name, sz);
1513 /* build the default bar format according to the defined enabled options */
1515 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1517 /* if format provided, just copy the buffers */
1518 if (bar_format != NULL) {
1519 strlcpy(fmtnew, fmtexp, sz);
1523 /* reset the output buffer */
1526 strlcat(fmtnew, "+N:+I ", sz);
1528 strlcat(fmtnew, "+S", sz);
1529 strlcat(fmtnew, " ", sz);
1531 /* only show the workspace name if there's actually one */
1532 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1533 strlcat(fmtnew, "<+D>", sz);
1534 strlcat(fmtnew, "+3<", sz);
1536 if (clock_enabled) {
1537 strlcat(fmtnew, fmtexp, sz);
1538 strlcat(fmtnew, "+4<", sz);
1541 /* bar_urgent already adds the space before the last asterisk */
1543 strlcat(fmtnew, "* +U*+4<", sz);
1545 if (title_class_enabled) {
1546 strlcat(fmtnew, "+C", sz);
1547 if (title_name_enabled == 0)
1548 strlcat(fmtnew, "+4<", sz);
1551 /* checks needed by the colon and floating strlcat(3) calls below */
1552 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1553 if (title_name_enabled) {
1554 if (title_class_enabled)
1555 strlcat(fmtnew, ":", sz);
1556 strlcat(fmtnew, "+T+4<", sz);
1558 if (window_name_enabled) {
1559 if (r->ws->focus->floating)
1560 strlcat(fmtnew, "+F ", sz);
1561 strlcat(fmtnew, "+64W ", sz);
1565 /* finally add the action script output and the version */
1566 strlcat(fmtnew, "+4<+A+4<+V", sz);
1570 bar_replace_pad(char *tmp, int *limit, size_t sz)
1572 /* special case; no limit given, pad one space, instead */
1573 if (*limit == (int)sz - 1)
1575 snprintf(tmp, sz, "%*s", *limit, " ");
1578 /* replaces the bar format character sequences (like in tmux(1)) */
1580 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1584 char tmp[SWM_BAR_MAX];
1588 /* reset strlcat(3) buffer */
1591 /* get number, if any */
1594 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1595 limit = sizeof tmp - 1;
1596 if (limit <= 0 || limit >= (int)sizeof tmp)
1597 limit = sizeof tmp - 1;
1599 /* there is nothing to replace (ie EOL) */
1606 bar_replace_pad(tmp, &limit, sizeof tmp);
1609 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1612 bar_class_name(tmp, sizeof tmp, r);
1615 bar_workspace_name(tmp, sizeof tmp, r);
1618 bar_window_float(tmp, sizeof tmp, r);
1621 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1624 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1627 bar_class_title_name(tmp, sizeof tmp, r);
1630 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1633 bar_title_name(tmp, sizeof tmp, r);
1636 bar_urgent(tmp, sizeof tmp);
1639 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1642 bar_window_name(tmp, sizeof tmp, r);
1645 /* unknown character sequence; copy as-is */
1646 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1652 if ((int)len < limit)
1654 while (limit-- > 0) {
1655 if (*offrep >= sz - 1)
1657 fmtrep[(*offrep)++] = *ptr++;
1665 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1670 while (*fmt != '\0') {
1672 /* skip ordinary characters */
1675 fmtrep[off++] = *fmt++;
1679 /* character sequence found; replace it */
1680 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1689 bar_fmt_expand(char *fmtexp, size_t sz)
1696 /* start by grabbing the current time and date */
1698 localtime_r(&tmt, &tm);
1700 /* figure out what to expand */
1701 if (bar_format != NULL)
1703 else if (bar_format == NULL && clock_enabled)
1705 /* if nothing to expand bail out */
1711 /* copy as-is, just in case the format shouldn't be expanded below */
1712 strlcpy(fmtexp, fmt, sz);
1713 /* finally pass the string through strftime(3) */
1714 #ifndef SWM_DENY_CLOCK_FORMAT
1715 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1716 warnx("format too long");
1724 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1725 char fmtrep[SWM_BAR_MAX];
1727 struct swm_region *r;
1729 /* expand the format by first passing it through strftime(3) */
1730 bar_fmt_expand(fmtexp, sizeof fmtexp);
1732 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1733 for (i = 0; i < num_screens; i++) {
1734 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1737 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1738 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1739 bar_print(r, fmtrep);
1750 if (bar_enabled == 0)
1752 if (bar_extra && bar_extra_running) {
1753 /* ignore short reads; it'll correct itself */
1754 while ((b = fgetln(stdin, &len)) != NULL)
1755 if (b && b[len - 1] == '\n') {
1757 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1759 if (b == NULL && errno != EAGAIN) {
1760 warn("bar_update: bar_extra failed");
1764 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1777 bar_toggle(struct swm_region *r, union arg *args)
1779 struct swm_region *tmpr;
1782 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1784 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1786 for (i = 0; i < num_screens; i++)
1787 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1789 xcb_unmap_window(conn, tmpr->bar->id);
1791 for (i = 0; i < num_screens; i++)
1792 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1794 map_window_raised(tmpr->bar->id);
1797 bar_enabled = !bar_enabled;
1800 /* must be after stack */
1809 struct swm_region *r;
1813 /* do this here because the conf file is in memory */
1814 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1815 /* launch external status app */
1816 bar_extra_running = 1;
1817 if (pipe(bar_pipe) == -1)
1818 err(1, "pipe error");
1819 socket_setnonblock(bar_pipe[0]);
1820 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1821 if (dup2(bar_pipe[0], 0) == -1)
1823 if (dup2(bar_pipe[1], 1) == -1)
1825 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1826 err(1, "could not disable SIGPIPE");
1827 switch (bar_pid = fork()) {
1829 err(1, "cannot fork");
1833 execvp(bar_argv[0], bar_argv);
1834 err(1, "%s external app failed", bar_argv[0]);
1836 default: /* parent */
1842 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1843 for (i = 0; i < num_screens; i++)
1844 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1847 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1848 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1849 xcb_change_window_attributes(conn, r->bar->id,
1850 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1856 bar_setup(struct swm_region *r)
1859 xcb_screen_t *screen = get_screen(r->s->idx);
1864 XftColorFree(display, DefaultVisual(display, r->s->idx),
1865 DefaultColormap(display, r->s->idx), &bar_font_color);
1866 XftFontClose(display, bar_font);
1870 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1871 err(1, "bar_setup: calloc: failed to allocate memory.");
1873 while ((font = strsep(&bar_fonts, " ,")) != NULL) {
1877 DNPRINTF(SWM_D_INIT, "bar_setup: try font %s\n", font);
1878 bar_font = XftFontOpenName(display, r->s->idx, font);
1880 warnx("unable to load font %s", font);
1883 DNPRINTF(SWM_D_INIT, "successfully opened font %s\n",
1888 if (bar_font == NULL)
1889 errx(1, "unable to open a font");
1891 bar_height = bar_font->height + 4 * bar_border_width;
1896 color.alpha = 0xffff;
1897 color.red = (r->s->c[SWM_S_COLOR_BAR_FONT].color >> 16) & 0xffff;
1898 color.green = (r->s->c[SWM_S_COLOR_BAR_FONT].color >> 8) & 0xffff;
1899 color.blue = r->s->c[SWM_S_COLOR_BAR_FONT].color & 0xffff;
1900 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
1901 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
1902 warn("unable to allocate Xft color");
1905 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1906 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1907 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1909 r->bar->id = xcb_generate_id(conn);
1910 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1911 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1912 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1913 DNPRINTF(SWM_D_BAR, "bar_setup: create_window: (x,y) w x h: (%d,%d) "
1914 "%d x %d\n", X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar));
1915 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1916 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1917 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1918 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1919 | XCB_CW_EVENT_MASK, wa);
1921 r->bar->buffer = xcb_generate_id(conn);
1922 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1923 WIDTH(r->bar), HEIGHT(r->bar));
1925 xcb_randr_select_input(conn, r->bar->id,
1926 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1929 map_window_raised(r->bar->id);
1931 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1932 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1935 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1936 err(1, "could not install bar_signal");
1941 bar_cleanup(struct swm_region *r)
1945 xcb_destroy_window(conn, r->bar->id);
1946 xcb_free_pixmap(conn, r->bar->buffer);
1952 set_win_state(struct ws_win *win, uint16_t state)
1954 uint16_t data[2] = { state, XCB_ATOM_NONE };
1956 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
1961 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
1962 a_state, 32, 2, data);
1966 getstate(xcb_window_t w)
1968 uint16_t result = 0;
1969 xcb_get_property_cookie_t c;
1970 xcb_get_property_reply_t *r;
1972 c = xcb_get_property(conn, False, w, a_state, a_state, 0L, 2L);
1973 r = xcb_get_property_reply(conn, c, NULL);
1976 result = *((uint16_t *)xcb_get_property_value(r));
1980 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
1986 version(struct swm_region *r, union arg *args)
1988 bar_version = !bar_version;
1990 snprintf(bar_vertext, sizeof bar_vertext,
1991 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1993 strlcpy(bar_vertext, "", sizeof bar_vertext);
2000 client_msg(struct ws_win *win, xcb_atom_t a)
2002 xcb_client_message_event_t ev;
2007 bzero(&ev, sizeof ev);
2008 ev.response_type = XCB_CLIENT_MESSAGE;
2009 ev.window = win->id;
2012 ev.data.data32[0] = a;
2013 ev.data.data32[1] = XCB_CURRENT_TIME;
2015 xcb_send_event(conn, False, win->id,
2016 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2019 /* synthetic response to a ConfigureRequest when not making a change */
2021 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2023 xcb_configure_notify_event_t ce;
2028 /* send notification of unchanged state. */
2029 bzero(&ce, sizeof(ce));
2030 ce.response_type = XCB_CONFIGURE_NOTIFY;
2033 ce.width = WIDTH(win);
2034 ce.height = HEIGHT(win);
2035 ce.override_redirect = False;
2040 ce.window = win->id;
2041 ce.border_width = BORDER(win);
2042 ce.above_sibling = XCB_WINDOW_NONE;
2045 ce.event = ev->window;
2046 ce.window = ev->window;
2048 /* make response appear more WM_SIZE_HINTS-compliant */
2050 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2051 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2052 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2053 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2054 SH_INC_W(win), SH_INC_H(win));
2058 /* the hint may be set... to 0! */
2059 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2060 ce.width = SH_MIN_W(win);
2061 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2062 ce.height = SH_MIN_H(win);
2067 /* may also be advertized as 0 */
2068 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2069 ce.width = SH_MAX_W(win);
2070 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2071 ce.height = SH_MAX_H(win);
2074 /* resize increment. */
2076 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2077 ce.width -= (ce.width - SH_MIN_W(win)) %
2079 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2080 ce.height -= (ce.height - SH_MIN_H(win)) %
2084 /* adjust x and y for requested border_width. */
2085 ce.x += BORDER(win) - ev->border_width;
2086 ce.y += BORDER(win) - ev->border_width;
2087 ce.border_width = ev->border_width;
2088 ce.above_sibling = ev->sibling;
2091 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2092 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2093 ce.y, ce.width, ce.height, ce.border_width);
2095 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2100 count_win(struct workspace *ws, int count_transient)
2105 TAILQ_FOREACH(win, &ws->winlist, entry) {
2106 if (count_transient == 0 && win->floating)
2108 if (count_transient == 0 && win->transient)
2114 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2120 quit(struct swm_region *r, union arg *args)
2122 DNPRINTF(SWM_D_MISC, "quit\n");
2127 unmap_window(struct ws_win *win)
2129 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2134 /* don't unmap again */
2135 if (getstate(win->id) == XCB_ICCCM_WM_STATE_WITHDRAWN)
2138 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
2140 xcb_unmap_window(conn, win->id);
2141 xcb_change_window_attributes(conn, win->id,
2142 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2149 int i, j, num_screens;
2151 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2152 for (i = 0; i < num_screens; i++)
2153 for (j = 0; j < workspace_limit; j++)
2154 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2159 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2161 xcb_key_press_event_t event;
2162 xcb_keycode_t *keycode;
2167 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2169 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2172 bzero(&event, sizeof(event));
2173 event.event = win->id;
2174 event.root = win->s->root;
2175 event.child = XCB_WINDOW_NONE;
2176 event.time = XCB_CURRENT_TIME;
2177 event.event_x = X(win);
2178 event.event_y = Y(win);
2181 event.same_screen = True;
2182 event.detail = *keycode;
2183 event.state = modifiers;
2185 event.response_type = XCB_KEY_PRESS;
2186 xcb_send_event(conn, True, win->id,
2187 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2189 event.response_type = XCB_KEY_RELEASE;
2190 xcb_send_event(conn, True, win->id,
2191 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2195 restart(struct swm_region *r, union arg *args)
2197 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2199 /* disable alarm because the following code may not be interrupted */
2201 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2202 err(1, "can't disable alarm");
2208 XftFontClose(display, bar_font);
2209 XftColorFree(display, DefaultVisual(display, r->s->idx),
2210 DefaultColormap(display, r->s->idx), &bar_font_color);
2211 xcb_key_symbols_free(syms);
2213 xcb_disconnect(conn);
2215 execvp(start_argv[0], start_argv);
2216 warn("execvp failed");
2221 root_to_region(xcb_window_t root)
2223 struct swm_region *r = NULL;
2225 xcb_query_pointer_reply_t *qpr;
2227 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2229 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2230 for (i = 0; i < num_screens; i++)
2231 if (screens[i].root == root)
2234 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2235 screens[i].root), NULL);
2238 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2239 qpr->root_x, qpr->root_y);
2240 /* choose a region based on pointer location */
2241 TAILQ_FOREACH(r, &screens[i].rl, entry)
2242 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2243 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2249 r = TAILQ_FIRST(&screens[i].rl);
2255 find_unmanaged_window(xcb_window_t id)
2258 int i, j, num_screens;
2260 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2261 for (i = 0; i < num_screens; i++)
2262 for (j = 0; j < workspace_limit; j++)
2263 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2271 find_window(xcb_window_t id)
2274 int i, j, num_screens;
2275 xcb_query_tree_reply_t *r;
2277 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2278 for (i = 0; i < num_screens; i++)
2279 for (j = 0; j < workspace_limit; j++)
2280 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2284 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2288 /* if we were looking for the parent return that window instead */
2289 if (r->parent == 0 || r->root == r->parent)
2292 /* look for parent */
2293 for (i = 0; i < num_screens; i++)
2294 for (j = 0; j < workspace_limit; j++)
2295 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2296 if (r->parent == win->id) {
2306 spawn(int ws_idx, union arg *args, int close_fd)
2311 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2313 close(xcb_get_file_descriptor(conn));
2315 setenv("LD_PRELOAD", SWM_LIB, 1);
2317 if (asprintf(&ret, "%d", ws_idx) == -1) {
2318 warn("spawn: asprintf SWM_WS");
2321 setenv("_SWM_WS", ret, 1);
2325 if (asprintf(&ret, "%d", getpid()) == -1) {
2326 warn("spawn: asprintf _SWM_PID");
2329 setenv("_SWM_PID", ret, 1);
2333 if (setsid() == -1) {
2334 warn("spawn: setsid");
2340 * close stdin and stdout to prevent interaction between apps
2341 * and the baraction script
2342 * leave stderr open to record errors
2344 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2345 warn("spawn: open");
2348 dup2(fd, STDIN_FILENO);
2349 dup2(fd, STDOUT_FILENO);
2354 execvp(args->argv[0], args->argv);
2356 warn("spawn: execvp");
2361 kill_refs(struct ws_win *win)
2363 int i, x, num_screens;
2364 struct swm_region *r;
2365 struct workspace *ws;
2370 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2371 for (i = 0; i < num_screens; i++)
2372 TAILQ_FOREACH(r, &screens[i].rl, entry)
2373 for (x = 0; x < workspace_limit; x++) {
2375 if (win == ws->focus)
2377 if (win == ws->focus_prev)
2378 ws->focus_prev = NULL;
2383 validate_win(struct ws_win *testwin)
2386 struct workspace *ws;
2387 struct swm_region *r;
2388 int i, x, num_screens;
2390 if (testwin == NULL)
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++) {
2398 TAILQ_FOREACH(win, &ws->winlist, entry)
2406 validate_ws(struct workspace *testws)
2408 struct swm_region *r;
2409 struct workspace *ws;
2410 int i, x, num_screens;
2412 /* validate all ws */
2413 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2414 for (i = 0; i < num_screens; i++)
2415 TAILQ_FOREACH(r, &screens[i].rl, entry)
2416 for (x = 0; x < workspace_limit; x++) {
2425 unfocus_win(struct ws_win *win)
2427 xcb_window_t none = XCB_WINDOW_NONE;
2429 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2433 if (win->ws == NULL)
2436 if (validate_ws(win->ws))
2437 return; /* XXX this gets hit with thunderbird, needs fixing */
2439 if (win->ws->r == NULL)
2442 if (validate_win(win)) {
2447 if (win->ws->focus == win) {
2448 win->ws->focus = NULL;
2449 win->ws->focus_prev = win;
2452 if (validate_win(win->ws->focus)) {
2453 kill_refs(win->ws->focus);
2454 win->ws->focus = NULL;
2456 if (validate_win(win->ws->focus_prev)) {
2457 kill_refs(win->ws->focus_prev);
2458 win->ws->focus_prev = NULL;
2461 grabbuttons(win, 0);
2462 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2463 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2464 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2465 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2472 int i, j, num_screens;
2474 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2476 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2477 for (i = 0; i < num_screens; i++)
2478 for (j = 0; j < workspace_limit; j++)
2479 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2484 focus_win(struct ws_win *win)
2486 struct ws_win *cfw = NULL;
2487 xcb_get_input_focus_reply_t *r;
2488 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2490 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2494 if (win->ws == NULL)
2497 if (validate_ws(win->ws))
2498 return; /* XXX this gets hit with thunderbird, needs fixing */
2500 if (validate_win(win)) {
2505 if (validate_win(win)) {
2510 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2512 cur_focus = r->focus;
2515 if ((cfw = find_window(cur_focus)) != NULL)
2519 /* use larger hammer since the window was killed somehow */
2520 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2521 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2522 xcb_change_window_attributes(conn, cfw->id,
2523 XCB_CW_BORDER_PIXEL,
2524 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2528 win->ws->focus = win;
2530 if (win->ws->r != NULL) {
2532 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2533 win->id, XCB_CURRENT_TIME);
2534 grabbuttons(win, 1);
2535 xcb_change_window_attributes(conn, win->id,
2536 XCB_CW_BORDER_PIXEL,
2537 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2538 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2539 win->ws->always_raise)
2540 map_window_raised(win->id);
2542 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2543 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2551 event_drain(uint8_t rt)
2553 xcb_generic_event_t *evt;
2556 while ((evt = xcb_poll_for_event(conn))) {
2557 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2565 switchws(struct swm_region *r, union arg *args)
2567 int wsid = args->id, unmap_old = 0;
2568 struct swm_region *this_r, *other_r;
2570 struct workspace *new_ws, *old_ws;
2576 if (wsid >= workspace_limit)
2580 old_ws = this_r->ws;
2581 new_ws = &this_r->s->ws[wsid];
2583 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2584 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2586 if (new_ws == NULL || old_ws == NULL)
2588 if (new_ws == old_ws)
2591 other_r = new_ws->r;
2592 if (other_r == NULL) {
2593 /* the other workspace is hidden, hide this one */
2597 /* the other ws is visible in another region, exchange them */
2598 other_r->ws_prior = new_ws;
2599 other_r->ws = old_ws;
2600 old_ws->r = other_r;
2602 this_r->ws_prior = old_ws;
2603 this_r->ws = new_ws;
2606 /* this is needed so that we can click on a window after a restart */
2610 a.id = SWM_ARG_ID_FOCUSCUR;
2611 focus(new_ws->r, &a);
2613 /* unmap old windows */
2615 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2618 if (focus_mode == SWM_FOCUS_DEFAULT)
2619 event_drain(XCB_ENTER_NOTIFY);
2623 DNPRINTF(SWM_D_WS, "switchws: done\n");
2627 cyclews(struct swm_region *r, union arg *args)
2630 struct swm_screen *s = r->s;
2633 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2634 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2639 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2642 case SWM_ARG_ID_CYCLEWS_UP:
2643 if (a.id < workspace_limit - 1)
2648 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2651 case SWM_ARG_ID_CYCLEWS_DOWN:
2655 a.id = workspace_limit - 1;
2662 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2664 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2668 } while (a.id != r->ws->idx);
2672 priorws(struct swm_region *r, union arg *args)
2676 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2677 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2679 if (r->ws_prior == NULL)
2682 a.id = r->ws_prior->idx;
2687 cyclescr(struct swm_region *r, union arg *args)
2689 struct swm_region *rr = NULL;
2691 int i, x, y, num_screens;
2693 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2694 /* do nothing if we don't have more than one screen */
2695 if (!(num_screens > 1 || outputs > 1))
2700 case SWM_ARG_ID_CYCLESC_UP:
2701 rr = TAILQ_NEXT(r, entry);
2703 rr = TAILQ_FIRST(&screens[i].rl);
2705 case SWM_ARG_ID_CYCLESC_DOWN:
2706 rr = TAILQ_PREV(r, swm_region_list, entry);
2708 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2716 /* move mouse to region */
2718 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2719 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2722 a.id = SWM_ARG_ID_FOCUSCUR;
2725 if (rr->ws->focus) {
2726 /* move to focus window */
2727 x = X(rr->ws->focus) + 1;
2728 y = Y(rr->ws->focus) + 1;
2729 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2735 sort_windows(struct ws_win_list *wl)
2737 struct ws_win *win, *parent, *nxt;
2742 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2743 nxt = TAILQ_NEXT(win, entry);
2744 if (win->transient) {
2745 parent = find_window(win->transient);
2746 if (parent == NULL) {
2747 warnx("not possible bug");
2750 TAILQ_REMOVE(wl, win, entry);
2751 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2758 swapwin(struct swm_region *r, union arg *args)
2760 struct ws_win *target, *source;
2761 struct ws_win *cur_focus;
2762 struct ws_win_list *wl;
2765 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2766 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2768 cur_focus = r->ws->focus;
2769 if (cur_focus == NULL)
2773 wl = &source->ws->winlist;
2776 case SWM_ARG_ID_SWAPPREV:
2777 if (source->transient)
2778 source = find_window(source->transient);
2779 target = TAILQ_PREV(source, ws_win_list, entry);
2780 if (target && target->transient)
2781 target = find_window(target->transient);
2782 TAILQ_REMOVE(wl, source, entry);
2784 TAILQ_INSERT_TAIL(wl, source, entry);
2786 TAILQ_INSERT_BEFORE(target, source, entry);
2788 case SWM_ARG_ID_SWAPNEXT:
2789 target = TAILQ_NEXT(source, entry);
2790 /* move the parent and let the sort handle the move */
2791 if (source->transient)
2792 source = find_window(source->transient);
2793 TAILQ_REMOVE(wl, source, entry);
2795 TAILQ_INSERT_HEAD(wl, source, entry);
2797 TAILQ_INSERT_AFTER(wl, target, source, entry);
2799 case SWM_ARG_ID_SWAPMAIN:
2800 target = TAILQ_FIRST(wl);
2801 if (target == source) {
2802 if (source->ws->focus_prev != NULL &&
2803 source->ws->focus_prev != target)
2804 source = source->ws->focus_prev;
2808 if (target == NULL || source == NULL)
2810 source->ws->focus_prev = target;
2811 TAILQ_REMOVE(wl, target, entry);
2812 TAILQ_INSERT_BEFORE(source, target, entry);
2813 TAILQ_REMOVE(wl, source, entry);
2814 TAILQ_INSERT_HEAD(wl, source, entry);
2816 case SWM_ARG_ID_MOVELAST:
2817 TAILQ_REMOVE(wl, source, entry);
2818 TAILQ_INSERT_TAIL(wl, source, entry);
2821 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2833 focus_prev(struct ws_win *win)
2835 struct ws_win *winfocus = NULL;
2836 struct ws_win *cur_focus = NULL;
2837 struct ws_win_list *wl = NULL;
2838 struct workspace *ws = NULL;
2840 if (!(win && win->ws))
2845 cur_focus = ws->focus;
2847 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2848 WINID(win), WINID(cur_focus));
2850 /* pickle, just focus on whatever */
2851 if (cur_focus == NULL) {
2852 /* use prev_focus if valid */
2853 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2854 find_window(WINID(ws->focus_prev)))
2855 winfocus = ws->focus_prev;
2859 /* if transient focus on parent */
2860 if (cur_focus->transient) {
2861 winfocus = find_window(cur_focus->transient);
2865 /* if in max_stack try harder */
2866 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2867 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2868 if (cur_focus != ws->focus_prev)
2869 winfocus = ws->focus_prev;
2871 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2876 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2878 if (winfocus == NULL || winfocus == win) {
2879 switch (focus_close) {
2880 case SWM_STACK_BOTTOM:
2881 winfocus = TAILQ_FIRST(wl);
2884 winfocus = TAILQ_LAST(wl, ws_win_list);
2886 case SWM_STACK_ABOVE:
2887 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2888 if (focus_close_wrap)
2889 winfocus = TAILQ_FIRST(wl);
2891 winfocus = TAILQ_PREV(cur_focus,
2892 ws_win_list, entry);
2895 case SWM_STACK_BELOW:
2896 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2898 if (focus_close_wrap)
2899 winfocus = TAILQ_LAST(wl, ws_win_list);
2901 winfocus = TAILQ_NEXT(cur_focus, entry);
2907 if (winfocus == NULL) {
2908 if (focus_default == SWM_STACK_TOP)
2909 winfocus = TAILQ_LAST(wl, ws_win_list);
2911 winfocus = TAILQ_FIRST(wl);
2915 focus_magic(winfocus);
2919 focus(struct swm_region *r, union arg *args)
2921 struct ws_win *winfocus = NULL, *head;
2922 struct ws_win *cur_focus = NULL;
2923 struct ws_win_list *wl = NULL;
2924 struct workspace *ws = NULL;
2930 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2932 /* treat FOCUS_CUR special */
2933 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2934 if (r->ws->focus && r->ws->focus->iconic == 0)
2935 winfocus = r->ws->focus;
2936 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2937 winfocus = r->ws->focus_prev;
2939 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2940 if (winfocus->iconic == 0)
2943 focus_magic(winfocus);
2947 if ((cur_focus = r->ws->focus) == NULL)
2951 if (TAILQ_EMPTY(wl))
2953 /* make sure there is at least one uniconified window */
2955 TAILQ_FOREACH(winfocus, wl, entry)
2956 if (winfocus->iconic == 0) {
2964 case SWM_ARG_ID_FOCUSPREV:
2965 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2967 head = TAILQ_LAST(wl, ws_win_list);
2969 if (WINID(winfocus) == cur_focus->transient) {
2970 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2972 head = TAILQ_LAST(wl, ws_win_list);
2977 if (winfocus && winfocus->iconic) {
2978 while (winfocus != cur_focus) {
2979 if (winfocus == NULL)
2980 winfocus = TAILQ_LAST(wl, ws_win_list);
2981 if (winfocus->iconic == 0)
2983 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2989 case SWM_ARG_ID_FOCUSNEXT:
2990 head = TAILQ_NEXT(cur_focus, entry);
2992 head = TAILQ_FIRST(wl);
2996 if (winfocus && winfocus->iconic) {
2997 while (winfocus != cur_focus) {
2998 if (winfocus == NULL)
2999 winfocus = TAILQ_FIRST(wl);
3000 if (winfocus->iconic == 0)
3002 winfocus = TAILQ_NEXT(winfocus, entry);
3007 case SWM_ARG_ID_FOCUSMAIN:
3008 winfocus = TAILQ_FIRST(wl);
3009 if (winfocus == cur_focus)
3010 winfocus = cur_focus->ws->focus_prev;
3017 focus_magic(winfocus);
3023 cycle_layout(struct swm_region *r, union arg *args)
3025 struct workspace *ws = r->ws;
3028 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3031 if (ws->cur_layout->l_stack == NULL)
3032 ws->cur_layout = &layouts[0];
3037 if (focus_mode == SWM_FOCUS_DEFAULT)
3038 event_drain(XCB_ENTER_NOTIFY);
3040 a.id = SWM_ARG_ID_FOCUSCUR;
3045 stack_config(struct swm_region *r, union arg *args)
3047 struct workspace *ws = r->ws;
3049 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3052 if (ws->cur_layout->l_config != NULL)
3053 ws->cur_layout->l_config(ws, args->id);
3055 if (args->id != SWM_ARG_ID_STACKINIT)
3062 struct swm_geometry g;
3063 struct swm_region *r;
3069 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3071 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3072 for (i = 0; i < num_screens; i++) {
3076 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3077 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3078 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3080 /* start with screen geometry, adjust for bar */
3082 g.w -= 2 * border_width;
3083 g.h -= 2 * border_width;
3089 r->ws->cur_layout->l_stack(r->ws, &g);
3090 r->ws->cur_layout->l_string(r->ws);
3091 /* save r so we can track region changes */
3098 if (focus_mode == SWM_FOCUS_DEFAULT)
3099 event_drain(XCB_ENTER_NOTIFY);
3101 DNPRINTF(SWM_D_STACK, "stack: end\n");
3105 store_float_geom(struct ws_win *win, struct swm_region *r)
3107 /* retain window geom and region geom */
3108 win->g_float = win->g;
3109 win->g_float.x -= X(r);
3110 win->g_float.y -= Y(r);
3111 win->g_floatvalid = 1;
3112 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3113 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3114 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3115 win->g_float.w, win->g_float.h);
3119 stack_floater(struct ws_win *win, struct swm_region *r)
3124 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3127 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3128 * geom on ws switches or return from max mode
3130 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3131 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3132 /* refloat at last floating relative position */
3133 win->g = win->g_float;
3138 win->floatmaxed = 0;
3141 * if set to fullscreen mode, configure window to maximum size.
3143 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3144 if (!win->g_floatvalid)
3145 store_float_geom(win, win->ws->r);
3147 win->g = win->ws->r->g;
3151 * remove border on fullscreen floater when in fullscreen mode or when
3152 * the quirk is present.
3154 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3155 ((win->quirks & SWM_Q_FULLSCREEN) &&
3156 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3157 if (win->bordered) {
3159 X(win) += border_width;
3160 Y(win) += border_width;
3162 } else if (!win->bordered) {
3164 X(win) -= border_width;
3165 Y(win) -= border_width;
3168 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3169 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3170 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3175 * floaters and transients are auto-centred unless moved
3178 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3179 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3182 /* keep window within region bounds */
3183 constrain_window(win, r, 0);
3189 * Send keystrokes to terminal to decrease/increase the font size as the
3190 * window size changes.
3193 adjust_font(struct ws_win *win)
3195 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3196 win->floating || win->transient)
3199 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3200 WIDTH(win) / win->sh.width_inc < term_width &&
3201 win->font_steps < SWM_MAX_FONT_STEPS) {
3202 win->font_size_boundary[win->font_steps] =
3203 (win->sh.width_inc * term_width) + win->sh.base_width;
3206 win->last_inc = win->sh.width_inc;
3207 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3208 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3209 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3212 win->last_inc = win->sh.width_inc;
3213 fake_keypress(win, XK_KP_Add, ShiftMask);
3217 #define SWAPXY(g) do { \
3219 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3220 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3223 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3225 struct swm_geometry win_g, r_g = *g;
3226 struct ws_win *win, *fs_win = NULL;
3227 int i, j, s, stacks;
3228 int w_inc = 1, h_inc, w_base = 1, h_base;
3229 int hrh, extra = 0, h_slice, last_h = 0;
3230 int split, colno, winno, mwin, msize, mscale;
3231 int remain, missing, v_slice, reconfigure;
3233 xcb_get_window_attributes_reply_t *war;
3235 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3236 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3238 winno = count_win(ws, 0);
3239 if (winno == 0 && count_win(ws, 1) == 0)
3242 TAILQ_FOREACH(win, &ws->winlist, entry)
3243 if (win->transient == 0 && win->floating == 0
3244 && win->iconic == 0)
3251 w_inc = win->sh.width_inc;
3252 w_base = win->sh.base_width;
3253 mwin = ws->l_state.horizontal_mwin;
3254 mscale = ws->l_state.horizontal_msize;
3255 stacks = ws->l_state.horizontal_stacks;
3258 w_inc = win->sh.height_inc;
3259 w_base = win->sh.base_height;
3260 mwin = ws->l_state.vertical_mwin;
3261 mscale = ws->l_state.vertical_msize;
3262 stacks = ws->l_state.vertical_stacks;
3266 if (stacks > winno - mwin)
3267 stacks = winno - mwin;
3271 h_slice = r_g.h / SWM_H_SLICE;
3272 if (mwin && winno > mwin) {
3273 v_slice = r_g.w / SWM_V_SLICE;
3277 win_g.w = v_slice * mscale;
3279 if (w_inc > 1 && w_inc < v_slice) {
3280 /* adjust for window's requested size increment */
3281 remain = (win_g.w - w_base) % w_inc;
3287 win_g.x += r_g.w - msize;
3290 colno = split = winno / stacks;
3291 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3292 2 * border_width) / stacks);
3294 hrh = r_g.h / colno;
3295 extra = r_g.h - (colno * hrh);
3296 win_g.h = hrh - 2 * border_width;
3298 /* stack all the tiled windows */
3299 i = j = 0, s = stacks;
3300 TAILQ_FOREACH(win, &ws->winlist, entry) {
3301 if (win->transient != 0 || win->floating != 0)
3303 if (win->iconic != 0)
3306 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3311 if (split && i == split) {
3312 colno = (winno - mwin) / stacks;
3313 if (s <= (winno - mwin) % stacks)
3315 split = split + colno;
3316 hrh = (r_g.h / colno);
3317 extra = r_g.h - (colno * hrh);
3321 win_g.x += win_g.w + 2 * border_width;
3322 win_g.w = (r_g.w - msize -
3323 (stacks * 2 * border_width)) / stacks;
3325 win_g.w += (r_g.w - msize -
3326 (stacks * 2 * border_width)) % stacks;
3330 win_g.h = hrh - 2 * border_width;
3332 h_inc = win->sh.width_inc;
3333 h_base = win->sh.base_width;
3335 h_inc = win->sh.height_inc;
3336 h_base = win->sh.base_height;
3338 if (j == colno - 1) {
3339 win_g.h = hrh + extra;
3340 } else if (h_inc > 1 && h_inc < h_slice) {
3341 /* adjust for window's requested size increment */
3342 remain = (win_g.h - h_base) % h_inc;
3343 missing = h_inc - remain;
3345 if (missing <= extra || j == 0) {
3357 win_g.y += last_h + 2 * border_width;
3359 if (disable_border && bar_enabled == 0 && winno == 1){
3361 win_g.w += 2 * border_width;
3362 win_g.h += 2 * border_width;
3367 if (X(win) != win_g.y || Y(win) != win_g.x ||
3368 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3372 WIDTH(win) = win_g.h;
3373 HEIGHT(win) = win_g.w;
3376 if (X(win) != win_g.x || Y(win) != win_g.y ||
3377 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3381 WIDTH(win) = win_g.w;
3382 HEIGHT(win) = win_g.h;
3386 if (bordered != win->bordered) {
3388 win->bordered = bordered;
3396 war = xcb_get_window_attributes_reply(conn,
3397 xcb_get_window_attributes(conn, win->id),
3400 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3401 map_window_raised(win->id);
3411 /* now, stack all the floaters and transients */
3412 TAILQ_FOREACH(win, &ws->winlist, entry) {
3413 if (win->transient == 0 && win->floating == 0)
3415 if (win->iconic == 1)
3417 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3422 stack_floater(win, ws->r);
3423 map_window_raised(win->id);
3427 stack_floater(fs_win, ws->r);
3428 map_window_raised(fs_win->id);
3433 vertical_config(struct workspace *ws, int id)
3435 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3439 case SWM_ARG_ID_STACKRESET:
3440 case SWM_ARG_ID_STACKINIT:
3441 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3442 ws->l_state.vertical_mwin = 1;
3443 ws->l_state.vertical_stacks = 1;
3445 case SWM_ARG_ID_MASTERSHRINK:
3446 if (ws->l_state.vertical_msize > 1)
3447 ws->l_state.vertical_msize--;
3449 case SWM_ARG_ID_MASTERGROW:
3450 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3451 ws->l_state.vertical_msize++;
3453 case SWM_ARG_ID_MASTERADD:
3454 ws->l_state.vertical_mwin++;
3456 case SWM_ARG_ID_MASTERDEL:
3457 if (ws->l_state.vertical_mwin > 0)
3458 ws->l_state.vertical_mwin--;
3460 case SWM_ARG_ID_STACKINC:
3461 ws->l_state.vertical_stacks++;
3463 case SWM_ARG_ID_STACKDEC:
3464 if (ws->l_state.vertical_stacks > 1)
3465 ws->l_state.vertical_stacks--;
3467 case SWM_ARG_ID_FLIPLAYOUT:
3468 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3476 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3478 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3480 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3484 horizontal_config(struct workspace *ws, int id)
3486 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3489 case SWM_ARG_ID_STACKRESET:
3490 case SWM_ARG_ID_STACKINIT:
3491 ws->l_state.horizontal_mwin = 1;
3492 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3493 ws->l_state.horizontal_stacks = 1;
3495 case SWM_ARG_ID_MASTERSHRINK:
3496 if (ws->l_state.horizontal_msize > 1)
3497 ws->l_state.horizontal_msize--;
3499 case SWM_ARG_ID_MASTERGROW:
3500 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3501 ws->l_state.horizontal_msize++;
3503 case SWM_ARG_ID_MASTERADD:
3504 ws->l_state.horizontal_mwin++;
3506 case SWM_ARG_ID_MASTERDEL:
3507 if (ws->l_state.horizontal_mwin > 0)
3508 ws->l_state.horizontal_mwin--;
3510 case SWM_ARG_ID_STACKINC:
3511 ws->l_state.horizontal_stacks++;
3513 case SWM_ARG_ID_STACKDEC:
3514 if (ws->l_state.horizontal_stacks > 1)
3515 ws->l_state.horizontal_stacks--;
3517 case SWM_ARG_ID_FLIPLAYOUT:
3518 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3526 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3528 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3530 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3533 /* fullscreen view */
3535 max_stack(struct workspace *ws, struct swm_geometry *g)
3537 struct swm_geometry gg = *g;
3538 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3539 int winno, num_screens;
3541 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3546 winno = count_win(ws, 0);
3547 if (winno == 0 && count_win(ws, 1) == 0)
3550 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3551 TAILQ_FOREACH(win, &ws->winlist, entry) {
3552 if (win->transient) {
3554 parent = find_window(win->transient);
3558 if (win->floating && win->floatmaxed == 0 ) {
3560 * retain geometry for retrieval on exit from
3563 store_float_geom(win, ws->r);
3564 win->floatmaxed = 1;
3567 /* only reconfigure if necessary */
3568 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3569 HEIGHT(win) != gg.h) {
3575 WIDTH(win) += 2 * border_width;
3576 HEIGHT(win) += 2 * border_width;
3581 /* unmap only if we don't have multi screen */
3582 if (win != ws->focus)
3583 if (!(num_screens > 1 || outputs > 1))
3587 /* put the last transient on top */
3590 map_window_raised(parent->id);
3591 stack_floater(wintrans, ws->r);
3592 focus_magic(wintrans);
3597 send_to_ws(struct swm_region *r, union arg *args)
3599 int wsid = args->id;
3600 struct ws_win *win = NULL, *parent;
3601 struct workspace *ws, *nws;
3602 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3603 char ws_idx_str[SWM_PROPLEN];
3606 if (wsid >= workspace_limit)
3609 if (r && r->ws && r->ws->focus)
3615 if (win->ws->idx == wsid)
3618 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3621 nws = &win->s->ws[wsid];
3623 a.id = SWM_ARG_ID_FOCUSPREV;
3625 if (win->transient) {
3626 parent = find_window(win->transient);
3628 unmap_window(parent);
3629 TAILQ_REMOVE(&ws->winlist, parent, entry);
3630 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3635 TAILQ_REMOVE(&ws->winlist, win, entry);
3636 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3637 if (TAILQ_EMPTY(&ws->winlist))
3638 r->ws->focus = NULL;
3641 /* Try to update the window's workspace property */
3642 ws_idx_atom = get_atom_from_string("_SWM_WS");
3644 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3646 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3648 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3649 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3660 pressbutton(struct swm_region *r, union arg *args)
3662 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3663 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3664 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3665 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3669 raise_toggle(struct swm_region *r, union arg *args)
3671 if (r == NULL || r->ws == NULL)
3674 r->ws->always_raise = !r->ws->always_raise;
3676 /* bring floaters back to top */
3677 if (r->ws->always_raise == 0)
3684 iconify(struct swm_region *r, union arg *args)
3688 if (r->ws->focus == NULL)
3690 unmap_window(r->ws->focus);
3691 update_iconic(r->ws->focus, 1);
3694 r->ws->focus = NULL;
3695 a.id = SWM_ARG_ID_FOCUSCUR;
3702 get_win_name(xcb_window_t win)
3705 xcb_get_property_cookie_t c;
3706 xcb_icccm_get_text_property_reply_t r;
3708 c = xcb_icccm_get_wm_name(conn, win);
3709 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3710 if (r.name_len > 0) {
3711 name = malloc(r.name_len + 1);
3713 xcb_icccm_get_text_property_reply_wipe(&r);
3716 memcpy(name, r.name, r.name_len);
3717 name[r.name_len] = '\0';
3719 xcb_icccm_get_text_property_reply_wipe(&r);
3726 uniconify(struct swm_region *r, union arg *args)
3733 DNPRINTF(SWM_D_MISC, "uniconify\n");
3735 if (r == NULL || r->ws == NULL)
3738 /* make sure we have anything to uniconify */
3739 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3740 if (win->ws == NULL)
3741 continue; /* should never happen */
3742 if (win->iconic == 0)
3750 search_resp_action = SWM_SEARCH_UNICONIFY;
3752 spawn_select(r, args, "search", &searchpid);
3754 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3757 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3758 if (win->ws == NULL)
3759 continue; /* should never happen */
3760 if (win->iconic == 0)
3763 name = get_win_name(win->id);
3766 fprintf(lfile, "%s.%u\n", name, win->id);
3776 name_workspace(struct swm_region *r, union arg *args)
3780 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3786 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3788 spawn_select(r, args, "name_workspace", &searchpid);
3790 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3793 fprintf(lfile, "%s", "");
3798 search_workspace(struct swm_region *r, union arg *args)
3801 struct workspace *ws;
3804 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3810 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3812 spawn_select(r, args, "search", &searchpid);
3814 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3817 for (i = 0; i < workspace_limit; i++) {
3821 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3822 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3829 search_win_cleanup(void)
3831 struct search_window *sw = NULL;
3833 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3834 xcb_destroy_window(conn, sw->indicator);
3835 xcb_free_gc(conn, sw->gc);
3836 TAILQ_REMOVE(&search_wl, sw, entry);
3842 search_win(struct swm_region *r, union arg *args)
3844 struct ws_win *win = NULL;
3845 struct search_window *sw = NULL;
3847 uint32_t gcv[3], wa[2];
3854 DNPRINTF(SWM_D_MISC, "search_win\n");
3857 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3859 spawn_select(r, args, "search", &searchpid);
3861 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3864 TAILQ_INIT(&search_wl);
3867 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3868 if (win->iconic == 1)
3871 sw = calloc(1, sizeof(struct search_window));
3873 warn("search_win: calloc");
3875 search_win_cleanup();
3881 snprintf(s, sizeof s, "%d", i);
3884 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
3886 w = xcb_generate_id(conn);
3887 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3888 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3889 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3890 info.width + 4, bar_font->height + 4,
3891 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3892 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3895 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3897 sw->gc = xcb_generate_id(conn);
3898 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3899 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3901 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3902 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3903 map_window_raised(w);
3905 xcb_image_text_8(conn, len, w, sw->gc, 2, bar_font->height, s);
3907 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3909 fprintf(lfile, "%d\n", i);
3919 search_resp_uniconify(char *resp, unsigned long len)
3925 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3927 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3928 if (win->iconic == 0)
3930 name = get_win_name(win->id);
3933 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3938 if (strncmp(s, resp, len) == 0) {
3939 /* XXX this should be a callback to generalize */
3940 update_iconic(win, 0);
3949 search_resp_name_workspace(char *resp, unsigned long len)
3951 struct workspace *ws;
3953 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3955 if (search_r->ws == NULL)
3960 free(search_r->ws->name);
3961 search_r->ws->name = NULL;
3965 ws->name = strdup(resp);
3966 if (ws->name == NULL) {
3967 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3968 "strdup: %s", strerror(errno));
3975 search_resp_search_workspace(char *resp, unsigned long len)
3982 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3986 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3993 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
3995 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4002 switchws(search_r, &a);
4006 search_resp_search_window(char *resp, unsigned long len)
4011 struct search_window *sw;
4013 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4017 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4022 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4024 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4031 TAILQ_FOREACH(sw, &search_wl, entry)
4032 if (idx == sw->idx) {
4038 #define MAX_RESP_LEN 1024
4041 search_do_resp(void)
4047 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4052 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4053 warn("search: calloc");
4057 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4059 warn("search: read error");
4062 resp[rbytes] = '\0';
4065 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4066 * newline, so work around that by sanitizing the resp now.
4068 resp[strcspn(resp, "\n")] = '\0';
4071 switch (search_resp_action) {
4072 case SWM_SEARCH_UNICONIFY:
4073 search_resp_uniconify(resp, len);
4075 case SWM_SEARCH_NAME_WORKSPACE:
4076 search_resp_name_workspace(resp, len);
4078 case SWM_SEARCH_SEARCH_WORKSPACE:
4079 search_resp_search_workspace(resp, len);
4081 case SWM_SEARCH_SEARCH_WINDOW:
4082 search_resp_search_window(resp, len);
4087 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4088 search_win_cleanup();
4090 search_resp_action = SWM_SEARCH_NONE;
4091 close(select_resp_pipe[0]);
4096 wkill(struct swm_region *r, union arg *args)
4098 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4100 if (r->ws->focus == NULL)
4103 if (args->id == SWM_ARG_ID_KILLWINDOW)
4104 xcb_kill_client(conn, r->ws->focus->id);
4106 if (r->ws->focus->can_delete)
4107 client_msg(r->ws->focus, a_delete);
4114 floating_toggle_win(struct ws_win *win)
4116 struct swm_region *r;
4126 /* reject floating toggles in max stack mode */
4127 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4130 if (win->floating) {
4131 if (!win->floatmaxed) {
4132 /* retain position for refloat */
4133 store_float_geom(win, r);
4137 if (win->g_floatvalid) {
4138 /* refloat at last floating relative position */
4139 X(win) = win->g_float.x + X(r);
4140 Y(win) = win->g_float.y + Y(r);
4141 WIDTH(win) = win->g_float.w;
4142 HEIGHT(win) = win->g_float.h;
4147 ewmh_update_actions(win);
4153 floating_toggle(struct swm_region *r, union arg *args)
4155 struct ws_win *win = r->ws->focus;
4161 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4162 _NET_WM_STATE_TOGGLE);
4166 if (win == win->ws->focus) {
4167 a.id = SWM_ARG_ID_FOCUSCUR;
4168 focus(win->ws->r, &a);
4172 event_drain(XCB_ENTER_NOTIFY);
4176 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4178 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4180 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4182 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4185 if (X(win) + BORDER(win) < X(r)) {
4187 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4189 X(win) = X(r) - BORDER(win);
4192 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4194 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4196 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4199 if (Y(win) + BORDER(win) < Y(r)) {
4201 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4203 Y(win) = Y(r) - BORDER(win);
4209 if (HEIGHT(win) < 1)
4215 update_window(struct ws_win *win)
4220 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4221 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4222 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4226 wc[3] = HEIGHT(win);
4227 wc[4] = BORDER(win);
4229 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4230 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4231 wc[3], YESNO(win->bordered));
4233 xcb_configure_window(conn, win->id, mask, wc);
4236 #define SWM_RESIZE_STEPS (50)
4239 resize(struct ws_win *win, union arg *args)
4241 xcb_timestamp_t timestamp = 0;
4242 struct swm_region *r = NULL;
4243 int resize_step = 0;
4244 struct swm_geometry g;
4245 int top = 0, left = 0, resizing;
4247 unsigned int shape; /* cursor style */
4248 xcb_cursor_t cursor;
4249 xcb_font_t cursor_font;
4250 xcb_grab_pointer_cookie_t gpc;
4251 xcb_grab_pointer_reply_t *gpr;
4252 xcb_query_pointer_reply_t *xpr;
4253 xcb_generic_event_t *evt;
4254 xcb_motion_notify_event_t *mne;
4260 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4263 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4264 "transient: 0x%x\n", win->id, YESNO(win->floating),
4267 if (!(win->transient != 0 || win->floating != 0))
4270 /* reject resizes in max mode for floaters (transient ok) */
4271 if (win->floatmaxed)
4275 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4281 case SWM_ARG_ID_WIDTHSHRINK:
4282 WIDTH(win) -= SWM_RESIZE_STEPS;
4285 case SWM_ARG_ID_WIDTHGROW:
4286 WIDTH(win) += SWM_RESIZE_STEPS;
4289 case SWM_ARG_ID_HEIGHTSHRINK:
4290 HEIGHT(win) -= SWM_RESIZE_STEPS;
4293 case SWM_ARG_ID_HEIGHTGROW:
4294 HEIGHT(win) += SWM_RESIZE_STEPS;
4301 constrain_window(win, r, 1);
4303 store_float_geom(win,r);
4307 /* get cursor offset from window root */
4308 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4315 if (xpr->win_x < WIDTH(win) / 2)
4318 if (xpr->win_y < HEIGHT(win) / 2)
4321 if (args->id == SWM_ARG_ID_CENTER)
4324 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4326 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4328 cursor_font = xcb_generate_id(conn);
4329 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4331 cursor = xcb_generate_id(conn);
4332 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4333 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4335 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4336 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4338 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4340 xcb_free_cursor(conn, cursor);
4341 xcb_close_font(conn, cursor_font);
4348 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4349 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4350 case XCB_BUTTON_RELEASE:
4351 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4354 case XCB_MOTION_NOTIFY:
4355 mne = (xcb_motion_notify_event_t *)evt;
4356 /* cursor offset/delta from start of the operation */
4357 dx = mne->root_x - xpr->root_x;
4358 dy = mne->root_y - xpr->root_y;
4364 if (args->id == SWM_ARG_ID_CENTER) {
4365 if (g.h / 2 + dy < 1)
4369 HEIGHT(win) = g.h + 2 * dy;
4377 HEIGHT(win) = g.h + dy;
4384 if (args->id == SWM_ARG_ID_CENTER) {
4385 if (g.w / 2 + dx < 1)
4389 WIDTH(win) = g.w + 2 * dx;
4397 WIDTH(win) = g.w + dx;
4400 constrain_window(win, r, 1);
4402 /* not free, don't sync more than 120 times / second */
4403 if ((mne->time - timestamp) > (1000 / 120) ) {
4404 timestamp = mne->time;
4419 store_float_geom(win,r);
4421 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4422 xcb_free_cursor(conn, cursor);
4423 xcb_close_font(conn, cursor_font);
4426 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4430 resize_step(struct swm_region *r, union arg *args)
4432 struct ws_win *win = NULL;
4434 if (r && r->ws && r->ws->focus)
4442 #define SWM_MOVE_STEPS (50)
4445 move(struct ws_win *win, union arg *args)
4447 xcb_timestamp_t timestamp = 0;
4448 int move_step = 0, moving;
4449 struct swm_region *r = NULL;
4450 xcb_font_t cursor_font;
4451 xcb_cursor_t cursor;
4452 xcb_grab_pointer_cookie_t gpc;
4453 xcb_grab_pointer_reply_t *gpr;
4454 xcb_query_pointer_reply_t *qpr;
4455 xcb_generic_event_t *evt;
4456 xcb_motion_notify_event_t *mne;
4462 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4465 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4466 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4468 /* in max_stack mode should only move transients */
4469 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4473 if (win->floating == 0 && !win->transient) {
4474 store_float_geom(win, r);
4475 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4478 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4485 case SWM_ARG_ID_MOVELEFT:
4486 X(win) -= (SWM_MOVE_STEPS - border_width);
4489 case SWM_ARG_ID_MOVERIGHT:
4490 X(win) += (SWM_MOVE_STEPS - border_width);
4493 case SWM_ARG_ID_MOVEUP:
4494 Y(win) -= (SWM_MOVE_STEPS - border_width);
4497 case SWM_ARG_ID_MOVEDOWN:
4498 Y(win) += (SWM_MOVE_STEPS - border_width);
4505 constrain_window(win, r, 0);
4507 store_float_geom(win, r);
4511 cursor_font = xcb_generate_id(conn);
4512 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4514 cursor = xcb_generate_id(conn);
4515 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4516 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4518 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4519 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4520 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4521 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4523 xcb_free_cursor(conn, cursor);
4524 xcb_close_font(conn, cursor_font);
4528 /* get cursor offset from window root */
4529 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4532 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4533 xcb_free_cursor(conn, cursor);
4534 xcb_close_font(conn, cursor_font);
4540 while ((evt = xcb_wait_for_event(conn)) && moving) {
4541 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4542 case XCB_BUTTON_RELEASE:
4543 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4546 case XCB_MOTION_NOTIFY:
4547 mne = (xcb_motion_notify_event_t *)evt;
4548 X(win) = mne->root_x - qpr->win_x - border_width;
4549 Y(win) = mne->root_y - qpr->win_y - border_width;
4551 constrain_window(win, r, 0);
4553 /* not free, don't sync more than 120 times / second */
4554 if ((mne->time - timestamp) > (1000 / 120) ) {
4555 timestamp = mne->time;
4570 store_float_geom(win, r);
4572 xcb_free_cursor(conn, cursor);
4573 xcb_close_font(conn, cursor_font);
4574 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4575 DNPRINTF(SWM_D_EVENT, "move: done\n");
4579 move_step(struct swm_region *r, union arg *args)
4581 struct ws_win *win = NULL;
4583 if (r && r->ws && r->ws->focus)
4588 if (!(win->transient != 0 || win->floating != 0))
4595 /* user/key callable function IDs */
4645 KF_SEARCH_WORKSPACE,
4686 KF_DUMPWINS, /* MUST BE LAST */
4690 /* key definitions */
4692 char name[SWM_FUNCNAME_LEN];
4693 void (*func)(struct swm_region *r, union arg *);
4695 } keyfuncs[KF_INVALID + 1] = {
4696 /* name function argument */
4697 { "bar_toggle", bar_toggle, {0} },
4698 { "button2", pressbutton, {2} },
4699 { "cycle_layout", cycle_layout, {0} },
4700 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4701 { "float_toggle", floating_toggle,{0} },
4702 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4703 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4704 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4705 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4706 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4707 { "iconify", iconify, {0} },
4708 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4709 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4710 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4711 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4712 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4713 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4714 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4715 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4716 { "mvws_1", send_to_ws, {.id = 0} },
4717 { "mvws_2", send_to_ws, {.id = 1} },
4718 { "mvws_3", send_to_ws, {.id = 2} },
4719 { "mvws_4", send_to_ws, {.id = 3} },
4720 { "mvws_5", send_to_ws, {.id = 4} },
4721 { "mvws_6", send_to_ws, {.id = 5} },
4722 { "mvws_7", send_to_ws, {.id = 6} },
4723 { "mvws_8", send_to_ws, {.id = 7} },
4724 { "mvws_9", send_to_ws, {.id = 8} },
4725 { "mvws_10", send_to_ws, {.id = 9} },
4726 { "mvws_11", send_to_ws, {.id = 10} },
4727 { "mvws_12", send_to_ws, {.id = 11} },
4728 { "mvws_13", send_to_ws, {.id = 12} },
4729 { "mvws_14", send_to_ws, {.id = 13} },
4730 { "mvws_15", send_to_ws, {.id = 14} },
4731 { "mvws_16", send_to_ws, {.id = 15} },
4732 { "mvws_17", send_to_ws, {.id = 16} },
4733 { "mvws_18", send_to_ws, {.id = 17} },
4734 { "mvws_19", send_to_ws, {.id = 18} },
4735 { "mvws_20", send_to_ws, {.id = 19} },
4736 { "mvws_21", send_to_ws, {.id = 20} },
4737 { "mvws_22", send_to_ws, {.id = 21} },
4738 { "name_workspace", name_workspace, {0} },
4739 { "quit", quit, {0} },
4740 { "raise_toggle", raise_toggle, {0} },
4741 { "restart", restart, {0} },
4742 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4743 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4744 { "search_win", search_win, {0} },
4745 { "search_workspace", search_workspace, {0} },
4746 { "spawn_custom", NULL, {0} },
4747 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4748 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4749 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4750 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4751 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4752 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4753 { "uniconify", uniconify, {0} },
4754 { "version", version, {0} },
4755 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4756 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4757 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4758 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4759 { "ws_1", switchws, {.id = 0} },
4760 { "ws_2", switchws, {.id = 1} },
4761 { "ws_3", switchws, {.id = 2} },
4762 { "ws_4", switchws, {.id = 3} },
4763 { "ws_5", switchws, {.id = 4} },
4764 { "ws_6", switchws, {.id = 5} },
4765 { "ws_7", switchws, {.id = 6} },
4766 { "ws_8", switchws, {.id = 7} },
4767 { "ws_9", switchws, {.id = 8} },
4768 { "ws_10", switchws, {.id = 9} },
4769 { "ws_11", switchws, {.id = 10} },
4770 { "ws_12", switchws, {.id = 11} },
4771 { "ws_13", switchws, {.id = 12} },
4772 { "ws_14", switchws, {.id = 13} },
4773 { "ws_15", switchws, {.id = 14} },
4774 { "ws_16", switchws, {.id = 15} },
4775 { "ws_17", switchws, {.id = 16} },
4776 { "ws_18", switchws, {.id = 17} },
4777 { "ws_19", switchws, {.id = 18} },
4778 { "ws_20", switchws, {.id = 19} },
4779 { "ws_21", switchws, {.id = 20} },
4780 { "ws_22", switchws, {.id = 21} },
4781 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4782 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4783 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4784 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4785 { "ws_prior", priorws, {0} },
4786 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4787 { "invalid key func", NULL, {0} },
4790 RB_ENTRY(key) entry;
4793 enum keyfuncid funcid;
4796 RB_HEAD(key_tree, key);
4799 key_cmp(struct key *kp1, struct key *kp2)
4801 if (kp1->keysym < kp2->keysym)
4803 if (kp1->keysym > kp2->keysym)
4806 if (kp1->mod < kp2->mod)
4808 if (kp1->mod > kp2->mod)
4814 RB_GENERATE(key_tree, key, entry, key_cmp);
4815 struct key_tree keys;
4818 enum { client_click, root_click };
4820 unsigned int action;
4822 unsigned int button;
4823 void (*func)(struct ws_win *, union arg *);
4826 /* action key mouse button func args */
4827 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4828 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4829 { client_click, MODKEY, Button1, move, {0} },
4833 update_modkey(unsigned int mod)
4839 RB_FOREACH(kp, key_tree, &keys)
4840 if (kp->mod & ShiftMask)
4841 kp->mod = mod | ShiftMask;
4845 for (i = 0; i < LENGTH(buttons); i++)
4846 if (buttons[i].mask & ShiftMask)
4847 buttons[i].mask = mod | ShiftMask;
4849 buttons[i].mask = mod;
4854 TAILQ_ENTRY(spawn_prog) entry;
4859 TAILQ_HEAD(spawn_list, spawn_prog);
4860 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4863 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4866 struct spawn_prog *prog = NULL;
4868 char *ap, **real_args;
4870 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4873 TAILQ_FOREACH(prog, &spawns, entry) {
4874 if (!strcasecmp(spawn_name, prog->name))
4878 warnx("spawn_custom: program %s not found", spawn_name);
4882 /* make room for expanded args */
4883 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4884 err(1, "spawn_custom: calloc real_args");
4886 /* expand spawn_args into real_args */
4887 for (i = 0; i < prog->argc; i++) {
4889 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4890 if (!strcasecmp(ap, "$bar_border")) {
4892 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4894 err(1, "spawn_custom border color");
4895 } else if (!strcasecmp(ap, "$bar_color")) {
4897 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4899 err(1, "spawn_custom bar color");
4900 } else if (!strcasecmp(ap, "$bar_font")) {
4901 if ((real_args[i] = strdup(bar_fonts))
4903 err(1, "spawn_custom bar fonts");
4904 } else if (!strcasecmp(ap, "$bar_font_color")) {
4906 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4908 err(1, "spawn_custom color font");
4909 } else if (!strcasecmp(ap, "$color_focus")) {
4911 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4913 err(1, "spawn_custom color focus");
4914 } else if (!strcasecmp(ap, "$color_unfocus")) {
4916 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4918 err(1, "spawn_custom color unfocus");
4920 /* no match --> copy as is */
4921 if ((real_args[i] = strdup(ap)) == NULL)
4922 err(1, "spawn_custom strdup(ap)");
4924 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4929 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4930 for (i = 0; i < prog->argc; i++)
4931 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4932 DNPRINTF(SWM_D_SPAWN, "\n");
4934 *ret_args = real_args;
4935 return (prog->argc);
4939 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4945 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4949 spawn(r->ws->idx, &a, 1);
4951 for (i = 0; i < spawn_argc; i++)
4957 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4963 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4967 if (pipe(select_list_pipe) == -1)
4968 err(1, "pipe error");
4969 if (pipe(select_resp_pipe) == -1)
4970 err(1, "pipe error");
4972 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4973 err(1, "could not disable SIGPIPE");
4974 switch (*pid = fork()) {
4976 err(1, "cannot fork");
4979 if (dup2(select_list_pipe[0], 0) == -1)
4981 if (dup2(select_resp_pipe[1], 1) == -1)
4983 close(select_list_pipe[1]);
4984 close(select_resp_pipe[0]);
4985 spawn(r->ws->idx, &a, 0);
4987 default: /* parent */
4988 close(select_list_pipe[0]);
4989 close(select_resp_pipe[1]);
4993 for (i = 0; i < spawn_argc; i++)
4999 spawn_insert(char *name, char *args)
5001 char *arg, *cp, *ptr;
5002 struct spawn_prog *sp;
5004 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5006 if ((sp = calloc(1, sizeof *sp)) == NULL)
5007 err(1, "spawn_insert: calloc");
5008 if ((sp->name = strdup(name)) == NULL)
5009 err(1, "spawn_insert: strdup");
5011 /* convert the arguments to an argument list */
5012 if ((ptr = cp = strdup(args)) == NULL)
5013 err(1, "spawn_insert: strdup");
5014 while ((arg = strsep(&ptr, " \t")) != NULL) {
5015 /* empty field; skip it */
5020 if ((sp->argv = realloc(sp->argv, sp->argc *
5021 sizeof *sp->argv)) == NULL)
5022 err(1, "spawn_insert: realloc");
5023 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5024 err(1, "spawn_insert: strdup");
5028 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5029 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5033 spawn_remove(struct spawn_prog *sp)
5037 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5039 TAILQ_REMOVE(&spawns, sp, entry);
5040 for (i = 0; i < sp->argc; i++)
5046 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5050 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5052 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5055 spawn_insert(name, args);
5057 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5061 setspawn(char *name, char *args)
5063 struct spawn_prog *sp;
5065 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5070 TAILQ_FOREACH(sp, &spawns, entry) {
5071 if (!strcmp(sp->name, name)) {
5075 spawn_replace(sp, name, args);
5076 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5080 if (*args == '\0') {
5081 warnx("error: setspawn: cannot find program: %s", name);
5085 spawn_insert(name, args);
5086 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5090 setconfspawn(char *selector, char *value, int flags)
5092 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5094 setspawn(selector, value);
5096 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5103 setconfspawn("term", "xterm", 0);
5104 setconfspawn("spawn_term", "xterm", 0);
5105 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5106 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5107 setconfspawn("lock", "xlock", 0);
5108 setconfspawn("initscr", "initscreen.sh", 0);
5109 setconfspawn("menu", "dmenu_run"
5112 " -nf $bar_font_color"
5114 " -sf $bar_color", 0);
5115 setconfspawn("search", "dmenu"
5119 " -nf $bar_font_color"
5121 " -sf $bar_color", 0);
5122 setconfspawn("name_workspace", "dmenu"
5126 " -nf $bar_font_color"
5128 " -sf $bar_color", 0);
5132 #define SWM_MODNAME_SIZE 32
5133 #define SWM_KEY_WS "\n+ \t"
5135 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5139 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5140 if (mod == NULL || ks == NULL) {
5141 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5144 if (keystr == NULL || strlen(keystr) == 0) {
5145 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5149 *ks = XCB_NO_SYMBOL;
5151 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5152 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5154 cp += (long)strspn(cp, SWM_KEY_WS);
5155 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5157 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5159 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5161 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5163 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5165 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5167 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5168 *mod |= ControlMask;
5170 *ks = XStringToKeysym(name);
5171 XConvertCase(*ks, ks, &uks);
5172 if (ks == XCB_NO_SYMBOL) {
5174 "parsekeys: invalid key %s\n",
5180 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5185 strdupsafe(char *str)
5190 return (strdup(str));
5194 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5198 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5199 keyfuncs[kfid].name, spawn_name);
5201 if ((kp = malloc(sizeof *kp)) == NULL)
5202 err(1, "key_insert: malloc");
5207 kp->spawn_name = strdupsafe(spawn_name);
5208 RB_INSERT(key_tree, &keys, kp);
5210 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5214 key_lookup(unsigned int mod, KeySym ks)
5221 return (RB_FIND(key_tree, &keys, &kp));
5225 key_remove(struct key *kp)
5227 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5229 RB_REMOVE(key_tree, &keys, kp);
5230 free(kp->spawn_name);
5233 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5237 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5240 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5244 key_insert(mod, ks, kfid, spawn_name);
5246 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5250 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5255 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5256 keyfuncs[kfid].name, spawn_name);
5258 if ((kp = key_lookup(mod, ks)) != NULL) {
5259 if (kfid == KF_INVALID)
5262 key_replace(kp, mod, ks, kfid, spawn_name);
5263 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5266 if (kfid == KF_INVALID) {
5267 warnx("error: setkeybinding: cannot find mod/key combination");
5268 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5272 key_insert(mod, ks, kfid, spawn_name);
5273 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5277 setconfbinding(char *selector, char *value, int flags)
5279 enum keyfuncid kfid;
5282 struct spawn_prog *sp;
5283 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5284 if (selector == NULL) {
5285 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5286 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5288 setkeybinding(mod, ks, kfid, NULL);
5293 /* search by key function name */
5294 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5295 if (strncasecmp(selector, keyfuncs[kfid].name,
5296 SWM_FUNCNAME_LEN) == 0) {
5297 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5299 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5300 setkeybinding(mod, ks, kfid, NULL);
5306 /* search by custom spawn name */
5307 TAILQ_FOREACH(sp, &spawns, entry) {
5308 if (strcasecmp(selector, sp->name) == 0) {
5309 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5311 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5312 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5319 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5326 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5327 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5328 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5329 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5330 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5331 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5332 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5333 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5334 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5335 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5336 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5337 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5338 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5339 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5340 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5341 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5342 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5343 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5344 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5345 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5346 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5347 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5348 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5349 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5350 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5351 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5352 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5353 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5354 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5355 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5356 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5357 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5358 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5359 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5360 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5361 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5362 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5363 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5364 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5365 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5366 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5367 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5368 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5369 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5370 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5371 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5372 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5374 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5375 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5376 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5377 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5380 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5381 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5382 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5383 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5384 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5385 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5386 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5387 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5388 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5389 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5390 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5391 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5392 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5393 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5394 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5395 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5396 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5397 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5398 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5399 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5400 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5401 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5402 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5403 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5404 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5405 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5406 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5407 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5408 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5409 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5410 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5411 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5412 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5414 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5415 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5416 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5419 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5420 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5421 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5432 while (RB_EMPTY(&keys) == 0) {
5433 kp = RB_ROOT(&keys);
5439 setkeymapping(char *selector, char *value, int flags)
5441 char keymapping_file[PATH_MAX];
5442 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5443 if (value[0] == '~')
5444 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5445 pwd->pw_dir, &value[1]);
5447 strlcpy(keymapping_file, value, sizeof keymapping_file);
5449 /* load new key bindings; if it fails, revert to default bindings */
5450 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5454 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5459 updatenumlockmask(void)
5462 xcb_get_modifier_mapping_reply_t *modmap_r;
5463 xcb_keycode_t *modmap, kc;
5465 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5468 modmap_r = xcb_get_modifier_mapping_reply(conn,
5469 xcb_get_modifier_mapping(conn),
5472 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5473 for (i = 0; i < 8; i++) {
5474 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5475 kc = modmap[i * modmap_r->keycodes_per_modifier
5478 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5480 numlockmask = (1 << i);
5492 xcb_keycode_t *code;
5493 unsigned int modifiers[] =
5494 { 0, LockMask, numlockmask, numlockmask | LockMask };
5497 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5498 updatenumlockmask();
5500 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5501 for (k = 0; k < num_screens; k++) {
5502 if (TAILQ_EMPTY(&screens[k].rl))
5504 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5506 RB_FOREACH(kp, key_tree, &keys) {
5507 if ((code = xcb_key_symbols_get_keycode(syms,
5509 for (j = 0; j < LENGTH(modifiers); j++)
5510 xcb_grab_key(conn, True,
5512 kp->mod | modifiers[j],
5513 *code, XCB_GRAB_MODE_ASYNC,
5514 XCB_GRAB_MODE_ASYNC);
5520 grabbuttons(struct ws_win *win, int focused)
5523 unsigned int modifiers[] =
5524 { 0, LockMask, numlockmask, numlockmask|LockMask };
5526 updatenumlockmask();
5527 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5528 XCB_BUTTON_MASK_ANY);
5530 for (i = 0; i < LENGTH(buttons); i++)
5531 if (buttons[i].action == client_click)
5532 for (j = 0; j < LENGTH(modifiers); j++)
5533 xcb_grab_button(conn, False, win->id,
5535 XCB_GRAB_MODE_ASYNC,
5542 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5543 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5544 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5547 const char *quirkname[] = {
5548 "NONE", /* config string for "no value" */
5557 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5558 #define SWM_Q_WS "\n|+ \t"
5560 parsequirks(char *qstr, unsigned long *quirk)
5570 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5572 cp += (long)strspn(cp, SWM_Q_WS);
5573 for (i = 0; i < LENGTH(quirkname); i++) {
5574 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5575 DNPRINTF(SWM_D_QUIRK,
5576 "parsequirks: %s\n", name);
5581 *quirk |= 1 << (i-1);
5585 if (i >= LENGTH(quirkname)) {
5586 DNPRINTF(SWM_D_QUIRK,
5587 "parsequirks: invalid quirk [%s]\n", name);
5595 quirk_insert(const char *class, const char *name, unsigned long quirk)
5599 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5602 if ((qp = malloc(sizeof *qp)) == NULL)
5603 err(1, "quirk_insert: malloc");
5604 if ((qp->class = strdup(class)) == NULL)
5605 err(1, "quirk_insert: strdup");
5606 if ((qp->name = strdup(name)) == NULL)
5607 err(1, "quirk_insert: strdup");
5610 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5612 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5616 quirk_remove(struct quirk *qp)
5618 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5619 qp->name, qp->quirk);
5621 TAILQ_REMOVE(&quirks, qp, entry);
5626 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5630 quirk_replace(struct quirk *qp, const char *class, const char *name,
5631 unsigned long quirk)
5633 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5634 qp->name, qp->quirk);
5637 quirk_insert(class, name, quirk);
5639 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5643 setquirk(const char *class, const char *name, unsigned long quirk)
5647 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5650 TAILQ_FOREACH(qp, &quirks, entry) {
5651 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5655 quirk_replace(qp, class, name, quirk);
5656 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5661 warnx("error: setquirk: cannot find class/name combination");
5665 quirk_insert(class, name, quirk);
5666 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5670 setconfquirk(char *selector, char *value, int flags)
5672 char *cp, *class, *name;
5674 unsigned long quirks;
5675 if (selector == NULL)
5677 if ((cp = strchr(selector, ':')) == NULL)
5682 if ((retval = parsequirks(value, &quirks)) == 0)
5683 setquirk(class, name, quirks);
5690 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5691 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5692 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5693 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5694 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5695 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5696 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5697 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5698 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5699 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5700 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5701 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5702 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5705 /* conf file stuff */
5706 #define SWM_CONF_FILE "spectrwm.conf"
5707 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5711 SWM_S_BAR_AT_BOTTOM,
5712 SWM_S_BAR_BORDER_WIDTH,
5719 SWM_S_CLOCK_ENABLED,
5722 SWM_S_CYCLE_VISIBLE,
5724 SWM_S_DISABLE_BORDER,
5726 SWM_S_FOCUS_CLOSE_WRAP,
5727 SWM_S_FOCUS_DEFAULT,
5733 SWM_S_STACK_ENABLED,
5735 SWM_S_TITLE_CLASS_ENABLED,
5736 SWM_S_TITLE_NAME_ENABLED,
5737 SWM_S_URGENT_ENABLED,
5738 SWM_S_VERBOSE_LAYOUT,
5739 SWM_S_WINDOW_NAME_ENABLED,
5740 SWM_S_WORKSPACE_LIMIT
5744 setconfvalue(char *selector, char *value, int flags)
5750 case SWM_S_BAR_ACTION:
5752 if ((bar_argv[0] = strdup(value)) == NULL)
5753 err(1, "setconfvalue: bar_action");
5755 case SWM_S_BAR_AT_BOTTOM:
5756 bar_at_bottom = atoi(value);
5758 case SWM_S_BAR_BORDER_WIDTH:
5759 bar_border_width = atoi(value);
5760 if (bar_border_width < 0)
5761 bar_border_width = 0;
5763 case SWM_S_BAR_DELAY:
5764 bar_delay = atoi(value);
5766 case SWM_S_BAR_ENABLED:
5767 bar_enabled = atoi(value);
5769 case SWM_S_BAR_FONT:
5771 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5772 err(1, "setconfvalue: asprintf: failed to allocate "
5773 "memory for bar_fonts.");
5776 case SWM_S_BAR_FORMAT:
5778 if ((bar_format = strdup(value)) == NULL)
5779 err(1, "setconfvalue: bar_format");
5781 case SWM_S_BAR_JUSTIFY:
5782 if (!strcmp(value, "left"))
5783 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5784 else if (!strcmp(value, "center"))
5785 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5786 else if (!strcmp(value, "right"))
5787 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5789 errx(1, "invalid bar_justify");
5791 case SWM_S_BORDER_WIDTH:
5792 border_width = atoi(value);
5793 if (border_width < 0)
5796 case SWM_S_CLOCK_ENABLED:
5797 clock_enabled = atoi(value);
5799 case SWM_S_CLOCK_FORMAT:
5800 #ifndef SWM_DENY_CLOCK_FORMAT
5802 if ((clock_format = strdup(value)) == NULL)
5803 err(1, "setconfvalue: clock_format");
5806 case SWM_S_CYCLE_EMPTY:
5807 cycle_empty = atoi(value);
5809 case SWM_S_CYCLE_VISIBLE:
5810 cycle_visible = atoi(value);
5812 case SWM_S_DIALOG_RATIO:
5813 dialog_ratio = atof(value);
5814 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5817 case SWM_S_DISABLE_BORDER:
5818 disable_border = atoi(value);
5820 case SWM_S_FOCUS_CLOSE:
5821 if (!strcmp(value, "first"))
5822 focus_close = SWM_STACK_BOTTOM;
5823 else if (!strcmp(value, "last"))
5824 focus_close = SWM_STACK_TOP;
5825 else if (!strcmp(value, "next"))
5826 focus_close = SWM_STACK_ABOVE;
5827 else if (!strcmp(value, "previous"))
5828 focus_close = SWM_STACK_BELOW;
5830 errx(1, "focus_close");
5832 case SWM_S_FOCUS_CLOSE_WRAP:
5833 focus_close_wrap = atoi(value);
5835 case SWM_S_FOCUS_DEFAULT:
5836 if (!strcmp(value, "last"))
5837 focus_default = SWM_STACK_TOP;
5838 else if (!strcmp(value, "first"))
5839 focus_default = SWM_STACK_BOTTOM;
5841 errx(1, "focus_default");
5843 case SWM_S_FOCUS_MODE:
5844 if (!strcmp(value, "default"))
5845 focus_mode = SWM_FOCUS_DEFAULT;
5846 else if (!strcmp(value, "follow_cursor"))
5847 focus_mode = SWM_FOCUS_FOLLOW;
5848 else if (!strcmp(value, "synergy"))
5849 focus_mode = SWM_FOCUS_SYNERGY;
5851 errx(1, "focus_mode");
5853 case SWM_S_SPAWN_ORDER:
5854 if (!strcmp(value, "first"))
5855 spawn_position = SWM_STACK_BOTTOM;
5856 else if (!strcmp(value, "last"))
5857 spawn_position = SWM_STACK_TOP;
5858 else if (!strcmp(value, "next"))
5859 spawn_position = SWM_STACK_ABOVE;
5860 else if (!strcmp(value, "previous"))
5861 spawn_position = SWM_STACK_BELOW;
5863 errx(1, "spawn_position");
5865 case SWM_S_SPAWN_TERM:
5866 setconfspawn("term", value, 0);
5867 setconfspawn("spawn_term", value, 0);
5871 case SWM_S_SS_ENABLED:
5872 ss_enabled = atoi(value);
5874 case SWM_S_STACK_ENABLED:
5875 stack_enabled = atoi(value);
5877 case SWM_S_TERM_WIDTH:
5878 term_width = atoi(value);
5882 case SWM_S_TITLE_CLASS_ENABLED:
5883 title_class_enabled = atoi(value);
5885 case SWM_S_TITLE_NAME_ENABLED:
5886 title_name_enabled = atoi(value);
5888 case SWM_S_URGENT_ENABLED:
5889 urgent_enabled = atoi(value);
5891 case SWM_S_VERBOSE_LAYOUT:
5892 verbose_layout = atoi(value);
5893 for (i = 0; layouts[i].l_stack != NULL; i++) {
5895 layouts[i].l_string = fancy_stacker;
5897 layouts[i].l_string = plain_stacker;
5900 case SWM_S_WINDOW_NAME_ENABLED:
5901 window_name_enabled = atoi(value);
5903 case SWM_S_WORKSPACE_LIMIT:
5904 workspace_limit = atoi(value);
5905 if (workspace_limit > SWM_WS_MAX)
5906 workspace_limit = SWM_WS_MAX;
5907 else if (workspace_limit < 1)
5908 workspace_limit = 1;
5917 setconfmodkey(char *selector, char *value, int flags)
5919 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5920 update_modkey(Mod1Mask);
5921 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5922 update_modkey(Mod2Mask);
5923 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5924 update_modkey(Mod3Mask);
5925 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5926 update_modkey(Mod4Mask);
5933 setconfcolor(char *selector, char *value, int flags)
5935 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5940 setconfregion(char *selector, char *value, int flags)
5942 custom_region(value);
5947 setautorun(char *selector, char *value, int flags)
5957 if (getenv("SWM_STARTED"))
5961 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5962 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5964 if (ws_id < 0 || ws_id >= workspace_limit)
5965 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5968 * This is a little intricate
5970 * If the pid already exists we simply reuse it because it means it was
5971 * used before AND not claimed by manage_window. We get away with
5972 * altering it in the parent after INSERT because this can not be a race
5975 while ((ap = strsep(&sp, " \t")) != NULL) {
5978 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5980 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5981 err(1, "setautorun: realloc");
5982 a.argv[argc - 1] = ap;
5985 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5986 err(1, "setautorun: realloc");
5987 a.argv[argc] = NULL;
5989 if ((pid = fork()) == 0) {
5990 spawn(ws_id, &a, 1);
5999 p = calloc(1, sizeof *p);
6002 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6012 setlayout(char *selector, char *value, int flags)
6014 int ws_id, i, x, mg, ma, si, raise, f = 0;
6015 int st = SWM_V_STACK, num_screens;
6017 struct workspace *ws;
6019 if (getenv("SWM_STARTED"))
6023 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6024 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6025 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6026 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6029 if (ws_id < 0 || ws_id >= workspace_limit)
6030 errx(1, "layout: invalid workspace %d", ws_id + 1);
6032 if (!strcasecmp(s, "vertical"))
6034 else if (!strcasecmp(s, "vertical_flip")) {
6037 } else if (!strcasecmp(s, "horizontal"))
6039 else if (!strcasecmp(s, "horizontal_flip")) {
6042 } else if (!strcasecmp(s, "fullscreen"))
6045 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6046 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6049 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6050 for (i = 0; i < num_screens; i++) {
6051 ws = (struct workspace *)&screens[i].ws;
6052 ws[ws_id].cur_layout = &layouts[st];
6054 ws[ws_id].always_raise = raise;
6055 if (st == SWM_MAX_STACK)
6059 for (x = 0; x < abs(mg); x++) {
6060 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6061 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6062 SWM_ARG_ID_MASTERSHRINK);
6066 for (x = 0; x < abs(ma); x++) {
6067 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6068 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6069 SWM_ARG_ID_MASTERDEL);
6073 for (x = 0; x < abs(si); x++) {
6074 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6075 si >= 0 ? SWM_ARG_ID_STACKINC :
6076 SWM_ARG_ID_STACKDEC);
6081 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6082 SWM_ARG_ID_FLIPLAYOUT);
6090 /* config options */
6091 struct config_option {
6093 int (*func)(char*, char*, int);
6096 struct config_option configopt[] = {
6097 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6098 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6099 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6100 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6101 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6102 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6103 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6104 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6105 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6106 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6107 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6108 { "keyboard_mapping", setkeymapping, 0 },
6109 { "bind", setconfbinding, 0 },
6110 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6111 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6112 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6113 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6114 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6115 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6116 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6117 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6118 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6119 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6120 { "modkey", setconfmodkey, 0 },
6121 { "program", setconfspawn, 0 },
6122 { "quirk", setconfquirk, 0 },
6123 { "region", setconfregion, 0 },
6124 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6125 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6126 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6127 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6128 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6129 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6130 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6131 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6132 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6133 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6134 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6135 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6136 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6137 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6138 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6139 { "autorun", setautorun, 0 },
6140 { "layout", setlayout, 0 },
6145 conf_load(char *filename, int keymapping)
6148 char *line, *cp, *optsub, *optval;
6149 size_t linelen, lineno = 0;
6150 int wordlen, i, optind;
6151 struct config_option *opt;
6153 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6155 if (filename == NULL) {
6156 warnx("conf_load: no filename");
6159 if ((config = fopen(filename, "r")) == NULL) {
6160 warn("conf_load: fopen: %s", filename);
6164 while (!feof(config)) {
6165 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6168 err(1, "%s", filename);
6173 cp += strspn(cp, " \t\n"); /* eat whitespace */
6174 if (cp[0] == '\0') {
6179 /* get config option */
6180 wordlen = strcspn(cp, "=[ \t\n");
6182 warnx("%s: line %zd: no option found",
6187 for (i = 0; i < LENGTH(configopt); i++) {
6188 opt = &configopt[i];
6189 if (!strncasecmp(cp, opt->optname, wordlen) &&
6190 (int)strlen(opt->optname) == wordlen) {
6196 warnx("%s: line %zd: unknown option %.*s",
6197 filename, lineno, wordlen, cp);
6200 if (keymapping && strcmp(opt->optname, "bind")) {
6201 warnx("%s: line %zd: invalid option %.*s",
6202 filename, lineno, wordlen, cp);
6206 cp += strspn(cp, " \t\n"); /* eat whitespace */
6207 /* get [selector] if any */
6211 wordlen = strcspn(cp, "]");
6214 warnx("%s: line %zd: syntax error",
6219 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6221 warnx("%s: line %zd: unable to allocate"
6222 "memory for selector", filename,
6228 cp += strspn(cp, "] \t\n"); /* eat trailing */
6230 cp += strspn(cp, "= \t\n"); /* eat trailing */
6232 optval = strdup(cp);
6233 /* call function to deal with it all */
6234 if (configopt[optind].func(optsub, optval,
6235 configopt[optind].funcflags) != 0)
6236 errx(1, "%s: line %zd: invalid data for %s",
6237 filename, lineno, configopt[optind].optname);
6244 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6251 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6257 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6259 struct ws_win *parent, *w;
6260 struct swm_region *r;
6261 struct workspace *ws;
6262 xcb_icccm_wm_hints_t wmh;
6264 parent = find_window(win->transient);
6266 parent->child_trans = win;
6268 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6269 " for 0x%x trans 0x%x\n", win->id, win->transient);
6271 r = root_to_region(win->wa->root);
6273 /* parent doen't exist in our window list */
6274 TAILQ_FOREACH(w, &ws->winlist, entry) {
6275 if (xcb_icccm_get_wm_hints_reply(conn,
6276 xcb_icccm_get_wm_hints(conn, w->id),
6278 warnx("can't get hints for 0x%x", w->id);
6282 if (win->hints.window_group != wmh.window_group)
6285 w->child_trans = win;
6286 win->transient = w->id;
6288 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6289 "transient to 0x%x\n", win->transient);
6296 window_get_pid(xcb_window_t win)
6301 xcb_get_property_cookie_t pc;
6302 xcb_get_property_reply_t *pr;
6304 apid = get_atom_from_string("_NET_WM_PID");
6305 if (apid == XCB_ATOM_NONE)
6308 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6309 pr = xcb_get_property_reply(conn, pc, NULL);
6312 if (pr->type != XCB_ATOM_CARDINAL) {
6317 ret = *((pid_t *)xcb_get_property_value(pr));
6323 apid = get_atom_from_string("_SWM_PID");
6324 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6326 pr = xcb_get_property_reply(conn, pc, NULL);
6329 if (pr->type != XCB_ATOM_STRING) {
6334 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6341 manage_window(xcb_window_t id)
6343 xcb_window_t trans = XCB_WINDOW_NONE;
6344 struct workspace *ws;
6345 struct ws_win *win, *ww;
6346 int ws_idx, border_me = 0;
6347 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6348 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6350 struct swm_region *r;
6354 uint32_t event_mask, i;
6356 xcb_get_property_reply_t *gpr;
6357 xcb_icccm_get_wm_protocols_reply_t wpr;
6359 if ((win = find_window(id)) != NULL)
6360 return (win); /* already being managed */
6362 /* see if we are on the unmanaged list */
6363 if ((win = find_unmanaged_window(id)) != NULL) {
6364 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6365 "window: 0x%x\n", win->id);
6366 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6368 set_child_transient(win, &trans);
6370 if (trans && (ww = find_window(trans)))
6371 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6372 else if ((ww = win->ws->focus) &&
6373 spawn_position == SWM_STACK_ABOVE)
6374 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6376 else if (ww && spawn_position == SWM_STACK_BELOW)
6377 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6378 else switch (spawn_position) {
6381 case SWM_STACK_ABOVE:
6382 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6384 case SWM_STACK_BOTTOM:
6385 case SWM_STACK_BELOW:
6386 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6389 ewmh_update_actions(win);
6393 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6394 err(1, "manage_window: calloc: failed to allocate memory for "
6400 /* see if we need to override the workspace */
6401 p = find_pid(window_get_pid(id));
6403 /* Get all the window data in one shot */
6404 ws_idx_atom = get_atom_from_string("_SWM_WS");
6406 gpr = xcb_get_property_reply(conn,
6407 xcb_get_property(conn, False, id, ws_idx_atom,
6408 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6411 proplen = xcb_get_property_value_length(gpr);
6413 prop = malloc(proplen + 1);
6416 xcb_get_property_value(gpr),
6418 prop[proplen] = '\0';
6424 win->wa = xcb_get_geometry_reply(conn,
6425 xcb_get_geometry(conn, id),
6427 xcb_icccm_get_wm_normal_hints_reply(conn,
6428 xcb_icccm_get_wm_normal_hints(conn, id),
6430 xcb_icccm_get_wm_hints_reply(conn,
6431 xcb_icccm_get_wm_hints(conn, id),
6433 xcb_icccm_get_wm_transient_for_reply(conn,
6434 xcb_icccm_get_wm_transient_for(conn, id),
6437 win->transient = trans;
6438 set_child_transient(win, &trans);
6439 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6440 "transient: 0x%x\n", win->id, win->transient);
6443 prot = get_atom_from_string("WM_PROTOCOLS");
6445 /* get supported protocols */
6446 if (xcb_icccm_get_wm_protocols_reply(conn,
6447 xcb_icccm_get_wm_protocols(conn, id, prot),
6449 for (i = 0; i < wpr.atoms_len; i++) {
6450 if (wpr.atoms[i] == a_takefocus)
6451 win->take_focus = 1;
6452 if (wpr.atoms[i] == a_delete)
6453 win->can_delete = 1;
6455 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6458 win->iconic = get_iconic(win);
6461 * Figure out where to put the window. If it was previously assigned to
6462 * a workspace (either by spawn() or manually moving), and isn't
6463 * transient, * put it in the same workspace
6465 r = root_to_region(win->wa->root);
6467 ws = &r->s->ws[p->ws];
6468 TAILQ_REMOVE(&pidlist, p, entry);
6471 } else if (prop && win->transient == 0) {
6472 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6473 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1,
6476 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6479 ws = &r->s->ws[ws_idx];
6482 /* this should launch transients in the same ws as parent */
6484 if ((ww = find_window(trans)) != NULL)
6490 warnx("manage_window: fix this "
6496 /* set up the window layout */
6499 win->s = r->s; /* this never changes */
6500 if (trans && (ww = find_window(trans)))
6501 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6502 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6503 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6505 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6506 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6507 else switch (spawn_position) {
6510 case SWM_STACK_ABOVE:
6511 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6513 case SWM_STACK_BOTTOM:
6514 case SWM_STACK_BELOW:
6515 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6518 /* ignore window border if there is one. */
6519 WIDTH(win) = win->wa->width;
6520 HEIGHT(win) = win->wa->height;
6521 X(win) = win->wa->x + win->wa->border_width;
6522 Y(win) = win->wa->y + win->wa->border_width;
6524 win->g_floatvalid = 0;
6525 win->floatmaxed = 0;
6526 win->ewmh_flags = 0;
6528 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6529 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6530 HEIGHT(win), ws->idx);
6532 constrain_window(win, r, 0);
6534 /* Set window properties so we can remember this after reincarnation */
6535 if (ws_idx_atom && prop == NULL &&
6536 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6538 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6540 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6541 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6547 ewmh_autoquirk(win);
6549 if (xcb_icccm_get_wm_class_reply(conn,
6550 xcb_icccm_get_wm_class(conn, win->id),
6552 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6553 win->ch.class_name, win->ch.instance_name);
6555 /* java is retarded so treat it special */
6556 if (strstr(win->ch.instance_name, "sun-awt")) {
6561 TAILQ_FOREACH(qp, &quirks, entry) {
6562 if (!strcmp(win->ch.class_name, qp->class) &&
6563 !strcmp(win->ch.instance_name, qp->name)) {
6564 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6565 "class: %s, name: %s\n", win->ch.class_name,
6566 win->ch.instance_name);
6567 if (qp->quirk & SWM_Q_FLOAT) {
6571 win->quirks = qp->quirk;
6576 /* alter window position if quirky */
6577 if (win->quirks & SWM_Q_ANYWHERE) {
6578 win->manual = 1; /* don't center the quirky windows */
6579 if (bar_enabled && Y(win) < bar_height)
6580 Y(win) = bar_height;
6581 if (WIDTH(win) + X(win) > WIDTH(r))
6582 X(win) = WIDTH(r) - WIDTH(win) - 2;
6586 /* Reset font sizes (the bruteforce way; no default keybinding). */
6587 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6588 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6589 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6590 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6591 fake_keypress(win, XK_KP_Add, ShiftMask);
6594 ewmh_get_win_state(win);
6595 ewmh_update_actions(win);
6596 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6601 X(win) -= border_width;
6602 Y(win) -= border_width;
6606 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6607 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6609 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6611 /* floaters need to be mapped if they are in the current workspace */
6612 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6613 map_window_raised(win->id);
6619 free_window(struct ws_win *win)
6621 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6626 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6631 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6636 memset(win, 0xff, sizeof *win); /* XXX kill later */
6639 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6643 unmanage_window(struct ws_win *win)
6645 struct ws_win *parent;
6646 xcb_screen_t *screen;
6651 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6653 if (win->transient) {
6654 parent = find_window(win->transient);
6656 parent->child_trans = NULL;
6659 /* focus on root just in case */
6660 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6661 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6662 screen->root, XCB_CURRENT_TIME);
6666 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6667 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6671 focus_magic(struct ws_win *win)
6673 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6676 /* if there are no windows clear the status-bar */
6681 if (win->child_trans) {
6682 /* win = parent & has a transient so focus on that */
6684 focus_win(win->child_trans);
6685 if (win->child_trans->take_focus)
6686 client_msg(win, a_takefocus);
6688 /* make sure transient hasn't disappeared */
6689 if (validate_win(win->child_trans) == 0) {
6690 focus_win(win->child_trans);
6691 if (win->child_trans->take_focus)
6692 client_msg(win->child_trans,
6695 win->child_trans = NULL;
6697 if (win->take_focus)
6698 client_msg(win, a_takefocus);
6704 if (win->take_focus)
6705 client_msg(win, a_takefocus);
6710 expose(xcb_expose_event_t *e)
6713 struct swm_region *r;
6715 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6717 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6718 for (i = 0; i < num_screens; i++)
6719 TAILQ_FOREACH(r, &screens[i].rl, entry)
6720 if (e->window == WINID(r->bar))
6727 keypress(xcb_key_press_event_t *e)
6729 xcb_keysym_t keysym;
6732 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6734 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6736 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6739 if (kp->funcid == KF_SPAWN_CUSTOM)
6740 spawn_custom(root_to_region(e->root),
6741 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6742 else if (keyfuncs[kp->funcid].func)
6743 keyfuncs[kp->funcid].func(root_to_region(e->root),
6744 &(keyfuncs[kp->funcid].args));
6748 buttonpress(xcb_button_press_event_t *e)
6752 unsigned int action;
6754 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6755 e->event, e->detail);
6757 if ((win = find_window(e->event)) == NULL)
6761 action = client_click;
6763 for (i = 0; i < LENGTH(buttons); i++)
6764 if (action == buttons[i].action && buttons[i].func &&
6765 buttons[i].button == e->detail &&
6766 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6767 buttons[i].func(win, &buttons[i].args);
6774 print_win_geom(xcb_window_t w)
6776 xcb_get_geometry_reply_t *wa;
6778 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
6780 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
6781 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6782 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
6790 configurerequest(xcb_configure_request_event_t *e)
6795 uint32_t wc[7] = {0};
6797 if ((win = find_window(e->window)) == NULL)
6798 if ((win = find_unmanaged_window(e->window)) == NULL)
6801 print_win_geom(e->window);
6804 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6805 "value_mask: 0x%x", e->window, e->value_mask);
6806 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6807 mask |= XCB_CONFIG_WINDOW_X;
6809 DPRINTF(", X: %d", e->x);
6811 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6812 mask |= XCB_CONFIG_WINDOW_Y;
6814 DPRINTF(", Y: %d", e->y);
6816 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6817 mask |= XCB_CONFIG_WINDOW_WIDTH;
6819 DPRINTF(", W: %u", e->width);
6821 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6822 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6823 wc[i++] = e->height;
6824 DPRINTF(", H: %u", e->height);
6826 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6827 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6828 wc[i++] = e->border_width;
6829 DPRINTF(", Border: %u", e->border_width);
6831 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6832 mask |= XCB_CONFIG_WINDOW_SIBLING;
6833 wc[i++] = e->sibling;
6834 DPRINTF(", Sibling: 0x%x", e->sibling);
6836 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6837 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6838 wc[i++] = e->stack_mode;
6839 DPRINTF(", StackMode: %u", e->stack_mode);
6843 xcb_configure_window(conn, e->window, mask, wc);
6845 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
6846 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6847 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6848 win->g_float.x = e->x - X(win->ws->r);
6849 win->g_float.y = e->y - Y(win->ws->r);
6850 win->g_float.w = e->width;
6851 win->g_float.h = e->height;
6852 win->g_floatvalid = 1;
6854 if (win->floating) {
6855 win->g = win->g_float;
6856 win->g.x += X(win->ws->r);
6857 win->g.y += Y(win->ws->r);
6870 configurenotify(xcb_configure_notify_event_t *e)
6874 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6877 win = find_window(e->window);
6879 xcb_icccm_get_wm_normal_hints_reply(conn,
6880 xcb_icccm_get_wm_normal_hints(conn, win->id),
6889 destroynotify(xcb_destroy_notify_event_t *e)
6893 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6895 if ((win = find_window(e->window)) == NULL) {
6896 if ((win = find_unmanaged_window(e->window)) == NULL)
6902 /* make sure we focus on something */
6905 unmanage_window(win);
6911 enternotify(xcb_enter_notify_event_t *e)
6914 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6915 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6916 "state: %d\n", e->event, e->mode, e->detail, e->root,
6917 e->child, YESNO(e->same_screen_focus), e->state);
6919 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6920 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6925 switch (focus_mode) {
6926 case SWM_FOCUS_DEFAULT:
6928 case SWM_FOCUS_FOLLOW:
6930 case SWM_FOCUS_SYNERGY:
6934 if ((win = find_window(e->event)) == NULL) {
6935 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6944 /* lets us use one switch statement for arbitrary mode/detail combinations */
6945 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6948 mapnotify(xcb_map_notify_event_t *e)
6952 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
6954 win = manage_window(e->window);
6956 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
6959 * focus_win can only set input focus on a mapped window.
6960 * make sure the window really has focus since it is just being mapped.
6962 if (win->ws->focus == win)
6967 mappingnotify(xcb_mapping_notify_event_t *e)
6969 xcb_refresh_keyboard_mapping(syms, e);
6971 if (e->request == XCB_MAPPING_KEYBOARD)
6976 maprequest(xcb_map_request_event_t *e)
6979 struct swm_region *r;
6980 xcb_get_window_attributes_reply_t *war;
6982 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
6985 war = xcb_get_window_attributes_reply(conn,
6986 xcb_get_window_attributes(conn, e->window),
6990 if (war->override_redirect) {
6996 win = manage_window(e->window);
6998 return; /* can't happen */
7003 /* make new win focused */
7004 r = root_to_region(win->wa->root);
7005 if (win->ws == r->ws)
7011 get_atom_name(xcb_atom_t atom)
7015 xcb_get_atom_name_reply_t *r;
7016 xcb_map_request_event_t mre;
7018 r = xcb_get_atom_name_reply(conn,
7019 xcb_get_atom_name(conn, atom),
7022 len = xcb_get_atom_name_name_length(r);
7024 name = malloc(len + 1);
7026 memcpy(name, xcb_get_atom_name_name(r), len);
7040 propertynotify(xcb_property_notify_event_t *e)
7046 name = get_atom_name(e->atom);
7047 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7048 e->window, name, e->atom);
7052 win = find_window(e->window);
7056 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7057 update_iconic(win, 0);
7058 map_window_raised(win->id);
7065 case XCB_ATOM_WM_CLASS:
7066 case XCB_ATOM_WM_NAME:
7075 unmapnotify(xcb_unmap_notify_event_t *e)
7079 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7081 /* determine if we need to help unmanage this window */
7082 win = find_window(e->window);
7086 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7087 unmanage_window(win);
7090 /* resend unmap because we ated it */
7091 xcb_unmap_window(conn, e->window);
7098 visibilitynotify(xcb_visibility_notify_event_t *e)
7100 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7106 clientmessage(xcb_client_message_event_t *e)
7109 xcb_map_request_event_t mre;
7113 name = get_atom_name(e->type);
7114 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7115 e->window, name, e->type);
7118 win = find_window(e->window);
7121 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7122 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7123 "unmanaged window.\n");
7124 mre.window = e->window;
7130 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7131 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7134 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7135 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7136 if (win->can_delete)
7137 client_msg(win, a_delete);
7139 xcb_kill_client(conn, win->id);
7141 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7142 DNPRINTF(SWM_D_EVENT,
7143 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7144 if (win->floating) {
7145 if (e->data.data32[0] & (1<<8)) /* x */
7146 X(win) = e->data.data32[1];
7147 if (e->data.data32[0] & (1<<9)) /* y */
7148 Y(win) = e->data.data32[2];
7149 if (e->data.data32[0] & (1<<10)) /* width */
7150 WIDTH(win) = e->data.data32[3];
7151 if (e->data.data32[0] & (1<<11)) /* height */
7152 HEIGHT(win) = e->data.data32[4];
7157 /* TODO: Change stack sizes */
7158 /* notify no change was made. */
7159 config_win(win, NULL);
7162 if (e->type == ewmh[_NET_WM_STATE].atom) {
7163 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7164 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7165 if (e->data.data32[2])
7166 ewmh_update_win_state(win, e->data.data32[2],
7179 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7181 xcb_void_cookie_t wac;
7182 xcb_generic_error_t *error;
7184 /* this causes an error if some other window manager is running */
7185 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7186 for (i = 0; i < num_screens; i++) {
7188 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7190 wac = xcb_change_window_attributes_checked(conn, sc->root,
7191 XCB_CW_EVENT_MASK, &val);
7192 if ((error = xcb_request_check(conn, wac))) {
7193 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7204 new_region(struct swm_screen *s, int x, int y, int w, int h)
7206 struct swm_region *r, *n;
7207 struct workspace *ws = NULL;
7210 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7211 s->idx, w, h, x, y);
7213 /* remove any conflicting regions */
7214 n = TAILQ_FIRST(&s->rl);
7217 n = TAILQ_NEXT(r, entry);
7218 if (X(r) < (x + w) &&
7219 (X(r) + WIDTH(r)) > x &&
7221 (Y(r) + HEIGHT(r)) > y) {
7222 if (r->ws->r != NULL)
7223 r->ws->old_r = r->ws->r;
7226 TAILQ_REMOVE(&s->rl, r, entry);
7227 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7231 /* search old regions for one to reuse */
7233 /* size + location match */
7234 TAILQ_FOREACH(r, &s->orl, entry)
7235 if (X(r) == x && Y(r) == y &&
7236 HEIGHT(r) == h && WIDTH(r) == w)
7240 TAILQ_FOREACH(r, &s->orl, entry)
7241 if (HEIGHT(r) == h && WIDTH(r) == w)
7245 TAILQ_REMOVE(&s->orl, r, entry);
7246 /* try to use old region's workspace */
7247 if (r->ws->r == NULL)
7250 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7251 err(1, "new_region: calloc: failed to allocate memory "
7254 /* if we don't have a workspace already, find one */
7256 for (i = 0; i < workspace_limit; i++)
7257 if (s->ws[i].r == NULL) {
7264 errx(1, "new_region: no free workspaces");
7275 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7281 #ifdef SWM_XRR_HAS_CRTC
7284 #endif /* SWM_XRR_HAS_CRTC */
7285 struct swm_region *r;
7287 xcb_randr_get_screen_resources_current_cookie_t src;
7288 xcb_randr_get_screen_resources_current_reply_t *srr;
7289 xcb_randr_get_crtc_info_cookie_t cic;
7290 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7291 xcb_randr_crtc_t *crtc;
7292 xcb_screen_t *screen = get_screen(i);
7294 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7295 if (i >= num_screens)
7296 errx(1, "scan_xrandr: invalid screen");
7298 /* remove any old regions */
7299 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7300 r->ws->old_r = r->ws->r = NULL;
7302 TAILQ_REMOVE(&screens[i].rl, r, entry);
7303 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7307 /* map virtual screens onto physical screens */
7308 #ifdef SWM_XRR_HAS_CRTC
7309 if (xrandr_support) {
7310 src = xcb_randr_get_screen_resources_current(conn,
7312 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7315 new_region(&screens[i], 0, 0,
7316 screen->width_in_pixels,
7317 screen->height_in_pixels);
7320 ncrtc = srr->num_crtcs;
7321 for (c = 0; c < ncrtc; c++) {
7322 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7323 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7325 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7328 if (cir->num_outputs == 0) {
7334 new_region(&screens[i], 0, 0,
7335 screen->width_in_pixels,
7336 screen->height_in_pixels);
7338 new_region(&screens[i],
7339 cir->x, cir->y, cir->width, cir->height);
7344 #endif /* SWM_XRR_HAS_CRTC */
7346 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7347 screen->height_in_pixels);
7352 screenchange(xcb_randr_screen_change_notify_event_t *e)
7354 struct swm_region *r;
7357 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7359 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7360 /* silly event doesn't include the screen index */
7361 for (i = 0; i < num_screens; i++)
7362 if (screens[i].root == e->root)
7364 if (i >= num_screens)
7365 errx(1, "screenchange: screen not found");
7367 /* brute force for now, just re-enumerate the regions */
7371 print_win_geom(e->root);
7373 /* add bars to all regions */
7374 for (i = 0; i < num_screens; i++)
7375 TAILQ_FOREACH(r, &screens[i].rl, entry)
7383 xcb_window_t *wins = NULL;
7385 int i, j, num_screens;
7386 uint16_t state, manage;
7388 xcb_query_tree_cookie_t qtc;
7389 xcb_query_tree_reply_t *qtr;
7390 xcb_get_window_attributes_cookie_t c;
7391 xcb_get_window_attributes_reply_t *r;
7392 xcb_get_property_cookie_t pc;
7394 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7395 for (i = 0; i < num_screens; i++) {
7396 qtc = xcb_query_tree(conn, screens[i].root);
7397 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7400 wins = xcb_query_tree_children(qtr);
7401 no = xcb_query_tree_children_length(qtr);
7402 /* attach windows to a region */
7403 /* normal windows */
7404 for (j = 0; j < no; j++) {
7405 c = xcb_get_window_attributes(conn, wins[j]);
7406 r = xcb_get_window_attributes_reply(conn, c, NULL);
7409 if (r->override_redirect) {
7414 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7415 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7421 state = getstate(wins[j]);
7422 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7423 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7424 manage_window(wins[j]);
7427 /* transient windows */
7428 for (j = 0; j < no; j++) {
7429 c = xcb_get_window_attributes(conn, wins[j]);
7430 r = xcb_get_window_attributes_reply(conn, c, NULL);
7433 if (r->override_redirect) {
7439 state = getstate(wins[j]);
7440 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7441 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7442 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7443 &wins[j], NULL) && manage)
7444 manage_window(wins[j]);
7453 int i, j, k, num_screens;
7454 struct workspace *ws;
7455 uint32_t gcv[1], wa[1];
7456 const xcb_query_extension_reply_t *qep;
7457 xcb_cursor_t cursor;
7458 xcb_font_t cursor_font;
7459 xcb_randr_query_version_cookie_t c;
7460 xcb_randr_query_version_reply_t *r;
7462 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7463 if ((screens = calloc(num_screens,
7464 sizeof(struct swm_screen))) == NULL)
7465 err(1, "setup_screens: calloc: failed to allocate memory for "
7468 /* initial Xrandr setup */
7469 xrandr_support = False;
7470 c = xcb_randr_query_version(conn, True, True);
7471 r = xcb_randr_query_version_reply(conn, c, NULL);
7473 if (r->major_version >= 1)
7474 xrandr_support = True;
7477 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7478 xrandr_eventbase = qep->first_event;
7480 cursor_font = xcb_generate_id(conn);
7481 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7483 cursor = xcb_generate_id(conn);
7484 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7485 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7488 /* map physical screens */
7489 for (i = 0; i < num_screens; i++) {
7490 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7492 TAILQ_INIT(&screens[i].rl);
7493 TAILQ_INIT(&screens[i].orl);
7494 screens[i].root = get_screen(i)->root;
7496 /* set default colors */
7497 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7498 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7499 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7500 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7501 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7503 /* create graphics context on screen */
7504 screens[i].bar_gc = xcb_generate_id(conn);
7506 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7507 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7509 /* set default cursor */
7510 xcb_change_window_attributes(conn, screens[i].root,
7513 /* init all workspaces */
7514 /* XXX these should be dynamically allocated too */
7515 for (j = 0; j < SWM_WS_MAX; j++) {
7516 ws = &screens[i].ws[j];
7522 TAILQ_INIT(&ws->winlist);
7523 TAILQ_INIT(&ws->unmanagedlist);
7525 for (k = 0; layouts[k].l_stack != NULL; k++)
7526 if (layouts[k].l_config != NULL)
7527 layouts[k].l_config(ws,
7528 SWM_ARG_ID_STACKINIT);
7529 ws->cur_layout = &layouts[0];
7530 ws->cur_layout->l_string(ws);
7536 xcb_randr_select_input(conn, screens[i].root,
7537 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7539 xcb_free_cursor(conn, cursor);
7540 xcb_close_font(conn, cursor_font);
7546 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7547 err(1, "setup_globals: strdup: failed to allocate memory.");
7549 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7550 err(1, "setup_globals: strdup: failed to allocate memory.");
7552 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7553 errx(1, "unable to allocate key symbols");
7555 a_state = get_atom_from_string("WM_STATE");
7556 a_prot = get_atom_from_string("WM_PROTOCOLS");
7557 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7558 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7559 a_wmname = get_atom_from_string("WM_NAME");
7560 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7561 a_utf8_string = get_atom_from_string("UTF8_STRING");
7562 a_string = get_atom_from_string("STRING");
7563 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7570 xcb_atom_t netwmcheck, netwmname, utf8_string;
7571 xcb_window_t root, win;
7574 /* work around sun jdk bugs, code from wmname */
7575 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7576 netwmname = get_atom_from_string("_NET_WM_NAME");
7577 utf8_string = get_atom_from_string("UTF8_STRING");
7579 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7580 for (i = 0; i < num_screens; i++) {
7581 root = screens[i].root;
7583 win = xcb_generate_id(conn);
7584 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7585 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7586 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7587 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7588 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7590 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7591 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7592 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7593 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7594 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7595 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7600 event_error(xcb_generic_error_t *e)
7602 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7603 "resource_id: %u, minor_code: %u\n",
7604 xcb_event_get_error_label(e->error_code), e->error_code,
7605 xcb_event_get_request_label(e->major_code), e->major_code,
7606 e->sequence, e->resource_id, e->minor_code);
7610 event_handle(xcb_generic_event_t *evt)
7612 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7614 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7615 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7616 XCB_EVENT_RESPONSE_TYPE(evt));
7619 #define EVENT(type, callback) case type: callback((void *)evt); return
7620 EVENT(0, event_error);
7621 EVENT(XCB_BUTTON_PRESS, buttonpress);
7622 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7623 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7624 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7625 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7626 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7627 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7628 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7629 /*EVENT(XCB_CREATE_NOTIFY, );*/
7630 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7631 EVENT(XCB_ENTER_NOTIFY, enternotify);
7632 EVENT(XCB_EXPOSE, expose);
7633 /*EVENT(XCB_FOCUS_IN, );*/
7634 /*EVENT(XCB_FOCUS_OUT, );*/
7635 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7636 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7637 EVENT(XCB_KEY_PRESS, keypress);
7638 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7639 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7640 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7641 EVENT(XCB_MAP_NOTIFY, mapnotify);
7642 EVENT(XCB_MAP_REQUEST, maprequest);
7643 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7644 /*EVENT(XCB_MOTION_NOTIFY, );*/
7645 /*EVENT(XCB_NO_EXPOSURE, );*/
7646 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7647 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7648 /*EVENT(XCB_RESIZE_REQUEST, );*/
7649 /*EVENT(XCB_SELECTION_CLEAR, );*/
7650 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7651 /*EVENT(XCB_SELECTION_REQUEST, );*/
7652 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7653 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7656 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7657 screenchange((void *)evt);
7661 main(int argc, char *argv[])
7663 struct swm_region *r, *rr;
7664 struct ws_win *winfocus = NULL;
7666 char conf[PATH_MAX], *cfile = NULL;
7668 int xfd, i, num_screens;
7669 struct sigaction sact;
7670 xcb_generic_event_t *evt;
7674 time_started = time(NULL);
7677 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7678 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
7679 warnx("no locale support");
7681 /* handle some signals */
7682 bzero(&sact, sizeof(sact));
7683 sigemptyset(&sact.sa_mask);
7685 sact.sa_handler = sighdlr;
7686 sigaction(SIGINT, &sact, NULL);
7687 sigaction(SIGQUIT, &sact, NULL);
7688 sigaction(SIGTERM, &sact, NULL);
7689 sigaction(SIGHUP, &sact, NULL);
7691 sact.sa_handler = sighdlr;
7692 sact.sa_flags = SA_NOCLDSTOP;
7693 sigaction(SIGCHLD, &sact, NULL);
7695 if (!(display = XOpenDisplay(0)))
7696 errx(1, "can not open display");
7698 conn = XGetXCBConnection(display);
7699 if (xcb_connection_has_error(conn))
7700 errx(1, "can not get XCB connection");
7702 XSetEventQueueOwner(display, XCBOwnsEventQueue);
7704 xcb_prefetch_extension_data(conn, &xcb_randr_id);
7705 xfd = xcb_get_file_descriptor(conn);
7707 /* look for local and global conf file */
7708 pwd = getpwuid(getuid());
7710 errx(1, "invalid user: %d", getuid());
7712 xcb_grab_server(conn);
7715 /* flush all events */
7716 while ((evt = xcb_poll_for_event(conn))) {
7717 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7722 if (enable_wm() != 0)
7723 errx(1, "another window manager is currently running");
7734 for (i = 0; ; i++) {
7739 snprintf(conf, sizeof conf, "%s/.%s",
7740 pwd->pw_dir, SWM_CONF_FILE);
7744 snprintf(conf, sizeof conf, "/etc/%s",
7749 snprintf(conf, sizeof conf, "%s/.%s",
7750 pwd->pw_dir, SWM_CONF_FILE_OLD);
7754 snprintf(conf, sizeof conf, "/etc/%s",
7761 if (strlen(conf) && stat(conf, &sb) != -1)
7762 if (S_ISREG(sb.st_mode)) {
7769 /* load conf (if any) */
7771 conf_load(cfile, SWM_CONF_DEFAULT);
7774 /* set some values to work around bad programs */
7776 /* grab existing windows (before we build the bars) */
7779 if (getenv("SWM_STARTED") == NULL)
7780 setenv("SWM_STARTED", "YES", 1);
7782 /* setup all bars */
7783 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7784 for (i = 0; i < num_screens; i++)
7785 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7786 if (winfocus == NULL)
7787 winfocus = TAILQ_FIRST(&r->ws->winlist);
7796 xcb_ungrab_server(conn);
7800 while ((evt = xcb_poll_for_event(conn))) {
7807 /* if we are being restarted go focus on first window */
7809 rr = winfocus->ws->r;
7811 /* not a visible window */
7815 /* move pointer to first screen if multi screen */
7816 if (num_screens > 1 || outputs > 1)
7817 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7818 rr->s[0].root, 0, 0, 0, 0, X(rr),
7819 Y(rr) + (bar_enabled ? bar_height : 0));
7821 a.id = SWM_ARG_ID_FOCUSCUR;
7831 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7833 DNPRINTF(SWM_D_MISC, "select failed");
7834 if (restart_wm == 1)
7835 restart(NULL, NULL);
7836 if (search_resp == 1)
7849 for (i = 0; i < num_screens; ++i)
7850 if (screens[i].bar_gc != 0)
7851 xcb_free_gc(conn, screens[i].bar_gc);
7853 XftFontClose(display, bar_font);
7854 XftColorFree(display, DefaultVisual(display, 0),
7855 DefaultColormap(display, 0), &bar_font_color);
7856 xcb_key_symbols_free(syms);
7858 xcb_disconnect(conn);