2 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
84 #include <X11/extensions/XTest.h>
92 #ifdef SCROTWM_BUILDSTR
93 static const char *buildstr = SCROTWM_BUILDSTR;
95 static const char *buildstr = SCROTWM_VERSION;
99 # error XRandR versions less than 1.0 are not supported
104 #define SWM_XRR_HAS_CRTC
108 /*#define SWM_DEBUG*/
110 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
111 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
112 #define SWM_D_MISC 0x0001
113 #define SWM_D_EVENT 0x0002
114 #define SWM_D_WS 0x0004
115 #define SWM_D_FOCUS 0x0008
116 #define SWM_D_MOVE 0x0010
117 #define SWM_D_STACK 0x0020
118 #define SWM_D_MOUSE 0x0040
119 #define SWM_D_PROP 0x0080
120 #define SWM_D_CLASS 0x0100
121 #define SWM_D_KEY 0x0200
122 #define SWM_D_QUIRK 0x0400
123 #define SWM_D_SPAWN 0x0800
124 #define SWM_D_EVENTQ 0x1000
125 #define SWM_D_CONF 0x2000
127 u_int32_t swm_debug = 0
144 #define DPRINTF(x...)
145 #define DNPRINTF(n,x...)
148 #define LENGTH(x) (sizeof x / sizeof x[0])
149 #define MODKEY Mod1Mask
150 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
151 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
152 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
153 #define SWM_PROPLEN (16)
154 #define SWM_FUNCNAME_LEN (32)
155 #define SWM_KEYS_LEN (255)
156 #define SWM_QUIRK_LEN (64)
157 #define X(r) (r)->g.x
158 #define Y(r) (r)->g.y
159 #define WIDTH(r) (r)->g.w
160 #define HEIGHT(r) (r)->g.h
161 #define SWM_MAX_FONT_STEPS (3)
162 #define WINID(w) (w ? w->id : 0)
164 #define SWM_FOCUS_DEFAULT (0)
165 #define SWM_FOCUS_SYNERGY (1)
166 #define SWM_FOCUS_FOLLOW (2)
168 #define SWM_CONF_DEFAULT (0)
169 #define SWM_CONF_KEYMAPPING (1)
172 #define SWM_LIB "/usr/local/lib/libswmhack.so"
184 volatile sig_atomic_t running = 1;
185 volatile sig_atomic_t restart_wm = 0;
187 int last_focus_event = FocusOut;
188 int (*xerrorxlib)(Display *, XErrorEvent *);
192 int xrandr_eventbase;
193 unsigned int numlockmask = 0;
197 int cycle_visible = 0;
199 int font_adjusted = 0;
200 unsigned int mod_key = MODKEY;
203 struct swm_region *search_r;
204 int select_list_pipe[2];
205 int select_resp_pipe[2];
207 volatile sig_atomic_t search_resp;
208 int search_resp_action;
210 struct search_window {
211 TAILQ_ENTRY(search_window) entry;
216 TAILQ_HEAD(search_winlist, search_window);
218 struct search_winlist search_wl;
223 SWM_SEARCH_UNICONIFY,
224 SWM_SEARCH_NAME_WORKSPACE,
225 SWM_SEARCH_SEARCH_WORKSPACE,
226 SWM_SEARCH_SEARCH_WINDOW
230 double dialog_ratio = .6;
232 #define SWM_BAR_MAX (256)
233 #define SWM_BAR_JUSTIFY_LEFT (0)
234 #define SWM_BAR_JUSTIFY_CENTER (1)
235 #define SWM_BAR_JUSTIFY_RIGHT (2)
236 #define SWM_BAR_OFFSET (4)
237 char *bar_argv[] = { NULL, NULL };
239 char bar_ext[SWM_BAR_MAX];
240 char bar_vertext[SWM_BAR_MAX];
242 sig_atomic_t bar_alarm = 0;
245 int bar_border_width = 1;
246 int bar_at_bottom = 0;
248 int bar_extra_running = 0;
251 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
252 int stack_enabled = 1;
253 int clock_enabled = 1;
254 int urgent_enabled = 0;
255 char *clock_format = NULL;
256 int title_name_enabled = 0;
257 int title_class_enabled = 0;
258 int window_name_enabled = 0;
259 int focus_mode = SWM_FOCUS_DEFAULT;
260 int disable_border = 0;
261 int border_width = 1;
262 int verbose_layout = 0;
268 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
269 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
272 #define SWM_MENU_FN (2)
273 #define SWM_MENU_NB (4)
274 #define SWM_MENU_NF (6)
275 #define SWM_MENU_SB (8)
276 #define SWM_MENU_SF (10)
278 /* layout manager data */
279 struct swm_geometry {
289 /* virtual "screens" */
291 TAILQ_ENTRY(swm_region) entry;
292 struct swm_geometry g;
293 struct workspace *ws; /* current workspace on this region */
294 struct workspace *ws_prior; /* prior workspace on this region */
295 struct swm_screen *s; /* screen idx */
298 TAILQ_HEAD(swm_region_list, swm_region);
301 TAILQ_ENTRY(ws_win) entry;
304 struct ws_win *child_trans; /* transient child window */
305 struct swm_geometry g; /* current geometry */
306 struct swm_geometry g_float; /* geometry when floating */
307 struct swm_geometry rg_float; /* region geom when floating */
308 int g_floatvalid; /* flag: geometry in g_float is valid */
309 int floatmaxed; /* flag: floater was maxed in max_stack */
313 unsigned int ewmh_flags;
314 int font_size_boundary[SWM_MAX_FONT_STEPS];
320 unsigned long quirks;
321 struct workspace *ws; /* always valid */
322 struct swm_screen *s; /* always valid, never changes */
323 XWindowAttributes wa;
328 TAILQ_HEAD(ws_win_list, ws_win);
332 TAILQ_ENTRY(pid_e) entry;
336 TAILQ_HEAD(pid_list, pid_e);
337 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
339 /* layout handlers */
341 void vertical_config(struct workspace *, int);
342 void vertical_stack(struct workspace *, struct swm_geometry *);
343 void horizontal_config(struct workspace *, int);
344 void horizontal_stack(struct workspace *, struct swm_geometry *);
345 void max_stack(struct workspace *, struct swm_geometry *);
346 void plain_stacker(struct workspace *);
347 void fancy_stacker(struct workspace *);
349 struct ws_win *find_window(Window);
351 void grabbuttons(struct ws_win *, int);
352 void new_region(struct swm_screen *, int, int, int, int);
353 void unmanage_window(struct ws_win *);
354 long getstate(Window);
356 int conf_load(char *, int);
359 void (*l_stack)(struct workspace *, struct swm_geometry *);
360 void (*l_config)(struct workspace *, int);
362 #define SWM_L_FOCUSPREV (1<<0)
363 #define SWM_L_MAPONFOCUS (1<<1)
364 void (*l_string)(struct workspace *);
366 /* stack, configure */
367 { vertical_stack, vertical_config, 0, plain_stacker },
368 { horizontal_stack, horizontal_config, 0, plain_stacker },
370 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
371 { NULL, NULL, 0, NULL },
374 /* position of max_stack mode in the layouts array, index into layouts! */
375 #define SWM_V_STACK (0)
376 #define SWM_H_STACK (1)
377 #define SWM_MAX_STACK (2)
379 #define SWM_H_SLICE (32)
380 #define SWM_V_SLICE (32)
382 /* define work spaces */
384 int idx; /* workspace index */
385 char *name; /* workspace name */
386 int always_raise; /* raise windows on focus */
387 struct layout *cur_layout; /* current layout handlers */
388 struct ws_win *focus; /* may be NULL */
389 struct ws_win *focus_prev; /* may be NULL */
390 struct swm_region *r; /* may be NULL */
391 struct swm_region *old_r; /* may be NULL */
392 struct ws_win_list winlist; /* list of windows in ws */
393 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
394 char stacker[10]; /* display stacker and layout */
398 int horizontal_msize;
400 int horizontal_stacks;
407 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
408 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
410 /* physical screen mapping */
411 #define SWM_WS_MAX (10)
413 int idx; /* screen index */
414 struct swm_region_list rl; /* list of regions on this screen */
415 struct swm_region_list orl; /* list of old regions */
417 struct workspace ws[SWM_WS_MAX];
423 } c[SWM_S_COLOR_MAX];
425 struct swm_screen *screens;
428 /* args to functions */
431 #define SWM_ARG_ID_FOCUSNEXT (0)
432 #define SWM_ARG_ID_FOCUSPREV (1)
433 #define SWM_ARG_ID_FOCUSMAIN (2)
434 #define SWM_ARG_ID_FOCUSCUR (4)
435 #define SWM_ARG_ID_SWAPNEXT (10)
436 #define SWM_ARG_ID_SWAPPREV (11)
437 #define SWM_ARG_ID_SWAPMAIN (12)
438 #define SWM_ARG_ID_MOVELAST (13)
439 #define SWM_ARG_ID_MASTERSHRINK (20)
440 #define SWM_ARG_ID_MASTERGROW (21)
441 #define SWM_ARG_ID_MASTERADD (22)
442 #define SWM_ARG_ID_MASTERDEL (23)
443 #define SWM_ARG_ID_STACKRESET (30)
444 #define SWM_ARG_ID_STACKINIT (31)
445 #define SWM_ARG_ID_CYCLEWS_UP (40)
446 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
447 #define SWM_ARG_ID_CYCLESC_UP (42)
448 #define SWM_ARG_ID_CYCLESC_DOWN (43)
449 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
450 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
451 #define SWM_ARG_ID_STACKINC (50)
452 #define SWM_ARG_ID_STACKDEC (51)
453 #define SWM_ARG_ID_SS_ALL (60)
454 #define SWM_ARG_ID_SS_WINDOW (61)
455 #define SWM_ARG_ID_DONTCENTER (70)
456 #define SWM_ARG_ID_CENTER (71)
457 #define SWM_ARG_ID_KILLWINDOW (80)
458 #define SWM_ARG_ID_DELETEWINDOW (81)
459 #define SWM_ARG_ID_WIDTHGROW (90)
460 #define SWM_ARG_ID_WIDTHSHRINK (91)
461 #define SWM_ARG_ID_HEIGHTGROW (92)
462 #define SWM_ARG_ID_HEIGHTSHRINK (93)
463 #define SWM_ARG_ID_MOVEUP (100)
464 #define SWM_ARG_ID_MOVEDOWN (101)
465 #define SWM_ARG_ID_MOVELEFT (102)
466 #define SWM_ARG_ID_MOVERIGHT (103)
470 void focus(struct swm_region *, union arg *);
471 void focus_magic(struct ws_win *);
478 #define SWM_Q_FLOAT (1<<0) /* float this window */
479 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
480 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
481 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
482 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
483 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
485 int quirks_size = 0, quirks_length = 0;
486 struct quirk *quirks = NULL;
489 * Supported EWMH hints should be added to
490 * both the enum and the ewmh array
492 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
493 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
494 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
495 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
496 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
497 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
498 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
499 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
500 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
501 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
507 } ewmh[SWM_EWMH_HINT_MAX] = {
508 /* must be in same order as in the enum */
509 {"_NET_ACTIVE_WINDOW", None},
510 {"_NET_MOVERESIZE_WINDOW", None},
511 {"_NET_CLOSE_WINDOW", None},
512 {"_NET_WM_WINDOW_TYPE", None},
513 {"_NET_WM_WINDOW_TYPE_DOCK", None},
514 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
515 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
516 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
517 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
518 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
519 {"_NET_WM_STATE", None},
520 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
521 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
522 {"_NET_WM_STATE_SKIP_TASKBAR", None},
523 {"_NET_WM_STATE_SKIP_PAGER", None},
524 {"_NET_WM_STATE_HIDDEN", None},
525 {"_NET_WM_STATE_ABOVE", None},
526 {"_SWM_WM_STATE_MANUAL", None},
527 {"_NET_WM_STATE_FULLSCREEN", None},
528 {"_NET_WM_ALLOWED_ACTIONS", None},
529 {"_NET_WM_ACTION_MOVE", None},
530 {"_NET_WM_ACTION_RESIZE", None},
531 {"_NET_WM_ACTION_FULLSCREEN", None},
532 {"_NET_WM_ACTION_CLOSE", None},
535 void store_float_geom(struct ws_win *win, struct swm_region *r);
536 int floating_toggle_win(struct ws_win *win);
537 void spawn_select(struct swm_region *, union arg *, char *, int *);
540 get_property(Window id, Atom atom, long count, Atom type,
541 unsigned long *n, unsigned char **data)
544 unsigned long tmp, extra;
545 unsigned long *nitems;
548 nitems = n != NULL ? n : &tmp;
549 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
550 &real, &format, nitems, &extra, data);
552 if (status != Success)
561 update_iconic(struct ws_win *win, int newv)
568 iprop = XInternAtom(display, "_SWM_ICONIC", False);
572 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
573 PropModeReplace, (unsigned char *)&v, 1);
575 XDeleteProperty(display, win->id, iprop);
579 get_iconic(struct ws_win *win)
584 unsigned long nitems, extra;
585 unsigned char *prop = NULL;
587 iprop = XInternAtom(display, "_SWM_ICONIC", False);
590 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
591 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
592 if (status != Success)
594 if (rettype != XA_INTEGER || retfmt != 32)
598 v = *((int32_t *)prop);
612 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
614 for (i = 0; i < LENGTH(ewmh); i++)
615 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
617 for (i = 0; i < ScreenCount(display); i++) {
618 /* Support check window will be created by workaround(). */
620 /* Report supported atoms */
621 XDeleteProperty(display, screens[i].root, sup_list);
622 for (j = 0; j < LENGTH(ewmh); j++)
623 XChangeProperty(display, screens[i].root,
624 sup_list, XA_ATOM, 32,
625 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
633 unsigned char *data = NULL;
635 Atom sup_check, sup_list;
638 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
639 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
641 for (i = 0; i < ScreenCount(display); i++) {
642 /* Get the support check window and destroy it */
643 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
648 XDestroyWindow(display, id);
649 XDeleteProperty(display, screens[i].root, sup_check);
650 XDeleteProperty(display, screens[i].root, sup_list);
658 ewmh_autoquirk(struct ws_win *win)
661 unsigned long *data = NULL, n;
664 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
665 XA_ATOM, &n, (void *)&data);
672 for (i = 0; i < n; i++) {
674 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
676 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
677 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
678 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
680 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
683 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
684 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
686 win->quirks = SWM_Q_FLOAT;
694 #define SWM_EWMH_ACTION_COUNT_MAX (6)
695 #define EWMH_F_FULLSCREEN (1<<0)
696 #define EWMH_F_ABOVE (1<<1)
697 #define EWMH_F_HIDDEN (1<<2)
698 #define EWMH_F_SKIP_PAGER (1<<3)
699 #define EWMH_F_SKIP_TASKBAR (1<<4)
700 #define SWM_F_MANUAL (1<<5)
703 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
705 struct swm_geometry rg;
713 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
719 store_float_geom(win, win->ws->r);
726 if (win->g_floatvalid) {
727 /* refloat at last floating relative position */
728 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
729 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
730 win->g.w = win->g_float.w;
731 win->g.h = 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);
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";
975 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
977 e->xany.window, name, e->type, QLength(display));
979 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
981 e->xany.window, e->type, QLength(display));
985 dumpwins(struct swm_region *r, union arg *args)
989 XWindowAttributes wa;
992 fprintf(stderr, "invalid workspace\n");
996 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
998 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
999 state = getstate(win->id);
1000 if (!XGetWindowAttributes(display, win->id, &wa))
1001 fprintf(stderr, "window: %lu failed "
1002 "XGetWindowAttributes\n", win->id);
1003 fprintf(stderr, "window: %lu map_state: %d state: %d "
1005 win->id, wa.map_state, state, win->transient);
1008 fprintf(stderr, "===== unmanaged window list =====\n");
1009 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1010 state = getstate(win->id);
1011 if (!XGetWindowAttributes(display, win->id, &wa))
1012 fprintf(stderr, "window: %lu failed "
1013 "XGetWindowAttributes\n", win->id);
1014 fprintf(stderr, "window: %lu map_state: %d state: %d "
1016 win->id, wa.map_state, state, win->transient);
1019 fprintf(stderr, "=================================\n");
1022 #define dumpevent(e)
1024 dumpwins(struct swm_region *r, union arg *args)
1027 #endif /* SWM_DEBUG */
1029 void expose(XEvent *);
1030 void keypress(XEvent *);
1031 void buttonpress(XEvent *);
1032 void configurerequest(XEvent *);
1033 void configurenotify(XEvent *);
1034 void destroynotify(XEvent *);
1035 void enternotify(XEvent *);
1036 void focusevent(XEvent *);
1037 void mapnotify(XEvent *);
1038 void mappingnotify(XEvent *);
1039 void maprequest(XEvent *);
1040 void propertynotify(XEvent *);
1041 void unmapnotify(XEvent *);
1042 void visibilitynotify(XEvent *);
1043 void clientmessage(XEvent *);
1045 void (*handler[LASTEvent])(XEvent *) = {
1047 [KeyPress] = keypress,
1048 [ButtonPress] = buttonpress,
1049 [ConfigureRequest] = configurerequest,
1050 [ConfigureNotify] = configurenotify,
1051 [DestroyNotify] = destroynotify,
1052 [EnterNotify] = enternotify,
1053 [FocusIn] = focusevent,
1054 [FocusOut] = focusevent,
1055 [MapNotify] = mapnotify,
1056 [MappingNotify] = mappingnotify,
1057 [MapRequest] = maprequest,
1058 [PropertyNotify] = propertynotify,
1059 [UnmapNotify] = unmapnotify,
1060 [VisibilityNotify] = visibilitynotify,
1061 [ClientMessage] = clientmessage,
1067 int saved_errno, status;
1070 saved_errno = errno;
1074 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1079 if (errno != ECHILD)
1080 warn("sighdlr: waitpid");
1081 #endif /* SWM_DEBUG */
1084 if (pid == searchpid)
1088 if (WIFEXITED(status)) {
1089 if (WEXITSTATUS(status) != 0)
1090 warnx("sighdlr: child exit status: %d",
1091 WEXITSTATUS(status));
1093 warnx("sighdlr: child is terminated "
1095 #endif /* SWM_DEBUG */
1109 errno = saved_errno;
1115 struct pid_e *p = NULL;
1117 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1122 TAILQ_FOREACH(p, &pidlist, entry) {
1131 name_to_color(char *colorname)
1135 XColor screen_def, exact_def;
1136 unsigned long result = 0;
1137 char cname[32] = "#";
1139 cmap = DefaultColormap(display, screens[0].idx);
1140 status = XAllocNamedColor(display, cmap, colorname,
1141 &screen_def, &exact_def);
1143 strlcat(cname, colorname + 2, sizeof cname - 1);
1144 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1148 result = screen_def.pixel;
1150 fprintf(stderr, "color '%s' not found.\n", colorname);
1156 setscreencolor(char *val, int i, int c)
1158 if (i > 0 && i <= ScreenCount(display)) {
1159 screens[i - 1].c[c].color = name_to_color(val);
1160 free(screens[i - 1].c[c].name);
1161 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1163 } else if (i == -1) {
1164 for (i = 0; i < ScreenCount(display); i++) {
1165 screens[i].c[c].color = name_to_color(val);
1166 free(screens[i].c[c].name);
1167 if ((screens[i].c[c].name = strdup(val)) == NULL)
1171 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1172 i, ScreenCount(display));
1176 fancy_stacker(struct workspace *ws)
1178 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1179 if (ws->cur_layout->l_stack == vertical_stack)
1180 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1181 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1182 if (ws->cur_layout->l_stack == horizontal_stack)
1183 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1184 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1188 plain_stacker(struct workspace *ws)
1190 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1191 if (ws->cur_layout->l_stack == vertical_stack)
1192 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1193 if (ws->cur_layout->l_stack == horizontal_stack)
1194 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1198 custom_region(char *val)
1200 unsigned int sidx, x, y, w, h;
1202 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1203 errx(1, "invalid custom region, "
1204 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1205 if (sidx < 1 || sidx > ScreenCount(display))
1206 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1207 sidx, ScreenCount(display));
1211 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1213 if (x > DisplayWidth(display, sidx) ||
1214 y > DisplayHeight(display, sidx) ||
1215 w + x > DisplayWidth(display, sidx) ||
1216 h + y > DisplayHeight(display, sidx)) {
1217 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1218 "- not within screen boundaries "
1219 "(%ux%u)\n", w, h, x, y,
1220 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1224 new_region(&screens[sidx], x, y, w, h);
1228 socket_setnonblock(int fd)
1232 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1233 err(1, "fcntl F_GETFL");
1234 flags |= O_NONBLOCK;
1235 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1236 err(1, "fcntl F_SETFL");
1240 bar_print(struct swm_region *r, char *s)
1245 XClearWindow(display, r->bar_window);
1246 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1249 textwidth = XTextWidth(bar_fs, s, len);
1251 switch (bar_justify) {
1252 case SWM_BAR_JUSTIFY_LEFT:
1255 case SWM_BAR_JUSTIFY_CENTER:
1256 x = (WIDTH(r) - textwidth) / 2;
1258 case SWM_BAR_JUSTIFY_RIGHT:
1259 x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
1263 if (x < SWM_BAR_OFFSET)
1266 XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
1270 bar_extra_stop(void)
1274 bzero(bar_pipe, sizeof bar_pipe);
1277 kill(bar_pid, SIGTERM);
1280 strlcpy(bar_ext, "", sizeof bar_ext);
1285 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1287 int do_class, do_name;
1289 XClassHint *xch = NULL;
1291 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1292 cur_focus != NULL) {
1293 if ((xch = XAllocClassHint()) == NULL)
1295 status = XGetClassHint(display, cur_focus->id, xch);
1296 if (status == BadWindow || status == BadAlloc)
1298 do_class = (title_class_enabled && xch->res_class != NULL);
1299 do_name = (title_name_enabled && xch->res_name != NULL);
1301 strlcat(s, xch->res_class, sz);
1302 if (do_class && do_name)
1303 strlcat(s, ":", sz);
1305 strlcat(s, xch->res_name, sz);
1306 strlcat(s, " ", sz);
1314 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1318 if (window_name_enabled && cur_focus != NULL) {
1319 XFetchName(display, cur_focus->id, &title);
1321 if (cur_focus->floating)
1322 strlcat(s, "(f) ", sz);
1323 strlcat(s, title, sz);
1324 strlcat(s, " ", sz);
1330 int urgent[SWM_WS_MAX];
1332 bar_urgent(char *s, ssize_t sz)
1334 XWMHints *wmh = NULL;
1339 if (urgent_enabled == 0)
1342 for (i = 0; i < SWM_WS_MAX; i++)
1345 for (i = 0; i < ScreenCount(display); i++)
1346 for (j = 0; j < SWM_WS_MAX; j++)
1347 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1348 wmh = XGetWMHints(display, win->id);
1352 if (wmh->flags & XUrgencyHint)
1357 strlcat(s, "* ", sz);
1358 for (i = 0; i < SWM_WS_MAX; i++) {
1360 snprintf(b, sizeof b, "%d ", i + 1);
1362 snprintf(b, sizeof b, "- ");
1365 strlcat(s, "* ", sz);
1373 struct swm_region *r;
1376 char ws[SWM_BAR_MAX];
1377 char s[SWM_BAR_MAX];
1378 char cn[SWM_BAR_MAX];
1379 char loc[SWM_BAR_MAX];
1380 char *b, *stack = "";
1382 if (bar_enabled == 0)
1384 if (bar_extra && bar_extra_running) {
1385 /* ignore short reads; it'll correct itself */
1386 while ((b = fgetln(stdin, &len)) != NULL)
1387 if (b && b[len - 1] == '\n') {
1389 strlcpy(bar_ext, b, sizeof bar_ext);
1391 if (b == NULL && errno != EAGAIN) {
1392 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1393 errno, strerror(errno));
1397 strlcpy(bar_ext, "", sizeof bar_ext);
1399 if (clock_enabled == 0)
1400 strlcpy(s, "", sizeof s);
1403 localtime_r(&tmt, &tm);
1404 strftime(s, sizeof s, clock_format, &tm);
1405 strlcat(s, " ", sizeof s);
1408 for (i = 0; i < ScreenCount(display); i++) {
1410 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1411 strlcpy(cn, "", sizeof cn);
1412 strlcpy(ws, "", sizeof ws);
1414 bar_urgent(cn, sizeof cn);
1415 bar_class_name(cn, sizeof cn, r->ws->focus);
1416 bar_window_name(cn, sizeof cn, r->ws->focus);
1418 snprintf(ws, sizeof ws, "<%s>", r->ws->name);
1421 stack = r->ws->stacker;
1423 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s %s",
1424 x++, r->ws->idx + 1, stack, ws, s, cn, bar_ext,
1439 bar_toggle(struct swm_region *r, union arg *args)
1441 struct swm_region *tmpr;
1442 int i, sc = ScreenCount(display);
1444 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1447 for (i = 0; i < sc; i++)
1448 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1449 XUnmapWindow(display, tmpr->bar_window);
1451 for (i = 0; i < sc; i++)
1452 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1453 XMapRaised(display, tmpr->bar_window);
1455 bar_enabled = !bar_enabled;
1458 /* must be after stack */
1465 XSetWindowAttributes wa;
1466 struct swm_region *r;
1469 /* do this here because the conf file is in memory */
1470 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1471 /* launch external status app */
1472 bar_extra_running = 1;
1473 if (pipe(bar_pipe) == -1)
1474 err(1, "pipe error");
1475 socket_setnonblock(bar_pipe[0]);
1476 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1477 if (dup2(bar_pipe[0], 0) == -1)
1479 if (dup2(bar_pipe[1], 1) == -1)
1481 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1482 err(1, "could not disable SIGPIPE");
1483 switch (bar_pid = fork()) {
1485 err(1, "cannot fork");
1489 execvp(bar_argv[0], bar_argv);
1490 err(1, "%s external app failed", bar_argv[0]);
1492 default: /* parent */
1498 bzero(&wa, sizeof wa);
1499 for (i = 0; i < ScreenCount(display); i++)
1500 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1502 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1503 wa.background_pixel =
1504 screens[i].c[SWM_S_COLOR_BAR].color;
1505 XChangeWindowAttributes(display, r->bar_window,
1506 CWBackPixel | CWBorderPixel, &wa);
1512 bar_setup(struct swm_region *r)
1517 XFreeFont(display, bar_fs);
1521 for (i = 0; bar_fonts[i] != NULL; i++) {
1522 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1528 if (bar_fonts[i] == NULL)
1529 errx(1, "couldn't load font");
1531 errx(1, "couldn't create font structure");
1533 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1534 2 * bar_border_width;
1536 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1538 r->bar_window = XCreateSimpleWindow(display,
1539 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1540 bar_height - 2 * bar_border_width,
1541 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1542 r->s->c[SWM_S_COLOR_BAR].color);
1543 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1544 XSetFont(display, bar_gc, bar_fs->fid);
1545 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1547 XMapRaised(display, r->bar_window);
1548 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1550 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1551 err(1, "could not install bar_signal");
1556 drain_enter_notify(void)
1561 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1564 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1568 set_win_state(struct ws_win *win, long state)
1570 long data[] = {state, None};
1572 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1577 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1578 (unsigned char *)data, 2);
1585 unsigned char *p = NULL;
1588 if (!get_property(w, astate, 2L, astate, &n, &p))
1591 result = *((long *)p);
1597 version(struct swm_region *r, union arg *args)
1599 bar_version = !bar_version;
1601 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s Build: %s",
1602 SCROTWM_VERSION, buildstr);
1604 strlcpy(bar_vertext, "", sizeof bar_vertext);
1609 client_msg(struct ws_win *win, Atom a)
1611 XClientMessageEvent cm;
1616 bzero(&cm, sizeof cm);
1617 cm.type = ClientMessage;
1618 cm.window = win->id;
1619 cm.message_type = aprot;
1622 cm.data.l[1] = CurrentTime;
1623 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1627 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1635 DNPRINTF(SWM_D_MISC,
1636 "config_win: win %lu x %d y %d w %d h %d\n",
1637 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1639 ce.type = ConfigureNotify;
1640 ce.display = display;
1642 ce.window = win->id;
1645 ce.width = win->g.w;
1646 ce.height = win->g.h;
1647 ce.border_width = border_width;
1649 ce.override_redirect = False;
1651 DNPRINTF(SWM_D_MISC,
1652 "config_win: ev win %lu x %d y %d w %d h %d\n",
1653 ev->window, ev->x, ev->y, ev->width, ev->height);
1654 ce.type = ConfigureNotify;
1655 ce.display = ev->display;
1656 ce.event = ev->window;
1657 ce.window = ev->window;
1660 ce.width = ev->width;
1661 ce.height = ev->height;
1662 ce.border_width = ev->border_width;
1663 ce.above = ev->above;
1664 ce.override_redirect = False;
1667 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1671 count_win(struct workspace *ws, int count_transient)
1676 TAILQ_FOREACH(win, &ws->winlist, entry) {
1677 if (count_transient == 0 && win->floating)
1679 if (count_transient == 0 && win->transient)
1685 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1691 quit(struct swm_region *r, union arg *args)
1693 DNPRINTF(SWM_D_MISC, "quit\n");
1698 unmap_window(struct ws_win *win)
1703 /* don't unmap again */
1704 if (getstate(win->id) == IconicState)
1707 set_win_state(win, IconicState);
1709 XUnmapWindow(display, win->id);
1710 XSetWindowBorder(display, win->id,
1711 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1720 for (i = 0; i < ScreenCount(display); i++)
1721 for (j = 0; j < SWM_WS_MAX; j++)
1722 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1727 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1734 event.display = display; /* Ignored, but what the hell */
1735 event.window = win->id;
1736 event.root = win->s->root;
1737 event.subwindow = None;
1738 event.time = CurrentTime;
1743 event.same_screen = True;
1744 event.keycode = XKeysymToKeycode(display, keysym);
1745 event.state = modifiers;
1747 event.type = KeyPress;
1748 XSendEvent(event.display, event.window, True,
1749 KeyPressMask, (XEvent *)&event);
1751 event.type = KeyRelease;
1752 XSendEvent(event.display, event.window, True,
1753 KeyPressMask, (XEvent *)&event);
1758 restart(struct swm_region *r, union arg *args)
1760 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1762 /* disable alarm because the following code may not be interrupted */
1764 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1765 errx(1, "can't disable alarm");
1770 XCloseDisplay(display);
1771 execvp(start_argv[0], start_argv);
1772 fprintf(stderr, "execvp failed\n");
1778 root_to_region(Window root)
1780 struct swm_region *r = NULL;
1782 int i, x, y, wx, wy;
1785 for (i = 0; i < ScreenCount(display); i++)
1786 if (screens[i].root == root)
1789 if (XQueryPointer(display, screens[i].root,
1790 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1791 /* choose a region based on pointer location */
1792 TAILQ_FOREACH(r, &screens[i].rl, entry)
1793 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1794 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1799 r = TAILQ_FIRST(&screens[i].rl);
1805 find_unmanaged_window(Window id)
1810 for (i = 0; i < ScreenCount(display); i++)
1811 for (j = 0; j < SWM_WS_MAX; j++)
1812 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1820 find_window(Window id)
1823 Window wrr, wpr, *wcr = NULL;
1827 for (i = 0; i < ScreenCount(display); i++)
1828 for (j = 0; j < SWM_WS_MAX; j++)
1829 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1833 /* if we were looking for the parent return that window instead */
1834 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1839 /* ignore not found and root */
1840 if (wpr == 0 || wrr == wpr)
1843 /* look for parent */
1844 for (i = 0; i < ScreenCount(display); i++)
1845 for (j = 0; j < SWM_WS_MAX; j++)
1846 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1854 spawn(int ws_idx, union arg *args, int close_fd)
1859 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1862 close(ConnectionNumber(display));
1864 setenv("LD_PRELOAD", SWM_LIB, 1);
1866 if (asprintf(&ret, "%d", ws_idx) == -1) {
1870 setenv("_SWM_WS", ret, 1);
1874 if (asprintf(&ret, "%d", getpid()) == -1) {
1878 setenv("_SWM_PID", ret, 1);
1882 if (setsid() == -1) {
1889 * close stdin and stdout to prevent interaction between apps
1890 * and the baraction script
1891 * leave stderr open to record errors
1893 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1897 dup2(fd, STDIN_FILENO);
1898 dup2(fd, STDOUT_FILENO);
1903 execvp(args->argv[0], args->argv);
1910 spawnterm(struct swm_region *r, union arg *args)
1912 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1916 setenv("_SWM_XTERM_FONTADJ", "", 1);
1917 spawn(r->ws->idx, args, 1);
1922 kill_refs(struct ws_win *win)
1925 struct swm_region *r;
1926 struct workspace *ws;
1931 for (i = 0; i < ScreenCount(display); i++)
1932 TAILQ_FOREACH(r, &screens[i].rl, entry)
1933 for (x = 0; x < SWM_WS_MAX; x++) {
1935 if (win == ws->focus)
1937 if (win == ws->focus_prev)
1938 ws->focus_prev = NULL;
1943 validate_win(struct ws_win *testwin)
1946 struct workspace *ws;
1947 struct swm_region *r;
1948 int i, x, foundit = 0;
1950 if (testwin == NULL)
1953 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1954 TAILQ_FOREACH(r, &screens[i].rl, entry)
1955 for (x = 0; x < SWM_WS_MAX; x++) {
1957 TAILQ_FOREACH(win, &ws->winlist, entry)
1965 validate_ws(struct workspace *testws)
1967 struct swm_region *r;
1968 struct workspace *ws;
1971 /* validate all ws */
1972 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1973 TAILQ_FOREACH(r, &screens[i].rl, entry)
1974 for (x = 0; x < SWM_WS_MAX; x++) {
1983 unfocus_win(struct ws_win *win)
1988 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1992 if (win->ws == NULL)
1995 if (validate_ws(win->ws))
1996 return; /* XXX this gets hit with thunderbird, needs fixing */
1998 if (win->ws->r == NULL)
2001 if (validate_win(win)) {
2006 if (win->ws->focus == win) {
2007 win->ws->focus = NULL;
2008 win->ws->focus_prev = win;
2011 if (validate_win(win->ws->focus)) {
2012 kill_refs(win->ws->focus);
2013 win->ws->focus = NULL;
2015 if (validate_win(win->ws->focus_prev)) {
2016 kill_refs(win->ws->focus_prev);
2017 win->ws->focus_prev = NULL;
2020 /* drain all previous unfocus events */
2021 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2024 grabbuttons(win, 0);
2025 XSetWindowBorder(display, win->id,
2026 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2028 XChangeProperty(display, win->s->root,
2029 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2030 PropModeReplace, (unsigned char *)&none,1);
2039 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2041 for (i = 0; i < ScreenCount(display); i++)
2042 for (j = 0; j < SWM_WS_MAX; j++)
2043 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2048 focus_win(struct ws_win *win)
2053 struct ws_win *cfw = NULL;
2056 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
2060 if (win->ws == NULL)
2063 if (validate_ws(win->ws))
2064 return; /* XXX this gets hit with thunderbird, needs fixing */
2066 if (validate_win(win)) {
2071 if (validate_win(win)) {
2076 XGetInputFocus(display, &cur_focus, &rr);
2077 if ((cfw = find_window(cur_focus)) != NULL)
2080 /* use larger hammer since the window was killed somehow */
2081 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2082 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2083 XSetWindowBorder(display, cfw->id,
2084 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2087 win->ws->focus = win;
2089 if (win->ws->r != NULL) {
2090 /* drain all previous focus events */
2091 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2095 XSetInputFocus(display, win->id,
2096 RevertToParent, CurrentTime);
2097 grabbuttons(win, 1);
2098 XSetWindowBorder(display, win->id,
2099 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2100 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2101 win->ws->always_raise)
2102 XMapRaised(display, win->id);
2104 XChangeProperty(display, win->s->root,
2105 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2106 PropModeReplace, (unsigned char *)&win->id,1);
2109 if (window_name_enabled)
2114 switchws(struct swm_region *r, union arg *args)
2116 int wsid = args->id, unmap_old = 0;
2117 struct swm_region *this_r, *other_r;
2119 struct workspace *new_ws, *old_ws;
2126 old_ws = this_r->ws;
2127 new_ws = &this_r->s->ws[wsid];
2129 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2130 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2133 if (new_ws == NULL || old_ws == NULL)
2135 if (new_ws == old_ws)
2138 other_r = new_ws->r;
2139 if (other_r == NULL) {
2140 /* the other workspace is hidden, hide this one */
2144 /* the other ws is visible in another region, exchange them */
2145 other_r->ws_prior = new_ws;
2146 other_r->ws = old_ws;
2147 old_ws->r = other_r;
2149 this_r->ws_prior = old_ws;
2150 this_r->ws = new_ws;
2153 /* this is needed so that we can click on a window after a restart */
2157 a.id = SWM_ARG_ID_FOCUSCUR;
2158 focus(new_ws->r, &a);
2162 /* unmap old windows */
2164 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2167 if (focus_mode == SWM_FOCUS_DEFAULT)
2168 drain_enter_notify();
2172 cyclews(struct swm_region *r, union arg *args)
2175 struct swm_screen *s = r->s;
2178 DNPRINTF(SWM_D_WS, "cyclews id %d "
2179 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2180 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2185 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2188 case SWM_ARG_ID_CYCLEWS_UP:
2189 if (a.id < SWM_WS_MAX - 1)
2194 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2197 case SWM_ARG_ID_CYCLEWS_DOWN:
2201 a.id = SWM_WS_MAX - 1;
2208 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2210 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2214 } while (a.id != r->ws->idx);
2218 priorws(struct swm_region *r, union arg *args)
2222 DNPRINTF(SWM_D_WS, "priorws id %d "
2223 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2224 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2226 if (r->ws_prior == NULL)
2229 a.id = r->ws_prior->idx;
2234 cyclescr(struct swm_region *r, union arg *args)
2236 struct swm_region *rr = NULL;
2240 /* do nothing if we don't have more than one screen */
2241 if (!(ScreenCount(display) > 1 || outputs > 1))
2246 case SWM_ARG_ID_CYCLESC_UP:
2247 rr = TAILQ_NEXT(r, entry);
2249 rr = TAILQ_FIRST(&screens[i].rl);
2251 case SWM_ARG_ID_CYCLESC_DOWN:
2252 rr = TAILQ_PREV(r, swm_region_list, entry);
2254 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2262 /* move mouse to region */
2264 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2265 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2267 a.id = SWM_ARG_ID_FOCUSCUR;
2270 if (rr->ws->focus) {
2271 /* move to focus window */
2272 x = rr->ws->focus->g.x + 1;
2273 y = rr->ws->focus->g.y + 1;
2274 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2279 sort_windows(struct ws_win_list *wl)
2281 struct ws_win *win, *parent, *nxt;
2286 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2287 nxt = TAILQ_NEXT(win, entry);
2288 if (win->transient) {
2289 parent = find_window(win->transient);
2290 if (parent == NULL) {
2291 fprintf(stderr, "not possible bug\n");
2294 TAILQ_REMOVE(wl, win, entry);
2295 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2302 swapwin(struct swm_region *r, union arg *args)
2304 struct ws_win *target, *source;
2305 struct ws_win *cur_focus;
2306 struct ws_win_list *wl;
2309 DNPRINTF(SWM_D_WS, "swapwin id %d "
2310 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2311 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2313 cur_focus = r->ws->focus;
2314 if (cur_focus == NULL)
2318 wl = &source->ws->winlist;
2321 case SWM_ARG_ID_SWAPPREV:
2322 if (source->transient)
2323 source = find_window(source->transient);
2324 target = TAILQ_PREV(source, ws_win_list, entry);
2325 if (target && target->transient)
2326 target = find_window(target->transient);
2327 TAILQ_REMOVE(wl, source, entry);
2329 TAILQ_INSERT_TAIL(wl, source, entry);
2331 TAILQ_INSERT_BEFORE(target, source, entry);
2333 case SWM_ARG_ID_SWAPNEXT:
2334 target = TAILQ_NEXT(source, entry);
2335 /* move the parent and let the sort handle the move */
2336 if (source->transient)
2337 source = find_window(source->transient);
2338 TAILQ_REMOVE(wl, source, entry);
2340 TAILQ_INSERT_HEAD(wl, source, entry);
2342 TAILQ_INSERT_AFTER(wl, target, source, entry);
2344 case SWM_ARG_ID_SWAPMAIN:
2345 target = TAILQ_FIRST(wl);
2346 if (target == source) {
2347 if (source->ws->focus_prev != NULL &&
2348 source->ws->focus_prev != target)
2350 source = source->ws->focus_prev;
2354 if (target == NULL || source == NULL)
2356 source->ws->focus_prev = target;
2357 TAILQ_REMOVE(wl, target, entry);
2358 TAILQ_INSERT_BEFORE(source, target, entry);
2359 TAILQ_REMOVE(wl, source, entry);
2360 TAILQ_INSERT_HEAD(wl, source, entry);
2362 case SWM_ARG_ID_MOVELAST:
2363 TAILQ_REMOVE(wl, source, entry);
2364 TAILQ_INSERT_TAIL(wl, source, entry);
2367 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2377 focus_prev(struct ws_win *win)
2379 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2380 struct ws_win *cur_focus = NULL;
2381 struct ws_win_list *wl = NULL;
2382 struct workspace *ws = NULL;
2384 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2386 if (!(win && win->ws))
2391 cur_focus = ws->focus;
2392 winlostfocus = cur_focus;
2394 /* pickle, just focus on whatever */
2395 if (cur_focus == NULL) {
2396 /* use prev_focus if valid */
2397 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2398 find_window(WINID(ws->focus_prev)))
2399 winfocus = ws->focus_prev;
2400 if (winfocus == NULL)
2401 winfocus = TAILQ_FIRST(wl);
2405 /* if transient focus on parent */
2406 if (cur_focus->transient) {
2407 winfocus = find_window(cur_focus->transient);
2411 /* if in max_stack try harder */
2412 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2413 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2414 if (cur_focus != ws->focus_prev)
2415 winfocus = ws->focus_prev;
2416 else if (cur_focus != ws->focus)
2417 winfocus = ws->focus;
2419 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2424 if (cur_focus == win)
2425 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2426 if (winfocus == NULL)
2427 winfocus = TAILQ_LAST(wl, ws_win_list);
2428 if (winfocus == NULL || winfocus == win)
2429 winfocus = TAILQ_NEXT(cur_focus, entry);
2431 if (winfocus == winlostfocus || winfocus == NULL)
2434 focus_magic(winfocus);
2438 focus(struct swm_region *r, union arg *args)
2440 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2441 struct ws_win *cur_focus = NULL;
2442 struct ws_win_list *wl = NULL;
2443 struct workspace *ws = NULL;
2448 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2450 /* treat FOCUS_CUR special */
2451 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2452 if (r->ws->focus && r->ws->focus->iconic == 0)
2453 winfocus = r->ws->focus;
2454 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2455 winfocus = r->ws->focus_prev;
2457 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2458 if (winfocus->iconic == 0)
2461 focus_magic(winfocus);
2465 if ((cur_focus = r->ws->focus) == NULL)
2470 winlostfocus = cur_focus;
2473 case SWM_ARG_ID_FOCUSPREV:
2474 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2476 head = TAILQ_LAST(wl, ws_win_list);
2478 if (WINID(winfocus) == cur_focus->transient) {
2479 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2481 head = TAILQ_LAST(wl, ws_win_list);
2486 if (winfocus && winfocus->iconic) {
2487 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2488 if (winfocus->iconic == 0)
2493 case SWM_ARG_ID_FOCUSNEXT:
2494 head = TAILQ_NEXT(cur_focus, entry);
2496 head = TAILQ_FIRST(wl);
2500 if (winfocus && winfocus->iconic) {
2501 TAILQ_FOREACH(winfocus, wl, entry)
2502 if (winfocus->iconic == 0)
2507 case SWM_ARG_ID_FOCUSMAIN:
2508 winfocus = TAILQ_FIRST(wl);
2509 if (winfocus == cur_focus)
2510 winfocus = cur_focus->ws->focus_prev;
2516 if (winfocus == winlostfocus || winfocus == NULL)
2519 focus_magic(winfocus);
2523 cycle_layout(struct swm_region *r, union arg *args)
2525 struct workspace *ws = r->ws;
2526 struct ws_win *winfocus;
2529 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2531 winfocus = ws->focus;
2534 if (ws->cur_layout->l_stack == NULL)
2535 ws->cur_layout = &layouts[0];
2538 if (focus_mode == SWM_FOCUS_DEFAULT)
2539 drain_enter_notify();
2540 a.id = SWM_ARG_ID_FOCUSCUR;
2546 stack_config(struct swm_region *r, union arg *args)
2548 struct workspace *ws = r->ws;
2550 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2553 if (ws->cur_layout->l_config != NULL)
2554 ws->cur_layout->l_config(ws, args->id);
2556 if (args->id != SWM_ARG_ID_STACKINIT)
2563 struct swm_geometry g;
2564 struct swm_region *r;
2567 DNPRINTF(SWM_D_STACK, "stack\n");
2569 for (i = 0; i < ScreenCount(display); i++) {
2571 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2572 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2573 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2575 /* start with screen geometry, adjust for bar */
2577 g.w -= 2 * border_width;
2578 g.h -= 2 * border_width;
2584 r->ws->cur_layout->l_stack(r->ws, &g);
2585 r->ws->cur_layout->l_string(r->ws);
2586 /* save r so we can track region changes */
2593 if (focus_mode == SWM_FOCUS_DEFAULT)
2594 drain_enter_notify();
2598 store_float_geom(struct ws_win *win, struct swm_region *r)
2600 /* retain window geom and region geom */
2601 win->g_float.x = win->g.x;
2602 win->g_float.y = win->g.y;
2603 win->g_float.w = win->g.w;
2604 win->g_float.h = win->g.h;
2605 win->rg_float.x = r->g.x;
2606 win->rg_float.y = r->g.y;
2607 win->rg_float.w = r->g.w;
2608 win->rg_float.h = r->g.h;
2609 win->g_floatvalid = 1;
2613 stack_floater(struct ws_win *win, struct swm_region *r)
2621 bzero(&wc, sizeof wc);
2622 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2625 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2626 * geom on ws switches or return from max mode
2629 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2630 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2632 * use stored g and rg to set relative position and size
2633 * as in old region or before max stack mode
2635 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2636 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2637 win->g.w = win->g_float.w;
2638 win->g.h = win->g_float.h;
2639 win->g_floatvalid = 0;
2642 win->floatmaxed = 0;
2644 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2645 (win->g.h >= HEIGHT(r)))
2646 wc.border_width = 0;
2648 wc.border_width = border_width;
2649 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2650 win->g.w = (double)WIDTH(r) * dialog_ratio;
2651 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2656 * floaters and transients are auto-centred unless moved
2659 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2660 2 - wc.border_width;
2661 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2662 2 - wc.border_width;
2665 /* win can be outside r if new r smaller than old r */
2666 /* Ensure top left corner inside r (move probs otherwise) */
2667 if (win->g.x < r->g.x - wc.border_width)
2668 win->g.x = r->g.x - wc.border_width;
2669 if (win->g.x > r->g.x + r->g.w - 1)
2670 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2671 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2672 if (win->g.y < r->g.y - wc.border_width)
2673 win->g.y = r->g.y - wc.border_width;
2674 if (win->g.y > r->g.y + r->g.h - 1)
2675 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2676 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2680 wc.width = win->g.w;
2681 wc.height = win->g.h;
2684 * Retain floater and transient geometry for correct positioning
2685 * when ws changes region
2687 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2688 store_float_geom(win, r);
2690 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2691 win->id, wc.x, wc.y, wc.width, wc.height);
2693 XConfigureWindow(display, win->id, mask, &wc);
2697 * Send keystrokes to terminal to decrease/increase the font size as the
2698 * window size changes.
2701 adjust_font(struct ws_win *win)
2703 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2704 win->floating || win->transient)
2707 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2708 win->g.w / win->sh.width_inc < term_width &&
2709 win->font_steps < SWM_MAX_FONT_STEPS) {
2710 win->font_size_boundary[win->font_steps] =
2711 (win->sh.width_inc * term_width) + win->sh.base_width;
2714 win->last_inc = win->sh.width_inc;
2715 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2716 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2717 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2720 win->last_inc = win->sh.width_inc;
2721 fake_keypress(win, XK_KP_Add, ShiftMask);
2725 #define SWAPXY(g) do { \
2727 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2728 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2731 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2734 XWindowAttributes wa;
2735 struct swm_geometry win_g, r_g = *g;
2736 struct ws_win *win, *fs_win = 0;
2737 int i, j, s, stacks;
2738 int w_inc = 1, h_inc, w_base = 1, h_base;
2739 int hrh, extra = 0, h_slice, last_h = 0;
2740 int split, colno, winno, mwin, msize, mscale;
2741 int remain, missing, v_slice, reconfigure;
2744 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2745 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2747 winno = count_win(ws, 0);
2748 if (winno == 0 && count_win(ws, 1) == 0)
2751 TAILQ_FOREACH(win, &ws->winlist, entry)
2752 if (win->transient == 0 && win->floating == 0
2753 && win->iconic == 0)
2760 w_inc = win->sh.width_inc;
2761 w_base = win->sh.base_width;
2762 mwin = ws->l_state.horizontal_mwin;
2763 mscale = ws->l_state.horizontal_msize;
2764 stacks = ws->l_state.horizontal_stacks;
2767 w_inc = win->sh.height_inc;
2768 w_base = win->sh.base_height;
2769 mwin = ws->l_state.vertical_mwin;
2770 mscale = ws->l_state.vertical_msize;
2771 stacks = ws->l_state.vertical_stacks;
2775 if (stacks > winno - mwin)
2776 stacks = winno - mwin;
2780 h_slice = r_g.h / SWM_H_SLICE;
2781 if (mwin && winno > mwin) {
2782 v_slice = r_g.w / SWM_V_SLICE;
2786 win_g.w = v_slice * mscale;
2788 if (w_inc > 1 && w_inc < v_slice) {
2789 /* adjust for window's requested size increment */
2790 remain = (win_g.w - w_base) % w_inc;
2791 missing = w_inc - remain;
2798 win_g.x += r_g.w - msize;
2801 colno = split = winno / stacks;
2802 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2803 2 * border_width) / stacks);
2805 hrh = r_g.h / colno;
2806 extra = r_g.h - (colno * hrh);
2807 win_g.h = hrh - 2 * border_width;
2809 /* stack all the tiled windows */
2810 i = j = 0, s = stacks;
2811 TAILQ_FOREACH(win, &ws->winlist, entry) {
2812 if (win->transient != 0 || win->floating != 0)
2814 if (win->iconic != 0)
2817 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2822 if (split && i == split) {
2823 colno = (winno - mwin) / stacks;
2824 if (s <= (winno - mwin) % stacks)
2826 split = split + colno;
2827 hrh = (r_g.h / colno);
2828 extra = r_g.h - (colno * hrh);
2832 win_g.x += win_g.w + 2 * border_width;
2833 win_g.w = (r_g.w - msize -
2834 (stacks * 2 * border_width)) / stacks;
2836 win_g.w += (r_g.w - msize -
2837 (stacks * 2 * border_width)) % stacks;
2841 win_g.h = hrh - 2 * border_width;
2843 h_inc = win->sh.width_inc;
2844 h_base = win->sh.base_width;
2846 h_inc = win->sh.height_inc;
2847 h_base = win->sh.base_height;
2849 if (j == colno - 1) {
2850 win_g.h = hrh + extra;
2851 } else if (h_inc > 1 && h_inc < h_slice) {
2852 /* adjust for window's requested size increment */
2853 remain = (win_g.h - h_base) % h_inc;
2854 missing = h_inc - remain;
2856 if (missing <= extra || j == 0) {
2868 win_g.y += last_h + 2 * border_width;
2870 bzero(&wc, sizeof wc);
2871 if (disable_border && bar_enabled == 0 && winno == 1){
2872 wc.border_width = 0;
2873 win_g.w += 2 * border_width;
2874 win_g.h += 2 * border_width;
2876 wc.border_width = border_width;
2879 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2880 win->g.w != win_g.h || win->g.h != win_g.w) {
2882 win->g.x = wc.x = win_g.y;
2883 win->g.y = wc.y = win_g.x;
2884 win->g.w = wc.width = win_g.h;
2885 win->g.h = wc.height = win_g.w;
2888 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2889 win->g.w != win_g.w || win->g.h != win_g.h) {
2891 win->g.x = wc.x = win_g.x;
2892 win->g.y = wc.y = win_g.y;
2893 win->g.w = wc.width = win_g.w;
2894 win->g.h = wc.height = win_g.h;
2899 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2900 XConfigureWindow(display, win->id, mask, &wc);
2903 if (XGetWindowAttributes(display, win->id, &wa))
2904 if (wa.map_state == IsUnmapped)
2905 XMapRaised(display, win->id);
2913 /* now, stack all the floaters and transients */
2914 TAILQ_FOREACH(win, &ws->winlist, entry) {
2915 if (win->transient == 0 && win->floating == 0)
2917 if (win->iconic == 1)
2919 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2924 stack_floater(win, ws->r);
2925 XMapRaised(display, win->id);
2929 stack_floater(fs_win, ws->r);
2930 XMapRaised(display, fs_win->id);
2935 vertical_config(struct workspace *ws, int id)
2937 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2940 case SWM_ARG_ID_STACKRESET:
2941 case SWM_ARG_ID_STACKINIT:
2942 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2943 ws->l_state.vertical_mwin = 1;
2944 ws->l_state.vertical_stacks = 1;
2946 case SWM_ARG_ID_MASTERSHRINK:
2947 if (ws->l_state.vertical_msize > 1)
2948 ws->l_state.vertical_msize--;
2950 case SWM_ARG_ID_MASTERGROW:
2951 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2952 ws->l_state.vertical_msize++;
2954 case SWM_ARG_ID_MASTERADD:
2955 ws->l_state.vertical_mwin++;
2957 case SWM_ARG_ID_MASTERDEL:
2958 if (ws->l_state.vertical_mwin > 0)
2959 ws->l_state.vertical_mwin--;
2961 case SWM_ARG_ID_STACKINC:
2962 ws->l_state.vertical_stacks++;
2964 case SWM_ARG_ID_STACKDEC:
2965 if (ws->l_state.vertical_stacks > 1)
2966 ws->l_state.vertical_stacks--;
2974 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2976 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2978 stack_master(ws, g, 0, 0);
2982 horizontal_config(struct workspace *ws, int id)
2984 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2987 case SWM_ARG_ID_STACKRESET:
2988 case SWM_ARG_ID_STACKINIT:
2989 ws->l_state.horizontal_mwin = 1;
2990 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2991 ws->l_state.horizontal_stacks = 1;
2993 case SWM_ARG_ID_MASTERSHRINK:
2994 if (ws->l_state.horizontal_msize > 1)
2995 ws->l_state.horizontal_msize--;
2997 case SWM_ARG_ID_MASTERGROW:
2998 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2999 ws->l_state.horizontal_msize++;
3001 case SWM_ARG_ID_MASTERADD:
3002 ws->l_state.horizontal_mwin++;
3004 case SWM_ARG_ID_MASTERDEL:
3005 if (ws->l_state.horizontal_mwin > 0)
3006 ws->l_state.horizontal_mwin--;
3008 case SWM_ARG_ID_STACKINC:
3009 ws->l_state.horizontal_stacks++;
3011 case SWM_ARG_ID_STACKDEC:
3012 if (ws->l_state.horizontal_stacks > 1)
3013 ws->l_state.horizontal_stacks--;
3021 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3023 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3025 stack_master(ws, g, 1, 0);
3028 /* fullscreen view */
3030 max_stack(struct workspace *ws, struct swm_geometry *g)
3033 struct swm_geometry gg = *g;
3034 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3038 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3043 winno = count_win(ws, 0);
3044 if (winno == 0 && count_win(ws, 1) == 0)
3047 TAILQ_FOREACH(win, &ws->winlist, entry) {
3048 if (win->transient) {
3050 parent = find_window(win->transient);
3054 if (win->floating && win->floatmaxed == 0 ) {
3056 * retain geometry for retrieval on exit from
3059 store_float_geom(win, ws->r);
3060 win->floatmaxed = 1;
3063 /* only reconfigure if necessary */
3064 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3066 bzero(&wc, sizeof wc);
3067 win->g.x = wc.x = gg.x;
3068 win->g.y = wc.y = gg.y;
3070 wc.border_width = border_width;
3071 win->g.w = wc.width = gg.w;
3072 win->g.h = wc.height = gg.h;
3074 wc.border_width = 0;
3075 win->g.w = wc.width = gg.w + 2 * border_width;
3076 win->g.h = wc.height = gg.h + 2 * border_width;
3078 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3079 XConfigureWindow(display, win->id, mask, &wc);
3081 /* unmap only if we don't have multi screen */
3082 if (win != ws->focus)
3083 if (!(ScreenCount(display) > 1 || outputs > 1))
3087 /* put the last transient on top */
3090 XMapRaised(display, parent->id);
3091 stack_floater(wintrans, ws->r);
3092 focus_magic(wintrans);
3097 send_to_ws(struct swm_region *r, union arg *args)
3099 int wsid = args->id;
3100 struct ws_win *win = NULL, *parent;
3101 struct workspace *ws, *nws;
3102 Atom ws_idx_atom = 0;
3103 unsigned char ws_idx_str[SWM_PROPLEN];
3112 if (win->ws->idx == wsid)
3115 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3118 nws = &win->s->ws[wsid];
3120 a.id = SWM_ARG_ID_FOCUSPREV;
3122 if (win->transient) {
3123 parent = find_window(win->transient);
3125 unmap_window(parent);
3126 TAILQ_REMOVE(&ws->winlist, parent, entry);
3127 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3132 TAILQ_REMOVE(&ws->winlist, win, entry);
3133 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3136 /* Try to update the window's workspace property */
3137 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3139 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3141 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3143 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3144 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3151 pressbutton(struct swm_region *r, union arg *args)
3153 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3154 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3158 raise_toggle(struct swm_region *r, union arg *args)
3160 if (r && r->ws == NULL)
3163 r->ws->always_raise = !r->ws->always_raise;
3165 /* bring floaters back to top */
3166 if (r->ws->always_raise == 0)
3171 iconify(struct swm_region *r, union arg *args)
3175 if (r->ws->focus == NULL)
3177 unmap_window(r->ws->focus);
3178 update_iconic(r->ws->focus, 1);
3180 if (focus_mode == SWM_FOCUS_DEFAULT)
3181 drain_enter_notify();
3182 r->ws->focus = NULL;
3183 a.id = SWM_ARG_ID_FOCUSCUR;
3188 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3189 unsigned long *slen)
3192 unsigned long nitems, nbytes, nextra;
3193 unsigned char *prop = NULL;
3196 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3197 &rettype, &retfmt, &nitems, &nbytes, &prop);
3198 if (status != Success)
3202 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3203 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3204 if (status != Success) {
3208 if (rettype != stype) {
3217 uniconify(struct swm_region *r, union arg *args)
3221 unsigned char *name;
3225 DNPRINTF(SWM_D_MISC, "uniconify\n");
3227 if (r && r->ws == NULL)
3230 /* make sure we have anything to uniconify */
3231 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3232 if (win->ws == NULL)
3233 continue; /* should never happen */
3234 if (win->iconic == 0)
3242 search_resp_action = SWM_SEARCH_UNICONIFY;
3244 spawn_select(r, args, "search", &searchpid);
3246 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3249 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3250 if (win->ws == NULL)
3251 continue; /* should never happen */
3252 if (win->iconic == 0)
3255 name = get_win_name(display, win->id, a_wmname, a_string,
3259 fprintf(lfile, "%s.%lu\n", name, win->id);
3267 name_workspace(struct swm_region *r, union arg *args)
3269 struct workspace *ws;
3272 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3280 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3282 spawn_select(r, args, "name_workspace", &searchpid);
3284 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3287 fprintf(lfile, "%s", "");
3292 search_workspace(struct swm_region *r, union arg *args)
3295 struct workspace *ws;
3298 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3304 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3306 spawn_select(r, args, "search", &searchpid);
3308 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3311 for (i = 0; i < SWM_WS_MAX; i++) {
3315 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3316 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3323 search_win_cleanup(void)
3325 struct search_window *sw = NULL;
3327 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3328 XDestroyWindow(display, sw->indicator);
3329 TAILQ_REMOVE(&search_wl, sw, entry);
3335 search_win(struct swm_region *r, union arg *args)
3337 struct ws_win *win = NULL;
3338 struct search_window *sw = NULL;
3348 DNPRINTF(SWM_D_MISC, "search_win\n");
3351 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3353 spawn_select(r, args, "search", &searchpid);
3355 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3358 TAILQ_INIT(&search_wl);
3361 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3362 if (win->iconic == 1)
3365 sw = calloc(1, sizeof(struct search_window));
3367 fprintf(stderr, "search_win: calloc: %s", strerror(errno));
3369 search_win_cleanup();
3375 snprintf(s, sizeof s, "%d", i);
3377 textwidth = XTextWidth(bar_fs, s, len);
3379 w = XCreateSimpleWindow(display,
3380 win->id, 0, 0, textwidth + 12,
3381 bar_fs->ascent + bar_fs->descent + 4, 1,
3382 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3383 r->s->c[SWM_S_COLOR_FOCUS].color);
3386 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3388 gc = XCreateGC(display, w, 0, &gcv);
3389 XSetFont(display, gc, bar_fs->fid);
3390 XMapRaised(display, w);
3391 XSetForeground(display, gc, r->s->c[SWM_S_COLOR_BAR].color);
3393 XDrawString(display, w, gc, 6, bar_fs->ascent + 2, s, len);
3395 fprintf(lfile, "%d\n", i);
3403 search_resp_uniconify(char *resp, unsigned long len)
3405 unsigned char *name;
3409 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp %s\n", resp);
3411 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3412 if (win->iconic == 0)
3414 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3417 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3422 if (strncmp(s, resp, len) == 0) {
3423 /* XXX this should be a callback to generalize */
3424 update_iconic(win, 0);
3433 search_resp_name_workspace(char *resp, unsigned long len)
3435 struct workspace *ws;
3437 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp %s\n", resp);
3439 if (search_r->ws == NULL)
3444 free(search_r->ws->name);
3445 search_r->ws->name = NULL;
3449 ws->name = strdup(resp);
3450 if (ws->name == NULL) {
3451 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: strdup: %s",
3459 search_resp_search_workspace(char *resp, unsigned long len)
3466 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp %s\n", resp);
3470 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3477 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3479 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3486 switchws(search_r, &a);
3490 search_resp_search_window(char *resp, unsigned long len)
3495 struct search_window *sw;
3497 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp %s\n", resp);
3501 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3506 idx = strtonum(s, 1, INT_MAX, &errstr);
3508 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3515 TAILQ_FOREACH(sw, &search_wl, entry)
3516 if (idx == sw->idx) {
3522 #define MAX_RESP_LEN 1024
3525 search_do_resp(void)
3531 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3536 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3537 fprintf(stderr, "search: calloc\n");
3541 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3543 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3546 resp[rbytes] = '\0';
3549 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3550 * newline, so work around that by sanitizing the resp now.
3552 resp[strcspn(resp, "\n")] = '\0';
3555 switch (search_resp_action) {
3556 case SWM_SEARCH_UNICONIFY:
3557 search_resp_uniconify(resp, len);
3559 case SWM_SEARCH_NAME_WORKSPACE:
3560 search_resp_name_workspace(resp, len);
3562 case SWM_SEARCH_SEARCH_WORKSPACE:
3563 search_resp_search_workspace(resp, len);
3565 case SWM_SEARCH_SEARCH_WINDOW:
3566 search_resp_search_window(resp, len);
3571 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3572 search_win_cleanup();
3574 search_resp_action = SWM_SEARCH_NONE;
3575 close(select_resp_pipe[0]);
3580 wkill(struct swm_region *r, union arg *args)
3582 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3584 if (r->ws->focus == NULL)
3587 if (args->id == SWM_ARG_ID_KILLWINDOW)
3588 XKillClient(display, r->ws->focus->id);
3590 if (r->ws->focus->can_delete)
3591 client_msg(r->ws->focus, adelete);
3596 floating_toggle_win(struct ws_win *win)
3598 struct swm_region *r;
3608 /* reject floating toggles in max stack mode */
3609 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3612 if (win->floating) {
3613 if (!win->floatmaxed) {
3614 /* retain position for refloat */
3615 store_float_geom(win, r);
3619 if (win->g_floatvalid) {
3620 /* refloat at last floating relative position */
3621 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3622 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3623 win->g.w = win->g_float.w;
3624 win->g.h = win->g_float.h;
3629 ewmh_update_actions(win);
3635 floating_toggle(struct swm_region *r, union arg *args)
3637 struct ws_win *win = r->ws->focus;
3643 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3644 _NET_WM_STATE_TOGGLE);
3647 if (focus_mode == SWM_FOCUS_DEFAULT)
3648 drain_enter_notify();
3650 if (win == win->ws->focus) {
3651 a.id = SWM_ARG_ID_FOCUSCUR;
3652 focus(win->ws->r, &a);
3657 resize_window(struct ws_win *win, int center)
3661 struct swm_region *r;
3663 r = root_to_region(win->wa.root);
3664 bzero(&wc, sizeof wc);
3665 mask = CWBorderWidth | CWWidth | CWHeight;
3666 wc.border_width = border_width;
3667 wc.width = win->g.w;
3668 wc.height = win->g.h;
3669 if (center == SWM_ARG_ID_CENTER) {
3670 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3671 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3675 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3676 win->id, wc.x, wc.y, wc.width, wc.height);
3678 XConfigureWindow(display, win->id, mask, &wc);
3681 #define SWM_RESIZE_STEPS (50)
3684 resize(struct ws_win *win, union arg *args)
3688 struct swm_region *r = NULL;
3690 int resize_step = 0;
3696 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3697 win->id, win->floating, win->transient);
3699 if (!(win->transient != 0 || win->floating != 0))
3702 /* reject resizes in max mode for floaters (transient ok) */
3703 if (win->floatmaxed)
3707 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3713 case SWM_ARG_ID_WIDTHSHRINK:
3714 win->g.w -= SWM_RESIZE_STEPS;
3717 case SWM_ARG_ID_WIDTHGROW:
3718 win->g.w += SWM_RESIZE_STEPS;
3721 case SWM_ARG_ID_HEIGHTSHRINK:
3722 win->g.h -= SWM_RESIZE_STEPS;
3725 case SWM_ARG_ID_HEIGHTGROW:
3726 win->g.h += SWM_RESIZE_STEPS;
3733 resize_window(win, 0);
3734 store_float_geom(win,r);
3738 if (focus_mode == SWM_FOCUS_DEFAULT)
3739 drain_enter_notify();
3741 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3742 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3745 /* place pointer at bottom left corner or nearest point inside r */
3746 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3747 relx = win->g.w - 1;
3749 relx = r->g.x + r->g.w - win->g.x - 1;
3751 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3752 rely = win->g.h - 1;
3754 rely = r->g.y + r->g.h - win->g.y - 1;
3756 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3758 XMaskEvent(display, MOUSEMASK | ExposureMask |
3759 SubstructureRedirectMask, &ev);
3761 case ConfigureRequest:
3764 handler[ev.type](&ev);
3767 /* do not allow resize outside of region */
3768 if ( ev.xmotion.x_root < r->g.x ||
3769 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3770 ev.xmotion.y_root < r->g.y ||
3771 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3774 if (ev.xmotion.x <= 1)
3776 if (ev.xmotion.y <= 1)
3778 win->g.w = ev.xmotion.x + 1;
3779 win->g.h = ev.xmotion.y + 1;
3781 /* not free, don't sync more than 120 times / second */
3782 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3783 time = ev.xmotion.time;
3784 XSync(display, False);
3785 resize_window(win, args->id);
3789 } while (ev.type != ButtonRelease);
3791 XSync(display, False);
3792 resize_window(win, args->id);
3794 store_float_geom(win,r);
3796 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3798 XUngrabPointer(display, CurrentTime);
3801 drain_enter_notify();
3805 resize_step(struct swm_region *r, union arg *args)
3807 struct ws_win *win = NULL;
3809 if (r && r->ws && r->ws->focus)
3819 move_window(struct ws_win *win)
3823 struct swm_region *r;
3825 r = root_to_region(win->wa.root);
3826 bzero(&wc, sizeof wc);
3830 wc.border_width = border_width;
3832 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3833 win->id, wc.x, wc.y, wc.width, wc.height);
3835 XConfigureWindow(display, win->id, mask, &wc);
3838 #define SWM_MOVE_STEPS (50)
3841 move(struct ws_win *win, union arg *args)
3846 struct swm_region *r = NULL;
3852 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3853 win->id, win->floating, win->transient);
3855 /* in max_stack mode should only move transients */
3856 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3860 if (win->floating == 0 && !win->transient) {
3861 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3864 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3871 case SWM_ARG_ID_MOVELEFT:
3872 win->g.x -= (SWM_MOVE_STEPS - border_width);
3875 case SWM_ARG_ID_MOVERIGHT:
3876 win->g.x += (SWM_MOVE_STEPS - border_width);
3879 case SWM_ARG_ID_MOVEUP:
3880 win->g.y -= (SWM_MOVE_STEPS - border_width);
3883 case SWM_ARG_ID_MOVEDOWN:
3884 win->g.y += (SWM_MOVE_STEPS - border_width);
3892 store_float_geom(win,r);
3897 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3898 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3900 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3902 XMaskEvent(display, MOUSEMASK | ExposureMask |
3903 SubstructureRedirectMask, &ev);
3905 case ConfigureRequest:
3908 handler[ev.type](&ev);
3911 /* don't allow to move window origin out of region */
3912 if ( ev.xmotion.x_root < r->g.x ||
3913 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3914 ev.xmotion.y_root < r->g.y ||
3915 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3918 win->g.x = ev.xmotion.x_root - border_width;
3919 win->g.y = ev.xmotion.y_root - border_width;
3921 /* not free, don't sync more than 120 times / second */
3922 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3923 time = ev.xmotion.time;
3924 XSync(display, False);
3929 } while (ev.type != ButtonRelease);
3931 XSync(display, False);
3934 store_float_geom(win,r);
3935 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3936 XUngrabPointer(display, CurrentTime);
3939 drain_enter_notify();
3943 move_step(struct swm_region *r, union arg *args)
3945 struct ws_win *win = NULL;
3947 if (r && r->ws && r->ws->focus)
3952 if (!(win->transient != 0 || win->floating != 0))
3959 /* user/key callable function IDs */
4029 kf_search_workspace,
4031 kf_dumpwins, /* MUST BE LAST */
4035 /* key definitions */
4037 dummykeyfunc(struct swm_region *r, union arg *args)
4042 legacyfunc(struct swm_region *r, union arg *args)
4047 char name[SWM_FUNCNAME_LEN];
4048 void (*func)(struct swm_region *r, union arg *);
4050 } keyfuncs[kf_invalid + 1] = {
4051 /* name function argument */
4052 { "cycle_layout", cycle_layout, {0} },
4053 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4054 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4055 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4056 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4057 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4058 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4059 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4060 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4061 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4062 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4063 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4064 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4065 { "spawn_term", spawnterm, {.argv = spawn_term} },
4066 { "spawn_menu", legacyfunc, {0} },
4067 { "quit", quit, {0} },
4068 { "restart", restart, {0} },
4069 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4070 { "ws_1", switchws, {.id = 0} },
4071 { "ws_2", switchws, {.id = 1} },
4072 { "ws_3", switchws, {.id = 2} },
4073 { "ws_4", switchws, {.id = 3} },
4074 { "ws_5", switchws, {.id = 4} },
4075 { "ws_6", switchws, {.id = 5} },
4076 { "ws_7", switchws, {.id = 6} },
4077 { "ws_8", switchws, {.id = 7} },
4078 { "ws_9", switchws, {.id = 8} },
4079 { "ws_10", switchws, {.id = 9} },
4080 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4081 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4082 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4083 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4084 { "ws_prior", priorws, {0} },
4085 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4086 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4087 { "mvws_1", send_to_ws, {.id = 0} },
4088 { "mvws_2", send_to_ws, {.id = 1} },
4089 { "mvws_3", send_to_ws, {.id = 2} },
4090 { "mvws_4", send_to_ws, {.id = 3} },
4091 { "mvws_5", send_to_ws, {.id = 4} },
4092 { "mvws_6", send_to_ws, {.id = 5} },
4093 { "mvws_7", send_to_ws, {.id = 6} },
4094 { "mvws_8", send_to_ws, {.id = 7} },
4095 { "mvws_9", send_to_ws, {.id = 8} },
4096 { "mvws_10", send_to_ws, {.id = 9} },
4097 { "bar_toggle", bar_toggle, {0} },
4098 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4099 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4100 { "screenshot_all", legacyfunc, {0} },
4101 { "screenshot_wind", legacyfunc, {0} },
4102 { "float_toggle", floating_toggle,{0} },
4103 { "version", version, {0} },
4104 { "spawn_lock", legacyfunc, {0} },
4105 { "spawn_initscr", legacyfunc, {0} },
4106 { "spawn_custom", dummykeyfunc, {0} },
4107 { "iconify", iconify, {0} },
4108 { "uniconify", uniconify, {0} },
4109 { "raise_toggle", raise_toggle, {0} },
4110 { "button2", pressbutton, {2} },
4111 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4112 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4113 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4114 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4115 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4116 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4117 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4118 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4119 { "name_workspace", name_workspace, {0} },
4120 { "search_workspace", search_workspace, {0} },
4121 { "search_win", search_win, {0} },
4122 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4123 { "invalid key func", NULL, {0} },
4128 enum keyfuncid funcid;
4131 int keys_size = 0, keys_length = 0;
4132 struct key *keys = NULL;
4135 enum { client_click, root_click };
4137 unsigned int action;
4139 unsigned int button;
4140 void (*func)(struct ws_win *, union arg *);
4143 /* action key mouse button func args */
4144 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4145 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4146 { client_click, MODKEY, Button1, move, {0} },
4150 update_modkey(unsigned int mod)
4155 for (i = 0; i < keys_length; i++)
4156 if (keys[i].mod & ShiftMask)
4157 keys[i].mod = mod | ShiftMask;
4161 for (i = 0; i < LENGTH(buttons); i++)
4162 if (buttons[i].mask & ShiftMask)
4163 buttons[i].mask = mod | ShiftMask;
4165 buttons[i].mask = mod;
4175 int spawns_size = 0, spawns_length = 0;
4176 struct spawn_prog *spawns = NULL;
4179 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4182 struct spawn_prog *prog = NULL;
4184 char *ap, **real_args;
4186 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
4189 for (i = 0; i < spawns_length; i++) {
4190 if (!strcasecmp(spawn_name, spawns[i].name))
4194 fprintf(stderr, "spawn_custom: program %s not found\n",
4199 /* make room for expanded args */
4200 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4201 err(1, "spawn_custom: calloc real_args");
4203 /* expand spawn_args into real_args */
4204 for (i = 0; i < prog->argc; i++) {
4206 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
4207 if (!strcasecmp(ap, "$bar_border")) {
4209 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4211 err(1, "spawn_custom border color");
4212 } else if (!strcasecmp(ap, "$bar_color")) {
4214 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4216 err(1, "spawn_custom bar color");
4217 } else if (!strcasecmp(ap, "$bar_font")) {
4218 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
4220 err(1, "spawn_custom bar fonts");
4221 } else if (!strcasecmp(ap, "$bar_font_color")) {
4223 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4225 err(1, "spawn_custom color font");
4226 } else if (!strcasecmp(ap, "$color_focus")) {
4228 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4230 err(1, "spawn_custom color focus");
4231 } else if (!strcasecmp(ap, "$color_unfocus")) {
4233 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4235 err(1, "spawn_custom color unfocus");
4237 /* no match --> copy as is */
4238 if ((real_args[i] = strdup(ap)) == NULL)
4239 err(1, "spawn_custom strdup(ap)");
4241 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
4246 if ((swm_debug & SWM_D_SPAWN) != 0) {
4247 fprintf(stderr, "spawn_custom: result = ");
4248 for (i = 0; i < prog->argc; i++)
4249 fprintf(stderr, "\"%s\" ", real_args[i]);
4250 fprintf(stderr, "\n");
4253 *ret_args = real_args;
4254 return (prog->argc);
4258 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4264 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4268 spawn(r->ws->idx, &a, 1);
4270 for (i = 0; i < spawn_argc; i++)
4276 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4282 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4286 if (pipe(select_list_pipe) == -1)
4287 err(1, "pipe error");
4288 if (pipe(select_resp_pipe) == -1)
4289 err(1, "pipe error");
4291 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4292 err(1, "could not disable SIGPIPE");
4293 switch (*pid = fork()) {
4295 err(1, "cannot fork");
4298 if (dup2(select_list_pipe[0], 0) == -1)
4300 if (dup2(select_resp_pipe[1], 1) == -1)
4302 close(select_list_pipe[1]);
4303 close(select_resp_pipe[0]);
4304 spawn(r->ws->idx, &a, 0);
4306 default: /* parent */
4307 close(select_list_pipe[0]);
4308 close(select_resp_pipe[1]);
4312 for (i = 0; i < spawn_argc; i++)
4318 setspawn(struct spawn_prog *prog)
4322 if (prog == NULL || prog->name == NULL)
4326 for (i = 0; i < spawns_length; i++) {
4327 if (!strcmp(spawns[i].name, prog->name)) {
4329 if (prog->argv == NULL) {
4331 DNPRINTF(SWM_D_SPAWN,
4332 "setspawn: delete #%d %s\n",
4334 free(spawns[i].name);
4335 for (j = 0; j < spawns[i].argc; j++)
4336 free(spawns[i].argv[j]);
4337 free(spawns[i].argv);
4338 j = spawns_length - 1;
4340 spawns[i] = spawns[j];
4345 DNPRINTF(SWM_D_SPAWN,
4346 "setspawn: replace #%d %s\n",
4348 free(spawns[i].name);
4349 for (j = 0; j < spawns[i].argc; j++)
4350 free(spawns[i].argv[j]);
4351 free(spawns[i].argv);
4354 /* found case handled */
4360 if (prog->argv == NULL) {
4362 "error: setspawn: cannot find program %s", prog->name);
4367 /* not found: add */
4368 if (spawns_size == 0 || spawns == NULL) {
4370 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4371 spawns = malloc((size_t)spawns_size *
4372 sizeof(struct spawn_prog));
4373 if (spawns == NULL) {
4374 fprintf(stderr, "setspawn: malloc failed\n");
4378 } else if (spawns_length == spawns_size) {
4380 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4381 spawns = realloc(spawns, (size_t)spawns_size *
4382 sizeof(struct spawn_prog));
4383 if (spawns == NULL) {
4384 fprintf(stderr, "setspawn: realloc failed\n");
4390 if (spawns_length < spawns_size) {
4391 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4392 spawns_length, prog->name);
4393 i = spawns_length++;
4396 fprintf(stderr, "spawns array problem?\n");
4397 if (spawns == NULL) {
4398 fprintf(stderr, "spawns array is NULL!\n");
4406 setconfspawn(char *selector, char *value, int flags)
4408 struct spawn_prog *prog;
4409 char *vp, *cp, *word;
4411 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4412 if ((prog = calloc(1, sizeof *prog)) == NULL)
4413 err(1, "setconfspawn: calloc prog");
4414 prog->name = strdup(selector);
4415 if (prog->name == NULL)
4416 err(1, "setconfspawn prog->name");
4417 if ((cp = vp = strdup(value)) == NULL)
4418 err(1, "setconfspawn: strdup(value) ");
4419 while ((word = strsep(&cp, " \t")) != NULL) {
4420 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4422 cp += (long)strspn(cp, " \t");
4423 if (strlen(word) > 0) {
4425 if ((prog->argv = realloc(prog->argv,
4426 prog->argc * sizeof(char *))) == NULL)
4427 err(1, "setconfspawn: realloc");
4428 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4429 err(1, "setconfspawn: strdup");
4436 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4443 setconfspawn("term", "xterm", 0);
4444 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4445 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4446 setconfspawn("lock", "xlock", 0);
4447 setconfspawn("initscr", "initscreen.sh", 0);
4448 setconfspawn("menu", "dmenu_run"
4451 " -nf $bar_font_color"
4453 " -sf $bar_color", 0);
4454 setconfspawn("search", "dmenu"
4458 " -nf $bar_font_color"
4460 " -sf $bar_color", 0);
4461 setconfspawn("name_workspace", "dmenu"
4465 " -nf $bar_font_color"
4467 " -sf $bar_color", 0);
4471 #define SWM_MODNAME_SIZE 32
4472 #define SWM_KEY_WS "\n+ \t"
4474 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4478 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4479 if (mod == NULL || ks == NULL) {
4480 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4483 if (keystr == NULL || strlen(keystr) == 0) {
4484 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4490 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4491 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4493 cp += (long)strspn(cp, SWM_KEY_WS);
4494 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4496 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4498 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4500 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4502 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4504 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4506 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4507 *mod |= ControlMask;
4509 *ks = XStringToKeysym(name);
4510 XConvertCase(*ks, ks, &uks);
4511 if (ks == NoSymbol) {
4513 "parsekeys: invalid key %s\n",
4519 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4524 strdupsafe(char *str)
4529 return (strdup(str));
4533 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4537 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4538 keyfuncs[kfid].name, spawn_name);
4540 for (i = 0; i < keys_length; i++) {
4541 if (keys[i].mod == mod && keys[i].keysym == ks) {
4542 if (kfid == kf_invalid) {
4545 "setkeybinding: delete #%d %s\n",
4546 i, keyfuncs[keys[i].funcid].name);
4547 free(keys[i].spawn_name);
4548 j = keys_length - 1;
4552 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4555 /* found: replace */
4557 "setkeybinding: replace #%d %s %s\n",
4558 i, keyfuncs[keys[i].funcid].name,
4560 free(keys[i].spawn_name);
4562 keys[i].keysym = ks;
4563 keys[i].funcid = kfid;
4564 keys[i].spawn_name = strdupsafe(spawn_name);
4565 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4570 if (kfid == kf_invalid) {
4572 "error: setkeybinding: cannot find mod/key combination");
4573 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4576 /* not found: add */
4577 if (keys_size == 0 || keys == NULL) {
4579 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4580 keys = malloc((size_t)keys_size * sizeof(struct key));
4582 fprintf(stderr, "malloc failed\n");
4586 } else if (keys_length == keys_size) {
4588 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4589 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4591 fprintf(stderr, "realloc failed\n");
4596 if (keys_length < keys_size) {
4598 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4599 j, keyfuncs[kfid].name, spawn_name);
4601 keys[j].keysym = ks;
4602 keys[j].funcid = kfid;
4603 keys[j].spawn_name = strdupsafe(spawn_name);
4605 fprintf(stderr, "keys array problem?\n");
4607 fprintf(stderr, "keys array problem\n");
4611 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4615 setconfbinding(char *selector, char *value, int flags)
4617 enum keyfuncid kfid;
4621 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4622 if (selector == NULL) {
4623 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4624 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4626 setkeybinding(mod, ks, kfid, NULL);
4631 /* search by key function name */
4632 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4633 if (strncasecmp(selector, keyfuncs[kfid].name,
4634 SWM_FUNCNAME_LEN) == 0) {
4635 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4637 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4638 setkeybinding(mod, ks, kfid, NULL);
4644 /* search by custom spawn name */
4645 for (i = 0; i < spawns_length; i++) {
4646 if (strcasecmp(selector, spawns[i].name) == 0) {
4647 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4649 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4650 setkeybinding(mod, ks, kf_spawn_custom,
4657 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4664 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4665 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4666 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4667 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4668 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4669 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4670 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4671 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4672 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4673 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4674 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4675 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4676 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4677 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4678 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4679 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4680 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4681 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4682 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4683 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4684 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4685 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4686 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4687 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4688 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4689 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4690 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4691 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4692 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4693 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4694 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4695 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4696 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4697 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4698 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4699 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4700 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4701 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4702 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4703 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4704 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4705 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4706 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4707 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4708 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4709 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4710 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4711 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4712 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4713 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4714 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4715 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4716 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4717 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4718 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4719 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4720 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4721 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4722 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4723 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4724 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4725 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4726 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4727 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4728 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4729 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4730 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4731 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4732 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4733 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4734 setkeybinding(MODKEY, XK_s, kf_search_win, NULL);
4736 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4745 /* clear all key bindings, if any */
4746 for (i = 0; i < keys_length; i++)
4747 free(keys[i].spawn_name);
4752 setkeymapping(char *selector, char *value, int flags)
4754 char keymapping_file[PATH_MAX];
4755 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4756 if (value[0] == '~')
4757 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4758 pwd->pw_dir, &value[1]);
4760 strlcpy(keymapping_file, value, sizeof keymapping_file);
4762 /* load new key bindings; if it fails, revert to default bindings */
4763 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4767 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4772 updatenumlockmask(void)
4775 XModifierKeymap *modmap;
4777 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4779 modmap = XGetModifierMapping(display);
4780 for (i = 0; i < 8; i++)
4781 for (j = 0; j < modmap->max_keypermod; j++)
4782 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4783 == XKeysymToKeycode(display, XK_Num_Lock))
4784 numlockmask = (1 << i);
4786 XFreeModifiermap(modmap);
4792 unsigned int i, j, k;
4794 unsigned int modifiers[] =
4795 { 0, LockMask, numlockmask, numlockmask | LockMask };
4797 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4798 updatenumlockmask();
4800 for (k = 0; k < ScreenCount(display); k++) {
4801 if (TAILQ_EMPTY(&screens[k].rl))
4803 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4804 for (i = 0; i < keys_length; i++) {
4805 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4806 for (j = 0; j < LENGTH(modifiers); j++)
4807 XGrabKey(display, code,
4808 keys[i].mod | modifiers[j],
4809 screens[k].root, True,
4810 GrabModeAsync, GrabModeAsync);
4816 grabbuttons(struct ws_win *win, int focused)
4819 unsigned int modifiers[] =
4820 { 0, LockMask, numlockmask, numlockmask|LockMask };
4822 updatenumlockmask();
4823 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4825 for (i = 0; i < LENGTH(buttons); i++)
4826 if (buttons[i].action == client_click)
4827 for (j = 0; j < LENGTH(modifiers); j++)
4828 XGrabButton(display, buttons[i].button,
4829 buttons[i].mask | modifiers[j],
4830 win->id, False, BUTTONMASK,
4831 GrabModeAsync, GrabModeSync, None,
4834 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4835 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4838 const char *quirkname[] = {
4839 "NONE", /* config string for "no value" */
4848 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4849 #define SWM_Q_WS "\n|+ \t"
4851 parsequirks(char *qstr, unsigned long *quirk)
4861 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4863 cp += (long)strspn(cp, SWM_Q_WS);
4864 for (i = 0; i < LENGTH(quirkname); i++) {
4865 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4866 DNPRINTF(SWM_D_QUIRK,
4867 "parsequirks: %s\n", name);
4872 *quirk |= 1 << (i-1);
4876 if (i >= LENGTH(quirkname)) {
4877 DNPRINTF(SWM_D_QUIRK,
4878 "parsequirks: invalid quirk [%s]\n", name);
4886 setquirk(const char *class, const char *name, const int quirk)
4891 for (i = 0; i < quirks_length; i++) {
4892 if (!strcmp(quirks[i].class, class) &&
4893 !strcmp(quirks[i].name, name)) {
4896 DNPRINTF(SWM_D_QUIRK,
4897 "setquirk: delete #%d %s:%s\n",
4898 i, quirks[i].class, quirks[i].name);
4899 free(quirks[i].class);
4900 free(quirks[i].name);
4901 j = quirks_length - 1;
4903 quirks[i] = quirks[j];
4907 /* found: replace */
4908 DNPRINTF(SWM_D_QUIRK,
4909 "setquirk: replace #%d %s:%s\n",
4910 i, quirks[i].class, quirks[i].name);
4911 free(quirks[i].class);
4912 free(quirks[i].name);
4913 quirks[i].class = strdup(class);
4914 quirks[i].name = strdup(name);
4915 quirks[i].quirk = quirk;
4922 "error: setquirk: cannot find class/name combination");
4925 /* not found: add */
4926 if (quirks_size == 0 || quirks == NULL) {
4928 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4929 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4930 if (quirks == NULL) {
4931 fprintf(stderr, "setquirk: malloc failed\n");
4935 } else if (quirks_length == quirks_size) {
4937 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4938 quirks = realloc(quirks,
4939 (size_t)quirks_size * sizeof(struct quirk));
4940 if (quirks == NULL) {
4941 fprintf(stderr, "setquirk: realloc failed\n");
4946 if (quirks_length < quirks_size) {
4947 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4948 j = quirks_length++;
4949 quirks[j].class = strdup(class);
4950 quirks[j].name = strdup(name);
4951 quirks[j].quirk = quirk;
4953 fprintf(stderr, "quirks array problem?\n");
4954 if (quirks == NULL) {
4955 fprintf(stderr, "quirks array problem!\n");
4962 setconfquirk(char *selector, char *value, int flags)
4964 char *cp, *class, *name;
4966 unsigned long quirks;
4967 if (selector == NULL)
4969 if ((cp = strchr(selector, ':')) == NULL)
4974 if ((retval = parsequirks(value, &quirks)) == 0)
4975 setquirk(class, name, quirks);
4982 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4983 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4984 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4985 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4986 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4987 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4988 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4989 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4990 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4991 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4992 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4993 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4994 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4997 /* conf file stuff */
4998 #define SWM_CONF_FILE "scrotwm.conf"
5000 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5001 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5002 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5003 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5004 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5005 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5006 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5007 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5008 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5012 setconfvalue(char *selector, char *value, int flags)
5017 case SWM_S_BAR_DELAY:
5018 bar_delay = atoi(value);
5020 case SWM_S_BAR_ENABLED:
5021 bar_enabled = atoi(value);
5023 case SWM_S_BAR_BORDER_WIDTH:
5024 bar_border_width = atoi(value);
5026 case SWM_S_BAR_AT_BOTTOM:
5027 bar_at_bottom = atoi(value);
5029 case SWM_S_BAR_JUSTIFY:
5030 if (!strcmp(value, "left"))
5031 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5032 else if (!strcmp(value, "center"))
5033 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5034 else if (!strcmp(value, "right"))
5035 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5037 errx(1, "invalid bar_justify");
5039 case SWM_S_STACK_ENABLED:
5040 stack_enabled = atoi(value);
5042 case SWM_S_CLOCK_ENABLED:
5043 clock_enabled = atoi(value);
5045 case SWM_S_CLOCK_FORMAT:
5046 #ifndef SWM_DENY_CLOCK_FORMAT
5048 if ((clock_format = strdup(value)) == NULL)
5049 err(1, "setconfvalue: clock_format");
5052 case SWM_S_CYCLE_EMPTY:
5053 cycle_empty = atoi(value);
5055 case SWM_S_CYCLE_VISIBLE:
5056 cycle_visible = atoi(value);
5058 case SWM_S_SS_ENABLED:
5059 ss_enabled = atoi(value);
5061 case SWM_S_TERM_WIDTH:
5062 term_width = atoi(value);
5064 case SWM_S_TITLE_CLASS_ENABLED:
5065 title_class_enabled = atoi(value);
5067 case SWM_S_WINDOW_NAME_ENABLED:
5068 window_name_enabled = atoi(value);
5070 case SWM_S_TITLE_NAME_ENABLED:
5071 title_name_enabled = atoi(value);
5073 case SWM_S_URGENT_ENABLED:
5074 urgent_enabled = atoi(value);
5076 case SWM_S_FOCUS_MODE:
5077 if (!strcmp(value, "default"))
5078 focus_mode = SWM_FOCUS_DEFAULT;
5079 else if (!strcmp(value, "follow_cursor"))
5080 focus_mode = SWM_FOCUS_FOLLOW;
5081 else if (!strcmp(value, "synergy"))
5082 focus_mode = SWM_FOCUS_SYNERGY;
5084 err(1, "focus_mode");
5086 case SWM_S_DISABLE_BORDER:
5087 disable_border = atoi(value);
5089 case SWM_S_BORDER_WIDTH:
5090 border_width = atoi(value);
5092 case SWM_S_BAR_FONT:
5094 if ((bar_fonts[0] = strdup(value)) == NULL)
5095 err(1, "setconfvalue: bar_font");
5097 case SWM_S_BAR_ACTION:
5099 if ((bar_argv[0] = strdup(value)) == NULL)
5100 err(1, "setconfvalue: bar_action");
5102 case SWM_S_SPAWN_TERM:
5103 free(spawn_term[0]);
5104 if ((spawn_term[0] = strdup(value)) == NULL)
5105 err(1, "setconfvalue: spawn_term");
5109 case SWM_S_DIALOG_RATIO:
5110 dialog_ratio = atof(value);
5111 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5114 case SWM_S_VERBOSE_LAYOUT:
5115 verbose_layout = atoi(value);
5116 for (i = 0; layouts[i].l_stack != NULL; i++) {
5118 layouts[i].l_string = fancy_stacker;
5120 layouts[i].l_string = plain_stacker;
5130 setconfmodkey(char *selector, char *value, int flags)
5132 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5133 update_modkey(Mod1Mask);
5134 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5135 update_modkey(Mod2Mask);
5136 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5137 update_modkey(Mod3Mask);
5138 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5139 update_modkey(Mod4Mask);
5146 setconfcolor(char *selector, char *value, int flags)
5148 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5153 setconfregion(char *selector, char *value, int flags)
5155 custom_region(value);
5160 setautorun(char *selector, char *value, int flags)
5170 if (getenv("SWM_STARTED"))
5174 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5175 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
5177 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5178 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
5181 * This is a little intricate
5183 * If the pid already exists we simply reuse it because it means it was
5184 * used before AND not claimed by manage_window. We get away with
5185 * altering it in the parent after INSERT because this can not be a race
5188 while ((ap = strsep(&sp, " \t")) != NULL) {
5191 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5193 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5194 err(1, "setautorun: realloc");
5195 a.argv[argc - 1] = ap;
5198 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5199 err(1, "setautorun: realloc");
5200 a.argv[argc] = NULL;
5202 if ((pid = fork()) == 0) {
5203 spawn(ws_id, &a, 1);
5212 p = calloc(1, sizeof *p);
5215 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5225 setlayout(char *selector, char *value, int flags)
5227 int ws_id, i, x, mg, ma, si, raise;
5228 int st = SWM_V_STACK;
5230 struct workspace *ws;
5232 if (getenv("SWM_STARTED"))
5236 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5237 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5238 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5239 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5242 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5243 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
5245 if (!strcasecmp(s, "vertical"))
5247 else if (!strcasecmp(s, "horizontal"))
5249 else if (!strcasecmp(s, "fullscreen"))
5252 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5253 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5256 for (i = 0; i < ScreenCount(display); i++) {
5257 ws = (struct workspace *)&screens[i].ws;
5258 ws[ws_id].cur_layout = &layouts[st];
5260 ws[ws_id].always_raise = raise;
5261 if (st == SWM_MAX_STACK)
5265 for (x = 0; x < abs(mg); x++) {
5266 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5267 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5268 SWM_ARG_ID_MASTERSHRINK);
5272 for (x = 0; x < abs(ma); x++) {
5273 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5274 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5275 SWM_ARG_ID_MASTERDEL);
5279 for (x = 0; x < abs(si); x++) {
5280 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5281 si >= 0 ? SWM_ARG_ID_STACKINC :
5282 SWM_ARG_ID_STACKDEC);
5290 /* config options */
5291 struct config_option {
5293 int (*func)(char*, char*, int);
5296 struct config_option configopt[] = {
5297 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5298 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5299 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5300 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5301 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5302 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5303 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5304 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5305 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5306 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5307 { "keyboard_mapping", setkeymapping, 0 },
5308 { "bind", setconfbinding, 0 },
5309 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5310 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5311 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5312 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5313 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5314 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5315 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5316 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5317 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5318 { "modkey", setconfmodkey, 0 },
5319 { "program", setconfspawn, 0 },
5320 { "quirk", setconfquirk, 0 },
5321 { "region", setconfregion, 0 },
5322 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5323 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5324 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5325 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5326 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5327 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5328 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5329 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5330 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5331 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5332 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5333 { "autorun", setautorun, 0 },
5334 { "layout", setlayout, 0 },
5339 conf_load(char *filename, int keymapping)
5342 char *line, *cp, *optsub, *optval;
5343 size_t linelen, lineno = 0;
5344 int wordlen, i, optind;
5345 struct config_option *opt;
5347 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
5349 if (filename == NULL) {
5350 fprintf(stderr, "conf_load: no filename\n");
5353 if ((config = fopen(filename, "r")) == NULL) {
5354 warn("conf_load: fopen: %s", filename);
5358 while (!feof(config)) {
5359 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5362 err(1, "%s", filename);
5367 cp += strspn(cp, " \t\n"); /* eat whitespace */
5368 if (cp[0] == '\0') {
5373 /* get config option */
5374 wordlen = strcspn(cp, "=[ \t\n");
5376 warnx("%s: line %zd: no option found",
5381 for (i = 0; i < LENGTH(configopt); i++) {
5382 opt = &configopt[i];
5383 if (!strncasecmp(cp, opt->optname, wordlen) &&
5384 strlen(opt->optname) == wordlen) {
5390 warnx("%s: line %zd: unknown option %.*s",
5391 filename, lineno, wordlen, cp);
5394 if (keymapping && strcmp(opt->optname, "bind")) {
5395 warnx("%s: line %zd: invalid option %.*s",
5396 filename, lineno, wordlen, cp);
5400 cp += strspn(cp, " \t\n"); /* eat whitespace */
5401 /* get [selector] if any */
5405 wordlen = strcspn(cp, "]");
5408 warnx("%s: line %zd: syntax error",
5412 asprintf(&optsub, "%.*s", wordlen, cp);
5415 cp += strspn(cp, "] \t\n"); /* eat trailing */
5417 cp += strspn(cp, "= \t\n"); /* eat trailing */
5419 optval = strdup(cp);
5420 /* call function to deal with it all */
5421 if (configopt[optind].func(optsub, optval,
5422 configopt[optind].funcflags) != 0) {
5423 fprintf(stderr, "%s line %zd: %s\n",
5424 filename, lineno, line);
5425 errx(1, "%s: line %zd: invalid data for %s",
5426 filename, lineno, configopt[optind].optname);
5434 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5440 set_child_transient(struct ws_win *win, Window *trans)
5442 struct ws_win *parent, *w;
5443 XWMHints *wmh = NULL;
5444 struct swm_region *r;
5445 struct workspace *ws;
5447 parent = find_window(win->transient);
5449 parent->child_trans = win;
5451 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5452 " for %lu trans %lu\n", win->id, win->transient);
5454 if (win->hints == NULL) {
5455 fprintf(stderr, "no hints for %lu\n", win->id);
5459 r = root_to_region(win->wa.root);
5461 /* parent doen't exist in our window list */
5462 TAILQ_FOREACH(w, &ws->winlist, entry) {
5466 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5467 fprintf(stderr, "can't get hints for %lu\n",
5472 if (win->hints->window_group != wmh->window_group)
5475 w->child_trans = win;
5476 win->transient = w->id;
5478 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5479 "transient to %lu\n", win->transient);
5489 window_get_pid(Window win)
5491 Atom actual_type_return;
5492 int actual_format_return = 0;
5493 unsigned long nitems_return = 0;
5494 unsigned long bytes_after_return = 0;
5498 unsigned char *prop = NULL;
5500 if (XGetWindowProperty(display, win,
5501 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5502 XA_CARDINAL, &actual_type_return, &actual_format_return,
5503 &nitems_return, &bytes_after_return,
5504 (unsigned char**)(void*)&pid) != Success)
5506 if (actual_type_return != XA_CARDINAL)
5517 if (XGetWindowProperty(display, win,
5518 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5519 XA_STRING, &actual_type_return, &actual_format_return,
5520 &nitems_return, &bytes_after_return, &prop) != Success)
5522 if (actual_type_return != XA_STRING)
5527 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5528 /* ignore error because strtonum returns 0 anyway */
5535 manage_window(Window id)
5538 struct workspace *ws;
5539 struct ws_win *win, *ww;
5540 int format, i, ws_idx, n, border_me = 0;
5541 unsigned long nitems, bytes;
5542 Atom ws_idx_atom = 0, type;
5543 Atom *prot = NULL, *pp;
5544 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5545 struct swm_region *r;
5551 if ((win = find_window(id)) != NULL)
5552 return (win); /* already being managed */
5554 /* see if we are on the unmanaged list */
5555 if ((win = find_unmanaged_window(id)) != NULL) {
5556 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5558 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5559 if (win->transient) {
5560 set_child_transient(win, &trans);
5561 } if (trans && (ww = find_window(trans)))
5562 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5564 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5565 ewmh_update_actions(win);
5569 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5570 errx(1, "calloc: failed to allocate memory for new window");
5574 /* see if we need to override the workspace */
5575 p = find_pid(window_get_pid(id));
5577 /* Get all the window data in one shot */
5578 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5580 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5581 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5583 XGetWindowAttributes(display, id, &win->wa);
5584 XGetWMNormalHints(display, id, &win->sh, &mask);
5585 win->hints = XGetWMHints(display, id);
5586 XGetTransientForHint(display, id, &trans);
5588 win->transient = trans;
5589 set_child_transient(win, &trans);
5590 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5591 win->id, win->transient);
5594 /* get supported protocols */
5595 if (XGetWMProtocols(display, id, &prot, &n)) {
5596 for (i = 0, pp = prot; i < n; i++, pp++) {
5597 if (*pp == takefocus)
5598 win->take_focus = 1;
5600 win->can_delete = 1;
5606 win->iconic = get_iconic(win);
5609 * Figure out where to put the window. If it was previously assigned to
5610 * a workspace (either by spawn() or manually moving), and isn't
5611 * transient, * put it in the same workspace
5613 r = root_to_region(win->wa.root);
5615 ws = &r->s->ws[p->ws];
5616 TAILQ_REMOVE(&pidlist, p, entry);
5619 } else if (prop && win->transient == 0) {
5620 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5621 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5623 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5626 ws = &r->s->ws[ws_idx];
5629 /* this should launch transients in the same ws as parent */
5631 if ((ww = find_window(trans)) != NULL)
5638 "fix this bug mcbride\n");
5643 /* set up the window layout */
5646 win->s = r->s; /* this never changes */
5647 if (trans && (ww = find_window(trans)))
5648 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5650 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5652 win->g.w = win->wa.width;
5653 win->g.h = win->wa.height;
5654 win->g.x = win->wa.x;
5655 win->g.y = win->wa.y;
5656 win->g_floatvalid = 0;
5657 win->floatmaxed = 0;
5658 win->ewmh_flags = 0;
5660 /* Set window properties so we can remember this after reincarnation */
5661 if (ws_idx_atom && prop == NULL &&
5662 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5664 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5666 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5667 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5672 ewmh_autoquirk(win);
5674 if (XGetClassHint(display, win->id, &win->ch)) {
5675 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5676 win->ch.res_class, win->ch.res_name);
5678 /* java is retarded so treat it special */
5679 if (strstr(win->ch.res_name, "sun-awt")) {
5684 for (i = 0; i < quirks_length; i++){
5685 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5686 !strcmp(win->ch.res_name, quirks[i].name)) {
5687 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5688 win->ch.res_class, win->ch.res_name);
5689 if (quirks[i].quirk & SWM_Q_FLOAT) {
5693 win->quirks = quirks[i].quirk;
5698 /* alter window position if quirky */
5699 if (win->quirks & SWM_Q_ANYWHERE) {
5700 win->manual = 1; /* don't center the quirky windows */
5701 bzero(&wc, sizeof wc);
5703 if (bar_enabled && win->g.y < bar_height) {
5704 win->g.y = wc.y = bar_height;
5707 if (win->g.w + win->g.x > WIDTH(r)) {
5708 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5714 /* Reset font sizes (the bruteforce way; no default keybinding). */
5715 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5716 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5717 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5718 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5719 fake_keypress(win, XK_KP_Add, ShiftMask);
5722 ewmh_get_win_state(win);
5723 ewmh_update_actions(win);
5724 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5728 bzero(&wc, sizeof wc);
5729 wc.border_width = border_width;
5730 mask = CWBorderWidth;
5731 XConfigureWindow(display, win->id, mask, &wc);
5734 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5735 PropertyChangeMask | StructureNotifyMask);
5737 /* floaters need to be mapped if they are in the current workspace */
5738 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5739 XMapRaised(display, win->id);
5745 free_window(struct ws_win *win)
5747 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5752 /* needed for restart wm */
5753 set_win_state(win, WithdrawnState);
5755 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5757 if (win->ch.res_class)
5758 XFree(win->ch.res_class);
5759 if (win->ch.res_name)
5760 XFree(win->ch.res_name);
5765 memset(win, 0xff, sizeof *win); /* XXX kill later */
5771 unmanage_window(struct ws_win *win)
5773 struct ws_win *parent;
5778 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5780 if (win->transient) {
5781 parent = find_window(win->transient);
5783 parent->child_trans = NULL;
5786 /* focus on root just in case */
5787 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5791 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5792 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5798 focus_magic(struct ws_win *win)
5800 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5805 if (win->child_trans) {
5806 /* win = parent & has a transient so focus on that */
5808 focus_win(win->child_trans);
5809 if (win->child_trans->take_focus)
5810 client_msg(win, takefocus);
5812 /* make sure transient hasn't dissapeared */
5813 if (validate_win(win->child_trans) == 0) {
5814 focus_win(win->child_trans);
5815 if (win->child_trans->take_focus)
5816 client_msg(win->child_trans, takefocus);
5818 win->child_trans = NULL;
5820 if (win->take_focus)
5821 client_msg(win, takefocus);
5827 if (win->take_focus)
5828 client_msg(win, takefocus);
5835 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5843 XKeyEvent *ev = &e->xkey;
5845 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5847 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5848 for (i = 0; i < keys_length; i++)
5849 if (keysym == keys[i].keysym
5850 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5851 && keyfuncs[keys[i].funcid].func) {
5852 if (keys[i].funcid == kf_spawn_custom)
5854 root_to_region(ev->root),
5855 &(keyfuncs[keys[i].funcid].args),
5859 keyfuncs[keys[i].funcid].func(
5860 root_to_region(ev->root),
5861 &(keyfuncs[keys[i].funcid].args)
5867 buttonpress(XEvent *e)
5871 XButtonPressedEvent *ev = &e->xbutton;
5873 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5875 if ((win = find_window(ev->window)) == NULL)
5879 action = client_click;
5881 for (i = 0; i < LENGTH(buttons); i++)
5882 if (action == buttons[i].action && buttons[i].func &&
5883 buttons[i].button == ev->button &&
5884 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5885 buttons[i].func(win, &buttons[i].args);
5889 configurerequest(XEvent *e)
5891 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5896 if ((win = find_window(ev->window)) == NULL)
5897 if ((win = find_unmanaged_window(ev->window)) == NULL)
5901 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5903 bzero(&wc, sizeof wc);
5906 wc.width = ev->width;
5907 wc.height = ev->height;
5908 wc.border_width = ev->border_width;
5909 wc.sibling = ev->above;
5910 wc.stack_mode = ev->detail;
5911 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5913 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5915 config_win(win, ev);
5920 configurenotify(XEvent *e)
5925 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5926 e->xconfigure.window);
5928 win = find_window(e->xconfigure.window);
5930 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5934 if (focus_mode == SWM_FOCUS_DEFAULT)
5935 drain_enter_notify();
5940 destroynotify(XEvent *e)
5943 XDestroyWindowEvent *ev = &e->xdestroywindow;
5945 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5947 if ((win = find_window(ev->window)) == NULL) {
5948 if ((win = find_unmanaged_window(ev->window)) == NULL)
5954 /* make sure we focus on something */
5957 unmanage_window(win);
5959 if (focus_mode == SWM_FOCUS_DEFAULT)
5960 drain_enter_notify();
5965 enternotify(XEvent *e)
5967 XCrossingEvent *ev = &e->xcrossing;
5972 Window focus_return;
5973 int revert_to_return;
5975 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5976 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5977 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5978 ev->same_screen, ev->focus, ev->state);
5980 switch (focus_mode) {
5981 case SWM_FOCUS_DEFAULT:
5983 case SWM_FOCUS_FOLLOW:
5985 case SWM_FOCUS_SYNERGY:
5988 * all these checks need to be in this order because the
5989 * XCheckTypedWindowEvent relies on weeding out the previous events
5991 * making this code an option would enable a follow mouse for focus
5996 * state is set when we are switching workspaces and focus is set when
5997 * the window or a subwindow already has focus (occurs during restart).
5999 * Only honor the focus flag if last_focus_event is not FocusOut,
6000 * this allows scrotwm to continue to control focus when another
6001 * program is also playing with it.
6003 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6004 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6009 * happens when a window is created or destroyed and the border
6010 * crosses the mouse pointer and when switching ws
6012 * we need the subwindow test to see if we came from root in order
6013 * to give focus to floaters
6015 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6016 ev->subwindow == 0) {
6017 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6021 /* this window already has focus */
6022 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6023 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6027 /* this window is being deleted or moved to another ws */
6028 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6030 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6031 XPutBackEvent(display, &cne);
6035 if ((win = find_window(ev->window)) == NULL) {
6036 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6041 * In fullstack kill all enters unless they come from a different ws
6042 * (i.e. another region) or focus has been grabbed externally.
6044 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6045 last_focus_event != FocusOut) {
6046 XGetInputFocus(display, &focus_return, &revert_to_return);
6047 if ((w = find_window(focus_return)) == NULL ||
6049 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6057 if ((win = find_window(ev->window)) == NULL) {
6058 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6063 * if we have more enternotifies let them handle it in due time
6065 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6066 DNPRINTF(SWM_D_EVENT,
6067 "ignoring enternotify: got more enternotify\n");
6068 XPutBackEvent(display, &cne);
6075 /* lets us use one switch statement for arbitrary mode/detail combinations */
6076 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6079 focusevent(XEvent *e)
6083 u_int32_t mode_detail;
6084 XFocusChangeEvent *ev = &e->xfocus;
6086 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
6087 ev->type == FocusIn ? "entering" : "leaving",
6088 ev->window, ev->mode, ev->detail);
6090 if (last_focus_event == ev->type) {
6091 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6095 last_focus_event = ev->type;
6096 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6098 switch (mode_detail) {
6099 /* synergy client focus operations */
6100 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6101 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6103 /* synergy server focus operations */
6104 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6106 /* Entering applications like rdesktop that mangle the pointer */
6107 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6109 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6110 XSetWindowBorder(display, win->id,
6111 win->ws->r->s->c[ev->type == FocusIn ?
6112 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6115 fprintf(stderr, "ignoring focusevent\n");
6116 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6123 mapnotify(XEvent *e)
6126 XMapEvent *ev = &e->xmap;
6128 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
6130 win = manage_window(ev->window);
6132 set_win_state(win, NormalState);
6136 mappingnotify(XEvent *e)
6138 XMappingEvent *ev = &e->xmapping;
6140 XRefreshKeyboardMapping(ev);
6141 if (ev->request == MappingKeyboard)
6146 maprequest(XEvent *e)
6149 struct swm_region *r;
6150 XWindowAttributes wa;
6151 XMapRequestEvent *ev = &e->xmaprequest;
6153 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
6154 e->xmaprequest.window);
6156 if (!XGetWindowAttributes(display, ev->window, &wa))
6158 if (wa.override_redirect)
6161 win = manage_window(e->xmaprequest.window);
6163 return; /* can't happen */
6167 /* make new win focused */
6168 r = root_to_region(win->wa.root);
6169 if (win->ws == r->ws)
6174 propertynotify(XEvent *e)
6177 XPropertyEvent *ev = &e->xproperty;
6179 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
6182 win = find_window(ev->window);
6186 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6187 update_iconic(win, 0);
6188 XMapRaised(display, win->id);
6195 case XA_WM_NORMAL_HINTS:
6198 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6199 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6200 if (win->sh.flags & PMinSize) {
6201 win->g.w = win->sh.min_width;
6202 win->g.h = win->sh.min_height;
6203 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
6205 XMoveResizeWindow(display, win->id,
6206 win->g.x, win->g.y, win->g.w, win->g.h);
6208 if (window_name_enabled)
6217 unmapnotify(XEvent *e)
6221 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
6223 /* determine if we need to help unmanage this window */
6224 win = find_window(e->xunmap.window);
6228 if (getstate(e->xunmap.window) == NormalState) {
6229 unmanage_window(win);
6232 /* giant hack for apps that don't destroy transient windows */
6233 /* eat a bunch of events to prevent remanaging the window */
6235 while (XCheckWindowEvent(display, e->xunmap.window,
6236 EnterWindowMask, &cne))
6238 while (XCheckWindowEvent(display, e->xunmap.window,
6239 StructureNotifyMask, &cne))
6241 while (XCheckWindowEvent(display, e->xunmap.window,
6242 SubstructureNotifyMask, &cne))
6244 /* resend unmap because we ated it */
6245 XUnmapWindow(display, e->xunmap.window);
6248 if (focus_mode == SWM_FOCUS_DEFAULT)
6249 drain_enter_notify();
6253 visibilitynotify(XEvent *e)
6256 struct swm_region *r;
6258 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
6259 e->xvisibility.window);
6260 if (e->xvisibility.state == VisibilityUnobscured)
6261 for (i = 0; i < ScreenCount(display); i++)
6262 TAILQ_FOREACH(r, &screens[i].rl, entry)
6263 if (e->xvisibility.window == r->bar_window)
6268 clientmessage(XEvent *e)
6270 XClientMessageEvent *ev;
6275 win = find_window(ev->window);
6279 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
6280 ev->window, ev->message_type);
6282 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6283 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
6286 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6287 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
6288 if (win->can_delete)
6289 client_msg(win, adelete);
6291 XKillClient(display, win->id);
6293 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6294 DNPRINTF(SWM_D_EVENT,
6295 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
6296 if (win->floating) {
6297 if (ev->data.l[0] & (1<<8)) /* x */
6298 win->g.x = ev->data.l[1];
6299 if (ev->data.l[0] & (1<<9)) /* y */
6300 win->g.y = ev->data.l[2];
6301 if (ev->data.l[0] & (1<<10)) /* width */
6302 win->g.w = ev->data.l[3];
6303 if (ev->data.l[0] & (1<<11)) /* height */
6304 win->g.h = ev->data.l[4];
6307 /* TODO: Change stack sizes */
6309 config_win(win, NULL);
6311 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6312 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
6313 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6315 ewmh_update_win_state(win, ev->data.l[2],
6323 xerror_start(Display *d, XErrorEvent *ee)
6330 xerror(Display *d, XErrorEvent *ee)
6332 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6340 xerrorxlib = XSetErrorHandler(xerror_start);
6342 /* this causes an error if some other window manager is running */
6343 XSelectInput(display, DefaultRootWindow(display),
6344 SubstructureRedirectMask);
6345 XSync(display, False);
6349 XSetErrorHandler(xerror);
6350 XSync(display, False);
6355 new_region(struct swm_screen *s, int x, int y, int w, int h)
6357 struct swm_region *r, *n;
6358 struct workspace *ws = NULL;
6361 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6362 s->idx, w, h, x, y);
6364 /* remove any conflicting regions */
6365 n = TAILQ_FIRST(&s->rl);
6368 n = TAILQ_NEXT(r, entry);
6369 if (X(r) < (x + w) &&
6370 (X(r) + WIDTH(r)) > x &&
6372 (Y(r) + HEIGHT(r)) > y) {
6373 if (r->ws->r != NULL)
6374 r->ws->old_r = r->ws->r;
6376 XDestroyWindow(display, r->bar_window);
6377 TAILQ_REMOVE(&s->rl, r, entry);
6378 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6382 /* search old regions for one to reuse */
6384 /* size + location match */
6385 TAILQ_FOREACH(r, &s->orl, entry)
6386 if (X(r) == x && Y(r) == y &&
6387 HEIGHT(r) == h && WIDTH(r) == w)
6391 TAILQ_FOREACH(r, &s->orl, entry)
6392 if (HEIGHT(r) == h && WIDTH(r) == w)
6396 TAILQ_REMOVE(&s->orl, r, entry);
6397 /* try to use old region's workspace */
6398 if (r->ws->r == NULL)
6401 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6402 errx(1, "calloc: failed to allocate memory for screen");
6404 /* if we don't have a workspace already, find one */
6406 for (i = 0; i < SWM_WS_MAX; i++)
6407 if (s->ws[i].r == NULL) {
6414 errx(1, "no free workspaces\n");
6425 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6431 #ifdef SWM_XRR_HAS_CRTC
6433 XRRScreenResources *sr;
6436 #endif /* SWM_XRR_HAS_CRTC */
6437 struct swm_region *r;
6440 if (i >= ScreenCount(display))
6441 errx(1, "invalid screen");
6443 /* remove any old regions */
6444 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6445 r->ws->old_r = r->ws->r = NULL;
6446 XDestroyWindow(display, r->bar_window);
6447 TAILQ_REMOVE(&screens[i].rl, r, entry);
6448 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6452 /* map virtual screens onto physical screens */
6453 #ifdef SWM_XRR_HAS_CRTC
6454 if (xrandr_support) {
6455 sr = XRRGetScreenResources(display, screens[i].root);
6457 new_region(&screens[i], 0, 0,
6458 DisplayWidth(display, i),
6459 DisplayHeight(display, i));
6463 for (c = 0, ci = NULL; c < ncrtc; c++) {
6464 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6465 if (ci->noutput == 0)
6468 if (ci != NULL && ci->mode == None)
6469 new_region(&screens[i], 0, 0,
6470 DisplayWidth(display, i),
6471 DisplayHeight(display, i));
6473 new_region(&screens[i],
6474 ci->x, ci->y, ci->width, ci->height);
6477 XRRFreeCrtcInfo(ci);
6478 XRRFreeScreenResources(sr);
6480 #endif /* SWM_XRR_HAS_CRTC */
6482 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6483 DisplayHeight(display, i));
6488 screenchange(XEvent *e) {
6489 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6490 struct swm_region *r;
6493 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6495 if (!XRRUpdateConfiguration(e))
6498 /* silly event doesn't include the screen index */
6499 for (i = 0; i < ScreenCount(display); i++)
6500 if (screens[i].root == xe->root)
6502 if (i >= ScreenCount(display))
6503 errx(1, "screenchange: screen not found\n");
6505 /* brute force for now, just re-enumerate the regions */
6508 /* add bars to all regions */
6509 for (i = 0; i < ScreenCount(display); i++)
6510 TAILQ_FOREACH(r, &screens[i].rl, entry)
6513 if (focus_mode == SWM_FOCUS_DEFAULT)
6514 drain_enter_notify();
6520 Window d1, d2, *wins = NULL;
6521 XWindowAttributes wa;
6526 for (i = 0; i < ScreenCount(display); i++) {
6527 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6530 /* attach windows to a region */
6531 /* normal windows */
6532 for (j = 0; j < no; j++) {
6533 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6534 wa.override_redirect ||
6535 XGetTransientForHint(display, wins[j], &d1))
6538 state = getstate(wins[j]);
6539 manage = state == IconicState;
6540 if (wa.map_state == IsViewable || manage)
6541 manage_window(wins[j]);
6543 /* transient windows */
6544 for (j = 0; j < no; j++) {
6545 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6546 wa.override_redirect)
6549 state = getstate(wins[j]);
6550 manage = state == IconicState;
6551 if (XGetTransientForHint(display, wins[j], &d1) &&
6553 manage_window(wins[j]);
6566 int errorbase, major, minor;
6567 struct workspace *ws;
6569 if ((screens = calloc(ScreenCount(display),
6570 sizeof(struct swm_screen))) == NULL)
6571 errx(1, "calloc: screens");
6573 /* initial Xrandr setup */
6574 xrandr_support = XRRQueryExtension(display,
6575 &xrandr_eventbase, &errorbase);
6577 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6580 /* map physical screens */
6581 for (i = 0; i < ScreenCount(display); i++) {
6582 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6584 TAILQ_INIT(&screens[i].rl);
6585 TAILQ_INIT(&screens[i].orl);
6586 screens[i].root = RootWindow(display, i);
6588 /* set default colors */
6589 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6590 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6591 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6592 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6593 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6595 /* set default cursor */
6596 XDefineCursor(display, screens[i].root,
6597 XCreateFontCursor(display, XC_left_ptr));
6599 /* init all workspaces */
6600 /* XXX these should be dynamically allocated too */
6601 for (j = 0; j < SWM_WS_MAX; j++) {
6602 ws = &screens[i].ws[j];
6608 TAILQ_INIT(&ws->winlist);
6609 TAILQ_INIT(&ws->unmanagedlist);
6611 for (k = 0; layouts[k].l_stack != NULL; k++)
6612 if (layouts[k].l_config != NULL)
6613 layouts[k].l_config(ws,
6614 SWM_ARG_ID_STACKINIT);
6615 ws->cur_layout = &layouts[0];
6616 ws->cur_layout->l_string(ws);
6622 XRRSelectInput(display, screens[i].root,
6623 RRScreenChangeNotifyMask);
6630 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6632 err(1, "setup_globals: strdup");
6633 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6635 err(1, "setup_globals: strdup");
6636 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6638 err(1, "setup_globals: strdup");
6639 if ((spawn_term[0] = strdup("xterm")) == NULL)
6640 err(1, "setup_globals: strdup");
6641 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6649 Atom netwmcheck, netwmname, utf8_string;
6652 /* work around sun jdk bugs, code from wmname */
6653 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6654 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6655 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6656 for (i = 0; i < ScreenCount(display); i++) {
6657 root = screens[i].root;
6658 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6659 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6660 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6662 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6663 PropModeReplace, (unsigned char *)&win,1);
6664 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6665 PropModeReplace, (unsigned char *)&win,1);
6666 XChangeProperty(display, win, netwmname, utf8_string, 8,
6667 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6672 main(int argc, char *argv[])
6674 struct swm_region *r, *rr;
6675 struct ws_win *winfocus = NULL;
6678 char conf[PATH_MAX], *cfile = NULL;
6683 struct sigaction sact;
6686 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6687 SCROTWM_VERSION, buildstr);
6688 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6690 warnx("no locale support");
6692 if (!(display = XOpenDisplay(0)))
6693 errx(1, "can not open display");
6696 errx(1, "other wm running");
6698 /* handle some signals */
6699 bzero(&sact, sizeof(sact));
6700 sigemptyset(&sact.sa_mask);
6702 sact.sa_handler = sighdlr;
6703 sigaction(SIGINT, &sact, NULL);
6704 sigaction(SIGQUIT, &sact, NULL);
6705 sigaction(SIGTERM, &sact, NULL);
6706 sigaction(SIGHUP, &sact, NULL);
6708 sact.sa_handler = sighdlr;
6709 sact.sa_flags = SA_NOCLDSTOP;
6710 sigaction(SIGCHLD, &sact, NULL);
6712 astate = XInternAtom(display, "WM_STATE", False);
6713 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6714 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6715 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6716 a_wmname = XInternAtom(display, "WM_NAME", False);
6717 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6718 a_string = XInternAtom(display, "STRING", False);
6719 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6721 /* look for local and global conf file */
6722 pwd = getpwuid(getuid());
6724 errx(1, "invalid user %d", getuid());
6733 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6734 if (stat(conf, &sb) != -1) {
6735 if (S_ISREG(sb.st_mode))
6738 /* try global conf file */
6739 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6740 if (!stat(conf, &sb))
6741 if (S_ISREG(sb.st_mode))
6745 conf_load(cfile, SWM_CONF_DEFAULT);
6748 /* set some values to work around bad programs */
6750 /* grab existing windows (before we build the bars) */
6753 if (getenv("SWM_STARTED") == NULL)
6754 setenv("SWM_STARTED", "YES", 1);
6756 /* setup all bars */
6757 for (i = 0; i < ScreenCount(display); i++)
6758 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6759 if (winfocus == NULL)
6760 winfocus = TAILQ_FIRST(&r->ws->winlist);
6768 if (focus_mode == SWM_FOCUS_DEFAULT)
6769 drain_enter_notify();
6771 xfd = ConnectionNumber(display);
6773 while (XPending(display)) {
6774 XNextEvent(display, &e);
6777 if (e.type < LASTEvent) {
6779 if (handler[e.type])
6780 handler[e.type](&e);
6782 DNPRINTF(SWM_D_EVENT,
6783 "win: %lu unknown event: %d\n",
6784 e.xany.window, e.type);
6786 switch (e.type - xrandr_eventbase) {
6787 case RRScreenChangeNotify:
6791 DNPRINTF(SWM_D_EVENT,
6792 "win: %lu unknown xrandr event: "
6793 "%d\n", e.xany.window, e.type);
6799 /* if we are being restarted go focus on first window */
6801 rr = winfocus->ws->r;
6803 /* not a visible window */
6807 /* move pointer to first screen if multi screen */
6808 if (ScreenCount(display) > 1 || outputs > 1)
6809 XWarpPointer(display, None, rr->s[0].root,
6810 0, 0, 0, 0, rr->g.x,
6811 rr->g.y + (bar_enabled ? bar_height : 0));
6813 a.id = SWM_ARG_ID_FOCUSCUR;
6823 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6825 DNPRINTF(SWM_D_MISC, "select failed");
6826 if (restart_wm == 1)
6827 restart(NULL, NULL);
6828 if (search_resp == 1)
6840 XFreeGC(display, bar_gc);
6841 XCloseDisplay(display);