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
127 #define SWM_D_BAR 0x4000
129 u_int32_t swm_debug = 0
147 #define DPRINTF(x...)
148 #define DNPRINTF(n,x...)
151 #define LENGTH(x) (sizeof x / sizeof x[0])
152 #define MODKEY Mod1Mask
153 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
154 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
155 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
156 #define SWM_PROPLEN (16)
157 #define SWM_FUNCNAME_LEN (32)
158 #define SWM_KEYS_LEN (255)
159 #define SWM_QUIRK_LEN (64)
160 #define X(r) (r)->g.x
161 #define Y(r) (r)->g.y
162 #define WIDTH(r) (r)->g.w
163 #define HEIGHT(r) (r)->g.h
164 #define SH_MIN(w) (w)->sh_mask & PMinSize
165 #define SH_MIN_W(w) (w)->sh.min_width
166 #define SH_MIN_H(w) (w)->sh.min_height
167 #define SH_MAX(w) (w)->sh_mask & PMaxSize
168 #define SH_MAX_W(w) (w)->sh.max_width
169 #define SH_MAX_H(w) (w)->sh.max_height
170 #define SH_INC(w) (w)->sh_mask & PResizeInc
171 #define SH_INC_W(w) (w)->sh.width_inc
172 #define SH_INC_H(w) (w)->sh.height_inc
173 #define SWM_MAX_FONT_STEPS (3)
174 #define WINID(w) ((w) ? (w)->id : 0)
175 #define YESNO(x) ((x) ? "yes" : "no")
177 #define SWM_FOCUS_DEFAULT (0)
178 #define SWM_FOCUS_SYNERGY (1)
179 #define SWM_FOCUS_FOLLOW (2)
181 #define SWM_CONF_DEFAULT (0)
182 #define SWM_CONF_KEYMAPPING (1)
185 #define SWM_LIB "/usr/local/lib/libswmhack.so"
198 volatile sig_atomic_t running = 1;
199 volatile sig_atomic_t restart_wm = 0;
201 int last_focus_event = FocusOut;
202 int (*xerrorxlib)(Display *, XErrorEvent *);
206 int xrandr_eventbase;
207 unsigned int numlockmask = 0;
211 int cycle_visible = 0;
213 int font_adjusted = 0;
214 unsigned int mod_key = MODKEY;
217 struct swm_region *search_r;
218 int select_list_pipe[2];
219 int select_resp_pipe[2];
221 volatile sig_atomic_t search_resp;
222 int search_resp_action;
224 struct search_window {
225 TAILQ_ENTRY(search_window) entry;
231 TAILQ_HEAD(search_winlist, search_window);
233 struct search_winlist search_wl;
238 SWM_SEARCH_UNICONIFY,
239 SWM_SEARCH_NAME_WORKSPACE,
240 SWM_SEARCH_SEARCH_WORKSPACE,
241 SWM_SEARCH_SEARCH_WINDOW
245 double dialog_ratio = 0.6;
247 #define SWM_BAR_MAX (256)
248 #define SWM_BAR_JUSTIFY_LEFT (0)
249 #define SWM_BAR_JUSTIFY_CENTER (1)
250 #define SWM_BAR_JUSTIFY_RIGHT (2)
251 #define SWM_BAR_OFFSET (4)
252 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
253 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
254 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
255 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
257 #ifdef X_HAVE_UTF8_STRING
258 #define DRAWSTRING(x...) Xutf8DrawString(x)
260 #define DRAWSTRING(x...) XmbDrawString(x)
263 char *bar_argv[] = { NULL, NULL };
265 unsigned char bar_ext[SWM_BAR_MAX];
266 char bar_vertext[SWM_BAR_MAX];
268 sig_atomic_t bar_alarm = 0;
271 int bar_border_width = 1;
272 int bar_at_bottom = 0;
274 int bar_extra_running = 0;
277 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
278 int stack_enabled = 1;
279 int clock_enabled = 1;
280 int urgent_enabled = 0;
281 char *clock_format = NULL;
282 int title_name_enabled = 0;
283 int title_class_enabled = 0;
284 int window_name_enabled = 0;
285 int focus_mode = SWM_FOCUS_DEFAULT;
286 int disable_border = 0;
287 int border_width = 1;
288 int verbose_layout = 0;
291 XFontSetExtents *bar_fs_extents;
293 char *spawn_term[] = { NULL, NULL }; /* XXX fully dynamic */
296 #define SWM_MENU_FN (2)
297 #define SWM_MENU_NB (4)
298 #define SWM_MENU_NF (6)
299 #define SWM_MENU_SB (8)
300 #define SWM_MENU_SF (10)
302 /* layout manager data */
303 struct swm_geometry {
313 /* virtual "screens" */
315 TAILQ_ENTRY(swm_region) entry;
316 struct swm_geometry g;
317 struct workspace *ws; /* current workspace on this region */
318 struct workspace *ws_prior; /* prior workspace on this region */
319 struct swm_screen *s; /* screen idx */
322 TAILQ_HEAD(swm_region_list, swm_region);
325 TAILQ_ENTRY(ws_win) entry;
328 struct ws_win *child_trans; /* transient child window */
329 struct swm_geometry g; /* current geometry */
330 struct swm_geometry g_float; /* geometry when floating */
331 struct swm_geometry rg_float; /* region geom when floating */
332 int g_floatvalid; /* g_float geometry validity */
333 int floatmaxed; /* whether maxed by max_stack */
337 unsigned int ewmh_flags;
338 int font_size_boundary[SWM_MAX_FONT_STEPS];
344 unsigned long quirks;
345 struct workspace *ws; /* always valid */
346 struct swm_screen *s; /* always valid, never changes */
347 XWindowAttributes wa;
353 TAILQ_HEAD(ws_win_list, ws_win);
357 TAILQ_ENTRY(pid_e) entry;
361 TAILQ_HEAD(pid_list, pid_e);
362 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
364 /* layout handlers */
366 void vertical_config(struct workspace *, int);
367 void vertical_stack(struct workspace *, struct swm_geometry *);
368 void horizontal_config(struct workspace *, int);
369 void horizontal_stack(struct workspace *, struct swm_geometry *);
370 void max_stack(struct workspace *, struct swm_geometry *);
371 void plain_stacker(struct workspace *);
372 void fancy_stacker(struct workspace *);
374 struct ws_win *find_window(Window);
376 void grabbuttons(struct ws_win *, int);
377 void new_region(struct swm_screen *, int, int, int, int);
378 void unmanage_window(struct ws_win *);
379 long getstate(Window);
381 int conf_load(char *, int);
384 void (*l_stack)(struct workspace *, struct swm_geometry *);
385 void (*l_config)(struct workspace *, int);
387 #define SWM_L_FOCUSPREV (1<<0)
388 #define SWM_L_MAPONFOCUS (1<<1)
389 void (*l_string)(struct workspace *);
391 /* stack, configure */
392 { vertical_stack, vertical_config, 0, plain_stacker },
393 { horizontal_stack, horizontal_config, 0, plain_stacker },
395 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
396 { NULL, NULL, 0, NULL },
399 /* position of max_stack mode in the layouts array, index into layouts! */
400 #define SWM_V_STACK (0)
401 #define SWM_H_STACK (1)
402 #define SWM_MAX_STACK (2)
404 #define SWM_H_SLICE (32)
405 #define SWM_V_SLICE (32)
407 /* define work spaces */
409 int idx; /* workspace index */
410 char *name; /* workspace name */
411 int always_raise; /* raise windows on focus */
412 struct layout *cur_layout; /* current layout handlers */
413 struct ws_win *focus; /* may be NULL */
414 struct ws_win *focus_prev; /* may be NULL */
415 struct swm_region *r; /* may be NULL */
416 struct swm_region *old_r; /* may be NULL */
417 struct ws_win_list winlist; /* list of windows in ws */
418 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
419 char stacker[10]; /* display stacker and layout */
423 int horizontal_msize;
425 int horizontal_stacks;
432 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
433 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
435 /* physical screen mapping */
436 #define SWM_WS_MAX (10)
438 int idx; /* screen index */
439 struct swm_region_list rl; /* list of regions on this screen */
440 struct swm_region_list orl; /* list of old regions */
442 struct workspace ws[SWM_WS_MAX];
448 } c[SWM_S_COLOR_MAX];
452 struct swm_screen *screens;
455 /* args to functions */
458 #define SWM_ARG_ID_FOCUSNEXT (0)
459 #define SWM_ARG_ID_FOCUSPREV (1)
460 #define SWM_ARG_ID_FOCUSMAIN (2)
461 #define SWM_ARG_ID_FOCUSCUR (4)
462 #define SWM_ARG_ID_SWAPNEXT (10)
463 #define SWM_ARG_ID_SWAPPREV (11)
464 #define SWM_ARG_ID_SWAPMAIN (12)
465 #define SWM_ARG_ID_MOVELAST (13)
466 #define SWM_ARG_ID_MASTERSHRINK (20)
467 #define SWM_ARG_ID_MASTERGROW (21)
468 #define SWM_ARG_ID_MASTERADD (22)
469 #define SWM_ARG_ID_MASTERDEL (23)
470 #define SWM_ARG_ID_STACKRESET (30)
471 #define SWM_ARG_ID_STACKINIT (31)
472 #define SWM_ARG_ID_CYCLEWS_UP (40)
473 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
474 #define SWM_ARG_ID_CYCLESC_UP (42)
475 #define SWM_ARG_ID_CYCLESC_DOWN (43)
476 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
477 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
478 #define SWM_ARG_ID_STACKINC (50)
479 #define SWM_ARG_ID_STACKDEC (51)
480 #define SWM_ARG_ID_SS_ALL (60)
481 #define SWM_ARG_ID_SS_WINDOW (61)
482 #define SWM_ARG_ID_DONTCENTER (70)
483 #define SWM_ARG_ID_CENTER (71)
484 #define SWM_ARG_ID_KILLWINDOW (80)
485 #define SWM_ARG_ID_DELETEWINDOW (81)
486 #define SWM_ARG_ID_WIDTHGROW (90)
487 #define SWM_ARG_ID_WIDTHSHRINK (91)
488 #define SWM_ARG_ID_HEIGHTGROW (92)
489 #define SWM_ARG_ID_HEIGHTSHRINK (93)
490 #define SWM_ARG_ID_MOVEUP (100)
491 #define SWM_ARG_ID_MOVEDOWN (101)
492 #define SWM_ARG_ID_MOVELEFT (102)
493 #define SWM_ARG_ID_MOVERIGHT (103)
497 void focus(struct swm_region *, union arg *);
498 void focus_magic(struct ws_win *);
502 TAILQ_ENTRY(quirk) entry;
506 #define SWM_Q_FLOAT (1<<0) /* float this window */
507 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
508 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
509 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
510 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
511 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
513 TAILQ_HEAD(quirk_list, quirk);
514 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
517 * Supported EWMH hints should be added to
518 * both the enum and the ewmh array
520 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
521 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
522 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
523 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
524 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
525 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
526 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
527 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
528 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
529 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
535 } ewmh[SWM_EWMH_HINT_MAX] = {
536 /* must be in same order as in the enum */
537 {"_NET_ACTIVE_WINDOW", None},
538 {"_NET_MOVERESIZE_WINDOW", None},
539 {"_NET_CLOSE_WINDOW", None},
540 {"_NET_WM_WINDOW_TYPE", None},
541 {"_NET_WM_WINDOW_TYPE_DOCK", None},
542 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
543 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
544 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
545 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
546 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
547 {"_NET_WM_STATE", None},
548 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
549 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
550 {"_NET_WM_STATE_SKIP_TASKBAR", None},
551 {"_NET_WM_STATE_SKIP_PAGER", None},
552 {"_NET_WM_STATE_HIDDEN", None},
553 {"_NET_WM_STATE_ABOVE", None},
554 {"_SWM_WM_STATE_MANUAL", None},
555 {"_NET_WM_STATE_FULLSCREEN", None},
556 {"_NET_WM_ALLOWED_ACTIONS", None},
557 {"_NET_WM_ACTION_MOVE", None},
558 {"_NET_WM_ACTION_RESIZE", None},
559 {"_NET_WM_ACTION_FULLSCREEN", None},
560 {"_NET_WM_ACTION_CLOSE", None},
563 void store_float_geom(struct ws_win *, struct swm_region *);
564 int floating_toggle_win(struct ws_win *);
565 void spawn_select(struct swm_region *, union arg *, char *, int *);
566 unsigned char *get_win_name(Display *, Window, Atom, Atom, unsigned long *);
569 get_property(Window id, Atom atom, long count, Atom type,
570 unsigned long *n, unsigned char **data)
573 unsigned long tmp, extra;
574 unsigned long *nitems;
577 nitems = n != NULL ? n : &tmp;
578 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
579 &real, &format, nitems, &extra, data);
581 if (status != Success)
590 update_iconic(struct ws_win *win, int newv)
597 iprop = XInternAtom(display, "_SWM_ICONIC", False);
601 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
602 PropModeReplace, (unsigned char *)&v, 1);
604 XDeleteProperty(display, win->id, iprop);
608 get_iconic(struct ws_win *win)
613 unsigned long nitems, extra;
614 unsigned char *prop = NULL;
616 iprop = XInternAtom(display, "_SWM_ICONIC", False);
619 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
620 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
621 if (status != Success)
623 if (rettype != XA_INTEGER || retfmt != 32)
627 v = *((int32_t *)prop);
641 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
643 for (i = 0; i < LENGTH(ewmh); i++)
644 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
646 for (i = 0; i < ScreenCount(display); i++) {
647 /* Support check window will be created by workaround(). */
649 /* Report supported atoms */
650 XDeleteProperty(display, screens[i].root, sup_list);
651 for (j = 0; j < LENGTH(ewmh); j++)
652 XChangeProperty(display, screens[i].root,
653 sup_list, XA_ATOM, 32,
654 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
662 unsigned char *data = NULL;
664 Atom sup_check, sup_list;
667 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
668 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
670 for (i = 0; i < ScreenCount(display); i++) {
671 /* Get the support check window and destroy it */
672 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
677 XDestroyWindow(display, id);
678 XDeleteProperty(display, screens[i].root, sup_check);
679 XDeleteProperty(display, screens[i].root, sup_list);
687 ewmh_autoquirk(struct ws_win *win)
690 unsigned long *data = NULL, n;
693 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
694 XA_ATOM, &n, (void *)&data);
701 for (i = 0; i < n; i++) {
703 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
705 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
706 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
707 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
709 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
712 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
713 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
715 win->quirks = SWM_Q_FLOAT;
723 #define SWM_EWMH_ACTION_COUNT_MAX (6)
724 #define EWMH_F_FULLSCREEN (1<<0)
725 #define EWMH_F_ABOVE (1<<1)
726 #define EWMH_F_HIDDEN (1<<2)
727 #define EWMH_F_SKIP_PAGER (1<<3)
728 #define EWMH_F_SKIP_TASKBAR (1<<4)
729 #define SWM_F_MANUAL (1<<5)
732 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
734 struct swm_geometry rg;
742 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
743 "fullscreen %s\n", win->id, YESNO(fs));
748 store_float_geom(win, win->ws->r);
752 if (win->g_floatvalid) {
753 /* refloat at last floating relative position */
754 X(win) = win->g_float.x - win->rg_float.x + rg.x;
755 Y(win) = win->g_float.y - win->rg_float.y + rg.y;
756 WIDTH(win) = win->g_float.w;
757 HEIGHT(win) = win->g_float.h;
765 ewmh_update_actions(struct ws_win *win)
767 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
773 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
776 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
777 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
780 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
781 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
784 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
785 #define _NET_WM_STATE_ADD 1 /* add/set property */
786 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
789 ewmh_update_win_state(struct ws_win *win, long state, long action)
791 unsigned int mask = 0;
792 unsigned int changed = 0;
793 unsigned int orig_flags;
798 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
799 mask = EWMH_F_FULLSCREEN;
800 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
802 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
804 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
805 mask = EWMH_F_SKIP_PAGER;
806 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
807 mask = EWMH_F_SKIP_TASKBAR;
810 orig_flags = win->ewmh_flags;
813 case _NET_WM_STATE_REMOVE:
814 win->ewmh_flags &= ~mask;
816 case _NET_WM_STATE_ADD:
817 win->ewmh_flags |= mask;
819 case _NET_WM_STATE_TOGGLE:
820 win->ewmh_flags ^= mask;
824 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
826 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
828 if (!floating_toggle_win(win))
829 win->ewmh_flags = orig_flags; /* revert */
830 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
832 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
833 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
835 if (!ewmh_set_win_fullscreen(win,
836 win->ewmh_flags & EWMH_F_FULLSCREEN))
837 win->ewmh_flags = orig_flags; /* revert */
839 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
841 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
842 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
843 XA_ATOM, 32, PropModeAppend,
844 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
845 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
846 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
847 XA_ATOM, 32, PropModeAppend,
848 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
849 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
850 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
851 XA_ATOM, 32, PropModeAppend,
852 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
853 if (win->ewmh_flags & EWMH_F_ABOVE)
854 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
855 XA_ATOM, 32, PropModeAppend,
856 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
857 if (win->ewmh_flags & SWM_F_MANUAL)
858 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
859 XA_ATOM, 32, PropModeAppend,
860 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
864 ewmh_get_win_state(struct ws_win *win)
875 win->ewmh_flags |= EWMH_F_ABOVE;
877 win->ewmh_flags |= SWM_F_MANUAL;
879 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
880 (~0L), XA_ATOM, &n, (void *)&states);
885 for (i = 0; i < n; i++)
886 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
894 geteventname(XEvent *e)
906 name = "ButtonPress";
909 name = "ButtonRelease";
912 name = "MotionNotify";
915 name = "EnterNotify";
918 name = "LeaveNotify";
927 name = "KeymapNotify";
933 name = "GraphicsExpose";
938 case VisibilityNotify:
939 name = "VisibilityNotify";
942 name = "CreateNotify";
945 name = "DestroyNotify";
948 name = "UnmapNotify";
957 name = "ReparentNotify";
959 case ConfigureNotify:
960 name = "ConfigureNotify";
962 case ConfigureRequest:
963 name = "ConfigureRequest";
966 name = "GravityNotify";
969 name = "ResizeRequest";
971 case CirculateNotify:
972 name = "CirculateNotify";
974 case CirculateRequest:
975 name = "CirculateRequest";
978 name = "PropertyNotify";
981 name = "SelectionClear";
983 case SelectionRequest:
984 name = "SelectionRequest";
986 case SelectionNotify:
987 name = "SelectionNotify";
990 name = "ColormapNotify";
993 name = "ClientMessage";
996 name = "MappingNotify";
1006 xrandr_geteventname(XEvent *e)
1010 switch(e->type - xrandr_eventbase) {
1011 case RRScreenChangeNotify:
1012 name = "RRScreenChangeNotify";
1022 dumpwins(struct swm_region *r, union arg *args)
1026 XWindowAttributes wa;
1028 if (r->ws == NULL) {
1029 fprintf(stderr, "dumpwins: invalid workspace\n");
1033 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
1035 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1036 state = getstate(win->id);
1037 if (!XGetWindowAttributes(display, win->id, &wa))
1038 fprintf(stderr, "window: 0x%lx, failed "
1039 "XGetWindowAttributes\n", win->id);
1040 fprintf(stderr, "window: 0x%lx, map_state: %d, state: %d, "
1041 "transient: 0x%lx\n",
1042 win->id, wa.map_state, state, win->transient);
1045 fprintf(stderr, "===== unmanaged window list =====\n");
1046 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1047 state = getstate(win->id);
1048 if (!XGetWindowAttributes(display, win->id, &wa))
1049 fprintf(stderr, "window: 0x%lx, failed "
1050 "XGetWindowAttributes\n", win->id);
1051 fprintf(stderr, "window: 0x%lx, map_state: %d, state: %d, "
1052 "transient: 0x%lx\n",
1053 win->id, wa.map_state, state, win->transient);
1056 fprintf(stderr, "=================================\n");
1060 dumpwins(struct swm_region *r, union arg *args)
1063 #endif /* SWM_DEBUG */
1065 void expose(XEvent *);
1066 void keypress(XEvent *);
1067 void buttonpress(XEvent *);
1068 void configurerequest(XEvent *);
1069 void configurenotify(XEvent *);
1070 void destroynotify(XEvent *);
1071 void enternotify(XEvent *);
1072 void focusevent(XEvent *);
1073 void mapnotify(XEvent *);
1074 void mappingnotify(XEvent *);
1075 void maprequest(XEvent *);
1076 void propertynotify(XEvent *);
1077 void unmapnotify(XEvent *);
1078 void visibilitynotify(XEvent *);
1079 void clientmessage(XEvent *);
1081 void (*handler[LASTEvent])(XEvent *) = {
1083 [KeyPress] = keypress,
1084 [ButtonPress] = buttonpress,
1085 [ConfigureRequest] = configurerequest,
1086 [ConfigureNotify] = configurenotify,
1087 [DestroyNotify] = destroynotify,
1088 [EnterNotify] = enternotify,
1089 [FocusIn] = focusevent,
1090 [FocusOut] = focusevent,
1091 [MapNotify] = mapnotify,
1092 [MappingNotify] = mappingnotify,
1093 [MapRequest] = maprequest,
1094 [PropertyNotify] = propertynotify,
1095 [UnmapNotify] = unmapnotify,
1096 [VisibilityNotify] = visibilitynotify,
1097 [ClientMessage] = clientmessage,
1103 int saved_errno, status;
1106 saved_errno = errno;
1110 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1115 if (errno != ECHILD)
1116 warn("sighdlr: waitpid");
1117 #endif /* SWM_DEBUG */
1120 if (pid == searchpid)
1124 if (WIFEXITED(status)) {
1125 if (WEXITSTATUS(status) != 0)
1126 warnx("sighdlr: child exit status: %d",
1127 WEXITSTATUS(status));
1129 warnx("sighdlr: child is terminated "
1131 #endif /* SWM_DEBUG */
1145 errno = saved_errno;
1151 struct pid_e *p = NULL;
1153 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1158 TAILQ_FOREACH(p, &pidlist, entry) {
1167 name_to_color(char *colorname)
1171 XColor screen_def, exact_def;
1172 unsigned long result = 0;
1173 char cname[32] = "#";
1175 cmap = DefaultColormap(display, screens[0].idx);
1176 status = XAllocNamedColor(display, cmap, colorname,
1177 &screen_def, &exact_def);
1179 strlcat(cname, colorname + 2, sizeof cname - 1);
1180 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1184 result = screen_def.pixel;
1186 fprintf(stderr, "color '%s' not found.\n", colorname);
1192 setscreencolor(char *val, int i, int c)
1194 if (i > 0 && i <= ScreenCount(display)) {
1195 screens[i - 1].c[c].color = name_to_color(val);
1196 free(screens[i - 1].c[c].name);
1197 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1199 } else if (i == -1) {
1200 for (i = 0; i < ScreenCount(display); i++) {
1201 screens[i].c[c].color = name_to_color(val);
1202 free(screens[i].c[c].name);
1203 if ((screens[i].c[c].name = strdup(val)) == NULL)
1207 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1208 i, ScreenCount(display));
1212 fancy_stacker(struct workspace *ws)
1214 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1215 if (ws->cur_layout->l_stack == vertical_stack)
1216 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1217 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1218 if (ws->cur_layout->l_stack == horizontal_stack)
1219 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1220 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1224 plain_stacker(struct workspace *ws)
1226 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1227 if (ws->cur_layout->l_stack == vertical_stack)
1228 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1229 if (ws->cur_layout->l_stack == horizontal_stack)
1230 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1234 custom_region(char *val)
1236 unsigned int sidx, x, y, w, h;
1238 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1239 errx(1, "invalid custom region, "
1240 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1241 if (sidx < 1 || sidx > ScreenCount(display))
1242 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1243 sidx, ScreenCount(display));
1247 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1249 if (x > DisplayWidth(display, sidx) ||
1250 y > DisplayHeight(display, sidx) ||
1251 w + x > DisplayWidth(display, sidx) ||
1252 h + y > DisplayHeight(display, sidx)) {
1253 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1254 "- not within screen boundaries "
1255 "(%ux%u)\n", w, h, x, y,
1256 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1260 new_region(&screens[sidx], x, y, w, h);
1264 socket_setnonblock(int fd)
1268 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1269 err(1, "fcntl F_GETFL");
1270 flags |= O_NONBLOCK;
1271 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1272 err(1, "fcntl F_SETFL");
1276 bar_print(struct swm_region *r, char *s)
1280 XRectangle ibox, lbox;
1282 XClearWindow(display, r->bar_window);
1285 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1287 switch (bar_justify) {
1288 case SWM_BAR_JUSTIFY_LEFT:
1291 case SWM_BAR_JUSTIFY_CENTER:
1292 x = (WIDTH(r) - lbox.width) / 2;
1294 case SWM_BAR_JUSTIFY_RIGHT:
1295 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1299 if (x < SWM_BAR_OFFSET)
1302 DRAWSTRING(display, r->bar_window, bar_fs, r->s->bar_gc,
1303 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1308 bar_extra_stop(void)
1312 bzero(bar_pipe, sizeof bar_pipe);
1315 kill(bar_pid, SIGTERM);
1318 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1323 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1325 int do_class, do_name;
1327 XClassHint *xch = NULL;
1329 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1330 cur_focus != NULL) {
1331 if ((xch = XAllocClassHint()) == NULL)
1333 status = XGetClassHint(display, cur_focus->id, xch);
1334 if (status == BadWindow || status == BadAlloc)
1336 do_class = (title_class_enabled && xch->res_class != NULL);
1337 do_name = (title_name_enabled && xch->res_name != NULL);
1339 strlcat(s, xch->res_class, sz);
1340 if (do_class && do_name)
1341 strlcat(s, ":", sz);
1343 strlcat(s, xch->res_name, sz);
1344 strlcat(s, " ", sz);
1348 XFree(xch->res_name);
1349 XFree(xch->res_class);
1355 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1357 unsigned char *title;
1360 if (window_name_enabled && cur_focus != NULL) {
1361 if ((title = get_win_name(display, cur_focus->id, a_netwmname,
1362 a_utf8_string, &len)) != NULL) {
1363 DNPRINTF(SWM_D_BAR, "bar_window_name: title: %s\n",
1366 if (cur_focus->floating)
1367 strlcat(s, "(f) ", sz);
1368 strlcat(s, (char *)title, sz);
1369 strlcat(s, " ", sz);
1375 int urgent[SWM_WS_MAX];
1377 bar_urgent(char *s, ssize_t sz)
1379 XWMHints *wmh = NULL;
1384 if (urgent_enabled == 0)
1387 for (i = 0; i < SWM_WS_MAX; i++)
1390 for (i = 0; i < ScreenCount(display); i++)
1391 for (j = 0; j < SWM_WS_MAX; j++)
1392 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1393 wmh = XGetWMHints(display, win->id);
1397 if (wmh->flags & XUrgencyHint)
1402 strlcat(s, "* ", sz);
1403 for (i = 0; i < SWM_WS_MAX; i++) {
1405 snprintf(b, sizeof b, "%d ", i + 1);
1407 snprintf(b, sizeof b, "- ");
1410 strlcat(s, "* ", sz);
1418 struct swm_region *r;
1421 char ws[SWM_BAR_MAX];
1422 char s[SWM_BAR_MAX];
1423 unsigned char cn[SWM_BAR_MAX];
1424 char loc[SWM_BAR_MAX];
1425 char *b, *stack = "";
1427 if (bar_enabled == 0)
1429 if (bar_extra && bar_extra_running) {
1430 /* ignore short reads; it'll correct itself */
1431 while ((b = fgetln(stdin, &len)) != NULL)
1432 if (b && b[len - 1] == '\n') {
1434 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1436 if (b == NULL && errno != EAGAIN) {
1437 fprintf(stderr, "bar_update: bar_extra failed: "
1438 "errno: %d %s\n", errno, strerror(errno));
1442 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1444 if (clock_enabled == 0)
1445 strlcpy(s, "", sizeof s);
1448 localtime_r(&tmt, &tm);
1449 strftime(s, sizeof s, clock_format, &tm);
1450 strlcat(s, " ", sizeof s);
1453 for (i = 0; i < ScreenCount(display); i++) {
1455 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1456 strlcpy((char *)cn, "", sizeof cn);
1457 strlcpy(ws, "", sizeof ws);
1459 bar_urgent((char *)cn, sizeof cn);
1460 bar_class_name((char *)cn, sizeof cn,
1462 bar_window_name((char *)cn, sizeof cn,
1465 snprintf(ws, sizeof ws, "<%s>",
1469 stack = r->ws->stacker;
1471 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s "
1472 "%s", x++, r->ws->idx + 1, stack, ws, s, cn,
1473 bar_ext, bar_vertext);
1487 bar_toggle(struct swm_region *r, union arg *args)
1489 struct swm_region *tmpr;
1490 int i, sc = ScreenCount(display);
1492 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1495 for (i = 0; i < sc; i++)
1496 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1497 XUnmapWindow(display, tmpr->bar_window);
1499 for (i = 0; i < sc; i++)
1500 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1501 XMapRaised(display, tmpr->bar_window);
1503 bar_enabled = !bar_enabled;
1506 /* must be after stack */
1513 XSetWindowAttributes wa;
1514 struct swm_region *r;
1517 /* do this here because the conf file is in memory */
1518 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1519 /* launch external status app */
1520 bar_extra_running = 1;
1521 if (pipe(bar_pipe) == -1)
1522 err(1, "pipe error");
1523 socket_setnonblock(bar_pipe[0]);
1524 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1525 if (dup2(bar_pipe[0], 0) == -1)
1527 if (dup2(bar_pipe[1], 1) == -1)
1529 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1530 err(1, "could not disable SIGPIPE");
1531 switch (bar_pid = fork()) {
1533 err(1, "cannot fork");
1537 execvp(bar_argv[0], bar_argv);
1538 err(1, "%s external app failed", bar_argv[0]);
1540 default: /* parent */
1546 bzero(&wa, sizeof wa);
1547 for (i = 0; i < ScreenCount(display); i++)
1548 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1550 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1551 wa.background_pixel =
1552 screens[i].c[SWM_S_COLOR_BAR].color;
1553 XChangeWindowAttributes(display, r->bar_window,
1554 CWBackPixel | CWBorderPixel, &wa);
1560 bar_setup(struct swm_region *r)
1562 char *default_string;
1563 char **missing_charsets;
1564 int num_missing_charsets = 0;
1568 XFreeFontSet(display, bar_fs);
1573 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1575 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1576 &num_missing_charsets, &default_string);
1578 if (num_missing_charsets > 0) {
1579 warnx("Unable to load charset(s):");
1581 for (i = 0; i < num_missing_charsets; ++i)
1582 warnx("%s", missing_charsets[i]);
1584 XFreeStringList(missing_charsets);
1586 if (strcmp(default_string, ""))
1587 warnx("Glyphs from those sets will be replaced "
1588 "by '%s'.", default_string);
1590 warnx("Glyphs from those sets won't be drawn.");
1594 errx(1, "Error creating font set structure.");
1596 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1598 bar_height = bar_fs_extents->max_logical_extent.height +
1599 2 * bar_border_width;
1605 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1607 r->bar_window = XCreateSimpleWindow(display,
1608 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1609 bar_height - 2 * bar_border_width,
1610 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1611 r->s->c[SWM_S_COLOR_BAR].color);
1612 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1614 XMapRaised(display, r->bar_window);
1615 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1617 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1618 err(1, "could not install bar_signal");
1623 drain_enter_notify(void)
1628 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1631 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1635 set_win_state(struct ws_win *win, long state)
1637 long data[] = {state, None};
1639 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1644 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1645 (unsigned char *)data, 2);
1652 unsigned char *p = NULL;
1655 if (!get_property(w, astate, 2L, astate, &n, &p))
1658 result = *((long *)p);
1664 version(struct swm_region *r, union arg *args)
1666 bar_version = !bar_version;
1668 snprintf(bar_vertext, sizeof bar_vertext,
1669 "Version: %s Build: %s", SCROTWM_VERSION, buildstr);
1671 strlcpy(bar_vertext, "", sizeof bar_vertext);
1676 client_msg(struct ws_win *win, Atom a)
1678 XClientMessageEvent cm;
1683 bzero(&cm, sizeof cm);
1684 cm.type = ClientMessage;
1685 cm.window = win->id;
1686 cm.message_type = aprot;
1689 cm.data.l[1] = CurrentTime;
1690 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1693 /* synthetic response to a ConfigureRequest when not making a change */
1695 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1702 /* send notification of unchanged state. */
1703 ce.type = ConfigureNotify;
1706 ce.width = WIDTH(win);
1707 ce.height = HEIGHT(win);
1708 ce.override_redirect = False;
1712 ce.display = display;
1714 ce.window = win->id;
1715 ce.border_width = border_width;
1719 ce.display = ev->display;
1720 ce.event = ev->window;
1721 ce.window = ev->window;
1723 /* make response appear more WM_SIZE_HINTS-compliant */
1725 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1726 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1727 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1728 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1729 SH_INC_W(win), SH_INC_H(win));
1733 /* the hint may be set... to 0! */
1734 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1735 ce.width = SH_MIN_W(win);
1736 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1737 ce.height = SH_MIN_H(win);
1742 /* may also be advertized as 0 */
1743 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1744 ce.width = SH_MAX_W(win);
1745 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1746 ce.height = SH_MAX_H(win);
1749 /* resize increment. */
1751 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1752 ce.width -= (ce.width - SH_MIN_W(win)) %
1754 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1755 ce.height -= (ce.height - SH_MIN_H(win)) %
1759 /* adjust x and y for requested border_width. */
1760 ce.x += border_width - ev->border_width;
1761 ce.y += border_width - ev->border_width;
1762 ce.border_width = ev->border_width;
1763 ce.above = ev->above;
1766 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1767 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1768 ce.y, ce.width, ce.height, ce.border_width);
1770 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1774 count_win(struct workspace *ws, int count_transient)
1779 TAILQ_FOREACH(win, &ws->winlist, entry) {
1780 if (count_transient == 0 && win->floating)
1782 if (count_transient == 0 && win->transient)
1788 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1794 quit(struct swm_region *r, union arg *args)
1796 DNPRINTF(SWM_D_MISC, "quit\n");
1801 unmap_window(struct ws_win *win)
1806 /* don't unmap again */
1807 if (getstate(win->id) == IconicState)
1810 set_win_state(win, IconicState);
1812 XUnmapWindow(display, win->id);
1813 XSetWindowBorder(display, win->id,
1814 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1823 for (i = 0; i < ScreenCount(display); i++)
1824 for (j = 0; j < SWM_WS_MAX; j++)
1825 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1830 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1837 event.display = display; /* Ignored, but what the hell */
1838 event.window = win->id;
1839 event.root = win->s->root;
1840 event.subwindow = None;
1841 event.time = CurrentTime;
1846 event.same_screen = True;
1847 event.keycode = XKeysymToKeycode(display, keysym);
1848 event.state = modifiers;
1850 event.type = KeyPress;
1851 XSendEvent(event.display, event.window, True,
1852 KeyPressMask, (XEvent *)&event);
1854 event.type = KeyRelease;
1855 XSendEvent(event.display, event.window, True,
1856 KeyPressMask, (XEvent *)&event);
1861 restart(struct swm_region *r, union arg *args)
1863 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1865 /* disable alarm because the following code may not be interrupted */
1867 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1868 err(1, "can't disable alarm");
1873 XCloseDisplay(display);
1874 execvp(start_argv[0], start_argv);
1875 fprintf(stderr, "execvp failed\n");
1881 root_to_region(Window root)
1883 struct swm_region *r = NULL;
1885 int i, x, y, wx, wy;
1888 for (i = 0; i < ScreenCount(display); i++)
1889 if (screens[i].root == root)
1892 if (XQueryPointer(display, screens[i].root,
1893 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1894 /* choose a region based on pointer location */
1895 TAILQ_FOREACH(r, &screens[i].rl, entry)
1896 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1897 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1902 r = TAILQ_FIRST(&screens[i].rl);
1908 find_unmanaged_window(Window id)
1913 for (i = 0; i < ScreenCount(display); i++)
1914 for (j = 0; j < SWM_WS_MAX; j++)
1915 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1923 find_window(Window id)
1926 Window wrr, wpr, *wcr = NULL;
1930 for (i = 0; i < ScreenCount(display); i++)
1931 for (j = 0; j < SWM_WS_MAX; j++)
1932 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1936 /* if we were looking for the parent return that window instead */
1937 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1942 /* ignore not found and root */
1943 if (wpr == 0 || wrr == wpr)
1946 /* look for parent */
1947 for (i = 0; i < ScreenCount(display); i++)
1948 for (j = 0; j < SWM_WS_MAX; j++)
1949 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1957 spawn(int ws_idx, union arg *args, int close_fd)
1962 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1965 close(ConnectionNumber(display));
1967 setenv("LD_PRELOAD", SWM_LIB, 1);
1969 if (asprintf(&ret, "%d", ws_idx) == -1) {
1973 setenv("_SWM_WS", ret, 1);
1977 if (asprintf(&ret, "%d", getpid()) == -1) {
1981 setenv("_SWM_PID", ret, 1);
1985 if (setsid() == -1) {
1992 * close stdin and stdout to prevent interaction between apps
1993 * and the baraction script
1994 * leave stderr open to record errors
1996 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2000 dup2(fd, STDIN_FILENO);
2001 dup2(fd, STDOUT_FILENO);
2006 execvp(args->argv[0], args->argv);
2013 spawnterm(struct swm_region *r, union arg *args)
2015 DNPRINTF(SWM_D_MISC, "spawnterm\n");
2019 setenv("_SWM_XTERM_FONTADJ", "", 1);
2020 spawn(r->ws->idx, args, 1);
2025 kill_refs(struct ws_win *win)
2028 struct swm_region *r;
2029 struct workspace *ws;
2034 for (i = 0; i < ScreenCount(display); i++)
2035 TAILQ_FOREACH(r, &screens[i].rl, entry)
2036 for (x = 0; x < SWM_WS_MAX; x++) {
2038 if (win == ws->focus)
2040 if (win == ws->focus_prev)
2041 ws->focus_prev = NULL;
2046 validate_win(struct ws_win *testwin)
2049 struct workspace *ws;
2050 struct swm_region *r;
2053 if (testwin == NULL)
2056 for (i = 0; i < ScreenCount(display); i++)
2057 TAILQ_FOREACH(r, &screens[i].rl, entry)
2058 for (x = 0; x < SWM_WS_MAX; x++) {
2060 TAILQ_FOREACH(win, &ws->winlist, entry)
2068 validate_ws(struct workspace *testws)
2070 struct swm_region *r;
2071 struct workspace *ws;
2074 /* validate all ws */
2075 for (i = 0; i < ScreenCount(display); i++)
2076 TAILQ_FOREACH(r, &screens[i].rl, entry)
2077 for (x = 0; x < SWM_WS_MAX; x++) {
2086 unfocus_win(struct ws_win *win)
2091 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2095 if (win->ws == NULL)
2098 if (validate_ws(win->ws))
2099 return; /* XXX this gets hit with thunderbird, needs fixing */
2101 if (win->ws->r == NULL)
2104 if (validate_win(win)) {
2109 if (win->ws->focus == win) {
2110 win->ws->focus = NULL;
2111 win->ws->focus_prev = win;
2114 if (validate_win(win->ws->focus)) {
2115 kill_refs(win->ws->focus);
2116 win->ws->focus = NULL;
2118 if (validate_win(win->ws->focus_prev)) {
2119 kill_refs(win->ws->focus_prev);
2120 win->ws->focus_prev = NULL;
2123 /* drain all previous unfocus events */
2124 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2127 grabbuttons(win, 0);
2128 XSetWindowBorder(display, win->id,
2129 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2131 XChangeProperty(display, win->s->root,
2132 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2133 PropModeReplace, (unsigned char *)&none,1);
2142 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2144 for (i = 0; i < ScreenCount(display); i++)
2145 for (j = 0; j < SWM_WS_MAX; j++)
2146 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2151 focus_win(struct ws_win *win)
2156 struct ws_win *cfw = NULL;
2159 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2163 if (win->ws == NULL)
2166 if (validate_ws(win->ws))
2167 return; /* XXX this gets hit with thunderbird, needs fixing */
2169 if (validate_win(win)) {
2174 if (validate_win(win)) {
2179 XGetInputFocus(display, &cur_focus, &rr);
2180 if ((cfw = find_window(cur_focus)) != NULL)
2183 /* use larger hammer since the window was killed somehow */
2184 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2185 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2186 XSetWindowBorder(display, cfw->id,
2187 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2190 win->ws->focus = win;
2192 if (win->ws->r != NULL) {
2193 /* drain all previous focus events */
2194 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2198 XSetInputFocus(display, win->id,
2199 RevertToParent, CurrentTime);
2200 grabbuttons(win, 1);
2201 XSetWindowBorder(display, win->id,
2202 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2203 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2204 win->ws->always_raise)
2205 XMapRaised(display, win->id);
2207 XChangeProperty(display, win->s->root,
2208 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2209 PropModeReplace, (unsigned char *)&win->id,1);
2212 if (window_name_enabled)
2217 switchws(struct swm_region *r, union arg *args)
2219 int wsid = args->id, unmap_old = 0;
2220 struct swm_region *this_r, *other_r;
2222 struct workspace *new_ws, *old_ws;
2229 old_ws = this_r->ws;
2230 new_ws = &this_r->s->ws[wsid];
2232 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2233 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2235 if (new_ws == NULL || old_ws == NULL)
2237 if (new_ws == old_ws)
2240 other_r = new_ws->r;
2241 if (other_r == NULL) {
2242 /* the other workspace is hidden, hide this one */
2246 /* the other ws is visible in another region, exchange them */
2247 other_r->ws_prior = new_ws;
2248 other_r->ws = old_ws;
2249 old_ws->r = other_r;
2251 this_r->ws_prior = old_ws;
2252 this_r->ws = new_ws;
2255 /* this is needed so that we can click on a window after a restart */
2259 a.id = SWM_ARG_ID_FOCUSCUR;
2260 focus(new_ws->r, &a);
2264 /* unmap old windows */
2266 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2269 if (focus_mode == SWM_FOCUS_DEFAULT)
2270 drain_enter_notify();
2274 cyclews(struct swm_region *r, union arg *args)
2277 struct swm_screen *s = r->s;
2280 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2281 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2286 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2289 case SWM_ARG_ID_CYCLEWS_UP:
2290 if (a.id < SWM_WS_MAX - 1)
2295 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2298 case SWM_ARG_ID_CYCLEWS_DOWN:
2302 a.id = SWM_WS_MAX - 1;
2309 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2311 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2315 } while (a.id != r->ws->idx);
2319 priorws(struct swm_region *r, union arg *args)
2323 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2324 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2326 if (r->ws_prior == NULL)
2329 a.id = r->ws_prior->idx;
2334 cyclescr(struct swm_region *r, union arg *args)
2336 struct swm_region *rr = NULL;
2340 /* do nothing if we don't have more than one screen */
2341 if (!(ScreenCount(display) > 1 || outputs > 1))
2346 case SWM_ARG_ID_CYCLESC_UP:
2347 rr = TAILQ_NEXT(r, entry);
2349 rr = TAILQ_FIRST(&screens[i].rl);
2351 case SWM_ARG_ID_CYCLESC_DOWN:
2352 rr = TAILQ_PREV(r, swm_region_list, entry);
2354 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2362 /* move mouse to region */
2364 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2365 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2367 a.id = SWM_ARG_ID_FOCUSCUR;
2370 if (rr->ws->focus) {
2371 /* move to focus window */
2372 x = X(rr->ws->focus) + 1;
2373 y = Y(rr->ws->focus) + 1;
2374 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2379 sort_windows(struct ws_win_list *wl)
2381 struct ws_win *win, *parent, *nxt;
2386 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2387 nxt = TAILQ_NEXT(win, entry);
2388 if (win->transient) {
2389 parent = find_window(win->transient);
2390 if (parent == NULL) {
2391 fprintf(stderr, "not possible bug\n");
2394 TAILQ_REMOVE(wl, win, entry);
2395 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2402 swapwin(struct swm_region *r, union arg *args)
2404 struct ws_win *target, *source;
2405 struct ws_win *cur_focus;
2406 struct ws_win_list *wl;
2409 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2410 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2412 cur_focus = r->ws->focus;
2413 if (cur_focus == NULL)
2417 wl = &source->ws->winlist;
2420 case SWM_ARG_ID_SWAPPREV:
2421 if (source->transient)
2422 source = find_window(source->transient);
2423 target = TAILQ_PREV(source, ws_win_list, entry);
2424 if (target && target->transient)
2425 target = find_window(target->transient);
2426 TAILQ_REMOVE(wl, source, entry);
2428 TAILQ_INSERT_TAIL(wl, source, entry);
2430 TAILQ_INSERT_BEFORE(target, source, entry);
2432 case SWM_ARG_ID_SWAPNEXT:
2433 target = TAILQ_NEXT(source, entry);
2434 /* move the parent and let the sort handle the move */
2435 if (source->transient)
2436 source = find_window(source->transient);
2437 TAILQ_REMOVE(wl, source, entry);
2439 TAILQ_INSERT_HEAD(wl, source, entry);
2441 TAILQ_INSERT_AFTER(wl, target, source, entry);
2443 case SWM_ARG_ID_SWAPMAIN:
2444 target = TAILQ_FIRST(wl);
2445 if (target == source) {
2446 if (source->ws->focus_prev != NULL &&
2447 source->ws->focus_prev != target)
2449 source = source->ws->focus_prev;
2453 if (target == NULL || source == NULL)
2455 source->ws->focus_prev = target;
2456 TAILQ_REMOVE(wl, target, entry);
2457 TAILQ_INSERT_BEFORE(source, target, entry);
2458 TAILQ_REMOVE(wl, source, entry);
2459 TAILQ_INSERT_HEAD(wl, source, entry);
2461 case SWM_ARG_ID_MOVELAST:
2462 TAILQ_REMOVE(wl, source, entry);
2463 TAILQ_INSERT_TAIL(wl, source, entry);
2466 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2476 focus_prev(struct ws_win *win)
2478 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2479 struct ws_win *cur_focus = NULL;
2480 struct ws_win_list *wl = NULL;
2481 struct workspace *ws = NULL;
2483 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx\n", WINID(win));
2485 if (!(win && win->ws))
2490 cur_focus = ws->focus;
2491 winlostfocus = cur_focus;
2493 /* pickle, just focus on whatever */
2494 if (cur_focus == NULL) {
2495 /* use prev_focus if valid */
2496 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2497 find_window(WINID(ws->focus_prev)))
2498 winfocus = ws->focus_prev;
2499 if (winfocus == NULL)
2500 winfocus = TAILQ_FIRST(wl);
2504 /* if transient focus on parent */
2505 if (cur_focus->transient) {
2506 winfocus = find_window(cur_focus->transient);
2510 /* if in max_stack try harder */
2511 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2512 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2513 if (cur_focus != ws->focus_prev)
2514 winfocus = ws->focus_prev;
2515 else if (cur_focus != ws->focus)
2516 winfocus = ws->focus;
2518 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2523 if (cur_focus == win)
2524 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2525 if (winfocus == NULL)
2526 winfocus = TAILQ_LAST(wl, ws_win_list);
2527 if (winfocus == NULL || winfocus == win)
2528 winfocus = TAILQ_NEXT(cur_focus, entry);
2530 if (winfocus == winlostfocus || winfocus == NULL) {
2531 /* update the bar so that title/class/name will be cleared. */
2532 if (window_name_enabled || title_name_enabled ||
2533 title_class_enabled)
2538 focus_magic(winfocus);
2542 focus(struct swm_region *r, union arg *args)
2544 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2545 struct ws_win *cur_focus = NULL;
2546 struct ws_win_list *wl = NULL;
2547 struct workspace *ws = NULL;
2553 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2555 /* treat FOCUS_CUR special */
2556 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2557 if (r->ws->focus && r->ws->focus->iconic == 0)
2558 winfocus = r->ws->focus;
2559 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2560 winfocus = r->ws->focus_prev;
2562 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2563 if (winfocus->iconic == 0)
2566 focus_magic(winfocus);
2570 if ((cur_focus = r->ws->focus) == NULL)
2574 if (TAILQ_EMPTY(wl))
2576 /* make sure there is at least one uniconified window */
2578 TAILQ_FOREACH(winfocus, wl, entry)
2579 if (winfocus->iconic == 0) {
2586 winlostfocus = cur_focus;
2589 case SWM_ARG_ID_FOCUSPREV:
2590 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2592 head = TAILQ_LAST(wl, ws_win_list);
2594 if (WINID(winfocus) == cur_focus->transient) {
2595 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2597 head = TAILQ_LAST(wl, ws_win_list);
2602 if (winfocus && winfocus->iconic) {
2603 while (winfocus != cur_focus) {
2604 if (winfocus == NULL)
2605 winfocus = TAILQ_LAST(wl, ws_win_list);
2606 if (winfocus->iconic == 0)
2608 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2614 case SWM_ARG_ID_FOCUSNEXT:
2615 head = TAILQ_NEXT(cur_focus, entry);
2617 head = TAILQ_FIRST(wl);
2621 if (winfocus && winfocus->iconic) {
2622 while (winfocus != cur_focus) {
2623 if (winfocus == NULL)
2624 winfocus = TAILQ_FIRST(wl);
2625 if (winfocus->iconic == 0)
2627 winfocus = TAILQ_NEXT(winfocus, entry);
2632 case SWM_ARG_ID_FOCUSMAIN:
2633 winfocus = TAILQ_FIRST(wl);
2634 if (winfocus == cur_focus)
2635 winfocus = cur_focus->ws->focus_prev;
2641 if (winfocus == winlostfocus || winfocus == NULL) {
2642 /* update the bar so that title/class/name will be cleared. */
2643 if (window_name_enabled || title_name_enabled ||
2644 title_class_enabled)
2650 focus_magic(winfocus);
2654 cycle_layout(struct swm_region *r, union arg *args)
2656 struct workspace *ws = r->ws;
2659 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2662 if (ws->cur_layout->l_stack == NULL)
2663 ws->cur_layout = &layouts[0];
2666 if (focus_mode == SWM_FOCUS_DEFAULT)
2667 drain_enter_notify();
2668 a.id = SWM_ARG_ID_FOCUSCUR;
2674 stack_config(struct swm_region *r, union arg *args)
2676 struct workspace *ws = r->ws;
2678 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2681 if (ws->cur_layout->l_config != NULL)
2682 ws->cur_layout->l_config(ws, args->id);
2684 if (args->id != SWM_ARG_ID_STACKINIT)
2691 struct swm_geometry g;
2692 struct swm_region *r;
2698 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2700 for (i = 0; i < ScreenCount(display); i++) {
2704 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2705 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2706 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2708 /* start with screen geometry, adjust for bar */
2710 g.w -= 2 * border_width;
2711 g.h -= 2 * border_width;
2717 r->ws->cur_layout->l_stack(r->ws, &g);
2718 r->ws->cur_layout->l_string(r->ws);
2719 /* save r so we can track region changes */
2726 if (focus_mode == SWM_FOCUS_DEFAULT)
2727 drain_enter_notify();
2729 DNPRINTF(SWM_D_STACK, "stack: end\n");
2733 store_float_geom(struct ws_win *win, struct swm_region *r)
2735 /* retain window geom and region geom */
2736 win->g_float = win->g;
2737 win->rg_float = r->g;
2738 win->g_floatvalid = 1;
2742 stack_floater(struct ws_win *win, struct swm_region *r)
2750 bzero(&wc, sizeof wc);
2751 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2754 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2755 * geom on ws switches or return from max mode
2757 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2758 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2760 * use stored g and rg to set relative position and size
2761 * as in old region or before max stack mode
2763 X(win) = win->g_float.x - win->rg_float.x + X(r);
2764 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2765 WIDTH(win) = win->g_float.w;
2766 HEIGHT(win) = win->g_float.h;
2767 win->g_floatvalid = 0;
2770 win->floatmaxed = 0;
2772 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2773 (HEIGHT(win) >= HEIGHT(r)))
2774 wc.border_width = 0;
2776 wc.border_width = border_width;
2777 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2778 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
2779 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
2784 * floaters and transients are auto-centred unless moved
2787 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
2788 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
2791 /* win can be outside r if new r smaller than old r */
2792 /* Ensure top left corner inside r (move probs otherwise) */
2793 if (X(win) < X(r) - wc.border_width)
2794 X(win) = X(r) - wc.border_width;
2795 if (X(win) > X(r) + WIDTH(r) - 1)
2796 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
2797 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
2798 if (Y(win) < Y(r) - wc.border_width)
2799 Y(win) = Y(r) - wc.border_width;
2800 if (Y(win) > Y(r) + HEIGHT(r) - 1)
2801 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
2802 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
2806 wc.width = WIDTH(win);
2807 wc.height = HEIGHT(win);
2810 * Retain floater and transient geometry for correct positioning
2811 * when ws changes region
2813 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2814 store_float_geom(win, r);
2816 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2817 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
2819 XConfigureWindow(display, win->id, mask, &wc);
2823 * Send keystrokes to terminal to decrease/increase the font size as the
2824 * window size changes.
2827 adjust_font(struct ws_win *win)
2829 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2830 win->floating || win->transient)
2833 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2834 WIDTH(win) / win->sh.width_inc < term_width &&
2835 win->font_steps < SWM_MAX_FONT_STEPS) {
2836 win->font_size_boundary[win->font_steps] =
2837 (win->sh.width_inc * term_width) + win->sh.base_width;
2840 win->last_inc = win->sh.width_inc;
2841 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2842 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2843 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
2846 win->last_inc = win->sh.width_inc;
2847 fake_keypress(win, XK_KP_Add, ShiftMask);
2851 #define SWAPXY(g) do { \
2853 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2854 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2857 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2860 XWindowAttributes wa;
2861 struct swm_geometry win_g, r_g = *g;
2862 struct ws_win *win, *fs_win = 0;
2863 int i, j, s, stacks;
2864 int w_inc = 1, h_inc, w_base = 1, h_base;
2865 int hrh, extra = 0, h_slice, last_h = 0;
2866 int split, colno, winno, mwin, msize, mscale;
2867 int remain, missing, v_slice, reconfigure;
2870 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
2871 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
2873 winno = count_win(ws, 0);
2874 if (winno == 0 && count_win(ws, 1) == 0)
2877 TAILQ_FOREACH(win, &ws->winlist, entry)
2878 if (win->transient == 0 && win->floating == 0
2879 && win->iconic == 0)
2886 w_inc = win->sh.width_inc;
2887 w_base = win->sh.base_width;
2888 mwin = ws->l_state.horizontal_mwin;
2889 mscale = ws->l_state.horizontal_msize;
2890 stacks = ws->l_state.horizontal_stacks;
2893 w_inc = win->sh.height_inc;
2894 w_base = win->sh.base_height;
2895 mwin = ws->l_state.vertical_mwin;
2896 mscale = ws->l_state.vertical_msize;
2897 stacks = ws->l_state.vertical_stacks;
2901 if (stacks > winno - mwin)
2902 stacks = winno - mwin;
2906 h_slice = r_g.h / SWM_H_SLICE;
2907 if (mwin && winno > mwin) {
2908 v_slice = r_g.w / SWM_V_SLICE;
2912 win_g.w = v_slice * mscale;
2914 if (w_inc > 1 && w_inc < v_slice) {
2915 /* adjust for window's requested size increment */
2916 remain = (win_g.w - w_base) % w_inc;
2917 missing = w_inc - remain;
2924 win_g.x += r_g.w - msize;
2927 colno = split = winno / stacks;
2928 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2929 2 * border_width) / stacks);
2931 hrh = r_g.h / colno;
2932 extra = r_g.h - (colno * hrh);
2933 win_g.h = hrh - 2 * border_width;
2935 /* stack all the tiled windows */
2936 i = j = 0, s = stacks;
2937 TAILQ_FOREACH(win, &ws->winlist, entry) {
2938 if (win->transient != 0 || win->floating != 0)
2940 if (win->iconic != 0)
2943 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2948 if (split && i == split) {
2949 colno = (winno - mwin) / stacks;
2950 if (s <= (winno - mwin) % stacks)
2952 split = split + colno;
2953 hrh = (r_g.h / colno);
2954 extra = r_g.h - (colno * hrh);
2958 win_g.x += win_g.w + 2 * border_width;
2959 win_g.w = (r_g.w - msize -
2960 (stacks * 2 * border_width)) / stacks;
2962 win_g.w += (r_g.w - msize -
2963 (stacks * 2 * border_width)) % stacks;
2967 win_g.h = hrh - 2 * border_width;
2969 h_inc = win->sh.width_inc;
2970 h_base = win->sh.base_width;
2972 h_inc = win->sh.height_inc;
2973 h_base = win->sh.base_height;
2975 if (j == colno - 1) {
2976 win_g.h = hrh + extra;
2977 } else if (h_inc > 1 && h_inc < h_slice) {
2978 /* adjust for window's requested size increment */
2979 remain = (win_g.h - h_base) % h_inc;
2980 missing = h_inc - remain;
2982 if (missing <= extra || j == 0) {
2994 win_g.y += last_h + 2 * border_width;
2996 bzero(&wc, sizeof wc);
2997 if (disable_border && bar_enabled == 0 && winno == 1){
2998 wc.border_width = 0;
2999 win_g.w += 2 * border_width;
3000 win_g.h += 2 * border_width;
3002 wc.border_width = border_width;
3005 if (X(win) != win_g.y || Y(win) != win_g.x ||
3006 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3008 X(win) = wc.x = win_g.y;
3009 Y(win) = wc.y = win_g.x;
3010 WIDTH(win) = wc.width = win_g.h;
3011 HEIGHT(win) = wc.height = win_g.w;
3014 if (X(win) != win_g.x || Y(win) != win_g.y ||
3015 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3017 X(win) = wc.x = win_g.x;
3018 Y(win) = wc.y = win_g.y;
3019 WIDTH(win) = wc.width = win_g.w;
3020 HEIGHT(win) = wc.height = win_g.h;
3025 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3026 XConfigureWindow(display, win->id, mask, &wc);
3029 if (XGetWindowAttributes(display, win->id, &wa))
3030 if (wa.map_state == IsUnmapped)
3031 XMapRaised(display, win->id);
3039 /* now, stack all the floaters and transients */
3040 TAILQ_FOREACH(win, &ws->winlist, entry) {
3041 if (win->transient == 0 && win->floating == 0)
3043 if (win->iconic == 1)
3045 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3050 stack_floater(win, ws->r);
3051 XMapRaised(display, win->id);
3055 stack_floater(fs_win, ws->r);
3056 XMapRaised(display, fs_win->id);
3061 vertical_config(struct workspace *ws, int id)
3063 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3067 case SWM_ARG_ID_STACKRESET:
3068 case SWM_ARG_ID_STACKINIT:
3069 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3070 ws->l_state.vertical_mwin = 1;
3071 ws->l_state.vertical_stacks = 1;
3073 case SWM_ARG_ID_MASTERSHRINK:
3074 if (ws->l_state.vertical_msize > 1)
3075 ws->l_state.vertical_msize--;
3077 case SWM_ARG_ID_MASTERGROW:
3078 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3079 ws->l_state.vertical_msize++;
3081 case SWM_ARG_ID_MASTERADD:
3082 ws->l_state.vertical_mwin++;
3084 case SWM_ARG_ID_MASTERDEL:
3085 if (ws->l_state.vertical_mwin > 0)
3086 ws->l_state.vertical_mwin--;
3088 case SWM_ARG_ID_STACKINC:
3089 ws->l_state.vertical_stacks++;
3091 case SWM_ARG_ID_STACKDEC:
3092 if (ws->l_state.vertical_stacks > 1)
3093 ws->l_state.vertical_stacks--;
3101 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3103 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3105 stack_master(ws, g, 0, 0);
3109 horizontal_config(struct workspace *ws, int id)
3111 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3114 case SWM_ARG_ID_STACKRESET:
3115 case SWM_ARG_ID_STACKINIT:
3116 ws->l_state.horizontal_mwin = 1;
3117 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3118 ws->l_state.horizontal_stacks = 1;
3120 case SWM_ARG_ID_MASTERSHRINK:
3121 if (ws->l_state.horizontal_msize > 1)
3122 ws->l_state.horizontal_msize--;
3124 case SWM_ARG_ID_MASTERGROW:
3125 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3126 ws->l_state.horizontal_msize++;
3128 case SWM_ARG_ID_MASTERADD:
3129 ws->l_state.horizontal_mwin++;
3131 case SWM_ARG_ID_MASTERDEL:
3132 if (ws->l_state.horizontal_mwin > 0)
3133 ws->l_state.horizontal_mwin--;
3135 case SWM_ARG_ID_STACKINC:
3136 ws->l_state.horizontal_stacks++;
3138 case SWM_ARG_ID_STACKDEC:
3139 if (ws->l_state.horizontal_stacks > 1)
3140 ws->l_state.horizontal_stacks--;
3148 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3150 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3152 stack_master(ws, g, 1, 0);
3155 /* fullscreen view */
3157 max_stack(struct workspace *ws, struct swm_geometry *g)
3160 struct swm_geometry gg = *g;
3161 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3165 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3170 winno = count_win(ws, 0);
3171 if (winno == 0 && count_win(ws, 1) == 0)
3174 TAILQ_FOREACH(win, &ws->winlist, entry) {
3175 if (win->transient) {
3177 parent = find_window(win->transient);
3181 if (win->floating && win->floatmaxed == 0 ) {
3183 * retain geometry for retrieval on exit from
3186 store_float_geom(win, ws->r);
3187 win->floatmaxed = 1;
3190 /* only reconfigure if necessary */
3191 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3192 HEIGHT(win) != gg.h) {
3193 bzero(&wc, sizeof wc);
3194 X(win) = wc.x = gg.x;
3195 Y(win) = wc.y = gg.y;
3197 wc.border_width = border_width;
3198 WIDTH(win) = wc.width = gg.w;
3199 HEIGHT(win) = wc.height = gg.h;
3201 wc.border_width = 0;
3202 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3203 HEIGHT(win) = wc.height = gg.h +
3206 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3207 XConfigureWindow(display, win->id, mask, &wc);
3209 /* unmap only if we don't have multi screen */
3210 if (win != ws->focus)
3211 if (!(ScreenCount(display) > 1 || outputs > 1))
3215 /* put the last transient on top */
3218 XMapRaised(display, parent->id);
3219 stack_floater(wintrans, ws->r);
3220 focus_magic(wintrans);
3225 send_to_ws(struct swm_region *r, union arg *args)
3227 int wsid = args->id;
3228 struct ws_win *win = NULL, *parent;
3229 struct workspace *ws, *nws;
3230 Atom ws_idx_atom = 0;
3231 unsigned char ws_idx_str[SWM_PROPLEN];
3234 if (r && r->ws && r->ws->focus)
3240 if (win->ws->idx == wsid)
3243 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3246 nws = &win->s->ws[wsid];
3248 a.id = SWM_ARG_ID_FOCUSPREV;
3250 if (win->transient) {
3251 parent = find_window(win->transient);
3253 unmap_window(parent);
3254 TAILQ_REMOVE(&ws->winlist, parent, entry);
3255 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3260 TAILQ_REMOVE(&ws->winlist, win, entry);
3261 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3262 if (TAILQ_EMPTY(&ws->winlist))
3263 r->ws->focus = NULL;
3266 /* Try to update the window's workspace property */
3267 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3269 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3271 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3273 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3274 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3281 pressbutton(struct swm_region *r, union arg *args)
3283 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3284 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3288 raise_toggle(struct swm_region *r, union arg *args)
3290 if (r && r->ws == NULL)
3293 r->ws->always_raise = !r->ws->always_raise;
3295 /* bring floaters back to top */
3296 if (r->ws->always_raise == 0)
3301 iconify(struct swm_region *r, union arg *args)
3305 if (r->ws->focus == NULL)
3307 unmap_window(r->ws->focus);
3308 update_iconic(r->ws->focus, 1);
3310 if (focus_mode == SWM_FOCUS_DEFAULT)
3311 drain_enter_notify();
3312 r->ws->focus = NULL;
3313 a.id = SWM_ARG_ID_FOCUSCUR;
3318 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3319 unsigned long *slen)
3322 unsigned long nitems, nbytes, nextra;
3323 unsigned char *prop = NULL;
3326 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3327 &rettype, &retfmt, &nitems, &nbytes, &prop);
3328 if (status != Success)
3332 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3333 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3334 if (status != Success) {
3338 if (rettype != stype) {
3347 uniconify(struct swm_region *r, union arg *args)
3351 unsigned char *name;
3355 DNPRINTF(SWM_D_MISC, "uniconify\n");
3357 if (r && r->ws == NULL)
3360 /* make sure we have anything to uniconify */
3361 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3362 if (win->ws == NULL)
3363 continue; /* should never happen */
3364 if (win->iconic == 0)
3372 search_resp_action = SWM_SEARCH_UNICONIFY;
3374 spawn_select(r, args, "search", &searchpid);
3376 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3379 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3380 if (win->ws == NULL)
3381 continue; /* should never happen */
3382 if (win->iconic == 0)
3385 name = get_win_name(display, win->id, a_netwmname,
3386 a_utf8_string, &len);
3389 fprintf(lfile, "%s.%lu\n", name, win->id);
3397 name_workspace(struct swm_region *r, union arg *args)
3401 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3407 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3409 spawn_select(r, args, "name_workspace", &searchpid);
3411 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3414 fprintf(lfile, "%s", "");
3419 search_workspace(struct swm_region *r, union arg *args)
3422 struct workspace *ws;
3425 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3431 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3433 spawn_select(r, args, "search", &searchpid);
3435 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3438 for (i = 0; i < SWM_WS_MAX; i++) {
3442 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3443 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3450 search_win_cleanup(void)
3452 struct search_window *sw = NULL;
3454 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3455 XDestroyWindow(display, sw->indicator);
3456 XFreeGC(display, sw->gc);
3457 TAILQ_REMOVE(&search_wl, sw, entry);
3463 search_win(struct swm_region *r, union arg *args)
3465 struct ws_win *win = NULL;
3466 struct search_window *sw = NULL;
3473 XRectangle ibox, lbox;
3475 DNPRINTF(SWM_D_MISC, "search_win\n");
3478 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3480 spawn_select(r, args, "search", &searchpid);
3482 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3485 TAILQ_INIT(&search_wl);
3488 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3489 if (win->iconic == 1)
3492 sw = calloc(1, sizeof(struct search_window));
3494 fprintf(stderr, "search_win: calloc: %s",
3497 search_win_cleanup();
3503 snprintf(s, sizeof s, "%d", i);
3506 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3508 w = XCreateSimpleWindow(display,
3509 win->id, 0, 0,lbox.width + 4,
3510 bar_fs_extents->max_logical_extent.height, 1,
3511 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3512 r->s->c[SWM_S_COLOR_FOCUS].color);
3515 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3517 sw->gc = XCreateGC(display, w, 0, &gcv);
3518 XMapRaised(display, w);
3519 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3521 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3522 (bar_fs_extents->max_logical_extent.height -
3523 lbox.height) / 2 - lbox.y, s, len);
3525 fprintf(lfile, "%d\n", i);
3533 search_resp_uniconify(char *resp, unsigned long len)
3535 unsigned char *name;
3539 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3541 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3542 if (win->iconic == 0)
3544 name = get_win_name(display, win->id, a_netwmname,
3545 a_utf8_string, &len);
3548 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3553 if (strncmp(s, resp, len) == 0) {
3554 /* XXX this should be a callback to generalize */
3555 update_iconic(win, 0);
3564 search_resp_name_workspace(char *resp, unsigned long len)
3566 struct workspace *ws;
3568 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3570 if (search_r->ws == NULL)
3575 free(search_r->ws->name);
3576 search_r->ws->name = NULL;
3580 ws->name = strdup(resp);
3581 if (ws->name == NULL) {
3582 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3583 "strdup: %s", strerror(errno));
3590 search_resp_search_workspace(char *resp, unsigned long len)
3597 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3601 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3608 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3610 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3617 switchws(search_r, &a);
3621 search_resp_search_window(char *resp, unsigned long len)
3626 struct search_window *sw;
3628 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3632 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3637 idx = strtonum(s, 1, INT_MAX, &errstr);
3639 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3646 TAILQ_FOREACH(sw, &search_wl, entry)
3647 if (idx == sw->idx) {
3653 #define MAX_RESP_LEN 1024
3656 search_do_resp(void)
3662 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3667 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3668 fprintf(stderr, "search: calloc\n");
3672 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3674 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3677 resp[rbytes] = '\0';
3680 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3681 * newline, so work around that by sanitizing the resp now.
3683 resp[strcspn(resp, "\n")] = '\0';
3686 switch (search_resp_action) {
3687 case SWM_SEARCH_UNICONIFY:
3688 search_resp_uniconify(resp, len);
3690 case SWM_SEARCH_NAME_WORKSPACE:
3691 search_resp_name_workspace(resp, len);
3693 case SWM_SEARCH_SEARCH_WORKSPACE:
3694 search_resp_search_workspace(resp, len);
3696 case SWM_SEARCH_SEARCH_WINDOW:
3697 search_resp_search_window(resp, len);
3702 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3703 search_win_cleanup();
3705 search_resp_action = SWM_SEARCH_NONE;
3706 close(select_resp_pipe[0]);
3711 wkill(struct swm_region *r, union arg *args)
3713 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3715 if (r->ws->focus == NULL)
3718 if (args->id == SWM_ARG_ID_KILLWINDOW)
3719 XKillClient(display, r->ws->focus->id);
3721 if (r->ws->focus->can_delete)
3722 client_msg(r->ws->focus, adelete);
3727 floating_toggle_win(struct ws_win *win)
3729 struct swm_region *r;
3739 /* reject floating toggles in max stack mode */
3740 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3743 if (win->floating) {
3744 if (!win->floatmaxed) {
3745 /* retain position for refloat */
3746 store_float_geom(win, r);
3750 if (win->g_floatvalid) {
3751 /* refloat at last floating relative position */
3752 X(win) = win->g_float.x - win->rg_float.x + X(r);
3753 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3754 WIDTH(win) = win->g_float.w;
3755 HEIGHT(win) = win->g_float.h;
3760 ewmh_update_actions(win);
3766 floating_toggle(struct swm_region *r, union arg *args)
3768 struct ws_win *win = r->ws->focus;
3774 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3775 _NET_WM_STATE_TOGGLE);
3778 if (focus_mode == SWM_FOCUS_DEFAULT)
3779 drain_enter_notify();
3781 if (win == win->ws->focus) {
3782 a.id = SWM_ARG_ID_FOCUSCUR;
3783 focus(win->ws->r, &a);
3788 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3790 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
3792 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
3794 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
3797 if (X(win) < X(r) - border_width) {
3799 WIDTH(win) -= X(r) - X(win) - border_width;
3801 X(win) = X(r) - border_width;
3804 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
3806 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
3808 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
3811 if (Y(win) < Y(r) - border_width) {
3813 HEIGHT(win) -= Y(r) - Y(win) - border_width;
3815 Y(win) = Y(r) - border_width;
3820 if (HEIGHT(win) < 1)
3825 update_window(struct ws_win *win)
3830 bzero(&wc, sizeof wc);
3831 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
3832 wc.border_width = border_width;
3835 wc.width = WIDTH(win);
3836 wc.height = HEIGHT(win);
3838 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
3839 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3841 XConfigureWindow(display, win->id, mask, &wc);
3844 #define SWM_RESIZE_STEPS (50)
3847 resize(struct ws_win *win, union arg *args)
3851 struct swm_region *r = NULL;
3852 int resize_step = 0;
3856 struct swm_geometry g;
3857 int top = 0, left = 0;
3860 unsigned int shape; /* cursor style */
3866 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
3867 "transient: 0x%lx\n", win->id, YESNO(win->floating),
3870 if (!(win->transient != 0 || win->floating != 0))
3873 /* reject resizes in max mode for floaters (transient ok) */
3874 if (win->floatmaxed)
3878 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3884 case SWM_ARG_ID_WIDTHSHRINK:
3885 WIDTH(win) -= SWM_RESIZE_STEPS;
3888 case SWM_ARG_ID_WIDTHGROW:
3889 WIDTH(win) += SWM_RESIZE_STEPS;
3892 case SWM_ARG_ID_HEIGHTSHRINK:
3893 HEIGHT(win) -= SWM_RESIZE_STEPS;
3896 case SWM_ARG_ID_HEIGHTGROW:
3897 HEIGHT(win) += SWM_RESIZE_STEPS;
3904 constrain_window(win, r, 1);
3906 store_float_geom(win,r);
3910 if (focus_mode == SWM_FOCUS_DEFAULT)
3911 drain_enter_notify();
3913 /* get cursor offset from window root */
3914 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
3919 if (wx < WIDTH(win) / 2)
3922 if (wy < HEIGHT(win) / 2)
3925 if (args->id == SWM_ARG_ID_CENTER)
3928 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
3930 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
3932 cursor = XCreateFontCursor(display, shape);
3934 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3935 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
3936 XFreeCursor(display, cursor);
3941 XMaskEvent(display, MOUSEMASK | ExposureMask |
3942 SubstructureRedirectMask, &ev);
3944 case ConfigureRequest:
3947 handler[ev.type](&ev);
3950 /* cursor offset/delta from start of the operation */
3951 dx = ev.xmotion.x_root - x;
3952 dy = ev.xmotion.y_root - y;
3958 if (args->id == SWM_ARG_ID_CENTER) {
3959 if (g.h / 2 + dy < 1)
3963 HEIGHT(win) = g.h + 2 * dy;
3971 HEIGHT(win) = g.h + dy;
3978 if (args->id == SWM_ARG_ID_CENTER) {
3979 if (g.w / 2 + dx < 1)
3983 WIDTH(win) = g.w + 2 * dx;
3991 WIDTH(win) = g.w + dx;
3994 constrain_window(win, r, 1);
3996 /* not free, don't sync more than 120 times / second */
3997 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3998 time = ev.xmotion.time;
3999 XSync(display, False);
4004 } while (ev.type != ButtonRelease);
4006 XSync(display, False);
4009 store_float_geom(win,r);
4011 XUngrabPointer(display, CurrentTime);
4012 XFreeCursor(display, cursor);
4015 drain_enter_notify();
4019 resize_step(struct swm_region *r, union arg *args)
4021 struct ws_win *win = NULL;
4023 if (r && r->ws && r->ws->focus)
4031 #define SWM_MOVE_STEPS (50)
4034 move(struct ws_win *win, union arg *args)
4039 struct swm_region *r = NULL;
4049 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4050 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4052 /* in max_stack mode should only move transients */
4053 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4057 if (win->floating == 0 && !win->transient) {
4058 store_float_geom(win,r);
4059 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4062 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4069 case SWM_ARG_ID_MOVELEFT:
4070 X(win) -= (SWM_MOVE_STEPS - border_width);
4073 case SWM_ARG_ID_MOVERIGHT:
4074 X(win) += (SWM_MOVE_STEPS - border_width);
4077 case SWM_ARG_ID_MOVEUP:
4078 Y(win) -= (SWM_MOVE_STEPS - border_width);
4081 case SWM_ARG_ID_MOVEDOWN:
4082 Y(win) += (SWM_MOVE_STEPS - border_width);
4089 constrain_window(win, r, 0);
4091 store_float_geom(win, r);
4095 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4096 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4097 CurrentTime) != GrabSuccess)
4100 /* get cursor offset from window root */
4101 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4105 XMaskEvent(display, MOUSEMASK | ExposureMask |
4106 SubstructureRedirectMask, &ev);
4108 case ConfigureRequest:
4111 handler[ev.type](&ev);
4114 X(win) = ev.xmotion.x_root - wx - border_width;
4115 Y(win) = ev.xmotion.y_root - wy - border_width;
4117 constrain_window(win, r, 0);
4119 /* not free, don't sync more than 120 times / second */
4120 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4121 time = ev.xmotion.time;
4122 XSync(display, False);
4127 } while (ev.type != ButtonRelease);
4129 XSync(display, False);
4132 store_float_geom(win,r);
4133 XUngrabPointer(display, CurrentTime);
4136 drain_enter_notify();
4140 move_step(struct swm_region *r, union arg *args)
4142 struct ws_win *win = NULL;
4144 if (r && r->ws && r->ws->focus)
4149 if (!(win->transient != 0 || win->floating != 0))
4156 /* user/key callable function IDs */
4226 kf_search_workspace,
4228 kf_dumpwins, /* MUST BE LAST */
4232 /* key definitions */
4234 dummykeyfunc(struct swm_region *r, union arg *args)
4239 legacyfunc(struct swm_region *r, union arg *args)
4244 char name[SWM_FUNCNAME_LEN];
4245 void (*func)(struct swm_region *r, union arg *);
4247 } keyfuncs[kf_invalid + 1] = {
4248 /* name function argument */
4249 { "cycle_layout", cycle_layout, {0} },
4250 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4251 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4252 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4253 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4254 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4255 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4256 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4257 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4258 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4259 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4260 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4261 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4262 { "spawn_term", spawnterm, {.argv = spawn_term} },
4263 { "spawn_menu", legacyfunc, {0} },
4264 { "quit", quit, {0} },
4265 { "restart", restart, {0} },
4266 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4267 { "ws_1", switchws, {.id = 0} },
4268 { "ws_2", switchws, {.id = 1} },
4269 { "ws_3", switchws, {.id = 2} },
4270 { "ws_4", switchws, {.id = 3} },
4271 { "ws_5", switchws, {.id = 4} },
4272 { "ws_6", switchws, {.id = 5} },
4273 { "ws_7", switchws, {.id = 6} },
4274 { "ws_8", switchws, {.id = 7} },
4275 { "ws_9", switchws, {.id = 8} },
4276 { "ws_10", switchws, {.id = 9} },
4277 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4278 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4279 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4280 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4281 { "ws_prior", priorws, {0} },
4282 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4283 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4284 { "mvws_1", send_to_ws, {.id = 0} },
4285 { "mvws_2", send_to_ws, {.id = 1} },
4286 { "mvws_3", send_to_ws, {.id = 2} },
4287 { "mvws_4", send_to_ws, {.id = 3} },
4288 { "mvws_5", send_to_ws, {.id = 4} },
4289 { "mvws_6", send_to_ws, {.id = 5} },
4290 { "mvws_7", send_to_ws, {.id = 6} },
4291 { "mvws_8", send_to_ws, {.id = 7} },
4292 { "mvws_9", send_to_ws, {.id = 8} },
4293 { "mvws_10", send_to_ws, {.id = 9} },
4294 { "bar_toggle", bar_toggle, {0} },
4295 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4296 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4297 { "screenshot_all", legacyfunc, {0} },
4298 { "screenshot_wind", legacyfunc, {0} },
4299 { "float_toggle", floating_toggle,{0} },
4300 { "version", version, {0} },
4301 { "spawn_lock", legacyfunc, {0} },
4302 { "spawn_initscr", legacyfunc, {0} },
4303 { "spawn_custom", dummykeyfunc, {0} },
4304 { "iconify", iconify, {0} },
4305 { "uniconify", uniconify, {0} },
4306 { "raise_toggle", raise_toggle, {0} },
4307 { "button2", pressbutton, {2} },
4308 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4309 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4310 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4311 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4312 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4313 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4314 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4315 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4316 { "name_workspace", name_workspace, {0} },
4317 { "search_workspace", search_workspace, {0} },
4318 { "search_win", search_win, {0} },
4319 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4320 { "invalid key func", NULL, {0} },
4323 TAILQ_ENTRY(key) entry;
4326 enum keyfuncid funcid;
4329 TAILQ_HEAD(key_list, key);
4330 struct key_list keys = TAILQ_HEAD_INITIALIZER(keys);
4333 enum { client_click, root_click };
4335 unsigned int action;
4337 unsigned int button;
4338 void (*func)(struct ws_win *, union arg *);
4341 /* action key mouse button func args */
4342 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4343 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4344 { client_click, MODKEY, Button1, move, {0} },
4348 update_modkey(unsigned int mod)
4354 TAILQ_FOREACH(kp, &keys, entry)
4355 if (kp->mod & ShiftMask)
4356 kp->mod = mod | ShiftMask;
4360 for (i = 0; i < LENGTH(buttons); i++)
4361 if (buttons[i].mask & ShiftMask)
4362 buttons[i].mask = mod | ShiftMask;
4364 buttons[i].mask = mod;
4374 int spawns_size = 0, spawns_length = 0;
4375 struct spawn_prog *spawns = NULL;
4378 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4381 struct spawn_prog *prog = NULL;
4383 char *ap, **real_args;
4385 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4388 for (i = 0; i < spawns_length; i++) {
4389 if (!strcasecmp(spawn_name, spawns[i].name))
4393 fprintf(stderr, "spawn_custom: program %s not found\n",
4398 /* make room for expanded args */
4399 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4400 err(1, "spawn_custom: calloc real_args");
4402 /* expand spawn_args into real_args */
4403 for (i = 0; i < prog->argc; i++) {
4405 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4406 if (!strcasecmp(ap, "$bar_border")) {
4408 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4410 err(1, "spawn_custom border color");
4411 } else if (!strcasecmp(ap, "$bar_color")) {
4413 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4415 err(1, "spawn_custom bar color");
4416 } else if (!strcasecmp(ap, "$bar_font")) {
4417 if ((real_args[i] = strdup(bar_fonts))
4419 err(1, "spawn_custom bar fonts");
4420 } else if (!strcasecmp(ap, "$bar_font_color")) {
4422 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4424 err(1, "spawn_custom color font");
4425 } else if (!strcasecmp(ap, "$color_focus")) {
4427 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4429 err(1, "spawn_custom color focus");
4430 } else if (!strcasecmp(ap, "$color_unfocus")) {
4432 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4434 err(1, "spawn_custom color unfocus");
4436 /* no match --> copy as is */
4437 if ((real_args[i] = strdup(ap)) == NULL)
4438 err(1, "spawn_custom strdup(ap)");
4440 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4445 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4446 for (i = 0; i < prog->argc; i++)
4447 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4448 DNPRINTF(SWM_D_SPAWN, "\n");
4450 *ret_args = real_args;
4451 return (prog->argc);
4455 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4461 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4465 spawn(r->ws->idx, &a, 1);
4467 for (i = 0; i < spawn_argc; i++)
4473 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4479 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4483 if (pipe(select_list_pipe) == -1)
4484 err(1, "pipe error");
4485 if (pipe(select_resp_pipe) == -1)
4486 err(1, "pipe error");
4488 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4489 err(1, "could not disable SIGPIPE");
4490 switch (*pid = fork()) {
4492 err(1, "cannot fork");
4495 if (dup2(select_list_pipe[0], 0) == -1)
4497 if (dup2(select_resp_pipe[1], 1) == -1)
4499 close(select_list_pipe[1]);
4500 close(select_resp_pipe[0]);
4501 spawn(r->ws->idx, &a, 0);
4503 default: /* parent */
4504 close(select_list_pipe[0]);
4505 close(select_resp_pipe[1]);
4509 for (i = 0; i < spawn_argc; i++)
4515 setspawn(struct spawn_prog *prog)
4519 if (prog == NULL || prog->name == NULL)
4523 for (i = 0; i < spawns_length; i++) {
4524 if (!strcmp(spawns[i].name, prog->name)) {
4526 if (prog->argv == NULL) {
4528 DNPRINTF(SWM_D_SPAWN,
4529 "setspawn: delete #%d: %s\n",
4531 free(spawns[i].name);
4532 for (j = 0; j < spawns[i].argc; j++)
4533 free(spawns[i].argv[j]);
4534 free(spawns[i].argv);
4535 j = spawns_length - 1;
4537 spawns[i] = spawns[j];
4542 DNPRINTF(SWM_D_SPAWN,
4543 "setspawn: replace #%d: %s\n",
4545 free(spawns[i].name);
4546 for (j = 0; j < spawns[i].argc; j++)
4547 free(spawns[i].argv[j]);
4548 free(spawns[i].argv);
4551 /* found case handled */
4557 if (prog->argv == NULL) {
4559 "error: setspawn: cannot find program: %s", prog->name);
4564 /* not found: add */
4565 if (spawns_size == 0 || spawns == NULL) {
4567 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4568 spawns = malloc((size_t)spawns_size *
4569 sizeof(struct spawn_prog));
4570 if (spawns == NULL) {
4571 fprintf(stderr, "setspawn: malloc failed\n");
4575 } else if (spawns_length == spawns_size) {
4577 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4578 spawns = realloc(spawns, (size_t)spawns_size *
4579 sizeof(struct spawn_prog));
4580 if (spawns == NULL) {
4581 fprintf(stderr, "setspawn: realloc failed\n");
4587 if (spawns_length < spawns_size) {
4588 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4589 spawns_length, prog->name);
4590 i = spawns_length++;
4593 fprintf(stderr, "spawns array problem?\n");
4594 if (spawns == NULL) {
4595 fprintf(stderr, "spawns array is NULL!\n");
4603 setconfspawn(char *selector, char *value, int flags)
4605 struct spawn_prog *prog;
4606 char *vp, *cp, *word;
4608 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4609 if ((prog = calloc(1, sizeof *prog)) == NULL)
4610 err(1, "setconfspawn: calloc prog");
4611 prog->name = strdup(selector);
4612 if (prog->name == NULL)
4613 err(1, "setconfspawn prog->name");
4614 if ((cp = vp = strdup(value)) == NULL)
4615 err(1, "setconfspawn: strdup(value) ");
4616 while ((word = strsep(&cp, " \t")) != NULL) {
4617 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4619 cp += (long)strspn(cp, " \t");
4620 if (strlen(word) > 0) {
4622 if ((prog->argv = realloc(prog->argv,
4623 prog->argc * sizeof(char *))) == NULL)
4624 err(1, "setconfspawn: realloc");
4625 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4626 err(1, "setconfspawn: strdup");
4633 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4640 setconfspawn("term", "xterm", 0);
4641 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4642 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4643 setconfspawn("lock", "xlock", 0);
4644 setconfspawn("initscr", "initscreen.sh", 0);
4645 setconfspawn("menu", "dmenu_run"
4648 " -nf $bar_font_color"
4650 " -sf $bar_color", 0);
4651 setconfspawn("search", "dmenu"
4655 " -nf $bar_font_color"
4657 " -sf $bar_color", 0);
4658 setconfspawn("name_workspace", "dmenu"
4662 " -nf $bar_font_color"
4664 " -sf $bar_color", 0);
4668 #define SWM_MODNAME_SIZE 32
4669 #define SWM_KEY_WS "\n+ \t"
4671 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4675 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4676 if (mod == NULL || ks == NULL) {
4677 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4680 if (keystr == NULL || strlen(keystr) == 0) {
4681 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4687 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4688 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4690 cp += (long)strspn(cp, SWM_KEY_WS);
4691 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4693 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4695 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4697 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4699 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4701 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4703 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4704 *mod |= ControlMask;
4706 *ks = XStringToKeysym(name);
4707 XConvertCase(*ks, ks, &uks);
4708 if (ks == NoSymbol) {
4710 "parsekeys: invalid key %s\n",
4716 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4721 strdupsafe(char *str)
4726 return (strdup(str));
4730 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
4734 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
4735 keyfuncs[kfid].name, spawn_name);
4737 if ((kp = malloc(sizeof *kp)) == NULL)
4738 err(1, "key_insert: malloc");
4743 kp->spawn_name = strdupsafe(spawn_name);
4744 TAILQ_INSERT_TAIL(&keys, kp, entry);
4746 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
4750 key_remove(struct key *kp)
4752 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
4754 TAILQ_REMOVE(&keys, kp, entry);
4755 free(kp->spawn_name);
4758 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
4762 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
4765 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
4769 key_insert(mod, ks, kfid, spawn_name);
4771 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
4775 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4780 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4781 keyfuncs[kfid].name, spawn_name);
4783 TAILQ_FOREACH(kp, &keys, entry) {
4784 if (kp->mod == mod && kp->keysym == ks) {
4785 if (kfid == kf_invalid)
4788 key_replace(kp, mod, ks, kfid, spawn_name);
4789 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4793 if (kfid == kf_invalid) {
4794 warnx("error: setkeybinding: cannot find mod/key combination");
4795 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4799 key_insert(mod, ks, kfid, spawn_name);
4800 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4804 setconfbinding(char *selector, char *value, int flags)
4806 enum keyfuncid kfid;
4810 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4811 if (selector == NULL) {
4812 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4813 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4815 setkeybinding(mod, ks, kfid, NULL);
4820 /* search by key function name */
4821 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4822 if (strncasecmp(selector, keyfuncs[kfid].name,
4823 SWM_FUNCNAME_LEN) == 0) {
4824 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4826 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4827 setkeybinding(mod, ks, kfid, NULL);
4833 /* search by custom spawn name */
4834 for (i = 0; i < spawns_length; i++) {
4835 if (strcasecmp(selector, spawns[i].name) == 0) {
4836 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4838 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4839 setkeybinding(mod, ks, kf_spawn_custom,
4846 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4853 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4854 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4855 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4856 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4857 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4858 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4859 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4860 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4861 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4862 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4863 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4864 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4865 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4866 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4867 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4868 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4869 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4870 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4871 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4872 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4873 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4874 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4875 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4876 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4877 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4878 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4879 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4880 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4881 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4882 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4883 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4884 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4885 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4886 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4887 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4888 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4889 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4890 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4891 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4892 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4893 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4894 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4895 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4896 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4897 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4898 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4899 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4900 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4901 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4902 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4903 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4904 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4905 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4906 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4907 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4908 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4909 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4910 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4911 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4912 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4913 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4914 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4915 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4916 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4917 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4918 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4919 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4920 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4921 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4922 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4923 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4925 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4932 struct key *kp_loop, *kp_next;
4934 kp_loop = TAILQ_FIRST(&keys);
4935 while (kp_loop != NULL) {
4936 kp_next = TAILQ_NEXT(kp_loop, entry);
4937 key_remove(kp_loop);
4943 setkeymapping(char *selector, char *value, int flags)
4945 char keymapping_file[PATH_MAX];
4946 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4947 if (value[0] == '~')
4948 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4949 pwd->pw_dir, &value[1]);
4951 strlcpy(keymapping_file, value, sizeof keymapping_file);
4953 /* load new key bindings; if it fails, revert to default bindings */
4954 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4958 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4963 updatenumlockmask(void)
4966 XModifierKeymap *modmap;
4968 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4970 modmap = XGetModifierMapping(display);
4971 for (i = 0; i < 8; i++)
4972 for (j = 0; j < modmap->max_keypermod; j++)
4973 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4974 == XKeysymToKeycode(display, XK_Num_Lock))
4975 numlockmask = (1 << i);
4977 XFreeModifiermap(modmap);
4985 unsigned int modifiers[] =
4986 { 0, LockMask, numlockmask, numlockmask | LockMask };
4989 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4990 updatenumlockmask();
4992 for (k = 0; k < ScreenCount(display); k++) {
4993 if (TAILQ_EMPTY(&screens[k].rl))
4995 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4996 TAILQ_FOREACH(kp, &keys, entry) {
4997 if ((code = XKeysymToKeycode(display, kp->keysym)))
4998 for (j = 0; j < LENGTH(modifiers); j++)
4999 XGrabKey(display, code,
5000 kp->mod | modifiers[j],
5001 screens[k].root, True,
5002 GrabModeAsync, GrabModeAsync);
5008 grabbuttons(struct ws_win *win, int focused)
5011 unsigned int modifiers[] =
5012 { 0, LockMask, numlockmask, numlockmask|LockMask };
5014 updatenumlockmask();
5015 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5017 for (i = 0; i < LENGTH(buttons); i++)
5018 if (buttons[i].action == client_click)
5019 for (j = 0; j < LENGTH(modifiers); j++)
5020 XGrabButton(display, buttons[i].button,
5021 buttons[i].mask | modifiers[j],
5022 win->id, False, BUTTONMASK,
5023 GrabModeAsync, GrabModeSync, None,
5026 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5027 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5030 const char *quirkname[] = {
5031 "NONE", /* config string for "no value" */
5040 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5041 #define SWM_Q_WS "\n|+ \t"
5043 parsequirks(char *qstr, unsigned long *quirk)
5053 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5055 cp += (long)strspn(cp, SWM_Q_WS);
5056 for (i = 0; i < LENGTH(quirkname); i++) {
5057 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5058 DNPRINTF(SWM_D_QUIRK,
5059 "parsequirks: %s\n", name);
5064 *quirk |= 1 << (i-1);
5068 if (i >= LENGTH(quirkname)) {
5069 DNPRINTF(SWM_D_QUIRK,
5070 "parsequirks: invalid quirk [%s]\n", name);
5078 quirk_insert(const char *class, const char *name, unsigned long quirk)
5082 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5085 if ((qp = malloc(sizeof *qp)) == NULL)
5086 err(1, "quirk_insert: malloc");
5087 if ((qp->class = strdup(class)) == NULL)
5088 err(1, "quirk_insert: strdup");
5089 if ((qp->name = strdup(name)) == NULL)
5090 err(1, "quirk_insert: strdup");
5093 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5095 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5099 quirk_remove(struct quirk *qp)
5101 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5102 qp->name, qp->quirk);
5104 TAILQ_REMOVE(&quirks, qp, entry);
5109 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5113 quirk_replace(struct quirk *qp, const char *class, const char *name,
5114 unsigned long quirk)
5116 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5117 qp->name, qp->quirk);
5120 quirk_insert(class, name, quirk);
5122 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5126 setquirk(const char *class, const char *name, unsigned long quirk)
5130 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5133 TAILQ_FOREACH(qp, &quirks, entry) {
5134 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5138 quirk_replace(qp, class, name, quirk);
5139 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5144 warnx("error: setquirk: cannot find class/name combination");
5148 quirk_insert(class, name, quirk);
5149 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5153 setconfquirk(char *selector, char *value, int flags)
5155 char *cp, *class, *name;
5157 unsigned long quirks;
5158 if (selector == NULL)
5160 if ((cp = strchr(selector, ':')) == NULL)
5165 if ((retval = parsequirks(value, &quirks)) == 0)
5166 setquirk(class, name, quirks);
5173 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5174 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5175 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5176 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5177 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5178 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5179 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5180 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5181 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5182 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5183 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5184 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5185 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5188 /* conf file stuff */
5189 #define SWM_CONF_FILE "scrotwm.conf"
5191 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5192 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5193 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5194 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5195 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5196 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5197 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5198 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5199 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5203 setconfvalue(char *selector, char *value, int flags)
5209 case SWM_S_BAR_DELAY:
5210 bar_delay = atoi(value);
5212 case SWM_S_BAR_ENABLED:
5213 bar_enabled = atoi(value);
5215 case SWM_S_BAR_BORDER_WIDTH:
5216 bar_border_width = atoi(value);
5218 case SWM_S_BAR_AT_BOTTOM:
5219 bar_at_bottom = atoi(value);
5221 case SWM_S_BAR_JUSTIFY:
5222 if (!strcmp(value, "left"))
5223 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5224 else if (!strcmp(value, "center"))
5225 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5226 else if (!strcmp(value, "right"))
5227 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5229 errx(1, "invalid bar_justify");
5231 case SWM_S_STACK_ENABLED:
5232 stack_enabled = atoi(value);
5234 case SWM_S_CLOCK_ENABLED:
5235 clock_enabled = atoi(value);
5237 case SWM_S_CLOCK_FORMAT:
5238 #ifndef SWM_DENY_CLOCK_FORMAT
5240 if ((clock_format = strdup(value)) == NULL)
5241 err(1, "setconfvalue: clock_format");
5244 case SWM_S_CYCLE_EMPTY:
5245 cycle_empty = atoi(value);
5247 case SWM_S_CYCLE_VISIBLE:
5248 cycle_visible = atoi(value);
5250 case SWM_S_SS_ENABLED:
5251 ss_enabled = atoi(value);
5253 case SWM_S_TERM_WIDTH:
5254 term_width = atoi(value);
5256 case SWM_S_TITLE_CLASS_ENABLED:
5257 title_class_enabled = atoi(value);
5259 case SWM_S_WINDOW_NAME_ENABLED:
5260 window_name_enabled = atoi(value);
5262 case SWM_S_TITLE_NAME_ENABLED:
5263 title_name_enabled = atoi(value);
5265 case SWM_S_URGENT_ENABLED:
5266 urgent_enabled = atoi(value);
5268 case SWM_S_FOCUS_MODE:
5269 if (!strcmp(value, "default"))
5270 focus_mode = SWM_FOCUS_DEFAULT;
5271 else if (!strcmp(value, "follow_cursor"))
5272 focus_mode = SWM_FOCUS_FOLLOW;
5273 else if (!strcmp(value, "synergy"))
5274 focus_mode = SWM_FOCUS_SYNERGY;
5276 errx(1, "focus_mode");
5278 case SWM_S_DISABLE_BORDER:
5279 disable_border = atoi(value);
5281 case SWM_S_BORDER_WIDTH:
5282 border_width = atoi(value);
5284 case SWM_S_BAR_FONT:
5286 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5287 err(1, "setconfvalue: asprintf: failed to allocate "
5288 "memory for bar_fonts.");
5292 case SWM_S_BAR_ACTION:
5294 if ((bar_argv[0] = strdup(value)) == NULL)
5295 err(1, "setconfvalue: bar_action");
5297 case SWM_S_SPAWN_TERM:
5298 free(spawn_term[0]);
5299 if ((spawn_term[0] = strdup(value)) == NULL)
5300 err(1, "setconfvalue: spawn_term");
5304 case SWM_S_DIALOG_RATIO:
5305 dialog_ratio = atof(value);
5306 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5309 case SWM_S_VERBOSE_LAYOUT:
5310 verbose_layout = atoi(value);
5311 for (i = 0; layouts[i].l_stack != NULL; i++) {
5313 layouts[i].l_string = fancy_stacker;
5315 layouts[i].l_string = plain_stacker;
5325 setconfmodkey(char *selector, char *value, int flags)
5327 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5328 update_modkey(Mod1Mask);
5329 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5330 update_modkey(Mod2Mask);
5331 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5332 update_modkey(Mod3Mask);
5333 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5334 update_modkey(Mod4Mask);
5341 setconfcolor(char *selector, char *value, int flags)
5343 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5348 setconfregion(char *selector, char *value, int flags)
5350 custom_region(value);
5355 setautorun(char *selector, char *value, int flags)
5365 if (getenv("SWM_STARTED"))
5369 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5370 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5372 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5373 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5376 * This is a little intricate
5378 * If the pid already exists we simply reuse it because it means it was
5379 * used before AND not claimed by manage_window. We get away with
5380 * altering it in the parent after INSERT because this can not be a race
5383 while ((ap = strsep(&sp, " \t")) != NULL) {
5386 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5388 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5389 err(1, "setautorun: realloc");
5390 a.argv[argc - 1] = ap;
5393 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5394 err(1, "setautorun: realloc");
5395 a.argv[argc] = NULL;
5397 if ((pid = fork()) == 0) {
5398 spawn(ws_id, &a, 1);
5407 p = calloc(1, sizeof *p);
5410 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5420 setlayout(char *selector, char *value, int flags)
5422 int ws_id, i, x, mg, ma, si, raise;
5423 int st = SWM_V_STACK;
5425 struct workspace *ws;
5427 if (getenv("SWM_STARTED"))
5431 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5432 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5433 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5434 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5437 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5438 errx(1, "layout: invalid workspace %d", ws_id + 1);
5440 if (!strcasecmp(s, "vertical"))
5442 else if (!strcasecmp(s, "horizontal"))
5444 else if (!strcasecmp(s, "fullscreen"))
5447 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5448 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5451 for (i = 0; i < ScreenCount(display); i++) {
5452 ws = (struct workspace *)&screens[i].ws;
5453 ws[ws_id].cur_layout = &layouts[st];
5455 ws[ws_id].always_raise = raise;
5456 if (st == SWM_MAX_STACK)
5460 for (x = 0; x < abs(mg); x++) {
5461 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5462 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5463 SWM_ARG_ID_MASTERSHRINK);
5467 for (x = 0; x < abs(ma); x++) {
5468 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5469 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5470 SWM_ARG_ID_MASTERDEL);
5474 for (x = 0; x < abs(si); x++) {
5475 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5476 si >= 0 ? SWM_ARG_ID_STACKINC :
5477 SWM_ARG_ID_STACKDEC);
5485 /* config options */
5486 struct config_option {
5488 int (*func)(char*, char*, int);
5491 struct config_option configopt[] = {
5492 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5493 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5494 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5495 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5496 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5497 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5498 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5499 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5500 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5501 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5502 { "keyboard_mapping", setkeymapping, 0 },
5503 { "bind", setconfbinding, 0 },
5504 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5505 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5506 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5507 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5508 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5509 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5510 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5511 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5512 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5513 { "modkey", setconfmodkey, 0 },
5514 { "program", setconfspawn, 0 },
5515 { "quirk", setconfquirk, 0 },
5516 { "region", setconfregion, 0 },
5517 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5518 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5519 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5520 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5521 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5522 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5523 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5524 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5525 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5526 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5527 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5528 { "autorun", setautorun, 0 },
5529 { "layout", setlayout, 0 },
5534 conf_load(char *filename, int keymapping)
5537 char *line, *cp, *optsub, *optval;
5538 size_t linelen, lineno = 0;
5539 int wordlen, i, optind;
5540 struct config_option *opt;
5542 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5544 if (filename == NULL) {
5545 fprintf(stderr, "conf_load: no filename\n");
5548 if ((config = fopen(filename, "r")) == NULL) {
5549 warn("conf_load: fopen: %s", filename);
5553 while (!feof(config)) {
5554 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5557 err(1, "%s", filename);
5562 cp += strspn(cp, " \t\n"); /* eat whitespace */
5563 if (cp[0] == '\0') {
5568 /* get config option */
5569 wordlen = strcspn(cp, "=[ \t\n");
5571 warnx("%s: line %zd: no option found",
5576 for (i = 0; i < LENGTH(configopt); i++) {
5577 opt = &configopt[i];
5578 if (!strncasecmp(cp, opt->optname, wordlen) &&
5579 strlen(opt->optname) == wordlen) {
5585 warnx("%s: line %zd: unknown option %.*s",
5586 filename, lineno, wordlen, cp);
5589 if (keymapping && strcmp(opt->optname, "bind")) {
5590 warnx("%s: line %zd: invalid option %.*s",
5591 filename, lineno, wordlen, cp);
5595 cp += strspn(cp, " \t\n"); /* eat whitespace */
5596 /* get [selector] if any */
5600 wordlen = strcspn(cp, "]");
5603 warnx("%s: line %zd: syntax error",
5608 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5610 warnx("%s: line %zd: unable to allocate"
5611 "memory for selector", filename,
5617 cp += strspn(cp, "] \t\n"); /* eat trailing */
5619 cp += strspn(cp, "= \t\n"); /* eat trailing */
5621 optval = strdup(cp);
5622 /* call function to deal with it all */
5623 if (configopt[optind].func(optsub, optval,
5624 configopt[optind].funcflags) != 0) {
5625 fprintf(stderr, "%s line %zd: %s\n",
5626 filename, lineno, line);
5627 errx(1, "%s: line %zd: invalid data for %s",
5628 filename, lineno, configopt[optind].optname);
5636 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5643 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
5649 set_child_transient(struct ws_win *win, Window *trans)
5651 struct ws_win *parent, *w;
5652 XWMHints *wmh = NULL;
5653 struct swm_region *r;
5654 struct workspace *ws;
5656 parent = find_window(win->transient);
5658 parent->child_trans = win;
5660 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5661 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
5663 if (win->hints == NULL) {
5664 fprintf(stderr, "no hints for 0x%lx\n", win->id);
5668 r = root_to_region(win->wa.root);
5670 /* parent doen't exist in our window list */
5671 TAILQ_FOREACH(w, &ws->winlist, entry) {
5675 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5676 fprintf(stderr, "can't get hints for 0x%lx\n",
5681 if (win->hints->window_group != wmh->window_group)
5684 w->child_trans = win;
5685 win->transient = w->id;
5687 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5688 "transient to 0x%lx\n", win->transient);
5698 window_get_pid(Window win)
5700 Atom actual_type_return;
5701 int actual_format_return = 0;
5702 unsigned long nitems_return = 0;
5703 unsigned long bytes_after_return = 0;
5707 unsigned char *prop = NULL;
5709 if (XGetWindowProperty(display, win,
5710 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5711 XA_CARDINAL, &actual_type_return, &actual_format_return,
5712 &nitems_return, &bytes_after_return,
5713 (unsigned char**)(void*)&pid) != Success)
5715 if (actual_type_return != XA_CARDINAL)
5726 if (XGetWindowProperty(display, win,
5727 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5728 XA_STRING, &actual_type_return, &actual_format_return,
5729 &nitems_return, &bytes_after_return, &prop) != Success)
5731 if (actual_type_return != XA_STRING)
5736 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5737 /* ignore error because strtonum returns 0 anyway */
5744 manage_window(Window id)
5747 struct workspace *ws;
5748 struct ws_win *win, *ww;
5749 int format, i, ws_idx, n, border_me = 0;
5750 unsigned long nitems, bytes;
5751 Atom ws_idx_atom = 0, type;
5752 Atom *prot = NULL, *pp;
5753 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5754 struct swm_region *r;
5761 if ((win = find_window(id)) != NULL)
5762 return (win); /* already being managed */
5764 /* see if we are on the unmanaged list */
5765 if ((win = find_unmanaged_window(id)) != NULL) {
5766 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
5767 "window: 0x%lx\n", win->id);
5768 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5769 if (win->transient) {
5770 set_child_transient(win, &trans);
5771 } if (trans && (ww = find_window(trans)))
5772 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5774 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5775 ewmh_update_actions(win);
5779 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5780 err(1, "manage_window: calloc: failed to allocate memory for "
5785 /* see if we need to override the workspace */
5786 p = find_pid(window_get_pid(id));
5788 /* Get all the window data in one shot */
5789 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5791 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5792 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5794 XGetWindowAttributes(display, id, &win->wa);
5795 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
5796 win->hints = XGetWMHints(display, id);
5797 XGetTransientForHint(display, id, &trans);
5799 win->transient = trans;
5800 set_child_transient(win, &trans);
5801 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
5802 "transient: 0x%lx\n", win->id, win->transient);
5805 /* get supported protocols */
5806 if (XGetWMProtocols(display, id, &prot, &n)) {
5807 for (i = 0, pp = prot; i < n; i++, pp++) {
5808 if (*pp == takefocus)
5809 win->take_focus = 1;
5811 win->can_delete = 1;
5817 win->iconic = get_iconic(win);
5820 * Figure out where to put the window. If it was previously assigned to
5821 * a workspace (either by spawn() or manually moving), and isn't
5822 * transient, * put it in the same workspace
5824 r = root_to_region(win->wa.root);
5826 ws = &r->s->ws[p->ws];
5827 TAILQ_REMOVE(&pidlist, p, entry);
5830 } else if (prop && win->transient == 0) {
5831 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
5832 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5834 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
5837 ws = &r->s->ws[ws_idx];
5840 /* this should launch transients in the same ws as parent */
5842 if ((ww = find_window(trans)) != NULL)
5849 "manage_window: fix this "
5855 /* set up the window layout */
5858 win->s = r->s; /* this never changes */
5859 if (trans && (ww = find_window(trans)))
5860 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5862 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5864 WIDTH(win) = win->wa.width;
5865 HEIGHT(win) = win->wa.height;
5868 win->g_floatvalid = 0;
5869 win->floatmaxed = 0;
5870 win->ewmh_flags = 0;
5872 /* Set window properties so we can remember this after reincarnation */
5873 if (ws_idx_atom && prop == NULL &&
5874 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5876 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
5878 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5879 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5884 ewmh_autoquirk(win);
5886 if (XGetClassHint(display, win->id, &win->ch)) {
5887 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
5888 win->ch.res_class, win->ch.res_name);
5890 /* java is retarded so treat it special */
5891 if (strstr(win->ch.res_name, "sun-awt")) {
5896 TAILQ_FOREACH(qp, &quirks, entry) {
5897 if (!strcmp(win->ch.res_class, qp->class) &&
5898 !strcmp(win->ch.res_name, qp->name)) {
5899 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
5900 "class: %s, name: %s\n", win->ch.res_class,
5902 if (qp->quirk & SWM_Q_FLOAT) {
5906 win->quirks = qp->quirk;
5911 /* alter window position if quirky */
5912 if (win->quirks & SWM_Q_ANYWHERE) {
5913 win->manual = 1; /* don't center the quirky windows */
5914 bzero(&wc, sizeof wc);
5916 if (bar_enabled && Y(win) < bar_height) {
5917 Y(win) = wc.y = bar_height;
5920 if (WIDTH(win) + X(win) > WIDTH(r)) {
5921 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
5927 /* Reset font sizes (the bruteforce way; no default keybinding). */
5928 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5929 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5930 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5931 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5932 fake_keypress(win, XK_KP_Add, ShiftMask);
5935 ewmh_get_win_state(win);
5936 ewmh_update_actions(win);
5937 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5941 bzero(&wc, sizeof wc);
5942 wc.border_width = border_width;
5943 mask = CWBorderWidth;
5944 XConfigureWindow(display, win->id, mask, &wc);
5947 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5948 PropertyChangeMask | StructureNotifyMask);
5950 /* floaters need to be mapped if they are in the current workspace */
5951 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5952 XMapRaised(display, win->id);
5958 free_window(struct ws_win *win)
5960 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
5965 /* needed for restart wm */
5966 set_win_state(win, WithdrawnState);
5968 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5970 if (win->ch.res_class)
5971 XFree(win->ch.res_class);
5972 if (win->ch.res_name)
5973 XFree(win->ch.res_name);
5978 memset(win, 0xff, sizeof *win); /* XXX kill later */
5984 unmanage_window(struct ws_win *win)
5986 struct ws_win *parent;
5991 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
5993 if (win->transient) {
5994 parent = find_window(win->transient);
5996 parent->child_trans = NULL;
5999 /* focus on root just in case */
6000 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6009 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6010 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6016 focus_magic(struct ws_win *win)
6018 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6023 if (win->child_trans) {
6024 /* win = parent & has a transient so focus on that */
6026 focus_win(win->child_trans);
6027 if (win->child_trans->take_focus)
6028 client_msg(win, takefocus);
6030 /* make sure transient hasn't dissapeared */
6031 if (validate_win(win->child_trans) == 0) {
6032 focus_win(win->child_trans);
6033 if (win->child_trans->take_focus)
6034 client_msg(win->child_trans, takefocus);
6036 win->child_trans = NULL;
6038 if (win->take_focus)
6039 client_msg(win, takefocus);
6045 if (win->take_focus)
6046 client_msg(win, takefocus);
6053 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6060 XKeyEvent *ev = &e->xkey;
6063 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
6064 TAILQ_FOREACH(kp, &keys, entry)
6065 if (keysym == kp->keysym
6066 && CLEANMASK(kp->mod) == CLEANMASK(ev->state)
6067 && keyfuncs[kp->funcid].func) {
6068 if (kp->funcid == kf_spawn_custom)
6070 root_to_region(ev->root),
6071 &(keyfuncs[kp->funcid].args),
6075 keyfuncs[kp->funcid].func(
6076 root_to_region(ev->root),
6077 &(keyfuncs[kp->funcid].args)
6083 buttonpress(XEvent *e)
6087 XButtonPressedEvent *ev = &e->xbutton;
6089 if ((win = find_window(ev->window)) == NULL)
6093 action = client_click;
6095 for (i = 0; i < LENGTH(buttons); i++)
6096 if (action == buttons[i].action && buttons[i].func &&
6097 buttons[i].button == ev->button &&
6098 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6099 buttons[i].func(win, &buttons[i].args);
6103 configurerequest(XEvent *e)
6105 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6110 if ((win = find_window(ev->window)) == NULL)
6111 if ((win = find_unmanaged_window(ev->window)) == NULL)
6114 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6115 ev->window, YESNO(new));
6118 bzero(&wc, sizeof wc);
6121 wc.width = ev->width;
6122 wc.height = ev->height;
6123 wc.border_width = ev->border_width;
6124 wc.sibling = ev->above;
6125 wc.stack_mode = ev->detail;
6126 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6128 config_win(win, ev);
6133 configurenotify(XEvent *e)
6137 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6138 e->xconfigure.window);
6140 win = find_window(e->xconfigure.window);
6142 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6146 if (focus_mode == SWM_FOCUS_DEFAULT)
6147 drain_enter_notify();
6152 destroynotify(XEvent *e)
6155 XDestroyWindowEvent *ev = &e->xdestroywindow;
6157 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6159 if ((win = find_window(ev->window)) == NULL) {
6160 if ((win = find_unmanaged_window(ev->window)) == NULL)
6166 /* make sure we focus on something */
6169 unmanage_window(win);
6171 if (focus_mode == SWM_FOCUS_DEFAULT)
6172 drain_enter_notify();
6177 enternotify(XEvent *e)
6179 XCrossingEvent *ev = &e->xcrossing;
6184 Window focus_return;
6185 int revert_to_return;
6187 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6188 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6189 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6190 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6192 if (ev->mode != NotifyNormal) {
6193 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6198 switch (focus_mode) {
6199 case SWM_FOCUS_DEFAULT:
6201 case SWM_FOCUS_FOLLOW:
6203 case SWM_FOCUS_SYNERGY:
6206 * all these checks need to be in this order because the
6207 * XCheckTypedWindowEvent relies on weeding out the previous events
6209 * making this code an option would enable a follow mouse for focus
6214 * state is set when we are switching workspaces and focus is set when
6215 * the window or a subwindow already has focus (occurs during restart).
6217 * Only honor the focus flag if last_focus_event is not FocusOut,
6218 * this allows scrotwm to continue to control focus when another
6219 * program is also playing with it.
6221 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6222 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6227 * happens when a window is created or destroyed and the border
6228 * crosses the mouse pointer and when switching ws
6230 * we need the subwindow test to see if we came from root in order
6231 * to give focus to floaters
6233 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6234 ev->subwindow == 0) {
6235 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6239 /* this window already has focus */
6240 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6241 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6245 /* this window is being deleted or moved to another ws */
6246 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6248 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6249 XPutBackEvent(display, &cne);
6253 if ((win = find_window(ev->window)) == NULL) {
6254 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6259 * In fullstack kill all enters unless they come from a different ws
6260 * (i.e. another region) or focus has been grabbed externally.
6262 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6263 last_focus_event != FocusOut) {
6264 XGetInputFocus(display, &focus_return, &revert_to_return);
6265 if ((w = find_window(focus_return)) == NULL ||
6267 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6275 if ((win = find_window(ev->window)) == NULL) {
6276 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6281 * if we have more enternotifies let them handle it in due time
6283 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6284 DNPRINTF(SWM_D_EVENT,
6285 "ignoring enternotify: got more enternotify\n");
6286 XPutBackEvent(display, &cne);
6293 /* lets us use one switch statement for arbitrary mode/detail combinations */
6294 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6297 focusevent(XEvent *e)
6301 u_int32_t mode_detail;
6302 XFocusChangeEvent *ev = &e->xfocus;
6304 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6305 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6306 ev->window, ev->mode, ev->detail);
6308 if (last_focus_event == ev->type) {
6309 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6313 last_focus_event = ev->type;
6314 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6316 switch (mode_detail) {
6317 /* synergy client focus operations */
6318 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6319 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6321 /* synergy server focus operations */
6322 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6324 /* Entering applications like rdesktop that mangle the pointer */
6325 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6327 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6328 XSetWindowBorder(display, win->id,
6329 win->ws->r->s->c[ev->type == FocusIn ?
6330 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6333 fprintf(stderr, "ignoring focusevent\n");
6334 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6341 mapnotify(XEvent *e)
6344 XMapEvent *ev = &e->xmap;
6346 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6348 win = manage_window(ev->window);
6350 set_win_state(win, NormalState);
6354 mappingnotify(XEvent *e)
6356 XMappingEvent *ev = &e->xmapping;
6358 XRefreshKeyboardMapping(ev);
6359 if (ev->request == MappingKeyboard)
6364 maprequest(XEvent *e)
6367 struct swm_region *r;
6368 XWindowAttributes wa;
6369 XMapRequestEvent *ev = &e->xmaprequest;
6371 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6372 e->xmaprequest.window);
6374 if (!XGetWindowAttributes(display, ev->window, &wa))
6376 if (wa.override_redirect)
6379 win = manage_window(e->xmaprequest.window);
6381 return; /* can't happen */
6385 /* make new win focused */
6386 r = root_to_region(win->wa.root);
6387 if (win->ws == r->ws)
6392 propertynotify(XEvent *e)
6395 XPropertyEvent *ev = &e->xproperty;
6398 name = XGetAtomName(display, ev->atom);
6399 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6404 win = find_window(ev->window);
6408 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6409 update_iconic(win, 0);
6410 XMapRaised(display, win->id);
6418 case XA_WM_NORMAL_HINTS:
6420 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6421 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6422 if (win->sh.flags & PMinSize) {
6423 WIDTH(win) = win->sh.min_width;
6424 HEIGHT(win) = win->sh.min_height;
6425 fprintf(stderr, "min %d %d\n", WIDTH(win), HEIGHT(win));
6427 XMoveResizeWindow(display, win->id,
6428 X(win), Y(win), WIDTH(win), HEIGHT(win));
6431 if (title_name_enabled || title_class_enabled)
6435 if (window_name_enabled)
6444 unmapnotify(XEvent *e)
6448 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6450 /* determine if we need to help unmanage this window */
6451 win = find_window(e->xunmap.window);
6455 if (getstate(e->xunmap.window) == NormalState) {
6456 unmanage_window(win);
6459 /* giant hack for apps that don't destroy transient windows */
6460 /* eat a bunch of events to prevent remanaging the window */
6462 while (XCheckWindowEvent(display, e->xunmap.window,
6463 EnterWindowMask, &cne))
6465 while (XCheckWindowEvent(display, e->xunmap.window,
6466 StructureNotifyMask, &cne))
6468 while (XCheckWindowEvent(display, e->xunmap.window,
6469 SubstructureNotifyMask, &cne))
6471 /* resend unmap because we ated it */
6472 XUnmapWindow(display, e->xunmap.window);
6475 if (focus_mode == SWM_FOCUS_DEFAULT)
6476 drain_enter_notify();
6480 visibilitynotify(XEvent *e)
6483 struct swm_region *r;
6485 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6486 e->xvisibility.window);
6487 if (e->xvisibility.state == VisibilityUnobscured)
6488 for (i = 0; i < ScreenCount(display); i++)
6489 TAILQ_FOREACH(r, &screens[i].rl, entry)
6490 if (e->xvisibility.window == r->bar_window)
6495 clientmessage(XEvent *e)
6497 XClientMessageEvent *ev;
6502 win = find_window(ev->window);
6506 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6507 ev->window, ev->message_type);
6509 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6510 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6513 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6514 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6515 if (win->can_delete)
6516 client_msg(win, adelete);
6518 XKillClient(display, win->id);
6520 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6521 DNPRINTF(SWM_D_EVENT,
6522 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6523 if (win->floating) {
6524 if (ev->data.l[0] & (1<<8)) /* x */
6525 X(win) = ev->data.l[1];
6526 if (ev->data.l[0] & (1<<9)) /* y */
6527 Y(win) = ev->data.l[2];
6528 if (ev->data.l[0] & (1<<10)) /* width */
6529 WIDTH(win) = ev->data.l[3];
6530 if (ev->data.l[0] & (1<<11)) /* height */
6531 HEIGHT(win) = ev->data.l[4];
6536 /* TODO: Change stack sizes */
6537 /* notify no change was made. */
6538 config_win(win, NULL);
6541 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6542 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6543 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6545 ewmh_update_win_state(win, ev->data.l[2],
6553 xerror_start(Display *d, XErrorEvent *ee)
6560 xerror(Display *d, XErrorEvent *ee)
6562 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6570 xerrorxlib = XSetErrorHandler(xerror_start);
6572 /* this causes an error if some other window manager is running */
6573 XSelectInput(display, DefaultRootWindow(display),
6574 SubstructureRedirectMask);
6575 XSync(display, False);
6579 XSetErrorHandler(xerror);
6580 XSync(display, False);
6585 new_region(struct swm_screen *s, int x, int y, int w, int h)
6587 struct swm_region *r, *n;
6588 struct workspace *ws = NULL;
6591 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6592 s->idx, w, h, x, y);
6594 /* remove any conflicting regions */
6595 n = TAILQ_FIRST(&s->rl);
6598 n = TAILQ_NEXT(r, entry);
6599 if (X(r) < (x + w) &&
6600 (X(r) + WIDTH(r)) > x &&
6602 (Y(r) + HEIGHT(r)) > y) {
6603 if (r->ws->r != NULL)
6604 r->ws->old_r = r->ws->r;
6606 XDestroyWindow(display, r->bar_window);
6607 TAILQ_REMOVE(&s->rl, r, entry);
6608 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6612 /* search old regions for one to reuse */
6614 /* size + location match */
6615 TAILQ_FOREACH(r, &s->orl, entry)
6616 if (X(r) == x && Y(r) == y &&
6617 HEIGHT(r) == h && WIDTH(r) == w)
6621 TAILQ_FOREACH(r, &s->orl, entry)
6622 if (HEIGHT(r) == h && WIDTH(r) == w)
6626 TAILQ_REMOVE(&s->orl, r, entry);
6627 /* try to use old region's workspace */
6628 if (r->ws->r == NULL)
6631 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6632 err(1, "new_region: calloc: failed to allocate memory "
6635 /* if we don't have a workspace already, find one */
6637 for (i = 0; i < SWM_WS_MAX; i++)
6638 if (s->ws[i].r == NULL) {
6645 errx(1, "new_region: no free workspaces");
6656 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6662 #ifdef SWM_XRR_HAS_CRTC
6664 XRRScreenResources *sr;
6667 #endif /* SWM_XRR_HAS_CRTC */
6668 struct swm_region *r;
6671 if (i >= ScreenCount(display))
6672 errx(1, "scan_xrandr: invalid screen");
6674 /* remove any old regions */
6675 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6676 r->ws->old_r = r->ws->r = NULL;
6677 XDestroyWindow(display, r->bar_window);
6678 TAILQ_REMOVE(&screens[i].rl, r, entry);
6679 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6683 /* map virtual screens onto physical screens */
6684 #ifdef SWM_XRR_HAS_CRTC
6685 if (xrandr_support) {
6686 sr = XRRGetScreenResources(display, screens[i].root);
6688 new_region(&screens[i], 0, 0,
6689 DisplayWidth(display, i),
6690 DisplayHeight(display, i));
6694 for (c = 0, ci = NULL; c < ncrtc; c++) {
6695 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6696 if (ci->noutput == 0)
6699 if (ci != NULL && ci->mode == None)
6700 new_region(&screens[i], 0, 0,
6701 DisplayWidth(display, i),
6702 DisplayHeight(display, i));
6704 new_region(&screens[i],
6705 ci->x, ci->y, ci->width, ci->height);
6708 XRRFreeCrtcInfo(ci);
6709 XRRFreeScreenResources(sr);
6711 #endif /* SWM_XRR_HAS_CRTC */
6713 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6714 DisplayHeight(display, i));
6719 screenchange(XEvent *e) {
6720 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6721 struct swm_region *r;
6724 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
6726 if (!XRRUpdateConfiguration(e))
6729 /* silly event doesn't include the screen index */
6730 for (i = 0; i < ScreenCount(display); i++)
6731 if (screens[i].root == xe->root)
6733 if (i >= ScreenCount(display))
6734 errx(1, "screenchange: screen not found");
6736 /* brute force for now, just re-enumerate the regions */
6739 /* add bars to all regions */
6740 for (i = 0; i < ScreenCount(display); i++)
6741 TAILQ_FOREACH(r, &screens[i].rl, entry)
6744 if (focus_mode == SWM_FOCUS_DEFAULT)
6745 drain_enter_notify();
6751 Window d1, d2, *wins = NULL;
6752 XWindowAttributes wa;
6757 for (i = 0; i < ScreenCount(display); i++) {
6758 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6761 /* attach windows to a region */
6762 /* normal windows */
6763 for (j = 0; j < no; j++) {
6764 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6765 wa.override_redirect ||
6766 XGetTransientForHint(display, wins[j], &d1))
6769 state = getstate(wins[j]);
6770 manage = state == IconicState;
6771 if (wa.map_state == IsViewable || manage)
6772 manage_window(wins[j]);
6774 /* transient windows */
6775 for (j = 0; j < no; j++) {
6776 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6777 wa.override_redirect)
6780 state = getstate(wins[j]);
6781 manage = state == IconicState;
6782 if (XGetTransientForHint(display, wins[j], &d1) &&
6784 manage_window(wins[j]);
6797 int errorbase, major, minor;
6798 struct workspace *ws;
6801 if ((screens = calloc(ScreenCount(display),
6802 sizeof(struct swm_screen))) == NULL)
6803 err(1, "setup_screens: calloc: failed to allocate memory for "
6806 /* initial Xrandr setup */
6807 xrandr_support = XRRQueryExtension(display,
6808 &xrandr_eventbase, &errorbase);
6810 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6813 /* map physical screens */
6814 for (i = 0; i < ScreenCount(display); i++) {
6815 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
6817 TAILQ_INIT(&screens[i].rl);
6818 TAILQ_INIT(&screens[i].orl);
6819 screens[i].root = RootWindow(display, i);
6821 /* set default colors */
6822 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6823 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6824 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6825 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6826 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6828 /* create graphics context on screen with default font color */
6829 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
6832 XSetForeground(display, screens[i].bar_gc,
6833 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6835 /* set default cursor */
6836 XDefineCursor(display, screens[i].root,
6837 XCreateFontCursor(display, XC_left_ptr));
6839 /* init all workspaces */
6840 /* XXX these should be dynamically allocated too */
6841 for (j = 0; j < SWM_WS_MAX; j++) {
6842 ws = &screens[i].ws[j];
6848 TAILQ_INIT(&ws->winlist);
6849 TAILQ_INIT(&ws->unmanagedlist);
6851 for (k = 0; layouts[k].l_stack != NULL; k++)
6852 if (layouts[k].l_config != NULL)
6853 layouts[k].l_config(ws,
6854 SWM_ARG_ID_STACKINIT);
6855 ws->cur_layout = &layouts[0];
6856 ws->cur_layout->l_string(ws);
6862 XRRSelectInput(display, screens[i].root,
6863 RRScreenChangeNotifyMask);
6870 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
6871 err(1, "setup_globals: strdup: failed to allocate memory.");
6873 if ((spawn_term[0] = strdup("xterm")) == NULL)
6874 err(1, "setup_globals: strdup: failed to allocate memory.");
6876 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6877 err(1, "setup_globals: strdup: failed to allocate memory.");
6884 Atom netwmcheck, netwmname, utf8_string;
6887 /* work around sun jdk bugs, code from wmname */
6888 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6889 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6890 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6891 for (i = 0; i < ScreenCount(display); i++) {
6892 root = screens[i].root;
6893 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6894 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6895 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6897 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6898 PropModeReplace, (unsigned char *)&win,1);
6899 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6900 PropModeReplace, (unsigned char *)&win,1);
6901 XChangeProperty(display, win, netwmname, utf8_string, 8,
6902 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6907 main(int argc, char *argv[])
6909 struct swm_region *r, *rr;
6910 struct ws_win *winfocus = NULL;
6913 char conf[PATH_MAX], *cfile = NULL;
6918 struct sigaction sact;
6921 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6922 SCROTWM_VERSION, buildstr);
6923 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6925 warnx("no locale support");
6927 if (!X_HAVE_UTF8_STRING)
6928 warnx("no UTF-8 support");
6930 if (!(display = XOpenDisplay(0)))
6931 errx(1, "can not open display");
6934 errx(1, "other wm running");
6936 /* handle some signals */
6937 bzero(&sact, sizeof(sact));
6938 sigemptyset(&sact.sa_mask);
6940 sact.sa_handler = sighdlr;
6941 sigaction(SIGINT, &sact, NULL);
6942 sigaction(SIGQUIT, &sact, NULL);
6943 sigaction(SIGTERM, &sact, NULL);
6944 sigaction(SIGHUP, &sact, NULL);
6946 sact.sa_handler = sighdlr;
6947 sact.sa_flags = SA_NOCLDSTOP;
6948 sigaction(SIGCHLD, &sact, NULL);
6950 astate = XInternAtom(display, "WM_STATE", False);
6951 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6952 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6953 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6954 a_wmname = XInternAtom(display, "WM_NAME", False);
6955 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6956 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6957 a_string = XInternAtom(display, "STRING", False);
6958 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6960 /* look for local and global conf file */
6961 pwd = getpwuid(getuid());
6963 errx(1, "invalid user: %d", getuid());
6972 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6973 if (stat(conf, &sb) != -1) {
6974 if (S_ISREG(sb.st_mode))
6977 /* try global conf file */
6978 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6979 if (!stat(conf, &sb))
6980 if (S_ISREG(sb.st_mode))
6984 /* load conf (if any) and refresh font color in bar graphics contexts */
6985 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
6986 for (i = 0; i < ScreenCount(display); ++i)
6987 XSetForeground(display, screens[i].bar_gc,
6988 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6991 /* set some values to work around bad programs */
6993 /* grab existing windows (before we build the bars) */
6996 if (getenv("SWM_STARTED") == NULL)
6997 setenv("SWM_STARTED", "YES", 1);
6999 /* setup all bars */
7000 for (i = 0; i < ScreenCount(display); i++)
7001 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7002 if (winfocus == NULL)
7003 winfocus = TAILQ_FIRST(&r->ws->winlist);
7011 if (focus_mode == SWM_FOCUS_DEFAULT)
7012 drain_enter_notify();
7014 xfd = ConnectionNumber(display);
7016 while (XPending(display)) {
7017 XNextEvent(display, &e);
7020 if (e.type < LASTEvent) {
7021 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7022 "window: 0x%lx, type: %s (%d), %d remaining"
7023 "\n", YESNO(handler[e.type]),
7024 e.xany.window, geteventname(&e),
7025 e.type, QLength(display));
7027 if (handler[e.type])
7028 handler[e.type](&e);
7030 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7031 "0x%lx, type: %s (%d)\n", e.xany.window,
7032 xrandr_geteventname(&e), e.type);
7034 switch (e.type - xrandr_eventbase) {
7035 case RRScreenChangeNotify:
7044 /* if we are being restarted go focus on first window */
7046 rr = winfocus->ws->r;
7048 /* not a visible window */
7052 /* move pointer to first screen if multi screen */
7053 if (ScreenCount(display) > 1 || outputs > 1)
7054 XWarpPointer(display, None, rr->s[0].root,
7056 Y(rr) + (bar_enabled ? bar_height : 0));
7058 a.id = SWM_ARG_ID_FOCUSCUR;
7068 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7070 DNPRINTF(SWM_D_MISC, "select failed");
7071 if (restart_wm == 1)
7072 restart(NULL, NULL);
7073 if (search_resp == 1)
7086 for (i = 0; i < ScreenCount(display); ++i)
7087 if (screens[i].bar_gc != NULL)
7088 XFreeGC(display, screens[i].bar_gc);
7090 XFreeFontSet(display, bar_fs);
7091 XCloseDisplay(display);