2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xft/Xft.h>
94 #include <X11/Xlib-xcb.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <xcb/randr.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr = SPECTRWM_BUILDSTR;
112 static const char *buildstr = SPECTRWM_VERSION;
116 # error XRandR versions less than 1.0 are not supported
121 #define SWM_XRR_HAS_CRTC
125 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
126 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
128 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
129 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
130 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
131 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
132 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
133 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
134 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
135 #define xcb_icccm_get_wm_class xcb_get_wm_class
136 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
137 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
138 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
139 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
140 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
141 #define xcb_icccm_get_wm_name xcb_get_wm_name
142 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
143 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
144 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
145 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
146 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
147 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
148 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
149 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
150 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
151 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
154 /*#define SWM_DEBUG*/
156 #define DPRINTF(x...) do { \
158 fprintf(stderr, x); \
160 #define DNPRINTF(n,x...) do { \
161 if (swm_debug & n) { \
162 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
163 fprintf(stderr, x); \
166 #define SWM_D_MISC 0x0001
167 #define SWM_D_EVENT 0x0002
168 #define SWM_D_WS 0x0004
169 #define SWM_D_FOCUS 0x0008
170 #define SWM_D_MOVE 0x0010
171 #define SWM_D_STACK 0x0020
172 #define SWM_D_MOUSE 0x0040
173 #define SWM_D_PROP 0x0080
174 #define SWM_D_CLASS 0x0100
175 #define SWM_D_KEY 0x0200
176 #define SWM_D_QUIRK 0x0400
177 #define SWM_D_SPAWN 0x0800
178 #define SWM_D_EVENTQ 0x1000
179 #define SWM_D_CONF 0x2000
180 #define SWM_D_BAR 0x4000
181 #define SWM_D_INIT 0x8000
183 u_int32_t swm_debug = 0
202 #define DPRINTF(x...)
203 #define DNPRINTF(n,x...)
206 /* convert 8-bit to 16-bit */
207 #define RGB_8_TO_16(col) ((col) << 8) + (col)
209 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
210 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
211 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
212 xrc.blue = RGB_8_TO_16((px) & 0xff); \
215 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
216 #define MODKEY Mod1Mask
217 #define CLEANMASK(mask) ((mask) & ~(numlockmask | LockMask))
218 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
219 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
220 #define SWM_PROPLEN (16)
221 #define SWM_FUNCNAME_LEN (32)
222 #define SWM_KEYS_LEN (255)
223 #define SWM_QUIRK_LEN (64)
224 #define X(r) (r)->g.x
225 #define Y(r) (r)->g.y
226 #define WIDTH(r) (r)->g.w
227 #define HEIGHT(r) (r)->g.h
228 #define BORDER(w) ((w)->bordered ? border_width : 0)
229 #define MAX_X(r) ((r)->g.x + (r)->g.w)
230 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
231 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
232 #define SH_MIN_W(w) (w)->sh.min_width
233 #define SH_MIN_H(w) (w)->sh.min_height
234 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
235 #define SH_MAX_W(w) (w)->sh.max_width
236 #define SH_MAX_H(w) (w)->sh.max_height
237 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
238 #define SH_INC_W(w) (w)->sh.width_inc
239 #define SH_INC_H(w) (w)->sh.height_inc
240 #define SWM_MAX_FONT_STEPS (3)
241 #define WINID(w) ((w) ? (w)->id : 0)
242 #define YESNO(x) ((x) ? "yes" : "no")
244 #define SWM_FOCUS_DEFAULT (0)
245 #define SWM_FOCUS_SYNERGY (1)
246 #define SWM_FOCUS_FOLLOW (2)
248 #define SWM_CONF_DEFAULT (0)
249 #define SWM_CONF_KEYMAPPING (1)
252 #define SWM_LIB "/usr/local/lib/libswmhack.so"
259 xcb_atom_t a_takefocus;
261 xcb_atom_t a_netwmname;
262 xcb_atom_t a_utf8_string;
264 xcb_atom_t a_swm_iconic;
266 volatile sig_atomic_t running = 1;
267 volatile sig_atomic_t restart_wm = 0;
269 /*int last_focus_event = FocusOut;*/
273 int xrandr_eventbase;
274 unsigned int numlockmask = 0;
277 xcb_connection_t *conn;
278 xcb_key_symbols_t *syms;
281 int cycle_visible = 0;
283 int font_adjusted = 0;
284 unsigned int mod_key = MODKEY;
287 struct swm_region *search_r;
288 int select_list_pipe[2];
289 int select_resp_pipe[2];
291 volatile sig_atomic_t search_resp;
292 int search_resp_action;
294 struct search_window {
295 TAILQ_ENTRY(search_window) entry;
299 xcb_window_t indicator;
301 TAILQ_HEAD(search_winlist, search_window);
302 struct search_winlist search_wl;
307 SWM_SEARCH_UNICONIFY,
308 SWM_SEARCH_NAME_WORKSPACE,
309 SWM_SEARCH_SEARCH_WORKSPACE,
310 SWM_SEARCH_SEARCH_WINDOW
313 #define SWM_STACK_TOP (0)
314 #define SWM_STACK_BOTTOM (1)
315 #define SWM_STACK_ABOVE (2)
316 #define SWM_STACK_BELOW (3)
319 double dialog_ratio = 0.6;
321 #define SWM_BAR_MAX (256)
322 #define SWM_BAR_JUSTIFY_LEFT (0)
323 #define SWM_BAR_JUSTIFY_CENTER (1)
324 #define SWM_BAR_JUSTIFY_RIGHT (2)
325 #define SWM_BAR_OFFSET (4)
326 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
327 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
328 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
329 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
330 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
332 #ifdef X_HAVE_UTF8_STRING
333 #define DRAWSTRING(x...) Xutf8DrawString(x)
335 #define DRAWSTRING(x...) XmbDrawString(x)
338 char *bar_argv[] = { NULL, NULL };
340 unsigned char bar_ext[SWM_BAR_MAX];
341 char bar_vertext[SWM_BAR_MAX];
343 sig_atomic_t bar_alarm = 0;
346 int bar_border_width = 1;
347 int bar_at_bottom = 0;
349 int bar_extra_running = 0;
352 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
353 char *bar_format = NULL;
354 int stack_enabled = 1;
355 int clock_enabled = 1;
356 int urgent_enabled = 0;
357 char *clock_format = NULL;
358 int title_name_enabled = 0;
359 int title_class_enabled = 0;
360 int window_name_enabled = 0;
361 int focus_mode = SWM_FOCUS_DEFAULT;
362 int focus_close = SWM_STACK_BELOW;
363 int focus_close_wrap = 1;
364 int focus_default = SWM_STACK_TOP;
365 int spawn_position = SWM_STACK_TOP;
366 int disable_border = 0;
367 int border_width = 1;
368 int verbose_layout = 0;
372 XFontSetExtents *bar_fs_extents;
374 int bar_font_legacy = 0;
376 XftColor bar_font_color;
379 /* layout manager data */
380 struct swm_geometry {
393 struct swm_geometry g;
396 /* virtual "screens" */
398 TAILQ_ENTRY(swm_region) entry;
399 struct swm_geometry g;
400 struct workspace *ws; /* current workspace on this region */
401 struct workspace *ws_prior; /* prior workspace on this region */
402 struct swm_screen *s; /* screen idx */
405 TAILQ_HEAD(swm_region_list, swm_region);
408 TAILQ_ENTRY(ws_win) entry;
410 xcb_window_t transient;
411 struct ws_win *child_trans; /* transient child window */
412 struct swm_geometry g; /* current geometry */
413 struct swm_geometry g_float; /* region coordinates */
414 int g_floatvalid; /* g_float geometry validity */
415 int floatmaxed; /* whether maxed by max_stack */
420 unsigned int ewmh_flags;
421 int font_size_boundary[SWM_MAX_FONT_STEPS];
427 unsigned long quirks;
428 struct workspace *ws; /* always valid */
429 struct swm_screen *s; /* always valid, never changes */
430 xcb_get_geometry_reply_t *wa;
432 xcb_icccm_get_wm_class_reply_t ch;
433 xcb_icccm_wm_hints_t hints;
435 TAILQ_HEAD(ws_win_list, ws_win);
439 TAILQ_ENTRY(pid_e) entry;
443 TAILQ_HEAD(pid_list, pid_e);
444 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
446 /* layout handlers */
448 void vertical_config(struct workspace *, int);
449 void vertical_stack(struct workspace *, struct swm_geometry *);
450 void horizontal_config(struct workspace *, int);
451 void horizontal_stack(struct workspace *, struct swm_geometry *);
452 void max_stack(struct workspace *, struct swm_geometry *);
453 void plain_stacker(struct workspace *);
454 void fancy_stacker(struct workspace *);
457 void (*l_stack)(struct workspace *, struct swm_geometry *);
458 void (*l_config)(struct workspace *, int);
460 #define SWM_L_FOCUSPREV (1<<0)
461 #define SWM_L_MAPONFOCUS (1<<1)
462 void (*l_string)(struct workspace *);
464 /* stack, configure */
465 { vertical_stack, vertical_config, 0, plain_stacker },
466 { horizontal_stack, horizontal_config, 0, plain_stacker },
468 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
469 { NULL, NULL, 0, NULL },
472 /* position of max_stack mode in the layouts array, index into layouts! */
473 #define SWM_V_STACK (0)
474 #define SWM_H_STACK (1)
475 #define SWM_MAX_STACK (2)
477 #define SWM_H_SLICE (32)
478 #define SWM_V_SLICE (32)
480 /* define work spaces */
482 int idx; /* workspace index */
483 char *name; /* workspace name */
484 int always_raise; /* raise windows on focus */
485 struct layout *cur_layout; /* current layout handlers */
486 struct ws_win *focus; /* may be NULL */
487 struct ws_win *focus_prev; /* may be NULL */
488 struct swm_region *r; /* may be NULL */
489 struct swm_region *old_r; /* may be NULL */
490 struct ws_win_list winlist; /* list of windows in ws */
491 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
492 char stacker[10]; /* display stacker and layout */
496 int horizontal_msize;
498 int horizontal_stacks;
509 SWM_S_COLOR_BAR_BORDER,
510 SWM_S_COLOR_BAR_FONT,
516 /* physical screen mapping */
517 #define SWM_WS_MAX (22) /* hard limit */
518 int workspace_limit = 10; /* soft limit */
521 int idx; /* screen index */
522 struct swm_region_list rl; /* list of regions on this screen */
523 struct swm_region_list orl; /* list of old regions */
525 struct workspace ws[SWM_WS_MAX];
531 } c[SWM_S_COLOR_MAX];
533 xcb_gcontext_t bar_gc;
536 struct swm_screen *screens;
538 /* args to functions */
541 #define SWM_ARG_ID_FOCUSNEXT (0)
542 #define SWM_ARG_ID_FOCUSPREV (1)
543 #define SWM_ARG_ID_FOCUSMAIN (2)
544 #define SWM_ARG_ID_FOCUSCUR (4)
545 #define SWM_ARG_ID_SWAPNEXT (10)
546 #define SWM_ARG_ID_SWAPPREV (11)
547 #define SWM_ARG_ID_SWAPMAIN (12)
548 #define SWM_ARG_ID_MOVELAST (13)
549 #define SWM_ARG_ID_MASTERSHRINK (20)
550 #define SWM_ARG_ID_MASTERGROW (21)
551 #define SWM_ARG_ID_MASTERADD (22)
552 #define SWM_ARG_ID_MASTERDEL (23)
553 #define SWM_ARG_ID_FLIPLAYOUT (24)
554 #define SWM_ARG_ID_STACKRESET (30)
555 #define SWM_ARG_ID_STACKINIT (31)
556 #define SWM_ARG_ID_CYCLEWS_UP (40)
557 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
558 #define SWM_ARG_ID_CYCLESC_UP (42)
559 #define SWM_ARG_ID_CYCLESC_DOWN (43)
560 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
561 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
562 #define SWM_ARG_ID_STACKINC (50)
563 #define SWM_ARG_ID_STACKDEC (51)
564 #define SWM_ARG_ID_SS_ALL (60)
565 #define SWM_ARG_ID_SS_WINDOW (61)
566 #define SWM_ARG_ID_DONTCENTER (70)
567 #define SWM_ARG_ID_CENTER (71)
568 #define SWM_ARG_ID_KILLWINDOW (80)
569 #define SWM_ARG_ID_DELETEWINDOW (81)
570 #define SWM_ARG_ID_WIDTHGROW (90)
571 #define SWM_ARG_ID_WIDTHSHRINK (91)
572 #define SWM_ARG_ID_HEIGHTGROW (92)
573 #define SWM_ARG_ID_HEIGHTSHRINK (93)
574 #define SWM_ARG_ID_MOVEUP (100)
575 #define SWM_ARG_ID_MOVEDOWN (101)
576 #define SWM_ARG_ID_MOVELEFT (102)
577 #define SWM_ARG_ID_MOVERIGHT (103)
583 TAILQ_ENTRY(quirk) entry;
587 #define SWM_Q_FLOAT (1<<0) /* float this window */
588 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
589 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
590 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
591 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
592 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
594 TAILQ_HEAD(quirk_list, quirk);
595 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
598 * Supported EWMH hints should be added to
599 * both the enum and the ewmh array
604 _NET_MOVERESIZE_WINDOW,
605 _NET_WM_ACTION_CLOSE,
606 _NET_WM_ACTION_FULLSCREEN,
608 _NET_WM_ACTION_RESIZE,
609 _NET_WM_ALLOWED_ACTIONS,
613 _NET_WM_STATE_FULLSCREEN,
614 _NET_WM_STATE_HIDDEN,
615 _NET_WM_STATE_MAXIMIZED_HORZ,
616 _NET_WM_STATE_MAXIMIZED_VERT,
617 _NET_WM_STATE_SKIP_PAGER,
618 _NET_WM_STATE_SKIP_TASKBAR,
620 _NET_WM_WINDOW_TYPE_DIALOG,
621 _NET_WM_WINDOW_TYPE_DOCK,
622 _NET_WM_WINDOW_TYPE_NORMAL,
623 _NET_WM_WINDOW_TYPE_SPLASH,
624 _NET_WM_WINDOW_TYPE_TOOLBAR,
625 _NET_WM_WINDOW_TYPE_UTILITY,
626 _SWM_WM_STATE_MANUAL,
633 } ewmh[SWM_EWMH_HINT_MAX] = {
634 /* must be in same order as in the enum */
635 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
636 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
637 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
638 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
639 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
640 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
641 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
642 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
643 {"_NET_WM_NAME", XCB_ATOM_NONE},
644 {"_NET_WM_STATE", XCB_ATOM_NONE},
645 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
646 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
647 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
648 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
649 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
650 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
651 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
652 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
653 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
654 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
655 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
656 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
657 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
658 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
659 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
662 /* function prototypes */
663 void buttonpress(xcb_button_press_event_t *);
664 void check_conn(void);
665 void clientmessage(xcb_client_message_event_t *);
666 int conf_load(char *, int);
667 void configurenotify(xcb_configure_notify_event_t *);
668 void configurerequest(xcb_configure_request_event_t *);
669 void constrain_window(struct ws_win *, struct swm_region *, int);
670 void destroynotify(xcb_destroy_notify_event_t *);
672 void enternotify(xcb_enter_notify_event_t *);
673 void event_error(xcb_generic_error_t *);
674 void event_handle(xcb_generic_event_t *);
675 char *expand_tilde(char *);
676 void expose(xcb_expose_event_t *);
677 struct ws_win *find_window(xcb_window_t);
678 int floating_toggle_win(struct ws_win *);
679 void focus(struct swm_region *, union arg *);
680 void focus_magic(struct ws_win *);
682 void focusin(xcb_focus_in_event_t *);
684 xcb_atom_t get_atom_from_string(const char *);
686 char *get_atom_name(xcb_atom_t);
687 char *get_notify_detail_label(uint8_t);
688 char *get_notify_mode_label(uint8_t);
690 xcb_screen_t *get_screen(int);
691 char *get_win_name(xcb_window_t);
692 uint32_t getstate(xcb_window_t);
693 void grabbuttons(struct ws_win *, int);
694 void keypress(xcb_key_press_event_t *);
696 void leavenotify(xcb_leave_notify_event_t *);
698 void map_window_raised(xcb_window_t);
699 void mapnotify(xcb_map_notify_event_t *);
700 void mappingnotify(xcb_mapping_notify_event_t *);
701 void maprequest(xcb_map_request_event_t *);
702 void new_region(struct swm_screen *, int, int, int, int);
703 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
704 void propertynotify(xcb_property_notify_event_t *);
705 void spawn_select(struct swm_region *, union arg *, char *, int *);
706 void screenchange(xcb_randr_screen_change_notify_event_t *);
707 void shutdown_cleanup(void);
708 void store_float_geom(struct ws_win *, struct swm_region *);
709 void unmanage_window(struct ws_win *);
710 void unmapnotify(xcb_unmap_notify_event_t *);
711 void update_window(struct ws_win *);
712 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
715 expand_tilde(char *s)
724 errx(1, "expand_tilde: NULL string.");
733 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
734 errx(1, "expand_tilde: sysconf");
736 if ((user = calloc(1, max + 1)) == NULL)
737 errx(1, "expand_tilde: calloc");
739 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
744 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
748 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
752 errx(1, "expand_tilde: failed to allocate memory.");
758 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
760 unsigned int tmpr, tmpg, tmpb;
762 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
765 *rr = RGB_8_TO_16(tmpr);
766 *gg = RGB_8_TO_16(tmpg);
767 *bb = RGB_8_TO_16(tmpb);
773 get_screen(int screen)
775 const xcb_setup_t *r;
776 xcb_screen_iterator_t iter;
778 if ((r = xcb_get_setup(conn)) == NULL) {
779 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
783 iter = xcb_setup_roots_iterator(r);
784 for (; iter.rem; --screen, xcb_screen_next(&iter))
794 xcb_get_input_focus_cookie_t c;
795 xcb_get_input_focus_reply_t *r;
798 c = xcb_get_input_focus(conn);
800 r = xcb_get_input_focus_reply(conn, c, NULL);
806 map_window_raised(xcb_window_t win)
808 uint32_t val = XCB_STACK_MODE_ABOVE;
810 xcb_configure_window(conn, win,
811 XCB_CONFIG_WINDOW_STACK_MODE, &val);
813 xcb_map_window(conn, win);
817 get_atom_from_string(const char *str)
819 xcb_intern_atom_cookie_t c;
820 xcb_intern_atom_reply_t *r;
823 c = xcb_intern_atom(conn, 0, strlen(str), str);
824 r = xcb_intern_atom_reply(conn, c, NULL);
832 return (XCB_ATOM_NONE);
836 update_iconic(struct ws_win *win, int newv)
843 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
844 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
846 xcb_delete_property(conn, win->id, a_swm_iconic);
850 get_iconic(struct ws_win *win)
853 xcb_get_property_reply_t *pr = NULL;
856 pr = xcb_get_property_reply(conn,
857 xcb_get_property(conn, 0, win->id, a_swm_iconic,
858 XCB_ATOM_INTEGER, 0, 1), NULL);
861 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
863 v = *((int32_t *)xcb_get_property_value(pr));
874 int i, j, num_screens;
876 sup_list = get_atom_from_string("_NET_SUPPORTED");
878 for (i = 0; i < LENGTH(ewmh); i++)
879 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
881 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
882 for (i = 0; i < num_screens; i++) {
883 /* Support check window will be created by workaround(). */
885 /* Report supported atoms */
886 xcb_delete_property(conn, screens[i].root, sup_list);
887 for (j = 0; j < LENGTH(ewmh); j++)
888 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
889 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
898 xcb_atom_t sup_check, sup_list;
900 xcb_get_property_cookie_t pc;
901 xcb_get_property_reply_t *pr;
903 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
904 sup_list = get_atom_from_string("_NET_SUPPORTED");
905 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
907 for (i = 0; i < num_screens; i++) {
908 /* Get the support check window and destroy it */
909 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
910 XCB_ATOM_WINDOW, 0, 1);
911 pr = xcb_get_property_reply(conn, pc, NULL);
914 if (pr->format == sup_check) {
915 id = *((xcb_window_t *)xcb_get_property_value(pr));
917 xcb_destroy_window(conn, id);
918 xcb_delete_property(conn, screens[i].root, sup_check);
919 xcb_delete_property(conn, screens[i].root, sup_list);
926 ewmh_autoquirk(struct ws_win *win)
930 xcb_get_property_cookie_t c;
931 xcb_get_property_reply_t *r;
933 c = xcb_get_property(conn, 0, win->id,
934 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
935 r = xcb_get_property_reply(conn, c, NULL);
939 n = xcb_get_property_value_length(r);
940 type = xcb_get_property_value(r);
942 for (i = 0; i < n; i++) {
943 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
945 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
946 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
947 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
949 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
952 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
953 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
955 win->quirks = SWM_Q_FLOAT;
962 #define SWM_EWMH_ACTION_COUNT_MAX (6)
963 #define EWMH_F_FULLSCREEN (1<<0)
964 #define EWMH_F_ABOVE (1<<1)
965 #define EWMH_F_HIDDEN (1<<2)
966 #define EWMH_F_SKIP_PAGER (1<<3)
967 #define EWMH_F_SKIP_TASKBAR (1<<4)
968 #define SWM_F_MANUAL (1<<5)
971 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
979 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
980 "fullscreen %s\n", win->id, YESNO(fs));
983 if (!win->g_floatvalid)
984 store_float_geom(win, win->ws->r);
986 win->g = win->ws->r->g;
989 if (win->g_floatvalid) {
990 /* refloat at last floating relative position */
991 win->g = win->g_float;
992 X(win) += X(win->ws->r);
993 Y(win) += Y(win->ws->r);
1001 ewmh_update_actions(struct ws_win *win)
1003 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1009 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1011 if (win->floating) {
1012 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1013 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1016 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1017 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1020 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1021 #define _NET_WM_STATE_ADD 1 /* add/set property */
1022 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1025 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1027 unsigned int mask = 0;
1028 unsigned int changed = 0;
1029 unsigned int orig_flags;
1034 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1035 "action: %ld\n", win->id, (unsigned long)state, action);
1037 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1038 mask = EWMH_F_FULLSCREEN;
1039 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1040 mask = EWMH_F_ABOVE;
1041 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1042 mask = SWM_F_MANUAL;
1043 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1044 mask = EWMH_F_SKIP_PAGER;
1045 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1046 mask = EWMH_F_SKIP_TASKBAR;
1049 orig_flags = win->ewmh_flags;
1052 case _NET_WM_STATE_REMOVE:
1053 win->ewmh_flags &= ~mask;
1055 case _NET_WM_STATE_ADD:
1056 win->ewmh_flags |= mask;
1058 case _NET_WM_STATE_TOGGLE:
1059 win->ewmh_flags ^= mask;
1063 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1065 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1067 if (!floating_toggle_win(win))
1068 win->ewmh_flags = orig_flags; /* revert */
1069 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1071 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1072 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1074 if (!ewmh_set_win_fullscreen(win,
1075 win->ewmh_flags & EWMH_F_FULLSCREEN))
1076 win->ewmh_flags = orig_flags; /* revert */
1078 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1080 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1081 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1082 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1083 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1084 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
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_SKIP_PAGER].atom);
1088 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
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_TASKBAR].atom);
1092 if (win->ewmh_flags & EWMH_F_ABOVE)
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_ABOVE].atom);
1096 if (win->ewmh_flags & SWM_F_MANUAL)
1097 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1098 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1099 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1103 ewmh_get_win_state(struct ws_win *win)
1106 xcb_get_property_cookie_t c;
1107 xcb_get_property_reply_t *r;
1113 win->ewmh_flags = 0;
1115 win->ewmh_flags |= EWMH_F_ABOVE;
1117 win->ewmh_flags |= SWM_F_MANUAL;
1119 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1120 XCB_ATOM_ATOM, 0, UINT32_MAX);
1121 r = xcb_get_property_reply(conn, c, NULL);
1125 states = xcb_get_property_value(r);
1126 n = xcb_get_property_value_length(r);
1128 for (i = 0; i < n; i++)
1129 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1137 dumpwins(struct swm_region *r, union arg *args)
1141 xcb_get_window_attributes_cookie_t c;
1142 xcb_get_window_attributes_reply_t *wa;
1144 /* suppress unused warning since var is needed */
1147 if (r->ws == NULL) {
1148 warnx("dumpwins: invalid workspace");
1152 warnx("=== managed window list ws %02d ===", r->ws->idx);
1153 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1154 state = getstate(win->id);
1155 c = xcb_get_window_attributes(conn, win->id);
1156 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1158 warnx("window: 0x%x, map_state: %d, state: %u, "
1159 "transient: 0x%x", win->id, wa->map_state,
1160 state, win->transient);
1163 warnx("window: 0x%x, failed xcb_get_window_attributes",
1167 warnx("===== unmanaged window list =====");
1168 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1169 state = getstate(win->id);
1170 c = xcb_get_window_attributes(conn, win->id);
1171 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1173 warnx("window: 0x%x, map_state: %d, state: %u, "
1174 "transient: 0x%x", win->id, wa->map_state,
1175 state, win->transient);
1178 warnx("window: 0x%x, failed xcb_get_window_attributes",
1182 warnx("=================================");
1186 dumpwins(struct swm_region *r, union arg *s)
1191 #endif /* SWM_DEBUG */
1196 int saved_errno, status;
1199 saved_errno = errno;
1203 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1208 if (errno != ECHILD)
1209 warn("sighdlr: waitpid");
1210 #endif /* SWM_DEBUG */
1213 if (pid == searchpid)
1217 if (WIFEXITED(status)) {
1218 if (WEXITSTATUS(status) != 0)
1219 warnx("sighdlr: child exit status: %d",
1220 WEXITSTATUS(status));
1222 warnx("sighdlr: child is terminated "
1224 #endif /* SWM_DEBUG */
1238 errno = saved_errno;
1244 struct pid_e *p = NULL;
1246 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1251 TAILQ_FOREACH(p, &pidlist, entry) {
1260 name_to_pixel(const char *colorname)
1262 uint32_t result = 0;
1263 char cname[32] = "#";
1264 xcb_screen_t *screen;
1265 xcb_colormap_t cmap;
1266 xcb_alloc_color_reply_t *cr;
1267 xcb_alloc_named_color_reply_t *nr;
1268 uint16_t rr, gg, bb;
1270 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1271 cmap = screen->default_colormap;
1273 /* color is in format rgb://rr/gg/bb */
1274 if (strncmp(colorname, "rgb:", 4) == 0) {
1275 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1276 warnx("could not parse rgb %s", colorname);
1278 cr = xcb_alloc_color_reply(conn,
1279 xcb_alloc_color(conn, cmap, rr, gg, bb),
1285 warnx("color '%s' not found", colorname);
1288 nr = xcb_alloc_named_color_reply(conn,
1289 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1292 strlcat(cname, colorname + 2, sizeof cname - 1);
1293 nr = xcb_alloc_named_color_reply(conn,
1294 xcb_alloc_named_color(conn, cmap, strlen(cname),
1301 warnx("color '%s' not found", colorname);
1308 setscreencolor(char *val, int i, int c)
1312 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1313 if (i > 0 && i <= num_screens) {
1314 screens[i - 1].c[c].pixel = name_to_pixel(val);
1315 free(screens[i - 1].c[c].name);
1316 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1318 } else if (i == -1) {
1319 for (i = 0; i < num_screens; i++) {
1320 screens[i].c[c].pixel = name_to_pixel(val);
1321 free(screens[i].c[c].name);
1322 if ((screens[i].c[c].name = strdup(val)) == NULL)
1326 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1331 fancy_stacker(struct workspace *ws)
1333 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1334 if (ws->cur_layout->l_stack == vertical_stack)
1335 snprintf(ws->stacker, sizeof ws->stacker,
1336 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1337 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1338 if (ws->cur_layout->l_stack == horizontal_stack)
1339 snprintf(ws->stacker, sizeof ws->stacker,
1340 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1341 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1345 plain_stacker(struct workspace *ws)
1347 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1348 if (ws->cur_layout->l_stack == vertical_stack)
1349 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1350 sizeof ws->stacker);
1351 if (ws->cur_layout->l_stack == horizontal_stack)
1352 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1353 sizeof ws->stacker);
1357 custom_region(char *val)
1359 unsigned int x, y, w, h;
1360 int sidx, num_screens;
1361 xcb_screen_t *screen;
1363 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1364 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1365 errx(1, "invalid custom region, "
1366 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1367 if (sidx < 1 || sidx > num_screens)
1368 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1372 if ((screen = get_screen(sidx)) == NULL)
1373 errx(1, "ERROR: can't get screen %d.", sidx);
1376 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1378 if (x > screen->width_in_pixels ||
1379 y > screen->height_in_pixels ||
1380 w + x > screen->width_in_pixels ||
1381 h + y > screen->height_in_pixels) {
1382 warnx("ignoring region %ux%u+%u+%u - not within screen "
1383 "boundaries (%ux%u)", w, h, x, y,
1384 screen->width_in_pixels, screen->height_in_pixels);
1388 new_region(&screens[sidx], x, y, w, h);
1392 socket_setnonblock(int fd)
1396 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1397 err(1, "fcntl F_GETFL");
1398 flags |= O_NONBLOCK;
1399 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1400 err(1, "fcntl F_SETFL");
1404 bar_print_legacy(struct swm_region *r, const char *s)
1406 xcb_rectangle_t rect;
1411 XRectangle ibox, lbox;
1415 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1417 switch (bar_justify) {
1418 case SWM_BAR_JUSTIFY_LEFT:
1421 case SWM_BAR_JUSTIFY_CENTER:
1422 x = (WIDTH(r) - lbox.width) / 2;
1424 case SWM_BAR_JUSTIFY_RIGHT:
1425 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1429 if (x < SWM_BAR_OFFSET)
1434 rect.width = WIDTH(r->bar);
1435 rect.height = HEIGHT(r->bar);
1437 /* clear back buffer */
1438 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1439 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1440 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1441 sizeof(rect), &rect);
1443 /* draw back buffer */
1444 gcvd.graphics_exposures = 0;
1445 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1446 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1447 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1448 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1450 XFreeGC(display, draw);
1453 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1454 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1458 bar_print(struct swm_region *r, const char *s)
1461 xcb_rectangle_t rect;
1469 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1471 switch (bar_justify) {
1472 case SWM_BAR_JUSTIFY_LEFT:
1475 case SWM_BAR_JUSTIFY_CENTER:
1476 x = (WIDTH(r) - info.width) / 2;
1478 case SWM_BAR_JUSTIFY_RIGHT:
1479 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1483 if (x < SWM_BAR_OFFSET)
1488 rect.width = WIDTH(r->bar);
1489 rect.height = HEIGHT(r->bar);
1491 /* clear back buffer */
1492 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1493 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1494 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1495 sizeof(rect), &rect);
1497 /* draw back buffer */
1498 draw = XftDrawCreate(display, r->bar->buffer,
1499 DefaultVisual(display, r->s->idx),
1500 DefaultColormap(display, r->s->idx));
1502 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1503 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1506 XftDrawDestroy(draw);
1509 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1510 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1514 bar_extra_stop(void)
1518 bzero(bar_pipe, sizeof bar_pipe);
1521 kill(bar_pid, SIGTERM);
1524 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1529 bar_class_name(char *s, size_t sz, struct swm_region *r)
1531 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1533 if (r->ws->focus->ch.class_name != NULL)
1534 strlcat(s, r->ws->focus->ch.class_name, sz);
1538 bar_title_name(char *s, size_t sz, struct swm_region *r)
1540 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1542 if (r->ws->focus->ch.instance_name != NULL)
1543 strlcat(s, r->ws->focus->ch.instance_name, sz);
1547 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1549 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1552 bar_class_name(s, sz, r);
1553 strlcat(s, ":", sz);
1554 bar_title_name(s, sz, r);
1558 bar_window_float(char *s, size_t sz, struct swm_region *r)
1560 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1562 if (r->ws->focus->floating)
1563 strlcat(s, "(f)", sz);
1567 bar_window_name(char *s, size_t sz, struct swm_region *r)
1571 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1573 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1576 strlcat(s, title, sz);
1580 int urgent[SWM_WS_MAX];
1582 bar_urgent(char *s, size_t sz)
1585 int i, j, num_screens;
1587 xcb_get_property_cookie_t c;
1588 xcb_icccm_wm_hints_t hints;
1590 for (i = 0; i < workspace_limit; i++)
1593 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1594 for (i = 0; i < num_screens; i++)
1595 for (j = 0; j < workspace_limit; j++)
1596 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1597 c = xcb_icccm_get_wm_hints(conn, win->id);
1598 if (xcb_icccm_get_wm_hints_reply(conn, c,
1601 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1605 for (i = 0; i < workspace_limit; i++) {
1607 snprintf(b, sizeof b, "%d ", i + 1);
1609 snprintf(b, sizeof b, "- ");
1615 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1617 if (r == NULL || r->ws == NULL)
1619 if (r->ws->name != NULL)
1620 strlcat(s, r->ws->name, sz);
1623 /* build the default bar format according to the defined enabled options */
1625 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1627 /* if format provided, just copy the buffers */
1628 if (bar_format != NULL) {
1629 strlcpy(fmtnew, fmtexp, sz);
1633 /* reset the output buffer */
1636 strlcat(fmtnew, "+N:+I ", sz);
1638 strlcat(fmtnew, "+S", sz);
1639 strlcat(fmtnew, " ", sz);
1641 /* only show the workspace name if there's actually one */
1642 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1643 strlcat(fmtnew, "<+D>", sz);
1644 strlcat(fmtnew, "+3<", sz);
1646 if (clock_enabled) {
1647 strlcat(fmtnew, fmtexp, sz);
1648 strlcat(fmtnew, "+4<", sz);
1651 /* bar_urgent already adds the space before the last asterisk */
1653 strlcat(fmtnew, "* +U*+4<", sz);
1655 if (title_class_enabled) {
1656 strlcat(fmtnew, "+C", sz);
1657 if (title_name_enabled == 0)
1658 strlcat(fmtnew, "+4<", sz);
1661 /* checks needed by the colon and floating strlcat(3) calls below */
1662 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1663 if (title_name_enabled) {
1664 if (title_class_enabled)
1665 strlcat(fmtnew, ":", sz);
1666 strlcat(fmtnew, "+T+4<", sz);
1668 if (window_name_enabled) {
1669 if (r->ws->focus->floating)
1670 strlcat(fmtnew, "+F ", sz);
1671 strlcat(fmtnew, "+64W ", sz);
1675 /* finally add the action script output and the version */
1676 strlcat(fmtnew, "+4<+A+4<+V", sz);
1680 bar_replace_pad(char *tmp, int *limit, size_t sz)
1682 /* special case; no limit given, pad one space, instead */
1683 if (*limit == (int)sz - 1)
1685 snprintf(tmp, sz, "%*s", *limit, " ");
1688 /* replaces the bar format character sequences (like in tmux(1)) */
1690 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1694 char tmp[SWM_BAR_MAX];
1698 /* reset strlcat(3) buffer */
1701 /* get number, if any */
1704 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1705 limit = sizeof tmp - 1;
1706 if (limit <= 0 || limit >= (int)sizeof tmp)
1707 limit = sizeof tmp - 1;
1709 /* there is nothing to replace (ie EOL) */
1716 bar_replace_pad(tmp, &limit, sizeof tmp);
1719 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1722 bar_class_name(tmp, sizeof tmp, r);
1725 bar_workspace_name(tmp, sizeof tmp, r);
1728 bar_window_float(tmp, sizeof tmp, r);
1731 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1734 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1737 bar_class_title_name(tmp, sizeof tmp, r);
1740 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1743 bar_title_name(tmp, sizeof tmp, r);
1746 bar_urgent(tmp, sizeof tmp);
1749 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1752 bar_window_name(tmp, sizeof tmp, r);
1755 /* unknown character sequence; copy as-is */
1756 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1762 if ((int)len < limit)
1764 while (limit-- > 0) {
1765 if (*offrep >= sz - 1)
1767 fmtrep[(*offrep)++] = *ptr++;
1775 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1780 while (*fmt != '\0') {
1782 /* skip ordinary characters */
1785 fmtrep[off++] = *fmt++;
1789 /* character sequence found; replace it */
1790 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1799 bar_fmt_expand(char *fmtexp, size_t sz)
1806 /* start by grabbing the current time and date */
1808 localtime_r(&tmt, &tm);
1810 /* figure out what to expand */
1811 if (bar_format != NULL)
1813 else if (bar_format == NULL && clock_enabled)
1815 /* if nothing to expand bail out */
1821 /* copy as-is, just in case the format shouldn't be expanded below */
1822 strlcpy(fmtexp, fmt, sz);
1823 /* finally pass the string through strftime(3) */
1824 #ifndef SWM_DENY_CLOCK_FORMAT
1825 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1826 warnx("format too long");
1834 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1835 char fmtrep[SWM_BAR_MAX];
1837 struct swm_region *r;
1839 /* expand the format by first passing it through strftime(3) */
1840 bar_fmt_expand(fmtexp, sizeof fmtexp);
1842 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1843 for (i = 0; i < num_screens; i++) {
1844 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1847 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1848 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1849 if (bar_font_legacy)
1850 bar_print_legacy(r, fmtrep);
1852 bar_print(r, fmtrep);
1863 if (bar_enabled == 0)
1865 if (bar_extra && bar_extra_running) {
1866 /* ignore short reads; it'll correct itself */
1867 while ((b = fgetln(stdin, &len)) != NULL)
1868 if (b && b[len - 1] == '\n') {
1870 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1872 if (b == NULL && errno != EAGAIN) {
1873 warn("bar_update: bar_extra failed");
1877 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1886 /* suppress unused warning since var is needed */
1893 bar_toggle(struct swm_region *r, union arg *args)
1895 struct swm_region *tmpr;
1898 /* suppress unused warnings since vars are needed */
1902 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1904 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1906 for (i = 0; i < num_screens; i++)
1907 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1909 xcb_unmap_window(conn, tmpr->bar->id);
1911 for (i = 0; i < num_screens; i++)
1912 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1914 map_window_raised(tmpr->bar->id);
1917 bar_enabled = !bar_enabled;
1920 /* must be after stack */
1929 struct swm_region *r;
1933 /* do this here because the conf file is in memory */
1934 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1935 /* launch external status app */
1936 bar_extra_running = 1;
1937 if (pipe(bar_pipe) == -1)
1938 err(1, "pipe error");
1939 socket_setnonblock(bar_pipe[0]);
1940 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1941 if (dup2(bar_pipe[0], 0) == -1)
1943 if (dup2(bar_pipe[1], 1) == -1)
1945 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1946 err(1, "could not disable SIGPIPE");
1947 switch (bar_pid = fork()) {
1949 err(1, "cannot fork");
1953 execvp(bar_argv[0], bar_argv);
1954 err(1, "%s external app failed", bar_argv[0]);
1956 default: /* parent */
1962 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1963 for (i = 0; i < num_screens; i++)
1964 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1967 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1968 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1969 xcb_change_window_attributes(conn, r->bar->id,
1970 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1980 while ((s = index(s, '-'))) {
1985 return (count == 14);
1991 char *default_string;
1992 char **missing_charsets;
1993 int num_missing_charsets = 0;
1997 XFreeFontSet(display, bar_fs);
2001 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2003 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2004 &num_missing_charsets, &default_string);
2006 if (num_missing_charsets > 0) {
2007 warnx("Unable to load charset(s):");
2009 for (i = 0; i < num_missing_charsets; ++i)
2010 warnx("%s", missing_charsets[i]);
2012 XFreeStringList(missing_charsets);
2014 if (strcmp(default_string, ""))
2015 warnx("Glyphs from those sets will be replaced "
2016 "by '%s'.", default_string);
2018 warnx("Glyphs from those sets won't be drawn.");
2022 errx(1, "Error creating font set structure.");
2024 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2026 bar_height = bar_fs_extents->max_logical_extent.height +
2027 2 * bar_border_width;
2034 xft_init(struct swm_region *r)
2036 char *font, *d, *search;
2039 if (bar_font == NULL) {
2040 if ((d = strdup(bar_fonts)) == NULL)
2041 errx(1, "insufficient memory.");
2043 while ((font = strsep(&search, ",")) != NULL) {
2047 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2050 bar_font = XftFontOpenXlfd(display, r->s->idx,
2053 bar_font = XftFontOpenName(display, r->s->idx,
2058 warnx("unable to load font %s", font);
2061 DNPRINTF(SWM_D_INIT, "successfully opened "
2069 if (bar_font == NULL)
2070 errx(1, "unable to open a font");
2072 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2074 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2075 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2076 warn("unable to allocate Xft color");
2078 bar_height = bar_font->height + 2 * bar_border_width;
2085 bar_setup(struct swm_region *r)
2087 xcb_screen_t *screen;
2090 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2093 if ((screen = get_screen(r->s->idx)) == NULL)
2094 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2099 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2100 err(1, "bar_setup: calloc: failed to allocate memory.");
2102 if (bar_font_legacy)
2108 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2109 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2110 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2112 r->bar->id = xcb_generate_id(conn);
2113 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2114 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2115 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2117 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2118 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2119 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2120 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2121 | XCB_CW_EVENT_MASK, wa);
2123 r->bar->buffer = xcb_generate_id(conn);
2124 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2125 WIDTH(r->bar), HEIGHT(r->bar));
2128 xcb_randr_select_input(conn, r->bar->id,
2129 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2132 map_window_raised(r->bar->id);
2134 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2135 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2138 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2139 err(1, "could not install bar_signal");
2144 bar_cleanup(struct swm_region *r)
2148 xcb_destroy_window(conn, r->bar->id);
2149 xcb_free_pixmap(conn, r->bar->buffer);
2155 set_win_state(struct ws_win *win, uint16_t state)
2157 uint16_t data[2] = { state, XCB_ATOM_NONE };
2159 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2164 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2165 a_state, 32, 2, data);
2169 getstate(xcb_window_t w)
2171 uint32_t result = 0;
2172 xcb_get_property_cookie_t c;
2173 xcb_get_property_reply_t *r;
2175 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2176 r = xcb_get_property_reply(conn, c, NULL);
2179 if (r->type == a_state && r->format == 32 && r->length == 2)
2180 result = *((uint32_t *)xcb_get_property_value(r));
2184 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2190 version(struct swm_region *r, union arg *args)
2192 /* suppress unused warnings since vars are needed */
2196 bar_version = !bar_version;
2198 snprintf(bar_vertext, sizeof bar_vertext,
2199 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2201 strlcpy(bar_vertext, "", sizeof bar_vertext);
2208 client_msg(struct ws_win *win, xcb_atom_t a)
2210 xcb_client_message_event_t ev;
2215 bzero(&ev, sizeof ev);
2216 ev.response_type = XCB_CLIENT_MESSAGE;
2217 ev.window = win->id;
2220 ev.data.data32[0] = a;
2221 ev.data.data32[1] = XCB_CURRENT_TIME;
2223 xcb_send_event(conn, 0, win->id,
2224 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2227 /* synthetic response to a ConfigureRequest when not making a change */
2229 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2231 xcb_configure_notify_event_t ce;
2236 /* send notification of unchanged state. */
2237 bzero(&ce, sizeof(ce));
2238 ce.response_type = XCB_CONFIGURE_NOTIFY;
2241 ce.width = WIDTH(win);
2242 ce.height = HEIGHT(win);
2243 ce.override_redirect = 0;
2248 ce.window = win->id;
2249 ce.border_width = BORDER(win);
2250 ce.above_sibling = XCB_WINDOW_NONE;
2253 ce.event = ev->window;
2254 ce.window = ev->window;
2256 /* make response appear more WM_SIZE_HINTS-compliant */
2257 if (win->sh.flags) {
2258 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2259 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2260 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2261 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2262 SH_INC_W(win), SH_INC_H(win));
2267 /* the hint may be set... to 0! */
2268 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2269 ce.width = SH_MIN_W(win);
2270 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2271 ce.height = SH_MIN_H(win);
2276 /* may also be advertized as 0 */
2277 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2278 ce.width = SH_MAX_W(win);
2279 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2280 ce.height = SH_MAX_H(win);
2283 /* resize increment. */
2285 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2286 ce.width -= (ce.width - SH_MIN_W(win)) %
2288 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2289 ce.height -= (ce.height - SH_MIN_H(win)) %
2293 /* adjust x and y for requested border_width. */
2294 ce.x += BORDER(win) - ev->border_width;
2295 ce.y += BORDER(win) - ev->border_width;
2296 ce.border_width = ev->border_width;
2297 ce.above_sibling = ev->sibling;
2300 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2301 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2302 ce.y, ce.width, ce.height, ce.border_width);
2304 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2309 count_win(struct workspace *ws, int count_transient)
2314 TAILQ_FOREACH(win, &ws->winlist, entry) {
2315 if (count_transient == 0 && win->floating)
2317 if (count_transient == 0 && win->transient)
2323 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2329 quit(struct swm_region *r, union arg *args)
2331 /* suppress unused warnings since vars are needed */
2335 DNPRINTF(SWM_D_MISC, "quit\n");
2340 unmap_window(struct ws_win *win)
2342 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2347 /* don't unmap again */
2348 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2351 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2353 xcb_unmap_window(conn, win->id);
2354 xcb_change_window_attributes(conn, win->id,
2355 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2362 int i, j, num_screens;
2364 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2365 for (i = 0; i < num_screens; i++)
2366 for (j = 0; j < workspace_limit; j++)
2367 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2372 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2374 xcb_key_press_event_t event;
2375 xcb_keycode_t *keycode;
2380 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2382 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2385 bzero(&event, sizeof(event));
2386 event.event = win->id;
2387 event.root = win->s->root;
2388 event.child = XCB_WINDOW_NONE;
2389 event.time = XCB_CURRENT_TIME;
2390 event.event_x = X(win);
2391 event.event_y = Y(win);
2394 event.same_screen = 1;
2395 event.detail = *keycode;
2396 event.state = modifiers;
2398 event.response_type = XCB_KEY_PRESS;
2399 xcb_send_event(conn, 1, win->id,
2400 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2402 event.response_type = XCB_KEY_RELEASE;
2403 xcb_send_event(conn, 1, win->id,
2404 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2410 restart(struct swm_region *r, union arg *args)
2412 /* suppress unused warning since var is needed */
2416 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2420 execvp(start_argv[0], start_argv);
2421 warn("execvp failed");
2426 root_to_region(xcb_window_t root)
2428 struct swm_region *r = NULL;
2430 xcb_query_pointer_reply_t *qpr;
2432 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2434 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2435 for (i = 0; i < num_screens; i++)
2436 if (screens[i].root == root)
2439 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2440 screens[i].root), NULL);
2443 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2444 qpr->root_x, qpr->root_y);
2445 /* choose a region based on pointer location */
2446 TAILQ_FOREACH(r, &screens[i].rl, entry)
2447 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2448 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2454 r = TAILQ_FIRST(&screens[i].rl);
2460 find_unmanaged_window(xcb_window_t id)
2463 int i, j, num_screens;
2465 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2466 for (i = 0; i < num_screens; i++)
2467 for (j = 0; j < workspace_limit; j++)
2468 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2476 find_window(xcb_window_t id)
2479 int i, j, num_screens;
2480 xcb_query_tree_reply_t *r;
2482 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2483 for (i = 0; i < num_screens; i++)
2484 for (j = 0; j < workspace_limit; j++)
2485 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2489 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2493 /* if we were looking for the parent return that window instead */
2494 if (r->parent == 0 || r->root == r->parent) {
2499 /* look for parent */
2500 for (i = 0; i < num_screens; i++)
2501 for (j = 0; j < workspace_limit; j++)
2502 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2503 if (r->parent == win->id) {
2513 spawn(int ws_idx, union arg *args, int close_fd)
2518 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2520 close(xcb_get_file_descriptor(conn));
2522 setenv("LD_PRELOAD", SWM_LIB, 1);
2524 if (asprintf(&ret, "%d", ws_idx) == -1) {
2525 warn("spawn: asprintf SWM_WS");
2528 setenv("_SWM_WS", ret, 1);
2532 if (asprintf(&ret, "%d", getpid()) == -1) {
2533 warn("spawn: asprintf _SWM_PID");
2536 setenv("_SWM_PID", ret, 1);
2540 if (setsid() == -1) {
2541 warn("spawn: setsid");
2547 * close stdin and stdout to prevent interaction between apps
2548 * and the baraction script
2549 * leave stderr open to record errors
2551 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2552 warn("spawn: open");
2555 dup2(fd, STDIN_FILENO);
2556 dup2(fd, STDOUT_FILENO);
2561 execvp(args->argv[0], args->argv);
2563 warn("spawn: execvp");
2568 kill_refs(struct ws_win *win)
2570 int i, x, num_screens;
2571 struct swm_region *r;
2572 struct workspace *ws;
2577 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2578 for (i = 0; i < num_screens; i++)
2579 TAILQ_FOREACH(r, &screens[i].rl, entry)
2580 for (x = 0; x < workspace_limit; x++) {
2582 if (win == ws->focus)
2584 if (win == ws->focus_prev)
2585 ws->focus_prev = NULL;
2590 validate_win(struct ws_win *testwin)
2593 struct workspace *ws;
2594 struct swm_region *r;
2595 int i, x, num_screens;
2597 if (testwin == NULL)
2600 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2601 for (i = 0; i < num_screens; i++)
2602 TAILQ_FOREACH(r, &screens[i].rl, entry)
2603 for (x = 0; x < workspace_limit; x++) {
2605 TAILQ_FOREACH(win, &ws->winlist, entry)
2613 validate_ws(struct workspace *testws)
2615 struct swm_region *r;
2616 struct workspace *ws;
2617 int i, x, num_screens;
2619 /* validate all ws */
2620 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2621 for (i = 0; i < num_screens; i++)
2622 TAILQ_FOREACH(r, &screens[i].rl, entry)
2623 for (x = 0; x < workspace_limit; x++) {
2632 unfocus_win(struct ws_win *win)
2634 xcb_window_t none = XCB_WINDOW_NONE;
2636 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2640 if (win->ws == NULL)
2643 if (validate_ws(win->ws))
2644 return; /* XXX this gets hit with thunderbird, needs fixing */
2646 if (win->ws->r == NULL)
2649 if (validate_win(win)) {
2654 if (win->ws->focus == win) {
2655 win->ws->focus = NULL;
2656 win->ws->focus_prev = win;
2659 if (validate_win(win->ws->focus)) {
2660 kill_refs(win->ws->focus);
2661 win->ws->focus = NULL;
2663 if (validate_win(win->ws->focus_prev)) {
2664 kill_refs(win->ws->focus_prev);
2665 win->ws->focus_prev = NULL;
2668 grabbuttons(win, 0);
2669 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2670 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2671 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2672 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2679 int i, j, num_screens;
2681 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2683 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2684 for (i = 0; i < num_screens; i++)
2685 for (j = 0; j < workspace_limit; j++)
2686 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2691 focus_win(struct ws_win *win)
2693 struct ws_win *cfw = NULL;
2694 xcb_get_input_focus_reply_t *r;
2695 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2697 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2701 if (win->ws == NULL)
2704 if (validate_ws(win->ws))
2705 return; /* XXX this gets hit with thunderbird, needs fixing */
2707 if (validate_win(win)) {
2712 if (validate_win(win)) {
2717 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2719 cur_focus = r->focus;
2722 if ((cfw = find_window(cur_focus)) != NULL)
2726 /* use larger hammer since the window was killed somehow */
2727 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2728 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2729 xcb_change_window_attributes(conn, cfw->id,
2730 XCB_CW_BORDER_PIXEL,
2731 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2735 win->ws->focus = win;
2737 if (win->ws->r != NULL) {
2739 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2740 win->id, XCB_CURRENT_TIME);
2741 grabbuttons(win, 1);
2742 xcb_change_window_attributes(conn, win->id,
2743 XCB_CW_BORDER_PIXEL,
2744 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2745 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2746 win->ws->always_raise)
2747 map_window_raised(win->id);
2749 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2750 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2758 event_drain(uint8_t rt)
2760 xcb_generic_event_t *evt;
2763 while ((evt = xcb_poll_for_event(conn))) {
2764 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2772 switchws(struct swm_region *r, union arg *args)
2774 int wsid = args->id, unmap_old = 0;
2775 struct swm_region *this_r, *other_r;
2777 struct workspace *new_ws, *old_ws;
2783 if (wsid >= workspace_limit)
2787 old_ws = this_r->ws;
2788 new_ws = &this_r->s->ws[wsid];
2790 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2791 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2793 if (new_ws == NULL || old_ws == NULL)
2795 if (new_ws == old_ws)
2798 other_r = new_ws->r;
2799 if (other_r == NULL) {
2800 /* the other workspace is hidden, hide this one */
2804 /* the other ws is visible in another region, exchange them */
2805 other_r->ws_prior = new_ws;
2806 other_r->ws = old_ws;
2807 old_ws->r = other_r;
2809 this_r->ws_prior = old_ws;
2810 this_r->ws = new_ws;
2813 /* this is needed so that we can click on a window after a restart */
2817 a.id = SWM_ARG_ID_FOCUSCUR;
2818 focus(new_ws->r, &a);
2820 /* unmap old windows */
2822 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2825 if (focus_mode == SWM_FOCUS_DEFAULT)
2826 event_drain(XCB_ENTER_NOTIFY);
2830 DNPRINTF(SWM_D_WS, "switchws: done\n");
2834 cyclews(struct swm_region *r, union arg *args)
2837 struct swm_screen *s = r->s;
2840 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2841 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2846 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2849 case SWM_ARG_ID_CYCLEWS_UP:
2850 if (a.id < workspace_limit - 1)
2855 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2858 case SWM_ARG_ID_CYCLEWS_DOWN:
2862 a.id = workspace_limit - 1;
2869 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2871 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2875 } while (a.id != r->ws->idx);
2879 priorws(struct swm_region *r, union arg *args)
2885 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2886 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2888 if (r->ws_prior == NULL)
2891 a.id = r->ws_prior->idx;
2896 cyclescr(struct swm_region *r, union arg *args)
2898 struct swm_region *rr = NULL;
2900 int i, x, y, num_screens;
2902 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2903 /* do nothing if we don't have more than one screen */
2904 if (!(num_screens > 1 || outputs > 1))
2909 case SWM_ARG_ID_CYCLESC_UP:
2910 rr = TAILQ_NEXT(r, entry);
2912 rr = TAILQ_FIRST(&screens[i].rl);
2914 case SWM_ARG_ID_CYCLESC_DOWN:
2915 rr = TAILQ_PREV(r, swm_region_list, entry);
2917 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2925 /* move mouse to region */
2927 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2928 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2931 a.id = SWM_ARG_ID_FOCUSCUR;
2934 if (rr->ws->focus) {
2935 /* move to focus window */
2936 x = X(rr->ws->focus) + 1;
2937 y = Y(rr->ws->focus) + 1;
2938 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2944 sort_windows(struct ws_win_list *wl)
2946 struct ws_win *win, *parent, *nxt;
2951 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2952 nxt = TAILQ_NEXT(win, entry);
2953 if (win->transient) {
2954 parent = find_window(win->transient);
2955 if (parent == NULL) {
2956 warnx("not possible bug");
2959 TAILQ_REMOVE(wl, win, entry);
2960 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2967 swapwin(struct swm_region *r, union arg *args)
2969 struct ws_win *target, *source;
2970 struct ws_win *cur_focus;
2971 struct ws_win_list *wl;
2974 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2975 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2977 cur_focus = r->ws->focus;
2978 if (cur_focus == NULL)
2982 wl = &source->ws->winlist;
2985 case SWM_ARG_ID_SWAPPREV:
2986 if (source->transient)
2987 source = find_window(source->transient);
2988 target = TAILQ_PREV(source, ws_win_list, entry);
2989 if (target && target->transient)
2990 target = find_window(target->transient);
2991 TAILQ_REMOVE(wl, source, entry);
2993 TAILQ_INSERT_TAIL(wl, source, entry);
2995 TAILQ_INSERT_BEFORE(target, source, entry);
2997 case SWM_ARG_ID_SWAPNEXT:
2998 target = TAILQ_NEXT(source, entry);
2999 /* move the parent and let the sort handle the move */
3000 if (source->transient)
3001 source = find_window(source->transient);
3002 TAILQ_REMOVE(wl, source, entry);
3004 TAILQ_INSERT_HEAD(wl, source, entry);
3006 TAILQ_INSERT_AFTER(wl, target, source, entry);
3008 case SWM_ARG_ID_SWAPMAIN:
3009 target = TAILQ_FIRST(wl);
3010 if (target == source) {
3011 if (source->ws->focus_prev != NULL &&
3012 source->ws->focus_prev != target)
3013 source = source->ws->focus_prev;
3017 if (target == NULL || source == NULL)
3019 source->ws->focus_prev = target;
3020 TAILQ_REMOVE(wl, target, entry);
3021 TAILQ_INSERT_BEFORE(source, target, entry);
3022 TAILQ_REMOVE(wl, source, entry);
3023 TAILQ_INSERT_HEAD(wl, source, entry);
3025 case SWM_ARG_ID_MOVELAST:
3026 TAILQ_REMOVE(wl, source, entry);
3027 TAILQ_INSERT_TAIL(wl, source, entry);
3030 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3042 focus_prev(struct ws_win *win)
3044 struct ws_win *winfocus = NULL;
3045 struct ws_win *cur_focus = NULL;
3046 struct ws_win_list *wl = NULL;
3047 struct workspace *ws = NULL;
3049 if (!(win && win->ws))
3054 cur_focus = ws->focus;
3056 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3057 WINID(win), WINID(cur_focus));
3059 /* pickle, just focus on whatever */
3060 if (cur_focus == NULL) {
3061 /* use prev_focus if valid */
3062 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3063 find_window(WINID(ws->focus_prev)))
3064 winfocus = ws->focus_prev;
3068 /* if transient focus on parent */
3069 if (cur_focus->transient) {
3070 winfocus = find_window(cur_focus->transient);
3074 /* if in max_stack try harder */
3075 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3076 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3077 if (cur_focus != ws->focus_prev)
3078 winfocus = ws->focus_prev;
3080 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3085 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
3087 if (winfocus == NULL || winfocus == win) {
3088 switch (focus_close) {
3089 case SWM_STACK_BOTTOM:
3090 winfocus = TAILQ_FIRST(wl);
3093 winfocus = TAILQ_LAST(wl, ws_win_list);
3095 case SWM_STACK_ABOVE:
3096 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3097 if (focus_close_wrap)
3098 winfocus = TAILQ_FIRST(wl);
3100 winfocus = TAILQ_PREV(cur_focus,
3101 ws_win_list, entry);
3104 case SWM_STACK_BELOW:
3105 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3107 if (focus_close_wrap)
3108 winfocus = TAILQ_LAST(wl, ws_win_list);
3110 winfocus = TAILQ_NEXT(cur_focus, entry);
3116 if (winfocus == NULL) {
3117 if (focus_default == SWM_STACK_TOP)
3118 winfocus = TAILQ_LAST(wl, ws_win_list);
3120 winfocus = TAILQ_FIRST(wl);
3124 focus_magic(winfocus);
3128 focus(struct swm_region *r, union arg *args)
3130 struct ws_win *winfocus = NULL, *head;
3131 struct ws_win *cur_focus = NULL;
3132 struct ws_win_list *wl = NULL;
3133 struct workspace *ws = NULL;
3139 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3141 /* treat FOCUS_CUR special */
3142 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3143 if (r->ws->focus && r->ws->focus->iconic == 0)
3144 winfocus = r->ws->focus;
3145 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3146 winfocus = r->ws->focus_prev;
3148 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3149 if (winfocus->iconic == 0)
3152 focus_magic(winfocus);
3156 if ((cur_focus = r->ws->focus) == NULL)
3160 if (TAILQ_EMPTY(wl))
3162 /* make sure there is at least one uniconified window */
3164 TAILQ_FOREACH(winfocus, wl, entry)
3165 if (winfocus->iconic == 0) {
3173 case SWM_ARG_ID_FOCUSPREV:
3174 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3176 head = TAILQ_LAST(wl, ws_win_list);
3178 if (WINID(winfocus) == cur_focus->transient) {
3179 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3181 head = TAILQ_LAST(wl, ws_win_list);
3186 if (winfocus && winfocus->iconic) {
3187 while (winfocus != cur_focus) {
3188 if (winfocus == NULL)
3189 winfocus = TAILQ_LAST(wl, ws_win_list);
3190 if (winfocus->iconic == 0)
3192 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3198 case SWM_ARG_ID_FOCUSNEXT:
3199 head = TAILQ_NEXT(cur_focus, entry);
3201 head = TAILQ_FIRST(wl);
3205 if (winfocus && winfocus->iconic) {
3206 while (winfocus != cur_focus) {
3207 if (winfocus == NULL)
3208 winfocus = TAILQ_FIRST(wl);
3209 if (winfocus->iconic == 0)
3211 winfocus = TAILQ_NEXT(winfocus, entry);
3216 case SWM_ARG_ID_FOCUSMAIN:
3217 winfocus = TAILQ_FIRST(wl);
3218 if (winfocus == cur_focus)
3219 winfocus = cur_focus->ws->focus_prev;
3226 focus_magic(winfocus);
3232 cycle_layout(struct swm_region *r, union arg *args)
3234 struct workspace *ws = r->ws;
3237 /* suppress unused warning since var is needed */
3240 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3243 if (ws->cur_layout->l_stack == NULL)
3244 ws->cur_layout = &layouts[0];
3249 if (focus_mode == SWM_FOCUS_DEFAULT)
3250 event_drain(XCB_ENTER_NOTIFY);
3252 a.id = SWM_ARG_ID_FOCUSCUR;
3257 stack_config(struct swm_region *r, union arg *args)
3259 struct workspace *ws = r->ws;
3261 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3264 if (ws->cur_layout->l_config != NULL)
3265 ws->cur_layout->l_config(ws, args->id);
3267 if (args->id != SWM_ARG_ID_STACKINIT)
3274 struct swm_geometry g;
3275 struct swm_region *r;
3281 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3283 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3284 for (i = 0; i < num_screens; i++) {
3288 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3289 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3290 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3292 /* start with screen geometry, adjust for bar */
3294 g.w -= 2 * border_width;
3295 g.h -= 2 * border_width;
3301 r->ws->cur_layout->l_stack(r->ws, &g);
3302 r->ws->cur_layout->l_string(r->ws);
3303 /* save r so we can track region changes */
3310 if (focus_mode == SWM_FOCUS_DEFAULT)
3311 event_drain(XCB_ENTER_NOTIFY);
3313 DNPRINTF(SWM_D_STACK, "stack: end\n");
3317 store_float_geom(struct ws_win *win, struct swm_region *r)
3319 /* retain window geom and region geom */
3320 win->g_float = win->g;
3321 win->g_float.x -= X(r);
3322 win->g_float.y -= Y(r);
3323 win->g_floatvalid = 1;
3324 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3325 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3326 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3327 win->g_float.w, win->g_float.h);
3331 stack_floater(struct ws_win *win, struct swm_region *r)
3336 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3339 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3340 * geom on ws switches or return from max mode
3342 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3343 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3344 /* refloat at last floating relative position */
3345 win->g = win->g_float;
3350 win->floatmaxed = 0;
3353 * if set to fullscreen mode, configure window to maximum size.
3355 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3356 if (!win->g_floatvalid)
3357 store_float_geom(win, win->ws->r);
3359 win->g = win->ws->r->g;
3363 * remove border on fullscreen floater when in fullscreen mode or when
3364 * the quirk is present.
3366 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3367 ((win->quirks & SWM_Q_FULLSCREEN) &&
3368 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3369 if (win->bordered) {
3371 X(win) += border_width;
3372 Y(win) += border_width;
3374 } else if (!win->bordered) {
3376 X(win) -= border_width;
3377 Y(win) -= border_width;
3380 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3381 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3382 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3387 * floaters and transients are auto-centred unless moved
3390 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3391 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3394 /* keep window within region bounds */
3395 constrain_window(win, r, 0);
3401 * Send keystrokes to terminal to decrease/increase the font size as the
3402 * window size changes.
3405 adjust_font(struct ws_win *win)
3407 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3408 win->floating || win->transient)
3411 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3412 WIDTH(win) / win->sh.width_inc < term_width &&
3413 win->font_steps < SWM_MAX_FONT_STEPS) {
3414 win->font_size_boundary[win->font_steps] =
3415 (win->sh.width_inc * term_width) + win->sh.base_width;
3418 win->last_inc = win->sh.width_inc;
3419 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3420 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3421 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3424 win->last_inc = win->sh.width_inc;
3425 fake_keypress(win, XK_KP_Add, ShiftMask);
3429 #define SWAPXY(g) do { \
3431 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3432 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3435 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3437 struct swm_geometry win_g, r_g = *g;
3438 struct ws_win *win, *fs_win = NULL;
3439 int i, j, s, stacks;
3440 int w_inc = 1, h_inc, w_base = 1, h_base;
3441 int hrh, extra = 0, h_slice, last_h = 0;
3442 int split, colno, winno, mwin, msize, mscale;
3443 int remain, missing, v_slice, reconfigure;
3445 xcb_get_window_attributes_reply_t *war;
3447 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3448 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3450 winno = count_win(ws, 0);
3451 if (winno == 0 && count_win(ws, 1) == 0)
3454 TAILQ_FOREACH(win, &ws->winlist, entry)
3455 if (win->transient == 0 && win->floating == 0
3456 && win->iconic == 0)
3463 w_inc = win->sh.width_inc;
3464 w_base = win->sh.base_width;
3465 mwin = ws->l_state.horizontal_mwin;
3466 mscale = ws->l_state.horizontal_msize;
3467 stacks = ws->l_state.horizontal_stacks;
3470 w_inc = win->sh.height_inc;
3471 w_base = win->sh.base_height;
3472 mwin = ws->l_state.vertical_mwin;
3473 mscale = ws->l_state.vertical_msize;
3474 stacks = ws->l_state.vertical_stacks;
3478 if (stacks > winno - mwin)
3479 stacks = winno - mwin;
3483 h_slice = r_g.h / SWM_H_SLICE;
3484 if (mwin && winno > mwin) {
3485 v_slice = r_g.w / SWM_V_SLICE;
3489 win_g.w = v_slice * mscale;
3491 if (w_inc > 1 && w_inc < v_slice) {
3492 /* adjust for window's requested size increment */
3493 remain = (win_g.w - w_base) % w_inc;
3499 win_g.x += r_g.w - msize;
3502 colno = split = winno / stacks;
3503 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3504 2 * border_width) / stacks);
3506 hrh = r_g.h / colno;
3507 extra = r_g.h - (colno * hrh);
3508 win_g.h = hrh - 2 * border_width;
3510 /* stack all the tiled windows */
3511 i = j = 0, s = stacks;
3512 TAILQ_FOREACH(win, &ws->winlist, entry) {
3513 if (win->transient != 0 || win->floating != 0)
3515 if (win->iconic != 0)
3518 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3523 if (split && i == split) {
3524 colno = (winno - mwin) / stacks;
3525 if (s <= (winno - mwin) % stacks)
3527 split = split + colno;
3528 hrh = (r_g.h / colno);
3529 extra = r_g.h - (colno * hrh);
3533 win_g.x += win_g.w + 2 * border_width;
3534 win_g.w = (r_g.w - msize -
3535 (stacks * 2 * border_width)) / stacks;
3537 win_g.w += (r_g.w - msize -
3538 (stacks * 2 * border_width)) % stacks;
3542 win_g.h = hrh - 2 * border_width;
3544 h_inc = win->sh.width_inc;
3545 h_base = win->sh.base_width;
3547 h_inc = win->sh.height_inc;
3548 h_base = win->sh.base_height;
3550 if (j == colno - 1) {
3551 win_g.h = hrh + extra;
3552 } else if (h_inc > 1 && h_inc < h_slice) {
3553 /* adjust for window's requested size increment */
3554 remain = (win_g.h - h_base) % h_inc;
3555 missing = h_inc - remain;
3557 if (missing <= extra || j == 0) {
3569 win_g.y += last_h + 2 * border_width;
3571 if (disable_border && bar_enabled == 0 && winno == 1){
3573 win_g.w += 2 * border_width;
3574 win_g.h += 2 * border_width;
3579 if (X(win) != win_g.y || Y(win) != win_g.x ||
3580 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3584 WIDTH(win) = win_g.h;
3585 HEIGHT(win) = win_g.w;
3588 if (X(win) != win_g.x || Y(win) != win_g.y ||
3589 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3593 WIDTH(win) = win_g.w;
3594 HEIGHT(win) = win_g.h;
3598 if (bordered != win->bordered) {
3600 win->bordered = bordered;
3608 war = xcb_get_window_attributes_reply(conn,
3609 xcb_get_window_attributes(conn, win->id),
3612 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3613 map_window_raised(win->id);
3623 /* now, stack all the floaters and transients */
3624 TAILQ_FOREACH(win, &ws->winlist, entry) {
3625 if (win->transient == 0 && win->floating == 0)
3627 if (win->iconic == 1)
3629 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3634 stack_floater(win, ws->r);
3635 map_window_raised(win->id);
3639 stack_floater(fs_win, ws->r);
3640 map_window_raised(fs_win->id);
3645 vertical_config(struct workspace *ws, int id)
3647 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3651 case SWM_ARG_ID_STACKRESET:
3652 case SWM_ARG_ID_STACKINIT:
3653 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3654 ws->l_state.vertical_mwin = 1;
3655 ws->l_state.vertical_stacks = 1;
3657 case SWM_ARG_ID_MASTERSHRINK:
3658 if (ws->l_state.vertical_msize > 1)
3659 ws->l_state.vertical_msize--;
3661 case SWM_ARG_ID_MASTERGROW:
3662 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3663 ws->l_state.vertical_msize++;
3665 case SWM_ARG_ID_MASTERADD:
3666 ws->l_state.vertical_mwin++;
3668 case SWM_ARG_ID_MASTERDEL:
3669 if (ws->l_state.vertical_mwin > 0)
3670 ws->l_state.vertical_mwin--;
3672 case SWM_ARG_ID_STACKINC:
3673 ws->l_state.vertical_stacks++;
3675 case SWM_ARG_ID_STACKDEC:
3676 if (ws->l_state.vertical_stacks > 1)
3677 ws->l_state.vertical_stacks--;
3679 case SWM_ARG_ID_FLIPLAYOUT:
3680 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3688 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3690 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3692 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3696 horizontal_config(struct workspace *ws, int id)
3698 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3701 case SWM_ARG_ID_STACKRESET:
3702 case SWM_ARG_ID_STACKINIT:
3703 ws->l_state.horizontal_mwin = 1;
3704 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3705 ws->l_state.horizontal_stacks = 1;
3707 case SWM_ARG_ID_MASTERSHRINK:
3708 if (ws->l_state.horizontal_msize > 1)
3709 ws->l_state.horizontal_msize--;
3711 case SWM_ARG_ID_MASTERGROW:
3712 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3713 ws->l_state.horizontal_msize++;
3715 case SWM_ARG_ID_MASTERADD:
3716 ws->l_state.horizontal_mwin++;
3718 case SWM_ARG_ID_MASTERDEL:
3719 if (ws->l_state.horizontal_mwin > 0)
3720 ws->l_state.horizontal_mwin--;
3722 case SWM_ARG_ID_STACKINC:
3723 ws->l_state.horizontal_stacks++;
3725 case SWM_ARG_ID_STACKDEC:
3726 if (ws->l_state.horizontal_stacks > 1)
3727 ws->l_state.horizontal_stacks--;
3729 case SWM_ARG_ID_FLIPLAYOUT:
3730 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3738 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3740 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3742 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3745 /* fullscreen view */
3747 max_stack(struct workspace *ws, struct swm_geometry *g)
3749 struct swm_geometry gg = *g;
3750 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3751 int winno, num_screens;
3753 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3758 winno = count_win(ws, 0);
3759 if (winno == 0 && count_win(ws, 1) == 0)
3762 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3763 TAILQ_FOREACH(win, &ws->winlist, entry) {
3764 if (win->transient) {
3766 parent = find_window(win->transient);
3770 if (win->floating && win->floatmaxed == 0 ) {
3772 * retain geometry for retrieval on exit from
3775 store_float_geom(win, ws->r);
3776 win->floatmaxed = 1;
3779 /* only reconfigure if necessary */
3780 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3781 HEIGHT(win) != gg.h) {
3787 WIDTH(win) += 2 * border_width;
3788 HEIGHT(win) += 2 * border_width;
3793 /* unmap only if we don't have multi screen */
3794 if (win != ws->focus)
3795 if (!(num_screens > 1 || outputs > 1))
3799 /* put the last transient on top */
3802 map_window_raised(parent->id);
3803 stack_floater(wintrans, ws->r);
3804 focus_magic(wintrans);
3809 send_to_ws(struct swm_region *r, union arg *args)
3811 int wsid = args->id;
3812 struct ws_win *win = NULL, *parent;
3813 struct workspace *ws, *nws;
3814 char ws_idx_str[SWM_PROPLEN];
3817 if (wsid >= workspace_limit)
3820 if (r && r->ws && r->ws->focus)
3826 if (win->ws->idx == wsid)
3829 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3832 nws = &win->s->ws[wsid];
3834 a.id = SWM_ARG_ID_FOCUSPREV;
3836 if (win->transient) {
3837 parent = find_window(win->transient);
3839 unmap_window(parent);
3840 TAILQ_REMOVE(&ws->winlist, parent, entry);
3841 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3846 TAILQ_REMOVE(&ws->winlist, win, entry);
3847 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3848 if (TAILQ_EMPTY(&ws->winlist))
3849 r->ws->focus = NULL;
3852 /* Try to update the window's workspace property */
3853 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3854 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3856 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3857 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3868 pressbutton(struct swm_region *r, union arg *args)
3870 /* suppress unused warning since var is needed */
3873 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3874 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3875 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3876 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3880 raise_toggle(struct swm_region *r, union arg *args)
3882 /* suppress unused warning since var is needed */
3885 if (r == NULL || r->ws == NULL)
3888 r->ws->always_raise = !r->ws->always_raise;
3890 /* bring floaters back to top */
3891 if (r->ws->always_raise == 0)
3898 iconify(struct swm_region *r, union arg *args)
3902 /* suppress unused warning since var is needed */
3905 if (r->ws->focus == NULL)
3907 unmap_window(r->ws->focus);
3908 update_iconic(r->ws->focus, 1);
3911 r->ws->focus = NULL;
3912 a.id = SWM_ARG_ID_FOCUSCUR;
3919 get_win_name(xcb_window_t win)
3922 xcb_get_property_cookie_t c;
3923 xcb_get_property_reply_t *r;
3925 /* First try _NET_WM_NAME for UTF-8. */
3926 c = xcb_get_property(conn, 0, win, a_netwmname,
3927 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
3928 r = xcb_get_property_reply(conn, c, NULL);
3930 if (!r || r->type == XCB_NONE) {
3932 /* Use WM_NAME instead; no UTF-8. */
3933 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
3934 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
3935 r = xcb_get_property_reply(conn, c, NULL);
3937 if(!r || r->type == XCB_NONE) {
3944 name = strndup(xcb_get_property_value(r),
3945 xcb_get_property_value_length(r));
3952 uniconify(struct swm_region *r, union arg *args)
3959 DNPRINTF(SWM_D_MISC, "uniconify\n");
3961 if (r == NULL || r->ws == NULL)
3964 /* make sure we have anything to uniconify */
3965 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3966 if (win->ws == NULL)
3967 continue; /* should never happen */
3968 if (win->iconic == 0)
3976 search_resp_action = SWM_SEARCH_UNICONIFY;
3978 spawn_select(r, args, "search", &searchpid);
3980 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3983 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3984 if (win->ws == NULL)
3985 continue; /* should never happen */
3986 if (win->iconic == 0)
3989 name = get_win_name(win->id);
3992 fprintf(lfile, "%s.%u\n", name, win->id);
4002 name_workspace(struct swm_region *r, union arg *args)
4006 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4012 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4014 spawn_select(r, args, "name_workspace", &searchpid);
4016 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4019 fprintf(lfile, "%s", "");
4024 search_workspace(struct swm_region *r, union arg *args)
4027 struct workspace *ws;
4030 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4036 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4038 spawn_select(r, args, "search", &searchpid);
4040 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4043 for (i = 0; i < workspace_limit; i++) {
4047 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4048 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4055 search_win_cleanup(void)
4057 struct search_window *sw = NULL;
4059 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4060 xcb_destroy_window(conn, sw->indicator);
4061 TAILQ_REMOVE(&search_wl, sw, entry);
4067 search_win(struct swm_region *r, union arg *args)
4069 struct ws_win *win = NULL;
4070 struct search_window *sw = NULL;
4073 int i, width, height;
4081 XRectangle l_ibox, l_lbox;
4083 DNPRINTF(SWM_D_MISC, "search_win\n");
4086 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4088 spawn_select(r, args, "search", &searchpid);
4090 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4093 TAILQ_INIT(&search_wl);
4096 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4097 if (win->iconic == 1)
4100 sw = calloc(1, sizeof(struct search_window));
4102 warn("search_win: calloc");
4104 search_win_cleanup();
4110 snprintf(s, sizeof s, "%d", i);
4113 w = xcb_generate_id(conn);
4114 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4115 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4117 if (bar_font_legacy) {
4118 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4119 width = l_lbox.width + 4;
4120 height = bar_fs_extents->max_logical_extent.height + 4;
4122 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4124 width = info.width + 4;
4125 height = bar_font->height + 4;
4128 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4129 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4130 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4131 XCB_CW_BORDER_PIXEL, wa);
4133 map_window_raised(w);
4136 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4138 if (bar_font_legacy) {
4139 l_gcv.graphics_exposures = 0;
4140 l_draw = XCreateGC(display, w, 0, &l_gcv);
4142 XSetForeground(display, l_draw,
4143 r->s->c[SWM_S_COLOR_BAR].pixel);
4145 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4146 (bar_fs_extents->max_logical_extent.height -
4147 l_lbox.height) / 2 - l_lbox.y, s, len);
4149 XFreeGC(display, l_draw);
4152 draw = XftDrawCreate(display, w,
4153 DefaultVisual(display, r->s->idx),
4154 DefaultColormap(display, r->s->idx));
4156 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4157 (HEIGHT(r->bar) + bar_font->height) / 2 -
4158 bar_font->descent, (FcChar8 *)s, len);
4160 XftDrawDestroy(draw);
4163 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4165 fprintf(lfile, "%d\n", i);
4175 search_resp_uniconify(char *resp, unsigned long len)
4181 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4183 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4184 if (win->iconic == 0)
4186 name = get_win_name(win->id);
4189 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4194 if (strncmp(s, resp, len) == 0) {
4195 /* XXX this should be a callback to generalize */
4196 update_iconic(win, 0);
4205 search_resp_name_workspace(char *resp, unsigned long len)
4207 struct workspace *ws;
4209 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4211 if (search_r->ws == NULL)
4216 free(search_r->ws->name);
4217 search_r->ws->name = NULL;
4221 ws->name = strdup(resp);
4222 if (ws->name == NULL) {
4223 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4224 "strdup: %s", strerror(errno));
4231 search_resp_search_workspace(char *resp)
4238 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4242 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4249 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4251 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4258 switchws(search_r, &a);
4262 search_resp_search_window(char *resp)
4267 struct search_window *sw;
4269 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4273 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4278 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4280 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4287 TAILQ_FOREACH(sw, &search_wl, entry)
4288 if (idx == sw->idx) {
4294 #define MAX_RESP_LEN 1024
4297 search_do_resp(void)
4303 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4308 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4309 warn("search: calloc");
4313 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4315 warn("search: read error");
4318 resp[rbytes] = '\0';
4321 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4322 * newline, so work around that by sanitizing the resp now.
4324 resp[strcspn(resp, "\n")] = '\0';
4327 switch (search_resp_action) {
4328 case SWM_SEARCH_UNICONIFY:
4329 search_resp_uniconify(resp, len);
4331 case SWM_SEARCH_NAME_WORKSPACE:
4332 search_resp_name_workspace(resp, len);
4334 case SWM_SEARCH_SEARCH_WORKSPACE:
4335 search_resp_search_workspace(resp);
4337 case SWM_SEARCH_SEARCH_WINDOW:
4338 search_resp_search_window(resp);
4343 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4344 search_win_cleanup();
4346 search_resp_action = SWM_SEARCH_NONE;
4347 close(select_resp_pipe[0]);
4352 wkill(struct swm_region *r, union arg *args)
4354 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4356 if (r->ws->focus == NULL)
4359 if (args->id == SWM_ARG_ID_KILLWINDOW)
4360 xcb_kill_client(conn, r->ws->focus->id);
4362 if (r->ws->focus->can_delete)
4363 client_msg(r->ws->focus, a_delete);
4370 floating_toggle_win(struct ws_win *win)
4372 struct swm_region *r;
4382 /* reject floating toggles in max stack mode */
4383 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4386 if (win->floating) {
4387 if (!win->floatmaxed) {
4388 /* retain position for refloat */
4389 store_float_geom(win, r);
4393 if (win->g_floatvalid) {
4394 /* refloat at last floating relative position */
4395 X(win) = win->g_float.x + X(r);
4396 Y(win) = win->g_float.y + Y(r);
4397 WIDTH(win) = win->g_float.w;
4398 HEIGHT(win) = win->g_float.h;
4403 ewmh_update_actions(win);
4409 floating_toggle(struct swm_region *r, union arg *args)
4411 struct ws_win *win = r->ws->focus;
4414 /* suppress unused warning since var is needed */
4420 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4421 _NET_WM_STATE_TOGGLE);
4425 if (win == win->ws->focus) {
4426 a.id = SWM_ARG_ID_FOCUSCUR;
4427 focus(win->ws->r, &a);
4431 event_drain(XCB_ENTER_NOTIFY);
4435 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4437 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4439 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4441 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4444 if (X(win) + BORDER(win) < X(r)) {
4446 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4448 X(win) = X(r) - BORDER(win);
4451 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4453 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4455 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4458 if (Y(win) + BORDER(win) < Y(r)) {
4460 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4462 Y(win) = Y(r) - BORDER(win);
4468 if (HEIGHT(win) < 1)
4474 update_window(struct ws_win *win)
4479 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4480 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4481 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4485 wc[3] = HEIGHT(win);
4486 wc[4] = BORDER(win);
4488 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4489 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4490 wc[3], YESNO(win->bordered));
4492 xcb_configure_window(conn, win->id, mask, wc);
4495 #define SWM_RESIZE_STEPS (50)
4498 resize(struct ws_win *win, union arg *args)
4500 xcb_timestamp_t timestamp = 0;
4501 struct swm_region *r = NULL;
4502 int resize_step = 0;
4503 struct swm_geometry g;
4504 int top = 0, left = 0, resizing;
4506 unsigned int shape; /* cursor style */
4507 xcb_cursor_t cursor;
4508 xcb_font_t cursor_font;
4509 xcb_query_pointer_reply_t *xpr;
4510 xcb_generic_event_t *evt;
4511 xcb_motion_notify_event_t *mne;
4517 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4520 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4521 "transient: 0x%x\n", win->id, YESNO(win->floating),
4524 if (!(win->transient != 0 || win->floating != 0))
4527 /* reject resizes in max mode for floaters (transient ok) */
4528 if (win->floatmaxed)
4532 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4538 case SWM_ARG_ID_WIDTHSHRINK:
4539 WIDTH(win) -= SWM_RESIZE_STEPS;
4542 case SWM_ARG_ID_WIDTHGROW:
4543 WIDTH(win) += SWM_RESIZE_STEPS;
4546 case SWM_ARG_ID_HEIGHTSHRINK:
4547 HEIGHT(win) -= SWM_RESIZE_STEPS;
4550 case SWM_ARG_ID_HEIGHTGROW:
4551 HEIGHT(win) += SWM_RESIZE_STEPS;
4558 constrain_window(win, r, 1);
4560 store_float_geom(win,r);
4564 /* get cursor offset from window root */
4565 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4572 if (xpr->win_x < WIDTH(win) / 2)
4575 if (xpr->win_y < HEIGHT(win) / 2)
4578 if (args->id == SWM_ARG_ID_CENTER)
4581 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4583 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4585 cursor_font = xcb_generate_id(conn);
4586 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4588 cursor = xcb_generate_id(conn);
4589 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4590 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4592 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4593 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4598 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4599 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4600 case XCB_BUTTON_RELEASE:
4601 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4604 case XCB_MOTION_NOTIFY:
4605 mne = (xcb_motion_notify_event_t *)evt;
4606 /* cursor offset/delta from start of the operation */
4607 dx = mne->root_x - xpr->root_x;
4608 dy = mne->root_y - xpr->root_y;
4614 if (args->id == SWM_ARG_ID_CENTER) {
4615 if (g.h / 2 + dy < 1)
4619 HEIGHT(win) = g.h + 2 * dy;
4627 HEIGHT(win) = g.h + dy;
4634 if (args->id == SWM_ARG_ID_CENTER) {
4635 if (g.w / 2 + dx < 1)
4639 WIDTH(win) = g.w + 2 * dx;
4647 WIDTH(win) = g.w + dx;
4650 constrain_window(win, r, 1);
4652 /* not free, don't sync more than 120 times / second */
4653 if ((mne->time - timestamp) > (1000 / 120) ) {
4654 timestamp = mne->time;
4669 store_float_geom(win,r);
4671 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4672 xcb_free_cursor(conn, cursor);
4673 xcb_close_font(conn, cursor_font);
4675 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4679 resize_step(struct swm_region *r, union arg *args)
4681 struct ws_win *win = NULL;
4683 if (r && r->ws && r->ws->focus)
4691 #define SWM_MOVE_STEPS (50)
4694 move(struct ws_win *win, union arg *args)
4696 xcb_timestamp_t timestamp = 0;
4697 int move_step = 0, moving;
4698 struct swm_region *r = NULL;
4699 xcb_font_t cursor_font;
4700 xcb_cursor_t cursor;
4701 xcb_query_pointer_reply_t *qpr;
4702 xcb_generic_event_t *evt;
4703 xcb_motion_notify_event_t *mne;
4709 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4712 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4713 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4715 /* in max_stack mode should only move transients */
4716 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4720 if (win->floating == 0 && !win->transient) {
4721 store_float_geom(win, r);
4722 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4725 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4732 case SWM_ARG_ID_MOVELEFT:
4733 X(win) -= (SWM_MOVE_STEPS - border_width);
4736 case SWM_ARG_ID_MOVERIGHT:
4737 X(win) += (SWM_MOVE_STEPS - border_width);
4740 case SWM_ARG_ID_MOVEUP:
4741 Y(win) -= (SWM_MOVE_STEPS - border_width);
4744 case SWM_ARG_ID_MOVEDOWN:
4745 Y(win) += (SWM_MOVE_STEPS - border_width);
4752 constrain_window(win, r, 0);
4754 store_float_geom(win, r);
4758 cursor_font = xcb_generate_id(conn);
4759 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4761 cursor = xcb_generate_id(conn);
4762 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4763 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4765 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4766 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4767 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4769 /* get cursor offset from window root */
4770 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4773 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4774 xcb_free_cursor(conn, cursor);
4775 xcb_close_font(conn, cursor_font);
4781 while ((evt = xcb_wait_for_event(conn)) && moving) {
4782 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4783 case XCB_BUTTON_RELEASE:
4784 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4787 case XCB_MOTION_NOTIFY:
4788 mne = (xcb_motion_notify_event_t *)evt;
4789 X(win) = mne->root_x - qpr->win_x - border_width;
4790 Y(win) = mne->root_y - qpr->win_y - border_width;
4792 constrain_window(win, r, 0);
4794 /* not free, don't sync more than 120 times / second */
4795 if ((mne->time - timestamp) > (1000 / 120) ) {
4796 timestamp = mne->time;
4811 store_float_geom(win, r);
4813 xcb_free_cursor(conn, cursor);
4814 xcb_close_font(conn, cursor_font);
4815 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4816 DNPRINTF(SWM_D_EVENT, "move: done\n");
4820 move_step(struct swm_region *r, union arg *args)
4822 struct ws_win *win = NULL;
4824 if (r && r->ws && r->ws->focus)
4829 if (!(win->transient != 0 || win->floating != 0))
4836 /* user/key callable function IDs */
4886 KF_SEARCH_WORKSPACE,
4927 KF_DUMPWINS, /* MUST BE LAST */
4931 /* key definitions */
4933 char name[SWM_FUNCNAME_LEN];
4934 void (*func)(struct swm_region *r, union arg *);
4936 } keyfuncs[KF_INVALID + 1] = {
4937 /* name function argument */
4938 { "bar_toggle", bar_toggle, {0} },
4939 { "button2", pressbutton, {2} },
4940 { "cycle_layout", cycle_layout, {0} },
4941 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4942 { "float_toggle", floating_toggle,{0} },
4943 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4944 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4945 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4946 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4947 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4948 { "iconify", iconify, {0} },
4949 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4950 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4951 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4952 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4953 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4954 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4955 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4956 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4957 { "mvws_1", send_to_ws, {.id = 0} },
4958 { "mvws_2", send_to_ws, {.id = 1} },
4959 { "mvws_3", send_to_ws, {.id = 2} },
4960 { "mvws_4", send_to_ws, {.id = 3} },
4961 { "mvws_5", send_to_ws, {.id = 4} },
4962 { "mvws_6", send_to_ws, {.id = 5} },
4963 { "mvws_7", send_to_ws, {.id = 6} },
4964 { "mvws_8", send_to_ws, {.id = 7} },
4965 { "mvws_9", send_to_ws, {.id = 8} },
4966 { "mvws_10", send_to_ws, {.id = 9} },
4967 { "mvws_11", send_to_ws, {.id = 10} },
4968 { "mvws_12", send_to_ws, {.id = 11} },
4969 { "mvws_13", send_to_ws, {.id = 12} },
4970 { "mvws_14", send_to_ws, {.id = 13} },
4971 { "mvws_15", send_to_ws, {.id = 14} },
4972 { "mvws_16", send_to_ws, {.id = 15} },
4973 { "mvws_17", send_to_ws, {.id = 16} },
4974 { "mvws_18", send_to_ws, {.id = 17} },
4975 { "mvws_19", send_to_ws, {.id = 18} },
4976 { "mvws_20", send_to_ws, {.id = 19} },
4977 { "mvws_21", send_to_ws, {.id = 20} },
4978 { "mvws_22", send_to_ws, {.id = 21} },
4979 { "name_workspace", name_workspace, {0} },
4980 { "quit", quit, {0} },
4981 { "raise_toggle", raise_toggle, {0} },
4982 { "restart", restart, {0} },
4983 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4984 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4985 { "search_win", search_win, {0} },
4986 { "search_workspace", search_workspace, {0} },
4987 { "spawn_custom", NULL, {0} },
4988 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4989 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4990 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4991 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4992 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4993 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4994 { "uniconify", uniconify, {0} },
4995 { "version", version, {0} },
4996 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4997 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4998 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4999 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5000 { "ws_1", switchws, {.id = 0} },
5001 { "ws_2", switchws, {.id = 1} },
5002 { "ws_3", switchws, {.id = 2} },
5003 { "ws_4", switchws, {.id = 3} },
5004 { "ws_5", switchws, {.id = 4} },
5005 { "ws_6", switchws, {.id = 5} },
5006 { "ws_7", switchws, {.id = 6} },
5007 { "ws_8", switchws, {.id = 7} },
5008 { "ws_9", switchws, {.id = 8} },
5009 { "ws_10", switchws, {.id = 9} },
5010 { "ws_11", switchws, {.id = 10} },
5011 { "ws_12", switchws, {.id = 11} },
5012 { "ws_13", switchws, {.id = 12} },
5013 { "ws_14", switchws, {.id = 13} },
5014 { "ws_15", switchws, {.id = 14} },
5015 { "ws_16", switchws, {.id = 15} },
5016 { "ws_17", switchws, {.id = 16} },
5017 { "ws_18", switchws, {.id = 17} },
5018 { "ws_19", switchws, {.id = 18} },
5019 { "ws_20", switchws, {.id = 19} },
5020 { "ws_21", switchws, {.id = 20} },
5021 { "ws_22", switchws, {.id = 21} },
5022 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5023 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5024 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5025 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5026 { "ws_prior", priorws, {0} },
5027 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5028 { "invalid key func", NULL, {0} },
5031 RB_ENTRY(key) entry;
5034 enum keyfuncid funcid;
5037 RB_HEAD(key_tree, key);
5040 key_cmp(struct key *kp1, struct key *kp2)
5042 if (kp1->keysym < kp2->keysym)
5044 if (kp1->keysym > kp2->keysym)
5047 if (kp1->mod < kp2->mod)
5049 if (kp1->mod > kp2->mod)
5055 RB_GENERATE(key_tree, key, entry, key_cmp);
5056 struct key_tree keys;
5059 enum { client_click, root_click };
5061 unsigned int action;
5063 unsigned int button;
5064 void (*func)(struct ws_win *, union arg *);
5067 /* action key mouse button func args */
5068 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5069 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
5070 { client_click, MODKEY, Button1, move, {0} },
5074 update_modkey(unsigned int mod)
5080 RB_FOREACH(kp, key_tree, &keys)
5081 if (kp->mod & ShiftMask)
5082 kp->mod = mod | ShiftMask;
5086 for (i = 0; i < LENGTH(buttons); i++)
5087 if (buttons[i].mask & ShiftMask)
5088 buttons[i].mask = mod | ShiftMask;
5090 buttons[i].mask = mod;
5095 TAILQ_ENTRY(spawn_prog) entry;
5100 TAILQ_HEAD(spawn_list, spawn_prog);
5101 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
5104 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
5107 struct spawn_prog *prog = NULL;
5109 char *ap, **real_args;
5111 /* suppress unused warning since var is needed */
5114 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5117 TAILQ_FOREACH(prog, &spawns, entry) {
5118 if (!strcasecmp(spawn_name, prog->name))
5122 warnx("spawn_custom: program %s not found", spawn_name);
5126 /* make room for expanded args */
5127 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5128 err(1, "spawn_custom: calloc real_args");
5130 /* expand spawn_args into real_args */
5131 for (i = 0; i < prog->argc; i++) {
5133 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5134 if (!strcasecmp(ap, "$bar_border")) {
5136 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5138 err(1, "spawn_custom border color");
5139 } else if (!strcasecmp(ap, "$bar_color")) {
5141 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5143 err(1, "spawn_custom bar color");
5144 } else if (!strcasecmp(ap, "$bar_font")) {
5145 if ((real_args[i] = strdup(bar_fonts))
5147 err(1, "spawn_custom bar fonts");
5148 } else if (!strcasecmp(ap, "$bar_font_color")) {
5150 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5152 err(1, "spawn_custom color font");
5153 } else if (!strcasecmp(ap, "$color_focus")) {
5155 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5157 err(1, "spawn_custom color focus");
5158 } else if (!strcasecmp(ap, "$color_unfocus")) {
5160 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5162 err(1, "spawn_custom color unfocus");
5164 /* no match --> copy as is */
5165 if ((real_args[i] = strdup(ap)) == NULL)
5166 err(1, "spawn_custom strdup(ap)");
5168 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5173 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5174 for (i = 0; i < prog->argc; i++)
5175 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5176 DNPRINTF(SWM_D_SPAWN, "\n");
5178 *ret_args = real_args;
5179 return (prog->argc);
5183 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5189 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5193 spawn(r->ws->idx, &a, 1);
5195 for (i = 0; i < spawn_argc; i++)
5201 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5207 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5211 if (pipe(select_list_pipe) == -1)
5212 err(1, "pipe error");
5213 if (pipe(select_resp_pipe) == -1)
5214 err(1, "pipe error");
5216 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5217 err(1, "could not disable SIGPIPE");
5218 switch (*pid = fork()) {
5220 err(1, "cannot fork");
5223 if (dup2(select_list_pipe[0], 0) == -1)
5225 if (dup2(select_resp_pipe[1], 1) == -1)
5227 close(select_list_pipe[1]);
5228 close(select_resp_pipe[0]);
5229 spawn(r->ws->idx, &a, 0);
5231 default: /* parent */
5232 close(select_list_pipe[0]);
5233 close(select_resp_pipe[1]);
5237 for (i = 0; i < spawn_argc; i++)
5243 spawn_insert(char *name, char *args)
5245 char *arg, *cp, *ptr;
5246 struct spawn_prog *sp;
5248 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5250 if ((sp = calloc(1, sizeof *sp)) == NULL)
5251 err(1, "spawn_insert: calloc");
5252 if ((sp->name = strdup(name)) == NULL)
5253 err(1, "spawn_insert: strdup");
5255 /* convert the arguments to an argument list */
5256 if ((ptr = cp = strdup(args)) == NULL)
5257 err(1, "spawn_insert: strdup");
5258 while ((arg = strsep(&ptr, " \t")) != NULL) {
5259 /* empty field; skip it */
5264 if ((sp->argv = realloc(sp->argv, sp->argc *
5265 sizeof *sp->argv)) == NULL)
5266 err(1, "spawn_insert: realloc");
5267 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5268 err(1, "spawn_insert: strdup");
5272 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5273 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5277 spawn_remove(struct spawn_prog *sp)
5281 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5283 TAILQ_REMOVE(&spawns, sp, entry);
5284 for (i = 0; i < sp->argc; i++)
5290 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5294 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5296 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5299 spawn_insert(name, args);
5301 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5305 setspawn(char *name, char *args)
5307 struct spawn_prog *sp;
5309 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5314 TAILQ_FOREACH(sp, &spawns, entry) {
5315 if (!strcmp(sp->name, name)) {
5319 spawn_replace(sp, name, args);
5320 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5324 if (*args == '\0') {
5325 warnx("error: setspawn: cannot find program: %s", name);
5329 spawn_insert(name, args);
5330 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5334 setconfspawn(char *selector, char *value, int flags)
5338 /* suppress unused warning since var is needed */
5341 args = expand_tilde(value);
5343 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5345 setspawn(selector, args);
5348 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5355 setconfspawn("term", "xterm", 0);
5356 setconfspawn("spawn_term", "xterm", 0);
5357 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5358 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5359 setconfspawn("lock", "xlock", 0);
5360 setconfspawn("initscr", "initscreen.sh", 0);
5361 setconfspawn("menu", "dmenu_run"
5364 " -nf $bar_font_color"
5366 " -sf $bar_color", 0);
5367 setconfspawn("search", "dmenu"
5371 " -nf $bar_font_color"
5373 " -sf $bar_color", 0);
5374 setconfspawn("name_workspace", "dmenu"
5378 " -nf $bar_font_color"
5380 " -sf $bar_color", 0);
5384 #define SWM_MODNAME_SIZE 32
5385 #define SWM_KEY_WS "\n+ \t"
5387 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5391 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5392 if (mod == NULL || ks == NULL) {
5393 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5396 if (keystr == NULL || strlen(keystr) == 0) {
5397 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5401 *ks = XCB_NO_SYMBOL;
5403 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5404 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5406 cp += (long)strspn(cp, SWM_KEY_WS);
5407 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5409 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5411 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5413 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5415 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5417 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5419 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5420 *mod |= ControlMask;
5422 *ks = XStringToKeysym(name);
5423 XConvertCase(*ks, ks, &uks);
5424 if (ks == XCB_NO_SYMBOL) {
5426 "parsekeys: invalid key %s\n",
5432 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5437 strdupsafe(char *str)
5442 return (strdup(str));
5446 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5450 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5451 keyfuncs[kfid].name, spawn_name);
5453 if ((kp = malloc(sizeof *kp)) == NULL)
5454 err(1, "key_insert: malloc");
5459 kp->spawn_name = strdupsafe(spawn_name);
5460 RB_INSERT(key_tree, &keys, kp);
5462 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5466 key_lookup(unsigned int mod, KeySym ks)
5473 return (RB_FIND(key_tree, &keys, &kp));
5477 key_remove(struct key *kp)
5479 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5481 RB_REMOVE(key_tree, &keys, kp);
5482 free(kp->spawn_name);
5485 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5489 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5492 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5496 key_insert(mod, ks, kfid, spawn_name);
5498 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5502 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5507 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5508 keyfuncs[kfid].name, spawn_name);
5510 if ((kp = key_lookup(mod, ks)) != NULL) {
5511 if (kfid == KF_INVALID)
5514 key_replace(kp, mod, ks, kfid, spawn_name);
5515 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5518 if (kfid == KF_INVALID) {
5519 warnx("error: setkeybinding: cannot find mod/key combination");
5520 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5524 key_insert(mod, ks, kfid, spawn_name);
5525 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5529 setconfbinding(char *selector, char *value, int flags)
5531 enum keyfuncid kfid;
5534 struct spawn_prog *sp;
5536 /* suppress unused warning since var is needed */
5539 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5540 if (selector == NULL) {
5541 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5542 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5544 setkeybinding(mod, ks, kfid, NULL);
5549 /* search by key function name */
5550 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5551 if (strncasecmp(selector, keyfuncs[kfid].name,
5552 SWM_FUNCNAME_LEN) == 0) {
5553 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5555 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5556 setkeybinding(mod, ks, kfid, NULL);
5562 /* search by custom spawn name */
5563 TAILQ_FOREACH(sp, &spawns, entry) {
5564 if (strcasecmp(selector, sp->name) == 0) {
5565 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5567 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5568 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5575 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5582 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5583 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5584 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5585 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5586 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5587 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5588 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5589 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5590 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5591 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5592 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5593 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5594 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5595 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5596 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5597 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5598 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5599 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5600 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5601 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5602 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5603 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5604 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5605 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5606 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5607 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5608 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5609 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5610 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5611 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5612 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5613 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5614 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5615 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5616 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5617 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5618 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5619 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5620 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5621 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5622 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5623 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5624 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5625 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5626 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5627 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5628 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5629 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5630 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5631 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5632 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5633 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5634 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5635 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5636 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5637 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5638 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5639 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5640 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5641 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5642 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5643 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5644 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5645 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5646 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5647 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5648 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5649 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5650 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5651 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5652 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5653 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5654 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5655 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5656 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5657 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5658 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5659 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5660 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5661 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5662 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5663 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5664 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5665 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5666 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5667 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5668 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5669 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5670 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5671 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5672 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5673 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5674 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5675 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5676 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5677 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5679 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5688 while (RB_EMPTY(&keys) == 0) {
5689 kp = RB_ROOT(&keys);
5695 setkeymapping(char *selector, char *value, int flags)
5697 char keymapping_file[PATH_MAX];
5699 /* suppress unused warnings since vars are needed */
5703 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5704 if (value[0] == '~')
5705 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5706 pwd->pw_dir, &value[1]);
5708 strlcpy(keymapping_file, value, sizeof keymapping_file);
5710 /* load new key bindings; if it fails, revert to default bindings */
5711 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5715 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5720 updatenumlockmask(void)
5723 xcb_get_modifier_mapping_reply_t *modmap_r;
5724 xcb_keycode_t *modmap, kc, *keycode;
5726 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5729 modmap_r = xcb_get_modifier_mapping_reply(conn,
5730 xcb_get_modifier_mapping(conn),
5733 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5734 for (i = 0; i < 8; i++) {
5735 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5736 kc = modmap[i * modmap_r->keycodes_per_modifier
5738 keycode = xcb_key_symbols_get_keycode(syms,
5741 numlockmask = (1 << i);
5754 xcb_keycode_t *code;
5755 unsigned int modifiers[] =
5756 { 0, LockMask, numlockmask, numlockmask | LockMask };
5759 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5760 updatenumlockmask();
5762 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5763 for (k = 0; k < num_screens; k++) {
5764 if (TAILQ_EMPTY(&screens[k].rl))
5766 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5768 RB_FOREACH(kp, key_tree, &keys) {
5769 if ((code = xcb_key_symbols_get_keycode(syms,
5771 for (j = 0; j < LENGTH(modifiers); j++)
5772 xcb_grab_key(conn, 1,
5774 kp->mod | modifiers[j],
5775 *code, XCB_GRAB_MODE_ASYNC,
5776 XCB_GRAB_MODE_ASYNC);
5783 grabbuttons(struct ws_win *win, int focused)
5786 unsigned int modifiers[] =
5787 { 0, LockMask, numlockmask, numlockmask|LockMask };
5789 updatenumlockmask();
5790 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5791 XCB_BUTTON_MASK_ANY);
5793 for (i = 0; i < LENGTH(buttons); i++)
5794 if (buttons[i].action == client_click)
5795 for (j = 0; j < LENGTH(modifiers); j++)
5796 xcb_grab_button(conn, 0, win->id,
5798 XCB_GRAB_MODE_ASYNC,
5805 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5806 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5807 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5810 const char *quirkname[] = {
5811 "NONE", /* config string for "no value" */
5820 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5821 #define SWM_Q_WS "\n|+ \t"
5823 parsequirks(char *qstr, unsigned long *quirk)
5833 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5835 cp += (long)strspn(cp, SWM_Q_WS);
5836 for (i = 0; i < LENGTH(quirkname); i++) {
5837 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5838 DNPRINTF(SWM_D_QUIRK,
5839 "parsequirks: %s\n", name);
5844 *quirk |= 1 << (i-1);
5848 if (i >= LENGTH(quirkname)) {
5849 DNPRINTF(SWM_D_QUIRK,
5850 "parsequirks: invalid quirk [%s]\n", name);
5858 quirk_insert(const char *class, const char *name, unsigned long quirk)
5862 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5865 if ((qp = malloc(sizeof *qp)) == NULL)
5866 err(1, "quirk_insert: malloc");
5867 if ((qp->class = strdup(class)) == NULL)
5868 err(1, "quirk_insert: strdup");
5869 if ((qp->name = strdup(name)) == NULL)
5870 err(1, "quirk_insert: strdup");
5873 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5875 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5879 quirk_remove(struct quirk *qp)
5881 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5882 qp->name, qp->quirk);
5884 TAILQ_REMOVE(&quirks, qp, entry);
5889 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5893 quirk_replace(struct quirk *qp, const char *class, const char *name,
5894 unsigned long quirk)
5896 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5897 qp->name, qp->quirk);
5900 quirk_insert(class, name, quirk);
5902 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5906 setquirk(const char *class, const char *name, unsigned long quirk)
5910 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5913 TAILQ_FOREACH(qp, &quirks, entry) {
5914 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5918 quirk_replace(qp, class, name, quirk);
5919 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5924 warnx("error: setquirk: cannot find class/name combination");
5928 quirk_insert(class, name, quirk);
5929 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5933 setconfquirk(char *selector, char *value, int flags)
5935 char *cp, *class, *name;
5939 /* suppress unused warning since var is needed */
5942 if (selector == NULL)
5944 if ((cp = strchr(selector, ':')) == NULL)
5949 if ((retval = parsequirks(value, &qrks)) == 0)
5950 setquirk(class, name, qrks);
5957 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5958 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5959 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5960 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5961 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5962 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5963 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5964 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5965 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5966 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5967 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5968 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5969 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5972 /* conf file stuff */
5973 #define SWM_CONF_FILE "spectrwm.conf"
5974 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5978 SWM_S_BAR_AT_BOTTOM,
5979 SWM_S_BAR_BORDER_WIDTH,
5986 SWM_S_CLOCK_ENABLED,
5989 SWM_S_CYCLE_VISIBLE,
5991 SWM_S_DISABLE_BORDER,
5993 SWM_S_FOCUS_CLOSE_WRAP,
5994 SWM_S_FOCUS_DEFAULT,
6000 SWM_S_STACK_ENABLED,
6002 SWM_S_TITLE_CLASS_ENABLED,
6003 SWM_S_TITLE_NAME_ENABLED,
6004 SWM_S_URGENT_ENABLED,
6005 SWM_S_VERBOSE_LAYOUT,
6006 SWM_S_WINDOW_NAME_ENABLED,
6007 SWM_S_WORKSPACE_LIMIT
6011 setconfvalue(char *selector, char *value, int flags)
6016 /* suppress unused warning since var is needed */
6020 case SWM_S_BAR_ACTION:
6022 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6023 err(1, "setconfvalue: bar_action");
6025 case SWM_S_BAR_AT_BOTTOM:
6026 bar_at_bottom = atoi(value);
6028 case SWM_S_BAR_BORDER_WIDTH:
6029 bar_border_width = atoi(value);
6030 if (bar_border_width < 0)
6031 bar_border_width = 0;
6033 case SWM_S_BAR_DELAY:
6034 bar_delay = atoi(value);
6036 case SWM_S_BAR_ENABLED:
6037 bar_enabled = atoi(value);
6039 case SWM_S_BAR_FONT:
6041 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6042 err(1, "setconfvalue: asprintf: failed to allocate "
6043 "memory for bar_fonts.");
6046 /* check if entry contains only xlfd entries */
6047 bar_font_legacy = 1;
6049 while ((b = strsep(&value, ",")) != NULL) {
6053 bar_font_legacy = 0;
6058 case SWM_S_BAR_FORMAT:
6060 if ((bar_format = strdup(value)) == NULL)
6061 err(1, "setconfvalue: bar_format");
6063 case SWM_S_BAR_JUSTIFY:
6064 if (!strcmp(value, "left"))
6065 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6066 else if (!strcmp(value, "center"))
6067 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6068 else if (!strcmp(value, "right"))
6069 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6071 errx(1, "invalid bar_justify");
6073 case SWM_S_BORDER_WIDTH:
6074 border_width = atoi(value);
6075 if (border_width < 0)
6078 case SWM_S_CLOCK_ENABLED:
6079 clock_enabled = atoi(value);
6081 case SWM_S_CLOCK_FORMAT:
6082 #ifndef SWM_DENY_CLOCK_FORMAT
6084 if ((clock_format = strdup(value)) == NULL)
6085 err(1, "setconfvalue: clock_format");
6088 case SWM_S_CYCLE_EMPTY:
6089 cycle_empty = atoi(value);
6091 case SWM_S_CYCLE_VISIBLE:
6092 cycle_visible = atoi(value);
6094 case SWM_S_DIALOG_RATIO:
6095 dialog_ratio = atof(value);
6096 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6099 case SWM_S_DISABLE_BORDER:
6100 disable_border = atoi(value);
6102 case SWM_S_FOCUS_CLOSE:
6103 if (!strcmp(value, "first"))
6104 focus_close = SWM_STACK_BOTTOM;
6105 else if (!strcmp(value, "last"))
6106 focus_close = SWM_STACK_TOP;
6107 else if (!strcmp(value, "next"))
6108 focus_close = SWM_STACK_ABOVE;
6109 else if (!strcmp(value, "previous"))
6110 focus_close = SWM_STACK_BELOW;
6112 errx(1, "focus_close");
6114 case SWM_S_FOCUS_CLOSE_WRAP:
6115 focus_close_wrap = atoi(value);
6117 case SWM_S_FOCUS_DEFAULT:
6118 if (!strcmp(value, "last"))
6119 focus_default = SWM_STACK_TOP;
6120 else if (!strcmp(value, "first"))
6121 focus_default = SWM_STACK_BOTTOM;
6123 errx(1, "focus_default");
6125 case SWM_S_FOCUS_MODE:
6126 if (!strcmp(value, "default"))
6127 focus_mode = SWM_FOCUS_DEFAULT;
6128 else if (!strcmp(value, "follow_cursor"))
6129 focus_mode = SWM_FOCUS_FOLLOW;
6130 else if (!strcmp(value, "synergy"))
6131 focus_mode = SWM_FOCUS_SYNERGY;
6133 errx(1, "focus_mode");
6135 case SWM_S_SPAWN_ORDER:
6136 if (!strcmp(value, "first"))
6137 spawn_position = SWM_STACK_BOTTOM;
6138 else if (!strcmp(value, "last"))
6139 spawn_position = SWM_STACK_TOP;
6140 else if (!strcmp(value, "next"))
6141 spawn_position = SWM_STACK_ABOVE;
6142 else if (!strcmp(value, "previous"))
6143 spawn_position = SWM_STACK_BELOW;
6145 errx(1, "spawn_position");
6147 case SWM_S_SPAWN_TERM:
6148 setconfspawn("term", value, 0);
6149 setconfspawn("spawn_term", value, 0);
6153 case SWM_S_SS_ENABLED:
6154 ss_enabled = atoi(value);
6156 case SWM_S_STACK_ENABLED:
6157 stack_enabled = atoi(value);
6159 case SWM_S_TERM_WIDTH:
6160 term_width = atoi(value);
6164 case SWM_S_TITLE_CLASS_ENABLED:
6165 title_class_enabled = atoi(value);
6167 case SWM_S_TITLE_NAME_ENABLED:
6168 title_name_enabled = atoi(value);
6170 case SWM_S_URGENT_ENABLED:
6171 urgent_enabled = atoi(value);
6173 case SWM_S_VERBOSE_LAYOUT:
6174 verbose_layout = atoi(value);
6175 for (i = 0; layouts[i].l_stack != NULL; i++) {
6177 layouts[i].l_string = fancy_stacker;
6179 layouts[i].l_string = plain_stacker;
6182 case SWM_S_WINDOW_NAME_ENABLED:
6183 window_name_enabled = atoi(value);
6185 case SWM_S_WORKSPACE_LIMIT:
6186 workspace_limit = atoi(value);
6187 if (workspace_limit > SWM_WS_MAX)
6188 workspace_limit = SWM_WS_MAX;
6189 else if (workspace_limit < 1)
6190 workspace_limit = 1;
6199 setconfmodkey(char *selector, char *value, int flags)
6201 /* suppress unused warnings since vars are needed */
6205 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6206 update_modkey(Mod1Mask);
6207 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6208 update_modkey(Mod2Mask);
6209 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6210 update_modkey(Mod3Mask);
6211 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6212 update_modkey(Mod4Mask);
6219 setconfcolor(char *selector, char *value, int flags)
6221 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6226 setconfregion(char *selector, char *value, int flags)
6228 /* suppress unused warnings since vars are needed */
6232 custom_region(value);
6237 setautorun(char *selector, char *value, int flags)
6247 /* suppress unused warnings since vars are needed */
6251 if (getenv("SWM_STARTED"))
6255 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6256 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6258 if (ws_id < 0 || ws_id >= workspace_limit)
6259 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6262 * This is a little intricate
6264 * If the pid already exists we simply reuse it because it means it was
6265 * used before AND not claimed by manage_window. We get away with
6266 * altering it in the parent after INSERT because this can not be a race
6269 while ((ap = strsep(&sp, " \t")) != NULL) {
6272 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6274 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6275 err(1, "setautorun: realloc");
6276 a.argv[argc - 1] = ap;
6279 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6280 err(1, "setautorun: realloc");
6281 a.argv[argc] = NULL;
6283 if ((pid = fork()) == 0) {
6284 spawn(ws_id, &a, 1);
6293 p = calloc(1, sizeof *p);
6296 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6306 setlayout(char *selector, char *value, int flags)
6308 int ws_id, i, x, mg, ma, si, ar, f = 0;
6309 int st = SWM_V_STACK, num_screens;
6311 struct workspace *ws;
6313 /* suppress unused warnings since vars are needed */
6317 if (getenv("SWM_STARTED"))
6321 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6322 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6323 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6324 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6327 if (ws_id < 0 || ws_id >= workspace_limit)
6328 errx(1, "layout: invalid workspace %d", ws_id + 1);
6330 if (!strcasecmp(s, "vertical"))
6332 else if (!strcasecmp(s, "vertical_flip")) {
6335 } else if (!strcasecmp(s, "horizontal"))
6337 else if (!strcasecmp(s, "horizontal_flip")) {
6340 } else if (!strcasecmp(s, "fullscreen"))
6343 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6344 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6347 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6348 for (i = 0; i < num_screens; i++) {
6349 ws = (struct workspace *)&screens[i].ws;
6350 ws[ws_id].cur_layout = &layouts[st];
6352 ws[ws_id].always_raise = ar;
6353 if (st == SWM_MAX_STACK)
6357 for (x = 0; x < abs(mg); x++) {
6358 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6359 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6360 SWM_ARG_ID_MASTERSHRINK);
6364 for (x = 0; x < abs(ma); x++) {
6365 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6366 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6367 SWM_ARG_ID_MASTERDEL);
6371 for (x = 0; x < abs(si); x++) {
6372 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6373 si >= 0 ? SWM_ARG_ID_STACKINC :
6374 SWM_ARG_ID_STACKDEC);
6379 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6380 SWM_ARG_ID_FLIPLAYOUT);
6388 /* config options */
6389 struct config_option {
6391 int (*func)(char*, char*, int);
6394 struct config_option configopt[] = {
6395 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6396 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6397 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6398 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6399 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6400 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6401 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6402 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6403 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6404 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6405 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6406 { "keyboard_mapping", setkeymapping, 0 },
6407 { "bind", setconfbinding, 0 },
6408 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6409 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6410 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6411 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6412 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6413 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6414 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6415 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6416 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6417 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6418 { "modkey", setconfmodkey, 0 },
6419 { "program", setconfspawn, 0 },
6420 { "quirk", setconfquirk, 0 },
6421 { "region", setconfregion, 0 },
6422 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6423 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6424 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6425 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6426 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6427 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6428 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6429 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6430 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6431 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6432 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6433 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6434 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6435 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6436 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6437 { "autorun", setautorun, 0 },
6438 { "layout", setlayout, 0 },
6443 conf_load(char *filename, int keymapping)
6446 char *line, *cp, *optsub, *optval;
6447 size_t linelen, lineno = 0;
6448 int wordlen, i, optidx;
6449 struct config_option *opt;
6451 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6453 if (filename == NULL) {
6454 warnx("conf_load: no filename");
6457 if ((config = fopen(filename, "r")) == NULL) {
6458 warn("conf_load: fopen: %s", filename);
6462 while (!feof(config)) {
6463 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6466 err(1, "%s", filename);
6471 cp += strspn(cp, " \t\n"); /* eat whitespace */
6472 if (cp[0] == '\0') {
6477 /* get config option */
6478 wordlen = strcspn(cp, "=[ \t\n");
6480 warnx("%s: line %zd: no option found",
6485 for (i = 0; i < LENGTH(configopt); i++) {
6486 opt = &configopt[i];
6487 if (!strncasecmp(cp, opt->optname, wordlen) &&
6488 (int)strlen(opt->optname) == wordlen) {
6494 warnx("%s: line %zd: unknown option %.*s",
6495 filename, lineno, wordlen, cp);
6498 if (keymapping && strcmp(opt->optname, "bind")) {
6499 warnx("%s: line %zd: invalid option %.*s",
6500 filename, lineno, wordlen, cp);
6504 cp += strspn(cp, " \t\n"); /* eat whitespace */
6505 /* get [selector] if any */
6509 wordlen = strcspn(cp, "]");
6512 warnx("%s: line %zd: syntax error",
6517 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6519 warnx("%s: line %zd: unable to allocate"
6520 "memory for selector", filename,
6526 cp += strspn(cp, "] \t\n"); /* eat trailing */
6528 cp += strspn(cp, "= \t\n"); /* eat trailing */
6530 optval = strdup(cp);
6531 /* call function to deal with it all */
6532 if (configopt[optidx].func(optsub, optval,
6533 configopt[optidx].funcflags) != 0)
6534 errx(1, "%s: line %zd: invalid data for %s",
6535 filename, lineno, configopt[optidx].optname);
6542 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6549 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6555 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6557 struct ws_win *parent, *w;
6558 struct swm_region *r;
6559 struct workspace *ws;
6560 xcb_icccm_wm_hints_t wmh;
6562 parent = find_window(win->transient);
6564 parent->child_trans = win;
6566 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6567 " for 0x%x trans 0x%x\n", win->id, win->transient);
6569 r = root_to_region(win->wa->root);
6571 /* parent doen't exist in our window list */
6572 TAILQ_FOREACH(w, &ws->winlist, entry) {
6573 if (xcb_icccm_get_wm_hints_reply(conn,
6574 xcb_icccm_get_wm_hints(conn, w->id),
6576 warnx("can't get hints for 0x%x", w->id);
6580 if (win->hints.window_group != wmh.window_group)
6583 w->child_trans = win;
6584 win->transient = w->id;
6586 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6587 "transient to 0x%x\n", win->transient);
6594 window_get_pid(xcb_window_t win)
6599 xcb_get_property_cookie_t pc;
6600 xcb_get_property_reply_t *pr;
6602 apid = get_atom_from_string("_NET_WM_PID");
6603 if (apid == XCB_ATOM_NONE)
6606 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6607 pr = xcb_get_property_reply(conn, pc, NULL);
6610 if (pr->type != XCB_ATOM_CARDINAL) {
6615 if (pr->type == apid && pr->format == 32)
6616 ret = *((pid_t *)xcb_get_property_value(pr));
6622 apid = get_atom_from_string("_SWM_PID");
6623 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6625 pr = xcb_get_property_reply(conn, pc, NULL);
6628 if (pr->type != apid) {
6633 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6640 get_ws_idx(xcb_window_t id)
6646 xcb_get_property_reply_t *gpr;
6648 gpr = xcb_get_property_reply(conn,
6649 xcb_get_property(conn, 0, id, a_swm_ws,
6650 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6655 proplen = xcb_get_property_value_length(gpr);
6657 prop = malloc(proplen + 1);
6660 xcb_get_property_value(gpr),
6662 prop[proplen] = '\0';
6669 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6670 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6672 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6682 manage_window(xcb_window_t id)
6684 xcb_window_t trans = XCB_WINDOW_NONE;
6685 struct workspace *ws;
6686 struct ws_win *win, *ww;
6687 int ws_idx, border_me = 0;
6688 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6689 struct swm_region *r;
6692 uint32_t event_mask, i;
6693 xcb_icccm_get_wm_protocols_reply_t wpr;
6695 if ((win = find_window(id)) != NULL)
6696 return (win); /* already being managed */
6698 /* see if we are on the unmanaged list */
6699 if ((win = find_unmanaged_window(id)) != NULL) {
6700 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6701 "window: 0x%x\n", win->id);
6702 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6704 set_child_transient(win, &trans);
6706 if (trans && (ww = find_window(trans)))
6707 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6708 else if ((ww = win->ws->focus) &&
6709 spawn_position == SWM_STACK_ABOVE)
6710 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6712 else if (ww && spawn_position == SWM_STACK_BELOW)
6713 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6714 else switch (spawn_position) {
6717 case SWM_STACK_ABOVE:
6718 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6720 case SWM_STACK_BOTTOM:
6721 case SWM_STACK_BELOW:
6722 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6725 ewmh_update_actions(win);
6729 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6730 err(1, "manage_window: calloc: failed to allocate memory for "
6736 /* see if we need to override the workspace */
6737 p = find_pid(window_get_pid(id));
6739 /* Get all the window data in one shot */
6740 ws_idx = get_ws_idx(id);
6742 win->wa = xcb_get_geometry_reply(conn,
6743 xcb_get_geometry(conn, id),
6745 xcb_icccm_get_wm_normal_hints_reply(conn,
6746 xcb_icccm_get_wm_normal_hints(conn, id),
6748 xcb_icccm_get_wm_hints_reply(conn,
6749 xcb_icccm_get_wm_hints(conn, id),
6751 xcb_icccm_get_wm_transient_for_reply(conn,
6752 xcb_icccm_get_wm_transient_for(conn, id),
6755 win->transient = trans;
6756 set_child_transient(win, &trans);
6757 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6758 "transient: 0x%x\n", win->id, win->transient);
6761 /* get supported protocols */
6762 if (xcb_icccm_get_wm_protocols_reply(conn,
6763 xcb_icccm_get_wm_protocols(conn, id, a_prot),
6765 for (i = 0; i < wpr.atoms_len; i++) {
6766 if (wpr.atoms[i] == a_takefocus)
6767 win->take_focus = 1;
6768 if (wpr.atoms[i] == a_delete)
6769 win->can_delete = 1;
6771 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6774 win->iconic = get_iconic(win);
6777 * Figure out where to put the window. If it was previously assigned to
6778 * a workspace (either by spawn() or manually moving), and isn't
6779 * transient, * put it in the same workspace
6781 r = root_to_region(win->wa->root);
6783 ws = &r->s->ws[p->ws];
6784 TAILQ_REMOVE(&pidlist, p, entry);
6787 } else if (ws_idx != -1 && win->transient == 0) {
6788 ws = &r->s->ws[ws_idx];
6791 /* this should launch transients in the same ws as parent */
6793 if ((ww = find_window(trans)) != NULL)
6799 warnx("manage_window: fix this "
6805 /* set up the window layout */
6808 win->s = r->s; /* this never changes */
6809 if (trans && (ww = find_window(trans)))
6810 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6811 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6812 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6814 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6815 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6816 else switch (spawn_position) {
6819 case SWM_STACK_ABOVE:
6820 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6822 case SWM_STACK_BOTTOM:
6823 case SWM_STACK_BELOW:
6824 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6827 /* ignore window border if there is one. */
6828 WIDTH(win) = win->wa->width;
6829 HEIGHT(win) = win->wa->height;
6830 X(win) = win->wa->x + win->wa->border_width;
6831 Y(win) = win->wa->y + win->wa->border_width;
6833 win->g_floatvalid = 0;
6834 win->floatmaxed = 0;
6835 win->ewmh_flags = 0;
6837 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6838 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6839 HEIGHT(win), ws->idx);
6841 constrain_window(win, r, 0);
6843 /* Set window properties so we can remember this after reincarnation */
6844 if (prop == NULL && snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6846 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6848 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6849 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6855 ewmh_autoquirk(win);
6857 if (xcb_icccm_get_wm_class_reply(conn,
6858 xcb_icccm_get_wm_class(conn, win->id),
6860 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6861 win->ch.class_name, win->ch.instance_name);
6863 /* java is retarded so treat it special */
6864 if (strstr(win->ch.instance_name, "sun-awt")) {
6869 TAILQ_FOREACH(qp, &quirks, entry) {
6870 if (!strcmp(win->ch.class_name, qp->class) &&
6871 !strcmp(win->ch.instance_name, qp->name)) {
6872 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6873 "class: %s, name: %s\n", win->ch.class_name,
6874 win->ch.instance_name);
6875 if (qp->quirk & SWM_Q_FLOAT) {
6879 win->quirks = qp->quirk;
6884 /* alter window position if quirky */
6885 if (win->quirks & SWM_Q_ANYWHERE) {
6886 win->manual = 1; /* don't center the quirky windows */
6887 if (bar_enabled && Y(win) < bar_height)
6888 Y(win) = bar_height;
6889 if (WIDTH(win) + X(win) > WIDTH(r))
6890 X(win) = WIDTH(r) - WIDTH(win) - 2;
6894 /* Reset font sizes (the bruteforce way; no default keybinding). */
6895 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6896 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6897 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6898 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6899 fake_keypress(win, XK_KP_Add, ShiftMask);
6902 ewmh_get_win_state(win);
6903 ewmh_update_actions(win);
6904 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6909 X(win) -= border_width;
6910 Y(win) -= border_width;
6914 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6915 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6917 event_mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
6920 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6922 /* floaters need to be mapped if they are in the current workspace */
6923 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6924 map_window_raised(win->id);
6930 free_window(struct ws_win *win)
6932 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6937 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6942 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6947 memset(win, 0xff, sizeof *win); /* XXX kill later */
6950 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6954 unmanage_window(struct ws_win *win)
6956 struct ws_win *parent;
6957 xcb_screen_t *screen;
6962 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6964 if (win->transient) {
6965 parent = find_window(win->transient);
6967 parent->child_trans = NULL;
6970 /* focus on root just in case */
6971 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6972 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6973 screen->root, XCB_CURRENT_TIME);
6977 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6978 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6982 focus_magic(struct ws_win *win)
6984 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6987 /* if there are no windows clear the status-bar */
6992 if (win->child_trans) {
6993 /* win = parent & has a transient so focus on that */
6995 focus_win(win->child_trans);
6996 if (win->child_trans->take_focus)
6997 client_msg(win, a_takefocus);
6999 /* make sure transient hasn't disappeared */
7000 if (validate_win(win->child_trans) == 0) {
7001 focus_win(win->child_trans);
7002 if (win->child_trans->take_focus)
7003 client_msg(win->child_trans,
7006 win->child_trans = NULL;
7008 if (win->take_focus)
7009 client_msg(win, a_takefocus);
7015 if (win->take_focus)
7016 client_msg(win, a_takefocus);
7021 expose(xcb_expose_event_t *e)
7024 struct swm_region *r;
7026 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7028 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7029 for (i = 0; i < num_screens; i++)
7030 TAILQ_FOREACH(r, &screens[i].rl, entry)
7031 if (e->window == WINID(r->bar))
7039 focusin(xcb_focus_in_event_t *e)
7041 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7042 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7043 e->mode, get_notify_detail_label(e->detail), e->detail);
7048 keypress(xcb_key_press_event_t *e)
7050 xcb_keysym_t keysym;
7053 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7055 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7057 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7060 if (kp->funcid == KF_SPAWN_CUSTOM)
7061 spawn_custom(root_to_region(e->root),
7062 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7063 else if (keyfuncs[kp->funcid].func)
7064 keyfuncs[kp->funcid].func(root_to_region(e->root),
7065 &(keyfuncs[kp->funcid].args));
7069 buttonpress(xcb_button_press_event_t *e)
7073 unsigned int action;
7075 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
7076 e->event, e->detail);
7078 if ((win = find_window(e->event)) == NULL)
7082 action = client_click;
7084 for (i = 0; i < LENGTH(buttons); i++)
7085 if (action == buttons[i].action && buttons[i].func &&
7086 buttons[i].button == e->detail &&
7087 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
7088 buttons[i].func(win, &buttons[i].args);
7095 print_win_geom(xcb_window_t w)
7097 xcb_get_geometry_reply_t *wa;
7099 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7102 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7107 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7108 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7109 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7117 configurerequest(xcb_configure_request_event_t *e)
7122 uint32_t wc[7] = {0};
7124 if ((win = find_window(e->window)) == NULL)
7125 if ((win = find_unmanaged_window(e->window)) == NULL)
7128 print_win_geom(e->window);
7131 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
7132 "value_mask: 0x%x", e->window, e->value_mask);
7133 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7134 mask |= XCB_CONFIG_WINDOW_X;
7136 DPRINTF(", X: %d", e->x);
7138 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7139 mask |= XCB_CONFIG_WINDOW_Y;
7141 DPRINTF(", Y: %d", e->y);
7143 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7144 mask |= XCB_CONFIG_WINDOW_WIDTH;
7146 DPRINTF(", W: %u", e->width);
7148 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7149 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7150 wc[i++] = e->height;
7151 DPRINTF(", H: %u", e->height);
7153 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7154 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7155 wc[i++] = e->border_width;
7156 DPRINTF(", Border: %u", e->border_width);
7158 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7159 mask |= XCB_CONFIG_WINDOW_SIBLING;
7160 wc[i++] = e->sibling;
7161 DPRINTF(", Sibling: 0x%x", e->sibling);
7163 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7164 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7165 wc[i++] = e->stack_mode;
7166 DPRINTF(", StackMode: %u", e->stack_mode);
7170 xcb_configure_window(conn, e->window, mask, wc);
7172 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
7173 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7174 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7175 win->g_float.x = e->x - X(win->ws->r);
7176 win->g_float.y = e->y - Y(win->ws->r);
7177 win->g_float.w = e->width;
7178 win->g_float.h = e->height;
7179 win->g_floatvalid = 1;
7181 if (win->floating) {
7182 win->g = win->g_float;
7183 win->g.x += X(win->ws->r);
7184 win->g.y += Y(win->ws->r);
7197 configurenotify(xcb_configure_notify_event_t *e)
7201 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
7204 win = find_window(e->window);
7206 xcb_icccm_get_wm_normal_hints_reply(conn,
7207 xcb_icccm_get_wm_normal_hints(conn, win->id),
7216 destroynotify(xcb_destroy_notify_event_t *e)
7220 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7222 if ((win = find_window(e->window)) == NULL) {
7223 if ((win = find_unmanaged_window(e->window)) == NULL)
7229 /* make sure we focus on something */
7232 unmanage_window(win);
7239 get_notify_detail_label(uint8_t detail)
7244 case XCB_NOTIFY_DETAIL_ANCESTOR:
7247 case XCB_NOTIFY_DETAIL_VIRTUAL:
7250 case XCB_NOTIFY_DETAIL_INFERIOR:
7253 case XCB_NOTIFY_DETAIL_NONLINEAR:
7254 label = "Nonlinear";
7256 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7257 label = "NonlinearVirtual";
7259 case XCB_NOTIFY_DETAIL_POINTER:
7262 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7263 label = "PointerRoot";
7265 case XCB_NOTIFY_DETAIL_NONE:
7276 get_notify_mode_label(uint8_t mode)
7281 case XCB_NOTIFY_MODE_NORMAL:
7284 case XCB_NOTIFY_MODE_GRAB:
7287 case XCB_NOTIFY_MODE_UNGRAB:
7290 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7291 label = "WhileGrabbed";
7302 enternotify(xcb_enter_notify_event_t *e)
7305 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %s(%d), "
7306 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7307 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7308 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7309 e->child, YESNO(e->same_screen_focus), e->state);
7311 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
7312 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
7317 switch (focus_mode) {
7318 case SWM_FOCUS_DEFAULT:
7320 case SWM_FOCUS_FOLLOW:
7322 case SWM_FOCUS_SYNERGY:
7326 if ((win = find_window(e->event)) == NULL) {
7327 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7338 leavenotify(xcb_leave_notify_event_t *e)
7340 DNPRINTF(SWM_D_FOCUS, "leavenotify: window: 0x%x, mode: %s(%d), "
7341 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7342 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7343 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7344 e->child, YESNO(e->same_screen_focus), e->state);
7348 /* lets us use one switch statement for arbitrary mode/detail combinations */
7349 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7352 mapnotify(xcb_map_notify_event_t *e)
7356 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7358 win = manage_window(e->window);
7360 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7363 * focus_win can only set input focus on a mapped window.
7364 * make sure the window really has focus since it is just being mapped.
7366 if (win->ws->focus == win)
7371 mappingnotify(xcb_mapping_notify_event_t *e)
7373 xcb_refresh_keyboard_mapping(syms, e);
7375 if (e->request == XCB_MAPPING_KEYBOARD)
7380 maprequest(xcb_map_request_event_t *e)
7383 struct swm_region *r;
7384 xcb_get_window_attributes_reply_t *war;
7386 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7389 war = xcb_get_window_attributes_reply(conn,
7390 xcb_get_window_attributes(conn, e->window),
7394 if (war->override_redirect) {
7400 win = manage_window(e->window);
7402 return; /* can't happen */
7407 /* make new win focused */
7408 r = root_to_region(win->wa->root);
7409 if (win->ws == r->ws)
7415 get_atom_name(xcb_atom_t atom)
7419 xcb_get_atom_name_reply_t *r;
7421 r = xcb_get_atom_name_reply(conn,
7422 xcb_get_atom_name(conn, atom),
7425 len = xcb_get_atom_name_name_length(r);
7427 name = malloc(len + 1);
7429 memcpy(name, xcb_get_atom_name_name(r), len);
7441 propertynotify(xcb_property_notify_event_t *e)
7447 name = get_atom_name(e->atom);
7448 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7449 e->window, name, e->atom);
7453 win = find_window(e->window);
7457 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7458 update_iconic(win, 0);
7459 map_window_raised(win->id);
7466 case XCB_ATOM_WM_CLASS:
7467 case XCB_ATOM_WM_NAME:
7476 unmapnotify(xcb_unmap_notify_event_t *e)
7480 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7482 /* determine if we need to help unmanage this window */
7483 win = find_window(e->window);
7487 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7488 unmanage_window(win);
7491 /* resend unmap because we ated it */
7492 xcb_unmap_window(conn, e->window);
7499 visibilitynotify(xcb_visibility_notify_event_t *e)
7501 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7507 clientmessage(xcb_client_message_event_t *e)
7510 xcb_map_request_event_t mre;
7514 name = get_atom_name(e->type);
7515 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7516 e->window, name, e->type);
7519 win = find_window(e->window);
7522 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7523 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7524 "unmanaged window.\n");
7525 mre.window = e->window;
7531 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7532 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7535 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7536 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7537 if (win->can_delete)
7538 client_msg(win, a_delete);
7540 xcb_kill_client(conn, win->id);
7542 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7543 DNPRINTF(SWM_D_EVENT,
7544 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7545 if (win->floating) {
7546 if (e->data.data32[0] & (1<<8)) /* x */
7547 X(win) = e->data.data32[1];
7548 if (e->data.data32[0] & (1<<9)) /* y */
7549 Y(win) = e->data.data32[2];
7550 if (e->data.data32[0] & (1<<10)) /* width */
7551 WIDTH(win) = e->data.data32[3];
7552 if (e->data.data32[0] & (1<<11)) /* height */
7553 HEIGHT(win) = e->data.data32[4];
7558 /* TODO: Change stack sizes */
7559 /* notify no change was made. */
7560 config_win(win, NULL);
7563 if (e->type == ewmh[_NET_WM_STATE].atom) {
7564 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7565 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7566 if (e->data.data32[2])
7567 ewmh_update_win_state(win, e->data.data32[2],
7579 int errcode = xcb_connection_has_error(conn);
7580 #ifdef XCB_CONN_ERROR
7583 case XCB_CONN_ERROR:
7584 s = "Socket error, pipe error or other stream error.";
7586 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
7587 s = "Extension not supported.";
7589 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
7590 s = "Insufficient memory.";
7592 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
7593 s = "Request length was exceeded.";
7595 case XCB_CONN_CLOSED_PARSE_ERR:
7596 s = "Error parsing display string.";
7599 s = "Unknown error.";
7602 errx(errcode, "X CONNECTION ERROR: %s", s);
7605 errx(errcode, "X CONNECTION ERROR");
7613 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7615 xcb_void_cookie_t wac;
7616 xcb_generic_error_t *error;
7618 /* this causes an error if some other window manager is running */
7619 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7620 for (i = 0; i < num_screens; i++) {
7621 if ((sc = get_screen(i)) == NULL)
7622 errx(1, "ERROR: can't get screen %d.", i);
7623 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7625 wac = xcb_change_window_attributes_checked(conn, sc->root,
7626 XCB_CW_EVENT_MASK, &val);
7627 if ((error = xcb_request_check(conn, wac))) {
7628 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7639 new_region(struct swm_screen *s, int x, int y, int w, int h)
7641 struct swm_region *r, *n;
7642 struct workspace *ws = NULL;
7645 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7646 s->idx, w, h, x, y);
7648 /* remove any conflicting regions */
7649 n = TAILQ_FIRST(&s->rl);
7652 n = TAILQ_NEXT(r, entry);
7653 if (X(r) < (x + w) &&
7654 (X(r) + WIDTH(r)) > x &&
7656 (Y(r) + HEIGHT(r)) > y) {
7657 if (r->ws->r != NULL)
7658 r->ws->old_r = r->ws->r;
7661 TAILQ_REMOVE(&s->rl, r, entry);
7662 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7666 /* search old regions for one to reuse */
7668 /* size + location match */
7669 TAILQ_FOREACH(r, &s->orl, entry)
7670 if (X(r) == x && Y(r) == y &&
7671 HEIGHT(r) == h && WIDTH(r) == w)
7675 TAILQ_FOREACH(r, &s->orl, entry)
7676 if (HEIGHT(r) == h && WIDTH(r) == w)
7680 TAILQ_REMOVE(&s->orl, r, entry);
7681 /* try to use old region's workspace */
7682 if (r->ws->r == NULL)
7685 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7686 err(1, "new_region: calloc: failed to allocate memory "
7689 /* if we don't have a workspace already, find one */
7691 for (i = 0; i < workspace_limit; i++)
7692 if (s->ws[i].r == NULL) {
7699 errx(1, "new_region: no free workspaces");
7710 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7716 #ifdef SWM_XRR_HAS_CRTC
7719 #endif /* SWM_XRR_HAS_CRTC */
7720 struct swm_region *r;
7722 xcb_randr_get_screen_resources_current_cookie_t src;
7723 xcb_randr_get_screen_resources_current_reply_t *srr;
7724 xcb_randr_get_crtc_info_cookie_t cic;
7725 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7726 xcb_randr_crtc_t *crtc;
7727 xcb_screen_t *screen;
7729 if ((screen = get_screen(i)) == NULL)
7730 errx(1, "ERROR: can't get screen %d.", i);
7732 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7733 if (i >= num_screens)
7734 errx(1, "scan_xrandr: invalid screen");
7736 /* remove any old regions */
7737 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7738 r->ws->old_r = r->ws->r = NULL;
7740 TAILQ_REMOVE(&screens[i].rl, r, entry);
7741 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7745 /* map virtual screens onto physical screens */
7746 #ifdef SWM_XRR_HAS_CRTC
7747 if (xrandr_support) {
7748 src = xcb_randr_get_screen_resources_current(conn,
7750 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7753 new_region(&screens[i], 0, 0,
7754 screen->width_in_pixels,
7755 screen->height_in_pixels);
7758 ncrtc = srr->num_crtcs;
7760 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7761 for (c = 0; c < ncrtc; c++) {
7762 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7764 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7767 if (cir->num_outputs == 0) {
7773 new_region(&screens[i], 0, 0,
7774 screen->width_in_pixels,
7775 screen->height_in_pixels);
7777 new_region(&screens[i],
7778 cir->x, cir->y, cir->width, cir->height);
7783 #endif /* SWM_XRR_HAS_CRTC */
7785 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7786 screen->height_in_pixels);
7791 screenchange(xcb_randr_screen_change_notify_event_t *e)
7793 struct swm_region *r;
7796 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7798 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7799 /* silly event doesn't include the screen index */
7800 for (i = 0; i < num_screens; i++)
7801 if (screens[i].root == e->root)
7803 if (i >= num_screens)
7804 errx(1, "screenchange: screen not found");
7806 /* brute force for now, just re-enumerate the regions */
7810 print_win_geom(e->root);
7812 /* add bars to all regions */
7813 for (i = 0; i < num_screens; i++)
7814 TAILQ_FOREACH(r, &screens[i].rl, entry)
7822 xcb_window_t *wins = NULL;
7824 int i, j, num_screens;
7825 uint16_t state, manage;
7827 xcb_query_tree_cookie_t qtc;
7828 xcb_query_tree_reply_t *qtr;
7829 xcb_get_window_attributes_cookie_t c;
7830 xcb_get_window_attributes_reply_t *r;
7831 xcb_get_property_cookie_t pc;
7833 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
7834 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7835 for (i = 0; i < num_screens; i++) {
7836 qtc = xcb_query_tree(conn, screens[i].root);
7837 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7840 wins = xcb_query_tree_children(qtr);
7841 no = xcb_query_tree_children_length(qtr);
7842 /* attach windows to a region */
7843 /* normal windows */
7844 for (j = 0; j < no; j++) {
7845 c = xcb_get_window_attributes(conn, wins[j]);
7846 r = xcb_get_window_attributes_reply(conn, c, NULL);
7849 if (r->override_redirect) {
7854 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7855 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7861 state = getstate(wins[j]);
7862 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7863 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7864 manage_window(wins[j]);
7867 /* transient windows */
7868 for (j = 0; j < no; j++) {
7869 c = xcb_get_window_attributes(conn, wins[j]);
7870 r = xcb_get_window_attributes_reply(conn, c, NULL);
7873 if (r->override_redirect) {
7879 state = getstate(wins[j]);
7880 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7881 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7882 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7883 &wins[j], NULL) && manage)
7884 manage_window(wins[j]);
7888 DNPRINTF(SWM_D_INIT, "grab_windows: done\n");
7894 int i, j, k, num_screens;
7895 struct workspace *ws;
7896 uint32_t gcv[1], wa[1];
7897 const xcb_query_extension_reply_t *qep;
7898 xcb_screen_t *screen;
7899 xcb_cursor_t cursor;
7900 xcb_font_t cursor_font;
7901 xcb_randr_query_version_cookie_t c;
7902 xcb_randr_query_version_reply_t *r;
7904 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7905 if ((screens = calloc(num_screens,
7906 sizeof(struct swm_screen))) == NULL)
7907 err(1, "setup_screens: calloc: failed to allocate memory for "
7910 /* initial Xrandr setup */
7912 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7914 c = xcb_randr_query_version(conn, 1, 1);
7915 r = xcb_randr_query_version_reply(conn, c, NULL);
7917 if (r->major_version >= 1) {
7919 xrandr_eventbase = qep->first_event;
7925 cursor_font = xcb_generate_id(conn);
7926 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7928 cursor = xcb_generate_id(conn);
7929 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7930 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7933 /* map physical screens */
7934 for (i = 0; i < num_screens; i++) {
7935 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7937 TAILQ_INIT(&screens[i].rl);
7938 TAILQ_INIT(&screens[i].orl);
7939 if ((screen = get_screen(i)) == NULL)
7940 errx(1, "ERROR: can't get screen %d.", i);
7941 screens[i].root = screen->root;
7943 /* set default colors */
7944 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7945 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7946 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7947 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7948 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7950 /* create graphics context on screen */
7951 screens[i].bar_gc = xcb_generate_id(conn);
7953 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7954 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7956 /* set default cursor */
7957 xcb_change_window_attributes(conn, screens[i].root,
7960 /* init all workspaces */
7961 /* XXX these should be dynamically allocated too */
7962 for (j = 0; j < SWM_WS_MAX; j++) {
7963 ws = &screens[i].ws[j];
7969 TAILQ_INIT(&ws->winlist);
7970 TAILQ_INIT(&ws->unmanagedlist);
7972 for (k = 0; layouts[k].l_stack != NULL; k++)
7973 if (layouts[k].l_config != NULL)
7974 layouts[k].l_config(ws,
7975 SWM_ARG_ID_STACKINIT);
7976 ws->cur_layout = &layouts[0];
7977 ws->cur_layout->l_string(ws);
7983 xcb_randr_select_input(conn, screens[i].root,
7984 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7986 xcb_free_cursor(conn, cursor);
7987 xcb_close_font(conn, cursor_font);
7993 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7994 err(1, "setup_globals: strdup: failed to allocate memory.");
7996 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7997 err(1, "setup_globals: strdup: failed to allocate memory.");
7999 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8000 errx(1, "unable to allocate key symbols");
8002 a_state = get_atom_from_string("WM_STATE");
8003 a_prot = get_atom_from_string("WM_PROTOCOLS");
8004 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8005 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8006 a_wmname = get_atom_from_string("WM_NAME");
8007 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8008 a_utf8_string = get_atom_from_string("UTF8_STRING");
8009 a_string = get_atom_from_string("STRING");
8010 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8011 a_swm_ws = get_atom_from_string("_SWM_WS");
8018 xcb_atom_t netwmcheck;
8019 xcb_window_t root, win;
8022 /* work around sun jdk bugs, code from wmname */
8023 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8025 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8026 for (i = 0; i < num_screens; i++) {
8027 root = screens[i].root;
8029 win = xcb_generate_id(conn);
8030 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
8031 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
8032 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
8033 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
8034 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
8036 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8037 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8038 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8039 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8040 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8041 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8046 shutdown_cleanup(void)
8050 /* disable alarm because the following code may not be interrupted */
8052 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8053 err(1, "can't disable alarm");
8061 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8062 for (i = 0; i < num_screens; ++i) {
8063 if (screens[i].bar_gc != 0)
8064 xcb_free_gc(conn, screens[i].bar_gc);
8065 if (!bar_font_legacy)
8066 XftColorFree(display, DefaultVisual(display, i),
8067 DefaultColormap(display, i), &bar_font_color);
8070 if (bar_font_legacy)
8071 XFreeFontSet(display, bar_fs);
8073 XftFontClose(display, bar_font);
8076 xcb_key_symbols_free(syms);
8078 xcb_disconnect(conn);
8082 event_error(xcb_generic_error_t *e)
8086 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8087 "resource_id: %u, minor_code: %u\n",
8088 xcb_event_get_error_label(e->error_code), e->error_code,
8089 xcb_event_get_request_label(e->major_code), e->major_code,
8090 e->sequence, e->resource_id, e->minor_code);
8094 event_handle(xcb_generic_event_t *evt)
8096 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8098 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
8099 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8100 XCB_EVENT_RESPONSE_TYPE(evt));
8103 #define EVENT(type, callback) case type: callback((void *)evt); return
8104 EVENT(0, event_error);
8105 EVENT(XCB_BUTTON_PRESS, buttonpress);
8106 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8107 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8108 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8109 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8110 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8111 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8112 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8113 /*EVENT(XCB_CREATE_NOTIFY, );*/
8114 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8115 EVENT(XCB_ENTER_NOTIFY, enternotify);
8116 EVENT(XCB_EXPOSE, expose);
8118 EVENT(XCB_FOCUS_IN, focusin);
8120 /*EVENT(XCB_FOCUS_OUT, );*/
8121 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8122 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8123 EVENT(XCB_KEY_PRESS, keypress);
8124 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8125 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8127 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8129 EVENT(XCB_MAP_NOTIFY, mapnotify);
8130 EVENT(XCB_MAP_REQUEST, maprequest);
8131 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8132 /*EVENT(XCB_MOTION_NOTIFY, );*/
8133 /*EVENT(XCB_NO_EXPOSURE, );*/
8134 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8135 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8136 /*EVENT(XCB_RESIZE_REQUEST, );*/
8137 /*EVENT(XCB_SELECTION_CLEAR, );*/
8138 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8139 /*EVENT(XCB_SELECTION_REQUEST, );*/
8140 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8141 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8144 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8145 screenchange((void *)evt);
8149 main(int argc, char *argv[])
8151 struct swm_region *r, *rr;
8152 struct ws_win *winfocus = NULL;
8154 char conf[PATH_MAX], *cfile = NULL;
8156 int xfd, i, num_screens;
8157 struct sigaction sact;
8158 xcb_generic_event_t *evt;
8162 /* suppress unused warning since var is needed */
8165 time_started = time(NULL);
8168 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8169 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8170 warnx("no locale support");
8172 /* handle some signals */
8173 bzero(&sact, sizeof(sact));
8174 sigemptyset(&sact.sa_mask);
8176 sact.sa_handler = sighdlr;
8177 sigaction(SIGINT, &sact, NULL);
8178 sigaction(SIGQUIT, &sact, NULL);
8179 sigaction(SIGTERM, &sact, NULL);
8180 sigaction(SIGHUP, &sact, NULL);
8182 sact.sa_handler = sighdlr;
8183 sact.sa_flags = SA_NOCLDSTOP;
8184 sigaction(SIGCHLD, &sact, NULL);
8186 if (!(display = XOpenDisplay(0)))
8187 errx(1, "can not open display");
8189 conn = XGetXCBConnection(display);
8190 if (xcb_connection_has_error(conn))
8191 errx(1, "can not get XCB connection");
8193 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8195 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8196 xfd = xcb_get_file_descriptor(conn);
8198 /* look for local and global conf file */
8199 pwd = getpwuid(getuid());
8201 errx(1, "invalid user: %d", getuid());
8203 xcb_grab_server(conn);
8206 /* flush all events */
8207 while ((evt = xcb_poll_for_event(conn))) {
8208 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8213 if (enable_wm() != 0)
8214 errx(1, "another window manager is currently running");
8225 for (i = 0; ; i++) {
8230 snprintf(conf, sizeof conf, "%s/.%s",
8231 pwd->pw_dir, SWM_CONF_FILE);
8235 snprintf(conf, sizeof conf, "/etc/%s",
8240 snprintf(conf, sizeof conf, "%s/.%s",
8241 pwd->pw_dir, SWM_CONF_FILE_OLD);
8245 snprintf(conf, sizeof conf, "/etc/%s",
8252 if (strlen(conf) && stat(conf, &sb) != -1)
8253 if (S_ISREG(sb.st_mode)) {
8260 /* load conf (if any) */
8262 conf_load(cfile, SWM_CONF_DEFAULT);
8265 /* set some values to work around bad programs */
8267 /* grab existing windows (before we build the bars) */
8270 if (getenv("SWM_STARTED") == NULL)
8271 setenv("SWM_STARTED", "YES", 1);
8273 /* setup all bars */
8274 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8275 for (i = 0; i < num_screens; i++)
8276 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8277 if (winfocus == NULL)
8278 winfocus = TAILQ_FIRST(&r->ws->winlist);
8287 xcb_ungrab_server(conn);
8291 while ((evt = xcb_poll_for_event(conn))) {
8298 /* if we are being restarted go focus on first window */
8300 rr = winfocus->ws->r;
8302 /* not a visible window */
8306 /* move pointer to first screen if multi screen */
8307 if (num_screens > 1 || outputs > 1)
8308 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
8309 rr->s[0].root, 0, 0, 0, 0, X(rr),
8310 Y(rr) + (bar_enabled ? bar_height : 0));
8312 a.id = SWM_ARG_ID_FOCUSCUR;
8322 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8323 if (errno != EINTR) {
8324 DNPRINTF(SWM_D_MISC, "select failed");
8326 if (restart_wm == 1)
8327 restart(NULL, NULL);
8328 if (search_resp == 1)