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.27"
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 focus_mode = SWM_FOCUS_DEFAULT;
212 int disable_border = 0;
218 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
219 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
221 #define SWM_MENU_FN (2)
222 #define SWM_MENU_NB (4)
223 #define SWM_MENU_NF (6)
224 #define SWM_MENU_SB (8)
225 #define SWM_MENU_SF (10)
227 /* layout manager data */
228 struct swm_geometry {
238 /* virtual "screens" */
240 TAILQ_ENTRY(swm_region) entry;
241 struct swm_geometry g;
242 struct workspace *ws; /* current workspace on this region */
243 struct workspace *ws_prior; /* prior workspace on this region */
244 struct swm_screen *s; /* screen idx */
247 TAILQ_HEAD(swm_region_list, swm_region);
250 TAILQ_ENTRY(ws_win) entry;
253 struct ws_win *child_trans; /* transient child window */
254 struct swm_geometry g; /* current geometry */
255 struct swm_geometry g_float; /* geometry when floating */
256 struct swm_geometry rg_float; /* region geom when floating */
257 int g_floatvalid; /* flag: geometry in g_float is valid */
258 int floatmaxed; /* flag: floater was maxed in max_stack */
261 unsigned int ewmh_flags;
262 int font_size_boundary[SWM_MAX_FONT_STEPS];
268 unsigned long quirks;
269 struct workspace *ws; /* always valid */
270 struct swm_screen *s; /* always valid, never changes */
271 XWindowAttributes wa;
275 TAILQ_HEAD(ws_win_list, ws_win);
277 /* layout handlers */
279 void vertical_config(struct workspace *, int);
280 void vertical_stack(struct workspace *, struct swm_geometry *);
281 void horizontal_config(struct workspace *, int);
282 void horizontal_stack(struct workspace *, struct swm_geometry *);
283 void max_stack(struct workspace *, struct swm_geometry *);
285 struct ws_win *find_window(Window);
287 void grabbuttons(struct ws_win *, int);
288 void new_region(struct swm_screen *, int, int, int, int);
289 void unmanage_window(struct ws_win *);
290 long getstate(Window);
293 void (*l_stack)(struct workspace *, struct swm_geometry *);
294 void (*l_config)(struct workspace *, int);
296 #define SWM_L_FOCUSPREV (1<<0)
297 #define SWM_L_MAPONFOCUS (1<<1)
300 /* stack, configure */
301 { vertical_stack, vertical_config, 0, "[|]" },
302 { horizontal_stack, horizontal_config, 0, "[-]" },
304 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
305 { NULL, NULL, 0, NULL },
308 /* position of max_stack mode in the layouts array */
309 #define SWM_MAX_STACK 2
311 #define SWM_H_SLICE (32)
312 #define SWM_V_SLICE (32)
314 /* define work spaces */
316 int idx; /* workspace index */
317 struct layout *cur_layout; /* current layout handlers */
318 struct ws_win *focus; /* may be NULL */
319 struct ws_win *focus_prev; /* may be NULL */
320 struct swm_region *r; /* may be NULL */
321 struct swm_region *old_r; /* may be NULL */
322 struct ws_win_list winlist; /* list of windows in ws */
323 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
327 int horizontal_msize;
329 int horizontal_stacks;
336 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
337 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
339 /* physical screen mapping */
340 #define SWM_WS_MAX (10)
342 int idx; /* screen index */
343 struct swm_region_list rl; /* list of regions on this screen */
344 struct swm_region_list orl; /* list of old regions */
346 struct workspace ws[SWM_WS_MAX];
352 } c[SWM_S_COLOR_MAX];
354 struct swm_screen *screens;
357 /* args to functions */
360 #define SWM_ARG_ID_FOCUSNEXT (0)
361 #define SWM_ARG_ID_FOCUSPREV (1)
362 #define SWM_ARG_ID_FOCUSMAIN (2)
363 #define SWM_ARG_ID_FOCUSCUR (4)
364 #define SWM_ARG_ID_SWAPNEXT (10)
365 #define SWM_ARG_ID_SWAPPREV (11)
366 #define SWM_ARG_ID_SWAPMAIN (12)
367 #define SWM_ARG_ID_MOVELAST (13)
368 #define SWM_ARG_ID_MASTERSHRINK (20)
369 #define SWM_ARG_ID_MASTERGROW (21)
370 #define SWM_ARG_ID_MASTERADD (22)
371 #define SWM_ARG_ID_MASTERDEL (23)
372 #define SWM_ARG_ID_STACKRESET (30)
373 #define SWM_ARG_ID_STACKINIT (31)
374 #define SWM_ARG_ID_CYCLEWS_UP (40)
375 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
376 #define SWM_ARG_ID_CYCLESC_UP (42)
377 #define SWM_ARG_ID_CYCLESC_DOWN (43)
378 #define SWM_ARG_ID_STACKINC (50)
379 #define SWM_ARG_ID_STACKDEC (51)
380 #define SWM_ARG_ID_SS_ALL (60)
381 #define SWM_ARG_ID_SS_WINDOW (61)
382 #define SWM_ARG_ID_DONTCENTER (70)
383 #define SWM_ARG_ID_CENTER (71)
384 #define SWM_ARG_ID_KILLWINDOW (80)
385 #define SWM_ARG_ID_DELETEWINDOW (81)
389 void focus(struct swm_region *, union arg *);
390 void focus_magic(struct ws_win *, int);
391 #define SWM_F_GENERIC (0)
392 #define SWM_F_TRANSIENT (1)
398 #define SWM_Q_FLOAT (1<<0) /* float this window */
399 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
400 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
401 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
402 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
404 int quirks_size = 0, quirks_length = 0;
405 struct quirk *quirks = NULL;
408 * Supported EWMH hints should be added to
409 * both the enum and the ewmh array
411 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
412 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
413 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
414 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
415 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
416 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
417 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
418 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
419 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
420 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
426 } ewmh[SWM_EWMH_HINT_MAX] = {
427 /* must be in same order as in the enum */
428 {"_NET_ACTIVE_WINDOW", None},
429 {"_NET_MOVERESIZE_WINDOW", None},
430 {"_NET_CLOSE_WINDOW", None},
431 {"_NET_WM_WINDOW_TYPE", None},
432 {"_NET_WM_WINDOW_TYPE_DOCK", None},
433 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
434 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
435 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
436 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
437 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
438 {"_NET_WM_STATE", None},
439 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
440 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
441 {"_NET_WM_STATE_SKIP_TASKBAR", None},
442 {"_NET_WM_STATE_SKIP_PAGER", None},
443 {"_NET_WM_STATE_HIDDEN", None},
444 {"_NET_WM_STATE_ABOVE", None},
445 {"_SWM_WM_STATE_MANUAL", None},
446 {"_NET_WM_STATE_FULLSCREEN", None},
447 {"_NET_WM_ALLOWED_ACTIONS", None},
448 {"_NET_WM_ACTION_MOVE", None},
449 {"_NET_WM_ACTION_RESIZE", None},
450 {"_NET_WM_ACTION_FULLSCREEN", None},
451 {"_NET_WM_ACTION_CLOSE", None},
454 void store_float_geom(struct ws_win *win, struct swm_region *r);
455 int floating_toggle_win(struct ws_win *win);
458 get_property(Window id, Atom atom, long count, Atom type,
459 unsigned long *n, unsigned char **data)
462 unsigned long tmp, extra;
463 unsigned long *nitems;
466 nitems = n != NULL ? n : &tmp;
467 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
468 &real, &format, nitems, &extra, data);
470 if (status != Success)
484 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
486 for (i = 0; i < LENGTH(ewmh); i++)
487 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
489 for (i = 0; i < ScreenCount(display); i++) {
490 /* Support check window will be created by workaround(). */
492 /* Report supported atoms */
493 XDeleteProperty(display, screens[i].root, sup_list);
494 for (j = 0; j < LENGTH(ewmh); j++)
495 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
496 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
504 unsigned char *data = NULL;
506 Atom sup_check, sup_list;
509 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
510 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
512 for (i = 0; i < ScreenCount(display); i++) {
513 /* Get the support check window and destroy it */
514 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
519 XDestroyWindow(display, id);
520 XDeleteProperty(display, screens[i].root, sup_check);
521 XDeleteProperty(display, screens[i].root, sup_list);
529 ewmh_autoquirk(struct ws_win *win)
532 unsigned long *data = NULL;
536 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
537 XA_ATOM, &n, (unsigned char **)&data);
544 for (i = 0; i < n; i++) {
546 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
548 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
549 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
550 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
552 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
555 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
556 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
558 win->quirks = SWM_Q_FLOAT;
566 #define SWM_EWMH_ACTION_COUNT_MAX (6)
567 #define EWMH_F_FULLSCREEN (1<<0)
568 #define EWMH_F_ABOVE (1<<1)
569 #define EWMH_F_HIDDEN (1<<2)
570 #define EWMH_F_SKIP_PAGER (1<<3)
571 #define EWMH_F_SKIP_TASKBAR (1<<4)
572 #define SWM_F_MANUAL (1<<5)
575 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
577 struct swm_geometry rg;
585 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
591 store_float_geom(win, win->ws->r);
598 if (win->g_floatvalid) {
599 /* refloat at last floating relative position */
600 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
601 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
602 win->g.w = win->g_float.w;
603 win->g.h = win->g_float.h;
611 ewmh_update_actions(struct ws_win *win)
613 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
619 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
622 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
623 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
626 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
627 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
630 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
631 #define _NET_WM_STATE_ADD 1 /* add/set property */
632 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
635 ewmh_update_win_state(struct ws_win *win, long state, long action)
637 unsigned int mask = 0;
638 unsigned int changed = 0;
639 unsigned int orig_flags;
644 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
645 mask = EWMH_F_FULLSCREEN;
646 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
648 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
650 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
651 mask = EWMH_F_SKIP_PAGER;
652 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
653 mask = EWMH_F_SKIP_TASKBAR;
656 orig_flags = win->ewmh_flags;
659 case _NET_WM_STATE_REMOVE:
660 win->ewmh_flags &= ~mask;
662 case _NET_WM_STATE_ADD:
663 win->ewmh_flags |= mask;
665 case _NET_WM_STATE_TOGGLE:
666 win->ewmh_flags ^= mask;
670 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
672 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
674 if (!floating_toggle_win(win))
675 win->ewmh_flags = orig_flags; /* revert */
676 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
678 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
679 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
681 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
682 win->ewmh_flags = orig_flags; /* revert */
684 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
686 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
687 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
688 XA_ATOM, 32, PropModeAppend,
689 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
690 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
691 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
692 XA_ATOM, 32, PropModeAppend,
693 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
694 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
695 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
696 XA_ATOM, 32, PropModeAppend,
697 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
698 if (win->ewmh_flags & EWMH_F_ABOVE)
699 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
700 XA_ATOM, 32, PropModeAppend,
701 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
702 if (win->ewmh_flags & SWM_F_MANUAL)
703 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
704 XA_ATOM, 32, PropModeAppend,
705 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
709 ewmh_get_win_state(struct ws_win *win)
720 win->ewmh_flags |= EWMH_F_ABOVE;
722 win->ewmh_flags |= SWM_F_MANUAL;
724 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
725 &n, (unsigned char **)&states);
730 for (i = 0; i < n; i++)
731 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
751 name = "ButtonPress";
754 name = "ButtonRelease";
757 name = "MotionNotify";
760 name = "EnterNotify";
763 name = "LeaveNotify";
772 name = "KeymapNotify";
778 name = "GraphicsExpose";
783 case VisibilityNotify:
784 name = "VisibilityNotify";
787 name = "CreateNotify";
790 name = "DestroyNotify";
793 name = "UnmapNotify";
802 name = "ReparentNotify";
804 case ConfigureNotify:
805 name = "ConfigureNotify";
807 case ConfigureRequest:
808 name = "ConfigureRequest";
811 name = "GravityNotify";
814 name = "ResizeRequest";
816 case CirculateNotify:
817 name = "CirculateNotify";
819 case CirculateRequest:
820 name = "CirculateRequest";
823 name = "PropertyNotify";
826 name = "SelectionClear";
828 case SelectionRequest:
829 name = "SelectionRequest";
831 case SelectionNotify:
832 name = "SelectionNotify";
835 name = "ColormapNotify";
838 name = "ClientMessage";
841 name = "MappingNotify";
846 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
848 e->xany.window, name, e->type, QLength(display));
850 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
852 e->xany.window, e->type, QLength(display));
856 dumpwins(struct swm_region *r, union arg *args)
860 XWindowAttributes wa;
863 fprintf(stderr, "invalid workspace\n");
867 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
869 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
870 state = getstate(win->id);
871 if (!XGetWindowAttributes(display, win->id, &wa))
872 fprintf(stderr, "window: %lu failed "
873 "XGetWindowAttributes\n", win->id);
874 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
875 win->id, wa.map_state, state);
878 fprintf(stderr, "===== unmanaged window list =====\n");
879 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
880 state = getstate(win->id);
881 if (!XGetWindowAttributes(display, win->id, &wa))
882 fprintf(stderr, "window: %lu failed "
883 "XGetWindowAttributes\n", win->id);
884 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
885 win->id, wa.map_state, state);
888 fprintf(stderr, "=================================\n");
893 dumpwins(struct swm_region *r, union arg *args)
896 #endif /* SWM_DEBUG */
898 void expose(XEvent *);
899 void keypress(XEvent *);
900 void buttonpress(XEvent *);
901 void configurerequest(XEvent *);
902 void configurenotify(XEvent *);
903 void destroynotify(XEvent *);
904 void enternotify(XEvent *);
905 void focusevent(XEvent *);
906 void mapnotify(XEvent *);
907 void mappingnotify(XEvent *);
908 void maprequest(XEvent *);
909 void propertynotify(XEvent *);
910 void unmapnotify(XEvent *);
911 void visibilitynotify(XEvent *);
912 void clientmessage(XEvent *);
914 void (*handler[LASTEvent])(XEvent *) = {
916 [KeyPress] = keypress,
917 [ButtonPress] = buttonpress,
918 [ConfigureRequest] = configurerequest,
919 [ConfigureNotify] = configurenotify,
920 [DestroyNotify] = destroynotify,
921 [EnterNotify] = enternotify,
922 [FocusIn] = focusevent,
923 [FocusOut] = focusevent,
924 [MapNotify] = mapnotify,
925 [MappingNotify] = mappingnotify,
926 [MapRequest] = maprequest,
927 [PropertyNotify] = propertynotify,
928 [UnmapNotify] = unmapnotify,
929 [VisibilityNotify] = visibilitynotify,
930 [ClientMessage] = clientmessage,
936 int saved_errno, status;
943 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
949 warn("sighdlr: waitpid");
950 #endif /* SWM_DEBUG */
955 if (WIFEXITED(status)) {
956 if (WEXITSTATUS(status) != 0)
957 warnx("sighdlr: child exit status: %d",
958 WEXITSTATUS(status));
960 warnx("sighdlr: child is terminated "
962 #endif /* SWM_DEBUG */
980 name_to_color(char *colorname)
984 XColor screen_def, exact_def;
985 unsigned long result = 0;
986 char cname[32] = "#";
988 cmap = DefaultColormap(display, screens[0].idx);
989 status = XAllocNamedColor(display, cmap, colorname,
990 &screen_def, &exact_def);
992 strlcat(cname, colorname + 2, sizeof cname - 1);
993 status = XAllocNamedColor(display, cmap, cname, &screen_def,
997 result = screen_def.pixel;
999 fprintf(stderr, "color '%s' not found.\n", colorname);
1005 setscreencolor(char *val, int i, int c)
1007 if (i > 0 && i <= ScreenCount(display)) {
1008 screens[i - 1].c[c].color = name_to_color(val);
1009 free(screens[i - 1].c[c].name);
1010 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1012 } else if (i == -1) {
1013 for (i = 0; i < ScreenCount(display); i++) {
1014 screens[i].c[c].color = name_to_color(val);
1015 free(screens[i].c[c].name);
1016 if ((screens[i].c[c].name = strdup(val)) == NULL)
1020 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1021 i, ScreenCount(display));
1025 custom_region(char *val)
1027 unsigned int sidx, x, y, w, h;
1029 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1030 errx(1, "invalid custom region, "
1031 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1032 if (sidx < 1 || sidx > ScreenCount(display))
1033 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1034 sidx, ScreenCount(display));
1038 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1040 if (x < 0 || x > DisplayWidth(display, sidx) ||
1041 y < 0 || y > DisplayHeight(display, sidx) ||
1042 w + x > DisplayWidth(display, sidx) ||
1043 h + y > DisplayHeight(display, sidx)) {
1044 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1045 "(%ux%u)\n", w, h, x, y,
1046 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1050 new_region(&screens[sidx], x, y, w, h);
1054 socket_setnonblock(int fd)
1058 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1059 err(1, "fcntl F_GETFL");
1060 flags |= O_NONBLOCK;
1061 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1062 err(1, "fcntl F_SETFL");
1066 bar_print(struct swm_region *r, char *s)
1068 XClearWindow(display, r->bar_window);
1069 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1070 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1075 bar_extra_stop(void)
1079 bzero(bar_pipe, sizeof bar_pipe);
1082 kill(bar_pid, SIGTERM);
1085 strlcpy(bar_ext, "", sizeof bar_ext);
1090 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1092 int do_class, do_name;
1094 XClassHint *xch = NULL;
1096 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1097 cur_focus != NULL) {
1098 if ((xch = XAllocClassHint()) == NULL)
1100 status = XGetClassHint(display, cur_focus->id, xch);
1101 if (status == BadWindow || status == BadAlloc)
1103 do_class = (title_class_enabled && xch->res_class != NULL);
1104 do_name = (title_name_enabled && xch->res_name != NULL);
1106 strlcat(s, xch->res_class, sz);
1107 if (do_class && do_name)
1108 strlcat(s, ":", sz);
1110 strlcat(s, xch->res_name, sz);
1111 strlcat(s, " ", sz);
1123 struct swm_region *r;
1126 char s[SWM_BAR_MAX];
1127 char cn[SWM_BAR_MAX];
1128 char loc[SWM_BAR_MAX];
1132 if (bar_enabled == 0)
1134 if (bar_extra && bar_extra_running) {
1135 /* ignore short reads; it'll correct itself */
1136 while ((b = fgetln(stdin, &len)) != NULL)
1137 if (b && b[len - 1] == '\n') {
1139 strlcpy(bar_ext, b, sizeof bar_ext);
1141 if (b == NULL && errno != EAGAIN) {
1142 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1143 errno, strerror(errno));
1147 strlcpy(bar_ext, "", sizeof bar_ext);
1149 if (clock_enabled == 0)
1150 strlcpy(s, "", sizeof s);
1153 localtime_r(&tmt, &tm);
1154 strftime(s, sizeof s, clock_format, &tm);
1155 strlcat(s, " ", sizeof s);
1158 for (i = 0; i < ScreenCount(display); i++) {
1160 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1161 strlcpy(cn, "", sizeof cn);
1163 bar_class_name(cn, sizeof cn, r->ws->focus);
1166 stack = r->ws->cur_layout->name;
1168 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1169 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1184 bar_toggle(struct swm_region *r, union arg *args)
1186 struct swm_region *tmpr;
1187 int i, sc = ScreenCount(display);
1189 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1192 for (i = 0; i < sc; i++)
1193 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1194 XUnmapWindow(display, tmpr->bar_window);
1196 for (i = 0; i < sc; i++)
1197 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1198 XMapRaised(display, tmpr->bar_window);
1200 bar_enabled = !bar_enabled;
1203 /* must be after stack */
1210 XSetWindowAttributes wa;
1211 struct swm_region *r;
1214 /* do this here because the conf file is in memory */
1215 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1216 /* launch external status app */
1217 bar_extra_running = 1;
1218 if (pipe(bar_pipe) == -1)
1219 err(1, "pipe error");
1220 socket_setnonblock(bar_pipe[0]);
1221 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1222 if (dup2(bar_pipe[0], 0) == -1)
1224 if (dup2(bar_pipe[1], 1) == -1)
1226 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1227 err(1, "could not disable SIGPIPE");
1228 switch (bar_pid = fork()) {
1230 err(1, "cannot fork");
1234 execvp(bar_argv[0], bar_argv);
1235 err(1, "%s external app failed", bar_argv[0]);
1237 default: /* parent */
1243 bzero(&wa, sizeof wa);
1244 for (i = 0; i < ScreenCount(display); i++)
1245 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1247 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1248 wa.background_pixel =
1249 screens[i].c[SWM_S_COLOR_BAR].color;
1250 XChangeWindowAttributes(display, r->bar_window,
1251 CWBackPixel | CWBorderPixel, &wa);
1257 bar_setup(struct swm_region *r)
1262 XFreeFont(display, bar_fs);
1266 for (i = 0; bar_fonts[i] != NULL; i++) {
1267 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1273 if (bar_fonts[i] == NULL)
1274 errx(1, "couldn't load font");
1276 errx(1, "couldn't create font structure");
1278 bar_height = bar_fs->ascent + bar_fs->descent + 3;
1280 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1282 r->bar_window = XCreateSimpleWindow(display,
1283 r->s->root, x, y, WIDTH(r) - 2, bar_height - 2,
1284 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1285 r->s->c[SWM_S_COLOR_BAR].color);
1286 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1287 XSetFont(display, bar_gc, bar_fs->fid);
1288 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1290 XMapRaised(display, r->bar_window);
1291 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1293 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1294 err(1, "could not install bar_signal");
1299 set_win_state(struct ws_win *win, long state)
1301 long data[] = {state, None};
1303 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1308 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1309 (unsigned char *)data, 2);
1316 unsigned char *p = NULL;
1319 if (!get_property(w, astate, 2L, astate, &n, &p))
1322 result = *((long *)p);
1328 version(struct swm_region *r, union arg *args)
1330 bar_version = !bar_version;
1332 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1333 SWM_VERSION, cvstag);
1335 strlcpy(bar_vertext, "", sizeof bar_vertext);
1340 client_msg(struct ws_win *win, Atom a)
1342 XClientMessageEvent cm;
1347 bzero(&cm, sizeof cm);
1348 cm.type = ClientMessage;
1349 cm.window = win->id;
1350 cm.message_type = aprot;
1353 cm.data.l[1] = CurrentTime;
1354 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1358 configreq_win(struct ws_win *win)
1360 XConfigureRequestEvent cr;
1365 bzero(&cr, sizeof cr);
1366 cr.type = ConfigureRequest;
1367 cr.display = display;
1368 cr.parent = win->id;
1369 cr.window = win->id;
1372 cr.width = win->g.w;
1373 cr.height = win->g.h;
1374 cr.border_width = 1;
1376 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1380 config_win(struct ws_win *win)
1384 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1385 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1390 ce.type = ConfigureNotify;
1391 ce.display = display;
1393 ce.window = win->id;
1396 ce.width = win->g.w;
1397 ce.height = win->g.h;
1398 ce.border_width = 1; /* XXX store this! */
1400 ce.override_redirect = False;
1401 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1405 count_win(struct workspace *ws, int count_transient)
1410 TAILQ_FOREACH(win, &ws->winlist, entry) {
1411 if (count_transient == 0 && win->floating)
1413 if (count_transient == 0 && win->transient)
1417 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1423 quit(struct swm_region *r, union arg *args)
1425 DNPRINTF(SWM_D_MISC, "quit\n");
1430 unmap_window(struct ws_win *win)
1435 /* don't unmap again */
1436 if (getstate(win->id) == IconicState)
1439 set_win_state(win, IconicState);
1441 XUnmapWindow(display, win->id);
1442 XSetWindowBorder(display, win->id,
1443 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1452 for (i = 0; i < ScreenCount(display); i++)
1453 for (j = 0; j < SWM_WS_MAX; j++)
1454 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1459 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1466 event.display = display; /* Ignored, but what the hell */
1467 event.window = win->id;
1468 event.root = win->s->root;
1469 event.subwindow = None;
1470 event.time = CurrentTime;
1475 event.same_screen = True;
1476 event.keycode = XKeysymToKeycode(display, keysym);
1477 event.state = modifiers;
1479 event.type = KeyPress;
1480 XSendEvent(event.display, event.window, True,
1481 KeyPressMask, (XEvent *)&event);
1483 event.type = KeyRelease;
1484 XSendEvent(event.display, event.window, True,
1485 KeyPressMask, (XEvent *)&event);
1490 restart(struct swm_region *r, union arg *args)
1492 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1494 /* disable alarm because the following code may not be interrupted */
1496 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1497 errx(1, "can't disable alarm");
1502 XCloseDisplay(display);
1503 execvp(start_argv[0], start_argv);
1504 fprintf(stderr, "execvp failed\n");
1510 root_to_region(Window root)
1512 struct swm_region *r = NULL;
1514 int i, x, y, wx, wy;
1517 for (i = 0; i < ScreenCount(display); i++)
1518 if (screens[i].root == root)
1521 if (XQueryPointer(display, screens[i].root,
1522 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1523 /* choose a region based on pointer location */
1524 TAILQ_FOREACH(r, &screens[i].rl, entry)
1525 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1526 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1531 r = TAILQ_FIRST(&screens[i].rl);
1537 find_unmanaged_window(Window id)
1542 for (i = 0; i < ScreenCount(display); i++)
1543 for (j = 0; j < SWM_WS_MAX; j++)
1544 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1552 find_window(Window id)
1555 Window wrr, wpr, *wcr = NULL;
1559 for (i = 0; i < ScreenCount(display); i++)
1560 for (j = 0; j < SWM_WS_MAX; j++)
1561 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1565 /* if we were looking for the parent return that window instead */
1566 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1571 /* ignore not found and root */
1572 if (wpr == 0 || wrr == wpr)
1575 /* look for parent */
1576 for (i = 0; i < ScreenCount(display); i++)
1577 for (j = 0; j < SWM_WS_MAX; j++)
1578 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1586 spawn(struct swm_region *r, union arg *args)
1591 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1595 close(ConnectionNumber(display));
1597 setenv("LD_PRELOAD", SWM_LIB, 1);
1599 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1603 setenv("_SWM_WS", ret, 1);
1607 if (asprintf(&ret, "%d", getpid()) == -1) {
1611 setenv("_SWM_PID", ret, 1);
1615 if (setsid() == -1) {
1621 * close stdin and stdout to prevent interaction between apps
1622 * and the baraction script
1623 * leave stderr open to record errors
1625 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1629 dup2(fd, STDIN_FILENO);
1630 dup2(fd, STDOUT_FILENO);
1634 execvp(args->argv[0], args->argv);
1642 spawnterm(struct swm_region *r, union arg *args)
1644 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1647 setenv("_SWM_XTERM_FONTADJ", "", 1);
1652 kill_refs(struct ws_win *win)
1655 struct swm_region *r;
1656 struct workspace *ws;
1661 for (i = 0; i < ScreenCount(display); i++)
1662 TAILQ_FOREACH(r, &screens[i].rl, entry)
1663 for (x = 0; x < SWM_WS_MAX; x++) {
1665 if (win == ws->focus)
1667 if (win == ws->focus_prev)
1668 ws->focus_prev = NULL;
1673 validate_win(struct ws_win *testwin)
1676 struct workspace *ws;
1677 struct swm_region *r;
1678 int i, x, foundit = 0;
1680 if (testwin == NULL)
1683 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1684 TAILQ_FOREACH(r, &screens[i].rl, entry)
1685 for (x = 0; x < SWM_WS_MAX; x++) {
1687 TAILQ_FOREACH(win, &ws->winlist, entry)
1695 validate_ws(struct workspace *testws)
1697 struct swm_region *r;
1698 struct workspace *ws;
1701 /* validate all ws */
1702 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1703 TAILQ_FOREACH(r, &screens[i].rl, entry)
1704 for (x = 0; x < SWM_WS_MAX; x++) {
1713 unfocus_win(struct ws_win *win)
1718 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1722 if (win->ws == NULL)
1725 if (validate_ws(win->ws))
1726 abort(); /* XXX replace with return at some point */
1728 if (win->ws->r == NULL)
1731 if (validate_win(win)) {
1736 if (win->ws->focus == win) {
1737 win->ws->focus = NULL;
1738 win->ws->focus_prev = win;
1741 if (validate_win(win->ws->focus)) {
1742 kill_refs(win->ws->focus);
1743 win->ws->focus = NULL;
1745 if (validate_win(win->ws->focus_prev)) {
1746 kill_refs(win->ws->focus_prev);
1747 win->ws->focus_prev = NULL;
1750 /* drain all previous unfocus events */
1751 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1754 grabbuttons(win, 0);
1755 XSetWindowBorder(display, win->id,
1756 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1758 XChangeProperty(display, win->s->root,
1759 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1760 PropModeReplace, (unsigned char *)&none,1);
1769 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1771 for (i = 0; i < ScreenCount(display); i++)
1772 for (j = 0; j < SWM_WS_MAX; j++)
1773 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1778 focus_win(struct ws_win *win)
1783 struct ws_win *cfw = NULL;
1786 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1790 if (win->ws == NULL)
1793 if (validate_ws(win->ws))
1794 abort(); /* XXX replace with return at some point */
1796 if (validate_win(win)) {
1801 if (validate_win(win)) {
1806 XGetInputFocus(display, &cur_focus, &rr);
1807 if ((cfw = find_window(cur_focus)) != NULL)
1810 win->ws->focus = win;
1812 if (win->ws->r != NULL) {
1813 /* drain all previous focus events */
1814 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1818 XSetInputFocus(display, win->id,
1819 RevertToParent, CurrentTime);
1820 grabbuttons(win, 1);
1821 XSetWindowBorder(display, win->id,
1822 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1823 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1824 XMapRaised(display, win->id);
1826 XChangeProperty(display, win->s->root,
1827 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1828 PropModeReplace, (unsigned char *)&win->id,1);
1833 switchws(struct swm_region *r, union arg *args)
1835 int wsid = args->id, unmap_old = 0;
1836 struct swm_region *this_r, *other_r;
1838 struct workspace *new_ws, *old_ws;
1845 old_ws = this_r->ws;
1846 new_ws = &this_r->s->ws[wsid];
1848 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1849 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1852 if (new_ws == NULL || old_ws == NULL)
1854 if (new_ws == old_ws)
1857 other_r = new_ws->r;
1858 if (other_r == NULL) {
1859 /* the other workspace is hidden, hide this one */
1863 /* the other ws is visible in another region, exchange them */
1864 other_r->ws_prior = new_ws;
1865 other_r->ws = old_ws;
1866 old_ws->r = other_r;
1868 this_r->ws_prior = old_ws;
1869 this_r->ws = new_ws;
1873 a.id = SWM_ARG_ID_FOCUSCUR;
1874 focus(new_ws->r, &a);
1877 /* unmap old windows */
1879 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1884 cyclews(struct swm_region *r, union arg *args)
1887 struct swm_screen *s = r->s;
1889 DNPRINTF(SWM_D_WS, "cyclews id %d "
1890 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1891 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1896 case SWM_ARG_ID_CYCLEWS_UP:
1897 if (a.id < SWM_WS_MAX - 1)
1902 case SWM_ARG_ID_CYCLEWS_DOWN:
1906 a.id = SWM_WS_MAX - 1;
1912 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1914 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1918 } while (a.id != r->ws->idx);
1922 priorws(struct swm_region *r, union arg *args)
1926 DNPRINTF(SWM_D_WS, "priorws id %d "
1927 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1928 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1930 if (r->ws_prior == NULL)
1933 a.id = r->ws_prior->idx;
1938 cyclescr(struct swm_region *r, union arg *args)
1940 struct swm_region *rr = NULL;
1944 /* do nothing if we don't have more than one screen */
1945 if (!(ScreenCount(display) > 1 || outputs > 1))
1950 case SWM_ARG_ID_CYCLESC_UP:
1951 rr = TAILQ_NEXT(r, entry);
1953 rr = TAILQ_FIRST(&screens[i].rl);
1955 case SWM_ARG_ID_CYCLESC_DOWN:
1956 rr = TAILQ_PREV(r, swm_region_list, entry);
1958 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1966 /* move mouse to region */
1968 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1969 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1971 a.id = SWM_ARG_ID_FOCUSCUR;
1974 if (rr->ws->focus) {
1975 /* move to focus window */
1976 x = rr->ws->focus->g.x + 1;
1977 y = rr->ws->focus->g.y + 1;
1978 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1983 swapwin(struct swm_region *r, union arg *args)
1985 struct ws_win *target, *source;
1986 struct ws_win *cur_focus;
1987 struct ws_win_list *wl;
1990 DNPRINTF(SWM_D_WS, "swapwin id %d "
1991 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1992 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1994 cur_focus = r->ws->focus;
1995 if (cur_focus == NULL)
1999 wl = &source->ws->winlist;
2002 case SWM_ARG_ID_SWAPPREV:
2003 target = TAILQ_PREV(source, ws_win_list, entry);
2004 TAILQ_REMOVE(wl, cur_focus, entry);
2006 TAILQ_INSERT_TAIL(wl, source, entry);
2008 TAILQ_INSERT_BEFORE(target, source, entry);
2010 case SWM_ARG_ID_SWAPNEXT:
2011 target = TAILQ_NEXT(source, entry);
2012 TAILQ_REMOVE(wl, source, entry);
2014 TAILQ_INSERT_HEAD(wl, source, entry);
2016 TAILQ_INSERT_AFTER(wl, target, source, entry);
2018 case SWM_ARG_ID_SWAPMAIN:
2019 target = TAILQ_FIRST(wl);
2020 if (target == source) {
2021 if (source->ws->focus_prev != NULL &&
2022 source->ws->focus_prev != target)
2024 source = source->ws->focus_prev;
2028 if (target == NULL || source == NULL)
2030 source->ws->focus_prev = target;
2031 TAILQ_REMOVE(wl, target, entry);
2032 TAILQ_INSERT_BEFORE(source, target, entry);
2033 TAILQ_REMOVE(wl, source, entry);
2034 TAILQ_INSERT_HEAD(wl, source, entry);
2036 case SWM_ARG_ID_MOVELAST:
2037 TAILQ_REMOVE(wl, source, entry);
2038 TAILQ_INSERT_TAIL(wl, source, entry);
2041 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2049 focus_prev(struct ws_win *win)
2051 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2052 struct ws_win *cur_focus = NULL;
2053 struct ws_win_list *wl = NULL;
2054 struct workspace *ws = NULL;
2056 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2058 if (!(win && win->ws))
2063 cur_focus = ws->focus;
2064 winlostfocus = cur_focus;
2066 /* pickle, just focus on whatever */
2067 if (cur_focus == NULL) {
2068 /* use prev_focus if valid */
2069 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2070 find_window(WINID(ws->focus_prev)))
2071 winfocus = ws->focus_prev;
2072 if (winfocus == NULL)
2073 winfocus = TAILQ_FIRST(wl);
2077 /* if transient focus on parent */
2078 if (cur_focus->transient) {
2079 winfocus = find_window(cur_focus->transient);
2083 /* if in max_stack try harder */
2084 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
2085 if (cur_focus != ws->focus_prev)
2086 winfocus = ws->focus_prev;
2087 else if (cur_focus != ws->focus)
2088 winfocus = ws->focus;
2090 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2095 if (cur_focus == win)
2096 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2097 if (winfocus == NULL)
2098 winfocus = TAILQ_LAST(wl, ws_win_list);
2099 if (winfocus == NULL || winfocus == win)
2100 winfocus = TAILQ_NEXT(cur_focus, entry);
2102 if (winfocus == winlostfocus || winfocus == NULL)
2105 focus_magic(winfocus, SWM_F_GENERIC);
2109 focus(struct swm_region *r, union arg *args)
2111 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2112 struct ws_win *cur_focus = NULL;
2113 struct ws_win_list *wl = NULL;
2114 struct workspace *ws = NULL;
2119 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2121 /* treat FOCUS_CUR special */
2122 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2124 winfocus = r->ws->focus;
2125 else if (r->ws->focus_prev)
2126 winfocus = r->ws->focus_prev;
2128 winfocus = TAILQ_FIRST(&r->ws->winlist);
2130 focus_magic(winfocus, SWM_F_GENERIC);
2134 if ((cur_focus = r->ws->focus) == NULL)
2139 winlostfocus = cur_focus;
2142 case SWM_ARG_ID_FOCUSPREV:
2143 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
2144 if (winfocus == NULL)
2145 winfocus = TAILQ_LAST(wl, ws_win_list);
2148 case SWM_ARG_ID_FOCUSNEXT:
2149 winfocus = TAILQ_NEXT(cur_focus, entry);
2150 if (winfocus == NULL)
2151 winfocus = TAILQ_FIRST(wl);
2154 case SWM_ARG_ID_FOCUSMAIN:
2155 winfocus = TAILQ_FIRST(wl);
2156 if (winfocus == cur_focus)
2157 winfocus = cur_focus->ws->focus_prev;
2164 if (winfocus == winlostfocus || winfocus == NULL)
2167 focus_magic(winfocus, SWM_F_GENERIC);
2171 cycle_layout(struct swm_region *r, union arg *args)
2173 struct workspace *ws = r->ws;
2174 struct ws_win *winfocus;
2177 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2179 winfocus = ws->focus;
2182 if (ws->cur_layout->l_stack == NULL)
2183 ws->cur_layout = &layouts[0];
2186 a.id = SWM_ARG_ID_FOCUSCUR;
2192 stack_config(struct swm_region *r, union arg *args)
2194 struct workspace *ws = r->ws;
2196 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2199 if (ws->cur_layout->l_config != NULL)
2200 ws->cur_layout->l_config(ws, args->id);
2202 if (args->id != SWM_ARG_ID_STACKINIT);
2208 struct swm_geometry g;
2209 struct swm_region *r;
2212 DNPRINTF(SWM_D_STACK, "stack\n");
2214 for (i = 0; i < ScreenCount(display); i++) {
2216 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2217 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2218 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2220 /* start with screen geometry, adjust for bar */
2229 r->ws->cur_layout->l_stack(r->ws, &g);
2230 /* save r so we can track region changes */
2239 store_float_geom(struct ws_win *win, struct swm_region *r)
2241 /* retain window geom and region geom */
2242 win->g_float.x = win->g.x;
2243 win->g_float.y = win->g.y;
2244 win->g_float.w = win->g.w;
2245 win->g_float.h = win->g.h;
2246 win->rg_float.x = r->g.x;
2247 win->rg_float.y = r->g.y;
2248 win->rg_float.w = r->g.w;
2249 win->rg_float.h = r->g.h;
2250 win->g_floatvalid = 1;
2254 stack_floater(struct ws_win *win, struct swm_region *r)
2262 bzero(&wc, sizeof wc);
2263 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2266 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2267 * geom on ws switches or return from max mode
2270 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2271 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2273 * use stored g and rg to set relative position and size
2274 * as in old region or before max stack mode
2276 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2277 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2278 win->g.w = win->g_float.w;
2279 win->g.h = win->g_float.h;
2280 win->g_floatvalid = 0;
2283 win->floatmaxed = 0;
2285 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2286 (win->g.h >= HEIGHT(r)))
2287 wc.border_width = 0;
2289 wc.border_width = 1;
2290 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2291 win->g.w = (double)WIDTH(r) * dialog_ratio;
2292 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2297 * floaters and transients are auto-centred unless moved
2300 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2;
2301 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2;
2304 /* win can be outside r if new r smaller than old r */
2305 /* Ensure top left corner inside r (move probs otherwise) */
2306 if (win->g.x < r->g.x )
2308 if (win->g.x > r->g.x + r->g.w - 1)
2309 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2310 (r->g.x + r->g.w - win->g.w - 2);
2311 if (win->g.y < r->g.y )
2313 if (win->g.y > r->g.y + r->g.h - 1)
2314 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2315 (r->g.y + r->g.h - win->g.h - 2);
2319 wc.width = win->g.w;
2320 wc.height = win->g.h;
2323 * Retain floater and transient geometry for correct positioning
2324 * when ws changes region
2326 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2327 store_float_geom(win, r);
2329 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2330 win->id, wc.x, wc.y, wc.width, wc.height);
2332 XConfigureWindow(display, win->id, mask, &wc);
2337 * Send keystrokes to terminal to decrease/increase the font size as the
2338 * window size changes.
2341 adjust_font(struct ws_win *win)
2343 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2344 win->floating || win->transient)
2347 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2348 win->g.w / win->sh.width_inc < term_width &&
2349 win->font_steps < SWM_MAX_FONT_STEPS) {
2350 win->font_size_boundary[win->font_steps] =
2351 (win->sh.width_inc * term_width) + win->sh.base_width;
2354 win->last_inc = win->sh.width_inc;
2355 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2356 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2357 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2360 win->last_inc = win->sh.width_inc;
2361 fake_keypress(win, XK_KP_Add, ShiftMask);
2365 #define SWAPXY(g) do { \
2367 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2368 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2371 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2374 XWindowAttributes wa;
2375 struct swm_geometry win_g, r_g = *g;
2376 struct ws_win *win, *fs_win = 0;
2377 int i, j, s, stacks;
2378 int w_inc = 1, h_inc, w_base = 1, h_base;
2379 int hrh, extra = 0, h_slice, last_h = 0;
2380 int split, colno, winno, mwin, msize, mscale;
2381 int remain, missing, v_slice, reconfigure;
2384 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2385 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2387 winno = count_win(ws, 0);
2388 if (winno == 0 && count_win(ws, 1) == 0)
2391 TAILQ_FOREACH(win, &ws->winlist, entry)
2392 if (win->transient == 0 && win->floating == 0)
2399 w_inc = win->sh.width_inc;
2400 w_base = win->sh.base_width;
2401 mwin = ws->l_state.horizontal_mwin;
2402 mscale = ws->l_state.horizontal_msize;
2403 stacks = ws->l_state.horizontal_stacks;
2406 w_inc = win->sh.height_inc;
2407 w_base = win->sh.base_height;
2408 mwin = ws->l_state.vertical_mwin;
2409 mscale = ws->l_state.vertical_msize;
2410 stacks = ws->l_state.vertical_stacks;
2414 if (stacks > winno - mwin)
2415 stacks = winno - mwin;
2419 h_slice = r_g.h / SWM_H_SLICE;
2420 if (mwin && winno > mwin) {
2421 v_slice = r_g.w / SWM_V_SLICE;
2425 win_g.w = v_slice * mscale;
2427 if (w_inc > 1 && w_inc < v_slice) {
2428 /* adjust for window's requested size increment */
2429 remain = (win_g.w - w_base) % w_inc;
2430 missing = w_inc - remain;
2437 win_g.x += r_g.w - msize;
2440 colno = split = winno / stacks;
2441 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
2443 hrh = r_g.h / colno;
2444 extra = r_g.h - (colno * hrh);
2447 /* stack all the tiled windows */
2448 i = j = 0, s = stacks;
2449 TAILQ_FOREACH(win, &ws->winlist, entry) {
2450 if (win->transient != 0 || win->floating != 0)
2453 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2458 if (split && i == split) {
2459 colno = (winno - mwin) / stacks;
2460 if (s <= (winno - mwin) % stacks)
2462 split = split + colno;
2463 hrh = (r_g.h / colno);
2464 extra = r_g.h - (colno * hrh);
2468 win_g.x += win_g.w + 2;
2469 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2471 win_g.w += (r_g.w - msize - (stacks * 2)) %
2478 h_inc = win->sh.width_inc;
2479 h_base = win->sh.base_width;
2481 h_inc = win->sh.height_inc;
2482 h_base = win->sh.base_height;
2484 if (j == colno - 1) {
2485 win_g.h = hrh + extra;
2486 } else if (h_inc > 1 && h_inc < h_slice) {
2487 /* adjust for window's requested size increment */
2488 remain = (win_g.h - h_base) % h_inc;
2489 missing = h_inc - remain;
2491 if (missing <= extra || j == 0) {
2503 win_g.y += last_h + 2;
2505 bzero(&wc, sizeof wc);
2506 if (disable_border && bar_enabled == 0 && winno == 1){
2507 wc.border_width = 0;
2511 wc.border_width = 1;
2514 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2515 win->g.w != win_g.h || win->g.h != win_g.w) {
2517 win->g.x = wc.x = win_g.y;
2518 win->g.y = wc.y = win_g.x;
2519 win->g.w = wc.width = win_g.h;
2520 win->g.h = wc.height = win_g.w;
2523 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2524 win->g.w != win_g.w || win->g.h != win_g.h) {
2526 win->g.x = wc.x = win_g.x;
2527 win->g.y = wc.y = win_g.y;
2528 win->g.w = wc.width = win_g.w;
2529 win->g.h = wc.height = win_g.h;
2534 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2535 XConfigureWindow(display, win->id, mask, &wc);
2539 if (XGetWindowAttributes(display, win->id, &wa))
2540 if (wa.map_state == IsUnmapped)
2541 XMapRaised(display, win->id);
2549 /* now, stack all the floaters and transients */
2550 TAILQ_FOREACH(win, &ws->winlist, entry) {
2551 if (win->transient == 0 && win->floating == 0)
2554 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2559 stack_floater(win, ws->r);
2560 XMapRaised(display, win->id);
2564 stack_floater(fs_win, ws->r);
2565 XMapRaised(display, fs_win->id);
2570 vertical_config(struct workspace *ws, int id)
2572 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2575 case SWM_ARG_ID_STACKRESET:
2576 case SWM_ARG_ID_STACKINIT:
2577 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2578 ws->l_state.vertical_mwin = 1;
2579 ws->l_state.vertical_stacks = 1;
2581 case SWM_ARG_ID_MASTERSHRINK:
2582 if (ws->l_state.vertical_msize > 1)
2583 ws->l_state.vertical_msize--;
2585 case SWM_ARG_ID_MASTERGROW:
2586 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2587 ws->l_state.vertical_msize++;
2589 case SWM_ARG_ID_MASTERADD:
2590 ws->l_state.vertical_mwin++;
2592 case SWM_ARG_ID_MASTERDEL:
2593 if (ws->l_state.vertical_mwin > 0)
2594 ws->l_state.vertical_mwin--;
2596 case SWM_ARG_ID_STACKINC:
2597 ws->l_state.vertical_stacks++;
2599 case SWM_ARG_ID_STACKDEC:
2600 if (ws->l_state.vertical_stacks > 1)
2601 ws->l_state.vertical_stacks--;
2609 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2611 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2613 stack_master(ws, g, 0, 0);
2617 horizontal_config(struct workspace *ws, int id)
2619 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2622 case SWM_ARG_ID_STACKRESET:
2623 case SWM_ARG_ID_STACKINIT:
2624 ws->l_state.horizontal_mwin = 1;
2625 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2626 ws->l_state.horizontal_stacks = 1;
2628 case SWM_ARG_ID_MASTERSHRINK:
2629 if (ws->l_state.horizontal_msize > 1)
2630 ws->l_state.horizontal_msize--;
2632 case SWM_ARG_ID_MASTERGROW:
2633 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2634 ws->l_state.horizontal_msize++;
2636 case SWM_ARG_ID_MASTERADD:
2637 ws->l_state.horizontal_mwin++;
2639 case SWM_ARG_ID_MASTERDEL:
2640 if (ws->l_state.horizontal_mwin > 0)
2641 ws->l_state.horizontal_mwin--;
2643 case SWM_ARG_ID_STACKINC:
2644 ws->l_state.horizontal_stacks++;
2646 case SWM_ARG_ID_STACKDEC:
2647 if (ws->l_state.horizontal_stacks > 1)
2648 ws->l_state.horizontal_stacks--;
2656 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2658 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2660 stack_master(ws, g, 1, 0);
2663 /* fullscreen view */
2665 max_stack(struct workspace *ws, struct swm_geometry *g)
2668 struct swm_geometry gg = *g;
2669 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2673 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2678 winno = count_win(ws, 0);
2679 if (winno == 0 && count_win(ws, 1) == 0)
2682 TAILQ_FOREACH(win, &ws->winlist, entry) {
2683 if (win->transient) {
2685 parent = find_window(win->transient);
2689 if (win->floating && win->floatmaxed == 0 ) {
2691 * retain geometry for retrieval on exit from
2694 store_float_geom(win, ws->r);
2695 win->floatmaxed = 1;
2698 /* only reconfigure if necessary */
2699 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2701 bzero(&wc, sizeof wc);
2702 win->g.x = wc.x = gg.x;
2703 win->g.y = wc.y = gg.y;
2705 wc.border_width = 1;
2706 win->g.w = wc.width = gg.w;
2707 win->g.h = wc.height = gg.h;
2709 wc.border_width = 0;
2710 win->g.w = wc.width = gg.w + 2;
2711 win->g.h = wc.height = gg.h + 2;
2713 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2714 XConfigureWindow(display, win->id, mask, &wc);
2717 /* unmap only if we don't have multi screen */
2718 if (win != ws->focus)
2719 if (!(ScreenCount(display) > 1 || outputs > 1))
2723 /* put the last transient on top */
2726 XMapRaised(display, parent->id);
2727 stack_floater(wintrans, ws->r);
2728 focus_magic(wintrans, SWM_F_TRANSIENT);
2733 send_to_ws(struct swm_region *r, union arg *args)
2735 int wsid = args->id;
2736 struct ws_win *win = win;
2737 struct workspace *ws, *nws;
2738 Atom ws_idx_atom = 0;
2739 unsigned char ws_idx_str[SWM_PROPLEN];
2748 if (win->ws->idx == wsid)
2751 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2754 nws = &win->s->ws[wsid];
2756 a.id = SWM_ARG_ID_FOCUSPREV;
2759 TAILQ_REMOVE(&ws->winlist, win, entry);
2760 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2763 /* Try to update the window's workspace property */
2764 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2766 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2767 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2769 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2770 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2777 wkill(struct swm_region *r, union arg *args)
2779 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2781 if (r->ws->focus == NULL)
2784 if (args->id == SWM_ARG_ID_KILLWINDOW)
2785 XKillClient(display, r->ws->focus->id);
2787 if (r->ws->focus->can_delete)
2788 client_msg(r->ws->focus, adelete);
2793 floating_toggle_win(struct ws_win *win)
2795 struct swm_region *r;
2805 /* reject floating toggles in max stack mode */
2806 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
2809 if (win->floating) {
2810 if (!win->floatmaxed) {
2811 /* retain position for refloat */
2812 store_float_geom(win, r);
2816 if (win->g_floatvalid) {
2817 /* refloat at last floating relative position */
2818 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2819 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2820 win->g.w = win->g_float.w;
2821 win->g.h = win->g_float.h;
2826 ewmh_update_actions(win);
2832 floating_toggle(struct swm_region *r, union arg *args)
2834 struct ws_win *win = r->ws->focus;
2840 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
2841 _NET_WM_STATE_TOGGLE);
2845 if (win == win->ws->focus) {
2846 a.id = SWM_ARG_ID_FOCUSCUR;
2847 focus(win->ws->r, &a);
2852 resize_window(struct ws_win *win, int center)
2856 struct swm_region *r;
2858 r = root_to_region(win->wa.root);
2859 bzero(&wc, sizeof wc);
2860 mask = CWBorderWidth | CWWidth | CWHeight;
2861 wc.border_width = 1;
2862 wc.width = win->g.w;
2863 wc.height = win->g.h;
2864 if (center == SWM_ARG_ID_CENTER) {
2865 wc.x = (WIDTH(r) - win->g.w) / 2;
2866 wc.y = (HEIGHT(r) - win->g.h) / 2;
2870 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2871 win->id, wc.x, wc.y, wc.width, wc.height);
2873 XConfigureWindow(display, win->id, mask, &wc);
2878 resize(struct ws_win *win, union arg *args)
2882 struct swm_region *r = win->ws->r;
2887 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2888 win->id, win->floating, win->transient);
2890 if (!(win->transient != 0 || win->floating != 0))
2893 /* reject resizes in max mode for floaters (transient ok) */
2894 if (win->floatmaxed)
2898 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
2900 /* raise the window = move to last in window list */
2901 a.id = SWM_ARG_ID_MOVELAST;
2905 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2906 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2909 /* place pointer at bottom left corner or nearest point inside r */
2910 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2913 relx = r->g.x + r->g.w - win->g.x - 2;
2915 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2918 rely = r->g.y + r->g.h - win->g.y - 2;
2920 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2922 XMaskEvent(display, MOUSEMASK | ExposureMask |
2923 SubstructureRedirectMask, &ev);
2925 case ConfigureRequest:
2928 handler[ev.type](&ev);
2931 /* do not allow resize outside of region */
2932 if ( ev.xmotion.x_root < r->g.x ||
2933 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2934 ev.xmotion.y_root < r->g.y ||
2935 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2938 if (ev.xmotion.x <= 1)
2940 if (ev.xmotion.y <= 1)
2942 win->g.w = ev.xmotion.x;
2943 win->g.h = ev.xmotion.y;
2945 /* not free, don't sync more than 60 times / second */
2946 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2947 time = ev.xmotion.time;
2948 XSync(display, False);
2949 resize_window(win, args->id);
2953 } while (ev.type != ButtonRelease);
2955 XSync(display, False);
2956 resize_window(win, args->id);
2958 store_float_geom(win,r);
2960 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2962 XUngrabPointer(display, CurrentTime);
2965 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2969 move_window(struct ws_win *win)
2973 struct swm_region *r;
2975 r = root_to_region(win->wa.root);
2976 bzero(&wc, sizeof wc);
2981 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2982 win->id, wc.x, wc.y, wc.width, wc.height);
2984 XConfigureWindow(display, win->id, mask, &wc);
2989 move(struct ws_win *win, union arg *args)
2993 struct swm_region *r = win->ws->r;
2996 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2997 win->id, win->floating, win->transient);
2999 /* in max_stack mode should only move transients */
3000 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3004 if (win->floating == 0 && !win->transient) {
3006 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3009 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3012 /* raise the window = move to last in window list */
3013 a.id = SWM_ARG_ID_MOVELAST;
3017 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3018 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3020 XWarpPointer(display, None, win->id, 0, 0, 0, 0, -1, -1);
3022 XMaskEvent(display, MOUSEMASK | ExposureMask |
3023 SubstructureRedirectMask, &ev);
3025 case ConfigureRequest:
3028 handler[ev.type](&ev);
3031 /* don't allow to move window origin out of region */
3032 if ( ev.xmotion.x_root < r->g.x ||
3033 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3034 ev.xmotion.y_root < r->g.y ||
3035 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3038 win->g.x = ev.xmotion.x_root;
3039 win->g.y = ev.xmotion.y_root;
3041 /* not free, don't sync more than 60 times / second */
3042 if ((ev.xmotion.time - time) > (1000 / 60) ) {
3043 time = ev.xmotion.time;
3044 XSync(display, False);
3049 } while (ev.type != ButtonRelease);
3051 XSync(display, False);
3054 store_float_geom(win,r);
3055 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3056 XUngrabPointer(display, CurrentTime);
3059 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
3062 /* user/key callable function IDs */
3121 /* key definitions */
3123 dummykeyfunc(struct swm_region *r, union arg *args)
3128 legacyfunc(struct swm_region *r, union arg *args)
3133 char name[SWM_FUNCNAME_LEN];
3134 void (*func)(struct swm_region *r, union arg *);
3136 } keyfuncs[kf_invalid + 1] = {
3137 /* name function argument */
3138 { "cycle_layout", cycle_layout, {0} },
3139 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3140 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3141 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3142 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3143 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3144 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3145 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3146 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3147 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3148 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3149 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3150 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3151 { "spawn_term", spawnterm, {.argv = spawn_term} },
3152 { "spawn_menu", legacyfunc, {0} },
3153 { "quit", quit, {0} },
3154 { "restart", restart, {0} },
3155 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3156 { "ws_1", switchws, {.id = 0} },
3157 { "ws_2", switchws, {.id = 1} },
3158 { "ws_3", switchws, {.id = 2} },
3159 { "ws_4", switchws, {.id = 3} },
3160 { "ws_5", switchws, {.id = 4} },
3161 { "ws_6", switchws, {.id = 5} },
3162 { "ws_7", switchws, {.id = 6} },
3163 { "ws_8", switchws, {.id = 7} },
3164 { "ws_9", switchws, {.id = 8} },
3165 { "ws_10", switchws, {.id = 9} },
3166 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3167 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3168 { "ws_prior", priorws, {0} },
3169 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3170 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3171 { "mvws_1", send_to_ws, {.id = 0} },
3172 { "mvws_2", send_to_ws, {.id = 1} },
3173 { "mvws_3", send_to_ws, {.id = 2} },
3174 { "mvws_4", send_to_ws, {.id = 3} },
3175 { "mvws_5", send_to_ws, {.id = 4} },
3176 { "mvws_6", send_to_ws, {.id = 5} },
3177 { "mvws_7", send_to_ws, {.id = 6} },
3178 { "mvws_8", send_to_ws, {.id = 7} },
3179 { "mvws_9", send_to_ws, {.id = 8} },
3180 { "mvws_10", send_to_ws, {.id = 9} },
3181 { "bar_toggle", bar_toggle, {0} },
3182 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3183 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3184 { "screenshot_all", legacyfunc, {0} },
3185 { "screenshot_wind", legacyfunc, {0} },
3186 { "float_toggle", floating_toggle,{0} },
3187 { "version", version, {0} },
3188 { "spawn_lock", legacyfunc, {0} },
3189 { "spawn_initscr", legacyfunc, {0} },
3190 { "spawn_custom", dummykeyfunc, {0} },
3191 { "dumpwins", dumpwins, {0} },
3192 { "invalid key func", NULL, {0} },
3197 enum keyfuncid funcid;
3200 int keys_size = 0, keys_length = 0;
3201 struct key *keys = NULL;
3204 enum { client_click, root_click };
3206 unsigned int action;
3208 unsigned int button;
3209 void (*func)(struct ws_win *, union arg *);
3212 /* action key mouse button func args */
3213 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3214 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3215 { client_click, MODKEY, Button1, move, {0} },
3219 update_modkey(unsigned int mod)
3224 for (i = 0; i < keys_length; i++)
3225 if (keys[i].mod & ShiftMask)
3226 keys[i].mod = mod | ShiftMask;
3230 for (i = 0; i < LENGTH(buttons); i++)
3231 if (buttons[i].mask & ShiftMask)
3232 buttons[i].mask = mod | ShiftMask;
3234 buttons[i].mask = mod;
3244 int spawns_size = 0, spawns_length = 0;
3245 struct spawn_prog *spawns = NULL;
3248 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3251 struct spawn_prog *prog = NULL;
3253 char *ap, **real_args;
3255 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
3258 for (i = 0; i < spawns_length; i++) {
3259 if (!strcasecmp(spawn_name, spawns[i].name))
3263 fprintf(stderr, "spawn_custom: program %s not found\n",
3268 /* make room for expanded args */
3269 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3270 err(1, "spawn_custom: calloc real_args");
3272 /* expand spawn_args into real_args */
3273 for (i = 0; i < prog->argc; i++) {
3275 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3276 if (!strcasecmp(ap, "$bar_border")) {
3278 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3280 err(1, "spawn_custom border color");
3281 } else if (!strcasecmp(ap, "$bar_color")) {
3283 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3285 err(1, "spawn_custom bar color");
3286 } else if (!strcasecmp(ap, "$bar_font")) {
3287 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3289 err(1, "spawn_custom bar fonts");
3290 } else if (!strcasecmp(ap, "$bar_font_color")) {
3292 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3294 err(1, "spawn_custom color font");
3295 } else if (!strcasecmp(ap, "$color_focus")) {
3297 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3299 err(1, "spawn_custom color focus");
3300 } else if (!strcasecmp(ap, "$color_unfocus")) {
3302 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3304 err(1, "spawn_custom color unfocus");
3306 /* no match --> copy as is */
3307 if ((real_args[i] = strdup(ap)) == NULL)
3308 err(1, "spawn_custom strdup(ap)");
3310 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3315 if ((swm_debug & SWM_D_SPAWN) != 0) {
3316 fprintf(stderr, "spawn_custom: result = ");
3317 for (i = 0; i < prog->argc; i++)
3318 fprintf(stderr, "\"%s\" ", real_args[i]);
3319 fprintf(stderr, "\n");
3325 for (i = 0; i < prog->argc; i++)
3331 setspawn(struct spawn_prog *prog)
3335 if (prog == NULL || prog->name == NULL)
3339 for (i = 0; i < spawns_length; i++) {
3340 if (!strcmp(spawns[i].name, prog->name)) {
3342 if (prog->argv == NULL) {
3344 DNPRINTF(SWM_D_SPAWN,
3345 "setspawn: delete #%d %s\n",
3347 free(spawns[i].name);
3348 for (j = 0; j < spawns[i].argc; j++)
3349 free(spawns[i].argv[j]);
3350 free(spawns[i].argv);
3351 j = spawns_length - 1;
3353 spawns[i] = spawns[j];
3358 DNPRINTF(SWM_D_SPAWN,
3359 "setspawn: replace #%d %s\n",
3361 free(spawns[i].name);
3362 for (j = 0; j < spawns[i].argc; j++)
3363 free(spawns[i].argv[j]);
3364 free(spawns[i].argv);
3367 /* found case handled */
3373 if (prog->argv == NULL) {
3375 "error: setspawn: cannot find program %s", prog->name);
3380 /* not found: add */
3381 if (spawns_size == 0 || spawns == NULL) {
3383 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3384 spawns = malloc((size_t)spawns_size *
3385 sizeof(struct spawn_prog));
3386 if (spawns == NULL) {
3387 fprintf(stderr, "setspawn: malloc failed\n");
3391 } else if (spawns_length == spawns_size) {
3393 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3394 spawns = realloc(spawns, (size_t)spawns_size *
3395 sizeof(struct spawn_prog));
3396 if (spawns == NULL) {
3397 fprintf(stderr, "setspawn: realloc failed\n");
3403 if (spawns_length < spawns_size) {
3404 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3405 spawns_length, prog->name);
3406 i = spawns_length++;
3409 fprintf(stderr, "spawns array problem?\n");
3410 if (spawns == NULL) {
3411 fprintf(stderr, "spawns array is NULL!\n");
3419 setconfspawn(char *selector, char *value, int flags)
3421 struct spawn_prog *prog;
3422 char *vp, *cp, *word;
3424 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3425 if ((prog = calloc(1, sizeof *prog)) == NULL)
3426 err(1, "setconfspawn: calloc prog");
3427 prog->name = strdup(selector);
3428 if (prog->name == NULL)
3429 err(1, "setconfspawn prog->name");
3430 if ((cp = vp = strdup(value)) == NULL)
3431 err(1, "setconfspawn: strdup(value) ");
3432 while ((word = strsep(&cp, " \t")) != NULL) {
3433 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3435 cp += (long)strspn(cp, " \t");
3436 if (strlen(word) > 0) {
3438 prog->argv = realloc(prog->argv,
3439 prog->argc * sizeof(char *));
3440 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3441 err(1, "setconfspawn: strdup");
3448 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3455 setconfspawn("term", "xterm", 0);
3456 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3457 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3458 setconfspawn("lock", "xlock", 0);
3459 setconfspawn("initscr", "initscreen.sh", 0);
3460 setconfspawn("menu", "dmenu_run"
3463 " -nf $bar_font_color"
3465 " -sf $bar_color", 0);
3469 #define SWM_MODNAME_SIZE 32
3470 #define SWM_KEY_WS "\n+ \t"
3472 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3476 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3477 if (mod == NULL || ks == NULL) {
3478 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3481 if (keystr == NULL || strlen(keystr) == 0) {
3482 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3488 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3489 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3491 cp += (long)strspn(cp, SWM_KEY_WS);
3492 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3494 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3496 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3498 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3500 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3502 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3504 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3505 *mod |= ControlMask;
3507 *ks = XStringToKeysym(name);
3508 XConvertCase(*ks, ks, &uks);
3509 if (ks == NoSymbol) {
3511 "parsekeys: invalid key %s\n",
3517 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3522 strdupsafe(char *str)
3527 return (strdup(str));
3531 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3534 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3535 keyfuncs[kfid].name, spawn_name);
3537 for (i = 0; i < keys_length; i++) {
3538 if (keys[i].mod == mod && keys[i].keysym == ks) {
3539 if (kfid == kf_invalid) {
3542 "setkeybinding: delete #%d %s\n",
3543 i, keyfuncs[keys[i].funcid].name);
3544 free(keys[i].spawn_name);
3545 j = keys_length - 1;
3549 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3552 /* found: replace */
3554 "setkeybinding: replace #%d %s %s\n",
3555 i, keyfuncs[keys[i].funcid].name,
3557 free(keys[i].spawn_name);
3559 keys[i].keysym = ks;
3560 keys[i].funcid = kfid;
3561 keys[i].spawn_name = strdupsafe(spawn_name);
3562 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3567 if (kfid == kf_invalid) {
3569 "error: setkeybinding: cannot find mod/key combination");
3570 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3573 /* not found: add */
3574 if (keys_size == 0 || keys == NULL) {
3576 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3577 keys = malloc((size_t)keys_size * sizeof(struct key));
3579 fprintf(stderr, "malloc failed\n");
3583 } else if (keys_length == keys_size) {
3585 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3586 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3588 fprintf(stderr, "realloc failed\n");
3593 if (keys_length < keys_size) {
3595 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3596 j, keyfuncs[kfid].name, spawn_name);
3598 keys[j].keysym = ks;
3599 keys[j].funcid = kfid;
3600 keys[j].spawn_name = strdupsafe(spawn_name);
3602 fprintf(stderr, "keys array problem?\n");
3604 fprintf(stderr, "keys array problem\n");
3608 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3612 setconfbinding(char *selector, char *value, int flags)
3614 enum keyfuncid kfid;
3618 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3619 if (selector == NULL) {
3620 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3621 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3623 setkeybinding(mod, ks, kfid, NULL);
3628 /* search by key function name */
3629 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3630 if (strncasecmp(selector, keyfuncs[kfid].name,
3631 SWM_FUNCNAME_LEN) == 0) {
3632 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3634 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3635 setkeybinding(mod, ks, kfid, NULL);
3641 /* search by custom spawn name */
3642 for (i = 0; i < spawns_length; i++) {
3643 if (strcasecmp(selector, spawns[i].name) == 0) {
3644 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3646 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3647 setkeybinding(mod, ks, kf_spawn_custom,
3654 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3661 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3662 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3663 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3664 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3665 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3666 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3667 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3668 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3669 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3670 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3671 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3672 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3673 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3674 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3675 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3676 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3677 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3678 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3679 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3680 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3681 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3682 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3683 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3684 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3685 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3686 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3687 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3688 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3689 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3690 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3691 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3692 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3693 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3694 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3695 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3696 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3697 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3698 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3699 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3700 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3701 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3702 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3703 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3704 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3705 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3706 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3707 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3708 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3709 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3710 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3711 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3712 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3713 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3714 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3716 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3721 updatenumlockmask(void)
3724 XModifierKeymap *modmap;
3726 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3728 modmap = XGetModifierMapping(display);
3729 for (i = 0; i < 8; i++)
3730 for (j = 0; j < modmap->max_keypermod; j++)
3731 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3732 == XKeysymToKeycode(display, XK_Num_Lock))
3733 numlockmask = (1 << i);
3735 XFreeModifiermap(modmap);
3741 unsigned int i, j, k;
3743 unsigned int modifiers[] =
3744 { 0, LockMask, numlockmask, numlockmask | LockMask };
3746 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3747 updatenumlockmask();
3749 for (k = 0; k < ScreenCount(display); k++) {
3750 if (TAILQ_EMPTY(&screens[k].rl))
3752 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3753 for (i = 0; i < keys_length; i++) {
3754 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3755 for (j = 0; j < LENGTH(modifiers); j++)
3756 XGrabKey(display, code,
3757 keys[i].mod | modifiers[j],
3758 screens[k].root, True,
3759 GrabModeAsync, GrabModeAsync);
3765 grabbuttons(struct ws_win *win, int focused)
3768 unsigned int modifiers[] =
3769 { 0, LockMask, numlockmask, numlockmask|LockMask };
3771 updatenumlockmask();
3772 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3774 for (i = 0; i < LENGTH(buttons); i++)
3775 if (buttons[i].action == client_click)
3776 for (j = 0; j < LENGTH(modifiers); j++)
3777 XGrabButton(display, buttons[i].button,
3778 buttons[i].mask | modifiers[j],
3779 win->id, False, BUTTONMASK,
3780 GrabModeAsync, GrabModeSync, None,
3783 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3784 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3787 const char *quirkname[] = {
3788 "NONE", /* config string for "no value" */
3796 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3797 #define SWM_Q_WS "\n|+ \t"
3799 parsequirks(char *qstr, unsigned long *quirk)
3809 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3811 cp += (long)strspn(cp, SWM_Q_WS);
3812 for (i = 0; i < LENGTH(quirkname); i++) {
3813 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3814 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3819 *quirk |= 1 << (i-1);
3823 if (i >= LENGTH(quirkname)) {
3824 DNPRINTF(SWM_D_QUIRK,
3825 "parsequirks: invalid quirk [%s]\n", name);
3833 setquirk(const char *class, const char *name, const int quirk)
3838 for (i = 0; i < quirks_length; i++) {
3839 if (!strcmp(quirks[i].class, class) &&
3840 !strcmp(quirks[i].name, name)) {
3843 DNPRINTF(SWM_D_QUIRK,
3844 "setquirk: delete #%d %s:%s\n",
3845 i, quirks[i].class, quirks[i].name);
3846 free(quirks[i].class);
3847 free(quirks[i].name);
3848 j = quirks_length - 1;
3850 quirks[i] = quirks[j];
3854 /* found: replace */
3855 DNPRINTF(SWM_D_QUIRK,
3856 "setquirk: replace #%d %s:%s\n",
3857 i, quirks[i].class, quirks[i].name);
3858 free(quirks[i].class);
3859 free(quirks[i].name);
3860 quirks[i].class = strdup(class);
3861 quirks[i].name = strdup(name);
3862 quirks[i].quirk = quirk;
3869 "error: setquirk: cannot find class/name combination");
3872 /* not found: add */
3873 if (quirks_size == 0 || quirks == NULL) {
3875 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3876 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3878 fprintf(stderr, "setquirk: malloc failed\n");
3882 } else if (quirks_length == quirks_size) {
3884 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3885 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3887 fprintf(stderr, "setquirk: realloc failed\n");
3892 if (quirks_length < quirks_size) {
3893 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3894 j = quirks_length++;
3895 quirks[j].class = strdup(class);
3896 quirks[j].name = strdup(name);
3897 quirks[j].quirk = quirk;
3899 fprintf(stderr, "quirks array problem?\n");
3901 fprintf(stderr, "quirks array problem!\n");
3908 setconfquirk(char *selector, char *value, int flags)
3910 char *cp, *class, *name;
3912 unsigned long quirks;
3913 if (selector == NULL)
3915 if ((cp = strchr(selector, ':')) == NULL)
3920 if ((retval = parsequirks(value, &quirks)) == 0)
3921 setquirk(class, name, quirks);
3928 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3929 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3930 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3931 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3932 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3933 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3934 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3935 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3936 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3937 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3938 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3939 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3942 /* conf file stuff */
3943 #define SWM_CONF_FILE "scrotwm.conf"
3945 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3946 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3947 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3948 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3949 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BAR_FONT,
3950 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
3955 setconfvalue(char *selector, char *value, int flags)
3958 case SWM_S_BAR_DELAY:
3959 bar_delay = atoi(value);
3961 case SWM_S_BAR_ENABLED:
3962 bar_enabled = atoi(value);
3964 case SWM_S_BAR_AT_BOTTOM:
3965 bar_at_bottom = atoi(value);
3967 case SWM_S_STACK_ENABLED:
3968 stack_enabled = atoi(value);
3970 case SWM_S_CLOCK_ENABLED:
3971 clock_enabled = atoi(value);
3973 case SWM_S_CLOCK_FORMAT:
3974 #ifndef SWM_DENY_CLOCK_FORMAT
3976 if ((clock_format = strdup(value)) == NULL)
3977 err(1, "setconfvalue: clock_format");
3980 case SWM_S_CYCLE_EMPTY:
3981 cycle_empty = atoi(value);
3983 case SWM_S_CYCLE_VISIBLE:
3984 cycle_visible = atoi(value);
3986 case SWM_S_SS_ENABLED:
3987 ss_enabled = atoi(value);
3989 case SWM_S_TERM_WIDTH:
3990 term_width = atoi(value);
3992 case SWM_S_TITLE_CLASS_ENABLED:
3993 title_class_enabled = atoi(value);
3995 case SWM_S_TITLE_NAME_ENABLED:
3996 title_name_enabled = atoi(value);
3998 case SWM_S_FOCUS_MODE:
3999 if (!strcmp(value, "default"))
4000 focus_mode = SWM_FOCUS_DEFAULT;
4001 else if (!strcmp(value, "follow_cursor"))
4002 focus_mode = SWM_FOCUS_FOLLOW;
4003 else if (!strcmp(value, "synergy"))
4004 focus_mode = SWM_FOCUS_SYNERGY;
4006 err(1, "focus_mode");
4008 case SWM_S_DISABLE_BORDER:
4009 disable_border = atoi(value);
4011 case SWM_S_BAR_FONT:
4013 if ((bar_fonts[0] = strdup(value)) == NULL)
4014 err(1, "setconfvalue: bar_font");
4016 case SWM_S_BAR_ACTION:
4018 if ((bar_argv[0] = strdup(value)) == NULL)
4019 err(1, "setconfvalue: bar_action");
4021 case SWM_S_SPAWN_TERM:
4022 free(spawn_term[0]);
4023 if ((spawn_term[0] = strdup(value)) == NULL)
4024 err(1, "setconfvalue: spawn_term");
4028 case SWM_S_DIALOG_RATIO:
4029 dialog_ratio = atof(value);
4030 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4040 setconfmodkey(char *selector, char *value, int flags)
4042 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4043 update_modkey(Mod1Mask);
4044 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4045 update_modkey(Mod2Mask);
4046 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4047 update_modkey(Mod3Mask);
4048 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4049 update_modkey(Mod4Mask);
4056 setconfcolor(char *selector, char *value, int flags)
4058 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4063 setconfregion(char *selector, char *value, int flags)
4065 custom_region(value);
4069 /* config options */
4070 struct config_option {
4072 int (*func)(char*, char*, int);
4075 struct config_option configopt[] = {
4076 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4077 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4078 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4079 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4080 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4081 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4082 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4083 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4084 { "bind", setconfbinding, 0 },
4085 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4086 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4087 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4088 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4089 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4090 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4091 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4092 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4093 { "modkey", setconfmodkey, 0 },
4094 { "program", setconfspawn, 0 },
4095 { "quirk", setconfquirk, 0 },
4096 { "region", setconfregion, 0 },
4097 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4098 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4099 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4100 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4101 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4102 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4103 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4104 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4109 conf_load(char *filename)
4112 char *line, *cp, *optsub, *optval;
4113 size_t linelen, lineno = 0;
4114 int wordlen, i, optind;
4115 struct config_option *opt;
4117 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4119 if (filename == NULL) {
4120 fprintf(stderr, "conf_load: no filename\n");
4123 if ((config = fopen(filename, "r")) == NULL) {
4124 warn("conf_load: fopen");
4128 while (!feof(config)) {
4129 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4132 err(1, "%s", filename);
4137 cp += strspn(cp, " \t\n"); /* eat whitespace */
4138 if (cp[0] == '\0') {
4143 /* get config option */
4144 wordlen = strcspn(cp, "=[ \t\n");
4146 warnx("%s: line %zd: no option found",
4151 for (i = 0; i < LENGTH(configopt); i++) {
4152 opt = &configopt[i];
4153 if (!strncasecmp(cp, opt->optname, wordlen) &&
4154 strlen(opt->optname) == wordlen) {
4160 warnx("%s: line %zd: unknown option %.*s",
4161 filename, lineno, wordlen, cp);
4165 cp += strspn(cp, " \t\n"); /* eat whitespace */
4166 /* get [selector] if any */
4170 wordlen = strcspn(cp, "]");
4173 warnx("%s: line %zd: syntax error",
4177 asprintf(&optsub, "%.*s", wordlen, cp);
4180 cp += strspn(cp, "] \t\n"); /* eat trailing */
4182 cp += strspn(cp, "= \t\n"); /* eat trailing */
4184 optval = strdup(cp);
4185 /* call function to deal with it all */
4186 if (configopt[optind].func(optsub, optval,
4187 configopt[optind].funcflags) != 0) {
4188 fprintf(stderr, "%s line %zd: %s\n",
4189 filename, lineno, line);
4190 errx(1, "%s: line %zd: invalid data for %s",
4191 filename, lineno, configopt[optind].optname);
4199 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4205 set_child_transient(struct ws_win *win)
4207 struct ws_win *parent;
4209 parent = find_window(win->transient);
4211 parent->child_trans = win;
4215 manage_window(Window id)
4218 struct workspace *ws;
4219 struct ws_win *win, *ww;
4220 int format, i, ws_idx, n, border_me = 0;
4221 unsigned long nitems, bytes;
4222 Atom ws_idx_atom = 0, type;
4223 Atom *prot = NULL, *pp;
4224 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4225 struct swm_region *r;
4230 if ((win = find_window(id)) != NULL)
4231 return (win); /* already being managed */
4233 /* see if we are on the unmanaged list */
4234 if ((win = find_unmanaged_window(id)) != NULL) {
4235 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4237 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4238 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4240 set_child_transient(win);
4241 ewmh_update_actions(win);
4245 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4246 errx(1, "calloc: failed to allocate memory for new window");
4248 /* Get all the window data in one shot */
4249 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4251 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4252 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4253 XGetWindowAttributes(display, id, &win->wa);
4254 XGetWMNormalHints(display, id, &win->sh, &mask);
4255 XGetTransientForHint(display, id, &trans);
4257 win->transient = trans;
4258 set_child_transient(win);
4259 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4260 win->id, win->transient);
4263 /* get supported protocols */
4264 if (XGetWMProtocols(display, id, &prot, &n)) {
4265 for (i = 0, pp = prot; i < n; i++, pp++) {
4266 if (*pp == takefocus)
4267 win->take_focus = 1;
4269 win->can_delete = 1;
4276 * Figure out where to put the window. If it was previously assigned to
4277 * a workspace (either by spawn() or manually moving), and isn't
4278 * transient, * put it in the same workspace
4280 r = root_to_region(win->wa.root);
4281 if (prop && win->transient == 0) {
4282 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4283 ws_idx = strtonum(prop, 0, 9, &errstr);
4285 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4288 ws = &r->s->ws[ws_idx];
4291 /* this should launch transients in the same ws as parent */
4293 if ((ww = find_window(trans)) != NULL)
4300 "fix this bug mcbride\n");
4305 /* set up the window layout */
4308 win->s = r->s; /* this never changes */
4309 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4311 win->g.w = win->wa.width;
4312 win->g.h = win->wa.height;
4313 win->g.x = win->wa.x;
4314 win->g.y = win->wa.y;
4315 win->g_floatvalid = 0;
4316 win->floatmaxed = 0;
4317 win->ewmh_flags = 0;
4319 /* Set window properties so we can remember this after reincarnation */
4320 if (ws_idx_atom && prop == NULL &&
4321 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4322 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4324 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4325 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4330 ewmh_autoquirk(win);
4332 if (XGetClassHint(display, win->id, &win->ch)) {
4333 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4334 win->ch.res_class, win->ch.res_name);
4336 /* java is retarded so treat it special */
4337 if (strstr(win->ch.res_name, "sun-awt")) {
4342 for (i = 0; i < quirks_length; i++){
4343 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4344 !strcmp(win->ch.res_name, quirks[i].name)) {
4345 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4346 win->ch.res_class, win->ch.res_name);
4347 if (quirks[i].quirk & SWM_Q_FLOAT) {
4351 win->quirks = quirks[i].quirk;
4356 /* alter window position if quirky */
4357 if (win->quirks & SWM_Q_ANYWHERE) {
4358 win->manual = 1; /* don't center the quirky windows */
4359 bzero(&wc, sizeof wc);
4361 if (bar_enabled && win->g.y < bar_height) {
4362 win->g.y = wc.y = bar_height;
4365 if (win->g.w + win->g.x > WIDTH(r)) {
4366 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4372 /* Reset font sizes (the bruteforce way; no default keybinding). */
4373 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4374 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4375 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4376 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4377 fake_keypress(win, XK_KP_Add, ShiftMask);
4380 ewmh_get_win_state(win);
4381 ewmh_update_actions(win);
4382 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4386 bzero(&wc, sizeof wc);
4387 wc.border_width = 1;
4388 mask = CWBorderWidth;
4389 XConfigureWindow(display, win->id, mask, &wc);
4393 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4394 PropertyChangeMask | StructureNotifyMask);
4396 set_win_state(win, NormalState);
4398 /* floaters need to be mapped if they are in the current workspace */
4399 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4400 XMapRaised(display, win->id);
4406 free_window(struct ws_win *win)
4408 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4413 /* needed for restart wm */
4414 set_win_state(win, WithdrawnState);
4416 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4418 if (win->ch.res_class)
4419 XFree(win->ch.res_class);
4420 if (win->ch.res_name)
4421 XFree(win->ch.res_name);
4426 memset(win, 0xff, sizeof *win); /* XXX kill later */
4432 unmanage_window(struct ws_win *win)
4434 struct ws_win *parent;
4439 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4441 if (win->transient) {
4442 parent = find_window(win->transient);
4444 parent->child_trans = NULL;
4447 /* focus on root just in case */
4448 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4453 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4454 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4460 focus_magic(struct ws_win *win, int do_trans)
4462 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4467 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4468 /* win = parent & has a transient so focus on that */
4470 focus_win(win->child_trans);
4471 if (win->child_trans->take_focus)
4472 client_msg(win, takefocus);
4474 /* make sure transient hasn't dissapeared */
4475 if (validate_win(win->child_trans) == 0) {
4476 focus_win(win->child_trans);
4477 if (win->child_trans->take_focus)
4478 client_msg(win->child_trans, takefocus);
4480 win->child_trans = NULL;
4482 if (win->take_focus)
4483 client_msg(win, takefocus);
4489 if (win->take_focus)
4490 client_msg(win, takefocus);
4497 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4505 XKeyEvent *ev = &e->xkey;
4507 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4509 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4510 for (i = 0; i < keys_length; i++)
4511 if (keysym == keys[i].keysym
4512 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4513 && keyfuncs[keys[i].funcid].func) {
4514 if (keys[i].funcid == kf_spawn_custom)
4516 root_to_region(ev->root),
4517 &(keyfuncs[keys[i].funcid].args),
4521 keyfuncs[keys[i].funcid].func(
4522 root_to_region(ev->root),
4523 &(keyfuncs[keys[i].funcid].args)
4529 buttonpress(XEvent *e)
4533 XButtonPressedEvent *ev = &e->xbutton;
4535 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4537 action = root_click;
4538 if ((win = find_window(ev->window)) == NULL)
4541 focus_magic(win, SWM_F_TRANSIENT);
4542 action = client_click;
4544 for (i = 0; i < LENGTH(buttons); i++)
4545 if (action == buttons[i].action && buttons[i].func &&
4546 buttons[i].button == ev->button &&
4547 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4548 buttons[i].func(win, &buttons[i].args);
4552 configurerequest(XEvent *e)
4554 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4559 if ((win = find_window(ev->window)) == NULL)
4560 if ((win = find_unmanaged_window(ev->window)) == NULL)
4564 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4566 bzero(&wc, sizeof wc);
4569 wc.width = ev->width;
4570 wc.height = ev->height;
4571 wc.border_width = ev->border_width;
4572 wc.sibling = ev->above;
4573 wc.stack_mode = ev->detail;
4574 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4576 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4578 if (win->floating) {
4579 if (ev->value_mask & CWX)
4581 if (ev->value_mask & CWY)
4583 if (ev->value_mask & CWWidth)
4584 win->g.w = ev->width;
4585 if (ev->value_mask & CWHeight)
4586 win->g.h = ev->height;
4593 configurenotify(XEvent *e)
4598 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4599 e->xconfigure.window);
4601 win = find_window(e->xconfigure.window);
4603 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4611 destroynotify(XEvent *e)
4614 XDestroyWindowEvent *ev = &e->xdestroywindow;
4616 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4618 if ((win = find_window(ev->window)) == NULL) {
4619 if ((win = find_unmanaged_window(ev->window)) == NULL)
4625 /* make sure we focus on something */
4628 unmanage_window(win);
4634 enternotify(XEvent *e)
4636 XCrossingEvent *ev = &e->xcrossing;
4641 Window focus_return;
4642 int revert_to_return;
4644 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4645 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4646 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4647 ev->same_screen, ev->focus, ev->state);
4649 switch (focus_mode) {
4650 case SWM_FOCUS_DEFAULT:
4651 if (QLength(display)) {
4652 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4657 case SWM_FOCUS_FOLLOW:
4659 case SWM_FOCUS_SYNERGY:
4662 * all these checks need to be in this order because the
4663 * XCheckTypedWindowEvent relies on weeding out the previous events
4665 * making this code an option would enable a follow mouse for focus
4670 * state is set when we are switching workspaces and focus is set when
4671 * the window or a subwindow already has focus (occurs during restart).
4673 * Only honor the focus flag if last_focus_event is not FocusOut,
4674 * this allows scrotwm to continue to control focus when another
4675 * program is also playing with it.
4677 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4678 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4683 * happens when a window is created or destroyed and the border
4684 * crosses the mouse pointer and when switching ws
4686 * we need the subwindow test to see if we came from root in order
4687 * to give focus to floaters
4689 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4690 ev->subwindow == 0) {
4691 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4695 /* this window already has focus */
4696 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4697 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4701 /* this window is being deleted or moved to another ws */
4702 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4704 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4705 XPutBackEvent(display, &cne);
4709 if ((win = find_window(ev->window)) == NULL) {
4710 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4715 * In fullstack kill all enters unless they come from a different ws
4716 * (i.e. another region) or focus has been grabbed externally.
4718 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4719 last_focus_event != FocusOut) {
4720 XGetInputFocus(display, &focus_return, &revert_to_return);
4721 if ((w = find_window(focus_return)) == NULL ||
4723 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4731 if ((win = find_window(ev->window)) == NULL) {
4732 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4737 * if we have more enternotifies let them handle it in due time
4739 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4740 DNPRINTF(SWM_D_EVENT,
4741 "ignoring enternotify: got more enternotify\n");
4742 XPutBackEvent(display, &cne);
4746 focus_magic(win, SWM_F_TRANSIENT);
4749 /* lets us use one switch statement for arbitrary mode/detail combinations */
4750 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4753 focusevent(XEvent *e)
4757 u_int32_t mode_detail;
4758 XFocusChangeEvent *ev = &e->xfocus;
4760 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4761 ev->type == FocusIn ? "entering" : "leaving",
4762 ev->window, ev->mode, ev->detail);
4764 if (last_focus_event == ev->type) {
4765 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4769 last_focus_event = ev->type;
4770 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4772 switch (mode_detail) {
4773 /* synergy client focus operations */
4774 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4775 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4777 /* synergy server focus operations */
4778 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4780 /* Entering applications like rdesktop that mangle the pointer */
4781 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4783 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4784 XSetWindowBorder(display, win->id,
4785 win->ws->r->s->c[ev->type == FocusIn ?
4786 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4789 fprintf(stderr, "ignoring focusevent\n");
4790 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4797 mapnotify(XEvent *e)
4800 XMapEvent *ev = &e->xmap;
4802 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4804 win = manage_window(ev->window);
4806 set_win_state(win, NormalState);
4810 mappingnotify(XEvent *e)
4812 XMappingEvent *ev = &e->xmapping;
4814 XRefreshKeyboardMapping(ev);
4815 if (ev->request == MappingKeyboard)
4820 maprequest(XEvent *e)
4823 struct swm_region *r;
4824 XWindowAttributes wa;
4825 XMapRequestEvent *ev = &e->xmaprequest;
4827 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4828 e->xmaprequest.window);
4830 if (!XGetWindowAttributes(display, ev->window, &wa))
4832 if (wa.override_redirect)
4835 win = manage_window(e->xmaprequest.window);
4837 return; /* can't happen */
4841 /* make new win focused */
4842 r = root_to_region(win->wa.root);
4843 if (win->ws == r->ws)
4844 focus_magic(win, SWM_F_GENERIC);
4848 propertynotify(XEvent *e)
4851 XPropertyEvent *ev = &e->xproperty;
4853 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4856 if (ev->state == PropertyDelete)
4857 return; /* ignore */
4858 win = find_window(ev->window);
4863 case XA_WM_NORMAL_HINTS:
4866 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4867 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4868 if (win->sh.flags & PMinSize) {
4869 win->g.w = win->sh.min_width;
4870 win->g.h = win->sh.min_height;
4871 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4873 XMoveResizeWindow(display, win->id,
4874 win->g.x, win->g.y, win->g.w, win->g.h);
4883 unmapnotify(XEvent *e)
4887 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4889 /* determine if we need to help unmanage this window */
4890 win = find_window(e->xunmap.window);
4894 if (getstate(e->xunmap.window) == NormalState) {
4895 unmanage_window(win);
4901 visibilitynotify(XEvent *e)
4904 struct swm_region *r;
4906 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4907 e->xvisibility.window);
4908 if (e->xvisibility.state == VisibilityUnobscured)
4909 for (i = 0; i < ScreenCount(display); i++)
4910 TAILQ_FOREACH(r, &screens[i].rl, entry)
4911 if (e->xvisibility.window == r->bar_window)
4916 clientmessage(XEvent *e)
4918 XClientMessageEvent *ev;
4923 win = find_window(ev->window);
4927 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
4928 ev->window, ev->message_type);
4930 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
4931 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
4934 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
4935 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
4936 if (win->can_delete)
4937 client_msg(win, adelete);
4939 XKillClient(display, win->id);
4941 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
4942 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
4943 if (win->floating) {
4944 if (ev->data.l[0] & (1<<8)) /* x */
4945 win->g.x = ev->data.l[1];
4946 if (ev->data.l[0] & (1<<9)) /* y */
4947 win->g.y = ev->data.l[2];
4948 if (ev->data.l[0] & (1<<10)) /* width */
4949 win->g.w = ev->data.l[3];
4950 if (ev->data.l[0] & (1<<11)) /* height */
4951 win->g.h = ev->data.l[4];
4954 /* TODO: Change stack sizes */
4958 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
4959 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
4960 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
4962 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
4969 xerror_start(Display *d, XErrorEvent *ee)
4976 xerror(Display *d, XErrorEvent *ee)
4978 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4986 xerrorxlib = XSetErrorHandler(xerror_start);
4988 /* this causes an error if some other window manager is running */
4989 XSelectInput(display, DefaultRootWindow(display),
4990 SubstructureRedirectMask);
4991 XSync(display, False);
4995 XSetErrorHandler(xerror);
4996 XSync(display, False);
5001 new_region(struct swm_screen *s, int x, int y, int w, int h)
5003 struct swm_region *r, *n;
5004 struct workspace *ws = NULL;
5007 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5008 s->idx, w, h, x, y);
5010 /* remove any conflicting regions */
5011 n = TAILQ_FIRST(&s->rl);
5014 n = TAILQ_NEXT(r, entry);
5015 if (X(r) < (x + w) &&
5016 (X(r) + WIDTH(r)) > x &&
5018 (Y(r) + HEIGHT(r)) > y) {
5019 if (r->ws->r != NULL)
5020 r->ws->old_r = r->ws->r;
5022 XDestroyWindow(display, r->bar_window);
5023 TAILQ_REMOVE(&s->rl, r, entry);
5024 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5028 /* search old regions for one to reuse */
5030 /* size + location match */
5031 TAILQ_FOREACH(r, &s->orl, entry)
5032 if (X(r) == x && Y(r) == y &&
5033 HEIGHT(r) == h && WIDTH(r) == w)
5037 TAILQ_FOREACH(r, &s->orl, entry)
5038 if (HEIGHT(r) == h && WIDTH(r) == w)
5042 TAILQ_REMOVE(&s->orl, r, entry);
5043 /* try to use old region's workspace */
5044 if (r->ws->r == NULL)
5047 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5048 errx(1, "calloc: failed to allocate memory for screen");
5050 /* if we don't have a workspace already, find one */
5052 for (i = 0; i < SWM_WS_MAX; i++)
5053 if (s->ws[i].r == NULL) {
5060 errx(1, "no free workspaces\n");
5071 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5077 #ifdef SWM_XRR_HAS_CRTC
5079 XRRScreenResources *sr;
5082 #endif /* SWM_XRR_HAS_CRTC */
5083 struct swm_region *r;
5086 if (i >= ScreenCount(display))
5087 errx(1, "invalid screen");
5089 /* remove any old regions */
5090 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5091 r->ws->old_r = r->ws->r = NULL;
5092 XDestroyWindow(display, r->bar_window);
5093 TAILQ_REMOVE(&screens[i].rl, r, entry);
5094 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5098 /* map virtual screens onto physical screens */
5099 #ifdef SWM_XRR_HAS_CRTC
5100 if (xrandr_support) {
5101 sr = XRRGetScreenResources(display, screens[i].root);
5103 new_region(&screens[i], 0, 0,
5104 DisplayWidth(display, i),
5105 DisplayHeight(display, i));
5109 for (c = 0, ci = NULL; c < ncrtc; c++) {
5110 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5111 if (ci->noutput == 0)
5114 if (ci != NULL && ci->mode == None)
5115 new_region(&screens[i], 0, 0,
5116 DisplayWidth(display, i),
5117 DisplayHeight(display, i));
5119 new_region(&screens[i],
5120 ci->x, ci->y, ci->width, ci->height);
5123 XRRFreeCrtcInfo(ci);
5124 XRRFreeScreenResources(sr);
5126 #endif /* SWM_XRR_HAS_CRTC */
5128 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5129 DisplayHeight(display, i));
5134 screenchange(XEvent *e) {
5135 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5136 struct swm_region *r;
5139 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5141 if (!XRRUpdateConfiguration(e))
5144 /* silly event doesn't include the screen index */
5145 for (i = 0; i < ScreenCount(display); i++)
5146 if (screens[i].root == xe->root)
5148 if (i >= ScreenCount(display))
5149 errx(1, "screenchange: screen not found\n");
5151 /* brute force for now, just re-enumerate the regions */
5154 /* add bars to all regions */
5155 for (i = 0; i < ScreenCount(display); i++)
5156 TAILQ_FOREACH(r, &screens[i].rl, entry)
5164 Window d1, d2, *wins = NULL;
5165 XWindowAttributes wa;
5170 for (i = 0; i < ScreenCount(display); i++) {
5171 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5174 /* attach windows to a region */
5175 /* normal windows */
5176 for (j = 0; j < no; j++) {
5177 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5178 wa.override_redirect ||
5179 XGetTransientForHint(display, wins[j], &d1))
5182 state = getstate(wins[j]);
5183 manage = state == IconicState;
5184 if (wa.map_state == IsViewable || manage)
5185 manage_window(wins[j]);
5187 /* transient windows */
5188 for (j = 0; j < no; j++) {
5189 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5190 wa.override_redirect)
5193 state = getstate(wins[j]);
5194 manage = state == IconicState;
5195 if (XGetTransientForHint(display, wins[j], &d1) &&
5197 manage_window(wins[j]);
5210 int errorbase, major, minor;
5211 struct workspace *ws;
5214 if ((screens = calloc(ScreenCount(display),
5215 sizeof(struct swm_screen))) == NULL)
5216 errx(1, "calloc: screens");
5218 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5220 /* initial Xrandr setup */
5221 xrandr_support = XRRQueryExtension(display,
5222 &xrandr_eventbase, &errorbase);
5224 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5227 /* map physical screens */
5228 for (i = 0; i < ScreenCount(display); i++) {
5229 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5231 TAILQ_INIT(&screens[i].rl);
5232 TAILQ_INIT(&screens[i].orl);
5233 screens[i].root = RootWindow(display, i);
5235 /* set default colors */
5236 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5237 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5238 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5239 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5240 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5242 /* set default cursor */
5243 XDefineCursor(display, screens[i].root,
5244 XCreateFontCursor(display, XC_left_ptr));
5246 /* init all workspaces */
5247 /* XXX these should be dynamically allocated too */
5248 for (j = 0; j < SWM_WS_MAX; j++) {
5249 ws = &screens[i].ws[j];
5254 TAILQ_INIT(&ws->winlist);
5255 TAILQ_INIT(&ws->unmanagedlist);
5257 for (k = 0; layouts[k].l_stack != NULL; k++)
5258 if (layouts[k].l_config != NULL)
5259 layouts[k].l_config(ws,
5260 SWM_ARG_ID_STACKINIT);
5261 ws->cur_layout = &layouts[0];
5267 XRRSelectInput(display, screens[i].root,
5268 RRScreenChangeNotifyMask);
5275 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5277 err(1, "setup_globals: strdup");
5278 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5280 err(1, "setup_globals: strdup");
5281 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5283 err(1, "setup_globals: strdup");
5284 if ((spawn_term[0] = strdup("xterm")) == NULL)
5285 err(1, "setup_globals: strdup");
5286 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5294 Atom netwmcheck, netwmname, utf8_string;
5297 /* work around sun jdk bugs, code from wmname */
5298 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5299 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5300 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5301 for (i = 0; i < ScreenCount(display); i++) {
5302 root = screens[i].root;
5303 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5304 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5305 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5307 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5308 PropModeReplace, (unsigned char *)&win,1);
5309 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5310 PropModeReplace, (unsigned char *)&win,1);
5311 XChangeProperty(display, win, netwmname, utf8_string, 8,
5312 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5317 main(int argc, char *argv[])
5320 struct swm_region *r, *rr;
5321 struct ws_win *winfocus = NULL;
5324 char conf[PATH_MAX], *cfile = NULL;
5329 struct sigaction sact;
5332 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5333 SWM_VERSION, cvstag);
5334 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5335 warnx("no locale support");
5337 if (!(display = XOpenDisplay(0)))
5338 errx(1, "can not open display");
5341 errx(1, "other wm running");
5343 /* handle some signals */
5344 bzero(&sact, sizeof(sact));
5345 sigemptyset(&sact.sa_mask);
5347 sact.sa_handler = sighdlr;
5348 sigaction(SIGINT, &sact, NULL);
5349 sigaction(SIGQUIT, &sact, NULL);
5350 sigaction(SIGTERM, &sact, NULL);
5351 sigaction(SIGHUP, &sact, NULL);
5353 sact.sa_handler = sighdlr;
5354 sact.sa_flags = SA_NOCLDSTOP;
5355 sigaction(SIGCHLD, &sact, NULL);
5357 astate = XInternAtom(display, "WM_STATE", False);
5358 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5359 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5360 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5362 /* look for local and global conf file */
5363 pwd = getpwuid(getuid());
5365 errx(1, "invalid user %d", getuid());
5374 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5375 if (stat(conf, &sb) != -1) {
5376 if (S_ISREG(sb.st_mode))
5379 /* try global conf file */
5380 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5381 if (!stat(conf, &sb))
5382 if (S_ISREG(sb.st_mode))
5389 /* set some values to work around bad programs */
5392 /* grab existing windows (before we build the bars) */
5395 /* setup all bars */
5396 for (i = 0; i < ScreenCount(display); i++)
5397 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5398 if (winfocus == NULL)
5399 winfocus = TAILQ_FIRST(&r->ws->winlist);
5408 xfd = ConnectionNumber(display);
5410 while (XPending(display)) {
5411 XNextEvent(display, &e);
5414 if (e.type < LASTEvent) {
5416 if (handler[e.type])
5417 handler[e.type](&e);
5419 DNPRINTF(SWM_D_EVENT,
5420 "win: %lu unknown event: %d\n",
5421 e.xany.window, e.type);
5423 switch (e.type - xrandr_eventbase) {
5424 case RRScreenChangeNotify:
5428 DNPRINTF(SWM_D_EVENT,
5429 "win: %lu unknown xrandr event: "
5430 "%d\n", e.xany.window, e.type);
5436 /* if we are being restarted go focus on first window */
5438 rr = winfocus->ws->r;
5440 /* not a visible window */
5444 /* move pointer to first screen if multi screen */
5445 if (ScreenCount(display) > 1 || outputs > 1)
5446 XWarpPointer(display, None, rr->s[0].root,
5447 0, 0, 0, 0, rr->g.x,
5448 rr->g.y + (bar_enabled ? bar_height : 0));
5450 a.id = SWM_ARG_ID_FOCUSCUR;
5460 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5462 DNPRINTF(SWM_D_MISC, "select failed");
5463 if (restart_wm == 1)
5464 restart(NULL, NULL);
5475 XFreeGC(display, bar_gc);
5476 XCloseDisplay(display);