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.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_icccm.h>
97 #include <xcb/xcb_keysyms.h>
98 #include <X11/Xproto.h>
99 #include <X11/Xutil.h>
100 #include <X11/extensions/Xrandr.h>
101 #include <X11/extensions/XTest.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr = SPECTRWM_BUILDSTR;
112 static const char *buildstr = SPECTRWM_VERSION;
116 # error XRandR versions less than 1.0 are not supported
121 #define SWM_XRR_HAS_CRTC
125 #if defined(__OpenBSD__)
126 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
127 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
128 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
129 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
130 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
131 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
132 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
133 #define xcb_icccm_get_wm_name xcb_get_wm_name
134 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
135 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
136 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
141 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
142 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
143 #define SWM_D_MISC 0x0001
144 #define SWM_D_EVENT 0x0002
145 #define SWM_D_WS 0x0004
146 #define SWM_D_FOCUS 0x0008
147 #define SWM_D_MOVE 0x0010
148 #define SWM_D_STACK 0x0020
149 #define SWM_D_MOUSE 0x0040
150 #define SWM_D_PROP 0x0080
151 #define SWM_D_CLASS 0x0100
152 #define SWM_D_KEY 0x0200
153 #define SWM_D_QUIRK 0x0400
154 #define SWM_D_SPAWN 0x0800
155 #define SWM_D_EVENTQ 0x1000
156 #define SWM_D_CONF 0x2000
157 #define SWM_D_BAR 0x4000
159 u_int32_t swm_debug = 0
177 #define DPRINTF(x...)
178 #define DNPRINTF(n,x...)
181 #define LENGTH(x) (sizeof x / sizeof x[0])
182 #define MODKEY Mod1Mask
183 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
184 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
185 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
186 #define SWM_PROPLEN (16)
187 #define SWM_FUNCNAME_LEN (32)
188 #define SWM_KEYS_LEN (255)
189 #define SWM_QUIRK_LEN (64)
190 #define X(r) (r)->g.x
191 #define Y(r) (r)->g.y
192 #define WIDTH(r) (r)->g.w
193 #define HEIGHT(r) (r)->g.h
194 #define BORDER(w) (w->bordered ? border_width : 0)
195 #define MAX_X(r) ((r)->g.x + (r)->g.w)
196 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
197 #define SH_MIN(w) (w)->sh_mask & PMinSize
198 #define SH_MIN_W(w) (w)->sh.min_width
199 #define SH_MIN_H(w) (w)->sh.min_height
200 #define SH_MAX(w) (w)->sh_mask & PMaxSize
201 #define SH_MAX_W(w) (w)->sh.max_width
202 #define SH_MAX_H(w) (w)->sh.max_height
203 #define SH_INC(w) (w)->sh_mask & PResizeInc
204 #define SH_INC_W(w) (w)->sh.width_inc
205 #define SH_INC_H(w) (w)->sh.height_inc
206 #define SWM_MAX_FONT_STEPS (3)
207 #define WINID(w) ((w) ? (w)->id : 0)
208 #define YESNO(x) ((x) ? "yes" : "no")
210 #define SWM_FOCUS_DEFAULT (0)
211 #define SWM_FOCUS_SYNERGY (1)
212 #define SWM_FOCUS_FOLLOW (2)
214 #define SWM_CONF_DEFAULT (0)
215 #define SWM_CONF_KEYMAPPING (1)
218 #define SWM_LIB "/usr/local/lib/libswmhack.so"
225 xcb_atom_t takefocus;
227 xcb_atom_t a_netwmname;
228 xcb_atom_t a_utf8_string;
230 xcb_atom_t a_swm_iconic;
231 volatile sig_atomic_t running = 1;
232 volatile sig_atomic_t restart_wm = 0;
234 int last_focus_event = FocusOut;
235 int (*xerrorxlib)(Display *, XErrorEvent *);
239 int xrandr_eventbase;
240 unsigned int numlockmask = 0;
242 xcb_connection_t *conn;
245 int cycle_visible = 0;
247 int font_adjusted = 0;
248 unsigned int mod_key = MODKEY;
251 struct swm_region *search_r;
252 int select_list_pipe[2];
253 int select_resp_pipe[2];
255 volatile sig_atomic_t search_resp;
256 int search_resp_action;
258 struct search_window {
259 TAILQ_ENTRY(search_window) entry;
263 xcb_window_t indicator;
265 TAILQ_HEAD(search_winlist, search_window);
266 struct search_winlist search_wl;
271 SWM_SEARCH_UNICONIFY,
272 SWM_SEARCH_NAME_WORKSPACE,
273 SWM_SEARCH_SEARCH_WORKSPACE,
274 SWM_SEARCH_SEARCH_WINDOW
277 #define SWM_STACK_TOP (0)
278 #define SWM_STACK_BOTTOM (1)
279 #define SWM_STACK_ABOVE (2)
280 #define SWM_STACK_BELOW (3)
283 double dialog_ratio = 0.6;
285 #define SWM_BAR_MAX (256)
286 #define SWM_BAR_JUSTIFY_LEFT (0)
287 #define SWM_BAR_JUSTIFY_CENTER (1)
288 #define SWM_BAR_JUSTIFY_RIGHT (2)
289 #define SWM_BAR_OFFSET (4)
290 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
291 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
292 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
293 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
294 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
296 char *bar_argv[] = { NULL, NULL };
298 unsigned char bar_ext[SWM_BAR_MAX];
299 char bar_vertext[SWM_BAR_MAX];
301 sig_atomic_t bar_alarm = 0;
304 int bar_border_width = 1;
305 int bar_at_bottom = 0;
307 int bar_extra_running = 0;
310 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
311 char *bar_format = NULL;
312 int stack_enabled = 1;
313 int clock_enabled = 1;
314 int urgent_enabled = 0;
315 char *clock_format = NULL;
316 int title_name_enabled = 0;
317 int title_class_enabled = 0;
318 int window_name_enabled = 0;
319 int focus_mode = SWM_FOCUS_DEFAULT;
320 int focus_close = SWM_STACK_BELOW;
321 int focus_close_wrap = 1;
322 int focus_default = SWM_STACK_TOP;
323 int spawn_position = SWM_STACK_TOP;
324 int disable_border = 0;
325 int border_width = 1;
326 int verbose_layout = 0;
329 XFontSetExtents *bar_fs_extents;
333 #define SWM_MENU_FN (2)
334 #define SWM_MENU_NB (4)
335 #define SWM_MENU_NF (6)
336 #define SWM_MENU_SB (8)
337 #define SWM_MENU_SF (10)
339 /* layout manager data */
340 struct swm_geometry {
353 struct swm_geometry g;
356 /* virtual "screens" */
358 TAILQ_ENTRY(swm_region) entry;
359 struct swm_geometry g;
360 struct workspace *ws; /* current workspace on this region */
361 struct workspace *ws_prior; /* prior workspace on this region */
362 struct swm_screen *s; /* screen idx */
365 TAILQ_HEAD(swm_region_list, swm_region);
368 TAILQ_ENTRY(ws_win) entry;
370 xcb_window_t transient;
371 struct ws_win *child_trans; /* transient child window */
372 struct swm_geometry g; /* current geometry */
373 struct swm_geometry g_float; /* region coordinates */
374 int g_floatvalid; /* g_float geometry validity */
375 int floatmaxed; /* whether maxed by max_stack */
380 unsigned int ewmh_flags;
381 int font_size_boundary[SWM_MAX_FONT_STEPS];
387 unsigned long quirks;
388 struct workspace *ws; /* always valid */
389 struct swm_screen *s; /* always valid, never changes */
390 xcb_get_geometry_reply_t *wa;
393 xcb_get_wm_class_reply_t ch;
394 xcb_wm_hints_t hints;
396 TAILQ_HEAD(ws_win_list, ws_win);
400 TAILQ_ENTRY(pid_e) entry;
404 TAILQ_HEAD(pid_list, pid_e);
405 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
407 /* layout handlers */
409 void vertical_config(struct workspace *, int);
410 void vertical_stack(struct workspace *, struct swm_geometry *);
411 void horizontal_config(struct workspace *, int);
412 void horizontal_stack(struct workspace *, struct swm_geometry *);
413 void max_stack(struct workspace *, struct swm_geometry *);
414 void plain_stacker(struct workspace *);
415 void fancy_stacker(struct workspace *);
417 struct ws_win *find_window(xcb_window_t);
419 void grabbuttons(struct ws_win *, int);
420 void new_region(struct swm_screen *, int, int, int, int);
421 void unmanage_window(struct ws_win *);
422 uint16_t getstate(xcb_window_t);
424 int conf_load(char *, int);
427 void (*l_stack)(struct workspace *, struct swm_geometry *);
428 void (*l_config)(struct workspace *, int);
430 #define SWM_L_FOCUSPREV (1<<0)
431 #define SWM_L_MAPONFOCUS (1<<1)
432 void (*l_string)(struct workspace *);
434 /* stack, configure */
435 { vertical_stack, vertical_config, 0, plain_stacker },
436 { horizontal_stack, horizontal_config, 0, plain_stacker },
438 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
439 { NULL, NULL, 0, NULL },
442 /* position of max_stack mode in the layouts array, index into layouts! */
443 #define SWM_V_STACK (0)
444 #define SWM_H_STACK (1)
445 #define SWM_MAX_STACK (2)
447 #define SWM_H_SLICE (32)
448 #define SWM_V_SLICE (32)
450 /* define work spaces */
452 int idx; /* workspace index */
453 char *name; /* workspace name */
454 int always_raise; /* raise windows on focus */
455 struct layout *cur_layout; /* current layout handlers */
456 struct ws_win *focus; /* may be NULL */
457 struct ws_win *focus_prev; /* may be NULL */
458 struct swm_region *r; /* may be NULL */
459 struct swm_region *old_r; /* may be NULL */
460 struct ws_win_list winlist; /* list of windows in ws */
461 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
462 char stacker[10]; /* display stacker and layout */
466 int horizontal_msize;
468 int horizontal_stacks;
479 SWM_S_COLOR_BAR_BORDER,
480 SWM_S_COLOR_BAR_FONT,
486 /* physical screen mapping */
487 #define SWM_WS_MAX (22) /* hard limit */
488 int workspace_limit = 10; /* soft limit */
491 int idx; /* screen index */
492 struct swm_region_list rl; /* list of regions on this screen */
493 struct swm_region_list orl; /* list of old regions */
495 struct workspace ws[SWM_WS_MAX];
501 } c[SWM_S_COLOR_MAX];
503 xcb_gcontext_t bar_gc;
505 struct swm_screen *screens;
507 /* args to functions */
510 #define SWM_ARG_ID_FOCUSNEXT (0)
511 #define SWM_ARG_ID_FOCUSPREV (1)
512 #define SWM_ARG_ID_FOCUSMAIN (2)
513 #define SWM_ARG_ID_FOCUSCUR (4)
514 #define SWM_ARG_ID_SWAPNEXT (10)
515 #define SWM_ARG_ID_SWAPPREV (11)
516 #define SWM_ARG_ID_SWAPMAIN (12)
517 #define SWM_ARG_ID_MOVELAST (13)
518 #define SWM_ARG_ID_MASTERSHRINK (20)
519 #define SWM_ARG_ID_MASTERGROW (21)
520 #define SWM_ARG_ID_MASTERADD (22)
521 #define SWM_ARG_ID_MASTERDEL (23)
522 #define SWM_ARG_ID_FLIPLAYOUT (24)
523 #define SWM_ARG_ID_STACKRESET (30)
524 #define SWM_ARG_ID_STACKINIT (31)
525 #define SWM_ARG_ID_CYCLEWS_UP (40)
526 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
527 #define SWM_ARG_ID_CYCLESC_UP (42)
528 #define SWM_ARG_ID_CYCLESC_DOWN (43)
529 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
530 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
531 #define SWM_ARG_ID_STACKINC (50)
532 #define SWM_ARG_ID_STACKDEC (51)
533 #define SWM_ARG_ID_SS_ALL (60)
534 #define SWM_ARG_ID_SS_WINDOW (61)
535 #define SWM_ARG_ID_DONTCENTER (70)
536 #define SWM_ARG_ID_CENTER (71)
537 #define SWM_ARG_ID_KILLWINDOW (80)
538 #define SWM_ARG_ID_DELETEWINDOW (81)
539 #define SWM_ARG_ID_WIDTHGROW (90)
540 #define SWM_ARG_ID_WIDTHSHRINK (91)
541 #define SWM_ARG_ID_HEIGHTGROW (92)
542 #define SWM_ARG_ID_HEIGHTSHRINK (93)
543 #define SWM_ARG_ID_MOVEUP (100)
544 #define SWM_ARG_ID_MOVEDOWN (101)
545 #define SWM_ARG_ID_MOVELEFT (102)
546 #define SWM_ARG_ID_MOVERIGHT (103)
550 void focus(struct swm_region *, union arg *);
551 void focus_magic(struct ws_win *);
555 TAILQ_ENTRY(quirk) entry;
559 #define SWM_Q_FLOAT (1<<0) /* float this window */
560 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
561 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
562 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
563 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
564 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
566 TAILQ_HEAD(quirk_list, quirk);
567 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
570 * Supported EWMH hints should be added to
571 * both the enum and the ewmh array
576 _NET_MOVERESIZE_WINDOW,
577 _NET_WM_ACTION_CLOSE,
578 _NET_WM_ACTION_FULLSCREEN,
580 _NET_WM_ACTION_RESIZE,
581 _NET_WM_ALLOWED_ACTIONS,
584 _NET_WM_STATE_FULLSCREEN,
585 _NET_WM_STATE_HIDDEN,
586 _NET_WM_STATE_MAXIMIZED_HORZ,
587 _NET_WM_STATE_MAXIMIZED_VERT,
588 _NET_WM_STATE_SKIP_PAGER,
589 _NET_WM_STATE_SKIP_TASKBAR,
591 _NET_WM_WINDOW_TYPE_DIALOG,
592 _NET_WM_WINDOW_TYPE_DOCK,
593 _NET_WM_WINDOW_TYPE_NORMAL,
594 _NET_WM_WINDOW_TYPE_SPLASH,
595 _NET_WM_WINDOW_TYPE_TOOLBAR,
596 _NET_WM_WINDOW_TYPE_UTILITY,
597 _SWM_WM_STATE_MANUAL,
604 } ewmh[SWM_EWMH_HINT_MAX] = {
605 /* must be in same order as in the enum */
606 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
607 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
608 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
609 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
610 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
611 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
612 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
613 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
614 {"_NET_WM_STATE", XCB_ATOM_NONE},
615 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
616 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
617 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
618 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
619 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
620 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
621 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
622 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
623 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
624 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
625 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
626 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
627 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
628 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
629 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
632 void store_float_geom(struct ws_win *, struct swm_region *);
633 int floating_toggle_win(struct ws_win *);
634 void constrain_window(struct ws_win *, struct swm_region *, int);
635 void update_window(struct ws_win *);
636 void spawn_select(struct swm_region *, union arg *, char *, int *);
637 char *get_win_name(xcb_window_t);
638 xcb_atom_t get_atom_from_string(const char *);
639 void map_window_raised(xcb_window_t);
641 xcb_screen_t *get_screen(int);
642 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
645 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
647 unsigned int tmpr, tmpg, tmpb;
649 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
660 get_screen(int screen)
662 xcb_screen_iterator_t i;
664 i = xcb_setup_roots_iterator(xcb_get_setup(conn));
665 for (; i.rem; --screen, xcb_screen_next(&i))
675 xcb_get_input_focus_cookie_t c;
676 xcb_get_input_focus_reply_t *r;
679 c = xcb_get_input_focus(conn);
681 r = xcb_get_input_focus_reply(conn, c, NULL);
687 map_window_raised(xcb_window_t win)
689 uint32_t val = XCB_STACK_MODE_ABOVE;
691 xcb_configure_window(conn, win,
692 XCB_CONFIG_WINDOW_STACK_MODE, &val);
694 xcb_map_window(conn, win);
699 get_atom_from_string(const char *str)
701 xcb_intern_atom_cookie_t c;
702 xcb_intern_atom_reply_t *r;
705 c = xcb_intern_atom(conn, False, strlen(str), str);
706 r = xcb_intern_atom_reply(conn, c, NULL);
714 return (XCB_ATOM_NONE);
718 update_iconic(struct ws_win *win, int newv)
725 iprop = get_atom_from_string("_SWM_ICONIC");
726 if (iprop == XCB_ATOM_NONE)
730 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
731 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
733 xcb_delete_property(conn, win->id, iprop);
737 get_iconic(struct ws_win *win)
741 xcb_get_property_reply_t *pr = NULL;
743 iprop = get_atom_from_string("_SWM_ICONIC");
744 if (iprop == XCB_ATOM_NONE)
747 pr = xcb_get_property_reply(conn,
748 xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
753 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
755 v = *((int32_t *)xcb_get_property_value(pr));
766 int i, j, num_screens;
768 sup_list = get_atom_from_string("_NET_SUPPORTED");
770 for (i = 0; i < LENGTH(ewmh); i++)
771 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
773 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
774 for (i = 0; i < num_screens; i++) {
775 /* Support check window will be created by workaround(). */
777 /* Report supported atoms */
778 xcb_delete_property(conn, screens[i].root, sup_list);
779 for (j = 0; j < LENGTH(ewmh); j++)
780 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
781 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
790 xcb_atom_t sup_check, sup_list;
792 xcb_get_property_cookie_t pc;
793 xcb_get_property_reply_t *pr;
795 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
796 sup_list = get_atom_from_string("_NET_SUPPORTED");
797 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
799 for (i = 0; i < num_screens; i++) {
800 /* Get the support check window and destroy it */
801 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
802 XCB_ATOM_WINDOW, 0, 1);
803 pr = xcb_get_property_reply(conn, pc, NULL);
805 id = *((xcb_window_t *)xcb_get_property_value(pr));
807 xcb_destroy_window(conn, id);
808 xcb_delete_property(conn, screens[i].root, sup_check);
809 xcb_delete_property(conn, screens[i].root, sup_list);
817 ewmh_autoquirk(struct ws_win *win)
823 xcb_get_property_cookie_t c;
824 xcb_get_property_reply_t *r;
826 c = xcb_get_property(conn, False, win->id,
827 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
828 r = xcb_get_property_reply(conn, c, NULL);
831 n = xcb_get_property_value_length(r);
833 for (i = 0; i < n; i++) {
834 type = *((xcb_atom_t *)xcb_get_property_value(r));
835 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
837 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
838 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
839 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
841 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
844 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
845 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
847 win->quirks = SWM_Q_FLOAT;
854 #define SWM_EWMH_ACTION_COUNT_MAX (6)
855 #define EWMH_F_FULLSCREEN (1<<0)
856 #define EWMH_F_ABOVE (1<<1)
857 #define EWMH_F_HIDDEN (1<<2)
858 #define EWMH_F_SKIP_PAGER (1<<3)
859 #define EWMH_F_SKIP_TASKBAR (1<<4)
860 #define SWM_F_MANUAL (1<<5)
863 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
871 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
872 "fullscreen %s\n", win->id, YESNO(fs));
875 if (!win->g_floatvalid)
876 store_float_geom(win, win->ws->r);
878 win->g = win->ws->r->g;
881 if (win->g_floatvalid) {
882 /* refloat at last floating relative position */
883 win->g = win->g_float;
884 X(win) += X(win->ws->r);
885 Y(win) += Y(win->ws->r);
893 ewmh_update_actions(struct ws_win *win)
895 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
901 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
904 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
905 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
908 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
909 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1,
913 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
914 #define _NET_WM_STATE_ADD 1 /* add/set property */
915 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
918 ewmh_update_win_state(struct ws_win *win, long state, long action)
920 unsigned int mask = 0;
921 unsigned int changed = 0;
922 unsigned int orig_flags;
927 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
928 mask = EWMH_F_FULLSCREEN;
929 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
931 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
933 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
934 mask = EWMH_F_SKIP_PAGER;
935 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
936 mask = EWMH_F_SKIP_TASKBAR;
939 orig_flags = win->ewmh_flags;
942 case _NET_WM_STATE_REMOVE:
943 win->ewmh_flags &= ~mask;
945 case _NET_WM_STATE_ADD:
946 win->ewmh_flags |= mask;
948 case _NET_WM_STATE_TOGGLE:
949 win->ewmh_flags ^= mask;
953 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
955 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
957 if (!floating_toggle_win(win))
958 win->ewmh_flags = orig_flags; /* revert */
959 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
961 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
962 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
964 if (!ewmh_set_win_fullscreen(win,
965 win->ewmh_flags & EWMH_F_FULLSCREEN))
966 win->ewmh_flags = orig_flags; /* revert */
968 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
970 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
971 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
972 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
973 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
974 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
975 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
976 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
977 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
978 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
979 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
980 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
981 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
982 if (win->ewmh_flags & EWMH_F_ABOVE)
983 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
984 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
985 &ewmh[_NET_WM_STATE_ABOVE].atom);
986 if (win->ewmh_flags & SWM_F_MANUAL)
987 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
988 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
989 &ewmh[_SWM_WM_STATE_MANUAL].atom);
993 ewmh_get_win_state(struct ws_win *win)
996 xcb_get_property_cookie_t c;
997 xcb_get_property_reply_t *r;
1003 win->ewmh_flags = 0;
1005 win->ewmh_flags |= EWMH_F_ABOVE;
1007 win->ewmh_flags |= SWM_F_MANUAL;
1009 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1010 XCB_ATOM_ATOM, 0, (~0L));
1011 r = xcb_get_property_reply(conn, c, NULL);
1015 states = xcb_get_property_value(r);
1016 n = xcb_get_property_value_length(r);
1018 for (i = 0; i < n; i++)
1019 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1027 geteventname(XEvent *e)
1036 name = "KeyRelease";
1039 name = "ButtonPress";
1042 name = "ButtonRelease";
1045 name = "MotionNotify";
1048 name = "EnterNotify";
1051 name = "LeaveNotify";
1060 name = "KeymapNotify";
1065 case GraphicsExpose:
1066 name = "GraphicsExpose";
1071 case VisibilityNotify:
1072 name = "VisibilityNotify";
1075 name = "CreateNotify";
1078 name = "DestroyNotify";
1081 name = "UnmapNotify";
1087 name = "MapRequest";
1089 case ReparentNotify:
1090 name = "ReparentNotify";
1092 case ConfigureNotify:
1093 name = "ConfigureNotify";
1095 case ConfigureRequest:
1096 name = "ConfigureRequest";
1099 name = "GravityNotify";
1102 name = "ResizeRequest";
1104 case CirculateNotify:
1105 name = "CirculateNotify";
1107 case CirculateRequest:
1108 name = "CirculateRequest";
1110 case PropertyNotify:
1111 name = "PropertyNotify";
1113 case SelectionClear:
1114 name = "SelectionClear";
1116 case SelectionRequest:
1117 name = "SelectionRequest";
1119 case SelectionNotify:
1120 name = "SelectionNotify";
1122 case ColormapNotify:
1123 name = "ColormapNotify";
1126 name = "ClientMessage";
1129 name = "MappingNotify";
1139 xrandr_geteventname(XEvent *e)
1143 switch(e->type - xrandr_eventbase) {
1144 case RRScreenChangeNotify:
1145 name = "RRScreenChangeNotify";
1155 dumpwins(struct swm_region *r, union arg *args)
1159 xcb_get_window_attributes_cookie_t c;
1160 xcb_get_window_attributes_reply_t *wa;
1162 if (r->ws == NULL) {
1163 warnx("dumpwins: invalid workspace");
1167 warnx("=== managed window list ws %02d ===", r->ws->idx);
1168 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1169 state = getstate(win->id);
1170 c = xcb_get_window_attributes(conn, win->id);
1171 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1173 warnx("window: 0x%x, map_state: %d, state: %u, "
1174 "transient: 0x%x", win->id, wa->map_state,
1175 state, win->transient);
1178 warnx("window: 0x%x, failed xcb_get_window_attributes",
1182 warnx("===== unmanaged window list =====");
1183 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1184 state = getstate(win->id);
1185 c = xcb_get_window_attributes(conn, win->id);
1186 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1188 warnx("window: 0x%x, map_state: %d, state: %u, "
1189 "transient: 0x%x", win->id, wa->map_state,
1190 state, win->transient);
1193 warnx("window: 0x%x, failed xcb_get_window_attributes",
1197 warnx("=================================");
1201 dumpwins(struct swm_region *r, union arg *args)
1204 #endif /* SWM_DEBUG */
1206 void expose(XEvent *);
1207 void keypress(XEvent *);
1208 void buttonpress(XEvent *);
1209 void configurerequest(XEvent *);
1210 void configurenotify(XEvent *);
1211 void destroynotify(XEvent *);
1212 void enternotify(XEvent *);
1213 void focusevent(XEvent *);
1214 void mapnotify(XEvent *);
1215 void mappingnotify(XEvent *);
1216 void maprequest(XEvent *);
1217 void propertynotify(XEvent *);
1218 void unmapnotify(XEvent *);
1219 void visibilitynotify(XEvent *);
1220 void clientmessage(XEvent *);
1222 void (*handler[LASTEvent])(XEvent *) = {
1224 [KeyPress] = keypress,
1225 [ButtonPress] = buttonpress,
1226 [ConfigureRequest] = configurerequest,
1227 [ConfigureNotify] = configurenotify,
1228 [DestroyNotify] = destroynotify,
1229 [EnterNotify] = enternotify,
1230 [FocusIn] = focusevent,
1231 [FocusOut] = focusevent,
1232 [MapNotify] = mapnotify,
1233 [MappingNotify] = mappingnotify,
1234 [MapRequest] = maprequest,
1235 [PropertyNotify] = propertynotify,
1236 [UnmapNotify] = unmapnotify,
1237 [VisibilityNotify] = visibilitynotify,
1238 [ClientMessage] = clientmessage,
1244 int saved_errno, status;
1247 saved_errno = errno;
1251 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1256 if (errno != ECHILD)
1257 warn("sighdlr: waitpid");
1258 #endif /* SWM_DEBUG */
1261 if (pid == searchpid)
1265 if (WIFEXITED(status)) {
1266 if (WEXITSTATUS(status) != 0)
1267 warnx("sighdlr: child exit status: %d",
1268 WEXITSTATUS(status));
1270 warnx("sighdlr: child is terminated "
1272 #endif /* SWM_DEBUG */
1286 errno = saved_errno;
1292 struct pid_e *p = NULL;
1294 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1299 TAILQ_FOREACH(p, &pidlist, entry) {
1308 name_to_color(const char *colorname)
1310 uint32_t result = 0;
1311 char cname[32] = "#";
1312 xcb_screen_t *screen;
1313 xcb_colormap_t cmap;
1314 xcb_alloc_color_reply_t *cr;
1315 xcb_alloc_named_color_reply_t *nr;
1316 uint16_t rr, gg, bb;
1318 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1319 cmap = screen->default_colormap;
1321 /* color is in format rgb://rr/gg/bb */
1322 if (strncmp(colorname, "rgb:", 4) == 0) {
1323 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1324 warnx("could not parse rgb %s", colorname);
1326 cr = xcb_alloc_color_reply(conn,
1327 xcb_alloc_color(conn, cmap, rr, gg, bb),
1333 warnx("color '%s' not found", colorname);
1336 nr = xcb_alloc_named_color_reply(conn,
1337 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1341 strlcat(cname, colorname + 2, sizeof cname - 1);
1342 nr = xcb_alloc_named_color_reply(conn,
1343 xcb_alloc_named_color(conn, cmap, strlen(cname),
1351 warnx("color '%s' not found", colorname);
1358 setscreencolor(char *val, int i, int c)
1362 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1363 if (i > 0 && i <= num_screens) {
1364 screens[i - 1].c[c].color = name_to_color(val);
1365 free(screens[i - 1].c[c].name);
1366 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1368 } else if (i == -1) {
1369 for (i = 0; i < num_screens; i++) {
1370 screens[i].c[c].color = name_to_color(val);
1371 free(screens[i].c[c].name);
1372 if ((screens[i].c[c].name = strdup(val)) == NULL)
1376 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1381 fancy_stacker(struct workspace *ws)
1383 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1384 if (ws->cur_layout->l_stack == vertical_stack)
1385 snprintf(ws->stacker, sizeof ws->stacker,
1386 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1387 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1388 if (ws->cur_layout->l_stack == horizontal_stack)
1389 snprintf(ws->stacker, sizeof ws->stacker,
1390 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1391 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1395 plain_stacker(struct workspace *ws)
1397 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1398 if (ws->cur_layout->l_stack == vertical_stack)
1399 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1400 sizeof ws->stacker);
1401 if (ws->cur_layout->l_stack == horizontal_stack)
1402 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1403 sizeof ws->stacker);
1407 custom_region(char *val)
1409 unsigned int sidx, x, y, w, h;
1411 xcb_screen_t *screen;
1413 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1414 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1415 errx(1, "invalid custom region, "
1416 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1417 if (sidx < 1 || sidx > num_screens)
1418 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1422 screen = get_screen(sidx);
1424 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1426 if (x > screen->width_in_pixels ||
1427 y > screen->height_in_pixels ||
1428 w + x > screen->width_in_pixels ||
1429 h + y > screen->height_in_pixels) {
1430 warnx("ignoring region %ux%u+%u+%u - not within screen "
1431 "boundaries (%ux%u)", w, h, x, y,
1432 screen->width_in_pixels, screen->height_in_pixels);
1436 new_region(&screens[sidx], x, y, w, h);
1440 socket_setnonblock(int fd)
1444 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1445 err(1, "fcntl F_GETFL");
1446 flags |= O_NONBLOCK;
1447 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1448 err(1, "fcntl F_SETFL");
1452 bar_print(struct swm_region *r, const char *s)
1456 xcb_rectangle_t rect;
1458 XRectangle ibox, lbox;
1461 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1463 switch (bar_justify) {
1464 case SWM_BAR_JUSTIFY_LEFT:
1467 case SWM_BAR_JUSTIFY_CENTER:
1468 x = (WIDTH(r) - lbox.width) / 2;
1470 case SWM_BAR_JUSTIFY_RIGHT:
1471 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1475 if (x < SWM_BAR_OFFSET)
1480 rect.width = WIDTH(r->bar);
1481 rect.height = HEIGHT(r->bar);
1483 /* clear back buffer */
1484 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
1485 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1486 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc,
1487 sizeof(rect), &rect);
1489 /* draw back buffer */
1490 gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
1491 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1492 xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
1493 (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1497 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1498 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1502 bar_extra_stop(void)
1506 bzero(bar_pipe, sizeof bar_pipe);
1509 kill(bar_pid, SIGTERM);
1512 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1517 bar_class_name(char *s, size_t sz, struct swm_region *r)
1519 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1521 if (r->ws->focus->ch.class_name != NULL)
1522 strlcat(s, r->ws->focus->ch.class_name, sz);
1526 bar_title_name(char *s, size_t sz, struct swm_region *r)
1528 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1530 if (r->ws->focus->ch.instance_name != NULL)
1531 strlcat(s, r->ws->focus->ch.instance_name, sz);
1535 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1537 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1540 bar_class_name(s, sz, r);
1541 strlcat(s, ":", sz);
1542 bar_title_name(s, sz, r);
1546 bar_window_float(char *s, size_t sz, struct swm_region *r)
1548 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1550 if (r->ws->focus->floating)
1551 strlcat(s, "(f)", sz);
1555 bar_window_name(char *s, size_t sz, struct swm_region *r)
1559 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1561 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1564 strlcat(s, title, sz);
1568 int urgent[SWM_WS_MAX];
1570 bar_urgent(char *s, size_t sz)
1573 int i, j, num_screens;
1575 xcb_get_property_cookie_t c;
1576 xcb_icccm_wm_hints_t hints;
1578 for (i = 0; i < workspace_limit; i++)
1581 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1582 for (i = 0; i < num_screens; i++)
1583 for (j = 0; j < workspace_limit; j++)
1584 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1585 c = xcb_icccm_get_wm_hints(conn, win->id);
1586 if (xcb_icccm_get_wm_hints_reply(conn, c,
1589 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1593 for (i = 0; i < workspace_limit; i++) {
1595 snprintf(b, sizeof b, "%d ", i + 1);
1597 snprintf(b, sizeof b, "- ");
1603 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1605 if (r == NULL || r->ws == NULL)
1607 if (r->ws->name != NULL)
1608 strlcat(s, r->ws->name, sz);
1611 /* build the default bar format according to the defined enabled options */
1613 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1615 /* if format provided, just copy the buffers */
1616 if (bar_format != NULL) {
1617 strlcpy(fmtnew, fmtexp, sz);
1621 /* reset the output buffer */
1624 strlcat(fmtnew, "+N:+I ", sz);
1626 strlcat(fmtnew, "+S", sz);
1627 strlcat(fmtnew, " ", sz);
1629 /* only show the workspace name if there's actually one */
1630 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1631 strlcat(fmtnew, "<+D>", sz);
1632 strlcat(fmtnew, "+3<", sz);
1634 if (clock_enabled) {
1635 strlcat(fmtnew, fmtexp, sz);
1636 strlcat(fmtnew, "+4<", sz);
1639 /* bar_urgent already adds the space before the last asterisk */
1641 strlcat(fmtnew, "* +U*+4<", sz);
1643 if (title_class_enabled) {
1644 strlcat(fmtnew, "+C", sz);
1645 if (title_name_enabled == 0)
1646 strlcat(fmtnew, "+4<", sz);
1649 /* checks needed by the colon and floating strlcat(3) calls below */
1650 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1651 if (title_name_enabled) {
1652 if (title_class_enabled)
1653 strlcat(fmtnew, ":", sz);
1654 strlcat(fmtnew, "+T+4<", sz);
1656 if (window_name_enabled) {
1657 if (r->ws->focus->floating)
1658 strlcat(fmtnew, "+F ", sz);
1659 strlcat(fmtnew, "+64W ", sz);
1663 /* finally add the action script output and the version */
1664 strlcat(fmtnew, "+4<+A+4<+V", sz);
1668 bar_replace_pad(char *tmp, int *limit, size_t sz)
1670 /* special case; no limit given, pad one space, instead */
1671 if (*limit == sz - 1)
1673 snprintf(tmp, sz, "%*s", *limit, " ");
1676 /* replaces the bar format character sequences (like in tmux(1)) */
1678 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1682 char tmp[SWM_BAR_MAX];
1686 /* reset strlcat(3) buffer */
1689 /* get number, if any */
1692 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1693 limit = sizeof tmp - 1;
1694 if (limit <= 0 || limit >= sizeof tmp)
1695 limit = sizeof tmp - 1;
1697 /* there is nothing to replace (ie EOL) */
1704 bar_replace_pad(tmp, &limit, sizeof tmp);
1707 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1710 bar_class_name(tmp, sizeof tmp, r);
1713 bar_workspace_name(tmp, sizeof tmp, r);
1716 bar_window_float(tmp, sizeof tmp, r);
1719 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1722 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1725 bar_class_title_name(tmp, sizeof tmp, r);
1728 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1731 bar_title_name(tmp, sizeof tmp, r);
1734 bar_urgent(tmp, sizeof tmp);
1737 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1740 bar_window_name(tmp, sizeof tmp, r);
1743 /* unknown character sequence; copy as-is */
1744 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1752 while (limit-- > 0) {
1753 if (*offrep >= sz - 1)
1755 fmtrep[(*offrep)++] = *ptr++;
1763 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1768 while (*fmt != '\0') {
1770 /* skip ordinary characters */
1773 fmtrep[off++] = *fmt++;
1777 /* character sequence found; replace it */
1778 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1787 bar_fmt_expand(char *fmtexp, size_t sz)
1794 /* start by grabbing the current time and date */
1796 localtime_r(&tmt, &tm);
1798 /* figure out what to expand */
1799 if (bar_format != NULL)
1801 else if (bar_format == NULL && clock_enabled)
1803 /* if nothing to expand bail out */
1809 /* copy as-is, just in case the format shouldn't be expanded below */
1810 strlcpy(fmtexp, fmt, sz);
1811 /* finally pass the string through strftime(3) */
1812 #ifndef SWM_DENY_CLOCK_FORMAT
1813 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1814 warnx("format too long");
1822 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1823 char fmtrep[SWM_BAR_MAX];
1825 struct swm_region *r;
1827 /* expand the format by first passing it through strftime(3) */
1828 bar_fmt_expand(fmtexp, sizeof fmtexp);
1830 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1831 for (i = 0; i < num_screens; i++) {
1832 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1835 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1836 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1837 bar_print(r, fmtrep);
1848 if (bar_enabled == 0)
1850 if (bar_extra && bar_extra_running) {
1851 /* ignore short reads; it'll correct itself */
1852 while ((b = fgetln(stdin, &len)) != NULL)
1853 if (b && b[len - 1] == '\n') {
1855 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1857 if (b == NULL && errno != EAGAIN) {
1858 warn("bar_update: bar_extra failed");
1862 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1875 bar_toggle(struct swm_region *r, union arg *args)
1877 struct swm_region *tmpr;
1880 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1882 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1884 for (i = 0; i < num_screens; i++)
1885 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1887 xcb_unmap_window(conn, tmpr->bar->id);
1889 for (i = 0; i < num_screens; i++)
1890 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1892 map_window_raised(tmpr->bar->id);
1895 bar_enabled = !bar_enabled;
1898 /* must be after stack */
1905 struct swm_region *r;
1909 /* do this here because the conf file is in memory */
1910 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1911 /* launch external status app */
1912 bar_extra_running = 1;
1913 if (pipe(bar_pipe) == -1)
1914 err(1, "pipe error");
1915 socket_setnonblock(bar_pipe[0]);
1916 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1917 if (dup2(bar_pipe[0], 0) == -1)
1919 if (dup2(bar_pipe[1], 1) == -1)
1921 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1922 err(1, "could not disable SIGPIPE");
1923 switch (bar_pid = fork()) {
1925 err(1, "cannot fork");
1929 execvp(bar_argv[0], bar_argv);
1930 err(1, "%s external app failed", bar_argv[0]);
1932 default: /* parent */
1938 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1939 for (i = 0; i < num_screens; i++)
1940 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1943 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1944 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1945 xcb_change_window_attributes(conn, r->bar->id,
1946 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1952 bar_setup(struct swm_region *r)
1954 char *default_string;
1955 char **missing_charsets;
1956 int num_missing_charsets = 0;
1958 xcb_screen_t *screen = get_screen(r->s->idx);
1962 XFreeFontSet(display, bar_fs);
1966 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1967 err(1, "bar_setup: calloc: failed to allocate memory.");
1969 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1971 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1972 &num_missing_charsets, &default_string);
1974 if (num_missing_charsets > 0) {
1975 warnx("Unable to load charset(s):");
1977 for (i = 0; i < num_missing_charsets; ++i)
1978 warnx("%s", missing_charsets[i]);
1980 XFreeStringList(missing_charsets);
1982 if (strcmp(default_string, ""))
1983 warnx("Glyphs from those sets will be replaced "
1984 "by '%s'.", default_string);
1986 warnx("Glyphs from those sets won't be drawn.");
1990 errx(1, "Error creating font set structure.");
1992 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1994 bar_height = bar_fs_extents->max_logical_extent.height +
1995 2 * bar_border_width;
2001 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2002 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2003 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2005 r->bar->id = xcb_generate_id(conn);
2006 wa[0] = r->s->c[SWM_S_COLOR_BAR].color;
2007 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].color;
2008 xcb_create_window(conn, screen->root_depth, r->bar->id, r->s->root,
2009 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2010 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2011 screen->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL,
2014 r->bar->buffer = xcb_generate_id(conn);
2015 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2016 WIDTH(r->bar), HEIGHT(r->bar));
2018 xcb_randr_select_input(conn, r->bar->id,
2019 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2022 map_window_raised(r->bar->id);
2024 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2025 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2028 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2029 err(1, "could not install bar_signal");
2034 bar_cleanup(struct swm_region *r)
2038 xcb_destroy_window(conn, r->bar->id);
2039 xcb_free_pixmap(conn, r->bar->buffer);
2045 drain_enter_notify(void)
2049 while (xcb_poll_for_event(conn))
2052 DNPRINTF(SWM_D_EVENT, "drain_enter_notify: drained: %d\n", i);
2056 set_win_state(struct ws_win *win, uint16_t state)
2058 uint16_t data[2] = { state, XCB_ATOM_NONE };
2060 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2065 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
2066 astate, 32, 2, data);
2070 getstate(xcb_window_t w)
2072 uint16_t result = 0;
2073 xcb_get_property_cookie_t c;
2074 xcb_get_property_reply_t *r;
2076 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
2077 r = xcb_get_property_reply(conn, c, NULL);
2080 result = *((uint16_t *)xcb_get_property_value(r));
2088 version(struct swm_region *r, union arg *args)
2090 bar_version = !bar_version;
2092 snprintf(bar_vertext, sizeof bar_vertext,
2093 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2095 strlcpy(bar_vertext, "", sizeof bar_vertext);
2100 client_msg(struct ws_win *win, xcb_atom_t a)
2102 xcb_client_message_event_t ev;
2107 bzero(&ev, sizeof ev);
2108 ev.response_type = XCB_CLIENT_MESSAGE;
2109 ev.window = win->id;
2112 ev.data.data32[0] = a;
2113 ev.data.data32[1] = XCB_CURRENT_TIME;
2115 xcb_send_event(conn, False, win->id,
2116 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2120 /* synthetic response to a ConfigureRequest when not making a change */
2122 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
2124 xcb_configure_notify_event_t ce;
2129 /* send notification of unchanged state. */
2130 bzero(&ce, sizeof(ce));
2131 ce.response_type = XCB_CONFIGURE_NOTIFY;
2134 ce.width = WIDTH(win);
2135 ce.height = HEIGHT(win);
2136 ce.override_redirect = False;
2141 ce.window = win->id;
2142 ce.border_width = BORDER(win);
2143 ce.above_sibling = XCB_WINDOW_NONE;
2146 ce.event = ev->window;
2147 ce.window = ev->window;
2149 /* make response appear more WM_SIZE_HINTS-compliant */
2151 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2152 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2153 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
2154 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2155 SH_INC_W(win), SH_INC_H(win));
2159 /* the hint may be set... to 0! */
2160 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2161 ce.width = SH_MIN_W(win);
2162 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2163 ce.height = SH_MIN_H(win);
2168 /* may also be advertized as 0 */
2169 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2170 ce.width = SH_MAX_W(win);
2171 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2172 ce.height = SH_MAX_H(win);
2175 /* resize increment. */
2177 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2178 ce.width -= (ce.width - SH_MIN_W(win)) %
2180 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2181 ce.height -= (ce.height - SH_MIN_H(win)) %
2185 /* adjust x and y for requested border_width. */
2186 ce.x += BORDER(win) - ev->border_width;
2187 ce.y += BORDER(win) - ev->border_width;
2188 ce.border_width = ev->border_width;
2189 ce.above_sibling = ev->above;
2192 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2193 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2194 ce.y, ce.width, ce.height, ce.border_width);
2196 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2202 count_win(struct workspace *ws, int count_transient)
2207 TAILQ_FOREACH(win, &ws->winlist, entry) {
2208 if (count_transient == 0 && win->floating)
2210 if (count_transient == 0 && win->transient)
2216 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2222 quit(struct swm_region *r, union arg *args)
2224 DNPRINTF(SWM_D_MISC, "quit\n");
2229 unmap_window(struct ws_win *win)
2234 /* don't unmap again */
2235 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2238 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2240 xcb_unmap_window(conn, win->id);
2241 xcb_change_window_attributes(conn, win->id,
2242 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2249 int i, j, num_screens;
2251 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2252 for (i = 0; i < num_screens; i++)
2253 for (j = 0; j < workspace_limit; j++)
2254 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2259 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2261 xcb_key_press_event_t event;
2262 xcb_key_symbols_t *syms;
2263 xcb_keycode_t *keycode;
2268 syms = xcb_key_symbols_alloc(conn);
2269 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2271 event.event = win->id;
2272 event.root = win->s->root;
2273 event.child = XCB_WINDOW_NONE;
2274 event.time = XCB_CURRENT_TIME;
2275 event.event_x = X(win);
2276 event.event_y = Y(win);
2279 event.same_screen = True;
2280 event.detail = *keycode;
2281 event.state = modifiers;
2283 event.response_type = XCB_KEY_PRESS;
2284 xcb_send_event(conn, True, win->id,
2285 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2287 event.response_type = XCB_KEY_RELEASE;
2288 xcb_send_event(conn, True, win->id,
2289 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2292 xcb_key_symbols_free(syms);
2296 restart(struct swm_region *r, union arg *args)
2298 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2300 /* disable alarm because the following code may not be interrupted */
2302 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2303 err(1, "can't disable alarm");
2308 XCloseDisplay(display);
2309 execvp(start_argv[0], start_argv);
2310 warn("execvp failed");
2315 root_to_region(xcb_window_t root)
2317 struct swm_region *r = NULL;
2319 xcb_query_pointer_reply_t *qpr;
2321 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2323 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2324 for (i = 0; i < num_screens; i++)
2325 if (screens[i].root == root)
2328 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2329 screens[i].root), NULL);
2332 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2333 qpr->root_x, qpr->root_y);
2334 /* choose a region based on pointer location */
2335 TAILQ_FOREACH(r, &screens[i].rl, entry)
2336 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2337 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2343 r = TAILQ_FIRST(&screens[i].rl);
2349 find_unmanaged_window(xcb_window_t id)
2352 int i, j, num_screens;
2354 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2355 for (i = 0; i < num_screens; i++)
2356 for (j = 0; j < workspace_limit; j++)
2357 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2365 find_window(xcb_window_t id)
2368 int i, j, num_screens;
2369 xcb_query_tree_cookie_t c;
2370 xcb_query_tree_reply_t *r;
2372 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2373 for (i = 0; i < num_screens; i++)
2374 for (j = 0; j < workspace_limit; j++)
2375 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2379 c = xcb_query_tree(conn, id);
2380 r = xcb_query_tree_reply(conn, c, NULL);
2384 /* if we were looking for the parent return that window instead */
2385 if (r->parent == 0 || r->root == r->parent)
2388 /* look for parent */
2389 for (i = 0; i < num_screens; i++)
2390 for (j = 0; j < workspace_limit; j++)
2391 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2392 if (r->parent == win->id) {
2402 spawn(int ws_idx, union arg *args, int close_fd)
2407 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2410 close(xcb_get_file_descriptor(conn));
2412 setenv("LD_PRELOAD", SWM_LIB, 1);
2414 if (asprintf(&ret, "%d", ws_idx) == -1) {
2415 warn("spawn: asprintf SWM_WS");
2418 setenv("_SWM_WS", ret, 1);
2422 if (asprintf(&ret, "%d", getpid()) == -1) {
2423 warn("spawn: asprintf _SWM_PID");
2426 setenv("_SWM_PID", ret, 1);
2430 if (setsid() == -1) {
2431 warn("spawn: setsid");
2437 * close stdin and stdout to prevent interaction between apps
2438 * and the baraction script
2439 * leave stderr open to record errors
2441 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2442 warn("spawn: open");
2445 dup2(fd, STDIN_FILENO);
2446 dup2(fd, STDOUT_FILENO);
2451 execvp(args->argv[0], args->argv);
2453 warn("spawn: execvp");
2458 kill_refs(struct ws_win *win)
2460 int i, x, num_screens;
2461 struct swm_region *r;
2462 struct workspace *ws;
2467 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2468 for (i = 0; i < num_screens; i++)
2469 TAILQ_FOREACH(r, &screens[i].rl, entry)
2470 for (x = 0; x < workspace_limit; x++) {
2472 if (win == ws->focus)
2474 if (win == ws->focus_prev)
2475 ws->focus_prev = NULL;
2480 validate_win(struct ws_win *testwin)
2483 struct workspace *ws;
2484 struct swm_region *r;
2485 int i, x, num_screens;
2487 if (testwin == NULL)
2490 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2491 for (i = 0; i < num_screens; i++)
2492 TAILQ_FOREACH(r, &screens[i].rl, entry)
2493 for (x = 0; x < workspace_limit; x++) {
2495 TAILQ_FOREACH(win, &ws->winlist, entry)
2503 validate_ws(struct workspace *testws)
2505 struct swm_region *r;
2506 struct workspace *ws;
2507 int i, x, num_screens;
2509 /* validate all ws */
2510 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2511 for (i = 0; i < num_screens; i++)
2512 TAILQ_FOREACH(r, &screens[i].rl, entry)
2513 for (x = 0; x < workspace_limit; x++) {
2522 unfocus_win(struct ws_win *win)
2525 xcb_window_t none = XCB_WINDOW_NONE;
2527 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2531 if (win->ws == NULL)
2534 if (validate_ws(win->ws))
2535 return; /* XXX this gets hit with thunderbird, needs fixing */
2537 if (win->ws->r == NULL)
2540 if (validate_win(win)) {
2545 if (win->ws->focus == win) {
2546 win->ws->focus = NULL;
2547 win->ws->focus_prev = win;
2550 if (validate_win(win->ws->focus)) {
2551 kill_refs(win->ws->focus);
2552 win->ws->focus = NULL;
2554 if (validate_win(win->ws->focus_prev)) {
2555 kill_refs(win->ws->focus_prev);
2556 win->ws->focus_prev = NULL;
2559 /* drain all previous unfocus events */
2560 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2563 grabbuttons(win, 0);
2564 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2565 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2566 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2567 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2575 int i, j, num_screens;
2577 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2579 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2580 for (i = 0; i < num_screens; i++)
2581 for (j = 0; j < workspace_limit; j++)
2582 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2587 focus_win(struct ws_win *win)
2590 struct ws_win *cfw = NULL;
2591 xcb_get_input_focus_cookie_t c;
2592 xcb_get_input_focus_reply_t *r;
2593 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2595 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2599 if (win->ws == NULL)
2602 if (validate_ws(win->ws))
2603 return; /* XXX this gets hit with thunderbird, needs fixing */
2605 if (validate_win(win)) {
2610 if (validate_win(win)) {
2615 c = xcb_get_input_focus(conn);
2616 r = xcb_get_input_focus_reply(conn, c, NULL);
2618 cur_focus = r->focus;
2621 if ((cfw = find_window(cur_focus)) != NULL)
2624 /* use larger hammer since the window was killed somehow */
2625 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2626 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2627 xcb_change_window_attributes(conn, cfw->id,
2628 XCB_CW_BORDER_PIXEL,
2629 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2632 win->ws->focus = win;
2634 if (win->ws->r != NULL) {
2635 /* drain all previous focus events */
2636 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2640 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2641 win->id, XCB_CURRENT_TIME);
2642 grabbuttons(win, 1);
2643 xcb_change_window_attributes(conn, win->id,
2644 XCB_CW_BORDER_PIXEL,
2645 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2646 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2647 win->ws->always_raise)
2648 map_window_raised(win->id);
2650 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2651 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2659 switchws(struct swm_region *r, union arg *args)
2661 int wsid = args->id, unmap_old = 0;
2662 struct swm_region *this_r, *other_r;
2664 struct workspace *new_ws, *old_ws;
2670 if (wsid >= workspace_limit)
2674 old_ws = this_r->ws;
2675 new_ws = &this_r->s->ws[wsid];
2677 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2678 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2680 if (new_ws == NULL || old_ws == NULL)
2682 if (new_ws == old_ws)
2685 other_r = new_ws->r;
2686 if (other_r == NULL) {
2687 /* the other workspace is hidden, hide this one */
2691 /* the other ws is visible in another region, exchange them */
2692 other_r->ws_prior = new_ws;
2693 other_r->ws = old_ws;
2694 old_ws->r = other_r;
2696 this_r->ws_prior = old_ws;
2697 this_r->ws = new_ws;
2700 /* this is needed so that we can click on a window after a restart */
2704 a.id = SWM_ARG_ID_FOCUSCUR;
2705 focus(new_ws->r, &a);
2707 /* unmap old windows */
2709 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2712 if (focus_mode == SWM_FOCUS_DEFAULT)
2713 drain_enter_notify();
2717 cyclews(struct swm_region *r, union arg *args)
2720 struct swm_screen *s = r->s;
2723 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2724 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2729 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2732 case SWM_ARG_ID_CYCLEWS_UP:
2733 if (a.id < workspace_limit - 1)
2738 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2741 case SWM_ARG_ID_CYCLEWS_DOWN:
2745 a.id = workspace_limit - 1;
2752 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2754 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2758 } while (a.id != r->ws->idx);
2762 priorws(struct swm_region *r, union arg *args)
2766 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2767 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2769 if (r->ws_prior == NULL)
2772 a.id = r->ws_prior->idx;
2777 cyclescr(struct swm_region *r, union arg *args)
2779 struct swm_region *rr = NULL;
2781 int i, x, y, num_screens;
2783 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2784 /* do nothing if we don't have more than one screen */
2785 if (!(num_screens > 1 || outputs > 1))
2790 case SWM_ARG_ID_CYCLESC_UP:
2791 rr = TAILQ_NEXT(r, entry);
2793 rr = TAILQ_FIRST(&screens[i].rl);
2795 case SWM_ARG_ID_CYCLESC_DOWN:
2796 rr = TAILQ_PREV(r, swm_region_list, entry);
2798 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2806 /* move mouse to region */
2808 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2809 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2812 a.id = SWM_ARG_ID_FOCUSCUR;
2815 if (rr->ws->focus) {
2816 /* move to focus window */
2817 x = X(rr->ws->focus) + 1;
2818 y = Y(rr->ws->focus) + 1;
2819 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2825 sort_windows(struct ws_win_list *wl)
2827 struct ws_win *win, *parent, *nxt;
2832 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2833 nxt = TAILQ_NEXT(win, entry);
2834 if (win->transient) {
2835 parent = find_window(win->transient);
2836 if (parent == NULL) {
2837 warnx("not possible bug");
2840 TAILQ_REMOVE(wl, win, entry);
2841 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2848 swapwin(struct swm_region *r, union arg *args)
2850 struct ws_win *target, *source;
2851 struct ws_win *cur_focus;
2852 struct ws_win_list *wl;
2855 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2856 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2858 cur_focus = r->ws->focus;
2859 if (cur_focus == NULL)
2863 wl = &source->ws->winlist;
2866 case SWM_ARG_ID_SWAPPREV:
2867 if (source->transient)
2868 source = find_window(source->transient);
2869 target = TAILQ_PREV(source, ws_win_list, entry);
2870 if (target && target->transient)
2871 target = find_window(target->transient);
2872 TAILQ_REMOVE(wl, source, entry);
2874 TAILQ_INSERT_TAIL(wl, source, entry);
2876 TAILQ_INSERT_BEFORE(target, source, entry);
2878 case SWM_ARG_ID_SWAPNEXT:
2879 target = TAILQ_NEXT(source, entry);
2880 /* move the parent and let the sort handle the move */
2881 if (source->transient)
2882 source = find_window(source->transient);
2883 TAILQ_REMOVE(wl, source, entry);
2885 TAILQ_INSERT_HEAD(wl, source, entry);
2887 TAILQ_INSERT_AFTER(wl, target, source, entry);
2889 case SWM_ARG_ID_SWAPMAIN:
2890 target = TAILQ_FIRST(wl);
2891 if (target == source) {
2892 if (source->ws->focus_prev != NULL &&
2893 source->ws->focus_prev != target)
2894 source = source->ws->focus_prev;
2898 if (target == NULL || source == NULL)
2900 source->ws->focus_prev = target;
2901 TAILQ_REMOVE(wl, target, entry);
2902 TAILQ_INSERT_BEFORE(source, target, entry);
2903 TAILQ_REMOVE(wl, source, entry);
2904 TAILQ_INSERT_HEAD(wl, source, entry);
2906 case SWM_ARG_ID_MOVELAST:
2907 TAILQ_REMOVE(wl, source, entry);
2908 TAILQ_INSERT_TAIL(wl, source, entry);
2911 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2921 focus_prev(struct ws_win *win)
2923 struct ws_win *winfocus = NULL;
2924 struct ws_win *cur_focus = NULL;
2925 struct ws_win_list *wl = NULL;
2926 struct workspace *ws = NULL;
2928 if (!(win && win->ws))
2933 cur_focus = ws->focus;
2935 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2936 WINID(win), WINID(cur_focus));
2938 /* pickle, just focus on whatever */
2939 if (cur_focus == NULL) {
2940 /* use prev_focus if valid */
2941 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2942 find_window(WINID(ws->focus_prev)))
2943 winfocus = ws->focus_prev;
2947 /* if transient focus on parent */
2948 if (cur_focus->transient) {
2949 winfocus = find_window(cur_focus->transient);
2953 /* if in max_stack try harder */
2954 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2955 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2956 if (cur_focus != ws->focus_prev)
2957 winfocus = ws->focus_prev;
2959 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2964 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2966 if (winfocus == NULL || winfocus == win) {
2967 switch (focus_close) {
2968 case SWM_STACK_BOTTOM:
2969 winfocus = TAILQ_FIRST(wl);
2972 winfocus = TAILQ_LAST(wl, ws_win_list);
2974 case SWM_STACK_ABOVE:
2975 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2976 if (focus_close_wrap)
2977 winfocus = TAILQ_FIRST(wl);
2979 winfocus = TAILQ_PREV(cur_focus,
2980 ws_win_list, entry);
2983 case SWM_STACK_BELOW:
2984 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2986 if (focus_close_wrap)
2987 winfocus = TAILQ_LAST(wl, ws_win_list);
2989 winfocus = TAILQ_NEXT(cur_focus, entry);
2995 if (winfocus == NULL) {
2996 if (focus_default == SWM_STACK_TOP)
2997 winfocus = TAILQ_LAST(wl, ws_win_list);
2999 winfocus = TAILQ_FIRST(wl);
3003 focus_magic(winfocus);
3007 focus(struct swm_region *r, union arg *args)
3009 struct ws_win *winfocus = NULL, *head;
3010 struct ws_win *cur_focus = NULL;
3011 struct ws_win_list *wl = NULL;
3012 struct workspace *ws = NULL;
3018 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3020 /* treat FOCUS_CUR special */
3021 if (args->id == SWM_ARG_ID_FOCUSCUR) {
3022 if (r->ws->focus && r->ws->focus->iconic == 0)
3023 winfocus = r->ws->focus;
3024 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
3025 winfocus = r->ws->focus_prev;
3027 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3028 if (winfocus->iconic == 0)
3031 focus_magic(winfocus);
3035 if ((cur_focus = r->ws->focus) == NULL)
3039 if (TAILQ_EMPTY(wl))
3041 /* make sure there is at least one uniconified window */
3043 TAILQ_FOREACH(winfocus, wl, entry)
3044 if (winfocus->iconic == 0) {
3052 case SWM_ARG_ID_FOCUSPREV:
3053 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3055 head = TAILQ_LAST(wl, ws_win_list);
3057 if (WINID(winfocus) == cur_focus->transient) {
3058 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3060 head = TAILQ_LAST(wl, ws_win_list);
3065 if (winfocus && winfocus->iconic) {
3066 while (winfocus != cur_focus) {
3067 if (winfocus == NULL)
3068 winfocus = TAILQ_LAST(wl, ws_win_list);
3069 if (winfocus->iconic == 0)
3071 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3077 case SWM_ARG_ID_FOCUSNEXT:
3078 head = TAILQ_NEXT(cur_focus, entry);
3080 head = TAILQ_FIRST(wl);
3084 if (winfocus && winfocus->iconic) {
3085 while (winfocus != cur_focus) {
3086 if (winfocus == NULL)
3087 winfocus = TAILQ_FIRST(wl);
3088 if (winfocus->iconic == 0)
3090 winfocus = TAILQ_NEXT(winfocus, entry);
3095 case SWM_ARG_ID_FOCUSMAIN:
3096 winfocus = TAILQ_FIRST(wl);
3097 if (winfocus == cur_focus)
3098 winfocus = cur_focus->ws->focus_prev;
3105 focus_magic(winfocus);
3109 cycle_layout(struct swm_region *r, union arg *args)
3111 struct workspace *ws = r->ws;
3114 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3117 if (ws->cur_layout->l_stack == NULL)
3118 ws->cur_layout = &layouts[0];
3121 if (focus_mode == SWM_FOCUS_DEFAULT)
3122 drain_enter_notify();
3123 a.id = SWM_ARG_ID_FOCUSCUR;
3128 stack_config(struct swm_region *r, union arg *args)
3130 struct workspace *ws = r->ws;
3132 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3135 if (ws->cur_layout->l_config != NULL)
3136 ws->cur_layout->l_config(ws, args->id);
3138 if (args->id != SWM_ARG_ID_STACKINIT)
3145 struct swm_geometry g;
3146 struct swm_region *r;
3152 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3154 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3155 for (i = 0; i < num_screens; i++) {
3159 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3160 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3161 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3163 /* start with screen geometry, adjust for bar */
3165 g.w -= 2 * border_width;
3166 g.h -= 2 * border_width;
3172 r->ws->cur_layout->l_stack(r->ws, &g);
3173 r->ws->cur_layout->l_string(r->ws);
3174 /* save r so we can track region changes */
3181 if (focus_mode == SWM_FOCUS_DEFAULT)
3182 drain_enter_notify();
3184 DNPRINTF(SWM_D_STACK, "stack: end\n");
3188 store_float_geom(struct ws_win *win, struct swm_region *r)
3190 /* retain window geom and region geom */
3191 win->g_float = win->g;
3192 win->g_float.x -= X(r);
3193 win->g_float.y -= Y(r);
3194 win->g_floatvalid = 1;
3195 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3196 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3197 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3198 win->g_float.w, win->g_float.h);
3202 stack_floater(struct ws_win *win, struct swm_region *r)
3207 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3210 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3211 * geom on ws switches or return from max mode
3213 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3214 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3215 /* refloat at last floating relative position */
3216 win->g = win->g_float;
3221 win->floatmaxed = 0;
3224 * if set to fullscreen mode, configure window to maximum size.
3226 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3227 if (!win->g_floatvalid)
3228 store_float_geom(win, win->ws->r);
3230 win->g = win->ws->r->g;
3234 * remove border on fullscreen floater when in fullscreen mode or when
3235 * the quirk is present.
3237 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3238 ((win->quirks & SWM_Q_FULLSCREEN) &&
3239 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3240 if (win->bordered) {
3242 X(win) += border_width;
3243 Y(win) += border_width;
3245 } else if (!win->bordered) {
3247 X(win) -= border_width;
3248 Y(win) -= border_width;
3251 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3252 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3253 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3258 * floaters and transients are auto-centred unless moved
3261 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3262 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3265 /* keep window within region bounds */
3266 constrain_window(win, r, 0);
3272 * Send keystrokes to terminal to decrease/increase the font size as the
3273 * window size changes.
3276 adjust_font(struct ws_win *win)
3278 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3279 win->floating || win->transient)
3282 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3283 WIDTH(win) / win->sh.width_inc < term_width &&
3284 win->font_steps < SWM_MAX_FONT_STEPS) {
3285 win->font_size_boundary[win->font_steps] =
3286 (win->sh.width_inc * term_width) + win->sh.base_width;
3289 win->last_inc = win->sh.width_inc;
3290 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3291 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3292 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3295 win->last_inc = win->sh.width_inc;
3296 fake_keypress(win, XK_KP_Add, ShiftMask);
3300 #define SWAPXY(g) do { \
3302 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3303 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3306 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3308 XWindowAttributes wa;
3309 struct swm_geometry win_g, r_g = *g;
3310 struct ws_win *win, *fs_win = NULL;
3311 int i, j, s, stacks;
3312 int w_inc = 1, h_inc, w_base = 1, h_base;
3313 int hrh, extra = 0, h_slice, last_h = 0;
3314 int split, colno, winno, mwin, msize, mscale;
3315 int remain, missing, v_slice, reconfigure;
3318 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3319 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3321 winno = count_win(ws, 0);
3322 if (winno == 0 && count_win(ws, 1) == 0)
3325 TAILQ_FOREACH(win, &ws->winlist, entry)
3326 if (win->transient == 0 && win->floating == 0
3327 && win->iconic == 0)
3334 w_inc = win->sh.width_inc;
3335 w_base = win->sh.base_width;
3336 mwin = ws->l_state.horizontal_mwin;
3337 mscale = ws->l_state.horizontal_msize;
3338 stacks = ws->l_state.horizontal_stacks;
3341 w_inc = win->sh.height_inc;
3342 w_base = win->sh.base_height;
3343 mwin = ws->l_state.vertical_mwin;
3344 mscale = ws->l_state.vertical_msize;
3345 stacks = ws->l_state.vertical_stacks;
3349 if (stacks > winno - mwin)
3350 stacks = winno - mwin;
3354 h_slice = r_g.h / SWM_H_SLICE;
3355 if (mwin && winno > mwin) {
3356 v_slice = r_g.w / SWM_V_SLICE;
3360 win_g.w = v_slice * mscale;
3362 if (w_inc > 1 && w_inc < v_slice) {
3363 /* adjust for window's requested size increment */
3364 remain = (win_g.w - w_base) % w_inc;
3370 win_g.x += r_g.w - msize;
3373 colno = split = winno / stacks;
3374 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3375 2 * border_width) / stacks);
3377 hrh = r_g.h / colno;
3378 extra = r_g.h - (colno * hrh);
3379 win_g.h = hrh - 2 * border_width;
3381 /* stack all the tiled windows */
3382 i = j = 0, s = stacks;
3383 TAILQ_FOREACH(win, &ws->winlist, entry) {
3384 if (win->transient != 0 || win->floating != 0)
3386 if (win->iconic != 0)
3389 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3394 if (split && i == split) {
3395 colno = (winno - mwin) / stacks;
3396 if (s <= (winno - mwin) % stacks)
3398 split = split + colno;
3399 hrh = (r_g.h / colno);
3400 extra = r_g.h - (colno * hrh);
3404 win_g.x += win_g.w + 2 * border_width;
3405 win_g.w = (r_g.w - msize -
3406 (stacks * 2 * border_width)) / stacks;
3408 win_g.w += (r_g.w - msize -
3409 (stacks * 2 * border_width)) % stacks;
3413 win_g.h = hrh - 2 * border_width;
3415 h_inc = win->sh.width_inc;
3416 h_base = win->sh.base_width;
3418 h_inc = win->sh.height_inc;
3419 h_base = win->sh.base_height;
3421 if (j == colno - 1) {
3422 win_g.h = hrh + extra;
3423 } else if (h_inc > 1 && h_inc < h_slice) {
3424 /* adjust for window's requested size increment */
3425 remain = (win_g.h - h_base) % h_inc;
3426 missing = h_inc - remain;
3428 if (missing <= extra || j == 0) {
3440 win_g.y += last_h + 2 * border_width;
3442 if (disable_border && bar_enabled == 0 && winno == 1){
3444 win_g.w += 2 * border_width;
3445 win_g.h += 2 * border_width;
3450 if (X(win) != win_g.y || Y(win) != win_g.x ||
3451 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3455 WIDTH(win) = win_g.h;
3456 HEIGHT(win) = win_g.w;
3459 if (X(win) != win_g.x || Y(win) != win_g.y ||
3460 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3464 WIDTH(win) = win_g.w;
3465 HEIGHT(win) = win_g.h;
3469 if (bordered != win->bordered) {
3471 win->bordered = bordered;
3479 if (XGetWindowAttributes(display, win->id, &wa))
3480 if (wa.map_state == IsUnmapped)
3481 map_window_raised(win->id);
3489 /* now, stack all the floaters and transients */
3490 TAILQ_FOREACH(win, &ws->winlist, entry) {
3491 if (win->transient == 0 && win->floating == 0)
3493 if (win->iconic == 1)
3495 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3500 stack_floater(win, ws->r);
3501 map_window_raised(win->id);
3505 stack_floater(fs_win, ws->r);
3506 map_window_raised(fs_win->id);
3511 vertical_config(struct workspace *ws, int id)
3513 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3517 case SWM_ARG_ID_STACKRESET:
3518 case SWM_ARG_ID_STACKINIT:
3519 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3520 ws->l_state.vertical_mwin = 1;
3521 ws->l_state.vertical_stacks = 1;
3523 case SWM_ARG_ID_MASTERSHRINK:
3524 if (ws->l_state.vertical_msize > 1)
3525 ws->l_state.vertical_msize--;
3527 case SWM_ARG_ID_MASTERGROW:
3528 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3529 ws->l_state.vertical_msize++;
3531 case SWM_ARG_ID_MASTERADD:
3532 ws->l_state.vertical_mwin++;
3534 case SWM_ARG_ID_MASTERDEL:
3535 if (ws->l_state.vertical_mwin > 0)
3536 ws->l_state.vertical_mwin--;
3538 case SWM_ARG_ID_STACKINC:
3539 ws->l_state.vertical_stacks++;
3541 case SWM_ARG_ID_STACKDEC:
3542 if (ws->l_state.vertical_stacks > 1)
3543 ws->l_state.vertical_stacks--;
3545 case SWM_ARG_ID_FLIPLAYOUT:
3546 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3554 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3556 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3558 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3562 horizontal_config(struct workspace *ws, int id)
3564 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3567 case SWM_ARG_ID_STACKRESET:
3568 case SWM_ARG_ID_STACKINIT:
3569 ws->l_state.horizontal_mwin = 1;
3570 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3571 ws->l_state.horizontal_stacks = 1;
3573 case SWM_ARG_ID_MASTERSHRINK:
3574 if (ws->l_state.horizontal_msize > 1)
3575 ws->l_state.horizontal_msize--;
3577 case SWM_ARG_ID_MASTERGROW:
3578 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3579 ws->l_state.horizontal_msize++;
3581 case SWM_ARG_ID_MASTERADD:
3582 ws->l_state.horizontal_mwin++;
3584 case SWM_ARG_ID_MASTERDEL:
3585 if (ws->l_state.horizontal_mwin > 0)
3586 ws->l_state.horizontal_mwin--;
3588 case SWM_ARG_ID_STACKINC:
3589 ws->l_state.horizontal_stacks++;
3591 case SWM_ARG_ID_STACKDEC:
3592 if (ws->l_state.horizontal_stacks > 1)
3593 ws->l_state.horizontal_stacks--;
3595 case SWM_ARG_ID_FLIPLAYOUT:
3596 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3604 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3606 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3608 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3611 /* fullscreen view */
3613 max_stack(struct workspace *ws, struct swm_geometry *g)
3615 struct swm_geometry gg = *g;
3616 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3617 int winno, num_screens;
3619 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3624 winno = count_win(ws, 0);
3625 if (winno == 0 && count_win(ws, 1) == 0)
3628 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3629 TAILQ_FOREACH(win, &ws->winlist, entry) {
3630 if (win->transient) {
3632 parent = find_window(win->transient);
3636 if (win->floating && win->floatmaxed == 0 ) {
3638 * retain geometry for retrieval on exit from
3641 store_float_geom(win, ws->r);
3642 win->floatmaxed = 1;
3645 /* only reconfigure if necessary */
3646 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3647 HEIGHT(win) != gg.h) {
3653 WIDTH(win) += 2 * border_width;
3654 HEIGHT(win) += 2 * border_width;
3659 /* unmap only if we don't have multi screen */
3660 if (win != ws->focus)
3661 if (!(num_screens > 1 || outputs > 1))
3665 /* put the last transient on top */
3668 map_window_raised(parent->id);
3669 stack_floater(wintrans, ws->r);
3670 focus_magic(wintrans);
3675 send_to_ws(struct swm_region *r, union arg *args)
3677 int wsid = args->id;
3678 struct ws_win *win = NULL, *parent;
3679 struct workspace *ws, *nws;
3680 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3681 char ws_idx_str[SWM_PROPLEN];
3684 if (wsid >= workspace_limit)
3687 if (r && r->ws && r->ws->focus)
3693 if (win->ws->idx == wsid)
3696 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3699 nws = &win->s->ws[wsid];
3701 a.id = SWM_ARG_ID_FOCUSPREV;
3703 if (win->transient) {
3704 parent = find_window(win->transient);
3706 unmap_window(parent);
3707 TAILQ_REMOVE(&ws->winlist, parent, entry);
3708 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3713 TAILQ_REMOVE(&ws->winlist, win, entry);
3714 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3715 if (TAILQ_EMPTY(&ws->winlist))
3716 r->ws->focus = NULL;
3719 /* Try to update the window's workspace property */
3720 ws_idx_atom = get_atom_from_string("_SWM_WS");
3722 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3724 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3726 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3727 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3736 pressbutton(struct swm_region *r, union arg *args)
3738 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3739 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3743 raise_toggle(struct swm_region *r, union arg *args)
3745 if (r == NULL || r->ws == NULL)
3748 r->ws->always_raise = !r->ws->always_raise;
3750 /* bring floaters back to top */
3751 if (r->ws->always_raise == 0)
3756 iconify(struct swm_region *r, union arg *args)
3760 if (r->ws->focus == NULL)
3762 unmap_window(r->ws->focus);
3763 update_iconic(r->ws->focus, 1);
3765 if (focus_mode == SWM_FOCUS_DEFAULT)
3766 drain_enter_notify();
3767 r->ws->focus = NULL;
3768 a.id = SWM_ARG_ID_FOCUSCUR;
3773 get_win_name(xcb_window_t win)
3776 xcb_get_property_cookie_t c;
3777 xcb_get_text_property_reply_t r;
3779 c = xcb_icccm_get_wm_name(conn, win);
3780 if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
3781 if (r.name_len > 0) {
3782 name = malloc(r.name_len + 1);
3784 xcb_get_text_property_reply_wipe(&r);
3787 memcpy(name, r.name, r.name_len);
3788 name[r.name_len] = '\0';
3790 xcb_get_text_property_reply_wipe(&r);
3797 uniconify(struct swm_region *r, union arg *args)
3804 DNPRINTF(SWM_D_MISC, "uniconify\n");
3806 if (r == NULL || r->ws == NULL)
3809 /* make sure we have anything to uniconify */
3810 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3811 if (win->ws == NULL)
3812 continue; /* should never happen */
3813 if (win->iconic == 0)
3821 search_resp_action = SWM_SEARCH_UNICONIFY;
3823 spawn_select(r, args, "search", &searchpid);
3825 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3828 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3829 if (win->ws == NULL)
3830 continue; /* should never happen */
3831 if (win->iconic == 0)
3834 name = get_win_name(win->id);
3837 fprintf(lfile, "%s.%u\n", name, win->id);
3845 name_workspace(struct swm_region *r, union arg *args)
3849 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3855 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3857 spawn_select(r, args, "name_workspace", &searchpid);
3859 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3862 fprintf(lfile, "%s", "");
3867 search_workspace(struct swm_region *r, union arg *args)
3870 struct workspace *ws;
3873 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3879 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3881 spawn_select(r, args, "search", &searchpid);
3883 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3886 for (i = 0; i < workspace_limit; i++) {
3890 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3891 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3898 search_win_cleanup(void)
3900 struct search_window *sw = NULL;
3902 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3903 xcb_destroy_window(conn, sw->indicator);
3904 xcb_free_gc(conn, sw->gc);
3905 TAILQ_REMOVE(&search_wl, sw, entry);
3911 search_win(struct swm_region *r, union arg *args)
3913 struct ws_win *win = NULL;
3914 struct search_window *sw = NULL;
3916 uint32_t gcv[1], wa[2];
3921 XRectangle ibox, lbox;
3922 xcb_screen_t *screen;
3923 DNPRINTF(SWM_D_MISC, "search_win\n");
3926 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3928 spawn_select(r, args, "search", &searchpid);
3930 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3933 TAILQ_INIT(&search_wl);
3936 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3937 if (win->iconic == 1)
3940 sw = calloc(1, sizeof(struct search_window));
3942 warn("search_win: calloc");
3944 search_win_cleanup();
3949 screen = get_screen(sw->idx);
3951 snprintf(s, sizeof s, "%d", i);
3954 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3956 w = xcb_generate_id(conn);
3957 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
3958 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
3959 xcb_create_window(conn, screen->root_depth, w, win->id,
3960 0, 0, lbox.width + 4,
3961 bar_fs_extents->max_logical_extent.height, 1,
3962 XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual,
3963 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
3966 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3968 sw->gc = xcb_generate_id(conn);
3969 gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
3970 xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND, gcv);
3971 map_window_raised(w);
3973 xcb_image_text_8(conn, len, w, sw->gc, 2,
3974 (bar_fs_extents->max_logical_extent.height -
3975 lbox.height) / 2 - lbox.y, s);
3977 fprintf(lfile, "%d\n", i);
3985 search_resp_uniconify(char *resp, unsigned long len)
3991 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3993 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3994 if (win->iconic == 0)
3996 name = get_win_name(win->id);
3999 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4004 if (strncmp(s, resp, len) == 0) {
4005 /* XXX this should be a callback to generalize */
4006 update_iconic(win, 0);
4015 search_resp_name_workspace(char *resp, unsigned long len)
4017 struct workspace *ws;
4019 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4021 if (search_r->ws == NULL)
4026 free(search_r->ws->name);
4027 search_r->ws->name = NULL;
4031 ws->name = strdup(resp);
4032 if (ws->name == NULL) {
4033 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4034 "strdup: %s", strerror(errno));
4041 search_resp_search_workspace(char *resp, unsigned long len)
4048 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4052 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4059 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
4061 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4068 switchws(search_r, &a);
4072 search_resp_search_window(char *resp, unsigned long len)
4077 struct search_window *sw;
4079 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4083 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4088 idx = strtonum(s, 1, INT_MAX, &errstr);
4090 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4097 TAILQ_FOREACH(sw, &search_wl, entry)
4098 if (idx == sw->idx) {
4104 #define MAX_RESP_LEN 1024
4107 search_do_resp(void)
4113 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4118 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4119 warn("search: calloc");
4123 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4125 warn("search: read error");
4128 resp[rbytes] = '\0';
4131 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4132 * newline, so work around that by sanitizing the resp now.
4134 resp[strcspn(resp, "\n")] = '\0';
4137 switch (search_resp_action) {
4138 case SWM_SEARCH_UNICONIFY:
4139 search_resp_uniconify(resp, len);
4141 case SWM_SEARCH_NAME_WORKSPACE:
4142 search_resp_name_workspace(resp, len);
4144 case SWM_SEARCH_SEARCH_WORKSPACE:
4145 search_resp_search_workspace(resp, len);
4147 case SWM_SEARCH_SEARCH_WINDOW:
4148 search_resp_search_window(resp, len);
4153 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4154 search_win_cleanup();
4156 search_resp_action = SWM_SEARCH_NONE;
4157 close(select_resp_pipe[0]);
4162 wkill(struct swm_region *r, union arg *args)
4164 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4166 if (r->ws->focus == NULL)
4169 if (args->id == SWM_ARG_ID_KILLWINDOW)
4170 xcb_kill_client(conn, r->ws->focus->id);
4172 if (r->ws->focus->can_delete)
4173 client_msg(r->ws->focus, adelete);
4178 floating_toggle_win(struct ws_win *win)
4180 struct swm_region *r;
4190 /* reject floating toggles in max stack mode */
4191 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4194 if (win->floating) {
4195 if (!win->floatmaxed) {
4196 /* retain position for refloat */
4197 store_float_geom(win, r);
4201 if (win->g_floatvalid) {
4202 /* refloat at last floating relative position */
4203 X(win) = win->g_float.x + X(r);
4204 Y(win) = win->g_float.y + Y(r);
4205 WIDTH(win) = win->g_float.w;
4206 HEIGHT(win) = win->g_float.h;
4211 ewmh_update_actions(win);
4217 floating_toggle(struct swm_region *r, union arg *args)
4219 struct ws_win *win = r->ws->focus;
4225 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4226 _NET_WM_STATE_TOGGLE);
4229 if (focus_mode == SWM_FOCUS_DEFAULT)
4230 drain_enter_notify();
4232 if (win == win->ws->focus) {
4233 a.id = SWM_ARG_ID_FOCUSCUR;
4234 focus(win->ws->r, &a);
4239 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4241 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4243 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4245 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4248 if (X(win) + BORDER(win) < X(r)) {
4250 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4252 X(win) = X(r) - BORDER(win);
4255 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4257 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4259 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4262 if (Y(win) + BORDER(win) < Y(r)) {
4264 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4266 Y(win) = Y(r) - BORDER(win);
4272 if (HEIGHT(win) < 1)
4278 update_window(struct ws_win *win)
4283 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4284 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4285 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4289 wc[3] = HEIGHT(win);
4290 wc[4] = BORDER(win);
4292 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4293 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4294 wc[3], YESNO(win->bordered));
4296 xcb_configure_window(conn, win->id, mask, wc);
4299 #define SWM_RESIZE_STEPS (50)
4302 resize(struct ws_win *win, union arg *args)
4306 struct swm_region *r = NULL;
4307 int resize_step = 0;
4308 struct swm_geometry g;
4309 int top = 0, left = 0;
4311 unsigned int shape; /* cursor style */
4312 xcb_cursor_t cursor;
4313 xcb_font_t cursor_font;
4314 xcb_grab_pointer_reply_t *gpr;
4315 xcb_query_pointer_reply_t *xpr;
4321 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4324 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%x, floating: %s, "
4325 "transient: 0x%x\n", win->id, YESNO(win->floating),
4328 if (!(win->transient != 0 || win->floating != 0))
4331 /* reject resizes in max mode for floaters (transient ok) */
4332 if (win->floatmaxed)
4336 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4342 case SWM_ARG_ID_WIDTHSHRINK:
4343 WIDTH(win) -= SWM_RESIZE_STEPS;
4346 case SWM_ARG_ID_WIDTHGROW:
4347 WIDTH(win) += SWM_RESIZE_STEPS;
4350 case SWM_ARG_ID_HEIGHTSHRINK:
4351 HEIGHT(win) -= SWM_RESIZE_STEPS;
4354 case SWM_ARG_ID_HEIGHTGROW:
4355 HEIGHT(win) += SWM_RESIZE_STEPS;
4362 constrain_window(win, r, 1);
4364 store_float_geom(win,r);
4368 if (focus_mode == SWM_FOCUS_DEFAULT)
4369 drain_enter_notify();
4371 /* get cursor offset from window root */
4372 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4379 if (xpr->win_x < WIDTH(win) / 2)
4382 if (xpr->win_y < HEIGHT(win) / 2)
4385 if (args->id == SWM_ARG_ID_CENTER)
4388 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4390 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4392 cursor_font = xcb_generate_id(conn);
4393 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4395 cursor = xcb_generate_id(conn);
4396 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4397 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4399 gpr = xcb_grab_pointer_reply(conn,
4400 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4401 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
4402 cursor, XCB_CURRENT_TIME),
4405 xcb_free_cursor(conn, cursor);
4406 xcb_close_font(conn, cursor_font);
4412 XMaskEvent(display, MOUSEMASK | ExposureMask |
4413 SubstructureRedirectMask, &ev);
4415 case ConfigureRequest:
4418 handler[ev.type](&ev);
4421 /* cursor offset/delta from start of the operation */
4422 dx = ev.xmotion.x_root - xpr->root_x;
4423 dy = ev.xmotion.y_root - xpr->root_y;
4429 if (args->id == SWM_ARG_ID_CENTER) {
4430 if (g.h / 2 + dy < 1)
4434 HEIGHT(win) = g.h + 2 * dy;
4442 HEIGHT(win) = g.h + dy;
4449 if (args->id == SWM_ARG_ID_CENTER) {
4450 if (g.w / 2 + dx < 1)
4454 WIDTH(win) = g.w + 2 * dx;
4462 WIDTH(win) = g.w + dx;
4465 constrain_window(win, r, 1);
4467 /* not free, don't sync more than 120 times / second */
4468 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4469 time = ev.xmotion.time;
4475 } while (ev.type != ButtonRelease);
4480 store_float_geom(win,r);
4482 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4483 xcb_free_cursor(conn, cursor);
4484 xcb_close_font(conn, cursor_font);
4489 drain_enter_notify();
4493 resize_step(struct swm_region *r, union arg *args)
4495 struct ws_win *win = NULL;
4497 if (r && r->ws && r->ws->focus)
4505 #define SWM_MOVE_STEPS (50)
4508 move(struct ws_win *win, union arg *args)
4513 struct swm_region *r = NULL;
4514 xcb_font_t cursor_font;
4515 xcb_cursor_t cursor;
4516 xcb_grab_pointer_reply_t *gpr;
4517 xcb_query_pointer_reply_t *qpr;
4523 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4526 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%x, floating: %s, transient: "
4527 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4529 /* in max_stack mode should only move transients */
4530 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4534 if (win->floating == 0 && !win->transient) {
4535 store_float_geom(win, r);
4536 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4539 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4546 case SWM_ARG_ID_MOVELEFT:
4547 X(win) -= (SWM_MOVE_STEPS - border_width);
4550 case SWM_ARG_ID_MOVERIGHT:
4551 X(win) += (SWM_MOVE_STEPS - border_width);
4554 case SWM_ARG_ID_MOVEUP:
4555 Y(win) -= (SWM_MOVE_STEPS - border_width);
4558 case SWM_ARG_ID_MOVEDOWN:
4559 Y(win) += (SWM_MOVE_STEPS - border_width);
4566 constrain_window(win, r, 0);
4568 store_float_geom(win, r);
4572 cursor_font = xcb_generate_id(conn);
4573 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4575 cursor = xcb_generate_id(conn);
4576 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4577 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4579 gpr = xcb_grab_pointer_reply(conn,
4580 xcb_grab_pointer(conn, False, win->id, MOUSEMASK,
4581 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4582 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME),
4585 xcb_free_cursor(conn, cursor);
4586 xcb_close_font(conn, cursor_font);
4590 /* get cursor offset from window root */
4591 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4594 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4595 xcb_free_cursor(conn, cursor);
4596 xcb_close_font(conn, cursor_font);
4600 XMaskEvent(display, MOUSEMASK | ExposureMask |
4601 SubstructureRedirectMask, &ev);
4603 case ConfigureRequest:
4606 handler[ev.type](&ev);
4609 X(win) = ev.xmotion.x_root - qpr->win_x - border_width;
4610 Y(win) = ev.xmotion.y_root - qpr->win_y - border_width;
4612 constrain_window(win, r, 0);
4614 /* not free, don't sync more than 120 times / second */
4615 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4616 time = ev.xmotion.time;
4622 } while (ev.type != ButtonRelease);
4627 store_float_geom(win, r);
4629 xcb_free_cursor(conn, cursor);
4630 xcb_close_font(conn, cursor_font);
4631 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4634 drain_enter_notify();
4638 move_step(struct swm_region *r, union arg *args)
4640 struct ws_win *win = NULL;
4642 if (r && r->ws && r->ws->focus)
4647 if (!(win->transient != 0 || win->floating != 0))
4654 /* user/key callable function IDs */
4743 kf_search_workspace,
4745 kf_dumpwins, /* MUST BE LAST */
4749 /* key definitions */
4751 dummykeyfunc(struct swm_region *r, union arg *args)
4756 char name[SWM_FUNCNAME_LEN];
4757 void (*func)(struct swm_region *r, union arg *);
4759 } keyfuncs[kf_invalid + 1] = {
4760 /* name function argument */
4761 { "cycle_layout", cycle_layout, {0} },
4762 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4763 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4764 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4765 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4766 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4767 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4768 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4769 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4770 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4771 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4772 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4773 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4774 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4775 { "quit", quit, {0} },
4776 { "restart", restart, {0} },
4777 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4778 { "ws_1", switchws, {.id = 0} },
4779 { "ws_2", switchws, {.id = 1} },
4780 { "ws_3", switchws, {.id = 2} },
4781 { "ws_4", switchws, {.id = 3} },
4782 { "ws_5", switchws, {.id = 4} },
4783 { "ws_6", switchws, {.id = 5} },
4784 { "ws_7", switchws, {.id = 6} },
4785 { "ws_8", switchws, {.id = 7} },
4786 { "ws_9", switchws, {.id = 8} },
4787 { "ws_10", switchws, {.id = 9} },
4788 { "ws_11", switchws, {.id = 10} },
4789 { "ws_12", switchws, {.id = 11} },
4790 { "ws_13", switchws, {.id = 12} },
4791 { "ws_14", switchws, {.id = 13} },
4792 { "ws_15", switchws, {.id = 14} },
4793 { "ws_16", switchws, {.id = 15} },
4794 { "ws_17", switchws, {.id = 16} },
4795 { "ws_18", switchws, {.id = 17} },
4796 { "ws_19", switchws, {.id = 18} },
4797 { "ws_20", switchws, {.id = 19} },
4798 { "ws_21", switchws, {.id = 20} },
4799 { "ws_22", switchws, {.id = 21} },
4800 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4801 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4802 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4803 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4804 { "ws_prior", priorws, {0} },
4805 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4806 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4807 { "mvws_1", send_to_ws, {.id = 0} },
4808 { "mvws_2", send_to_ws, {.id = 1} },
4809 { "mvws_3", send_to_ws, {.id = 2} },
4810 { "mvws_4", send_to_ws, {.id = 3} },
4811 { "mvws_5", send_to_ws, {.id = 4} },
4812 { "mvws_6", send_to_ws, {.id = 5} },
4813 { "mvws_7", send_to_ws, {.id = 6} },
4814 { "mvws_8", send_to_ws, {.id = 7} },
4815 { "mvws_9", send_to_ws, {.id = 8} },
4816 { "mvws_10", send_to_ws, {.id = 9} },
4817 { "mvws_11", send_to_ws, {.id = 10} },
4818 { "mvws_12", send_to_ws, {.id = 11} },
4819 { "mvws_13", send_to_ws, {.id = 12} },
4820 { "mvws_14", send_to_ws, {.id = 13} },
4821 { "mvws_15", send_to_ws, {.id = 14} },
4822 { "mvws_16", send_to_ws, {.id = 15} },
4823 { "mvws_17", send_to_ws, {.id = 16} },
4824 { "mvws_18", send_to_ws, {.id = 17} },
4825 { "mvws_19", send_to_ws, {.id = 18} },
4826 { "mvws_20", send_to_ws, {.id = 19} },
4827 { "mvws_21", send_to_ws, {.id = 20} },
4828 { "mvws_22", send_to_ws, {.id = 21} },
4829 { "bar_toggle", bar_toggle, {0} },
4830 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4831 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4832 { "float_toggle", floating_toggle,{0} },
4833 { "version", version, {0} },
4834 { "spawn_custom", dummykeyfunc, {0} },
4835 { "iconify", iconify, {0} },
4836 { "uniconify", uniconify, {0} },
4837 { "raise_toggle", raise_toggle, {0} },
4838 { "button2", pressbutton, {2} },
4839 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4840 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4841 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4842 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4843 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4844 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4845 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4846 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4847 { "name_workspace", name_workspace, {0} },
4848 { "search_workspace", search_workspace, {0} },
4849 { "search_win", search_win, {0} },
4850 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4851 { "invalid key func", NULL, {0} },
4854 RB_ENTRY(key) entry;
4857 enum keyfuncid funcid;
4860 RB_HEAD(key_tree, key);
4863 key_cmp(struct key *kp1, struct key *kp2)
4865 if (kp1->keysym < kp2->keysym)
4867 if (kp1->keysym > kp2->keysym)
4870 if (kp1->mod < kp2->mod)
4872 if (kp1->mod > kp2->mod)
4878 RB_GENERATE(key_tree, key, entry, key_cmp);
4879 struct key_tree keys;
4882 enum { client_click, root_click };
4884 unsigned int action;
4886 unsigned int button;
4887 void (*func)(struct ws_win *, union arg *);
4890 /* action key mouse button func args */
4891 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4892 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4893 { client_click, MODKEY, Button1, move, {0} },
4897 update_modkey(unsigned int mod)
4903 RB_FOREACH(kp, key_tree, &keys)
4904 if (kp->mod & ShiftMask)
4905 kp->mod = mod | ShiftMask;
4909 for (i = 0; i < LENGTH(buttons); i++)
4910 if (buttons[i].mask & ShiftMask)
4911 buttons[i].mask = mod | ShiftMask;
4913 buttons[i].mask = mod;
4918 TAILQ_ENTRY(spawn_prog) entry;
4923 TAILQ_HEAD(spawn_list, spawn_prog);
4924 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4927 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4930 struct spawn_prog *prog = NULL;
4932 char *ap, **real_args;
4934 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4937 TAILQ_FOREACH(prog, &spawns, entry) {
4938 if (!strcasecmp(spawn_name, prog->name))
4942 warnx("spawn_custom: program %s not found", spawn_name);
4946 /* make room for expanded args */
4947 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4948 err(1, "spawn_custom: calloc real_args");
4950 /* expand spawn_args into real_args */
4951 for (i = 0; i < prog->argc; i++) {
4953 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4954 if (!strcasecmp(ap, "$bar_border")) {
4956 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4958 err(1, "spawn_custom border color");
4959 } else if (!strcasecmp(ap, "$bar_color")) {
4961 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4963 err(1, "spawn_custom bar color");
4964 } else if (!strcasecmp(ap, "$bar_font")) {
4965 if ((real_args[i] = strdup(bar_fonts))
4967 err(1, "spawn_custom bar fonts");
4968 } else if (!strcasecmp(ap, "$bar_font_color")) {
4970 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4972 err(1, "spawn_custom color font");
4973 } else if (!strcasecmp(ap, "$color_focus")) {
4975 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4977 err(1, "spawn_custom color focus");
4978 } else if (!strcasecmp(ap, "$color_unfocus")) {
4980 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4982 err(1, "spawn_custom color unfocus");
4984 /* no match --> copy as is */
4985 if ((real_args[i] = strdup(ap)) == NULL)
4986 err(1, "spawn_custom strdup(ap)");
4988 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4993 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4994 for (i = 0; i < prog->argc; i++)
4995 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4996 DNPRINTF(SWM_D_SPAWN, "\n");
4998 *ret_args = real_args;
4999 return (prog->argc);
5003 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5009 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5013 spawn(r->ws->idx, &a, 1);
5015 for (i = 0; i < spawn_argc; i++)
5021 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5027 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5031 if (pipe(select_list_pipe) == -1)
5032 err(1, "pipe error");
5033 if (pipe(select_resp_pipe) == -1)
5034 err(1, "pipe error");
5036 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5037 err(1, "could not disable SIGPIPE");
5038 switch (*pid = fork()) {
5040 err(1, "cannot fork");
5043 if (dup2(select_list_pipe[0], 0) == -1)
5045 if (dup2(select_resp_pipe[1], 1) == -1)
5047 close(select_list_pipe[1]);
5048 close(select_resp_pipe[0]);
5049 spawn(r->ws->idx, &a, 0);
5051 default: /* parent */
5052 close(select_list_pipe[0]);
5053 close(select_resp_pipe[1]);
5057 for (i = 0; i < spawn_argc; i++)
5063 spawn_insert(char *name, char *args)
5065 char *arg, *cp, *ptr;
5066 struct spawn_prog *sp;
5068 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5070 if ((sp = calloc(1, sizeof *sp)) == NULL)
5071 err(1, "spawn_insert: malloc");
5072 if ((sp->name = strdup(name)) == NULL)
5073 err(1, "spawn_insert: strdup");
5075 /* convert the arguments to an argument list */
5076 if ((ptr = cp = strdup(args)) == NULL)
5077 err(1, "spawn_insert: strdup");
5078 while ((arg = strsep(&ptr, " \t")) != NULL) {
5079 /* empty field; skip it */
5084 if ((sp->argv = realloc(sp->argv, sp->argc *
5085 sizeof *sp->argv)) == NULL)
5086 err(1, "spawn_insert: realloc");
5087 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5088 err(1, "spawn_insert: strdup");
5092 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5093 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5097 spawn_remove(struct spawn_prog *sp)
5101 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5103 TAILQ_REMOVE(&spawns, sp, entry);
5104 for (i = 0; i < sp->argc; i++)
5110 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5114 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5116 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5119 spawn_insert(name, args);
5121 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5125 setspawn(char *name, char *args)
5127 struct spawn_prog *sp;
5129 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5134 TAILQ_FOREACH(sp, &spawns, entry) {
5135 if (!strcmp(sp->name, name)) {
5139 spawn_replace(sp, name, args);
5140 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5144 if (*args == '\0') {
5145 warnx("error: setspawn: cannot find program: %s", name);
5149 spawn_insert(name, args);
5150 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5154 setconfspawn(char *selector, char *value, int flags)
5156 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5158 setspawn(selector, value);
5160 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5167 setconfspawn("term", "xterm", 0);
5168 setconfspawn("spawn_term", "xterm", 0);
5169 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5170 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5171 setconfspawn("lock", "xlock", 0);
5172 setconfspawn("initscr", "initscreen.sh", 0);
5173 setconfspawn("menu", "dmenu_run"
5176 " -nf $bar_font_color"
5178 " -sf $bar_color", 0);
5179 setconfspawn("search", "dmenu"
5183 " -nf $bar_font_color"
5185 " -sf $bar_color", 0);
5186 setconfspawn("name_workspace", "dmenu"
5190 " -nf $bar_font_color"
5192 " -sf $bar_color", 0);
5196 #define SWM_MODNAME_SIZE 32
5197 #define SWM_KEY_WS "\n+ \t"
5199 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5203 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5204 if (mod == NULL || ks == NULL) {
5205 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5208 if (keystr == NULL || strlen(keystr) == 0) {
5209 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5215 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5216 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5218 cp += (long)strspn(cp, SWM_KEY_WS);
5219 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5221 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5223 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5225 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5227 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5229 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5231 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5232 *mod |= ControlMask;
5234 *ks = XStringToKeysym(name);
5235 XConvertCase(*ks, ks, &uks);
5236 if (ks == NoSymbol) {
5238 "parsekeys: invalid key %s\n",
5244 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5249 strdupsafe(char *str)
5254 return (strdup(str));
5258 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5262 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5263 keyfuncs[kfid].name, spawn_name);
5265 if ((kp = malloc(sizeof *kp)) == NULL)
5266 err(1, "key_insert: malloc");
5271 kp->spawn_name = strdupsafe(spawn_name);
5272 RB_INSERT(key_tree, &keys, kp);
5274 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5278 key_lookup(unsigned int mod, KeySym ks)
5285 return (RB_FIND(key_tree, &keys, &kp));
5289 key_remove(struct key *kp)
5291 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5293 RB_REMOVE(key_tree, &keys, kp);
5294 free(kp->spawn_name);
5297 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5301 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5304 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5308 key_insert(mod, ks, kfid, spawn_name);
5310 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5314 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5319 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5320 keyfuncs[kfid].name, spawn_name);
5322 if ((kp = key_lookup(mod, ks)) != NULL) {
5323 if (kfid == kf_invalid)
5326 key_replace(kp, mod, ks, kfid, spawn_name);
5327 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5330 if (kfid == kf_invalid) {
5331 warnx("error: setkeybinding: cannot find mod/key combination");
5332 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5336 key_insert(mod, ks, kfid, spawn_name);
5337 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5341 setconfbinding(char *selector, char *value, int flags)
5343 enum keyfuncid kfid;
5346 struct spawn_prog *sp;
5347 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5348 if (selector == NULL) {
5349 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5350 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5352 setkeybinding(mod, ks, kfid, NULL);
5357 /* search by key function name */
5358 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5359 if (strncasecmp(selector, keyfuncs[kfid].name,
5360 SWM_FUNCNAME_LEN) == 0) {
5361 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5363 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5364 setkeybinding(mod, ks, kfid, NULL);
5370 /* search by custom spawn name */
5371 TAILQ_FOREACH(sp, &spawns, entry) {
5372 if (strcasecmp(selector, sp->name) == 0) {
5373 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5375 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5376 setkeybinding(mod, ks, kf_spawn_custom,
5383 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5390 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5391 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5392 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5393 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5394 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5395 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5396 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5397 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5398 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5399 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5400 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5401 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5402 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5403 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5404 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5405 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5406 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5407 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5408 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5409 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5410 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5411 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5412 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5413 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5414 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5415 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5416 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5417 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5418 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5419 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5420 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5421 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5422 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5423 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5424 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5425 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5426 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5427 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5428 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5429 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5430 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5431 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5432 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5433 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5434 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5435 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5436 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5437 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5438 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5439 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5440 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5441 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5442 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5443 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5444 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5445 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5446 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5447 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5448 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5449 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5450 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5451 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5452 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5453 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5454 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5455 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5456 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5457 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5458 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5459 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5460 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5461 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5462 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5463 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5464 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5465 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5466 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5467 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5468 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5469 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5470 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5471 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5472 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5473 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5474 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5475 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5476 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5477 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5478 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5479 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5480 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5481 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5482 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5483 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5484 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5485 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5487 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5496 while (RB_EMPTY(&keys) == 0) {
5497 kp = RB_ROOT(&keys);
5503 setkeymapping(char *selector, char *value, int flags)
5505 char keymapping_file[PATH_MAX];
5506 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5507 if (value[0] == '~')
5508 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5509 pwd->pw_dir, &value[1]);
5511 strlcpy(keymapping_file, value, sizeof keymapping_file);
5513 /* load new key bindings; if it fails, revert to default bindings */
5514 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5518 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5523 updatenumlockmask(void)
5526 XModifierKeymap *modmap;
5528 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5530 modmap = XGetModifierMapping(display);
5531 for (i = 0; i < 8; i++)
5532 for (j = 0; j < modmap->max_keypermod; j++)
5533 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5534 == XKeysymToKeycode(display, XK_Num_Lock))
5535 numlockmask = (1 << i);
5537 XFreeModifiermap(modmap);
5546 unsigned int modifiers[] =
5547 { 0, LockMask, numlockmask, numlockmask | LockMask };
5550 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5551 updatenumlockmask();
5553 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5554 for (k = 0; k < num_screens; k++) {
5555 if (TAILQ_EMPTY(&screens[k].rl))
5557 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5559 RB_FOREACH(kp, key_tree, &keys) {
5560 if ((code = XKeysymToKeycode(display, kp->keysym)))
5561 for (j = 0; j < LENGTH(modifiers); j++)
5562 XGrabKey(display, code,
5563 kp->mod | modifiers[j],
5564 screens[k].root, True,
5565 GrabModeAsync, GrabModeAsync);
5571 grabbuttons(struct ws_win *win, int focused)
5574 unsigned int modifiers[] =
5575 { 0, LockMask, numlockmask, numlockmask|LockMask };
5577 updatenumlockmask();
5578 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5579 XCB_BUTTON_MASK_ANY);
5581 for (i = 0; i < LENGTH(buttons); i++)
5582 if (buttons[i].action == client_click)
5583 for (j = 0; j < LENGTH(modifiers); j++)
5584 xcb_grab_button(conn, False, win->id,
5586 XCB_GRAB_MODE_ASYNC,
5593 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5594 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC,
5595 XCB_WINDOW_NONE, XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY,
5596 XCB_BUTTON_MASK_ANY);
5599 const char *quirkname[] = {
5600 "NONE", /* config string for "no value" */
5609 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5610 #define SWM_Q_WS "\n|+ \t"
5612 parsequirks(char *qstr, unsigned long *quirk)
5622 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5624 cp += (long)strspn(cp, SWM_Q_WS);
5625 for (i = 0; i < LENGTH(quirkname); i++) {
5626 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5627 DNPRINTF(SWM_D_QUIRK,
5628 "parsequirks: %s\n", name);
5633 *quirk |= 1 << (i-1);
5637 if (i >= LENGTH(quirkname)) {
5638 DNPRINTF(SWM_D_QUIRK,
5639 "parsequirks: invalid quirk [%s]\n", name);
5647 quirk_insert(const char *class, const char *name, unsigned long quirk)
5651 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5654 if ((qp = malloc(sizeof *qp)) == NULL)
5655 err(1, "quirk_insert: malloc");
5656 if ((qp->class = strdup(class)) == NULL)
5657 err(1, "quirk_insert: strdup");
5658 if ((qp->name = strdup(name)) == NULL)
5659 err(1, "quirk_insert: strdup");
5662 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5664 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5668 quirk_remove(struct quirk *qp)
5670 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5671 qp->name, qp->quirk);
5673 TAILQ_REMOVE(&quirks, qp, entry);
5678 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5682 quirk_replace(struct quirk *qp, const char *class, const char *name,
5683 unsigned long quirk)
5685 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5686 qp->name, qp->quirk);
5689 quirk_insert(class, name, quirk);
5691 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5695 setquirk(const char *class, const char *name, unsigned long quirk)
5699 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5702 TAILQ_FOREACH(qp, &quirks, entry) {
5703 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5707 quirk_replace(qp, class, name, quirk);
5708 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5713 warnx("error: setquirk: cannot find class/name combination");
5717 quirk_insert(class, name, quirk);
5718 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5722 setconfquirk(char *selector, char *value, int flags)
5724 char *cp, *class, *name;
5726 unsigned long quirks;
5727 if (selector == NULL)
5729 if ((cp = strchr(selector, ':')) == NULL)
5734 if ((retval = parsequirks(value, &quirks)) == 0)
5735 setquirk(class, name, quirks);
5742 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5743 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5744 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5745 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5746 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5747 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5748 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5749 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5750 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5751 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5752 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5753 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5754 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5757 /* conf file stuff */
5758 #define SWM_CONF_FILE "spectrwm.conf"
5759 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5763 SWM_S_BAR_AT_BOTTOM,
5764 SWM_S_BAR_BORDER_WIDTH,
5771 SWM_S_CLOCK_ENABLED,
5774 SWM_S_CYCLE_VISIBLE,
5776 SWM_S_DISABLE_BORDER,
5778 SWM_S_FOCUS_CLOSE_WRAP,
5779 SWM_S_FOCUS_DEFAULT,
5785 SWM_S_STACK_ENABLED,
5787 SWM_S_TITLE_CLASS_ENABLED,
5788 SWM_S_TITLE_NAME_ENABLED,
5789 SWM_S_URGENT_ENABLED,
5790 SWM_S_VERBOSE_LAYOUT,
5791 SWM_S_WINDOW_NAME_ENABLED,
5792 SWM_S_WORKSPACE_LIMIT
5796 setconfvalue(char *selector, char *value, int flags)
5802 case SWM_S_BAR_ACTION:
5804 if ((bar_argv[0] = strdup(value)) == NULL)
5805 err(1, "setconfvalue: bar_action");
5807 case SWM_S_BAR_AT_BOTTOM:
5808 bar_at_bottom = atoi(value);
5810 case SWM_S_BAR_BORDER_WIDTH:
5811 bar_border_width = atoi(value);
5812 if (bar_border_width < 0)
5813 bar_border_width = 0;
5815 case SWM_S_BAR_DELAY:
5816 bar_delay = atoi(value);
5818 case SWM_S_BAR_ENABLED:
5819 bar_enabled = atoi(value);
5821 case SWM_S_BAR_FONT:
5823 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5824 err(1, "setconfvalue: asprintf: failed to allocate "
5825 "memory for bar_fonts.");
5829 case SWM_S_BAR_FORMAT:
5831 if ((bar_format = strdup(value)) == NULL)
5832 err(1, "setconfvalue: bar_format");
5834 case SWM_S_BAR_JUSTIFY:
5835 if (!strcmp(value, "left"))
5836 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5837 else if (!strcmp(value, "center"))
5838 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5839 else if (!strcmp(value, "right"))
5840 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5842 errx(1, "invalid bar_justify");
5844 case SWM_S_BORDER_WIDTH:
5845 border_width = atoi(value);
5846 if (border_width < 0)
5849 case SWM_S_CLOCK_ENABLED:
5850 clock_enabled = atoi(value);
5852 case SWM_S_CLOCK_FORMAT:
5853 #ifndef SWM_DENY_CLOCK_FORMAT
5855 if ((clock_format = strdup(value)) == NULL)
5856 err(1, "setconfvalue: clock_format");
5859 case SWM_S_CYCLE_EMPTY:
5860 cycle_empty = atoi(value);
5862 case SWM_S_CYCLE_VISIBLE:
5863 cycle_visible = atoi(value);
5865 case SWM_S_DIALOG_RATIO:
5866 dialog_ratio = atof(value);
5867 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5870 case SWM_S_DISABLE_BORDER:
5871 disable_border = atoi(value);
5873 case SWM_S_FOCUS_CLOSE:
5874 if (!strcmp(value, "first"))
5875 focus_close = SWM_STACK_BOTTOM;
5876 else if (!strcmp(value, "last"))
5877 focus_close = SWM_STACK_TOP;
5878 else if (!strcmp(value, "next"))
5879 focus_close = SWM_STACK_ABOVE;
5880 else if (!strcmp(value, "previous"))
5881 focus_close = SWM_STACK_BELOW;
5883 errx(1, "focus_close");
5885 case SWM_S_FOCUS_CLOSE_WRAP:
5886 focus_close_wrap = atoi(value);
5888 case SWM_S_FOCUS_DEFAULT:
5889 if (!strcmp(value, "last"))
5890 focus_default = SWM_STACK_TOP;
5891 else if (!strcmp(value, "first"))
5892 focus_default = SWM_STACK_BOTTOM;
5894 errx(1, "focus_default");
5896 case SWM_S_FOCUS_MODE:
5897 if (!strcmp(value, "default"))
5898 focus_mode = SWM_FOCUS_DEFAULT;
5899 else if (!strcmp(value, "follow_cursor"))
5900 focus_mode = SWM_FOCUS_FOLLOW;
5901 else if (!strcmp(value, "synergy"))
5902 focus_mode = SWM_FOCUS_SYNERGY;
5904 errx(1, "focus_mode");
5906 case SWM_S_SPAWN_ORDER:
5907 if (!strcmp(value, "first"))
5908 spawn_position = SWM_STACK_BOTTOM;
5909 else if (!strcmp(value, "last"))
5910 spawn_position = SWM_STACK_TOP;
5911 else if (!strcmp(value, "next"))
5912 spawn_position = SWM_STACK_ABOVE;
5913 else if (!strcmp(value, "previous"))
5914 spawn_position = SWM_STACK_BELOW;
5916 errx(1, "spawn_position");
5918 case SWM_S_SPAWN_TERM:
5919 setconfspawn("term", value, 0);
5920 setconfspawn("spawn_term", value, 0);
5924 case SWM_S_SS_ENABLED:
5925 ss_enabled = atoi(value);
5927 case SWM_S_STACK_ENABLED:
5928 stack_enabled = atoi(value);
5930 case SWM_S_TERM_WIDTH:
5931 term_width = atoi(value);
5935 case SWM_S_TITLE_CLASS_ENABLED:
5936 title_class_enabled = atoi(value);
5938 case SWM_S_TITLE_NAME_ENABLED:
5939 title_name_enabled = atoi(value);
5941 case SWM_S_URGENT_ENABLED:
5942 urgent_enabled = atoi(value);
5944 case SWM_S_VERBOSE_LAYOUT:
5945 verbose_layout = atoi(value);
5946 for (i = 0; layouts[i].l_stack != NULL; i++) {
5948 layouts[i].l_string = fancy_stacker;
5950 layouts[i].l_string = plain_stacker;
5953 case SWM_S_WINDOW_NAME_ENABLED:
5954 window_name_enabled = atoi(value);
5956 case SWM_S_WORKSPACE_LIMIT:
5957 workspace_limit = atoi(value);
5958 if (workspace_limit > SWM_WS_MAX)
5959 workspace_limit = SWM_WS_MAX;
5960 else if (workspace_limit < 1)
5961 workspace_limit = 1;
5970 setconfmodkey(char *selector, char *value, int flags)
5972 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5973 update_modkey(Mod1Mask);
5974 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5975 update_modkey(Mod2Mask);
5976 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5977 update_modkey(Mod3Mask);
5978 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5979 update_modkey(Mod4Mask);
5986 setconfcolor(char *selector, char *value, int flags)
5988 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5993 setconfregion(char *selector, char *value, int flags)
5995 custom_region(value);
6000 setautorun(char *selector, char *value, int flags)
6010 if (getenv("SWM_STARTED"))
6014 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6015 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6017 if (ws_id < 0 || ws_id >= workspace_limit)
6018 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6021 * This is a little intricate
6023 * If the pid already exists we simply reuse it because it means it was
6024 * used before AND not claimed by manage_window. We get away with
6025 * altering it in the parent after INSERT because this can not be a race
6028 while ((ap = strsep(&sp, " \t")) != NULL) {
6031 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6033 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6034 err(1, "setautorun: realloc");
6035 a.argv[argc - 1] = ap;
6038 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6039 err(1, "setautorun: realloc");
6040 a.argv[argc] = NULL;
6042 if ((pid = fork()) == 0) {
6043 spawn(ws_id, &a, 1);
6052 p = calloc(1, sizeof *p);
6055 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6065 setlayout(char *selector, char *value, int flags)
6067 int ws_id, i, x, mg, ma, si, raise, f = 0;
6068 int st = SWM_V_STACK, num_screens;
6070 struct workspace *ws;
6072 if (getenv("SWM_STARTED"))
6076 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6077 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6078 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6079 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6082 if (ws_id < 0 || ws_id >= workspace_limit)
6083 errx(1, "layout: invalid workspace %d", ws_id + 1);
6085 if (!strcasecmp(s, "vertical"))
6087 else if (!strcasecmp(s, "vertical_flip")) {
6090 } else if (!strcasecmp(s, "horizontal"))
6092 else if (!strcasecmp(s, "horizontal_flip")) {
6095 } else if (!strcasecmp(s, "fullscreen"))
6098 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6099 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6102 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6103 for (i = 0; i < num_screens; i++) {
6104 ws = (struct workspace *)&screens[i].ws;
6105 ws[ws_id].cur_layout = &layouts[st];
6107 ws[ws_id].always_raise = raise;
6108 if (st == SWM_MAX_STACK)
6112 for (x = 0; x < abs(mg); x++) {
6113 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6114 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6115 SWM_ARG_ID_MASTERSHRINK);
6119 for (x = 0; x < abs(ma); x++) {
6120 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6121 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6122 SWM_ARG_ID_MASTERDEL);
6126 for (x = 0; x < abs(si); x++) {
6127 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6128 si >= 0 ? SWM_ARG_ID_STACKINC :
6129 SWM_ARG_ID_STACKDEC);
6134 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6135 SWM_ARG_ID_FLIPLAYOUT);
6143 /* config options */
6144 struct config_option {
6146 int (*func)(char*, char*, int);
6149 struct config_option configopt[] = {
6150 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6151 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6152 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6153 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6154 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6155 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6156 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6157 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6158 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6159 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6160 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6161 { "keyboard_mapping", setkeymapping, 0 },
6162 { "bind", setconfbinding, 0 },
6163 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6164 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6165 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6166 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6167 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6168 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6169 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6170 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6171 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6172 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6173 { "modkey", setconfmodkey, 0 },
6174 { "program", setconfspawn, 0 },
6175 { "quirk", setconfquirk, 0 },
6176 { "region", setconfregion, 0 },
6177 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6178 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6179 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6180 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6181 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6182 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6183 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6184 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6185 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6186 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6187 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6188 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6189 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6190 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6191 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6192 { "autorun", setautorun, 0 },
6193 { "layout", setlayout, 0 },
6198 conf_load(char *filename, int keymapping)
6201 char *line, *cp, *optsub, *optval;
6202 size_t linelen, lineno = 0;
6203 int wordlen, i, optind;
6204 struct config_option *opt;
6206 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6208 if (filename == NULL) {
6209 warnx("conf_load: no filename");
6212 if ((config = fopen(filename, "r")) == NULL) {
6213 warn("conf_load: fopen: %s", filename);
6217 while (!feof(config)) {
6218 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6221 err(1, "%s", filename);
6226 cp += strspn(cp, " \t\n"); /* eat whitespace */
6227 if (cp[0] == '\0') {
6232 /* get config option */
6233 wordlen = strcspn(cp, "=[ \t\n");
6235 warnx("%s: line %zd: no option found",
6240 for (i = 0; i < LENGTH(configopt); i++) {
6241 opt = &configopt[i];
6242 if (!strncasecmp(cp, opt->optname, wordlen) &&
6243 strlen(opt->optname) == wordlen) {
6249 warnx("%s: line %zd: unknown option %.*s",
6250 filename, lineno, wordlen, cp);
6253 if (keymapping && strcmp(opt->optname, "bind")) {
6254 warnx("%s: line %zd: invalid option %.*s",
6255 filename, lineno, wordlen, cp);
6259 cp += strspn(cp, " \t\n"); /* eat whitespace */
6260 /* get [selector] if any */
6264 wordlen = strcspn(cp, "]");
6267 warnx("%s: line %zd: syntax error",
6272 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6274 warnx("%s: line %zd: unable to allocate"
6275 "memory for selector", filename,
6281 cp += strspn(cp, "] \t\n"); /* eat trailing */
6283 cp += strspn(cp, "= \t\n"); /* eat trailing */
6285 optval = strdup(cp);
6286 /* call function to deal with it all */
6287 if (configopt[optind].func(optsub, optval,
6288 configopt[optind].funcflags) != 0)
6289 errx(1, "%s: line %zd: invalid data for %s",
6290 filename, lineno, configopt[optind].optname);
6297 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6304 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6310 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6312 struct ws_win *parent, *w;
6313 struct swm_region *r;
6314 struct workspace *ws;
6317 parent = find_window(win->transient);
6319 parent->child_trans = win;
6321 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6322 " for 0x%x trans 0x%x\n", win->id, win->transient);
6324 r = root_to_region(win->wa->root);
6326 /* parent doen't exist in our window list */
6327 TAILQ_FOREACH(w, &ws->winlist, entry) {
6328 if (xcb_icccm_get_wm_hints_reply(conn,
6329 xcb_icccm_get_wm_hints(conn, w->id),
6331 warnx("can't get hints for 0x%x", w->id);
6335 if (win->hints.window_group != wmh.window_group)
6338 w->child_trans = win;
6339 win->transient = w->id;
6341 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6342 "transient to 0x%x\n", win->transient);
6349 window_get_pid(xcb_window_t win)
6354 xcb_get_property_cookie_t pc;
6355 xcb_get_property_reply_t *pr;
6357 apid = get_atom_from_string("_NET_WM_PID");
6358 if (apid == XCB_ATOM_NONE)
6361 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6362 pr = xcb_get_property_reply(conn, pc, NULL);
6365 if (pr->type != XCB_ATOM_CARDINAL)
6368 ret = *(long *)xcb_get_property_value(pr);
6374 apid = get_atom_from_string("_SWM_PID");
6375 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6377 pr = xcb_get_property_reply(conn, pc, NULL);
6380 if (pr->type != XCB_ATOM_STRING)
6383 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6390 manage_window(xcb_window_t id)
6392 xcb_window_t trans = XCB_WINDOW_NONE;
6393 struct workspace *ws;
6394 struct ws_win *win, *ww;
6395 int i, ws_idx, border_me = 0;
6396 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
6397 char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6399 struct swm_region *r;
6403 uint32_t event_mask;
6405 xcb_get_property_reply_t *gpr;
6406 xcb_get_wm_protocols_reply_t wpr;
6408 if ((win = find_window(id)) != NULL)
6409 return (win); /* already being managed */
6411 /* see if we are on the unmanaged list */
6412 if ((win = find_unmanaged_window(id)) != NULL) {
6413 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6414 "window: 0x%x\n", win->id);
6415 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6417 set_child_transient(win, &trans);
6419 if (trans && (ww = find_window(trans)))
6420 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6421 else if ((ww = win->ws->focus) &&
6422 spawn_position == SWM_STACK_ABOVE)
6423 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6425 else if (ww && spawn_position == SWM_STACK_BELOW)
6426 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6427 else switch (spawn_position) {
6430 case SWM_STACK_ABOVE:
6431 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6433 case SWM_STACK_BOTTOM:
6434 case SWM_STACK_BELOW:
6435 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6438 ewmh_update_actions(win);
6442 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6443 err(1, "manage_window: calloc: failed to allocate memory for "
6449 /* see if we need to override the workspace */
6450 p = find_pid(window_get_pid(id));
6452 /* Get all the window data in one shot */
6453 ws_idx_atom = get_atom_from_string("_SWM_WS");
6454 fprintf(stderr, "ws_idx_atom: %d\n", ws_idx_atom);
6456 gpr = xcb_get_property_reply(conn,
6457 xcb_get_property(conn, False, id, ws_idx_atom,
6458 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6461 proplen = xcb_get_property_value_length(gpr);
6463 prop = malloc(proplen + 1);
6466 xcb_get_property_value(gpr),
6468 prop[proplen] = '\0';
6474 win->wa = xcb_get_geometry_reply(conn,
6475 xcb_get_geometry(conn, id),
6477 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6478 xcb_icccm_get_wm_hints_reply(conn,
6479 xcb_icccm_get_wm_hints(conn, id),
6481 xcb_get_wm_transient_for_reply(conn,
6482 xcb_get_wm_transient_for(conn, id),
6485 win->transient = trans;
6486 set_child_transient(win, &trans);
6487 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6488 "transient: 0x%x\n", win->id, win->transient);
6491 prot = xcb_atom_get_fast_reply(conn,
6492 xcb_atom_get_fast(conn, False, strlen("WM_PROTOCOLS"),
6495 /* get supported protocols */
6496 if (xcb_get_wm_protocols_reply(conn,
6497 xcb_get_wm_protocols(conn, id, prot),
6499 for (i = 0; i < wpr.atoms_len; i++) {
6500 if (wpr.atoms[i] == takefocus)
6501 win->take_focus = 1;
6502 if (wpr.atoms[i] == adelete)
6503 win->can_delete = 1;
6505 xcb_get_wm_protocols_reply_wipe(&wpr);
6508 win->iconic = get_iconic(win);
6511 * Figure out where to put the window. If it was previously assigned to
6512 * a workspace (either by spawn() or manually moving), and isn't
6513 * transient, * put it in the same workspace
6515 r = root_to_region(win->wa->root);
6517 ws = &r->s->ws[p->ws];
6518 TAILQ_REMOVE(&pidlist, p, entry);
6521 } else if (prop && win->transient == 0) {
6522 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6523 ws_idx = strtonum(prop, 0, workspace_limit - 1,
6526 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6529 ws = &r->s->ws[ws_idx];
6532 /* this should launch transients in the same ws as parent */
6534 if ((ww = find_window(trans)) != NULL)
6540 warnx("manage_window: fix this "
6546 /* set up the window layout */
6549 win->s = r->s; /* this never changes */
6550 if (trans && (ww = find_window(trans)))
6551 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6552 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6553 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6555 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6556 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6557 else switch (spawn_position) {
6560 case SWM_STACK_ABOVE:
6561 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6563 case SWM_STACK_BOTTOM:
6564 case SWM_STACK_BELOW:
6565 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6568 /* ignore window border if there is one. */
6569 WIDTH(win) = win->wa->width;
6570 HEIGHT(win) = win->wa->height;
6571 X(win) = win->wa->x + win->wa->border_width;
6572 Y(win) = win->wa->y + win->wa->border_width;
6574 win->g_floatvalid = 0;
6575 win->floatmaxed = 0;
6576 win->ewmh_flags = 0;
6578 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6579 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6580 HEIGHT(win), ws->idx);
6582 constrain_window(win, r, 0);
6584 /* Set window properties so we can remember this after reincarnation */
6585 if (ws_idx_atom && prop == NULL &&
6586 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6588 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6590 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6591 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6597 ewmh_autoquirk(win);
6599 if (xcb_get_wm_class_reply(conn,
6600 xcb_get_wm_class(conn, win->id),
6602 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6603 win->ch.class_name, win->ch.instance_name);
6605 /* java is retarded so treat it special */
6606 if (strstr(win->ch.instance_name, "sun-awt")) {
6611 TAILQ_FOREACH(qp, &quirks, entry) {
6612 if (!strcmp(win->ch.class_name, qp->class) &&
6613 !strcmp(win->ch.instance_name, qp->name)) {
6614 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6615 "class: %s, name: %s\n", win->ch.class_name,
6616 win->ch.instance_name);
6617 if (qp->quirk & SWM_Q_FLOAT) {
6621 win->quirks = qp->quirk;
6626 /* alter window position if quirky */
6627 if (win->quirks & SWM_Q_ANYWHERE) {
6628 win->manual = 1; /* don't center the quirky windows */
6629 if (bar_enabled && Y(win) < bar_height)
6630 Y(win) = bar_height;
6631 if (WIDTH(win) + X(win) > WIDTH(r))
6632 X(win) = WIDTH(r) - WIDTH(win) - 2;
6636 /* Reset font sizes (the bruteforce way; no default keybinding). */
6637 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6638 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6639 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6640 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6641 fake_keypress(win, XK_KP_Add, ShiftMask);
6644 ewmh_get_win_state(win);
6645 ewmh_update_actions(win);
6646 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6651 X(win) -= border_width;
6652 Y(win) -= border_width;
6656 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6657 XCB_EVENT_MASK_PROPERTY_CHANGE |
6658 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6660 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6662 /* floaters need to be mapped if they are in the current workspace */
6663 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6664 map_window_raised(win->id);
6670 free_window(struct ws_win *win)
6672 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6677 /* needed for restart wm */
6678 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
6680 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6685 xcb_get_wm_class_reply_wipe(&win->ch);
6690 memset(win, 0xff, sizeof *win); /* XXX kill later */
6696 unmanage_window(struct ws_win *win)
6698 struct ws_win *parent;
6699 xcb_screen_t *screen;
6704 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6706 if (win->transient) {
6707 parent = find_window(win->transient);
6709 parent->child_trans = NULL;
6712 /* focus on root just in case */
6713 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6714 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6715 screen->root, XCB_CURRENT_TIME);
6719 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6720 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6724 focus_magic(struct ws_win *win)
6726 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6729 /* if there are no windows clear the status-bar */
6734 if (win->child_trans) {
6735 /* win = parent & has a transient so focus on that */
6737 focus_win(win->child_trans);
6738 if (win->child_trans->take_focus)
6739 client_msg(win, takefocus);
6741 /* make sure transient hasn't disappeared */
6742 if (validate_win(win->child_trans) == 0) {
6743 focus_win(win->child_trans);
6744 if (win->child_trans->take_focus)
6745 client_msg(win->child_trans, takefocus);
6747 win->child_trans = NULL;
6749 if (win->take_focus)
6750 client_msg(win, takefocus);
6756 if (win->take_focus)
6757 client_msg(win, takefocus);
6764 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6771 XKeyEvent *ev = &e->xkey;
6773 struct swm_region *r;
6775 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6776 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6778 if (keyfuncs[kp->funcid].func == NULL)
6781 r = root_to_region(ev->root);
6782 if (kp->funcid == kf_spawn_custom)
6783 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6785 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6789 buttonpress(XEvent *e)
6793 XButtonPressedEvent *ev = &e->xbutton;
6795 if ((win = find_window(ev->window)) == NULL)
6799 action = client_click;
6801 for (i = 0; i < LENGTH(buttons); i++)
6802 if (action == buttons[i].action && buttons[i].func &&
6803 buttons[i].button == ev->button &&
6804 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6805 buttons[i].func(win, &buttons[i].args);
6809 configurerequest(XEvent *e)
6811 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6816 if ((win = find_window(ev->window)) == NULL)
6817 if ((win = find_unmanaged_window(ev->window)) == NULL)
6821 bzero(&wc, sizeof wc);
6824 wc.width = ev->width;
6825 wc.height = ev->height;
6826 wc.border_width = ev->border_width;
6827 wc.sibling = ev->above;
6828 wc.stack_mode = ev->detail;
6830 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%lx, "
6831 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev->window,
6832 YESNO(new), wc.x, wc.y, wc.width, wc.height);
6834 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6835 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6836 !(win->sh_mask & EWMH_F_FULLSCREEN)) {
6837 win->g_float.x = ev->x - X(win->ws->r);
6838 win->g_float.y = ev->y - Y(win->ws->r);
6839 win->g_float.w = ev->width;
6840 win->g_float.h = ev->height;
6841 win->g_floatvalid = 1;
6843 if (win->floating) {
6844 win->g = win->g_float;
6845 win->g.x += X(win->ws->r);
6846 win->g.y += Y(win->ws->r);
6849 config_win(win, ev);
6852 config_win(win, ev);
6857 configurenotify(XEvent *e)
6861 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6862 e->xconfigure.window);
6864 win = find_window(e->xconfigure.window);
6866 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6870 if (focus_mode == SWM_FOCUS_DEFAULT)
6871 drain_enter_notify();
6876 destroynotify(XEvent *e)
6879 XDestroyWindowEvent *ev = &e->xdestroywindow;
6881 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6883 if ((win = find_window(ev->window)) == NULL) {
6884 if ((win = find_unmanaged_window(ev->window)) == NULL)
6890 /* make sure we focus on something */
6893 unmanage_window(win);
6895 if (focus_mode == SWM_FOCUS_DEFAULT)
6896 drain_enter_notify();
6901 enternotify(XEvent *e)
6903 XCrossingEvent *ev = &e->xcrossing;
6908 Window focus_return;
6909 int revert_to_return;
6911 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6912 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6913 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6914 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6916 if (ev->mode != NotifyNormal) {
6917 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6922 switch (focus_mode) {
6923 case SWM_FOCUS_DEFAULT:
6925 case SWM_FOCUS_FOLLOW:
6927 case SWM_FOCUS_SYNERGY:
6930 * all these checks need to be in this order because the
6931 * XCheckTypedWindowEvent relies on weeding out the previous events
6933 * making this code an option would enable a follow mouse for focus
6938 * state is set when we are switching workspaces and focus is set when
6939 * the window or a subwindow already has focus (occurs during restart).
6941 * Only honor the focus flag if last_focus_event is not FocusOut,
6942 * this allows spectrwm to continue to control focus when another
6943 * program is also playing with it.
6945 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6946 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6951 * happens when a window is created or destroyed and the border
6952 * crosses the mouse pointer and when switching ws
6954 * we need the subwindow test to see if we came from root in order
6955 * to give focus to floaters
6957 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6958 ev->subwindow == 0) {
6959 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6963 /* this window already has focus */
6964 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6965 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6969 /* this window is being deleted or moved to another ws */
6970 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6972 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6973 XPutBackEvent(display, &cne);
6977 if ((win = find_window(ev->window)) == NULL) {
6978 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6983 * In fullstack kill all enters unless they come from a different ws
6984 * (i.e. another region) or focus has been grabbed externally.
6986 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6987 last_focus_event != FocusOut) {
6988 XGetInputFocus(display, &focus_return, &revert_to_return);
6989 if ((w = find_window(focus_return)) == NULL ||
6991 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6999 if ((win = find_window(ev->window)) == NULL) {
7000 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
7005 * if we have more enternotifies let them handle it in due time
7007 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
7008 DNPRINTF(SWM_D_EVENT,
7009 "ignoring enternotify: got more enternotify\n");
7010 XPutBackEvent(display, &cne);
7017 /* lets us use one switch statement for arbitrary mode/detail combinations */
7018 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7021 focusevent(XEvent *e)
7025 u_int32_t mode_detail;
7026 XFocusChangeEvent *ev = &e->xfocus;
7028 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
7029 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
7030 ev->window, ev->mode, ev->detail);
7032 if (last_focus_event == ev->type) {
7033 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
7037 last_focus_event = ev->type;
7038 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
7040 switch (mode_detail) {
7041 /* synergy client focus operations */
7042 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
7043 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
7045 /* synergy server focus operations */
7046 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
7048 /* Entering applications like rdesktop that mangle the pointer */
7049 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
7051 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
7052 XSetWindowBorder(display, win->id,
7053 win->ws->r->s->c[ev->type == FocusIn ?
7054 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
7057 warnx("ignoring focusevent");
7058 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
7065 mapnotify(XEvent *e)
7068 XMapEvent *ev = &e->xmap;
7070 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
7072 win = manage_window(ev->window);
7074 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7077 * focus_win can only set input focus on a mapped window.
7078 * make sure the window really has focus since it is just being mapped.
7080 if (win->ws->focus == win)
7085 mappingnotify(XEvent *e)
7087 XMappingEvent *ev = &e->xmapping;
7089 XRefreshKeyboardMapping(ev);
7090 if (ev->request == MappingKeyboard)
7095 maprequest(XEvent *e)
7098 struct swm_region *r;
7099 XMapRequestEvent *ev = &e->xmaprequest;
7100 xcb_get_window_attributes_reply_t *war;
7102 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
7103 e->xmaprequest.window);
7105 war = xcb_get_window_attributes_reply(conn,
7106 xcb_get_window_attributes(conn, ev->window),
7110 if (war->override_redirect) {
7116 win = manage_window(e->xmaprequest.window);
7118 return; /* can't happen */
7123 /* make new win focused */
7124 r = root_to_region(win->wa->root);
7125 if (win->ws == r->ws)
7130 propertynotify(XEvent *e)
7133 XPropertyEvent *ev = &e->xproperty;
7137 xcb_get_atom_name_reply_t *r;
7139 r = xcb_get_atom_name_reply(conn,
7140 xcb_get_atom_name(conn, ev->atom),
7143 len = xcb_get_atom_name_name_length(r);
7145 name = malloc(len + 1);
7147 memcpy(name, xcb_get_atom_name_name(r), len);
7150 DNPRINTF(SWM_D_EVENT,
7151 "propertynotify: window: 0x%lx, "
7161 win = find_window(ev->window);
7165 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
7166 update_iconic(win, 0);
7167 map_window_raised(win->id);
7175 case XA_WM_NORMAL_HINTS:
7177 XGetWMNormalHints(display, win->id, &win->sh, &mask);
7178 warnx("normal hints: flag 0x%x", win->sh.flags);
7179 if (win->sh.flags & PMinSize) {
7180 WIDTH(win) = win->sh.min_width;
7181 HEIGHT(win) = win->sh.min_height;
7182 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7184 XMoveResizeWindow(display, win->id,
7185 X(win), Y(win), WIDTH(win), HEIGHT(win));
7197 unmapnotify(XEvent *e)
7201 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
7203 /* determine if we need to help unmanage this window */
7204 win = find_window(e->xunmap.window);
7208 if (getstate(e->xunmap.window) == XCB_ICCCM_WM_STATE_NORMAL) {
7209 unmanage_window(win);
7212 /* giant hack for apps that don't destroy transient windows */
7213 /* eat a bunch of events to prevent remanaging the window */
7215 while (XCheckWindowEvent(display, e->xunmap.window,
7216 EnterWindowMask, &cne))
7218 while (XCheckWindowEvent(display, e->xunmap.window,
7219 StructureNotifyMask, &cne))
7221 while (XCheckWindowEvent(display, e->xunmap.window,
7222 SubstructureNotifyMask, &cne))
7224 /* resend unmap because we ated it */
7225 XUnmapWindow(display, e->xunmap.window);
7228 if (focus_mode == SWM_FOCUS_DEFAULT)
7229 drain_enter_notify();
7233 visibilitynotify(XEvent *e)
7236 struct swm_region *r;
7238 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
7239 e->xvisibility.window);
7241 if (e->xvisibility.state == VisibilityUnobscured) {
7242 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7243 for (i = 0; i < num_screens; i++)
7244 TAILQ_FOREACH(r, &screens[i].rl, entry)
7245 if (e->xvisibility.window == WINID(r->bar))
7251 clientmessage(XEvent *e)
7253 XClientMessageEvent *ev;
7258 win = find_window(ev->window);
7260 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7261 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7262 "unmanaged window.\n");
7263 e->xmaprequest.window = ev->window;
7269 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
7270 ev->window, ev->message_type);
7272 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7273 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7276 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7277 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7278 if (win->can_delete)
7279 client_msg(win, adelete);
7281 xcb_kill_client(conn, win->id);
7283 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7284 DNPRINTF(SWM_D_EVENT,
7285 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7286 if (win->floating) {
7287 if (ev->data.l[0] & (1<<8)) /* x */
7288 X(win) = ev->data.l[1];
7289 if (ev->data.l[0] & (1<<9)) /* y */
7290 Y(win) = ev->data.l[2];
7291 if (ev->data.l[0] & (1<<10)) /* width */
7292 WIDTH(win) = ev->data.l[3];
7293 if (ev->data.l[0] & (1<<11)) /* height */
7294 HEIGHT(win) = ev->data.l[4];
7299 /* TODO: Change stack sizes */
7300 /* notify no change was made. */
7301 config_win(win, NULL);
7304 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
7305 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7306 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
7308 ewmh_update_win_state(win, ev->data.l[2],
7316 xerror_start(Display *d, XErrorEvent *ee)
7323 xerror(Display *d, XErrorEvent *ee)
7325 /* warnx("error: %p %p", display, ee); */
7333 xerrorxlib = XSetErrorHandler(xerror_start);
7335 /* this causes an error if some other window manager is running */
7336 XSelectInput(display, DefaultRootWindow(display),
7337 SubstructureRedirectMask);
7342 XSetErrorHandler(xerror);
7348 new_region(struct swm_screen *s, int x, int y, int w, int h)
7350 struct swm_region *r, *n;
7351 struct workspace *ws = NULL;
7354 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7355 s->idx, w, h, x, y);
7357 /* remove any conflicting regions */
7358 n = TAILQ_FIRST(&s->rl);
7361 n = TAILQ_NEXT(r, entry);
7362 if (X(r) < (x + w) &&
7363 (X(r) + WIDTH(r)) > x &&
7365 (Y(r) + HEIGHT(r)) > y) {
7366 if (r->ws->r != NULL)
7367 r->ws->old_r = r->ws->r;
7370 TAILQ_REMOVE(&s->rl, r, entry);
7371 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7375 /* search old regions for one to reuse */
7377 /* size + location match */
7378 TAILQ_FOREACH(r, &s->orl, entry)
7379 if (X(r) == x && Y(r) == y &&
7380 HEIGHT(r) == h && WIDTH(r) == w)
7384 TAILQ_FOREACH(r, &s->orl, entry)
7385 if (HEIGHT(r) == h && WIDTH(r) == w)
7389 TAILQ_REMOVE(&s->orl, r, entry);
7390 /* try to use old region's workspace */
7391 if (r->ws->r == NULL)
7394 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7395 err(1, "new_region: calloc: failed to allocate memory "
7398 /* if we don't have a workspace already, find one */
7400 for (i = 0; i < workspace_limit; i++)
7401 if (s->ws[i].r == NULL) {
7408 errx(1, "new_region: no free workspaces");
7419 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7425 #ifdef SWM_XRR_HAS_CRTC
7428 #endif /* SWM_XRR_HAS_CRTC */
7429 struct swm_region *r;
7431 xcb_randr_get_screen_resources_current_cookie_t src;
7432 xcb_randr_get_screen_resources_current_reply_t *srr;
7433 xcb_randr_get_crtc_info_cookie_t cic;
7434 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7435 xcb_randr_crtc_t *crtc;
7436 xcb_screen_t *screen = get_screen(i);
7438 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7439 if (i >= num_screens)
7440 errx(1, "scan_xrandr: invalid screen");
7442 /* remove any old regions */
7443 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7444 r->ws->old_r = r->ws->r = NULL;
7446 TAILQ_REMOVE(&screens[i].rl, r, entry);
7447 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7451 /* map virtual screens onto physical screens */
7452 #ifdef SWM_XRR_HAS_CRTC
7453 if (xrandr_support) {
7454 src = xcb_randr_get_screen_resources_current(conn,
7456 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7459 new_region(&screens[i], 0, 0,
7460 screen->width_in_pixels,
7461 screen->height_in_pixels);
7464 ncrtc = srr->num_crtcs;
7465 for (c = 0; c < ncrtc; c++) {
7466 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7467 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7469 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7472 if (cir->num_outputs == 0) {
7478 new_region(&screens[i], 0, 0,
7479 screen->width_in_pixels,
7480 screen->height_in_pixels);
7482 new_region(&screens[i],
7483 cir->x, cir->y, cir->width, cir->height);
7488 #endif /* SWM_XRR_HAS_CRTC */
7490 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7491 screen->height_in_pixels);
7496 screenchange(XEvent *e)
7498 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7499 struct swm_region *r;
7502 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7504 if (!XRRUpdateConfiguration(e))
7507 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7508 /* silly event doesn't include the screen index */
7509 for (i = 0; i < num_screens; i++)
7510 if (screens[i].root == xe->root)
7512 if (i >= num_screens)
7513 errx(1, "screenchange: screen not found");
7515 /* brute force for now, just re-enumerate the regions */
7518 /* add bars to all regions */
7519 for (i = 0; i < num_screens; i++)
7520 TAILQ_FOREACH(r, &screens[i].rl, entry)
7523 if (focus_mode == SWM_FOCUS_DEFAULT)
7524 drain_enter_notify();
7530 xcb_window_t *wins = NULL;
7532 int i, j, num_screens;
7533 uint16_t state, manage;
7535 xcb_query_tree_cookie_t qtc;
7536 xcb_query_tree_reply_t *qtr;
7537 xcb_get_window_attributes_cookie_t c;
7538 xcb_get_window_attributes_reply_t *r;
7539 xcb_get_property_cookie_t pc;
7541 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7542 for (i = 0; i < num_screens; i++) {
7543 qtc = xcb_query_tree(conn, screens[i].root);
7544 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7547 wins = xcb_query_tree_children(qtr);
7548 no = xcb_query_tree_children_length(qtr);
7549 /* attach windows to a region */
7550 /* normal windows */
7551 for (j = 0; j < no; j++) {
7552 c = xcb_get_window_attributes(conn, wins[j]);
7553 r = xcb_get_window_attributes_reply(conn, c, NULL);
7556 if (r->override_redirect) {
7561 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7562 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7568 state = getstate(wins[j]);
7569 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7570 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7571 manage_window(wins[j]);
7574 /* transient windows */
7575 for (j = 0; j < no; j++) {
7576 c = xcb_get_window_attributes(conn, wins[j]);
7577 r = xcb_get_window_attributes_reply(conn, c, NULL);
7580 if (r->override_redirect) {
7586 state = getstate(wins[j]);
7587 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7588 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7589 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7591 manage_window(wins[j]);
7600 int i, j, k, num_screens;
7601 struct workspace *ws;
7602 uint32_t gcv[1], wa[1];
7603 const xcb_query_extension_reply_t *qep;
7604 xcb_cursor_t cursor;
7605 xcb_font_t cursor_font;
7606 xcb_randr_query_version_cookie_t c;
7607 xcb_randr_query_version_reply_t *r;
7609 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7610 if ((screens = calloc(num_screens,
7611 sizeof(struct swm_screen))) == NULL)
7612 err(1, "setup_screens: calloc: failed to allocate memory for "
7615 /* initial Xrandr setup */
7616 xrandr_support = False;
7617 c = xcb_randr_query_version(conn, True, True);
7618 r = xcb_randr_query_version_reply(conn, c, NULL);
7620 if (r->major_version >= 1)
7621 xrandr_support = True;
7624 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7625 xrandr_eventbase = qep->first_event;
7627 cursor_font = xcb_generate_id(conn);
7628 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
7630 cursor = xcb_generate_id(conn);
7631 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
7632 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7635 /* map physical screens */
7636 for (i = 0; i < num_screens; i++) {
7637 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7639 TAILQ_INIT(&screens[i].rl);
7640 TAILQ_INIT(&screens[i].orl);
7641 screens[i].root = RootWindow(display, i);
7643 /* set default colors */
7644 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7645 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7646 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7647 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7648 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7650 /* create graphics context on screen */
7651 screens[i].bar_gc = xcb_generate_id(conn);
7653 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
7654 XCB_GC_GRAPHICS_EXPOSURES, gcv);
7656 /* set default cursor */
7657 xcb_change_window_attributes(conn, screens[i].root,
7660 /* init all workspaces */
7661 /* XXX these should be dynamically allocated too */
7662 for (j = 0; j < SWM_WS_MAX; j++) {
7663 ws = &screens[i].ws[j];
7669 TAILQ_INIT(&ws->winlist);
7670 TAILQ_INIT(&ws->unmanagedlist);
7672 for (k = 0; layouts[k].l_stack != NULL; k++)
7673 if (layouts[k].l_config != NULL)
7674 layouts[k].l_config(ws,
7675 SWM_ARG_ID_STACKINIT);
7676 ws->cur_layout = &layouts[0];
7677 ws->cur_layout->l_string(ws);
7683 xcb_randr_select_input(conn, screens[i].root,
7684 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7686 xcb_free_cursor(conn, cursor);
7687 xcb_close_font(conn, cursor_font);
7693 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7694 err(1, "setup_globals: strdup: failed to allocate memory.");
7696 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7697 err(1, "setup_globals: strdup: failed to allocate memory.");
7704 xcb_atom_t netwmcheck, netwmname, utf8_string;
7705 xcb_window_t root, win;
7706 xcb_screen_t *screen;
7709 /* work around sun jdk bugs, code from wmname */
7710 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7711 netwmname = get_atom_from_string("_NET_WM_NAME");
7712 utf8_string = get_atom_from_string("UTF8_STRING");
7714 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7715 for (i = 0; i < num_screens; i++) {
7716 root = screens[i].root;
7717 screen = get_screen(i);
7719 win = xcb_generate_id(conn);
7720 wa[0] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7721 wa[1] = screens[i].c[SWM_S_COLOR_UNFOCUS].color;
7722 xcb_create_window(conn, screen->root_depth, win, 0,
7723 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
7724 screen->root_visual,
7725 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
7727 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7728 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7729 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7730 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7731 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7732 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7737 main(int argc, char *argv[])
7739 struct swm_region *r, *rr;
7740 struct ws_win *winfocus = NULL;
7743 char conf[PATH_MAX], *cfile = NULL;
7746 int xfd, i, num_screens;
7748 struct sigaction sact;
7751 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7752 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7754 warnx("no locale support");
7756 if (!X_HAVE_UTF8_STRING)
7757 warnx("no UTF-8 support");
7759 if (!(display = XOpenDisplay(0)))
7760 errx(1, "can not open display");
7762 if (!(conn = XGetXCBConnection(display)))
7763 errx(1, "can not get XCB connection");
7766 errx(1, "other wm running");
7768 /* handle some signals */
7769 bzero(&sact, sizeof(sact));
7770 sigemptyset(&sact.sa_mask);
7772 sact.sa_handler = sighdlr;
7773 sigaction(SIGINT, &sact, NULL);
7774 sigaction(SIGQUIT, &sact, NULL);
7775 sigaction(SIGTERM, &sact, NULL);
7776 sigaction(SIGHUP, &sact, NULL);
7778 sact.sa_handler = sighdlr;
7779 sact.sa_flags = SA_NOCLDSTOP;
7780 sigaction(SIGCHLD, &sact, NULL);
7782 astate = get_atom_from_string("WM_STATE");
7783 aprot = get_atom_from_string("WM_PROTOCOLS");
7784 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7785 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7786 a_wmname = get_atom_from_string("WM_NAME");
7787 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7788 a_utf8_string = get_atom_from_string("UTF8_STRING");
7789 a_string = get_atom_from_string("STRING");
7790 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7792 /* look for local and global conf file */
7793 pwd = getpwuid(getuid());
7795 errx(1, "invalid user: %d", getuid());
7804 for (i = 0; ; i++) {
7809 snprintf(conf, sizeof conf, "%s/.%s",
7810 pwd->pw_dir, SWM_CONF_FILE);
7814 snprintf(conf, sizeof conf, "/etc/%s",
7819 snprintf(conf, sizeof conf, "%s/.%s",
7820 pwd->pw_dir, SWM_CONF_FILE_OLD);
7824 snprintf(conf, sizeof conf, "/etc/%s",
7831 if (strlen(conf) && stat(conf, &sb) != -1)
7832 if (S_ISREG(sb.st_mode)) {
7839 /* load conf (if any) */
7841 conf_load(cfile, SWM_CONF_DEFAULT);
7844 /* set some values to work around bad programs */
7846 /* grab existing windows (before we build the bars) */
7849 if (getenv("SWM_STARTED") == NULL)
7850 setenv("SWM_STARTED", "YES", 1);
7852 /* setup all bars */
7853 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7854 for (i = 0; i < num_screens; i++)
7855 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7856 if (winfocus == NULL)
7857 winfocus = TAILQ_FIRST(&r->ws->winlist);
7865 if (focus_mode == SWM_FOCUS_DEFAULT)
7866 drain_enter_notify();
7868 xfd = xcb_get_file_descriptor(conn);
7870 while (XPending(display)) {
7871 XNextEvent(display, &e);
7874 if (e.type < LASTEvent) {
7875 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7876 "window: 0x%lx, type: %s (%d), %d remaining"
7877 "\n", YESNO(handler[e.type]),
7878 e.xany.window, geteventname(&e),
7879 e.type, QLength(display));
7881 if (handler[e.type])
7882 handler[e.type](&e);
7884 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7885 "0x%lx, type: %s (%d)\n", e.xany.window,
7886 xrandr_geteventname(&e), e.type);
7888 switch (e.type - xrandr_eventbase) {
7889 case XCB_RANDR_SCREEN_CHANGE_NOTIFY:
7898 /* if we are being restarted go focus on first window */
7900 rr = winfocus->ws->r;
7902 /* not a visible window */
7906 /* move pointer to first screen if multi screen */
7907 if (num_screens > 1 || outputs > 1)
7908 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7909 rr->s[0].root, 0, 0, 0, 0, X(rr),
7910 Y(rr) + (bar_enabled ? bar_height : 0));
7912 a.id = SWM_ARG_ID_FOCUSCUR;
7922 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7924 DNPRINTF(SWM_D_MISC, "select failed");
7925 if (restart_wm == 1)
7926 restart(NULL, NULL);
7927 if (search_resp == 1)
7940 for (i = 0; i < num_screens; ++i)
7941 if (screens[i].bar_gc != 0)
7942 xcb_free_gc(conn, screens[i].bar_gc);
7943 XFreeFontSet(display, bar_fs);
7944 xcb_disconnect(conn);
7945 XCloseDisplay(display);