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>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xft/Xft.h>
94 #include <X11/Xlib-xcb.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <xcb/randr.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr = SPECTRWM_BUILDSTR;
112 static const char *buildstr = SPECTRWM_VERSION;
116 # error XRandR versions less than 1.0 are not supported
121 #define SWM_XRR_HAS_CRTC
125 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
126 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
128 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
129 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
130 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
131 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
132 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
133 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
134 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
135 #define xcb_icccm_get_wm_class xcb_get_wm_class
136 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
137 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
138 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
139 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
140 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
141 #define xcb_icccm_get_wm_name xcb_get_wm_name
142 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
143 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
144 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
145 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
146 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
147 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
148 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
149 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
150 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
151 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
156 #define DPRINTF(x...) do { \
158 fprintf(stderr, x); \
160 #define DNPRINTF(n,x...) do { \
161 if (swm_debug & n) { \
162 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
163 fprintf(stderr, x); \
166 #define SWM_D_MISC 0x0001
167 #define SWM_D_EVENT 0x0002
168 #define SWM_D_WS 0x0004
169 #define SWM_D_FOCUS 0x0008
170 #define SWM_D_MOVE 0x0010
171 #define SWM_D_STACK 0x0020
172 #define SWM_D_MOUSE 0x0040
173 #define SWM_D_PROP 0x0080
174 #define SWM_D_CLASS 0x0100
175 #define SWM_D_KEY 0x0200
176 #define SWM_D_QUIRK 0x0400
177 #define SWM_D_SPAWN 0x0800
178 #define SWM_D_EVENTQ 0x1000
179 #define SWM_D_CONF 0x2000
180 #define SWM_D_BAR 0x4000
181 #define SWM_D_INIT 0x8000
183 u_int32_t swm_debug = 0
202 #define DPRINTF(x...)
203 #define DNPRINTF(n,x...)
206 /* convert 8-bit to 16-bit */
207 #define RGB_8_TO_16(col) ((col) << 8) + (col)
209 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
210 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
211 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
212 xrc.blue = RGB_8_TO_16((px) & 0xff); \
215 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
216 #define MODKEY Mod1Mask
217 #define CLEANMASK(mask) ((mask) & ~(numlockmask | LockMask))
218 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
219 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
220 #define SWM_PROPLEN (16)
221 #define SWM_FUNCNAME_LEN (32)
222 #define SWM_KEYS_LEN (255)
223 #define SWM_QUIRK_LEN (64)
224 #define X(r) (r)->g.x
225 #define Y(r) (r)->g.y
226 #define WIDTH(r) (r)->g.w
227 #define HEIGHT(r) (r)->g.h
228 #define BORDER(w) ((w)->bordered ? border_width : 0)
229 #define MAX_X(r) ((r)->g.x + (r)->g.w)
230 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
231 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
232 #define SH_MIN_W(w) (w)->sh.min_width
233 #define SH_MIN_H(w) (w)->sh.min_height
234 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
235 #define SH_MAX_W(w) (w)->sh.max_width
236 #define SH_MAX_H(w) (w)->sh.max_height
237 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
238 #define SH_INC_W(w) (w)->sh.width_inc
239 #define SH_INC_H(w) (w)->sh.height_inc
240 #define SWM_MAX_FONT_STEPS (3)
241 #define WINID(w) ((w) ? (w)->id : 0)
242 #define YESNO(x) ((x) ? "yes" : "no")
244 #define SWM_FOCUS_DEFAULT (0)
245 #define SWM_FOCUS_SYNERGY (1)
246 #define SWM_FOCUS_FOLLOW (2)
248 #define SWM_CONF_DEFAULT (0)
249 #define SWM_CONF_KEYMAPPING (1)
252 #define SWM_LIB "/usr/local/lib/libswmhack.so"
259 xcb_atom_t a_takefocus;
261 xcb_atom_t a_netwmname;
262 xcb_atom_t a_utf8_string;
264 xcb_atom_t a_swm_iconic;
266 volatile sig_atomic_t running = 1;
267 volatile sig_atomic_t restart_wm = 0;
269 /*int last_focus_event = FocusOut;*/
273 int xrandr_eventbase;
274 unsigned int numlockmask = 0;
277 xcb_connection_t *conn;
278 xcb_key_symbols_t *syms;
281 int cycle_visible = 0;
283 int font_adjusted = 0;
284 unsigned int mod_key = MODKEY;
287 struct swm_region *search_r;
288 int select_list_pipe[2];
289 int select_resp_pipe[2];
291 volatile sig_atomic_t search_resp;
292 int search_resp_action;
294 struct search_window {
295 TAILQ_ENTRY(search_window) entry;
299 xcb_window_t indicator;
301 TAILQ_HEAD(search_winlist, search_window);
302 struct search_winlist search_wl;
307 SWM_SEARCH_UNICONIFY,
308 SWM_SEARCH_NAME_WORKSPACE,
309 SWM_SEARCH_SEARCH_WORKSPACE,
310 SWM_SEARCH_SEARCH_WINDOW
313 #define SWM_STACK_TOP (0)
314 #define SWM_STACK_BOTTOM (1)
315 #define SWM_STACK_ABOVE (2)
316 #define SWM_STACK_BELOW (3)
319 double dialog_ratio = 0.6;
321 #define SWM_BAR_MAX (256)
322 #define SWM_BAR_JUSTIFY_LEFT (0)
323 #define SWM_BAR_JUSTIFY_CENTER (1)
324 #define SWM_BAR_JUSTIFY_RIGHT (2)
325 #define SWM_BAR_OFFSET (4)
326 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
327 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
328 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
329 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
330 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
332 char *bar_argv[] = { NULL, NULL };
334 unsigned char bar_ext[SWM_BAR_MAX];
335 char bar_vertext[SWM_BAR_MAX];
337 sig_atomic_t bar_alarm = 0;
340 int bar_border_width = 1;
341 int bar_at_bottom = 0;
343 int bar_extra_running = 0;
346 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
347 char *bar_format = NULL;
348 int stack_enabled = 1;
349 int clock_enabled = 1;
350 int urgent_enabled = 0;
351 char *clock_format = NULL;
352 int title_name_enabled = 0;
353 int title_class_enabled = 0;
354 int window_name_enabled = 0;
355 int focus_mode = SWM_FOCUS_DEFAULT;
356 int focus_close = SWM_STACK_BELOW;
357 int focus_close_wrap = 1;
358 int focus_default = SWM_STACK_TOP;
359 int spawn_position = SWM_STACK_TOP;
360 int disable_border = 0;
361 int border_width = 1;
362 int verbose_layout = 0;
367 XftColor bar_font_color;
370 /* layout manager data */
371 struct swm_geometry {
384 struct swm_geometry g;
387 /* virtual "screens" */
389 TAILQ_ENTRY(swm_region) entry;
390 struct swm_geometry g;
391 struct workspace *ws; /* current workspace on this region */
392 struct workspace *ws_prior; /* prior workspace on this region */
393 struct swm_screen *s; /* screen idx */
396 TAILQ_HEAD(swm_region_list, swm_region);
399 TAILQ_ENTRY(ws_win) entry;
401 xcb_window_t transient;
402 struct ws_win *child_trans; /* transient child window */
403 struct swm_geometry g; /* current geometry */
404 struct swm_geometry g_float; /* region coordinates */
405 int g_floatvalid; /* g_float geometry validity */
406 int floatmaxed; /* whether maxed by max_stack */
411 unsigned int ewmh_flags;
412 int font_size_boundary[SWM_MAX_FONT_STEPS];
418 unsigned long quirks;
419 struct workspace *ws; /* always valid */
420 struct swm_screen *s; /* always valid, never changes */
421 xcb_get_geometry_reply_t *wa;
423 xcb_icccm_get_wm_class_reply_t ch;
424 xcb_icccm_wm_hints_t hints;
426 TAILQ_HEAD(ws_win_list, ws_win);
430 TAILQ_ENTRY(pid_e) entry;
434 TAILQ_HEAD(pid_list, pid_e);
435 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
437 /* layout handlers */
439 void vertical_config(struct workspace *, int);
440 void vertical_stack(struct workspace *, struct swm_geometry *);
441 void horizontal_config(struct workspace *, int);
442 void horizontal_stack(struct workspace *, struct swm_geometry *);
443 void max_stack(struct workspace *, struct swm_geometry *);
444 void plain_stacker(struct workspace *);
445 void fancy_stacker(struct workspace *);
448 void (*l_stack)(struct workspace *, struct swm_geometry *);
449 void (*l_config)(struct workspace *, int);
451 #define SWM_L_FOCUSPREV (1<<0)
452 #define SWM_L_MAPONFOCUS (1<<1)
453 void (*l_string)(struct workspace *);
455 /* stack, configure */
456 { vertical_stack, vertical_config, 0, plain_stacker },
457 { horizontal_stack, horizontal_config, 0, plain_stacker },
459 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
460 { NULL, NULL, 0, NULL },
463 /* position of max_stack mode in the layouts array, index into layouts! */
464 #define SWM_V_STACK (0)
465 #define SWM_H_STACK (1)
466 #define SWM_MAX_STACK (2)
468 #define SWM_H_SLICE (32)
469 #define SWM_V_SLICE (32)
471 /* define work spaces */
473 int idx; /* workspace index */
474 char *name; /* workspace name */
475 int always_raise; /* raise windows on focus */
476 struct layout *cur_layout; /* current layout handlers */
477 struct ws_win *focus; /* may be NULL */
478 struct ws_win *focus_prev; /* may be NULL */
479 struct swm_region *r; /* may be NULL */
480 struct swm_region *old_r; /* may be NULL */
481 struct ws_win_list winlist; /* list of windows in ws */
482 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
483 char stacker[10]; /* display stacker and layout */
487 int horizontal_msize;
489 int horizontal_stacks;
500 SWM_S_COLOR_BAR_BORDER,
501 SWM_S_COLOR_BAR_FONT,
507 /* physical screen mapping */
508 #define SWM_WS_MAX (22) /* hard limit */
509 int workspace_limit = 10; /* soft limit */
512 int idx; /* screen index */
513 struct swm_region_list rl; /* list of regions on this screen */
514 struct swm_region_list orl; /* list of old regions */
516 struct workspace ws[SWM_WS_MAX];
522 } c[SWM_S_COLOR_MAX];
524 xcb_gcontext_t bar_gc;
526 struct swm_screen *screens;
528 /* args to functions */
531 #define SWM_ARG_ID_FOCUSNEXT (0)
532 #define SWM_ARG_ID_FOCUSPREV (1)
533 #define SWM_ARG_ID_FOCUSMAIN (2)
534 #define SWM_ARG_ID_FOCUSCUR (4)
535 #define SWM_ARG_ID_SWAPNEXT (10)
536 #define SWM_ARG_ID_SWAPPREV (11)
537 #define SWM_ARG_ID_SWAPMAIN (12)
538 #define SWM_ARG_ID_MOVELAST (13)
539 #define SWM_ARG_ID_MASTERSHRINK (20)
540 #define SWM_ARG_ID_MASTERGROW (21)
541 #define SWM_ARG_ID_MASTERADD (22)
542 #define SWM_ARG_ID_MASTERDEL (23)
543 #define SWM_ARG_ID_FLIPLAYOUT (24)
544 #define SWM_ARG_ID_STACKRESET (30)
545 #define SWM_ARG_ID_STACKINIT (31)
546 #define SWM_ARG_ID_CYCLEWS_UP (40)
547 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
548 #define SWM_ARG_ID_CYCLESC_UP (42)
549 #define SWM_ARG_ID_CYCLESC_DOWN (43)
550 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
551 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
552 #define SWM_ARG_ID_STACKINC (50)
553 #define SWM_ARG_ID_STACKDEC (51)
554 #define SWM_ARG_ID_SS_ALL (60)
555 #define SWM_ARG_ID_SS_WINDOW (61)
556 #define SWM_ARG_ID_DONTCENTER (70)
557 #define SWM_ARG_ID_CENTER (71)
558 #define SWM_ARG_ID_KILLWINDOW (80)
559 #define SWM_ARG_ID_DELETEWINDOW (81)
560 #define SWM_ARG_ID_WIDTHGROW (90)
561 #define SWM_ARG_ID_WIDTHSHRINK (91)
562 #define SWM_ARG_ID_HEIGHTGROW (92)
563 #define SWM_ARG_ID_HEIGHTSHRINK (93)
564 #define SWM_ARG_ID_MOVEUP (100)
565 #define SWM_ARG_ID_MOVEDOWN (101)
566 #define SWM_ARG_ID_MOVELEFT (102)
567 #define SWM_ARG_ID_MOVERIGHT (103)
573 TAILQ_ENTRY(quirk) entry;
577 #define SWM_Q_FLOAT (1<<0) /* float this window */
578 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
579 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
580 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
581 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
582 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
584 TAILQ_HEAD(quirk_list, quirk);
585 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
588 * Supported EWMH hints should be added to
589 * both the enum and the ewmh array
594 _NET_MOVERESIZE_WINDOW,
595 _NET_WM_ACTION_CLOSE,
596 _NET_WM_ACTION_FULLSCREEN,
598 _NET_WM_ACTION_RESIZE,
599 _NET_WM_ALLOWED_ACTIONS,
602 _NET_WM_STATE_FULLSCREEN,
603 _NET_WM_STATE_HIDDEN,
604 _NET_WM_STATE_MAXIMIZED_HORZ,
605 _NET_WM_STATE_MAXIMIZED_VERT,
606 _NET_WM_STATE_SKIP_PAGER,
607 _NET_WM_STATE_SKIP_TASKBAR,
609 _NET_WM_WINDOW_TYPE_DIALOG,
610 _NET_WM_WINDOW_TYPE_DOCK,
611 _NET_WM_WINDOW_TYPE_NORMAL,
612 _NET_WM_WINDOW_TYPE_SPLASH,
613 _NET_WM_WINDOW_TYPE_TOOLBAR,
614 _NET_WM_WINDOW_TYPE_UTILITY,
615 _SWM_WM_STATE_MANUAL,
622 } ewmh[SWM_EWMH_HINT_MAX] = {
623 /* must be in same order as in the enum */
624 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
625 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
626 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
627 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
628 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
629 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
630 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
631 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
632 {"_NET_WM_STATE", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
637 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
638 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
639 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
644 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
645 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
646 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
647 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
650 /* function prototypes */
651 void buttonpress(xcb_button_press_event_t *);
652 void clientmessage(xcb_client_message_event_t *);
653 int conf_load(char *, int);
654 void configurenotify(xcb_configure_notify_event_t *);
655 void configurerequest(xcb_configure_request_event_t *);
656 void constrain_window(struct ws_win *, struct swm_region *, int);
657 void destroynotify(xcb_destroy_notify_event_t *);
659 void enternotify(xcb_enter_notify_event_t *);
660 void event_error(xcb_generic_error_t *);
661 void event_handle(xcb_generic_event_t *);
662 char *expand_tilde(char *);
663 void expose(xcb_expose_event_t *);
664 struct ws_win *find_window(xcb_window_t);
665 int floating_toggle_win(struct ws_win *);
666 void focus(struct swm_region *, union arg *);
667 void focus_magic(struct ws_win *);
669 void focusin(xcb_focus_in_event_t *);
671 xcb_atom_t get_atom_from_string(const char *);
673 char *get_atom_name(xcb_atom_t);
674 char *get_notify_detail_label(uint8_t);
675 char *get_notify_mode_label(uint8_t);
677 xcb_screen_t *get_screen(int);
678 char *get_win_name(xcb_window_t);
679 uint32_t getstate(xcb_window_t);
680 void grabbuttons(struct ws_win *, int);
681 void keypress(xcb_key_press_event_t *);
683 void leavenotify(xcb_leave_notify_event_t *);
685 void map_window_raised(xcb_window_t);
686 void mapnotify(xcb_map_notify_event_t *);
687 void mappingnotify(xcb_mapping_notify_event_t *);
688 void maprequest(xcb_map_request_event_t *);
689 void new_region(struct swm_screen *, int, int, int, int);
690 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
691 void propertynotify(xcb_property_notify_event_t *);
692 void spawn_select(struct swm_region *, union arg *, char *, int *);
693 void screenchange(xcb_randr_screen_change_notify_event_t *);
694 void store_float_geom(struct ws_win *, struct swm_region *);
695 void unmanage_window(struct ws_win *);
696 void unmapnotify(xcb_unmap_notify_event_t *);
697 void update_window(struct ws_win *);
698 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
701 expand_tilde(char *s)
705 char user[LOGIN_NAME_MAX];
710 errx(1, "expand_tilde: NULL string.");
718 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
723 pwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
727 if (asprintf(&result, "%s%s", pwd->pw_dir, s) == -1)
731 errx(1, "expand_tilde: failed to allocate memory.");
737 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
739 unsigned int tmpr, tmpg, tmpb;
741 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
744 *rr = RGB_8_TO_16(tmpr);
745 *gg = RGB_8_TO_16(tmpg);
746 *bb = RGB_8_TO_16(tmpb);
752 get_screen(int screen)
754 xcb_screen_iterator_t i;
756 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
757 for (; i.rem; --screen, xcb_screen_next(&i))
767 xcb_get_input_focus_cookie_t c;
768 xcb_get_input_focus_reply_t *r;
771 c = xcb_get_input_focus(conn);
773 r = xcb_get_input_focus_reply(conn, c, NULL);
779 map_window_raised(xcb_window_t win)
781 uint32_t val = XCB_STACK_MODE_ABOVE;
783 xcb_configure_window(conn, win,
784 XCB_CONFIG_WINDOW_STACK_MODE, &val);
786 xcb_map_window(conn, win);
790 get_atom_from_string(const char *str)
792 xcb_intern_atom_cookie_t c;
793 xcb_intern_atom_reply_t *r;
796 c = xcb_intern_atom(conn, 0, strlen(str), str);
797 r = xcb_intern_atom_reply(conn, c, NULL);
805 return (XCB_ATOM_NONE);
809 update_iconic(struct ws_win *win, int newv)
816 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
817 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
819 xcb_delete_property(conn, win->id, a_swm_iconic);
823 get_iconic(struct ws_win *win)
826 xcb_get_property_reply_t *pr = NULL;
829 pr = xcb_get_property_reply(conn,
830 xcb_get_property(conn, 0, win->id, a_swm_iconic,
831 XCB_ATOM_INTEGER, 0, 1), NULL);
834 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
836 v = *((int32_t *)xcb_get_property_value(pr));
847 int i, j, num_screens;
849 sup_list = get_atom_from_string("_NET_SUPPORTED");
851 for (i = 0; i < LENGTH(ewmh); i++)
852 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
854 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
855 for (i = 0; i < num_screens; i++) {
856 /* Support check window will be created by workaround(). */
858 /* Report supported atoms */
859 xcb_delete_property(conn, screens[i].root, sup_list);
860 for (j = 0; j < LENGTH(ewmh); j++)
861 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
862 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
871 xcb_atom_t sup_check, sup_list;
873 xcb_get_property_cookie_t pc;
874 xcb_get_property_reply_t *pr;
876 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
877 sup_list = get_atom_from_string("_NET_SUPPORTED");
878 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
880 for (i = 0; i < num_screens; i++) {
881 /* Get the support check window and destroy it */
882 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
883 XCB_ATOM_WINDOW, 0, 1);
884 pr = xcb_get_property_reply(conn, pc, NULL);
887 if (pr->format == sup_check) {
888 id = *((xcb_window_t *)xcb_get_property_value(pr));
890 xcb_destroy_window(conn, id);
891 xcb_delete_property(conn, screens[i].root, sup_check);
892 xcb_delete_property(conn, screens[i].root, sup_list);
899 ewmh_autoquirk(struct ws_win *win)
903 xcb_get_property_cookie_t c;
904 xcb_get_property_reply_t *r;
906 c = xcb_get_property(conn, 0, win->id,
907 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
908 r = xcb_get_property_reply(conn, c, NULL);
912 n = xcb_get_property_value_length(r);
913 type = xcb_get_property_value(r);
915 for (i = 0; i < n; i++) {
916 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
918 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
919 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
920 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
922 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
925 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
926 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
928 win->quirks = SWM_Q_FLOAT;
935 #define SWM_EWMH_ACTION_COUNT_MAX (6)
936 #define EWMH_F_FULLSCREEN (1<<0)
937 #define EWMH_F_ABOVE (1<<1)
938 #define EWMH_F_HIDDEN (1<<2)
939 #define EWMH_F_SKIP_PAGER (1<<3)
940 #define EWMH_F_SKIP_TASKBAR (1<<4)
941 #define SWM_F_MANUAL (1<<5)
944 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
952 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
953 "fullscreen %s\n", win->id, YESNO(fs));
956 if (!win->g_floatvalid)
957 store_float_geom(win, win->ws->r);
959 win->g = win->ws->r->g;
962 if (win->g_floatvalid) {
963 /* refloat at last floating relative position */
964 win->g = win->g_float;
965 X(win) += X(win->ws->r);
966 Y(win) += Y(win->ws->r);
974 ewmh_update_actions(struct ws_win *win)
976 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
982 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
985 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
986 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
989 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
990 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
993 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
994 #define _NET_WM_STATE_ADD 1 /* add/set property */
995 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
998 ewmh_update_win_state(struct ws_win *win, long state, long action)
1000 unsigned int mask = 0;
1001 unsigned int changed = 0;
1002 unsigned int orig_flags;
1007 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1008 "action: %ld\n", win->id, state, action);
1010 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1011 mask = EWMH_F_FULLSCREEN;
1012 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1013 mask = EWMH_F_ABOVE;
1014 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1015 mask = SWM_F_MANUAL;
1016 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1017 mask = EWMH_F_SKIP_PAGER;
1018 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1019 mask = EWMH_F_SKIP_TASKBAR;
1022 orig_flags = win->ewmh_flags;
1025 case _NET_WM_STATE_REMOVE:
1026 win->ewmh_flags &= ~mask;
1028 case _NET_WM_STATE_ADD:
1029 win->ewmh_flags |= mask;
1031 case _NET_WM_STATE_TOGGLE:
1032 win->ewmh_flags ^= mask;
1036 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1038 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1040 if (!floating_toggle_win(win))
1041 win->ewmh_flags = orig_flags; /* revert */
1042 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1044 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1045 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1047 if (!ewmh_set_win_fullscreen(win,
1048 win->ewmh_flags & EWMH_F_FULLSCREEN))
1049 win->ewmh_flags = orig_flags; /* revert */
1051 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1053 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1054 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1055 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1056 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1057 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1058 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1059 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1060 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1061 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1062 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1063 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1064 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1065 if (win->ewmh_flags & EWMH_F_ABOVE)
1066 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1067 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1068 &ewmh[_NET_WM_STATE_ABOVE].atom);
1069 if (win->ewmh_flags & SWM_F_MANUAL)
1070 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1071 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1072 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1076 ewmh_get_win_state(struct ws_win *win)
1079 xcb_get_property_cookie_t c;
1080 xcb_get_property_reply_t *r;
1086 win->ewmh_flags = 0;
1088 win->ewmh_flags |= EWMH_F_ABOVE;
1090 win->ewmh_flags |= SWM_F_MANUAL;
1092 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1093 XCB_ATOM_ATOM, 0, UINT32_MAX);
1094 r = xcb_get_property_reply(conn, c, NULL);
1098 states = xcb_get_property_value(r);
1099 n = xcb_get_property_value_length(r);
1101 for (i = 0; i < n; i++)
1102 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1110 dumpwins(struct swm_region *r, union arg *args)
1114 xcb_get_window_attributes_cookie_t c;
1115 xcb_get_window_attributes_reply_t *wa;
1117 /* suppress unused warning since var is needed */
1120 if (r->ws == NULL) {
1121 warnx("dumpwins: invalid workspace");
1125 warnx("=== managed window list ws %02d ===", r->ws->idx);
1126 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1127 state = getstate(win->id);
1128 c = xcb_get_window_attributes(conn, win->id);
1129 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1131 warnx("window: 0x%x, map_state: %d, state: %u, "
1132 "transient: 0x%x", win->id, wa->map_state,
1133 state, win->transient);
1136 warnx("window: 0x%x, failed xcb_get_window_attributes",
1140 warnx("===== unmanaged window list =====");
1141 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1142 state = getstate(win->id);
1143 c = xcb_get_window_attributes(conn, win->id);
1144 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1146 warnx("window: 0x%x, map_state: %d, state: %u, "
1147 "transient: 0x%x", win->id, wa->map_state,
1148 state, win->transient);
1151 warnx("window: 0x%x, failed xcb_get_window_attributes",
1155 warnx("=================================");
1159 dumpwins(struct swm_region *r, union arg *args)
1162 #endif /* SWM_DEBUG */
1167 int saved_errno, status;
1170 saved_errno = errno;
1174 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1179 if (errno != ECHILD)
1180 warn("sighdlr: waitpid");
1181 #endif /* SWM_DEBUG */
1184 if (pid == searchpid)
1188 if (WIFEXITED(status)) {
1189 if (WEXITSTATUS(status) != 0)
1190 warnx("sighdlr: child exit status: %d",
1191 WEXITSTATUS(status));
1193 warnx("sighdlr: child is terminated "
1195 #endif /* SWM_DEBUG */
1209 errno = saved_errno;
1215 struct pid_e *p = NULL;
1217 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1222 TAILQ_FOREACH(p, &pidlist, entry) {
1231 name_to_pixel(const char *colorname)
1233 uint32_t result = 0;
1234 char cname[32] = "#";
1235 xcb_screen_t *screen;
1236 xcb_colormap_t cmap;
1237 xcb_alloc_color_reply_t *cr;
1238 xcb_alloc_named_color_reply_t *nr;
1239 uint16_t rr, gg, bb;
1241 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1242 cmap = screen->default_colormap;
1244 /* color is in format rgb://rr/gg/bb */
1245 if (strncmp(colorname, "rgb:", 4) == 0) {
1246 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1247 warnx("could not parse rgb %s", colorname);
1249 cr = xcb_alloc_color_reply(conn,
1250 xcb_alloc_color(conn, cmap, rr, gg, bb),
1256 warnx("color '%s' not found", colorname);
1259 nr = xcb_alloc_named_color_reply(conn,
1260 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1263 strlcat(cname, colorname + 2, sizeof cname - 1);
1264 nr = xcb_alloc_named_color_reply(conn,
1265 xcb_alloc_named_color(conn, cmap, strlen(cname),
1272 warnx("color '%s' not found", colorname);
1279 setscreencolor(char *val, int i, int c)
1283 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1284 if (i > 0 && i <= num_screens) {
1285 screens[i - 1].c[c].pixel = name_to_pixel(val);
1286 free(screens[i - 1].c[c].name);
1287 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1289 } else if (i == -1) {
1290 for (i = 0; i < num_screens; i++) {
1291 screens[i].c[c].pixel = name_to_pixel(val);
1292 free(screens[i].c[c].name);
1293 if ((screens[i].c[c].name = strdup(val)) == NULL)
1297 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1302 fancy_stacker(struct workspace *ws)
1304 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1305 if (ws->cur_layout->l_stack == vertical_stack)
1306 snprintf(ws->stacker, sizeof ws->stacker,
1307 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1308 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1309 if (ws->cur_layout->l_stack == horizontal_stack)
1310 snprintf(ws->stacker, sizeof ws->stacker,
1311 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1312 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1316 plain_stacker(struct workspace *ws)
1318 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1319 if (ws->cur_layout->l_stack == vertical_stack)
1320 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1321 sizeof ws->stacker);
1322 if (ws->cur_layout->l_stack == horizontal_stack)
1323 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1324 sizeof ws->stacker);
1328 custom_region(char *val)
1330 unsigned int x, y, w, h;
1331 int sidx, num_screens;
1332 xcb_screen_t *screen;
1334 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1335 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1336 errx(1, "invalid custom region, "
1337 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1338 if (sidx < 1 || sidx > num_screens)
1339 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1343 screen = get_screen(sidx);
1345 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1347 if (x > screen->width_in_pixels ||
1348 y > screen->height_in_pixels ||
1349 w + x > screen->width_in_pixels ||
1350 h + y > screen->height_in_pixels) {
1351 warnx("ignoring region %ux%u+%u+%u - not within screen "
1352 "boundaries (%ux%u)", w, h, x, y,
1353 screen->width_in_pixels, screen->height_in_pixels);
1357 new_region(&screens[sidx], x, y, w, h);
1361 socket_setnonblock(int fd)
1365 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1366 err(1, "fcntl F_GETFL");
1367 flags |= O_NONBLOCK;
1368 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1369 err(1, "fcntl F_SETFL");
1373 bar_print(struct swm_region *r, const char *s)
1376 xcb_rectangle_t rect;
1384 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1386 switch (bar_justify) {
1387 case SWM_BAR_JUSTIFY_LEFT:
1390 case SWM_BAR_JUSTIFY_CENTER:
1391 x = (WIDTH(r) - info.width) / 2;
1393 case SWM_BAR_JUSTIFY_RIGHT:
1394 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1398 if (x < SWM_BAR_OFFSET)
1403 rect.width = WIDTH(r->bar);
1404 rect.height = HEIGHT(r->bar);
1406 /* clear back buffer */
1407 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1408 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1409 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1410 sizeof(rect), &rect);
1412 /* draw back buffer */
1413 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1414 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1415 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].pixel;
1416 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1418 draw = XftDrawCreate(display, r->bar->buffer,
1419 DefaultVisual(display, r->s->idx),
1420 DefaultColormap(display, r->s->idx));
1422 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1423 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1426 XftDrawDestroy(draw);
1429 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1430 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1434 bar_extra_stop(void)
1438 bzero(bar_pipe, sizeof bar_pipe);
1441 kill(bar_pid, SIGTERM);
1444 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1449 bar_class_name(char *s, size_t sz, struct swm_region *r)
1451 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1453 if (r->ws->focus->ch.class_name != NULL)
1454 strlcat(s, r->ws->focus->ch.class_name, sz);
1458 bar_title_name(char *s, size_t sz, struct swm_region *r)
1460 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1462 if (r->ws->focus->ch.instance_name != NULL)
1463 strlcat(s, r->ws->focus->ch.instance_name, sz);
1467 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1469 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1472 bar_class_name(s, sz, r);
1473 strlcat(s, ":", sz);
1474 bar_title_name(s, sz, r);
1478 bar_window_float(char *s, size_t sz, struct swm_region *r)
1480 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1482 if (r->ws->focus->floating)
1483 strlcat(s, "(f)", sz);
1487 bar_window_name(char *s, size_t sz, struct swm_region *r)
1491 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1493 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1496 strlcat(s, title, sz);
1500 int urgent[SWM_WS_MAX];
1502 bar_urgent(char *s, size_t sz)
1505 int i, j, num_screens;
1507 xcb_get_property_cookie_t c;
1508 xcb_icccm_wm_hints_t hints;
1510 for (i = 0; i < workspace_limit; i++)
1513 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1514 for (i = 0; i < num_screens; i++)
1515 for (j = 0; j < workspace_limit; j++)
1516 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1517 c = xcb_icccm_get_wm_hints(conn, win->id);
1518 if (xcb_icccm_get_wm_hints_reply(conn, c,
1521 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1525 for (i = 0; i < workspace_limit; i++) {
1527 snprintf(b, sizeof b, "%d ", i + 1);
1529 snprintf(b, sizeof b, "- ");
1535 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1537 if (r == NULL || r->ws == NULL)
1539 if (r->ws->name != NULL)
1540 strlcat(s, r->ws->name, sz);
1543 /* build the default bar format according to the defined enabled options */
1545 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1547 /* if format provided, just copy the buffers */
1548 if (bar_format != NULL) {
1549 strlcpy(fmtnew, fmtexp, sz);
1553 /* reset the output buffer */
1556 strlcat(fmtnew, "+N:+I ", sz);
1558 strlcat(fmtnew, "+S", sz);
1559 strlcat(fmtnew, " ", sz);
1561 /* only show the workspace name if there's actually one */
1562 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1563 strlcat(fmtnew, "<+D>", sz);
1564 strlcat(fmtnew, "+3<", sz);
1566 if (clock_enabled) {
1567 strlcat(fmtnew, fmtexp, sz);
1568 strlcat(fmtnew, "+4<", sz);
1571 /* bar_urgent already adds the space before the last asterisk */
1573 strlcat(fmtnew, "* +U*+4<", sz);
1575 if (title_class_enabled) {
1576 strlcat(fmtnew, "+C", sz);
1577 if (title_name_enabled == 0)
1578 strlcat(fmtnew, "+4<", sz);
1581 /* checks needed by the colon and floating strlcat(3) calls below */
1582 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1583 if (title_name_enabled) {
1584 if (title_class_enabled)
1585 strlcat(fmtnew, ":", sz);
1586 strlcat(fmtnew, "+T+4<", sz);
1588 if (window_name_enabled) {
1589 if (r->ws->focus->floating)
1590 strlcat(fmtnew, "+F ", sz);
1591 strlcat(fmtnew, "+64W ", sz);
1595 /* finally add the action script output and the version */
1596 strlcat(fmtnew, "+4<+A+4<+V", sz);
1600 bar_replace_pad(char *tmp, int *limit, size_t sz)
1602 /* special case; no limit given, pad one space, instead */
1603 if (*limit == (int)sz - 1)
1605 snprintf(tmp, sz, "%*s", *limit, " ");
1608 /* replaces the bar format character sequences (like in tmux(1)) */
1610 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1614 char tmp[SWM_BAR_MAX];
1618 /* reset strlcat(3) buffer */
1621 /* get number, if any */
1624 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1625 limit = sizeof tmp - 1;
1626 if (limit <= 0 || limit >= (int)sizeof tmp)
1627 limit = sizeof tmp - 1;
1629 /* there is nothing to replace (ie EOL) */
1636 bar_replace_pad(tmp, &limit, sizeof tmp);
1639 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1642 bar_class_name(tmp, sizeof tmp, r);
1645 bar_workspace_name(tmp, sizeof tmp, r);
1648 bar_window_float(tmp, sizeof tmp, r);
1651 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1654 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1657 bar_class_title_name(tmp, sizeof tmp, r);
1660 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1663 bar_title_name(tmp, sizeof tmp, r);
1666 bar_urgent(tmp, sizeof tmp);
1669 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1672 bar_window_name(tmp, sizeof tmp, r);
1675 /* unknown character sequence; copy as-is */
1676 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1682 if ((int)len < limit)
1684 while (limit-- > 0) {
1685 if (*offrep >= sz - 1)
1687 fmtrep[(*offrep)++] = *ptr++;
1695 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1700 while (*fmt != '\0') {
1702 /* skip ordinary characters */
1705 fmtrep[off++] = *fmt++;
1709 /* character sequence found; replace it */
1710 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1719 bar_fmt_expand(char *fmtexp, size_t sz)
1726 /* start by grabbing the current time and date */
1728 localtime_r(&tmt, &tm);
1730 /* figure out what to expand */
1731 if (bar_format != NULL)
1733 else if (bar_format == NULL && clock_enabled)
1735 /* if nothing to expand bail out */
1741 /* copy as-is, just in case the format shouldn't be expanded below */
1742 strlcpy(fmtexp, fmt, sz);
1743 /* finally pass the string through strftime(3) */
1744 #ifndef SWM_DENY_CLOCK_FORMAT
1745 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1746 warnx("format too long");
1754 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1755 char fmtrep[SWM_BAR_MAX];
1757 struct swm_region *r;
1759 /* expand the format by first passing it through strftime(3) */
1760 bar_fmt_expand(fmtexp, sizeof fmtexp);
1762 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1763 for (i = 0; i < num_screens; i++) {
1764 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1767 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1768 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1769 bar_print(r, fmtrep);
1780 if (bar_enabled == 0)
1782 if (bar_extra && bar_extra_running) {
1783 /* ignore short reads; it'll correct itself */
1784 while ((b = fgetln(stdin, &len)) != NULL)
1785 if (b && b[len - 1] == '\n') {
1787 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1789 if (b == NULL && errno != EAGAIN) {
1790 warn("bar_update: bar_extra failed");
1794 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1803 /* suppress unused warning since var is needed */
1810 bar_toggle(struct swm_region *r, union arg *args)
1812 struct swm_region *tmpr;
1815 /* suppress unused warnings since vars are needed */
1819 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1821 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1823 for (i = 0; i < num_screens; i++)
1824 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1826 xcb_unmap_window(conn, tmpr->bar->id);
1828 for (i = 0; i < num_screens; i++)
1829 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1831 map_window_raised(tmpr->bar->id);
1834 bar_enabled = !bar_enabled;
1837 /* must be after stack */
1846 struct swm_region *r;
1850 /* do this here because the conf file is in memory */
1851 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1852 /* launch external status app */
1853 bar_extra_running = 1;
1854 if (pipe(bar_pipe) == -1)
1855 err(1, "pipe error");
1856 socket_setnonblock(bar_pipe[0]);
1857 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1858 if (dup2(bar_pipe[0], 0) == -1)
1860 if (dup2(bar_pipe[1], 1) == -1)
1862 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1863 err(1, "could not disable SIGPIPE");
1864 switch (bar_pid = fork()) {
1866 err(1, "cannot fork");
1870 execvp(bar_argv[0], bar_argv);
1871 err(1, "%s external app failed", bar_argv[0]);
1873 default: /* parent */
1879 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1880 for (i = 0; i < num_screens; i++)
1881 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1884 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1885 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1886 xcb_change_window_attributes(conn, r->bar->id,
1887 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1893 bar_setup(struct swm_region *r)
1895 char *font, *fontpos, *dup, *search;
1897 xcb_screen_t *screen = get_screen(r->s->idx);
1904 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1905 err(1, "bar_setup: calloc: failed to allocate memory.");
1907 if (bar_font == NULL) {
1908 if ((dup = strdup(bar_fonts)) == NULL)
1909 errx(1, "insufficient memory.");
1911 while ((font = strsep(&search, ",")) != NULL) {
1915 DNPRINTF(SWM_D_INIT, "bar_setup: try font %s\n", font);
1919 while ((fontpos = index(fontpos, '-'))) {
1925 bar_font = XftFontOpenXlfd(display, r->s->idx,
1928 bar_font = XftFontOpenName(display, r->s->idx,
1931 warnx("unable to load font %s", font);
1934 DNPRINTF(SWM_D_INIT, "successfully opened "
1942 if (bar_font == NULL)
1943 errx(1, "unable to open a font");
1945 bar_height = bar_font->height + 2 * bar_border_width;
1950 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
1952 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
1953 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
1954 warn("unable to allocate Xft color");
1957 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1958 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1959 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1961 r->bar->id = xcb_generate_id(conn);
1962 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1963 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
1964 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1966 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1967 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1968 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1969 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1970 | XCB_CW_EVENT_MASK, wa);
1972 r->bar->buffer = xcb_generate_id(conn);
1973 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1974 WIDTH(r->bar), HEIGHT(r->bar));
1976 xcb_randr_select_input(conn, r->bar->id,
1977 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1980 map_window_raised(r->bar->id);
1982 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1983 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1986 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1987 err(1, "could not install bar_signal");
1992 bar_cleanup(struct swm_region *r)
1996 xcb_destroy_window(conn, r->bar->id);
1997 xcb_free_pixmap(conn, r->bar->buffer);
2003 set_win_state(struct ws_win *win, uint16_t state)
2005 uint16_t data[2] = { state, XCB_ATOM_NONE };
2007 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2012 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2013 a_state, 32, 2, data);
2017 getstate(xcb_window_t w)
2019 uint32_t result = 0;
2020 xcb_get_property_cookie_t c;
2021 xcb_get_property_reply_t *r;
2023 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2024 r = xcb_get_property_reply(conn, c, NULL);
2027 if (r->type == a_state && r->format == 32 && r->length == 2)
2028 result = *((uint32_t *)xcb_get_property_value(r));
2032 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2038 version(struct swm_region *r, union arg *args)
2040 /* suppress unused warnings since vars are needed */
2044 bar_version = !bar_version;
2046 snprintf(bar_vertext, sizeof bar_vertext,
2047 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2049 strlcpy(bar_vertext, "", sizeof bar_vertext);
2056 client_msg(struct ws_win *win, xcb_atom_t a)
2058 xcb_client_message_event_t ev;
2063 bzero(&ev, sizeof ev);
2064 ev.response_type = XCB_CLIENT_MESSAGE;
2065 ev.window = win->id;
2068 ev.data.data32[0] = a;
2069 ev.data.data32[1] = XCB_CURRENT_TIME;
2071 xcb_send_event(conn, 0, win->id,
2072 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2075 /* synthetic response to a ConfigureRequest when not making a change */
2077 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2079 xcb_configure_notify_event_t ce;
2084 /* send notification of unchanged state. */
2085 bzero(&ce, sizeof(ce));
2086 ce.response_type = XCB_CONFIGURE_NOTIFY;
2089 ce.width = WIDTH(win);
2090 ce.height = HEIGHT(win);
2091 ce.override_redirect = 0;
2096 ce.window = win->id;
2097 ce.border_width = BORDER(win);
2098 ce.above_sibling = XCB_WINDOW_NONE;
2101 ce.event = ev->window;
2102 ce.window = ev->window;
2104 /* make response appear more WM_SIZE_HINTS-compliant */
2106 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2107 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2108 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2109 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2110 SH_INC_W(win), SH_INC_H(win));
2114 /* the hint may be set... to 0! */
2115 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2116 ce.width = SH_MIN_W(win);
2117 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2118 ce.height = SH_MIN_H(win);
2123 /* may also be advertized as 0 */
2124 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2125 ce.width = SH_MAX_W(win);
2126 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2127 ce.height = SH_MAX_H(win);
2130 /* resize increment. */
2132 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2133 ce.width -= (ce.width - SH_MIN_W(win)) %
2135 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2136 ce.height -= (ce.height - SH_MIN_H(win)) %
2140 /* adjust x and y for requested border_width. */
2141 ce.x += BORDER(win) - ev->border_width;
2142 ce.y += BORDER(win) - ev->border_width;
2143 ce.border_width = ev->border_width;
2144 ce.above_sibling = ev->sibling;
2147 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2148 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2149 ce.y, ce.width, ce.height, ce.border_width);
2151 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2156 count_win(struct workspace *ws, int count_transient)
2161 TAILQ_FOREACH(win, &ws->winlist, entry) {
2162 if (count_transient == 0 && win->floating)
2164 if (count_transient == 0 && win->transient)
2170 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2176 quit(struct swm_region *r, union arg *args)
2178 /* suppress unused warnings since vars are needed */
2182 DNPRINTF(SWM_D_MISC, "quit\n");
2187 unmap_window(struct ws_win *win)
2189 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2194 /* don't unmap again */
2195 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2198 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2200 xcb_unmap_window(conn, win->id);
2201 xcb_change_window_attributes(conn, win->id,
2202 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2209 int i, j, num_screens;
2211 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2212 for (i = 0; i < num_screens; i++)
2213 for (j = 0; j < workspace_limit; j++)
2214 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2219 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2221 xcb_key_press_event_t event;
2222 xcb_keycode_t *keycode;
2227 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2229 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2232 bzero(&event, sizeof(event));
2233 event.event = win->id;
2234 event.root = win->s->root;
2235 event.child = XCB_WINDOW_NONE;
2236 event.time = XCB_CURRENT_TIME;
2237 event.event_x = X(win);
2238 event.event_y = Y(win);
2241 event.same_screen = 1;
2242 event.detail = *keycode;
2243 event.state = modifiers;
2245 event.response_type = XCB_KEY_PRESS;
2246 xcb_send_event(conn, 1, win->id,
2247 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2249 event.response_type = XCB_KEY_RELEASE;
2250 xcb_send_event(conn, 1, win->id,
2251 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2257 restart(struct swm_region *r, union arg *args)
2259 /* suppress unused warning since var is needed */
2262 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2264 /* disable alarm because the following code may not be interrupted */
2266 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2267 err(1, "can't disable alarm");
2273 XftFontClose(display, bar_font);
2274 XftColorFree(display, DefaultVisual(display, r->s->idx),
2275 DefaultColormap(display, r->s->idx), &bar_font_color);
2276 xcb_key_symbols_free(syms);
2278 xcb_disconnect(conn);
2280 execvp(start_argv[0], start_argv);
2281 warn("execvp failed");
2286 root_to_region(xcb_window_t root)
2288 struct swm_region *r = NULL;
2290 xcb_query_pointer_reply_t *qpr;
2292 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2294 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2295 for (i = 0; i < num_screens; i++)
2296 if (screens[i].root == root)
2299 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2300 screens[i].root), NULL);
2303 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2304 qpr->root_x, qpr->root_y);
2305 /* choose a region based on pointer location */
2306 TAILQ_FOREACH(r, &screens[i].rl, entry)
2307 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2308 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2314 r = TAILQ_FIRST(&screens[i].rl);
2320 find_unmanaged_window(xcb_window_t id)
2323 int i, j, num_screens;
2325 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2326 for (i = 0; i < num_screens; i++)
2327 for (j = 0; j < workspace_limit; j++)
2328 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2336 find_window(xcb_window_t id)
2339 int i, j, num_screens;
2340 xcb_query_tree_reply_t *r;
2342 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2343 for (i = 0; i < num_screens; i++)
2344 for (j = 0; j < workspace_limit; j++)
2345 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2349 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2353 /* if we were looking for the parent return that window instead */
2354 if (r->parent == 0 || r->root == r->parent) {
2359 /* look for parent */
2360 for (i = 0; i < num_screens; i++)
2361 for (j = 0; j < workspace_limit; j++)
2362 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2363 if (r->parent == win->id) {
2373 spawn(int ws_idx, union arg *args, int close_fd)
2378 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2380 close(xcb_get_file_descriptor(conn));
2382 setenv("LD_PRELOAD", SWM_LIB, 1);
2384 if (asprintf(&ret, "%d", ws_idx) == -1) {
2385 warn("spawn: asprintf SWM_WS");
2388 setenv("_SWM_WS", ret, 1);
2392 if (asprintf(&ret, "%d", getpid()) == -1) {
2393 warn("spawn: asprintf _SWM_PID");
2396 setenv("_SWM_PID", ret, 1);
2400 if (setsid() == -1) {
2401 warn("spawn: setsid");
2407 * close stdin and stdout to prevent interaction between apps
2408 * and the baraction script
2409 * leave stderr open to record errors
2411 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2412 warn("spawn: open");
2415 dup2(fd, STDIN_FILENO);
2416 dup2(fd, STDOUT_FILENO);
2421 execvp(args->argv[0], args->argv);
2423 warn("spawn: execvp");
2428 kill_refs(struct ws_win *win)
2430 int i, x, num_screens;
2431 struct swm_region *r;
2432 struct workspace *ws;
2437 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2438 for (i = 0; i < num_screens; i++)
2439 TAILQ_FOREACH(r, &screens[i].rl, entry)
2440 for (x = 0; x < workspace_limit; x++) {
2442 if (win == ws->focus)
2444 if (win == ws->focus_prev)
2445 ws->focus_prev = NULL;
2450 validate_win(struct ws_win *testwin)
2453 struct workspace *ws;
2454 struct swm_region *r;
2455 int i, x, num_screens;
2457 if (testwin == NULL)
2460 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2461 for (i = 0; i < num_screens; i++)
2462 TAILQ_FOREACH(r, &screens[i].rl, entry)
2463 for (x = 0; x < workspace_limit; x++) {
2465 TAILQ_FOREACH(win, &ws->winlist, entry)
2473 validate_ws(struct workspace *testws)
2475 struct swm_region *r;
2476 struct workspace *ws;
2477 int i, x, num_screens;
2479 /* validate all ws */
2480 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2481 for (i = 0; i < num_screens; i++)
2482 TAILQ_FOREACH(r, &screens[i].rl, entry)
2483 for (x = 0; x < workspace_limit; x++) {
2492 unfocus_win(struct ws_win *win)
2494 xcb_window_t none = XCB_WINDOW_NONE;
2496 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2500 if (win->ws == NULL)
2503 if (validate_ws(win->ws))
2504 return; /* XXX this gets hit with thunderbird, needs fixing */
2506 if (win->ws->r == NULL)
2509 if (validate_win(win)) {
2514 if (win->ws->focus == win) {
2515 win->ws->focus = NULL;
2516 win->ws->focus_prev = win;
2519 if (validate_win(win->ws->focus)) {
2520 kill_refs(win->ws->focus);
2521 win->ws->focus = NULL;
2523 if (validate_win(win->ws->focus_prev)) {
2524 kill_refs(win->ws->focus_prev);
2525 win->ws->focus_prev = NULL;
2528 grabbuttons(win, 0);
2529 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2530 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2531 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2532 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2539 int i, j, num_screens;
2541 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2543 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2544 for (i = 0; i < num_screens; i++)
2545 for (j = 0; j < workspace_limit; j++)
2546 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2551 focus_win(struct ws_win *win)
2553 struct ws_win *cfw = NULL;
2554 xcb_get_input_focus_reply_t *r;
2555 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2557 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2561 if (win->ws == NULL)
2564 if (validate_ws(win->ws))
2565 return; /* XXX this gets hit with thunderbird, needs fixing */
2567 if (validate_win(win)) {
2572 if (validate_win(win)) {
2577 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2579 cur_focus = r->focus;
2582 if ((cfw = find_window(cur_focus)) != NULL)
2586 /* use larger hammer since the window was killed somehow */
2587 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2588 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2589 xcb_change_window_attributes(conn, cfw->id,
2590 XCB_CW_BORDER_PIXEL,
2591 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2595 win->ws->focus = win;
2597 if (win->ws->r != NULL) {
2599 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2600 win->id, XCB_CURRENT_TIME);
2601 grabbuttons(win, 1);
2602 xcb_change_window_attributes(conn, win->id,
2603 XCB_CW_BORDER_PIXEL,
2604 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2605 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2606 win->ws->always_raise)
2607 map_window_raised(win->id);
2609 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2610 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2618 event_drain(uint8_t rt)
2620 xcb_generic_event_t *evt;
2623 while ((evt = xcb_poll_for_event(conn))) {
2624 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2632 switchws(struct swm_region *r, union arg *args)
2634 int wsid = args->id, unmap_old = 0;
2635 struct swm_region *this_r, *other_r;
2637 struct workspace *new_ws, *old_ws;
2643 if (wsid >= workspace_limit)
2647 old_ws = this_r->ws;
2648 new_ws = &this_r->s->ws[wsid];
2650 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2651 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2653 if (new_ws == NULL || old_ws == NULL)
2655 if (new_ws == old_ws)
2658 other_r = new_ws->r;
2659 if (other_r == NULL) {
2660 /* the other workspace is hidden, hide this one */
2664 /* the other ws is visible in another region, exchange them */
2665 other_r->ws_prior = new_ws;
2666 other_r->ws = old_ws;
2667 old_ws->r = other_r;
2669 this_r->ws_prior = old_ws;
2670 this_r->ws = new_ws;
2673 /* this is needed so that we can click on a window after a restart */
2677 a.id = SWM_ARG_ID_FOCUSCUR;
2678 focus(new_ws->r, &a);
2680 /* unmap old windows */
2682 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2685 if (focus_mode == SWM_FOCUS_DEFAULT)
2686 event_drain(XCB_ENTER_NOTIFY);
2690 DNPRINTF(SWM_D_WS, "switchws: done\n");
2694 cyclews(struct swm_region *r, union arg *args)
2697 struct swm_screen *s = r->s;
2700 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2701 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2706 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2709 case SWM_ARG_ID_CYCLEWS_UP:
2710 if (a.id < workspace_limit - 1)
2715 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2718 case SWM_ARG_ID_CYCLEWS_DOWN:
2722 a.id = workspace_limit - 1;
2729 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2731 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2735 } while (a.id != r->ws->idx);
2739 priorws(struct swm_region *r, union arg *args)
2743 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2744 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2746 if (r->ws_prior == NULL)
2749 a.id = r->ws_prior->idx;
2754 cyclescr(struct swm_region *r, union arg *args)
2756 struct swm_region *rr = NULL;
2758 int i, x, y, num_screens;
2760 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2761 /* do nothing if we don't have more than one screen */
2762 if (!(num_screens > 1 || outputs > 1))
2767 case SWM_ARG_ID_CYCLESC_UP:
2768 rr = TAILQ_NEXT(r, entry);
2770 rr = TAILQ_FIRST(&screens[i].rl);
2772 case SWM_ARG_ID_CYCLESC_DOWN:
2773 rr = TAILQ_PREV(r, swm_region_list, entry);
2775 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2783 /* move mouse to region */
2785 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2786 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2789 a.id = SWM_ARG_ID_FOCUSCUR;
2792 if (rr->ws->focus) {
2793 /* move to focus window */
2794 x = X(rr->ws->focus) + 1;
2795 y = Y(rr->ws->focus) + 1;
2796 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2802 sort_windows(struct ws_win_list *wl)
2804 struct ws_win *win, *parent, *nxt;
2809 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2810 nxt = TAILQ_NEXT(win, entry);
2811 if (win->transient) {
2812 parent = find_window(win->transient);
2813 if (parent == NULL) {
2814 warnx("not possible bug");
2817 TAILQ_REMOVE(wl, win, entry);
2818 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2825 swapwin(struct swm_region *r, union arg *args)
2827 struct ws_win *target, *source;
2828 struct ws_win *cur_focus;
2829 struct ws_win_list *wl;
2832 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2833 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2835 cur_focus = r->ws->focus;
2836 if (cur_focus == NULL)
2840 wl = &source->ws->winlist;
2843 case SWM_ARG_ID_SWAPPREV:
2844 if (source->transient)
2845 source = find_window(source->transient);
2846 target = TAILQ_PREV(source, ws_win_list, entry);
2847 if (target && target->transient)
2848 target = find_window(target->transient);
2849 TAILQ_REMOVE(wl, source, entry);
2851 TAILQ_INSERT_TAIL(wl, source, entry);
2853 TAILQ_INSERT_BEFORE(target, source, entry);
2855 case SWM_ARG_ID_SWAPNEXT:
2856 target = TAILQ_NEXT(source, entry);
2857 /* move the parent and let the sort handle the move */
2858 if (source->transient)
2859 source = find_window(source->transient);
2860 TAILQ_REMOVE(wl, source, entry);
2862 TAILQ_INSERT_HEAD(wl, source, entry);
2864 TAILQ_INSERT_AFTER(wl, target, source, entry);
2866 case SWM_ARG_ID_SWAPMAIN:
2867 target = TAILQ_FIRST(wl);
2868 if (target == source) {
2869 if (source->ws->focus_prev != NULL &&
2870 source->ws->focus_prev != target)
2871 source = source->ws->focus_prev;
2875 if (target == NULL || source == NULL)
2877 source->ws->focus_prev = target;
2878 TAILQ_REMOVE(wl, target, entry);
2879 TAILQ_INSERT_BEFORE(source, target, entry);
2880 TAILQ_REMOVE(wl, source, entry);
2881 TAILQ_INSERT_HEAD(wl, source, entry);
2883 case SWM_ARG_ID_MOVELAST:
2884 TAILQ_REMOVE(wl, source, entry);
2885 TAILQ_INSERT_TAIL(wl, source, entry);
2888 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2900 focus_prev(struct ws_win *win)
2902 struct ws_win *winfocus = NULL;
2903 struct ws_win *cur_focus = NULL;
2904 struct ws_win_list *wl = NULL;
2905 struct workspace *ws = NULL;
2907 if (!(win && win->ws))
2912 cur_focus = ws->focus;
2914 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2915 WINID(win), WINID(cur_focus));
2917 /* pickle, just focus on whatever */
2918 if (cur_focus == NULL) {
2919 /* use prev_focus if valid */
2920 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2921 find_window(WINID(ws->focus_prev)))
2922 winfocus = ws->focus_prev;
2926 /* if transient focus on parent */
2927 if (cur_focus->transient) {
2928 winfocus = find_window(cur_focus->transient);
2932 /* if in max_stack try harder */
2933 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2934 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2935 if (cur_focus != ws->focus_prev)
2936 winfocus = ws->focus_prev;
2938 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2943 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2945 if (winfocus == NULL || winfocus == win) {
2946 switch (focus_close) {
2947 case SWM_STACK_BOTTOM:
2948 winfocus = TAILQ_FIRST(wl);
2951 winfocus = TAILQ_LAST(wl, ws_win_list);
2953 case SWM_STACK_ABOVE:
2954 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2955 if (focus_close_wrap)
2956 winfocus = TAILQ_FIRST(wl);
2958 winfocus = TAILQ_PREV(cur_focus,
2959 ws_win_list, entry);
2962 case SWM_STACK_BELOW:
2963 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2965 if (focus_close_wrap)
2966 winfocus = TAILQ_LAST(wl, ws_win_list);
2968 winfocus = TAILQ_NEXT(cur_focus, entry);
2974 if (winfocus == NULL) {
2975 if (focus_default == SWM_STACK_TOP)
2976 winfocus = TAILQ_LAST(wl, ws_win_list);
2978 winfocus = TAILQ_FIRST(wl);
2982 focus_magic(winfocus);
2986 focus(struct swm_region *r, union arg *args)
2988 struct ws_win *winfocus = NULL, *head;
2989 struct ws_win *cur_focus = NULL;
2990 struct ws_win_list *wl = NULL;
2991 struct workspace *ws = NULL;
2997 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2999 /* treat FOCUS_CUR special */
3000 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3001 if (r->ws->focus && r->ws->focus->iconic == 0)
3002 winfocus = r->ws->focus;
3003 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3004 winfocus = r->ws->focus_prev;
3006 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3007 if (winfocus->iconic == 0)
3010 focus_magic(winfocus);
3014 if ((cur_focus = r->ws->focus) == NULL)
3018 if (TAILQ_EMPTY(wl))
3020 /* make sure there is at least one uniconified window */
3022 TAILQ_FOREACH(winfocus, wl, entry)
3023 if (winfocus->iconic == 0) {
3031 case SWM_ARG_ID_FOCUSPREV:
3032 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3034 head = TAILQ_LAST(wl, ws_win_list);
3036 if (WINID(winfocus) == cur_focus->transient) {
3037 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3039 head = TAILQ_LAST(wl, ws_win_list);
3044 if (winfocus && winfocus->iconic) {
3045 while (winfocus != cur_focus) {
3046 if (winfocus == NULL)
3047 winfocus = TAILQ_LAST(wl, ws_win_list);
3048 if (winfocus->iconic == 0)
3050 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3056 case SWM_ARG_ID_FOCUSNEXT:
3057 head = TAILQ_NEXT(cur_focus, entry);
3059 head = TAILQ_FIRST(wl);
3063 if (winfocus && winfocus->iconic) {
3064 while (winfocus != cur_focus) {
3065 if (winfocus == NULL)
3066 winfocus = TAILQ_FIRST(wl);
3067 if (winfocus->iconic == 0)
3069 winfocus = TAILQ_NEXT(winfocus, entry);
3074 case SWM_ARG_ID_FOCUSMAIN:
3075 winfocus = TAILQ_FIRST(wl);
3076 if (winfocus == cur_focus)
3077 winfocus = cur_focus->ws->focus_prev;
3084 focus_magic(winfocus);
3090 cycle_layout(struct swm_region *r, union arg *args)
3092 struct workspace *ws = r->ws;
3095 /* suppress unused warning since var is needed */
3098 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3101 if (ws->cur_layout->l_stack == NULL)
3102 ws->cur_layout = &layouts[0];
3107 if (focus_mode == SWM_FOCUS_DEFAULT)
3108 event_drain(XCB_ENTER_NOTIFY);
3110 a.id = SWM_ARG_ID_FOCUSCUR;
3115 stack_config(struct swm_region *r, union arg *args)
3117 struct workspace *ws = r->ws;
3119 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3122 if (ws->cur_layout->l_config != NULL)
3123 ws->cur_layout->l_config(ws, args->id);
3125 if (args->id != SWM_ARG_ID_STACKINIT)
3132 struct swm_geometry g;
3133 struct swm_region *r;
3139 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3141 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3142 for (i = 0; i < num_screens; i++) {
3146 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3147 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3148 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3150 /* start with screen geometry, adjust for bar */
3152 g.w -= 2 * border_width;
3153 g.h -= 2 * border_width;
3159 r->ws->cur_layout->l_stack(r->ws, &g);
3160 r->ws->cur_layout->l_string(r->ws);
3161 /* save r so we can track region changes */
3168 if (focus_mode == SWM_FOCUS_DEFAULT)
3169 event_drain(XCB_ENTER_NOTIFY);
3171 DNPRINTF(SWM_D_STACK, "stack: end\n");
3175 store_float_geom(struct ws_win *win, struct swm_region *r)
3177 /* retain window geom and region geom */
3178 win->g_float = win->g;
3179 win->g_float.x -= X(r);
3180 win->g_float.y -= Y(r);
3181 win->g_floatvalid = 1;
3182 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3183 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3184 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3185 win->g_float.w, win->g_float.h);
3189 stack_floater(struct ws_win *win, struct swm_region *r)
3194 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3197 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3198 * geom on ws switches or return from max mode
3200 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3201 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3202 /* refloat at last floating relative position */
3203 win->g = win->g_float;
3208 win->floatmaxed = 0;
3211 * if set to fullscreen mode, configure window to maximum size.
3213 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3214 if (!win->g_floatvalid)
3215 store_float_geom(win, win->ws->r);
3217 win->g = win->ws->r->g;
3221 * remove border on fullscreen floater when in fullscreen mode or when
3222 * the quirk is present.
3224 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3225 ((win->quirks & SWM_Q_FULLSCREEN) &&
3226 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3227 if (win->bordered) {
3229 X(win) += border_width;
3230 Y(win) += border_width;
3232 } else if (!win->bordered) {
3234 X(win) -= border_width;
3235 Y(win) -= border_width;
3238 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3239 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3240 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3245 * floaters and transients are auto-centred unless moved
3248 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3249 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3252 /* keep window within region bounds */
3253 constrain_window(win, r, 0);
3259 * Send keystrokes to terminal to decrease/increase the font size as the
3260 * window size changes.
3263 adjust_font(struct ws_win *win)
3265 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3266 win->floating || win->transient)
3269 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3270 WIDTH(win) / win->sh.width_inc < term_width &&
3271 win->font_steps < SWM_MAX_FONT_STEPS) {
3272 win->font_size_boundary[win->font_steps] =
3273 (win->sh.width_inc * term_width) + win->sh.base_width;
3276 win->last_inc = win->sh.width_inc;
3277 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3278 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3279 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3282 win->last_inc = win->sh.width_inc;
3283 fake_keypress(win, XK_KP_Add, ShiftMask);
3287 #define SWAPXY(g) do { \
3289 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3290 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3293 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3295 struct swm_geometry win_g, r_g = *g;
3296 struct ws_win *win, *fs_win = NULL;
3297 int i, j, s, stacks;
3298 int w_inc = 1, h_inc, w_base = 1, h_base;
3299 int hrh, extra = 0, h_slice, last_h = 0;
3300 int split, colno, winno, mwin, msize, mscale;
3301 int remain, missing, v_slice, reconfigure;
3303 xcb_get_window_attributes_reply_t *war;
3305 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3306 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3308 winno = count_win(ws, 0);
3309 if (winno == 0 && count_win(ws, 1) == 0)
3312 TAILQ_FOREACH(win, &ws->winlist, entry)
3313 if (win->transient == 0 && win->floating == 0
3314 && win->iconic == 0)
3321 w_inc = win->sh.width_inc;
3322 w_base = win->sh.base_width;
3323 mwin = ws->l_state.horizontal_mwin;
3324 mscale = ws->l_state.horizontal_msize;
3325 stacks = ws->l_state.horizontal_stacks;
3328 w_inc = win->sh.height_inc;
3329 w_base = win->sh.base_height;
3330 mwin = ws->l_state.vertical_mwin;
3331 mscale = ws->l_state.vertical_msize;
3332 stacks = ws->l_state.vertical_stacks;
3336 if (stacks > winno - mwin)
3337 stacks = winno - mwin;
3341 h_slice = r_g.h / SWM_H_SLICE;
3342 if (mwin && winno > mwin) {
3343 v_slice = r_g.w / SWM_V_SLICE;
3347 win_g.w = v_slice * mscale;
3349 if (w_inc > 1 && w_inc < v_slice) {
3350 /* adjust for window's requested size increment */
3351 remain = (win_g.w - w_base) % w_inc;
3357 win_g.x += r_g.w - msize;
3360 colno = split = winno / stacks;
3361 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3362 2 * border_width) / stacks);
3364 hrh = r_g.h / colno;
3365 extra = r_g.h - (colno * hrh);
3366 win_g.h = hrh - 2 * border_width;
3368 /* stack all the tiled windows */
3369 i = j = 0, s = stacks;
3370 TAILQ_FOREACH(win, &ws->winlist, entry) {
3371 if (win->transient != 0 || win->floating != 0)
3373 if (win->iconic != 0)
3376 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3381 if (split && i == split) {
3382 colno = (winno - mwin) / stacks;
3383 if (s <= (winno - mwin) % stacks)
3385 split = split + colno;
3386 hrh = (r_g.h / colno);
3387 extra = r_g.h - (colno * hrh);
3391 win_g.x += win_g.w + 2 * border_width;
3392 win_g.w = (r_g.w - msize -
3393 (stacks * 2 * border_width)) / stacks;
3395 win_g.w += (r_g.w - msize -
3396 (stacks * 2 * border_width)) % stacks;
3400 win_g.h = hrh - 2 * border_width;
3402 h_inc = win->sh.width_inc;
3403 h_base = win->sh.base_width;
3405 h_inc = win->sh.height_inc;
3406 h_base = win->sh.base_height;
3408 if (j == colno - 1) {
3409 win_g.h = hrh + extra;
3410 } else if (h_inc > 1 && h_inc < h_slice) {
3411 /* adjust for window's requested size increment */
3412 remain = (win_g.h - h_base) % h_inc;
3413 missing = h_inc - remain;
3415 if (missing <= extra || j == 0) {
3427 win_g.y += last_h + 2 * border_width;
3429 if (disable_border && bar_enabled == 0 && winno == 1){
3431 win_g.w += 2 * border_width;
3432 win_g.h += 2 * border_width;
3437 if (X(win) != win_g.y || Y(win) != win_g.x ||
3438 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3442 WIDTH(win) = win_g.h;
3443 HEIGHT(win) = win_g.w;
3446 if (X(win) != win_g.x || Y(win) != win_g.y ||
3447 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3451 WIDTH(win) = win_g.w;
3452 HEIGHT(win) = win_g.h;
3456 if (bordered != win->bordered) {
3458 win->bordered = bordered;
3466 war = xcb_get_window_attributes_reply(conn,
3467 xcb_get_window_attributes(conn, win->id),
3470 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3471 map_window_raised(win->id);
3481 /* now, stack all the floaters and transients */
3482 TAILQ_FOREACH(win, &ws->winlist, entry) {
3483 if (win->transient == 0 && win->floating == 0)
3485 if (win->iconic == 1)
3487 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3492 stack_floater(win, ws->r);
3493 map_window_raised(win->id);
3497 stack_floater(fs_win, ws->r);
3498 map_window_raised(fs_win->id);
3503 vertical_config(struct workspace *ws, int id)
3505 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3509 case SWM_ARG_ID_STACKRESET:
3510 case SWM_ARG_ID_STACKINIT:
3511 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3512 ws->l_state.vertical_mwin = 1;
3513 ws->l_state.vertical_stacks = 1;
3515 case SWM_ARG_ID_MASTERSHRINK:
3516 if (ws->l_state.vertical_msize > 1)
3517 ws->l_state.vertical_msize--;
3519 case SWM_ARG_ID_MASTERGROW:
3520 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3521 ws->l_state.vertical_msize++;
3523 case SWM_ARG_ID_MASTERADD:
3524 ws->l_state.vertical_mwin++;
3526 case SWM_ARG_ID_MASTERDEL:
3527 if (ws->l_state.vertical_mwin > 0)
3528 ws->l_state.vertical_mwin--;
3530 case SWM_ARG_ID_STACKINC:
3531 ws->l_state.vertical_stacks++;
3533 case SWM_ARG_ID_STACKDEC:
3534 if (ws->l_state.vertical_stacks > 1)
3535 ws->l_state.vertical_stacks--;
3537 case SWM_ARG_ID_FLIPLAYOUT:
3538 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3546 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3548 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3550 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3554 horizontal_config(struct workspace *ws, int id)
3556 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3559 case SWM_ARG_ID_STACKRESET:
3560 case SWM_ARG_ID_STACKINIT:
3561 ws->l_state.horizontal_mwin = 1;
3562 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3563 ws->l_state.horizontal_stacks = 1;
3565 case SWM_ARG_ID_MASTERSHRINK:
3566 if (ws->l_state.horizontal_msize > 1)
3567 ws->l_state.horizontal_msize--;
3569 case SWM_ARG_ID_MASTERGROW:
3570 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3571 ws->l_state.horizontal_msize++;
3573 case SWM_ARG_ID_MASTERADD:
3574 ws->l_state.horizontal_mwin++;
3576 case SWM_ARG_ID_MASTERDEL:
3577 if (ws->l_state.horizontal_mwin > 0)
3578 ws->l_state.horizontal_mwin--;
3580 case SWM_ARG_ID_STACKINC:
3581 ws->l_state.horizontal_stacks++;
3583 case SWM_ARG_ID_STACKDEC:
3584 if (ws->l_state.horizontal_stacks > 1)
3585 ws->l_state.horizontal_stacks--;
3587 case SWM_ARG_ID_FLIPLAYOUT:
3588 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3596 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3598 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3600 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3603 /* fullscreen view */
3605 max_stack(struct workspace *ws, struct swm_geometry *g)
3607 struct swm_geometry gg = *g;
3608 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3609 int winno, num_screens;
3611 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3616 winno = count_win(ws, 0);
3617 if (winno == 0 && count_win(ws, 1) == 0)
3620 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3621 TAILQ_FOREACH(win, &ws->winlist, entry) {
3622 if (win->transient) {
3624 parent = find_window(win->transient);
3628 if (win->floating && win->floatmaxed == 0 ) {
3630 * retain geometry for retrieval on exit from
3633 store_float_geom(win, ws->r);
3634 win->floatmaxed = 1;
3637 /* only reconfigure if necessary */
3638 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3639 HEIGHT(win) != gg.h) {
3645 WIDTH(win) += 2 * border_width;
3646 HEIGHT(win) += 2 * border_width;
3651 /* unmap only if we don't have multi screen */
3652 if (win != ws->focus)
3653 if (!(num_screens > 1 || outputs > 1))
3657 /* put the last transient on top */
3660 map_window_raised(parent->id);
3661 stack_floater(wintrans, ws->r);
3662 focus_magic(wintrans);
3667 send_to_ws(struct swm_region *r, union arg *args)
3669 int wsid = args->id;
3670 struct ws_win *win = NULL, *parent;
3671 struct workspace *ws, *nws;
3672 char ws_idx_str[SWM_PROPLEN];
3675 if (wsid >= workspace_limit)
3678 if (r && r->ws && r->ws->focus)
3684 if (win->ws->idx == wsid)
3687 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3690 nws = &win->s->ws[wsid];
3692 a.id = SWM_ARG_ID_FOCUSPREV;
3694 if (win->transient) {
3695 parent = find_window(win->transient);
3697 unmap_window(parent);
3698 TAILQ_REMOVE(&ws->winlist, parent, entry);
3699 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3704 TAILQ_REMOVE(&ws->winlist, win, entry);
3705 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3706 if (TAILQ_EMPTY(&ws->winlist))
3707 r->ws->focus = NULL;
3710 /* Try to update the window's workspace property */
3711 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3712 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3714 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3715 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3726 pressbutton(struct swm_region *r, union arg *args)
3728 /* suppress unused warning since var is needed */
3731 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3732 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3733 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3734 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3738 raise_toggle(struct swm_region *r, union arg *args)
3740 /* suppress unused warning since var is needed */
3743 if (r == NULL || r->ws == NULL)
3746 r->ws->always_raise = !r->ws->always_raise;
3748 /* bring floaters back to top */
3749 if (r->ws->always_raise == 0)
3756 iconify(struct swm_region *r, union arg *args)
3760 /* suppress unused warning since var is needed */
3763 if (r->ws->focus == NULL)
3765 unmap_window(r->ws->focus);
3766 update_iconic(r->ws->focus, 1);
3769 r->ws->focus = NULL;
3770 a.id = SWM_ARG_ID_FOCUSCUR;
3777 get_win_name(xcb_window_t win)
3780 xcb_get_property_cookie_t c;
3781 xcb_icccm_get_text_property_reply_t r;
3783 c = xcb_icccm_get_wm_name(conn, win);
3784 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3785 if (r.name_len > 0) {
3786 name = malloc(r.name_len + 1);
3788 memcpy(name, r.name, r.name_len);
3789 name[r.name_len] = '\0';
3792 xcb_icccm_get_text_property_reply_wipe(&r);
3799 uniconify(struct swm_region *r, union arg *args)
3806 DNPRINTF(SWM_D_MISC, "uniconify\n");
3808 if (r == NULL || r->ws == NULL)
3811 /* make sure we have anything to uniconify */
3812 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3813 if (win->ws == NULL)
3814 continue; /* should never happen */
3815 if (win->iconic == 0)
3823 search_resp_action = SWM_SEARCH_UNICONIFY;
3825 spawn_select(r, args, "search", &searchpid);
3827 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3830 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3831 if (win->ws == NULL)
3832 continue; /* should never happen */
3833 if (win->iconic == 0)
3836 name = get_win_name(win->id);
3839 fprintf(lfile, "%s.%u\n", name, win->id);
3849 name_workspace(struct swm_region *r, union arg *args)
3853 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3859 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3861 spawn_select(r, args, "name_workspace", &searchpid);
3863 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3866 fprintf(lfile, "%s", "");
3871 search_workspace(struct swm_region *r, union arg *args)
3874 struct workspace *ws;
3877 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3883 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3885 spawn_select(r, args, "search", &searchpid);
3887 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3890 for (i = 0; i < workspace_limit; i++) {
3894 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3895 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3902 search_win_cleanup(void)
3904 struct search_window *sw = NULL;
3906 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3907 xcb_destroy_window(conn, sw->indicator);
3908 xcb_free_gc(conn, sw->gc);
3909 TAILQ_REMOVE(&search_wl, sw, entry);
3915 search_win(struct swm_region *r, union arg *args)
3917 struct ws_win *win = NULL;
3918 struct search_window *sw = NULL;
3920 uint32_t gcv[3], wa[2];
3928 DNPRINTF(SWM_D_MISC, "search_win\n");
3931 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3933 spawn_select(r, args, "search", &searchpid);
3935 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3938 TAILQ_INIT(&search_wl);
3941 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3942 if (win->iconic == 1)
3945 sw = calloc(1, sizeof(struct search_window));
3947 warn("search_win: calloc");
3949 search_win_cleanup();
3955 snprintf(s, sizeof s, "%d", i);
3958 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
3960 w = xcb_generate_id(conn);
3961 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
3962 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
3963 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3964 info.width + 4, bar_font->height + 4,
3965 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3966 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3969 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3971 sw->gc = xcb_generate_id(conn);
3972 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
3973 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
3975 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3976 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3977 map_window_raised(w);
3979 draw = XftDrawCreate(display, w,
3980 DefaultVisual(display, r->s->idx),
3981 DefaultColormap(display, r->s->idx));
3983 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
3984 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
3987 XftDrawDestroy(draw);
3989 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3991 fprintf(lfile, "%d\n", i);
4001 search_resp_uniconify(char *resp, unsigned long len)
4007 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4009 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4010 if (win->iconic == 0)
4012 name = get_win_name(win->id);
4015 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4020 if (strncmp(s, resp, len) == 0) {
4021 /* XXX this should be a callback to generalize */
4022 update_iconic(win, 0);
4031 search_resp_name_workspace(char *resp, unsigned long len)
4033 struct workspace *ws;
4035 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4037 if (search_r->ws == NULL)
4042 free(search_r->ws->name);
4043 search_r->ws->name = NULL;
4047 ws->name = strdup(resp);
4048 if (ws->name == NULL) {
4049 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4050 "strdup: %s", strerror(errno));
4057 search_resp_search_workspace(char *resp)
4064 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4068 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4075 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4077 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4084 switchws(search_r, &a);
4088 search_resp_search_window(char *resp)
4093 struct search_window *sw;
4095 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4099 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4104 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4106 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4113 TAILQ_FOREACH(sw, &search_wl, entry)
4114 if (idx == sw->idx) {
4120 #define MAX_RESP_LEN 1024
4123 search_do_resp(void)
4129 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4134 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4135 warn("search: calloc");
4139 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4141 warn("search: read error");
4144 resp[rbytes] = '\0';
4147 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4148 * newline, so work around that by sanitizing the resp now.
4150 resp[strcspn(resp, "\n")] = '\0';
4153 switch (search_resp_action) {
4154 case SWM_SEARCH_UNICONIFY:
4155 search_resp_uniconify(resp, len);
4157 case SWM_SEARCH_NAME_WORKSPACE:
4158 search_resp_name_workspace(resp, len);
4160 case SWM_SEARCH_SEARCH_WORKSPACE:
4161 search_resp_search_workspace(resp);
4163 case SWM_SEARCH_SEARCH_WINDOW:
4164 search_resp_search_window(resp);
4169 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4170 search_win_cleanup();
4172 search_resp_action = SWM_SEARCH_NONE;
4173 close(select_resp_pipe[0]);
4178 wkill(struct swm_region *r, union arg *args)
4180 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4182 if (r->ws->focus == NULL)
4185 if (args->id == SWM_ARG_ID_KILLWINDOW)
4186 xcb_kill_client(conn, r->ws->focus->id);
4188 if (r->ws->focus->can_delete)
4189 client_msg(r->ws->focus, a_delete);
4196 floating_toggle_win(struct ws_win *win)
4198 struct swm_region *r;
4208 /* reject floating toggles in max stack mode */
4209 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4212 if (win->floating) {
4213 if (!win->floatmaxed) {
4214 /* retain position for refloat */
4215 store_float_geom(win, r);
4219 if (win->g_floatvalid) {
4220 /* refloat at last floating relative position */
4221 X(win) = win->g_float.x + X(r);
4222 Y(win) = win->g_float.y + Y(r);
4223 WIDTH(win) = win->g_float.w;
4224 HEIGHT(win) = win->g_float.h;
4229 ewmh_update_actions(win);
4235 floating_toggle(struct swm_region *r, union arg *args)
4237 struct ws_win *win = r->ws->focus;
4240 /* suppress unused warning since var is needed */
4246 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4247 _NET_WM_STATE_TOGGLE);
4251 if (win == win->ws->focus) {
4252 a.id = SWM_ARG_ID_FOCUSCUR;
4253 focus(win->ws->r, &a);
4257 event_drain(XCB_ENTER_NOTIFY);
4261 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4263 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4265 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4267 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4270 if (X(win) + BORDER(win) < X(r)) {
4272 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4274 X(win) = X(r) - BORDER(win);
4277 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4279 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4281 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4284 if (Y(win) + BORDER(win) < Y(r)) {
4286 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4288 Y(win) = Y(r) - BORDER(win);
4294 if (HEIGHT(win) < 1)
4300 update_window(struct ws_win *win)
4305 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4306 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4307 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4311 wc[3] = HEIGHT(win);
4312 wc[4] = BORDER(win);
4314 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4315 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4316 wc[3], YESNO(win->bordered));
4318 xcb_configure_window(conn, win->id, mask, wc);
4321 #define SWM_RESIZE_STEPS (50)
4324 resize(struct ws_win *win, union arg *args)
4326 xcb_timestamp_t timestamp = 0;
4327 struct swm_region *r = NULL;
4328 int resize_step = 0;
4329 struct swm_geometry g;
4330 int top = 0, left = 0, resizing;
4332 unsigned int shape; /* cursor style */
4333 xcb_cursor_t cursor;
4334 xcb_font_t cursor_font;
4335 xcb_query_pointer_reply_t *xpr;
4336 xcb_generic_event_t *evt;
4337 xcb_motion_notify_event_t *mne;
4343 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4346 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4347 "transient: 0x%x\n", win->id, YESNO(win->floating),
4350 if (!(win->transient != 0 || win->floating != 0))
4353 /* reject resizes in max mode for floaters (transient ok) */
4354 if (win->floatmaxed)
4358 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4364 case SWM_ARG_ID_WIDTHSHRINK:
4365 WIDTH(win) -= SWM_RESIZE_STEPS;
4368 case SWM_ARG_ID_WIDTHGROW:
4369 WIDTH(win) += SWM_RESIZE_STEPS;
4372 case SWM_ARG_ID_HEIGHTSHRINK:
4373 HEIGHT(win) -= SWM_RESIZE_STEPS;
4376 case SWM_ARG_ID_HEIGHTGROW:
4377 HEIGHT(win) += SWM_RESIZE_STEPS;
4384 constrain_window(win, r, 1);
4386 store_float_geom(win,r);
4390 /* get cursor offset from window root */
4391 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4398 if (xpr->win_x < WIDTH(win) / 2)
4401 if (xpr->win_y < HEIGHT(win) / 2)
4404 if (args->id == SWM_ARG_ID_CENTER)
4407 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4409 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4411 cursor_font = xcb_generate_id(conn);
4412 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4414 cursor = xcb_generate_id(conn);
4415 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4416 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4418 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4419 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4424 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4425 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4426 case XCB_BUTTON_RELEASE:
4427 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4430 case XCB_MOTION_NOTIFY:
4431 mne = (xcb_motion_notify_event_t *)evt;
4432 /* cursor offset/delta from start of the operation */
4433 dx = mne->root_x - xpr->root_x;
4434 dy = mne->root_y - xpr->root_y;
4440 if (args->id == SWM_ARG_ID_CENTER) {
4441 if (g.h / 2 + dy < 1)
4445 HEIGHT(win) = g.h + 2 * dy;
4453 HEIGHT(win) = g.h + dy;
4460 if (args->id == SWM_ARG_ID_CENTER) {
4461 if (g.w / 2 + dx < 1)
4465 WIDTH(win) = g.w + 2 * dx;
4473 WIDTH(win) = g.w + dx;
4476 constrain_window(win, r, 1);
4478 /* not free, don't sync more than 120 times / second */
4479 if ((mne->time - timestamp) > (1000 / 120) ) {
4480 timestamp = mne->time;
4495 store_float_geom(win,r);
4497 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4498 xcb_free_cursor(conn, cursor);
4499 xcb_close_font(conn, cursor_font);
4501 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4505 resize_step(struct swm_region *r, union arg *args)
4507 struct ws_win *win = NULL;
4509 if (r && r->ws && r->ws->focus)
4517 #define SWM_MOVE_STEPS (50)
4520 move(struct ws_win *win, union arg *args)
4522 xcb_timestamp_t timestamp = 0;
4523 int move_step = 0, moving;
4524 struct swm_region *r = NULL;
4525 xcb_font_t cursor_font;
4526 xcb_cursor_t cursor;
4527 xcb_query_pointer_reply_t *qpr;
4528 xcb_generic_event_t *evt;
4529 xcb_motion_notify_event_t *mne;
4535 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4538 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4539 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4541 /* in max_stack mode should only move transients */
4542 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4546 if (win->floating == 0 && !win->transient) {
4547 store_float_geom(win, r);
4548 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4551 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4558 case SWM_ARG_ID_MOVELEFT:
4559 X(win) -= (SWM_MOVE_STEPS - border_width);
4562 case SWM_ARG_ID_MOVERIGHT:
4563 X(win) += (SWM_MOVE_STEPS - border_width);
4566 case SWM_ARG_ID_MOVEUP:
4567 Y(win) -= (SWM_MOVE_STEPS - border_width);
4570 case SWM_ARG_ID_MOVEDOWN:
4571 Y(win) += (SWM_MOVE_STEPS - border_width);
4578 constrain_window(win, r, 0);
4580 store_float_geom(win, r);
4584 cursor_font = xcb_generate_id(conn);
4585 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4587 cursor = xcb_generate_id(conn);
4588 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4589 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4591 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4592 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4593 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4595 /* get cursor offset from window root */
4596 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4599 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4600 xcb_free_cursor(conn, cursor);
4601 xcb_close_font(conn, cursor_font);
4607 while ((evt = xcb_wait_for_event(conn)) && moving) {
4608 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4609 case XCB_BUTTON_RELEASE:
4610 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4613 case XCB_MOTION_NOTIFY:
4614 mne = (xcb_motion_notify_event_t *)evt;
4615 X(win) = mne->root_x - qpr->win_x - border_width;
4616 Y(win) = mne->root_y - qpr->win_y - border_width;
4618 constrain_window(win, r, 0);
4620 /* not free, don't sync more than 120 times / second */
4621 if ((mne->time - timestamp) > (1000 / 120) ) {
4622 timestamp = mne->time;
4637 store_float_geom(win, r);
4639 xcb_free_cursor(conn, cursor);
4640 xcb_close_font(conn, cursor_font);
4641 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4642 DNPRINTF(SWM_D_EVENT, "move: done\n");
4646 move_step(struct swm_region *r, union arg *args)
4648 struct ws_win *win = NULL;
4650 if (r && r->ws && r->ws->focus)
4655 if (!(win->transient != 0 || win->floating != 0))
4662 /* user/key callable function IDs */
4712 KF_SEARCH_WORKSPACE,
4753 KF_DUMPWINS, /* MUST BE LAST */
4757 /* key definitions */
4759 char name[SWM_FUNCNAME_LEN];
4760 void (*func)(struct swm_region *r, union arg *);
4762 } keyfuncs[KF_INVALID + 1] = {
4763 /* name function argument */
4764 { "bar_toggle", bar_toggle, {0} },
4765 { "button2", pressbutton, {2} },
4766 { "cycle_layout", cycle_layout, {0} },
4767 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4768 { "float_toggle", floating_toggle,{0} },
4769 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4770 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4771 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4772 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4773 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4774 { "iconify", iconify, {0} },
4775 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4776 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4777 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4778 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4779 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4780 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4781 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4782 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4783 { "mvws_1", send_to_ws, {.id = 0} },
4784 { "mvws_2", send_to_ws, {.id = 1} },
4785 { "mvws_3", send_to_ws, {.id = 2} },
4786 { "mvws_4", send_to_ws, {.id = 3} },
4787 { "mvws_5", send_to_ws, {.id = 4} },
4788 { "mvws_6", send_to_ws, {.id = 5} },
4789 { "mvws_7", send_to_ws, {.id = 6} },
4790 { "mvws_8", send_to_ws, {.id = 7} },
4791 { "mvws_9", send_to_ws, {.id = 8} },
4792 { "mvws_10", send_to_ws, {.id = 9} },
4793 { "mvws_11", send_to_ws, {.id = 10} },
4794 { "mvws_12", send_to_ws, {.id = 11} },
4795 { "mvws_13", send_to_ws, {.id = 12} },
4796 { "mvws_14", send_to_ws, {.id = 13} },
4797 { "mvws_15", send_to_ws, {.id = 14} },
4798 { "mvws_16", send_to_ws, {.id = 15} },
4799 { "mvws_17", send_to_ws, {.id = 16} },
4800 { "mvws_18", send_to_ws, {.id = 17} },
4801 { "mvws_19", send_to_ws, {.id = 18} },
4802 { "mvws_20", send_to_ws, {.id = 19} },
4803 { "mvws_21", send_to_ws, {.id = 20} },
4804 { "mvws_22", send_to_ws, {.id = 21} },
4805 { "name_workspace", name_workspace, {0} },
4806 { "quit", quit, {0} },
4807 { "raise_toggle", raise_toggle, {0} },
4808 { "restart", restart, {0} },
4809 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4810 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4811 { "search_win", search_win, {0} },
4812 { "search_workspace", search_workspace, {0} },
4813 { "spawn_custom", NULL, {0} },
4814 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4815 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4816 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4817 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4818 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4819 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4820 { "uniconify", uniconify, {0} },
4821 { "version", version, {0} },
4822 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4823 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4824 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4825 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4826 { "ws_1", switchws, {.id = 0} },
4827 { "ws_2", switchws, {.id = 1} },
4828 { "ws_3", switchws, {.id = 2} },
4829 { "ws_4", switchws, {.id = 3} },
4830 { "ws_5", switchws, {.id = 4} },
4831 { "ws_6", switchws, {.id = 5} },
4832 { "ws_7", switchws, {.id = 6} },
4833 { "ws_8", switchws, {.id = 7} },
4834 { "ws_9", switchws, {.id = 8} },
4835 { "ws_10", switchws, {.id = 9} },
4836 { "ws_11", switchws, {.id = 10} },
4837 { "ws_12", switchws, {.id = 11} },
4838 { "ws_13", switchws, {.id = 12} },
4839 { "ws_14", switchws, {.id = 13} },
4840 { "ws_15", switchws, {.id = 14} },
4841 { "ws_16", switchws, {.id = 15} },
4842 { "ws_17", switchws, {.id = 16} },
4843 { "ws_18", switchws, {.id = 17} },
4844 { "ws_19", switchws, {.id = 18} },
4845 { "ws_20", switchws, {.id = 19} },
4846 { "ws_21", switchws, {.id = 20} },
4847 { "ws_22", switchws, {.id = 21} },
4848 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4849 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4850 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4851 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4852 { "ws_prior", priorws, {0} },
4853 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4854 { "invalid key func", NULL, {0} },
4857 RB_ENTRY(key) entry;
4860 enum keyfuncid funcid;
4863 RB_HEAD(key_tree, key);
4866 key_cmp(struct key *kp1, struct key *kp2)
4868 if (kp1->keysym < kp2->keysym)
4870 if (kp1->keysym > kp2->keysym)
4873 if (kp1->mod < kp2->mod)
4875 if (kp1->mod > kp2->mod)
4881 RB_GENERATE(key_tree, key, entry, key_cmp);
4882 struct key_tree keys;
4885 enum { client_click, root_click };
4887 unsigned int action;
4889 unsigned int button;
4890 void (*func)(struct ws_win *, union arg *);
4893 /* action key mouse button func args */
4894 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4895 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4896 { client_click, MODKEY, Button1, move, {0} },
4900 update_modkey(unsigned int mod)
4906 RB_FOREACH(kp, key_tree, &keys)
4907 if (kp->mod & ShiftMask)
4908 kp->mod = mod | ShiftMask;
4912 for (i = 0; i < LENGTH(buttons); i++)
4913 if (buttons[i].mask & ShiftMask)
4914 buttons[i].mask = mod | ShiftMask;
4916 buttons[i].mask = mod;
4921 TAILQ_ENTRY(spawn_prog) entry;
4926 TAILQ_HEAD(spawn_list, spawn_prog);
4927 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4930 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4933 struct spawn_prog *prog = NULL;
4935 char *ap, **real_args;
4937 /* suppress unused warning since var is needed */
4940 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4943 TAILQ_FOREACH(prog, &spawns, entry) {
4944 if (!strcasecmp(spawn_name, prog->name))
4948 warnx("spawn_custom: program %s not found", spawn_name);
4952 /* make room for expanded args */
4953 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4954 err(1, "spawn_custom: calloc real_args");
4956 /* expand spawn_args into real_args */
4957 for (i = 0; i < prog->argc; i++) {
4959 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4960 if (!strcasecmp(ap, "$bar_border")) {
4962 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4964 err(1, "spawn_custom border color");
4965 } else if (!strcasecmp(ap, "$bar_color")) {
4967 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4969 err(1, "spawn_custom bar color");
4970 } else if (!strcasecmp(ap, "$bar_font")) {
4971 if ((real_args[i] = strdup(bar_fonts))
4973 err(1, "spawn_custom bar fonts");
4974 } else if (!strcasecmp(ap, "$bar_font_color")) {
4976 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4978 err(1, "spawn_custom color font");
4979 } else if (!strcasecmp(ap, "$color_focus")) {
4981 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4983 err(1, "spawn_custom color focus");
4984 } else if (!strcasecmp(ap, "$color_unfocus")) {
4986 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4988 err(1, "spawn_custom color unfocus");
4990 /* no match --> copy as is */
4991 if ((real_args[i] = strdup(ap)) == NULL)
4992 err(1, "spawn_custom strdup(ap)");
4994 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4999 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5000 for (i = 0; i < prog->argc; i++)
5001 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5002 DNPRINTF(SWM_D_SPAWN, "\n");
5004 *ret_args = real_args;
5005 return (prog->argc);
5009 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5015 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5019 spawn(r->ws->idx, &a, 1);
5021 for (i = 0; i < spawn_argc; i++)
5027 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5033 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5037 if (pipe(select_list_pipe) == -1)
5038 err(1, "pipe error");
5039 if (pipe(select_resp_pipe) == -1)
5040 err(1, "pipe error");
5042 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5043 err(1, "could not disable SIGPIPE");
5044 switch (*pid = fork()) {
5046 err(1, "cannot fork");
5049 if (dup2(select_list_pipe[0], 0) == -1)
5051 if (dup2(select_resp_pipe[1], 1) == -1)
5053 close(select_list_pipe[1]);
5054 close(select_resp_pipe[0]);
5055 spawn(r->ws->idx, &a, 0);
5057 default: /* parent */
5058 close(select_list_pipe[0]);
5059 close(select_resp_pipe[1]);
5063 for (i = 0; i < spawn_argc; i++)
5069 spawn_insert(char *name, char *args)
5071 char *arg, *cp, *ptr;
5072 struct spawn_prog *sp;
5074 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5076 if ((sp = calloc(1, sizeof *sp)) == NULL)
5077 err(1, "spawn_insert: calloc");
5078 if ((sp->name = strdup(name)) == NULL)
5079 err(1, "spawn_insert: strdup");
5081 /* convert the arguments to an argument list */
5082 if ((ptr = cp = strdup(args)) == NULL)
5083 err(1, "spawn_insert: strdup");
5084 while ((arg = strsep(&ptr, " \t")) != NULL) {
5085 /* empty field; skip it */
5090 if ((sp->argv = realloc(sp->argv, sp->argc *
5091 sizeof *sp->argv)) == NULL)
5092 err(1, "spawn_insert: realloc");
5093 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5094 err(1, "spawn_insert: strdup");
5098 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5099 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5103 spawn_remove(struct spawn_prog *sp)
5107 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5109 TAILQ_REMOVE(&spawns, sp, entry);
5110 for (i = 0; i < sp->argc; i++)
5116 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5120 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5122 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5125 spawn_insert(name, args);
5127 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5131 setspawn(char *name, char *args)
5133 struct spawn_prog *sp;
5135 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5140 TAILQ_FOREACH(sp, &spawns, entry) {
5141 if (!strcmp(sp->name, name)) {
5145 spawn_replace(sp, name, args);
5146 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5150 if (*args == '\0') {
5151 warnx("error: setspawn: cannot find program: %s", name);
5155 spawn_insert(name, args);
5156 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5160 setconfspawn(char *selector, char *value, int flags)
5164 /* suppress unused warning since var is needed */
5167 args = expand_tilde(value);
5169 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5171 setspawn(selector, args);
5174 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5181 setconfspawn("term", "xterm", 0);
5182 setconfspawn("spawn_term", "xterm", 0);
5183 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5184 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5185 setconfspawn("lock", "xlock", 0);
5186 setconfspawn("initscr", "initscreen.sh", 0);
5187 setconfspawn("menu", "dmenu_run"
5190 " -nf $bar_font_color"
5192 " -sf $bar_color", 0);
5193 setconfspawn("search", "dmenu"
5197 " -nf $bar_font_color"
5199 " -sf $bar_color", 0);
5200 setconfspawn("name_workspace", "dmenu"
5204 " -nf $bar_font_color"
5206 " -sf $bar_color", 0);
5210 #define SWM_MODNAME_SIZE 32
5211 #define SWM_KEY_WS "\n+ \t"
5213 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5217 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5218 if (mod == NULL || ks == NULL) {
5219 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5222 if (keystr == NULL || strlen(keystr) == 0) {
5223 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5227 *ks = XCB_NO_SYMBOL;
5229 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5230 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5232 cp += (long)strspn(cp, SWM_KEY_WS);
5233 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5235 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5237 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5239 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5241 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5243 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5245 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5246 *mod |= ControlMask;
5248 *ks = XStringToKeysym(name);
5249 XConvertCase(*ks, ks, &uks);
5250 if (ks == XCB_NO_SYMBOL) {
5252 "parsekeys: invalid key %s\n",
5258 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5263 strdupsafe(char *str)
5268 return (strdup(str));
5272 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5276 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5277 keyfuncs[kfid].name, spawn_name);
5279 if ((kp = malloc(sizeof *kp)) == NULL)
5280 err(1, "key_insert: malloc");
5285 kp->spawn_name = strdupsafe(spawn_name);
5286 RB_INSERT(key_tree, &keys, kp);
5288 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5292 key_lookup(unsigned int mod, KeySym ks)
5299 return (RB_FIND(key_tree, &keys, &kp));
5303 key_remove(struct key *kp)
5305 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5307 RB_REMOVE(key_tree, &keys, kp);
5308 free(kp->spawn_name);
5311 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5315 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5318 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5322 key_insert(mod, ks, kfid, spawn_name);
5324 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5328 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5333 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5334 keyfuncs[kfid].name, spawn_name);
5336 if ((kp = key_lookup(mod, ks)) != NULL) {
5337 if (kfid == KF_INVALID)
5340 key_replace(kp, mod, ks, kfid, spawn_name);
5341 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5344 if (kfid == KF_INVALID) {
5345 warnx("error: setkeybinding: cannot find mod/key combination");
5346 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5350 key_insert(mod, ks, kfid, spawn_name);
5351 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5355 setconfbinding(char *selector, char *value, int flags)
5357 enum keyfuncid kfid;
5360 struct spawn_prog *sp;
5362 /* suppress unused warning since var is needed */
5365 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5366 if (selector == NULL) {
5367 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5368 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5370 setkeybinding(mod, ks, kfid, NULL);
5375 /* search by key function name */
5376 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5377 if (strncasecmp(selector, keyfuncs[kfid].name,
5378 SWM_FUNCNAME_LEN) == 0) {
5379 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5381 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5382 setkeybinding(mod, ks, kfid, NULL);
5388 /* search by custom spawn name */
5389 TAILQ_FOREACH(sp, &spawns, entry) {
5390 if (strcasecmp(selector, sp->name) == 0) {
5391 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5393 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5394 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5401 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5408 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5409 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5410 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5411 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5412 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5413 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5414 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5415 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5416 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5417 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5418 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5419 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5420 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5421 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5422 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5423 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5424 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5425 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5426 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5427 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5428 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5429 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5430 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5431 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5432 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5433 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5434 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5435 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5436 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5437 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5438 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5439 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5440 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5441 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5442 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5443 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5444 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5445 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5446 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5447 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5448 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5449 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5450 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5451 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5452 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5453 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5454 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5455 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5456 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5457 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5458 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5459 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5460 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5461 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5462 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5463 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5464 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5465 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5466 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5467 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5468 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5469 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5470 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5471 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5472 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5473 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5474 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5475 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5476 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5477 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5478 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5479 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5480 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5481 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5482 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5483 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5484 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5485 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5486 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5487 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5488 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5489 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5490 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5491 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5492 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5493 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5494 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5495 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5496 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5497 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5498 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5499 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5500 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5501 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5502 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5503 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5505 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5514 while (RB_EMPTY(&keys) == 0) {
5515 kp = RB_ROOT(&keys);
5521 setkeymapping(char *selector, char *value, int flags)
5523 char keymapping_file[PATH_MAX];
5525 /* suppress unused warnings since vars are needed */
5529 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5530 if (value[0] == '~')
5531 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5532 pwd->pw_dir, &value[1]);
5534 strlcpy(keymapping_file, value, sizeof keymapping_file);
5536 /* load new key bindings; if it fails, revert to default bindings */
5537 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5541 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5546 updatenumlockmask(void)
5549 xcb_get_modifier_mapping_reply_t *modmap_r;
5550 xcb_keycode_t *modmap, kc, *keycode;
5552 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5555 modmap_r = xcb_get_modifier_mapping_reply(conn,
5556 xcb_get_modifier_mapping(conn),
5559 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5560 for (i = 0; i < 8; i++) {
5561 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5562 kc = modmap[i * modmap_r->keycodes_per_modifier
5564 keycode = xcb_key_symbols_get_keycode(syms,
5567 numlockmask = (1 << i);
5580 xcb_keycode_t *code;
5581 unsigned int modifiers[] =
5582 { 0, LockMask, numlockmask, numlockmask | LockMask };
5585 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5586 updatenumlockmask();
5588 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5589 for (k = 0; k < num_screens; k++) {
5590 if (TAILQ_EMPTY(&screens[k].rl))
5592 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5594 RB_FOREACH(kp, key_tree, &keys) {
5595 if ((code = xcb_key_symbols_get_keycode(syms,
5597 for (j = 0; j < LENGTH(modifiers); j++)
5598 xcb_grab_key(conn, 1,
5600 kp->mod | modifiers[j],
5601 *code, XCB_GRAB_MODE_ASYNC,
5602 XCB_GRAB_MODE_ASYNC);
5609 grabbuttons(struct ws_win *win, int focused)
5612 unsigned int modifiers[] =
5613 { 0, LockMask, numlockmask, numlockmask|LockMask };
5615 updatenumlockmask();
5616 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5617 XCB_BUTTON_MASK_ANY);
5619 for (i = 0; i < LENGTH(buttons); i++)
5620 if (buttons[i].action == client_click)
5621 for (j = 0; j < LENGTH(modifiers); j++)
5622 xcb_grab_button(conn, 0, win->id,
5624 XCB_GRAB_MODE_ASYNC,
5631 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5632 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5633 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5636 const char *quirkname[] = {
5637 "NONE", /* config string for "no value" */
5646 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5647 #define SWM_Q_WS "\n|+ \t"
5649 parsequirks(char *qstr, unsigned long *quirk)
5659 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5661 cp += (long)strspn(cp, SWM_Q_WS);
5662 for (i = 0; i < LENGTH(quirkname); i++) {
5663 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5664 DNPRINTF(SWM_D_QUIRK,
5665 "parsequirks: %s\n", name);
5670 *quirk |= 1 << (i-1);
5674 if (i >= LENGTH(quirkname)) {
5675 DNPRINTF(SWM_D_QUIRK,
5676 "parsequirks: invalid quirk [%s]\n", name);
5684 quirk_insert(const char *class, const char *name, unsigned long quirk)
5688 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5691 if ((qp = malloc(sizeof *qp)) == NULL)
5692 err(1, "quirk_insert: malloc");
5693 if ((qp->class = strdup(class)) == NULL)
5694 err(1, "quirk_insert: strdup");
5695 if ((qp->name = strdup(name)) == NULL)
5696 err(1, "quirk_insert: strdup");
5699 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5701 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5705 quirk_remove(struct quirk *qp)
5707 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5708 qp->name, qp->quirk);
5710 TAILQ_REMOVE(&quirks, qp, entry);
5715 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5719 quirk_replace(struct quirk *qp, const char *class, const char *name,
5720 unsigned long quirk)
5722 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5723 qp->name, qp->quirk);
5726 quirk_insert(class, name, quirk);
5728 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5732 setquirk(const char *class, const char *name, unsigned long quirk)
5736 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5739 TAILQ_FOREACH(qp, &quirks, entry) {
5740 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5744 quirk_replace(qp, class, name, quirk);
5745 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5750 warnx("error: setquirk: cannot find class/name combination");
5754 quirk_insert(class, name, quirk);
5755 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5759 setconfquirk(char *selector, char *value, int flags)
5761 char *cp, *class, *name;
5763 unsigned long quirks;
5765 /* suppress unused warning since var is needed */
5768 if (selector == NULL)
5770 if ((cp = strchr(selector, ':')) == NULL)
5775 if ((retval = parsequirks(value, &quirks)) == 0)
5776 setquirk(class, name, quirks);
5783 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5784 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5785 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5786 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5787 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5788 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5789 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5790 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5791 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5792 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5793 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5794 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5795 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5798 /* conf file stuff */
5799 #define SWM_CONF_FILE "spectrwm.conf"
5800 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5804 SWM_S_BAR_AT_BOTTOM,
5805 SWM_S_BAR_BORDER_WIDTH,
5812 SWM_S_CLOCK_ENABLED,
5815 SWM_S_CYCLE_VISIBLE,
5817 SWM_S_DISABLE_BORDER,
5819 SWM_S_FOCUS_CLOSE_WRAP,
5820 SWM_S_FOCUS_DEFAULT,
5826 SWM_S_STACK_ENABLED,
5828 SWM_S_TITLE_CLASS_ENABLED,
5829 SWM_S_TITLE_NAME_ENABLED,
5830 SWM_S_URGENT_ENABLED,
5831 SWM_S_VERBOSE_LAYOUT,
5832 SWM_S_WINDOW_NAME_ENABLED,
5833 SWM_S_WORKSPACE_LIMIT
5837 setconfvalue(char *selector, char *value, int flags)
5842 /* suppress unused warning since var is needed */
5846 case SWM_S_BAR_ACTION:
5848 if ((bar_argv[0] = expand_tilde(value)) == NULL)
5849 err(1, "setconfvalue: bar_action");
5851 case SWM_S_BAR_AT_BOTTOM:
5852 bar_at_bottom = atoi(value);
5854 case SWM_S_BAR_BORDER_WIDTH:
5855 bar_border_width = atoi(value);
5856 if (bar_border_width < 0)
5857 bar_border_width = 0;
5859 case SWM_S_BAR_DELAY:
5860 bar_delay = atoi(value);
5862 case SWM_S_BAR_ENABLED:
5863 bar_enabled = atoi(value);
5865 case SWM_S_BAR_FONT:
5867 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5868 err(1, "setconfvalue: asprintf: failed to allocate "
5869 "memory for bar_fonts.");
5872 case SWM_S_BAR_FORMAT:
5874 if ((bar_format = strdup(value)) == NULL)
5875 err(1, "setconfvalue: bar_format");
5877 case SWM_S_BAR_JUSTIFY:
5878 if (!strcmp(value, "left"))
5879 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5880 else if (!strcmp(value, "center"))
5881 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5882 else if (!strcmp(value, "right"))
5883 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5885 errx(1, "invalid bar_justify");
5887 case SWM_S_BORDER_WIDTH:
5888 border_width = atoi(value);
5889 if (border_width < 0)
5892 case SWM_S_CLOCK_ENABLED:
5893 clock_enabled = atoi(value);
5895 case SWM_S_CLOCK_FORMAT:
5896 #ifndef SWM_DENY_CLOCK_FORMAT
5898 if ((clock_format = strdup(value)) == NULL)
5899 err(1, "setconfvalue: clock_format");
5902 case SWM_S_CYCLE_EMPTY:
5903 cycle_empty = atoi(value);
5905 case SWM_S_CYCLE_VISIBLE:
5906 cycle_visible = atoi(value);
5908 case SWM_S_DIALOG_RATIO:
5909 dialog_ratio = atof(value);
5910 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5913 case SWM_S_DISABLE_BORDER:
5914 disable_border = atoi(value);
5916 case SWM_S_FOCUS_CLOSE:
5917 if (!strcmp(value, "first"))
5918 focus_close = SWM_STACK_BOTTOM;
5919 else if (!strcmp(value, "last"))
5920 focus_close = SWM_STACK_TOP;
5921 else if (!strcmp(value, "next"))
5922 focus_close = SWM_STACK_ABOVE;
5923 else if (!strcmp(value, "previous"))
5924 focus_close = SWM_STACK_BELOW;
5926 errx(1, "focus_close");
5928 case SWM_S_FOCUS_CLOSE_WRAP:
5929 focus_close_wrap = atoi(value);
5931 case SWM_S_FOCUS_DEFAULT:
5932 if (!strcmp(value, "last"))
5933 focus_default = SWM_STACK_TOP;
5934 else if (!strcmp(value, "first"))
5935 focus_default = SWM_STACK_BOTTOM;
5937 errx(1, "focus_default");
5939 case SWM_S_FOCUS_MODE:
5940 if (!strcmp(value, "default"))
5941 focus_mode = SWM_FOCUS_DEFAULT;
5942 else if (!strcmp(value, "follow_cursor"))
5943 focus_mode = SWM_FOCUS_FOLLOW;
5944 else if (!strcmp(value, "synergy"))
5945 focus_mode = SWM_FOCUS_SYNERGY;
5947 errx(1, "focus_mode");
5949 case SWM_S_SPAWN_ORDER:
5950 if (!strcmp(value, "first"))
5951 spawn_position = SWM_STACK_BOTTOM;
5952 else if (!strcmp(value, "last"))
5953 spawn_position = SWM_STACK_TOP;
5954 else if (!strcmp(value, "next"))
5955 spawn_position = SWM_STACK_ABOVE;
5956 else if (!strcmp(value, "previous"))
5957 spawn_position = SWM_STACK_BELOW;
5959 errx(1, "spawn_position");
5961 case SWM_S_SPAWN_TERM:
5962 setconfspawn("term", value, 0);
5963 setconfspawn("spawn_term", value, 0);
5967 case SWM_S_SS_ENABLED:
5968 ss_enabled = atoi(value);
5970 case SWM_S_STACK_ENABLED:
5971 stack_enabled = atoi(value);
5973 case SWM_S_TERM_WIDTH:
5974 term_width = atoi(value);
5978 case SWM_S_TITLE_CLASS_ENABLED:
5979 title_class_enabled = atoi(value);
5981 case SWM_S_TITLE_NAME_ENABLED:
5982 title_name_enabled = atoi(value);
5984 case SWM_S_URGENT_ENABLED:
5985 urgent_enabled = atoi(value);
5987 case SWM_S_VERBOSE_LAYOUT:
5988 verbose_layout = atoi(value);
5989 for (i = 0; layouts[i].l_stack != NULL; i++) {
5991 layouts[i].l_string = fancy_stacker;
5993 layouts[i].l_string = plain_stacker;
5996 case SWM_S_WINDOW_NAME_ENABLED:
5997 window_name_enabled = atoi(value);
5999 case SWM_S_WORKSPACE_LIMIT:
6000 workspace_limit = atoi(value);
6001 if (workspace_limit > SWM_WS_MAX)
6002 workspace_limit = SWM_WS_MAX;
6003 else if (workspace_limit < 1)
6004 workspace_limit = 1;
6013 setconfmodkey(char *selector, char *value, int flags)
6015 /* suppress unused warnings since vars are needed */
6019 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6020 update_modkey(Mod1Mask);
6021 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6022 update_modkey(Mod2Mask);
6023 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6024 update_modkey(Mod3Mask);
6025 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6026 update_modkey(Mod4Mask);
6033 setconfcolor(char *selector, char *value, int flags)
6035 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6040 setconfregion(char *selector, char *value, int flags)
6042 /* suppress unused warnings since vars are needed */
6046 custom_region(value);
6051 setautorun(char *selector, char *value, int flags)
6061 /* suppress unused warnings since vars are needed */
6065 if (getenv("SWM_STARTED"))
6069 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6070 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6072 if (ws_id < 0 || ws_id >= workspace_limit)
6073 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6076 * This is a little intricate
6078 * If the pid already exists we simply reuse it because it means it was
6079 * used before AND not claimed by manage_window. We get away with
6080 * altering it in the parent after INSERT because this can not be a race
6083 while ((ap = strsep(&sp, " \t")) != NULL) {
6086 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6088 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6089 err(1, "setautorun: realloc");
6090 a.argv[argc - 1] = ap;
6093 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6094 err(1, "setautorun: realloc");
6095 a.argv[argc] = NULL;
6097 if ((pid = fork()) == 0) {
6098 spawn(ws_id, &a, 1);
6107 p = calloc(1, sizeof *p);
6110 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6120 setlayout(char *selector, char *value, int flags)
6122 int ws_id, i, x, mg, ma, si, raise, f = 0;
6123 int st = SWM_V_STACK, num_screens;
6125 struct workspace *ws;
6127 /* suppress unused warnings since vars are needed */
6131 if (getenv("SWM_STARTED"))
6135 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6136 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6137 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6138 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6141 if (ws_id < 0 || ws_id >= workspace_limit)
6142 errx(1, "layout: invalid workspace %d", ws_id + 1);
6144 if (!strcasecmp(s, "vertical"))
6146 else if (!strcasecmp(s, "vertical_flip")) {
6149 } else if (!strcasecmp(s, "horizontal"))
6151 else if (!strcasecmp(s, "horizontal_flip")) {
6154 } else if (!strcasecmp(s, "fullscreen"))
6157 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6158 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6161 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6162 for (i = 0; i < num_screens; i++) {
6163 ws = (struct workspace *)&screens[i].ws;
6164 ws[ws_id].cur_layout = &layouts[st];
6166 ws[ws_id].always_raise = raise;
6167 if (st == SWM_MAX_STACK)
6171 for (x = 0; x < abs(mg); x++) {
6172 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6173 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6174 SWM_ARG_ID_MASTERSHRINK);
6178 for (x = 0; x < abs(ma); x++) {
6179 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6180 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6181 SWM_ARG_ID_MASTERDEL);
6185 for (x = 0; x < abs(si); x++) {
6186 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6187 si >= 0 ? SWM_ARG_ID_STACKINC :
6188 SWM_ARG_ID_STACKDEC);
6193 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6194 SWM_ARG_ID_FLIPLAYOUT);
6202 /* config options */
6203 struct config_option {
6205 int (*func)(char*, char*, int);
6208 struct config_option configopt[] = {
6209 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6210 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6211 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6212 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6213 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6214 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6215 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6216 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6217 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6218 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6219 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6220 { "keyboard_mapping", setkeymapping, 0 },
6221 { "bind", setconfbinding, 0 },
6222 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6223 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6224 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6225 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6226 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6227 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6228 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6229 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6230 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6231 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6232 { "modkey", setconfmodkey, 0 },
6233 { "program", setconfspawn, 0 },
6234 { "quirk", setconfquirk, 0 },
6235 { "region", setconfregion, 0 },
6236 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6237 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6238 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6239 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6240 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6241 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6242 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6243 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6244 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6245 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6246 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6247 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6248 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6249 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6250 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6251 { "autorun", setautorun, 0 },
6252 { "layout", setlayout, 0 },
6257 conf_load(char *filename, int keymapping)
6260 char *line, *cp, *optsub, *optval;
6261 size_t linelen, lineno = 0;
6262 int wordlen, i, optind;
6263 struct config_option *opt;
6265 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6267 if (filename == NULL) {
6268 warnx("conf_load: no filename");
6271 if ((config = fopen(filename, "r")) == NULL) {
6272 warn("conf_load: fopen: %s", filename);
6276 while (!feof(config)) {
6277 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6280 err(1, "%s", filename);
6285 cp += strspn(cp, " \t\n"); /* eat whitespace */
6286 if (cp[0] == '\0') {
6291 /* get config option */
6292 wordlen = strcspn(cp, "=[ \t\n");
6294 warnx("%s: line %zd: no option found",
6299 for (i = 0; i < LENGTH(configopt); i++) {
6300 opt = &configopt[i];
6301 if (!strncasecmp(cp, opt->optname, wordlen) &&
6302 (int)strlen(opt->optname) == wordlen) {
6308 warnx("%s: line %zd: unknown option %.*s",
6309 filename, lineno, wordlen, cp);
6312 if (keymapping && strcmp(opt->optname, "bind")) {
6313 warnx("%s: line %zd: invalid option %.*s",
6314 filename, lineno, wordlen, cp);
6318 cp += strspn(cp, " \t\n"); /* eat whitespace */
6319 /* get [selector] if any */
6323 wordlen = strcspn(cp, "]");
6326 warnx("%s: line %zd: syntax error",
6331 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6333 warnx("%s: line %zd: unable to allocate"
6334 "memory for selector", filename,
6340 cp += strspn(cp, "] \t\n"); /* eat trailing */
6342 cp += strspn(cp, "= \t\n"); /* eat trailing */
6344 optval = strdup(cp);
6345 /* call function to deal with it all */
6346 if (configopt[optind].func(optsub, optval,
6347 configopt[optind].funcflags) != 0)
6348 errx(1, "%s: line %zd: invalid data for %s",
6349 filename, lineno, configopt[optind].optname);
6356 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6363 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6369 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6371 struct ws_win *parent, *w;
6372 struct swm_region *r;
6373 struct workspace *ws;
6374 xcb_icccm_wm_hints_t wmh;
6376 parent = find_window(win->transient);
6378 parent->child_trans = win;
6380 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6381 " for 0x%x trans 0x%x\n", win->id, win->transient);
6383 r = root_to_region(win->wa->root);
6385 /* parent doen't exist in our window list */
6386 TAILQ_FOREACH(w, &ws->winlist, entry) {
6387 if (xcb_icccm_get_wm_hints_reply(conn,
6388 xcb_icccm_get_wm_hints(conn, w->id),
6390 warnx("can't get hints for 0x%x", w->id);
6394 if (win->hints.window_group != wmh.window_group)
6397 w->child_trans = win;
6398 win->transient = w->id;
6400 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6401 "transient to 0x%x\n", win->transient);
6408 window_get_pid(xcb_window_t win)
6413 xcb_get_property_cookie_t pc;
6414 xcb_get_property_reply_t *pr;
6416 apid = get_atom_from_string("_NET_WM_PID");
6417 if (apid == XCB_ATOM_NONE)
6420 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6421 pr = xcb_get_property_reply(conn, pc, NULL);
6424 if (pr->type != XCB_ATOM_CARDINAL) {
6429 if (pr->type == apid && pr->format == 32)
6430 ret = *((pid_t *)xcb_get_property_value(pr));
6436 apid = get_atom_from_string("_SWM_PID");
6437 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6439 pr = xcb_get_property_reply(conn, pc, NULL);
6442 if (pr->type != apid) {
6447 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6454 get_ws_idx(xcb_window_t id)
6460 xcb_get_property_reply_t *gpr;
6462 gpr = xcb_get_property_reply(conn,
6463 xcb_get_property(conn, 0, id, a_swm_ws,
6464 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6469 proplen = xcb_get_property_value_length(gpr);
6471 prop = malloc(proplen + 1);
6474 xcb_get_property_value(gpr),
6476 prop[proplen] = '\0';
6483 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6484 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6486 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6496 manage_window(xcb_window_t id)
6498 xcb_window_t trans = XCB_WINDOW_NONE;
6499 struct workspace *ws;
6500 struct ws_win *win, *ww;
6501 int ws_idx, border_me = 0;
6502 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6503 struct swm_region *r;
6506 uint32_t event_mask, i;
6507 xcb_icccm_get_wm_protocols_reply_t wpr;
6508 xcb_icccm_get_wm_class_reply_t tmpch;
6510 if ((win = find_window(id)) != NULL)
6511 return (win); /* already being managed */
6513 /* see if we are on the unmanaged list */
6514 if ((win = find_unmanaged_window(id)) != NULL) {
6515 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6516 "window: 0x%x\n", win->id);
6517 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6519 set_child_transient(win, &trans);
6521 if (trans && (ww = find_window(trans)))
6522 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6523 else if ((ww = win->ws->focus) &&
6524 spawn_position == SWM_STACK_ABOVE)
6525 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6527 else if (ww && spawn_position == SWM_STACK_BELOW)
6528 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6529 else switch (spawn_position) {
6532 case SWM_STACK_ABOVE:
6533 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6535 case SWM_STACK_BOTTOM:
6536 case SWM_STACK_BELOW:
6537 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6540 ewmh_update_actions(win);
6544 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6545 err(1, "manage_window: calloc: failed to allocate memory for "
6551 /* see if we need to override the workspace */
6552 p = find_pid(window_get_pid(id));
6554 /* Get all the window data in one shot */
6555 ws_idx = get_ws_idx(id);
6557 win->wa = xcb_get_geometry_reply(conn,
6558 xcb_get_geometry(conn, id),
6560 xcb_icccm_get_wm_normal_hints_reply(conn,
6561 xcb_icccm_get_wm_normal_hints(conn, id),
6563 xcb_icccm_get_wm_hints_reply(conn,
6564 xcb_icccm_get_wm_hints(conn, id),
6566 xcb_icccm_get_wm_transient_for_reply(conn,
6567 xcb_icccm_get_wm_transient_for(conn, id),
6570 win->transient = trans;
6571 set_child_transient(win, &trans);
6572 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6573 "transient: 0x%x\n", win->id, win->transient);
6576 /* get supported protocols */
6577 if (xcb_icccm_get_wm_protocols_reply(conn,
6578 xcb_icccm_get_wm_protocols(conn, id, a_prot),
6580 for (i = 0; i < wpr.atoms_len; i++) {
6581 if (wpr.atoms[i] == a_takefocus)
6582 win->take_focus = 1;
6583 if (wpr.atoms[i] == a_delete)
6584 win->can_delete = 1;
6586 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6589 win->iconic = get_iconic(win);
6592 * Figure out where to put the window. If it was previously assigned to
6593 * a workspace (either by spawn() or manually moving), and isn't
6594 * transient, * put it in the same workspace
6596 r = root_to_region(win->wa->root);
6598 ws = &r->s->ws[p->ws];
6599 TAILQ_REMOVE(&pidlist, p, entry);
6602 } else if (ws_idx != -1 && win->transient == 0) {
6603 ws = &r->s->ws[ws_idx];
6606 /* this should launch transients in the same ws as parent */
6608 if ((ww = find_window(trans)) != NULL)
6614 warnx("manage_window: fix this "
6620 /* set up the window layout */
6623 win->s = r->s; /* this never changes */
6624 if (trans && (ww = find_window(trans)))
6625 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6626 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6627 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6629 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6630 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6631 else switch (spawn_position) {
6634 case SWM_STACK_ABOVE:
6635 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6637 case SWM_STACK_BOTTOM:
6638 case SWM_STACK_BELOW:
6639 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6642 /* ignore window border if there is one. */
6643 WIDTH(win) = win->wa->width;
6644 HEIGHT(win) = win->wa->height;
6645 X(win) = win->wa->x + win->wa->border_width;
6646 Y(win) = win->wa->y + win->wa->border_width;
6648 win->g_floatvalid = 0;
6649 win->floatmaxed = 0;
6650 win->ewmh_flags = 0;
6652 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6653 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6654 HEIGHT(win), ws->idx);
6656 constrain_window(win, r, 0);
6658 /* Set window properties so we can remember this after reincarnation */
6659 if (prop == NULL && snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6661 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6663 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6664 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6670 ewmh_autoquirk(win);
6672 if (xcb_icccm_get_wm_class_reply(conn,
6673 xcb_icccm_get_wm_class(conn, win->id),
6675 win->ch.class_name = tmpch.class_name;
6676 win->ch.instance_name = tmpch.instance_name;
6678 xcb_icccm_get_wm_class_reply_wipe(&tmpch);
6680 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6681 win->ch.class_name, win->ch.instance_name);
6683 /* java is retarded so treat it special */
6684 if (strstr(win->ch.instance_name, "sun-awt")) {
6689 TAILQ_FOREACH(qp, &quirks, entry) {
6690 if (!strcmp(win->ch.class_name, qp->class) &&
6691 !strcmp(win->ch.instance_name, qp->name)) {
6692 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6693 "class: %s, name: %s\n", win->ch.class_name,
6694 win->ch.instance_name);
6695 if (qp->quirk & SWM_Q_FLOAT) {
6699 win->quirks = qp->quirk;
6704 /* alter window position if quirky */
6705 if (win->quirks & SWM_Q_ANYWHERE) {
6706 win->manual = 1; /* don't center the quirky windows */
6707 if (bar_enabled && Y(win) < bar_height)
6708 Y(win) = bar_height;
6709 if (WIDTH(win) + X(win) > WIDTH(r))
6710 X(win) = WIDTH(r) - WIDTH(win) - 2;
6714 /* Reset font sizes (the bruteforce way; no default keybinding). */
6715 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6716 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6717 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6718 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6719 fake_keypress(win, XK_KP_Add, ShiftMask);
6722 ewmh_get_win_state(win);
6723 ewmh_update_actions(win);
6724 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6729 X(win) -= border_width;
6730 Y(win) -= border_width;
6734 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6735 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6737 event_mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
6740 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6742 /* floaters need to be mapped if they are in the current workspace */
6743 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6744 map_window_raised(win->id);
6750 free_window(struct ws_win *win)
6752 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6757 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6765 memset(win, 0xff, sizeof *win); /* XXX kill later */
6768 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6772 unmanage_window(struct ws_win *win)
6774 struct ws_win *parent;
6775 xcb_screen_t *screen;
6780 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6782 if (win->transient) {
6783 parent = find_window(win->transient);
6785 parent->child_trans = NULL;
6788 /* focus on root just in case */
6789 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6790 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6791 screen->root, XCB_CURRENT_TIME);
6795 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6796 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6800 focus_magic(struct ws_win *win)
6802 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6805 /* if there are no windows clear the status-bar */
6810 if (win->child_trans) {
6811 /* win = parent & has a transient so focus on that */
6813 focus_win(win->child_trans);
6814 if (win->child_trans->take_focus)
6815 client_msg(win, a_takefocus);
6817 /* make sure transient hasn't disappeared */
6818 if (validate_win(win->child_trans) == 0) {
6819 focus_win(win->child_trans);
6820 if (win->child_trans->take_focus)
6821 client_msg(win->child_trans,
6824 win->child_trans = NULL;
6826 if (win->take_focus)
6827 client_msg(win, a_takefocus);
6833 if (win->take_focus)
6834 client_msg(win, a_takefocus);
6839 expose(xcb_expose_event_t *e)
6842 struct swm_region *r;
6844 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6846 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6847 for (i = 0; i < num_screens; i++)
6848 TAILQ_FOREACH(r, &screens[i].rl, entry)
6849 if (e->window == WINID(r->bar))
6857 focusin(xcb_focus_in_event_t *e)
6859 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
6860 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
6861 e->mode, get_notify_detail_label(e->detail), e->detail);
6866 keypress(xcb_key_press_event_t *e)
6868 xcb_keysym_t keysym;
6871 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6873 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6875 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6878 if (kp->funcid == KF_SPAWN_CUSTOM)
6879 spawn_custom(root_to_region(e->root),
6880 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6881 else if (keyfuncs[kp->funcid].func)
6882 keyfuncs[kp->funcid].func(root_to_region(e->root),
6883 &(keyfuncs[kp->funcid].args));
6887 buttonpress(xcb_button_press_event_t *e)
6891 unsigned int action;
6893 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6894 e->event, e->detail);
6896 if ((win = find_window(e->event)) == NULL)
6900 action = client_click;
6902 for (i = 0; i < LENGTH(buttons); i++)
6903 if (action == buttons[i].action && buttons[i].func &&
6904 buttons[i].button == e->detail &&
6905 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6906 buttons[i].func(win, &buttons[i].args);
6913 print_win_geom(xcb_window_t w)
6915 xcb_get_geometry_reply_t *wa;
6917 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
6920 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
6925 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
6926 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6927 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
6935 configurerequest(xcb_configure_request_event_t *e)
6940 uint32_t wc[7] = {0};
6942 if ((win = find_window(e->window)) == NULL)
6943 if ((win = find_unmanaged_window(e->window)) == NULL)
6946 print_win_geom(e->window);
6949 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6950 "value_mask: 0x%x", e->window, e->value_mask);
6951 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6952 mask |= XCB_CONFIG_WINDOW_X;
6954 DPRINTF(", X: %d", e->x);
6956 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6957 mask |= XCB_CONFIG_WINDOW_Y;
6959 DPRINTF(", Y: %d", e->y);
6961 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6962 mask |= XCB_CONFIG_WINDOW_WIDTH;
6964 DPRINTF(", W: %u", e->width);
6966 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6967 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6968 wc[i++] = e->height;
6969 DPRINTF(", H: %u", e->height);
6971 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6972 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6973 wc[i++] = e->border_width;
6974 DPRINTF(", Border: %u", e->border_width);
6976 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6977 mask |= XCB_CONFIG_WINDOW_SIBLING;
6978 wc[i++] = e->sibling;
6979 DPRINTF(", Sibling: 0x%x", e->sibling);
6981 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6982 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6983 wc[i++] = e->stack_mode;
6984 DPRINTF(", StackMode: %u", e->stack_mode);
6988 xcb_configure_window(conn, e->window, mask, wc);
6990 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
6991 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6992 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6993 win->g_float.x = e->x - X(win->ws->r);
6994 win->g_float.y = e->y - Y(win->ws->r);
6995 win->g_float.w = e->width;
6996 win->g_float.h = e->height;
6997 win->g_floatvalid = 1;
6999 if (win->floating) {
7000 win->g = win->g_float;
7001 win->g.x += X(win->ws->r);
7002 win->g.y += Y(win->ws->r);
7015 configurenotify(xcb_configure_notify_event_t *e)
7019 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
7022 win = find_window(e->window);
7024 xcb_icccm_get_wm_normal_hints_reply(conn,
7025 xcb_icccm_get_wm_normal_hints(conn, win->id),
7034 destroynotify(xcb_destroy_notify_event_t *e)
7038 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7040 if ((win = find_window(e->window)) == NULL) {
7041 if ((win = find_unmanaged_window(e->window)) == NULL)
7047 /* make sure we focus on something */
7050 unmanage_window(win);
7057 get_notify_detail_label(uint8_t detail)
7062 case XCB_NOTIFY_DETAIL_ANCESTOR:
7065 case XCB_NOTIFY_DETAIL_VIRTUAL:
7068 case XCB_NOTIFY_DETAIL_INFERIOR:
7071 case XCB_NOTIFY_DETAIL_NONLINEAR:
7072 label = "Nonlinear";
7074 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7075 label = "NonlinearVirtual";
7077 case XCB_NOTIFY_DETAIL_POINTER:
7080 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7081 label = "PointerRoot";
7083 case XCB_NOTIFY_DETAIL_NONE:
7094 get_notify_mode_label(uint8_t mode)
7099 case XCB_NOTIFY_MODE_NORMAL:
7102 case XCB_NOTIFY_MODE_GRAB:
7105 case XCB_NOTIFY_MODE_UNGRAB:
7108 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7109 label = "WhileGrabbed";
7120 enternotify(xcb_enter_notify_event_t *e)
7123 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %s(%d), "
7124 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7125 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7126 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7127 e->child, YESNO(e->same_screen_focus), e->state);
7129 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
7130 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
7135 switch (focus_mode) {
7136 case SWM_FOCUS_DEFAULT:
7138 case SWM_FOCUS_FOLLOW:
7140 case SWM_FOCUS_SYNERGY:
7144 if ((win = find_window(e->event)) == NULL) {
7145 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7156 leavenotify(xcb_leave_notify_event_t *e)
7159 DNPRINTF(SWM_D_FOCUS, "leavenotify: window: 0x%x, mode: %s(%d), "
7160 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7161 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7162 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7163 e->child, YESNO(e->same_screen_focus), e->state);
7167 /* lets us use one switch statement for arbitrary mode/detail combinations */
7168 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7171 mapnotify(xcb_map_notify_event_t *e)
7175 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7177 win = manage_window(e->window);
7179 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7182 * focus_win can only set input focus on a mapped window.
7183 * make sure the window really has focus since it is just being mapped.
7185 if (win->ws->focus == win)
7190 mappingnotify(xcb_mapping_notify_event_t *e)
7192 xcb_refresh_keyboard_mapping(syms, e);
7194 if (e->request == XCB_MAPPING_KEYBOARD)
7199 maprequest(xcb_map_request_event_t *e)
7202 struct swm_region *r;
7203 xcb_get_window_attributes_reply_t *war;
7205 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7208 war = xcb_get_window_attributes_reply(conn,
7209 xcb_get_window_attributes(conn, e->window),
7213 if (war->override_redirect) {
7219 win = manage_window(e->window);
7221 return; /* can't happen */
7226 /* make new win focused */
7227 r = root_to_region(win->wa->root);
7228 if (win->ws == r->ws)
7234 get_atom_name(xcb_atom_t atom)
7238 xcb_get_atom_name_reply_t *r;
7240 r = xcb_get_atom_name_reply(conn,
7241 xcb_get_atom_name(conn, atom),
7244 len = xcb_get_atom_name_name_length(r);
7246 name = malloc(len + 1);
7248 memcpy(name, xcb_get_atom_name_name(r), len);
7260 propertynotify(xcb_property_notify_event_t *e)
7266 name = get_atom_name(e->atom);
7267 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7268 e->window, name, e->atom);
7272 win = find_window(e->window);
7276 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7277 update_iconic(win, 0);
7278 map_window_raised(win->id);
7285 case XCB_ATOM_WM_CLASS:
7286 case XCB_ATOM_WM_NAME:
7295 unmapnotify(xcb_unmap_notify_event_t *e)
7299 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7301 /* determine if we need to help unmanage this window */
7302 win = find_window(e->window);
7306 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7307 unmanage_window(win);
7310 /* resend unmap because we ated it */
7311 xcb_unmap_window(conn, e->window);
7318 visibilitynotify(xcb_visibility_notify_event_t *e)
7320 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7326 clientmessage(xcb_client_message_event_t *e)
7329 xcb_map_request_event_t mre;
7333 name = get_atom_name(e->type);
7334 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7335 e->window, name, e->type);
7338 win = find_window(e->window);
7341 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7342 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7343 "unmanaged window.\n");
7344 mre.window = e->window;
7350 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7351 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7354 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7355 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7356 if (win->can_delete)
7357 client_msg(win, a_delete);
7359 xcb_kill_client(conn, win->id);
7361 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7362 DNPRINTF(SWM_D_EVENT,
7363 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7364 if (win->floating) {
7365 if (e->data.data32[0] & (1<<8)) /* x */
7366 X(win) = e->data.data32[1];
7367 if (e->data.data32[0] & (1<<9)) /* y */
7368 Y(win) = e->data.data32[2];
7369 if (e->data.data32[0] & (1<<10)) /* width */
7370 WIDTH(win) = e->data.data32[3];
7371 if (e->data.data32[0] & (1<<11)) /* height */
7372 HEIGHT(win) = e->data.data32[4];
7377 /* TODO: Change stack sizes */
7378 /* notify no change was made. */
7379 config_win(win, NULL);
7382 if (e->type == ewmh[_NET_WM_STATE].atom) {
7383 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7384 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7385 if (e->data.data32[2])
7386 ewmh_update_win_state(win, e->data.data32[2],
7399 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7401 xcb_void_cookie_t wac;
7402 xcb_generic_error_t *error;
7404 /* this causes an error if some other window manager is running */
7405 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7406 for (i = 0; i < num_screens; i++) {
7408 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7410 wac = xcb_change_window_attributes_checked(conn, sc->root,
7411 XCB_CW_EVENT_MASK, &val);
7412 if ((error = xcb_request_check(conn, wac))) {
7413 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7424 new_region(struct swm_screen *s, int x, int y, int w, int h)
7426 struct swm_region *r, *n;
7427 struct workspace *ws = NULL;
7430 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7431 s->idx, w, h, x, y);
7433 /* remove any conflicting regions */
7434 n = TAILQ_FIRST(&s->rl);
7437 n = TAILQ_NEXT(r, entry);
7438 if (X(r) < (x + w) &&
7439 (X(r) + WIDTH(r)) > x &&
7441 (Y(r) + HEIGHT(r)) > y) {
7442 if (r->ws->r != NULL)
7443 r->ws->old_r = r->ws->r;
7446 TAILQ_REMOVE(&s->rl, r, entry);
7447 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7451 /* search old regions for one to reuse */
7453 /* size + location match */
7454 TAILQ_FOREACH(r, &s->orl, entry)
7455 if (X(r) == x && Y(r) == y &&
7456 HEIGHT(r) == h && WIDTH(r) == w)
7460 TAILQ_FOREACH(r, &s->orl, entry)
7461 if (HEIGHT(r) == h && WIDTH(r) == w)
7465 TAILQ_REMOVE(&s->orl, r, entry);
7466 /* try to use old region's workspace */
7467 if (r->ws->r == NULL)
7470 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7471 err(1, "new_region: calloc: failed to allocate memory "
7474 /* if we don't have a workspace already, find one */
7476 for (i = 0; i < workspace_limit; i++)
7477 if (s->ws[i].r == NULL) {
7484 errx(1, "new_region: no free workspaces");
7495 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7501 #ifdef SWM_XRR_HAS_CRTC
7504 #endif /* SWM_XRR_HAS_CRTC */
7505 struct swm_region *r;
7507 xcb_randr_get_screen_resources_current_cookie_t src;
7508 xcb_randr_get_screen_resources_current_reply_t *srr;
7509 xcb_randr_get_crtc_info_cookie_t cic;
7510 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7511 xcb_randr_crtc_t *crtc;
7512 xcb_screen_t *screen = get_screen(i);
7514 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7515 if (i >= num_screens)
7516 errx(1, "scan_xrandr: invalid screen");
7518 /* remove any old regions */
7519 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7520 r->ws->old_r = r->ws->r = NULL;
7522 TAILQ_REMOVE(&screens[i].rl, r, entry);
7523 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7527 /* map virtual screens onto physical screens */
7528 #ifdef SWM_XRR_HAS_CRTC
7529 if (xrandr_support) {
7530 src = xcb_randr_get_screen_resources_current(conn,
7532 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7535 new_region(&screens[i], 0, 0,
7536 screen->width_in_pixels,
7537 screen->height_in_pixels);
7540 ncrtc = srr->num_crtcs;
7542 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7543 for (c = 0; c < ncrtc; c++) {
7544 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7546 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7549 if (cir->num_outputs == 0) {
7555 new_region(&screens[i], 0, 0,
7556 screen->width_in_pixels,
7557 screen->height_in_pixels);
7559 new_region(&screens[i],
7560 cir->x, cir->y, cir->width, cir->height);
7565 #endif /* SWM_XRR_HAS_CRTC */
7567 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7568 screen->height_in_pixels);
7573 screenchange(xcb_randr_screen_change_notify_event_t *e)
7575 struct swm_region *r;
7578 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7580 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7581 /* silly event doesn't include the screen index */
7582 for (i = 0; i < num_screens; i++)
7583 if (screens[i].root == e->root)
7585 if (i >= num_screens)
7586 errx(1, "screenchange: screen not found");
7588 /* brute force for now, just re-enumerate the regions */
7592 print_win_geom(e->root);
7594 /* add bars to all regions */
7595 for (i = 0; i < num_screens; i++)
7596 TAILQ_FOREACH(r, &screens[i].rl, entry)
7604 xcb_window_t *wins = NULL;
7606 int i, j, num_screens;
7607 uint16_t state, manage;
7609 xcb_query_tree_cookie_t qtc;
7610 xcb_query_tree_reply_t *qtr;
7611 xcb_get_window_attributes_cookie_t c;
7612 xcb_get_window_attributes_reply_t *r;
7613 xcb_get_property_cookie_t pc;
7615 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
7616 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7617 for (i = 0; i < num_screens; i++) {
7618 qtc = xcb_query_tree(conn, screens[i].root);
7619 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7622 wins = xcb_query_tree_children(qtr);
7623 no = xcb_query_tree_children_length(qtr);
7624 /* attach windows to a region */
7625 /* normal windows */
7626 for (j = 0; j < no; j++) {
7627 c = xcb_get_window_attributes(conn, wins[j]);
7628 r = xcb_get_window_attributes_reply(conn, c, NULL);
7631 if (r->override_redirect) {
7636 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7637 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7643 state = getstate(wins[j]);
7644 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7645 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7646 manage_window(wins[j]);
7649 /* transient windows */
7650 for (j = 0; j < no; j++) {
7651 c = xcb_get_window_attributes(conn, wins[j]);
7652 r = xcb_get_window_attributes_reply(conn, c, NULL);
7655 if (r->override_redirect) {
7661 state = getstate(wins[j]);
7662 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7663 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7664 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7665 &wins[j], NULL) && manage)
7666 manage_window(wins[j]);
7670 DNPRINTF(SWM_D_INIT, "grab_windows: done\n");
7676 int i, j, k, num_screens;
7677 struct workspace *ws;
7678 uint32_t gcv[1], wa[1];
7679 const xcb_query_extension_reply_t *qep;
7680 xcb_cursor_t cursor;
7681 xcb_font_t cursor_font;
7682 xcb_randr_query_version_cookie_t c;
7683 xcb_randr_query_version_reply_t *r;
7685 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7686 if ((screens = calloc(num_screens,
7687 sizeof(struct swm_screen))) == NULL)
7688 err(1, "setup_screens: calloc: failed to allocate memory for "
7691 /* initial Xrandr setup */
7693 c = xcb_randr_query_version(conn, 1, 1);
7694 r = xcb_randr_query_version_reply(conn, c, NULL);
7696 if (r->major_version >= 1)
7700 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7701 xrandr_eventbase = qep->first_event;
7703 cursor_font = xcb_generate_id(conn);
7704 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7706 cursor = xcb_generate_id(conn);
7707 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7708 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7711 /* map physical screens */
7712 for (i = 0; i < num_screens; i++) {
7713 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7715 TAILQ_INIT(&screens[i].rl);
7716 TAILQ_INIT(&screens[i].orl);
7717 screens[i].root = get_screen(i)->root;
7719 /* set default colors */
7720 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7721 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7722 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7723 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7724 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7726 /* create graphics context on screen */
7727 screens[i].bar_gc = xcb_generate_id(conn);
7729 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7730 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7732 /* set default cursor */
7733 xcb_change_window_attributes(conn, screens[i].root,
7736 /* init all workspaces */
7737 /* XXX these should be dynamically allocated too */
7738 for (j = 0; j < SWM_WS_MAX; j++) {
7739 ws = &screens[i].ws[j];
7745 TAILQ_INIT(&ws->winlist);
7746 TAILQ_INIT(&ws->unmanagedlist);
7748 for (k = 0; layouts[k].l_stack != NULL; k++)
7749 if (layouts[k].l_config != NULL)
7750 layouts[k].l_config(ws,
7751 SWM_ARG_ID_STACKINIT);
7752 ws->cur_layout = &layouts[0];
7753 ws->cur_layout->l_string(ws);
7759 xcb_randr_select_input(conn, screens[i].root,
7760 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7762 xcb_free_cursor(conn, cursor);
7763 xcb_close_font(conn, cursor_font);
7769 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7770 err(1, "setup_globals: strdup: failed to allocate memory.");
7772 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7773 err(1, "setup_globals: strdup: failed to allocate memory.");
7775 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7776 errx(1, "unable to allocate key symbols");
7778 a_state = get_atom_from_string("WM_STATE");
7779 a_prot = get_atom_from_string("WM_PROTOCOLS");
7780 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7781 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7782 a_wmname = get_atom_from_string("WM_NAME");
7783 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7784 a_utf8_string = get_atom_from_string("UTF8_STRING");
7785 a_string = get_atom_from_string("STRING");
7786 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7787 a_swm_ws = get_atom_from_string("_SWM_WS");
7794 xcb_atom_t netwmcheck;
7795 xcb_window_t root, win;
7798 /* work around sun jdk bugs, code from wmname */
7799 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7801 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7802 for (i = 0; i < num_screens; i++) {
7803 root = screens[i].root;
7805 win = xcb_generate_id(conn);
7806 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
7807 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
7808 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7809 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7810 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7812 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7813 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7814 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7815 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7816 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7817 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
7822 event_error(xcb_generic_error_t *e)
7824 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7825 "resource_id: %u, minor_code: %u\n",
7826 xcb_event_get_error_label(e->error_code), e->error_code,
7827 xcb_event_get_request_label(e->major_code), e->major_code,
7828 e->sequence, e->resource_id, e->minor_code);
7832 event_handle(xcb_generic_event_t *evt)
7834 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7836 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7837 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7838 XCB_EVENT_RESPONSE_TYPE(evt));
7841 #define EVENT(type, callback) case type: callback((void *)evt); return
7842 EVENT(0, event_error);
7843 EVENT(XCB_BUTTON_PRESS, buttonpress);
7844 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7845 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7846 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7847 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7848 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7849 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7850 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7851 /*EVENT(XCB_CREATE_NOTIFY, );*/
7852 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7853 EVENT(XCB_ENTER_NOTIFY, enternotify);
7854 EVENT(XCB_EXPOSE, expose);
7856 EVENT(XCB_FOCUS_IN, focusin);
7858 /*EVENT(XCB_FOCUS_OUT, );*/
7859 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7860 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7861 EVENT(XCB_KEY_PRESS, keypress);
7862 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7863 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7865 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
7867 EVENT(XCB_MAP_NOTIFY, mapnotify);
7868 EVENT(XCB_MAP_REQUEST, maprequest);
7869 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7870 /*EVENT(XCB_MOTION_NOTIFY, );*/
7871 /*EVENT(XCB_NO_EXPOSURE, );*/
7872 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7873 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7874 /*EVENT(XCB_RESIZE_REQUEST, );*/
7875 /*EVENT(XCB_SELECTION_CLEAR, );*/
7876 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7877 /*EVENT(XCB_SELECTION_REQUEST, );*/
7878 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7879 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7882 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7883 screenchange((void *)evt);
7887 main(int argc, char *argv[])
7889 struct swm_region *r, *rr;
7890 struct ws_win *winfocus = NULL;
7892 char conf[PATH_MAX], *cfile = NULL;
7894 int xfd, i, num_screens;
7895 struct sigaction sact;
7896 xcb_generic_event_t *evt;
7900 /* suppress unused warning since var is needed */
7903 time_started = time(NULL);
7906 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7907 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
7908 warnx("no locale support");
7910 /* handle some signals */
7911 bzero(&sact, sizeof(sact));
7912 sigemptyset(&sact.sa_mask);
7914 sact.sa_handler = sighdlr;
7915 sigaction(SIGINT, &sact, NULL);
7916 sigaction(SIGQUIT, &sact, NULL);
7917 sigaction(SIGTERM, &sact, NULL);
7918 sigaction(SIGHUP, &sact, NULL);
7920 sact.sa_handler = sighdlr;
7921 sact.sa_flags = SA_NOCLDSTOP;
7922 sigaction(SIGCHLD, &sact, NULL);
7924 if (!(display = XOpenDisplay(0)))
7925 errx(1, "can not open display");
7927 conn = XGetXCBConnection(display);
7928 if (xcb_connection_has_error(conn))
7929 errx(1, "can not get XCB connection");
7931 XSetEventQueueOwner(display, XCBOwnsEventQueue);
7933 xcb_prefetch_extension_data(conn, &xcb_randr_id);
7934 xfd = xcb_get_file_descriptor(conn);
7936 /* look for local and global conf file */
7937 pwd = getpwuid(getuid());
7939 errx(1, "invalid user: %d", getuid());
7941 xcb_grab_server(conn);
7944 /* flush all events */
7945 while ((evt = xcb_poll_for_event(conn))) {
7946 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7951 if (enable_wm() != 0)
7952 errx(1, "another window manager is currently running");
7963 for (i = 0; ; i++) {
7968 snprintf(conf, sizeof conf, "%s/.%s",
7969 pwd->pw_dir, SWM_CONF_FILE);
7973 snprintf(conf, sizeof conf, "/etc/%s",
7978 snprintf(conf, sizeof conf, "%s/.%s",
7979 pwd->pw_dir, SWM_CONF_FILE_OLD);
7983 snprintf(conf, sizeof conf, "/etc/%s",
7990 if (strlen(conf) && stat(conf, &sb) != -1)
7991 if (S_ISREG(sb.st_mode)) {
7998 /* load conf (if any) */
8000 conf_load(cfile, SWM_CONF_DEFAULT);
8003 /* set some values to work around bad programs */
8005 /* grab existing windows (before we build the bars) */
8008 if (getenv("SWM_STARTED") == NULL)
8009 setenv("SWM_STARTED", "YES", 1);
8011 /* setup all bars */
8012 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8013 for (i = 0; i < num_screens; i++)
8014 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8015 if (winfocus == NULL)
8016 winfocus = TAILQ_FIRST(&r->ws->winlist);
8025 xcb_ungrab_server(conn);
8029 while ((evt = xcb_poll_for_event(conn))) {
8036 /* if we are being restarted go focus on first window */
8038 rr = winfocus->ws->r;
8040 /* not a visible window */
8044 /* move pointer to first screen if multi screen */
8045 if (num_screens > 1 || outputs > 1)
8046 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
8047 rr->s[0].root, 0, 0, 0, 0, X(rr),
8048 Y(rr) + (bar_enabled ? bar_height : 0));
8050 a.id = SWM_ARG_ID_FOCUSCUR;
8060 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8062 DNPRINTF(SWM_D_MISC, "select failed");
8063 if (restart_wm == 1)
8064 restart(NULL, NULL);
8065 if (search_resp == 1)
8078 for (i = 0; i < num_screens; ++i)
8079 if (screens[i].bar_gc != 0)
8080 xcb_free_gc(conn, screens[i].bar_gc);
8082 XftFontClose(display, bar_font);
8083 XftColorFree(display, DefaultVisual(display, 0),
8084 DefaultColormap(display, 0), &bar_font_color);
8085 xcb_key_symbols_free(syms);
8087 xcb_disconnect(conn);