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>
94 #include <X11/Xproto.h>
95 #include <X11/Xutil.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/extensions/XTest.h>
105 #ifdef SPECTRWM_BUILDSTR
106 static const char *buildstr = SPECTRWM_BUILDSTR;
108 static const char *buildstr = SPECTRWM_VERSION;
112 # error XRandR versions less than 1.0 are not supported
117 #define SWM_XRR_HAS_CRTC
121 /*#define SWM_DEBUG*/
123 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
124 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
125 #define SWM_D_MISC 0x0001
126 #define SWM_D_EVENT 0x0002
127 #define SWM_D_WS 0x0004
128 #define SWM_D_FOCUS 0x0008
129 #define SWM_D_MOVE 0x0010
130 #define SWM_D_STACK 0x0020
131 #define SWM_D_MOUSE 0x0040
132 #define SWM_D_PROP 0x0080
133 #define SWM_D_CLASS 0x0100
134 #define SWM_D_KEY 0x0200
135 #define SWM_D_QUIRK 0x0400
136 #define SWM_D_SPAWN 0x0800
137 #define SWM_D_EVENTQ 0x1000
138 #define SWM_D_CONF 0x2000
139 #define SWM_D_BAR 0x4000
141 u_int32_t swm_debug = 0
159 #define DPRINTF(x...)
160 #define DNPRINTF(n,x...)
163 #define LENGTH(x) (sizeof x / sizeof x[0])
164 #define MODKEY Mod1Mask
165 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
166 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
167 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
168 #define SWM_PROPLEN (16)
169 #define SWM_FUNCNAME_LEN (32)
170 #define SWM_KEYS_LEN (255)
171 #define SWM_QUIRK_LEN (64)
172 #define X(r) (r)->g.x
173 #define Y(r) (r)->g.y
174 #define WIDTH(r) (r)->g.w
175 #define HEIGHT(r) (r)->g.h
176 #define BORDER(w) (w->bordered ? border_width : 0)
177 #define MAX_X(r) ((r)->g.x + (r)->g.w)
178 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
179 #define SH_MIN(w) (w)->sh_mask & PMinSize
180 #define SH_MIN_W(w) (w)->sh.min_width
181 #define SH_MIN_H(w) (w)->sh.min_height
182 #define SH_MAX(w) (w)->sh_mask & PMaxSize
183 #define SH_MAX_W(w) (w)->sh.max_width
184 #define SH_MAX_H(w) (w)->sh.max_height
185 #define SH_INC(w) (w)->sh_mask & PResizeInc
186 #define SH_INC_W(w) (w)->sh.width_inc
187 #define SH_INC_H(w) (w)->sh.height_inc
188 #define SWM_MAX_FONT_STEPS (3)
189 #define WINID(w) ((w) ? (w)->id : 0)
190 #define YESNO(x) ((x) ? "yes" : "no")
192 #define SWM_FOCUS_DEFAULT (0)
193 #define SWM_FOCUS_SYNERGY (1)
194 #define SWM_FOCUS_FOLLOW (2)
196 #define SWM_CONF_DEFAULT (0)
197 #define SWM_CONF_KEYMAPPING (1)
200 #define SWM_LIB "/usr/local/lib/libswmhack.so"
213 volatile sig_atomic_t running = 1;
214 volatile sig_atomic_t restart_wm = 0;
216 int last_focus_event = FocusOut;
217 int (*xerrorxlib)(Display *, XErrorEvent *);
221 int xrandr_eventbase;
222 unsigned int numlockmask = 0;
226 int cycle_visible = 0;
228 int font_adjusted = 0;
229 unsigned int mod_key = MODKEY;
232 struct swm_region *search_r;
233 int select_list_pipe[2];
234 int select_resp_pipe[2];
236 volatile sig_atomic_t search_resp;
237 int search_resp_action;
239 struct search_window {
240 TAILQ_ENTRY(search_window) entry;
246 TAILQ_HEAD(search_winlist, search_window);
247 struct search_winlist search_wl;
252 SWM_SEARCH_UNICONIFY,
253 SWM_SEARCH_NAME_WORKSPACE,
254 SWM_SEARCH_SEARCH_WORKSPACE,
255 SWM_SEARCH_SEARCH_WINDOW
258 #define SWM_STACK_TOP (0)
259 #define SWM_STACK_BOTTOM (1)
260 #define SWM_STACK_ABOVE (2)
261 #define SWM_STACK_BELOW (3)
264 double dialog_ratio = 0.6;
266 #define SWM_BAR_MAX (256)
267 #define SWM_BAR_JUSTIFY_LEFT (0)
268 #define SWM_BAR_JUSTIFY_CENTER (1)
269 #define SWM_BAR_JUSTIFY_RIGHT (2)
270 #define SWM_BAR_OFFSET (4)
271 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
272 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
273 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
274 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
275 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
277 #ifdef X_HAVE_UTF8_STRING
278 #define DRAWSTRING(x...) Xutf8DrawString(x)
280 #define DRAWSTRING(x...) XmbDrawString(x)
283 char *bar_argv[] = { NULL, NULL };
285 unsigned char bar_ext[SWM_BAR_MAX];
286 char bar_vertext[SWM_BAR_MAX];
288 sig_atomic_t bar_alarm = 0;
291 int bar_border_width = 1;
292 int bar_at_bottom = 0;
294 int bar_extra_running = 0;
297 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
298 char *bar_format = NULL;
299 int stack_enabled = 1;
300 int clock_enabled = 1;
301 int urgent_enabled = 0;
302 char *clock_format = NULL;
303 int title_name_enabled = 0;
304 int title_class_enabled = 0;
305 int window_name_enabled = 0;
306 int focus_mode = SWM_FOCUS_DEFAULT;
307 int focus_close = SWM_STACK_BELOW;
308 int focus_close_wrap = 1;
309 int focus_default = SWM_STACK_TOP;
310 int spawn_position = SWM_STACK_TOP;
311 int disable_border = 0;
312 int border_width = 1;
313 int verbose_layout = 0;
316 XFontSetExtents *bar_fs_extents;
320 #define SWM_MENU_FN (2)
321 #define SWM_MENU_NB (4)
322 #define SWM_MENU_NF (6)
323 #define SWM_MENU_SB (8)
324 #define SWM_MENU_SF (10)
326 /* layout manager data */
327 struct swm_geometry {
340 struct swm_geometry g;
343 /* virtual "screens" */
345 TAILQ_ENTRY(swm_region) entry;
346 struct swm_geometry g;
347 struct workspace *ws; /* current workspace on this region */
348 struct workspace *ws_prior; /* prior workspace on this region */
349 struct swm_screen *s; /* screen idx */
352 TAILQ_HEAD(swm_region_list, swm_region);
355 TAILQ_ENTRY(ws_win) entry;
358 struct ws_win *child_trans; /* transient child window */
359 struct swm_geometry g; /* current geometry */
360 struct swm_geometry g_float; /* region coordinates */
361 int g_floatvalid; /* g_float geometry validity */
362 int floatmaxed; /* whether maxed by max_stack */
367 unsigned int ewmh_flags;
368 int font_size_boundary[SWM_MAX_FONT_STEPS];
374 unsigned long quirks;
375 struct workspace *ws; /* always valid */
376 struct swm_screen *s; /* always valid, never changes */
377 XWindowAttributes wa;
383 TAILQ_HEAD(ws_win_list, ws_win);
387 TAILQ_ENTRY(pid_e) entry;
391 TAILQ_HEAD(pid_list, pid_e);
392 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
394 /* layout handlers */
396 void vertical_config(struct workspace *, int);
397 void vertical_stack(struct workspace *, struct swm_geometry *);
398 void horizontal_config(struct workspace *, int);
399 void horizontal_stack(struct workspace *, struct swm_geometry *);
400 void max_stack(struct workspace *, struct swm_geometry *);
401 void plain_stacker(struct workspace *);
402 void fancy_stacker(struct workspace *);
404 struct ws_win *find_window(Window);
406 void grabbuttons(struct ws_win *, int);
407 void new_region(struct swm_screen *, int, int, int, int);
408 void unmanage_window(struct ws_win *);
409 long getstate(Window);
411 int conf_load(char *, int);
414 void (*l_stack)(struct workspace *, struct swm_geometry *);
415 void (*l_config)(struct workspace *, int);
417 #define SWM_L_FOCUSPREV (1<<0)
418 #define SWM_L_MAPONFOCUS (1<<1)
419 void (*l_string)(struct workspace *);
421 /* stack, configure */
422 { vertical_stack, vertical_config, 0, plain_stacker },
423 { horizontal_stack, horizontal_config, 0, plain_stacker },
425 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
426 { NULL, NULL, 0, NULL },
429 /* position of max_stack mode in the layouts array, index into layouts! */
430 #define SWM_V_STACK (0)
431 #define SWM_H_STACK (1)
432 #define SWM_MAX_STACK (2)
434 #define SWM_H_SLICE (32)
435 #define SWM_V_SLICE (32)
437 /* define work spaces */
439 int idx; /* workspace index */
440 char *name; /* workspace name */
441 int always_raise; /* raise windows on focus */
442 struct layout *cur_layout; /* current layout handlers */
443 struct ws_win *focus; /* may be NULL */
444 struct ws_win *focus_prev; /* may be NULL */
445 struct swm_region *r; /* may be NULL */
446 struct swm_region *old_r; /* may be NULL */
447 struct ws_win_list winlist; /* list of windows in ws */
448 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
449 char stacker[10]; /* display stacker and layout */
453 int horizontal_msize;
455 int horizontal_stacks;
466 SWM_S_COLOR_BAR_BORDER,
467 SWM_S_COLOR_BAR_FONT,
473 /* physical screen mapping */
474 #define SWM_WS_MAX (22) /* hard limit */
475 int workspace_limit = 10; /* soft limit */
478 int idx; /* screen index */
479 struct swm_region_list rl; /* list of regions on this screen */
480 struct swm_region_list orl; /* list of old regions */
482 struct workspace ws[SWM_WS_MAX];
488 } c[SWM_S_COLOR_MAX];
492 struct swm_screen *screens;
494 /* args to functions */
497 #define SWM_ARG_ID_FOCUSNEXT (0)
498 #define SWM_ARG_ID_FOCUSPREV (1)
499 #define SWM_ARG_ID_FOCUSMAIN (2)
500 #define SWM_ARG_ID_FOCUSCUR (4)
501 #define SWM_ARG_ID_SWAPNEXT (10)
502 #define SWM_ARG_ID_SWAPPREV (11)
503 #define SWM_ARG_ID_SWAPMAIN (12)
504 #define SWM_ARG_ID_MOVELAST (13)
505 #define SWM_ARG_ID_MASTERSHRINK (20)
506 #define SWM_ARG_ID_MASTERGROW (21)
507 #define SWM_ARG_ID_MASTERADD (22)
508 #define SWM_ARG_ID_MASTERDEL (23)
509 #define SWM_ARG_ID_FLIPLAYOUT (24)
510 #define SWM_ARG_ID_STACKRESET (30)
511 #define SWM_ARG_ID_STACKINIT (31)
512 #define SWM_ARG_ID_CYCLEWS_UP (40)
513 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
514 #define SWM_ARG_ID_CYCLESC_UP (42)
515 #define SWM_ARG_ID_CYCLESC_DOWN (43)
516 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
517 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
518 #define SWM_ARG_ID_STACKINC (50)
519 #define SWM_ARG_ID_STACKDEC (51)
520 #define SWM_ARG_ID_SS_ALL (60)
521 #define SWM_ARG_ID_SS_WINDOW (61)
522 #define SWM_ARG_ID_DONTCENTER (70)
523 #define SWM_ARG_ID_CENTER (71)
524 #define SWM_ARG_ID_KILLWINDOW (80)
525 #define SWM_ARG_ID_DELETEWINDOW (81)
526 #define SWM_ARG_ID_WIDTHGROW (90)
527 #define SWM_ARG_ID_WIDTHSHRINK (91)
528 #define SWM_ARG_ID_HEIGHTGROW (92)
529 #define SWM_ARG_ID_HEIGHTSHRINK (93)
530 #define SWM_ARG_ID_MOVEUP (100)
531 #define SWM_ARG_ID_MOVEDOWN (101)
532 #define SWM_ARG_ID_MOVELEFT (102)
533 #define SWM_ARG_ID_MOVERIGHT (103)
537 void focus(struct swm_region *, union arg *);
538 void focus_magic(struct ws_win *);
542 TAILQ_ENTRY(quirk) entry;
546 #define SWM_Q_FLOAT (1<<0) /* float this window */
547 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
548 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
549 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
550 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
551 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
553 TAILQ_HEAD(quirk_list, quirk);
554 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
557 * Supported EWMH hints should be added to
558 * both the enum and the ewmh array
563 _NET_MOVERESIZE_WINDOW,
564 _NET_WM_ACTION_CLOSE,
565 _NET_WM_ACTION_FULLSCREEN,
567 _NET_WM_ACTION_RESIZE,
568 _NET_WM_ALLOWED_ACTIONS,
571 _NET_WM_STATE_FULLSCREEN,
572 _NET_WM_STATE_HIDDEN,
573 _NET_WM_STATE_MAXIMIZED_HORZ,
574 _NET_WM_STATE_MAXIMIZED_VERT,
575 _NET_WM_STATE_SKIP_PAGER,
576 _NET_WM_STATE_SKIP_TASKBAR,
578 _NET_WM_WINDOW_TYPE_DIALOG,
579 _NET_WM_WINDOW_TYPE_DOCK,
580 _NET_WM_WINDOW_TYPE_NORMAL,
581 _NET_WM_WINDOW_TYPE_SPLASH,
582 _NET_WM_WINDOW_TYPE_TOOLBAR,
583 _NET_WM_WINDOW_TYPE_UTILITY,
584 _SWM_WM_STATE_MANUAL,
591 } ewmh[SWM_EWMH_HINT_MAX] = {
592 /* must be in same order as in the enum */
593 {"_NET_ACTIVE_WINDOW", None},
594 {"_NET_CLOSE_WINDOW", None},
595 {"_NET_MOVERESIZE_WINDOW", None},
596 {"_NET_WM_ACTION_CLOSE", None},
597 {"_NET_WM_ACTION_FULLSCREEN", None},
598 {"_NET_WM_ACTION_MOVE", None},
599 {"_NET_WM_ACTION_RESIZE", None},
600 {"_NET_WM_ALLOWED_ACTIONS", None},
601 {"_NET_WM_STATE", None},
602 {"_NET_WM_STATE_ABOVE", None},
603 {"_NET_WM_STATE_FULLSCREEN", None},
604 {"_NET_WM_STATE_HIDDEN", None},
605 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
606 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
607 {"_NET_WM_STATE_SKIP_PAGER", None},
608 {"_NET_WM_STATE_SKIP_TASKBAR", None},
609 {"_NET_WM_WINDOW_TYPE", None},
610 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
611 {"_NET_WM_WINDOW_TYPE_DOCK", None},
612 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
613 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
614 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
615 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
616 {"_SWM_WM_STATE_MANUAL", None},
619 void store_float_geom(struct ws_win *, struct swm_region *);
620 int floating_toggle_win(struct ws_win *);
621 void constrain_window(struct ws_win *, struct swm_region *, int);
622 void update_window(struct ws_win *);
623 void spawn_select(struct swm_region *, union arg *, char *, int *);
624 unsigned char *get_win_name(Window);
627 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
628 unsigned long *nbytes, unsigned char **data)
631 unsigned long *nbytes_ret, *nitems_ret;
632 unsigned long nbytes_tmp, nitems_tmp;
635 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
636 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
638 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
639 &real, &format, nitems_ret, nbytes_ret, data);
641 if (status != Success)
650 update_iconic(struct ws_win *win, int newv)
657 iprop = XInternAtom(display, "_SWM_ICONIC", False);
661 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
662 PropModeReplace, (unsigned char *)&v, 1);
664 XDeleteProperty(display, win->id, iprop);
668 get_iconic(struct ws_win *win)
673 unsigned long nitems, extra;
674 unsigned char *prop = NULL;
676 iprop = XInternAtom(display, "_SWM_ICONIC", False);
679 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
680 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
681 if (status != Success)
683 if (rettype != XA_INTEGER || retfmt != 32)
687 v = *((int32_t *)prop);
701 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
703 for (i = 0; i < LENGTH(ewmh); i++)
704 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
706 for (i = 0; i < ScreenCount(display); i++) {
707 /* Support check window will be created by workaround(). */
709 /* Report supported atoms */
710 XDeleteProperty(display, screens[i].root, sup_list);
711 for (j = 0; j < LENGTH(ewmh); j++)
712 XChangeProperty(display, screens[i].root,
713 sup_list, XA_ATOM, 32,
714 PropModeAppend, (unsigned char *)&ewmh[j].atom, 1);
722 unsigned char *data = NULL;
724 Atom sup_check, sup_list;
727 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
728 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
730 for (i = 0; i < ScreenCount(display); i++) {
731 /* Get the support check window and destroy it */
732 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
737 XDestroyWindow(display, id);
738 XDeleteProperty(display, screens[i].root, sup_check);
739 XDeleteProperty(display, screens[i].root, sup_list);
747 ewmh_autoquirk(struct ws_win *win)
750 unsigned long *data = NULL, n;
753 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
754 XA_ATOM, &n, NULL, (void *)&data);
761 for (i = 0; i < n; i++) {
763 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
765 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
766 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
767 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
769 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
772 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
773 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
775 win->quirks = SWM_Q_FLOAT;
783 #define SWM_EWMH_ACTION_COUNT_MAX (6)
784 #define EWMH_F_FULLSCREEN (1<<0)
785 #define EWMH_F_ABOVE (1<<1)
786 #define EWMH_F_HIDDEN (1<<2)
787 #define EWMH_F_SKIP_PAGER (1<<3)
788 #define EWMH_F_SKIP_TASKBAR (1<<4)
789 #define SWM_F_MANUAL (1<<5)
792 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
800 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
801 "fullscreen %s\n", win->id, YESNO(fs));
804 if (!win->g_floatvalid)
805 store_float_geom(win, win->ws->r);
807 win->g = win->ws->r->g;
810 if (win->g_floatvalid) {
811 /* refloat at last floating relative position */
812 win->g = win->g_float;
813 X(win) += X(win->ws->r);
814 Y(win) += Y(win->ws->r);
822 ewmh_update_actions(struct ws_win *win)
824 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
830 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
833 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
834 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
837 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
838 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
841 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
842 #define _NET_WM_STATE_ADD 1 /* add/set property */
843 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
846 ewmh_update_win_state(struct ws_win *win, long state, long action)
848 unsigned int mask = 0;
849 unsigned int changed = 0;
850 unsigned int orig_flags;
855 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
856 mask = EWMH_F_FULLSCREEN;
857 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
859 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
861 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
862 mask = EWMH_F_SKIP_PAGER;
863 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
864 mask = EWMH_F_SKIP_TASKBAR;
867 orig_flags = win->ewmh_flags;
870 case _NET_WM_STATE_REMOVE:
871 win->ewmh_flags &= ~mask;
873 case _NET_WM_STATE_ADD:
874 win->ewmh_flags |= mask;
876 case _NET_WM_STATE_TOGGLE:
877 win->ewmh_flags ^= mask;
881 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
883 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
885 if (!floating_toggle_win(win))
886 win->ewmh_flags = orig_flags; /* revert */
887 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
889 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
890 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
892 if (!ewmh_set_win_fullscreen(win,
893 win->ewmh_flags & EWMH_F_FULLSCREEN))
894 win->ewmh_flags = orig_flags; /* revert */
896 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
898 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
899 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
900 XA_ATOM, 32, PropModeAppend,
901 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
902 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
903 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
904 XA_ATOM, 32, PropModeAppend,
905 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
906 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
907 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
908 XA_ATOM, 32, PropModeAppend,
909 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
910 if (win->ewmh_flags & EWMH_F_ABOVE)
911 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
912 XA_ATOM, 32, PropModeAppend,
913 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
914 if (win->ewmh_flags & SWM_F_MANUAL)
915 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
916 XA_ATOM, 32, PropModeAppend,
917 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
921 ewmh_get_win_state(struct ws_win *win)
932 win->ewmh_flags |= EWMH_F_ABOVE;
934 win->ewmh_flags |= SWM_F_MANUAL;
936 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
937 (~0L), XA_ATOM, &n, NULL, (void *)&states);
942 for (i = 0; i < n; i++)
943 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
951 geteventname(XEvent *e)
963 name = "ButtonPress";
966 name = "ButtonRelease";
969 name = "MotionNotify";
972 name = "EnterNotify";
975 name = "LeaveNotify";
984 name = "KeymapNotify";
990 name = "GraphicsExpose";
995 case VisibilityNotify:
996 name = "VisibilityNotify";
999 name = "CreateNotify";
1002 name = "DestroyNotify";
1005 name = "UnmapNotify";
1011 name = "MapRequest";
1013 case ReparentNotify:
1014 name = "ReparentNotify";
1016 case ConfigureNotify:
1017 name = "ConfigureNotify";
1019 case ConfigureRequest:
1020 name = "ConfigureRequest";
1023 name = "GravityNotify";
1026 name = "ResizeRequest";
1028 case CirculateNotify:
1029 name = "CirculateNotify";
1031 case CirculateRequest:
1032 name = "CirculateRequest";
1034 case PropertyNotify:
1035 name = "PropertyNotify";
1037 case SelectionClear:
1038 name = "SelectionClear";
1040 case SelectionRequest:
1041 name = "SelectionRequest";
1043 case SelectionNotify:
1044 name = "SelectionNotify";
1046 case ColormapNotify:
1047 name = "ColormapNotify";
1050 name = "ClientMessage";
1053 name = "MappingNotify";
1063 xrandr_geteventname(XEvent *e)
1067 switch(e->type - xrandr_eventbase) {
1068 case RRScreenChangeNotify:
1069 name = "RRScreenChangeNotify";
1079 dumpwins(struct swm_region *r, union arg *args)
1083 XWindowAttributes wa;
1085 if (r->ws == NULL) {
1086 warnx("dumpwins: invalid workspace");
1090 warnx("=== managed window list ws %02d ===", r->ws->idx);
1092 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1093 state = getstate(win->id);
1094 if (!XGetWindowAttributes(display, win->id, &wa))
1095 warnx("window: 0x%lx, failed XGetWindowAttributes",
1097 warnx("window: 0x%lx, map_state: %d, state: %d, "
1098 "transient: 0x%lx", win->id, wa.map_state, state,
1102 warnx("===== unmanaged window list =====");
1103 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1104 state = getstate(win->id);
1105 if (!XGetWindowAttributes(display, win->id, &wa))
1106 warnx("window: 0x%lx, failed XGetWindowAttributes",
1108 warnx("window: 0x%lx, map_state: %d, state: %d, "
1109 "transient: 0x%lx", win->id, wa.map_state, state,
1113 warnx("=================================");
1117 dumpwins(struct swm_region *r, union arg *args)
1120 #endif /* SWM_DEBUG */
1122 void expose(XEvent *);
1123 void keypress(XEvent *);
1124 void buttonpress(XEvent *);
1125 void configurerequest(XEvent *);
1126 void configurenotify(XEvent *);
1127 void destroynotify(XEvent *);
1128 void enternotify(XEvent *);
1129 void focusevent(XEvent *);
1130 void mapnotify(XEvent *);
1131 void mappingnotify(XEvent *);
1132 void maprequest(XEvent *);
1133 void propertynotify(XEvent *);
1134 void unmapnotify(XEvent *);
1135 void visibilitynotify(XEvent *);
1136 void clientmessage(XEvent *);
1138 void (*handler[LASTEvent])(XEvent *) = {
1140 [KeyPress] = keypress,
1141 [ButtonPress] = buttonpress,
1142 [ConfigureRequest] = configurerequest,
1143 [ConfigureNotify] = configurenotify,
1144 [DestroyNotify] = destroynotify,
1145 [EnterNotify] = enternotify,
1146 [FocusIn] = focusevent,
1147 [FocusOut] = focusevent,
1148 [MapNotify] = mapnotify,
1149 [MappingNotify] = mappingnotify,
1150 [MapRequest] = maprequest,
1151 [PropertyNotify] = propertynotify,
1152 [UnmapNotify] = unmapnotify,
1153 [VisibilityNotify] = visibilitynotify,
1154 [ClientMessage] = clientmessage,
1160 int saved_errno, status;
1163 saved_errno = errno;
1167 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1172 if (errno != ECHILD)
1173 warn("sighdlr: waitpid");
1174 #endif /* SWM_DEBUG */
1177 if (pid == searchpid)
1181 if (WIFEXITED(status)) {
1182 if (WEXITSTATUS(status) != 0)
1183 warnx("sighdlr: child exit status: %d",
1184 WEXITSTATUS(status));
1186 warnx("sighdlr: child is terminated "
1188 #endif /* SWM_DEBUG */
1202 errno = saved_errno;
1208 struct pid_e *p = NULL;
1210 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1215 TAILQ_FOREACH(p, &pidlist, entry) {
1224 name_to_color(char *colorname)
1228 XColor screen_def, exact_def;
1229 unsigned long result = 0;
1230 char cname[32] = "#";
1232 cmap = DefaultColormap(display, screens[0].idx);
1233 status = XAllocNamedColor(display, cmap, colorname,
1234 &screen_def, &exact_def);
1236 strlcat(cname, colorname + 2, sizeof cname - 1);
1237 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1241 result = screen_def.pixel;
1243 warnx("color '%s' not found", colorname);
1249 setscreencolor(char *val, int i, int c)
1251 if (i > 0 && i <= ScreenCount(display)) {
1252 screens[i - 1].c[c].color = name_to_color(val);
1253 free(screens[i - 1].c[c].name);
1254 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1256 } else if (i == -1) {
1257 for (i = 0; i < ScreenCount(display); i++) {
1258 screens[i].c[c].color = name_to_color(val);
1259 free(screens[i].c[c].name);
1260 if ((screens[i].c[c].name = strdup(val)) == NULL)
1264 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1265 i, ScreenCount(display));
1269 fancy_stacker(struct workspace *ws)
1271 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1272 if (ws->cur_layout->l_stack == vertical_stack)
1273 snprintf(ws->stacker, sizeof ws->stacker,
1274 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1275 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1276 if (ws->cur_layout->l_stack == horizontal_stack)
1277 snprintf(ws->stacker, sizeof ws->stacker,
1278 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1279 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1283 plain_stacker(struct workspace *ws)
1285 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1286 if (ws->cur_layout->l_stack == vertical_stack)
1287 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1288 sizeof ws->stacker);
1289 if (ws->cur_layout->l_stack == horizontal_stack)
1290 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1291 sizeof ws->stacker);
1295 custom_region(char *val)
1297 unsigned int sidx, x, y, w, h;
1299 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1300 errx(1, "invalid custom region, "
1301 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1302 if (sidx < 1 || sidx > ScreenCount(display))
1303 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1304 sidx, ScreenCount(display));
1308 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1310 if (x > DisplayWidth(display, sidx) ||
1311 y > DisplayHeight(display, sidx) ||
1312 w + x > DisplayWidth(display, sidx) ||
1313 h + y > DisplayHeight(display, sidx)) {
1314 warnx("ignoring region %ux%u+%u+%u - not within screen "
1315 "boundaries (%ux%u)", w, h, x, y,
1316 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1320 new_region(&screens[sidx], x, y, w, h);
1324 socket_setnonblock(int fd)
1328 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1329 err(1, "fcntl F_GETFL");
1330 flags |= O_NONBLOCK;
1331 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1332 err(1, "fcntl F_SETFL");
1336 bar_print(struct swm_region *r, const char *s)
1340 XRectangle ibox, lbox;
1343 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1345 switch (bar_justify) {
1346 case SWM_BAR_JUSTIFY_LEFT:
1349 case SWM_BAR_JUSTIFY_CENTER:
1350 x = (WIDTH(r) - lbox.width) / 2;
1352 case SWM_BAR_JUSTIFY_RIGHT:
1353 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1357 if (x < SWM_BAR_OFFSET)
1360 /* clear back buffer */
1361 XSetForeground(display, r->s->bar_gc, r->s->c[SWM_S_COLOR_BAR].color);
1362 XFillRectangle(display, r->bar->buffer, r->s->bar_gc, 0, 0,
1363 WIDTH(r->bar), HEIGHT(r->bar));
1365 /* draw back buffer */
1366 XSetForeground(display, r->s->bar_gc,
1367 r->s->c[SWM_S_COLOR_BAR_FONT].color);
1368 DRAWSTRING(display, r->bar->buffer, bar_fs, r->s->bar_gc,
1369 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1373 XCopyArea(display, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1374 WIDTH(r->bar), HEIGHT(r->bar), 0, 0);
1378 bar_extra_stop(void)
1382 bzero(bar_pipe, sizeof bar_pipe);
1385 kill(bar_pid, SIGTERM);
1388 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1393 bar_class_name(char *s, size_t sz, struct swm_region *r)
1395 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1397 if (r->ws->focus->ch.res_class != NULL)
1398 strlcat(s, r->ws->focus->ch.res_class, sz);
1402 bar_title_name(char *s, size_t sz, struct swm_region *r)
1404 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1406 if (r->ws->focus->ch.res_name != NULL)
1407 strlcat(s, r->ws->focus->ch.res_name, sz);
1411 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1413 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1416 bar_class_name(s, sz, r);
1417 strlcat(s, ":", sz);
1418 bar_title_name(s, sz, r);
1422 bar_window_float(char *s, size_t sz, struct swm_region *r)
1424 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1426 if (r->ws->focus->floating)
1427 strlcat(s, "(f)", sz);
1431 bar_window_name(char *s, size_t sz, struct swm_region *r)
1433 unsigned char *title;
1435 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1437 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1440 strlcat(s, (char *)title, sz);
1444 int urgent[SWM_WS_MAX];
1446 bar_urgent(char *s, size_t sz)
1448 XWMHints *wmh = NULL;
1453 for (i = 0; i < workspace_limit; i++)
1456 for (i = 0; i < ScreenCount(display); i++)
1457 for (j = 0; j < workspace_limit; j++)
1458 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1459 wmh = XGetWMHints(display, win->id);
1463 if (wmh->flags & XUrgencyHint)
1468 for (i = 0; i < workspace_limit; i++) {
1470 snprintf(b, sizeof b, "%d ", i + 1);
1472 snprintf(b, sizeof b, "- ");
1478 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1480 if (r == NULL || r->ws == NULL)
1482 if (r->ws->name != NULL)
1483 strlcat(s, r->ws->name, sz);
1486 /* build the default bar format according to the defined enabled options */
1488 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1490 /* if format provided, just copy the buffers */
1491 if (bar_format != NULL) {
1492 strlcpy(fmtnew, fmtexp, sz);
1496 /* reset the output buffer */
1499 strlcat(fmtnew, "+N:+I ", sz);
1501 strlcat(fmtnew, "+S", sz);
1502 strlcat(fmtnew, " ", sz);
1504 /* only show the workspace name if there's actually one */
1505 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1506 strlcat(fmtnew, "<+D>", sz);
1507 strlcat(fmtnew, "+3<", sz);
1509 if (clock_enabled) {
1510 strlcat(fmtnew, fmtexp, sz);
1511 strlcat(fmtnew, "+4<", sz);
1514 /* bar_urgent already adds the space before the last asterisk */
1516 strlcat(fmtnew, "* +U*+4<", sz);
1518 if (title_class_enabled) {
1519 strlcat(fmtnew, "+C", sz);
1520 if (title_name_enabled == 0)
1521 strlcat(fmtnew, "+4<", sz);
1524 /* checks needed by the colon and floating strlcat(3) calls below */
1525 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1526 if (title_name_enabled) {
1527 if (title_class_enabled)
1528 strlcat(fmtnew, ":", sz);
1529 strlcat(fmtnew, "+T+4<", sz);
1531 if (window_name_enabled) {
1532 if (r->ws->focus->floating)
1533 strlcat(fmtnew, "+F ", sz);
1534 strlcat(fmtnew, "+64W ", sz);
1538 /* finally add the action script output and the version */
1539 strlcat(fmtnew, "+4<+A+4<+V", sz);
1542 /* replaces the bar format character sequences (like in tmux(1)) */
1544 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1548 char num[8], tmp[SWM_BAR_MAX];
1550 size_t len, numoff = 0;
1552 /* reset strlcat(3) buffer */
1555 /* get number, if any */
1557 while (*fmt != '\0' && isdigit((unsigned char) *fmt)) {
1558 if (numoff >= sizeof num - 1)
1560 num[numoff++] = *fmt++;
1564 if ((limit = strtonum(num, 1, sizeof tmp - 1, NULL)) == 0)
1565 limit = sizeof tmp - 1;
1567 /* if number is too big, skip to the first non-digit */
1568 if (numoff >= sizeof num - 1) {
1569 while (*fmt != '\0' && isdigit((unsigned char) *fmt))
1572 /* there is nothing to replace (ie EOL) */
1578 /* special case; no limit given, pad one space, instead */
1579 if (limit == sizeof tmp - 1)
1581 snprintf(tmp, sizeof tmp, "%*s", limit, " ");
1584 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1587 bar_class_name(tmp, sizeof tmp, r);
1590 bar_workspace_name(tmp, sizeof tmp, r);
1593 bar_window_float(tmp, sizeof tmp, r);
1596 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1599 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1602 bar_class_title_name(tmp, sizeof tmp, r);
1605 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1608 bar_title_name(tmp, sizeof tmp, r);
1611 bar_urgent(tmp, sizeof tmp);
1614 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1617 bar_window_name(tmp, sizeof tmp, r);
1620 /* unknown character sequence; copy as-is */
1621 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1629 while (limit-- > 0) {
1630 if (*offrep >= sz - 1)
1632 fmtrep[(*offrep)++] = *ptr++;
1640 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1645 while (*fmt != '\0') {
1647 /* skip ordinary characters */
1650 fmtrep[off++] = *fmt++;
1654 /* character sequence found; replace it */
1655 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1664 bar_fmt_expand(char *fmtexp, size_t sz)
1671 /* start by grabbing the current time and date */
1673 localtime_r(&tmt, &tm);
1675 /* figure out what to expand */
1676 if (bar_format != NULL)
1678 else if (bar_format == NULL && clock_enabled)
1680 /* if nothing to expand bail out */
1686 /* copy as-is, just in case the format shouldn't be expanded below */
1687 strlcpy(fmtexp, fmt, sz);
1688 /* finally pass the string through strftime(3) */
1689 #ifndef SWM_DENY_CLOCK_FORMAT
1690 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1691 warnx("format too long");
1699 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1700 char fmtrep[SWM_BAR_MAX];
1702 struct swm_region *r;
1704 /* expand the format by first passing it through strftime(3) */
1705 bar_fmt_expand(fmtexp, sizeof fmtexp);
1707 for (i = 0; i < ScreenCount(display); i++) {
1708 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1711 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1712 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1713 bar_print(r, fmtrep);
1724 if (bar_enabled == 0)
1726 if (bar_extra && bar_extra_running) {
1727 /* ignore short reads; it'll correct itself */
1728 while ((b = fgetln(stdin, &len)) != NULL)
1729 if (b && b[len - 1] == '\n') {
1731 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1733 if (b == NULL && errno != EAGAIN) {
1734 warn("bar_update: bar_extra failed");
1738 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1751 bar_toggle(struct swm_region *r, union arg *args)
1753 struct swm_region *tmpr;
1754 int i, sc = ScreenCount(display);
1756 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1759 for (i = 0; i < sc; i++)
1760 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1762 XUnmapWindow(display, tmpr->bar->id);
1764 for (i = 0; i < sc; i++)
1765 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1767 XMapRaised(display, tmpr->bar->id);
1770 bar_enabled = !bar_enabled;
1773 /* must be after stack */
1780 XSetWindowAttributes wa;
1781 struct swm_region *r;
1784 /* do this here because the conf file is in memory */
1785 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1786 /* launch external status app */
1787 bar_extra_running = 1;
1788 if (pipe(bar_pipe) == -1)
1789 err(1, "pipe error");
1790 socket_setnonblock(bar_pipe[0]);
1791 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1792 if (dup2(bar_pipe[0], 0) == -1)
1794 if (dup2(bar_pipe[1], 1) == -1)
1796 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1797 err(1, "could not disable SIGPIPE");
1798 switch (bar_pid = fork()) {
1800 err(1, "cannot fork");
1804 execvp(bar_argv[0], bar_argv);
1805 err(1, "%s external app failed", bar_argv[0]);
1807 default: /* parent */
1813 bzero(&wa, sizeof wa);
1814 for (i = 0; i < ScreenCount(display); i++)
1815 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1819 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1820 wa.background_pixel =
1821 screens[i].c[SWM_S_COLOR_BAR].color;
1822 XChangeWindowAttributes(display, r->bar->id,
1823 CWBackPixel | CWBorderPixel, &wa);
1829 bar_setup(struct swm_region *r)
1831 char *default_string;
1832 char **missing_charsets;
1833 int num_missing_charsets = 0;
1837 XFreeFontSet(display, bar_fs);
1841 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1842 err(1, "bar_setup: calloc: failed to allocate memory.");
1844 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1846 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1847 &num_missing_charsets, &default_string);
1849 if (num_missing_charsets > 0) {
1850 warnx("Unable to load charset(s):");
1852 for (i = 0; i < num_missing_charsets; ++i)
1853 warnx("%s", missing_charsets[i]);
1855 XFreeStringList(missing_charsets);
1857 if (strcmp(default_string, ""))
1858 warnx("Glyphs from those sets will be replaced "
1859 "by '%s'.", default_string);
1861 warnx("Glyphs from those sets won't be drawn.");
1865 errx(1, "Error creating font set structure.");
1867 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1869 bar_height = bar_fs_extents->max_logical_extent.height +
1870 2 * bar_border_width;
1876 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1877 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1878 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1880 r->bar->id = XCreateSimpleWindow(display,
1881 r->s->root, X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1882 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1883 r->s->c[SWM_S_COLOR_BAR].color);
1885 r->bar->buffer = XCreatePixmap(display, r->bar->id, WIDTH(r->bar),
1886 HEIGHT(r->bar), DefaultDepth(display, r->s->idx));
1888 XSelectInput(display, r->bar->id, VisibilityChangeMask);
1891 XMapRaised(display, r->bar->id);
1893 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%lx, (x,y) w x h: (%d,%d) "
1894 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1897 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1898 err(1, "could not install bar_signal");
1903 bar_cleanup(struct swm_region *r)
1907 XDestroyWindow(display, r->bar->id);
1908 XFreePixmap(display, r->bar->buffer);
1914 drain_enter_notify(void)
1919 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1922 DNPRINTF(SWM_D_EVENT, "drain_enter_notify: drained: %d\n", i);
1926 set_win_state(struct ws_win *win, long state)
1928 long data[] = {state, None};
1930 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1935 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1936 (unsigned char *)data, 2);
1943 unsigned char *p = NULL;
1946 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1949 result = *((long *)p);
1955 version(struct swm_region *r, union arg *args)
1957 bar_version = !bar_version;
1959 snprintf(bar_vertext, sizeof bar_vertext,
1960 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1962 strlcpy(bar_vertext, "", sizeof bar_vertext);
1967 client_msg(struct ws_win *win, Atom a)
1969 XClientMessageEvent cm;
1974 bzero(&cm, sizeof cm);
1975 cm.type = ClientMessage;
1976 cm.window = win->id;
1977 cm.message_type = aprot;
1980 cm.data.l[1] = CurrentTime;
1981 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1984 /* synthetic response to a ConfigureRequest when not making a change */
1986 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1993 /* send notification of unchanged state. */
1994 ce.type = ConfigureNotify;
1997 ce.width = WIDTH(win);
1998 ce.height = HEIGHT(win);
1999 ce.override_redirect = False;
2003 ce.display = display;
2005 ce.window = win->id;
2006 ce.border_width = BORDER(win);
2010 ce.display = ev->display;
2011 ce.event = ev->window;
2012 ce.window = ev->window;
2014 /* make response appear more WM_SIZE_HINTS-compliant */
2016 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
2017 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2018 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
2019 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2020 SH_INC_W(win), SH_INC_H(win));
2024 /* the hint may be set... to 0! */
2025 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2026 ce.width = SH_MIN_W(win);
2027 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2028 ce.height = SH_MIN_H(win);
2033 /* may also be advertized as 0 */
2034 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2035 ce.width = SH_MAX_W(win);
2036 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2037 ce.height = SH_MAX_H(win);
2040 /* resize increment. */
2042 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2043 ce.width -= (ce.width - SH_MIN_W(win)) %
2045 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2046 ce.height -= (ce.height - SH_MIN_H(win)) %
2050 /* adjust x and y for requested border_width. */
2051 ce.x += BORDER(win) - ev->border_width;
2052 ce.y += BORDER(win) - ev->border_width;
2053 ce.border_width = ev->border_width;
2054 ce.above = ev->above;
2057 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2058 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2059 ce.y, ce.width, ce.height, ce.border_width);
2061 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
2065 count_win(struct workspace *ws, int count_transient)
2070 TAILQ_FOREACH(win, &ws->winlist, entry) {
2071 if (count_transient == 0 && win->floating)
2073 if (count_transient == 0 && win->transient)
2079 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2085 quit(struct swm_region *r, union arg *args)
2087 DNPRINTF(SWM_D_MISC, "quit\n");
2092 unmap_window(struct ws_win *win)
2097 /* don't unmap again */
2098 if (getstate(win->id) == IconicState)
2101 set_win_state(win, IconicState);
2103 XUnmapWindow(display, win->id);
2104 XSetWindowBorder(display, win->id,
2105 win->s->c[SWM_S_COLOR_UNFOCUS].color);
2114 for (i = 0; i < ScreenCount(display); i++)
2115 for (j = 0; j < workspace_limit; j++)
2116 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2121 fake_keypress(struct ws_win *win, int keysym, int modifiers)
2128 event.display = display; /* Ignored, but what the hell */
2129 event.window = win->id;
2130 event.root = win->s->root;
2131 event.subwindow = None;
2132 event.time = CurrentTime;
2137 event.same_screen = True;
2138 event.keycode = XKeysymToKeycode(display, keysym);
2139 event.state = modifiers;
2141 event.type = KeyPress;
2142 XSendEvent(event.display, event.window, True,
2143 KeyPressMask, (XEvent *)&event);
2145 event.type = KeyRelease;
2146 XSendEvent(event.display, event.window, True,
2147 KeyPressMask, (XEvent *)&event);
2152 restart(struct swm_region *r, union arg *args)
2154 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2156 /* disable alarm because the following code may not be interrupted */
2158 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2159 err(1, "can't disable alarm");
2164 XCloseDisplay(display);
2165 execvp(start_argv[0], start_argv);
2166 warn("execvp failed");
2171 root_to_region(Window root)
2173 struct swm_region *r = NULL;
2175 int i, x, y, wx, wy;
2178 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%lx\n", root);
2180 for (i = 0; i < ScreenCount(display); i++)
2181 if (screens[i].root == root)
2184 if (XQueryPointer(display, screens[i].root,
2185 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
2186 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2188 /* choose a region based on pointer location */
2189 TAILQ_FOREACH(r, &screens[i].rl, entry)
2190 if (X(r) <= x && x < MAX_X(r) &&
2191 Y(r) <= y && y < MAX_Y(r))
2196 r = TAILQ_FIRST(&screens[i].rl);
2202 find_unmanaged_window(Window id)
2207 for (i = 0; i < ScreenCount(display); i++)
2208 for (j = 0; j < workspace_limit; j++)
2209 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2217 find_window(Window id)
2220 Window wrr, wpr, *wcr = NULL;
2224 for (i = 0; i < ScreenCount(display); i++)
2225 for (j = 0; j < workspace_limit; j++)
2226 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2230 /* if we were looking for the parent return that window instead */
2231 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
2236 /* ignore not found and root */
2237 if (wpr == 0 || wrr == wpr)
2240 /* look for parent */
2241 for (i = 0; i < ScreenCount(display); i++)
2242 for (j = 0; j < workspace_limit; j++)
2243 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2251 spawn(int ws_idx, union arg *args, int close_fd)
2256 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2259 close(ConnectionNumber(display));
2261 setenv("LD_PRELOAD", SWM_LIB, 1);
2263 if (asprintf(&ret, "%d", ws_idx) == -1) {
2264 warn("spawn: asprintf SWM_WS");
2267 setenv("_SWM_WS", ret, 1);
2271 if (asprintf(&ret, "%d", getpid()) == -1) {
2272 warn("spawn: asprintf _SWM_PID");
2275 setenv("_SWM_PID", ret, 1);
2279 if (setsid() == -1) {
2280 warn("spawn: setsid");
2286 * close stdin and stdout to prevent interaction between apps
2287 * and the baraction script
2288 * leave stderr open to record errors
2290 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2291 warn("spawn: open");
2294 dup2(fd, STDIN_FILENO);
2295 dup2(fd, STDOUT_FILENO);
2300 execvp(args->argv[0], args->argv);
2302 warn("spawn: execvp");
2307 kill_refs(struct ws_win *win)
2310 struct swm_region *r;
2311 struct workspace *ws;
2316 for (i = 0; i < ScreenCount(display); i++)
2317 TAILQ_FOREACH(r, &screens[i].rl, entry)
2318 for (x = 0; x < workspace_limit; x++) {
2320 if (win == ws->focus)
2322 if (win == ws->focus_prev)
2323 ws->focus_prev = NULL;
2328 validate_win(struct ws_win *testwin)
2331 struct workspace *ws;
2332 struct swm_region *r;
2335 if (testwin == NULL)
2338 for (i = 0; i < ScreenCount(display); i++)
2339 TAILQ_FOREACH(r, &screens[i].rl, entry)
2340 for (x = 0; x < workspace_limit; x++) {
2342 TAILQ_FOREACH(win, &ws->winlist, entry)
2350 validate_ws(struct workspace *testws)
2352 struct swm_region *r;
2353 struct workspace *ws;
2356 /* validate all ws */
2357 for (i = 0; i < ScreenCount(display); i++)
2358 TAILQ_FOREACH(r, &screens[i].rl, entry)
2359 for (x = 0; x < workspace_limit; x++) {
2368 unfocus_win(struct ws_win *win)
2373 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2377 if (win->ws == NULL)
2380 if (validate_ws(win->ws))
2381 return; /* XXX this gets hit with thunderbird, needs fixing */
2383 if (win->ws->r == NULL)
2386 if (validate_win(win)) {
2391 if (win->ws->focus == win) {
2392 win->ws->focus = NULL;
2393 win->ws->focus_prev = win;
2396 if (validate_win(win->ws->focus)) {
2397 kill_refs(win->ws->focus);
2398 win->ws->focus = NULL;
2400 if (validate_win(win->ws->focus_prev)) {
2401 kill_refs(win->ws->focus_prev);
2402 win->ws->focus_prev = NULL;
2405 /* drain all previous unfocus events */
2406 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2409 grabbuttons(win, 0);
2410 XSetWindowBorder(display, win->id,
2411 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2413 XChangeProperty(display, win->s->root,
2414 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2415 PropModeReplace, (unsigned char *)&none, 1);
2424 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2426 for (i = 0; i < ScreenCount(display); i++)
2427 for (j = 0; j < workspace_limit; j++)
2428 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2433 focus_win(struct ws_win *win)
2438 struct ws_win *cfw = NULL;
2441 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2445 if (win->ws == NULL)
2448 if (validate_ws(win->ws))
2449 return; /* XXX this gets hit with thunderbird, needs fixing */
2451 if (validate_win(win)) {
2456 if (validate_win(win)) {
2461 XGetInputFocus(display, &cur_focus, &rr);
2462 if ((cfw = find_window(cur_focus)) != NULL)
2465 /* use larger hammer since the window was killed somehow */
2466 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2467 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2468 XSetWindowBorder(display, cfw->id,
2469 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2472 win->ws->focus = win;
2474 if (win->ws->r != NULL) {
2475 /* drain all previous focus events */
2476 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2480 XSetInputFocus(display, win->id,
2481 RevertToParent, CurrentTime);
2482 grabbuttons(win, 1);
2483 XSetWindowBorder(display, win->id,
2484 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2485 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2486 win->ws->always_raise)
2487 XMapRaised(display, win->id);
2489 XChangeProperty(display, win->s->root,
2490 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2491 PropModeReplace, (unsigned char *)&win->id, 1);
2498 switchws(struct swm_region *r, union arg *args)
2500 int wsid = args->id, unmap_old = 0;
2501 struct swm_region *this_r, *other_r;
2503 struct workspace *new_ws, *old_ws;
2509 if (wsid >= workspace_limit)
2513 old_ws = this_r->ws;
2514 new_ws = &this_r->s->ws[wsid];
2516 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2517 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2519 if (new_ws == NULL || old_ws == NULL)
2521 if (new_ws == old_ws)
2524 other_r = new_ws->r;
2525 if (other_r == NULL) {
2526 /* the other workspace is hidden, hide this one */
2530 /* the other ws is visible in another region, exchange them */
2531 other_r->ws_prior = new_ws;
2532 other_r->ws = old_ws;
2533 old_ws->r = other_r;
2535 this_r->ws_prior = old_ws;
2536 this_r->ws = new_ws;
2539 /* this is needed so that we can click on a window after a restart */
2543 a.id = SWM_ARG_ID_FOCUSCUR;
2544 focus(new_ws->r, &a);
2546 /* unmap old windows */
2548 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2551 if (focus_mode == SWM_FOCUS_DEFAULT)
2552 drain_enter_notify();
2556 cyclews(struct swm_region *r, union arg *args)
2559 struct swm_screen *s = r->s;
2562 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2563 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2568 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2571 case SWM_ARG_ID_CYCLEWS_UP:
2572 if (a.id < workspace_limit - 1)
2577 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2580 case SWM_ARG_ID_CYCLEWS_DOWN:
2584 a.id = workspace_limit - 1;
2591 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2593 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2597 } while (a.id != r->ws->idx);
2601 priorws(struct swm_region *r, union arg *args)
2605 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2606 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2608 if (r->ws_prior == NULL)
2611 a.id = r->ws_prior->idx;
2616 cyclescr(struct swm_region *r, union arg *args)
2618 struct swm_region *rr = NULL;
2622 /* do nothing if we don't have more than one screen */
2623 if (!(ScreenCount(display) > 1 || outputs > 1))
2628 case SWM_ARG_ID_CYCLESC_UP:
2629 rr = TAILQ_NEXT(r, entry);
2631 rr = TAILQ_FIRST(&screens[i].rl);
2633 case SWM_ARG_ID_CYCLESC_DOWN:
2634 rr = TAILQ_PREV(r, swm_region_list, entry);
2636 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2644 /* move mouse to region */
2646 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2647 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2649 a.id = SWM_ARG_ID_FOCUSCUR;
2652 if (rr->ws->focus) {
2653 /* move to focus window */
2654 x = X(rr->ws->focus) + 1;
2655 y = Y(rr->ws->focus) + 1;
2656 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2661 sort_windows(struct ws_win_list *wl)
2663 struct ws_win *win, *parent, *nxt;
2668 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2669 nxt = TAILQ_NEXT(win, entry);
2670 if (win->transient) {
2671 parent = find_window(win->transient);
2672 if (parent == NULL) {
2673 warnx("not possible bug");
2676 TAILQ_REMOVE(wl, win, entry);
2677 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2684 swapwin(struct swm_region *r, union arg *args)
2686 struct ws_win *target, *source;
2687 struct ws_win *cur_focus;
2688 struct ws_win_list *wl;
2691 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2692 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2694 cur_focus = r->ws->focus;
2695 if (cur_focus == NULL)
2699 wl = &source->ws->winlist;
2702 case SWM_ARG_ID_SWAPPREV:
2703 if (source->transient)
2704 source = find_window(source->transient);
2705 target = TAILQ_PREV(source, ws_win_list, entry);
2706 if (target && target->transient)
2707 target = find_window(target->transient);
2708 TAILQ_REMOVE(wl, source, entry);
2710 TAILQ_INSERT_TAIL(wl, source, entry);
2712 TAILQ_INSERT_BEFORE(target, source, entry);
2714 case SWM_ARG_ID_SWAPNEXT:
2715 target = TAILQ_NEXT(source, entry);
2716 /* move the parent and let the sort handle the move */
2717 if (source->transient)
2718 source = find_window(source->transient);
2719 TAILQ_REMOVE(wl, source, entry);
2721 TAILQ_INSERT_HEAD(wl, source, entry);
2723 TAILQ_INSERT_AFTER(wl, target, source, entry);
2725 case SWM_ARG_ID_SWAPMAIN:
2726 target = TAILQ_FIRST(wl);
2727 if (target == source) {
2728 if (source->ws->focus_prev != NULL &&
2729 source->ws->focus_prev != target)
2730 source = source->ws->focus_prev;
2734 if (target == NULL || source == NULL)
2736 source->ws->focus_prev = target;
2737 TAILQ_REMOVE(wl, target, entry);
2738 TAILQ_INSERT_BEFORE(source, target, entry);
2739 TAILQ_REMOVE(wl, source, entry);
2740 TAILQ_INSERT_HEAD(wl, source, entry);
2742 case SWM_ARG_ID_MOVELAST:
2743 TAILQ_REMOVE(wl, source, entry);
2744 TAILQ_INSERT_TAIL(wl, source, entry);
2747 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2757 focus_prev(struct ws_win *win)
2759 struct ws_win *winfocus = NULL;
2760 struct ws_win *cur_focus = NULL;
2761 struct ws_win_list *wl = NULL;
2762 struct workspace *ws = NULL;
2764 if (!(win && win->ws))
2769 cur_focus = ws->focus;
2771 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2772 WINID(win), WINID(cur_focus));
2774 /* pickle, just focus on whatever */
2775 if (cur_focus == NULL) {
2776 /* use prev_focus if valid */
2777 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2778 find_window(WINID(ws->focus_prev)))
2779 winfocus = ws->focus_prev;
2783 /* if transient focus on parent */
2784 if (cur_focus->transient) {
2785 winfocus = find_window(cur_focus->transient);
2789 /* if in max_stack try harder */
2790 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2791 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2792 if (cur_focus != ws->focus_prev)
2793 winfocus = ws->focus_prev;
2795 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2800 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2802 if (winfocus == NULL || winfocus == win) {
2803 switch (focus_close) {
2804 case SWM_STACK_BOTTOM:
2805 winfocus = TAILQ_FIRST(wl);
2808 winfocus = TAILQ_LAST(wl, ws_win_list);
2810 case SWM_STACK_ABOVE:
2811 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2812 if (focus_close_wrap)
2813 winfocus = TAILQ_FIRST(wl);
2815 winfocus = TAILQ_PREV(cur_focus,
2816 ws_win_list, entry);
2819 case SWM_STACK_BELOW:
2820 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2822 if (focus_close_wrap)
2823 winfocus = TAILQ_LAST(wl, ws_win_list);
2825 winfocus = TAILQ_NEXT(cur_focus, entry);
2831 if (winfocus == NULL) {
2832 if (focus_default == SWM_STACK_TOP)
2833 winfocus = TAILQ_LAST(wl, ws_win_list);
2835 winfocus = TAILQ_FIRST(wl);
2839 focus_magic(winfocus);
2843 focus(struct swm_region *r, union arg *args)
2845 struct ws_win *winfocus = NULL, *head;
2846 struct ws_win *cur_focus = NULL;
2847 struct ws_win_list *wl = NULL;
2848 struct workspace *ws = NULL;
2854 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2856 /* treat FOCUS_CUR special */
2857 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2858 if (r->ws->focus && r->ws->focus->iconic == 0)
2859 winfocus = r->ws->focus;
2860 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2861 winfocus = r->ws->focus_prev;
2863 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2864 if (winfocus->iconic == 0)
2867 focus_magic(winfocus);
2871 if ((cur_focus = r->ws->focus) == NULL)
2875 if (TAILQ_EMPTY(wl))
2877 /* make sure there is at least one uniconified window */
2879 TAILQ_FOREACH(winfocus, wl, entry)
2880 if (winfocus->iconic == 0) {
2888 case SWM_ARG_ID_FOCUSPREV:
2889 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2891 head = TAILQ_LAST(wl, ws_win_list);
2893 if (WINID(winfocus) == cur_focus->transient) {
2894 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2896 head = TAILQ_LAST(wl, ws_win_list);
2901 if (winfocus && winfocus->iconic) {
2902 while (winfocus != cur_focus) {
2903 if (winfocus == NULL)
2904 winfocus = TAILQ_LAST(wl, ws_win_list);
2905 if (winfocus->iconic == 0)
2907 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2913 case SWM_ARG_ID_FOCUSNEXT:
2914 head = TAILQ_NEXT(cur_focus, entry);
2916 head = TAILQ_FIRST(wl);
2920 if (winfocus && winfocus->iconic) {
2921 while (winfocus != cur_focus) {
2922 if (winfocus == NULL)
2923 winfocus = TAILQ_FIRST(wl);
2924 if (winfocus->iconic == 0)
2926 winfocus = TAILQ_NEXT(winfocus, entry);
2931 case SWM_ARG_ID_FOCUSMAIN:
2932 winfocus = TAILQ_FIRST(wl);
2933 if (winfocus == cur_focus)
2934 winfocus = cur_focus->ws->focus_prev;
2941 focus_magic(winfocus);
2945 cycle_layout(struct swm_region *r, union arg *args)
2947 struct workspace *ws = r->ws;
2950 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2953 if (ws->cur_layout->l_stack == NULL)
2954 ws->cur_layout = &layouts[0];
2957 if (focus_mode == SWM_FOCUS_DEFAULT)
2958 drain_enter_notify();
2959 a.id = SWM_ARG_ID_FOCUSCUR;
2964 stack_config(struct swm_region *r, union arg *args)
2966 struct workspace *ws = r->ws;
2968 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2971 if (ws->cur_layout->l_config != NULL)
2972 ws->cur_layout->l_config(ws, args->id);
2974 if (args->id != SWM_ARG_ID_STACKINIT)
2981 struct swm_geometry g;
2982 struct swm_region *r;
2988 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2990 for (i = 0; i < ScreenCount(display); i++) {
2994 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2995 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2996 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2998 /* start with screen geometry, adjust for bar */
3000 g.w -= 2 * border_width;
3001 g.h -= 2 * border_width;
3007 r->ws->cur_layout->l_stack(r->ws, &g);
3008 r->ws->cur_layout->l_string(r->ws);
3009 /* save r so we can track region changes */
3016 if (focus_mode == SWM_FOCUS_DEFAULT)
3017 drain_enter_notify();
3019 DNPRINTF(SWM_D_STACK, "stack: end\n");
3023 store_float_geom(struct ws_win *win, struct swm_region *r)
3025 /* retain window geom and region geom */
3026 win->g_float = win->g;
3027 win->g_float.x -= X(r);
3028 win->g_float.y -= Y(r);
3029 win->g_floatvalid = 1;
3030 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3031 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3032 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3033 win->g_float.w, win->g_float.h);
3037 stack_floater(struct ws_win *win, struct swm_region *r)
3042 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%lx\n", win->id);
3045 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3046 * geom on ws switches or return from max mode
3048 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3049 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3050 /* refloat at last floating relative position */
3051 win->g = win->g_float;
3056 win->floatmaxed = 0;
3059 * if set to fullscreen mode, configure window to maximum size.
3061 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3062 if (!win->g_floatvalid)
3063 store_float_geom(win, win->ws->r);
3065 win->g = win->ws->r->g;
3069 * remove border on fullscreen floater when in fullscreen mode or when
3070 * the quirk is present.
3072 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3073 ((win->quirks & SWM_Q_FULLSCREEN) &&
3074 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3075 if (win->bordered) {
3077 X(win) += border_width;
3078 Y(win) += border_width;
3080 } else if (!win->bordered) {
3082 X(win) -= border_width;
3083 Y(win) -= border_width;
3086 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3087 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3088 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3093 * floaters and transients are auto-centred unless moved
3096 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3097 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3100 /* keep window within region bounds */
3101 constrain_window(win, r, 0);
3107 * Send keystrokes to terminal to decrease/increase the font size as the
3108 * window size changes.
3111 adjust_font(struct ws_win *win)
3113 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3114 win->floating || win->transient)
3117 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3118 WIDTH(win) / win->sh.width_inc < term_width &&
3119 win->font_steps < SWM_MAX_FONT_STEPS) {
3120 win->font_size_boundary[win->font_steps] =
3121 (win->sh.width_inc * term_width) + win->sh.base_width;
3124 win->last_inc = win->sh.width_inc;
3125 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3126 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3127 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3130 win->last_inc = win->sh.width_inc;
3131 fake_keypress(win, XK_KP_Add, ShiftMask);
3135 #define SWAPXY(g) do { \
3137 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3138 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3141 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3143 XWindowAttributes wa;
3144 struct swm_geometry win_g, r_g = *g;
3145 struct ws_win *win, *fs_win = NULL;
3146 int i, j, s, stacks;
3147 int w_inc = 1, h_inc, w_base = 1, h_base;
3148 int hrh, extra = 0, h_slice, last_h = 0;
3149 int split, colno, winno, mwin, msize, mscale;
3150 int remain, missing, v_slice, reconfigure;
3153 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3154 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3156 winno = count_win(ws, 0);
3157 if (winno == 0 && count_win(ws, 1) == 0)
3160 TAILQ_FOREACH(win, &ws->winlist, entry)
3161 if (win->transient == 0 && win->floating == 0
3162 && win->iconic == 0)
3169 w_inc = win->sh.width_inc;
3170 w_base = win->sh.base_width;
3171 mwin = ws->l_state.horizontal_mwin;
3172 mscale = ws->l_state.horizontal_msize;
3173 stacks = ws->l_state.horizontal_stacks;
3176 w_inc = win->sh.height_inc;
3177 w_base = win->sh.base_height;
3178 mwin = ws->l_state.vertical_mwin;
3179 mscale = ws->l_state.vertical_msize;
3180 stacks = ws->l_state.vertical_stacks;
3184 if (stacks > winno - mwin)
3185 stacks = winno - mwin;
3189 h_slice = r_g.h / SWM_H_SLICE;
3190 if (mwin && winno > mwin) {
3191 v_slice = r_g.w / SWM_V_SLICE;
3195 win_g.w = v_slice * mscale;
3197 if (w_inc > 1 && w_inc < v_slice) {
3198 /* adjust for window's requested size increment */
3199 remain = (win_g.w - w_base) % w_inc;
3205 win_g.x += r_g.w - msize;
3208 colno = split = winno / stacks;
3209 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3210 2 * border_width) / stacks);
3212 hrh = r_g.h / colno;
3213 extra = r_g.h - (colno * hrh);
3214 win_g.h = hrh - 2 * border_width;
3216 /* stack all the tiled windows */
3217 i = j = 0, s = stacks;
3218 TAILQ_FOREACH(win, &ws->winlist, entry) {
3219 if (win->transient != 0 || win->floating != 0)
3221 if (win->iconic != 0)
3224 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3229 if (split && i == split) {
3230 colno = (winno - mwin) / stacks;
3231 if (s <= (winno - mwin) % stacks)
3233 split = split + colno;
3234 hrh = (r_g.h / colno);
3235 extra = r_g.h - (colno * hrh);
3239 win_g.x += win_g.w + 2 * border_width;
3240 win_g.w = (r_g.w - msize -
3241 (stacks * 2 * border_width)) / stacks;
3243 win_g.w += (r_g.w - msize -
3244 (stacks * 2 * border_width)) % stacks;
3248 win_g.h = hrh - 2 * border_width;
3250 h_inc = win->sh.width_inc;
3251 h_base = win->sh.base_width;
3253 h_inc = win->sh.height_inc;
3254 h_base = win->sh.base_height;
3256 if (j == colno - 1) {
3257 win_g.h = hrh + extra;
3258 } else if (h_inc > 1 && h_inc < h_slice) {
3259 /* adjust for window's requested size increment */
3260 remain = (win_g.h - h_base) % h_inc;
3261 missing = h_inc - remain;
3263 if (missing <= extra || j == 0) {
3275 win_g.y += last_h + 2 * border_width;
3277 if (disable_border && bar_enabled == 0 && winno == 1){
3279 win_g.w += 2 * border_width;
3280 win_g.h += 2 * border_width;
3285 if (X(win) != win_g.y || Y(win) != win_g.x ||
3286 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3290 WIDTH(win) = win_g.h;
3291 HEIGHT(win) = win_g.w;
3294 if (X(win) != win_g.x || Y(win) != win_g.y ||
3295 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3299 WIDTH(win) = win_g.w;
3300 HEIGHT(win) = win_g.h;
3304 if (bordered != win->bordered) {
3306 win->bordered = bordered;
3314 if (XGetWindowAttributes(display, win->id, &wa))
3315 if (wa.map_state == IsUnmapped)
3316 XMapRaised(display, win->id);
3324 /* now, stack all the floaters and transients */
3325 TAILQ_FOREACH(win, &ws->winlist, entry) {
3326 if (win->transient == 0 && win->floating == 0)
3328 if (win->iconic == 1)
3330 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3335 stack_floater(win, ws->r);
3336 XMapRaised(display, win->id);
3340 stack_floater(fs_win, ws->r);
3341 XMapRaised(display, fs_win->id);
3346 vertical_config(struct workspace *ws, int id)
3348 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3352 case SWM_ARG_ID_STACKRESET:
3353 case SWM_ARG_ID_STACKINIT:
3354 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3355 ws->l_state.vertical_mwin = 1;
3356 ws->l_state.vertical_stacks = 1;
3358 case SWM_ARG_ID_MASTERSHRINK:
3359 if (ws->l_state.vertical_msize > 1)
3360 ws->l_state.vertical_msize--;
3362 case SWM_ARG_ID_MASTERGROW:
3363 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3364 ws->l_state.vertical_msize++;
3366 case SWM_ARG_ID_MASTERADD:
3367 ws->l_state.vertical_mwin++;
3369 case SWM_ARG_ID_MASTERDEL:
3370 if (ws->l_state.vertical_mwin > 0)
3371 ws->l_state.vertical_mwin--;
3373 case SWM_ARG_ID_STACKINC:
3374 ws->l_state.vertical_stacks++;
3376 case SWM_ARG_ID_STACKDEC:
3377 if (ws->l_state.vertical_stacks > 1)
3378 ws->l_state.vertical_stacks--;
3380 case SWM_ARG_ID_FLIPLAYOUT:
3381 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3389 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3391 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3393 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3397 horizontal_config(struct workspace *ws, int id)
3399 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3402 case SWM_ARG_ID_STACKRESET:
3403 case SWM_ARG_ID_STACKINIT:
3404 ws->l_state.horizontal_mwin = 1;
3405 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3406 ws->l_state.horizontal_stacks = 1;
3408 case SWM_ARG_ID_MASTERSHRINK:
3409 if (ws->l_state.horizontal_msize > 1)
3410 ws->l_state.horizontal_msize--;
3412 case SWM_ARG_ID_MASTERGROW:
3413 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3414 ws->l_state.horizontal_msize++;
3416 case SWM_ARG_ID_MASTERADD:
3417 ws->l_state.horizontal_mwin++;
3419 case SWM_ARG_ID_MASTERDEL:
3420 if (ws->l_state.horizontal_mwin > 0)
3421 ws->l_state.horizontal_mwin--;
3423 case SWM_ARG_ID_STACKINC:
3424 ws->l_state.horizontal_stacks++;
3426 case SWM_ARG_ID_STACKDEC:
3427 if (ws->l_state.horizontal_stacks > 1)
3428 ws->l_state.horizontal_stacks--;
3430 case SWM_ARG_ID_FLIPLAYOUT:
3431 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3439 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3441 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3443 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3446 /* fullscreen view */
3448 max_stack(struct workspace *ws, struct swm_geometry *g)
3450 struct swm_geometry gg = *g;
3451 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3454 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3459 winno = count_win(ws, 0);
3460 if (winno == 0 && count_win(ws, 1) == 0)
3463 TAILQ_FOREACH(win, &ws->winlist, entry) {
3464 if (win->transient) {
3466 parent = find_window(win->transient);
3470 if (win->floating && win->floatmaxed == 0 ) {
3472 * retain geometry for retrieval on exit from
3475 store_float_geom(win, ws->r);
3476 win->floatmaxed = 1;
3479 /* only reconfigure if necessary */
3480 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3481 HEIGHT(win) != gg.h) {
3487 WIDTH(win) += 2 * border_width;
3488 HEIGHT(win) += 2 * border_width;
3493 /* unmap only if we don't have multi screen */
3494 if (win != ws->focus)
3495 if (!(ScreenCount(display) > 1 || outputs > 1))
3499 /* put the last transient on top */
3502 XMapRaised(display, parent->id);
3503 stack_floater(wintrans, ws->r);
3504 focus_magic(wintrans);
3509 send_to_ws(struct swm_region *r, union arg *args)
3511 int wsid = args->id;
3512 struct ws_win *win = NULL, *parent;
3513 struct workspace *ws, *nws;
3514 Atom ws_idx_atom = 0;
3515 unsigned char ws_idx_str[SWM_PROPLEN];
3518 if (wsid >= workspace_limit)
3521 if (r && r->ws && r->ws->focus)
3527 if (win->ws->idx == wsid)
3530 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3533 nws = &win->s->ws[wsid];
3535 a.id = SWM_ARG_ID_FOCUSPREV;
3537 if (win->transient) {
3538 parent = find_window(win->transient);
3540 unmap_window(parent);
3541 TAILQ_REMOVE(&ws->winlist, parent, entry);
3542 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3547 TAILQ_REMOVE(&ws->winlist, win, entry);
3548 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3549 if (TAILQ_EMPTY(&ws->winlist))
3550 r->ws->focus = NULL;
3553 /* Try to update the window's workspace property */
3554 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3556 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3558 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3560 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3561 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
3569 pressbutton(struct swm_region *r, union arg *args)
3571 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3572 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3576 raise_toggle(struct swm_region *r, union arg *args)
3578 if (r == NULL || r->ws == NULL)
3581 r->ws->always_raise = !r->ws->always_raise;
3583 /* bring floaters back to top */
3584 if (r->ws->always_raise == 0)
3589 iconify(struct swm_region *r, union arg *args)
3593 if (r->ws->focus == NULL)
3595 unmap_window(r->ws->focus);
3596 update_iconic(r->ws->focus, 1);
3598 if (focus_mode == SWM_FOCUS_DEFAULT)
3599 drain_enter_notify();
3600 r->ws->focus = NULL;
3601 a.id = SWM_ARG_ID_FOCUSCUR;
3606 get_win_name(Window win)
3608 unsigned char *prop = NULL;
3609 unsigned long nbytes, nitems;
3611 /* try _NET_WM_NAME first */
3612 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3615 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3616 &nitems, NULL, &prop))
3620 /* fallback to WM_NAME */
3621 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3624 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3631 uniconify(struct swm_region *r, union arg *args)
3635 unsigned char *name;
3638 DNPRINTF(SWM_D_MISC, "uniconify\n");
3640 if (r == NULL || r->ws == NULL)
3643 /* make sure we have anything to uniconify */
3644 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3645 if (win->ws == NULL)
3646 continue; /* should never happen */
3647 if (win->iconic == 0)
3655 search_resp_action = SWM_SEARCH_UNICONIFY;
3657 spawn_select(r, args, "search", &searchpid);
3659 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3662 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3663 if (win->ws == NULL)
3664 continue; /* should never happen */
3665 if (win->iconic == 0)
3668 name = get_win_name(win->id);
3671 fprintf(lfile, "%s.%lu\n", name, win->id);
3679 name_workspace(struct swm_region *r, union arg *args)
3683 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3689 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3691 spawn_select(r, args, "name_workspace", &searchpid);
3693 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3696 fprintf(lfile, "%s", "");
3701 search_workspace(struct swm_region *r, union arg *args)
3704 struct workspace *ws;
3707 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3713 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3715 spawn_select(r, args, "search", &searchpid);
3717 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3720 for (i = 0; i < workspace_limit; i++) {
3724 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3725 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3732 search_win_cleanup(void)
3734 struct search_window *sw = NULL;
3736 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3737 XDestroyWindow(display, sw->indicator);
3738 XFreeGC(display, sw->gc);
3739 TAILQ_REMOVE(&search_wl, sw, entry);
3745 search_win(struct swm_region *r, union arg *args)
3747 struct ws_win *win = NULL;
3748 struct search_window *sw = NULL;
3755 XRectangle ibox, lbox;
3757 DNPRINTF(SWM_D_MISC, "search_win\n");
3760 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3762 spawn_select(r, args, "search", &searchpid);
3764 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3767 TAILQ_INIT(&search_wl);
3770 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3771 if (win->iconic == 1)
3774 sw = calloc(1, sizeof(struct search_window));
3776 warn("search_win: calloc");
3778 search_win_cleanup();
3784 snprintf(s, sizeof s, "%d", i);
3787 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3789 w = XCreateSimpleWindow(display,
3790 win->id, 0, 0,lbox.width + 4,
3791 bar_fs_extents->max_logical_extent.height, 1,
3792 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3793 r->s->c[SWM_S_COLOR_FOCUS].color);
3796 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3798 sw->gc = XCreateGC(display, w, 0, &gcv);
3799 XMapRaised(display, w);
3800 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3802 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3803 (bar_fs_extents->max_logical_extent.height -
3804 lbox.height) / 2 - lbox.y, s, len);
3806 fprintf(lfile, "%d\n", i);
3814 search_resp_uniconify(char *resp, unsigned long len)
3816 unsigned char *name;
3820 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3822 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3823 if (win->iconic == 0)
3825 name = get_win_name(win->id);
3828 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3833 if (strncmp(s, resp, len) == 0) {
3834 /* XXX this should be a callback to generalize */
3835 update_iconic(win, 0);
3844 search_resp_name_workspace(char *resp, unsigned long len)
3846 struct workspace *ws;
3848 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3850 if (search_r->ws == NULL)
3855 free(search_r->ws->name);
3856 search_r->ws->name = NULL;
3860 ws->name = strdup(resp);
3861 if (ws->name == NULL) {
3862 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3863 "strdup: %s", strerror(errno));
3870 search_resp_search_workspace(char *resp, unsigned long len)
3877 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3881 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3888 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3890 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3897 switchws(search_r, &a);
3901 search_resp_search_window(char *resp, unsigned long len)
3906 struct search_window *sw;
3908 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3912 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3917 idx = strtonum(s, 1, INT_MAX, &errstr);
3919 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3926 TAILQ_FOREACH(sw, &search_wl, entry)
3927 if (idx == sw->idx) {
3933 #define MAX_RESP_LEN 1024
3936 search_do_resp(void)
3942 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3947 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3948 warn("search: calloc");
3952 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3954 warn("search: read error");
3957 resp[rbytes] = '\0';
3960 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3961 * newline, so work around that by sanitizing the resp now.
3963 resp[strcspn(resp, "\n")] = '\0';
3966 switch (search_resp_action) {
3967 case SWM_SEARCH_UNICONIFY:
3968 search_resp_uniconify(resp, len);
3970 case SWM_SEARCH_NAME_WORKSPACE:
3971 search_resp_name_workspace(resp, len);
3973 case SWM_SEARCH_SEARCH_WORKSPACE:
3974 search_resp_search_workspace(resp, len);
3976 case SWM_SEARCH_SEARCH_WINDOW:
3977 search_resp_search_window(resp, len);
3982 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3983 search_win_cleanup();
3985 search_resp_action = SWM_SEARCH_NONE;
3986 close(select_resp_pipe[0]);
3991 wkill(struct swm_region *r, union arg *args)
3993 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3995 if (r->ws->focus == NULL)
3998 if (args->id == SWM_ARG_ID_KILLWINDOW)
3999 XKillClient(display, r->ws->focus->id);
4001 if (r->ws->focus->can_delete)
4002 client_msg(r->ws->focus, adelete);
4007 floating_toggle_win(struct ws_win *win)
4009 struct swm_region *r;
4019 /* reject floating toggles in max stack mode */
4020 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4023 if (win->floating) {
4024 if (!win->floatmaxed) {
4025 /* retain position for refloat */
4026 store_float_geom(win, r);
4030 if (win->g_floatvalid) {
4031 /* refloat at last floating relative position */
4032 X(win) = win->g_float.x + X(r);
4033 Y(win) = win->g_float.y + Y(r);
4034 WIDTH(win) = win->g_float.w;
4035 HEIGHT(win) = win->g_float.h;
4040 ewmh_update_actions(win);
4046 floating_toggle(struct swm_region *r, union arg *args)
4048 struct ws_win *win = r->ws->focus;
4054 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4055 _NET_WM_STATE_TOGGLE);
4058 if (focus_mode == SWM_FOCUS_DEFAULT)
4059 drain_enter_notify();
4061 if (win == win->ws->focus) {
4062 a.id = SWM_ARG_ID_FOCUSCUR;
4063 focus(win->ws->r, &a);
4068 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4070 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4072 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4074 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4077 if (X(win) + BORDER(win) < X(r)) {
4079 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4081 X(win) = X(r) - BORDER(win);
4084 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4086 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4088 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4091 if (Y(win) + BORDER(win) < Y(r)) {
4093 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4095 Y(win) = Y(r) - BORDER(win);
4101 if (HEIGHT(win) < 1)
4107 update_window(struct ws_win *win)
4112 bzero(&wc, sizeof wc);
4113 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
4115 wc.border_width = BORDER(win);
4118 wc.width = WIDTH(win);
4119 wc.height = HEIGHT(win);
4121 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%lx, (x,y) w x h: "
4122 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc.x, wc.y, wc.width,
4123 wc.height, YESNO(win->bordered));
4125 XConfigureWindow(display, win->id, mask, &wc);
4128 #define SWM_RESIZE_STEPS (50)
4131 resize(struct ws_win *win, union arg *args)
4135 struct swm_region *r = NULL;
4136 int resize_step = 0;
4140 struct swm_geometry g;
4141 int top = 0, left = 0;
4144 unsigned int shape; /* cursor style */
4150 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4153 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
4154 "transient: 0x%lx\n", win->id, YESNO(win->floating),
4157 if (!(win->transient != 0 || win->floating != 0))
4160 /* reject resizes in max mode for floaters (transient ok) */
4161 if (win->floatmaxed)
4165 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4171 case SWM_ARG_ID_WIDTHSHRINK:
4172 WIDTH(win) -= SWM_RESIZE_STEPS;
4175 case SWM_ARG_ID_WIDTHGROW:
4176 WIDTH(win) += SWM_RESIZE_STEPS;
4179 case SWM_ARG_ID_HEIGHTSHRINK:
4180 HEIGHT(win) -= SWM_RESIZE_STEPS;
4183 case SWM_ARG_ID_HEIGHTGROW:
4184 HEIGHT(win) += SWM_RESIZE_STEPS;
4191 constrain_window(win, r, 1);
4193 store_float_geom(win,r);
4197 if (focus_mode == SWM_FOCUS_DEFAULT)
4198 drain_enter_notify();
4200 /* get cursor offset from window root */
4201 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4206 if (wx < WIDTH(win) / 2)
4209 if (wy < HEIGHT(win) / 2)
4212 if (args->id == SWM_ARG_ID_CENTER)
4215 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4217 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4219 cursor = XCreateFontCursor(display, shape);
4221 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4222 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
4223 XFreeCursor(display, cursor);
4228 XMaskEvent(display, MOUSEMASK | ExposureMask |
4229 SubstructureRedirectMask, &ev);
4231 case ConfigureRequest:
4234 handler[ev.type](&ev);
4237 /* cursor offset/delta from start of the operation */
4238 dx = ev.xmotion.x_root - x;
4239 dy = ev.xmotion.y_root - y;
4245 if (args->id == SWM_ARG_ID_CENTER) {
4246 if (g.h / 2 + dy < 1)
4250 HEIGHT(win) = g.h + 2 * dy;
4258 HEIGHT(win) = g.h + dy;
4265 if (args->id == SWM_ARG_ID_CENTER) {
4266 if (g.w / 2 + dx < 1)
4270 WIDTH(win) = g.w + 2 * dx;
4278 WIDTH(win) = g.w + dx;
4281 constrain_window(win, r, 1);
4283 /* not free, don't sync more than 120 times / second */
4284 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4285 time = ev.xmotion.time;
4286 XSync(display, False);
4291 } while (ev.type != ButtonRelease);
4293 XSync(display, False);
4296 store_float_geom(win,r);
4298 XUngrabPointer(display, CurrentTime);
4299 XFreeCursor(display, cursor);
4302 drain_enter_notify();
4306 resize_step(struct swm_region *r, union arg *args)
4308 struct ws_win *win = NULL;
4310 if (r && r->ws && r->ws->focus)
4318 #define SWM_MOVE_STEPS (50)
4321 move(struct ws_win *win, union arg *args)
4326 struct swm_region *r = NULL;
4336 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4339 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4340 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4342 /* in max_stack mode should only move transients */
4343 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4347 if (win->floating == 0 && !win->transient) {
4348 store_float_geom(win, r);
4349 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4352 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4359 case SWM_ARG_ID_MOVELEFT:
4360 X(win) -= (SWM_MOVE_STEPS - border_width);
4363 case SWM_ARG_ID_MOVERIGHT:
4364 X(win) += (SWM_MOVE_STEPS - border_width);
4367 case SWM_ARG_ID_MOVEUP:
4368 Y(win) -= (SWM_MOVE_STEPS - border_width);
4371 case SWM_ARG_ID_MOVEDOWN:
4372 Y(win) += (SWM_MOVE_STEPS - border_width);
4379 constrain_window(win, r, 0);
4381 store_float_geom(win, r);
4385 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4386 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4387 CurrentTime) != GrabSuccess)
4390 /* get cursor offset from window root */
4391 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4395 XMaskEvent(display, MOUSEMASK | ExposureMask |
4396 SubstructureRedirectMask, &ev);
4398 case ConfigureRequest:
4401 handler[ev.type](&ev);
4404 X(win) = ev.xmotion.x_root - wx - border_width;
4405 Y(win) = ev.xmotion.y_root - wy - border_width;
4407 constrain_window(win, r, 0);
4409 /* not free, don't sync more than 120 times / second */
4410 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4411 time = ev.xmotion.time;
4412 XSync(display, False);
4417 } while (ev.type != ButtonRelease);
4419 XSync(display, False);
4422 store_float_geom(win, r);
4423 XUngrabPointer(display, CurrentTime);
4426 drain_enter_notify();
4430 move_step(struct swm_region *r, union arg *args)
4432 struct ws_win *win = NULL;
4434 if (r && r->ws && r->ws->focus)
4439 if (!(win->transient != 0 || win->floating != 0))
4446 /* user/key callable function IDs */
4535 kf_search_workspace,
4537 kf_dumpwins, /* MUST BE LAST */
4541 /* key definitions */
4543 dummykeyfunc(struct swm_region *r, union arg *args)
4548 char name[SWM_FUNCNAME_LEN];
4549 void (*func)(struct swm_region *r, union arg *);
4551 } keyfuncs[kf_invalid + 1] = {
4552 /* name function argument */
4553 { "cycle_layout", cycle_layout, {0} },
4554 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4555 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4556 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4557 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4558 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4559 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4560 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4561 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4562 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4563 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4564 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4565 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4566 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4567 { "quit", quit, {0} },
4568 { "restart", restart, {0} },
4569 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4570 { "ws_1", switchws, {.id = 0} },
4571 { "ws_2", switchws, {.id = 1} },
4572 { "ws_3", switchws, {.id = 2} },
4573 { "ws_4", switchws, {.id = 3} },
4574 { "ws_5", switchws, {.id = 4} },
4575 { "ws_6", switchws, {.id = 5} },
4576 { "ws_7", switchws, {.id = 6} },
4577 { "ws_8", switchws, {.id = 7} },
4578 { "ws_9", switchws, {.id = 8} },
4579 { "ws_10", switchws, {.id = 9} },
4580 { "ws_11", switchws, {.id = 10} },
4581 { "ws_12", switchws, {.id = 11} },
4582 { "ws_13", switchws, {.id = 12} },
4583 { "ws_14", switchws, {.id = 13} },
4584 { "ws_15", switchws, {.id = 14} },
4585 { "ws_16", switchws, {.id = 15} },
4586 { "ws_17", switchws, {.id = 16} },
4587 { "ws_18", switchws, {.id = 17} },
4588 { "ws_19", switchws, {.id = 18} },
4589 { "ws_20", switchws, {.id = 19} },
4590 { "ws_21", switchws, {.id = 20} },
4591 { "ws_22", switchws, {.id = 21} },
4592 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4593 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4594 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4595 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4596 { "ws_prior", priorws, {0} },
4597 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4598 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4599 { "mvws_1", send_to_ws, {.id = 0} },
4600 { "mvws_2", send_to_ws, {.id = 1} },
4601 { "mvws_3", send_to_ws, {.id = 2} },
4602 { "mvws_4", send_to_ws, {.id = 3} },
4603 { "mvws_5", send_to_ws, {.id = 4} },
4604 { "mvws_6", send_to_ws, {.id = 5} },
4605 { "mvws_7", send_to_ws, {.id = 6} },
4606 { "mvws_8", send_to_ws, {.id = 7} },
4607 { "mvws_9", send_to_ws, {.id = 8} },
4608 { "mvws_10", send_to_ws, {.id = 9} },
4609 { "mvws_11", send_to_ws, {.id = 10} },
4610 { "mvws_12", send_to_ws, {.id = 11} },
4611 { "mvws_13", send_to_ws, {.id = 12} },
4612 { "mvws_14", send_to_ws, {.id = 13} },
4613 { "mvws_15", send_to_ws, {.id = 14} },
4614 { "mvws_16", send_to_ws, {.id = 15} },
4615 { "mvws_17", send_to_ws, {.id = 16} },
4616 { "mvws_18", send_to_ws, {.id = 17} },
4617 { "mvws_19", send_to_ws, {.id = 18} },
4618 { "mvws_20", send_to_ws, {.id = 19} },
4619 { "mvws_21", send_to_ws, {.id = 20} },
4620 { "mvws_22", send_to_ws, {.id = 21} },
4621 { "bar_toggle", bar_toggle, {0} },
4622 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4623 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4624 { "float_toggle", floating_toggle,{0} },
4625 { "version", version, {0} },
4626 { "spawn_custom", dummykeyfunc, {0} },
4627 { "iconify", iconify, {0} },
4628 { "uniconify", uniconify, {0} },
4629 { "raise_toggle", raise_toggle, {0} },
4630 { "button2", pressbutton, {2} },
4631 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4632 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4633 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4634 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4635 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4636 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4637 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4638 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4639 { "name_workspace", name_workspace, {0} },
4640 { "search_workspace", search_workspace, {0} },
4641 { "search_win", search_win, {0} },
4642 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4643 { "invalid key func", NULL, {0} },
4646 RB_ENTRY(key) entry;
4649 enum keyfuncid funcid;
4652 RB_HEAD(key_tree, key);
4655 key_cmp(struct key *kp1, struct key *kp2)
4657 if (kp1->keysym < kp2->keysym)
4659 if (kp1->keysym > kp2->keysym)
4662 if (kp1->mod < kp2->mod)
4664 if (kp1->mod > kp2->mod)
4670 RB_GENERATE(key_tree, key, entry, key_cmp);
4671 struct key_tree keys;
4674 enum { client_click, root_click };
4676 unsigned int action;
4678 unsigned int button;
4679 void (*func)(struct ws_win *, union arg *);
4682 /* action key mouse button func args */
4683 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4684 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4685 { client_click, MODKEY, Button1, move, {0} },
4689 update_modkey(unsigned int mod)
4695 RB_FOREACH(kp, key_tree, &keys)
4696 if (kp->mod & ShiftMask)
4697 kp->mod = mod | ShiftMask;
4701 for (i = 0; i < LENGTH(buttons); i++)
4702 if (buttons[i].mask & ShiftMask)
4703 buttons[i].mask = mod | ShiftMask;
4705 buttons[i].mask = mod;
4710 TAILQ_ENTRY(spawn_prog) entry;
4715 TAILQ_HEAD(spawn_list, spawn_prog);
4716 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4719 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4722 struct spawn_prog *prog = NULL;
4724 char *ap, **real_args;
4726 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4729 TAILQ_FOREACH(prog, &spawns, entry) {
4730 if (!strcasecmp(spawn_name, prog->name))
4734 warnx("spawn_custom: program %s not found", spawn_name);
4738 /* make room for expanded args */
4739 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4740 err(1, "spawn_custom: calloc real_args");
4742 /* expand spawn_args into real_args */
4743 for (i = 0; i < prog->argc; i++) {
4745 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4746 if (!strcasecmp(ap, "$bar_border")) {
4748 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4750 err(1, "spawn_custom border color");
4751 } else if (!strcasecmp(ap, "$bar_color")) {
4753 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4755 err(1, "spawn_custom bar color");
4756 } else if (!strcasecmp(ap, "$bar_font")) {
4757 if ((real_args[i] = strdup(bar_fonts))
4759 err(1, "spawn_custom bar fonts");
4760 } else if (!strcasecmp(ap, "$bar_font_color")) {
4762 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4764 err(1, "spawn_custom color font");
4765 } else if (!strcasecmp(ap, "$color_focus")) {
4767 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4769 err(1, "spawn_custom color focus");
4770 } else if (!strcasecmp(ap, "$color_unfocus")) {
4772 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4774 err(1, "spawn_custom color unfocus");
4776 /* no match --> copy as is */
4777 if ((real_args[i] = strdup(ap)) == NULL)
4778 err(1, "spawn_custom strdup(ap)");
4780 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4785 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4786 for (i = 0; i < prog->argc; i++)
4787 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4788 DNPRINTF(SWM_D_SPAWN, "\n");
4790 *ret_args = real_args;
4791 return (prog->argc);
4795 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4801 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4805 spawn(r->ws->idx, &a, 1);
4807 for (i = 0; i < spawn_argc; i++)
4813 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4819 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4823 if (pipe(select_list_pipe) == -1)
4824 err(1, "pipe error");
4825 if (pipe(select_resp_pipe) == -1)
4826 err(1, "pipe error");
4828 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4829 err(1, "could not disable SIGPIPE");
4830 switch (*pid = fork()) {
4832 err(1, "cannot fork");
4835 if (dup2(select_list_pipe[0], 0) == -1)
4837 if (dup2(select_resp_pipe[1], 1) == -1)
4839 close(select_list_pipe[1]);
4840 close(select_resp_pipe[0]);
4841 spawn(r->ws->idx, &a, 0);
4843 default: /* parent */
4844 close(select_list_pipe[0]);
4845 close(select_resp_pipe[1]);
4849 for (i = 0; i < spawn_argc; i++)
4855 spawn_insert(char *name, char *args)
4857 char *arg, *cp, *ptr;
4858 struct spawn_prog *sp;
4860 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4862 if ((sp = calloc(1, sizeof *sp)) == NULL)
4863 err(1, "spawn_insert: malloc");
4864 if ((sp->name = strdup(name)) == NULL)
4865 err(1, "spawn_insert: strdup");
4867 /* convert the arguments to an argument list */
4868 if ((ptr = cp = strdup(args)) == NULL)
4869 err(1, "spawn_insert: strdup");
4870 while ((arg = strsep(&ptr, " \t")) != NULL) {
4871 /* empty field; skip it */
4876 if ((sp->argv = realloc(sp->argv, sp->argc *
4877 sizeof *sp->argv)) == NULL)
4878 err(1, "spawn_insert: realloc");
4879 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4880 err(1, "spawn_insert: strdup");
4884 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4885 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4889 spawn_remove(struct spawn_prog *sp)
4893 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4895 TAILQ_REMOVE(&spawns, sp, entry);
4896 for (i = 0; i < sp->argc; i++)
4902 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4906 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4908 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4911 spawn_insert(name, args);
4913 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4917 setspawn(char *name, char *args)
4919 struct spawn_prog *sp;
4921 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4926 TAILQ_FOREACH(sp, &spawns, entry) {
4927 if (!strcmp(sp->name, name)) {
4931 spawn_replace(sp, name, args);
4932 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4936 if (*args == '\0') {
4937 warnx("error: setspawn: cannot find program: %s", name);
4941 spawn_insert(name, args);
4942 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4946 setconfspawn(char *selector, char *value, int flags)
4948 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4950 setspawn(selector, value);
4952 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4959 setconfspawn("term", "xterm", 0);
4960 setconfspawn("spawn_term", "xterm", 0);
4961 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4962 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4963 setconfspawn("lock", "xlock", 0);
4964 setconfspawn("initscr", "initscreen.sh", 0);
4965 setconfspawn("menu", "dmenu_run"
4968 " -nf $bar_font_color"
4970 " -sf $bar_color", 0);
4971 setconfspawn("search", "dmenu"
4975 " -nf $bar_font_color"
4977 " -sf $bar_color", 0);
4978 setconfspawn("name_workspace", "dmenu"
4982 " -nf $bar_font_color"
4984 " -sf $bar_color", 0);
4988 #define SWM_MODNAME_SIZE 32
4989 #define SWM_KEY_WS "\n+ \t"
4991 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4995 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4996 if (mod == NULL || ks == NULL) {
4997 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5000 if (keystr == NULL || strlen(keystr) == 0) {
5001 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5007 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5008 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5010 cp += (long)strspn(cp, SWM_KEY_WS);
5011 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5013 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5015 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5017 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5019 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5021 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5023 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5024 *mod |= ControlMask;
5026 *ks = XStringToKeysym(name);
5027 XConvertCase(*ks, ks, &uks);
5028 if (ks == NoSymbol) {
5030 "parsekeys: invalid key %s\n",
5036 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5041 strdupsafe(char *str)
5046 return (strdup(str));
5050 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5054 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5055 keyfuncs[kfid].name, spawn_name);
5057 if ((kp = malloc(sizeof *kp)) == NULL)
5058 err(1, "key_insert: malloc");
5063 kp->spawn_name = strdupsafe(spawn_name);
5064 RB_INSERT(key_tree, &keys, kp);
5066 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5070 key_lookup(unsigned int mod, KeySym ks)
5077 return (RB_FIND(key_tree, &keys, &kp));
5081 key_remove(struct key *kp)
5083 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5085 RB_REMOVE(key_tree, &keys, kp);
5086 free(kp->spawn_name);
5089 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5093 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5096 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5100 key_insert(mod, ks, kfid, spawn_name);
5102 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5106 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5111 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5112 keyfuncs[kfid].name, spawn_name);
5114 if ((kp = key_lookup(mod, ks)) != NULL) {
5115 if (kfid == kf_invalid)
5118 key_replace(kp, mod, ks, kfid, spawn_name);
5119 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5122 if (kfid == kf_invalid) {
5123 warnx("error: setkeybinding: cannot find mod/key combination");
5124 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5128 key_insert(mod, ks, kfid, spawn_name);
5129 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5133 setconfbinding(char *selector, char *value, int flags)
5135 enum keyfuncid kfid;
5138 struct spawn_prog *sp;
5139 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5140 if (selector == NULL) {
5141 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5142 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5144 setkeybinding(mod, ks, kfid, NULL);
5149 /* search by key function name */
5150 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5151 if (strncasecmp(selector, keyfuncs[kfid].name,
5152 SWM_FUNCNAME_LEN) == 0) {
5153 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5155 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5156 setkeybinding(mod, ks, kfid, NULL);
5162 /* search by custom spawn name */
5163 TAILQ_FOREACH(sp, &spawns, entry) {
5164 if (strcasecmp(selector, sp->name) == 0) {
5165 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5167 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5168 setkeybinding(mod, ks, kf_spawn_custom,
5175 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5182 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5183 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5184 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5185 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5186 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5187 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5188 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5189 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5190 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5191 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5192 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5193 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5194 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5195 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5196 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5197 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5198 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5199 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5200 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5201 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5202 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5203 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5204 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5205 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5206 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5207 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5208 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5209 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5210 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5211 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5212 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5213 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5214 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5215 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5216 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5217 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5218 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5219 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5220 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5221 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5222 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5223 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5224 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5225 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5226 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5227 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5228 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5229 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5230 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5231 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5232 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5233 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5234 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5235 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5236 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5237 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5238 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5239 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5240 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5241 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5242 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5243 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5244 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5245 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5246 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5247 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5248 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5249 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5250 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5251 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5252 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5253 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5254 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5255 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5256 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5257 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5258 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5259 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5260 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5261 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5262 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5263 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5264 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5265 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5266 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5267 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5268 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5269 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5270 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5271 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5272 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5273 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5274 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5275 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5276 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5277 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5279 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5288 while (RB_EMPTY(&keys) == 0) {
5289 kp = RB_ROOT(&keys);
5295 setkeymapping(char *selector, char *value, int flags)
5297 char keymapping_file[PATH_MAX];
5298 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5299 if (value[0] == '~')
5300 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5301 pwd->pw_dir, &value[1]);
5303 strlcpy(keymapping_file, value, sizeof keymapping_file);
5305 /* load new key bindings; if it fails, revert to default bindings */
5306 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5310 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5315 updatenumlockmask(void)
5318 XModifierKeymap *modmap;
5320 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5322 modmap = XGetModifierMapping(display);
5323 for (i = 0; i < 8; i++)
5324 for (j = 0; j < modmap->max_keypermod; j++)
5325 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5326 == XKeysymToKeycode(display, XK_Num_Lock))
5327 numlockmask = (1 << i);
5329 XFreeModifiermap(modmap);
5337 unsigned int modifiers[] =
5338 { 0, LockMask, numlockmask, numlockmask | LockMask };
5341 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5342 updatenumlockmask();
5344 for (k = 0; k < ScreenCount(display); k++) {
5345 if (TAILQ_EMPTY(&screens[k].rl))
5347 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
5348 RB_FOREACH(kp, key_tree, &keys) {
5349 if ((code = XKeysymToKeycode(display, kp->keysym)))
5350 for (j = 0; j < LENGTH(modifiers); j++)
5351 XGrabKey(display, code,
5352 kp->mod | modifiers[j],
5353 screens[k].root, True,
5354 GrabModeAsync, GrabModeAsync);
5360 grabbuttons(struct ws_win *win, int focused)
5363 unsigned int modifiers[] =
5364 { 0, LockMask, numlockmask, numlockmask|LockMask };
5366 updatenumlockmask();
5367 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5369 for (i = 0; i < LENGTH(buttons); i++)
5370 if (buttons[i].action == client_click)
5371 for (j = 0; j < LENGTH(modifiers); j++)
5372 XGrabButton(display, buttons[i].button,
5373 buttons[i].mask | modifiers[j],
5374 win->id, False, BUTTONMASK,
5375 GrabModeAsync, GrabModeSync, None,
5378 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5379 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5382 const char *quirkname[] = {
5383 "NONE", /* config string for "no value" */
5392 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5393 #define SWM_Q_WS "\n|+ \t"
5395 parsequirks(char *qstr, unsigned long *quirk)
5405 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5407 cp += (long)strspn(cp, SWM_Q_WS);
5408 for (i = 0; i < LENGTH(quirkname); i++) {
5409 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5410 DNPRINTF(SWM_D_QUIRK,
5411 "parsequirks: %s\n", name);
5416 *quirk |= 1 << (i-1);
5420 if (i >= LENGTH(quirkname)) {
5421 DNPRINTF(SWM_D_QUIRK,
5422 "parsequirks: invalid quirk [%s]\n", name);
5430 quirk_insert(const char *class, const char *name, unsigned long quirk)
5434 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5437 if ((qp = malloc(sizeof *qp)) == NULL)
5438 err(1, "quirk_insert: malloc");
5439 if ((qp->class = strdup(class)) == NULL)
5440 err(1, "quirk_insert: strdup");
5441 if ((qp->name = strdup(name)) == NULL)
5442 err(1, "quirk_insert: strdup");
5445 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5447 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5451 quirk_remove(struct quirk *qp)
5453 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5454 qp->name, qp->quirk);
5456 TAILQ_REMOVE(&quirks, qp, entry);
5461 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5465 quirk_replace(struct quirk *qp, const char *class, const char *name,
5466 unsigned long quirk)
5468 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5469 qp->name, qp->quirk);
5472 quirk_insert(class, name, quirk);
5474 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5478 setquirk(const char *class, const char *name, unsigned long quirk)
5482 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5485 TAILQ_FOREACH(qp, &quirks, entry) {
5486 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5490 quirk_replace(qp, class, name, quirk);
5491 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5496 warnx("error: setquirk: cannot find class/name combination");
5500 quirk_insert(class, name, quirk);
5501 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5505 setconfquirk(char *selector, char *value, int flags)
5507 char *cp, *class, *name;
5509 unsigned long quirks;
5510 if (selector == NULL)
5512 if ((cp = strchr(selector, ':')) == NULL)
5517 if ((retval = parsequirks(value, &quirks)) == 0)
5518 setquirk(class, name, quirks);
5525 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5526 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5527 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5528 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5529 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5530 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5531 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5532 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5533 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5534 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5535 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5536 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5537 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5540 /* conf file stuff */
5541 #define SWM_CONF_FILE "spectrwm.conf"
5542 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5546 SWM_S_BAR_AT_BOTTOM,
5547 SWM_S_BAR_BORDER_WIDTH,
5554 SWM_S_CLOCK_ENABLED,
5557 SWM_S_CYCLE_VISIBLE,
5559 SWM_S_DISABLE_BORDER,
5561 SWM_S_FOCUS_CLOSE_WRAP,
5562 SWM_S_FOCUS_DEFAULT,
5568 SWM_S_STACK_ENABLED,
5570 SWM_S_TITLE_CLASS_ENABLED,
5571 SWM_S_TITLE_NAME_ENABLED,
5572 SWM_S_URGENT_ENABLED,
5573 SWM_S_VERBOSE_LAYOUT,
5574 SWM_S_WINDOW_NAME_ENABLED,
5575 SWM_S_WORKSPACE_LIMIT
5579 setconfvalue(char *selector, char *value, int flags)
5585 case SWM_S_BAR_ACTION:
5587 if ((bar_argv[0] = strdup(value)) == NULL)
5588 err(1, "setconfvalue: bar_action");
5590 case SWM_S_BAR_AT_BOTTOM:
5591 bar_at_bottom = atoi(value);
5593 case SWM_S_BAR_BORDER_WIDTH:
5594 bar_border_width = atoi(value);
5595 if (bar_border_width < 0)
5596 bar_border_width = 0;
5598 case SWM_S_BAR_DELAY:
5599 bar_delay = atoi(value);
5601 case SWM_S_BAR_ENABLED:
5602 bar_enabled = atoi(value);
5604 case SWM_S_BAR_FONT:
5606 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5607 err(1, "setconfvalue: asprintf: failed to allocate "
5608 "memory for bar_fonts.");
5612 case SWM_S_BAR_FORMAT:
5614 if ((bar_format = strdup(value)) == NULL)
5615 err(1, "setconfvalue: bar_format");
5617 case SWM_S_BAR_JUSTIFY:
5618 if (!strcmp(value, "left"))
5619 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5620 else if (!strcmp(value, "center"))
5621 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5622 else if (!strcmp(value, "right"))
5623 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5625 errx(1, "invalid bar_justify");
5627 case SWM_S_BORDER_WIDTH:
5628 border_width = atoi(value);
5629 if (border_width < 0)
5632 case SWM_S_CLOCK_ENABLED:
5633 clock_enabled = atoi(value);
5635 case SWM_S_CLOCK_FORMAT:
5636 #ifndef SWM_DENY_CLOCK_FORMAT
5638 if ((clock_format = strdup(value)) == NULL)
5639 err(1, "setconfvalue: clock_format");
5642 case SWM_S_CYCLE_EMPTY:
5643 cycle_empty = atoi(value);
5645 case SWM_S_CYCLE_VISIBLE:
5646 cycle_visible = atoi(value);
5648 case SWM_S_DIALOG_RATIO:
5649 dialog_ratio = atof(value);
5650 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5653 case SWM_S_DISABLE_BORDER:
5654 disable_border = atoi(value);
5656 case SWM_S_FOCUS_CLOSE:
5657 if (!strcmp(value, "first"))
5658 focus_close = SWM_STACK_BOTTOM;
5659 else if (!strcmp(value, "last"))
5660 focus_close = SWM_STACK_TOP;
5661 else if (!strcmp(value, "next"))
5662 focus_close = SWM_STACK_ABOVE;
5663 else if (!strcmp(value, "previous"))
5664 focus_close = SWM_STACK_BELOW;
5666 errx(1, "focus_close");
5668 case SWM_S_FOCUS_CLOSE_WRAP:
5669 focus_close_wrap = atoi(value);
5671 case SWM_S_FOCUS_DEFAULT:
5672 if (!strcmp(value, "last"))
5673 focus_default = SWM_STACK_TOP;
5674 else if (!strcmp(value, "first"))
5675 focus_default = SWM_STACK_BOTTOM;
5677 errx(1, "focus_default");
5679 case SWM_S_FOCUS_MODE:
5680 if (!strcmp(value, "default"))
5681 focus_mode = SWM_FOCUS_DEFAULT;
5682 else if (!strcmp(value, "follow_cursor"))
5683 focus_mode = SWM_FOCUS_FOLLOW;
5684 else if (!strcmp(value, "synergy"))
5685 focus_mode = SWM_FOCUS_SYNERGY;
5687 errx(1, "focus_mode");
5689 case SWM_S_SPAWN_ORDER:
5690 if (!strcmp(value, "first"))
5691 spawn_position = SWM_STACK_BOTTOM;
5692 else if (!strcmp(value, "last"))
5693 spawn_position = SWM_STACK_TOP;
5694 else if (!strcmp(value, "next"))
5695 spawn_position = SWM_STACK_ABOVE;
5696 else if (!strcmp(value, "previous"))
5697 spawn_position = SWM_STACK_BELOW;
5699 errx(1, "spawn_position");
5701 case SWM_S_SPAWN_TERM:
5702 setconfspawn("term", value, 0);
5703 setconfspawn("spawn_term", value, 0);
5707 case SWM_S_SS_ENABLED:
5708 ss_enabled = atoi(value);
5710 case SWM_S_STACK_ENABLED:
5711 stack_enabled = atoi(value);
5713 case SWM_S_TERM_WIDTH:
5714 term_width = atoi(value);
5718 case SWM_S_TITLE_CLASS_ENABLED:
5719 title_class_enabled = atoi(value);
5721 case SWM_S_TITLE_NAME_ENABLED:
5722 title_name_enabled = atoi(value);
5724 case SWM_S_URGENT_ENABLED:
5725 urgent_enabled = atoi(value);
5727 case SWM_S_VERBOSE_LAYOUT:
5728 verbose_layout = atoi(value);
5729 for (i = 0; layouts[i].l_stack != NULL; i++) {
5731 layouts[i].l_string = fancy_stacker;
5733 layouts[i].l_string = plain_stacker;
5736 case SWM_S_WINDOW_NAME_ENABLED:
5737 window_name_enabled = atoi(value);
5739 case SWM_S_WORKSPACE_LIMIT:
5740 workspace_limit = atoi(value);
5741 if (workspace_limit > SWM_WS_MAX)
5742 workspace_limit = SWM_WS_MAX;
5743 else if (workspace_limit < 1)
5744 workspace_limit = 1;
5753 setconfmodkey(char *selector, char *value, int flags)
5755 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5756 update_modkey(Mod1Mask);
5757 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5758 update_modkey(Mod2Mask);
5759 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5760 update_modkey(Mod3Mask);
5761 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5762 update_modkey(Mod4Mask);
5769 setconfcolor(char *selector, char *value, int flags)
5771 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5776 setconfregion(char *selector, char *value, int flags)
5778 custom_region(value);
5783 setautorun(char *selector, char *value, int flags)
5793 if (getenv("SWM_STARTED"))
5797 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5798 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5800 if (ws_id < 0 || ws_id >= workspace_limit)
5801 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5804 * This is a little intricate
5806 * If the pid already exists we simply reuse it because it means it was
5807 * used before AND not claimed by manage_window. We get away with
5808 * altering it in the parent after INSERT because this can not be a race
5811 while ((ap = strsep(&sp, " \t")) != NULL) {
5814 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5816 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5817 err(1, "setautorun: realloc");
5818 a.argv[argc - 1] = ap;
5821 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5822 err(1, "setautorun: realloc");
5823 a.argv[argc] = NULL;
5825 if ((pid = fork()) == 0) {
5826 spawn(ws_id, &a, 1);
5835 p = calloc(1, sizeof *p);
5838 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5848 setlayout(char *selector, char *value, int flags)
5850 int ws_id, i, x, mg, ma, si, raise, f = 0;
5851 int st = SWM_V_STACK;
5853 struct workspace *ws;
5855 if (getenv("SWM_STARTED"))
5859 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5860 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5861 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5862 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5865 if (ws_id < 0 || ws_id >= workspace_limit)
5866 errx(1, "layout: invalid workspace %d", ws_id + 1);
5868 if (!strcasecmp(s, "vertical"))
5870 else if (!strcasecmp(s, "vertical_flip")) {
5873 } else if (!strcasecmp(s, "horizontal"))
5875 else if (!strcasecmp(s, "horizontal_flip")) {
5878 } else if (!strcasecmp(s, "fullscreen"))
5881 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5882 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5885 for (i = 0; i < ScreenCount(display); i++) {
5886 ws = (struct workspace *)&screens[i].ws;
5887 ws[ws_id].cur_layout = &layouts[st];
5889 ws[ws_id].always_raise = raise;
5890 if (st == SWM_MAX_STACK)
5894 for (x = 0; x < abs(mg); x++) {
5895 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5896 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5897 SWM_ARG_ID_MASTERSHRINK);
5901 for (x = 0; x < abs(ma); x++) {
5902 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5903 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5904 SWM_ARG_ID_MASTERDEL);
5908 for (x = 0; x < abs(si); x++) {
5909 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5910 si >= 0 ? SWM_ARG_ID_STACKINC :
5911 SWM_ARG_ID_STACKDEC);
5916 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5917 SWM_ARG_ID_FLIPLAYOUT);
5925 /* config options */
5926 struct config_option {
5928 int (*func)(char*, char*, int);
5931 struct config_option configopt[] = {
5932 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5933 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5934 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5935 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5936 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5937 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5938 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5939 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5940 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5941 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5942 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
5943 { "keyboard_mapping", setkeymapping, 0 },
5944 { "bind", setconfbinding, 0 },
5945 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5946 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5947 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5948 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5949 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5950 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5951 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5952 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
5953 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5954 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5955 { "modkey", setconfmodkey, 0 },
5956 { "program", setconfspawn, 0 },
5957 { "quirk", setconfquirk, 0 },
5958 { "region", setconfregion, 0 },
5959 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5960 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5961 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5962 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5963 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5964 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5965 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5966 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5967 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5968 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
5969 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
5970 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
5971 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
5972 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5973 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5974 { "autorun", setautorun, 0 },
5975 { "layout", setlayout, 0 },
5980 conf_load(char *filename, int keymapping)
5983 char *line, *cp, *optsub, *optval;
5984 size_t linelen, lineno = 0;
5985 int wordlen, i, optind;
5986 struct config_option *opt;
5988 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5990 if (filename == NULL) {
5991 warnx("conf_load: no filename");
5994 if ((config = fopen(filename, "r")) == NULL) {
5995 warn("conf_load: fopen: %s", filename);
5999 while (!feof(config)) {
6000 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6003 err(1, "%s", filename);
6008 cp += strspn(cp, " \t\n"); /* eat whitespace */
6009 if (cp[0] == '\0') {
6014 /* get config option */
6015 wordlen = strcspn(cp, "=[ \t\n");
6017 warnx("%s: line %zd: no option found",
6022 for (i = 0; i < LENGTH(configopt); i++) {
6023 opt = &configopt[i];
6024 if (!strncasecmp(cp, opt->optname, wordlen) &&
6025 strlen(opt->optname) == wordlen) {
6031 warnx("%s: line %zd: unknown option %.*s",
6032 filename, lineno, wordlen, cp);
6035 if (keymapping && strcmp(opt->optname, "bind")) {
6036 warnx("%s: line %zd: invalid option %.*s",
6037 filename, lineno, wordlen, cp);
6041 cp += strspn(cp, " \t\n"); /* eat whitespace */
6042 /* get [selector] if any */
6046 wordlen = strcspn(cp, "]");
6049 warnx("%s: line %zd: syntax error",
6054 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6056 warnx("%s: line %zd: unable to allocate"
6057 "memory for selector", filename,
6063 cp += strspn(cp, "] \t\n"); /* eat trailing */
6065 cp += strspn(cp, "= \t\n"); /* eat trailing */
6067 optval = strdup(cp);
6068 /* call function to deal with it all */
6069 if (configopt[optind].func(optsub, optval,
6070 configopt[optind].funcflags) != 0)
6071 errx(1, "%s: line %zd: invalid data for %s",
6072 filename, lineno, configopt[optind].optname);
6079 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6086 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6092 set_child_transient(struct ws_win *win, Window *trans)
6094 struct ws_win *parent, *w;
6095 XWMHints *wmh = NULL;
6096 struct swm_region *r;
6097 struct workspace *ws;
6099 parent = find_window(win->transient);
6101 parent->child_trans = win;
6103 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6104 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
6106 if (win->hints == NULL) {
6107 warnx("no hints for 0x%lx", win->id);
6111 r = root_to_region(win->wa.root);
6113 /* parent doen't exist in our window list */
6114 TAILQ_FOREACH(w, &ws->winlist, entry) {
6118 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
6119 warnx("can't get hints for 0x%lx", w->id);
6123 if (win->hints->window_group != wmh->window_group)
6126 w->child_trans = win;
6127 win->transient = w->id;
6129 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
6130 "transient to 0x%lx\n", win->transient);
6140 window_get_pid(Window win)
6142 Atom actual_type_return;
6143 int actual_format_return = 0;
6144 unsigned long nitems_return = 0;
6145 unsigned long bytes_after_return = 0;
6149 unsigned char *prop = NULL;
6151 if (XGetWindowProperty(display, win,
6152 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
6153 XA_CARDINAL, &actual_type_return, &actual_format_return,
6154 &nitems_return, &bytes_after_return,
6155 (unsigned char**)(void*)&pid) != Success)
6157 if (actual_type_return != XA_CARDINAL)
6168 if (XGetWindowProperty(display, win,
6169 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
6170 XA_STRING, &actual_type_return, &actual_format_return,
6171 &nitems_return, &bytes_after_return, &prop) != Success)
6173 if (actual_type_return != XA_STRING)
6178 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
6179 /* ignore error because strtonum returns 0 anyway */
6186 manage_window(Window id)
6189 struct workspace *ws;
6190 struct ws_win *win, *ww;
6191 int format, i, ws_idx, n, border_me = 0;
6192 unsigned long nitems, bytes;
6193 Atom ws_idx_atom = 0, type;
6194 Atom *prot = NULL, *pp;
6195 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6196 struct swm_region *r;
6201 if ((win = find_window(id)) != NULL)
6202 return (win); /* already being managed */
6204 /* see if we are on the unmanaged list */
6205 if ((win = find_unmanaged_window(id)) != NULL) {
6206 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6207 "window: 0x%lx\n", win->id);
6208 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6210 set_child_transient(win, &trans);
6212 if (trans && (ww = find_window(trans)))
6213 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6214 else if ((ww = win->ws->focus) &&
6215 spawn_position == SWM_STACK_ABOVE)
6216 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6217 else if (ww && spawn_position == SWM_STACK_BELOW)
6218 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6219 else switch (spawn_position) {
6222 case SWM_STACK_ABOVE:
6223 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6225 case SWM_STACK_BOTTOM:
6226 case SWM_STACK_BELOW:
6227 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6230 ewmh_update_actions(win);
6234 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6235 err(1, "manage_window: calloc: failed to allocate memory for "
6241 /* see if we need to override the workspace */
6242 p = find_pid(window_get_pid(id));
6244 /* Get all the window data in one shot */
6245 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
6247 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
6248 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
6250 XGetWindowAttributes(display, id, &win->wa);
6251 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6252 win->hints = XGetWMHints(display, id);
6253 XGetTransientForHint(display, id, &trans);
6255 win->transient = trans;
6256 set_child_transient(win, &trans);
6257 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
6258 "transient: 0x%lx\n", win->id, win->transient);
6261 /* get supported protocols */
6262 if (XGetWMProtocols(display, id, &prot, &n)) {
6263 for (i = 0, pp = prot; i < n; i++, pp++) {
6264 if (*pp == takefocus)
6265 win->take_focus = 1;
6267 win->can_delete = 1;
6273 win->iconic = get_iconic(win);
6276 * Figure out where to put the window. If it was previously assigned to
6277 * a workspace (either by spawn() or manually moving), and isn't
6278 * transient, * put it in the same workspace
6280 r = root_to_region(win->wa.root);
6282 ws = &r->s->ws[p->ws];
6283 TAILQ_REMOVE(&pidlist, p, entry);
6286 } else if (prop && win->transient == 0) {
6287 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6288 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
6291 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6294 ws = &r->s->ws[ws_idx];
6297 /* this should launch transients in the same ws as parent */
6299 if ((ww = find_window(trans)) != NULL)
6305 warnx("manage_window: fix this "
6311 /* set up the window layout */
6314 win->s = r->s; /* this never changes */
6315 if (trans && (ww = find_window(trans)))
6316 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6317 else if (spawn_position == SWM_STACK_ABOVE && win->ws->focus)
6318 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6320 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
6322 /* ignore window border if there is one. */
6323 WIDTH(win) = win->wa.width;
6324 HEIGHT(win) = win->wa.height;
6325 X(win) = win->wa.x + win->wa.border_width;
6326 Y(win) = win->wa.y + win->wa.border_width;
6328 win->g_floatvalid = 0;
6329 win->floatmaxed = 0;
6330 win->ewmh_flags = 0;
6332 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, (x,y) w x h: "
6333 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6334 HEIGHT(win), ws->idx);
6336 constrain_window(win, r, 0);
6338 /* Set window properties so we can remember this after reincarnation */
6339 if (ws_idx_atom && prop == NULL &&
6340 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6342 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6344 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
6345 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
6350 ewmh_autoquirk(win);
6352 if (XGetClassHint(display, win->id, &win->ch)) {
6353 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6354 win->ch.res_class, win->ch.res_name);
6356 /* java is retarded so treat it special */
6357 if (strstr(win->ch.res_name, "sun-awt")) {
6362 TAILQ_FOREACH(qp, &quirks, entry) {
6363 if (!strcmp(win->ch.res_class, qp->class) &&
6364 !strcmp(win->ch.res_name, qp->name)) {
6365 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6366 "class: %s, name: %s\n", win->ch.res_class,
6368 if (qp->quirk & SWM_Q_FLOAT) {
6372 win->quirks = qp->quirk;
6377 /* alter window position if quirky */
6378 if (win->quirks & SWM_Q_ANYWHERE) {
6379 win->manual = 1; /* don't center the quirky windows */
6380 if (bar_enabled && Y(win) < bar_height)
6381 Y(win) = bar_height;
6382 if (WIDTH(win) + X(win) > WIDTH(r))
6383 X(win) = WIDTH(r) - WIDTH(win) - 2;
6387 /* Reset font sizes (the bruteforce way; no default keybinding). */
6388 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6389 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6390 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6391 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6392 fake_keypress(win, XK_KP_Add, ShiftMask);
6395 ewmh_get_win_state(win);
6396 ewmh_update_actions(win);
6397 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6402 X(win) -= border_width;
6403 Y(win) -= border_width;
6407 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
6408 PropertyChangeMask | StructureNotifyMask);
6410 /* floaters need to be mapped if they are in the current workspace */
6411 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6412 XMapRaised(display, win->id);
6418 free_window(struct ws_win *win)
6420 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
6425 /* needed for restart wm */
6426 set_win_state(win, WithdrawnState);
6428 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6430 if (win->ch.res_class)
6431 XFree(win->ch.res_class);
6432 if (win->ch.res_name)
6433 XFree(win->ch.res_name);
6438 memset(win, 0xff, sizeof *win); /* XXX kill later */
6444 unmanage_window(struct ws_win *win)
6446 struct ws_win *parent;
6451 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
6453 if (win->transient) {
6454 parent = find_window(win->transient);
6456 parent->child_trans = NULL;
6459 /* focus on root just in case */
6460 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6469 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6470 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6474 focus_magic(struct ws_win *win)
6476 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6479 /* if there are no windows clear the status-bar */
6484 if (win->child_trans) {
6485 /* win = parent & has a transient so focus on that */
6487 focus_win(win->child_trans);
6488 if (win->child_trans->take_focus)
6489 client_msg(win, takefocus);
6491 /* make sure transient hasn't disappeared */
6492 if (validate_win(win->child_trans) == 0) {
6493 focus_win(win->child_trans);
6494 if (win->child_trans->take_focus)
6495 client_msg(win->child_trans, takefocus);
6497 win->child_trans = NULL;
6499 if (win->take_focus)
6500 client_msg(win, takefocus);
6506 if (win->take_focus)
6507 client_msg(win, takefocus);
6514 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6521 XKeyEvent *ev = &e->xkey;
6523 struct swm_region *r;
6525 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6526 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6528 if (keyfuncs[kp->funcid].func == NULL)
6531 r = root_to_region(ev->root);
6532 if (kp->funcid == kf_spawn_custom)
6533 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6535 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6539 buttonpress(XEvent *e)
6543 XButtonPressedEvent *ev = &e->xbutton;
6545 if ((win = find_window(ev->window)) == NULL)
6549 action = client_click;
6551 for (i = 0; i < LENGTH(buttons); i++)
6552 if (action == buttons[i].action && buttons[i].func &&
6553 buttons[i].button == ev->button &&
6554 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6555 buttons[i].func(win, &buttons[i].args);
6559 configurerequest(XEvent *e)
6561 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6566 if ((win = find_window(ev->window)) == NULL)
6567 if ((win = find_unmanaged_window(ev->window)) == NULL)
6571 bzero(&wc, sizeof wc);
6574 wc.width = ev->width;
6575 wc.height = ev->height;
6576 wc.border_width = ev->border_width;
6577 wc.sibling = ev->above;
6578 wc.stack_mode = ev->detail;
6580 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%lx, "
6581 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev->window,
6582 YESNO(new), wc.x, wc.y, wc.width, wc.height);
6584 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6585 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6586 !(win->sh_mask & EWMH_F_FULLSCREEN)) {
6587 win->g_float.x = ev->x - X(win->ws->r);
6588 win->g_float.y = ev->y - Y(win->ws->r);
6589 win->g_float.w = ev->width;
6590 win->g_float.h = ev->height;
6591 win->g_floatvalid = 1;
6593 if (win->floating) {
6594 win->g = win->g_float;
6595 win->g.x += X(win->ws->r);
6596 win->g.y += Y(win->ws->r);
6599 config_win(win, ev);
6602 config_win(win, ev);
6607 configurenotify(XEvent *e)
6611 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6612 e->xconfigure.window);
6614 win = find_window(e->xconfigure.window);
6616 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6620 if (focus_mode == SWM_FOCUS_DEFAULT)
6621 drain_enter_notify();
6626 destroynotify(XEvent *e)
6629 XDestroyWindowEvent *ev = &e->xdestroywindow;
6631 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6633 if ((win = find_window(ev->window)) == NULL) {
6634 if ((win = find_unmanaged_window(ev->window)) == NULL)
6640 /* make sure we focus on something */
6643 unmanage_window(win);
6645 if (focus_mode == SWM_FOCUS_DEFAULT)
6646 drain_enter_notify();
6651 enternotify(XEvent *e)
6653 XCrossingEvent *ev = &e->xcrossing;
6658 Window focus_return;
6659 int revert_to_return;
6661 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6662 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6663 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6664 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6666 if (ev->mode != NotifyNormal) {
6667 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6672 switch (focus_mode) {
6673 case SWM_FOCUS_DEFAULT:
6675 case SWM_FOCUS_FOLLOW:
6677 case SWM_FOCUS_SYNERGY:
6680 * all these checks need to be in this order because the
6681 * XCheckTypedWindowEvent relies on weeding out the previous events
6683 * making this code an option would enable a follow mouse for focus
6688 * state is set when we are switching workspaces and focus is set when
6689 * the window or a subwindow already has focus (occurs during restart).
6691 * Only honor the focus flag if last_focus_event is not FocusOut,
6692 * this allows spectrwm to continue to control focus when another
6693 * program is also playing with it.
6695 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6696 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6701 * happens when a window is created or destroyed and the border
6702 * crosses the mouse pointer and when switching ws
6704 * we need the subwindow test to see if we came from root in order
6705 * to give focus to floaters
6707 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6708 ev->subwindow == 0) {
6709 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6713 /* this window already has focus */
6714 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6715 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6719 /* this window is being deleted or moved to another ws */
6720 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6722 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6723 XPutBackEvent(display, &cne);
6727 if ((win = find_window(ev->window)) == NULL) {
6728 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6733 * In fullstack kill all enters unless they come from a different ws
6734 * (i.e. another region) or focus has been grabbed externally.
6736 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6737 last_focus_event != FocusOut) {
6738 XGetInputFocus(display, &focus_return, &revert_to_return);
6739 if ((w = find_window(focus_return)) == NULL ||
6741 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6749 if ((win = find_window(ev->window)) == NULL) {
6750 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6755 * if we have more enternotifies let them handle it in due time
6757 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6758 DNPRINTF(SWM_D_EVENT,
6759 "ignoring enternotify: got more enternotify\n");
6760 XPutBackEvent(display, &cne);
6767 /* lets us use one switch statement for arbitrary mode/detail combinations */
6768 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6771 focusevent(XEvent *e)
6775 u_int32_t mode_detail;
6776 XFocusChangeEvent *ev = &e->xfocus;
6778 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6779 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6780 ev->window, ev->mode, ev->detail);
6782 if (last_focus_event == ev->type) {
6783 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6787 last_focus_event = ev->type;
6788 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6790 switch (mode_detail) {
6791 /* synergy client focus operations */
6792 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6793 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6795 /* synergy server focus operations */
6796 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6798 /* Entering applications like rdesktop that mangle the pointer */
6799 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6801 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6802 XSetWindowBorder(display, win->id,
6803 win->ws->r->s->c[ev->type == FocusIn ?
6804 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6807 warnx("ignoring focusevent");
6808 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6815 mapnotify(XEvent *e)
6818 XMapEvent *ev = &e->xmap;
6820 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6822 win = manage_window(ev->window);
6824 set_win_state(win, NormalState);
6827 * focus_win can only set input focus on a mapped window.
6828 * make sure the window really has focus since it is just being mapped.
6830 if (win->ws->focus == win)
6835 mappingnotify(XEvent *e)
6837 XMappingEvent *ev = &e->xmapping;
6839 XRefreshKeyboardMapping(ev);
6840 if (ev->request == MappingKeyboard)
6845 maprequest(XEvent *e)
6848 struct swm_region *r;
6849 XWindowAttributes wa;
6850 XMapRequestEvent *ev = &e->xmaprequest;
6852 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6853 e->xmaprequest.window);
6855 if (!XGetWindowAttributes(display, ev->window, &wa))
6857 if (wa.override_redirect)
6860 win = manage_window(e->xmaprequest.window);
6862 return; /* can't happen */
6866 /* make new win focused */
6867 r = root_to_region(win->wa.root);
6868 if (win->ws == r->ws)
6873 propertynotify(XEvent *e)
6876 XPropertyEvent *ev = &e->xproperty;
6879 name = XGetAtomName(display, ev->atom);
6880 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6885 win = find_window(ev->window);
6889 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6890 update_iconic(win, 0);
6891 XMapRaised(display, win->id);
6899 case XA_WM_NORMAL_HINTS:
6901 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6902 warnx("normal hints: flag 0x%x", win->sh.flags);
6903 if (win->sh.flags & PMinSize) {
6904 WIDTH(win) = win->sh.min_width;
6905 HEIGHT(win) = win->sh.min_height;
6906 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6908 XMoveResizeWindow(display, win->id,
6909 X(win), Y(win), WIDTH(win), HEIGHT(win));
6921 unmapnotify(XEvent *e)
6925 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6927 /* determine if we need to help unmanage this window */
6928 win = find_window(e->xunmap.window);
6932 if (getstate(e->xunmap.window) == NormalState) {
6933 unmanage_window(win);
6936 /* giant hack for apps that don't destroy transient windows */
6937 /* eat a bunch of events to prevent remanaging the window */
6939 while (XCheckWindowEvent(display, e->xunmap.window,
6940 EnterWindowMask, &cne))
6942 while (XCheckWindowEvent(display, e->xunmap.window,
6943 StructureNotifyMask, &cne))
6945 while (XCheckWindowEvent(display, e->xunmap.window,
6946 SubstructureNotifyMask, &cne))
6948 /* resend unmap because we ated it */
6949 XUnmapWindow(display, e->xunmap.window);
6952 if (focus_mode == SWM_FOCUS_DEFAULT)
6953 drain_enter_notify();
6957 visibilitynotify(XEvent *e)
6960 struct swm_region *r;
6962 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6963 e->xvisibility.window);
6964 if (e->xvisibility.state == VisibilityUnobscured)
6965 for (i = 0; i < ScreenCount(display); i++)
6966 TAILQ_FOREACH(r, &screens[i].rl, entry)
6967 if (e->xvisibility.window == WINID(r->bar))
6972 clientmessage(XEvent *e)
6974 XClientMessageEvent *ev;
6979 win = find_window(ev->window);
6983 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6984 ev->window, ev->message_type);
6986 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6987 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6990 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6991 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6992 if (win->can_delete)
6993 client_msg(win, adelete);
6995 XKillClient(display, win->id);
6997 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6998 DNPRINTF(SWM_D_EVENT,
6999 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7000 if (win->floating) {
7001 if (ev->data.l[0] & (1<<8)) /* x */
7002 X(win) = ev->data.l[1];
7003 if (ev->data.l[0] & (1<<9)) /* y */
7004 Y(win) = ev->data.l[2];
7005 if (ev->data.l[0] & (1<<10)) /* width */
7006 WIDTH(win) = ev->data.l[3];
7007 if (ev->data.l[0] & (1<<11)) /* height */
7008 HEIGHT(win) = ev->data.l[4];
7013 /* TODO: Change stack sizes */
7014 /* notify no change was made. */
7015 config_win(win, NULL);
7018 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
7019 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7020 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
7022 ewmh_update_win_state(win, ev->data.l[2],
7030 xerror_start(Display *d, XErrorEvent *ee)
7037 xerror(Display *d, XErrorEvent *ee)
7039 /* warnx("error: %p %p", display, ee); */
7047 xerrorxlib = XSetErrorHandler(xerror_start);
7049 /* this causes an error if some other window manager is running */
7050 XSelectInput(display, DefaultRootWindow(display),
7051 SubstructureRedirectMask);
7052 XSync(display, False);
7056 XSetErrorHandler(xerror);
7057 XSync(display, False);
7062 new_region(struct swm_screen *s, int x, int y, int w, int h)
7064 struct swm_region *r, *n;
7065 struct workspace *ws = NULL;
7068 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7069 s->idx, w, h, x, y);
7071 /* remove any conflicting regions */
7072 n = TAILQ_FIRST(&s->rl);
7075 n = TAILQ_NEXT(r, entry);
7076 if (X(r) < (x + w) &&
7077 (X(r) + WIDTH(r)) > x &&
7079 (Y(r) + HEIGHT(r)) > y) {
7080 if (r->ws->r != NULL)
7081 r->ws->old_r = r->ws->r;
7084 TAILQ_REMOVE(&s->rl, r, entry);
7085 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7089 /* search old regions for one to reuse */
7091 /* size + location match */
7092 TAILQ_FOREACH(r, &s->orl, entry)
7093 if (X(r) == x && Y(r) == y &&
7094 HEIGHT(r) == h && WIDTH(r) == w)
7098 TAILQ_FOREACH(r, &s->orl, entry)
7099 if (HEIGHT(r) == h && WIDTH(r) == w)
7103 TAILQ_REMOVE(&s->orl, r, entry);
7104 /* try to use old region's workspace */
7105 if (r->ws->r == NULL)
7108 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7109 err(1, "new_region: calloc: failed to allocate memory "
7112 /* if we don't have a workspace already, find one */
7114 for (i = 0; i < workspace_limit; i++)
7115 if (s->ws[i].r == NULL) {
7122 errx(1, "new_region: no free workspaces");
7133 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7139 #ifdef SWM_XRR_HAS_CRTC
7141 XRRScreenResources *sr;
7144 #endif /* SWM_XRR_HAS_CRTC */
7145 struct swm_region *r;
7148 if (i >= ScreenCount(display))
7149 errx(1, "scan_xrandr: invalid screen");
7151 /* remove any old regions */
7152 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7153 r->ws->old_r = r->ws->r = NULL;
7155 TAILQ_REMOVE(&screens[i].rl, r, entry);
7156 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7160 /* map virtual screens onto physical screens */
7161 #ifdef SWM_XRR_HAS_CRTC
7162 if (xrandr_support) {
7163 sr = XRRGetScreenResources(display, screens[i].root);
7165 new_region(&screens[i], 0, 0,
7166 DisplayWidth(display, i),
7167 DisplayHeight(display, i));
7171 for (c = 0, ci = NULL; c < ncrtc; c++) {
7172 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
7173 if (ci->noutput == 0)
7176 if (ci != NULL && ci->mode == None)
7177 new_region(&screens[i], 0, 0,
7178 DisplayWidth(display, i),
7179 DisplayHeight(display, i));
7181 new_region(&screens[i],
7182 ci->x, ci->y, ci->width, ci->height);
7185 XRRFreeCrtcInfo(ci);
7186 XRRFreeScreenResources(sr);
7188 #endif /* SWM_XRR_HAS_CRTC */
7190 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
7191 DisplayHeight(display, i));
7196 screenchange(XEvent *e) {
7197 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7198 struct swm_region *r;
7201 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7203 if (!XRRUpdateConfiguration(e))
7206 /* silly event doesn't include the screen index */
7207 for (i = 0; i < ScreenCount(display); i++)
7208 if (screens[i].root == xe->root)
7210 if (i >= ScreenCount(display))
7211 errx(1, "screenchange: screen not found");
7213 /* brute force for now, just re-enumerate the regions */
7216 /* add bars to all regions */
7217 for (i = 0; i < ScreenCount(display); i++)
7218 TAILQ_FOREACH(r, &screens[i].rl, entry)
7221 if (focus_mode == SWM_FOCUS_DEFAULT)
7222 drain_enter_notify();
7228 Window d1, d2, *wins = NULL;
7229 XWindowAttributes wa;
7234 for (i = 0; i < ScreenCount(display); i++) {
7235 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
7238 /* attach windows to a region */
7239 /* normal windows */
7240 for (j = 0; j < no; j++) {
7241 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7242 wa.override_redirect ||
7243 XGetTransientForHint(display, wins[j], &d1))
7246 state = getstate(wins[j]);
7247 manage = state == IconicState;
7248 if (wa.map_state == IsViewable || manage)
7249 manage_window(wins[j]);
7251 /* transient windows */
7252 for (j = 0; j < no; j++) {
7253 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7254 wa.override_redirect)
7257 state = getstate(wins[j]);
7258 manage = state == IconicState;
7259 if (XGetTransientForHint(display, wins[j], &d1) &&
7261 manage_window(wins[j]);
7274 int errorbase, major, minor;
7275 struct workspace *ws;
7278 if ((screens = calloc(ScreenCount(display),
7279 sizeof(struct swm_screen))) == NULL)
7280 err(1, "setup_screens: calloc: failed to allocate memory for "
7283 /* initial Xrandr setup */
7284 xrandr_support = XRRQueryExtension(display,
7285 &xrandr_eventbase, &errorbase);
7287 if (XRRQueryVersion(display, &major, &minor) && major < 1)
7290 /* map physical screens */
7291 for (i = 0; i < ScreenCount(display); i++) {
7292 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7294 TAILQ_INIT(&screens[i].rl);
7295 TAILQ_INIT(&screens[i].orl);
7296 screens[i].root = RootWindow(display, i);
7298 /* set default colors */
7299 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7300 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7301 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7302 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7303 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7305 /* create graphics context on screen */
7306 gcv.graphics_exposures = 0;
7307 screens[i].bar_gc = XCreateGC(display, screens[i].root,
7308 GCGraphicsExposures, &gcv);
7310 /* set default cursor */
7311 XDefineCursor(display, screens[i].root,
7312 XCreateFontCursor(display, XC_left_ptr));
7314 /* init all workspaces */
7315 /* XXX these should be dynamically allocated too */
7316 for (j = 0; j < SWM_WS_MAX; j++) {
7317 ws = &screens[i].ws[j];
7323 TAILQ_INIT(&ws->winlist);
7324 TAILQ_INIT(&ws->unmanagedlist);
7326 for (k = 0; layouts[k].l_stack != NULL; k++)
7327 if (layouts[k].l_config != NULL)
7328 layouts[k].l_config(ws,
7329 SWM_ARG_ID_STACKINIT);
7330 ws->cur_layout = &layouts[0];
7331 ws->cur_layout->l_string(ws);
7337 XRRSelectInput(display, screens[i].root,
7338 RRScreenChangeNotifyMask);
7345 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7346 err(1, "setup_globals: strdup: failed to allocate memory.");
7348 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7349 err(1, "setup_globals: strdup: failed to allocate memory.");
7356 Atom netwmcheck, netwmname, utf8_string;
7359 /* work around sun jdk bugs, code from wmname */
7360 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
7361 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7362 utf8_string = XInternAtom(display, "UTF8_STRING", False);
7363 for (i = 0; i < ScreenCount(display); i++) {
7364 root = screens[i].root;
7365 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
7366 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
7367 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
7369 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
7370 PropModeReplace, (unsigned char *)&win, 1);
7371 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
7372 PropModeReplace, (unsigned char *)&win, 1);
7373 XChangeProperty(display, win, netwmname, utf8_string, 8,
7374 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
7379 main(int argc, char *argv[])
7381 struct swm_region *r, *rr;
7382 struct ws_win *winfocus = NULL;
7385 char conf[PATH_MAX], *cfile = NULL;
7390 struct sigaction sact;
7393 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7394 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7396 warnx("no locale support");
7398 if (!X_HAVE_UTF8_STRING)
7399 warnx("no UTF-8 support");
7401 if (!(display = XOpenDisplay(0)))
7402 errx(1, "can not open display");
7405 errx(1, "other wm running");
7407 /* handle some signals */
7408 bzero(&sact, sizeof(sact));
7409 sigemptyset(&sact.sa_mask);
7411 sact.sa_handler = sighdlr;
7412 sigaction(SIGINT, &sact, NULL);
7413 sigaction(SIGQUIT, &sact, NULL);
7414 sigaction(SIGTERM, &sact, NULL);
7415 sigaction(SIGHUP, &sact, NULL);
7417 sact.sa_handler = sighdlr;
7418 sact.sa_flags = SA_NOCLDSTOP;
7419 sigaction(SIGCHLD, &sact, NULL);
7421 astate = XInternAtom(display, "WM_STATE", False);
7422 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
7423 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
7424 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
7425 a_wmname = XInternAtom(display, "WM_NAME", False);
7426 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7427 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
7428 a_string = XInternAtom(display, "STRING", False);
7429 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
7431 /* look for local and global conf file */
7432 pwd = getpwuid(getuid());
7434 errx(1, "invalid user: %d", getuid());
7443 for (i = 0; ; i++) {
7448 snprintf(conf, sizeof conf, "%s/.%s",
7449 pwd->pw_dir, SWM_CONF_FILE);
7453 snprintf(conf, sizeof conf, "/etc/%s",
7458 snprintf(conf, sizeof conf, "%s/.%s",
7459 pwd->pw_dir, SWM_CONF_FILE_OLD);
7463 snprintf(conf, sizeof conf, "/etc/%s",
7470 if (strlen(conf) && stat(conf, &sb) != -1)
7471 if (S_ISREG(sb.st_mode)) {
7478 /* load conf (if any) */
7480 conf_load(cfile, SWM_CONF_DEFAULT);
7483 /* set some values to work around bad programs */
7485 /* grab existing windows (before we build the bars) */
7488 if (getenv("SWM_STARTED") == NULL)
7489 setenv("SWM_STARTED", "YES", 1);
7491 /* setup all bars */
7492 for (i = 0; i < ScreenCount(display); i++)
7493 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7494 if (winfocus == NULL)
7495 winfocus = TAILQ_FIRST(&r->ws->winlist);
7503 if (focus_mode == SWM_FOCUS_DEFAULT)
7504 drain_enter_notify();
7506 xfd = ConnectionNumber(display);
7508 while (XPending(display)) {
7509 XNextEvent(display, &e);
7512 if (e.type < LASTEvent) {
7513 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7514 "window: 0x%lx, type: %s (%d), %d remaining"
7515 "\n", YESNO(handler[e.type]),
7516 e.xany.window, geteventname(&e),
7517 e.type, QLength(display));
7519 if (handler[e.type])
7520 handler[e.type](&e);
7522 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7523 "0x%lx, type: %s (%d)\n", e.xany.window,
7524 xrandr_geteventname(&e), e.type);
7526 switch (e.type - xrandr_eventbase) {
7527 case RRScreenChangeNotify:
7536 /* if we are being restarted go focus on first window */
7538 rr = winfocus->ws->r;
7540 /* not a visible window */
7544 /* move pointer to first screen if multi screen */
7545 if (ScreenCount(display) > 1 || outputs > 1)
7546 XWarpPointer(display, None, rr->s[0].root,
7548 Y(rr) + (bar_enabled ? bar_height : 0));
7550 a.id = SWM_ARG_ID_FOCUSCUR;
7560 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7562 DNPRINTF(SWM_D_MISC, "select failed");
7563 if (restart_wm == 1)
7564 restart(NULL, NULL);
7565 if (search_resp == 1)
7578 for (i = 0; i < ScreenCount(display); ++i)
7579 if (screens[i].bar_gc != NULL)
7580 XFreeGC(display, screens[i].bar_gc);
7582 XFreeFontSet(display, bar_fs);
7583 XCloseDisplay(display);