3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
8 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
55 static const char *cvstag =
58 #define SWM_VERSION "0.9.34"
75 #include <sys/types.h>
79 #include <sys/queue.h>
80 #include <sys/param.h>
81 #include <sys/select.h>
83 #include <X11/cursorfont.h>
84 #include <X11/keysym.h>
85 #include <X11/Xatom.h>
87 #include <X11/Xproto.h>
88 #include <X11/Xutil.h>
89 #include <X11/extensions/Xrandr.h>
90 #include <X11/extensions/XTest.h>
97 # error XRandR versions less than 1.0 are not supported
102 #define SWM_XRR_HAS_CRTC
106 /*#define SWM_DEBUG*/
108 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
109 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
110 #define SWM_D_MISC 0x0001
111 #define SWM_D_EVENT 0x0002
112 #define SWM_D_WS 0x0004
113 #define SWM_D_FOCUS 0x0008
114 #define SWM_D_MOVE 0x0010
115 #define SWM_D_STACK 0x0020
116 #define SWM_D_MOUSE 0x0040
117 #define SWM_D_PROP 0x0080
118 #define SWM_D_CLASS 0x0100
119 #define SWM_D_KEY 0x0200
120 #define SWM_D_QUIRK 0x0400
121 #define SWM_D_SPAWN 0x0800
122 #define SWM_D_EVENTQ 0x1000
123 #define SWM_D_CONF 0x2000
125 u_int32_t swm_debug = 0
142 #define DPRINTF(x...)
143 #define DNPRINTF(n,x...)
146 #define LENGTH(x) (sizeof x / sizeof x[0])
147 #define MODKEY Mod1Mask
148 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
149 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
150 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
151 #define SWM_PROPLEN (16)
152 #define SWM_FUNCNAME_LEN (32)
153 #define SWM_KEYS_LEN (255)
154 #define SWM_QUIRK_LEN (64)
155 #define X(r) (r)->g.x
156 #define Y(r) (r)->g.y
157 #define WIDTH(r) (r)->g.w
158 #define HEIGHT(r) (r)->g.h
159 #define SWM_MAX_FONT_STEPS (3)
160 #define WINID(w) (w ? w->id : 0)
162 #define SWM_FOCUS_DEFAULT (0)
163 #define SWM_FOCUS_SYNERGY (1)
164 #define SWM_FOCUS_FOLLOW (2)
167 #define SWM_LIB "/usr/local/lib/libswmhack.so"
179 volatile sig_atomic_t running = 1;
180 volatile sig_atomic_t restart_wm = 0;
182 int last_focus_event = FocusOut;
183 int (*xerrorxlib)(Display *, XErrorEvent *);
187 int xrandr_eventbase;
188 unsigned int numlockmask = 0;
192 int cycle_visible = 0;
194 int font_adjusted = 0;
195 unsigned int mod_key = MODKEY;
198 struct swm_region *search_r;
199 int select_list_pipe[2];
200 int select_resp_pipe[2];
202 volatile sig_atomic_t search_resp;
205 double dialog_ratio = .6;
207 #define SWM_BAR_MAX (256)
208 char *bar_argv[] = { NULL, NULL };
210 char bar_ext[SWM_BAR_MAX];
211 char bar_vertext[SWM_BAR_MAX];
213 sig_atomic_t bar_alarm = 0;
216 int bar_border_width = 1;
217 int bar_at_bottom = 0;
219 int bar_extra_running = 0;
222 int stack_enabled = 1;
223 int clock_enabled = 1;
224 int urgent_enabled = 0;
225 char *clock_format = NULL;
226 int title_name_enabled = 0;
227 int title_class_enabled = 0;
228 int window_name_enabled = 0;
229 int focus_mode = SWM_FOCUS_DEFAULT;
230 int disable_border = 0;
231 int border_width = 1;
232 int verbose_layout = 0;
238 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
239 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
241 #define SWM_MENU_FN (2)
242 #define SWM_MENU_NB (4)
243 #define SWM_MENU_NF (6)
244 #define SWM_MENU_SB (8)
245 #define SWM_MENU_SF (10)
247 /* layout manager data */
248 struct swm_geometry {
258 /* virtual "screens" */
260 TAILQ_ENTRY(swm_region) entry;
261 struct swm_geometry g;
262 struct workspace *ws; /* current workspace on this region */
263 struct workspace *ws_prior; /* prior workspace on this region */
264 struct swm_screen *s; /* screen idx */
267 TAILQ_HEAD(swm_region_list, swm_region);
270 TAILQ_ENTRY(ws_win) entry;
273 struct ws_win *child_trans; /* transient child window */
274 struct swm_geometry g; /* current geometry */
275 struct swm_geometry g_float; /* geometry when floating */
276 struct swm_geometry rg_float; /* region geom when floating */
277 int g_floatvalid; /* flag: geometry in g_float is valid */
278 int floatmaxed; /* flag: floater was maxed in max_stack */
282 unsigned int ewmh_flags;
283 int font_size_boundary[SWM_MAX_FONT_STEPS];
289 unsigned long quirks;
290 struct workspace *ws; /* always valid */
291 struct swm_screen *s; /* always valid, never changes */
292 XWindowAttributes wa;
297 TAILQ_HEAD(ws_win_list, ws_win);
301 TAILQ_ENTRY(pid_e) entry;
305 TAILQ_HEAD(pid_list, pid_e);
306 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
308 /* layout handlers */
310 void vertical_config(struct workspace *, int);
311 void vertical_stack(struct workspace *, struct swm_geometry *);
312 void horizontal_config(struct workspace *, int);
313 void horizontal_stack(struct workspace *, struct swm_geometry *);
314 void max_stack(struct workspace *, struct swm_geometry *);
315 void plain_stacker(struct workspace *);
316 void fancy_stacker(struct workspace *);
318 struct ws_win *find_window(Window);
320 void grabbuttons(struct ws_win *, int);
321 void new_region(struct swm_screen *, int, int, int, int);
322 void unmanage_window(struct ws_win *);
323 long getstate(Window);
326 void (*l_stack)(struct workspace *, struct swm_geometry *);
327 void (*l_config)(struct workspace *, int);
329 #define SWM_L_FOCUSPREV (1<<0)
330 #define SWM_L_MAPONFOCUS (1<<1)
331 void (*l_string)(struct workspace *);
333 /* stack, configure */
334 { vertical_stack, vertical_config, 0, plain_stacker },
335 { horizontal_stack, horizontal_config, 0, plain_stacker },
337 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
338 { NULL, NULL, 0, NULL },
341 /* position of max_stack mode in the layouts array, index into layouts! */
342 #define SWM_V_STACK (0)
343 #define SWM_H_STACK (1)
344 #define SWM_MAX_STACK (2)
346 #define SWM_H_SLICE (32)
347 #define SWM_V_SLICE (32)
349 /* define work spaces */
351 int idx; /* workspace index */
352 int always_raise; /* raise windows on focus */
353 struct layout *cur_layout; /* current layout handlers */
354 struct ws_win *focus; /* may be NULL */
355 struct ws_win *focus_prev; /* may be NULL */
356 struct swm_region *r; /* may be NULL */
357 struct swm_region *old_r; /* may be NULL */
358 struct ws_win_list winlist; /* list of windows in ws */
359 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
360 char stacker[10]; /* display stacker and layout */
364 int horizontal_msize;
366 int horizontal_stacks;
373 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
374 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
376 /* physical screen mapping */
377 #define SWM_WS_MAX (10)
379 int idx; /* screen index */
380 struct swm_region_list rl; /* list of regions on this screen */
381 struct swm_region_list orl; /* list of old regions */
383 struct workspace ws[SWM_WS_MAX];
389 } c[SWM_S_COLOR_MAX];
391 struct swm_screen *screens;
394 /* args to functions */
397 #define SWM_ARG_ID_FOCUSNEXT (0)
398 #define SWM_ARG_ID_FOCUSPREV (1)
399 #define SWM_ARG_ID_FOCUSMAIN (2)
400 #define SWM_ARG_ID_FOCUSCUR (4)
401 #define SWM_ARG_ID_SWAPNEXT (10)
402 #define SWM_ARG_ID_SWAPPREV (11)
403 #define SWM_ARG_ID_SWAPMAIN (12)
404 #define SWM_ARG_ID_MOVELAST (13)
405 #define SWM_ARG_ID_MASTERSHRINK (20)
406 #define SWM_ARG_ID_MASTERGROW (21)
407 #define SWM_ARG_ID_MASTERADD (22)
408 #define SWM_ARG_ID_MASTERDEL (23)
409 #define SWM_ARG_ID_STACKRESET (30)
410 #define SWM_ARG_ID_STACKINIT (31)
411 #define SWM_ARG_ID_CYCLEWS_UP (40)
412 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
413 #define SWM_ARG_ID_CYCLESC_UP (42)
414 #define SWM_ARG_ID_CYCLESC_DOWN (43)
415 #define SWM_ARG_ID_STACKINC (50)
416 #define SWM_ARG_ID_STACKDEC (51)
417 #define SWM_ARG_ID_SS_ALL (60)
418 #define SWM_ARG_ID_SS_WINDOW (61)
419 #define SWM_ARG_ID_DONTCENTER (70)
420 #define SWM_ARG_ID_CENTER (71)
421 #define SWM_ARG_ID_KILLWINDOW (80)
422 #define SWM_ARG_ID_DELETEWINDOW (81)
423 #define SWM_ARG_ID_WIDTHGROW (90)
424 #define SWM_ARG_ID_WIDTHSHRINK (91)
425 #define SWM_ARG_ID_HEIGHTGROW (92)
426 #define SWM_ARG_ID_HEIGHTSHRINK (93)
427 #define SWM_ARG_ID_MOVEUP (100)
428 #define SWM_ARG_ID_MOVEDOWN (101)
429 #define SWM_ARG_ID_MOVELEFT (102)
430 #define SWM_ARG_ID_MOVERIGHT (103)
434 void focus(struct swm_region *, union arg *);
435 void focus_magic(struct ws_win *);
442 #define SWM_Q_FLOAT (1<<0) /* float this window */
443 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
444 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
445 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
446 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
447 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
449 int quirks_size = 0, quirks_length = 0;
450 struct quirk *quirks = NULL;
453 * Supported EWMH hints should be added to
454 * both the enum and the ewmh array
456 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
457 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
458 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
459 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
460 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
461 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
462 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
463 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
464 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
465 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
471 } ewmh[SWM_EWMH_HINT_MAX] = {
472 /* must be in same order as in the enum */
473 {"_NET_ACTIVE_WINDOW", None},
474 {"_NET_MOVERESIZE_WINDOW", None},
475 {"_NET_CLOSE_WINDOW", None},
476 {"_NET_WM_WINDOW_TYPE", None},
477 {"_NET_WM_WINDOW_TYPE_DOCK", None},
478 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
479 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
480 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
481 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
482 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
483 {"_NET_WM_STATE", None},
484 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
485 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
486 {"_NET_WM_STATE_SKIP_TASKBAR", None},
487 {"_NET_WM_STATE_SKIP_PAGER", None},
488 {"_NET_WM_STATE_HIDDEN", None},
489 {"_NET_WM_STATE_ABOVE", None},
490 {"_SWM_WM_STATE_MANUAL", None},
491 {"_NET_WM_STATE_FULLSCREEN", None},
492 {"_NET_WM_ALLOWED_ACTIONS", None},
493 {"_NET_WM_ACTION_MOVE", None},
494 {"_NET_WM_ACTION_RESIZE", None},
495 {"_NET_WM_ACTION_FULLSCREEN", None},
496 {"_NET_WM_ACTION_CLOSE", None},
499 void store_float_geom(struct ws_win *win, struct swm_region *r);
500 int floating_toggle_win(struct ws_win *win);
501 void spawn_select(struct swm_region *, union arg *, char *, int *);
504 get_property(Window id, Atom atom, long count, Atom type,
505 unsigned long *n, unsigned char **data)
508 unsigned long tmp, extra;
509 unsigned long *nitems;
512 nitems = n != NULL ? n : &tmp;
513 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
514 &real, &format, nitems, &extra, data);
516 if (status != Success)
525 update_iconic(struct ws_win *win, int newv)
532 iprop = XInternAtom(display, "_SWM_ICONIC", False);
536 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
537 PropModeReplace, (unsigned char *)&v, 1);
539 XDeleteProperty(display, win->id, iprop);
543 get_iconic(struct ws_win *win)
548 unsigned long nitems, extra;
549 unsigned char *prop = NULL;
551 iprop = XInternAtom(display, "_SWM_ICONIC", False);
554 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
555 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
556 if (status != Success)
558 if (rettype != XA_INTEGER || retfmt != 32)
562 v = *((int32_t *)prop);
576 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
578 for (i = 0; i < LENGTH(ewmh); i++)
579 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
581 for (i = 0; i < ScreenCount(display); i++) {
582 /* Support check window will be created by workaround(). */
584 /* Report supported atoms */
585 XDeleteProperty(display, screens[i].root, sup_list);
586 for (j = 0; j < LENGTH(ewmh); j++)
587 XChangeProperty(display, screens[i].root,
588 sup_list, XA_ATOM, 32,
589 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
597 unsigned char *data = NULL;
599 Atom sup_check, sup_list;
602 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
603 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
605 for (i = 0; i < ScreenCount(display); i++) {
606 /* Get the support check window and destroy it */
607 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
612 XDestroyWindow(display, id);
613 XDeleteProperty(display, screens[i].root, sup_check);
614 XDeleteProperty(display, screens[i].root, sup_list);
622 ewmh_autoquirk(struct ws_win *win)
625 unsigned long *data = NULL, n;
628 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
629 XA_ATOM, &n, (void *)&data);
636 for (i = 0; i < n; i++) {
638 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
640 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
641 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
642 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
644 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
647 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
648 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
650 win->quirks = SWM_Q_FLOAT;
658 #define SWM_EWMH_ACTION_COUNT_MAX (6)
659 #define EWMH_F_FULLSCREEN (1<<0)
660 #define EWMH_F_ABOVE (1<<1)
661 #define EWMH_F_HIDDEN (1<<2)
662 #define EWMH_F_SKIP_PAGER (1<<3)
663 #define EWMH_F_SKIP_TASKBAR (1<<4)
664 #define SWM_F_MANUAL (1<<5)
667 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
669 struct swm_geometry rg;
677 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
683 store_float_geom(win, win->ws->r);
690 if (win->g_floatvalid) {
691 /* refloat at last floating relative position */
692 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
693 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
694 win->g.w = win->g_float.w;
695 win->g.h = win->g_float.h;
703 ewmh_update_actions(struct ws_win *win)
705 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
711 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
714 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
715 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
718 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
719 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
722 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
723 #define _NET_WM_STATE_ADD 1 /* add/set property */
724 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
727 ewmh_update_win_state(struct ws_win *win, long state, long action)
729 unsigned int mask = 0;
730 unsigned int changed = 0;
731 unsigned int orig_flags;
736 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
737 mask = EWMH_F_FULLSCREEN;
738 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
740 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
742 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
743 mask = EWMH_F_SKIP_PAGER;
744 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
745 mask = EWMH_F_SKIP_TASKBAR;
748 orig_flags = win->ewmh_flags;
751 case _NET_WM_STATE_REMOVE:
752 win->ewmh_flags &= ~mask;
754 case _NET_WM_STATE_ADD:
755 win->ewmh_flags |= mask;
757 case _NET_WM_STATE_TOGGLE:
758 win->ewmh_flags ^= mask;
762 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
764 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
766 if (!floating_toggle_win(win))
767 win->ewmh_flags = orig_flags; /* revert */
768 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
770 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
771 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
773 if (!ewmh_set_win_fullscreen(win,
774 win->ewmh_flags & EWMH_F_FULLSCREEN))
775 win->ewmh_flags = orig_flags; /* revert */
777 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
779 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
780 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
781 XA_ATOM, 32, PropModeAppend,
782 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
783 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
784 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
785 XA_ATOM, 32, PropModeAppend,
786 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
787 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
788 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
789 XA_ATOM, 32, PropModeAppend,
790 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
791 if (win->ewmh_flags & EWMH_F_ABOVE)
792 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
793 XA_ATOM, 32, PropModeAppend,
794 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
795 if (win->ewmh_flags & SWM_F_MANUAL)
796 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
797 XA_ATOM, 32, PropModeAppend,
798 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
802 ewmh_get_win_state(struct ws_win *win)
813 win->ewmh_flags |= EWMH_F_ABOVE;
815 win->ewmh_flags |= SWM_F_MANUAL;
817 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
818 (~0L), XA_ATOM, &n, (void *)&states);
823 for (i = 0; i < n; i++)
824 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
844 name = "ButtonPress";
847 name = "ButtonRelease";
850 name = "MotionNotify";
853 name = "EnterNotify";
856 name = "LeaveNotify";
865 name = "KeymapNotify";
871 name = "GraphicsExpose";
876 case VisibilityNotify:
877 name = "VisibilityNotify";
880 name = "CreateNotify";
883 name = "DestroyNotify";
886 name = "UnmapNotify";
895 name = "ReparentNotify";
897 case ConfigureNotify:
898 name = "ConfigureNotify";
900 case ConfigureRequest:
901 name = "ConfigureRequest";
904 name = "GravityNotify";
907 name = "ResizeRequest";
909 case CirculateNotify:
910 name = "CirculateNotify";
912 case CirculateRequest:
913 name = "CirculateRequest";
916 name = "PropertyNotify";
919 name = "SelectionClear";
921 case SelectionRequest:
922 name = "SelectionRequest";
924 case SelectionNotify:
925 name = "SelectionNotify";
928 name = "ColormapNotify";
931 name = "ClientMessage";
934 name = "MappingNotify";
939 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
941 e->xany.window, name, e->type, QLength(display));
943 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
945 e->xany.window, e->type, QLength(display));
949 dumpwins(struct swm_region *r, union arg *args)
953 XWindowAttributes wa;
956 fprintf(stderr, "invalid workspace\n");
960 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
962 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
963 state = getstate(win->id);
964 if (!XGetWindowAttributes(display, win->id, &wa))
965 fprintf(stderr, "window: %lu failed "
966 "XGetWindowAttributes\n", win->id);
967 fprintf(stderr, "window: %lu map_state: %d state: %d "
969 win->id, wa.map_state, state, win->transient);
972 fprintf(stderr, "===== unmanaged window list =====\n");
973 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
974 state = getstate(win->id);
975 if (!XGetWindowAttributes(display, win->id, &wa))
976 fprintf(stderr, "window: %lu failed "
977 "XGetWindowAttributes\n", win->id);
978 fprintf(stderr, "window: %lu map_state: %d state: %d "
980 win->id, wa.map_state, state, win->transient);
983 fprintf(stderr, "=================================\n");
988 dumpwins(struct swm_region *r, union arg *args)
991 #endif /* SWM_DEBUG */
993 void expose(XEvent *);
994 void keypress(XEvent *);
995 void buttonpress(XEvent *);
996 void configurerequest(XEvent *);
997 void configurenotify(XEvent *);
998 void destroynotify(XEvent *);
999 void enternotify(XEvent *);
1000 void focusevent(XEvent *);
1001 void mapnotify(XEvent *);
1002 void mappingnotify(XEvent *);
1003 void maprequest(XEvent *);
1004 void propertynotify(XEvent *);
1005 void unmapnotify(XEvent *);
1006 void visibilitynotify(XEvent *);
1007 void clientmessage(XEvent *);
1009 void (*handler[LASTEvent])(XEvent *) = {
1011 [KeyPress] = keypress,
1012 [ButtonPress] = buttonpress,
1013 [ConfigureRequest] = configurerequest,
1014 [ConfigureNotify] = configurenotify,
1015 [DestroyNotify] = destroynotify,
1016 [EnterNotify] = enternotify,
1017 [FocusIn] = focusevent,
1018 [FocusOut] = focusevent,
1019 [MapNotify] = mapnotify,
1020 [MappingNotify] = mappingnotify,
1021 [MapRequest] = maprequest,
1022 [PropertyNotify] = propertynotify,
1023 [UnmapNotify] = unmapnotify,
1024 [VisibilityNotify] = visibilitynotify,
1025 [ClientMessage] = clientmessage,
1031 int saved_errno, status;
1034 saved_errno = errno;
1038 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1043 if (errno != ECHILD)
1044 warn("sighdlr: waitpid");
1045 #endif /* SWM_DEBUG */
1048 if (pid == searchpid)
1052 if (WIFEXITED(status)) {
1053 if (WEXITSTATUS(status) != 0)
1054 warnx("sighdlr: child exit status: %d",
1055 WEXITSTATUS(status));
1057 warnx("sighdlr: child is terminated "
1059 #endif /* SWM_DEBUG */
1073 errno = saved_errno;
1079 struct pid_e *p = NULL;
1081 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1086 TAILQ_FOREACH(p, &pidlist, entry) {
1095 name_to_color(char *colorname)
1099 XColor screen_def, exact_def;
1100 unsigned long result = 0;
1101 char cname[32] = "#";
1103 cmap = DefaultColormap(display, screens[0].idx);
1104 status = XAllocNamedColor(display, cmap, colorname,
1105 &screen_def, &exact_def);
1107 strlcat(cname, colorname + 2, sizeof cname - 1);
1108 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1112 result = screen_def.pixel;
1114 fprintf(stderr, "color '%s' not found.\n", colorname);
1120 setscreencolor(char *val, int i, int c)
1122 if (i > 0 && i <= ScreenCount(display)) {
1123 screens[i - 1].c[c].color = name_to_color(val);
1124 free(screens[i - 1].c[c].name);
1125 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1127 } else if (i == -1) {
1128 for (i = 0; i < ScreenCount(display); i++) {
1129 screens[i].c[c].color = name_to_color(val);
1130 free(screens[i].c[c].name);
1131 if ((screens[i].c[c].name = strdup(val)) == NULL)
1135 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1136 i, ScreenCount(display));
1140 fancy_stacker(struct workspace *ws)
1142 strcpy(ws->stacker, "[ ]");
1143 if (ws->cur_layout->l_stack == vertical_stack)
1144 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1145 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1146 if (ws->cur_layout->l_stack == horizontal_stack)
1147 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1148 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1152 plain_stacker(struct workspace *ws)
1154 strcpy(ws->stacker, "[ ]");
1155 if (ws->cur_layout->l_stack == vertical_stack)
1156 strcpy(ws->stacker, "[|]");
1157 if (ws->cur_layout->l_stack == horizontal_stack)
1158 strcpy(ws->stacker, "[-]");
1162 custom_region(char *val)
1164 unsigned int sidx, x, y, w, h;
1166 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1167 errx(1, "invalid custom region, "
1168 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1169 if (sidx < 1 || sidx > ScreenCount(display))
1170 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1171 sidx, ScreenCount(display));
1175 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1177 if (x > DisplayWidth(display, sidx) ||
1178 y > DisplayHeight(display, sidx) ||
1179 w + x > DisplayWidth(display, sidx) ||
1180 h + y > DisplayHeight(display, sidx)) {
1181 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1182 "- not within screen boundaries "
1183 "(%ux%u)\n", w, h, x, y,
1184 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1188 new_region(&screens[sidx], x, y, w, h);
1192 socket_setnonblock(int fd)
1196 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1197 err(1, "fcntl F_GETFL");
1198 flags |= O_NONBLOCK;
1199 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1200 err(1, "fcntl F_SETFL");
1204 bar_print(struct swm_region *r, char *s)
1206 XClearWindow(display, r->bar_window);
1207 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1208 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1213 bar_extra_stop(void)
1217 bzero(bar_pipe, sizeof bar_pipe);
1220 kill(bar_pid, SIGTERM);
1223 strlcpy(bar_ext, "", sizeof bar_ext);
1228 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1230 int do_class, do_name;
1232 XClassHint *xch = NULL;
1234 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1235 cur_focus != NULL) {
1236 if ((xch = XAllocClassHint()) == NULL)
1238 status = XGetClassHint(display, cur_focus->id, xch);
1239 if (status == BadWindow || status == BadAlloc)
1241 do_class = (title_class_enabled && xch->res_class != NULL);
1242 do_name = (title_name_enabled && xch->res_name != NULL);
1244 strlcat(s, xch->res_class, sz);
1245 if (do_class && do_name)
1246 strlcat(s, ":", sz);
1248 strlcat(s, xch->res_name, sz);
1249 strlcat(s, " ", sz);
1257 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1261 if (window_name_enabled && cur_focus != NULL) {
1262 XFetchName(display, cur_focus->id, &title);
1264 if (cur_focus->floating)
1265 strlcat(s, "(f) ", sz);
1266 strlcat(s, title, sz);
1267 strlcat(s, " ", sz);
1273 int urgent[SWM_WS_MAX];
1275 bar_urgent(char *s, ssize_t sz)
1277 XWMHints *wmh = NULL;
1282 if (urgent_enabled == 0)
1285 for (i = 0; i < SWM_WS_MAX; i++)
1288 for (i = 0; i < ScreenCount(display); i++)
1289 for (j = 0; j < SWM_WS_MAX; j++)
1290 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1291 wmh = XGetWMHints(display, win->id);
1295 if (wmh->flags & XUrgencyHint)
1300 strlcat(s, "* ", sz);
1301 for (i = 0; i < SWM_WS_MAX; i++) {
1303 snprintf(b, sizeof b, "%d ", i + 1);
1305 snprintf(b, sizeof b, "- ");
1308 strlcat(s, "* ", sz);
1316 struct swm_region *r;
1319 char s[SWM_BAR_MAX];
1320 char cn[SWM_BAR_MAX];
1321 char loc[SWM_BAR_MAX];
1324 if (bar_enabled == 0)
1326 if (bar_extra && bar_extra_running) {
1327 /* ignore short reads; it'll correct itself */
1328 while ((b = fgetln(stdin, &len)) != NULL)
1329 if (b && b[len - 1] == '\n') {
1331 strlcpy(bar_ext, b, sizeof bar_ext);
1333 if (b == NULL && errno != EAGAIN) {
1334 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1335 errno, strerror(errno));
1339 strlcpy(bar_ext, "", sizeof bar_ext);
1341 if (clock_enabled == 0)
1342 strlcpy(s, "", sizeof s);
1345 localtime_r(&tmt, &tm);
1346 strftime(s, sizeof s, clock_format, &tm);
1347 strlcat(s, " ", sizeof s);
1350 for (i = 0; i < ScreenCount(display); i++) {
1352 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1353 strlcpy(cn, "", sizeof cn);
1355 bar_urgent(cn, sizeof cn);
1356 bar_class_name(cn, sizeof cn, r->ws->focus);
1357 bar_window_name(cn, sizeof cn, r->ws->focus);
1360 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1361 x++, r->ws->idx + 1, r->ws->stacker, s, cn, bar_ext,
1376 bar_toggle(struct swm_region *r, union arg *args)
1378 struct swm_region *tmpr;
1379 int i, sc = ScreenCount(display);
1381 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1384 for (i = 0; i < sc; i++)
1385 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1386 XUnmapWindow(display, tmpr->bar_window);
1388 for (i = 0; i < sc; i++)
1389 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1390 XMapRaised(display, tmpr->bar_window);
1392 bar_enabled = !bar_enabled;
1395 /* must be after stack */
1402 XSetWindowAttributes wa;
1403 struct swm_region *r;
1406 /* do this here because the conf file is in memory */
1407 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1408 /* launch external status app */
1409 bar_extra_running = 1;
1410 if (pipe(bar_pipe) == -1)
1411 err(1, "pipe error");
1412 socket_setnonblock(bar_pipe[0]);
1413 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1414 if (dup2(bar_pipe[0], 0) == -1)
1416 if (dup2(bar_pipe[1], 1) == -1)
1418 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1419 err(1, "could not disable SIGPIPE");
1420 switch (bar_pid = fork()) {
1422 err(1, "cannot fork");
1426 execvp(bar_argv[0], bar_argv);
1427 err(1, "%s external app failed", bar_argv[0]);
1429 default: /* parent */
1435 bzero(&wa, sizeof wa);
1436 for (i = 0; i < ScreenCount(display); i++)
1437 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1439 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1440 wa.background_pixel =
1441 screens[i].c[SWM_S_COLOR_BAR].color;
1442 XChangeWindowAttributes(display, r->bar_window,
1443 CWBackPixel | CWBorderPixel, &wa);
1449 bar_setup(struct swm_region *r)
1454 XFreeFont(display, bar_fs);
1458 for (i = 0; bar_fonts[i] != NULL; i++) {
1459 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1465 if (bar_fonts[i] == NULL)
1466 errx(1, "couldn't load font");
1468 errx(1, "couldn't create font structure");
1470 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1471 2 * bar_border_width;
1473 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1475 r->bar_window = XCreateSimpleWindow(display,
1476 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1477 bar_height - 2 * bar_border_width,
1478 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1479 r->s->c[SWM_S_COLOR_BAR].color);
1480 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1481 XSetFont(display, bar_gc, bar_fs->fid);
1482 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1484 XMapRaised(display, r->bar_window);
1485 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1487 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1488 err(1, "could not install bar_signal");
1493 drain_enter_notify(void)
1498 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1501 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1505 set_win_state(struct ws_win *win, long state)
1507 long data[] = {state, None};
1509 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1514 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1515 (unsigned char *)data, 2);
1522 unsigned char *p = NULL;
1525 if (!get_property(w, astate, 2L, astate, &n, &p))
1528 result = *((long *)p);
1534 version(struct swm_region *r, union arg *args)
1536 bar_version = !bar_version;
1538 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1539 SWM_VERSION, cvstag);
1541 strlcpy(bar_vertext, "", sizeof bar_vertext);
1546 client_msg(struct ws_win *win, Atom a)
1548 XClientMessageEvent cm;
1553 bzero(&cm, sizeof cm);
1554 cm.type = ClientMessage;
1555 cm.window = win->id;
1556 cm.message_type = aprot;
1559 cm.data.l[1] = CurrentTime;
1560 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1564 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1572 DNPRINTF(SWM_D_MISC,
1573 "config_win: win %lu x %d y %d w %d h %d\n",
1574 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1576 ce.type = ConfigureNotify;
1577 ce.display = display;
1579 ce.window = win->id;
1582 ce.width = win->g.w;
1583 ce.height = win->g.h;
1584 ce.border_width = border_width;
1586 ce.override_redirect = False;
1588 DNPRINTF(SWM_D_MISC,
1589 "config_win: ev win %lu x %d y %d w %d h %d\n",
1590 ev->window, ev->x, ev->y, ev->width, ev->height);
1591 ce.type = ConfigureNotify;
1592 ce.display = ev->display;
1593 ce.event = ev->window;
1594 ce.window = ev->window;
1597 ce.width = ev->width;
1598 ce.height = ev->height;
1599 ce.border_width = ev->border_width;
1600 ce.above = ev->above;
1601 ce.override_redirect = False;
1604 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1608 count_win(struct workspace *ws, int count_transient)
1613 TAILQ_FOREACH(win, &ws->winlist, entry) {
1614 if (count_transient == 0 && win->floating)
1616 if (count_transient == 0 && win->transient)
1622 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1628 quit(struct swm_region *r, union arg *args)
1630 DNPRINTF(SWM_D_MISC, "quit\n");
1635 unmap_window(struct ws_win *win)
1640 /* don't unmap again */
1641 if (getstate(win->id) == IconicState)
1644 set_win_state(win, IconicState);
1646 XUnmapWindow(display, win->id);
1647 XSetWindowBorder(display, win->id,
1648 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1657 for (i = 0; i < ScreenCount(display); i++)
1658 for (j = 0; j < SWM_WS_MAX; j++)
1659 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1664 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1671 event.display = display; /* Ignored, but what the hell */
1672 event.window = win->id;
1673 event.root = win->s->root;
1674 event.subwindow = None;
1675 event.time = CurrentTime;
1680 event.same_screen = True;
1681 event.keycode = XKeysymToKeycode(display, keysym);
1682 event.state = modifiers;
1684 event.type = KeyPress;
1685 XSendEvent(event.display, event.window, True,
1686 KeyPressMask, (XEvent *)&event);
1688 event.type = KeyRelease;
1689 XSendEvent(event.display, event.window, True,
1690 KeyPressMask, (XEvent *)&event);
1695 restart(struct swm_region *r, union arg *args)
1697 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1699 /* disable alarm because the following code may not be interrupted */
1701 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1702 errx(1, "can't disable alarm");
1707 XCloseDisplay(display);
1708 execvp(start_argv[0], start_argv);
1709 fprintf(stderr, "execvp failed\n");
1715 root_to_region(Window root)
1717 struct swm_region *r = NULL;
1719 int i, x, y, wx, wy;
1722 for (i = 0; i < ScreenCount(display); i++)
1723 if (screens[i].root == root)
1726 if (XQueryPointer(display, screens[i].root,
1727 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1728 /* choose a region based on pointer location */
1729 TAILQ_FOREACH(r, &screens[i].rl, entry)
1730 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1731 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1736 r = TAILQ_FIRST(&screens[i].rl);
1742 find_unmanaged_window(Window id)
1747 for (i = 0; i < ScreenCount(display); i++)
1748 for (j = 0; j < SWM_WS_MAX; j++)
1749 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1757 find_window(Window id)
1760 Window wrr, wpr, *wcr = NULL;
1764 for (i = 0; i < ScreenCount(display); i++)
1765 for (j = 0; j < SWM_WS_MAX; j++)
1766 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1770 /* if we were looking for the parent return that window instead */
1771 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1776 /* ignore not found and root */
1777 if (wpr == 0 || wrr == wpr)
1780 /* look for parent */
1781 for (i = 0; i < ScreenCount(display); i++)
1782 for (j = 0; j < SWM_WS_MAX; j++)
1783 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1791 spawn(int ws_idx, union arg *args, int close_fd)
1796 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1799 close(ConnectionNumber(display));
1801 setenv("LD_PRELOAD", SWM_LIB, 1);
1803 if (asprintf(&ret, "%d", ws_idx) == -1) {
1807 setenv("_SWM_WS", ret, 1);
1811 if (asprintf(&ret, "%d", getpid()) == -1) {
1815 setenv("_SWM_PID", ret, 1);
1819 if (setsid() == -1) {
1826 * close stdin and stdout to prevent interaction between apps
1827 * and the baraction script
1828 * leave stderr open to record errors
1830 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1834 dup2(fd, STDIN_FILENO);
1835 dup2(fd, STDOUT_FILENO);
1840 execvp(args->argv[0], args->argv);
1847 spawnterm(struct swm_region *r, union arg *args)
1849 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1853 setenv("_SWM_XTERM_FONTADJ", "", 1);
1854 spawn(r->ws->idx, args, 1);
1859 kill_refs(struct ws_win *win)
1862 struct swm_region *r;
1863 struct workspace *ws;
1868 for (i = 0; i < ScreenCount(display); i++)
1869 TAILQ_FOREACH(r, &screens[i].rl, entry)
1870 for (x = 0; x < SWM_WS_MAX; x++) {
1872 if (win == ws->focus)
1874 if (win == ws->focus_prev)
1875 ws->focus_prev = NULL;
1880 validate_win(struct ws_win *testwin)
1883 struct workspace *ws;
1884 struct swm_region *r;
1885 int i, x, foundit = 0;
1887 if (testwin == NULL)
1890 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1891 TAILQ_FOREACH(r, &screens[i].rl, entry)
1892 for (x = 0; x < SWM_WS_MAX; x++) {
1894 TAILQ_FOREACH(win, &ws->winlist, entry)
1902 validate_ws(struct workspace *testws)
1904 struct swm_region *r;
1905 struct workspace *ws;
1908 /* validate all ws */
1909 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1910 TAILQ_FOREACH(r, &screens[i].rl, entry)
1911 for (x = 0; x < SWM_WS_MAX; x++) {
1920 unfocus_win(struct ws_win *win)
1925 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1929 if (win->ws == NULL)
1932 if (validate_ws(win->ws))
1933 return; /* XXX this gets hit with thunderbird, needs fixing */
1935 if (win->ws->r == NULL)
1938 if (validate_win(win)) {
1943 if (win->ws->focus == win) {
1944 win->ws->focus = NULL;
1945 win->ws->focus_prev = win;
1948 if (validate_win(win->ws->focus)) {
1949 kill_refs(win->ws->focus);
1950 win->ws->focus = NULL;
1952 if (validate_win(win->ws->focus_prev)) {
1953 kill_refs(win->ws->focus_prev);
1954 win->ws->focus_prev = NULL;
1957 /* drain all previous unfocus events */
1958 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1961 grabbuttons(win, 0);
1962 XSetWindowBorder(display, win->id,
1963 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1965 XChangeProperty(display, win->s->root,
1966 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1967 PropModeReplace, (unsigned char *)&none,1);
1976 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1978 for (i = 0; i < ScreenCount(display); i++)
1979 for (j = 0; j < SWM_WS_MAX; j++)
1980 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1985 focus_win(struct ws_win *win)
1990 struct ws_win *cfw = NULL;
1993 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1997 if (win->ws == NULL)
2000 if (validate_ws(win->ws))
2001 return; /* XXX this gets hit with thunderbird, needs fixing */
2003 if (validate_win(win)) {
2008 if (validate_win(win)) {
2013 XGetInputFocus(display, &cur_focus, &rr);
2014 if ((cfw = find_window(cur_focus)) != NULL)
2017 /* use larger hammer since the window was killed somehow */
2018 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2019 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2020 XSetWindowBorder(display, cfw->id,
2021 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2024 win->ws->focus = win;
2026 if (win->ws->r != NULL) {
2027 /* drain all previous focus events */
2028 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2032 XSetInputFocus(display, win->id,
2033 RevertToParent, CurrentTime);
2034 grabbuttons(win, 1);
2035 XSetWindowBorder(display, win->id,
2036 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2037 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2038 win->ws->always_raise)
2039 XMapRaised(display, win->id);
2041 XChangeProperty(display, win->s->root,
2042 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2043 PropModeReplace, (unsigned char *)&win->id,1);
2046 if (window_name_enabled)
2051 switchws(struct swm_region *r, union arg *args)
2053 int wsid = args->id, unmap_old = 0;
2054 struct swm_region *this_r, *other_r;
2056 struct workspace *new_ws, *old_ws;
2063 old_ws = this_r->ws;
2064 new_ws = &this_r->s->ws[wsid];
2066 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2067 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2070 if (new_ws == NULL || old_ws == NULL)
2072 if (new_ws == old_ws)
2075 other_r = new_ws->r;
2076 if (other_r == NULL) {
2077 /* the other workspace is hidden, hide this one */
2081 /* the other ws is visible in another region, exchange them */
2082 other_r->ws_prior = new_ws;
2083 other_r->ws = old_ws;
2084 old_ws->r = other_r;
2086 this_r->ws_prior = old_ws;
2087 this_r->ws = new_ws;
2090 /* this is needed so that we can click on a window after a restart */
2094 a.id = SWM_ARG_ID_FOCUSCUR;
2095 focus(new_ws->r, &a);
2099 /* unmap old windows */
2101 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2104 if (focus_mode == SWM_FOCUS_DEFAULT)
2105 drain_enter_notify();
2109 cyclews(struct swm_region *r, union arg *args)
2112 struct swm_screen *s = r->s;
2114 DNPRINTF(SWM_D_WS, "cyclews id %d "
2115 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2116 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2121 case SWM_ARG_ID_CYCLEWS_UP:
2122 if (a.id < SWM_WS_MAX - 1)
2127 case SWM_ARG_ID_CYCLEWS_DOWN:
2131 a.id = SWM_WS_MAX - 1;
2137 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2139 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2143 } while (a.id != r->ws->idx);
2147 priorws(struct swm_region *r, union arg *args)
2151 DNPRINTF(SWM_D_WS, "priorws id %d "
2152 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2153 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2155 if (r->ws_prior == NULL)
2158 a.id = r->ws_prior->idx;
2163 cyclescr(struct swm_region *r, union arg *args)
2165 struct swm_region *rr = NULL;
2169 /* do nothing if we don't have more than one screen */
2170 if (!(ScreenCount(display) > 1 || outputs > 1))
2175 case SWM_ARG_ID_CYCLESC_UP:
2176 rr = TAILQ_NEXT(r, entry);
2178 rr = TAILQ_FIRST(&screens[i].rl);
2180 case SWM_ARG_ID_CYCLESC_DOWN:
2181 rr = TAILQ_PREV(r, swm_region_list, entry);
2183 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2191 /* move mouse to region */
2193 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2194 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2196 a.id = SWM_ARG_ID_FOCUSCUR;
2199 if (rr->ws->focus) {
2200 /* move to focus window */
2201 x = rr->ws->focus->g.x + 1;
2202 y = rr->ws->focus->g.y + 1;
2203 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2208 sort_windows(struct ws_win_list *wl)
2210 struct ws_win *win, *parent, *nxt;
2215 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2216 nxt = TAILQ_NEXT(win, entry);
2217 if (win->transient) {
2218 parent = find_window(win->transient);
2219 if (parent == NULL) {
2220 fprintf(stderr, "not possible bug\n");
2223 TAILQ_REMOVE(wl, win, entry);
2224 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2231 swapwin(struct swm_region *r, union arg *args)
2233 struct ws_win *target, *source;
2234 struct ws_win *cur_focus;
2235 struct ws_win_list *wl;
2238 DNPRINTF(SWM_D_WS, "swapwin id %d "
2239 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2240 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2242 cur_focus = r->ws->focus;
2243 if (cur_focus == NULL)
2247 wl = &source->ws->winlist;
2250 case SWM_ARG_ID_SWAPPREV:
2251 if (source->transient)
2252 source = find_window(source->transient);
2253 target = TAILQ_PREV(source, ws_win_list, entry);
2254 if (target && target->transient)
2255 target = find_window(target->transient);
2256 TAILQ_REMOVE(wl, source, entry);
2258 TAILQ_INSERT_TAIL(wl, source, entry);
2260 TAILQ_INSERT_BEFORE(target, source, entry);
2262 case SWM_ARG_ID_SWAPNEXT:
2263 target = TAILQ_NEXT(source, entry);
2264 /* move the parent and let the sort handle the move */
2265 if (source->transient)
2266 source = find_window(source->transient);
2267 TAILQ_REMOVE(wl, source, entry);
2269 TAILQ_INSERT_HEAD(wl, source, entry);
2271 TAILQ_INSERT_AFTER(wl, target, source, entry);
2273 case SWM_ARG_ID_SWAPMAIN:
2274 target = TAILQ_FIRST(wl);
2275 if (target == source) {
2276 if (source->ws->focus_prev != NULL &&
2277 source->ws->focus_prev != target)
2279 source = source->ws->focus_prev;
2283 if (target == NULL || source == NULL)
2285 source->ws->focus_prev = target;
2286 TAILQ_REMOVE(wl, target, entry);
2287 TAILQ_INSERT_BEFORE(source, target, entry);
2288 TAILQ_REMOVE(wl, source, entry);
2289 TAILQ_INSERT_HEAD(wl, source, entry);
2291 case SWM_ARG_ID_MOVELAST:
2292 TAILQ_REMOVE(wl, source, entry);
2293 TAILQ_INSERT_TAIL(wl, source, entry);
2296 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2306 focus_prev(struct ws_win *win)
2308 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2309 struct ws_win *cur_focus = NULL;
2310 struct ws_win_list *wl = NULL;
2311 struct workspace *ws = NULL;
2313 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2315 if (!(win && win->ws))
2320 cur_focus = ws->focus;
2321 winlostfocus = cur_focus;
2323 /* pickle, just focus on whatever */
2324 if (cur_focus == NULL) {
2325 /* use prev_focus if valid */
2326 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2327 find_window(WINID(ws->focus_prev)))
2328 winfocus = ws->focus_prev;
2329 if (winfocus == NULL)
2330 winfocus = TAILQ_FIRST(wl);
2334 /* if transient focus on parent */
2335 if (cur_focus->transient) {
2336 winfocus = find_window(cur_focus->transient);
2340 /* if in max_stack try harder */
2341 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2342 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2343 if (cur_focus != ws->focus_prev)
2344 winfocus = ws->focus_prev;
2345 else if (cur_focus != ws->focus)
2346 winfocus = ws->focus;
2348 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2353 if (cur_focus == win)
2354 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2355 if (winfocus == NULL)
2356 winfocus = TAILQ_LAST(wl, ws_win_list);
2357 if (winfocus == NULL || winfocus == win)
2358 winfocus = TAILQ_NEXT(cur_focus, entry);
2360 if (winfocus == winlostfocus || winfocus == NULL)
2363 focus_magic(winfocus);
2367 focus(struct swm_region *r, union arg *args)
2369 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2370 struct ws_win *cur_focus = NULL;
2371 struct ws_win_list *wl = NULL;
2372 struct workspace *ws = NULL;
2377 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2379 /* treat FOCUS_CUR special */
2380 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2381 if (r->ws->focus && r->ws->focus->iconic == 0)
2382 winfocus = r->ws->focus;
2383 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2384 winfocus = r->ws->focus_prev;
2386 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2387 if (winfocus->iconic == 0)
2390 focus_magic(winfocus);
2394 if ((cur_focus = r->ws->focus) == NULL)
2399 winlostfocus = cur_focus;
2402 case SWM_ARG_ID_FOCUSPREV:
2403 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2405 head = TAILQ_LAST(wl, ws_win_list);
2407 if (WINID(winfocus) == cur_focus->transient) {
2408 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2410 head = TAILQ_LAST(wl, ws_win_list);
2415 if (winfocus && winfocus->iconic) {
2416 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2417 if (winfocus->iconic == 0)
2422 case SWM_ARG_ID_FOCUSNEXT:
2423 head = TAILQ_NEXT(cur_focus, entry);
2425 head = TAILQ_FIRST(wl);
2429 if (winfocus && winfocus->iconic) {
2430 TAILQ_FOREACH(winfocus, wl, entry)
2431 if (winfocus->iconic == 0)
2436 case SWM_ARG_ID_FOCUSMAIN:
2437 winfocus = TAILQ_FIRST(wl);
2438 if (winfocus == cur_focus)
2439 winfocus = cur_focus->ws->focus_prev;
2445 if (winfocus == winlostfocus || winfocus == NULL)
2448 focus_magic(winfocus);
2452 cycle_layout(struct swm_region *r, union arg *args)
2454 struct workspace *ws = r->ws;
2455 struct ws_win *winfocus;
2458 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2460 winfocus = ws->focus;
2463 if (ws->cur_layout->l_stack == NULL)
2464 ws->cur_layout = &layouts[0];
2467 if (focus_mode == SWM_FOCUS_DEFAULT)
2468 drain_enter_notify();
2469 a.id = SWM_ARG_ID_FOCUSCUR;
2475 stack_config(struct swm_region *r, union arg *args)
2477 struct workspace *ws = r->ws;
2479 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2482 if (ws->cur_layout->l_config != NULL)
2483 ws->cur_layout->l_config(ws, args->id);
2485 if (args->id != SWM_ARG_ID_STACKINIT)
2492 struct swm_geometry g;
2493 struct swm_region *r;
2496 DNPRINTF(SWM_D_STACK, "stack\n");
2498 for (i = 0; i < ScreenCount(display); i++) {
2500 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2501 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2502 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2504 /* start with screen geometry, adjust for bar */
2506 g.w -= 2 * border_width;
2507 g.h -= 2 * border_width;
2513 r->ws->cur_layout->l_stack(r->ws, &g);
2514 r->ws->cur_layout->l_string(r->ws);
2515 /* save r so we can track region changes */
2522 if (focus_mode == SWM_FOCUS_DEFAULT)
2523 drain_enter_notify();
2527 store_float_geom(struct ws_win *win, struct swm_region *r)
2529 /* retain window geom and region geom */
2530 win->g_float.x = win->g.x;
2531 win->g_float.y = win->g.y;
2532 win->g_float.w = win->g.w;
2533 win->g_float.h = win->g.h;
2534 win->rg_float.x = r->g.x;
2535 win->rg_float.y = r->g.y;
2536 win->rg_float.w = r->g.w;
2537 win->rg_float.h = r->g.h;
2538 win->g_floatvalid = 1;
2542 stack_floater(struct ws_win *win, struct swm_region *r)
2550 bzero(&wc, sizeof wc);
2551 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2554 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2555 * geom on ws switches or return from max mode
2558 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2559 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2561 * use stored g and rg to set relative position and size
2562 * as in old region or before max stack mode
2564 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2565 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2566 win->g.w = win->g_float.w;
2567 win->g.h = win->g_float.h;
2568 win->g_floatvalid = 0;
2571 win->floatmaxed = 0;
2573 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2574 (win->g.h >= HEIGHT(r)))
2575 wc.border_width = 0;
2577 wc.border_width = border_width;
2578 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2579 win->g.w = (double)WIDTH(r) * dialog_ratio;
2580 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2585 * floaters and transients are auto-centred unless moved
2588 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2589 2 - wc.border_width;
2590 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2591 2 - wc.border_width;
2594 /* win can be outside r if new r smaller than old r */
2595 /* Ensure top left corner inside r (move probs otherwise) */
2596 if (win->g.x < r->g.x - wc.border_width)
2597 win->g.x = r->g.x - wc.border_width;
2598 if (win->g.x > r->g.x + r->g.w - 1)
2599 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2600 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2601 if (win->g.y < r->g.y - wc.border_width)
2602 win->g.y = r->g.y - wc.border_width;
2603 if (win->g.y > r->g.y + r->g.h - 1)
2604 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2605 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2609 wc.width = win->g.w;
2610 wc.height = win->g.h;
2613 * Retain floater and transient geometry for correct positioning
2614 * when ws changes region
2616 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2617 store_float_geom(win, r);
2619 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2620 win->id, wc.x, wc.y, wc.width, wc.height);
2622 XConfigureWindow(display, win->id, mask, &wc);
2626 * Send keystrokes to terminal to decrease/increase the font size as the
2627 * window size changes.
2630 adjust_font(struct ws_win *win)
2632 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2633 win->floating || win->transient)
2636 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2637 win->g.w / win->sh.width_inc < term_width &&
2638 win->font_steps < SWM_MAX_FONT_STEPS) {
2639 win->font_size_boundary[win->font_steps] =
2640 (win->sh.width_inc * term_width) + win->sh.base_width;
2643 win->last_inc = win->sh.width_inc;
2644 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2645 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2646 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2649 win->last_inc = win->sh.width_inc;
2650 fake_keypress(win, XK_KP_Add, ShiftMask);
2654 #define SWAPXY(g) do { \
2656 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2657 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2660 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2663 XWindowAttributes wa;
2664 struct swm_geometry win_g, r_g = *g;
2665 struct ws_win *win, *fs_win = 0;
2666 int i, j, s, stacks;
2667 int w_inc = 1, h_inc, w_base = 1, h_base;
2668 int hrh, extra = 0, h_slice, last_h = 0;
2669 int split, colno, winno, mwin, msize, mscale;
2670 int remain, missing, v_slice, reconfigure;
2673 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2674 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2676 winno = count_win(ws, 0);
2677 if (winno == 0 && count_win(ws, 1) == 0)
2680 TAILQ_FOREACH(win, &ws->winlist, entry)
2681 if (win->transient == 0 && win->floating == 0
2682 && win->iconic == 0)
2689 w_inc = win->sh.width_inc;
2690 w_base = win->sh.base_width;
2691 mwin = ws->l_state.horizontal_mwin;
2692 mscale = ws->l_state.horizontal_msize;
2693 stacks = ws->l_state.horizontal_stacks;
2696 w_inc = win->sh.height_inc;
2697 w_base = win->sh.base_height;
2698 mwin = ws->l_state.vertical_mwin;
2699 mscale = ws->l_state.vertical_msize;
2700 stacks = ws->l_state.vertical_stacks;
2704 if (stacks > winno - mwin)
2705 stacks = winno - mwin;
2709 h_slice = r_g.h / SWM_H_SLICE;
2710 if (mwin && winno > mwin) {
2711 v_slice = r_g.w / SWM_V_SLICE;
2715 win_g.w = v_slice * mscale;
2717 if (w_inc > 1 && w_inc < v_slice) {
2718 /* adjust for window's requested size increment */
2719 remain = (win_g.w - w_base) % w_inc;
2720 missing = w_inc - remain;
2727 win_g.x += r_g.w - msize;
2730 colno = split = winno / stacks;
2731 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2732 2 * border_width) / stacks);
2734 hrh = r_g.h / colno;
2735 extra = r_g.h - (colno * hrh);
2736 win_g.h = hrh - 2 * border_width;
2738 /* stack all the tiled windows */
2739 i = j = 0, s = stacks;
2740 TAILQ_FOREACH(win, &ws->winlist, entry) {
2741 if (win->transient != 0 || win->floating != 0)
2743 if (win->iconic != 0)
2746 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2751 if (split && i == split) {
2752 colno = (winno - mwin) / stacks;
2753 if (s <= (winno - mwin) % stacks)
2755 split = split + colno;
2756 hrh = (r_g.h / colno);
2757 extra = r_g.h - (colno * hrh);
2761 win_g.x += win_g.w + 2 * border_width;
2762 win_g.w = (r_g.w - msize -
2763 (stacks * 2 * border_width)) / stacks;
2765 win_g.w += (r_g.w - msize -
2766 (stacks * 2 * border_width)) % stacks;
2770 win_g.h = hrh - 2 * border_width;
2772 h_inc = win->sh.width_inc;
2773 h_base = win->sh.base_width;
2775 h_inc = win->sh.height_inc;
2776 h_base = win->sh.base_height;
2778 if (j == colno - 1) {
2779 win_g.h = hrh + extra;
2780 } else if (h_inc > 1 && h_inc < h_slice) {
2781 /* adjust for window's requested size increment */
2782 remain = (win_g.h - h_base) % h_inc;
2783 missing = h_inc - remain;
2785 if (missing <= extra || j == 0) {
2797 win_g.y += last_h + 2 * border_width;
2799 bzero(&wc, sizeof wc);
2800 if (disable_border && bar_enabled == 0 && winno == 1){
2801 wc.border_width = 0;
2802 win_g.w += 2 * border_width;
2803 win_g.h += 2 * border_width;
2805 wc.border_width = border_width;
2808 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2809 win->g.w != win_g.h || win->g.h != win_g.w) {
2811 win->g.x = wc.x = win_g.y;
2812 win->g.y = wc.y = win_g.x;
2813 win->g.w = wc.width = win_g.h;
2814 win->g.h = wc.height = win_g.w;
2817 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2818 win->g.w != win_g.w || win->g.h != win_g.h) {
2820 win->g.x = wc.x = win_g.x;
2821 win->g.y = wc.y = win_g.y;
2822 win->g.w = wc.width = win_g.w;
2823 win->g.h = wc.height = win_g.h;
2828 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2829 XConfigureWindow(display, win->id, mask, &wc);
2832 if (XGetWindowAttributes(display, win->id, &wa))
2833 if (wa.map_state == IsUnmapped)
2834 XMapRaised(display, win->id);
2842 /* now, stack all the floaters and transients */
2843 TAILQ_FOREACH(win, &ws->winlist, entry) {
2844 if (win->transient == 0 && win->floating == 0)
2846 if (win->iconic == 1)
2848 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2853 stack_floater(win, ws->r);
2854 XMapRaised(display, win->id);
2858 stack_floater(fs_win, ws->r);
2859 XMapRaised(display, fs_win->id);
2864 vertical_config(struct workspace *ws, int id)
2866 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2869 case SWM_ARG_ID_STACKRESET:
2870 case SWM_ARG_ID_STACKINIT:
2871 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2872 ws->l_state.vertical_mwin = 1;
2873 ws->l_state.vertical_stacks = 1;
2875 case SWM_ARG_ID_MASTERSHRINK:
2876 if (ws->l_state.vertical_msize > 1)
2877 ws->l_state.vertical_msize--;
2879 case SWM_ARG_ID_MASTERGROW:
2880 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2881 ws->l_state.vertical_msize++;
2883 case SWM_ARG_ID_MASTERADD:
2884 ws->l_state.vertical_mwin++;
2886 case SWM_ARG_ID_MASTERDEL:
2887 if (ws->l_state.vertical_mwin > 0)
2888 ws->l_state.vertical_mwin--;
2890 case SWM_ARG_ID_STACKINC:
2891 ws->l_state.vertical_stacks++;
2893 case SWM_ARG_ID_STACKDEC:
2894 if (ws->l_state.vertical_stacks > 1)
2895 ws->l_state.vertical_stacks--;
2903 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2905 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2907 stack_master(ws, g, 0, 0);
2911 horizontal_config(struct workspace *ws, int id)
2913 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2916 case SWM_ARG_ID_STACKRESET:
2917 case SWM_ARG_ID_STACKINIT:
2918 ws->l_state.horizontal_mwin = 1;
2919 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2920 ws->l_state.horizontal_stacks = 1;
2922 case SWM_ARG_ID_MASTERSHRINK:
2923 if (ws->l_state.horizontal_msize > 1)
2924 ws->l_state.horizontal_msize--;
2926 case SWM_ARG_ID_MASTERGROW:
2927 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2928 ws->l_state.horizontal_msize++;
2930 case SWM_ARG_ID_MASTERADD:
2931 ws->l_state.horizontal_mwin++;
2933 case SWM_ARG_ID_MASTERDEL:
2934 if (ws->l_state.horizontal_mwin > 0)
2935 ws->l_state.horizontal_mwin--;
2937 case SWM_ARG_ID_STACKINC:
2938 ws->l_state.horizontal_stacks++;
2940 case SWM_ARG_ID_STACKDEC:
2941 if (ws->l_state.horizontal_stacks > 1)
2942 ws->l_state.horizontal_stacks--;
2950 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2952 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2954 stack_master(ws, g, 1, 0);
2957 /* fullscreen view */
2959 max_stack(struct workspace *ws, struct swm_geometry *g)
2962 struct swm_geometry gg = *g;
2963 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2967 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2972 winno = count_win(ws, 0);
2973 if (winno == 0 && count_win(ws, 1) == 0)
2976 TAILQ_FOREACH(win, &ws->winlist, entry) {
2977 if (win->transient) {
2979 parent = find_window(win->transient);
2983 if (win->floating && win->floatmaxed == 0 ) {
2985 * retain geometry for retrieval on exit from
2988 store_float_geom(win, ws->r);
2989 win->floatmaxed = 1;
2992 /* only reconfigure if necessary */
2993 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2995 bzero(&wc, sizeof wc);
2996 win->g.x = wc.x = gg.x;
2997 win->g.y = wc.y = gg.y;
2999 wc.border_width = border_width;
3000 win->g.w = wc.width = gg.w;
3001 win->g.h = wc.height = gg.h;
3003 wc.border_width = 0;
3004 win->g.w = wc.width = gg.w + 2 * border_width;
3005 win->g.h = wc.height = gg.h + 2 * border_width;
3007 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3008 XConfigureWindow(display, win->id, mask, &wc);
3010 /* unmap only if we don't have multi screen */
3011 if (win != ws->focus)
3012 if (!(ScreenCount(display) > 1 || outputs > 1))
3016 /* put the last transient on top */
3019 XMapRaised(display, parent->id);
3020 stack_floater(wintrans, ws->r);
3021 focus_magic(wintrans);
3026 send_to_ws(struct swm_region *r, union arg *args)
3028 int wsid = args->id;
3029 struct ws_win *win = NULL, *parent;
3030 struct workspace *ws, *nws;
3031 Atom ws_idx_atom = 0;
3032 unsigned char ws_idx_str[SWM_PROPLEN];
3041 if (win->ws->idx == wsid)
3044 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3047 nws = &win->s->ws[wsid];
3049 a.id = SWM_ARG_ID_FOCUSPREV;
3051 if (win->transient) {
3052 parent = find_window(win->transient);
3054 unmap_window(parent);
3055 TAILQ_REMOVE(&ws->winlist, parent, entry);
3056 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3061 TAILQ_REMOVE(&ws->winlist, win, entry);
3062 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3065 /* Try to update the window's workspace property */
3066 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3068 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3070 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3072 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3073 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3080 pressbutton(struct swm_region *r, union arg *args)
3082 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3083 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3087 raise_toggle(struct swm_region *r, union arg *args)
3089 if (r && r->ws == NULL)
3092 r->ws->always_raise = !r->ws->always_raise;
3094 /* bring floaters back to top */
3095 if (r->ws->always_raise == 0)
3100 iconify(struct swm_region *r, union arg *args)
3104 if (r->ws->focus == NULL)
3106 unmap_window(r->ws->focus);
3107 update_iconic(r->ws->focus, 1);
3109 if (focus_mode == SWM_FOCUS_DEFAULT)
3110 drain_enter_notify();
3111 r->ws->focus = NULL;
3112 a.id = SWM_ARG_ID_FOCUSCUR;
3117 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3118 unsigned long *slen)
3121 unsigned long nitems, nbytes, nextra;
3122 unsigned char *prop = NULL;
3125 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3126 &rettype, &retfmt, &nitems, &nbytes, &prop);
3127 if (status != Success)
3131 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3132 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3133 if (status != Success) {
3137 if (rettype != stype) {
3146 uniconify(struct swm_region *r, union arg *args)
3150 unsigned char *name;
3154 DNPRINTF(SWM_D_MISC, "uniconify\n");
3156 if (r && r->ws == NULL)
3159 /* make sure we have anything to uniconify */
3160 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3161 if (win->ws == NULL)
3162 continue; /* should never happen */
3163 if (win->iconic == 0)
3172 spawn_select(r, args, "uniconify", &searchpid);
3174 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3177 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3178 if (win->ws == NULL)
3179 continue; /* should never happen */
3180 if (win->iconic == 0)
3183 name = get_win_name(display, win->id, a_wmname, a_string,
3187 fprintf(lfile, "%s.%lu\n", name, win->id);
3194 #define MAX_RESP_LEN 1024
3197 search_do_resp(void)
3201 unsigned char *name;
3205 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3210 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3211 fprintf(stderr, "search: calloc\n");
3215 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3217 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3220 resp[rbytes] = '\0';
3223 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3224 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3225 if (win->iconic == 0)
3227 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3230 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3235 if (strncmp(s, resp, len) == 0) {
3236 /* XXX this should be a callback to generalize */
3237 update_iconic(win, 0);
3248 wkill(struct swm_region *r, union arg *args)
3250 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3252 if (r->ws->focus == NULL)
3255 if (args->id == SWM_ARG_ID_KILLWINDOW)
3256 XKillClient(display, r->ws->focus->id);
3258 if (r->ws->focus->can_delete)
3259 client_msg(r->ws->focus, adelete);
3264 floating_toggle_win(struct ws_win *win)
3266 struct swm_region *r;
3276 /* reject floating toggles in max stack mode */
3277 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3280 if (win->floating) {
3281 if (!win->floatmaxed) {
3282 /* retain position for refloat */
3283 store_float_geom(win, r);
3287 if (win->g_floatvalid) {
3288 /* refloat at last floating relative position */
3289 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3290 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3291 win->g.w = win->g_float.w;
3292 win->g.h = win->g_float.h;
3297 ewmh_update_actions(win);
3303 floating_toggle(struct swm_region *r, union arg *args)
3305 struct ws_win *win = r->ws->focus;
3311 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3312 _NET_WM_STATE_TOGGLE);
3315 if (focus_mode == SWM_FOCUS_DEFAULT)
3316 drain_enter_notify();
3318 if (win == win->ws->focus) {
3319 a.id = SWM_ARG_ID_FOCUSCUR;
3320 focus(win->ws->r, &a);
3325 resize_window(struct ws_win *win, int center)
3329 struct swm_region *r;
3331 r = root_to_region(win->wa.root);
3332 bzero(&wc, sizeof wc);
3333 mask = CWBorderWidth | CWWidth | CWHeight;
3334 wc.border_width = border_width;
3335 wc.width = win->g.w;
3336 wc.height = win->g.h;
3337 if (center == SWM_ARG_ID_CENTER) {
3338 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3339 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3343 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3344 win->id, wc.x, wc.y, wc.width, wc.height);
3346 XConfigureWindow(display, win->id, mask, &wc);
3349 #define SWM_RESIZE_STEPS (50)
3352 resize(struct ws_win *win, union arg *args)
3356 struct swm_region *r = win->ws->r;
3358 int resize_step = 0;
3361 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3362 win->id, win->floating, win->transient);
3364 if (!(win->transient != 0 || win->floating != 0))
3367 /* reject resizes in max mode for floaters (transient ok) */
3368 if (win->floatmaxed)
3372 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3378 case SWM_ARG_ID_WIDTHSHRINK:
3379 win->g.w -= SWM_RESIZE_STEPS;
3382 case SWM_ARG_ID_WIDTHGROW:
3383 win->g.w += SWM_RESIZE_STEPS;
3386 case SWM_ARG_ID_HEIGHTSHRINK:
3387 win->g.h -= SWM_RESIZE_STEPS;
3390 case SWM_ARG_ID_HEIGHTGROW:
3391 win->g.h += SWM_RESIZE_STEPS;
3398 resize_window(win, 0);
3399 store_float_geom(win,r);
3403 if (focus_mode == SWM_FOCUS_DEFAULT)
3404 drain_enter_notify();
3406 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3407 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3410 /* place pointer at bottom left corner or nearest point inside r */
3411 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3412 relx = win->g.w - 1;
3414 relx = r->g.x + r->g.w - win->g.x - 1;
3416 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3417 rely = win->g.h - 1;
3419 rely = r->g.y + r->g.h - win->g.y - 1;
3421 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3423 XMaskEvent(display, MOUSEMASK | ExposureMask |
3424 SubstructureRedirectMask, &ev);
3426 case ConfigureRequest:
3429 handler[ev.type](&ev);
3432 /* do not allow resize outside of region */
3433 if ( ev.xmotion.x_root < r->g.x ||
3434 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3435 ev.xmotion.y_root < r->g.y ||
3436 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3439 if (ev.xmotion.x <= 1)
3441 if (ev.xmotion.y <= 1)
3443 win->g.w = ev.xmotion.x + 1;
3444 win->g.h = ev.xmotion.y + 1;
3446 /* not free, don't sync more than 120 times / second */
3447 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3448 time = ev.xmotion.time;
3449 XSync(display, False);
3450 resize_window(win, args->id);
3454 } while (ev.type != ButtonRelease);
3456 XSync(display, False);
3457 resize_window(win, args->id);
3459 store_float_geom(win,r);
3461 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3463 XUngrabPointer(display, CurrentTime);
3466 drain_enter_notify();
3470 resize_step(struct swm_region *r, union arg *args)
3472 struct ws_win *win = NULL;
3484 move_window(struct ws_win *win)
3488 struct swm_region *r;
3490 r = root_to_region(win->wa.root);
3491 bzero(&wc, sizeof wc);
3495 wc.border_width = border_width;
3497 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3498 win->id, wc.x, wc.y, wc.width, wc.height);
3500 XConfigureWindow(display, win->id, mask, &wc);
3503 #define SWM_MOVE_STEPS (50)
3506 move(struct ws_win *win, union arg *args)
3511 struct swm_region *r = win->ws->r;
3513 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3514 win->id, win->floating, win->transient);
3516 /* in max_stack mode should only move transients */
3517 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3521 if (win->floating == 0 && !win->transient) {
3522 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3525 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3532 case SWM_ARG_ID_MOVELEFT:
3533 win->g.x -= (SWM_MOVE_STEPS - border_width);
3536 case SWM_ARG_ID_MOVERIGHT:
3537 win->g.x += (SWM_MOVE_STEPS - border_width);
3540 case SWM_ARG_ID_MOVEUP:
3541 win->g.y -= (SWM_MOVE_STEPS - border_width);
3544 case SWM_ARG_ID_MOVEDOWN:
3545 win->g.y += (SWM_MOVE_STEPS - border_width);
3553 store_float_geom(win,r);
3558 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3559 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3561 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3563 XMaskEvent(display, MOUSEMASK | ExposureMask |
3564 SubstructureRedirectMask, &ev);
3566 case ConfigureRequest:
3569 handler[ev.type](&ev);
3572 /* don't allow to move window origin out of region */
3573 if ( ev.xmotion.x_root < r->g.x ||
3574 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3575 ev.xmotion.y_root < r->g.y ||
3576 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3579 win->g.x = ev.xmotion.x_root - border_width;
3580 win->g.y = ev.xmotion.y_root - border_width;
3582 /* not free, don't sync more than 120 times / second */
3583 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3584 time = ev.xmotion.time;
3585 XSync(display, False);
3590 } while (ev.type != ButtonRelease);
3592 XSync(display, False);
3595 store_float_geom(win,r);
3596 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3597 XUngrabPointer(display, CurrentTime);
3600 drain_enter_notify();
3604 move_step(struct swm_region *r, union arg *args)
3606 struct ws_win *win = NULL;
3613 if (!(win->transient != 0 || win->floating != 0))
3620 /* user/key callable function IDs */
3687 kf_dumpwins, /* MUST BE LAST */
3691 /* key definitions */
3693 dummykeyfunc(struct swm_region *r, union arg *args)
3698 legacyfunc(struct swm_region *r, union arg *args)
3703 char name[SWM_FUNCNAME_LEN];
3704 void (*func)(struct swm_region *r, union arg *);
3706 } keyfuncs[kf_invalid + 1] = {
3707 /* name function argument */
3708 { "cycle_layout", cycle_layout, {0} },
3709 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3710 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3711 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3712 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3713 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3714 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3715 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3716 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3717 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3718 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3719 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3720 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3721 { "spawn_term", spawnterm, {.argv = spawn_term} },
3722 { "spawn_menu", legacyfunc, {0} },
3723 { "quit", quit, {0} },
3724 { "restart", restart, {0} },
3725 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3726 { "ws_1", switchws, {.id = 0} },
3727 { "ws_2", switchws, {.id = 1} },
3728 { "ws_3", switchws, {.id = 2} },
3729 { "ws_4", switchws, {.id = 3} },
3730 { "ws_5", switchws, {.id = 4} },
3731 { "ws_6", switchws, {.id = 5} },
3732 { "ws_7", switchws, {.id = 6} },
3733 { "ws_8", switchws, {.id = 7} },
3734 { "ws_9", switchws, {.id = 8} },
3735 { "ws_10", switchws, {.id = 9} },
3736 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3737 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3738 { "ws_prior", priorws, {0} },
3739 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3740 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3741 { "mvws_1", send_to_ws, {.id = 0} },
3742 { "mvws_2", send_to_ws, {.id = 1} },
3743 { "mvws_3", send_to_ws, {.id = 2} },
3744 { "mvws_4", send_to_ws, {.id = 3} },
3745 { "mvws_5", send_to_ws, {.id = 4} },
3746 { "mvws_6", send_to_ws, {.id = 5} },
3747 { "mvws_7", send_to_ws, {.id = 6} },
3748 { "mvws_8", send_to_ws, {.id = 7} },
3749 { "mvws_9", send_to_ws, {.id = 8} },
3750 { "mvws_10", send_to_ws, {.id = 9} },
3751 { "bar_toggle", bar_toggle, {0} },
3752 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3753 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3754 { "screenshot_all", legacyfunc, {0} },
3755 { "screenshot_wind", legacyfunc, {0} },
3756 { "float_toggle", floating_toggle,{0} },
3757 { "version", version, {0} },
3758 { "spawn_lock", legacyfunc, {0} },
3759 { "spawn_initscr", legacyfunc, {0} },
3760 { "spawn_custom", dummykeyfunc, {0} },
3761 { "iconify", iconify, {0} },
3762 { "uniconify", uniconify, {0} },
3763 { "raise_toggle", raise_toggle, {0} },
3764 { "button2", pressbutton, {2} },
3765 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
3766 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
3767 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
3768 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
3769 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
3770 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
3771 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
3772 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
3773 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3774 { "invalid key func", NULL, {0} },
3779 enum keyfuncid funcid;
3782 int keys_size = 0, keys_length = 0;
3783 struct key *keys = NULL;
3786 enum { client_click, root_click };
3788 unsigned int action;
3790 unsigned int button;
3791 void (*func)(struct ws_win *, union arg *);
3794 /* action key mouse button func args */
3795 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3796 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3797 { client_click, MODKEY, Button1, move, {0} },
3801 update_modkey(unsigned int mod)
3806 for (i = 0; i < keys_length; i++)
3807 if (keys[i].mod & ShiftMask)
3808 keys[i].mod = mod | ShiftMask;
3812 for (i = 0; i < LENGTH(buttons); i++)
3813 if (buttons[i].mask & ShiftMask)
3814 buttons[i].mask = mod | ShiftMask;
3816 buttons[i].mask = mod;
3826 int spawns_size = 0, spawns_length = 0;
3827 struct spawn_prog *spawns = NULL;
3830 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3833 struct spawn_prog *prog = NULL;
3835 char *ap, **real_args;
3837 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3840 for (i = 0; i < spawns_length; i++) {
3841 if (!strcasecmp(spawn_name, spawns[i].name))
3845 fprintf(stderr, "spawn_custom: program %s not found\n",
3850 /* make room for expanded args */
3851 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3852 err(1, "spawn_custom: calloc real_args");
3854 /* expand spawn_args into real_args */
3855 for (i = 0; i < prog->argc; i++) {
3857 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3858 if (!strcasecmp(ap, "$bar_border")) {
3860 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3862 err(1, "spawn_custom border color");
3863 } else if (!strcasecmp(ap, "$bar_color")) {
3865 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3867 err(1, "spawn_custom bar color");
3868 } else if (!strcasecmp(ap, "$bar_font")) {
3869 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3871 err(1, "spawn_custom bar fonts");
3872 } else if (!strcasecmp(ap, "$bar_font_color")) {
3874 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3876 err(1, "spawn_custom color font");
3877 } else if (!strcasecmp(ap, "$color_focus")) {
3879 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3881 err(1, "spawn_custom color focus");
3882 } else if (!strcasecmp(ap, "$color_unfocus")) {
3884 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3886 err(1, "spawn_custom color unfocus");
3888 /* no match --> copy as is */
3889 if ((real_args[i] = strdup(ap)) == NULL)
3890 err(1, "spawn_custom strdup(ap)");
3892 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3897 if ((swm_debug & SWM_D_SPAWN) != 0) {
3898 fprintf(stderr, "spawn_custom: result = ");
3899 for (i = 0; i < prog->argc; i++)
3900 fprintf(stderr, "\"%s\" ", real_args[i]);
3901 fprintf(stderr, "\n");
3904 *ret_args = real_args;
3905 return (prog->argc);
3909 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3915 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3919 spawn(r->ws->idx, &a, 1);
3921 for (i = 0; i < spawn_argc; i++)
3927 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3933 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3937 if (pipe(select_list_pipe) == -1)
3938 err(1, "pipe error");
3939 if (pipe(select_resp_pipe) == -1)
3940 err(1, "pipe error");
3942 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3943 err(1, "could not disable SIGPIPE");
3944 switch (*pid = fork()) {
3946 err(1, "cannot fork");
3949 if (dup2(select_list_pipe[0], 0) == -1)
3951 if (dup2(select_resp_pipe[1], 1) == -1)
3953 close(select_list_pipe[1]);
3954 close(select_resp_pipe[0]);
3955 spawn(r->ws->idx, &a, 0);
3957 default: /* parent */
3958 close(select_list_pipe[0]);
3959 close(select_resp_pipe[1]);
3963 for (i = 0; i < spawn_argc; i++)
3969 setspawn(struct spawn_prog *prog)
3973 if (prog == NULL || prog->name == NULL)
3977 for (i = 0; i < spawns_length; i++) {
3978 if (!strcmp(spawns[i].name, prog->name)) {
3980 if (prog->argv == NULL) {
3982 DNPRINTF(SWM_D_SPAWN,
3983 "setspawn: delete #%d %s\n",
3985 free(spawns[i].name);
3986 for (j = 0; j < spawns[i].argc; j++)
3987 free(spawns[i].argv[j]);
3988 free(spawns[i].argv);
3989 j = spawns_length - 1;
3991 spawns[i] = spawns[j];
3996 DNPRINTF(SWM_D_SPAWN,
3997 "setspawn: replace #%d %s\n",
3999 free(spawns[i].name);
4000 for (j = 0; j < spawns[i].argc; j++)
4001 free(spawns[i].argv[j]);
4002 free(spawns[i].argv);
4005 /* found case handled */
4011 if (prog->argv == NULL) {
4013 "error: setspawn: cannot find program %s", prog->name);
4018 /* not found: add */
4019 if (spawns_size == 0 || spawns == NULL) {
4021 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4022 spawns = malloc((size_t)spawns_size *
4023 sizeof(struct spawn_prog));
4024 if (spawns == NULL) {
4025 fprintf(stderr, "setspawn: malloc failed\n");
4029 } else if (spawns_length == spawns_size) {
4031 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4032 spawns = realloc(spawns, (size_t)spawns_size *
4033 sizeof(struct spawn_prog));
4034 if (spawns == NULL) {
4035 fprintf(stderr, "setspawn: realloc failed\n");
4041 if (spawns_length < spawns_size) {
4042 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4043 spawns_length, prog->name);
4044 i = spawns_length++;
4047 fprintf(stderr, "spawns array problem?\n");
4048 if (spawns == NULL) {
4049 fprintf(stderr, "spawns array is NULL!\n");
4057 setconfspawn(char *selector, char *value, int flags)
4059 struct spawn_prog *prog;
4060 char *vp, *cp, *word;
4062 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4063 if ((prog = calloc(1, sizeof *prog)) == NULL)
4064 err(1, "setconfspawn: calloc prog");
4065 prog->name = strdup(selector);
4066 if (prog->name == NULL)
4067 err(1, "setconfspawn prog->name");
4068 if ((cp = vp = strdup(value)) == NULL)
4069 err(1, "setconfspawn: strdup(value) ");
4070 while ((word = strsep(&cp, " \t")) != NULL) {
4071 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4073 cp += (long)strspn(cp, " \t");
4074 if (strlen(word) > 0) {
4076 if ((prog->argv = realloc(prog->argv,
4077 prog->argc * sizeof(char *))) == NULL)
4078 err(1, "setconfspawn: realloc");
4079 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4080 err(1, "setconfspawn: strdup");
4087 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4094 setconfspawn("term", "xterm", 0);
4095 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4096 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4097 setconfspawn("lock", "xlock", 0);
4098 setconfspawn("initscr", "initscreen.sh", 0);
4099 setconfspawn("menu", "dmenu_run"
4102 " -nf $bar_font_color"
4104 " -sf $bar_color", 0);
4105 setconfspawn("uniconify", "dmenu"
4109 " -nf $bar_font_color"
4111 " -sf $bar_color", 0);
4115 #define SWM_MODNAME_SIZE 32
4116 #define SWM_KEY_WS "\n+ \t"
4118 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4122 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4123 if (mod == NULL || ks == NULL) {
4124 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4127 if (keystr == NULL || strlen(keystr) == 0) {
4128 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4134 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4135 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4137 cp += (long)strspn(cp, SWM_KEY_WS);
4138 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4140 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4142 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4144 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4146 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4148 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4150 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4151 *mod |= ControlMask;
4153 *ks = XStringToKeysym(name);
4154 XConvertCase(*ks, ks, &uks);
4155 if (ks == NoSymbol) {
4157 "parsekeys: invalid key %s\n",
4163 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4168 strdupsafe(char *str)
4173 return (strdup(str));
4177 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4181 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4182 keyfuncs[kfid].name, spawn_name);
4184 for (i = 0; i < keys_length; i++) {
4185 if (keys[i].mod == mod && keys[i].keysym == ks) {
4186 if (kfid == kf_invalid) {
4189 "setkeybinding: delete #%d %s\n",
4190 i, keyfuncs[keys[i].funcid].name);
4191 free(keys[i].spawn_name);
4192 j = keys_length - 1;
4196 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4199 /* found: replace */
4201 "setkeybinding: replace #%d %s %s\n",
4202 i, keyfuncs[keys[i].funcid].name,
4204 free(keys[i].spawn_name);
4206 keys[i].keysym = ks;
4207 keys[i].funcid = kfid;
4208 keys[i].spawn_name = strdupsafe(spawn_name);
4209 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4214 if (kfid == kf_invalid) {
4216 "error: setkeybinding: cannot find mod/key combination");
4217 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4220 /* not found: add */
4221 if (keys_size == 0 || keys == NULL) {
4223 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4224 keys = malloc((size_t)keys_size * sizeof(struct key));
4226 fprintf(stderr, "malloc failed\n");
4230 } else if (keys_length == keys_size) {
4232 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4233 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4235 fprintf(stderr, "realloc failed\n");
4240 if (keys_length < keys_size) {
4242 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4243 j, keyfuncs[kfid].name, spawn_name);
4245 keys[j].keysym = ks;
4246 keys[j].funcid = kfid;
4247 keys[j].spawn_name = strdupsafe(spawn_name);
4249 fprintf(stderr, "keys array problem?\n");
4251 fprintf(stderr, "keys array problem\n");
4255 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4259 setconfbinding(char *selector, char *value, int flags)
4261 enum keyfuncid kfid;
4265 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4266 if (selector == NULL) {
4267 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4268 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4270 setkeybinding(mod, ks, kfid, NULL);
4275 /* search by key function name */
4276 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4277 if (strncasecmp(selector, keyfuncs[kfid].name,
4278 SWM_FUNCNAME_LEN) == 0) {
4279 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4281 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4282 setkeybinding(mod, ks, kfid, NULL);
4288 /* search by custom spawn name */
4289 for (i = 0; i < spawns_length; i++) {
4290 if (strcasecmp(selector, spawns[i].name) == 0) {
4291 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4293 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4294 setkeybinding(mod, ks, kf_spawn_custom,
4301 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4308 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4309 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4310 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4311 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4312 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4313 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4314 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4315 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4316 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4317 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4318 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4319 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4320 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4321 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4322 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4323 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4324 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4325 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4326 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4327 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4328 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4329 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4330 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4331 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4332 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4333 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4334 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4335 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4336 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4337 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4338 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4339 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4340 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4341 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4342 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4343 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4344 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4345 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4346 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4347 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4348 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4349 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4350 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4351 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4352 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4353 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4354 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4355 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4356 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4357 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4358 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4359 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4360 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4361 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4362 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4363 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4364 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4365 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4366 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4367 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4368 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4369 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4370 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4371 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4372 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4373 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4375 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4380 updatenumlockmask(void)
4383 XModifierKeymap *modmap;
4385 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4387 modmap = XGetModifierMapping(display);
4388 for (i = 0; i < 8; i++)
4389 for (j = 0; j < modmap->max_keypermod; j++)
4390 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4391 == XKeysymToKeycode(display, XK_Num_Lock))
4392 numlockmask = (1 << i);
4394 XFreeModifiermap(modmap);
4400 unsigned int i, j, k;
4402 unsigned int modifiers[] =
4403 { 0, LockMask, numlockmask, numlockmask | LockMask };
4405 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4406 updatenumlockmask();
4408 for (k = 0; k < ScreenCount(display); k++) {
4409 if (TAILQ_EMPTY(&screens[k].rl))
4411 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4412 for (i = 0; i < keys_length; i++) {
4413 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4414 for (j = 0; j < LENGTH(modifiers); j++)
4415 XGrabKey(display, code,
4416 keys[i].mod | modifiers[j],
4417 screens[k].root, True,
4418 GrabModeAsync, GrabModeAsync);
4424 grabbuttons(struct ws_win *win, int focused)
4427 unsigned int modifiers[] =
4428 { 0, LockMask, numlockmask, numlockmask|LockMask };
4430 updatenumlockmask();
4431 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4433 for (i = 0; i < LENGTH(buttons); i++)
4434 if (buttons[i].action == client_click)
4435 for (j = 0; j < LENGTH(modifiers); j++)
4436 XGrabButton(display, buttons[i].button,
4437 buttons[i].mask | modifiers[j],
4438 win->id, False, BUTTONMASK,
4439 GrabModeAsync, GrabModeSync, None,
4442 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4443 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4446 const char *quirkname[] = {
4447 "NONE", /* config string for "no value" */
4456 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4457 #define SWM_Q_WS "\n|+ \t"
4459 parsequirks(char *qstr, unsigned long *quirk)
4469 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4471 cp += (long)strspn(cp, SWM_Q_WS);
4472 for (i = 0; i < LENGTH(quirkname); i++) {
4473 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4474 DNPRINTF(SWM_D_QUIRK,
4475 "parsequirks: %s\n", name);
4480 *quirk |= 1 << (i-1);
4484 if (i >= LENGTH(quirkname)) {
4485 DNPRINTF(SWM_D_QUIRK,
4486 "parsequirks: invalid quirk [%s]\n", name);
4494 setquirk(const char *class, const char *name, const int quirk)
4499 for (i = 0; i < quirks_length; i++) {
4500 if (!strcmp(quirks[i].class, class) &&
4501 !strcmp(quirks[i].name, name)) {
4504 DNPRINTF(SWM_D_QUIRK,
4505 "setquirk: delete #%d %s:%s\n",
4506 i, quirks[i].class, quirks[i].name);
4507 free(quirks[i].class);
4508 free(quirks[i].name);
4509 j = quirks_length - 1;
4511 quirks[i] = quirks[j];
4515 /* found: replace */
4516 DNPRINTF(SWM_D_QUIRK,
4517 "setquirk: replace #%d %s:%s\n",
4518 i, quirks[i].class, quirks[i].name);
4519 free(quirks[i].class);
4520 free(quirks[i].name);
4521 quirks[i].class = strdup(class);
4522 quirks[i].name = strdup(name);
4523 quirks[i].quirk = quirk;
4530 "error: setquirk: cannot find class/name combination");
4533 /* not found: add */
4534 if (quirks_size == 0 || quirks == NULL) {
4536 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4537 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4538 if (quirks == NULL) {
4539 fprintf(stderr, "setquirk: malloc failed\n");
4543 } else if (quirks_length == quirks_size) {
4545 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4546 quirks = realloc(quirks,
4547 (size_t)quirks_size * sizeof(struct quirk));
4548 if (quirks == NULL) {
4549 fprintf(stderr, "setquirk: realloc failed\n");
4554 if (quirks_length < quirks_size) {
4555 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4556 j = quirks_length++;
4557 quirks[j].class = strdup(class);
4558 quirks[j].name = strdup(name);
4559 quirks[j].quirk = quirk;
4561 fprintf(stderr, "quirks array problem?\n");
4562 if (quirks == NULL) {
4563 fprintf(stderr, "quirks array problem!\n");
4570 setconfquirk(char *selector, char *value, int flags)
4572 char *cp, *class, *name;
4574 unsigned long quirks;
4575 if (selector == NULL)
4577 if ((cp = strchr(selector, ':')) == NULL)
4582 if ((retval = parsequirks(value, &quirks)) == 0)
4583 setquirk(class, name, quirks);
4590 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4591 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4592 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4593 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4594 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4595 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4596 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4597 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4598 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4599 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4600 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4601 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4602 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4605 /* conf file stuff */
4606 #define SWM_CONF_FILE "scrotwm.conf"
4608 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4609 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4610 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4611 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4612 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
4613 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4614 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4615 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
4616 SWM_S_VERBOSE_LAYOUT
4620 setconfvalue(char *selector, char *value, int flags)
4624 case SWM_S_BAR_DELAY:
4625 bar_delay = atoi(value);
4627 case SWM_S_BAR_ENABLED:
4628 bar_enabled = atoi(value);
4630 case SWM_S_BAR_BORDER_WIDTH:
4631 bar_border_width = atoi(value);
4633 case SWM_S_BAR_AT_BOTTOM:
4634 bar_at_bottom = atoi(value);
4636 case SWM_S_STACK_ENABLED:
4637 stack_enabled = atoi(value);
4639 case SWM_S_CLOCK_ENABLED:
4640 clock_enabled = atoi(value);
4642 case SWM_S_CLOCK_FORMAT:
4643 #ifndef SWM_DENY_CLOCK_FORMAT
4645 if ((clock_format = strdup(value)) == NULL)
4646 err(1, "setconfvalue: clock_format");
4649 case SWM_S_CYCLE_EMPTY:
4650 cycle_empty = atoi(value);
4652 case SWM_S_CYCLE_VISIBLE:
4653 cycle_visible = atoi(value);
4655 case SWM_S_SS_ENABLED:
4656 ss_enabled = atoi(value);
4658 case SWM_S_TERM_WIDTH:
4659 term_width = atoi(value);
4661 case SWM_S_TITLE_CLASS_ENABLED:
4662 title_class_enabled = atoi(value);
4664 case SWM_S_WINDOW_NAME_ENABLED:
4665 window_name_enabled = atoi(value);
4667 case SWM_S_TITLE_NAME_ENABLED:
4668 title_name_enabled = atoi(value);
4670 case SWM_S_URGENT_ENABLED:
4671 urgent_enabled = atoi(value);
4673 case SWM_S_FOCUS_MODE:
4674 if (!strcmp(value, "default"))
4675 focus_mode = SWM_FOCUS_DEFAULT;
4676 else if (!strcmp(value, "follow_cursor"))
4677 focus_mode = SWM_FOCUS_FOLLOW;
4678 else if (!strcmp(value, "synergy"))
4679 focus_mode = SWM_FOCUS_SYNERGY;
4681 err(1, "focus_mode");
4683 case SWM_S_DISABLE_BORDER:
4684 disable_border = atoi(value);
4686 case SWM_S_BORDER_WIDTH:
4687 border_width = atoi(value);
4689 case SWM_S_BAR_FONT:
4691 if ((bar_fonts[0] = strdup(value)) == NULL)
4692 err(1, "setconfvalue: bar_font");
4694 case SWM_S_BAR_ACTION:
4696 if ((bar_argv[0] = strdup(value)) == NULL)
4697 err(1, "setconfvalue: bar_action");
4699 case SWM_S_SPAWN_TERM:
4700 free(spawn_term[0]);
4701 if ((spawn_term[0] = strdup(value)) == NULL)
4702 err(1, "setconfvalue: spawn_term");
4706 case SWM_S_DIALOG_RATIO:
4707 dialog_ratio = atof(value);
4708 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4711 case SWM_S_VERBOSE_LAYOUT:
4712 verbose_layout = atoi(value);
4713 for (i = 0; layouts[i].l_stack != NULL; i++) {
4715 layouts[i].l_string = fancy_stacker;
4717 layouts[i].l_string = plain_stacker;
4727 setconfmodkey(char *selector, char *value, int flags)
4729 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4730 update_modkey(Mod1Mask);
4731 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4732 update_modkey(Mod2Mask);
4733 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4734 update_modkey(Mod3Mask);
4735 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4736 update_modkey(Mod4Mask);
4743 setconfcolor(char *selector, char *value, int flags)
4745 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4750 setconfregion(char *selector, char *value, int flags)
4752 custom_region(value);
4757 setautorun(char *selector, char *value, int flags)
4767 if (getenv("SWM_STARTED"))
4771 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4772 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4774 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4775 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4778 * This is a little intricate
4780 * If the pid already exists we simply reuse it because it means it was
4781 * used before AND not claimed by manage_window. We get away with
4782 * altering it in the parent after INSERT because this can not be a race
4785 while ((ap = strsep(&sp, " \t")) != NULL) {
4788 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4790 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4791 err(1, "setautorun: realloc");
4792 a.argv[argc - 1] = ap;
4795 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4796 err(1, "setautorun: realloc");
4797 a.argv[argc] = NULL;
4799 if ((pid = fork()) == 0) {
4800 spawn(ws_id, &a, 1);
4809 p = calloc(1, sizeof *p);
4812 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4822 setlayout(char *selector, char *value, int flags)
4824 int ws_id, st, i, x, mg, ma, si, raise;
4826 struct workspace *ws;
4828 if (getenv("SWM_STARTED"))
4832 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4833 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4834 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4835 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4838 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4839 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4841 if (!strcasecmp(s, "vertical"))
4843 else if (!strcasecmp(s, "horizontal"))
4845 else if (!strcasecmp(s, "fullscreen"))
4848 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4849 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4852 for (i = 0; i < ScreenCount(display); i++) {
4853 ws = (struct workspace *)&screens[i].ws;
4854 ws[ws_id].cur_layout = &layouts[st];
4856 ws[ws_id].always_raise = raise;
4857 if (st == SWM_MAX_STACK)
4861 for (x = 0; x < abs(mg); x++) {
4862 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4863 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4864 SWM_ARG_ID_MASTERSHRINK);
4868 for (x = 0; x < abs(ma); x++) {
4869 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4870 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4871 SWM_ARG_ID_MASTERDEL);
4875 for (x = 0; x < abs(si); x++) {
4876 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4877 si >= 0 ? SWM_ARG_ID_STACKINC :
4878 SWM_ARG_ID_STACKDEC);
4886 /* config options */
4887 struct config_option {
4889 int (*func)(char*, char*, int);
4892 struct config_option configopt[] = {
4893 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4894 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4895 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4896 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4897 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4898 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4899 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4900 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4901 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4902 { "bind", setconfbinding, 0 },
4903 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4904 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4905 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4906 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4907 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4908 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4909 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4910 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4911 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
4912 { "modkey", setconfmodkey, 0 },
4913 { "program", setconfspawn, 0 },
4914 { "quirk", setconfquirk, 0 },
4915 { "region", setconfregion, 0 },
4916 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4917 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4918 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4919 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4920 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
4921 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4922 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4923 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4924 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4925 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4926 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4927 { "autorun", setautorun, 0 },
4928 { "layout", setlayout, 0 },
4933 conf_load(char *filename)
4936 char *line, *cp, *optsub, *optval;
4937 size_t linelen, lineno = 0;
4938 int wordlen, i, optind;
4939 struct config_option *opt;
4941 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4943 if (filename == NULL) {
4944 fprintf(stderr, "conf_load: no filename\n");
4947 if ((config = fopen(filename, "r")) == NULL) {
4948 warn("conf_load: fopen");
4952 while (!feof(config)) {
4953 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4956 err(1, "%s", filename);
4961 cp += strspn(cp, " \t\n"); /* eat whitespace */
4962 if (cp[0] == '\0') {
4967 /* get config option */
4968 wordlen = strcspn(cp, "=[ \t\n");
4970 warnx("%s: line %zd: no option found",
4975 for (i = 0; i < LENGTH(configopt); i++) {
4976 opt = &configopt[i];
4977 if (!strncasecmp(cp, opt->optname, wordlen) &&
4978 strlen(opt->optname) == wordlen) {
4984 warnx("%s: line %zd: unknown option %.*s",
4985 filename, lineno, wordlen, cp);
4989 cp += strspn(cp, " \t\n"); /* eat whitespace */
4990 /* get [selector] if any */
4994 wordlen = strcspn(cp, "]");
4997 warnx("%s: line %zd: syntax error",
5001 asprintf(&optsub, "%.*s", wordlen, cp);
5004 cp += strspn(cp, "] \t\n"); /* eat trailing */
5006 cp += strspn(cp, "= \t\n"); /* eat trailing */
5008 optval = strdup(cp);
5009 /* call function to deal with it all */
5010 if (configopt[optind].func(optsub, optval,
5011 configopt[optind].funcflags) != 0) {
5012 fprintf(stderr, "%s line %zd: %s\n",
5013 filename, lineno, line);
5014 errx(1, "%s: line %zd: invalid data for %s",
5015 filename, lineno, configopt[optind].optname);
5023 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5029 set_child_transient(struct ws_win *win, Window *trans)
5031 struct ws_win *parent, *w;
5032 XWMHints *wmh = NULL;
5033 struct swm_region *r;
5034 struct workspace *ws;
5036 parent = find_window(win->transient);
5038 parent->child_trans = win;
5040 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5041 " for %lu trans %lu\n", win->id, win->transient);
5043 if (win->hints == NULL) {
5044 fprintf(stderr, "no hints for %lu\n", win->id);
5048 r = root_to_region(win->wa.root);
5050 /* parent doen't exist in our window list */
5051 TAILQ_FOREACH(w, &ws->winlist, entry) {
5055 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5056 fprintf(stderr, "can't get hints for %lu\n",
5061 if (win->hints->window_group != wmh->window_group)
5064 w->child_trans = win;
5065 win->transient = w->id;
5067 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5068 "transient to %lu\n", win->transient);
5078 window_get_pid(Window win)
5080 Atom actual_type_return;
5081 int actual_format_return = 0;
5082 unsigned long nitems_return = 0;
5083 unsigned long bytes_after_return = 0;
5087 unsigned char *prop = NULL;
5089 if (XGetWindowProperty(display, win,
5090 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5091 XA_CARDINAL, &actual_type_return, &actual_format_return,
5092 &nitems_return, &bytes_after_return,
5093 (unsigned char**)(void*)&pid) != Success)
5095 if (actual_type_return != XA_CARDINAL)
5106 if (XGetWindowProperty(display, win,
5107 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5108 XA_STRING, &actual_type_return, &actual_format_return,
5109 &nitems_return, &bytes_after_return, &prop) != Success)
5111 if (actual_type_return != XA_STRING)
5116 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5117 /* ignore error because strtonum returns 0 anyway */
5124 manage_window(Window id)
5127 struct workspace *ws;
5128 struct ws_win *win, *ww;
5129 int format, i, ws_idx, n, border_me = 0;
5130 unsigned long nitems, bytes;
5131 Atom ws_idx_atom = 0, type;
5132 Atom *prot = NULL, *pp;
5133 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5134 struct swm_region *r;
5140 if ((win = find_window(id)) != NULL)
5141 return (win); /* already being managed */
5143 /* see if we are on the unmanaged list */
5144 if ((win = find_unmanaged_window(id)) != NULL) {
5145 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5147 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5148 if (win->transient) {
5149 set_child_transient(win, &trans);
5150 } if (trans && (ww = find_window(trans)))
5151 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5153 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5154 ewmh_update_actions(win);
5158 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5159 errx(1, "calloc: failed to allocate memory for new window");
5163 /* see if we need to override the workspace */
5164 p = find_pid(window_get_pid(id));
5166 /* Get all the window data in one shot */
5167 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5169 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5170 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5172 XGetWindowAttributes(display, id, &win->wa);
5173 XGetWMNormalHints(display, id, &win->sh, &mask);
5174 win->hints = XGetWMHints(display, id);
5175 XGetTransientForHint(display, id, &trans);
5177 win->transient = trans;
5178 set_child_transient(win, &trans);
5179 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5180 win->id, win->transient);
5183 /* get supported protocols */
5184 if (XGetWMProtocols(display, id, &prot, &n)) {
5185 for (i = 0, pp = prot; i < n; i++, pp++) {
5186 if (*pp == takefocus)
5187 win->take_focus = 1;
5189 win->can_delete = 1;
5195 win->iconic = get_iconic(win);
5198 * Figure out where to put the window. If it was previously assigned to
5199 * a workspace (either by spawn() or manually moving), and isn't
5200 * transient, * put it in the same workspace
5202 r = root_to_region(win->wa.root);
5204 ws = &r->s->ws[p->ws];
5205 TAILQ_REMOVE(&pidlist, p, entry);
5208 } else if (prop && win->transient == 0) {
5209 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5210 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5212 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5215 ws = &r->s->ws[ws_idx];
5218 /* this should launch transients in the same ws as parent */
5220 if ((ww = find_window(trans)) != NULL)
5227 "fix this bug mcbride\n");
5232 /* set up the window layout */
5235 win->s = r->s; /* this never changes */
5236 if (trans && (ww = find_window(trans)))
5237 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5239 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5241 win->g.w = win->wa.width;
5242 win->g.h = win->wa.height;
5243 win->g.x = win->wa.x;
5244 win->g.y = win->wa.y;
5245 win->g_floatvalid = 0;
5246 win->floatmaxed = 0;
5247 win->ewmh_flags = 0;
5249 /* Set window properties so we can remember this after reincarnation */
5250 if (ws_idx_atom && prop == NULL &&
5251 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5253 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5255 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5256 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5261 ewmh_autoquirk(win);
5263 if (XGetClassHint(display, win->id, &win->ch)) {
5264 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5265 win->ch.res_class, win->ch.res_name);
5267 /* java is retarded so treat it special */
5268 if (strstr(win->ch.res_name, "sun-awt")) {
5273 for (i = 0; i < quirks_length; i++){
5274 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5275 !strcmp(win->ch.res_name, quirks[i].name)) {
5276 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5277 win->ch.res_class, win->ch.res_name);
5278 if (quirks[i].quirk & SWM_Q_FLOAT) {
5282 win->quirks = quirks[i].quirk;
5287 /* alter window position if quirky */
5288 if (win->quirks & SWM_Q_ANYWHERE) {
5289 win->manual = 1; /* don't center the quirky windows */
5290 bzero(&wc, sizeof wc);
5292 if (bar_enabled && win->g.y < bar_height) {
5293 win->g.y = wc.y = bar_height;
5296 if (win->g.w + win->g.x > WIDTH(r)) {
5297 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5303 /* Reset font sizes (the bruteforce way; no default keybinding). */
5304 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5305 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5306 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5307 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5308 fake_keypress(win, XK_KP_Add, ShiftMask);
5311 ewmh_get_win_state(win);
5312 ewmh_update_actions(win);
5313 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5317 bzero(&wc, sizeof wc);
5318 wc.border_width = border_width;
5319 mask = CWBorderWidth;
5320 XConfigureWindow(display, win->id, mask, &wc);
5323 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5324 PropertyChangeMask | StructureNotifyMask);
5326 /* floaters need to be mapped if they are in the current workspace */
5327 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5328 XMapRaised(display, win->id);
5334 free_window(struct ws_win *win)
5336 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5341 /* needed for restart wm */
5342 set_win_state(win, WithdrawnState);
5344 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5346 if (win->ch.res_class)
5347 XFree(win->ch.res_class);
5348 if (win->ch.res_name)
5349 XFree(win->ch.res_name);
5354 memset(win, 0xff, sizeof *win); /* XXX kill later */
5360 unmanage_window(struct ws_win *win)
5362 struct ws_win *parent;
5367 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5369 if (win->transient) {
5370 parent = find_window(win->transient);
5372 parent->child_trans = NULL;
5375 /* focus on root just in case */
5376 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5380 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5381 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5387 focus_magic(struct ws_win *win)
5389 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5394 if (win->child_trans) {
5395 /* win = parent & has a transient so focus on that */
5397 focus_win(win->child_trans);
5398 if (win->child_trans->take_focus)
5399 client_msg(win, takefocus);
5401 /* make sure transient hasn't dissapeared */
5402 if (validate_win(win->child_trans) == 0) {
5403 focus_win(win->child_trans);
5404 if (win->child_trans->take_focus)
5405 client_msg(win->child_trans, takefocus);
5407 win->child_trans = NULL;
5409 if (win->take_focus)
5410 client_msg(win, takefocus);
5416 if (win->take_focus)
5417 client_msg(win, takefocus);
5424 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5431 KeySym keysym, skeysym;
5432 XKeyEvent *ev = &e->xkey;
5434 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5436 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5437 skeysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 1);
5438 for (i = 0; i < keys_length; i++)
5439 if ((keysym == keys[i].keysym || skeysym == keys[i].keysym)
5440 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5441 && keyfuncs[keys[i].funcid].func) {
5442 if (keys[i].funcid == kf_spawn_custom)
5444 root_to_region(ev->root),
5445 &(keyfuncs[keys[i].funcid].args),
5449 keyfuncs[keys[i].funcid].func(
5450 root_to_region(ev->root),
5451 &(keyfuncs[keys[i].funcid].args)
5457 buttonpress(XEvent *e)
5461 XButtonPressedEvent *ev = &e->xbutton;
5463 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5465 action = root_click;
5466 if ((win = find_window(ev->window)) == NULL)
5470 action = client_click;
5472 for (i = 0; i < LENGTH(buttons); i++)
5473 if (action == buttons[i].action && buttons[i].func &&
5474 buttons[i].button == ev->button &&
5475 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5476 buttons[i].func(win, &buttons[i].args);
5480 configurerequest(XEvent *e)
5482 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5487 if ((win = find_window(ev->window)) == NULL)
5488 if ((win = find_unmanaged_window(ev->window)) == NULL)
5492 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5494 bzero(&wc, sizeof wc);
5497 wc.width = ev->width;
5498 wc.height = ev->height;
5499 wc.border_width = ev->border_width;
5500 wc.sibling = ev->above;
5501 wc.stack_mode = ev->detail;
5502 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5504 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5506 config_win(win, ev);
5511 configurenotify(XEvent *e)
5516 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5517 e->xconfigure.window);
5519 win = find_window(e->xconfigure.window);
5521 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5525 if (focus_mode == SWM_FOCUS_DEFAULT)
5526 drain_enter_notify();
5531 destroynotify(XEvent *e)
5534 XDestroyWindowEvent *ev = &e->xdestroywindow;
5536 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5538 if ((win = find_window(ev->window)) == NULL) {
5539 if ((win = find_unmanaged_window(ev->window)) == NULL)
5545 /* make sure we focus on something */
5548 unmanage_window(win);
5550 if (focus_mode == SWM_FOCUS_DEFAULT)
5551 drain_enter_notify();
5556 enternotify(XEvent *e)
5558 XCrossingEvent *ev = &e->xcrossing;
5563 Window focus_return;
5564 int revert_to_return;
5566 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5567 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5568 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5569 ev->same_screen, ev->focus, ev->state);
5571 switch (focus_mode) {
5572 case SWM_FOCUS_DEFAULT:
5574 case SWM_FOCUS_FOLLOW:
5576 case SWM_FOCUS_SYNERGY:
5579 * all these checks need to be in this order because the
5580 * XCheckTypedWindowEvent relies on weeding out the previous events
5582 * making this code an option would enable a follow mouse for focus
5587 * state is set when we are switching workspaces and focus is set when
5588 * the window or a subwindow already has focus (occurs during restart).
5590 * Only honor the focus flag if last_focus_event is not FocusOut,
5591 * this allows scrotwm to continue to control focus when another
5592 * program is also playing with it.
5594 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5595 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5600 * happens when a window is created or destroyed and the border
5601 * crosses the mouse pointer and when switching ws
5603 * we need the subwindow test to see if we came from root in order
5604 * to give focus to floaters
5606 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5607 ev->subwindow == 0) {
5608 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5612 /* this window already has focus */
5613 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5614 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5618 /* this window is being deleted or moved to another ws */
5619 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5621 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5622 XPutBackEvent(display, &cne);
5626 if ((win = find_window(ev->window)) == NULL) {
5627 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5632 * In fullstack kill all enters unless they come from a different ws
5633 * (i.e. another region) or focus has been grabbed externally.
5635 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5636 last_focus_event != FocusOut) {
5637 XGetInputFocus(display, &focus_return, &revert_to_return);
5638 if ((w = find_window(focus_return)) == NULL ||
5640 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5648 if ((win = find_window(ev->window)) == NULL) {
5649 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5654 * if we have more enternotifies let them handle it in due time
5656 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5657 DNPRINTF(SWM_D_EVENT,
5658 "ignoring enternotify: got more enternotify\n");
5659 XPutBackEvent(display, &cne);
5666 /* lets us use one switch statement for arbitrary mode/detail combinations */
5667 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5670 focusevent(XEvent *e)
5674 u_int32_t mode_detail;
5675 XFocusChangeEvent *ev = &e->xfocus;
5677 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5678 ev->type == FocusIn ? "entering" : "leaving",
5679 ev->window, ev->mode, ev->detail);
5681 if (last_focus_event == ev->type) {
5682 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5686 last_focus_event = ev->type;
5687 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5689 switch (mode_detail) {
5690 /* synergy client focus operations */
5691 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5692 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5694 /* synergy server focus operations */
5695 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5697 /* Entering applications like rdesktop that mangle the pointer */
5698 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5700 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5701 XSetWindowBorder(display, win->id,
5702 win->ws->r->s->c[ev->type == FocusIn ?
5703 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5706 fprintf(stderr, "ignoring focusevent\n");
5707 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5714 mapnotify(XEvent *e)
5717 XMapEvent *ev = &e->xmap;
5719 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5721 win = manage_window(ev->window);
5723 set_win_state(win, NormalState);
5727 mappingnotify(XEvent *e)
5729 XMappingEvent *ev = &e->xmapping;
5731 XRefreshKeyboardMapping(ev);
5732 if (ev->request == MappingKeyboard)
5737 maprequest(XEvent *e)
5740 struct swm_region *r;
5741 XWindowAttributes wa;
5742 XMapRequestEvent *ev = &e->xmaprequest;
5744 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5745 e->xmaprequest.window);
5747 if (!XGetWindowAttributes(display, ev->window, &wa))
5749 if (wa.override_redirect)
5752 win = manage_window(e->xmaprequest.window);
5754 return; /* can't happen */
5758 /* make new win focused */
5759 r = root_to_region(win->wa.root);
5760 if (win->ws == r->ws)
5765 propertynotify(XEvent *e)
5768 XPropertyEvent *ev = &e->xproperty;
5770 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5773 win = find_window(ev->window);
5777 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5778 update_iconic(win, 0);
5779 XMapRaised(display, win->id);
5786 case XA_WM_NORMAL_HINTS:
5789 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5790 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5791 if (win->sh.flags & PMinSize) {
5792 win->g.w = win->sh.min_width;
5793 win->g.h = win->sh.min_height;
5794 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5796 XMoveResizeWindow(display, win->id,
5797 win->g.x, win->g.y, win->g.w, win->g.h);
5799 if (window_name_enabled)
5808 unmapnotify(XEvent *e)
5812 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5814 /* determine if we need to help unmanage this window */
5815 win = find_window(e->xunmap.window);
5819 if (getstate(e->xunmap.window) == NormalState) {
5820 unmanage_window(win);
5823 /* giant hack for apps that don't destroy transient windows */
5824 /* eat a bunch of events to prevent remanaging the window */
5826 while (XCheckWindowEvent(display, e->xunmap.window,
5827 EnterWindowMask, &cne))
5829 while (XCheckWindowEvent(display, e->xunmap.window,
5830 StructureNotifyMask, &cne))
5832 while (XCheckWindowEvent(display, e->xunmap.window,
5833 SubstructureNotifyMask, &cne))
5835 /* resend unmap because we ated it */
5836 XUnmapWindow(display, e->xunmap.window);
5839 if (focus_mode == SWM_FOCUS_DEFAULT)
5840 drain_enter_notify();
5844 visibilitynotify(XEvent *e)
5847 struct swm_region *r;
5849 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5850 e->xvisibility.window);
5851 if (e->xvisibility.state == VisibilityUnobscured)
5852 for (i = 0; i < ScreenCount(display); i++)
5853 TAILQ_FOREACH(r, &screens[i].rl, entry)
5854 if (e->xvisibility.window == r->bar_window)
5859 clientmessage(XEvent *e)
5861 XClientMessageEvent *ev;
5866 win = find_window(ev->window);
5870 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5871 ev->window, ev->message_type);
5873 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5874 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5877 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5878 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5879 if (win->can_delete)
5880 client_msg(win, adelete);
5882 XKillClient(display, win->id);
5884 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5885 DNPRINTF(SWM_D_EVENT,
5886 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5887 if (win->floating) {
5888 if (ev->data.l[0] & (1<<8)) /* x */
5889 win->g.x = ev->data.l[1];
5890 if (ev->data.l[0] & (1<<9)) /* y */
5891 win->g.y = ev->data.l[2];
5892 if (ev->data.l[0] & (1<<10)) /* width */
5893 win->g.w = ev->data.l[3];
5894 if (ev->data.l[0] & (1<<11)) /* height */
5895 win->g.h = ev->data.l[4];
5898 /* TODO: Change stack sizes */
5900 config_win(win, NULL);
5902 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5903 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5904 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5906 ewmh_update_win_state(win, ev->data.l[2],
5914 xerror_start(Display *d, XErrorEvent *ee)
5921 xerror(Display *d, XErrorEvent *ee)
5923 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5931 xerrorxlib = XSetErrorHandler(xerror_start);
5933 /* this causes an error if some other window manager is running */
5934 XSelectInput(display, DefaultRootWindow(display),
5935 SubstructureRedirectMask);
5936 XSync(display, False);
5940 XSetErrorHandler(xerror);
5941 XSync(display, False);
5946 new_region(struct swm_screen *s, int x, int y, int w, int h)
5948 struct swm_region *r, *n;
5949 struct workspace *ws = NULL;
5952 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5953 s->idx, w, h, x, y);
5955 /* remove any conflicting regions */
5956 n = TAILQ_FIRST(&s->rl);
5959 n = TAILQ_NEXT(r, entry);
5960 if (X(r) < (x + w) &&
5961 (X(r) + WIDTH(r)) > x &&
5963 (Y(r) + HEIGHT(r)) > y) {
5964 if (r->ws->r != NULL)
5965 r->ws->old_r = r->ws->r;
5967 XDestroyWindow(display, r->bar_window);
5968 TAILQ_REMOVE(&s->rl, r, entry);
5969 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5973 /* search old regions for one to reuse */
5975 /* size + location match */
5976 TAILQ_FOREACH(r, &s->orl, entry)
5977 if (X(r) == x && Y(r) == y &&
5978 HEIGHT(r) == h && WIDTH(r) == w)
5982 TAILQ_FOREACH(r, &s->orl, entry)
5983 if (HEIGHT(r) == h && WIDTH(r) == w)
5987 TAILQ_REMOVE(&s->orl, r, entry);
5988 /* try to use old region's workspace */
5989 if (r->ws->r == NULL)
5992 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5993 errx(1, "calloc: failed to allocate memory for screen");
5995 /* if we don't have a workspace already, find one */
5997 for (i = 0; i < SWM_WS_MAX; i++)
5998 if (s->ws[i].r == NULL) {
6005 errx(1, "no free workspaces\n");
6016 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6022 #ifdef SWM_XRR_HAS_CRTC
6024 XRRScreenResources *sr;
6027 #endif /* SWM_XRR_HAS_CRTC */
6028 struct swm_region *r;
6031 if (i >= ScreenCount(display))
6032 errx(1, "invalid screen");
6034 /* remove any old regions */
6035 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6036 r->ws->old_r = r->ws->r = NULL;
6037 XDestroyWindow(display, r->bar_window);
6038 TAILQ_REMOVE(&screens[i].rl, r, entry);
6039 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6043 /* map virtual screens onto physical screens */
6044 #ifdef SWM_XRR_HAS_CRTC
6045 if (xrandr_support) {
6046 sr = XRRGetScreenResources(display, screens[i].root);
6048 new_region(&screens[i], 0, 0,
6049 DisplayWidth(display, i),
6050 DisplayHeight(display, i));
6054 for (c = 0, ci = NULL; c < ncrtc; c++) {
6055 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6056 if (ci->noutput == 0)
6059 if (ci != NULL && ci->mode == None)
6060 new_region(&screens[i], 0, 0,
6061 DisplayWidth(display, i),
6062 DisplayHeight(display, i));
6064 new_region(&screens[i],
6065 ci->x, ci->y, ci->width, ci->height);
6068 XRRFreeCrtcInfo(ci);
6069 XRRFreeScreenResources(sr);
6071 #endif /* SWM_XRR_HAS_CRTC */
6073 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6074 DisplayHeight(display, i));
6079 screenchange(XEvent *e) {
6080 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6081 struct swm_region *r;
6084 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6086 if (!XRRUpdateConfiguration(e))
6089 /* silly event doesn't include the screen index */
6090 for (i = 0; i < ScreenCount(display); i++)
6091 if (screens[i].root == xe->root)
6093 if (i >= ScreenCount(display))
6094 errx(1, "screenchange: screen not found\n");
6096 /* brute force for now, just re-enumerate the regions */
6099 /* add bars to all regions */
6100 for (i = 0; i < ScreenCount(display); i++)
6101 TAILQ_FOREACH(r, &screens[i].rl, entry)
6104 if (focus_mode == SWM_FOCUS_DEFAULT)
6105 drain_enter_notify();
6111 Window d1, d2, *wins = NULL;
6112 XWindowAttributes wa;
6117 for (i = 0; i < ScreenCount(display); i++) {
6118 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6121 /* attach windows to a region */
6122 /* normal windows */
6123 for (j = 0; j < no; j++) {
6124 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6125 wa.override_redirect ||
6126 XGetTransientForHint(display, wins[j], &d1))
6129 state = getstate(wins[j]);
6130 manage = state == IconicState;
6131 if (wa.map_state == IsViewable || manage)
6132 manage_window(wins[j]);
6134 /* transient windows */
6135 for (j = 0; j < no; j++) {
6136 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6137 wa.override_redirect)
6140 state = getstate(wins[j]);
6141 manage = state == IconicState;
6142 if (XGetTransientForHint(display, wins[j], &d1) &&
6144 manage_window(wins[j]);
6157 int errorbase, major, minor;
6158 struct workspace *ws;
6160 if ((screens = calloc(ScreenCount(display),
6161 sizeof(struct swm_screen))) == NULL)
6162 errx(1, "calloc: screens");
6164 /* initial Xrandr setup */
6165 xrandr_support = XRRQueryExtension(display,
6166 &xrandr_eventbase, &errorbase);
6168 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6171 /* map physical screens */
6172 for (i = 0; i < ScreenCount(display); i++) {
6173 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6175 TAILQ_INIT(&screens[i].rl);
6176 TAILQ_INIT(&screens[i].orl);
6177 screens[i].root = RootWindow(display, i);
6179 /* set default colors */
6180 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6181 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6182 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6183 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6184 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6186 /* set default cursor */
6187 XDefineCursor(display, screens[i].root,
6188 XCreateFontCursor(display, XC_left_ptr));
6190 /* init all workspaces */
6191 /* XXX these should be dynamically allocated too */
6192 for (j = 0; j < SWM_WS_MAX; j++) {
6193 ws = &screens[i].ws[j];
6198 TAILQ_INIT(&ws->winlist);
6199 TAILQ_INIT(&ws->unmanagedlist);
6201 for (k = 0; layouts[k].l_stack != NULL; k++)
6202 if (layouts[k].l_config != NULL)
6203 layouts[k].l_config(ws,
6204 SWM_ARG_ID_STACKINIT);
6205 ws->cur_layout = &layouts[0];
6206 ws->cur_layout->l_string(ws);
6212 XRRSelectInput(display, screens[i].root,
6213 RRScreenChangeNotifyMask);
6220 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6222 err(1, "setup_globals: strdup");
6223 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6225 err(1, "setup_globals: strdup");
6226 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6228 err(1, "setup_globals: strdup");
6229 if ((spawn_term[0] = strdup("xterm")) == NULL)
6230 err(1, "setup_globals: strdup");
6231 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6239 Atom netwmcheck, netwmname, utf8_string;
6242 /* work around sun jdk bugs, code from wmname */
6243 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6244 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6245 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6246 for (i = 0; i < ScreenCount(display); i++) {
6247 root = screens[i].root;
6248 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6249 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6250 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6252 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6253 PropModeReplace, (unsigned char *)&win,1);
6254 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6255 PropModeReplace, (unsigned char *)&win,1);
6256 XChangeProperty(display, win, netwmname, utf8_string, 8,
6257 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6262 main(int argc, char *argv[])
6265 struct swm_region *r, *rr;
6266 struct ws_win *winfocus = NULL;
6269 char conf[PATH_MAX], *cfile = NULL;
6274 struct sigaction sact;
6277 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
6278 SWM_VERSION, cvstag);
6279 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6280 warnx("no locale support");
6282 if (!(display = XOpenDisplay(0)))
6283 errx(1, "can not open display");
6286 errx(1, "other wm running");
6288 /* handle some signals */
6289 bzero(&sact, sizeof(sact));
6290 sigemptyset(&sact.sa_mask);
6292 sact.sa_handler = sighdlr;
6293 sigaction(SIGINT, &sact, NULL);
6294 sigaction(SIGQUIT, &sact, NULL);
6295 sigaction(SIGTERM, &sact, NULL);
6296 sigaction(SIGHUP, &sact, NULL);
6298 sact.sa_handler = sighdlr;
6299 sact.sa_flags = SA_NOCLDSTOP;
6300 sigaction(SIGCHLD, &sact, NULL);
6302 astate = XInternAtom(display, "WM_STATE", False);
6303 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6304 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6305 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6306 a_wmname = XInternAtom(display, "WM_NAME", False);
6307 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6308 a_string = XInternAtom(display, "STRING", False);
6309 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6311 /* look for local and global conf file */
6312 pwd = getpwuid(getuid());
6314 errx(1, "invalid user %d", getuid());
6323 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6324 if (stat(conf, &sb) != -1) {
6325 if (S_ISREG(sb.st_mode))
6328 /* try global conf file */
6329 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6330 if (!stat(conf, &sb))
6331 if (S_ISREG(sb.st_mode))
6338 /* set some values to work around bad programs */
6340 /* grab existing windows (before we build the bars) */
6343 if (getenv("SWM_STARTED") == NULL)
6344 setenv("SWM_STARTED", "YES", 1);
6346 /* setup all bars */
6347 for (i = 0; i < ScreenCount(display); i++)
6348 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6349 if (winfocus == NULL)
6350 winfocus = TAILQ_FIRST(&r->ws->winlist);
6358 if (focus_mode == SWM_FOCUS_DEFAULT)
6359 drain_enter_notify();
6361 xfd = ConnectionNumber(display);
6363 while (XPending(display)) {
6364 XNextEvent(display, &e);
6367 if (e.type < LASTEvent) {
6369 if (handler[e.type])
6370 handler[e.type](&e);
6372 DNPRINTF(SWM_D_EVENT,
6373 "win: %lu unknown event: %d\n",
6374 e.xany.window, e.type);
6376 switch (e.type - xrandr_eventbase) {
6377 case RRScreenChangeNotify:
6381 DNPRINTF(SWM_D_EVENT,
6382 "win: %lu unknown xrandr event: "
6383 "%d\n", e.xany.window, e.type);
6389 /* if we are being restarted go focus on first window */
6391 rr = winfocus->ws->r;
6393 /* not a visible window */
6397 /* move pointer to first screen if multi screen */
6398 if (ScreenCount(display) > 1 || outputs > 1)
6399 XWarpPointer(display, None, rr->s[0].root,
6400 0, 0, 0, 0, rr->g.x,
6401 rr->g.y + (bar_enabled ? bar_height : 0));
6403 a.id = SWM_ARG_ID_FOCUSCUR;
6413 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6415 DNPRINTF(SWM_D_MISC, "select failed");
6416 if (restart_wm == 1)
6417 restart(NULL, NULL);
6418 if (search_resp == 1)
6430 XFreeGC(display, bar_gc);
6431 XCloseDisplay(display);