2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xft/Xft.h>
94 #include <X11/Xlib-xcb.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <xcb/randr.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr = SPECTRWM_BUILDSTR;
112 static const char *buildstr = SPECTRWM_VERSION;
116 # error XRandR versions less than 1.0 are not supported
121 #define SWM_XRR_HAS_CRTC
125 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
126 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
128 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
129 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
130 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
131 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
132 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
133 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
134 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
135 #define xcb_icccm_get_wm_class xcb_get_wm_class
136 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
137 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
138 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
139 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
140 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
141 #define xcb_icccm_get_wm_name xcb_get_wm_name
142 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
143 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
144 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
145 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
146 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
147 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
148 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
149 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
150 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
151 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
156 #define DPRINTF(x...) do { \
158 fprintf(stderr, x); \
160 #define DNPRINTF(n,x...) do { \
161 if (swm_debug & n) { \
162 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
163 fprintf(stderr, x); \
166 #define SWM_D_MISC 0x0001
167 #define SWM_D_EVENT 0x0002
168 #define SWM_D_WS 0x0004
169 #define SWM_D_FOCUS 0x0008
170 #define SWM_D_MOVE 0x0010
171 #define SWM_D_STACK 0x0020
172 #define SWM_D_MOUSE 0x0040
173 #define SWM_D_PROP 0x0080
174 #define SWM_D_CLASS 0x0100
175 #define SWM_D_KEY 0x0200
176 #define SWM_D_QUIRK 0x0400
177 #define SWM_D_SPAWN 0x0800
178 #define SWM_D_EVENTQ 0x1000
179 #define SWM_D_CONF 0x2000
180 #define SWM_D_BAR 0x4000
181 #define SWM_D_INIT 0x8000
183 u_int32_t swm_debug = 0
202 #define DPRINTF(x...)
203 #define DNPRINTF(n,x...)
206 /* convert 8-bit to 16-bit */
207 #define RGB_8_TO_16(col) ((col) << 8) + (col)
209 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
210 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
211 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
212 xrc.blue = RGB_8_TO_16((px) & 0xff); \
215 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
216 #define MODKEY Mod1Mask
217 #define CLEANMASK(mask) ((mask) & ~(numlockmask | LockMask))
218 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
219 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
220 #define SWM_PROPLEN (16)
221 #define SWM_FUNCNAME_LEN (32)
222 #define SWM_KEYS_LEN (255)
223 #define SWM_QUIRK_LEN (64)
224 #define X(r) (r)->g.x
225 #define Y(r) (r)->g.y
226 #define WIDTH(r) (r)->g.w
227 #define HEIGHT(r) (r)->g.h
228 #define BORDER(w) ((w)->bordered ? border_width : 0)
229 #define MAX_X(r) ((r)->g.x + (r)->g.w)
230 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
231 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
232 #define SH_MIN_W(w) (w)->sh.min_width
233 #define SH_MIN_H(w) (w)->sh.min_height
234 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
235 #define SH_MAX_W(w) (w)->sh.max_width
236 #define SH_MAX_H(w) (w)->sh.max_height
237 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
238 #define SH_INC_W(w) (w)->sh.width_inc
239 #define SH_INC_H(w) (w)->sh.height_inc
240 #define SWM_MAX_FONT_STEPS (3)
241 #define WINID(w) ((w) ? (w)->id : 0)
242 #define YESNO(x) ((x) ? "yes" : "no")
244 #define SWM_FOCUS_DEFAULT (0)
245 #define SWM_FOCUS_SYNERGY (1)
246 #define SWM_FOCUS_FOLLOW (2)
248 #define SWM_CONF_DEFAULT (0)
249 #define SWM_CONF_KEYMAPPING (1)
252 #define SWM_LIB "/usr/local/lib/libswmhack.so"
259 xcb_atom_t a_takefocus;
261 xcb_atom_t a_netwmname;
262 xcb_atom_t a_utf8_string;
264 xcb_atom_t a_swm_iconic;
266 volatile sig_atomic_t running = 1;
267 volatile sig_atomic_t restart_wm = 0;
269 /*int last_focus_event = FocusOut;*/
273 int xrandr_eventbase;
274 unsigned int numlockmask = 0;
277 xcb_connection_t *conn;
278 xcb_key_symbols_t *syms;
281 int cycle_visible = 0;
283 int font_adjusted = 0;
284 unsigned int mod_key = MODKEY;
287 struct swm_region *search_r;
288 int select_list_pipe[2];
289 int select_resp_pipe[2];
291 volatile sig_atomic_t search_resp;
292 int search_resp_action;
294 struct search_window {
295 TAILQ_ENTRY(search_window) entry;
299 xcb_window_t indicator;
301 TAILQ_HEAD(search_winlist, search_window);
302 struct search_winlist search_wl;
307 SWM_SEARCH_UNICONIFY,
308 SWM_SEARCH_NAME_WORKSPACE,
309 SWM_SEARCH_SEARCH_WORKSPACE,
310 SWM_SEARCH_SEARCH_WINDOW
313 #define SWM_STACK_TOP (0)
314 #define SWM_STACK_BOTTOM (1)
315 #define SWM_STACK_ABOVE (2)
316 #define SWM_STACK_BELOW (3)
319 double dialog_ratio = 0.6;
321 #define SWM_BAR_MAX (256)
322 #define SWM_BAR_JUSTIFY_LEFT (0)
323 #define SWM_BAR_JUSTIFY_CENTER (1)
324 #define SWM_BAR_JUSTIFY_RIGHT (2)
325 #define SWM_BAR_OFFSET (4)
326 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
327 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
328 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
329 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
330 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
332 char *bar_argv[] = { NULL, NULL };
334 unsigned char bar_ext[SWM_BAR_MAX];
335 char bar_vertext[SWM_BAR_MAX];
337 sig_atomic_t bar_alarm = 0;
340 int bar_border_width = 1;
341 int bar_at_bottom = 0;
343 int bar_extra_running = 0;
346 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
347 char *bar_format = NULL;
348 int stack_enabled = 1;
349 int clock_enabled = 1;
350 int urgent_enabled = 0;
351 char *clock_format = NULL;
352 int title_name_enabled = 0;
353 int title_class_enabled = 0;
354 int window_name_enabled = 0;
355 int focus_mode = SWM_FOCUS_DEFAULT;
356 int focus_close = SWM_STACK_BELOW;
357 int focus_close_wrap = 1;
358 int focus_default = SWM_STACK_TOP;
359 int spawn_position = SWM_STACK_TOP;
360 int disable_border = 0;
361 int border_width = 1;
362 int verbose_layout = 0;
367 XftColor bar_font_color;
370 /* layout manager data */
371 struct swm_geometry {
384 struct swm_geometry g;
387 /* virtual "screens" */
389 TAILQ_ENTRY(swm_region) entry;
390 struct swm_geometry g;
391 struct workspace *ws; /* current workspace on this region */
392 struct workspace *ws_prior; /* prior workspace on this region */
393 struct swm_screen *s; /* screen idx */
396 TAILQ_HEAD(swm_region_list, swm_region);
399 TAILQ_ENTRY(ws_win) entry;
401 xcb_window_t transient;
402 struct ws_win *child_trans; /* transient child window */
403 struct swm_geometry g; /* current geometry */
404 struct swm_geometry g_float; /* region coordinates */
405 int g_floatvalid; /* g_float geometry validity */
406 int floatmaxed; /* whether maxed by max_stack */
411 unsigned int ewmh_flags;
412 int font_size_boundary[SWM_MAX_FONT_STEPS];
418 unsigned long quirks;
419 struct workspace *ws; /* always valid */
420 struct swm_screen *s; /* always valid, never changes */
421 xcb_get_geometry_reply_t *wa;
423 xcb_icccm_get_wm_class_reply_t ch;
424 xcb_icccm_wm_hints_t hints;
426 TAILQ_HEAD(ws_win_list, ws_win);
430 TAILQ_ENTRY(pid_e) entry;
434 TAILQ_HEAD(pid_list, pid_e);
435 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
437 /* layout handlers */
439 void vertical_config(struct workspace *, int);
440 void vertical_stack(struct workspace *, struct swm_geometry *);
441 void horizontal_config(struct workspace *, int);
442 void horizontal_stack(struct workspace *, struct swm_geometry *);
443 void max_stack(struct workspace *, struct swm_geometry *);
444 void plain_stacker(struct workspace *);
445 void fancy_stacker(struct workspace *);
448 void (*l_stack)(struct workspace *, struct swm_geometry *);
449 void (*l_config)(struct workspace *, int);
451 #define SWM_L_FOCUSPREV (1<<0)
452 #define SWM_L_MAPONFOCUS (1<<1)
453 void (*l_string)(struct workspace *);
455 /* stack, configure */
456 { vertical_stack, vertical_config, 0, plain_stacker },
457 { horizontal_stack, horizontal_config, 0, plain_stacker },
459 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
460 { NULL, NULL, 0, NULL },
463 /* position of max_stack mode in the layouts array, index into layouts! */
464 #define SWM_V_STACK (0)
465 #define SWM_H_STACK (1)
466 #define SWM_MAX_STACK (2)
468 #define SWM_H_SLICE (32)
469 #define SWM_V_SLICE (32)
471 /* define work spaces */
473 int idx; /* workspace index */
474 char *name; /* workspace name */
475 int always_raise; /* raise windows on focus */
476 struct layout *cur_layout; /* current layout handlers */
477 struct ws_win *focus; /* may be NULL */
478 struct ws_win *focus_prev; /* may be NULL */
479 struct swm_region *r; /* may be NULL */
480 struct swm_region *old_r; /* may be NULL */
481 struct ws_win_list winlist; /* list of windows in ws */
482 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
483 char stacker[10]; /* display stacker and layout */
487 int horizontal_msize;
489 int horizontal_stacks;
500 SWM_S_COLOR_BAR_BORDER,
501 SWM_S_COLOR_BAR_FONT,
507 /* physical screen mapping */
508 #define SWM_WS_MAX (22) /* hard limit */
509 int workspace_limit = 10; /* soft limit */
512 int idx; /* screen index */
513 struct swm_region_list rl; /* list of regions on this screen */
514 struct swm_region_list orl; /* list of old regions */
516 struct workspace ws[SWM_WS_MAX];
522 } c[SWM_S_COLOR_MAX];
524 xcb_gcontext_t bar_gc;
526 struct swm_screen *screens;
528 /* args to functions */
531 #define SWM_ARG_ID_FOCUSNEXT (0)
532 #define SWM_ARG_ID_FOCUSPREV (1)
533 #define SWM_ARG_ID_FOCUSMAIN (2)
534 #define SWM_ARG_ID_FOCUSCUR (4)
535 #define SWM_ARG_ID_SWAPNEXT (10)
536 #define SWM_ARG_ID_SWAPPREV (11)
537 #define SWM_ARG_ID_SWAPMAIN (12)
538 #define SWM_ARG_ID_MOVELAST (13)
539 #define SWM_ARG_ID_MASTERSHRINK (20)
540 #define SWM_ARG_ID_MASTERGROW (21)
541 #define SWM_ARG_ID_MASTERADD (22)
542 #define SWM_ARG_ID_MASTERDEL (23)
543 #define SWM_ARG_ID_FLIPLAYOUT (24)
544 #define SWM_ARG_ID_STACKRESET (30)
545 #define SWM_ARG_ID_STACKINIT (31)
546 #define SWM_ARG_ID_CYCLEWS_UP (40)
547 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
548 #define SWM_ARG_ID_CYCLESC_UP (42)
549 #define SWM_ARG_ID_CYCLESC_DOWN (43)
550 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
551 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
552 #define SWM_ARG_ID_STACKINC (50)
553 #define SWM_ARG_ID_STACKDEC (51)
554 #define SWM_ARG_ID_SS_ALL (60)
555 #define SWM_ARG_ID_SS_WINDOW (61)
556 #define SWM_ARG_ID_DONTCENTER (70)
557 #define SWM_ARG_ID_CENTER (71)
558 #define SWM_ARG_ID_KILLWINDOW (80)
559 #define SWM_ARG_ID_DELETEWINDOW (81)
560 #define SWM_ARG_ID_WIDTHGROW (90)
561 #define SWM_ARG_ID_WIDTHSHRINK (91)
562 #define SWM_ARG_ID_HEIGHTGROW (92)
563 #define SWM_ARG_ID_HEIGHTSHRINK (93)
564 #define SWM_ARG_ID_MOVEUP (100)
565 #define SWM_ARG_ID_MOVEDOWN (101)
566 #define SWM_ARG_ID_MOVELEFT (102)
567 #define SWM_ARG_ID_MOVERIGHT (103)
573 TAILQ_ENTRY(quirk) entry;
577 #define SWM_Q_FLOAT (1<<0) /* float this window */
578 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
579 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
580 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
581 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
582 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
584 TAILQ_HEAD(quirk_list, quirk);
585 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
588 * Supported EWMH hints should be added to
589 * both the enum and the ewmh array
594 _NET_MOVERESIZE_WINDOW,
595 _NET_WM_ACTION_CLOSE,
596 _NET_WM_ACTION_FULLSCREEN,
598 _NET_WM_ACTION_RESIZE,
599 _NET_WM_ALLOWED_ACTIONS,
602 _NET_WM_STATE_FULLSCREEN,
603 _NET_WM_STATE_HIDDEN,
604 _NET_WM_STATE_MAXIMIZED_HORZ,
605 _NET_WM_STATE_MAXIMIZED_VERT,
606 _NET_WM_STATE_SKIP_PAGER,
607 _NET_WM_STATE_SKIP_TASKBAR,
609 _NET_WM_WINDOW_TYPE_DIALOG,
610 _NET_WM_WINDOW_TYPE_DOCK,
611 _NET_WM_WINDOW_TYPE_NORMAL,
612 _NET_WM_WINDOW_TYPE_SPLASH,
613 _NET_WM_WINDOW_TYPE_TOOLBAR,
614 _NET_WM_WINDOW_TYPE_UTILITY,
615 _SWM_WM_STATE_MANUAL,
622 } ewmh[SWM_EWMH_HINT_MAX] = {
623 /* must be in same order as in the enum */
624 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
625 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
626 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
627 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
628 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
629 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
630 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
631 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
632 {"_NET_WM_STATE", XCB_ATOM_NONE},
633 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
634 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
637 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
638 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
639 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
640 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
641 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
644 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
645 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
646 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
647 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
650 /* function prototypes */
651 void buttonpress(xcb_button_press_event_t *);
652 void clientmessage(xcb_client_message_event_t *);
653 int conf_load(char *, int);
654 void configurenotify(xcb_configure_notify_event_t *);
655 void configurerequest(xcb_configure_request_event_t *);
656 void constrain_window(struct ws_win *, struct swm_region *, int);
657 void destroynotify(xcb_destroy_notify_event_t *);
659 void enternotify(xcb_enter_notify_event_t *);
660 void event_error(xcb_generic_error_t *);
661 void event_handle(xcb_generic_event_t *);
662 void expose(xcb_expose_event_t *);
663 struct ws_win *find_window(xcb_window_t);
664 int floating_toggle_win(struct ws_win *);
665 void focus(struct swm_region *, union arg *);
666 void focus_magic(struct ws_win *);
668 void focusin(xcb_focus_in_event_t *);
670 xcb_atom_t get_atom_from_string(const char *);
672 char *get_atom_name(xcb_atom_t);
673 char *get_notify_detail_label(uint8_t);
674 char *get_notify_mode_label(uint8_t);
676 xcb_screen_t *get_screen(int);
677 char *get_win_name(xcb_window_t);
678 uint32_t getstate(xcb_window_t);
679 void grabbuttons(struct ws_win *, int);
680 void keypress(xcb_key_press_event_t *);
682 void leavenotify(xcb_leave_notify_event_t *);
684 void map_window_raised(xcb_window_t);
685 void mapnotify(xcb_map_notify_event_t *);
686 void mappingnotify(xcb_mapping_notify_event_t *);
687 void maprequest(xcb_map_request_event_t *);
688 void new_region(struct swm_screen *, int, int, int, int);
689 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
690 void propertynotify(xcb_property_notify_event_t *);
691 void spawn_select(struct swm_region *, union arg *, char *, int *);
692 void screenchange(xcb_randr_screen_change_notify_event_t *);
693 void store_float_geom(struct ws_win *, struct swm_region *);
694 void unmanage_window(struct ws_win *);
695 void unmapnotify(xcb_unmap_notify_event_t *);
696 void update_window(struct ws_win *);
697 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
700 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
702 unsigned int tmpr, tmpg, tmpb;
704 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
707 *rr = RGB_8_TO_16(tmpr);
708 *gg = RGB_8_TO_16(tmpg);
709 *bb = RGB_8_TO_16(tmpb);
715 get_screen(int screen)
717 xcb_screen_iterator_t i;
719 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
720 for (; i.rem; --screen, xcb_screen_next(&i))
730 xcb_get_input_focus_cookie_t c;
731 xcb_get_input_focus_reply_t *r;
734 c = xcb_get_input_focus(conn);
736 r = xcb_get_input_focus_reply(conn, c, NULL);
742 map_window_raised(xcb_window_t win)
744 uint32_t val = XCB_STACK_MODE_ABOVE;
746 xcb_configure_window(conn, win,
747 XCB_CONFIG_WINDOW_STACK_MODE, &val);
749 xcb_map_window(conn, win);
753 get_atom_from_string(const char *str)
755 xcb_intern_atom_cookie_t c;
756 xcb_intern_atom_reply_t *r;
759 c = xcb_intern_atom(conn, 0, strlen(str), str);
760 r = xcb_intern_atom_reply(conn, c, NULL);
768 return (XCB_ATOM_NONE);
772 update_iconic(struct ws_win *win, int newv)
779 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
780 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
782 xcb_delete_property(conn, win->id, a_swm_iconic);
786 get_iconic(struct ws_win *win)
789 xcb_get_property_reply_t *pr = NULL;
792 pr = xcb_get_property_reply(conn,
793 xcb_get_property(conn, 0, win->id, a_swm_iconic,
794 XCB_ATOM_INTEGER, 0, 1), NULL);
797 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
799 v = *((int32_t *)xcb_get_property_value(pr));
810 int i, j, num_screens;
812 sup_list = get_atom_from_string("_NET_SUPPORTED");
814 for (i = 0; i < LENGTH(ewmh); i++)
815 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
817 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
818 for (i = 0; i < num_screens; i++) {
819 /* Support check window will be created by workaround(). */
821 /* Report supported atoms */
822 xcb_delete_property(conn, screens[i].root, sup_list);
823 for (j = 0; j < LENGTH(ewmh); j++)
824 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
825 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
834 xcb_atom_t sup_check, sup_list;
836 xcb_get_property_cookie_t pc;
837 xcb_get_property_reply_t *pr;
839 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
840 sup_list = get_atom_from_string("_NET_SUPPORTED");
841 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
843 for (i = 0; i < num_screens; i++) {
844 /* Get the support check window and destroy it */
845 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
846 XCB_ATOM_WINDOW, 0, 1);
847 pr = xcb_get_property_reply(conn, pc, NULL);
850 if (pr->format == sup_check) {
851 id = *((xcb_window_t *)xcb_get_property_value(pr));
853 xcb_destroy_window(conn, id);
854 xcb_delete_property(conn, screens[i].root, sup_check);
855 xcb_delete_property(conn, screens[i].root, sup_list);
862 ewmh_autoquirk(struct ws_win *win)
866 xcb_get_property_cookie_t c;
867 xcb_get_property_reply_t *r;
869 c = xcb_get_property(conn, 0, win->id,
870 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
871 r = xcb_get_property_reply(conn, c, NULL);
875 n = xcb_get_property_value_length(r);
876 type = xcb_get_property_value(r);
878 for (i = 0; i < n; i++) {
879 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
881 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
882 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
883 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
885 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
888 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
889 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
891 win->quirks = SWM_Q_FLOAT;
898 #define SWM_EWMH_ACTION_COUNT_MAX (6)
899 #define EWMH_F_FULLSCREEN (1<<0)
900 #define EWMH_F_ABOVE (1<<1)
901 #define EWMH_F_HIDDEN (1<<2)
902 #define EWMH_F_SKIP_PAGER (1<<3)
903 #define EWMH_F_SKIP_TASKBAR (1<<4)
904 #define SWM_F_MANUAL (1<<5)
907 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
915 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
916 "fullscreen %s\n", win->id, YESNO(fs));
919 if (!win->g_floatvalid)
920 store_float_geom(win, win->ws->r);
922 win->g = win->ws->r->g;
925 if (win->g_floatvalid) {
926 /* refloat at last floating relative position */
927 win->g = win->g_float;
928 X(win) += X(win->ws->r);
929 Y(win) += Y(win->ws->r);
937 ewmh_update_actions(struct ws_win *win)
939 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
945 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
948 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
949 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
952 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
953 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
956 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
957 #define _NET_WM_STATE_ADD 1 /* add/set property */
958 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
961 ewmh_update_win_state(struct ws_win *win, long state, long action)
963 unsigned int mask = 0;
964 unsigned int changed = 0;
965 unsigned int orig_flags;
970 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
971 "action: %ld\n", win->id, state, action);
973 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
974 mask = EWMH_F_FULLSCREEN;
975 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
977 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
979 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
980 mask = EWMH_F_SKIP_PAGER;
981 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
982 mask = EWMH_F_SKIP_TASKBAR;
985 orig_flags = win->ewmh_flags;
988 case _NET_WM_STATE_REMOVE:
989 win->ewmh_flags &= ~mask;
991 case _NET_WM_STATE_ADD:
992 win->ewmh_flags |= mask;
994 case _NET_WM_STATE_TOGGLE:
995 win->ewmh_flags ^= mask;
999 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1001 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1003 if (!floating_toggle_win(win))
1004 win->ewmh_flags = orig_flags; /* revert */
1005 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1007 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1008 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1010 if (!ewmh_set_win_fullscreen(win,
1011 win->ewmh_flags & EWMH_F_FULLSCREEN))
1012 win->ewmh_flags = orig_flags; /* revert */
1014 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1016 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1017 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1018 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1019 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1020 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1021 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1022 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1023 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1024 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1025 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1026 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1027 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1028 if (win->ewmh_flags & EWMH_F_ABOVE)
1029 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1030 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1031 &ewmh[_NET_WM_STATE_ABOVE].atom);
1032 if (win->ewmh_flags & SWM_F_MANUAL)
1033 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1034 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1035 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1039 ewmh_get_win_state(struct ws_win *win)
1042 xcb_get_property_cookie_t c;
1043 xcb_get_property_reply_t *r;
1049 win->ewmh_flags = 0;
1051 win->ewmh_flags |= EWMH_F_ABOVE;
1053 win->ewmh_flags |= SWM_F_MANUAL;
1055 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1056 XCB_ATOM_ATOM, 0, UINT32_MAX);
1057 r = xcb_get_property_reply(conn, c, NULL);
1061 states = xcb_get_property_value(r);
1062 n = xcb_get_property_value_length(r);
1064 for (i = 0; i < n; i++)
1065 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1073 dumpwins(struct swm_region *r, union arg *args)
1077 xcb_get_window_attributes_cookie_t c;
1078 xcb_get_window_attributes_reply_t *wa;
1080 /* suppress unused warning since var is needed */
1083 if (r->ws == NULL) {
1084 warnx("dumpwins: invalid workspace");
1088 warnx("=== managed window list ws %02d ===", r->ws->idx);
1089 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1090 state = getstate(win->id);
1091 c = xcb_get_window_attributes(conn, win->id);
1092 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1094 warnx("window: 0x%x, map_state: %d, state: %u, "
1095 "transient: 0x%x", win->id, wa->map_state,
1096 state, win->transient);
1099 warnx("window: 0x%x, failed xcb_get_window_attributes",
1103 warnx("===== unmanaged window list =====");
1104 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1105 state = getstate(win->id);
1106 c = xcb_get_window_attributes(conn, win->id);
1107 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1109 warnx("window: 0x%x, map_state: %d, state: %u, "
1110 "transient: 0x%x", win->id, wa->map_state,
1111 state, win->transient);
1114 warnx("window: 0x%x, failed xcb_get_window_attributes",
1118 warnx("=================================");
1122 dumpwins(struct swm_region *r, union arg *args)
1125 #endif /* SWM_DEBUG */
1130 int saved_errno, status;
1133 saved_errno = errno;
1137 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1142 if (errno != ECHILD)
1143 warn("sighdlr: waitpid");
1144 #endif /* SWM_DEBUG */
1147 if (pid == searchpid)
1151 if (WIFEXITED(status)) {
1152 if (WEXITSTATUS(status) != 0)
1153 warnx("sighdlr: child exit status: %d",
1154 WEXITSTATUS(status));
1156 warnx("sighdlr: child is terminated "
1158 #endif /* SWM_DEBUG */
1172 errno = saved_errno;
1178 struct pid_e *p = NULL;
1180 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1185 TAILQ_FOREACH(p, &pidlist, entry) {
1194 name_to_pixel(const char *colorname)
1196 uint32_t result = 0;
1197 char cname[32] = "#";
1198 xcb_screen_t *screen;
1199 xcb_colormap_t cmap;
1200 xcb_alloc_color_reply_t *cr;
1201 xcb_alloc_named_color_reply_t *nr;
1202 uint16_t rr, gg, bb;
1204 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1205 cmap = screen->default_colormap;
1207 /* color is in format rgb://rr/gg/bb */
1208 if (strncmp(colorname, "rgb:", 4) == 0) {
1209 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1210 warnx("could not parse rgb %s", colorname);
1212 cr = xcb_alloc_color_reply(conn,
1213 xcb_alloc_color(conn, cmap, rr, gg, bb),
1219 warnx("color '%s' not found", colorname);
1222 nr = xcb_alloc_named_color_reply(conn,
1223 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1226 strlcat(cname, colorname + 2, sizeof cname - 1);
1227 nr = xcb_alloc_named_color_reply(conn,
1228 xcb_alloc_named_color(conn, cmap, strlen(cname),
1235 warnx("color '%s' not found", colorname);
1242 setscreencolor(char *val, int i, int c)
1246 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1247 if (i > 0 && i <= num_screens) {
1248 screens[i - 1].c[c].pixel = name_to_pixel(val);
1249 free(screens[i - 1].c[c].name);
1250 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1252 } else if (i == -1) {
1253 for (i = 0; i < num_screens; i++) {
1254 screens[i].c[c].pixel = name_to_pixel(val);
1255 free(screens[i].c[c].name);
1256 if ((screens[i].c[c].name = strdup(val)) == NULL)
1260 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1265 fancy_stacker(struct workspace *ws)
1267 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1268 if (ws->cur_layout->l_stack == vertical_stack)
1269 snprintf(ws->stacker, sizeof ws->stacker,
1270 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1271 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1272 if (ws->cur_layout->l_stack == horizontal_stack)
1273 snprintf(ws->stacker, sizeof ws->stacker,
1274 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1275 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1279 plain_stacker(struct workspace *ws)
1281 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1282 if (ws->cur_layout->l_stack == vertical_stack)
1283 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1284 sizeof ws->stacker);
1285 if (ws->cur_layout->l_stack == horizontal_stack)
1286 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1287 sizeof ws->stacker);
1291 custom_region(char *val)
1293 unsigned int x, y, w, h;
1294 int sidx, num_screens;
1295 xcb_screen_t *screen;
1297 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1298 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1299 errx(1, "invalid custom region, "
1300 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1301 if (sidx < 1 || sidx > num_screens)
1302 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1306 screen = get_screen(sidx);
1308 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1310 if (x > screen->width_in_pixels ||
1311 y > screen->height_in_pixels ||
1312 w + x > screen->width_in_pixels ||
1313 h + y > screen->height_in_pixels) {
1314 warnx("ignoring region %ux%u+%u+%u - not within screen "
1315 "boundaries (%ux%u)", w, h, x, y,
1316 screen->width_in_pixels, screen->height_in_pixels);
1320 new_region(&screens[sidx], x, y, w, h);
1324 socket_setnonblock(int fd)
1328 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1329 err(1, "fcntl F_GETFL");
1330 flags |= O_NONBLOCK;
1331 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1332 err(1, "fcntl F_SETFL");
1336 bar_print(struct swm_region *r, const char *s)
1339 xcb_rectangle_t rect;
1347 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1349 switch (bar_justify) {
1350 case SWM_BAR_JUSTIFY_LEFT:
1353 case SWM_BAR_JUSTIFY_CENTER:
1354 x = (WIDTH(r) - info.width) / 2;
1356 case SWM_BAR_JUSTIFY_RIGHT:
1357 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1361 if (x < SWM_BAR_OFFSET)
1366 rect.width = WIDTH(r->bar);
1367 rect.height = HEIGHT(r->bar);
1369 /* clear back buffer */
1370 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1371 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1372 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1373 sizeof(rect), &rect);
1375 /* draw back buffer */
1376 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1377 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1378 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].pixel;
1379 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1381 draw = XftDrawCreate(display, r->bar->buffer,
1382 DefaultVisual(display, r->s->idx),
1383 DefaultColormap(display, r->s->idx));
1385 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1386 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1389 XftDrawDestroy(draw);
1392 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1393 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1397 bar_extra_stop(void)
1401 bzero(bar_pipe, sizeof bar_pipe);
1404 kill(bar_pid, SIGTERM);
1407 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1412 bar_class_name(char *s, size_t sz, struct swm_region *r)
1414 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1416 if (r->ws->focus->ch.class_name != NULL)
1417 strlcat(s, r->ws->focus->ch.class_name, sz);
1421 bar_title_name(char *s, size_t sz, struct swm_region *r)
1423 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1425 if (r->ws->focus->ch.instance_name != NULL)
1426 strlcat(s, r->ws->focus->ch.instance_name, sz);
1430 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1432 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1435 bar_class_name(s, sz, r);
1436 strlcat(s, ":", sz);
1437 bar_title_name(s, sz, r);
1441 bar_window_float(char *s, size_t sz, struct swm_region *r)
1443 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1445 if (r->ws->focus->floating)
1446 strlcat(s, "(f)", sz);
1450 bar_window_name(char *s, size_t sz, struct swm_region *r)
1454 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1456 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1459 strlcat(s, title, sz);
1463 int urgent[SWM_WS_MAX];
1465 bar_urgent(char *s, size_t sz)
1468 int i, j, num_screens;
1470 xcb_get_property_cookie_t c;
1471 xcb_icccm_wm_hints_t hints;
1473 for (i = 0; i < workspace_limit; i++)
1476 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1477 for (i = 0; i < num_screens; i++)
1478 for (j = 0; j < workspace_limit; j++)
1479 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1480 c = xcb_icccm_get_wm_hints(conn, win->id);
1481 if (xcb_icccm_get_wm_hints_reply(conn, c,
1484 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1488 for (i = 0; i < workspace_limit; i++) {
1490 snprintf(b, sizeof b, "%d ", i + 1);
1492 snprintf(b, sizeof b, "- ");
1498 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1500 if (r == NULL || r->ws == NULL)
1502 if (r->ws->name != NULL)
1503 strlcat(s, r->ws->name, sz);
1506 /* build the default bar format according to the defined enabled options */
1508 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1510 /* if format provided, just copy the buffers */
1511 if (bar_format != NULL) {
1512 strlcpy(fmtnew, fmtexp, sz);
1516 /* reset the output buffer */
1519 strlcat(fmtnew, "+N:+I ", sz);
1521 strlcat(fmtnew, "+S", sz);
1522 strlcat(fmtnew, " ", sz);
1524 /* only show the workspace name if there's actually one */
1525 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1526 strlcat(fmtnew, "<+D>", sz);
1527 strlcat(fmtnew, "+3<", sz);
1529 if (clock_enabled) {
1530 strlcat(fmtnew, fmtexp, sz);
1531 strlcat(fmtnew, "+4<", sz);
1534 /* bar_urgent already adds the space before the last asterisk */
1536 strlcat(fmtnew, "* +U*+4<", sz);
1538 if (title_class_enabled) {
1539 strlcat(fmtnew, "+C", sz);
1540 if (title_name_enabled == 0)
1541 strlcat(fmtnew, "+4<", sz);
1544 /* checks needed by the colon and floating strlcat(3) calls below */
1545 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1546 if (title_name_enabled) {
1547 if (title_class_enabled)
1548 strlcat(fmtnew, ":", sz);
1549 strlcat(fmtnew, "+T+4<", sz);
1551 if (window_name_enabled) {
1552 if (r->ws->focus->floating)
1553 strlcat(fmtnew, "+F ", sz);
1554 strlcat(fmtnew, "+64W ", sz);
1558 /* finally add the action script output and the version */
1559 strlcat(fmtnew, "+4<+A+4<+V", sz);
1563 bar_replace_pad(char *tmp, int *limit, size_t sz)
1565 /* special case; no limit given, pad one space, instead */
1566 if (*limit == (int)sz - 1)
1568 snprintf(tmp, sz, "%*s", *limit, " ");
1571 /* replaces the bar format character sequences (like in tmux(1)) */
1573 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1577 char tmp[SWM_BAR_MAX];
1581 /* reset strlcat(3) buffer */
1584 /* get number, if any */
1587 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1588 limit = sizeof tmp - 1;
1589 if (limit <= 0 || limit >= (int)sizeof tmp)
1590 limit = sizeof tmp - 1;
1592 /* there is nothing to replace (ie EOL) */
1599 bar_replace_pad(tmp, &limit, sizeof tmp);
1602 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1605 bar_class_name(tmp, sizeof tmp, r);
1608 bar_workspace_name(tmp, sizeof tmp, r);
1611 bar_window_float(tmp, sizeof tmp, r);
1614 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1617 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1620 bar_class_title_name(tmp, sizeof tmp, r);
1623 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1626 bar_title_name(tmp, sizeof tmp, r);
1629 bar_urgent(tmp, sizeof tmp);
1632 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1635 bar_window_name(tmp, sizeof tmp, r);
1638 /* unknown character sequence; copy as-is */
1639 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1645 if ((int)len < limit)
1647 while (limit-- > 0) {
1648 if (*offrep >= sz - 1)
1650 fmtrep[(*offrep)++] = *ptr++;
1658 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1663 while (*fmt != '\0') {
1665 /* skip ordinary characters */
1668 fmtrep[off++] = *fmt++;
1672 /* character sequence found; replace it */
1673 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1682 bar_fmt_expand(char *fmtexp, size_t sz)
1689 /* start by grabbing the current time and date */
1691 localtime_r(&tmt, &tm);
1693 /* figure out what to expand */
1694 if (bar_format != NULL)
1696 else if (bar_format == NULL && clock_enabled)
1698 /* if nothing to expand bail out */
1704 /* copy as-is, just in case the format shouldn't be expanded below */
1705 strlcpy(fmtexp, fmt, sz);
1706 /* finally pass the string through strftime(3) */
1707 #ifndef SWM_DENY_CLOCK_FORMAT
1708 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1709 warnx("format too long");
1717 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1718 char fmtrep[SWM_BAR_MAX];
1720 struct swm_region *r;
1722 /* expand the format by first passing it through strftime(3) */
1723 bar_fmt_expand(fmtexp, sizeof fmtexp);
1725 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1726 for (i = 0; i < num_screens; i++) {
1727 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1730 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1731 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1732 bar_print(r, fmtrep);
1743 if (bar_enabled == 0)
1745 if (bar_extra && bar_extra_running) {
1746 /* ignore short reads; it'll correct itself */
1747 while ((b = fgetln(stdin, &len)) != NULL)
1748 if (b && b[len - 1] == '\n') {
1750 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1752 if (b == NULL && errno != EAGAIN) {
1753 warn("bar_update: bar_extra failed");
1757 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1766 /* suppress unused warning since var is needed */
1773 bar_toggle(struct swm_region *r, union arg *args)
1775 struct swm_region *tmpr;
1778 /* suppress unused warnings since vars are needed */
1782 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1784 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1786 for (i = 0; i < num_screens; i++)
1787 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1789 xcb_unmap_window(conn, tmpr->bar->id);
1791 for (i = 0; i < num_screens; i++)
1792 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1794 map_window_raised(tmpr->bar->id);
1797 bar_enabled = !bar_enabled;
1800 /* must be after stack */
1809 struct swm_region *r;
1813 /* do this here because the conf file is in memory */
1814 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1815 /* launch external status app */
1816 bar_extra_running = 1;
1817 if (pipe(bar_pipe) == -1)
1818 err(1, "pipe error");
1819 socket_setnonblock(bar_pipe[0]);
1820 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1821 if (dup2(bar_pipe[0], 0) == -1)
1823 if (dup2(bar_pipe[1], 1) == -1)
1825 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1826 err(1, "could not disable SIGPIPE");
1827 switch (bar_pid = fork()) {
1829 err(1, "cannot fork");
1833 execvp(bar_argv[0], bar_argv);
1834 err(1, "%s external app failed", bar_argv[0]);
1836 default: /* parent */
1842 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1843 for (i = 0; i < num_screens; i++)
1844 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1847 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1848 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1849 xcb_change_window_attributes(conn, r->bar->id,
1850 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1856 bar_setup(struct swm_region *r)
1858 char *font, *fontpos, *dup, *search;
1860 xcb_screen_t *screen = get_screen(r->s->idx);
1867 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1868 err(1, "bar_setup: calloc: failed to allocate memory.");
1870 if (bar_font == NULL) {
1871 if ((dup = strdup(bar_fonts)) == NULL)
1872 errx(1, "insufficient memory.");
1874 while ((font = strsep(&search, ",")) != NULL) {
1878 DNPRINTF(SWM_D_INIT, "bar_setup: try font %s\n", font);
1882 while ((fontpos = index(fontpos, '-'))) {
1888 bar_font = XftFontOpenXlfd(display, r->s->idx,
1891 bar_font = XftFontOpenName(display, r->s->idx,
1894 warnx("unable to load font %s", font);
1897 DNPRINTF(SWM_D_INIT, "successfully opened "
1905 if (bar_font == NULL)
1906 errx(1, "unable to open a font");
1908 bar_height = bar_font->height + 2 * bar_border_width;
1913 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
1915 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
1916 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
1917 warn("unable to allocate Xft color");
1920 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1921 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1922 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1924 r->bar->id = xcb_generate_id(conn);
1925 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1926 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
1927 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1929 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1930 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1931 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1932 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1933 | XCB_CW_EVENT_MASK, wa);
1935 r->bar->buffer = xcb_generate_id(conn);
1936 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1937 WIDTH(r->bar), HEIGHT(r->bar));
1939 xcb_randr_select_input(conn, r->bar->id,
1940 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1943 map_window_raised(r->bar->id);
1945 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1946 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1949 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1950 err(1, "could not install bar_signal");
1955 bar_cleanup(struct swm_region *r)
1959 xcb_destroy_window(conn, r->bar->id);
1960 xcb_free_pixmap(conn, r->bar->buffer);
1966 set_win_state(struct ws_win *win, uint16_t state)
1968 uint16_t data[2] = { state, XCB_ATOM_NONE };
1970 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
1975 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
1976 a_state, 32, 2, data);
1980 getstate(xcb_window_t w)
1982 uint32_t result = 0;
1983 xcb_get_property_cookie_t c;
1984 xcb_get_property_reply_t *r;
1986 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
1987 r = xcb_get_property_reply(conn, c, NULL);
1990 if (r->type == a_state && r->format == 32 && r->length == 2)
1991 result = *((uint32_t *)xcb_get_property_value(r));
1995 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2001 version(struct swm_region *r, union arg *args)
2003 /* suppress unused warnings since vars are needed */
2007 bar_version = !bar_version;
2009 snprintf(bar_vertext, sizeof bar_vertext,
2010 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2012 strlcpy(bar_vertext, "", sizeof bar_vertext);
2019 client_msg(struct ws_win *win, xcb_atom_t a)
2021 xcb_client_message_event_t ev;
2026 bzero(&ev, sizeof ev);
2027 ev.response_type = XCB_CLIENT_MESSAGE;
2028 ev.window = win->id;
2031 ev.data.data32[0] = a;
2032 ev.data.data32[1] = XCB_CURRENT_TIME;
2034 xcb_send_event(conn, 0, win->id,
2035 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2038 /* synthetic response to a ConfigureRequest when not making a change */
2040 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2042 xcb_configure_notify_event_t ce;
2047 /* send notification of unchanged state. */
2048 bzero(&ce, sizeof(ce));
2049 ce.response_type = XCB_CONFIGURE_NOTIFY;
2052 ce.width = WIDTH(win);
2053 ce.height = HEIGHT(win);
2054 ce.override_redirect = 0;
2059 ce.window = win->id;
2060 ce.border_width = BORDER(win);
2061 ce.above_sibling = XCB_WINDOW_NONE;
2064 ce.event = ev->window;
2065 ce.window = ev->window;
2067 /* make response appear more WM_SIZE_HINTS-compliant */
2069 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2070 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2071 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2072 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2073 SH_INC_W(win), SH_INC_H(win));
2077 /* the hint may be set... to 0! */
2078 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2079 ce.width = SH_MIN_W(win);
2080 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2081 ce.height = SH_MIN_H(win);
2086 /* may also be advertized as 0 */
2087 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2088 ce.width = SH_MAX_W(win);
2089 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2090 ce.height = SH_MAX_H(win);
2093 /* resize increment. */
2095 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2096 ce.width -= (ce.width - SH_MIN_W(win)) %
2098 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2099 ce.height -= (ce.height - SH_MIN_H(win)) %
2103 /* adjust x and y for requested border_width. */
2104 ce.x += BORDER(win) - ev->border_width;
2105 ce.y += BORDER(win) - ev->border_width;
2106 ce.border_width = ev->border_width;
2107 ce.above_sibling = ev->sibling;
2110 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2111 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2112 ce.y, ce.width, ce.height, ce.border_width);
2114 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2119 count_win(struct workspace *ws, int count_transient)
2124 TAILQ_FOREACH(win, &ws->winlist, entry) {
2125 if (count_transient == 0 && win->floating)
2127 if (count_transient == 0 && win->transient)
2133 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2139 quit(struct swm_region *r, union arg *args)
2141 /* suppress unused warnings since vars are needed */
2145 DNPRINTF(SWM_D_MISC, "quit\n");
2150 unmap_window(struct ws_win *win)
2152 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2157 /* don't unmap again */
2158 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2161 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2163 xcb_unmap_window(conn, win->id);
2164 xcb_change_window_attributes(conn, win->id,
2165 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2172 int i, j, num_screens;
2174 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2175 for (i = 0; i < num_screens; i++)
2176 for (j = 0; j < workspace_limit; j++)
2177 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2182 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2184 xcb_key_press_event_t event;
2185 xcb_keycode_t *keycode;
2190 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2192 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2195 bzero(&event, sizeof(event));
2196 event.event = win->id;
2197 event.root = win->s->root;
2198 event.child = XCB_WINDOW_NONE;
2199 event.time = XCB_CURRENT_TIME;
2200 event.event_x = X(win);
2201 event.event_y = Y(win);
2204 event.same_screen = 1;
2205 event.detail = *keycode;
2206 event.state = modifiers;
2208 event.response_type = XCB_KEY_PRESS;
2209 xcb_send_event(conn, 1, win->id,
2210 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2212 event.response_type = XCB_KEY_RELEASE;
2213 xcb_send_event(conn, 1, win->id,
2214 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2220 restart(struct swm_region *r, union arg *args)
2222 /* suppress unused warning since var is needed */
2225 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2227 /* disable alarm because the following code may not be interrupted */
2229 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2230 err(1, "can't disable alarm");
2236 XftFontClose(display, bar_font);
2237 XftColorFree(display, DefaultVisual(display, r->s->idx),
2238 DefaultColormap(display, r->s->idx), &bar_font_color);
2239 xcb_key_symbols_free(syms);
2241 xcb_disconnect(conn);
2243 execvp(start_argv[0], start_argv);
2244 warn("execvp failed");
2249 root_to_region(xcb_window_t root)
2251 struct swm_region *r = NULL;
2253 xcb_query_pointer_reply_t *qpr;
2255 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2257 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2258 for (i = 0; i < num_screens; i++)
2259 if (screens[i].root == root)
2262 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2263 screens[i].root), NULL);
2266 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2267 qpr->root_x, qpr->root_y);
2268 /* choose a region based on pointer location */
2269 TAILQ_FOREACH(r, &screens[i].rl, entry)
2270 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2271 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2277 r = TAILQ_FIRST(&screens[i].rl);
2283 find_unmanaged_window(xcb_window_t id)
2286 int i, j, num_screens;
2288 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2289 for (i = 0; i < num_screens; i++)
2290 for (j = 0; j < workspace_limit; j++)
2291 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2299 find_window(xcb_window_t id)
2302 int i, j, num_screens;
2303 xcb_query_tree_reply_t *r;
2305 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2306 for (i = 0; i < num_screens; i++)
2307 for (j = 0; j < workspace_limit; j++)
2308 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2312 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2316 /* if we were looking for the parent return that window instead */
2317 if (r->parent == 0 || r->root == r->parent) {
2322 /* look for parent */
2323 for (i = 0; i < num_screens; i++)
2324 for (j = 0; j < workspace_limit; j++)
2325 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2326 if (r->parent == win->id) {
2336 spawn(int ws_idx, union arg *args, int close_fd)
2341 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2343 close(xcb_get_file_descriptor(conn));
2345 setenv("LD_PRELOAD", SWM_LIB, 1);
2347 if (asprintf(&ret, "%d", ws_idx) == -1) {
2348 warn("spawn: asprintf SWM_WS");
2351 setenv("_SWM_WS", ret, 1);
2355 if (asprintf(&ret, "%d", getpid()) == -1) {
2356 warn("spawn: asprintf _SWM_PID");
2359 setenv("_SWM_PID", ret, 1);
2363 if (setsid() == -1) {
2364 warn("spawn: setsid");
2370 * close stdin and stdout to prevent interaction between apps
2371 * and the baraction script
2372 * leave stderr open to record errors
2374 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2375 warn("spawn: open");
2378 dup2(fd, STDIN_FILENO);
2379 dup2(fd, STDOUT_FILENO);
2384 execvp(args->argv[0], args->argv);
2386 warn("spawn: execvp");
2391 kill_refs(struct ws_win *win)
2393 int i, x, num_screens;
2394 struct swm_region *r;
2395 struct workspace *ws;
2400 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2401 for (i = 0; i < num_screens; i++)
2402 TAILQ_FOREACH(r, &screens[i].rl, entry)
2403 for (x = 0; x < workspace_limit; x++) {
2405 if (win == ws->focus)
2407 if (win == ws->focus_prev)
2408 ws->focus_prev = NULL;
2413 validate_win(struct ws_win *testwin)
2416 struct workspace *ws;
2417 struct swm_region *r;
2418 int i, x, num_screens;
2420 if (testwin == NULL)
2423 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2424 for (i = 0; i < num_screens; i++)
2425 TAILQ_FOREACH(r, &screens[i].rl, entry)
2426 for (x = 0; x < workspace_limit; x++) {
2428 TAILQ_FOREACH(win, &ws->winlist, entry)
2436 validate_ws(struct workspace *testws)
2438 struct swm_region *r;
2439 struct workspace *ws;
2440 int i, x, num_screens;
2442 /* validate all ws */
2443 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2444 for (i = 0; i < num_screens; i++)
2445 TAILQ_FOREACH(r, &screens[i].rl, entry)
2446 for (x = 0; x < workspace_limit; x++) {
2455 unfocus_win(struct ws_win *win)
2457 xcb_window_t none = XCB_WINDOW_NONE;
2459 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2463 if (win->ws == NULL)
2466 if (validate_ws(win->ws))
2467 return; /* XXX this gets hit with thunderbird, needs fixing */
2469 if (win->ws->r == NULL)
2472 if (validate_win(win)) {
2477 if (win->ws->focus == win) {
2478 win->ws->focus = NULL;
2479 win->ws->focus_prev = win;
2482 if (validate_win(win->ws->focus)) {
2483 kill_refs(win->ws->focus);
2484 win->ws->focus = NULL;
2486 if (validate_win(win->ws->focus_prev)) {
2487 kill_refs(win->ws->focus_prev);
2488 win->ws->focus_prev = NULL;
2491 grabbuttons(win, 0);
2492 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2493 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2494 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2495 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2502 int i, j, num_screens;
2504 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2506 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2507 for (i = 0; i < num_screens; i++)
2508 for (j = 0; j < workspace_limit; j++)
2509 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2514 focus_win(struct ws_win *win)
2516 struct ws_win *cfw = NULL;
2517 xcb_get_input_focus_reply_t *r;
2518 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2520 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2524 if (win->ws == NULL)
2527 if (validate_ws(win->ws))
2528 return; /* XXX this gets hit with thunderbird, needs fixing */
2530 if (validate_win(win)) {
2535 if (validate_win(win)) {
2540 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2542 cur_focus = r->focus;
2545 if ((cfw = find_window(cur_focus)) != NULL)
2549 /* use larger hammer since the window was killed somehow */
2550 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2551 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2552 xcb_change_window_attributes(conn, cfw->id,
2553 XCB_CW_BORDER_PIXEL,
2554 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2558 win->ws->focus = win;
2560 if (win->ws->r != NULL) {
2562 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2563 win->id, XCB_CURRENT_TIME);
2564 grabbuttons(win, 1);
2565 xcb_change_window_attributes(conn, win->id,
2566 XCB_CW_BORDER_PIXEL,
2567 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2568 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2569 win->ws->always_raise)
2570 map_window_raised(win->id);
2572 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2573 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2581 event_drain(uint8_t rt)
2583 xcb_generic_event_t *evt;
2586 while ((evt = xcb_poll_for_event(conn))) {
2587 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2595 switchws(struct swm_region *r, union arg *args)
2597 int wsid = args->id, unmap_old = 0;
2598 struct swm_region *this_r, *other_r;
2600 struct workspace *new_ws, *old_ws;
2606 if (wsid >= workspace_limit)
2610 old_ws = this_r->ws;
2611 new_ws = &this_r->s->ws[wsid];
2613 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2614 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2616 if (new_ws == NULL || old_ws == NULL)
2618 if (new_ws == old_ws)
2621 other_r = new_ws->r;
2622 if (other_r == NULL) {
2623 /* the other workspace is hidden, hide this one */
2627 /* the other ws is visible in another region, exchange them */
2628 other_r->ws_prior = new_ws;
2629 other_r->ws = old_ws;
2630 old_ws->r = other_r;
2632 this_r->ws_prior = old_ws;
2633 this_r->ws = new_ws;
2636 /* this is needed so that we can click on a window after a restart */
2640 a.id = SWM_ARG_ID_FOCUSCUR;
2641 focus(new_ws->r, &a);
2643 /* unmap old windows */
2645 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2648 if (focus_mode == SWM_FOCUS_DEFAULT)
2649 event_drain(XCB_ENTER_NOTIFY);
2653 DNPRINTF(SWM_D_WS, "switchws: done\n");
2657 cyclews(struct swm_region *r, union arg *args)
2660 struct swm_screen *s = r->s;
2663 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2664 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2669 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2672 case SWM_ARG_ID_CYCLEWS_UP:
2673 if (a.id < workspace_limit - 1)
2678 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2681 case SWM_ARG_ID_CYCLEWS_DOWN:
2685 a.id = workspace_limit - 1;
2692 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2694 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2698 } while (a.id != r->ws->idx);
2702 priorws(struct swm_region *r, union arg *args)
2706 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2707 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2709 if (r->ws_prior == NULL)
2712 a.id = r->ws_prior->idx;
2717 cyclescr(struct swm_region *r, union arg *args)
2719 struct swm_region *rr = NULL;
2721 int i, x, y, num_screens;
2723 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2724 /* do nothing if we don't have more than one screen */
2725 if (!(num_screens > 1 || outputs > 1))
2730 case SWM_ARG_ID_CYCLESC_UP:
2731 rr = TAILQ_NEXT(r, entry);
2733 rr = TAILQ_FIRST(&screens[i].rl);
2735 case SWM_ARG_ID_CYCLESC_DOWN:
2736 rr = TAILQ_PREV(r, swm_region_list, entry);
2738 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2746 /* move mouse to region */
2748 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2749 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2752 a.id = SWM_ARG_ID_FOCUSCUR;
2755 if (rr->ws->focus) {
2756 /* move to focus window */
2757 x = X(rr->ws->focus) + 1;
2758 y = Y(rr->ws->focus) + 1;
2759 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2765 sort_windows(struct ws_win_list *wl)
2767 struct ws_win *win, *parent, *nxt;
2772 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2773 nxt = TAILQ_NEXT(win, entry);
2774 if (win->transient) {
2775 parent = find_window(win->transient);
2776 if (parent == NULL) {
2777 warnx("not possible bug");
2780 TAILQ_REMOVE(wl, win, entry);
2781 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2788 swapwin(struct swm_region *r, union arg *args)
2790 struct ws_win *target, *source;
2791 struct ws_win *cur_focus;
2792 struct ws_win_list *wl;
2795 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2796 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2798 cur_focus = r->ws->focus;
2799 if (cur_focus == NULL)
2803 wl = &source->ws->winlist;
2806 case SWM_ARG_ID_SWAPPREV:
2807 if (source->transient)
2808 source = find_window(source->transient);
2809 target = TAILQ_PREV(source, ws_win_list, entry);
2810 if (target && target->transient)
2811 target = find_window(target->transient);
2812 TAILQ_REMOVE(wl, source, entry);
2814 TAILQ_INSERT_TAIL(wl, source, entry);
2816 TAILQ_INSERT_BEFORE(target, source, entry);
2818 case SWM_ARG_ID_SWAPNEXT:
2819 target = TAILQ_NEXT(source, entry);
2820 /* move the parent and let the sort handle the move */
2821 if (source->transient)
2822 source = find_window(source->transient);
2823 TAILQ_REMOVE(wl, source, entry);
2825 TAILQ_INSERT_HEAD(wl, source, entry);
2827 TAILQ_INSERT_AFTER(wl, target, source, entry);
2829 case SWM_ARG_ID_SWAPMAIN:
2830 target = TAILQ_FIRST(wl);
2831 if (target == source) {
2832 if (source->ws->focus_prev != NULL &&
2833 source->ws->focus_prev != target)
2834 source = source->ws->focus_prev;
2838 if (target == NULL || source == NULL)
2840 source->ws->focus_prev = target;
2841 TAILQ_REMOVE(wl, target, entry);
2842 TAILQ_INSERT_BEFORE(source, target, entry);
2843 TAILQ_REMOVE(wl, source, entry);
2844 TAILQ_INSERT_HEAD(wl, source, entry);
2846 case SWM_ARG_ID_MOVELAST:
2847 TAILQ_REMOVE(wl, source, entry);
2848 TAILQ_INSERT_TAIL(wl, source, entry);
2851 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2863 focus_prev(struct ws_win *win)
2865 struct ws_win *winfocus = NULL;
2866 struct ws_win *cur_focus = NULL;
2867 struct ws_win_list *wl = NULL;
2868 struct workspace *ws = NULL;
2870 if (!(win && win->ws))
2875 cur_focus = ws->focus;
2877 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2878 WINID(win), WINID(cur_focus));
2880 /* pickle, just focus on whatever */
2881 if (cur_focus == NULL) {
2882 /* use prev_focus if valid */
2883 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2884 find_window(WINID(ws->focus_prev)))
2885 winfocus = ws->focus_prev;
2889 /* if transient focus on parent */
2890 if (cur_focus->transient) {
2891 winfocus = find_window(cur_focus->transient);
2895 /* if in max_stack try harder */
2896 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2897 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2898 if (cur_focus != ws->focus_prev)
2899 winfocus = ws->focus_prev;
2901 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2906 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2908 if (winfocus == NULL || winfocus == win) {
2909 switch (focus_close) {
2910 case SWM_STACK_BOTTOM:
2911 winfocus = TAILQ_FIRST(wl);
2914 winfocus = TAILQ_LAST(wl, ws_win_list);
2916 case SWM_STACK_ABOVE:
2917 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2918 if (focus_close_wrap)
2919 winfocus = TAILQ_FIRST(wl);
2921 winfocus = TAILQ_PREV(cur_focus,
2922 ws_win_list, entry);
2925 case SWM_STACK_BELOW:
2926 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2928 if (focus_close_wrap)
2929 winfocus = TAILQ_LAST(wl, ws_win_list);
2931 winfocus = TAILQ_NEXT(cur_focus, entry);
2937 if (winfocus == NULL) {
2938 if (focus_default == SWM_STACK_TOP)
2939 winfocus = TAILQ_LAST(wl, ws_win_list);
2941 winfocus = TAILQ_FIRST(wl);
2945 focus_magic(winfocus);
2949 focus(struct swm_region *r, union arg *args)
2951 struct ws_win *winfocus = NULL, *head;
2952 struct ws_win *cur_focus = NULL;
2953 struct ws_win_list *wl = NULL;
2954 struct workspace *ws = NULL;
2960 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2962 /* treat FOCUS_CUR special */
2963 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2964 if (r->ws->focus && r->ws->focus->iconic == 0)
2965 winfocus = r->ws->focus;
2966 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2967 winfocus = r->ws->focus_prev;
2969 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2970 if (winfocus->iconic == 0)
2973 focus_magic(winfocus);
2977 if ((cur_focus = r->ws->focus) == NULL)
2981 if (TAILQ_EMPTY(wl))
2983 /* make sure there is at least one uniconified window */
2985 TAILQ_FOREACH(winfocus, wl, entry)
2986 if (winfocus->iconic == 0) {
2994 case SWM_ARG_ID_FOCUSPREV:
2995 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2997 head = TAILQ_LAST(wl, ws_win_list);
2999 if (WINID(winfocus) == cur_focus->transient) {
3000 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3002 head = TAILQ_LAST(wl, ws_win_list);
3007 if (winfocus && winfocus->iconic) {
3008 while (winfocus != cur_focus) {
3009 if (winfocus == NULL)
3010 winfocus = TAILQ_LAST(wl, ws_win_list);
3011 if (winfocus->iconic == 0)
3013 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3019 case SWM_ARG_ID_FOCUSNEXT:
3020 head = TAILQ_NEXT(cur_focus, entry);
3022 head = TAILQ_FIRST(wl);
3026 if (winfocus && winfocus->iconic) {
3027 while (winfocus != cur_focus) {
3028 if (winfocus == NULL)
3029 winfocus = TAILQ_FIRST(wl);
3030 if (winfocus->iconic == 0)
3032 winfocus = TAILQ_NEXT(winfocus, entry);
3037 case SWM_ARG_ID_FOCUSMAIN:
3038 winfocus = TAILQ_FIRST(wl);
3039 if (winfocus == cur_focus)
3040 winfocus = cur_focus->ws->focus_prev;
3047 focus_magic(winfocus);
3053 cycle_layout(struct swm_region *r, union arg *args)
3055 struct workspace *ws = r->ws;
3058 /* suppress unused warning since var is needed */
3061 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3064 if (ws->cur_layout->l_stack == NULL)
3065 ws->cur_layout = &layouts[0];
3070 if (focus_mode == SWM_FOCUS_DEFAULT)
3071 event_drain(XCB_ENTER_NOTIFY);
3073 a.id = SWM_ARG_ID_FOCUSCUR;
3078 stack_config(struct swm_region *r, union arg *args)
3080 struct workspace *ws = r->ws;
3082 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3085 if (ws->cur_layout->l_config != NULL)
3086 ws->cur_layout->l_config(ws, args->id);
3088 if (args->id != SWM_ARG_ID_STACKINIT)
3095 struct swm_geometry g;
3096 struct swm_region *r;
3102 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3104 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3105 for (i = 0; i < num_screens; i++) {
3109 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3110 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3111 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3113 /* start with screen geometry, adjust for bar */
3115 g.w -= 2 * border_width;
3116 g.h -= 2 * border_width;
3122 r->ws->cur_layout->l_stack(r->ws, &g);
3123 r->ws->cur_layout->l_string(r->ws);
3124 /* save r so we can track region changes */
3131 if (focus_mode == SWM_FOCUS_DEFAULT)
3132 event_drain(XCB_ENTER_NOTIFY);
3134 DNPRINTF(SWM_D_STACK, "stack: end\n");
3138 store_float_geom(struct ws_win *win, struct swm_region *r)
3140 /* retain window geom and region geom */
3141 win->g_float = win->g;
3142 win->g_float.x -= X(r);
3143 win->g_float.y -= Y(r);
3144 win->g_floatvalid = 1;
3145 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3146 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3147 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3148 win->g_float.w, win->g_float.h);
3152 stack_floater(struct ws_win *win, struct swm_region *r)
3157 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3160 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3161 * geom on ws switches or return from max mode
3163 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3164 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3165 /* refloat at last floating relative position */
3166 win->g = win->g_float;
3171 win->floatmaxed = 0;
3174 * if set to fullscreen mode, configure window to maximum size.
3176 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3177 if (!win->g_floatvalid)
3178 store_float_geom(win, win->ws->r);
3180 win->g = win->ws->r->g;
3184 * remove border on fullscreen floater when in fullscreen mode or when
3185 * the quirk is present.
3187 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3188 ((win->quirks & SWM_Q_FULLSCREEN) &&
3189 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3190 if (win->bordered) {
3192 X(win) += border_width;
3193 Y(win) += border_width;
3195 } else if (!win->bordered) {
3197 X(win) -= border_width;
3198 Y(win) -= border_width;
3201 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3202 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3203 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3208 * floaters and transients are auto-centred unless moved
3211 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3212 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3215 /* keep window within region bounds */
3216 constrain_window(win, r, 0);
3222 * Send keystrokes to terminal to decrease/increase the font size as the
3223 * window size changes.
3226 adjust_font(struct ws_win *win)
3228 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3229 win->floating || win->transient)
3232 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3233 WIDTH(win) / win->sh.width_inc < term_width &&
3234 win->font_steps < SWM_MAX_FONT_STEPS) {
3235 win->font_size_boundary[win->font_steps] =
3236 (win->sh.width_inc * term_width) + win->sh.base_width;
3239 win->last_inc = win->sh.width_inc;
3240 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3241 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3242 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3245 win->last_inc = win->sh.width_inc;
3246 fake_keypress(win, XK_KP_Add, ShiftMask);
3250 #define SWAPXY(g) do { \
3252 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3253 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3256 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3258 struct swm_geometry win_g, r_g = *g;
3259 struct ws_win *win, *fs_win = NULL;
3260 int i, j, s, stacks;
3261 int w_inc = 1, h_inc, w_base = 1, h_base;
3262 int hrh, extra = 0, h_slice, last_h = 0;
3263 int split, colno, winno, mwin, msize, mscale;
3264 int remain, missing, v_slice, reconfigure;
3266 xcb_get_window_attributes_reply_t *war;
3268 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3269 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3271 winno = count_win(ws, 0);
3272 if (winno == 0 && count_win(ws, 1) == 0)
3275 TAILQ_FOREACH(win, &ws->winlist, entry)
3276 if (win->transient == 0 && win->floating == 0
3277 && win->iconic == 0)
3284 w_inc = win->sh.width_inc;
3285 w_base = win->sh.base_width;
3286 mwin = ws->l_state.horizontal_mwin;
3287 mscale = ws->l_state.horizontal_msize;
3288 stacks = ws->l_state.horizontal_stacks;
3291 w_inc = win->sh.height_inc;
3292 w_base = win->sh.base_height;
3293 mwin = ws->l_state.vertical_mwin;
3294 mscale = ws->l_state.vertical_msize;
3295 stacks = ws->l_state.vertical_stacks;
3299 if (stacks > winno - mwin)
3300 stacks = winno - mwin;
3304 h_slice = r_g.h / SWM_H_SLICE;
3305 if (mwin && winno > mwin) {
3306 v_slice = r_g.w / SWM_V_SLICE;
3310 win_g.w = v_slice * mscale;
3312 if (w_inc > 1 && w_inc < v_slice) {
3313 /* adjust for window's requested size increment */
3314 remain = (win_g.w - w_base) % w_inc;
3320 win_g.x += r_g.w - msize;
3323 colno = split = winno / stacks;
3324 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3325 2 * border_width) / stacks);
3327 hrh = r_g.h / colno;
3328 extra = r_g.h - (colno * hrh);
3329 win_g.h = hrh - 2 * border_width;
3331 /* stack all the tiled windows */
3332 i = j = 0, s = stacks;
3333 TAILQ_FOREACH(win, &ws->winlist, entry) {
3334 if (win->transient != 0 || win->floating != 0)
3336 if (win->iconic != 0)
3339 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3344 if (split && i == split) {
3345 colno = (winno - mwin) / stacks;
3346 if (s <= (winno - mwin) % stacks)
3348 split = split + colno;
3349 hrh = (r_g.h / colno);
3350 extra = r_g.h - (colno * hrh);
3354 win_g.x += win_g.w + 2 * border_width;
3355 win_g.w = (r_g.w - msize -
3356 (stacks * 2 * border_width)) / stacks;
3358 win_g.w += (r_g.w - msize -
3359 (stacks * 2 * border_width)) % stacks;
3363 win_g.h = hrh - 2 * border_width;
3365 h_inc = win->sh.width_inc;
3366 h_base = win->sh.base_width;
3368 h_inc = win->sh.height_inc;
3369 h_base = win->sh.base_height;
3371 if (j == colno - 1) {
3372 win_g.h = hrh + extra;
3373 } else if (h_inc > 1 && h_inc < h_slice) {
3374 /* adjust for window's requested size increment */
3375 remain = (win_g.h - h_base) % h_inc;
3376 missing = h_inc - remain;
3378 if (missing <= extra || j == 0) {
3390 win_g.y += last_h + 2 * border_width;
3392 if (disable_border && bar_enabled == 0 && winno == 1){
3394 win_g.w += 2 * border_width;
3395 win_g.h += 2 * border_width;
3400 if (X(win) != win_g.y || Y(win) != win_g.x ||
3401 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3405 WIDTH(win) = win_g.h;
3406 HEIGHT(win) = win_g.w;
3409 if (X(win) != win_g.x || Y(win) != win_g.y ||
3410 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3414 WIDTH(win) = win_g.w;
3415 HEIGHT(win) = win_g.h;
3419 if (bordered != win->bordered) {
3421 win->bordered = bordered;
3429 war = xcb_get_window_attributes_reply(conn,
3430 xcb_get_window_attributes(conn, win->id),
3433 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3434 map_window_raised(win->id);
3444 /* now, stack all the floaters and transients */
3445 TAILQ_FOREACH(win, &ws->winlist, entry) {
3446 if (win->transient == 0 && win->floating == 0)
3448 if (win->iconic == 1)
3450 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3455 stack_floater(win, ws->r);
3456 map_window_raised(win->id);
3460 stack_floater(fs_win, ws->r);
3461 map_window_raised(fs_win->id);
3466 vertical_config(struct workspace *ws, int id)
3468 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3472 case SWM_ARG_ID_STACKRESET:
3473 case SWM_ARG_ID_STACKINIT:
3474 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3475 ws->l_state.vertical_mwin = 1;
3476 ws->l_state.vertical_stacks = 1;
3478 case SWM_ARG_ID_MASTERSHRINK:
3479 if (ws->l_state.vertical_msize > 1)
3480 ws->l_state.vertical_msize--;
3482 case SWM_ARG_ID_MASTERGROW:
3483 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3484 ws->l_state.vertical_msize++;
3486 case SWM_ARG_ID_MASTERADD:
3487 ws->l_state.vertical_mwin++;
3489 case SWM_ARG_ID_MASTERDEL:
3490 if (ws->l_state.vertical_mwin > 0)
3491 ws->l_state.vertical_mwin--;
3493 case SWM_ARG_ID_STACKINC:
3494 ws->l_state.vertical_stacks++;
3496 case SWM_ARG_ID_STACKDEC:
3497 if (ws->l_state.vertical_stacks > 1)
3498 ws->l_state.vertical_stacks--;
3500 case SWM_ARG_ID_FLIPLAYOUT:
3501 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3509 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3511 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3513 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3517 horizontal_config(struct workspace *ws, int id)
3519 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3522 case SWM_ARG_ID_STACKRESET:
3523 case SWM_ARG_ID_STACKINIT:
3524 ws->l_state.horizontal_mwin = 1;
3525 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3526 ws->l_state.horizontal_stacks = 1;
3528 case SWM_ARG_ID_MASTERSHRINK:
3529 if (ws->l_state.horizontal_msize > 1)
3530 ws->l_state.horizontal_msize--;
3532 case SWM_ARG_ID_MASTERGROW:
3533 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3534 ws->l_state.horizontal_msize++;
3536 case SWM_ARG_ID_MASTERADD:
3537 ws->l_state.horizontal_mwin++;
3539 case SWM_ARG_ID_MASTERDEL:
3540 if (ws->l_state.horizontal_mwin > 0)
3541 ws->l_state.horizontal_mwin--;
3543 case SWM_ARG_ID_STACKINC:
3544 ws->l_state.horizontal_stacks++;
3546 case SWM_ARG_ID_STACKDEC:
3547 if (ws->l_state.horizontal_stacks > 1)
3548 ws->l_state.horizontal_stacks--;
3550 case SWM_ARG_ID_FLIPLAYOUT:
3551 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3559 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3561 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3563 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3566 /* fullscreen view */
3568 max_stack(struct workspace *ws, struct swm_geometry *g)
3570 struct swm_geometry gg = *g;
3571 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3572 int winno, num_screens;
3574 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3579 winno = count_win(ws, 0);
3580 if (winno == 0 && count_win(ws, 1) == 0)
3583 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3584 TAILQ_FOREACH(win, &ws->winlist, entry) {
3585 if (win->transient) {
3587 parent = find_window(win->transient);
3591 if (win->floating && win->floatmaxed == 0 ) {
3593 * retain geometry for retrieval on exit from
3596 store_float_geom(win, ws->r);
3597 win->floatmaxed = 1;
3600 /* only reconfigure if necessary */
3601 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3602 HEIGHT(win) != gg.h) {
3608 WIDTH(win) += 2 * border_width;
3609 HEIGHT(win) += 2 * border_width;
3614 /* unmap only if we don't have multi screen */
3615 if (win != ws->focus)
3616 if (!(num_screens > 1 || outputs > 1))
3620 /* put the last transient on top */
3623 map_window_raised(parent->id);
3624 stack_floater(wintrans, ws->r);
3625 focus_magic(wintrans);
3630 send_to_ws(struct swm_region *r, union arg *args)
3632 int wsid = args->id;
3633 struct ws_win *win = NULL, *parent;
3634 struct workspace *ws, *nws;
3635 char ws_idx_str[SWM_PROPLEN];
3638 if (wsid >= workspace_limit)
3641 if (r && r->ws && r->ws->focus)
3647 if (win->ws->idx == wsid)
3650 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3653 nws = &win->s->ws[wsid];
3655 a.id = SWM_ARG_ID_FOCUSPREV;
3657 if (win->transient) {
3658 parent = find_window(win->transient);
3660 unmap_window(parent);
3661 TAILQ_REMOVE(&ws->winlist, parent, entry);
3662 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3667 TAILQ_REMOVE(&ws->winlist, win, entry);
3668 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3669 if (TAILQ_EMPTY(&ws->winlist))
3670 r->ws->focus = NULL;
3673 /* Try to update the window's workspace property */
3674 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3675 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3677 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3678 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3689 pressbutton(struct swm_region *r, union arg *args)
3691 /* suppress unused warning since var is needed */
3694 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3695 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3696 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3697 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3701 raise_toggle(struct swm_region *r, union arg *args)
3703 /* suppress unused warning since var is needed */
3706 if (r == NULL || r->ws == NULL)
3709 r->ws->always_raise = !r->ws->always_raise;
3711 /* bring floaters back to top */
3712 if (r->ws->always_raise == 0)
3719 iconify(struct swm_region *r, union arg *args)
3723 /* suppress unused warning since var is needed */
3726 if (r->ws->focus == NULL)
3728 unmap_window(r->ws->focus);
3729 update_iconic(r->ws->focus, 1);
3732 r->ws->focus = NULL;
3733 a.id = SWM_ARG_ID_FOCUSCUR;
3740 get_win_name(xcb_window_t win)
3743 xcb_get_property_cookie_t c;
3744 xcb_icccm_get_text_property_reply_t r;
3746 c = xcb_icccm_get_wm_name(conn, win);
3747 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3748 if (r.name_len > 0) {
3749 name = malloc(r.name_len + 1);
3751 memcpy(name, r.name, r.name_len);
3752 name[r.name_len] = '\0';
3755 xcb_icccm_get_text_property_reply_wipe(&r);
3762 uniconify(struct swm_region *r, union arg *args)
3769 DNPRINTF(SWM_D_MISC, "uniconify\n");
3771 if (r == NULL || r->ws == NULL)
3774 /* make sure we have anything to uniconify */
3775 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3776 if (win->ws == NULL)
3777 continue; /* should never happen */
3778 if (win->iconic == 0)
3786 search_resp_action = SWM_SEARCH_UNICONIFY;
3788 spawn_select(r, args, "search", &searchpid);
3790 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3793 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3794 if (win->ws == NULL)
3795 continue; /* should never happen */
3796 if (win->iconic == 0)
3799 name = get_win_name(win->id);
3802 fprintf(lfile, "%s.%u\n", name, win->id);
3812 name_workspace(struct swm_region *r, union arg *args)
3816 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3822 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3824 spawn_select(r, args, "name_workspace", &searchpid);
3826 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3829 fprintf(lfile, "%s", "");
3834 search_workspace(struct swm_region *r, union arg *args)
3837 struct workspace *ws;
3840 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3846 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3848 spawn_select(r, args, "search", &searchpid);
3850 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3853 for (i = 0; i < workspace_limit; i++) {
3857 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3858 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3865 search_win_cleanup(void)
3867 struct search_window *sw = NULL;
3869 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3870 xcb_destroy_window(conn, sw->indicator);
3871 xcb_free_gc(conn, sw->gc);
3872 TAILQ_REMOVE(&search_wl, sw, entry);
3878 search_win(struct swm_region *r, union arg *args)
3880 struct ws_win *win = NULL;
3881 struct search_window *sw = NULL;
3883 uint32_t gcv[3], wa[2];
3891 DNPRINTF(SWM_D_MISC, "search_win\n");
3894 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3896 spawn_select(r, args, "search", &searchpid);
3898 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3901 TAILQ_INIT(&search_wl);
3904 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3905 if (win->iconic == 1)
3908 sw = calloc(1, sizeof(struct search_window));
3910 warn("search_win: calloc");
3912 search_win_cleanup();
3918 snprintf(s, sizeof s, "%d", i);
3921 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
3923 w = xcb_generate_id(conn);
3924 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
3925 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
3926 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3927 info.width + 4, bar_font->height + 4,
3928 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3929 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3932 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3934 sw->gc = xcb_generate_id(conn);
3935 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
3936 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
3938 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3939 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3940 map_window_raised(w);
3942 draw = XftDrawCreate(display, w,
3943 DefaultVisual(display, r->s->idx),
3944 DefaultColormap(display, r->s->idx));
3946 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
3947 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
3950 XftDrawDestroy(draw);
3952 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3954 fprintf(lfile, "%d\n", i);
3964 search_resp_uniconify(char *resp, unsigned long len)
3970 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3972 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3973 if (win->iconic == 0)
3975 name = get_win_name(win->id);
3978 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3983 if (strncmp(s, resp, len) == 0) {
3984 /* XXX this should be a callback to generalize */
3985 update_iconic(win, 0);
3994 search_resp_name_workspace(char *resp, unsigned long len)
3996 struct workspace *ws;
3998 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4000 if (search_r->ws == NULL)
4005 free(search_r->ws->name);
4006 search_r->ws->name = NULL;
4010 ws->name = strdup(resp);
4011 if (ws->name == NULL) {
4012 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4013 "strdup: %s", strerror(errno));
4020 search_resp_search_workspace(char *resp)
4027 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4031 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4038 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4040 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4047 switchws(search_r, &a);
4051 search_resp_search_window(char *resp)
4056 struct search_window *sw;
4058 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4062 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4067 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4069 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4076 TAILQ_FOREACH(sw, &search_wl, entry)
4077 if (idx == sw->idx) {
4083 #define MAX_RESP_LEN 1024
4086 search_do_resp(void)
4092 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4097 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4098 warn("search: calloc");
4102 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4104 warn("search: read error");
4107 resp[rbytes] = '\0';
4110 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4111 * newline, so work around that by sanitizing the resp now.
4113 resp[strcspn(resp, "\n")] = '\0';
4116 switch (search_resp_action) {
4117 case SWM_SEARCH_UNICONIFY:
4118 search_resp_uniconify(resp, len);
4120 case SWM_SEARCH_NAME_WORKSPACE:
4121 search_resp_name_workspace(resp, len);
4123 case SWM_SEARCH_SEARCH_WORKSPACE:
4124 search_resp_search_workspace(resp);
4126 case SWM_SEARCH_SEARCH_WINDOW:
4127 search_resp_search_window(resp);
4132 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4133 search_win_cleanup();
4135 search_resp_action = SWM_SEARCH_NONE;
4136 close(select_resp_pipe[0]);
4141 wkill(struct swm_region *r, union arg *args)
4143 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4145 if (r->ws->focus == NULL)
4148 if (args->id == SWM_ARG_ID_KILLWINDOW)
4149 xcb_kill_client(conn, r->ws->focus->id);
4151 if (r->ws->focus->can_delete)
4152 client_msg(r->ws->focus, a_delete);
4159 floating_toggle_win(struct ws_win *win)
4161 struct swm_region *r;
4171 /* reject floating toggles in max stack mode */
4172 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4175 if (win->floating) {
4176 if (!win->floatmaxed) {
4177 /* retain position for refloat */
4178 store_float_geom(win, r);
4182 if (win->g_floatvalid) {
4183 /* refloat at last floating relative position */
4184 X(win) = win->g_float.x + X(r);
4185 Y(win) = win->g_float.y + Y(r);
4186 WIDTH(win) = win->g_float.w;
4187 HEIGHT(win) = win->g_float.h;
4192 ewmh_update_actions(win);
4198 floating_toggle(struct swm_region *r, union arg *args)
4200 struct ws_win *win = r->ws->focus;
4203 /* suppress unused warning since var is needed */
4209 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4210 _NET_WM_STATE_TOGGLE);
4214 if (win == win->ws->focus) {
4215 a.id = SWM_ARG_ID_FOCUSCUR;
4216 focus(win->ws->r, &a);
4220 event_drain(XCB_ENTER_NOTIFY);
4224 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4226 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4228 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4230 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4233 if (X(win) + BORDER(win) < X(r)) {
4235 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4237 X(win) = X(r) - BORDER(win);
4240 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4242 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4244 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4247 if (Y(win) + BORDER(win) < Y(r)) {
4249 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4251 Y(win) = Y(r) - BORDER(win);
4257 if (HEIGHT(win) < 1)
4263 update_window(struct ws_win *win)
4268 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4269 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4270 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4274 wc[3] = HEIGHT(win);
4275 wc[4] = BORDER(win);
4277 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4278 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4279 wc[3], YESNO(win->bordered));
4281 xcb_configure_window(conn, win->id, mask, wc);
4284 #define SWM_RESIZE_STEPS (50)
4287 resize(struct ws_win *win, union arg *args)
4289 xcb_timestamp_t timestamp = 0;
4290 struct swm_region *r = NULL;
4291 int resize_step = 0;
4292 struct swm_geometry g;
4293 int top = 0, left = 0, resizing;
4295 unsigned int shape; /* cursor style */
4296 xcb_cursor_t cursor;
4297 xcb_font_t cursor_font;
4298 xcb_query_pointer_reply_t *xpr;
4299 xcb_generic_event_t *evt;
4300 xcb_motion_notify_event_t *mne;
4306 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4309 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4310 "transient: 0x%x\n", win->id, YESNO(win->floating),
4313 if (!(win->transient != 0 || win->floating != 0))
4316 /* reject resizes in max mode for floaters (transient ok) */
4317 if (win->floatmaxed)
4321 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4327 case SWM_ARG_ID_WIDTHSHRINK:
4328 WIDTH(win) -= SWM_RESIZE_STEPS;
4331 case SWM_ARG_ID_WIDTHGROW:
4332 WIDTH(win) += SWM_RESIZE_STEPS;
4335 case SWM_ARG_ID_HEIGHTSHRINK:
4336 HEIGHT(win) -= SWM_RESIZE_STEPS;
4339 case SWM_ARG_ID_HEIGHTGROW:
4340 HEIGHT(win) += SWM_RESIZE_STEPS;
4347 constrain_window(win, r, 1);
4349 store_float_geom(win,r);
4353 /* get cursor offset from window root */
4354 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4361 if (xpr->win_x < WIDTH(win) / 2)
4364 if (xpr->win_y < HEIGHT(win) / 2)
4367 if (args->id == SWM_ARG_ID_CENTER)
4370 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4372 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4374 cursor_font = xcb_generate_id(conn);
4375 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4377 cursor = xcb_generate_id(conn);
4378 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4379 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4381 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4382 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4387 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4388 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4389 case XCB_BUTTON_RELEASE:
4390 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4393 case XCB_MOTION_NOTIFY:
4394 mne = (xcb_motion_notify_event_t *)evt;
4395 /* cursor offset/delta from start of the operation */
4396 dx = mne->root_x - xpr->root_x;
4397 dy = mne->root_y - xpr->root_y;
4403 if (args->id == SWM_ARG_ID_CENTER) {
4404 if (g.h / 2 + dy < 1)
4408 HEIGHT(win) = g.h + 2 * dy;
4416 HEIGHT(win) = g.h + dy;
4423 if (args->id == SWM_ARG_ID_CENTER) {
4424 if (g.w / 2 + dx < 1)
4428 WIDTH(win) = g.w + 2 * dx;
4436 WIDTH(win) = g.w + dx;
4439 constrain_window(win, r, 1);
4441 /* not free, don't sync more than 120 times / second */
4442 if ((mne->time - timestamp) > (1000 / 120) ) {
4443 timestamp = mne->time;
4458 store_float_geom(win,r);
4460 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4461 xcb_free_cursor(conn, cursor);
4462 xcb_close_font(conn, cursor_font);
4464 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4468 resize_step(struct swm_region *r, union arg *args)
4470 struct ws_win *win = NULL;
4472 if (r && r->ws && r->ws->focus)
4480 #define SWM_MOVE_STEPS (50)
4483 move(struct ws_win *win, union arg *args)
4485 xcb_timestamp_t timestamp = 0;
4486 int move_step = 0, moving;
4487 struct swm_region *r = NULL;
4488 xcb_font_t cursor_font;
4489 xcb_cursor_t cursor;
4490 xcb_query_pointer_reply_t *qpr;
4491 xcb_generic_event_t *evt;
4492 xcb_motion_notify_event_t *mne;
4498 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4501 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4502 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4504 /* in max_stack mode should only move transients */
4505 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4509 if (win->floating == 0 && !win->transient) {
4510 store_float_geom(win, r);
4511 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4514 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4521 case SWM_ARG_ID_MOVELEFT:
4522 X(win) -= (SWM_MOVE_STEPS - border_width);
4525 case SWM_ARG_ID_MOVERIGHT:
4526 X(win) += (SWM_MOVE_STEPS - border_width);
4529 case SWM_ARG_ID_MOVEUP:
4530 Y(win) -= (SWM_MOVE_STEPS - border_width);
4533 case SWM_ARG_ID_MOVEDOWN:
4534 Y(win) += (SWM_MOVE_STEPS - border_width);
4541 constrain_window(win, r, 0);
4543 store_float_geom(win, r);
4547 cursor_font = xcb_generate_id(conn);
4548 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4550 cursor = xcb_generate_id(conn);
4551 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4552 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4554 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4555 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4556 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4558 /* get cursor offset from window root */
4559 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4562 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4563 xcb_free_cursor(conn, cursor);
4564 xcb_close_font(conn, cursor_font);
4570 while ((evt = xcb_wait_for_event(conn)) && moving) {
4571 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4572 case XCB_BUTTON_RELEASE:
4573 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4576 case XCB_MOTION_NOTIFY:
4577 mne = (xcb_motion_notify_event_t *)evt;
4578 X(win) = mne->root_x - qpr->win_x - border_width;
4579 Y(win) = mne->root_y - qpr->win_y - border_width;
4581 constrain_window(win, r, 0);
4583 /* not free, don't sync more than 120 times / second */
4584 if ((mne->time - timestamp) > (1000 / 120) ) {
4585 timestamp = mne->time;
4600 store_float_geom(win, r);
4602 xcb_free_cursor(conn, cursor);
4603 xcb_close_font(conn, cursor_font);
4604 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4605 DNPRINTF(SWM_D_EVENT, "move: done\n");
4609 move_step(struct swm_region *r, union arg *args)
4611 struct ws_win *win = NULL;
4613 if (r && r->ws && r->ws->focus)
4618 if (!(win->transient != 0 || win->floating != 0))
4625 /* user/key callable function IDs */
4675 KF_SEARCH_WORKSPACE,
4716 KF_DUMPWINS, /* MUST BE LAST */
4720 /* key definitions */
4722 char name[SWM_FUNCNAME_LEN];
4723 void (*func)(struct swm_region *r, union arg *);
4725 } keyfuncs[KF_INVALID + 1] = {
4726 /* name function argument */
4727 { "bar_toggle", bar_toggle, {0} },
4728 { "button2", pressbutton, {2} },
4729 { "cycle_layout", cycle_layout, {0} },
4730 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4731 { "float_toggle", floating_toggle,{0} },
4732 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4733 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4734 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4735 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4736 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4737 { "iconify", iconify, {0} },
4738 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4739 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4740 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4741 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4742 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4743 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4744 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4745 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4746 { "mvws_1", send_to_ws, {.id = 0} },
4747 { "mvws_2", send_to_ws, {.id = 1} },
4748 { "mvws_3", send_to_ws, {.id = 2} },
4749 { "mvws_4", send_to_ws, {.id = 3} },
4750 { "mvws_5", send_to_ws, {.id = 4} },
4751 { "mvws_6", send_to_ws, {.id = 5} },
4752 { "mvws_7", send_to_ws, {.id = 6} },
4753 { "mvws_8", send_to_ws, {.id = 7} },
4754 { "mvws_9", send_to_ws, {.id = 8} },
4755 { "mvws_10", send_to_ws, {.id = 9} },
4756 { "mvws_11", send_to_ws, {.id = 10} },
4757 { "mvws_12", send_to_ws, {.id = 11} },
4758 { "mvws_13", send_to_ws, {.id = 12} },
4759 { "mvws_14", send_to_ws, {.id = 13} },
4760 { "mvws_15", send_to_ws, {.id = 14} },
4761 { "mvws_16", send_to_ws, {.id = 15} },
4762 { "mvws_17", send_to_ws, {.id = 16} },
4763 { "mvws_18", send_to_ws, {.id = 17} },
4764 { "mvws_19", send_to_ws, {.id = 18} },
4765 { "mvws_20", send_to_ws, {.id = 19} },
4766 { "mvws_21", send_to_ws, {.id = 20} },
4767 { "mvws_22", send_to_ws, {.id = 21} },
4768 { "name_workspace", name_workspace, {0} },
4769 { "quit", quit, {0} },
4770 { "raise_toggle", raise_toggle, {0} },
4771 { "restart", restart, {0} },
4772 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4773 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4774 { "search_win", search_win, {0} },
4775 { "search_workspace", search_workspace, {0} },
4776 { "spawn_custom", NULL, {0} },
4777 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4778 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4779 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4780 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4781 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4782 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4783 { "uniconify", uniconify, {0} },
4784 { "version", version, {0} },
4785 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4786 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4787 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4788 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4789 { "ws_1", switchws, {.id = 0} },
4790 { "ws_2", switchws, {.id = 1} },
4791 { "ws_3", switchws, {.id = 2} },
4792 { "ws_4", switchws, {.id = 3} },
4793 { "ws_5", switchws, {.id = 4} },
4794 { "ws_6", switchws, {.id = 5} },
4795 { "ws_7", switchws, {.id = 6} },
4796 { "ws_8", switchws, {.id = 7} },
4797 { "ws_9", switchws, {.id = 8} },
4798 { "ws_10", switchws, {.id = 9} },
4799 { "ws_11", switchws, {.id = 10} },
4800 { "ws_12", switchws, {.id = 11} },
4801 { "ws_13", switchws, {.id = 12} },
4802 { "ws_14", switchws, {.id = 13} },
4803 { "ws_15", switchws, {.id = 14} },
4804 { "ws_16", switchws, {.id = 15} },
4805 { "ws_17", switchws, {.id = 16} },
4806 { "ws_18", switchws, {.id = 17} },
4807 { "ws_19", switchws, {.id = 18} },
4808 { "ws_20", switchws, {.id = 19} },
4809 { "ws_21", switchws, {.id = 20} },
4810 { "ws_22", switchws, {.id = 21} },
4811 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4812 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4813 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4814 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4815 { "ws_prior", priorws, {0} },
4816 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4817 { "invalid key func", NULL, {0} },
4820 RB_ENTRY(key) entry;
4823 enum keyfuncid funcid;
4826 RB_HEAD(key_tree, key);
4829 key_cmp(struct key *kp1, struct key *kp2)
4831 if (kp1->keysym < kp2->keysym)
4833 if (kp1->keysym > kp2->keysym)
4836 if (kp1->mod < kp2->mod)
4838 if (kp1->mod > kp2->mod)
4844 RB_GENERATE(key_tree, key, entry, key_cmp);
4845 struct key_tree keys;
4848 enum { client_click, root_click };
4850 unsigned int action;
4852 unsigned int button;
4853 void (*func)(struct ws_win *, union arg *);
4856 /* action key mouse button func args */
4857 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4858 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4859 { client_click, MODKEY, Button1, move, {0} },
4863 update_modkey(unsigned int mod)
4869 RB_FOREACH(kp, key_tree, &keys)
4870 if (kp->mod & ShiftMask)
4871 kp->mod = mod | ShiftMask;
4875 for (i = 0; i < LENGTH(buttons); i++)
4876 if (buttons[i].mask & ShiftMask)
4877 buttons[i].mask = mod | ShiftMask;
4879 buttons[i].mask = mod;
4884 TAILQ_ENTRY(spawn_prog) entry;
4889 TAILQ_HEAD(spawn_list, spawn_prog);
4890 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4893 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4896 struct spawn_prog *prog = NULL;
4898 char *ap, **real_args;
4900 /* suppress unused warning since var is needed */
4903 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4906 TAILQ_FOREACH(prog, &spawns, entry) {
4907 if (!strcasecmp(spawn_name, prog->name))
4911 warnx("spawn_custom: program %s not found", spawn_name);
4915 /* make room for expanded args */
4916 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4917 err(1, "spawn_custom: calloc real_args");
4919 /* expand spawn_args into real_args */
4920 for (i = 0; i < prog->argc; i++) {
4922 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4923 if (!strcasecmp(ap, "$bar_border")) {
4925 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4927 err(1, "spawn_custom border color");
4928 } else if (!strcasecmp(ap, "$bar_color")) {
4930 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4932 err(1, "spawn_custom bar color");
4933 } else if (!strcasecmp(ap, "$bar_font")) {
4934 if ((real_args[i] = strdup(bar_fonts))
4936 err(1, "spawn_custom bar fonts");
4937 } else if (!strcasecmp(ap, "$bar_font_color")) {
4939 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4941 err(1, "spawn_custom color font");
4942 } else if (!strcasecmp(ap, "$color_focus")) {
4944 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4946 err(1, "spawn_custom color focus");
4947 } else if (!strcasecmp(ap, "$color_unfocus")) {
4949 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4951 err(1, "spawn_custom color unfocus");
4953 /* no match --> copy as is */
4954 if ((real_args[i] = strdup(ap)) == NULL)
4955 err(1, "spawn_custom strdup(ap)");
4957 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4962 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4963 for (i = 0; i < prog->argc; i++)
4964 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4965 DNPRINTF(SWM_D_SPAWN, "\n");
4967 *ret_args = real_args;
4968 return (prog->argc);
4972 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4978 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4982 spawn(r->ws->idx, &a, 1);
4984 for (i = 0; i < spawn_argc; i++)
4990 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4996 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5000 if (pipe(select_list_pipe) == -1)
5001 err(1, "pipe error");
5002 if (pipe(select_resp_pipe) == -1)
5003 err(1, "pipe error");
5005 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5006 err(1, "could not disable SIGPIPE");
5007 switch (*pid = fork()) {
5009 err(1, "cannot fork");
5012 if (dup2(select_list_pipe[0], 0) == -1)
5014 if (dup2(select_resp_pipe[1], 1) == -1)
5016 close(select_list_pipe[1]);
5017 close(select_resp_pipe[0]);
5018 spawn(r->ws->idx, &a, 0);
5020 default: /* parent */
5021 close(select_list_pipe[0]);
5022 close(select_resp_pipe[1]);
5026 for (i = 0; i < spawn_argc; i++)
5032 spawn_insert(char *name, char *args)
5034 char *arg, *cp, *ptr;
5035 struct spawn_prog *sp;
5037 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5039 if ((sp = calloc(1, sizeof *sp)) == NULL)
5040 err(1, "spawn_insert: calloc");
5041 if ((sp->name = strdup(name)) == NULL)
5042 err(1, "spawn_insert: strdup");
5044 /* convert the arguments to an argument list */
5045 if ((ptr = cp = strdup(args)) == NULL)
5046 err(1, "spawn_insert: strdup");
5047 while ((arg = strsep(&ptr, " \t")) != NULL) {
5048 /* empty field; skip it */
5053 if ((sp->argv = realloc(sp->argv, sp->argc *
5054 sizeof *sp->argv)) == NULL)
5055 err(1, "spawn_insert: realloc");
5056 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5057 err(1, "spawn_insert: strdup");
5061 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5062 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5066 spawn_remove(struct spawn_prog *sp)
5070 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5072 TAILQ_REMOVE(&spawns, sp, entry);
5073 for (i = 0; i < sp->argc; i++)
5079 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5083 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5085 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5088 spawn_insert(name, args);
5090 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5094 setspawn(char *name, char *args)
5096 struct spawn_prog *sp;
5098 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5103 TAILQ_FOREACH(sp, &spawns, entry) {
5104 if (!strcmp(sp->name, name)) {
5108 spawn_replace(sp, name, args);
5109 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5113 if (*args == '\0') {
5114 warnx("error: setspawn: cannot find program: %s", name);
5118 spawn_insert(name, args);
5119 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5123 setconfspawn(char *selector, char *value, int flags)
5125 /* suppress unused warning since var is needed */
5128 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5130 setspawn(selector, value);
5132 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5139 setconfspawn("term", "xterm", 0);
5140 setconfspawn("spawn_term", "xterm", 0);
5141 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5142 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5143 setconfspawn("lock", "xlock", 0);
5144 setconfspawn("initscr", "initscreen.sh", 0);
5145 setconfspawn("menu", "dmenu_run"
5148 " -nf $bar_font_color"
5150 " -sf $bar_color", 0);
5151 setconfspawn("search", "dmenu"
5155 " -nf $bar_font_color"
5157 " -sf $bar_color", 0);
5158 setconfspawn("name_workspace", "dmenu"
5162 " -nf $bar_font_color"
5164 " -sf $bar_color", 0);
5168 #define SWM_MODNAME_SIZE 32
5169 #define SWM_KEY_WS "\n+ \t"
5171 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5175 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5176 if (mod == NULL || ks == NULL) {
5177 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5180 if (keystr == NULL || strlen(keystr) == 0) {
5181 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5185 *ks = XCB_NO_SYMBOL;
5187 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5188 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5190 cp += (long)strspn(cp, SWM_KEY_WS);
5191 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5193 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5195 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5197 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5199 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5201 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5203 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5204 *mod |= ControlMask;
5206 *ks = XStringToKeysym(name);
5207 XConvertCase(*ks, ks, &uks);
5208 if (ks == XCB_NO_SYMBOL) {
5210 "parsekeys: invalid key %s\n",
5216 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5221 strdupsafe(char *str)
5226 return (strdup(str));
5230 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5234 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5235 keyfuncs[kfid].name, spawn_name);
5237 if ((kp = malloc(sizeof *kp)) == NULL)
5238 err(1, "key_insert: malloc");
5243 kp->spawn_name = strdupsafe(spawn_name);
5244 RB_INSERT(key_tree, &keys, kp);
5246 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5250 key_lookup(unsigned int mod, KeySym ks)
5257 return (RB_FIND(key_tree, &keys, &kp));
5261 key_remove(struct key *kp)
5263 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5265 RB_REMOVE(key_tree, &keys, kp);
5266 free(kp->spawn_name);
5269 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5273 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5276 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5280 key_insert(mod, ks, kfid, spawn_name);
5282 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5286 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5291 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5292 keyfuncs[kfid].name, spawn_name);
5294 if ((kp = key_lookup(mod, ks)) != NULL) {
5295 if (kfid == KF_INVALID)
5298 key_replace(kp, mod, ks, kfid, spawn_name);
5299 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5302 if (kfid == KF_INVALID) {
5303 warnx("error: setkeybinding: cannot find mod/key combination");
5304 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5308 key_insert(mod, ks, kfid, spawn_name);
5309 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5313 setconfbinding(char *selector, char *value, int flags)
5315 enum keyfuncid kfid;
5318 struct spawn_prog *sp;
5320 /* suppress unused warning since var is needed */
5323 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5324 if (selector == NULL) {
5325 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5326 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5328 setkeybinding(mod, ks, kfid, NULL);
5333 /* search by key function name */
5334 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5335 if (strncasecmp(selector, keyfuncs[kfid].name,
5336 SWM_FUNCNAME_LEN) == 0) {
5337 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5339 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5340 setkeybinding(mod, ks, kfid, NULL);
5346 /* search by custom spawn name */
5347 TAILQ_FOREACH(sp, &spawns, entry) {
5348 if (strcasecmp(selector, sp->name) == 0) {
5349 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5351 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5352 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5359 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5366 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5367 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5368 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5369 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5370 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5371 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5372 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5374 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5375 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5376 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5377 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5380 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5381 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5382 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5383 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5384 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5385 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5386 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5387 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5388 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5389 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5390 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5391 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5392 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5393 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5394 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5395 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5396 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5397 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5398 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5399 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5400 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5401 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5402 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5403 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5404 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5405 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5406 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5407 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5408 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5409 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5410 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5411 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5412 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5413 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5414 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5415 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5416 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5419 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5420 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5421 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5422 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5424 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5425 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5426 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5427 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5428 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5429 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5430 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5431 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5432 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5433 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5434 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5435 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5436 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5437 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5438 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5439 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5440 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5441 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5442 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5443 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5444 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5445 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5446 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5447 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5448 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5449 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5450 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5451 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5452 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5453 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5454 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5455 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5456 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5457 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5458 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5459 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5460 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5461 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5463 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5472 while (RB_EMPTY(&keys) == 0) {
5473 kp = RB_ROOT(&keys);
5479 setkeymapping(char *selector, char *value, int flags)
5481 char keymapping_file[PATH_MAX];
5483 /* suppress unused warnings since vars are needed */
5487 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5488 if (value[0] == '~')
5489 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5490 pwd->pw_dir, &value[1]);
5492 strlcpy(keymapping_file, value, sizeof keymapping_file);
5494 /* load new key bindings; if it fails, revert to default bindings */
5495 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5499 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5504 updatenumlockmask(void)
5507 xcb_get_modifier_mapping_reply_t *modmap_r;
5508 xcb_keycode_t *modmap, kc, *keycode;
5510 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5513 modmap_r = xcb_get_modifier_mapping_reply(conn,
5514 xcb_get_modifier_mapping(conn),
5517 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5518 for (i = 0; i < 8; i++) {
5519 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5520 kc = modmap[i * modmap_r->keycodes_per_modifier
5522 keycode = xcb_key_symbols_get_keycode(syms,
5525 numlockmask = (1 << i);
5538 xcb_keycode_t *code;
5539 unsigned int modifiers[] =
5540 { 0, LockMask, numlockmask, numlockmask | LockMask };
5543 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5544 updatenumlockmask();
5546 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5547 for (k = 0; k < num_screens; k++) {
5548 if (TAILQ_EMPTY(&screens[k].rl))
5550 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5552 RB_FOREACH(kp, key_tree, &keys) {
5553 if ((code = xcb_key_symbols_get_keycode(syms,
5555 for (j = 0; j < LENGTH(modifiers); j++)
5556 xcb_grab_key(conn, 1,
5558 kp->mod | modifiers[j],
5559 *code, XCB_GRAB_MODE_ASYNC,
5560 XCB_GRAB_MODE_ASYNC);
5567 grabbuttons(struct ws_win *win, int focused)
5570 unsigned int modifiers[] =
5571 { 0, LockMask, numlockmask, numlockmask|LockMask };
5573 updatenumlockmask();
5574 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5575 XCB_BUTTON_MASK_ANY);
5577 for (i = 0; i < LENGTH(buttons); i++)
5578 if (buttons[i].action == client_click)
5579 for (j = 0; j < LENGTH(modifiers); j++)
5580 xcb_grab_button(conn, 0, win->id,
5582 XCB_GRAB_MODE_ASYNC,
5589 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5590 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5591 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5594 const char *quirkname[] = {
5595 "NONE", /* config string for "no value" */
5604 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5605 #define SWM_Q_WS "\n|+ \t"
5607 parsequirks(char *qstr, unsigned long *quirk)
5617 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5619 cp += (long)strspn(cp, SWM_Q_WS);
5620 for (i = 0; i < LENGTH(quirkname); i++) {
5621 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5622 DNPRINTF(SWM_D_QUIRK,
5623 "parsequirks: %s\n", name);
5628 *quirk |= 1 << (i-1);
5632 if (i >= LENGTH(quirkname)) {
5633 DNPRINTF(SWM_D_QUIRK,
5634 "parsequirks: invalid quirk [%s]\n", name);
5642 quirk_insert(const char *class, const char *name, unsigned long quirk)
5646 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5649 if ((qp = malloc(sizeof *qp)) == NULL)
5650 err(1, "quirk_insert: malloc");
5651 if ((qp->class = strdup(class)) == NULL)
5652 err(1, "quirk_insert: strdup");
5653 if ((qp->name = strdup(name)) == NULL)
5654 err(1, "quirk_insert: strdup");
5657 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5659 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5663 quirk_remove(struct quirk *qp)
5665 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5666 qp->name, qp->quirk);
5668 TAILQ_REMOVE(&quirks, qp, entry);
5673 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5677 quirk_replace(struct quirk *qp, const char *class, const char *name,
5678 unsigned long quirk)
5680 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5681 qp->name, qp->quirk);
5684 quirk_insert(class, name, quirk);
5686 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5690 setquirk(const char *class, const char *name, unsigned long quirk)
5694 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5697 TAILQ_FOREACH(qp, &quirks, entry) {
5698 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5702 quirk_replace(qp, class, name, quirk);
5703 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5708 warnx("error: setquirk: cannot find class/name combination");
5712 quirk_insert(class, name, quirk);
5713 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5717 setconfquirk(char *selector, char *value, int flags)
5719 char *cp, *class, *name;
5721 unsigned long quirks;
5723 /* suppress unused warning since var is needed */
5726 if (selector == NULL)
5728 if ((cp = strchr(selector, ':')) == NULL)
5733 if ((retval = parsequirks(value, &quirks)) == 0)
5734 setquirk(class, name, quirks);
5741 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5742 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5743 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5744 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5745 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5746 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5747 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5748 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5749 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5750 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5751 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5752 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5753 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5756 /* conf file stuff */
5757 #define SWM_CONF_FILE "spectrwm.conf"
5758 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5762 SWM_S_BAR_AT_BOTTOM,
5763 SWM_S_BAR_BORDER_WIDTH,
5770 SWM_S_CLOCK_ENABLED,
5773 SWM_S_CYCLE_VISIBLE,
5775 SWM_S_DISABLE_BORDER,
5777 SWM_S_FOCUS_CLOSE_WRAP,
5778 SWM_S_FOCUS_DEFAULT,
5784 SWM_S_STACK_ENABLED,
5786 SWM_S_TITLE_CLASS_ENABLED,
5787 SWM_S_TITLE_NAME_ENABLED,
5788 SWM_S_URGENT_ENABLED,
5789 SWM_S_VERBOSE_LAYOUT,
5790 SWM_S_WINDOW_NAME_ENABLED,
5791 SWM_S_WORKSPACE_LIMIT
5795 setconfvalue(char *selector, char *value, int flags)
5800 /* suppress unused warning since var is needed */
5804 case SWM_S_BAR_ACTION:
5806 if ((bar_argv[0] = strdup(value)) == NULL)
5807 err(1, "setconfvalue: bar_action");
5809 case SWM_S_BAR_AT_BOTTOM:
5810 bar_at_bottom = atoi(value);
5812 case SWM_S_BAR_BORDER_WIDTH:
5813 bar_border_width = atoi(value);
5814 if (bar_border_width < 0)
5815 bar_border_width = 0;
5817 case SWM_S_BAR_DELAY:
5818 bar_delay = atoi(value);
5820 case SWM_S_BAR_ENABLED:
5821 bar_enabled = atoi(value);
5823 case SWM_S_BAR_FONT:
5825 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5826 err(1, "setconfvalue: asprintf: failed to allocate "
5827 "memory for bar_fonts.");
5830 case SWM_S_BAR_FORMAT:
5832 if ((bar_format = strdup(value)) == NULL)
5833 err(1, "setconfvalue: bar_format");
5835 case SWM_S_BAR_JUSTIFY:
5836 if (!strcmp(value, "left"))
5837 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5838 else if (!strcmp(value, "center"))
5839 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5840 else if (!strcmp(value, "right"))
5841 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5843 errx(1, "invalid bar_justify");
5845 case SWM_S_BORDER_WIDTH:
5846 border_width = atoi(value);
5847 if (border_width < 0)
5850 case SWM_S_CLOCK_ENABLED:
5851 clock_enabled = atoi(value);
5853 case SWM_S_CLOCK_FORMAT:
5854 #ifndef SWM_DENY_CLOCK_FORMAT
5856 if ((clock_format = strdup(value)) == NULL)
5857 err(1, "setconfvalue: clock_format");
5860 case SWM_S_CYCLE_EMPTY:
5861 cycle_empty = atoi(value);
5863 case SWM_S_CYCLE_VISIBLE:
5864 cycle_visible = atoi(value);
5866 case SWM_S_DIALOG_RATIO:
5867 dialog_ratio = atof(value);
5868 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5871 case SWM_S_DISABLE_BORDER:
5872 disable_border = atoi(value);
5874 case SWM_S_FOCUS_CLOSE:
5875 if (!strcmp(value, "first"))
5876 focus_close = SWM_STACK_BOTTOM;
5877 else if (!strcmp(value, "last"))
5878 focus_close = SWM_STACK_TOP;
5879 else if (!strcmp(value, "next"))
5880 focus_close = SWM_STACK_ABOVE;
5881 else if (!strcmp(value, "previous"))
5882 focus_close = SWM_STACK_BELOW;
5884 errx(1, "focus_close");
5886 case SWM_S_FOCUS_CLOSE_WRAP:
5887 focus_close_wrap = atoi(value);
5889 case SWM_S_FOCUS_DEFAULT:
5890 if (!strcmp(value, "last"))
5891 focus_default = SWM_STACK_TOP;
5892 else if (!strcmp(value, "first"))
5893 focus_default = SWM_STACK_BOTTOM;
5895 errx(1, "focus_default");
5897 case SWM_S_FOCUS_MODE:
5898 if (!strcmp(value, "default"))
5899 focus_mode = SWM_FOCUS_DEFAULT;
5900 else if (!strcmp(value, "follow_cursor"))
5901 focus_mode = SWM_FOCUS_FOLLOW;
5902 else if (!strcmp(value, "synergy"))
5903 focus_mode = SWM_FOCUS_SYNERGY;
5905 errx(1, "focus_mode");
5907 case SWM_S_SPAWN_ORDER:
5908 if (!strcmp(value, "first"))
5909 spawn_position = SWM_STACK_BOTTOM;
5910 else if (!strcmp(value, "last"))
5911 spawn_position = SWM_STACK_TOP;
5912 else if (!strcmp(value, "next"))
5913 spawn_position = SWM_STACK_ABOVE;
5914 else if (!strcmp(value, "previous"))
5915 spawn_position = SWM_STACK_BELOW;
5917 errx(1, "spawn_position");
5919 case SWM_S_SPAWN_TERM:
5920 setconfspawn("term", value, 0);
5921 setconfspawn("spawn_term", value, 0);
5925 case SWM_S_SS_ENABLED:
5926 ss_enabled = atoi(value);
5928 case SWM_S_STACK_ENABLED:
5929 stack_enabled = atoi(value);
5931 case SWM_S_TERM_WIDTH:
5932 term_width = atoi(value);
5936 case SWM_S_TITLE_CLASS_ENABLED:
5937 title_class_enabled = atoi(value);
5939 case SWM_S_TITLE_NAME_ENABLED:
5940 title_name_enabled = atoi(value);
5942 case SWM_S_URGENT_ENABLED:
5943 urgent_enabled = atoi(value);
5945 case SWM_S_VERBOSE_LAYOUT:
5946 verbose_layout = atoi(value);
5947 for (i = 0; layouts[i].l_stack != NULL; i++) {
5949 layouts[i].l_string = fancy_stacker;
5951 layouts[i].l_string = plain_stacker;
5954 case SWM_S_WINDOW_NAME_ENABLED:
5955 window_name_enabled = atoi(value);
5957 case SWM_S_WORKSPACE_LIMIT:
5958 workspace_limit = atoi(value);
5959 if (workspace_limit > SWM_WS_MAX)
5960 workspace_limit = SWM_WS_MAX;
5961 else if (workspace_limit < 1)
5962 workspace_limit = 1;
5971 setconfmodkey(char *selector, char *value, int flags)
5973 /* suppress unused warnings since vars are needed */
5977 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5978 update_modkey(Mod1Mask);
5979 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5980 update_modkey(Mod2Mask);
5981 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5982 update_modkey(Mod3Mask);
5983 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5984 update_modkey(Mod4Mask);
5991 setconfcolor(char *selector, char *value, int flags)
5993 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5998 setconfregion(char *selector, char *value, int flags)
6000 /* suppress unused warnings since vars are needed */
6004 custom_region(value);
6009 setautorun(char *selector, char *value, int flags)
6019 /* suppress unused warnings since vars are needed */
6023 if (getenv("SWM_STARTED"))
6027 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6028 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6030 if (ws_id < 0 || ws_id >= workspace_limit)
6031 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6034 * This is a little intricate
6036 * If the pid already exists we simply reuse it because it means it was
6037 * used before AND not claimed by manage_window. We get away with
6038 * altering it in the parent after INSERT because this can not be a race
6041 while ((ap = strsep(&sp, " \t")) != NULL) {
6044 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6046 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6047 err(1, "setautorun: realloc");
6048 a.argv[argc - 1] = ap;
6051 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6052 err(1, "setautorun: realloc");
6053 a.argv[argc] = NULL;
6055 if ((pid = fork()) == 0) {
6056 spawn(ws_id, &a, 1);
6065 p = calloc(1, sizeof *p);
6068 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6078 setlayout(char *selector, char *value, int flags)
6080 int ws_id, i, x, mg, ma, si, raise, f = 0;
6081 int st = SWM_V_STACK, num_screens;
6083 struct workspace *ws;
6085 /* suppress unused warnings since vars are needed */
6089 if (getenv("SWM_STARTED"))
6093 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6094 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6095 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6096 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6099 if (ws_id < 0 || ws_id >= workspace_limit)
6100 errx(1, "layout: invalid workspace %d", ws_id + 1);
6102 if (!strcasecmp(s, "vertical"))
6104 else if (!strcasecmp(s, "vertical_flip")) {
6107 } else if (!strcasecmp(s, "horizontal"))
6109 else if (!strcasecmp(s, "horizontal_flip")) {
6112 } else if (!strcasecmp(s, "fullscreen"))
6115 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6116 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6119 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6120 for (i = 0; i < num_screens; i++) {
6121 ws = (struct workspace *)&screens[i].ws;
6122 ws[ws_id].cur_layout = &layouts[st];
6124 ws[ws_id].always_raise = raise;
6125 if (st == SWM_MAX_STACK)
6129 for (x = 0; x < abs(mg); x++) {
6130 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6131 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6132 SWM_ARG_ID_MASTERSHRINK);
6136 for (x = 0; x < abs(ma); x++) {
6137 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6138 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6139 SWM_ARG_ID_MASTERDEL);
6143 for (x = 0; x < abs(si); x++) {
6144 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6145 si >= 0 ? SWM_ARG_ID_STACKINC :
6146 SWM_ARG_ID_STACKDEC);
6151 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6152 SWM_ARG_ID_FLIPLAYOUT);
6160 /* config options */
6161 struct config_option {
6163 int (*func)(char*, char*, int);
6166 struct config_option configopt[] = {
6167 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6168 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6169 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6170 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6171 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6172 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6173 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6174 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6175 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6176 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6177 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6178 { "keyboard_mapping", setkeymapping, 0 },
6179 { "bind", setconfbinding, 0 },
6180 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6181 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6182 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6183 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6184 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6185 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6186 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6187 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6188 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6189 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6190 { "modkey", setconfmodkey, 0 },
6191 { "program", setconfspawn, 0 },
6192 { "quirk", setconfquirk, 0 },
6193 { "region", setconfregion, 0 },
6194 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6195 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6196 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6197 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6198 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6199 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6200 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6201 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6202 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6203 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6204 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6205 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6206 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6207 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6208 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6209 { "autorun", setautorun, 0 },
6210 { "layout", setlayout, 0 },
6215 conf_load(char *filename, int keymapping)
6218 char *line, *cp, *optsub, *optval;
6219 size_t linelen, lineno = 0;
6220 int wordlen, i, optind;
6221 struct config_option *opt;
6223 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6225 if (filename == NULL) {
6226 warnx("conf_load: no filename");
6229 if ((config = fopen(filename, "r")) == NULL) {
6230 warn("conf_load: fopen: %s", filename);
6234 while (!feof(config)) {
6235 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6238 err(1, "%s", filename);
6243 cp += strspn(cp, " \t\n"); /* eat whitespace */
6244 if (cp[0] == '\0') {
6249 /* get config option */
6250 wordlen = strcspn(cp, "=[ \t\n");
6252 warnx("%s: line %zd: no option found",
6257 for (i = 0; i < LENGTH(configopt); i++) {
6258 opt = &configopt[i];
6259 if (!strncasecmp(cp, opt->optname, wordlen) &&
6260 (int)strlen(opt->optname) == wordlen) {
6266 warnx("%s: line %zd: unknown option %.*s",
6267 filename, lineno, wordlen, cp);
6270 if (keymapping && strcmp(opt->optname, "bind")) {
6271 warnx("%s: line %zd: invalid option %.*s",
6272 filename, lineno, wordlen, cp);
6276 cp += strspn(cp, " \t\n"); /* eat whitespace */
6277 /* get [selector] if any */
6281 wordlen = strcspn(cp, "]");
6284 warnx("%s: line %zd: syntax error",
6289 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6291 warnx("%s: line %zd: unable to allocate"
6292 "memory for selector", filename,
6298 cp += strspn(cp, "] \t\n"); /* eat trailing */
6300 cp += strspn(cp, "= \t\n"); /* eat trailing */
6302 optval = strdup(cp);
6303 /* call function to deal with it all */
6304 if (configopt[optind].func(optsub, optval,
6305 configopt[optind].funcflags) != 0)
6306 errx(1, "%s: line %zd: invalid data for %s",
6307 filename, lineno, configopt[optind].optname);
6314 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6321 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6327 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6329 struct ws_win *parent, *w;
6330 struct swm_region *r;
6331 struct workspace *ws;
6332 xcb_icccm_wm_hints_t wmh;
6334 parent = find_window(win->transient);
6336 parent->child_trans = win;
6338 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6339 " for 0x%x trans 0x%x\n", win->id, win->transient);
6341 r = root_to_region(win->wa->root);
6343 /* parent doen't exist in our window list */
6344 TAILQ_FOREACH(w, &ws->winlist, entry) {
6345 if (xcb_icccm_get_wm_hints_reply(conn,
6346 xcb_icccm_get_wm_hints(conn, w->id),
6348 warnx("can't get hints for 0x%x", w->id);
6352 if (win->hints.window_group != wmh.window_group)
6355 w->child_trans = win;
6356 win->transient = w->id;
6358 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6359 "transient to 0x%x\n", win->transient);
6366 window_get_pid(xcb_window_t win)
6371 xcb_get_property_cookie_t pc;
6372 xcb_get_property_reply_t *pr;
6374 apid = get_atom_from_string("_NET_WM_PID");
6375 if (apid == XCB_ATOM_NONE)
6378 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6379 pr = xcb_get_property_reply(conn, pc, NULL);
6382 if (pr->type != XCB_ATOM_CARDINAL) {
6387 if (pr->type == apid && pr->format == 32)
6388 ret = *((pid_t *)xcb_get_property_value(pr));
6394 apid = get_atom_from_string("_SWM_PID");
6395 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6397 pr = xcb_get_property_reply(conn, pc, NULL);
6400 if (pr->type != apid) {
6405 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6412 get_ws_idx(xcb_window_t id)
6418 xcb_get_property_reply_t *gpr;
6420 gpr = xcb_get_property_reply(conn,
6421 xcb_get_property(conn, 0, id, a_swm_ws,
6422 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6427 proplen = xcb_get_property_value_length(gpr);
6429 prop = malloc(proplen + 1);
6432 xcb_get_property_value(gpr),
6434 prop[proplen] = '\0';
6441 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6442 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6444 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6454 manage_window(xcb_window_t id)
6456 xcb_window_t trans = XCB_WINDOW_NONE;
6457 struct workspace *ws;
6458 struct ws_win *win, *ww;
6459 int ws_idx, border_me = 0;
6460 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6461 struct swm_region *r;
6464 uint32_t event_mask, i;
6465 xcb_icccm_get_wm_protocols_reply_t wpr;
6466 xcb_icccm_get_wm_class_reply_t tmpch;
6468 if ((win = find_window(id)) != NULL)
6469 return (win); /* already being managed */
6471 /* see if we are on the unmanaged list */
6472 if ((win = find_unmanaged_window(id)) != NULL) {
6473 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6474 "window: 0x%x\n", win->id);
6475 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6477 set_child_transient(win, &trans);
6479 if (trans && (ww = find_window(trans)))
6480 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6481 else if ((ww = win->ws->focus) &&
6482 spawn_position == SWM_STACK_ABOVE)
6483 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6485 else if (ww && spawn_position == SWM_STACK_BELOW)
6486 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6487 else switch (spawn_position) {
6490 case SWM_STACK_ABOVE:
6491 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6493 case SWM_STACK_BOTTOM:
6494 case SWM_STACK_BELOW:
6495 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6498 ewmh_update_actions(win);
6502 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6503 err(1, "manage_window: calloc: failed to allocate memory for "
6509 /* see if we need to override the workspace */
6510 p = find_pid(window_get_pid(id));
6512 /* Get all the window data in one shot */
6513 ws_idx = get_ws_idx(id);
6515 win->wa = xcb_get_geometry_reply(conn,
6516 xcb_get_geometry(conn, id),
6518 xcb_icccm_get_wm_normal_hints_reply(conn,
6519 xcb_icccm_get_wm_normal_hints(conn, id),
6521 xcb_icccm_get_wm_hints_reply(conn,
6522 xcb_icccm_get_wm_hints(conn, id),
6524 xcb_icccm_get_wm_transient_for_reply(conn,
6525 xcb_icccm_get_wm_transient_for(conn, id),
6528 win->transient = trans;
6529 set_child_transient(win, &trans);
6530 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6531 "transient: 0x%x\n", win->id, win->transient);
6534 /* get supported protocols */
6535 if (xcb_icccm_get_wm_protocols_reply(conn,
6536 xcb_icccm_get_wm_protocols(conn, id, a_prot),
6538 for (i = 0; i < wpr.atoms_len; i++) {
6539 if (wpr.atoms[i] == a_takefocus)
6540 win->take_focus = 1;
6541 if (wpr.atoms[i] == a_delete)
6542 win->can_delete = 1;
6544 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6547 win->iconic = get_iconic(win);
6550 * Figure out where to put the window. If it was previously assigned to
6551 * a workspace (either by spawn() or manually moving), and isn't
6552 * transient, * put it in the same workspace
6554 r = root_to_region(win->wa->root);
6556 ws = &r->s->ws[p->ws];
6557 TAILQ_REMOVE(&pidlist, p, entry);
6560 } else if (ws_idx != -1 && win->transient == 0) {
6561 ws = &r->s->ws[ws_idx];
6564 /* this should launch transients in the same ws as parent */
6566 if ((ww = find_window(trans)) != NULL)
6572 warnx("manage_window: fix this "
6578 /* set up the window layout */
6581 win->s = r->s; /* this never changes */
6582 if (trans && (ww = find_window(trans)))
6583 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6584 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6585 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6587 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6588 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6589 else switch (spawn_position) {
6592 case SWM_STACK_ABOVE:
6593 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6595 case SWM_STACK_BOTTOM:
6596 case SWM_STACK_BELOW:
6597 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6600 /* ignore window border if there is one. */
6601 WIDTH(win) = win->wa->width;
6602 HEIGHT(win) = win->wa->height;
6603 X(win) = win->wa->x + win->wa->border_width;
6604 Y(win) = win->wa->y + win->wa->border_width;
6606 win->g_floatvalid = 0;
6607 win->floatmaxed = 0;
6608 win->ewmh_flags = 0;
6610 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6611 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6612 HEIGHT(win), ws->idx);
6614 constrain_window(win, r, 0);
6616 /* Set window properties so we can remember this after reincarnation */
6617 if (prop == NULL && snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6619 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6621 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6622 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6628 ewmh_autoquirk(win);
6630 if (xcb_icccm_get_wm_class_reply(conn,
6631 xcb_icccm_get_wm_class(conn, win->id),
6633 win->ch.class_name = tmpch.class_name;
6634 win->ch.instance_name = tmpch.instance_name;
6636 xcb_icccm_get_wm_class_reply_wipe(&tmpch);
6638 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6639 win->ch.class_name, win->ch.instance_name);
6641 /* java is retarded so treat it special */
6642 if (strstr(win->ch.instance_name, "sun-awt")) {
6647 TAILQ_FOREACH(qp, &quirks, entry) {
6648 if (!strcmp(win->ch.class_name, qp->class) &&
6649 !strcmp(win->ch.instance_name, qp->name)) {
6650 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6651 "class: %s, name: %s\n", win->ch.class_name,
6652 win->ch.instance_name);
6653 if (qp->quirk & SWM_Q_FLOAT) {
6657 win->quirks = qp->quirk;
6662 /* alter window position if quirky */
6663 if (win->quirks & SWM_Q_ANYWHERE) {
6664 win->manual = 1; /* don't center the quirky windows */
6665 if (bar_enabled && Y(win) < bar_height)
6666 Y(win) = bar_height;
6667 if (WIDTH(win) + X(win) > WIDTH(r))
6668 X(win) = WIDTH(r) - WIDTH(win) - 2;
6672 /* Reset font sizes (the bruteforce way; no default keybinding). */
6673 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6674 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6675 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6676 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6677 fake_keypress(win, XK_KP_Add, ShiftMask);
6680 ewmh_get_win_state(win);
6681 ewmh_update_actions(win);
6682 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6687 X(win) -= border_width;
6688 Y(win) -= border_width;
6692 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6693 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6695 event_mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
6698 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6700 /* floaters need to be mapped if they are in the current workspace */
6701 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6702 map_window_raised(win->id);
6708 free_window(struct ws_win *win)
6710 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6715 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6723 memset(win, 0xff, sizeof *win); /* XXX kill later */
6726 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6730 unmanage_window(struct ws_win *win)
6732 struct ws_win *parent;
6733 xcb_screen_t *screen;
6738 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6740 if (win->transient) {
6741 parent = find_window(win->transient);
6743 parent->child_trans = NULL;
6746 /* focus on root just in case */
6747 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6748 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6749 screen->root, XCB_CURRENT_TIME);
6753 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6754 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6758 focus_magic(struct ws_win *win)
6760 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6763 /* if there are no windows clear the status-bar */
6768 if (win->child_trans) {
6769 /* win = parent & has a transient so focus on that */
6771 focus_win(win->child_trans);
6772 if (win->child_trans->take_focus)
6773 client_msg(win, a_takefocus);
6775 /* make sure transient hasn't disappeared */
6776 if (validate_win(win->child_trans) == 0) {
6777 focus_win(win->child_trans);
6778 if (win->child_trans->take_focus)
6779 client_msg(win->child_trans,
6782 win->child_trans = NULL;
6784 if (win->take_focus)
6785 client_msg(win, a_takefocus);
6791 if (win->take_focus)
6792 client_msg(win, a_takefocus);
6797 expose(xcb_expose_event_t *e)
6800 struct swm_region *r;
6802 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6804 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6805 for (i = 0; i < num_screens; i++)
6806 TAILQ_FOREACH(r, &screens[i].rl, entry)
6807 if (e->window == WINID(r->bar))
6815 focusin(xcb_focus_in_event_t *e)
6817 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
6818 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
6819 e->mode, get_notify_detail_label(e->detail), e->detail);
6824 keypress(xcb_key_press_event_t *e)
6826 xcb_keysym_t keysym;
6829 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6831 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6833 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6836 if (kp->funcid == KF_SPAWN_CUSTOM)
6837 spawn_custom(root_to_region(e->root),
6838 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6839 else if (keyfuncs[kp->funcid].func)
6840 keyfuncs[kp->funcid].func(root_to_region(e->root),
6841 &(keyfuncs[kp->funcid].args));
6845 buttonpress(xcb_button_press_event_t *e)
6849 unsigned int action;
6851 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6852 e->event, e->detail);
6854 if ((win = find_window(e->event)) == NULL)
6858 action = client_click;
6860 for (i = 0; i < LENGTH(buttons); i++)
6861 if (action == buttons[i].action && buttons[i].func &&
6862 buttons[i].button == e->detail &&
6863 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6864 buttons[i].func(win, &buttons[i].args);
6871 print_win_geom(xcb_window_t w)
6873 xcb_get_geometry_reply_t *wa;
6875 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
6878 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
6883 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
6884 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6885 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
6893 configurerequest(xcb_configure_request_event_t *e)
6898 uint32_t wc[7] = {0};
6900 if ((win = find_window(e->window)) == NULL)
6901 if ((win = find_unmanaged_window(e->window)) == NULL)
6904 print_win_geom(e->window);
6907 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6908 "value_mask: 0x%x", e->window, e->value_mask);
6909 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6910 mask |= XCB_CONFIG_WINDOW_X;
6912 DPRINTF(", X: %d", e->x);
6914 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6915 mask |= XCB_CONFIG_WINDOW_Y;
6917 DPRINTF(", Y: %d", e->y);
6919 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6920 mask |= XCB_CONFIG_WINDOW_WIDTH;
6922 DPRINTF(", W: %u", e->width);
6924 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6925 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6926 wc[i++] = e->height;
6927 DPRINTF(", H: %u", e->height);
6929 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6930 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6931 wc[i++] = e->border_width;
6932 DPRINTF(", Border: %u", e->border_width);
6934 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6935 mask |= XCB_CONFIG_WINDOW_SIBLING;
6936 wc[i++] = e->sibling;
6937 DPRINTF(", Sibling: 0x%x", e->sibling);
6939 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6940 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6941 wc[i++] = e->stack_mode;
6942 DPRINTF(", StackMode: %u", e->stack_mode);
6946 xcb_configure_window(conn, e->window, mask, wc);
6948 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
6949 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6950 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6951 win->g_float.x = e->x - X(win->ws->r);
6952 win->g_float.y = e->y - Y(win->ws->r);
6953 win->g_float.w = e->width;
6954 win->g_float.h = e->height;
6955 win->g_floatvalid = 1;
6957 if (win->floating) {
6958 win->g = win->g_float;
6959 win->g.x += X(win->ws->r);
6960 win->g.y += Y(win->ws->r);
6973 configurenotify(xcb_configure_notify_event_t *e)
6977 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6980 win = find_window(e->window);
6982 xcb_icccm_get_wm_normal_hints_reply(conn,
6983 xcb_icccm_get_wm_normal_hints(conn, win->id),
6992 destroynotify(xcb_destroy_notify_event_t *e)
6996 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6998 if ((win = find_window(e->window)) == NULL) {
6999 if ((win = find_unmanaged_window(e->window)) == NULL)
7005 /* make sure we focus on something */
7008 unmanage_window(win);
7015 get_notify_detail_label(uint8_t detail)
7020 case XCB_NOTIFY_DETAIL_ANCESTOR:
7023 case XCB_NOTIFY_DETAIL_VIRTUAL:
7026 case XCB_NOTIFY_DETAIL_INFERIOR:
7029 case XCB_NOTIFY_DETAIL_NONLINEAR:
7030 label = "Nonlinear";
7032 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7033 label = "NonlinearVirtual";
7035 case XCB_NOTIFY_DETAIL_POINTER:
7038 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7039 label = "PointerRoot";
7041 case XCB_NOTIFY_DETAIL_NONE:
7052 get_notify_mode_label(uint8_t mode)
7057 case XCB_NOTIFY_MODE_NORMAL:
7060 case XCB_NOTIFY_MODE_GRAB:
7063 case XCB_NOTIFY_MODE_UNGRAB:
7066 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7067 label = "WhileGrabbed";
7078 enternotify(xcb_enter_notify_event_t *e)
7081 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %s(%d), "
7082 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7083 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7084 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7085 e->child, YESNO(e->same_screen_focus), e->state);
7087 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
7088 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
7093 switch (focus_mode) {
7094 case SWM_FOCUS_DEFAULT:
7096 case SWM_FOCUS_FOLLOW:
7098 case SWM_FOCUS_SYNERGY:
7102 if ((win = find_window(e->event)) == NULL) {
7103 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7114 leavenotify(xcb_leave_notify_event_t *e)
7117 DNPRINTF(SWM_D_FOCUS, "leavenotify: window: 0x%x, mode: %s(%d), "
7118 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7119 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7120 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7121 e->child, YESNO(e->same_screen_focus), e->state);
7125 /* lets us use one switch statement for arbitrary mode/detail combinations */
7126 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7129 mapnotify(xcb_map_notify_event_t *e)
7133 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7135 win = manage_window(e->window);
7137 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7140 * focus_win can only set input focus on a mapped window.
7141 * make sure the window really has focus since it is just being mapped.
7143 if (win->ws->focus == win)
7148 mappingnotify(xcb_mapping_notify_event_t *e)
7150 xcb_refresh_keyboard_mapping(syms, e);
7152 if (e->request == XCB_MAPPING_KEYBOARD)
7157 maprequest(xcb_map_request_event_t *e)
7160 struct swm_region *r;
7161 xcb_get_window_attributes_reply_t *war;
7163 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7166 war = xcb_get_window_attributes_reply(conn,
7167 xcb_get_window_attributes(conn, e->window),
7171 if (war->override_redirect) {
7177 win = manage_window(e->window);
7179 return; /* can't happen */
7184 /* make new win focused */
7185 r = root_to_region(win->wa->root);
7186 if (win->ws == r->ws)
7192 get_atom_name(xcb_atom_t atom)
7196 xcb_get_atom_name_reply_t *r;
7198 r = xcb_get_atom_name_reply(conn,
7199 xcb_get_atom_name(conn, atom),
7202 len = xcb_get_atom_name_name_length(r);
7204 name = malloc(len + 1);
7206 memcpy(name, xcb_get_atom_name_name(r), len);
7218 propertynotify(xcb_property_notify_event_t *e)
7224 name = get_atom_name(e->atom);
7225 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7226 e->window, name, e->atom);
7230 win = find_window(e->window);
7234 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7235 update_iconic(win, 0);
7236 map_window_raised(win->id);
7243 case XCB_ATOM_WM_CLASS:
7244 case XCB_ATOM_WM_NAME:
7253 unmapnotify(xcb_unmap_notify_event_t *e)
7257 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7259 /* determine if we need to help unmanage this window */
7260 win = find_window(e->window);
7264 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7265 unmanage_window(win);
7268 /* resend unmap because we ated it */
7269 xcb_unmap_window(conn, e->window);
7276 visibilitynotify(xcb_visibility_notify_event_t *e)
7278 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7284 clientmessage(xcb_client_message_event_t *e)
7287 xcb_map_request_event_t mre;
7291 name = get_atom_name(e->type);
7292 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7293 e->window, name, e->type);
7296 win = find_window(e->window);
7299 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7300 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7301 "unmanaged window.\n");
7302 mre.window = e->window;
7308 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7309 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7312 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7313 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7314 if (win->can_delete)
7315 client_msg(win, a_delete);
7317 xcb_kill_client(conn, win->id);
7319 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7320 DNPRINTF(SWM_D_EVENT,
7321 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7322 if (win->floating) {
7323 if (e->data.data32[0] & (1<<8)) /* x */
7324 X(win) = e->data.data32[1];
7325 if (e->data.data32[0] & (1<<9)) /* y */
7326 Y(win) = e->data.data32[2];
7327 if (e->data.data32[0] & (1<<10)) /* width */
7328 WIDTH(win) = e->data.data32[3];
7329 if (e->data.data32[0] & (1<<11)) /* height */
7330 HEIGHT(win) = e->data.data32[4];
7335 /* TODO: Change stack sizes */
7336 /* notify no change was made. */
7337 config_win(win, NULL);
7340 if (e->type == ewmh[_NET_WM_STATE].atom) {
7341 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7342 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7343 if (e->data.data32[2])
7344 ewmh_update_win_state(win, e->data.data32[2],
7357 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7359 xcb_void_cookie_t wac;
7360 xcb_generic_error_t *error;
7362 /* this causes an error if some other window manager is running */
7363 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7364 for (i = 0; i < num_screens; i++) {
7366 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7368 wac = xcb_change_window_attributes_checked(conn, sc->root,
7369 XCB_CW_EVENT_MASK, &val);
7370 if ((error = xcb_request_check(conn, wac))) {
7371 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7382 new_region(struct swm_screen *s, int x, int y, int w, int h)
7384 struct swm_region *r, *n;
7385 struct workspace *ws = NULL;
7388 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7389 s->idx, w, h, x, y);
7391 /* remove any conflicting regions */
7392 n = TAILQ_FIRST(&s->rl);
7395 n = TAILQ_NEXT(r, entry);
7396 if (X(r) < (x + w) &&
7397 (X(r) + WIDTH(r)) > x &&
7399 (Y(r) + HEIGHT(r)) > y) {
7400 if (r->ws->r != NULL)
7401 r->ws->old_r = r->ws->r;
7404 TAILQ_REMOVE(&s->rl, r, entry);
7405 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7409 /* search old regions for one to reuse */
7411 /* size + location match */
7412 TAILQ_FOREACH(r, &s->orl, entry)
7413 if (X(r) == x && Y(r) == y &&
7414 HEIGHT(r) == h && WIDTH(r) == w)
7418 TAILQ_FOREACH(r, &s->orl, entry)
7419 if (HEIGHT(r) == h && WIDTH(r) == w)
7423 TAILQ_REMOVE(&s->orl, r, entry);
7424 /* try to use old region's workspace */
7425 if (r->ws->r == NULL)
7428 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7429 err(1, "new_region: calloc: failed to allocate memory "
7432 /* if we don't have a workspace already, find one */
7434 for (i = 0; i < workspace_limit; i++)
7435 if (s->ws[i].r == NULL) {
7442 errx(1, "new_region: no free workspaces");
7453 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7459 #ifdef SWM_XRR_HAS_CRTC
7462 #endif /* SWM_XRR_HAS_CRTC */
7463 struct swm_region *r;
7465 xcb_randr_get_screen_resources_current_cookie_t src;
7466 xcb_randr_get_screen_resources_current_reply_t *srr;
7467 xcb_randr_get_crtc_info_cookie_t cic;
7468 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7469 xcb_randr_crtc_t *crtc;
7470 xcb_screen_t *screen = get_screen(i);
7472 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7473 if (i >= num_screens)
7474 errx(1, "scan_xrandr: invalid screen");
7476 /* remove any old regions */
7477 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7478 r->ws->old_r = r->ws->r = NULL;
7480 TAILQ_REMOVE(&screens[i].rl, r, entry);
7481 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7485 /* map virtual screens onto physical screens */
7486 #ifdef SWM_XRR_HAS_CRTC
7487 if (xrandr_support) {
7488 src = xcb_randr_get_screen_resources_current(conn,
7490 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7493 new_region(&screens[i], 0, 0,
7494 screen->width_in_pixels,
7495 screen->height_in_pixels);
7498 ncrtc = srr->num_crtcs;
7500 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7501 for (c = 0; c < ncrtc; c++) {
7502 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7504 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7507 if (cir->num_outputs == 0) {
7513 new_region(&screens[i], 0, 0,
7514 screen->width_in_pixels,
7515 screen->height_in_pixels);
7517 new_region(&screens[i],
7518 cir->x, cir->y, cir->width, cir->height);
7523 #endif /* SWM_XRR_HAS_CRTC */
7525 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7526 screen->height_in_pixels);
7531 screenchange(xcb_randr_screen_change_notify_event_t *e)
7533 struct swm_region *r;
7536 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7538 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7539 /* silly event doesn't include the screen index */
7540 for (i = 0; i < num_screens; i++)
7541 if (screens[i].root == e->root)
7543 if (i >= num_screens)
7544 errx(1, "screenchange: screen not found");
7546 /* brute force for now, just re-enumerate the regions */
7550 print_win_geom(e->root);
7552 /* add bars to all regions */
7553 for (i = 0; i < num_screens; i++)
7554 TAILQ_FOREACH(r, &screens[i].rl, entry)
7562 xcb_window_t *wins = NULL;
7564 int i, j, num_screens;
7565 uint16_t state, manage;
7567 xcb_query_tree_cookie_t qtc;
7568 xcb_query_tree_reply_t *qtr;
7569 xcb_get_window_attributes_cookie_t c;
7570 xcb_get_window_attributes_reply_t *r;
7571 xcb_get_property_cookie_t pc;
7573 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
7574 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7575 for (i = 0; i < num_screens; i++) {
7576 qtc = xcb_query_tree(conn, screens[i].root);
7577 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7580 wins = xcb_query_tree_children(qtr);
7581 no = xcb_query_tree_children_length(qtr);
7582 /* attach windows to a region */
7583 /* normal windows */
7584 for (j = 0; j < no; j++) {
7585 c = xcb_get_window_attributes(conn, wins[j]);
7586 r = xcb_get_window_attributes_reply(conn, c, NULL);
7589 if (r->override_redirect) {
7594 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7595 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7601 state = getstate(wins[j]);
7602 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7603 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7604 manage_window(wins[j]);
7607 /* transient windows */
7608 for (j = 0; j < no; j++) {
7609 c = xcb_get_window_attributes(conn, wins[j]);
7610 r = xcb_get_window_attributes_reply(conn, c, NULL);
7613 if (r->override_redirect) {
7619 state = getstate(wins[j]);
7620 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7621 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7622 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7623 &wins[j], NULL) && manage)
7624 manage_window(wins[j]);
7628 DNPRINTF(SWM_D_INIT, "grab_windows: done\n");
7634 int i, j, k, num_screens;
7635 struct workspace *ws;
7636 uint32_t gcv[1], wa[1];
7637 const xcb_query_extension_reply_t *qep;
7638 xcb_cursor_t cursor;
7639 xcb_font_t cursor_font;
7640 xcb_randr_query_version_cookie_t c;
7641 xcb_randr_query_version_reply_t *r;
7643 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7644 if ((screens = calloc(num_screens,
7645 sizeof(struct swm_screen))) == NULL)
7646 err(1, "setup_screens: calloc: failed to allocate memory for "
7649 /* initial Xrandr setup */
7651 c = xcb_randr_query_version(conn, 1, 1);
7652 r = xcb_randr_query_version_reply(conn, c, NULL);
7654 if (r->major_version >= 1)
7658 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7659 xrandr_eventbase = qep->first_event;
7661 cursor_font = xcb_generate_id(conn);
7662 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7664 cursor = xcb_generate_id(conn);
7665 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7666 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7669 /* map physical screens */
7670 for (i = 0; i < num_screens; i++) {
7671 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7673 TAILQ_INIT(&screens[i].rl);
7674 TAILQ_INIT(&screens[i].orl);
7675 screens[i].root = get_screen(i)->root;
7677 /* set default colors */
7678 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7679 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7680 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7681 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7682 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7684 /* create graphics context on screen */
7685 screens[i].bar_gc = xcb_generate_id(conn);
7687 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7688 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7690 /* set default cursor */
7691 xcb_change_window_attributes(conn, screens[i].root,
7694 /* init all workspaces */
7695 /* XXX these should be dynamically allocated too */
7696 for (j = 0; j < SWM_WS_MAX; j++) {
7697 ws = &screens[i].ws[j];
7703 TAILQ_INIT(&ws->winlist);
7704 TAILQ_INIT(&ws->unmanagedlist);
7706 for (k = 0; layouts[k].l_stack != NULL; k++)
7707 if (layouts[k].l_config != NULL)
7708 layouts[k].l_config(ws,
7709 SWM_ARG_ID_STACKINIT);
7710 ws->cur_layout = &layouts[0];
7711 ws->cur_layout->l_string(ws);
7717 xcb_randr_select_input(conn, screens[i].root,
7718 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7720 xcb_free_cursor(conn, cursor);
7721 xcb_close_font(conn, cursor_font);
7727 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7728 err(1, "setup_globals: strdup: failed to allocate memory.");
7730 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7731 err(1, "setup_globals: strdup: failed to allocate memory.");
7733 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7734 errx(1, "unable to allocate key symbols");
7736 a_state = get_atom_from_string("WM_STATE");
7737 a_prot = get_atom_from_string("WM_PROTOCOLS");
7738 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7739 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7740 a_wmname = get_atom_from_string("WM_NAME");
7741 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7742 a_utf8_string = get_atom_from_string("UTF8_STRING");
7743 a_string = get_atom_from_string("STRING");
7744 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7745 a_swm_ws = get_atom_from_string("_SWM_WS");
7752 xcb_atom_t netwmcheck;
7753 xcb_window_t root, win;
7756 /* work around sun jdk bugs, code from wmname */
7757 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7759 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7760 for (i = 0; i < num_screens; i++) {
7761 root = screens[i].root;
7763 win = xcb_generate_id(conn);
7764 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
7765 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
7766 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7767 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7768 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7770 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7771 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7772 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7773 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7774 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7775 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
7780 event_error(xcb_generic_error_t *e)
7782 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7783 "resource_id: %u, minor_code: %u\n",
7784 xcb_event_get_error_label(e->error_code), e->error_code,
7785 xcb_event_get_request_label(e->major_code), e->major_code,
7786 e->sequence, e->resource_id, e->minor_code);
7790 event_handle(xcb_generic_event_t *evt)
7792 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7794 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7795 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7796 XCB_EVENT_RESPONSE_TYPE(evt));
7799 #define EVENT(type, callback) case type: callback((void *)evt); return
7800 EVENT(0, event_error);
7801 EVENT(XCB_BUTTON_PRESS, buttonpress);
7802 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7803 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7804 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7805 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7806 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7807 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7808 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7809 /*EVENT(XCB_CREATE_NOTIFY, );*/
7810 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7811 EVENT(XCB_ENTER_NOTIFY, enternotify);
7812 EVENT(XCB_EXPOSE, expose);
7814 EVENT(XCB_FOCUS_IN, focusin);
7816 /*EVENT(XCB_FOCUS_OUT, );*/
7817 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7818 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7819 EVENT(XCB_KEY_PRESS, keypress);
7820 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7821 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7823 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
7825 EVENT(XCB_MAP_NOTIFY, mapnotify);
7826 EVENT(XCB_MAP_REQUEST, maprequest);
7827 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7828 /*EVENT(XCB_MOTION_NOTIFY, );*/
7829 /*EVENT(XCB_NO_EXPOSURE, );*/
7830 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7831 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7832 /*EVENT(XCB_RESIZE_REQUEST, );*/
7833 /*EVENT(XCB_SELECTION_CLEAR, );*/
7834 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7835 /*EVENT(XCB_SELECTION_REQUEST, );*/
7836 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7837 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7840 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7841 screenchange((void *)evt);
7845 main(int argc, char *argv[])
7847 struct swm_region *r, *rr;
7848 struct ws_win *winfocus = NULL;
7850 char conf[PATH_MAX], *cfile = NULL;
7852 int xfd, i, num_screens;
7853 struct sigaction sact;
7854 xcb_generic_event_t *evt;
7858 /* suppress unused warning since var is needed */
7861 time_started = time(NULL);
7864 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7865 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
7866 warnx("no locale support");
7868 /* handle some signals */
7869 bzero(&sact, sizeof(sact));
7870 sigemptyset(&sact.sa_mask);
7872 sact.sa_handler = sighdlr;
7873 sigaction(SIGINT, &sact, NULL);
7874 sigaction(SIGQUIT, &sact, NULL);
7875 sigaction(SIGTERM, &sact, NULL);
7876 sigaction(SIGHUP, &sact, NULL);
7878 sact.sa_handler = sighdlr;
7879 sact.sa_flags = SA_NOCLDSTOP;
7880 sigaction(SIGCHLD, &sact, NULL);
7882 if (!(display = XOpenDisplay(0)))
7883 errx(1, "can not open display");
7885 conn = XGetXCBConnection(display);
7886 if (xcb_connection_has_error(conn))
7887 errx(1, "can not get XCB connection");
7889 XSetEventQueueOwner(display, XCBOwnsEventQueue);
7891 xcb_prefetch_extension_data(conn, &xcb_randr_id);
7892 xfd = xcb_get_file_descriptor(conn);
7894 /* look for local and global conf file */
7895 pwd = getpwuid(getuid());
7897 errx(1, "invalid user: %d", getuid());
7899 xcb_grab_server(conn);
7902 /* flush all events */
7903 while ((evt = xcb_poll_for_event(conn))) {
7904 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7909 if (enable_wm() != 0)
7910 errx(1, "another window manager is currently running");
7921 for (i = 0; ; i++) {
7926 snprintf(conf, sizeof conf, "%s/.%s",
7927 pwd->pw_dir, SWM_CONF_FILE);
7931 snprintf(conf, sizeof conf, "/etc/%s",
7936 snprintf(conf, sizeof conf, "%s/.%s",
7937 pwd->pw_dir, SWM_CONF_FILE_OLD);
7941 snprintf(conf, sizeof conf, "/etc/%s",
7948 if (strlen(conf) && stat(conf, &sb) != -1)
7949 if (S_ISREG(sb.st_mode)) {
7956 /* load conf (if any) */
7958 conf_load(cfile, SWM_CONF_DEFAULT);
7961 /* set some values to work around bad programs */
7963 /* grab existing windows (before we build the bars) */
7966 if (getenv("SWM_STARTED") == NULL)
7967 setenv("SWM_STARTED", "YES", 1);
7969 /* setup all bars */
7970 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7971 for (i = 0; i < num_screens; i++)
7972 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7973 if (winfocus == NULL)
7974 winfocus = TAILQ_FIRST(&r->ws->winlist);
7983 xcb_ungrab_server(conn);
7987 while ((evt = xcb_poll_for_event(conn))) {
7994 /* if we are being restarted go focus on first window */
7996 rr = winfocus->ws->r;
7998 /* not a visible window */
8002 /* move pointer to first screen if multi screen */
8003 if (num_screens > 1 || outputs > 1)
8004 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
8005 rr->s[0].root, 0, 0, 0, 0, X(rr),
8006 Y(rr) + (bar_enabled ? bar_height : 0));
8008 a.id = SWM_ARG_ID_FOCUSCUR;
8018 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8020 DNPRINTF(SWM_D_MISC, "select failed");
8021 if (restart_wm == 1)
8022 restart(NULL, NULL);
8023 if (search_resp == 1)
8036 for (i = 0; i < num_screens; ++i)
8037 if (screens[i].bar_gc != 0)
8038 xcb_free_gc(conn, screens[i].bar_gc);
8040 XftFontClose(display, bar_font);
8041 XftColorFree(display, DefaultVisual(display, 0),
8042 DefaultColormap(display, 0), &bar_font_color);
8043 xcb_key_symbols_free(syms);
8045 xcb_disconnect(conn);