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 /* XXX REMOVE FOR 2.0 */
110 #undef SPECTRWM_BUILDSTR
111 #define SPECTRWM_BUILDSTR "BETA"
113 #ifdef SPECTRWM_BUILDSTR
114 static const char *buildstr = SPECTRWM_BUILDSTR;
116 static const char *buildstr = SPECTRWM_VERSION;
120 # error XRandR versions less than 1.0 are not supported
125 #define SWM_XRR_HAS_CRTC
129 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
130 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug = 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY Mod1Mask
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | LockMask))
222 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
223 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
224 #define SWM_PROPLEN (16)
225 #define SWM_FUNCNAME_LEN (32)
226 #define SWM_KEYS_LEN (255)
227 #define SWM_QUIRK_LEN (64)
228 #define X(r) (r)->g.x
229 #define Y(r) (r)->g.y
230 #define WIDTH(r) (r)->g.w
231 #define HEIGHT(r) (r)->g.h
232 #define BORDER(w) ((w)->bordered ? border_width : 0)
233 #define MAX_X(r) ((r)->g.x + (r)->g.w)
234 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
235 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
236 #define SH_MIN_W(w) (w)->sh.min_width
237 #define SH_MIN_H(w) (w)->sh.min_height
238 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
239 #define SH_MAX_W(w) (w)->sh.max_width
240 #define SH_MAX_H(w) (w)->sh.max_height
241 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
242 #define SH_INC_W(w) (w)->sh.width_inc
243 #define SH_INC_H(w) (w)->sh.height_inc
244 #define SWM_MAX_FONT_STEPS (3)
245 #define WINID(w) ((w) ? (w)->id : 0)
246 #define YESNO(x) ((x) ? "yes" : "no")
248 #define SWM_FOCUS_DEFAULT (0)
249 #define SWM_FOCUS_SYNERGY (1)
250 #define SWM_FOCUS_FOLLOW (2)
252 #define SWM_CONF_DEFAULT (0)
253 #define SWM_CONF_KEYMAPPING (1)
256 #define SWM_LIB "/usr/local/lib/libswmhack.so"
263 xcb_atom_t a_takefocus;
265 xcb_atom_t a_netwmname;
266 xcb_atom_t a_utf8_string;
268 xcb_atom_t a_swm_iconic;
270 volatile sig_atomic_t running = 1;
271 volatile sig_atomic_t restart_wm = 0;
273 /*int last_focus_event = FocusOut;*/
277 int xrandr_eventbase;
278 unsigned int numlockmask = 0;
281 xcb_connection_t *conn;
282 xcb_key_symbols_t *syms;
285 int cycle_visible = 0;
287 int font_adjusted = 0;
288 unsigned int mod_key = MODKEY;
291 struct swm_region *search_r;
292 int select_list_pipe[2];
293 int select_resp_pipe[2];
295 volatile sig_atomic_t search_resp;
296 int search_resp_action;
298 struct search_window {
299 TAILQ_ENTRY(search_window) entry;
303 xcb_window_t indicator;
305 TAILQ_HEAD(search_winlist, search_window);
306 struct search_winlist search_wl;
311 SWM_SEARCH_UNICONIFY,
312 SWM_SEARCH_NAME_WORKSPACE,
313 SWM_SEARCH_SEARCH_WORKSPACE,
314 SWM_SEARCH_SEARCH_WINDOW
317 #define SWM_STACK_TOP (0)
318 #define SWM_STACK_BOTTOM (1)
319 #define SWM_STACK_ABOVE (2)
320 #define SWM_STACK_BELOW (3)
323 double dialog_ratio = 0.6;
325 #define SWM_BAR_MAX (256)
326 #define SWM_BAR_JUSTIFY_LEFT (0)
327 #define SWM_BAR_JUSTIFY_CENTER (1)
328 #define SWM_BAR_JUSTIFY_RIGHT (2)
329 #define SWM_BAR_OFFSET (4)
330 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
331 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
332 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
333 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
334 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
336 #ifdef X_HAVE_UTF8_STRING
337 #define DRAWSTRING(x...) Xutf8DrawString(x)
339 #define DRAWSTRING(x...) XmbDrawString(x)
342 char *bar_argv[] = { NULL, NULL };
344 unsigned char bar_ext[SWM_BAR_MAX];
345 char bar_vertext[SWM_BAR_MAX];
347 sig_atomic_t bar_alarm = 0;
350 int bar_border_width = 1;
351 int bar_at_bottom = 0;
353 int bar_extra_running = 0;
356 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
357 char *bar_format = NULL;
358 int stack_enabled = 1;
359 int clock_enabled = 1;
360 int urgent_enabled = 0;
361 char *clock_format = NULL;
362 int title_name_enabled = 0;
363 int title_class_enabled = 0;
364 int window_name_enabled = 0;
365 int focus_mode = SWM_FOCUS_DEFAULT;
366 int focus_close = SWM_STACK_BELOW;
367 int focus_close_wrap = 1;
368 int focus_default = SWM_STACK_TOP;
369 int spawn_position = SWM_STACK_TOP;
370 int disable_border = 0;
371 int border_width = 1;
372 int verbose_layout = 0;
376 XFontSetExtents *bar_fs_extents;
378 int bar_font_legacy = 1;
380 XftColor bar_font_color;
383 /* layout manager data */
384 struct swm_geometry {
397 struct swm_geometry g;
400 /* virtual "screens" */
402 TAILQ_ENTRY(swm_region) entry;
403 struct swm_geometry g;
404 struct workspace *ws; /* current workspace on this region */
405 struct workspace *ws_prior; /* prior workspace on this region */
406 struct swm_screen *s; /* screen idx */
409 TAILQ_HEAD(swm_region_list, swm_region);
412 TAILQ_ENTRY(ws_win) entry;
414 xcb_window_t transient;
415 struct ws_win *child_trans; /* transient child window */
416 struct swm_geometry g; /* current geometry */
417 struct swm_geometry g_float; /* region coordinates */
418 int g_floatvalid; /* g_float geometry validity */
419 int floatmaxed; /* whether maxed by max_stack */
424 unsigned int ewmh_flags;
425 int font_size_boundary[SWM_MAX_FONT_STEPS];
431 unsigned long quirks;
432 struct workspace *ws; /* always valid */
433 struct swm_screen *s; /* always valid, never changes */
434 xcb_get_geometry_reply_t *wa;
436 xcb_icccm_get_wm_class_reply_t ch;
437 xcb_icccm_wm_hints_t hints;
439 TAILQ_HEAD(ws_win_list, ws_win);
443 TAILQ_ENTRY(pid_e) entry;
447 TAILQ_HEAD(pid_list, pid_e);
448 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
450 /* layout handlers */
452 void vertical_config(struct workspace *, int);
453 void vertical_stack(struct workspace *, struct swm_geometry *);
454 void horizontal_config(struct workspace *, int);
455 void horizontal_stack(struct workspace *, struct swm_geometry *);
456 void max_stack(struct workspace *, struct swm_geometry *);
457 void plain_stacker(struct workspace *);
458 void fancy_stacker(struct workspace *);
461 void (*l_stack)(struct workspace *, struct swm_geometry *);
462 void (*l_config)(struct workspace *, int);
464 #define SWM_L_FOCUSPREV (1<<0)
465 #define SWM_L_MAPONFOCUS (1<<1)
466 void (*l_string)(struct workspace *);
468 /* stack, configure */
469 { vertical_stack, vertical_config, 0, plain_stacker },
470 { horizontal_stack, horizontal_config, 0, plain_stacker },
472 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
473 { NULL, NULL, 0, NULL },
476 /* position of max_stack mode in the layouts array, index into layouts! */
477 #define SWM_V_STACK (0)
478 #define SWM_H_STACK (1)
479 #define SWM_MAX_STACK (2)
481 #define SWM_H_SLICE (32)
482 #define SWM_V_SLICE (32)
484 /* define work spaces */
486 int idx; /* workspace index */
487 char *name; /* workspace name */
488 int always_raise; /* raise windows on focus */
489 struct layout *cur_layout; /* current layout handlers */
490 struct ws_win *focus; /* may be NULL */
491 struct ws_win *focus_prev; /* may be NULL */
492 struct swm_region *r; /* may be NULL */
493 struct swm_region *old_r; /* may be NULL */
494 struct ws_win_list winlist; /* list of windows in ws */
495 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
496 char stacker[10]; /* display stacker and layout */
500 int horizontal_msize;
502 int horizontal_stacks;
513 SWM_S_COLOR_BAR_BORDER,
514 SWM_S_COLOR_BAR_FONT,
520 /* physical screen mapping */
521 #define SWM_WS_MAX (22) /* hard limit */
522 int workspace_limit = 10; /* soft limit */
525 int idx; /* screen index */
526 struct swm_region_list rl; /* list of regions on this screen */
527 struct swm_region_list orl; /* list of old regions */
529 struct workspace ws[SWM_WS_MAX];
535 } c[SWM_S_COLOR_MAX];
537 xcb_gcontext_t bar_gc;
540 struct swm_screen *screens;
542 /* args to functions */
545 #define SWM_ARG_ID_FOCUSNEXT (0)
546 #define SWM_ARG_ID_FOCUSPREV (1)
547 #define SWM_ARG_ID_FOCUSMAIN (2)
548 #define SWM_ARG_ID_FOCUSCUR (4)
549 #define SWM_ARG_ID_SWAPNEXT (10)
550 #define SWM_ARG_ID_SWAPPREV (11)
551 #define SWM_ARG_ID_SWAPMAIN (12)
552 #define SWM_ARG_ID_MOVELAST (13)
553 #define SWM_ARG_ID_MASTERSHRINK (20)
554 #define SWM_ARG_ID_MASTERGROW (21)
555 #define SWM_ARG_ID_MASTERADD (22)
556 #define SWM_ARG_ID_MASTERDEL (23)
557 #define SWM_ARG_ID_FLIPLAYOUT (24)
558 #define SWM_ARG_ID_STACKRESET (30)
559 #define SWM_ARG_ID_STACKINIT (31)
560 #define SWM_ARG_ID_CYCLEWS_UP (40)
561 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
562 #define SWM_ARG_ID_CYCLESC_UP (42)
563 #define SWM_ARG_ID_CYCLESC_DOWN (43)
564 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
565 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
566 #define SWM_ARG_ID_STACKINC (50)
567 #define SWM_ARG_ID_STACKDEC (51)
568 #define SWM_ARG_ID_SS_ALL (60)
569 #define SWM_ARG_ID_SS_WINDOW (61)
570 #define SWM_ARG_ID_DONTCENTER (70)
571 #define SWM_ARG_ID_CENTER (71)
572 #define SWM_ARG_ID_KILLWINDOW (80)
573 #define SWM_ARG_ID_DELETEWINDOW (81)
574 #define SWM_ARG_ID_WIDTHGROW (90)
575 #define SWM_ARG_ID_WIDTHSHRINK (91)
576 #define SWM_ARG_ID_HEIGHTGROW (92)
577 #define SWM_ARG_ID_HEIGHTSHRINK (93)
578 #define SWM_ARG_ID_MOVEUP (100)
579 #define SWM_ARG_ID_MOVEDOWN (101)
580 #define SWM_ARG_ID_MOVELEFT (102)
581 #define SWM_ARG_ID_MOVERIGHT (103)
587 TAILQ_ENTRY(quirk) entry;
591 #define SWM_Q_FLOAT (1<<0) /* float this window */
592 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
593 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
594 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
595 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
596 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
598 TAILQ_HEAD(quirk_list, quirk);
599 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
602 * Supported EWMH hints should be added to
603 * both the enum and the ewmh array
608 _NET_MOVERESIZE_WINDOW,
609 _NET_WM_ACTION_CLOSE,
610 _NET_WM_ACTION_FULLSCREEN,
612 _NET_WM_ACTION_RESIZE,
613 _NET_WM_ALLOWED_ACTIONS,
617 _NET_WM_STATE_FULLSCREEN,
618 _NET_WM_STATE_HIDDEN,
619 _NET_WM_STATE_MAXIMIZED_HORZ,
620 _NET_WM_STATE_MAXIMIZED_VERT,
621 _NET_WM_STATE_SKIP_PAGER,
622 _NET_WM_STATE_SKIP_TASKBAR,
624 _NET_WM_WINDOW_TYPE_DIALOG,
625 _NET_WM_WINDOW_TYPE_DOCK,
626 _NET_WM_WINDOW_TYPE_NORMAL,
627 _NET_WM_WINDOW_TYPE_SPLASH,
628 _NET_WM_WINDOW_TYPE_TOOLBAR,
629 _NET_WM_WINDOW_TYPE_UTILITY,
630 _SWM_WM_STATE_MANUAL,
637 } ewmh[SWM_EWMH_HINT_MAX] = {
638 /* must be in same order as in the enum */
639 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
640 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
641 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
642 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
643 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
644 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
645 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
646 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
647 {"_NET_WM_NAME", XCB_ATOM_NONE},
648 {"_NET_WM_STATE", XCB_ATOM_NONE},
649 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
650 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
651 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
652 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
653 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
654 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
655 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
656 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
657 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
658 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
659 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
660 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
661 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
662 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
663 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
666 /* function prototypes */
667 void buttonpress(xcb_button_press_event_t *);
668 void check_conn(void);
669 void clientmessage(xcb_client_message_event_t *);
670 int conf_load(char *, int);
671 void configurenotify(xcb_configure_notify_event_t *);
672 void configurerequest(xcb_configure_request_event_t *);
673 void constrain_window(struct ws_win *, struct swm_region *, int);
674 void destroynotify(xcb_destroy_notify_event_t *);
676 void enternotify(xcb_enter_notify_event_t *);
677 void event_error(xcb_generic_error_t *);
678 void event_handle(xcb_generic_event_t *);
679 char *expand_tilde(char *);
680 void expose(xcb_expose_event_t *);
681 struct ws_win *find_window(xcb_window_t);
682 int floating_toggle_win(struct ws_win *);
683 void focus(struct swm_region *, union arg *);
684 void focus_magic(struct ws_win *);
686 void focusin(xcb_focus_in_event_t *);
688 xcb_atom_t get_atom_from_string(const char *);
690 char *get_atom_name(xcb_atom_t);
691 char *get_notify_detail_label(uint8_t);
692 char *get_notify_mode_label(uint8_t);
694 xcb_screen_t *get_screen(int);
695 char *get_win_name(xcb_window_t);
696 uint32_t getstate(xcb_window_t);
697 void grabbuttons(struct ws_win *, int);
698 void keypress(xcb_key_press_event_t *);
700 void leavenotify(xcb_leave_notify_event_t *);
702 void map_window_raised(xcb_window_t);
703 void mapnotify(xcb_map_notify_event_t *);
704 void mappingnotify(xcb_mapping_notify_event_t *);
705 void maprequest(xcb_map_request_event_t *);
706 void new_region(struct swm_screen *, int, int, int, int);
707 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
708 void propertynotify(xcb_property_notify_event_t *);
709 void spawn_select(struct swm_region *, union arg *, char *, int *);
710 void screenchange(xcb_randr_screen_change_notify_event_t *);
711 void shutdown_cleanup(void);
712 void store_float_geom(struct ws_win *, struct swm_region *);
713 void unmanage_window(struct ws_win *);
714 void unmapnotify(xcb_unmap_notify_event_t *);
715 void update_window(struct ws_win *);
716 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
719 expand_tilde(char *s)
728 errx(1, "expand_tilde: NULL string.");
737 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
738 errx(1, "expand_tilde: sysconf");
740 if ((user = calloc(1, max + 1)) == NULL)
741 errx(1, "expand_tilde: calloc");
743 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
748 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
752 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
756 errx(1, "expand_tilde: failed to allocate memory.");
762 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
764 unsigned int tmpr, tmpg, tmpb;
766 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
769 *rr = RGB_8_TO_16(tmpr);
770 *gg = RGB_8_TO_16(tmpg);
771 *bb = RGB_8_TO_16(tmpb);
777 get_screen(int screen)
779 const xcb_setup_t *r;
780 xcb_screen_iterator_t iter;
782 if ((r = xcb_get_setup(conn)) == NULL) {
783 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
787 iter = xcb_setup_roots_iterator(r);
788 for (; iter.rem; --screen, xcb_screen_next(&iter))
798 xcb_get_input_focus_cookie_t c;
799 xcb_get_input_focus_reply_t *r;
802 c = xcb_get_input_focus(conn);
804 r = xcb_get_input_focus_reply(conn, c, NULL);
810 map_window_raised(xcb_window_t win)
812 uint32_t val = XCB_STACK_MODE_ABOVE;
814 xcb_configure_window(conn, win,
815 XCB_CONFIG_WINDOW_STACK_MODE, &val);
817 xcb_map_window(conn, win);
821 get_atom_from_string(const char *str)
823 xcb_intern_atom_cookie_t c;
824 xcb_intern_atom_reply_t *r;
827 c = xcb_intern_atom(conn, 0, strlen(str), str);
828 r = xcb_intern_atom_reply(conn, c, NULL);
836 return (XCB_ATOM_NONE);
840 update_iconic(struct ws_win *win, int newv)
847 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
848 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
850 xcb_delete_property(conn, win->id, a_swm_iconic);
854 get_iconic(struct ws_win *win)
857 xcb_get_property_reply_t *pr = NULL;
860 pr = xcb_get_property_reply(conn,
861 xcb_get_property(conn, 0, win->id, a_swm_iconic,
862 XCB_ATOM_INTEGER, 0, 1), NULL);
865 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
867 v = *((int32_t *)xcb_get_property_value(pr));
878 int i, j, num_screens;
880 sup_list = get_atom_from_string("_NET_SUPPORTED");
882 for (i = 0; i < LENGTH(ewmh); i++)
883 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
885 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
886 for (i = 0; i < num_screens; i++) {
887 /* Support check window will be created by workaround(). */
889 /* Report supported atoms */
890 xcb_delete_property(conn, screens[i].root, sup_list);
891 for (j = 0; j < LENGTH(ewmh); j++)
892 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
893 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
902 xcb_atom_t sup_check, sup_list;
904 xcb_get_property_cookie_t pc;
905 xcb_get_property_reply_t *pr;
907 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
908 sup_list = get_atom_from_string("_NET_SUPPORTED");
909 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
911 for (i = 0; i < num_screens; i++) {
912 /* Get the support check window and destroy it */
913 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
914 XCB_ATOM_WINDOW, 0, 1);
915 pr = xcb_get_property_reply(conn, pc, NULL);
918 if (pr->format == sup_check) {
919 id = *((xcb_window_t *)xcb_get_property_value(pr));
921 xcb_destroy_window(conn, id);
922 xcb_delete_property(conn, screens[i].root, sup_check);
923 xcb_delete_property(conn, screens[i].root, sup_list);
930 ewmh_autoquirk(struct ws_win *win)
934 xcb_get_property_cookie_t c;
935 xcb_get_property_reply_t *r;
937 c = xcb_get_property(conn, 0, win->id,
938 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
939 r = xcb_get_property_reply(conn, c, NULL);
943 n = xcb_get_property_value_length(r);
944 type = xcb_get_property_value(r);
946 for (i = 0; i < n; i++) {
947 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
949 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
950 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
951 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
953 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
956 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
957 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
959 win->quirks = SWM_Q_FLOAT;
966 #define SWM_EWMH_ACTION_COUNT_MAX (6)
967 #define EWMH_F_FULLSCREEN (1<<0)
968 #define EWMH_F_ABOVE (1<<1)
969 #define EWMH_F_HIDDEN (1<<2)
970 #define EWMH_F_SKIP_PAGER (1<<3)
971 #define EWMH_F_SKIP_TASKBAR (1<<4)
972 #define SWM_F_MANUAL (1<<5)
975 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
983 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
984 "fullscreen %s\n", win->id, YESNO(fs));
987 if (!win->g_floatvalid)
988 store_float_geom(win, win->ws->r);
990 win->g = win->ws->r->g;
993 if (win->g_floatvalid) {
994 /* refloat at last floating relative position */
995 win->g = win->g_float;
996 X(win) += X(win->ws->r);
997 Y(win) += Y(win->ws->r);
1005 ewmh_update_actions(struct ws_win *win)
1007 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1013 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1015 if (win->floating) {
1016 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1017 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1020 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1021 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1024 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1025 #define _NET_WM_STATE_ADD 1 /* add/set property */
1026 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1029 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1031 unsigned int mask = 0;
1032 unsigned int changed = 0;
1033 unsigned int orig_flags;
1038 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1039 "action: %ld\n", win->id, (unsigned long)state, action);
1041 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1042 mask = EWMH_F_FULLSCREEN;
1043 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1044 mask = EWMH_F_ABOVE;
1045 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1046 mask = SWM_F_MANUAL;
1047 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1048 mask = EWMH_F_SKIP_PAGER;
1049 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1050 mask = EWMH_F_SKIP_TASKBAR;
1053 orig_flags = win->ewmh_flags;
1056 case _NET_WM_STATE_REMOVE:
1057 win->ewmh_flags &= ~mask;
1059 case _NET_WM_STATE_ADD:
1060 win->ewmh_flags |= mask;
1062 case _NET_WM_STATE_TOGGLE:
1063 win->ewmh_flags ^= mask;
1067 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1069 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1071 if (!floating_toggle_win(win))
1072 win->ewmh_flags = orig_flags; /* revert */
1073 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1075 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1076 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1078 if (!ewmh_set_win_fullscreen(win,
1079 win->ewmh_flags & EWMH_F_FULLSCREEN))
1080 win->ewmh_flags = orig_flags; /* revert */
1082 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1084 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1085 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1086 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1087 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1088 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1089 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1090 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1091 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1092 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1093 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1094 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1095 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1096 if (win->ewmh_flags & EWMH_F_ABOVE)
1097 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1098 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1099 &ewmh[_NET_WM_STATE_ABOVE].atom);
1100 if (win->ewmh_flags & SWM_F_MANUAL)
1101 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1102 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1103 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1107 ewmh_get_win_state(struct ws_win *win)
1110 xcb_get_property_cookie_t c;
1111 xcb_get_property_reply_t *r;
1117 win->ewmh_flags = 0;
1119 win->ewmh_flags |= EWMH_F_ABOVE;
1121 win->ewmh_flags |= SWM_F_MANUAL;
1123 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1124 XCB_ATOM_ATOM, 0, UINT32_MAX);
1125 r = xcb_get_property_reply(conn, c, NULL);
1129 states = xcb_get_property_value(r);
1130 n = xcb_get_property_value_length(r);
1132 for (i = 0; i < n; i++)
1133 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1141 dumpwins(struct swm_region *r, union arg *args)
1145 xcb_get_window_attributes_cookie_t c;
1146 xcb_get_window_attributes_reply_t *wa;
1148 /* suppress unused warning since var is needed */
1151 if (r->ws == NULL) {
1152 warnx("dumpwins: invalid workspace");
1156 warnx("=== managed window list ws %02d ===", r->ws->idx);
1157 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1158 state = getstate(win->id);
1159 c = xcb_get_window_attributes(conn, win->id);
1160 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1162 warnx("window: 0x%x, map_state: %d, state: %u, "
1163 "transient: 0x%x", win->id, wa->map_state,
1164 state, win->transient);
1167 warnx("window: 0x%x, failed xcb_get_window_attributes",
1171 warnx("===== unmanaged window list =====");
1172 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1173 state = getstate(win->id);
1174 c = xcb_get_window_attributes(conn, win->id);
1175 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1177 warnx("window: 0x%x, map_state: %d, state: %u, "
1178 "transient: 0x%x", win->id, wa->map_state,
1179 state, win->transient);
1182 warnx("window: 0x%x, failed xcb_get_window_attributes",
1186 warnx("=================================");
1190 dumpwins(struct swm_region *r, union arg *s)
1195 #endif /* SWM_DEBUG */
1200 int saved_errno, status;
1203 saved_errno = errno;
1207 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1212 if (errno != ECHILD)
1213 warn("sighdlr: waitpid");
1214 #endif /* SWM_DEBUG */
1217 if (pid == searchpid)
1221 if (WIFEXITED(status)) {
1222 if (WEXITSTATUS(status) != 0)
1223 warnx("sighdlr: child exit status: %d",
1224 WEXITSTATUS(status));
1226 warnx("sighdlr: child is terminated "
1228 #endif /* SWM_DEBUG */
1242 errno = saved_errno;
1248 struct pid_e *p = NULL;
1250 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1255 TAILQ_FOREACH(p, &pidlist, entry) {
1264 name_to_pixel(const char *colorname)
1266 uint32_t result = 0;
1267 char cname[32] = "#";
1268 xcb_screen_t *screen;
1269 xcb_colormap_t cmap;
1270 xcb_alloc_color_reply_t *cr;
1271 xcb_alloc_named_color_reply_t *nr;
1272 uint16_t rr, gg, bb;
1274 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1275 cmap = screen->default_colormap;
1277 /* color is in format rgb://rr/gg/bb */
1278 if (strncmp(colorname, "rgb:", 4) == 0) {
1279 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1280 warnx("could not parse rgb %s", colorname);
1282 cr = xcb_alloc_color_reply(conn,
1283 xcb_alloc_color(conn, cmap, rr, gg, bb),
1289 warnx("color '%s' not found", colorname);
1292 nr = xcb_alloc_named_color_reply(conn,
1293 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1296 strlcat(cname, colorname + 2, sizeof cname - 1);
1297 nr = xcb_alloc_named_color_reply(conn,
1298 xcb_alloc_named_color(conn, cmap, strlen(cname),
1305 warnx("color '%s' not found", colorname);
1312 setscreencolor(char *val, int i, int c)
1316 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1317 if (i > 0 && i <= num_screens) {
1318 screens[i - 1].c[c].pixel = name_to_pixel(val);
1319 free(screens[i - 1].c[c].name);
1320 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1322 } else if (i == -1) {
1323 for (i = 0; i < num_screens; i++) {
1324 screens[i].c[c].pixel = name_to_pixel(val);
1325 free(screens[i].c[c].name);
1326 if ((screens[i].c[c].name = strdup(val)) == NULL)
1330 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1335 fancy_stacker(struct workspace *ws)
1337 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1338 if (ws->cur_layout->l_stack == vertical_stack)
1339 snprintf(ws->stacker, sizeof ws->stacker,
1340 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1341 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1342 if (ws->cur_layout->l_stack == horizontal_stack)
1343 snprintf(ws->stacker, sizeof ws->stacker,
1344 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1345 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1349 plain_stacker(struct workspace *ws)
1351 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1352 if (ws->cur_layout->l_stack == vertical_stack)
1353 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1354 sizeof ws->stacker);
1355 if (ws->cur_layout->l_stack == horizontal_stack)
1356 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1357 sizeof ws->stacker);
1361 custom_region(char *val)
1363 unsigned int x, y, w, h;
1364 int sidx, num_screens;
1365 xcb_screen_t *screen;
1367 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1368 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1369 errx(1, "invalid custom region, "
1370 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1371 if (sidx < 1 || sidx > num_screens)
1372 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1376 if ((screen = get_screen(sidx)) == NULL)
1377 errx(1, "ERROR: can't get screen %d.", sidx);
1380 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1382 if (x > screen->width_in_pixels ||
1383 y > screen->height_in_pixels ||
1384 w + x > screen->width_in_pixels ||
1385 h + y > screen->height_in_pixels) {
1386 warnx("ignoring region %ux%u+%u+%u - not within screen "
1387 "boundaries (%ux%u)", w, h, x, y,
1388 screen->width_in_pixels, screen->height_in_pixels);
1392 new_region(&screens[sidx], x, y, w, h);
1396 socket_setnonblock(int fd)
1400 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1401 err(1, "fcntl F_GETFL");
1402 flags |= O_NONBLOCK;
1403 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1404 err(1, "fcntl F_SETFL");
1408 bar_print_legacy(struct swm_region *r, const char *s)
1410 xcb_rectangle_t rect;
1415 XRectangle ibox, lbox;
1419 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1421 switch (bar_justify) {
1422 case SWM_BAR_JUSTIFY_LEFT:
1425 case SWM_BAR_JUSTIFY_CENTER:
1426 x = (WIDTH(r) - lbox.width) / 2;
1428 case SWM_BAR_JUSTIFY_RIGHT:
1429 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1433 if (x < SWM_BAR_OFFSET)
1438 rect.width = WIDTH(r->bar);
1439 rect.height = HEIGHT(r->bar);
1441 /* clear back buffer */
1442 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1443 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1444 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1445 sizeof(rect), &rect);
1447 /* draw back buffer */
1448 gcvd.graphics_exposures = 0;
1449 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1450 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1451 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1452 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1454 XFreeGC(display, draw);
1457 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1458 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1462 bar_print(struct swm_region *r, const char *s)
1465 xcb_rectangle_t rect;
1473 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1475 switch (bar_justify) {
1476 case SWM_BAR_JUSTIFY_LEFT:
1479 case SWM_BAR_JUSTIFY_CENTER:
1480 x = (WIDTH(r) - info.width) / 2;
1482 case SWM_BAR_JUSTIFY_RIGHT:
1483 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1487 if (x < SWM_BAR_OFFSET)
1492 rect.width = WIDTH(r->bar);
1493 rect.height = HEIGHT(r->bar);
1495 /* clear back buffer */
1496 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1497 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1498 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1499 sizeof(rect), &rect);
1501 /* draw back buffer */
1502 draw = XftDrawCreate(display, r->bar->buffer,
1503 DefaultVisual(display, r->s->idx),
1504 DefaultColormap(display, r->s->idx));
1506 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1507 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1510 XftDrawDestroy(draw);
1513 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1514 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1518 bar_extra_stop(void)
1522 bzero(bar_pipe, sizeof bar_pipe);
1525 kill(bar_pid, SIGTERM);
1528 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1533 bar_class_name(char *s, size_t sz, struct swm_region *r)
1535 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1537 if (r->ws->focus->ch.class_name != NULL)
1538 strlcat(s, r->ws->focus->ch.class_name, sz);
1542 bar_title_name(char *s, size_t sz, struct swm_region *r)
1544 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1546 if (r->ws->focus->ch.instance_name != NULL)
1547 strlcat(s, r->ws->focus->ch.instance_name, sz);
1551 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1553 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1556 bar_class_name(s, sz, r);
1557 strlcat(s, ":", sz);
1558 bar_title_name(s, sz, r);
1562 bar_window_float(char *s, size_t sz, struct swm_region *r)
1564 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1566 if (r->ws->focus->floating)
1567 strlcat(s, "(f)", sz);
1571 bar_window_name(char *s, size_t sz, struct swm_region *r)
1575 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1577 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1580 strlcat(s, title, sz);
1584 int urgent[SWM_WS_MAX];
1586 bar_urgent(char *s, size_t sz)
1589 int i, j, num_screens;
1591 xcb_get_property_cookie_t c;
1592 xcb_icccm_wm_hints_t hints;
1594 for (i = 0; i < workspace_limit; i++)
1597 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1598 for (i = 0; i < num_screens; i++)
1599 for (j = 0; j < workspace_limit; j++)
1600 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1601 c = xcb_icccm_get_wm_hints(conn, win->id);
1602 if (xcb_icccm_get_wm_hints_reply(conn, c,
1605 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1609 for (i = 0; i < workspace_limit; i++) {
1611 snprintf(b, sizeof b, "%d ", i + 1);
1613 snprintf(b, sizeof b, "- ");
1619 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1621 if (r == NULL || r->ws == NULL)
1623 if (r->ws->name != NULL)
1624 strlcat(s, r->ws->name, sz);
1627 /* build the default bar format according to the defined enabled options */
1629 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1631 /* if format provided, just copy the buffers */
1632 if (bar_format != NULL) {
1633 strlcpy(fmtnew, fmtexp, sz);
1637 /* reset the output buffer */
1640 strlcat(fmtnew, "+N:+I ", sz);
1642 strlcat(fmtnew, "+S", sz);
1643 strlcat(fmtnew, " ", sz);
1645 /* only show the workspace name if there's actually one */
1646 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1647 strlcat(fmtnew, "<+D>", sz);
1648 strlcat(fmtnew, "+3<", sz);
1650 if (clock_enabled) {
1651 strlcat(fmtnew, fmtexp, sz);
1652 strlcat(fmtnew, "+4<", sz);
1655 /* bar_urgent already adds the space before the last asterisk */
1657 strlcat(fmtnew, "* +U*+4<", sz);
1659 if (title_class_enabled) {
1660 strlcat(fmtnew, "+C", sz);
1661 if (title_name_enabled == 0)
1662 strlcat(fmtnew, "+4<", sz);
1665 /* checks needed by the colon and floating strlcat(3) calls below */
1666 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1667 if (title_name_enabled) {
1668 if (title_class_enabled)
1669 strlcat(fmtnew, ":", sz);
1670 strlcat(fmtnew, "+T+4<", sz);
1672 if (window_name_enabled) {
1673 if (r->ws->focus->floating)
1674 strlcat(fmtnew, "+F ", sz);
1675 strlcat(fmtnew, "+64W ", sz);
1679 /* finally add the action script output and the version */
1680 strlcat(fmtnew, "+4<+A+4<+V", sz);
1684 bar_replace_pad(char *tmp, int *limit, size_t sz)
1686 /* special case; no limit given, pad one space, instead */
1687 if (*limit == (int)sz - 1)
1689 snprintf(tmp, sz, "%*s", *limit, " ");
1692 /* replaces the bar format character sequences (like in tmux(1)) */
1694 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1698 char tmp[SWM_BAR_MAX];
1702 /* reset strlcat(3) buffer */
1705 /* get number, if any */
1708 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1709 limit = sizeof tmp - 1;
1710 if (limit <= 0 || limit >= (int)sizeof tmp)
1711 limit = sizeof tmp - 1;
1713 /* there is nothing to replace (ie EOL) */
1720 bar_replace_pad(tmp, &limit, sizeof tmp);
1723 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1726 bar_class_name(tmp, sizeof tmp, r);
1729 bar_workspace_name(tmp, sizeof tmp, r);
1732 bar_window_float(tmp, sizeof tmp, r);
1735 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1738 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1741 bar_class_title_name(tmp, sizeof tmp, r);
1744 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1747 bar_title_name(tmp, sizeof tmp, r);
1750 bar_urgent(tmp, sizeof tmp);
1753 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1756 bar_window_name(tmp, sizeof tmp, r);
1759 /* unknown character sequence; copy as-is */
1760 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1766 if ((int)len < limit)
1768 while (limit-- > 0) {
1769 if (*offrep >= sz - 1)
1771 fmtrep[(*offrep)++] = *ptr++;
1779 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1784 while (*fmt != '\0') {
1786 /* skip ordinary characters */
1789 fmtrep[off++] = *fmt++;
1793 /* character sequence found; replace it */
1794 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1803 bar_fmt_expand(char *fmtexp, size_t sz)
1810 /* start by grabbing the current time and date */
1812 localtime_r(&tmt, &tm);
1814 /* figure out what to expand */
1815 if (bar_format != NULL)
1817 else if (bar_format == NULL && clock_enabled)
1819 /* if nothing to expand bail out */
1825 /* copy as-is, just in case the format shouldn't be expanded below */
1826 strlcpy(fmtexp, fmt, sz);
1827 /* finally pass the string through strftime(3) */
1828 #ifndef SWM_DENY_CLOCK_FORMAT
1829 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1830 warnx("format too long");
1838 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1839 char fmtrep[SWM_BAR_MAX];
1841 struct swm_region *r;
1843 /* expand the format by first passing it through strftime(3) */
1844 bar_fmt_expand(fmtexp, sizeof fmtexp);
1846 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1847 for (i = 0; i < num_screens; i++) {
1848 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1851 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1852 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1853 if (bar_font_legacy)
1854 bar_print_legacy(r, fmtrep);
1856 bar_print(r, fmtrep);
1867 if (bar_enabled == 0)
1869 if (bar_extra && bar_extra_running) {
1870 /* ignore short reads; it'll correct itself */
1871 while ((b = fgetln(stdin, &len)) != NULL)
1872 if (b && b[len - 1] == '\n') {
1874 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1876 if (b == NULL && errno != EAGAIN) {
1877 warn("bar_update: bar_extra failed");
1881 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1890 /* suppress unused warning since var is needed */
1897 bar_toggle(struct swm_region *r, union arg *args)
1899 struct swm_region *tmpr;
1902 /* suppress unused warnings since vars are needed */
1906 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1908 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1910 for (i = 0; i < num_screens; i++)
1911 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1913 xcb_unmap_window(conn, tmpr->bar->id);
1915 for (i = 0; i < num_screens; i++)
1916 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1918 map_window_raised(tmpr->bar->id);
1921 bar_enabled = !bar_enabled;
1924 /* must be after stack */
1933 struct swm_region *r;
1937 /* do this here because the conf file is in memory */
1938 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1939 /* launch external status app */
1940 bar_extra_running = 1;
1941 if (pipe(bar_pipe) == -1)
1942 err(1, "pipe error");
1943 socket_setnonblock(bar_pipe[0]);
1944 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1945 if (dup2(bar_pipe[0], 0) == -1)
1947 if (dup2(bar_pipe[1], 1) == -1)
1949 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1950 err(1, "could not disable SIGPIPE");
1951 switch (bar_pid = fork()) {
1953 err(1, "cannot fork");
1957 execvp(bar_argv[0], bar_argv);
1958 err(1, "%s external app failed", bar_argv[0]);
1960 default: /* parent */
1966 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1967 for (i = 0; i < num_screens; i++)
1968 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1971 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1972 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1973 xcb_change_window_attributes(conn, r->bar->id,
1974 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1984 while ((s = index(s, '-'))) {
1989 return (count == 14);
1995 char *default_string;
1996 char **missing_charsets;
1997 int num_missing_charsets = 0;
2001 XFreeFontSet(display, bar_fs);
2005 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2007 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2008 &num_missing_charsets, &default_string);
2010 if (num_missing_charsets > 0) {
2011 warnx("Unable to load charset(s):");
2013 for (i = 0; i < num_missing_charsets; ++i)
2014 warnx("%s", missing_charsets[i]);
2016 XFreeStringList(missing_charsets);
2018 if (strcmp(default_string, ""))
2019 warnx("Glyphs from those sets will be replaced "
2020 "by '%s'.", default_string);
2022 warnx("Glyphs from those sets won't be drawn.");
2026 errx(1, "Error creating font set structure.");
2028 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2030 bar_height = bar_fs_extents->max_logical_extent.height +
2031 2 * bar_border_width;
2038 xft_init(struct swm_region *r)
2040 char *font, *d, *search;
2043 if (bar_font == NULL) {
2044 if ((d = strdup(bar_fonts)) == NULL)
2045 errx(1, "insufficient memory.");
2047 while ((font = strsep(&search, ",")) != NULL) {
2051 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2054 bar_font = XftFontOpenXlfd(display, r->s->idx,
2057 bar_font = XftFontOpenName(display, r->s->idx,
2062 warnx("unable to load font %s", font);
2065 DNPRINTF(SWM_D_INIT, "successfully opened "
2073 if (bar_font == NULL)
2074 errx(1, "unable to open a font");
2076 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2078 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2079 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2080 warn("unable to allocate Xft color");
2082 bar_height = bar_font->height + 2 * bar_border_width;
2089 bar_setup(struct swm_region *r)
2091 xcb_screen_t *screen;
2094 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2097 if ((screen = get_screen(r->s->idx)) == NULL)
2098 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2103 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2104 err(1, "bar_setup: calloc: failed to allocate memory.");
2106 if (bar_font_legacy)
2112 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2113 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2114 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2116 r->bar->id = xcb_generate_id(conn);
2117 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2118 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2119 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2121 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2122 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2123 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2124 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2125 | XCB_CW_EVENT_MASK, wa);
2127 r->bar->buffer = xcb_generate_id(conn);
2128 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2129 WIDTH(r->bar), HEIGHT(r->bar));
2132 xcb_randr_select_input(conn, r->bar->id,
2133 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2136 map_window_raised(r->bar->id);
2138 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2139 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2142 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2143 err(1, "could not install bar_signal");
2148 bar_cleanup(struct swm_region *r)
2152 xcb_destroy_window(conn, r->bar->id);
2153 xcb_free_pixmap(conn, r->bar->buffer);
2159 set_win_state(struct ws_win *win, uint16_t state)
2161 uint16_t data[2] = { state, XCB_ATOM_NONE };
2163 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2168 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2169 a_state, 32, 2, data);
2173 getstate(xcb_window_t w)
2175 uint32_t result = 0;
2176 xcb_get_property_cookie_t c;
2177 xcb_get_property_reply_t *r;
2179 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2180 r = xcb_get_property_reply(conn, c, NULL);
2183 if (r->type == a_state && r->format == 32 && r->length == 2)
2184 result = *((uint32_t *)xcb_get_property_value(r));
2188 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2194 version(struct swm_region *r, union arg *args)
2196 /* suppress unused warnings since vars are needed */
2200 bar_version = !bar_version;
2202 snprintf(bar_vertext, sizeof bar_vertext,
2203 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2205 strlcpy(bar_vertext, "", sizeof bar_vertext);
2212 client_msg(struct ws_win *win, xcb_atom_t a)
2214 xcb_client_message_event_t ev;
2219 bzero(&ev, sizeof ev);
2220 ev.response_type = XCB_CLIENT_MESSAGE;
2221 ev.window = win->id;
2224 ev.data.data32[0] = a;
2225 ev.data.data32[1] = XCB_CURRENT_TIME;
2227 xcb_send_event(conn, 0, win->id,
2228 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2231 /* synthetic response to a ConfigureRequest when not making a change */
2233 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2235 xcb_configure_notify_event_t ce;
2240 /* send notification of unchanged state. */
2241 bzero(&ce, sizeof(ce));
2242 ce.response_type = XCB_CONFIGURE_NOTIFY;
2245 ce.width = WIDTH(win);
2246 ce.height = HEIGHT(win);
2247 ce.override_redirect = 0;
2252 ce.window = win->id;
2253 ce.border_width = BORDER(win);
2254 ce.above_sibling = XCB_WINDOW_NONE;
2257 ce.event = ev->window;
2258 ce.window = ev->window;
2260 /* make response appear more WM_SIZE_HINTS-compliant */
2261 if (win->sh.flags) {
2262 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2263 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2264 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2265 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2266 SH_INC_W(win), SH_INC_H(win));
2271 /* the hint may be set... to 0! */
2272 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2273 ce.width = SH_MIN_W(win);
2274 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2275 ce.height = SH_MIN_H(win);
2280 /* may also be advertized as 0 */
2281 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2282 ce.width = SH_MAX_W(win);
2283 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2284 ce.height = SH_MAX_H(win);
2287 /* resize increment. */
2289 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2290 ce.width -= (ce.width - SH_MIN_W(win)) %
2292 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2293 ce.height -= (ce.height - SH_MIN_H(win)) %
2297 /* adjust x and y for requested border_width. */
2298 ce.x += BORDER(win) - ev->border_width;
2299 ce.y += BORDER(win) - ev->border_width;
2300 ce.border_width = ev->border_width;
2301 ce.above_sibling = ev->sibling;
2304 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2305 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2306 ce.y, ce.width, ce.height, ce.border_width);
2308 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2313 count_win(struct workspace *ws, int count_transient)
2318 TAILQ_FOREACH(win, &ws->winlist, entry) {
2319 if (count_transient == 0 && win->floating)
2321 if (count_transient == 0 && win->transient)
2327 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2333 quit(struct swm_region *r, union arg *args)
2335 /* suppress unused warnings since vars are needed */
2339 DNPRINTF(SWM_D_MISC, "quit\n");
2344 unmap_window(struct ws_win *win)
2346 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2351 /* don't unmap again */
2352 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2355 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2357 xcb_unmap_window(conn, win->id);
2358 xcb_change_window_attributes(conn, win->id,
2359 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2366 int i, j, num_screens;
2368 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2369 for (i = 0; i < num_screens; i++)
2370 for (j = 0; j < workspace_limit; j++)
2371 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2376 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2378 xcb_key_press_event_t event;
2379 xcb_keycode_t *keycode;
2384 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2386 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2389 bzero(&event, sizeof(event));
2390 event.event = win->id;
2391 event.root = win->s->root;
2392 event.child = XCB_WINDOW_NONE;
2393 event.time = XCB_CURRENT_TIME;
2394 event.event_x = X(win);
2395 event.event_y = Y(win);
2398 event.same_screen = 1;
2399 event.detail = *keycode;
2400 event.state = modifiers;
2402 event.response_type = XCB_KEY_PRESS;
2403 xcb_send_event(conn, 1, win->id,
2404 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2406 event.response_type = XCB_KEY_RELEASE;
2407 xcb_send_event(conn, 1, win->id,
2408 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2414 restart(struct swm_region *r, union arg *args)
2416 /* suppress unused warning since var is needed */
2420 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2424 execvp(start_argv[0], start_argv);
2425 warn("execvp failed");
2430 root_to_region(xcb_window_t root)
2432 struct swm_region *r = NULL;
2434 xcb_query_pointer_reply_t *qpr;
2436 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2438 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2439 for (i = 0; i < num_screens; i++)
2440 if (screens[i].root == root)
2443 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2444 screens[i].root), NULL);
2447 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2448 qpr->root_x, qpr->root_y);
2449 /* choose a region based on pointer location */
2450 TAILQ_FOREACH(r, &screens[i].rl, entry)
2451 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2452 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2458 r = TAILQ_FIRST(&screens[i].rl);
2464 find_unmanaged_window(xcb_window_t id)
2467 int i, j, num_screens;
2469 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2470 for (i = 0; i < num_screens; i++)
2471 for (j = 0; j < workspace_limit; j++)
2472 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2480 find_window(xcb_window_t id)
2483 int i, j, num_screens;
2484 xcb_query_tree_reply_t *r;
2486 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2487 for (i = 0; i < num_screens; i++)
2488 for (j = 0; j < workspace_limit; j++)
2489 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2493 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2497 /* if we were looking for the parent return that window instead */
2498 if (r->parent == 0 || r->root == r->parent) {
2503 /* look for parent */
2504 for (i = 0; i < num_screens; i++)
2505 for (j = 0; j < workspace_limit; j++)
2506 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2507 if (r->parent == win->id) {
2517 spawn(int ws_idx, union arg *args, int close_fd)
2522 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2524 close(xcb_get_file_descriptor(conn));
2526 setenv("LD_PRELOAD", SWM_LIB, 1);
2528 if (asprintf(&ret, "%d", ws_idx) == -1) {
2529 warn("spawn: asprintf SWM_WS");
2532 setenv("_SWM_WS", ret, 1);
2536 if (asprintf(&ret, "%d", getpid()) == -1) {
2537 warn("spawn: asprintf _SWM_PID");
2540 setenv("_SWM_PID", ret, 1);
2544 if (setsid() == -1) {
2545 warn("spawn: setsid");
2551 * close stdin and stdout to prevent interaction between apps
2552 * and the baraction script
2553 * leave stderr open to record errors
2555 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2556 warn("spawn: open");
2559 dup2(fd, STDIN_FILENO);
2560 dup2(fd, STDOUT_FILENO);
2565 execvp(args->argv[0], args->argv);
2567 warn("spawn: execvp");
2572 kill_refs(struct ws_win *win)
2574 int i, x, num_screens;
2575 struct swm_region *r;
2576 struct workspace *ws;
2581 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2582 for (i = 0; i < num_screens; i++)
2583 TAILQ_FOREACH(r, &screens[i].rl, entry)
2584 for (x = 0; x < workspace_limit; x++) {
2586 if (win == ws->focus)
2588 if (win == ws->focus_prev)
2589 ws->focus_prev = NULL;
2594 validate_win(struct ws_win *testwin)
2597 struct workspace *ws;
2598 struct swm_region *r;
2599 int i, x, num_screens;
2601 if (testwin == NULL)
2604 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2605 for (i = 0; i < num_screens; i++)
2606 TAILQ_FOREACH(r, &screens[i].rl, entry)
2607 for (x = 0; x < workspace_limit; x++) {
2609 TAILQ_FOREACH(win, &ws->winlist, entry)
2617 validate_ws(struct workspace *testws)
2619 struct swm_region *r;
2620 struct workspace *ws;
2621 int i, x, num_screens;
2623 /* validate all ws */
2624 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2625 for (i = 0; i < num_screens; i++)
2626 TAILQ_FOREACH(r, &screens[i].rl, entry)
2627 for (x = 0; x < workspace_limit; x++) {
2636 unfocus_win(struct ws_win *win)
2638 xcb_window_t none = XCB_WINDOW_NONE;
2640 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2644 if (win->ws == NULL)
2647 if (validate_ws(win->ws))
2648 return; /* XXX this gets hit with thunderbird, needs fixing */
2650 if (win->ws->r == NULL)
2653 if (validate_win(win)) {
2658 if (win->ws->focus == win) {
2659 win->ws->focus = NULL;
2660 win->ws->focus_prev = win;
2663 if (validate_win(win->ws->focus)) {
2664 kill_refs(win->ws->focus);
2665 win->ws->focus = NULL;
2667 if (validate_win(win->ws->focus_prev)) {
2668 kill_refs(win->ws->focus_prev);
2669 win->ws->focus_prev = NULL;
2672 grabbuttons(win, 0);
2673 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2674 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2675 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2676 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2683 int i, j, num_screens;
2685 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2687 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2688 for (i = 0; i < num_screens; i++)
2689 for (j = 0; j < workspace_limit; j++)
2690 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2695 focus_win(struct ws_win *win)
2697 struct ws_win *cfw = NULL;
2698 xcb_get_input_focus_reply_t *r;
2699 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2701 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2705 if (win->ws == NULL)
2708 if (validate_ws(win->ws))
2709 return; /* XXX this gets hit with thunderbird, needs fixing */
2711 if (validate_win(win)) {
2716 if (validate_win(win)) {
2721 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2723 cur_focus = r->focus;
2726 if ((cfw = find_window(cur_focus)) != NULL)
2730 /* use larger hammer since the window was killed somehow */
2731 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2732 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2733 xcb_change_window_attributes(conn, cfw->id,
2734 XCB_CW_BORDER_PIXEL,
2735 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2739 win->ws->focus = win;
2741 if (win->ws->r != NULL) {
2743 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2744 win->id, XCB_CURRENT_TIME);
2745 grabbuttons(win, 1);
2746 xcb_change_window_attributes(conn, win->id,
2747 XCB_CW_BORDER_PIXEL,
2748 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2749 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2750 win->ws->always_raise)
2751 map_window_raised(win->id);
2753 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2754 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2762 event_drain(uint8_t rt)
2764 xcb_generic_event_t *evt;
2767 while ((evt = xcb_poll_for_event(conn))) {
2768 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2776 switchws(struct swm_region *r, union arg *args)
2778 int wsid = args->id, unmap_old = 0;
2779 struct swm_region *this_r, *other_r;
2781 struct workspace *new_ws, *old_ws;
2787 if (wsid >= workspace_limit)
2791 old_ws = this_r->ws;
2792 new_ws = &this_r->s->ws[wsid];
2794 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2795 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2797 if (new_ws == NULL || old_ws == NULL)
2799 if (new_ws == old_ws)
2802 other_r = new_ws->r;
2803 if (other_r == NULL) {
2804 /* the other workspace is hidden, hide this one */
2808 /* the other ws is visible in another region, exchange them */
2809 other_r->ws_prior = new_ws;
2810 other_r->ws = old_ws;
2811 old_ws->r = other_r;
2813 this_r->ws_prior = old_ws;
2814 this_r->ws = new_ws;
2817 /* this is needed so that we can click on a window after a restart */
2821 a.id = SWM_ARG_ID_FOCUSCUR;
2822 focus(new_ws->r, &a);
2824 /* unmap old windows */
2826 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2829 if (focus_mode == SWM_FOCUS_DEFAULT)
2830 event_drain(XCB_ENTER_NOTIFY);
2834 DNPRINTF(SWM_D_WS, "switchws: done\n");
2838 cyclews(struct swm_region *r, union arg *args)
2841 struct swm_screen *s = r->s;
2844 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2845 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2850 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2853 case SWM_ARG_ID_CYCLEWS_UP:
2854 if (a.id < workspace_limit - 1)
2859 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2862 case SWM_ARG_ID_CYCLEWS_DOWN:
2866 a.id = workspace_limit - 1;
2873 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2875 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2879 } while (a.id != r->ws->idx);
2883 priorws(struct swm_region *r, union arg *args)
2889 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2890 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2892 if (r->ws_prior == NULL)
2895 a.id = r->ws_prior->idx;
2900 cyclescr(struct swm_region *r, union arg *args)
2902 struct swm_region *rr = NULL;
2904 int i, x, y, num_screens;
2906 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2907 /* do nothing if we don't have more than one screen */
2908 if (!(num_screens > 1 || outputs > 1))
2913 case SWM_ARG_ID_CYCLESC_UP:
2914 rr = TAILQ_NEXT(r, entry);
2916 rr = TAILQ_FIRST(&screens[i].rl);
2918 case SWM_ARG_ID_CYCLESC_DOWN:
2919 rr = TAILQ_PREV(r, swm_region_list, entry);
2921 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2929 /* move mouse to region */
2931 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2932 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2935 a.id = SWM_ARG_ID_FOCUSCUR;
2938 if (rr->ws->focus) {
2939 /* move to focus window */
2940 x = X(rr->ws->focus) + 1;
2941 y = Y(rr->ws->focus) + 1;
2942 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2948 sort_windows(struct ws_win_list *wl)
2950 struct ws_win *win, *parent, *nxt;
2955 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2956 nxt = TAILQ_NEXT(win, entry);
2957 if (win->transient) {
2958 parent = find_window(win->transient);
2959 if (parent == NULL) {
2960 warnx("not possible bug");
2963 TAILQ_REMOVE(wl, win, entry);
2964 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2971 swapwin(struct swm_region *r, union arg *args)
2973 struct ws_win *target, *source;
2974 struct ws_win *cur_focus;
2975 struct ws_win_list *wl;
2978 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2979 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2981 cur_focus = r->ws->focus;
2982 if (cur_focus == NULL)
2986 wl = &source->ws->winlist;
2989 case SWM_ARG_ID_SWAPPREV:
2990 if (source->transient)
2991 source = find_window(source->transient);
2992 target = TAILQ_PREV(source, ws_win_list, entry);
2993 if (target && target->transient)
2994 target = find_window(target->transient);
2995 TAILQ_REMOVE(wl, source, entry);
2997 TAILQ_INSERT_TAIL(wl, source, entry);
2999 TAILQ_INSERT_BEFORE(target, source, entry);
3001 case SWM_ARG_ID_SWAPNEXT:
3002 target = TAILQ_NEXT(source, entry);
3003 /* move the parent and let the sort handle the move */
3004 if (source->transient)
3005 source = find_window(source->transient);
3006 TAILQ_REMOVE(wl, source, entry);
3008 TAILQ_INSERT_HEAD(wl, source, entry);
3010 TAILQ_INSERT_AFTER(wl, target, source, entry);
3012 case SWM_ARG_ID_SWAPMAIN:
3013 target = TAILQ_FIRST(wl);
3014 if (target == source) {
3015 if (source->ws->focus_prev != NULL &&
3016 source->ws->focus_prev != target)
3017 source = source->ws->focus_prev;
3021 if (target == NULL || source == NULL)
3023 source->ws->focus_prev = target;
3024 TAILQ_REMOVE(wl, target, entry);
3025 TAILQ_INSERT_BEFORE(source, target, entry);
3026 TAILQ_REMOVE(wl, source, entry);
3027 TAILQ_INSERT_HEAD(wl, source, entry);
3029 case SWM_ARG_ID_MOVELAST:
3030 TAILQ_REMOVE(wl, source, entry);
3031 TAILQ_INSERT_TAIL(wl, source, entry);
3034 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3046 focus_prev(struct ws_win *win)
3048 struct ws_win *winfocus = NULL;
3049 struct ws_win *cur_focus = NULL;
3050 struct ws_win_list *wl = NULL;
3051 struct workspace *ws = NULL;
3053 if (!(win && win->ws))
3058 cur_focus = ws->focus;
3060 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3061 WINID(win), WINID(cur_focus));
3063 /* pickle, just focus on whatever */
3064 if (cur_focus == NULL) {
3065 /* use prev_focus if valid */
3066 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3067 find_window(WINID(ws->focus_prev)))
3068 winfocus = ws->focus_prev;
3072 /* if transient focus on parent */
3073 if (cur_focus->transient) {
3074 winfocus = find_window(cur_focus->transient);
3078 /* if in max_stack try harder */
3079 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3080 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3081 if (cur_focus != ws->focus_prev)
3082 winfocus = ws->focus_prev;
3084 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3089 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
3091 if (winfocus == NULL || winfocus == win) {
3092 switch (focus_close) {
3093 case SWM_STACK_BOTTOM:
3094 winfocus = TAILQ_FIRST(wl);
3097 winfocus = TAILQ_LAST(wl, ws_win_list);
3099 case SWM_STACK_ABOVE:
3100 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3101 if (focus_close_wrap)
3102 winfocus = TAILQ_FIRST(wl);
3104 winfocus = TAILQ_PREV(cur_focus,
3105 ws_win_list, entry);
3108 case SWM_STACK_BELOW:
3109 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3111 if (focus_close_wrap)
3112 winfocus = TAILQ_LAST(wl, ws_win_list);
3114 winfocus = TAILQ_NEXT(cur_focus, entry);
3120 if (winfocus == NULL) {
3121 if (focus_default == SWM_STACK_TOP)
3122 winfocus = TAILQ_LAST(wl, ws_win_list);
3124 winfocus = TAILQ_FIRST(wl);
3128 focus_magic(winfocus);
3132 focus(struct swm_region *r, union arg *args)
3134 struct ws_win *winfocus = NULL, *head;
3135 struct ws_win *cur_focus = NULL;
3136 struct ws_win_list *wl = NULL;
3137 struct workspace *ws = NULL;
3143 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3145 /* treat FOCUS_CUR special */
3146 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3147 if (r->ws->focus && r->ws->focus->iconic == 0)
3148 winfocus = r->ws->focus;
3149 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3150 winfocus = r->ws->focus_prev;
3152 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3153 if (winfocus->iconic == 0)
3156 focus_magic(winfocus);
3160 if ((cur_focus = r->ws->focus) == NULL)
3164 if (TAILQ_EMPTY(wl))
3166 /* make sure there is at least one uniconified window */
3168 TAILQ_FOREACH(winfocus, wl, entry)
3169 if (winfocus->iconic == 0) {
3177 case SWM_ARG_ID_FOCUSPREV:
3178 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3180 head = TAILQ_LAST(wl, ws_win_list);
3182 if (WINID(winfocus) == cur_focus->transient) {
3183 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3185 head = TAILQ_LAST(wl, ws_win_list);
3190 if (winfocus && winfocus->iconic) {
3191 while (winfocus != cur_focus) {
3192 if (winfocus == NULL)
3193 winfocus = TAILQ_LAST(wl, ws_win_list);
3194 if (winfocus->iconic == 0)
3196 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3202 case SWM_ARG_ID_FOCUSNEXT:
3203 head = TAILQ_NEXT(cur_focus, entry);
3205 head = TAILQ_FIRST(wl);
3209 if (winfocus && winfocus->iconic) {
3210 while (winfocus != cur_focus) {
3211 if (winfocus == NULL)
3212 winfocus = TAILQ_FIRST(wl);
3213 if (winfocus->iconic == 0)
3215 winfocus = TAILQ_NEXT(winfocus, entry);
3220 case SWM_ARG_ID_FOCUSMAIN:
3221 winfocus = TAILQ_FIRST(wl);
3222 if (winfocus == cur_focus)
3223 winfocus = cur_focus->ws->focus_prev;
3230 focus_magic(winfocus);
3236 cycle_layout(struct swm_region *r, union arg *args)
3238 struct workspace *ws = r->ws;
3241 /* suppress unused warning since var is needed */
3244 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3247 if (ws->cur_layout->l_stack == NULL)
3248 ws->cur_layout = &layouts[0];
3253 if (focus_mode == SWM_FOCUS_DEFAULT)
3254 event_drain(XCB_ENTER_NOTIFY);
3256 a.id = SWM_ARG_ID_FOCUSCUR;
3261 stack_config(struct swm_region *r, union arg *args)
3263 struct workspace *ws = r->ws;
3265 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3268 if (ws->cur_layout->l_config != NULL)
3269 ws->cur_layout->l_config(ws, args->id);
3271 if (args->id != SWM_ARG_ID_STACKINIT)
3278 struct swm_geometry g;
3279 struct swm_region *r;
3285 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3287 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3288 for (i = 0; i < num_screens; i++) {
3292 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3293 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3294 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3296 /* start with screen geometry, adjust for bar */
3298 g.w -= 2 * border_width;
3299 g.h -= 2 * border_width;
3305 r->ws->cur_layout->l_stack(r->ws, &g);
3306 r->ws->cur_layout->l_string(r->ws);
3307 /* save r so we can track region changes */
3314 if (focus_mode == SWM_FOCUS_DEFAULT)
3315 event_drain(XCB_ENTER_NOTIFY);
3317 DNPRINTF(SWM_D_STACK, "stack: end\n");
3321 store_float_geom(struct ws_win *win, struct swm_region *r)
3323 /* retain window geom and region geom */
3324 win->g_float = win->g;
3325 win->g_float.x -= X(r);
3326 win->g_float.y -= Y(r);
3327 win->g_floatvalid = 1;
3328 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3329 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3330 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3331 win->g_float.w, win->g_float.h);
3335 stack_floater(struct ws_win *win, struct swm_region *r)
3340 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3343 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3344 * geom on ws switches or return from max mode
3346 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3347 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3348 /* refloat at last floating relative position */
3349 win->g = win->g_float;
3354 win->floatmaxed = 0;
3357 * if set to fullscreen mode, configure window to maximum size.
3359 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3360 if (!win->g_floatvalid)
3361 store_float_geom(win, win->ws->r);
3363 win->g = win->ws->r->g;
3367 * remove border on fullscreen floater when in fullscreen mode or when
3368 * the quirk is present.
3370 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3371 ((win->quirks & SWM_Q_FULLSCREEN) &&
3372 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3373 if (win->bordered) {
3375 X(win) += border_width;
3376 Y(win) += border_width;
3378 } else if (!win->bordered) {
3380 X(win) -= border_width;
3381 Y(win) -= border_width;
3384 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3385 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3386 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3391 * floaters and transients are auto-centred unless moved
3394 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3395 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3398 /* keep window within region bounds */
3399 constrain_window(win, r, 0);
3405 * Send keystrokes to terminal to decrease/increase the font size as the
3406 * window size changes.
3409 adjust_font(struct ws_win *win)
3411 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3412 win->floating || win->transient)
3415 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3416 WIDTH(win) / win->sh.width_inc < term_width &&
3417 win->font_steps < SWM_MAX_FONT_STEPS) {
3418 win->font_size_boundary[win->font_steps] =
3419 (win->sh.width_inc * term_width) + win->sh.base_width;
3422 win->last_inc = win->sh.width_inc;
3423 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3424 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3425 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3428 win->last_inc = win->sh.width_inc;
3429 fake_keypress(win, XK_KP_Add, ShiftMask);
3433 #define SWAPXY(g) do { \
3435 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3436 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3439 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3441 struct swm_geometry win_g, r_g = *g;
3442 struct ws_win *win, *fs_win = NULL;
3443 int i, j, s, stacks;
3444 int w_inc = 1, h_inc, w_base = 1, h_base;
3445 int hrh, extra = 0, h_slice, last_h = 0;
3446 int split, colno, winno, mwin, msize, mscale;
3447 int remain, missing, v_slice, reconfigure;
3449 xcb_get_window_attributes_reply_t *war;
3451 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3452 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3454 winno = count_win(ws, 0);
3455 if (winno == 0 && count_win(ws, 1) == 0)
3458 TAILQ_FOREACH(win, &ws->winlist, entry)
3459 if (win->transient == 0 && win->floating == 0
3460 && win->iconic == 0)
3467 w_inc = win->sh.width_inc;
3468 w_base = win->sh.base_width;
3469 mwin = ws->l_state.horizontal_mwin;
3470 mscale = ws->l_state.horizontal_msize;
3471 stacks = ws->l_state.horizontal_stacks;
3474 w_inc = win->sh.height_inc;
3475 w_base = win->sh.base_height;
3476 mwin = ws->l_state.vertical_mwin;
3477 mscale = ws->l_state.vertical_msize;
3478 stacks = ws->l_state.vertical_stacks;
3482 if (stacks > winno - mwin)
3483 stacks = winno - mwin;
3487 h_slice = r_g.h / SWM_H_SLICE;
3488 if (mwin && winno > mwin) {
3489 v_slice = r_g.w / SWM_V_SLICE;
3493 win_g.w = v_slice * mscale;
3495 if (w_inc > 1 && w_inc < v_slice) {
3496 /* adjust for window's requested size increment */
3497 remain = (win_g.w - w_base) % w_inc;
3503 win_g.x += r_g.w - msize;
3506 colno = split = winno / stacks;
3507 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3508 2 * border_width) / stacks);
3510 hrh = r_g.h / colno;
3511 extra = r_g.h - (colno * hrh);
3512 win_g.h = hrh - 2 * border_width;
3514 /* stack all the tiled windows */
3515 i = j = 0, s = stacks;
3516 TAILQ_FOREACH(win, &ws->winlist, entry) {
3517 if (win->transient != 0 || win->floating != 0)
3519 if (win->iconic != 0)
3522 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3527 if (split && i == split) {
3528 colno = (winno - mwin) / stacks;
3529 if (s <= (winno - mwin) % stacks)
3531 split = split + colno;
3532 hrh = (r_g.h / colno);
3533 extra = r_g.h - (colno * hrh);
3537 win_g.x += win_g.w + 2 * border_width;
3538 win_g.w = (r_g.w - msize -
3539 (stacks * 2 * border_width)) / stacks;
3541 win_g.w += (r_g.w - msize -
3542 (stacks * 2 * border_width)) % stacks;
3546 win_g.h = hrh - 2 * border_width;
3548 h_inc = win->sh.width_inc;
3549 h_base = win->sh.base_width;
3551 h_inc = win->sh.height_inc;
3552 h_base = win->sh.base_height;
3554 if (j == colno - 1) {
3555 win_g.h = hrh + extra;
3556 } else if (h_inc > 1 && h_inc < h_slice) {
3557 /* adjust for window's requested size increment */
3558 remain = (win_g.h - h_base) % h_inc;
3559 missing = h_inc - remain;
3561 if (missing <= extra || j == 0) {
3573 win_g.y += last_h + 2 * border_width;
3575 if (disable_border && bar_enabled == 0 && winno == 1){
3577 win_g.w += 2 * border_width;
3578 win_g.h += 2 * border_width;
3583 if (X(win) != win_g.y || Y(win) != win_g.x ||
3584 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3588 WIDTH(win) = win_g.h;
3589 HEIGHT(win) = win_g.w;
3592 if (X(win) != win_g.x || Y(win) != win_g.y ||
3593 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3597 WIDTH(win) = win_g.w;
3598 HEIGHT(win) = win_g.h;
3602 if (bordered != win->bordered) {
3604 win->bordered = bordered;
3612 war = xcb_get_window_attributes_reply(conn,
3613 xcb_get_window_attributes(conn, win->id),
3616 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3617 map_window_raised(win->id);
3627 /* now, stack all the floaters and transients */
3628 TAILQ_FOREACH(win, &ws->winlist, entry) {
3629 if (win->transient == 0 && win->floating == 0)
3631 if (win->iconic == 1)
3633 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3638 stack_floater(win, ws->r);
3639 map_window_raised(win->id);
3643 stack_floater(fs_win, ws->r);
3644 map_window_raised(fs_win->id);
3649 vertical_config(struct workspace *ws, int id)
3651 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3655 case SWM_ARG_ID_STACKRESET:
3656 case SWM_ARG_ID_STACKINIT:
3657 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3658 ws->l_state.vertical_mwin = 1;
3659 ws->l_state.vertical_stacks = 1;
3661 case SWM_ARG_ID_MASTERSHRINK:
3662 if (ws->l_state.vertical_msize > 1)
3663 ws->l_state.vertical_msize--;
3665 case SWM_ARG_ID_MASTERGROW:
3666 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3667 ws->l_state.vertical_msize++;
3669 case SWM_ARG_ID_MASTERADD:
3670 ws->l_state.vertical_mwin++;
3672 case SWM_ARG_ID_MASTERDEL:
3673 if (ws->l_state.vertical_mwin > 0)
3674 ws->l_state.vertical_mwin--;
3676 case SWM_ARG_ID_STACKINC:
3677 ws->l_state.vertical_stacks++;
3679 case SWM_ARG_ID_STACKDEC:
3680 if (ws->l_state.vertical_stacks > 1)
3681 ws->l_state.vertical_stacks--;
3683 case SWM_ARG_ID_FLIPLAYOUT:
3684 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3692 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3694 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3696 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3700 horizontal_config(struct workspace *ws, int id)
3702 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3705 case SWM_ARG_ID_STACKRESET:
3706 case SWM_ARG_ID_STACKINIT:
3707 ws->l_state.horizontal_mwin = 1;
3708 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3709 ws->l_state.horizontal_stacks = 1;
3711 case SWM_ARG_ID_MASTERSHRINK:
3712 if (ws->l_state.horizontal_msize > 1)
3713 ws->l_state.horizontal_msize--;
3715 case SWM_ARG_ID_MASTERGROW:
3716 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3717 ws->l_state.horizontal_msize++;
3719 case SWM_ARG_ID_MASTERADD:
3720 ws->l_state.horizontal_mwin++;
3722 case SWM_ARG_ID_MASTERDEL:
3723 if (ws->l_state.horizontal_mwin > 0)
3724 ws->l_state.horizontal_mwin--;
3726 case SWM_ARG_ID_STACKINC:
3727 ws->l_state.horizontal_stacks++;
3729 case SWM_ARG_ID_STACKDEC:
3730 if (ws->l_state.horizontal_stacks > 1)
3731 ws->l_state.horizontal_stacks--;
3733 case SWM_ARG_ID_FLIPLAYOUT:
3734 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3742 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3744 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3746 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3749 /* fullscreen view */
3751 max_stack(struct workspace *ws, struct swm_geometry *g)
3753 struct swm_geometry gg = *g;
3754 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3755 int winno, num_screens;
3757 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3762 winno = count_win(ws, 0);
3763 if (winno == 0 && count_win(ws, 1) == 0)
3766 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3767 TAILQ_FOREACH(win, &ws->winlist, entry) {
3768 if (win->transient) {
3770 parent = find_window(win->transient);
3774 if (win->floating && win->floatmaxed == 0 ) {
3776 * retain geometry for retrieval on exit from
3779 store_float_geom(win, ws->r);
3780 win->floatmaxed = 1;
3783 /* only reconfigure if necessary */
3784 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3785 HEIGHT(win) != gg.h) {
3791 WIDTH(win) += 2 * border_width;
3792 HEIGHT(win) += 2 * border_width;
3797 /* unmap only if we don't have multi screen */
3798 if (win != ws->focus)
3799 if (!(num_screens > 1 || outputs > 1))
3803 /* put the last transient on top */
3806 map_window_raised(parent->id);
3807 stack_floater(wintrans, ws->r);
3808 focus_magic(wintrans);
3813 send_to_ws(struct swm_region *r, union arg *args)
3815 int wsid = args->id;
3816 struct ws_win *win = NULL, *parent;
3817 struct workspace *ws, *nws;
3818 char ws_idx_str[SWM_PROPLEN];
3821 if (wsid >= workspace_limit)
3824 if (r && r->ws && r->ws->focus)
3830 if (win->ws->idx == wsid)
3833 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3836 nws = &win->s->ws[wsid];
3838 a.id = SWM_ARG_ID_FOCUSPREV;
3840 if (win->transient) {
3841 parent = find_window(win->transient);
3843 unmap_window(parent);
3844 TAILQ_REMOVE(&ws->winlist, parent, entry);
3845 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3850 TAILQ_REMOVE(&ws->winlist, win, entry);
3851 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3852 if (TAILQ_EMPTY(&ws->winlist))
3853 r->ws->focus = NULL;
3856 /* Try to update the window's workspace property */
3857 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3858 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3860 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3861 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3872 pressbutton(struct swm_region *r, union arg *args)
3874 /* suppress unused warning since var is needed */
3877 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3878 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3879 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3880 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3884 raise_toggle(struct swm_region *r, union arg *args)
3886 /* suppress unused warning since var is needed */
3889 if (r == NULL || r->ws == NULL)
3892 r->ws->always_raise = !r->ws->always_raise;
3894 /* bring floaters back to top */
3895 if (r->ws->always_raise == 0)
3902 iconify(struct swm_region *r, union arg *args)
3906 /* suppress unused warning since var is needed */
3909 if (r->ws->focus == NULL)
3911 unmap_window(r->ws->focus);
3912 update_iconic(r->ws->focus, 1);
3915 r->ws->focus = NULL;
3916 a.id = SWM_ARG_ID_FOCUSCUR;
3923 get_win_name(xcb_window_t win)
3926 xcb_get_property_cookie_t c;
3927 xcb_get_property_reply_t *r;
3929 /* First try _NET_WM_NAME for UTF-8. */
3930 c = xcb_get_property(conn, 0, win, a_netwmname,
3931 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
3932 r = xcb_get_property_reply(conn, c, NULL);
3934 if (!r || r->type == XCB_NONE) {
3936 /* Use WM_NAME instead; no UTF-8. */
3937 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
3938 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
3939 r = xcb_get_property_reply(conn, c, NULL);
3941 if(!r || r->type == XCB_NONE) {
3948 name = strndup(xcb_get_property_value(r),
3949 xcb_get_property_value_length(r));
3956 uniconify(struct swm_region *r, union arg *args)
3963 DNPRINTF(SWM_D_MISC, "uniconify\n");
3965 if (r == NULL || r->ws == NULL)
3968 /* make sure we have anything to uniconify */
3969 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3970 if (win->ws == NULL)
3971 continue; /* should never happen */
3972 if (win->iconic == 0)
3980 search_resp_action = SWM_SEARCH_UNICONIFY;
3982 spawn_select(r, args, "search", &searchpid);
3984 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3987 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3988 if (win->ws == NULL)
3989 continue; /* should never happen */
3990 if (win->iconic == 0)
3993 name = get_win_name(win->id);
3996 fprintf(lfile, "%s.%u\n", name, win->id);
4006 name_workspace(struct swm_region *r, union arg *args)
4010 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4016 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4018 spawn_select(r, args, "name_workspace", &searchpid);
4020 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4023 fprintf(lfile, "%s", "");
4028 search_workspace(struct swm_region *r, union arg *args)
4031 struct workspace *ws;
4034 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4040 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4042 spawn_select(r, args, "search", &searchpid);
4044 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4047 for (i = 0; i < workspace_limit; i++) {
4051 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4052 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4059 search_win_cleanup(void)
4061 struct search_window *sw = NULL;
4063 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4064 xcb_destroy_window(conn, sw->indicator);
4065 TAILQ_REMOVE(&search_wl, sw, entry);
4071 search_win(struct swm_region *r, union arg *args)
4073 struct ws_win *win = NULL;
4074 struct search_window *sw = NULL;
4077 int i, width, height;
4085 XRectangle l_ibox, l_lbox;
4087 DNPRINTF(SWM_D_MISC, "search_win\n");
4090 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4092 spawn_select(r, args, "search", &searchpid);
4094 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4097 TAILQ_INIT(&search_wl);
4100 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4101 if (win->iconic == 1)
4104 sw = calloc(1, sizeof(struct search_window));
4106 warn("search_win: calloc");
4108 search_win_cleanup();
4114 snprintf(s, sizeof s, "%d", i);
4117 w = xcb_generate_id(conn);
4118 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4119 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4121 if (bar_font_legacy) {
4122 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4123 width = l_lbox.width + 4;
4124 height = bar_fs_extents->max_logical_extent.height + 4;
4126 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4128 width = info.width + 4;
4129 height = bar_font->height + 4;
4132 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4133 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4134 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4135 XCB_CW_BORDER_PIXEL, wa);
4137 map_window_raised(w);
4140 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4142 if (bar_font_legacy) {
4143 l_gcv.graphics_exposures = 0;
4144 l_draw = XCreateGC(display, w, 0, &l_gcv);
4146 XSetForeground(display, l_draw,
4147 r->s->c[SWM_S_COLOR_BAR].pixel);
4149 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4150 (bar_fs_extents->max_logical_extent.height -
4151 l_lbox.height) / 2 - l_lbox.y, s, len);
4153 XFreeGC(display, l_draw);
4156 draw = XftDrawCreate(display, w,
4157 DefaultVisual(display, r->s->idx),
4158 DefaultColormap(display, r->s->idx));
4160 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4161 (HEIGHT(r->bar) + bar_font->height) / 2 -
4162 bar_font->descent, (FcChar8 *)s, len);
4164 XftDrawDestroy(draw);
4167 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4169 fprintf(lfile, "%d\n", i);
4179 search_resp_uniconify(char *resp, unsigned long len)
4185 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4187 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4188 if (win->iconic == 0)
4190 name = get_win_name(win->id);
4193 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4198 if (strncmp(s, resp, len) == 0) {
4199 /* XXX this should be a callback to generalize */
4200 update_iconic(win, 0);
4209 search_resp_name_workspace(char *resp, unsigned long len)
4211 struct workspace *ws;
4213 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4215 if (search_r->ws == NULL)
4220 free(search_r->ws->name);
4221 search_r->ws->name = NULL;
4225 ws->name = strdup(resp);
4226 if (ws->name == NULL) {
4227 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4228 "strdup: %s", strerror(errno));
4235 search_resp_search_workspace(char *resp)
4242 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4246 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4253 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4255 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4262 switchws(search_r, &a);
4266 search_resp_search_window(char *resp)
4271 struct search_window *sw;
4273 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4277 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4282 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4284 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4291 TAILQ_FOREACH(sw, &search_wl, entry)
4292 if (idx == sw->idx) {
4298 #define MAX_RESP_LEN 1024
4301 search_do_resp(void)
4307 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4312 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4313 warn("search: calloc");
4317 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4319 warn("search: read error");
4322 resp[rbytes] = '\0';
4325 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4326 * newline, so work around that by sanitizing the resp now.
4328 resp[strcspn(resp, "\n")] = '\0';
4331 switch (search_resp_action) {
4332 case SWM_SEARCH_UNICONIFY:
4333 search_resp_uniconify(resp, len);
4335 case SWM_SEARCH_NAME_WORKSPACE:
4336 search_resp_name_workspace(resp, len);
4338 case SWM_SEARCH_SEARCH_WORKSPACE:
4339 search_resp_search_workspace(resp);
4341 case SWM_SEARCH_SEARCH_WINDOW:
4342 search_resp_search_window(resp);
4347 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4348 search_win_cleanup();
4350 search_resp_action = SWM_SEARCH_NONE;
4351 close(select_resp_pipe[0]);
4356 wkill(struct swm_region *r, union arg *args)
4358 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4360 if (r->ws->focus == NULL)
4363 if (args->id == SWM_ARG_ID_KILLWINDOW)
4364 xcb_kill_client(conn, r->ws->focus->id);
4366 if (r->ws->focus->can_delete)
4367 client_msg(r->ws->focus, a_delete);
4374 floating_toggle_win(struct ws_win *win)
4376 struct swm_region *r;
4386 /* reject floating toggles in max stack mode */
4387 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4390 if (win->floating) {
4391 if (!win->floatmaxed) {
4392 /* retain position for refloat */
4393 store_float_geom(win, r);
4397 if (win->g_floatvalid) {
4398 /* refloat at last floating relative position */
4399 X(win) = win->g_float.x + X(r);
4400 Y(win) = win->g_float.y + Y(r);
4401 WIDTH(win) = win->g_float.w;
4402 HEIGHT(win) = win->g_float.h;
4407 ewmh_update_actions(win);
4413 floating_toggle(struct swm_region *r, union arg *args)
4415 struct ws_win *win = r->ws->focus;
4418 /* suppress unused warning since var is needed */
4424 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4425 _NET_WM_STATE_TOGGLE);
4429 if (win == win->ws->focus) {
4430 a.id = SWM_ARG_ID_FOCUSCUR;
4431 focus(win->ws->r, &a);
4435 event_drain(XCB_ENTER_NOTIFY);
4439 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4441 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4443 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4445 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4448 if (X(win) + BORDER(win) < X(r)) {
4450 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4452 X(win) = X(r) - BORDER(win);
4455 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4457 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4459 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4462 if (Y(win) + BORDER(win) < Y(r)) {
4464 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4466 Y(win) = Y(r) - BORDER(win);
4472 if (HEIGHT(win) < 1)
4478 update_window(struct ws_win *win)
4483 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4484 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4485 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4489 wc[3] = HEIGHT(win);
4490 wc[4] = BORDER(win);
4492 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4493 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4494 wc[3], YESNO(win->bordered));
4496 xcb_configure_window(conn, win->id, mask, wc);
4499 #define SWM_RESIZE_STEPS (50)
4502 resize(struct ws_win *win, union arg *args)
4504 xcb_timestamp_t timestamp = 0;
4505 struct swm_region *r = NULL;
4506 int resize_step = 0;
4507 struct swm_geometry g;
4508 int top = 0, left = 0, resizing;
4510 unsigned int shape; /* cursor style */
4511 xcb_cursor_t cursor;
4512 xcb_font_t cursor_font;
4513 xcb_query_pointer_reply_t *xpr;
4514 xcb_generic_event_t *evt;
4515 xcb_motion_notify_event_t *mne;
4521 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4524 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4525 "transient: 0x%x\n", win->id, YESNO(win->floating),
4528 if (!(win->transient != 0 || win->floating != 0))
4531 /* reject resizes in max mode for floaters (transient ok) */
4532 if (win->floatmaxed)
4536 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4542 case SWM_ARG_ID_WIDTHSHRINK:
4543 WIDTH(win) -= SWM_RESIZE_STEPS;
4546 case SWM_ARG_ID_WIDTHGROW:
4547 WIDTH(win) += SWM_RESIZE_STEPS;
4550 case SWM_ARG_ID_HEIGHTSHRINK:
4551 HEIGHT(win) -= SWM_RESIZE_STEPS;
4554 case SWM_ARG_ID_HEIGHTGROW:
4555 HEIGHT(win) += SWM_RESIZE_STEPS;
4562 constrain_window(win, r, 1);
4564 store_float_geom(win,r);
4568 /* get cursor offset from window root */
4569 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4576 if (xpr->win_x < WIDTH(win) / 2)
4579 if (xpr->win_y < HEIGHT(win) / 2)
4582 if (args->id == SWM_ARG_ID_CENTER)
4585 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4587 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4589 cursor_font = xcb_generate_id(conn);
4590 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4592 cursor = xcb_generate_id(conn);
4593 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4594 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4596 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4597 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4602 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4603 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4604 case XCB_BUTTON_RELEASE:
4605 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4608 case XCB_MOTION_NOTIFY:
4609 mne = (xcb_motion_notify_event_t *)evt;
4610 /* cursor offset/delta from start of the operation */
4611 dx = mne->root_x - xpr->root_x;
4612 dy = mne->root_y - xpr->root_y;
4618 if (args->id == SWM_ARG_ID_CENTER) {
4619 if (g.h / 2 + dy < 1)
4623 HEIGHT(win) = g.h + 2 * dy;
4631 HEIGHT(win) = g.h + dy;
4638 if (args->id == SWM_ARG_ID_CENTER) {
4639 if (g.w / 2 + dx < 1)
4643 WIDTH(win) = g.w + 2 * dx;
4651 WIDTH(win) = g.w + dx;
4654 constrain_window(win, r, 1);
4656 /* not free, don't sync more than 120 times / second */
4657 if ((mne->time - timestamp) > (1000 / 120) ) {
4658 timestamp = mne->time;
4673 store_float_geom(win,r);
4675 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4676 xcb_free_cursor(conn, cursor);
4677 xcb_close_font(conn, cursor_font);
4679 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4683 resize_step(struct swm_region *r, union arg *args)
4685 struct ws_win *win = NULL;
4687 if (r && r->ws && r->ws->focus)
4695 #define SWM_MOVE_STEPS (50)
4698 move(struct ws_win *win, union arg *args)
4700 xcb_timestamp_t timestamp = 0;
4701 int move_step = 0, moving;
4702 struct swm_region *r = NULL;
4703 xcb_font_t cursor_font;
4704 xcb_cursor_t cursor;
4705 xcb_query_pointer_reply_t *qpr;
4706 xcb_generic_event_t *evt;
4707 xcb_motion_notify_event_t *mne;
4713 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4716 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4717 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4719 /* in max_stack mode should only move transients */
4720 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4724 if (win->floating == 0 && !win->transient) {
4725 store_float_geom(win, r);
4726 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4729 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4736 case SWM_ARG_ID_MOVELEFT:
4737 X(win) -= (SWM_MOVE_STEPS - border_width);
4740 case SWM_ARG_ID_MOVERIGHT:
4741 X(win) += (SWM_MOVE_STEPS - border_width);
4744 case SWM_ARG_ID_MOVEUP:
4745 Y(win) -= (SWM_MOVE_STEPS - border_width);
4748 case SWM_ARG_ID_MOVEDOWN:
4749 Y(win) += (SWM_MOVE_STEPS - border_width);
4756 constrain_window(win, r, 0);
4758 store_float_geom(win, r);
4762 cursor_font = xcb_generate_id(conn);
4763 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4765 cursor = xcb_generate_id(conn);
4766 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4767 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4769 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4770 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4771 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4773 /* get cursor offset from window root */
4774 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4777 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4778 xcb_free_cursor(conn, cursor);
4779 xcb_close_font(conn, cursor_font);
4785 while ((evt = xcb_wait_for_event(conn)) && moving) {
4786 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4787 case XCB_BUTTON_RELEASE:
4788 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4791 case XCB_MOTION_NOTIFY:
4792 mne = (xcb_motion_notify_event_t *)evt;
4793 X(win) = mne->root_x - qpr->win_x - border_width;
4794 Y(win) = mne->root_y - qpr->win_y - border_width;
4796 constrain_window(win, r, 0);
4798 /* not free, don't sync more than 120 times / second */
4799 if ((mne->time - timestamp) > (1000 / 120) ) {
4800 timestamp = mne->time;
4815 store_float_geom(win, r);
4817 xcb_free_cursor(conn, cursor);
4818 xcb_close_font(conn, cursor_font);
4819 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4820 DNPRINTF(SWM_D_EVENT, "move: done\n");
4824 move_step(struct swm_region *r, union arg *args)
4826 struct ws_win *win = NULL;
4828 if (r && r->ws && r->ws->focus)
4833 if (!(win->transient != 0 || win->floating != 0))
4840 /* user/key callable function IDs */
4890 KF_SEARCH_WORKSPACE,
4931 KF_DUMPWINS, /* MUST BE LAST */
4935 /* key definitions */
4937 char name[SWM_FUNCNAME_LEN];
4938 void (*func)(struct swm_region *r, union arg *);
4940 } keyfuncs[KF_INVALID + 1] = {
4941 /* name function argument */
4942 { "bar_toggle", bar_toggle, {0} },
4943 { "button2", pressbutton, {2} },
4944 { "cycle_layout", cycle_layout, {0} },
4945 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4946 { "float_toggle", floating_toggle,{0} },
4947 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4948 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4949 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4950 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4951 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4952 { "iconify", iconify, {0} },
4953 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4954 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4955 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4956 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4957 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4958 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4959 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4960 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4961 { "mvws_1", send_to_ws, {.id = 0} },
4962 { "mvws_2", send_to_ws, {.id = 1} },
4963 { "mvws_3", send_to_ws, {.id = 2} },
4964 { "mvws_4", send_to_ws, {.id = 3} },
4965 { "mvws_5", send_to_ws, {.id = 4} },
4966 { "mvws_6", send_to_ws, {.id = 5} },
4967 { "mvws_7", send_to_ws, {.id = 6} },
4968 { "mvws_8", send_to_ws, {.id = 7} },
4969 { "mvws_9", send_to_ws, {.id = 8} },
4970 { "mvws_10", send_to_ws, {.id = 9} },
4971 { "mvws_11", send_to_ws, {.id = 10} },
4972 { "mvws_12", send_to_ws, {.id = 11} },
4973 { "mvws_13", send_to_ws, {.id = 12} },
4974 { "mvws_14", send_to_ws, {.id = 13} },
4975 { "mvws_15", send_to_ws, {.id = 14} },
4976 { "mvws_16", send_to_ws, {.id = 15} },
4977 { "mvws_17", send_to_ws, {.id = 16} },
4978 { "mvws_18", send_to_ws, {.id = 17} },
4979 { "mvws_19", send_to_ws, {.id = 18} },
4980 { "mvws_20", send_to_ws, {.id = 19} },
4981 { "mvws_21", send_to_ws, {.id = 20} },
4982 { "mvws_22", send_to_ws, {.id = 21} },
4983 { "name_workspace", name_workspace, {0} },
4984 { "quit", quit, {0} },
4985 { "raise_toggle", raise_toggle, {0} },
4986 { "restart", restart, {0} },
4987 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4988 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4989 { "search_win", search_win, {0} },
4990 { "search_workspace", search_workspace, {0} },
4991 { "spawn_custom", NULL, {0} },
4992 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4993 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4994 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4995 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4996 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4997 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4998 { "uniconify", uniconify, {0} },
4999 { "version", version, {0} },
5000 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5001 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5002 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5003 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5004 { "ws_1", switchws, {.id = 0} },
5005 { "ws_2", switchws, {.id = 1} },
5006 { "ws_3", switchws, {.id = 2} },
5007 { "ws_4", switchws, {.id = 3} },
5008 { "ws_5", switchws, {.id = 4} },
5009 { "ws_6", switchws, {.id = 5} },
5010 { "ws_7", switchws, {.id = 6} },
5011 { "ws_8", switchws, {.id = 7} },
5012 { "ws_9", switchws, {.id = 8} },
5013 { "ws_10", switchws, {.id = 9} },
5014 { "ws_11", switchws, {.id = 10} },
5015 { "ws_12", switchws, {.id = 11} },
5016 { "ws_13", switchws, {.id = 12} },
5017 { "ws_14", switchws, {.id = 13} },
5018 { "ws_15", switchws, {.id = 14} },
5019 { "ws_16", switchws, {.id = 15} },
5020 { "ws_17", switchws, {.id = 16} },
5021 { "ws_18", switchws, {.id = 17} },
5022 { "ws_19", switchws, {.id = 18} },
5023 { "ws_20", switchws, {.id = 19} },
5024 { "ws_21", switchws, {.id = 20} },
5025 { "ws_22", switchws, {.id = 21} },
5026 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5027 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5028 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5029 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5030 { "ws_prior", priorws, {0} },
5031 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5032 { "invalid key func", NULL, {0} },
5035 RB_ENTRY(key) entry;
5038 enum keyfuncid funcid;
5041 RB_HEAD(key_tree, key);
5044 key_cmp(struct key *kp1, struct key *kp2)
5046 if (kp1->keysym < kp2->keysym)
5048 if (kp1->keysym > kp2->keysym)
5051 if (kp1->mod < kp2->mod)
5053 if (kp1->mod > kp2->mod)
5059 RB_GENERATE(key_tree, key, entry, key_cmp);
5060 struct key_tree keys;
5063 enum { client_click, root_click };
5065 unsigned int action;
5067 unsigned int button;
5068 void (*func)(struct ws_win *, union arg *);
5071 /* action key mouse button func args */
5072 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5073 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
5074 { client_click, MODKEY, Button1, move, {0} },
5078 update_modkey(unsigned int mod)
5084 RB_FOREACH(kp, key_tree, &keys)
5085 if (kp->mod & ShiftMask)
5086 kp->mod = mod | ShiftMask;
5090 for (i = 0; i < LENGTH(buttons); i++)
5091 if (buttons[i].mask & ShiftMask)
5092 buttons[i].mask = mod | ShiftMask;
5094 buttons[i].mask = mod;
5099 TAILQ_ENTRY(spawn_prog) entry;
5104 TAILQ_HEAD(spawn_list, spawn_prog);
5105 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
5108 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
5111 struct spawn_prog *prog = NULL;
5113 char *ap, **real_args;
5115 /* suppress unused warning since var is needed */
5118 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5121 TAILQ_FOREACH(prog, &spawns, entry) {
5122 if (!strcasecmp(spawn_name, prog->name))
5126 warnx("spawn_custom: program %s not found", spawn_name);
5130 /* make room for expanded args */
5131 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5132 err(1, "spawn_custom: calloc real_args");
5134 /* expand spawn_args into real_args */
5135 for (i = 0; i < prog->argc; i++) {
5137 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5138 if (!strcasecmp(ap, "$bar_border")) {
5140 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5142 err(1, "spawn_custom border color");
5143 } else if (!strcasecmp(ap, "$bar_color")) {
5145 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5147 err(1, "spawn_custom bar color");
5148 } else if (!strcasecmp(ap, "$bar_font")) {
5149 if ((real_args[i] = strdup(bar_fonts))
5151 err(1, "spawn_custom bar fonts");
5152 } else if (!strcasecmp(ap, "$bar_font_color")) {
5154 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5156 err(1, "spawn_custom color font");
5157 } else if (!strcasecmp(ap, "$color_focus")) {
5159 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5161 err(1, "spawn_custom color focus");
5162 } else if (!strcasecmp(ap, "$color_unfocus")) {
5164 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5166 err(1, "spawn_custom color unfocus");
5168 /* no match --> copy as is */
5169 if ((real_args[i] = strdup(ap)) == NULL)
5170 err(1, "spawn_custom strdup(ap)");
5172 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5177 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5178 for (i = 0; i < prog->argc; i++)
5179 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5180 DNPRINTF(SWM_D_SPAWN, "\n");
5182 *ret_args = real_args;
5183 return (prog->argc);
5187 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5193 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5197 spawn(r->ws->idx, &a, 1);
5199 for (i = 0; i < spawn_argc; i++)
5205 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5211 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5215 if (pipe(select_list_pipe) == -1)
5216 err(1, "pipe error");
5217 if (pipe(select_resp_pipe) == -1)
5218 err(1, "pipe error");
5220 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5221 err(1, "could not disable SIGPIPE");
5222 switch (*pid = fork()) {
5224 err(1, "cannot fork");
5227 if (dup2(select_list_pipe[0], 0) == -1)
5229 if (dup2(select_resp_pipe[1], 1) == -1)
5231 close(select_list_pipe[1]);
5232 close(select_resp_pipe[0]);
5233 spawn(r->ws->idx, &a, 0);
5235 default: /* parent */
5236 close(select_list_pipe[0]);
5237 close(select_resp_pipe[1]);
5241 for (i = 0; i < spawn_argc; i++)
5247 spawn_insert(char *name, char *args)
5249 char *arg, *cp, *ptr;
5250 struct spawn_prog *sp;
5252 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5254 if ((sp = calloc(1, sizeof *sp)) == NULL)
5255 err(1, "spawn_insert: calloc");
5256 if ((sp->name = strdup(name)) == NULL)
5257 err(1, "spawn_insert: strdup");
5259 /* convert the arguments to an argument list */
5260 if ((ptr = cp = strdup(args)) == NULL)
5261 err(1, "spawn_insert: strdup");
5262 while ((arg = strsep(&ptr, " \t")) != NULL) {
5263 /* empty field; skip it */
5268 if ((sp->argv = realloc(sp->argv, sp->argc *
5269 sizeof *sp->argv)) == NULL)
5270 err(1, "spawn_insert: realloc");
5271 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5272 err(1, "spawn_insert: strdup");
5276 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5277 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5281 spawn_remove(struct spawn_prog *sp)
5285 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5287 TAILQ_REMOVE(&spawns, sp, entry);
5288 for (i = 0; i < sp->argc; i++)
5294 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5298 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5300 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5303 spawn_insert(name, args);
5305 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5309 setspawn(char *name, char *args)
5311 struct spawn_prog *sp;
5313 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5318 TAILQ_FOREACH(sp, &spawns, entry) {
5319 if (!strcmp(sp->name, name)) {
5323 spawn_replace(sp, name, args);
5324 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5328 if (*args == '\0') {
5329 warnx("error: setspawn: cannot find program: %s", name);
5333 spawn_insert(name, args);
5334 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5338 setconfspawn(char *selector, char *value, int flags)
5342 /* suppress unused warning since var is needed */
5345 args = expand_tilde(value);
5347 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5349 setspawn(selector, args);
5352 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5359 setconfspawn("term", "xterm", 0);
5360 setconfspawn("spawn_term", "xterm", 0);
5361 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5362 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5363 setconfspawn("lock", "xlock", 0);
5364 setconfspawn("initscr", "initscreen.sh", 0);
5365 setconfspawn("menu", "dmenu_run"
5368 " -nf $bar_font_color"
5370 " -sf $bar_color", 0);
5371 setconfspawn("search", "dmenu"
5375 " -nf $bar_font_color"
5377 " -sf $bar_color", 0);
5378 setconfspawn("name_workspace", "dmenu"
5382 " -nf $bar_font_color"
5384 " -sf $bar_color", 0);
5388 #define SWM_MODNAME_SIZE 32
5389 #define SWM_KEY_WS "\n+ \t"
5391 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5395 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5396 if (mod == NULL || ks == NULL) {
5397 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5400 if (keystr == NULL || strlen(keystr) == 0) {
5401 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5405 *ks = XCB_NO_SYMBOL;
5407 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5408 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5410 cp += (long)strspn(cp, SWM_KEY_WS);
5411 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5413 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5415 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5417 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5419 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5421 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5423 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5424 *mod |= ControlMask;
5426 *ks = XStringToKeysym(name);
5427 XConvertCase(*ks, ks, &uks);
5428 if (ks == XCB_NO_SYMBOL) {
5430 "parsekeys: invalid key %s\n",
5436 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5441 strdupsafe(char *str)
5446 return (strdup(str));
5450 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5454 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5455 keyfuncs[kfid].name, spawn_name);
5457 if ((kp = malloc(sizeof *kp)) == NULL)
5458 err(1, "key_insert: malloc");
5463 kp->spawn_name = strdupsafe(spawn_name);
5464 RB_INSERT(key_tree, &keys, kp);
5466 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5470 key_lookup(unsigned int mod, KeySym ks)
5477 return (RB_FIND(key_tree, &keys, &kp));
5481 key_remove(struct key *kp)
5483 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5485 RB_REMOVE(key_tree, &keys, kp);
5486 free(kp->spawn_name);
5489 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5493 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5496 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5500 key_insert(mod, ks, kfid, spawn_name);
5502 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5506 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5511 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5512 keyfuncs[kfid].name, spawn_name);
5514 if ((kp = key_lookup(mod, ks)) != NULL) {
5515 if (kfid == KF_INVALID)
5518 key_replace(kp, mod, ks, kfid, spawn_name);
5519 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5522 if (kfid == KF_INVALID) {
5523 warnx("error: setkeybinding: cannot find mod/key combination");
5524 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5528 key_insert(mod, ks, kfid, spawn_name);
5529 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5533 setconfbinding(char *selector, char *value, int flags)
5535 enum keyfuncid kfid;
5538 struct spawn_prog *sp;
5540 /* suppress unused warning since var is needed */
5543 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5544 if (selector == NULL) {
5545 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5546 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5548 setkeybinding(mod, ks, kfid, NULL);
5553 /* search by key function name */
5554 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5555 if (strncasecmp(selector, keyfuncs[kfid].name,
5556 SWM_FUNCNAME_LEN) == 0) {
5557 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5559 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5560 setkeybinding(mod, ks, kfid, NULL);
5566 /* search by custom spawn name */
5567 TAILQ_FOREACH(sp, &spawns, entry) {
5568 if (strcasecmp(selector, sp->name) == 0) {
5569 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5571 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5572 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5579 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5586 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5587 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5588 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5589 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5590 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5591 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5592 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5593 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5594 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5595 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5596 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5597 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5598 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5599 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5600 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5601 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5602 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5603 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5604 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5605 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5606 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5607 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5608 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5609 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5610 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5611 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5612 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5613 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5614 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5615 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5616 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5617 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5618 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5619 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5620 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5621 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5622 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5623 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5624 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5625 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5626 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5627 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5628 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5629 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5630 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5631 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5632 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5633 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5634 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5635 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5636 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5637 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5638 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5639 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5640 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5641 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5642 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5643 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5644 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5645 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5646 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5647 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5648 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5649 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5650 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5651 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5652 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5653 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5654 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5655 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5656 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5657 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5658 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5659 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5660 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5661 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5662 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5663 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5664 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5665 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5666 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5667 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5668 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5669 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5670 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5671 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5672 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5673 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5674 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5675 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5676 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5677 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5678 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5679 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5680 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5681 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5683 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5692 while (RB_EMPTY(&keys) == 0) {
5693 kp = RB_ROOT(&keys);
5699 setkeymapping(char *selector, char *value, int flags)
5701 char keymapping_file[PATH_MAX];
5703 /* suppress unused warnings since vars are needed */
5707 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5708 if (value[0] == '~')
5709 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5710 pwd->pw_dir, &value[1]);
5712 strlcpy(keymapping_file, value, sizeof keymapping_file);
5714 /* load new key bindings; if it fails, revert to default bindings */
5715 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5719 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5724 updatenumlockmask(void)
5727 xcb_get_modifier_mapping_reply_t *modmap_r;
5728 xcb_keycode_t *modmap, kc, *keycode;
5730 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5733 modmap_r = xcb_get_modifier_mapping_reply(conn,
5734 xcb_get_modifier_mapping(conn),
5737 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5738 for (i = 0; i < 8; i++) {
5739 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5740 kc = modmap[i * modmap_r->keycodes_per_modifier
5742 keycode = xcb_key_symbols_get_keycode(syms,
5745 numlockmask = (1 << i);
5758 xcb_keycode_t *code;
5759 unsigned int modifiers[] =
5760 { 0, LockMask, numlockmask, numlockmask | LockMask };
5763 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5764 updatenumlockmask();
5766 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5767 for (k = 0; k < num_screens; k++) {
5768 if (TAILQ_EMPTY(&screens[k].rl))
5770 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5772 RB_FOREACH(kp, key_tree, &keys) {
5773 if ((code = xcb_key_symbols_get_keycode(syms,
5775 for (j = 0; j < LENGTH(modifiers); j++)
5776 xcb_grab_key(conn, 1,
5778 kp->mod | modifiers[j],
5779 *code, XCB_GRAB_MODE_ASYNC,
5780 XCB_GRAB_MODE_ASYNC);
5787 grabbuttons(struct ws_win *win, int focused)
5790 unsigned int modifiers[] =
5791 { 0, LockMask, numlockmask, numlockmask|LockMask };
5793 updatenumlockmask();
5794 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5795 XCB_BUTTON_MASK_ANY);
5797 for (i = 0; i < LENGTH(buttons); i++)
5798 if (buttons[i].action == client_click)
5799 for (j = 0; j < LENGTH(modifiers); j++)
5800 xcb_grab_button(conn, 0, win->id,
5802 XCB_GRAB_MODE_ASYNC,
5809 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5810 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5811 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5814 const char *quirkname[] = {
5815 "NONE", /* config string for "no value" */
5824 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5825 #define SWM_Q_WS "\n|+ \t"
5827 parsequirks(char *qstr, unsigned long *quirk)
5837 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5839 cp += (long)strspn(cp, SWM_Q_WS);
5840 for (i = 0; i < LENGTH(quirkname); i++) {
5841 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5842 DNPRINTF(SWM_D_QUIRK,
5843 "parsequirks: %s\n", name);
5848 *quirk |= 1 << (i-1);
5852 if (i >= LENGTH(quirkname)) {
5853 DNPRINTF(SWM_D_QUIRK,
5854 "parsequirks: invalid quirk [%s]\n", name);
5862 quirk_insert(const char *class, const char *name, unsigned long quirk)
5866 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5869 if ((qp = malloc(sizeof *qp)) == NULL)
5870 err(1, "quirk_insert: malloc");
5871 if ((qp->class = strdup(class)) == NULL)
5872 err(1, "quirk_insert: strdup");
5873 if ((qp->name = strdup(name)) == NULL)
5874 err(1, "quirk_insert: strdup");
5877 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5879 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5883 quirk_remove(struct quirk *qp)
5885 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5886 qp->name, qp->quirk);
5888 TAILQ_REMOVE(&quirks, qp, entry);
5893 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5897 quirk_replace(struct quirk *qp, const char *class, const char *name,
5898 unsigned long quirk)
5900 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5901 qp->name, qp->quirk);
5904 quirk_insert(class, name, quirk);
5906 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5910 setquirk(const char *class, const char *name, unsigned long quirk)
5914 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5917 TAILQ_FOREACH(qp, &quirks, entry) {
5918 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5922 quirk_replace(qp, class, name, quirk);
5923 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5928 warnx("error: setquirk: cannot find class/name combination");
5932 quirk_insert(class, name, quirk);
5933 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5937 setconfquirk(char *selector, char *value, int flags)
5939 char *cp, *class, *name;
5943 /* suppress unused warning since var is needed */
5946 if (selector == NULL)
5948 if ((cp = strchr(selector, ':')) == NULL)
5953 if ((retval = parsequirks(value, &qrks)) == 0)
5954 setquirk(class, name, qrks);
5961 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5962 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5963 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5964 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5965 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5966 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5967 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5968 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5969 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5970 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5971 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5972 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5973 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5976 /* conf file stuff */
5977 #define SWM_CONF_FILE "spectrwm.conf"
5978 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5982 SWM_S_BAR_AT_BOTTOM,
5983 SWM_S_BAR_BORDER_WIDTH,
5990 SWM_S_CLOCK_ENABLED,
5993 SWM_S_CYCLE_VISIBLE,
5995 SWM_S_DISABLE_BORDER,
5997 SWM_S_FOCUS_CLOSE_WRAP,
5998 SWM_S_FOCUS_DEFAULT,
6004 SWM_S_STACK_ENABLED,
6006 SWM_S_TITLE_CLASS_ENABLED,
6007 SWM_S_TITLE_NAME_ENABLED,
6008 SWM_S_URGENT_ENABLED,
6009 SWM_S_VERBOSE_LAYOUT,
6010 SWM_S_WINDOW_NAME_ENABLED,
6011 SWM_S_WORKSPACE_LIMIT
6015 setconfvalue(char *selector, char *value, int flags)
6020 /* suppress unused warning since var is needed */
6024 case SWM_S_BAR_ACTION:
6026 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6027 err(1, "setconfvalue: bar_action");
6029 case SWM_S_BAR_AT_BOTTOM:
6030 bar_at_bottom = atoi(value);
6032 case SWM_S_BAR_BORDER_WIDTH:
6033 bar_border_width = atoi(value);
6034 if (bar_border_width < 0)
6035 bar_border_width = 0;
6037 case SWM_S_BAR_DELAY:
6038 bar_delay = atoi(value);
6040 case SWM_S_BAR_ENABLED:
6041 bar_enabled = atoi(value);
6043 case SWM_S_BAR_FONT:
6045 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6046 err(1, "setconfvalue: asprintf: failed to allocate "
6047 "memory for bar_fonts.");
6050 /* If already in xft mode, then we are done. */
6051 if (!bar_font_legacy)
6054 /* If there are any non-XLFD entries, switch to Xft mode. */
6055 while ((b = strsep(&value, ",")) != NULL) {
6059 bar_font_legacy = 0;
6064 case SWM_S_BAR_FORMAT:
6066 if ((bar_format = strdup(value)) == NULL)
6067 err(1, "setconfvalue: bar_format");
6069 case SWM_S_BAR_JUSTIFY:
6070 if (!strcmp(value, "left"))
6071 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6072 else if (!strcmp(value, "center"))
6073 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6074 else if (!strcmp(value, "right"))
6075 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6077 errx(1, "invalid bar_justify");
6079 case SWM_S_BORDER_WIDTH:
6080 border_width = atoi(value);
6081 if (border_width < 0)
6084 case SWM_S_CLOCK_ENABLED:
6085 clock_enabled = atoi(value);
6087 case SWM_S_CLOCK_FORMAT:
6088 #ifndef SWM_DENY_CLOCK_FORMAT
6090 if ((clock_format = strdup(value)) == NULL)
6091 err(1, "setconfvalue: clock_format");
6094 case SWM_S_CYCLE_EMPTY:
6095 cycle_empty = atoi(value);
6097 case SWM_S_CYCLE_VISIBLE:
6098 cycle_visible = atoi(value);
6100 case SWM_S_DIALOG_RATIO:
6101 dialog_ratio = atof(value);
6102 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6105 case SWM_S_DISABLE_BORDER:
6106 disable_border = atoi(value);
6108 case SWM_S_FOCUS_CLOSE:
6109 if (!strcmp(value, "first"))
6110 focus_close = SWM_STACK_BOTTOM;
6111 else if (!strcmp(value, "last"))
6112 focus_close = SWM_STACK_TOP;
6113 else if (!strcmp(value, "next"))
6114 focus_close = SWM_STACK_ABOVE;
6115 else if (!strcmp(value, "previous"))
6116 focus_close = SWM_STACK_BELOW;
6118 errx(1, "focus_close");
6120 case SWM_S_FOCUS_CLOSE_WRAP:
6121 focus_close_wrap = atoi(value);
6123 case SWM_S_FOCUS_DEFAULT:
6124 if (!strcmp(value, "last"))
6125 focus_default = SWM_STACK_TOP;
6126 else if (!strcmp(value, "first"))
6127 focus_default = SWM_STACK_BOTTOM;
6129 errx(1, "focus_default");
6131 case SWM_S_FOCUS_MODE:
6132 if (!strcmp(value, "default"))
6133 focus_mode = SWM_FOCUS_DEFAULT;
6134 else if (!strcmp(value, "follow_cursor"))
6135 focus_mode = SWM_FOCUS_FOLLOW;
6136 else if (!strcmp(value, "synergy"))
6137 focus_mode = SWM_FOCUS_SYNERGY;
6139 errx(1, "focus_mode");
6141 case SWM_S_SPAWN_ORDER:
6142 if (!strcmp(value, "first"))
6143 spawn_position = SWM_STACK_BOTTOM;
6144 else if (!strcmp(value, "last"))
6145 spawn_position = SWM_STACK_TOP;
6146 else if (!strcmp(value, "next"))
6147 spawn_position = SWM_STACK_ABOVE;
6148 else if (!strcmp(value, "previous"))
6149 spawn_position = SWM_STACK_BELOW;
6151 errx(1, "spawn_position");
6153 case SWM_S_SPAWN_TERM:
6154 setconfspawn("term", value, 0);
6155 setconfspawn("spawn_term", value, 0);
6159 case SWM_S_SS_ENABLED:
6160 ss_enabled = atoi(value);
6162 case SWM_S_STACK_ENABLED:
6163 stack_enabled = atoi(value);
6165 case SWM_S_TERM_WIDTH:
6166 term_width = atoi(value);
6170 case SWM_S_TITLE_CLASS_ENABLED:
6171 title_class_enabled = atoi(value);
6173 case SWM_S_TITLE_NAME_ENABLED:
6174 title_name_enabled = atoi(value);
6176 case SWM_S_URGENT_ENABLED:
6177 urgent_enabled = atoi(value);
6179 case SWM_S_VERBOSE_LAYOUT:
6180 verbose_layout = atoi(value);
6181 for (i = 0; layouts[i].l_stack != NULL; i++) {
6183 layouts[i].l_string = fancy_stacker;
6185 layouts[i].l_string = plain_stacker;
6188 case SWM_S_WINDOW_NAME_ENABLED:
6189 window_name_enabled = atoi(value);
6191 case SWM_S_WORKSPACE_LIMIT:
6192 workspace_limit = atoi(value);
6193 if (workspace_limit > SWM_WS_MAX)
6194 workspace_limit = SWM_WS_MAX;
6195 else if (workspace_limit < 1)
6196 workspace_limit = 1;
6205 setconfmodkey(char *selector, char *value, int flags)
6207 /* suppress unused warnings since vars are needed */
6211 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6212 update_modkey(Mod1Mask);
6213 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6214 update_modkey(Mod2Mask);
6215 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6216 update_modkey(Mod3Mask);
6217 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6218 update_modkey(Mod4Mask);
6225 setconfcolor(char *selector, char *value, int flags)
6227 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6232 setconfregion(char *selector, char *value, int flags)
6234 /* suppress unused warnings since vars are needed */
6238 custom_region(value);
6243 setautorun(char *selector, char *value, int flags)
6253 /* suppress unused warnings since vars are needed */
6257 if (getenv("SWM_STARTED"))
6261 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6262 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6264 if (ws_id < 0 || ws_id >= workspace_limit)
6265 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6268 * This is a little intricate
6270 * If the pid already exists we simply reuse it because it means it was
6271 * used before AND not claimed by manage_window. We get away with
6272 * altering it in the parent after INSERT because this can not be a race
6275 while ((ap = strsep(&sp, " \t")) != NULL) {
6278 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6280 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6281 err(1, "setautorun: realloc");
6282 a.argv[argc - 1] = ap;
6285 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6286 err(1, "setautorun: realloc");
6287 a.argv[argc] = NULL;
6289 if ((pid = fork()) == 0) {
6290 spawn(ws_id, &a, 1);
6299 p = calloc(1, sizeof *p);
6302 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6312 setlayout(char *selector, char *value, int flags)
6314 int ws_id, i, x, mg, ma, si, ar, f = 0;
6315 int st = SWM_V_STACK, num_screens;
6317 struct workspace *ws;
6319 /* suppress unused warnings since vars are needed */
6323 if (getenv("SWM_STARTED"))
6327 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6328 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6329 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6330 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6333 if (ws_id < 0 || ws_id >= workspace_limit)
6334 errx(1, "layout: invalid workspace %d", ws_id + 1);
6336 if (!strcasecmp(s, "vertical"))
6338 else if (!strcasecmp(s, "vertical_flip")) {
6341 } else if (!strcasecmp(s, "horizontal"))
6343 else if (!strcasecmp(s, "horizontal_flip")) {
6346 } else if (!strcasecmp(s, "fullscreen"))
6349 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6350 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6353 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6354 for (i = 0; i < num_screens; i++) {
6355 ws = (struct workspace *)&screens[i].ws;
6356 ws[ws_id].cur_layout = &layouts[st];
6358 ws[ws_id].always_raise = ar;
6359 if (st == SWM_MAX_STACK)
6363 for (x = 0; x < abs(mg); x++) {
6364 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6365 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6366 SWM_ARG_ID_MASTERSHRINK);
6370 for (x = 0; x < abs(ma); x++) {
6371 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6372 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6373 SWM_ARG_ID_MASTERDEL);
6377 for (x = 0; x < abs(si); x++) {
6378 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6379 si >= 0 ? SWM_ARG_ID_STACKINC :
6380 SWM_ARG_ID_STACKDEC);
6385 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6386 SWM_ARG_ID_FLIPLAYOUT);
6394 /* config options */
6395 struct config_option {
6397 int (*func)(char*, char*, int);
6400 struct config_option configopt[] = {
6401 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6402 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6403 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6404 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6405 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6406 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6407 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6408 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6409 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6410 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6411 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6412 { "keyboard_mapping", setkeymapping, 0 },
6413 { "bind", setconfbinding, 0 },
6414 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6415 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6416 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6417 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6418 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6419 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6420 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6421 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6422 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6423 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6424 { "modkey", setconfmodkey, 0 },
6425 { "program", setconfspawn, 0 },
6426 { "quirk", setconfquirk, 0 },
6427 { "region", setconfregion, 0 },
6428 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6429 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6430 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6431 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6432 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6433 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6434 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6435 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6436 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6437 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6438 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6439 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6440 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6441 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6442 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6443 { "autorun", setautorun, 0 },
6444 { "layout", setlayout, 0 },
6449 conf_load(char *filename, int keymapping)
6452 char *line, *cp, *optsub, *optval;
6453 size_t linelen, lineno = 0;
6454 int wordlen, i, optidx;
6455 struct config_option *opt;
6457 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6459 if (filename == NULL) {
6460 warnx("conf_load: no filename");
6463 if ((config = fopen(filename, "r")) == NULL) {
6464 warn("conf_load: fopen: %s", filename);
6468 while (!feof(config)) {
6469 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6472 err(1, "%s", filename);
6477 cp += strspn(cp, " \t\n"); /* eat whitespace */
6478 if (cp[0] == '\0') {
6483 /* get config option */
6484 wordlen = strcspn(cp, "=[ \t\n");
6486 warnx("%s: line %zd: no option found",
6491 for (i = 0; i < LENGTH(configopt); i++) {
6492 opt = &configopt[i];
6493 if (!strncasecmp(cp, opt->optname, wordlen) &&
6494 (int)strlen(opt->optname) == wordlen) {
6500 warnx("%s: line %zd: unknown option %.*s",
6501 filename, lineno, wordlen, cp);
6504 if (keymapping && strcmp(opt->optname, "bind")) {
6505 warnx("%s: line %zd: invalid option %.*s",
6506 filename, lineno, wordlen, cp);
6510 cp += strspn(cp, " \t\n"); /* eat whitespace */
6511 /* get [selector] if any */
6515 wordlen = strcspn(cp, "]");
6518 warnx("%s: line %zd: syntax error",
6523 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6525 warnx("%s: line %zd: unable to allocate"
6526 "memory for selector", filename,
6532 cp += strspn(cp, "] \t\n"); /* eat trailing */
6534 cp += strspn(cp, "= \t\n"); /* eat trailing */
6536 optval = strdup(cp);
6537 /* call function to deal with it all */
6538 if (configopt[optidx].func(optsub, optval,
6539 configopt[optidx].funcflags) != 0)
6540 errx(1, "%s: line %zd: invalid data for %s",
6541 filename, lineno, configopt[optidx].optname);
6548 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6555 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6561 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6563 struct ws_win *parent, *w;
6564 struct swm_region *r;
6565 struct workspace *ws;
6566 xcb_icccm_wm_hints_t wmh;
6568 parent = find_window(win->transient);
6570 parent->child_trans = win;
6572 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6573 " for 0x%x trans 0x%x\n", win->id, win->transient);
6575 r = root_to_region(win->wa->root);
6577 /* parent doen't exist in our window list */
6578 TAILQ_FOREACH(w, &ws->winlist, entry) {
6579 if (xcb_icccm_get_wm_hints_reply(conn,
6580 xcb_icccm_get_wm_hints(conn, w->id),
6582 warnx("can't get hints for 0x%x", w->id);
6586 if (win->hints.window_group != wmh.window_group)
6589 w->child_trans = win;
6590 win->transient = w->id;
6592 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6593 "transient to 0x%x\n", win->transient);
6600 window_get_pid(xcb_window_t win)
6605 xcb_get_property_cookie_t pc;
6606 xcb_get_property_reply_t *pr;
6608 apid = get_atom_from_string("_NET_WM_PID");
6609 if (apid == XCB_ATOM_NONE)
6612 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6613 pr = xcb_get_property_reply(conn, pc, NULL);
6616 if (pr->type != XCB_ATOM_CARDINAL) {
6621 if (pr->type == apid && pr->format == 32)
6622 ret = *((pid_t *)xcb_get_property_value(pr));
6628 apid = get_atom_from_string("_SWM_PID");
6629 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6631 pr = xcb_get_property_reply(conn, pc, NULL);
6634 if (pr->type != apid) {
6639 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6646 get_ws_idx(xcb_window_t id)
6652 xcb_get_property_reply_t *gpr;
6654 gpr = xcb_get_property_reply(conn,
6655 xcb_get_property(conn, 0, id, a_swm_ws,
6656 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6661 proplen = xcb_get_property_value_length(gpr);
6663 prop = malloc(proplen + 1);
6666 xcb_get_property_value(gpr),
6668 prop[proplen] = '\0';
6675 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6676 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6678 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6688 manage_window(xcb_window_t id)
6690 xcb_window_t trans = XCB_WINDOW_NONE;
6691 struct workspace *ws;
6692 struct ws_win *win, *ww;
6693 int ws_idx, border_me = 0;
6694 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6695 struct swm_region *r;
6698 uint32_t event_mask, i;
6699 xcb_icccm_get_wm_protocols_reply_t wpr;
6701 if ((win = find_window(id)) != NULL)
6702 return (win); /* already being managed */
6704 /* see if we are on the unmanaged list */
6705 if ((win = find_unmanaged_window(id)) != NULL) {
6706 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6707 "window: 0x%x\n", win->id);
6708 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6710 set_child_transient(win, &trans);
6712 if (trans && (ww = find_window(trans)))
6713 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6714 else if ((ww = win->ws->focus) &&
6715 spawn_position == SWM_STACK_ABOVE)
6716 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6718 else if (ww && spawn_position == SWM_STACK_BELOW)
6719 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6720 else switch (spawn_position) {
6723 case SWM_STACK_ABOVE:
6724 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6726 case SWM_STACK_BOTTOM:
6727 case SWM_STACK_BELOW:
6728 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6731 ewmh_update_actions(win);
6735 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6736 err(1, "manage_window: calloc: failed to allocate memory for "
6742 /* see if we need to override the workspace */
6743 p = find_pid(window_get_pid(id));
6745 /* Get all the window data in one shot */
6746 ws_idx = get_ws_idx(id);
6748 win->wa = xcb_get_geometry_reply(conn,
6749 xcb_get_geometry(conn, id),
6751 xcb_icccm_get_wm_normal_hints_reply(conn,
6752 xcb_icccm_get_wm_normal_hints(conn, id),
6754 xcb_icccm_get_wm_hints_reply(conn,
6755 xcb_icccm_get_wm_hints(conn, id),
6757 xcb_icccm_get_wm_transient_for_reply(conn,
6758 xcb_icccm_get_wm_transient_for(conn, id),
6761 win->transient = trans;
6762 set_child_transient(win, &trans);
6763 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6764 "transient: 0x%x\n", win->id, win->transient);
6767 /* get supported protocols */
6768 if (xcb_icccm_get_wm_protocols_reply(conn,
6769 xcb_icccm_get_wm_protocols(conn, id, a_prot),
6771 for (i = 0; i < wpr.atoms_len; i++) {
6772 if (wpr.atoms[i] == a_takefocus)
6773 win->take_focus = 1;
6774 if (wpr.atoms[i] == a_delete)
6775 win->can_delete = 1;
6777 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6780 win->iconic = get_iconic(win);
6783 * Figure out where to put the window. If it was previously assigned to
6784 * a workspace (either by spawn() or manually moving), and isn't
6785 * transient, * put it in the same workspace
6787 r = root_to_region(win->wa->root);
6789 ws = &r->s->ws[p->ws];
6790 TAILQ_REMOVE(&pidlist, p, entry);
6793 } else if (ws_idx != -1 && win->transient == 0) {
6794 ws = &r->s->ws[ws_idx];
6797 /* this should launch transients in the same ws as parent */
6799 if ((ww = find_window(trans)) != NULL)
6805 warnx("manage_window: fix this "
6811 /* set up the window layout */
6814 win->s = r->s; /* this never changes */
6815 if (trans && (ww = find_window(trans)))
6816 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6817 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6818 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6820 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6821 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6822 else switch (spawn_position) {
6825 case SWM_STACK_ABOVE:
6826 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6828 case SWM_STACK_BOTTOM:
6829 case SWM_STACK_BELOW:
6830 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6833 /* ignore window border if there is one. */
6834 WIDTH(win) = win->wa->width;
6835 HEIGHT(win) = win->wa->height;
6836 X(win) = win->wa->x + win->wa->border_width;
6837 Y(win) = win->wa->y + win->wa->border_width;
6839 win->g_floatvalid = 0;
6840 win->floatmaxed = 0;
6841 win->ewmh_flags = 0;
6843 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6844 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6845 HEIGHT(win), ws->idx);
6847 constrain_window(win, r, 0);
6849 /* Set window properties so we can remember this after reincarnation */
6850 if (prop == NULL && snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6852 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6854 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6855 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6861 ewmh_autoquirk(win);
6863 if (xcb_icccm_get_wm_class_reply(conn,
6864 xcb_icccm_get_wm_class(conn, win->id),
6866 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6867 win->ch.class_name, win->ch.instance_name);
6869 /* java is retarded so treat it special */
6870 if (strstr(win->ch.instance_name, "sun-awt")) {
6875 TAILQ_FOREACH(qp, &quirks, entry) {
6876 if (!strcmp(win->ch.class_name, qp->class) &&
6877 !strcmp(win->ch.instance_name, qp->name)) {
6878 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6879 "class: %s, name: %s\n", win->ch.class_name,
6880 win->ch.instance_name);
6881 if (qp->quirk & SWM_Q_FLOAT) {
6885 win->quirks = qp->quirk;
6890 /* alter window position if quirky */
6891 if (win->quirks & SWM_Q_ANYWHERE) {
6892 win->manual = 1; /* don't center the quirky windows */
6893 if (bar_enabled && Y(win) < bar_height)
6894 Y(win) = bar_height;
6895 if (WIDTH(win) + X(win) > WIDTH(r))
6896 X(win) = WIDTH(r) - WIDTH(win) - 2;
6900 /* Reset font sizes (the bruteforce way; no default keybinding). */
6901 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6902 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6903 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6904 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6905 fake_keypress(win, XK_KP_Add, ShiftMask);
6908 ewmh_get_win_state(win);
6909 ewmh_update_actions(win);
6910 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6915 X(win) -= border_width;
6916 Y(win) -= border_width;
6920 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6921 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6923 event_mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
6926 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6928 /* floaters need to be mapped if they are in the current workspace */
6929 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6930 map_window_raised(win->id);
6936 free_window(struct ws_win *win)
6938 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6943 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6948 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6953 memset(win, 0xff, sizeof *win); /* XXX kill later */
6956 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6960 unmanage_window(struct ws_win *win)
6962 struct ws_win *parent;
6963 xcb_screen_t *screen;
6968 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6970 if (win->transient) {
6971 parent = find_window(win->transient);
6973 parent->child_trans = NULL;
6976 /* focus on root just in case */
6977 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6978 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6979 screen->root, XCB_CURRENT_TIME);
6983 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6984 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6988 focus_magic(struct ws_win *win)
6990 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6993 /* if there are no windows clear the status-bar */
6998 if (win->child_trans) {
6999 /* win = parent & has a transient so focus on that */
7001 focus_win(win->child_trans);
7002 if (win->child_trans->take_focus)
7003 client_msg(win, a_takefocus);
7005 /* make sure transient hasn't disappeared */
7006 if (validate_win(win->child_trans) == 0) {
7007 focus_win(win->child_trans);
7008 if (win->child_trans->take_focus)
7009 client_msg(win->child_trans,
7012 win->child_trans = NULL;
7014 if (win->take_focus)
7015 client_msg(win, a_takefocus);
7021 if (win->take_focus)
7022 client_msg(win, a_takefocus);
7027 expose(xcb_expose_event_t *e)
7030 struct swm_region *r;
7032 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7034 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7035 for (i = 0; i < num_screens; i++)
7036 TAILQ_FOREACH(r, &screens[i].rl, entry)
7037 if (e->window == WINID(r->bar))
7045 focusin(xcb_focus_in_event_t *e)
7047 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7048 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7049 e->mode, get_notify_detail_label(e->detail), e->detail);
7054 keypress(xcb_key_press_event_t *e)
7056 xcb_keysym_t keysym;
7059 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7061 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7063 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7066 if (kp->funcid == KF_SPAWN_CUSTOM)
7067 spawn_custom(root_to_region(e->root),
7068 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7069 else if (keyfuncs[kp->funcid].func)
7070 keyfuncs[kp->funcid].func(root_to_region(e->root),
7071 &(keyfuncs[kp->funcid].args));
7075 buttonpress(xcb_button_press_event_t *e)
7079 unsigned int action;
7081 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
7082 e->event, e->detail);
7084 if ((win = find_window(e->event)) == NULL)
7088 action = client_click;
7090 for (i = 0; i < LENGTH(buttons); i++)
7091 if (action == buttons[i].action && buttons[i].func &&
7092 buttons[i].button == e->detail &&
7093 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
7094 buttons[i].func(win, &buttons[i].args);
7101 print_win_geom(xcb_window_t w)
7103 xcb_get_geometry_reply_t *wa;
7105 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7108 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7113 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7114 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7115 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7123 configurerequest(xcb_configure_request_event_t *e)
7128 uint32_t wc[7] = {0};
7130 if ((win = find_window(e->window)) == NULL)
7131 if ((win = find_unmanaged_window(e->window)) == NULL)
7134 print_win_geom(e->window);
7137 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
7138 "value_mask: 0x%x", e->window, e->value_mask);
7139 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7140 mask |= XCB_CONFIG_WINDOW_X;
7142 DPRINTF(", X: %d", e->x);
7144 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7145 mask |= XCB_CONFIG_WINDOW_Y;
7147 DPRINTF(", Y: %d", e->y);
7149 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7150 mask |= XCB_CONFIG_WINDOW_WIDTH;
7152 DPRINTF(", W: %u", e->width);
7154 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7155 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7156 wc[i++] = e->height;
7157 DPRINTF(", H: %u", e->height);
7159 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7160 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7161 wc[i++] = e->border_width;
7162 DPRINTF(", Border: %u", e->border_width);
7164 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7165 mask |= XCB_CONFIG_WINDOW_SIBLING;
7166 wc[i++] = e->sibling;
7167 DPRINTF(", Sibling: 0x%x", e->sibling);
7169 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7170 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7171 wc[i++] = e->stack_mode;
7172 DPRINTF(", StackMode: %u", e->stack_mode);
7176 xcb_configure_window(conn, e->window, mask, wc);
7178 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
7179 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7180 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7181 win->g_float.x = e->x - X(win->ws->r);
7182 win->g_float.y = e->y - Y(win->ws->r);
7183 win->g_float.w = e->width;
7184 win->g_float.h = e->height;
7185 win->g_floatvalid = 1;
7187 if (win->floating) {
7188 win->g = win->g_float;
7189 win->g.x += X(win->ws->r);
7190 win->g.y += Y(win->ws->r);
7203 configurenotify(xcb_configure_notify_event_t *e)
7207 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
7210 win = find_window(e->window);
7212 xcb_icccm_get_wm_normal_hints_reply(conn,
7213 xcb_icccm_get_wm_normal_hints(conn, win->id),
7222 destroynotify(xcb_destroy_notify_event_t *e)
7226 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7228 if ((win = find_window(e->window)) == NULL) {
7229 if ((win = find_unmanaged_window(e->window)) == NULL)
7235 /* make sure we focus on something */
7238 unmanage_window(win);
7245 get_notify_detail_label(uint8_t detail)
7250 case XCB_NOTIFY_DETAIL_ANCESTOR:
7253 case XCB_NOTIFY_DETAIL_VIRTUAL:
7256 case XCB_NOTIFY_DETAIL_INFERIOR:
7259 case XCB_NOTIFY_DETAIL_NONLINEAR:
7260 label = "Nonlinear";
7262 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7263 label = "NonlinearVirtual";
7265 case XCB_NOTIFY_DETAIL_POINTER:
7268 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7269 label = "PointerRoot";
7271 case XCB_NOTIFY_DETAIL_NONE:
7282 get_notify_mode_label(uint8_t mode)
7287 case XCB_NOTIFY_MODE_NORMAL:
7290 case XCB_NOTIFY_MODE_GRAB:
7293 case XCB_NOTIFY_MODE_UNGRAB:
7296 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7297 label = "WhileGrabbed";
7308 enternotify(xcb_enter_notify_event_t *e)
7311 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %s(%d), "
7312 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7313 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7314 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7315 e->child, YESNO(e->same_screen_focus), e->state);
7317 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
7318 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
7323 switch (focus_mode) {
7324 case SWM_FOCUS_DEFAULT:
7326 case SWM_FOCUS_FOLLOW:
7328 case SWM_FOCUS_SYNERGY:
7332 if ((win = find_window(e->event)) == NULL) {
7333 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7344 leavenotify(xcb_leave_notify_event_t *e)
7346 DNPRINTF(SWM_D_FOCUS, "leavenotify: window: 0x%x, mode: %s(%d), "
7347 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7348 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7349 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7350 e->child, YESNO(e->same_screen_focus), e->state);
7354 /* lets us use one switch statement for arbitrary mode/detail combinations */
7355 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7358 mapnotify(xcb_map_notify_event_t *e)
7362 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7364 win = manage_window(e->window);
7366 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7369 * focus_win can only set input focus on a mapped window.
7370 * make sure the window really has focus since it is just being mapped.
7372 if (win->ws->focus == win)
7377 mappingnotify(xcb_mapping_notify_event_t *e)
7379 xcb_refresh_keyboard_mapping(syms, e);
7381 if (e->request == XCB_MAPPING_KEYBOARD)
7386 maprequest(xcb_map_request_event_t *e)
7389 struct swm_region *r;
7390 xcb_get_window_attributes_reply_t *war;
7392 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7395 war = xcb_get_window_attributes_reply(conn,
7396 xcb_get_window_attributes(conn, e->window),
7400 if (war->override_redirect) {
7406 win = manage_window(e->window);
7408 return; /* can't happen */
7413 /* make new win focused */
7414 r = root_to_region(win->wa->root);
7415 if (win->ws == r->ws)
7421 get_atom_name(xcb_atom_t atom)
7425 xcb_get_atom_name_reply_t *r;
7427 r = xcb_get_atom_name_reply(conn,
7428 xcb_get_atom_name(conn, atom),
7431 len = xcb_get_atom_name_name_length(r);
7433 name = malloc(len + 1);
7435 memcpy(name, xcb_get_atom_name_name(r), len);
7447 propertynotify(xcb_property_notify_event_t *e)
7453 name = get_atom_name(e->atom);
7454 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7455 e->window, name, e->atom);
7459 win = find_window(e->window);
7463 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7464 update_iconic(win, 0);
7465 map_window_raised(win->id);
7472 case XCB_ATOM_WM_CLASS:
7473 case XCB_ATOM_WM_NAME:
7482 unmapnotify(xcb_unmap_notify_event_t *e)
7486 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7488 /* determine if we need to help unmanage this window */
7489 win = find_window(e->window);
7493 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7494 unmanage_window(win);
7497 /* resend unmap because we ated it */
7498 xcb_unmap_window(conn, e->window);
7505 visibilitynotify(xcb_visibility_notify_event_t *e)
7507 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7513 clientmessage(xcb_client_message_event_t *e)
7516 xcb_map_request_event_t mre;
7520 name = get_atom_name(e->type);
7521 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7522 e->window, name, e->type);
7525 win = find_window(e->window);
7528 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7529 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7530 "unmanaged window.\n");
7531 mre.window = e->window;
7537 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7538 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7541 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7542 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7543 if (win->can_delete)
7544 client_msg(win, a_delete);
7546 xcb_kill_client(conn, win->id);
7548 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7549 DNPRINTF(SWM_D_EVENT,
7550 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7551 if (win->floating) {
7552 if (e->data.data32[0] & (1<<8)) /* x */
7553 X(win) = e->data.data32[1];
7554 if (e->data.data32[0] & (1<<9)) /* y */
7555 Y(win) = e->data.data32[2];
7556 if (e->data.data32[0] & (1<<10)) /* width */
7557 WIDTH(win) = e->data.data32[3];
7558 if (e->data.data32[0] & (1<<11)) /* height */
7559 HEIGHT(win) = e->data.data32[4];
7564 /* TODO: Change stack sizes */
7565 /* notify no change was made. */
7566 config_win(win, NULL);
7569 if (e->type == ewmh[_NET_WM_STATE].atom) {
7570 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7571 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7572 if (e->data.data32[2])
7573 ewmh_update_win_state(win, e->data.data32[2],
7585 int errcode = xcb_connection_has_error(conn);
7586 #ifdef XCB_CONN_ERROR
7589 case XCB_CONN_ERROR:
7590 s = "Socket error, pipe error or other stream error.";
7592 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
7593 s = "Extension not supported.";
7595 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
7596 s = "Insufficient memory.";
7598 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
7599 s = "Request length was exceeded.";
7601 case XCB_CONN_CLOSED_PARSE_ERR:
7602 s = "Error parsing display string.";
7605 s = "Unknown error.";
7608 errx(errcode, "X CONNECTION ERROR: %s", s);
7611 errx(errcode, "X CONNECTION ERROR");
7619 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7621 xcb_void_cookie_t wac;
7622 xcb_generic_error_t *error;
7624 /* this causes an error if some other window manager is running */
7625 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7626 for (i = 0; i < num_screens; i++) {
7627 if ((sc = get_screen(i)) == NULL)
7628 errx(1, "ERROR: can't get screen %d.", i);
7629 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7631 wac = xcb_change_window_attributes_checked(conn, sc->root,
7632 XCB_CW_EVENT_MASK, &val);
7633 if ((error = xcb_request_check(conn, wac))) {
7634 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7645 new_region(struct swm_screen *s, int x, int y, int w, int h)
7647 struct swm_region *r, *n;
7648 struct workspace *ws = NULL;
7651 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7652 s->idx, w, h, x, y);
7654 /* remove any conflicting regions */
7655 n = TAILQ_FIRST(&s->rl);
7658 n = TAILQ_NEXT(r, entry);
7659 if (X(r) < (x + w) &&
7660 (X(r) + WIDTH(r)) > x &&
7662 (Y(r) + HEIGHT(r)) > y) {
7663 if (r->ws->r != NULL)
7664 r->ws->old_r = r->ws->r;
7667 TAILQ_REMOVE(&s->rl, r, entry);
7668 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7672 /* search old regions for one to reuse */
7674 /* size + location match */
7675 TAILQ_FOREACH(r, &s->orl, entry)
7676 if (X(r) == x && Y(r) == y &&
7677 HEIGHT(r) == h && WIDTH(r) == w)
7681 TAILQ_FOREACH(r, &s->orl, entry)
7682 if (HEIGHT(r) == h && WIDTH(r) == w)
7686 TAILQ_REMOVE(&s->orl, r, entry);
7687 /* try to use old region's workspace */
7688 if (r->ws->r == NULL)
7691 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7692 err(1, "new_region: calloc: failed to allocate memory "
7695 /* if we don't have a workspace already, find one */
7697 for (i = 0; i < workspace_limit; i++)
7698 if (s->ws[i].r == NULL) {
7705 errx(1, "new_region: no free workspaces");
7716 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7722 #ifdef SWM_XRR_HAS_CRTC
7725 #endif /* SWM_XRR_HAS_CRTC */
7726 struct swm_region *r;
7728 xcb_randr_get_screen_resources_current_cookie_t src;
7729 xcb_randr_get_screen_resources_current_reply_t *srr;
7730 xcb_randr_get_crtc_info_cookie_t cic;
7731 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7732 xcb_randr_crtc_t *crtc;
7733 xcb_screen_t *screen;
7735 if ((screen = get_screen(i)) == NULL)
7736 errx(1, "ERROR: can't get screen %d.", i);
7738 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7739 if (i >= num_screens)
7740 errx(1, "scan_xrandr: invalid screen");
7742 /* remove any old regions */
7743 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7744 r->ws->old_r = r->ws->r = NULL;
7746 TAILQ_REMOVE(&screens[i].rl, r, entry);
7747 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7751 /* map virtual screens onto physical screens */
7752 #ifdef SWM_XRR_HAS_CRTC
7753 if (xrandr_support) {
7754 src = xcb_randr_get_screen_resources_current(conn,
7756 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7759 new_region(&screens[i], 0, 0,
7760 screen->width_in_pixels,
7761 screen->height_in_pixels);
7764 ncrtc = srr->num_crtcs;
7766 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7767 for (c = 0; c < ncrtc; c++) {
7768 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7770 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7773 if (cir->num_outputs == 0) {
7779 new_region(&screens[i], 0, 0,
7780 screen->width_in_pixels,
7781 screen->height_in_pixels);
7783 new_region(&screens[i],
7784 cir->x, cir->y, cir->width, cir->height);
7789 #endif /* SWM_XRR_HAS_CRTC */
7791 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7792 screen->height_in_pixels);
7797 screenchange(xcb_randr_screen_change_notify_event_t *e)
7799 struct swm_region *r;
7802 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7804 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7805 /* silly event doesn't include the screen index */
7806 for (i = 0; i < num_screens; i++)
7807 if (screens[i].root == e->root)
7809 if (i >= num_screens)
7810 errx(1, "screenchange: screen not found");
7812 /* brute force for now, just re-enumerate the regions */
7816 print_win_geom(e->root);
7818 /* add bars to all regions */
7819 for (i = 0; i < num_screens; i++)
7820 TAILQ_FOREACH(r, &screens[i].rl, entry)
7828 xcb_window_t *wins = NULL;
7830 int i, j, num_screens;
7831 uint16_t state, manage;
7833 xcb_query_tree_cookie_t qtc;
7834 xcb_query_tree_reply_t *qtr;
7835 xcb_get_window_attributes_cookie_t c;
7836 xcb_get_window_attributes_reply_t *r;
7837 xcb_get_property_cookie_t pc;
7839 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
7840 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7841 for (i = 0; i < num_screens; i++) {
7842 qtc = xcb_query_tree(conn, screens[i].root);
7843 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7846 wins = xcb_query_tree_children(qtr);
7847 no = xcb_query_tree_children_length(qtr);
7848 /* attach windows to a region */
7849 /* normal windows */
7850 for (j = 0; j < no; j++) {
7851 c = xcb_get_window_attributes(conn, wins[j]);
7852 r = xcb_get_window_attributes_reply(conn, c, NULL);
7855 if (r->override_redirect) {
7860 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7861 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7867 state = getstate(wins[j]);
7868 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7869 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7870 manage_window(wins[j]);
7873 /* transient windows */
7874 for (j = 0; j < no; j++) {
7875 c = xcb_get_window_attributes(conn, wins[j]);
7876 r = xcb_get_window_attributes_reply(conn, c, NULL);
7879 if (r->override_redirect) {
7885 state = getstate(wins[j]);
7886 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7887 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7888 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7889 &wins[j], NULL) && manage)
7890 manage_window(wins[j]);
7894 DNPRINTF(SWM_D_INIT, "grab_windows: done\n");
7900 int i, j, k, num_screens;
7901 struct workspace *ws;
7902 uint32_t gcv[1], wa[1];
7903 const xcb_query_extension_reply_t *qep;
7904 xcb_screen_t *screen;
7905 xcb_cursor_t cursor;
7906 xcb_font_t cursor_font;
7907 xcb_randr_query_version_cookie_t c;
7908 xcb_randr_query_version_reply_t *r;
7910 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7911 if ((screens = calloc(num_screens,
7912 sizeof(struct swm_screen))) == NULL)
7913 err(1, "setup_screens: calloc: failed to allocate memory for "
7916 /* initial Xrandr setup */
7918 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7920 c = xcb_randr_query_version(conn, 1, 1);
7921 r = xcb_randr_query_version_reply(conn, c, NULL);
7923 if (r->major_version >= 1) {
7925 xrandr_eventbase = qep->first_event;
7931 cursor_font = xcb_generate_id(conn);
7932 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7934 cursor = xcb_generate_id(conn);
7935 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7936 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7939 /* map physical screens */
7940 for (i = 0; i < num_screens; i++) {
7941 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7943 TAILQ_INIT(&screens[i].rl);
7944 TAILQ_INIT(&screens[i].orl);
7945 if ((screen = get_screen(i)) == NULL)
7946 errx(1, "ERROR: can't get screen %d.", i);
7947 screens[i].root = screen->root;
7949 /* set default colors */
7950 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7951 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7952 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7953 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7954 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7956 /* create graphics context on screen */
7957 screens[i].bar_gc = xcb_generate_id(conn);
7959 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7960 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7962 /* set default cursor */
7963 xcb_change_window_attributes(conn, screens[i].root,
7966 /* init all workspaces */
7967 /* XXX these should be dynamically allocated too */
7968 for (j = 0; j < SWM_WS_MAX; j++) {
7969 ws = &screens[i].ws[j];
7975 TAILQ_INIT(&ws->winlist);
7976 TAILQ_INIT(&ws->unmanagedlist);
7978 for (k = 0; layouts[k].l_stack != NULL; k++)
7979 if (layouts[k].l_config != NULL)
7980 layouts[k].l_config(ws,
7981 SWM_ARG_ID_STACKINIT);
7982 ws->cur_layout = &layouts[0];
7983 ws->cur_layout->l_string(ws);
7989 xcb_randr_select_input(conn, screens[i].root,
7990 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7992 xcb_free_cursor(conn, cursor);
7993 xcb_close_font(conn, cursor_font);
7999 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8000 err(1, "setup_globals: strdup: failed to allocate memory.");
8002 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8003 err(1, "setup_globals: strdup: failed to allocate memory.");
8005 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8006 errx(1, "unable to allocate key symbols");
8008 a_state = get_atom_from_string("WM_STATE");
8009 a_prot = get_atom_from_string("WM_PROTOCOLS");
8010 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8011 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8012 a_wmname = get_atom_from_string("WM_NAME");
8013 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8014 a_utf8_string = get_atom_from_string("UTF8_STRING");
8015 a_string = get_atom_from_string("STRING");
8016 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8017 a_swm_ws = get_atom_from_string("_SWM_WS");
8024 xcb_atom_t netwmcheck;
8025 xcb_window_t root, win;
8028 /* work around sun jdk bugs, code from wmname */
8029 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8031 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8032 for (i = 0; i < num_screens; i++) {
8033 root = screens[i].root;
8035 win = xcb_generate_id(conn);
8036 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
8037 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
8038 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
8039 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
8040 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
8042 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8043 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8044 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8045 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8046 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8047 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8052 shutdown_cleanup(void)
8056 /* disable alarm because the following code may not be interrupted */
8058 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8059 err(1, "can't disable alarm");
8067 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8068 for (i = 0; i < num_screens; ++i) {
8069 if (screens[i].bar_gc != 0)
8070 xcb_free_gc(conn, screens[i].bar_gc);
8071 if (!bar_font_legacy)
8072 XftColorFree(display, DefaultVisual(display, i),
8073 DefaultColormap(display, i), &bar_font_color);
8076 if (bar_font_legacy)
8077 XFreeFontSet(display, bar_fs);
8079 XftFontClose(display, bar_font);
8082 xcb_key_symbols_free(syms);
8084 xcb_disconnect(conn);
8088 event_error(xcb_generic_error_t *e)
8092 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8093 "resource_id: %u, minor_code: %u\n",
8094 xcb_event_get_error_label(e->error_code), e->error_code,
8095 xcb_event_get_request_label(e->major_code), e->major_code,
8096 e->sequence, e->resource_id, e->minor_code);
8100 event_handle(xcb_generic_event_t *evt)
8102 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8104 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
8105 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8106 XCB_EVENT_RESPONSE_TYPE(evt));
8109 #define EVENT(type, callback) case type: callback((void *)evt); return
8110 EVENT(0, event_error);
8111 EVENT(XCB_BUTTON_PRESS, buttonpress);
8112 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8113 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8114 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8115 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8116 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8117 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8118 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8119 /*EVENT(XCB_CREATE_NOTIFY, );*/
8120 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8121 EVENT(XCB_ENTER_NOTIFY, enternotify);
8122 EVENT(XCB_EXPOSE, expose);
8124 EVENT(XCB_FOCUS_IN, focusin);
8126 /*EVENT(XCB_FOCUS_OUT, );*/
8127 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8128 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8129 EVENT(XCB_KEY_PRESS, keypress);
8130 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8131 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8133 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8135 EVENT(XCB_MAP_NOTIFY, mapnotify);
8136 EVENT(XCB_MAP_REQUEST, maprequest);
8137 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8138 /*EVENT(XCB_MOTION_NOTIFY, );*/
8139 /*EVENT(XCB_NO_EXPOSURE, );*/
8140 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8141 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8142 /*EVENT(XCB_RESIZE_REQUEST, );*/
8143 /*EVENT(XCB_SELECTION_CLEAR, );*/
8144 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8145 /*EVENT(XCB_SELECTION_REQUEST, );*/
8146 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8147 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8150 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8151 screenchange((void *)evt);
8155 main(int argc, char *argv[])
8157 struct swm_region *r, *rr;
8158 struct ws_win *winfocus = NULL;
8160 char conf[PATH_MAX], *cfile = NULL;
8162 int xfd, i, num_screens;
8163 struct sigaction sact;
8164 xcb_generic_event_t *evt;
8168 /* suppress unused warning since var is needed */
8171 time_started = time(NULL);
8174 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8175 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8176 warnx("no locale support");
8178 /* handle some signals */
8179 bzero(&sact, sizeof(sact));
8180 sigemptyset(&sact.sa_mask);
8182 sact.sa_handler = sighdlr;
8183 sigaction(SIGINT, &sact, NULL);
8184 sigaction(SIGQUIT, &sact, NULL);
8185 sigaction(SIGTERM, &sact, NULL);
8186 sigaction(SIGHUP, &sact, NULL);
8188 sact.sa_handler = sighdlr;
8189 sact.sa_flags = SA_NOCLDSTOP;
8190 sigaction(SIGCHLD, &sact, NULL);
8192 if (!(display = XOpenDisplay(0)))
8193 errx(1, "can not open display");
8195 conn = XGetXCBConnection(display);
8196 if (xcb_connection_has_error(conn))
8197 errx(1, "can not get XCB connection");
8199 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8201 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8202 xfd = xcb_get_file_descriptor(conn);
8204 /* look for local and global conf file */
8205 pwd = getpwuid(getuid());
8207 errx(1, "invalid user: %d", getuid());
8209 xcb_grab_server(conn);
8212 /* flush all events */
8213 while ((evt = xcb_poll_for_event(conn))) {
8214 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8219 if (enable_wm() != 0)
8220 errx(1, "another window manager is currently running");
8231 for (i = 0; ; i++) {
8236 snprintf(conf, sizeof conf, "%s/.%s",
8237 pwd->pw_dir, SWM_CONF_FILE);
8241 snprintf(conf, sizeof conf, "/etc/%s",
8246 snprintf(conf, sizeof conf, "%s/.%s",
8247 pwd->pw_dir, SWM_CONF_FILE_OLD);
8251 snprintf(conf, sizeof conf, "/etc/%s",
8258 if (strlen(conf) && stat(conf, &sb) != -1)
8259 if (S_ISREG(sb.st_mode)) {
8266 /* load conf (if any) */
8268 conf_load(cfile, SWM_CONF_DEFAULT);
8271 /* set some values to work around bad programs */
8273 /* grab existing windows (before we build the bars) */
8276 if (getenv("SWM_STARTED") == NULL)
8277 setenv("SWM_STARTED", "YES", 1);
8279 /* setup all bars */
8280 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8281 for (i = 0; i < num_screens; i++)
8282 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8283 if (winfocus == NULL)
8284 winfocus = TAILQ_FIRST(&r->ws->winlist);
8293 xcb_ungrab_server(conn);
8297 while ((evt = xcb_poll_for_event(conn))) {
8304 /* if we are being restarted go focus on first window */
8306 rr = winfocus->ws->r;
8308 /* not a visible window */
8312 /* move pointer to first screen if multi screen */
8313 if (num_screens > 1 || outputs > 1)
8314 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
8315 rr->s[0].root, 0, 0, 0, 0, X(rr),
8316 Y(rr) + (bar_enabled ? bar_height : 0));
8318 a.id = SWM_ARG_ID_FOCUSCUR;
8328 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8329 if (errno != EINTR) {
8330 DNPRINTF(SWM_D_MISC, "select failed");
8332 if (restart_wm == 1)
8333 restart(NULL, NULL);
8334 if (search_resp == 1)