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>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
58 #include <sys/types.h>
62 #include <sys/queue.h>
63 #include <sys/param.h>
64 #include <sys/select.h>
65 #if defined(__linux__)
67 #elif defined(__OpenBSD__)
69 #elif defined(__FreeBSD__)
90 #include <X11/cursorfont.h>
91 #include <X11/extensions/Xrandr.h>
92 #include <X11/Xft/Xft.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/xcb_atom.h>
95 #include <xcb/xcb_aux.h>
96 #include <xcb/xcb_event.h>
97 #include <xcb/xcb_icccm.h>
98 #include <xcb/xcb_keysyms.h>
99 #include <xcb/xtest.h>
100 #include <xcb/randr.h>
108 #ifdef SPECTRWM_BUILDSTR
109 static const char *buildstr = SPECTRWM_BUILDSTR;
111 static const char *buildstr = SPECTRWM_VERSION;
115 # error XRandR versions less than 1.0 are not supported
120 #define SWM_XRR_HAS_CRTC
124 #if defined(__OpenBSD__)
125 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
126 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
128 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
129 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
130 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
131 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
132 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
133 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
134 #define xcb_icccm_get_wm_class xcb_get_wm_class
135 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
136 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
137 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
138 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
139 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
140 #define xcb_icccm_get_wm_name xcb_get_wm_name
141 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
142 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
143 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
144 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
145 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
146 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
147 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
148 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
149 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
150 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
155 #define DPRINTF(x...) do { \
157 fprintf(stderr, x); \
159 #define DNPRINTF(n,x...) do { \
160 if (swm_debug & n) { \
161 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
162 fprintf(stderr, x); \
165 #define SWM_D_MISC 0x0001
166 #define SWM_D_EVENT 0x0002
167 #define SWM_D_WS 0x0004
168 #define SWM_D_FOCUS 0x0008
169 #define SWM_D_MOVE 0x0010
170 #define SWM_D_STACK 0x0020
171 #define SWM_D_MOUSE 0x0040
172 #define SWM_D_PROP 0x0080
173 #define SWM_D_CLASS 0x0100
174 #define SWM_D_KEY 0x0200
175 #define SWM_D_QUIRK 0x0400
176 #define SWM_D_SPAWN 0x0800
177 #define SWM_D_EVENTQ 0x1000
178 #define SWM_D_CONF 0x2000
179 #define SWM_D_BAR 0x4000
180 #define SWM_D_INIT 0x8000
182 u_int32_t swm_debug = 0
201 #define DPRINTF(x...)
202 #define DNPRINTF(n,x...)
205 #define LENGTH(x) (int)(sizeof x / sizeof x[0])
206 #define MODKEY Mod1Mask
207 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
208 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
209 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
210 #define SWM_PROPLEN (16)
211 #define SWM_FUNCNAME_LEN (32)
212 #define SWM_KEYS_LEN (255)
213 #define SWM_QUIRK_LEN (64)
214 #define X(r) (r)->g.x
215 #define Y(r) (r)->g.y
216 #define WIDTH(r) (r)->g.w
217 #define HEIGHT(r) (r)->g.h
218 #define BORDER(w) (w->bordered ? border_width : 0)
219 #define MAX_X(r) ((r)->g.x + (r)->g.w)
220 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
221 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
222 #define SH_MIN_W(w) (w)->sh.min_width
223 #define SH_MIN_H(w) (w)->sh.min_height
224 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
225 #define SH_MAX_W(w) (w)->sh.max_width
226 #define SH_MAX_H(w) (w)->sh.max_height
227 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
228 #define SH_INC_W(w) (w)->sh.width_inc
229 #define SH_INC_H(w) (w)->sh.height_inc
230 #define SWM_MAX_FONT_STEPS (3)
231 #define WINID(w) ((w) ? (w)->id : 0)
232 #define YESNO(x) ((x) ? "yes" : "no")
234 #define SWM_FOCUS_DEFAULT (0)
235 #define SWM_FOCUS_SYNERGY (1)
236 #define SWM_FOCUS_FOLLOW (2)
238 #define SWM_CONF_DEFAULT (0)
239 #define SWM_CONF_KEYMAPPING (1)
242 #define SWM_LIB "/usr/local/lib/libswmhack.so"
249 xcb_atom_t a_takefocus;
251 xcb_atom_t a_netwmname;
252 xcb_atom_t a_utf8_string;
254 xcb_atom_t a_swm_iconic;
255 volatile sig_atomic_t running = 1;
256 volatile sig_atomic_t restart_wm = 0;
258 /*int last_focus_event = FocusOut;*/
262 int xrandr_eventbase;
263 unsigned int numlockmask = 0;
266 xcb_connection_t *conn;
267 xcb_key_symbols_t *syms;
270 int cycle_visible = 0;
272 int font_adjusted = 0;
273 unsigned int mod_key = MODKEY;
276 struct swm_region *search_r;
277 int select_list_pipe[2];
278 int select_resp_pipe[2];
280 volatile sig_atomic_t search_resp;
281 int search_resp_action;
283 struct search_window {
284 TAILQ_ENTRY(search_window) entry;
288 xcb_window_t indicator;
290 TAILQ_HEAD(search_winlist, search_window);
291 struct search_winlist search_wl;
296 SWM_SEARCH_UNICONIFY,
297 SWM_SEARCH_NAME_WORKSPACE,
298 SWM_SEARCH_SEARCH_WORKSPACE,
299 SWM_SEARCH_SEARCH_WINDOW
302 #define SWM_STACK_TOP (0)
303 #define SWM_STACK_BOTTOM (1)
304 #define SWM_STACK_ABOVE (2)
305 #define SWM_STACK_BELOW (3)
308 double dialog_ratio = 0.6;
310 #define SWM_BAR_MAX (256)
311 #define SWM_BAR_JUSTIFY_LEFT (0)
312 #define SWM_BAR_JUSTIFY_CENTER (1)
313 #define SWM_BAR_JUSTIFY_RIGHT (2)
314 #define SWM_BAR_OFFSET (4)
315 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
316 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
317 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
318 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
319 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
321 char *bar_argv[] = { NULL, NULL };
323 unsigned char bar_ext[SWM_BAR_MAX];
324 char bar_vertext[SWM_BAR_MAX];
326 sig_atomic_t bar_alarm = 0;
329 int bar_border_width = 1;
330 int bar_at_bottom = 0;
332 int bar_extra_running = 0;
335 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
336 char *bar_format = NULL;
337 int stack_enabled = 1;
338 int clock_enabled = 1;
339 int urgent_enabled = 0;
340 char *clock_format = NULL;
341 int title_name_enabled = 0;
342 int title_class_enabled = 0;
343 int window_name_enabled = 0;
344 int focus_mode = SWM_FOCUS_DEFAULT;
345 int focus_close = SWM_STACK_BELOW;
346 int focus_close_wrap = 1;
347 int focus_default = SWM_STACK_TOP;
348 int spawn_position = SWM_STACK_TOP;
349 int disable_border = 0;
350 int border_width = 1;
351 int verbose_layout = 0;
358 /* layout manager data */
359 struct swm_geometry {
372 struct swm_geometry g;
375 /* virtual "screens" */
377 TAILQ_ENTRY(swm_region) entry;
378 struct swm_geometry g;
379 struct workspace *ws; /* current workspace on this region */
380 struct workspace *ws_prior; /* prior workspace on this region */
381 struct swm_screen *s; /* screen idx */
384 TAILQ_HEAD(swm_region_list, swm_region);
387 TAILQ_ENTRY(ws_win) entry;
389 xcb_window_t transient;
390 struct ws_win *child_trans; /* transient child window */
391 struct swm_geometry g; /* current geometry */
392 struct swm_geometry g_float; /* region coordinates */
393 int g_floatvalid; /* g_float geometry validity */
394 int floatmaxed; /* whether maxed by max_stack */
399 unsigned int ewmh_flags;
400 int font_size_boundary[SWM_MAX_FONT_STEPS];
406 unsigned long quirks;
407 struct workspace *ws; /* always valid */
408 struct swm_screen *s; /* always valid, never changes */
409 xcb_get_geometry_reply_t *wa;
411 xcb_icccm_get_wm_class_reply_t ch;
412 xcb_icccm_wm_hints_t hints;
414 TAILQ_HEAD(ws_win_list, ws_win);
418 TAILQ_ENTRY(pid_e) entry;
422 TAILQ_HEAD(pid_list, pid_e);
423 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
425 /* layout handlers */
427 void vertical_config(struct workspace *, int);
428 void vertical_stack(struct workspace *, struct swm_geometry *);
429 void horizontal_config(struct workspace *, int);
430 void horizontal_stack(struct workspace *, struct swm_geometry *);
431 void max_stack(struct workspace *, struct swm_geometry *);
432 void plain_stacker(struct workspace *);
433 void fancy_stacker(struct workspace *);
436 void (*l_stack)(struct workspace *, struct swm_geometry *);
437 void (*l_config)(struct workspace *, int);
439 #define SWM_L_FOCUSPREV (1<<0)
440 #define SWM_L_MAPONFOCUS (1<<1)
441 void (*l_string)(struct workspace *);
443 /* stack, configure */
444 { vertical_stack, vertical_config, 0, plain_stacker },
445 { horizontal_stack, horizontal_config, 0, plain_stacker },
447 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
448 { NULL, NULL, 0, NULL },
451 /* position of max_stack mode in the layouts array, index into layouts! */
452 #define SWM_V_STACK (0)
453 #define SWM_H_STACK (1)
454 #define SWM_MAX_STACK (2)
456 #define SWM_H_SLICE (32)
457 #define SWM_V_SLICE (32)
459 /* define work spaces */
461 int idx; /* workspace index */
462 char *name; /* workspace name */
463 int always_raise; /* raise windows on focus */
464 struct layout *cur_layout; /* current layout handlers */
465 struct ws_win *focus; /* may be NULL */
466 struct ws_win *focus_prev; /* may be NULL */
467 struct swm_region *r; /* may be NULL */
468 struct swm_region *old_r; /* may be NULL */
469 struct ws_win_list winlist; /* list of windows in ws */
470 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
471 char stacker[10]; /* display stacker and layout */
475 int horizontal_msize;
477 int horizontal_stacks;
488 SWM_S_COLOR_BAR_BORDER,
489 SWM_S_COLOR_BAR_FONT,
495 /* physical screen mapping */
496 #define SWM_WS_MAX (22) /* hard limit */
497 int workspace_limit = 10; /* soft limit */
500 int idx; /* screen index */
501 struct swm_region_list rl; /* list of regions on this screen */
502 struct swm_region_list orl; /* list of old regions */
504 struct workspace ws[SWM_WS_MAX];
510 } c[SWM_S_COLOR_MAX];
512 xcb_gcontext_t bar_gc;
514 struct swm_screen *screens;
516 /* args to functions */
519 #define SWM_ARG_ID_FOCUSNEXT (0)
520 #define SWM_ARG_ID_FOCUSPREV (1)
521 #define SWM_ARG_ID_FOCUSMAIN (2)
522 #define SWM_ARG_ID_FOCUSCUR (4)
523 #define SWM_ARG_ID_SWAPNEXT (10)
524 #define SWM_ARG_ID_SWAPPREV (11)
525 #define SWM_ARG_ID_SWAPMAIN (12)
526 #define SWM_ARG_ID_MOVELAST (13)
527 #define SWM_ARG_ID_MASTERSHRINK (20)
528 #define SWM_ARG_ID_MASTERGROW (21)
529 #define SWM_ARG_ID_MASTERADD (22)
530 #define SWM_ARG_ID_MASTERDEL (23)
531 #define SWM_ARG_ID_FLIPLAYOUT (24)
532 #define SWM_ARG_ID_STACKRESET (30)
533 #define SWM_ARG_ID_STACKINIT (31)
534 #define SWM_ARG_ID_CYCLEWS_UP (40)
535 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
536 #define SWM_ARG_ID_CYCLESC_UP (42)
537 #define SWM_ARG_ID_CYCLESC_DOWN (43)
538 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
539 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
540 #define SWM_ARG_ID_STACKINC (50)
541 #define SWM_ARG_ID_STACKDEC (51)
542 #define SWM_ARG_ID_SS_ALL (60)
543 #define SWM_ARG_ID_SS_WINDOW (61)
544 #define SWM_ARG_ID_DONTCENTER (70)
545 #define SWM_ARG_ID_CENTER (71)
546 #define SWM_ARG_ID_KILLWINDOW (80)
547 #define SWM_ARG_ID_DELETEWINDOW (81)
548 #define SWM_ARG_ID_WIDTHGROW (90)
549 #define SWM_ARG_ID_WIDTHSHRINK (91)
550 #define SWM_ARG_ID_HEIGHTGROW (92)
551 #define SWM_ARG_ID_HEIGHTSHRINK (93)
552 #define SWM_ARG_ID_MOVEUP (100)
553 #define SWM_ARG_ID_MOVEDOWN (101)
554 #define SWM_ARG_ID_MOVELEFT (102)
555 #define SWM_ARG_ID_MOVERIGHT (103)
561 TAILQ_ENTRY(quirk) entry;
565 #define SWM_Q_FLOAT (1<<0) /* float this window */
566 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
567 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
568 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
569 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
570 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
572 TAILQ_HEAD(quirk_list, quirk);
573 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
576 * Supported EWMH hints should be added to
577 * both the enum and the ewmh array
582 _NET_MOVERESIZE_WINDOW,
583 _NET_WM_ACTION_CLOSE,
584 _NET_WM_ACTION_FULLSCREEN,
586 _NET_WM_ACTION_RESIZE,
587 _NET_WM_ALLOWED_ACTIONS,
590 _NET_WM_STATE_FULLSCREEN,
591 _NET_WM_STATE_HIDDEN,
592 _NET_WM_STATE_MAXIMIZED_HORZ,
593 _NET_WM_STATE_MAXIMIZED_VERT,
594 _NET_WM_STATE_SKIP_PAGER,
595 _NET_WM_STATE_SKIP_TASKBAR,
597 _NET_WM_WINDOW_TYPE_DIALOG,
598 _NET_WM_WINDOW_TYPE_DOCK,
599 _NET_WM_WINDOW_TYPE_NORMAL,
600 _NET_WM_WINDOW_TYPE_SPLASH,
601 _NET_WM_WINDOW_TYPE_TOOLBAR,
602 _NET_WM_WINDOW_TYPE_UTILITY,
603 _SWM_WM_STATE_MANUAL,
610 } ewmh[SWM_EWMH_HINT_MAX] = {
611 /* must be in same order as in the enum */
612 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
613 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
614 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
615 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
616 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
617 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
618 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
619 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
620 {"_NET_WM_STATE", XCB_ATOM_NONE},
621 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
622 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
623 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
624 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
625 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
626 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
627 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
628 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
629 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
630 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
631 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
632 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
633 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
634 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
635 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
638 /* function prototypes */
639 xcb_char2b_t *char2b(const char *);
640 int conf_load(char *, int);
641 void constrain_window(struct ws_win *, struct swm_region *, int);
643 void event_handle(xcb_generic_event_t *);
644 struct ws_win *find_window(xcb_window_t);
645 int floating_toggle_win(struct ws_win *);
646 void focus(struct swm_region *, union arg *);
647 void focus_magic(struct ws_win *);
648 xcb_atom_t get_atom_from_string(const char *);
649 xcb_screen_t *get_screen(int);
650 char *get_win_name(xcb_window_t);
651 uint16_t getstate(xcb_window_t);
652 void grabbuttons(struct ws_win *, int);
653 void map_window_raised(xcb_window_t);
654 void new_region(struct swm_screen *, int, int, int, int);
655 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
656 void spawn_select(struct swm_region *, union arg *, char *, int *);
657 void store_float_geom(struct ws_win *, struct swm_region *);
658 void unmanage_window(struct ws_win *);
659 void update_window(struct ws_win *);
661 char *get_atom_name(xcb_atom_t);
664 /* function definitions */
666 char2b(const char *str)
672 s = malloc(len * sizeof(xcb_char2b_t));
676 for (i = 0; i < len; i++) {
685 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
687 unsigned int tmpr, tmpg, tmpb;
689 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
700 get_screen(int screen)
702 xcb_screen_iterator_t i;
704 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
705 for (; i.rem; --screen, xcb_screen_next(&i))
715 xcb_get_input_focus_cookie_t c;
716 xcb_get_input_focus_reply_t *r;
719 c = xcb_get_input_focus(conn);
721 r = xcb_get_input_focus_reply(conn, c, NULL);
727 map_window_raised(xcb_window_t win)
729 uint32_t val = XCB_STACK_MODE_ABOVE;
731 xcb_configure_window(conn, win,
732 XCB_CONFIG_WINDOW_STACK_MODE, &val);
734 xcb_map_window(conn, win);
738 get_atom_from_string(const char *str)
740 xcb_intern_atom_cookie_t c;
741 xcb_intern_atom_reply_t *r;
744 c = xcb_intern_atom(conn, False, strlen(str), str);
745 r = xcb_intern_atom_reply(conn, c, NULL);
753 return (XCB_ATOM_NONE);
757 update_iconic(struct ws_win *win, int newv)
764 iprop = get_atom_from_string("_SWM_ICONIC");
765 if (iprop == XCB_ATOM_NONE)
769 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
770 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
772 xcb_delete_property(conn, win->id, iprop);
776 get_iconic(struct ws_win *win)
780 xcb_get_property_reply_t *pr = NULL;
782 iprop = get_atom_from_string("_SWM_ICONIC");
783 if (iprop == XCB_ATOM_NONE)
786 pr = xcb_get_property_reply(conn,
787 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
791 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
793 v = *((int32_t *)xcb_get_property_value(pr));
804 int i, j, num_screens;
806 sup_list = get_atom_from_string("_NET_SUPPORTED");
808 for (i = 0; i < LENGTH(ewmh); i++)
809 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
811 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
812 for (i = 0; i < num_screens; i++) {
813 /* Support check window will be created by workaround(). */
815 /* Report supported atoms */
816 xcb_delete_property(conn, screens[i].root, sup_list);
817 for (j = 0; j < LENGTH(ewmh); j++)
818 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
819 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
828 xcb_atom_t sup_check, sup_list;
830 xcb_get_property_cookie_t pc;
831 xcb_get_property_reply_t *pr;
833 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
834 sup_list = get_atom_from_string("_NET_SUPPORTED");
835 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
837 for (i = 0; i < num_screens; i++) {
838 /* Get the support check window and destroy it */
839 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
840 XCB_ATOM_WINDOW, 0, 1);
841 pr = xcb_get_property_reply(conn, pc, NULL);
843 id = *((xcb_window_t *)xcb_get_property_value(pr));
845 xcb_destroy_window(conn, id);
846 xcb_delete_property(conn, screens[i].root, sup_check);
847 xcb_delete_property(conn, screens[i].root, sup_list);
855 ewmh_autoquirk(struct ws_win *win)
859 xcb_get_property_cookie_t c;
860 xcb_get_property_reply_t *r;
862 c = xcb_get_property(conn, False, win->id,
863 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
864 r = xcb_get_property_reply(conn, c, NULL);
868 n = xcb_get_property_value_length(r);
869 type = xcb_get_property_value(r);
871 for (i = 0; i < n; i++) {
872 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
874 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
875 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
876 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
878 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
881 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
882 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
884 win->quirks = SWM_Q_FLOAT;
891 #define SWM_EWMH_ACTION_COUNT_MAX (6)
892 #define EWMH_F_FULLSCREEN (1<<0)
893 #define EWMH_F_ABOVE (1<<1)
894 #define EWMH_F_HIDDEN (1<<2)
895 #define EWMH_F_SKIP_PAGER (1<<3)
896 #define EWMH_F_SKIP_TASKBAR (1<<4)
897 #define SWM_F_MANUAL (1<<5)
900 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
908 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
909 "fullscreen %s\n", win->id, YESNO(fs));
912 if (!win->g_floatvalid)
913 store_float_geom(win, win->ws->r);
915 win->g = win->ws->r->g;
918 if (win->g_floatvalid) {
919 /* refloat at last floating relative position */
920 win->g = win->g_float;
921 X(win) += X(win->ws->r);
922 Y(win) += Y(win->ws->r);
930 ewmh_update_actions(struct ws_win *win)
932 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
938 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
941 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
942 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
945 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
946 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
949 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
950 #define _NET_WM_STATE_ADD 1 /* add/set property */
951 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
954 ewmh_update_win_state(struct ws_win *win, long state, long action)
956 unsigned int mask = 0;
957 unsigned int changed = 0;
958 unsigned int orig_flags;
963 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %d, "
964 "action: %d\n", win->id, state, action);
966 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
967 mask = EWMH_F_FULLSCREEN;
968 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
970 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
972 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
973 mask = EWMH_F_SKIP_PAGER;
974 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
975 mask = EWMH_F_SKIP_TASKBAR;
978 orig_flags = win->ewmh_flags;
981 case _NET_WM_STATE_REMOVE:
982 win->ewmh_flags &= ~mask;
984 case _NET_WM_STATE_ADD:
985 win->ewmh_flags |= mask;
987 case _NET_WM_STATE_TOGGLE:
988 win->ewmh_flags ^= mask;
992 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
994 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
996 if (!floating_toggle_win(win))
997 win->ewmh_flags = orig_flags; /* revert */
998 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1000 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1001 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1003 if (!ewmh_set_win_fullscreen(win,
1004 win->ewmh_flags & EWMH_F_FULLSCREEN))
1005 win->ewmh_flags = orig_flags; /* revert */
1007 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1009 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1010 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1011 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1012 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1013 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1014 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1015 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1016 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1017 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1018 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1019 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1020 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1021 if (win->ewmh_flags & EWMH_F_ABOVE)
1022 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1023 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1024 &ewmh[_NET_WM_STATE_ABOVE].atom);
1025 if (win->ewmh_flags & SWM_F_MANUAL)
1026 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1027 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1028 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1032 ewmh_get_win_state(struct ws_win *win)
1035 xcb_get_property_cookie_t c;
1036 xcb_get_property_reply_t *r;
1042 win->ewmh_flags = 0;
1044 win->ewmh_flags |= EWMH_F_ABOVE;
1046 win->ewmh_flags |= SWM_F_MANUAL;
1048 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1049 XCB_ATOM_ATOM, 0, UINT32_MAX);
1050 r = xcb_get_property_reply(conn, c, NULL);
1054 states = xcb_get_property_value(r);
1055 n = xcb_get_property_value_length(r);
1057 for (i = 0; i < n; i++)
1058 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1066 dumpwins(struct swm_region *r, union arg *args)
1070 xcb_get_window_attributes_cookie_t c;
1071 xcb_get_window_attributes_reply_t *wa;
1073 if (r->ws == NULL) {
1074 warnx("dumpwins: invalid workspace");
1078 warnx("=== managed window list ws %02d ===", r->ws->idx);
1079 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1080 state = getstate(win->id);
1081 c = xcb_get_window_attributes(conn, win->id);
1082 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1084 warnx("window: 0x%x, map_state: %d, state: %u, "
1085 "transient: 0x%x", win->id, wa->map_state,
1086 state, win->transient);
1089 warnx("window: 0x%x, failed xcb_get_window_attributes",
1093 warnx("===== unmanaged window list =====");
1094 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1095 state = getstate(win->id);
1096 c = xcb_get_window_attributes(conn, win->id);
1097 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1099 warnx("window: 0x%x, map_state: %d, state: %u, "
1100 "transient: 0x%x", win->id, wa->map_state,
1101 state, win->transient);
1104 warnx("window: 0x%x, failed xcb_get_window_attributes",
1108 warnx("=================================");
1112 dumpwins(struct swm_region *r, union arg *args)
1115 #endif /* SWM_DEBUG */
1117 void event_error(xcb_generic_error_t *);
1118 void expose(xcb_expose_event_t *);
1119 void keypress(xcb_key_press_event_t *);
1120 void buttonpress(xcb_button_press_event_t *);
1121 void configurerequest(xcb_configure_request_event_t *);
1122 void configurenotify(xcb_configure_notify_event_t *);
1123 void destroynotify(xcb_destroy_notify_event_t *);
1124 void enternotify(xcb_enter_notify_event_t *);
1125 void mapnotify(xcb_map_notify_event_t *);
1126 void mappingnotify(xcb_mapping_notify_event_t *);
1127 void maprequest(xcb_map_request_event_t *);
1128 void propertynotify(xcb_property_notify_event_t *);
1129 void unmapnotify(xcb_unmap_notify_event_t *);
1130 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1131 void clientmessage(xcb_client_message_event_t *);
1132 void screenchange(xcb_randr_screen_change_notify_event_t *);
1137 int saved_errno, status;
1140 saved_errno = errno;
1144 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1149 if (errno != ECHILD)
1150 warn("sighdlr: waitpid");
1151 #endif /* SWM_DEBUG */
1154 if (pid == searchpid)
1158 if (WIFEXITED(status)) {
1159 if (WEXITSTATUS(status) != 0)
1160 warnx("sighdlr: child exit status: %d",
1161 WEXITSTATUS(status));
1163 warnx("sighdlr: child is terminated "
1165 #endif /* SWM_DEBUG */
1179 errno = saved_errno;
1185 struct pid_e *p = NULL;
1187 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1192 TAILQ_FOREACH(p, &pidlist, entry) {
1201 name_to_color(const char *colorname)
1203 uint32_t result = 0;
1204 char cname[32] = "#";
1205 xcb_screen_t *screen;
1206 xcb_colormap_t cmap;
1207 xcb_alloc_color_reply_t *cr;
1208 xcb_alloc_named_color_reply_t *nr;
1209 uint16_t rr, gg, bb;
1211 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1212 cmap = screen->default_colormap;
1214 /* color is in format rgb://rr/gg/bb */
1215 if (strncmp(colorname, "rgb:", 4) == 0) {
1216 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1217 warnx("could not parse rgb %s", colorname);
1219 cr = xcb_alloc_color_reply(conn,
1220 xcb_alloc_color(conn, cmap, rr, gg, bb),
1226 warnx("color '%s' not found", colorname);
1229 nr = xcb_alloc_named_color_reply(conn,
1230 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1233 strlcat(cname, colorname + 2, sizeof cname - 1);
1234 nr = xcb_alloc_named_color_reply(conn,
1235 xcb_alloc_named_color(conn, cmap, strlen(cname),
1242 warnx("color '%s' not found", colorname);
1249 setscreencolor(char *val, int i, int c)
1253 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1254 if (i > 0 && i <= num_screens) {
1255 screens[i - 1].c[c].color = name_to_color(val);
1256 free(screens[i - 1].c[c].name);
1257 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1259 } else if (i == -1) {
1260 for (i = 0; i < num_screens; i++) {
1261 screens[i].c[c].color = name_to_color(val);
1262 free(screens[i].c[c].name);
1263 if ((screens[i].c[c].name = strdup(val)) == NULL)
1267 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1272 fancy_stacker(struct workspace *ws)
1274 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1275 if (ws->cur_layout->l_stack == vertical_stack)
1276 snprintf(ws->stacker, sizeof ws->stacker,
1277 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1278 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1279 if (ws->cur_layout->l_stack == horizontal_stack)
1280 snprintf(ws->stacker, sizeof ws->stacker,
1281 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1282 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1286 plain_stacker(struct workspace *ws)
1288 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1289 if (ws->cur_layout->l_stack == vertical_stack)
1290 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1291 sizeof ws->stacker);
1292 if (ws->cur_layout->l_stack == horizontal_stack)
1293 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1294 sizeof ws->stacker);
1298 custom_region(char *val)
1300 unsigned int x, y, w, h;
1301 int sidx, num_screens;
1302 xcb_screen_t *screen;
1304 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1305 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1306 errx(1, "invalid custom region, "
1307 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1308 if (sidx < 1 || sidx > num_screens)
1309 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1313 screen = get_screen(sidx);
1315 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1317 if (x > screen->width_in_pixels ||
1318 y > screen->height_in_pixels ||
1319 w + x > screen->width_in_pixels ||
1320 h + y > screen->height_in_pixels) {
1321 warnx("ignoring region %ux%u+%u+%u - not within screen "
1322 "boundaries (%ux%u)", w, h, x, y,
1323 screen->width_in_pixels, screen->height_in_pixels);
1327 new_region(&screens[sidx], x, y, w, h);
1331 socket_setnonblock(int fd)
1335 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1336 err(1, "fcntl F_GETFL");
1337 flags |= O_NONBLOCK;
1338 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1339 err(1, "fcntl F_SETFL");
1343 bar_print(struct swm_region *r, const char *s)
1346 xcb_rectangle_t rect;
1353 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1355 switch (bar_justify) {
1356 case SWM_BAR_JUSTIFY_LEFT:
1359 case SWM_BAR_JUSTIFY_CENTER:
1360 x = (WIDTH(r) - info.width) / 2;
1362 case SWM_BAR_JUSTIFY_RIGHT:
1363 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1367 if (x < SWM_BAR_OFFSET)
1372 rect.width = WIDTH(r->bar);
1373 rect.height = HEIGHT(r->bar);
1375 /* clear back buffer */
1376 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1377 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1378 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1379 sizeof(rect), &rect);
1381 /* draw back buffer */
1382 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1383 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
1384 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1385 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1387 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1388 bar_font->height, s);
1391 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1392 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1396 bar_extra_stop(void)
1400 bzero(bar_pipe, sizeof bar_pipe);
1403 kill(bar_pid, SIGTERM);
1406 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1411 bar_class_name(char *s, size_t sz, struct swm_region *r)
1413 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1415 if (r->ws->focus->ch.class_name != NULL)
1416 strlcat(s, r->ws->focus->ch.class_name, sz);
1420 bar_title_name(char *s, size_t sz, struct swm_region *r)
1422 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1424 if (r->ws->focus->ch.instance_name != NULL)
1425 strlcat(s, r->ws->focus->ch.instance_name, sz);
1429 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1431 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1434 bar_class_name(s, sz, r);
1435 strlcat(s, ":", sz);
1436 bar_title_name(s, sz, r);
1440 bar_window_float(char *s, size_t sz, struct swm_region *r)
1442 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1444 if (r->ws->focus->floating)
1445 strlcat(s, "(f)", sz);
1449 bar_window_name(char *s, size_t sz, struct swm_region *r)
1453 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1455 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1458 strlcat(s, title, sz);
1462 int urgent[SWM_WS_MAX];
1464 bar_urgent(char *s, size_t sz)
1467 int i, j, num_screens;
1469 xcb_get_property_cookie_t c;
1470 xcb_icccm_wm_hints_t hints;
1472 for (i = 0; i < workspace_limit; i++)
1475 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1476 for (i = 0; i < num_screens; i++)
1477 for (j = 0; j < workspace_limit; j++)
1478 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1479 c = xcb_icccm_get_wm_hints(conn, win->id);
1480 if (xcb_icccm_get_wm_hints_reply(conn, c,
1483 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1487 for (i = 0; i < workspace_limit; i++) {
1489 snprintf(b, sizeof b, "%d ", i + 1);
1491 snprintf(b, sizeof b, "- ");
1497 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1499 if (r == NULL || r->ws == NULL)
1501 if (r->ws->name != NULL)
1502 strlcat(s, r->ws->name, sz);
1505 /* build the default bar format according to the defined enabled options */
1507 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1509 /* if format provided, just copy the buffers */
1510 if (bar_format != NULL) {
1511 strlcpy(fmtnew, fmtexp, sz);
1515 /* reset the output buffer */
1518 strlcat(fmtnew, "+N:+I ", sz);
1520 strlcat(fmtnew, "+S", sz);
1521 strlcat(fmtnew, " ", sz);
1523 /* only show the workspace name if there's actually one */
1524 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1525 strlcat(fmtnew, "<+D>", sz);
1526 strlcat(fmtnew, "+3<", sz);
1528 if (clock_enabled) {
1529 strlcat(fmtnew, fmtexp, sz);
1530 strlcat(fmtnew, "+4<", sz);
1533 /* bar_urgent already adds the space before the last asterisk */
1535 strlcat(fmtnew, "* +U*+4<", sz);
1537 if (title_class_enabled) {
1538 strlcat(fmtnew, "+C", sz);
1539 if (title_name_enabled == 0)
1540 strlcat(fmtnew, "+4<", sz);
1543 /* checks needed by the colon and floating strlcat(3) calls below */
1544 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1545 if (title_name_enabled) {
1546 if (title_class_enabled)
1547 strlcat(fmtnew, ":", sz);
1548 strlcat(fmtnew, "+T+4<", sz);
1550 if (window_name_enabled) {
1551 if (r->ws->focus->floating)
1552 strlcat(fmtnew, "+F ", sz);
1553 strlcat(fmtnew, "+64W ", sz);
1557 /* finally add the action script output and the version */
1558 strlcat(fmtnew, "+4<+A+4<+V", sz);
1562 bar_replace_pad(char *tmp, int *limit, size_t sz)
1564 /* special case; no limit given, pad one space, instead */
1565 if (*limit == (int)sz - 1)
1567 snprintf(tmp, sz, "%*s", *limit, " ");
1570 /* replaces the bar format character sequences (like in tmux(1)) */
1572 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1576 char tmp[SWM_BAR_MAX];
1580 /* reset strlcat(3) buffer */
1583 /* get number, if any */
1586 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1587 limit = sizeof tmp - 1;
1588 if (limit <= 0 || limit >= (int)sizeof tmp)
1589 limit = sizeof tmp - 1;
1591 /* there is nothing to replace (ie EOL) */
1598 bar_replace_pad(tmp, &limit, sizeof tmp);
1601 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1604 bar_class_name(tmp, sizeof tmp, r);
1607 bar_workspace_name(tmp, sizeof tmp, r);
1610 bar_window_float(tmp, sizeof tmp, r);
1613 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1616 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1619 bar_class_title_name(tmp, sizeof tmp, r);
1622 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1625 bar_title_name(tmp, sizeof tmp, r);
1628 bar_urgent(tmp, sizeof tmp);
1631 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1634 bar_window_name(tmp, sizeof tmp, r);
1637 /* unknown character sequence; copy as-is */
1638 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1644 if ((int)len < limit)
1646 while (limit-- > 0) {
1647 if (*offrep >= sz - 1)
1649 fmtrep[(*offrep)++] = *ptr++;
1657 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1662 while (*fmt != '\0') {
1664 /* skip ordinary characters */
1667 fmtrep[off++] = *fmt++;
1671 /* character sequence found; replace it */
1672 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1681 bar_fmt_expand(char *fmtexp, size_t sz)
1688 /* start by grabbing the current time and date */
1690 localtime_r(&tmt, &tm);
1692 /* figure out what to expand */
1693 if (bar_format != NULL)
1695 else if (bar_format == NULL && clock_enabled)
1697 /* if nothing to expand bail out */
1703 /* copy as-is, just in case the format shouldn't be expanded below */
1704 strlcpy(fmtexp, fmt, sz);
1705 /* finally pass the string through strftime(3) */
1706 #ifndef SWM_DENY_CLOCK_FORMAT
1707 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1708 warnx("format too long");
1716 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1717 char fmtrep[SWM_BAR_MAX];
1719 struct swm_region *r;
1721 /* expand the format by first passing it through strftime(3) */
1722 bar_fmt_expand(fmtexp, sizeof fmtexp);
1724 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1725 for (i = 0; i < num_screens; i++) {
1726 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1729 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1730 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1731 bar_print(r, fmtrep);
1742 if (bar_enabled == 0)
1744 if (bar_extra && bar_extra_running) {
1745 /* ignore short reads; it'll correct itself */
1746 while ((b = fgetln(stdin, &len)) != NULL)
1747 if (b && b[len - 1] == '\n') {
1749 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1751 if (b == NULL && errno != EAGAIN) {
1752 warn("bar_update: bar_extra failed");
1756 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1769 bar_toggle(struct swm_region *r, union arg *args)
1771 struct swm_region *tmpr;
1774 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1776 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1778 for (i = 0; i < num_screens; i++)
1779 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1781 xcb_unmap_window(conn, tmpr->bar->id);
1783 for (i = 0; i < num_screens; i++)
1784 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1786 map_window_raised(tmpr->bar->id);
1789 bar_enabled = !bar_enabled;
1792 /* must be after stack */
1801 struct swm_region *r;
1805 /* do this here because the conf file is in memory */
1806 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1807 /* launch external status app */
1808 bar_extra_running = 1;
1809 if (pipe(bar_pipe) == -1)
1810 err(1, "pipe error");
1811 socket_setnonblock(bar_pipe[0]);
1812 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1813 if (dup2(bar_pipe[0], 0) == -1)
1815 if (dup2(bar_pipe[1], 1) == -1)
1817 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1818 err(1, "could not disable SIGPIPE");
1819 switch (bar_pid = fork()) {
1821 err(1, "cannot fork");
1825 execvp(bar_argv[0], bar_argv);
1826 err(1, "%s external app failed", bar_argv[0]);
1828 default: /* parent */
1834 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1835 for (i = 0; i < num_screens; i++)
1836 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1839 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1840 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1841 xcb_change_window_attributes(conn, r->bar->id,
1842 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1848 bar_setup(struct swm_region *r)
1851 xcb_screen_t *screen = get_screen(r->s->idx);
1855 XftFontClose(display, bar_font);
1859 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1860 err(1, "bar_setup: calloc: failed to allocate memory.");
1862 while ((font = strsep(&bar_fonts, " ,")) != NULL) {
1866 DNPRINTF(SWM_D_INIT, "bar_setup: try font %s\n", font);
1867 bar_font = XftFontOpenName(display, DefaultScreen(display),
1870 warnx("unable to load font %s", font);
1873 DNPRINTF(SWM_D_INIT, "successfully opened font %s\n",
1879 bar_height = bar_font->height + 4 * bar_border_width;
1885 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1886 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1887 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1889 r->bar->id = xcb_generate_id(conn);
1890 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
1891 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
1892 wa[2] = XCB_EVENT_MASK_EXPOSURE;
1893 DNPRINTF(SWM_D_BAR, "bar_setup: create_window: (x,y) w x h: (%d,%d) "
1894 "%d x %d\n", X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar));
1895 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
1896 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1897 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1898 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
1899 | XCB_CW_EVENT_MASK, wa);
1901 r->bar->buffer = xcb_generate_id(conn);
1902 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
1903 WIDTH(r->bar), HEIGHT(r->bar));
1905 xcb_randr_select_input(conn, r->bar->id,
1906 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1909 map_window_raised(r->bar->id);
1911 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1912 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1915 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1916 err(1, "could not install bar_signal");
1921 bar_cleanup(struct swm_region *r)
1925 xcb_destroy_window(conn, r->bar->id);
1926 xcb_free_pixmap(conn, r->bar->buffer);
1932 set_win_state(struct ws_win *win, uint16_t state)
1934 uint16_t data[2] = { state, XCB_ATOM_NONE };
1936 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
1941 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
1942 a_state, 32, 2, data);
1946 getstate(xcb_window_t w)
1948 uint16_t result = 0;
1949 xcb_get_property_cookie_t c;
1950 xcb_get_property_reply_t *r;
1952 c = xcb_get_property(conn, False, w, a_state, a_state, 0L, 2L);
1953 r = xcb_get_property_reply(conn, c, NULL);
1956 result = *((uint16_t *)xcb_get_property_value(r));
1960 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
1966 version(struct swm_region *r, union arg *args)
1968 bar_version = !bar_version;
1970 snprintf(bar_vertext, sizeof bar_vertext,
1971 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1973 strlcpy(bar_vertext, "", sizeof bar_vertext);
1980 client_msg(struct ws_win *win, xcb_atom_t a)
1982 xcb_client_message_event_t ev;
1987 bzero(&ev, sizeof ev);
1988 ev.response_type = XCB_CLIENT_MESSAGE;
1989 ev.window = win->id;
1992 ev.data.data32[0] = a;
1993 ev.data.data32[1] = XCB_CURRENT_TIME;
1995 xcb_send_event(conn, False, win->id,
1996 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
1999 /* synthetic response to a ConfigureRequest when not making a change */
2001 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2003 xcb_configure_notify_event_t ce;
2008 /* send notification of unchanged state. */
2009 bzero(&ce, sizeof(ce));
2010 ce.response_type = XCB_CONFIGURE_NOTIFY;
2013 ce.width = WIDTH(win);
2014 ce.height = HEIGHT(win);
2015 ce.override_redirect = False;
2020 ce.window = win->id;
2021 ce.border_width = BORDER(win);
2022 ce.above_sibling = XCB_WINDOW_NONE;
2025 ce.event = ev->window;
2026 ce.window = ev->window;
2028 /* make response appear more WM_SIZE_HINTS-compliant */
2030 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2031 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2032 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2033 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2034 SH_INC_W(win), SH_INC_H(win));
2038 /* the hint may be set... to 0! */
2039 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2040 ce.width = SH_MIN_W(win);
2041 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2042 ce.height = SH_MIN_H(win);
2047 /* may also be advertized as 0 */
2048 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2049 ce.width = SH_MAX_W(win);
2050 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2051 ce.height = SH_MAX_H(win);
2054 /* resize increment. */
2056 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2057 ce.width -= (ce.width - SH_MIN_W(win)) %
2059 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2060 ce.height -= (ce.height - SH_MIN_H(win)) %
2064 /* adjust x and y for requested border_width. */
2065 ce.x += BORDER(win) - ev->border_width;
2066 ce.y += BORDER(win) - ev->border_width;
2067 ce.border_width = ev->border_width;
2068 ce.above_sibling = ev->sibling;
2071 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2072 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2073 ce.y, ce.width, ce.height, ce.border_width);
2075 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2080 count_win(struct workspace *ws, int count_transient)
2085 TAILQ_FOREACH(win, &ws->winlist, entry) {
2086 if (count_transient == 0 && win->floating)
2088 if (count_transient == 0 && win->transient)
2094 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2100 quit(struct swm_region *r, union arg *args)
2102 DNPRINTF(SWM_D_MISC, "quit\n");
2107 unmap_window(struct ws_win *win)
2109 DNPRINTF(SWM_D_EVENT, "unmap_window: window: 0x%x\n", win->id);
2114 /* don't unmap again */
2115 if (getstate(win->id) == XCB_ICCCM_WM_STATE_WITHDRAWN)
2118 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
2120 xcb_unmap_window(conn, win->id);
2121 xcb_change_window_attributes(conn, win->id,
2122 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2129 int i, j, num_screens;
2131 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2132 for (i = 0; i < num_screens; i++)
2133 for (j = 0; j < workspace_limit; j++)
2134 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2139 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2141 xcb_key_press_event_t event;
2142 xcb_keycode_t *keycode;
2147 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2149 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2152 bzero(&event, sizeof(event));
2153 event.event = win->id;
2154 event.root = win->s->root;
2155 event.child = XCB_WINDOW_NONE;
2156 event.time = XCB_CURRENT_TIME;
2157 event.event_x = X(win);
2158 event.event_y = Y(win);
2161 event.same_screen = True;
2162 event.detail = *keycode;
2163 event.state = modifiers;
2165 event.response_type = XCB_KEY_PRESS;
2166 xcb_send_event(conn, True, win->id,
2167 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2169 event.response_type = XCB_KEY_RELEASE;
2170 xcb_send_event(conn, True, win->id,
2171 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2175 restart(struct swm_region *r, union arg *args)
2177 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2179 /* disable alarm because the following code may not be interrupted */
2181 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2182 err(1, "can't disable alarm");
2188 xcb_key_symbols_free(syms);
2190 xcb_disconnect(conn);
2192 execvp(start_argv[0], start_argv);
2193 warn("execvp failed");
2198 root_to_region(xcb_window_t root)
2200 struct swm_region *r = NULL;
2202 xcb_query_pointer_reply_t *qpr;
2204 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2206 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2207 for (i = 0; i < num_screens; i++)
2208 if (screens[i].root == root)
2211 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2212 screens[i].root), NULL);
2215 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2216 qpr->root_x, qpr->root_y);
2217 /* choose a region based on pointer location */
2218 TAILQ_FOREACH(r, &screens[i].rl, entry)
2219 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2220 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2226 r = TAILQ_FIRST(&screens[i].rl);
2232 find_unmanaged_window(xcb_window_t id)
2235 int i, j, num_screens;
2237 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2238 for (i = 0; i < num_screens; i++)
2239 for (j = 0; j < workspace_limit; j++)
2240 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2248 find_window(xcb_window_t id)
2251 int i, j, num_screens;
2252 xcb_query_tree_reply_t *r;
2254 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2255 for (i = 0; i < num_screens; i++)
2256 for (j = 0; j < workspace_limit; j++)
2257 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2261 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2265 /* if we were looking for the parent return that window instead */
2266 if (r->parent == 0 || r->root == r->parent)
2269 /* look for parent */
2270 for (i = 0; i < num_screens; i++)
2271 for (j = 0; j < workspace_limit; j++)
2272 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2273 if (r->parent == win->id) {
2283 spawn(int ws_idx, union arg *args, int close_fd)
2288 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2290 close(xcb_get_file_descriptor(conn));
2292 setenv("LD_PRELOAD", SWM_LIB, 1);
2294 if (asprintf(&ret, "%d", ws_idx) == -1) {
2295 warn("spawn: asprintf SWM_WS");
2298 setenv("_SWM_WS", ret, 1);
2302 if (asprintf(&ret, "%d", getpid()) == -1) {
2303 warn("spawn: asprintf _SWM_PID");
2306 setenv("_SWM_PID", ret, 1);
2310 if (setsid() == -1) {
2311 warn("spawn: setsid");
2317 * close stdin and stdout to prevent interaction between apps
2318 * and the baraction script
2319 * leave stderr open to record errors
2321 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2322 warn("spawn: open");
2325 dup2(fd, STDIN_FILENO);
2326 dup2(fd, STDOUT_FILENO);
2331 execvp(args->argv[0], args->argv);
2333 warn("spawn: execvp");
2338 kill_refs(struct ws_win *win)
2340 int i, x, num_screens;
2341 struct swm_region *r;
2342 struct workspace *ws;
2347 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2348 for (i = 0; i < num_screens; i++)
2349 TAILQ_FOREACH(r, &screens[i].rl, entry)
2350 for (x = 0; x < workspace_limit; x++) {
2352 if (win == ws->focus)
2354 if (win == ws->focus_prev)
2355 ws->focus_prev = NULL;
2360 validate_win(struct ws_win *testwin)
2363 struct workspace *ws;
2364 struct swm_region *r;
2365 int i, x, num_screens;
2367 if (testwin == NULL)
2370 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2371 for (i = 0; i < num_screens; i++)
2372 TAILQ_FOREACH(r, &screens[i].rl, entry)
2373 for (x = 0; x < workspace_limit; x++) {
2375 TAILQ_FOREACH(win, &ws->winlist, entry)
2383 validate_ws(struct workspace *testws)
2385 struct swm_region *r;
2386 struct workspace *ws;
2387 int i, x, num_screens;
2389 /* validate all ws */
2390 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2391 for (i = 0; i < num_screens; i++)
2392 TAILQ_FOREACH(r, &screens[i].rl, entry)
2393 for (x = 0; x < workspace_limit; x++) {
2402 unfocus_win(struct ws_win *win)
2404 xcb_window_t none = XCB_WINDOW_NONE;
2406 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2410 if (win->ws == NULL)
2413 if (validate_ws(win->ws))
2414 return; /* XXX this gets hit with thunderbird, needs fixing */
2416 if (win->ws->r == NULL)
2419 if (validate_win(win)) {
2424 if (win->ws->focus == win) {
2425 win->ws->focus = NULL;
2426 win->ws->focus_prev = win;
2429 if (validate_win(win->ws->focus)) {
2430 kill_refs(win->ws->focus);
2431 win->ws->focus = NULL;
2433 if (validate_win(win->ws->focus_prev)) {
2434 kill_refs(win->ws->focus_prev);
2435 win->ws->focus_prev = NULL;
2438 grabbuttons(win, 0);
2439 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2440 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2441 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2442 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2449 int i, j, num_screens;
2451 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2453 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2454 for (i = 0; i < num_screens; i++)
2455 for (j = 0; j < workspace_limit; j++)
2456 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2461 focus_win(struct ws_win *win)
2463 struct ws_win *cfw = NULL;
2464 xcb_get_input_focus_reply_t *r;
2465 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2467 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2471 if (win->ws == NULL)
2474 if (validate_ws(win->ws))
2475 return; /* XXX this gets hit with thunderbird, needs fixing */
2477 if (validate_win(win)) {
2482 if (validate_win(win)) {
2487 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2489 cur_focus = r->focus;
2492 if ((cfw = find_window(cur_focus)) != NULL)
2495 /* use larger hammer since the window was killed somehow */
2496 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2497 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2498 xcb_change_window_attributes(conn, cfw->id,
2499 XCB_CW_BORDER_PIXEL,
2500 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2503 win->ws->focus = win;
2505 if (win->ws->r != NULL) {
2507 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2508 win->id, XCB_CURRENT_TIME);
2509 grabbuttons(win, 1);
2510 xcb_change_window_attributes(conn, win->id,
2511 XCB_CW_BORDER_PIXEL,
2512 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2513 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2514 win->ws->always_raise)
2515 map_window_raised(win->id);
2517 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2518 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2526 event_drain(uint8_t rt)
2528 xcb_generic_event_t *evt;
2531 while ((evt = xcb_poll_for_event(conn))) {
2532 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2540 switchws(struct swm_region *r, union arg *args)
2542 int wsid = args->id, unmap_old = 0;
2543 struct swm_region *this_r, *other_r;
2545 struct workspace *new_ws, *old_ws;
2551 if (wsid >= workspace_limit)
2555 old_ws = this_r->ws;
2556 new_ws = &this_r->s->ws[wsid];
2558 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2559 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2561 if (new_ws == NULL || old_ws == NULL)
2563 if (new_ws == old_ws)
2566 other_r = new_ws->r;
2567 if (other_r == NULL) {
2568 /* the other workspace is hidden, hide this one */
2572 /* the other ws is visible in another region, exchange them */
2573 other_r->ws_prior = new_ws;
2574 other_r->ws = old_ws;
2575 old_ws->r = other_r;
2577 this_r->ws_prior = old_ws;
2578 this_r->ws = new_ws;
2581 /* this is needed so that we can click on a window after a restart */
2585 a.id = SWM_ARG_ID_FOCUSCUR;
2586 focus(new_ws->r, &a);
2588 /* unmap old windows */
2590 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2593 if (focus_mode == SWM_FOCUS_DEFAULT)
2594 event_drain(XCB_ENTER_NOTIFY);
2598 DNPRINTF(SWM_D_WS, "switchws: done\n");
2602 cyclews(struct swm_region *r, union arg *args)
2605 struct swm_screen *s = r->s;
2608 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2609 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2614 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2617 case SWM_ARG_ID_CYCLEWS_UP:
2618 if (a.id < workspace_limit - 1)
2623 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2626 case SWM_ARG_ID_CYCLEWS_DOWN:
2630 a.id = workspace_limit - 1;
2637 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2639 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2643 } while (a.id != r->ws->idx);
2647 priorws(struct swm_region *r, union arg *args)
2651 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2652 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2654 if (r->ws_prior == NULL)
2657 a.id = r->ws_prior->idx;
2662 cyclescr(struct swm_region *r, union arg *args)
2664 struct swm_region *rr = NULL;
2666 int i, x, y, num_screens;
2668 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2669 /* do nothing if we don't have more than one screen */
2670 if (!(num_screens > 1 || outputs > 1))
2675 case SWM_ARG_ID_CYCLESC_UP:
2676 rr = TAILQ_NEXT(r, entry);
2678 rr = TAILQ_FIRST(&screens[i].rl);
2680 case SWM_ARG_ID_CYCLESC_DOWN:
2681 rr = TAILQ_PREV(r, swm_region_list, entry);
2683 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2691 /* move mouse to region */
2693 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2694 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2697 a.id = SWM_ARG_ID_FOCUSCUR;
2700 if (rr->ws->focus) {
2701 /* move to focus window */
2702 x = X(rr->ws->focus) + 1;
2703 y = Y(rr->ws->focus) + 1;
2704 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2710 sort_windows(struct ws_win_list *wl)
2712 struct ws_win *win, *parent, *nxt;
2717 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2718 nxt = TAILQ_NEXT(win, entry);
2719 if (win->transient) {
2720 parent = find_window(win->transient);
2721 if (parent == NULL) {
2722 warnx("not possible bug");
2725 TAILQ_REMOVE(wl, win, entry);
2726 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2733 swapwin(struct swm_region *r, union arg *args)
2735 struct ws_win *target, *source;
2736 struct ws_win *cur_focus;
2737 struct ws_win_list *wl;
2740 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2741 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2743 cur_focus = r->ws->focus;
2744 if (cur_focus == NULL)
2748 wl = &source->ws->winlist;
2751 case SWM_ARG_ID_SWAPPREV:
2752 if (source->transient)
2753 source = find_window(source->transient);
2754 target = TAILQ_PREV(source, ws_win_list, entry);
2755 if (target && target->transient)
2756 target = find_window(target->transient);
2757 TAILQ_REMOVE(wl, source, entry);
2759 TAILQ_INSERT_TAIL(wl, source, entry);
2761 TAILQ_INSERT_BEFORE(target, source, entry);
2763 case SWM_ARG_ID_SWAPNEXT:
2764 target = TAILQ_NEXT(source, entry);
2765 /* move the parent and let the sort handle the move */
2766 if (source->transient)
2767 source = find_window(source->transient);
2768 TAILQ_REMOVE(wl, source, entry);
2770 TAILQ_INSERT_HEAD(wl, source, entry);
2772 TAILQ_INSERT_AFTER(wl, target, source, entry);
2774 case SWM_ARG_ID_SWAPMAIN:
2775 target = TAILQ_FIRST(wl);
2776 if (target == source) {
2777 if (source->ws->focus_prev != NULL &&
2778 source->ws->focus_prev != target)
2779 source = source->ws->focus_prev;
2783 if (target == NULL || source == NULL)
2785 source->ws->focus_prev = target;
2786 TAILQ_REMOVE(wl, target, entry);
2787 TAILQ_INSERT_BEFORE(source, target, entry);
2788 TAILQ_REMOVE(wl, source, entry);
2789 TAILQ_INSERT_HEAD(wl, source, entry);
2791 case SWM_ARG_ID_MOVELAST:
2792 TAILQ_REMOVE(wl, source, entry);
2793 TAILQ_INSERT_TAIL(wl, source, entry);
2796 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2808 focus_prev(struct ws_win *win)
2810 struct ws_win *winfocus = NULL;
2811 struct ws_win *cur_focus = NULL;
2812 struct ws_win_list *wl = NULL;
2813 struct workspace *ws = NULL;
2815 if (!(win && win->ws))
2820 cur_focus = ws->focus;
2822 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2823 WINID(win), WINID(cur_focus));
2825 /* pickle, just focus on whatever */
2826 if (cur_focus == NULL) {
2827 /* use prev_focus if valid */
2828 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2829 find_window(WINID(ws->focus_prev)))
2830 winfocus = ws->focus_prev;
2834 /* if transient focus on parent */
2835 if (cur_focus->transient) {
2836 winfocus = find_window(cur_focus->transient);
2840 /* if in max_stack try harder */
2841 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2842 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2843 if (cur_focus != ws->focus_prev)
2844 winfocus = ws->focus_prev;
2846 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2851 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2853 if (winfocus == NULL || winfocus == win) {
2854 switch (focus_close) {
2855 case SWM_STACK_BOTTOM:
2856 winfocus = TAILQ_FIRST(wl);
2859 winfocus = TAILQ_LAST(wl, ws_win_list);
2861 case SWM_STACK_ABOVE:
2862 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2863 if (focus_close_wrap)
2864 winfocus = TAILQ_FIRST(wl);
2866 winfocus = TAILQ_PREV(cur_focus,
2867 ws_win_list, entry);
2870 case SWM_STACK_BELOW:
2871 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2873 if (focus_close_wrap)
2874 winfocus = TAILQ_LAST(wl, ws_win_list);
2876 winfocus = TAILQ_NEXT(cur_focus, entry);
2882 if (winfocus == NULL) {
2883 if (focus_default == SWM_STACK_TOP)
2884 winfocus = TAILQ_LAST(wl, ws_win_list);
2886 winfocus = TAILQ_FIRST(wl);
2890 focus_magic(winfocus);
2894 focus(struct swm_region *r, union arg *args)
2896 struct ws_win *winfocus = NULL, *head;
2897 struct ws_win *cur_focus = NULL;
2898 struct ws_win_list *wl = NULL;
2899 struct workspace *ws = NULL;
2905 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2907 /* treat FOCUS_CUR special */
2908 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2909 if (r->ws->focus && r->ws->focus->iconic == 0)
2910 winfocus = r->ws->focus;
2911 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2912 winfocus = r->ws->focus_prev;
2914 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2915 if (winfocus->iconic == 0)
2918 focus_magic(winfocus);
2922 if ((cur_focus = r->ws->focus) == NULL)
2926 if (TAILQ_EMPTY(wl))
2928 /* make sure there is at least one uniconified window */
2930 TAILQ_FOREACH(winfocus, wl, entry)
2931 if (winfocus->iconic == 0) {
2939 case SWM_ARG_ID_FOCUSPREV:
2940 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2942 head = TAILQ_LAST(wl, ws_win_list);
2944 if (WINID(winfocus) == cur_focus->transient) {
2945 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2947 head = TAILQ_LAST(wl, ws_win_list);
2952 if (winfocus && winfocus->iconic) {
2953 while (winfocus != cur_focus) {
2954 if (winfocus == NULL)
2955 winfocus = TAILQ_LAST(wl, ws_win_list);
2956 if (winfocus->iconic == 0)
2958 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2964 case SWM_ARG_ID_FOCUSNEXT:
2965 head = TAILQ_NEXT(cur_focus, entry);
2967 head = TAILQ_FIRST(wl);
2971 if (winfocus && winfocus->iconic) {
2972 while (winfocus != cur_focus) {
2973 if (winfocus == NULL)
2974 winfocus = TAILQ_FIRST(wl);
2975 if (winfocus->iconic == 0)
2977 winfocus = TAILQ_NEXT(winfocus, entry);
2982 case SWM_ARG_ID_FOCUSMAIN:
2983 winfocus = TAILQ_FIRST(wl);
2984 if (winfocus == cur_focus)
2985 winfocus = cur_focus->ws->focus_prev;
2992 focus_magic(winfocus);
2998 cycle_layout(struct swm_region *r, union arg *args)
3000 struct workspace *ws = r->ws;
3003 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3006 if (ws->cur_layout->l_stack == NULL)
3007 ws->cur_layout = &layouts[0];
3012 if (focus_mode == SWM_FOCUS_DEFAULT)
3013 event_drain(XCB_ENTER_NOTIFY);
3015 a.id = SWM_ARG_ID_FOCUSCUR;
3020 stack_config(struct swm_region *r, union arg *args)
3022 struct workspace *ws = r->ws;
3024 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3027 if (ws->cur_layout->l_config != NULL)
3028 ws->cur_layout->l_config(ws, args->id);
3030 if (args->id != SWM_ARG_ID_STACKINIT)
3037 struct swm_geometry g;
3038 struct swm_region *r;
3044 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3046 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3047 for (i = 0; i < num_screens; i++) {
3051 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3052 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3053 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3055 /* start with screen geometry, adjust for bar */
3057 g.w -= 2 * border_width;
3058 g.h -= 2 * border_width;
3064 r->ws->cur_layout->l_stack(r->ws, &g);
3065 r->ws->cur_layout->l_string(r->ws);
3066 /* save r so we can track region changes */
3073 if (focus_mode == SWM_FOCUS_DEFAULT)
3074 event_drain(XCB_ENTER_NOTIFY);
3076 DNPRINTF(SWM_D_STACK, "stack: end\n");
3080 store_float_geom(struct ws_win *win, struct swm_region *r)
3082 /* retain window geom and region geom */
3083 win->g_float = win->g;
3084 win->g_float.x -= X(r);
3085 win->g_float.y -= Y(r);
3086 win->g_floatvalid = 1;
3087 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3088 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3089 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3090 win->g_float.w, win->g_float.h);
3094 stack_floater(struct ws_win *win, struct swm_region *r)
3099 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3102 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3103 * geom on ws switches or return from max mode
3105 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3106 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3107 /* refloat at last floating relative position */
3108 win->g = win->g_float;
3113 win->floatmaxed = 0;
3116 * if set to fullscreen mode, configure window to maximum size.
3118 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3119 if (!win->g_floatvalid)
3120 store_float_geom(win, win->ws->r);
3122 win->g = win->ws->r->g;
3126 * remove border on fullscreen floater when in fullscreen mode or when
3127 * the quirk is present.
3129 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3130 ((win->quirks & SWM_Q_FULLSCREEN) &&
3131 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3132 if (win->bordered) {
3134 X(win) += border_width;
3135 Y(win) += border_width;
3137 } else if (!win->bordered) {
3139 X(win) -= border_width;
3140 Y(win) -= border_width;
3143 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3144 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3145 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3150 * floaters and transients are auto-centred unless moved
3153 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3154 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3157 /* keep window within region bounds */
3158 constrain_window(win, r, 0);
3164 * Send keystrokes to terminal to decrease/increase the font size as the
3165 * window size changes.
3168 adjust_font(struct ws_win *win)
3170 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3171 win->floating || win->transient)
3174 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3175 WIDTH(win) / win->sh.width_inc < term_width &&
3176 win->font_steps < SWM_MAX_FONT_STEPS) {
3177 win->font_size_boundary[win->font_steps] =
3178 (win->sh.width_inc * term_width) + win->sh.base_width;
3181 win->last_inc = win->sh.width_inc;
3182 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3183 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3184 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3187 win->last_inc = win->sh.width_inc;
3188 fake_keypress(win, XK_KP_Add, ShiftMask);
3192 #define SWAPXY(g) do { \
3194 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3195 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3198 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3200 struct swm_geometry win_g, r_g = *g;
3201 struct ws_win *win, *fs_win = NULL;
3202 int i, j, s, stacks;
3203 int w_inc = 1, h_inc, w_base = 1, h_base;
3204 int hrh, extra = 0, h_slice, last_h = 0;
3205 int split, colno, winno, mwin, msize, mscale;
3206 int remain, missing, v_slice, reconfigure;
3208 xcb_get_window_attributes_reply_t *war;
3210 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3211 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3213 winno = count_win(ws, 0);
3214 if (winno == 0 && count_win(ws, 1) == 0)
3217 TAILQ_FOREACH(win, &ws->winlist, entry)
3218 if (win->transient == 0 && win->floating == 0
3219 && win->iconic == 0)
3226 w_inc = win->sh.width_inc;
3227 w_base = win->sh.base_width;
3228 mwin = ws->l_state.horizontal_mwin;
3229 mscale = ws->l_state.horizontal_msize;
3230 stacks = ws->l_state.horizontal_stacks;
3233 w_inc = win->sh.height_inc;
3234 w_base = win->sh.base_height;
3235 mwin = ws->l_state.vertical_mwin;
3236 mscale = ws->l_state.vertical_msize;
3237 stacks = ws->l_state.vertical_stacks;
3241 if (stacks > winno - mwin)
3242 stacks = winno - mwin;
3246 h_slice = r_g.h / SWM_H_SLICE;
3247 if (mwin && winno > mwin) {
3248 v_slice = r_g.w / SWM_V_SLICE;
3252 win_g.w = v_slice * mscale;
3254 if (w_inc > 1 && w_inc < v_slice) {
3255 /* adjust for window's requested size increment */
3256 remain = (win_g.w - w_base) % w_inc;
3262 win_g.x += r_g.w - msize;
3265 colno = split = winno / stacks;
3266 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3267 2 * border_width) / stacks);
3269 hrh = r_g.h / colno;
3270 extra = r_g.h - (colno * hrh);
3271 win_g.h = hrh - 2 * border_width;
3273 /* stack all the tiled windows */
3274 i = j = 0, s = stacks;
3275 TAILQ_FOREACH(win, &ws->winlist, entry) {
3276 if (win->transient != 0 || win->floating != 0)
3278 if (win->iconic != 0)
3281 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3286 if (split && i == split) {
3287 colno = (winno - mwin) / stacks;
3288 if (s <= (winno - mwin) % stacks)
3290 split = split + colno;
3291 hrh = (r_g.h / colno);
3292 extra = r_g.h - (colno * hrh);
3296 win_g.x += win_g.w + 2 * border_width;
3297 win_g.w = (r_g.w - msize -
3298 (stacks * 2 * border_width)) / stacks;
3300 win_g.w += (r_g.w - msize -
3301 (stacks * 2 * border_width)) % stacks;
3305 win_g.h = hrh - 2 * border_width;
3307 h_inc = win->sh.width_inc;
3308 h_base = win->sh.base_width;
3310 h_inc = win->sh.height_inc;
3311 h_base = win->sh.base_height;
3313 if (j == colno - 1) {
3314 win_g.h = hrh + extra;
3315 } else if (h_inc > 1 && h_inc < h_slice) {
3316 /* adjust for window's requested size increment */
3317 remain = (win_g.h - h_base) % h_inc;
3318 missing = h_inc - remain;
3320 if (missing <= extra || j == 0) {
3332 win_g.y += last_h + 2 * border_width;
3334 if (disable_border && bar_enabled == 0 && winno == 1){
3336 win_g.w += 2 * border_width;
3337 win_g.h += 2 * border_width;
3342 if (X(win) != win_g.y || Y(win) != win_g.x ||
3343 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3347 WIDTH(win) = win_g.h;
3348 HEIGHT(win) = win_g.w;
3351 if (X(win) != win_g.x || Y(win) != win_g.y ||
3352 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3356 WIDTH(win) = win_g.w;
3357 HEIGHT(win) = win_g.h;
3361 if (bordered != win->bordered) {
3363 win->bordered = bordered;
3371 war = xcb_get_window_attributes_reply(conn,
3372 xcb_get_window_attributes(conn, win->id),
3375 if (war->map_state == XCB_MAP_STATE_UNMAPPED)
3376 map_window_raised(win->id);
3386 /* now, stack all the floaters and transients */
3387 TAILQ_FOREACH(win, &ws->winlist, entry) {
3388 if (win->transient == 0 && win->floating == 0)
3390 if (win->iconic == 1)
3392 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3397 stack_floater(win, ws->r);
3398 map_window_raised(win->id);
3402 stack_floater(fs_win, ws->r);
3403 map_window_raised(fs_win->id);
3408 vertical_config(struct workspace *ws, int id)
3410 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3414 case SWM_ARG_ID_STACKRESET:
3415 case SWM_ARG_ID_STACKINIT:
3416 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3417 ws->l_state.vertical_mwin = 1;
3418 ws->l_state.vertical_stacks = 1;
3420 case SWM_ARG_ID_MASTERSHRINK:
3421 if (ws->l_state.vertical_msize > 1)
3422 ws->l_state.vertical_msize--;
3424 case SWM_ARG_ID_MASTERGROW:
3425 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3426 ws->l_state.vertical_msize++;
3428 case SWM_ARG_ID_MASTERADD:
3429 ws->l_state.vertical_mwin++;
3431 case SWM_ARG_ID_MASTERDEL:
3432 if (ws->l_state.vertical_mwin > 0)
3433 ws->l_state.vertical_mwin--;
3435 case SWM_ARG_ID_STACKINC:
3436 ws->l_state.vertical_stacks++;
3438 case SWM_ARG_ID_STACKDEC:
3439 if (ws->l_state.vertical_stacks > 1)
3440 ws->l_state.vertical_stacks--;
3442 case SWM_ARG_ID_FLIPLAYOUT:
3443 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3451 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3453 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3455 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3459 horizontal_config(struct workspace *ws, int id)
3461 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3464 case SWM_ARG_ID_STACKRESET:
3465 case SWM_ARG_ID_STACKINIT:
3466 ws->l_state.horizontal_mwin = 1;
3467 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3468 ws->l_state.horizontal_stacks = 1;
3470 case SWM_ARG_ID_MASTERSHRINK:
3471 if (ws->l_state.horizontal_msize > 1)
3472 ws->l_state.horizontal_msize--;
3474 case SWM_ARG_ID_MASTERGROW:
3475 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3476 ws->l_state.horizontal_msize++;
3478 case SWM_ARG_ID_MASTERADD:
3479 ws->l_state.horizontal_mwin++;
3481 case SWM_ARG_ID_MASTERDEL:
3482 if (ws->l_state.horizontal_mwin > 0)
3483 ws->l_state.horizontal_mwin--;
3485 case SWM_ARG_ID_STACKINC:
3486 ws->l_state.horizontal_stacks++;
3488 case SWM_ARG_ID_STACKDEC:
3489 if (ws->l_state.horizontal_stacks > 1)
3490 ws->l_state.horizontal_stacks--;
3492 case SWM_ARG_ID_FLIPLAYOUT:
3493 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3501 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3503 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3505 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3508 /* fullscreen view */
3510 max_stack(struct workspace *ws, struct swm_geometry *g)
3512 struct swm_geometry gg = *g;
3513 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3514 int winno, num_screens;
3516 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3521 winno = count_win(ws, 0);
3522 if (winno == 0 && count_win(ws, 1) == 0)
3525 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3526 TAILQ_FOREACH(win, &ws->winlist, entry) {
3527 if (win->transient) {
3529 parent = find_window(win->transient);
3533 if (win->floating && win->floatmaxed == 0 ) {
3535 * retain geometry for retrieval on exit from
3538 store_float_geom(win, ws->r);
3539 win->floatmaxed = 1;
3542 /* only reconfigure if necessary */
3543 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3544 HEIGHT(win) != gg.h) {
3550 WIDTH(win) += 2 * border_width;
3551 HEIGHT(win) += 2 * border_width;
3556 /* unmap only if we don't have multi screen */
3557 if (win != ws->focus)
3558 if (!(num_screens > 1 || outputs > 1))
3562 /* put the last transient on top */
3565 map_window_raised(parent->id);
3566 stack_floater(wintrans, ws->r);
3567 focus_magic(wintrans);
3572 send_to_ws(struct swm_region *r, union arg *args)
3574 int wsid = args->id;
3575 struct ws_win *win = NULL, *parent;
3576 struct workspace *ws, *nws;
3577 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3578 char ws_idx_str[SWM_PROPLEN];
3581 if (wsid >= workspace_limit)
3584 if (r && r->ws && r->ws->focus)
3590 if (win->ws->idx == wsid)
3593 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3596 nws = &win->s->ws[wsid];
3598 a.id = SWM_ARG_ID_FOCUSPREV;
3600 if (win->transient) {
3601 parent = find_window(win->transient);
3603 unmap_window(parent);
3604 TAILQ_REMOVE(&ws->winlist, parent, entry);
3605 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3610 TAILQ_REMOVE(&ws->winlist, win, entry);
3611 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3612 if (TAILQ_EMPTY(&ws->winlist))
3613 r->ws->focus = NULL;
3616 /* Try to update the window's workspace property */
3617 ws_idx_atom = get_atom_from_string("_SWM_WS");
3619 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3621 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3623 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3624 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3635 pressbutton(struct swm_region *r, union arg *args)
3637 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3638 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3639 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3640 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3644 raise_toggle(struct swm_region *r, union arg *args)
3646 if (r == NULL || r->ws == NULL)
3649 r->ws->always_raise = !r->ws->always_raise;
3651 /* bring floaters back to top */
3652 if (r->ws->always_raise == 0)
3659 iconify(struct swm_region *r, union arg *args)
3663 if (r->ws->focus == NULL)
3665 unmap_window(r->ws->focus);
3666 update_iconic(r->ws->focus, 1);
3669 r->ws->focus = NULL;
3670 a.id = SWM_ARG_ID_FOCUSCUR;
3677 get_win_name(xcb_window_t win)
3680 xcb_get_property_cookie_t c;
3681 xcb_icccm_get_text_property_reply_t r;
3683 c = xcb_icccm_get_wm_name(conn, win);
3684 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3685 if (r.name_len > 0) {
3686 name = malloc(r.name_len + 1);
3688 xcb_icccm_get_text_property_reply_wipe(&r);
3691 memcpy(name, r.name, r.name_len);
3692 name[r.name_len] = '\0';
3694 xcb_icccm_get_text_property_reply_wipe(&r);
3701 uniconify(struct swm_region *r, union arg *args)
3708 DNPRINTF(SWM_D_MISC, "uniconify\n");
3710 if (r == NULL || r->ws == NULL)
3713 /* make sure we have anything to uniconify */
3714 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3715 if (win->ws == NULL)
3716 continue; /* should never happen */
3717 if (win->iconic == 0)
3725 search_resp_action = SWM_SEARCH_UNICONIFY;
3727 spawn_select(r, args, "search", &searchpid);
3729 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3732 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3733 if (win->ws == NULL)
3734 continue; /* should never happen */
3735 if (win->iconic == 0)
3738 name = get_win_name(win->id);
3741 fprintf(lfile, "%s.%u\n", name, win->id);
3751 name_workspace(struct swm_region *r, union arg *args)
3755 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3761 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3763 spawn_select(r, args, "name_workspace", &searchpid);
3765 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3768 fprintf(lfile, "%s", "");
3773 search_workspace(struct swm_region *r, union arg *args)
3776 struct workspace *ws;
3779 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3785 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3787 spawn_select(r, args, "search", &searchpid);
3789 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3792 for (i = 0; i < workspace_limit; i++) {
3796 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3797 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3804 search_win_cleanup(void)
3806 struct search_window *sw = NULL;
3808 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3809 xcb_destroy_window(conn, sw->indicator);
3810 xcb_free_gc(conn, sw->gc);
3811 TAILQ_REMOVE(&search_wl, sw, entry);
3817 search_win(struct swm_region *r, union arg *args)
3819 struct ws_win *win = NULL;
3820 struct search_window *sw = NULL;
3822 uint32_t gcv[3], wa[2];
3829 DNPRINTF(SWM_D_MISC, "search_win\n");
3832 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3834 spawn_select(r, args, "search", &searchpid);
3836 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3839 TAILQ_INIT(&search_wl);
3842 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3843 if (win->iconic == 1)
3846 sw = calloc(1, sizeof(struct search_window));
3848 warn("search_win: calloc");
3850 search_win_cleanup();
3856 snprintf(s, sizeof s, "%d", i);
3859 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
3861 w = xcb_generate_id(conn);
3862 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3863 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3864 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
3865 info.width + 4, bar_font->height + 4,
3866 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
3867 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3870 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3872 sw->gc = xcb_generate_id(conn);
3873 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3874 gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
3876 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
3877 XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
3878 map_window_raised(w);
3880 xcb_image_text_8(conn, len, w, sw->gc, 2, bar_font->height, s);
3882 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
3884 fprintf(lfile, "%d\n", i);
3894 search_resp_uniconify(char *resp, unsigned long len)
3900 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3902 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3903 if (win->iconic == 0)
3905 name = get_win_name(win->id);
3908 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3913 if (strncmp(s, resp, len) == 0) {
3914 /* XXX this should be a callback to generalize */
3915 update_iconic(win, 0);
3924 search_resp_name_workspace(char *resp, unsigned long len)
3926 struct workspace *ws;
3928 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3930 if (search_r->ws == NULL)
3935 free(search_r->ws->name);
3936 search_r->ws->name = NULL;
3940 ws->name = strdup(resp);
3941 if (ws->name == NULL) {
3942 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3943 "strdup: %s", strerror(errno));
3950 search_resp_search_workspace(char *resp, unsigned long len)
3957 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3961 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3968 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
3970 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3977 switchws(search_r, &a);
3981 search_resp_search_window(char *resp, unsigned long len)
3986 struct search_window *sw;
3988 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3992 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3997 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
3999 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4006 TAILQ_FOREACH(sw, &search_wl, entry)
4007 if (idx == sw->idx) {
4013 #define MAX_RESP_LEN 1024
4016 search_do_resp(void)
4022 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4027 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4028 warn("search: calloc");
4032 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4034 warn("search: read error");
4037 resp[rbytes] = '\0';
4040 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4041 * newline, so work around that by sanitizing the resp now.
4043 resp[strcspn(resp, "\n")] = '\0';
4046 switch (search_resp_action) {
4047 case SWM_SEARCH_UNICONIFY:
4048 search_resp_uniconify(resp, len);
4050 case SWM_SEARCH_NAME_WORKSPACE:
4051 search_resp_name_workspace(resp, len);
4053 case SWM_SEARCH_SEARCH_WORKSPACE:
4054 search_resp_search_workspace(resp, len);
4056 case SWM_SEARCH_SEARCH_WINDOW:
4057 search_resp_search_window(resp, len);
4062 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4063 search_win_cleanup();
4065 search_resp_action = SWM_SEARCH_NONE;
4066 close(select_resp_pipe[0]);
4071 wkill(struct swm_region *r, union arg *args)
4073 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4075 if (r->ws->focus == NULL)
4078 if (args->id == SWM_ARG_ID_KILLWINDOW)
4079 xcb_kill_client(conn, r->ws->focus->id);
4081 if (r->ws->focus->can_delete)
4082 client_msg(r->ws->focus, a_delete);
4089 floating_toggle_win(struct ws_win *win)
4091 struct swm_region *r;
4101 /* reject floating toggles in max stack mode */
4102 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4105 if (win->floating) {
4106 if (!win->floatmaxed) {
4107 /* retain position for refloat */
4108 store_float_geom(win, r);
4112 if (win->g_floatvalid) {
4113 /* refloat at last floating relative position */
4114 X(win) = win->g_float.x + X(r);
4115 Y(win) = win->g_float.y + Y(r);
4116 WIDTH(win) = win->g_float.w;
4117 HEIGHT(win) = win->g_float.h;
4122 ewmh_update_actions(win);
4128 floating_toggle(struct swm_region *r, union arg *args)
4130 struct ws_win *win = r->ws->focus;
4136 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4137 _NET_WM_STATE_TOGGLE);
4141 if (win == win->ws->focus) {
4142 a.id = SWM_ARG_ID_FOCUSCUR;
4143 focus(win->ws->r, &a);
4147 event_drain(XCB_ENTER_NOTIFY);
4151 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4153 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4155 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4157 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4160 if (X(win) + BORDER(win) < X(r)) {
4162 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4164 X(win) = X(r) - BORDER(win);
4167 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4169 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4171 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4174 if (Y(win) + BORDER(win) < Y(r)) {
4176 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4178 Y(win) = Y(r) - BORDER(win);
4184 if (HEIGHT(win) < 1)
4190 update_window(struct ws_win *win)
4195 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4196 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4197 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4201 wc[3] = HEIGHT(win);
4202 wc[4] = BORDER(win);
4204 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4205 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4206 wc[3], YESNO(win->bordered));
4208 xcb_configure_window(conn, win->id, mask, wc);
4211 #define SWM_RESIZE_STEPS (50)
4214 resize(struct ws_win *win, union arg *args)
4216 xcb_timestamp_t timestamp = 0;
4217 struct swm_region *r = NULL;
4218 int resize_step = 0;
4219 struct swm_geometry g;
4220 int top = 0, left = 0, resizing;
4222 unsigned int shape; /* cursor style */
4223 xcb_cursor_t cursor;
4224 xcb_font_t cursor_font;
4225 xcb_grab_pointer_cookie_t gpc;
4226 xcb_grab_pointer_reply_t *gpr;
4227 xcb_query_pointer_reply_t *xpr;
4228 xcb_generic_event_t *evt;
4229 xcb_motion_notify_event_t *mne;
4235 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4238 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4239 "transient: 0x%x\n", win->id, YESNO(win->floating),
4242 if (!(win->transient != 0 || win->floating != 0))
4245 /* reject resizes in max mode for floaters (transient ok) */
4246 if (win->floatmaxed)
4250 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4256 case SWM_ARG_ID_WIDTHSHRINK:
4257 WIDTH(win) -= SWM_RESIZE_STEPS;
4260 case SWM_ARG_ID_WIDTHGROW:
4261 WIDTH(win) += SWM_RESIZE_STEPS;
4264 case SWM_ARG_ID_HEIGHTSHRINK:
4265 HEIGHT(win) -= SWM_RESIZE_STEPS;
4268 case SWM_ARG_ID_HEIGHTGROW:
4269 HEIGHT(win) += SWM_RESIZE_STEPS;
4276 constrain_window(win, r, 1);
4278 store_float_geom(win,r);
4282 /* get cursor offset from window root */
4283 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4290 if (xpr->win_x < WIDTH(win) / 2)
4293 if (xpr->win_y < HEIGHT(win) / 2)
4296 if (args->id == SWM_ARG_ID_CENTER)
4299 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4301 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4303 cursor_font = xcb_generate_id(conn);
4304 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4306 cursor = xcb_generate_id(conn);
4307 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4308 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4310 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4311 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4313 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4315 xcb_free_cursor(conn, cursor);
4316 xcb_close_font(conn, cursor_font);
4323 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4324 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4325 case XCB_BUTTON_RELEASE:
4326 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4329 case XCB_MOTION_NOTIFY:
4330 mne = (xcb_motion_notify_event_t *)evt;
4331 /* cursor offset/delta from start of the operation */
4332 dx = mne->root_x - xpr->root_x;
4333 dy = mne->root_y - xpr->root_y;
4339 if (args->id == SWM_ARG_ID_CENTER) {
4340 if (g.h / 2 + dy < 1)
4344 HEIGHT(win) = g.h + 2 * dy;
4352 HEIGHT(win) = g.h + dy;
4359 if (args->id == SWM_ARG_ID_CENTER) {
4360 if (g.w / 2 + dx < 1)
4364 WIDTH(win) = g.w + 2 * dx;
4372 WIDTH(win) = g.w + dx;
4375 constrain_window(win, r, 1);
4377 /* not free, don't sync more than 120 times / second */
4378 if ((mne->time - timestamp) > (1000 / 120) ) {
4379 timestamp = mne->time;
4394 store_float_geom(win,r);
4396 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4397 xcb_free_cursor(conn, cursor);
4398 xcb_close_font(conn, cursor_font);
4401 DNPRINTF(SWM_D_EVENT, "resize: done\n");
4405 resize_step(struct swm_region *r, union arg *args)
4407 struct ws_win *win = NULL;
4409 if (r && r->ws && r->ws->focus)
4417 #define SWM_MOVE_STEPS (50)
4420 move(struct ws_win *win, union arg *args)
4422 xcb_timestamp_t timestamp = 0;
4423 int move_step = 0, moving;
4424 struct swm_region *r = NULL;
4425 xcb_font_t cursor_font;
4426 xcb_cursor_t cursor;
4427 xcb_grab_pointer_cookie_t gpc;
4428 xcb_grab_pointer_reply_t *gpr;
4429 xcb_query_pointer_reply_t *qpr;
4430 xcb_generic_event_t *evt;
4431 xcb_motion_notify_event_t *mne;
4437 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4440 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4441 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4443 /* in max_stack mode should only move transients */
4444 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4448 if (win->floating == 0 && !win->transient) {
4449 store_float_geom(win, r);
4450 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4453 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4460 case SWM_ARG_ID_MOVELEFT:
4461 X(win) -= (SWM_MOVE_STEPS - border_width);
4464 case SWM_ARG_ID_MOVERIGHT:
4465 X(win) += (SWM_MOVE_STEPS - border_width);
4468 case SWM_ARG_ID_MOVEUP:
4469 Y(win) -= (SWM_MOVE_STEPS - border_width);
4472 case SWM_ARG_ID_MOVEDOWN:
4473 Y(win) += (SWM_MOVE_STEPS - border_width);
4480 constrain_window(win, r, 0);
4482 store_float_geom(win, r);
4486 cursor_font = xcb_generate_id(conn);
4487 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4489 cursor = xcb_generate_id(conn);
4490 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4491 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4493 gpc = xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4494 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4495 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4496 gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
4498 xcb_free_cursor(conn, cursor);
4499 xcb_close_font(conn, cursor_font);
4503 /* get cursor offset from window root */
4504 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4507 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4508 xcb_free_cursor(conn, cursor);
4509 xcb_close_font(conn, cursor_font);
4515 while ((evt = xcb_wait_for_event(conn)) && moving) {
4516 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4517 case XCB_BUTTON_RELEASE:
4518 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4521 case XCB_MOTION_NOTIFY:
4522 mne = (xcb_motion_notify_event_t *)evt;
4523 X(win) = mne->root_x - qpr->win_x - border_width;
4524 Y(win) = mne->root_y - qpr->win_y - border_width;
4526 constrain_window(win, r, 0);
4528 /* not free, don't sync more than 120 times / second */
4529 if ((mne->time - timestamp) > (1000 / 120) ) {
4530 timestamp = mne->time;
4545 store_float_geom(win, r);
4547 xcb_free_cursor(conn, cursor);
4548 xcb_close_font(conn, cursor_font);
4549 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4550 DNPRINTF(SWM_D_EVENT, "move: done\n");
4554 move_step(struct swm_region *r, union arg *args)
4556 struct ws_win *win = NULL;
4558 if (r && r->ws && r->ws->focus)
4563 if (!(win->transient != 0 || win->floating != 0))
4570 /* user/key callable function IDs */
4620 KF_SEARCH_WORKSPACE,
4661 KF_DUMPWINS, /* MUST BE LAST */
4665 /* key definitions */
4667 char name[SWM_FUNCNAME_LEN];
4668 void (*func)(struct swm_region *r, union arg *);
4670 } keyfuncs[KF_INVALID + 1] = {
4671 /* name function argument */
4672 { "bar_toggle", bar_toggle, {0} },
4673 { "button2", pressbutton, {2} },
4674 { "cycle_layout", cycle_layout, {0} },
4675 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4676 { "float_toggle", floating_toggle,{0} },
4677 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4678 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4679 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4680 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4681 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4682 { "iconify", iconify, {0} },
4683 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4684 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4685 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4686 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4687 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4688 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4689 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4690 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4691 { "mvws_1", send_to_ws, {.id = 0} },
4692 { "mvws_2", send_to_ws, {.id = 1} },
4693 { "mvws_3", send_to_ws, {.id = 2} },
4694 { "mvws_4", send_to_ws, {.id = 3} },
4695 { "mvws_5", send_to_ws, {.id = 4} },
4696 { "mvws_6", send_to_ws, {.id = 5} },
4697 { "mvws_7", send_to_ws, {.id = 6} },
4698 { "mvws_8", send_to_ws, {.id = 7} },
4699 { "mvws_9", send_to_ws, {.id = 8} },
4700 { "mvws_10", send_to_ws, {.id = 9} },
4701 { "mvws_11", send_to_ws, {.id = 10} },
4702 { "mvws_12", send_to_ws, {.id = 11} },
4703 { "mvws_13", send_to_ws, {.id = 12} },
4704 { "mvws_14", send_to_ws, {.id = 13} },
4705 { "mvws_15", send_to_ws, {.id = 14} },
4706 { "mvws_16", send_to_ws, {.id = 15} },
4707 { "mvws_17", send_to_ws, {.id = 16} },
4708 { "mvws_18", send_to_ws, {.id = 17} },
4709 { "mvws_19", send_to_ws, {.id = 18} },
4710 { "mvws_20", send_to_ws, {.id = 19} },
4711 { "mvws_21", send_to_ws, {.id = 20} },
4712 { "mvws_22", send_to_ws, {.id = 21} },
4713 { "name_workspace", name_workspace, {0} },
4714 { "quit", quit, {0} },
4715 { "raise_toggle", raise_toggle, {0} },
4716 { "restart", restart, {0} },
4717 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4718 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4719 { "search_win", search_win, {0} },
4720 { "search_workspace", search_workspace, {0} },
4721 { "spawn_custom", NULL, {0} },
4722 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4723 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4724 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4725 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4726 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4727 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4728 { "uniconify", uniconify, {0} },
4729 { "version", version, {0} },
4730 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4731 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4732 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4733 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4734 { "ws_1", switchws, {.id = 0} },
4735 { "ws_2", switchws, {.id = 1} },
4736 { "ws_3", switchws, {.id = 2} },
4737 { "ws_4", switchws, {.id = 3} },
4738 { "ws_5", switchws, {.id = 4} },
4739 { "ws_6", switchws, {.id = 5} },
4740 { "ws_7", switchws, {.id = 6} },
4741 { "ws_8", switchws, {.id = 7} },
4742 { "ws_9", switchws, {.id = 8} },
4743 { "ws_10", switchws, {.id = 9} },
4744 { "ws_11", switchws, {.id = 10} },
4745 { "ws_12", switchws, {.id = 11} },
4746 { "ws_13", switchws, {.id = 12} },
4747 { "ws_14", switchws, {.id = 13} },
4748 { "ws_15", switchws, {.id = 14} },
4749 { "ws_16", switchws, {.id = 15} },
4750 { "ws_17", switchws, {.id = 16} },
4751 { "ws_18", switchws, {.id = 17} },
4752 { "ws_19", switchws, {.id = 18} },
4753 { "ws_20", switchws, {.id = 19} },
4754 { "ws_21", switchws, {.id = 20} },
4755 { "ws_22", switchws, {.id = 21} },
4756 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4757 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4758 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4759 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4760 { "ws_prior", priorws, {0} },
4761 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4762 { "invalid key func", NULL, {0} },
4765 RB_ENTRY(key) entry;
4768 enum keyfuncid funcid;
4771 RB_HEAD(key_tree, key);
4774 key_cmp(struct key *kp1, struct key *kp2)
4776 if (kp1->keysym < kp2->keysym)
4778 if (kp1->keysym > kp2->keysym)
4781 if (kp1->mod < kp2->mod)
4783 if (kp1->mod > kp2->mod)
4789 RB_GENERATE(key_tree, key, entry, key_cmp);
4790 struct key_tree keys;
4793 enum { client_click, root_click };
4795 unsigned int action;
4797 unsigned int button;
4798 void (*func)(struct ws_win *, union arg *);
4801 /* action key mouse button func args */
4802 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4803 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4804 { client_click, MODKEY, Button1, move, {0} },
4808 update_modkey(unsigned int mod)
4814 RB_FOREACH(kp, key_tree, &keys)
4815 if (kp->mod & ShiftMask)
4816 kp->mod = mod | ShiftMask;
4820 for (i = 0; i < LENGTH(buttons); i++)
4821 if (buttons[i].mask & ShiftMask)
4822 buttons[i].mask = mod | ShiftMask;
4824 buttons[i].mask = mod;
4829 TAILQ_ENTRY(spawn_prog) entry;
4834 TAILQ_HEAD(spawn_list, spawn_prog);
4835 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4838 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4841 struct spawn_prog *prog = NULL;
4843 char *ap, **real_args;
4845 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4848 TAILQ_FOREACH(prog, &spawns, entry) {
4849 if (!strcasecmp(spawn_name, prog->name))
4853 warnx("spawn_custom: program %s not found", spawn_name);
4857 /* make room for expanded args */
4858 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4859 err(1, "spawn_custom: calloc real_args");
4861 /* expand spawn_args into real_args */
4862 for (i = 0; i < prog->argc; i++) {
4864 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4865 if (!strcasecmp(ap, "$bar_border")) {
4867 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4869 err(1, "spawn_custom border color");
4870 } else if (!strcasecmp(ap, "$bar_color")) {
4872 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4874 err(1, "spawn_custom bar color");
4875 } else if (!strcasecmp(ap, "$bar_font")) {
4876 if ((real_args[i] = strdup(bar_fonts))
4878 err(1, "spawn_custom bar fonts");
4879 } else if (!strcasecmp(ap, "$bar_font_color")) {
4881 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4883 err(1, "spawn_custom color font");
4884 } else if (!strcasecmp(ap, "$color_focus")) {
4886 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4888 err(1, "spawn_custom color focus");
4889 } else if (!strcasecmp(ap, "$color_unfocus")) {
4891 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4893 err(1, "spawn_custom color unfocus");
4895 /* no match --> copy as is */
4896 if ((real_args[i] = strdup(ap)) == NULL)
4897 err(1, "spawn_custom strdup(ap)");
4899 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4904 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4905 for (i = 0; i < prog->argc; i++)
4906 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4907 DNPRINTF(SWM_D_SPAWN, "\n");
4909 *ret_args = real_args;
4910 return (prog->argc);
4914 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4920 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4924 spawn(r->ws->idx, &a, 1);
4926 for (i = 0; i < spawn_argc; i++)
4932 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4938 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4942 if (pipe(select_list_pipe) == -1)
4943 err(1, "pipe error");
4944 if (pipe(select_resp_pipe) == -1)
4945 err(1, "pipe error");
4947 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4948 err(1, "could not disable SIGPIPE");
4949 switch (*pid = fork()) {
4951 err(1, "cannot fork");
4954 if (dup2(select_list_pipe[0], 0) == -1)
4956 if (dup2(select_resp_pipe[1], 1) == -1)
4958 close(select_list_pipe[1]);
4959 close(select_resp_pipe[0]);
4960 spawn(r->ws->idx, &a, 0);
4962 default: /* parent */
4963 close(select_list_pipe[0]);
4964 close(select_resp_pipe[1]);
4968 for (i = 0; i < spawn_argc; i++)
4974 spawn_insert(char *name, char *args)
4976 char *arg, *cp, *ptr;
4977 struct spawn_prog *sp;
4979 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4981 if ((sp = calloc(1, sizeof *sp)) == NULL)
4982 err(1, "spawn_insert: calloc");
4983 if ((sp->name = strdup(name)) == NULL)
4984 err(1, "spawn_insert: strdup");
4986 /* convert the arguments to an argument list */
4987 if ((ptr = cp = strdup(args)) == NULL)
4988 err(1, "spawn_insert: strdup");
4989 while ((arg = strsep(&ptr, " \t")) != NULL) {
4990 /* empty field; skip it */
4995 if ((sp->argv = realloc(sp->argv, sp->argc *
4996 sizeof *sp->argv)) == NULL)
4997 err(1, "spawn_insert: realloc");
4998 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4999 err(1, "spawn_insert: strdup");
5003 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5004 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5008 spawn_remove(struct spawn_prog *sp)
5012 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5014 TAILQ_REMOVE(&spawns, sp, entry);
5015 for (i = 0; i < sp->argc; i++)
5021 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5025 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5027 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5030 spawn_insert(name, args);
5032 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5036 setspawn(char *name, char *args)
5038 struct spawn_prog *sp;
5040 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5045 TAILQ_FOREACH(sp, &spawns, entry) {
5046 if (!strcmp(sp->name, name)) {
5050 spawn_replace(sp, name, args);
5051 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5055 if (*args == '\0') {
5056 warnx("error: setspawn: cannot find program: %s", name);
5060 spawn_insert(name, args);
5061 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5065 setconfspawn(char *selector, char *value, int flags)
5067 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5069 setspawn(selector, value);
5071 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5078 setconfspawn("term", "xterm", 0);
5079 setconfspawn("spawn_term", "xterm", 0);
5080 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5081 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5082 setconfspawn("lock", "xlock", 0);
5083 setconfspawn("initscr", "initscreen.sh", 0);
5084 setconfspawn("menu", "dmenu_run"
5087 " -nf $bar_font_color"
5089 " -sf $bar_color", 0);
5090 setconfspawn("search", "dmenu"
5094 " -nf $bar_font_color"
5096 " -sf $bar_color", 0);
5097 setconfspawn("name_workspace", "dmenu"
5101 " -nf $bar_font_color"
5103 " -sf $bar_color", 0);
5107 #define SWM_MODNAME_SIZE 32
5108 #define SWM_KEY_WS "\n+ \t"
5110 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5114 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5115 if (mod == NULL || ks == NULL) {
5116 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5119 if (keystr == NULL || strlen(keystr) == 0) {
5120 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5124 *ks = XCB_NO_SYMBOL;
5126 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5127 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5129 cp += (long)strspn(cp, SWM_KEY_WS);
5130 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5132 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5134 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5136 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5138 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5140 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5142 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5143 *mod |= ControlMask;
5145 *ks = XStringToKeysym(name);
5146 XConvertCase(*ks, ks, &uks);
5147 if (ks == XCB_NO_SYMBOL) {
5149 "parsekeys: invalid key %s\n",
5155 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5160 strdupsafe(char *str)
5165 return (strdup(str));
5169 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5173 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5174 keyfuncs[kfid].name, spawn_name);
5176 if ((kp = malloc(sizeof *kp)) == NULL)
5177 err(1, "key_insert: malloc");
5182 kp->spawn_name = strdupsafe(spawn_name);
5183 RB_INSERT(key_tree, &keys, kp);
5185 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5189 key_lookup(unsigned int mod, KeySym ks)
5196 return (RB_FIND(key_tree, &keys, &kp));
5200 key_remove(struct key *kp)
5202 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5204 RB_REMOVE(key_tree, &keys, kp);
5205 free(kp->spawn_name);
5208 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5212 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5215 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5219 key_insert(mod, ks, kfid, spawn_name);
5221 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5225 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5230 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5231 keyfuncs[kfid].name, spawn_name);
5233 if ((kp = key_lookup(mod, ks)) != NULL) {
5234 if (kfid == KF_INVALID)
5237 key_replace(kp, mod, ks, kfid, spawn_name);
5238 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5241 if (kfid == KF_INVALID) {
5242 warnx("error: setkeybinding: cannot find mod/key combination");
5243 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5247 key_insert(mod, ks, kfid, spawn_name);
5248 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5252 setconfbinding(char *selector, char *value, int flags)
5254 enum keyfuncid kfid;
5257 struct spawn_prog *sp;
5258 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5259 if (selector == NULL) {
5260 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5261 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5263 setkeybinding(mod, ks, kfid, NULL);
5268 /* search by key function name */
5269 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5270 if (strncasecmp(selector, keyfuncs[kfid].name,
5271 SWM_FUNCNAME_LEN) == 0) {
5272 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5274 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5275 setkeybinding(mod, ks, kfid, NULL);
5281 /* search by custom spawn name */
5282 TAILQ_FOREACH(sp, &spawns, entry) {
5283 if (strcasecmp(selector, sp->name) == 0) {
5284 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5286 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5287 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5294 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5301 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5302 setkeybinding(MODKEY|ShiftMask, XK_backslash, KF_FLIP_LAYOUT, NULL);
5303 setkeybinding(MODKEY|ShiftMask, XK_space, KF_STACK_RESET, NULL);
5304 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5305 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5306 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5307 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5308 setkeybinding(MODKEY|ShiftMask, XK_comma, KF_STACK_INC, NULL);
5309 setkeybinding(MODKEY|ShiftMask, XK_period, KF_STACK_DEC, NULL);
5310 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5311 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5312 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5313 setkeybinding(MODKEY|ShiftMask, XK_j, KF_SWAP_NEXT, NULL);
5314 setkeybinding(MODKEY|ShiftMask, XK_k, KF_SWAP_PREV, NULL);
5315 setkeybinding(MODKEY|ShiftMask, XK_Return, KF_SPAWN_CUSTOM,"term");
5316 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5317 setkeybinding(MODKEY|ShiftMask, XK_q, KF_QUIT, NULL);
5318 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5319 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5320 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5321 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5322 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5323 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5324 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5325 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5326 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5327 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5328 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5329 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5330 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5331 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5332 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5333 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5334 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5335 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5336 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5337 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5338 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5339 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5340 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5341 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5342 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5343 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5344 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5345 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5346 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5347 setkeybinding(MODKEY|ShiftMask, XK_Right, KF_SCREEN_NEXT, NULL);
5348 setkeybinding(MODKEY|ShiftMask, XK_Left, KF_SCREEN_PREV, NULL);
5349 setkeybinding(MODKEY|ShiftMask, XK_1, KF_MVWS_1, NULL);
5350 setkeybinding(MODKEY|ShiftMask, XK_2, KF_MVWS_2, NULL);
5351 setkeybinding(MODKEY|ShiftMask, XK_3, KF_MVWS_3, NULL);
5352 setkeybinding(MODKEY|ShiftMask, XK_4, KF_MVWS_4, NULL);
5353 setkeybinding(MODKEY|ShiftMask, XK_5, KF_MVWS_5, NULL);
5354 setkeybinding(MODKEY|ShiftMask, XK_6, KF_MVWS_6, NULL);
5355 setkeybinding(MODKEY|ShiftMask, XK_7, KF_MVWS_7, NULL);
5356 setkeybinding(MODKEY|ShiftMask, XK_8, KF_MVWS_8, NULL);
5357 setkeybinding(MODKEY|ShiftMask, XK_9, KF_MVWS_9, NULL);
5358 setkeybinding(MODKEY|ShiftMask, XK_0, KF_MVWS_10, NULL);
5359 setkeybinding(MODKEY|ShiftMask, XK_F1, KF_MVWS_11, NULL);
5360 setkeybinding(MODKEY|ShiftMask, XK_F2, KF_MVWS_12, NULL);
5361 setkeybinding(MODKEY|ShiftMask, XK_F3, KF_MVWS_13, NULL);
5362 setkeybinding(MODKEY|ShiftMask, XK_F4, KF_MVWS_14, NULL);
5363 setkeybinding(MODKEY|ShiftMask, XK_F5, KF_MVWS_15, NULL);
5364 setkeybinding(MODKEY|ShiftMask, XK_F6, KF_MVWS_16, NULL);
5365 setkeybinding(MODKEY|ShiftMask, XK_F7, KF_MVWS_17, NULL);
5366 setkeybinding(MODKEY|ShiftMask, XK_F8, KF_MVWS_18, NULL);
5367 setkeybinding(MODKEY|ShiftMask, XK_F9, KF_MVWS_19, NULL);
5368 setkeybinding(MODKEY|ShiftMask, XK_F10, KF_MVWS_20, NULL);
5369 setkeybinding(MODKEY|ShiftMask, XK_F11, KF_MVWS_21, NULL);
5370 setkeybinding(MODKEY|ShiftMask, XK_F12, KF_MVWS_22, NULL);
5371 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5372 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_Tab, KF_FOCUS_PREV, NULL);
5374 setkeybinding(MODKEY|ShiftMask, XK_x, KF_WIND_KILL, NULL);
5375 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5376 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5377 setkeybinding(MODKEY|ShiftMask, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5378 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_v, KF_VERSION, NULL);
5380 setkeybinding(MODKEY|ShiftMask, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5381 setkeybinding(MODKEY|ShiftMask, XK_i, KF_SPAWN_CUSTOM,"initscr");
5382 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5383 setkeybinding(MODKEY|ShiftMask, XK_w, KF_UNICONIFY, NULL);
5384 setkeybinding(MODKEY|ShiftMask, XK_r, KF_RAISE_TOGGLE,NULL);
5385 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5386 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5387 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5388 setkeybinding(MODKEY|ShiftMask, XK_equal, KF_HEIGHT_GROW,NULL);
5389 setkeybinding(MODKEY|ShiftMask, XK_minus, KF_HEIGHT_SHRINK,NULL);
5390 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5391 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5392 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, KF_MOVE_UP, NULL);
5393 setkeybinding(MODKEY|ShiftMask, XK_bracketright,KF_MOVE_DOWN,NULL);
5394 setkeybinding(MODKEY|ShiftMask, XK_slash, KF_NAME_WORKSPACE,NULL);
5395 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5396 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5398 setkeybinding(MODKEY|ShiftMask, XK_d, KF_DUMPWINS, NULL);
5407 while (RB_EMPTY(&keys) == 0) {
5408 kp = RB_ROOT(&keys);
5414 setkeymapping(char *selector, char *value, int flags)
5416 char keymapping_file[PATH_MAX];
5417 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5418 if (value[0] == '~')
5419 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5420 pwd->pw_dir, &value[1]);
5422 strlcpy(keymapping_file, value, sizeof keymapping_file);
5424 /* load new key bindings; if it fails, revert to default bindings */
5425 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5429 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5434 updatenumlockmask(void)
5437 xcb_get_modifier_mapping_reply_t *modmap_r;
5438 xcb_keycode_t *modmap, kc;
5440 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5443 modmap_r = xcb_get_modifier_mapping_reply(conn,
5444 xcb_get_modifier_mapping(conn),
5447 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5448 for (i = 0; i < 8; i++) {
5449 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5450 kc = modmap[i * modmap_r->keycodes_per_modifier
5453 if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
5455 numlockmask = (1 << i);
5467 xcb_keycode_t *code;
5468 unsigned int modifiers[] =
5469 { 0, LockMask, numlockmask, numlockmask | LockMask };
5472 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5473 updatenumlockmask();
5475 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5476 for (k = 0; k < num_screens; k++) {
5477 if (TAILQ_EMPTY(&screens[k].rl))
5479 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5481 RB_FOREACH(kp, key_tree, &keys) {
5482 if ((code = xcb_key_symbols_get_keycode(syms,
5484 for (j = 0; j < LENGTH(modifiers); j++)
5485 xcb_grab_key(conn, True,
5487 kp->mod | modifiers[j],
5488 *code, XCB_GRAB_MODE_ASYNC,
5489 XCB_GRAB_MODE_ASYNC);
5495 grabbuttons(struct ws_win *win, int focused)
5498 unsigned int modifiers[] =
5499 { 0, LockMask, numlockmask, numlockmask|LockMask };
5501 updatenumlockmask();
5502 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5503 XCB_BUTTON_MASK_ANY);
5505 for (i = 0; i < LENGTH(buttons); i++)
5506 if (buttons[i].action == client_click)
5507 for (j = 0; j < LENGTH(modifiers); j++)
5508 xcb_grab_button(conn, False, win->id,
5510 XCB_GRAB_MODE_ASYNC,
5517 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5518 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_WINDOW_NONE,
5519 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY);
5522 const char *quirkname[] = {
5523 "NONE", /* config string for "no value" */
5532 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5533 #define SWM_Q_WS "\n|+ \t"
5535 parsequirks(char *qstr, unsigned long *quirk)
5545 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5547 cp += (long)strspn(cp, SWM_Q_WS);
5548 for (i = 0; i < LENGTH(quirkname); i++) {
5549 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5550 DNPRINTF(SWM_D_QUIRK,
5551 "parsequirks: %s\n", name);
5556 *quirk |= 1 << (i-1);
5560 if (i >= LENGTH(quirkname)) {
5561 DNPRINTF(SWM_D_QUIRK,
5562 "parsequirks: invalid quirk [%s]\n", name);
5570 quirk_insert(const char *class, const char *name, unsigned long quirk)
5574 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5577 if ((qp = malloc(sizeof *qp)) == NULL)
5578 err(1, "quirk_insert: malloc");
5579 if ((qp->class = strdup(class)) == NULL)
5580 err(1, "quirk_insert: strdup");
5581 if ((qp->name = strdup(name)) == NULL)
5582 err(1, "quirk_insert: strdup");
5585 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5587 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5591 quirk_remove(struct quirk *qp)
5593 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5594 qp->name, qp->quirk);
5596 TAILQ_REMOVE(&quirks, qp, entry);
5601 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5605 quirk_replace(struct quirk *qp, const char *class, const char *name,
5606 unsigned long quirk)
5608 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5609 qp->name, qp->quirk);
5612 quirk_insert(class, name, quirk);
5614 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5618 setquirk(const char *class, const char *name, unsigned long quirk)
5622 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5625 TAILQ_FOREACH(qp, &quirks, entry) {
5626 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5630 quirk_replace(qp, class, name, quirk);
5631 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5636 warnx("error: setquirk: cannot find class/name combination");
5640 quirk_insert(class, name, quirk);
5641 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5645 setconfquirk(char *selector, char *value, int flags)
5647 char *cp, *class, *name;
5649 unsigned long quirks;
5650 if (selector == NULL)
5652 if ((cp = strchr(selector, ':')) == NULL)
5657 if ((retval = parsequirks(value, &quirks)) == 0)
5658 setquirk(class, name, quirks);
5665 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5666 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5667 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5668 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5669 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5670 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5671 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5672 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5673 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5674 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5675 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5676 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5677 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5680 /* conf file stuff */
5681 #define SWM_CONF_FILE "spectrwm.conf"
5682 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5686 SWM_S_BAR_AT_BOTTOM,
5687 SWM_S_BAR_BORDER_WIDTH,
5694 SWM_S_CLOCK_ENABLED,
5697 SWM_S_CYCLE_VISIBLE,
5699 SWM_S_DISABLE_BORDER,
5701 SWM_S_FOCUS_CLOSE_WRAP,
5702 SWM_S_FOCUS_DEFAULT,
5708 SWM_S_STACK_ENABLED,
5710 SWM_S_TITLE_CLASS_ENABLED,
5711 SWM_S_TITLE_NAME_ENABLED,
5712 SWM_S_URGENT_ENABLED,
5713 SWM_S_VERBOSE_LAYOUT,
5714 SWM_S_WINDOW_NAME_ENABLED,
5715 SWM_S_WORKSPACE_LIMIT
5719 setconfvalue(char *selector, char *value, int flags)
5725 case SWM_S_BAR_ACTION:
5727 if ((bar_argv[0] = strdup(value)) == NULL)
5728 err(1, "setconfvalue: bar_action");
5730 case SWM_S_BAR_AT_BOTTOM:
5731 bar_at_bottom = atoi(value);
5733 case SWM_S_BAR_BORDER_WIDTH:
5734 bar_border_width = atoi(value);
5735 if (bar_border_width < 0)
5736 bar_border_width = 0;
5738 case SWM_S_BAR_DELAY:
5739 bar_delay = atoi(value);
5741 case SWM_S_BAR_ENABLED:
5742 bar_enabled = atoi(value);
5744 case SWM_S_BAR_FONT:
5746 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5747 err(1, "setconfvalue: asprintf: failed to allocate "
5748 "memory for bar_fonts.");
5751 case SWM_S_BAR_FORMAT:
5753 if ((bar_format = strdup(value)) == NULL)
5754 err(1, "setconfvalue: bar_format");
5756 case SWM_S_BAR_JUSTIFY:
5757 if (!strcmp(value, "left"))
5758 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5759 else if (!strcmp(value, "center"))
5760 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5761 else if (!strcmp(value, "right"))
5762 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5764 errx(1, "invalid bar_justify");
5766 case SWM_S_BORDER_WIDTH:
5767 border_width = atoi(value);
5768 if (border_width < 0)
5771 case SWM_S_CLOCK_ENABLED:
5772 clock_enabled = atoi(value);
5774 case SWM_S_CLOCK_FORMAT:
5775 #ifndef SWM_DENY_CLOCK_FORMAT
5777 if ((clock_format = strdup(value)) == NULL)
5778 err(1, "setconfvalue: clock_format");
5781 case SWM_S_CYCLE_EMPTY:
5782 cycle_empty = atoi(value);
5784 case SWM_S_CYCLE_VISIBLE:
5785 cycle_visible = atoi(value);
5787 case SWM_S_DIALOG_RATIO:
5788 dialog_ratio = atof(value);
5789 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5792 case SWM_S_DISABLE_BORDER:
5793 disable_border = atoi(value);
5795 case SWM_S_FOCUS_CLOSE:
5796 if (!strcmp(value, "first"))
5797 focus_close = SWM_STACK_BOTTOM;
5798 else if (!strcmp(value, "last"))
5799 focus_close = SWM_STACK_TOP;
5800 else if (!strcmp(value, "next"))
5801 focus_close = SWM_STACK_ABOVE;
5802 else if (!strcmp(value, "previous"))
5803 focus_close = SWM_STACK_BELOW;
5805 errx(1, "focus_close");
5807 case SWM_S_FOCUS_CLOSE_WRAP:
5808 focus_close_wrap = atoi(value);
5810 case SWM_S_FOCUS_DEFAULT:
5811 if (!strcmp(value, "last"))
5812 focus_default = SWM_STACK_TOP;
5813 else if (!strcmp(value, "first"))
5814 focus_default = SWM_STACK_BOTTOM;
5816 errx(1, "focus_default");
5818 case SWM_S_FOCUS_MODE:
5819 if (!strcmp(value, "default"))
5820 focus_mode = SWM_FOCUS_DEFAULT;
5821 else if (!strcmp(value, "follow_cursor"))
5822 focus_mode = SWM_FOCUS_FOLLOW;
5823 else if (!strcmp(value, "synergy"))
5824 focus_mode = SWM_FOCUS_SYNERGY;
5826 errx(1, "focus_mode");
5828 case SWM_S_SPAWN_ORDER:
5829 if (!strcmp(value, "first"))
5830 spawn_position = SWM_STACK_BOTTOM;
5831 else if (!strcmp(value, "last"))
5832 spawn_position = SWM_STACK_TOP;
5833 else if (!strcmp(value, "next"))
5834 spawn_position = SWM_STACK_ABOVE;
5835 else if (!strcmp(value, "previous"))
5836 spawn_position = SWM_STACK_BELOW;
5838 errx(1, "spawn_position");
5840 case SWM_S_SPAWN_TERM:
5841 setconfspawn("term", value, 0);
5842 setconfspawn("spawn_term", value, 0);
5846 case SWM_S_SS_ENABLED:
5847 ss_enabled = atoi(value);
5849 case SWM_S_STACK_ENABLED:
5850 stack_enabled = atoi(value);
5852 case SWM_S_TERM_WIDTH:
5853 term_width = atoi(value);
5857 case SWM_S_TITLE_CLASS_ENABLED:
5858 title_class_enabled = atoi(value);
5860 case SWM_S_TITLE_NAME_ENABLED:
5861 title_name_enabled = atoi(value);
5863 case SWM_S_URGENT_ENABLED:
5864 urgent_enabled = atoi(value);
5866 case SWM_S_VERBOSE_LAYOUT:
5867 verbose_layout = atoi(value);
5868 for (i = 0; layouts[i].l_stack != NULL; i++) {
5870 layouts[i].l_string = fancy_stacker;
5872 layouts[i].l_string = plain_stacker;
5875 case SWM_S_WINDOW_NAME_ENABLED:
5876 window_name_enabled = atoi(value);
5878 case SWM_S_WORKSPACE_LIMIT:
5879 workspace_limit = atoi(value);
5880 if (workspace_limit > SWM_WS_MAX)
5881 workspace_limit = SWM_WS_MAX;
5882 else if (workspace_limit < 1)
5883 workspace_limit = 1;
5892 setconfmodkey(char *selector, char *value, int flags)
5894 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5895 update_modkey(Mod1Mask);
5896 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5897 update_modkey(Mod2Mask);
5898 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5899 update_modkey(Mod3Mask);
5900 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5901 update_modkey(Mod4Mask);
5908 setconfcolor(char *selector, char *value, int flags)
5910 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5915 setconfregion(char *selector, char *value, int flags)
5917 custom_region(value);
5922 setautorun(char *selector, char *value, int flags)
5932 if (getenv("SWM_STARTED"))
5936 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5937 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5939 if (ws_id < 0 || ws_id >= workspace_limit)
5940 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5943 * This is a little intricate
5945 * If the pid already exists we simply reuse it because it means it was
5946 * used before AND not claimed by manage_window. We get away with
5947 * altering it in the parent after INSERT because this can not be a race
5950 while ((ap = strsep(&sp, " \t")) != NULL) {
5953 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5955 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5956 err(1, "setautorun: realloc");
5957 a.argv[argc - 1] = ap;
5960 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5961 err(1, "setautorun: realloc");
5962 a.argv[argc] = NULL;
5964 if ((pid = fork()) == 0) {
5965 spawn(ws_id, &a, 1);
5974 p = calloc(1, sizeof *p);
5977 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5987 setlayout(char *selector, char *value, int flags)
5989 int ws_id, i, x, mg, ma, si, raise, f = 0;
5990 int st = SWM_V_STACK, num_screens;
5992 struct workspace *ws;
5994 if (getenv("SWM_STARTED"))
5998 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5999 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6000 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6001 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6004 if (ws_id < 0 || ws_id >= workspace_limit)
6005 errx(1, "layout: invalid workspace %d", ws_id + 1);
6007 if (!strcasecmp(s, "vertical"))
6009 else if (!strcasecmp(s, "vertical_flip")) {
6012 } else if (!strcasecmp(s, "horizontal"))
6014 else if (!strcasecmp(s, "horizontal_flip")) {
6017 } else if (!strcasecmp(s, "fullscreen"))
6020 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6021 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6024 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6025 for (i = 0; i < num_screens; i++) {
6026 ws = (struct workspace *)&screens[i].ws;
6027 ws[ws_id].cur_layout = &layouts[st];
6029 ws[ws_id].always_raise = raise;
6030 if (st == SWM_MAX_STACK)
6034 for (x = 0; x < abs(mg); x++) {
6035 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6036 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6037 SWM_ARG_ID_MASTERSHRINK);
6041 for (x = 0; x < abs(ma); x++) {
6042 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6043 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6044 SWM_ARG_ID_MASTERDEL);
6048 for (x = 0; x < abs(si); x++) {
6049 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6050 si >= 0 ? SWM_ARG_ID_STACKINC :
6051 SWM_ARG_ID_STACKDEC);
6056 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6057 SWM_ARG_ID_FLIPLAYOUT);
6065 /* config options */
6066 struct config_option {
6068 int (*func)(char*, char*, int);
6071 struct config_option configopt[] = {
6072 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6073 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6074 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6075 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6076 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6077 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6078 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6079 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6080 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6081 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6082 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6083 { "keyboard_mapping", setkeymapping, 0 },
6084 { "bind", setconfbinding, 0 },
6085 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6086 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6087 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6088 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6089 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6090 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6091 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6092 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6093 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6094 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6095 { "modkey", setconfmodkey, 0 },
6096 { "program", setconfspawn, 0 },
6097 { "quirk", setconfquirk, 0 },
6098 { "region", setconfregion, 0 },
6099 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6100 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6101 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6102 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6103 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6104 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6105 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6106 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6107 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6108 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6109 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6110 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6111 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6112 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6113 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6114 { "autorun", setautorun, 0 },
6115 { "layout", setlayout, 0 },
6120 conf_load(char *filename, int keymapping)
6123 char *line, *cp, *optsub, *optval;
6124 size_t linelen, lineno = 0;
6125 int wordlen, i, optind;
6126 struct config_option *opt;
6128 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6130 if (filename == NULL) {
6131 warnx("conf_load: no filename");
6134 if ((config = fopen(filename, "r")) == NULL) {
6135 warn("conf_load: fopen: %s", filename);
6139 while (!feof(config)) {
6140 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6143 err(1, "%s", filename);
6148 cp += strspn(cp, " \t\n"); /* eat whitespace */
6149 if (cp[0] == '\0') {
6154 /* get config option */
6155 wordlen = strcspn(cp, "=[ \t\n");
6157 warnx("%s: line %zd: no option found",
6162 for (i = 0; i < LENGTH(configopt); i++) {
6163 opt = &configopt[i];
6164 if (!strncasecmp(cp, opt->optname, wordlen) &&
6165 (int)strlen(opt->optname) == wordlen) {
6171 warnx("%s: line %zd: unknown option %.*s",
6172 filename, lineno, wordlen, cp);
6175 if (keymapping && strcmp(opt->optname, "bind")) {
6176 warnx("%s: line %zd: invalid option %.*s",
6177 filename, lineno, wordlen, cp);
6181 cp += strspn(cp, " \t\n"); /* eat whitespace */
6182 /* get [selector] if any */
6186 wordlen = strcspn(cp, "]");
6189 warnx("%s: line %zd: syntax error",
6194 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6196 warnx("%s: line %zd: unable to allocate"
6197 "memory for selector", filename,
6203 cp += strspn(cp, "] \t\n"); /* eat trailing */
6205 cp += strspn(cp, "= \t\n"); /* eat trailing */
6207 optval = strdup(cp);
6208 /* call function to deal with it all */
6209 if (configopt[optind].func(optsub, optval,
6210 configopt[optind].funcflags) != 0)
6211 errx(1, "%s: line %zd: invalid data for %s",
6212 filename, lineno, configopt[optind].optname);
6219 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6226 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6232 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6234 struct ws_win *parent, *w;
6235 struct swm_region *r;
6236 struct workspace *ws;
6237 xcb_icccm_wm_hints_t wmh;
6239 parent = find_window(win->transient);
6241 parent->child_trans = win;
6243 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6244 " for 0x%x trans 0x%x\n", win->id, win->transient);
6246 r = root_to_region(win->wa->root);
6248 /* parent doen't exist in our window list */
6249 TAILQ_FOREACH(w, &ws->winlist, entry) {
6250 if (xcb_icccm_get_wm_hints_reply(conn,
6251 xcb_icccm_get_wm_hints(conn, w->id),
6253 warnx("can't get hints for 0x%x", w->id);
6257 if (win->hints.window_group != wmh.window_group)
6260 w->child_trans = win;
6261 win->transient = w->id;
6263 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6264 "transient to 0x%x\n", win->transient);
6271 window_get_pid(xcb_window_t win)
6276 xcb_get_property_cookie_t pc;
6277 xcb_get_property_reply_t *pr;
6279 apid = get_atom_from_string("_NET_WM_PID");
6280 if (apid == XCB_ATOM_NONE)
6283 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6284 pr = xcb_get_property_reply(conn, pc, NULL);
6287 if (pr->type != XCB_ATOM_CARDINAL) {
6292 ret = *((pid_t *)xcb_get_property_value(pr));
6298 apid = get_atom_from_string("_SWM_PID");
6299 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6301 pr = xcb_get_property_reply(conn, pc, NULL);
6304 if (pr->type != XCB_ATOM_STRING) {
6309 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6316 manage_window(xcb_window_t id)
6318 xcb_window_t trans = XCB_WINDOW_NONE;
6319 struct workspace *ws;
6320 struct ws_win *win, *ww;
6321 int ws_idx, border_me = 0;
6322 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6323 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6325 struct swm_region *r;
6329 uint32_t event_mask, i;
6331 xcb_get_property_reply_t *gpr;
6332 xcb_icccm_get_wm_protocols_reply_t wpr;
6334 if ((win = find_window(id)) != NULL)
6335 return (win); /* already being managed */
6337 /* see if we are on the unmanaged list */
6338 if ((win = find_unmanaged_window(id)) != NULL) {
6339 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6340 "window: 0x%x\n", win->id);
6341 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6343 set_child_transient(win, &trans);
6345 if (trans && (ww = find_window(trans)))
6346 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6347 else if ((ww = win->ws->focus) &&
6348 spawn_position == SWM_STACK_ABOVE)
6349 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6351 else if (ww && spawn_position == SWM_STACK_BELOW)
6352 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6353 else switch (spawn_position) {
6356 case SWM_STACK_ABOVE:
6357 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6359 case SWM_STACK_BOTTOM:
6360 case SWM_STACK_BELOW:
6361 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6364 ewmh_update_actions(win);
6368 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6369 err(1, "manage_window: calloc: failed to allocate memory for "
6375 /* see if we need to override the workspace */
6376 p = find_pid(window_get_pid(id));
6378 /* Get all the window data in one shot */
6379 ws_idx_atom = get_atom_from_string("_SWM_WS");
6381 gpr = xcb_get_property_reply(conn,
6382 xcb_get_property(conn, False, id, ws_idx_atom,
6383 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6386 proplen = xcb_get_property_value_length(gpr);
6388 prop = malloc(proplen + 1);
6391 xcb_get_property_value(gpr),
6393 prop[proplen] = '\0';
6399 win->wa = xcb_get_geometry_reply(conn,
6400 xcb_get_geometry(conn, id),
6402 xcb_icccm_get_wm_normal_hints_reply(conn,
6403 xcb_icccm_get_wm_normal_hints(conn, id),
6405 xcb_icccm_get_wm_hints_reply(conn,
6406 xcb_icccm_get_wm_hints(conn, id),
6408 xcb_icccm_get_wm_transient_for_reply(conn,
6409 xcb_icccm_get_wm_transient_for(conn, id),
6412 win->transient = trans;
6413 set_child_transient(win, &trans);
6414 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6415 "transient: 0x%x\n", win->id, win->transient);
6418 prot = get_atom_from_string("WM_PROTOCOLS");
6420 /* get supported protocols */
6421 if (xcb_icccm_get_wm_protocols_reply(conn,
6422 xcb_icccm_get_wm_protocols(conn, id, prot),
6424 for (i = 0; i < wpr.atoms_len; i++) {
6425 if (wpr.atoms[i] == a_takefocus)
6426 win->take_focus = 1;
6427 if (wpr.atoms[i] == a_delete)
6428 win->can_delete = 1;
6430 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6433 win->iconic = get_iconic(win);
6436 * Figure out where to put the window. If it was previously assigned to
6437 * a workspace (either by spawn() or manually moving), and isn't
6438 * transient, * put it in the same workspace
6440 r = root_to_region(win->wa->root);
6442 ws = &r->s->ws[p->ws];
6443 TAILQ_REMOVE(&pidlist, p, entry);
6446 } else if (prop && win->transient == 0) {
6447 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6448 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1,
6451 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6454 ws = &r->s->ws[ws_idx];
6457 /* this should launch transients in the same ws as parent */
6459 if ((ww = find_window(trans)) != NULL)
6465 warnx("manage_window: fix this "
6471 /* set up the window layout */
6474 win->s = r->s; /* this never changes */
6475 if (trans && (ww = find_window(trans)))
6476 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6477 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6478 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6480 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6481 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6482 else switch (spawn_position) {
6485 case SWM_STACK_ABOVE:
6486 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6488 case SWM_STACK_BOTTOM:
6489 case SWM_STACK_BELOW:
6490 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6493 /* ignore window border if there is one. */
6494 WIDTH(win) = win->wa->width;
6495 HEIGHT(win) = win->wa->height;
6496 X(win) = win->wa->x + win->wa->border_width;
6497 Y(win) = win->wa->y + win->wa->border_width;
6499 win->g_floatvalid = 0;
6500 win->floatmaxed = 0;
6501 win->ewmh_flags = 0;
6503 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6504 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6505 HEIGHT(win), ws->idx);
6507 constrain_window(win, r, 0);
6509 /* Set window properties so we can remember this after reincarnation */
6510 if (ws_idx_atom && prop == NULL &&
6511 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6513 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6515 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6516 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6522 ewmh_autoquirk(win);
6524 if (xcb_icccm_get_wm_class_reply(conn,
6525 xcb_icccm_get_wm_class(conn, win->id),
6527 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6528 win->ch.class_name, win->ch.instance_name);
6530 /* java is retarded so treat it special */
6531 if (strstr(win->ch.instance_name, "sun-awt")) {
6536 TAILQ_FOREACH(qp, &quirks, entry) {
6537 if (!strcmp(win->ch.class_name, qp->class) &&
6538 !strcmp(win->ch.instance_name, qp->name)) {
6539 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6540 "class: %s, name: %s\n", win->ch.class_name,
6541 win->ch.instance_name);
6542 if (qp->quirk & SWM_Q_FLOAT) {
6546 win->quirks = qp->quirk;
6551 /* alter window position if quirky */
6552 if (win->quirks & SWM_Q_ANYWHERE) {
6553 win->manual = 1; /* don't center the quirky windows */
6554 if (bar_enabled && Y(win) < bar_height)
6555 Y(win) = bar_height;
6556 if (WIDTH(win) + X(win) > WIDTH(r))
6557 X(win) = WIDTH(r) - WIDTH(win) - 2;
6561 /* Reset font sizes (the bruteforce way; no default keybinding). */
6562 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6563 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6564 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6565 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6566 fake_keypress(win, XK_KP_Add, ShiftMask);
6569 ewmh_get_win_state(win);
6570 ewmh_update_actions(win);
6571 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6576 X(win) -= border_width;
6577 Y(win) -= border_width;
6581 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6582 XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6584 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6586 /* floaters need to be mapped if they are in the current workspace */
6587 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6588 map_window_raised(win->id);
6594 free_window(struct ws_win *win)
6596 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6601 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6606 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6611 memset(win, 0xff, sizeof *win); /* XXX kill later */
6614 DNPRINTF(SWM_D_MISC, "free_window: done\n");
6618 unmanage_window(struct ws_win *win)
6620 struct ws_win *parent;
6621 xcb_screen_t *screen;
6626 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6628 if (win->transient) {
6629 parent = find_window(win->transient);
6631 parent->child_trans = NULL;
6634 /* focus on root just in case */
6635 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6636 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6637 screen->root, XCB_CURRENT_TIME);
6641 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6642 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6646 focus_magic(struct ws_win *win)
6648 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6651 /* if there are no windows clear the status-bar */
6656 if (win->child_trans) {
6657 /* win = parent & has a transient so focus on that */
6659 focus_win(win->child_trans);
6660 if (win->child_trans->take_focus)
6661 client_msg(win, a_takefocus);
6663 /* make sure transient hasn't disappeared */
6664 if (validate_win(win->child_trans) == 0) {
6665 focus_win(win->child_trans);
6666 if (win->child_trans->take_focus)
6667 client_msg(win->child_trans,
6670 win->child_trans = NULL;
6672 if (win->take_focus)
6673 client_msg(win, a_takefocus);
6679 if (win->take_focus)
6680 client_msg(win, a_takefocus);
6685 expose(xcb_expose_event_t *e)
6688 struct swm_region *r;
6690 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
6692 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6693 for (i = 0; i < num_screens; i++)
6694 TAILQ_FOREACH(r, &screens[i].rl, entry)
6695 if (e->window == WINID(r->bar))
6702 keypress(xcb_key_press_event_t *e)
6704 xcb_keysym_t keysym;
6707 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
6709 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
6711 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
6714 if (kp->funcid == KF_SPAWN_CUSTOM)
6715 spawn_custom(root_to_region(e->root),
6716 &(keyfuncs[kp->funcid].args), kp->spawn_name);
6717 else if (keyfuncs[kp->funcid].func)
6718 keyfuncs[kp->funcid].func(root_to_region(e->root),
6719 &(keyfuncs[kp->funcid].args));
6723 buttonpress(xcb_button_press_event_t *e)
6727 unsigned int action;
6729 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
6730 e->event, e->detail);
6732 if ((win = find_window(e->event)) == NULL)
6736 action = client_click;
6738 for (i = 0; i < LENGTH(buttons); i++)
6739 if (action == buttons[i].action && buttons[i].func &&
6740 buttons[i].button == e->detail &&
6741 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state))
6742 buttons[i].func(win, &buttons[i].args);
6749 print_win_geom(xcb_window_t w)
6751 xcb_get_geometry_reply_t *wa;
6754 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
6756 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
6757 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6758 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
6766 configurerequest(xcb_configure_request_event_t *e)
6771 uint32_t wc[7] = {0};
6773 if ((win = find_window(e->window)) == NULL)
6774 if ((win = find_unmanaged_window(e->window)) == NULL)
6777 print_win_geom(e->window);
6780 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%x, "
6781 "value_mask: 0x%x", e->window, e->value_mask);
6782 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
6783 mask |= XCB_CONFIG_WINDOW_X;
6785 DPRINTF(", X: %d", e->x);
6787 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
6788 mask |= XCB_CONFIG_WINDOW_Y;
6790 DPRINTF(", Y: %d", e->y);
6792 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
6793 mask |= XCB_CONFIG_WINDOW_WIDTH;
6795 DPRINTF(", W: %u", e->width);
6797 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
6798 mask |= XCB_CONFIG_WINDOW_HEIGHT;
6799 wc[i++] = e->height;
6800 DPRINTF(", H: %u", e->height);
6802 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
6803 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
6804 wc[i++] = e->border_width;
6805 DPRINTF(", Border: %u", e->border_width);
6807 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
6808 mask |= XCB_CONFIG_WINDOW_SIBLING;
6809 wc[i++] = e->sibling;
6810 DPRINTF(", Sibling: 0x%x", e->sibling);
6812 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
6813 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
6814 wc[i++] = e->stack_mode;
6815 DPRINTF(", StackMode: %u", e->stack_mode);
6819 xcb_configure_window(conn, e->window, mask, wc);
6821 DPRINTF(", Sent: %s\n", YESNO((mask != 0)));
6822 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6823 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
6824 win->g_float.x = e->x - X(win->ws->r);
6825 win->g_float.y = e->y - Y(win->ws->r);
6826 win->g_float.w = e->width;
6827 win->g_float.h = e->height;
6828 win->g_floatvalid = 1;
6830 if (win->floating) {
6831 win->g = win->g_float;
6832 win->g.x += X(win->ws->r);
6833 win->g.y += Y(win->ws->r);
6846 configurenotify(xcb_configure_notify_event_t *e)
6850 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
6853 win = find_window(e->window);
6855 xcb_icccm_get_wm_normal_hints_reply(conn,
6856 xcb_icccm_get_wm_normal_hints(conn, win->id),
6865 destroynotify(xcb_destroy_notify_event_t *e)
6869 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
6871 if ((win = find_window(e->window)) == NULL) {
6872 if ((win = find_unmanaged_window(e->window)) == NULL)
6878 /* make sure we focus on something */
6881 unmanage_window(win);
6887 enternotify(xcb_enter_notify_event_t *e)
6890 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%x, mode: %d, detail: "
6891 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6892 "state: %d\n", e->event, e->mode, e->detail, e->root,
6893 e->child, YESNO(e->same_screen_focus), e->state);
6895 if (e->mode != XCB_NOTIFY_MODE_NORMAL) {
6896 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6901 switch (focus_mode) {
6902 case SWM_FOCUS_DEFAULT:
6904 case SWM_FOCUS_FOLLOW:
6906 case SWM_FOCUS_SYNERGY:
6910 if ((win = find_window(e->event)) == NULL) {
6911 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6920 /* lets us use one switch statement for arbitrary mode/detail combinations */
6921 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6924 mapnotify(xcb_map_notify_event_t *e)
6928 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
6930 win = manage_window(e->window);
6932 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
6935 * focus_win can only set input focus on a mapped window.
6936 * make sure the window really has focus since it is just being mapped.
6938 if (win->ws->focus == win)
6943 mappingnotify(xcb_mapping_notify_event_t *e)
6945 xcb_refresh_keyboard_mapping(syms, e);
6947 if (e->request == XCB_MAPPING_KEYBOARD)
6952 maprequest(xcb_map_request_event_t *e)
6955 struct swm_region *r;
6956 xcb_get_window_attributes_reply_t *war;
6958 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%x\n",
6961 war = xcb_get_window_attributes_reply(conn,
6962 xcb_get_window_attributes(conn, e->window),
6966 if (war->override_redirect) {
6972 win = manage_window(e->window);
6974 return; /* can't happen */
6979 /* make new win focused */
6980 r = root_to_region(win->wa->root);
6981 if (win->ws == r->ws)
6987 get_atom_name(xcb_atom_t atom)
6991 xcb_get_atom_name_reply_t *r;
6992 xcb_map_request_event_t mre;
6994 r = xcb_get_atom_name_reply(conn,
6995 xcb_get_atom_name(conn, atom),
6998 len = xcb_get_atom_name_name_length(r);
7000 name = malloc(len + 1);
7002 memcpy(name, xcb_get_atom_name_name(r), len);
7016 propertynotify(xcb_property_notify_event_t *e)
7022 name = get_atom_name(e->atom);
7023 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7024 e->window, name, e->atom);
7028 win = find_window(e->window);
7032 if (e->state == XCB_PROPERTY_DELETE && e->atom == a_swm_iconic) {
7033 update_iconic(win, 0);
7034 map_window_raised(win->id);
7041 case XCB_ATOM_WM_CLASS:
7042 case XCB_ATOM_WM_NAME:
7051 unmapnotify(xcb_unmap_notify_event_t *e)
7055 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7057 /* determine if we need to help unmanage this window */
7058 win = find_window(e->window);
7062 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7063 unmanage_window(win);
7066 /* resend unmap because we ated it */
7067 xcb_unmap_window(conn, e->window);
7073 visibilitynotify(xcb_visibility_notify_event_t *e)
7075 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7080 clientmessage(xcb_client_message_event_t *e)
7083 xcb_map_request_event_t mre;
7087 name = get_atom_name(e->type);
7088 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7089 e->window, name, e->type);
7092 win = find_window(e->window);
7095 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7096 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7097 "unmanaged window.\n");
7098 mre.window = e->window;
7104 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7105 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7108 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7109 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7110 if (win->can_delete)
7111 client_msg(win, a_delete);
7113 xcb_kill_client(conn, win->id);
7115 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7116 DNPRINTF(SWM_D_EVENT,
7117 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7118 if (win->floating) {
7119 if (e->data.data32[0] & (1<<8)) /* x */
7120 X(win) = e->data.data32[1];
7121 if (e->data.data32[0] & (1<<9)) /* y */
7122 Y(win) = e->data.data32[2];
7123 if (e->data.data32[0] & (1<<10)) /* width */
7124 WIDTH(win) = e->data.data32[3];
7125 if (e->data.data32[0] & (1<<11)) /* height */
7126 HEIGHT(win) = e->data.data32[4];
7131 /* TODO: Change stack sizes */
7132 /* notify no change was made. */
7133 config_win(win, NULL);
7136 if (e->type == ewmh[_NET_WM_STATE].atom) {
7137 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7138 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7139 if (e->data.data32[2])
7140 ewmh_update_win_state(win, e->data.data32[2],
7153 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7155 xcb_void_cookie_t wac;
7156 xcb_generic_error_t *error;
7158 /* this causes an error if some other window manager is running */
7159 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7160 for (i = 0; i < num_screens; i++) {
7162 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7164 wac = xcb_change_window_attributes_checked(conn, sc->root,
7165 XCB_CW_EVENT_MASK, &val);
7166 if ((error = xcb_request_check(conn, wac))) {
7167 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7178 new_region(struct swm_screen *s, int x, int y, int w, int h)
7180 struct swm_region *r, *n;
7181 struct workspace *ws = NULL;
7184 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7185 s->idx, w, h, x, y);
7187 /* remove any conflicting regions */
7188 n = TAILQ_FIRST(&s->rl);
7191 n = TAILQ_NEXT(r, entry);
7192 if (X(r) < (x + w) &&
7193 (X(r) + WIDTH(r)) > x &&
7195 (Y(r) + HEIGHT(r)) > y) {
7196 if (r->ws->r != NULL)
7197 r->ws->old_r = r->ws->r;
7200 TAILQ_REMOVE(&s->rl, r, entry);
7201 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7205 /* search old regions for one to reuse */
7207 /* size + location match */
7208 TAILQ_FOREACH(r, &s->orl, entry)
7209 if (X(r) == x && Y(r) == y &&
7210 HEIGHT(r) == h && WIDTH(r) == w)
7214 TAILQ_FOREACH(r, &s->orl, entry)
7215 if (HEIGHT(r) == h && WIDTH(r) == w)
7219 TAILQ_REMOVE(&s->orl, r, entry);
7220 /* try to use old region's workspace */
7221 if (r->ws->r == NULL)
7224 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7225 err(1, "new_region: calloc: failed to allocate memory "
7228 /* if we don't have a workspace already, find one */
7230 for (i = 0; i < workspace_limit; i++)
7231 if (s->ws[i].r == NULL) {
7238 errx(1, "new_region: no free workspaces");
7249 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7255 #ifdef SWM_XRR_HAS_CRTC
7258 #endif /* SWM_XRR_HAS_CRTC */
7259 struct swm_region *r;
7261 xcb_randr_get_screen_resources_current_cookie_t src;
7262 xcb_randr_get_screen_resources_current_reply_t *srr;
7263 xcb_randr_get_crtc_info_cookie_t cic;
7264 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7265 xcb_randr_crtc_t *crtc;
7266 xcb_screen_t *screen = get_screen(i);
7268 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7269 if (i >= num_screens)
7270 errx(1, "scan_xrandr: invalid screen");
7272 /* remove any old regions */
7273 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7274 r->ws->old_r = r->ws->r = NULL;
7276 TAILQ_REMOVE(&screens[i].rl, r, entry);
7277 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7281 /* map virtual screens onto physical screens */
7282 #ifdef SWM_XRR_HAS_CRTC
7283 if (xrandr_support) {
7284 src = xcb_randr_get_screen_resources_current(conn,
7286 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7289 new_region(&screens[i], 0, 0,
7290 screen->width_in_pixels,
7291 screen->height_in_pixels);
7294 ncrtc = srr->num_crtcs;
7295 for (c = 0; c < ncrtc; c++) {
7296 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7297 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7299 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7302 if (cir->num_outputs == 0) {
7308 new_region(&screens[i], 0, 0,
7309 screen->width_in_pixels,
7310 screen->height_in_pixels);
7312 new_region(&screens[i],
7313 cir->x, cir->y, cir->width, cir->height);
7318 #endif /* SWM_XRR_HAS_CRTC */
7320 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7321 screen->height_in_pixels);
7326 screenchange(xcb_randr_screen_change_notify_event_t *e)
7328 struct swm_region *r;
7331 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7333 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7334 /* silly event doesn't include the screen index */
7335 for (i = 0; i < num_screens; i++)
7336 if (screens[i].root == e->root)
7338 if (i >= num_screens)
7339 errx(1, "screenchange: screen not found");
7341 /* brute force for now, just re-enumerate the regions */
7345 print_win_geom(e->root);
7347 /* add bars to all regions */
7348 for (i = 0; i < num_screens; i++)
7349 TAILQ_FOREACH(r, &screens[i].rl, entry)
7357 xcb_window_t *wins = NULL;
7359 int i, j, num_screens;
7360 uint16_t state, manage;
7362 xcb_query_tree_cookie_t qtc;
7363 xcb_query_tree_reply_t *qtr;
7364 xcb_get_window_attributes_cookie_t c;
7365 xcb_get_window_attributes_reply_t *r;
7366 xcb_get_property_cookie_t pc;
7368 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7369 for (i = 0; i < num_screens; i++) {
7370 qtc = xcb_query_tree(conn, screens[i].root);
7371 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7374 wins = xcb_query_tree_children(qtr);
7375 no = xcb_query_tree_children_length(qtr);
7376 /* attach windows to a region */
7377 /* normal windows */
7378 for (j = 0; j < no; j++) {
7379 c = xcb_get_window_attributes(conn, wins[j]);
7380 r = xcb_get_window_attributes_reply(conn, c, NULL);
7383 if (r->override_redirect) {
7388 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7389 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7395 state = getstate(wins[j]);
7396 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7397 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7398 manage_window(wins[j]);
7401 /* transient windows */
7402 for (j = 0; j < no; j++) {
7403 c = xcb_get_window_attributes(conn, wins[j]);
7404 r = xcb_get_window_attributes_reply(conn, c, NULL);
7407 if (r->override_redirect) {
7413 state = getstate(wins[j]);
7414 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7415 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7416 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7417 &wins[j], NULL) && manage)
7418 manage_window(wins[j]);
7427 int i, j, k, num_screens;
7428 struct workspace *ws;
7429 uint32_t gcv[1], wa[1];
7430 const xcb_query_extension_reply_t *qep;
7431 xcb_cursor_t cursor;
7432 xcb_font_t cursor_font;
7433 xcb_randr_query_version_cookie_t c;
7434 xcb_randr_query_version_reply_t *r;
7436 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7437 if ((screens = calloc(num_screens,
7438 sizeof(struct swm_screen))) == NULL)
7439 err(1, "setup_screens: calloc: failed to allocate memory for "
7442 /* initial Xrandr setup */
7443 xrandr_support = False;
7444 c = xcb_randr_query_version(conn, True, True);
7445 r = xcb_randr_query_version_reply(conn, c, NULL);
7447 if (r->major_version >= 1)
7448 xrandr_support = True;
7451 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7452 xrandr_eventbase = qep->first_event;
7454 cursor_font = xcb_generate_id(conn);
7455 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7457 cursor = xcb_generate_id(conn);
7458 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7459 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7462 /* map physical screens */
7463 for (i = 0; i < num_screens; i++) {
7464 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7466 TAILQ_INIT(&screens[i].rl);
7467 TAILQ_INIT(&screens[i].orl);
7468 screens[i].root = get_screen(i)->root;
7470 /* set default colors */
7471 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7472 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7473 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7474 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7475 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7477 /* create graphics context on screen */
7478 screens[i].bar_gc = xcb_generate_id(conn);
7480 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7481 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7483 /* set default cursor */
7484 xcb_change_window_attributes(conn, screens[i].root,
7487 /* init all workspaces */
7488 /* XXX these should be dynamically allocated too */
7489 for (j = 0; j < SWM_WS_MAX; j++) {
7490 ws = &screens[i].ws[j];
7496 TAILQ_INIT(&ws->winlist);
7497 TAILQ_INIT(&ws->unmanagedlist);
7499 for (k = 0; layouts[k].l_stack != NULL; k++)
7500 if (layouts[k].l_config != NULL)
7501 layouts[k].l_config(ws,
7502 SWM_ARG_ID_STACKINIT);
7503 ws->cur_layout = &layouts[0];
7504 ws->cur_layout->l_string(ws);
7510 xcb_randr_select_input(conn, screens[i].root,
7511 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7513 xcb_free_cursor(conn, cursor);
7514 xcb_close_font(conn, cursor_font);
7520 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7521 err(1, "setup_globals: strdup: failed to allocate memory.");
7523 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7524 err(1, "setup_globals: strdup: failed to allocate memory.");
7526 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
7527 errx(1, "unable to allocate key symbols");
7529 a_state = get_atom_from_string("WM_STATE");
7530 a_prot = get_atom_from_string("WM_PROTOCOLS");
7531 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
7532 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7533 a_wmname = get_atom_from_string("WM_NAME");
7534 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7535 a_utf8_string = get_atom_from_string("UTF8_STRING");
7536 a_string = get_atom_from_string("STRING");
7537 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7544 xcb_atom_t netwmcheck, netwmname, utf8_string;
7545 xcb_window_t root, win;
7548 /* work around sun jdk bugs, code from wmname */
7549 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7550 netwmname = get_atom_from_string("_NET_WM_NAME");
7551 utf8_string = get_atom_from_string("UTF8_STRING");
7553 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7554 for (i = 0; i < num_screens; i++) {
7555 root = screens[i].root;
7557 win = xcb_generate_id(conn);
7558 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7559 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7560 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, 0, 0, 0, 1,
7561 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
7562 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7564 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7565 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7566 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7567 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7568 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7569 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7574 event_error(xcb_generic_error_t *e)
7576 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
7577 "resource_id: %u, minor_code: %u\n",
7578 xcb_event_get_error_label(e->error_code), e->error_code,
7579 xcb_event_get_request_label(e->major_code), e->major_code,
7580 e->sequence, e->resource_id, e->minor_code);
7584 event_handle(xcb_generic_event_t *evt)
7586 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
7588 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d)\n",
7589 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
7590 XCB_EVENT_RESPONSE_TYPE(evt));
7593 #define EVENT(type, callback) case type: callback((void *)evt); return
7594 EVENT(0, event_error);
7595 EVENT(XCB_BUTTON_PRESS, buttonpress);
7596 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7597 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7598 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7599 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
7600 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7601 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
7602 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
7603 /*EVENT(XCB_CREATE_NOTIFY, );*/
7604 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
7605 EVENT(XCB_ENTER_NOTIFY, enternotify);
7606 EVENT(XCB_EXPOSE, expose);
7607 /*EVENT(XCB_FOCUS_IN, );*/
7608 /*EVENT(XCB_FOCUS_OUT, );*/
7609 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7610 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7611 EVENT(XCB_KEY_PRESS, keypress);
7612 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7613 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7614 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7615 EVENT(XCB_MAP_NOTIFY, mapnotify);
7616 EVENT(XCB_MAP_REQUEST, maprequest);
7617 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
7618 /*EVENT(XCB_MOTION_NOTIFY, );*/
7619 /*EVENT(XCB_NO_EXPOSURE, );*/
7620 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
7621 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7622 /*EVENT(XCB_RESIZE_REQUEST, );*/
7623 /*EVENT(XCB_SELECTION_CLEAR, );*/
7624 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7625 /*EVENT(XCB_SELECTION_REQUEST, );*/
7626 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
7627 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7630 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
7631 screenchange((void *)evt);
7635 main(int argc, char *argv[])
7637 struct swm_region *r, *rr;
7638 struct ws_win *winfocus = NULL;
7640 char conf[PATH_MAX], *cfile = NULL;
7642 int xfd, i, num_screens;
7643 struct sigaction sact;
7644 xcb_generic_event_t *evt;
7648 time_started = time(NULL);
7651 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7652 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
7653 warnx("no locale support");
7655 /* handle some signals */
7656 bzero(&sact, sizeof(sact));
7657 sigemptyset(&sact.sa_mask);
7659 sact.sa_handler = sighdlr;
7660 sigaction(SIGINT, &sact, NULL);
7661 sigaction(SIGQUIT, &sact, NULL);
7662 sigaction(SIGTERM, &sact, NULL);
7663 sigaction(SIGHUP, &sact, NULL);
7665 sact.sa_handler = sighdlr;
7666 sact.sa_flags = SA_NOCLDSTOP;
7667 sigaction(SIGCHLD, &sact, NULL);
7669 if (!(display = XOpenDisplay(0)))
7670 errx(1, "can not open display");
7672 conn = XGetXCBConnection(display);
7673 if (xcb_connection_has_error(conn))
7674 errx(1, "can not get XCB connection");
7676 XSetEventQueueOwner(display, XCBOwnsEventQueue);
7678 xcb_prefetch_extension_data(conn, &xcb_randr_id);
7679 xfd = xcb_get_file_descriptor(conn);
7681 /* look for local and global conf file */
7682 pwd = getpwuid(getuid());
7684 errx(1, "invalid user: %d", getuid());
7686 xcb_grab_server(conn);
7689 /* flush all events */
7690 while ((evt = xcb_poll_for_event(conn))) {
7691 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
7696 if (enable_wm() != 0)
7697 errx(1, "another window manager is currently running");
7708 for (i = 0; ; i++) {
7713 snprintf(conf, sizeof conf, "%s/.%s",
7714 pwd->pw_dir, SWM_CONF_FILE);
7718 snprintf(conf, sizeof conf, "/etc/%s",
7723 snprintf(conf, sizeof conf, "%s/.%s",
7724 pwd->pw_dir, SWM_CONF_FILE_OLD);
7728 snprintf(conf, sizeof conf, "/etc/%s",
7735 if (strlen(conf) && stat(conf, &sb) != -1)
7736 if (S_ISREG(sb.st_mode)) {
7743 /* load conf (if any) */
7745 conf_load(cfile, SWM_CONF_DEFAULT);
7748 /* set some values to work around bad programs */
7750 /* grab existing windows (before we build the bars) */
7753 if (getenv("SWM_STARTED") == NULL)
7754 setenv("SWM_STARTED", "YES", 1);
7756 /* setup all bars */
7757 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7758 for (i = 0; i < num_screens; i++)
7759 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7760 if (winfocus == NULL)
7761 winfocus = TAILQ_FIRST(&r->ws->winlist);
7770 xcb_ungrab_server(conn);
7774 while ((evt = xcb_poll_for_event(conn))) {
7781 /* if we are being restarted go focus on first window */
7783 rr = winfocus->ws->r;
7785 /* not a visible window */
7789 /* move pointer to first screen if multi screen */
7790 if (num_screens > 1 || outputs > 1)
7791 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7792 rr->s[0].root, 0, 0, 0, 0, X(rr),
7793 Y(rr) + (bar_enabled ? bar_height : 0));
7795 a.id = SWM_ARG_ID_FOCUSCUR;
7805 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7807 DNPRINTF(SWM_D_MISC, "select failed");
7808 if (restart_wm == 1)
7809 restart(NULL, NULL);
7810 if (search_resp == 1)
7823 for (i = 0; i < num_screens; ++i)
7824 if (screens[i].bar_gc != 0)
7825 xcb_free_gc(conn, screens[i].bar_gc);
7827 XFreeFontSet(display, bar_font);
7829 xcb_key_symbols_free(syms);
7831 xcb_disconnect(conn);