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"
185 volatile sig_atomic_t running = 1;
186 volatile sig_atomic_t restart_wm = 0;
188 int last_focus_event = FocusOut;
189 int (*xerrorxlib)(Display *, XErrorEvent *);
193 int xrandr_eventbase;
194 unsigned int numlockmask = 0;
198 int cycle_visible = 0;
200 int font_adjusted = 0;
201 unsigned int mod_key = MODKEY;
204 struct swm_region *search_r;
205 int select_list_pipe[2];
206 int select_resp_pipe[2];
208 volatile sig_atomic_t search_resp;
209 int search_resp_action;
211 struct search_window {
212 TAILQ_ENTRY(search_window) entry;
217 TAILQ_HEAD(search_winlist, search_window);
219 struct search_winlist search_wl;
224 SWM_SEARCH_UNICONIFY,
225 SWM_SEARCH_NAME_WORKSPACE,
226 SWM_SEARCH_SEARCH_WORKSPACE,
227 SWM_SEARCH_SEARCH_WINDOW
231 double dialog_ratio = .6;
233 #define SWM_BAR_MAX (256)
234 #define SWM_BAR_JUSTIFY_LEFT (0)
235 #define SWM_BAR_JUSTIFY_CENTER (1)
236 #define SWM_BAR_JUSTIFY_RIGHT (2)
237 #define SWM_BAR_OFFSET (4)
238 char *bar_argv[] = { NULL, NULL };
240 char bar_ext[SWM_BAR_MAX];
241 char bar_vertext[SWM_BAR_MAX];
243 sig_atomic_t bar_alarm = 0;
246 int bar_border_width = 1;
247 int bar_at_bottom = 0;
249 int bar_extra_running = 0;
252 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
253 int stack_enabled = 1;
254 int clock_enabled = 1;
255 int urgent_enabled = 0;
256 char *clock_format = NULL;
257 int title_name_enabled = 0;
258 int title_class_enabled = 0;
259 int window_name_enabled = 0;
260 int focus_mode = SWM_FOCUS_DEFAULT;
261 int disable_border = 0;
262 int border_width = 1;
263 int verbose_layout = 0;
269 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
270 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
273 #define SWM_MENU_FN (2)
274 #define SWM_MENU_NB (4)
275 #define SWM_MENU_NF (6)
276 #define SWM_MENU_SB (8)
277 #define SWM_MENU_SF (10)
279 /* layout manager data */
280 struct swm_geometry {
290 /* virtual "screens" */
292 TAILQ_ENTRY(swm_region) entry;
293 struct swm_geometry g;
294 struct workspace *ws; /* current workspace on this region */
295 struct workspace *ws_prior; /* prior workspace on this region */
296 struct swm_screen *s; /* screen idx */
299 TAILQ_HEAD(swm_region_list, swm_region);
302 TAILQ_ENTRY(ws_win) entry;
305 struct ws_win *child_trans; /* transient child window */
306 struct swm_geometry g; /* current geometry */
307 struct swm_geometry g_float; /* geometry when floating */
308 struct swm_geometry rg_float; /* region geom when floating */
309 int g_floatvalid; /* flag: geometry in g_float is valid */
310 int floatmaxed; /* flag: floater was maxed in max_stack */
314 unsigned int ewmh_flags;
315 int font_size_boundary[SWM_MAX_FONT_STEPS];
321 unsigned long quirks;
322 struct workspace *ws; /* always valid */
323 struct swm_screen *s; /* always valid, never changes */
324 XWindowAttributes wa;
329 TAILQ_HEAD(ws_win_list, ws_win);
333 TAILQ_ENTRY(pid_e) entry;
337 TAILQ_HEAD(pid_list, pid_e);
338 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
340 /* layout handlers */
342 void vertical_config(struct workspace *, int);
343 void vertical_stack(struct workspace *, struct swm_geometry *);
344 void horizontal_config(struct workspace *, int);
345 void horizontal_stack(struct workspace *, struct swm_geometry *);
346 void max_stack(struct workspace *, struct swm_geometry *);
347 void plain_stacker(struct workspace *);
348 void fancy_stacker(struct workspace *);
350 struct ws_win *find_window(Window);
352 void grabbuttons(struct ws_win *, int);
353 void new_region(struct swm_screen *, int, int, int, int);
354 void unmanage_window(struct ws_win *);
355 long getstate(Window);
357 int conf_load(char *, int);
360 void (*l_stack)(struct workspace *, struct swm_geometry *);
361 void (*l_config)(struct workspace *, int);
363 #define SWM_L_FOCUSPREV (1<<0)
364 #define SWM_L_MAPONFOCUS (1<<1)
365 void (*l_string)(struct workspace *);
367 /* stack, configure */
368 { vertical_stack, vertical_config, 0, plain_stacker },
369 { horizontal_stack, horizontal_config, 0, plain_stacker },
371 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
372 { NULL, NULL, 0, NULL },
375 /* position of max_stack mode in the layouts array, index into layouts! */
376 #define SWM_V_STACK (0)
377 #define SWM_H_STACK (1)
378 #define SWM_MAX_STACK (2)
380 #define SWM_H_SLICE (32)
381 #define SWM_V_SLICE (32)
383 /* define work spaces */
385 int idx; /* workspace index */
386 char *name; /* workspace name */
387 int always_raise; /* raise windows on focus */
388 struct layout *cur_layout; /* current layout handlers */
389 struct ws_win *focus; /* may be NULL */
390 struct ws_win *focus_prev; /* may be NULL */
391 struct swm_region *r; /* may be NULL */
392 struct swm_region *old_r; /* may be NULL */
393 struct ws_win_list winlist; /* list of windows in ws */
394 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
395 char stacker[10]; /* display stacker and layout */
399 int horizontal_msize;
401 int horizontal_stacks;
408 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
409 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
411 /* physical screen mapping */
412 #define SWM_WS_MAX (10)
414 int idx; /* screen index */
415 struct swm_region_list rl; /* list of regions on this screen */
416 struct swm_region_list orl; /* list of old regions */
418 struct workspace ws[SWM_WS_MAX];
424 } c[SWM_S_COLOR_MAX];
426 struct swm_screen *screens;
429 /* args to functions */
432 #define SWM_ARG_ID_FOCUSNEXT (0)
433 #define SWM_ARG_ID_FOCUSPREV (1)
434 #define SWM_ARG_ID_FOCUSMAIN (2)
435 #define SWM_ARG_ID_FOCUSCUR (4)
436 #define SWM_ARG_ID_SWAPNEXT (10)
437 #define SWM_ARG_ID_SWAPPREV (11)
438 #define SWM_ARG_ID_SWAPMAIN (12)
439 #define SWM_ARG_ID_MOVELAST (13)
440 #define SWM_ARG_ID_MASTERSHRINK (20)
441 #define SWM_ARG_ID_MASTERGROW (21)
442 #define SWM_ARG_ID_MASTERADD (22)
443 #define SWM_ARG_ID_MASTERDEL (23)
444 #define SWM_ARG_ID_STACKRESET (30)
445 #define SWM_ARG_ID_STACKINIT (31)
446 #define SWM_ARG_ID_CYCLEWS_UP (40)
447 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
448 #define SWM_ARG_ID_CYCLESC_UP (42)
449 #define SWM_ARG_ID_CYCLESC_DOWN (43)
450 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
451 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
452 #define SWM_ARG_ID_STACKINC (50)
453 #define SWM_ARG_ID_STACKDEC (51)
454 #define SWM_ARG_ID_SS_ALL (60)
455 #define SWM_ARG_ID_SS_WINDOW (61)
456 #define SWM_ARG_ID_DONTCENTER (70)
457 #define SWM_ARG_ID_CENTER (71)
458 #define SWM_ARG_ID_KILLWINDOW (80)
459 #define SWM_ARG_ID_DELETEWINDOW (81)
460 #define SWM_ARG_ID_WIDTHGROW (90)
461 #define SWM_ARG_ID_WIDTHSHRINK (91)
462 #define SWM_ARG_ID_HEIGHTGROW (92)
463 #define SWM_ARG_ID_HEIGHTSHRINK (93)
464 #define SWM_ARG_ID_MOVEUP (100)
465 #define SWM_ARG_ID_MOVEDOWN (101)
466 #define SWM_ARG_ID_MOVELEFT (102)
467 #define SWM_ARG_ID_MOVERIGHT (103)
471 void focus(struct swm_region *, union arg *);
472 void focus_magic(struct ws_win *);
479 #define SWM_Q_FLOAT (1<<0) /* float this window */
480 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
481 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
482 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
483 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
484 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
486 int quirks_size = 0, quirks_length = 0;
487 struct quirk *quirks = NULL;
490 * Supported EWMH hints should be added to
491 * both the enum and the ewmh array
493 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
494 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
495 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
496 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
497 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
498 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
499 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
500 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
501 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
502 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
508 } ewmh[SWM_EWMH_HINT_MAX] = {
509 /* must be in same order as in the enum */
510 {"_NET_ACTIVE_WINDOW", None},
511 {"_NET_MOVERESIZE_WINDOW", None},
512 {"_NET_CLOSE_WINDOW", None},
513 {"_NET_WM_WINDOW_TYPE", None},
514 {"_NET_WM_WINDOW_TYPE_DOCK", None},
515 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
516 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
517 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
518 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
519 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
520 {"_NET_WM_STATE", None},
521 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
522 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
523 {"_NET_WM_STATE_SKIP_TASKBAR", None},
524 {"_NET_WM_STATE_SKIP_PAGER", None},
525 {"_NET_WM_STATE_HIDDEN", None},
526 {"_NET_WM_STATE_ABOVE", None},
527 {"_SWM_WM_STATE_MANUAL", None},
528 {"_NET_WM_STATE_FULLSCREEN", None},
529 {"_NET_WM_ALLOWED_ACTIONS", None},
530 {"_NET_WM_ACTION_MOVE", None},
531 {"_NET_WM_ACTION_RESIZE", None},
532 {"_NET_WM_ACTION_FULLSCREEN", None},
533 {"_NET_WM_ACTION_CLOSE", None},
536 void store_float_geom(struct ws_win *win, struct swm_region *r);
537 int floating_toggle_win(struct ws_win *win);
538 void spawn_select(struct swm_region *, union arg *, char *, int *);
541 get_property(Window id, Atom atom, long count, Atom type,
542 unsigned long *n, unsigned char **data)
545 unsigned long tmp, extra;
546 unsigned long *nitems;
549 nitems = n != NULL ? n : &tmp;
550 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
551 &real, &format, nitems, &extra, data);
553 if (status != Success)
562 update_iconic(struct ws_win *win, int newv)
569 iprop = XInternAtom(display, "_SWM_ICONIC", False);
573 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
574 PropModeReplace, (unsigned char *)&v, 1);
576 XDeleteProperty(display, win->id, iprop);
580 get_iconic(struct ws_win *win)
585 unsigned long nitems, extra;
586 unsigned char *prop = NULL;
588 iprop = XInternAtom(display, "_SWM_ICONIC", False);
591 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
592 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
593 if (status != Success)
595 if (rettype != XA_INTEGER || retfmt != 32)
599 v = *((int32_t *)prop);
613 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
615 for (i = 0; i < LENGTH(ewmh); i++)
616 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
618 for (i = 0; i < ScreenCount(display); i++) {
619 /* Support check window will be created by workaround(). */
621 /* Report supported atoms */
622 XDeleteProperty(display, screens[i].root, sup_list);
623 for (j = 0; j < LENGTH(ewmh); j++)
624 XChangeProperty(display, screens[i].root,
625 sup_list, XA_ATOM, 32,
626 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
634 unsigned char *data = NULL;
636 Atom sup_check, sup_list;
639 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
640 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
642 for (i = 0; i < ScreenCount(display); i++) {
643 /* Get the support check window and destroy it */
644 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
649 XDestroyWindow(display, id);
650 XDeleteProperty(display, screens[i].root, sup_check);
651 XDeleteProperty(display, screens[i].root, sup_list);
659 ewmh_autoquirk(struct ws_win *win)
662 unsigned long *data = NULL, n;
665 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
666 XA_ATOM, &n, (void *)&data);
673 for (i = 0; i < n; i++) {
675 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
677 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
678 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
679 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
681 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
684 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
685 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
687 win->quirks = SWM_Q_FLOAT;
695 #define SWM_EWMH_ACTION_COUNT_MAX (6)
696 #define EWMH_F_FULLSCREEN (1<<0)
697 #define EWMH_F_ABOVE (1<<1)
698 #define EWMH_F_HIDDEN (1<<2)
699 #define EWMH_F_SKIP_PAGER (1<<3)
700 #define EWMH_F_SKIP_TASKBAR (1<<4)
701 #define SWM_F_MANUAL (1<<5)
704 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
706 struct swm_geometry rg;
714 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
720 store_float_geom(win, win->ws->r);
727 if (win->g_floatvalid) {
728 /* refloat at last floating relative position */
729 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
730 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
731 win->g.w = win->g_float.w;
732 win->g.h = win->g_float.h;
740 ewmh_update_actions(struct ws_win *win)
742 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
748 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
751 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
752 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
755 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
756 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
759 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
760 #define _NET_WM_STATE_ADD 1 /* add/set property */
761 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
764 ewmh_update_win_state(struct ws_win *win, long state, long action)
766 unsigned int mask = 0;
767 unsigned int changed = 0;
768 unsigned int orig_flags;
773 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
774 mask = EWMH_F_FULLSCREEN;
775 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
777 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
779 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
780 mask = EWMH_F_SKIP_PAGER;
781 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
782 mask = EWMH_F_SKIP_TASKBAR;
785 orig_flags = win->ewmh_flags;
788 case _NET_WM_STATE_REMOVE:
789 win->ewmh_flags &= ~mask;
791 case _NET_WM_STATE_ADD:
792 win->ewmh_flags |= mask;
794 case _NET_WM_STATE_TOGGLE:
795 win->ewmh_flags ^= mask;
799 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
801 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
803 if (!floating_toggle_win(win))
804 win->ewmh_flags = orig_flags; /* revert */
805 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
807 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
808 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
810 if (!ewmh_set_win_fullscreen(win,
811 win->ewmh_flags & EWMH_F_FULLSCREEN))
812 win->ewmh_flags = orig_flags; /* revert */
814 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
816 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
817 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
818 XA_ATOM, 32, PropModeAppend,
819 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
820 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
821 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
822 XA_ATOM, 32, PropModeAppend,
823 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
824 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
825 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
826 XA_ATOM, 32, PropModeAppend,
827 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
828 if (win->ewmh_flags & EWMH_F_ABOVE)
829 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
830 XA_ATOM, 32, PropModeAppend,
831 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
832 if (win->ewmh_flags & SWM_F_MANUAL)
833 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
834 XA_ATOM, 32, PropModeAppend,
835 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
839 ewmh_get_win_state(struct ws_win *win)
850 win->ewmh_flags |= EWMH_F_ABOVE;
852 win->ewmh_flags |= SWM_F_MANUAL;
854 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
855 (~0L), XA_ATOM, &n, (void *)&states);
860 for (i = 0; i < n; i++)
861 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
881 name = "ButtonPress";
884 name = "ButtonRelease";
887 name = "MotionNotify";
890 name = "EnterNotify";
893 name = "LeaveNotify";
902 name = "KeymapNotify";
908 name = "GraphicsExpose";
913 case VisibilityNotify:
914 name = "VisibilityNotify";
917 name = "CreateNotify";
920 name = "DestroyNotify";
923 name = "UnmapNotify";
932 name = "ReparentNotify";
934 case ConfigureNotify:
935 name = "ConfigureNotify";
937 case ConfigureRequest:
938 name = "ConfigureRequest";
941 name = "GravityNotify";
944 name = "ResizeRequest";
946 case CirculateNotify:
947 name = "CirculateNotify";
949 case CirculateRequest:
950 name = "CirculateRequest";
953 name = "PropertyNotify";
956 name = "SelectionClear";
958 case SelectionRequest:
959 name = "SelectionRequest";
961 case SelectionNotify:
962 name = "SelectionNotify";
965 name = "ColormapNotify";
968 name = "ClientMessage";
971 name = "MappingNotify";
976 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
978 e->xany.window, name, e->type, QLength(display));
980 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
982 e->xany.window, e->type, QLength(display));
986 dumpwins(struct swm_region *r, union arg *args)
990 XWindowAttributes wa;
993 fprintf(stderr, "invalid workspace\n");
997 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
999 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1000 state = getstate(win->id);
1001 if (!XGetWindowAttributes(display, win->id, &wa))
1002 fprintf(stderr, "window: %lu failed "
1003 "XGetWindowAttributes\n", win->id);
1004 fprintf(stderr, "window: %lu map_state: %d state: %d "
1006 win->id, wa.map_state, state, win->transient);
1009 fprintf(stderr, "===== unmanaged window list =====\n");
1010 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1011 state = getstate(win->id);
1012 if (!XGetWindowAttributes(display, win->id, &wa))
1013 fprintf(stderr, "window: %lu failed "
1014 "XGetWindowAttributes\n", win->id);
1015 fprintf(stderr, "window: %lu map_state: %d state: %d "
1017 win->id, wa.map_state, state, win->transient);
1020 fprintf(stderr, "=================================\n");
1023 #define dumpevent(e)
1025 dumpwins(struct swm_region *r, union arg *args)
1028 #endif /* SWM_DEBUG */
1030 void expose(XEvent *);
1031 void keypress(XEvent *);
1032 void buttonpress(XEvent *);
1033 void configurerequest(XEvent *);
1034 void configurenotify(XEvent *);
1035 void destroynotify(XEvent *);
1036 void enternotify(XEvent *);
1037 void focusevent(XEvent *);
1038 void mapnotify(XEvent *);
1039 void mappingnotify(XEvent *);
1040 void maprequest(XEvent *);
1041 void propertynotify(XEvent *);
1042 void unmapnotify(XEvent *);
1043 void visibilitynotify(XEvent *);
1044 void clientmessage(XEvent *);
1046 void (*handler[LASTEvent])(XEvent *) = {
1048 [KeyPress] = keypress,
1049 [ButtonPress] = buttonpress,
1050 [ConfigureRequest] = configurerequest,
1051 [ConfigureNotify] = configurenotify,
1052 [DestroyNotify] = destroynotify,
1053 [EnterNotify] = enternotify,
1054 [FocusIn] = focusevent,
1055 [FocusOut] = focusevent,
1056 [MapNotify] = mapnotify,
1057 [MappingNotify] = mappingnotify,
1058 [MapRequest] = maprequest,
1059 [PropertyNotify] = propertynotify,
1060 [UnmapNotify] = unmapnotify,
1061 [VisibilityNotify] = visibilitynotify,
1062 [ClientMessage] = clientmessage,
1068 int saved_errno, status;
1071 saved_errno = errno;
1075 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1080 if (errno != ECHILD)
1081 warn("sighdlr: waitpid");
1082 #endif /* SWM_DEBUG */
1085 if (pid == searchpid)
1089 if (WIFEXITED(status)) {
1090 if (WEXITSTATUS(status) != 0)
1091 warnx("sighdlr: child exit status: %d",
1092 WEXITSTATUS(status));
1094 warnx("sighdlr: child is terminated "
1096 #endif /* SWM_DEBUG */
1110 errno = saved_errno;
1116 struct pid_e *p = NULL;
1118 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1123 TAILQ_FOREACH(p, &pidlist, entry) {
1132 name_to_color(char *colorname)
1136 XColor screen_def, exact_def;
1137 unsigned long result = 0;
1138 char cname[32] = "#";
1140 cmap = DefaultColormap(display, screens[0].idx);
1141 status = XAllocNamedColor(display, cmap, colorname,
1142 &screen_def, &exact_def);
1144 strlcat(cname, colorname + 2, sizeof cname - 1);
1145 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1149 result = screen_def.pixel;
1151 fprintf(stderr, "color '%s' not found.\n", colorname);
1157 setscreencolor(char *val, int i, int c)
1159 if (i > 0 && i <= ScreenCount(display)) {
1160 screens[i - 1].c[c].color = name_to_color(val);
1161 free(screens[i - 1].c[c].name);
1162 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1164 } else if (i == -1) {
1165 for (i = 0; i < ScreenCount(display); i++) {
1166 screens[i].c[c].color = name_to_color(val);
1167 free(screens[i].c[c].name);
1168 if ((screens[i].c[c].name = strdup(val)) == NULL)
1172 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1173 i, ScreenCount(display));
1177 fancy_stacker(struct workspace *ws)
1179 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1180 if (ws->cur_layout->l_stack == vertical_stack)
1181 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1182 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1183 if (ws->cur_layout->l_stack == horizontal_stack)
1184 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1185 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1189 plain_stacker(struct workspace *ws)
1191 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1192 if (ws->cur_layout->l_stack == vertical_stack)
1193 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1194 if (ws->cur_layout->l_stack == horizontal_stack)
1195 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1199 custom_region(char *val)
1201 unsigned int sidx, x, y, w, h;
1203 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1204 errx(1, "invalid custom region, "
1205 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1206 if (sidx < 1 || sidx > ScreenCount(display))
1207 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1208 sidx, ScreenCount(display));
1212 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1214 if (x > DisplayWidth(display, sidx) ||
1215 y > DisplayHeight(display, sidx) ||
1216 w + x > DisplayWidth(display, sidx) ||
1217 h + y > DisplayHeight(display, sidx)) {
1218 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1219 "- not within screen boundaries "
1220 "(%ux%u)\n", w, h, x, y,
1221 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1225 new_region(&screens[sidx], x, y, w, h);
1229 socket_setnonblock(int fd)
1233 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1234 err(1, "fcntl F_GETFL");
1235 flags |= O_NONBLOCK;
1236 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1237 err(1, "fcntl F_SETFL");
1241 bar_print(struct swm_region *r, char *s)
1246 XClearWindow(display, r->bar_window);
1247 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1250 textwidth = XTextWidth(bar_fs, s, len);
1252 switch (bar_justify) {
1253 case SWM_BAR_JUSTIFY_LEFT:
1256 case SWM_BAR_JUSTIFY_CENTER:
1257 x = (WIDTH(r) - textwidth) / 2;
1259 case SWM_BAR_JUSTIFY_RIGHT:
1260 x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
1264 if (x < SWM_BAR_OFFSET)
1267 XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
1271 bar_extra_stop(void)
1275 bzero(bar_pipe, sizeof bar_pipe);
1278 kill(bar_pid, SIGTERM);
1281 strlcpy(bar_ext, "", sizeof bar_ext);
1286 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1288 int do_class, do_name;
1290 XClassHint *xch = NULL;
1292 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1293 cur_focus != NULL) {
1294 if ((xch = XAllocClassHint()) == NULL)
1296 status = XGetClassHint(display, cur_focus->id, xch);
1297 if (status == BadWindow || status == BadAlloc)
1299 do_class = (title_class_enabled && xch->res_class != NULL);
1300 do_name = (title_name_enabled && xch->res_name != NULL);
1302 strlcat(s, xch->res_class, sz);
1303 if (do_class && do_name)
1304 strlcat(s, ":", sz);
1306 strlcat(s, xch->res_name, sz);
1307 strlcat(s, " ", sz);
1315 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1319 if (window_name_enabled && cur_focus != NULL) {
1320 XFetchName(display, cur_focus->id, &title);
1322 if (cur_focus->floating)
1323 strlcat(s, "(f) ", sz);
1324 strlcat(s, title, sz);
1325 strlcat(s, " ", sz);
1331 int urgent[SWM_WS_MAX];
1333 bar_urgent(char *s, ssize_t sz)
1335 XWMHints *wmh = NULL;
1340 if (urgent_enabled == 0)
1343 for (i = 0; i < SWM_WS_MAX; i++)
1346 for (i = 0; i < ScreenCount(display); i++)
1347 for (j = 0; j < SWM_WS_MAX; j++)
1348 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1349 wmh = XGetWMHints(display, win->id);
1353 if (wmh->flags & XUrgencyHint)
1358 strlcat(s, "* ", sz);
1359 for (i = 0; i < SWM_WS_MAX; i++) {
1361 snprintf(b, sizeof b, "%d ", i + 1);
1363 snprintf(b, sizeof b, "- ");
1366 strlcat(s, "* ", sz);
1374 struct swm_region *r;
1377 char ws[SWM_BAR_MAX];
1378 char s[SWM_BAR_MAX];
1379 char cn[SWM_BAR_MAX];
1380 char loc[SWM_BAR_MAX];
1381 char *b, *stack = "";
1383 if (bar_enabled == 0)
1385 if (bar_extra && bar_extra_running) {
1386 /* ignore short reads; it'll correct itself */
1387 while ((b = fgetln(stdin, &len)) != NULL)
1388 if (b && b[len - 1] == '\n') {
1390 strlcpy(bar_ext, b, sizeof bar_ext);
1392 if (b == NULL && errno != EAGAIN) {
1393 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1394 errno, strerror(errno));
1398 strlcpy(bar_ext, "", sizeof bar_ext);
1400 if (clock_enabled == 0)
1401 strlcpy(s, "", sizeof s);
1404 localtime_r(&tmt, &tm);
1405 strftime(s, sizeof s, clock_format, &tm);
1406 strlcat(s, " ", sizeof s);
1409 for (i = 0; i < ScreenCount(display); i++) {
1411 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1412 strlcpy(cn, "", sizeof cn);
1413 strlcpy(ws, "", sizeof ws);
1415 bar_urgent(cn, sizeof cn);
1416 bar_class_name(cn, sizeof cn, r->ws->focus);
1417 bar_window_name(cn, sizeof cn, r->ws->focus);
1419 snprintf(ws, sizeof ws, "<%s>", r->ws->name);
1422 stack = r->ws->stacker;
1424 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s %s",
1425 x++, r->ws->idx + 1, stack, ws, s, cn, bar_ext,
1440 bar_toggle(struct swm_region *r, union arg *args)
1442 struct swm_region *tmpr;
1443 int i, sc = ScreenCount(display);
1445 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1448 for (i = 0; i < sc; i++)
1449 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1450 XUnmapWindow(display, tmpr->bar_window);
1452 for (i = 0; i < sc; i++)
1453 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1454 XMapRaised(display, tmpr->bar_window);
1456 bar_enabled = !bar_enabled;
1459 /* must be after stack */
1466 XSetWindowAttributes wa;
1467 struct swm_region *r;
1470 /* do this here because the conf file is in memory */
1471 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1472 /* launch external status app */
1473 bar_extra_running = 1;
1474 if (pipe(bar_pipe) == -1)
1475 err(1, "pipe error");
1476 socket_setnonblock(bar_pipe[0]);
1477 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1478 if (dup2(bar_pipe[0], 0) == -1)
1480 if (dup2(bar_pipe[1], 1) == -1)
1482 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1483 err(1, "could not disable SIGPIPE");
1484 switch (bar_pid = fork()) {
1486 err(1, "cannot fork");
1490 execvp(bar_argv[0], bar_argv);
1491 err(1, "%s external app failed", bar_argv[0]);
1493 default: /* parent */
1499 bzero(&wa, sizeof wa);
1500 for (i = 0; i < ScreenCount(display); i++)
1501 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1503 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1504 wa.background_pixel =
1505 screens[i].c[SWM_S_COLOR_BAR].color;
1506 XChangeWindowAttributes(display, r->bar_window,
1507 CWBackPixel | CWBorderPixel, &wa);
1513 bar_setup(struct swm_region *r)
1518 XFreeFont(display, bar_fs);
1522 for (i = 0; bar_fonts[i] != NULL; i++) {
1523 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1529 if (bar_fonts[i] == NULL)
1530 errx(1, "couldn't load font");
1532 errx(1, "couldn't create font structure");
1534 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1535 2 * bar_border_width;
1537 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1539 r->bar_window = XCreateSimpleWindow(display,
1540 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1541 bar_height - 2 * bar_border_width,
1542 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1543 r->s->c[SWM_S_COLOR_BAR].color);
1544 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1545 XSetFont(display, bar_gc, bar_fs->fid);
1546 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1548 XMapRaised(display, r->bar_window);
1549 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1551 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1552 err(1, "could not install bar_signal");
1557 drain_enter_notify(void)
1562 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1565 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1569 set_win_state(struct ws_win *win, long state)
1571 long data[] = {state, None};
1573 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1578 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1579 (unsigned char *)data, 2);
1586 unsigned char *p = NULL;
1589 if (!get_property(w, astate, 2L, astate, &n, &p))
1592 result = *((long *)p);
1598 version(struct swm_region *r, union arg *args)
1600 bar_version = !bar_version;
1602 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s Build: %s",
1603 SCROTWM_VERSION, buildstr);
1605 strlcpy(bar_vertext, "", sizeof bar_vertext);
1610 client_msg(struct ws_win *win, Atom a)
1612 XClientMessageEvent cm;
1617 bzero(&cm, sizeof cm);
1618 cm.type = ClientMessage;
1619 cm.window = win->id;
1620 cm.message_type = aprot;
1623 cm.data.l[1] = CurrentTime;
1624 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1628 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1636 DNPRINTF(SWM_D_MISC,
1637 "config_win: win %lu x %d y %d w %d h %d\n",
1638 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1640 ce.type = ConfigureNotify;
1641 ce.display = display;
1643 ce.window = win->id;
1646 ce.width = win->g.w;
1647 ce.height = win->g.h;
1648 ce.border_width = border_width;
1650 ce.override_redirect = False;
1652 DNPRINTF(SWM_D_MISC,
1653 "config_win: ev win %lu x %d y %d w %d h %d\n",
1654 ev->window, ev->x, ev->y, ev->width, ev->height);
1655 ce.type = ConfigureNotify;
1656 ce.display = ev->display;
1657 ce.event = ev->window;
1658 ce.window = ev->window;
1661 ce.width = ev->width;
1662 ce.height = ev->height;
1663 ce.border_width = ev->border_width;
1664 ce.above = ev->above;
1665 ce.override_redirect = False;
1668 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1672 count_win(struct workspace *ws, int count_transient)
1677 TAILQ_FOREACH(win, &ws->winlist, entry) {
1678 if (count_transient == 0 && win->floating)
1680 if (count_transient == 0 && win->transient)
1686 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1692 quit(struct swm_region *r, union arg *args)
1694 DNPRINTF(SWM_D_MISC, "quit\n");
1699 unmap_window(struct ws_win *win)
1704 /* don't unmap again */
1705 if (getstate(win->id) == IconicState)
1708 set_win_state(win, IconicState);
1710 XUnmapWindow(display, win->id);
1711 XSetWindowBorder(display, win->id,
1712 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1721 for (i = 0; i < ScreenCount(display); i++)
1722 for (j = 0; j < SWM_WS_MAX; j++)
1723 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1728 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1735 event.display = display; /* Ignored, but what the hell */
1736 event.window = win->id;
1737 event.root = win->s->root;
1738 event.subwindow = None;
1739 event.time = CurrentTime;
1744 event.same_screen = True;
1745 event.keycode = XKeysymToKeycode(display, keysym);
1746 event.state = modifiers;
1748 event.type = KeyPress;
1749 XSendEvent(event.display, event.window, True,
1750 KeyPressMask, (XEvent *)&event);
1752 event.type = KeyRelease;
1753 XSendEvent(event.display, event.window, True,
1754 KeyPressMask, (XEvent *)&event);
1759 restart(struct swm_region *r, union arg *args)
1761 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1763 /* disable alarm because the following code may not be interrupted */
1765 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1766 err(1, "can't disable alarm");
1771 XCloseDisplay(display);
1772 execvp(start_argv[0], start_argv);
1773 fprintf(stderr, "execvp failed\n");
1779 root_to_region(Window root)
1781 struct swm_region *r = NULL;
1783 int i, x, y, wx, wy;
1786 for (i = 0; i < ScreenCount(display); i++)
1787 if (screens[i].root == root)
1790 if (XQueryPointer(display, screens[i].root,
1791 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1792 /* choose a region based on pointer location */
1793 TAILQ_FOREACH(r, &screens[i].rl, entry)
1794 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1795 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1800 r = TAILQ_FIRST(&screens[i].rl);
1806 find_unmanaged_window(Window id)
1811 for (i = 0; i < ScreenCount(display); i++)
1812 for (j = 0; j < SWM_WS_MAX; j++)
1813 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1821 find_window(Window id)
1824 Window wrr, wpr, *wcr = NULL;
1828 for (i = 0; i < ScreenCount(display); i++)
1829 for (j = 0; j < SWM_WS_MAX; j++)
1830 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1834 /* if we were looking for the parent return that window instead */
1835 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1840 /* ignore not found and root */
1841 if (wpr == 0 || wrr == wpr)
1844 /* look for parent */
1845 for (i = 0; i < ScreenCount(display); i++)
1846 for (j = 0; j < SWM_WS_MAX; j++)
1847 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1855 spawn(int ws_idx, union arg *args, int close_fd)
1860 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1863 close(ConnectionNumber(display));
1865 setenv("LD_PRELOAD", SWM_LIB, 1);
1867 if (asprintf(&ret, "%d", ws_idx) == -1) {
1871 setenv("_SWM_WS", ret, 1);
1875 if (asprintf(&ret, "%d", getpid()) == -1) {
1879 setenv("_SWM_PID", ret, 1);
1883 if (setsid() == -1) {
1890 * close stdin and stdout to prevent interaction between apps
1891 * and the baraction script
1892 * leave stderr open to record errors
1894 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1898 dup2(fd, STDIN_FILENO);
1899 dup2(fd, STDOUT_FILENO);
1904 execvp(args->argv[0], args->argv);
1911 spawnterm(struct swm_region *r, union arg *args)
1913 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1917 setenv("_SWM_XTERM_FONTADJ", "", 1);
1918 spawn(r->ws->idx, args, 1);
1923 kill_refs(struct ws_win *win)
1926 struct swm_region *r;
1927 struct workspace *ws;
1932 for (i = 0; i < ScreenCount(display); i++)
1933 TAILQ_FOREACH(r, &screens[i].rl, entry)
1934 for (x = 0; x < SWM_WS_MAX; x++) {
1936 if (win == ws->focus)
1938 if (win == ws->focus_prev)
1939 ws->focus_prev = NULL;
1944 validate_win(struct ws_win *testwin)
1947 struct workspace *ws;
1948 struct swm_region *r;
1951 if (testwin == NULL)
1954 for (i = 0; i < ScreenCount(display); i++)
1955 TAILQ_FOREACH(r, &screens[i].rl, entry)
1956 for (x = 0; x < SWM_WS_MAX; x++) {
1958 TAILQ_FOREACH(win, &ws->winlist, entry)
1966 validate_ws(struct workspace *testws)
1968 struct swm_region *r;
1969 struct workspace *ws;
1972 /* validate all ws */
1973 for (i = 0; i < ScreenCount(display); i++)
1974 TAILQ_FOREACH(r, &screens[i].rl, entry)
1975 for (x = 0; x < SWM_WS_MAX; x++) {
1984 unfocus_win(struct ws_win *win)
1989 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1993 if (win->ws == NULL)
1996 if (validate_ws(win->ws))
1997 return; /* XXX this gets hit with thunderbird, needs fixing */
1999 if (win->ws->r == NULL)
2002 if (validate_win(win)) {
2007 if (win->ws->focus == win) {
2008 win->ws->focus = NULL;
2009 win->ws->focus_prev = win;
2012 if (validate_win(win->ws->focus)) {
2013 kill_refs(win->ws->focus);
2014 win->ws->focus = NULL;
2016 if (validate_win(win->ws->focus_prev)) {
2017 kill_refs(win->ws->focus_prev);
2018 win->ws->focus_prev = NULL;
2021 /* drain all previous unfocus events */
2022 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2025 grabbuttons(win, 0);
2026 XSetWindowBorder(display, win->id,
2027 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2029 XChangeProperty(display, win->s->root,
2030 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2031 PropModeReplace, (unsigned char *)&none,1);
2040 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2042 for (i = 0; i < ScreenCount(display); i++)
2043 for (j = 0; j < SWM_WS_MAX; j++)
2044 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2049 focus_win(struct ws_win *win)
2054 struct ws_win *cfw = NULL;
2057 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
2061 if (win->ws == NULL)
2064 if (validate_ws(win->ws))
2065 return; /* XXX this gets hit with thunderbird, needs fixing */
2067 if (validate_win(win)) {
2072 if (validate_win(win)) {
2077 XGetInputFocus(display, &cur_focus, &rr);
2078 if ((cfw = find_window(cur_focus)) != NULL)
2081 /* use larger hammer since the window was killed somehow */
2082 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2083 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2084 XSetWindowBorder(display, cfw->id,
2085 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2088 win->ws->focus = win;
2090 if (win->ws->r != NULL) {
2091 /* drain all previous focus events */
2092 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2096 XSetInputFocus(display, win->id,
2097 RevertToParent, CurrentTime);
2098 grabbuttons(win, 1);
2099 XSetWindowBorder(display, win->id,
2100 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2101 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2102 win->ws->always_raise)
2103 XMapRaised(display, win->id);
2105 XChangeProperty(display, win->s->root,
2106 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2107 PropModeReplace, (unsigned char *)&win->id,1);
2110 if (window_name_enabled)
2115 switchws(struct swm_region *r, union arg *args)
2117 int wsid = args->id, unmap_old = 0;
2118 struct swm_region *this_r, *other_r;
2120 struct workspace *new_ws, *old_ws;
2127 old_ws = this_r->ws;
2128 new_ws = &this_r->s->ws[wsid];
2130 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2131 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2134 if (new_ws == NULL || old_ws == NULL)
2136 if (new_ws == old_ws)
2139 other_r = new_ws->r;
2140 if (other_r == NULL) {
2141 /* the other workspace is hidden, hide this one */
2145 /* the other ws is visible in another region, exchange them */
2146 other_r->ws_prior = new_ws;
2147 other_r->ws = old_ws;
2148 old_ws->r = other_r;
2150 this_r->ws_prior = old_ws;
2151 this_r->ws = new_ws;
2154 /* this is needed so that we can click on a window after a restart */
2158 a.id = SWM_ARG_ID_FOCUSCUR;
2159 focus(new_ws->r, &a);
2163 /* unmap old windows */
2165 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2168 if (focus_mode == SWM_FOCUS_DEFAULT)
2169 drain_enter_notify();
2173 cyclews(struct swm_region *r, union arg *args)
2176 struct swm_screen *s = r->s;
2179 DNPRINTF(SWM_D_WS, "cyclews id %d "
2180 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2181 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2186 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2189 case SWM_ARG_ID_CYCLEWS_UP:
2190 if (a.id < SWM_WS_MAX - 1)
2195 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2198 case SWM_ARG_ID_CYCLEWS_DOWN:
2202 a.id = SWM_WS_MAX - 1;
2209 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2211 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2215 } while (a.id != r->ws->idx);
2219 priorws(struct swm_region *r, union arg *args)
2223 DNPRINTF(SWM_D_WS, "priorws id %d "
2224 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2225 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2227 if (r->ws_prior == NULL)
2230 a.id = r->ws_prior->idx;
2235 cyclescr(struct swm_region *r, union arg *args)
2237 struct swm_region *rr = NULL;
2241 /* do nothing if we don't have more than one screen */
2242 if (!(ScreenCount(display) > 1 || outputs > 1))
2247 case SWM_ARG_ID_CYCLESC_UP:
2248 rr = TAILQ_NEXT(r, entry);
2250 rr = TAILQ_FIRST(&screens[i].rl);
2252 case SWM_ARG_ID_CYCLESC_DOWN:
2253 rr = TAILQ_PREV(r, swm_region_list, entry);
2255 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2263 /* move mouse to region */
2265 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2266 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2268 a.id = SWM_ARG_ID_FOCUSCUR;
2271 if (rr->ws->focus) {
2272 /* move to focus window */
2273 x = rr->ws->focus->g.x + 1;
2274 y = rr->ws->focus->g.y + 1;
2275 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2280 sort_windows(struct ws_win_list *wl)
2282 struct ws_win *win, *parent, *nxt;
2287 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2288 nxt = TAILQ_NEXT(win, entry);
2289 if (win->transient) {
2290 parent = find_window(win->transient);
2291 if (parent == NULL) {
2292 fprintf(stderr, "not possible bug\n");
2295 TAILQ_REMOVE(wl, win, entry);
2296 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2303 swapwin(struct swm_region *r, union arg *args)
2305 struct ws_win *target, *source;
2306 struct ws_win *cur_focus;
2307 struct ws_win_list *wl;
2310 DNPRINTF(SWM_D_WS, "swapwin id %d "
2311 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2312 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2314 cur_focus = r->ws->focus;
2315 if (cur_focus == NULL)
2319 wl = &source->ws->winlist;
2322 case SWM_ARG_ID_SWAPPREV:
2323 if (source->transient)
2324 source = find_window(source->transient);
2325 target = TAILQ_PREV(source, ws_win_list, entry);
2326 if (target && target->transient)
2327 target = find_window(target->transient);
2328 TAILQ_REMOVE(wl, source, entry);
2330 TAILQ_INSERT_TAIL(wl, source, entry);
2332 TAILQ_INSERT_BEFORE(target, source, entry);
2334 case SWM_ARG_ID_SWAPNEXT:
2335 target = TAILQ_NEXT(source, entry);
2336 /* move the parent and let the sort handle the move */
2337 if (source->transient)
2338 source = find_window(source->transient);
2339 TAILQ_REMOVE(wl, source, entry);
2341 TAILQ_INSERT_HEAD(wl, source, entry);
2343 TAILQ_INSERT_AFTER(wl, target, source, entry);
2345 case SWM_ARG_ID_SWAPMAIN:
2346 target = TAILQ_FIRST(wl);
2347 if (target == source) {
2348 if (source->ws->focus_prev != NULL &&
2349 source->ws->focus_prev != target)
2351 source = source->ws->focus_prev;
2355 if (target == NULL || source == NULL)
2357 source->ws->focus_prev = target;
2358 TAILQ_REMOVE(wl, target, entry);
2359 TAILQ_INSERT_BEFORE(source, target, entry);
2360 TAILQ_REMOVE(wl, source, entry);
2361 TAILQ_INSERT_HEAD(wl, source, entry);
2363 case SWM_ARG_ID_MOVELAST:
2364 TAILQ_REMOVE(wl, source, entry);
2365 TAILQ_INSERT_TAIL(wl, source, entry);
2368 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2378 focus_prev(struct ws_win *win)
2380 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2381 struct ws_win *cur_focus = NULL;
2382 struct ws_win_list *wl = NULL;
2383 struct workspace *ws = NULL;
2385 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2387 if (!(win && win->ws))
2392 cur_focus = ws->focus;
2393 winlostfocus = cur_focus;
2395 /* pickle, just focus on whatever */
2396 if (cur_focus == NULL) {
2397 /* use prev_focus if valid */
2398 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2399 find_window(WINID(ws->focus_prev)))
2400 winfocus = ws->focus_prev;
2401 if (winfocus == NULL)
2402 winfocus = TAILQ_FIRST(wl);
2406 /* if transient focus on parent */
2407 if (cur_focus->transient) {
2408 winfocus = find_window(cur_focus->transient);
2412 /* if in max_stack try harder */
2413 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2414 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2415 if (cur_focus != ws->focus_prev)
2416 winfocus = ws->focus_prev;
2417 else if (cur_focus != ws->focus)
2418 winfocus = ws->focus;
2420 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2425 if (cur_focus == win)
2426 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2427 if (winfocus == NULL)
2428 winfocus = TAILQ_LAST(wl, ws_win_list);
2429 if (winfocus == NULL || winfocus == win)
2430 winfocus = TAILQ_NEXT(cur_focus, entry);
2432 if (winfocus == winlostfocus || winfocus == NULL) {
2433 /* update the bar so that title/class/name will be cleared. */
2434 if (window_name_enabled || title_name_enabled || title_class_enabled)
2440 focus_magic(winfocus);
2444 focus(struct swm_region *r, union arg *args)
2446 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2447 struct ws_win *cur_focus = NULL;
2448 struct ws_win_list *wl = NULL;
2449 struct workspace *ws = NULL;
2455 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2457 /* treat FOCUS_CUR special */
2458 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2459 if (r->ws->focus && r->ws->focus->iconic == 0)
2460 winfocus = r->ws->focus;
2461 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2462 winfocus = r->ws->focus_prev;
2464 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2465 if (winfocus->iconic == 0)
2468 focus_magic(winfocus);
2472 if ((cur_focus = r->ws->focus) == NULL)
2476 if (TAILQ_EMPTY(wl))
2478 /* make sure there is at least one uniconified window */
2480 TAILQ_FOREACH(winfocus, wl, entry)
2481 if (winfocus->iconic == 0) {
2488 winlostfocus = cur_focus;
2491 case SWM_ARG_ID_FOCUSPREV:
2492 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2494 head = TAILQ_LAST(wl, ws_win_list);
2496 if (WINID(winfocus) == cur_focus->transient) {
2497 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2499 head = TAILQ_LAST(wl, ws_win_list);
2504 if (winfocus && winfocus->iconic) {
2505 while (winfocus != cur_focus) {
2506 if (winfocus == NULL)
2507 winfocus = TAILQ_LAST(wl, ws_win_list);
2508 if (winfocus->iconic == 0)
2510 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
2515 case SWM_ARG_ID_FOCUSNEXT:
2516 head = TAILQ_NEXT(cur_focus, entry);
2518 head = TAILQ_FIRST(wl);
2522 if (winfocus && winfocus->iconic) {
2523 while (winfocus != cur_focus) {
2524 if (winfocus == NULL)
2525 winfocus = TAILQ_FIRST(wl);
2526 if (winfocus->iconic == 0)
2528 winfocus = TAILQ_NEXT(winfocus, entry);
2533 case SWM_ARG_ID_FOCUSMAIN:
2534 winfocus = TAILQ_FIRST(wl);
2535 if (winfocus == cur_focus)
2536 winfocus = cur_focus->ws->focus_prev;
2542 if (winfocus == winlostfocus || winfocus == NULL) {
2543 /* update the bar so that title/class/name will be cleared. */
2544 if (window_name_enabled || title_name_enabled || title_class_enabled)
2550 focus_magic(winfocus);
2554 cycle_layout(struct swm_region *r, union arg *args)
2556 struct workspace *ws = r->ws;
2559 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2562 if (ws->cur_layout->l_stack == NULL)
2563 ws->cur_layout = &layouts[0];
2566 if (focus_mode == SWM_FOCUS_DEFAULT)
2567 drain_enter_notify();
2568 a.id = SWM_ARG_ID_FOCUSCUR;
2574 stack_config(struct swm_region *r, union arg *args)
2576 struct workspace *ws = r->ws;
2578 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2581 if (ws->cur_layout->l_config != NULL)
2582 ws->cur_layout->l_config(ws, args->id);
2584 if (args->id != SWM_ARG_ID_STACKINIT)
2591 struct swm_geometry g;
2592 struct swm_region *r;
2595 DNPRINTF(SWM_D_STACK, "stack\n");
2597 for (i = 0; i < ScreenCount(display); i++) {
2599 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2600 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2601 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2603 /* start with screen geometry, adjust for bar */
2605 g.w -= 2 * border_width;
2606 g.h -= 2 * border_width;
2612 r->ws->cur_layout->l_stack(r->ws, &g);
2613 r->ws->cur_layout->l_string(r->ws);
2614 /* save r so we can track region changes */
2621 if (focus_mode == SWM_FOCUS_DEFAULT)
2622 drain_enter_notify();
2626 store_float_geom(struct ws_win *win, struct swm_region *r)
2628 /* retain window geom and region geom */
2629 win->g_float.x = win->g.x;
2630 win->g_float.y = win->g.y;
2631 win->g_float.w = win->g.w;
2632 win->g_float.h = win->g.h;
2633 win->rg_float.x = r->g.x;
2634 win->rg_float.y = r->g.y;
2635 win->rg_float.w = r->g.w;
2636 win->rg_float.h = r->g.h;
2637 win->g_floatvalid = 1;
2641 stack_floater(struct ws_win *win, struct swm_region *r)
2649 bzero(&wc, sizeof wc);
2650 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2653 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2654 * geom on ws switches or return from max mode
2657 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2658 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2660 * use stored g and rg to set relative position and size
2661 * as in old region or before max stack mode
2663 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2664 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2665 win->g.w = win->g_float.w;
2666 win->g.h = win->g_float.h;
2667 win->g_floatvalid = 0;
2670 win->floatmaxed = 0;
2672 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2673 (win->g.h >= HEIGHT(r)))
2674 wc.border_width = 0;
2676 wc.border_width = border_width;
2677 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2678 win->g.w = (double)WIDTH(r) * dialog_ratio;
2679 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2684 * floaters and transients are auto-centred unless moved
2687 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2688 2 - wc.border_width;
2689 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2690 2 - wc.border_width;
2693 /* win can be outside r if new r smaller than old r */
2694 /* Ensure top left corner inside r (move probs otherwise) */
2695 if (win->g.x < r->g.x - wc.border_width)
2696 win->g.x = r->g.x - wc.border_width;
2697 if (win->g.x > r->g.x + r->g.w - 1)
2698 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2699 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2700 if (win->g.y < r->g.y - wc.border_width)
2701 win->g.y = r->g.y - wc.border_width;
2702 if (win->g.y > r->g.y + r->g.h - 1)
2703 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2704 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2708 wc.width = win->g.w;
2709 wc.height = win->g.h;
2712 * Retain floater and transient geometry for correct positioning
2713 * when ws changes region
2715 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2716 store_float_geom(win, r);
2718 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2719 win->id, wc.x, wc.y, wc.width, wc.height);
2721 XConfigureWindow(display, win->id, mask, &wc);
2725 * Send keystrokes to terminal to decrease/increase the font size as the
2726 * window size changes.
2729 adjust_font(struct ws_win *win)
2731 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2732 win->floating || win->transient)
2735 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2736 win->g.w / win->sh.width_inc < term_width &&
2737 win->font_steps < SWM_MAX_FONT_STEPS) {
2738 win->font_size_boundary[win->font_steps] =
2739 (win->sh.width_inc * term_width) + win->sh.base_width;
2742 win->last_inc = win->sh.width_inc;
2743 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2744 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2745 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2748 win->last_inc = win->sh.width_inc;
2749 fake_keypress(win, XK_KP_Add, ShiftMask);
2753 #define SWAPXY(g) do { \
2755 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2756 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2759 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2762 XWindowAttributes wa;
2763 struct swm_geometry win_g, r_g = *g;
2764 struct ws_win *win, *fs_win = 0;
2765 int i, j, s, stacks;
2766 int w_inc = 1, h_inc, w_base = 1, h_base;
2767 int hrh, extra = 0, h_slice, last_h = 0;
2768 int split, colno, winno, mwin, msize, mscale;
2769 int remain, missing, v_slice, reconfigure;
2772 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2773 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2775 winno = count_win(ws, 0);
2776 if (winno == 0 && count_win(ws, 1) == 0)
2779 TAILQ_FOREACH(win, &ws->winlist, entry)
2780 if (win->transient == 0 && win->floating == 0
2781 && win->iconic == 0)
2788 w_inc = win->sh.width_inc;
2789 w_base = win->sh.base_width;
2790 mwin = ws->l_state.horizontal_mwin;
2791 mscale = ws->l_state.horizontal_msize;
2792 stacks = ws->l_state.horizontal_stacks;
2795 w_inc = win->sh.height_inc;
2796 w_base = win->sh.base_height;
2797 mwin = ws->l_state.vertical_mwin;
2798 mscale = ws->l_state.vertical_msize;
2799 stacks = ws->l_state.vertical_stacks;
2803 if (stacks > winno - mwin)
2804 stacks = winno - mwin;
2808 h_slice = r_g.h / SWM_H_SLICE;
2809 if (mwin && winno > mwin) {
2810 v_slice = r_g.w / SWM_V_SLICE;
2814 win_g.w = v_slice * mscale;
2816 if (w_inc > 1 && w_inc < v_slice) {
2817 /* adjust for window's requested size increment */
2818 remain = (win_g.w - w_base) % w_inc;
2819 missing = w_inc - remain;
2826 win_g.x += r_g.w - msize;
2829 colno = split = winno / stacks;
2830 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2831 2 * border_width) / stacks);
2833 hrh = r_g.h / colno;
2834 extra = r_g.h - (colno * hrh);
2835 win_g.h = hrh - 2 * border_width;
2837 /* stack all the tiled windows */
2838 i = j = 0, s = stacks;
2839 TAILQ_FOREACH(win, &ws->winlist, entry) {
2840 if (win->transient != 0 || win->floating != 0)
2842 if (win->iconic != 0)
2845 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2850 if (split && i == split) {
2851 colno = (winno - mwin) / stacks;
2852 if (s <= (winno - mwin) % stacks)
2854 split = split + colno;
2855 hrh = (r_g.h / colno);
2856 extra = r_g.h - (colno * hrh);
2860 win_g.x += win_g.w + 2 * border_width;
2861 win_g.w = (r_g.w - msize -
2862 (stacks * 2 * border_width)) / stacks;
2864 win_g.w += (r_g.w - msize -
2865 (stacks * 2 * border_width)) % stacks;
2869 win_g.h = hrh - 2 * border_width;
2871 h_inc = win->sh.width_inc;
2872 h_base = win->sh.base_width;
2874 h_inc = win->sh.height_inc;
2875 h_base = win->sh.base_height;
2877 if (j == colno - 1) {
2878 win_g.h = hrh + extra;
2879 } else if (h_inc > 1 && h_inc < h_slice) {
2880 /* adjust for window's requested size increment */
2881 remain = (win_g.h - h_base) % h_inc;
2882 missing = h_inc - remain;
2884 if (missing <= extra || j == 0) {
2896 win_g.y += last_h + 2 * border_width;
2898 bzero(&wc, sizeof wc);
2899 if (disable_border && bar_enabled == 0 && winno == 1){
2900 wc.border_width = 0;
2901 win_g.w += 2 * border_width;
2902 win_g.h += 2 * border_width;
2904 wc.border_width = border_width;
2907 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2908 win->g.w != win_g.h || win->g.h != win_g.w) {
2910 win->g.x = wc.x = win_g.y;
2911 win->g.y = wc.y = win_g.x;
2912 win->g.w = wc.width = win_g.h;
2913 win->g.h = wc.height = win_g.w;
2916 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2917 win->g.w != win_g.w || win->g.h != win_g.h) {
2919 win->g.x = wc.x = win_g.x;
2920 win->g.y = wc.y = win_g.y;
2921 win->g.w = wc.width = win_g.w;
2922 win->g.h = wc.height = win_g.h;
2927 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2928 XConfigureWindow(display, win->id, mask, &wc);
2931 if (XGetWindowAttributes(display, win->id, &wa))
2932 if (wa.map_state == IsUnmapped)
2933 XMapRaised(display, win->id);
2941 /* now, stack all the floaters and transients */
2942 TAILQ_FOREACH(win, &ws->winlist, entry) {
2943 if (win->transient == 0 && win->floating == 0)
2945 if (win->iconic == 1)
2947 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2952 stack_floater(win, ws->r);
2953 XMapRaised(display, win->id);
2957 stack_floater(fs_win, ws->r);
2958 XMapRaised(display, fs_win->id);
2963 vertical_config(struct workspace *ws, int id)
2965 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2968 case SWM_ARG_ID_STACKRESET:
2969 case SWM_ARG_ID_STACKINIT:
2970 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2971 ws->l_state.vertical_mwin = 1;
2972 ws->l_state.vertical_stacks = 1;
2974 case SWM_ARG_ID_MASTERSHRINK:
2975 if (ws->l_state.vertical_msize > 1)
2976 ws->l_state.vertical_msize--;
2978 case SWM_ARG_ID_MASTERGROW:
2979 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2980 ws->l_state.vertical_msize++;
2982 case SWM_ARG_ID_MASTERADD:
2983 ws->l_state.vertical_mwin++;
2985 case SWM_ARG_ID_MASTERDEL:
2986 if (ws->l_state.vertical_mwin > 0)
2987 ws->l_state.vertical_mwin--;
2989 case SWM_ARG_ID_STACKINC:
2990 ws->l_state.vertical_stacks++;
2992 case SWM_ARG_ID_STACKDEC:
2993 if (ws->l_state.vertical_stacks > 1)
2994 ws->l_state.vertical_stacks--;
3002 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3004 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3006 stack_master(ws, g, 0, 0);
3010 horizontal_config(struct workspace *ws, int id)
3012 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3015 case SWM_ARG_ID_STACKRESET:
3016 case SWM_ARG_ID_STACKINIT:
3017 ws->l_state.horizontal_mwin = 1;
3018 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3019 ws->l_state.horizontal_stacks = 1;
3021 case SWM_ARG_ID_MASTERSHRINK:
3022 if (ws->l_state.horizontal_msize > 1)
3023 ws->l_state.horizontal_msize--;
3025 case SWM_ARG_ID_MASTERGROW:
3026 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3027 ws->l_state.horizontal_msize++;
3029 case SWM_ARG_ID_MASTERADD:
3030 ws->l_state.horizontal_mwin++;
3032 case SWM_ARG_ID_MASTERDEL:
3033 if (ws->l_state.horizontal_mwin > 0)
3034 ws->l_state.horizontal_mwin--;
3036 case SWM_ARG_ID_STACKINC:
3037 ws->l_state.horizontal_stacks++;
3039 case SWM_ARG_ID_STACKDEC:
3040 if (ws->l_state.horizontal_stacks > 1)
3041 ws->l_state.horizontal_stacks--;
3049 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3051 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3053 stack_master(ws, g, 1, 0);
3056 /* fullscreen view */
3058 max_stack(struct workspace *ws, struct swm_geometry *g)
3061 struct swm_geometry gg = *g;
3062 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3066 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3071 winno = count_win(ws, 0);
3072 if (winno == 0 && count_win(ws, 1) == 0)
3075 TAILQ_FOREACH(win, &ws->winlist, entry) {
3076 if (win->transient) {
3078 parent = find_window(win->transient);
3082 if (win->floating && win->floatmaxed == 0 ) {
3084 * retain geometry for retrieval on exit from
3087 store_float_geom(win, ws->r);
3088 win->floatmaxed = 1;
3091 /* only reconfigure if necessary */
3092 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3094 bzero(&wc, sizeof wc);
3095 win->g.x = wc.x = gg.x;
3096 win->g.y = wc.y = gg.y;
3098 wc.border_width = border_width;
3099 win->g.w = wc.width = gg.w;
3100 win->g.h = wc.height = gg.h;
3102 wc.border_width = 0;
3103 win->g.w = wc.width = gg.w + 2 * border_width;
3104 win->g.h = wc.height = gg.h + 2 * border_width;
3106 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3107 XConfigureWindow(display, win->id, mask, &wc);
3109 /* unmap only if we don't have multi screen */
3110 if (win != ws->focus)
3111 if (!(ScreenCount(display) > 1 || outputs > 1))
3115 /* put the last transient on top */
3118 XMapRaised(display, parent->id);
3119 stack_floater(wintrans, ws->r);
3120 focus_magic(wintrans);
3125 send_to_ws(struct swm_region *r, union arg *args)
3127 int wsid = args->id;
3128 struct ws_win *win = NULL, *parent;
3129 struct workspace *ws, *nws;
3130 Atom ws_idx_atom = 0;
3131 unsigned char ws_idx_str[SWM_PROPLEN];
3134 if (r && r->ws && r->ws->focus)
3140 if (win->ws->idx == wsid)
3143 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3146 nws = &win->s->ws[wsid];
3148 a.id = SWM_ARG_ID_FOCUSPREV;
3150 if (win->transient) {
3151 parent = find_window(win->transient);
3153 unmap_window(parent);
3154 TAILQ_REMOVE(&ws->winlist, parent, entry);
3155 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3160 TAILQ_REMOVE(&ws->winlist, win, entry);
3161 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3162 if (TAILQ_EMPTY(&ws->winlist))
3163 r->ws->focus = NULL;
3166 /* Try to update the window's workspace property */
3167 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3169 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3171 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3173 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3174 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3181 pressbutton(struct swm_region *r, union arg *args)
3183 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3184 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3188 raise_toggle(struct swm_region *r, union arg *args)
3190 if (r && r->ws == NULL)
3193 r->ws->always_raise = !r->ws->always_raise;
3195 /* bring floaters back to top */
3196 if (r->ws->always_raise == 0)
3201 iconify(struct swm_region *r, union arg *args)
3205 if (r->ws->focus == NULL)
3207 unmap_window(r->ws->focus);
3208 update_iconic(r->ws->focus, 1);
3210 if (focus_mode == SWM_FOCUS_DEFAULT)
3211 drain_enter_notify();
3212 r->ws->focus = NULL;
3213 a.id = SWM_ARG_ID_FOCUSCUR;
3218 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3219 unsigned long *slen)
3222 unsigned long nitems, nbytes, nextra;
3223 unsigned char *prop = NULL;
3226 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3227 &rettype, &retfmt, &nitems, &nbytes, &prop);
3228 if (status != Success)
3232 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3233 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3234 if (status != Success) {
3238 if (rettype != stype) {
3247 uniconify(struct swm_region *r, union arg *args)
3251 unsigned char *name;
3255 DNPRINTF(SWM_D_MISC, "uniconify\n");
3257 if (r && r->ws == NULL)
3260 /* make sure we have anything to uniconify */
3261 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3262 if (win->ws == NULL)
3263 continue; /* should never happen */
3264 if (win->iconic == 0)
3272 search_resp_action = SWM_SEARCH_UNICONIFY;
3274 spawn_select(r, args, "search", &searchpid);
3276 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3279 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3280 if (win->ws == NULL)
3281 continue; /* should never happen */
3282 if (win->iconic == 0)
3285 name = get_win_name(display, win->id, a_netwmname, a_utf8_string,
3289 fprintf(lfile, "%s.%lu\n", name, win->id);
3297 name_workspace(struct swm_region *r, union arg *args)
3301 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3307 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3309 spawn_select(r, args, "name_workspace", &searchpid);
3311 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3314 fprintf(lfile, "%s", "");
3319 search_workspace(struct swm_region *r, union arg *args)
3322 struct workspace *ws;
3325 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3331 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3333 spawn_select(r, args, "search", &searchpid);
3335 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3338 for (i = 0; i < SWM_WS_MAX; i++) {
3342 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3343 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3350 search_win_cleanup(void)
3352 struct search_window *sw = NULL;
3354 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3355 XDestroyWindow(display, sw->indicator);
3356 TAILQ_REMOVE(&search_wl, sw, entry);
3362 search_win(struct swm_region *r, union arg *args)
3364 struct ws_win *win = NULL;
3365 struct search_window *sw = NULL;
3375 DNPRINTF(SWM_D_MISC, "search_win\n");
3378 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3380 spawn_select(r, args, "search", &searchpid);
3382 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3385 TAILQ_INIT(&search_wl);
3388 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3389 if (win->iconic == 1)
3392 sw = calloc(1, sizeof(struct search_window));
3394 fprintf(stderr, "search_win: calloc: %s", strerror(errno));
3396 search_win_cleanup();
3402 snprintf(s, sizeof s, "%d", i);
3404 textwidth = XTextWidth(bar_fs, s, len);
3406 w = XCreateSimpleWindow(display,
3407 win->id, 0, 0, textwidth + 12,
3408 bar_fs->ascent + bar_fs->descent + 4, 1,
3409 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3410 r->s->c[SWM_S_COLOR_FOCUS].color);
3413 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3415 gc = XCreateGC(display, w, 0, &gcv);
3416 XSetFont(display, gc, bar_fs->fid);
3417 XMapRaised(display, w);
3418 XSetForeground(display, gc, r->s->c[SWM_S_COLOR_BAR].color);
3420 XDrawString(display, w, gc, 6, bar_fs->ascent + 2, s, len);
3422 fprintf(lfile, "%d\n", i);
3430 search_resp_uniconify(char *resp, unsigned long len)
3432 unsigned char *name;
3436 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp %s\n", resp);
3438 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3439 if (win->iconic == 0)
3441 name = get_win_name(display, win->id, a_netwmname, a_utf8_string, &len);
3444 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3449 if (strncmp(s, resp, len) == 0) {
3450 /* XXX this should be a callback to generalize */
3451 update_iconic(win, 0);
3460 search_resp_name_workspace(char *resp, unsigned long len)
3462 struct workspace *ws;
3464 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp %s\n", resp);
3466 if (search_r->ws == NULL)
3471 free(search_r->ws->name);
3472 search_r->ws->name = NULL;
3476 ws->name = strdup(resp);
3477 if (ws->name == NULL) {
3478 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: strdup: %s",
3486 search_resp_search_workspace(char *resp, unsigned long len)
3493 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp %s\n", resp);
3497 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3504 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3506 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3513 switchws(search_r, &a);
3517 search_resp_search_window(char *resp, unsigned long len)
3522 struct search_window *sw;
3524 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp %s\n", resp);
3528 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3533 idx = strtonum(s, 1, INT_MAX, &errstr);
3535 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3542 TAILQ_FOREACH(sw, &search_wl, entry)
3543 if (idx == sw->idx) {
3549 #define MAX_RESP_LEN 1024
3552 search_do_resp(void)
3558 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3563 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3564 fprintf(stderr, "search: calloc\n");
3568 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3570 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3573 resp[rbytes] = '\0';
3576 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3577 * newline, so work around that by sanitizing the resp now.
3579 resp[strcspn(resp, "\n")] = '\0';
3582 switch (search_resp_action) {
3583 case SWM_SEARCH_UNICONIFY:
3584 search_resp_uniconify(resp, len);
3586 case SWM_SEARCH_NAME_WORKSPACE:
3587 search_resp_name_workspace(resp, len);
3589 case SWM_SEARCH_SEARCH_WORKSPACE:
3590 search_resp_search_workspace(resp, len);
3592 case SWM_SEARCH_SEARCH_WINDOW:
3593 search_resp_search_window(resp, len);
3598 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3599 search_win_cleanup();
3601 search_resp_action = SWM_SEARCH_NONE;
3602 close(select_resp_pipe[0]);
3607 wkill(struct swm_region *r, union arg *args)
3609 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3611 if (r->ws->focus == NULL)
3614 if (args->id == SWM_ARG_ID_KILLWINDOW)
3615 XKillClient(display, r->ws->focus->id);
3617 if (r->ws->focus->can_delete)
3618 client_msg(r->ws->focus, adelete);
3623 floating_toggle_win(struct ws_win *win)
3625 struct swm_region *r;
3635 /* reject floating toggles in max stack mode */
3636 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3639 if (win->floating) {
3640 if (!win->floatmaxed) {
3641 /* retain position for refloat */
3642 store_float_geom(win, r);
3646 if (win->g_floatvalid) {
3647 /* refloat at last floating relative position */
3648 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3649 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3650 win->g.w = win->g_float.w;
3651 win->g.h = win->g_float.h;
3656 ewmh_update_actions(win);
3662 floating_toggle(struct swm_region *r, union arg *args)
3664 struct ws_win *win = r->ws->focus;
3670 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3671 _NET_WM_STATE_TOGGLE);
3674 if (focus_mode == SWM_FOCUS_DEFAULT)
3675 drain_enter_notify();
3677 if (win == win->ws->focus) {
3678 a.id = SWM_ARG_ID_FOCUSCUR;
3679 focus(win->ws->r, &a);
3684 resize_window(struct ws_win *win, int center)
3688 struct swm_region *r;
3690 r = root_to_region(win->wa.root);
3691 bzero(&wc, sizeof wc);
3692 mask = CWBorderWidth | CWWidth | CWHeight;
3693 wc.border_width = border_width;
3694 wc.width = win->g.w;
3695 wc.height = win->g.h;
3696 if (center == SWM_ARG_ID_CENTER) {
3697 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3698 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3702 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3703 win->id, wc.x, wc.y, wc.width, wc.height);
3705 XConfigureWindow(display, win->id, mask, &wc);
3708 #define SWM_RESIZE_STEPS (50)
3711 resize(struct ws_win *win, union arg *args)
3715 struct swm_region *r = NULL;
3717 int resize_step = 0;
3723 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3724 win->id, win->floating, win->transient);
3726 if (!(win->transient != 0 || win->floating != 0))
3729 /* reject resizes in max mode for floaters (transient ok) */
3730 if (win->floatmaxed)
3734 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3740 case SWM_ARG_ID_WIDTHSHRINK:
3741 win->g.w -= SWM_RESIZE_STEPS;
3744 case SWM_ARG_ID_WIDTHGROW:
3745 win->g.w += SWM_RESIZE_STEPS;
3748 case SWM_ARG_ID_HEIGHTSHRINK:
3749 win->g.h -= SWM_RESIZE_STEPS;
3752 case SWM_ARG_ID_HEIGHTGROW:
3753 win->g.h += SWM_RESIZE_STEPS;
3760 resize_window(win, 0);
3761 store_float_geom(win,r);
3765 if (focus_mode == SWM_FOCUS_DEFAULT)
3766 drain_enter_notify();
3768 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3769 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3772 /* place pointer at bottom left corner or nearest point inside r */
3773 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3774 relx = win->g.w - 1;
3776 relx = r->g.x + r->g.w - win->g.x - 1;
3778 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3779 rely = win->g.h - 1;
3781 rely = r->g.y + r->g.h - win->g.y - 1;
3783 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3785 XMaskEvent(display, MOUSEMASK | ExposureMask |
3786 SubstructureRedirectMask, &ev);
3788 case ConfigureRequest:
3791 handler[ev.type](&ev);
3794 /* do not allow resize outside of region */
3795 if ( ev.xmotion.x_root < r->g.x ||
3796 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3797 ev.xmotion.y_root < r->g.y ||
3798 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3801 if (ev.xmotion.x <= 1)
3803 if (ev.xmotion.y <= 1)
3805 win->g.w = ev.xmotion.x + 1;
3806 win->g.h = ev.xmotion.y + 1;
3808 /* not free, don't sync more than 120 times / second */
3809 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3810 time = ev.xmotion.time;
3811 XSync(display, False);
3812 resize_window(win, args->id);
3816 } while (ev.type != ButtonRelease);
3818 XSync(display, False);
3819 resize_window(win, args->id);
3821 store_float_geom(win,r);
3823 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3825 XUngrabPointer(display, CurrentTime);
3828 drain_enter_notify();
3832 resize_step(struct swm_region *r, union arg *args)
3834 struct ws_win *win = NULL;
3836 if (r && r->ws && r->ws->focus)
3846 move_window(struct ws_win *win)
3851 bzero(&wc, sizeof wc);
3855 wc.border_width = border_width;
3857 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3858 win->id, wc.x, wc.y, wc.width, wc.height);
3860 XConfigureWindow(display, win->id, mask, &wc);
3863 #define SWM_MOVE_STEPS (50)
3866 move(struct ws_win *win, union arg *args)
3871 struct swm_region *r = NULL;
3877 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3878 win->id, win->floating, win->transient);
3880 /* in max_stack mode should only move transients */
3881 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3885 if (win->floating == 0 && !win->transient) {
3886 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3889 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3896 case SWM_ARG_ID_MOVELEFT:
3897 win->g.x -= (SWM_MOVE_STEPS - border_width);
3900 case SWM_ARG_ID_MOVERIGHT:
3901 win->g.x += (SWM_MOVE_STEPS - border_width);
3904 case SWM_ARG_ID_MOVEUP:
3905 win->g.y -= (SWM_MOVE_STEPS - border_width);
3908 case SWM_ARG_ID_MOVEDOWN:
3909 win->g.y += (SWM_MOVE_STEPS - border_width);
3917 store_float_geom(win,r);
3922 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3923 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3925 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3927 XMaskEvent(display, MOUSEMASK | ExposureMask |
3928 SubstructureRedirectMask, &ev);
3930 case ConfigureRequest:
3933 handler[ev.type](&ev);
3936 /* don't allow to move window origin out of region */
3937 if ( ev.xmotion.x_root < r->g.x ||
3938 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3939 ev.xmotion.y_root < r->g.y ||
3940 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3943 win->g.x = ev.xmotion.x_root - border_width;
3944 win->g.y = ev.xmotion.y_root - border_width;
3946 /* not free, don't sync more than 120 times / second */
3947 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3948 time = ev.xmotion.time;
3949 XSync(display, False);
3954 } while (ev.type != ButtonRelease);
3956 XSync(display, False);
3959 store_float_geom(win,r);
3960 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3961 XUngrabPointer(display, CurrentTime);
3964 drain_enter_notify();
3968 move_step(struct swm_region *r, union arg *args)
3970 struct ws_win *win = NULL;
3972 if (r && r->ws && r->ws->focus)
3977 if (!(win->transient != 0 || win->floating != 0))
3984 /* user/key callable function IDs */
4054 kf_search_workspace,
4056 kf_dumpwins, /* MUST BE LAST */
4060 /* key definitions */
4062 dummykeyfunc(struct swm_region *r, union arg *args)
4067 legacyfunc(struct swm_region *r, union arg *args)
4072 char name[SWM_FUNCNAME_LEN];
4073 void (*func)(struct swm_region *r, union arg *);
4075 } keyfuncs[kf_invalid + 1] = {
4076 /* name function argument */
4077 { "cycle_layout", cycle_layout, {0} },
4078 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4079 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4080 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4081 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4082 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4083 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4084 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4085 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4086 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4087 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4088 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4089 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4090 { "spawn_term", spawnterm, {.argv = spawn_term} },
4091 { "spawn_menu", legacyfunc, {0} },
4092 { "quit", quit, {0} },
4093 { "restart", restart, {0} },
4094 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4095 { "ws_1", switchws, {.id = 0} },
4096 { "ws_2", switchws, {.id = 1} },
4097 { "ws_3", switchws, {.id = 2} },
4098 { "ws_4", switchws, {.id = 3} },
4099 { "ws_5", switchws, {.id = 4} },
4100 { "ws_6", switchws, {.id = 5} },
4101 { "ws_7", switchws, {.id = 6} },
4102 { "ws_8", switchws, {.id = 7} },
4103 { "ws_9", switchws, {.id = 8} },
4104 { "ws_10", switchws, {.id = 9} },
4105 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4106 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4107 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4108 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4109 { "ws_prior", priorws, {0} },
4110 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4111 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4112 { "mvws_1", send_to_ws, {.id = 0} },
4113 { "mvws_2", send_to_ws, {.id = 1} },
4114 { "mvws_3", send_to_ws, {.id = 2} },
4115 { "mvws_4", send_to_ws, {.id = 3} },
4116 { "mvws_5", send_to_ws, {.id = 4} },
4117 { "mvws_6", send_to_ws, {.id = 5} },
4118 { "mvws_7", send_to_ws, {.id = 6} },
4119 { "mvws_8", send_to_ws, {.id = 7} },
4120 { "mvws_9", send_to_ws, {.id = 8} },
4121 { "mvws_10", send_to_ws, {.id = 9} },
4122 { "bar_toggle", bar_toggle, {0} },
4123 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4124 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4125 { "screenshot_all", legacyfunc, {0} },
4126 { "screenshot_wind", legacyfunc, {0} },
4127 { "float_toggle", floating_toggle,{0} },
4128 { "version", version, {0} },
4129 { "spawn_lock", legacyfunc, {0} },
4130 { "spawn_initscr", legacyfunc, {0} },
4131 { "spawn_custom", dummykeyfunc, {0} },
4132 { "iconify", iconify, {0} },
4133 { "uniconify", uniconify, {0} },
4134 { "raise_toggle", raise_toggle, {0} },
4135 { "button2", pressbutton, {2} },
4136 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4137 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4138 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4139 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4140 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4141 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4142 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4143 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4144 { "name_workspace", name_workspace, {0} },
4145 { "search_workspace", search_workspace, {0} },
4146 { "search_win", search_win, {0} },
4147 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4148 { "invalid key func", NULL, {0} },
4153 enum keyfuncid funcid;
4156 int keys_size = 0, keys_length = 0;
4157 struct key *keys = NULL;
4160 enum { client_click, root_click };
4162 unsigned int action;
4164 unsigned int button;
4165 void (*func)(struct ws_win *, union arg *);
4168 /* action key mouse button func args */
4169 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4170 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4171 { client_click, MODKEY, Button1, move, {0} },
4175 update_modkey(unsigned int mod)
4180 for (i = 0; i < keys_length; i++)
4181 if (keys[i].mod & ShiftMask)
4182 keys[i].mod = mod | ShiftMask;
4186 for (i = 0; i < LENGTH(buttons); i++)
4187 if (buttons[i].mask & ShiftMask)
4188 buttons[i].mask = mod | ShiftMask;
4190 buttons[i].mask = mod;
4200 int spawns_size = 0, spawns_length = 0;
4201 struct spawn_prog *spawns = NULL;
4204 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4207 struct spawn_prog *prog = NULL;
4209 char *ap, **real_args;
4211 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
4214 for (i = 0; i < spawns_length; i++) {
4215 if (!strcasecmp(spawn_name, spawns[i].name))
4219 fprintf(stderr, "spawn_custom: program %s not found\n",
4224 /* make room for expanded args */
4225 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4226 err(1, "spawn_custom: calloc real_args");
4228 /* expand spawn_args into real_args */
4229 for (i = 0; i < prog->argc; i++) {
4231 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
4232 if (!strcasecmp(ap, "$bar_border")) {
4234 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4236 err(1, "spawn_custom border color");
4237 } else if (!strcasecmp(ap, "$bar_color")) {
4239 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4241 err(1, "spawn_custom bar color");
4242 } else if (!strcasecmp(ap, "$bar_font")) {
4243 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
4245 err(1, "spawn_custom bar fonts");
4246 } else if (!strcasecmp(ap, "$bar_font_color")) {
4248 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4250 err(1, "spawn_custom color font");
4251 } else if (!strcasecmp(ap, "$color_focus")) {
4253 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4255 err(1, "spawn_custom color focus");
4256 } else if (!strcasecmp(ap, "$color_unfocus")) {
4258 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4260 err(1, "spawn_custom color unfocus");
4262 /* no match --> copy as is */
4263 if ((real_args[i] = strdup(ap)) == NULL)
4264 err(1, "spawn_custom strdup(ap)");
4266 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
4271 if ((swm_debug & SWM_D_SPAWN) != 0) {
4272 fprintf(stderr, "spawn_custom: result = ");
4273 for (i = 0; i < prog->argc; i++)
4274 fprintf(stderr, "\"%s\" ", real_args[i]);
4275 fprintf(stderr, "\n");
4278 *ret_args = real_args;
4279 return (prog->argc);
4283 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4289 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4293 spawn(r->ws->idx, &a, 1);
4295 for (i = 0; i < spawn_argc; i++)
4301 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4307 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4311 if (pipe(select_list_pipe) == -1)
4312 err(1, "pipe error");
4313 if (pipe(select_resp_pipe) == -1)
4314 err(1, "pipe error");
4316 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4317 err(1, "could not disable SIGPIPE");
4318 switch (*pid = fork()) {
4320 err(1, "cannot fork");
4323 if (dup2(select_list_pipe[0], 0) == -1)
4325 if (dup2(select_resp_pipe[1], 1) == -1)
4327 close(select_list_pipe[1]);
4328 close(select_resp_pipe[0]);
4329 spawn(r->ws->idx, &a, 0);
4331 default: /* parent */
4332 close(select_list_pipe[0]);
4333 close(select_resp_pipe[1]);
4337 for (i = 0; i < spawn_argc; i++)
4343 setspawn(struct spawn_prog *prog)
4347 if (prog == NULL || prog->name == NULL)
4351 for (i = 0; i < spawns_length; i++) {
4352 if (!strcmp(spawns[i].name, prog->name)) {
4354 if (prog->argv == NULL) {
4356 DNPRINTF(SWM_D_SPAWN,
4357 "setspawn: delete #%d %s\n",
4359 free(spawns[i].name);
4360 for (j = 0; j < spawns[i].argc; j++)
4361 free(spawns[i].argv[j]);
4362 free(spawns[i].argv);
4363 j = spawns_length - 1;
4365 spawns[i] = spawns[j];
4370 DNPRINTF(SWM_D_SPAWN,
4371 "setspawn: replace #%d %s\n",
4373 free(spawns[i].name);
4374 for (j = 0; j < spawns[i].argc; j++)
4375 free(spawns[i].argv[j]);
4376 free(spawns[i].argv);
4379 /* found case handled */
4385 if (prog->argv == NULL) {
4387 "error: setspawn: cannot find program %s", prog->name);
4392 /* not found: add */
4393 if (spawns_size == 0 || spawns == NULL) {
4395 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4396 spawns = malloc((size_t)spawns_size *
4397 sizeof(struct spawn_prog));
4398 if (spawns == NULL) {
4399 fprintf(stderr, "setspawn: malloc failed\n");
4403 } else if (spawns_length == spawns_size) {
4405 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4406 spawns = realloc(spawns, (size_t)spawns_size *
4407 sizeof(struct spawn_prog));
4408 if (spawns == NULL) {
4409 fprintf(stderr, "setspawn: realloc failed\n");
4415 if (spawns_length < spawns_size) {
4416 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4417 spawns_length, prog->name);
4418 i = spawns_length++;
4421 fprintf(stderr, "spawns array problem?\n");
4422 if (spawns == NULL) {
4423 fprintf(stderr, "spawns array is NULL!\n");
4431 setconfspawn(char *selector, char *value, int flags)
4433 struct spawn_prog *prog;
4434 char *vp, *cp, *word;
4436 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4437 if ((prog = calloc(1, sizeof *prog)) == NULL)
4438 err(1, "setconfspawn: calloc prog");
4439 prog->name = strdup(selector);
4440 if (prog->name == NULL)
4441 err(1, "setconfspawn prog->name");
4442 if ((cp = vp = strdup(value)) == NULL)
4443 err(1, "setconfspawn: strdup(value) ");
4444 while ((word = strsep(&cp, " \t")) != NULL) {
4445 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4447 cp += (long)strspn(cp, " \t");
4448 if (strlen(word) > 0) {
4450 if ((prog->argv = realloc(prog->argv,
4451 prog->argc * sizeof(char *))) == NULL)
4452 err(1, "setconfspawn: realloc");
4453 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4454 err(1, "setconfspawn: strdup");
4461 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4468 setconfspawn("term", "xterm", 0);
4469 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4470 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4471 setconfspawn("lock", "xlock", 0);
4472 setconfspawn("initscr", "initscreen.sh", 0);
4473 setconfspawn("menu", "dmenu_run"
4476 " -nf $bar_font_color"
4478 " -sf $bar_color", 0);
4479 setconfspawn("search", "dmenu"
4483 " -nf $bar_font_color"
4485 " -sf $bar_color", 0);
4486 setconfspawn("name_workspace", "dmenu"
4490 " -nf $bar_font_color"
4492 " -sf $bar_color", 0);
4496 #define SWM_MODNAME_SIZE 32
4497 #define SWM_KEY_WS "\n+ \t"
4499 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4503 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4504 if (mod == NULL || ks == NULL) {
4505 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4508 if (keystr == NULL || strlen(keystr) == 0) {
4509 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4515 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4516 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4518 cp += (long)strspn(cp, SWM_KEY_WS);
4519 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4521 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4523 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4525 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4527 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4529 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4531 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4532 *mod |= ControlMask;
4534 *ks = XStringToKeysym(name);
4535 XConvertCase(*ks, ks, &uks);
4536 if (ks == NoSymbol) {
4538 "parsekeys: invalid key %s\n",
4544 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4549 strdupsafe(char *str)
4554 return (strdup(str));
4558 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4562 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4563 keyfuncs[kfid].name, spawn_name);
4565 for (i = 0; i < keys_length; i++) {
4566 if (keys[i].mod == mod && keys[i].keysym == ks) {
4567 if (kfid == kf_invalid) {
4570 "setkeybinding: delete #%d %s\n",
4571 i, keyfuncs[keys[i].funcid].name);
4572 free(keys[i].spawn_name);
4573 j = keys_length - 1;
4577 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4580 /* found: replace */
4582 "setkeybinding: replace #%d %s %s\n",
4583 i, keyfuncs[keys[i].funcid].name,
4585 free(keys[i].spawn_name);
4587 keys[i].keysym = ks;
4588 keys[i].funcid = kfid;
4589 keys[i].spawn_name = strdupsafe(spawn_name);
4590 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4595 if (kfid == kf_invalid) {
4597 "error: setkeybinding: cannot find mod/key combination");
4598 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4601 /* not found: add */
4602 if (keys_size == 0 || keys == NULL) {
4604 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4605 keys = malloc((size_t)keys_size * sizeof(struct key));
4607 fprintf(stderr, "malloc failed\n");
4611 } else if (keys_length == keys_size) {
4613 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4614 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4616 fprintf(stderr, "realloc failed\n");
4621 if (keys_length < keys_size) {
4623 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4624 j, keyfuncs[kfid].name, spawn_name);
4626 keys[j].keysym = ks;
4627 keys[j].funcid = kfid;
4628 keys[j].spawn_name = strdupsafe(spawn_name);
4630 fprintf(stderr, "keys array problem?\n");
4632 fprintf(stderr, "keys array problem\n");
4636 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4640 setconfbinding(char *selector, char *value, int flags)
4642 enum keyfuncid kfid;
4646 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4647 if (selector == NULL) {
4648 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4649 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4651 setkeybinding(mod, ks, kfid, NULL);
4656 /* search by key function name */
4657 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4658 if (strncasecmp(selector, keyfuncs[kfid].name,
4659 SWM_FUNCNAME_LEN) == 0) {
4660 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4662 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4663 setkeybinding(mod, ks, kfid, NULL);
4669 /* search by custom spawn name */
4670 for (i = 0; i < spawns_length; i++) {
4671 if (strcasecmp(selector, spawns[i].name) == 0) {
4672 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4674 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4675 setkeybinding(mod, ks, kf_spawn_custom,
4682 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4689 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4690 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4691 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4692 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4693 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4694 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4695 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4696 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4697 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4698 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4699 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4700 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4701 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4702 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4703 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4704 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4705 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4706 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4707 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4708 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4709 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4710 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4711 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4712 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4713 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4714 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4715 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4716 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4717 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4718 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4719 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4720 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4721 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4722 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4723 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4724 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4725 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4726 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4727 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4728 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4729 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4730 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4731 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4732 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4733 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4734 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4735 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4736 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4737 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4738 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4739 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4740 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4741 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4742 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4743 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4744 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4745 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4746 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4747 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4748 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4749 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4750 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4751 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4752 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4753 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4754 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4755 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4756 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4757 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4758 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4759 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4761 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4770 /* clear all key bindings, if any */
4771 for (i = 0; i < keys_length; i++)
4772 free(keys[i].spawn_name);
4777 setkeymapping(char *selector, char *value, int flags)
4779 char keymapping_file[PATH_MAX];
4780 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4781 if (value[0] == '~')
4782 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4783 pwd->pw_dir, &value[1]);
4785 strlcpy(keymapping_file, value, sizeof keymapping_file);
4787 /* load new key bindings; if it fails, revert to default bindings */
4788 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4792 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4797 updatenumlockmask(void)
4800 XModifierKeymap *modmap;
4802 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4804 modmap = XGetModifierMapping(display);
4805 for (i = 0; i < 8; i++)
4806 for (j = 0; j < modmap->max_keypermod; j++)
4807 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4808 == XKeysymToKeycode(display, XK_Num_Lock))
4809 numlockmask = (1 << i);
4811 XFreeModifiermap(modmap);
4817 unsigned int i, j, k;
4819 unsigned int modifiers[] =
4820 { 0, LockMask, numlockmask, numlockmask | LockMask };
4822 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4823 updatenumlockmask();
4825 for (k = 0; k < ScreenCount(display); k++) {
4826 if (TAILQ_EMPTY(&screens[k].rl))
4828 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4829 for (i = 0; i < keys_length; i++) {
4830 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4831 for (j = 0; j < LENGTH(modifiers); j++)
4832 XGrabKey(display, code,
4833 keys[i].mod | modifiers[j],
4834 screens[k].root, True,
4835 GrabModeAsync, GrabModeAsync);
4841 grabbuttons(struct ws_win *win, int focused)
4844 unsigned int modifiers[] =
4845 { 0, LockMask, numlockmask, numlockmask|LockMask };
4847 updatenumlockmask();
4848 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4850 for (i = 0; i < LENGTH(buttons); i++)
4851 if (buttons[i].action == client_click)
4852 for (j = 0; j < LENGTH(modifiers); j++)
4853 XGrabButton(display, buttons[i].button,
4854 buttons[i].mask | modifiers[j],
4855 win->id, False, BUTTONMASK,
4856 GrabModeAsync, GrabModeSync, None,
4859 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4860 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4863 const char *quirkname[] = {
4864 "NONE", /* config string for "no value" */
4873 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4874 #define SWM_Q_WS "\n|+ \t"
4876 parsequirks(char *qstr, unsigned long *quirk)
4886 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4888 cp += (long)strspn(cp, SWM_Q_WS);
4889 for (i = 0; i < LENGTH(quirkname); i++) {
4890 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4891 DNPRINTF(SWM_D_QUIRK,
4892 "parsequirks: %s\n", name);
4897 *quirk |= 1 << (i-1);
4901 if (i >= LENGTH(quirkname)) {
4902 DNPRINTF(SWM_D_QUIRK,
4903 "parsequirks: invalid quirk [%s]\n", name);
4911 setquirk(const char *class, const char *name, const int quirk)
4916 for (i = 0; i < quirks_length; i++) {
4917 if (!strcmp(quirks[i].class, class) &&
4918 !strcmp(quirks[i].name, name)) {
4921 DNPRINTF(SWM_D_QUIRK,
4922 "setquirk: delete #%d %s:%s\n",
4923 i, quirks[i].class, quirks[i].name);
4924 free(quirks[i].class);
4925 free(quirks[i].name);
4926 j = quirks_length - 1;
4928 quirks[i] = quirks[j];
4932 /* found: replace */
4933 DNPRINTF(SWM_D_QUIRK,
4934 "setquirk: replace #%d %s:%s\n",
4935 i, quirks[i].class, quirks[i].name);
4936 free(quirks[i].class);
4937 free(quirks[i].name);
4938 quirks[i].class = strdup(class);
4939 quirks[i].name = strdup(name);
4940 quirks[i].quirk = quirk;
4947 "error: setquirk: cannot find class/name combination");
4950 /* not found: add */
4951 if (quirks_size == 0 || quirks == NULL) {
4953 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4954 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4955 if (quirks == NULL) {
4956 fprintf(stderr, "setquirk: malloc failed\n");
4960 } else if (quirks_length == quirks_size) {
4962 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4963 quirks = realloc(quirks,
4964 (size_t)quirks_size * sizeof(struct quirk));
4965 if (quirks == NULL) {
4966 fprintf(stderr, "setquirk: realloc failed\n");
4971 if (quirks_length < quirks_size) {
4972 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4973 j = quirks_length++;
4974 quirks[j].class = strdup(class);
4975 quirks[j].name = strdup(name);
4976 quirks[j].quirk = quirk;
4978 fprintf(stderr, "quirks array problem?\n");
4979 if (quirks == NULL) {
4980 fprintf(stderr, "quirks array problem!\n");
4987 setconfquirk(char *selector, char *value, int flags)
4989 char *cp, *class, *name;
4991 unsigned long quirks;
4992 if (selector == NULL)
4994 if ((cp = strchr(selector, ':')) == NULL)
4999 if ((retval = parsequirks(value, &quirks)) == 0)
5000 setquirk(class, name, quirks);
5007 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5008 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5009 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5010 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5011 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5012 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5013 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5014 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5015 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5016 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5017 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5018 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5019 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5022 /* conf file stuff */
5023 #define SWM_CONF_FILE "scrotwm.conf"
5025 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5026 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5027 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5028 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5029 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5030 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5031 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5032 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5033 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5037 setconfvalue(char *selector, char *value, int flags)
5042 case SWM_S_BAR_DELAY:
5043 bar_delay = atoi(value);
5045 case SWM_S_BAR_ENABLED:
5046 bar_enabled = atoi(value);
5048 case SWM_S_BAR_BORDER_WIDTH:
5049 bar_border_width = atoi(value);
5051 case SWM_S_BAR_AT_BOTTOM:
5052 bar_at_bottom = atoi(value);
5054 case SWM_S_BAR_JUSTIFY:
5055 if (!strcmp(value, "left"))
5056 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5057 else if (!strcmp(value, "center"))
5058 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5059 else if (!strcmp(value, "right"))
5060 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5062 errx(1, "invalid bar_justify");
5064 case SWM_S_STACK_ENABLED:
5065 stack_enabled = atoi(value);
5067 case SWM_S_CLOCK_ENABLED:
5068 clock_enabled = atoi(value);
5070 case SWM_S_CLOCK_FORMAT:
5071 #ifndef SWM_DENY_CLOCK_FORMAT
5073 if ((clock_format = strdup(value)) == NULL)
5074 err(1, "setconfvalue: clock_format");
5077 case SWM_S_CYCLE_EMPTY:
5078 cycle_empty = atoi(value);
5080 case SWM_S_CYCLE_VISIBLE:
5081 cycle_visible = atoi(value);
5083 case SWM_S_SS_ENABLED:
5084 ss_enabled = atoi(value);
5086 case SWM_S_TERM_WIDTH:
5087 term_width = atoi(value);
5089 case SWM_S_TITLE_CLASS_ENABLED:
5090 title_class_enabled = atoi(value);
5092 case SWM_S_WINDOW_NAME_ENABLED:
5093 window_name_enabled = atoi(value);
5095 case SWM_S_TITLE_NAME_ENABLED:
5096 title_name_enabled = atoi(value);
5098 case SWM_S_URGENT_ENABLED:
5099 urgent_enabled = atoi(value);
5101 case SWM_S_FOCUS_MODE:
5102 if (!strcmp(value, "default"))
5103 focus_mode = SWM_FOCUS_DEFAULT;
5104 else if (!strcmp(value, "follow_cursor"))
5105 focus_mode = SWM_FOCUS_FOLLOW;
5106 else if (!strcmp(value, "synergy"))
5107 focus_mode = SWM_FOCUS_SYNERGY;
5109 errx(1, "focus_mode");
5111 case SWM_S_DISABLE_BORDER:
5112 disable_border = atoi(value);
5114 case SWM_S_BORDER_WIDTH:
5115 border_width = atoi(value);
5117 case SWM_S_BAR_FONT:
5119 if ((bar_fonts[0] = strdup(value)) == NULL)
5120 err(1, "setconfvalue: bar_font");
5122 case SWM_S_BAR_ACTION:
5124 if ((bar_argv[0] = strdup(value)) == NULL)
5125 err(1, "setconfvalue: bar_action");
5127 case SWM_S_SPAWN_TERM:
5128 free(spawn_term[0]);
5129 if ((spawn_term[0] = strdup(value)) == NULL)
5130 err(1, "setconfvalue: spawn_term");
5134 case SWM_S_DIALOG_RATIO:
5135 dialog_ratio = atof(value);
5136 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5139 case SWM_S_VERBOSE_LAYOUT:
5140 verbose_layout = atoi(value);
5141 for (i = 0; layouts[i].l_stack != NULL; i++) {
5143 layouts[i].l_string = fancy_stacker;
5145 layouts[i].l_string = plain_stacker;
5155 setconfmodkey(char *selector, char *value, int flags)
5157 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5158 update_modkey(Mod1Mask);
5159 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5160 update_modkey(Mod2Mask);
5161 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5162 update_modkey(Mod3Mask);
5163 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5164 update_modkey(Mod4Mask);
5171 setconfcolor(char *selector, char *value, int flags)
5173 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5178 setconfregion(char *selector, char *value, int flags)
5180 custom_region(value);
5185 setautorun(char *selector, char *value, int flags)
5195 if (getenv("SWM_STARTED"))
5199 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5200 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5202 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5203 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5206 * This is a little intricate
5208 * If the pid already exists we simply reuse it because it means it was
5209 * used before AND not claimed by manage_window. We get away with
5210 * altering it in the parent after INSERT because this can not be a race
5213 while ((ap = strsep(&sp, " \t")) != NULL) {
5216 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5218 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5219 err(1, "setautorun: realloc");
5220 a.argv[argc - 1] = ap;
5223 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5224 err(1, "setautorun: realloc");
5225 a.argv[argc] = NULL;
5227 if ((pid = fork()) == 0) {
5228 spawn(ws_id, &a, 1);
5237 p = calloc(1, sizeof *p);
5240 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5250 setlayout(char *selector, char *value, int flags)
5252 int ws_id, i, x, mg, ma, si, raise;
5253 int st = SWM_V_STACK;
5255 struct workspace *ws;
5257 if (getenv("SWM_STARTED"))
5261 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5262 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5263 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5264 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5267 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5268 errx(1, "layout: invalid workspace %d", ws_id + 1);
5270 if (!strcasecmp(s, "vertical"))
5272 else if (!strcasecmp(s, "horizontal"))
5274 else if (!strcasecmp(s, "fullscreen"))
5277 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5278 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5281 for (i = 0; i < ScreenCount(display); i++) {
5282 ws = (struct workspace *)&screens[i].ws;
5283 ws[ws_id].cur_layout = &layouts[st];
5285 ws[ws_id].always_raise = raise;
5286 if (st == SWM_MAX_STACK)
5290 for (x = 0; x < abs(mg); x++) {
5291 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5292 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5293 SWM_ARG_ID_MASTERSHRINK);
5297 for (x = 0; x < abs(ma); x++) {
5298 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5299 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5300 SWM_ARG_ID_MASTERDEL);
5304 for (x = 0; x < abs(si); x++) {
5305 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5306 si >= 0 ? SWM_ARG_ID_STACKINC :
5307 SWM_ARG_ID_STACKDEC);
5315 /* config options */
5316 struct config_option {
5318 int (*func)(char*, char*, int);
5321 struct config_option configopt[] = {
5322 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5323 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5324 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5325 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5326 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5327 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5328 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5329 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5330 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5331 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5332 { "keyboard_mapping", setkeymapping, 0 },
5333 { "bind", setconfbinding, 0 },
5334 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5335 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5336 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5337 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5338 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5339 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5340 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5341 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5342 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5343 { "modkey", setconfmodkey, 0 },
5344 { "program", setconfspawn, 0 },
5345 { "quirk", setconfquirk, 0 },
5346 { "region", setconfregion, 0 },
5347 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5348 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5349 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5350 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5351 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5352 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5353 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5354 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5355 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5356 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5357 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5358 { "autorun", setautorun, 0 },
5359 { "layout", setlayout, 0 },
5364 conf_load(char *filename, int keymapping)
5367 char *line, *cp, *optsub, *optval;
5368 size_t linelen, lineno = 0;
5369 int wordlen, i, optind;
5370 struct config_option *opt;
5372 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
5374 if (filename == NULL) {
5375 fprintf(stderr, "conf_load: no filename\n");
5378 if ((config = fopen(filename, "r")) == NULL) {
5379 warn("conf_load: fopen: %s", filename);
5383 while (!feof(config)) {
5384 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5387 err(1, "%s", filename);
5392 cp += strspn(cp, " \t\n"); /* eat whitespace */
5393 if (cp[0] == '\0') {
5398 /* get config option */
5399 wordlen = strcspn(cp, "=[ \t\n");
5401 warnx("%s: line %zd: no option found",
5406 for (i = 0; i < LENGTH(configopt); i++) {
5407 opt = &configopt[i];
5408 if (!strncasecmp(cp, opt->optname, wordlen) &&
5409 strlen(opt->optname) == wordlen) {
5415 warnx("%s: line %zd: unknown option %.*s",
5416 filename, lineno, wordlen, cp);
5419 if (keymapping && strcmp(opt->optname, "bind")) {
5420 warnx("%s: line %zd: invalid option %.*s",
5421 filename, lineno, wordlen, cp);
5425 cp += strspn(cp, " \t\n"); /* eat whitespace */
5426 /* get [selector] if any */
5430 wordlen = strcspn(cp, "]");
5433 warnx("%s: line %zd: syntax error",
5437 asprintf(&optsub, "%.*s", wordlen, cp);
5440 cp += strspn(cp, "] \t\n"); /* eat trailing */
5442 cp += strspn(cp, "= \t\n"); /* eat trailing */
5444 optval = strdup(cp);
5445 /* call function to deal with it all */
5446 if (configopt[optind].func(optsub, optval,
5447 configopt[optind].funcflags) != 0) {
5448 fprintf(stderr, "%s line %zd: %s\n",
5449 filename, lineno, line);
5450 errx(1, "%s: line %zd: invalid data for %s",
5451 filename, lineno, configopt[optind].optname);
5459 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5471 set_child_transient(struct ws_win *win, Window *trans)
5473 struct ws_win *parent, *w;
5474 XWMHints *wmh = NULL;
5475 struct swm_region *r;
5476 struct workspace *ws;
5478 parent = find_window(win->transient);
5480 parent->child_trans = win;
5482 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5483 " for %lu trans %lu\n", win->id, win->transient);
5485 if (win->hints == NULL) {
5486 fprintf(stderr, "no hints for %lu\n", win->id);
5490 r = root_to_region(win->wa.root);
5492 /* parent doen't exist in our window list */
5493 TAILQ_FOREACH(w, &ws->winlist, entry) {
5497 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5498 fprintf(stderr, "can't get hints for %lu\n",
5503 if (win->hints->window_group != wmh->window_group)
5506 w->child_trans = win;
5507 win->transient = w->id;
5509 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5510 "transient to %lu\n", win->transient);
5520 window_get_pid(Window win)
5522 Atom actual_type_return;
5523 int actual_format_return = 0;
5524 unsigned long nitems_return = 0;
5525 unsigned long bytes_after_return = 0;
5529 unsigned char *prop = NULL;
5531 if (XGetWindowProperty(display, win,
5532 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5533 XA_CARDINAL, &actual_type_return, &actual_format_return,
5534 &nitems_return, &bytes_after_return,
5535 (unsigned char**)(void*)&pid) != Success)
5537 if (actual_type_return != XA_CARDINAL)
5548 if (XGetWindowProperty(display, win,
5549 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5550 XA_STRING, &actual_type_return, &actual_format_return,
5551 &nitems_return, &bytes_after_return, &prop) != Success)
5553 if (actual_type_return != XA_STRING)
5558 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5559 /* ignore error because strtonum returns 0 anyway */
5566 manage_window(Window id)
5569 struct workspace *ws;
5570 struct ws_win *win, *ww;
5571 int format, i, ws_idx, n, border_me = 0;
5572 unsigned long nitems, bytes;
5573 Atom ws_idx_atom = 0, type;
5574 Atom *prot = NULL, *pp;
5575 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5576 struct swm_region *r;
5582 if ((win = find_window(id)) != NULL)
5583 return (win); /* already being managed */
5585 /* see if we are on the unmanaged list */
5586 if ((win = find_unmanaged_window(id)) != NULL) {
5587 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5589 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5590 if (win->transient) {
5591 set_child_transient(win, &trans);
5592 } if (trans && (ww = find_window(trans)))
5593 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5595 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5596 ewmh_update_actions(win);
5600 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5601 err(1, "calloc: failed to allocate memory for new window");
5605 /* see if we need to override the workspace */
5606 p = find_pid(window_get_pid(id));
5608 /* Get all the window data in one shot */
5609 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5611 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5612 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5614 XGetWindowAttributes(display, id, &win->wa);
5615 XGetWMNormalHints(display, id, &win->sh, &mask);
5616 win->hints = XGetWMHints(display, id);
5617 XGetTransientForHint(display, id, &trans);
5619 win->transient = trans;
5620 set_child_transient(win, &trans);
5621 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5622 win->id, win->transient);
5625 /* get supported protocols */
5626 if (XGetWMProtocols(display, id, &prot, &n)) {
5627 for (i = 0, pp = prot; i < n; i++, pp++) {
5628 if (*pp == takefocus)
5629 win->take_focus = 1;
5631 win->can_delete = 1;
5637 win->iconic = get_iconic(win);
5640 * Figure out where to put the window. If it was previously assigned to
5641 * a workspace (either by spawn() or manually moving), and isn't
5642 * transient, * put it in the same workspace
5644 r = root_to_region(win->wa.root);
5646 ws = &r->s->ws[p->ws];
5647 TAILQ_REMOVE(&pidlist, p, entry);
5650 } else if (prop && win->transient == 0) {
5651 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5652 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5654 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5657 ws = &r->s->ws[ws_idx];
5660 /* this should launch transients in the same ws as parent */
5662 if ((ww = find_window(trans)) != NULL)
5669 "fix this bug mcbride\n");
5674 /* set up the window layout */
5677 win->s = r->s; /* this never changes */
5678 if (trans && (ww = find_window(trans)))
5679 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5681 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5683 win->g.w = win->wa.width;
5684 win->g.h = win->wa.height;
5685 win->g.x = win->wa.x;
5686 win->g.y = win->wa.y;
5687 win->g_floatvalid = 0;
5688 win->floatmaxed = 0;
5689 win->ewmh_flags = 0;
5691 /* Set window properties so we can remember this after reincarnation */
5692 if (ws_idx_atom && prop == NULL &&
5693 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5695 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5697 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5698 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5703 ewmh_autoquirk(win);
5705 if (XGetClassHint(display, win->id, &win->ch)) {
5706 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5707 win->ch.res_class, win->ch.res_name);
5709 /* java is retarded so treat it special */
5710 if (strstr(win->ch.res_name, "sun-awt")) {
5715 for (i = 0; i < quirks_length; i++){
5716 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5717 !strcmp(win->ch.res_name, quirks[i].name)) {
5718 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5719 win->ch.res_class, win->ch.res_name);
5720 if (quirks[i].quirk & SWM_Q_FLOAT) {
5724 win->quirks = quirks[i].quirk;
5729 /* alter window position if quirky */
5730 if (win->quirks & SWM_Q_ANYWHERE) {
5731 win->manual = 1; /* don't center the quirky windows */
5732 bzero(&wc, sizeof wc);
5734 if (bar_enabled && win->g.y < bar_height) {
5735 win->g.y = wc.y = bar_height;
5738 if (win->g.w + win->g.x > WIDTH(r)) {
5739 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5745 /* Reset font sizes (the bruteforce way; no default keybinding). */
5746 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5747 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5748 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5749 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5750 fake_keypress(win, XK_KP_Add, ShiftMask);
5753 ewmh_get_win_state(win);
5754 ewmh_update_actions(win);
5755 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5759 bzero(&wc, sizeof wc);
5760 wc.border_width = border_width;
5761 mask = CWBorderWidth;
5762 XConfigureWindow(display, win->id, mask, &wc);
5765 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5766 PropertyChangeMask | StructureNotifyMask);
5768 /* floaters need to be mapped if they are in the current workspace */
5769 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5770 XMapRaised(display, win->id);
5776 free_window(struct ws_win *win)
5778 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5783 /* needed for restart wm */
5784 set_win_state(win, WithdrawnState);
5786 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5788 if (win->ch.res_class)
5789 XFree(win->ch.res_class);
5790 if (win->ch.res_name)
5791 XFree(win->ch.res_name);
5796 memset(win, 0xff, sizeof *win); /* XXX kill later */
5802 unmanage_window(struct ws_win *win)
5804 struct ws_win *parent;
5809 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5811 if (win->transient) {
5812 parent = find_window(win->transient);
5814 parent->child_trans = NULL;
5817 /* focus on root just in case */
5818 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5822 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5823 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5829 focus_magic(struct ws_win *win)
5831 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5836 if (win->child_trans) {
5837 /* win = parent & has a transient so focus on that */
5839 focus_win(win->child_trans);
5840 if (win->child_trans->take_focus)
5841 client_msg(win, takefocus);
5843 /* make sure transient hasn't dissapeared */
5844 if (validate_win(win->child_trans) == 0) {
5845 focus_win(win->child_trans);
5846 if (win->child_trans->take_focus)
5847 client_msg(win->child_trans, takefocus);
5849 win->child_trans = NULL;
5851 if (win->take_focus)
5852 client_msg(win, takefocus);
5858 if (win->take_focus)
5859 client_msg(win, takefocus);
5866 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5874 XKeyEvent *ev = &e->xkey;
5876 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5878 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5879 for (i = 0; i < keys_length; i++)
5880 if (keysym == keys[i].keysym
5881 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5882 && keyfuncs[keys[i].funcid].func) {
5883 if (keys[i].funcid == kf_spawn_custom)
5885 root_to_region(ev->root),
5886 &(keyfuncs[keys[i].funcid].args),
5890 keyfuncs[keys[i].funcid].func(
5891 root_to_region(ev->root),
5892 &(keyfuncs[keys[i].funcid].args)
5898 buttonpress(XEvent *e)
5902 XButtonPressedEvent *ev = &e->xbutton;
5904 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5906 if ((win = find_window(ev->window)) == NULL)
5910 action = client_click;
5912 for (i = 0; i < LENGTH(buttons); i++)
5913 if (action == buttons[i].action && buttons[i].func &&
5914 buttons[i].button == ev->button &&
5915 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5916 buttons[i].func(win, &buttons[i].args);
5920 configurerequest(XEvent *e)
5922 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5927 if ((win = find_window(ev->window)) == NULL)
5928 if ((win = find_unmanaged_window(ev->window)) == NULL)
5932 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5934 bzero(&wc, sizeof wc);
5937 wc.width = ev->width;
5938 wc.height = ev->height;
5939 wc.border_width = ev->border_width;
5940 wc.sibling = ev->above;
5941 wc.stack_mode = ev->detail;
5942 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5944 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5946 config_win(win, ev);
5951 configurenotify(XEvent *e)
5956 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5957 e->xconfigure.window);
5959 win = find_window(e->xconfigure.window);
5961 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5965 if (focus_mode == SWM_FOCUS_DEFAULT)
5966 drain_enter_notify();
5971 destroynotify(XEvent *e)
5974 XDestroyWindowEvent *ev = &e->xdestroywindow;
5976 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5978 if ((win = find_window(ev->window)) == NULL) {
5979 if ((win = find_unmanaged_window(ev->window)) == NULL)
5985 /* make sure we focus on something */
5988 unmanage_window(win);
5990 if (focus_mode == SWM_FOCUS_DEFAULT)
5991 drain_enter_notify();
5996 enternotify(XEvent *e)
5998 XCrossingEvent *ev = &e->xcrossing;
6003 Window focus_return;
6004 int revert_to_return;
6006 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
6007 "%lu subwindow %lu same_screen %d focus %d state %d\n",
6008 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
6009 ev->same_screen, ev->focus, ev->state);
6011 switch (focus_mode) {
6012 case SWM_FOCUS_DEFAULT:
6014 case SWM_FOCUS_FOLLOW:
6016 case SWM_FOCUS_SYNERGY:
6019 * all these checks need to be in this order because the
6020 * XCheckTypedWindowEvent relies on weeding out the previous events
6022 * making this code an option would enable a follow mouse for focus
6027 * state is set when we are switching workspaces and focus is set when
6028 * the window or a subwindow already has focus (occurs during restart).
6030 * Only honor the focus flag if last_focus_event is not FocusOut,
6031 * this allows scrotwm to continue to control focus when another
6032 * program is also playing with it.
6034 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6035 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6040 * happens when a window is created or destroyed and the border
6041 * crosses the mouse pointer and when switching ws
6043 * we need the subwindow test to see if we came from root in order
6044 * to give focus to floaters
6046 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6047 ev->subwindow == 0) {
6048 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6052 /* this window already has focus */
6053 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6054 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6058 /* this window is being deleted or moved to another ws */
6059 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6061 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6062 XPutBackEvent(display, &cne);
6066 if ((win = find_window(ev->window)) == NULL) {
6067 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6072 * In fullstack kill all enters unless they come from a different ws
6073 * (i.e. another region) or focus has been grabbed externally.
6075 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6076 last_focus_event != FocusOut) {
6077 XGetInputFocus(display, &focus_return, &revert_to_return);
6078 if ((w = find_window(focus_return)) == NULL ||
6080 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6088 if ((win = find_window(ev->window)) == NULL) {
6089 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6094 * if we have more enternotifies let them handle it in due time
6096 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6097 DNPRINTF(SWM_D_EVENT,
6098 "ignoring enternotify: got more enternotify\n");
6099 XPutBackEvent(display, &cne);
6106 /* lets us use one switch statement for arbitrary mode/detail combinations */
6107 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6110 focusevent(XEvent *e)
6114 u_int32_t mode_detail;
6115 XFocusChangeEvent *ev = &e->xfocus;
6117 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
6118 ev->type == FocusIn ? "entering" : "leaving",
6119 ev->window, ev->mode, ev->detail);
6121 if (last_focus_event == ev->type) {
6122 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6126 last_focus_event = ev->type;
6127 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6129 switch (mode_detail) {
6130 /* synergy client focus operations */
6131 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6132 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6134 /* synergy server focus operations */
6135 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6137 /* Entering applications like rdesktop that mangle the pointer */
6138 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6140 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6141 XSetWindowBorder(display, win->id,
6142 win->ws->r->s->c[ev->type == FocusIn ?
6143 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6146 fprintf(stderr, "ignoring focusevent\n");
6147 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6154 mapnotify(XEvent *e)
6157 XMapEvent *ev = &e->xmap;
6159 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
6161 win = manage_window(ev->window);
6163 set_win_state(win, NormalState);
6167 mappingnotify(XEvent *e)
6169 XMappingEvent *ev = &e->xmapping;
6171 XRefreshKeyboardMapping(ev);
6172 if (ev->request == MappingKeyboard)
6177 maprequest(XEvent *e)
6180 struct swm_region *r;
6181 XWindowAttributes wa;
6182 XMapRequestEvent *ev = &e->xmaprequest;
6184 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
6185 e->xmaprequest.window);
6187 if (!XGetWindowAttributes(display, ev->window, &wa))
6189 if (wa.override_redirect)
6192 win = manage_window(e->xmaprequest.window);
6194 return; /* can't happen */
6198 /* make new win focused */
6199 r = root_to_region(win->wa.root);
6200 if (win->ws == r->ws)
6205 propertynotify(XEvent *e)
6208 XPropertyEvent *ev = &e->xproperty;
6210 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
6213 win = find_window(ev->window);
6217 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6218 update_iconic(win, 0);
6219 XMapRaised(display, win->id);
6227 case XA_WM_NORMAL_HINTS:
6229 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6230 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6231 if (win->sh.flags & PMinSize) {
6232 win->g.w = win->sh.min_width;
6233 win->g.h = win->sh.min_height;
6234 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
6236 XMoveResizeWindow(display, win->id,
6237 win->g.x, win->g.y, win->g.w, win->g.h);
6240 if (title_name_enabled || title_class_enabled)
6244 if (window_name_enabled)
6253 unmapnotify(XEvent *e)
6257 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
6259 /* determine if we need to help unmanage this window */
6260 win = find_window(e->xunmap.window);
6264 if (getstate(e->xunmap.window) == NormalState) {
6265 unmanage_window(win);
6268 /* giant hack for apps that don't destroy transient windows */
6269 /* eat a bunch of events to prevent remanaging the window */
6271 while (XCheckWindowEvent(display, e->xunmap.window,
6272 EnterWindowMask, &cne))
6274 while (XCheckWindowEvent(display, e->xunmap.window,
6275 StructureNotifyMask, &cne))
6277 while (XCheckWindowEvent(display, e->xunmap.window,
6278 SubstructureNotifyMask, &cne))
6280 /* resend unmap because we ated it */
6281 XUnmapWindow(display, e->xunmap.window);
6284 if (focus_mode == SWM_FOCUS_DEFAULT)
6285 drain_enter_notify();
6289 visibilitynotify(XEvent *e)
6292 struct swm_region *r;
6294 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
6295 e->xvisibility.window);
6296 if (e->xvisibility.state == VisibilityUnobscured)
6297 for (i = 0; i < ScreenCount(display); i++)
6298 TAILQ_FOREACH(r, &screens[i].rl, entry)
6299 if (e->xvisibility.window == r->bar_window)
6304 clientmessage(XEvent *e)
6306 XClientMessageEvent *ev;
6311 win = find_window(ev->window);
6315 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
6316 ev->window, ev->message_type);
6318 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6319 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
6322 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6323 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
6324 if (win->can_delete)
6325 client_msg(win, adelete);
6327 XKillClient(display, win->id);
6329 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6330 DNPRINTF(SWM_D_EVENT,
6331 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
6332 if (win->floating) {
6333 if (ev->data.l[0] & (1<<8)) /* x */
6334 win->g.x = ev->data.l[1];
6335 if (ev->data.l[0] & (1<<9)) /* y */
6336 win->g.y = ev->data.l[2];
6337 if (ev->data.l[0] & (1<<10)) /* width */
6338 win->g.w = ev->data.l[3];
6339 if (ev->data.l[0] & (1<<11)) /* height */
6340 win->g.h = ev->data.l[4];
6343 /* TODO: Change stack sizes */
6345 config_win(win, NULL);
6347 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6348 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
6349 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6351 ewmh_update_win_state(win, ev->data.l[2],
6359 xerror_start(Display *d, XErrorEvent *ee)
6366 xerror(Display *d, XErrorEvent *ee)
6368 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6376 xerrorxlib = XSetErrorHandler(xerror_start);
6378 /* this causes an error if some other window manager is running */
6379 XSelectInput(display, DefaultRootWindow(display),
6380 SubstructureRedirectMask);
6381 XSync(display, False);
6385 XSetErrorHandler(xerror);
6386 XSync(display, False);
6391 new_region(struct swm_screen *s, int x, int y, int w, int h)
6393 struct swm_region *r, *n;
6394 struct workspace *ws = NULL;
6397 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6398 s->idx, w, h, x, y);
6400 /* remove any conflicting regions */
6401 n = TAILQ_FIRST(&s->rl);
6404 n = TAILQ_NEXT(r, entry);
6405 if (X(r) < (x + w) &&
6406 (X(r) + WIDTH(r)) > x &&
6408 (Y(r) + HEIGHT(r)) > y) {
6409 if (r->ws->r != NULL)
6410 r->ws->old_r = r->ws->r;
6412 XDestroyWindow(display, r->bar_window);
6413 TAILQ_REMOVE(&s->rl, r, entry);
6414 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6418 /* search old regions for one to reuse */
6420 /* size + location match */
6421 TAILQ_FOREACH(r, &s->orl, entry)
6422 if (X(r) == x && Y(r) == y &&
6423 HEIGHT(r) == h && WIDTH(r) == w)
6427 TAILQ_FOREACH(r, &s->orl, entry)
6428 if (HEIGHT(r) == h && WIDTH(r) == w)
6432 TAILQ_REMOVE(&s->orl, r, entry);
6433 /* try to use old region's workspace */
6434 if (r->ws->r == NULL)
6437 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6438 err(1, "calloc: failed to allocate memory for screen");
6440 /* if we don't have a workspace already, find one */
6442 for (i = 0; i < SWM_WS_MAX; i++)
6443 if (s->ws[i].r == NULL) {
6450 errx(1, "no free workspaces");
6461 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6467 #ifdef SWM_XRR_HAS_CRTC
6469 XRRScreenResources *sr;
6472 #endif /* SWM_XRR_HAS_CRTC */
6473 struct swm_region *r;
6476 if (i >= ScreenCount(display))
6477 errx(1, "invalid screen");
6479 /* remove any old regions */
6480 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6481 r->ws->old_r = r->ws->r = NULL;
6482 XDestroyWindow(display, r->bar_window);
6483 TAILQ_REMOVE(&screens[i].rl, r, entry);
6484 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6488 /* map virtual screens onto physical screens */
6489 #ifdef SWM_XRR_HAS_CRTC
6490 if (xrandr_support) {
6491 sr = XRRGetScreenResources(display, screens[i].root);
6493 new_region(&screens[i], 0, 0,
6494 DisplayWidth(display, i),
6495 DisplayHeight(display, i));
6499 for (c = 0, ci = NULL; c < ncrtc; c++) {
6500 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6501 if (ci->noutput == 0)
6504 if (ci != NULL && ci->mode == None)
6505 new_region(&screens[i], 0, 0,
6506 DisplayWidth(display, i),
6507 DisplayHeight(display, i));
6509 new_region(&screens[i],
6510 ci->x, ci->y, ci->width, ci->height);
6513 XRRFreeCrtcInfo(ci);
6514 XRRFreeScreenResources(sr);
6516 #endif /* SWM_XRR_HAS_CRTC */
6518 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6519 DisplayHeight(display, i));
6524 screenchange(XEvent *e) {
6525 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6526 struct swm_region *r;
6529 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6531 if (!XRRUpdateConfiguration(e))
6534 /* silly event doesn't include the screen index */
6535 for (i = 0; i < ScreenCount(display); i++)
6536 if (screens[i].root == xe->root)
6538 if (i >= ScreenCount(display))
6539 errx(1, "screenchange: screen not found");
6541 /* brute force for now, just re-enumerate the regions */
6544 /* add bars to all regions */
6545 for (i = 0; i < ScreenCount(display); i++)
6546 TAILQ_FOREACH(r, &screens[i].rl, entry)
6549 if (focus_mode == SWM_FOCUS_DEFAULT)
6550 drain_enter_notify();
6556 Window d1, d2, *wins = NULL;
6557 XWindowAttributes wa;
6562 for (i = 0; i < ScreenCount(display); i++) {
6563 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6566 /* attach windows to a region */
6567 /* normal windows */
6568 for (j = 0; j < no; j++) {
6569 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6570 wa.override_redirect ||
6571 XGetTransientForHint(display, wins[j], &d1))
6574 state = getstate(wins[j]);
6575 manage = state == IconicState;
6576 if (wa.map_state == IsViewable || manage)
6577 manage_window(wins[j]);
6579 /* transient windows */
6580 for (j = 0; j < no; j++) {
6581 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6582 wa.override_redirect)
6585 state = getstate(wins[j]);
6586 manage = state == IconicState;
6587 if (XGetTransientForHint(display, wins[j], &d1) &&
6589 manage_window(wins[j]);
6602 int errorbase, major, minor;
6603 struct workspace *ws;
6605 if ((screens = calloc(ScreenCount(display),
6606 sizeof(struct swm_screen))) == NULL)
6607 err(1, "calloc: screens");
6609 /* initial Xrandr setup */
6610 xrandr_support = XRRQueryExtension(display,
6611 &xrandr_eventbase, &errorbase);
6613 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6616 /* map physical screens */
6617 for (i = 0; i < ScreenCount(display); i++) {
6618 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6620 TAILQ_INIT(&screens[i].rl);
6621 TAILQ_INIT(&screens[i].orl);
6622 screens[i].root = RootWindow(display, i);
6624 /* set default colors */
6625 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6626 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6627 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6628 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6629 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6631 /* set default cursor */
6632 XDefineCursor(display, screens[i].root,
6633 XCreateFontCursor(display, XC_left_ptr));
6635 /* init all workspaces */
6636 /* XXX these should be dynamically allocated too */
6637 for (j = 0; j < SWM_WS_MAX; j++) {
6638 ws = &screens[i].ws[j];
6644 TAILQ_INIT(&ws->winlist);
6645 TAILQ_INIT(&ws->unmanagedlist);
6647 for (k = 0; layouts[k].l_stack != NULL; k++)
6648 if (layouts[k].l_config != NULL)
6649 layouts[k].l_config(ws,
6650 SWM_ARG_ID_STACKINIT);
6651 ws->cur_layout = &layouts[0];
6652 ws->cur_layout->l_string(ws);
6658 XRRSelectInput(display, screens[i].root,
6659 RRScreenChangeNotifyMask);
6666 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6668 err(1, "setup_globals: strdup");
6669 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6671 err(1, "setup_globals: strdup");
6672 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6674 err(1, "setup_globals: strdup");
6675 if ((spawn_term[0] = strdup("xterm")) == NULL)
6676 err(1, "setup_globals: strdup");
6677 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6685 Atom netwmcheck, netwmname, utf8_string;
6688 /* work around sun jdk bugs, code from wmname */
6689 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6690 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6691 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6692 for (i = 0; i < ScreenCount(display); i++) {
6693 root = screens[i].root;
6694 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6695 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6696 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6698 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6699 PropModeReplace, (unsigned char *)&win,1);
6700 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6701 PropModeReplace, (unsigned char *)&win,1);
6702 XChangeProperty(display, win, netwmname, utf8_string, 8,
6703 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6708 main(int argc, char *argv[])
6710 struct swm_region *r, *rr;
6711 struct ws_win *winfocus = NULL;
6714 char conf[PATH_MAX], *cfile = NULL;
6719 struct sigaction sact;
6722 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6723 SCROTWM_VERSION, buildstr);
6724 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6726 warnx("no locale support");
6728 if (!(display = XOpenDisplay(0)))
6729 errx(1, "can not open display");
6732 errx(1, "other wm running");
6734 /* handle some signals */
6735 bzero(&sact, sizeof(sact));
6736 sigemptyset(&sact.sa_mask);
6738 sact.sa_handler = sighdlr;
6739 sigaction(SIGINT, &sact, NULL);
6740 sigaction(SIGQUIT, &sact, NULL);
6741 sigaction(SIGTERM, &sact, NULL);
6742 sigaction(SIGHUP, &sact, NULL);
6744 sact.sa_handler = sighdlr;
6745 sact.sa_flags = SA_NOCLDSTOP;
6746 sigaction(SIGCHLD, &sact, NULL);
6748 astate = XInternAtom(display, "WM_STATE", False);
6749 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6750 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6751 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6752 a_wmname = XInternAtom(display, "WM_NAME", False);
6753 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6754 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6755 a_string = XInternAtom(display, "STRING", False);
6756 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6758 /* look for local and global conf file */
6759 pwd = getpwuid(getuid());
6761 errx(1, "invalid user %d", getuid());
6770 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6771 if (stat(conf, &sb) != -1) {
6772 if (S_ISREG(sb.st_mode))
6775 /* try global conf file */
6776 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6777 if (!stat(conf, &sb))
6778 if (S_ISREG(sb.st_mode))
6782 conf_load(cfile, SWM_CONF_DEFAULT);
6785 /* set some values to work around bad programs */
6787 /* grab existing windows (before we build the bars) */
6790 if (getenv("SWM_STARTED") == NULL)
6791 setenv("SWM_STARTED", "YES", 1);
6793 /* setup all bars */
6794 for (i = 0; i < ScreenCount(display); i++)
6795 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6796 if (winfocus == NULL)
6797 winfocus = TAILQ_FIRST(&r->ws->winlist);
6805 if (focus_mode == SWM_FOCUS_DEFAULT)
6806 drain_enter_notify();
6808 xfd = ConnectionNumber(display);
6810 while (XPending(display)) {
6811 XNextEvent(display, &e);
6814 if (e.type < LASTEvent) {
6816 if (handler[e.type])
6817 handler[e.type](&e);
6819 DNPRINTF(SWM_D_EVENT,
6820 "win: %lu unknown event: %d\n",
6821 e.xany.window, e.type);
6823 switch (e.type - xrandr_eventbase) {
6824 case RRScreenChangeNotify:
6828 DNPRINTF(SWM_D_EVENT,
6829 "win: %lu unknown xrandr event: "
6830 "%d\n", e.xany.window, e.type);
6836 /* if we are being restarted go focus on first window */
6838 rr = winfocus->ws->r;
6840 /* not a visible window */
6844 /* move pointer to first screen if multi screen */
6845 if (ScreenCount(display) > 1 || outputs > 1)
6846 XWarpPointer(display, None, rr->s[0].root,
6847 0, 0, 0, 0, rr->g.x,
6848 rr->g.y + (bar_enabled ? bar_height : 0));
6850 a.id = SWM_ARG_ID_FOCUSCUR;
6860 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6862 DNPRINTF(SWM_D_MISC, "select failed");
6863 if (restart_wm == 1)
6864 restart(NULL, NULL);
6865 if (search_resp == 1)
6877 XFreeGC(display, bar_gc);
6878 XCloseDisplay(display);