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>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
77 #if defined(__linux__)
78 #include "linux/tree.h"
79 #elif defined(__OpenBSD__)
83 #include <X11/cursorfont.h>
84 #include <X11/keysym.h>
85 #include <X11/Xatom.h>
87 #include <X11/Xproto.h>
88 #include <X11/Xutil.h>
89 #include <X11/extensions/Xrandr.h>
90 #include <X11/extensions/XTest.h>
98 #ifdef SCROTWM_BUILDSTR
99 static const char *buildstr = SCROTWM_BUILDSTR;
101 static const char *buildstr = SCROTWM_VERSION;
105 # error XRandR versions less than 1.0 are not supported
110 #define SWM_XRR_HAS_CRTC
114 /*#define SWM_DEBUG*/
116 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
117 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
118 #define SWM_D_MISC 0x0001
119 #define SWM_D_EVENT 0x0002
120 #define SWM_D_WS 0x0004
121 #define SWM_D_FOCUS 0x0008
122 #define SWM_D_MOVE 0x0010
123 #define SWM_D_STACK 0x0020
124 #define SWM_D_MOUSE 0x0040
125 #define SWM_D_PROP 0x0080
126 #define SWM_D_CLASS 0x0100
127 #define SWM_D_KEY 0x0200
128 #define SWM_D_QUIRK 0x0400
129 #define SWM_D_SPAWN 0x0800
130 #define SWM_D_EVENTQ 0x1000
131 #define SWM_D_CONF 0x2000
132 #define SWM_D_BAR 0x4000
134 u_int32_t swm_debug = 0
152 #define DPRINTF(x...)
153 #define DNPRINTF(n,x...)
156 #define LENGTH(x) (sizeof x / sizeof x[0])
157 #define MODKEY Mod1Mask
158 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
159 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
160 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
161 #define SWM_PROPLEN (16)
162 #define SWM_FUNCNAME_LEN (32)
163 #define SWM_KEYS_LEN (255)
164 #define SWM_QUIRK_LEN (64)
165 #define X(r) (r)->g.x
166 #define Y(r) (r)->g.y
167 #define WIDTH(r) (r)->g.w
168 #define HEIGHT(r) (r)->g.h
169 #define SH_MIN(w) (w)->sh_mask & PMinSize
170 #define SH_MIN_W(w) (w)->sh.min_width
171 #define SH_MIN_H(w) (w)->sh.min_height
172 #define SH_MAX(w) (w)->sh_mask & PMaxSize
173 #define SH_MAX_W(w) (w)->sh.max_width
174 #define SH_MAX_H(w) (w)->sh.max_height
175 #define SH_INC(w) (w)->sh_mask & PResizeInc
176 #define SH_INC_W(w) (w)->sh.width_inc
177 #define SH_INC_H(w) (w)->sh.height_inc
178 #define SWM_MAX_FONT_STEPS (3)
179 #define WINID(w) ((w) ? (w)->id : 0)
180 #define YESNO(x) ((x) ? "yes" : "no")
182 #define SWM_FOCUS_DEFAULT (0)
183 #define SWM_FOCUS_SYNERGY (1)
184 #define SWM_FOCUS_FOLLOW (2)
186 #define SWM_CONF_DEFAULT (0)
187 #define SWM_CONF_KEYMAPPING (1)
190 #define SWM_LIB "/usr/local/lib/libswmhack.so"
203 volatile sig_atomic_t running = 1;
204 volatile sig_atomic_t restart_wm = 0;
206 int last_focus_event = FocusOut;
207 int (*xerrorxlib)(Display *, XErrorEvent *);
211 int xrandr_eventbase;
212 unsigned int numlockmask = 0;
216 int cycle_visible = 0;
218 int font_adjusted = 0;
219 unsigned int mod_key = MODKEY;
222 struct swm_region *search_r;
223 int select_list_pipe[2];
224 int select_resp_pipe[2];
226 volatile sig_atomic_t search_resp;
227 int search_resp_action;
229 struct search_window {
230 TAILQ_ENTRY(search_window) entry;
236 TAILQ_HEAD(search_winlist, search_window);
238 struct search_winlist search_wl;
243 SWM_SEARCH_UNICONIFY,
244 SWM_SEARCH_NAME_WORKSPACE,
245 SWM_SEARCH_SEARCH_WORKSPACE,
246 SWM_SEARCH_SEARCH_WINDOW
250 double dialog_ratio = 0.6;
252 #define SWM_BAR_MAX (256)
253 #define SWM_BAR_JUSTIFY_LEFT (0)
254 #define SWM_BAR_JUSTIFY_CENTER (1)
255 #define SWM_BAR_JUSTIFY_RIGHT (2)
256 #define SWM_BAR_OFFSET (4)
257 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
258 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
259 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
260 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
262 #ifdef X_HAVE_UTF8_STRING
263 #define DRAWSTRING(x...) Xutf8DrawString(x)
265 #define DRAWSTRING(x...) XmbDrawString(x)
268 char *bar_argv[] = { NULL, NULL };
270 unsigned char bar_ext[SWM_BAR_MAX];
271 char bar_vertext[SWM_BAR_MAX];
273 sig_atomic_t bar_alarm = 0;
276 int bar_border_width = 1;
277 int bar_at_bottom = 0;
279 int bar_extra_running = 0;
282 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
283 int stack_enabled = 1;
284 int clock_enabled = 1;
285 int urgent_enabled = 0;
286 char *clock_format = NULL;
287 int title_name_enabled = 0;
288 int title_class_enabled = 0;
289 int window_name_enabled = 0;
290 int focus_mode = SWM_FOCUS_DEFAULT;
291 int disable_border = 0;
292 int border_width = 1;
293 int verbose_layout = 0;
296 XFontSetExtents *bar_fs_extents;
298 char *spawn_term[] = { NULL, NULL }; /* XXX fully dynamic */
301 #define SWM_MENU_FN (2)
302 #define SWM_MENU_NB (4)
303 #define SWM_MENU_NF (6)
304 #define SWM_MENU_SB (8)
305 #define SWM_MENU_SF (10)
307 /* layout manager data */
308 struct swm_geometry {
318 /* virtual "screens" */
320 TAILQ_ENTRY(swm_region) entry;
321 struct swm_geometry g;
322 struct workspace *ws; /* current workspace on this region */
323 struct workspace *ws_prior; /* prior workspace on this region */
324 struct swm_screen *s; /* screen idx */
327 TAILQ_HEAD(swm_region_list, swm_region);
330 TAILQ_ENTRY(ws_win) entry;
333 struct ws_win *child_trans; /* transient child window */
334 struct swm_geometry g; /* current geometry */
335 struct swm_geometry g_float; /* geometry when floating */
336 struct swm_geometry rg_float; /* region geom when floating */
337 int g_floatvalid; /* g_float geometry validity */
338 int floatmaxed; /* whether maxed by max_stack */
342 unsigned int ewmh_flags;
343 int font_size_boundary[SWM_MAX_FONT_STEPS];
349 unsigned long quirks;
350 struct workspace *ws; /* always valid */
351 struct swm_screen *s; /* always valid, never changes */
352 XWindowAttributes wa;
358 TAILQ_HEAD(ws_win_list, ws_win);
362 TAILQ_ENTRY(pid_e) entry;
366 TAILQ_HEAD(pid_list, pid_e);
367 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
369 /* layout handlers */
371 void vertical_config(struct workspace *, int);
372 void vertical_stack(struct workspace *, struct swm_geometry *);
373 void horizontal_config(struct workspace *, int);
374 void horizontal_stack(struct workspace *, struct swm_geometry *);
375 void max_stack(struct workspace *, struct swm_geometry *);
376 void plain_stacker(struct workspace *);
377 void fancy_stacker(struct workspace *);
379 struct ws_win *find_window(Window);
381 void grabbuttons(struct ws_win *, int);
382 void new_region(struct swm_screen *, int, int, int, int);
383 void unmanage_window(struct ws_win *);
384 long getstate(Window);
386 int conf_load(char *, int);
389 void (*l_stack)(struct workspace *, struct swm_geometry *);
390 void (*l_config)(struct workspace *, int);
392 #define SWM_L_FOCUSPREV (1<<0)
393 #define SWM_L_MAPONFOCUS (1<<1)
394 void (*l_string)(struct workspace *);
396 /* stack, configure */
397 { vertical_stack, vertical_config, 0, plain_stacker },
398 { horizontal_stack, horizontal_config, 0, plain_stacker },
400 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
401 { NULL, NULL, 0, NULL },
404 /* position of max_stack mode in the layouts array, index into layouts! */
405 #define SWM_V_STACK (0)
406 #define SWM_H_STACK (1)
407 #define SWM_MAX_STACK (2)
409 #define SWM_H_SLICE (32)
410 #define SWM_V_SLICE (32)
412 /* define work spaces */
414 int idx; /* workspace index */
415 char *name; /* workspace name */
416 int always_raise; /* raise windows on focus */
417 struct layout *cur_layout; /* current layout handlers */
418 struct ws_win *focus; /* may be NULL */
419 struct ws_win *focus_prev; /* may be NULL */
420 struct swm_region *r; /* may be NULL */
421 struct swm_region *old_r; /* may be NULL */
422 struct ws_win_list winlist; /* list of windows in ws */
423 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
424 char stacker[10]; /* display stacker and layout */
428 int horizontal_msize;
430 int horizontal_stacks;
439 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
440 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
442 /* physical screen mapping */
443 #define SWM_WS_MAX (10)
445 int idx; /* screen index */
446 struct swm_region_list rl; /* list of regions on this screen */
447 struct swm_region_list orl; /* list of old regions */
449 struct workspace ws[SWM_WS_MAX];
455 } c[SWM_S_COLOR_MAX];
459 struct swm_screen *screens;
462 /* args to functions */
465 #define SWM_ARG_ID_FOCUSNEXT (0)
466 #define SWM_ARG_ID_FOCUSPREV (1)
467 #define SWM_ARG_ID_FOCUSMAIN (2)
468 #define SWM_ARG_ID_FOCUSCUR (4)
469 #define SWM_ARG_ID_SWAPNEXT (10)
470 #define SWM_ARG_ID_SWAPPREV (11)
471 #define SWM_ARG_ID_SWAPMAIN (12)
472 #define SWM_ARG_ID_MOVELAST (13)
473 #define SWM_ARG_ID_MASTERSHRINK (20)
474 #define SWM_ARG_ID_MASTERGROW (21)
475 #define SWM_ARG_ID_MASTERADD (22)
476 #define SWM_ARG_ID_MASTERDEL (23)
477 #define SWM_ARG_ID_FLIPLAYOUT (24)
478 #define SWM_ARG_ID_STACKRESET (30)
479 #define SWM_ARG_ID_STACKINIT (31)
480 #define SWM_ARG_ID_CYCLEWS_UP (40)
481 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
482 #define SWM_ARG_ID_CYCLESC_UP (42)
483 #define SWM_ARG_ID_CYCLESC_DOWN (43)
484 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
485 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
486 #define SWM_ARG_ID_STACKINC (50)
487 #define SWM_ARG_ID_STACKDEC (51)
488 #define SWM_ARG_ID_SS_ALL (60)
489 #define SWM_ARG_ID_SS_WINDOW (61)
490 #define SWM_ARG_ID_DONTCENTER (70)
491 #define SWM_ARG_ID_CENTER (71)
492 #define SWM_ARG_ID_KILLWINDOW (80)
493 #define SWM_ARG_ID_DELETEWINDOW (81)
494 #define SWM_ARG_ID_WIDTHGROW (90)
495 #define SWM_ARG_ID_WIDTHSHRINK (91)
496 #define SWM_ARG_ID_HEIGHTGROW (92)
497 #define SWM_ARG_ID_HEIGHTSHRINK (93)
498 #define SWM_ARG_ID_MOVEUP (100)
499 #define SWM_ARG_ID_MOVEDOWN (101)
500 #define SWM_ARG_ID_MOVELEFT (102)
501 #define SWM_ARG_ID_MOVERIGHT (103)
505 void focus(struct swm_region *, union arg *);
506 void focus_magic(struct ws_win *);
510 TAILQ_ENTRY(quirk) entry;
514 #define SWM_Q_FLOAT (1<<0) /* float this window */
515 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
516 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
517 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
518 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
519 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
521 TAILQ_HEAD(quirk_list, quirk);
522 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
525 * Supported EWMH hints should be added to
526 * both the enum and the ewmh array
528 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
529 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
530 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
531 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
532 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
533 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
534 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
535 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
536 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
537 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
543 } ewmh[SWM_EWMH_HINT_MAX] = {
544 /* must be in same order as in the enum */
545 {"_NET_ACTIVE_WINDOW", None},
546 {"_NET_MOVERESIZE_WINDOW", None},
547 {"_NET_CLOSE_WINDOW", None},
548 {"_NET_WM_WINDOW_TYPE", None},
549 {"_NET_WM_WINDOW_TYPE_DOCK", None},
550 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
551 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
552 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
553 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
554 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
555 {"_NET_WM_STATE", None},
556 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
557 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
558 {"_NET_WM_STATE_SKIP_TASKBAR", None},
559 {"_NET_WM_STATE_SKIP_PAGER", None},
560 {"_NET_WM_STATE_HIDDEN", None},
561 {"_NET_WM_STATE_ABOVE", None},
562 {"_SWM_WM_STATE_MANUAL", None},
563 {"_NET_WM_STATE_FULLSCREEN", None},
564 {"_NET_WM_ALLOWED_ACTIONS", None},
565 {"_NET_WM_ACTION_MOVE", None},
566 {"_NET_WM_ACTION_RESIZE", None},
567 {"_NET_WM_ACTION_FULLSCREEN", None},
568 {"_NET_WM_ACTION_CLOSE", None},
571 void store_float_geom(struct ws_win *, struct swm_region *);
572 int floating_toggle_win(struct ws_win *);
573 void spawn_select(struct swm_region *, union arg *, char *, int *);
574 unsigned char *get_win_name(Window);
577 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
578 unsigned long *nbytes, unsigned char **data)
581 unsigned long *nbytes_ret, *nitems_ret;
582 unsigned long nbytes_tmp, nitems_tmp;
585 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
586 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
588 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
589 &real, &format, nitems_ret, nbytes_ret, data);
591 if (status != Success)
600 update_iconic(struct ws_win *win, int newv)
607 iprop = XInternAtom(display, "_SWM_ICONIC", False);
611 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
612 PropModeReplace, (unsigned char *)&v, 1);
614 XDeleteProperty(display, win->id, iprop);
618 get_iconic(struct ws_win *win)
623 unsigned long nitems, extra;
624 unsigned char *prop = NULL;
626 iprop = XInternAtom(display, "_SWM_ICONIC", False);
629 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
630 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
631 if (status != Success)
633 if (rettype != XA_INTEGER || retfmt != 32)
637 v = *((int32_t *)prop);
651 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
653 for (i = 0; i < LENGTH(ewmh); i++)
654 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
656 for (i = 0; i < ScreenCount(display); i++) {
657 /* Support check window will be created by workaround(). */
659 /* Report supported atoms */
660 XDeleteProperty(display, screens[i].root, sup_list);
661 for (j = 0; j < LENGTH(ewmh); j++)
662 XChangeProperty(display, screens[i].root,
663 sup_list, XA_ATOM, 32,
664 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
672 unsigned char *data = NULL;
674 Atom sup_check, sup_list;
677 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
678 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
680 for (i = 0; i < ScreenCount(display); i++) {
681 /* Get the support check window and destroy it */
682 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
687 XDestroyWindow(display, id);
688 XDeleteProperty(display, screens[i].root, sup_check);
689 XDeleteProperty(display, screens[i].root, sup_list);
697 ewmh_autoquirk(struct ws_win *win)
700 unsigned long *data = NULL, n;
703 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
704 XA_ATOM, &n, NULL, (void *)&data);
711 for (i = 0; i < n; i++) {
713 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
715 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
716 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
717 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
719 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
722 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
723 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
725 win->quirks = SWM_Q_FLOAT;
733 #define SWM_EWMH_ACTION_COUNT_MAX (6)
734 #define EWMH_F_FULLSCREEN (1<<0)
735 #define EWMH_F_ABOVE (1<<1)
736 #define EWMH_F_HIDDEN (1<<2)
737 #define EWMH_F_SKIP_PAGER (1<<3)
738 #define EWMH_F_SKIP_TASKBAR (1<<4)
739 #define SWM_F_MANUAL (1<<5)
742 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
744 struct swm_geometry rg;
752 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
753 "fullscreen %s\n", win->id, YESNO(fs));
758 store_float_geom(win, win->ws->r);
762 if (win->g_floatvalid) {
763 /* refloat at last floating relative position */
764 X(win) = win->g_float.x - win->rg_float.x + rg.x;
765 Y(win) = win->g_float.y - win->rg_float.y + rg.y;
766 WIDTH(win) = win->g_float.w;
767 HEIGHT(win) = win->g_float.h;
775 ewmh_update_actions(struct ws_win *win)
777 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
783 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
786 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
787 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
790 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
791 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
794 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
795 #define _NET_WM_STATE_ADD 1 /* add/set property */
796 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
799 ewmh_update_win_state(struct ws_win *win, long state, long action)
801 unsigned int mask = 0;
802 unsigned int changed = 0;
803 unsigned int orig_flags;
808 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
809 mask = EWMH_F_FULLSCREEN;
810 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
812 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
814 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
815 mask = EWMH_F_SKIP_PAGER;
816 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
817 mask = EWMH_F_SKIP_TASKBAR;
820 orig_flags = win->ewmh_flags;
823 case _NET_WM_STATE_REMOVE:
824 win->ewmh_flags &= ~mask;
826 case _NET_WM_STATE_ADD:
827 win->ewmh_flags |= mask;
829 case _NET_WM_STATE_TOGGLE:
830 win->ewmh_flags ^= mask;
834 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
836 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
838 if (!floating_toggle_win(win))
839 win->ewmh_flags = orig_flags; /* revert */
840 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
842 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
843 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
845 if (!ewmh_set_win_fullscreen(win,
846 win->ewmh_flags & EWMH_F_FULLSCREEN))
847 win->ewmh_flags = orig_flags; /* revert */
849 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
851 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
852 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
853 XA_ATOM, 32, PropModeAppend,
854 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
855 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
856 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
857 XA_ATOM, 32, PropModeAppend,
858 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
859 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
860 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
861 XA_ATOM, 32, PropModeAppend,
862 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
863 if (win->ewmh_flags & EWMH_F_ABOVE)
864 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
865 XA_ATOM, 32, PropModeAppend,
866 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
867 if (win->ewmh_flags & SWM_F_MANUAL)
868 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
869 XA_ATOM, 32, PropModeAppend,
870 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
874 ewmh_get_win_state(struct ws_win *win)
885 win->ewmh_flags |= EWMH_F_ABOVE;
887 win->ewmh_flags |= SWM_F_MANUAL;
889 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
890 (~0L), XA_ATOM, &n, NULL, (void *)&states);
895 for (i = 0; i < n; i++)
896 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
904 geteventname(XEvent *e)
916 name = "ButtonPress";
919 name = "ButtonRelease";
922 name = "MotionNotify";
925 name = "EnterNotify";
928 name = "LeaveNotify";
937 name = "KeymapNotify";
943 name = "GraphicsExpose";
948 case VisibilityNotify:
949 name = "VisibilityNotify";
952 name = "CreateNotify";
955 name = "DestroyNotify";
958 name = "UnmapNotify";
967 name = "ReparentNotify";
969 case ConfigureNotify:
970 name = "ConfigureNotify";
972 case ConfigureRequest:
973 name = "ConfigureRequest";
976 name = "GravityNotify";
979 name = "ResizeRequest";
981 case CirculateNotify:
982 name = "CirculateNotify";
984 case CirculateRequest:
985 name = "CirculateRequest";
988 name = "PropertyNotify";
991 name = "SelectionClear";
993 case SelectionRequest:
994 name = "SelectionRequest";
996 case SelectionNotify:
997 name = "SelectionNotify";
1000 name = "ColormapNotify";
1003 name = "ClientMessage";
1006 name = "MappingNotify";
1016 xrandr_geteventname(XEvent *e)
1020 switch(e->type - xrandr_eventbase) {
1021 case RRScreenChangeNotify:
1022 name = "RRScreenChangeNotify";
1032 dumpwins(struct swm_region *r, union arg *args)
1036 XWindowAttributes wa;
1038 if (r->ws == NULL) {
1039 warnx("dumpwins: invalid workspace");
1043 warnx("=== managed window list ws %02d ===", r->ws->idx);
1045 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1046 state = getstate(win->id);
1047 if (!XGetWindowAttributes(display, win->id, &wa))
1048 warnx("window: 0x%lx, failed XGetWindowAttributes",
1050 warnx("window: 0x%lx, map_state: %d, state: %d, "
1051 "transient: 0x%lx", win->id, wa.map_state, state,
1055 warnx("===== unmanaged window list =====");
1056 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1057 state = getstate(win->id);
1058 if (!XGetWindowAttributes(display, win->id, &wa))
1059 warnx("window: 0x%lx, failed XGetWindowAttributes",
1061 warnx("window: 0x%lx, map_state: %d, state: %d, "
1062 "transient: 0x%lx", win->id, wa.map_state, state,
1066 warnx("=================================");
1070 dumpwins(struct swm_region *r, union arg *args)
1073 #endif /* SWM_DEBUG */
1075 void expose(XEvent *);
1076 void keypress(XEvent *);
1077 void buttonpress(XEvent *);
1078 void configurerequest(XEvent *);
1079 void configurenotify(XEvent *);
1080 void destroynotify(XEvent *);
1081 void enternotify(XEvent *);
1082 void focusevent(XEvent *);
1083 void mapnotify(XEvent *);
1084 void mappingnotify(XEvent *);
1085 void maprequest(XEvent *);
1086 void propertynotify(XEvent *);
1087 void unmapnotify(XEvent *);
1088 void visibilitynotify(XEvent *);
1089 void clientmessage(XEvent *);
1091 void (*handler[LASTEvent])(XEvent *) = {
1093 [KeyPress] = keypress,
1094 [ButtonPress] = buttonpress,
1095 [ConfigureRequest] = configurerequest,
1096 [ConfigureNotify] = configurenotify,
1097 [DestroyNotify] = destroynotify,
1098 [EnterNotify] = enternotify,
1099 [FocusIn] = focusevent,
1100 [FocusOut] = focusevent,
1101 [MapNotify] = mapnotify,
1102 [MappingNotify] = mappingnotify,
1103 [MapRequest] = maprequest,
1104 [PropertyNotify] = propertynotify,
1105 [UnmapNotify] = unmapnotify,
1106 [VisibilityNotify] = visibilitynotify,
1107 [ClientMessage] = clientmessage,
1113 int saved_errno, status;
1116 saved_errno = errno;
1120 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1125 if (errno != ECHILD)
1126 warn("sighdlr: waitpid");
1127 #endif /* SWM_DEBUG */
1130 if (pid == searchpid)
1134 if (WIFEXITED(status)) {
1135 if (WEXITSTATUS(status) != 0)
1136 warnx("sighdlr: child exit status: %d",
1137 WEXITSTATUS(status));
1139 warnx("sighdlr: child is terminated "
1141 #endif /* SWM_DEBUG */
1155 errno = saved_errno;
1161 struct pid_e *p = NULL;
1163 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1168 TAILQ_FOREACH(p, &pidlist, entry) {
1177 name_to_color(char *colorname)
1181 XColor screen_def, exact_def;
1182 unsigned long result = 0;
1183 char cname[32] = "#";
1185 cmap = DefaultColormap(display, screens[0].idx);
1186 status = XAllocNamedColor(display, cmap, colorname,
1187 &screen_def, &exact_def);
1189 strlcat(cname, colorname + 2, sizeof cname - 1);
1190 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1194 result = screen_def.pixel;
1196 warnx("color '%s' not found", colorname);
1202 setscreencolor(char *val, int i, int c)
1204 if (i > 0 && i <= ScreenCount(display)) {
1205 screens[i - 1].c[c].color = name_to_color(val);
1206 free(screens[i - 1].c[c].name);
1207 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1209 } else if (i == -1) {
1210 for (i = 0; i < ScreenCount(display); i++) {
1211 screens[i].c[c].color = name_to_color(val);
1212 free(screens[i].c[c].name);
1213 if ((screens[i].c[c].name = strdup(val)) == NULL)
1217 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1218 i, ScreenCount(display));
1222 fancy_stacker(struct workspace *ws)
1224 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1225 if (ws->cur_layout->l_stack == vertical_stack)
1226 snprintf(ws->stacker, sizeof ws->stacker,
1227 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1228 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1229 if (ws->cur_layout->l_stack == horizontal_stack)
1230 snprintf(ws->stacker, sizeof ws->stacker,
1231 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1232 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1236 plain_stacker(struct workspace *ws)
1238 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1239 if (ws->cur_layout->l_stack == vertical_stack)
1240 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1241 sizeof ws->stacker);
1242 if (ws->cur_layout->l_stack == horizontal_stack)
1243 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1244 sizeof ws->stacker);
1248 custom_region(char *val)
1250 unsigned int sidx, x, y, w, h;
1252 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1253 errx(1, "invalid custom region, "
1254 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1255 if (sidx < 1 || sidx > ScreenCount(display))
1256 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1257 sidx, ScreenCount(display));
1261 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1263 if (x > DisplayWidth(display, sidx) ||
1264 y > DisplayHeight(display, sidx) ||
1265 w + x > DisplayWidth(display, sidx) ||
1266 h + y > DisplayHeight(display, sidx)) {
1267 warnx("ignoring region %ux%u+%u+%u - not within screen "
1268 "boundaries (%ux%u)", w, h, x, y,
1269 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1273 new_region(&screens[sidx], x, y, w, h);
1277 socket_setnonblock(int fd)
1281 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1282 err(1, "fcntl F_GETFL");
1283 flags |= O_NONBLOCK;
1284 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1285 err(1, "fcntl F_SETFL");
1289 bar_print(struct swm_region *r, char *s)
1293 XRectangle ibox, lbox;
1295 XClearWindow(display, r->bar_window);
1298 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1300 switch (bar_justify) {
1301 case SWM_BAR_JUSTIFY_LEFT:
1304 case SWM_BAR_JUSTIFY_CENTER:
1305 x = (WIDTH(r) - lbox.width) / 2;
1307 case SWM_BAR_JUSTIFY_RIGHT:
1308 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1312 if (x < SWM_BAR_OFFSET)
1315 DRAWSTRING(display, r->bar_window, bar_fs, r->s->bar_gc,
1316 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1321 bar_extra_stop(void)
1325 bzero(bar_pipe, sizeof bar_pipe);
1328 kill(bar_pid, SIGTERM);
1331 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1336 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1338 int do_class, do_name;
1340 XClassHint *xch = NULL;
1342 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1343 cur_focus != NULL) {
1344 if ((xch = XAllocClassHint()) == NULL)
1346 status = XGetClassHint(display, cur_focus->id, xch);
1347 if (status == BadWindow || status == BadAlloc)
1349 do_class = (title_class_enabled && xch->res_class != NULL);
1350 do_name = (title_name_enabled && xch->res_name != NULL);
1352 strlcat(s, xch->res_class, sz);
1353 if (do_class && do_name)
1354 strlcat(s, ":", sz);
1356 strlcat(s, xch->res_name, sz);
1357 strlcat(s, " ", sz);
1361 XFree(xch->res_name);
1362 XFree(xch->res_class);
1368 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1370 unsigned char *title;
1372 if (window_name_enabled && cur_focus != NULL) {
1373 title = get_win_name(cur_focus->id);
1374 if (title != NULL) {
1375 DNPRINTF(SWM_D_BAR, "bar_window_name: title: %s\n",
1378 if (cur_focus->floating)
1379 strlcat(s, "(f) ", sz);
1380 strlcat(s, (char *)title, sz);
1381 strlcat(s, " ", sz);
1387 int urgent[SWM_WS_MAX];
1389 bar_urgent(char *s, ssize_t sz)
1391 XWMHints *wmh = NULL;
1396 if (urgent_enabled == 0)
1399 for (i = 0; i < SWM_WS_MAX; i++)
1402 for (i = 0; i < ScreenCount(display); i++)
1403 for (j = 0; j < SWM_WS_MAX; j++)
1404 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1405 wmh = XGetWMHints(display, win->id);
1409 if (wmh->flags & XUrgencyHint)
1414 strlcat(s, "* ", sz);
1415 for (i = 0; i < SWM_WS_MAX; i++) {
1417 snprintf(b, sizeof b, "%d ", i + 1);
1419 snprintf(b, sizeof b, "- ");
1422 strlcat(s, "* ", sz);
1430 struct swm_region *r;
1433 char ws[SWM_BAR_MAX];
1434 char s[SWM_BAR_MAX];
1435 unsigned char cn[SWM_BAR_MAX];
1436 char loc[SWM_BAR_MAX];
1437 char *b, *stack = "";
1439 if (bar_enabled == 0)
1441 if (bar_extra && bar_extra_running) {
1442 /* ignore short reads; it'll correct itself */
1443 while ((b = fgetln(stdin, &len)) != NULL)
1444 if (b && b[len - 1] == '\n') {
1446 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1448 if (b == NULL && errno != EAGAIN) {
1449 warn("bar_update: bar_extra failed");
1453 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1455 if (clock_enabled == 0)
1456 strlcpy(s, "", sizeof s);
1459 localtime_r(&tmt, &tm);
1460 len = strftime(s, sizeof s, clock_format, &tm);
1462 strlcat(s, " ", sizeof s);
1465 for (i = 0; i < ScreenCount(display); i++) {
1467 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1468 strlcpy((char *)cn, "", sizeof cn);
1469 strlcpy(ws, "", sizeof ws);
1471 bar_urgent((char *)cn, sizeof cn);
1472 bar_class_name((char *)cn, sizeof cn,
1474 bar_window_name((char *)cn, sizeof cn,
1477 snprintf(ws, sizeof ws, "<%s>",
1481 stack = r->ws->stacker;
1483 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s "
1484 "%s", x++, r->ws->idx + 1, stack, ws, s, cn,
1485 bar_ext, bar_vertext);
1493 bar_check_opts(void)
1495 if (title_class_enabled || title_name_enabled || window_name_enabled)
1506 bar_toggle(struct swm_region *r, union arg *args)
1508 struct swm_region *tmpr;
1509 int i, sc = ScreenCount(display);
1511 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1514 for (i = 0; i < sc; i++)
1515 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1516 XUnmapWindow(display, tmpr->bar_window);
1518 for (i = 0; i < sc; i++)
1519 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1520 XMapRaised(display, tmpr->bar_window);
1522 bar_enabled = !bar_enabled;
1525 /* must be after stack */
1532 XSetWindowAttributes wa;
1533 struct swm_region *r;
1536 /* do this here because the conf file is in memory */
1537 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1538 /* launch external status app */
1539 bar_extra_running = 1;
1540 if (pipe(bar_pipe) == -1)
1541 err(1, "pipe error");
1542 socket_setnonblock(bar_pipe[0]);
1543 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1544 if (dup2(bar_pipe[0], 0) == -1)
1546 if (dup2(bar_pipe[1], 1) == -1)
1548 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1549 err(1, "could not disable SIGPIPE");
1550 switch (bar_pid = fork()) {
1552 err(1, "cannot fork");
1556 execvp(bar_argv[0], bar_argv);
1557 err(1, "%s external app failed", bar_argv[0]);
1559 default: /* parent */
1565 bzero(&wa, sizeof wa);
1566 for (i = 0; i < ScreenCount(display); i++)
1567 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1569 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1570 wa.background_pixel =
1571 screens[i].c[SWM_S_COLOR_BAR].color;
1572 XChangeWindowAttributes(display, r->bar_window,
1573 CWBackPixel | CWBorderPixel, &wa);
1579 bar_setup(struct swm_region *r)
1581 char *default_string;
1582 char **missing_charsets;
1583 int num_missing_charsets = 0;
1587 XFreeFontSet(display, bar_fs);
1592 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1594 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1595 &num_missing_charsets, &default_string);
1597 if (num_missing_charsets > 0) {
1598 warnx("Unable to load charset(s):");
1600 for (i = 0; i < num_missing_charsets; ++i)
1601 warnx("%s", missing_charsets[i]);
1603 XFreeStringList(missing_charsets);
1605 if (strcmp(default_string, ""))
1606 warnx("Glyphs from those sets will be replaced "
1607 "by '%s'.", default_string);
1609 warnx("Glyphs from those sets won't be drawn.");
1613 errx(1, "Error creating font set structure.");
1615 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1617 bar_height = bar_fs_extents->max_logical_extent.height +
1618 2 * bar_border_width;
1624 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1626 r->bar_window = XCreateSimpleWindow(display,
1627 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1628 bar_height - 2 * bar_border_width,
1629 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1630 r->s->c[SWM_S_COLOR_BAR].color);
1631 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1633 XMapRaised(display, r->bar_window);
1634 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1636 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1637 err(1, "could not install bar_signal");
1642 drain_enter_notify(void)
1647 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1650 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1654 set_win_state(struct ws_win *win, long state)
1656 long data[] = {state, None};
1658 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1663 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1664 (unsigned char *)data, 2);
1671 unsigned char *p = NULL;
1674 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1677 result = *((long *)p);
1683 version(struct swm_region *r, union arg *args)
1685 bar_version = !bar_version;
1687 snprintf(bar_vertext, sizeof bar_vertext,
1688 "Version: %s Build: %s", SCROTWM_VERSION, buildstr);
1690 strlcpy(bar_vertext, "", sizeof bar_vertext);
1695 client_msg(struct ws_win *win, Atom a)
1697 XClientMessageEvent cm;
1702 bzero(&cm, sizeof cm);
1703 cm.type = ClientMessage;
1704 cm.window = win->id;
1705 cm.message_type = aprot;
1708 cm.data.l[1] = CurrentTime;
1709 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1712 /* synthetic response to a ConfigureRequest when not making a change */
1714 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1721 /* send notification of unchanged state. */
1722 ce.type = ConfigureNotify;
1725 ce.width = WIDTH(win);
1726 ce.height = HEIGHT(win);
1727 ce.override_redirect = False;
1731 ce.display = display;
1733 ce.window = win->id;
1734 ce.border_width = border_width;
1738 ce.display = ev->display;
1739 ce.event = ev->window;
1740 ce.window = ev->window;
1742 /* make response appear more WM_SIZE_HINTS-compliant */
1744 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1745 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1746 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1747 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1748 SH_INC_W(win), SH_INC_H(win));
1752 /* the hint may be set... to 0! */
1753 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1754 ce.width = SH_MIN_W(win);
1755 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1756 ce.height = SH_MIN_H(win);
1761 /* may also be advertized as 0 */
1762 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1763 ce.width = SH_MAX_W(win);
1764 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1765 ce.height = SH_MAX_H(win);
1768 /* resize increment. */
1770 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1771 ce.width -= (ce.width - SH_MIN_W(win)) %
1773 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1774 ce.height -= (ce.height - SH_MIN_H(win)) %
1778 /* adjust x and y for requested border_width. */
1779 ce.x += border_width - ev->border_width;
1780 ce.y += border_width - ev->border_width;
1781 ce.border_width = ev->border_width;
1782 ce.above = ev->above;
1785 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1786 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1787 ce.y, ce.width, ce.height, ce.border_width);
1789 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1793 count_win(struct workspace *ws, int count_transient)
1798 TAILQ_FOREACH(win, &ws->winlist, entry) {
1799 if (count_transient == 0 && win->floating)
1801 if (count_transient == 0 && win->transient)
1807 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1813 quit(struct swm_region *r, union arg *args)
1815 DNPRINTF(SWM_D_MISC, "quit\n");
1820 unmap_window(struct ws_win *win)
1825 /* don't unmap again */
1826 if (getstate(win->id) == IconicState)
1829 set_win_state(win, IconicState);
1831 XUnmapWindow(display, win->id);
1832 XSetWindowBorder(display, win->id,
1833 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1842 for (i = 0; i < ScreenCount(display); i++)
1843 for (j = 0; j < SWM_WS_MAX; j++)
1844 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1849 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1856 event.display = display; /* Ignored, but what the hell */
1857 event.window = win->id;
1858 event.root = win->s->root;
1859 event.subwindow = None;
1860 event.time = CurrentTime;
1865 event.same_screen = True;
1866 event.keycode = XKeysymToKeycode(display, keysym);
1867 event.state = modifiers;
1869 event.type = KeyPress;
1870 XSendEvent(event.display, event.window, True,
1871 KeyPressMask, (XEvent *)&event);
1873 event.type = KeyRelease;
1874 XSendEvent(event.display, event.window, True,
1875 KeyPressMask, (XEvent *)&event);
1880 restart(struct swm_region *r, union arg *args)
1882 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1884 /* disable alarm because the following code may not be interrupted */
1886 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1887 err(1, "can't disable alarm");
1892 XCloseDisplay(display);
1893 execvp(start_argv[0], start_argv);
1894 warn("execvp failed");
1899 root_to_region(Window root)
1901 struct swm_region *r = NULL;
1903 int i, x, y, wx, wy;
1906 for (i = 0; i < ScreenCount(display); i++)
1907 if (screens[i].root == root)
1910 if (XQueryPointer(display, screens[i].root,
1911 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1912 /* choose a region based on pointer location */
1913 TAILQ_FOREACH(r, &screens[i].rl, entry)
1914 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1915 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1920 r = TAILQ_FIRST(&screens[i].rl);
1926 find_unmanaged_window(Window id)
1931 for (i = 0; i < ScreenCount(display); i++)
1932 for (j = 0; j < SWM_WS_MAX; j++)
1933 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1941 find_window(Window id)
1944 Window wrr, wpr, *wcr = NULL;
1948 for (i = 0; i < ScreenCount(display); i++)
1949 for (j = 0; j < SWM_WS_MAX; j++)
1950 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1954 /* if we were looking for the parent return that window instead */
1955 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1960 /* ignore not found and root */
1961 if (wpr == 0 || wrr == wpr)
1964 /* look for parent */
1965 for (i = 0; i < ScreenCount(display); i++)
1966 for (j = 0; j < SWM_WS_MAX; j++)
1967 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1975 spawn(int ws_idx, union arg *args, int close_fd)
1980 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1983 close(ConnectionNumber(display));
1985 setenv("LD_PRELOAD", SWM_LIB, 1);
1987 if (asprintf(&ret, "%d", ws_idx) == -1) {
1988 warn("spawn: asprintf SWM_WS");
1991 setenv("_SWM_WS", ret, 1);
1995 if (asprintf(&ret, "%d", getpid()) == -1) {
1996 warn("spawn: asprintf _SWM_PID");
1999 setenv("_SWM_PID", ret, 1);
2003 if (setsid() == -1) {
2004 warn("spawn: setsid");
2010 * close stdin and stdout to prevent interaction between apps
2011 * and the baraction script
2012 * leave stderr open to record errors
2014 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2015 warn("spawn: open");
2018 dup2(fd, STDIN_FILENO);
2019 dup2(fd, STDOUT_FILENO);
2024 execvp(args->argv[0], args->argv);
2026 warn("spawn: execvp");
2031 spawnterm(struct swm_region *r, union arg *args)
2033 DNPRINTF(SWM_D_MISC, "spawnterm\n");
2037 setenv("_SWM_XTERM_FONTADJ", "", 1);
2038 spawn(r->ws->idx, args, 1);
2043 kill_refs(struct ws_win *win)
2046 struct swm_region *r;
2047 struct workspace *ws;
2052 for (i = 0; i < ScreenCount(display); i++)
2053 TAILQ_FOREACH(r, &screens[i].rl, entry)
2054 for (x = 0; x < SWM_WS_MAX; x++) {
2056 if (win == ws->focus)
2058 if (win == ws->focus_prev)
2059 ws->focus_prev = NULL;
2064 validate_win(struct ws_win *testwin)
2067 struct workspace *ws;
2068 struct swm_region *r;
2071 if (testwin == NULL)
2074 for (i = 0; i < ScreenCount(display); i++)
2075 TAILQ_FOREACH(r, &screens[i].rl, entry)
2076 for (x = 0; x < SWM_WS_MAX; x++) {
2078 TAILQ_FOREACH(win, &ws->winlist, entry)
2086 validate_ws(struct workspace *testws)
2088 struct swm_region *r;
2089 struct workspace *ws;
2092 /* validate all ws */
2093 for (i = 0; i < ScreenCount(display); i++)
2094 TAILQ_FOREACH(r, &screens[i].rl, entry)
2095 for (x = 0; x < SWM_WS_MAX; x++) {
2104 unfocus_win(struct ws_win *win)
2109 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2113 if (win->ws == NULL)
2116 if (validate_ws(win->ws))
2117 return; /* XXX this gets hit with thunderbird, needs fixing */
2119 if (win->ws->r == NULL)
2122 if (validate_win(win)) {
2127 if (win->ws->focus == win) {
2128 win->ws->focus = NULL;
2129 win->ws->focus_prev = win;
2132 if (validate_win(win->ws->focus)) {
2133 kill_refs(win->ws->focus);
2134 win->ws->focus = NULL;
2136 if (validate_win(win->ws->focus_prev)) {
2137 kill_refs(win->ws->focus_prev);
2138 win->ws->focus_prev = NULL;
2141 /* drain all previous unfocus events */
2142 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2145 grabbuttons(win, 0);
2146 XSetWindowBorder(display, win->id,
2147 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2149 XChangeProperty(display, win->s->root,
2150 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2151 PropModeReplace, (unsigned char *)&none,1);
2160 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2162 for (i = 0; i < ScreenCount(display); i++)
2163 for (j = 0; j < SWM_WS_MAX; j++)
2164 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2169 focus_win(struct ws_win *win)
2174 struct ws_win *cfw = NULL;
2177 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2181 if (win->ws == NULL)
2184 if (validate_ws(win->ws))
2185 return; /* XXX this gets hit with thunderbird, needs fixing */
2187 if (validate_win(win)) {
2192 if (validate_win(win)) {
2197 XGetInputFocus(display, &cur_focus, &rr);
2198 if ((cfw = find_window(cur_focus)) != NULL)
2201 /* use larger hammer since the window was killed somehow */
2202 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2203 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2204 XSetWindowBorder(display, cfw->id,
2205 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2208 win->ws->focus = win;
2210 if (win->ws->r != NULL) {
2211 /* drain all previous focus events */
2212 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2216 XSetInputFocus(display, win->id,
2217 RevertToParent, CurrentTime);
2218 grabbuttons(win, 1);
2219 XSetWindowBorder(display, win->id,
2220 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2221 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2222 win->ws->always_raise)
2223 XMapRaised(display, win->id);
2225 XChangeProperty(display, win->s->root,
2226 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2227 PropModeReplace, (unsigned char *)&win->id,1);
2234 switchws(struct swm_region *r, union arg *args)
2236 int wsid = args->id, unmap_old = 0;
2237 struct swm_region *this_r, *other_r;
2239 struct workspace *new_ws, *old_ws;
2246 old_ws = this_r->ws;
2247 new_ws = &this_r->s->ws[wsid];
2249 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2250 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2252 if (new_ws == NULL || old_ws == NULL)
2254 if (new_ws == old_ws)
2257 other_r = new_ws->r;
2258 if (other_r == NULL) {
2259 /* the other workspace is hidden, hide this one */
2263 /* the other ws is visible in another region, exchange them */
2264 other_r->ws_prior = new_ws;
2265 other_r->ws = old_ws;
2266 old_ws->r = other_r;
2268 this_r->ws_prior = old_ws;
2269 this_r->ws = new_ws;
2272 /* this is needed so that we can click on a window after a restart */
2276 a.id = SWM_ARG_ID_FOCUSCUR;
2277 focus(new_ws->r, &a);
2281 /* unmap old windows */
2283 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2286 if (focus_mode == SWM_FOCUS_DEFAULT)
2287 drain_enter_notify();
2291 cyclews(struct swm_region *r, union arg *args)
2294 struct swm_screen *s = r->s;
2297 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2298 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2303 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2306 case SWM_ARG_ID_CYCLEWS_UP:
2307 if (a.id < SWM_WS_MAX - 1)
2312 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2315 case SWM_ARG_ID_CYCLEWS_DOWN:
2319 a.id = SWM_WS_MAX - 1;
2326 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2328 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2332 } while (a.id != r->ws->idx);
2336 priorws(struct swm_region *r, union arg *args)
2340 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2341 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2343 if (r->ws_prior == NULL)
2346 a.id = r->ws_prior->idx;
2351 cyclescr(struct swm_region *r, union arg *args)
2353 struct swm_region *rr = NULL;
2357 /* do nothing if we don't have more than one screen */
2358 if (!(ScreenCount(display) > 1 || outputs > 1))
2363 case SWM_ARG_ID_CYCLESC_UP:
2364 rr = TAILQ_NEXT(r, entry);
2366 rr = TAILQ_FIRST(&screens[i].rl);
2368 case SWM_ARG_ID_CYCLESC_DOWN:
2369 rr = TAILQ_PREV(r, swm_region_list, entry);
2371 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2379 /* move mouse to region */
2381 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2382 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2384 a.id = SWM_ARG_ID_FOCUSCUR;
2387 if (rr->ws->focus) {
2388 /* move to focus window */
2389 x = X(rr->ws->focus) + 1;
2390 y = Y(rr->ws->focus) + 1;
2391 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2396 sort_windows(struct ws_win_list *wl)
2398 struct ws_win *win, *parent, *nxt;
2403 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2404 nxt = TAILQ_NEXT(win, entry);
2405 if (win->transient) {
2406 parent = find_window(win->transient);
2407 if (parent == NULL) {
2408 warnx("not possible bug");
2411 TAILQ_REMOVE(wl, win, entry);
2412 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2419 swapwin(struct swm_region *r, union arg *args)
2421 struct ws_win *target, *source;
2422 struct ws_win *cur_focus;
2423 struct ws_win_list *wl;
2426 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2427 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2429 cur_focus = r->ws->focus;
2430 if (cur_focus == NULL)
2434 wl = &source->ws->winlist;
2437 case SWM_ARG_ID_SWAPPREV:
2438 if (source->transient)
2439 source = find_window(source->transient);
2440 target = TAILQ_PREV(source, ws_win_list, entry);
2441 if (target && target->transient)
2442 target = find_window(target->transient);
2443 TAILQ_REMOVE(wl, source, entry);
2445 TAILQ_INSERT_TAIL(wl, source, entry);
2447 TAILQ_INSERT_BEFORE(target, source, entry);
2449 case SWM_ARG_ID_SWAPNEXT:
2450 target = TAILQ_NEXT(source, entry);
2451 /* move the parent and let the sort handle the move */
2452 if (source->transient)
2453 source = find_window(source->transient);
2454 TAILQ_REMOVE(wl, source, entry);
2456 TAILQ_INSERT_HEAD(wl, source, entry);
2458 TAILQ_INSERT_AFTER(wl, target, source, entry);
2460 case SWM_ARG_ID_SWAPMAIN:
2461 target = TAILQ_FIRST(wl);
2462 if (target == source) {
2463 if (source->ws->focus_prev != NULL &&
2464 source->ws->focus_prev != target)
2466 source = source->ws->focus_prev;
2470 if (target == NULL || source == NULL)
2472 source->ws->focus_prev = target;
2473 TAILQ_REMOVE(wl, target, entry);
2474 TAILQ_INSERT_BEFORE(source, target, entry);
2475 TAILQ_REMOVE(wl, source, entry);
2476 TAILQ_INSERT_HEAD(wl, source, entry);
2478 case SWM_ARG_ID_MOVELAST:
2479 TAILQ_REMOVE(wl, source, entry);
2480 TAILQ_INSERT_TAIL(wl, source, entry);
2483 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2493 focus_prev(struct ws_win *win)
2495 struct ws_win *winfocus = NULL;
2496 struct ws_win *cur_focus = NULL;
2497 struct ws_win_list *wl = NULL;
2498 struct workspace *ws = NULL;
2500 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx\n", WINID(win));
2502 if (!(win && win->ws))
2507 cur_focus = ws->focus;
2509 /* pickle, just focus on whatever */
2510 if (cur_focus == NULL) {
2511 /* use prev_focus if valid */
2512 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2513 find_window(WINID(ws->focus_prev)))
2514 winfocus = ws->focus_prev;
2515 if (winfocus == NULL)
2516 winfocus = TAILQ_FIRST(wl);
2520 /* if transient focus on parent */
2521 if (cur_focus->transient) {
2522 winfocus = find_window(cur_focus->transient);
2526 /* if in max_stack try harder */
2527 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2528 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2529 if (cur_focus != ws->focus_prev)
2530 winfocus = ws->focus_prev;
2531 else if (cur_focus != ws->focus)
2532 winfocus = ws->focus;
2534 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2539 if (cur_focus == win)
2540 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2541 if (winfocus == NULL)
2542 winfocus = TAILQ_LAST(wl, ws_win_list);
2543 if (winfocus == NULL || winfocus == win)
2544 winfocus = TAILQ_NEXT(cur_focus, entry);
2547 focus_magic(winfocus);
2551 focus(struct swm_region *r, union arg *args)
2553 struct ws_win *winfocus = NULL, *head;
2554 struct ws_win *cur_focus = NULL;
2555 struct ws_win_list *wl = NULL;
2556 struct workspace *ws = NULL;
2562 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2564 /* treat FOCUS_CUR special */
2565 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2566 if (r->ws->focus && r->ws->focus->iconic == 0)
2567 winfocus = r->ws->focus;
2568 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2569 winfocus = r->ws->focus_prev;
2571 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2572 if (winfocus->iconic == 0)
2575 focus_magic(winfocus);
2579 if ((cur_focus = r->ws->focus) == NULL)
2583 if (TAILQ_EMPTY(wl))
2585 /* make sure there is at least one uniconified window */
2587 TAILQ_FOREACH(winfocus, wl, entry)
2588 if (winfocus->iconic == 0) {
2596 case SWM_ARG_ID_FOCUSPREV:
2597 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2599 head = TAILQ_LAST(wl, ws_win_list);
2601 if (WINID(winfocus) == cur_focus->transient) {
2602 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2604 head = TAILQ_LAST(wl, ws_win_list);
2609 if (winfocus && winfocus->iconic) {
2610 while (winfocus != cur_focus) {
2611 if (winfocus == NULL)
2612 winfocus = TAILQ_LAST(wl, ws_win_list);
2613 if (winfocus->iconic == 0)
2615 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2621 case SWM_ARG_ID_FOCUSNEXT:
2622 head = TAILQ_NEXT(cur_focus, entry);
2624 head = TAILQ_FIRST(wl);
2628 if (winfocus && winfocus->iconic) {
2629 while (winfocus != cur_focus) {
2630 if (winfocus == NULL)
2631 winfocus = TAILQ_FIRST(wl);
2632 if (winfocus->iconic == 0)
2634 winfocus = TAILQ_NEXT(winfocus, entry);
2639 case SWM_ARG_ID_FOCUSMAIN:
2640 winfocus = TAILQ_FIRST(wl);
2641 if (winfocus == cur_focus)
2642 winfocus = cur_focus->ws->focus_prev;
2649 focus_magic(winfocus);
2653 cycle_layout(struct swm_region *r, union arg *args)
2655 struct workspace *ws = r->ws;
2658 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2661 if (ws->cur_layout->l_stack == NULL)
2662 ws->cur_layout = &layouts[0];
2665 if (focus_mode == SWM_FOCUS_DEFAULT)
2666 drain_enter_notify();
2667 a.id = SWM_ARG_ID_FOCUSCUR;
2673 stack_config(struct swm_region *r, union arg *args)
2675 struct workspace *ws = r->ws;
2677 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2680 if (ws->cur_layout->l_config != NULL)
2681 ws->cur_layout->l_config(ws, args->id);
2683 if (args->id != SWM_ARG_ID_STACKINIT)
2690 struct swm_geometry g;
2691 struct swm_region *r;
2697 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2699 for (i = 0; i < ScreenCount(display); i++) {
2703 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2704 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2705 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2707 /* start with screen geometry, adjust for bar */
2709 g.w -= 2 * border_width;
2710 g.h -= 2 * border_width;
2716 r->ws->cur_layout->l_stack(r->ws, &g);
2717 r->ws->cur_layout->l_string(r->ws);
2718 /* save r so we can track region changes */
2725 if (focus_mode == SWM_FOCUS_DEFAULT)
2726 drain_enter_notify();
2728 DNPRINTF(SWM_D_STACK, "stack: end\n");
2732 store_float_geom(struct ws_win *win, struct swm_region *r)
2734 /* retain window geom and region geom */
2735 win->g_float = win->g;
2736 win->rg_float = r->g;
2737 win->g_floatvalid = 1;
2741 stack_floater(struct ws_win *win, struct swm_region *r)
2749 bzero(&wc, sizeof wc);
2750 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2753 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2754 * geom on ws switches or return from max mode
2756 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2757 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2759 * use stored g and rg to set relative position and size
2760 * as in old region or before max stack mode
2762 X(win) = win->g_float.x - win->rg_float.x + X(r);
2763 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2764 WIDTH(win) = win->g_float.w;
2765 HEIGHT(win) = win->g_float.h;
2766 win->g_floatvalid = 0;
2769 win->floatmaxed = 0;
2771 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2772 (HEIGHT(win) >= HEIGHT(r)))
2773 wc.border_width = 0;
2775 wc.border_width = border_width;
2776 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2777 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
2778 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
2783 * floaters and transients are auto-centred unless moved
2786 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
2787 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
2790 /* win can be outside r if new r smaller than old r */
2791 /* Ensure top left corner inside r (move probs otherwise) */
2792 if (X(win) < X(r) - wc.border_width)
2793 X(win) = X(r) - wc.border_width;
2794 if (X(win) > X(r) + WIDTH(r) - 1)
2795 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
2796 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
2797 if (Y(win) < Y(r) - wc.border_width)
2798 Y(win) = Y(r) - wc.border_width;
2799 if (Y(win) > Y(r) + HEIGHT(r) - 1)
2800 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
2801 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
2805 wc.width = WIDTH(win);
2806 wc.height = HEIGHT(win);
2809 * Retain floater and transient geometry for correct positioning
2810 * when ws changes region
2812 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2813 store_float_geom(win, r);
2815 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2816 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
2818 XConfigureWindow(display, win->id, mask, &wc);
2822 * Send keystrokes to terminal to decrease/increase the font size as the
2823 * window size changes.
2826 adjust_font(struct ws_win *win)
2828 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2829 win->floating || win->transient)
2832 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2833 WIDTH(win) / win->sh.width_inc < term_width &&
2834 win->font_steps < SWM_MAX_FONT_STEPS) {
2835 win->font_size_boundary[win->font_steps] =
2836 (win->sh.width_inc * term_width) + win->sh.base_width;
2839 win->last_inc = win->sh.width_inc;
2840 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2841 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2842 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
2845 win->last_inc = win->sh.width_inc;
2846 fake_keypress(win, XK_KP_Add, ShiftMask);
2850 #define SWAPXY(g) do { \
2852 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2853 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2856 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2859 XWindowAttributes wa;
2860 struct swm_geometry win_g, r_g = *g;
2861 struct ws_win *win, *fs_win = 0;
2862 int i, j, s, stacks;
2863 int w_inc = 1, h_inc, w_base = 1, h_base;
2864 int hrh, extra = 0, h_slice, last_h = 0;
2865 int split, colno, winno, mwin, msize, mscale;
2866 int remain, missing, v_slice, reconfigure;
2869 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
2870 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
2872 winno = count_win(ws, 0);
2873 if (winno == 0 && count_win(ws, 1) == 0)
2876 TAILQ_FOREACH(win, &ws->winlist, entry)
2877 if (win->transient == 0 && win->floating == 0
2878 && win->iconic == 0)
2885 w_inc = win->sh.width_inc;
2886 w_base = win->sh.base_width;
2887 mwin = ws->l_state.horizontal_mwin;
2888 mscale = ws->l_state.horizontal_msize;
2889 stacks = ws->l_state.horizontal_stacks;
2892 w_inc = win->sh.height_inc;
2893 w_base = win->sh.base_height;
2894 mwin = ws->l_state.vertical_mwin;
2895 mscale = ws->l_state.vertical_msize;
2896 stacks = ws->l_state.vertical_stacks;
2900 if (stacks > winno - mwin)
2901 stacks = winno - mwin;
2905 h_slice = r_g.h / SWM_H_SLICE;
2906 if (mwin && winno > mwin) {
2907 v_slice = r_g.w / SWM_V_SLICE;
2911 win_g.w = v_slice * mscale;
2913 if (w_inc > 1 && w_inc < v_slice) {
2914 /* adjust for window's requested size increment */
2915 remain = (win_g.w - w_base) % w_inc;
2916 missing = w_inc - remain;
2923 win_g.x += r_g.w - msize;
2926 colno = split = winno / stacks;
2927 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2928 2 * border_width) / stacks);
2930 hrh = r_g.h / colno;
2931 extra = r_g.h - (colno * hrh);
2932 win_g.h = hrh - 2 * border_width;
2934 /* stack all the tiled windows */
2935 i = j = 0, s = stacks;
2936 TAILQ_FOREACH(win, &ws->winlist, entry) {
2937 if (win->transient != 0 || win->floating != 0)
2939 if (win->iconic != 0)
2942 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2947 if (split && i == split) {
2948 colno = (winno - mwin) / stacks;
2949 if (s <= (winno - mwin) % stacks)
2951 split = split + colno;
2952 hrh = (r_g.h / colno);
2953 extra = r_g.h - (colno * hrh);
2957 win_g.x += win_g.w + 2 * border_width;
2958 win_g.w = (r_g.w - msize -
2959 (stacks * 2 * border_width)) / stacks;
2961 win_g.w += (r_g.w - msize -
2962 (stacks * 2 * border_width)) % stacks;
2966 win_g.h = hrh - 2 * border_width;
2968 h_inc = win->sh.width_inc;
2969 h_base = win->sh.base_width;
2971 h_inc = win->sh.height_inc;
2972 h_base = win->sh.base_height;
2974 if (j == colno - 1) {
2975 win_g.h = hrh + extra;
2976 } else if (h_inc > 1 && h_inc < h_slice) {
2977 /* adjust for window's requested size increment */
2978 remain = (win_g.h - h_base) % h_inc;
2979 missing = h_inc - remain;
2981 if (missing <= extra || j == 0) {
2993 win_g.y += last_h + 2 * border_width;
2995 bzero(&wc, sizeof wc);
2996 if (disable_border && bar_enabled == 0 && winno == 1){
2997 wc.border_width = 0;
2998 win_g.w += 2 * border_width;
2999 win_g.h += 2 * border_width;
3001 wc.border_width = border_width;
3004 if (X(win) != win_g.y || Y(win) != win_g.x ||
3005 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3007 X(win) = wc.x = win_g.y;
3008 Y(win) = wc.y = win_g.x;
3009 WIDTH(win) = wc.width = win_g.h;
3010 HEIGHT(win) = wc.height = win_g.w;
3013 if (X(win) != win_g.x || Y(win) != win_g.y ||
3014 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3016 X(win) = wc.x = win_g.x;
3017 Y(win) = wc.y = win_g.y;
3018 WIDTH(win) = wc.width = win_g.w;
3019 HEIGHT(win) = wc.height = win_g.h;
3024 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3025 XConfigureWindow(display, win->id, mask, &wc);
3028 if (XGetWindowAttributes(display, win->id, &wa))
3029 if (wa.map_state == IsUnmapped)
3030 XMapRaised(display, win->id);
3038 /* now, stack all the floaters and transients */
3039 TAILQ_FOREACH(win, &ws->winlist, entry) {
3040 if (win->transient == 0 && win->floating == 0)
3042 if (win->iconic == 1)
3044 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3049 stack_floater(win, ws->r);
3050 XMapRaised(display, win->id);
3054 stack_floater(fs_win, ws->r);
3055 XMapRaised(display, fs_win->id);
3060 vertical_config(struct workspace *ws, int id)
3062 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3066 case SWM_ARG_ID_STACKRESET:
3067 case SWM_ARG_ID_STACKINIT:
3068 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3069 ws->l_state.vertical_mwin = 1;
3070 ws->l_state.vertical_stacks = 1;
3072 case SWM_ARG_ID_MASTERSHRINK:
3073 if (ws->l_state.vertical_msize > 1)
3074 ws->l_state.vertical_msize--;
3076 case SWM_ARG_ID_MASTERGROW:
3077 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3078 ws->l_state.vertical_msize++;
3080 case SWM_ARG_ID_MASTERADD:
3081 ws->l_state.vertical_mwin++;
3083 case SWM_ARG_ID_MASTERDEL:
3084 if (ws->l_state.vertical_mwin > 0)
3085 ws->l_state.vertical_mwin--;
3087 case SWM_ARG_ID_STACKINC:
3088 ws->l_state.vertical_stacks++;
3090 case SWM_ARG_ID_STACKDEC:
3091 if (ws->l_state.vertical_stacks > 1)
3092 ws->l_state.vertical_stacks--;
3094 case SWM_ARG_ID_FLIPLAYOUT:
3095 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3103 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3105 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3107 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3111 horizontal_config(struct workspace *ws, int id)
3113 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3116 case SWM_ARG_ID_STACKRESET:
3117 case SWM_ARG_ID_STACKINIT:
3118 ws->l_state.horizontal_mwin = 1;
3119 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3120 ws->l_state.horizontal_stacks = 1;
3122 case SWM_ARG_ID_MASTERSHRINK:
3123 if (ws->l_state.horizontal_msize > 1)
3124 ws->l_state.horizontal_msize--;
3126 case SWM_ARG_ID_MASTERGROW:
3127 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3128 ws->l_state.horizontal_msize++;
3130 case SWM_ARG_ID_MASTERADD:
3131 ws->l_state.horizontal_mwin++;
3133 case SWM_ARG_ID_MASTERDEL:
3134 if (ws->l_state.horizontal_mwin > 0)
3135 ws->l_state.horizontal_mwin--;
3137 case SWM_ARG_ID_STACKINC:
3138 ws->l_state.horizontal_stacks++;
3140 case SWM_ARG_ID_STACKDEC:
3141 if (ws->l_state.horizontal_stacks > 1)
3142 ws->l_state.horizontal_stacks--;
3144 case SWM_ARG_ID_FLIPLAYOUT:
3145 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3153 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3155 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3157 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3160 /* fullscreen view */
3162 max_stack(struct workspace *ws, struct swm_geometry *g)
3165 struct swm_geometry gg = *g;
3166 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3170 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3175 winno = count_win(ws, 0);
3176 if (winno == 0 && count_win(ws, 1) == 0)
3179 TAILQ_FOREACH(win, &ws->winlist, entry) {
3180 if (win->transient) {
3182 parent = find_window(win->transient);
3186 if (win->floating && win->floatmaxed == 0 ) {
3188 * retain geometry for retrieval on exit from
3191 store_float_geom(win, ws->r);
3192 win->floatmaxed = 1;
3195 /* only reconfigure if necessary */
3196 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3197 HEIGHT(win) != gg.h) {
3198 bzero(&wc, sizeof wc);
3199 X(win) = wc.x = gg.x;
3200 Y(win) = wc.y = gg.y;
3202 wc.border_width = border_width;
3203 WIDTH(win) = wc.width = gg.w;
3204 HEIGHT(win) = wc.height = gg.h;
3206 wc.border_width = 0;
3207 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3208 HEIGHT(win) = wc.height = gg.h +
3211 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3212 XConfigureWindow(display, win->id, mask, &wc);
3214 /* unmap only if we don't have multi screen */
3215 if (win != ws->focus)
3216 if (!(ScreenCount(display) > 1 || outputs > 1))
3220 /* put the last transient on top */
3223 XMapRaised(display, parent->id);
3224 stack_floater(wintrans, ws->r);
3225 focus_magic(wintrans);
3230 send_to_ws(struct swm_region *r, union arg *args)
3232 int wsid = args->id;
3233 struct ws_win *win = NULL, *parent;
3234 struct workspace *ws, *nws;
3235 Atom ws_idx_atom = 0;
3236 unsigned char ws_idx_str[SWM_PROPLEN];
3239 if (r && r->ws && r->ws->focus)
3245 if (win->ws->idx == wsid)
3248 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3251 nws = &win->s->ws[wsid];
3253 a.id = SWM_ARG_ID_FOCUSPREV;
3255 if (win->transient) {
3256 parent = find_window(win->transient);
3258 unmap_window(parent);
3259 TAILQ_REMOVE(&ws->winlist, parent, entry);
3260 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3265 TAILQ_REMOVE(&ws->winlist, win, entry);
3266 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3267 if (TAILQ_EMPTY(&ws->winlist))
3268 r->ws->focus = NULL;
3271 /* Try to update the window's workspace property */
3272 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3274 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3276 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3278 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3279 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3286 pressbutton(struct swm_region *r, union arg *args)
3288 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3289 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3293 raise_toggle(struct swm_region *r, union arg *args)
3295 if (r && r->ws == NULL)
3298 r->ws->always_raise = !r->ws->always_raise;
3300 /* bring floaters back to top */
3301 if (r->ws->always_raise == 0)
3306 iconify(struct swm_region *r, union arg *args)
3310 if (r->ws->focus == NULL)
3312 unmap_window(r->ws->focus);
3313 update_iconic(r->ws->focus, 1);
3315 if (focus_mode == SWM_FOCUS_DEFAULT)
3316 drain_enter_notify();
3317 r->ws->focus = NULL;
3318 a.id = SWM_ARG_ID_FOCUSCUR;
3323 get_win_name(Window win)
3325 unsigned char *prop = NULL;
3326 unsigned long nbytes, nitems;
3328 /* try _NET_WM_NAME first */
3329 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3332 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3333 &nitems, NULL, &prop))
3337 /* fallback to WM_NAME */
3338 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3341 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3348 uniconify(struct swm_region *r, union arg *args)
3352 unsigned char *name;
3355 DNPRINTF(SWM_D_MISC, "uniconify\n");
3357 if (r && r->ws == NULL)
3360 /* make sure we have anything to uniconify */
3361 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3362 if (win->ws == NULL)
3363 continue; /* should never happen */
3364 if (win->iconic == 0)
3372 search_resp_action = SWM_SEARCH_UNICONIFY;
3374 spawn_select(r, args, "search", &searchpid);
3376 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3379 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3380 if (win->ws == NULL)
3381 continue; /* should never happen */
3382 if (win->iconic == 0)
3385 name = get_win_name(win->id);
3388 fprintf(lfile, "%s.%lu\n", name, win->id);
3396 name_workspace(struct swm_region *r, union arg *args)
3400 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3406 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3408 spawn_select(r, args, "name_workspace", &searchpid);
3410 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3413 fprintf(lfile, "%s", "");
3418 search_workspace(struct swm_region *r, union arg *args)
3421 struct workspace *ws;
3424 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3430 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3432 spawn_select(r, args, "search", &searchpid);
3434 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3437 for (i = 0; i < SWM_WS_MAX; i++) {
3441 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3442 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3449 search_win_cleanup(void)
3451 struct search_window *sw = NULL;
3453 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3454 XDestroyWindow(display, sw->indicator);
3455 XFreeGC(display, sw->gc);
3456 TAILQ_REMOVE(&search_wl, sw, entry);
3462 search_win(struct swm_region *r, union arg *args)
3464 struct ws_win *win = NULL;
3465 struct search_window *sw = NULL;
3472 XRectangle ibox, lbox;
3474 DNPRINTF(SWM_D_MISC, "search_win\n");
3477 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3479 spawn_select(r, args, "search", &searchpid);
3481 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3484 TAILQ_INIT(&search_wl);
3487 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3488 if (win->iconic == 1)
3491 sw = calloc(1, sizeof(struct search_window));
3493 warn("search_win: calloc");
3495 search_win_cleanup();
3501 snprintf(s, sizeof s, "%d", i);
3504 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3506 w = XCreateSimpleWindow(display,
3507 win->id, 0, 0,lbox.width + 4,
3508 bar_fs_extents->max_logical_extent.height, 1,
3509 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3510 r->s->c[SWM_S_COLOR_FOCUS].color);
3513 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3515 sw->gc = XCreateGC(display, w, 0, &gcv);
3516 XMapRaised(display, w);
3517 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3519 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3520 (bar_fs_extents->max_logical_extent.height -
3521 lbox.height) / 2 - lbox.y, s, len);
3523 fprintf(lfile, "%d\n", i);
3531 search_resp_uniconify(char *resp, unsigned long len)
3533 unsigned char *name;
3537 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3539 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3540 if (win->iconic == 0)
3542 name = get_win_name(win->id);
3545 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3550 if (strncmp(s, resp, len) == 0) {
3551 /* XXX this should be a callback to generalize */
3552 update_iconic(win, 0);
3561 search_resp_name_workspace(char *resp, unsigned long len)
3563 struct workspace *ws;
3565 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3567 if (search_r->ws == NULL)
3572 free(search_r->ws->name);
3573 search_r->ws->name = NULL;
3577 ws->name = strdup(resp);
3578 if (ws->name == NULL) {
3579 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3580 "strdup: %s", strerror(errno));
3587 search_resp_search_workspace(char *resp, unsigned long len)
3594 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3598 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3605 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3607 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3614 switchws(search_r, &a);
3618 search_resp_search_window(char *resp, unsigned long len)
3623 struct search_window *sw;
3625 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3629 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3634 idx = strtonum(s, 1, INT_MAX, &errstr);
3636 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3643 TAILQ_FOREACH(sw, &search_wl, entry)
3644 if (idx == sw->idx) {
3650 #define MAX_RESP_LEN 1024
3653 search_do_resp(void)
3659 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3664 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3665 warn("search: calloc");
3669 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3671 warn("search: read error");
3674 resp[rbytes] = '\0';
3677 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3678 * newline, so work around that by sanitizing the resp now.
3680 resp[strcspn(resp, "\n")] = '\0';
3683 switch (search_resp_action) {
3684 case SWM_SEARCH_UNICONIFY:
3685 search_resp_uniconify(resp, len);
3687 case SWM_SEARCH_NAME_WORKSPACE:
3688 search_resp_name_workspace(resp, len);
3690 case SWM_SEARCH_SEARCH_WORKSPACE:
3691 search_resp_search_workspace(resp, len);
3693 case SWM_SEARCH_SEARCH_WINDOW:
3694 search_resp_search_window(resp, len);
3699 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3700 search_win_cleanup();
3702 search_resp_action = SWM_SEARCH_NONE;
3703 close(select_resp_pipe[0]);
3708 wkill(struct swm_region *r, union arg *args)
3710 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3712 if (r->ws->focus == NULL)
3715 if (args->id == SWM_ARG_ID_KILLWINDOW)
3716 XKillClient(display, r->ws->focus->id);
3718 if (r->ws->focus->can_delete)
3719 client_msg(r->ws->focus, adelete);
3724 floating_toggle_win(struct ws_win *win)
3726 struct swm_region *r;
3736 /* reject floating toggles in max stack mode */
3737 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3740 if (win->floating) {
3741 if (!win->floatmaxed) {
3742 /* retain position for refloat */
3743 store_float_geom(win, r);
3747 if (win->g_floatvalid) {
3748 /* refloat at last floating relative position */
3749 X(win) = win->g_float.x - win->rg_float.x + X(r);
3750 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3751 WIDTH(win) = win->g_float.w;
3752 HEIGHT(win) = win->g_float.h;
3757 ewmh_update_actions(win);
3763 floating_toggle(struct swm_region *r, union arg *args)
3765 struct ws_win *win = r->ws->focus;
3771 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3772 _NET_WM_STATE_TOGGLE);
3775 if (focus_mode == SWM_FOCUS_DEFAULT)
3776 drain_enter_notify();
3778 if (win == win->ws->focus) {
3779 a.id = SWM_ARG_ID_FOCUSCUR;
3780 focus(win->ws->r, &a);
3785 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3787 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
3789 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
3791 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
3794 if (X(win) < X(r) - border_width) {
3796 WIDTH(win) -= X(r) - X(win) - border_width;
3798 X(win) = X(r) - border_width;
3801 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
3803 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
3805 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
3808 if (Y(win) < Y(r) - border_width) {
3810 HEIGHT(win) -= Y(r) - Y(win) - border_width;
3812 Y(win) = Y(r) - border_width;
3817 if (HEIGHT(win) < 1)
3822 update_window(struct ws_win *win)
3827 bzero(&wc, sizeof wc);
3828 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
3829 wc.border_width = border_width;
3832 wc.width = WIDTH(win);
3833 wc.height = HEIGHT(win);
3835 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
3836 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3838 XConfigureWindow(display, win->id, mask, &wc);
3841 #define SWM_RESIZE_STEPS (50)
3844 resize(struct ws_win *win, union arg *args)
3848 struct swm_region *r = NULL;
3849 int resize_step = 0;
3853 struct swm_geometry g;
3854 int top = 0, left = 0;
3857 unsigned int shape; /* cursor style */
3863 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
3864 "transient: 0x%lx\n", win->id, YESNO(win->floating),
3867 if (!(win->transient != 0 || win->floating != 0))
3870 /* reject resizes in max mode for floaters (transient ok) */
3871 if (win->floatmaxed)
3875 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3881 case SWM_ARG_ID_WIDTHSHRINK:
3882 WIDTH(win) -= SWM_RESIZE_STEPS;
3885 case SWM_ARG_ID_WIDTHGROW:
3886 WIDTH(win) += SWM_RESIZE_STEPS;
3889 case SWM_ARG_ID_HEIGHTSHRINK:
3890 HEIGHT(win) -= SWM_RESIZE_STEPS;
3893 case SWM_ARG_ID_HEIGHTGROW:
3894 HEIGHT(win) += SWM_RESIZE_STEPS;
3901 constrain_window(win, r, 1);
3903 store_float_geom(win,r);
3907 if (focus_mode == SWM_FOCUS_DEFAULT)
3908 drain_enter_notify();
3910 /* get cursor offset from window root */
3911 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
3916 if (wx < WIDTH(win) / 2)
3919 if (wy < HEIGHT(win) / 2)
3922 if (args->id == SWM_ARG_ID_CENTER)
3925 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
3927 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
3929 cursor = XCreateFontCursor(display, shape);
3931 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3932 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
3933 XFreeCursor(display, cursor);
3938 XMaskEvent(display, MOUSEMASK | ExposureMask |
3939 SubstructureRedirectMask, &ev);
3941 case ConfigureRequest:
3944 handler[ev.type](&ev);
3947 /* cursor offset/delta from start of the operation */
3948 dx = ev.xmotion.x_root - x;
3949 dy = ev.xmotion.y_root - y;
3955 if (args->id == SWM_ARG_ID_CENTER) {
3956 if (g.h / 2 + dy < 1)
3960 HEIGHT(win) = g.h + 2 * dy;
3968 HEIGHT(win) = g.h + dy;
3975 if (args->id == SWM_ARG_ID_CENTER) {
3976 if (g.w / 2 + dx < 1)
3980 WIDTH(win) = g.w + 2 * dx;
3988 WIDTH(win) = g.w + dx;
3991 constrain_window(win, r, 1);
3993 /* not free, don't sync more than 120 times / second */
3994 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3995 time = ev.xmotion.time;
3996 XSync(display, False);
4001 } while (ev.type != ButtonRelease);
4003 XSync(display, False);
4006 store_float_geom(win,r);
4008 XUngrabPointer(display, CurrentTime);
4009 XFreeCursor(display, cursor);
4012 drain_enter_notify();
4016 resize_step(struct swm_region *r, union arg *args)
4018 struct ws_win *win = NULL;
4020 if (r && r->ws && r->ws->focus)
4028 #define SWM_MOVE_STEPS (50)
4031 move(struct ws_win *win, union arg *args)
4036 struct swm_region *r = NULL;
4046 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4047 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4049 /* in max_stack mode should only move transients */
4050 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4054 if (win->floating == 0 && !win->transient) {
4055 store_float_geom(win,r);
4056 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4059 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4066 case SWM_ARG_ID_MOVELEFT:
4067 X(win) -= (SWM_MOVE_STEPS - border_width);
4070 case SWM_ARG_ID_MOVERIGHT:
4071 X(win) += (SWM_MOVE_STEPS - border_width);
4074 case SWM_ARG_ID_MOVEUP:
4075 Y(win) -= (SWM_MOVE_STEPS - border_width);
4078 case SWM_ARG_ID_MOVEDOWN:
4079 Y(win) += (SWM_MOVE_STEPS - border_width);
4086 constrain_window(win, r, 0);
4088 store_float_geom(win, r);
4092 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4093 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4094 CurrentTime) != GrabSuccess)
4097 /* get cursor offset from window root */
4098 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4102 XMaskEvent(display, MOUSEMASK | ExposureMask |
4103 SubstructureRedirectMask, &ev);
4105 case ConfigureRequest:
4108 handler[ev.type](&ev);
4111 X(win) = ev.xmotion.x_root - wx - border_width;
4112 Y(win) = ev.xmotion.y_root - wy - border_width;
4114 constrain_window(win, r, 0);
4116 /* not free, don't sync more than 120 times / second */
4117 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4118 time = ev.xmotion.time;
4119 XSync(display, False);
4124 } while (ev.type != ButtonRelease);
4126 XSync(display, False);
4129 store_float_geom(win,r);
4130 XUngrabPointer(display, CurrentTime);
4133 drain_enter_notify();
4137 move_step(struct swm_region *r, union arg *args)
4139 struct ws_win *win = NULL;
4141 if (r && r->ws && r->ws->focus)
4146 if (!(win->transient != 0 || win->floating != 0))
4153 /* user/key callable function IDs */
4224 kf_search_workspace,
4226 kf_dumpwins, /* MUST BE LAST */
4230 /* key definitions */
4232 dummykeyfunc(struct swm_region *r, union arg *args)
4237 legacyfunc(struct swm_region *r, union arg *args)
4242 char name[SWM_FUNCNAME_LEN];
4243 void (*func)(struct swm_region *r, union arg *);
4245 } keyfuncs[kf_invalid + 1] = {
4246 /* name function argument */
4247 { "cycle_layout", cycle_layout, {0} },
4248 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4249 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4250 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4251 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4252 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4253 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4254 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4255 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4256 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4257 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4258 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4259 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4260 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4261 { "spawn_term", spawnterm, {.argv = spawn_term} },
4262 { "spawn_menu", legacyfunc, {0} },
4263 { "quit", quit, {0} },
4264 { "restart", restart, {0} },
4265 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4266 { "ws_1", switchws, {.id = 0} },
4267 { "ws_2", switchws, {.id = 1} },
4268 { "ws_3", switchws, {.id = 2} },
4269 { "ws_4", switchws, {.id = 3} },
4270 { "ws_5", switchws, {.id = 4} },
4271 { "ws_6", switchws, {.id = 5} },
4272 { "ws_7", switchws, {.id = 6} },
4273 { "ws_8", switchws, {.id = 7} },
4274 { "ws_9", switchws, {.id = 8} },
4275 { "ws_10", switchws, {.id = 9} },
4276 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4277 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4278 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4279 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4280 { "ws_prior", priorws, {0} },
4281 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4282 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4283 { "mvws_1", send_to_ws, {.id = 0} },
4284 { "mvws_2", send_to_ws, {.id = 1} },
4285 { "mvws_3", send_to_ws, {.id = 2} },
4286 { "mvws_4", send_to_ws, {.id = 3} },
4287 { "mvws_5", send_to_ws, {.id = 4} },
4288 { "mvws_6", send_to_ws, {.id = 5} },
4289 { "mvws_7", send_to_ws, {.id = 6} },
4290 { "mvws_8", send_to_ws, {.id = 7} },
4291 { "mvws_9", send_to_ws, {.id = 8} },
4292 { "mvws_10", send_to_ws, {.id = 9} },
4293 { "bar_toggle", bar_toggle, {0} },
4294 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4295 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4296 { "screenshot_all", legacyfunc, {0} },
4297 { "screenshot_wind", legacyfunc, {0} },
4298 { "float_toggle", floating_toggle,{0} },
4299 { "version", version, {0} },
4300 { "spawn_lock", legacyfunc, {0} },
4301 { "spawn_initscr", legacyfunc, {0} },
4302 { "spawn_custom", dummykeyfunc, {0} },
4303 { "iconify", iconify, {0} },
4304 { "uniconify", uniconify, {0} },
4305 { "raise_toggle", raise_toggle, {0} },
4306 { "button2", pressbutton, {2} },
4307 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4308 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4309 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4310 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4311 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4312 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4313 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4314 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4315 { "name_workspace", name_workspace, {0} },
4316 { "search_workspace", search_workspace, {0} },
4317 { "search_win", search_win, {0} },
4318 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4319 { "invalid key func", NULL, {0} },
4322 RB_ENTRY(key) entry;
4325 enum keyfuncid funcid;
4328 RB_HEAD(key_list, key);
4331 key_cmp(struct key *kp1, struct key *kp2)
4333 if (kp1->keysym < kp2->keysym)
4335 if (kp1->keysym > kp2->keysym)
4338 if (kp1->mod < kp2->mod)
4340 if (kp1->mod > kp2->mod)
4346 RB_GENERATE_STATIC(key_list, key, entry, key_cmp);
4347 struct key_list keys;
4350 enum { client_click, root_click };
4352 unsigned int action;
4354 unsigned int button;
4355 void (*func)(struct ws_win *, union arg *);
4358 /* action key mouse button func args */
4359 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4360 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4361 { client_click, MODKEY, Button1, move, {0} },
4365 update_modkey(unsigned int mod)
4371 RB_FOREACH(kp, key_list, &keys)
4372 if (kp->mod & ShiftMask)
4373 kp->mod = mod | ShiftMask;
4377 for (i = 0; i < LENGTH(buttons); i++)
4378 if (buttons[i].mask & ShiftMask)
4379 buttons[i].mask = mod | ShiftMask;
4381 buttons[i].mask = mod;
4386 TAILQ_ENTRY(spawn_prog) entry;
4391 TAILQ_HEAD(spawn_list, spawn_prog);
4392 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4395 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4398 struct spawn_prog *prog = NULL;
4400 char *ap, **real_args;
4402 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4405 TAILQ_FOREACH(prog, &spawns, entry) {
4406 if (!strcasecmp(spawn_name, prog->name))
4410 warnx("spawn_custom: program %s not found", spawn_name);
4414 /* make room for expanded args */
4415 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4416 err(1, "spawn_custom: calloc real_args");
4418 /* expand spawn_args into real_args */
4419 for (i = 0; i < prog->argc; i++) {
4421 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4422 if (!strcasecmp(ap, "$bar_border")) {
4424 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4426 err(1, "spawn_custom border color");
4427 } else if (!strcasecmp(ap, "$bar_color")) {
4429 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4431 err(1, "spawn_custom bar color");
4432 } else if (!strcasecmp(ap, "$bar_font")) {
4433 if ((real_args[i] = strdup(bar_fonts))
4435 err(1, "spawn_custom bar fonts");
4436 } else if (!strcasecmp(ap, "$bar_font_color")) {
4438 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4440 err(1, "spawn_custom color font");
4441 } else if (!strcasecmp(ap, "$color_focus")) {
4443 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4445 err(1, "spawn_custom color focus");
4446 } else if (!strcasecmp(ap, "$color_unfocus")) {
4448 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4450 err(1, "spawn_custom color unfocus");
4452 /* no match --> copy as is */
4453 if ((real_args[i] = strdup(ap)) == NULL)
4454 err(1, "spawn_custom strdup(ap)");
4456 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4461 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4462 for (i = 0; i < prog->argc; i++)
4463 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4464 DNPRINTF(SWM_D_SPAWN, "\n");
4466 *ret_args = real_args;
4467 return (prog->argc);
4471 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4477 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4481 spawn(r->ws->idx, &a, 1);
4483 for (i = 0; i < spawn_argc; i++)
4489 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4495 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4499 if (pipe(select_list_pipe) == -1)
4500 err(1, "pipe error");
4501 if (pipe(select_resp_pipe) == -1)
4502 err(1, "pipe error");
4504 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4505 err(1, "could not disable SIGPIPE");
4506 switch (*pid = fork()) {
4508 err(1, "cannot fork");
4511 if (dup2(select_list_pipe[0], 0) == -1)
4513 if (dup2(select_resp_pipe[1], 1) == -1)
4515 close(select_list_pipe[1]);
4516 close(select_resp_pipe[0]);
4517 spawn(r->ws->idx, &a, 0);
4519 default: /* parent */
4520 close(select_list_pipe[0]);
4521 close(select_resp_pipe[1]);
4525 for (i = 0; i < spawn_argc; i++)
4531 spawn_insert(char *name, char *args)
4533 char *arg, *cp, *ptr;
4534 struct spawn_prog *sp;
4536 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4538 if ((sp = calloc(1, sizeof *sp)) == NULL)
4539 err(1, "spawn_insert: malloc");
4540 if ((sp->name = strdup(name)) == NULL)
4541 err(1, "spawn_insert: strdup");
4543 /* convert the arguments to an argument list */
4544 if ((ptr = cp = strdup(args)) == NULL)
4545 err(1, "spawn_insert: strdup");
4546 while ((arg = strsep(&ptr, " \t")) != NULL) {
4547 /* empty field; skip it */
4552 if ((sp->argv = realloc(sp->argv, sp->argc *
4553 sizeof *sp->argv)) == NULL)
4554 err(1, "spawn_insert: realloc");
4555 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4556 err(1, "spawn_insert: strdup");
4560 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4561 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4565 spawn_remove(struct spawn_prog *sp)
4569 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4571 TAILQ_REMOVE(&spawns, sp, entry);
4572 for (i = 0; i < sp->argc; i++)
4578 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4582 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4584 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4587 spawn_insert(name, args);
4589 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4593 setspawn(char *name, char *args)
4595 struct spawn_prog *sp;
4597 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4602 TAILQ_FOREACH(sp, &spawns, entry) {
4603 if (!strcmp(sp->name, name)) {
4607 spawn_replace(sp, name, args);
4608 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4612 if (*args == '\0') {
4613 warnx("error: setspawn: cannot find program: %s", name);
4617 spawn_insert(name, args);
4618 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4622 setconfspawn(char *selector, char *value, int flags)
4624 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4626 setspawn(selector, value);
4628 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4635 setconfspawn("term", "xterm", 0);
4636 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4637 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4638 setconfspawn("lock", "xlock", 0);
4639 setconfspawn("initscr", "initscreen.sh", 0);
4640 setconfspawn("menu", "dmenu_run"
4643 " -nf $bar_font_color"
4645 " -sf $bar_color", 0);
4646 setconfspawn("search", "dmenu"
4650 " -nf $bar_font_color"
4652 " -sf $bar_color", 0);
4653 setconfspawn("name_workspace", "dmenu"
4657 " -nf $bar_font_color"
4659 " -sf $bar_color", 0);
4663 #define SWM_MODNAME_SIZE 32
4664 #define SWM_KEY_WS "\n+ \t"
4666 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4670 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4671 if (mod == NULL || ks == NULL) {
4672 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4675 if (keystr == NULL || strlen(keystr) == 0) {
4676 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4682 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4683 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4685 cp += (long)strspn(cp, SWM_KEY_WS);
4686 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4688 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4690 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4692 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4694 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4696 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4698 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4699 *mod |= ControlMask;
4701 *ks = XStringToKeysym(name);
4702 XConvertCase(*ks, ks, &uks);
4703 if (ks == NoSymbol) {
4705 "parsekeys: invalid key %s\n",
4711 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4716 strdupsafe(char *str)
4721 return (strdup(str));
4725 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
4729 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
4730 keyfuncs[kfid].name, spawn_name);
4732 if ((kp = malloc(sizeof *kp)) == NULL)
4733 err(1, "key_insert: malloc");
4738 kp->spawn_name = strdupsafe(spawn_name);
4739 RB_INSERT(key_list, &keys, kp);
4741 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
4745 key_lookup(unsigned int mod, KeySym ks)
4752 return (RB_FIND(key_list, &keys, &kp));
4756 key_remove(struct key *kp)
4758 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
4760 RB_REMOVE(key_list, &keys, kp);
4761 free(kp->spawn_name);
4764 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
4768 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
4771 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
4775 key_insert(mod, ks, kfid, spawn_name);
4777 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
4781 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4786 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4787 keyfuncs[kfid].name, spawn_name);
4789 if ((kp = key_lookup(mod, ks)) != NULL) {
4790 if (kfid == kf_invalid)
4793 key_replace(kp, mod, ks, kfid, spawn_name);
4794 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4797 if (kfid == kf_invalid) {
4798 warnx("error: setkeybinding: cannot find mod/key combination");
4799 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4803 key_insert(mod, ks, kfid, spawn_name);
4804 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4808 setconfbinding(char *selector, char *value, int flags)
4810 enum keyfuncid kfid;
4813 struct spawn_prog *sp;
4814 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4815 if (selector == NULL) {
4816 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4817 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4819 setkeybinding(mod, ks, kfid, NULL);
4824 /* search by key function name */
4825 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4826 if (strncasecmp(selector, keyfuncs[kfid].name,
4827 SWM_FUNCNAME_LEN) == 0) {
4828 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4830 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4831 setkeybinding(mod, ks, kfid, NULL);
4837 /* search by custom spawn name */
4838 TAILQ_FOREACH(sp, &spawns, entry) {
4839 if (strcasecmp(selector, sp->name) == 0) {
4840 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4842 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4843 setkeybinding(mod, ks, kf_spawn_custom,
4850 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4857 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4858 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
4859 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4860 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4861 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4862 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4863 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4864 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4865 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4866 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4867 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4868 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4869 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4870 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4871 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4872 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4873 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4874 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4875 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4876 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4877 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4878 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4879 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4880 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4881 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4882 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4883 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4884 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4885 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4886 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4887 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4888 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4889 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4890 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4891 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4892 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4893 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4894 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4895 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4896 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4897 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4898 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4899 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4900 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4901 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4902 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4903 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4904 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4905 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4906 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4907 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4908 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4909 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4910 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4911 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4912 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4913 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4914 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4915 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4916 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4917 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4918 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4919 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4920 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4921 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4922 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4923 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4924 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4925 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4926 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4927 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4928 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4930 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4939 while (RB_EMPTY(&keys) == 0) {
4940 kp = RB_ROOT(&keys);
4946 setkeymapping(char *selector, char *value, int flags)
4948 char keymapping_file[PATH_MAX];
4949 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4950 if (value[0] == '~')
4951 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4952 pwd->pw_dir, &value[1]);
4954 strlcpy(keymapping_file, value, sizeof keymapping_file);
4956 /* load new key bindings; if it fails, revert to default bindings */
4957 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4961 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4966 updatenumlockmask(void)
4969 XModifierKeymap *modmap;
4971 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4973 modmap = XGetModifierMapping(display);
4974 for (i = 0; i < 8; i++)
4975 for (j = 0; j < modmap->max_keypermod; j++)
4976 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4977 == XKeysymToKeycode(display, XK_Num_Lock))
4978 numlockmask = (1 << i);
4980 XFreeModifiermap(modmap);
4988 unsigned int modifiers[] =
4989 { 0, LockMask, numlockmask, numlockmask | LockMask };
4992 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4993 updatenumlockmask();
4995 for (k = 0; k < ScreenCount(display); k++) {
4996 if (TAILQ_EMPTY(&screens[k].rl))
4998 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4999 RB_FOREACH(kp, key_list, &keys) {
5000 if ((code = XKeysymToKeycode(display, kp->keysym)))
5001 for (j = 0; j < LENGTH(modifiers); j++)
5002 XGrabKey(display, code,
5003 kp->mod | modifiers[j],
5004 screens[k].root, True,
5005 GrabModeAsync, GrabModeAsync);
5011 grabbuttons(struct ws_win *win, int focused)
5014 unsigned int modifiers[] =
5015 { 0, LockMask, numlockmask, numlockmask|LockMask };
5017 updatenumlockmask();
5018 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5020 for (i = 0; i < LENGTH(buttons); i++)
5021 if (buttons[i].action == client_click)
5022 for (j = 0; j < LENGTH(modifiers); j++)
5023 XGrabButton(display, buttons[i].button,
5024 buttons[i].mask | modifiers[j],
5025 win->id, False, BUTTONMASK,
5026 GrabModeAsync, GrabModeSync, None,
5029 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5030 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5033 const char *quirkname[] = {
5034 "NONE", /* config string for "no value" */
5043 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5044 #define SWM_Q_WS "\n|+ \t"
5046 parsequirks(char *qstr, unsigned long *quirk)
5056 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5058 cp += (long)strspn(cp, SWM_Q_WS);
5059 for (i = 0; i < LENGTH(quirkname); i++) {
5060 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5061 DNPRINTF(SWM_D_QUIRK,
5062 "parsequirks: %s\n", name);
5067 *quirk |= 1 << (i-1);
5071 if (i >= LENGTH(quirkname)) {
5072 DNPRINTF(SWM_D_QUIRK,
5073 "parsequirks: invalid quirk [%s]\n", name);
5081 quirk_insert(const char *class, const char *name, unsigned long quirk)
5085 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5088 if ((qp = malloc(sizeof *qp)) == NULL)
5089 err(1, "quirk_insert: malloc");
5090 if ((qp->class = strdup(class)) == NULL)
5091 err(1, "quirk_insert: strdup");
5092 if ((qp->name = strdup(name)) == NULL)
5093 err(1, "quirk_insert: strdup");
5096 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5098 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5102 quirk_remove(struct quirk *qp)
5104 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5105 qp->name, qp->quirk);
5107 TAILQ_REMOVE(&quirks, qp, entry);
5112 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5116 quirk_replace(struct quirk *qp, const char *class, const char *name,
5117 unsigned long quirk)
5119 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5120 qp->name, qp->quirk);
5123 quirk_insert(class, name, quirk);
5125 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5129 setquirk(const char *class, const char *name, unsigned long quirk)
5133 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5136 TAILQ_FOREACH(qp, &quirks, entry) {
5137 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5141 quirk_replace(qp, class, name, quirk);
5142 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5147 warnx("error: setquirk: cannot find class/name combination");
5151 quirk_insert(class, name, quirk);
5152 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5156 setconfquirk(char *selector, char *value, int flags)
5158 char *cp, *class, *name;
5160 unsigned long quirks;
5161 if (selector == NULL)
5163 if ((cp = strchr(selector, ':')) == NULL)
5168 if ((retval = parsequirks(value, &quirks)) == 0)
5169 setquirk(class, name, quirks);
5176 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5177 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5178 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5179 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5180 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5181 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5182 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5183 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5184 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5185 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5186 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5187 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5188 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5191 /* conf file stuff */
5192 #define SWM_CONF_FILE "scrotwm.conf"
5194 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5195 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5196 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5197 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5198 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5199 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5200 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5201 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5202 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5206 setconfvalue(char *selector, char *value, int flags)
5212 case SWM_S_BAR_DELAY:
5213 bar_delay = atoi(value);
5215 case SWM_S_BAR_ENABLED:
5216 bar_enabled = atoi(value);
5218 case SWM_S_BAR_BORDER_WIDTH:
5219 bar_border_width = atoi(value);
5221 case SWM_S_BAR_AT_BOTTOM:
5222 bar_at_bottom = atoi(value);
5224 case SWM_S_BAR_JUSTIFY:
5225 if (!strcmp(value, "left"))
5226 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5227 else if (!strcmp(value, "center"))
5228 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5229 else if (!strcmp(value, "right"))
5230 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5232 errx(1, "invalid bar_justify");
5234 case SWM_S_STACK_ENABLED:
5235 stack_enabled = atoi(value);
5237 case SWM_S_CLOCK_ENABLED:
5238 clock_enabled = atoi(value);
5240 case SWM_S_CLOCK_FORMAT:
5241 #ifndef SWM_DENY_CLOCK_FORMAT
5243 if ((clock_format = strdup(value)) == NULL)
5244 err(1, "setconfvalue: clock_format");
5247 case SWM_S_CYCLE_EMPTY:
5248 cycle_empty = atoi(value);
5250 case SWM_S_CYCLE_VISIBLE:
5251 cycle_visible = atoi(value);
5253 case SWM_S_SS_ENABLED:
5254 ss_enabled = atoi(value);
5256 case SWM_S_TERM_WIDTH:
5257 term_width = atoi(value);
5259 case SWM_S_TITLE_CLASS_ENABLED:
5260 title_class_enabled = atoi(value);
5262 case SWM_S_WINDOW_NAME_ENABLED:
5263 window_name_enabled = atoi(value);
5265 case SWM_S_TITLE_NAME_ENABLED:
5266 title_name_enabled = atoi(value);
5268 case SWM_S_URGENT_ENABLED:
5269 urgent_enabled = atoi(value);
5271 case SWM_S_FOCUS_MODE:
5272 if (!strcmp(value, "default"))
5273 focus_mode = SWM_FOCUS_DEFAULT;
5274 else if (!strcmp(value, "follow_cursor"))
5275 focus_mode = SWM_FOCUS_FOLLOW;
5276 else if (!strcmp(value, "synergy"))
5277 focus_mode = SWM_FOCUS_SYNERGY;
5279 errx(1, "focus_mode");
5281 case SWM_S_DISABLE_BORDER:
5282 disable_border = atoi(value);
5284 case SWM_S_BORDER_WIDTH:
5285 border_width = atoi(value);
5287 case SWM_S_BAR_FONT:
5289 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5290 err(1, "setconfvalue: asprintf: failed to allocate "
5291 "memory for bar_fonts.");
5295 case SWM_S_BAR_ACTION:
5297 if ((bar_argv[0] = strdup(value)) == NULL)
5298 err(1, "setconfvalue: bar_action");
5300 case SWM_S_SPAWN_TERM:
5301 free(spawn_term[0]);
5302 if ((spawn_term[0] = strdup(value)) == NULL)
5303 err(1, "setconfvalue: spawn_term");
5307 case SWM_S_DIALOG_RATIO:
5308 dialog_ratio = atof(value);
5309 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5312 case SWM_S_VERBOSE_LAYOUT:
5313 verbose_layout = atoi(value);
5314 for (i = 0; layouts[i].l_stack != NULL; i++) {
5316 layouts[i].l_string = fancy_stacker;
5318 layouts[i].l_string = plain_stacker;
5328 setconfmodkey(char *selector, char *value, int flags)
5330 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5331 update_modkey(Mod1Mask);
5332 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5333 update_modkey(Mod2Mask);
5334 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5335 update_modkey(Mod3Mask);
5336 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5337 update_modkey(Mod4Mask);
5344 setconfcolor(char *selector, char *value, int flags)
5346 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5351 setconfregion(char *selector, char *value, int flags)
5353 custom_region(value);
5358 setautorun(char *selector, char *value, int flags)
5368 if (getenv("SWM_STARTED"))
5372 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5373 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5375 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5376 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5379 * This is a little intricate
5381 * If the pid already exists we simply reuse it because it means it was
5382 * used before AND not claimed by manage_window. We get away with
5383 * altering it in the parent after INSERT because this can not be a race
5386 while ((ap = strsep(&sp, " \t")) != NULL) {
5389 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5391 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5392 err(1, "setautorun: realloc");
5393 a.argv[argc - 1] = ap;
5396 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5397 err(1, "setautorun: realloc");
5398 a.argv[argc] = NULL;
5400 if ((pid = fork()) == 0) {
5401 spawn(ws_id, &a, 1);
5410 p = calloc(1, sizeof *p);
5413 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5423 setlayout(char *selector, char *value, int flags)
5425 int ws_id, i, x, mg, ma, si, raise;
5426 int st = SWM_V_STACK;
5428 struct workspace *ws;
5430 if (getenv("SWM_STARTED"))
5434 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5435 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5436 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5437 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5440 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5441 errx(1, "layout: invalid workspace %d", ws_id + 1);
5443 if (!strcasecmp(s, "vertical"))
5445 else if (!strcasecmp(s, "horizontal"))
5447 else if (!strcasecmp(s, "fullscreen"))
5450 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5451 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5454 for (i = 0; i < ScreenCount(display); i++) {
5455 ws = (struct workspace *)&screens[i].ws;
5456 ws[ws_id].cur_layout = &layouts[st];
5458 ws[ws_id].always_raise = raise;
5459 if (st == SWM_MAX_STACK)
5463 for (x = 0; x < abs(mg); x++) {
5464 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5465 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5466 SWM_ARG_ID_MASTERSHRINK);
5470 for (x = 0; x < abs(ma); x++) {
5471 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5472 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5473 SWM_ARG_ID_MASTERDEL);
5477 for (x = 0; x < abs(si); x++) {
5478 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5479 si >= 0 ? SWM_ARG_ID_STACKINC :
5480 SWM_ARG_ID_STACKDEC);
5488 /* config options */
5489 struct config_option {
5491 int (*func)(char*, char*, int);
5494 struct config_option configopt[] = {
5495 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5496 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5497 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5498 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5499 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5500 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5501 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5502 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5503 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5504 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5505 { "keyboard_mapping", setkeymapping, 0 },
5506 { "bind", setconfbinding, 0 },
5507 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5508 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5509 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5510 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5511 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5512 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5513 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5514 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5515 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5516 { "modkey", setconfmodkey, 0 },
5517 { "program", setconfspawn, 0 },
5518 { "quirk", setconfquirk, 0 },
5519 { "region", setconfregion, 0 },
5520 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5521 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5522 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5523 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5524 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5525 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5526 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5527 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5528 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5529 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5530 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5531 { "autorun", setautorun, 0 },
5532 { "layout", setlayout, 0 },
5537 conf_load(char *filename, int keymapping)
5540 char *line, *cp, *optsub, *optval;
5541 size_t linelen, lineno = 0;
5542 int wordlen, i, optind;
5543 struct config_option *opt;
5545 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5547 if (filename == NULL) {
5548 warnx("conf_load: no filename");
5551 if ((config = fopen(filename, "r")) == NULL) {
5552 warn("conf_load: fopen: %s", filename);
5556 while (!feof(config)) {
5557 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5560 err(1, "%s", filename);
5565 cp += strspn(cp, " \t\n"); /* eat whitespace */
5566 if (cp[0] == '\0') {
5571 /* get config option */
5572 wordlen = strcspn(cp, "=[ \t\n");
5574 warnx("%s: line %zd: no option found",
5579 for (i = 0; i < LENGTH(configopt); i++) {
5580 opt = &configopt[i];
5581 if (!strncasecmp(cp, opt->optname, wordlen) &&
5582 strlen(opt->optname) == wordlen) {
5588 warnx("%s: line %zd: unknown option %.*s",
5589 filename, lineno, wordlen, cp);
5592 if (keymapping && strcmp(opt->optname, "bind")) {
5593 warnx("%s: line %zd: invalid option %.*s",
5594 filename, lineno, wordlen, cp);
5598 cp += strspn(cp, " \t\n"); /* eat whitespace */
5599 /* get [selector] if any */
5603 wordlen = strcspn(cp, "]");
5606 warnx("%s: line %zd: syntax error",
5611 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5613 warnx("%s: line %zd: unable to allocate"
5614 "memory for selector", filename,
5620 cp += strspn(cp, "] \t\n"); /* eat trailing */
5622 cp += strspn(cp, "= \t\n"); /* eat trailing */
5624 optval = strdup(cp);
5625 /* call function to deal with it all */
5626 if (configopt[optind].func(optsub, optval,
5627 configopt[optind].funcflags) != 0)
5628 errx(1, "%s: line %zd: invalid data for %s",
5629 filename, lineno, configopt[optind].optname);
5636 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5643 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
5649 set_child_transient(struct ws_win *win, Window *trans)
5651 struct ws_win *parent, *w;
5652 XWMHints *wmh = NULL;
5653 struct swm_region *r;
5654 struct workspace *ws;
5656 parent = find_window(win->transient);
5658 parent->child_trans = win;
5660 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5661 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
5663 if (win->hints == NULL) {
5664 warnx("no hints for 0x%lx", win->id);
5668 r = root_to_region(win->wa.root);
5670 /* parent doen't exist in our window list */
5671 TAILQ_FOREACH(w, &ws->winlist, entry) {
5675 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5676 warnx("can't get hints for 0x%lx", w->id);
5680 if (win->hints->window_group != wmh->window_group)
5683 w->child_trans = win;
5684 win->transient = w->id;
5686 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5687 "transient to 0x%lx\n", win->transient);
5697 window_get_pid(Window win)
5699 Atom actual_type_return;
5700 int actual_format_return = 0;
5701 unsigned long nitems_return = 0;
5702 unsigned long bytes_after_return = 0;
5706 unsigned char *prop = NULL;
5708 if (XGetWindowProperty(display, win,
5709 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5710 XA_CARDINAL, &actual_type_return, &actual_format_return,
5711 &nitems_return, &bytes_after_return,
5712 (unsigned char**)(void*)&pid) != Success)
5714 if (actual_type_return != XA_CARDINAL)
5725 if (XGetWindowProperty(display, win,
5726 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5727 XA_STRING, &actual_type_return, &actual_format_return,
5728 &nitems_return, &bytes_after_return, &prop) != Success)
5730 if (actual_type_return != XA_STRING)
5735 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5736 /* ignore error because strtonum returns 0 anyway */
5743 manage_window(Window id)
5746 struct workspace *ws;
5747 struct ws_win *win, *ww;
5748 int format, i, ws_idx, n, border_me = 0;
5749 unsigned long nitems, bytes;
5750 Atom ws_idx_atom = 0, type;
5751 Atom *prot = NULL, *pp;
5752 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5753 struct swm_region *r;
5760 if ((win = find_window(id)) != NULL)
5761 return (win); /* already being managed */
5763 /* see if we are on the unmanaged list */
5764 if ((win = find_unmanaged_window(id)) != NULL) {
5765 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
5766 "window: 0x%lx\n", win->id);
5767 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5768 if (win->transient) {
5769 set_child_transient(win, &trans);
5770 } if (trans && (ww = find_window(trans)))
5771 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5773 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5774 ewmh_update_actions(win);
5778 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5779 err(1, "manage_window: calloc: failed to allocate memory for "
5784 /* see if we need to override the workspace */
5785 p = find_pid(window_get_pid(id));
5787 /* Get all the window data in one shot */
5788 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5790 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5791 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5793 XGetWindowAttributes(display, id, &win->wa);
5794 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
5795 win->hints = XGetWMHints(display, id);
5796 XGetTransientForHint(display, id, &trans);
5798 win->transient = trans;
5799 set_child_transient(win, &trans);
5800 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
5801 "transient: 0x%lx\n", win->id, win->transient);
5804 /* get supported protocols */
5805 if (XGetWMProtocols(display, id, &prot, &n)) {
5806 for (i = 0, pp = prot; i < n; i++, pp++) {
5807 if (*pp == takefocus)
5808 win->take_focus = 1;
5810 win->can_delete = 1;
5816 win->iconic = get_iconic(win);
5819 * Figure out where to put the window. If it was previously assigned to
5820 * a workspace (either by spawn() or manually moving), and isn't
5821 * transient, * put it in the same workspace
5823 r = root_to_region(win->wa.root);
5825 ws = &r->s->ws[p->ws];
5826 TAILQ_REMOVE(&pidlist, p, entry);
5829 } else if (prop && win->transient == 0) {
5830 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
5831 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5833 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
5836 ws = &r->s->ws[ws_idx];
5839 /* this should launch transients in the same ws as parent */
5841 if ((ww = find_window(trans)) != NULL)
5847 warnx("manage_window: fix this "
5853 /* set up the window layout */
5856 win->s = r->s; /* this never changes */
5857 if (trans && (ww = find_window(trans)))
5858 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5860 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5862 WIDTH(win) = win->wa.width;
5863 HEIGHT(win) = win->wa.height;
5866 win->g_floatvalid = 0;
5867 win->floatmaxed = 0;
5868 win->ewmh_flags = 0;
5870 /* Set window properties so we can remember this after reincarnation */
5871 if (ws_idx_atom && prop == NULL &&
5872 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5874 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
5876 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5877 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5882 ewmh_autoquirk(win);
5884 if (XGetClassHint(display, win->id, &win->ch)) {
5885 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
5886 win->ch.res_class, win->ch.res_name);
5888 /* java is retarded so treat it special */
5889 if (strstr(win->ch.res_name, "sun-awt")) {
5894 TAILQ_FOREACH(qp, &quirks, entry) {
5895 if (!strcmp(win->ch.res_class, qp->class) &&
5896 !strcmp(win->ch.res_name, qp->name)) {
5897 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
5898 "class: %s, name: %s\n", win->ch.res_class,
5900 if (qp->quirk & SWM_Q_FLOAT) {
5904 win->quirks = qp->quirk;
5909 /* alter window position if quirky */
5910 if (win->quirks & SWM_Q_ANYWHERE) {
5911 win->manual = 1; /* don't center the quirky windows */
5912 bzero(&wc, sizeof wc);
5914 if (bar_enabled && Y(win) < bar_height) {
5915 Y(win) = wc.y = bar_height;
5918 if (WIDTH(win) + X(win) > WIDTH(r)) {
5919 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
5925 /* Reset font sizes (the bruteforce way; no default keybinding). */
5926 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5927 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5928 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5929 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5930 fake_keypress(win, XK_KP_Add, ShiftMask);
5933 ewmh_get_win_state(win);
5934 ewmh_update_actions(win);
5935 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5939 bzero(&wc, sizeof wc);
5940 wc.border_width = border_width;
5941 mask = CWBorderWidth;
5942 XConfigureWindow(display, win->id, mask, &wc);
5945 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5946 PropertyChangeMask | StructureNotifyMask);
5948 /* floaters need to be mapped if they are in the current workspace */
5949 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5950 XMapRaised(display, win->id);
5956 free_window(struct ws_win *win)
5958 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
5963 /* needed for restart wm */
5964 set_win_state(win, WithdrawnState);
5966 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5968 if (win->ch.res_class)
5969 XFree(win->ch.res_class);
5970 if (win->ch.res_name)
5971 XFree(win->ch.res_name);
5976 memset(win, 0xff, sizeof *win); /* XXX kill later */
5982 unmanage_window(struct ws_win *win)
5984 struct ws_win *parent;
5989 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
5991 if (win->transient) {
5992 parent = find_window(win->transient);
5994 parent->child_trans = NULL;
5997 /* focus on root just in case */
5998 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6007 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6008 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6014 focus_magic(struct ws_win *win)
6016 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6019 /* if there are no windows clear the status-bar */
6024 if (win->child_trans) {
6025 /* win = parent & has a transient so focus on that */
6027 focus_win(win->child_trans);
6028 if (win->child_trans->take_focus)
6029 client_msg(win, takefocus);
6031 /* make sure transient hasn't disappeared */
6032 if (validate_win(win->child_trans) == 0) {
6033 focus_win(win->child_trans);
6034 if (win->child_trans->take_focus)
6035 client_msg(win->child_trans, takefocus);
6037 win->child_trans = NULL;
6039 if (win->take_focus)
6040 client_msg(win, takefocus);
6046 if (win->take_focus)
6047 client_msg(win, takefocus);
6054 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6061 XKeyEvent *ev = &e->xkey;
6063 struct swm_region *r;
6065 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
6066 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6068 if (keyfuncs[kp->funcid].func == NULL)
6071 r = root_to_region(ev->root);
6072 if (kp->funcid == kf_spawn_custom)
6073 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6075 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6079 buttonpress(XEvent *e)
6083 XButtonPressedEvent *ev = &e->xbutton;
6085 if ((win = find_window(ev->window)) == NULL)
6089 action = client_click;
6091 for (i = 0; i < LENGTH(buttons); i++)
6092 if (action == buttons[i].action && buttons[i].func &&
6093 buttons[i].button == ev->button &&
6094 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6095 buttons[i].func(win, &buttons[i].args);
6099 configurerequest(XEvent *e)
6101 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6106 if ((win = find_window(ev->window)) == NULL)
6107 if ((win = find_unmanaged_window(ev->window)) == NULL)
6110 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6111 ev->window, YESNO(new));
6114 bzero(&wc, sizeof wc);
6117 wc.width = ev->width;
6118 wc.height = ev->height;
6119 wc.border_width = ev->border_width;
6120 wc.sibling = ev->above;
6121 wc.stack_mode = ev->detail;
6122 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6124 config_win(win, ev);
6129 configurenotify(XEvent *e)
6133 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6134 e->xconfigure.window);
6136 win = find_window(e->xconfigure.window);
6138 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6142 if (focus_mode == SWM_FOCUS_DEFAULT)
6143 drain_enter_notify();
6148 destroynotify(XEvent *e)
6151 XDestroyWindowEvent *ev = &e->xdestroywindow;
6153 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6155 if ((win = find_window(ev->window)) == NULL) {
6156 if ((win = find_unmanaged_window(ev->window)) == NULL)
6162 /* make sure we focus on something */
6165 unmanage_window(win);
6167 if (focus_mode == SWM_FOCUS_DEFAULT)
6168 drain_enter_notify();
6173 enternotify(XEvent *e)
6175 XCrossingEvent *ev = &e->xcrossing;
6180 Window focus_return;
6181 int revert_to_return;
6183 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6184 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6185 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6186 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6188 if (ev->mode != NotifyNormal) {
6189 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6194 switch (focus_mode) {
6195 case SWM_FOCUS_DEFAULT:
6197 case SWM_FOCUS_FOLLOW:
6199 case SWM_FOCUS_SYNERGY:
6202 * all these checks need to be in this order because the
6203 * XCheckTypedWindowEvent relies on weeding out the previous events
6205 * making this code an option would enable a follow mouse for focus
6210 * state is set when we are switching workspaces and focus is set when
6211 * the window or a subwindow already has focus (occurs during restart).
6213 * Only honor the focus flag if last_focus_event is not FocusOut,
6214 * this allows scrotwm to continue to control focus when another
6215 * program is also playing with it.
6217 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6218 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6223 * happens when a window is created or destroyed and the border
6224 * crosses the mouse pointer and when switching ws
6226 * we need the subwindow test to see if we came from root in order
6227 * to give focus to floaters
6229 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6230 ev->subwindow == 0) {
6231 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6235 /* this window already has focus */
6236 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6237 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6241 /* this window is being deleted or moved to another ws */
6242 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6244 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6245 XPutBackEvent(display, &cne);
6249 if ((win = find_window(ev->window)) == NULL) {
6250 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6255 * In fullstack kill all enters unless they come from a different ws
6256 * (i.e. another region) or focus has been grabbed externally.
6258 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6259 last_focus_event != FocusOut) {
6260 XGetInputFocus(display, &focus_return, &revert_to_return);
6261 if ((w = find_window(focus_return)) == NULL ||
6263 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6271 if ((win = find_window(ev->window)) == NULL) {
6272 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6277 * if we have more enternotifies let them handle it in due time
6279 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6280 DNPRINTF(SWM_D_EVENT,
6281 "ignoring enternotify: got more enternotify\n");
6282 XPutBackEvent(display, &cne);
6289 /* lets us use one switch statement for arbitrary mode/detail combinations */
6290 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6293 focusevent(XEvent *e)
6297 u_int32_t mode_detail;
6298 XFocusChangeEvent *ev = &e->xfocus;
6300 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6301 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6302 ev->window, ev->mode, ev->detail);
6304 if (last_focus_event == ev->type) {
6305 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6309 last_focus_event = ev->type;
6310 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6312 switch (mode_detail) {
6313 /* synergy client focus operations */
6314 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6315 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6317 /* synergy server focus operations */
6318 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6320 /* Entering applications like rdesktop that mangle the pointer */
6321 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6323 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6324 XSetWindowBorder(display, win->id,
6325 win->ws->r->s->c[ev->type == FocusIn ?
6326 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6329 warnx("ignoring focusevent");
6330 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6337 mapnotify(XEvent *e)
6340 XMapEvent *ev = &e->xmap;
6342 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6344 win = manage_window(ev->window);
6346 set_win_state(win, NormalState);
6350 mappingnotify(XEvent *e)
6352 XMappingEvent *ev = &e->xmapping;
6354 XRefreshKeyboardMapping(ev);
6355 if (ev->request == MappingKeyboard)
6360 maprequest(XEvent *e)
6363 struct swm_region *r;
6364 XWindowAttributes wa;
6365 XMapRequestEvent *ev = &e->xmaprequest;
6367 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6368 e->xmaprequest.window);
6370 if (!XGetWindowAttributes(display, ev->window, &wa))
6372 if (wa.override_redirect)
6375 win = manage_window(e->xmaprequest.window);
6377 return; /* can't happen */
6381 /* make new win focused */
6382 r = root_to_region(win->wa.root);
6383 if (win->ws == r->ws)
6388 propertynotify(XEvent *e)
6391 XPropertyEvent *ev = &e->xproperty;
6394 name = XGetAtomName(display, ev->atom);
6395 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6400 win = find_window(ev->window);
6404 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6405 update_iconic(win, 0);
6406 XMapRaised(display, win->id);
6414 case XA_WM_NORMAL_HINTS:
6416 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6417 warnx("normal hints: flag 0x%x", win->sh.flags);
6418 if (win->sh.flags & PMinSize) {
6419 WIDTH(win) = win->sh.min_width;
6420 HEIGHT(win) = win->sh.min_height;
6421 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6423 XMoveResizeWindow(display, win->id,
6424 X(win), Y(win), WIDTH(win), HEIGHT(win));
6427 if (title_name_enabled || title_class_enabled)
6431 if (window_name_enabled)
6440 unmapnotify(XEvent *e)
6444 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6446 /* determine if we need to help unmanage this window */
6447 win = find_window(e->xunmap.window);
6451 if (getstate(e->xunmap.window) == NormalState) {
6452 unmanage_window(win);
6455 /* giant hack for apps that don't destroy transient windows */
6456 /* eat a bunch of events to prevent remanaging the window */
6458 while (XCheckWindowEvent(display, e->xunmap.window,
6459 EnterWindowMask, &cne))
6461 while (XCheckWindowEvent(display, e->xunmap.window,
6462 StructureNotifyMask, &cne))
6464 while (XCheckWindowEvent(display, e->xunmap.window,
6465 SubstructureNotifyMask, &cne))
6467 /* resend unmap because we ated it */
6468 XUnmapWindow(display, e->xunmap.window);
6471 if (focus_mode == SWM_FOCUS_DEFAULT)
6472 drain_enter_notify();
6476 visibilitynotify(XEvent *e)
6479 struct swm_region *r;
6481 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6482 e->xvisibility.window);
6483 if (e->xvisibility.state == VisibilityUnobscured)
6484 for (i = 0; i < ScreenCount(display); i++)
6485 TAILQ_FOREACH(r, &screens[i].rl, entry)
6486 if (e->xvisibility.window == r->bar_window)
6491 clientmessage(XEvent *e)
6493 XClientMessageEvent *ev;
6498 win = find_window(ev->window);
6502 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6503 ev->window, ev->message_type);
6505 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6506 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6509 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6510 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6511 if (win->can_delete)
6512 client_msg(win, adelete);
6514 XKillClient(display, win->id);
6516 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6517 DNPRINTF(SWM_D_EVENT,
6518 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6519 if (win->floating) {
6520 if (ev->data.l[0] & (1<<8)) /* x */
6521 X(win) = ev->data.l[1];
6522 if (ev->data.l[0] & (1<<9)) /* y */
6523 Y(win) = ev->data.l[2];
6524 if (ev->data.l[0] & (1<<10)) /* width */
6525 WIDTH(win) = ev->data.l[3];
6526 if (ev->data.l[0] & (1<<11)) /* height */
6527 HEIGHT(win) = ev->data.l[4];
6532 /* TODO: Change stack sizes */
6533 /* notify no change was made. */
6534 config_win(win, NULL);
6537 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6538 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6539 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6541 ewmh_update_win_state(win, ev->data.l[2],
6549 xerror_start(Display *d, XErrorEvent *ee)
6556 xerror(Display *d, XErrorEvent *ee)
6558 /* warnx("error: %p %p", display, ee); */
6566 xerrorxlib = XSetErrorHandler(xerror_start);
6568 /* this causes an error if some other window manager is running */
6569 XSelectInput(display, DefaultRootWindow(display),
6570 SubstructureRedirectMask);
6571 XSync(display, False);
6575 XSetErrorHandler(xerror);
6576 XSync(display, False);
6581 new_region(struct swm_screen *s, int x, int y, int w, int h)
6583 struct swm_region *r, *n;
6584 struct workspace *ws = NULL;
6587 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6588 s->idx, w, h, x, y);
6590 /* remove any conflicting regions */
6591 n = TAILQ_FIRST(&s->rl);
6594 n = TAILQ_NEXT(r, entry);
6595 if (X(r) < (x + w) &&
6596 (X(r) + WIDTH(r)) > x &&
6598 (Y(r) + HEIGHT(r)) > y) {
6599 if (r->ws->r != NULL)
6600 r->ws->old_r = r->ws->r;
6602 XDestroyWindow(display, r->bar_window);
6603 TAILQ_REMOVE(&s->rl, r, entry);
6604 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6608 /* search old regions for one to reuse */
6610 /* size + location match */
6611 TAILQ_FOREACH(r, &s->orl, entry)
6612 if (X(r) == x && Y(r) == y &&
6613 HEIGHT(r) == h && WIDTH(r) == w)
6617 TAILQ_FOREACH(r, &s->orl, entry)
6618 if (HEIGHT(r) == h && WIDTH(r) == w)
6622 TAILQ_REMOVE(&s->orl, r, entry);
6623 /* try to use old region's workspace */
6624 if (r->ws->r == NULL)
6627 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6628 err(1, "new_region: calloc: failed to allocate memory "
6631 /* if we don't have a workspace already, find one */
6633 for (i = 0; i < SWM_WS_MAX; i++)
6634 if (s->ws[i].r == NULL) {
6641 errx(1, "new_region: no free workspaces");
6652 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6658 #ifdef SWM_XRR_HAS_CRTC
6660 XRRScreenResources *sr;
6663 #endif /* SWM_XRR_HAS_CRTC */
6664 struct swm_region *r;
6667 if (i >= ScreenCount(display))
6668 errx(1, "scan_xrandr: invalid screen");
6670 /* remove any old regions */
6671 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6672 r->ws->old_r = r->ws->r = NULL;
6673 XDestroyWindow(display, r->bar_window);
6674 TAILQ_REMOVE(&screens[i].rl, r, entry);
6675 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6679 /* map virtual screens onto physical screens */
6680 #ifdef SWM_XRR_HAS_CRTC
6681 if (xrandr_support) {
6682 sr = XRRGetScreenResources(display, screens[i].root);
6684 new_region(&screens[i], 0, 0,
6685 DisplayWidth(display, i),
6686 DisplayHeight(display, i));
6690 for (c = 0, ci = NULL; c < ncrtc; c++) {
6691 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6692 if (ci->noutput == 0)
6695 if (ci != NULL && ci->mode == None)
6696 new_region(&screens[i], 0, 0,
6697 DisplayWidth(display, i),
6698 DisplayHeight(display, i));
6700 new_region(&screens[i],
6701 ci->x, ci->y, ci->width, ci->height);
6704 XRRFreeCrtcInfo(ci);
6705 XRRFreeScreenResources(sr);
6707 #endif /* SWM_XRR_HAS_CRTC */
6709 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6710 DisplayHeight(display, i));
6715 screenchange(XEvent *e) {
6716 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6717 struct swm_region *r;
6720 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
6722 if (!XRRUpdateConfiguration(e))
6725 /* silly event doesn't include the screen index */
6726 for (i = 0; i < ScreenCount(display); i++)
6727 if (screens[i].root == xe->root)
6729 if (i >= ScreenCount(display))
6730 errx(1, "screenchange: screen not found");
6732 /* brute force for now, just re-enumerate the regions */
6735 /* add bars to all regions */
6736 for (i = 0; i < ScreenCount(display); i++)
6737 TAILQ_FOREACH(r, &screens[i].rl, entry)
6740 if (focus_mode == SWM_FOCUS_DEFAULT)
6741 drain_enter_notify();
6747 Window d1, d2, *wins = NULL;
6748 XWindowAttributes wa;
6753 for (i = 0; i < ScreenCount(display); i++) {
6754 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6757 /* attach windows to a region */
6758 /* normal windows */
6759 for (j = 0; j < no; j++) {
6760 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6761 wa.override_redirect ||
6762 XGetTransientForHint(display, wins[j], &d1))
6765 state = getstate(wins[j]);
6766 manage = state == IconicState;
6767 if (wa.map_state == IsViewable || manage)
6768 manage_window(wins[j]);
6770 /* transient windows */
6771 for (j = 0; j < no; j++) {
6772 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6773 wa.override_redirect)
6776 state = getstate(wins[j]);
6777 manage = state == IconicState;
6778 if (XGetTransientForHint(display, wins[j], &d1) &&
6780 manage_window(wins[j]);
6793 int errorbase, major, minor;
6794 struct workspace *ws;
6797 if ((screens = calloc(ScreenCount(display),
6798 sizeof(struct swm_screen))) == NULL)
6799 err(1, "setup_screens: calloc: failed to allocate memory for "
6802 /* initial Xrandr setup */
6803 xrandr_support = XRRQueryExtension(display,
6804 &xrandr_eventbase, &errorbase);
6806 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6809 /* map physical screens */
6810 for (i = 0; i < ScreenCount(display); i++) {
6811 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
6813 TAILQ_INIT(&screens[i].rl);
6814 TAILQ_INIT(&screens[i].orl);
6815 screens[i].root = RootWindow(display, i);
6817 /* set default colors */
6818 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6819 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6820 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6821 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6822 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6824 /* create graphics context on screen with default font color */
6825 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
6828 XSetForeground(display, screens[i].bar_gc,
6829 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6831 /* set default cursor */
6832 XDefineCursor(display, screens[i].root,
6833 XCreateFontCursor(display, XC_left_ptr));
6835 /* init all workspaces */
6836 /* XXX these should be dynamically allocated too */
6837 for (j = 0; j < SWM_WS_MAX; j++) {
6838 ws = &screens[i].ws[j];
6844 TAILQ_INIT(&ws->winlist);
6845 TAILQ_INIT(&ws->unmanagedlist);
6847 for (k = 0; layouts[k].l_stack != NULL; k++)
6848 if (layouts[k].l_config != NULL)
6849 layouts[k].l_config(ws,
6850 SWM_ARG_ID_STACKINIT);
6851 ws->cur_layout = &layouts[0];
6852 ws->cur_layout->l_string(ws);
6858 XRRSelectInput(display, screens[i].root,
6859 RRScreenChangeNotifyMask);
6866 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
6867 err(1, "setup_globals: strdup: failed to allocate memory.");
6869 if ((spawn_term[0] = strdup("xterm")) == NULL)
6870 err(1, "setup_globals: strdup: failed to allocate memory.");
6872 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6873 err(1, "setup_globals: strdup: failed to allocate memory.");
6880 Atom netwmcheck, netwmname, utf8_string;
6883 /* work around sun jdk bugs, code from wmname */
6884 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6885 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6886 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6887 for (i = 0; i < ScreenCount(display); i++) {
6888 root = screens[i].root;
6889 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6890 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6891 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6893 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6894 PropModeReplace, (unsigned char *)&win,1);
6895 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6896 PropModeReplace, (unsigned char *)&win,1);
6897 XChangeProperty(display, win, netwmname, utf8_string, 8,
6898 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6903 main(int argc, char *argv[])
6905 struct swm_region *r, *rr;
6906 struct ws_win *winfocus = NULL;
6909 char conf[PATH_MAX], *cfile = NULL;
6914 struct sigaction sact;
6917 warnx("Welcome to scrotwm V%s Build: %s", SCROTWM_VERSION, buildstr);
6918 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6920 warnx("no locale support");
6922 if (!X_HAVE_UTF8_STRING)
6923 warnx("no UTF-8 support");
6925 if (!(display = XOpenDisplay(0)))
6926 errx(1, "can not open display");
6929 errx(1, "other wm running");
6931 /* handle some signals */
6932 bzero(&sact, sizeof(sact));
6933 sigemptyset(&sact.sa_mask);
6935 sact.sa_handler = sighdlr;
6936 sigaction(SIGINT, &sact, NULL);
6937 sigaction(SIGQUIT, &sact, NULL);
6938 sigaction(SIGTERM, &sact, NULL);
6939 sigaction(SIGHUP, &sact, NULL);
6941 sact.sa_handler = sighdlr;
6942 sact.sa_flags = SA_NOCLDSTOP;
6943 sigaction(SIGCHLD, &sact, NULL);
6945 astate = XInternAtom(display, "WM_STATE", False);
6946 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6947 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6948 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6949 a_wmname = XInternAtom(display, "WM_NAME", False);
6950 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6951 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6952 a_string = XInternAtom(display, "STRING", False);
6953 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6955 /* look for local and global conf file */
6956 pwd = getpwuid(getuid());
6958 errx(1, "invalid user: %d", getuid());
6967 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6968 if (stat(conf, &sb) != -1) {
6969 if (S_ISREG(sb.st_mode))
6972 /* try global conf file */
6973 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6974 if (!stat(conf, &sb))
6975 if (S_ISREG(sb.st_mode))
6979 /* load conf (if any) and refresh font color in bar graphics contexts */
6980 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
6981 for (i = 0; i < ScreenCount(display); ++i)
6982 XSetForeground(display, screens[i].bar_gc,
6983 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6986 /* set some values to work around bad programs */
6988 /* grab existing windows (before we build the bars) */
6991 if (getenv("SWM_STARTED") == NULL)
6992 setenv("SWM_STARTED", "YES", 1);
6994 /* setup all bars */
6995 for (i = 0; i < ScreenCount(display); i++)
6996 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6997 if (winfocus == NULL)
6998 winfocus = TAILQ_FIRST(&r->ws->winlist);
7006 if (focus_mode == SWM_FOCUS_DEFAULT)
7007 drain_enter_notify();
7009 xfd = ConnectionNumber(display);
7011 while (XPending(display)) {
7012 XNextEvent(display, &e);
7015 if (e.type < LASTEvent) {
7016 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7017 "window: 0x%lx, type: %s (%d), %d remaining"
7018 "\n", YESNO(handler[e.type]),
7019 e.xany.window, geteventname(&e),
7020 e.type, QLength(display));
7022 if (handler[e.type])
7023 handler[e.type](&e);
7025 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7026 "0x%lx, type: %s (%d)\n", e.xany.window,
7027 xrandr_geteventname(&e), e.type);
7029 switch (e.type - xrandr_eventbase) {
7030 case RRScreenChangeNotify:
7039 /* if we are being restarted go focus on first window */
7041 rr = winfocus->ws->r;
7043 /* not a visible window */
7047 /* move pointer to first screen if multi screen */
7048 if (ScreenCount(display) > 1 || outputs > 1)
7049 XWarpPointer(display, None, rr->s[0].root,
7051 Y(rr) + (bar_enabled ? bar_height : 0));
7053 a.id = SWM_ARG_ID_FOCUSCUR;
7063 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7065 DNPRINTF(SWM_D_MISC, "select failed");
7066 if (restart_wm == 1)
7067 restart(NULL, NULL);
7068 if (search_resp == 1)
7081 for (i = 0; i < ScreenCount(display); ++i)
7082 if (screens[i].bar_gc != NULL)
7083 XFreeGC(display, screens[i].bar_gc);
7085 XFreeFontSet(display, bar_fs);
7086 XCloseDisplay(display);