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 window_name_enabled = 0;
212 int focus_mode = SWM_FOCUS_DEFAULT;
213 int disable_border = 0;
219 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
220 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
222 #define SWM_MENU_FN (2)
223 #define SWM_MENU_NB (4)
224 #define SWM_MENU_NF (6)
225 #define SWM_MENU_SB (8)
226 #define SWM_MENU_SF (10)
228 /* layout manager data */
229 struct swm_geometry {
239 /* virtual "screens" */
241 TAILQ_ENTRY(swm_region) entry;
242 struct swm_geometry g;
243 struct workspace *ws; /* current workspace on this region */
244 struct workspace *ws_prior; /* prior workspace on this region */
245 struct swm_screen *s; /* screen idx */
248 TAILQ_HEAD(swm_region_list, swm_region);
251 TAILQ_ENTRY(ws_win) entry;
254 struct ws_win *child_trans; /* transient child window */
255 struct swm_geometry g; /* current geometry */
256 struct swm_geometry g_float; /* geometry when floating */
257 struct swm_geometry rg_float; /* region geom when floating */
258 int g_floatvalid; /* flag: geometry in g_float is valid */
259 int floatmaxed; /* flag: floater was maxed in max_stack */
262 unsigned int ewmh_flags;
263 int font_size_boundary[SWM_MAX_FONT_STEPS];
269 unsigned long quirks;
270 struct workspace *ws; /* always valid */
271 struct swm_screen *s; /* always valid, never changes */
272 XWindowAttributes wa;
276 TAILQ_HEAD(ws_win_list, ws_win);
278 /* layout handlers */
280 void vertical_config(struct workspace *, int);
281 void vertical_stack(struct workspace *, struct swm_geometry *);
282 void horizontal_config(struct workspace *, int);
283 void horizontal_stack(struct workspace *, struct swm_geometry *);
284 void max_stack(struct workspace *, struct swm_geometry *);
286 struct ws_win *find_window(Window);
288 void grabbuttons(struct ws_win *, int);
289 void new_region(struct swm_screen *, int, int, int, int);
290 void unmanage_window(struct ws_win *);
291 long getstate(Window);
294 void (*l_stack)(struct workspace *, struct swm_geometry *);
295 void (*l_config)(struct workspace *, int);
297 #define SWM_L_FOCUSPREV (1<<0)
298 #define SWM_L_MAPONFOCUS (1<<1)
301 /* stack, configure */
302 { vertical_stack, vertical_config, 0, "[|]" },
303 { horizontal_stack, horizontal_config, 0, "[-]" },
305 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
306 { NULL, NULL, 0, NULL },
309 /* position of max_stack mode in the layouts array */
310 #define SWM_MAX_STACK 2
312 #define SWM_H_SLICE (32)
313 #define SWM_V_SLICE (32)
315 /* define work spaces */
317 int idx; /* workspace index */
318 struct layout *cur_layout; /* current layout handlers */
319 struct ws_win *focus; /* may be NULL */
320 struct ws_win *focus_prev; /* may be NULL */
321 struct swm_region *r; /* may be NULL */
322 struct swm_region *old_r; /* may be NULL */
323 struct ws_win_list winlist; /* list of windows in ws */
324 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
328 int horizontal_msize;
330 int horizontal_stacks;
337 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
338 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
340 /* physical screen mapping */
341 #define SWM_WS_MAX (10)
343 int idx; /* screen index */
344 struct swm_region_list rl; /* list of regions on this screen */
345 struct swm_region_list orl; /* list of old regions */
347 struct workspace ws[SWM_WS_MAX];
353 } c[SWM_S_COLOR_MAX];
355 struct swm_screen *screens;
358 /* args to functions */
361 #define SWM_ARG_ID_FOCUSNEXT (0)
362 #define SWM_ARG_ID_FOCUSPREV (1)
363 #define SWM_ARG_ID_FOCUSMAIN (2)
364 #define SWM_ARG_ID_FOCUSCUR (4)
365 #define SWM_ARG_ID_SWAPNEXT (10)
366 #define SWM_ARG_ID_SWAPPREV (11)
367 #define SWM_ARG_ID_SWAPMAIN (12)
368 #define SWM_ARG_ID_MOVELAST (13)
369 #define SWM_ARG_ID_MASTERSHRINK (20)
370 #define SWM_ARG_ID_MASTERGROW (21)
371 #define SWM_ARG_ID_MASTERADD (22)
372 #define SWM_ARG_ID_MASTERDEL (23)
373 #define SWM_ARG_ID_STACKRESET (30)
374 #define SWM_ARG_ID_STACKINIT (31)
375 #define SWM_ARG_ID_CYCLEWS_UP (40)
376 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
377 #define SWM_ARG_ID_CYCLESC_UP (42)
378 #define SWM_ARG_ID_CYCLESC_DOWN (43)
379 #define SWM_ARG_ID_STACKINC (50)
380 #define SWM_ARG_ID_STACKDEC (51)
381 #define SWM_ARG_ID_SS_ALL (60)
382 #define SWM_ARG_ID_SS_WINDOW (61)
383 #define SWM_ARG_ID_DONTCENTER (70)
384 #define SWM_ARG_ID_CENTER (71)
385 #define SWM_ARG_ID_KILLWINDOW (80)
386 #define SWM_ARG_ID_DELETEWINDOW (81)
390 void focus(struct swm_region *, union arg *);
391 void focus_magic(struct ws_win *, int);
392 #define SWM_F_GENERIC (0)
393 #define SWM_F_TRANSIENT (1)
399 #define SWM_Q_FLOAT (1<<0) /* float this window */
400 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
401 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
402 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
403 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
405 int quirks_size = 0, quirks_length = 0;
406 struct quirk *quirks = NULL;
409 * Supported EWMH hints should be added to
410 * both the enum and the ewmh array
412 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
413 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
414 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
415 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
416 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
417 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
418 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
419 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
420 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
421 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
427 } ewmh[SWM_EWMH_HINT_MAX] = {
428 /* must be in same order as in the enum */
429 {"_NET_ACTIVE_WINDOW", None},
430 {"_NET_MOVERESIZE_WINDOW", None},
431 {"_NET_CLOSE_WINDOW", None},
432 {"_NET_WM_WINDOW_TYPE", None},
433 {"_NET_WM_WINDOW_TYPE_DOCK", None},
434 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
435 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
436 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
437 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
438 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
439 {"_NET_WM_STATE", None},
440 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
441 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
442 {"_NET_WM_STATE_SKIP_TASKBAR", None},
443 {"_NET_WM_STATE_SKIP_PAGER", None},
444 {"_NET_WM_STATE_HIDDEN", None},
445 {"_NET_WM_STATE_ABOVE", None},
446 {"_SWM_WM_STATE_MANUAL", None},
447 {"_NET_WM_STATE_FULLSCREEN", None},
448 {"_NET_WM_ALLOWED_ACTIONS", None},
449 {"_NET_WM_ACTION_MOVE", None},
450 {"_NET_WM_ACTION_RESIZE", None},
451 {"_NET_WM_ACTION_FULLSCREEN", None},
452 {"_NET_WM_ACTION_CLOSE", None},
455 void store_float_geom(struct ws_win *win, struct swm_region *r);
456 int floating_toggle_win(struct ws_win *win);
459 get_property(Window id, Atom atom, long count, Atom type,
460 unsigned long *n, unsigned char **data)
463 unsigned long tmp, extra;
464 unsigned long *nitems;
467 nitems = n != NULL ? n : &tmp;
468 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
469 &real, &format, nitems, &extra, data);
471 if (status != Success)
485 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
487 for (i = 0; i < LENGTH(ewmh); i++)
488 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
490 for (i = 0; i < ScreenCount(display); i++) {
491 /* Support check window will be created by workaround(). */
493 /* Report supported atoms */
494 XDeleteProperty(display, screens[i].root, sup_list);
495 for (j = 0; j < LENGTH(ewmh); j++)
496 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
497 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
505 unsigned char *data = NULL;
507 Atom sup_check, sup_list;
510 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
511 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
513 for (i = 0; i < ScreenCount(display); i++) {
514 /* Get the support check window and destroy it */
515 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
520 XDestroyWindow(display, id);
521 XDeleteProperty(display, screens[i].root, sup_check);
522 XDeleteProperty(display, screens[i].root, sup_list);
530 ewmh_autoquirk(struct ws_win *win)
533 unsigned long *data = NULL;
537 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
538 XA_ATOM, &n, (unsigned char **)&data);
545 for (i = 0; i < n; i++) {
547 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
549 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
550 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
551 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
553 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
556 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
557 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
559 win->quirks = SWM_Q_FLOAT;
567 #define SWM_EWMH_ACTION_COUNT_MAX (6)
568 #define EWMH_F_FULLSCREEN (1<<0)
569 #define EWMH_F_ABOVE (1<<1)
570 #define EWMH_F_HIDDEN (1<<2)
571 #define EWMH_F_SKIP_PAGER (1<<3)
572 #define EWMH_F_SKIP_TASKBAR (1<<4)
573 #define SWM_F_MANUAL (1<<5)
576 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
578 struct swm_geometry rg;
586 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
592 store_float_geom(win, win->ws->r);
599 if (win->g_floatvalid) {
600 /* refloat at last floating relative position */
601 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
602 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
603 win->g.w = win->g_float.w;
604 win->g.h = win->g_float.h;
612 ewmh_update_actions(struct ws_win *win)
614 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
620 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
623 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
624 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
627 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
628 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
631 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
632 #define _NET_WM_STATE_ADD 1 /* add/set property */
633 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
636 ewmh_update_win_state(struct ws_win *win, long state, long action)
638 unsigned int mask = 0;
639 unsigned int changed = 0;
640 unsigned int orig_flags;
645 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
646 mask = EWMH_F_FULLSCREEN;
647 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
649 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
651 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
652 mask = EWMH_F_SKIP_PAGER;
653 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
654 mask = EWMH_F_SKIP_TASKBAR;
657 orig_flags = win->ewmh_flags;
660 case _NET_WM_STATE_REMOVE:
661 win->ewmh_flags &= ~mask;
663 case _NET_WM_STATE_ADD:
664 win->ewmh_flags |= mask;
666 case _NET_WM_STATE_TOGGLE:
667 win->ewmh_flags ^= mask;
671 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
673 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
675 if (!floating_toggle_win(win))
676 win->ewmh_flags = orig_flags; /* revert */
677 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
679 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
680 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
682 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
683 win->ewmh_flags = orig_flags; /* revert */
685 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
687 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
688 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
689 XA_ATOM, 32, PropModeAppend,
690 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
691 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
692 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
693 XA_ATOM, 32, PropModeAppend,
694 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
695 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
696 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
697 XA_ATOM, 32, PropModeAppend,
698 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
699 if (win->ewmh_flags & EWMH_F_ABOVE)
700 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
701 XA_ATOM, 32, PropModeAppend,
702 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
703 if (win->ewmh_flags & SWM_F_MANUAL)
704 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
705 XA_ATOM, 32, PropModeAppend,
706 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
710 ewmh_get_win_state(struct ws_win *win)
721 win->ewmh_flags |= EWMH_F_ABOVE;
723 win->ewmh_flags |= SWM_F_MANUAL;
725 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
726 &n, (unsigned char **)&states);
731 for (i = 0; i < n; i++)
732 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
752 name = "ButtonPress";
755 name = "ButtonRelease";
758 name = "MotionNotify";
761 name = "EnterNotify";
764 name = "LeaveNotify";
773 name = "KeymapNotify";
779 name = "GraphicsExpose";
784 case VisibilityNotify:
785 name = "VisibilityNotify";
788 name = "CreateNotify";
791 name = "DestroyNotify";
794 name = "UnmapNotify";
803 name = "ReparentNotify";
805 case ConfigureNotify:
806 name = "ConfigureNotify";
808 case ConfigureRequest:
809 name = "ConfigureRequest";
812 name = "GravityNotify";
815 name = "ResizeRequest";
817 case CirculateNotify:
818 name = "CirculateNotify";
820 case CirculateRequest:
821 name = "CirculateRequest";
824 name = "PropertyNotify";
827 name = "SelectionClear";
829 case SelectionRequest:
830 name = "SelectionRequest";
832 case SelectionNotify:
833 name = "SelectionNotify";
836 name = "ColormapNotify";
839 name = "ClientMessage";
842 name = "MappingNotify";
847 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
849 e->xany.window, name, e->type, QLength(display));
851 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
853 e->xany.window, e->type, QLength(display));
857 dumpwins(struct swm_region *r, union arg *args)
861 XWindowAttributes wa;
864 fprintf(stderr, "invalid workspace\n");
868 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
870 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
871 state = getstate(win->id);
872 if (!XGetWindowAttributes(display, win->id, &wa))
873 fprintf(stderr, "window: %lu failed "
874 "XGetWindowAttributes\n", win->id);
875 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
876 win->id, wa.map_state, state);
879 fprintf(stderr, "===== unmanaged window list =====\n");
880 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
881 state = getstate(win->id);
882 if (!XGetWindowAttributes(display, win->id, &wa))
883 fprintf(stderr, "window: %lu failed "
884 "XGetWindowAttributes\n", win->id);
885 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
886 win->id, wa.map_state, state);
889 fprintf(stderr, "=================================\n");
894 dumpwins(struct swm_region *r, union arg *args)
897 #endif /* SWM_DEBUG */
899 void expose(XEvent *);
900 void keypress(XEvent *);
901 void buttonpress(XEvent *);
902 void configurerequest(XEvent *);
903 void configurenotify(XEvent *);
904 void destroynotify(XEvent *);
905 void enternotify(XEvent *);
906 void focusevent(XEvent *);
907 void mapnotify(XEvent *);
908 void mappingnotify(XEvent *);
909 void maprequest(XEvent *);
910 void propertynotify(XEvent *);
911 void unmapnotify(XEvent *);
912 void visibilitynotify(XEvent *);
913 void clientmessage(XEvent *);
915 void (*handler[LASTEvent])(XEvent *) = {
917 [KeyPress] = keypress,
918 [ButtonPress] = buttonpress,
919 [ConfigureRequest] = configurerequest,
920 [ConfigureNotify] = configurenotify,
921 [DestroyNotify] = destroynotify,
922 [EnterNotify] = enternotify,
923 [FocusIn] = focusevent,
924 [FocusOut] = focusevent,
925 [MapNotify] = mapnotify,
926 [MappingNotify] = mappingnotify,
927 [MapRequest] = maprequest,
928 [PropertyNotify] = propertynotify,
929 [UnmapNotify] = unmapnotify,
930 [VisibilityNotify] = visibilitynotify,
931 [ClientMessage] = clientmessage,
937 int saved_errno, status;
944 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
950 warn("sighdlr: waitpid");
951 #endif /* SWM_DEBUG */
956 if (WIFEXITED(status)) {
957 if (WEXITSTATUS(status) != 0)
958 warnx("sighdlr: child exit status: %d",
959 WEXITSTATUS(status));
961 warnx("sighdlr: child is terminated "
963 #endif /* SWM_DEBUG */
981 name_to_color(char *colorname)
985 XColor screen_def, exact_def;
986 unsigned long result = 0;
987 char cname[32] = "#";
989 cmap = DefaultColormap(display, screens[0].idx);
990 status = XAllocNamedColor(display, cmap, colorname,
991 &screen_def, &exact_def);
993 strlcat(cname, colorname + 2, sizeof cname - 1);
994 status = XAllocNamedColor(display, cmap, cname, &screen_def,
998 result = screen_def.pixel;
1000 fprintf(stderr, "color '%s' not found.\n", colorname);
1006 setscreencolor(char *val, int i, int c)
1008 if (i > 0 && i <= ScreenCount(display)) {
1009 screens[i - 1].c[c].color = name_to_color(val);
1010 free(screens[i - 1].c[c].name);
1011 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1013 } else if (i == -1) {
1014 for (i = 0; i < ScreenCount(display); i++) {
1015 screens[i].c[c].color = name_to_color(val);
1016 free(screens[i].c[c].name);
1017 if ((screens[i].c[c].name = strdup(val)) == NULL)
1021 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1022 i, ScreenCount(display));
1026 custom_region(char *val)
1028 unsigned int sidx, x, y, w, h;
1030 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1031 errx(1, "invalid custom region, "
1032 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1033 if (sidx < 1 || sidx > ScreenCount(display))
1034 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1035 sidx, ScreenCount(display));
1039 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1041 if (x < 0 || x > DisplayWidth(display, sidx) ||
1042 y < 0 || y > DisplayHeight(display, sidx) ||
1043 w + x > DisplayWidth(display, sidx) ||
1044 h + y > DisplayHeight(display, sidx)) {
1045 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1046 "(%ux%u)\n", w, h, x, y,
1047 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1051 new_region(&screens[sidx], x, y, w, h);
1055 socket_setnonblock(int fd)
1059 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1060 err(1, "fcntl F_GETFL");
1061 flags |= O_NONBLOCK;
1062 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1063 err(1, "fcntl F_SETFL");
1067 bar_print(struct swm_region *r, char *s)
1069 XClearWindow(display, r->bar_window);
1070 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1071 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1076 bar_extra_stop(void)
1080 bzero(bar_pipe, sizeof bar_pipe);
1083 kill(bar_pid, SIGTERM);
1086 strlcpy(bar_ext, "", sizeof bar_ext);
1091 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1093 int do_class, do_name;
1095 XClassHint *xch = NULL;
1097 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1098 cur_focus != NULL) {
1099 if ((xch = XAllocClassHint()) == NULL)
1101 status = XGetClassHint(display, cur_focus->id, xch);
1102 if (status == BadWindow || status == BadAlloc)
1104 do_class = (title_class_enabled && xch->res_class != NULL);
1105 do_name = (title_name_enabled && xch->res_name != NULL);
1107 strlcat(s, xch->res_class, sz);
1108 if (do_class && do_name)
1109 strlcat(s, ":", sz);
1111 strlcat(s, xch->res_name, sz);
1112 strlcat(s, " ", sz);
1120 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1124 if (window_name_enabled && cur_focus != NULL) {
1125 XFetchName(display, cur_focus->id, &title);
1127 if (cur_focus->floating)
1128 strlcat(s, "(f) ", sz);
1129 strlcat(s, title, sz);
1130 strlcat(s, " ", sz);
1141 struct swm_region *r;
1144 char s[SWM_BAR_MAX];
1145 char cn[SWM_BAR_MAX];
1146 char loc[SWM_BAR_MAX];
1150 if (bar_enabled == 0)
1152 if (bar_extra && bar_extra_running) {
1153 /* ignore short reads; it'll correct itself */
1154 while ((b = fgetln(stdin, &len)) != NULL)
1155 if (b && b[len - 1] == '\n') {
1157 strlcpy(bar_ext, b, sizeof bar_ext);
1159 if (b == NULL && errno != EAGAIN) {
1160 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1161 errno, strerror(errno));
1165 strlcpy(bar_ext, "", sizeof bar_ext);
1167 if (clock_enabled == 0)
1168 strlcpy(s, "", sizeof s);
1171 localtime_r(&tmt, &tm);
1172 strftime(s, sizeof s, clock_format, &tm);
1173 strlcat(s, " ", sizeof s);
1176 for (i = 0; i < ScreenCount(display); i++) {
1178 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1179 strlcpy(cn, "", sizeof cn);
1181 bar_class_name(cn, sizeof cn, r->ws->focus);
1182 bar_window_name(cn, sizeof cn, r->ws->focus);
1186 stack = r->ws->cur_layout->name;
1188 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1189 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1204 bar_toggle(struct swm_region *r, union arg *args)
1206 struct swm_region *tmpr;
1207 int i, sc = ScreenCount(display);
1209 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1212 for (i = 0; i < sc; i++)
1213 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1214 XUnmapWindow(display, tmpr->bar_window);
1216 for (i = 0; i < sc; i++)
1217 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1218 XMapRaised(display, tmpr->bar_window);
1220 bar_enabled = !bar_enabled;
1223 /* must be after stack */
1230 XSetWindowAttributes wa;
1231 struct swm_region *r;
1234 /* do this here because the conf file is in memory */
1235 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1236 /* launch external status app */
1237 bar_extra_running = 1;
1238 if (pipe(bar_pipe) == -1)
1239 err(1, "pipe error");
1240 socket_setnonblock(bar_pipe[0]);
1241 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1242 if (dup2(bar_pipe[0], 0) == -1)
1244 if (dup2(bar_pipe[1], 1) == -1)
1246 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1247 err(1, "could not disable SIGPIPE");
1248 switch (bar_pid = fork()) {
1250 err(1, "cannot fork");
1254 execvp(bar_argv[0], bar_argv);
1255 err(1, "%s external app failed", bar_argv[0]);
1257 default: /* parent */
1263 bzero(&wa, sizeof wa);
1264 for (i = 0; i < ScreenCount(display); i++)
1265 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1267 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1268 wa.background_pixel =
1269 screens[i].c[SWM_S_COLOR_BAR].color;
1270 XChangeWindowAttributes(display, r->bar_window,
1271 CWBackPixel | CWBorderPixel, &wa);
1277 bar_setup(struct swm_region *r)
1282 XFreeFont(display, bar_fs);
1286 for (i = 0; bar_fonts[i] != NULL; i++) {
1287 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1293 if (bar_fonts[i] == NULL)
1294 errx(1, "couldn't load font");
1296 errx(1, "couldn't create font structure");
1298 bar_height = bar_fs->ascent + bar_fs->descent + 3;
1300 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1302 r->bar_window = XCreateSimpleWindow(display,
1303 r->s->root, x, y, WIDTH(r) - 2, bar_height - 2,
1304 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1305 r->s->c[SWM_S_COLOR_BAR].color);
1306 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1307 XSetFont(display, bar_gc, bar_fs->fid);
1308 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1310 XMapRaised(display, r->bar_window);
1311 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1313 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1314 err(1, "could not install bar_signal");
1319 set_win_state(struct ws_win *win, long state)
1321 long data[] = {state, None};
1323 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1328 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1329 (unsigned char *)data, 2);
1336 unsigned char *p = NULL;
1339 if (!get_property(w, astate, 2L, astate, &n, &p))
1342 result = *((long *)p);
1348 version(struct swm_region *r, union arg *args)
1350 bar_version = !bar_version;
1352 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1353 SWM_VERSION, cvstag);
1355 strlcpy(bar_vertext, "", sizeof bar_vertext);
1360 client_msg(struct ws_win *win, Atom a)
1362 XClientMessageEvent cm;
1367 bzero(&cm, sizeof cm);
1368 cm.type = ClientMessage;
1369 cm.window = win->id;
1370 cm.message_type = aprot;
1373 cm.data.l[1] = CurrentTime;
1374 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1378 configreq_win(struct ws_win *win)
1380 XConfigureRequestEvent cr;
1385 bzero(&cr, sizeof cr);
1386 cr.type = ConfigureRequest;
1387 cr.display = display;
1388 cr.parent = win->id;
1389 cr.window = win->id;
1392 cr.width = win->g.w;
1393 cr.height = win->g.h;
1394 cr.border_width = 1;
1396 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1400 config_win(struct ws_win *win)
1404 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1405 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1410 ce.type = ConfigureNotify;
1411 ce.display = display;
1413 ce.window = win->id;
1416 ce.width = win->g.w;
1417 ce.height = win->g.h;
1418 ce.border_width = 1; /* XXX store this! */
1420 ce.override_redirect = False;
1421 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1425 count_win(struct workspace *ws, int count_transient)
1430 TAILQ_FOREACH(win, &ws->winlist, entry) {
1431 if (count_transient == 0 && win->floating)
1433 if (count_transient == 0 && win->transient)
1437 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1443 quit(struct swm_region *r, union arg *args)
1445 DNPRINTF(SWM_D_MISC, "quit\n");
1450 unmap_window(struct ws_win *win)
1455 /* don't unmap again */
1456 if (getstate(win->id) == IconicState)
1459 set_win_state(win, IconicState);
1461 XUnmapWindow(display, win->id);
1462 XSetWindowBorder(display, win->id,
1463 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1472 for (i = 0; i < ScreenCount(display); i++)
1473 for (j = 0; j < SWM_WS_MAX; j++)
1474 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1479 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1486 event.display = display; /* Ignored, but what the hell */
1487 event.window = win->id;
1488 event.root = win->s->root;
1489 event.subwindow = None;
1490 event.time = CurrentTime;
1495 event.same_screen = True;
1496 event.keycode = XKeysymToKeycode(display, keysym);
1497 event.state = modifiers;
1499 event.type = KeyPress;
1500 XSendEvent(event.display, event.window, True,
1501 KeyPressMask, (XEvent *)&event);
1503 event.type = KeyRelease;
1504 XSendEvent(event.display, event.window, True,
1505 KeyPressMask, (XEvent *)&event);
1510 restart(struct swm_region *r, union arg *args)
1512 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1514 /* disable alarm because the following code may not be interrupted */
1516 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1517 errx(1, "can't disable alarm");
1522 XCloseDisplay(display);
1523 execvp(start_argv[0], start_argv);
1524 fprintf(stderr, "execvp failed\n");
1530 root_to_region(Window root)
1532 struct swm_region *r = NULL;
1534 int i, x, y, wx, wy;
1537 for (i = 0; i < ScreenCount(display); i++)
1538 if (screens[i].root == root)
1541 if (XQueryPointer(display, screens[i].root,
1542 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1543 /* choose a region based on pointer location */
1544 TAILQ_FOREACH(r, &screens[i].rl, entry)
1545 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1546 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1551 r = TAILQ_FIRST(&screens[i].rl);
1557 find_unmanaged_window(Window id)
1562 for (i = 0; i < ScreenCount(display); i++)
1563 for (j = 0; j < SWM_WS_MAX; j++)
1564 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1572 find_window(Window id)
1575 Window wrr, wpr, *wcr = NULL;
1579 for (i = 0; i < ScreenCount(display); i++)
1580 for (j = 0; j < SWM_WS_MAX; j++)
1581 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1585 /* if we were looking for the parent return that window instead */
1586 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1591 /* ignore not found and root */
1592 if (wpr == 0 || wrr == wpr)
1595 /* look for parent */
1596 for (i = 0; i < ScreenCount(display); i++)
1597 for (j = 0; j < SWM_WS_MAX; j++)
1598 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1606 spawn(struct swm_region *r, union arg *args)
1611 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1615 close(ConnectionNumber(display));
1617 setenv("LD_PRELOAD", SWM_LIB, 1);
1619 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1623 setenv("_SWM_WS", ret, 1);
1627 if (asprintf(&ret, "%d", getpid()) == -1) {
1631 setenv("_SWM_PID", ret, 1);
1635 if (setsid() == -1) {
1641 * close stdin and stdout to prevent interaction between apps
1642 * and the baraction script
1643 * leave stderr open to record errors
1645 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1649 dup2(fd, STDIN_FILENO);
1650 dup2(fd, STDOUT_FILENO);
1654 execvp(args->argv[0], args->argv);
1662 spawnterm(struct swm_region *r, union arg *args)
1664 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1667 setenv("_SWM_XTERM_FONTADJ", "", 1);
1672 kill_refs(struct ws_win *win)
1675 struct swm_region *r;
1676 struct workspace *ws;
1681 for (i = 0; i < ScreenCount(display); i++)
1682 TAILQ_FOREACH(r, &screens[i].rl, entry)
1683 for (x = 0; x < SWM_WS_MAX; x++) {
1685 if (win == ws->focus)
1687 if (win == ws->focus_prev)
1688 ws->focus_prev = NULL;
1693 validate_win(struct ws_win *testwin)
1696 struct workspace *ws;
1697 struct swm_region *r;
1698 int i, x, foundit = 0;
1700 if (testwin == NULL)
1703 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1704 TAILQ_FOREACH(r, &screens[i].rl, entry)
1705 for (x = 0; x < SWM_WS_MAX; x++) {
1707 TAILQ_FOREACH(win, &ws->winlist, entry)
1715 validate_ws(struct workspace *testws)
1717 struct swm_region *r;
1718 struct workspace *ws;
1721 /* validate all ws */
1722 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1723 TAILQ_FOREACH(r, &screens[i].rl, entry)
1724 for (x = 0; x < SWM_WS_MAX; x++) {
1733 unfocus_win(struct ws_win *win)
1738 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1742 if (win->ws == NULL)
1745 if (validate_ws(win->ws))
1746 return; /* XXX this gets hit with thunderbird, needs fixing */
1748 if (win->ws->r == NULL)
1751 if (validate_win(win)) {
1756 if (win->ws->focus == win) {
1757 win->ws->focus = NULL;
1758 win->ws->focus_prev = win;
1761 if (validate_win(win->ws->focus)) {
1762 kill_refs(win->ws->focus);
1763 win->ws->focus = NULL;
1765 if (validate_win(win->ws->focus_prev)) {
1766 kill_refs(win->ws->focus_prev);
1767 win->ws->focus_prev = NULL;
1770 /* drain all previous unfocus events */
1771 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1774 grabbuttons(win, 0);
1775 XSetWindowBorder(display, win->id,
1776 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1778 XChangeProperty(display, win->s->root,
1779 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1780 PropModeReplace, (unsigned char *)&none,1);
1789 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1791 for (i = 0; i < ScreenCount(display); i++)
1792 for (j = 0; j < SWM_WS_MAX; j++)
1793 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1798 focus_win(struct ws_win *win)
1803 struct ws_win *cfw = NULL;
1806 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1810 if (win->ws == NULL)
1813 if (validate_ws(win->ws))
1814 return; /* XXX this gets hit with thunderbird, needs fixing */
1816 if (validate_win(win)) {
1821 if (validate_win(win)) {
1826 XGetInputFocus(display, &cur_focus, &rr);
1827 if ((cfw = find_window(cur_focus)) != NULL)
1830 win->ws->focus = win;
1832 if (win->ws->r != NULL) {
1833 /* drain all previous focus events */
1834 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1838 XSetInputFocus(display, win->id,
1839 RevertToParent, CurrentTime);
1840 grabbuttons(win, 1);
1841 XSetWindowBorder(display, win->id,
1842 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1843 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1844 XMapRaised(display, win->id);
1846 XChangeProperty(display, win->s->root,
1847 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1848 PropModeReplace, (unsigned char *)&win->id,1);
1851 if (window_name_enabled)
1856 switchws(struct swm_region *r, union arg *args)
1858 int wsid = args->id, unmap_old = 0;
1859 struct swm_region *this_r, *other_r;
1861 struct workspace *new_ws, *old_ws;
1868 old_ws = this_r->ws;
1869 new_ws = &this_r->s->ws[wsid];
1871 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1872 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1875 if (new_ws == NULL || old_ws == NULL)
1877 if (new_ws == old_ws)
1880 other_r = new_ws->r;
1881 if (other_r == NULL) {
1882 /* the other workspace is hidden, hide this one */
1886 /* the other ws is visible in another region, exchange them */
1887 other_r->ws_prior = new_ws;
1888 other_r->ws = old_ws;
1889 old_ws->r = other_r;
1891 this_r->ws_prior = old_ws;
1892 this_r->ws = new_ws;
1896 a.id = SWM_ARG_ID_FOCUSCUR;
1897 focus(new_ws->r, &a);
1900 /* unmap old windows */
1902 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1907 cyclews(struct swm_region *r, union arg *args)
1910 struct swm_screen *s = r->s;
1912 DNPRINTF(SWM_D_WS, "cyclews id %d "
1913 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1914 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1919 case SWM_ARG_ID_CYCLEWS_UP:
1920 if (a.id < SWM_WS_MAX - 1)
1925 case SWM_ARG_ID_CYCLEWS_DOWN:
1929 a.id = SWM_WS_MAX - 1;
1935 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1937 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1941 } while (a.id != r->ws->idx);
1945 priorws(struct swm_region *r, union arg *args)
1949 DNPRINTF(SWM_D_WS, "priorws id %d "
1950 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1951 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1953 if (r->ws_prior == NULL)
1956 a.id = r->ws_prior->idx;
1961 cyclescr(struct swm_region *r, union arg *args)
1963 struct swm_region *rr = NULL;
1967 /* do nothing if we don't have more than one screen */
1968 if (!(ScreenCount(display) > 1 || outputs > 1))
1973 case SWM_ARG_ID_CYCLESC_UP:
1974 rr = TAILQ_NEXT(r, entry);
1976 rr = TAILQ_FIRST(&screens[i].rl);
1978 case SWM_ARG_ID_CYCLESC_DOWN:
1979 rr = TAILQ_PREV(r, swm_region_list, entry);
1981 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1989 /* move mouse to region */
1991 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1992 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1994 a.id = SWM_ARG_ID_FOCUSCUR;
1997 if (rr->ws->focus) {
1998 /* move to focus window */
1999 x = rr->ws->focus->g.x + 1;
2000 y = rr->ws->focus->g.y + 1;
2001 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2006 swapwin(struct swm_region *r, union arg *args)
2008 struct ws_win *target, *source;
2009 struct ws_win *cur_focus;
2010 struct ws_win_list *wl;
2013 DNPRINTF(SWM_D_WS, "swapwin id %d "
2014 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2015 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2017 cur_focus = r->ws->focus;
2018 if (cur_focus == NULL)
2022 wl = &source->ws->winlist;
2025 case SWM_ARG_ID_SWAPPREV:
2026 target = TAILQ_PREV(source, ws_win_list, entry);
2027 TAILQ_REMOVE(wl, cur_focus, entry);
2029 TAILQ_INSERT_TAIL(wl, source, entry);
2031 TAILQ_INSERT_BEFORE(target, source, entry);
2033 case SWM_ARG_ID_SWAPNEXT:
2034 target = TAILQ_NEXT(source, entry);
2035 TAILQ_REMOVE(wl, source, entry);
2037 TAILQ_INSERT_HEAD(wl, source, entry);
2039 TAILQ_INSERT_AFTER(wl, target, source, entry);
2041 case SWM_ARG_ID_SWAPMAIN:
2042 target = TAILQ_FIRST(wl);
2043 if (target == source) {
2044 if (source->ws->focus_prev != NULL &&
2045 source->ws->focus_prev != target)
2047 source = source->ws->focus_prev;
2051 if (target == NULL || source == NULL)
2053 source->ws->focus_prev = target;
2054 TAILQ_REMOVE(wl, target, entry);
2055 TAILQ_INSERT_BEFORE(source, target, entry);
2056 TAILQ_REMOVE(wl, source, entry);
2057 TAILQ_INSERT_HEAD(wl, source, entry);
2059 case SWM_ARG_ID_MOVELAST:
2060 TAILQ_REMOVE(wl, source, entry);
2061 TAILQ_INSERT_TAIL(wl, source, entry);
2064 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2072 focus_prev(struct ws_win *win)
2074 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2075 struct ws_win *cur_focus = NULL;
2076 struct ws_win_list *wl = NULL;
2077 struct workspace *ws = NULL;
2079 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2081 if (!(win && win->ws))
2086 cur_focus = ws->focus;
2087 winlostfocus = cur_focus;
2089 /* pickle, just focus on whatever */
2090 if (cur_focus == NULL) {
2091 /* use prev_focus if valid */
2092 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2093 find_window(WINID(ws->focus_prev)))
2094 winfocus = ws->focus_prev;
2095 if (winfocus == NULL)
2096 winfocus = TAILQ_FIRST(wl);
2100 /* if transient focus on parent */
2101 if (cur_focus->transient) {
2102 winfocus = find_window(cur_focus->transient);
2106 /* if in max_stack try harder */
2107 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
2108 if (cur_focus != ws->focus_prev)
2109 winfocus = ws->focus_prev;
2110 else if (cur_focus != ws->focus)
2111 winfocus = ws->focus;
2113 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2118 if (cur_focus == win)
2119 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2120 if (winfocus == NULL)
2121 winfocus = TAILQ_LAST(wl, ws_win_list);
2122 if (winfocus == NULL || winfocus == win)
2123 winfocus = TAILQ_NEXT(cur_focus, entry);
2125 if (winfocus == winlostfocus || winfocus == NULL)
2128 focus_magic(winfocus, SWM_F_GENERIC);
2132 focus(struct swm_region *r, union arg *args)
2134 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2135 struct ws_win *cur_focus = NULL;
2136 struct ws_win_list *wl = NULL;
2137 struct workspace *ws = NULL;
2142 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2144 /* treat FOCUS_CUR special */
2145 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2147 winfocus = r->ws->focus;
2148 else if (r->ws->focus_prev)
2149 winfocus = r->ws->focus_prev;
2151 winfocus = TAILQ_FIRST(&r->ws->winlist);
2153 focus_magic(winfocus, SWM_F_GENERIC);
2157 if ((cur_focus = r->ws->focus) == NULL)
2162 winlostfocus = cur_focus;
2165 case SWM_ARG_ID_FOCUSPREV:
2166 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
2167 if (winfocus == NULL)
2168 winfocus = TAILQ_LAST(wl, ws_win_list);
2171 case SWM_ARG_ID_FOCUSNEXT:
2172 winfocus = TAILQ_NEXT(cur_focus, entry);
2173 if (winfocus == NULL)
2174 winfocus = TAILQ_FIRST(wl);
2177 case SWM_ARG_ID_FOCUSMAIN:
2178 winfocus = TAILQ_FIRST(wl);
2179 if (winfocus == cur_focus)
2180 winfocus = cur_focus->ws->focus_prev;
2187 if (winfocus == winlostfocus || winfocus == NULL)
2190 focus_magic(winfocus, SWM_F_GENERIC);
2194 cycle_layout(struct swm_region *r, union arg *args)
2196 struct workspace *ws = r->ws;
2197 struct ws_win *winfocus;
2200 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2202 winfocus = ws->focus;
2205 if (ws->cur_layout->l_stack == NULL)
2206 ws->cur_layout = &layouts[0];
2209 a.id = SWM_ARG_ID_FOCUSCUR;
2215 stack_config(struct swm_region *r, union arg *args)
2217 struct workspace *ws = r->ws;
2219 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2222 if (ws->cur_layout->l_config != NULL)
2223 ws->cur_layout->l_config(ws, args->id);
2225 if (args->id != SWM_ARG_ID_STACKINIT);
2231 struct swm_geometry g;
2232 struct swm_region *r;
2235 DNPRINTF(SWM_D_STACK, "stack\n");
2237 for (i = 0; i < ScreenCount(display); i++) {
2239 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2240 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2241 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2243 /* start with screen geometry, adjust for bar */
2252 r->ws->cur_layout->l_stack(r->ws, &g);
2253 /* save r so we can track region changes */
2262 store_float_geom(struct ws_win *win, struct swm_region *r)
2264 /* retain window geom and region geom */
2265 win->g_float.x = win->g.x;
2266 win->g_float.y = win->g.y;
2267 win->g_float.w = win->g.w;
2268 win->g_float.h = win->g.h;
2269 win->rg_float.x = r->g.x;
2270 win->rg_float.y = r->g.y;
2271 win->rg_float.w = r->g.w;
2272 win->rg_float.h = r->g.h;
2273 win->g_floatvalid = 1;
2277 stack_floater(struct ws_win *win, struct swm_region *r)
2285 bzero(&wc, sizeof wc);
2286 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2289 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2290 * geom on ws switches or return from max mode
2293 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2294 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2296 * use stored g and rg to set relative position and size
2297 * as in old region or before max stack mode
2299 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2300 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2301 win->g.w = win->g_float.w;
2302 win->g.h = win->g_float.h;
2303 win->g_floatvalid = 0;
2306 win->floatmaxed = 0;
2308 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2309 (win->g.h >= HEIGHT(r)))
2310 wc.border_width = 0;
2312 wc.border_width = 1;
2313 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2314 win->g.w = (double)WIDTH(r) * dialog_ratio;
2315 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2320 * floaters and transients are auto-centred unless moved
2323 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2;
2324 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2;
2327 /* win can be outside r if new r smaller than old r */
2328 /* Ensure top left corner inside r (move probs otherwise) */
2329 if (win->g.x < r->g.x )
2331 if (win->g.x > r->g.x + r->g.w - 1)
2332 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2333 (r->g.x + r->g.w - win->g.w - 2);
2334 if (win->g.y < r->g.y )
2336 if (win->g.y > r->g.y + r->g.h - 1)
2337 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2338 (r->g.y + r->g.h - win->g.h - 2);
2342 wc.width = win->g.w;
2343 wc.height = win->g.h;
2346 * Retain floater and transient geometry for correct positioning
2347 * when ws changes region
2349 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2350 store_float_geom(win, r);
2352 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2353 win->id, wc.x, wc.y, wc.width, wc.height);
2355 XConfigureWindow(display, win->id, mask, &wc);
2360 * Send keystrokes to terminal to decrease/increase the font size as the
2361 * window size changes.
2364 adjust_font(struct ws_win *win)
2366 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2367 win->floating || win->transient)
2370 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2371 win->g.w / win->sh.width_inc < term_width &&
2372 win->font_steps < SWM_MAX_FONT_STEPS) {
2373 win->font_size_boundary[win->font_steps] =
2374 (win->sh.width_inc * term_width) + win->sh.base_width;
2377 win->last_inc = win->sh.width_inc;
2378 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2379 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2380 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2383 win->last_inc = win->sh.width_inc;
2384 fake_keypress(win, XK_KP_Add, ShiftMask);
2388 #define SWAPXY(g) do { \
2390 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2391 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2394 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2397 XWindowAttributes wa;
2398 struct swm_geometry win_g, r_g = *g;
2399 struct ws_win *win, *fs_win = 0;
2400 int i, j, s, stacks;
2401 int w_inc = 1, h_inc, w_base = 1, h_base;
2402 int hrh, extra = 0, h_slice, last_h = 0;
2403 int split, colno, winno, mwin, msize, mscale;
2404 int remain, missing, v_slice, reconfigure;
2407 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2408 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2410 winno = count_win(ws, 0);
2411 if (winno == 0 && count_win(ws, 1) == 0)
2414 TAILQ_FOREACH(win, &ws->winlist, entry)
2415 if (win->transient == 0 && win->floating == 0)
2422 w_inc = win->sh.width_inc;
2423 w_base = win->sh.base_width;
2424 mwin = ws->l_state.horizontal_mwin;
2425 mscale = ws->l_state.horizontal_msize;
2426 stacks = ws->l_state.horizontal_stacks;
2429 w_inc = win->sh.height_inc;
2430 w_base = win->sh.base_height;
2431 mwin = ws->l_state.vertical_mwin;
2432 mscale = ws->l_state.vertical_msize;
2433 stacks = ws->l_state.vertical_stacks;
2437 if (stacks > winno - mwin)
2438 stacks = winno - mwin;
2442 h_slice = r_g.h / SWM_H_SLICE;
2443 if (mwin && winno > mwin) {
2444 v_slice = r_g.w / SWM_V_SLICE;
2448 win_g.w = v_slice * mscale;
2450 if (w_inc > 1 && w_inc < v_slice) {
2451 /* adjust for window's requested size increment */
2452 remain = (win_g.w - w_base) % w_inc;
2453 missing = w_inc - remain;
2460 win_g.x += r_g.w - msize;
2463 colno = split = winno / stacks;
2464 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
2466 hrh = r_g.h / colno;
2467 extra = r_g.h - (colno * hrh);
2470 /* stack all the tiled windows */
2471 i = j = 0, s = stacks;
2472 TAILQ_FOREACH(win, &ws->winlist, entry) {
2473 if (win->transient != 0 || win->floating != 0)
2476 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2481 if (split && i == split) {
2482 colno = (winno - mwin) / stacks;
2483 if (s <= (winno - mwin) % stacks)
2485 split = split + colno;
2486 hrh = (r_g.h / colno);
2487 extra = r_g.h - (colno * hrh);
2491 win_g.x += win_g.w + 2;
2492 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2494 win_g.w += (r_g.w - msize - (stacks * 2)) %
2501 h_inc = win->sh.width_inc;
2502 h_base = win->sh.base_width;
2504 h_inc = win->sh.height_inc;
2505 h_base = win->sh.base_height;
2507 if (j == colno - 1) {
2508 win_g.h = hrh + extra;
2509 } else if (h_inc > 1 && h_inc < h_slice) {
2510 /* adjust for window's requested size increment */
2511 remain = (win_g.h - h_base) % h_inc;
2512 missing = h_inc - remain;
2514 if (missing <= extra || j == 0) {
2526 win_g.y += last_h + 2;
2528 bzero(&wc, sizeof wc);
2529 if (disable_border && bar_enabled == 0 && winno == 1){
2530 wc.border_width = 0;
2534 wc.border_width = 1;
2537 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2538 win->g.w != win_g.h || win->g.h != win_g.w) {
2540 win->g.x = wc.x = win_g.y;
2541 win->g.y = wc.y = win_g.x;
2542 win->g.w = wc.width = win_g.h;
2543 win->g.h = wc.height = win_g.w;
2546 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2547 win->g.w != win_g.w || win->g.h != win_g.h) {
2549 win->g.x = wc.x = win_g.x;
2550 win->g.y = wc.y = win_g.y;
2551 win->g.w = wc.width = win_g.w;
2552 win->g.h = wc.height = win_g.h;
2557 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2558 XConfigureWindow(display, win->id, mask, &wc);
2562 if (XGetWindowAttributes(display, win->id, &wa))
2563 if (wa.map_state == IsUnmapped)
2564 XMapRaised(display, win->id);
2572 /* now, stack all the floaters and transients */
2573 TAILQ_FOREACH(win, &ws->winlist, entry) {
2574 if (win->transient == 0 && win->floating == 0)
2577 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2582 stack_floater(win, ws->r);
2583 XMapRaised(display, win->id);
2587 stack_floater(fs_win, ws->r);
2588 XMapRaised(display, fs_win->id);
2593 vertical_config(struct workspace *ws, int id)
2595 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2598 case SWM_ARG_ID_STACKRESET:
2599 case SWM_ARG_ID_STACKINIT:
2600 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2601 ws->l_state.vertical_mwin = 1;
2602 ws->l_state.vertical_stacks = 1;
2604 case SWM_ARG_ID_MASTERSHRINK:
2605 if (ws->l_state.vertical_msize > 1)
2606 ws->l_state.vertical_msize--;
2608 case SWM_ARG_ID_MASTERGROW:
2609 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2610 ws->l_state.vertical_msize++;
2612 case SWM_ARG_ID_MASTERADD:
2613 ws->l_state.vertical_mwin++;
2615 case SWM_ARG_ID_MASTERDEL:
2616 if (ws->l_state.vertical_mwin > 0)
2617 ws->l_state.vertical_mwin--;
2619 case SWM_ARG_ID_STACKINC:
2620 ws->l_state.vertical_stacks++;
2622 case SWM_ARG_ID_STACKDEC:
2623 if (ws->l_state.vertical_stacks > 1)
2624 ws->l_state.vertical_stacks--;
2632 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2634 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2636 stack_master(ws, g, 0, 0);
2640 horizontal_config(struct workspace *ws, int id)
2642 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2645 case SWM_ARG_ID_STACKRESET:
2646 case SWM_ARG_ID_STACKINIT:
2647 ws->l_state.horizontal_mwin = 1;
2648 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2649 ws->l_state.horizontal_stacks = 1;
2651 case SWM_ARG_ID_MASTERSHRINK:
2652 if (ws->l_state.horizontal_msize > 1)
2653 ws->l_state.horizontal_msize--;
2655 case SWM_ARG_ID_MASTERGROW:
2656 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2657 ws->l_state.horizontal_msize++;
2659 case SWM_ARG_ID_MASTERADD:
2660 ws->l_state.horizontal_mwin++;
2662 case SWM_ARG_ID_MASTERDEL:
2663 if (ws->l_state.horizontal_mwin > 0)
2664 ws->l_state.horizontal_mwin--;
2666 case SWM_ARG_ID_STACKINC:
2667 ws->l_state.horizontal_stacks++;
2669 case SWM_ARG_ID_STACKDEC:
2670 if (ws->l_state.horizontal_stacks > 1)
2671 ws->l_state.horizontal_stacks--;
2679 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2681 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2683 stack_master(ws, g, 1, 0);
2686 /* fullscreen view */
2688 max_stack(struct workspace *ws, struct swm_geometry *g)
2691 struct swm_geometry gg = *g;
2692 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2696 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2701 winno = count_win(ws, 0);
2702 if (winno == 0 && count_win(ws, 1) == 0)
2705 TAILQ_FOREACH(win, &ws->winlist, entry) {
2706 if (win->transient) {
2708 parent = find_window(win->transient);
2712 if (win->floating && win->floatmaxed == 0 ) {
2714 * retain geometry for retrieval on exit from
2717 store_float_geom(win, ws->r);
2718 win->floatmaxed = 1;
2721 /* only reconfigure if necessary */
2722 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2724 bzero(&wc, sizeof wc);
2725 win->g.x = wc.x = gg.x;
2726 win->g.y = wc.y = gg.y;
2728 wc.border_width = 1;
2729 win->g.w = wc.width = gg.w;
2730 win->g.h = wc.height = gg.h;
2732 wc.border_width = 0;
2733 win->g.w = wc.width = gg.w + 2;
2734 win->g.h = wc.height = gg.h + 2;
2736 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2737 XConfigureWindow(display, win->id, mask, &wc);
2740 /* unmap only if we don't have multi screen */
2741 if (win != ws->focus)
2742 if (!(ScreenCount(display) > 1 || outputs > 1))
2746 /* put the last transient on top */
2749 XMapRaised(display, parent->id);
2750 stack_floater(wintrans, ws->r);
2751 focus_magic(wintrans, SWM_F_TRANSIENT);
2756 send_to_ws(struct swm_region *r, union arg *args)
2758 int wsid = args->id;
2759 struct ws_win *win = win;
2760 struct workspace *ws, *nws;
2761 Atom ws_idx_atom = 0;
2762 unsigned char ws_idx_str[SWM_PROPLEN];
2771 if (win->ws->idx == wsid)
2774 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2777 nws = &win->s->ws[wsid];
2779 a.id = SWM_ARG_ID_FOCUSPREV;
2782 TAILQ_REMOVE(&ws->winlist, win, entry);
2783 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2786 /* Try to update the window's workspace property */
2787 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2789 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2790 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2792 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2793 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2800 wkill(struct swm_region *r, union arg *args)
2802 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2804 if (r->ws->focus == NULL)
2807 if (args->id == SWM_ARG_ID_KILLWINDOW)
2808 XKillClient(display, r->ws->focus->id);
2810 if (r->ws->focus->can_delete)
2811 client_msg(r->ws->focus, adelete);
2816 floating_toggle_win(struct ws_win *win)
2818 struct swm_region *r;
2828 /* reject floating toggles in max stack mode */
2829 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
2832 if (win->floating) {
2833 if (!win->floatmaxed) {
2834 /* retain position for refloat */
2835 store_float_geom(win, r);
2839 if (win->g_floatvalid) {
2840 /* refloat at last floating relative position */
2841 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2842 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2843 win->g.w = win->g_float.w;
2844 win->g.h = win->g_float.h;
2849 ewmh_update_actions(win);
2855 floating_toggle(struct swm_region *r, union arg *args)
2857 struct ws_win *win = r->ws->focus;
2863 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
2864 _NET_WM_STATE_TOGGLE);
2868 if (win == win->ws->focus) {
2869 a.id = SWM_ARG_ID_FOCUSCUR;
2870 focus(win->ws->r, &a);
2875 resize_window(struct ws_win *win, int center)
2879 struct swm_region *r;
2881 r = root_to_region(win->wa.root);
2882 bzero(&wc, sizeof wc);
2883 mask = CWBorderWidth | CWWidth | CWHeight;
2884 wc.border_width = 1;
2885 wc.width = win->g.w;
2886 wc.height = win->g.h;
2887 if (center == SWM_ARG_ID_CENTER) {
2888 wc.x = (WIDTH(r) - win->g.w) / 2;
2889 wc.y = (HEIGHT(r) - win->g.h) / 2;
2893 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2894 win->id, wc.x, wc.y, wc.width, wc.height);
2896 XConfigureWindow(display, win->id, mask, &wc);
2901 resize(struct ws_win *win, union arg *args)
2905 struct swm_region *r = win->ws->r;
2910 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2911 win->id, win->floating, win->transient);
2913 if (!(win->transient != 0 || win->floating != 0))
2916 /* reject resizes in max mode for floaters (transient ok) */
2917 if (win->floatmaxed)
2921 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
2923 /* raise the window = move to last in window list */
2924 a.id = SWM_ARG_ID_MOVELAST;
2928 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2929 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2932 /* place pointer at bottom left corner or nearest point inside r */
2933 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2936 relx = r->g.x + r->g.w - win->g.x - 2;
2938 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2941 rely = r->g.y + r->g.h - win->g.y - 2;
2943 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2945 XMaskEvent(display, MOUSEMASK | ExposureMask |
2946 SubstructureRedirectMask, &ev);
2948 case ConfigureRequest:
2951 handler[ev.type](&ev);
2954 /* do not allow resize outside of region */
2955 if ( ev.xmotion.x_root < r->g.x ||
2956 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2957 ev.xmotion.y_root < r->g.y ||
2958 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2961 if (ev.xmotion.x <= 1)
2963 if (ev.xmotion.y <= 1)
2965 win->g.w = ev.xmotion.x;
2966 win->g.h = ev.xmotion.y;
2968 /* not free, don't sync more than 60 times / second */
2969 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2970 time = ev.xmotion.time;
2971 XSync(display, False);
2972 resize_window(win, args->id);
2976 } while (ev.type != ButtonRelease);
2978 XSync(display, False);
2979 resize_window(win, args->id);
2981 store_float_geom(win,r);
2983 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2985 XUngrabPointer(display, CurrentTime);
2988 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2992 move_window(struct ws_win *win)
2996 struct swm_region *r;
2998 r = root_to_region(win->wa.root);
2999 bzero(&wc, sizeof wc);
3004 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3005 win->id, wc.x, wc.y, wc.width, wc.height);
3007 XConfigureWindow(display, win->id, mask, &wc);
3012 move(struct ws_win *win, union arg *args)
3016 struct swm_region *r = win->ws->r;
3019 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3020 win->id, win->floating, win->transient);
3022 /* in max_stack mode should only move transients */
3023 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3027 if (win->floating == 0 && !win->transient) {
3029 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3032 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3035 /* raise the window = move to last in window list */
3036 a.id = SWM_ARG_ID_MOVELAST;
3040 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3041 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3043 XWarpPointer(display, None, win->id, 0, 0, 0, 0, -1, -1);
3045 XMaskEvent(display, MOUSEMASK | ExposureMask |
3046 SubstructureRedirectMask, &ev);
3048 case ConfigureRequest:
3051 handler[ev.type](&ev);
3054 /* don't allow to move window origin out of region */
3055 if ( ev.xmotion.x_root < r->g.x ||
3056 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3057 ev.xmotion.y_root < r->g.y ||
3058 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3061 win->g.x = ev.xmotion.x_root;
3062 win->g.y = ev.xmotion.y_root;
3064 /* not free, don't sync more than 60 times / second */
3065 if ((ev.xmotion.time - time) > (1000 / 60) ) {
3066 time = ev.xmotion.time;
3067 XSync(display, False);
3072 } while (ev.type != ButtonRelease);
3074 XSync(display, False);
3077 store_float_geom(win,r);
3078 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3079 XUngrabPointer(display, CurrentTime);
3082 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
3085 /* user/key callable function IDs */
3144 /* key definitions */
3146 dummykeyfunc(struct swm_region *r, union arg *args)
3151 legacyfunc(struct swm_region *r, union arg *args)
3156 char name[SWM_FUNCNAME_LEN];
3157 void (*func)(struct swm_region *r, union arg *);
3159 } keyfuncs[kf_invalid + 1] = {
3160 /* name function argument */
3161 { "cycle_layout", cycle_layout, {0} },
3162 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3163 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3164 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3165 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3166 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3167 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3168 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3169 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3170 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3171 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3172 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3173 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3174 { "spawn_term", spawnterm, {.argv = spawn_term} },
3175 { "spawn_menu", legacyfunc, {0} },
3176 { "quit", quit, {0} },
3177 { "restart", restart, {0} },
3178 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3179 { "ws_1", switchws, {.id = 0} },
3180 { "ws_2", switchws, {.id = 1} },
3181 { "ws_3", switchws, {.id = 2} },
3182 { "ws_4", switchws, {.id = 3} },
3183 { "ws_5", switchws, {.id = 4} },
3184 { "ws_6", switchws, {.id = 5} },
3185 { "ws_7", switchws, {.id = 6} },
3186 { "ws_8", switchws, {.id = 7} },
3187 { "ws_9", switchws, {.id = 8} },
3188 { "ws_10", switchws, {.id = 9} },
3189 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3190 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3191 { "ws_prior", priorws, {0} },
3192 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3193 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3194 { "mvws_1", send_to_ws, {.id = 0} },
3195 { "mvws_2", send_to_ws, {.id = 1} },
3196 { "mvws_3", send_to_ws, {.id = 2} },
3197 { "mvws_4", send_to_ws, {.id = 3} },
3198 { "mvws_5", send_to_ws, {.id = 4} },
3199 { "mvws_6", send_to_ws, {.id = 5} },
3200 { "mvws_7", send_to_ws, {.id = 6} },
3201 { "mvws_8", send_to_ws, {.id = 7} },
3202 { "mvws_9", send_to_ws, {.id = 8} },
3203 { "mvws_10", send_to_ws, {.id = 9} },
3204 { "bar_toggle", bar_toggle, {0} },
3205 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3206 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3207 { "screenshot_all", legacyfunc, {0} },
3208 { "screenshot_wind", legacyfunc, {0} },
3209 { "float_toggle", floating_toggle,{0} },
3210 { "version", version, {0} },
3211 { "spawn_lock", legacyfunc, {0} },
3212 { "spawn_initscr", legacyfunc, {0} },
3213 { "spawn_custom", dummykeyfunc, {0} },
3214 { "dumpwins", dumpwins, {0} },
3215 { "invalid key func", NULL, {0} },
3220 enum keyfuncid funcid;
3223 int keys_size = 0, keys_length = 0;
3224 struct key *keys = NULL;
3227 enum { client_click, root_click };
3229 unsigned int action;
3231 unsigned int button;
3232 void (*func)(struct ws_win *, union arg *);
3235 /* action key mouse button func args */
3236 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3237 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3238 { client_click, MODKEY, Button1, move, {0} },
3242 update_modkey(unsigned int mod)
3247 for (i = 0; i < keys_length; i++)
3248 if (keys[i].mod & ShiftMask)
3249 keys[i].mod = mod | ShiftMask;
3253 for (i = 0; i < LENGTH(buttons); i++)
3254 if (buttons[i].mask & ShiftMask)
3255 buttons[i].mask = mod | ShiftMask;
3257 buttons[i].mask = mod;
3267 int spawns_size = 0, spawns_length = 0;
3268 struct spawn_prog *spawns = NULL;
3271 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3274 struct spawn_prog *prog = NULL;
3276 char *ap, **real_args;
3278 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
3281 for (i = 0; i < spawns_length; i++) {
3282 if (!strcasecmp(spawn_name, spawns[i].name))
3286 fprintf(stderr, "spawn_custom: program %s not found\n",
3291 /* make room for expanded args */
3292 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3293 err(1, "spawn_custom: calloc real_args");
3295 /* expand spawn_args into real_args */
3296 for (i = 0; i < prog->argc; i++) {
3298 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3299 if (!strcasecmp(ap, "$bar_border")) {
3301 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3303 err(1, "spawn_custom border color");
3304 } else if (!strcasecmp(ap, "$bar_color")) {
3306 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3308 err(1, "spawn_custom bar color");
3309 } else if (!strcasecmp(ap, "$bar_font")) {
3310 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3312 err(1, "spawn_custom bar fonts");
3313 } else if (!strcasecmp(ap, "$bar_font_color")) {
3315 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3317 err(1, "spawn_custom color font");
3318 } else if (!strcasecmp(ap, "$color_focus")) {
3320 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3322 err(1, "spawn_custom color focus");
3323 } else if (!strcasecmp(ap, "$color_unfocus")) {
3325 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3327 err(1, "spawn_custom color unfocus");
3329 /* no match --> copy as is */
3330 if ((real_args[i] = strdup(ap)) == NULL)
3331 err(1, "spawn_custom strdup(ap)");
3333 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3338 if ((swm_debug & SWM_D_SPAWN) != 0) {
3339 fprintf(stderr, "spawn_custom: result = ");
3340 for (i = 0; i < prog->argc; i++)
3341 fprintf(stderr, "\"%s\" ", real_args[i]);
3342 fprintf(stderr, "\n");
3348 for (i = 0; i < prog->argc; i++)
3354 setspawn(struct spawn_prog *prog)
3358 if (prog == NULL || prog->name == NULL)
3362 for (i = 0; i < spawns_length; i++) {
3363 if (!strcmp(spawns[i].name, prog->name)) {
3365 if (prog->argv == NULL) {
3367 DNPRINTF(SWM_D_SPAWN,
3368 "setspawn: delete #%d %s\n",
3370 free(spawns[i].name);
3371 for (j = 0; j < spawns[i].argc; j++)
3372 free(spawns[i].argv[j]);
3373 free(spawns[i].argv);
3374 j = spawns_length - 1;
3376 spawns[i] = spawns[j];
3381 DNPRINTF(SWM_D_SPAWN,
3382 "setspawn: replace #%d %s\n",
3384 free(spawns[i].name);
3385 for (j = 0; j < spawns[i].argc; j++)
3386 free(spawns[i].argv[j]);
3387 free(spawns[i].argv);
3390 /* found case handled */
3396 if (prog->argv == NULL) {
3398 "error: setspawn: cannot find program %s", prog->name);
3403 /* not found: add */
3404 if (spawns_size == 0 || spawns == NULL) {
3406 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3407 spawns = malloc((size_t)spawns_size *
3408 sizeof(struct spawn_prog));
3409 if (spawns == NULL) {
3410 fprintf(stderr, "setspawn: malloc failed\n");
3414 } else if (spawns_length == spawns_size) {
3416 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3417 spawns = realloc(spawns, (size_t)spawns_size *
3418 sizeof(struct spawn_prog));
3419 if (spawns == NULL) {
3420 fprintf(stderr, "setspawn: realloc failed\n");
3426 if (spawns_length < spawns_size) {
3427 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3428 spawns_length, prog->name);
3429 i = spawns_length++;
3432 fprintf(stderr, "spawns array problem?\n");
3433 if (spawns == NULL) {
3434 fprintf(stderr, "spawns array is NULL!\n");
3442 setconfspawn(char *selector, char *value, int flags)
3444 struct spawn_prog *prog;
3445 char *vp, *cp, *word;
3447 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3448 if ((prog = calloc(1, sizeof *prog)) == NULL)
3449 err(1, "setconfspawn: calloc prog");
3450 prog->name = strdup(selector);
3451 if (prog->name == NULL)
3452 err(1, "setconfspawn prog->name");
3453 if ((cp = vp = strdup(value)) == NULL)
3454 err(1, "setconfspawn: strdup(value) ");
3455 while ((word = strsep(&cp, " \t")) != NULL) {
3456 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3458 cp += (long)strspn(cp, " \t");
3459 if (strlen(word) > 0) {
3461 prog->argv = realloc(prog->argv,
3462 prog->argc * sizeof(char *));
3463 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3464 err(1, "setconfspawn: strdup");
3471 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3478 setconfspawn("term", "xterm", 0);
3479 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3480 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3481 setconfspawn("lock", "xlock", 0);
3482 setconfspawn("initscr", "initscreen.sh", 0);
3483 setconfspawn("menu", "dmenu_run"
3486 " -nf $bar_font_color"
3488 " -sf $bar_color", 0);
3492 #define SWM_MODNAME_SIZE 32
3493 #define SWM_KEY_WS "\n+ \t"
3495 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3499 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3500 if (mod == NULL || ks == NULL) {
3501 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3504 if (keystr == NULL || strlen(keystr) == 0) {
3505 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3511 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3512 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3514 cp += (long)strspn(cp, SWM_KEY_WS);
3515 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3517 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3519 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3521 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3523 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3525 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3527 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3528 *mod |= ControlMask;
3530 *ks = XStringToKeysym(name);
3531 XConvertCase(*ks, ks, &uks);
3532 if (ks == NoSymbol) {
3534 "parsekeys: invalid key %s\n",
3540 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3545 strdupsafe(char *str)
3550 return (strdup(str));
3554 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3557 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3558 keyfuncs[kfid].name, spawn_name);
3560 for (i = 0; i < keys_length; i++) {
3561 if (keys[i].mod == mod && keys[i].keysym == ks) {
3562 if (kfid == kf_invalid) {
3565 "setkeybinding: delete #%d %s\n",
3566 i, keyfuncs[keys[i].funcid].name);
3567 free(keys[i].spawn_name);
3568 j = keys_length - 1;
3572 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3575 /* found: replace */
3577 "setkeybinding: replace #%d %s %s\n",
3578 i, keyfuncs[keys[i].funcid].name,
3580 free(keys[i].spawn_name);
3582 keys[i].keysym = ks;
3583 keys[i].funcid = kfid;
3584 keys[i].spawn_name = strdupsafe(spawn_name);
3585 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3590 if (kfid == kf_invalid) {
3592 "error: setkeybinding: cannot find mod/key combination");
3593 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3596 /* not found: add */
3597 if (keys_size == 0 || keys == NULL) {
3599 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3600 keys = malloc((size_t)keys_size * sizeof(struct key));
3602 fprintf(stderr, "malloc failed\n");
3606 } else if (keys_length == keys_size) {
3608 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3609 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3611 fprintf(stderr, "realloc failed\n");
3616 if (keys_length < keys_size) {
3618 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3619 j, keyfuncs[kfid].name, spawn_name);
3621 keys[j].keysym = ks;
3622 keys[j].funcid = kfid;
3623 keys[j].spawn_name = strdupsafe(spawn_name);
3625 fprintf(stderr, "keys array problem?\n");
3627 fprintf(stderr, "keys array problem\n");
3631 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3635 setconfbinding(char *selector, char *value, int flags)
3637 enum keyfuncid kfid;
3641 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3642 if (selector == NULL) {
3643 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3644 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3646 setkeybinding(mod, ks, kfid, NULL);
3651 /* search by key function name */
3652 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3653 if (strncasecmp(selector, keyfuncs[kfid].name,
3654 SWM_FUNCNAME_LEN) == 0) {
3655 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3657 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3658 setkeybinding(mod, ks, kfid, NULL);
3664 /* search by custom spawn name */
3665 for (i = 0; i < spawns_length; i++) {
3666 if (strcasecmp(selector, spawns[i].name) == 0) {
3667 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3669 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3670 setkeybinding(mod, ks, kf_spawn_custom,
3677 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3684 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3685 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3686 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3687 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3688 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3689 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3690 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3691 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3692 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3693 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3694 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3695 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3696 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3697 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3698 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3699 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3700 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3701 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3702 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3703 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3704 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3705 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3706 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3707 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3708 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3709 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3710 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3711 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3712 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3713 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3714 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3715 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3716 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3717 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3718 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3719 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3720 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3721 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3722 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3723 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3724 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3725 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3726 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3727 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3728 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3729 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3730 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3731 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3732 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3733 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3734 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3735 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3736 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3737 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3739 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3744 updatenumlockmask(void)
3747 XModifierKeymap *modmap;
3749 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3751 modmap = XGetModifierMapping(display);
3752 for (i = 0; i < 8; i++)
3753 for (j = 0; j < modmap->max_keypermod; j++)
3754 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3755 == XKeysymToKeycode(display, XK_Num_Lock))
3756 numlockmask = (1 << i);
3758 XFreeModifiermap(modmap);
3764 unsigned int i, j, k;
3766 unsigned int modifiers[] =
3767 { 0, LockMask, numlockmask, numlockmask | LockMask };
3769 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3770 updatenumlockmask();
3772 for (k = 0; k < ScreenCount(display); k++) {
3773 if (TAILQ_EMPTY(&screens[k].rl))
3775 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3776 for (i = 0; i < keys_length; i++) {
3777 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3778 for (j = 0; j < LENGTH(modifiers); j++)
3779 XGrabKey(display, code,
3780 keys[i].mod | modifiers[j],
3781 screens[k].root, True,
3782 GrabModeAsync, GrabModeAsync);
3788 grabbuttons(struct ws_win *win, int focused)
3791 unsigned int modifiers[] =
3792 { 0, LockMask, numlockmask, numlockmask|LockMask };
3794 updatenumlockmask();
3795 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3797 for (i = 0; i < LENGTH(buttons); i++)
3798 if (buttons[i].action == client_click)
3799 for (j = 0; j < LENGTH(modifiers); j++)
3800 XGrabButton(display, buttons[i].button,
3801 buttons[i].mask | modifiers[j],
3802 win->id, False, BUTTONMASK,
3803 GrabModeAsync, GrabModeSync, None,
3806 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3807 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3810 const char *quirkname[] = {
3811 "NONE", /* config string for "no value" */
3819 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3820 #define SWM_Q_WS "\n|+ \t"
3822 parsequirks(char *qstr, unsigned long *quirk)
3832 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3834 cp += (long)strspn(cp, SWM_Q_WS);
3835 for (i = 0; i < LENGTH(quirkname); i++) {
3836 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3837 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3842 *quirk |= 1 << (i-1);
3846 if (i >= LENGTH(quirkname)) {
3847 DNPRINTF(SWM_D_QUIRK,
3848 "parsequirks: invalid quirk [%s]\n", name);
3856 setquirk(const char *class, const char *name, const int quirk)
3861 for (i = 0; i < quirks_length; i++) {
3862 if (!strcmp(quirks[i].class, class) &&
3863 !strcmp(quirks[i].name, name)) {
3866 DNPRINTF(SWM_D_QUIRK,
3867 "setquirk: delete #%d %s:%s\n",
3868 i, quirks[i].class, quirks[i].name);
3869 free(quirks[i].class);
3870 free(quirks[i].name);
3871 j = quirks_length - 1;
3873 quirks[i] = quirks[j];
3877 /* found: replace */
3878 DNPRINTF(SWM_D_QUIRK,
3879 "setquirk: replace #%d %s:%s\n",
3880 i, quirks[i].class, quirks[i].name);
3881 free(quirks[i].class);
3882 free(quirks[i].name);
3883 quirks[i].class = strdup(class);
3884 quirks[i].name = strdup(name);
3885 quirks[i].quirk = quirk;
3892 "error: setquirk: cannot find class/name combination");
3895 /* not found: add */
3896 if (quirks_size == 0 || quirks == NULL) {
3898 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3899 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3901 fprintf(stderr, "setquirk: malloc failed\n");
3905 } else if (quirks_length == quirks_size) {
3907 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3908 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3910 fprintf(stderr, "setquirk: realloc failed\n");
3915 if (quirks_length < quirks_size) {
3916 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3917 j = quirks_length++;
3918 quirks[j].class = strdup(class);
3919 quirks[j].name = strdup(name);
3920 quirks[j].quirk = quirk;
3922 fprintf(stderr, "quirks array problem?\n");
3924 fprintf(stderr, "quirks array problem!\n");
3931 setconfquirk(char *selector, char *value, int flags)
3933 char *cp, *class, *name;
3935 unsigned long quirks;
3936 if (selector == NULL)
3938 if ((cp = strchr(selector, ':')) == NULL)
3943 if ((retval = parsequirks(value, &quirks)) == 0)
3944 setquirk(class, name, quirks);
3951 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3952 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3953 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3954 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3955 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3956 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3957 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3958 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3959 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3960 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3961 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3962 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3965 /* conf file stuff */
3966 #define SWM_CONF_FILE "scrotwm.conf"
3968 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3969 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3970 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3971 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
3972 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BAR_FONT,
3973 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
3978 setconfvalue(char *selector, char *value, int flags)
3981 case SWM_S_BAR_DELAY:
3982 bar_delay = atoi(value);
3984 case SWM_S_BAR_ENABLED:
3985 bar_enabled = atoi(value);
3987 case SWM_S_BAR_AT_BOTTOM:
3988 bar_at_bottom = atoi(value);
3990 case SWM_S_STACK_ENABLED:
3991 stack_enabled = atoi(value);
3993 case SWM_S_CLOCK_ENABLED:
3994 clock_enabled = atoi(value);
3996 case SWM_S_CLOCK_FORMAT:
3997 #ifndef SWM_DENY_CLOCK_FORMAT
3999 if ((clock_format = strdup(value)) == NULL)
4000 err(1, "setconfvalue: clock_format");
4003 case SWM_S_CYCLE_EMPTY:
4004 cycle_empty = atoi(value);
4006 case SWM_S_CYCLE_VISIBLE:
4007 cycle_visible = atoi(value);
4009 case SWM_S_SS_ENABLED:
4010 ss_enabled = atoi(value);
4012 case SWM_S_TERM_WIDTH:
4013 term_width = atoi(value);
4015 case SWM_S_TITLE_CLASS_ENABLED:
4016 title_class_enabled = atoi(value);
4018 case SWM_S_WINDOW_NAME_ENABLED:
4019 window_name_enabled = atoi(value);
4021 case SWM_S_TITLE_NAME_ENABLED:
4022 title_name_enabled = atoi(value);
4024 case SWM_S_FOCUS_MODE:
4025 if (!strcmp(value, "default"))
4026 focus_mode = SWM_FOCUS_DEFAULT;
4027 else if (!strcmp(value, "follow_cursor"))
4028 focus_mode = SWM_FOCUS_FOLLOW;
4029 else if (!strcmp(value, "synergy"))
4030 focus_mode = SWM_FOCUS_SYNERGY;
4032 err(1, "focus_mode");
4034 case SWM_S_DISABLE_BORDER:
4035 disable_border = atoi(value);
4037 case SWM_S_BAR_FONT:
4039 if ((bar_fonts[0] = strdup(value)) == NULL)
4040 err(1, "setconfvalue: bar_font");
4042 case SWM_S_BAR_ACTION:
4044 if ((bar_argv[0] = strdup(value)) == NULL)
4045 err(1, "setconfvalue: bar_action");
4047 case SWM_S_SPAWN_TERM:
4048 free(spawn_term[0]);
4049 if ((spawn_term[0] = strdup(value)) == NULL)
4050 err(1, "setconfvalue: spawn_term");
4054 case SWM_S_DIALOG_RATIO:
4055 dialog_ratio = atof(value);
4056 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4066 setconfmodkey(char *selector, char *value, int flags)
4068 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4069 update_modkey(Mod1Mask);
4070 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4071 update_modkey(Mod2Mask);
4072 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4073 update_modkey(Mod3Mask);
4074 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4075 update_modkey(Mod4Mask);
4082 setconfcolor(char *selector, char *value, int flags)
4084 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4089 setconfregion(char *selector, char *value, int flags)
4091 custom_region(value);
4095 /* config options */
4096 struct config_option {
4098 int (*func)(char*, char*, int);
4101 struct config_option configopt[] = {
4102 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4103 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4104 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4105 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4106 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4107 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4108 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4109 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4110 { "bind", setconfbinding, 0 },
4111 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4112 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4113 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4114 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4115 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4116 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4117 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4118 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4119 { "modkey", setconfmodkey, 0 },
4120 { "program", setconfspawn, 0 },
4121 { "quirk", setconfquirk, 0 },
4122 { "region", setconfregion, 0 },
4123 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4124 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4125 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4126 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4127 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4128 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4129 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4130 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4131 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4136 conf_load(char *filename)
4139 char *line, *cp, *optsub, *optval;
4140 size_t linelen, lineno = 0;
4141 int wordlen, i, optind;
4142 struct config_option *opt;
4144 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4146 if (filename == NULL) {
4147 fprintf(stderr, "conf_load: no filename\n");
4150 if ((config = fopen(filename, "r")) == NULL) {
4151 warn("conf_load: fopen");
4155 while (!feof(config)) {
4156 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4159 err(1, "%s", filename);
4164 cp += strspn(cp, " \t\n"); /* eat whitespace */
4165 if (cp[0] == '\0') {
4170 /* get config option */
4171 wordlen = strcspn(cp, "=[ \t\n");
4173 warnx("%s: line %zd: no option found",
4178 for (i = 0; i < LENGTH(configopt); i++) {
4179 opt = &configopt[i];
4180 if (!strncasecmp(cp, opt->optname, wordlen) &&
4181 strlen(opt->optname) == wordlen) {
4187 warnx("%s: line %zd: unknown option %.*s",
4188 filename, lineno, wordlen, cp);
4192 cp += strspn(cp, " \t\n"); /* eat whitespace */
4193 /* get [selector] if any */
4197 wordlen = strcspn(cp, "]");
4200 warnx("%s: line %zd: syntax error",
4204 asprintf(&optsub, "%.*s", wordlen, cp);
4207 cp += strspn(cp, "] \t\n"); /* eat trailing */
4209 cp += strspn(cp, "= \t\n"); /* eat trailing */
4211 optval = strdup(cp);
4212 /* call function to deal with it all */
4213 if (configopt[optind].func(optsub, optval,
4214 configopt[optind].funcflags) != 0) {
4215 fprintf(stderr, "%s line %zd: %s\n",
4216 filename, lineno, line);
4217 errx(1, "%s: line %zd: invalid data for %s",
4218 filename, lineno, configopt[optind].optname);
4226 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4232 set_child_transient(struct ws_win *win)
4234 struct ws_win *parent;
4236 parent = find_window(win->transient);
4238 parent->child_trans = win;
4242 manage_window(Window id)
4245 struct workspace *ws;
4246 struct ws_win *win, *ww;
4247 int format, i, ws_idx, n, border_me = 0;
4248 unsigned long nitems, bytes;
4249 Atom ws_idx_atom = 0, type;
4250 Atom *prot = NULL, *pp;
4251 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4252 struct swm_region *r;
4257 if ((win = find_window(id)) != NULL)
4258 return (win); /* already being managed */
4260 /* see if we are on the unmanaged list */
4261 if ((win = find_unmanaged_window(id)) != NULL) {
4262 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4264 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4265 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4267 set_child_transient(win);
4268 ewmh_update_actions(win);
4272 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4273 errx(1, "calloc: failed to allocate memory for new window");
4275 /* Get all the window data in one shot */
4276 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4278 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4279 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4280 XGetWindowAttributes(display, id, &win->wa);
4281 XGetWMNormalHints(display, id, &win->sh, &mask);
4282 XGetTransientForHint(display, id, &trans);
4284 win->transient = trans;
4285 set_child_transient(win);
4286 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4287 win->id, win->transient);
4290 /* get supported protocols */
4291 if (XGetWMProtocols(display, id, &prot, &n)) {
4292 for (i = 0, pp = prot; i < n; i++, pp++) {
4293 if (*pp == takefocus)
4294 win->take_focus = 1;
4296 win->can_delete = 1;
4303 * Figure out where to put the window. If it was previously assigned to
4304 * a workspace (either by spawn() or manually moving), and isn't
4305 * transient, * put it in the same workspace
4307 r = root_to_region(win->wa.root);
4308 if (prop && win->transient == 0) {
4309 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4310 ws_idx = strtonum(prop, 0, 9, &errstr);
4312 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4315 ws = &r->s->ws[ws_idx];
4318 /* this should launch transients in the same ws as parent */
4320 if ((ww = find_window(trans)) != NULL)
4327 "fix this bug mcbride\n");
4332 /* set up the window layout */
4335 win->s = r->s; /* this never changes */
4336 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4338 win->g.w = win->wa.width;
4339 win->g.h = win->wa.height;
4340 win->g.x = win->wa.x;
4341 win->g.y = win->wa.y;
4342 win->g_floatvalid = 0;
4343 win->floatmaxed = 0;
4344 win->ewmh_flags = 0;
4346 /* Set window properties so we can remember this after reincarnation */
4347 if (ws_idx_atom && prop == NULL &&
4348 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4349 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4351 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4352 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4357 ewmh_autoquirk(win);
4359 if (XGetClassHint(display, win->id, &win->ch)) {
4360 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4361 win->ch.res_class, win->ch.res_name);
4363 /* java is retarded so treat it special */
4364 if (strstr(win->ch.res_name, "sun-awt")) {
4369 for (i = 0; i < quirks_length; i++){
4370 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4371 !strcmp(win->ch.res_name, quirks[i].name)) {
4372 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4373 win->ch.res_class, win->ch.res_name);
4374 if (quirks[i].quirk & SWM_Q_FLOAT) {
4378 win->quirks = quirks[i].quirk;
4383 /* alter window position if quirky */
4384 if (win->quirks & SWM_Q_ANYWHERE) {
4385 win->manual = 1; /* don't center the quirky windows */
4386 bzero(&wc, sizeof wc);
4388 if (bar_enabled && win->g.y < bar_height) {
4389 win->g.y = wc.y = bar_height;
4392 if (win->g.w + win->g.x > WIDTH(r)) {
4393 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4399 /* Reset font sizes (the bruteforce way; no default keybinding). */
4400 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4401 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4402 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4403 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4404 fake_keypress(win, XK_KP_Add, ShiftMask);
4407 ewmh_get_win_state(win);
4408 ewmh_update_actions(win);
4409 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4413 bzero(&wc, sizeof wc);
4414 wc.border_width = 1;
4415 mask = CWBorderWidth;
4416 XConfigureWindow(display, win->id, mask, &wc);
4420 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4421 PropertyChangeMask | StructureNotifyMask);
4423 set_win_state(win, NormalState);
4425 /* floaters need to be mapped if they are in the current workspace */
4426 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4427 XMapRaised(display, win->id);
4433 free_window(struct ws_win *win)
4435 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4440 /* needed for restart wm */
4441 set_win_state(win, WithdrawnState);
4443 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4445 if (win->ch.res_class)
4446 XFree(win->ch.res_class);
4447 if (win->ch.res_name)
4448 XFree(win->ch.res_name);
4453 memset(win, 0xff, sizeof *win); /* XXX kill later */
4459 unmanage_window(struct ws_win *win)
4461 struct ws_win *parent;
4466 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4468 if (win->transient) {
4469 parent = find_window(win->transient);
4471 parent->child_trans = NULL;
4474 /* focus on root just in case */
4475 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4480 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4481 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4487 focus_magic(struct ws_win *win, int do_trans)
4489 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4494 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4495 /* win = parent & has a transient so focus on that */
4497 focus_win(win->child_trans);
4498 if (win->child_trans->take_focus)
4499 client_msg(win, takefocus);
4501 /* make sure transient hasn't dissapeared */
4502 if (validate_win(win->child_trans) == 0) {
4503 focus_win(win->child_trans);
4504 if (win->child_trans->take_focus)
4505 client_msg(win->child_trans, takefocus);
4507 win->child_trans = NULL;
4509 if (win->take_focus)
4510 client_msg(win, takefocus);
4516 if (win->take_focus)
4517 client_msg(win, takefocus);
4524 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4532 XKeyEvent *ev = &e->xkey;
4534 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4536 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4537 for (i = 0; i < keys_length; i++)
4538 if (keysym == keys[i].keysym
4539 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4540 && keyfuncs[keys[i].funcid].func) {
4541 if (keys[i].funcid == kf_spawn_custom)
4543 root_to_region(ev->root),
4544 &(keyfuncs[keys[i].funcid].args),
4548 keyfuncs[keys[i].funcid].func(
4549 root_to_region(ev->root),
4550 &(keyfuncs[keys[i].funcid].args)
4556 buttonpress(XEvent *e)
4560 XButtonPressedEvent *ev = &e->xbutton;
4562 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4564 action = root_click;
4565 if ((win = find_window(ev->window)) == NULL)
4568 focus_magic(win, SWM_F_TRANSIENT);
4569 action = client_click;
4571 for (i = 0; i < LENGTH(buttons); i++)
4572 if (action == buttons[i].action && buttons[i].func &&
4573 buttons[i].button == ev->button &&
4574 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4575 buttons[i].func(win, &buttons[i].args);
4579 configurerequest(XEvent *e)
4581 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4586 if ((win = find_window(ev->window)) == NULL)
4587 if ((win = find_unmanaged_window(ev->window)) == NULL)
4591 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4593 bzero(&wc, sizeof wc);
4596 wc.width = ev->width;
4597 wc.height = ev->height;
4598 wc.border_width = ev->border_width;
4599 wc.sibling = ev->above;
4600 wc.stack_mode = ev->detail;
4601 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4603 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4605 if (win->floating) {
4606 if (ev->value_mask & CWX)
4608 if (ev->value_mask & CWY)
4610 if (ev->value_mask & CWWidth)
4611 win->g.w = ev->width;
4612 if (ev->value_mask & CWHeight)
4613 win->g.h = ev->height;
4620 configurenotify(XEvent *e)
4625 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4626 e->xconfigure.window);
4628 win = find_window(e->xconfigure.window);
4630 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4638 destroynotify(XEvent *e)
4641 XDestroyWindowEvent *ev = &e->xdestroywindow;
4643 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4645 if ((win = find_window(ev->window)) == NULL) {
4646 if ((win = find_unmanaged_window(ev->window)) == NULL)
4652 /* make sure we focus on something */
4655 unmanage_window(win);
4661 enternotify(XEvent *e)
4663 XCrossingEvent *ev = &e->xcrossing;
4668 Window focus_return;
4669 int revert_to_return;
4671 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4672 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4673 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4674 ev->same_screen, ev->focus, ev->state);
4676 switch (focus_mode) {
4677 case SWM_FOCUS_DEFAULT:
4678 if (QLength(display)) {
4679 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4684 case SWM_FOCUS_FOLLOW:
4686 case SWM_FOCUS_SYNERGY:
4689 * all these checks need to be in this order because the
4690 * XCheckTypedWindowEvent relies on weeding out the previous events
4692 * making this code an option would enable a follow mouse for focus
4697 * state is set when we are switching workspaces and focus is set when
4698 * the window or a subwindow already has focus (occurs during restart).
4700 * Only honor the focus flag if last_focus_event is not FocusOut,
4701 * this allows scrotwm to continue to control focus when another
4702 * program is also playing with it.
4704 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4705 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4710 * happens when a window is created or destroyed and the border
4711 * crosses the mouse pointer and when switching ws
4713 * we need the subwindow test to see if we came from root in order
4714 * to give focus to floaters
4716 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4717 ev->subwindow == 0) {
4718 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4722 /* this window already has focus */
4723 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4724 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4728 /* this window is being deleted or moved to another ws */
4729 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4731 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4732 XPutBackEvent(display, &cne);
4736 if ((win = find_window(ev->window)) == NULL) {
4737 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4742 * In fullstack kill all enters unless they come from a different ws
4743 * (i.e. another region) or focus has been grabbed externally.
4745 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4746 last_focus_event != FocusOut) {
4747 XGetInputFocus(display, &focus_return, &revert_to_return);
4748 if ((w = find_window(focus_return)) == NULL ||
4750 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4758 if ((win = find_window(ev->window)) == NULL) {
4759 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4764 * if we have more enternotifies let them handle it in due time
4766 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4767 DNPRINTF(SWM_D_EVENT,
4768 "ignoring enternotify: got more enternotify\n");
4769 XPutBackEvent(display, &cne);
4773 focus_magic(win, SWM_F_TRANSIENT);
4776 /* lets us use one switch statement for arbitrary mode/detail combinations */
4777 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4780 focusevent(XEvent *e)
4784 u_int32_t mode_detail;
4785 XFocusChangeEvent *ev = &e->xfocus;
4787 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4788 ev->type == FocusIn ? "entering" : "leaving",
4789 ev->window, ev->mode, ev->detail);
4791 if (last_focus_event == ev->type) {
4792 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4796 last_focus_event = ev->type;
4797 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4799 switch (mode_detail) {
4800 /* synergy client focus operations */
4801 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4802 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4804 /* synergy server focus operations */
4805 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4807 /* Entering applications like rdesktop that mangle the pointer */
4808 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4810 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4811 XSetWindowBorder(display, win->id,
4812 win->ws->r->s->c[ev->type == FocusIn ?
4813 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4816 fprintf(stderr, "ignoring focusevent\n");
4817 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4824 mapnotify(XEvent *e)
4827 XMapEvent *ev = &e->xmap;
4829 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4831 win = manage_window(ev->window);
4833 set_win_state(win, NormalState);
4837 mappingnotify(XEvent *e)
4839 XMappingEvent *ev = &e->xmapping;
4841 XRefreshKeyboardMapping(ev);
4842 if (ev->request == MappingKeyboard)
4847 maprequest(XEvent *e)
4850 struct swm_region *r;
4851 XWindowAttributes wa;
4852 XMapRequestEvent *ev = &e->xmaprequest;
4854 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4855 e->xmaprequest.window);
4857 if (!XGetWindowAttributes(display, ev->window, &wa))
4859 if (wa.override_redirect)
4862 win = manage_window(e->xmaprequest.window);
4864 return; /* can't happen */
4868 /* make new win focused */
4869 r = root_to_region(win->wa.root);
4870 if (win->ws == r->ws)
4871 focus_magic(win, SWM_F_GENERIC);
4875 propertynotify(XEvent *e)
4878 XPropertyEvent *ev = &e->xproperty;
4880 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4883 if (ev->state == PropertyDelete)
4884 return; /* ignore */
4885 win = find_window(ev->window);
4890 case XA_WM_NORMAL_HINTS:
4893 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4894 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4895 if (win->sh.flags & PMinSize) {
4896 win->g.w = win->sh.min_width;
4897 win->g.h = win->sh.min_height;
4898 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4900 XMoveResizeWindow(display, win->id,
4901 win->g.x, win->g.y, win->g.w, win->g.h);
4903 if (window_name_enabled)
4912 unmapnotify(XEvent *e)
4916 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4918 /* determine if we need to help unmanage this window */
4919 win = find_window(e->xunmap.window);
4923 if (getstate(e->xunmap.window) == NormalState) {
4924 unmanage_window(win);
4930 visibilitynotify(XEvent *e)
4933 struct swm_region *r;
4935 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4936 e->xvisibility.window);
4937 if (e->xvisibility.state == VisibilityUnobscured)
4938 for (i = 0; i < ScreenCount(display); i++)
4939 TAILQ_FOREACH(r, &screens[i].rl, entry)
4940 if (e->xvisibility.window == r->bar_window)
4945 clientmessage(XEvent *e)
4947 XClientMessageEvent *ev;
4952 win = find_window(ev->window);
4956 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
4957 ev->window, ev->message_type);
4959 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
4960 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
4963 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
4964 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
4965 if (win->can_delete)
4966 client_msg(win, adelete);
4968 XKillClient(display, win->id);
4970 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
4971 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
4972 if (win->floating) {
4973 if (ev->data.l[0] & (1<<8)) /* x */
4974 win->g.x = ev->data.l[1];
4975 if (ev->data.l[0] & (1<<9)) /* y */
4976 win->g.y = ev->data.l[2];
4977 if (ev->data.l[0] & (1<<10)) /* width */
4978 win->g.w = ev->data.l[3];
4979 if (ev->data.l[0] & (1<<11)) /* height */
4980 win->g.h = ev->data.l[4];
4983 /* TODO: Change stack sizes */
4987 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
4988 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
4989 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
4991 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
4998 xerror_start(Display *d, XErrorEvent *ee)
5005 xerror(Display *d, XErrorEvent *ee)
5007 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5015 xerrorxlib = XSetErrorHandler(xerror_start);
5017 /* this causes an error if some other window manager is running */
5018 XSelectInput(display, DefaultRootWindow(display),
5019 SubstructureRedirectMask);
5020 XSync(display, False);
5024 XSetErrorHandler(xerror);
5025 XSync(display, False);
5030 new_region(struct swm_screen *s, int x, int y, int w, int h)
5032 struct swm_region *r, *n;
5033 struct workspace *ws = NULL;
5036 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5037 s->idx, w, h, x, y);
5039 /* remove any conflicting regions */
5040 n = TAILQ_FIRST(&s->rl);
5043 n = TAILQ_NEXT(r, entry);
5044 if (X(r) < (x + w) &&
5045 (X(r) + WIDTH(r)) > x &&
5047 (Y(r) + HEIGHT(r)) > y) {
5048 if (r->ws->r != NULL)
5049 r->ws->old_r = r->ws->r;
5051 XDestroyWindow(display, r->bar_window);
5052 TAILQ_REMOVE(&s->rl, r, entry);
5053 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5057 /* search old regions for one to reuse */
5059 /* size + location match */
5060 TAILQ_FOREACH(r, &s->orl, entry)
5061 if (X(r) == x && Y(r) == y &&
5062 HEIGHT(r) == h && WIDTH(r) == w)
5066 TAILQ_FOREACH(r, &s->orl, entry)
5067 if (HEIGHT(r) == h && WIDTH(r) == w)
5071 TAILQ_REMOVE(&s->orl, r, entry);
5072 /* try to use old region's workspace */
5073 if (r->ws->r == NULL)
5076 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5077 errx(1, "calloc: failed to allocate memory for screen");
5079 /* if we don't have a workspace already, find one */
5081 for (i = 0; i < SWM_WS_MAX; i++)
5082 if (s->ws[i].r == NULL) {
5089 errx(1, "no free workspaces\n");
5100 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5106 #ifdef SWM_XRR_HAS_CRTC
5108 XRRScreenResources *sr;
5111 #endif /* SWM_XRR_HAS_CRTC */
5112 struct swm_region *r;
5115 if (i >= ScreenCount(display))
5116 errx(1, "invalid screen");
5118 /* remove any old regions */
5119 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5120 r->ws->old_r = r->ws->r = NULL;
5121 XDestroyWindow(display, r->bar_window);
5122 TAILQ_REMOVE(&screens[i].rl, r, entry);
5123 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5127 /* map virtual screens onto physical screens */
5128 #ifdef SWM_XRR_HAS_CRTC
5129 if (xrandr_support) {
5130 sr = XRRGetScreenResources(display, screens[i].root);
5132 new_region(&screens[i], 0, 0,
5133 DisplayWidth(display, i),
5134 DisplayHeight(display, i));
5138 for (c = 0, ci = NULL; c < ncrtc; c++) {
5139 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5140 if (ci->noutput == 0)
5143 if (ci != NULL && ci->mode == None)
5144 new_region(&screens[i], 0, 0,
5145 DisplayWidth(display, i),
5146 DisplayHeight(display, i));
5148 new_region(&screens[i],
5149 ci->x, ci->y, ci->width, ci->height);
5152 XRRFreeCrtcInfo(ci);
5153 XRRFreeScreenResources(sr);
5155 #endif /* SWM_XRR_HAS_CRTC */
5157 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5158 DisplayHeight(display, i));
5163 screenchange(XEvent *e) {
5164 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5165 struct swm_region *r;
5168 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5170 if (!XRRUpdateConfiguration(e))
5173 /* silly event doesn't include the screen index */
5174 for (i = 0; i < ScreenCount(display); i++)
5175 if (screens[i].root == xe->root)
5177 if (i >= ScreenCount(display))
5178 errx(1, "screenchange: screen not found\n");
5180 /* brute force for now, just re-enumerate the regions */
5183 /* add bars to all regions */
5184 for (i = 0; i < ScreenCount(display); i++)
5185 TAILQ_FOREACH(r, &screens[i].rl, entry)
5193 Window d1, d2, *wins = NULL;
5194 XWindowAttributes wa;
5199 for (i = 0; i < ScreenCount(display); i++) {
5200 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5203 /* attach windows to a region */
5204 /* normal windows */
5205 for (j = 0; j < no; j++) {
5206 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5207 wa.override_redirect ||
5208 XGetTransientForHint(display, wins[j], &d1))
5211 state = getstate(wins[j]);
5212 manage = state == IconicState;
5213 if (wa.map_state == IsViewable || manage)
5214 manage_window(wins[j]);
5216 /* transient windows */
5217 for (j = 0; j < no; j++) {
5218 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5219 wa.override_redirect)
5222 state = getstate(wins[j]);
5223 manage = state == IconicState;
5224 if (XGetTransientForHint(display, wins[j], &d1) &&
5226 manage_window(wins[j]);
5239 int errorbase, major, minor;
5240 struct workspace *ws;
5243 if ((screens = calloc(ScreenCount(display),
5244 sizeof(struct swm_screen))) == NULL)
5245 errx(1, "calloc: screens");
5247 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5249 /* initial Xrandr setup */
5250 xrandr_support = XRRQueryExtension(display,
5251 &xrandr_eventbase, &errorbase);
5253 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5256 /* map physical screens */
5257 for (i = 0; i < ScreenCount(display); i++) {
5258 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5260 TAILQ_INIT(&screens[i].rl);
5261 TAILQ_INIT(&screens[i].orl);
5262 screens[i].root = RootWindow(display, i);
5264 /* set default colors */
5265 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5266 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5267 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5268 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5269 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5271 /* set default cursor */
5272 XDefineCursor(display, screens[i].root,
5273 XCreateFontCursor(display, XC_left_ptr));
5275 /* init all workspaces */
5276 /* XXX these should be dynamically allocated too */
5277 for (j = 0; j < SWM_WS_MAX; j++) {
5278 ws = &screens[i].ws[j];
5283 TAILQ_INIT(&ws->winlist);
5284 TAILQ_INIT(&ws->unmanagedlist);
5286 for (k = 0; layouts[k].l_stack != NULL; k++)
5287 if (layouts[k].l_config != NULL)
5288 layouts[k].l_config(ws,
5289 SWM_ARG_ID_STACKINIT);
5290 ws->cur_layout = &layouts[0];
5296 XRRSelectInput(display, screens[i].root,
5297 RRScreenChangeNotifyMask);
5304 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5306 err(1, "setup_globals: strdup");
5307 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5309 err(1, "setup_globals: strdup");
5310 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5312 err(1, "setup_globals: strdup");
5313 if ((spawn_term[0] = strdup("xterm")) == NULL)
5314 err(1, "setup_globals: strdup");
5315 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5323 Atom netwmcheck, netwmname, utf8_string;
5326 /* work around sun jdk bugs, code from wmname */
5327 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5328 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5329 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5330 for (i = 0; i < ScreenCount(display); i++) {
5331 root = screens[i].root;
5332 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5333 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5334 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5336 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5337 PropModeReplace, (unsigned char *)&win,1);
5338 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5339 PropModeReplace, (unsigned char *)&win,1);
5340 XChangeProperty(display, win, netwmname, utf8_string, 8,
5341 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5346 main(int argc, char *argv[])
5349 struct swm_region *r, *rr;
5350 struct ws_win *winfocus = NULL;
5353 char conf[PATH_MAX], *cfile = NULL;
5358 struct sigaction sact;
5361 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5362 SWM_VERSION, cvstag);
5363 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5364 warnx("no locale support");
5366 if (!(display = XOpenDisplay(0)))
5367 errx(1, "can not open display");
5370 errx(1, "other wm running");
5372 /* handle some signals */
5373 bzero(&sact, sizeof(sact));
5374 sigemptyset(&sact.sa_mask);
5376 sact.sa_handler = sighdlr;
5377 sigaction(SIGINT, &sact, NULL);
5378 sigaction(SIGQUIT, &sact, NULL);
5379 sigaction(SIGTERM, &sact, NULL);
5380 sigaction(SIGHUP, &sact, NULL);
5382 sact.sa_handler = sighdlr;
5383 sact.sa_flags = SA_NOCLDSTOP;
5384 sigaction(SIGCHLD, &sact, NULL);
5386 astate = XInternAtom(display, "WM_STATE", False);
5387 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5388 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5389 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5391 /* look for local and global conf file */
5392 pwd = getpwuid(getuid());
5394 errx(1, "invalid user %d", getuid());
5403 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5404 if (stat(conf, &sb) != -1) {
5405 if (S_ISREG(sb.st_mode))
5408 /* try global conf file */
5409 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5410 if (!stat(conf, &sb))
5411 if (S_ISREG(sb.st_mode))
5418 /* set some values to work around bad programs */
5421 /* grab existing windows (before we build the bars) */
5424 /* setup all bars */
5425 for (i = 0; i < ScreenCount(display); i++)
5426 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5427 if (winfocus == NULL)
5428 winfocus = TAILQ_FIRST(&r->ws->winlist);
5437 xfd = ConnectionNumber(display);
5439 while (XPending(display)) {
5440 XNextEvent(display, &e);
5443 if (e.type < LASTEvent) {
5445 if (handler[e.type])
5446 handler[e.type](&e);
5448 DNPRINTF(SWM_D_EVENT,
5449 "win: %lu unknown event: %d\n",
5450 e.xany.window, e.type);
5452 switch (e.type - xrandr_eventbase) {
5453 case RRScreenChangeNotify:
5457 DNPRINTF(SWM_D_EVENT,
5458 "win: %lu unknown xrandr event: "
5459 "%d\n", e.xany.window, e.type);
5465 /* if we are being restarted go focus on first window */
5467 rr = winfocus->ws->r;
5469 /* not a visible window */
5473 /* move pointer to first screen if multi screen */
5474 if (ScreenCount(display) > 1 || outputs > 1)
5475 XWarpPointer(display, None, rr->s[0].root,
5476 0, 0, 0, 0, rr->g.x,
5477 rr->g.y + (bar_enabled ? bar_height : 0));
5479 a.id = SWM_ARG_ID_FOCUSCUR;
5489 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5491 DNPRINTF(SWM_D_MISC, "select failed");
5492 if (restart_wm == 1)
5493 restart(NULL, NULL);
5504 XFreeGC(display, bar_gc);
5505 XCloseDisplay(display);