3 * Copyright (c) 2009-2010 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag = "$scrotwm$";
55 #define SWM_VERSION "0.9.29"
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
80 #include <X11/cursorfont.h>
81 #include <X11/keysym.h>
82 #include <X11/Xatom.h>
84 #include <X11/Xproto.h>
85 #include <X11/Xutil.h>
86 #include <X11/extensions/Xrandr.h>
93 # error XRandR versions less than 1.0 are not supported
98 #define SWM_XRR_HAS_CRTC
102 /* #define SWM_DEBUG */
104 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
105 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
106 #define SWM_D_MISC 0x0001
107 #define SWM_D_EVENT 0x0002
108 #define SWM_D_WS 0x0004
109 #define SWM_D_FOCUS 0x0008
110 #define SWM_D_MOVE 0x0010
111 #define SWM_D_STACK 0x0020
112 #define SWM_D_MOUSE 0x0040
113 #define SWM_D_PROP 0x0080
114 #define SWM_D_CLASS 0x0100
115 #define SWM_D_KEY 0x0200
116 #define SWM_D_QUIRK 0x0400
117 #define SWM_D_SPAWN 0x0800
118 #define SWM_D_EVENTQ 0x1000
119 #define SWM_D_CONF 0x2000
121 u_int32_t swm_debug = 0
138 #define DPRINTF(x...)
139 #define DNPRINTF(n,x...)
142 #define LENGTH(x) (sizeof x / sizeof x[0])
143 #define MODKEY Mod1Mask
144 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
145 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
146 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
147 #define SWM_PROPLEN (16)
148 #define SWM_FUNCNAME_LEN (32)
149 #define SWM_KEYS_LEN (255)
150 #define SWM_QUIRK_LEN (64)
151 #define X(r) (r)->g.x
152 #define Y(r) (r)->g.y
153 #define WIDTH(r) (r)->g.w
154 #define HEIGHT(r) (r)->g.h
155 #define SWM_MAX_FONT_STEPS (3)
156 #define WINID(w) (w ? w->id : 0)
158 #define SWM_FOCUS_DEFAULT (0)
159 #define SWM_FOCUS_SYNERGY (1)
160 #define SWM_FOCUS_FOLLOW (2)
163 #define SWM_LIB "/usr/local/lib/libswmhack.so"
171 volatile sig_atomic_t running = 1;
172 volatile sig_atomic_t restart_wm = 0;
174 int last_focus_event = FocusOut;
175 int (*xerrorxlib)(Display *, XErrorEvent *);
179 int xrandr_eventbase;
180 unsigned int numlockmask = 0;
184 int cycle_visible = 0;
186 int font_adjusted = 0;
187 unsigned int mod_key = MODKEY;
190 double dialog_ratio = .6;
192 #define SWM_BAR_MAX (256)
193 char *bar_argv[] = { NULL, NULL };
195 char bar_ext[SWM_BAR_MAX];
196 char bar_vertext[SWM_BAR_MAX];
198 sig_atomic_t bar_alarm = 0;
201 int bar_at_bottom = 0;
203 int bar_extra_running = 0;
206 int stack_enabled = 1;
207 int clock_enabled = 1;
208 char *clock_format = NULL;
209 int title_name_enabled = 0;
210 int title_class_enabled = 0;
211 int window_name_enabled = 0;
212 int focus_mode = SWM_FOCUS_DEFAULT;
213 int disable_border = 0;
214 int border_width = 1;
220 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
221 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
223 #define SWM_MENU_FN (2)
224 #define SWM_MENU_NB (4)
225 #define SWM_MENU_NF (6)
226 #define SWM_MENU_SB (8)
227 #define SWM_MENU_SF (10)
229 /* layout manager data */
230 struct swm_geometry {
240 /* virtual "screens" */
242 TAILQ_ENTRY(swm_region) entry;
243 struct swm_geometry g;
244 struct workspace *ws; /* current workspace on this region */
245 struct workspace *ws_prior; /* prior workspace on this region */
246 struct swm_screen *s; /* screen idx */
249 TAILQ_HEAD(swm_region_list, swm_region);
252 TAILQ_ENTRY(ws_win) entry;
255 struct ws_win *child_trans; /* transient child window */
256 struct swm_geometry g; /* current geometry */
257 struct swm_geometry g_float; /* geometry when floating */
258 struct swm_geometry rg_float; /* region geom when floating */
259 int g_floatvalid; /* flag: geometry in g_float is valid */
260 int floatmaxed; /* flag: floater was maxed in max_stack */
263 unsigned int ewmh_flags;
264 int font_size_boundary[SWM_MAX_FONT_STEPS];
270 unsigned long quirks;
271 struct workspace *ws; /* always valid */
272 struct swm_screen *s; /* always valid, never changes */
273 XWindowAttributes wa;
277 TAILQ_HEAD(ws_win_list, ws_win);
279 /* layout handlers */
281 void vertical_config(struct workspace *, int);
282 void vertical_stack(struct workspace *, struct swm_geometry *);
283 void horizontal_config(struct workspace *, int);
284 void horizontal_stack(struct workspace *, struct swm_geometry *);
285 void max_stack(struct workspace *, struct swm_geometry *);
287 struct ws_win *find_window(Window);
289 void grabbuttons(struct ws_win *, int);
290 void new_region(struct swm_screen *, int, int, int, int);
291 void unmanage_window(struct ws_win *);
292 long getstate(Window);
295 void (*l_stack)(struct workspace *, struct swm_geometry *);
296 void (*l_config)(struct workspace *, int);
298 #define SWM_L_FOCUSPREV (1<<0)
299 #define SWM_L_MAPONFOCUS (1<<1)
302 /* stack, configure */
303 { vertical_stack, vertical_config, 0, "[|]" },
304 { horizontal_stack, horizontal_config, 0, "[-]" },
306 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
307 { NULL, NULL, 0, NULL },
310 /* position of max_stack mode in the layouts array */
311 #define SWM_MAX_STACK 2
313 #define SWM_H_SLICE (32)
314 #define SWM_V_SLICE (32)
316 /* define work spaces */
318 int idx; /* workspace index */
319 struct layout *cur_layout; /* current layout handlers */
320 struct ws_win *focus; /* may be NULL */
321 struct ws_win *focus_prev; /* may be NULL */
322 struct swm_region *r; /* may be NULL */
323 struct swm_region *old_r; /* may be NULL */
324 struct ws_win_list winlist; /* list of windows in ws */
325 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
329 int horizontal_msize;
331 int horizontal_stacks;
338 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
339 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
341 /* physical screen mapping */
342 #define SWM_WS_MAX (10)
344 int idx; /* screen index */
345 struct swm_region_list rl; /* list of regions on this screen */
346 struct swm_region_list orl; /* list of old regions */
348 struct workspace ws[SWM_WS_MAX];
354 } c[SWM_S_COLOR_MAX];
356 struct swm_screen *screens;
359 /* args to functions */
362 #define SWM_ARG_ID_FOCUSNEXT (0)
363 #define SWM_ARG_ID_FOCUSPREV (1)
364 #define SWM_ARG_ID_FOCUSMAIN (2)
365 #define SWM_ARG_ID_FOCUSCUR (4)
366 #define SWM_ARG_ID_SWAPNEXT (10)
367 #define SWM_ARG_ID_SWAPPREV (11)
368 #define SWM_ARG_ID_SWAPMAIN (12)
369 #define SWM_ARG_ID_MOVELAST (13)
370 #define SWM_ARG_ID_MASTERSHRINK (20)
371 #define SWM_ARG_ID_MASTERGROW (21)
372 #define SWM_ARG_ID_MASTERADD (22)
373 #define SWM_ARG_ID_MASTERDEL (23)
374 #define SWM_ARG_ID_STACKRESET (30)
375 #define SWM_ARG_ID_STACKINIT (31)
376 #define SWM_ARG_ID_CYCLEWS_UP (40)
377 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
378 #define SWM_ARG_ID_CYCLESC_UP (42)
379 #define SWM_ARG_ID_CYCLESC_DOWN (43)
380 #define SWM_ARG_ID_STACKINC (50)
381 #define SWM_ARG_ID_STACKDEC (51)
382 #define SWM_ARG_ID_SS_ALL (60)
383 #define SWM_ARG_ID_SS_WINDOW (61)
384 #define SWM_ARG_ID_DONTCENTER (70)
385 #define SWM_ARG_ID_CENTER (71)
386 #define SWM_ARG_ID_KILLWINDOW (80)
387 #define SWM_ARG_ID_DELETEWINDOW (81)
391 void focus(struct swm_region *, union arg *);
392 void focus_magic(struct ws_win *, int);
393 #define SWM_F_GENERIC (0)
394 #define SWM_F_TRANSIENT (1)
400 #define SWM_Q_FLOAT (1<<0) /* float this window */
401 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
402 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
403 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
404 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
406 int quirks_size = 0, quirks_length = 0;
407 struct quirk *quirks = NULL;
410 * Supported EWMH hints should be added to
411 * both the enum and the ewmh array
413 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
414 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
415 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
416 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
417 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
418 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
419 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
420 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
421 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
422 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
428 } ewmh[SWM_EWMH_HINT_MAX] = {
429 /* must be in same order as in the enum */
430 {"_NET_ACTIVE_WINDOW", None},
431 {"_NET_MOVERESIZE_WINDOW", None},
432 {"_NET_CLOSE_WINDOW", None},
433 {"_NET_WM_WINDOW_TYPE", None},
434 {"_NET_WM_WINDOW_TYPE_DOCK", None},
435 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
436 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
437 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
438 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
439 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
440 {"_NET_WM_STATE", None},
441 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
442 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
443 {"_NET_WM_STATE_SKIP_TASKBAR", None},
444 {"_NET_WM_STATE_SKIP_PAGER", None},
445 {"_NET_WM_STATE_HIDDEN", None},
446 {"_NET_WM_STATE_ABOVE", None},
447 {"_SWM_WM_STATE_MANUAL", None},
448 {"_NET_WM_STATE_FULLSCREEN", None},
449 {"_NET_WM_ALLOWED_ACTIONS", None},
450 {"_NET_WM_ACTION_MOVE", None},
451 {"_NET_WM_ACTION_RESIZE", None},
452 {"_NET_WM_ACTION_FULLSCREEN", None},
453 {"_NET_WM_ACTION_CLOSE", None},
456 void store_float_geom(struct ws_win *win, struct swm_region *r);
457 int floating_toggle_win(struct ws_win *win);
460 get_property(Window id, Atom atom, long count, Atom type,
461 unsigned long *n, unsigned char **data)
464 unsigned long tmp, extra;
465 unsigned long *nitems;
468 nitems = n != NULL ? n : &tmp;
469 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
470 &real, &format, nitems, &extra, data);
472 if (status != Success)
486 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
488 for (i = 0; i < LENGTH(ewmh); i++)
489 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
491 for (i = 0; i < ScreenCount(display); i++) {
492 /* Support check window will be created by workaround(). */
494 /* Report supported atoms */
495 XDeleteProperty(display, screens[i].root, sup_list);
496 for (j = 0; j < LENGTH(ewmh); j++)
497 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
498 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
506 unsigned char *data = NULL;
508 Atom sup_check, sup_list;
511 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
512 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
514 for (i = 0; i < ScreenCount(display); i++) {
515 /* Get the support check window and destroy it */
516 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
521 XDestroyWindow(display, id);
522 XDeleteProperty(display, screens[i].root, sup_check);
523 XDeleteProperty(display, screens[i].root, sup_list);
531 ewmh_autoquirk(struct ws_win *win)
534 unsigned long *data = NULL;
538 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
539 XA_ATOM, &n, (unsigned char **)&data);
546 for (i = 0; i < n; i++) {
548 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
550 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
551 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
552 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
554 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
557 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
558 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
560 win->quirks = SWM_Q_FLOAT;
568 #define SWM_EWMH_ACTION_COUNT_MAX (6)
569 #define EWMH_F_FULLSCREEN (1<<0)
570 #define EWMH_F_ABOVE (1<<1)
571 #define EWMH_F_HIDDEN (1<<2)
572 #define EWMH_F_SKIP_PAGER (1<<3)
573 #define EWMH_F_SKIP_TASKBAR (1<<4)
574 #define SWM_F_MANUAL (1<<5)
577 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
579 struct swm_geometry rg;
587 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
593 store_float_geom(win, win->ws->r);
600 if (win->g_floatvalid) {
601 /* refloat at last floating relative position */
602 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
603 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
604 win->g.w = win->g_float.w;
605 win->g.h = win->g_float.h;
613 ewmh_update_actions(struct ws_win *win)
615 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
621 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
624 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
625 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
628 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
629 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
632 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
633 #define _NET_WM_STATE_ADD 1 /* add/set property */
634 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
637 ewmh_update_win_state(struct ws_win *win, long state, long action)
639 unsigned int mask = 0;
640 unsigned int changed = 0;
641 unsigned int orig_flags;
646 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
647 mask = EWMH_F_FULLSCREEN;
648 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
650 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
652 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
653 mask = EWMH_F_SKIP_PAGER;
654 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
655 mask = EWMH_F_SKIP_TASKBAR;
658 orig_flags = win->ewmh_flags;
661 case _NET_WM_STATE_REMOVE:
662 win->ewmh_flags &= ~mask;
664 case _NET_WM_STATE_ADD:
665 win->ewmh_flags |= mask;
667 case _NET_WM_STATE_TOGGLE:
668 win->ewmh_flags ^= mask;
672 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
674 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
676 if (!floating_toggle_win(win))
677 win->ewmh_flags = orig_flags; /* revert */
678 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
680 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
681 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
683 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
684 win->ewmh_flags = orig_flags; /* revert */
686 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
688 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
689 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
690 XA_ATOM, 32, PropModeAppend,
691 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
692 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
693 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
694 XA_ATOM, 32, PropModeAppend,
695 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
696 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
697 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
698 XA_ATOM, 32, PropModeAppend,
699 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
700 if (win->ewmh_flags & EWMH_F_ABOVE)
701 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
702 XA_ATOM, 32, PropModeAppend,
703 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
704 if (win->ewmh_flags & SWM_F_MANUAL)
705 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
706 XA_ATOM, 32, PropModeAppend,
707 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
711 ewmh_get_win_state(struct ws_win *win)
722 win->ewmh_flags |= EWMH_F_ABOVE;
724 win->ewmh_flags |= SWM_F_MANUAL;
726 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
727 &n, (unsigned char **)&states);
732 for (i = 0; i < n; i++)
733 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
753 name = "ButtonPress";
756 name = "ButtonRelease";
759 name = "MotionNotify";
762 name = "EnterNotify";
765 name = "LeaveNotify";
774 name = "KeymapNotify";
780 name = "GraphicsExpose";
785 case VisibilityNotify:
786 name = "VisibilityNotify";
789 name = "CreateNotify";
792 name = "DestroyNotify";
795 name = "UnmapNotify";
804 name = "ReparentNotify";
806 case ConfigureNotify:
807 name = "ConfigureNotify";
809 case ConfigureRequest:
810 name = "ConfigureRequest";
813 name = "GravityNotify";
816 name = "ResizeRequest";
818 case CirculateNotify:
819 name = "CirculateNotify";
821 case CirculateRequest:
822 name = "CirculateRequest";
825 name = "PropertyNotify";
828 name = "SelectionClear";
830 case SelectionRequest:
831 name = "SelectionRequest";
833 case SelectionNotify:
834 name = "SelectionNotify";
837 name = "ColormapNotify";
840 name = "ClientMessage";
843 name = "MappingNotify";
848 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
850 e->xany.window, name, e->type, QLength(display));
852 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
854 e->xany.window, e->type, QLength(display));
858 dumpwins(struct swm_region *r, union arg *args)
862 XWindowAttributes wa;
865 fprintf(stderr, "invalid workspace\n");
869 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
871 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
872 state = getstate(win->id);
873 if (!XGetWindowAttributes(display, win->id, &wa))
874 fprintf(stderr, "window: %lu failed "
875 "XGetWindowAttributes\n", win->id);
876 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
877 win->id, wa.map_state, state);
880 fprintf(stderr, "===== unmanaged window list =====\n");
881 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
882 state = getstate(win->id);
883 if (!XGetWindowAttributes(display, win->id, &wa))
884 fprintf(stderr, "window: %lu failed "
885 "XGetWindowAttributes\n", win->id);
886 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
887 win->id, wa.map_state, state);
890 fprintf(stderr, "=================================\n");
895 dumpwins(struct swm_region *r, union arg *args)
898 #endif /* SWM_DEBUG */
900 void expose(XEvent *);
901 void keypress(XEvent *);
902 void buttonpress(XEvent *);
903 void configurerequest(XEvent *);
904 void configurenotify(XEvent *);
905 void destroynotify(XEvent *);
906 void enternotify(XEvent *);
907 void focusevent(XEvent *);
908 void mapnotify(XEvent *);
909 void mappingnotify(XEvent *);
910 void maprequest(XEvent *);
911 void propertynotify(XEvent *);
912 void unmapnotify(XEvent *);
913 void visibilitynotify(XEvent *);
914 void clientmessage(XEvent *);
916 void (*handler[LASTEvent])(XEvent *) = {
918 [KeyPress] = keypress,
919 [ButtonPress] = buttonpress,
920 [ConfigureRequest] = configurerequest,
921 [ConfigureNotify] = configurenotify,
922 [DestroyNotify] = destroynotify,
923 [EnterNotify] = enternotify,
924 [FocusIn] = focusevent,
925 [FocusOut] = focusevent,
926 [MapNotify] = mapnotify,
927 [MappingNotify] = mappingnotify,
928 [MapRequest] = maprequest,
929 [PropertyNotify] = propertynotify,
930 [UnmapNotify] = unmapnotify,
931 [VisibilityNotify] = visibilitynotify,
932 [ClientMessage] = clientmessage,
938 int saved_errno, status;
945 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
951 warn("sighdlr: waitpid");
952 #endif /* SWM_DEBUG */
957 if (WIFEXITED(status)) {
958 if (WEXITSTATUS(status) != 0)
959 warnx("sighdlr: child exit status: %d",
960 WEXITSTATUS(status));
962 warnx("sighdlr: child is terminated "
964 #endif /* SWM_DEBUG */
982 name_to_color(char *colorname)
986 XColor screen_def, exact_def;
987 unsigned long result = 0;
988 char cname[32] = "#";
990 cmap = DefaultColormap(display, screens[0].idx);
991 status = XAllocNamedColor(display, cmap, colorname,
992 &screen_def, &exact_def);
994 strlcat(cname, colorname + 2, sizeof cname - 1);
995 status = XAllocNamedColor(display, cmap, cname, &screen_def,
999 result = screen_def.pixel;
1001 fprintf(stderr, "color '%s' not found.\n", colorname);
1007 setscreencolor(char *val, int i, int c)
1009 if (i > 0 && i <= ScreenCount(display)) {
1010 screens[i - 1].c[c].color = name_to_color(val);
1011 free(screens[i - 1].c[c].name);
1012 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1014 } else if (i == -1) {
1015 for (i = 0; i < ScreenCount(display); i++) {
1016 screens[i].c[c].color = name_to_color(val);
1017 free(screens[i].c[c].name);
1018 if ((screens[i].c[c].name = strdup(val)) == NULL)
1022 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1023 i, ScreenCount(display));
1027 custom_region(char *val)
1029 unsigned int sidx, x, y, w, h;
1031 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1032 errx(1, "invalid custom region, "
1033 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1034 if (sidx < 1 || sidx > ScreenCount(display))
1035 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1036 sidx, ScreenCount(display));
1040 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1042 if (x < 0 || x > DisplayWidth(display, sidx) ||
1043 y < 0 || y > DisplayHeight(display, sidx) ||
1044 w + x > DisplayWidth(display, sidx) ||
1045 h + y > DisplayHeight(display, sidx)) {
1046 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1047 "(%ux%u)\n", w, h, x, y,
1048 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1052 new_region(&screens[sidx], x, y, w, h);
1056 socket_setnonblock(int fd)
1060 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1061 err(1, "fcntl F_GETFL");
1062 flags |= O_NONBLOCK;
1063 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1064 err(1, "fcntl F_SETFL");
1068 bar_print(struct swm_region *r, char *s)
1070 XClearWindow(display, r->bar_window);
1071 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1072 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1077 bar_extra_stop(void)
1081 bzero(bar_pipe, sizeof bar_pipe);
1084 kill(bar_pid, SIGTERM);
1087 strlcpy(bar_ext, "", sizeof bar_ext);
1092 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1094 int do_class, do_name;
1096 XClassHint *xch = NULL;
1098 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1099 cur_focus != NULL) {
1100 if ((xch = XAllocClassHint()) == NULL)
1102 status = XGetClassHint(display, cur_focus->id, xch);
1103 if (status == BadWindow || status == BadAlloc)
1105 do_class = (title_class_enabled && xch->res_class != NULL);
1106 do_name = (title_name_enabled && xch->res_name != NULL);
1108 strlcat(s, xch->res_class, sz);
1109 if (do_class && do_name)
1110 strlcat(s, ":", sz);
1112 strlcat(s, xch->res_name, sz);
1113 strlcat(s, " ", sz);
1121 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1125 if (window_name_enabled && cur_focus != NULL) {
1126 XFetchName(display, cur_focus->id, &title);
1128 if (cur_focus->floating)
1129 strlcat(s, "(f) ", sz);
1130 strlcat(s, title, sz);
1131 strlcat(s, " ", sz);
1142 struct swm_region *r;
1145 char s[SWM_BAR_MAX];
1146 char cn[SWM_BAR_MAX];
1147 char loc[SWM_BAR_MAX];
1151 if (bar_enabled == 0)
1153 if (bar_extra && bar_extra_running) {
1154 /* ignore short reads; it'll correct itself */
1155 while ((b = fgetln(stdin, &len)) != NULL)
1156 if (b && b[len - 1] == '\n') {
1158 strlcpy(bar_ext, b, sizeof bar_ext);
1160 if (b == NULL && errno != EAGAIN) {
1161 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1162 errno, strerror(errno));
1166 strlcpy(bar_ext, "", sizeof bar_ext);
1168 if (clock_enabled == 0)
1169 strlcpy(s, "", sizeof s);
1172 localtime_r(&tmt, &tm);
1173 strftime(s, sizeof s, clock_format, &tm);
1174 strlcat(s, " ", sizeof s);
1177 for (i = 0; i < ScreenCount(display); i++) {
1179 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1180 strlcpy(cn, "", sizeof cn);
1182 bar_class_name(cn, sizeof cn, r->ws->focus);
1183 bar_window_name(cn, sizeof cn, r->ws->focus);
1187 stack = r->ws->cur_layout->name;
1189 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1190 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1205 bar_toggle(struct swm_region *r, union arg *args)
1207 struct swm_region *tmpr;
1208 int i, sc = ScreenCount(display);
1210 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1213 for (i = 0; i < sc; i++)
1214 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1215 XUnmapWindow(display, tmpr->bar_window);
1217 for (i = 0; i < sc; i++)
1218 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1219 XMapRaised(display, tmpr->bar_window);
1221 bar_enabled = !bar_enabled;
1224 /* must be after stack */
1231 XSetWindowAttributes wa;
1232 struct swm_region *r;
1235 /* do this here because the conf file is in memory */
1236 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1237 /* launch external status app */
1238 bar_extra_running = 1;
1239 if (pipe(bar_pipe) == -1)
1240 err(1, "pipe error");
1241 socket_setnonblock(bar_pipe[0]);
1242 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1243 if (dup2(bar_pipe[0], 0) == -1)
1245 if (dup2(bar_pipe[1], 1) == -1)
1247 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1248 err(1, "could not disable SIGPIPE");
1249 switch (bar_pid = fork()) {
1251 err(1, "cannot fork");
1255 execvp(bar_argv[0], bar_argv);
1256 err(1, "%s external app failed", bar_argv[0]);
1258 default: /* parent */
1264 bzero(&wa, sizeof wa);
1265 for (i = 0; i < ScreenCount(display); i++)
1266 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1268 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1269 wa.background_pixel =
1270 screens[i].c[SWM_S_COLOR_BAR].color;
1271 XChangeWindowAttributes(display, r->bar_window,
1272 CWBackPixel | CWBorderPixel, &wa);
1278 bar_setup(struct swm_region *r)
1283 XFreeFont(display, bar_fs);
1287 for (i = 0; bar_fonts[i] != NULL; i++) {
1288 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1294 if (bar_fonts[i] == NULL)
1295 errx(1, "couldn't load font");
1297 errx(1, "couldn't create font structure");
1299 bar_height = bar_fs->ascent + bar_fs->descent + 3;
1301 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1303 r->bar_window = XCreateSimpleWindow(display,
1304 r->s->root, x, y, WIDTH(r) - 2, bar_height - 2,
1305 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1306 r->s->c[SWM_S_COLOR_BAR].color);
1307 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1308 XSetFont(display, bar_gc, bar_fs->fid);
1309 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1311 XMapRaised(display, r->bar_window);
1312 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1314 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1315 err(1, "could not install bar_signal");
1320 set_win_state(struct ws_win *win, long state)
1322 long data[] = {state, None};
1324 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1329 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1330 (unsigned char *)data, 2);
1337 unsigned char *p = NULL;
1340 if (!get_property(w, astate, 2L, astate, &n, &p))
1343 result = *((long *)p);
1349 version(struct swm_region *r, union arg *args)
1351 bar_version = !bar_version;
1353 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1354 SWM_VERSION, cvstag);
1356 strlcpy(bar_vertext, "", sizeof bar_vertext);
1361 client_msg(struct ws_win *win, Atom a)
1363 XClientMessageEvent cm;
1368 bzero(&cm, sizeof cm);
1369 cm.type = ClientMessage;
1370 cm.window = win->id;
1371 cm.message_type = aprot;
1374 cm.data.l[1] = CurrentTime;
1375 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1379 configreq_win(struct ws_win *win)
1381 XConfigureRequestEvent cr;
1386 bzero(&cr, sizeof cr);
1387 cr.type = ConfigureRequest;
1388 cr.display = display;
1389 cr.parent = win->id;
1390 cr.window = win->id;
1393 cr.width = win->g.w;
1394 cr.height = win->g.h;
1395 cr.border_width = border_width;
1397 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1401 config_win(struct ws_win *win)
1405 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1406 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1411 ce.type = ConfigureNotify;
1412 ce.display = display;
1414 ce.window = win->id;
1417 ce.width = win->g.w;
1418 ce.height = win->g.h;
1419 ce.border_width = border_width; /* XXX store this! */
1421 ce.override_redirect = False;
1422 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1426 count_win(struct workspace *ws, int count_transient)
1431 TAILQ_FOREACH(win, &ws->winlist, entry) {
1432 if (count_transient == 0 && win->floating)
1434 if (count_transient == 0 && win->transient)
1438 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1444 quit(struct swm_region *r, union arg *args)
1446 DNPRINTF(SWM_D_MISC, "quit\n");
1451 unmap_window(struct ws_win *win)
1456 /* don't unmap again */
1457 if (getstate(win->id) == IconicState)
1460 set_win_state(win, IconicState);
1462 XUnmapWindow(display, win->id);
1463 XSetWindowBorder(display, win->id,
1464 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1473 for (i = 0; i < ScreenCount(display); i++)
1474 for (j = 0; j < SWM_WS_MAX; j++)
1475 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1480 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1487 event.display = display; /* Ignored, but what the hell */
1488 event.window = win->id;
1489 event.root = win->s->root;
1490 event.subwindow = None;
1491 event.time = CurrentTime;
1496 event.same_screen = True;
1497 event.keycode = XKeysymToKeycode(display, keysym);
1498 event.state = modifiers;
1500 event.type = KeyPress;
1501 XSendEvent(event.display, event.window, True,
1502 KeyPressMask, (XEvent *)&event);
1504 event.type = KeyRelease;
1505 XSendEvent(event.display, event.window, True,
1506 KeyPressMask, (XEvent *)&event);
1511 restart(struct swm_region *r, union arg *args)
1513 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1515 /* disable alarm because the following code may not be interrupted */
1517 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1518 errx(1, "can't disable alarm");
1523 XCloseDisplay(display);
1524 execvp(start_argv[0], start_argv);
1525 fprintf(stderr, "execvp failed\n");
1531 root_to_region(Window root)
1533 struct swm_region *r = NULL;
1535 int i, x, y, wx, wy;
1538 for (i = 0; i < ScreenCount(display); i++)
1539 if (screens[i].root == root)
1542 if (XQueryPointer(display, screens[i].root,
1543 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1544 /* choose a region based on pointer location */
1545 TAILQ_FOREACH(r, &screens[i].rl, entry)
1546 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1547 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1552 r = TAILQ_FIRST(&screens[i].rl);
1558 find_unmanaged_window(Window id)
1563 for (i = 0; i < ScreenCount(display); i++)
1564 for (j = 0; j < SWM_WS_MAX; j++)
1565 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1573 find_window(Window id)
1576 Window wrr, wpr, *wcr = NULL;
1580 for (i = 0; i < ScreenCount(display); i++)
1581 for (j = 0; j < SWM_WS_MAX; j++)
1582 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1586 /* if we were looking for the parent return that window instead */
1587 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1592 /* ignore not found and root */
1593 if (wpr == 0 || wrr == wpr)
1596 /* look for parent */
1597 for (i = 0; i < ScreenCount(display); i++)
1598 for (j = 0; j < SWM_WS_MAX; j++)
1599 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1607 spawn(struct swm_region *r, union arg *args)
1612 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1616 close(ConnectionNumber(display));
1618 setenv("LD_PRELOAD", SWM_LIB, 1);
1620 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1624 setenv("_SWM_WS", ret, 1);
1628 if (asprintf(&ret, "%d", getpid()) == -1) {
1632 setenv("_SWM_PID", ret, 1);
1636 if (setsid() == -1) {
1642 * close stdin and stdout to prevent interaction between apps
1643 * and the baraction script
1644 * leave stderr open to record errors
1646 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1650 dup2(fd, STDIN_FILENO);
1651 dup2(fd, STDOUT_FILENO);
1655 execvp(args->argv[0], args->argv);
1663 spawnterm(struct swm_region *r, union arg *args)
1665 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1668 setenv("_SWM_XTERM_FONTADJ", "", 1);
1673 kill_refs(struct ws_win *win)
1676 struct swm_region *r;
1677 struct workspace *ws;
1682 for (i = 0; i < ScreenCount(display); i++)
1683 TAILQ_FOREACH(r, &screens[i].rl, entry)
1684 for (x = 0; x < SWM_WS_MAX; x++) {
1686 if (win == ws->focus)
1688 if (win == ws->focus_prev)
1689 ws->focus_prev = NULL;
1694 validate_win(struct ws_win *testwin)
1697 struct workspace *ws;
1698 struct swm_region *r;
1699 int i, x, foundit = 0;
1701 if (testwin == NULL)
1704 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1705 TAILQ_FOREACH(r, &screens[i].rl, entry)
1706 for (x = 0; x < SWM_WS_MAX; x++) {
1708 TAILQ_FOREACH(win, &ws->winlist, entry)
1716 validate_ws(struct workspace *testws)
1718 struct swm_region *r;
1719 struct workspace *ws;
1722 /* validate all ws */
1723 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1724 TAILQ_FOREACH(r, &screens[i].rl, entry)
1725 for (x = 0; x < SWM_WS_MAX; x++) {
1734 unfocus_win(struct ws_win *win)
1739 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1743 if (win->ws == NULL)
1746 if (validate_ws(win->ws))
1747 return; /* XXX this gets hit with thunderbird, needs fixing */
1749 if (win->ws->r == NULL)
1752 if (validate_win(win)) {
1757 if (win->ws->focus == win) {
1758 win->ws->focus = NULL;
1759 win->ws->focus_prev = win;
1762 if (validate_win(win->ws->focus)) {
1763 kill_refs(win->ws->focus);
1764 win->ws->focus = NULL;
1766 if (validate_win(win->ws->focus_prev)) {
1767 kill_refs(win->ws->focus_prev);
1768 win->ws->focus_prev = NULL;
1771 /* drain all previous unfocus events */
1772 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1775 grabbuttons(win, 0);
1776 XSetWindowBorder(display, win->id,
1777 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1779 XChangeProperty(display, win->s->root,
1780 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1781 PropModeReplace, (unsigned char *)&none,1);
1790 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1792 for (i = 0; i < ScreenCount(display); i++)
1793 for (j = 0; j < SWM_WS_MAX; j++)
1794 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1799 focus_win(struct ws_win *win)
1804 struct ws_win *cfw = NULL;
1807 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1811 if (win->ws == NULL)
1814 if (validate_ws(win->ws))
1815 return; /* XXX this gets hit with thunderbird, needs fixing */
1817 if (validate_win(win)) {
1822 if (validate_win(win)) {
1827 XGetInputFocus(display, &cur_focus, &rr);
1828 if ((cfw = find_window(cur_focus)) != NULL)
1831 win->ws->focus = win;
1833 if (win->ws->r != NULL) {
1834 /* drain all previous focus events */
1835 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1839 XSetInputFocus(display, win->id,
1840 RevertToParent, CurrentTime);
1841 grabbuttons(win, 1);
1842 XSetWindowBorder(display, win->id,
1843 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1844 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1845 XMapRaised(display, win->id);
1847 XChangeProperty(display, win->s->root,
1848 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1849 PropModeReplace, (unsigned char *)&win->id,1);
1852 if (window_name_enabled)
1857 switchws(struct swm_region *r, union arg *args)
1859 int wsid = args->id, unmap_old = 0;
1860 struct swm_region *this_r, *other_r;
1862 struct workspace *new_ws, *old_ws;
1869 old_ws = this_r->ws;
1870 new_ws = &this_r->s->ws[wsid];
1872 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1873 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1876 if (new_ws == NULL || old_ws == NULL)
1878 if (new_ws == old_ws)
1881 other_r = new_ws->r;
1882 if (other_r == NULL) {
1883 /* the other workspace is hidden, hide this one */
1887 /* the other ws is visible in another region, exchange them */
1888 other_r->ws_prior = new_ws;
1889 other_r->ws = old_ws;
1890 old_ws->r = other_r;
1892 this_r->ws_prior = old_ws;
1893 this_r->ws = new_ws;
1897 a.id = SWM_ARG_ID_FOCUSCUR;
1898 focus(new_ws->r, &a);
1901 /* unmap old windows */
1903 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1908 cyclews(struct swm_region *r, union arg *args)
1911 struct swm_screen *s = r->s;
1913 DNPRINTF(SWM_D_WS, "cyclews id %d "
1914 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1915 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1920 case SWM_ARG_ID_CYCLEWS_UP:
1921 if (a.id < SWM_WS_MAX - 1)
1926 case SWM_ARG_ID_CYCLEWS_DOWN:
1930 a.id = SWM_WS_MAX - 1;
1936 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1938 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1942 } while (a.id != r->ws->idx);
1946 priorws(struct swm_region *r, union arg *args)
1950 DNPRINTF(SWM_D_WS, "priorws id %d "
1951 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1952 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1954 if (r->ws_prior == NULL)
1957 a.id = r->ws_prior->idx;
1962 cyclescr(struct swm_region *r, union arg *args)
1964 struct swm_region *rr = NULL;
1968 /* do nothing if we don't have more than one screen */
1969 if (!(ScreenCount(display) > 1 || outputs > 1))
1974 case SWM_ARG_ID_CYCLESC_UP:
1975 rr = TAILQ_NEXT(r, entry);
1977 rr = TAILQ_FIRST(&screens[i].rl);
1979 case SWM_ARG_ID_CYCLESC_DOWN:
1980 rr = TAILQ_PREV(r, swm_region_list, entry);
1982 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1990 /* move mouse to region */
1992 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1993 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1995 a.id = SWM_ARG_ID_FOCUSCUR;
1998 if (rr->ws->focus) {
1999 /* move to focus window */
2000 x = rr->ws->focus->g.x + 1;
2001 y = rr->ws->focus->g.y + 1;
2002 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2007 swapwin(struct swm_region *r, union arg *args)
2009 struct ws_win *target, *source;
2010 struct ws_win *cur_focus;
2011 struct ws_win_list *wl;
2014 DNPRINTF(SWM_D_WS, "swapwin id %d "
2015 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2016 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2018 cur_focus = r->ws->focus;
2019 if (cur_focus == NULL)
2023 wl = &source->ws->winlist;
2026 case SWM_ARG_ID_SWAPPREV:
2027 target = TAILQ_PREV(source, ws_win_list, entry);
2028 TAILQ_REMOVE(wl, cur_focus, entry);
2030 TAILQ_INSERT_TAIL(wl, source, entry);
2032 TAILQ_INSERT_BEFORE(target, source, entry);
2034 case SWM_ARG_ID_SWAPNEXT:
2035 target = TAILQ_NEXT(source, entry);
2036 TAILQ_REMOVE(wl, source, entry);
2038 TAILQ_INSERT_HEAD(wl, source, entry);
2040 TAILQ_INSERT_AFTER(wl, target, source, entry);
2042 case SWM_ARG_ID_SWAPMAIN:
2043 target = TAILQ_FIRST(wl);
2044 if (target == source) {
2045 if (source->ws->focus_prev != NULL &&
2046 source->ws->focus_prev != target)
2048 source = source->ws->focus_prev;
2052 if (target == NULL || source == NULL)
2054 source->ws->focus_prev = target;
2055 TAILQ_REMOVE(wl, target, entry);
2056 TAILQ_INSERT_BEFORE(source, target, entry);
2057 TAILQ_REMOVE(wl, source, entry);
2058 TAILQ_INSERT_HEAD(wl, source, entry);
2060 case SWM_ARG_ID_MOVELAST:
2061 TAILQ_REMOVE(wl, source, entry);
2062 TAILQ_INSERT_TAIL(wl, source, entry);
2065 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2073 focus_prev(struct ws_win *win)
2075 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2076 struct ws_win *cur_focus = NULL;
2077 struct ws_win_list *wl = NULL;
2078 struct workspace *ws = NULL;
2080 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2082 if (!(win && win->ws))
2087 cur_focus = ws->focus;
2088 winlostfocus = cur_focus;
2090 /* pickle, just focus on whatever */
2091 if (cur_focus == NULL) {
2092 /* use prev_focus if valid */
2093 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2094 find_window(WINID(ws->focus_prev)))
2095 winfocus = ws->focus_prev;
2096 if (winfocus == NULL)
2097 winfocus = TAILQ_FIRST(wl);
2101 /* if transient focus on parent */
2102 if (cur_focus->transient) {
2103 winfocus = find_window(cur_focus->transient);
2107 /* if in max_stack try harder */
2108 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
2109 if (cur_focus != ws->focus_prev)
2110 winfocus = ws->focus_prev;
2111 else if (cur_focus != ws->focus)
2112 winfocus = ws->focus;
2114 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2119 if (cur_focus == win)
2120 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2121 if (winfocus == NULL)
2122 winfocus = TAILQ_LAST(wl, ws_win_list);
2123 if (winfocus == NULL || winfocus == win)
2124 winfocus = TAILQ_NEXT(cur_focus, entry);
2126 if (winfocus == winlostfocus || winfocus == NULL)
2129 focus_magic(winfocus, SWM_F_GENERIC);
2133 focus(struct swm_region *r, union arg *args)
2135 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2136 struct ws_win *cur_focus = NULL;
2137 struct ws_win_list *wl = NULL;
2138 struct workspace *ws = NULL;
2143 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2145 /* treat FOCUS_CUR special */
2146 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2148 winfocus = r->ws->focus;
2149 else if (r->ws->focus_prev)
2150 winfocus = r->ws->focus_prev;
2152 winfocus = TAILQ_FIRST(&r->ws->winlist);
2154 focus_magic(winfocus, SWM_F_GENERIC);
2158 if ((cur_focus = r->ws->focus) == NULL)
2163 winlostfocus = cur_focus;
2166 case SWM_ARG_ID_FOCUSPREV:
2167 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
2168 if (winfocus == NULL)
2169 winfocus = TAILQ_LAST(wl, ws_win_list);
2172 case SWM_ARG_ID_FOCUSNEXT:
2173 winfocus = TAILQ_NEXT(cur_focus, entry);
2174 if (winfocus == NULL)
2175 winfocus = TAILQ_FIRST(wl);
2178 case SWM_ARG_ID_FOCUSMAIN:
2179 winfocus = TAILQ_FIRST(wl);
2180 if (winfocus == cur_focus)
2181 winfocus = cur_focus->ws->focus_prev;
2188 if (winfocus == winlostfocus || winfocus == NULL)
2191 focus_magic(winfocus, SWM_F_GENERIC);
2195 cycle_layout(struct swm_region *r, union arg *args)
2197 struct workspace *ws = r->ws;
2198 struct ws_win *winfocus;
2201 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2203 winfocus = ws->focus;
2206 if (ws->cur_layout->l_stack == NULL)
2207 ws->cur_layout = &layouts[0];
2210 a.id = SWM_ARG_ID_FOCUSCUR;
2216 stack_config(struct swm_region *r, union arg *args)
2218 struct workspace *ws = r->ws;
2220 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2223 if (ws->cur_layout->l_config != NULL)
2224 ws->cur_layout->l_config(ws, args->id);
2226 if (args->id != SWM_ARG_ID_STACKINIT);
2232 struct swm_geometry g;
2233 struct swm_region *r;
2236 DNPRINTF(SWM_D_STACK, "stack\n");
2238 for (i = 0; i < ScreenCount(display); i++) {
2240 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2241 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2242 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2244 /* start with screen geometry, adjust for bar */
2246 g.w -= 2 * border_width;
2247 g.h -= 2 * border_width;
2253 r->ws->cur_layout->l_stack(r->ws, &g);
2254 /* save r so we can track region changes */
2263 store_float_geom(struct ws_win *win, struct swm_region *r)
2265 /* retain window geom and region geom */
2266 win->g_float.x = win->g.x;
2267 win->g_float.y = win->g.y;
2268 win->g_float.w = win->g.w;
2269 win->g_float.h = win->g.h;
2270 win->rg_float.x = r->g.x;
2271 win->rg_float.y = r->g.y;
2272 win->rg_float.w = r->g.w;
2273 win->rg_float.h = r->g.h;
2274 win->g_floatvalid = 1;
2278 stack_floater(struct ws_win *win, struct swm_region *r)
2286 bzero(&wc, sizeof wc);
2287 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2290 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2291 * geom on ws switches or return from max mode
2294 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2295 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2297 * use stored g and rg to set relative position and size
2298 * as in old region or before max stack mode
2300 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2301 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2302 win->g.w = win->g_float.w;
2303 win->g.h = win->g_float.h;
2304 win->g_floatvalid = 0;
2307 win->floatmaxed = 0;
2309 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2310 (win->g.h >= HEIGHT(r)))
2311 wc.border_width = 0;
2313 wc.border_width = border_width;
2314 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2315 win->g.w = (double)WIDTH(r) * dialog_ratio;
2316 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2321 * floaters and transients are auto-centred unless moved
2324 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - border_width;
2325 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - border_width;
2328 /* win can be outside r if new r smaller than old r */
2329 /* Ensure top left corner inside r (move probs otherwise) */
2330 if (win->g.x < r->g.x - border_width)
2331 win->g.x = r->g.x - border_width;
2332 if (win->g.x > r->g.x + r->g.w - 1)
2333 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2334 (r->g.x + r->g.w - win->g.w - 2 * border_width);
2335 if (win->g.y < r->g.y - border_width)
2336 win->g.y = r->g.y - border_width;
2337 if (win->g.y > r->g.y + r->g.h - 1)
2338 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2339 (r->g.y + r->g.h - win->g.h - 2 * border_width);
2343 wc.width = win->g.w;
2344 wc.height = win->g.h;
2347 * Retain floater and transient geometry for correct positioning
2348 * when ws changes region
2350 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2351 store_float_geom(win, r);
2353 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2354 win->id, wc.x, wc.y, wc.width, wc.height);
2356 XConfigureWindow(display, win->id, mask, &wc);
2361 * Send keystrokes to terminal to decrease/increase the font size as the
2362 * window size changes.
2365 adjust_font(struct ws_win *win)
2367 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2368 win->floating || win->transient)
2371 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2372 win->g.w / win->sh.width_inc < term_width &&
2373 win->font_steps < SWM_MAX_FONT_STEPS) {
2374 win->font_size_boundary[win->font_steps] =
2375 (win->sh.width_inc * term_width) + win->sh.base_width;
2378 win->last_inc = win->sh.width_inc;
2379 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2380 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2381 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2384 win->last_inc = win->sh.width_inc;
2385 fake_keypress(win, XK_KP_Add, ShiftMask);
2389 #define SWAPXY(g) do { \
2391 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2392 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2395 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2398 XWindowAttributes wa;
2399 struct swm_geometry win_g, r_g = *g;
2400 struct ws_win *win, *fs_win = 0;
2401 int i, j, s, stacks;
2402 int w_inc = 1, h_inc, w_base = 1, h_base;
2403 int hrh, extra = 0, h_slice, last_h = 0;
2404 int split, colno, winno, mwin, msize, mscale;
2405 int remain, missing, v_slice, reconfigure;
2408 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2409 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2411 winno = count_win(ws, 0);
2412 if (winno == 0 && count_win(ws, 1) == 0)
2415 TAILQ_FOREACH(win, &ws->winlist, entry)
2416 if (win->transient == 0 && win->floating == 0)
2423 w_inc = win->sh.width_inc;
2424 w_base = win->sh.base_width;
2425 mwin = ws->l_state.horizontal_mwin;
2426 mscale = ws->l_state.horizontal_msize;
2427 stacks = ws->l_state.horizontal_stacks;
2430 w_inc = win->sh.height_inc;
2431 w_base = win->sh.base_height;
2432 mwin = ws->l_state.vertical_mwin;
2433 mscale = ws->l_state.vertical_msize;
2434 stacks = ws->l_state.vertical_stacks;
2438 if (stacks > winno - mwin)
2439 stacks = winno - mwin;
2443 h_slice = r_g.h / SWM_H_SLICE;
2444 if (mwin && winno > mwin) {
2445 v_slice = r_g.w / SWM_V_SLICE;
2449 win_g.w = v_slice * mscale;
2451 if (w_inc > 1 && w_inc < v_slice) {
2452 /* adjust for window's requested size increment */
2453 remain = (win_g.w - w_base) % w_inc;
2454 missing = w_inc - remain;
2461 win_g.x += r_g.w - msize;
2464 colno = split = winno / stacks;
2465 win_g.w = ((r_g.w - (stacks * 2 * border_width) + 2 * border_width) / stacks);
2467 hrh = r_g.h / colno;
2468 extra = r_g.h - (colno * hrh);
2469 win_g.h = hrh - 2 * border_width;
2471 /* stack all the tiled windows */
2472 i = j = 0, s = stacks;
2473 TAILQ_FOREACH(win, &ws->winlist, entry) {
2474 if (win->transient != 0 || win->floating != 0)
2477 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2482 if (split && i == split) {
2483 colno = (winno - mwin) / stacks;
2484 if (s <= (winno - mwin) % stacks)
2486 split = split + colno;
2487 hrh = (r_g.h / colno);
2488 extra = r_g.h - (colno * hrh);
2492 win_g.x += win_g.w + 2 * border_width;
2493 win_g.w = (r_g.w - msize - (stacks * 2 * border_width)) / stacks;
2495 win_g.w += (r_g.w - msize - (stacks * 2 * border_width)) %
2500 win_g.h = hrh - 2 * border_width;
2502 h_inc = win->sh.width_inc;
2503 h_base = win->sh.base_width;
2505 h_inc = win->sh.height_inc;
2506 h_base = win->sh.base_height;
2508 if (j == colno - 1) {
2509 win_g.h = hrh + extra;
2510 } else if (h_inc > 1 && h_inc < h_slice) {
2511 /* adjust for window's requested size increment */
2512 remain = (win_g.h - h_base) % h_inc;
2513 missing = h_inc - remain;
2515 if (missing <= extra || j == 0) {
2527 win_g.y += last_h + 2 * border_width;
2529 bzero(&wc, sizeof wc);
2530 if (disable_border && bar_enabled == 0 && winno == 1){
2531 wc.border_width = 0;
2532 win_g.w += 2 * border_width;
2533 win_g.h += 2 * border_width;
2535 wc.border_width = border_width;
2538 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2539 win->g.w != win_g.h || win->g.h != win_g.w) {
2541 win->g.x = wc.x = win_g.y;
2542 win->g.y = wc.y = win_g.x;
2543 win->g.w = wc.width = win_g.h;
2544 win->g.h = wc.height = win_g.w;
2547 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2548 win->g.w != win_g.w || win->g.h != win_g.h) {
2550 win->g.x = wc.x = win_g.x;
2551 win->g.y = wc.y = win_g.y;
2552 win->g.w = wc.width = win_g.w;
2553 win->g.h = wc.height = win_g.h;
2558 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2559 XConfigureWindow(display, win->id, mask, &wc);
2563 if (XGetWindowAttributes(display, win->id, &wa))
2564 if (wa.map_state == IsUnmapped)
2565 XMapRaised(display, win->id);
2573 /* now, stack all the floaters and transients */
2574 TAILQ_FOREACH(win, &ws->winlist, entry) {
2575 if (win->transient == 0 && win->floating == 0)
2578 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2583 stack_floater(win, ws->r);
2584 XMapRaised(display, win->id);
2588 stack_floater(fs_win, ws->r);
2589 XMapRaised(display, fs_win->id);
2594 vertical_config(struct workspace *ws, int id)
2596 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2599 case SWM_ARG_ID_STACKRESET:
2600 case SWM_ARG_ID_STACKINIT:
2601 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2602 ws->l_state.vertical_mwin = 1;
2603 ws->l_state.vertical_stacks = 1;
2605 case SWM_ARG_ID_MASTERSHRINK:
2606 if (ws->l_state.vertical_msize > 1)
2607 ws->l_state.vertical_msize--;
2609 case SWM_ARG_ID_MASTERGROW:
2610 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2611 ws->l_state.vertical_msize++;
2613 case SWM_ARG_ID_MASTERADD:
2614 ws->l_state.vertical_mwin++;
2616 case SWM_ARG_ID_MASTERDEL:
2617 if (ws->l_state.vertical_mwin > 0)
2618 ws->l_state.vertical_mwin--;
2620 case SWM_ARG_ID_STACKINC:
2621 ws->l_state.vertical_stacks++;
2623 case SWM_ARG_ID_STACKDEC:
2624 if (ws->l_state.vertical_stacks > 1)
2625 ws->l_state.vertical_stacks--;
2633 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2635 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2637 stack_master(ws, g, 0, 0);
2641 horizontal_config(struct workspace *ws, int id)
2643 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2646 case SWM_ARG_ID_STACKRESET:
2647 case SWM_ARG_ID_STACKINIT:
2648 ws->l_state.horizontal_mwin = 1;
2649 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2650 ws->l_state.horizontal_stacks = 1;
2652 case SWM_ARG_ID_MASTERSHRINK:
2653 if (ws->l_state.horizontal_msize > 1)
2654 ws->l_state.horizontal_msize--;
2656 case SWM_ARG_ID_MASTERGROW:
2657 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2658 ws->l_state.horizontal_msize++;
2660 case SWM_ARG_ID_MASTERADD:
2661 ws->l_state.horizontal_mwin++;
2663 case SWM_ARG_ID_MASTERDEL:
2664 if (ws->l_state.horizontal_mwin > 0)
2665 ws->l_state.horizontal_mwin--;
2667 case SWM_ARG_ID_STACKINC:
2668 ws->l_state.horizontal_stacks++;
2670 case SWM_ARG_ID_STACKDEC:
2671 if (ws->l_state.horizontal_stacks > 1)
2672 ws->l_state.horizontal_stacks--;
2680 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2682 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2684 stack_master(ws, g, 1, 0);
2687 /* fullscreen view */
2689 max_stack(struct workspace *ws, struct swm_geometry *g)
2692 struct swm_geometry gg = *g;
2693 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2697 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2702 winno = count_win(ws, 0);
2703 if (winno == 0 && count_win(ws, 1) == 0)
2706 TAILQ_FOREACH(win, &ws->winlist, entry) {
2707 if (win->transient) {
2709 parent = find_window(win->transient);
2713 if (win->floating && win->floatmaxed == 0 ) {
2715 * retain geometry for retrieval on exit from
2718 store_float_geom(win, ws->r);
2719 win->floatmaxed = 1;
2722 /* only reconfigure if necessary */
2723 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2725 bzero(&wc, sizeof wc);
2726 win->g.x = wc.x = gg.x;
2727 win->g.y = wc.y = gg.y;
2729 wc.border_width = border_width;
2730 win->g.w = wc.width = gg.w;
2731 win->g.h = wc.height = gg.h;
2733 wc.border_width = 0;
2734 win->g.w = wc.width = gg.w + 2 * border_width;
2735 win->g.h = wc.height = gg.h + 2 * border_width;
2737 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2738 XConfigureWindow(display, win->id, mask, &wc);
2741 /* unmap only if we don't have multi screen */
2742 if (win != ws->focus)
2743 if (!(ScreenCount(display) > 1 || outputs > 1))
2747 /* put the last transient on top */
2750 XMapRaised(display, parent->id);
2751 stack_floater(wintrans, ws->r);
2752 focus_magic(wintrans, SWM_F_TRANSIENT);
2757 send_to_ws(struct swm_region *r, union arg *args)
2759 int wsid = args->id;
2760 struct ws_win *win = win;
2761 struct workspace *ws, *nws;
2762 Atom ws_idx_atom = 0;
2763 unsigned char ws_idx_str[SWM_PROPLEN];
2772 if (win->ws->idx == wsid)
2775 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2778 nws = &win->s->ws[wsid];
2780 a.id = SWM_ARG_ID_FOCUSPREV;
2783 TAILQ_REMOVE(&ws->winlist, win, entry);
2784 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2787 /* Try to update the window's workspace property */
2788 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2790 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2791 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2793 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2794 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2801 wkill(struct swm_region *r, union arg *args)
2803 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2805 if (r->ws->focus == NULL)
2808 if (args->id == SWM_ARG_ID_KILLWINDOW)
2809 XKillClient(display, r->ws->focus->id);
2811 if (r->ws->focus->can_delete)
2812 client_msg(r->ws->focus, adelete);
2817 floating_toggle_win(struct ws_win *win)
2819 struct swm_region *r;
2829 /* reject floating toggles in max stack mode */
2830 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
2833 if (win->floating) {
2834 if (!win->floatmaxed) {
2835 /* retain position for refloat */
2836 store_float_geom(win, r);
2840 if (win->g_floatvalid) {
2841 /* refloat at last floating relative position */
2842 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2843 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2844 win->g.w = win->g_float.w;
2845 win->g.h = win->g_float.h;
2850 ewmh_update_actions(win);
2856 floating_toggle(struct swm_region *r, union arg *args)
2858 struct ws_win *win = r->ws->focus;
2864 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
2865 _NET_WM_STATE_TOGGLE);
2869 if (win == win->ws->focus) {
2870 a.id = SWM_ARG_ID_FOCUSCUR;
2871 focus(win->ws->r, &a);
2876 resize_window(struct ws_win *win, int center)
2880 struct swm_region *r;
2882 r = root_to_region(win->wa.root);
2883 bzero(&wc, sizeof wc);
2884 mask = CWBorderWidth | CWWidth | CWHeight;
2885 wc.border_width = border_width;
2886 wc.width = win->g.w;
2887 wc.height = win->g.h;
2888 if (center == SWM_ARG_ID_CENTER) {
2889 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
2890 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
2894 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2895 win->id, wc.x, wc.y, wc.width, wc.height);
2897 XConfigureWindow(display, win->id, mask, &wc);
2902 resize(struct ws_win *win, union arg *args)
2906 struct swm_region *r = win->ws->r;
2911 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2912 win->id, win->floating, win->transient);
2914 if (!(win->transient != 0 || win->floating != 0))
2917 /* reject resizes in max mode for floaters (transient ok) */
2918 if (win->floatmaxed)
2922 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
2924 /* raise the window = move to last in window list */
2925 a.id = SWM_ARG_ID_MOVELAST;
2929 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2930 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2933 /* place pointer at bottom left corner or nearest point inside r */
2934 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2935 relx = win->g.w - 1;
2937 relx = r->g.x + r->g.w - win->g.x - 1;
2939 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2940 rely = win->g.h - 1;
2942 rely = r->g.y + r->g.h - win->g.y - 1;
2944 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2946 XMaskEvent(display, MOUSEMASK | ExposureMask |
2947 SubstructureRedirectMask, &ev);
2949 case ConfigureRequest:
2952 handler[ev.type](&ev);
2955 /* do not allow resize outside of region */
2956 if ( ev.xmotion.x_root < r->g.x ||
2957 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2958 ev.xmotion.y_root < r->g.y ||
2959 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2962 if (ev.xmotion.x <= 1)
2964 if (ev.xmotion.y <= 1)
2966 win->g.w = ev.xmotion.x + 1;
2967 win->g.h = ev.xmotion.y + 1;
2969 /* not free, don't sync more than 120 times / second */
2970 if ((ev.xmotion.time - time) > (1000 / 120) ) {
2971 time = ev.xmotion.time;
2972 XSync(display, False);
2973 resize_window(win, args->id);
2977 } while (ev.type != ButtonRelease);
2979 XSync(display, False);
2980 resize_window(win, args->id);
2982 store_float_geom(win,r);
2984 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2986 XUngrabPointer(display, CurrentTime);
2989 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2993 move_window(struct ws_win *win)
2997 struct swm_region *r;
2999 r = root_to_region(win->wa.root);
3000 bzero(&wc, sizeof wc);
3005 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3006 win->id, wc.x, wc.y, wc.width, wc.height);
3008 XConfigureWindow(display, win->id, mask, &wc);
3013 move(struct ws_win *win, union arg *args)
3017 struct swm_region *r = win->ws->r;
3020 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3021 win->id, win->floating, win->transient);
3023 /* in max_stack mode should only move transients */
3024 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3028 if (win->floating == 0 && !win->transient) {
3030 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3033 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3036 /* raise the window = move to last in window list */
3037 a.id = SWM_ARG_ID_MOVELAST;
3041 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3042 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3044 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3046 XMaskEvent(display, MOUSEMASK | ExposureMask |
3047 SubstructureRedirectMask, &ev);
3049 case ConfigureRequest:
3052 handler[ev.type](&ev);
3055 /* don't allow to move window origin out of region */
3056 if ( ev.xmotion.x_root < r->g.x ||
3057 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3058 ev.xmotion.y_root < r->g.y ||
3059 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3062 win->g.x = ev.xmotion.x_root - border_width;
3063 win->g.y = ev.xmotion.y_root - border_width;
3065 /* not free, don't sync more than 120 times / second */
3066 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3067 time = ev.xmotion.time;
3068 XSync(display, False);
3073 } while (ev.type != ButtonRelease);
3075 XSync(display, False);
3078 store_float_geom(win,r);
3079 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3080 XUngrabPointer(display, CurrentTime);
3083 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
3086 /* user/key callable function IDs */
3145 /* key definitions */
3147 dummykeyfunc(struct swm_region *r, union arg *args)
3152 legacyfunc(struct swm_region *r, union arg *args)
3157 char name[SWM_FUNCNAME_LEN];
3158 void (*func)(struct swm_region *r, union arg *);
3160 } keyfuncs[kf_invalid + 1] = {
3161 /* name function argument */
3162 { "cycle_layout", cycle_layout, {0} },
3163 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3164 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3165 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3166 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3167 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3168 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3169 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3170 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3171 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3172 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3173 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3174 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3175 { "spawn_term", spawnterm, {.argv = spawn_term} },
3176 { "spawn_menu", legacyfunc, {0} },
3177 { "quit", quit, {0} },
3178 { "restart", restart, {0} },
3179 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3180 { "ws_1", switchws, {.id = 0} },
3181 { "ws_2", switchws, {.id = 1} },
3182 { "ws_3", switchws, {.id = 2} },
3183 { "ws_4", switchws, {.id = 3} },
3184 { "ws_5", switchws, {.id = 4} },
3185 { "ws_6", switchws, {.id = 5} },
3186 { "ws_7", switchws, {.id = 6} },
3187 { "ws_8", switchws, {.id = 7} },
3188 { "ws_9", switchws, {.id = 8} },
3189 { "ws_10", switchws, {.id = 9} },
3190 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3191 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3192 { "ws_prior", priorws, {0} },
3193 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3194 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3195 { "mvws_1", send_to_ws, {.id = 0} },
3196 { "mvws_2", send_to_ws, {.id = 1} },
3197 { "mvws_3", send_to_ws, {.id = 2} },
3198 { "mvws_4", send_to_ws, {.id = 3} },
3199 { "mvws_5", send_to_ws, {.id = 4} },
3200 { "mvws_6", send_to_ws, {.id = 5} },
3201 { "mvws_7", send_to_ws, {.id = 6} },
3202 { "mvws_8", send_to_ws, {.id = 7} },
3203 { "mvws_9", send_to_ws, {.id = 8} },
3204 { "mvws_10", send_to_ws, {.id = 9} },
3205 { "bar_toggle", bar_toggle, {0} },
3206 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3207 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3208 { "screenshot_all", legacyfunc, {0} },
3209 { "screenshot_wind", legacyfunc, {0} },
3210 { "float_toggle", floating_toggle,{0} },
3211 { "version", version, {0} },
3212 { "spawn_lock", legacyfunc, {0} },
3213 { "spawn_initscr", legacyfunc, {0} },
3214 { "spawn_custom", dummykeyfunc, {0} },
3215 { "dumpwins", dumpwins, {0} },
3216 { "invalid key func", NULL, {0} },
3221 enum keyfuncid funcid;
3224 int keys_size = 0, keys_length = 0;
3225 struct key *keys = NULL;
3228 enum { client_click, root_click };
3230 unsigned int action;
3232 unsigned int button;
3233 void (*func)(struct ws_win *, union arg *);
3236 /* action key mouse button func args */
3237 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3238 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3239 { client_click, MODKEY, Button1, move, {0} },
3243 update_modkey(unsigned int mod)
3248 for (i = 0; i < keys_length; i++)
3249 if (keys[i].mod & ShiftMask)
3250 keys[i].mod = mod | ShiftMask;
3254 for (i = 0; i < LENGTH(buttons); i++)
3255 if (buttons[i].mask & ShiftMask)
3256 buttons[i].mask = mod | ShiftMask;
3258 buttons[i].mask = mod;
3268 int spawns_size = 0, spawns_length = 0;
3269 struct spawn_prog *spawns = NULL;
3272 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3275 struct spawn_prog *prog = NULL;
3277 char *ap, **real_args;
3279 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
3282 for (i = 0; i < spawns_length; i++) {
3283 if (!strcasecmp(spawn_name, spawns[i].name))
3287 fprintf(stderr, "spawn_custom: program %s not found\n",
3292 /* make room for expanded args */
3293 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3294 err(1, "spawn_custom: calloc real_args");
3296 /* expand spawn_args into real_args */
3297 for (i = 0; i < prog->argc; i++) {
3299 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3300 if (!strcasecmp(ap, "$bar_border")) {
3302 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3304 err(1, "spawn_custom border color");
3305 } else if (!strcasecmp(ap, "$bar_color")) {
3307 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3309 err(1, "spawn_custom bar color");
3310 } else if (!strcasecmp(ap, "$bar_font")) {
3311 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3313 err(1, "spawn_custom bar fonts");
3314 } else if (!strcasecmp(ap, "$bar_font_color")) {
3316 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3318 err(1, "spawn_custom color font");
3319 } else if (!strcasecmp(ap, "$color_focus")) {
3321 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3323 err(1, "spawn_custom color focus");
3324 } else if (!strcasecmp(ap, "$color_unfocus")) {
3326 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3328 err(1, "spawn_custom color unfocus");
3330 /* no match --> copy as is */
3331 if ((real_args[i] = strdup(ap)) == NULL)
3332 err(1, "spawn_custom strdup(ap)");
3334 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3339 if ((swm_debug & SWM_D_SPAWN) != 0) {
3340 fprintf(stderr, "spawn_custom: result = ");
3341 for (i = 0; i < prog->argc; i++)
3342 fprintf(stderr, "\"%s\" ", real_args[i]);
3343 fprintf(stderr, "\n");
3349 for (i = 0; i < prog->argc; i++)
3355 setspawn(struct spawn_prog *prog)
3359 if (prog == NULL || prog->name == NULL)
3363 for (i = 0; i < spawns_length; i++) {
3364 if (!strcmp(spawns[i].name, prog->name)) {
3366 if (prog->argv == NULL) {
3368 DNPRINTF(SWM_D_SPAWN,
3369 "setspawn: delete #%d %s\n",
3371 free(spawns[i].name);
3372 for (j = 0; j < spawns[i].argc; j++)
3373 free(spawns[i].argv[j]);
3374 free(spawns[i].argv);
3375 j = spawns_length - 1;
3377 spawns[i] = spawns[j];
3382 DNPRINTF(SWM_D_SPAWN,
3383 "setspawn: replace #%d %s\n",
3385 free(spawns[i].name);
3386 for (j = 0; j < spawns[i].argc; j++)
3387 free(spawns[i].argv[j]);
3388 free(spawns[i].argv);
3391 /* found case handled */
3397 if (prog->argv == NULL) {
3399 "error: setspawn: cannot find program %s", prog->name);
3404 /* not found: add */
3405 if (spawns_size == 0 || spawns == NULL) {
3407 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3408 spawns = malloc((size_t)spawns_size *
3409 sizeof(struct spawn_prog));
3410 if (spawns == NULL) {
3411 fprintf(stderr, "setspawn: malloc failed\n");
3415 } else if (spawns_length == spawns_size) {
3417 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3418 spawns = realloc(spawns, (size_t)spawns_size *
3419 sizeof(struct spawn_prog));
3420 if (spawns == NULL) {
3421 fprintf(stderr, "setspawn: realloc failed\n");
3427 if (spawns_length < spawns_size) {
3428 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3429 spawns_length, prog->name);
3430 i = spawns_length++;
3433 fprintf(stderr, "spawns array problem?\n");
3434 if (spawns == NULL) {
3435 fprintf(stderr, "spawns array is NULL!\n");
3443 setconfspawn(char *selector, char *value, int flags)
3445 struct spawn_prog *prog;
3446 char *vp, *cp, *word;
3448 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3449 if ((prog = calloc(1, sizeof *prog)) == NULL)
3450 err(1, "setconfspawn: calloc prog");
3451 prog->name = strdup(selector);
3452 if (prog->name == NULL)
3453 err(1, "setconfspawn prog->name");
3454 if ((cp = vp = strdup(value)) == NULL)
3455 err(1, "setconfspawn: strdup(value) ");
3456 while ((word = strsep(&cp, " \t")) != NULL) {
3457 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3459 cp += (long)strspn(cp, " \t");
3460 if (strlen(word) > 0) {
3462 prog->argv = realloc(prog->argv,
3463 prog->argc * sizeof(char *));
3464 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3465 err(1, "setconfspawn: strdup");
3472 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3479 setconfspawn("term", "xterm", 0);
3480 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3481 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3482 setconfspawn("lock", "xlock", 0);
3483 setconfspawn("initscr", "initscreen.sh", 0);
3484 setconfspawn("menu", "dmenu_run"
3487 " -nf $bar_font_color"
3489 " -sf $bar_color", 0);
3493 #define SWM_MODNAME_SIZE 32
3494 #define SWM_KEY_WS "\n+ \t"
3496 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3500 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3501 if (mod == NULL || ks == NULL) {
3502 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3505 if (keystr == NULL || strlen(keystr) == 0) {
3506 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3512 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3513 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3515 cp += (long)strspn(cp, SWM_KEY_WS);
3516 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3518 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3520 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3522 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3524 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3526 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3528 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3529 *mod |= ControlMask;
3531 *ks = XStringToKeysym(name);
3532 XConvertCase(*ks, ks, &uks);
3533 if (ks == NoSymbol) {
3535 "parsekeys: invalid key %s\n",
3541 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3546 strdupsafe(char *str)
3551 return (strdup(str));
3555 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3558 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3559 keyfuncs[kfid].name, spawn_name);
3561 for (i = 0; i < keys_length; i++) {
3562 if (keys[i].mod == mod && keys[i].keysym == ks) {
3563 if (kfid == kf_invalid) {
3566 "setkeybinding: delete #%d %s\n",
3567 i, keyfuncs[keys[i].funcid].name);
3568 free(keys[i].spawn_name);
3569 j = keys_length - 1;
3573 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3576 /* found: replace */
3578 "setkeybinding: replace #%d %s %s\n",
3579 i, keyfuncs[keys[i].funcid].name,
3581 free(keys[i].spawn_name);
3583 keys[i].keysym = ks;
3584 keys[i].funcid = kfid;
3585 keys[i].spawn_name = strdupsafe(spawn_name);
3586 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3591 if (kfid == kf_invalid) {
3593 "error: setkeybinding: cannot find mod/key combination");
3594 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3597 /* not found: add */
3598 if (keys_size == 0 || keys == NULL) {
3600 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3601 keys = malloc((size_t)keys_size * sizeof(struct key));
3603 fprintf(stderr, "malloc failed\n");
3607 } else if (keys_length == keys_size) {
3609 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3610 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3612 fprintf(stderr, "realloc failed\n");
3617 if (keys_length < keys_size) {
3619 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3620 j, keyfuncs[kfid].name, spawn_name);
3622 keys[j].keysym = ks;
3623 keys[j].funcid = kfid;
3624 keys[j].spawn_name = strdupsafe(spawn_name);
3626 fprintf(stderr, "keys array problem?\n");
3628 fprintf(stderr, "keys array problem\n");
3632 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3636 setconfbinding(char *selector, char *value, int flags)
3638 enum keyfuncid kfid;
3642 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3643 if (selector == NULL) {
3644 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3645 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3647 setkeybinding(mod, ks, kfid, NULL);
3652 /* search by key function name */
3653 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3654 if (strncasecmp(selector, keyfuncs[kfid].name,
3655 SWM_FUNCNAME_LEN) == 0) {
3656 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3658 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3659 setkeybinding(mod, ks, kfid, NULL);
3665 /* search by custom spawn name */
3666 for (i = 0; i < spawns_length; i++) {
3667 if (strcasecmp(selector, spawns[i].name) == 0) {
3668 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3670 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3671 setkeybinding(mod, ks, kf_spawn_custom,
3678 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3685 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3686 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3687 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3688 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3689 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3690 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3691 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3692 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3693 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3694 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3695 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3696 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3697 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3698 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3699 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3700 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3701 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3702 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3703 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3704 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3705 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3706 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3707 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3708 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3709 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3710 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3711 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3712 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3713 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3714 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3715 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3716 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3717 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3718 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3719 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3720 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3721 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3722 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3723 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3724 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3725 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3726 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3727 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3728 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3729 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3730 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3731 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3732 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3733 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3734 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3735 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3736 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3737 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3738 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3740 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3745 updatenumlockmask(void)
3748 XModifierKeymap *modmap;
3750 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3752 modmap = XGetModifierMapping(display);
3753 for (i = 0; i < 8; i++)
3754 for (j = 0; j < modmap->max_keypermod; j++)
3755 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3756 == XKeysymToKeycode(display, XK_Num_Lock))
3757 numlockmask = (1 << i);
3759 XFreeModifiermap(modmap);
3765 unsigned int i, j, k;
3767 unsigned int modifiers[] =
3768 { 0, LockMask, numlockmask, numlockmask | LockMask };
3770 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3771 updatenumlockmask();
3773 for (k = 0; k < ScreenCount(display); k++) {
3774 if (TAILQ_EMPTY(&screens[k].rl))
3776 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3777 for (i = 0; i < keys_length; i++) {
3778 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3779 for (j = 0; j < LENGTH(modifiers); j++)
3780 XGrabKey(display, code,
3781 keys[i].mod | modifiers[j],
3782 screens[k].root, True,
3783 GrabModeAsync, GrabModeAsync);
3789 grabbuttons(struct ws_win *win, int focused)
3792 unsigned int modifiers[] =
3793 { 0, LockMask, numlockmask, numlockmask|LockMask };
3795 updatenumlockmask();
3796 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3798 for (i = 0; i < LENGTH(buttons); i++)
3799 if (buttons[i].action == client_click)
3800 for (j = 0; j < LENGTH(modifiers); j++)
3801 XGrabButton(display, buttons[i].button,
3802 buttons[i].mask | modifiers[j],
3803 win->id, False, BUTTONMASK,
3804 GrabModeAsync, GrabModeSync, None,
3807 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3808 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3811 const char *quirkname[] = {
3812 "NONE", /* config string for "no value" */
3820 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3821 #define SWM_Q_WS "\n|+ \t"
3823 parsequirks(char *qstr, unsigned long *quirk)
3833 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3835 cp += (long)strspn(cp, SWM_Q_WS);
3836 for (i = 0; i < LENGTH(quirkname); i++) {
3837 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3838 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3843 *quirk |= 1 << (i-1);
3847 if (i >= LENGTH(quirkname)) {
3848 DNPRINTF(SWM_D_QUIRK,
3849 "parsequirks: invalid quirk [%s]\n", name);
3857 setquirk(const char *class, const char *name, const int quirk)
3862 for (i = 0; i < quirks_length; i++) {
3863 if (!strcmp(quirks[i].class, class) &&
3864 !strcmp(quirks[i].name, name)) {
3867 DNPRINTF(SWM_D_QUIRK,
3868 "setquirk: delete #%d %s:%s\n",
3869 i, quirks[i].class, quirks[i].name);
3870 free(quirks[i].class);
3871 free(quirks[i].name);
3872 j = quirks_length - 1;
3874 quirks[i] = quirks[j];
3878 /* found: replace */
3879 DNPRINTF(SWM_D_QUIRK,
3880 "setquirk: replace #%d %s:%s\n",
3881 i, quirks[i].class, quirks[i].name);
3882 free(quirks[i].class);
3883 free(quirks[i].name);
3884 quirks[i].class = strdup(class);
3885 quirks[i].name = strdup(name);
3886 quirks[i].quirk = quirk;
3893 "error: setquirk: cannot find class/name combination");
3896 /* not found: add */
3897 if (quirks_size == 0 || quirks == NULL) {
3899 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3900 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3902 fprintf(stderr, "setquirk: malloc failed\n");
3906 } else if (quirks_length == quirks_size) {
3908 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3909 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3911 fprintf(stderr, "setquirk: realloc failed\n");
3916 if (quirks_length < quirks_size) {
3917 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3918 j = quirks_length++;
3919 quirks[j].class = strdup(class);
3920 quirks[j].name = strdup(name);
3921 quirks[j].quirk = quirk;
3923 fprintf(stderr, "quirks array problem?\n");
3925 fprintf(stderr, "quirks array problem!\n");
3932 setconfquirk(char *selector, char *value, int flags)
3934 char *cp, *class, *name;
3936 unsigned long quirks;
3937 if (selector == NULL)
3939 if ((cp = strchr(selector, ':')) == NULL)
3944 if ((retval = parsequirks(value, &quirks)) == 0)
3945 setquirk(class, name, quirks);
3952 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3953 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3954 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3955 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3956 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3957 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3958 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3959 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3960 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3961 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3962 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3963 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3966 /* conf file stuff */
3967 #define SWM_CONF_FILE "scrotwm.conf"
3969 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3970 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3971 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3972 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
3973 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
3974 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
3979 setconfvalue(char *selector, char *value, int flags)
3982 case SWM_S_BAR_DELAY:
3983 bar_delay = atoi(value);
3985 case SWM_S_BAR_ENABLED:
3986 bar_enabled = atoi(value);
3988 case SWM_S_BAR_AT_BOTTOM:
3989 bar_at_bottom = atoi(value);
3991 case SWM_S_STACK_ENABLED:
3992 stack_enabled = atoi(value);
3994 case SWM_S_CLOCK_ENABLED:
3995 clock_enabled = atoi(value);
3997 case SWM_S_CLOCK_FORMAT:
3998 #ifndef SWM_DENY_CLOCK_FORMAT
4000 if ((clock_format = strdup(value)) == NULL)
4001 err(1, "setconfvalue: clock_format");
4004 case SWM_S_CYCLE_EMPTY:
4005 cycle_empty = atoi(value);
4007 case SWM_S_CYCLE_VISIBLE:
4008 cycle_visible = atoi(value);
4010 case SWM_S_SS_ENABLED:
4011 ss_enabled = atoi(value);
4013 case SWM_S_TERM_WIDTH:
4014 term_width = atoi(value);
4016 case SWM_S_TITLE_CLASS_ENABLED:
4017 title_class_enabled = atoi(value);
4019 case SWM_S_WINDOW_NAME_ENABLED:
4020 window_name_enabled = atoi(value);
4022 case SWM_S_TITLE_NAME_ENABLED:
4023 title_name_enabled = atoi(value);
4025 case SWM_S_FOCUS_MODE:
4026 if (!strcmp(value, "default"))
4027 focus_mode = SWM_FOCUS_DEFAULT;
4028 else if (!strcmp(value, "follow_cursor"))
4029 focus_mode = SWM_FOCUS_FOLLOW;
4030 else if (!strcmp(value, "synergy"))
4031 focus_mode = SWM_FOCUS_SYNERGY;
4033 err(1, "focus_mode");
4035 case SWM_S_DISABLE_BORDER:
4036 disable_border = atoi(value);
4038 case SWM_S_BORDER_WIDTH:
4039 border_width = atoi(value);
4041 case SWM_S_BAR_FONT:
4043 if ((bar_fonts[0] = strdup(value)) == NULL)
4044 err(1, "setconfvalue: bar_font");
4046 case SWM_S_BAR_ACTION:
4048 if ((bar_argv[0] = strdup(value)) == NULL)
4049 err(1, "setconfvalue: bar_action");
4051 case SWM_S_SPAWN_TERM:
4052 free(spawn_term[0]);
4053 if ((spawn_term[0] = strdup(value)) == NULL)
4054 err(1, "setconfvalue: spawn_term");
4058 case SWM_S_DIALOG_RATIO:
4059 dialog_ratio = atof(value);
4060 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4070 setconfmodkey(char *selector, char *value, int flags)
4072 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4073 update_modkey(Mod1Mask);
4074 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4075 update_modkey(Mod2Mask);
4076 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4077 update_modkey(Mod3Mask);
4078 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4079 update_modkey(Mod4Mask);
4086 setconfcolor(char *selector, char *value, int flags)
4088 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4093 setconfregion(char *selector, char *value, int flags)
4095 custom_region(value);
4099 /* config options */
4100 struct config_option {
4102 int (*func)(char*, char*, int);
4105 struct config_option configopt[] = {
4106 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4107 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4108 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4109 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4110 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4111 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4112 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4113 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4114 { "bind", setconfbinding, 0 },
4115 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4116 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4117 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4118 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4119 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4120 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4121 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4122 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4123 { "modkey", setconfmodkey, 0 },
4124 { "program", setconfspawn, 0 },
4125 { "quirk", setconfquirk, 0 },
4126 { "region", setconfregion, 0 },
4127 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4128 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4129 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4130 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4131 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4132 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4133 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4134 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4135 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4136 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4141 conf_load(char *filename)
4144 char *line, *cp, *optsub, *optval;
4145 size_t linelen, lineno = 0;
4146 int wordlen, i, optind;
4147 struct config_option *opt;
4149 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4151 if (filename == NULL) {
4152 fprintf(stderr, "conf_load: no filename\n");
4155 if ((config = fopen(filename, "r")) == NULL) {
4156 warn("conf_load: fopen");
4160 while (!feof(config)) {
4161 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4164 err(1, "%s", filename);
4169 cp += strspn(cp, " \t\n"); /* eat whitespace */
4170 if (cp[0] == '\0') {
4175 /* get config option */
4176 wordlen = strcspn(cp, "=[ \t\n");
4178 warnx("%s: line %zd: no option found",
4183 for (i = 0; i < LENGTH(configopt); i++) {
4184 opt = &configopt[i];
4185 if (!strncasecmp(cp, opt->optname, wordlen) &&
4186 strlen(opt->optname) == wordlen) {
4192 warnx("%s: line %zd: unknown option %.*s",
4193 filename, lineno, wordlen, cp);
4197 cp += strspn(cp, " \t\n"); /* eat whitespace */
4198 /* get [selector] if any */
4202 wordlen = strcspn(cp, "]");
4205 warnx("%s: line %zd: syntax error",
4209 asprintf(&optsub, "%.*s", wordlen, cp);
4212 cp += strspn(cp, "] \t\n"); /* eat trailing */
4214 cp += strspn(cp, "= \t\n"); /* eat trailing */
4216 optval = strdup(cp);
4217 /* call function to deal with it all */
4218 if (configopt[optind].func(optsub, optval,
4219 configopt[optind].funcflags) != 0) {
4220 fprintf(stderr, "%s line %zd: %s\n",
4221 filename, lineno, line);
4222 errx(1, "%s: line %zd: invalid data for %s",
4223 filename, lineno, configopt[optind].optname);
4231 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4237 set_child_transient(struct ws_win *win)
4239 struct ws_win *parent;
4241 parent = find_window(win->transient);
4243 parent->child_trans = win;
4247 manage_window(Window id)
4250 struct workspace *ws;
4251 struct ws_win *win, *ww;
4252 int format, i, ws_idx, n, border_me = 0;
4253 unsigned long nitems, bytes;
4254 Atom ws_idx_atom = 0, type;
4255 Atom *prot = NULL, *pp;
4256 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4257 struct swm_region *r;
4262 if ((win = find_window(id)) != NULL)
4263 return (win); /* already being managed */
4265 /* see if we are on the unmanaged list */
4266 if ((win = find_unmanaged_window(id)) != NULL) {
4267 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4269 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4270 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4272 set_child_transient(win);
4273 ewmh_update_actions(win);
4277 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4278 errx(1, "calloc: failed to allocate memory for new window");
4280 /* Get all the window data in one shot */
4281 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4283 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4284 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4285 XGetWindowAttributes(display, id, &win->wa);
4286 XGetWMNormalHints(display, id, &win->sh, &mask);
4287 XGetTransientForHint(display, id, &trans);
4289 win->transient = trans;
4290 set_child_transient(win);
4291 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4292 win->id, win->transient);
4295 /* get supported protocols */
4296 if (XGetWMProtocols(display, id, &prot, &n)) {
4297 for (i = 0, pp = prot; i < n; i++, pp++) {
4298 if (*pp == takefocus)
4299 win->take_focus = 1;
4301 win->can_delete = 1;
4308 * Figure out where to put the window. If it was previously assigned to
4309 * a workspace (either by spawn() or manually moving), and isn't
4310 * transient, * put it in the same workspace
4312 r = root_to_region(win->wa.root);
4313 if (prop && win->transient == 0) {
4314 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4315 ws_idx = strtonum(prop, 0, 9, &errstr);
4317 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4320 ws = &r->s->ws[ws_idx];
4323 /* this should launch transients in the same ws as parent */
4325 if ((ww = find_window(trans)) != NULL)
4332 "fix this bug mcbride\n");
4337 /* set up the window layout */
4340 win->s = r->s; /* this never changes */
4341 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4343 win->g.w = win->wa.width;
4344 win->g.h = win->wa.height;
4345 win->g.x = win->wa.x;
4346 win->g.y = win->wa.y;
4347 win->g_floatvalid = 0;
4348 win->floatmaxed = 0;
4349 win->ewmh_flags = 0;
4351 /* Set window properties so we can remember this after reincarnation */
4352 if (ws_idx_atom && prop == NULL &&
4353 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4354 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4356 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4357 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4362 ewmh_autoquirk(win);
4364 if (XGetClassHint(display, win->id, &win->ch)) {
4365 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4366 win->ch.res_class, win->ch.res_name);
4368 /* java is retarded so treat it special */
4369 if (strstr(win->ch.res_name, "sun-awt")) {
4374 for (i = 0; i < quirks_length; i++){
4375 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4376 !strcmp(win->ch.res_name, quirks[i].name)) {
4377 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4378 win->ch.res_class, win->ch.res_name);
4379 if (quirks[i].quirk & SWM_Q_FLOAT) {
4383 win->quirks = quirks[i].quirk;
4388 /* alter window position if quirky */
4389 if (win->quirks & SWM_Q_ANYWHERE) {
4390 win->manual = 1; /* don't center the quirky windows */
4391 bzero(&wc, sizeof wc);
4393 if (bar_enabled && win->g.y < bar_height) {
4394 win->g.y = wc.y = bar_height;
4397 if (win->g.w + win->g.x > WIDTH(r)) {
4398 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4404 /* Reset font sizes (the bruteforce way; no default keybinding). */
4405 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4406 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4407 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4408 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4409 fake_keypress(win, XK_KP_Add, ShiftMask);
4412 ewmh_get_win_state(win);
4413 ewmh_update_actions(win);
4414 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4418 bzero(&wc, sizeof wc);
4419 wc.border_width = border_width;
4420 mask = CWBorderWidth;
4421 XConfigureWindow(display, win->id, mask, &wc);
4425 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4426 PropertyChangeMask | StructureNotifyMask);
4428 set_win_state(win, NormalState);
4430 /* floaters need to be mapped if they are in the current workspace */
4431 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4432 XMapRaised(display, win->id);
4438 free_window(struct ws_win *win)
4440 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4445 /* needed for restart wm */
4446 set_win_state(win, WithdrawnState);
4448 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4450 if (win->ch.res_class)
4451 XFree(win->ch.res_class);
4452 if (win->ch.res_name)
4453 XFree(win->ch.res_name);
4458 memset(win, 0xff, sizeof *win); /* XXX kill later */
4464 unmanage_window(struct ws_win *win)
4466 struct ws_win *parent;
4471 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4473 if (win->transient) {
4474 parent = find_window(win->transient);
4476 parent->child_trans = NULL;
4479 /* focus on root just in case */
4480 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4485 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4486 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4492 focus_magic(struct ws_win *win, int do_trans)
4494 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4499 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4500 /* win = parent & has a transient so focus on that */
4502 focus_win(win->child_trans);
4503 if (win->child_trans->take_focus)
4504 client_msg(win, takefocus);
4506 /* make sure transient hasn't dissapeared */
4507 if (validate_win(win->child_trans) == 0) {
4508 focus_win(win->child_trans);
4509 if (win->child_trans->take_focus)
4510 client_msg(win->child_trans, takefocus);
4512 win->child_trans = NULL;
4514 if (win->take_focus)
4515 client_msg(win, takefocus);
4521 if (win->take_focus)
4522 client_msg(win, takefocus);
4529 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4537 XKeyEvent *ev = &e->xkey;
4539 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4541 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4542 for (i = 0; i < keys_length; i++)
4543 if (keysym == keys[i].keysym
4544 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4545 && keyfuncs[keys[i].funcid].func) {
4546 if (keys[i].funcid == kf_spawn_custom)
4548 root_to_region(ev->root),
4549 &(keyfuncs[keys[i].funcid].args),
4553 keyfuncs[keys[i].funcid].func(
4554 root_to_region(ev->root),
4555 &(keyfuncs[keys[i].funcid].args)
4561 buttonpress(XEvent *e)
4565 XButtonPressedEvent *ev = &e->xbutton;
4567 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4569 action = root_click;
4570 if ((win = find_window(ev->window)) == NULL)
4573 focus_magic(win, SWM_F_TRANSIENT);
4574 action = client_click;
4576 for (i = 0; i < LENGTH(buttons); i++)
4577 if (action == buttons[i].action && buttons[i].func &&
4578 buttons[i].button == ev->button &&
4579 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4580 buttons[i].func(win, &buttons[i].args);
4584 configurerequest(XEvent *e)
4586 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4591 if ((win = find_window(ev->window)) == NULL)
4592 if ((win = find_unmanaged_window(ev->window)) == NULL)
4596 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4598 bzero(&wc, sizeof wc);
4601 wc.width = ev->width;
4602 wc.height = ev->height;
4603 wc.border_width = ev->border_width;
4604 wc.sibling = ev->above;
4605 wc.stack_mode = ev->detail;
4606 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4608 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4610 if (win->floating) {
4611 if (ev->value_mask & CWX)
4613 if (ev->value_mask & CWY)
4615 if (ev->value_mask & CWWidth)
4616 win->g.w = ev->width;
4617 if (ev->value_mask & CWHeight)
4618 win->g.h = ev->height;
4625 configurenotify(XEvent *e)
4630 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4631 e->xconfigure.window);
4633 win = find_window(e->xconfigure.window);
4635 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4643 destroynotify(XEvent *e)
4646 XDestroyWindowEvent *ev = &e->xdestroywindow;
4648 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4650 if ((win = find_window(ev->window)) == NULL) {
4651 if ((win = find_unmanaged_window(ev->window)) == NULL)
4657 /* make sure we focus on something */
4660 unmanage_window(win);
4666 enternotify(XEvent *e)
4668 XCrossingEvent *ev = &e->xcrossing;
4673 Window focus_return;
4674 int revert_to_return;
4676 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4677 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4678 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4679 ev->same_screen, ev->focus, ev->state);
4681 switch (focus_mode) {
4682 case SWM_FOCUS_DEFAULT:
4683 if (QLength(display)) {
4684 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4689 case SWM_FOCUS_FOLLOW:
4691 case SWM_FOCUS_SYNERGY:
4694 * all these checks need to be in this order because the
4695 * XCheckTypedWindowEvent relies on weeding out the previous events
4697 * making this code an option would enable a follow mouse for focus
4702 * state is set when we are switching workspaces and focus is set when
4703 * the window or a subwindow already has focus (occurs during restart).
4705 * Only honor the focus flag if last_focus_event is not FocusOut,
4706 * this allows scrotwm to continue to control focus when another
4707 * program is also playing with it.
4709 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4710 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4715 * happens when a window is created or destroyed and the border
4716 * crosses the mouse pointer and when switching ws
4718 * we need the subwindow test to see if we came from root in order
4719 * to give focus to floaters
4721 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4722 ev->subwindow == 0) {
4723 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4727 /* this window already has focus */
4728 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4729 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4733 /* this window is being deleted or moved to another ws */
4734 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4736 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4737 XPutBackEvent(display, &cne);
4741 if ((win = find_window(ev->window)) == NULL) {
4742 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4747 * In fullstack kill all enters unless they come from a different ws
4748 * (i.e. another region) or focus has been grabbed externally.
4750 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4751 last_focus_event != FocusOut) {
4752 XGetInputFocus(display, &focus_return, &revert_to_return);
4753 if ((w = find_window(focus_return)) == NULL ||
4755 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4763 if ((win = find_window(ev->window)) == NULL) {
4764 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4769 * if we have more enternotifies let them handle it in due time
4771 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4772 DNPRINTF(SWM_D_EVENT,
4773 "ignoring enternotify: got more enternotify\n");
4774 XPutBackEvent(display, &cne);
4778 focus_magic(win, SWM_F_TRANSIENT);
4781 /* lets us use one switch statement for arbitrary mode/detail combinations */
4782 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4785 focusevent(XEvent *e)
4789 u_int32_t mode_detail;
4790 XFocusChangeEvent *ev = &e->xfocus;
4792 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4793 ev->type == FocusIn ? "entering" : "leaving",
4794 ev->window, ev->mode, ev->detail);
4796 if (last_focus_event == ev->type) {
4797 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4801 last_focus_event = ev->type;
4802 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4804 switch (mode_detail) {
4805 /* synergy client focus operations */
4806 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4807 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4809 /* synergy server focus operations */
4810 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4812 /* Entering applications like rdesktop that mangle the pointer */
4813 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4815 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4816 XSetWindowBorder(display, win->id,
4817 win->ws->r->s->c[ev->type == FocusIn ?
4818 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4821 fprintf(stderr, "ignoring focusevent\n");
4822 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4829 mapnotify(XEvent *e)
4832 XMapEvent *ev = &e->xmap;
4834 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4836 win = manage_window(ev->window);
4838 set_win_state(win, NormalState);
4842 mappingnotify(XEvent *e)
4844 XMappingEvent *ev = &e->xmapping;
4846 XRefreshKeyboardMapping(ev);
4847 if (ev->request == MappingKeyboard)
4852 maprequest(XEvent *e)
4855 struct swm_region *r;
4856 XWindowAttributes wa;
4857 XMapRequestEvent *ev = &e->xmaprequest;
4859 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4860 e->xmaprequest.window);
4862 if (!XGetWindowAttributes(display, ev->window, &wa))
4864 if (wa.override_redirect)
4867 win = manage_window(e->xmaprequest.window);
4869 return; /* can't happen */
4873 /* make new win focused */
4874 r = root_to_region(win->wa.root);
4875 if (win->ws == r->ws)
4876 focus_magic(win, SWM_F_GENERIC);
4880 propertynotify(XEvent *e)
4883 XPropertyEvent *ev = &e->xproperty;
4885 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4888 if (ev->state == PropertyDelete)
4889 return; /* ignore */
4890 win = find_window(ev->window);
4895 case XA_WM_NORMAL_HINTS:
4898 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4899 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4900 if (win->sh.flags & PMinSize) {
4901 win->g.w = win->sh.min_width;
4902 win->g.h = win->sh.min_height;
4903 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4905 XMoveResizeWindow(display, win->id,
4906 win->g.x, win->g.y, win->g.w, win->g.h);
4908 if (window_name_enabled)
4917 unmapnotify(XEvent *e)
4921 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4923 /* determine if we need to help unmanage this window */
4924 win = find_window(e->xunmap.window);
4928 if (getstate(e->xunmap.window) == NormalState) {
4929 unmanage_window(win);
4935 visibilitynotify(XEvent *e)
4938 struct swm_region *r;
4940 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4941 e->xvisibility.window);
4942 if (e->xvisibility.state == VisibilityUnobscured)
4943 for (i = 0; i < ScreenCount(display); i++)
4944 TAILQ_FOREACH(r, &screens[i].rl, entry)
4945 if (e->xvisibility.window == r->bar_window)
4950 clientmessage(XEvent *e)
4952 XClientMessageEvent *ev;
4957 win = find_window(ev->window);
4961 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
4962 ev->window, ev->message_type);
4964 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
4965 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
4968 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
4969 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
4970 if (win->can_delete)
4971 client_msg(win, adelete);
4973 XKillClient(display, win->id);
4975 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
4976 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
4977 if (win->floating) {
4978 if (ev->data.l[0] & (1<<8)) /* x */
4979 win->g.x = ev->data.l[1];
4980 if (ev->data.l[0] & (1<<9)) /* y */
4981 win->g.y = ev->data.l[2];
4982 if (ev->data.l[0] & (1<<10)) /* width */
4983 win->g.w = ev->data.l[3];
4984 if (ev->data.l[0] & (1<<11)) /* height */
4985 win->g.h = ev->data.l[4];
4988 /* TODO: Change stack sizes */
4992 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
4993 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
4994 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
4996 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
5003 xerror_start(Display *d, XErrorEvent *ee)
5010 xerror(Display *d, XErrorEvent *ee)
5012 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5020 xerrorxlib = XSetErrorHandler(xerror_start);
5022 /* this causes an error if some other window manager is running */
5023 XSelectInput(display, DefaultRootWindow(display),
5024 SubstructureRedirectMask);
5025 XSync(display, False);
5029 XSetErrorHandler(xerror);
5030 XSync(display, False);
5035 new_region(struct swm_screen *s, int x, int y, int w, int h)
5037 struct swm_region *r, *n;
5038 struct workspace *ws = NULL;
5041 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5042 s->idx, w, h, x, y);
5044 /* remove any conflicting regions */
5045 n = TAILQ_FIRST(&s->rl);
5048 n = TAILQ_NEXT(r, entry);
5049 if (X(r) < (x + w) &&
5050 (X(r) + WIDTH(r)) > x &&
5052 (Y(r) + HEIGHT(r)) > y) {
5053 if (r->ws->r != NULL)
5054 r->ws->old_r = r->ws->r;
5056 XDestroyWindow(display, r->bar_window);
5057 TAILQ_REMOVE(&s->rl, r, entry);
5058 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5062 /* search old regions for one to reuse */
5064 /* size + location match */
5065 TAILQ_FOREACH(r, &s->orl, entry)
5066 if (X(r) == x && Y(r) == y &&
5067 HEIGHT(r) == h && WIDTH(r) == w)
5071 TAILQ_FOREACH(r, &s->orl, entry)
5072 if (HEIGHT(r) == h && WIDTH(r) == w)
5076 TAILQ_REMOVE(&s->orl, r, entry);
5077 /* try to use old region's workspace */
5078 if (r->ws->r == NULL)
5081 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5082 errx(1, "calloc: failed to allocate memory for screen");
5084 /* if we don't have a workspace already, find one */
5086 for (i = 0; i < SWM_WS_MAX; i++)
5087 if (s->ws[i].r == NULL) {
5094 errx(1, "no free workspaces\n");
5105 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5111 #ifdef SWM_XRR_HAS_CRTC
5113 XRRScreenResources *sr;
5116 #endif /* SWM_XRR_HAS_CRTC */
5117 struct swm_region *r;
5120 if (i >= ScreenCount(display))
5121 errx(1, "invalid screen");
5123 /* remove any old regions */
5124 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5125 r->ws->old_r = r->ws->r = NULL;
5126 XDestroyWindow(display, r->bar_window);
5127 TAILQ_REMOVE(&screens[i].rl, r, entry);
5128 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5132 /* map virtual screens onto physical screens */
5133 #ifdef SWM_XRR_HAS_CRTC
5134 if (xrandr_support) {
5135 sr = XRRGetScreenResources(display, screens[i].root);
5137 new_region(&screens[i], 0, 0,
5138 DisplayWidth(display, i),
5139 DisplayHeight(display, i));
5143 for (c = 0, ci = NULL; c < ncrtc; c++) {
5144 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5145 if (ci->noutput == 0)
5148 if (ci != NULL && ci->mode == None)
5149 new_region(&screens[i], 0, 0,
5150 DisplayWidth(display, i),
5151 DisplayHeight(display, i));
5153 new_region(&screens[i],
5154 ci->x, ci->y, ci->width, ci->height);
5157 XRRFreeCrtcInfo(ci);
5158 XRRFreeScreenResources(sr);
5160 #endif /* SWM_XRR_HAS_CRTC */
5162 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5163 DisplayHeight(display, i));
5168 screenchange(XEvent *e) {
5169 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5170 struct swm_region *r;
5173 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5175 if (!XRRUpdateConfiguration(e))
5178 /* silly event doesn't include the screen index */
5179 for (i = 0; i < ScreenCount(display); i++)
5180 if (screens[i].root == xe->root)
5182 if (i >= ScreenCount(display))
5183 errx(1, "screenchange: screen not found\n");
5185 /* brute force for now, just re-enumerate the regions */
5188 /* add bars to all regions */
5189 for (i = 0; i < ScreenCount(display); i++)
5190 TAILQ_FOREACH(r, &screens[i].rl, entry)
5198 Window d1, d2, *wins = NULL;
5199 XWindowAttributes wa;
5204 for (i = 0; i < ScreenCount(display); i++) {
5205 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5208 /* attach windows to a region */
5209 /* normal windows */
5210 for (j = 0; j < no; j++) {
5211 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5212 wa.override_redirect ||
5213 XGetTransientForHint(display, wins[j], &d1))
5216 state = getstate(wins[j]);
5217 manage = state == IconicState;
5218 if (wa.map_state == IsViewable || manage)
5219 manage_window(wins[j]);
5221 /* transient windows */
5222 for (j = 0; j < no; j++) {
5223 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5224 wa.override_redirect)
5227 state = getstate(wins[j]);
5228 manage = state == IconicState;
5229 if (XGetTransientForHint(display, wins[j], &d1) &&
5231 manage_window(wins[j]);
5244 int errorbase, major, minor;
5245 struct workspace *ws;
5248 if ((screens = calloc(ScreenCount(display),
5249 sizeof(struct swm_screen))) == NULL)
5250 errx(1, "calloc: screens");
5252 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5254 /* initial Xrandr setup */
5255 xrandr_support = XRRQueryExtension(display,
5256 &xrandr_eventbase, &errorbase);
5258 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5261 /* map physical screens */
5262 for (i = 0; i < ScreenCount(display); i++) {
5263 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5265 TAILQ_INIT(&screens[i].rl);
5266 TAILQ_INIT(&screens[i].orl);
5267 screens[i].root = RootWindow(display, i);
5269 /* set default colors */
5270 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5271 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5272 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5273 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5274 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5276 /* set default cursor */
5277 XDefineCursor(display, screens[i].root,
5278 XCreateFontCursor(display, XC_left_ptr));
5280 /* init all workspaces */
5281 /* XXX these should be dynamically allocated too */
5282 for (j = 0; j < SWM_WS_MAX; j++) {
5283 ws = &screens[i].ws[j];
5288 TAILQ_INIT(&ws->winlist);
5289 TAILQ_INIT(&ws->unmanagedlist);
5291 for (k = 0; layouts[k].l_stack != NULL; k++)
5292 if (layouts[k].l_config != NULL)
5293 layouts[k].l_config(ws,
5294 SWM_ARG_ID_STACKINIT);
5295 ws->cur_layout = &layouts[0];
5301 XRRSelectInput(display, screens[i].root,
5302 RRScreenChangeNotifyMask);
5309 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5311 err(1, "setup_globals: strdup");
5312 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5314 err(1, "setup_globals: strdup");
5315 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5317 err(1, "setup_globals: strdup");
5318 if ((spawn_term[0] = strdup("xterm")) == NULL)
5319 err(1, "setup_globals: strdup");
5320 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5328 Atom netwmcheck, netwmname, utf8_string;
5331 /* work around sun jdk bugs, code from wmname */
5332 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5333 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5334 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5335 for (i = 0; i < ScreenCount(display); i++) {
5336 root = screens[i].root;
5337 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5338 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5339 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5341 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5342 PropModeReplace, (unsigned char *)&win,1);
5343 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5344 PropModeReplace, (unsigned char *)&win,1);
5345 XChangeProperty(display, win, netwmname, utf8_string, 8,
5346 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5351 main(int argc, char *argv[])
5354 struct swm_region *r, *rr;
5355 struct ws_win *winfocus = NULL;
5358 char conf[PATH_MAX], *cfile = NULL;
5363 struct sigaction sact;
5366 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5367 SWM_VERSION, cvstag);
5368 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5369 warnx("no locale support");
5371 if (!(display = XOpenDisplay(0)))
5372 errx(1, "can not open display");
5375 errx(1, "other wm running");
5377 /* handle some signals */
5378 bzero(&sact, sizeof(sact));
5379 sigemptyset(&sact.sa_mask);
5381 sact.sa_handler = sighdlr;
5382 sigaction(SIGINT, &sact, NULL);
5383 sigaction(SIGQUIT, &sact, NULL);
5384 sigaction(SIGTERM, &sact, NULL);
5385 sigaction(SIGHUP, &sact, NULL);
5387 sact.sa_handler = sighdlr;
5388 sact.sa_flags = SA_NOCLDSTOP;
5389 sigaction(SIGCHLD, &sact, NULL);
5391 astate = XInternAtom(display, "WM_STATE", False);
5392 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5393 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5394 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5396 /* look for local and global conf file */
5397 pwd = getpwuid(getuid());
5399 errx(1, "invalid user %d", getuid());
5408 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5409 if (stat(conf, &sb) != -1) {
5410 if (S_ISREG(sb.st_mode))
5413 /* try global conf file */
5414 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5415 if (!stat(conf, &sb))
5416 if (S_ISREG(sb.st_mode))
5423 /* set some values to work around bad programs */
5426 /* grab existing windows (before we build the bars) */
5429 /* setup all bars */
5430 for (i = 0; i < ScreenCount(display); i++)
5431 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5432 if (winfocus == NULL)
5433 winfocus = TAILQ_FIRST(&r->ws->winlist);
5442 xfd = ConnectionNumber(display);
5444 while (XPending(display)) {
5445 XNextEvent(display, &e);
5448 if (e.type < LASTEvent) {
5450 if (handler[e.type])
5451 handler[e.type](&e);
5453 DNPRINTF(SWM_D_EVENT,
5454 "win: %lu unknown event: %d\n",
5455 e.xany.window, e.type);
5457 switch (e.type - xrandr_eventbase) {
5458 case RRScreenChangeNotify:
5462 DNPRINTF(SWM_D_EVENT,
5463 "win: %lu unknown xrandr event: "
5464 "%d\n", e.xany.window, e.type);
5470 /* if we are being restarted go focus on first window */
5472 rr = winfocus->ws->r;
5474 /* not a visible window */
5478 /* move pointer to first screen if multi screen */
5479 if (ScreenCount(display) > 1 || outputs > 1)
5480 XWarpPointer(display, None, rr->s[0].root,
5481 0, 0, 0, 0, rr->g.x,
5482 rr->g.y + (bar_enabled ? bar_height : 0));
5484 a.id = SWM_ARG_ID_FOCUSCUR;
5494 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5496 DNPRINTF(SWM_D_MISC, "select failed");
5497 if (restart_wm == 1)
5498 restart(NULL, NULL);
5509 XFreeGC(display, bar_gc);
5510 XCloseDisplay(display);