2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-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>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
85 #include <X11/extensions/XTest.h>
93 #ifdef SCROTWM_BUILDSTR
94 static const char *buildstr = SCROTWM_BUILDSTR;
96 static const char *buildstr = SCROTWM_VERSION;
100 # error XRandR versions less than 1.0 are not supported
105 #define SWM_XRR_HAS_CRTC
109 /*#define SWM_DEBUG*/
111 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
112 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
113 #define SWM_D_MISC 0x0001
114 #define SWM_D_EVENT 0x0002
115 #define SWM_D_WS 0x0004
116 #define SWM_D_FOCUS 0x0008
117 #define SWM_D_MOVE 0x0010
118 #define SWM_D_STACK 0x0020
119 #define SWM_D_MOUSE 0x0040
120 #define SWM_D_PROP 0x0080
121 #define SWM_D_CLASS 0x0100
122 #define SWM_D_KEY 0x0200
123 #define SWM_D_QUIRK 0x0400
124 #define SWM_D_SPAWN 0x0800
125 #define SWM_D_EVENTQ 0x1000
126 #define SWM_D_CONF 0x2000
128 u_int32_t swm_debug = 0
145 #define DPRINTF(x...)
146 #define DNPRINTF(n,x...)
149 #define LENGTH(x) (sizeof x / sizeof x[0])
150 #define MODKEY Mod1Mask
151 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
152 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
153 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
154 #define SWM_PROPLEN (16)
155 #define SWM_FUNCNAME_LEN (32)
156 #define SWM_KEYS_LEN (255)
157 #define SWM_QUIRK_LEN (64)
158 #define X(r) (r)->g.x
159 #define Y(r) (r)->g.y
160 #define WIDTH(r) (r)->g.w
161 #define HEIGHT(r) (r)->g.h
162 #define SWM_MAX_FONT_STEPS (3)
163 #define WINID(w) (w ? w->id : 0)
164 #define YESNO(x) (x ? "yes" : "no")
166 #define SWM_FOCUS_DEFAULT (0)
167 #define SWM_FOCUS_SYNERGY (1)
168 #define SWM_FOCUS_FOLLOW (2)
170 #define SWM_CONF_DEFAULT (0)
171 #define SWM_CONF_KEYMAPPING (1)
174 #define SWM_LIB "/usr/local/lib/libswmhack.so"
187 volatile sig_atomic_t running = 1;
188 volatile sig_atomic_t restart_wm = 0;
190 int last_focus_event = FocusOut;
191 int (*xerrorxlib)(Display *, XErrorEvent *);
195 int xrandr_eventbase;
196 unsigned int numlockmask = 0;
200 int cycle_visible = 0;
202 int font_adjusted = 0;
203 unsigned int mod_key = MODKEY;
206 struct swm_region *search_r;
207 int select_list_pipe[2];
208 int select_resp_pipe[2];
210 volatile sig_atomic_t search_resp;
211 int search_resp_action;
213 struct search_window {
214 TAILQ_ENTRY(search_window) entry;
219 TAILQ_HEAD(search_winlist, search_window);
221 struct search_winlist search_wl;
226 SWM_SEARCH_UNICONIFY,
227 SWM_SEARCH_NAME_WORKSPACE,
228 SWM_SEARCH_SEARCH_WORKSPACE,
229 SWM_SEARCH_SEARCH_WINDOW
233 double dialog_ratio = .6;
235 #define SWM_BAR_MAX (256)
236 #define SWM_BAR_JUSTIFY_LEFT (0)
237 #define SWM_BAR_JUSTIFY_CENTER (1)
238 #define SWM_BAR_JUSTIFY_RIGHT (2)
239 #define SWM_BAR_OFFSET (4)
240 char *bar_argv[] = { NULL, NULL };
242 char bar_ext[SWM_BAR_MAX];
243 char bar_vertext[SWM_BAR_MAX];
245 sig_atomic_t bar_alarm = 0;
248 int bar_border_width = 1;
249 int bar_at_bottom = 0;
251 int bar_extra_running = 0;
254 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
255 int stack_enabled = 1;
256 int clock_enabled = 1;
257 int urgent_enabled = 0;
258 char *clock_format = NULL;
259 int title_name_enabled = 0;
260 int title_class_enabled = 0;
261 int window_name_enabled = 0;
262 int focus_mode = SWM_FOCUS_DEFAULT;
263 int disable_border = 0;
264 int border_width = 1;
265 int verbose_layout = 0;
271 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
272 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
275 #define SWM_MENU_FN (2)
276 #define SWM_MENU_NB (4)
277 #define SWM_MENU_NF (6)
278 #define SWM_MENU_SB (8)
279 #define SWM_MENU_SF (10)
281 /* layout manager data */
282 struct swm_geometry {
292 /* virtual "screens" */
294 TAILQ_ENTRY(swm_region) entry;
295 struct swm_geometry g;
296 struct workspace *ws; /* current workspace on this region */
297 struct workspace *ws_prior; /* prior workspace on this region */
298 struct swm_screen *s; /* screen idx */
301 TAILQ_HEAD(swm_region_list, swm_region);
304 TAILQ_ENTRY(ws_win) entry;
307 struct ws_win *child_trans; /* transient child window */
308 struct swm_geometry g; /* current geometry */
309 struct swm_geometry g_float; /* geometry when floating */
310 struct swm_geometry rg_float; /* region geom when floating */
311 int g_floatvalid; /* g_float geometry validity */
312 int floatmaxed; /* whether maxed by max_stack */
316 unsigned int ewmh_flags;
317 int font_size_boundary[SWM_MAX_FONT_STEPS];
323 unsigned long quirks;
324 struct workspace *ws; /* always valid */
325 struct swm_screen *s; /* always valid, never changes */
326 XWindowAttributes wa;
331 TAILQ_HEAD(ws_win_list, ws_win);
335 TAILQ_ENTRY(pid_e) entry;
339 TAILQ_HEAD(pid_list, pid_e);
340 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
342 /* layout handlers */
344 void vertical_config(struct workspace *, int);
345 void vertical_stack(struct workspace *, struct swm_geometry *);
346 void horizontal_config(struct workspace *, int);
347 void horizontal_stack(struct workspace *, struct swm_geometry *);
348 void max_stack(struct workspace *, struct swm_geometry *);
349 void plain_stacker(struct workspace *);
350 void fancy_stacker(struct workspace *);
352 struct ws_win *find_window(Window);
354 void grabbuttons(struct ws_win *, int);
355 void new_region(struct swm_screen *, int, int, int, int);
356 void unmanage_window(struct ws_win *);
357 long getstate(Window);
359 int conf_load(char *, int);
362 void (*l_stack)(struct workspace *, struct swm_geometry *);
363 void (*l_config)(struct workspace *, int);
365 #define SWM_L_FOCUSPREV (1<<0)
366 #define SWM_L_MAPONFOCUS (1<<1)
367 void (*l_string)(struct workspace *);
369 /* stack, configure */
370 { vertical_stack, vertical_config, 0, plain_stacker },
371 { horizontal_stack, horizontal_config, 0, plain_stacker },
373 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
374 { NULL, NULL, 0, NULL },
377 /* position of max_stack mode in the layouts array, index into layouts! */
378 #define SWM_V_STACK (0)
379 #define SWM_H_STACK (1)
380 #define SWM_MAX_STACK (2)
382 #define SWM_H_SLICE (32)
383 #define SWM_V_SLICE (32)
385 /* define work spaces */
387 int idx; /* workspace index */
388 char *name; /* workspace name */
389 int always_raise; /* raise windows on focus */
390 struct layout *cur_layout; /* current layout handlers */
391 struct ws_win *focus; /* may be NULL */
392 struct ws_win *focus_prev; /* may be NULL */
393 struct swm_region *r; /* may be NULL */
394 struct swm_region *old_r; /* may be NULL */
395 struct ws_win_list winlist; /* list of windows in ws */
396 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
397 char stacker[10]; /* display stacker and layout */
401 int horizontal_msize;
403 int horizontal_stacks;
410 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
411 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
413 /* physical screen mapping */
414 #define SWM_WS_MAX (10)
416 int idx; /* screen index */
417 struct swm_region_list rl; /* list of regions on this screen */
418 struct swm_region_list orl; /* list of old regions */
420 struct workspace ws[SWM_WS_MAX];
426 } c[SWM_S_COLOR_MAX];
428 struct swm_screen *screens;
431 /* args to functions */
434 #define SWM_ARG_ID_FOCUSNEXT (0)
435 #define SWM_ARG_ID_FOCUSPREV (1)
436 #define SWM_ARG_ID_FOCUSMAIN (2)
437 #define SWM_ARG_ID_FOCUSCUR (4)
438 #define SWM_ARG_ID_SWAPNEXT (10)
439 #define SWM_ARG_ID_SWAPPREV (11)
440 #define SWM_ARG_ID_SWAPMAIN (12)
441 #define SWM_ARG_ID_MOVELAST (13)
442 #define SWM_ARG_ID_MASTERSHRINK (20)
443 #define SWM_ARG_ID_MASTERGROW (21)
444 #define SWM_ARG_ID_MASTERADD (22)
445 #define SWM_ARG_ID_MASTERDEL (23)
446 #define SWM_ARG_ID_STACKRESET (30)
447 #define SWM_ARG_ID_STACKINIT (31)
448 #define SWM_ARG_ID_CYCLEWS_UP (40)
449 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
450 #define SWM_ARG_ID_CYCLESC_UP (42)
451 #define SWM_ARG_ID_CYCLESC_DOWN (43)
452 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
453 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
454 #define SWM_ARG_ID_STACKINC (50)
455 #define SWM_ARG_ID_STACKDEC (51)
456 #define SWM_ARG_ID_SS_ALL (60)
457 #define SWM_ARG_ID_SS_WINDOW (61)
458 #define SWM_ARG_ID_DONTCENTER (70)
459 #define SWM_ARG_ID_CENTER (71)
460 #define SWM_ARG_ID_KILLWINDOW (80)
461 #define SWM_ARG_ID_DELETEWINDOW (81)
462 #define SWM_ARG_ID_WIDTHGROW (90)
463 #define SWM_ARG_ID_WIDTHSHRINK (91)
464 #define SWM_ARG_ID_HEIGHTGROW (92)
465 #define SWM_ARG_ID_HEIGHTSHRINK (93)
466 #define SWM_ARG_ID_MOVEUP (100)
467 #define SWM_ARG_ID_MOVEDOWN (101)
468 #define SWM_ARG_ID_MOVELEFT (102)
469 #define SWM_ARG_ID_MOVERIGHT (103)
473 void focus(struct swm_region *, union arg *);
474 void focus_magic(struct ws_win *);
481 #define SWM_Q_FLOAT (1<<0) /* float this window */
482 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
483 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
484 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
485 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
486 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
488 int quirks_size = 0, quirks_length = 0;
489 struct quirk *quirks = NULL;
492 * Supported EWMH hints should be added to
493 * both the enum and the ewmh array
495 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
496 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
497 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
498 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
499 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
500 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
501 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
502 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
503 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
504 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
510 } ewmh[SWM_EWMH_HINT_MAX] = {
511 /* must be in same order as in the enum */
512 {"_NET_ACTIVE_WINDOW", None},
513 {"_NET_MOVERESIZE_WINDOW", None},
514 {"_NET_CLOSE_WINDOW", None},
515 {"_NET_WM_WINDOW_TYPE", None},
516 {"_NET_WM_WINDOW_TYPE_DOCK", None},
517 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
518 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
519 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
520 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
521 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
522 {"_NET_WM_STATE", None},
523 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
524 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
525 {"_NET_WM_STATE_SKIP_TASKBAR", None},
526 {"_NET_WM_STATE_SKIP_PAGER", None},
527 {"_NET_WM_STATE_HIDDEN", None},
528 {"_NET_WM_STATE_ABOVE", None},
529 {"_SWM_WM_STATE_MANUAL", None},
530 {"_NET_WM_STATE_FULLSCREEN", None},
531 {"_NET_WM_ALLOWED_ACTIONS", None},
532 {"_NET_WM_ACTION_MOVE", None},
533 {"_NET_WM_ACTION_RESIZE", None},
534 {"_NET_WM_ACTION_FULLSCREEN", None},
535 {"_NET_WM_ACTION_CLOSE", None},
538 void store_float_geom(struct ws_win *, struct swm_region *);
539 int floating_toggle_win(struct ws_win *);
540 void spawn_select(struct swm_region *, union arg *, char *, int *);
543 get_property(Window id, Atom atom, long count, Atom type,
544 unsigned long *n, unsigned char **data)
547 unsigned long tmp, extra;
548 unsigned long *nitems;
551 nitems = n != NULL ? n : &tmp;
552 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
553 &real, &format, nitems, &extra, data);
555 if (status != Success)
564 update_iconic(struct ws_win *win, int newv)
571 iprop = XInternAtom(display, "_SWM_ICONIC", False);
575 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
576 PropModeReplace, (unsigned char *)&v, 1);
578 XDeleteProperty(display, win->id, iprop);
582 get_iconic(struct ws_win *win)
587 unsigned long nitems, extra;
588 unsigned char *prop = NULL;
590 iprop = XInternAtom(display, "_SWM_ICONIC", False);
593 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
594 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
595 if (status != Success)
597 if (rettype != XA_INTEGER || retfmt != 32)
601 v = *((int32_t *)prop);
615 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
617 for (i = 0; i < LENGTH(ewmh); i++)
618 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
620 for (i = 0; i < ScreenCount(display); i++) {
621 /* Support check window will be created by workaround(). */
623 /* Report supported atoms */
624 XDeleteProperty(display, screens[i].root, sup_list);
625 for (j = 0; j < LENGTH(ewmh); j++)
626 XChangeProperty(display, screens[i].root,
627 sup_list, XA_ATOM, 32,
628 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
636 unsigned char *data = NULL;
638 Atom sup_check, sup_list;
641 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
642 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
644 for (i = 0; i < ScreenCount(display); i++) {
645 /* Get the support check window and destroy it */
646 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
651 XDestroyWindow(display, id);
652 XDeleteProperty(display, screens[i].root, sup_check);
653 XDeleteProperty(display, screens[i].root, sup_list);
661 ewmh_autoquirk(struct ws_win *win)
664 unsigned long *data = NULL, n;
667 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
668 XA_ATOM, &n, (void *)&data);
675 for (i = 0; i < n; i++) {
677 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
679 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
680 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
681 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
683 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
686 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
687 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
689 win->quirks = SWM_Q_FLOAT;
697 #define SWM_EWMH_ACTION_COUNT_MAX (6)
698 #define EWMH_F_FULLSCREEN (1<<0)
699 #define EWMH_F_ABOVE (1<<1)
700 #define EWMH_F_HIDDEN (1<<2)
701 #define EWMH_F_SKIP_PAGER (1<<3)
702 #define EWMH_F_SKIP_TASKBAR (1<<4)
703 #define SWM_F_MANUAL (1<<5)
706 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
708 struct swm_geometry rg;
716 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
717 "fullscreen %s\n", win->id, YESNO(fs));
722 store_float_geom(win, win->ws->r);
726 if (win->g_floatvalid) {
727 /* refloat at last floating relative position */
728 X(win) = win->g_float.x - win->rg_float.x + rg.x;
729 Y(win) = win->g_float.y - win->rg_float.y + rg.y;
730 WIDTH(win) = win->g_float.w;
731 HEIGHT(win) = win->g_float.h;
739 ewmh_update_actions(struct ws_win *win)
741 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
747 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
750 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
751 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
754 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
755 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
758 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
759 #define _NET_WM_STATE_ADD 1 /* add/set property */
760 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
763 ewmh_update_win_state(struct ws_win *win, long state, long action)
765 unsigned int mask = 0;
766 unsigned int changed = 0;
767 unsigned int orig_flags;
772 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
773 mask = EWMH_F_FULLSCREEN;
774 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
776 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
778 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
779 mask = EWMH_F_SKIP_PAGER;
780 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
781 mask = EWMH_F_SKIP_TASKBAR;
784 orig_flags = win->ewmh_flags;
787 case _NET_WM_STATE_REMOVE:
788 win->ewmh_flags &= ~mask;
790 case _NET_WM_STATE_ADD:
791 win->ewmh_flags |= mask;
793 case _NET_WM_STATE_TOGGLE:
794 win->ewmh_flags ^= mask;
798 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
800 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
802 if (!floating_toggle_win(win))
803 win->ewmh_flags = orig_flags; /* revert */
804 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
806 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
807 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
809 if (!ewmh_set_win_fullscreen(win,
810 win->ewmh_flags & EWMH_F_FULLSCREEN))
811 win->ewmh_flags = orig_flags; /* revert */
813 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
815 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
816 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
817 XA_ATOM, 32, PropModeAppend,
818 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
819 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
820 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
821 XA_ATOM, 32, PropModeAppend,
822 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
823 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
824 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
825 XA_ATOM, 32, PropModeAppend,
826 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
827 if (win->ewmh_flags & EWMH_F_ABOVE)
828 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
829 XA_ATOM, 32, PropModeAppend,
830 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
831 if (win->ewmh_flags & SWM_F_MANUAL)
832 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
833 XA_ATOM, 32, PropModeAppend,
834 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
838 ewmh_get_win_state(struct ws_win *win)
849 win->ewmh_flags |= EWMH_F_ABOVE;
851 win->ewmh_flags |= SWM_F_MANUAL;
853 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
854 (~0L), XA_ATOM, &n, (void *)&states);
859 for (i = 0; i < n; i++)
860 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
868 geteventname(XEvent *e)
880 name = "ButtonPress";
883 name = "ButtonRelease";
886 name = "MotionNotify";
889 name = "EnterNotify";
892 name = "LeaveNotify";
901 name = "KeymapNotify";
907 name = "GraphicsExpose";
912 case VisibilityNotify:
913 name = "VisibilityNotify";
916 name = "CreateNotify";
919 name = "DestroyNotify";
922 name = "UnmapNotify";
931 name = "ReparentNotify";
933 case ConfigureNotify:
934 name = "ConfigureNotify";
936 case ConfigureRequest:
937 name = "ConfigureRequest";
940 name = "GravityNotify";
943 name = "ResizeRequest";
945 case CirculateNotify:
946 name = "CirculateNotify";
948 case CirculateRequest:
949 name = "CirculateRequest";
952 name = "PropertyNotify";
955 name = "SelectionClear";
957 case SelectionRequest:
958 name = "SelectionRequest";
960 case SelectionNotify:
961 name = "SelectionNotify";
964 name = "ColormapNotify";
967 name = "ClientMessage";
970 name = "MappingNotify";
980 xrandr_geteventname(XEvent *e)
984 switch(e->type - xrandr_eventbase) {
985 case RRScreenChangeNotify:
986 name = "RRScreenChangeNotify";
996 dumpwins(struct swm_region *r, union arg *args)
1000 XWindowAttributes wa;
1002 if (r->ws == NULL) {
1003 fprintf(stderr, "dumpwins: invalid workspace\n");
1007 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
1009 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1010 state = getstate(win->id);
1011 if (!XGetWindowAttributes(display, win->id, &wa))
1012 fprintf(stderr, "window: 0x%lx, failed "
1013 "XGetWindowAttributes\n", win->id);
1014 fprintf(stderr, "window: 0x%lx, map_state: %d, state: %d, "
1015 "transient: 0x%lx\n",
1016 win->id, wa.map_state, state, win->transient);
1019 fprintf(stderr, "===== unmanaged window list =====\n");
1020 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1021 state = getstate(win->id);
1022 if (!XGetWindowAttributes(display, win->id, &wa))
1023 fprintf(stderr, "window: 0x%lx, failed "
1024 "XGetWindowAttributes\n", win->id);
1025 fprintf(stderr, "window: 0x%lx, map_state: %d, state: %d, "
1026 "transient: 0x%lx\n",
1027 win->id, wa.map_state, state, win->transient);
1030 fprintf(stderr, "=================================\n");
1034 dumpwins(struct swm_region *r, union arg *args)
1037 #endif /* SWM_DEBUG */
1039 void expose(XEvent *);
1040 void keypress(XEvent *);
1041 void buttonpress(XEvent *);
1042 void configurerequest(XEvent *);
1043 void configurenotify(XEvent *);
1044 void destroynotify(XEvent *);
1045 void enternotify(XEvent *);
1046 void focusevent(XEvent *);
1047 void mapnotify(XEvent *);
1048 void mappingnotify(XEvent *);
1049 void maprequest(XEvent *);
1050 void propertynotify(XEvent *);
1051 void unmapnotify(XEvent *);
1052 void visibilitynotify(XEvent *);
1053 void clientmessage(XEvent *);
1055 void (*handler[LASTEvent])(XEvent *) = {
1057 [KeyPress] = keypress,
1058 [ButtonPress] = buttonpress,
1059 [ConfigureRequest] = configurerequest,
1060 [ConfigureNotify] = configurenotify,
1061 [DestroyNotify] = destroynotify,
1062 [EnterNotify] = enternotify,
1063 [FocusIn] = focusevent,
1064 [FocusOut] = focusevent,
1065 [MapNotify] = mapnotify,
1066 [MappingNotify] = mappingnotify,
1067 [MapRequest] = maprequest,
1068 [PropertyNotify] = propertynotify,
1069 [UnmapNotify] = unmapnotify,
1070 [VisibilityNotify] = visibilitynotify,
1071 [ClientMessage] = clientmessage,
1077 int saved_errno, status;
1080 saved_errno = errno;
1084 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1089 if (errno != ECHILD)
1090 warn("sighdlr: waitpid");
1091 #endif /* SWM_DEBUG */
1094 if (pid == searchpid)
1098 if (WIFEXITED(status)) {
1099 if (WEXITSTATUS(status) != 0)
1100 warnx("sighdlr: child exit status: %d",
1101 WEXITSTATUS(status));
1103 warnx("sighdlr: child is terminated "
1105 #endif /* SWM_DEBUG */
1119 errno = saved_errno;
1125 struct pid_e *p = NULL;
1127 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1132 TAILQ_FOREACH(p, &pidlist, entry) {
1141 name_to_color(char *colorname)
1145 XColor screen_def, exact_def;
1146 unsigned long result = 0;
1147 char cname[32] = "#";
1149 cmap = DefaultColormap(display, screens[0].idx);
1150 status = XAllocNamedColor(display, cmap, colorname,
1151 &screen_def, &exact_def);
1153 strlcat(cname, colorname + 2, sizeof cname - 1);
1154 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1158 result = screen_def.pixel;
1160 fprintf(stderr, "color '%s' not found.\n", colorname);
1166 setscreencolor(char *val, int i, int c)
1168 if (i > 0 && i <= ScreenCount(display)) {
1169 screens[i - 1].c[c].color = name_to_color(val);
1170 free(screens[i - 1].c[c].name);
1171 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1173 } else if (i == -1) {
1174 for (i = 0; i < ScreenCount(display); i++) {
1175 screens[i].c[c].color = name_to_color(val);
1176 free(screens[i].c[c].name);
1177 if ((screens[i].c[c].name = strdup(val)) == NULL)
1181 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1182 i, ScreenCount(display));
1186 fancy_stacker(struct workspace *ws)
1188 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1189 if (ws->cur_layout->l_stack == vertical_stack)
1190 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1191 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1192 if (ws->cur_layout->l_stack == horizontal_stack)
1193 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1194 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1198 plain_stacker(struct workspace *ws)
1200 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1201 if (ws->cur_layout->l_stack == vertical_stack)
1202 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1203 if (ws->cur_layout->l_stack == horizontal_stack)
1204 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1208 custom_region(char *val)
1210 unsigned int sidx, x, y, w, h;
1212 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1213 errx(1, "invalid custom region, "
1214 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1215 if (sidx < 1 || sidx > ScreenCount(display))
1216 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1217 sidx, ScreenCount(display));
1221 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1223 if (x > DisplayWidth(display, sidx) ||
1224 y > DisplayHeight(display, sidx) ||
1225 w + x > DisplayWidth(display, sidx) ||
1226 h + y > DisplayHeight(display, sidx)) {
1227 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1228 "- not within screen boundaries "
1229 "(%ux%u)\n", w, h, x, y,
1230 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1234 new_region(&screens[sidx], x, y, w, h);
1238 socket_setnonblock(int fd)
1242 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1243 err(1, "fcntl F_GETFL");
1244 flags |= O_NONBLOCK;
1245 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1246 err(1, "fcntl F_SETFL");
1250 bar_print(struct swm_region *r, char *s)
1252 int textwidth, x = 0;
1255 XClearWindow(display, r->bar_window);
1256 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1259 textwidth = XTextWidth(bar_fs, s, len);
1261 switch (bar_justify) {
1262 case SWM_BAR_JUSTIFY_LEFT:
1265 case SWM_BAR_JUSTIFY_CENTER:
1266 x = (WIDTH(r) - textwidth) / 2;
1268 case SWM_BAR_JUSTIFY_RIGHT:
1269 x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
1273 if (x < SWM_BAR_OFFSET)
1276 XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
1280 bar_extra_stop(void)
1284 bzero(bar_pipe, sizeof bar_pipe);
1287 kill(bar_pid, SIGTERM);
1290 strlcpy(bar_ext, "", sizeof bar_ext);
1295 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1297 int do_class, do_name;
1299 XClassHint *xch = NULL;
1301 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1302 cur_focus != NULL) {
1303 if ((xch = XAllocClassHint()) == NULL)
1305 status = XGetClassHint(display, cur_focus->id, xch);
1306 if (status == BadWindow || status == BadAlloc)
1308 do_class = (title_class_enabled && xch->res_class != NULL);
1309 do_name = (title_name_enabled && xch->res_name != NULL);
1311 strlcat(s, xch->res_class, sz);
1312 if (do_class && do_name)
1313 strlcat(s, ":", sz);
1315 strlcat(s, xch->res_name, sz);
1316 strlcat(s, " ", sz);
1324 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1328 if (window_name_enabled && cur_focus != NULL) {
1329 XFetchName(display, cur_focus->id, &title);
1331 if (cur_focus->floating)
1332 strlcat(s, "(f) ", sz);
1333 strlcat(s, title, sz);
1334 strlcat(s, " ", sz);
1340 int urgent[SWM_WS_MAX];
1342 bar_urgent(char *s, ssize_t sz)
1344 XWMHints *wmh = NULL;
1349 if (urgent_enabled == 0)
1352 for (i = 0; i < SWM_WS_MAX; i++)
1355 for (i = 0; i < ScreenCount(display); i++)
1356 for (j = 0; j < SWM_WS_MAX; j++)
1357 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1358 wmh = XGetWMHints(display, win->id);
1362 if (wmh->flags & XUrgencyHint)
1367 strlcat(s, "* ", sz);
1368 for (i = 0; i < SWM_WS_MAX; i++) {
1370 snprintf(b, sizeof b, "%d ", i + 1);
1372 snprintf(b, sizeof b, "- ");
1375 strlcat(s, "* ", sz);
1383 struct swm_region *r;
1386 char ws[SWM_BAR_MAX];
1387 char s[SWM_BAR_MAX];
1388 char cn[SWM_BAR_MAX];
1389 char loc[SWM_BAR_MAX];
1390 char *b, *stack = "";
1392 if (bar_enabled == 0)
1394 if (bar_extra && bar_extra_running) {
1395 /* ignore short reads; it'll correct itself */
1396 while ((b = fgetln(stdin, &len)) != NULL)
1397 if (b && b[len - 1] == '\n') {
1399 strlcpy(bar_ext, b, sizeof bar_ext);
1401 if (b == NULL && errno != EAGAIN) {
1402 fprintf(stderr, "bar_update: bar_extra failed: "
1403 "errno: %d %s\n", errno, strerror(errno));
1407 strlcpy(bar_ext, "", sizeof bar_ext);
1409 if (clock_enabled == 0)
1410 strlcpy(s, "", sizeof s);
1413 localtime_r(&tmt, &tm);
1414 strftime(s, sizeof s, clock_format, &tm);
1415 strlcat(s, " ", sizeof s);
1418 for (i = 0; i < ScreenCount(display); i++) {
1420 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1421 strlcpy(cn, "", sizeof cn);
1422 strlcpy(ws, "", sizeof ws);
1424 bar_urgent(cn, sizeof cn);
1425 bar_class_name(cn, sizeof cn, r->ws->focus);
1426 bar_window_name(cn, sizeof cn, r->ws->focus);
1428 snprintf(ws, sizeof ws, "<%s>",
1432 stack = r->ws->stacker;
1434 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s "
1435 "%s", x++, r->ws->idx + 1, stack, ws, s, cn,
1436 bar_ext, bar_vertext);
1450 bar_toggle(struct swm_region *r, union arg *args)
1452 struct swm_region *tmpr;
1453 int i, sc = ScreenCount(display);
1455 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1458 for (i = 0; i < sc; i++)
1459 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1460 XUnmapWindow(display, tmpr->bar_window);
1462 for (i = 0; i < sc; i++)
1463 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1464 XMapRaised(display, tmpr->bar_window);
1466 bar_enabled = !bar_enabled;
1469 /* must be after stack */
1476 XSetWindowAttributes wa;
1477 struct swm_region *r;
1480 /* do this here because the conf file is in memory */
1481 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1482 /* launch external status app */
1483 bar_extra_running = 1;
1484 if (pipe(bar_pipe) == -1)
1485 err(1, "pipe error");
1486 socket_setnonblock(bar_pipe[0]);
1487 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1488 if (dup2(bar_pipe[0], 0) == -1)
1490 if (dup2(bar_pipe[1], 1) == -1)
1492 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1493 err(1, "could not disable SIGPIPE");
1494 switch (bar_pid = fork()) {
1496 err(1, "cannot fork");
1500 execvp(bar_argv[0], bar_argv);
1501 err(1, "%s external app failed", bar_argv[0]);
1503 default: /* parent */
1509 bzero(&wa, sizeof wa);
1510 for (i = 0; i < ScreenCount(display); i++)
1511 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1513 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1514 wa.background_pixel =
1515 screens[i].c[SWM_S_COLOR_BAR].color;
1516 XChangeWindowAttributes(display, r->bar_window,
1517 CWBackPixel | CWBorderPixel, &wa);
1523 bar_setup(struct swm_region *r)
1528 XFreeFont(display, bar_fs);
1532 for (i = 0; bar_fonts[i] != NULL; i++) {
1533 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1539 if (bar_fonts[i] == NULL)
1540 errx(1, "couldn't load font");
1542 errx(1, "couldn't create font structure");
1544 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1545 2 * bar_border_width;
1547 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1549 r->bar_window = XCreateSimpleWindow(display,
1550 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1551 bar_height - 2 * bar_border_width,
1552 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1553 r->s->c[SWM_S_COLOR_BAR].color);
1554 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1555 XSetFont(display, bar_gc, bar_fs->fid);
1556 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1558 XMapRaised(display, r->bar_window);
1559 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1561 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1562 err(1, "could not install bar_signal");
1567 drain_enter_notify(void)
1572 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1575 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1579 set_win_state(struct ws_win *win, long state)
1581 long data[] = {state, None};
1583 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1588 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1589 (unsigned char *)data, 2);
1596 unsigned char *p = NULL;
1599 if (!get_property(w, astate, 2L, astate, &n, &p))
1602 result = *((long *)p);
1608 version(struct swm_region *r, union arg *args)
1610 bar_version = !bar_version;
1612 snprintf(bar_vertext, sizeof bar_vertext,
1613 "Version: %s Build: %s", SCROTWM_VERSION, buildstr);
1615 strlcpy(bar_vertext, "", sizeof bar_vertext);
1620 client_msg(struct ws_win *win, Atom a)
1622 XClientMessageEvent cm;
1627 bzero(&cm, sizeof cm);
1628 cm.type = ClientMessage;
1629 cm.window = win->id;
1630 cm.message_type = aprot;
1633 cm.data.l[1] = CurrentTime;
1634 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1637 /* synthetic response to a ConfigureRequest when not making a change */
1639 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1646 /* send notification of unchanged state. */
1647 ce.type = ConfigureNotify;
1650 ce.width = WIDTH(win);
1651 ce.height = HEIGHT(win);
1652 ce.override_redirect = False;
1656 ce.display = display;
1658 ce.window = win->id;
1659 ce.border_width = border_width;
1663 ce.display = ev->display;
1664 ce.event = ev->window;
1665 ce.window = ev->window;
1666 /* adjust x and y for requested border_width. */
1667 ce.x += border_width - ev->border_width;
1668 ce.y += border_width - ev->border_width;
1669 ce.border_width = ev->border_width;
1670 ce.above = ev->above;
1673 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1674 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1675 ce.y, ce.width, ce.height, ce.border_width);
1677 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1681 count_win(struct workspace *ws, int count_transient)
1686 TAILQ_FOREACH(win, &ws->winlist, entry) {
1687 if (count_transient == 0 && win->floating)
1689 if (count_transient == 0 && win->transient)
1695 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1701 quit(struct swm_region *r, union arg *args)
1703 DNPRINTF(SWM_D_MISC, "quit\n");
1708 unmap_window(struct ws_win *win)
1713 /* don't unmap again */
1714 if (getstate(win->id) == IconicState)
1717 set_win_state(win, IconicState);
1719 XUnmapWindow(display, win->id);
1720 XSetWindowBorder(display, win->id,
1721 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1730 for (i = 0; i < ScreenCount(display); i++)
1731 for (j = 0; j < SWM_WS_MAX; j++)
1732 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1737 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1744 event.display = display; /* Ignored, but what the hell */
1745 event.window = win->id;
1746 event.root = win->s->root;
1747 event.subwindow = None;
1748 event.time = CurrentTime;
1753 event.same_screen = True;
1754 event.keycode = XKeysymToKeycode(display, keysym);
1755 event.state = modifiers;
1757 event.type = KeyPress;
1758 XSendEvent(event.display, event.window, True,
1759 KeyPressMask, (XEvent *)&event);
1761 event.type = KeyRelease;
1762 XSendEvent(event.display, event.window, True,
1763 KeyPressMask, (XEvent *)&event);
1768 restart(struct swm_region *r, union arg *args)
1770 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1772 /* disable alarm because the following code may not be interrupted */
1774 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1775 err(1, "can't disable alarm");
1780 XCloseDisplay(display);
1781 execvp(start_argv[0], start_argv);
1782 fprintf(stderr, "execvp failed\n");
1788 root_to_region(Window root)
1790 struct swm_region *r = NULL;
1792 int i, x, y, wx, wy;
1795 for (i = 0; i < ScreenCount(display); i++)
1796 if (screens[i].root == root)
1799 if (XQueryPointer(display, screens[i].root,
1800 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1801 /* choose a region based on pointer location */
1802 TAILQ_FOREACH(r, &screens[i].rl, entry)
1803 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1804 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1809 r = TAILQ_FIRST(&screens[i].rl);
1815 find_unmanaged_window(Window id)
1820 for (i = 0; i < ScreenCount(display); i++)
1821 for (j = 0; j < SWM_WS_MAX; j++)
1822 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1830 find_window(Window id)
1833 Window wrr, wpr, *wcr = NULL;
1837 for (i = 0; i < ScreenCount(display); i++)
1838 for (j = 0; j < SWM_WS_MAX; j++)
1839 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1843 /* if we were looking for the parent return that window instead */
1844 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1849 /* ignore not found and root */
1850 if (wpr == 0 || wrr == wpr)
1853 /* look for parent */
1854 for (i = 0; i < ScreenCount(display); i++)
1855 for (j = 0; j < SWM_WS_MAX; j++)
1856 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1864 spawn(int ws_idx, union arg *args, int close_fd)
1869 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1872 close(ConnectionNumber(display));
1874 setenv("LD_PRELOAD", SWM_LIB, 1);
1876 if (asprintf(&ret, "%d", ws_idx) == -1) {
1880 setenv("_SWM_WS", ret, 1);
1884 if (asprintf(&ret, "%d", getpid()) == -1) {
1888 setenv("_SWM_PID", ret, 1);
1892 if (setsid() == -1) {
1899 * close stdin and stdout to prevent interaction between apps
1900 * and the baraction script
1901 * leave stderr open to record errors
1903 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1907 dup2(fd, STDIN_FILENO);
1908 dup2(fd, STDOUT_FILENO);
1913 execvp(args->argv[0], args->argv);
1920 spawnterm(struct swm_region *r, union arg *args)
1922 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1926 setenv("_SWM_XTERM_FONTADJ", "", 1);
1927 spawn(r->ws->idx, args, 1);
1932 kill_refs(struct ws_win *win)
1935 struct swm_region *r;
1936 struct workspace *ws;
1941 for (i = 0; i < ScreenCount(display); i++)
1942 TAILQ_FOREACH(r, &screens[i].rl, entry)
1943 for (x = 0; x < SWM_WS_MAX; x++) {
1945 if (win == ws->focus)
1947 if (win == ws->focus_prev)
1948 ws->focus_prev = NULL;
1953 validate_win(struct ws_win *testwin)
1956 struct workspace *ws;
1957 struct swm_region *r;
1960 if (testwin == NULL)
1963 for (i = 0; i < ScreenCount(display); i++)
1964 TAILQ_FOREACH(r, &screens[i].rl, entry)
1965 for (x = 0; x < SWM_WS_MAX; x++) {
1967 TAILQ_FOREACH(win, &ws->winlist, entry)
1975 validate_ws(struct workspace *testws)
1977 struct swm_region *r;
1978 struct workspace *ws;
1981 /* validate all ws */
1982 for (i = 0; i < ScreenCount(display); i++)
1983 TAILQ_FOREACH(r, &screens[i].rl, entry)
1984 for (x = 0; x < SWM_WS_MAX; x++) {
1993 unfocus_win(struct ws_win *win)
1998 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2002 if (win->ws == NULL)
2005 if (validate_ws(win->ws))
2006 return; /* XXX this gets hit with thunderbird, needs fixing */
2008 if (win->ws->r == NULL)
2011 if (validate_win(win)) {
2016 if (win->ws->focus == win) {
2017 win->ws->focus = NULL;
2018 win->ws->focus_prev = win;
2021 if (validate_win(win->ws->focus)) {
2022 kill_refs(win->ws->focus);
2023 win->ws->focus = NULL;
2025 if (validate_win(win->ws->focus_prev)) {
2026 kill_refs(win->ws->focus_prev);
2027 win->ws->focus_prev = NULL;
2030 /* drain all previous unfocus events */
2031 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2034 grabbuttons(win, 0);
2035 XSetWindowBorder(display, win->id,
2036 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2038 XChangeProperty(display, win->s->root,
2039 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2040 PropModeReplace, (unsigned char *)&none,1);
2049 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2051 for (i = 0; i < ScreenCount(display); i++)
2052 for (j = 0; j < SWM_WS_MAX; j++)
2053 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2058 focus_win(struct ws_win *win)
2063 struct ws_win *cfw = NULL;
2066 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2070 if (win->ws == NULL)
2073 if (validate_ws(win->ws))
2074 return; /* XXX this gets hit with thunderbird, needs fixing */
2076 if (validate_win(win)) {
2081 if (validate_win(win)) {
2086 XGetInputFocus(display, &cur_focus, &rr);
2087 if ((cfw = find_window(cur_focus)) != NULL)
2090 /* use larger hammer since the window was killed somehow */
2091 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2092 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2093 XSetWindowBorder(display, cfw->id,
2094 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2097 win->ws->focus = win;
2099 if (win->ws->r != NULL) {
2100 /* drain all previous focus events */
2101 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2105 XSetInputFocus(display, win->id,
2106 RevertToParent, CurrentTime);
2107 grabbuttons(win, 1);
2108 XSetWindowBorder(display, win->id,
2109 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2110 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2111 win->ws->always_raise)
2112 XMapRaised(display, win->id);
2114 XChangeProperty(display, win->s->root,
2115 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2116 PropModeReplace, (unsigned char *)&win->id,1);
2119 if (window_name_enabled)
2124 switchws(struct swm_region *r, union arg *args)
2126 int wsid = args->id, unmap_old = 0;
2127 struct swm_region *this_r, *other_r;
2129 struct workspace *new_ws, *old_ws;
2136 old_ws = this_r->ws;
2137 new_ws = &this_r->s->ws[wsid];
2139 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2140 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2142 if (new_ws == NULL || old_ws == NULL)
2144 if (new_ws == old_ws)
2147 other_r = new_ws->r;
2148 if (other_r == NULL) {
2149 /* the other workspace is hidden, hide this one */
2153 /* the other ws is visible in another region, exchange them */
2154 other_r->ws_prior = new_ws;
2155 other_r->ws = old_ws;
2156 old_ws->r = other_r;
2158 this_r->ws_prior = old_ws;
2159 this_r->ws = new_ws;
2162 /* this is needed so that we can click on a window after a restart */
2166 a.id = SWM_ARG_ID_FOCUSCUR;
2167 focus(new_ws->r, &a);
2171 /* unmap old windows */
2173 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2176 if (focus_mode == SWM_FOCUS_DEFAULT)
2177 drain_enter_notify();
2181 cyclews(struct swm_region *r, union arg *args)
2184 struct swm_screen *s = r->s;
2187 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2188 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2193 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2196 case SWM_ARG_ID_CYCLEWS_UP:
2197 if (a.id < SWM_WS_MAX - 1)
2202 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2205 case SWM_ARG_ID_CYCLEWS_DOWN:
2209 a.id = SWM_WS_MAX - 1;
2216 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2218 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2222 } while (a.id != r->ws->idx);
2226 priorws(struct swm_region *r, union arg *args)
2230 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2231 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2233 if (r->ws_prior == NULL)
2236 a.id = r->ws_prior->idx;
2241 cyclescr(struct swm_region *r, union arg *args)
2243 struct swm_region *rr = NULL;
2247 /* do nothing if we don't have more than one screen */
2248 if (!(ScreenCount(display) > 1 || outputs > 1))
2253 case SWM_ARG_ID_CYCLESC_UP:
2254 rr = TAILQ_NEXT(r, entry);
2256 rr = TAILQ_FIRST(&screens[i].rl);
2258 case SWM_ARG_ID_CYCLESC_DOWN:
2259 rr = TAILQ_PREV(r, swm_region_list, entry);
2261 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2269 /* move mouse to region */
2271 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2272 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2274 a.id = SWM_ARG_ID_FOCUSCUR;
2277 if (rr->ws->focus) {
2278 /* move to focus window */
2279 x = X(rr->ws->focus) + 1;
2280 y = Y(rr->ws->focus) + 1;
2281 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2286 sort_windows(struct ws_win_list *wl)
2288 struct ws_win *win, *parent, *nxt;
2293 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2294 nxt = TAILQ_NEXT(win, entry);
2295 if (win->transient) {
2296 parent = find_window(win->transient);
2297 if (parent == NULL) {
2298 fprintf(stderr, "not possible bug\n");
2301 TAILQ_REMOVE(wl, win, entry);
2302 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2309 swapwin(struct swm_region *r, union arg *args)
2311 struct ws_win *target, *source;
2312 struct ws_win *cur_focus;
2313 struct ws_win_list *wl;
2316 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2317 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2319 cur_focus = r->ws->focus;
2320 if (cur_focus == NULL)
2324 wl = &source->ws->winlist;
2327 case SWM_ARG_ID_SWAPPREV:
2328 if (source->transient)
2329 source = find_window(source->transient);
2330 target = TAILQ_PREV(source, ws_win_list, entry);
2331 if (target && target->transient)
2332 target = find_window(target->transient);
2333 TAILQ_REMOVE(wl, source, entry);
2335 TAILQ_INSERT_TAIL(wl, source, entry);
2337 TAILQ_INSERT_BEFORE(target, source, entry);
2339 case SWM_ARG_ID_SWAPNEXT:
2340 target = TAILQ_NEXT(source, entry);
2341 /* move the parent and let the sort handle the move */
2342 if (source->transient)
2343 source = find_window(source->transient);
2344 TAILQ_REMOVE(wl, source, entry);
2346 TAILQ_INSERT_HEAD(wl, source, entry);
2348 TAILQ_INSERT_AFTER(wl, target, source, entry);
2350 case SWM_ARG_ID_SWAPMAIN:
2351 target = TAILQ_FIRST(wl);
2352 if (target == source) {
2353 if (source->ws->focus_prev != NULL &&
2354 source->ws->focus_prev != target)
2356 source = source->ws->focus_prev;
2360 if (target == NULL || source == NULL)
2362 source->ws->focus_prev = target;
2363 TAILQ_REMOVE(wl, target, entry);
2364 TAILQ_INSERT_BEFORE(source, target, entry);
2365 TAILQ_REMOVE(wl, source, entry);
2366 TAILQ_INSERT_HEAD(wl, source, entry);
2368 case SWM_ARG_ID_MOVELAST:
2369 TAILQ_REMOVE(wl, source, entry);
2370 TAILQ_INSERT_TAIL(wl, source, entry);
2373 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2383 focus_prev(struct ws_win *win)
2385 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2386 struct ws_win *cur_focus = NULL;
2387 struct ws_win_list *wl = NULL;
2388 struct workspace *ws = NULL;
2390 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx\n", WINID(win));
2392 if (!(win && win->ws))
2397 cur_focus = ws->focus;
2398 winlostfocus = cur_focus;
2400 /* pickle, just focus on whatever */
2401 if (cur_focus == NULL) {
2402 /* use prev_focus if valid */
2403 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2404 find_window(WINID(ws->focus_prev)))
2405 winfocus = ws->focus_prev;
2406 if (winfocus == NULL)
2407 winfocus = TAILQ_FIRST(wl);
2411 /* if transient focus on parent */
2412 if (cur_focus->transient) {
2413 winfocus = find_window(cur_focus->transient);
2417 /* if in max_stack try harder */
2418 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2419 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2420 if (cur_focus != ws->focus_prev)
2421 winfocus = ws->focus_prev;
2422 else if (cur_focus != ws->focus)
2423 winfocus = ws->focus;
2425 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2430 if (cur_focus == win)
2431 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2432 if (winfocus == NULL)
2433 winfocus = TAILQ_LAST(wl, ws_win_list);
2434 if (winfocus == NULL || winfocus == win)
2435 winfocus = TAILQ_NEXT(cur_focus, entry);
2437 if (winfocus == winlostfocus || winfocus == NULL) {
2438 /* update the bar so that title/class/name will be cleared. */
2439 if (window_name_enabled || title_name_enabled ||
2440 title_class_enabled)
2446 focus_magic(winfocus);
2450 focus(struct swm_region *r, union arg *args)
2452 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2453 struct ws_win *cur_focus = NULL;
2454 struct ws_win_list *wl = NULL;
2455 struct workspace *ws = NULL;
2461 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2463 /* treat FOCUS_CUR special */
2464 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2465 if (r->ws->focus && r->ws->focus->iconic == 0)
2466 winfocus = r->ws->focus;
2467 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2468 winfocus = r->ws->focus_prev;
2470 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2471 if (winfocus->iconic == 0)
2474 focus_magic(winfocus);
2478 if ((cur_focus = r->ws->focus) == NULL)
2482 if (TAILQ_EMPTY(wl))
2484 /* make sure there is at least one uniconified window */
2486 TAILQ_FOREACH(winfocus, wl, entry)
2487 if (winfocus->iconic == 0) {
2494 winlostfocus = cur_focus;
2497 case SWM_ARG_ID_FOCUSPREV:
2498 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2500 head = TAILQ_LAST(wl, ws_win_list);
2502 if (WINID(winfocus) == cur_focus->transient) {
2503 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2505 head = TAILQ_LAST(wl, ws_win_list);
2510 if (winfocus && winfocus->iconic) {
2511 while (winfocus != cur_focus) {
2512 if (winfocus == NULL)
2513 winfocus = TAILQ_LAST(wl, ws_win_list);
2514 if (winfocus->iconic == 0)
2516 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2522 case SWM_ARG_ID_FOCUSNEXT:
2523 head = TAILQ_NEXT(cur_focus, entry);
2525 head = TAILQ_FIRST(wl);
2529 if (winfocus && winfocus->iconic) {
2530 while (winfocus != cur_focus) {
2531 if (winfocus == NULL)
2532 winfocus = TAILQ_FIRST(wl);
2533 if (winfocus->iconic == 0)
2535 winfocus = TAILQ_NEXT(winfocus, entry);
2540 case SWM_ARG_ID_FOCUSMAIN:
2541 winfocus = TAILQ_FIRST(wl);
2542 if (winfocus == cur_focus)
2543 winfocus = cur_focus->ws->focus_prev;
2549 if (winfocus == winlostfocus || winfocus == NULL) {
2550 /* update the bar so that title/class/name will be cleared. */
2551 if (window_name_enabled || title_name_enabled ||
2552 title_class_enabled)
2558 focus_magic(winfocus);
2562 cycle_layout(struct swm_region *r, union arg *args)
2564 struct workspace *ws = r->ws;
2567 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2570 if (ws->cur_layout->l_stack == NULL)
2571 ws->cur_layout = &layouts[0];
2574 if (focus_mode == SWM_FOCUS_DEFAULT)
2575 drain_enter_notify();
2576 a.id = SWM_ARG_ID_FOCUSCUR;
2582 stack_config(struct swm_region *r, union arg *args)
2584 struct workspace *ws = r->ws;
2586 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2589 if (ws->cur_layout->l_config != NULL)
2590 ws->cur_layout->l_config(ws, args->id);
2592 if (args->id != SWM_ARG_ID_STACKINIT)
2599 struct swm_geometry g;
2600 struct swm_region *r;
2606 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2608 for (i = 0; i < ScreenCount(display); i++) {
2612 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2613 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2614 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2616 /* start with screen geometry, adjust for bar */
2618 g.w -= 2 * border_width;
2619 g.h -= 2 * border_width;
2625 r->ws->cur_layout->l_stack(r->ws, &g);
2626 r->ws->cur_layout->l_string(r->ws);
2627 /* save r so we can track region changes */
2634 if (focus_mode == SWM_FOCUS_DEFAULT)
2635 drain_enter_notify();
2637 DNPRINTF(SWM_D_STACK, "stack: end\n");
2641 store_float_geom(struct ws_win *win, struct swm_region *r)
2643 /* retain window geom and region geom */
2644 win->g_float = win->g;
2645 win->rg_float = r->g;
2646 win->g_floatvalid = 1;
2650 stack_floater(struct ws_win *win, struct swm_region *r)
2658 bzero(&wc, sizeof wc);
2659 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2662 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2663 * geom on ws switches or return from max mode
2665 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2666 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2668 * use stored g and rg to set relative position and size
2669 * as in old region or before max stack mode
2671 X(win) = win->g_float.x - win->rg_float.x + X(r);
2672 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2673 WIDTH(win) = win->g_float.w;
2674 HEIGHT(win) = win->g_float.h;
2675 win->g_floatvalid = 0;
2678 win->floatmaxed = 0;
2680 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2681 (HEIGHT(win) >= HEIGHT(r)))
2682 wc.border_width = 0;
2684 wc.border_width = border_width;
2685 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2686 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
2687 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
2692 * floaters and transients are auto-centred unless moved
2695 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
2696 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
2699 /* win can be outside r if new r smaller than old r */
2700 /* Ensure top left corner inside r (move probs otherwise) */
2701 if (X(win) < X(r) - wc.border_width)
2702 X(win) = X(r) - wc.border_width;
2703 if (X(win) > X(r) + WIDTH(r) - 1)
2704 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
2705 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
2706 if (Y(win) < Y(r) - wc.border_width)
2707 Y(win) = Y(r) - wc.border_width;
2708 if (Y(win) > Y(r) + HEIGHT(r) - 1)
2709 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
2710 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
2714 wc.width = WIDTH(win);
2715 wc.height = HEIGHT(win);
2718 * Retain floater and transient geometry for correct positioning
2719 * when ws changes region
2721 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2722 store_float_geom(win, r);
2724 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2725 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
2727 XConfigureWindow(display, win->id, mask, &wc);
2731 * Send keystrokes to terminal to decrease/increase the font size as the
2732 * window size changes.
2735 adjust_font(struct ws_win *win)
2737 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2738 win->floating || win->transient)
2741 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2742 WIDTH(win) / win->sh.width_inc < term_width &&
2743 win->font_steps < SWM_MAX_FONT_STEPS) {
2744 win->font_size_boundary[win->font_steps] =
2745 (win->sh.width_inc * term_width) + win->sh.base_width;
2748 win->last_inc = win->sh.width_inc;
2749 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2750 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2751 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
2754 win->last_inc = win->sh.width_inc;
2755 fake_keypress(win, XK_KP_Add, ShiftMask);
2759 #define SWAPXY(g) do { \
2761 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2762 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2765 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2768 XWindowAttributes wa;
2769 struct swm_geometry win_g, r_g = *g;
2770 struct ws_win *win, *fs_win = 0;
2771 int i, j, s, stacks;
2772 int w_inc = 1, h_inc, w_base = 1, h_base;
2773 int hrh, extra = 0, h_slice, last_h = 0;
2774 int split, colno, winno, mwin, msize, mscale;
2775 int remain, missing, v_slice, reconfigure;
2778 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
2779 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
2781 winno = count_win(ws, 0);
2782 if (winno == 0 && count_win(ws, 1) == 0)
2785 TAILQ_FOREACH(win, &ws->winlist, entry)
2786 if (win->transient == 0 && win->floating == 0
2787 && win->iconic == 0)
2794 w_inc = win->sh.width_inc;
2795 w_base = win->sh.base_width;
2796 mwin = ws->l_state.horizontal_mwin;
2797 mscale = ws->l_state.horizontal_msize;
2798 stacks = ws->l_state.horizontal_stacks;
2801 w_inc = win->sh.height_inc;
2802 w_base = win->sh.base_height;
2803 mwin = ws->l_state.vertical_mwin;
2804 mscale = ws->l_state.vertical_msize;
2805 stacks = ws->l_state.vertical_stacks;
2809 if (stacks > winno - mwin)
2810 stacks = winno - mwin;
2814 h_slice = r_g.h / SWM_H_SLICE;
2815 if (mwin && winno > mwin) {
2816 v_slice = r_g.w / SWM_V_SLICE;
2820 win_g.w = v_slice * mscale;
2822 if (w_inc > 1 && w_inc < v_slice) {
2823 /* adjust for window's requested size increment */
2824 remain = (win_g.w - w_base) % w_inc;
2825 missing = w_inc - remain;
2832 win_g.x += r_g.w - msize;
2835 colno = split = winno / stacks;
2836 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2837 2 * border_width) / stacks);
2839 hrh = r_g.h / colno;
2840 extra = r_g.h - (colno * hrh);
2841 win_g.h = hrh - 2 * border_width;
2843 /* stack all the tiled windows */
2844 i = j = 0, s = stacks;
2845 TAILQ_FOREACH(win, &ws->winlist, entry) {
2846 if (win->transient != 0 || win->floating != 0)
2848 if (win->iconic != 0)
2851 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2856 if (split && i == split) {
2857 colno = (winno - mwin) / stacks;
2858 if (s <= (winno - mwin) % stacks)
2860 split = split + colno;
2861 hrh = (r_g.h / colno);
2862 extra = r_g.h - (colno * hrh);
2866 win_g.x += win_g.w + 2 * border_width;
2867 win_g.w = (r_g.w - msize -
2868 (stacks * 2 * border_width)) / stacks;
2870 win_g.w += (r_g.w - msize -
2871 (stacks * 2 * border_width)) % stacks;
2875 win_g.h = hrh - 2 * border_width;
2877 h_inc = win->sh.width_inc;
2878 h_base = win->sh.base_width;
2880 h_inc = win->sh.height_inc;
2881 h_base = win->sh.base_height;
2883 if (j == colno - 1) {
2884 win_g.h = hrh + extra;
2885 } else if (h_inc > 1 && h_inc < h_slice) {
2886 /* adjust for window's requested size increment */
2887 remain = (win_g.h - h_base) % h_inc;
2888 missing = h_inc - remain;
2890 if (missing <= extra || j == 0) {
2902 win_g.y += last_h + 2 * border_width;
2904 bzero(&wc, sizeof wc);
2905 if (disable_border && bar_enabled == 0 && winno == 1){
2906 wc.border_width = 0;
2907 win_g.w += 2 * border_width;
2908 win_g.h += 2 * border_width;
2910 wc.border_width = border_width;
2913 if (X(win) != win_g.y || Y(win) != win_g.x ||
2914 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
2916 X(win) = wc.x = win_g.y;
2917 Y(win) = wc.y = win_g.x;
2918 WIDTH(win) = wc.width = win_g.h;
2919 HEIGHT(win) = wc.height = win_g.w;
2922 if (X(win) != win_g.x || Y(win) != win_g.y ||
2923 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
2925 X(win) = wc.x = win_g.x;
2926 Y(win) = wc.y = win_g.y;
2927 WIDTH(win) = wc.width = win_g.w;
2928 HEIGHT(win) = wc.height = win_g.h;
2933 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2934 XConfigureWindow(display, win->id, mask, &wc);
2937 if (XGetWindowAttributes(display, win->id, &wa))
2938 if (wa.map_state == IsUnmapped)
2939 XMapRaised(display, win->id);
2947 /* now, stack all the floaters and transients */
2948 TAILQ_FOREACH(win, &ws->winlist, entry) {
2949 if (win->transient == 0 && win->floating == 0)
2951 if (win->iconic == 1)
2953 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2958 stack_floater(win, ws->r);
2959 XMapRaised(display, win->id);
2963 stack_floater(fs_win, ws->r);
2964 XMapRaised(display, fs_win->id);
2969 vertical_config(struct workspace *ws, int id)
2971 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
2975 case SWM_ARG_ID_STACKRESET:
2976 case SWM_ARG_ID_STACKINIT:
2977 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2978 ws->l_state.vertical_mwin = 1;
2979 ws->l_state.vertical_stacks = 1;
2981 case SWM_ARG_ID_MASTERSHRINK:
2982 if (ws->l_state.vertical_msize > 1)
2983 ws->l_state.vertical_msize--;
2985 case SWM_ARG_ID_MASTERGROW:
2986 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2987 ws->l_state.vertical_msize++;
2989 case SWM_ARG_ID_MASTERADD:
2990 ws->l_state.vertical_mwin++;
2992 case SWM_ARG_ID_MASTERDEL:
2993 if (ws->l_state.vertical_mwin > 0)
2994 ws->l_state.vertical_mwin--;
2996 case SWM_ARG_ID_STACKINC:
2997 ws->l_state.vertical_stacks++;
2999 case SWM_ARG_ID_STACKDEC:
3000 if (ws->l_state.vertical_stacks > 1)
3001 ws->l_state.vertical_stacks--;
3009 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3011 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3013 stack_master(ws, g, 0, 0);
3017 horizontal_config(struct workspace *ws, int id)
3019 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3022 case SWM_ARG_ID_STACKRESET:
3023 case SWM_ARG_ID_STACKINIT:
3024 ws->l_state.horizontal_mwin = 1;
3025 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3026 ws->l_state.horizontal_stacks = 1;
3028 case SWM_ARG_ID_MASTERSHRINK:
3029 if (ws->l_state.horizontal_msize > 1)
3030 ws->l_state.horizontal_msize--;
3032 case SWM_ARG_ID_MASTERGROW:
3033 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3034 ws->l_state.horizontal_msize++;
3036 case SWM_ARG_ID_MASTERADD:
3037 ws->l_state.horizontal_mwin++;
3039 case SWM_ARG_ID_MASTERDEL:
3040 if (ws->l_state.horizontal_mwin > 0)
3041 ws->l_state.horizontal_mwin--;
3043 case SWM_ARG_ID_STACKINC:
3044 ws->l_state.horizontal_stacks++;
3046 case SWM_ARG_ID_STACKDEC:
3047 if (ws->l_state.horizontal_stacks > 1)
3048 ws->l_state.horizontal_stacks--;
3056 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3058 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3060 stack_master(ws, g, 1, 0);
3063 /* fullscreen view */
3065 max_stack(struct workspace *ws, struct swm_geometry *g)
3068 struct swm_geometry gg = *g;
3069 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3073 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3078 winno = count_win(ws, 0);
3079 if (winno == 0 && count_win(ws, 1) == 0)
3082 TAILQ_FOREACH(win, &ws->winlist, entry) {
3083 if (win->transient) {
3085 parent = find_window(win->transient);
3089 if (win->floating && win->floatmaxed == 0 ) {
3091 * retain geometry for retrieval on exit from
3094 store_float_geom(win, ws->r);
3095 win->floatmaxed = 1;
3098 /* only reconfigure if necessary */
3099 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3100 HEIGHT(win) != gg.h) {
3101 bzero(&wc, sizeof wc);
3102 X(win) = wc.x = gg.x;
3103 Y(win) = wc.y = gg.y;
3105 wc.border_width = border_width;
3106 WIDTH(win) = wc.width = gg.w;
3107 HEIGHT(win) = wc.height = gg.h;
3109 wc.border_width = 0;
3110 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3111 HEIGHT(win) = wc.height = gg.h +
3114 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3115 XConfigureWindow(display, win->id, mask, &wc);
3117 /* unmap only if we don't have multi screen */
3118 if (win != ws->focus)
3119 if (!(ScreenCount(display) > 1 || outputs > 1))
3123 /* put the last transient on top */
3126 XMapRaised(display, parent->id);
3127 stack_floater(wintrans, ws->r);
3128 focus_magic(wintrans);
3133 send_to_ws(struct swm_region *r, union arg *args)
3135 int wsid = args->id;
3136 struct ws_win *win = NULL, *parent;
3137 struct workspace *ws, *nws;
3138 Atom ws_idx_atom = 0;
3139 unsigned char ws_idx_str[SWM_PROPLEN];
3142 if (r && r->ws && r->ws->focus)
3148 if (win->ws->idx == wsid)
3151 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3154 nws = &win->s->ws[wsid];
3156 a.id = SWM_ARG_ID_FOCUSPREV;
3158 if (win->transient) {
3159 parent = find_window(win->transient);
3161 unmap_window(parent);
3162 TAILQ_REMOVE(&ws->winlist, parent, entry);
3163 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3168 TAILQ_REMOVE(&ws->winlist, win, entry);
3169 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3170 if (TAILQ_EMPTY(&ws->winlist))
3171 r->ws->focus = NULL;
3174 /* Try to update the window's workspace property */
3175 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3177 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3179 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3181 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3182 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3189 pressbutton(struct swm_region *r, union arg *args)
3191 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3192 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3196 raise_toggle(struct swm_region *r, union arg *args)
3198 if (r && r->ws == NULL)
3201 r->ws->always_raise = !r->ws->always_raise;
3203 /* bring floaters back to top */
3204 if (r->ws->always_raise == 0)
3209 iconify(struct swm_region *r, union arg *args)
3213 if (r->ws->focus == NULL)
3215 unmap_window(r->ws->focus);
3216 update_iconic(r->ws->focus, 1);
3218 if (focus_mode == SWM_FOCUS_DEFAULT)
3219 drain_enter_notify();
3220 r->ws->focus = NULL;
3221 a.id = SWM_ARG_ID_FOCUSCUR;
3226 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3227 unsigned long *slen)
3230 unsigned long nitems, nbytes, nextra;
3231 unsigned char *prop = NULL;
3234 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3235 &rettype, &retfmt, &nitems, &nbytes, &prop);
3236 if (status != Success)
3240 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3241 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3242 if (status != Success) {
3246 if (rettype != stype) {
3255 uniconify(struct swm_region *r, union arg *args)
3259 unsigned char *name;
3263 DNPRINTF(SWM_D_MISC, "uniconify\n");
3265 if (r && r->ws == NULL)
3268 /* make sure we have anything to uniconify */
3269 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3270 if (win->ws == NULL)
3271 continue; /* should never happen */
3272 if (win->iconic == 0)
3280 search_resp_action = SWM_SEARCH_UNICONIFY;
3282 spawn_select(r, args, "search", &searchpid);
3284 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3287 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3288 if (win->ws == NULL)
3289 continue; /* should never happen */
3290 if (win->iconic == 0)
3293 name = get_win_name(display, win->id, a_netwmname,
3294 a_utf8_string, &len);
3297 fprintf(lfile, "%s.%lu\n", name, win->id);
3305 name_workspace(struct swm_region *r, union arg *args)
3309 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3315 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3317 spawn_select(r, args, "name_workspace", &searchpid);
3319 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3322 fprintf(lfile, "%s", "");
3327 search_workspace(struct swm_region *r, union arg *args)
3330 struct workspace *ws;
3333 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3339 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3341 spawn_select(r, args, "search", &searchpid);
3343 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3346 for (i = 0; i < SWM_WS_MAX; i++) {
3350 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3351 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3358 search_win_cleanup(void)
3360 struct search_window *sw = NULL;
3362 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3363 XDestroyWindow(display, sw->indicator);
3364 TAILQ_REMOVE(&search_wl, sw, entry);
3370 search_win(struct swm_region *r, union arg *args)
3372 struct ws_win *win = NULL;
3373 struct search_window *sw = NULL;
3383 DNPRINTF(SWM_D_MISC, "search_win\n");
3386 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3388 spawn_select(r, args, "search", &searchpid);
3390 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3393 TAILQ_INIT(&search_wl);
3396 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3397 if (win->iconic == 1)
3400 sw = calloc(1, sizeof(struct search_window));
3402 fprintf(stderr, "search_win: calloc: %s",
3405 search_win_cleanup();
3411 snprintf(s, sizeof s, "%d", i);
3413 textwidth = XTextWidth(bar_fs, s, len);
3415 w = XCreateSimpleWindow(display,
3416 win->id, 0, 0, textwidth + 12,
3417 bar_fs->ascent + bar_fs->descent + 4, 1,
3418 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3419 r->s->c[SWM_S_COLOR_FOCUS].color);
3422 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3424 gc = XCreateGC(display, w, 0, &gcv);
3425 XSetFont(display, gc, bar_fs->fid);
3426 XMapRaised(display, w);
3427 XSetForeground(display, gc, r->s->c[SWM_S_COLOR_BAR].color);
3429 XDrawString(display, w, gc, 6, bar_fs->ascent + 2, s, len);
3431 fprintf(lfile, "%d\n", i);
3439 search_resp_uniconify(char *resp, unsigned long len)
3441 unsigned char *name;
3445 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3447 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3448 if (win->iconic == 0)
3450 name = get_win_name(display, win->id, a_netwmname,
3451 a_utf8_string, &len);
3454 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3459 if (strncmp(s, resp, len) == 0) {
3460 /* XXX this should be a callback to generalize */
3461 update_iconic(win, 0);
3470 search_resp_name_workspace(char *resp, unsigned long len)
3472 struct workspace *ws;
3474 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3476 if (search_r->ws == NULL)
3481 free(search_r->ws->name);
3482 search_r->ws->name = NULL;
3486 ws->name = strdup(resp);
3487 if (ws->name == NULL) {
3488 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3489 "strdup: %s", strerror(errno));
3496 search_resp_search_workspace(char *resp, unsigned long len)
3503 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3507 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3514 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3516 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3523 switchws(search_r, &a);
3527 search_resp_search_window(char *resp, unsigned long len)
3532 struct search_window *sw;
3534 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3538 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3543 idx = strtonum(s, 1, INT_MAX, &errstr);
3545 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3552 TAILQ_FOREACH(sw, &search_wl, entry)
3553 if (idx == sw->idx) {
3559 #define MAX_RESP_LEN 1024
3562 search_do_resp(void)
3568 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3573 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3574 fprintf(stderr, "search: calloc\n");
3578 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3580 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3583 resp[rbytes] = '\0';
3586 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3587 * newline, so work around that by sanitizing the resp now.
3589 resp[strcspn(resp, "\n")] = '\0';
3592 switch (search_resp_action) {
3593 case SWM_SEARCH_UNICONIFY:
3594 search_resp_uniconify(resp, len);
3596 case SWM_SEARCH_NAME_WORKSPACE:
3597 search_resp_name_workspace(resp, len);
3599 case SWM_SEARCH_SEARCH_WORKSPACE:
3600 search_resp_search_workspace(resp, len);
3602 case SWM_SEARCH_SEARCH_WINDOW:
3603 search_resp_search_window(resp, len);
3608 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3609 search_win_cleanup();
3611 search_resp_action = SWM_SEARCH_NONE;
3612 close(select_resp_pipe[0]);
3617 wkill(struct swm_region *r, union arg *args)
3619 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3621 if (r->ws->focus == NULL)
3624 if (args->id == SWM_ARG_ID_KILLWINDOW)
3625 XKillClient(display, r->ws->focus->id);
3627 if (r->ws->focus->can_delete)
3628 client_msg(r->ws->focus, adelete);
3633 floating_toggle_win(struct ws_win *win)
3635 struct swm_region *r;
3645 /* reject floating toggles in max stack mode */
3646 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3649 if (win->floating) {
3650 if (!win->floatmaxed) {
3651 /* retain position for refloat */
3652 store_float_geom(win, r);
3656 if (win->g_floatvalid) {
3657 /* refloat at last floating relative position */
3658 X(win) = win->g_float.x - win->rg_float.x + X(r);
3659 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3660 WIDTH(win) = win->g_float.w;
3661 HEIGHT(win) = win->g_float.h;
3666 ewmh_update_actions(win);
3672 floating_toggle(struct swm_region *r, union arg *args)
3674 struct ws_win *win = r->ws->focus;
3680 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3681 _NET_WM_STATE_TOGGLE);
3684 if (focus_mode == SWM_FOCUS_DEFAULT)
3685 drain_enter_notify();
3687 if (win == win->ws->focus) {
3688 a.id = SWM_ARG_ID_FOCUSCUR;
3689 focus(win->ws->r, &a);
3694 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3696 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
3698 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
3700 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
3703 if (X(win) < X(r) - border_width) {
3705 WIDTH(win) -= X(r) - X(win) - border_width;
3707 X(win) = X(r) - border_width;
3710 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
3712 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
3714 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
3717 if (Y(win) < Y(r) - border_width) {
3719 HEIGHT(win) -= Y(r) - Y(win) - border_width;
3721 Y(win) = Y(r) - border_width;
3726 if (HEIGHT(win) < 1)
3731 update_window(struct ws_win *win)
3736 bzero(&wc, sizeof wc);
3737 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
3738 wc.border_width = border_width;
3741 wc.width = WIDTH(win);
3742 wc.height = HEIGHT(win);
3744 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
3745 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3747 XConfigureWindow(display, win->id, mask, &wc);
3750 #define SWM_RESIZE_STEPS (50)
3753 resize(struct ws_win *win, union arg *args)
3757 struct swm_region *r = NULL;
3758 int resize_step = 0;
3762 struct swm_geometry g;
3763 int top = 0, left = 0;
3766 unsigned int shape; /* cursor style */
3772 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
3773 "transient: 0x%lx\n", win->id, YESNO(win->floating),
3776 if (!(win->transient != 0 || win->floating != 0))
3779 /* reject resizes in max mode for floaters (transient ok) */
3780 if (win->floatmaxed)
3784 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3790 case SWM_ARG_ID_WIDTHSHRINK:
3791 WIDTH(win) -= SWM_RESIZE_STEPS;
3794 case SWM_ARG_ID_WIDTHGROW:
3795 WIDTH(win) += SWM_RESIZE_STEPS;
3798 case SWM_ARG_ID_HEIGHTSHRINK:
3799 HEIGHT(win) -= SWM_RESIZE_STEPS;
3802 case SWM_ARG_ID_HEIGHTGROW:
3803 HEIGHT(win) += SWM_RESIZE_STEPS;
3810 constrain_window(win, r, 1);
3812 store_float_geom(win,r);
3816 if (focus_mode == SWM_FOCUS_DEFAULT)
3817 drain_enter_notify();
3819 /* get cursor offset from window root */
3820 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
3825 if (wx < WIDTH(win) / 2)
3828 if (wy < HEIGHT(win) / 2)
3831 if (args->id == SWM_ARG_ID_CENTER)
3834 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
3836 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
3838 cursor = XCreateFontCursor(display, shape);
3840 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3841 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
3842 XFreeCursor(display, cursor);
3847 XMaskEvent(display, MOUSEMASK | ExposureMask |
3848 SubstructureRedirectMask, &ev);
3850 case ConfigureRequest:
3853 handler[ev.type](&ev);
3856 /* cursor offset/delta from start of the operation */
3857 dx = ev.xmotion.x_root - x;
3858 dy = ev.xmotion.y_root - y;
3864 if (args->id == SWM_ARG_ID_CENTER) {
3865 if (g.h / 2 + dy < 1)
3869 HEIGHT(win) = g.h + 2 * dy;
3877 HEIGHT(win) = g.h + dy;
3884 if (args->id == SWM_ARG_ID_CENTER) {
3885 if (g.w / 2 + dx < 1)
3889 WIDTH(win) = g.w + 2 * dx;
3897 WIDTH(win) = g.w + dx;
3900 constrain_window(win, r, 1);
3902 /* not free, don't sync more than 120 times / second */
3903 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3904 time = ev.xmotion.time;
3905 XSync(display, False);
3910 } while (ev.type != ButtonRelease);
3912 XSync(display, False);
3915 store_float_geom(win,r);
3917 XUngrabPointer(display, CurrentTime);
3918 XFreeCursor(display, cursor);
3921 drain_enter_notify();
3925 resize_step(struct swm_region *r, union arg *args)
3927 struct ws_win *win = NULL;
3929 if (r && r->ws && r->ws->focus)
3937 #define SWM_MOVE_STEPS (50)
3940 move(struct ws_win *win, union arg *args)
3945 struct swm_region *r = NULL;
3955 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
3956 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
3958 /* in max_stack mode should only move transients */
3959 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3963 if (win->floating == 0 && !win->transient) {
3964 store_float_geom(win,r);
3965 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3968 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3975 case SWM_ARG_ID_MOVELEFT:
3976 X(win) -= (SWM_MOVE_STEPS - border_width);
3979 case SWM_ARG_ID_MOVERIGHT:
3980 X(win) += (SWM_MOVE_STEPS - border_width);
3983 case SWM_ARG_ID_MOVEUP:
3984 Y(win) -= (SWM_MOVE_STEPS - border_width);
3987 case SWM_ARG_ID_MOVEDOWN:
3988 Y(win) += (SWM_MOVE_STEPS - border_width);
3995 constrain_window(win, r, 0);
3997 store_float_geom(win, r);
4001 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4002 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4003 CurrentTime) != GrabSuccess)
4006 /* get cursor offset from window root */
4007 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4011 XMaskEvent(display, MOUSEMASK | ExposureMask |
4012 SubstructureRedirectMask, &ev);
4014 case ConfigureRequest:
4017 handler[ev.type](&ev);
4020 X(win) = ev.xmotion.x_root - wx - border_width;
4021 Y(win) = ev.xmotion.y_root - wy - border_width;
4023 constrain_window(win, r, 0);
4025 /* not free, don't sync more than 120 times / second */
4026 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4027 time = ev.xmotion.time;
4028 XSync(display, False);
4033 } while (ev.type != ButtonRelease);
4035 XSync(display, False);
4038 store_float_geom(win,r);
4039 XUngrabPointer(display, CurrentTime);
4042 drain_enter_notify();
4046 move_step(struct swm_region *r, union arg *args)
4048 struct ws_win *win = NULL;
4050 if (r && r->ws && r->ws->focus)
4055 if (!(win->transient != 0 || win->floating != 0))
4062 /* user/key callable function IDs */
4132 kf_search_workspace,
4134 kf_dumpwins, /* MUST BE LAST */
4138 /* key definitions */
4140 dummykeyfunc(struct swm_region *r, union arg *args)
4145 legacyfunc(struct swm_region *r, union arg *args)
4150 char name[SWM_FUNCNAME_LEN];
4151 void (*func)(struct swm_region *r, union arg *);
4153 } keyfuncs[kf_invalid + 1] = {
4154 /* name function argument */
4155 { "cycle_layout", cycle_layout, {0} },
4156 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4157 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4158 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4159 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4160 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4161 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4162 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4163 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4164 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4165 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4166 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4167 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4168 { "spawn_term", spawnterm, {.argv = spawn_term} },
4169 { "spawn_menu", legacyfunc, {0} },
4170 { "quit", quit, {0} },
4171 { "restart", restart, {0} },
4172 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4173 { "ws_1", switchws, {.id = 0} },
4174 { "ws_2", switchws, {.id = 1} },
4175 { "ws_3", switchws, {.id = 2} },
4176 { "ws_4", switchws, {.id = 3} },
4177 { "ws_5", switchws, {.id = 4} },
4178 { "ws_6", switchws, {.id = 5} },
4179 { "ws_7", switchws, {.id = 6} },
4180 { "ws_8", switchws, {.id = 7} },
4181 { "ws_9", switchws, {.id = 8} },
4182 { "ws_10", switchws, {.id = 9} },
4183 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4184 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4185 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4186 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4187 { "ws_prior", priorws, {0} },
4188 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4189 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4190 { "mvws_1", send_to_ws, {.id = 0} },
4191 { "mvws_2", send_to_ws, {.id = 1} },
4192 { "mvws_3", send_to_ws, {.id = 2} },
4193 { "mvws_4", send_to_ws, {.id = 3} },
4194 { "mvws_5", send_to_ws, {.id = 4} },
4195 { "mvws_6", send_to_ws, {.id = 5} },
4196 { "mvws_7", send_to_ws, {.id = 6} },
4197 { "mvws_8", send_to_ws, {.id = 7} },
4198 { "mvws_9", send_to_ws, {.id = 8} },
4199 { "mvws_10", send_to_ws, {.id = 9} },
4200 { "bar_toggle", bar_toggle, {0} },
4201 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4202 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4203 { "screenshot_all", legacyfunc, {0} },
4204 { "screenshot_wind", legacyfunc, {0} },
4205 { "float_toggle", floating_toggle,{0} },
4206 { "version", version, {0} },
4207 { "spawn_lock", legacyfunc, {0} },
4208 { "spawn_initscr", legacyfunc, {0} },
4209 { "spawn_custom", dummykeyfunc, {0} },
4210 { "iconify", iconify, {0} },
4211 { "uniconify", uniconify, {0} },
4212 { "raise_toggle", raise_toggle, {0} },
4213 { "button2", pressbutton, {2} },
4214 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4215 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4216 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4217 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4218 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4219 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4220 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4221 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4222 { "name_workspace", name_workspace, {0} },
4223 { "search_workspace", search_workspace, {0} },
4224 { "search_win", search_win, {0} },
4225 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4226 { "invalid key func", NULL, {0} },
4231 enum keyfuncid funcid;
4234 int keys_size = 0, keys_length = 0;
4235 struct key *keys = NULL;
4238 enum { client_click, root_click };
4240 unsigned int action;
4242 unsigned int button;
4243 void (*func)(struct ws_win *, union arg *);
4246 /* action key mouse button func args */
4247 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4248 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4249 { client_click, MODKEY, Button1, move, {0} },
4253 update_modkey(unsigned int mod)
4258 for (i = 0; i < keys_length; i++)
4259 if (keys[i].mod & ShiftMask)
4260 keys[i].mod = mod | ShiftMask;
4264 for (i = 0; i < LENGTH(buttons); i++)
4265 if (buttons[i].mask & ShiftMask)
4266 buttons[i].mask = mod | ShiftMask;
4268 buttons[i].mask = mod;
4278 int spawns_size = 0, spawns_length = 0;
4279 struct spawn_prog *spawns = NULL;
4282 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4285 struct spawn_prog *prog = NULL;
4287 char *ap, **real_args;
4289 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4292 for (i = 0; i < spawns_length; i++) {
4293 if (!strcasecmp(spawn_name, spawns[i].name))
4297 fprintf(stderr, "spawn_custom: program %s not found\n",
4302 /* make room for expanded args */
4303 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4304 err(1, "spawn_custom: calloc real_args");
4306 /* expand spawn_args into real_args */
4307 for (i = 0; i < prog->argc; i++) {
4309 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4310 if (!strcasecmp(ap, "$bar_border")) {
4312 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4314 err(1, "spawn_custom border color");
4315 } else if (!strcasecmp(ap, "$bar_color")) {
4317 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4319 err(1, "spawn_custom bar color");
4320 } else if (!strcasecmp(ap, "$bar_font")) {
4321 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
4323 err(1, "spawn_custom bar fonts");
4324 } else if (!strcasecmp(ap, "$bar_font_color")) {
4326 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4328 err(1, "spawn_custom color font");
4329 } else if (!strcasecmp(ap, "$color_focus")) {
4331 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4333 err(1, "spawn_custom color focus");
4334 } else if (!strcasecmp(ap, "$color_unfocus")) {
4336 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4338 err(1, "spawn_custom color unfocus");
4340 /* no match --> copy as is */
4341 if ((real_args[i] = strdup(ap)) == NULL)
4342 err(1, "spawn_custom strdup(ap)");
4344 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4349 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4350 for (i = 0; i < prog->argc; i++)
4351 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4352 DNPRINTF(SWM_D_SPAWN, "\n");
4354 *ret_args = real_args;
4355 return (prog->argc);
4359 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4365 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4369 spawn(r->ws->idx, &a, 1);
4371 for (i = 0; i < spawn_argc; i++)
4377 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4383 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4387 if (pipe(select_list_pipe) == -1)
4388 err(1, "pipe error");
4389 if (pipe(select_resp_pipe) == -1)
4390 err(1, "pipe error");
4392 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4393 err(1, "could not disable SIGPIPE");
4394 switch (*pid = fork()) {
4396 err(1, "cannot fork");
4399 if (dup2(select_list_pipe[0], 0) == -1)
4401 if (dup2(select_resp_pipe[1], 1) == -1)
4403 close(select_list_pipe[1]);
4404 close(select_resp_pipe[0]);
4405 spawn(r->ws->idx, &a, 0);
4407 default: /* parent */
4408 close(select_list_pipe[0]);
4409 close(select_resp_pipe[1]);
4413 for (i = 0; i < spawn_argc; i++)
4419 setspawn(struct spawn_prog *prog)
4423 if (prog == NULL || prog->name == NULL)
4427 for (i = 0; i < spawns_length; i++) {
4428 if (!strcmp(spawns[i].name, prog->name)) {
4430 if (prog->argv == NULL) {
4432 DNPRINTF(SWM_D_SPAWN,
4433 "setspawn: delete #%d: %s\n",
4435 free(spawns[i].name);
4436 for (j = 0; j < spawns[i].argc; j++)
4437 free(spawns[i].argv[j]);
4438 free(spawns[i].argv);
4439 j = spawns_length - 1;
4441 spawns[i] = spawns[j];
4446 DNPRINTF(SWM_D_SPAWN,
4447 "setspawn: replace #%d: %s\n",
4449 free(spawns[i].name);
4450 for (j = 0; j < spawns[i].argc; j++)
4451 free(spawns[i].argv[j]);
4452 free(spawns[i].argv);
4455 /* found case handled */
4461 if (prog->argv == NULL) {
4463 "error: setspawn: cannot find program: %s", prog->name);
4468 /* not found: add */
4469 if (spawns_size == 0 || spawns == NULL) {
4471 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4472 spawns = malloc((size_t)spawns_size *
4473 sizeof(struct spawn_prog));
4474 if (spawns == NULL) {
4475 fprintf(stderr, "setspawn: malloc failed\n");
4479 } else if (spawns_length == spawns_size) {
4481 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4482 spawns = realloc(spawns, (size_t)spawns_size *
4483 sizeof(struct spawn_prog));
4484 if (spawns == NULL) {
4485 fprintf(stderr, "setspawn: realloc failed\n");
4491 if (spawns_length < spawns_size) {
4492 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4493 spawns_length, prog->name);
4494 i = spawns_length++;
4497 fprintf(stderr, "spawns array problem?\n");
4498 if (spawns == NULL) {
4499 fprintf(stderr, "spawns array is NULL!\n");
4507 setconfspawn(char *selector, char *value, int flags)
4509 struct spawn_prog *prog;
4510 char *vp, *cp, *word;
4512 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4513 if ((prog = calloc(1, sizeof *prog)) == NULL)
4514 err(1, "setconfspawn: calloc prog");
4515 prog->name = strdup(selector);
4516 if (prog->name == NULL)
4517 err(1, "setconfspawn prog->name");
4518 if ((cp = vp = strdup(value)) == NULL)
4519 err(1, "setconfspawn: strdup(value) ");
4520 while ((word = strsep(&cp, " \t")) != NULL) {
4521 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4523 cp += (long)strspn(cp, " \t");
4524 if (strlen(word) > 0) {
4526 if ((prog->argv = realloc(prog->argv,
4527 prog->argc * sizeof(char *))) == NULL)
4528 err(1, "setconfspawn: realloc");
4529 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4530 err(1, "setconfspawn: strdup");
4537 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4544 setconfspawn("term", "xterm", 0);
4545 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4546 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4547 setconfspawn("lock", "xlock", 0);
4548 setconfspawn("initscr", "initscreen.sh", 0);
4549 setconfspawn("menu", "dmenu_run"
4552 " -nf $bar_font_color"
4554 " -sf $bar_color", 0);
4555 setconfspawn("search", "dmenu"
4559 " -nf $bar_font_color"
4561 " -sf $bar_color", 0);
4562 setconfspawn("name_workspace", "dmenu"
4566 " -nf $bar_font_color"
4568 " -sf $bar_color", 0);
4572 #define SWM_MODNAME_SIZE 32
4573 #define SWM_KEY_WS "\n+ \t"
4575 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4579 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4580 if (mod == NULL || ks == NULL) {
4581 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4584 if (keystr == NULL || strlen(keystr) == 0) {
4585 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4591 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4592 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4594 cp += (long)strspn(cp, SWM_KEY_WS);
4595 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4597 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4599 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4601 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4603 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4605 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4607 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4608 *mod |= ControlMask;
4610 *ks = XStringToKeysym(name);
4611 XConvertCase(*ks, ks, &uks);
4612 if (ks == NoSymbol) {
4614 "parsekeys: invalid key %s\n",
4620 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4625 strdupsafe(char *str)
4630 return (strdup(str));
4634 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4638 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4639 keyfuncs[kfid].name, spawn_name);
4641 for (i = 0; i < keys_length; i++) {
4642 if (keys[i].mod == mod && keys[i].keysym == ks) {
4643 if (kfid == kf_invalid) {
4646 "setkeybinding: delete #%d %s\n",
4647 i, keyfuncs[keys[i].funcid].name);
4648 free(keys[i].spawn_name);
4649 j = keys_length - 1;
4653 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4656 /* found: replace */
4658 "setkeybinding: replace #%d %s [%s]\n",
4659 i, keyfuncs[keys[i].funcid].name,
4661 free(keys[i].spawn_name);
4663 keys[i].keysym = ks;
4664 keys[i].funcid = kfid;
4665 keys[i].spawn_name = strdupsafe(spawn_name);
4666 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4671 if (kfid == kf_invalid) {
4673 "error: setkeybinding: cannot find mod/key combination");
4674 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4677 /* not found: add */
4678 if (keys_size == 0 || keys == NULL) {
4680 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4681 keys = malloc((size_t)keys_size * sizeof(struct key));
4683 fprintf(stderr, "malloc failed\n");
4687 } else if (keys_length == keys_size) {
4689 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4690 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4692 fprintf(stderr, "realloc failed\n");
4697 if (keys_length < keys_size) {
4699 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s [%s]\n",
4700 j, keyfuncs[kfid].name, spawn_name);
4702 keys[j].keysym = ks;
4703 keys[j].funcid = kfid;
4704 keys[j].spawn_name = strdupsafe(spawn_name);
4706 fprintf(stderr, "keys array problem?\n");
4708 fprintf(stderr, "keys array problem\n");
4712 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4716 setconfbinding(char *selector, char *value, int flags)
4718 enum keyfuncid kfid;
4722 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4723 if (selector == NULL) {
4724 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4725 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4727 setkeybinding(mod, ks, kfid, NULL);
4732 /* search by key function name */
4733 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4734 if (strncasecmp(selector, keyfuncs[kfid].name,
4735 SWM_FUNCNAME_LEN) == 0) {
4736 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4738 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4739 setkeybinding(mod, ks, kfid, NULL);
4745 /* search by custom spawn name */
4746 for (i = 0; i < spawns_length; i++) {
4747 if (strcasecmp(selector, spawns[i].name) == 0) {
4748 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4750 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4751 setkeybinding(mod, ks, kf_spawn_custom,
4758 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4765 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4766 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4767 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4768 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4769 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4770 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4771 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4772 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4773 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4774 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4775 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4776 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4777 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4778 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4779 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4780 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4781 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4782 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4783 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4784 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4785 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4786 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4787 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4788 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4789 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4790 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4791 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4792 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4793 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4794 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4795 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4796 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4797 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4798 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4799 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4800 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4801 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4802 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4803 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4804 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4805 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4806 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4807 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4808 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4809 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4810 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4811 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4812 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4813 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4814 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4815 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4816 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4817 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4818 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4819 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4820 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4821 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4822 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4823 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4824 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4825 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4826 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4827 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4828 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4829 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4830 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4831 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4832 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4833 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4834 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4835 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4837 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4846 /* clear all key bindings, if any */
4847 for (i = 0; i < keys_length; i++)
4848 free(keys[i].spawn_name);
4853 setkeymapping(char *selector, char *value, int flags)
4855 char keymapping_file[PATH_MAX];
4856 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4857 if (value[0] == '~')
4858 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4859 pwd->pw_dir, &value[1]);
4861 strlcpy(keymapping_file, value, sizeof keymapping_file);
4863 /* load new key bindings; if it fails, revert to default bindings */
4864 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4868 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4873 updatenumlockmask(void)
4876 XModifierKeymap *modmap;
4878 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4880 modmap = XGetModifierMapping(display);
4881 for (i = 0; i < 8; i++)
4882 for (j = 0; j < modmap->max_keypermod; j++)
4883 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4884 == XKeysymToKeycode(display, XK_Num_Lock))
4885 numlockmask = (1 << i);
4887 XFreeModifiermap(modmap);
4893 unsigned int i, j, k;
4895 unsigned int modifiers[] =
4896 { 0, LockMask, numlockmask, numlockmask | LockMask };
4898 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4899 updatenumlockmask();
4901 for (k = 0; k < ScreenCount(display); k++) {
4902 if (TAILQ_EMPTY(&screens[k].rl))
4904 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4905 for (i = 0; i < keys_length; i++) {
4906 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4907 for (j = 0; j < LENGTH(modifiers); j++)
4908 XGrabKey(display, code,
4909 keys[i].mod | modifiers[j],
4910 screens[k].root, True,
4911 GrabModeAsync, GrabModeAsync);
4917 grabbuttons(struct ws_win *win, int focused)
4920 unsigned int modifiers[] =
4921 { 0, LockMask, numlockmask, numlockmask|LockMask };
4923 updatenumlockmask();
4924 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4926 for (i = 0; i < LENGTH(buttons); i++)
4927 if (buttons[i].action == client_click)
4928 for (j = 0; j < LENGTH(modifiers); j++)
4929 XGrabButton(display, buttons[i].button,
4930 buttons[i].mask | modifiers[j],
4931 win->id, False, BUTTONMASK,
4932 GrabModeAsync, GrabModeSync, None,
4935 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4936 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4939 const char *quirkname[] = {
4940 "NONE", /* config string for "no value" */
4949 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4950 #define SWM_Q_WS "\n|+ \t"
4952 parsequirks(char *qstr, unsigned long *quirk)
4962 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4964 cp += (long)strspn(cp, SWM_Q_WS);
4965 for (i = 0; i < LENGTH(quirkname); i++) {
4966 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4967 DNPRINTF(SWM_D_QUIRK,
4968 "parsequirks: %s\n", name);
4973 *quirk |= 1 << (i-1);
4977 if (i >= LENGTH(quirkname)) {
4978 DNPRINTF(SWM_D_QUIRK,
4979 "parsequirks: invalid quirk [%s]\n", name);
4987 setquirk(const char *class, const char *name, const int quirk)
4992 for (i = 0; i < quirks_length; i++) {
4993 if (!strcmp(quirks[i].class, class) &&
4994 !strcmp(quirks[i].name, name)) {
4997 DNPRINTF(SWM_D_QUIRK,
4998 "setquirk: delete #%d %s:%s\n",
4999 i, quirks[i].class, quirks[i].name);
5000 free(quirks[i].class);
5001 free(quirks[i].name);
5002 j = quirks_length - 1;
5004 quirks[i] = quirks[j];
5008 /* found: replace */
5009 DNPRINTF(SWM_D_QUIRK,
5010 "setquirk: replace #%d %s:%s\n",
5011 i, quirks[i].class, quirks[i].name);
5012 free(quirks[i].class);
5013 free(quirks[i].name);
5014 quirks[i].class = strdup(class);
5015 quirks[i].name = strdup(name);
5016 quirks[i].quirk = quirk;
5023 "error: setquirk: cannot find class/name combination");
5026 /* not found: add */
5027 if (quirks_size == 0 || quirks == NULL) {
5029 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
5030 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
5031 if (quirks == NULL) {
5032 fprintf(stderr, "setquirk: malloc failed\n");
5036 } else if (quirks_length == quirks_size) {
5038 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
5039 quirks = realloc(quirks,
5040 (size_t)quirks_size * sizeof(struct quirk));
5041 if (quirks == NULL) {
5042 fprintf(stderr, "setquirk: realloc failed\n");
5047 if (quirks_length < quirks_size) {
5048 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
5049 j = quirks_length++;
5050 quirks[j].class = strdup(class);
5051 quirks[j].name = strdup(name);
5052 quirks[j].quirk = quirk;
5054 fprintf(stderr, "quirks array problem?\n");
5055 if (quirks == NULL) {
5056 fprintf(stderr, "quirks array problem!\n");
5063 setconfquirk(char *selector, char *value, int flags)
5065 char *cp, *class, *name;
5067 unsigned long quirks;
5068 if (selector == NULL)
5070 if ((cp = strchr(selector, ':')) == NULL)
5075 if ((retval = parsequirks(value, &quirks)) == 0)
5076 setquirk(class, name, quirks);
5083 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5084 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5085 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5086 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5087 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5088 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5089 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5090 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5091 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5092 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5093 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5094 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5095 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5098 /* conf file stuff */
5099 #define SWM_CONF_FILE "scrotwm.conf"
5101 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5102 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5103 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5104 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5105 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5106 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5107 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5108 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5109 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5113 setconfvalue(char *selector, char *value, int flags)
5118 case SWM_S_BAR_DELAY:
5119 bar_delay = atoi(value);
5121 case SWM_S_BAR_ENABLED:
5122 bar_enabled = atoi(value);
5124 case SWM_S_BAR_BORDER_WIDTH:
5125 bar_border_width = atoi(value);
5127 case SWM_S_BAR_AT_BOTTOM:
5128 bar_at_bottom = atoi(value);
5130 case SWM_S_BAR_JUSTIFY:
5131 if (!strcmp(value, "left"))
5132 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5133 else if (!strcmp(value, "center"))
5134 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5135 else if (!strcmp(value, "right"))
5136 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5138 errx(1, "invalid bar_justify");
5140 case SWM_S_STACK_ENABLED:
5141 stack_enabled = atoi(value);
5143 case SWM_S_CLOCK_ENABLED:
5144 clock_enabled = atoi(value);
5146 case SWM_S_CLOCK_FORMAT:
5147 #ifndef SWM_DENY_CLOCK_FORMAT
5149 if ((clock_format = strdup(value)) == NULL)
5150 err(1, "setconfvalue: clock_format");
5153 case SWM_S_CYCLE_EMPTY:
5154 cycle_empty = atoi(value);
5156 case SWM_S_CYCLE_VISIBLE:
5157 cycle_visible = atoi(value);
5159 case SWM_S_SS_ENABLED:
5160 ss_enabled = atoi(value);
5162 case SWM_S_TERM_WIDTH:
5163 term_width = atoi(value);
5165 case SWM_S_TITLE_CLASS_ENABLED:
5166 title_class_enabled = atoi(value);
5168 case SWM_S_WINDOW_NAME_ENABLED:
5169 window_name_enabled = atoi(value);
5171 case SWM_S_TITLE_NAME_ENABLED:
5172 title_name_enabled = atoi(value);
5174 case SWM_S_URGENT_ENABLED:
5175 urgent_enabled = atoi(value);
5177 case SWM_S_FOCUS_MODE:
5178 if (!strcmp(value, "default"))
5179 focus_mode = SWM_FOCUS_DEFAULT;
5180 else if (!strcmp(value, "follow_cursor"))
5181 focus_mode = SWM_FOCUS_FOLLOW;
5182 else if (!strcmp(value, "synergy"))
5183 focus_mode = SWM_FOCUS_SYNERGY;
5185 errx(1, "focus_mode");
5187 case SWM_S_DISABLE_BORDER:
5188 disable_border = atoi(value);
5190 case SWM_S_BORDER_WIDTH:
5191 border_width = atoi(value);
5193 case SWM_S_BAR_FONT:
5195 if ((bar_fonts[0] = strdup(value)) == NULL)
5196 err(1, "setconfvalue: bar_font");
5198 case SWM_S_BAR_ACTION:
5200 if ((bar_argv[0] = strdup(value)) == NULL)
5201 err(1, "setconfvalue: bar_action");
5203 case SWM_S_SPAWN_TERM:
5204 free(spawn_term[0]);
5205 if ((spawn_term[0] = strdup(value)) == NULL)
5206 err(1, "setconfvalue: spawn_term");
5210 case SWM_S_DIALOG_RATIO:
5211 dialog_ratio = atof(value);
5212 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5215 case SWM_S_VERBOSE_LAYOUT:
5216 verbose_layout = atoi(value);
5217 for (i = 0; layouts[i].l_stack != NULL; i++) {
5219 layouts[i].l_string = fancy_stacker;
5221 layouts[i].l_string = plain_stacker;
5231 setconfmodkey(char *selector, char *value, int flags)
5233 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5234 update_modkey(Mod1Mask);
5235 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5236 update_modkey(Mod2Mask);
5237 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5238 update_modkey(Mod3Mask);
5239 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5240 update_modkey(Mod4Mask);
5247 setconfcolor(char *selector, char *value, int flags)
5249 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5254 setconfregion(char *selector, char *value, int flags)
5256 custom_region(value);
5261 setautorun(char *selector, char *value, int flags)
5271 if (getenv("SWM_STARTED"))
5275 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5276 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5278 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5279 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5282 * This is a little intricate
5284 * If the pid already exists we simply reuse it because it means it was
5285 * used before AND not claimed by manage_window. We get away with
5286 * altering it in the parent after INSERT because this can not be a race
5289 while ((ap = strsep(&sp, " \t")) != NULL) {
5292 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5294 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5295 err(1, "setautorun: realloc");
5296 a.argv[argc - 1] = ap;
5299 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5300 err(1, "setautorun: realloc");
5301 a.argv[argc] = NULL;
5303 if ((pid = fork()) == 0) {
5304 spawn(ws_id, &a, 1);
5313 p = calloc(1, sizeof *p);
5316 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5326 setlayout(char *selector, char *value, int flags)
5328 int ws_id, i, x, mg, ma, si, raise;
5329 int st = SWM_V_STACK;
5331 struct workspace *ws;
5333 if (getenv("SWM_STARTED"))
5337 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5338 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5339 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5340 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5343 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5344 errx(1, "layout: invalid workspace %d", ws_id + 1);
5346 if (!strcasecmp(s, "vertical"))
5348 else if (!strcasecmp(s, "horizontal"))
5350 else if (!strcasecmp(s, "fullscreen"))
5353 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5354 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5357 for (i = 0; i < ScreenCount(display); i++) {
5358 ws = (struct workspace *)&screens[i].ws;
5359 ws[ws_id].cur_layout = &layouts[st];
5361 ws[ws_id].always_raise = raise;
5362 if (st == SWM_MAX_STACK)
5366 for (x = 0; x < abs(mg); x++) {
5367 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5368 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5369 SWM_ARG_ID_MASTERSHRINK);
5373 for (x = 0; x < abs(ma); x++) {
5374 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5375 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5376 SWM_ARG_ID_MASTERDEL);
5380 for (x = 0; x < abs(si); x++) {
5381 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5382 si >= 0 ? SWM_ARG_ID_STACKINC :
5383 SWM_ARG_ID_STACKDEC);
5391 /* config options */
5392 struct config_option {
5394 int (*func)(char*, char*, int);
5397 struct config_option configopt[] = {
5398 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5399 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5400 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5401 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5402 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5403 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5404 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5405 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5406 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5407 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5408 { "keyboard_mapping", setkeymapping, 0 },
5409 { "bind", setconfbinding, 0 },
5410 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5411 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5412 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5413 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5414 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5415 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5416 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5417 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5418 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5419 { "modkey", setconfmodkey, 0 },
5420 { "program", setconfspawn, 0 },
5421 { "quirk", setconfquirk, 0 },
5422 { "region", setconfregion, 0 },
5423 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5424 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5425 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5426 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5427 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5428 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5429 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5430 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5431 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5432 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5433 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5434 { "autorun", setautorun, 0 },
5435 { "layout", setlayout, 0 },
5440 conf_load(char *filename, int keymapping)
5443 char *line, *cp, *optsub, *optval;
5444 size_t linelen, lineno = 0;
5445 int wordlen, i, optind;
5446 struct config_option *opt;
5448 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5450 if (filename == NULL) {
5451 fprintf(stderr, "conf_load: no filename\n");
5454 if ((config = fopen(filename, "r")) == NULL) {
5455 warn("conf_load: fopen: %s", filename);
5459 while (!feof(config)) {
5460 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5463 err(1, "%s", filename);
5468 cp += strspn(cp, " \t\n"); /* eat whitespace */
5469 if (cp[0] == '\0') {
5474 /* get config option */
5475 wordlen = strcspn(cp, "=[ \t\n");
5477 warnx("%s: line %zd: no option found",
5482 for (i = 0; i < LENGTH(configopt); i++) {
5483 opt = &configopt[i];
5484 if (!strncasecmp(cp, opt->optname, wordlen) &&
5485 strlen(opt->optname) == wordlen) {
5491 warnx("%s: line %zd: unknown option %.*s",
5492 filename, lineno, wordlen, cp);
5495 if (keymapping && strcmp(opt->optname, "bind")) {
5496 warnx("%s: line %zd: invalid option %.*s",
5497 filename, lineno, wordlen, cp);
5501 cp += strspn(cp, " \t\n"); /* eat whitespace */
5502 /* get [selector] if any */
5506 wordlen = strcspn(cp, "]");
5509 warnx("%s: line %zd: syntax error",
5514 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5516 warnx("%s: line %zd: unable to allocate"
5517 "memory for selector", filename,
5523 cp += strspn(cp, "] \t\n"); /* eat trailing */
5525 cp += strspn(cp, "= \t\n"); /* eat trailing */
5527 optval = strdup(cp);
5528 /* call function to deal with it all */
5529 if (configopt[optind].func(optsub, optval,
5530 configopt[optind].funcflags) != 0) {
5531 fprintf(stderr, "%s line %zd: %s\n",
5532 filename, lineno, line);
5533 errx(1, "%s: line %zd: invalid data for %s",
5534 filename, lineno, configopt[optind].optname);
5542 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5554 set_child_transient(struct ws_win *win, Window *trans)
5556 struct ws_win *parent, *w;
5557 XWMHints *wmh = NULL;
5558 struct swm_region *r;
5559 struct workspace *ws;
5561 parent = find_window(win->transient);
5563 parent->child_trans = win;
5565 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5566 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
5568 if (win->hints == NULL) {
5569 fprintf(stderr, "no hints for 0x%lx\n", win->id);
5573 r = root_to_region(win->wa.root);
5575 /* parent doen't exist in our window list */
5576 TAILQ_FOREACH(w, &ws->winlist, entry) {
5580 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5581 fprintf(stderr, "can't get hints for 0x%lx\n",
5586 if (win->hints->window_group != wmh->window_group)
5589 w->child_trans = win;
5590 win->transient = w->id;
5592 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5593 "transient to 0x%lx\n", win->transient);
5603 window_get_pid(Window win)
5605 Atom actual_type_return;
5606 int actual_format_return = 0;
5607 unsigned long nitems_return = 0;
5608 unsigned long bytes_after_return = 0;
5612 unsigned char *prop = NULL;
5614 if (XGetWindowProperty(display, win,
5615 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5616 XA_CARDINAL, &actual_type_return, &actual_format_return,
5617 &nitems_return, &bytes_after_return,
5618 (unsigned char**)(void*)&pid) != Success)
5620 if (actual_type_return != XA_CARDINAL)
5631 if (XGetWindowProperty(display, win,
5632 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5633 XA_STRING, &actual_type_return, &actual_format_return,
5634 &nitems_return, &bytes_after_return, &prop) != Success)
5636 if (actual_type_return != XA_STRING)
5641 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5642 /* ignore error because strtonum returns 0 anyway */
5649 manage_window(Window id)
5652 struct workspace *ws;
5653 struct ws_win *win, *ww;
5654 int format, i, ws_idx, n, border_me = 0;
5655 unsigned long nitems, bytes;
5656 Atom ws_idx_atom = 0, type;
5657 Atom *prot = NULL, *pp;
5658 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5659 struct swm_region *r;
5665 if ((win = find_window(id)) != NULL)
5666 return (win); /* already being managed */
5668 /* see if we are on the unmanaged list */
5669 if ((win = find_unmanaged_window(id)) != NULL) {
5670 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
5671 "window: 0x%lx\n", win->id);
5672 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5673 if (win->transient) {
5674 set_child_transient(win, &trans);
5675 } if (trans && (ww = find_window(trans)))
5676 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5678 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5679 ewmh_update_actions(win);
5683 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5684 err(1, "manage_window: calloc: failed to allocate memory for "
5689 /* see if we need to override the workspace */
5690 p = find_pid(window_get_pid(id));
5692 /* Get all the window data in one shot */
5693 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5695 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5696 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5698 XGetWindowAttributes(display, id, &win->wa);
5699 XGetWMNormalHints(display, id, &win->sh, &mask);
5700 win->hints = XGetWMHints(display, id);
5701 XGetTransientForHint(display, id, &trans);
5703 win->transient = trans;
5704 set_child_transient(win, &trans);
5705 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
5706 "transient: 0x%lx\n", win->id, win->transient);
5709 /* get supported protocols */
5710 if (XGetWMProtocols(display, id, &prot, &n)) {
5711 for (i = 0, pp = prot; i < n; i++, pp++) {
5712 if (*pp == takefocus)
5713 win->take_focus = 1;
5715 win->can_delete = 1;
5721 win->iconic = get_iconic(win);
5724 * Figure out where to put the window. If it was previously assigned to
5725 * a workspace (either by spawn() or manually moving), and isn't
5726 * transient, * put it in the same workspace
5728 r = root_to_region(win->wa.root);
5730 ws = &r->s->ws[p->ws];
5731 TAILQ_REMOVE(&pidlist, p, entry);
5734 } else if (prop && win->transient == 0) {
5735 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
5736 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5738 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
5741 ws = &r->s->ws[ws_idx];
5744 /* this should launch transients in the same ws as parent */
5746 if ((ww = find_window(trans)) != NULL)
5753 "manage_window: fix this "
5759 /* set up the window layout */
5762 win->s = r->s; /* this never changes */
5763 if (trans && (ww = find_window(trans)))
5764 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5766 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5768 WIDTH(win) = win->wa.width;
5769 HEIGHT(win) = win->wa.height;
5772 win->g_floatvalid = 0;
5773 win->floatmaxed = 0;
5774 win->ewmh_flags = 0;
5776 /* Set window properties so we can remember this after reincarnation */
5777 if (ws_idx_atom && prop == NULL &&
5778 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5780 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
5782 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5783 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5788 ewmh_autoquirk(win);
5790 if (XGetClassHint(display, win->id, &win->ch)) {
5791 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
5792 win->ch.res_class, win->ch.res_name);
5794 /* java is retarded so treat it special */
5795 if (strstr(win->ch.res_name, "sun-awt")) {
5800 for (i = 0; i < quirks_length; i++){
5801 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5802 !strcmp(win->ch.res_name, quirks[i].name)) {
5803 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
5804 "class: %s, name: %s\n", win->ch.res_class,
5806 if (quirks[i].quirk & SWM_Q_FLOAT) {
5810 win->quirks = quirks[i].quirk;
5815 /* alter window position if quirky */
5816 if (win->quirks & SWM_Q_ANYWHERE) {
5817 win->manual = 1; /* don't center the quirky windows */
5818 bzero(&wc, sizeof wc);
5820 if (bar_enabled && Y(win) < bar_height) {
5821 Y(win) = wc.y = bar_height;
5824 if (WIDTH(win) + X(win) > WIDTH(r)) {
5825 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
5831 /* Reset font sizes (the bruteforce way; no default keybinding). */
5832 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5833 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5834 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5835 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5836 fake_keypress(win, XK_KP_Add, ShiftMask);
5839 ewmh_get_win_state(win);
5840 ewmh_update_actions(win);
5841 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5845 bzero(&wc, sizeof wc);
5846 wc.border_width = border_width;
5847 mask = CWBorderWidth;
5848 XConfigureWindow(display, win->id, mask, &wc);
5851 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5852 PropertyChangeMask | StructureNotifyMask);
5854 /* floaters need to be mapped if they are in the current workspace */
5855 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5856 XMapRaised(display, win->id);
5862 free_window(struct ws_win *win)
5864 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
5869 /* needed for restart wm */
5870 set_win_state(win, WithdrawnState);
5872 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5874 if (win->ch.res_class)
5875 XFree(win->ch.res_class);
5876 if (win->ch.res_name)
5877 XFree(win->ch.res_name);
5882 memset(win, 0xff, sizeof *win); /* XXX kill later */
5888 unmanage_window(struct ws_win *win)
5890 struct ws_win *parent;
5895 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
5897 if (win->transient) {
5898 parent = find_window(win->transient);
5900 parent->child_trans = NULL;
5903 /* focus on root just in case */
5904 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5908 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5909 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5915 focus_magic(struct ws_win *win)
5917 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
5922 if (win->child_trans) {
5923 /* win = parent & has a transient so focus on that */
5925 focus_win(win->child_trans);
5926 if (win->child_trans->take_focus)
5927 client_msg(win, takefocus);
5929 /* make sure transient hasn't dissapeared */
5930 if (validate_win(win->child_trans) == 0) {
5931 focus_win(win->child_trans);
5932 if (win->child_trans->take_focus)
5933 client_msg(win->child_trans, takefocus);
5935 win->child_trans = NULL;
5937 if (win->take_focus)
5938 client_msg(win, takefocus);
5944 if (win->take_focus)
5945 client_msg(win, takefocus);
5952 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
5960 XKeyEvent *ev = &e->xkey;
5962 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5963 for (i = 0; i < keys_length; i++)
5964 if (keysym == keys[i].keysym
5965 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5966 && keyfuncs[keys[i].funcid].func) {
5967 if (keys[i].funcid == kf_spawn_custom)
5969 root_to_region(ev->root),
5970 &(keyfuncs[keys[i].funcid].args),
5974 keyfuncs[keys[i].funcid].func(
5975 root_to_region(ev->root),
5976 &(keyfuncs[keys[i].funcid].args)
5982 buttonpress(XEvent *e)
5986 XButtonPressedEvent *ev = &e->xbutton;
5988 if ((win = find_window(ev->window)) == NULL)
5992 action = client_click;
5994 for (i = 0; i < LENGTH(buttons); i++)
5995 if (action == buttons[i].action && buttons[i].func &&
5996 buttons[i].button == ev->button &&
5997 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5998 buttons[i].func(win, &buttons[i].args);
6002 configurerequest(XEvent *e)
6004 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6009 if ((win = find_window(ev->window)) == NULL)
6010 if ((win = find_unmanaged_window(ev->window)) == NULL)
6013 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6014 ev->window, YESNO(new));
6017 bzero(&wc, sizeof wc);
6020 wc.width = ev->width;
6021 wc.height = ev->height;
6022 wc.border_width = ev->border_width;
6023 wc.sibling = ev->above;
6024 wc.stack_mode = ev->detail;
6025 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6027 config_win(win, ev);
6032 configurenotify(XEvent *e)
6037 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6038 e->xconfigure.window);
6040 win = find_window(e->xconfigure.window);
6042 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6046 if (focus_mode == SWM_FOCUS_DEFAULT)
6047 drain_enter_notify();
6052 destroynotify(XEvent *e)
6055 XDestroyWindowEvent *ev = &e->xdestroywindow;
6057 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6059 if ((win = find_window(ev->window)) == NULL) {
6060 if ((win = find_unmanaged_window(ev->window)) == NULL)
6066 /* make sure we focus on something */
6069 unmanage_window(win);
6071 if (focus_mode == SWM_FOCUS_DEFAULT)
6072 drain_enter_notify();
6077 enternotify(XEvent *e)
6079 XCrossingEvent *ev = &e->xcrossing;
6084 Window focus_return;
6085 int revert_to_return;
6087 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6088 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6089 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6090 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6092 if (ev->mode != NotifyNormal) {
6093 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6098 switch (focus_mode) {
6099 case SWM_FOCUS_DEFAULT:
6101 case SWM_FOCUS_FOLLOW:
6103 case SWM_FOCUS_SYNERGY:
6106 * all these checks need to be in this order because the
6107 * XCheckTypedWindowEvent relies on weeding out the previous events
6109 * making this code an option would enable a follow mouse for focus
6114 * state is set when we are switching workspaces and focus is set when
6115 * the window or a subwindow already has focus (occurs during restart).
6117 * Only honor the focus flag if last_focus_event is not FocusOut,
6118 * this allows scrotwm to continue to control focus when another
6119 * program is also playing with it.
6121 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6122 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6127 * happens when a window is created or destroyed and the border
6128 * crosses the mouse pointer and when switching ws
6130 * we need the subwindow test to see if we came from root in order
6131 * to give focus to floaters
6133 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6134 ev->subwindow == 0) {
6135 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6139 /* this window already has focus */
6140 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6141 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6145 /* this window is being deleted or moved to another ws */
6146 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6148 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6149 XPutBackEvent(display, &cne);
6153 if ((win = find_window(ev->window)) == NULL) {
6154 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6159 * In fullstack kill all enters unless they come from a different ws
6160 * (i.e. another region) or focus has been grabbed externally.
6162 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6163 last_focus_event != FocusOut) {
6164 XGetInputFocus(display, &focus_return, &revert_to_return);
6165 if ((w = find_window(focus_return)) == NULL ||
6167 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6175 if ((win = find_window(ev->window)) == NULL) {
6176 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6181 * if we have more enternotifies let them handle it in due time
6183 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6184 DNPRINTF(SWM_D_EVENT,
6185 "ignoring enternotify: got more enternotify\n");
6186 XPutBackEvent(display, &cne);
6193 /* lets us use one switch statement for arbitrary mode/detail combinations */
6194 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6197 focusevent(XEvent *e)
6201 u_int32_t mode_detail;
6202 XFocusChangeEvent *ev = &e->xfocus;
6204 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6205 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6206 ev->window, ev->mode, ev->detail);
6208 if (last_focus_event == ev->type) {
6209 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6213 last_focus_event = ev->type;
6214 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6216 switch (mode_detail) {
6217 /* synergy client focus operations */
6218 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6219 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6221 /* synergy server focus operations */
6222 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6224 /* Entering applications like rdesktop that mangle the pointer */
6225 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6227 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6228 XSetWindowBorder(display, win->id,
6229 win->ws->r->s->c[ev->type == FocusIn ?
6230 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6233 fprintf(stderr, "ignoring focusevent\n");
6234 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6241 mapnotify(XEvent *e)
6244 XMapEvent *ev = &e->xmap;
6246 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6248 win = manage_window(ev->window);
6250 set_win_state(win, NormalState);
6254 mappingnotify(XEvent *e)
6256 XMappingEvent *ev = &e->xmapping;
6258 XRefreshKeyboardMapping(ev);
6259 if (ev->request == MappingKeyboard)
6264 maprequest(XEvent *e)
6267 struct swm_region *r;
6268 XWindowAttributes wa;
6269 XMapRequestEvent *ev = &e->xmaprequest;
6271 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6272 e->xmaprequest.window);
6274 if (!XGetWindowAttributes(display, ev->window, &wa))
6276 if (wa.override_redirect)
6279 win = manage_window(e->xmaprequest.window);
6281 return; /* can't happen */
6285 /* make new win focused */
6286 r = root_to_region(win->wa.root);
6287 if (win->ws == r->ws)
6292 propertynotify(XEvent *e)
6295 XPropertyEvent *ev = &e->xproperty;
6297 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6298 ev->window, XGetAtomName(display, ev->atom));
6300 win = find_window(ev->window);
6304 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6305 update_iconic(win, 0);
6306 XMapRaised(display, win->id);
6314 case XA_WM_NORMAL_HINTS:
6316 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6317 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6318 if (win->sh.flags & PMinSize) {
6319 WIDTH(win) = win->sh.min_width;
6320 HEIGHT(win) = win->sh.min_height;
6321 fprintf(stderr, "min %d %d\n", WIDTH(win), HEIGHT(win));
6323 XMoveResizeWindow(display, win->id,
6324 X(win), Y(win), WIDTH(win), HEIGHT(win));
6327 if (title_name_enabled || title_class_enabled)
6331 if (window_name_enabled)
6340 unmapnotify(XEvent *e)
6344 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6346 /* determine if we need to help unmanage this window */
6347 win = find_window(e->xunmap.window);
6351 if (getstate(e->xunmap.window) == NormalState) {
6352 unmanage_window(win);
6355 /* giant hack for apps that don't destroy transient windows */
6356 /* eat a bunch of events to prevent remanaging the window */
6358 while (XCheckWindowEvent(display, e->xunmap.window,
6359 EnterWindowMask, &cne))
6361 while (XCheckWindowEvent(display, e->xunmap.window,
6362 StructureNotifyMask, &cne))
6364 while (XCheckWindowEvent(display, e->xunmap.window,
6365 SubstructureNotifyMask, &cne))
6367 /* resend unmap because we ated it */
6368 XUnmapWindow(display, e->xunmap.window);
6371 if (focus_mode == SWM_FOCUS_DEFAULT)
6372 drain_enter_notify();
6376 visibilitynotify(XEvent *e)
6379 struct swm_region *r;
6381 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6382 e->xvisibility.window);
6383 if (e->xvisibility.state == VisibilityUnobscured)
6384 for (i = 0; i < ScreenCount(display); i++)
6385 TAILQ_FOREACH(r, &screens[i].rl, entry)
6386 if (e->xvisibility.window == r->bar_window)
6391 clientmessage(XEvent *e)
6393 XClientMessageEvent *ev;
6398 win = find_window(ev->window);
6402 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6403 ev->window, ev->message_type);
6405 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6406 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6409 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6410 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6411 if (win->can_delete)
6412 client_msg(win, adelete);
6414 XKillClient(display, win->id);
6416 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6417 DNPRINTF(SWM_D_EVENT,
6418 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6419 if (win->floating) {
6420 if (ev->data.l[0] & (1<<8)) /* x */
6421 X(win) = ev->data.l[1];
6422 if (ev->data.l[0] & (1<<9)) /* y */
6423 Y(win) = ev->data.l[2];
6424 if (ev->data.l[0] & (1<<10)) /* width */
6425 WIDTH(win) = ev->data.l[3];
6426 if (ev->data.l[0] & (1<<11)) /* height */
6427 HEIGHT(win) = ev->data.l[4];
6432 /* TODO: Change stack sizes */
6433 /* notify no change was made. */
6434 config_win(win, NULL);
6437 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6438 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6439 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6441 ewmh_update_win_state(win, ev->data.l[2],
6449 xerror_start(Display *d, XErrorEvent *ee)
6456 xerror(Display *d, XErrorEvent *ee)
6458 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6466 xerrorxlib = XSetErrorHandler(xerror_start);
6468 /* this causes an error if some other window manager is running */
6469 XSelectInput(display, DefaultRootWindow(display),
6470 SubstructureRedirectMask);
6471 XSync(display, False);
6475 XSetErrorHandler(xerror);
6476 XSync(display, False);
6481 new_region(struct swm_screen *s, int x, int y, int w, int h)
6483 struct swm_region *r, *n;
6484 struct workspace *ws = NULL;
6487 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6488 s->idx, w, h, x, y);
6490 /* remove any conflicting regions */
6491 n = TAILQ_FIRST(&s->rl);
6494 n = TAILQ_NEXT(r, entry);
6495 if (X(r) < (x + w) &&
6496 (X(r) + WIDTH(r)) > x &&
6498 (Y(r) + HEIGHT(r)) > y) {
6499 if (r->ws->r != NULL)
6500 r->ws->old_r = r->ws->r;
6502 XDestroyWindow(display, r->bar_window);
6503 TAILQ_REMOVE(&s->rl, r, entry);
6504 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6508 /* search old regions for one to reuse */
6510 /* size + location match */
6511 TAILQ_FOREACH(r, &s->orl, entry)
6512 if (X(r) == x && Y(r) == y &&
6513 HEIGHT(r) == h && WIDTH(r) == w)
6517 TAILQ_FOREACH(r, &s->orl, entry)
6518 if (HEIGHT(r) == h && WIDTH(r) == w)
6522 TAILQ_REMOVE(&s->orl, r, entry);
6523 /* try to use old region's workspace */
6524 if (r->ws->r == NULL)
6527 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6528 err(1, "new_region: calloc: failed to allocate memory "
6531 /* if we don't have a workspace already, find one */
6533 for (i = 0; i < SWM_WS_MAX; i++)
6534 if (s->ws[i].r == NULL) {
6541 errx(1, "new_region: no free workspaces");
6552 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6558 #ifdef SWM_XRR_HAS_CRTC
6560 XRRScreenResources *sr;
6563 #endif /* SWM_XRR_HAS_CRTC */
6564 struct swm_region *r;
6567 if (i >= ScreenCount(display))
6568 errx(1, "scan_xrandr: invalid screen");
6570 /* remove any old regions */
6571 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6572 r->ws->old_r = r->ws->r = NULL;
6573 XDestroyWindow(display, r->bar_window);
6574 TAILQ_REMOVE(&screens[i].rl, r, entry);
6575 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6579 /* map virtual screens onto physical screens */
6580 #ifdef SWM_XRR_HAS_CRTC
6581 if (xrandr_support) {
6582 sr = XRRGetScreenResources(display, screens[i].root);
6584 new_region(&screens[i], 0, 0,
6585 DisplayWidth(display, i),
6586 DisplayHeight(display, i));
6590 for (c = 0, ci = NULL; c < ncrtc; c++) {
6591 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6592 if (ci->noutput == 0)
6595 if (ci != NULL && ci->mode == None)
6596 new_region(&screens[i], 0, 0,
6597 DisplayWidth(display, i),
6598 DisplayHeight(display, i));
6600 new_region(&screens[i],
6601 ci->x, ci->y, ci->width, ci->height);
6604 XRRFreeCrtcInfo(ci);
6605 XRRFreeScreenResources(sr);
6607 #endif /* SWM_XRR_HAS_CRTC */
6609 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6610 DisplayHeight(display, i));
6615 screenchange(XEvent *e) {
6616 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6617 struct swm_region *r;
6620 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
6622 if (!XRRUpdateConfiguration(e))
6625 /* silly event doesn't include the screen index */
6626 for (i = 0; i < ScreenCount(display); i++)
6627 if (screens[i].root == xe->root)
6629 if (i >= ScreenCount(display))
6630 errx(1, "screenchange: screen not found");
6632 /* brute force for now, just re-enumerate the regions */
6635 /* add bars to all regions */
6636 for (i = 0; i < ScreenCount(display); i++)
6637 TAILQ_FOREACH(r, &screens[i].rl, entry)
6640 if (focus_mode == SWM_FOCUS_DEFAULT)
6641 drain_enter_notify();
6647 Window d1, d2, *wins = NULL;
6648 XWindowAttributes wa;
6653 for (i = 0; i < ScreenCount(display); i++) {
6654 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6657 /* attach windows to a region */
6658 /* normal windows */
6659 for (j = 0; j < no; j++) {
6660 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6661 wa.override_redirect ||
6662 XGetTransientForHint(display, wins[j], &d1))
6665 state = getstate(wins[j]);
6666 manage = state == IconicState;
6667 if (wa.map_state == IsViewable || manage)
6668 manage_window(wins[j]);
6670 /* transient windows */
6671 for (j = 0; j < no; j++) {
6672 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6673 wa.override_redirect)
6676 state = getstate(wins[j]);
6677 manage = state == IconicState;
6678 if (XGetTransientForHint(display, wins[j], &d1) &&
6680 manage_window(wins[j]);
6693 int errorbase, major, minor;
6694 struct workspace *ws;
6696 if ((screens = calloc(ScreenCount(display),
6697 sizeof(struct swm_screen))) == NULL)
6698 err(1, "setup_screens: calloc: failed to allocate memory for "
6701 /* initial Xrandr setup */
6702 xrandr_support = XRRQueryExtension(display,
6703 &xrandr_eventbase, &errorbase);
6705 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6708 /* map physical screens */
6709 for (i = 0; i < ScreenCount(display); i++) {
6710 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
6712 TAILQ_INIT(&screens[i].rl);
6713 TAILQ_INIT(&screens[i].orl);
6714 screens[i].root = RootWindow(display, i);
6716 /* set default colors */
6717 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6718 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6719 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6720 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6721 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6723 /* set default cursor */
6724 XDefineCursor(display, screens[i].root,
6725 XCreateFontCursor(display, XC_left_ptr));
6727 /* init all workspaces */
6728 /* XXX these should be dynamically allocated too */
6729 for (j = 0; j < SWM_WS_MAX; j++) {
6730 ws = &screens[i].ws[j];
6736 TAILQ_INIT(&ws->winlist);
6737 TAILQ_INIT(&ws->unmanagedlist);
6739 for (k = 0; layouts[k].l_stack != NULL; k++)
6740 if (layouts[k].l_config != NULL)
6741 layouts[k].l_config(ws,
6742 SWM_ARG_ID_STACKINIT);
6743 ws->cur_layout = &layouts[0];
6744 ws->cur_layout->l_string(ws);
6750 XRRSelectInput(display, screens[i].root,
6751 RRScreenChangeNotifyMask);
6758 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6760 err(1, "setup_globals: strdup");
6761 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6763 err(1, "setup_globals: strdup");
6764 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6766 err(1, "setup_globals: strdup");
6767 if ((spawn_term[0] = strdup("xterm")) == NULL)
6768 err(1, "setup_globals: strdup");
6769 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6777 Atom netwmcheck, netwmname, utf8_string;
6780 /* work around sun jdk bugs, code from wmname */
6781 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6782 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6783 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6784 for (i = 0; i < ScreenCount(display); i++) {
6785 root = screens[i].root;
6786 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6787 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6788 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6790 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6791 PropModeReplace, (unsigned char *)&win,1);
6792 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6793 PropModeReplace, (unsigned char *)&win,1);
6794 XChangeProperty(display, win, netwmname, utf8_string, 8,
6795 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6800 main(int argc, char *argv[])
6802 struct swm_region *r, *rr;
6803 struct ws_win *winfocus = NULL;
6806 char conf[PATH_MAX], *cfile = NULL;
6811 struct sigaction sact;
6814 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6815 SCROTWM_VERSION, buildstr);
6816 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6818 warnx("no locale support");
6820 if (!(display = XOpenDisplay(0)))
6821 errx(1, "can not open display");
6824 errx(1, "other wm running");
6826 /* handle some signals */
6827 bzero(&sact, sizeof(sact));
6828 sigemptyset(&sact.sa_mask);
6830 sact.sa_handler = sighdlr;
6831 sigaction(SIGINT, &sact, NULL);
6832 sigaction(SIGQUIT, &sact, NULL);
6833 sigaction(SIGTERM, &sact, NULL);
6834 sigaction(SIGHUP, &sact, NULL);
6836 sact.sa_handler = sighdlr;
6837 sact.sa_flags = SA_NOCLDSTOP;
6838 sigaction(SIGCHLD, &sact, NULL);
6840 astate = XInternAtom(display, "WM_STATE", False);
6841 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6842 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6843 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6844 a_wmname = XInternAtom(display, "WM_NAME", False);
6845 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6846 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6847 a_string = XInternAtom(display, "STRING", False);
6848 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6850 /* look for local and global conf file */
6851 pwd = getpwuid(getuid());
6853 errx(1, "invalid user: %d", getuid());
6862 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6863 if (stat(conf, &sb) != -1) {
6864 if (S_ISREG(sb.st_mode))
6867 /* try global conf file */
6868 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6869 if (!stat(conf, &sb))
6870 if (S_ISREG(sb.st_mode))
6874 conf_load(cfile, SWM_CONF_DEFAULT);
6877 /* set some values to work around bad programs */
6879 /* grab existing windows (before we build the bars) */
6882 if (getenv("SWM_STARTED") == NULL)
6883 setenv("SWM_STARTED", "YES", 1);
6885 /* setup all bars */
6886 for (i = 0; i < ScreenCount(display); i++)
6887 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6888 if (winfocus == NULL)
6889 winfocus = TAILQ_FIRST(&r->ws->winlist);
6897 if (focus_mode == SWM_FOCUS_DEFAULT)
6898 drain_enter_notify();
6900 xfd = ConnectionNumber(display);
6902 while (XPending(display)) {
6903 XNextEvent(display, &e);
6906 if (e.type < LASTEvent) {
6907 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
6908 "window: 0x%lx, type: %s (%d), %d remaining"
6909 "\n", YESNO(handler[e.type]),
6910 e.xany.window, geteventname(&e),
6911 e.type, QLength(display));
6913 if (handler[e.type])
6914 handler[e.type](&e);
6916 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
6917 "0x%lx, type: %s (%d)\n", e.xany.window,
6918 xrandr_geteventname(&e), e.type);
6920 switch (e.type - xrandr_eventbase) {
6921 case RRScreenChangeNotify:
6930 /* if we are being restarted go focus on first window */
6932 rr = winfocus->ws->r;
6934 /* not a visible window */
6938 /* move pointer to first screen if multi screen */
6939 if (ScreenCount(display) > 1 || outputs > 1)
6940 XWarpPointer(display, None, rr->s[0].root,
6942 Y(rr) + (bar_enabled ? bar_height : 0));
6944 a.id = SWM_ARG_ID_FOCUSCUR;
6954 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6956 DNPRINTF(SWM_D_MISC, "select failed");
6957 if (restart_wm == 1)
6958 restart(NULL, NULL);
6959 if (search_resp == 1)
6971 XFreeGC(display, bar_gc);
6972 XCloseDisplay(display);