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 SH_MIN(w) (w)->sh_mask & PMinSize
177 #define SH_MIN_W(w) (w)->sh.min_width
178 #define SH_MIN_H(w) (w)->sh.min_height
179 #define SH_MAX(w) (w)->sh_mask & PMaxSize
180 #define SH_MAX_W(w) (w)->sh.max_width
181 #define SH_MAX_H(w) (w)->sh.max_height
182 #define SH_INC(w) (w)->sh_mask & PResizeInc
183 #define SH_INC_W(w) (w)->sh.width_inc
184 #define SH_INC_H(w) (w)->sh.height_inc
185 #define SWM_MAX_FONT_STEPS (3)
186 #define WINID(w) ((w) ? (w)->id : 0)
187 #define YESNO(x) ((x) ? "yes" : "no")
189 #define SWM_FOCUS_DEFAULT (0)
190 #define SWM_FOCUS_SYNERGY (1)
191 #define SWM_FOCUS_FOLLOW (2)
193 #define SWM_CONF_DEFAULT (0)
194 #define SWM_CONF_KEYMAPPING (1)
197 #define SWM_LIB "/usr/local/lib/libswmhack.so"
210 volatile sig_atomic_t running = 1;
211 volatile sig_atomic_t restart_wm = 0;
213 int last_focus_event = FocusOut;
214 int (*xerrorxlib)(Display *, XErrorEvent *);
218 int xrandr_eventbase;
219 unsigned int numlockmask = 0;
223 int cycle_visible = 0;
225 int font_adjusted = 0;
226 unsigned int mod_key = MODKEY;
229 struct swm_region *search_r;
230 int select_list_pipe[2];
231 int select_resp_pipe[2];
233 volatile sig_atomic_t search_resp;
234 int search_resp_action;
236 struct search_window {
237 TAILQ_ENTRY(search_window) entry;
243 TAILQ_HEAD(search_winlist, search_window);
244 struct search_winlist search_wl;
249 SWM_SEARCH_UNICONIFY,
250 SWM_SEARCH_NAME_WORKSPACE,
251 SWM_SEARCH_SEARCH_WORKSPACE,
252 SWM_SEARCH_SEARCH_WINDOW
255 #define SWM_STACK_TOP (0)
256 #define SWM_STACK_BOTTOM (1)
257 #define SWM_STACK_ABOVE (2)
258 #define SWM_STACK_BELOW (3)
261 double dialog_ratio = 0.6;
263 #define SWM_BAR_MAX (256)
264 #define SWM_BAR_JUSTIFY_LEFT (0)
265 #define SWM_BAR_JUSTIFY_CENTER (1)
266 #define SWM_BAR_JUSTIFY_RIGHT (2)
267 #define SWM_BAR_OFFSET (4)
268 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
269 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
270 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
271 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
272 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
274 #ifdef X_HAVE_UTF8_STRING
275 #define DRAWSTRING(x...) Xutf8DrawString(x)
277 #define DRAWSTRING(x...) XmbDrawString(x)
280 char *bar_argv[] = { NULL, NULL };
282 unsigned char bar_ext[SWM_BAR_MAX];
283 char bar_vertext[SWM_BAR_MAX];
285 sig_atomic_t bar_alarm = 0;
288 int bar_border_width = 1;
289 int bar_at_bottom = 0;
291 int bar_extra_running = 0;
294 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
295 char *bar_format = NULL;
296 int stack_enabled = 1;
297 int clock_enabled = 1;
298 int urgent_enabled = 0;
299 char *clock_format = NULL;
300 int title_name_enabled = 0;
301 int title_class_enabled = 0;
302 int window_name_enabled = 0;
303 int focus_mode = SWM_FOCUS_DEFAULT;
304 int focus_close = SWM_STACK_BELOW;
305 int focus_close_wrap = 1;
306 int focus_default = SWM_STACK_TOP;
307 int spawn_position = SWM_STACK_TOP;
308 int disable_border = 0;
309 int border_width = 1;
310 int verbose_layout = 0;
313 XFontSetExtents *bar_fs_extents;
317 #define SWM_MENU_FN (2)
318 #define SWM_MENU_NB (4)
319 #define SWM_MENU_NF (6)
320 #define SWM_MENU_SB (8)
321 #define SWM_MENU_SF (10)
323 /* layout manager data */
324 struct swm_geometry {
334 /* virtual "screens" */
336 TAILQ_ENTRY(swm_region) entry;
337 struct swm_geometry g;
338 struct workspace *ws; /* current workspace on this region */
339 struct workspace *ws_prior; /* prior workspace on this region */
340 struct swm_screen *s; /* screen idx */
343 TAILQ_HEAD(swm_region_list, swm_region);
346 TAILQ_ENTRY(ws_win) entry;
349 struct ws_win *child_trans; /* transient child window */
350 struct swm_geometry g; /* current geometry */
351 struct swm_geometry g_float; /* geometry when floating */
352 struct swm_geometry rg_float; /* region geom when floating */
353 int g_floatvalid; /* g_float geometry validity */
354 int floatmaxed; /* whether maxed by max_stack */
358 unsigned int ewmh_flags;
359 int font_size_boundary[SWM_MAX_FONT_STEPS];
365 unsigned long quirks;
366 struct workspace *ws; /* always valid */
367 struct swm_screen *s; /* always valid, never changes */
368 XWindowAttributes wa;
374 TAILQ_HEAD(ws_win_list, ws_win);
378 TAILQ_ENTRY(pid_e) entry;
382 TAILQ_HEAD(pid_list, pid_e);
383 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
385 /* layout handlers */
387 void vertical_config(struct workspace *, int);
388 void vertical_stack(struct workspace *, struct swm_geometry *);
389 void horizontal_config(struct workspace *, int);
390 void horizontal_stack(struct workspace *, struct swm_geometry *);
391 void max_stack(struct workspace *, struct swm_geometry *);
392 void plain_stacker(struct workspace *);
393 void fancy_stacker(struct workspace *);
395 struct ws_win *find_window(Window);
397 void grabbuttons(struct ws_win *, int);
398 void new_region(struct swm_screen *, int, int, int, int);
399 void unmanage_window(struct ws_win *);
400 long getstate(Window);
402 int conf_load(char *, int);
405 void (*l_stack)(struct workspace *, struct swm_geometry *);
406 void (*l_config)(struct workspace *, int);
408 #define SWM_L_FOCUSPREV (1<<0)
409 #define SWM_L_MAPONFOCUS (1<<1)
410 void (*l_string)(struct workspace *);
412 /* stack, configure */
413 { vertical_stack, vertical_config, 0, plain_stacker },
414 { horizontal_stack, horizontal_config, 0, plain_stacker },
416 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
417 { NULL, NULL, 0, NULL },
420 /* position of max_stack mode in the layouts array, index into layouts! */
421 #define SWM_V_STACK (0)
422 #define SWM_H_STACK (1)
423 #define SWM_MAX_STACK (2)
425 #define SWM_H_SLICE (32)
426 #define SWM_V_SLICE (32)
428 /* define work spaces */
430 int idx; /* workspace index */
431 char *name; /* workspace name */
432 int always_raise; /* raise windows on focus */
433 struct layout *cur_layout; /* current layout handlers */
434 struct ws_win *focus; /* may be NULL */
435 struct ws_win *focus_prev; /* may be NULL */
436 struct swm_region *r; /* may be NULL */
437 struct swm_region *old_r; /* may be NULL */
438 struct ws_win_list winlist; /* list of windows in ws */
439 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
440 char stacker[10]; /* display stacker and layout */
444 int horizontal_msize;
446 int horizontal_stacks;
457 SWM_S_COLOR_BAR_BORDER,
458 SWM_S_COLOR_BAR_FONT,
464 /* physical screen mapping */
465 #define SWM_WS_MAX (22) /* hard limit */
466 int workspace_limit = 10; /* soft limit */
469 int idx; /* screen index */
470 struct swm_region_list rl; /* list of regions on this screen */
471 struct swm_region_list orl; /* list of old regions */
473 struct workspace ws[SWM_WS_MAX];
479 } c[SWM_S_COLOR_MAX];
483 struct swm_screen *screens;
485 /* args to functions */
488 #define SWM_ARG_ID_FOCUSNEXT (0)
489 #define SWM_ARG_ID_FOCUSPREV (1)
490 #define SWM_ARG_ID_FOCUSMAIN (2)
491 #define SWM_ARG_ID_FOCUSCUR (4)
492 #define SWM_ARG_ID_SWAPNEXT (10)
493 #define SWM_ARG_ID_SWAPPREV (11)
494 #define SWM_ARG_ID_SWAPMAIN (12)
495 #define SWM_ARG_ID_MOVELAST (13)
496 #define SWM_ARG_ID_MASTERSHRINK (20)
497 #define SWM_ARG_ID_MASTERGROW (21)
498 #define SWM_ARG_ID_MASTERADD (22)
499 #define SWM_ARG_ID_MASTERDEL (23)
500 #define SWM_ARG_ID_FLIPLAYOUT (24)
501 #define SWM_ARG_ID_STACKRESET (30)
502 #define SWM_ARG_ID_STACKINIT (31)
503 #define SWM_ARG_ID_CYCLEWS_UP (40)
504 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
505 #define SWM_ARG_ID_CYCLESC_UP (42)
506 #define SWM_ARG_ID_CYCLESC_DOWN (43)
507 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
508 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
509 #define SWM_ARG_ID_STACKINC (50)
510 #define SWM_ARG_ID_STACKDEC (51)
511 #define SWM_ARG_ID_SS_ALL (60)
512 #define SWM_ARG_ID_SS_WINDOW (61)
513 #define SWM_ARG_ID_DONTCENTER (70)
514 #define SWM_ARG_ID_CENTER (71)
515 #define SWM_ARG_ID_KILLWINDOW (80)
516 #define SWM_ARG_ID_DELETEWINDOW (81)
517 #define SWM_ARG_ID_WIDTHGROW (90)
518 #define SWM_ARG_ID_WIDTHSHRINK (91)
519 #define SWM_ARG_ID_HEIGHTGROW (92)
520 #define SWM_ARG_ID_HEIGHTSHRINK (93)
521 #define SWM_ARG_ID_MOVEUP (100)
522 #define SWM_ARG_ID_MOVEDOWN (101)
523 #define SWM_ARG_ID_MOVELEFT (102)
524 #define SWM_ARG_ID_MOVERIGHT (103)
528 void focus(struct swm_region *, union arg *);
529 void focus_magic(struct ws_win *);
533 TAILQ_ENTRY(quirk) entry;
537 #define SWM_Q_FLOAT (1<<0) /* float this window */
538 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
539 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
540 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
541 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
542 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
544 TAILQ_HEAD(quirk_list, quirk);
545 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
548 * Supported EWMH hints should be added to
549 * both the enum and the ewmh array
554 _NET_MOVERESIZE_WINDOW,
555 _NET_WM_ACTION_CLOSE,
556 _NET_WM_ACTION_FULLSCREEN,
558 _NET_WM_ACTION_RESIZE,
559 _NET_WM_ALLOWED_ACTIONS,
562 _NET_WM_STATE_FULLSCREEN,
563 _NET_WM_STATE_HIDDEN,
564 _NET_WM_STATE_MAXIMIZED_HORZ,
565 _NET_WM_STATE_MAXIMIZED_VERT,
566 _NET_WM_STATE_SKIP_PAGER,
567 _NET_WM_STATE_SKIP_TASKBAR,
569 _NET_WM_WINDOW_TYPE_DIALOG,
570 _NET_WM_WINDOW_TYPE_DOCK,
571 _NET_WM_WINDOW_TYPE_NORMAL,
572 _NET_WM_WINDOW_TYPE_SPLASH,
573 _NET_WM_WINDOW_TYPE_TOOLBAR,
574 _NET_WM_WINDOW_TYPE_UTILITY,
575 _SWM_WM_STATE_MANUAL,
582 } ewmh[SWM_EWMH_HINT_MAX] = {
583 /* must be in same order as in the enum */
584 {"_NET_ACTIVE_WINDOW", None},
585 {"_NET_CLOSE_WINDOW", None},
586 {"_NET_MOVERESIZE_WINDOW", None},
587 {"_NET_WM_ACTION_CLOSE", None},
588 {"_NET_WM_ACTION_FULLSCREEN", None},
589 {"_NET_WM_ACTION_MOVE", None},
590 {"_NET_WM_ACTION_RESIZE", None},
591 {"_NET_WM_ALLOWED_ACTIONS", None},
592 {"_NET_WM_STATE", None},
593 {"_NET_WM_STATE_ABOVE", None},
594 {"_NET_WM_STATE_FULLSCREEN", None},
595 {"_NET_WM_STATE_HIDDEN", None},
596 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
597 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
598 {"_NET_WM_STATE_SKIP_PAGER", None},
599 {"_NET_WM_STATE_SKIP_TASKBAR", None},
600 {"_NET_WM_WINDOW_TYPE", None},
601 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
602 {"_NET_WM_WINDOW_TYPE_DOCK", None},
603 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
604 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
605 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
606 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
607 {"_SWM_WM_STATE_MANUAL", None},
610 void store_float_geom(struct ws_win *, struct swm_region *);
611 int floating_toggle_win(struct ws_win *);
612 void spawn_select(struct swm_region *, union arg *, char *, int *);
613 unsigned char *get_win_name(Window);
616 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
617 unsigned long *nbytes, unsigned char **data)
620 unsigned long *nbytes_ret, *nitems_ret;
621 unsigned long nbytes_tmp, nitems_tmp;
624 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
625 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
627 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
628 &real, &format, nitems_ret, nbytes_ret, data);
630 if (status != Success)
639 update_iconic(struct ws_win *win, int newv)
646 iprop = XInternAtom(display, "_SWM_ICONIC", False);
650 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
651 PropModeReplace, (unsigned char *)&v, 1);
653 XDeleteProperty(display, win->id, iprop);
657 get_iconic(struct ws_win *win)
662 unsigned long nitems, extra;
663 unsigned char *prop = NULL;
665 iprop = XInternAtom(display, "_SWM_ICONIC", False);
668 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
669 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
670 if (status != Success)
672 if (rettype != XA_INTEGER || retfmt != 32)
676 v = *((int32_t *)prop);
690 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
692 for (i = 0; i < LENGTH(ewmh); i++)
693 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
695 for (i = 0; i < ScreenCount(display); i++) {
696 /* Support check window will be created by workaround(). */
698 /* Report supported atoms */
699 XDeleteProperty(display, screens[i].root, sup_list);
700 for (j = 0; j < LENGTH(ewmh); j++)
701 XChangeProperty(display, screens[i].root,
702 sup_list, XA_ATOM, 32,
703 PropModeAppend, (unsigned char *)&ewmh[j].atom, 1);
711 unsigned char *data = NULL;
713 Atom sup_check, sup_list;
716 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
717 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
719 for (i = 0; i < ScreenCount(display); i++) {
720 /* Get the support check window and destroy it */
721 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
726 XDestroyWindow(display, id);
727 XDeleteProperty(display, screens[i].root, sup_check);
728 XDeleteProperty(display, screens[i].root, sup_list);
736 ewmh_autoquirk(struct ws_win *win)
739 unsigned long *data = NULL, n;
742 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
743 XA_ATOM, &n, NULL, (void *)&data);
750 for (i = 0; i < n; i++) {
752 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
754 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
755 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
756 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
758 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
761 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
762 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
764 win->quirks = SWM_Q_FLOAT;
772 #define SWM_EWMH_ACTION_COUNT_MAX (6)
773 #define EWMH_F_FULLSCREEN (1<<0)
774 #define EWMH_F_ABOVE (1<<1)
775 #define EWMH_F_HIDDEN (1<<2)
776 #define EWMH_F_SKIP_PAGER (1<<3)
777 #define EWMH_F_SKIP_TASKBAR (1<<4)
778 #define SWM_F_MANUAL (1<<5)
781 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
783 struct swm_geometry rg;
791 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
792 "fullscreen %s\n", win->id, YESNO(fs));
797 store_float_geom(win, win->ws->r);
801 if (win->g_floatvalid) {
802 /* refloat at last floating relative position */
803 X(win) = win->g_float.x - win->rg_float.x + rg.x;
804 Y(win) = win->g_float.y - win->rg_float.y + rg.y;
805 WIDTH(win) = win->g_float.w;
806 HEIGHT(win) = win->g_float.h;
814 ewmh_update_actions(struct ws_win *win)
816 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
822 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
825 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
826 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
829 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
830 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
833 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
834 #define _NET_WM_STATE_ADD 1 /* add/set property */
835 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
838 ewmh_update_win_state(struct ws_win *win, long state, long action)
840 unsigned int mask = 0;
841 unsigned int changed = 0;
842 unsigned int orig_flags;
847 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
848 mask = EWMH_F_FULLSCREEN;
849 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
851 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
853 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
854 mask = EWMH_F_SKIP_PAGER;
855 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
856 mask = EWMH_F_SKIP_TASKBAR;
859 orig_flags = win->ewmh_flags;
862 case _NET_WM_STATE_REMOVE:
863 win->ewmh_flags &= ~mask;
865 case _NET_WM_STATE_ADD:
866 win->ewmh_flags |= mask;
868 case _NET_WM_STATE_TOGGLE:
869 win->ewmh_flags ^= mask;
873 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
875 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
877 if (!floating_toggle_win(win))
878 win->ewmh_flags = orig_flags; /* revert */
879 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
881 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
882 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
884 if (!ewmh_set_win_fullscreen(win,
885 win->ewmh_flags & EWMH_F_FULLSCREEN))
886 win->ewmh_flags = orig_flags; /* revert */
888 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
890 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
891 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
892 XA_ATOM, 32, PropModeAppend,
893 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
894 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
895 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
896 XA_ATOM, 32, PropModeAppend,
897 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
898 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
899 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
900 XA_ATOM, 32, PropModeAppend,
901 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
902 if (win->ewmh_flags & EWMH_F_ABOVE)
903 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
904 XA_ATOM, 32, PropModeAppend,
905 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
906 if (win->ewmh_flags & SWM_F_MANUAL)
907 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
908 XA_ATOM, 32, PropModeAppend,
909 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
913 ewmh_get_win_state(struct ws_win *win)
924 win->ewmh_flags |= EWMH_F_ABOVE;
926 win->ewmh_flags |= SWM_F_MANUAL;
928 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
929 (~0L), XA_ATOM, &n, NULL, (void *)&states);
934 for (i = 0; i < n; i++)
935 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
943 geteventname(XEvent *e)
955 name = "ButtonPress";
958 name = "ButtonRelease";
961 name = "MotionNotify";
964 name = "EnterNotify";
967 name = "LeaveNotify";
976 name = "KeymapNotify";
982 name = "GraphicsExpose";
987 case VisibilityNotify:
988 name = "VisibilityNotify";
991 name = "CreateNotify";
994 name = "DestroyNotify";
997 name = "UnmapNotify";
1003 name = "MapRequest";
1005 case ReparentNotify:
1006 name = "ReparentNotify";
1008 case ConfigureNotify:
1009 name = "ConfigureNotify";
1011 case ConfigureRequest:
1012 name = "ConfigureRequest";
1015 name = "GravityNotify";
1018 name = "ResizeRequest";
1020 case CirculateNotify:
1021 name = "CirculateNotify";
1023 case CirculateRequest:
1024 name = "CirculateRequest";
1026 case PropertyNotify:
1027 name = "PropertyNotify";
1029 case SelectionClear:
1030 name = "SelectionClear";
1032 case SelectionRequest:
1033 name = "SelectionRequest";
1035 case SelectionNotify:
1036 name = "SelectionNotify";
1038 case ColormapNotify:
1039 name = "ColormapNotify";
1042 name = "ClientMessage";
1045 name = "MappingNotify";
1055 xrandr_geteventname(XEvent *e)
1059 switch(e->type - xrandr_eventbase) {
1060 case RRScreenChangeNotify:
1061 name = "RRScreenChangeNotify";
1071 dumpwins(struct swm_region *r, union arg *args)
1075 XWindowAttributes wa;
1077 if (r->ws == NULL) {
1078 warnx("dumpwins: invalid workspace");
1082 warnx("=== managed window list ws %02d ===", r->ws->idx);
1084 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1085 state = getstate(win->id);
1086 if (!XGetWindowAttributes(display, win->id, &wa))
1087 warnx("window: 0x%lx, failed XGetWindowAttributes",
1089 warnx("window: 0x%lx, map_state: %d, state: %d, "
1090 "transient: 0x%lx", win->id, wa.map_state, state,
1094 warnx("===== unmanaged window list =====");
1095 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1096 state = getstate(win->id);
1097 if (!XGetWindowAttributes(display, win->id, &wa))
1098 warnx("window: 0x%lx, failed XGetWindowAttributes",
1100 warnx("window: 0x%lx, map_state: %d, state: %d, "
1101 "transient: 0x%lx", win->id, wa.map_state, state,
1105 warnx("=================================");
1109 dumpwins(struct swm_region *r, union arg *args)
1112 #endif /* SWM_DEBUG */
1114 void expose(XEvent *);
1115 void keypress(XEvent *);
1116 void buttonpress(XEvent *);
1117 void configurerequest(XEvent *);
1118 void configurenotify(XEvent *);
1119 void destroynotify(XEvent *);
1120 void enternotify(XEvent *);
1121 void focusevent(XEvent *);
1122 void mapnotify(XEvent *);
1123 void mappingnotify(XEvent *);
1124 void maprequest(XEvent *);
1125 void propertynotify(XEvent *);
1126 void unmapnotify(XEvent *);
1127 void visibilitynotify(XEvent *);
1128 void clientmessage(XEvent *);
1130 void (*handler[LASTEvent])(XEvent *) = {
1132 [KeyPress] = keypress,
1133 [ButtonPress] = buttonpress,
1134 [ConfigureRequest] = configurerequest,
1135 [ConfigureNotify] = configurenotify,
1136 [DestroyNotify] = destroynotify,
1137 [EnterNotify] = enternotify,
1138 [FocusIn] = focusevent,
1139 [FocusOut] = focusevent,
1140 [MapNotify] = mapnotify,
1141 [MappingNotify] = mappingnotify,
1142 [MapRequest] = maprequest,
1143 [PropertyNotify] = propertynotify,
1144 [UnmapNotify] = unmapnotify,
1145 [VisibilityNotify] = visibilitynotify,
1146 [ClientMessage] = clientmessage,
1152 int saved_errno, status;
1155 saved_errno = errno;
1159 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1164 if (errno != ECHILD)
1165 warn("sighdlr: waitpid");
1166 #endif /* SWM_DEBUG */
1169 if (pid == searchpid)
1173 if (WIFEXITED(status)) {
1174 if (WEXITSTATUS(status) != 0)
1175 warnx("sighdlr: child exit status: %d",
1176 WEXITSTATUS(status));
1178 warnx("sighdlr: child is terminated "
1180 #endif /* SWM_DEBUG */
1194 errno = saved_errno;
1200 struct pid_e *p = NULL;
1202 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1207 TAILQ_FOREACH(p, &pidlist, entry) {
1216 name_to_color(char *colorname)
1220 XColor screen_def, exact_def;
1221 unsigned long result = 0;
1222 char cname[32] = "#";
1224 cmap = DefaultColormap(display, screens[0].idx);
1225 status = XAllocNamedColor(display, cmap, colorname,
1226 &screen_def, &exact_def);
1228 strlcat(cname, colorname + 2, sizeof cname - 1);
1229 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1233 result = screen_def.pixel;
1235 warnx("color '%s' not found", colorname);
1241 setscreencolor(char *val, int i, int c)
1243 if (i > 0 && i <= ScreenCount(display)) {
1244 screens[i - 1].c[c].color = name_to_color(val);
1245 free(screens[i - 1].c[c].name);
1246 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1248 } else if (i == -1) {
1249 for (i = 0; i < ScreenCount(display); i++) {
1250 screens[i].c[c].color = name_to_color(val);
1251 free(screens[i].c[c].name);
1252 if ((screens[i].c[c].name = strdup(val)) == NULL)
1256 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1257 i, ScreenCount(display));
1261 fancy_stacker(struct workspace *ws)
1263 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1264 if (ws->cur_layout->l_stack == vertical_stack)
1265 snprintf(ws->stacker, sizeof ws->stacker,
1266 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1267 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1268 if (ws->cur_layout->l_stack == horizontal_stack)
1269 snprintf(ws->stacker, sizeof ws->stacker,
1270 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1271 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1275 plain_stacker(struct workspace *ws)
1277 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1278 if (ws->cur_layout->l_stack == vertical_stack)
1279 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1280 sizeof ws->stacker);
1281 if (ws->cur_layout->l_stack == horizontal_stack)
1282 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1283 sizeof ws->stacker);
1287 custom_region(char *val)
1289 unsigned int sidx, x, y, w, h;
1291 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1292 errx(1, "invalid custom region, "
1293 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1294 if (sidx < 1 || sidx > ScreenCount(display))
1295 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1296 sidx, ScreenCount(display));
1300 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1302 if (x > DisplayWidth(display, sidx) ||
1303 y > DisplayHeight(display, sidx) ||
1304 w + x > DisplayWidth(display, sidx) ||
1305 h + y > DisplayHeight(display, sidx)) {
1306 warnx("ignoring region %ux%u+%u+%u - not within screen "
1307 "boundaries (%ux%u)", w, h, x, y,
1308 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1312 new_region(&screens[sidx], x, y, w, h);
1316 socket_setnonblock(int fd)
1320 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1321 err(1, "fcntl F_GETFL");
1322 flags |= O_NONBLOCK;
1323 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1324 err(1, "fcntl F_SETFL");
1328 bar_print(struct swm_region *r, const char *s)
1332 XRectangle ibox, lbox;
1334 XClearWindow(display, r->bar_window);
1337 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1339 switch (bar_justify) {
1340 case SWM_BAR_JUSTIFY_LEFT:
1343 case SWM_BAR_JUSTIFY_CENTER:
1344 x = (WIDTH(r) - lbox.width) / 2;
1346 case SWM_BAR_JUSTIFY_RIGHT:
1347 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1351 if (x < SWM_BAR_OFFSET)
1354 DRAWSTRING(display, r->bar_window, bar_fs, r->s->bar_gc,
1355 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1360 bar_extra_stop(void)
1364 bzero(bar_pipe, sizeof bar_pipe);
1367 kill(bar_pid, SIGTERM);
1370 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1375 bar_class_name(char *s, size_t sz, struct swm_region *r)
1377 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1379 if (r->ws->focus->ch.res_class != NULL)
1380 strlcat(s, r->ws->focus->ch.res_class, sz);
1384 bar_title_name(char *s, size_t sz, struct swm_region *r)
1386 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1388 if (r->ws->focus->ch.res_name != NULL)
1389 strlcat(s, r->ws->focus->ch.res_name, sz);
1393 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1395 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1398 bar_class_name(s, sz, r);
1399 strlcat(s, ":", sz);
1400 bar_title_name(s, sz, r);
1404 bar_window_float(char *s, size_t sz, struct swm_region *r)
1406 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1408 if (r->ws->focus->floating)
1409 strlcat(s, "(f)", sz);
1413 bar_window_name(char *s, size_t sz, struct swm_region *r)
1415 unsigned char *title;
1417 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1419 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1422 strlcat(s, (char *)title, sz);
1426 int urgent[SWM_WS_MAX];
1428 bar_urgent(char *s, size_t sz)
1430 XWMHints *wmh = NULL;
1435 for (i = 0; i < workspace_limit; i++)
1438 for (i = 0; i < ScreenCount(display); i++)
1439 for (j = 0; j < workspace_limit; j++)
1440 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1441 wmh = XGetWMHints(display, win->id);
1445 if (wmh->flags & XUrgencyHint)
1450 for (i = 0; i < workspace_limit; i++) {
1452 snprintf(b, sizeof b, "%d ", i + 1);
1454 snprintf(b, sizeof b, "- ");
1460 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1462 if (r == NULL || r->ws == NULL)
1464 if (r->ws->name != NULL)
1465 strlcat(s, r->ws->name, sz);
1468 /* build the default bar format according to the defined enabled options */
1470 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1472 /* if format provided, just copy the buffers */
1473 if (bar_format != NULL) {
1474 strlcpy(fmtnew, fmtexp, sz);
1478 /* reset the output buffer */
1481 strlcat(fmtnew, "+N:+I ", sz);
1483 strlcat(fmtnew, "+S", sz);
1484 strlcat(fmtnew, " ", sz);
1486 /* only show the workspace name if there's actually one */
1487 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1488 strlcat(fmtnew, "<+D>", sz);
1489 strlcat(fmtnew, "+3<", sz);
1491 if (clock_enabled) {
1492 strlcat(fmtnew, fmtexp, sz);
1493 strlcat(fmtnew, "+4<", sz);
1496 /* bar_urgent already adds the space before the last asterisk */
1498 strlcat(fmtnew, "* +U*+4<", sz);
1500 if (title_class_enabled) {
1501 strlcat(fmtnew, "+C", sz);
1502 if (title_name_enabled == 0)
1503 strlcat(fmtnew, "+4<", sz);
1506 /* checks needed by the colon and floating strlcat(3) calls below */
1507 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1508 if (title_name_enabled) {
1509 if (title_class_enabled)
1510 strlcat(fmtnew, ":", sz);
1511 strlcat(fmtnew, "+T+4<", sz);
1513 if (window_name_enabled) {
1514 if (r->ws->focus->floating)
1515 strlcat(fmtnew, "+F ", sz);
1516 strlcat(fmtnew, "+64W ", sz);
1520 /* finally add the action script output and the version */
1521 strlcat(fmtnew, "+4<+A+4<+V", sz);
1524 /* replaces the bar format character sequences (like in tmux(1)) */
1526 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1530 char num[8], tmp[SWM_BAR_MAX];
1532 size_t len, numoff = 0;
1534 /* reset strlcat(3) buffer */
1537 /* get number, if any */
1539 while (*fmt != '\0' && isdigit((unsigned char) *fmt)) {
1540 if (numoff >= sizeof num - 1)
1542 num[numoff++] = *fmt++;
1546 if ((limit = strtonum(num, 1, sizeof tmp - 1, NULL)) == 0)
1547 limit = sizeof tmp - 1;
1549 /* if number is too big, skip to the first non-digit */
1550 if (numoff >= sizeof num - 1) {
1551 while (*fmt != '\0' && isdigit((unsigned char) *fmt))
1554 /* there is nothing to replace (ie EOL) */
1560 /* special case; no limit given, pad one space, instead */
1561 if (limit == sizeof tmp - 1)
1563 snprintf(tmp, sizeof tmp, "%*s", limit, " ");
1566 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1569 bar_class_name(tmp, sizeof tmp, r);
1572 bar_workspace_name(tmp, sizeof tmp, r);
1575 bar_window_float(tmp, sizeof tmp, r);
1578 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1581 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1584 bar_class_title_name(tmp, sizeof tmp, r);
1587 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1590 bar_title_name(tmp, sizeof tmp, r);
1593 bar_urgent(tmp, sizeof tmp);
1596 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1599 bar_window_name(tmp, sizeof tmp, r);
1602 /* unknown character sequence; copy as-is */
1603 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1611 while (limit-- > 0) {
1612 if (*offrep >= sz - 1)
1614 fmtrep[(*offrep)++] = *ptr++;
1622 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1627 while (*fmt != '\0') {
1629 /* skip ordinary characters */
1632 fmtrep[off++] = *fmt++;
1636 /* character sequence found; replace it */
1637 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1646 bar_fmt_expand(char *fmtexp, size_t sz)
1653 /* start by grabbing the current time and date */
1655 localtime_r(&tmt, &tm);
1657 /* figure out what to expand */
1658 if (bar_format != NULL)
1660 else if (bar_format == NULL && clock_enabled)
1662 /* if nothing to expand bail out */
1668 /* copy as-is, just in case the format shouldn't be expanded below */
1669 strlcpy(fmtexp, fmt, sz);
1670 /* finally pass the string through strftime(3) */
1671 #ifndef SWM_DENY_CLOCK_FORMAT
1672 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1673 warnx("format too long");
1681 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1682 char fmtrep[SWM_BAR_MAX];
1684 struct swm_region *r;
1686 /* expand the format by first passing it through strftime(3) */
1687 bar_fmt_expand(fmtexp, sizeof fmtexp);
1689 for (i = 0; i < ScreenCount(display); i++) {
1690 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1691 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1692 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1693 bar_print(r, fmtrep);
1704 if (bar_enabled == 0)
1706 if (bar_extra && bar_extra_running) {
1707 /* ignore short reads; it'll correct itself */
1708 while ((b = fgetln(stdin, &len)) != NULL)
1709 if (b && b[len - 1] == '\n') {
1711 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1713 if (b == NULL && errno != EAGAIN) {
1714 warn("bar_update: bar_extra failed");
1718 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1731 bar_toggle(struct swm_region *r, union arg *args)
1733 struct swm_region *tmpr;
1734 int i, sc = ScreenCount(display);
1736 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1739 for (i = 0; i < sc; i++)
1740 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1741 XUnmapWindow(display, tmpr->bar_window);
1743 for (i = 0; i < sc; i++)
1744 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1745 XMapRaised(display, tmpr->bar_window);
1747 bar_enabled = !bar_enabled;
1750 /* must be after stack */
1757 XSetWindowAttributes wa;
1758 struct swm_region *r;
1761 /* do this here because the conf file is in memory */
1762 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1763 /* launch external status app */
1764 bar_extra_running = 1;
1765 if (pipe(bar_pipe) == -1)
1766 err(1, "pipe error");
1767 socket_setnonblock(bar_pipe[0]);
1768 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1769 if (dup2(bar_pipe[0], 0) == -1)
1771 if (dup2(bar_pipe[1], 1) == -1)
1773 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1774 err(1, "could not disable SIGPIPE");
1775 switch (bar_pid = fork()) {
1777 err(1, "cannot fork");
1781 execvp(bar_argv[0], bar_argv);
1782 err(1, "%s external app failed", bar_argv[0]);
1784 default: /* parent */
1790 bzero(&wa, sizeof wa);
1791 for (i = 0; i < ScreenCount(display); i++)
1792 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1794 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1795 wa.background_pixel =
1796 screens[i].c[SWM_S_COLOR_BAR].color;
1797 XChangeWindowAttributes(display, r->bar_window,
1798 CWBackPixel | CWBorderPixel, &wa);
1804 bar_setup(struct swm_region *r)
1806 char *default_string;
1807 char **missing_charsets;
1808 int num_missing_charsets = 0;
1812 XFreeFontSet(display, bar_fs);
1817 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1819 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1820 &num_missing_charsets, &default_string);
1822 if (num_missing_charsets > 0) {
1823 warnx("Unable to load charset(s):");
1825 for (i = 0; i < num_missing_charsets; ++i)
1826 warnx("%s", missing_charsets[i]);
1828 XFreeStringList(missing_charsets);
1830 if (strcmp(default_string, ""))
1831 warnx("Glyphs from those sets will be replaced "
1832 "by '%s'.", default_string);
1834 warnx("Glyphs from those sets won't be drawn.");
1838 errx(1, "Error creating font set structure.");
1840 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1842 bar_height = bar_fs_extents->max_logical_extent.height +
1843 2 * bar_border_width;
1849 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1851 r->bar_window = XCreateSimpleWindow(display,
1852 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1853 bar_height - 2 * bar_border_width,
1854 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1855 r->s->c[SWM_S_COLOR_BAR].color);
1856 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1858 XMapRaised(display, r->bar_window);
1859 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1861 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1862 err(1, "could not install bar_signal");
1867 drain_enter_notify(void)
1872 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1875 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1879 set_win_state(struct ws_win *win, long state)
1881 long data[] = {state, None};
1883 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1888 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1889 (unsigned char *)data, 2);
1896 unsigned char *p = NULL;
1899 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1902 result = *((long *)p);
1908 version(struct swm_region *r, union arg *args)
1910 bar_version = !bar_version;
1912 snprintf(bar_vertext, sizeof bar_vertext,
1913 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1915 strlcpy(bar_vertext, "", sizeof bar_vertext);
1920 client_msg(struct ws_win *win, Atom a)
1922 XClientMessageEvent cm;
1927 bzero(&cm, sizeof cm);
1928 cm.type = ClientMessage;
1929 cm.window = win->id;
1930 cm.message_type = aprot;
1933 cm.data.l[1] = CurrentTime;
1934 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1937 /* synthetic response to a ConfigureRequest when not making a change */
1939 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1946 /* send notification of unchanged state. */
1947 ce.type = ConfigureNotify;
1950 ce.width = WIDTH(win);
1951 ce.height = HEIGHT(win);
1952 ce.override_redirect = False;
1956 ce.display = display;
1958 ce.window = win->id;
1959 ce.border_width = border_width;
1963 ce.display = ev->display;
1964 ce.event = ev->window;
1965 ce.window = ev->window;
1967 /* make response appear more WM_SIZE_HINTS-compliant */
1969 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1970 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1971 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1972 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1973 SH_INC_W(win), SH_INC_H(win));
1977 /* the hint may be set... to 0! */
1978 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1979 ce.width = SH_MIN_W(win);
1980 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1981 ce.height = SH_MIN_H(win);
1986 /* may also be advertized as 0 */
1987 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1988 ce.width = SH_MAX_W(win);
1989 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1990 ce.height = SH_MAX_H(win);
1993 /* resize increment. */
1995 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1996 ce.width -= (ce.width - SH_MIN_W(win)) %
1998 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1999 ce.height -= (ce.height - SH_MIN_H(win)) %
2003 /* adjust x and y for requested border_width. */
2004 ce.x += border_width - ev->border_width;
2005 ce.y += border_width - ev->border_width;
2006 ce.border_width = ev->border_width;
2007 ce.above = ev->above;
2010 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2011 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2012 ce.y, ce.width, ce.height, ce.border_width);
2014 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
2018 count_win(struct workspace *ws, int count_transient)
2023 TAILQ_FOREACH(win, &ws->winlist, entry) {
2024 if (count_transient == 0 && win->floating)
2026 if (count_transient == 0 && win->transient)
2032 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2038 quit(struct swm_region *r, union arg *args)
2040 DNPRINTF(SWM_D_MISC, "quit\n");
2045 unmap_window(struct ws_win *win)
2050 /* don't unmap again */
2051 if (getstate(win->id) == IconicState)
2054 set_win_state(win, IconicState);
2056 XUnmapWindow(display, win->id);
2057 XSetWindowBorder(display, win->id,
2058 win->s->c[SWM_S_COLOR_UNFOCUS].color);
2067 for (i = 0; i < ScreenCount(display); i++)
2068 for (j = 0; j < workspace_limit; j++)
2069 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2074 fake_keypress(struct ws_win *win, int keysym, int modifiers)
2081 event.display = display; /* Ignored, but what the hell */
2082 event.window = win->id;
2083 event.root = win->s->root;
2084 event.subwindow = None;
2085 event.time = CurrentTime;
2090 event.same_screen = True;
2091 event.keycode = XKeysymToKeycode(display, keysym);
2092 event.state = modifiers;
2094 event.type = KeyPress;
2095 XSendEvent(event.display, event.window, True,
2096 KeyPressMask, (XEvent *)&event);
2098 event.type = KeyRelease;
2099 XSendEvent(event.display, event.window, True,
2100 KeyPressMask, (XEvent *)&event);
2105 restart(struct swm_region *r, union arg *args)
2107 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2109 /* disable alarm because the following code may not be interrupted */
2111 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2112 err(1, "can't disable alarm");
2117 XCloseDisplay(display);
2118 execvp(start_argv[0], start_argv);
2119 warn("execvp failed");
2124 root_to_region(Window root)
2126 struct swm_region *r = NULL;
2128 int i, x, y, wx, wy;
2131 for (i = 0; i < ScreenCount(display); i++)
2132 if (screens[i].root == root)
2135 if (XQueryPointer(display, screens[i].root,
2136 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
2137 /* choose a region based on pointer location */
2138 TAILQ_FOREACH(r, &screens[i].rl, entry)
2139 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
2140 y >= Y(r) && y <= Y(r) + HEIGHT(r))
2145 r = TAILQ_FIRST(&screens[i].rl);
2151 find_unmanaged_window(Window id)
2156 for (i = 0; i < ScreenCount(display); i++)
2157 for (j = 0; j < workspace_limit; j++)
2158 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2166 find_window(Window id)
2169 Window wrr, wpr, *wcr = NULL;
2173 for (i = 0; i < ScreenCount(display); i++)
2174 for (j = 0; j < workspace_limit; j++)
2175 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2179 /* if we were looking for the parent return that window instead */
2180 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
2185 /* ignore not found and root */
2186 if (wpr == 0 || wrr == wpr)
2189 /* look for parent */
2190 for (i = 0; i < ScreenCount(display); i++)
2191 for (j = 0; j < workspace_limit; j++)
2192 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2200 spawn(int ws_idx, union arg *args, int close_fd)
2205 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2208 close(ConnectionNumber(display));
2210 setenv("LD_PRELOAD", SWM_LIB, 1);
2212 if (asprintf(&ret, "%d", ws_idx) == -1) {
2213 warn("spawn: asprintf SWM_WS");
2216 setenv("_SWM_WS", ret, 1);
2220 if (asprintf(&ret, "%d", getpid()) == -1) {
2221 warn("spawn: asprintf _SWM_PID");
2224 setenv("_SWM_PID", ret, 1);
2228 if (setsid() == -1) {
2229 warn("spawn: setsid");
2235 * close stdin and stdout to prevent interaction between apps
2236 * and the baraction script
2237 * leave stderr open to record errors
2239 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2240 warn("spawn: open");
2243 dup2(fd, STDIN_FILENO);
2244 dup2(fd, STDOUT_FILENO);
2249 execvp(args->argv[0], args->argv);
2251 warn("spawn: execvp");
2256 kill_refs(struct ws_win *win)
2259 struct swm_region *r;
2260 struct workspace *ws;
2265 for (i = 0; i < ScreenCount(display); i++)
2266 TAILQ_FOREACH(r, &screens[i].rl, entry)
2267 for (x = 0; x < workspace_limit; x++) {
2269 if (win == ws->focus)
2271 if (win == ws->focus_prev)
2272 ws->focus_prev = NULL;
2277 validate_win(struct ws_win *testwin)
2280 struct workspace *ws;
2281 struct swm_region *r;
2284 if (testwin == NULL)
2287 for (i = 0; i < ScreenCount(display); i++)
2288 TAILQ_FOREACH(r, &screens[i].rl, entry)
2289 for (x = 0; x < workspace_limit; x++) {
2291 TAILQ_FOREACH(win, &ws->winlist, entry)
2299 validate_ws(struct workspace *testws)
2301 struct swm_region *r;
2302 struct workspace *ws;
2305 /* validate all ws */
2306 for (i = 0; i < ScreenCount(display); i++)
2307 TAILQ_FOREACH(r, &screens[i].rl, entry)
2308 for (x = 0; x < workspace_limit; x++) {
2317 unfocus_win(struct ws_win *win)
2322 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2326 if (win->ws == NULL)
2329 if (validate_ws(win->ws))
2330 return; /* XXX this gets hit with thunderbird, needs fixing */
2332 if (win->ws->r == NULL)
2335 if (validate_win(win)) {
2340 if (win->ws->focus == win) {
2341 win->ws->focus = NULL;
2342 win->ws->focus_prev = win;
2345 if (validate_win(win->ws->focus)) {
2346 kill_refs(win->ws->focus);
2347 win->ws->focus = NULL;
2349 if (validate_win(win->ws->focus_prev)) {
2350 kill_refs(win->ws->focus_prev);
2351 win->ws->focus_prev = NULL;
2354 /* drain all previous unfocus events */
2355 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2358 grabbuttons(win, 0);
2359 XSetWindowBorder(display, win->id,
2360 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2362 XChangeProperty(display, win->s->root,
2363 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2364 PropModeReplace, (unsigned char *)&none, 1);
2373 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2375 for (i = 0; i < ScreenCount(display); i++)
2376 for (j = 0; j < workspace_limit; j++)
2377 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2382 focus_win(struct ws_win *win)
2387 struct ws_win *cfw = NULL;
2390 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2394 if (win->ws == NULL)
2397 if (validate_ws(win->ws))
2398 return; /* XXX this gets hit with thunderbird, needs fixing */
2400 if (validate_win(win)) {
2405 if (validate_win(win)) {
2410 XGetInputFocus(display, &cur_focus, &rr);
2411 if ((cfw = find_window(cur_focus)) != NULL)
2414 /* use larger hammer since the window was killed somehow */
2415 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2416 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2417 XSetWindowBorder(display, cfw->id,
2418 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2421 win->ws->focus = win;
2423 if (win->ws->r != NULL) {
2424 /* drain all previous focus events */
2425 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2429 XSetInputFocus(display, win->id,
2430 RevertToParent, CurrentTime);
2431 grabbuttons(win, 1);
2432 XSetWindowBorder(display, win->id,
2433 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2434 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2435 win->ws->always_raise)
2436 XMapRaised(display, win->id);
2438 XChangeProperty(display, win->s->root,
2439 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2440 PropModeReplace, (unsigned char *)&win->id, 1);
2447 switchws(struct swm_region *r, union arg *args)
2449 int wsid = args->id, unmap_old = 0;
2450 struct swm_region *this_r, *other_r;
2452 struct workspace *new_ws, *old_ws;
2458 if (wsid >= workspace_limit)
2462 old_ws = this_r->ws;
2463 new_ws = &this_r->s->ws[wsid];
2465 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2466 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2468 if (new_ws == NULL || old_ws == NULL)
2470 if (new_ws == old_ws)
2473 other_r = new_ws->r;
2474 if (other_r == NULL) {
2475 /* the other workspace is hidden, hide this one */
2479 /* the other ws is visible in another region, exchange them */
2480 other_r->ws_prior = new_ws;
2481 other_r->ws = old_ws;
2482 old_ws->r = other_r;
2484 this_r->ws_prior = old_ws;
2485 this_r->ws = new_ws;
2488 /* this is needed so that we can click on a window after a restart */
2492 a.id = SWM_ARG_ID_FOCUSCUR;
2493 focus(new_ws->r, &a);
2495 /* unmap old windows */
2497 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2500 if (focus_mode == SWM_FOCUS_DEFAULT)
2501 drain_enter_notify();
2505 cyclews(struct swm_region *r, union arg *args)
2508 struct swm_screen *s = r->s;
2511 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2512 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2517 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2520 case SWM_ARG_ID_CYCLEWS_UP:
2521 if (a.id < workspace_limit - 1)
2526 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2529 case SWM_ARG_ID_CYCLEWS_DOWN:
2533 a.id = workspace_limit - 1;
2540 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2542 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2546 } while (a.id != r->ws->idx);
2550 priorws(struct swm_region *r, union arg *args)
2554 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2555 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2557 if (r->ws_prior == NULL)
2560 a.id = r->ws_prior->idx;
2565 cyclescr(struct swm_region *r, union arg *args)
2567 struct swm_region *rr = NULL;
2571 /* do nothing if we don't have more than one screen */
2572 if (!(ScreenCount(display) > 1 || outputs > 1))
2577 case SWM_ARG_ID_CYCLESC_UP:
2578 rr = TAILQ_NEXT(r, entry);
2580 rr = TAILQ_FIRST(&screens[i].rl);
2582 case SWM_ARG_ID_CYCLESC_DOWN:
2583 rr = TAILQ_PREV(r, swm_region_list, entry);
2585 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2593 /* move mouse to region */
2595 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2596 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2598 a.id = SWM_ARG_ID_FOCUSCUR;
2601 if (rr->ws->focus) {
2602 /* move to focus window */
2603 x = X(rr->ws->focus) + 1;
2604 y = Y(rr->ws->focus) + 1;
2605 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2610 sort_windows(struct ws_win_list *wl)
2612 struct ws_win *win, *parent, *nxt;
2617 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2618 nxt = TAILQ_NEXT(win, entry);
2619 if (win->transient) {
2620 parent = find_window(win->transient);
2621 if (parent == NULL) {
2622 warnx("not possible bug");
2625 TAILQ_REMOVE(wl, win, entry);
2626 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2633 swapwin(struct swm_region *r, union arg *args)
2635 struct ws_win *target, *source;
2636 struct ws_win *cur_focus;
2637 struct ws_win_list *wl;
2640 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2641 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2643 cur_focus = r->ws->focus;
2644 if (cur_focus == NULL)
2648 wl = &source->ws->winlist;
2651 case SWM_ARG_ID_SWAPPREV:
2652 if (source->transient)
2653 source = find_window(source->transient);
2654 target = TAILQ_PREV(source, ws_win_list, entry);
2655 if (target && target->transient)
2656 target = find_window(target->transient);
2657 TAILQ_REMOVE(wl, source, entry);
2659 TAILQ_INSERT_TAIL(wl, source, entry);
2661 TAILQ_INSERT_BEFORE(target, source, entry);
2663 case SWM_ARG_ID_SWAPNEXT:
2664 target = TAILQ_NEXT(source, entry);
2665 /* move the parent and let the sort handle the move */
2666 if (source->transient)
2667 source = find_window(source->transient);
2668 TAILQ_REMOVE(wl, source, entry);
2670 TAILQ_INSERT_HEAD(wl, source, entry);
2672 TAILQ_INSERT_AFTER(wl, target, source, entry);
2674 case SWM_ARG_ID_SWAPMAIN:
2675 target = TAILQ_FIRST(wl);
2676 if (target == source) {
2677 if (source->ws->focus_prev != NULL &&
2678 source->ws->focus_prev != target)
2679 source = source->ws->focus_prev;
2683 if (target == NULL || source == NULL)
2685 source->ws->focus_prev = target;
2686 TAILQ_REMOVE(wl, target, entry);
2687 TAILQ_INSERT_BEFORE(source, target, entry);
2688 TAILQ_REMOVE(wl, source, entry);
2689 TAILQ_INSERT_HEAD(wl, source, entry);
2691 case SWM_ARG_ID_MOVELAST:
2692 TAILQ_REMOVE(wl, source, entry);
2693 TAILQ_INSERT_TAIL(wl, source, entry);
2696 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2706 focus_prev(struct ws_win *win)
2708 struct ws_win *winfocus = NULL;
2709 struct ws_win *cur_focus = NULL;
2710 struct ws_win_list *wl = NULL;
2711 struct workspace *ws = NULL;
2713 if (!(win && win->ws))
2718 cur_focus = ws->focus;
2720 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2721 WINID(win), WINID(cur_focus));
2723 /* pickle, just focus on whatever */
2724 if (cur_focus == NULL) {
2725 /* use prev_focus if valid */
2726 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2727 find_window(WINID(ws->focus_prev)))
2728 winfocus = ws->focus_prev;
2732 /* if transient focus on parent */
2733 if (cur_focus->transient) {
2734 winfocus = find_window(cur_focus->transient);
2738 /* if in max_stack try harder */
2739 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2740 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2741 if (cur_focus != ws->focus_prev)
2742 winfocus = ws->focus_prev;
2744 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2749 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2751 if (winfocus == NULL || winfocus == win) {
2752 switch (focus_close) {
2753 case SWM_STACK_BOTTOM:
2754 winfocus = TAILQ_FIRST(wl);
2757 winfocus = TAILQ_LAST(wl, ws_win_list);
2759 case SWM_STACK_ABOVE:
2760 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2761 if (focus_close_wrap)
2762 winfocus = TAILQ_FIRST(wl);
2764 winfocus = TAILQ_PREV(cur_focus,
2765 ws_win_list, entry);
2768 case SWM_STACK_BELOW:
2769 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2771 if (focus_close_wrap)
2772 winfocus = TAILQ_LAST(wl, ws_win_list);
2774 winfocus = TAILQ_NEXT(cur_focus, entry);
2780 if (winfocus == NULL) {
2781 if (focus_default == SWM_STACK_TOP)
2782 winfocus = TAILQ_LAST(wl, ws_win_list);
2784 winfocus = TAILQ_FIRST(wl);
2788 focus_magic(winfocus);
2792 focus(struct swm_region *r, union arg *args)
2794 struct ws_win *winfocus = NULL, *head;
2795 struct ws_win *cur_focus = NULL;
2796 struct ws_win_list *wl = NULL;
2797 struct workspace *ws = NULL;
2803 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2805 /* treat FOCUS_CUR special */
2806 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2807 if (r->ws->focus && r->ws->focus->iconic == 0)
2808 winfocus = r->ws->focus;
2809 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2810 winfocus = r->ws->focus_prev;
2812 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2813 if (winfocus->iconic == 0)
2816 focus_magic(winfocus);
2820 if ((cur_focus = r->ws->focus) == NULL)
2824 if (TAILQ_EMPTY(wl))
2826 /* make sure there is at least one uniconified window */
2828 TAILQ_FOREACH(winfocus, wl, entry)
2829 if (winfocus->iconic == 0) {
2837 case SWM_ARG_ID_FOCUSPREV:
2838 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2840 head = TAILQ_LAST(wl, ws_win_list);
2842 if (WINID(winfocus) == cur_focus->transient) {
2843 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2845 head = TAILQ_LAST(wl, ws_win_list);
2850 if (winfocus && winfocus->iconic) {
2851 while (winfocus != cur_focus) {
2852 if (winfocus == NULL)
2853 winfocus = TAILQ_LAST(wl, ws_win_list);
2854 if (winfocus->iconic == 0)
2856 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2862 case SWM_ARG_ID_FOCUSNEXT:
2863 head = TAILQ_NEXT(cur_focus, entry);
2865 head = TAILQ_FIRST(wl);
2869 if (winfocus && winfocus->iconic) {
2870 while (winfocus != cur_focus) {
2871 if (winfocus == NULL)
2872 winfocus = TAILQ_FIRST(wl);
2873 if (winfocus->iconic == 0)
2875 winfocus = TAILQ_NEXT(winfocus, entry);
2880 case SWM_ARG_ID_FOCUSMAIN:
2881 winfocus = TAILQ_FIRST(wl);
2882 if (winfocus == cur_focus)
2883 winfocus = cur_focus->ws->focus_prev;
2890 focus_magic(winfocus);
2894 cycle_layout(struct swm_region *r, union arg *args)
2896 struct workspace *ws = r->ws;
2899 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2902 if (ws->cur_layout->l_stack == NULL)
2903 ws->cur_layout = &layouts[0];
2906 if (focus_mode == SWM_FOCUS_DEFAULT)
2907 drain_enter_notify();
2908 a.id = SWM_ARG_ID_FOCUSCUR;
2913 stack_config(struct swm_region *r, union arg *args)
2915 struct workspace *ws = r->ws;
2917 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2920 if (ws->cur_layout->l_config != NULL)
2921 ws->cur_layout->l_config(ws, args->id);
2923 if (args->id != SWM_ARG_ID_STACKINIT)
2930 struct swm_geometry g;
2931 struct swm_region *r;
2937 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2939 for (i = 0; i < ScreenCount(display); i++) {
2943 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2944 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2945 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2947 /* start with screen geometry, adjust for bar */
2949 g.w -= 2 * border_width;
2950 g.h -= 2 * border_width;
2956 r->ws->cur_layout->l_stack(r->ws, &g);
2957 r->ws->cur_layout->l_string(r->ws);
2958 /* save r so we can track region changes */
2965 if (focus_mode == SWM_FOCUS_DEFAULT)
2966 drain_enter_notify();
2968 DNPRINTF(SWM_D_STACK, "stack: end\n");
2972 store_float_geom(struct ws_win *win, struct swm_region *r)
2974 /* retain window geom and region geom */
2975 win->g_float = win->g;
2976 win->rg_float = r->g;
2977 win->g_floatvalid = 1;
2981 stack_floater(struct ws_win *win, struct swm_region *r)
2989 bzero(&wc, sizeof wc);
2990 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2993 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2994 * geom on ws switches or return from max mode
2996 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2997 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2999 * use stored g and rg to set relative position and size
3000 * as in old region or before max stack mode
3002 X(win) = win->g_float.x - win->rg_float.x + X(r);
3003 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3004 WIDTH(win) = win->g_float.w;
3005 HEIGHT(win) = win->g_float.h;
3006 win->g_floatvalid = 0;
3009 win->floatmaxed = 0;
3011 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
3012 (HEIGHT(win) >= HEIGHT(r)))
3013 wc.border_width = 0;
3015 wc.border_width = border_width;
3016 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3017 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3018 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3023 * floaters and transients are auto-centred unless moved
3026 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
3027 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
3030 /* win can be outside r if new r smaller than old r */
3031 /* Ensure top left corner inside r (move probs otherwise) */
3032 if (X(win) < X(r) - wc.border_width)
3033 X(win) = X(r) - wc.border_width;
3034 if (X(win) > X(r) + WIDTH(r) - 1)
3035 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
3036 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
3037 if (Y(win) < Y(r) - wc.border_width)
3038 Y(win) = Y(r) - wc.border_width;
3039 if (Y(win) > Y(r) + HEIGHT(r) - 1)
3040 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
3041 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
3045 wc.width = WIDTH(win);
3046 wc.height = HEIGHT(win);
3049 * Retain floater and transient geometry for correct positioning
3050 * when ws changes region
3052 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
3053 store_float_geom(win, r);
3055 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
3056 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3058 XConfigureWindow(display, win->id, mask, &wc);
3062 * Send keystrokes to terminal to decrease/increase the font size as the
3063 * window size changes.
3066 adjust_font(struct ws_win *win)
3068 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3069 win->floating || win->transient)
3072 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3073 WIDTH(win) / win->sh.width_inc < term_width &&
3074 win->font_steps < SWM_MAX_FONT_STEPS) {
3075 win->font_size_boundary[win->font_steps] =
3076 (win->sh.width_inc * term_width) + win->sh.base_width;
3079 win->last_inc = win->sh.width_inc;
3080 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3081 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3082 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3085 win->last_inc = win->sh.width_inc;
3086 fake_keypress(win, XK_KP_Add, ShiftMask);
3090 #define SWAPXY(g) do { \
3092 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3093 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3096 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3099 XWindowAttributes wa;
3100 struct swm_geometry win_g, r_g = *g;
3101 struct ws_win *win, *fs_win = 0;
3102 int i, j, s, stacks;
3103 int w_inc = 1, h_inc, w_base = 1, h_base;
3104 int hrh, extra = 0, h_slice, last_h = 0;
3105 int split, colno, winno, mwin, msize, mscale;
3106 int remain, missing, v_slice, reconfigure;
3109 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3110 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3112 winno = count_win(ws, 0);
3113 if (winno == 0 && count_win(ws, 1) == 0)
3116 TAILQ_FOREACH(win, &ws->winlist, entry)
3117 if (win->transient == 0 && win->floating == 0
3118 && win->iconic == 0)
3125 w_inc = win->sh.width_inc;
3126 w_base = win->sh.base_width;
3127 mwin = ws->l_state.horizontal_mwin;
3128 mscale = ws->l_state.horizontal_msize;
3129 stacks = ws->l_state.horizontal_stacks;
3132 w_inc = win->sh.height_inc;
3133 w_base = win->sh.base_height;
3134 mwin = ws->l_state.vertical_mwin;
3135 mscale = ws->l_state.vertical_msize;
3136 stacks = ws->l_state.vertical_stacks;
3140 if (stacks > winno - mwin)
3141 stacks = winno - mwin;
3145 h_slice = r_g.h / SWM_H_SLICE;
3146 if (mwin && winno > mwin) {
3147 v_slice = r_g.w / SWM_V_SLICE;
3151 win_g.w = v_slice * mscale;
3153 if (w_inc > 1 && w_inc < v_slice) {
3154 /* adjust for window's requested size increment */
3155 remain = (win_g.w - w_base) % w_inc;
3161 win_g.x += r_g.w - msize;
3164 colno = split = winno / stacks;
3165 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3166 2 * border_width) / stacks);
3168 hrh = r_g.h / colno;
3169 extra = r_g.h - (colno * hrh);
3170 win_g.h = hrh - 2 * border_width;
3172 /* stack all the tiled windows */
3173 i = j = 0, s = stacks;
3174 TAILQ_FOREACH(win, &ws->winlist, entry) {
3175 if (win->transient != 0 || win->floating != 0)
3177 if (win->iconic != 0)
3180 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3185 if (split && i == split) {
3186 colno = (winno - mwin) / stacks;
3187 if (s <= (winno - mwin) % stacks)
3189 split = split + colno;
3190 hrh = (r_g.h / colno);
3191 extra = r_g.h - (colno * hrh);
3195 win_g.x += win_g.w + 2 * border_width;
3196 win_g.w = (r_g.w - msize -
3197 (stacks * 2 * border_width)) / stacks;
3199 win_g.w += (r_g.w - msize -
3200 (stacks * 2 * border_width)) % stacks;
3204 win_g.h = hrh - 2 * border_width;
3206 h_inc = win->sh.width_inc;
3207 h_base = win->sh.base_width;
3209 h_inc = win->sh.height_inc;
3210 h_base = win->sh.base_height;
3212 if (j == colno - 1) {
3213 win_g.h = hrh + extra;
3214 } else if (h_inc > 1 && h_inc < h_slice) {
3215 /* adjust for window's requested size increment */
3216 remain = (win_g.h - h_base) % h_inc;
3217 missing = h_inc - remain;
3219 if (missing <= extra || j == 0) {
3231 win_g.y += last_h + 2 * border_width;
3233 bzero(&wc, sizeof wc);
3234 if (disable_border && bar_enabled == 0 && winno == 1){
3235 wc.border_width = 0;
3236 win_g.w += 2 * border_width;
3237 win_g.h += 2 * border_width;
3239 wc.border_width = border_width;
3242 if (X(win) != win_g.y || Y(win) != win_g.x ||
3243 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3245 X(win) = wc.x = win_g.y;
3246 Y(win) = wc.y = win_g.x;
3247 WIDTH(win) = wc.width = win_g.h;
3248 HEIGHT(win) = wc.height = win_g.w;
3251 if (X(win) != win_g.x || Y(win) != win_g.y ||
3252 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3254 X(win) = wc.x = win_g.x;
3255 Y(win) = wc.y = win_g.y;
3256 WIDTH(win) = wc.width = win_g.w;
3257 HEIGHT(win) = wc.height = win_g.h;
3262 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3263 XConfigureWindow(display, win->id, mask, &wc);
3266 if (XGetWindowAttributes(display, win->id, &wa))
3267 if (wa.map_state == IsUnmapped)
3268 XMapRaised(display, win->id);
3276 /* now, stack all the floaters and transients */
3277 TAILQ_FOREACH(win, &ws->winlist, entry) {
3278 if (win->transient == 0 && win->floating == 0)
3280 if (win->iconic == 1)
3282 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3287 stack_floater(win, ws->r);
3288 XMapRaised(display, win->id);
3292 stack_floater(fs_win, ws->r);
3293 XMapRaised(display, fs_win->id);
3298 vertical_config(struct workspace *ws, int id)
3300 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3304 case SWM_ARG_ID_STACKRESET:
3305 case SWM_ARG_ID_STACKINIT:
3306 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3307 ws->l_state.vertical_mwin = 1;
3308 ws->l_state.vertical_stacks = 1;
3310 case SWM_ARG_ID_MASTERSHRINK:
3311 if (ws->l_state.vertical_msize > 1)
3312 ws->l_state.vertical_msize--;
3314 case SWM_ARG_ID_MASTERGROW:
3315 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3316 ws->l_state.vertical_msize++;
3318 case SWM_ARG_ID_MASTERADD:
3319 ws->l_state.vertical_mwin++;
3321 case SWM_ARG_ID_MASTERDEL:
3322 if (ws->l_state.vertical_mwin > 0)
3323 ws->l_state.vertical_mwin--;
3325 case SWM_ARG_ID_STACKINC:
3326 ws->l_state.vertical_stacks++;
3328 case SWM_ARG_ID_STACKDEC:
3329 if (ws->l_state.vertical_stacks > 1)
3330 ws->l_state.vertical_stacks--;
3332 case SWM_ARG_ID_FLIPLAYOUT:
3333 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3341 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3343 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3345 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3349 horizontal_config(struct workspace *ws, int id)
3351 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3354 case SWM_ARG_ID_STACKRESET:
3355 case SWM_ARG_ID_STACKINIT:
3356 ws->l_state.horizontal_mwin = 1;
3357 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3358 ws->l_state.horizontal_stacks = 1;
3360 case SWM_ARG_ID_MASTERSHRINK:
3361 if (ws->l_state.horizontal_msize > 1)
3362 ws->l_state.horizontal_msize--;
3364 case SWM_ARG_ID_MASTERGROW:
3365 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3366 ws->l_state.horizontal_msize++;
3368 case SWM_ARG_ID_MASTERADD:
3369 ws->l_state.horizontal_mwin++;
3371 case SWM_ARG_ID_MASTERDEL:
3372 if (ws->l_state.horizontal_mwin > 0)
3373 ws->l_state.horizontal_mwin--;
3375 case SWM_ARG_ID_STACKINC:
3376 ws->l_state.horizontal_stacks++;
3378 case SWM_ARG_ID_STACKDEC:
3379 if (ws->l_state.horizontal_stacks > 1)
3380 ws->l_state.horizontal_stacks--;
3382 case SWM_ARG_ID_FLIPLAYOUT:
3383 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3391 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3393 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3395 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3398 /* fullscreen view */
3400 max_stack(struct workspace *ws, struct swm_geometry *g)
3403 struct swm_geometry gg = *g;
3404 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3408 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3413 winno = count_win(ws, 0);
3414 if (winno == 0 && count_win(ws, 1) == 0)
3417 TAILQ_FOREACH(win, &ws->winlist, entry) {
3418 if (win->transient) {
3420 parent = find_window(win->transient);
3424 if (win->floating && win->floatmaxed == 0 ) {
3426 * retain geometry for retrieval on exit from
3429 store_float_geom(win, ws->r);
3430 win->floatmaxed = 1;
3433 /* only reconfigure if necessary */
3434 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3435 HEIGHT(win) != gg.h) {
3436 bzero(&wc, sizeof wc);
3437 X(win) = wc.x = gg.x;
3438 Y(win) = wc.y = gg.y;
3440 wc.border_width = border_width;
3441 WIDTH(win) = wc.width = gg.w;
3442 HEIGHT(win) = wc.height = gg.h;
3444 wc.border_width = 0;
3445 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3446 HEIGHT(win) = wc.height = gg.h +
3449 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3450 XConfigureWindow(display, win->id, mask, &wc);
3452 /* unmap only if we don't have multi screen */
3453 if (win != ws->focus)
3454 if (!(ScreenCount(display) > 1 || outputs > 1))
3458 /* put the last transient on top */
3461 XMapRaised(display, parent->id);
3462 stack_floater(wintrans, ws->r);
3463 focus_magic(wintrans);
3468 send_to_ws(struct swm_region *r, union arg *args)
3470 int wsid = args->id;
3471 struct ws_win *win = NULL, *parent;
3472 struct workspace *ws, *nws;
3473 Atom ws_idx_atom = 0;
3474 unsigned char ws_idx_str[SWM_PROPLEN];
3477 if (wsid >= workspace_limit)
3480 if (r && r->ws && r->ws->focus)
3486 if (win->ws->idx == wsid)
3489 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3492 nws = &win->s->ws[wsid];
3494 a.id = SWM_ARG_ID_FOCUSPREV;
3496 if (win->transient) {
3497 parent = find_window(win->transient);
3499 unmap_window(parent);
3500 TAILQ_REMOVE(&ws->winlist, parent, entry);
3501 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3506 TAILQ_REMOVE(&ws->winlist, win, entry);
3507 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3508 if (TAILQ_EMPTY(&ws->winlist))
3509 r->ws->focus = NULL;
3512 /* Try to update the window's workspace property */
3513 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3515 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3517 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3519 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3520 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
3528 pressbutton(struct swm_region *r, union arg *args)
3530 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3531 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3535 raise_toggle(struct swm_region *r, union arg *args)
3537 if (r == NULL || r->ws == NULL)
3540 r->ws->always_raise = !r->ws->always_raise;
3542 /* bring floaters back to top */
3543 if (r->ws->always_raise == 0)
3548 iconify(struct swm_region *r, union arg *args)
3552 if (r->ws->focus == NULL)
3554 unmap_window(r->ws->focus);
3555 update_iconic(r->ws->focus, 1);
3557 if (focus_mode == SWM_FOCUS_DEFAULT)
3558 drain_enter_notify();
3559 r->ws->focus = NULL;
3560 a.id = SWM_ARG_ID_FOCUSCUR;
3565 get_win_name(Window win)
3567 unsigned char *prop = NULL;
3568 unsigned long nbytes, nitems;
3570 /* try _NET_WM_NAME first */
3571 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3574 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3575 &nitems, NULL, &prop))
3579 /* fallback to WM_NAME */
3580 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3583 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3590 uniconify(struct swm_region *r, union arg *args)
3594 unsigned char *name;
3597 DNPRINTF(SWM_D_MISC, "uniconify\n");
3599 if (r == NULL || r->ws == NULL)
3602 /* make sure we have anything to uniconify */
3603 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3604 if (win->ws == NULL)
3605 continue; /* should never happen */
3606 if (win->iconic == 0)
3614 search_resp_action = SWM_SEARCH_UNICONIFY;
3616 spawn_select(r, args, "search", &searchpid);
3618 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3621 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3622 if (win->ws == NULL)
3623 continue; /* should never happen */
3624 if (win->iconic == 0)
3627 name = get_win_name(win->id);
3630 fprintf(lfile, "%s.%lu\n", name, win->id);
3638 name_workspace(struct swm_region *r, union arg *args)
3642 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3648 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3650 spawn_select(r, args, "name_workspace", &searchpid);
3652 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3655 fprintf(lfile, "%s", "");
3660 search_workspace(struct swm_region *r, union arg *args)
3663 struct workspace *ws;
3666 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3672 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3674 spawn_select(r, args, "search", &searchpid);
3676 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3679 for (i = 0; i < workspace_limit; i++) {
3683 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3684 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3691 search_win_cleanup(void)
3693 struct search_window *sw = NULL;
3695 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3696 XDestroyWindow(display, sw->indicator);
3697 XFreeGC(display, sw->gc);
3698 TAILQ_REMOVE(&search_wl, sw, entry);
3704 search_win(struct swm_region *r, union arg *args)
3706 struct ws_win *win = NULL;
3707 struct search_window *sw = NULL;
3714 XRectangle ibox, lbox;
3716 DNPRINTF(SWM_D_MISC, "search_win\n");
3719 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3721 spawn_select(r, args, "search", &searchpid);
3723 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3726 TAILQ_INIT(&search_wl);
3729 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3730 if (win->iconic == 1)
3733 sw = calloc(1, sizeof(struct search_window));
3735 warn("search_win: calloc");
3737 search_win_cleanup();
3743 snprintf(s, sizeof s, "%d", i);
3746 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3748 w = XCreateSimpleWindow(display,
3749 win->id, 0, 0,lbox.width + 4,
3750 bar_fs_extents->max_logical_extent.height, 1,
3751 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3752 r->s->c[SWM_S_COLOR_FOCUS].color);
3755 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3757 sw->gc = XCreateGC(display, w, 0, &gcv);
3758 XMapRaised(display, w);
3759 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3761 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3762 (bar_fs_extents->max_logical_extent.height -
3763 lbox.height) / 2 - lbox.y, s, len);
3765 fprintf(lfile, "%d\n", i);
3773 search_resp_uniconify(char *resp, unsigned long len)
3775 unsigned char *name;
3779 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3781 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3782 if (win->iconic == 0)
3784 name = get_win_name(win->id);
3787 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3792 if (strncmp(s, resp, len) == 0) {
3793 /* XXX this should be a callback to generalize */
3794 update_iconic(win, 0);
3803 search_resp_name_workspace(char *resp, unsigned long len)
3805 struct workspace *ws;
3807 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3809 if (search_r->ws == NULL)
3814 free(search_r->ws->name);
3815 search_r->ws->name = NULL;
3819 ws->name = strdup(resp);
3820 if (ws->name == NULL) {
3821 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3822 "strdup: %s", strerror(errno));
3829 search_resp_search_workspace(char *resp, unsigned long len)
3836 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3840 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3847 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3849 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3856 switchws(search_r, &a);
3860 search_resp_search_window(char *resp, unsigned long len)
3865 struct search_window *sw;
3867 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3871 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3876 idx = strtonum(s, 1, INT_MAX, &errstr);
3878 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3885 TAILQ_FOREACH(sw, &search_wl, entry)
3886 if (idx == sw->idx) {
3892 #define MAX_RESP_LEN 1024
3895 search_do_resp(void)
3901 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3906 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3907 warn("search: calloc");
3911 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3913 warn("search: read error");
3916 resp[rbytes] = '\0';
3919 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3920 * newline, so work around that by sanitizing the resp now.
3922 resp[strcspn(resp, "\n")] = '\0';
3925 switch (search_resp_action) {
3926 case SWM_SEARCH_UNICONIFY:
3927 search_resp_uniconify(resp, len);
3929 case SWM_SEARCH_NAME_WORKSPACE:
3930 search_resp_name_workspace(resp, len);
3932 case SWM_SEARCH_SEARCH_WORKSPACE:
3933 search_resp_search_workspace(resp, len);
3935 case SWM_SEARCH_SEARCH_WINDOW:
3936 search_resp_search_window(resp, len);
3941 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3942 search_win_cleanup();
3944 search_resp_action = SWM_SEARCH_NONE;
3945 close(select_resp_pipe[0]);
3950 wkill(struct swm_region *r, union arg *args)
3952 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3954 if (r->ws->focus == NULL)
3957 if (args->id == SWM_ARG_ID_KILLWINDOW)
3958 XKillClient(display, r->ws->focus->id);
3960 if (r->ws->focus->can_delete)
3961 client_msg(r->ws->focus, adelete);
3966 floating_toggle_win(struct ws_win *win)
3968 struct swm_region *r;
3978 /* reject floating toggles in max stack mode */
3979 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3982 if (win->floating) {
3983 if (!win->floatmaxed) {
3984 /* retain position for refloat */
3985 store_float_geom(win, r);
3989 if (win->g_floatvalid) {
3990 /* refloat at last floating relative position */
3991 X(win) = win->g_float.x - win->rg_float.x + X(r);
3992 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3993 WIDTH(win) = win->g_float.w;
3994 HEIGHT(win) = win->g_float.h;
3999 ewmh_update_actions(win);
4005 floating_toggle(struct swm_region *r, union arg *args)
4007 struct ws_win *win = r->ws->focus;
4013 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4014 _NET_WM_STATE_TOGGLE);
4017 if (focus_mode == SWM_FOCUS_DEFAULT)
4018 drain_enter_notify();
4020 if (win == win->ws->focus) {
4021 a.id = SWM_ARG_ID_FOCUSCUR;
4022 focus(win->ws->r, &a);
4027 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4029 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
4031 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
4033 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
4036 if (X(win) < X(r) - border_width) {
4038 WIDTH(win) -= X(r) - X(win) - border_width;
4040 X(win) = X(r) - border_width;
4043 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
4045 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
4047 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
4050 if (Y(win) < Y(r) - border_width) {
4052 HEIGHT(win) -= Y(r) - Y(win) - border_width;
4054 Y(win) = Y(r) - border_width;
4059 if (HEIGHT(win) < 1)
4064 update_window(struct ws_win *win)
4069 bzero(&wc, sizeof wc);
4070 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
4071 wc.border_width = border_width;
4074 wc.width = WIDTH(win);
4075 wc.height = HEIGHT(win);
4077 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
4078 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
4080 XConfigureWindow(display, win->id, mask, &wc);
4083 #define SWM_RESIZE_STEPS (50)
4086 resize(struct ws_win *win, union arg *args)
4090 struct swm_region *r = NULL;
4091 int resize_step = 0;
4095 struct swm_geometry g;
4096 int top = 0, left = 0;
4099 unsigned int shape; /* cursor style */
4105 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
4106 "transient: 0x%lx\n", win->id, YESNO(win->floating),
4109 if (!(win->transient != 0 || win->floating != 0))
4112 /* reject resizes in max mode for floaters (transient ok) */
4113 if (win->floatmaxed)
4117 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4123 case SWM_ARG_ID_WIDTHSHRINK:
4124 WIDTH(win) -= SWM_RESIZE_STEPS;
4127 case SWM_ARG_ID_WIDTHGROW:
4128 WIDTH(win) += SWM_RESIZE_STEPS;
4131 case SWM_ARG_ID_HEIGHTSHRINK:
4132 HEIGHT(win) -= SWM_RESIZE_STEPS;
4135 case SWM_ARG_ID_HEIGHTGROW:
4136 HEIGHT(win) += SWM_RESIZE_STEPS;
4143 constrain_window(win, r, 1);
4145 store_float_geom(win,r);
4149 if (focus_mode == SWM_FOCUS_DEFAULT)
4150 drain_enter_notify();
4152 /* get cursor offset from window root */
4153 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4158 if (wx < WIDTH(win) / 2)
4161 if (wy < HEIGHT(win) / 2)
4164 if (args->id == SWM_ARG_ID_CENTER)
4167 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4169 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4171 cursor = XCreateFontCursor(display, shape);
4173 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4174 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
4175 XFreeCursor(display, cursor);
4180 XMaskEvent(display, MOUSEMASK | ExposureMask |
4181 SubstructureRedirectMask, &ev);
4183 case ConfigureRequest:
4186 handler[ev.type](&ev);
4189 /* cursor offset/delta from start of the operation */
4190 dx = ev.xmotion.x_root - x;
4191 dy = ev.xmotion.y_root - y;
4197 if (args->id == SWM_ARG_ID_CENTER) {
4198 if (g.h / 2 + dy < 1)
4202 HEIGHT(win) = g.h + 2 * dy;
4210 HEIGHT(win) = g.h + dy;
4217 if (args->id == SWM_ARG_ID_CENTER) {
4218 if (g.w / 2 + dx < 1)
4222 WIDTH(win) = g.w + 2 * dx;
4230 WIDTH(win) = g.w + dx;
4233 constrain_window(win, r, 1);
4235 /* not free, don't sync more than 120 times / second */
4236 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4237 time = ev.xmotion.time;
4238 XSync(display, False);
4243 } while (ev.type != ButtonRelease);
4245 XSync(display, False);
4248 store_float_geom(win,r);
4250 XUngrabPointer(display, CurrentTime);
4251 XFreeCursor(display, cursor);
4254 drain_enter_notify();
4258 resize_step(struct swm_region *r, union arg *args)
4260 struct ws_win *win = NULL;
4262 if (r && r->ws && r->ws->focus)
4270 #define SWM_MOVE_STEPS (50)
4273 move(struct ws_win *win, union arg *args)
4278 struct swm_region *r = NULL;
4288 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4289 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4291 /* in max_stack mode should only move transients */
4292 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4296 if (win->floating == 0 && !win->transient) {
4297 store_float_geom(win,r);
4298 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4301 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4308 case SWM_ARG_ID_MOVELEFT:
4309 X(win) -= (SWM_MOVE_STEPS - border_width);
4312 case SWM_ARG_ID_MOVERIGHT:
4313 X(win) += (SWM_MOVE_STEPS - border_width);
4316 case SWM_ARG_ID_MOVEUP:
4317 Y(win) -= (SWM_MOVE_STEPS - border_width);
4320 case SWM_ARG_ID_MOVEDOWN:
4321 Y(win) += (SWM_MOVE_STEPS - border_width);
4328 constrain_window(win, r, 0);
4330 store_float_geom(win, r);
4334 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4335 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4336 CurrentTime) != GrabSuccess)
4339 /* get cursor offset from window root */
4340 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4344 XMaskEvent(display, MOUSEMASK | ExposureMask |
4345 SubstructureRedirectMask, &ev);
4347 case ConfigureRequest:
4350 handler[ev.type](&ev);
4353 X(win) = ev.xmotion.x_root - wx - border_width;
4354 Y(win) = ev.xmotion.y_root - wy - border_width;
4356 constrain_window(win, r, 0);
4358 /* not free, don't sync more than 120 times / second */
4359 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4360 time = ev.xmotion.time;
4361 XSync(display, False);
4366 } while (ev.type != ButtonRelease);
4368 XSync(display, False);
4371 store_float_geom(win,r);
4372 XUngrabPointer(display, CurrentTime);
4375 drain_enter_notify();
4379 move_step(struct swm_region *r, union arg *args)
4381 struct ws_win *win = NULL;
4383 if (r && r->ws && r->ws->focus)
4388 if (!(win->transient != 0 || win->floating != 0))
4395 /* user/key callable function IDs */
4484 kf_search_workspace,
4486 kf_dumpwins, /* MUST BE LAST */
4490 /* key definitions */
4492 dummykeyfunc(struct swm_region *r, union arg *args)
4497 char name[SWM_FUNCNAME_LEN];
4498 void (*func)(struct swm_region *r, union arg *);
4500 } keyfuncs[kf_invalid + 1] = {
4501 /* name function argument */
4502 { "cycle_layout", cycle_layout, {0} },
4503 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4504 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4505 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4506 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4507 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4508 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4509 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4510 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4511 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4512 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4513 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4514 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4515 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4516 { "quit", quit, {0} },
4517 { "restart", restart, {0} },
4518 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4519 { "ws_1", switchws, {.id = 0} },
4520 { "ws_2", switchws, {.id = 1} },
4521 { "ws_3", switchws, {.id = 2} },
4522 { "ws_4", switchws, {.id = 3} },
4523 { "ws_5", switchws, {.id = 4} },
4524 { "ws_6", switchws, {.id = 5} },
4525 { "ws_7", switchws, {.id = 6} },
4526 { "ws_8", switchws, {.id = 7} },
4527 { "ws_9", switchws, {.id = 8} },
4528 { "ws_10", switchws, {.id = 9} },
4529 { "ws_11", switchws, {.id = 10} },
4530 { "ws_12", switchws, {.id = 11} },
4531 { "ws_13", switchws, {.id = 12} },
4532 { "ws_14", switchws, {.id = 13} },
4533 { "ws_15", switchws, {.id = 14} },
4534 { "ws_16", switchws, {.id = 15} },
4535 { "ws_17", switchws, {.id = 16} },
4536 { "ws_18", switchws, {.id = 17} },
4537 { "ws_19", switchws, {.id = 18} },
4538 { "ws_20", switchws, {.id = 19} },
4539 { "ws_21", switchws, {.id = 20} },
4540 { "ws_22", switchws, {.id = 21} },
4541 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4542 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4543 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4544 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4545 { "ws_prior", priorws, {0} },
4546 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4547 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4548 { "mvws_1", send_to_ws, {.id = 0} },
4549 { "mvws_2", send_to_ws, {.id = 1} },
4550 { "mvws_3", send_to_ws, {.id = 2} },
4551 { "mvws_4", send_to_ws, {.id = 3} },
4552 { "mvws_5", send_to_ws, {.id = 4} },
4553 { "mvws_6", send_to_ws, {.id = 5} },
4554 { "mvws_7", send_to_ws, {.id = 6} },
4555 { "mvws_8", send_to_ws, {.id = 7} },
4556 { "mvws_9", send_to_ws, {.id = 8} },
4557 { "mvws_10", send_to_ws, {.id = 9} },
4558 { "mvws_11", send_to_ws, {.id = 10} },
4559 { "mvws_12", send_to_ws, {.id = 11} },
4560 { "mvws_13", send_to_ws, {.id = 12} },
4561 { "mvws_14", send_to_ws, {.id = 13} },
4562 { "mvws_15", send_to_ws, {.id = 14} },
4563 { "mvws_16", send_to_ws, {.id = 15} },
4564 { "mvws_17", send_to_ws, {.id = 16} },
4565 { "mvws_18", send_to_ws, {.id = 17} },
4566 { "mvws_19", send_to_ws, {.id = 18} },
4567 { "mvws_20", send_to_ws, {.id = 19} },
4568 { "mvws_21", send_to_ws, {.id = 20} },
4569 { "mvws_22", send_to_ws, {.id = 21} },
4570 { "bar_toggle", bar_toggle, {0} },
4571 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4572 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4573 { "float_toggle", floating_toggle,{0} },
4574 { "version", version, {0} },
4575 { "spawn_custom", dummykeyfunc, {0} },
4576 { "iconify", iconify, {0} },
4577 { "uniconify", uniconify, {0} },
4578 { "raise_toggle", raise_toggle, {0} },
4579 { "button2", pressbutton, {2} },
4580 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4581 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4582 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4583 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4584 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4585 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4586 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4587 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4588 { "name_workspace", name_workspace, {0} },
4589 { "search_workspace", search_workspace, {0} },
4590 { "search_win", search_win, {0} },
4591 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4592 { "invalid key func", NULL, {0} },
4595 RB_ENTRY(key) entry;
4598 enum keyfuncid funcid;
4601 RB_HEAD(key_tree, key);
4604 key_cmp(struct key *kp1, struct key *kp2)
4606 if (kp1->keysym < kp2->keysym)
4608 if (kp1->keysym > kp2->keysym)
4611 if (kp1->mod < kp2->mod)
4613 if (kp1->mod > kp2->mod)
4619 RB_GENERATE(key_tree, key, entry, key_cmp);
4620 struct key_tree keys;
4623 enum { client_click, root_click };
4625 unsigned int action;
4627 unsigned int button;
4628 void (*func)(struct ws_win *, union arg *);
4631 /* action key mouse button func args */
4632 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4633 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4634 { client_click, MODKEY, Button1, move, {0} },
4638 update_modkey(unsigned int mod)
4644 RB_FOREACH(kp, key_tree, &keys)
4645 if (kp->mod & ShiftMask)
4646 kp->mod = mod | ShiftMask;
4650 for (i = 0; i < LENGTH(buttons); i++)
4651 if (buttons[i].mask & ShiftMask)
4652 buttons[i].mask = mod | ShiftMask;
4654 buttons[i].mask = mod;
4659 TAILQ_ENTRY(spawn_prog) entry;
4664 TAILQ_HEAD(spawn_list, spawn_prog);
4665 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4668 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4671 struct spawn_prog *prog = NULL;
4673 char *ap, **real_args;
4675 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4678 TAILQ_FOREACH(prog, &spawns, entry) {
4679 if (!strcasecmp(spawn_name, prog->name))
4683 warnx("spawn_custom: program %s not found", spawn_name);
4687 /* make room for expanded args */
4688 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4689 err(1, "spawn_custom: calloc real_args");
4691 /* expand spawn_args into real_args */
4692 for (i = 0; i < prog->argc; i++) {
4694 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4695 if (!strcasecmp(ap, "$bar_border")) {
4697 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4699 err(1, "spawn_custom border color");
4700 } else if (!strcasecmp(ap, "$bar_color")) {
4702 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4704 err(1, "spawn_custom bar color");
4705 } else if (!strcasecmp(ap, "$bar_font")) {
4706 if ((real_args[i] = strdup(bar_fonts))
4708 err(1, "spawn_custom bar fonts");
4709 } else if (!strcasecmp(ap, "$bar_font_color")) {
4711 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4713 err(1, "spawn_custom color font");
4714 } else if (!strcasecmp(ap, "$color_focus")) {
4716 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4718 err(1, "spawn_custom color focus");
4719 } else if (!strcasecmp(ap, "$color_unfocus")) {
4721 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4723 err(1, "spawn_custom color unfocus");
4725 /* no match --> copy as is */
4726 if ((real_args[i] = strdup(ap)) == NULL)
4727 err(1, "spawn_custom strdup(ap)");
4729 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4734 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4735 for (i = 0; i < prog->argc; i++)
4736 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4737 DNPRINTF(SWM_D_SPAWN, "\n");
4739 *ret_args = real_args;
4740 return (prog->argc);
4744 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4750 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4754 spawn(r->ws->idx, &a, 1);
4756 for (i = 0; i < spawn_argc; i++)
4762 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4768 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4772 if (pipe(select_list_pipe) == -1)
4773 err(1, "pipe error");
4774 if (pipe(select_resp_pipe) == -1)
4775 err(1, "pipe error");
4777 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4778 err(1, "could not disable SIGPIPE");
4779 switch (*pid = fork()) {
4781 err(1, "cannot fork");
4784 if (dup2(select_list_pipe[0], 0) == -1)
4786 if (dup2(select_resp_pipe[1], 1) == -1)
4788 close(select_list_pipe[1]);
4789 close(select_resp_pipe[0]);
4790 spawn(r->ws->idx, &a, 0);
4792 default: /* parent */
4793 close(select_list_pipe[0]);
4794 close(select_resp_pipe[1]);
4798 for (i = 0; i < spawn_argc; i++)
4804 spawn_insert(char *name, char *args)
4806 char *arg, *cp, *ptr;
4807 struct spawn_prog *sp;
4809 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4811 if ((sp = calloc(1, sizeof *sp)) == NULL)
4812 err(1, "spawn_insert: malloc");
4813 if ((sp->name = strdup(name)) == NULL)
4814 err(1, "spawn_insert: strdup");
4816 /* convert the arguments to an argument list */
4817 if ((ptr = cp = strdup(args)) == NULL)
4818 err(1, "spawn_insert: strdup");
4819 while ((arg = strsep(&ptr, " \t")) != NULL) {
4820 /* empty field; skip it */
4825 if ((sp->argv = realloc(sp->argv, sp->argc *
4826 sizeof *sp->argv)) == NULL)
4827 err(1, "spawn_insert: realloc");
4828 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4829 err(1, "spawn_insert: strdup");
4833 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4834 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4838 spawn_remove(struct spawn_prog *sp)
4842 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4844 TAILQ_REMOVE(&spawns, sp, entry);
4845 for (i = 0; i < sp->argc; i++)
4851 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4855 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4857 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4860 spawn_insert(name, args);
4862 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4866 setspawn(char *name, char *args)
4868 struct spawn_prog *sp;
4870 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4875 TAILQ_FOREACH(sp, &spawns, entry) {
4876 if (!strcmp(sp->name, name)) {
4880 spawn_replace(sp, name, args);
4881 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4885 if (*args == '\0') {
4886 warnx("error: setspawn: cannot find program: %s", name);
4890 spawn_insert(name, args);
4891 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4895 setconfspawn(char *selector, char *value, int flags)
4897 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4899 setspawn(selector, value);
4901 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4908 setconfspawn("term", "xterm", 0);
4909 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4910 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4911 setconfspawn("lock", "xlock", 0);
4912 setconfspawn("initscr", "initscreen.sh", 0);
4913 setconfspawn("menu", "dmenu_run"
4916 " -nf $bar_font_color"
4918 " -sf $bar_color", 0);
4919 setconfspawn("search", "dmenu"
4923 " -nf $bar_font_color"
4925 " -sf $bar_color", 0);
4926 setconfspawn("name_workspace", "dmenu"
4930 " -nf $bar_font_color"
4932 " -sf $bar_color", 0);
4936 #define SWM_MODNAME_SIZE 32
4937 #define SWM_KEY_WS "\n+ \t"
4939 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4943 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4944 if (mod == NULL || ks == NULL) {
4945 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4948 if (keystr == NULL || strlen(keystr) == 0) {
4949 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4955 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4956 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4958 cp += (long)strspn(cp, SWM_KEY_WS);
4959 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4961 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4963 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4965 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4967 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4969 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4971 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4972 *mod |= ControlMask;
4974 *ks = XStringToKeysym(name);
4975 XConvertCase(*ks, ks, &uks);
4976 if (ks == NoSymbol) {
4978 "parsekeys: invalid key %s\n",
4984 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4989 strdupsafe(char *str)
4994 return (strdup(str));
4998 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5002 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5003 keyfuncs[kfid].name, spawn_name);
5005 if ((kp = malloc(sizeof *kp)) == NULL)
5006 err(1, "key_insert: malloc");
5011 kp->spawn_name = strdupsafe(spawn_name);
5012 RB_INSERT(key_tree, &keys, kp);
5014 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5018 key_lookup(unsigned int mod, KeySym ks)
5025 return (RB_FIND(key_tree, &keys, &kp));
5029 key_remove(struct key *kp)
5031 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5033 RB_REMOVE(key_tree, &keys, kp);
5034 free(kp->spawn_name);
5037 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5041 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5044 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5048 key_insert(mod, ks, kfid, spawn_name);
5050 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5054 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5059 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5060 keyfuncs[kfid].name, spawn_name);
5062 if ((kp = key_lookup(mod, ks)) != NULL) {
5063 if (kfid == kf_invalid)
5066 key_replace(kp, mod, ks, kfid, spawn_name);
5067 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5070 if (kfid == kf_invalid) {
5071 warnx("error: setkeybinding: cannot find mod/key combination");
5072 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5076 key_insert(mod, ks, kfid, spawn_name);
5077 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5081 setconfbinding(char *selector, char *value, int flags)
5083 enum keyfuncid kfid;
5086 struct spawn_prog *sp;
5087 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5088 if (selector == NULL) {
5089 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5090 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5092 setkeybinding(mod, ks, kfid, NULL);
5097 /* search by key function name */
5098 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5099 if (strncasecmp(selector, keyfuncs[kfid].name,
5100 SWM_FUNCNAME_LEN) == 0) {
5101 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5103 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5104 setkeybinding(mod, ks, kfid, NULL);
5110 /* search by custom spawn name */
5111 TAILQ_FOREACH(sp, &spawns, entry) {
5112 if (strcasecmp(selector, sp->name) == 0) {
5113 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5115 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5116 setkeybinding(mod, ks, kf_spawn_custom,
5123 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5130 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5131 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5132 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5133 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5134 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5135 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5136 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5137 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5138 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5139 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5140 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5141 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5142 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5143 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5144 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5145 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5146 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5147 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5148 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5149 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5150 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5151 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5152 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5153 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5154 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5155 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5156 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5157 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5158 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5159 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5160 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5161 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5162 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5163 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5164 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5165 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5166 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5167 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5168 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5169 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5170 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5171 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5172 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5173 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5174 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5175 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5176 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5177 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5178 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5179 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5180 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5181 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5182 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5183 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5184 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5185 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5186 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5187 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5188 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5189 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5190 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5191 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5192 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5193 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5194 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5195 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5196 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5197 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5198 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5199 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5200 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5201 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5202 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5203 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5204 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5205 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5206 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5207 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5208 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5209 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5210 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5211 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5212 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5213 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5214 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5215 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5216 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5217 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5218 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5219 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5220 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5221 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5222 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5223 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5224 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5225 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5227 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5236 while (RB_EMPTY(&keys) == 0) {
5237 kp = RB_ROOT(&keys);
5243 setkeymapping(char *selector, char *value, int flags)
5245 char keymapping_file[PATH_MAX];
5246 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5247 if (value[0] == '~')
5248 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5249 pwd->pw_dir, &value[1]);
5251 strlcpy(keymapping_file, value, sizeof keymapping_file);
5253 /* load new key bindings; if it fails, revert to default bindings */
5254 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5258 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5263 updatenumlockmask(void)
5266 XModifierKeymap *modmap;
5268 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5270 modmap = XGetModifierMapping(display);
5271 for (i = 0; i < 8; i++)
5272 for (j = 0; j < modmap->max_keypermod; j++)
5273 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5274 == XKeysymToKeycode(display, XK_Num_Lock))
5275 numlockmask = (1 << i);
5277 XFreeModifiermap(modmap);
5285 unsigned int modifiers[] =
5286 { 0, LockMask, numlockmask, numlockmask | LockMask };
5289 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5290 updatenumlockmask();
5292 for (k = 0; k < ScreenCount(display); k++) {
5293 if (TAILQ_EMPTY(&screens[k].rl))
5295 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
5296 RB_FOREACH(kp, key_tree, &keys) {
5297 if ((code = XKeysymToKeycode(display, kp->keysym)))
5298 for (j = 0; j < LENGTH(modifiers); j++)
5299 XGrabKey(display, code,
5300 kp->mod | modifiers[j],
5301 screens[k].root, True,
5302 GrabModeAsync, GrabModeAsync);
5308 grabbuttons(struct ws_win *win, int focused)
5311 unsigned int modifiers[] =
5312 { 0, LockMask, numlockmask, numlockmask|LockMask };
5314 updatenumlockmask();
5315 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5317 for (i = 0; i < LENGTH(buttons); i++)
5318 if (buttons[i].action == client_click)
5319 for (j = 0; j < LENGTH(modifiers); j++)
5320 XGrabButton(display, buttons[i].button,
5321 buttons[i].mask | modifiers[j],
5322 win->id, False, BUTTONMASK,
5323 GrabModeAsync, GrabModeSync, None,
5326 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5327 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5330 const char *quirkname[] = {
5331 "NONE", /* config string for "no value" */
5340 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5341 #define SWM_Q_WS "\n|+ \t"
5343 parsequirks(char *qstr, unsigned long *quirk)
5353 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5355 cp += (long)strspn(cp, SWM_Q_WS);
5356 for (i = 0; i < LENGTH(quirkname); i++) {
5357 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5358 DNPRINTF(SWM_D_QUIRK,
5359 "parsequirks: %s\n", name);
5364 *quirk |= 1 << (i-1);
5368 if (i >= LENGTH(quirkname)) {
5369 DNPRINTF(SWM_D_QUIRK,
5370 "parsequirks: invalid quirk [%s]\n", name);
5378 quirk_insert(const char *class, const char *name, unsigned long quirk)
5382 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5385 if ((qp = malloc(sizeof *qp)) == NULL)
5386 err(1, "quirk_insert: malloc");
5387 if ((qp->class = strdup(class)) == NULL)
5388 err(1, "quirk_insert: strdup");
5389 if ((qp->name = strdup(name)) == NULL)
5390 err(1, "quirk_insert: strdup");
5393 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5395 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5399 quirk_remove(struct quirk *qp)
5401 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5402 qp->name, qp->quirk);
5404 TAILQ_REMOVE(&quirks, qp, entry);
5409 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5413 quirk_replace(struct quirk *qp, const char *class, const char *name,
5414 unsigned long quirk)
5416 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5417 qp->name, qp->quirk);
5420 quirk_insert(class, name, quirk);
5422 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5426 setquirk(const char *class, const char *name, unsigned long quirk)
5430 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5433 TAILQ_FOREACH(qp, &quirks, entry) {
5434 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5438 quirk_replace(qp, class, name, quirk);
5439 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5444 warnx("error: setquirk: cannot find class/name combination");
5448 quirk_insert(class, name, quirk);
5449 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5453 setconfquirk(char *selector, char *value, int flags)
5455 char *cp, *class, *name;
5457 unsigned long quirks;
5458 if (selector == NULL)
5460 if ((cp = strchr(selector, ':')) == NULL)
5465 if ((retval = parsequirks(value, &quirks)) == 0)
5466 setquirk(class, name, quirks);
5473 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5474 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5475 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5476 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5477 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5478 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5479 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5480 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5481 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5482 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5483 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5484 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5485 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5488 /* conf file stuff */
5489 #define SWM_CONF_FILE "spectrwm.conf"
5490 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5494 SWM_S_BAR_AT_BOTTOM,
5495 SWM_S_BAR_BORDER_WIDTH,
5502 SWM_S_CLOCK_ENABLED,
5505 SWM_S_CYCLE_VISIBLE,
5507 SWM_S_DISABLE_BORDER,
5509 SWM_S_FOCUS_CLOSE_WRAP,
5510 SWM_S_FOCUS_DEFAULT,
5516 SWM_S_STACK_ENABLED,
5518 SWM_S_TITLE_CLASS_ENABLED,
5519 SWM_S_TITLE_NAME_ENABLED,
5520 SWM_S_URGENT_ENABLED,
5521 SWM_S_VERBOSE_LAYOUT,
5522 SWM_S_WINDOW_NAME_ENABLED,
5523 SWM_S_WORKSPACE_LIMIT
5527 setconfvalue(char *selector, char *value, int flags)
5533 case SWM_S_BAR_ACTION:
5535 if ((bar_argv[0] = strdup(value)) == NULL)
5536 err(1, "setconfvalue: bar_action");
5538 case SWM_S_BAR_AT_BOTTOM:
5539 bar_at_bottom = atoi(value);
5541 case SWM_S_BAR_BORDER_WIDTH:
5542 bar_border_width = atoi(value);
5543 if (bar_border_width < 0)
5544 bar_border_width = 0;
5546 case SWM_S_BAR_DELAY:
5547 bar_delay = atoi(value);
5549 case SWM_S_BAR_ENABLED:
5550 bar_enabled = atoi(value);
5552 case SWM_S_BAR_FONT:
5554 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5555 err(1, "setconfvalue: asprintf: failed to allocate "
5556 "memory for bar_fonts.");
5560 case SWM_S_BAR_FORMAT:
5562 if ((bar_format = strdup(value)) == NULL)
5563 err(1, "setconfvalue: bar_format");
5565 case SWM_S_BAR_JUSTIFY:
5566 if (!strcmp(value, "left"))
5567 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5568 else if (!strcmp(value, "center"))
5569 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5570 else if (!strcmp(value, "right"))
5571 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5573 errx(1, "invalid bar_justify");
5575 case SWM_S_BORDER_WIDTH:
5576 border_width = atoi(value);
5577 if (border_width < 0)
5580 case SWM_S_CLOCK_ENABLED:
5581 clock_enabled = atoi(value);
5583 case SWM_S_CLOCK_FORMAT:
5584 #ifndef SWM_DENY_CLOCK_FORMAT
5586 if ((clock_format = strdup(value)) == NULL)
5587 err(1, "setconfvalue: clock_format");
5590 case SWM_S_CYCLE_EMPTY:
5591 cycle_empty = atoi(value);
5593 case SWM_S_CYCLE_VISIBLE:
5594 cycle_visible = atoi(value);
5596 case SWM_S_DIALOG_RATIO:
5597 dialog_ratio = atof(value);
5598 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5601 case SWM_S_DISABLE_BORDER:
5602 disable_border = atoi(value);
5604 case SWM_S_FOCUS_CLOSE:
5605 if (!strcmp(value, "first"))
5606 focus_close = SWM_STACK_BOTTOM;
5607 else if (!strcmp(value, "last"))
5608 focus_close = SWM_STACK_TOP;
5609 else if (!strcmp(value, "next"))
5610 focus_close = SWM_STACK_ABOVE;
5611 else if (!strcmp(value, "previous"))
5612 focus_close = SWM_STACK_BELOW;
5614 errx(1, "focus_close");
5616 case SWM_S_FOCUS_CLOSE_WRAP:
5617 focus_close_wrap = atoi(value);
5619 case SWM_S_FOCUS_DEFAULT:
5620 if (!strcmp(value, "last"))
5621 focus_default = SWM_STACK_TOP;
5622 else if (!strcmp(value, "first"))
5623 focus_default = SWM_STACK_BOTTOM;
5625 errx(1, "focus_default");
5627 case SWM_S_FOCUS_MODE:
5628 if (!strcmp(value, "default"))
5629 focus_mode = SWM_FOCUS_DEFAULT;
5630 else if (!strcmp(value, "follow_cursor"))
5631 focus_mode = SWM_FOCUS_FOLLOW;
5632 else if (!strcmp(value, "synergy"))
5633 focus_mode = SWM_FOCUS_SYNERGY;
5635 errx(1, "focus_mode");
5637 case SWM_S_SPAWN_ORDER:
5638 if (!strcmp(value, "first"))
5639 spawn_position = SWM_STACK_BOTTOM;
5640 else if (!strcmp(value, "last"))
5641 spawn_position = SWM_STACK_TOP;
5642 else if (!strcmp(value, "next"))
5643 spawn_position = SWM_STACK_ABOVE;
5644 else if (!strcmp(value, "previous"))
5645 spawn_position = SWM_STACK_BELOW;
5647 errx(1, "spawn_position");
5649 case SWM_S_SPAWN_TERM:
5650 setconfspawn("term", value, 0);
5654 case SWM_S_SS_ENABLED:
5655 ss_enabled = atoi(value);
5657 case SWM_S_STACK_ENABLED:
5658 stack_enabled = atoi(value);
5660 case SWM_S_TERM_WIDTH:
5661 term_width = atoi(value);
5665 case SWM_S_TITLE_CLASS_ENABLED:
5666 title_class_enabled = atoi(value);
5668 case SWM_S_TITLE_NAME_ENABLED:
5669 title_name_enabled = atoi(value);
5671 case SWM_S_URGENT_ENABLED:
5672 urgent_enabled = atoi(value);
5674 case SWM_S_VERBOSE_LAYOUT:
5675 verbose_layout = atoi(value);
5676 for (i = 0; layouts[i].l_stack != NULL; i++) {
5678 layouts[i].l_string = fancy_stacker;
5680 layouts[i].l_string = plain_stacker;
5683 case SWM_S_WINDOW_NAME_ENABLED:
5684 window_name_enabled = atoi(value);
5686 case SWM_S_WORKSPACE_LIMIT:
5687 workspace_limit = atoi(value);
5688 if (workspace_limit > SWM_WS_MAX)
5689 workspace_limit = SWM_WS_MAX;
5690 else if (workspace_limit < 1)
5691 workspace_limit = 1;
5700 setconfmodkey(char *selector, char *value, int flags)
5702 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5703 update_modkey(Mod1Mask);
5704 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5705 update_modkey(Mod2Mask);
5706 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5707 update_modkey(Mod3Mask);
5708 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5709 update_modkey(Mod4Mask);
5716 setconfcolor(char *selector, char *value, int flags)
5718 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5723 setconfregion(char *selector, char *value, int flags)
5725 custom_region(value);
5730 setautorun(char *selector, char *value, int flags)
5740 if (getenv("SWM_STARTED"))
5744 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5745 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5747 if (ws_id < 0 || ws_id >= workspace_limit)
5748 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5751 * This is a little intricate
5753 * If the pid already exists we simply reuse it because it means it was
5754 * used before AND not claimed by manage_window. We get away with
5755 * altering it in the parent after INSERT because this can not be a race
5758 while ((ap = strsep(&sp, " \t")) != NULL) {
5761 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5763 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5764 err(1, "setautorun: realloc");
5765 a.argv[argc - 1] = ap;
5768 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5769 err(1, "setautorun: realloc");
5770 a.argv[argc] = NULL;
5772 if ((pid = fork()) == 0) {
5773 spawn(ws_id, &a, 1);
5782 p = calloc(1, sizeof *p);
5785 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5795 setlayout(char *selector, char *value, int flags)
5797 int ws_id, i, x, mg, ma, si, raise, f = 0;
5798 int st = SWM_V_STACK;
5800 struct workspace *ws;
5802 if (getenv("SWM_STARTED"))
5806 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5807 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5808 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5809 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5812 if (ws_id < 0 || ws_id >= workspace_limit)
5813 errx(1, "layout: invalid workspace %d", ws_id + 1);
5815 if (!strcasecmp(s, "vertical"))
5817 else if (!strcasecmp(s, "vertical_flip")) {
5820 } else if (!strcasecmp(s, "horizontal"))
5822 else if (!strcasecmp(s, "horizontal_flip")) {
5825 } else if (!strcasecmp(s, "fullscreen"))
5828 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5829 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5832 for (i = 0; i < ScreenCount(display); i++) {
5833 ws = (struct workspace *)&screens[i].ws;
5834 ws[ws_id].cur_layout = &layouts[st];
5836 ws[ws_id].always_raise = raise;
5837 if (st == SWM_MAX_STACK)
5841 for (x = 0; x < abs(mg); x++) {
5842 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5843 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5844 SWM_ARG_ID_MASTERSHRINK);
5848 for (x = 0; x < abs(ma); x++) {
5849 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5850 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5851 SWM_ARG_ID_MASTERDEL);
5855 for (x = 0; x < abs(si); x++) {
5856 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5857 si >= 0 ? SWM_ARG_ID_STACKINC :
5858 SWM_ARG_ID_STACKDEC);
5863 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5864 SWM_ARG_ID_FLIPLAYOUT);
5872 /* config options */
5873 struct config_option {
5875 int (*func)(char*, char*, int);
5878 struct config_option configopt[] = {
5879 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5880 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5881 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5882 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5883 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5884 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5885 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5886 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5887 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5888 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5889 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
5890 { "keyboard_mapping", setkeymapping, 0 },
5891 { "bind", setconfbinding, 0 },
5892 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5893 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5894 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5895 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5896 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5897 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5898 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5899 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
5900 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5901 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5902 { "modkey", setconfmodkey, 0 },
5903 { "program", setconfspawn, 0 },
5904 { "quirk", setconfquirk, 0 },
5905 { "region", setconfregion, 0 },
5906 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5907 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5908 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5909 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5910 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5911 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5912 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5913 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5914 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5915 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
5916 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
5917 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
5918 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
5919 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5920 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5921 { "autorun", setautorun, 0 },
5922 { "layout", setlayout, 0 },
5927 conf_load(char *filename, int keymapping)
5930 char *line, *cp, *optsub, *optval;
5931 size_t linelen, lineno = 0;
5932 int wordlen, i, optind;
5933 struct config_option *opt;
5935 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5937 if (filename == NULL) {
5938 warnx("conf_load: no filename");
5941 if ((config = fopen(filename, "r")) == NULL) {
5942 warn("conf_load: fopen: %s", filename);
5946 while (!feof(config)) {
5947 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5950 err(1, "%s", filename);
5955 cp += strspn(cp, " \t\n"); /* eat whitespace */
5956 if (cp[0] == '\0') {
5961 /* get config option */
5962 wordlen = strcspn(cp, "=[ \t\n");
5964 warnx("%s: line %zd: no option found",
5969 for (i = 0; i < LENGTH(configopt); i++) {
5970 opt = &configopt[i];
5971 if (!strncasecmp(cp, opt->optname, wordlen) &&
5972 strlen(opt->optname) == wordlen) {
5978 warnx("%s: line %zd: unknown option %.*s",
5979 filename, lineno, wordlen, cp);
5982 if (keymapping && strcmp(opt->optname, "bind")) {
5983 warnx("%s: line %zd: invalid option %.*s",
5984 filename, lineno, wordlen, cp);
5988 cp += strspn(cp, " \t\n"); /* eat whitespace */
5989 /* get [selector] if any */
5993 wordlen = strcspn(cp, "]");
5996 warnx("%s: line %zd: syntax error",
6001 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6003 warnx("%s: line %zd: unable to allocate"
6004 "memory for selector", filename,
6010 cp += strspn(cp, "] \t\n"); /* eat trailing */
6012 cp += strspn(cp, "= \t\n"); /* eat trailing */
6014 optval = strdup(cp);
6015 /* call function to deal with it all */
6016 if (configopt[optind].func(optsub, optval,
6017 configopt[optind].funcflags) != 0)
6018 errx(1, "%s: line %zd: invalid data for %s",
6019 filename, lineno, configopt[optind].optname);
6026 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6033 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6039 set_child_transient(struct ws_win *win, Window *trans)
6041 struct ws_win *parent, *w;
6042 XWMHints *wmh = NULL;
6043 struct swm_region *r;
6044 struct workspace *ws;
6046 parent = find_window(win->transient);
6048 parent->child_trans = win;
6050 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6051 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
6053 if (win->hints == NULL) {
6054 warnx("no hints for 0x%lx", win->id);
6058 r = root_to_region(win->wa.root);
6060 /* parent doen't exist in our window list */
6061 TAILQ_FOREACH(w, &ws->winlist, entry) {
6065 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
6066 warnx("can't get hints for 0x%lx", w->id);
6070 if (win->hints->window_group != wmh->window_group)
6073 w->child_trans = win;
6074 win->transient = w->id;
6076 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
6077 "transient to 0x%lx\n", win->transient);
6087 window_get_pid(Window win)
6089 Atom actual_type_return;
6090 int actual_format_return = 0;
6091 unsigned long nitems_return = 0;
6092 unsigned long bytes_after_return = 0;
6096 unsigned char *prop = NULL;
6098 if (XGetWindowProperty(display, win,
6099 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
6100 XA_CARDINAL, &actual_type_return, &actual_format_return,
6101 &nitems_return, &bytes_after_return,
6102 (unsigned char**)(void*)&pid) != Success)
6104 if (actual_type_return != XA_CARDINAL)
6115 if (XGetWindowProperty(display, win,
6116 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
6117 XA_STRING, &actual_type_return, &actual_format_return,
6118 &nitems_return, &bytes_after_return, &prop) != Success)
6120 if (actual_type_return != XA_STRING)
6125 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
6126 /* ignore error because strtonum returns 0 anyway */
6133 manage_window(Window id)
6136 struct workspace *ws;
6137 struct ws_win *win, *ww;
6138 int format, i, ws_idx, n, border_me = 0;
6139 unsigned long nitems, bytes;
6140 Atom ws_idx_atom = 0, type;
6141 Atom *prot = NULL, *pp;
6142 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6143 struct swm_region *r;
6150 if ((win = find_window(id)) != NULL)
6151 return (win); /* already being managed */
6153 /* see if we are on the unmanaged list */
6154 if ((win = find_unmanaged_window(id)) != NULL) {
6155 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6156 "window: 0x%lx\n", win->id);
6157 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6159 set_child_transient(win, &trans);
6161 if (trans && (ww = find_window(trans)))
6162 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6163 else if ((ww = win->ws->focus) &&
6164 spawn_position == SWM_STACK_ABOVE)
6165 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6166 else if (ww && spawn_position == SWM_STACK_BELOW)
6167 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6168 else switch (spawn_position) {
6171 case SWM_STACK_ABOVE:
6172 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6174 case SWM_STACK_BOTTOM:
6175 case SWM_STACK_BELOW:
6176 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6179 ewmh_update_actions(win);
6183 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6184 err(1, "manage_window: calloc: failed to allocate memory for "
6189 /* see if we need to override the workspace */
6190 p = find_pid(window_get_pid(id));
6192 /* Get all the window data in one shot */
6193 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
6195 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
6196 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
6198 XGetWindowAttributes(display, id, &win->wa);
6199 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6200 win->hints = XGetWMHints(display, id);
6201 XGetTransientForHint(display, id, &trans);
6203 win->transient = trans;
6204 set_child_transient(win, &trans);
6205 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
6206 "transient: 0x%lx\n", win->id, win->transient);
6209 /* get supported protocols */
6210 if (XGetWMProtocols(display, id, &prot, &n)) {
6211 for (i = 0, pp = prot; i < n; i++, pp++) {
6212 if (*pp == takefocus)
6213 win->take_focus = 1;
6215 win->can_delete = 1;
6221 win->iconic = get_iconic(win);
6224 * Figure out where to put the window. If it was previously assigned to
6225 * a workspace (either by spawn() or manually moving), and isn't
6226 * transient, * put it in the same workspace
6228 r = root_to_region(win->wa.root);
6230 ws = &r->s->ws[p->ws];
6231 TAILQ_REMOVE(&pidlist, p, entry);
6234 } else if (prop && win->transient == 0) {
6235 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6236 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
6239 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6242 ws = &r->s->ws[ws_idx];
6245 /* this should launch transients in the same ws as parent */
6247 if ((ww = find_window(trans)) != NULL)
6253 warnx("manage_window: fix this "
6259 /* set up the window layout */
6262 win->s = r->s; /* this never changes */
6263 if (trans && (ww = find_window(trans)))
6264 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6265 else if (spawn_position == SWM_STACK_ABOVE && win->ws->focus)
6266 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6268 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
6270 WIDTH(win) = win->wa.width;
6271 HEIGHT(win) = win->wa.height;
6274 win->g_floatvalid = 0;
6275 win->floatmaxed = 0;
6276 win->ewmh_flags = 0;
6278 /* Set window properties so we can remember this after reincarnation */
6279 if (ws_idx_atom && prop == NULL &&
6280 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6282 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6284 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
6285 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
6290 ewmh_autoquirk(win);
6292 if (XGetClassHint(display, win->id, &win->ch)) {
6293 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6294 win->ch.res_class, win->ch.res_name);
6296 /* java is retarded so treat it special */
6297 if (strstr(win->ch.res_name, "sun-awt")) {
6302 TAILQ_FOREACH(qp, &quirks, entry) {
6303 if (!strcmp(win->ch.res_class, qp->class) &&
6304 !strcmp(win->ch.res_name, qp->name)) {
6305 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6306 "class: %s, name: %s\n", win->ch.res_class,
6308 if (qp->quirk & SWM_Q_FLOAT) {
6312 win->quirks = qp->quirk;
6317 /* alter window position if quirky */
6318 if (win->quirks & SWM_Q_ANYWHERE) {
6319 win->manual = 1; /* don't center the quirky windows */
6320 bzero(&wc, sizeof wc);
6322 if (bar_enabled && Y(win) < bar_height) {
6323 Y(win) = wc.y = bar_height;
6326 if (WIDTH(win) + X(win) > WIDTH(r)) {
6327 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
6333 /* Reset font sizes (the bruteforce way; no default keybinding). */
6334 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6335 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6336 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6337 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6338 fake_keypress(win, XK_KP_Add, ShiftMask);
6341 ewmh_get_win_state(win);
6342 ewmh_update_actions(win);
6343 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6347 bzero(&wc, sizeof wc);
6348 wc.border_width = border_width;
6349 mask |= CWBorderWidth;
6350 XConfigureWindow(display, win->id, mask, &wc);
6353 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
6354 PropertyChangeMask | StructureNotifyMask);
6356 /* floaters need to be mapped if they are in the current workspace */
6357 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6358 XMapRaised(display, win->id);
6364 free_window(struct ws_win *win)
6366 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
6371 /* needed for restart wm */
6372 set_win_state(win, WithdrawnState);
6374 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6376 if (win->ch.res_class)
6377 XFree(win->ch.res_class);
6378 if (win->ch.res_name)
6379 XFree(win->ch.res_name);
6384 memset(win, 0xff, sizeof *win); /* XXX kill later */
6390 unmanage_window(struct ws_win *win)
6392 struct ws_win *parent;
6397 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
6399 if (win->transient) {
6400 parent = find_window(win->transient);
6402 parent->child_trans = NULL;
6405 /* focus on root just in case */
6406 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6415 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6416 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6420 focus_magic(struct ws_win *win)
6422 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6425 /* if there are no windows clear the status-bar */
6430 if (win->child_trans) {
6431 /* win = parent & has a transient so focus on that */
6433 focus_win(win->child_trans);
6434 if (win->child_trans->take_focus)
6435 client_msg(win, takefocus);
6437 /* make sure transient hasn't disappeared */
6438 if (validate_win(win->child_trans) == 0) {
6439 focus_win(win->child_trans);
6440 if (win->child_trans->take_focus)
6441 client_msg(win->child_trans, takefocus);
6443 win->child_trans = NULL;
6445 if (win->take_focus)
6446 client_msg(win, takefocus);
6452 if (win->take_focus)
6453 client_msg(win, takefocus);
6460 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6467 XKeyEvent *ev = &e->xkey;
6469 struct swm_region *r;
6471 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6472 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6474 if (keyfuncs[kp->funcid].func == NULL)
6477 r = root_to_region(ev->root);
6478 if (kp->funcid == kf_spawn_custom)
6479 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6481 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6485 buttonpress(XEvent *e)
6489 XButtonPressedEvent *ev = &e->xbutton;
6491 if ((win = find_window(ev->window)) == NULL)
6495 action = client_click;
6497 for (i = 0; i < LENGTH(buttons); i++)
6498 if (action == buttons[i].action && buttons[i].func &&
6499 buttons[i].button == ev->button &&
6500 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6501 buttons[i].func(win, &buttons[i].args);
6505 configurerequest(XEvent *e)
6507 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6512 if ((win = find_window(ev->window)) == NULL)
6513 if ((win = find_unmanaged_window(ev->window)) == NULL)
6516 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6517 ev->window, YESNO(new));
6520 bzero(&wc, sizeof wc);
6523 wc.width = ev->width;
6524 wc.height = ev->height;
6525 wc.border_width = ev->border_width;
6526 wc.sibling = ev->above;
6527 wc.stack_mode = ev->detail;
6528 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6530 config_win(win, ev);
6534 configurenotify(XEvent *e)
6538 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6539 e->xconfigure.window);
6541 win = find_window(e->xconfigure.window);
6543 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6547 if (focus_mode == SWM_FOCUS_DEFAULT)
6548 drain_enter_notify();
6553 destroynotify(XEvent *e)
6556 XDestroyWindowEvent *ev = &e->xdestroywindow;
6558 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6560 if ((win = find_window(ev->window)) == NULL) {
6561 if ((win = find_unmanaged_window(ev->window)) == NULL)
6567 /* make sure we focus on something */
6570 unmanage_window(win);
6572 if (focus_mode == SWM_FOCUS_DEFAULT)
6573 drain_enter_notify();
6578 enternotify(XEvent *e)
6580 XCrossingEvent *ev = &e->xcrossing;
6585 Window focus_return;
6586 int revert_to_return;
6588 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6589 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6590 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6591 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6593 if (ev->mode != NotifyNormal) {
6594 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6599 switch (focus_mode) {
6600 case SWM_FOCUS_DEFAULT:
6602 case SWM_FOCUS_FOLLOW:
6604 case SWM_FOCUS_SYNERGY:
6607 * all these checks need to be in this order because the
6608 * XCheckTypedWindowEvent relies on weeding out the previous events
6610 * making this code an option would enable a follow mouse for focus
6615 * state is set when we are switching workspaces and focus is set when
6616 * the window or a subwindow already has focus (occurs during restart).
6618 * Only honor the focus flag if last_focus_event is not FocusOut,
6619 * this allows spectrwm to continue to control focus when another
6620 * program is also playing with it.
6622 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6623 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6628 * happens when a window is created or destroyed and the border
6629 * crosses the mouse pointer and when switching ws
6631 * we need the subwindow test to see if we came from root in order
6632 * to give focus to floaters
6634 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6635 ev->subwindow == 0) {
6636 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6640 /* this window already has focus */
6641 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6642 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6646 /* this window is being deleted or moved to another ws */
6647 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6649 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6650 XPutBackEvent(display, &cne);
6654 if ((win = find_window(ev->window)) == NULL) {
6655 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6660 * In fullstack kill all enters unless they come from a different ws
6661 * (i.e. another region) or focus has been grabbed externally.
6663 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6664 last_focus_event != FocusOut) {
6665 XGetInputFocus(display, &focus_return, &revert_to_return);
6666 if ((w = find_window(focus_return)) == NULL ||
6668 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6676 if ((win = find_window(ev->window)) == NULL) {
6677 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6682 * if we have more enternotifies let them handle it in due time
6684 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6685 DNPRINTF(SWM_D_EVENT,
6686 "ignoring enternotify: got more enternotify\n");
6687 XPutBackEvent(display, &cne);
6694 /* lets us use one switch statement for arbitrary mode/detail combinations */
6695 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6698 focusevent(XEvent *e)
6702 u_int32_t mode_detail;
6703 XFocusChangeEvent *ev = &e->xfocus;
6705 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6706 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6707 ev->window, ev->mode, ev->detail);
6709 if (last_focus_event == ev->type) {
6710 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6714 last_focus_event = ev->type;
6715 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6717 switch (mode_detail) {
6718 /* synergy client focus operations */
6719 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6720 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6722 /* synergy server focus operations */
6723 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6725 /* Entering applications like rdesktop that mangle the pointer */
6726 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6728 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6729 XSetWindowBorder(display, win->id,
6730 win->ws->r->s->c[ev->type == FocusIn ?
6731 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6734 warnx("ignoring focusevent");
6735 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6742 mapnotify(XEvent *e)
6745 XMapEvent *ev = &e->xmap;
6747 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6749 win = manage_window(ev->window);
6751 set_win_state(win, NormalState);
6754 * focus_win can only set input focus on a mapped window.
6755 * make sure the window really has focus since it is just being mapped.
6757 if (win->ws->focus == win)
6762 mappingnotify(XEvent *e)
6764 XMappingEvent *ev = &e->xmapping;
6766 XRefreshKeyboardMapping(ev);
6767 if (ev->request == MappingKeyboard)
6772 maprequest(XEvent *e)
6775 struct swm_region *r;
6776 XWindowAttributes wa;
6777 XMapRequestEvent *ev = &e->xmaprequest;
6779 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6780 e->xmaprequest.window);
6782 if (!XGetWindowAttributes(display, ev->window, &wa))
6784 if (wa.override_redirect)
6787 win = manage_window(e->xmaprequest.window);
6789 return; /* can't happen */
6793 /* make new win focused */
6794 r = root_to_region(win->wa.root);
6795 if (win->ws == r->ws)
6800 propertynotify(XEvent *e)
6803 XPropertyEvent *ev = &e->xproperty;
6806 name = XGetAtomName(display, ev->atom);
6807 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6812 win = find_window(ev->window);
6816 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6817 update_iconic(win, 0);
6818 XMapRaised(display, win->id);
6826 case XA_WM_NORMAL_HINTS:
6828 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6829 warnx("normal hints: flag 0x%x", win->sh.flags);
6830 if (win->sh.flags & PMinSize) {
6831 WIDTH(win) = win->sh.min_width;
6832 HEIGHT(win) = win->sh.min_height;
6833 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6835 XMoveResizeWindow(display, win->id,
6836 X(win), Y(win), WIDTH(win), HEIGHT(win));
6848 unmapnotify(XEvent *e)
6852 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6854 /* determine if we need to help unmanage this window */
6855 win = find_window(e->xunmap.window);
6859 if (getstate(e->xunmap.window) == NormalState) {
6860 unmanage_window(win);
6863 /* giant hack for apps that don't destroy transient windows */
6864 /* eat a bunch of events to prevent remanaging the window */
6866 while (XCheckWindowEvent(display, e->xunmap.window,
6867 EnterWindowMask, &cne))
6869 while (XCheckWindowEvent(display, e->xunmap.window,
6870 StructureNotifyMask, &cne))
6872 while (XCheckWindowEvent(display, e->xunmap.window,
6873 SubstructureNotifyMask, &cne))
6875 /* resend unmap because we ated it */
6876 XUnmapWindow(display, e->xunmap.window);
6879 if (focus_mode == SWM_FOCUS_DEFAULT)
6880 drain_enter_notify();
6884 visibilitynotify(XEvent *e)
6887 struct swm_region *r;
6889 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6890 e->xvisibility.window);
6891 if (e->xvisibility.state == VisibilityUnobscured)
6892 for (i = 0; i < ScreenCount(display); i++)
6893 TAILQ_FOREACH(r, &screens[i].rl, entry)
6894 if (e->xvisibility.window == r->bar_window)
6899 clientmessage(XEvent *e)
6901 XClientMessageEvent *ev;
6906 win = find_window(ev->window);
6910 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6911 ev->window, ev->message_type);
6913 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6914 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6917 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6918 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6919 if (win->can_delete)
6920 client_msg(win, adelete);
6922 XKillClient(display, win->id);
6924 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6925 DNPRINTF(SWM_D_EVENT,
6926 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6927 if (win->floating) {
6928 if (ev->data.l[0] & (1<<8)) /* x */
6929 X(win) = ev->data.l[1];
6930 if (ev->data.l[0] & (1<<9)) /* y */
6931 Y(win) = ev->data.l[2];
6932 if (ev->data.l[0] & (1<<10)) /* width */
6933 WIDTH(win) = ev->data.l[3];
6934 if (ev->data.l[0] & (1<<11)) /* height */
6935 HEIGHT(win) = ev->data.l[4];
6940 /* TODO: Change stack sizes */
6941 /* notify no change was made. */
6942 config_win(win, NULL);
6945 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6946 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6947 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6949 ewmh_update_win_state(win, ev->data.l[2],
6957 xerror_start(Display *d, XErrorEvent *ee)
6964 xerror(Display *d, XErrorEvent *ee)
6966 /* warnx("error: %p %p", display, ee); */
6974 xerrorxlib = XSetErrorHandler(xerror_start);
6976 /* this causes an error if some other window manager is running */
6977 XSelectInput(display, DefaultRootWindow(display),
6978 SubstructureRedirectMask);
6979 XSync(display, False);
6983 XSetErrorHandler(xerror);
6984 XSync(display, False);
6989 new_region(struct swm_screen *s, int x, int y, int w, int h)
6991 struct swm_region *r, *n;
6992 struct workspace *ws = NULL;
6995 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6996 s->idx, w, h, x, y);
6998 /* remove any conflicting regions */
6999 n = TAILQ_FIRST(&s->rl);
7002 n = TAILQ_NEXT(r, entry);
7003 if (X(r) < (x + w) &&
7004 (X(r) + WIDTH(r)) > x &&
7006 (Y(r) + HEIGHT(r)) > y) {
7007 if (r->ws->r != NULL)
7008 r->ws->old_r = r->ws->r;
7010 XDestroyWindow(display, r->bar_window);
7011 TAILQ_REMOVE(&s->rl, r, entry);
7012 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7016 /* search old regions for one to reuse */
7018 /* size + location match */
7019 TAILQ_FOREACH(r, &s->orl, entry)
7020 if (X(r) == x && Y(r) == y &&
7021 HEIGHT(r) == h && WIDTH(r) == w)
7025 TAILQ_FOREACH(r, &s->orl, entry)
7026 if (HEIGHT(r) == h && WIDTH(r) == w)
7030 TAILQ_REMOVE(&s->orl, r, entry);
7031 /* try to use old region's workspace */
7032 if (r->ws->r == NULL)
7035 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7036 err(1, "new_region: calloc: failed to allocate memory "
7039 /* if we don't have a workspace already, find one */
7041 for (i = 0; i < workspace_limit; i++)
7042 if (s->ws[i].r == NULL) {
7049 errx(1, "new_region: no free workspaces");
7060 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7066 #ifdef SWM_XRR_HAS_CRTC
7068 XRRScreenResources *sr;
7071 #endif /* SWM_XRR_HAS_CRTC */
7072 struct swm_region *r;
7075 if (i >= ScreenCount(display))
7076 errx(1, "scan_xrandr: invalid screen");
7078 /* remove any old regions */
7079 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7080 r->ws->old_r = r->ws->r = NULL;
7081 XDestroyWindow(display, r->bar_window);
7082 TAILQ_REMOVE(&screens[i].rl, r, entry);
7083 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7087 /* map virtual screens onto physical screens */
7088 #ifdef SWM_XRR_HAS_CRTC
7089 if (xrandr_support) {
7090 sr = XRRGetScreenResources(display, screens[i].root);
7092 new_region(&screens[i], 0, 0,
7093 DisplayWidth(display, i),
7094 DisplayHeight(display, i));
7098 for (c = 0, ci = NULL; c < ncrtc; c++) {
7099 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
7100 if (ci->noutput == 0)
7103 if (ci != NULL && ci->mode == None)
7104 new_region(&screens[i], 0, 0,
7105 DisplayWidth(display, i),
7106 DisplayHeight(display, i));
7108 new_region(&screens[i],
7109 ci->x, ci->y, ci->width, ci->height);
7112 XRRFreeCrtcInfo(ci);
7113 XRRFreeScreenResources(sr);
7115 #endif /* SWM_XRR_HAS_CRTC */
7117 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
7118 DisplayHeight(display, i));
7123 screenchange(XEvent *e) {
7124 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7125 struct swm_region *r;
7128 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7130 if (!XRRUpdateConfiguration(e))
7133 /* silly event doesn't include the screen index */
7134 for (i = 0; i < ScreenCount(display); i++)
7135 if (screens[i].root == xe->root)
7137 if (i >= ScreenCount(display))
7138 errx(1, "screenchange: screen not found");
7140 /* brute force for now, just re-enumerate the regions */
7143 /* add bars to all regions */
7144 for (i = 0; i < ScreenCount(display); i++)
7145 TAILQ_FOREACH(r, &screens[i].rl, entry)
7148 if (focus_mode == SWM_FOCUS_DEFAULT)
7149 drain_enter_notify();
7155 Window d1, d2, *wins = NULL;
7156 XWindowAttributes wa;
7161 for (i = 0; i < ScreenCount(display); i++) {
7162 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
7165 /* attach windows to a region */
7166 /* normal windows */
7167 for (j = 0; j < no; j++) {
7168 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7169 wa.override_redirect ||
7170 XGetTransientForHint(display, wins[j], &d1))
7173 state = getstate(wins[j]);
7174 manage = state == IconicState;
7175 if (wa.map_state == IsViewable || manage)
7176 manage_window(wins[j]);
7178 /* transient windows */
7179 for (j = 0; j < no; j++) {
7180 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7181 wa.override_redirect)
7184 state = getstate(wins[j]);
7185 manage = state == IconicState;
7186 if (XGetTransientForHint(display, wins[j], &d1) &&
7188 manage_window(wins[j]);
7201 int errorbase, major, minor;
7202 struct workspace *ws;
7205 if ((screens = calloc(ScreenCount(display),
7206 sizeof(struct swm_screen))) == NULL)
7207 err(1, "setup_screens: calloc: failed to allocate memory for "
7210 /* initial Xrandr setup */
7211 xrandr_support = XRRQueryExtension(display,
7212 &xrandr_eventbase, &errorbase);
7214 if (XRRQueryVersion(display, &major, &minor) && major < 1)
7217 /* map physical screens */
7218 for (i = 0; i < ScreenCount(display); i++) {
7219 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7221 TAILQ_INIT(&screens[i].rl);
7222 TAILQ_INIT(&screens[i].orl);
7223 screens[i].root = RootWindow(display, i);
7225 /* set default colors */
7226 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7227 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7228 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7229 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7230 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7232 /* create graphics context on screen with default font color */
7233 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
7236 XSetForeground(display, screens[i].bar_gc,
7237 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7239 /* set default cursor */
7240 XDefineCursor(display, screens[i].root,
7241 XCreateFontCursor(display, XC_left_ptr));
7243 /* init all workspaces */
7244 /* XXX these should be dynamically allocated too */
7245 for (j = 0; j < SWM_WS_MAX; j++) {
7246 ws = &screens[i].ws[j];
7252 TAILQ_INIT(&ws->winlist);
7253 TAILQ_INIT(&ws->unmanagedlist);
7255 for (k = 0; layouts[k].l_stack != NULL; k++)
7256 if (layouts[k].l_config != NULL)
7257 layouts[k].l_config(ws,
7258 SWM_ARG_ID_STACKINIT);
7259 ws->cur_layout = &layouts[0];
7260 ws->cur_layout->l_string(ws);
7266 XRRSelectInput(display, screens[i].root,
7267 RRScreenChangeNotifyMask);
7274 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7275 err(1, "setup_globals: strdup: failed to allocate memory.");
7277 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7278 err(1, "setup_globals: strdup: failed to allocate memory.");
7285 Atom netwmcheck, netwmname, utf8_string;
7288 /* work around sun jdk bugs, code from wmname */
7289 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
7290 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7291 utf8_string = XInternAtom(display, "UTF8_STRING", False);
7292 for (i = 0; i < ScreenCount(display); i++) {
7293 root = screens[i].root;
7294 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
7295 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
7296 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
7298 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
7299 PropModeReplace, (unsigned char *)&win, 1);
7300 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
7301 PropModeReplace, (unsigned char *)&win, 1);
7302 XChangeProperty(display, win, netwmname, utf8_string, 8,
7303 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
7308 main(int argc, char *argv[])
7310 struct swm_region *r, *rr;
7311 struct ws_win *winfocus = NULL;
7314 char conf[PATH_MAX], *cfile = NULL;
7319 struct sigaction sact;
7322 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7323 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7325 warnx("no locale support");
7327 if (!X_HAVE_UTF8_STRING)
7328 warnx("no UTF-8 support");
7330 if (!(display = XOpenDisplay(0)))
7331 errx(1, "can not open display");
7334 errx(1, "other wm running");
7336 /* handle some signals */
7337 bzero(&sact, sizeof(sact));
7338 sigemptyset(&sact.sa_mask);
7340 sact.sa_handler = sighdlr;
7341 sigaction(SIGINT, &sact, NULL);
7342 sigaction(SIGQUIT, &sact, NULL);
7343 sigaction(SIGTERM, &sact, NULL);
7344 sigaction(SIGHUP, &sact, NULL);
7346 sact.sa_handler = sighdlr;
7347 sact.sa_flags = SA_NOCLDSTOP;
7348 sigaction(SIGCHLD, &sact, NULL);
7350 astate = XInternAtom(display, "WM_STATE", False);
7351 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
7352 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
7353 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
7354 a_wmname = XInternAtom(display, "WM_NAME", False);
7355 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7356 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
7357 a_string = XInternAtom(display, "STRING", False);
7358 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
7360 /* look for local and global conf file */
7361 pwd = getpwuid(getuid());
7363 errx(1, "invalid user: %d", getuid());
7372 for (i = 0; ; i++) {
7377 snprintf(conf, sizeof conf, "%s/.%s",
7378 pwd->pw_dir, SWM_CONF_FILE);
7382 snprintf(conf, sizeof conf, "/etc/%s",
7387 snprintf(conf, sizeof conf, "%s/.%s",
7388 pwd->pw_dir, SWM_CONF_FILE_OLD);
7392 snprintf(conf, sizeof conf, "/etc/%s",
7399 if (strlen(conf) && stat(conf, &sb) != -1)
7400 if (S_ISREG(sb.st_mode)) {
7407 /* load conf (if any) and refresh font color in bar graphics contexts */
7408 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
7409 for (i = 0; i < ScreenCount(display); ++i)
7410 XSetForeground(display, screens[i].bar_gc,
7411 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7414 /* set some values to work around bad programs */
7416 /* grab existing windows (before we build the bars) */
7419 if (getenv("SWM_STARTED") == NULL)
7420 setenv("SWM_STARTED", "YES", 1);
7422 /* setup all bars */
7423 for (i = 0; i < ScreenCount(display); i++)
7424 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7425 if (winfocus == NULL)
7426 winfocus = TAILQ_FIRST(&r->ws->winlist);
7434 if (focus_mode == SWM_FOCUS_DEFAULT)
7435 drain_enter_notify();
7437 xfd = ConnectionNumber(display);
7439 while (XPending(display)) {
7440 XNextEvent(display, &e);
7443 if (e.type < LASTEvent) {
7444 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7445 "window: 0x%lx, type: %s (%d), %d remaining"
7446 "\n", YESNO(handler[e.type]),
7447 e.xany.window, geteventname(&e),
7448 e.type, QLength(display));
7450 if (handler[e.type])
7451 handler[e.type](&e);
7453 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7454 "0x%lx, type: %s (%d)\n", e.xany.window,
7455 xrandr_geteventname(&e), e.type);
7457 switch (e.type - xrandr_eventbase) {
7458 case RRScreenChangeNotify:
7467 /* if we are being restarted go focus on first window */
7469 rr = winfocus->ws->r;
7471 /* not a visible window */
7475 /* move pointer to first screen if multi screen */
7476 if (ScreenCount(display) > 1 || outputs > 1)
7477 XWarpPointer(display, None, rr->s[0].root,
7479 Y(rr) + (bar_enabled ? bar_height : 0));
7481 a.id = SWM_ARG_ID_FOCUSCUR;
7491 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7493 DNPRINTF(SWM_D_MISC, "select failed");
7494 if (restart_wm == 1)
7495 restart(NULL, NULL);
7496 if (search_resp == 1)
7509 for (i = 0; i < ScreenCount(display); ++i)
7510 if (screens[i].bar_gc != NULL)
7511 XFreeGC(display, screens[i].bar_gc);
7513 XFreeFontSet(display, bar_fs);
7514 XCloseDisplay(display);