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 #define PSC '/' /* path separator char */
334 char *bar_argv[] = { NULL, NULL };
336 unsigned char bar_ext[SWM_BAR_MAX];
337 char bar_vertext[SWM_BAR_MAX];
339 sig_atomic_t bar_alarm = 0;
342 int bar_border_width = 1;
343 int bar_at_bottom = 0;
345 int bar_extra_running = 0;
348 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
349 char *bar_format = NULL;
350 int stack_enabled = 1;
351 int clock_enabled = 1;
352 int urgent_enabled = 0;
353 char *clock_format = NULL;
354 int title_name_enabled = 0;
355 int title_class_enabled = 0;
356 int window_name_enabled = 0;
357 int focus_mode = SWM_FOCUS_DEFAULT;
358 int focus_close = SWM_STACK_BELOW;
359 int focus_close_wrap = 1;
360 int focus_default = SWM_STACK_TOP;
361 int spawn_position = SWM_STACK_TOP;
362 int disable_border = 0;
363 int border_width = 1;
364 int verbose_layout = 0;
369 XftColor bar_font_color;
372 /* layout manager data */
373 struct swm_geometry {
386 struct swm_geometry g;
389 /* virtual "screens" */
391 TAILQ_ENTRY(swm_region) entry;
392 struct swm_geometry g;
393 struct workspace *ws; /* current workspace on this region */
394 struct workspace *ws_prior; /* prior workspace on this region */
395 struct swm_screen *s; /* screen idx */
398 TAILQ_HEAD(swm_region_list, swm_region);
401 TAILQ_ENTRY(ws_win) entry;
403 xcb_window_t transient;
404 struct ws_win *child_trans; /* transient child window */
405 struct swm_geometry g; /* current geometry */
406 struct swm_geometry g_float; /* region coordinates */
407 int g_floatvalid; /* g_float geometry validity */
408 int floatmaxed; /* whether maxed by max_stack */
413 unsigned int ewmh_flags;
414 int font_size_boundary[SWM_MAX_FONT_STEPS];
420 unsigned long quirks;
421 struct workspace *ws; /* always valid */
422 struct swm_screen *s; /* always valid, never changes */
423 xcb_get_geometry_reply_t *wa;
425 xcb_icccm_get_wm_class_reply_t ch;
426 xcb_icccm_wm_hints_t hints;
428 TAILQ_HEAD(ws_win_list, ws_win);
432 TAILQ_ENTRY(pid_e) entry;
436 TAILQ_HEAD(pid_list, pid_e);
437 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
439 /* layout handlers */
441 void vertical_config(struct workspace *, int);
442 void vertical_stack(struct workspace *, struct swm_geometry *);
443 void horizontal_config(struct workspace *, int);
444 void horizontal_stack(struct workspace *, struct swm_geometry *);
445 void max_stack(struct workspace *, struct swm_geometry *);
446 void plain_stacker(struct workspace *);
447 void fancy_stacker(struct workspace *);
450 void (*l_stack)(struct workspace *, struct swm_geometry *);
451 void (*l_config)(struct workspace *, int);
453 #define SWM_L_FOCUSPREV (1<<0)
454 #define SWM_L_MAPONFOCUS (1<<1)
455 void (*l_string)(struct workspace *);
457 /* stack, configure */
458 { vertical_stack, vertical_config, 0, plain_stacker },
459 { horizontal_stack, horizontal_config, 0, plain_stacker },
461 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
462 { NULL, NULL, 0, NULL },
465 /* position of max_stack mode in the layouts array, index into layouts! */
466 #define SWM_V_STACK (0)
467 #define SWM_H_STACK (1)
468 #define SWM_MAX_STACK (2)
470 #define SWM_H_SLICE (32)
471 #define SWM_V_SLICE (32)
473 /* define work spaces */
475 int idx; /* workspace index */
476 char *name; /* workspace name */
477 int always_raise; /* raise windows on focus */
478 struct layout *cur_layout; /* current layout handlers */
479 struct ws_win *focus; /* may be NULL */
480 struct ws_win *focus_prev; /* may be NULL */
481 struct swm_region *r; /* may be NULL */
482 struct swm_region *old_r; /* may be NULL */
483 struct ws_win_list winlist; /* list of windows in ws */
484 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
485 char stacker[10]; /* display stacker and layout */
489 int horizontal_msize;
491 int horizontal_stacks;
502 SWM_S_COLOR_BAR_BORDER,
503 SWM_S_COLOR_BAR_FONT,
509 /* physical screen mapping */
510 #define SWM_WS_MAX (22) /* hard limit */
511 int workspace_limit = 10; /* soft limit */
514 int idx; /* screen index */
515 struct swm_region_list rl; /* list of regions on this screen */
516 struct swm_region_list orl; /* list of old regions */
518 struct workspace ws[SWM_WS_MAX];
524 } c[SWM_S_COLOR_MAX];
526 xcb_gcontext_t bar_gc;
528 struct swm_screen *screens;
530 /* args to functions */
533 #define SWM_ARG_ID_FOCUSNEXT (0)
534 #define SWM_ARG_ID_FOCUSPREV (1)
535 #define SWM_ARG_ID_FOCUSMAIN (2)
536 #define SWM_ARG_ID_FOCUSCUR (4)
537 #define SWM_ARG_ID_SWAPNEXT (10)
538 #define SWM_ARG_ID_SWAPPREV (11)
539 #define SWM_ARG_ID_SWAPMAIN (12)
540 #define SWM_ARG_ID_MOVELAST (13)
541 #define SWM_ARG_ID_MASTERSHRINK (20)
542 #define SWM_ARG_ID_MASTERGROW (21)
543 #define SWM_ARG_ID_MASTERADD (22)
544 #define SWM_ARG_ID_MASTERDEL (23)
545 #define SWM_ARG_ID_FLIPLAYOUT (24)
546 #define SWM_ARG_ID_STACKRESET (30)
547 #define SWM_ARG_ID_STACKINIT (31)
548 #define SWM_ARG_ID_CYCLEWS_UP (40)
549 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
550 #define SWM_ARG_ID_CYCLESC_UP (42)
551 #define SWM_ARG_ID_CYCLESC_DOWN (43)
552 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
553 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
554 #define SWM_ARG_ID_STACKINC (50)
555 #define SWM_ARG_ID_STACKDEC (51)
556 #define SWM_ARG_ID_SS_ALL (60)
557 #define SWM_ARG_ID_SS_WINDOW (61)
558 #define SWM_ARG_ID_DONTCENTER (70)
559 #define SWM_ARG_ID_CENTER (71)
560 #define SWM_ARG_ID_KILLWINDOW (80)
561 #define SWM_ARG_ID_DELETEWINDOW (81)
562 #define SWM_ARG_ID_WIDTHGROW (90)
563 #define SWM_ARG_ID_WIDTHSHRINK (91)
564 #define SWM_ARG_ID_HEIGHTGROW (92)
565 #define SWM_ARG_ID_HEIGHTSHRINK (93)
566 #define SWM_ARG_ID_MOVEUP (100)
567 #define SWM_ARG_ID_MOVEDOWN (101)
568 #define SWM_ARG_ID_MOVELEFT (102)
569 #define SWM_ARG_ID_MOVERIGHT (103)
575 TAILQ_ENTRY(quirk) entry;
579 #define SWM_Q_FLOAT (1<<0) /* float this window */
580 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
581 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
582 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
583 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
584 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
586 TAILQ_HEAD(quirk_list, quirk);
587 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
590 * Supported EWMH hints should be added to
591 * both the enum and the ewmh array
596 _NET_MOVERESIZE_WINDOW,
597 _NET_WM_ACTION_CLOSE,
598 _NET_WM_ACTION_FULLSCREEN,
600 _NET_WM_ACTION_RESIZE,
601 _NET_WM_ALLOWED_ACTIONS,
604 _NET_WM_STATE_FULLSCREEN,
605 _NET_WM_STATE_HIDDEN,
606 _NET_WM_STATE_MAXIMIZED_HORZ,
607 _NET_WM_STATE_MAXIMIZED_VERT,
608 _NET_WM_STATE_SKIP_PAGER,
609 _NET_WM_STATE_SKIP_TASKBAR,
611 _NET_WM_WINDOW_TYPE_DIALOG,
612 _NET_WM_WINDOW_TYPE_DOCK,
613 _NET_WM_WINDOW_TYPE_NORMAL,
614 _NET_WM_WINDOW_TYPE_SPLASH,
615 _NET_WM_WINDOW_TYPE_TOOLBAR,
616 _NET_WM_WINDOW_TYPE_UTILITY,
617 _SWM_WM_STATE_MANUAL,
624 } ewmh[SWM_EWMH_HINT_MAX] = {
625 /* must be in same order as in the enum */
626 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
627 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
628 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
629 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
630 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
631 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
632 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
633 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
634 {"_NET_WM_STATE", XCB_ATOM_NONE},
635 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
636 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
637 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
638 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
639 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
640 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
641 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
642 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
643 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
644 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
645 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
646 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
647 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
648 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
649 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
652 /* function prototypes */
653 void buttonpress(xcb_button_press_event_t *);
654 void clientmessage(xcb_client_message_event_t *);
655 int conf_load(char *, int);
656 void configurenotify(xcb_configure_notify_event_t *);
657 void configurerequest(xcb_configure_request_event_t *);
658 void constrain_window(struct ws_win *, struct swm_region *, int);
659 void destroynotify(xcb_destroy_notify_event_t *);
661 void enternotify(xcb_enter_notify_event_t *);
662 void event_error(xcb_generic_error_t *);
663 void event_handle(xcb_generic_event_t *);
664 char *expand_tilde(char *);
665 void expose(xcb_expose_event_t *);
666 struct ws_win *find_window(xcb_window_t);
667 int floating_toggle_win(struct ws_win *);
668 void focus(struct swm_region *, union arg *);
669 void focus_magic(struct ws_win *);
671 void focusin(xcb_focus_in_event_t *);
673 xcb_atom_t get_atom_from_string(const char *);
675 char *get_atom_name(xcb_atom_t);
676 char *get_notify_detail_label(uint8_t);
677 char *get_notify_mode_label(uint8_t);
679 xcb_screen_t *get_screen(int);
680 char *get_win_name(xcb_window_t);
681 uint32_t getstate(xcb_window_t);
682 void grabbuttons(struct ws_win *, int);
683 void keypress(xcb_key_press_event_t *);
685 void leavenotify(xcb_leave_notify_event_t *);
687 void map_window_raised(xcb_window_t);
688 void mapnotify(xcb_map_notify_event_t *);
689 void mappingnotify(xcb_mapping_notify_event_t *);
690 void maprequest(xcb_map_request_event_t *);
691 void new_region(struct swm_screen *, int, int, int, int);
692 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
693 void propertynotify(xcb_property_notify_event_t *);
694 void spawn_select(struct swm_region *, union arg *, char *, int *);
695 void screenchange(xcb_randr_screen_change_notify_event_t *);
696 void store_float_geom(struct ws_win *, struct swm_region *);
697 void unmanage_window(struct ws_win *);
698 void unmapnotify(xcb_unmap_notify_event_t *);
699 void update_window(struct ws_win *);
700 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
703 expand_tilde(char *s)
707 char user[LOGIN_NAME_MAX];
712 errx(1, "expand_tilde: NULL string.");
720 for (i = 0; s[i] != PSC && s[i] != '\0'; ++i)
725 pwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
729 if (asprintf(&result, "%s%s", pwd->pw_dir, s) == -1)
733 errx(1, "expand_tilde: failed to allocate memory.");
739 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
741 unsigned int tmpr, tmpg, tmpb;
743 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
746 *rr = RGB_8_TO_16(tmpr);
747 *gg = RGB_8_TO_16(tmpg);
748 *bb = RGB_8_TO_16(tmpb);
754 get_screen(int screen)
756 xcb_screen_iterator_t i;
758 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
759 for (; i.rem; --screen, xcb_screen_next(&i))
769 xcb_get_input_focus_cookie_t c;
770 xcb_get_input_focus_reply_t *r;
773 c = xcb_get_input_focus(conn);
775 r = xcb_get_input_focus_reply(conn, c, NULL);
781 map_window_raised(xcb_window_t win)
783 uint32_t val = XCB_STACK_MODE_ABOVE;
785 xcb_configure_window(conn, win,
786 XCB_CONFIG_WINDOW_STACK_MODE, &val);
788 xcb_map_window(conn, win);
792 get_atom_from_string(const char *str)
794 xcb_intern_atom_cookie_t c;
795 xcb_intern_atom_reply_t *r;
798 c = xcb_intern_atom(conn, 0, strlen(str), str);
799 r = xcb_intern_atom_reply(conn, c, NULL);
807 return (XCB_ATOM_NONE);
811 update_iconic(struct ws_win *win, int newv)
818 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
819 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
821 xcb_delete_property(conn, win->id, a_swm_iconic);
825 get_iconic(struct ws_win *win)
828 xcb_get_property_reply_t *pr = NULL;
831 pr = xcb_get_property_reply(conn,
832 xcb_get_property(conn, 0, win->id, a_swm_iconic,
833 XCB_ATOM_INTEGER, 0, 1), NULL);
836 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
838 v = *((int32_t *)xcb_get_property_value(pr));
849 int i, j, num_screens;
851 sup_list = get_atom_from_string("_NET_SUPPORTED");
853 for (i = 0; i < LENGTH(ewmh); i++)
854 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
856 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
857 for (i = 0; i < num_screens; i++) {
858 /* Support check window will be created by workaround(). */
860 /* Report supported atoms */
861 xcb_delete_property(conn, screens[i].root, sup_list);
862 for (j = 0; j < LENGTH(ewmh); j++)
863 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
864 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
873 xcb_atom_t sup_check, sup_list;
875 xcb_get_property_cookie_t pc;
876 xcb_get_property_reply_t *pr;
878 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
879 sup_list = get_atom_from_string("_NET_SUPPORTED");
880 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
882 for (i = 0; i < num_screens; i++) {
883 /* Get the support check window and destroy it */
884 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
885 XCB_ATOM_WINDOW, 0, 1);
886 pr = xcb_get_property_reply(conn, pc, NULL);
889 if (pr->format == sup_check) {
890 id = *((xcb_window_t *)xcb_get_property_value(pr));
892 xcb_destroy_window(conn, id);
893 xcb_delete_property(conn, screens[i].root, sup_check);
894 xcb_delete_property(conn, screens[i].root, sup_list);
901 ewmh_autoquirk(struct ws_win *win)
905 xcb_get_property_cookie_t c;
906 xcb_get_property_reply_t *r;
908 c = xcb_get_property(conn, 0, win->id,
909 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
910 r = xcb_get_property_reply(conn, c, NULL);
914 n = xcb_get_property_value_length(r);
915 type = xcb_get_property_value(r);
917 for (i = 0; i < n; i++) {
918 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
920 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
921 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
922 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
924 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
927 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
928 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
930 win->quirks = SWM_Q_FLOAT;
937 #define SWM_EWMH_ACTION_COUNT_MAX (6)
938 #define EWMH_F_FULLSCREEN (1<<0)
939 #define EWMH_F_ABOVE (1<<1)
940 #define EWMH_F_HIDDEN (1<<2)
941 #define EWMH_F_SKIP_PAGER (1<<3)
942 #define EWMH_F_SKIP_TASKBAR (1<<4)
943 #define SWM_F_MANUAL (1<<5)
946 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
954 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
955 "fullscreen %s\n", win->id, YESNO(fs));
958 if (!win->g_floatvalid)
959 store_float_geom(win, win->ws->r);
961 win->g = win->ws->r->g;
964 if (win->g_floatvalid) {
965 /* refloat at last floating relative position */
966 win->g = win->g_float;
967 X(win) += X(win->ws->r);
968 Y(win) += Y(win->ws->r);
976 ewmh_update_actions(struct ws_win *win)
978 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
984 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
987 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
988 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
991 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
992 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
995 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
996 #define _NET_WM_STATE_ADD 1 /* add/set property */
997 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1000 ewmh_update_win_state(struct ws_win *win, long state, long action)
1002 unsigned int mask = 0;
1003 unsigned int changed = 0;
1004 unsigned int orig_flags;
1009 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1010 "action: %ld\n", win->id, state, action);
1012 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1013 mask = EWMH_F_FULLSCREEN;
1014 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1015 mask = EWMH_F_ABOVE;
1016 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1017 mask = SWM_F_MANUAL;
1018 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1019 mask = EWMH_F_SKIP_PAGER;
1020 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1021 mask = EWMH_F_SKIP_TASKBAR;
1024 orig_flags = win->ewmh_flags;
1027 case _NET_WM_STATE_REMOVE:
1028 win->ewmh_flags &= ~mask;
1030 case _NET_WM_STATE_ADD:
1031 win->ewmh_flags |= mask;
1033 case _NET_WM_STATE_TOGGLE:
1034 win->ewmh_flags ^= mask;
1038 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1040 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1042 if (!floating_toggle_win(win))
1043 win->ewmh_flags = orig_flags; /* revert */
1044 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1046 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1047 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1049 if (!ewmh_set_win_fullscreen(win,
1050 win->ewmh_flags & EWMH_F_FULLSCREEN))
1051 win->ewmh_flags = orig_flags; /* revert */
1053 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1055 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1056 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1057 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1058 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1059 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1060 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1061 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1062 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1063 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1064 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1065 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1066 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1067 if (win->ewmh_flags & EWMH_F_ABOVE)
1068 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1069 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1070 &ewmh[_NET_WM_STATE_ABOVE].atom);
1071 if (win->ewmh_flags & SWM_F_MANUAL)
1072 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1073 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1074 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1078 ewmh_get_win_state(struct ws_win *win)
1081 xcb_get_property_cookie_t c;
1082 xcb_get_property_reply_t *r;
1088 win->ewmh_flags = 0;
1090 win->ewmh_flags |= EWMH_F_ABOVE;
1092 win->ewmh_flags |= SWM_F_MANUAL;
1094 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1095 XCB_ATOM_ATOM, 0, UINT32_MAX);
1096 r = xcb_get_property_reply(conn, c, NULL);
1100 states = xcb_get_property_value(r);
1101 n = xcb_get_property_value_length(r);
1103 for (i = 0; i < n; i++)
1104 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1112 dumpwins(struct swm_region *r, union arg *args)
1116 xcb_get_window_attributes_cookie_t c;
1117 xcb_get_window_attributes_reply_t *wa;
1119 /* suppress unused warning since var is needed */
1122 if (r->ws == NULL) {
1123 warnx("dumpwins: invalid workspace");
1127 warnx("=== managed window list ws %02d ===", r->ws->idx);
1128 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1129 state = getstate(win->id);
1130 c = xcb_get_window_attributes(conn, win->id);
1131 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1133 warnx("window: 0x%x, map_state: %d, state: %u, "
1134 "transient: 0x%x", win->id, wa->map_state,
1135 state, win->transient);
1138 warnx("window: 0x%x, failed xcb_get_window_attributes",
1142 warnx("===== unmanaged window list =====");
1143 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1144 state = getstate(win->id);
1145 c = xcb_get_window_attributes(conn, win->id);
1146 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1148 warnx("window: 0x%x, map_state: %d, state: %u, "
1149 "transient: 0x%x", win->id, wa->map_state,
1150 state, win->transient);
1153 warnx("window: 0x%x, failed xcb_get_window_attributes",
1157 warnx("=================================");
1161 dumpwins(struct swm_region *r, union arg *args)
1164 #endif /* SWM_DEBUG */
1169 int saved_errno, status;
1172 saved_errno = errno;
1176 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1181 if (errno != ECHILD)
1182 warn("sighdlr: waitpid");
1183 #endif /* SWM_DEBUG */
1186 if (pid == searchpid)
1190 if (WIFEXITED(status)) {
1191 if (WEXITSTATUS(status) != 0)
1192 warnx("sighdlr: child exit status: %d",
1193 WEXITSTATUS(status));
1195 warnx("sighdlr: child is terminated "
1197 #endif /* SWM_DEBUG */
1211 errno = saved_errno;
1217 struct pid_e *p = NULL;
1219 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1224 TAILQ_FOREACH(p, &pidlist, entry) {
1233 name_to_pixel(const char *colorname)
1235 uint32_t result = 0;
1236 char cname[32] = "#";
1237 xcb_screen_t *screen;
1238 xcb_colormap_t cmap;
1239 xcb_alloc_color_reply_t *cr;
1240 xcb_alloc_named_color_reply_t *nr;
1241 uint16_t rr, gg, bb;
1243 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1244 cmap = screen->default_colormap;
1246 /* color is in format rgb://rr/gg/bb */
1247 if (strncmp(colorname, "rgb:", 4) == 0) {
1248 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1249 warnx("could not parse rgb %s", colorname);
1251 cr = xcb_alloc_color_reply(conn,
1252 xcb_alloc_color(conn, cmap, rr, gg, bb),
1258 warnx("color '%s' not found", colorname);
1261 nr = xcb_alloc_named_color_reply(conn,
1262 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1265 strlcat(cname, colorname + 2, sizeof cname - 1);
1266 nr = xcb_alloc_named_color_reply(conn,
1267 xcb_alloc_named_color(conn, cmap, strlen(cname),
1274 warnx("color '%s' not found", colorname);
1281 setscreencolor(char *val, int i, int c)
1285 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1286 if (i > 0 && i <= num_screens) {
1287 screens[i - 1].c[c].pixel = name_to_pixel(val);
1288 free(screens[i - 1].c[c].name);
1289 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1291 } else if (i == -1) {
1292 for (i = 0; i < num_screens; i++) {
1293 screens[i].c[c].pixel = name_to_pixel(val);
1294 free(screens[i].c[c].name);
1295 if ((screens[i].c[c].name = strdup(val)) == NULL)
1299 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1304 fancy_stacker(struct workspace *ws)
1306 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1307 if (ws->cur_layout->l_stack == vertical_stack)
1308 snprintf(ws->stacker, sizeof ws->stacker,
1309 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1310 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1311 if (ws->cur_layout->l_stack == horizontal_stack)
1312 snprintf(ws->stacker, sizeof ws->stacker,
1313 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1314 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1318 plain_stacker(struct workspace *ws)
1320 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1321 if (ws->cur_layout->l_stack == vertical_stack)
1322 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1323 sizeof ws->stacker);
1324 if (ws->cur_layout->l_stack == horizontal_stack)
1325 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1326 sizeof ws->stacker);
1330 custom_region(char *val)
1332 unsigned int x, y, w, h;
1333 int sidx, num_screens;
1334 xcb_screen_t *screen;
1336 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1337 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1338 errx(1, "invalid custom region, "
1339 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1340 if (sidx < 1 || sidx > num_screens)
1341 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1345 screen = get_screen(sidx);
1347 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1349 if (x > screen->width_in_pixels ||
1350 y > screen->height_in_pixels ||
1351 w + x > screen->width_in_pixels ||
1352 h + y > screen->height_in_pixels) {
1353 warnx("ignoring region %ux%u+%u+%u - not within screen "
1354 "boundaries (%ux%u)", w, h, x, y,
1355 screen->width_in_pixels, screen->height_in_pixels);
1359 new_region(&screens[sidx], x, y, w, h);
1363 socket_setnonblock(int fd)
1367 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1368 err(1, "fcntl F_GETFL");
1369 flags |= O_NONBLOCK;
1370 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1371 err(1, "fcntl F_SETFL");
1375 bar_print(struct swm_region *r, const char *s)
1378 xcb_rectangle_t rect;
1386 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1388 switch (bar_justify) {
1389 case SWM_BAR_JUSTIFY_LEFT:
1392 case SWM_BAR_JUSTIFY_CENTER:
1393 x = (WIDTH(r) - info.width) / 2;
1395 case SWM_BAR_JUSTIFY_RIGHT:
1396 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1400 if (x < SWM_BAR_OFFSET)
1405 rect.width = WIDTH(r->bar);
1406 rect.height = HEIGHT(r->bar);
1408 /* clear back buffer */
1409 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1410 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1411 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1412 sizeof(rect), &rect);
1414 /* draw back buffer */
1415 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1416 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1417 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].pixel;
1418 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1420 draw = XftDrawCreate(display, r->bar->buffer,
1421 DefaultVisual(display, r->s->idx),
1422 DefaultColormap(display, r->s->idx));
1424 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1425 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1428 XftDrawDestroy(draw);
1431 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1432 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1436 bar_extra_stop(void)
1440 bzero(bar_pipe, sizeof bar_pipe);
1443 kill(bar_pid, SIGTERM);
1446 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1451 bar_class_name(char *s, size_t sz, struct swm_region *r)
1453 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1455 if (r->ws->focus->ch.class_name != NULL)
1456 strlcat(s, r->ws->focus->ch.class_name, sz);
1460 bar_title_name(char *s, size_t sz, struct swm_region *r)
1462 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1464 if (r->ws->focus->ch.instance_name != NULL)
1465 strlcat(s, r->ws->focus->ch.instance_name, sz);
1469 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1471 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1474 bar_class_name(s, sz, r);
1475 strlcat(s, ":", sz);
1476 bar_title_name(s, sz, r);
1480 bar_window_float(char *s, size_t sz, struct swm_region *r)
1482 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1484 if (r->ws->focus->floating)
1485 strlcat(s, "(f)", sz);
1489 bar_window_name(char *s, size_t sz, struct swm_region *r)
1493 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1495 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1498 strlcat(s, title, sz);
1502 int urgent[SWM_WS_MAX];
1504 bar_urgent(char *s, size_t sz)
1507 int i, j, num_screens;
1509 xcb_get_property_cookie_t c;
1510 xcb_icccm_wm_hints_t hints;
1512 for (i = 0; i < workspace_limit; i++)
1515 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1516 for (i = 0; i < num_screens; i++)
1517 for (j = 0; j < workspace_limit; j++)
1518 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1519 c = xcb_icccm_get_wm_hints(conn, win->id);
1520 if (xcb_icccm_get_wm_hints_reply(conn, c,
1523 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1527 for (i = 0; i < workspace_limit; i++) {
1529 snprintf(b, sizeof b, "%d ", i + 1);
1531 snprintf(b, sizeof b, "- ");
1537 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1539 if (r == NULL || r->ws == NULL)
1541 if (r->ws->name != NULL)
1542 strlcat(s, r->ws->name, sz);
1545 /* build the default bar format according to the defined enabled options */
1547 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1549 /* if format provided, just copy the buffers */
1550 if (bar_format != NULL) {
1551 strlcpy(fmtnew, fmtexp, sz);
1555 /* reset the output buffer */
1558 strlcat(fmtnew, "+N:+I ", sz);
1560 strlcat(fmtnew, "+S", sz);
1561 strlcat(fmtnew, " ", sz);
1563 /* only show the workspace name if there's actually one */
1564 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1565 strlcat(fmtnew, "<+D>", sz);
1566 strlcat(fmtnew, "+3<", sz);
1568 if (clock_enabled) {
1569 strlcat(fmtnew, fmtexp, sz);
1570 strlcat(fmtnew, "+4<", sz);
1573 /* bar_urgent already adds the space before the last asterisk */
1575 strlcat(fmtnew, "* +U*+4<", sz);
1577 if (title_class_enabled) {
1578 strlcat(fmtnew, "+C", sz);
1579 if (title_name_enabled == 0)
1580 strlcat(fmtnew, "+4<", sz);
1583 /* checks needed by the colon and floating strlcat(3) calls below */
1584 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1585 if (title_name_enabled) {
1586 if (title_class_enabled)
1587 strlcat(fmtnew, ":", sz);
1588 strlcat(fmtnew, "+T+4<", sz);
1590 if (window_name_enabled) {
1591 if (r->ws->focus->floating)
1592 strlcat(fmtnew, "+F ", sz);
1593 strlcat(fmtnew, "+64W ", sz);
1597 /* finally add the action script output and the version */
1598 strlcat(fmtnew, "+4<+A+4<+V", sz);
1602 bar_replace_pad(char *tmp, int *limit, size_t sz)
1604 /* special case; no limit given, pad one space, instead */
1605 if (*limit == (int)sz - 1)
1607 snprintf(tmp, sz, "%*s", *limit, " ");
1610 /* replaces the bar format character sequences (like in tmux(1)) */
1612 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1616 char tmp[SWM_BAR_MAX];
1620 /* reset strlcat(3) buffer */
1623 /* get number, if any */
1626 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1627 limit = sizeof tmp - 1;
1628 if (limit <= 0 || limit >= (int)sizeof tmp)
1629 limit = sizeof tmp - 1;
1631 /* there is nothing to replace (ie EOL) */
1638 bar_replace_pad(tmp, &limit, sizeof tmp);
1641 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1644 bar_class_name(tmp, sizeof tmp, r);
1647 bar_workspace_name(tmp, sizeof tmp, r);
1650 bar_window_float(tmp, sizeof tmp, r);
1653 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1656 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1659 bar_class_title_name(tmp, sizeof tmp, r);
1662 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1665 bar_title_name(tmp, sizeof tmp, r);
1668 bar_urgent(tmp, sizeof tmp);
1671 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1674 bar_window_name(tmp, sizeof tmp, r);
1677 /* unknown character sequence; copy as-is */
1678 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1684 if ((int)len < limit)
1686 while (limit-- > 0) {
1687 if (*offrep >= sz - 1)
1689 fmtrep[(*offrep)++] = *ptr++;
1697 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1702 while (*fmt != '\0') {
1704 /* skip ordinary characters */
1707 fmtrep[off++] = *fmt++;
1711 /* character sequence found; replace it */
1712 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1721 bar_fmt_expand(char *fmtexp, size_t sz)
1728 /* start by grabbing the current time and date */
1730 localtime_r(&tmt, &tm);
1732 /* figure out what to expand */
1733 if (bar_format != NULL)
1735 else if (bar_format == NULL && clock_enabled)
1737 /* if nothing to expand bail out */
1743 /* copy as-is, just in case the format shouldn't be expanded below */
1744 strlcpy(fmtexp, fmt, sz);
1745 /* finally pass the string through strftime(3) */
1746 #ifndef SWM_DENY_CLOCK_FORMAT
1747 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1748 warnx("format too long");
1756 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1757 char fmtrep[SWM_BAR_MAX];
1759 struct swm_region *r;
1761 /* expand the format by first passing it through strftime(3) */
1762 bar_fmt_expand(fmtexp, sizeof fmtexp);
1764 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1765 for (i = 0; i < num_screens; i++) {
1766 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1769 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1770 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1771 bar_print(r, fmtrep);
1782 if (bar_enabled == 0)
1784 if (bar_extra && bar_extra_running) {
1785 /* ignore short reads; it'll correct itself */
1786 while ((b = fgetln(stdin, &len)) != NULL)
1787 if (b && b[len - 1] == '\n') {
1789 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1791 if (b == NULL && errno != EAGAIN) {
1792 warn("bar_update: bar_extra failed");
1796 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1805 /* suppress unused warning since var is needed */
1812 bar_toggle(struct swm_region *r, union arg *args)
1814 struct swm_region *tmpr;
1817 /* suppress unused warnings since vars are needed */
1821 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1823 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1825 for (i = 0; i < num_screens; i++)
1826 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1828 xcb_unmap_window(conn, tmpr->bar->id);
1830 for (i = 0; i < num_screens; i++)
1831 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1833 map_window_raised(tmpr->bar->id);
1836 bar_enabled = !bar_enabled;
1839 /* must be after stack */
1848 struct swm_region *r;
1852 /* do this here because the conf file is in memory */
1853 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1854 /* launch external status app */
1855 bar_extra_running = 1;
1856 if (pipe(bar_pipe) == -1)
1857 err(1, "pipe error");
1858 socket_setnonblock(bar_pipe[0]);
1859 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1860 if (dup2(bar_pipe[0], 0) == -1)
1862 if (dup2(bar_pipe[1], 1) == -1)
1864 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1865 err(1, "could not disable SIGPIPE");
1866 switch (bar_pid = fork()) {
1868 err(1, "cannot fork");
1872 execvp(bar_argv[0], bar_argv);
1873 err(1, "%s external app failed", bar_argv[0]);
1875 default: /* parent */
1881 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1882 for (i = 0; i < num_screens; i++)
1883 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1886 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1887 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1888 xcb_change_window_attributes(conn, r->bar->id,
1889 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1895 bar_setup(struct swm_region *r)
1897 char *font, *fontpos, *dup, *search;
1899 xcb_screen_t *screen = get_screen(r->s->idx);
1906 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1907 err(1, "bar_setup: calloc: failed to allocate memory.");
1909 if (bar_font == NULL) {
1910 if ((dup = strdup(bar_fonts)) == NULL)
1911 errx(1, "insufficient memory.");
1913 while ((font = strsep(&search, ",")) != NULL) {
1917 DNPRINTF(SWM_D_INIT, "bar_setup: try font %s\n", font);
1921 while ((fontpos = index(fontpos, '-'))) {
1927 bar_font = XftFontOpenXlfd(display, r->s->idx,
1930 bar_font = XftFontOpenName(display, r->s->idx,
1933 warnx("unable to load font %s", font);
1936 DNPRINTF(SWM_D_INIT, "successfully opened "
1944 if (bar_font == NULL)
1945 errx(1, "unable to open a font");
1947 bar_height = bar_font->height + 2 * bar_border_width;
1952 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
1954 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
1955 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
1956 warn("unable to allocate Xft color");
1959 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1960 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1961 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1963 r->bar->id = xcb_generate_id(conn);
1964 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1965 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
1966 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1968 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1969 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1970 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1971 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1972 | XCB_CW_EVENT_MASK, wa);
1974 r->bar->buffer = xcb_generate_id(conn);
1975 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1976 WIDTH(r->bar), HEIGHT(r->bar));
1978 xcb_randr_select_input(conn, r->bar->id,
1979 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1982 map_window_raised(r->bar->id);
1984 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1985 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1988 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1989 err(1, "could not install bar_signal");
1994 bar_cleanup(struct swm_region *r)
1998 xcb_destroy_window(conn, r->bar->id);
1999 xcb_free_pixmap(conn, r->bar->buffer);
2005 set_win_state(struct ws_win *win, uint16_t state)
2007 uint16_t data[2] = { state, XCB_ATOM_NONE };
2009 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2014 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2015 a_state, 32, 2, data);
2019 getstate(xcb_window_t w)
2021 uint32_t result = 0;
2022 xcb_get_property_cookie_t c;
2023 xcb_get_property_reply_t *r;
2025 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2026 r = xcb_get_property_reply(conn, c, NULL);
2029 if (r->type == a_state && r->format == 32 && r->length == 2)
2030 result = *((uint32_t *)xcb_get_property_value(r));
2034 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2040 version(struct swm_region *r, union arg *args)
2042 /* suppress unused warnings since vars are needed */
2046 bar_version = !bar_version;
2048 snprintf(bar_vertext, sizeof bar_vertext,
2049 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2051 strlcpy(bar_vertext, "", sizeof bar_vertext);
2058 client_msg(struct ws_win *win, xcb_atom_t a)
2060 xcb_client_message_event_t ev;
2065 bzero(&ev, sizeof ev);
2066 ev.response_type = XCB_CLIENT_MESSAGE;
2067 ev.window = win->id;
2070 ev.data.data32[0] = a;
2071 ev.data.data32[1] = XCB_CURRENT_TIME;
2073 xcb_send_event(conn, 0, win->id,
2074 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2077 /* synthetic response to a ConfigureRequest when not making a change */
2079 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2081 xcb_configure_notify_event_t ce;
2086 /* send notification of unchanged state. */
2087 bzero(&ce, sizeof(ce));
2088 ce.response_type = XCB_CONFIGURE_NOTIFY;
2091 ce.width = WIDTH(win);
2092 ce.height = HEIGHT(win);
2093 ce.override_redirect = 0;
2098 ce.window = win->id;
2099 ce.border_width = BORDER(win);
2100 ce.above_sibling = XCB_WINDOW_NONE;
2103 ce.event = ev->window;
2104 ce.window = ev->window;
2106 /* make response appear more WM_SIZE_HINTS-compliant */
2108 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2109 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2110 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2111 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2112 SH_INC_W(win), SH_INC_H(win));
2116 /* the hint may be set... to 0! */
2117 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2118 ce.width = SH_MIN_W(win);
2119 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2120 ce.height = SH_MIN_H(win);
2125 /* may also be advertized as 0 */
2126 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2127 ce.width = SH_MAX_W(win);
2128 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2129 ce.height = SH_MAX_H(win);
2132 /* resize increment. */
2134 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2135 ce.width -= (ce.width - SH_MIN_W(win)) %
2137 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2138 ce.height -= (ce.height - SH_MIN_H(win)) %
2142 /* adjust x and y for requested border_width. */
2143 ce.x += BORDER(win) - ev->border_width;
2144 ce.y += BORDER(win) - ev->border_width;
2145 ce.border_width = ev->border_width;
2146 ce.above_sibling = ev->sibling;
2149 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2150 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2151 ce.y, ce.width, ce.height, ce.border_width);
2153 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2158 count_win(struct workspace *ws, int count_transient)
2163 TAILQ_FOREACH(win, &ws->winlist, entry) {
2164 if (count_transient == 0 && win->floating)
2166 if (count_transient == 0 && win->transient)
2172 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2178 quit(struct swm_region *r, union arg *args)
2180 /* suppress unused warnings since vars are needed */
2184 DNPRINTF(SWM_D_MISC, "quit\n");
2189 unmap_window(struct ws_win *win)
2191 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2196 /* don't unmap again */
2197 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2200 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2202 xcb_unmap_window(conn, win->id);
2203 xcb_change_window_attributes(conn, win->id,
2204 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2211 int i, j, num_screens;
2213 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2214 for (i = 0; i < num_screens; i++)
2215 for (j = 0; j < workspace_limit; j++)
2216 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2221 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2223 xcb_key_press_event_t event;
2224 xcb_keycode_t *keycode;
2229 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2231 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2234 bzero(&event, sizeof(event));
2235 event.event = win->id;
2236 event.root = win->s->root;
2237 event.child = XCB_WINDOW_NONE;
2238 event.time = XCB_CURRENT_TIME;
2239 event.event_x = X(win);
2240 event.event_y = Y(win);
2243 event.same_screen = 1;
2244 event.detail = *keycode;
2245 event.state = modifiers;
2247 event.response_type = XCB_KEY_PRESS;
2248 xcb_send_event(conn, 1, win->id,
2249 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2251 event.response_type = XCB_KEY_RELEASE;
2252 xcb_send_event(conn, 1, win->id,
2253 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2259 restart(struct swm_region *r, union arg *args)
2261 /* suppress unused warning since var is needed */
2264 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2266 /* disable alarm because the following code may not be interrupted */
2268 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2269 err(1, "can't disable alarm");
2275 XftFontClose(display, bar_font);
2276 XftColorFree(display, DefaultVisual(display, r->s->idx),
2277 DefaultColormap(display, r->s->idx), &bar_font_color);
2278 xcb_key_symbols_free(syms);
2280 xcb_disconnect(conn);
2282 execvp(start_argv[0], start_argv);
2283 warn("execvp failed");
2288 root_to_region(xcb_window_t root)
2290 struct swm_region *r = NULL;
2292 xcb_query_pointer_reply_t *qpr;
2294 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2296 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2297 for (i = 0; i < num_screens; i++)
2298 if (screens[i].root == root)
2301 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2302 screens[i].root), NULL);
2305 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2306 qpr->root_x, qpr->root_y);
2307 /* choose a region based on pointer location */
2308 TAILQ_FOREACH(r, &screens[i].rl, entry)
2309 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2310 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2316 r = TAILQ_FIRST(&screens[i].rl);
2322 find_unmanaged_window(xcb_window_t id)
2325 int i, j, num_screens;
2327 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2328 for (i = 0; i < num_screens; i++)
2329 for (j = 0; j < workspace_limit; j++)
2330 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2338 find_window(xcb_window_t id)
2341 int i, j, num_screens;
2342 xcb_query_tree_reply_t *r;
2344 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2345 for (i = 0; i < num_screens; i++)
2346 for (j = 0; j < workspace_limit; j++)
2347 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2351 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2355 /* if we were looking for the parent return that window instead */
2356 if (r->parent == 0 || r->root == r->parent) {
2361 /* look for parent */
2362 for (i = 0; i < num_screens; i++)
2363 for (j = 0; j < workspace_limit; j++)
2364 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2365 if (r->parent == win->id) {
2375 spawn(int ws_idx, union arg *args, int close_fd)
2380 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2382 close(xcb_get_file_descriptor(conn));
2384 setenv("LD_PRELOAD", SWM_LIB, 1);
2386 if (asprintf(&ret, "%d", ws_idx) == -1) {
2387 warn("spawn: asprintf SWM_WS");
2390 setenv("_SWM_WS", ret, 1);
2394 if (asprintf(&ret, "%d", getpid()) == -1) {
2395 warn("spawn: asprintf _SWM_PID");
2398 setenv("_SWM_PID", ret, 1);
2402 if (setsid() == -1) {
2403 warn("spawn: setsid");
2409 * close stdin and stdout to prevent interaction between apps
2410 * and the baraction script
2411 * leave stderr open to record errors
2413 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2414 warn("spawn: open");
2417 dup2(fd, STDIN_FILENO);
2418 dup2(fd, STDOUT_FILENO);
2423 execvp(args->argv[0], args->argv);
2425 warn("spawn: execvp");
2430 kill_refs(struct ws_win *win)
2432 int i, x, num_screens;
2433 struct swm_region *r;
2434 struct workspace *ws;
2439 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2440 for (i = 0; i < num_screens; i++)
2441 TAILQ_FOREACH(r, &screens[i].rl, entry)
2442 for (x = 0; x < workspace_limit; x++) {
2444 if (win == ws->focus)
2446 if (win == ws->focus_prev)
2447 ws->focus_prev = NULL;
2452 validate_win(struct ws_win *testwin)
2455 struct workspace *ws;
2456 struct swm_region *r;
2457 int i, x, num_screens;
2459 if (testwin == NULL)
2462 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2463 for (i = 0; i < num_screens; i++)
2464 TAILQ_FOREACH(r, &screens[i].rl, entry)
2465 for (x = 0; x < workspace_limit; x++) {
2467 TAILQ_FOREACH(win, &ws->winlist, entry)
2475 validate_ws(struct workspace *testws)
2477 struct swm_region *r;
2478 struct workspace *ws;
2479 int i, x, num_screens;
2481 /* validate all ws */
2482 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2483 for (i = 0; i < num_screens; i++)
2484 TAILQ_FOREACH(r, &screens[i].rl, entry)
2485 for (x = 0; x < workspace_limit; x++) {
2494 unfocus_win(struct ws_win *win)
2496 xcb_window_t none = XCB_WINDOW_NONE;
2498 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2502 if (win->ws == NULL)
2505 if (validate_ws(win->ws))
2506 return; /* XXX this gets hit with thunderbird, needs fixing */
2508 if (win->ws->r == NULL)
2511 if (validate_win(win)) {
2516 if (win->ws->focus == win) {
2517 win->ws->focus = NULL;
2518 win->ws->focus_prev = win;
2521 if (validate_win(win->ws->focus)) {
2522 kill_refs(win->ws->focus);
2523 win->ws->focus = NULL;
2525 if (validate_win(win->ws->focus_prev)) {
2526 kill_refs(win->ws->focus_prev);
2527 win->ws->focus_prev = NULL;
2530 grabbuttons(win, 0);
2531 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2532 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2533 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2534 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2541 int i, j, num_screens;
2543 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2545 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2546 for (i = 0; i < num_screens; i++)
2547 for (j = 0; j < workspace_limit; j++)
2548 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2553 focus_win(struct ws_win *win)
2555 struct ws_win *cfw = NULL;
2556 xcb_get_input_focus_reply_t *r;
2557 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2559 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2563 if (win->ws == NULL)
2566 if (validate_ws(win->ws))
2567 return; /* XXX this gets hit with thunderbird, needs fixing */
2569 if (validate_win(win)) {
2574 if (validate_win(win)) {
2579 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2581 cur_focus = r->focus;
2584 if ((cfw = find_window(cur_focus)) != NULL)
2588 /* use larger hammer since the window was killed somehow */
2589 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2590 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2591 xcb_change_window_attributes(conn, cfw->id,
2592 XCB_CW_BORDER_PIXEL,
2593 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2597 win->ws->focus = win;
2599 if (win->ws->r != NULL) {
2601 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2602 win->id, XCB_CURRENT_TIME);
2603 grabbuttons(win, 1);
2604 xcb_change_window_attributes(conn, win->id,
2605 XCB_CW_BORDER_PIXEL,
2606 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2607 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2608 win->ws->always_raise)
2609 map_window_raised(win->id);
2611 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2612 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2620 event_drain(uint8_t rt)
2622 xcb_generic_event_t *evt;
2625 while ((evt = xcb_poll_for_event(conn))) {
2626 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2634 switchws(struct swm_region *r, union arg *args)
2636 int wsid = args->id, unmap_old = 0;
2637 struct swm_region *this_r, *other_r;
2639 struct workspace *new_ws, *old_ws;
2645 if (wsid >= workspace_limit)
2649 old_ws = this_r->ws;
2650 new_ws = &this_r->s->ws[wsid];
2652 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2653 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2655 if (new_ws == NULL || old_ws == NULL)
2657 if (new_ws == old_ws)
2660 other_r = new_ws->r;
2661 if (other_r == NULL) {
2662 /* the other workspace is hidden, hide this one */
2666 /* the other ws is visible in another region, exchange them */
2667 other_r->ws_prior = new_ws;
2668 other_r->ws = old_ws;
2669 old_ws->r = other_r;
2671 this_r->ws_prior = old_ws;
2672 this_r->ws = new_ws;
2675 /* this is needed so that we can click on a window after a restart */
2679 a.id = SWM_ARG_ID_FOCUSCUR;
2680 focus(new_ws->r, &a);
2682 /* unmap old windows */
2684 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2687 if (focus_mode == SWM_FOCUS_DEFAULT)
2688 event_drain(XCB_ENTER_NOTIFY);
2692 DNPRINTF(SWM_D_WS, "switchws: done\n");
2696 cyclews(struct swm_region *r, union arg *args)
2699 struct swm_screen *s = r->s;
2702 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2703 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2708 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2711 case SWM_ARG_ID_CYCLEWS_UP:
2712 if (a.id < workspace_limit - 1)
2717 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2720 case SWM_ARG_ID_CYCLEWS_DOWN:
2724 a.id = workspace_limit - 1;
2731 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2733 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2737 } while (a.id != r->ws->idx);
2741 priorws(struct swm_region *r, union arg *args)
2745 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2746 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2748 if (r->ws_prior == NULL)
2751 a.id = r->ws_prior->idx;
2756 cyclescr(struct swm_region *r, union arg *args)
2758 struct swm_region *rr = NULL;
2760 int i, x, y, num_screens;
2762 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2763 /* do nothing if we don't have more than one screen */
2764 if (!(num_screens > 1 || outputs > 1))
2769 case SWM_ARG_ID_CYCLESC_UP:
2770 rr = TAILQ_NEXT(r, entry);
2772 rr = TAILQ_FIRST(&screens[i].rl);
2774 case SWM_ARG_ID_CYCLESC_DOWN:
2775 rr = TAILQ_PREV(r, swm_region_list, entry);
2777 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2785 /* move mouse to region */
2787 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2788 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2791 a.id = SWM_ARG_ID_FOCUSCUR;
2794 if (rr->ws->focus) {
2795 /* move to focus window */
2796 x = X(rr->ws->focus) + 1;
2797 y = Y(rr->ws->focus) + 1;
2798 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2804 sort_windows(struct ws_win_list *wl)
2806 struct ws_win *win, *parent, *nxt;
2811 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2812 nxt = TAILQ_NEXT(win, entry);
2813 if (win->transient) {
2814 parent = find_window(win->transient);
2815 if (parent == NULL) {
2816 warnx("not possible bug");
2819 TAILQ_REMOVE(wl, win, entry);
2820 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2827 swapwin(struct swm_region *r, union arg *args)
2829 struct ws_win *target, *source;
2830 struct ws_win *cur_focus;
2831 struct ws_win_list *wl;
2834 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2835 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2837 cur_focus = r->ws->focus;
2838 if (cur_focus == NULL)
2842 wl = &source->ws->winlist;
2845 case SWM_ARG_ID_SWAPPREV:
2846 if (source->transient)
2847 source = find_window(source->transient);
2848 target = TAILQ_PREV(source, ws_win_list, entry);
2849 if (target && target->transient)
2850 target = find_window(target->transient);
2851 TAILQ_REMOVE(wl, source, entry);
2853 TAILQ_INSERT_TAIL(wl, source, entry);
2855 TAILQ_INSERT_BEFORE(target, source, entry);
2857 case SWM_ARG_ID_SWAPNEXT:
2858 target = TAILQ_NEXT(source, entry);
2859 /* move the parent and let the sort handle the move */
2860 if (source->transient)
2861 source = find_window(source->transient);
2862 TAILQ_REMOVE(wl, source, entry);
2864 TAILQ_INSERT_HEAD(wl, source, entry);
2866 TAILQ_INSERT_AFTER(wl, target, source, entry);
2868 case SWM_ARG_ID_SWAPMAIN:
2869 target = TAILQ_FIRST(wl);
2870 if (target == source) {
2871 if (source->ws->focus_prev != NULL &&
2872 source->ws->focus_prev != target)
2873 source = source->ws->focus_prev;
2877 if (target == NULL || source == NULL)
2879 source->ws->focus_prev = target;
2880 TAILQ_REMOVE(wl, target, entry);
2881 TAILQ_INSERT_BEFORE(source, target, entry);
2882 TAILQ_REMOVE(wl, source, entry);
2883 TAILQ_INSERT_HEAD(wl, source, entry);
2885 case SWM_ARG_ID_MOVELAST:
2886 TAILQ_REMOVE(wl, source, entry);
2887 TAILQ_INSERT_TAIL(wl, source, entry);
2890 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2902 focus_prev(struct ws_win *win)
2904 struct ws_win *winfocus = NULL;
2905 struct ws_win *cur_focus = NULL;
2906 struct ws_win_list *wl = NULL;
2907 struct workspace *ws = NULL;
2909 if (!(win && win->ws))
2914 cur_focus = ws->focus;
2916 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2917 WINID(win), WINID(cur_focus));
2919 /* pickle, just focus on whatever */
2920 if (cur_focus == NULL) {
2921 /* use prev_focus if valid */
2922 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2923 find_window(WINID(ws->focus_prev)))
2924 winfocus = ws->focus_prev;
2928 /* if transient focus on parent */
2929 if (cur_focus->transient) {
2930 winfocus = find_window(cur_focus->transient);
2934 /* if in max_stack try harder */
2935 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2936 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2937 if (cur_focus != ws->focus_prev)
2938 winfocus = ws->focus_prev;
2940 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2945 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2947 if (winfocus == NULL || winfocus == win) {
2948 switch (focus_close) {
2949 case SWM_STACK_BOTTOM:
2950 winfocus = TAILQ_FIRST(wl);
2953 winfocus = TAILQ_LAST(wl, ws_win_list);
2955 case SWM_STACK_ABOVE:
2956 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2957 if (focus_close_wrap)
2958 winfocus = TAILQ_FIRST(wl);
2960 winfocus = TAILQ_PREV(cur_focus,
2961 ws_win_list, entry);
2964 case SWM_STACK_BELOW:
2965 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2967 if (focus_close_wrap)
2968 winfocus = TAILQ_LAST(wl, ws_win_list);
2970 winfocus = TAILQ_NEXT(cur_focus, entry);
2976 if (winfocus == NULL) {
2977 if (focus_default == SWM_STACK_TOP)
2978 winfocus = TAILQ_LAST(wl, ws_win_list);
2980 winfocus = TAILQ_FIRST(wl);
2984 focus_magic(winfocus);
2988 focus(struct swm_region *r, union arg *args)
2990 struct ws_win *winfocus = NULL, *head;
2991 struct ws_win *cur_focus = NULL;
2992 struct ws_win_list *wl = NULL;
2993 struct workspace *ws = NULL;
2999 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3001 /* treat FOCUS_CUR special */
3002 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3003 if (r->ws->focus && r->ws->focus->iconic == 0)
3004 winfocus = r->ws->focus;
3005 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3006 winfocus = r->ws->focus_prev;
3008 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3009 if (winfocus->iconic == 0)
3012 focus_magic(winfocus);
3016 if ((cur_focus = r->ws->focus) == NULL)
3020 if (TAILQ_EMPTY(wl))
3022 /* make sure there is at least one uniconified window */
3024 TAILQ_FOREACH(winfocus, wl, entry)
3025 if (winfocus->iconic == 0) {
3033 case SWM_ARG_ID_FOCUSPREV:
3034 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3036 head = TAILQ_LAST(wl, ws_win_list);
3038 if (WINID(winfocus) == cur_focus->transient) {
3039 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3041 head = TAILQ_LAST(wl, ws_win_list);
3046 if (winfocus && winfocus->iconic) {
3047 while (winfocus != cur_focus) {
3048 if (winfocus == NULL)
3049 winfocus = TAILQ_LAST(wl, ws_win_list);
3050 if (winfocus->iconic == 0)
3052 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3058 case SWM_ARG_ID_FOCUSNEXT:
3059 head = TAILQ_NEXT(cur_focus, entry);
3061 head = TAILQ_FIRST(wl);
3065 if (winfocus && winfocus->iconic) {
3066 while (winfocus != cur_focus) {
3067 if (winfocus == NULL)
3068 winfocus = TAILQ_FIRST(wl);
3069 if (winfocus->iconic == 0)
3071 winfocus = TAILQ_NEXT(winfocus, entry);
3076 case SWM_ARG_ID_FOCUSMAIN:
3077 winfocus = TAILQ_FIRST(wl);
3078 if (winfocus == cur_focus)
3079 winfocus = cur_focus->ws->focus_prev;
3086 focus_magic(winfocus);
3092 cycle_layout(struct swm_region *r, union arg *args)
3094 struct workspace *ws = r->ws;
3097 /* suppress unused warning since var is needed */
3100 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3103 if (ws->cur_layout->l_stack == NULL)
3104 ws->cur_layout = &layouts[0];
3109 if (focus_mode == SWM_FOCUS_DEFAULT)
3110 event_drain(XCB_ENTER_NOTIFY);
3112 a.id = SWM_ARG_ID_FOCUSCUR;
3117 stack_config(struct swm_region *r, union arg *args)
3119 struct workspace *ws = r->ws;
3121 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3124 if (ws->cur_layout->l_config != NULL)
3125 ws->cur_layout->l_config(ws, args->id);
3127 if (args->id != SWM_ARG_ID_STACKINIT)
3134 struct swm_geometry g;
3135 struct swm_region *r;
3141 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3143 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3144 for (i = 0; i < num_screens; i++) {
3148 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3149 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3150 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3152 /* start with screen geometry, adjust for bar */
3154 g.w -= 2 * border_width;
3155 g.h -= 2 * border_width;
3161 r->ws->cur_layout->l_stack(r->ws, &g);
3162 r->ws->cur_layout->l_string(r->ws);
3163 /* save r so we can track region changes */
3170 if (focus_mode == SWM_FOCUS_DEFAULT)
3171 event_drain(XCB_ENTER_NOTIFY);
3173 DNPRINTF(SWM_D_STACK, "stack: end\n");
3177 store_float_geom(struct ws_win *win, struct swm_region *r)
3179 /* retain window geom and region geom */
3180 win->g_float = win->g;
3181 win->g_float.x -= X(r);
3182 win->g_float.y -= Y(r);
3183 win->g_floatvalid = 1;
3184 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3185 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3186 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3187 win->g_float.w, win->g_float.h);
3191 stack_floater(struct ws_win *win, struct swm_region *r)
3196 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3199 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3200 * geom on ws switches or return from max mode
3202 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3203 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3204 /* refloat at last floating relative position */
3205 win->g = win->g_float;
3210 win->floatmaxed = 0;
3213 * if set to fullscreen mode, configure window to maximum size.
3215 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3216 if (!win->g_floatvalid)
3217 store_float_geom(win, win->ws->r);
3219 win->g = win->ws->r->g;
3223 * remove border on fullscreen floater when in fullscreen mode or when
3224 * the quirk is present.
3226 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3227 ((win->quirks & SWM_Q_FULLSCREEN) &&
3228 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3229 if (win->bordered) {
3231 X(win) += border_width;
3232 Y(win) += border_width;
3234 } else if (!win->bordered) {
3236 X(win) -= border_width;
3237 Y(win) -= border_width;
3240 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3241 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3242 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3247 * floaters and transients are auto-centred unless moved
3250 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3251 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3254 /* keep window within region bounds */
3255 constrain_window(win, r, 0);
3261 * Send keystrokes to terminal to decrease/increase the font size as the
3262 * window size changes.
3265 adjust_font(struct ws_win *win)
3267 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3268 win->floating || win->transient)
3271 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3272 WIDTH(win) / win->sh.width_inc < term_width &&
3273 win->font_steps < SWM_MAX_FONT_STEPS) {
3274 win->font_size_boundary[win->font_steps] =
3275 (win->sh.width_inc * term_width) + win->sh.base_width;
3278 win->last_inc = win->sh.width_inc;
3279 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3280 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3281 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3284 win->last_inc = win->sh.width_inc;
3285 fake_keypress(win, XK_KP_Add, ShiftMask);
3289 #define SWAPXY(g) do { \
3291 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3292 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3295 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3297 struct swm_geometry win_g, r_g = *g;
3298 struct ws_win *win, *fs_win = NULL;
3299 int i, j, s, stacks;
3300 int w_inc = 1, h_inc, w_base = 1, h_base;
3301 int hrh, extra = 0, h_slice, last_h = 0;
3302 int split, colno, winno, mwin, msize, mscale;
3303 int remain, missing, v_slice, reconfigure;
3305 xcb_get_window_attributes_reply_t *war;
3307 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3308 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3310 winno = count_win(ws, 0);
3311 if (winno == 0 && count_win(ws, 1) == 0)
3314 TAILQ_FOREACH(win, &ws->winlist, entry)
3315 if (win->transient == 0 && win->floating == 0
3316 && win->iconic == 0)
3323 w_inc = win->sh.width_inc;
3324 w_base = win->sh.base_width;
3325 mwin = ws->l_state.horizontal_mwin;
3326 mscale = ws->l_state.horizontal_msize;
3327 stacks = ws->l_state.horizontal_stacks;
3330 w_inc = win->sh.height_inc;
3331 w_base = win->sh.base_height;
3332 mwin = ws->l_state.vertical_mwin;
3333 mscale = ws->l_state.vertical_msize;
3334 stacks = ws->l_state.vertical_stacks;
3338 if (stacks > winno - mwin)
3339 stacks = winno - mwin;
3343 h_slice = r_g.h / SWM_H_SLICE;
3344 if (mwin && winno > mwin) {
3345 v_slice = r_g.w / SWM_V_SLICE;
3349 win_g.w = v_slice * mscale;
3351 if (w_inc > 1 && w_inc < v_slice) {
3352 /* adjust for window's requested size increment */
3353 remain = (win_g.w - w_base) % w_inc;
3359 win_g.x += r_g.w - msize;
3362 colno = split = winno / stacks;
3363 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3364 2 * border_width) / stacks);
3366 hrh = r_g.h / colno;
3367 extra = r_g.h - (colno * hrh);
3368 win_g.h = hrh - 2 * border_width;
3370 /* stack all the tiled windows */
3371 i = j = 0, s = stacks;
3372 TAILQ_FOREACH(win, &ws->winlist, entry) {
3373 if (win->transient != 0 || win->floating != 0)
3375 if (win->iconic != 0)
3378 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3383 if (split && i == split) {
3384 colno = (winno - mwin) / stacks;
3385 if (s <= (winno - mwin) % stacks)
3387 split = split + colno;
3388 hrh = (r_g.h / colno);
3389 extra = r_g.h - (colno * hrh);
3393 win_g.x += win_g.w + 2 * border_width;
3394 win_g.w = (r_g.w - msize -
3395 (stacks * 2 * border_width)) / stacks;
3397 win_g.w += (r_g.w - msize -
3398 (stacks * 2 * border_width)) % stacks;
3402 win_g.h = hrh - 2 * border_width;
3404 h_inc = win->sh.width_inc;
3405 h_base = win->sh.base_width;
3407 h_inc = win->sh.height_inc;
3408 h_base = win->sh.base_height;
3410 if (j == colno - 1) {
3411 win_g.h = hrh + extra;
3412 } else if (h_inc > 1 && h_inc < h_slice) {
3413 /* adjust for window's requested size increment */
3414 remain = (win_g.h - h_base) % h_inc;
3415 missing = h_inc - remain;
3417 if (missing <= extra || j == 0) {
3429 win_g.y += last_h + 2 * border_width;
3431 if (disable_border && bar_enabled == 0 && winno == 1){
3433 win_g.w += 2 * border_width;
3434 win_g.h += 2 * border_width;
3439 if (X(win) != win_g.y || Y(win) != win_g.x ||
3440 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3444 WIDTH(win) = win_g.h;
3445 HEIGHT(win) = win_g.w;
3448 if (X(win) != win_g.x || Y(win) != win_g.y ||
3449 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3453 WIDTH(win) = win_g.w;
3454 HEIGHT(win) = win_g.h;
3458 if (bordered != win->bordered) {
3460 win->bordered = bordered;
3468 war = xcb_get_window_attributes_reply(conn,
3469 xcb_get_window_attributes(conn, win->id),
3472 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3473 map_window_raised(win->id);
3483 /* now, stack all the floaters and transients */
3484 TAILQ_FOREACH(win, &ws->winlist, entry) {
3485 if (win->transient == 0 && win->floating == 0)
3487 if (win->iconic == 1)
3489 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3494 stack_floater(win, ws->r);
3495 map_window_raised(win->id);
3499 stack_floater(fs_win, ws->r);
3500 map_window_raised(fs_win->id);
3505 vertical_config(struct workspace *ws, int id)
3507 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3511 case SWM_ARG_ID_STACKRESET:
3512 case SWM_ARG_ID_STACKINIT:
3513 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3514 ws->l_state.vertical_mwin = 1;
3515 ws->l_state.vertical_stacks = 1;
3517 case SWM_ARG_ID_MASTERSHRINK:
3518 if (ws->l_state.vertical_msize > 1)
3519 ws->l_state.vertical_msize--;
3521 case SWM_ARG_ID_MASTERGROW:
3522 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3523 ws->l_state.vertical_msize++;
3525 case SWM_ARG_ID_MASTERADD:
3526 ws->l_state.vertical_mwin++;
3528 case SWM_ARG_ID_MASTERDEL:
3529 if (ws->l_state.vertical_mwin > 0)
3530 ws->l_state.vertical_mwin--;
3532 case SWM_ARG_ID_STACKINC:
3533 ws->l_state.vertical_stacks++;
3535 case SWM_ARG_ID_STACKDEC:
3536 if (ws->l_state.vertical_stacks > 1)
3537 ws->l_state.vertical_stacks--;
3539 case SWM_ARG_ID_FLIPLAYOUT:
3540 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3548 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3550 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3552 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3556 horizontal_config(struct workspace *ws, int id)
3558 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3561 case SWM_ARG_ID_STACKRESET:
3562 case SWM_ARG_ID_STACKINIT:
3563 ws->l_state.horizontal_mwin = 1;
3564 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3565 ws->l_state.horizontal_stacks = 1;
3567 case SWM_ARG_ID_MASTERSHRINK:
3568 if (ws->l_state.horizontal_msize > 1)
3569 ws->l_state.horizontal_msize--;
3571 case SWM_ARG_ID_MASTERGROW:
3572 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3573 ws->l_state.horizontal_msize++;
3575 case SWM_ARG_ID_MASTERADD:
3576 ws->l_state.horizontal_mwin++;
3578 case SWM_ARG_ID_MASTERDEL:
3579 if (ws->l_state.horizontal_mwin > 0)
3580 ws->l_state.horizontal_mwin--;
3582 case SWM_ARG_ID_STACKINC:
3583 ws->l_state.horizontal_stacks++;
3585 case SWM_ARG_ID_STACKDEC:
3586 if (ws->l_state.horizontal_stacks > 1)
3587 ws->l_state.horizontal_stacks--;
3589 case SWM_ARG_ID_FLIPLAYOUT:
3590 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3598 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3600 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3602 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3605 /* fullscreen view */
3607 max_stack(struct workspace *ws, struct swm_geometry *g)
3609 struct swm_geometry gg = *g;
3610 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3611 int winno, num_screens;
3613 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3618 winno = count_win(ws, 0);
3619 if (winno == 0 && count_win(ws, 1) == 0)
3622 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3623 TAILQ_FOREACH(win, &ws->winlist, entry) {
3624 if (win->transient) {
3626 parent = find_window(win->transient);
3630 if (win->floating && win->floatmaxed == 0 ) {
3632 * retain geometry for retrieval on exit from
3635 store_float_geom(win, ws->r);
3636 win->floatmaxed = 1;
3639 /* only reconfigure if necessary */
3640 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3641 HEIGHT(win) != gg.h) {
3647 WIDTH(win) += 2 * border_width;
3648 HEIGHT(win) += 2 * border_width;
3653 /* unmap only if we don't have multi screen */
3654 if (win != ws->focus)
3655 if (!(num_screens > 1 || outputs > 1))
3659 /* put the last transient on top */
3662 map_window_raised(parent->id);
3663 stack_floater(wintrans, ws->r);
3664 focus_magic(wintrans);
3669 send_to_ws(struct swm_region *r, union arg *args)
3671 int wsid = args->id;
3672 struct ws_win *win = NULL, *parent;
3673 struct workspace *ws, *nws;
3674 char ws_idx_str[SWM_PROPLEN];
3677 if (wsid >= workspace_limit)
3680 if (r && r->ws && r->ws->focus)
3686 if (win->ws->idx == wsid)
3689 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3692 nws = &win->s->ws[wsid];
3694 a.id = SWM_ARG_ID_FOCUSPREV;
3696 if (win->transient) {
3697 parent = find_window(win->transient);
3699 unmap_window(parent);
3700 TAILQ_REMOVE(&ws->winlist, parent, entry);
3701 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3706 TAILQ_REMOVE(&ws->winlist, win, entry);
3707 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3708 if (TAILQ_EMPTY(&ws->winlist))
3709 r->ws->focus = NULL;
3712 /* Try to update the window's workspace property */
3713 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3714 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3716 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3717 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3728 pressbutton(struct swm_region *r, union arg *args)
3730 /* suppress unused warning since var is needed */
3733 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3734 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3735 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3736 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3740 raise_toggle(struct swm_region *r, union arg *args)
3742 /* suppress unused warning since var is needed */
3745 if (r == NULL || r->ws == NULL)
3748 r->ws->always_raise = !r->ws->always_raise;
3750 /* bring floaters back to top */
3751 if (r->ws->always_raise == 0)
3758 iconify(struct swm_region *r, union arg *args)
3762 /* suppress unused warning since var is needed */
3765 if (r->ws->focus == NULL)
3767 unmap_window(r->ws->focus);
3768 update_iconic(r->ws->focus, 1);
3771 r->ws->focus = NULL;
3772 a.id = SWM_ARG_ID_FOCUSCUR;
3779 get_win_name(xcb_window_t win)
3782 xcb_get_property_cookie_t c;
3783 xcb_icccm_get_text_property_reply_t r;
3785 c = xcb_icccm_get_wm_name(conn, win);
3786 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3787 if (r.name_len > 0) {
3788 name = malloc(r.name_len + 1);
3790 memcpy(name, r.name, r.name_len);
3791 name[r.name_len] = '\0';
3794 xcb_icccm_get_text_property_reply_wipe(&r);
3801 uniconify(struct swm_region *r, union arg *args)
3808 DNPRINTF(SWM_D_MISC, "uniconify\n");
3810 if (r == NULL || r->ws == NULL)
3813 /* make sure we have anything to uniconify */
3814 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3815 if (win->ws == NULL)
3816 continue; /* should never happen */
3817 if (win->iconic == 0)
3825 search_resp_action = SWM_SEARCH_UNICONIFY;
3827 spawn_select(r, args, "search", &searchpid);
3829 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3832 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3833 if (win->ws == NULL)
3834 continue; /* should never happen */
3835 if (win->iconic == 0)
3838 name = get_win_name(win->id);
3841 fprintf(lfile, "%s.%u\n", name, win->id);
3851 name_workspace(struct swm_region *r, union arg *args)
3855 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3861 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3863 spawn_select(r, args, "name_workspace", &searchpid);
3865 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3868 fprintf(lfile, "%s", "");
3873 search_workspace(struct swm_region *r, union arg *args)
3876 struct workspace *ws;
3879 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3885 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3887 spawn_select(r, args, "search", &searchpid);
3889 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3892 for (i = 0; i < workspace_limit; i++) {
3896 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3897 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3904 search_win_cleanup(void)
3906 struct search_window *sw = NULL;
3908 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3909 xcb_destroy_window(conn, sw->indicator);
3910 xcb_free_gc(conn, sw->gc);
3911 TAILQ_REMOVE(&search_wl, sw, entry);
3917 search_win(struct swm_region *r, union arg *args)
3919 struct ws_win *win = NULL;
3920 struct search_window *sw = NULL;
3922 uint32_t gcv[3], wa[2];
3930 DNPRINTF(SWM_D_MISC, "search_win\n");
3933 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3935 spawn_select(r, args, "search", &searchpid);
3937 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3940 TAILQ_INIT(&search_wl);
3943 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3944 if (win->iconic == 1)
3947 sw = calloc(1, sizeof(struct search_window));
3949 warn("search_win: calloc");
3951 search_win_cleanup();
3957 snprintf(s, sizeof s, "%d", i);
3960 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
3962 w = xcb_generate_id(conn);
3963 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
3964 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
3965 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3966 info.width + 4, bar_font->height + 4,
3967 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3968 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3971 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3973 sw->gc = xcb_generate_id(conn);
3974 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
3975 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
3977 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3978 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3979 map_window_raised(w);
3981 draw = XftDrawCreate(display, w,
3982 DefaultVisual(display, r->s->idx),
3983 DefaultColormap(display, r->s->idx));
3985 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
3986 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
3989 XftDrawDestroy(draw);
3991 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3993 fprintf(lfile, "%d\n", i);
4003 search_resp_uniconify(char *resp, unsigned long len)
4009 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4011 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4012 if (win->iconic == 0)
4014 name = get_win_name(win->id);
4017 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4022 if (strncmp(s, resp, len) == 0) {
4023 /* XXX this should be a callback to generalize */
4024 update_iconic(win, 0);
4033 search_resp_name_workspace(char *resp, unsigned long len)
4035 struct workspace *ws;
4037 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4039 if (search_r->ws == NULL)
4044 free(search_r->ws->name);
4045 search_r->ws->name = NULL;
4049 ws->name = strdup(resp);
4050 if (ws->name == NULL) {
4051 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4052 "strdup: %s", strerror(errno));
4059 search_resp_search_workspace(char *resp)
4066 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4070 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4077 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4079 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4086 switchws(search_r, &a);
4090 search_resp_search_window(char *resp)
4095 struct search_window *sw;
4097 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4101 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4106 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4108 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4115 TAILQ_FOREACH(sw, &search_wl, entry)
4116 if (idx == sw->idx) {
4122 #define MAX_RESP_LEN 1024
4125 search_do_resp(void)
4131 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4136 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4137 warn("search: calloc");
4141 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4143 warn("search: read error");
4146 resp[rbytes] = '\0';
4149 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4150 * newline, so work around that by sanitizing the resp now.
4152 resp[strcspn(resp, "\n")] = '\0';
4155 switch (search_resp_action) {
4156 case SWM_SEARCH_UNICONIFY:
4157 search_resp_uniconify(resp, len);
4159 case SWM_SEARCH_NAME_WORKSPACE:
4160 search_resp_name_workspace(resp, len);
4162 case SWM_SEARCH_SEARCH_WORKSPACE:
4163 search_resp_search_workspace(resp);
4165 case SWM_SEARCH_SEARCH_WINDOW:
4166 search_resp_search_window(resp);
4171 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4172 search_win_cleanup();
4174 search_resp_action = SWM_SEARCH_NONE;
4175 close(select_resp_pipe[0]);
4180 wkill(struct swm_region *r, union arg *args)
4182 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4184 if (r->ws->focus == NULL)
4187 if (args->id == SWM_ARG_ID_KILLWINDOW)
4188 xcb_kill_client(conn, r->ws->focus->id);
4190 if (r->ws->focus->can_delete)
4191 client_msg(r->ws->focus, a_delete);
4198 floating_toggle_win(struct ws_win *win)
4200 struct swm_region *r;
4210 /* reject floating toggles in max stack mode */
4211 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4214 if (win->floating) {
4215 if (!win->floatmaxed) {
4216 /* retain position for refloat */
4217 store_float_geom(win, r);
4221 if (win->g_floatvalid) {
4222 /* refloat at last floating relative position */
4223 X(win) = win->g_float.x + X(r);
4224 Y(win) = win->g_float.y + Y(r);
4225 WIDTH(win) = win->g_float.w;
4226 HEIGHT(win) = win->g_float.h;
4231 ewmh_update_actions(win);
4237 floating_toggle(struct swm_region *r, union arg *args)
4239 struct ws_win *win = r->ws->focus;
4242 /* suppress unused warning since var is needed */
4248 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4249 _NET_WM_STATE_TOGGLE);
4253 if (win == win->ws->focus) {
4254 a.id = SWM_ARG_ID_FOCUSCUR;
4255 focus(win->ws->r, &a);
4259 event_drain(XCB_ENTER_NOTIFY);
4263 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4265 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4267 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4269 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4272 if (X(win) + BORDER(win) < X(r)) {
4274 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4276 X(win) = X(r) - BORDER(win);
4279 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4281 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4283 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4286 if (Y(win) + BORDER(win) < Y(r)) {
4288 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4290 Y(win) = Y(r) - BORDER(win);
4296 if (HEIGHT(win) < 1)
4302 update_window(struct ws_win *win)
4307 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4308 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4309 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4313 wc[3] = HEIGHT(win);
4314 wc[4] = BORDER(win);
4316 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4317 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4318 wc[3], YESNO(win->bordered));
4320 xcb_configure_window(conn, win->id, mask, wc);
4323 #define SWM_RESIZE_STEPS (50)
4326 resize(struct ws_win *win, union arg *args)
4328 xcb_timestamp_t timestamp = 0;
4329 struct swm_region *r = NULL;
4330 int resize_step = 0;
4331 struct swm_geometry g;
4332 int top = 0, left = 0, resizing;
4334 unsigned int shape; /* cursor style */
4335 xcb_cursor_t cursor;
4336 xcb_font_t cursor_font;
4337 xcb_query_pointer_reply_t *xpr;
4338 xcb_generic_event_t *evt;
4339 xcb_motion_notify_event_t *mne;
4345 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4348 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4349 "transient: 0x%x\n", win->id, YESNO(win->floating),
4352 if (!(win->transient != 0 || win->floating != 0))
4355 /* reject resizes in max mode for floaters (transient ok) */
4356 if (win->floatmaxed)
4360 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4366 case SWM_ARG_ID_WIDTHSHRINK:
4367 WIDTH(win) -= SWM_RESIZE_STEPS;
4370 case SWM_ARG_ID_WIDTHGROW:
4371 WIDTH(win) += SWM_RESIZE_STEPS;
4374 case SWM_ARG_ID_HEIGHTSHRINK:
4375 HEIGHT(win) -= SWM_RESIZE_STEPS;
4378 case SWM_ARG_ID_HEIGHTGROW:
4379 HEIGHT(win) += SWM_RESIZE_STEPS;
4386 constrain_window(win, r, 1);
4388 store_float_geom(win,r);
4392 /* get cursor offset from window root */
4393 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4400 if (xpr->win_x < WIDTH(win) / 2)
4403 if (xpr->win_y < HEIGHT(win) / 2)
4406 if (args->id == SWM_ARG_ID_CENTER)
4409 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4411 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4413 cursor_font = xcb_generate_id(conn);
4414 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4416 cursor = xcb_generate_id(conn);
4417 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4418 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4420 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4421 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4426 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4427 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4428 case XCB_BUTTON_RELEASE:
4429 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4432 case XCB_MOTION_NOTIFY:
4433 mne = (xcb_motion_notify_event_t *)evt;
4434 /* cursor offset/delta from start of the operation */
4435 dx = mne->root_x - xpr->root_x;
4436 dy = mne->root_y - xpr->root_y;
4442 if (args->id == SWM_ARG_ID_CENTER) {
4443 if (g.h / 2 + dy < 1)
4447 HEIGHT(win) = g.h + 2 * dy;
4455 HEIGHT(win) = g.h + dy;
4462 if (args->id == SWM_ARG_ID_CENTER) {
4463 if (g.w / 2 + dx < 1)
4467 WIDTH(win) = g.w + 2 * dx;
4475 WIDTH(win) = g.w + dx;
4478 constrain_window(win, r, 1);
4480 /* not free, don't sync more than 120 times / second */
4481 if ((mne->time - timestamp) > (1000 / 120) ) {
4482 timestamp = mne->time;
4497 store_float_geom(win,r);
4499 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4500 xcb_free_cursor(conn, cursor);
4501 xcb_close_font(conn, cursor_font);
4503 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4507 resize_step(struct swm_region *r, union arg *args)
4509 struct ws_win *win = NULL;
4511 if (r && r->ws && r->ws->focus)
4519 #define SWM_MOVE_STEPS (50)
4522 move(struct ws_win *win, union arg *args)
4524 xcb_timestamp_t timestamp = 0;
4525 int move_step = 0, moving;
4526 struct swm_region *r = NULL;
4527 xcb_font_t cursor_font;
4528 xcb_cursor_t cursor;
4529 xcb_query_pointer_reply_t *qpr;
4530 xcb_generic_event_t *evt;
4531 xcb_motion_notify_event_t *mne;
4537 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4540 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4541 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4543 /* in max_stack mode should only move transients */
4544 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4548 if (win->floating == 0 && !win->transient) {
4549 store_float_geom(win, r);
4550 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4553 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4560 case SWM_ARG_ID_MOVELEFT:
4561 X(win) -= (SWM_MOVE_STEPS - border_width);
4564 case SWM_ARG_ID_MOVERIGHT:
4565 X(win) += (SWM_MOVE_STEPS - border_width);
4568 case SWM_ARG_ID_MOVEUP:
4569 Y(win) -= (SWM_MOVE_STEPS - border_width);
4572 case SWM_ARG_ID_MOVEDOWN:
4573 Y(win) += (SWM_MOVE_STEPS - border_width);
4580 constrain_window(win, r, 0);
4582 store_float_geom(win, r);
4586 cursor_font = xcb_generate_id(conn);
4587 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4589 cursor = xcb_generate_id(conn);
4590 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4591 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4593 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4594 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4595 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4597 /* get cursor offset from window root */
4598 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4601 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4602 xcb_free_cursor(conn, cursor);
4603 xcb_close_font(conn, cursor_font);
4609 while ((evt = xcb_wait_for_event(conn)) && moving) {
4610 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4611 case XCB_BUTTON_RELEASE:
4612 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4615 case XCB_MOTION_NOTIFY:
4616 mne = (xcb_motion_notify_event_t *)evt;
4617 X(win) = mne->root_x - qpr->win_x - border_width;
4618 Y(win) = mne->root_y - qpr->win_y - border_width;
4620 constrain_window(win, r, 0);
4622 /* not free, don't sync more than 120 times / second */
4623 if ((mne->time - timestamp) > (1000 / 120) ) {
4624 timestamp = mne->time;
4639 store_float_geom(win, r);
4641 xcb_free_cursor(conn, cursor);
4642 xcb_close_font(conn, cursor_font);
4643 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4644 DNPRINTF(SWM_D_EVENT, "move: done\n");
4648 move_step(struct swm_region *r, union arg *args)
4650 struct ws_win *win = NULL;
4652 if (r && r->ws && r->ws->focus)
4657 if (!(win->transient != 0 || win->floating != 0))
4664 /* user/key callable function IDs */
4714 KF_SEARCH_WORKSPACE,
4755 KF_DUMPWINS, /* MUST BE LAST */
4759 /* key definitions */
4761 char name[SWM_FUNCNAME_LEN];
4762 void (*func)(struct swm_region *r, union arg *);
4764 } keyfuncs[KF_INVALID + 1] = {
4765 /* name function argument */
4766 { "bar_toggle", bar_toggle, {0} },
4767 { "button2", pressbutton, {2} },
4768 { "cycle_layout", cycle_layout, {0} },
4769 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4770 { "float_toggle", floating_toggle,{0} },
4771 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4772 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4773 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4774 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4775 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4776 { "iconify", iconify, {0} },
4777 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4778 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4779 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4780 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4781 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4782 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4783 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4784 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4785 { "mvws_1", send_to_ws, {.id = 0} },
4786 { "mvws_2", send_to_ws, {.id = 1} },
4787 { "mvws_3", send_to_ws, {.id = 2} },
4788 { "mvws_4", send_to_ws, {.id = 3} },
4789 { "mvws_5", send_to_ws, {.id = 4} },
4790 { "mvws_6", send_to_ws, {.id = 5} },
4791 { "mvws_7", send_to_ws, {.id = 6} },
4792 { "mvws_8", send_to_ws, {.id = 7} },
4793 { "mvws_9", send_to_ws, {.id = 8} },
4794 { "mvws_10", send_to_ws, {.id = 9} },
4795 { "mvws_11", send_to_ws, {.id = 10} },
4796 { "mvws_12", send_to_ws, {.id = 11} },
4797 { "mvws_13", send_to_ws, {.id = 12} },
4798 { "mvws_14", send_to_ws, {.id = 13} },
4799 { "mvws_15", send_to_ws, {.id = 14} },
4800 { "mvws_16", send_to_ws, {.id = 15} },
4801 { "mvws_17", send_to_ws, {.id = 16} },
4802 { "mvws_18", send_to_ws, {.id = 17} },
4803 { "mvws_19", send_to_ws, {.id = 18} },
4804 { "mvws_20", send_to_ws, {.id = 19} },
4805 { "mvws_21", send_to_ws, {.id = 20} },
4806 { "mvws_22", send_to_ws, {.id = 21} },
4807 { "name_workspace", name_workspace, {0} },
4808 { "quit", quit, {0} },
4809 { "raise_toggle", raise_toggle, {0} },
4810 { "restart", restart, {0} },
4811 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4812 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4813 { "search_win", search_win, {0} },
4814 { "search_workspace", search_workspace, {0} },
4815 { "spawn_custom", NULL, {0} },
4816 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4817 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4818 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4819 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4820 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4821 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4822 { "uniconify", uniconify, {0} },
4823 { "version", version, {0} },
4824 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4825 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4826 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4827 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4828 { "ws_1", switchws, {.id = 0} },
4829 { "ws_2", switchws, {.id = 1} },
4830 { "ws_3", switchws, {.id = 2} },
4831 { "ws_4", switchws, {.id = 3} },
4832 { "ws_5", switchws, {.id = 4} },
4833 { "ws_6", switchws, {.id = 5} },
4834 { "ws_7", switchws, {.id = 6} },
4835 { "ws_8", switchws, {.id = 7} },
4836 { "ws_9", switchws, {.id = 8} },
4837 { "ws_10", switchws, {.id = 9} },
4838 { "ws_11", switchws, {.id = 10} },
4839 { "ws_12", switchws, {.id = 11} },
4840 { "ws_13", switchws, {.id = 12} },
4841 { "ws_14", switchws, {.id = 13} },
4842 { "ws_15", switchws, {.id = 14} },
4843 { "ws_16", switchws, {.id = 15} },
4844 { "ws_17", switchws, {.id = 16} },
4845 { "ws_18", switchws, {.id = 17} },
4846 { "ws_19", switchws, {.id = 18} },
4847 { "ws_20", switchws, {.id = 19} },
4848 { "ws_21", switchws, {.id = 20} },
4849 { "ws_22", switchws, {.id = 21} },
4850 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4851 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4852 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4853 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4854 { "ws_prior", priorws, {0} },
4855 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4856 { "invalid key func", NULL, {0} },
4859 RB_ENTRY(key) entry;
4862 enum keyfuncid funcid;
4865 RB_HEAD(key_tree, key);
4868 key_cmp(struct key *kp1, struct key *kp2)
4870 if (kp1->keysym < kp2->keysym)
4872 if (kp1->keysym > kp2->keysym)
4875 if (kp1->mod < kp2->mod)
4877 if (kp1->mod > kp2->mod)
4883 RB_GENERATE(key_tree, key, entry, key_cmp);
4884 struct key_tree keys;
4887 enum { client_click, root_click };
4889 unsigned int action;
4891 unsigned int button;
4892 void (*func)(struct ws_win *, union arg *);
4895 /* action key mouse button func args */
4896 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4897 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4898 { client_click, MODKEY, Button1, move, {0} },
4902 update_modkey(unsigned int mod)
4908 RB_FOREACH(kp, key_tree, &keys)
4909 if (kp->mod & ShiftMask)
4910 kp->mod = mod | ShiftMask;
4914 for (i = 0; i < LENGTH(buttons); i++)
4915 if (buttons[i].mask & ShiftMask)
4916 buttons[i].mask = mod | ShiftMask;
4918 buttons[i].mask = mod;
4923 TAILQ_ENTRY(spawn_prog) entry;
4928 TAILQ_HEAD(spawn_list, spawn_prog);
4929 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4932 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4935 struct spawn_prog *prog = NULL;
4937 char *ap, **real_args;
4939 /* suppress unused warning since var is needed */
4942 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4945 TAILQ_FOREACH(prog, &spawns, entry) {
4946 if (!strcasecmp(spawn_name, prog->name))
4950 warnx("spawn_custom: program %s not found", spawn_name);
4954 /* make room for expanded args */
4955 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4956 err(1, "spawn_custom: calloc real_args");
4958 /* expand spawn_args into real_args */
4959 for (i = 0; i < prog->argc; i++) {
4961 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4962 if (!strcasecmp(ap, "$bar_border")) {
4964 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4966 err(1, "spawn_custom border color");
4967 } else if (!strcasecmp(ap, "$bar_color")) {
4969 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4971 err(1, "spawn_custom bar color");
4972 } else if (!strcasecmp(ap, "$bar_font")) {
4973 if ((real_args[i] = strdup(bar_fonts))
4975 err(1, "spawn_custom bar fonts");
4976 } else if (!strcasecmp(ap, "$bar_font_color")) {
4978 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4980 err(1, "spawn_custom color font");
4981 } else if (!strcasecmp(ap, "$color_focus")) {
4983 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4985 err(1, "spawn_custom color focus");
4986 } else if (!strcasecmp(ap, "$color_unfocus")) {
4988 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4990 err(1, "spawn_custom color unfocus");
4992 /* no match --> copy as is */
4993 if ((real_args[i] = strdup(ap)) == NULL)
4994 err(1, "spawn_custom strdup(ap)");
4996 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5001 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5002 for (i = 0; i < prog->argc; i++)
5003 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5004 DNPRINTF(SWM_D_SPAWN, "\n");
5006 *ret_args = real_args;
5007 return (prog->argc);
5011 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5017 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5021 spawn(r->ws->idx, &a, 1);
5023 for (i = 0; i < spawn_argc; i++)
5029 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5035 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5039 if (pipe(select_list_pipe) == -1)
5040 err(1, "pipe error");
5041 if (pipe(select_resp_pipe) == -1)
5042 err(1, "pipe error");
5044 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5045 err(1, "could not disable SIGPIPE");
5046 switch (*pid = fork()) {
5048 err(1, "cannot fork");
5051 if (dup2(select_list_pipe[0], 0) == -1)
5053 if (dup2(select_resp_pipe[1], 1) == -1)
5055 close(select_list_pipe[1]);
5056 close(select_resp_pipe[0]);
5057 spawn(r->ws->idx, &a, 0);
5059 default: /* parent */
5060 close(select_list_pipe[0]);
5061 close(select_resp_pipe[1]);
5065 for (i = 0; i < spawn_argc; i++)
5071 spawn_insert(char *name, char *args)
5073 char *arg, *cp, *ptr;
5074 struct spawn_prog *sp;
5076 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5078 if ((sp = calloc(1, sizeof *sp)) == NULL)
5079 err(1, "spawn_insert: calloc");
5080 if ((sp->name = strdup(name)) == NULL)
5081 err(1, "spawn_insert: strdup");
5083 /* convert the arguments to an argument list */
5084 if ((ptr = cp = strdup(args)) == NULL)
5085 err(1, "spawn_insert: strdup");
5086 while ((arg = strsep(&ptr, " \t")) != NULL) {
5087 /* empty field; skip it */
5092 if ((sp->argv = realloc(sp->argv, sp->argc *
5093 sizeof *sp->argv)) == NULL)
5094 err(1, "spawn_insert: realloc");
5095 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5096 err(1, "spawn_insert: strdup");
5100 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5101 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5105 spawn_remove(struct spawn_prog *sp)
5109 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5111 TAILQ_REMOVE(&spawns, sp, entry);
5112 for (i = 0; i < sp->argc; i++)
5118 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5122 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5124 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5127 spawn_insert(name, args);
5129 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5133 setspawn(char *name, char *args)
5135 struct spawn_prog *sp;
5137 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5142 TAILQ_FOREACH(sp, &spawns, entry) {
5143 if (!strcmp(sp->name, name)) {
5147 spawn_replace(sp, name, args);
5148 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5152 if (*args == '\0') {
5153 warnx("error: setspawn: cannot find program: %s", name);
5157 spawn_insert(name, args);
5158 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5162 setconfspawn(char *selector, char *value, int flags)
5166 /* suppress unused warning since var is needed */
5169 args = expand_tilde(value);
5171 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5173 setspawn(selector, args);
5176 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5183 setconfspawn("term", "xterm", 0);
5184 setconfspawn("spawn_term", "xterm", 0);
5185 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5186 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5187 setconfspawn("lock", "xlock", 0);
5188 setconfspawn("initscr", "initscreen.sh", 0);
5189 setconfspawn("menu", "dmenu_run"
5192 " -nf $bar_font_color"
5194 " -sf $bar_color", 0);
5195 setconfspawn("search", "dmenu"
5199 " -nf $bar_font_color"
5201 " -sf $bar_color", 0);
5202 setconfspawn("name_workspace", "dmenu"
5206 " -nf $bar_font_color"
5208 " -sf $bar_color", 0);
5212 #define SWM_MODNAME_SIZE 32
5213 #define SWM_KEY_WS "\n+ \t"
5215 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5219 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5220 if (mod == NULL || ks == NULL) {
5221 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5224 if (keystr == NULL || strlen(keystr) == 0) {
5225 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5229 *ks = XCB_NO_SYMBOL;
5231 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5232 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5234 cp += (long)strspn(cp, SWM_KEY_WS);
5235 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5237 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5239 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5241 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5243 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5245 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5247 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5248 *mod |= ControlMask;
5250 *ks = XStringToKeysym(name);
5251 XConvertCase(*ks, ks, &uks);
5252 if (ks == XCB_NO_SYMBOL) {
5254 "parsekeys: invalid key %s\n",
5260 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5265 strdupsafe(char *str)
5270 return (strdup(str));
5274 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5278 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5279 keyfuncs[kfid].name, spawn_name);
5281 if ((kp = malloc(sizeof *kp)) == NULL)
5282 err(1, "key_insert: malloc");
5287 kp->spawn_name = strdupsafe(spawn_name);
5288 RB_INSERT(key_tree, &keys, kp);
5290 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5294 key_lookup(unsigned int mod, KeySym ks)
5301 return (RB_FIND(key_tree, &keys, &kp));
5305 key_remove(struct key *kp)
5307 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5309 RB_REMOVE(key_tree, &keys, kp);
5310 free(kp->spawn_name);
5313 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5317 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5320 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5324 key_insert(mod, ks, kfid, spawn_name);
5326 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5330 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5335 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5336 keyfuncs[kfid].name, spawn_name);
5338 if ((kp = key_lookup(mod, ks)) != NULL) {
5339 if (kfid == KF_INVALID)
5342 key_replace(kp, mod, ks, kfid, spawn_name);
5343 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5346 if (kfid == KF_INVALID) {
5347 warnx("error: setkeybinding: cannot find mod/key combination");
5348 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5352 key_insert(mod, ks, kfid, spawn_name);
5353 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5357 setconfbinding(char *selector, char *value, int flags)
5359 enum keyfuncid kfid;
5362 struct spawn_prog *sp;
5364 /* suppress unused warning since var is needed */
5367 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5368 if (selector == NULL) {
5369 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5370 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5372 setkeybinding(mod, ks, kfid, NULL);
5377 /* search by key function name */
5378 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5379 if (strncasecmp(selector, keyfuncs[kfid].name,
5380 SWM_FUNCNAME_LEN) == 0) {
5381 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5383 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5384 setkeybinding(mod, ks, kfid, NULL);
5390 /* search by custom spawn name */
5391 TAILQ_FOREACH(sp, &spawns, entry) {
5392 if (strcasecmp(selector, sp->name) == 0) {
5393 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5395 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5396 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5403 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5410 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5411 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5412 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5413 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5414 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5415 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5416 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5417 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5418 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5419 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5420 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5421 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5422 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5423 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5424 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5425 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5426 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5427 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5428 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5429 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5430 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5431 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5432 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5433 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5434 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5435 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5436 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5437 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5438 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5439 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5440 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5441 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5442 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5443 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5444 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5445 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5446 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5447 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5448 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5449 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5450 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5451 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5452 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5453 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5454 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5455 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5456 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5457 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5458 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5459 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5460 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5461 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5462 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5463 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5464 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5465 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5466 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5467 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5468 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5469 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5470 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5471 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5472 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5473 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5474 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5475 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5476 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5477 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5478 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5479 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5480 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5481 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5482 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5483 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5484 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5485 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5486 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5487 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5488 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5489 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5490 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5491 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5492 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5493 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5494 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5495 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5496 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5497 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5498 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5499 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5500 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5501 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5502 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5503 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5504 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5505 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5507 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5516 while (RB_EMPTY(&keys) == 0) {
5517 kp = RB_ROOT(&keys);
5523 setkeymapping(char *selector, char *value, int flags)
5525 char keymapping_file[PATH_MAX];
5527 /* suppress unused warnings since vars are needed */
5531 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5532 if (value[0] == '~')
5533 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5534 pwd->pw_dir, &value[1]);
5536 strlcpy(keymapping_file, value, sizeof keymapping_file);
5538 /* load new key bindings; if it fails, revert to default bindings */
5539 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5543 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5548 updatenumlockmask(void)
5551 xcb_get_modifier_mapping_reply_t *modmap_r;
5552 xcb_keycode_t *modmap, kc, *keycode;
5554 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5557 modmap_r = xcb_get_modifier_mapping_reply(conn,
5558 xcb_get_modifier_mapping(conn),
5561 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5562 for (i = 0; i < 8; i++) {
5563 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5564 kc = modmap[i * modmap_r->keycodes_per_modifier
5566 keycode = xcb_key_symbols_get_keycode(syms,
5569 numlockmask = (1 << i);
5582 xcb_keycode_t *code;
5583 unsigned int modifiers[] =
5584 { 0, LockMask, numlockmask, numlockmask | LockMask };
5587 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5588 updatenumlockmask();
5590 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5591 for (k = 0; k < num_screens; k++) {
5592 if (TAILQ_EMPTY(&screens[k].rl))
5594 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5596 RB_FOREACH(kp, key_tree, &keys) {
5597 if ((code = xcb_key_symbols_get_keycode(syms,
5599 for (j = 0; j < LENGTH(modifiers); j++)
5600 xcb_grab_key(conn, 1,
5602 kp->mod | modifiers[j],
5603 *code, XCB_GRAB_MODE_ASYNC,
5604 XCB_GRAB_MODE_ASYNC);
5611 grabbuttons(struct ws_win *win, int focused)
5614 unsigned int modifiers[] =
5615 { 0, LockMask, numlockmask, numlockmask|LockMask };
5617 updatenumlockmask();
5618 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5619 XCB_BUTTON_MASK_ANY);
5621 for (i = 0; i < LENGTH(buttons); i++)
5622 if (buttons[i].action == client_click)
5623 for (j = 0; j < LENGTH(modifiers); j++)
5624 xcb_grab_button(conn, 0, win->id,
5626 XCB_GRAB_MODE_ASYNC,
5633 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5634 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5635 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5638 const char *quirkname[] = {
5639 "NONE", /* config string for "no value" */
5648 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5649 #define SWM_Q_WS "\n|+ \t"
5651 parsequirks(char *qstr, unsigned long *quirk)
5661 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5663 cp += (long)strspn(cp, SWM_Q_WS);
5664 for (i = 0; i < LENGTH(quirkname); i++) {
5665 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5666 DNPRINTF(SWM_D_QUIRK,
5667 "parsequirks: %s\n", name);
5672 *quirk |= 1 << (i-1);
5676 if (i >= LENGTH(quirkname)) {
5677 DNPRINTF(SWM_D_QUIRK,
5678 "parsequirks: invalid quirk [%s]\n", name);
5686 quirk_insert(const char *class, const char *name, unsigned long quirk)
5690 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5693 if ((qp = malloc(sizeof *qp)) == NULL)
5694 err(1, "quirk_insert: malloc");
5695 if ((qp->class = strdup(class)) == NULL)
5696 err(1, "quirk_insert: strdup");
5697 if ((qp->name = strdup(name)) == NULL)
5698 err(1, "quirk_insert: strdup");
5701 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5703 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5707 quirk_remove(struct quirk *qp)
5709 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5710 qp->name, qp->quirk);
5712 TAILQ_REMOVE(&quirks, qp, entry);
5717 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5721 quirk_replace(struct quirk *qp, const char *class, const char *name,
5722 unsigned long quirk)
5724 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5725 qp->name, qp->quirk);
5728 quirk_insert(class, name, quirk);
5730 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5734 setquirk(const char *class, const char *name, unsigned long quirk)
5738 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5741 TAILQ_FOREACH(qp, &quirks, entry) {
5742 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5746 quirk_replace(qp, class, name, quirk);
5747 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5752 warnx("error: setquirk: cannot find class/name combination");
5756 quirk_insert(class, name, quirk);
5757 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5761 setconfquirk(char *selector, char *value, int flags)
5763 char *cp, *class, *name;
5765 unsigned long quirks;
5767 /* suppress unused warning since var is needed */
5770 if (selector == NULL)
5772 if ((cp = strchr(selector, ':')) == NULL)
5777 if ((retval = parsequirks(value, &quirks)) == 0)
5778 setquirk(class, name, quirks);
5785 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5786 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5787 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5788 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5789 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5790 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5791 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5792 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5793 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5794 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5795 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5796 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5797 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5800 /* conf file stuff */
5801 #define SWM_CONF_FILE "spectrwm.conf"
5802 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5806 SWM_S_BAR_AT_BOTTOM,
5807 SWM_S_BAR_BORDER_WIDTH,
5814 SWM_S_CLOCK_ENABLED,
5817 SWM_S_CYCLE_VISIBLE,
5819 SWM_S_DISABLE_BORDER,
5821 SWM_S_FOCUS_CLOSE_WRAP,
5822 SWM_S_FOCUS_DEFAULT,
5828 SWM_S_STACK_ENABLED,
5830 SWM_S_TITLE_CLASS_ENABLED,
5831 SWM_S_TITLE_NAME_ENABLED,
5832 SWM_S_URGENT_ENABLED,
5833 SWM_S_VERBOSE_LAYOUT,
5834 SWM_S_WINDOW_NAME_ENABLED,
5835 SWM_S_WORKSPACE_LIMIT
5839 setconfvalue(char *selector, char *value, int flags)
5844 /* suppress unused warning since var is needed */
5848 case SWM_S_BAR_ACTION:
5850 if ((bar_argv[0] = expand_tilde(value)) == NULL)
5851 err(1, "setconfvalue: bar_action");
5853 case SWM_S_BAR_AT_BOTTOM:
5854 bar_at_bottom = atoi(value);
5856 case SWM_S_BAR_BORDER_WIDTH:
5857 bar_border_width = atoi(value);
5858 if (bar_border_width < 0)
5859 bar_border_width = 0;
5861 case SWM_S_BAR_DELAY:
5862 bar_delay = atoi(value);
5864 case SWM_S_BAR_ENABLED:
5865 bar_enabled = atoi(value);
5867 case SWM_S_BAR_FONT:
5869 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5870 err(1, "setconfvalue: asprintf: failed to allocate "
5871 "memory for bar_fonts.");
5874 case SWM_S_BAR_FORMAT:
5876 if ((bar_format = strdup(value)) == NULL)
5877 err(1, "setconfvalue: bar_format");
5879 case SWM_S_BAR_JUSTIFY:
5880 if (!strcmp(value, "left"))
5881 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5882 else if (!strcmp(value, "center"))
5883 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5884 else if (!strcmp(value, "right"))
5885 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5887 errx(1, "invalid bar_justify");
5889 case SWM_S_BORDER_WIDTH:
5890 border_width = atoi(value);
5891 if (border_width < 0)
5894 case SWM_S_CLOCK_ENABLED:
5895 clock_enabled = atoi(value);
5897 case SWM_S_CLOCK_FORMAT:
5898 #ifndef SWM_DENY_CLOCK_FORMAT
5900 if ((clock_format = strdup(value)) == NULL)
5901 err(1, "setconfvalue: clock_format");
5904 case SWM_S_CYCLE_EMPTY:
5905 cycle_empty = atoi(value);
5907 case SWM_S_CYCLE_VISIBLE:
5908 cycle_visible = atoi(value);
5910 case SWM_S_DIALOG_RATIO:
5911 dialog_ratio = atof(value);
5912 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5915 case SWM_S_DISABLE_BORDER:
5916 disable_border = atoi(value);
5918 case SWM_S_FOCUS_CLOSE:
5919 if (!strcmp(value, "first"))
5920 focus_close = SWM_STACK_BOTTOM;
5921 else if (!strcmp(value, "last"))
5922 focus_close = SWM_STACK_TOP;
5923 else if (!strcmp(value, "next"))
5924 focus_close = SWM_STACK_ABOVE;
5925 else if (!strcmp(value, "previous"))
5926 focus_close = SWM_STACK_BELOW;
5928 errx(1, "focus_close");
5930 case SWM_S_FOCUS_CLOSE_WRAP:
5931 focus_close_wrap = atoi(value);
5933 case SWM_S_FOCUS_DEFAULT:
5934 if (!strcmp(value, "last"))
5935 focus_default = SWM_STACK_TOP;
5936 else if (!strcmp(value, "first"))
5937 focus_default = SWM_STACK_BOTTOM;
5939 errx(1, "focus_default");
5941 case SWM_S_FOCUS_MODE:
5942 if (!strcmp(value, "default"))
5943 focus_mode = SWM_FOCUS_DEFAULT;
5944 else if (!strcmp(value, "follow_cursor"))
5945 focus_mode = SWM_FOCUS_FOLLOW;
5946 else if (!strcmp(value, "synergy"))
5947 focus_mode = SWM_FOCUS_SYNERGY;
5949 errx(1, "focus_mode");
5951 case SWM_S_SPAWN_ORDER:
5952 if (!strcmp(value, "first"))
5953 spawn_position = SWM_STACK_BOTTOM;
5954 else if (!strcmp(value, "last"))
5955 spawn_position = SWM_STACK_TOP;
5956 else if (!strcmp(value, "next"))
5957 spawn_position = SWM_STACK_ABOVE;
5958 else if (!strcmp(value, "previous"))
5959 spawn_position = SWM_STACK_BELOW;
5961 errx(1, "spawn_position");
5963 case SWM_S_SPAWN_TERM:
5964 setconfspawn("term", value, 0);
5965 setconfspawn("spawn_term", value, 0);
5969 case SWM_S_SS_ENABLED:
5970 ss_enabled = atoi(value);
5972 case SWM_S_STACK_ENABLED:
5973 stack_enabled = atoi(value);
5975 case SWM_S_TERM_WIDTH:
5976 term_width = atoi(value);
5980 case SWM_S_TITLE_CLASS_ENABLED:
5981 title_class_enabled = atoi(value);
5983 case SWM_S_TITLE_NAME_ENABLED:
5984 title_name_enabled = atoi(value);
5986 case SWM_S_URGENT_ENABLED:
5987 urgent_enabled = atoi(value);
5989 case SWM_S_VERBOSE_LAYOUT:
5990 verbose_layout = atoi(value);
5991 for (i = 0; layouts[i].l_stack != NULL; i++) {
5993 layouts[i].l_string = fancy_stacker;
5995 layouts[i].l_string = plain_stacker;
5998 case SWM_S_WINDOW_NAME_ENABLED:
5999 window_name_enabled = atoi(value);
6001 case SWM_S_WORKSPACE_LIMIT:
6002 workspace_limit = atoi(value);
6003 if (workspace_limit > SWM_WS_MAX)
6004 workspace_limit = SWM_WS_MAX;
6005 else if (workspace_limit < 1)
6006 workspace_limit = 1;
6015 setconfmodkey(char *selector, char *value, int flags)
6017 /* suppress unused warnings since vars are needed */
6021 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6022 update_modkey(Mod1Mask);
6023 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6024 update_modkey(Mod2Mask);
6025 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6026 update_modkey(Mod3Mask);
6027 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6028 update_modkey(Mod4Mask);
6035 setconfcolor(char *selector, char *value, int flags)
6037 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6042 setconfregion(char *selector, char *value, int flags)
6044 /* suppress unused warnings since vars are needed */
6048 custom_region(value);
6053 setautorun(char *selector, char *value, int flags)
6063 /* suppress unused warnings since vars are needed */
6067 if (getenv("SWM_STARTED"))
6071 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6072 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6074 if (ws_id < 0 || ws_id >= workspace_limit)
6075 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6078 * This is a little intricate
6080 * If the pid already exists we simply reuse it because it means it was
6081 * used before AND not claimed by manage_window. We get away with
6082 * altering it in the parent after INSERT because this can not be a race
6085 while ((ap = strsep(&sp, " \t")) != NULL) {
6088 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6090 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6091 err(1, "setautorun: realloc");
6092 a.argv[argc - 1] = ap;
6095 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6096 err(1, "setautorun: realloc");
6097 a.argv[argc] = NULL;
6099 if ((pid = fork()) == 0) {
6100 spawn(ws_id, &a, 1);
6109 p = calloc(1, sizeof *p);
6112 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6122 setlayout(char *selector, char *value, int flags)
6124 int ws_id, i, x, mg, ma, si, raise, f = 0;
6125 int st = SWM_V_STACK, num_screens;
6127 struct workspace *ws;
6129 /* suppress unused warnings since vars are needed */
6133 if (getenv("SWM_STARTED"))
6137 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6138 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6139 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6140 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6143 if (ws_id < 0 || ws_id >= workspace_limit)
6144 errx(1, "layout: invalid workspace %d", ws_id + 1);
6146 if (!strcasecmp(s, "vertical"))
6148 else if (!strcasecmp(s, "vertical_flip")) {
6151 } else if (!strcasecmp(s, "horizontal"))
6153 else if (!strcasecmp(s, "horizontal_flip")) {
6156 } else if (!strcasecmp(s, "fullscreen"))
6159 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6160 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6163 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6164 for (i = 0; i < num_screens; i++) {
6165 ws = (struct workspace *)&screens[i].ws;
6166 ws[ws_id].cur_layout = &layouts[st];
6168 ws[ws_id].always_raise = raise;
6169 if (st == SWM_MAX_STACK)
6173 for (x = 0; x < abs(mg); x++) {
6174 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6175 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6176 SWM_ARG_ID_MASTERSHRINK);
6180 for (x = 0; x < abs(ma); x++) {
6181 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6182 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6183 SWM_ARG_ID_MASTERDEL);
6187 for (x = 0; x < abs(si); x++) {
6188 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6189 si >= 0 ? SWM_ARG_ID_STACKINC :
6190 SWM_ARG_ID_STACKDEC);
6195 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6196 SWM_ARG_ID_FLIPLAYOUT);
6204 /* config options */
6205 struct config_option {
6207 int (*func)(char*, char*, int);
6210 struct config_option configopt[] = {
6211 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6212 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6213 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6214 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6215 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6216 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6217 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6218 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6219 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6220 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6221 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6222 { "keyboard_mapping", setkeymapping, 0 },
6223 { "bind", setconfbinding, 0 },
6224 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6225 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6226 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6227 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6228 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6229 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6230 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6231 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6232 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6233 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6234 { "modkey", setconfmodkey, 0 },
6235 { "program", setconfspawn, 0 },
6236 { "quirk", setconfquirk, 0 },
6237 { "region", setconfregion, 0 },
6238 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6239 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6240 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6241 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6242 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6243 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6244 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6245 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6246 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6247 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6248 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6249 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6250 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6251 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6252 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6253 { "autorun", setautorun, 0 },
6254 { "layout", setlayout, 0 },
6259 conf_load(char *filename, int keymapping)
6262 char *line, *cp, *optsub, *optval;
6263 size_t linelen, lineno = 0;
6264 int wordlen, i, optind;
6265 struct config_option *opt;
6267 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6269 if (filename == NULL) {
6270 warnx("conf_load: no filename");
6273 if ((config = fopen(filename, "r")) == NULL) {
6274 warn("conf_load: fopen: %s", filename);
6278 while (!feof(config)) {
6279 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6282 err(1, "%s", filename);
6287 cp += strspn(cp, " \t\n"); /* eat whitespace */
6288 if (cp[0] == '\0') {
6293 /* get config option */
6294 wordlen = strcspn(cp, "=[ \t\n");
6296 warnx("%s: line %zd: no option found",
6301 for (i = 0; i < LENGTH(configopt); i++) {
6302 opt = &configopt[i];
6303 if (!strncasecmp(cp, opt->optname, wordlen) &&
6304 (int)strlen(opt->optname) == wordlen) {
6310 warnx("%s: line %zd: unknown option %.*s",
6311 filename, lineno, wordlen, cp);
6314 if (keymapping && strcmp(opt->optname, "bind")) {
6315 warnx("%s: line %zd: invalid option %.*s",
6316 filename, lineno, wordlen, cp);
6320 cp += strspn(cp, " \t\n"); /* eat whitespace */
6321 /* get [selector] if any */
6325 wordlen = strcspn(cp, "]");
6328 warnx("%s: line %zd: syntax error",
6333 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6335 warnx("%s: line %zd: unable to allocate"
6336 "memory for selector", filename,
6342 cp += strspn(cp, "] \t\n"); /* eat trailing */
6344 cp += strspn(cp, "= \t\n"); /* eat trailing */
6346 optval = strdup(cp);
6347 /* call function to deal with it all */
6348 if (configopt[optind].func(optsub, optval,
6349 configopt[optind].funcflags) != 0)
6350 errx(1, "%s: line %zd: invalid data for %s",
6351 filename, lineno, configopt[optind].optname);
6358 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6365 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6371 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6373 struct ws_win *parent, *w;
6374 struct swm_region *r;
6375 struct workspace *ws;
6376 xcb_icccm_wm_hints_t wmh;
6378 parent = find_window(win->transient);
6380 parent->child_trans = win;
6382 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6383 " for 0x%x trans 0x%x\n", win->id, win->transient);
6385 r = root_to_region(win->wa->root);
6387 /* parent doen't exist in our window list */
6388 TAILQ_FOREACH(w, &ws->winlist, entry) {
6389 if (xcb_icccm_get_wm_hints_reply(conn,
6390 xcb_icccm_get_wm_hints(conn, w->id),
6392 warnx("can't get hints for 0x%x", w->id);
6396 if (win->hints.window_group != wmh.window_group)
6399 w->child_trans = win;
6400 win->transient = w->id;
6402 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6403 "transient to 0x%x\n", win->transient);
6410 window_get_pid(xcb_window_t win)
6415 xcb_get_property_cookie_t pc;
6416 xcb_get_property_reply_t *pr;
6418 apid = get_atom_from_string("_NET_WM_PID");
6419 if (apid == XCB_ATOM_NONE)
6422 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6423 pr = xcb_get_property_reply(conn, pc, NULL);
6426 if (pr->type != XCB_ATOM_CARDINAL) {
6431 if (pr->type == apid && pr->format == 32)
6432 ret = *((pid_t *)xcb_get_property_value(pr));
6438 apid = get_atom_from_string("_SWM_PID");
6439 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6441 pr = xcb_get_property_reply(conn, pc, NULL);
6444 if (pr->type != apid) {
6449 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6456 get_ws_idx(xcb_window_t id)
6462 xcb_get_property_reply_t *gpr;
6464 gpr = xcb_get_property_reply(conn,
6465 xcb_get_property(conn, 0, id, a_swm_ws,
6466 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6471 proplen = xcb_get_property_value_length(gpr);
6473 prop = malloc(proplen + 1);
6476 xcb_get_property_value(gpr),
6478 prop[proplen] = '\0';
6485 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6486 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6488 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6498 manage_window(xcb_window_t id)
6500 xcb_window_t trans = XCB_WINDOW_NONE;
6501 struct workspace *ws;
6502 struct ws_win *win, *ww;
6503 int ws_idx, border_me = 0;
6504 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6505 struct swm_region *r;
6508 uint32_t event_mask, i;
6509 xcb_icccm_get_wm_protocols_reply_t wpr;
6510 xcb_icccm_get_wm_class_reply_t tmpch;
6512 if ((win = find_window(id)) != NULL)
6513 return (win); /* already being managed */
6515 /* see if we are on the unmanaged list */
6516 if ((win = find_unmanaged_window(id)) != NULL) {
6517 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6518 "window: 0x%x\n", win->id);
6519 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6521 set_child_transient(win, &trans);
6523 if (trans && (ww = find_window(trans)))
6524 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6525 else if ((ww = win->ws->focus) &&
6526 spawn_position == SWM_STACK_ABOVE)
6527 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6529 else if (ww && spawn_position == SWM_STACK_BELOW)
6530 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6531 else switch (spawn_position) {
6534 case SWM_STACK_ABOVE:
6535 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6537 case SWM_STACK_BOTTOM:
6538 case SWM_STACK_BELOW:
6539 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6542 ewmh_update_actions(win);
6546 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6547 err(1, "manage_window: calloc: failed to allocate memory for "
6553 /* see if we need to override the workspace */
6554 p = find_pid(window_get_pid(id));
6556 /* Get all the window data in one shot */
6557 ws_idx = get_ws_idx(id);
6559 win->wa = xcb_get_geometry_reply(conn,
6560 xcb_get_geometry(conn, id),
6562 xcb_icccm_get_wm_normal_hints_reply(conn,
6563 xcb_icccm_get_wm_normal_hints(conn, id),
6565 xcb_icccm_get_wm_hints_reply(conn,
6566 xcb_icccm_get_wm_hints(conn, id),
6568 xcb_icccm_get_wm_transient_for_reply(conn,
6569 xcb_icccm_get_wm_transient_for(conn, id),
6572 win->transient = trans;
6573 set_child_transient(win, &trans);
6574 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6575 "transient: 0x%x\n", win->id, win->transient);
6578 /* get supported protocols */
6579 if (xcb_icccm_get_wm_protocols_reply(conn,
6580 xcb_icccm_get_wm_protocols(conn, id, a_prot),
6582 for (i = 0; i < wpr.atoms_len; i++) {
6583 if (wpr.atoms[i] == a_takefocus)
6584 win->take_focus = 1;
6585 if (wpr.atoms[i] == a_delete)
6586 win->can_delete = 1;
6588 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6591 win->iconic = get_iconic(win);
6594 * Figure out where to put the window. If it was previously assigned to
6595 * a workspace (either by spawn() or manually moving), and isn't
6596 * transient, * put it in the same workspace
6598 r = root_to_region(win->wa->root);
6600 ws = &r->s->ws[p->ws];
6601 TAILQ_REMOVE(&pidlist, p, entry);
6604 } else if (ws_idx != -1 && win->transient == 0) {
6605 ws = &r->s->ws[ws_idx];
6608 /* this should launch transients in the same ws as parent */
6610 if ((ww = find_window(trans)) != NULL)
6616 warnx("manage_window: fix this "
6622 /* set up the window layout */
6625 win->s = r->s; /* this never changes */
6626 if (trans && (ww = find_window(trans)))
6627 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6628 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6629 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6631 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6632 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6633 else switch (spawn_position) {
6636 case SWM_STACK_ABOVE:
6637 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6639 case SWM_STACK_BOTTOM:
6640 case SWM_STACK_BELOW:
6641 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6644 /* ignore window border if there is one. */
6645 WIDTH(win) = win->wa->width;
6646 HEIGHT(win) = win->wa->height;
6647 X(win) = win->wa->x + win->wa->border_width;
6648 Y(win) = win->wa->y + win->wa->border_width;
6650 win->g_floatvalid = 0;
6651 win->floatmaxed = 0;
6652 win->ewmh_flags = 0;
6654 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6655 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6656 HEIGHT(win), ws->idx);
6658 constrain_window(win, r, 0);
6660 /* Set window properties so we can remember this after reincarnation */
6661 if (prop == NULL && snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6663 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6665 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6666 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6672 ewmh_autoquirk(win);
6674 if (xcb_icccm_get_wm_class_reply(conn,
6675 xcb_icccm_get_wm_class(conn, win->id),
6677 win->ch.class_name = tmpch.class_name;
6678 win->ch.instance_name = tmpch.instance_name;
6680 xcb_icccm_get_wm_class_reply_wipe(&tmpch);
6682 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6683 win->ch.class_name, win->ch.instance_name);
6685 /* java is retarded so treat it special */
6686 if (strstr(win->ch.instance_name, "sun-awt")) {
6691 TAILQ_FOREACH(qp, &quirks, entry) {
6692 if (!strcmp(win->ch.class_name, qp->class) &&
6693 !strcmp(win->ch.instance_name, qp->name)) {
6694 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6695 "class: %s, name: %s\n", win->ch.class_name,
6696 win->ch.instance_name);
6697 if (qp->quirk & SWM_Q_FLOAT) {
6701 win->quirks = qp->quirk;
6706 /* alter window position if quirky */
6707 if (win->quirks & SWM_Q_ANYWHERE) {
6708 win->manual = 1; /* don't center the quirky windows */
6709 if (bar_enabled && Y(win) < bar_height)
6710 Y(win) = bar_height;
6711 if (WIDTH(win) + X(win) > WIDTH(r))
6712 X(win) = WIDTH(r) - WIDTH(win) - 2;
6716 /* Reset font sizes (the bruteforce way; no default keybinding). */
6717 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6718 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6719 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6720 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6721 fake_keypress(win, XK_KP_Add, ShiftMask);
6724 ewmh_get_win_state(win);
6725 ewmh_update_actions(win);
6726 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6731 X(win) -= border_width;
6732 Y(win) -= border_width;
6736 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6737 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6739 event_mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
6742 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6744 /* floaters need to be mapped if they are in the current workspace */
6745 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6746 map_window_raised(win->id);
6752 free_window(struct ws_win *win)
6754 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6759 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6767 memset(win, 0xff, sizeof *win); /* XXX kill later */
6770 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6774 unmanage_window(struct ws_win *win)
6776 struct ws_win *parent;
6777 xcb_screen_t *screen;
6782 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6784 if (win->transient) {
6785 parent = find_window(win->transient);
6787 parent->child_trans = NULL;
6790 /* focus on root just in case */
6791 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6792 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6793 screen->root, XCB_CURRENT_TIME);
6797 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6798 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6802 focus_magic(struct ws_win *win)
6804 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6807 /* if there are no windows clear the status-bar */
6812 if (win->child_trans) {
6813 /* win = parent & has a transient so focus on that */
6815 focus_win(win->child_trans);
6816 if (win->child_trans->take_focus)
6817 client_msg(win, a_takefocus);
6819 /* make sure transient hasn't disappeared */
6820 if (validate_win(win->child_trans) == 0) {
6821 focus_win(win->child_trans);
6822 if (win->child_trans->take_focus)
6823 client_msg(win->child_trans,
6826 win->child_trans = NULL;
6828 if (win->take_focus)
6829 client_msg(win, a_takefocus);
6835 if (win->take_focus)
6836 client_msg(win, a_takefocus);
6841 expose(xcb_expose_event_t *e)
6844 struct swm_region *r;
6846 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6848 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6849 for (i = 0; i < num_screens; i++)
6850 TAILQ_FOREACH(r, &screens[i].rl, entry)
6851 if (e->window == WINID(r->bar))
6859 focusin(xcb_focus_in_event_t *e)
6861 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
6862 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
6863 e->mode, get_notify_detail_label(e->detail), e->detail);
6868 keypress(xcb_key_press_event_t *e)
6870 xcb_keysym_t keysym;
6873 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6875 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6877 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6880 if (kp->funcid == KF_SPAWN_CUSTOM)
6881 spawn_custom(root_to_region(e->root),
6882 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6883 else if (keyfuncs[kp->funcid].func)
6884 keyfuncs[kp->funcid].func(root_to_region(e->root),
6885 &(keyfuncs[kp->funcid].args));
6889 buttonpress(xcb_button_press_event_t *e)
6893 unsigned int action;
6895 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6896 e->event, e->detail);
6898 if ((win = find_window(e->event)) == NULL)
6902 action = client_click;
6904 for (i = 0; i < LENGTH(buttons); i++)
6905 if (action == buttons[i].action && buttons[i].func &&
6906 buttons[i].button == e->detail &&
6907 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6908 buttons[i].func(win, &buttons[i].args);
6915 print_win_geom(xcb_window_t w)
6917 xcb_get_geometry_reply_t *wa;
6919 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
6922 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
6927 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
6928 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6929 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
6937 configurerequest(xcb_configure_request_event_t *e)
6942 uint32_t wc[7] = {0};
6944 if ((win = find_window(e->window)) == NULL)
6945 if ((win = find_unmanaged_window(e->window)) == NULL)
6948 print_win_geom(e->window);
6951 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6952 "value_mask: 0x%x", e->window, e->value_mask);
6953 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6954 mask |= XCB_CONFIG_WINDOW_X;
6956 DPRINTF(", X: %d", e->x);
6958 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6959 mask |= XCB_CONFIG_WINDOW_Y;
6961 DPRINTF(", Y: %d", e->y);
6963 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6964 mask |= XCB_CONFIG_WINDOW_WIDTH;
6966 DPRINTF(", W: %u", e->width);
6968 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6969 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6970 wc[i++] = e->height;
6971 DPRINTF(", H: %u", e->height);
6973 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6974 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6975 wc[i++] = e->border_width;
6976 DPRINTF(", Border: %u", e->border_width);
6978 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6979 mask |= XCB_CONFIG_WINDOW_SIBLING;
6980 wc[i++] = e->sibling;
6981 DPRINTF(", Sibling: 0x%x", e->sibling);
6983 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6984 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6985 wc[i++] = e->stack_mode;
6986 DPRINTF(", StackMode: %u", e->stack_mode);
6990 xcb_configure_window(conn, e->window, mask, wc);
6992 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
6993 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6994 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6995 win->g_float.x = e->x - X(win->ws->r);
6996 win->g_float.y = e->y - Y(win->ws->r);
6997 win->g_float.w = e->width;
6998 win->g_float.h = e->height;
6999 win->g_floatvalid = 1;
7001 if (win->floating) {
7002 win->g = win->g_float;
7003 win->g.x += X(win->ws->r);
7004 win->g.y += Y(win->ws->r);
7017 configurenotify(xcb_configure_notify_event_t *e)
7021 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
7024 win = find_window(e->window);
7026 xcb_icccm_get_wm_normal_hints_reply(conn,
7027 xcb_icccm_get_wm_normal_hints(conn, win->id),
7036 destroynotify(xcb_destroy_notify_event_t *e)
7040 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7042 if ((win = find_window(e->window)) == NULL) {
7043 if ((win = find_unmanaged_window(e->window)) == NULL)
7049 /* make sure we focus on something */
7052 unmanage_window(win);
7059 get_notify_detail_label(uint8_t detail)
7064 case XCB_NOTIFY_DETAIL_ANCESTOR:
7067 case XCB_NOTIFY_DETAIL_VIRTUAL:
7070 case XCB_NOTIFY_DETAIL_INFERIOR:
7073 case XCB_NOTIFY_DETAIL_NONLINEAR:
7074 label = "Nonlinear";
7076 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7077 label = "NonlinearVirtual";
7079 case XCB_NOTIFY_DETAIL_POINTER:
7082 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7083 label = "PointerRoot";
7085 case XCB_NOTIFY_DETAIL_NONE:
7096 get_notify_mode_label(uint8_t mode)
7101 case XCB_NOTIFY_MODE_NORMAL:
7104 case XCB_NOTIFY_MODE_GRAB:
7107 case XCB_NOTIFY_MODE_UNGRAB:
7110 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7111 label = "WhileGrabbed";
7122 enternotify(xcb_enter_notify_event_t *e)
7125 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %s(%d), "
7126 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7127 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7128 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7129 e->child, YESNO(e->same_screen_focus), e->state);
7131 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
7132 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
7137 switch (focus_mode) {
7138 case SWM_FOCUS_DEFAULT:
7140 case SWM_FOCUS_FOLLOW:
7142 case SWM_FOCUS_SYNERGY:
7146 if ((win = find_window(e->event)) == NULL) {
7147 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7158 leavenotify(xcb_leave_notify_event_t *e)
7161 DNPRINTF(SWM_D_FOCUS, "leavenotify: window: 0x%x, mode: %s(%d), "
7162 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7163 "%s, state: %d\n", e->event, get_notify_mode_label(e->mode),
7164 e->mode, get_notify_detail_label(e->detail), e->detail, e->root,
7165 e->child, YESNO(e->same_screen_focus), e->state);
7169 /* lets us use one switch statement for arbitrary mode/detail combinations */
7170 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7173 mapnotify(xcb_map_notify_event_t *e)
7177 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7179 win = manage_window(e->window);
7181 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7184 * focus_win can only set input focus on a mapped window.
7185 * make sure the window really has focus since it is just being mapped.
7187 if (win->ws->focus == win)
7192 mappingnotify(xcb_mapping_notify_event_t *e)
7194 xcb_refresh_keyboard_mapping(syms, e);
7196 if (e->request == XCB_MAPPING_KEYBOARD)
7201 maprequest(xcb_map_request_event_t *e)
7204 struct swm_region *r;
7205 xcb_get_window_attributes_reply_t *war;
7207 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
7210 war = xcb_get_window_attributes_reply(conn,
7211 xcb_get_window_attributes(conn, e->window),
7215 if (war->override_redirect) {
7221 win = manage_window(e->window);
7223 return; /* can't happen */
7228 /* make new win focused */
7229 r = root_to_region(win->wa->root);
7230 if (win->ws == r->ws)
7236 get_atom_name(xcb_atom_t atom)
7240 xcb_get_atom_name_reply_t *r;
7242 r = xcb_get_atom_name_reply(conn,
7243 xcb_get_atom_name(conn, atom),
7246 len = xcb_get_atom_name_name_length(r);
7248 name = malloc(len + 1);
7250 memcpy(name, xcb_get_atom_name_name(r), len);
7262 propertynotify(xcb_property_notify_event_t *e)
7268 name = get_atom_name(e->atom);
7269 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7270 e->window, name, e->atom);
7274 win = find_window(e->window);
7278 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7279 update_iconic(win, 0);
7280 map_window_raised(win->id);
7287 case XCB_ATOM_WM_CLASS:
7288 case XCB_ATOM_WM_NAME:
7297 unmapnotify(xcb_unmap_notify_event_t *e)
7301 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7303 /* determine if we need to help unmanage this window */
7304 win = find_window(e->window);
7308 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7309 unmanage_window(win);
7312 /* resend unmap because we ated it */
7313 xcb_unmap_window(conn, e->window);
7320 visibilitynotify(xcb_visibility_notify_event_t *e)
7322 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7328 clientmessage(xcb_client_message_event_t *e)
7331 xcb_map_request_event_t mre;
7335 name = get_atom_name(e->type);
7336 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7337 e->window, name, e->type);
7340 win = find_window(e->window);
7343 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7344 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7345 "unmanaged window.\n");
7346 mre.window = e->window;
7352 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7353 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7356 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7357 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7358 if (win->can_delete)
7359 client_msg(win, a_delete);
7361 xcb_kill_client(conn, win->id);
7363 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7364 DNPRINTF(SWM_D_EVENT,
7365 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7366 if (win->floating) {
7367 if (e->data.data32[0] & (1<<8)) /* x */
7368 X(win) = e->data.data32[1];
7369 if (e->data.data32[0] & (1<<9)) /* y */
7370 Y(win) = e->data.data32[2];
7371 if (e->data.data32[0] & (1<<10)) /* width */
7372 WIDTH(win) = e->data.data32[3];
7373 if (e->data.data32[0] & (1<<11)) /* height */
7374 HEIGHT(win) = e->data.data32[4];
7379 /* TODO: Change stack sizes */
7380 /* notify no change was made. */
7381 config_win(win, NULL);
7384 if (e->type == ewmh[_NET_WM_STATE].atom) {
7385 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7386 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7387 if (e->data.data32[2])
7388 ewmh_update_win_state(win, e->data.data32[2],
7401 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7403 xcb_void_cookie_t wac;
7404 xcb_generic_error_t *error;
7406 /* this causes an error if some other window manager is running */
7407 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7408 for (i = 0; i < num_screens; i++) {
7410 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7412 wac = xcb_change_window_attributes_checked(conn, sc->root,
7413 XCB_CW_EVENT_MASK, &val);
7414 if ((error = xcb_request_check(conn, wac))) {
7415 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7426 new_region(struct swm_screen *s, int x, int y, int w, int h)
7428 struct swm_region *r, *n;
7429 struct workspace *ws = NULL;
7432 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7433 s->idx, w, h, x, y);
7435 /* remove any conflicting regions */
7436 n = TAILQ_FIRST(&s->rl);
7439 n = TAILQ_NEXT(r, entry);
7440 if (X(r) < (x + w) &&
7441 (X(r) + WIDTH(r)) > x &&
7443 (Y(r) + HEIGHT(r)) > y) {
7444 if (r->ws->r != NULL)
7445 r->ws->old_r = r->ws->r;
7448 TAILQ_REMOVE(&s->rl, r, entry);
7449 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7453 /* search old regions for one to reuse */
7455 /* size + location match */
7456 TAILQ_FOREACH(r, &s->orl, entry)
7457 if (X(r) == x && Y(r) == y &&
7458 HEIGHT(r) == h && WIDTH(r) == w)
7462 TAILQ_FOREACH(r, &s->orl, entry)
7463 if (HEIGHT(r) == h && WIDTH(r) == w)
7467 TAILQ_REMOVE(&s->orl, r, entry);
7468 /* try to use old region's workspace */
7469 if (r->ws->r == NULL)
7472 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7473 err(1, "new_region: calloc: failed to allocate memory "
7476 /* if we don't have a workspace already, find one */
7478 for (i = 0; i < workspace_limit; i++)
7479 if (s->ws[i].r == NULL) {
7486 errx(1, "new_region: no free workspaces");
7497 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7503 #ifdef SWM_XRR_HAS_CRTC
7506 #endif /* SWM_XRR_HAS_CRTC */
7507 struct swm_region *r;
7509 xcb_randr_get_screen_resources_current_cookie_t src;
7510 xcb_randr_get_screen_resources_current_reply_t *srr;
7511 xcb_randr_get_crtc_info_cookie_t cic;
7512 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7513 xcb_randr_crtc_t *crtc;
7514 xcb_screen_t *screen = get_screen(i);
7516 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7517 if (i >= num_screens)
7518 errx(1, "scan_xrandr: invalid screen");
7520 /* remove any old regions */
7521 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7522 r->ws->old_r = r->ws->r = NULL;
7524 TAILQ_REMOVE(&screens[i].rl, r, entry);
7525 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7529 /* map virtual screens onto physical screens */
7530 #ifdef SWM_XRR_HAS_CRTC
7531 if (xrandr_support) {
7532 src = xcb_randr_get_screen_resources_current(conn,
7534 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7537 new_region(&screens[i], 0, 0,
7538 screen->width_in_pixels,
7539 screen->height_in_pixels);
7542 ncrtc = srr->num_crtcs;
7544 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7545 for (c = 0; c < ncrtc; c++) {
7546 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7548 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7551 if (cir->num_outputs == 0) {
7557 new_region(&screens[i], 0, 0,
7558 screen->width_in_pixels,
7559 screen->height_in_pixels);
7561 new_region(&screens[i],
7562 cir->x, cir->y, cir->width, cir->height);
7567 #endif /* SWM_XRR_HAS_CRTC */
7569 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7570 screen->height_in_pixels);
7575 screenchange(xcb_randr_screen_change_notify_event_t *e)
7577 struct swm_region *r;
7580 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7582 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7583 /* silly event doesn't include the screen index */
7584 for (i = 0; i < num_screens; i++)
7585 if (screens[i].root == e->root)
7587 if (i >= num_screens)
7588 errx(1, "screenchange: screen not found");
7590 /* brute force for now, just re-enumerate the regions */
7594 print_win_geom(e->root);
7596 /* add bars to all regions */
7597 for (i = 0; i < num_screens; i++)
7598 TAILQ_FOREACH(r, &screens[i].rl, entry)
7606 xcb_window_t *wins = NULL;
7608 int i, j, num_screens;
7609 uint16_t state, manage;
7611 xcb_query_tree_cookie_t qtc;
7612 xcb_query_tree_reply_t *qtr;
7613 xcb_get_window_attributes_cookie_t c;
7614 xcb_get_window_attributes_reply_t *r;
7615 xcb_get_property_cookie_t pc;
7617 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
7618 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7619 for (i = 0; i < num_screens; i++) {
7620 qtc = xcb_query_tree(conn, screens[i].root);
7621 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7624 wins = xcb_query_tree_children(qtr);
7625 no = xcb_query_tree_children_length(qtr);
7626 /* attach windows to a region */
7627 /* normal windows */
7628 for (j = 0; j < no; j++) {
7629 c = xcb_get_window_attributes(conn, wins[j]);
7630 r = xcb_get_window_attributes_reply(conn, c, NULL);
7633 if (r->override_redirect) {
7638 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7639 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7645 state = getstate(wins[j]);
7646 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7647 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7648 manage_window(wins[j]);
7651 /* transient windows */
7652 for (j = 0; j < no; j++) {
7653 c = xcb_get_window_attributes(conn, wins[j]);
7654 r = xcb_get_window_attributes_reply(conn, c, NULL);
7657 if (r->override_redirect) {
7663 state = getstate(wins[j]);
7664 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7665 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7666 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7667 &wins[j], NULL) && manage)
7668 manage_window(wins[j]);
7672 DNPRINTF(SWM_D_INIT, "grab_windows: done\n");
7678 int i, j, k, num_screens;
7679 struct workspace *ws;
7680 uint32_t gcv[1], wa[1];
7681 const xcb_query_extension_reply_t *qep;
7682 xcb_cursor_t cursor;
7683 xcb_font_t cursor_font;
7684 xcb_randr_query_version_cookie_t c;
7685 xcb_randr_query_version_reply_t *r;
7687 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7688 if ((screens = calloc(num_screens,
7689 sizeof(struct swm_screen))) == NULL)
7690 err(1, "setup_screens: calloc: failed to allocate memory for "
7693 /* initial Xrandr setup */
7695 c = xcb_randr_query_version(conn, 1, 1);
7696 r = xcb_randr_query_version_reply(conn, c, NULL);
7698 if (r->major_version >= 1)
7702 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7703 xrandr_eventbase = qep->first_event;
7705 cursor_font = xcb_generate_id(conn);
7706 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7708 cursor = xcb_generate_id(conn);
7709 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7710 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7713 /* map physical screens */
7714 for (i = 0; i < num_screens; i++) {
7715 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7717 TAILQ_INIT(&screens[i].rl);
7718 TAILQ_INIT(&screens[i].orl);
7719 screens[i].root = get_screen(i)->root;
7721 /* set default colors */
7722 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7723 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7724 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7725 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7726 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7728 /* create graphics context on screen */
7729 screens[i].bar_gc = xcb_generate_id(conn);
7731 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7732 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7734 /* set default cursor */
7735 xcb_change_window_attributes(conn, screens[i].root,
7738 /* init all workspaces */
7739 /* XXX these should be dynamically allocated too */
7740 for (j = 0; j < SWM_WS_MAX; j++) {
7741 ws = &screens[i].ws[j];
7747 TAILQ_INIT(&ws->winlist);
7748 TAILQ_INIT(&ws->unmanagedlist);
7750 for (k = 0; layouts[k].l_stack != NULL; k++)
7751 if (layouts[k].l_config != NULL)
7752 layouts[k].l_config(ws,
7753 SWM_ARG_ID_STACKINIT);
7754 ws->cur_layout = &layouts[0];
7755 ws->cur_layout->l_string(ws);
7761 xcb_randr_select_input(conn, screens[i].root,
7762 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7764 xcb_free_cursor(conn, cursor);
7765 xcb_close_font(conn, cursor_font);
7771 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7772 err(1, "setup_globals: strdup: failed to allocate memory.");
7774 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7775 err(1, "setup_globals: strdup: failed to allocate memory.");
7777 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7778 errx(1, "unable to allocate key symbols");
7780 a_state = get_atom_from_string("WM_STATE");
7781 a_prot = get_atom_from_string("WM_PROTOCOLS");
7782 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7783 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7784 a_wmname = get_atom_from_string("WM_NAME");
7785 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7786 a_utf8_string = get_atom_from_string("UTF8_STRING");
7787 a_string = get_atom_from_string("STRING");
7788 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7789 a_swm_ws = get_atom_from_string("_SWM_WS");
7796 xcb_atom_t netwmcheck;
7797 xcb_window_t root, win;
7800 /* work around sun jdk bugs, code from wmname */
7801 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7803 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7804 for (i = 0; i < num_screens; i++) {
7805 root = screens[i].root;
7807 win = xcb_generate_id(conn);
7808 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
7809 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].pixel;
7810 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7811 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7812 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7814 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7815 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7816 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7817 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7818 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7819 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
7824 event_error(xcb_generic_error_t *e)
7826 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7827 "resource_id: %u, minor_code: %u\n",
7828 xcb_event_get_error_label(e->error_code), e->error_code,
7829 xcb_event_get_request_label(e->major_code), e->major_code,
7830 e->sequence, e->resource_id, e->minor_code);
7834 event_handle(xcb_generic_event_t *evt)
7836 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7838 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7839 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7840 XCB_EVENT_RESPONSE_TYPE(evt));
7843 #define EVENT(type, callback) case type: callback((void *)evt); return
7844 EVENT(0, event_error);
7845 EVENT(XCB_BUTTON_PRESS, buttonpress);
7846 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7847 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7848 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7849 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7850 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7851 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7852 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7853 /*EVENT(XCB_CREATE_NOTIFY, );*/
7854 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7855 EVENT(XCB_ENTER_NOTIFY, enternotify);
7856 EVENT(XCB_EXPOSE, expose);
7858 EVENT(XCB_FOCUS_IN, focusin);
7860 /*EVENT(XCB_FOCUS_OUT, );*/
7861 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7862 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7863 EVENT(XCB_KEY_PRESS, keypress);
7864 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7865 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7867 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
7869 EVENT(XCB_MAP_NOTIFY, mapnotify);
7870 EVENT(XCB_MAP_REQUEST, maprequest);
7871 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7872 /*EVENT(XCB_MOTION_NOTIFY, );*/
7873 /*EVENT(XCB_NO_EXPOSURE, );*/
7874 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7875 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7876 /*EVENT(XCB_RESIZE_REQUEST, );*/
7877 /*EVENT(XCB_SELECTION_CLEAR, );*/
7878 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7879 /*EVENT(XCB_SELECTION_REQUEST, );*/
7880 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7881 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7884 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7885 screenchange((void *)evt);
7889 main(int argc, char *argv[])
7891 struct swm_region *r, *rr;
7892 struct ws_win *winfocus = NULL;
7894 char conf[PATH_MAX], *cfile = NULL;
7896 int xfd, i, num_screens;
7897 struct sigaction sact;
7898 xcb_generic_event_t *evt;
7902 /* suppress unused warning since var is needed */
7905 time_started = time(NULL);
7908 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7909 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
7910 warnx("no locale support");
7912 /* handle some signals */
7913 bzero(&sact, sizeof(sact));
7914 sigemptyset(&sact.sa_mask);
7916 sact.sa_handler = sighdlr;
7917 sigaction(SIGINT, &sact, NULL);
7918 sigaction(SIGQUIT, &sact, NULL);
7919 sigaction(SIGTERM, &sact, NULL);
7920 sigaction(SIGHUP, &sact, NULL);
7922 sact.sa_handler = sighdlr;
7923 sact.sa_flags = SA_NOCLDSTOP;
7924 sigaction(SIGCHLD, &sact, NULL);
7926 if (!(display = XOpenDisplay(0)))
7927 errx(1, "can not open display");
7929 conn = XGetXCBConnection(display);
7930 if (xcb_connection_has_error(conn))
7931 errx(1, "can not get XCB connection");
7933 XSetEventQueueOwner(display, XCBOwnsEventQueue);
7935 xcb_prefetch_extension_data(conn, &xcb_randr_id);
7936 xfd = xcb_get_file_descriptor(conn);
7938 /* look for local and global conf file */
7939 pwd = getpwuid(getuid());
7941 errx(1, "invalid user: %d", getuid());
7943 xcb_grab_server(conn);
7946 /* flush all events */
7947 while ((evt = xcb_poll_for_event(conn))) {
7948 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7953 if (enable_wm() != 0)
7954 errx(1, "another window manager is currently running");
7965 for (i = 0; ; i++) {
7970 snprintf(conf, sizeof conf, "%s/.%s",
7971 pwd->pw_dir, SWM_CONF_FILE);
7975 snprintf(conf, sizeof conf, "/etc/%s",
7980 snprintf(conf, sizeof conf, "%s/.%s",
7981 pwd->pw_dir, SWM_CONF_FILE_OLD);
7985 snprintf(conf, sizeof conf, "/etc/%s",
7992 if (strlen(conf) && stat(conf, &sb) != -1)
7993 if (S_ISREG(sb.st_mode)) {
8000 /* load conf (if any) */
8002 conf_load(cfile, SWM_CONF_DEFAULT);
8005 /* set some values to work around bad programs */
8007 /* grab existing windows (before we build the bars) */
8010 if (getenv("SWM_STARTED") == NULL)
8011 setenv("SWM_STARTED", "YES", 1);
8013 /* setup all bars */
8014 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8015 for (i = 0; i < num_screens; i++)
8016 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8017 if (winfocus == NULL)
8018 winfocus = TAILQ_FIRST(&r->ws->winlist);
8027 xcb_ungrab_server(conn);
8031 while ((evt = xcb_poll_for_event(conn))) {
8038 /* if we are being restarted go focus on first window */
8040 rr = winfocus->ws->r;
8042 /* not a visible window */
8046 /* move pointer to first screen if multi screen */
8047 if (num_screens > 1 || outputs > 1)
8048 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
8049 rr->s[0].root, 0, 0, 0, 0, X(rr),
8050 Y(rr) + (bar_enabled ? bar_height : 0));
8052 a.id = SWM_ARG_ID_FOCUSCUR;
8062 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8064 DNPRINTF(SWM_D_MISC, "select failed");
8065 if (restart_wm == 1)
8066 restart(NULL, NULL);
8067 if (search_resp == 1)
8080 for (i = 0; i < num_screens; ++i)
8081 if (screens[i].bar_gc != 0)
8082 xcb_free_gc(conn, screens[i].bar_gc);
8084 XftFontClose(display, bar_font);
8085 XftColorFree(display, DefaultVisual(display, 0),
8086 DefaultColormap(display, 0), &bar_font_color);
8087 xcb_key_symbols_free(syms);
8089 xcb_disconnect(conn);