2 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
84 #include <X11/extensions/XTest.h>
92 #ifdef SCROTWM_BUILDSTR
93 static const char *buildstr = SCROTWM_BUILDSTR;
95 static const char *buildstr = SCROTWM_VERSION;
99 # error XRandR versions less than 1.0 are not supported
104 #define SWM_XRR_HAS_CRTC
108 /*#define SWM_DEBUG*/
110 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
111 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
112 #define SWM_D_MISC 0x0001
113 #define SWM_D_EVENT 0x0002
114 #define SWM_D_WS 0x0004
115 #define SWM_D_FOCUS 0x0008
116 #define SWM_D_MOVE 0x0010
117 #define SWM_D_STACK 0x0020
118 #define SWM_D_MOUSE 0x0040
119 #define SWM_D_PROP 0x0080
120 #define SWM_D_CLASS 0x0100
121 #define SWM_D_KEY 0x0200
122 #define SWM_D_QUIRK 0x0400
123 #define SWM_D_SPAWN 0x0800
124 #define SWM_D_EVENTQ 0x1000
125 #define SWM_D_CONF 0x2000
127 u_int32_t swm_debug = 0
144 #define DPRINTF(x...)
145 #define DNPRINTF(n,x...)
148 #define LENGTH(x) (sizeof x / sizeof x[0])
149 #define MODKEY Mod1Mask
150 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
151 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
152 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
153 #define SWM_PROPLEN (16)
154 #define SWM_FUNCNAME_LEN (32)
155 #define SWM_KEYS_LEN (255)
156 #define SWM_QUIRK_LEN (64)
157 #define X(r) (r)->g.x
158 #define Y(r) (r)->g.y
159 #define WIDTH(r) (r)->g.w
160 #define HEIGHT(r) (r)->g.h
161 #define SWM_MAX_FONT_STEPS (3)
162 #define WINID(w) (w ? w->id : 0)
164 #define SWM_FOCUS_DEFAULT (0)
165 #define SWM_FOCUS_SYNERGY (1)
166 #define SWM_FOCUS_FOLLOW (2)
168 #define SWM_CONF_DEFAULT (0)
169 #define SWM_CONF_KEYMAPPING (1)
172 #define SWM_LIB "/usr/local/lib/libswmhack.so"
184 volatile sig_atomic_t running = 1;
185 volatile sig_atomic_t restart_wm = 0;
187 int last_focus_event = FocusOut;
188 int (*xerrorxlib)(Display *, XErrorEvent *);
192 int xrandr_eventbase;
193 unsigned int numlockmask = 0;
197 int cycle_visible = 0;
199 int font_adjusted = 0;
200 unsigned int mod_key = MODKEY;
203 struct swm_region *search_r;
204 int select_list_pipe[2];
205 int select_resp_pipe[2];
207 volatile sig_atomic_t search_resp;
210 double dialog_ratio = .6;
212 #define SWM_BAR_MAX (256)
213 #define SWM_BAR_JUSTIFY_LEFT (0)
214 #define SWM_BAR_JUSTIFY_CENTER (1)
215 #define SWM_BAR_JUSTIFY_RIGHT (2)
216 #define SWM_BAR_OFFSET (4)
217 char *bar_argv[] = { NULL, NULL };
219 char bar_ext[SWM_BAR_MAX];
220 char bar_vertext[SWM_BAR_MAX];
222 sig_atomic_t bar_alarm = 0;
225 int bar_border_width = 1;
226 int bar_at_bottom = 0;
228 int bar_extra_running = 0;
231 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
232 int stack_enabled = 1;
233 int clock_enabled = 1;
234 int urgent_enabled = 0;
235 char *clock_format = NULL;
236 int title_name_enabled = 0;
237 int title_class_enabled = 0;
238 int window_name_enabled = 0;
239 int focus_mode = SWM_FOCUS_DEFAULT;
240 int disable_border = 0;
241 int border_width = 1;
242 int verbose_layout = 0;
248 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
249 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
252 #define SWM_MENU_FN (2)
253 #define SWM_MENU_NB (4)
254 #define SWM_MENU_NF (6)
255 #define SWM_MENU_SB (8)
256 #define SWM_MENU_SF (10)
258 /* layout manager data */
259 struct swm_geometry {
269 /* virtual "screens" */
271 TAILQ_ENTRY(swm_region) entry;
272 struct swm_geometry g;
273 struct workspace *ws; /* current workspace on this region */
274 struct workspace *ws_prior; /* prior workspace on this region */
275 struct swm_screen *s; /* screen idx */
278 TAILQ_HEAD(swm_region_list, swm_region);
281 TAILQ_ENTRY(ws_win) entry;
284 struct ws_win *child_trans; /* transient child window */
285 struct swm_geometry g; /* current geometry */
286 struct swm_geometry g_float; /* geometry when floating */
287 struct swm_geometry rg_float; /* region geom when floating */
288 int g_floatvalid; /* flag: geometry in g_float is valid */
289 int floatmaxed; /* flag: floater was maxed in max_stack */
293 unsigned int ewmh_flags;
294 int font_size_boundary[SWM_MAX_FONT_STEPS];
300 unsigned long quirks;
301 struct workspace *ws; /* always valid */
302 struct swm_screen *s; /* always valid, never changes */
303 XWindowAttributes wa;
308 TAILQ_HEAD(ws_win_list, ws_win);
312 TAILQ_ENTRY(pid_e) entry;
316 TAILQ_HEAD(pid_list, pid_e);
317 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
319 /* layout handlers */
321 void vertical_config(struct workspace *, int);
322 void vertical_stack(struct workspace *, struct swm_geometry *);
323 void horizontal_config(struct workspace *, int);
324 void horizontal_stack(struct workspace *, struct swm_geometry *);
325 void max_stack(struct workspace *, struct swm_geometry *);
326 void plain_stacker(struct workspace *);
327 void fancy_stacker(struct workspace *);
329 struct ws_win *find_window(Window);
331 void grabbuttons(struct ws_win *, int);
332 void new_region(struct swm_screen *, int, int, int, int);
333 void unmanage_window(struct ws_win *);
334 long getstate(Window);
336 int conf_load(char *, int);
339 void (*l_stack)(struct workspace *, struct swm_geometry *);
340 void (*l_config)(struct workspace *, int);
342 #define SWM_L_FOCUSPREV (1<<0)
343 #define SWM_L_MAPONFOCUS (1<<1)
344 void (*l_string)(struct workspace *);
346 /* stack, configure */
347 { vertical_stack, vertical_config, 0, plain_stacker },
348 { horizontal_stack, horizontal_config, 0, plain_stacker },
350 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
351 { NULL, NULL, 0, NULL },
354 /* position of max_stack mode in the layouts array, index into layouts! */
355 #define SWM_V_STACK (0)
356 #define SWM_H_STACK (1)
357 #define SWM_MAX_STACK (2)
359 #define SWM_H_SLICE (32)
360 #define SWM_V_SLICE (32)
362 /* define work spaces */
364 int idx; /* workspace index */
365 int always_raise; /* raise windows on focus */
366 struct layout *cur_layout; /* current layout handlers */
367 struct ws_win *focus; /* may be NULL */
368 struct ws_win *focus_prev; /* may be NULL */
369 struct swm_region *r; /* may be NULL */
370 struct swm_region *old_r; /* may be NULL */
371 struct ws_win_list winlist; /* list of windows in ws */
372 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
373 char stacker[10]; /* display stacker and layout */
377 int horizontal_msize;
379 int horizontal_stacks;
386 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
387 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
389 /* physical screen mapping */
390 #define SWM_WS_MAX (10)
392 int idx; /* screen index */
393 struct swm_region_list rl; /* list of regions on this screen */
394 struct swm_region_list orl; /* list of old regions */
396 struct workspace ws[SWM_WS_MAX];
402 } c[SWM_S_COLOR_MAX];
404 struct swm_screen *screens;
407 /* args to functions */
410 #define SWM_ARG_ID_FOCUSNEXT (0)
411 #define SWM_ARG_ID_FOCUSPREV (1)
412 #define SWM_ARG_ID_FOCUSMAIN (2)
413 #define SWM_ARG_ID_FOCUSCUR (4)
414 #define SWM_ARG_ID_SWAPNEXT (10)
415 #define SWM_ARG_ID_SWAPPREV (11)
416 #define SWM_ARG_ID_SWAPMAIN (12)
417 #define SWM_ARG_ID_MOVELAST (13)
418 #define SWM_ARG_ID_MASTERSHRINK (20)
419 #define SWM_ARG_ID_MASTERGROW (21)
420 #define SWM_ARG_ID_MASTERADD (22)
421 #define SWM_ARG_ID_MASTERDEL (23)
422 #define SWM_ARG_ID_STACKRESET (30)
423 #define SWM_ARG_ID_STACKINIT (31)
424 #define SWM_ARG_ID_CYCLEWS_UP (40)
425 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
426 #define SWM_ARG_ID_CYCLESC_UP (42)
427 #define SWM_ARG_ID_CYCLESC_DOWN (43)
428 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
429 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
430 #define SWM_ARG_ID_STACKINC (50)
431 #define SWM_ARG_ID_STACKDEC (51)
432 #define SWM_ARG_ID_SS_ALL (60)
433 #define SWM_ARG_ID_SS_WINDOW (61)
434 #define SWM_ARG_ID_DONTCENTER (70)
435 #define SWM_ARG_ID_CENTER (71)
436 #define SWM_ARG_ID_KILLWINDOW (80)
437 #define SWM_ARG_ID_DELETEWINDOW (81)
438 #define SWM_ARG_ID_WIDTHGROW (90)
439 #define SWM_ARG_ID_WIDTHSHRINK (91)
440 #define SWM_ARG_ID_HEIGHTGROW (92)
441 #define SWM_ARG_ID_HEIGHTSHRINK (93)
442 #define SWM_ARG_ID_MOVEUP (100)
443 #define SWM_ARG_ID_MOVEDOWN (101)
444 #define SWM_ARG_ID_MOVELEFT (102)
445 #define SWM_ARG_ID_MOVERIGHT (103)
449 void focus(struct swm_region *, union arg *);
450 void focus_magic(struct ws_win *);
457 #define SWM_Q_FLOAT (1<<0) /* float this window */
458 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
459 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
460 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
461 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
462 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
464 int quirks_size = 0, quirks_length = 0;
465 struct quirk *quirks = NULL;
468 * Supported EWMH hints should be added to
469 * both the enum and the ewmh array
471 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
472 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
473 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
474 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
475 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
476 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
477 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
478 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
479 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
480 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
486 } ewmh[SWM_EWMH_HINT_MAX] = {
487 /* must be in same order as in the enum */
488 {"_NET_ACTIVE_WINDOW", None},
489 {"_NET_MOVERESIZE_WINDOW", None},
490 {"_NET_CLOSE_WINDOW", None},
491 {"_NET_WM_WINDOW_TYPE", None},
492 {"_NET_WM_WINDOW_TYPE_DOCK", None},
493 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
494 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
495 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
496 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
497 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
498 {"_NET_WM_STATE", None},
499 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
500 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
501 {"_NET_WM_STATE_SKIP_TASKBAR", None},
502 {"_NET_WM_STATE_SKIP_PAGER", None},
503 {"_NET_WM_STATE_HIDDEN", None},
504 {"_NET_WM_STATE_ABOVE", None},
505 {"_SWM_WM_STATE_MANUAL", None},
506 {"_NET_WM_STATE_FULLSCREEN", None},
507 {"_NET_WM_ALLOWED_ACTIONS", None},
508 {"_NET_WM_ACTION_MOVE", None},
509 {"_NET_WM_ACTION_RESIZE", None},
510 {"_NET_WM_ACTION_FULLSCREEN", None},
511 {"_NET_WM_ACTION_CLOSE", None},
514 void store_float_geom(struct ws_win *win, struct swm_region *r);
515 int floating_toggle_win(struct ws_win *win);
516 void spawn_select(struct swm_region *, union arg *, char *, int *);
519 get_property(Window id, Atom atom, long count, Atom type,
520 unsigned long *n, unsigned char **data)
523 unsigned long tmp, extra;
524 unsigned long *nitems;
527 nitems = n != NULL ? n : &tmp;
528 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
529 &real, &format, nitems, &extra, data);
531 if (status != Success)
540 update_iconic(struct ws_win *win, int newv)
547 iprop = XInternAtom(display, "_SWM_ICONIC", False);
551 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
552 PropModeReplace, (unsigned char *)&v, 1);
554 XDeleteProperty(display, win->id, iprop);
558 get_iconic(struct ws_win *win)
563 unsigned long nitems, extra;
564 unsigned char *prop = NULL;
566 iprop = XInternAtom(display, "_SWM_ICONIC", False);
569 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
570 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
571 if (status != Success)
573 if (rettype != XA_INTEGER || retfmt != 32)
577 v = *((int32_t *)prop);
591 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
593 for (i = 0; i < LENGTH(ewmh); i++)
594 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
596 for (i = 0; i < ScreenCount(display); i++) {
597 /* Support check window will be created by workaround(). */
599 /* Report supported atoms */
600 XDeleteProperty(display, screens[i].root, sup_list);
601 for (j = 0; j < LENGTH(ewmh); j++)
602 XChangeProperty(display, screens[i].root,
603 sup_list, XA_ATOM, 32,
604 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
612 unsigned char *data = NULL;
614 Atom sup_check, sup_list;
617 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
618 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
620 for (i = 0; i < ScreenCount(display); i++) {
621 /* Get the support check window and destroy it */
622 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
627 XDestroyWindow(display, id);
628 XDeleteProperty(display, screens[i].root, sup_check);
629 XDeleteProperty(display, screens[i].root, sup_list);
637 ewmh_autoquirk(struct ws_win *win)
640 unsigned long *data = NULL, n;
643 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
644 XA_ATOM, &n, (void *)&data);
651 for (i = 0; i < n; i++) {
653 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
655 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
656 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
657 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
659 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
662 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
663 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
665 win->quirks = SWM_Q_FLOAT;
673 #define SWM_EWMH_ACTION_COUNT_MAX (6)
674 #define EWMH_F_FULLSCREEN (1<<0)
675 #define EWMH_F_ABOVE (1<<1)
676 #define EWMH_F_HIDDEN (1<<2)
677 #define EWMH_F_SKIP_PAGER (1<<3)
678 #define EWMH_F_SKIP_TASKBAR (1<<4)
679 #define SWM_F_MANUAL (1<<5)
682 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
684 struct swm_geometry rg;
692 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
698 store_float_geom(win, win->ws->r);
705 if (win->g_floatvalid) {
706 /* refloat at last floating relative position */
707 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
708 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
709 win->g.w = win->g_float.w;
710 win->g.h = win->g_float.h;
718 ewmh_update_actions(struct ws_win *win)
720 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
726 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
729 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
730 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
733 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
734 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
737 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
738 #define _NET_WM_STATE_ADD 1 /* add/set property */
739 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
742 ewmh_update_win_state(struct ws_win *win, long state, long action)
744 unsigned int mask = 0;
745 unsigned int changed = 0;
746 unsigned int orig_flags;
751 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
752 mask = EWMH_F_FULLSCREEN;
753 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
755 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
757 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
758 mask = EWMH_F_SKIP_PAGER;
759 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
760 mask = EWMH_F_SKIP_TASKBAR;
763 orig_flags = win->ewmh_flags;
766 case _NET_WM_STATE_REMOVE:
767 win->ewmh_flags &= ~mask;
769 case _NET_WM_STATE_ADD:
770 win->ewmh_flags |= mask;
772 case _NET_WM_STATE_TOGGLE:
773 win->ewmh_flags ^= mask;
777 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
779 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
781 if (!floating_toggle_win(win))
782 win->ewmh_flags = orig_flags; /* revert */
783 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
785 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
786 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
788 if (!ewmh_set_win_fullscreen(win,
789 win->ewmh_flags & EWMH_F_FULLSCREEN))
790 win->ewmh_flags = orig_flags; /* revert */
792 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
794 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
795 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
796 XA_ATOM, 32, PropModeAppend,
797 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
798 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
799 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
800 XA_ATOM, 32, PropModeAppend,
801 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
802 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
803 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
804 XA_ATOM, 32, PropModeAppend,
805 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
806 if (win->ewmh_flags & EWMH_F_ABOVE)
807 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
808 XA_ATOM, 32, PropModeAppend,
809 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
810 if (win->ewmh_flags & SWM_F_MANUAL)
811 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
812 XA_ATOM, 32, PropModeAppend,
813 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
817 ewmh_get_win_state(struct ws_win *win)
828 win->ewmh_flags |= EWMH_F_ABOVE;
830 win->ewmh_flags |= SWM_F_MANUAL;
832 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
833 (~0L), XA_ATOM, &n, (void *)&states);
838 for (i = 0; i < n; i++)
839 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
859 name = "ButtonPress";
862 name = "ButtonRelease";
865 name = "MotionNotify";
868 name = "EnterNotify";
871 name = "LeaveNotify";
880 name = "KeymapNotify";
886 name = "GraphicsExpose";
891 case VisibilityNotify:
892 name = "VisibilityNotify";
895 name = "CreateNotify";
898 name = "DestroyNotify";
901 name = "UnmapNotify";
910 name = "ReparentNotify";
912 case ConfigureNotify:
913 name = "ConfigureNotify";
915 case ConfigureRequest:
916 name = "ConfigureRequest";
919 name = "GravityNotify";
922 name = "ResizeRequest";
924 case CirculateNotify:
925 name = "CirculateNotify";
927 case CirculateRequest:
928 name = "CirculateRequest";
931 name = "PropertyNotify";
934 name = "SelectionClear";
936 case SelectionRequest:
937 name = "SelectionRequest";
939 case SelectionNotify:
940 name = "SelectionNotify";
943 name = "ColormapNotify";
946 name = "ClientMessage";
949 name = "MappingNotify";
954 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
956 e->xany.window, name, e->type, QLength(display));
958 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
960 e->xany.window, e->type, QLength(display));
964 dumpwins(struct swm_region *r, union arg *args)
968 XWindowAttributes wa;
971 fprintf(stderr, "invalid workspace\n");
975 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
977 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
978 state = getstate(win->id);
979 if (!XGetWindowAttributes(display, win->id, &wa))
980 fprintf(stderr, "window: %lu failed "
981 "XGetWindowAttributes\n", win->id);
982 fprintf(stderr, "window: %lu map_state: %d state: %d "
984 win->id, wa.map_state, state, win->transient);
987 fprintf(stderr, "===== unmanaged window list =====\n");
988 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
989 state = getstate(win->id);
990 if (!XGetWindowAttributes(display, win->id, &wa))
991 fprintf(stderr, "window: %lu failed "
992 "XGetWindowAttributes\n", win->id);
993 fprintf(stderr, "window: %lu map_state: %d state: %d "
995 win->id, wa.map_state, state, win->transient);
998 fprintf(stderr, "=================================\n");
1001 #define dumpevent(e)
1003 dumpwins(struct swm_region *r, union arg *args)
1006 #endif /* SWM_DEBUG */
1008 void expose(XEvent *);
1009 void keypress(XEvent *);
1010 void buttonpress(XEvent *);
1011 void configurerequest(XEvent *);
1012 void configurenotify(XEvent *);
1013 void destroynotify(XEvent *);
1014 void enternotify(XEvent *);
1015 void focusevent(XEvent *);
1016 void mapnotify(XEvent *);
1017 void mappingnotify(XEvent *);
1018 void maprequest(XEvent *);
1019 void propertynotify(XEvent *);
1020 void unmapnotify(XEvent *);
1021 void visibilitynotify(XEvent *);
1022 void clientmessage(XEvent *);
1024 void (*handler[LASTEvent])(XEvent *) = {
1026 [KeyPress] = keypress,
1027 [ButtonPress] = buttonpress,
1028 [ConfigureRequest] = configurerequest,
1029 [ConfigureNotify] = configurenotify,
1030 [DestroyNotify] = destroynotify,
1031 [EnterNotify] = enternotify,
1032 [FocusIn] = focusevent,
1033 [FocusOut] = focusevent,
1034 [MapNotify] = mapnotify,
1035 [MappingNotify] = mappingnotify,
1036 [MapRequest] = maprequest,
1037 [PropertyNotify] = propertynotify,
1038 [UnmapNotify] = unmapnotify,
1039 [VisibilityNotify] = visibilitynotify,
1040 [ClientMessage] = clientmessage,
1046 int saved_errno, status;
1049 saved_errno = errno;
1053 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1058 if (errno != ECHILD)
1059 warn("sighdlr: waitpid");
1060 #endif /* SWM_DEBUG */
1063 if (pid == searchpid)
1067 if (WIFEXITED(status)) {
1068 if (WEXITSTATUS(status) != 0)
1069 warnx("sighdlr: child exit status: %d",
1070 WEXITSTATUS(status));
1072 warnx("sighdlr: child is terminated "
1074 #endif /* SWM_DEBUG */
1088 errno = saved_errno;
1094 struct pid_e *p = NULL;
1096 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1101 TAILQ_FOREACH(p, &pidlist, entry) {
1110 name_to_color(char *colorname)
1114 XColor screen_def, exact_def;
1115 unsigned long result = 0;
1116 char cname[32] = "#";
1118 cmap = DefaultColormap(display, screens[0].idx);
1119 status = XAllocNamedColor(display, cmap, colorname,
1120 &screen_def, &exact_def);
1122 strlcat(cname, colorname + 2, sizeof cname - 1);
1123 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1127 result = screen_def.pixel;
1129 fprintf(stderr, "color '%s' not found.\n", colorname);
1135 setscreencolor(char *val, int i, int c)
1137 if (i > 0 && i <= ScreenCount(display)) {
1138 screens[i - 1].c[c].color = name_to_color(val);
1139 free(screens[i - 1].c[c].name);
1140 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1142 } else if (i == -1) {
1143 for (i = 0; i < ScreenCount(display); i++) {
1144 screens[i].c[c].color = name_to_color(val);
1145 free(screens[i].c[c].name);
1146 if ((screens[i].c[c].name = strdup(val)) == NULL)
1150 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1151 i, ScreenCount(display));
1155 fancy_stacker(struct workspace *ws)
1157 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1158 if (ws->cur_layout->l_stack == vertical_stack)
1159 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1160 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1161 if (ws->cur_layout->l_stack == horizontal_stack)
1162 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1163 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1167 plain_stacker(struct workspace *ws)
1169 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1170 if (ws->cur_layout->l_stack == vertical_stack)
1171 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1172 if (ws->cur_layout->l_stack == horizontal_stack)
1173 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1177 custom_region(char *val)
1179 unsigned int sidx, x, y, w, h;
1181 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1182 errx(1, "invalid custom region, "
1183 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1184 if (sidx < 1 || sidx > ScreenCount(display))
1185 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1186 sidx, ScreenCount(display));
1190 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1192 if (x > DisplayWidth(display, sidx) ||
1193 y > DisplayHeight(display, sidx) ||
1194 w + x > DisplayWidth(display, sidx) ||
1195 h + y > DisplayHeight(display, sidx)) {
1196 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1197 "- not within screen boundaries "
1198 "(%ux%u)\n", w, h, x, y,
1199 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1203 new_region(&screens[sidx], x, y, w, h);
1207 socket_setnonblock(int fd)
1211 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1212 err(1, "fcntl F_GETFL");
1213 flags |= O_NONBLOCK;
1214 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1215 err(1, "fcntl F_SETFL");
1219 bar_print(struct swm_region *r, char *s)
1224 XClearWindow(display, r->bar_window);
1225 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1228 textwidth = XTextWidth(bar_fs, s, len);
1230 switch (bar_justify) {
1231 case SWM_BAR_JUSTIFY_LEFT:
1234 case SWM_BAR_JUSTIFY_CENTER:
1235 x = (WIDTH(r) - textwidth) / 2;
1237 case SWM_BAR_JUSTIFY_RIGHT:
1238 x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
1242 if (x < SWM_BAR_OFFSET)
1245 XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
1249 bar_extra_stop(void)
1253 bzero(bar_pipe, sizeof bar_pipe);
1256 kill(bar_pid, SIGTERM);
1259 strlcpy(bar_ext, "", sizeof bar_ext);
1264 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1266 int do_class, do_name;
1268 XClassHint *xch = NULL;
1270 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1271 cur_focus != NULL) {
1272 if ((xch = XAllocClassHint()) == NULL)
1274 status = XGetClassHint(display, cur_focus->id, xch);
1275 if (status == BadWindow || status == BadAlloc)
1277 do_class = (title_class_enabled && xch->res_class != NULL);
1278 do_name = (title_name_enabled && xch->res_name != NULL);
1280 strlcat(s, xch->res_class, sz);
1281 if (do_class && do_name)
1282 strlcat(s, ":", sz);
1284 strlcat(s, xch->res_name, sz);
1285 strlcat(s, " ", sz);
1293 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1297 if (window_name_enabled && cur_focus != NULL) {
1298 XFetchName(display, cur_focus->id, &title);
1300 if (cur_focus->floating)
1301 strlcat(s, "(f) ", sz);
1302 strlcat(s, title, sz);
1303 strlcat(s, " ", sz);
1309 int urgent[SWM_WS_MAX];
1311 bar_urgent(char *s, ssize_t sz)
1313 XWMHints *wmh = NULL;
1318 if (urgent_enabled == 0)
1321 for (i = 0; i < SWM_WS_MAX; i++)
1324 for (i = 0; i < ScreenCount(display); i++)
1325 for (j = 0; j < SWM_WS_MAX; j++)
1326 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1327 wmh = XGetWMHints(display, win->id);
1331 if (wmh->flags & XUrgencyHint)
1336 strlcat(s, "* ", sz);
1337 for (i = 0; i < SWM_WS_MAX; i++) {
1339 snprintf(b, sizeof b, "%d ", i + 1);
1341 snprintf(b, sizeof b, "- ");
1344 strlcat(s, "* ", sz);
1352 struct swm_region *r;
1355 char s[SWM_BAR_MAX];
1356 char cn[SWM_BAR_MAX];
1357 char loc[SWM_BAR_MAX];
1358 char *b, *stack = "";
1360 if (bar_enabled == 0)
1362 if (bar_extra && bar_extra_running) {
1363 /* ignore short reads; it'll correct itself */
1364 while ((b = fgetln(stdin, &len)) != NULL)
1365 if (b && b[len - 1] == '\n') {
1367 strlcpy(bar_ext, b, sizeof bar_ext);
1369 if (b == NULL && errno != EAGAIN) {
1370 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1371 errno, strerror(errno));
1375 strlcpy(bar_ext, "", sizeof bar_ext);
1377 if (clock_enabled == 0)
1378 strlcpy(s, "", sizeof s);
1381 localtime_r(&tmt, &tm);
1382 strftime(s, sizeof s, clock_format, &tm);
1383 strlcat(s, " ", sizeof s);
1386 for (i = 0; i < ScreenCount(display); i++) {
1388 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1389 strlcpy(cn, "", sizeof cn);
1391 bar_urgent(cn, sizeof cn);
1392 bar_class_name(cn, sizeof cn, r->ws->focus);
1393 bar_window_name(cn, sizeof cn, r->ws->focus);
1396 stack = r->ws->stacker;
1398 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1399 x++, r->ws->idx + 1, stack, s, cn, bar_ext, bar_vertext);
1413 bar_toggle(struct swm_region *r, union arg *args)
1415 struct swm_region *tmpr;
1416 int i, sc = ScreenCount(display);
1418 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1421 for (i = 0; i < sc; i++)
1422 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1423 XUnmapWindow(display, tmpr->bar_window);
1425 for (i = 0; i < sc; i++)
1426 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1427 XMapRaised(display, tmpr->bar_window);
1429 bar_enabled = !bar_enabled;
1432 /* must be after stack */
1439 XSetWindowAttributes wa;
1440 struct swm_region *r;
1443 /* do this here because the conf file is in memory */
1444 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1445 /* launch external status app */
1446 bar_extra_running = 1;
1447 if (pipe(bar_pipe) == -1)
1448 err(1, "pipe error");
1449 socket_setnonblock(bar_pipe[0]);
1450 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1451 if (dup2(bar_pipe[0], 0) == -1)
1453 if (dup2(bar_pipe[1], 1) == -1)
1455 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1456 err(1, "could not disable SIGPIPE");
1457 switch (bar_pid = fork()) {
1459 err(1, "cannot fork");
1463 execvp(bar_argv[0], bar_argv);
1464 err(1, "%s external app failed", bar_argv[0]);
1466 default: /* parent */
1472 bzero(&wa, sizeof wa);
1473 for (i = 0; i < ScreenCount(display); i++)
1474 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1476 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1477 wa.background_pixel =
1478 screens[i].c[SWM_S_COLOR_BAR].color;
1479 XChangeWindowAttributes(display, r->bar_window,
1480 CWBackPixel | CWBorderPixel, &wa);
1486 bar_setup(struct swm_region *r)
1491 XFreeFont(display, bar_fs);
1495 for (i = 0; bar_fonts[i] != NULL; i++) {
1496 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1502 if (bar_fonts[i] == NULL)
1503 errx(1, "couldn't load font");
1505 errx(1, "couldn't create font structure");
1507 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1508 2 * bar_border_width;
1510 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1512 r->bar_window = XCreateSimpleWindow(display,
1513 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1514 bar_height - 2 * bar_border_width,
1515 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1516 r->s->c[SWM_S_COLOR_BAR].color);
1517 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1518 XSetFont(display, bar_gc, bar_fs->fid);
1519 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1521 XMapRaised(display, r->bar_window);
1522 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1524 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1525 err(1, "could not install bar_signal");
1530 drain_enter_notify(void)
1535 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1538 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1542 set_win_state(struct ws_win *win, long state)
1544 long data[] = {state, None};
1546 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1551 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1552 (unsigned char *)data, 2);
1559 unsigned char *p = NULL;
1562 if (!get_property(w, astate, 2L, astate, &n, &p))
1565 result = *((long *)p);
1571 version(struct swm_region *r, union arg *args)
1573 bar_version = !bar_version;
1575 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s Build: %s",
1576 SCROTWM_VERSION, buildstr);
1578 strlcpy(bar_vertext, "", sizeof bar_vertext);
1583 client_msg(struct ws_win *win, Atom a)
1585 XClientMessageEvent cm;
1590 bzero(&cm, sizeof cm);
1591 cm.type = ClientMessage;
1592 cm.window = win->id;
1593 cm.message_type = aprot;
1596 cm.data.l[1] = CurrentTime;
1597 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1601 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1609 DNPRINTF(SWM_D_MISC,
1610 "config_win: win %lu x %d y %d w %d h %d\n",
1611 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1613 ce.type = ConfigureNotify;
1614 ce.display = display;
1616 ce.window = win->id;
1619 ce.width = win->g.w;
1620 ce.height = win->g.h;
1621 ce.border_width = border_width;
1623 ce.override_redirect = False;
1625 DNPRINTF(SWM_D_MISC,
1626 "config_win: ev win %lu x %d y %d w %d h %d\n",
1627 ev->window, ev->x, ev->y, ev->width, ev->height);
1628 ce.type = ConfigureNotify;
1629 ce.display = ev->display;
1630 ce.event = ev->window;
1631 ce.window = ev->window;
1634 ce.width = ev->width;
1635 ce.height = ev->height;
1636 ce.border_width = ev->border_width;
1637 ce.above = ev->above;
1638 ce.override_redirect = False;
1641 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1645 count_win(struct workspace *ws, int count_transient)
1650 TAILQ_FOREACH(win, &ws->winlist, entry) {
1651 if (count_transient == 0 && win->floating)
1653 if (count_transient == 0 && win->transient)
1659 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1665 quit(struct swm_region *r, union arg *args)
1667 DNPRINTF(SWM_D_MISC, "quit\n");
1672 unmap_window(struct ws_win *win)
1677 /* don't unmap again */
1678 if (getstate(win->id) == IconicState)
1681 set_win_state(win, IconicState);
1683 XUnmapWindow(display, win->id);
1684 XSetWindowBorder(display, win->id,
1685 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1694 for (i = 0; i < ScreenCount(display); i++)
1695 for (j = 0; j < SWM_WS_MAX; j++)
1696 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1701 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1708 event.display = display; /* Ignored, but what the hell */
1709 event.window = win->id;
1710 event.root = win->s->root;
1711 event.subwindow = None;
1712 event.time = CurrentTime;
1717 event.same_screen = True;
1718 event.keycode = XKeysymToKeycode(display, keysym);
1719 event.state = modifiers;
1721 event.type = KeyPress;
1722 XSendEvent(event.display, event.window, True,
1723 KeyPressMask, (XEvent *)&event);
1725 event.type = KeyRelease;
1726 XSendEvent(event.display, event.window, True,
1727 KeyPressMask, (XEvent *)&event);
1732 restart(struct swm_region *r, union arg *args)
1734 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1736 /* disable alarm because the following code may not be interrupted */
1738 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1739 errx(1, "can't disable alarm");
1744 XCloseDisplay(display);
1745 execvp(start_argv[0], start_argv);
1746 fprintf(stderr, "execvp failed\n");
1752 root_to_region(Window root)
1754 struct swm_region *r = NULL;
1756 int i, x, y, wx, wy;
1759 for (i = 0; i < ScreenCount(display); i++)
1760 if (screens[i].root == root)
1763 if (XQueryPointer(display, screens[i].root,
1764 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1765 /* choose a region based on pointer location */
1766 TAILQ_FOREACH(r, &screens[i].rl, entry)
1767 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1768 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1773 r = TAILQ_FIRST(&screens[i].rl);
1779 find_unmanaged_window(Window id)
1784 for (i = 0; i < ScreenCount(display); i++)
1785 for (j = 0; j < SWM_WS_MAX; j++)
1786 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1794 find_window(Window id)
1797 Window wrr, wpr, *wcr = NULL;
1801 for (i = 0; i < ScreenCount(display); i++)
1802 for (j = 0; j < SWM_WS_MAX; j++)
1803 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1807 /* if we were looking for the parent return that window instead */
1808 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1813 /* ignore not found and root */
1814 if (wpr == 0 || wrr == wpr)
1817 /* look for parent */
1818 for (i = 0; i < ScreenCount(display); i++)
1819 for (j = 0; j < SWM_WS_MAX; j++)
1820 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1828 spawn(int ws_idx, union arg *args, int close_fd)
1833 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1836 close(ConnectionNumber(display));
1838 setenv("LD_PRELOAD", SWM_LIB, 1);
1840 if (asprintf(&ret, "%d", ws_idx) == -1) {
1844 setenv("_SWM_WS", ret, 1);
1848 if (asprintf(&ret, "%d", getpid()) == -1) {
1852 setenv("_SWM_PID", ret, 1);
1856 if (setsid() == -1) {
1863 * close stdin and stdout to prevent interaction between apps
1864 * and the baraction script
1865 * leave stderr open to record errors
1867 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1871 dup2(fd, STDIN_FILENO);
1872 dup2(fd, STDOUT_FILENO);
1877 execvp(args->argv[0], args->argv);
1884 spawnterm(struct swm_region *r, union arg *args)
1886 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1890 setenv("_SWM_XTERM_FONTADJ", "", 1);
1891 spawn(r->ws->idx, args, 1);
1896 kill_refs(struct ws_win *win)
1899 struct swm_region *r;
1900 struct workspace *ws;
1905 for (i = 0; i < ScreenCount(display); i++)
1906 TAILQ_FOREACH(r, &screens[i].rl, entry)
1907 for (x = 0; x < SWM_WS_MAX; x++) {
1909 if (win == ws->focus)
1911 if (win == ws->focus_prev)
1912 ws->focus_prev = NULL;
1917 validate_win(struct ws_win *testwin)
1920 struct workspace *ws;
1921 struct swm_region *r;
1922 int i, x, foundit = 0;
1924 if (testwin == NULL)
1927 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1928 TAILQ_FOREACH(r, &screens[i].rl, entry)
1929 for (x = 0; x < SWM_WS_MAX; x++) {
1931 TAILQ_FOREACH(win, &ws->winlist, entry)
1939 validate_ws(struct workspace *testws)
1941 struct swm_region *r;
1942 struct workspace *ws;
1945 /* validate all ws */
1946 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1947 TAILQ_FOREACH(r, &screens[i].rl, entry)
1948 for (x = 0; x < SWM_WS_MAX; x++) {
1957 unfocus_win(struct ws_win *win)
1962 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1966 if (win->ws == NULL)
1969 if (validate_ws(win->ws))
1970 return; /* XXX this gets hit with thunderbird, needs fixing */
1972 if (win->ws->r == NULL)
1975 if (validate_win(win)) {
1980 if (win->ws->focus == win) {
1981 win->ws->focus = NULL;
1982 win->ws->focus_prev = win;
1985 if (validate_win(win->ws->focus)) {
1986 kill_refs(win->ws->focus);
1987 win->ws->focus = NULL;
1989 if (validate_win(win->ws->focus_prev)) {
1990 kill_refs(win->ws->focus_prev);
1991 win->ws->focus_prev = NULL;
1994 /* drain all previous unfocus events */
1995 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1998 grabbuttons(win, 0);
1999 XSetWindowBorder(display, win->id,
2000 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2002 XChangeProperty(display, win->s->root,
2003 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2004 PropModeReplace, (unsigned char *)&none,1);
2013 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2015 for (i = 0; i < ScreenCount(display); i++)
2016 for (j = 0; j < SWM_WS_MAX; j++)
2017 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2022 focus_win(struct ws_win *win)
2027 struct ws_win *cfw = NULL;
2030 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
2034 if (win->ws == NULL)
2037 if (validate_ws(win->ws))
2038 return; /* XXX this gets hit with thunderbird, needs fixing */
2040 if (validate_win(win)) {
2045 if (validate_win(win)) {
2050 XGetInputFocus(display, &cur_focus, &rr);
2051 if ((cfw = find_window(cur_focus)) != NULL)
2054 /* use larger hammer since the window was killed somehow */
2055 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2056 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2057 XSetWindowBorder(display, cfw->id,
2058 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2061 win->ws->focus = win;
2063 if (win->ws->r != NULL) {
2064 /* drain all previous focus events */
2065 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2069 XSetInputFocus(display, win->id,
2070 RevertToParent, CurrentTime);
2071 grabbuttons(win, 1);
2072 XSetWindowBorder(display, win->id,
2073 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2074 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2075 win->ws->always_raise)
2076 XMapRaised(display, win->id);
2078 XChangeProperty(display, win->s->root,
2079 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2080 PropModeReplace, (unsigned char *)&win->id,1);
2083 if (window_name_enabled)
2088 switchws(struct swm_region *r, union arg *args)
2090 int wsid = args->id, unmap_old = 0;
2091 struct swm_region *this_r, *other_r;
2093 struct workspace *new_ws, *old_ws;
2100 old_ws = this_r->ws;
2101 new_ws = &this_r->s->ws[wsid];
2103 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2104 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2107 if (new_ws == NULL || old_ws == NULL)
2109 if (new_ws == old_ws)
2112 other_r = new_ws->r;
2113 if (other_r == NULL) {
2114 /* the other workspace is hidden, hide this one */
2118 /* the other ws is visible in another region, exchange them */
2119 other_r->ws_prior = new_ws;
2120 other_r->ws = old_ws;
2121 old_ws->r = other_r;
2123 this_r->ws_prior = old_ws;
2124 this_r->ws = new_ws;
2127 /* this is needed so that we can click on a window after a restart */
2131 a.id = SWM_ARG_ID_FOCUSCUR;
2132 focus(new_ws->r, &a);
2136 /* unmap old windows */
2138 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2141 if (focus_mode == SWM_FOCUS_DEFAULT)
2142 drain_enter_notify();
2146 cyclews(struct swm_region *r, union arg *args)
2149 struct swm_screen *s = r->s;
2152 DNPRINTF(SWM_D_WS, "cyclews id %d "
2153 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2154 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2159 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2162 case SWM_ARG_ID_CYCLEWS_UP:
2163 if (a.id < SWM_WS_MAX - 1)
2168 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2171 case SWM_ARG_ID_CYCLEWS_DOWN:
2175 a.id = SWM_WS_MAX - 1;
2182 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2184 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2188 } while (a.id != r->ws->idx);
2192 priorws(struct swm_region *r, union arg *args)
2196 DNPRINTF(SWM_D_WS, "priorws id %d "
2197 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2198 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2200 if (r->ws_prior == NULL)
2203 a.id = r->ws_prior->idx;
2208 cyclescr(struct swm_region *r, union arg *args)
2210 struct swm_region *rr = NULL;
2214 /* do nothing if we don't have more than one screen */
2215 if (!(ScreenCount(display) > 1 || outputs > 1))
2220 case SWM_ARG_ID_CYCLESC_UP:
2221 rr = TAILQ_NEXT(r, entry);
2223 rr = TAILQ_FIRST(&screens[i].rl);
2225 case SWM_ARG_ID_CYCLESC_DOWN:
2226 rr = TAILQ_PREV(r, swm_region_list, entry);
2228 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2236 /* move mouse to region */
2238 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2239 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2241 a.id = SWM_ARG_ID_FOCUSCUR;
2244 if (rr->ws->focus) {
2245 /* move to focus window */
2246 x = rr->ws->focus->g.x + 1;
2247 y = rr->ws->focus->g.y + 1;
2248 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2253 sort_windows(struct ws_win_list *wl)
2255 struct ws_win *win, *parent, *nxt;
2260 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2261 nxt = TAILQ_NEXT(win, entry);
2262 if (win->transient) {
2263 parent = find_window(win->transient);
2264 if (parent == NULL) {
2265 fprintf(stderr, "not possible bug\n");
2268 TAILQ_REMOVE(wl, win, entry);
2269 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2276 swapwin(struct swm_region *r, union arg *args)
2278 struct ws_win *target, *source;
2279 struct ws_win *cur_focus;
2280 struct ws_win_list *wl;
2283 DNPRINTF(SWM_D_WS, "swapwin id %d "
2284 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2285 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2287 cur_focus = r->ws->focus;
2288 if (cur_focus == NULL)
2292 wl = &source->ws->winlist;
2295 case SWM_ARG_ID_SWAPPREV:
2296 if (source->transient)
2297 source = find_window(source->transient);
2298 target = TAILQ_PREV(source, ws_win_list, entry);
2299 if (target && target->transient)
2300 target = find_window(target->transient);
2301 TAILQ_REMOVE(wl, source, entry);
2303 TAILQ_INSERT_TAIL(wl, source, entry);
2305 TAILQ_INSERT_BEFORE(target, source, entry);
2307 case SWM_ARG_ID_SWAPNEXT:
2308 target = TAILQ_NEXT(source, entry);
2309 /* move the parent and let the sort handle the move */
2310 if (source->transient)
2311 source = find_window(source->transient);
2312 TAILQ_REMOVE(wl, source, entry);
2314 TAILQ_INSERT_HEAD(wl, source, entry);
2316 TAILQ_INSERT_AFTER(wl, target, source, entry);
2318 case SWM_ARG_ID_SWAPMAIN:
2319 target = TAILQ_FIRST(wl);
2320 if (target == source) {
2321 if (source->ws->focus_prev != NULL &&
2322 source->ws->focus_prev != target)
2324 source = source->ws->focus_prev;
2328 if (target == NULL || source == NULL)
2330 source->ws->focus_prev = target;
2331 TAILQ_REMOVE(wl, target, entry);
2332 TAILQ_INSERT_BEFORE(source, target, entry);
2333 TAILQ_REMOVE(wl, source, entry);
2334 TAILQ_INSERT_HEAD(wl, source, entry);
2336 case SWM_ARG_ID_MOVELAST:
2337 TAILQ_REMOVE(wl, source, entry);
2338 TAILQ_INSERT_TAIL(wl, source, entry);
2341 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2351 focus_prev(struct ws_win *win)
2353 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2354 struct ws_win *cur_focus = NULL;
2355 struct ws_win_list *wl = NULL;
2356 struct workspace *ws = NULL;
2358 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2360 if (!(win && win->ws))
2365 cur_focus = ws->focus;
2366 winlostfocus = cur_focus;
2368 /* pickle, just focus on whatever */
2369 if (cur_focus == NULL) {
2370 /* use prev_focus if valid */
2371 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2372 find_window(WINID(ws->focus_prev)))
2373 winfocus = ws->focus_prev;
2374 if (winfocus == NULL)
2375 winfocus = TAILQ_FIRST(wl);
2379 /* if transient focus on parent */
2380 if (cur_focus->transient) {
2381 winfocus = find_window(cur_focus->transient);
2385 /* if in max_stack try harder */
2386 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2387 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2388 if (cur_focus != ws->focus_prev)
2389 winfocus = ws->focus_prev;
2390 else if (cur_focus != ws->focus)
2391 winfocus = ws->focus;
2393 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2398 if (cur_focus == win)
2399 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2400 if (winfocus == NULL)
2401 winfocus = TAILQ_LAST(wl, ws_win_list);
2402 if (winfocus == NULL || winfocus == win)
2403 winfocus = TAILQ_NEXT(cur_focus, entry);
2405 if (winfocus == winlostfocus || winfocus == NULL)
2408 focus_magic(winfocus);
2412 focus(struct swm_region *r, union arg *args)
2414 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2415 struct ws_win *cur_focus = NULL;
2416 struct ws_win_list *wl = NULL;
2417 struct workspace *ws = NULL;
2422 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2424 /* treat FOCUS_CUR special */
2425 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2426 if (r->ws->focus && r->ws->focus->iconic == 0)
2427 winfocus = r->ws->focus;
2428 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2429 winfocus = r->ws->focus_prev;
2431 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2432 if (winfocus->iconic == 0)
2435 focus_magic(winfocus);
2439 if ((cur_focus = r->ws->focus) == NULL)
2444 winlostfocus = cur_focus;
2447 case SWM_ARG_ID_FOCUSPREV:
2448 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2450 head = TAILQ_LAST(wl, ws_win_list);
2452 if (WINID(winfocus) == cur_focus->transient) {
2453 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2455 head = TAILQ_LAST(wl, ws_win_list);
2460 if (winfocus && winfocus->iconic) {
2461 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2462 if (winfocus->iconic == 0)
2467 case SWM_ARG_ID_FOCUSNEXT:
2468 head = TAILQ_NEXT(cur_focus, entry);
2470 head = TAILQ_FIRST(wl);
2474 if (winfocus && winfocus->iconic) {
2475 TAILQ_FOREACH(winfocus, wl, entry)
2476 if (winfocus->iconic == 0)
2481 case SWM_ARG_ID_FOCUSMAIN:
2482 winfocus = TAILQ_FIRST(wl);
2483 if (winfocus == cur_focus)
2484 winfocus = cur_focus->ws->focus_prev;
2490 if (winfocus == winlostfocus || winfocus == NULL)
2493 focus_magic(winfocus);
2497 cycle_layout(struct swm_region *r, union arg *args)
2499 struct workspace *ws = r->ws;
2500 struct ws_win *winfocus;
2503 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2505 winfocus = ws->focus;
2508 if (ws->cur_layout->l_stack == NULL)
2509 ws->cur_layout = &layouts[0];
2512 if (focus_mode == SWM_FOCUS_DEFAULT)
2513 drain_enter_notify();
2514 a.id = SWM_ARG_ID_FOCUSCUR;
2520 stack_config(struct swm_region *r, union arg *args)
2522 struct workspace *ws = r->ws;
2524 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2527 if (ws->cur_layout->l_config != NULL)
2528 ws->cur_layout->l_config(ws, args->id);
2530 if (args->id != SWM_ARG_ID_STACKINIT)
2537 struct swm_geometry g;
2538 struct swm_region *r;
2541 DNPRINTF(SWM_D_STACK, "stack\n");
2543 for (i = 0; i < ScreenCount(display); i++) {
2545 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2546 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2547 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2549 /* start with screen geometry, adjust for bar */
2551 g.w -= 2 * border_width;
2552 g.h -= 2 * border_width;
2558 r->ws->cur_layout->l_stack(r->ws, &g);
2559 r->ws->cur_layout->l_string(r->ws);
2560 /* save r so we can track region changes */
2567 if (focus_mode == SWM_FOCUS_DEFAULT)
2568 drain_enter_notify();
2572 store_float_geom(struct ws_win *win, struct swm_region *r)
2574 /* retain window geom and region geom */
2575 win->g_float.x = win->g.x;
2576 win->g_float.y = win->g.y;
2577 win->g_float.w = win->g.w;
2578 win->g_float.h = win->g.h;
2579 win->rg_float.x = r->g.x;
2580 win->rg_float.y = r->g.y;
2581 win->rg_float.w = r->g.w;
2582 win->rg_float.h = r->g.h;
2583 win->g_floatvalid = 1;
2587 stack_floater(struct ws_win *win, struct swm_region *r)
2595 bzero(&wc, sizeof wc);
2596 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2599 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2600 * geom on ws switches or return from max mode
2603 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2604 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2606 * use stored g and rg to set relative position and size
2607 * as in old region or before max stack mode
2609 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2610 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2611 win->g.w = win->g_float.w;
2612 win->g.h = win->g_float.h;
2613 win->g_floatvalid = 0;
2616 win->floatmaxed = 0;
2618 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2619 (win->g.h >= HEIGHT(r)))
2620 wc.border_width = 0;
2622 wc.border_width = border_width;
2623 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2624 win->g.w = (double)WIDTH(r) * dialog_ratio;
2625 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2630 * floaters and transients are auto-centred unless moved
2633 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2634 2 - wc.border_width;
2635 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2636 2 - wc.border_width;
2639 /* win can be outside r if new r smaller than old r */
2640 /* Ensure top left corner inside r (move probs otherwise) */
2641 if (win->g.x < r->g.x - wc.border_width)
2642 win->g.x = r->g.x - wc.border_width;
2643 if (win->g.x > r->g.x + r->g.w - 1)
2644 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2645 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2646 if (win->g.y < r->g.y - wc.border_width)
2647 win->g.y = r->g.y - wc.border_width;
2648 if (win->g.y > r->g.y + r->g.h - 1)
2649 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2650 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2654 wc.width = win->g.w;
2655 wc.height = win->g.h;
2658 * Retain floater and transient geometry for correct positioning
2659 * when ws changes region
2661 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2662 store_float_geom(win, r);
2664 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2665 win->id, wc.x, wc.y, wc.width, wc.height);
2667 XConfigureWindow(display, win->id, mask, &wc);
2671 * Send keystrokes to terminal to decrease/increase the font size as the
2672 * window size changes.
2675 adjust_font(struct ws_win *win)
2677 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2678 win->floating || win->transient)
2681 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2682 win->g.w / win->sh.width_inc < term_width &&
2683 win->font_steps < SWM_MAX_FONT_STEPS) {
2684 win->font_size_boundary[win->font_steps] =
2685 (win->sh.width_inc * term_width) + win->sh.base_width;
2688 win->last_inc = win->sh.width_inc;
2689 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2690 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2691 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2694 win->last_inc = win->sh.width_inc;
2695 fake_keypress(win, XK_KP_Add, ShiftMask);
2699 #define SWAPXY(g) do { \
2701 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2702 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2705 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2708 XWindowAttributes wa;
2709 struct swm_geometry win_g, r_g = *g;
2710 struct ws_win *win, *fs_win = 0;
2711 int i, j, s, stacks;
2712 int w_inc = 1, h_inc, w_base = 1, h_base;
2713 int hrh, extra = 0, h_slice, last_h = 0;
2714 int split, colno, winno, mwin, msize, mscale;
2715 int remain, missing, v_slice, reconfigure;
2718 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2719 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2721 winno = count_win(ws, 0);
2722 if (winno == 0 && count_win(ws, 1) == 0)
2725 TAILQ_FOREACH(win, &ws->winlist, entry)
2726 if (win->transient == 0 && win->floating == 0
2727 && win->iconic == 0)
2734 w_inc = win->sh.width_inc;
2735 w_base = win->sh.base_width;
2736 mwin = ws->l_state.horizontal_mwin;
2737 mscale = ws->l_state.horizontal_msize;
2738 stacks = ws->l_state.horizontal_stacks;
2741 w_inc = win->sh.height_inc;
2742 w_base = win->sh.base_height;
2743 mwin = ws->l_state.vertical_mwin;
2744 mscale = ws->l_state.vertical_msize;
2745 stacks = ws->l_state.vertical_stacks;
2749 if (stacks > winno - mwin)
2750 stacks = winno - mwin;
2754 h_slice = r_g.h / SWM_H_SLICE;
2755 if (mwin && winno > mwin) {
2756 v_slice = r_g.w / SWM_V_SLICE;
2760 win_g.w = v_slice * mscale;
2762 if (w_inc > 1 && w_inc < v_slice) {
2763 /* adjust for window's requested size increment */
2764 remain = (win_g.w - w_base) % w_inc;
2765 missing = w_inc - remain;
2772 win_g.x += r_g.w - msize;
2775 colno = split = winno / stacks;
2776 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2777 2 * border_width) / stacks);
2779 hrh = r_g.h / colno;
2780 extra = r_g.h - (colno * hrh);
2781 win_g.h = hrh - 2 * border_width;
2783 /* stack all the tiled windows */
2784 i = j = 0, s = stacks;
2785 TAILQ_FOREACH(win, &ws->winlist, entry) {
2786 if (win->transient != 0 || win->floating != 0)
2788 if (win->iconic != 0)
2791 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2796 if (split && i == split) {
2797 colno = (winno - mwin) / stacks;
2798 if (s <= (winno - mwin) % stacks)
2800 split = split + colno;
2801 hrh = (r_g.h / colno);
2802 extra = r_g.h - (colno * hrh);
2806 win_g.x += win_g.w + 2 * border_width;
2807 win_g.w = (r_g.w - msize -
2808 (stacks * 2 * border_width)) / stacks;
2810 win_g.w += (r_g.w - msize -
2811 (stacks * 2 * border_width)) % stacks;
2815 win_g.h = hrh - 2 * border_width;
2817 h_inc = win->sh.width_inc;
2818 h_base = win->sh.base_width;
2820 h_inc = win->sh.height_inc;
2821 h_base = win->sh.base_height;
2823 if (j == colno - 1) {
2824 win_g.h = hrh + extra;
2825 } else if (h_inc > 1 && h_inc < h_slice) {
2826 /* adjust for window's requested size increment */
2827 remain = (win_g.h - h_base) % h_inc;
2828 missing = h_inc - remain;
2830 if (missing <= extra || j == 0) {
2842 win_g.y += last_h + 2 * border_width;
2844 bzero(&wc, sizeof wc);
2845 if (disable_border && bar_enabled == 0 && winno == 1){
2846 wc.border_width = 0;
2847 win_g.w += 2 * border_width;
2848 win_g.h += 2 * border_width;
2850 wc.border_width = border_width;
2853 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2854 win->g.w != win_g.h || win->g.h != win_g.w) {
2856 win->g.x = wc.x = win_g.y;
2857 win->g.y = wc.y = win_g.x;
2858 win->g.w = wc.width = win_g.h;
2859 win->g.h = wc.height = win_g.w;
2862 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2863 win->g.w != win_g.w || win->g.h != win_g.h) {
2865 win->g.x = wc.x = win_g.x;
2866 win->g.y = wc.y = win_g.y;
2867 win->g.w = wc.width = win_g.w;
2868 win->g.h = wc.height = win_g.h;
2873 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2874 XConfigureWindow(display, win->id, mask, &wc);
2877 if (XGetWindowAttributes(display, win->id, &wa))
2878 if (wa.map_state == IsUnmapped)
2879 XMapRaised(display, win->id);
2887 /* now, stack all the floaters and transients */
2888 TAILQ_FOREACH(win, &ws->winlist, entry) {
2889 if (win->transient == 0 && win->floating == 0)
2891 if (win->iconic == 1)
2893 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2898 stack_floater(win, ws->r);
2899 XMapRaised(display, win->id);
2903 stack_floater(fs_win, ws->r);
2904 XMapRaised(display, fs_win->id);
2909 vertical_config(struct workspace *ws, int id)
2911 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2914 case SWM_ARG_ID_STACKRESET:
2915 case SWM_ARG_ID_STACKINIT:
2916 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2917 ws->l_state.vertical_mwin = 1;
2918 ws->l_state.vertical_stacks = 1;
2920 case SWM_ARG_ID_MASTERSHRINK:
2921 if (ws->l_state.vertical_msize > 1)
2922 ws->l_state.vertical_msize--;
2924 case SWM_ARG_ID_MASTERGROW:
2925 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2926 ws->l_state.vertical_msize++;
2928 case SWM_ARG_ID_MASTERADD:
2929 ws->l_state.vertical_mwin++;
2931 case SWM_ARG_ID_MASTERDEL:
2932 if (ws->l_state.vertical_mwin > 0)
2933 ws->l_state.vertical_mwin--;
2935 case SWM_ARG_ID_STACKINC:
2936 ws->l_state.vertical_stacks++;
2938 case SWM_ARG_ID_STACKDEC:
2939 if (ws->l_state.vertical_stacks > 1)
2940 ws->l_state.vertical_stacks--;
2948 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2950 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2952 stack_master(ws, g, 0, 0);
2956 horizontal_config(struct workspace *ws, int id)
2958 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2961 case SWM_ARG_ID_STACKRESET:
2962 case SWM_ARG_ID_STACKINIT:
2963 ws->l_state.horizontal_mwin = 1;
2964 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2965 ws->l_state.horizontal_stacks = 1;
2967 case SWM_ARG_ID_MASTERSHRINK:
2968 if (ws->l_state.horizontal_msize > 1)
2969 ws->l_state.horizontal_msize--;
2971 case SWM_ARG_ID_MASTERGROW:
2972 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2973 ws->l_state.horizontal_msize++;
2975 case SWM_ARG_ID_MASTERADD:
2976 ws->l_state.horizontal_mwin++;
2978 case SWM_ARG_ID_MASTERDEL:
2979 if (ws->l_state.horizontal_mwin > 0)
2980 ws->l_state.horizontal_mwin--;
2982 case SWM_ARG_ID_STACKINC:
2983 ws->l_state.horizontal_stacks++;
2985 case SWM_ARG_ID_STACKDEC:
2986 if (ws->l_state.horizontal_stacks > 1)
2987 ws->l_state.horizontal_stacks--;
2995 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2997 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2999 stack_master(ws, g, 1, 0);
3002 /* fullscreen view */
3004 max_stack(struct workspace *ws, struct swm_geometry *g)
3007 struct swm_geometry gg = *g;
3008 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3012 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3017 winno = count_win(ws, 0);
3018 if (winno == 0 && count_win(ws, 1) == 0)
3021 TAILQ_FOREACH(win, &ws->winlist, entry) {
3022 if (win->transient) {
3024 parent = find_window(win->transient);
3028 if (win->floating && win->floatmaxed == 0 ) {
3030 * retain geometry for retrieval on exit from
3033 store_float_geom(win, ws->r);
3034 win->floatmaxed = 1;
3037 /* only reconfigure if necessary */
3038 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3040 bzero(&wc, sizeof wc);
3041 win->g.x = wc.x = gg.x;
3042 win->g.y = wc.y = gg.y;
3044 wc.border_width = border_width;
3045 win->g.w = wc.width = gg.w;
3046 win->g.h = wc.height = gg.h;
3048 wc.border_width = 0;
3049 win->g.w = wc.width = gg.w + 2 * border_width;
3050 win->g.h = wc.height = gg.h + 2 * border_width;
3052 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3053 XConfigureWindow(display, win->id, mask, &wc);
3055 /* unmap only if we don't have multi screen */
3056 if (win != ws->focus)
3057 if (!(ScreenCount(display) > 1 || outputs > 1))
3061 /* put the last transient on top */
3064 XMapRaised(display, parent->id);
3065 stack_floater(wintrans, ws->r);
3066 focus_magic(wintrans);
3071 send_to_ws(struct swm_region *r, union arg *args)
3073 int wsid = args->id;
3074 struct ws_win *win = NULL, *parent;
3075 struct workspace *ws, *nws;
3076 Atom ws_idx_atom = 0;
3077 unsigned char ws_idx_str[SWM_PROPLEN];
3086 if (win->ws->idx == wsid)
3089 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3092 nws = &win->s->ws[wsid];
3094 a.id = SWM_ARG_ID_FOCUSPREV;
3096 if (win->transient) {
3097 parent = find_window(win->transient);
3099 unmap_window(parent);
3100 TAILQ_REMOVE(&ws->winlist, parent, entry);
3101 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3106 TAILQ_REMOVE(&ws->winlist, win, entry);
3107 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3110 /* Try to update the window's workspace property */
3111 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3113 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3115 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3117 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3118 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3125 pressbutton(struct swm_region *r, union arg *args)
3127 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3128 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3132 raise_toggle(struct swm_region *r, union arg *args)
3134 if (r && r->ws == NULL)
3137 r->ws->always_raise = !r->ws->always_raise;
3139 /* bring floaters back to top */
3140 if (r->ws->always_raise == 0)
3145 iconify(struct swm_region *r, union arg *args)
3149 if (r->ws->focus == NULL)
3151 unmap_window(r->ws->focus);
3152 update_iconic(r->ws->focus, 1);
3154 if (focus_mode == SWM_FOCUS_DEFAULT)
3155 drain_enter_notify();
3156 r->ws->focus = NULL;
3157 a.id = SWM_ARG_ID_FOCUSCUR;
3162 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3163 unsigned long *slen)
3166 unsigned long nitems, nbytes, nextra;
3167 unsigned char *prop = NULL;
3170 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3171 &rettype, &retfmt, &nitems, &nbytes, &prop);
3172 if (status != Success)
3176 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3177 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3178 if (status != Success) {
3182 if (rettype != stype) {
3191 uniconify(struct swm_region *r, union arg *args)
3195 unsigned char *name;
3199 DNPRINTF(SWM_D_MISC, "uniconify\n");
3201 if (r && r->ws == NULL)
3204 /* make sure we have anything to uniconify */
3205 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3206 if (win->ws == NULL)
3207 continue; /* should never happen */
3208 if (win->iconic == 0)
3217 spawn_select(r, args, "uniconify", &searchpid);
3219 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3222 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3223 if (win->ws == NULL)
3224 continue; /* should never happen */
3225 if (win->iconic == 0)
3228 name = get_win_name(display, win->id, a_wmname, a_string,
3232 fprintf(lfile, "%s.%lu\n", name, win->id);
3239 #define MAX_RESP_LEN 1024
3242 search_do_resp(void)
3246 unsigned char *name;
3250 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3255 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3256 fprintf(stderr, "search: calloc\n");
3260 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3262 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3265 resp[rbytes] = '\0';
3268 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3269 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3270 if (win->iconic == 0)
3272 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3275 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3280 if (strncmp(s, resp, len) == 0) {
3281 /* XXX this should be a callback to generalize */
3282 update_iconic(win, 0);
3289 close(select_resp_pipe[0]);
3294 wkill(struct swm_region *r, union arg *args)
3296 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3298 if (r->ws->focus == NULL)
3301 if (args->id == SWM_ARG_ID_KILLWINDOW)
3302 XKillClient(display, r->ws->focus->id);
3304 if (r->ws->focus->can_delete)
3305 client_msg(r->ws->focus, adelete);
3310 floating_toggle_win(struct ws_win *win)
3312 struct swm_region *r;
3322 /* reject floating toggles in max stack mode */
3323 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3326 if (win->floating) {
3327 if (!win->floatmaxed) {
3328 /* retain position for refloat */
3329 store_float_geom(win, r);
3333 if (win->g_floatvalid) {
3334 /* refloat at last floating relative position */
3335 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3336 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3337 win->g.w = win->g_float.w;
3338 win->g.h = win->g_float.h;
3343 ewmh_update_actions(win);
3349 floating_toggle(struct swm_region *r, union arg *args)
3351 struct ws_win *win = r->ws->focus;
3357 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3358 _NET_WM_STATE_TOGGLE);
3361 if (focus_mode == SWM_FOCUS_DEFAULT)
3362 drain_enter_notify();
3364 if (win == win->ws->focus) {
3365 a.id = SWM_ARG_ID_FOCUSCUR;
3366 focus(win->ws->r, &a);
3371 resize_window(struct ws_win *win, int center)
3375 struct swm_region *r;
3377 r = root_to_region(win->wa.root);
3378 bzero(&wc, sizeof wc);
3379 mask = CWBorderWidth | CWWidth | CWHeight;
3380 wc.border_width = border_width;
3381 wc.width = win->g.w;
3382 wc.height = win->g.h;
3383 if (center == SWM_ARG_ID_CENTER) {
3384 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3385 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3389 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3390 win->id, wc.x, wc.y, wc.width, wc.height);
3392 XConfigureWindow(display, win->id, mask, &wc);
3395 #define SWM_RESIZE_STEPS (50)
3398 resize(struct ws_win *win, union arg *args)
3402 struct swm_region *r = NULL;
3404 int resize_step = 0;
3410 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3411 win->id, win->floating, win->transient);
3413 if (!(win->transient != 0 || win->floating != 0))
3416 /* reject resizes in max mode for floaters (transient ok) */
3417 if (win->floatmaxed)
3421 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3427 case SWM_ARG_ID_WIDTHSHRINK:
3428 win->g.w -= SWM_RESIZE_STEPS;
3431 case SWM_ARG_ID_WIDTHGROW:
3432 win->g.w += SWM_RESIZE_STEPS;
3435 case SWM_ARG_ID_HEIGHTSHRINK:
3436 win->g.h -= SWM_RESIZE_STEPS;
3439 case SWM_ARG_ID_HEIGHTGROW:
3440 win->g.h += SWM_RESIZE_STEPS;
3447 resize_window(win, 0);
3448 store_float_geom(win,r);
3452 if (focus_mode == SWM_FOCUS_DEFAULT)
3453 drain_enter_notify();
3455 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3456 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3459 /* place pointer at bottom left corner or nearest point inside r */
3460 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3461 relx = win->g.w - 1;
3463 relx = r->g.x + r->g.w - win->g.x - 1;
3465 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3466 rely = win->g.h - 1;
3468 rely = r->g.y + r->g.h - win->g.y - 1;
3470 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3472 XMaskEvent(display, MOUSEMASK | ExposureMask |
3473 SubstructureRedirectMask, &ev);
3475 case ConfigureRequest:
3478 handler[ev.type](&ev);
3481 /* do not allow resize outside of region */
3482 if ( ev.xmotion.x_root < r->g.x ||
3483 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3484 ev.xmotion.y_root < r->g.y ||
3485 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3488 if (ev.xmotion.x <= 1)
3490 if (ev.xmotion.y <= 1)
3492 win->g.w = ev.xmotion.x + 1;
3493 win->g.h = ev.xmotion.y + 1;
3495 /* not free, don't sync more than 120 times / second */
3496 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3497 time = ev.xmotion.time;
3498 XSync(display, False);
3499 resize_window(win, args->id);
3503 } while (ev.type != ButtonRelease);
3505 XSync(display, False);
3506 resize_window(win, args->id);
3508 store_float_geom(win,r);
3510 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3512 XUngrabPointer(display, CurrentTime);
3515 drain_enter_notify();
3519 resize_step(struct swm_region *r, union arg *args)
3521 struct ws_win *win = NULL;
3523 if (r && r->ws && r->ws->focus)
3533 move_window(struct ws_win *win)
3537 struct swm_region *r;
3539 r = root_to_region(win->wa.root);
3540 bzero(&wc, sizeof wc);
3544 wc.border_width = border_width;
3546 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3547 win->id, wc.x, wc.y, wc.width, wc.height);
3549 XConfigureWindow(display, win->id, mask, &wc);
3552 #define SWM_MOVE_STEPS (50)
3555 move(struct ws_win *win, union arg *args)
3560 struct swm_region *r = NULL;
3566 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3567 win->id, win->floating, win->transient);
3569 /* in max_stack mode should only move transients */
3570 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3574 if (win->floating == 0 && !win->transient) {
3575 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3578 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3585 case SWM_ARG_ID_MOVELEFT:
3586 win->g.x -= (SWM_MOVE_STEPS - border_width);
3589 case SWM_ARG_ID_MOVERIGHT:
3590 win->g.x += (SWM_MOVE_STEPS - border_width);
3593 case SWM_ARG_ID_MOVEUP:
3594 win->g.y -= (SWM_MOVE_STEPS - border_width);
3597 case SWM_ARG_ID_MOVEDOWN:
3598 win->g.y += (SWM_MOVE_STEPS - border_width);
3606 store_float_geom(win,r);
3611 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3612 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3614 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3616 XMaskEvent(display, MOUSEMASK | ExposureMask |
3617 SubstructureRedirectMask, &ev);
3619 case ConfigureRequest:
3622 handler[ev.type](&ev);
3625 /* don't allow to move window origin out of region */
3626 if ( ev.xmotion.x_root < r->g.x ||
3627 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3628 ev.xmotion.y_root < r->g.y ||
3629 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3632 win->g.x = ev.xmotion.x_root - border_width;
3633 win->g.y = ev.xmotion.y_root - border_width;
3635 /* not free, don't sync more than 120 times / second */
3636 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3637 time = ev.xmotion.time;
3638 XSync(display, False);
3643 } while (ev.type != ButtonRelease);
3645 XSync(display, False);
3648 store_float_geom(win,r);
3649 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3650 XUngrabPointer(display, CurrentTime);
3653 drain_enter_notify();
3657 move_step(struct swm_region *r, union arg *args)
3659 struct ws_win *win = NULL;
3661 if (r && r->ws && r->ws->focus)
3666 if (!(win->transient != 0 || win->floating != 0))
3673 /* user/key callable function IDs */
3742 kf_dumpwins, /* MUST BE LAST */
3746 /* key definitions */
3748 dummykeyfunc(struct swm_region *r, union arg *args)
3753 legacyfunc(struct swm_region *r, union arg *args)
3758 char name[SWM_FUNCNAME_LEN];
3759 void (*func)(struct swm_region *r, union arg *);
3761 } keyfuncs[kf_invalid + 1] = {
3762 /* name function argument */
3763 { "cycle_layout", cycle_layout, {0} },
3764 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3765 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3766 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3767 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3768 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3769 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3770 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3771 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3772 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3773 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3774 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3775 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3776 { "spawn_term", spawnterm, {.argv = spawn_term} },
3777 { "spawn_menu", legacyfunc, {0} },
3778 { "quit", quit, {0} },
3779 { "restart", restart, {0} },
3780 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3781 { "ws_1", switchws, {.id = 0} },
3782 { "ws_2", switchws, {.id = 1} },
3783 { "ws_3", switchws, {.id = 2} },
3784 { "ws_4", switchws, {.id = 3} },
3785 { "ws_5", switchws, {.id = 4} },
3786 { "ws_6", switchws, {.id = 5} },
3787 { "ws_7", switchws, {.id = 6} },
3788 { "ws_8", switchws, {.id = 7} },
3789 { "ws_9", switchws, {.id = 8} },
3790 { "ws_10", switchws, {.id = 9} },
3791 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3792 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3793 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
3794 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
3795 { "ws_prior", priorws, {0} },
3796 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3797 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3798 { "mvws_1", send_to_ws, {.id = 0} },
3799 { "mvws_2", send_to_ws, {.id = 1} },
3800 { "mvws_3", send_to_ws, {.id = 2} },
3801 { "mvws_4", send_to_ws, {.id = 3} },
3802 { "mvws_5", send_to_ws, {.id = 4} },
3803 { "mvws_6", send_to_ws, {.id = 5} },
3804 { "mvws_7", send_to_ws, {.id = 6} },
3805 { "mvws_8", send_to_ws, {.id = 7} },
3806 { "mvws_9", send_to_ws, {.id = 8} },
3807 { "mvws_10", send_to_ws, {.id = 9} },
3808 { "bar_toggle", bar_toggle, {0} },
3809 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3810 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3811 { "screenshot_all", legacyfunc, {0} },
3812 { "screenshot_wind", legacyfunc, {0} },
3813 { "float_toggle", floating_toggle,{0} },
3814 { "version", version, {0} },
3815 { "spawn_lock", legacyfunc, {0} },
3816 { "spawn_initscr", legacyfunc, {0} },
3817 { "spawn_custom", dummykeyfunc, {0} },
3818 { "iconify", iconify, {0} },
3819 { "uniconify", uniconify, {0} },
3820 { "raise_toggle", raise_toggle, {0} },
3821 { "button2", pressbutton, {2} },
3822 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
3823 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
3824 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
3825 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
3826 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
3827 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
3828 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
3829 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
3830 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3831 { "invalid key func", NULL, {0} },
3836 enum keyfuncid funcid;
3839 int keys_size = 0, keys_length = 0;
3840 struct key *keys = NULL;
3843 enum { client_click, root_click };
3845 unsigned int action;
3847 unsigned int button;
3848 void (*func)(struct ws_win *, union arg *);
3851 /* action key mouse button func args */
3852 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3853 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3854 { client_click, MODKEY, Button1, move, {0} },
3858 update_modkey(unsigned int mod)
3863 for (i = 0; i < keys_length; i++)
3864 if (keys[i].mod & ShiftMask)
3865 keys[i].mod = mod | ShiftMask;
3869 for (i = 0; i < LENGTH(buttons); i++)
3870 if (buttons[i].mask & ShiftMask)
3871 buttons[i].mask = mod | ShiftMask;
3873 buttons[i].mask = mod;
3883 int spawns_size = 0, spawns_length = 0;
3884 struct spawn_prog *spawns = NULL;
3887 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3890 struct spawn_prog *prog = NULL;
3892 char *ap, **real_args;
3894 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3897 for (i = 0; i < spawns_length; i++) {
3898 if (!strcasecmp(spawn_name, spawns[i].name))
3902 fprintf(stderr, "spawn_custom: program %s not found\n",
3907 /* make room for expanded args */
3908 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3909 err(1, "spawn_custom: calloc real_args");
3911 /* expand spawn_args into real_args */
3912 for (i = 0; i < prog->argc; i++) {
3914 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3915 if (!strcasecmp(ap, "$bar_border")) {
3917 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3919 err(1, "spawn_custom border color");
3920 } else if (!strcasecmp(ap, "$bar_color")) {
3922 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3924 err(1, "spawn_custom bar color");
3925 } else if (!strcasecmp(ap, "$bar_font")) {
3926 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3928 err(1, "spawn_custom bar fonts");
3929 } else if (!strcasecmp(ap, "$bar_font_color")) {
3931 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3933 err(1, "spawn_custom color font");
3934 } else if (!strcasecmp(ap, "$color_focus")) {
3936 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3938 err(1, "spawn_custom color focus");
3939 } else if (!strcasecmp(ap, "$color_unfocus")) {
3941 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3943 err(1, "spawn_custom color unfocus");
3945 /* no match --> copy as is */
3946 if ((real_args[i] = strdup(ap)) == NULL)
3947 err(1, "spawn_custom strdup(ap)");
3949 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3954 if ((swm_debug & SWM_D_SPAWN) != 0) {
3955 fprintf(stderr, "spawn_custom: result = ");
3956 for (i = 0; i < prog->argc; i++)
3957 fprintf(stderr, "\"%s\" ", real_args[i]);
3958 fprintf(stderr, "\n");
3961 *ret_args = real_args;
3962 return (prog->argc);
3966 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3972 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3976 spawn(r->ws->idx, &a, 1);
3978 for (i = 0; i < spawn_argc; i++)
3984 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3990 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3994 if (pipe(select_list_pipe) == -1)
3995 err(1, "pipe error");
3996 if (pipe(select_resp_pipe) == -1)
3997 err(1, "pipe error");
3999 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4000 err(1, "could not disable SIGPIPE");
4001 switch (*pid = fork()) {
4003 err(1, "cannot fork");
4006 if (dup2(select_list_pipe[0], 0) == -1)
4008 if (dup2(select_resp_pipe[1], 1) == -1)
4010 close(select_list_pipe[1]);
4011 close(select_resp_pipe[0]);
4012 spawn(r->ws->idx, &a, 0);
4014 default: /* parent */
4015 close(select_list_pipe[0]);
4016 close(select_resp_pipe[1]);
4020 for (i = 0; i < spawn_argc; i++)
4026 setspawn(struct spawn_prog *prog)
4030 if (prog == NULL || prog->name == NULL)
4034 for (i = 0; i < spawns_length; i++) {
4035 if (!strcmp(spawns[i].name, prog->name)) {
4037 if (prog->argv == NULL) {
4039 DNPRINTF(SWM_D_SPAWN,
4040 "setspawn: delete #%d %s\n",
4042 free(spawns[i].name);
4043 for (j = 0; j < spawns[i].argc; j++)
4044 free(spawns[i].argv[j]);
4045 free(spawns[i].argv);
4046 j = spawns_length - 1;
4048 spawns[i] = spawns[j];
4053 DNPRINTF(SWM_D_SPAWN,
4054 "setspawn: replace #%d %s\n",
4056 free(spawns[i].name);
4057 for (j = 0; j < spawns[i].argc; j++)
4058 free(spawns[i].argv[j]);
4059 free(spawns[i].argv);
4062 /* found case handled */
4068 if (prog->argv == NULL) {
4070 "error: setspawn: cannot find program %s", prog->name);
4075 /* not found: add */
4076 if (spawns_size == 0 || spawns == NULL) {
4078 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4079 spawns = malloc((size_t)spawns_size *
4080 sizeof(struct spawn_prog));
4081 if (spawns == NULL) {
4082 fprintf(stderr, "setspawn: malloc failed\n");
4086 } else if (spawns_length == spawns_size) {
4088 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4089 spawns = realloc(spawns, (size_t)spawns_size *
4090 sizeof(struct spawn_prog));
4091 if (spawns == NULL) {
4092 fprintf(stderr, "setspawn: realloc failed\n");
4098 if (spawns_length < spawns_size) {
4099 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4100 spawns_length, prog->name);
4101 i = spawns_length++;
4104 fprintf(stderr, "spawns array problem?\n");
4105 if (spawns == NULL) {
4106 fprintf(stderr, "spawns array is NULL!\n");
4114 setconfspawn(char *selector, char *value, int flags)
4116 struct spawn_prog *prog;
4117 char *vp, *cp, *word;
4119 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4120 if ((prog = calloc(1, sizeof *prog)) == NULL)
4121 err(1, "setconfspawn: calloc prog");
4122 prog->name = strdup(selector);
4123 if (prog->name == NULL)
4124 err(1, "setconfspawn prog->name");
4125 if ((cp = vp = strdup(value)) == NULL)
4126 err(1, "setconfspawn: strdup(value) ");
4127 while ((word = strsep(&cp, " \t")) != NULL) {
4128 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4130 cp += (long)strspn(cp, " \t");
4131 if (strlen(word) > 0) {
4133 if ((prog->argv = realloc(prog->argv,
4134 prog->argc * sizeof(char *))) == NULL)
4135 err(1, "setconfspawn: realloc");
4136 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4137 err(1, "setconfspawn: strdup");
4144 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4151 setconfspawn("term", "xterm", 0);
4152 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4153 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4154 setconfspawn("lock", "xlock", 0);
4155 setconfspawn("initscr", "initscreen.sh", 0);
4156 setconfspawn("menu", "dmenu_run"
4159 " -nf $bar_font_color"
4161 " -sf $bar_color", 0);
4162 setconfspawn("uniconify", "dmenu"
4166 " -nf $bar_font_color"
4168 " -sf $bar_color", 0);
4172 #define SWM_MODNAME_SIZE 32
4173 #define SWM_KEY_WS "\n+ \t"
4175 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4179 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4180 if (mod == NULL || ks == NULL) {
4181 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4184 if (keystr == NULL || strlen(keystr) == 0) {
4185 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4191 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4192 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4194 cp += (long)strspn(cp, SWM_KEY_WS);
4195 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4197 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4199 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4201 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4203 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4205 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4207 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4208 *mod |= ControlMask;
4210 *ks = XStringToKeysym(name);
4211 XConvertCase(*ks, ks, &uks);
4212 if (ks == NoSymbol) {
4214 "parsekeys: invalid key %s\n",
4220 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4225 strdupsafe(char *str)
4230 return (strdup(str));
4234 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4238 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4239 keyfuncs[kfid].name, spawn_name);
4241 for (i = 0; i < keys_length; i++) {
4242 if (keys[i].mod == mod && keys[i].keysym == ks) {
4243 if (kfid == kf_invalid) {
4246 "setkeybinding: delete #%d %s\n",
4247 i, keyfuncs[keys[i].funcid].name);
4248 free(keys[i].spawn_name);
4249 j = keys_length - 1;
4253 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4256 /* found: replace */
4258 "setkeybinding: replace #%d %s %s\n",
4259 i, keyfuncs[keys[i].funcid].name,
4261 free(keys[i].spawn_name);
4263 keys[i].keysym = ks;
4264 keys[i].funcid = kfid;
4265 keys[i].spawn_name = strdupsafe(spawn_name);
4266 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4271 if (kfid == kf_invalid) {
4273 "error: setkeybinding: cannot find mod/key combination");
4274 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4277 /* not found: add */
4278 if (keys_size == 0 || keys == NULL) {
4280 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4281 keys = malloc((size_t)keys_size * sizeof(struct key));
4283 fprintf(stderr, "malloc failed\n");
4287 } else if (keys_length == keys_size) {
4289 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4290 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4292 fprintf(stderr, "realloc failed\n");
4297 if (keys_length < keys_size) {
4299 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4300 j, keyfuncs[kfid].name, spawn_name);
4302 keys[j].keysym = ks;
4303 keys[j].funcid = kfid;
4304 keys[j].spawn_name = strdupsafe(spawn_name);
4306 fprintf(stderr, "keys array problem?\n");
4308 fprintf(stderr, "keys array problem\n");
4312 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4316 setconfbinding(char *selector, char *value, int flags)
4318 enum keyfuncid kfid;
4322 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4323 if (selector == NULL) {
4324 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4325 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4327 setkeybinding(mod, ks, kfid, NULL);
4332 /* search by key function name */
4333 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4334 if (strncasecmp(selector, keyfuncs[kfid].name,
4335 SWM_FUNCNAME_LEN) == 0) {
4336 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4338 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4339 setkeybinding(mod, ks, kfid, NULL);
4345 /* search by custom spawn name */
4346 for (i = 0; i < spawns_length; i++) {
4347 if (strcasecmp(selector, spawns[i].name) == 0) {
4348 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4350 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4351 setkeybinding(mod, ks, kf_spawn_custom,
4358 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4365 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4366 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4367 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4368 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4369 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4370 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4371 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4372 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4373 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4374 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4375 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4376 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4377 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4378 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4379 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4380 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4381 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4382 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4383 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4384 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4385 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4386 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4387 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4388 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4389 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4390 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4391 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4392 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4393 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4394 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4395 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4396 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4397 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4398 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4399 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4400 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4401 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4402 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4403 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4404 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4405 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4406 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4407 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4408 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4409 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4410 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4411 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4412 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4413 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4414 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4415 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4416 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4417 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4418 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4419 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4420 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4421 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4422 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4423 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4424 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4425 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4426 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4427 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4428 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4429 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4430 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4431 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4432 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4434 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4443 /* clear all key bindings, if any */
4444 for (i = 0; i < keys_length; i++)
4445 free(keys[i].spawn_name);
4450 setkeymapping(char *selector, char *value, int flags)
4452 char keymapping_file[PATH_MAX];
4453 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4454 if (value[0] == '~')
4455 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4456 pwd->pw_dir, &value[1]);
4458 strlcpy(keymapping_file, value, sizeof keymapping_file);
4460 /* load new key bindings; if it fails, revert to default bindings */
4461 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4465 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4470 updatenumlockmask(void)
4473 XModifierKeymap *modmap;
4475 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4477 modmap = XGetModifierMapping(display);
4478 for (i = 0; i < 8; i++)
4479 for (j = 0; j < modmap->max_keypermod; j++)
4480 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4481 == XKeysymToKeycode(display, XK_Num_Lock))
4482 numlockmask = (1 << i);
4484 XFreeModifiermap(modmap);
4490 unsigned int i, j, k;
4492 unsigned int modifiers[] =
4493 { 0, LockMask, numlockmask, numlockmask | LockMask };
4495 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4496 updatenumlockmask();
4498 for (k = 0; k < ScreenCount(display); k++) {
4499 if (TAILQ_EMPTY(&screens[k].rl))
4501 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4502 for (i = 0; i < keys_length; i++) {
4503 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4504 for (j = 0; j < LENGTH(modifiers); j++)
4505 XGrabKey(display, code,
4506 keys[i].mod | modifiers[j],
4507 screens[k].root, True,
4508 GrabModeAsync, GrabModeAsync);
4514 grabbuttons(struct ws_win *win, int focused)
4517 unsigned int modifiers[] =
4518 { 0, LockMask, numlockmask, numlockmask|LockMask };
4520 updatenumlockmask();
4521 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4523 for (i = 0; i < LENGTH(buttons); i++)
4524 if (buttons[i].action == client_click)
4525 for (j = 0; j < LENGTH(modifiers); j++)
4526 XGrabButton(display, buttons[i].button,
4527 buttons[i].mask | modifiers[j],
4528 win->id, False, BUTTONMASK,
4529 GrabModeAsync, GrabModeSync, None,
4532 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4533 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4536 const char *quirkname[] = {
4537 "NONE", /* config string for "no value" */
4546 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4547 #define SWM_Q_WS "\n|+ \t"
4549 parsequirks(char *qstr, unsigned long *quirk)
4559 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4561 cp += (long)strspn(cp, SWM_Q_WS);
4562 for (i = 0; i < LENGTH(quirkname); i++) {
4563 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4564 DNPRINTF(SWM_D_QUIRK,
4565 "parsequirks: %s\n", name);
4570 *quirk |= 1 << (i-1);
4574 if (i >= LENGTH(quirkname)) {
4575 DNPRINTF(SWM_D_QUIRK,
4576 "parsequirks: invalid quirk [%s]\n", name);
4584 setquirk(const char *class, const char *name, const int quirk)
4589 for (i = 0; i < quirks_length; i++) {
4590 if (!strcmp(quirks[i].class, class) &&
4591 !strcmp(quirks[i].name, name)) {
4594 DNPRINTF(SWM_D_QUIRK,
4595 "setquirk: delete #%d %s:%s\n",
4596 i, quirks[i].class, quirks[i].name);
4597 free(quirks[i].class);
4598 free(quirks[i].name);
4599 j = quirks_length - 1;
4601 quirks[i] = quirks[j];
4605 /* found: replace */
4606 DNPRINTF(SWM_D_QUIRK,
4607 "setquirk: replace #%d %s:%s\n",
4608 i, quirks[i].class, quirks[i].name);
4609 free(quirks[i].class);
4610 free(quirks[i].name);
4611 quirks[i].class = strdup(class);
4612 quirks[i].name = strdup(name);
4613 quirks[i].quirk = quirk;
4620 "error: setquirk: cannot find class/name combination");
4623 /* not found: add */
4624 if (quirks_size == 0 || quirks == NULL) {
4626 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4627 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4628 if (quirks == NULL) {
4629 fprintf(stderr, "setquirk: malloc failed\n");
4633 } else if (quirks_length == quirks_size) {
4635 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4636 quirks = realloc(quirks,
4637 (size_t)quirks_size * sizeof(struct quirk));
4638 if (quirks == NULL) {
4639 fprintf(stderr, "setquirk: realloc failed\n");
4644 if (quirks_length < quirks_size) {
4645 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4646 j = quirks_length++;
4647 quirks[j].class = strdup(class);
4648 quirks[j].name = strdup(name);
4649 quirks[j].quirk = quirk;
4651 fprintf(stderr, "quirks array problem?\n");
4652 if (quirks == NULL) {
4653 fprintf(stderr, "quirks array problem!\n");
4660 setconfquirk(char *selector, char *value, int flags)
4662 char *cp, *class, *name;
4664 unsigned long quirks;
4665 if (selector == NULL)
4667 if ((cp = strchr(selector, ':')) == NULL)
4672 if ((retval = parsequirks(value, &quirks)) == 0)
4673 setquirk(class, name, quirks);
4680 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4681 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4682 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4683 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4684 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4685 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4686 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4687 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4688 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4689 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4690 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4691 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4692 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4695 /* conf file stuff */
4696 #define SWM_CONF_FILE "scrotwm.conf"
4698 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4699 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4700 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4701 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4702 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
4703 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4704 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4705 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
4706 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
4710 setconfvalue(char *selector, char *value, int flags)
4715 case SWM_S_BAR_DELAY:
4716 bar_delay = atoi(value);
4718 case SWM_S_BAR_ENABLED:
4719 bar_enabled = atoi(value);
4721 case SWM_S_BAR_BORDER_WIDTH:
4722 bar_border_width = atoi(value);
4724 case SWM_S_BAR_AT_BOTTOM:
4725 bar_at_bottom = atoi(value);
4727 case SWM_S_BAR_JUSTIFY:
4728 if (!strcmp(value, "left"))
4729 bar_justify = SWM_BAR_JUSTIFY_LEFT;
4730 else if (!strcmp(value, "center"))
4731 bar_justify = SWM_BAR_JUSTIFY_CENTER;
4732 else if (!strcmp(value, "right"))
4733 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
4735 errx(1, "invalid bar_justify");
4737 case SWM_S_STACK_ENABLED:
4738 stack_enabled = atoi(value);
4740 case SWM_S_CLOCK_ENABLED:
4741 clock_enabled = atoi(value);
4743 case SWM_S_CLOCK_FORMAT:
4744 #ifndef SWM_DENY_CLOCK_FORMAT
4746 if ((clock_format = strdup(value)) == NULL)
4747 err(1, "setconfvalue: clock_format");
4750 case SWM_S_CYCLE_EMPTY:
4751 cycle_empty = atoi(value);
4753 case SWM_S_CYCLE_VISIBLE:
4754 cycle_visible = atoi(value);
4756 case SWM_S_SS_ENABLED:
4757 ss_enabled = atoi(value);
4759 case SWM_S_TERM_WIDTH:
4760 term_width = atoi(value);
4762 case SWM_S_TITLE_CLASS_ENABLED:
4763 title_class_enabled = atoi(value);
4765 case SWM_S_WINDOW_NAME_ENABLED:
4766 window_name_enabled = atoi(value);
4768 case SWM_S_TITLE_NAME_ENABLED:
4769 title_name_enabled = atoi(value);
4771 case SWM_S_URGENT_ENABLED:
4772 urgent_enabled = atoi(value);
4774 case SWM_S_FOCUS_MODE:
4775 if (!strcmp(value, "default"))
4776 focus_mode = SWM_FOCUS_DEFAULT;
4777 else if (!strcmp(value, "follow_cursor"))
4778 focus_mode = SWM_FOCUS_FOLLOW;
4779 else if (!strcmp(value, "synergy"))
4780 focus_mode = SWM_FOCUS_SYNERGY;
4782 err(1, "focus_mode");
4784 case SWM_S_DISABLE_BORDER:
4785 disable_border = atoi(value);
4787 case SWM_S_BORDER_WIDTH:
4788 border_width = atoi(value);
4790 case SWM_S_BAR_FONT:
4792 if ((bar_fonts[0] = strdup(value)) == NULL)
4793 err(1, "setconfvalue: bar_font");
4795 case SWM_S_BAR_ACTION:
4797 if ((bar_argv[0] = strdup(value)) == NULL)
4798 err(1, "setconfvalue: bar_action");
4800 case SWM_S_SPAWN_TERM:
4801 free(spawn_term[0]);
4802 if ((spawn_term[0] = strdup(value)) == NULL)
4803 err(1, "setconfvalue: spawn_term");
4807 case SWM_S_DIALOG_RATIO:
4808 dialog_ratio = atof(value);
4809 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4812 case SWM_S_VERBOSE_LAYOUT:
4813 verbose_layout = atoi(value);
4814 for (i = 0; layouts[i].l_stack != NULL; i++) {
4816 layouts[i].l_string = fancy_stacker;
4818 layouts[i].l_string = plain_stacker;
4828 setconfmodkey(char *selector, char *value, int flags)
4830 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4831 update_modkey(Mod1Mask);
4832 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4833 update_modkey(Mod2Mask);
4834 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4835 update_modkey(Mod3Mask);
4836 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4837 update_modkey(Mod4Mask);
4844 setconfcolor(char *selector, char *value, int flags)
4846 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4851 setconfregion(char *selector, char *value, int flags)
4853 custom_region(value);
4858 setautorun(char *selector, char *value, int flags)
4868 if (getenv("SWM_STARTED"))
4872 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4873 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4875 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4876 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4879 * This is a little intricate
4881 * If the pid already exists we simply reuse it because it means it was
4882 * used before AND not claimed by manage_window. We get away with
4883 * altering it in the parent after INSERT because this can not be a race
4886 while ((ap = strsep(&sp, " \t")) != NULL) {
4889 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4891 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4892 err(1, "setautorun: realloc");
4893 a.argv[argc - 1] = ap;
4896 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4897 err(1, "setautorun: realloc");
4898 a.argv[argc] = NULL;
4900 if ((pid = fork()) == 0) {
4901 spawn(ws_id, &a, 1);
4910 p = calloc(1, sizeof *p);
4913 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4923 setlayout(char *selector, char *value, int flags)
4925 int ws_id, i, x, mg, ma, si, raise;
4926 int st = SWM_V_STACK;
4928 struct workspace *ws;
4930 if (getenv("SWM_STARTED"))
4934 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4935 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4936 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4937 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4940 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4941 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4943 if (!strcasecmp(s, "vertical"))
4945 else if (!strcasecmp(s, "horizontal"))
4947 else if (!strcasecmp(s, "fullscreen"))
4950 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4951 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4954 for (i = 0; i < ScreenCount(display); i++) {
4955 ws = (struct workspace *)&screens[i].ws;
4956 ws[ws_id].cur_layout = &layouts[st];
4958 ws[ws_id].always_raise = raise;
4959 if (st == SWM_MAX_STACK)
4963 for (x = 0; x < abs(mg); x++) {
4964 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4965 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4966 SWM_ARG_ID_MASTERSHRINK);
4970 for (x = 0; x < abs(ma); x++) {
4971 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4972 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4973 SWM_ARG_ID_MASTERDEL);
4977 for (x = 0; x < abs(si); x++) {
4978 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4979 si >= 0 ? SWM_ARG_ID_STACKINC :
4980 SWM_ARG_ID_STACKDEC);
4988 /* config options */
4989 struct config_option {
4991 int (*func)(char*, char*, int);
4994 struct config_option configopt[] = {
4995 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4996 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4997 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4998 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4999 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5000 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5001 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5002 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5003 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5004 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5005 { "keyboard_mapping", setkeymapping, 0 },
5006 { "bind", setconfbinding, 0 },
5007 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5008 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5009 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5010 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5011 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5012 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5013 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5014 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5015 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5016 { "modkey", setconfmodkey, 0 },
5017 { "program", setconfspawn, 0 },
5018 { "quirk", setconfquirk, 0 },
5019 { "region", setconfregion, 0 },
5020 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5021 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5022 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5023 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5024 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5025 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5026 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5027 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5028 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5029 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5030 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5031 { "autorun", setautorun, 0 },
5032 { "layout", setlayout, 0 },
5037 conf_load(char *filename, int keymapping)
5040 char *line, *cp, *optsub, *optval;
5041 size_t linelen, lineno = 0;
5042 int wordlen, i, optind;
5043 struct config_option *opt;
5045 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
5047 if (filename == NULL) {
5048 fprintf(stderr, "conf_load: no filename\n");
5051 if ((config = fopen(filename, "r")) == NULL) {
5052 warn("conf_load: fopen: %s", filename);
5056 while (!feof(config)) {
5057 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5060 err(1, "%s", filename);
5065 cp += strspn(cp, " \t\n"); /* eat whitespace */
5066 if (cp[0] == '\0') {
5071 /* get config option */
5072 wordlen = strcspn(cp, "=[ \t\n");
5074 warnx("%s: line %zd: no option found",
5079 for (i = 0; i < LENGTH(configopt); i++) {
5080 opt = &configopt[i];
5081 if (!strncasecmp(cp, opt->optname, wordlen) &&
5082 strlen(opt->optname) == wordlen) {
5088 warnx("%s: line %zd: unknown option %.*s",
5089 filename, lineno, wordlen, cp);
5092 if (keymapping && strcmp(opt->optname, "bind")) {
5093 warnx("%s: line %zd: invalid option %.*s",
5094 filename, lineno, wordlen, cp);
5098 cp += strspn(cp, " \t\n"); /* eat whitespace */
5099 /* get [selector] if any */
5103 wordlen = strcspn(cp, "]");
5106 warnx("%s: line %zd: syntax error",
5110 asprintf(&optsub, "%.*s", wordlen, cp);
5113 cp += strspn(cp, "] \t\n"); /* eat trailing */
5115 cp += strspn(cp, "= \t\n"); /* eat trailing */
5117 optval = strdup(cp);
5118 /* call function to deal with it all */
5119 if (configopt[optind].func(optsub, optval,
5120 configopt[optind].funcflags) != 0) {
5121 fprintf(stderr, "%s line %zd: %s\n",
5122 filename, lineno, line);
5123 errx(1, "%s: line %zd: invalid data for %s",
5124 filename, lineno, configopt[optind].optname);
5132 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5138 set_child_transient(struct ws_win *win, Window *trans)
5140 struct ws_win *parent, *w;
5141 XWMHints *wmh = NULL;
5142 struct swm_region *r;
5143 struct workspace *ws;
5145 parent = find_window(win->transient);
5147 parent->child_trans = win;
5149 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5150 " for %lu trans %lu\n", win->id, win->transient);
5152 if (win->hints == NULL) {
5153 fprintf(stderr, "no hints for %lu\n", win->id);
5157 r = root_to_region(win->wa.root);
5159 /* parent doen't exist in our window list */
5160 TAILQ_FOREACH(w, &ws->winlist, entry) {
5164 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5165 fprintf(stderr, "can't get hints for %lu\n",
5170 if (win->hints->window_group != wmh->window_group)
5173 w->child_trans = win;
5174 win->transient = w->id;
5176 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5177 "transient to %lu\n", win->transient);
5187 window_get_pid(Window win)
5189 Atom actual_type_return;
5190 int actual_format_return = 0;
5191 unsigned long nitems_return = 0;
5192 unsigned long bytes_after_return = 0;
5196 unsigned char *prop = NULL;
5198 if (XGetWindowProperty(display, win,
5199 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5200 XA_CARDINAL, &actual_type_return, &actual_format_return,
5201 &nitems_return, &bytes_after_return,
5202 (unsigned char**)(void*)&pid) != Success)
5204 if (actual_type_return != XA_CARDINAL)
5215 if (XGetWindowProperty(display, win,
5216 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5217 XA_STRING, &actual_type_return, &actual_format_return,
5218 &nitems_return, &bytes_after_return, &prop) != Success)
5220 if (actual_type_return != XA_STRING)
5225 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5226 /* ignore error because strtonum returns 0 anyway */
5233 manage_window(Window id)
5236 struct workspace *ws;
5237 struct ws_win *win, *ww;
5238 int format, i, ws_idx, n, border_me = 0;
5239 unsigned long nitems, bytes;
5240 Atom ws_idx_atom = 0, type;
5241 Atom *prot = NULL, *pp;
5242 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5243 struct swm_region *r;
5249 if ((win = find_window(id)) != NULL)
5250 return (win); /* already being managed */
5252 /* see if we are on the unmanaged list */
5253 if ((win = find_unmanaged_window(id)) != NULL) {
5254 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5256 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5257 if (win->transient) {
5258 set_child_transient(win, &trans);
5259 } if (trans && (ww = find_window(trans)))
5260 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5262 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5263 ewmh_update_actions(win);
5267 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5268 errx(1, "calloc: failed to allocate memory for new window");
5272 /* see if we need to override the workspace */
5273 p = find_pid(window_get_pid(id));
5275 /* Get all the window data in one shot */
5276 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5278 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5279 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5281 XGetWindowAttributes(display, id, &win->wa);
5282 XGetWMNormalHints(display, id, &win->sh, &mask);
5283 win->hints = XGetWMHints(display, id);
5284 XGetTransientForHint(display, id, &trans);
5286 win->transient = trans;
5287 set_child_transient(win, &trans);
5288 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5289 win->id, win->transient);
5292 /* get supported protocols */
5293 if (XGetWMProtocols(display, id, &prot, &n)) {
5294 for (i = 0, pp = prot; i < n; i++, pp++) {
5295 if (*pp == takefocus)
5296 win->take_focus = 1;
5298 win->can_delete = 1;
5304 win->iconic = get_iconic(win);
5307 * Figure out where to put the window. If it was previously assigned to
5308 * a workspace (either by spawn() or manually moving), and isn't
5309 * transient, * put it in the same workspace
5311 r = root_to_region(win->wa.root);
5313 ws = &r->s->ws[p->ws];
5314 TAILQ_REMOVE(&pidlist, p, entry);
5317 } else if (prop && win->transient == 0) {
5318 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5319 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5321 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5324 ws = &r->s->ws[ws_idx];
5327 /* this should launch transients in the same ws as parent */
5329 if ((ww = find_window(trans)) != NULL)
5336 "fix this bug mcbride\n");
5341 /* set up the window layout */
5344 win->s = r->s; /* this never changes */
5345 if (trans && (ww = find_window(trans)))
5346 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5348 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5350 win->g.w = win->wa.width;
5351 win->g.h = win->wa.height;
5352 win->g.x = win->wa.x;
5353 win->g.y = win->wa.y;
5354 win->g_floatvalid = 0;
5355 win->floatmaxed = 0;
5356 win->ewmh_flags = 0;
5358 /* Set window properties so we can remember this after reincarnation */
5359 if (ws_idx_atom && prop == NULL &&
5360 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5362 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5364 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5365 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5370 ewmh_autoquirk(win);
5372 if (XGetClassHint(display, win->id, &win->ch)) {
5373 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5374 win->ch.res_class, win->ch.res_name);
5376 /* java is retarded so treat it special */
5377 if (strstr(win->ch.res_name, "sun-awt")) {
5382 for (i = 0; i < quirks_length; i++){
5383 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5384 !strcmp(win->ch.res_name, quirks[i].name)) {
5385 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5386 win->ch.res_class, win->ch.res_name);
5387 if (quirks[i].quirk & SWM_Q_FLOAT) {
5391 win->quirks = quirks[i].quirk;
5396 /* alter window position if quirky */
5397 if (win->quirks & SWM_Q_ANYWHERE) {
5398 win->manual = 1; /* don't center the quirky windows */
5399 bzero(&wc, sizeof wc);
5401 if (bar_enabled && win->g.y < bar_height) {
5402 win->g.y = wc.y = bar_height;
5405 if (win->g.w + win->g.x > WIDTH(r)) {
5406 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5412 /* Reset font sizes (the bruteforce way; no default keybinding). */
5413 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5414 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5415 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5416 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5417 fake_keypress(win, XK_KP_Add, ShiftMask);
5420 ewmh_get_win_state(win);
5421 ewmh_update_actions(win);
5422 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5426 bzero(&wc, sizeof wc);
5427 wc.border_width = border_width;
5428 mask = CWBorderWidth;
5429 XConfigureWindow(display, win->id, mask, &wc);
5432 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5433 PropertyChangeMask | StructureNotifyMask);
5435 /* floaters need to be mapped if they are in the current workspace */
5436 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5437 XMapRaised(display, win->id);
5443 free_window(struct ws_win *win)
5445 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5450 /* needed for restart wm */
5451 set_win_state(win, WithdrawnState);
5453 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5455 if (win->ch.res_class)
5456 XFree(win->ch.res_class);
5457 if (win->ch.res_name)
5458 XFree(win->ch.res_name);
5463 memset(win, 0xff, sizeof *win); /* XXX kill later */
5469 unmanage_window(struct ws_win *win)
5471 struct ws_win *parent;
5476 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5478 if (win->transient) {
5479 parent = find_window(win->transient);
5481 parent->child_trans = NULL;
5484 /* focus on root just in case */
5485 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5489 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5490 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5496 focus_magic(struct ws_win *win)
5498 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5503 if (win->child_trans) {
5504 /* win = parent & has a transient so focus on that */
5506 focus_win(win->child_trans);
5507 if (win->child_trans->take_focus)
5508 client_msg(win, takefocus);
5510 /* make sure transient hasn't dissapeared */
5511 if (validate_win(win->child_trans) == 0) {
5512 focus_win(win->child_trans);
5513 if (win->child_trans->take_focus)
5514 client_msg(win->child_trans, takefocus);
5516 win->child_trans = NULL;
5518 if (win->take_focus)
5519 client_msg(win, takefocus);
5525 if (win->take_focus)
5526 client_msg(win, takefocus);
5533 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5541 XKeyEvent *ev = &e->xkey;
5543 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5545 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5546 for (i = 0; i < keys_length; i++)
5547 if (keysym == keys[i].keysym
5548 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5549 && keyfuncs[keys[i].funcid].func) {
5550 if (keys[i].funcid == kf_spawn_custom)
5552 root_to_region(ev->root),
5553 &(keyfuncs[keys[i].funcid].args),
5557 keyfuncs[keys[i].funcid].func(
5558 root_to_region(ev->root),
5559 &(keyfuncs[keys[i].funcid].args)
5565 buttonpress(XEvent *e)
5569 XButtonPressedEvent *ev = &e->xbutton;
5571 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5573 if ((win = find_window(ev->window)) == NULL)
5577 action = client_click;
5579 for (i = 0; i < LENGTH(buttons); i++)
5580 if (action == buttons[i].action && buttons[i].func &&
5581 buttons[i].button == ev->button &&
5582 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5583 buttons[i].func(win, &buttons[i].args);
5587 configurerequest(XEvent *e)
5589 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5594 if ((win = find_window(ev->window)) == NULL)
5595 if ((win = find_unmanaged_window(ev->window)) == NULL)
5599 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5601 bzero(&wc, sizeof wc);
5604 wc.width = ev->width;
5605 wc.height = ev->height;
5606 wc.border_width = ev->border_width;
5607 wc.sibling = ev->above;
5608 wc.stack_mode = ev->detail;
5609 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5611 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5613 config_win(win, ev);
5618 configurenotify(XEvent *e)
5623 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5624 e->xconfigure.window);
5626 win = find_window(e->xconfigure.window);
5628 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5632 if (focus_mode == SWM_FOCUS_DEFAULT)
5633 drain_enter_notify();
5638 destroynotify(XEvent *e)
5641 XDestroyWindowEvent *ev = &e->xdestroywindow;
5643 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5645 if ((win = find_window(ev->window)) == NULL) {
5646 if ((win = find_unmanaged_window(ev->window)) == NULL)
5652 /* make sure we focus on something */
5655 unmanage_window(win);
5657 if (focus_mode == SWM_FOCUS_DEFAULT)
5658 drain_enter_notify();
5663 enternotify(XEvent *e)
5665 XCrossingEvent *ev = &e->xcrossing;
5670 Window focus_return;
5671 int revert_to_return;
5673 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5674 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5675 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5676 ev->same_screen, ev->focus, ev->state);
5678 switch (focus_mode) {
5679 case SWM_FOCUS_DEFAULT:
5681 case SWM_FOCUS_FOLLOW:
5683 case SWM_FOCUS_SYNERGY:
5686 * all these checks need to be in this order because the
5687 * XCheckTypedWindowEvent relies on weeding out the previous events
5689 * making this code an option would enable a follow mouse for focus
5694 * state is set when we are switching workspaces and focus is set when
5695 * the window or a subwindow already has focus (occurs during restart).
5697 * Only honor the focus flag if last_focus_event is not FocusOut,
5698 * this allows scrotwm to continue to control focus when another
5699 * program is also playing with it.
5701 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5702 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5707 * happens when a window is created or destroyed and the border
5708 * crosses the mouse pointer and when switching ws
5710 * we need the subwindow test to see if we came from root in order
5711 * to give focus to floaters
5713 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5714 ev->subwindow == 0) {
5715 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5719 /* this window already has focus */
5720 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5721 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5725 /* this window is being deleted or moved to another ws */
5726 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5728 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5729 XPutBackEvent(display, &cne);
5733 if ((win = find_window(ev->window)) == NULL) {
5734 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5739 * In fullstack kill all enters unless they come from a different ws
5740 * (i.e. another region) or focus has been grabbed externally.
5742 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5743 last_focus_event != FocusOut) {
5744 XGetInputFocus(display, &focus_return, &revert_to_return);
5745 if ((w = find_window(focus_return)) == NULL ||
5747 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5755 if ((win = find_window(ev->window)) == NULL) {
5756 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5761 * if we have more enternotifies let them handle it in due time
5763 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5764 DNPRINTF(SWM_D_EVENT,
5765 "ignoring enternotify: got more enternotify\n");
5766 XPutBackEvent(display, &cne);
5773 /* lets us use one switch statement for arbitrary mode/detail combinations */
5774 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5777 focusevent(XEvent *e)
5781 u_int32_t mode_detail;
5782 XFocusChangeEvent *ev = &e->xfocus;
5784 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5785 ev->type == FocusIn ? "entering" : "leaving",
5786 ev->window, ev->mode, ev->detail);
5788 if (last_focus_event == ev->type) {
5789 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5793 last_focus_event = ev->type;
5794 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5796 switch (mode_detail) {
5797 /* synergy client focus operations */
5798 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5799 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5801 /* synergy server focus operations */
5802 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5804 /* Entering applications like rdesktop that mangle the pointer */
5805 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5807 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5808 XSetWindowBorder(display, win->id,
5809 win->ws->r->s->c[ev->type == FocusIn ?
5810 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5813 fprintf(stderr, "ignoring focusevent\n");
5814 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5821 mapnotify(XEvent *e)
5824 XMapEvent *ev = &e->xmap;
5826 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5828 win = manage_window(ev->window);
5830 set_win_state(win, NormalState);
5834 mappingnotify(XEvent *e)
5836 XMappingEvent *ev = &e->xmapping;
5838 XRefreshKeyboardMapping(ev);
5839 if (ev->request == MappingKeyboard)
5844 maprequest(XEvent *e)
5847 struct swm_region *r;
5848 XWindowAttributes wa;
5849 XMapRequestEvent *ev = &e->xmaprequest;
5851 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5852 e->xmaprequest.window);
5854 if (!XGetWindowAttributes(display, ev->window, &wa))
5856 if (wa.override_redirect)
5859 win = manage_window(e->xmaprequest.window);
5861 return; /* can't happen */
5865 /* make new win focused */
5866 r = root_to_region(win->wa.root);
5867 if (win->ws == r->ws)
5872 propertynotify(XEvent *e)
5875 XPropertyEvent *ev = &e->xproperty;
5877 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5880 win = find_window(ev->window);
5884 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5885 update_iconic(win, 0);
5886 XMapRaised(display, win->id);
5893 case XA_WM_NORMAL_HINTS:
5896 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5897 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5898 if (win->sh.flags & PMinSize) {
5899 win->g.w = win->sh.min_width;
5900 win->g.h = win->sh.min_height;
5901 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5903 XMoveResizeWindow(display, win->id,
5904 win->g.x, win->g.y, win->g.w, win->g.h);
5906 if (window_name_enabled)
5915 unmapnotify(XEvent *e)
5919 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5921 /* determine if we need to help unmanage this window */
5922 win = find_window(e->xunmap.window);
5926 if (getstate(e->xunmap.window) == NormalState) {
5927 unmanage_window(win);
5930 /* giant hack for apps that don't destroy transient windows */
5931 /* eat a bunch of events to prevent remanaging the window */
5933 while (XCheckWindowEvent(display, e->xunmap.window,
5934 EnterWindowMask, &cne))
5936 while (XCheckWindowEvent(display, e->xunmap.window,
5937 StructureNotifyMask, &cne))
5939 while (XCheckWindowEvent(display, e->xunmap.window,
5940 SubstructureNotifyMask, &cne))
5942 /* resend unmap because we ated it */
5943 XUnmapWindow(display, e->xunmap.window);
5946 if (focus_mode == SWM_FOCUS_DEFAULT)
5947 drain_enter_notify();
5951 visibilitynotify(XEvent *e)
5954 struct swm_region *r;
5956 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5957 e->xvisibility.window);
5958 if (e->xvisibility.state == VisibilityUnobscured)
5959 for (i = 0; i < ScreenCount(display); i++)
5960 TAILQ_FOREACH(r, &screens[i].rl, entry)
5961 if (e->xvisibility.window == r->bar_window)
5966 clientmessage(XEvent *e)
5968 XClientMessageEvent *ev;
5973 win = find_window(ev->window);
5977 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5978 ev->window, ev->message_type);
5980 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5981 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5984 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5985 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5986 if (win->can_delete)
5987 client_msg(win, adelete);
5989 XKillClient(display, win->id);
5991 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5992 DNPRINTF(SWM_D_EVENT,
5993 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5994 if (win->floating) {
5995 if (ev->data.l[0] & (1<<8)) /* x */
5996 win->g.x = ev->data.l[1];
5997 if (ev->data.l[0] & (1<<9)) /* y */
5998 win->g.y = ev->data.l[2];
5999 if (ev->data.l[0] & (1<<10)) /* width */
6000 win->g.w = ev->data.l[3];
6001 if (ev->data.l[0] & (1<<11)) /* height */
6002 win->g.h = ev->data.l[4];
6005 /* TODO: Change stack sizes */
6007 config_win(win, NULL);
6009 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6010 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
6011 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6013 ewmh_update_win_state(win, ev->data.l[2],
6021 xerror_start(Display *d, XErrorEvent *ee)
6028 xerror(Display *d, XErrorEvent *ee)
6030 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6038 xerrorxlib = XSetErrorHandler(xerror_start);
6040 /* this causes an error if some other window manager is running */
6041 XSelectInput(display, DefaultRootWindow(display),
6042 SubstructureRedirectMask);
6043 XSync(display, False);
6047 XSetErrorHandler(xerror);
6048 XSync(display, False);
6053 new_region(struct swm_screen *s, int x, int y, int w, int h)
6055 struct swm_region *r, *n;
6056 struct workspace *ws = NULL;
6059 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6060 s->idx, w, h, x, y);
6062 /* remove any conflicting regions */
6063 n = TAILQ_FIRST(&s->rl);
6066 n = TAILQ_NEXT(r, entry);
6067 if (X(r) < (x + w) &&
6068 (X(r) + WIDTH(r)) > x &&
6070 (Y(r) + HEIGHT(r)) > y) {
6071 if (r->ws->r != NULL)
6072 r->ws->old_r = r->ws->r;
6074 XDestroyWindow(display, r->bar_window);
6075 TAILQ_REMOVE(&s->rl, r, entry);
6076 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6080 /* search old regions for one to reuse */
6082 /* size + location match */
6083 TAILQ_FOREACH(r, &s->orl, entry)
6084 if (X(r) == x && Y(r) == y &&
6085 HEIGHT(r) == h && WIDTH(r) == w)
6089 TAILQ_FOREACH(r, &s->orl, entry)
6090 if (HEIGHT(r) == h && WIDTH(r) == w)
6094 TAILQ_REMOVE(&s->orl, r, entry);
6095 /* try to use old region's workspace */
6096 if (r->ws->r == NULL)
6099 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6100 errx(1, "calloc: failed to allocate memory for screen");
6102 /* if we don't have a workspace already, find one */
6104 for (i = 0; i < SWM_WS_MAX; i++)
6105 if (s->ws[i].r == NULL) {
6112 errx(1, "no free workspaces\n");
6123 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6129 #ifdef SWM_XRR_HAS_CRTC
6131 XRRScreenResources *sr;
6134 #endif /* SWM_XRR_HAS_CRTC */
6135 struct swm_region *r;
6138 if (i >= ScreenCount(display))
6139 errx(1, "invalid screen");
6141 /* remove any old regions */
6142 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6143 r->ws->old_r = r->ws->r = NULL;
6144 XDestroyWindow(display, r->bar_window);
6145 TAILQ_REMOVE(&screens[i].rl, r, entry);
6146 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6150 /* map virtual screens onto physical screens */
6151 #ifdef SWM_XRR_HAS_CRTC
6152 if (xrandr_support) {
6153 sr = XRRGetScreenResources(display, screens[i].root);
6155 new_region(&screens[i], 0, 0,
6156 DisplayWidth(display, i),
6157 DisplayHeight(display, i));
6161 for (c = 0, ci = NULL; c < ncrtc; c++) {
6162 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6163 if (ci->noutput == 0)
6166 if (ci != NULL && ci->mode == None)
6167 new_region(&screens[i], 0, 0,
6168 DisplayWidth(display, i),
6169 DisplayHeight(display, i));
6171 new_region(&screens[i],
6172 ci->x, ci->y, ci->width, ci->height);
6175 XRRFreeCrtcInfo(ci);
6176 XRRFreeScreenResources(sr);
6178 #endif /* SWM_XRR_HAS_CRTC */
6180 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6181 DisplayHeight(display, i));
6186 screenchange(XEvent *e) {
6187 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6188 struct swm_region *r;
6191 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6193 if (!XRRUpdateConfiguration(e))
6196 /* silly event doesn't include the screen index */
6197 for (i = 0; i < ScreenCount(display); i++)
6198 if (screens[i].root == xe->root)
6200 if (i >= ScreenCount(display))
6201 errx(1, "screenchange: screen not found\n");
6203 /* brute force for now, just re-enumerate the regions */
6206 /* add bars to all regions */
6207 for (i = 0; i < ScreenCount(display); i++)
6208 TAILQ_FOREACH(r, &screens[i].rl, entry)
6211 if (focus_mode == SWM_FOCUS_DEFAULT)
6212 drain_enter_notify();
6218 Window d1, d2, *wins = NULL;
6219 XWindowAttributes wa;
6224 for (i = 0; i < ScreenCount(display); i++) {
6225 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6228 /* attach windows to a region */
6229 /* normal windows */
6230 for (j = 0; j < no; j++) {
6231 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6232 wa.override_redirect ||
6233 XGetTransientForHint(display, wins[j], &d1))
6236 state = getstate(wins[j]);
6237 manage = state == IconicState;
6238 if (wa.map_state == IsViewable || manage)
6239 manage_window(wins[j]);
6241 /* transient windows */
6242 for (j = 0; j < no; j++) {
6243 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6244 wa.override_redirect)
6247 state = getstate(wins[j]);
6248 manage = state == IconicState;
6249 if (XGetTransientForHint(display, wins[j], &d1) &&
6251 manage_window(wins[j]);
6264 int errorbase, major, minor;
6265 struct workspace *ws;
6267 if ((screens = calloc(ScreenCount(display),
6268 sizeof(struct swm_screen))) == NULL)
6269 errx(1, "calloc: screens");
6271 /* initial Xrandr setup */
6272 xrandr_support = XRRQueryExtension(display,
6273 &xrandr_eventbase, &errorbase);
6275 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6278 /* map physical screens */
6279 for (i = 0; i < ScreenCount(display); i++) {
6280 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6282 TAILQ_INIT(&screens[i].rl);
6283 TAILQ_INIT(&screens[i].orl);
6284 screens[i].root = RootWindow(display, i);
6286 /* set default colors */
6287 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6288 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6289 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6290 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6291 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6293 /* set default cursor */
6294 XDefineCursor(display, screens[i].root,
6295 XCreateFontCursor(display, XC_left_ptr));
6297 /* init all workspaces */
6298 /* XXX these should be dynamically allocated too */
6299 for (j = 0; j < SWM_WS_MAX; j++) {
6300 ws = &screens[i].ws[j];
6305 TAILQ_INIT(&ws->winlist);
6306 TAILQ_INIT(&ws->unmanagedlist);
6308 for (k = 0; layouts[k].l_stack != NULL; k++)
6309 if (layouts[k].l_config != NULL)
6310 layouts[k].l_config(ws,
6311 SWM_ARG_ID_STACKINIT);
6312 ws->cur_layout = &layouts[0];
6313 ws->cur_layout->l_string(ws);
6319 XRRSelectInput(display, screens[i].root,
6320 RRScreenChangeNotifyMask);
6327 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6329 err(1, "setup_globals: strdup");
6330 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6332 err(1, "setup_globals: strdup");
6333 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6335 err(1, "setup_globals: strdup");
6336 if ((spawn_term[0] = strdup("xterm")) == NULL)
6337 err(1, "setup_globals: strdup");
6338 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6346 Atom netwmcheck, netwmname, utf8_string;
6349 /* work around sun jdk bugs, code from wmname */
6350 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6351 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6352 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6353 for (i = 0; i < ScreenCount(display); i++) {
6354 root = screens[i].root;
6355 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6356 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6357 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6359 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6360 PropModeReplace, (unsigned char *)&win,1);
6361 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6362 PropModeReplace, (unsigned char *)&win,1);
6363 XChangeProperty(display, win, netwmname, utf8_string, 8,
6364 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6369 main(int argc, char *argv[])
6371 struct swm_region *r, *rr;
6372 struct ws_win *winfocus = NULL;
6375 char conf[PATH_MAX], *cfile = NULL;
6380 struct sigaction sact;
6383 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6384 SCROTWM_VERSION, buildstr);
6385 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6387 warnx("no locale support");
6389 if (!(display = XOpenDisplay(0)))
6390 errx(1, "can not open display");
6393 errx(1, "other wm running");
6395 /* handle some signals */
6396 bzero(&sact, sizeof(sact));
6397 sigemptyset(&sact.sa_mask);
6399 sact.sa_handler = sighdlr;
6400 sigaction(SIGINT, &sact, NULL);
6401 sigaction(SIGQUIT, &sact, NULL);
6402 sigaction(SIGTERM, &sact, NULL);
6403 sigaction(SIGHUP, &sact, NULL);
6405 sact.sa_handler = sighdlr;
6406 sact.sa_flags = SA_NOCLDSTOP;
6407 sigaction(SIGCHLD, &sact, NULL);
6409 astate = XInternAtom(display, "WM_STATE", False);
6410 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6411 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6412 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6413 a_wmname = XInternAtom(display, "WM_NAME", False);
6414 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6415 a_string = XInternAtom(display, "STRING", False);
6416 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6418 /* look for local and global conf file */
6419 pwd = getpwuid(getuid());
6421 errx(1, "invalid user %d", getuid());
6430 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6431 if (stat(conf, &sb) != -1) {
6432 if (S_ISREG(sb.st_mode))
6435 /* try global conf file */
6436 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6437 if (!stat(conf, &sb))
6438 if (S_ISREG(sb.st_mode))
6442 conf_load(cfile, SWM_CONF_DEFAULT);
6445 /* set some values to work around bad programs */
6447 /* grab existing windows (before we build the bars) */
6450 if (getenv("SWM_STARTED") == NULL)
6451 setenv("SWM_STARTED", "YES", 1);
6453 /* setup all bars */
6454 for (i = 0; i < ScreenCount(display); i++)
6455 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6456 if (winfocus == NULL)
6457 winfocus = TAILQ_FIRST(&r->ws->winlist);
6465 if (focus_mode == SWM_FOCUS_DEFAULT)
6466 drain_enter_notify();
6468 xfd = ConnectionNumber(display);
6470 while (XPending(display)) {
6471 XNextEvent(display, &e);
6474 if (e.type < LASTEvent) {
6476 if (handler[e.type])
6477 handler[e.type](&e);
6479 DNPRINTF(SWM_D_EVENT,
6480 "win: %lu unknown event: %d\n",
6481 e.xany.window, e.type);
6483 switch (e.type - xrandr_eventbase) {
6484 case RRScreenChangeNotify:
6488 DNPRINTF(SWM_D_EVENT,
6489 "win: %lu unknown xrandr event: "
6490 "%d\n", e.xany.window, e.type);
6496 /* if we are being restarted go focus on first window */
6498 rr = winfocus->ws->r;
6500 /* not a visible window */
6504 /* move pointer to first screen if multi screen */
6505 if (ScreenCount(display) > 1 || outputs > 1)
6506 XWarpPointer(display, None, rr->s[0].root,
6507 0, 0, 0, 0, rr->g.x,
6508 rr->g.y + (bar_enabled ? bar_height : 0));
6510 a.id = SWM_ARG_ID_FOCUSCUR;
6520 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6522 DNPRINTF(SWM_D_MISC, "select failed");
6523 if (restart_wm == 1)
6524 restart(NULL, NULL);
6525 if (search_resp == 1)
6537 XFreeGC(display, bar_gc);
6538 XCloseDisplay(display);