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 len = strftime(s, sizeof s, clock_format, &tm);
1451 strlcat(s, " ", sizeof s);
1454 for (i = 0; i < ScreenCount(display); i++) {
1456 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1457 strlcpy((char *)cn, "", sizeof cn);
1458 strlcpy(ws, "", sizeof ws);
1460 bar_urgent((char *)cn, sizeof cn);
1461 bar_class_name((char *)cn, sizeof cn,
1463 bar_window_name((char *)cn, sizeof cn,
1466 snprintf(ws, sizeof ws, "<%s>",
1470 stack = r->ws->stacker;
1472 snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s "
1473 "%s", x++, r->ws->idx + 1, stack, ws, s, cn,
1474 bar_ext, bar_vertext);
1488 bar_toggle(struct swm_region *r, union arg *args)
1490 struct swm_region *tmpr;
1491 int i, sc = ScreenCount(display);
1493 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1496 for (i = 0; i < sc; i++)
1497 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1498 XUnmapWindow(display, tmpr->bar_window);
1500 for (i = 0; i < sc; i++)
1501 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1502 XMapRaised(display, tmpr->bar_window);
1504 bar_enabled = !bar_enabled;
1507 /* must be after stack */
1514 XSetWindowAttributes wa;
1515 struct swm_region *r;
1518 /* do this here because the conf file is in memory */
1519 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1520 /* launch external status app */
1521 bar_extra_running = 1;
1522 if (pipe(bar_pipe) == -1)
1523 err(1, "pipe error");
1524 socket_setnonblock(bar_pipe[0]);
1525 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1526 if (dup2(bar_pipe[0], 0) == -1)
1528 if (dup2(bar_pipe[1], 1) == -1)
1530 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1531 err(1, "could not disable SIGPIPE");
1532 switch (bar_pid = fork()) {
1534 err(1, "cannot fork");
1538 execvp(bar_argv[0], bar_argv);
1539 err(1, "%s external app failed", bar_argv[0]);
1541 default: /* parent */
1547 bzero(&wa, sizeof wa);
1548 for (i = 0; i < ScreenCount(display); i++)
1549 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1551 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1552 wa.background_pixel =
1553 screens[i].c[SWM_S_COLOR_BAR].color;
1554 XChangeWindowAttributes(display, r->bar_window,
1555 CWBackPixel | CWBorderPixel, &wa);
1561 bar_setup(struct swm_region *r)
1563 char *default_string;
1564 char **missing_charsets;
1565 int num_missing_charsets = 0;
1569 XFreeFontSet(display, bar_fs);
1574 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1576 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1577 &num_missing_charsets, &default_string);
1579 if (num_missing_charsets > 0) {
1580 warnx("Unable to load charset(s):");
1582 for (i = 0; i < num_missing_charsets; ++i)
1583 warnx("%s", missing_charsets[i]);
1585 XFreeStringList(missing_charsets);
1587 if (strcmp(default_string, ""))
1588 warnx("Glyphs from those sets will be replaced "
1589 "by '%s'.", default_string);
1591 warnx("Glyphs from those sets won't be drawn.");
1595 errx(1, "Error creating font set structure.");
1597 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1599 bar_height = bar_fs_extents->max_logical_extent.height +
1600 2 * bar_border_width;
1606 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1608 r->bar_window = XCreateSimpleWindow(display,
1609 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1610 bar_height - 2 * bar_border_width,
1611 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1612 r->s->c[SWM_S_COLOR_BAR].color);
1613 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1615 XMapRaised(display, r->bar_window);
1616 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1618 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1619 err(1, "could not install bar_signal");
1624 drain_enter_notify(void)
1629 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1632 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1636 set_win_state(struct ws_win *win, long state)
1638 long data[] = {state, None};
1640 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1645 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1646 (unsigned char *)data, 2);
1653 unsigned char *p = NULL;
1656 if (!get_property(w, astate, 2L, astate, &n, &p))
1659 result = *((long *)p);
1665 version(struct swm_region *r, union arg *args)
1667 bar_version = !bar_version;
1669 snprintf(bar_vertext, sizeof bar_vertext,
1670 "Version: %s Build: %s", SCROTWM_VERSION, buildstr);
1672 strlcpy(bar_vertext, "", sizeof bar_vertext);
1677 client_msg(struct ws_win *win, Atom a)
1679 XClientMessageEvent cm;
1684 bzero(&cm, sizeof cm);
1685 cm.type = ClientMessage;
1686 cm.window = win->id;
1687 cm.message_type = aprot;
1690 cm.data.l[1] = CurrentTime;
1691 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1694 /* synthetic response to a ConfigureRequest when not making a change */
1696 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1703 /* send notification of unchanged state. */
1704 ce.type = ConfigureNotify;
1707 ce.width = WIDTH(win);
1708 ce.height = HEIGHT(win);
1709 ce.override_redirect = False;
1713 ce.display = display;
1715 ce.window = win->id;
1716 ce.border_width = border_width;
1720 ce.display = ev->display;
1721 ce.event = ev->window;
1722 ce.window = ev->window;
1724 /* make response appear more WM_SIZE_HINTS-compliant */
1726 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1727 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1728 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1729 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1730 SH_INC_W(win), SH_INC_H(win));
1734 /* the hint may be set... to 0! */
1735 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1736 ce.width = SH_MIN_W(win);
1737 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1738 ce.height = SH_MIN_H(win);
1743 /* may also be advertized as 0 */
1744 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1745 ce.width = SH_MAX_W(win);
1746 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1747 ce.height = SH_MAX_H(win);
1750 /* resize increment. */
1752 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1753 ce.width -= (ce.width - SH_MIN_W(win)) %
1755 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1756 ce.height -= (ce.height - SH_MIN_H(win)) %
1760 /* adjust x and y for requested border_width. */
1761 ce.x += border_width - ev->border_width;
1762 ce.y += border_width - ev->border_width;
1763 ce.border_width = ev->border_width;
1764 ce.above = ev->above;
1767 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1768 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1769 ce.y, ce.width, ce.height, ce.border_width);
1771 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1775 count_win(struct workspace *ws, int count_transient)
1780 TAILQ_FOREACH(win, &ws->winlist, entry) {
1781 if (count_transient == 0 && win->floating)
1783 if (count_transient == 0 && win->transient)
1789 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1795 quit(struct swm_region *r, union arg *args)
1797 DNPRINTF(SWM_D_MISC, "quit\n");
1802 unmap_window(struct ws_win *win)
1807 /* don't unmap again */
1808 if (getstate(win->id) == IconicState)
1811 set_win_state(win, IconicState);
1813 XUnmapWindow(display, win->id);
1814 XSetWindowBorder(display, win->id,
1815 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1824 for (i = 0; i < ScreenCount(display); i++)
1825 for (j = 0; j < SWM_WS_MAX; j++)
1826 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1831 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1838 event.display = display; /* Ignored, but what the hell */
1839 event.window = win->id;
1840 event.root = win->s->root;
1841 event.subwindow = None;
1842 event.time = CurrentTime;
1847 event.same_screen = True;
1848 event.keycode = XKeysymToKeycode(display, keysym);
1849 event.state = modifiers;
1851 event.type = KeyPress;
1852 XSendEvent(event.display, event.window, True,
1853 KeyPressMask, (XEvent *)&event);
1855 event.type = KeyRelease;
1856 XSendEvent(event.display, event.window, True,
1857 KeyPressMask, (XEvent *)&event);
1862 restart(struct swm_region *r, union arg *args)
1864 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1866 /* disable alarm because the following code may not be interrupted */
1868 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1869 err(1, "can't disable alarm");
1874 XCloseDisplay(display);
1875 execvp(start_argv[0], start_argv);
1876 fprintf(stderr, "execvp failed\n");
1882 root_to_region(Window root)
1884 struct swm_region *r = NULL;
1886 int i, x, y, wx, wy;
1889 for (i = 0; i < ScreenCount(display); i++)
1890 if (screens[i].root == root)
1893 if (XQueryPointer(display, screens[i].root,
1894 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1895 /* choose a region based on pointer location */
1896 TAILQ_FOREACH(r, &screens[i].rl, entry)
1897 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1898 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1903 r = TAILQ_FIRST(&screens[i].rl);
1909 find_unmanaged_window(Window id)
1914 for (i = 0; i < ScreenCount(display); i++)
1915 for (j = 0; j < SWM_WS_MAX; j++)
1916 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1924 find_window(Window id)
1927 Window wrr, wpr, *wcr = NULL;
1931 for (i = 0; i < ScreenCount(display); i++)
1932 for (j = 0; j < SWM_WS_MAX; j++)
1933 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1937 /* if we were looking for the parent return that window instead */
1938 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1943 /* ignore not found and root */
1944 if (wpr == 0 || wrr == wpr)
1947 /* look for parent */
1948 for (i = 0; i < ScreenCount(display); i++)
1949 for (j = 0; j < SWM_WS_MAX; j++)
1950 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1958 spawn(int ws_idx, union arg *args, int close_fd)
1963 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1966 close(ConnectionNumber(display));
1968 setenv("LD_PRELOAD", SWM_LIB, 1);
1970 if (asprintf(&ret, "%d", ws_idx) == -1) {
1974 setenv("_SWM_WS", ret, 1);
1978 if (asprintf(&ret, "%d", getpid()) == -1) {
1982 setenv("_SWM_PID", ret, 1);
1986 if (setsid() == -1) {
1993 * close stdin and stdout to prevent interaction between apps
1994 * and the baraction script
1995 * leave stderr open to record errors
1997 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2001 dup2(fd, STDIN_FILENO);
2002 dup2(fd, STDOUT_FILENO);
2007 execvp(args->argv[0], args->argv);
2014 spawnterm(struct swm_region *r, union arg *args)
2016 DNPRINTF(SWM_D_MISC, "spawnterm\n");
2020 setenv("_SWM_XTERM_FONTADJ", "", 1);
2021 spawn(r->ws->idx, args, 1);
2026 kill_refs(struct ws_win *win)
2029 struct swm_region *r;
2030 struct workspace *ws;
2035 for (i = 0; i < ScreenCount(display); i++)
2036 TAILQ_FOREACH(r, &screens[i].rl, entry)
2037 for (x = 0; x < SWM_WS_MAX; x++) {
2039 if (win == ws->focus)
2041 if (win == ws->focus_prev)
2042 ws->focus_prev = NULL;
2047 validate_win(struct ws_win *testwin)
2050 struct workspace *ws;
2051 struct swm_region *r;
2054 if (testwin == NULL)
2057 for (i = 0; i < ScreenCount(display); i++)
2058 TAILQ_FOREACH(r, &screens[i].rl, entry)
2059 for (x = 0; x < SWM_WS_MAX; x++) {
2061 TAILQ_FOREACH(win, &ws->winlist, entry)
2069 validate_ws(struct workspace *testws)
2071 struct swm_region *r;
2072 struct workspace *ws;
2075 /* validate all ws */
2076 for (i = 0; i < ScreenCount(display); i++)
2077 TAILQ_FOREACH(r, &screens[i].rl, entry)
2078 for (x = 0; x < SWM_WS_MAX; x++) {
2087 unfocus_win(struct ws_win *win)
2092 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2096 if (win->ws == NULL)
2099 if (validate_ws(win->ws))
2100 return; /* XXX this gets hit with thunderbird, needs fixing */
2102 if (win->ws->r == NULL)
2105 if (validate_win(win)) {
2110 if (win->ws->focus == win) {
2111 win->ws->focus = NULL;
2112 win->ws->focus_prev = win;
2115 if (validate_win(win->ws->focus)) {
2116 kill_refs(win->ws->focus);
2117 win->ws->focus = NULL;
2119 if (validate_win(win->ws->focus_prev)) {
2120 kill_refs(win->ws->focus_prev);
2121 win->ws->focus_prev = NULL;
2124 /* drain all previous unfocus events */
2125 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2128 grabbuttons(win, 0);
2129 XSetWindowBorder(display, win->id,
2130 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2132 XChangeProperty(display, win->s->root,
2133 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2134 PropModeReplace, (unsigned char *)&none,1);
2143 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2145 for (i = 0; i < ScreenCount(display); i++)
2146 for (j = 0; j < SWM_WS_MAX; j++)
2147 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2152 focus_win(struct ws_win *win)
2157 struct ws_win *cfw = NULL;
2160 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2164 if (win->ws == NULL)
2167 if (validate_ws(win->ws))
2168 return; /* XXX this gets hit with thunderbird, needs fixing */
2170 if (validate_win(win)) {
2175 if (validate_win(win)) {
2180 XGetInputFocus(display, &cur_focus, &rr);
2181 if ((cfw = find_window(cur_focus)) != NULL)
2184 /* use larger hammer since the window was killed somehow */
2185 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2186 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2187 XSetWindowBorder(display, cfw->id,
2188 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2191 win->ws->focus = win;
2193 if (win->ws->r != NULL) {
2194 /* drain all previous focus events */
2195 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2199 XSetInputFocus(display, win->id,
2200 RevertToParent, CurrentTime);
2201 grabbuttons(win, 1);
2202 XSetWindowBorder(display, win->id,
2203 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2204 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2205 win->ws->always_raise)
2206 XMapRaised(display, win->id);
2208 XChangeProperty(display, win->s->root,
2209 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2210 PropModeReplace, (unsigned char *)&win->id,1);
2213 if (window_name_enabled)
2218 switchws(struct swm_region *r, union arg *args)
2220 int wsid = args->id, unmap_old = 0;
2221 struct swm_region *this_r, *other_r;
2223 struct workspace *new_ws, *old_ws;
2230 old_ws = this_r->ws;
2231 new_ws = &this_r->s->ws[wsid];
2233 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2234 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2236 if (new_ws == NULL || old_ws == NULL)
2238 if (new_ws == old_ws)
2241 other_r = new_ws->r;
2242 if (other_r == NULL) {
2243 /* the other workspace is hidden, hide this one */
2247 /* the other ws is visible in another region, exchange them */
2248 other_r->ws_prior = new_ws;
2249 other_r->ws = old_ws;
2250 old_ws->r = other_r;
2252 this_r->ws_prior = old_ws;
2253 this_r->ws = new_ws;
2256 /* this is needed so that we can click on a window after a restart */
2260 a.id = SWM_ARG_ID_FOCUSCUR;
2261 focus(new_ws->r, &a);
2265 /* unmap old windows */
2267 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2270 if (focus_mode == SWM_FOCUS_DEFAULT)
2271 drain_enter_notify();
2275 cyclews(struct swm_region *r, union arg *args)
2278 struct swm_screen *s = r->s;
2281 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2282 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2287 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2290 case SWM_ARG_ID_CYCLEWS_UP:
2291 if (a.id < SWM_WS_MAX - 1)
2296 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2299 case SWM_ARG_ID_CYCLEWS_DOWN:
2303 a.id = SWM_WS_MAX - 1;
2310 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2312 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2316 } while (a.id != r->ws->idx);
2320 priorws(struct swm_region *r, union arg *args)
2324 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2325 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2327 if (r->ws_prior == NULL)
2330 a.id = r->ws_prior->idx;
2335 cyclescr(struct swm_region *r, union arg *args)
2337 struct swm_region *rr = NULL;
2341 /* do nothing if we don't have more than one screen */
2342 if (!(ScreenCount(display) > 1 || outputs > 1))
2347 case SWM_ARG_ID_CYCLESC_UP:
2348 rr = TAILQ_NEXT(r, entry);
2350 rr = TAILQ_FIRST(&screens[i].rl);
2352 case SWM_ARG_ID_CYCLESC_DOWN:
2353 rr = TAILQ_PREV(r, swm_region_list, entry);
2355 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2363 /* move mouse to region */
2365 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2366 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2368 a.id = SWM_ARG_ID_FOCUSCUR;
2371 if (rr->ws->focus) {
2372 /* move to focus window */
2373 x = X(rr->ws->focus) + 1;
2374 y = Y(rr->ws->focus) + 1;
2375 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2380 sort_windows(struct ws_win_list *wl)
2382 struct ws_win *win, *parent, *nxt;
2387 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2388 nxt = TAILQ_NEXT(win, entry);
2389 if (win->transient) {
2390 parent = find_window(win->transient);
2391 if (parent == NULL) {
2392 fprintf(stderr, "not possible bug\n");
2395 TAILQ_REMOVE(wl, win, entry);
2396 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2403 swapwin(struct swm_region *r, union arg *args)
2405 struct ws_win *target, *source;
2406 struct ws_win *cur_focus;
2407 struct ws_win_list *wl;
2410 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2411 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2413 cur_focus = r->ws->focus;
2414 if (cur_focus == NULL)
2418 wl = &source->ws->winlist;
2421 case SWM_ARG_ID_SWAPPREV:
2422 if (source->transient)
2423 source = find_window(source->transient);
2424 target = TAILQ_PREV(source, ws_win_list, entry);
2425 if (target && target->transient)
2426 target = find_window(target->transient);
2427 TAILQ_REMOVE(wl, source, entry);
2429 TAILQ_INSERT_TAIL(wl, source, entry);
2431 TAILQ_INSERT_BEFORE(target, source, entry);
2433 case SWM_ARG_ID_SWAPNEXT:
2434 target = TAILQ_NEXT(source, entry);
2435 /* move the parent and let the sort handle the move */
2436 if (source->transient)
2437 source = find_window(source->transient);
2438 TAILQ_REMOVE(wl, source, entry);
2440 TAILQ_INSERT_HEAD(wl, source, entry);
2442 TAILQ_INSERT_AFTER(wl, target, source, entry);
2444 case SWM_ARG_ID_SWAPMAIN:
2445 target = TAILQ_FIRST(wl);
2446 if (target == source) {
2447 if (source->ws->focus_prev != NULL &&
2448 source->ws->focus_prev != target)
2450 source = source->ws->focus_prev;
2454 if (target == NULL || source == NULL)
2456 source->ws->focus_prev = target;
2457 TAILQ_REMOVE(wl, target, entry);
2458 TAILQ_INSERT_BEFORE(source, target, entry);
2459 TAILQ_REMOVE(wl, source, entry);
2460 TAILQ_INSERT_HEAD(wl, source, entry);
2462 case SWM_ARG_ID_MOVELAST:
2463 TAILQ_REMOVE(wl, source, entry);
2464 TAILQ_INSERT_TAIL(wl, source, entry);
2467 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2477 focus_prev(struct ws_win *win)
2479 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2480 struct ws_win *cur_focus = NULL;
2481 struct ws_win_list *wl = NULL;
2482 struct workspace *ws = NULL;
2484 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx\n", WINID(win));
2486 if (!(win && win->ws))
2491 cur_focus = ws->focus;
2492 winlostfocus = cur_focus;
2494 /* pickle, just focus on whatever */
2495 if (cur_focus == NULL) {
2496 /* use prev_focus if valid */
2497 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2498 find_window(WINID(ws->focus_prev)))
2499 winfocus = ws->focus_prev;
2500 if (winfocus == NULL)
2501 winfocus = TAILQ_FIRST(wl);
2505 /* if transient focus on parent */
2506 if (cur_focus->transient) {
2507 winfocus = find_window(cur_focus->transient);
2511 /* if in max_stack try harder */
2512 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2513 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2514 if (cur_focus != ws->focus_prev)
2515 winfocus = ws->focus_prev;
2516 else if (cur_focus != ws->focus)
2517 winfocus = ws->focus;
2519 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2524 if (cur_focus == win)
2525 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2526 if (winfocus == NULL)
2527 winfocus = TAILQ_LAST(wl, ws_win_list);
2528 if (winfocus == NULL || winfocus == win)
2529 winfocus = TAILQ_NEXT(cur_focus, entry);
2531 if (winfocus == winlostfocus || winfocus == NULL) {
2532 /* update the bar so that title/class/name will be cleared. */
2533 if (window_name_enabled || title_name_enabled ||
2534 title_class_enabled)
2539 focus_magic(winfocus);
2543 focus(struct swm_region *r, union arg *args)
2545 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2546 struct ws_win *cur_focus = NULL;
2547 struct ws_win_list *wl = NULL;
2548 struct workspace *ws = NULL;
2554 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2556 /* treat FOCUS_CUR special */
2557 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2558 if (r->ws->focus && r->ws->focus->iconic == 0)
2559 winfocus = r->ws->focus;
2560 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2561 winfocus = r->ws->focus_prev;
2563 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2564 if (winfocus->iconic == 0)
2567 focus_magic(winfocus);
2571 if ((cur_focus = r->ws->focus) == NULL)
2575 if (TAILQ_EMPTY(wl))
2577 /* make sure there is at least one uniconified window */
2579 TAILQ_FOREACH(winfocus, wl, entry)
2580 if (winfocus->iconic == 0) {
2587 winlostfocus = cur_focus;
2590 case SWM_ARG_ID_FOCUSPREV:
2591 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2593 head = TAILQ_LAST(wl, ws_win_list);
2595 if (WINID(winfocus) == cur_focus->transient) {
2596 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2598 head = TAILQ_LAST(wl, ws_win_list);
2603 if (winfocus && winfocus->iconic) {
2604 while (winfocus != cur_focus) {
2605 if (winfocus == NULL)
2606 winfocus = TAILQ_LAST(wl, ws_win_list);
2607 if (winfocus->iconic == 0)
2609 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2615 case SWM_ARG_ID_FOCUSNEXT:
2616 head = TAILQ_NEXT(cur_focus, entry);
2618 head = TAILQ_FIRST(wl);
2622 if (winfocus && winfocus->iconic) {
2623 while (winfocus != cur_focus) {
2624 if (winfocus == NULL)
2625 winfocus = TAILQ_FIRST(wl);
2626 if (winfocus->iconic == 0)
2628 winfocus = TAILQ_NEXT(winfocus, entry);
2633 case SWM_ARG_ID_FOCUSMAIN:
2634 winfocus = TAILQ_FIRST(wl);
2635 if (winfocus == cur_focus)
2636 winfocus = cur_focus->ws->focus_prev;
2642 if (winfocus == winlostfocus || winfocus == NULL) {
2643 /* update the bar so that title/class/name will be cleared. */
2644 if (window_name_enabled || title_name_enabled ||
2645 title_class_enabled)
2651 focus_magic(winfocus);
2655 cycle_layout(struct swm_region *r, union arg *args)
2657 struct workspace *ws = r->ws;
2660 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2663 if (ws->cur_layout->l_stack == NULL)
2664 ws->cur_layout = &layouts[0];
2667 if (focus_mode == SWM_FOCUS_DEFAULT)
2668 drain_enter_notify();
2669 a.id = SWM_ARG_ID_FOCUSCUR;
2675 stack_config(struct swm_region *r, union arg *args)
2677 struct workspace *ws = r->ws;
2679 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2682 if (ws->cur_layout->l_config != NULL)
2683 ws->cur_layout->l_config(ws, args->id);
2685 if (args->id != SWM_ARG_ID_STACKINIT)
2692 struct swm_geometry g;
2693 struct swm_region *r;
2699 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2701 for (i = 0; i < ScreenCount(display); i++) {
2705 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2706 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2707 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2709 /* start with screen geometry, adjust for bar */
2711 g.w -= 2 * border_width;
2712 g.h -= 2 * border_width;
2718 r->ws->cur_layout->l_stack(r->ws, &g);
2719 r->ws->cur_layout->l_string(r->ws);
2720 /* save r so we can track region changes */
2727 if (focus_mode == SWM_FOCUS_DEFAULT)
2728 drain_enter_notify();
2730 DNPRINTF(SWM_D_STACK, "stack: end\n");
2734 store_float_geom(struct ws_win *win, struct swm_region *r)
2736 /* retain window geom and region geom */
2737 win->g_float = win->g;
2738 win->rg_float = r->g;
2739 win->g_floatvalid = 1;
2743 stack_floater(struct ws_win *win, struct swm_region *r)
2751 bzero(&wc, sizeof wc);
2752 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2755 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2756 * geom on ws switches or return from max mode
2758 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2759 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2761 * use stored g and rg to set relative position and size
2762 * as in old region or before max stack mode
2764 X(win) = win->g_float.x - win->rg_float.x + X(r);
2765 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2766 WIDTH(win) = win->g_float.w;
2767 HEIGHT(win) = win->g_float.h;
2768 win->g_floatvalid = 0;
2771 win->floatmaxed = 0;
2773 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2774 (HEIGHT(win) >= HEIGHT(r)))
2775 wc.border_width = 0;
2777 wc.border_width = border_width;
2778 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2779 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
2780 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
2785 * floaters and transients are auto-centred unless moved
2788 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
2789 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
2792 /* win can be outside r if new r smaller than old r */
2793 /* Ensure top left corner inside r (move probs otherwise) */
2794 if (X(win) < X(r) - wc.border_width)
2795 X(win) = X(r) - wc.border_width;
2796 if (X(win) > X(r) + WIDTH(r) - 1)
2797 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
2798 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
2799 if (Y(win) < Y(r) - wc.border_width)
2800 Y(win) = Y(r) - wc.border_width;
2801 if (Y(win) > Y(r) + HEIGHT(r) - 1)
2802 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
2803 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
2807 wc.width = WIDTH(win);
2808 wc.height = HEIGHT(win);
2811 * Retain floater and transient geometry for correct positioning
2812 * when ws changes region
2814 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2815 store_float_geom(win, r);
2817 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2818 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
2820 XConfigureWindow(display, win->id, mask, &wc);
2824 * Send keystrokes to terminal to decrease/increase the font size as the
2825 * window size changes.
2828 adjust_font(struct ws_win *win)
2830 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2831 win->floating || win->transient)
2834 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2835 WIDTH(win) / win->sh.width_inc < term_width &&
2836 win->font_steps < SWM_MAX_FONT_STEPS) {
2837 win->font_size_boundary[win->font_steps] =
2838 (win->sh.width_inc * term_width) + win->sh.base_width;
2841 win->last_inc = win->sh.width_inc;
2842 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2843 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2844 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
2847 win->last_inc = win->sh.width_inc;
2848 fake_keypress(win, XK_KP_Add, ShiftMask);
2852 #define SWAPXY(g) do { \
2854 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2855 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2858 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2861 XWindowAttributes wa;
2862 struct swm_geometry win_g, r_g = *g;
2863 struct ws_win *win, *fs_win = 0;
2864 int i, j, s, stacks;
2865 int w_inc = 1, h_inc, w_base = 1, h_base;
2866 int hrh, extra = 0, h_slice, last_h = 0;
2867 int split, colno, winno, mwin, msize, mscale;
2868 int remain, missing, v_slice, reconfigure;
2871 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
2872 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
2874 winno = count_win(ws, 0);
2875 if (winno == 0 && count_win(ws, 1) == 0)
2878 TAILQ_FOREACH(win, &ws->winlist, entry)
2879 if (win->transient == 0 && win->floating == 0
2880 && win->iconic == 0)
2887 w_inc = win->sh.width_inc;
2888 w_base = win->sh.base_width;
2889 mwin = ws->l_state.horizontal_mwin;
2890 mscale = ws->l_state.horizontal_msize;
2891 stacks = ws->l_state.horizontal_stacks;
2894 w_inc = win->sh.height_inc;
2895 w_base = win->sh.base_height;
2896 mwin = ws->l_state.vertical_mwin;
2897 mscale = ws->l_state.vertical_msize;
2898 stacks = ws->l_state.vertical_stacks;
2902 if (stacks > winno - mwin)
2903 stacks = winno - mwin;
2907 h_slice = r_g.h / SWM_H_SLICE;
2908 if (mwin && winno > mwin) {
2909 v_slice = r_g.w / SWM_V_SLICE;
2913 win_g.w = v_slice * mscale;
2915 if (w_inc > 1 && w_inc < v_slice) {
2916 /* adjust for window's requested size increment */
2917 remain = (win_g.w - w_base) % w_inc;
2918 missing = w_inc - remain;
2925 win_g.x += r_g.w - msize;
2928 colno = split = winno / stacks;
2929 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2930 2 * border_width) / stacks);
2932 hrh = r_g.h / colno;
2933 extra = r_g.h - (colno * hrh);
2934 win_g.h = hrh - 2 * border_width;
2936 /* stack all the tiled windows */
2937 i = j = 0, s = stacks;
2938 TAILQ_FOREACH(win, &ws->winlist, entry) {
2939 if (win->transient != 0 || win->floating != 0)
2941 if (win->iconic != 0)
2944 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2949 if (split && i == split) {
2950 colno = (winno - mwin) / stacks;
2951 if (s <= (winno - mwin) % stacks)
2953 split = split + colno;
2954 hrh = (r_g.h / colno);
2955 extra = r_g.h - (colno * hrh);
2959 win_g.x += win_g.w + 2 * border_width;
2960 win_g.w = (r_g.w - msize -
2961 (stacks * 2 * border_width)) / stacks;
2963 win_g.w += (r_g.w - msize -
2964 (stacks * 2 * border_width)) % stacks;
2968 win_g.h = hrh - 2 * border_width;
2970 h_inc = win->sh.width_inc;
2971 h_base = win->sh.base_width;
2973 h_inc = win->sh.height_inc;
2974 h_base = win->sh.base_height;
2976 if (j == colno - 1) {
2977 win_g.h = hrh + extra;
2978 } else if (h_inc > 1 && h_inc < h_slice) {
2979 /* adjust for window's requested size increment */
2980 remain = (win_g.h - h_base) % h_inc;
2981 missing = h_inc - remain;
2983 if (missing <= extra || j == 0) {
2995 win_g.y += last_h + 2 * border_width;
2997 bzero(&wc, sizeof wc);
2998 if (disable_border && bar_enabled == 0 && winno == 1){
2999 wc.border_width = 0;
3000 win_g.w += 2 * border_width;
3001 win_g.h += 2 * border_width;
3003 wc.border_width = border_width;
3006 if (X(win) != win_g.y || Y(win) != win_g.x ||
3007 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3009 X(win) = wc.x = win_g.y;
3010 Y(win) = wc.y = win_g.x;
3011 WIDTH(win) = wc.width = win_g.h;
3012 HEIGHT(win) = wc.height = win_g.w;
3015 if (X(win) != win_g.x || Y(win) != win_g.y ||
3016 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3018 X(win) = wc.x = win_g.x;
3019 Y(win) = wc.y = win_g.y;
3020 WIDTH(win) = wc.width = win_g.w;
3021 HEIGHT(win) = wc.height = win_g.h;
3026 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3027 XConfigureWindow(display, win->id, mask, &wc);
3030 if (XGetWindowAttributes(display, win->id, &wa))
3031 if (wa.map_state == IsUnmapped)
3032 XMapRaised(display, win->id);
3040 /* now, stack all the floaters and transients */
3041 TAILQ_FOREACH(win, &ws->winlist, entry) {
3042 if (win->transient == 0 && win->floating == 0)
3044 if (win->iconic == 1)
3046 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3051 stack_floater(win, ws->r);
3052 XMapRaised(display, win->id);
3056 stack_floater(fs_win, ws->r);
3057 XMapRaised(display, fs_win->id);
3062 vertical_config(struct workspace *ws, int id)
3064 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3068 case SWM_ARG_ID_STACKRESET:
3069 case SWM_ARG_ID_STACKINIT:
3070 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3071 ws->l_state.vertical_mwin = 1;
3072 ws->l_state.vertical_stacks = 1;
3074 case SWM_ARG_ID_MASTERSHRINK:
3075 if (ws->l_state.vertical_msize > 1)
3076 ws->l_state.vertical_msize--;
3078 case SWM_ARG_ID_MASTERGROW:
3079 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3080 ws->l_state.vertical_msize++;
3082 case SWM_ARG_ID_MASTERADD:
3083 ws->l_state.vertical_mwin++;
3085 case SWM_ARG_ID_MASTERDEL:
3086 if (ws->l_state.vertical_mwin > 0)
3087 ws->l_state.vertical_mwin--;
3089 case SWM_ARG_ID_STACKINC:
3090 ws->l_state.vertical_stacks++;
3092 case SWM_ARG_ID_STACKDEC:
3093 if (ws->l_state.vertical_stacks > 1)
3094 ws->l_state.vertical_stacks--;
3102 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3104 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3106 stack_master(ws, g, 0, 0);
3110 horizontal_config(struct workspace *ws, int id)
3112 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3115 case SWM_ARG_ID_STACKRESET:
3116 case SWM_ARG_ID_STACKINIT:
3117 ws->l_state.horizontal_mwin = 1;
3118 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3119 ws->l_state.horizontal_stacks = 1;
3121 case SWM_ARG_ID_MASTERSHRINK:
3122 if (ws->l_state.horizontal_msize > 1)
3123 ws->l_state.horizontal_msize--;
3125 case SWM_ARG_ID_MASTERGROW:
3126 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3127 ws->l_state.horizontal_msize++;
3129 case SWM_ARG_ID_MASTERADD:
3130 ws->l_state.horizontal_mwin++;
3132 case SWM_ARG_ID_MASTERDEL:
3133 if (ws->l_state.horizontal_mwin > 0)
3134 ws->l_state.horizontal_mwin--;
3136 case SWM_ARG_ID_STACKINC:
3137 ws->l_state.horizontal_stacks++;
3139 case SWM_ARG_ID_STACKDEC:
3140 if (ws->l_state.horizontal_stacks > 1)
3141 ws->l_state.horizontal_stacks--;
3149 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3151 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3153 stack_master(ws, g, 1, 0);
3156 /* fullscreen view */
3158 max_stack(struct workspace *ws, struct swm_geometry *g)
3161 struct swm_geometry gg = *g;
3162 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3166 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3171 winno = count_win(ws, 0);
3172 if (winno == 0 && count_win(ws, 1) == 0)
3175 TAILQ_FOREACH(win, &ws->winlist, entry) {
3176 if (win->transient) {
3178 parent = find_window(win->transient);
3182 if (win->floating && win->floatmaxed == 0 ) {
3184 * retain geometry for retrieval on exit from
3187 store_float_geom(win, ws->r);
3188 win->floatmaxed = 1;
3191 /* only reconfigure if necessary */
3192 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3193 HEIGHT(win) != gg.h) {
3194 bzero(&wc, sizeof wc);
3195 X(win) = wc.x = gg.x;
3196 Y(win) = wc.y = gg.y;
3198 wc.border_width = border_width;
3199 WIDTH(win) = wc.width = gg.w;
3200 HEIGHT(win) = wc.height = gg.h;
3202 wc.border_width = 0;
3203 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3204 HEIGHT(win) = wc.height = gg.h +
3207 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3208 XConfigureWindow(display, win->id, mask, &wc);
3210 /* unmap only if we don't have multi screen */
3211 if (win != ws->focus)
3212 if (!(ScreenCount(display) > 1 || outputs > 1))
3216 /* put the last transient on top */
3219 XMapRaised(display, parent->id);
3220 stack_floater(wintrans, ws->r);
3221 focus_magic(wintrans);
3226 send_to_ws(struct swm_region *r, union arg *args)
3228 int wsid = args->id;
3229 struct ws_win *win = NULL, *parent;
3230 struct workspace *ws, *nws;
3231 Atom ws_idx_atom = 0;
3232 unsigned char ws_idx_str[SWM_PROPLEN];
3235 if (r && r->ws && r->ws->focus)
3241 if (win->ws->idx == wsid)
3244 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3247 nws = &win->s->ws[wsid];
3249 a.id = SWM_ARG_ID_FOCUSPREV;
3251 if (win->transient) {
3252 parent = find_window(win->transient);
3254 unmap_window(parent);
3255 TAILQ_REMOVE(&ws->winlist, parent, entry);
3256 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3261 TAILQ_REMOVE(&ws->winlist, win, entry);
3262 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3263 if (TAILQ_EMPTY(&ws->winlist))
3264 r->ws->focus = NULL;
3267 /* Try to update the window's workspace property */
3268 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3270 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3272 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3274 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3275 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3282 pressbutton(struct swm_region *r, union arg *args)
3284 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3285 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3289 raise_toggle(struct swm_region *r, union arg *args)
3291 if (r && r->ws == NULL)
3294 r->ws->always_raise = !r->ws->always_raise;
3296 /* bring floaters back to top */
3297 if (r->ws->always_raise == 0)
3302 iconify(struct swm_region *r, union arg *args)
3306 if (r->ws->focus == NULL)
3308 unmap_window(r->ws->focus);
3309 update_iconic(r->ws->focus, 1);
3311 if (focus_mode == SWM_FOCUS_DEFAULT)
3312 drain_enter_notify();
3313 r->ws->focus = NULL;
3314 a.id = SWM_ARG_ID_FOCUSCUR;
3319 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3320 unsigned long *slen)
3323 unsigned long nitems, nbytes, nextra;
3324 unsigned char *prop = NULL;
3327 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3328 &rettype, &retfmt, &nitems, &nbytes, &prop);
3329 if (status != Success)
3333 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3334 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3335 if (status != Success) {
3339 if (rettype != stype) {
3348 uniconify(struct swm_region *r, union arg *args)
3352 unsigned char *name;
3356 DNPRINTF(SWM_D_MISC, "uniconify\n");
3358 if (r && r->ws == NULL)
3361 /* make sure we have anything to uniconify */
3362 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3363 if (win->ws == NULL)
3364 continue; /* should never happen */
3365 if (win->iconic == 0)
3373 search_resp_action = SWM_SEARCH_UNICONIFY;
3375 spawn_select(r, args, "search", &searchpid);
3377 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3380 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3381 if (win->ws == NULL)
3382 continue; /* should never happen */
3383 if (win->iconic == 0)
3386 name = get_win_name(display, win->id, a_netwmname,
3387 a_utf8_string, &len);
3390 fprintf(lfile, "%s.%lu\n", name, win->id);
3398 name_workspace(struct swm_region *r, union arg *args)
3402 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3408 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3410 spawn_select(r, args, "name_workspace", &searchpid);
3412 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3415 fprintf(lfile, "%s", "");
3420 search_workspace(struct swm_region *r, union arg *args)
3423 struct workspace *ws;
3426 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3432 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3434 spawn_select(r, args, "search", &searchpid);
3436 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3439 for (i = 0; i < SWM_WS_MAX; i++) {
3443 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3444 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3451 search_win_cleanup(void)
3453 struct search_window *sw = NULL;
3455 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3456 XDestroyWindow(display, sw->indicator);
3457 XFreeGC(display, sw->gc);
3458 TAILQ_REMOVE(&search_wl, sw, entry);
3464 search_win(struct swm_region *r, union arg *args)
3466 struct ws_win *win = NULL;
3467 struct search_window *sw = NULL;
3474 XRectangle ibox, lbox;
3476 DNPRINTF(SWM_D_MISC, "search_win\n");
3479 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3481 spawn_select(r, args, "search", &searchpid);
3483 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3486 TAILQ_INIT(&search_wl);
3489 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3490 if (win->iconic == 1)
3493 sw = calloc(1, sizeof(struct search_window));
3495 fprintf(stderr, "search_win: calloc: %s",
3498 search_win_cleanup();
3504 snprintf(s, sizeof s, "%d", i);
3507 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3509 w = XCreateSimpleWindow(display,
3510 win->id, 0, 0,lbox.width + 4,
3511 bar_fs_extents->max_logical_extent.height, 1,
3512 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3513 r->s->c[SWM_S_COLOR_FOCUS].color);
3516 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3518 sw->gc = XCreateGC(display, w, 0, &gcv);
3519 XMapRaised(display, w);
3520 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3522 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3523 (bar_fs_extents->max_logical_extent.height -
3524 lbox.height) / 2 - lbox.y, s, len);
3526 fprintf(lfile, "%d\n", i);
3534 search_resp_uniconify(char *resp, unsigned long len)
3536 unsigned char *name;
3540 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3542 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3543 if (win->iconic == 0)
3545 name = get_win_name(display, win->id, a_netwmname,
3546 a_utf8_string, &len);
3549 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3554 if (strncmp(s, resp, len) == 0) {
3555 /* XXX this should be a callback to generalize */
3556 update_iconic(win, 0);
3565 search_resp_name_workspace(char *resp, unsigned long len)
3567 struct workspace *ws;
3569 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3571 if (search_r->ws == NULL)
3576 free(search_r->ws->name);
3577 search_r->ws->name = NULL;
3581 ws->name = strdup(resp);
3582 if (ws->name == NULL) {
3583 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3584 "strdup: %s", strerror(errno));
3591 search_resp_search_workspace(char *resp, unsigned long len)
3598 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3602 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3609 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3611 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3618 switchws(search_r, &a);
3622 search_resp_search_window(char *resp, unsigned long len)
3627 struct search_window *sw;
3629 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3633 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3638 idx = strtonum(s, 1, INT_MAX, &errstr);
3640 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3647 TAILQ_FOREACH(sw, &search_wl, entry)
3648 if (idx == sw->idx) {
3654 #define MAX_RESP_LEN 1024
3657 search_do_resp(void)
3663 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3668 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3669 fprintf(stderr, "search: calloc\n");
3673 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3675 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3678 resp[rbytes] = '\0';
3681 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3682 * newline, so work around that by sanitizing the resp now.
3684 resp[strcspn(resp, "\n")] = '\0';
3687 switch (search_resp_action) {
3688 case SWM_SEARCH_UNICONIFY:
3689 search_resp_uniconify(resp, len);
3691 case SWM_SEARCH_NAME_WORKSPACE:
3692 search_resp_name_workspace(resp, len);
3694 case SWM_SEARCH_SEARCH_WORKSPACE:
3695 search_resp_search_workspace(resp, len);
3697 case SWM_SEARCH_SEARCH_WINDOW:
3698 search_resp_search_window(resp, len);
3703 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3704 search_win_cleanup();
3706 search_resp_action = SWM_SEARCH_NONE;
3707 close(select_resp_pipe[0]);
3712 wkill(struct swm_region *r, union arg *args)
3714 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3716 if (r->ws->focus == NULL)
3719 if (args->id == SWM_ARG_ID_KILLWINDOW)
3720 XKillClient(display, r->ws->focus->id);
3722 if (r->ws->focus->can_delete)
3723 client_msg(r->ws->focus, adelete);
3728 floating_toggle_win(struct ws_win *win)
3730 struct swm_region *r;
3740 /* reject floating toggles in max stack mode */
3741 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3744 if (win->floating) {
3745 if (!win->floatmaxed) {
3746 /* retain position for refloat */
3747 store_float_geom(win, r);
3751 if (win->g_floatvalid) {
3752 /* refloat at last floating relative position */
3753 X(win) = win->g_float.x - win->rg_float.x + X(r);
3754 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3755 WIDTH(win) = win->g_float.w;
3756 HEIGHT(win) = win->g_float.h;
3761 ewmh_update_actions(win);
3767 floating_toggle(struct swm_region *r, union arg *args)
3769 struct ws_win *win = r->ws->focus;
3775 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3776 _NET_WM_STATE_TOGGLE);
3779 if (focus_mode == SWM_FOCUS_DEFAULT)
3780 drain_enter_notify();
3782 if (win == win->ws->focus) {
3783 a.id = SWM_ARG_ID_FOCUSCUR;
3784 focus(win->ws->r, &a);
3789 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3791 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
3793 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
3795 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
3798 if (X(win) < X(r) - border_width) {
3800 WIDTH(win) -= X(r) - X(win) - border_width;
3802 X(win) = X(r) - border_width;
3805 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
3807 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
3809 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
3812 if (Y(win) < Y(r) - border_width) {
3814 HEIGHT(win) -= Y(r) - Y(win) - border_width;
3816 Y(win) = Y(r) - border_width;
3821 if (HEIGHT(win) < 1)
3826 update_window(struct ws_win *win)
3831 bzero(&wc, sizeof wc);
3832 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
3833 wc.border_width = border_width;
3836 wc.width = WIDTH(win);
3837 wc.height = HEIGHT(win);
3839 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
3840 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3842 XConfigureWindow(display, win->id, mask, &wc);
3845 #define SWM_RESIZE_STEPS (50)
3848 resize(struct ws_win *win, union arg *args)
3852 struct swm_region *r = NULL;
3853 int resize_step = 0;
3857 struct swm_geometry g;
3858 int top = 0, left = 0;
3861 unsigned int shape; /* cursor style */
3867 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
3868 "transient: 0x%lx\n", win->id, YESNO(win->floating),
3871 if (!(win->transient != 0 || win->floating != 0))
3874 /* reject resizes in max mode for floaters (transient ok) */
3875 if (win->floatmaxed)
3879 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3885 case SWM_ARG_ID_WIDTHSHRINK:
3886 WIDTH(win) -= SWM_RESIZE_STEPS;
3889 case SWM_ARG_ID_WIDTHGROW:
3890 WIDTH(win) += SWM_RESIZE_STEPS;
3893 case SWM_ARG_ID_HEIGHTSHRINK:
3894 HEIGHT(win) -= SWM_RESIZE_STEPS;
3897 case SWM_ARG_ID_HEIGHTGROW:
3898 HEIGHT(win) += SWM_RESIZE_STEPS;
3905 constrain_window(win, r, 1);
3907 store_float_geom(win,r);
3911 if (focus_mode == SWM_FOCUS_DEFAULT)
3912 drain_enter_notify();
3914 /* get cursor offset from window root */
3915 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
3920 if (wx < WIDTH(win) / 2)
3923 if (wy < HEIGHT(win) / 2)
3926 if (args->id == SWM_ARG_ID_CENTER)
3929 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
3931 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
3933 cursor = XCreateFontCursor(display, shape);
3935 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3936 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
3937 XFreeCursor(display, cursor);
3942 XMaskEvent(display, MOUSEMASK | ExposureMask |
3943 SubstructureRedirectMask, &ev);
3945 case ConfigureRequest:
3948 handler[ev.type](&ev);
3951 /* cursor offset/delta from start of the operation */
3952 dx = ev.xmotion.x_root - x;
3953 dy = ev.xmotion.y_root - y;
3959 if (args->id == SWM_ARG_ID_CENTER) {
3960 if (g.h / 2 + dy < 1)
3964 HEIGHT(win) = g.h + 2 * dy;
3972 HEIGHT(win) = g.h + dy;
3979 if (args->id == SWM_ARG_ID_CENTER) {
3980 if (g.w / 2 + dx < 1)
3984 WIDTH(win) = g.w + 2 * dx;
3992 WIDTH(win) = g.w + dx;
3995 constrain_window(win, r, 1);
3997 /* not free, don't sync more than 120 times / second */
3998 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3999 time = ev.xmotion.time;
4000 XSync(display, False);
4005 } while (ev.type != ButtonRelease);
4007 XSync(display, False);
4010 store_float_geom(win,r);
4012 XUngrabPointer(display, CurrentTime);
4013 XFreeCursor(display, cursor);
4016 drain_enter_notify();
4020 resize_step(struct swm_region *r, union arg *args)
4022 struct ws_win *win = NULL;
4024 if (r && r->ws && r->ws->focus)
4032 #define SWM_MOVE_STEPS (50)
4035 move(struct ws_win *win, union arg *args)
4040 struct swm_region *r = NULL;
4050 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4051 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4053 /* in max_stack mode should only move transients */
4054 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4058 if (win->floating == 0 && !win->transient) {
4059 store_float_geom(win,r);
4060 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4063 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4070 case SWM_ARG_ID_MOVELEFT:
4071 X(win) -= (SWM_MOVE_STEPS - border_width);
4074 case SWM_ARG_ID_MOVERIGHT:
4075 X(win) += (SWM_MOVE_STEPS - border_width);
4078 case SWM_ARG_ID_MOVEUP:
4079 Y(win) -= (SWM_MOVE_STEPS - border_width);
4082 case SWM_ARG_ID_MOVEDOWN:
4083 Y(win) += (SWM_MOVE_STEPS - border_width);
4090 constrain_window(win, r, 0);
4092 store_float_geom(win, r);
4096 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4097 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4098 CurrentTime) != GrabSuccess)
4101 /* get cursor offset from window root */
4102 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4106 XMaskEvent(display, MOUSEMASK | ExposureMask |
4107 SubstructureRedirectMask, &ev);
4109 case ConfigureRequest:
4112 handler[ev.type](&ev);
4115 X(win) = ev.xmotion.x_root - wx - border_width;
4116 Y(win) = ev.xmotion.y_root - wy - border_width;
4118 constrain_window(win, r, 0);
4120 /* not free, don't sync more than 120 times / second */
4121 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4122 time = ev.xmotion.time;
4123 XSync(display, False);
4128 } while (ev.type != ButtonRelease);
4130 XSync(display, False);
4133 store_float_geom(win,r);
4134 XUngrabPointer(display, CurrentTime);
4137 drain_enter_notify();
4141 move_step(struct swm_region *r, union arg *args)
4143 struct ws_win *win = NULL;
4145 if (r && r->ws && r->ws->focus)
4150 if (!(win->transient != 0 || win->floating != 0))
4157 /* user/key callable function IDs */
4227 kf_search_workspace,
4229 kf_dumpwins, /* MUST BE LAST */
4233 /* key definitions */
4235 dummykeyfunc(struct swm_region *r, union arg *args)
4240 legacyfunc(struct swm_region *r, union arg *args)
4245 char name[SWM_FUNCNAME_LEN];
4246 void (*func)(struct swm_region *r, union arg *);
4248 } keyfuncs[kf_invalid + 1] = {
4249 /* name function argument */
4250 { "cycle_layout", cycle_layout, {0} },
4251 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4252 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4253 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4254 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4255 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4256 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4257 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4258 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4259 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4260 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4261 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4262 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4263 { "spawn_term", spawnterm, {.argv = spawn_term} },
4264 { "spawn_menu", legacyfunc, {0} },
4265 { "quit", quit, {0} },
4266 { "restart", restart, {0} },
4267 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4268 { "ws_1", switchws, {.id = 0} },
4269 { "ws_2", switchws, {.id = 1} },
4270 { "ws_3", switchws, {.id = 2} },
4271 { "ws_4", switchws, {.id = 3} },
4272 { "ws_5", switchws, {.id = 4} },
4273 { "ws_6", switchws, {.id = 5} },
4274 { "ws_7", switchws, {.id = 6} },
4275 { "ws_8", switchws, {.id = 7} },
4276 { "ws_9", switchws, {.id = 8} },
4277 { "ws_10", switchws, {.id = 9} },
4278 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4279 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4280 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4281 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4282 { "ws_prior", priorws, {0} },
4283 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4284 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4285 { "mvws_1", send_to_ws, {.id = 0} },
4286 { "mvws_2", send_to_ws, {.id = 1} },
4287 { "mvws_3", send_to_ws, {.id = 2} },
4288 { "mvws_4", send_to_ws, {.id = 3} },
4289 { "mvws_5", send_to_ws, {.id = 4} },
4290 { "mvws_6", send_to_ws, {.id = 5} },
4291 { "mvws_7", send_to_ws, {.id = 6} },
4292 { "mvws_8", send_to_ws, {.id = 7} },
4293 { "mvws_9", send_to_ws, {.id = 8} },
4294 { "mvws_10", send_to_ws, {.id = 9} },
4295 { "bar_toggle", bar_toggle, {0} },
4296 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4297 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4298 { "screenshot_all", legacyfunc, {0} },
4299 { "screenshot_wind", legacyfunc, {0} },
4300 { "float_toggle", floating_toggle,{0} },
4301 { "version", version, {0} },
4302 { "spawn_lock", legacyfunc, {0} },
4303 { "spawn_initscr", legacyfunc, {0} },
4304 { "spawn_custom", dummykeyfunc, {0} },
4305 { "iconify", iconify, {0} },
4306 { "uniconify", uniconify, {0} },
4307 { "raise_toggle", raise_toggle, {0} },
4308 { "button2", pressbutton, {2} },
4309 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4310 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4311 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4312 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4313 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4314 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4315 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4316 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4317 { "name_workspace", name_workspace, {0} },
4318 { "search_workspace", search_workspace, {0} },
4319 { "search_win", search_win, {0} },
4320 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4321 { "invalid key func", NULL, {0} },
4324 TAILQ_ENTRY(key) entry;
4327 enum keyfuncid funcid;
4330 TAILQ_HEAD(key_list, key);
4331 struct key_list keys = TAILQ_HEAD_INITIALIZER(keys);
4334 enum { client_click, root_click };
4336 unsigned int action;
4338 unsigned int button;
4339 void (*func)(struct ws_win *, union arg *);
4342 /* action key mouse button func args */
4343 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4344 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4345 { client_click, MODKEY, Button1, move, {0} },
4349 update_modkey(unsigned int mod)
4355 TAILQ_FOREACH(kp, &keys, entry)
4356 if (kp->mod & ShiftMask)
4357 kp->mod = mod | ShiftMask;
4361 for (i = 0; i < LENGTH(buttons); i++)
4362 if (buttons[i].mask & ShiftMask)
4363 buttons[i].mask = mod | ShiftMask;
4365 buttons[i].mask = mod;
4375 int spawns_size = 0, spawns_length = 0;
4376 struct spawn_prog *spawns = NULL;
4379 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4382 struct spawn_prog *prog = NULL;
4384 char *ap, **real_args;
4386 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4389 for (i = 0; i < spawns_length; i++) {
4390 if (!strcasecmp(spawn_name, spawns[i].name))
4394 fprintf(stderr, "spawn_custom: program %s not found\n",
4399 /* make room for expanded args */
4400 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4401 err(1, "spawn_custom: calloc real_args");
4403 /* expand spawn_args into real_args */
4404 for (i = 0; i < prog->argc; i++) {
4406 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4407 if (!strcasecmp(ap, "$bar_border")) {
4409 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4411 err(1, "spawn_custom border color");
4412 } else if (!strcasecmp(ap, "$bar_color")) {
4414 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4416 err(1, "spawn_custom bar color");
4417 } else if (!strcasecmp(ap, "$bar_font")) {
4418 if ((real_args[i] = strdup(bar_fonts))
4420 err(1, "spawn_custom bar fonts");
4421 } else if (!strcasecmp(ap, "$bar_font_color")) {
4423 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4425 err(1, "spawn_custom color font");
4426 } else if (!strcasecmp(ap, "$color_focus")) {
4428 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4430 err(1, "spawn_custom color focus");
4431 } else if (!strcasecmp(ap, "$color_unfocus")) {
4433 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4435 err(1, "spawn_custom color unfocus");
4437 /* no match --> copy as is */
4438 if ((real_args[i] = strdup(ap)) == NULL)
4439 err(1, "spawn_custom strdup(ap)");
4441 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4446 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4447 for (i = 0; i < prog->argc; i++)
4448 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4449 DNPRINTF(SWM_D_SPAWN, "\n");
4451 *ret_args = real_args;
4452 return (prog->argc);
4456 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4462 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4466 spawn(r->ws->idx, &a, 1);
4468 for (i = 0; i < spawn_argc; i++)
4474 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4480 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4484 if (pipe(select_list_pipe) == -1)
4485 err(1, "pipe error");
4486 if (pipe(select_resp_pipe) == -1)
4487 err(1, "pipe error");
4489 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4490 err(1, "could not disable SIGPIPE");
4491 switch (*pid = fork()) {
4493 err(1, "cannot fork");
4496 if (dup2(select_list_pipe[0], 0) == -1)
4498 if (dup2(select_resp_pipe[1], 1) == -1)
4500 close(select_list_pipe[1]);
4501 close(select_resp_pipe[0]);
4502 spawn(r->ws->idx, &a, 0);
4504 default: /* parent */
4505 close(select_list_pipe[0]);
4506 close(select_resp_pipe[1]);
4510 for (i = 0; i < spawn_argc; i++)
4516 setspawn(struct spawn_prog *prog)
4520 if (prog == NULL || prog->name == NULL)
4524 for (i = 0; i < spawns_length; i++) {
4525 if (!strcmp(spawns[i].name, prog->name)) {
4527 if (prog->argv == NULL) {
4529 DNPRINTF(SWM_D_SPAWN,
4530 "setspawn: delete #%d: %s\n",
4532 free(spawns[i].name);
4533 for (j = 0; j < spawns[i].argc; j++)
4534 free(spawns[i].argv[j]);
4535 free(spawns[i].argv);
4536 j = spawns_length - 1;
4538 spawns[i] = spawns[j];
4543 DNPRINTF(SWM_D_SPAWN,
4544 "setspawn: replace #%d: %s\n",
4546 free(spawns[i].name);
4547 for (j = 0; j < spawns[i].argc; j++)
4548 free(spawns[i].argv[j]);
4549 free(spawns[i].argv);
4552 /* found case handled */
4558 if (prog->argv == NULL) {
4560 "error: setspawn: cannot find program: %s", prog->name);
4565 /* not found: add */
4566 if (spawns_size == 0 || spawns == NULL) {
4568 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4569 spawns = malloc((size_t)spawns_size *
4570 sizeof(struct spawn_prog));
4571 if (spawns == NULL) {
4572 fprintf(stderr, "setspawn: malloc failed\n");
4576 } else if (spawns_length == spawns_size) {
4578 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4579 spawns = realloc(spawns, (size_t)spawns_size *
4580 sizeof(struct spawn_prog));
4581 if (spawns == NULL) {
4582 fprintf(stderr, "setspawn: realloc failed\n");
4588 if (spawns_length < spawns_size) {
4589 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4590 spawns_length, prog->name);
4591 i = spawns_length++;
4594 fprintf(stderr, "spawns array problem?\n");
4595 if (spawns == NULL) {
4596 fprintf(stderr, "spawns array is NULL!\n");
4604 setconfspawn(char *selector, char *value, int flags)
4606 struct spawn_prog *prog;
4607 char *vp, *cp, *word;
4609 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4610 if ((prog = calloc(1, sizeof *prog)) == NULL)
4611 err(1, "setconfspawn: calloc prog");
4612 prog->name = strdup(selector);
4613 if (prog->name == NULL)
4614 err(1, "setconfspawn prog->name");
4615 if ((cp = vp = strdup(value)) == NULL)
4616 err(1, "setconfspawn: strdup(value) ");
4617 while ((word = strsep(&cp, " \t")) != NULL) {
4618 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4620 cp += (long)strspn(cp, " \t");
4621 if (strlen(word) > 0) {
4623 if ((prog->argv = realloc(prog->argv,
4624 prog->argc * sizeof(char *))) == NULL)
4625 err(1, "setconfspawn: realloc");
4626 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4627 err(1, "setconfspawn: strdup");
4634 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4641 setconfspawn("term", "xterm", 0);
4642 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4643 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4644 setconfspawn("lock", "xlock", 0);
4645 setconfspawn("initscr", "initscreen.sh", 0);
4646 setconfspawn("menu", "dmenu_run"
4649 " -nf $bar_font_color"
4651 " -sf $bar_color", 0);
4652 setconfspawn("search", "dmenu"
4656 " -nf $bar_font_color"
4658 " -sf $bar_color", 0);
4659 setconfspawn("name_workspace", "dmenu"
4663 " -nf $bar_font_color"
4665 " -sf $bar_color", 0);
4669 #define SWM_MODNAME_SIZE 32
4670 #define SWM_KEY_WS "\n+ \t"
4672 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4676 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4677 if (mod == NULL || ks == NULL) {
4678 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4681 if (keystr == NULL || strlen(keystr) == 0) {
4682 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4688 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4689 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4691 cp += (long)strspn(cp, SWM_KEY_WS);
4692 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4694 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4696 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4698 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4700 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4702 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4704 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4705 *mod |= ControlMask;
4707 *ks = XStringToKeysym(name);
4708 XConvertCase(*ks, ks, &uks);
4709 if (ks == NoSymbol) {
4711 "parsekeys: invalid key %s\n",
4717 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4722 strdupsafe(char *str)
4727 return (strdup(str));
4731 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
4735 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
4736 keyfuncs[kfid].name, spawn_name);
4738 if ((kp = malloc(sizeof *kp)) == NULL)
4739 err(1, "key_insert: malloc");
4744 kp->spawn_name = strdupsafe(spawn_name);
4745 TAILQ_INSERT_TAIL(&keys, kp, entry);
4747 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
4751 key_remove(struct key *kp)
4753 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
4755 TAILQ_REMOVE(&keys, kp, entry);
4756 free(kp->spawn_name);
4759 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
4763 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
4766 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
4770 key_insert(mod, ks, kfid, spawn_name);
4772 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
4776 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4781 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4782 keyfuncs[kfid].name, spawn_name);
4784 TAILQ_FOREACH(kp, &keys, entry) {
4785 if (kp->mod == mod && kp->keysym == ks) {
4786 if (kfid == kf_invalid)
4789 key_replace(kp, mod, ks, kfid, spawn_name);
4790 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4794 if (kfid == kf_invalid) {
4795 warnx("error: setkeybinding: cannot find mod/key combination");
4796 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4800 key_insert(mod, ks, kfid, spawn_name);
4801 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4805 setconfbinding(char *selector, char *value, int flags)
4807 enum keyfuncid kfid;
4811 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4812 if (selector == NULL) {
4813 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4814 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4816 setkeybinding(mod, ks, kfid, NULL);
4821 /* search by key function name */
4822 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4823 if (strncasecmp(selector, keyfuncs[kfid].name,
4824 SWM_FUNCNAME_LEN) == 0) {
4825 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4827 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4828 setkeybinding(mod, ks, kfid, NULL);
4834 /* search by custom spawn name */
4835 for (i = 0; i < spawns_length; i++) {
4836 if (strcasecmp(selector, spawns[i].name) == 0) {
4837 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4839 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4840 setkeybinding(mod, ks, kf_spawn_custom,
4847 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4854 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4855 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4856 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4857 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4858 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4859 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4860 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4861 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4862 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4863 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4864 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4865 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4866 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4867 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4868 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4869 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4870 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4871 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4872 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4873 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4874 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4875 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4876 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4877 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4878 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4879 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4880 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4881 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4882 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4883 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4884 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4885 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4886 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4887 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4888 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4889 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4890 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4891 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4892 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4893 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4894 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4895 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4896 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4897 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4898 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4899 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4900 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4901 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4902 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4903 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4904 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4905 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4906 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4907 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4908 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4909 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4910 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4911 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4912 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4913 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4914 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4915 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4916 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4917 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4918 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4919 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4920 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4921 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4922 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4923 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4924 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4926 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4933 struct key *kp_loop, *kp_next;
4935 kp_loop = TAILQ_FIRST(&keys);
4936 while (kp_loop != NULL) {
4937 kp_next = TAILQ_NEXT(kp_loop, entry);
4938 key_remove(kp_loop);
4944 setkeymapping(char *selector, char *value, int flags)
4946 char keymapping_file[PATH_MAX];
4947 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4948 if (value[0] == '~')
4949 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4950 pwd->pw_dir, &value[1]);
4952 strlcpy(keymapping_file, value, sizeof keymapping_file);
4954 /* load new key bindings; if it fails, revert to default bindings */
4955 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4959 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4964 updatenumlockmask(void)
4967 XModifierKeymap *modmap;
4969 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4971 modmap = XGetModifierMapping(display);
4972 for (i = 0; i < 8; i++)
4973 for (j = 0; j < modmap->max_keypermod; j++)
4974 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4975 == XKeysymToKeycode(display, XK_Num_Lock))
4976 numlockmask = (1 << i);
4978 XFreeModifiermap(modmap);
4986 unsigned int modifiers[] =
4987 { 0, LockMask, numlockmask, numlockmask | LockMask };
4990 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4991 updatenumlockmask();
4993 for (k = 0; k < ScreenCount(display); k++) {
4994 if (TAILQ_EMPTY(&screens[k].rl))
4996 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4997 TAILQ_FOREACH(kp, &keys, entry) {
4998 if ((code = XKeysymToKeycode(display, kp->keysym)))
4999 for (j = 0; j < LENGTH(modifiers); j++)
5000 XGrabKey(display, code,
5001 kp->mod | modifiers[j],
5002 screens[k].root, True,
5003 GrabModeAsync, GrabModeAsync);
5009 grabbuttons(struct ws_win *win, int focused)
5012 unsigned int modifiers[] =
5013 { 0, LockMask, numlockmask, numlockmask|LockMask };
5015 updatenumlockmask();
5016 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5018 for (i = 0; i < LENGTH(buttons); i++)
5019 if (buttons[i].action == client_click)
5020 for (j = 0; j < LENGTH(modifiers); j++)
5021 XGrabButton(display, buttons[i].button,
5022 buttons[i].mask | modifiers[j],
5023 win->id, False, BUTTONMASK,
5024 GrabModeAsync, GrabModeSync, None,
5027 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5028 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5031 const char *quirkname[] = {
5032 "NONE", /* config string for "no value" */
5041 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5042 #define SWM_Q_WS "\n|+ \t"
5044 parsequirks(char *qstr, unsigned long *quirk)
5054 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5056 cp += (long)strspn(cp, SWM_Q_WS);
5057 for (i = 0; i < LENGTH(quirkname); i++) {
5058 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5059 DNPRINTF(SWM_D_QUIRK,
5060 "parsequirks: %s\n", name);
5065 *quirk |= 1 << (i-1);
5069 if (i >= LENGTH(quirkname)) {
5070 DNPRINTF(SWM_D_QUIRK,
5071 "parsequirks: invalid quirk [%s]\n", name);
5079 quirk_insert(const char *class, const char *name, unsigned long quirk)
5083 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5086 if ((qp = malloc(sizeof *qp)) == NULL)
5087 err(1, "quirk_insert: malloc");
5088 if ((qp->class = strdup(class)) == NULL)
5089 err(1, "quirk_insert: strdup");
5090 if ((qp->name = strdup(name)) == NULL)
5091 err(1, "quirk_insert: strdup");
5094 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5096 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5100 quirk_remove(struct quirk *qp)
5102 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5103 qp->name, qp->quirk);
5105 TAILQ_REMOVE(&quirks, qp, entry);
5110 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5114 quirk_replace(struct quirk *qp, const char *class, const char *name,
5115 unsigned long quirk)
5117 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5118 qp->name, qp->quirk);
5121 quirk_insert(class, name, quirk);
5123 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5127 setquirk(const char *class, const char *name, unsigned long quirk)
5131 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5134 TAILQ_FOREACH(qp, &quirks, entry) {
5135 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5139 quirk_replace(qp, class, name, quirk);
5140 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5145 warnx("error: setquirk: cannot find class/name combination");
5149 quirk_insert(class, name, quirk);
5150 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5154 setconfquirk(char *selector, char *value, int flags)
5156 char *cp, *class, *name;
5158 unsigned long quirks;
5159 if (selector == NULL)
5161 if ((cp = strchr(selector, ':')) == NULL)
5166 if ((retval = parsequirks(value, &quirks)) == 0)
5167 setquirk(class, name, quirks);
5174 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5175 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5176 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5177 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5178 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5179 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5180 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5181 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5182 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5183 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5184 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5185 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5186 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5189 /* conf file stuff */
5190 #define SWM_CONF_FILE "scrotwm.conf"
5192 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5193 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5194 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5195 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5196 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5197 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5198 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5199 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5200 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5204 setconfvalue(char *selector, char *value, int flags)
5210 case SWM_S_BAR_DELAY:
5211 bar_delay = atoi(value);
5213 case SWM_S_BAR_ENABLED:
5214 bar_enabled = atoi(value);
5216 case SWM_S_BAR_BORDER_WIDTH:
5217 bar_border_width = atoi(value);
5219 case SWM_S_BAR_AT_BOTTOM:
5220 bar_at_bottom = atoi(value);
5222 case SWM_S_BAR_JUSTIFY:
5223 if (!strcmp(value, "left"))
5224 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5225 else if (!strcmp(value, "center"))
5226 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5227 else if (!strcmp(value, "right"))
5228 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5230 errx(1, "invalid bar_justify");
5232 case SWM_S_STACK_ENABLED:
5233 stack_enabled = atoi(value);
5235 case SWM_S_CLOCK_ENABLED:
5236 clock_enabled = atoi(value);
5238 case SWM_S_CLOCK_FORMAT:
5239 #ifndef SWM_DENY_CLOCK_FORMAT
5241 if ((clock_format = strdup(value)) == NULL)
5242 err(1, "setconfvalue: clock_format");
5245 case SWM_S_CYCLE_EMPTY:
5246 cycle_empty = atoi(value);
5248 case SWM_S_CYCLE_VISIBLE:
5249 cycle_visible = atoi(value);
5251 case SWM_S_SS_ENABLED:
5252 ss_enabled = atoi(value);
5254 case SWM_S_TERM_WIDTH:
5255 term_width = atoi(value);
5257 case SWM_S_TITLE_CLASS_ENABLED:
5258 title_class_enabled = atoi(value);
5260 case SWM_S_WINDOW_NAME_ENABLED:
5261 window_name_enabled = atoi(value);
5263 case SWM_S_TITLE_NAME_ENABLED:
5264 title_name_enabled = atoi(value);
5266 case SWM_S_URGENT_ENABLED:
5267 urgent_enabled = atoi(value);
5269 case SWM_S_FOCUS_MODE:
5270 if (!strcmp(value, "default"))
5271 focus_mode = SWM_FOCUS_DEFAULT;
5272 else if (!strcmp(value, "follow_cursor"))
5273 focus_mode = SWM_FOCUS_FOLLOW;
5274 else if (!strcmp(value, "synergy"))
5275 focus_mode = SWM_FOCUS_SYNERGY;
5277 errx(1, "focus_mode");
5279 case SWM_S_DISABLE_BORDER:
5280 disable_border = atoi(value);
5282 case SWM_S_BORDER_WIDTH:
5283 border_width = atoi(value);
5285 case SWM_S_BAR_FONT:
5287 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5288 err(1, "setconfvalue: asprintf: failed to allocate "
5289 "memory for bar_fonts.");
5293 case SWM_S_BAR_ACTION:
5295 if ((bar_argv[0] = strdup(value)) == NULL)
5296 err(1, "setconfvalue: bar_action");
5298 case SWM_S_SPAWN_TERM:
5299 free(spawn_term[0]);
5300 if ((spawn_term[0] = strdup(value)) == NULL)
5301 err(1, "setconfvalue: spawn_term");
5305 case SWM_S_DIALOG_RATIO:
5306 dialog_ratio = atof(value);
5307 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5310 case SWM_S_VERBOSE_LAYOUT:
5311 verbose_layout = atoi(value);
5312 for (i = 0; layouts[i].l_stack != NULL; i++) {
5314 layouts[i].l_string = fancy_stacker;
5316 layouts[i].l_string = plain_stacker;
5326 setconfmodkey(char *selector, char *value, int flags)
5328 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5329 update_modkey(Mod1Mask);
5330 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5331 update_modkey(Mod2Mask);
5332 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5333 update_modkey(Mod3Mask);
5334 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5335 update_modkey(Mod4Mask);
5342 setconfcolor(char *selector, char *value, int flags)
5344 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5349 setconfregion(char *selector, char *value, int flags)
5351 custom_region(value);
5356 setautorun(char *selector, char *value, int flags)
5366 if (getenv("SWM_STARTED"))
5370 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5371 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5373 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5374 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5377 * This is a little intricate
5379 * If the pid already exists we simply reuse it because it means it was
5380 * used before AND not claimed by manage_window. We get away with
5381 * altering it in the parent after INSERT because this can not be a race
5384 while ((ap = strsep(&sp, " \t")) != NULL) {
5387 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5389 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5390 err(1, "setautorun: realloc");
5391 a.argv[argc - 1] = ap;
5394 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5395 err(1, "setautorun: realloc");
5396 a.argv[argc] = NULL;
5398 if ((pid = fork()) == 0) {
5399 spawn(ws_id, &a, 1);
5408 p = calloc(1, sizeof *p);
5411 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5421 setlayout(char *selector, char *value, int flags)
5423 int ws_id, i, x, mg, ma, si, raise;
5424 int st = SWM_V_STACK;
5426 struct workspace *ws;
5428 if (getenv("SWM_STARTED"))
5432 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5433 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5434 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5435 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5438 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5439 errx(1, "layout: invalid workspace %d", ws_id + 1);
5441 if (!strcasecmp(s, "vertical"))
5443 else if (!strcasecmp(s, "horizontal"))
5445 else if (!strcasecmp(s, "fullscreen"))
5448 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5449 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5452 for (i = 0; i < ScreenCount(display); i++) {
5453 ws = (struct workspace *)&screens[i].ws;
5454 ws[ws_id].cur_layout = &layouts[st];
5456 ws[ws_id].always_raise = raise;
5457 if (st == SWM_MAX_STACK)
5461 for (x = 0; x < abs(mg); x++) {
5462 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5463 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5464 SWM_ARG_ID_MASTERSHRINK);
5468 for (x = 0; x < abs(ma); x++) {
5469 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5470 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5471 SWM_ARG_ID_MASTERDEL);
5475 for (x = 0; x < abs(si); x++) {
5476 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5477 si >= 0 ? SWM_ARG_ID_STACKINC :
5478 SWM_ARG_ID_STACKDEC);
5486 /* config options */
5487 struct config_option {
5489 int (*func)(char*, char*, int);
5492 struct config_option configopt[] = {
5493 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5494 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5495 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5496 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5497 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5498 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5499 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5500 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5501 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5502 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5503 { "keyboard_mapping", setkeymapping, 0 },
5504 { "bind", setconfbinding, 0 },
5505 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5506 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5507 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5508 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5509 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5510 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5511 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5512 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5513 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5514 { "modkey", setconfmodkey, 0 },
5515 { "program", setconfspawn, 0 },
5516 { "quirk", setconfquirk, 0 },
5517 { "region", setconfregion, 0 },
5518 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5519 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5520 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5521 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5522 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5523 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5524 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5525 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5526 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5527 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5528 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5529 { "autorun", setautorun, 0 },
5530 { "layout", setlayout, 0 },
5535 conf_load(char *filename, int keymapping)
5538 char *line, *cp, *optsub, *optval;
5539 size_t linelen, lineno = 0;
5540 int wordlen, i, optind;
5541 struct config_option *opt;
5543 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5545 if (filename == NULL) {
5546 fprintf(stderr, "conf_load: no filename\n");
5549 if ((config = fopen(filename, "r")) == NULL) {
5550 warn("conf_load: fopen: %s", filename);
5554 while (!feof(config)) {
5555 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5558 err(1, "%s", filename);
5563 cp += strspn(cp, " \t\n"); /* eat whitespace */
5564 if (cp[0] == '\0') {
5569 /* get config option */
5570 wordlen = strcspn(cp, "=[ \t\n");
5572 warnx("%s: line %zd: no option found",
5577 for (i = 0; i < LENGTH(configopt); i++) {
5578 opt = &configopt[i];
5579 if (!strncasecmp(cp, opt->optname, wordlen) &&
5580 strlen(opt->optname) == wordlen) {
5586 warnx("%s: line %zd: unknown option %.*s",
5587 filename, lineno, wordlen, cp);
5590 if (keymapping && strcmp(opt->optname, "bind")) {
5591 warnx("%s: line %zd: invalid option %.*s",
5592 filename, lineno, wordlen, cp);
5596 cp += strspn(cp, " \t\n"); /* eat whitespace */
5597 /* get [selector] if any */
5601 wordlen = strcspn(cp, "]");
5604 warnx("%s: line %zd: syntax error",
5609 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5611 warnx("%s: line %zd: unable to allocate"
5612 "memory for selector", filename,
5618 cp += strspn(cp, "] \t\n"); /* eat trailing */
5620 cp += strspn(cp, "= \t\n"); /* eat trailing */
5622 optval = strdup(cp);
5623 /* call function to deal with it all */
5624 if (configopt[optind].func(optsub, optval,
5625 configopt[optind].funcflags) != 0) {
5626 fprintf(stderr, "%s line %zd: %s\n",
5627 filename, lineno, line);
5628 errx(1, "%s: line %zd: invalid data for %s",
5629 filename, lineno, configopt[optind].optname);
5637 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5644 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
5650 set_child_transient(struct ws_win *win, Window *trans)
5652 struct ws_win *parent, *w;
5653 XWMHints *wmh = NULL;
5654 struct swm_region *r;
5655 struct workspace *ws;
5657 parent = find_window(win->transient);
5659 parent->child_trans = win;
5661 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5662 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
5664 if (win->hints == NULL) {
5665 fprintf(stderr, "no hints for 0x%lx\n", win->id);
5669 r = root_to_region(win->wa.root);
5671 /* parent doen't exist in our window list */
5672 TAILQ_FOREACH(w, &ws->winlist, entry) {
5676 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5677 fprintf(stderr, "can't get hints for 0x%lx\n",
5682 if (win->hints->window_group != wmh->window_group)
5685 w->child_trans = win;
5686 win->transient = w->id;
5688 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5689 "transient to 0x%lx\n", win->transient);
5699 window_get_pid(Window win)
5701 Atom actual_type_return;
5702 int actual_format_return = 0;
5703 unsigned long nitems_return = 0;
5704 unsigned long bytes_after_return = 0;
5708 unsigned char *prop = NULL;
5710 if (XGetWindowProperty(display, win,
5711 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5712 XA_CARDINAL, &actual_type_return, &actual_format_return,
5713 &nitems_return, &bytes_after_return,
5714 (unsigned char**)(void*)&pid) != Success)
5716 if (actual_type_return != XA_CARDINAL)
5727 if (XGetWindowProperty(display, win,
5728 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5729 XA_STRING, &actual_type_return, &actual_format_return,
5730 &nitems_return, &bytes_after_return, &prop) != Success)
5732 if (actual_type_return != XA_STRING)
5737 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5738 /* ignore error because strtonum returns 0 anyway */
5745 manage_window(Window id)
5748 struct workspace *ws;
5749 struct ws_win *win, *ww;
5750 int format, i, ws_idx, n, border_me = 0;
5751 unsigned long nitems, bytes;
5752 Atom ws_idx_atom = 0, type;
5753 Atom *prot = NULL, *pp;
5754 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5755 struct swm_region *r;
5762 if ((win = find_window(id)) != NULL)
5763 return (win); /* already being managed */
5765 /* see if we are on the unmanaged list */
5766 if ((win = find_unmanaged_window(id)) != NULL) {
5767 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
5768 "window: 0x%lx\n", win->id);
5769 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5770 if (win->transient) {
5771 set_child_transient(win, &trans);
5772 } if (trans && (ww = find_window(trans)))
5773 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5775 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5776 ewmh_update_actions(win);
5780 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5781 err(1, "manage_window: calloc: failed to allocate memory for "
5786 /* see if we need to override the workspace */
5787 p = find_pid(window_get_pid(id));
5789 /* Get all the window data in one shot */
5790 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5792 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5793 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5795 XGetWindowAttributes(display, id, &win->wa);
5796 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
5797 win->hints = XGetWMHints(display, id);
5798 XGetTransientForHint(display, id, &trans);
5800 win->transient = trans;
5801 set_child_transient(win, &trans);
5802 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
5803 "transient: 0x%lx\n", win->id, win->transient);
5806 /* get supported protocols */
5807 if (XGetWMProtocols(display, id, &prot, &n)) {
5808 for (i = 0, pp = prot; i < n; i++, pp++) {
5809 if (*pp == takefocus)
5810 win->take_focus = 1;
5812 win->can_delete = 1;
5818 win->iconic = get_iconic(win);
5821 * Figure out where to put the window. If it was previously assigned to
5822 * a workspace (either by spawn() or manually moving), and isn't
5823 * transient, * put it in the same workspace
5825 r = root_to_region(win->wa.root);
5827 ws = &r->s->ws[p->ws];
5828 TAILQ_REMOVE(&pidlist, p, entry);
5831 } else if (prop && win->transient == 0) {
5832 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
5833 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5835 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
5838 ws = &r->s->ws[ws_idx];
5841 /* this should launch transients in the same ws as parent */
5843 if ((ww = find_window(trans)) != NULL)
5850 "manage_window: fix this "
5856 /* set up the window layout */
5859 win->s = r->s; /* this never changes */
5860 if (trans && (ww = find_window(trans)))
5861 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5863 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5865 WIDTH(win) = win->wa.width;
5866 HEIGHT(win) = win->wa.height;
5869 win->g_floatvalid = 0;
5870 win->floatmaxed = 0;
5871 win->ewmh_flags = 0;
5873 /* Set window properties so we can remember this after reincarnation */
5874 if (ws_idx_atom && prop == NULL &&
5875 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5877 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
5879 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5880 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5885 ewmh_autoquirk(win);
5887 if (XGetClassHint(display, win->id, &win->ch)) {
5888 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
5889 win->ch.res_class, win->ch.res_name);
5891 /* java is retarded so treat it special */
5892 if (strstr(win->ch.res_name, "sun-awt")) {
5897 TAILQ_FOREACH(qp, &quirks, entry) {
5898 if (!strcmp(win->ch.res_class, qp->class) &&
5899 !strcmp(win->ch.res_name, qp->name)) {
5900 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
5901 "class: %s, name: %s\n", win->ch.res_class,
5903 if (qp->quirk & SWM_Q_FLOAT) {
5907 win->quirks = qp->quirk;
5912 /* alter window position if quirky */
5913 if (win->quirks & SWM_Q_ANYWHERE) {
5914 win->manual = 1; /* don't center the quirky windows */
5915 bzero(&wc, sizeof wc);
5917 if (bar_enabled && Y(win) < bar_height) {
5918 Y(win) = wc.y = bar_height;
5921 if (WIDTH(win) + X(win) > WIDTH(r)) {
5922 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
5928 /* Reset font sizes (the bruteforce way; no default keybinding). */
5929 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5930 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5931 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5932 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5933 fake_keypress(win, XK_KP_Add, ShiftMask);
5936 ewmh_get_win_state(win);
5937 ewmh_update_actions(win);
5938 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5942 bzero(&wc, sizeof wc);
5943 wc.border_width = border_width;
5944 mask = CWBorderWidth;
5945 XConfigureWindow(display, win->id, mask, &wc);
5948 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5949 PropertyChangeMask | StructureNotifyMask);
5951 /* floaters need to be mapped if they are in the current workspace */
5952 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5953 XMapRaised(display, win->id);
5959 free_window(struct ws_win *win)
5961 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
5966 /* needed for restart wm */
5967 set_win_state(win, WithdrawnState);
5969 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5971 if (win->ch.res_class)
5972 XFree(win->ch.res_class);
5973 if (win->ch.res_name)
5974 XFree(win->ch.res_name);
5979 memset(win, 0xff, sizeof *win); /* XXX kill later */
5985 unmanage_window(struct ws_win *win)
5987 struct ws_win *parent;
5992 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
5994 if (win->transient) {
5995 parent = find_window(win->transient);
5997 parent->child_trans = NULL;
6000 /* focus on root just in case */
6001 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6010 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6011 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6017 focus_magic(struct ws_win *win)
6019 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6024 if (win->child_trans) {
6025 /* win = parent & has a transient so focus on that */
6027 focus_win(win->child_trans);
6028 if (win->child_trans->take_focus)
6029 client_msg(win, takefocus);
6031 /* make sure transient hasn't dissapeared */
6032 if (validate_win(win->child_trans) == 0) {
6033 focus_win(win->child_trans);
6034 if (win->child_trans->take_focus)
6035 client_msg(win->child_trans, takefocus);
6037 win->child_trans = NULL;
6039 if (win->take_focus)
6040 client_msg(win, takefocus);
6046 if (win->take_focus)
6047 client_msg(win, takefocus);
6054 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6061 XKeyEvent *ev = &e->xkey;
6064 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
6065 TAILQ_FOREACH(kp, &keys, entry)
6066 if (keysym == kp->keysym
6067 && CLEANMASK(kp->mod) == CLEANMASK(ev->state)
6068 && keyfuncs[kp->funcid].func) {
6069 if (kp->funcid == kf_spawn_custom)
6071 root_to_region(ev->root),
6072 &(keyfuncs[kp->funcid].args),
6076 keyfuncs[kp->funcid].func(
6077 root_to_region(ev->root),
6078 &(keyfuncs[kp->funcid].args)
6084 buttonpress(XEvent *e)
6088 XButtonPressedEvent *ev = &e->xbutton;
6090 if ((win = find_window(ev->window)) == NULL)
6094 action = client_click;
6096 for (i = 0; i < LENGTH(buttons); i++)
6097 if (action == buttons[i].action && buttons[i].func &&
6098 buttons[i].button == ev->button &&
6099 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6100 buttons[i].func(win, &buttons[i].args);
6104 configurerequest(XEvent *e)
6106 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6111 if ((win = find_window(ev->window)) == NULL)
6112 if ((win = find_unmanaged_window(ev->window)) == NULL)
6115 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6116 ev->window, YESNO(new));
6119 bzero(&wc, sizeof wc);
6122 wc.width = ev->width;
6123 wc.height = ev->height;
6124 wc.border_width = ev->border_width;
6125 wc.sibling = ev->above;
6126 wc.stack_mode = ev->detail;
6127 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6129 config_win(win, ev);
6134 configurenotify(XEvent *e)
6138 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6139 e->xconfigure.window);
6141 win = find_window(e->xconfigure.window);
6143 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6147 if (focus_mode == SWM_FOCUS_DEFAULT)
6148 drain_enter_notify();
6153 destroynotify(XEvent *e)
6156 XDestroyWindowEvent *ev = &e->xdestroywindow;
6158 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6160 if ((win = find_window(ev->window)) == NULL) {
6161 if ((win = find_unmanaged_window(ev->window)) == NULL)
6167 /* make sure we focus on something */
6170 unmanage_window(win);
6172 if (focus_mode == SWM_FOCUS_DEFAULT)
6173 drain_enter_notify();
6178 enternotify(XEvent *e)
6180 XCrossingEvent *ev = &e->xcrossing;
6185 Window focus_return;
6186 int revert_to_return;
6188 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6189 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6190 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6191 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6193 if (ev->mode != NotifyNormal) {
6194 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6199 switch (focus_mode) {
6200 case SWM_FOCUS_DEFAULT:
6202 case SWM_FOCUS_FOLLOW:
6204 case SWM_FOCUS_SYNERGY:
6207 * all these checks need to be in this order because the
6208 * XCheckTypedWindowEvent relies on weeding out the previous events
6210 * making this code an option would enable a follow mouse for focus
6215 * state is set when we are switching workspaces and focus is set when
6216 * the window or a subwindow already has focus (occurs during restart).
6218 * Only honor the focus flag if last_focus_event is not FocusOut,
6219 * this allows scrotwm to continue to control focus when another
6220 * program is also playing with it.
6222 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6223 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6228 * happens when a window is created or destroyed and the border
6229 * crosses the mouse pointer and when switching ws
6231 * we need the subwindow test to see if we came from root in order
6232 * to give focus to floaters
6234 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6235 ev->subwindow == 0) {
6236 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6240 /* this window already has focus */
6241 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6242 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6246 /* this window is being deleted or moved to another ws */
6247 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6249 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6250 XPutBackEvent(display, &cne);
6254 if ((win = find_window(ev->window)) == NULL) {
6255 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6260 * In fullstack kill all enters unless they come from a different ws
6261 * (i.e. another region) or focus has been grabbed externally.
6263 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6264 last_focus_event != FocusOut) {
6265 XGetInputFocus(display, &focus_return, &revert_to_return);
6266 if ((w = find_window(focus_return)) == NULL ||
6268 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6276 if ((win = find_window(ev->window)) == NULL) {
6277 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6282 * if we have more enternotifies let them handle it in due time
6284 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6285 DNPRINTF(SWM_D_EVENT,
6286 "ignoring enternotify: got more enternotify\n");
6287 XPutBackEvent(display, &cne);
6294 /* lets us use one switch statement for arbitrary mode/detail combinations */
6295 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6298 focusevent(XEvent *e)
6302 u_int32_t mode_detail;
6303 XFocusChangeEvent *ev = &e->xfocus;
6305 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6306 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6307 ev->window, ev->mode, ev->detail);
6309 if (last_focus_event == ev->type) {
6310 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6314 last_focus_event = ev->type;
6315 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6317 switch (mode_detail) {
6318 /* synergy client focus operations */
6319 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6320 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6322 /* synergy server focus operations */
6323 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6325 /* Entering applications like rdesktop that mangle the pointer */
6326 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6328 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6329 XSetWindowBorder(display, win->id,
6330 win->ws->r->s->c[ev->type == FocusIn ?
6331 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6334 fprintf(stderr, "ignoring focusevent\n");
6335 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6342 mapnotify(XEvent *e)
6345 XMapEvent *ev = &e->xmap;
6347 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6349 win = manage_window(ev->window);
6351 set_win_state(win, NormalState);
6355 mappingnotify(XEvent *e)
6357 XMappingEvent *ev = &e->xmapping;
6359 XRefreshKeyboardMapping(ev);
6360 if (ev->request == MappingKeyboard)
6365 maprequest(XEvent *e)
6368 struct swm_region *r;
6369 XWindowAttributes wa;
6370 XMapRequestEvent *ev = &e->xmaprequest;
6372 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6373 e->xmaprequest.window);
6375 if (!XGetWindowAttributes(display, ev->window, &wa))
6377 if (wa.override_redirect)
6380 win = manage_window(e->xmaprequest.window);
6382 return; /* can't happen */
6386 /* make new win focused */
6387 r = root_to_region(win->wa.root);
6388 if (win->ws == r->ws)
6393 propertynotify(XEvent *e)
6396 XPropertyEvent *ev = &e->xproperty;
6399 name = XGetAtomName(display, ev->atom);
6400 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6405 win = find_window(ev->window);
6409 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6410 update_iconic(win, 0);
6411 XMapRaised(display, win->id);
6419 case XA_WM_NORMAL_HINTS:
6421 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6422 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6423 if (win->sh.flags & PMinSize) {
6424 WIDTH(win) = win->sh.min_width;
6425 HEIGHT(win) = win->sh.min_height;
6426 fprintf(stderr, "min %d %d\n", WIDTH(win), HEIGHT(win));
6428 XMoveResizeWindow(display, win->id,
6429 X(win), Y(win), WIDTH(win), HEIGHT(win));
6432 if (title_name_enabled || title_class_enabled)
6436 if (window_name_enabled)
6445 unmapnotify(XEvent *e)
6449 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6451 /* determine if we need to help unmanage this window */
6452 win = find_window(e->xunmap.window);
6456 if (getstate(e->xunmap.window) == NormalState) {
6457 unmanage_window(win);
6460 /* giant hack for apps that don't destroy transient windows */
6461 /* eat a bunch of events to prevent remanaging the window */
6463 while (XCheckWindowEvent(display, e->xunmap.window,
6464 EnterWindowMask, &cne))
6466 while (XCheckWindowEvent(display, e->xunmap.window,
6467 StructureNotifyMask, &cne))
6469 while (XCheckWindowEvent(display, e->xunmap.window,
6470 SubstructureNotifyMask, &cne))
6472 /* resend unmap because we ated it */
6473 XUnmapWindow(display, e->xunmap.window);
6476 if (focus_mode == SWM_FOCUS_DEFAULT)
6477 drain_enter_notify();
6481 visibilitynotify(XEvent *e)
6484 struct swm_region *r;
6486 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6487 e->xvisibility.window);
6488 if (e->xvisibility.state == VisibilityUnobscured)
6489 for (i = 0; i < ScreenCount(display); i++)
6490 TAILQ_FOREACH(r, &screens[i].rl, entry)
6491 if (e->xvisibility.window == r->bar_window)
6496 clientmessage(XEvent *e)
6498 XClientMessageEvent *ev;
6503 win = find_window(ev->window);
6507 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6508 ev->window, ev->message_type);
6510 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6511 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6514 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6515 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6516 if (win->can_delete)
6517 client_msg(win, adelete);
6519 XKillClient(display, win->id);
6521 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6522 DNPRINTF(SWM_D_EVENT,
6523 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6524 if (win->floating) {
6525 if (ev->data.l[0] & (1<<8)) /* x */
6526 X(win) = ev->data.l[1];
6527 if (ev->data.l[0] & (1<<9)) /* y */
6528 Y(win) = ev->data.l[2];
6529 if (ev->data.l[0] & (1<<10)) /* width */
6530 WIDTH(win) = ev->data.l[3];
6531 if (ev->data.l[0] & (1<<11)) /* height */
6532 HEIGHT(win) = ev->data.l[4];
6537 /* TODO: Change stack sizes */
6538 /* notify no change was made. */
6539 config_win(win, NULL);
6542 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6543 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6544 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6546 ewmh_update_win_state(win, ev->data.l[2],
6554 xerror_start(Display *d, XErrorEvent *ee)
6561 xerror(Display *d, XErrorEvent *ee)
6563 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6571 xerrorxlib = XSetErrorHandler(xerror_start);
6573 /* this causes an error if some other window manager is running */
6574 XSelectInput(display, DefaultRootWindow(display),
6575 SubstructureRedirectMask);
6576 XSync(display, False);
6580 XSetErrorHandler(xerror);
6581 XSync(display, False);
6586 new_region(struct swm_screen *s, int x, int y, int w, int h)
6588 struct swm_region *r, *n;
6589 struct workspace *ws = NULL;
6592 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6593 s->idx, w, h, x, y);
6595 /* remove any conflicting regions */
6596 n = TAILQ_FIRST(&s->rl);
6599 n = TAILQ_NEXT(r, entry);
6600 if (X(r) < (x + w) &&
6601 (X(r) + WIDTH(r)) > x &&
6603 (Y(r) + HEIGHT(r)) > y) {
6604 if (r->ws->r != NULL)
6605 r->ws->old_r = r->ws->r;
6607 XDestroyWindow(display, r->bar_window);
6608 TAILQ_REMOVE(&s->rl, r, entry);
6609 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6613 /* search old regions for one to reuse */
6615 /* size + location match */
6616 TAILQ_FOREACH(r, &s->orl, entry)
6617 if (X(r) == x && Y(r) == y &&
6618 HEIGHT(r) == h && WIDTH(r) == w)
6622 TAILQ_FOREACH(r, &s->orl, entry)
6623 if (HEIGHT(r) == h && WIDTH(r) == w)
6627 TAILQ_REMOVE(&s->orl, r, entry);
6628 /* try to use old region's workspace */
6629 if (r->ws->r == NULL)
6632 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6633 err(1, "new_region: calloc: failed to allocate memory "
6636 /* if we don't have a workspace already, find one */
6638 for (i = 0; i < SWM_WS_MAX; i++)
6639 if (s->ws[i].r == NULL) {
6646 errx(1, "new_region: no free workspaces");
6657 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6663 #ifdef SWM_XRR_HAS_CRTC
6665 XRRScreenResources *sr;
6668 #endif /* SWM_XRR_HAS_CRTC */
6669 struct swm_region *r;
6672 if (i >= ScreenCount(display))
6673 errx(1, "scan_xrandr: invalid screen");
6675 /* remove any old regions */
6676 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6677 r->ws->old_r = r->ws->r = NULL;
6678 XDestroyWindow(display, r->bar_window);
6679 TAILQ_REMOVE(&screens[i].rl, r, entry);
6680 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6684 /* map virtual screens onto physical screens */
6685 #ifdef SWM_XRR_HAS_CRTC
6686 if (xrandr_support) {
6687 sr = XRRGetScreenResources(display, screens[i].root);
6689 new_region(&screens[i], 0, 0,
6690 DisplayWidth(display, i),
6691 DisplayHeight(display, i));
6695 for (c = 0, ci = NULL; c < ncrtc; c++) {
6696 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6697 if (ci->noutput == 0)
6700 if (ci != NULL && ci->mode == None)
6701 new_region(&screens[i], 0, 0,
6702 DisplayWidth(display, i),
6703 DisplayHeight(display, i));
6705 new_region(&screens[i],
6706 ci->x, ci->y, ci->width, ci->height);
6709 XRRFreeCrtcInfo(ci);
6710 XRRFreeScreenResources(sr);
6712 #endif /* SWM_XRR_HAS_CRTC */
6714 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6715 DisplayHeight(display, i));
6720 screenchange(XEvent *e) {
6721 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6722 struct swm_region *r;
6725 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
6727 if (!XRRUpdateConfiguration(e))
6730 /* silly event doesn't include the screen index */
6731 for (i = 0; i < ScreenCount(display); i++)
6732 if (screens[i].root == xe->root)
6734 if (i >= ScreenCount(display))
6735 errx(1, "screenchange: screen not found");
6737 /* brute force for now, just re-enumerate the regions */
6740 /* add bars to all regions */
6741 for (i = 0; i < ScreenCount(display); i++)
6742 TAILQ_FOREACH(r, &screens[i].rl, entry)
6745 if (focus_mode == SWM_FOCUS_DEFAULT)
6746 drain_enter_notify();
6752 Window d1, d2, *wins = NULL;
6753 XWindowAttributes wa;
6758 for (i = 0; i < ScreenCount(display); i++) {
6759 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6762 /* attach windows to a region */
6763 /* normal windows */
6764 for (j = 0; j < no; j++) {
6765 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6766 wa.override_redirect ||
6767 XGetTransientForHint(display, wins[j], &d1))
6770 state = getstate(wins[j]);
6771 manage = state == IconicState;
6772 if (wa.map_state == IsViewable || manage)
6773 manage_window(wins[j]);
6775 /* transient windows */
6776 for (j = 0; j < no; j++) {
6777 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6778 wa.override_redirect)
6781 state = getstate(wins[j]);
6782 manage = state == IconicState;
6783 if (XGetTransientForHint(display, wins[j], &d1) &&
6785 manage_window(wins[j]);
6798 int errorbase, major, minor;
6799 struct workspace *ws;
6802 if ((screens = calloc(ScreenCount(display),
6803 sizeof(struct swm_screen))) == NULL)
6804 err(1, "setup_screens: calloc: failed to allocate memory for "
6807 /* initial Xrandr setup */
6808 xrandr_support = XRRQueryExtension(display,
6809 &xrandr_eventbase, &errorbase);
6811 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6814 /* map physical screens */
6815 for (i = 0; i < ScreenCount(display); i++) {
6816 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
6818 TAILQ_INIT(&screens[i].rl);
6819 TAILQ_INIT(&screens[i].orl);
6820 screens[i].root = RootWindow(display, i);
6822 /* set default colors */
6823 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6824 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6825 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6826 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6827 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6829 /* create graphics context on screen with default font color */
6830 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
6833 XSetForeground(display, screens[i].bar_gc,
6834 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6836 /* set default cursor */
6837 XDefineCursor(display, screens[i].root,
6838 XCreateFontCursor(display, XC_left_ptr));
6840 /* init all workspaces */
6841 /* XXX these should be dynamically allocated too */
6842 for (j = 0; j < SWM_WS_MAX; j++) {
6843 ws = &screens[i].ws[j];
6849 TAILQ_INIT(&ws->winlist);
6850 TAILQ_INIT(&ws->unmanagedlist);
6852 for (k = 0; layouts[k].l_stack != NULL; k++)
6853 if (layouts[k].l_config != NULL)
6854 layouts[k].l_config(ws,
6855 SWM_ARG_ID_STACKINIT);
6856 ws->cur_layout = &layouts[0];
6857 ws->cur_layout->l_string(ws);
6863 XRRSelectInput(display, screens[i].root,
6864 RRScreenChangeNotifyMask);
6871 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
6872 err(1, "setup_globals: strdup: failed to allocate memory.");
6874 if ((spawn_term[0] = strdup("xterm")) == NULL)
6875 err(1, "setup_globals: strdup: failed to allocate memory.");
6877 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6878 err(1, "setup_globals: strdup: failed to allocate memory.");
6885 Atom netwmcheck, netwmname, utf8_string;
6888 /* work around sun jdk bugs, code from wmname */
6889 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6890 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6891 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6892 for (i = 0; i < ScreenCount(display); i++) {
6893 root = screens[i].root;
6894 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6895 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6896 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6898 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6899 PropModeReplace, (unsigned char *)&win,1);
6900 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6901 PropModeReplace, (unsigned char *)&win,1);
6902 XChangeProperty(display, win, netwmname, utf8_string, 8,
6903 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6908 main(int argc, char *argv[])
6910 struct swm_region *r, *rr;
6911 struct ws_win *winfocus = NULL;
6914 char conf[PATH_MAX], *cfile = NULL;
6919 struct sigaction sact;
6922 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6923 SCROTWM_VERSION, buildstr);
6924 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6926 warnx("no locale support");
6928 if (!X_HAVE_UTF8_STRING)
6929 warnx("no UTF-8 support");
6931 if (!(display = XOpenDisplay(0)))
6932 errx(1, "can not open display");
6935 errx(1, "other wm running");
6937 /* handle some signals */
6938 bzero(&sact, sizeof(sact));
6939 sigemptyset(&sact.sa_mask);
6941 sact.sa_handler = sighdlr;
6942 sigaction(SIGINT, &sact, NULL);
6943 sigaction(SIGQUIT, &sact, NULL);
6944 sigaction(SIGTERM, &sact, NULL);
6945 sigaction(SIGHUP, &sact, NULL);
6947 sact.sa_handler = sighdlr;
6948 sact.sa_flags = SA_NOCLDSTOP;
6949 sigaction(SIGCHLD, &sact, NULL);
6951 astate = XInternAtom(display, "WM_STATE", False);
6952 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6953 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6954 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6955 a_wmname = XInternAtom(display, "WM_NAME", False);
6956 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6957 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6958 a_string = XInternAtom(display, "STRING", False);
6959 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6961 /* look for local and global conf file */
6962 pwd = getpwuid(getuid());
6964 errx(1, "invalid user: %d", getuid());
6973 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6974 if (stat(conf, &sb) != -1) {
6975 if (S_ISREG(sb.st_mode))
6978 /* try global conf file */
6979 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6980 if (!stat(conf, &sb))
6981 if (S_ISREG(sb.st_mode))
6985 /* load conf (if any) and refresh font color in bar graphics contexts */
6986 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
6987 for (i = 0; i < ScreenCount(display); ++i)
6988 XSetForeground(display, screens[i].bar_gc,
6989 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6992 /* set some values to work around bad programs */
6994 /* grab existing windows (before we build the bars) */
6997 if (getenv("SWM_STARTED") == NULL)
6998 setenv("SWM_STARTED", "YES", 1);
7000 /* setup all bars */
7001 for (i = 0; i < ScreenCount(display); i++)
7002 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7003 if (winfocus == NULL)
7004 winfocus = TAILQ_FIRST(&r->ws->winlist);
7012 if (focus_mode == SWM_FOCUS_DEFAULT)
7013 drain_enter_notify();
7015 xfd = ConnectionNumber(display);
7017 while (XPending(display)) {
7018 XNextEvent(display, &e);
7021 if (e.type < LASTEvent) {
7022 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7023 "window: 0x%lx, type: %s (%d), %d remaining"
7024 "\n", YESNO(handler[e.type]),
7025 e.xany.window, geteventname(&e),
7026 e.type, QLength(display));
7028 if (handler[e.type])
7029 handler[e.type](&e);
7031 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7032 "0x%lx, type: %s (%d)\n", e.xany.window,
7033 xrandr_geteventname(&e), e.type);
7035 switch (e.type - xrandr_eventbase) {
7036 case RRScreenChangeNotify:
7045 /* if we are being restarted go focus on first window */
7047 rr = winfocus->ws->r;
7049 /* not a visible window */
7053 /* move pointer to first screen if multi screen */
7054 if (ScreenCount(display) > 1 || outputs > 1)
7055 XWarpPointer(display, None, rr->s[0].root,
7057 Y(rr) + (bar_enabled ? bar_height : 0));
7059 a.id = SWM_ARG_ID_FOCUSCUR;
7069 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7071 DNPRINTF(SWM_D_MISC, "select failed");
7072 if (restart_wm == 1)
7073 restart(NULL, NULL);
7074 if (search_resp == 1)
7087 for (i = 0; i < ScreenCount(display); ++i)
7088 if (screens[i].bar_gc != NULL)
7089 XFreeGC(display, screens[i].bar_gc);
7091 XFreeFontSet(display, bar_fs);
7092 XCloseDisplay(display);