2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
85 #include <X11/extensions/XTest.h>
93 #ifdef SCROTWM_BUILDSTR
94 static const char *buildstr = SCROTWM_BUILDSTR;
96 static const char *buildstr = SCROTWM_VERSION;
100 # error XRandR versions less than 1.0 are not supported
105 #define SWM_XRR_HAS_CRTC
109 /*#define SWM_DEBUG*/
111 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
112 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
113 #define SWM_D_MISC 0x0001
114 #define SWM_D_EVENT 0x0002
115 #define SWM_D_WS 0x0004
116 #define SWM_D_FOCUS 0x0008
117 #define SWM_D_MOVE 0x0010
118 #define SWM_D_STACK 0x0020
119 #define SWM_D_MOUSE 0x0040
120 #define SWM_D_PROP 0x0080
121 #define SWM_D_CLASS 0x0100
122 #define SWM_D_KEY 0x0200
123 #define SWM_D_QUIRK 0x0400
124 #define SWM_D_SPAWN 0x0800
125 #define SWM_D_EVENTQ 0x1000
126 #define SWM_D_CONF 0x2000
128 u_int32_t swm_debug = 0
145 #define DPRINTF(x...)
146 #define DNPRINTF(n,x...)
149 #define LENGTH(x) (sizeof x / sizeof x[0])
150 #define MODKEY Mod1Mask
151 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
152 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
153 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
154 #define SWM_PROPLEN (16)
155 #define SWM_FUNCNAME_LEN (32)
156 #define SWM_KEYS_LEN (255)
157 #define SWM_QUIRK_LEN (64)
158 #define X(r) (r)->g.x
159 #define Y(r) (r)->g.y
160 #define WIDTH(r) (r)->g.w
161 #define HEIGHT(r) (r)->g.h
162 #define SWM_MAX_FONT_STEPS (3)
163 #define WINID(w) (w ? w->id : 0)
165 #define SWM_FOCUS_DEFAULT (0)
166 #define SWM_FOCUS_SYNERGY (1)
167 #define SWM_FOCUS_FOLLOW (2)
169 #define SWM_CONF_DEFAULT (0)
170 #define SWM_CONF_KEYMAPPING (1)
173 #define SWM_LIB "/usr/local/lib/libswmhack.so"
186 volatile sig_atomic_t running = 1;
187 volatile sig_atomic_t restart_wm = 0;
189 int last_focus_event = FocusOut;
190 int (*xerrorxlib)(Display *, XErrorEvent *);
194 int xrandr_eventbase;
195 unsigned int numlockmask = 0;
199 int cycle_visible = 0;
201 int font_adjusted = 0;
202 unsigned int mod_key = MODKEY;
205 struct swm_region *search_r;
206 int select_list_pipe[2];
207 int select_resp_pipe[2];
209 volatile sig_atomic_t search_resp;
210 int search_resp_action;
212 struct search_window {
213 TAILQ_ENTRY(search_window) entry;
218 TAILQ_HEAD(search_winlist, search_window);
220 struct search_winlist search_wl;
225 SWM_SEARCH_UNICONIFY,
226 SWM_SEARCH_NAME_WORKSPACE,
227 SWM_SEARCH_SEARCH_WORKSPACE,
228 SWM_SEARCH_SEARCH_WINDOW
232 double dialog_ratio = .6;
234 #define SWM_BAR_MAX (256)
235 #define SWM_BAR_JUSTIFY_LEFT (0)
236 #define SWM_BAR_JUSTIFY_CENTER (1)
237 #define SWM_BAR_JUSTIFY_RIGHT (2)
238 #define SWM_BAR_OFFSET (4)
239 char *bar_argv[] = { NULL, NULL };
241 char bar_ext[SWM_BAR_MAX];
242 char bar_vertext[SWM_BAR_MAX];
244 sig_atomic_t bar_alarm = 0;
247 int bar_border_width = 1;
248 int bar_at_bottom = 0;
250 int bar_extra_running = 0;
253 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
254 int stack_enabled = 1;
255 int clock_enabled = 1;
256 int urgent_enabled = 0;
257 char *clock_format = NULL;
258 int title_name_enabled = 0;
259 int title_class_enabled = 0;
260 int window_name_enabled = 0;
261 int focus_mode = SWM_FOCUS_DEFAULT;
262 int disable_border = 0;
263 int border_width = 1;
264 int verbose_layout = 0;
270 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
271 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
274 #define SWM_MENU_FN (2)
275 #define SWM_MENU_NB (4)
276 #define SWM_MENU_NF (6)
277 #define SWM_MENU_SB (8)
278 #define SWM_MENU_SF (10)
280 /* layout manager data */
281 struct swm_geometry {
291 /* virtual "screens" */
293 TAILQ_ENTRY(swm_region) entry;
294 struct swm_geometry g;
295 struct workspace *ws; /* current workspace on this region */
296 struct workspace *ws_prior; /* prior workspace on this region */
297 struct swm_screen *s; /* screen idx */
300 TAILQ_HEAD(swm_region_list, swm_region);
303 TAILQ_ENTRY(ws_win) entry;
306 struct ws_win *child_trans; /* transient child window */
307 struct swm_geometry g; /* current geometry */
308 struct swm_geometry g_float; /* geometry when floating */
309 struct swm_geometry rg_float; /* region geom when floating */
310 int g_floatvalid; /* flag: geometry in g_float is valid */
311 int floatmaxed; /* flag: floater was maxed in max_stack */
315 unsigned int ewmh_flags;
316 int font_size_boundary[SWM_MAX_FONT_STEPS];
322 unsigned long quirks;
323 struct workspace *ws; /* always valid */
324 struct swm_screen *s; /* always valid, never changes */
325 XWindowAttributes wa;
330 TAILQ_HEAD(ws_win_list, ws_win);
334 TAILQ_ENTRY(pid_e) entry;
338 TAILQ_HEAD(pid_list, pid_e);
339 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
341 /* layout handlers */
343 void vertical_config(struct workspace *, int);
344 void vertical_stack(struct workspace *, struct swm_geometry *);
345 void horizontal_config(struct workspace *, int);
346 void horizontal_stack(struct workspace *, struct swm_geometry *);
347 void max_stack(struct workspace *, struct swm_geometry *);
348 void plain_stacker(struct workspace *);
349 void fancy_stacker(struct workspace *);
351 struct ws_win *find_window(Window);
353 void grabbuttons(struct ws_win *, int);
354 void new_region(struct swm_screen *, int, int, int, int);
355 void unmanage_window(struct ws_win *);
356 long getstate(Window);
358 int conf_load(char *, int);
361 void (*l_stack)(struct workspace *, struct swm_geometry *);
362 void (*l_config)(struct workspace *, int);
364 #define SWM_L_FOCUSPREV (1<<0)
365 #define SWM_L_MAPONFOCUS (1<<1)
366 void (*l_string)(struct workspace *);
368 /* stack, configure */
369 { vertical_stack, vertical_config, 0, plain_stacker },
370 { horizontal_stack, horizontal_config, 0, plain_stacker },
372 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
373 { NULL, NULL, 0, NULL },
376 /* position of max_stack mode in the layouts array, index into layouts! */
377 #define SWM_V_STACK (0)
378 #define SWM_H_STACK (1)
379 #define SWM_MAX_STACK (2)
381 #define SWM_H_SLICE (32)
382 #define SWM_V_SLICE (32)
384 /* define work spaces */
386 int idx; /* workspace index */
387 char *name; /* workspace name */
388 int always_raise; /* raise windows on focus */
389 struct layout *cur_layout; /* current layout handlers */
390 struct ws_win *focus; /* may be NULL */
391 struct ws_win *focus_prev; /* may be NULL */
392 struct swm_region *r; /* may be NULL */
393 struct swm_region *old_r; /* may be NULL */
394 struct ws_win_list winlist; /* list of windows in ws */
395 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
396 char stacker[10]; /* display stacker and layout */
400 int horizontal_msize;
402 int horizontal_stacks;
409 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
410 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
412 /* physical screen mapping */
413 #define SWM_WS_MAX (10)
415 int idx; /* screen index */
416 struct swm_region_list rl; /* list of regions on this screen */
417 struct swm_region_list orl; /* list of old regions */
419 struct workspace ws[SWM_WS_MAX];
425 } c[SWM_S_COLOR_MAX];
427 struct swm_screen *screens;
430 /* args to functions */
433 #define SWM_ARG_ID_FOCUSNEXT (0)
434 #define SWM_ARG_ID_FOCUSPREV (1)
435 #define SWM_ARG_ID_FOCUSMAIN (2)
436 #define SWM_ARG_ID_FOCUSCUR (4)
437 #define SWM_ARG_ID_SWAPNEXT (10)
438 #define SWM_ARG_ID_SWAPPREV (11)
439 #define SWM_ARG_ID_SWAPMAIN (12)
440 #define SWM_ARG_ID_MOVELAST (13)
441 #define SWM_ARG_ID_MASTERSHRINK (20)
442 #define SWM_ARG_ID_MASTERGROW (21)
443 #define SWM_ARG_ID_MASTERADD (22)
444 #define SWM_ARG_ID_MASTERDEL (23)
445 #define SWM_ARG_ID_STACKRESET (30)
446 #define SWM_ARG_ID_STACKINIT (31)
447 #define SWM_ARG_ID_CYCLEWS_UP (40)
448 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
449 #define SWM_ARG_ID_CYCLESC_UP (42)
450 #define SWM_ARG_ID_CYCLESC_DOWN (43)
451 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
452 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
453 #define SWM_ARG_ID_STACKINC (50)
454 #define SWM_ARG_ID_STACKDEC (51)
455 #define SWM_ARG_ID_SS_ALL (60)
456 #define SWM_ARG_ID_SS_WINDOW (61)
457 #define SWM_ARG_ID_DONTCENTER (70)
458 #define SWM_ARG_ID_CENTER (71)
459 #define SWM_ARG_ID_KILLWINDOW (80)
460 #define SWM_ARG_ID_DELETEWINDOW (81)
461 #define SWM_ARG_ID_WIDTHGROW (90)
462 #define SWM_ARG_ID_WIDTHSHRINK (91)
463 #define SWM_ARG_ID_HEIGHTGROW (92)
464 #define SWM_ARG_ID_HEIGHTSHRINK (93)
465 #define SWM_ARG_ID_MOVEUP (100)
466 #define SWM_ARG_ID_MOVEDOWN (101)
467 #define SWM_ARG_ID_MOVELEFT (102)
468 #define SWM_ARG_ID_MOVERIGHT (103)
472 void focus(struct swm_region *, union arg *);
473 void focus_magic(struct ws_win *);
480 #define SWM_Q_FLOAT (1<<0) /* float this window */
481 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
482 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
483 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
484 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
485 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
487 int quirks_size = 0, quirks_length = 0;
488 struct quirk *quirks = NULL;
491 * Supported EWMH hints should be added to
492 * both the enum and the ewmh array
494 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
495 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
496 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
497 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
498 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
499 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
500 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
501 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
502 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
503 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
509 } ewmh[SWM_EWMH_HINT_MAX] = {
510 /* must be in same order as in the enum */
511 {"_NET_ACTIVE_WINDOW", None},
512 {"_NET_MOVERESIZE_WINDOW", None},
513 {"_NET_CLOSE_WINDOW", None},
514 {"_NET_WM_WINDOW_TYPE", None},
515 {"_NET_WM_WINDOW_TYPE_DOCK", None},
516 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
517 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
518 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
519 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
520 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
521 {"_NET_WM_STATE", None},
522 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
523 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
524 {"_NET_WM_STATE_SKIP_TASKBAR", None},
525 {"_NET_WM_STATE_SKIP_PAGER", None},
526 {"_NET_WM_STATE_HIDDEN", None},
527 {"_NET_WM_STATE_ABOVE", None},
528 {"_SWM_WM_STATE_MANUAL", None},
529 {"_NET_WM_STATE_FULLSCREEN", None},
530 {"_NET_WM_ALLOWED_ACTIONS", None},
531 {"_NET_WM_ACTION_MOVE", None},
532 {"_NET_WM_ACTION_RESIZE", None},
533 {"_NET_WM_ACTION_FULLSCREEN", None},
534 {"_NET_WM_ACTION_CLOSE", None},
537 void store_float_geom(struct ws_win *win, struct swm_region *r);
538 int floating_toggle_win(struct ws_win *win);
539 void spawn_select(struct swm_region *, union arg *, char *, int *);
542 get_property(Window id, Atom atom, long count, Atom type,
543 unsigned long *n, unsigned char **data)
546 unsigned long tmp, extra;
547 unsigned long *nitems;
550 nitems = n != NULL ? n : &tmp;
551 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
552 &real, &format, nitems, &extra, data);
554 if (status != Success)
563 update_iconic(struct ws_win *win, int newv)
570 iprop = XInternAtom(display, "_SWM_ICONIC", False);
574 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
575 PropModeReplace, (unsigned char *)&v, 1);
577 XDeleteProperty(display, win->id, iprop);
581 get_iconic(struct ws_win *win)
586 unsigned long nitems, extra;
587 unsigned char *prop = NULL;
589 iprop = XInternAtom(display, "_SWM_ICONIC", False);
592 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
593 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
594 if (status != Success)
596 if (rettype != XA_INTEGER || retfmt != 32)
600 v = *((int32_t *)prop);
614 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
616 for (i = 0; i < LENGTH(ewmh); i++)
617 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
619 for (i = 0; i < ScreenCount(display); i++) {
620 /* Support check window will be created by workaround(). */
622 /* Report supported atoms */
623 XDeleteProperty(display, screens[i].root, sup_list);
624 for (j = 0; j < LENGTH(ewmh); j++)
625 XChangeProperty(display, screens[i].root,
626 sup_list, XA_ATOM, 32,
627 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
635 unsigned char *data = NULL;
637 Atom sup_check, sup_list;
640 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
641 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
643 for (i = 0; i < ScreenCount(display); i++) {
644 /* Get the support check window and destroy it */
645 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
650 XDestroyWindow(display, id);
651 XDeleteProperty(display, screens[i].root, sup_check);
652 XDeleteProperty(display, screens[i].root, sup_list);
660 ewmh_autoquirk(struct ws_win *win)
663 unsigned long *data = NULL, n;
666 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
667 XA_ATOM, &n, (void *)&data);
674 for (i = 0; i < n; i++) {
676 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
678 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
679 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
680 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
682 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
685 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
686 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
688 win->quirks = SWM_Q_FLOAT;
696 #define SWM_EWMH_ACTION_COUNT_MAX (6)
697 #define EWMH_F_FULLSCREEN (1<<0)
698 #define EWMH_F_ABOVE (1<<1)
699 #define EWMH_F_HIDDEN (1<<2)
700 #define EWMH_F_SKIP_PAGER (1<<3)
701 #define EWMH_F_SKIP_TASKBAR (1<<4)
702 #define SWM_F_MANUAL (1<<5)
705 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
707 struct swm_geometry rg;
715 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
721 store_float_geom(win, win->ws->r);
728 if (win->g_floatvalid) {
729 /* refloat at last floating relative position */
730 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
731 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
732 win->g.w = win->g_float.w;
733 win->g.h = win->g_float.h;
741 ewmh_update_actions(struct ws_win *win)
743 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
749 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
752 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
753 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
756 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
757 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
760 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
761 #define _NET_WM_STATE_ADD 1 /* add/set property */
762 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
765 ewmh_update_win_state(struct ws_win *win, long state, long action)
767 unsigned int mask = 0;
768 unsigned int changed = 0;
769 unsigned int orig_flags;
774 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
775 mask = EWMH_F_FULLSCREEN;
776 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
778 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
780 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
781 mask = EWMH_F_SKIP_PAGER;
782 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
783 mask = EWMH_F_SKIP_TASKBAR;
786 orig_flags = win->ewmh_flags;
789 case _NET_WM_STATE_REMOVE:
790 win->ewmh_flags &= ~mask;
792 case _NET_WM_STATE_ADD:
793 win->ewmh_flags |= mask;
795 case _NET_WM_STATE_TOGGLE:
796 win->ewmh_flags ^= mask;
800 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
802 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
804 if (!floating_toggle_win(win))
805 win->ewmh_flags = orig_flags; /* revert */
806 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
808 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
809 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
811 if (!ewmh_set_win_fullscreen(win,
812 win->ewmh_flags & EWMH_F_FULLSCREEN))
813 win->ewmh_flags = orig_flags; /* revert */
815 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
817 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
818 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
819 XA_ATOM, 32, PropModeAppend,
820 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
821 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
822 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
823 XA_ATOM, 32, PropModeAppend,
824 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
825 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
826 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
827 XA_ATOM, 32, PropModeAppend,
828 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
829 if (win->ewmh_flags & EWMH_F_ABOVE)
830 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
831 XA_ATOM, 32, PropModeAppend,
832 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
833 if (win->ewmh_flags & SWM_F_MANUAL)
834 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
835 XA_ATOM, 32, PropModeAppend,
836 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
840 ewmh_get_win_state(struct ws_win *win)
851 win->ewmh_flags |= EWMH_F_ABOVE;
853 win->ewmh_flags |= SWM_F_MANUAL;
855 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
856 (~0L), XA_ATOM, &n, (void *)&states);
861 for (i = 0; i < n; i++)
862 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
882 name = "ButtonPress";
885 name = "ButtonRelease";
888 name = "MotionNotify";
891 name = "EnterNotify";
894 name = "LeaveNotify";
903 name = "KeymapNotify";
909 name = "GraphicsExpose";
914 case VisibilityNotify:
915 name = "VisibilityNotify";
918 name = "CreateNotify";
921 name = "DestroyNotify";
924 name = "UnmapNotify";
933 name = "ReparentNotify";
935 case ConfigureNotify:
936 name = "ConfigureNotify";
938 case ConfigureRequest:
939 name = "ConfigureRequest";
942 name = "GravityNotify";
945 name = "ResizeRequest";
947 case CirculateNotify:
948 name = "CirculateNotify";
950 case CirculateRequest:
951 name = "CirculateRequest";
954 name = "PropertyNotify";
957 name = "SelectionClear";
959 case SelectionRequest:
960 name = "SelectionRequest";
962 case SelectionNotify:
963 name = "SelectionNotify";
966 name = "ColormapNotify";
969 name = "ClientMessage";
972 name = "MappingNotify";
977 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
979 e->xany.window, name, e->type, QLength(display));
981 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
983 e->xany.window, e->type, QLength(display));
987 dumpwins(struct swm_region *r, union arg *args)
991 XWindowAttributes wa;
994 fprintf(stderr, "invalid workspace\n");
998 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
1000 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1001 state = getstate(win->id);
1002 if (!XGetWindowAttributes(display, win->id, &wa))
1003 fprintf(stderr, "window: %lu failed "
1004 "XGetWindowAttributes\n", win->id);
1005 fprintf(stderr, "window: %lu map_state: %d state: %d "
1007 win->id, wa.map_state, state, win->transient);
1010 fprintf(stderr, "===== unmanaged window list =====\n");
1011 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1012 state = getstate(win->id);
1013 if (!XGetWindowAttributes(display, win->id, &wa))
1014 fprintf(stderr, "window: %lu failed "
1015 "XGetWindowAttributes\n", win->id);
1016 fprintf(stderr, "window: %lu map_state: %d state: %d "
1018 win->id, wa.map_state, state, win->transient);
1021 fprintf(stderr, "=================================\n");
1024 #define dumpevent(e)
1026 dumpwins(struct swm_region *r, union arg *args)
1029 #endif /* SWM_DEBUG */
1031 void expose(XEvent *);
1032 void keypress(XEvent *);
1033 void buttonpress(XEvent *);
1034 void configurerequest(XEvent *);
1035 void configurenotify(XEvent *);
1036 void destroynotify(XEvent *);
1037 void enternotify(XEvent *);
1038 void focusevent(XEvent *);
1039 void mapnotify(XEvent *);
1040 void mappingnotify(XEvent *);
1041 void maprequest(XEvent *);
1042 void propertynotify(XEvent *);
1043 void unmapnotify(XEvent *);
1044 void visibilitynotify(XEvent *);
1045 void clientmessage(XEvent *);
1047 void (*handler[LASTEvent])(XEvent *) = {
1049 [KeyPress] = keypress,
1050 [ButtonPress] = buttonpress,
1051 [ConfigureRequest] = configurerequest,
1052 [ConfigureNotify] = configurenotify,
1053 [DestroyNotify] = destroynotify,
1054 [EnterNotify] = enternotify,
1055 [FocusIn] = focusevent,
1056 [FocusOut] = focusevent,
1057 [MapNotify] = mapnotify,
1058 [MappingNotify] = mappingnotify,
1059 [MapRequest] = maprequest,
1060 [PropertyNotify] = propertynotify,
1061 [UnmapNotify] = unmapnotify,
1062 [VisibilityNotify] = visibilitynotify,
1063 [ClientMessage] = clientmessage,
1069 int saved_errno, status;
1072 saved_errno = errno;
1076 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1081 if (errno != ECHILD)
1082 warn("sighdlr: waitpid");
1083 #endif /* SWM_DEBUG */
1086 if (pid == searchpid)
1090 if (WIFEXITED(status)) {
1091 if (WEXITSTATUS(status) != 0)
1092 warnx("sighdlr: child exit status: %d",
1093 WEXITSTATUS(status));
1095 warnx("sighdlr: child is terminated "
1097 #endif /* SWM_DEBUG */
1111 errno = saved_errno;
1117 struct pid_e *p = NULL;
1119 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1124 TAILQ_FOREACH(p, &pidlist, entry) {
1133 name_to_color(char *colorname)
1137 XColor screen_def, exact_def;
1138 unsigned long result = 0;
1139 char cname[32] = "#";
1141 cmap = DefaultColormap(display, screens[0].idx);
1142 status = XAllocNamedColor(display, cmap, colorname,
1143 &screen_def, &exact_def);
1145 strlcat(cname, colorname + 2, sizeof cname - 1);
1146 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1150 result = screen_def.pixel;
1152 fprintf(stderr, "color '%s' not found.\n", colorname);
1158 setscreencolor(char *val, int i, int c)
1160 if (i > 0 && i <= ScreenCount(display)) {
1161 screens[i - 1].c[c].color = name_to_color(val);
1162 free(screens[i - 1].c[c].name);
1163 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1165 } else if (i == -1) {
1166 for (i = 0; i < ScreenCount(display); i++) {
1167 screens[i].c[c].color = name_to_color(val);
1168 free(screens[i].c[c].name);
1169 if ((screens[i].c[c].name = strdup(val)) == NULL)
1173 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1174 i, ScreenCount(display));
1178 fancy_stacker(struct workspace *ws)
1180 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1181 if (ws->cur_layout->l_stack == vertical_stack)
1182 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1183 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1184 if (ws->cur_layout->l_stack == horizontal_stack)
1185 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1186 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1190 plain_stacker(struct workspace *ws)
1192 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1193 if (ws->cur_layout->l_stack == vertical_stack)
1194 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1195 if (ws->cur_layout->l_stack == horizontal_stack)
1196 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1200 custom_region(char *val)
1202 unsigned int sidx, x, y, w, h;
1204 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1205 errx(1, "invalid custom region, "
1206 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1207 if (sidx < 1 || sidx > ScreenCount(display))
1208 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1209 sidx, ScreenCount(display));
1213 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1215 if (x > DisplayWidth(display, sidx) ||
1216 y > DisplayHeight(display, sidx) ||
1217 w + x > DisplayWidth(display, sidx) ||
1218 h + y > DisplayHeight(display, sidx)) {
1219 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1220 "- not within screen boundaries "
1221 "(%ux%u)\n", w, h, x, y,
1222 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1226 new_region(&screens[sidx], x, y, w, h);
1230 socket_setnonblock(int fd)
1234 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1235 err(1, "fcntl F_GETFL");
1236 flags |= O_NONBLOCK;
1237 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1238 err(1, "fcntl F_SETFL");
1242 bar_print(struct swm_region *r, char *s)
1244 int textwidth, x = 0;
1247 XClearWindow(display, r->bar_window);
1248 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1251 textwidth = XTextWidth(bar_fs, s, len);
1253 switch (bar_justify) {
1254 case SWM_BAR_JUSTIFY_LEFT:
1257 case SWM_BAR_JUSTIFY_CENTER:
1258 x = (WIDTH(r) - textwidth) / 2;
1260 case SWM_BAR_JUSTIFY_RIGHT:
1261 x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
1265 if (x < SWM_BAR_OFFSET)
1268 XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
1272 bar_extra_stop(void)
1276 bzero(bar_pipe, sizeof bar_pipe);
1279 kill(bar_pid, SIGTERM);
1282 strlcpy(bar_ext, "", sizeof bar_ext);
1287 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1289 int do_class, do_name;
1291 XClassHint *xch = NULL;
1293 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1294 cur_focus != NULL) {
1295 if ((xch = XAllocClassHint()) == NULL)
1297 status = XGetClassHint(display, cur_focus->id, xch);
1298 if (status == BadWindow || status == BadAlloc)
1300 do_class = (title_class_enabled && xch->res_class != NULL);
1301 do_name = (title_name_enabled && xch->res_name != NULL);
1303 strlcat(s, xch->res_class, sz);
1304 if (do_class && do_name)
1305 strlcat(s, ":", sz);
1307 strlcat(s, xch->res_name, sz);
1308 strlcat(s, " ", sz);
1316 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1320 if (window_name_enabled && cur_focus != NULL) {
1321 XFetchName(display, cur_focus->id, &title);
1323 if (cur_focus->floating)
1324 strlcat(s, "(f) ", sz);
1325 strlcat(s, title, sz);
1326 strlcat(s, " ", sz);
1332 int urgent[SWM_WS_MAX];
1334 bar_urgent(char *s, ssize_t sz)
1336 XWMHints *wmh = NULL;
1341 if (urgent_enabled == 0)
1344 for (i = 0; i < SWM_WS_MAX; i++)
1347 for (i = 0; i < ScreenCount(display); i++)
1348 for (j = 0; j < SWM_WS_MAX; j++)
1349 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1350 wmh = XGetWMHints(display, win->id);
1354 if (wmh->flags & XUrgencyHint)
1359 strlcat(s, "* ", sz);
1360 for (i = 0; i < SWM_WS_MAX; i++) {
1362 snprintf(b, sizeof b, "%d ", i + 1);
1364 snprintf(b, sizeof b, "- ");
1367 strlcat(s, "* ", sz);
1375 struct swm_region *r;
1378 char ws[SWM_BAR_MAX];
1379 char s[SWM_BAR_MAX];
1380 char cn[SWM_BAR_MAX];
1381 char loc[SWM_BAR_MAX];
1382 char *b, *stack = "";
1384 if (bar_enabled == 0)
1386 if (bar_extra && bar_extra_running) {
1387 /* ignore short reads; it'll correct itself */
1388 while ((b = fgetln(stdin, &len)) != NULL)
1389 if (b && b[len - 1] == '\n') {
1391 strlcpy(bar_ext, b, sizeof bar_ext);
1393 if (b == NULL && errno != EAGAIN) {
1394 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1395 errno, strerror(errno));
1399 strlcpy(bar_ext, "", sizeof bar_ext);
1401 if (clock_enabled == 0)
1402 strlcpy(s, "", sizeof s);
1405 localtime_r(&tmt, &tm);
1406 strftime(s, sizeof s, clock_format, &tm);
1407 strlcat(s, " ", sizeof s);
1410 for (i = 0; i < ScreenCount(display); i++) {
1412 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1413 strlcpy(cn, "", sizeof cn);
1414 strlcpy(ws, "", sizeof ws);
1416 bar_urgent(cn, sizeof cn);
1417 bar_class_name(cn, sizeof cn, r->ws->focus);
1418 bar_window_name(cn, sizeof cn, r->ws->focus);
1420 snprintf(ws, sizeof ws, "<%s>", r->ws->name);
1423 stack = r->ws->stacker;
1425 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s %s",
1426 x++, r->ws->idx + 1, stack, ws, s, cn, bar_ext,
1441 bar_toggle(struct swm_region *r, union arg *args)
1443 struct swm_region *tmpr;
1444 int i, sc = ScreenCount(display);
1446 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1449 for (i = 0; i < sc; i++)
1450 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1451 XUnmapWindow(display, tmpr->bar_window);
1453 for (i = 0; i < sc; i++)
1454 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1455 XMapRaised(display, tmpr->bar_window);
1457 bar_enabled = !bar_enabled;
1460 /* must be after stack */
1467 XSetWindowAttributes wa;
1468 struct swm_region *r;
1471 /* do this here because the conf file is in memory */
1472 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1473 /* launch external status app */
1474 bar_extra_running = 1;
1475 if (pipe(bar_pipe) == -1)
1476 err(1, "pipe error");
1477 socket_setnonblock(bar_pipe[0]);
1478 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1479 if (dup2(bar_pipe[0], 0) == -1)
1481 if (dup2(bar_pipe[1], 1) == -1)
1483 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1484 err(1, "could not disable SIGPIPE");
1485 switch (bar_pid = fork()) {
1487 err(1, "cannot fork");
1491 execvp(bar_argv[0], bar_argv);
1492 err(1, "%s external app failed", bar_argv[0]);
1494 default: /* parent */
1500 bzero(&wa, sizeof wa);
1501 for (i = 0; i < ScreenCount(display); i++)
1502 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1504 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1505 wa.background_pixel =
1506 screens[i].c[SWM_S_COLOR_BAR].color;
1507 XChangeWindowAttributes(display, r->bar_window,
1508 CWBackPixel | CWBorderPixel, &wa);
1514 bar_setup(struct swm_region *r)
1519 XFreeFont(display, bar_fs);
1523 for (i = 0; bar_fonts[i] != NULL; i++) {
1524 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1530 if (bar_fonts[i] == NULL)
1531 errx(1, "couldn't load font");
1533 errx(1, "couldn't create font structure");
1535 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1536 2 * bar_border_width;
1538 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1540 r->bar_window = XCreateSimpleWindow(display,
1541 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1542 bar_height - 2 * bar_border_width,
1543 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1544 r->s->c[SWM_S_COLOR_BAR].color);
1545 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1546 XSetFont(display, bar_gc, bar_fs->fid);
1547 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1549 XMapRaised(display, r->bar_window);
1550 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1552 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1553 err(1, "could not install bar_signal");
1558 drain_enter_notify(void)
1563 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1566 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1570 set_win_state(struct ws_win *win, long state)
1572 long data[] = {state, None};
1574 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1579 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1580 (unsigned char *)data, 2);
1587 unsigned char *p = NULL;
1590 if (!get_property(w, astate, 2L, astate, &n, &p))
1593 result = *((long *)p);
1599 version(struct swm_region *r, union arg *args)
1601 bar_version = !bar_version;
1603 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s Build: %s",
1604 SCROTWM_VERSION, buildstr);
1606 strlcpy(bar_vertext, "", sizeof bar_vertext);
1611 client_msg(struct ws_win *win, Atom a)
1613 XClientMessageEvent cm;
1618 bzero(&cm, sizeof cm);
1619 cm.type = ClientMessage;
1620 cm.window = win->id;
1621 cm.message_type = aprot;
1624 cm.data.l[1] = CurrentTime;
1625 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1629 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1637 DNPRINTF(SWM_D_MISC,
1638 "config_win: win %lu x %d y %d w %d h %d\n",
1639 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1641 ce.type = ConfigureNotify;
1642 ce.display = display;
1644 ce.window = win->id;
1647 ce.width = win->g.w;
1648 ce.height = win->g.h;
1649 ce.border_width = border_width;
1651 ce.override_redirect = False;
1653 DNPRINTF(SWM_D_MISC,
1654 "config_win: ev win %lu x %d y %d w %d h %d\n",
1655 ev->window, ev->x, ev->y, ev->width, ev->height);
1656 ce.type = ConfigureNotify;
1657 ce.display = ev->display;
1658 ce.event = ev->window;
1659 ce.window = ev->window;
1662 ce.width = ev->width;
1663 ce.height = ev->height;
1664 ce.border_width = ev->border_width;
1665 ce.above = ev->above;
1666 ce.override_redirect = False;
1669 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1673 count_win(struct workspace *ws, int count_transient)
1678 TAILQ_FOREACH(win, &ws->winlist, entry) {
1679 if (count_transient == 0 && win->floating)
1681 if (count_transient == 0 && win->transient)
1687 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1693 quit(struct swm_region *r, union arg *args)
1695 DNPRINTF(SWM_D_MISC, "quit\n");
1700 unmap_window(struct ws_win *win)
1705 /* don't unmap again */
1706 if (getstate(win->id) == IconicState)
1709 set_win_state(win, IconicState);
1711 XUnmapWindow(display, win->id);
1712 XSetWindowBorder(display, win->id,
1713 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1722 for (i = 0; i < ScreenCount(display); i++)
1723 for (j = 0; j < SWM_WS_MAX; j++)
1724 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1729 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1736 event.display = display; /* Ignored, but what the hell */
1737 event.window = win->id;
1738 event.root = win->s->root;
1739 event.subwindow = None;
1740 event.time = CurrentTime;
1745 event.same_screen = True;
1746 event.keycode = XKeysymToKeycode(display, keysym);
1747 event.state = modifiers;
1749 event.type = KeyPress;
1750 XSendEvent(event.display, event.window, True,
1751 KeyPressMask, (XEvent *)&event);
1753 event.type = KeyRelease;
1754 XSendEvent(event.display, event.window, True,
1755 KeyPressMask, (XEvent *)&event);
1760 restart(struct swm_region *r, union arg *args)
1762 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1764 /* disable alarm because the following code may not be interrupted */
1766 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1767 err(1, "can't disable alarm");
1772 XCloseDisplay(display);
1773 execvp(start_argv[0], start_argv);
1774 fprintf(stderr, "execvp failed\n");
1780 root_to_region(Window root)
1782 struct swm_region *r = NULL;
1784 int i, x, y, wx, wy;
1787 for (i = 0; i < ScreenCount(display); i++)
1788 if (screens[i].root == root)
1791 if (XQueryPointer(display, screens[i].root,
1792 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1793 /* choose a region based on pointer location */
1794 TAILQ_FOREACH(r, &screens[i].rl, entry)
1795 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1796 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1801 r = TAILQ_FIRST(&screens[i].rl);
1807 find_unmanaged_window(Window id)
1812 for (i = 0; i < ScreenCount(display); i++)
1813 for (j = 0; j < SWM_WS_MAX; j++)
1814 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1822 find_window(Window id)
1825 Window wrr, wpr, *wcr = NULL;
1829 for (i = 0; i < ScreenCount(display); i++)
1830 for (j = 0; j < SWM_WS_MAX; j++)
1831 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1835 /* if we were looking for the parent return that window instead */
1836 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1841 /* ignore not found and root */
1842 if (wpr == 0 || wrr == wpr)
1845 /* look for parent */
1846 for (i = 0; i < ScreenCount(display); i++)
1847 for (j = 0; j < SWM_WS_MAX; j++)
1848 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1856 spawn(int ws_idx, union arg *args, int close_fd)
1861 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1864 close(ConnectionNumber(display));
1866 setenv("LD_PRELOAD", SWM_LIB, 1);
1868 if (asprintf(&ret, "%d", ws_idx) == -1) {
1872 setenv("_SWM_WS", ret, 1);
1876 if (asprintf(&ret, "%d", getpid()) == -1) {
1880 setenv("_SWM_PID", ret, 1);
1884 if (setsid() == -1) {
1891 * close stdin and stdout to prevent interaction between apps
1892 * and the baraction script
1893 * leave stderr open to record errors
1895 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1899 dup2(fd, STDIN_FILENO);
1900 dup2(fd, STDOUT_FILENO);
1905 execvp(args->argv[0], args->argv);
1912 spawnterm(struct swm_region *r, union arg *args)
1914 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1918 setenv("_SWM_XTERM_FONTADJ", "", 1);
1919 spawn(r->ws->idx, args, 1);
1924 kill_refs(struct ws_win *win)
1927 struct swm_region *r;
1928 struct workspace *ws;
1933 for (i = 0; i < ScreenCount(display); i++)
1934 TAILQ_FOREACH(r, &screens[i].rl, entry)
1935 for (x = 0; x < SWM_WS_MAX; x++) {
1937 if (win == ws->focus)
1939 if (win == ws->focus_prev)
1940 ws->focus_prev = NULL;
1945 validate_win(struct ws_win *testwin)
1948 struct workspace *ws;
1949 struct swm_region *r;
1952 if (testwin == NULL)
1955 for (i = 0; i < ScreenCount(display); i++)
1956 TAILQ_FOREACH(r, &screens[i].rl, entry)
1957 for (x = 0; x < SWM_WS_MAX; x++) {
1959 TAILQ_FOREACH(win, &ws->winlist, entry)
1967 validate_ws(struct workspace *testws)
1969 struct swm_region *r;
1970 struct workspace *ws;
1973 /* validate all ws */
1974 for (i = 0; i < ScreenCount(display); i++)
1975 TAILQ_FOREACH(r, &screens[i].rl, entry)
1976 for (x = 0; x < SWM_WS_MAX; x++) {
1985 unfocus_win(struct ws_win *win)
1990 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1994 if (win->ws == NULL)
1997 if (validate_ws(win->ws))
1998 return; /* XXX this gets hit with thunderbird, needs fixing */
2000 if (win->ws->r == NULL)
2003 if (validate_win(win)) {
2008 if (win->ws->focus == win) {
2009 win->ws->focus = NULL;
2010 win->ws->focus_prev = win;
2013 if (validate_win(win->ws->focus)) {
2014 kill_refs(win->ws->focus);
2015 win->ws->focus = NULL;
2017 if (validate_win(win->ws->focus_prev)) {
2018 kill_refs(win->ws->focus_prev);
2019 win->ws->focus_prev = NULL;
2022 /* drain all previous unfocus events */
2023 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2026 grabbuttons(win, 0);
2027 XSetWindowBorder(display, win->id,
2028 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2030 XChangeProperty(display, win->s->root,
2031 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2032 PropModeReplace, (unsigned char *)&none,1);
2041 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2043 for (i = 0; i < ScreenCount(display); i++)
2044 for (j = 0; j < SWM_WS_MAX; j++)
2045 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2050 focus_win(struct ws_win *win)
2055 struct ws_win *cfw = NULL;
2058 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
2062 if (win->ws == NULL)
2065 if (validate_ws(win->ws))
2066 return; /* XXX this gets hit with thunderbird, needs fixing */
2068 if (validate_win(win)) {
2073 if (validate_win(win)) {
2078 XGetInputFocus(display, &cur_focus, &rr);
2079 if ((cfw = find_window(cur_focus)) != NULL)
2082 /* use larger hammer since the window was killed somehow */
2083 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2084 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2085 XSetWindowBorder(display, cfw->id,
2086 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2089 win->ws->focus = win;
2091 if (win->ws->r != NULL) {
2092 /* drain all previous focus events */
2093 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2097 XSetInputFocus(display, win->id,
2098 RevertToParent, CurrentTime);
2099 grabbuttons(win, 1);
2100 XSetWindowBorder(display, win->id,
2101 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2102 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2103 win->ws->always_raise)
2104 XMapRaised(display, win->id);
2106 XChangeProperty(display, win->s->root,
2107 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2108 PropModeReplace, (unsigned char *)&win->id,1);
2111 if (window_name_enabled)
2116 switchws(struct swm_region *r, union arg *args)
2118 int wsid = args->id, unmap_old = 0;
2119 struct swm_region *this_r, *other_r;
2121 struct workspace *new_ws, *old_ws;
2128 old_ws = this_r->ws;
2129 new_ws = &this_r->s->ws[wsid];
2131 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2132 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2135 if (new_ws == NULL || old_ws == NULL)
2137 if (new_ws == old_ws)
2140 other_r = new_ws->r;
2141 if (other_r == NULL) {
2142 /* the other workspace is hidden, hide this one */
2146 /* the other ws is visible in another region, exchange them */
2147 other_r->ws_prior = new_ws;
2148 other_r->ws = old_ws;
2149 old_ws->r = other_r;
2151 this_r->ws_prior = old_ws;
2152 this_r->ws = new_ws;
2155 /* this is needed so that we can click on a window after a restart */
2159 a.id = SWM_ARG_ID_FOCUSCUR;
2160 focus(new_ws->r, &a);
2164 /* unmap old windows */
2166 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2169 if (focus_mode == SWM_FOCUS_DEFAULT)
2170 drain_enter_notify();
2174 cyclews(struct swm_region *r, union arg *args)
2177 struct swm_screen *s = r->s;
2180 DNPRINTF(SWM_D_WS, "cyclews id %d "
2181 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2182 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2187 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2190 case SWM_ARG_ID_CYCLEWS_UP:
2191 if (a.id < SWM_WS_MAX - 1)
2196 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2199 case SWM_ARG_ID_CYCLEWS_DOWN:
2203 a.id = SWM_WS_MAX - 1;
2210 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2212 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2216 } while (a.id != r->ws->idx);
2220 priorws(struct swm_region *r, union arg *args)
2224 DNPRINTF(SWM_D_WS, "priorws id %d "
2225 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2226 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2228 if (r->ws_prior == NULL)
2231 a.id = r->ws_prior->idx;
2236 cyclescr(struct swm_region *r, union arg *args)
2238 struct swm_region *rr = NULL;
2242 /* do nothing if we don't have more than one screen */
2243 if (!(ScreenCount(display) > 1 || outputs > 1))
2248 case SWM_ARG_ID_CYCLESC_UP:
2249 rr = TAILQ_NEXT(r, entry);
2251 rr = TAILQ_FIRST(&screens[i].rl);
2253 case SWM_ARG_ID_CYCLESC_DOWN:
2254 rr = TAILQ_PREV(r, swm_region_list, entry);
2256 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2264 /* move mouse to region */
2266 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2267 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2269 a.id = SWM_ARG_ID_FOCUSCUR;
2272 if (rr->ws->focus) {
2273 /* move to focus window */
2274 x = rr->ws->focus->g.x + 1;
2275 y = rr->ws->focus->g.y + 1;
2276 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2281 sort_windows(struct ws_win_list *wl)
2283 struct ws_win *win, *parent, *nxt;
2288 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2289 nxt = TAILQ_NEXT(win, entry);
2290 if (win->transient) {
2291 parent = find_window(win->transient);
2292 if (parent == NULL) {
2293 fprintf(stderr, "not possible bug\n");
2296 TAILQ_REMOVE(wl, win, entry);
2297 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2304 swapwin(struct swm_region *r, union arg *args)
2306 struct ws_win *target, *source;
2307 struct ws_win *cur_focus;
2308 struct ws_win_list *wl;
2311 DNPRINTF(SWM_D_WS, "swapwin id %d "
2312 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2313 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2315 cur_focus = r->ws->focus;
2316 if (cur_focus == NULL)
2320 wl = &source->ws->winlist;
2323 case SWM_ARG_ID_SWAPPREV:
2324 if (source->transient)
2325 source = find_window(source->transient);
2326 target = TAILQ_PREV(source, ws_win_list, entry);
2327 if (target && target->transient)
2328 target = find_window(target->transient);
2329 TAILQ_REMOVE(wl, source, entry);
2331 TAILQ_INSERT_TAIL(wl, source, entry);
2333 TAILQ_INSERT_BEFORE(target, source, entry);
2335 case SWM_ARG_ID_SWAPNEXT:
2336 target = TAILQ_NEXT(source, entry);
2337 /* move the parent and let the sort handle the move */
2338 if (source->transient)
2339 source = find_window(source->transient);
2340 TAILQ_REMOVE(wl, source, entry);
2342 TAILQ_INSERT_HEAD(wl, source, entry);
2344 TAILQ_INSERT_AFTER(wl, target, source, entry);
2346 case SWM_ARG_ID_SWAPMAIN:
2347 target = TAILQ_FIRST(wl);
2348 if (target == source) {
2349 if (source->ws->focus_prev != NULL &&
2350 source->ws->focus_prev != target)
2352 source = source->ws->focus_prev;
2356 if (target == NULL || source == NULL)
2358 source->ws->focus_prev = target;
2359 TAILQ_REMOVE(wl, target, entry);
2360 TAILQ_INSERT_BEFORE(source, target, entry);
2361 TAILQ_REMOVE(wl, source, entry);
2362 TAILQ_INSERT_HEAD(wl, source, entry);
2364 case SWM_ARG_ID_MOVELAST:
2365 TAILQ_REMOVE(wl, source, entry);
2366 TAILQ_INSERT_TAIL(wl, source, entry);
2369 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2379 focus_prev(struct ws_win *win)
2381 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2382 struct ws_win *cur_focus = NULL;
2383 struct ws_win_list *wl = NULL;
2384 struct workspace *ws = NULL;
2386 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2388 if (!(win && win->ws))
2393 cur_focus = ws->focus;
2394 winlostfocus = cur_focus;
2396 /* pickle, just focus on whatever */
2397 if (cur_focus == NULL) {
2398 /* use prev_focus if valid */
2399 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2400 find_window(WINID(ws->focus_prev)))
2401 winfocus = ws->focus_prev;
2402 if (winfocus == NULL)
2403 winfocus = TAILQ_FIRST(wl);
2407 /* if transient focus on parent */
2408 if (cur_focus->transient) {
2409 winfocus = find_window(cur_focus->transient);
2413 /* if in max_stack try harder */
2414 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2415 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2416 if (cur_focus != ws->focus_prev)
2417 winfocus = ws->focus_prev;
2418 else if (cur_focus != ws->focus)
2419 winfocus = ws->focus;
2421 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2426 if (cur_focus == win)
2427 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2428 if (winfocus == NULL)
2429 winfocus = TAILQ_LAST(wl, ws_win_list);
2430 if (winfocus == NULL || winfocus == win)
2431 winfocus = TAILQ_NEXT(cur_focus, entry);
2433 if (winfocus == winlostfocus || winfocus == NULL) {
2434 /* update the bar so that title/class/name will be cleared. */
2435 if (window_name_enabled || title_name_enabled ||
2436 title_class_enabled)
2442 focus_magic(winfocus);
2446 focus(struct swm_region *r, union arg *args)
2448 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2449 struct ws_win *cur_focus = NULL;
2450 struct ws_win_list *wl = NULL;
2451 struct workspace *ws = NULL;
2457 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2459 /* treat FOCUS_CUR special */
2460 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2461 if (r->ws->focus && r->ws->focus->iconic == 0)
2462 winfocus = r->ws->focus;
2463 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2464 winfocus = r->ws->focus_prev;
2466 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2467 if (winfocus->iconic == 0)
2470 focus_magic(winfocus);
2474 if ((cur_focus = r->ws->focus) == NULL)
2478 if (TAILQ_EMPTY(wl))
2480 /* make sure there is at least one uniconified window */
2482 TAILQ_FOREACH(winfocus, wl, entry)
2483 if (winfocus->iconic == 0) {
2490 winlostfocus = cur_focus;
2493 case SWM_ARG_ID_FOCUSPREV:
2494 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2496 head = TAILQ_LAST(wl, ws_win_list);
2498 if (WINID(winfocus) == cur_focus->transient) {
2499 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2501 head = TAILQ_LAST(wl, ws_win_list);
2506 if (winfocus && winfocus->iconic) {
2507 while (winfocus != cur_focus) {
2508 if (winfocus == NULL)
2509 winfocus = TAILQ_LAST(wl, ws_win_list);
2510 if (winfocus->iconic == 0)
2512 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
2517 case SWM_ARG_ID_FOCUSNEXT:
2518 head = TAILQ_NEXT(cur_focus, entry);
2520 head = TAILQ_FIRST(wl);
2524 if (winfocus && winfocus->iconic) {
2525 while (winfocus != cur_focus) {
2526 if (winfocus == NULL)
2527 winfocus = TAILQ_FIRST(wl);
2528 if (winfocus->iconic == 0)
2530 winfocus = TAILQ_NEXT(winfocus, entry);
2535 case SWM_ARG_ID_FOCUSMAIN:
2536 winfocus = TAILQ_FIRST(wl);
2537 if (winfocus == cur_focus)
2538 winfocus = cur_focus->ws->focus_prev;
2544 if (winfocus == winlostfocus || winfocus == NULL) {
2545 /* update the bar so that title/class/name will be cleared. */
2546 if (window_name_enabled || title_name_enabled ||
2547 title_class_enabled)
2553 focus_magic(winfocus);
2557 cycle_layout(struct swm_region *r, union arg *args)
2559 struct workspace *ws = r->ws;
2562 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2565 if (ws->cur_layout->l_stack == NULL)
2566 ws->cur_layout = &layouts[0];
2569 if (focus_mode == SWM_FOCUS_DEFAULT)
2570 drain_enter_notify();
2571 a.id = SWM_ARG_ID_FOCUSCUR;
2577 stack_config(struct swm_region *r, union arg *args)
2579 struct workspace *ws = r->ws;
2581 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2584 if (ws->cur_layout->l_config != NULL)
2585 ws->cur_layout->l_config(ws, args->id);
2587 if (args->id != SWM_ARG_ID_STACKINIT)
2594 struct swm_geometry g;
2595 struct swm_region *r;
2601 DNPRINTF(SWM_D_STACK, "stack\n");
2603 for (i = 0; i < ScreenCount(display); i++) {
2607 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2608 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2609 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2611 /* start with screen geometry, adjust for bar */
2613 g.w -= 2 * border_width;
2614 g.h -= 2 * border_width;
2620 r->ws->cur_layout->l_stack(r->ws, &g);
2621 r->ws->cur_layout->l_string(r->ws);
2622 /* save r so we can track region changes */
2629 if (focus_mode == SWM_FOCUS_DEFAULT)
2630 drain_enter_notify();
2634 store_float_geom(struct ws_win *win, struct swm_region *r)
2636 /* retain window geom and region geom */
2637 win->g_float.x = win->g.x;
2638 win->g_float.y = win->g.y;
2639 win->g_float.w = win->g.w;
2640 win->g_float.h = win->g.h;
2641 win->rg_float.x = r->g.x;
2642 win->rg_float.y = r->g.y;
2643 win->rg_float.w = r->g.w;
2644 win->rg_float.h = r->g.h;
2645 win->g_floatvalid = 1;
2649 stack_floater(struct ws_win *win, struct swm_region *r)
2657 bzero(&wc, sizeof wc);
2658 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2661 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2662 * geom on ws switches or return from max mode
2665 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2666 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2668 * use stored g and rg to set relative position and size
2669 * as in old region or before max stack mode
2671 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2672 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2673 win->g.w = win->g_float.w;
2674 win->g.h = win->g_float.h;
2675 win->g_floatvalid = 0;
2678 win->floatmaxed = 0;
2680 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2681 (win->g.h >= HEIGHT(r)))
2682 wc.border_width = 0;
2684 wc.border_width = border_width;
2685 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2686 win->g.w = (double)WIDTH(r) * dialog_ratio;
2687 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2692 * floaters and transients are auto-centred unless moved
2695 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2696 2 - wc.border_width;
2697 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2698 2 - wc.border_width;
2701 /* win can be outside r if new r smaller than old r */
2702 /* Ensure top left corner inside r (move probs otherwise) */
2703 if (win->g.x < r->g.x - wc.border_width)
2704 win->g.x = r->g.x - wc.border_width;
2705 if (win->g.x > r->g.x + r->g.w - 1)
2706 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2707 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2708 if (win->g.y < r->g.y - wc.border_width)
2709 win->g.y = r->g.y - wc.border_width;
2710 if (win->g.y > r->g.y + r->g.h - 1)
2711 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2712 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2716 wc.width = win->g.w;
2717 wc.height = win->g.h;
2720 * Retain floater and transient geometry for correct positioning
2721 * when ws changes region
2723 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2724 store_float_geom(win, r);
2726 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2727 win->id, wc.x, wc.y, wc.width, wc.height);
2729 XConfigureWindow(display, win->id, mask, &wc);
2733 * Send keystrokes to terminal to decrease/increase the font size as the
2734 * window size changes.
2737 adjust_font(struct ws_win *win)
2739 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2740 win->floating || win->transient)
2743 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2744 win->g.w / win->sh.width_inc < term_width &&
2745 win->font_steps < SWM_MAX_FONT_STEPS) {
2746 win->font_size_boundary[win->font_steps] =
2747 (win->sh.width_inc * term_width) + win->sh.base_width;
2750 win->last_inc = win->sh.width_inc;
2751 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2752 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2753 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2756 win->last_inc = win->sh.width_inc;
2757 fake_keypress(win, XK_KP_Add, ShiftMask);
2761 #define SWAPXY(g) do { \
2763 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2764 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2767 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2770 XWindowAttributes wa;
2771 struct swm_geometry win_g, r_g = *g;
2772 struct ws_win *win, *fs_win = 0;
2773 int i, j, s, stacks;
2774 int w_inc = 1, h_inc, w_base = 1, h_base;
2775 int hrh, extra = 0, h_slice, last_h = 0;
2776 int split, colno, winno, mwin, msize, mscale;
2777 int remain, missing, v_slice, reconfigure;
2780 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2781 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2783 winno = count_win(ws, 0);
2784 if (winno == 0 && count_win(ws, 1) == 0)
2787 TAILQ_FOREACH(win, &ws->winlist, entry)
2788 if (win->transient == 0 && win->floating == 0
2789 && win->iconic == 0)
2796 w_inc = win->sh.width_inc;
2797 w_base = win->sh.base_width;
2798 mwin = ws->l_state.horizontal_mwin;
2799 mscale = ws->l_state.horizontal_msize;
2800 stacks = ws->l_state.horizontal_stacks;
2803 w_inc = win->sh.height_inc;
2804 w_base = win->sh.base_height;
2805 mwin = ws->l_state.vertical_mwin;
2806 mscale = ws->l_state.vertical_msize;
2807 stacks = ws->l_state.vertical_stacks;
2811 if (stacks > winno - mwin)
2812 stacks = winno - mwin;
2816 h_slice = r_g.h / SWM_H_SLICE;
2817 if (mwin && winno > mwin) {
2818 v_slice = r_g.w / SWM_V_SLICE;
2822 win_g.w = v_slice * mscale;
2824 if (w_inc > 1 && w_inc < v_slice) {
2825 /* adjust for window's requested size increment */
2826 remain = (win_g.w - w_base) % w_inc;
2827 missing = w_inc - remain;
2834 win_g.x += r_g.w - msize;
2837 colno = split = winno / stacks;
2838 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2839 2 * border_width) / stacks);
2841 hrh = r_g.h / colno;
2842 extra = r_g.h - (colno * hrh);
2843 win_g.h = hrh - 2 * border_width;
2845 /* stack all the tiled windows */
2846 i = j = 0, s = stacks;
2847 TAILQ_FOREACH(win, &ws->winlist, entry) {
2848 if (win->transient != 0 || win->floating != 0)
2850 if (win->iconic != 0)
2853 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2858 if (split && i == split) {
2859 colno = (winno - mwin) / stacks;
2860 if (s <= (winno - mwin) % stacks)
2862 split = split + colno;
2863 hrh = (r_g.h / colno);
2864 extra = r_g.h - (colno * hrh);
2868 win_g.x += win_g.w + 2 * border_width;
2869 win_g.w = (r_g.w - msize -
2870 (stacks * 2 * border_width)) / stacks;
2872 win_g.w += (r_g.w - msize -
2873 (stacks * 2 * border_width)) % stacks;
2877 win_g.h = hrh - 2 * border_width;
2879 h_inc = win->sh.width_inc;
2880 h_base = win->sh.base_width;
2882 h_inc = win->sh.height_inc;
2883 h_base = win->sh.base_height;
2885 if (j == colno - 1) {
2886 win_g.h = hrh + extra;
2887 } else if (h_inc > 1 && h_inc < h_slice) {
2888 /* adjust for window's requested size increment */
2889 remain = (win_g.h - h_base) % h_inc;
2890 missing = h_inc - remain;
2892 if (missing <= extra || j == 0) {
2904 win_g.y += last_h + 2 * border_width;
2906 bzero(&wc, sizeof wc);
2907 if (disable_border && bar_enabled == 0 && winno == 1){
2908 wc.border_width = 0;
2909 win_g.w += 2 * border_width;
2910 win_g.h += 2 * border_width;
2912 wc.border_width = border_width;
2915 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2916 win->g.w != win_g.h || win->g.h != win_g.w) {
2918 win->g.x = wc.x = win_g.y;
2919 win->g.y = wc.y = win_g.x;
2920 win->g.w = wc.width = win_g.h;
2921 win->g.h = wc.height = win_g.w;
2924 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2925 win->g.w != win_g.w || win->g.h != win_g.h) {
2927 win->g.x = wc.x = win_g.x;
2928 win->g.y = wc.y = win_g.y;
2929 win->g.w = wc.width = win_g.w;
2930 win->g.h = wc.height = win_g.h;
2935 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2936 XConfigureWindow(display, win->id, mask, &wc);
2939 if (XGetWindowAttributes(display, win->id, &wa))
2940 if (wa.map_state == IsUnmapped)
2941 XMapRaised(display, win->id);
2949 /* now, stack all the floaters and transients */
2950 TAILQ_FOREACH(win, &ws->winlist, entry) {
2951 if (win->transient == 0 && win->floating == 0)
2953 if (win->iconic == 1)
2955 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2960 stack_floater(win, ws->r);
2961 XMapRaised(display, win->id);
2965 stack_floater(fs_win, ws->r);
2966 XMapRaised(display, fs_win->id);
2971 vertical_config(struct workspace *ws, int id)
2973 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2976 case SWM_ARG_ID_STACKRESET:
2977 case SWM_ARG_ID_STACKINIT:
2978 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2979 ws->l_state.vertical_mwin = 1;
2980 ws->l_state.vertical_stacks = 1;
2982 case SWM_ARG_ID_MASTERSHRINK:
2983 if (ws->l_state.vertical_msize > 1)
2984 ws->l_state.vertical_msize--;
2986 case SWM_ARG_ID_MASTERGROW:
2987 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2988 ws->l_state.vertical_msize++;
2990 case SWM_ARG_ID_MASTERADD:
2991 ws->l_state.vertical_mwin++;
2993 case SWM_ARG_ID_MASTERDEL:
2994 if (ws->l_state.vertical_mwin > 0)
2995 ws->l_state.vertical_mwin--;
2997 case SWM_ARG_ID_STACKINC:
2998 ws->l_state.vertical_stacks++;
3000 case SWM_ARG_ID_STACKDEC:
3001 if (ws->l_state.vertical_stacks > 1)
3002 ws->l_state.vertical_stacks--;
3010 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3012 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3014 stack_master(ws, g, 0, 0);
3018 horizontal_config(struct workspace *ws, int id)
3020 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3023 case SWM_ARG_ID_STACKRESET:
3024 case SWM_ARG_ID_STACKINIT:
3025 ws->l_state.horizontal_mwin = 1;
3026 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3027 ws->l_state.horizontal_stacks = 1;
3029 case SWM_ARG_ID_MASTERSHRINK:
3030 if (ws->l_state.horizontal_msize > 1)
3031 ws->l_state.horizontal_msize--;
3033 case SWM_ARG_ID_MASTERGROW:
3034 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3035 ws->l_state.horizontal_msize++;
3037 case SWM_ARG_ID_MASTERADD:
3038 ws->l_state.horizontal_mwin++;
3040 case SWM_ARG_ID_MASTERDEL:
3041 if (ws->l_state.horizontal_mwin > 0)
3042 ws->l_state.horizontal_mwin--;
3044 case SWM_ARG_ID_STACKINC:
3045 ws->l_state.horizontal_stacks++;
3047 case SWM_ARG_ID_STACKDEC:
3048 if (ws->l_state.horizontal_stacks > 1)
3049 ws->l_state.horizontal_stacks--;
3057 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3059 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3061 stack_master(ws, g, 1, 0);
3064 /* fullscreen view */
3066 max_stack(struct workspace *ws, struct swm_geometry *g)
3069 struct swm_geometry gg = *g;
3070 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3074 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3079 winno = count_win(ws, 0);
3080 if (winno == 0 && count_win(ws, 1) == 0)
3083 TAILQ_FOREACH(win, &ws->winlist, entry) {
3084 if (win->transient) {
3086 parent = find_window(win->transient);
3090 if (win->floating && win->floatmaxed == 0 ) {
3092 * retain geometry for retrieval on exit from
3095 store_float_geom(win, ws->r);
3096 win->floatmaxed = 1;
3099 /* only reconfigure if necessary */
3100 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3102 bzero(&wc, sizeof wc);
3103 win->g.x = wc.x = gg.x;
3104 win->g.y = wc.y = gg.y;
3106 wc.border_width = border_width;
3107 win->g.w = wc.width = gg.w;
3108 win->g.h = wc.height = gg.h;
3110 wc.border_width = 0;
3111 win->g.w = wc.width = gg.w + 2 * border_width;
3112 win->g.h = wc.height = gg.h + 2 * border_width;
3114 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3115 XConfigureWindow(display, win->id, mask, &wc);
3117 /* unmap only if we don't have multi screen */
3118 if (win != ws->focus)
3119 if (!(ScreenCount(display) > 1 || outputs > 1))
3123 /* put the last transient on top */
3126 XMapRaised(display, parent->id);
3127 stack_floater(wintrans, ws->r);
3128 focus_magic(wintrans);
3133 send_to_ws(struct swm_region *r, union arg *args)
3135 int wsid = args->id;
3136 struct ws_win *win = NULL, *parent;
3137 struct workspace *ws, *nws;
3138 Atom ws_idx_atom = 0;
3139 unsigned char ws_idx_str[SWM_PROPLEN];
3142 if (r && r->ws && r->ws->focus)
3148 if (win->ws->idx == wsid)
3151 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3154 nws = &win->s->ws[wsid];
3156 a.id = SWM_ARG_ID_FOCUSPREV;
3158 if (win->transient) {
3159 parent = find_window(win->transient);
3161 unmap_window(parent);
3162 TAILQ_REMOVE(&ws->winlist, parent, entry);
3163 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3168 TAILQ_REMOVE(&ws->winlist, win, entry);
3169 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3170 if (TAILQ_EMPTY(&ws->winlist))
3171 r->ws->focus = NULL;
3174 /* Try to update the window's workspace property */
3175 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3177 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3179 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3181 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3182 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3189 pressbutton(struct swm_region *r, union arg *args)
3191 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3192 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3196 raise_toggle(struct swm_region *r, union arg *args)
3198 if (r && r->ws == NULL)
3201 r->ws->always_raise = !r->ws->always_raise;
3203 /* bring floaters back to top */
3204 if (r->ws->always_raise == 0)
3209 iconify(struct swm_region *r, union arg *args)
3213 if (r->ws->focus == NULL)
3215 unmap_window(r->ws->focus);
3216 update_iconic(r->ws->focus, 1);
3218 if (focus_mode == SWM_FOCUS_DEFAULT)
3219 drain_enter_notify();
3220 r->ws->focus = NULL;
3221 a.id = SWM_ARG_ID_FOCUSCUR;
3226 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3227 unsigned long *slen)
3230 unsigned long nitems, nbytes, nextra;
3231 unsigned char *prop = NULL;
3234 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3235 &rettype, &retfmt, &nitems, &nbytes, &prop);
3236 if (status != Success)
3240 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3241 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3242 if (status != Success) {
3246 if (rettype != stype) {
3255 uniconify(struct swm_region *r, union arg *args)
3259 unsigned char *name;
3263 DNPRINTF(SWM_D_MISC, "uniconify\n");
3265 if (r && r->ws == NULL)
3268 /* make sure we have anything to uniconify */
3269 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3270 if (win->ws == NULL)
3271 continue; /* should never happen */
3272 if (win->iconic == 0)
3280 search_resp_action = SWM_SEARCH_UNICONIFY;
3282 spawn_select(r, args, "search", &searchpid);
3284 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3287 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3288 if (win->ws == NULL)
3289 continue; /* should never happen */
3290 if (win->iconic == 0)
3293 name = get_win_name(display, win->id, a_netwmname, a_utf8_string,
3297 fprintf(lfile, "%s.%lu\n", name, win->id);
3305 name_workspace(struct swm_region *r, union arg *args)
3309 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3315 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3317 spawn_select(r, args, "name_workspace", &searchpid);
3319 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3322 fprintf(lfile, "%s", "");
3327 search_workspace(struct swm_region *r, union arg *args)
3330 struct workspace *ws;
3333 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3339 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3341 spawn_select(r, args, "search", &searchpid);
3343 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3346 for (i = 0; i < SWM_WS_MAX; i++) {
3350 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3351 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3358 search_win_cleanup(void)
3360 struct search_window *sw = NULL;
3362 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3363 XDestroyWindow(display, sw->indicator);
3364 TAILQ_REMOVE(&search_wl, sw, entry);
3370 search_win(struct swm_region *r, union arg *args)
3372 struct ws_win *win = NULL;
3373 struct search_window *sw = NULL;
3383 DNPRINTF(SWM_D_MISC, "search_win\n");
3386 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3388 spawn_select(r, args, "search", &searchpid);
3390 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3393 TAILQ_INIT(&search_wl);
3396 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3397 if (win->iconic == 1)
3400 sw = calloc(1, sizeof(struct search_window));
3402 fprintf(stderr, "search_win: calloc: %s", strerror(errno));
3404 search_win_cleanup();
3410 snprintf(s, sizeof s, "%d", i);
3412 textwidth = XTextWidth(bar_fs, s, len);
3414 w = XCreateSimpleWindow(display,
3415 win->id, 0, 0, textwidth + 12,
3416 bar_fs->ascent + bar_fs->descent + 4, 1,
3417 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3418 r->s->c[SWM_S_COLOR_FOCUS].color);
3421 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3423 gc = XCreateGC(display, w, 0, &gcv);
3424 XSetFont(display, gc, bar_fs->fid);
3425 XMapRaised(display, w);
3426 XSetForeground(display, gc, r->s->c[SWM_S_COLOR_BAR].color);
3428 XDrawString(display, w, gc, 6, bar_fs->ascent + 2, s, len);
3430 fprintf(lfile, "%d\n", i);
3438 search_resp_uniconify(char *resp, unsigned long len)
3440 unsigned char *name;
3444 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp %s\n", resp);
3446 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3447 if (win->iconic == 0)
3449 name = get_win_name(display, win->id, a_netwmname, a_utf8_string, &len);
3452 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3457 if (strncmp(s, resp, len) == 0) {
3458 /* XXX this should be a callback to generalize */
3459 update_iconic(win, 0);
3468 search_resp_name_workspace(char *resp, unsigned long len)
3470 struct workspace *ws;
3472 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp %s\n", resp);
3474 if (search_r->ws == NULL)
3479 free(search_r->ws->name);
3480 search_r->ws->name = NULL;
3484 ws->name = strdup(resp);
3485 if (ws->name == NULL) {
3486 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: strdup: %s",
3494 search_resp_search_workspace(char *resp, unsigned long len)
3501 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp %s\n", resp);
3505 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3512 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3514 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3521 switchws(search_r, &a);
3525 search_resp_search_window(char *resp, unsigned long len)
3530 struct search_window *sw;
3532 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp %s\n", resp);
3536 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3541 idx = strtonum(s, 1, INT_MAX, &errstr);
3543 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3550 TAILQ_FOREACH(sw, &search_wl, entry)
3551 if (idx == sw->idx) {
3557 #define MAX_RESP_LEN 1024
3560 search_do_resp(void)
3566 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3571 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3572 fprintf(stderr, "search: calloc\n");
3576 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3578 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3581 resp[rbytes] = '\0';
3584 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3585 * newline, so work around that by sanitizing the resp now.
3587 resp[strcspn(resp, "\n")] = '\0';
3590 switch (search_resp_action) {
3591 case SWM_SEARCH_UNICONIFY:
3592 search_resp_uniconify(resp, len);
3594 case SWM_SEARCH_NAME_WORKSPACE:
3595 search_resp_name_workspace(resp, len);
3597 case SWM_SEARCH_SEARCH_WORKSPACE:
3598 search_resp_search_workspace(resp, len);
3600 case SWM_SEARCH_SEARCH_WINDOW:
3601 search_resp_search_window(resp, len);
3606 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3607 search_win_cleanup();
3609 search_resp_action = SWM_SEARCH_NONE;
3610 close(select_resp_pipe[0]);
3615 wkill(struct swm_region *r, union arg *args)
3617 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3619 if (r->ws->focus == NULL)
3622 if (args->id == SWM_ARG_ID_KILLWINDOW)
3623 XKillClient(display, r->ws->focus->id);
3625 if (r->ws->focus->can_delete)
3626 client_msg(r->ws->focus, adelete);
3631 floating_toggle_win(struct ws_win *win)
3633 struct swm_region *r;
3643 /* reject floating toggles in max stack mode */
3644 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3647 if (win->floating) {
3648 if (!win->floatmaxed) {
3649 /* retain position for refloat */
3650 store_float_geom(win, r);
3654 if (win->g_floatvalid) {
3655 /* refloat at last floating relative position */
3656 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3657 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3658 win->g.w = win->g_float.w;
3659 win->g.h = win->g_float.h;
3664 ewmh_update_actions(win);
3670 floating_toggle(struct swm_region *r, union arg *args)
3672 struct ws_win *win = r->ws->focus;
3678 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3679 _NET_WM_STATE_TOGGLE);
3682 if (focus_mode == SWM_FOCUS_DEFAULT)
3683 drain_enter_notify();
3685 if (win == win->ws->focus) {
3686 a.id = SWM_ARG_ID_FOCUSCUR;
3687 focus(win->ws->r, &a);
3692 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3694 if (win->g.x + win->g.w > r->g.x + r->g.w - border_width) {
3696 win->g.w = r->g.x + r->g.w - win->g.x - border_width;
3698 win->g.x = r->g.x + r->g.w - win->g.w - border_width;
3701 if (win->g.x < r->g.x - border_width) {
3703 win->g.w -= r->g.x - win->g.x - border_width;
3705 win->g.x = r->g.x - border_width;
3708 if (win->g.y + win->g.h > r->g.y + r->g.h - border_width) {
3710 win->g.h = r->g.y + r->g.h - win->g.y - border_width;
3712 win->g.y = r->g.y + r->g.h - win->g.h - border_width;
3715 if (win->g.y < r->g.y - border_width) {
3717 win->g.h -= r->g.y - win->g.y - border_width;
3719 win->g.y = r->g.y - border_width;
3729 update_window(struct ws_win *win)
3734 bzero(&wc, sizeof wc);
3735 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
3736 wc.border_width = border_width;
3739 wc.width = win->g.w;
3740 wc.height = win->g.h;
3742 DNPRINTF(SWM_D_STACK, "update_window: win %lu x %d y %d w %d h %d\n",
3743 win->id, wc.x, wc.y, wc.width, wc.height);
3745 XConfigureWindow(display, win->id, mask, &wc);
3748 #define SWM_RESIZE_STEPS (50)
3751 resize(struct ws_win *win, union arg *args)
3755 struct swm_region *r = NULL;
3756 int resize_step = 0;
3760 struct swm_geometry g;
3761 int top = 0, left = 0;
3764 unsigned int shape; /* cursor style */
3770 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3771 win->id, win->floating, win->transient);
3773 if (!(win->transient != 0 || win->floating != 0))
3776 /* reject resizes in max mode for floaters (transient ok) */
3777 if (win->floatmaxed)
3781 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3787 case SWM_ARG_ID_WIDTHSHRINK:
3788 win->g.w -= SWM_RESIZE_STEPS;
3791 case SWM_ARG_ID_WIDTHGROW:
3792 win->g.w += SWM_RESIZE_STEPS;
3795 case SWM_ARG_ID_HEIGHTSHRINK:
3796 win->g.h -= SWM_RESIZE_STEPS;
3799 case SWM_ARG_ID_HEIGHTGROW:
3800 win->g.h += SWM_RESIZE_STEPS;
3807 constrain_window(win, r, 1);
3809 store_float_geom(win,r);
3813 if (focus_mode == SWM_FOCUS_DEFAULT)
3814 drain_enter_notify();
3816 /* get cursor offset from window root */
3817 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
3822 if (wx < win->g.w / 2)
3825 if (wy < win->g.h / 2)
3828 if (args->id == SWM_ARG_ID_CENTER)
3831 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
3833 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
3835 cursor = XCreateFontCursor(display, shape);
3837 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3838 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
3839 XFreeCursor(display, cursor);
3844 XMaskEvent(display, MOUSEMASK | ExposureMask |
3845 SubstructureRedirectMask, &ev);
3847 case ConfigureRequest:
3850 handler[ev.type](&ev);
3853 /* cursor offset/delta from start of the operation */
3854 dx = ev.xmotion.x_root - x;
3855 dy = ev.xmotion.y_root - y;
3861 if (args->id == SWM_ARG_ID_CENTER) {
3862 if (g.h / 2 + dy < 1)
3865 win->g.y = g.y - dy;
3866 win->g.h = g.h + 2 * dy;
3872 win->g.y = g.y - dy;
3874 win->g.h = g.h + dy;
3881 if (args->id == SWM_ARG_ID_CENTER) {
3882 if (g.w / 2 + dx < 1)
3885 win->g.x = g.x - dx;
3886 win->g.w = g.w + 2 * dx;
3892 win->g.x = g.x - dx;
3894 win->g.w = g.w + dx;
3897 constrain_window(win, r, 1);
3899 /* not free, don't sync more than 120 times / second */
3900 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3901 time = ev.xmotion.time;
3902 XSync(display, False);
3907 } while (ev.type != ButtonRelease);
3909 XSync(display, False);
3912 store_float_geom(win,r);
3914 XUngrabPointer(display, CurrentTime);
3915 XFreeCursor(display, cursor);
3918 drain_enter_notify();
3922 resize_step(struct swm_region *r, union arg *args)
3924 struct ws_win *win = NULL;
3926 if (r && r->ws && r->ws->focus)
3934 #define SWM_MOVE_STEPS (50)
3937 move(struct ws_win *win, union arg *args)
3942 struct swm_region *r = NULL;
3952 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3953 win->id, win->floating, win->transient);
3955 /* in max_stack mode should only move transients */
3956 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3960 if (win->floating == 0 && !win->transient) {
3961 store_float_geom(win,r);
3962 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3965 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3972 case SWM_ARG_ID_MOVELEFT:
3973 win->g.x -= (SWM_MOVE_STEPS - border_width);
3976 case SWM_ARG_ID_MOVERIGHT:
3977 win->g.x += (SWM_MOVE_STEPS - border_width);
3980 case SWM_ARG_ID_MOVEUP:
3981 win->g.y -= (SWM_MOVE_STEPS - border_width);
3984 case SWM_ARG_ID_MOVEDOWN:
3985 win->g.y += (SWM_MOVE_STEPS - border_width);
3992 constrain_window(win, r, 0);
3994 store_float_geom(win,r);
3998 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3999 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4000 CurrentTime) != GrabSuccess)
4003 /* get cursor offset from window root */
4004 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4008 XMaskEvent(display, MOUSEMASK | ExposureMask |
4009 SubstructureRedirectMask, &ev);
4011 case ConfigureRequest:
4014 handler[ev.type](&ev);
4017 win->g.x = ev.xmotion.x_root - wx - border_width;
4018 win->g.y = ev.xmotion.y_root - wy - border_width;
4020 constrain_window(win, r, 0);
4022 /* not free, don't sync more than 120 times / second */
4023 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4024 time = ev.xmotion.time;
4025 XSync(display, False);
4030 } while (ev.type != ButtonRelease);
4032 XSync(display, False);
4035 store_float_geom(win,r);
4036 XUngrabPointer(display, CurrentTime);
4039 drain_enter_notify();
4043 move_step(struct swm_region *r, union arg *args)
4045 struct ws_win *win = NULL;
4047 if (r && r->ws && r->ws->focus)
4052 if (!(win->transient != 0 || win->floating != 0))
4059 /* user/key callable function IDs */
4129 kf_search_workspace,
4131 kf_dumpwins, /* MUST BE LAST */
4135 /* key definitions */
4137 dummykeyfunc(struct swm_region *r, union arg *args)
4142 legacyfunc(struct swm_region *r, union arg *args)
4147 char name[SWM_FUNCNAME_LEN];
4148 void (*func)(struct swm_region *r, union arg *);
4150 } keyfuncs[kf_invalid + 1] = {
4151 /* name function argument */
4152 { "cycle_layout", cycle_layout, {0} },
4153 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4154 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4155 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4156 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4157 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4158 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4159 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4160 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4161 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4162 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4163 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4164 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4165 { "spawn_term", spawnterm, {.argv = spawn_term} },
4166 { "spawn_menu", legacyfunc, {0} },
4167 { "quit", quit, {0} },
4168 { "restart", restart, {0} },
4169 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4170 { "ws_1", switchws, {.id = 0} },
4171 { "ws_2", switchws, {.id = 1} },
4172 { "ws_3", switchws, {.id = 2} },
4173 { "ws_4", switchws, {.id = 3} },
4174 { "ws_5", switchws, {.id = 4} },
4175 { "ws_6", switchws, {.id = 5} },
4176 { "ws_7", switchws, {.id = 6} },
4177 { "ws_8", switchws, {.id = 7} },
4178 { "ws_9", switchws, {.id = 8} },
4179 { "ws_10", switchws, {.id = 9} },
4180 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4181 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4182 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4183 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4184 { "ws_prior", priorws, {0} },
4185 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4186 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4187 { "mvws_1", send_to_ws, {.id = 0} },
4188 { "mvws_2", send_to_ws, {.id = 1} },
4189 { "mvws_3", send_to_ws, {.id = 2} },
4190 { "mvws_4", send_to_ws, {.id = 3} },
4191 { "mvws_5", send_to_ws, {.id = 4} },
4192 { "mvws_6", send_to_ws, {.id = 5} },
4193 { "mvws_7", send_to_ws, {.id = 6} },
4194 { "mvws_8", send_to_ws, {.id = 7} },
4195 { "mvws_9", send_to_ws, {.id = 8} },
4196 { "mvws_10", send_to_ws, {.id = 9} },
4197 { "bar_toggle", bar_toggle, {0} },
4198 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4199 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4200 { "screenshot_all", legacyfunc, {0} },
4201 { "screenshot_wind", legacyfunc, {0} },
4202 { "float_toggle", floating_toggle,{0} },
4203 { "version", version, {0} },
4204 { "spawn_lock", legacyfunc, {0} },
4205 { "spawn_initscr", legacyfunc, {0} },
4206 { "spawn_custom", dummykeyfunc, {0} },
4207 { "iconify", iconify, {0} },
4208 { "uniconify", uniconify, {0} },
4209 { "raise_toggle", raise_toggle, {0} },
4210 { "button2", pressbutton, {2} },
4211 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4212 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4213 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4214 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4215 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4216 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4217 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4218 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4219 { "name_workspace", name_workspace, {0} },
4220 { "search_workspace", search_workspace, {0} },
4221 { "search_win", search_win, {0} },
4222 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4223 { "invalid key func", NULL, {0} },
4228 enum keyfuncid funcid;
4231 int keys_size = 0, keys_length = 0;
4232 struct key *keys = NULL;
4235 enum { client_click, root_click };
4237 unsigned int action;
4239 unsigned int button;
4240 void (*func)(struct ws_win *, union arg *);
4243 /* action key mouse button func args */
4244 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4245 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4246 { client_click, MODKEY, Button1, move, {0} },
4250 update_modkey(unsigned int mod)
4255 for (i = 0; i < keys_length; i++)
4256 if (keys[i].mod & ShiftMask)
4257 keys[i].mod = mod | ShiftMask;
4261 for (i = 0; i < LENGTH(buttons); i++)
4262 if (buttons[i].mask & ShiftMask)
4263 buttons[i].mask = mod | ShiftMask;
4265 buttons[i].mask = mod;
4275 int spawns_size = 0, spawns_length = 0;
4276 struct spawn_prog *spawns = NULL;
4279 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4282 struct spawn_prog *prog = NULL;
4284 char *ap, **real_args;
4286 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
4289 for (i = 0; i < spawns_length; i++) {
4290 if (!strcasecmp(spawn_name, spawns[i].name))
4294 fprintf(stderr, "spawn_custom: program %s not found\n",
4299 /* make room for expanded args */
4300 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4301 err(1, "spawn_custom: calloc real_args");
4303 /* expand spawn_args into real_args */
4304 for (i = 0; i < prog->argc; i++) {
4306 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
4307 if (!strcasecmp(ap, "$bar_border")) {
4309 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4311 err(1, "spawn_custom border color");
4312 } else if (!strcasecmp(ap, "$bar_color")) {
4314 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4316 err(1, "spawn_custom bar color");
4317 } else if (!strcasecmp(ap, "$bar_font")) {
4318 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
4320 err(1, "spawn_custom bar fonts");
4321 } else if (!strcasecmp(ap, "$bar_font_color")) {
4323 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4325 err(1, "spawn_custom color font");
4326 } else if (!strcasecmp(ap, "$color_focus")) {
4328 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4330 err(1, "spawn_custom color focus");
4331 } else if (!strcasecmp(ap, "$color_unfocus")) {
4333 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4335 err(1, "spawn_custom color unfocus");
4337 /* no match --> copy as is */
4338 if ((real_args[i] = strdup(ap)) == NULL)
4339 err(1, "spawn_custom strdup(ap)");
4341 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
4346 if ((swm_debug & SWM_D_SPAWN) != 0) {
4347 fprintf(stderr, "spawn_custom: result = ");
4348 for (i = 0; i < prog->argc; i++)
4349 fprintf(stderr, "\"%s\" ", real_args[i]);
4350 fprintf(stderr, "\n");
4353 *ret_args = real_args;
4354 return (prog->argc);
4358 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4364 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4368 spawn(r->ws->idx, &a, 1);
4370 for (i = 0; i < spawn_argc; i++)
4376 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4382 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4386 if (pipe(select_list_pipe) == -1)
4387 err(1, "pipe error");
4388 if (pipe(select_resp_pipe) == -1)
4389 err(1, "pipe error");
4391 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4392 err(1, "could not disable SIGPIPE");
4393 switch (*pid = fork()) {
4395 err(1, "cannot fork");
4398 if (dup2(select_list_pipe[0], 0) == -1)
4400 if (dup2(select_resp_pipe[1], 1) == -1)
4402 close(select_list_pipe[1]);
4403 close(select_resp_pipe[0]);
4404 spawn(r->ws->idx, &a, 0);
4406 default: /* parent */
4407 close(select_list_pipe[0]);
4408 close(select_resp_pipe[1]);
4412 for (i = 0; i < spawn_argc; i++)
4418 setspawn(struct spawn_prog *prog)
4422 if (prog == NULL || prog->name == NULL)
4426 for (i = 0; i < spawns_length; i++) {
4427 if (!strcmp(spawns[i].name, prog->name)) {
4429 if (prog->argv == NULL) {
4431 DNPRINTF(SWM_D_SPAWN,
4432 "setspawn: delete #%d %s\n",
4434 free(spawns[i].name);
4435 for (j = 0; j < spawns[i].argc; j++)
4436 free(spawns[i].argv[j]);
4437 free(spawns[i].argv);
4438 j = spawns_length - 1;
4440 spawns[i] = spawns[j];
4445 DNPRINTF(SWM_D_SPAWN,
4446 "setspawn: replace #%d %s\n",
4448 free(spawns[i].name);
4449 for (j = 0; j < spawns[i].argc; j++)
4450 free(spawns[i].argv[j]);
4451 free(spawns[i].argv);
4454 /* found case handled */
4460 if (prog->argv == NULL) {
4462 "error: setspawn: cannot find program %s", prog->name);
4467 /* not found: add */
4468 if (spawns_size == 0 || spawns == NULL) {
4470 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4471 spawns = malloc((size_t)spawns_size *
4472 sizeof(struct spawn_prog));
4473 if (spawns == NULL) {
4474 fprintf(stderr, "setspawn: malloc failed\n");
4478 } else if (spawns_length == spawns_size) {
4480 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4481 spawns = realloc(spawns, (size_t)spawns_size *
4482 sizeof(struct spawn_prog));
4483 if (spawns == NULL) {
4484 fprintf(stderr, "setspawn: realloc failed\n");
4490 if (spawns_length < spawns_size) {
4491 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4492 spawns_length, prog->name);
4493 i = spawns_length++;
4496 fprintf(stderr, "spawns array problem?\n");
4497 if (spawns == NULL) {
4498 fprintf(stderr, "spawns array is NULL!\n");
4506 setconfspawn(char *selector, char *value, int flags)
4508 struct spawn_prog *prog;
4509 char *vp, *cp, *word;
4511 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4512 if ((prog = calloc(1, sizeof *prog)) == NULL)
4513 err(1, "setconfspawn: calloc prog");
4514 prog->name = strdup(selector);
4515 if (prog->name == NULL)
4516 err(1, "setconfspawn prog->name");
4517 if ((cp = vp = strdup(value)) == NULL)
4518 err(1, "setconfspawn: strdup(value) ");
4519 while ((word = strsep(&cp, " \t")) != NULL) {
4520 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4522 cp += (long)strspn(cp, " \t");
4523 if (strlen(word) > 0) {
4525 if ((prog->argv = realloc(prog->argv,
4526 prog->argc * sizeof(char *))) == NULL)
4527 err(1, "setconfspawn: realloc");
4528 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4529 err(1, "setconfspawn: strdup");
4536 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4543 setconfspawn("term", "xterm", 0);
4544 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4545 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4546 setconfspawn("lock", "xlock", 0);
4547 setconfspawn("initscr", "initscreen.sh", 0);
4548 setconfspawn("menu", "dmenu_run"
4551 " -nf $bar_font_color"
4553 " -sf $bar_color", 0);
4554 setconfspawn("search", "dmenu"
4558 " -nf $bar_font_color"
4560 " -sf $bar_color", 0);
4561 setconfspawn("name_workspace", "dmenu"
4565 " -nf $bar_font_color"
4567 " -sf $bar_color", 0);
4571 #define SWM_MODNAME_SIZE 32
4572 #define SWM_KEY_WS "\n+ \t"
4574 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4578 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4579 if (mod == NULL || ks == NULL) {
4580 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4583 if (keystr == NULL || strlen(keystr) == 0) {
4584 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4590 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4591 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4593 cp += (long)strspn(cp, SWM_KEY_WS);
4594 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4596 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4598 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4600 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4602 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4604 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4606 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4607 *mod |= ControlMask;
4609 *ks = XStringToKeysym(name);
4610 XConvertCase(*ks, ks, &uks);
4611 if (ks == NoSymbol) {
4613 "parsekeys: invalid key %s\n",
4619 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4624 strdupsafe(char *str)
4629 return (strdup(str));
4633 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4637 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4638 keyfuncs[kfid].name, spawn_name);
4640 for (i = 0; i < keys_length; i++) {
4641 if (keys[i].mod == mod && keys[i].keysym == ks) {
4642 if (kfid == kf_invalid) {
4645 "setkeybinding: delete #%d %s\n",
4646 i, keyfuncs[keys[i].funcid].name);
4647 free(keys[i].spawn_name);
4648 j = keys_length - 1;
4652 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4655 /* found: replace */
4657 "setkeybinding: replace #%d %s %s\n",
4658 i, keyfuncs[keys[i].funcid].name,
4660 free(keys[i].spawn_name);
4662 keys[i].keysym = ks;
4663 keys[i].funcid = kfid;
4664 keys[i].spawn_name = strdupsafe(spawn_name);
4665 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4670 if (kfid == kf_invalid) {
4672 "error: setkeybinding: cannot find mod/key combination");
4673 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4676 /* not found: add */
4677 if (keys_size == 0 || keys == NULL) {
4679 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4680 keys = malloc((size_t)keys_size * sizeof(struct key));
4682 fprintf(stderr, "malloc failed\n");
4686 } else if (keys_length == keys_size) {
4688 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4689 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4691 fprintf(stderr, "realloc failed\n");
4696 if (keys_length < keys_size) {
4698 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4699 j, keyfuncs[kfid].name, spawn_name);
4701 keys[j].keysym = ks;
4702 keys[j].funcid = kfid;
4703 keys[j].spawn_name = strdupsafe(spawn_name);
4705 fprintf(stderr, "keys array problem?\n");
4707 fprintf(stderr, "keys array problem\n");
4711 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4715 setconfbinding(char *selector, char *value, int flags)
4717 enum keyfuncid kfid;
4721 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4722 if (selector == NULL) {
4723 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4724 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4726 setkeybinding(mod, ks, kfid, NULL);
4731 /* search by key function name */
4732 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4733 if (strncasecmp(selector, keyfuncs[kfid].name,
4734 SWM_FUNCNAME_LEN) == 0) {
4735 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4737 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4738 setkeybinding(mod, ks, kfid, NULL);
4744 /* search by custom spawn name */
4745 for (i = 0; i < spawns_length; i++) {
4746 if (strcasecmp(selector, spawns[i].name) == 0) {
4747 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4749 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4750 setkeybinding(mod, ks, kf_spawn_custom,
4757 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4764 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4765 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4766 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4767 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4768 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4769 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4770 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4771 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4772 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4773 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4774 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4775 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4776 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4777 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4778 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4779 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4780 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4781 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4782 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4783 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4784 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4785 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4786 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4787 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4788 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4789 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4790 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4791 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4792 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4793 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4794 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4795 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4796 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4797 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4798 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4799 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4800 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4801 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4802 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4803 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4804 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4805 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4806 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4807 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4808 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4809 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4810 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4811 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4812 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4813 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4814 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4815 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4816 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4817 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4818 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4819 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4820 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4821 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4822 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4823 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4824 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4825 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4826 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4827 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4828 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4829 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4830 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4831 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4832 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4833 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4834 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4836 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4845 /* clear all key bindings, if any */
4846 for (i = 0; i < keys_length; i++)
4847 free(keys[i].spawn_name);
4852 setkeymapping(char *selector, char *value, int flags)
4854 char keymapping_file[PATH_MAX];
4855 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4856 if (value[0] == '~')
4857 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4858 pwd->pw_dir, &value[1]);
4860 strlcpy(keymapping_file, value, sizeof keymapping_file);
4862 /* load new key bindings; if it fails, revert to default bindings */
4863 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4867 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4872 updatenumlockmask(void)
4875 XModifierKeymap *modmap;
4877 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4879 modmap = XGetModifierMapping(display);
4880 for (i = 0; i < 8; i++)
4881 for (j = 0; j < modmap->max_keypermod; j++)
4882 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4883 == XKeysymToKeycode(display, XK_Num_Lock))
4884 numlockmask = (1 << i);
4886 XFreeModifiermap(modmap);
4892 unsigned int i, j, k;
4894 unsigned int modifiers[] =
4895 { 0, LockMask, numlockmask, numlockmask | LockMask };
4897 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4898 updatenumlockmask();
4900 for (k = 0; k < ScreenCount(display); k++) {
4901 if (TAILQ_EMPTY(&screens[k].rl))
4903 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4904 for (i = 0; i < keys_length; i++) {
4905 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4906 for (j = 0; j < LENGTH(modifiers); j++)
4907 XGrabKey(display, code,
4908 keys[i].mod | modifiers[j],
4909 screens[k].root, True,
4910 GrabModeAsync, GrabModeAsync);
4916 grabbuttons(struct ws_win *win, int focused)
4919 unsigned int modifiers[] =
4920 { 0, LockMask, numlockmask, numlockmask|LockMask };
4922 updatenumlockmask();
4923 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4925 for (i = 0; i < LENGTH(buttons); i++)
4926 if (buttons[i].action == client_click)
4927 for (j = 0; j < LENGTH(modifiers); j++)
4928 XGrabButton(display, buttons[i].button,
4929 buttons[i].mask | modifiers[j],
4930 win->id, False, BUTTONMASK,
4931 GrabModeAsync, GrabModeSync, None,
4934 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4935 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4938 const char *quirkname[] = {
4939 "NONE", /* config string for "no value" */
4948 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4949 #define SWM_Q_WS "\n|+ \t"
4951 parsequirks(char *qstr, unsigned long *quirk)
4961 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4963 cp += (long)strspn(cp, SWM_Q_WS);
4964 for (i = 0; i < LENGTH(quirkname); i++) {
4965 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4966 DNPRINTF(SWM_D_QUIRK,
4967 "parsequirks: %s\n", name);
4972 *quirk |= 1 << (i-1);
4976 if (i >= LENGTH(quirkname)) {
4977 DNPRINTF(SWM_D_QUIRK,
4978 "parsequirks: invalid quirk [%s]\n", name);
4986 setquirk(const char *class, const char *name, const int quirk)
4991 for (i = 0; i < quirks_length; i++) {
4992 if (!strcmp(quirks[i].class, class) &&
4993 !strcmp(quirks[i].name, name)) {
4996 DNPRINTF(SWM_D_QUIRK,
4997 "setquirk: delete #%d %s:%s\n",
4998 i, quirks[i].class, quirks[i].name);
4999 free(quirks[i].class);
5000 free(quirks[i].name);
5001 j = quirks_length - 1;
5003 quirks[i] = quirks[j];
5007 /* found: replace */
5008 DNPRINTF(SWM_D_QUIRK,
5009 "setquirk: replace #%d %s:%s\n",
5010 i, quirks[i].class, quirks[i].name);
5011 free(quirks[i].class);
5012 free(quirks[i].name);
5013 quirks[i].class = strdup(class);
5014 quirks[i].name = strdup(name);
5015 quirks[i].quirk = quirk;
5022 "error: setquirk: cannot find class/name combination");
5025 /* not found: add */
5026 if (quirks_size == 0 || quirks == NULL) {
5028 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
5029 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
5030 if (quirks == NULL) {
5031 fprintf(stderr, "setquirk: malloc failed\n");
5035 } else if (quirks_length == quirks_size) {
5037 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
5038 quirks = realloc(quirks,
5039 (size_t)quirks_size * sizeof(struct quirk));
5040 if (quirks == NULL) {
5041 fprintf(stderr, "setquirk: realloc failed\n");
5046 if (quirks_length < quirks_size) {
5047 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
5048 j = quirks_length++;
5049 quirks[j].class = strdup(class);
5050 quirks[j].name = strdup(name);
5051 quirks[j].quirk = quirk;
5053 fprintf(stderr, "quirks array problem?\n");
5054 if (quirks == NULL) {
5055 fprintf(stderr, "quirks array problem!\n");
5062 setconfquirk(char *selector, char *value, int flags)
5064 char *cp, *class, *name;
5066 unsigned long quirks;
5067 if (selector == NULL)
5069 if ((cp = strchr(selector, ':')) == NULL)
5074 if ((retval = parsequirks(value, &quirks)) == 0)
5075 setquirk(class, name, quirks);
5082 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5083 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5084 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5085 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5086 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5087 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5088 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5089 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5090 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5091 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5092 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5093 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5094 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5097 /* conf file stuff */
5098 #define SWM_CONF_FILE "scrotwm.conf"
5100 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5101 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5102 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5103 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5104 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5105 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5106 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5107 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5108 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5112 setconfvalue(char *selector, char *value, int flags)
5117 case SWM_S_BAR_DELAY:
5118 bar_delay = atoi(value);
5120 case SWM_S_BAR_ENABLED:
5121 bar_enabled = atoi(value);
5123 case SWM_S_BAR_BORDER_WIDTH:
5124 bar_border_width = atoi(value);
5126 case SWM_S_BAR_AT_BOTTOM:
5127 bar_at_bottom = atoi(value);
5129 case SWM_S_BAR_JUSTIFY:
5130 if (!strcmp(value, "left"))
5131 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5132 else if (!strcmp(value, "center"))
5133 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5134 else if (!strcmp(value, "right"))
5135 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5137 errx(1, "invalid bar_justify");
5139 case SWM_S_STACK_ENABLED:
5140 stack_enabled = atoi(value);
5142 case SWM_S_CLOCK_ENABLED:
5143 clock_enabled = atoi(value);
5145 case SWM_S_CLOCK_FORMAT:
5146 #ifndef SWM_DENY_CLOCK_FORMAT
5148 if ((clock_format = strdup(value)) == NULL)
5149 err(1, "setconfvalue: clock_format");
5152 case SWM_S_CYCLE_EMPTY:
5153 cycle_empty = atoi(value);
5155 case SWM_S_CYCLE_VISIBLE:
5156 cycle_visible = atoi(value);
5158 case SWM_S_SS_ENABLED:
5159 ss_enabled = atoi(value);
5161 case SWM_S_TERM_WIDTH:
5162 term_width = atoi(value);
5164 case SWM_S_TITLE_CLASS_ENABLED:
5165 title_class_enabled = atoi(value);
5167 case SWM_S_WINDOW_NAME_ENABLED:
5168 window_name_enabled = atoi(value);
5170 case SWM_S_TITLE_NAME_ENABLED:
5171 title_name_enabled = atoi(value);
5173 case SWM_S_URGENT_ENABLED:
5174 urgent_enabled = atoi(value);
5176 case SWM_S_FOCUS_MODE:
5177 if (!strcmp(value, "default"))
5178 focus_mode = SWM_FOCUS_DEFAULT;
5179 else if (!strcmp(value, "follow_cursor"))
5180 focus_mode = SWM_FOCUS_FOLLOW;
5181 else if (!strcmp(value, "synergy"))
5182 focus_mode = SWM_FOCUS_SYNERGY;
5184 errx(1, "focus_mode");
5186 case SWM_S_DISABLE_BORDER:
5187 disable_border = atoi(value);
5189 case SWM_S_BORDER_WIDTH:
5190 border_width = atoi(value);
5192 case SWM_S_BAR_FONT:
5194 if ((bar_fonts[0] = strdup(value)) == NULL)
5195 err(1, "setconfvalue: bar_font");
5197 case SWM_S_BAR_ACTION:
5199 if ((bar_argv[0] = strdup(value)) == NULL)
5200 err(1, "setconfvalue: bar_action");
5202 case SWM_S_SPAWN_TERM:
5203 free(spawn_term[0]);
5204 if ((spawn_term[0] = strdup(value)) == NULL)
5205 err(1, "setconfvalue: spawn_term");
5209 case SWM_S_DIALOG_RATIO:
5210 dialog_ratio = atof(value);
5211 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5214 case SWM_S_VERBOSE_LAYOUT:
5215 verbose_layout = atoi(value);
5216 for (i = 0; layouts[i].l_stack != NULL; i++) {
5218 layouts[i].l_string = fancy_stacker;
5220 layouts[i].l_string = plain_stacker;
5230 setconfmodkey(char *selector, char *value, int flags)
5232 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5233 update_modkey(Mod1Mask);
5234 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5235 update_modkey(Mod2Mask);
5236 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5237 update_modkey(Mod3Mask);
5238 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5239 update_modkey(Mod4Mask);
5246 setconfcolor(char *selector, char *value, int flags)
5248 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5253 setconfregion(char *selector, char *value, int flags)
5255 custom_region(value);
5260 setautorun(char *selector, char *value, int flags)
5270 if (getenv("SWM_STARTED"))
5274 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5275 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5277 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5278 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5281 * This is a little intricate
5283 * If the pid already exists we simply reuse it because it means it was
5284 * used before AND not claimed by manage_window. We get away with
5285 * altering it in the parent after INSERT because this can not be a race
5288 while ((ap = strsep(&sp, " \t")) != NULL) {
5291 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5293 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5294 err(1, "setautorun: realloc");
5295 a.argv[argc - 1] = ap;
5298 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5299 err(1, "setautorun: realloc");
5300 a.argv[argc] = NULL;
5302 if ((pid = fork()) == 0) {
5303 spawn(ws_id, &a, 1);
5312 p = calloc(1, sizeof *p);
5315 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5325 setlayout(char *selector, char *value, int flags)
5327 int ws_id, i, x, mg, ma, si, raise;
5328 int st = SWM_V_STACK;
5330 struct workspace *ws;
5332 if (getenv("SWM_STARTED"))
5336 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5337 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5338 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5339 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5342 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5343 errx(1, "layout: invalid workspace %d", ws_id + 1);
5345 if (!strcasecmp(s, "vertical"))
5347 else if (!strcasecmp(s, "horizontal"))
5349 else if (!strcasecmp(s, "fullscreen"))
5352 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5353 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5356 for (i = 0; i < ScreenCount(display); i++) {
5357 ws = (struct workspace *)&screens[i].ws;
5358 ws[ws_id].cur_layout = &layouts[st];
5360 ws[ws_id].always_raise = raise;
5361 if (st == SWM_MAX_STACK)
5365 for (x = 0; x < abs(mg); x++) {
5366 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5367 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5368 SWM_ARG_ID_MASTERSHRINK);
5372 for (x = 0; x < abs(ma); x++) {
5373 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5374 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5375 SWM_ARG_ID_MASTERDEL);
5379 for (x = 0; x < abs(si); x++) {
5380 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5381 si >= 0 ? SWM_ARG_ID_STACKINC :
5382 SWM_ARG_ID_STACKDEC);
5390 /* config options */
5391 struct config_option {
5393 int (*func)(char*, char*, int);
5396 struct config_option configopt[] = {
5397 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5398 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5399 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5400 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5401 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5402 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5403 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5404 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5405 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5406 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5407 { "keyboard_mapping", setkeymapping, 0 },
5408 { "bind", setconfbinding, 0 },
5409 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5410 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5411 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5412 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5413 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5414 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5415 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5416 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5417 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5418 { "modkey", setconfmodkey, 0 },
5419 { "program", setconfspawn, 0 },
5420 { "quirk", setconfquirk, 0 },
5421 { "region", setconfregion, 0 },
5422 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5423 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5424 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5425 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5426 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5427 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5428 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5429 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5430 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5431 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5432 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5433 { "autorun", setautorun, 0 },
5434 { "layout", setlayout, 0 },
5439 conf_load(char *filename, int keymapping)
5442 char *line, *cp, *optsub, *optval;
5443 size_t linelen, lineno = 0;
5444 int wordlen, i, optind;
5445 struct config_option *opt;
5447 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
5449 if (filename == NULL) {
5450 fprintf(stderr, "conf_load: no filename\n");
5453 if ((config = fopen(filename, "r")) == NULL) {
5454 warn("conf_load: fopen: %s", filename);
5458 while (!feof(config)) {
5459 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5462 err(1, "%s", filename);
5467 cp += strspn(cp, " \t\n"); /* eat whitespace */
5468 if (cp[0] == '\0') {
5473 /* get config option */
5474 wordlen = strcspn(cp, "=[ \t\n");
5476 warnx("%s: line %zd: no option found",
5481 for (i = 0; i < LENGTH(configopt); i++) {
5482 opt = &configopt[i];
5483 if (!strncasecmp(cp, opt->optname, wordlen) &&
5484 strlen(opt->optname) == wordlen) {
5490 warnx("%s: line %zd: unknown option %.*s",
5491 filename, lineno, wordlen, cp);
5494 if (keymapping && strcmp(opt->optname, "bind")) {
5495 warnx("%s: line %zd: invalid option %.*s",
5496 filename, lineno, wordlen, cp);
5500 cp += strspn(cp, " \t\n"); /* eat whitespace */
5501 /* get [selector] if any */
5505 wordlen = strcspn(cp, "]");
5508 warnx("%s: line %zd: syntax error",
5513 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5515 warnx("%s: line %zd: unable to allocate"
5516 "memory for selector", filename,
5522 cp += strspn(cp, "] \t\n"); /* eat trailing */
5524 cp += strspn(cp, "= \t\n"); /* eat trailing */
5526 optval = strdup(cp);
5527 /* call function to deal with it all */
5528 if (configopt[optind].func(optsub, optval,
5529 configopt[optind].funcflags) != 0) {
5530 fprintf(stderr, "%s line %zd: %s\n",
5531 filename, lineno, line);
5532 errx(1, "%s: line %zd: invalid data for %s",
5533 filename, lineno, configopt[optind].optname);
5541 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5553 set_child_transient(struct ws_win *win, Window *trans)
5555 struct ws_win *parent, *w;
5556 XWMHints *wmh = NULL;
5557 struct swm_region *r;
5558 struct workspace *ws;
5560 parent = find_window(win->transient);
5562 parent->child_trans = win;
5564 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5565 " for %lu trans %lu\n", win->id, win->transient);
5567 if (win->hints == NULL) {
5568 fprintf(stderr, "no hints for %lu\n", win->id);
5572 r = root_to_region(win->wa.root);
5574 /* parent doen't exist in our window list */
5575 TAILQ_FOREACH(w, &ws->winlist, entry) {
5579 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5580 fprintf(stderr, "can't get hints for %lu\n",
5585 if (win->hints->window_group != wmh->window_group)
5588 w->child_trans = win;
5589 win->transient = w->id;
5591 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5592 "transient to %lu\n", win->transient);
5602 window_get_pid(Window win)
5604 Atom actual_type_return;
5605 int actual_format_return = 0;
5606 unsigned long nitems_return = 0;
5607 unsigned long bytes_after_return = 0;
5611 unsigned char *prop = NULL;
5613 if (XGetWindowProperty(display, win,
5614 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5615 XA_CARDINAL, &actual_type_return, &actual_format_return,
5616 &nitems_return, &bytes_after_return,
5617 (unsigned char**)(void*)&pid) != Success)
5619 if (actual_type_return != XA_CARDINAL)
5630 if (XGetWindowProperty(display, win,
5631 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5632 XA_STRING, &actual_type_return, &actual_format_return,
5633 &nitems_return, &bytes_after_return, &prop) != Success)
5635 if (actual_type_return != XA_STRING)
5640 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5641 /* ignore error because strtonum returns 0 anyway */
5648 manage_window(Window id)
5651 struct workspace *ws;
5652 struct ws_win *win, *ww;
5653 int format, i, ws_idx, n, border_me = 0;
5654 unsigned long nitems, bytes;
5655 Atom ws_idx_atom = 0, type;
5656 Atom *prot = NULL, *pp;
5657 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5658 struct swm_region *r;
5664 if ((win = find_window(id)) != NULL)
5665 return (win); /* already being managed */
5667 /* see if we are on the unmanaged list */
5668 if ((win = find_unmanaged_window(id)) != NULL) {
5669 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5671 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5672 if (win->transient) {
5673 set_child_transient(win, &trans);
5674 } if (trans && (ww = find_window(trans)))
5675 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5677 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5678 ewmh_update_actions(win);
5682 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5683 err(1, "calloc: failed to allocate memory for new window");
5687 /* see if we need to override the workspace */
5688 p = find_pid(window_get_pid(id));
5690 /* Get all the window data in one shot */
5691 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5693 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5694 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5696 XGetWindowAttributes(display, id, &win->wa);
5697 XGetWMNormalHints(display, id, &win->sh, &mask);
5698 win->hints = XGetWMHints(display, id);
5699 XGetTransientForHint(display, id, &trans);
5701 win->transient = trans;
5702 set_child_transient(win, &trans);
5703 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5704 win->id, win->transient);
5707 /* get supported protocols */
5708 if (XGetWMProtocols(display, id, &prot, &n)) {
5709 for (i = 0, pp = prot; i < n; i++, pp++) {
5710 if (*pp == takefocus)
5711 win->take_focus = 1;
5713 win->can_delete = 1;
5719 win->iconic = get_iconic(win);
5722 * Figure out where to put the window. If it was previously assigned to
5723 * a workspace (either by spawn() or manually moving), and isn't
5724 * transient, * put it in the same workspace
5726 r = root_to_region(win->wa.root);
5728 ws = &r->s->ws[p->ws];
5729 TAILQ_REMOVE(&pidlist, p, entry);
5732 } else if (prop && win->transient == 0) {
5733 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5734 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5736 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5739 ws = &r->s->ws[ws_idx];
5742 /* this should launch transients in the same ws as parent */
5744 if ((ww = find_window(trans)) != NULL)
5751 "fix this bug mcbride\n");
5756 /* set up the window layout */
5759 win->s = r->s; /* this never changes */
5760 if (trans && (ww = find_window(trans)))
5761 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5763 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5765 win->g.w = win->wa.width;
5766 win->g.h = win->wa.height;
5767 win->g.x = win->wa.x;
5768 win->g.y = win->wa.y;
5769 win->g_floatvalid = 0;
5770 win->floatmaxed = 0;
5771 win->ewmh_flags = 0;
5773 /* Set window properties so we can remember this after reincarnation */
5774 if (ws_idx_atom && prop == NULL &&
5775 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5777 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5779 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5780 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5785 ewmh_autoquirk(win);
5787 if (XGetClassHint(display, win->id, &win->ch)) {
5788 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5789 win->ch.res_class, win->ch.res_name);
5791 /* java is retarded so treat it special */
5792 if (strstr(win->ch.res_name, "sun-awt")) {
5797 for (i = 0; i < quirks_length; i++){
5798 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5799 !strcmp(win->ch.res_name, quirks[i].name)) {
5800 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5801 win->ch.res_class, win->ch.res_name);
5802 if (quirks[i].quirk & SWM_Q_FLOAT) {
5806 win->quirks = quirks[i].quirk;
5811 /* alter window position if quirky */
5812 if (win->quirks & SWM_Q_ANYWHERE) {
5813 win->manual = 1; /* don't center the quirky windows */
5814 bzero(&wc, sizeof wc);
5816 if (bar_enabled && win->g.y < bar_height) {
5817 win->g.y = wc.y = bar_height;
5820 if (win->g.w + win->g.x > WIDTH(r)) {
5821 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5827 /* Reset font sizes (the bruteforce way; no default keybinding). */
5828 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5829 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5830 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5831 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5832 fake_keypress(win, XK_KP_Add, ShiftMask);
5835 ewmh_get_win_state(win);
5836 ewmh_update_actions(win);
5837 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5841 bzero(&wc, sizeof wc);
5842 wc.border_width = border_width;
5843 mask = CWBorderWidth;
5844 XConfigureWindow(display, win->id, mask, &wc);
5847 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5848 PropertyChangeMask | StructureNotifyMask);
5850 /* floaters need to be mapped if they are in the current workspace */
5851 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5852 XMapRaised(display, win->id);
5858 free_window(struct ws_win *win)
5860 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5865 /* needed for restart wm */
5866 set_win_state(win, WithdrawnState);
5868 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5870 if (win->ch.res_class)
5871 XFree(win->ch.res_class);
5872 if (win->ch.res_name)
5873 XFree(win->ch.res_name);
5878 memset(win, 0xff, sizeof *win); /* XXX kill later */
5884 unmanage_window(struct ws_win *win)
5886 struct ws_win *parent;
5891 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5893 if (win->transient) {
5894 parent = find_window(win->transient);
5896 parent->child_trans = NULL;
5899 /* focus on root just in case */
5900 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5904 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5905 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5911 focus_magic(struct ws_win *win)
5913 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5918 if (win->child_trans) {
5919 /* win = parent & has a transient so focus on that */
5921 focus_win(win->child_trans);
5922 if (win->child_trans->take_focus)
5923 client_msg(win, takefocus);
5925 /* make sure transient hasn't dissapeared */
5926 if (validate_win(win->child_trans) == 0) {
5927 focus_win(win->child_trans);
5928 if (win->child_trans->take_focus)
5929 client_msg(win->child_trans, takefocus);
5931 win->child_trans = NULL;
5933 if (win->take_focus)
5934 client_msg(win, takefocus);
5940 if (win->take_focus)
5941 client_msg(win, takefocus);
5948 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5956 XKeyEvent *ev = &e->xkey;
5958 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5960 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5961 for (i = 0; i < keys_length; i++)
5962 if (keysym == keys[i].keysym
5963 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5964 && keyfuncs[keys[i].funcid].func) {
5965 if (keys[i].funcid == kf_spawn_custom)
5967 root_to_region(ev->root),
5968 &(keyfuncs[keys[i].funcid].args),
5972 keyfuncs[keys[i].funcid].func(
5973 root_to_region(ev->root),
5974 &(keyfuncs[keys[i].funcid].args)
5980 buttonpress(XEvent *e)
5984 XButtonPressedEvent *ev = &e->xbutton;
5986 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5988 if ((win = find_window(ev->window)) == NULL)
5992 action = client_click;
5994 for (i = 0; i < LENGTH(buttons); i++)
5995 if (action == buttons[i].action && buttons[i].func &&
5996 buttons[i].button == ev->button &&
5997 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5998 buttons[i].func(win, &buttons[i].args);
6002 configurerequest(XEvent *e)
6004 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6009 if ((win = find_window(ev->window)) == NULL)
6010 if ((win = find_unmanaged_window(ev->window)) == NULL)
6014 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
6016 bzero(&wc, sizeof wc);
6019 wc.width = ev->width;
6020 wc.height = ev->height;
6021 wc.border_width = ev->border_width;
6022 wc.sibling = ev->above;
6023 wc.stack_mode = ev->detail;
6024 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6026 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
6028 config_win(win, ev);
6033 configurenotify(XEvent *e)
6038 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
6039 e->xconfigure.window);
6041 win = find_window(e->xconfigure.window);
6043 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6047 if (focus_mode == SWM_FOCUS_DEFAULT)
6048 drain_enter_notify();
6053 destroynotify(XEvent *e)
6056 XDestroyWindowEvent *ev = &e->xdestroywindow;
6058 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
6060 if ((win = find_window(ev->window)) == NULL) {
6061 if ((win = find_unmanaged_window(ev->window)) == NULL)
6067 /* make sure we focus on something */
6070 unmanage_window(win);
6072 if (focus_mode == SWM_FOCUS_DEFAULT)
6073 drain_enter_notify();
6078 enternotify(XEvent *e)
6080 XCrossingEvent *ev = &e->xcrossing;
6085 Window focus_return;
6086 int revert_to_return;
6088 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
6089 "%lu subwindow %lu same_screen %d focus %d state %d\n",
6090 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
6091 ev->same_screen, ev->focus, ev->state);
6093 if (ev->mode != NotifyNormal) {
6094 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: generated by cursor grab.\n");
6098 switch (focus_mode) {
6099 case SWM_FOCUS_DEFAULT:
6101 case SWM_FOCUS_FOLLOW:
6103 case SWM_FOCUS_SYNERGY:
6106 * all these checks need to be in this order because the
6107 * XCheckTypedWindowEvent relies on weeding out the previous events
6109 * making this code an option would enable a follow mouse for focus
6114 * state is set when we are switching workspaces and focus is set when
6115 * the window or a subwindow already has focus (occurs during restart).
6117 * Only honor the focus flag if last_focus_event is not FocusOut,
6118 * this allows scrotwm to continue to control focus when another
6119 * program is also playing with it.
6121 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6122 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6127 * happens when a window is created or destroyed and the border
6128 * crosses the mouse pointer and when switching ws
6130 * we need the subwindow test to see if we came from root in order
6131 * to give focus to floaters
6133 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6134 ev->subwindow == 0) {
6135 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6139 /* this window already has focus */
6140 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6141 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6145 /* this window is being deleted or moved to another ws */
6146 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6148 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6149 XPutBackEvent(display, &cne);
6153 if ((win = find_window(ev->window)) == NULL) {
6154 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6159 * In fullstack kill all enters unless they come from a different ws
6160 * (i.e. another region) or focus has been grabbed externally.
6162 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6163 last_focus_event != FocusOut) {
6164 XGetInputFocus(display, &focus_return, &revert_to_return);
6165 if ((w = find_window(focus_return)) == NULL ||
6167 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6175 if ((win = find_window(ev->window)) == NULL) {
6176 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6181 * if we have more enternotifies let them handle it in due time
6183 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6184 DNPRINTF(SWM_D_EVENT,
6185 "ignoring enternotify: got more enternotify\n");
6186 XPutBackEvent(display, &cne);
6193 /* lets us use one switch statement for arbitrary mode/detail combinations */
6194 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6197 focusevent(XEvent *e)
6201 u_int32_t mode_detail;
6202 XFocusChangeEvent *ev = &e->xfocus;
6204 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
6205 ev->type == FocusIn ? "entering" : "leaving",
6206 ev->window, ev->mode, ev->detail);
6208 if (last_focus_event == ev->type) {
6209 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6213 last_focus_event = ev->type;
6214 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6216 switch (mode_detail) {
6217 /* synergy client focus operations */
6218 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6219 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6221 /* synergy server focus operations */
6222 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6224 /* Entering applications like rdesktop that mangle the pointer */
6225 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6227 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6228 XSetWindowBorder(display, win->id,
6229 win->ws->r->s->c[ev->type == FocusIn ?
6230 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6233 fprintf(stderr, "ignoring focusevent\n");
6234 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6241 mapnotify(XEvent *e)
6244 XMapEvent *ev = &e->xmap;
6246 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
6248 win = manage_window(ev->window);
6250 set_win_state(win, NormalState);
6254 mappingnotify(XEvent *e)
6256 XMappingEvent *ev = &e->xmapping;
6258 XRefreshKeyboardMapping(ev);
6259 if (ev->request == MappingKeyboard)
6264 maprequest(XEvent *e)
6267 struct swm_region *r;
6268 XWindowAttributes wa;
6269 XMapRequestEvent *ev = &e->xmaprequest;
6271 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
6272 e->xmaprequest.window);
6274 if (!XGetWindowAttributes(display, ev->window, &wa))
6276 if (wa.override_redirect)
6279 win = manage_window(e->xmaprequest.window);
6281 return; /* can't happen */
6285 /* make new win focused */
6286 r = root_to_region(win->wa.root);
6287 if (win->ws == r->ws)
6292 propertynotify(XEvent *e)
6295 XPropertyEvent *ev = &e->xproperty;
6297 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu atom:%s\n",
6298 ev->window, XGetAtomName(display, ev->atom));
6300 win = find_window(ev->window);
6304 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6305 update_iconic(win, 0);
6306 XMapRaised(display, win->id);
6314 case XA_WM_NORMAL_HINTS:
6316 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6317 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6318 if (win->sh.flags & PMinSize) {
6319 win->g.w = win->sh.min_width;
6320 win->g.h = win->sh.min_height;
6321 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
6323 XMoveResizeWindow(display, win->id,
6324 win->g.x, win->g.y, win->g.w, win->g.h);
6327 if (title_name_enabled || title_class_enabled)
6331 if (window_name_enabled)
6340 unmapnotify(XEvent *e)
6344 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
6346 /* determine if we need to help unmanage this window */
6347 win = find_window(e->xunmap.window);
6351 if (getstate(e->xunmap.window) == NormalState) {
6352 unmanage_window(win);
6355 /* giant hack for apps that don't destroy transient windows */
6356 /* eat a bunch of events to prevent remanaging the window */
6358 while (XCheckWindowEvent(display, e->xunmap.window,
6359 EnterWindowMask, &cne))
6361 while (XCheckWindowEvent(display, e->xunmap.window,
6362 StructureNotifyMask, &cne))
6364 while (XCheckWindowEvent(display, e->xunmap.window,
6365 SubstructureNotifyMask, &cne))
6367 /* resend unmap because we ated it */
6368 XUnmapWindow(display, e->xunmap.window);
6371 if (focus_mode == SWM_FOCUS_DEFAULT)
6372 drain_enter_notify();
6376 visibilitynotify(XEvent *e)
6379 struct swm_region *r;
6381 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
6382 e->xvisibility.window);
6383 if (e->xvisibility.state == VisibilityUnobscured)
6384 for (i = 0; i < ScreenCount(display); i++)
6385 TAILQ_FOREACH(r, &screens[i].rl, entry)
6386 if (e->xvisibility.window == r->bar_window)
6391 clientmessage(XEvent *e)
6393 XClientMessageEvent *ev;
6398 win = find_window(ev->window);
6402 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
6403 ev->window, ev->message_type);
6405 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6406 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
6409 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6410 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
6411 if (win->can_delete)
6412 client_msg(win, adelete);
6414 XKillClient(display, win->id);
6416 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6417 DNPRINTF(SWM_D_EVENT,
6418 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
6419 if (win->floating) {
6420 if (ev->data.l[0] & (1<<8)) /* x */
6421 win->g.x = ev->data.l[1];
6422 if (ev->data.l[0] & (1<<9)) /* y */
6423 win->g.y = ev->data.l[2];
6424 if (ev->data.l[0] & (1<<10)) /* width */
6425 win->g.w = ev->data.l[3];
6426 if (ev->data.l[0] & (1<<11)) /* height */
6427 win->g.h = ev->data.l[4];
6430 /* TODO: Change stack sizes */
6432 config_win(win, NULL);
6434 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6435 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
6436 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6438 ewmh_update_win_state(win, ev->data.l[2],
6446 xerror_start(Display *d, XErrorEvent *ee)
6453 xerror(Display *d, XErrorEvent *ee)
6455 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6463 xerrorxlib = XSetErrorHandler(xerror_start);
6465 /* this causes an error if some other window manager is running */
6466 XSelectInput(display, DefaultRootWindow(display),
6467 SubstructureRedirectMask);
6468 XSync(display, False);
6472 XSetErrorHandler(xerror);
6473 XSync(display, False);
6478 new_region(struct swm_screen *s, int x, int y, int w, int h)
6480 struct swm_region *r, *n;
6481 struct workspace *ws = NULL;
6484 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6485 s->idx, w, h, x, y);
6487 /* remove any conflicting regions */
6488 n = TAILQ_FIRST(&s->rl);
6491 n = TAILQ_NEXT(r, entry);
6492 if (X(r) < (x + w) &&
6493 (X(r) + WIDTH(r)) > x &&
6495 (Y(r) + HEIGHT(r)) > y) {
6496 if (r->ws->r != NULL)
6497 r->ws->old_r = r->ws->r;
6499 XDestroyWindow(display, r->bar_window);
6500 TAILQ_REMOVE(&s->rl, r, entry);
6501 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6505 /* search old regions for one to reuse */
6507 /* size + location match */
6508 TAILQ_FOREACH(r, &s->orl, entry)
6509 if (X(r) == x && Y(r) == y &&
6510 HEIGHT(r) == h && WIDTH(r) == w)
6514 TAILQ_FOREACH(r, &s->orl, entry)
6515 if (HEIGHT(r) == h && WIDTH(r) == w)
6519 TAILQ_REMOVE(&s->orl, r, entry);
6520 /* try to use old region's workspace */
6521 if (r->ws->r == NULL)
6524 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6525 err(1, "calloc: failed to allocate memory for screen");
6527 /* if we don't have a workspace already, find one */
6529 for (i = 0; i < SWM_WS_MAX; i++)
6530 if (s->ws[i].r == NULL) {
6537 errx(1, "no free workspaces");
6548 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6554 #ifdef SWM_XRR_HAS_CRTC
6556 XRRScreenResources *sr;
6559 #endif /* SWM_XRR_HAS_CRTC */
6560 struct swm_region *r;
6563 if (i >= ScreenCount(display))
6564 errx(1, "invalid screen");
6566 /* remove any old regions */
6567 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6568 r->ws->old_r = r->ws->r = NULL;
6569 XDestroyWindow(display, r->bar_window);
6570 TAILQ_REMOVE(&screens[i].rl, r, entry);
6571 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6575 /* map virtual screens onto physical screens */
6576 #ifdef SWM_XRR_HAS_CRTC
6577 if (xrandr_support) {
6578 sr = XRRGetScreenResources(display, screens[i].root);
6580 new_region(&screens[i], 0, 0,
6581 DisplayWidth(display, i),
6582 DisplayHeight(display, i));
6586 for (c = 0, ci = NULL; c < ncrtc; c++) {
6587 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6588 if (ci->noutput == 0)
6591 if (ci != NULL && ci->mode == None)
6592 new_region(&screens[i], 0, 0,
6593 DisplayWidth(display, i),
6594 DisplayHeight(display, i));
6596 new_region(&screens[i],
6597 ci->x, ci->y, ci->width, ci->height);
6600 XRRFreeCrtcInfo(ci);
6601 XRRFreeScreenResources(sr);
6603 #endif /* SWM_XRR_HAS_CRTC */
6605 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6606 DisplayHeight(display, i));
6611 screenchange(XEvent *e) {
6612 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6613 struct swm_region *r;
6616 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6618 if (!XRRUpdateConfiguration(e))
6621 /* silly event doesn't include the screen index */
6622 for (i = 0; i < ScreenCount(display); i++)
6623 if (screens[i].root == xe->root)
6625 if (i >= ScreenCount(display))
6626 errx(1, "screenchange: screen not found");
6628 /* brute force for now, just re-enumerate the regions */
6631 /* add bars to all regions */
6632 for (i = 0; i < ScreenCount(display); i++)
6633 TAILQ_FOREACH(r, &screens[i].rl, entry)
6636 if (focus_mode == SWM_FOCUS_DEFAULT)
6637 drain_enter_notify();
6643 Window d1, d2, *wins = NULL;
6644 XWindowAttributes wa;
6649 for (i = 0; i < ScreenCount(display); i++) {
6650 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6653 /* attach windows to a region */
6654 /* normal windows */
6655 for (j = 0; j < no; j++) {
6656 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6657 wa.override_redirect ||
6658 XGetTransientForHint(display, wins[j], &d1))
6661 state = getstate(wins[j]);
6662 manage = state == IconicState;
6663 if (wa.map_state == IsViewable || manage)
6664 manage_window(wins[j]);
6666 /* transient windows */
6667 for (j = 0; j < no; j++) {
6668 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6669 wa.override_redirect)
6672 state = getstate(wins[j]);
6673 manage = state == IconicState;
6674 if (XGetTransientForHint(display, wins[j], &d1) &&
6676 manage_window(wins[j]);
6689 int errorbase, major, minor;
6690 struct workspace *ws;
6692 if ((screens = calloc(ScreenCount(display),
6693 sizeof(struct swm_screen))) == NULL)
6694 err(1, "calloc: screens");
6696 /* initial Xrandr setup */
6697 xrandr_support = XRRQueryExtension(display,
6698 &xrandr_eventbase, &errorbase);
6700 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6703 /* map physical screens */
6704 for (i = 0; i < ScreenCount(display); i++) {
6705 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6707 TAILQ_INIT(&screens[i].rl);
6708 TAILQ_INIT(&screens[i].orl);
6709 screens[i].root = RootWindow(display, i);
6711 /* set default colors */
6712 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6713 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6714 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6715 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6716 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6718 /* set default cursor */
6719 XDefineCursor(display, screens[i].root,
6720 XCreateFontCursor(display, XC_left_ptr));
6722 /* init all workspaces */
6723 /* XXX these should be dynamically allocated too */
6724 for (j = 0; j < SWM_WS_MAX; j++) {
6725 ws = &screens[i].ws[j];
6731 TAILQ_INIT(&ws->winlist);
6732 TAILQ_INIT(&ws->unmanagedlist);
6734 for (k = 0; layouts[k].l_stack != NULL; k++)
6735 if (layouts[k].l_config != NULL)
6736 layouts[k].l_config(ws,
6737 SWM_ARG_ID_STACKINIT);
6738 ws->cur_layout = &layouts[0];
6739 ws->cur_layout->l_string(ws);
6745 XRRSelectInput(display, screens[i].root,
6746 RRScreenChangeNotifyMask);
6753 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6755 err(1, "setup_globals: strdup");
6756 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6758 err(1, "setup_globals: strdup");
6759 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6761 err(1, "setup_globals: strdup");
6762 if ((spawn_term[0] = strdup("xterm")) == NULL)
6763 err(1, "setup_globals: strdup");
6764 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6772 Atom netwmcheck, netwmname, utf8_string;
6775 /* work around sun jdk bugs, code from wmname */
6776 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6777 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6778 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6779 for (i = 0; i < ScreenCount(display); i++) {
6780 root = screens[i].root;
6781 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6782 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6783 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6785 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6786 PropModeReplace, (unsigned char *)&win,1);
6787 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6788 PropModeReplace, (unsigned char *)&win,1);
6789 XChangeProperty(display, win, netwmname, utf8_string, 8,
6790 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6795 main(int argc, char *argv[])
6797 struct swm_region *r, *rr;
6798 struct ws_win *winfocus = NULL;
6801 char conf[PATH_MAX], *cfile = NULL;
6806 struct sigaction sact;
6809 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6810 SCROTWM_VERSION, buildstr);
6811 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6813 warnx("no locale support");
6815 if (!(display = XOpenDisplay(0)))
6816 errx(1, "can not open display");
6819 errx(1, "other wm running");
6821 /* handle some signals */
6822 bzero(&sact, sizeof(sact));
6823 sigemptyset(&sact.sa_mask);
6825 sact.sa_handler = sighdlr;
6826 sigaction(SIGINT, &sact, NULL);
6827 sigaction(SIGQUIT, &sact, NULL);
6828 sigaction(SIGTERM, &sact, NULL);
6829 sigaction(SIGHUP, &sact, NULL);
6831 sact.sa_handler = sighdlr;
6832 sact.sa_flags = SA_NOCLDSTOP;
6833 sigaction(SIGCHLD, &sact, NULL);
6835 astate = XInternAtom(display, "WM_STATE", False);
6836 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6837 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6838 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6839 a_wmname = XInternAtom(display, "WM_NAME", False);
6840 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6841 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6842 a_string = XInternAtom(display, "STRING", False);
6843 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6845 /* look for local and global conf file */
6846 pwd = getpwuid(getuid());
6848 errx(1, "invalid user %d", getuid());
6857 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6858 if (stat(conf, &sb) != -1) {
6859 if (S_ISREG(sb.st_mode))
6862 /* try global conf file */
6863 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6864 if (!stat(conf, &sb))
6865 if (S_ISREG(sb.st_mode))
6869 conf_load(cfile, SWM_CONF_DEFAULT);
6872 /* set some values to work around bad programs */
6874 /* grab existing windows (before we build the bars) */
6877 if (getenv("SWM_STARTED") == NULL)
6878 setenv("SWM_STARTED", "YES", 1);
6880 /* setup all bars */
6881 for (i = 0; i < ScreenCount(display); i++)
6882 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6883 if (winfocus == NULL)
6884 winfocus = TAILQ_FIRST(&r->ws->winlist);
6892 if (focus_mode == SWM_FOCUS_DEFAULT)
6893 drain_enter_notify();
6895 xfd = ConnectionNumber(display);
6897 while (XPending(display)) {
6898 XNextEvent(display, &e);
6901 if (e.type < LASTEvent) {
6903 if (handler[e.type])
6904 handler[e.type](&e);
6906 DNPRINTF(SWM_D_EVENT,
6907 "win: %lu unknown event: %d\n",
6908 e.xany.window, e.type);
6910 switch (e.type - xrandr_eventbase) {
6911 case RRScreenChangeNotify:
6915 DNPRINTF(SWM_D_EVENT,
6916 "win: %lu unknown xrandr event: "
6917 "%d\n", e.xany.window, e.type);
6923 /* if we are being restarted go focus on first window */
6925 rr = winfocus->ws->r;
6927 /* not a visible window */
6931 /* move pointer to first screen if multi screen */
6932 if (ScreenCount(display) > 1 || outputs > 1)
6933 XWarpPointer(display, None, rr->s[0].root,
6934 0, 0, 0, 0, rr->g.x,
6935 rr->g.y + (bar_enabled ? bar_height : 0));
6937 a.id = SWM_ARG_ID_FOCUSCUR;
6947 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6949 DNPRINTF(SWM_D_MISC, "select failed");
6950 if (restart_wm == 1)
6951 restart(NULL, NULL);
6952 if (search_resp == 1)
6964 XFreeGC(display, bar_gc);
6965 XCloseDisplay(display);