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>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
94 #include <X11/Xproto.h>
95 #include <X11/Xutil.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/extensions/XTest.h>
105 #ifdef SPECTRWM_BUILDSTR
106 static const char *buildstr = SPECTRWM_BUILDSTR;
108 static const char *buildstr = SPECTRWM_VERSION;
112 # error XRandR versions less than 1.0 are not supported
117 #define SWM_XRR_HAS_CRTC
121 /*#define SWM_DEBUG*/
123 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
124 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
125 #define SWM_D_MISC 0x0001
126 #define SWM_D_EVENT 0x0002
127 #define SWM_D_WS 0x0004
128 #define SWM_D_FOCUS 0x0008
129 #define SWM_D_MOVE 0x0010
130 #define SWM_D_STACK 0x0020
131 #define SWM_D_MOUSE 0x0040
132 #define SWM_D_PROP 0x0080
133 #define SWM_D_CLASS 0x0100
134 #define SWM_D_KEY 0x0200
135 #define SWM_D_QUIRK 0x0400
136 #define SWM_D_SPAWN 0x0800
137 #define SWM_D_EVENTQ 0x1000
138 #define SWM_D_CONF 0x2000
139 #define SWM_D_BAR 0x4000
141 u_int32_t swm_debug = 0
159 #define DPRINTF(x...)
160 #define DNPRINTF(n,x...)
163 #define LENGTH(x) (sizeof x / sizeof x[0])
164 #define MODKEY Mod1Mask
165 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
166 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
167 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
168 #define SWM_PROPLEN (16)
169 #define SWM_FUNCNAME_LEN (32)
170 #define SWM_KEYS_LEN (255)
171 #define SWM_QUIRK_LEN (64)
172 #define X(r) (r)->g.x
173 #define Y(r) (r)->g.y
174 #define WIDTH(r) (r)->g.w
175 #define HEIGHT(r) (r)->g.h
176 #define SH_MIN(w) (w)->sh_mask & PMinSize
177 #define SH_MIN_W(w) (w)->sh.min_width
178 #define SH_MIN_H(w) (w)->sh.min_height
179 #define SH_MAX(w) (w)->sh_mask & PMaxSize
180 #define SH_MAX_W(w) (w)->sh.max_width
181 #define SH_MAX_H(w) (w)->sh.max_height
182 #define SH_INC(w) (w)->sh_mask & PResizeInc
183 #define SH_INC_W(w) (w)->sh.width_inc
184 #define SH_INC_H(w) (w)->sh.height_inc
185 #define SWM_MAX_FONT_STEPS (3)
186 #define WINID(w) ((w) ? (w)->id : 0)
187 #define YESNO(x) ((x) ? "yes" : "no")
189 #define SWM_FOCUS_DEFAULT (0)
190 #define SWM_FOCUS_SYNERGY (1)
191 #define SWM_FOCUS_FOLLOW (2)
193 #define SWM_CONF_DEFAULT (0)
194 #define SWM_CONF_KEYMAPPING (1)
197 #define SWM_LIB "/usr/local/lib/libswmhack.so"
210 volatile sig_atomic_t running = 1;
211 volatile sig_atomic_t restart_wm = 0;
213 int last_focus_event = FocusOut;
214 int (*xerrorxlib)(Display *, XErrorEvent *);
218 int xrandr_eventbase;
219 unsigned int numlockmask = 0;
223 int cycle_visible = 0;
225 int font_adjusted = 0;
226 unsigned int mod_key = MODKEY;
229 struct swm_region *search_r;
230 int select_list_pipe[2];
231 int select_resp_pipe[2];
233 volatile sig_atomic_t search_resp;
234 int search_resp_action;
236 struct search_window {
237 TAILQ_ENTRY(search_window) entry;
243 TAILQ_HEAD(search_winlist, search_window);
245 struct search_winlist search_wl;
250 SWM_SEARCH_UNICONIFY,
251 SWM_SEARCH_NAME_WORKSPACE,
252 SWM_SEARCH_SEARCH_WORKSPACE,
253 SWM_SEARCH_SEARCH_WINDOW
257 double dialog_ratio = 0.6;
259 #define SWM_BAR_MAX (256)
260 #define SWM_BAR_JUSTIFY_LEFT (0)
261 #define SWM_BAR_JUSTIFY_CENTER (1)
262 #define SWM_BAR_JUSTIFY_RIGHT (2)
263 #define SWM_BAR_OFFSET (4)
264 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
265 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
266 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
267 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
268 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
270 #ifdef X_HAVE_UTF8_STRING
271 #define DRAWSTRING(x...) Xutf8DrawString(x)
273 #define DRAWSTRING(x...) XmbDrawString(x)
276 char *bar_argv[] = { NULL, NULL };
278 unsigned char bar_ext[SWM_BAR_MAX];
279 char bar_vertext[SWM_BAR_MAX];
281 sig_atomic_t bar_alarm = 0;
284 int bar_border_width = 1;
285 int bar_at_bottom = 0;
287 int bar_extra_running = 0;
290 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
291 int stack_enabled = 1;
292 int clock_enabled = 1;
293 int urgent_enabled = 0;
294 char *clock_format = NULL;
295 int title_name_enabled = 0;
296 int title_class_enabled = 0;
297 int window_name_enabled = 0;
298 int focus_mode = SWM_FOCUS_DEFAULT;
299 int disable_border = 0;
300 int border_width = 1;
301 int verbose_layout = 0;
304 XFontSetExtents *bar_fs_extents;
306 char *spawn_term[] = { NULL, NULL }; /* XXX fully dynamic */
309 #define SWM_MENU_FN (2)
310 #define SWM_MENU_NB (4)
311 #define SWM_MENU_NF (6)
312 #define SWM_MENU_SB (8)
313 #define SWM_MENU_SF (10)
315 /* layout manager data */
316 struct swm_geometry {
326 /* virtual "screens" */
328 TAILQ_ENTRY(swm_region) entry;
329 struct swm_geometry g;
330 struct workspace *ws; /* current workspace on this region */
331 struct workspace *ws_prior; /* prior workspace on this region */
332 struct swm_screen *s; /* screen idx */
335 TAILQ_HEAD(swm_region_list, swm_region);
338 TAILQ_ENTRY(ws_win) entry;
341 struct ws_win *child_trans; /* transient child window */
342 struct swm_geometry g; /* current geometry */
343 struct swm_geometry g_float; /* geometry when floating */
344 struct swm_geometry rg_float; /* region geom when floating */
345 int g_floatvalid; /* g_float geometry validity */
346 int floatmaxed; /* whether maxed by max_stack */
350 unsigned int ewmh_flags;
351 int font_size_boundary[SWM_MAX_FONT_STEPS];
357 unsigned long quirks;
358 struct workspace *ws; /* always valid */
359 struct swm_screen *s; /* always valid, never changes */
360 XWindowAttributes wa;
366 TAILQ_HEAD(ws_win_list, ws_win);
370 TAILQ_ENTRY(pid_e) entry;
374 TAILQ_HEAD(pid_list, pid_e);
375 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
377 /* layout handlers */
379 void vertical_config(struct workspace *, int);
380 void vertical_stack(struct workspace *, struct swm_geometry *);
381 void horizontal_config(struct workspace *, int);
382 void horizontal_stack(struct workspace *, struct swm_geometry *);
383 void max_stack(struct workspace *, struct swm_geometry *);
384 void plain_stacker(struct workspace *);
385 void fancy_stacker(struct workspace *);
387 struct ws_win *find_window(Window);
389 void grabbuttons(struct ws_win *, int);
390 void new_region(struct swm_screen *, int, int, int, int);
391 void unmanage_window(struct ws_win *);
392 long getstate(Window);
394 int conf_load(char *, int);
397 void (*l_stack)(struct workspace *, struct swm_geometry *);
398 void (*l_config)(struct workspace *, int);
400 #define SWM_L_FOCUSPREV (1<<0)
401 #define SWM_L_MAPONFOCUS (1<<1)
402 void (*l_string)(struct workspace *);
404 /* stack, configure */
405 { vertical_stack, vertical_config, 0, plain_stacker },
406 { horizontal_stack, horizontal_config, 0, plain_stacker },
408 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
409 { NULL, NULL, 0, NULL },
412 /* position of max_stack mode in the layouts array, index into layouts! */
413 #define SWM_V_STACK (0)
414 #define SWM_H_STACK (1)
415 #define SWM_MAX_STACK (2)
417 #define SWM_H_SLICE (32)
418 #define SWM_V_SLICE (32)
420 /* define work spaces */
422 int idx; /* workspace index */
423 char *name; /* workspace name */
424 int always_raise; /* raise windows on focus */
425 struct layout *cur_layout; /* current layout handlers */
426 struct ws_win *focus; /* may be NULL */
427 struct ws_win *focus_prev; /* may be NULL */
428 struct swm_region *r; /* may be NULL */
429 struct swm_region *old_r; /* may be NULL */
430 struct ws_win_list winlist; /* list of windows in ws */
431 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
432 char stacker[10]; /* display stacker and layout */
436 int horizontal_msize;
438 int horizontal_stacks;
447 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
448 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
450 /* physical screen mapping */
451 #define SWM_WS_MAX (22) /* hard limit */
452 int workspace_limit = 10; /* soft limit */
455 int idx; /* screen index */
456 struct swm_region_list rl; /* list of regions on this screen */
457 struct swm_region_list orl; /* list of old regions */
459 struct workspace ws[SWM_WS_MAX];
465 } c[SWM_S_COLOR_MAX];
469 struct swm_screen *screens;
472 /* args to functions */
475 #define SWM_ARG_ID_FOCUSNEXT (0)
476 #define SWM_ARG_ID_FOCUSPREV (1)
477 #define SWM_ARG_ID_FOCUSMAIN (2)
478 #define SWM_ARG_ID_FOCUSCUR (4)
479 #define SWM_ARG_ID_SWAPNEXT (10)
480 #define SWM_ARG_ID_SWAPPREV (11)
481 #define SWM_ARG_ID_SWAPMAIN (12)
482 #define SWM_ARG_ID_MOVELAST (13)
483 #define SWM_ARG_ID_MASTERSHRINK (20)
484 #define SWM_ARG_ID_MASTERGROW (21)
485 #define SWM_ARG_ID_MASTERADD (22)
486 #define SWM_ARG_ID_MASTERDEL (23)
487 #define SWM_ARG_ID_FLIPLAYOUT (24)
488 #define SWM_ARG_ID_STACKRESET (30)
489 #define SWM_ARG_ID_STACKINIT (31)
490 #define SWM_ARG_ID_CYCLEWS_UP (40)
491 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
492 #define SWM_ARG_ID_CYCLESC_UP (42)
493 #define SWM_ARG_ID_CYCLESC_DOWN (43)
494 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
495 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
496 #define SWM_ARG_ID_STACKINC (50)
497 #define SWM_ARG_ID_STACKDEC (51)
498 #define SWM_ARG_ID_SS_ALL (60)
499 #define SWM_ARG_ID_SS_WINDOW (61)
500 #define SWM_ARG_ID_DONTCENTER (70)
501 #define SWM_ARG_ID_CENTER (71)
502 #define SWM_ARG_ID_KILLWINDOW (80)
503 #define SWM_ARG_ID_DELETEWINDOW (81)
504 #define SWM_ARG_ID_WIDTHGROW (90)
505 #define SWM_ARG_ID_WIDTHSHRINK (91)
506 #define SWM_ARG_ID_HEIGHTGROW (92)
507 #define SWM_ARG_ID_HEIGHTSHRINK (93)
508 #define SWM_ARG_ID_MOVEUP (100)
509 #define SWM_ARG_ID_MOVEDOWN (101)
510 #define SWM_ARG_ID_MOVELEFT (102)
511 #define SWM_ARG_ID_MOVERIGHT (103)
515 void focus(struct swm_region *, union arg *);
516 void focus_magic(struct ws_win *);
520 TAILQ_ENTRY(quirk) entry;
524 #define SWM_Q_FLOAT (1<<0) /* float this window */
525 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
526 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
527 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
528 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
529 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
531 TAILQ_HEAD(quirk_list, quirk);
532 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
535 * Supported EWMH hints should be added to
536 * both the enum and the ewmh array
538 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
539 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
540 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
541 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
542 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
543 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
544 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
545 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
546 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
547 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
553 } ewmh[SWM_EWMH_HINT_MAX] = {
554 /* must be in same order as in the enum */
555 {"_NET_ACTIVE_WINDOW", None},
556 {"_NET_MOVERESIZE_WINDOW", None},
557 {"_NET_CLOSE_WINDOW", None},
558 {"_NET_WM_WINDOW_TYPE", None},
559 {"_NET_WM_WINDOW_TYPE_DOCK", None},
560 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
561 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
562 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
563 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
564 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
565 {"_NET_WM_STATE", None},
566 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
567 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
568 {"_NET_WM_STATE_SKIP_TASKBAR", None},
569 {"_NET_WM_STATE_SKIP_PAGER", None},
570 {"_NET_WM_STATE_HIDDEN", None},
571 {"_NET_WM_STATE_ABOVE", None},
572 {"_SWM_WM_STATE_MANUAL", None},
573 {"_NET_WM_STATE_FULLSCREEN", None},
574 {"_NET_WM_ALLOWED_ACTIONS", None},
575 {"_NET_WM_ACTION_MOVE", None},
576 {"_NET_WM_ACTION_RESIZE", None},
577 {"_NET_WM_ACTION_FULLSCREEN", None},
578 {"_NET_WM_ACTION_CLOSE", None},
581 void store_float_geom(struct ws_win *, struct swm_region *);
582 int floating_toggle_win(struct ws_win *);
583 void spawn_select(struct swm_region *, union arg *, char *, int *);
584 unsigned char *get_win_name(Window);
587 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
588 unsigned long *nbytes, unsigned char **data)
591 unsigned long *nbytes_ret, *nitems_ret;
592 unsigned long nbytes_tmp, nitems_tmp;
595 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
596 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
598 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
599 &real, &format, nitems_ret, nbytes_ret, data);
601 if (status != Success)
610 update_iconic(struct ws_win *win, int newv)
617 iprop = XInternAtom(display, "_SWM_ICONIC", False);
621 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
622 PropModeReplace, (unsigned char *)&v, 1);
624 XDeleteProperty(display, win->id, iprop);
628 get_iconic(struct ws_win *win)
633 unsigned long nitems, extra;
634 unsigned char *prop = NULL;
636 iprop = XInternAtom(display, "_SWM_ICONIC", False);
639 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
640 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
641 if (status != Success)
643 if (rettype != XA_INTEGER || retfmt != 32)
647 v = *((int32_t *)prop);
661 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
663 for (i = 0; i < LENGTH(ewmh); i++)
664 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
666 for (i = 0; i < ScreenCount(display); i++) {
667 /* Support check window will be created by workaround(). */
669 /* Report supported atoms */
670 XDeleteProperty(display, screens[i].root, sup_list);
671 for (j = 0; j < LENGTH(ewmh); j++)
672 XChangeProperty(display, screens[i].root,
673 sup_list, XA_ATOM, 32,
674 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
682 unsigned char *data = NULL;
684 Atom sup_check, sup_list;
687 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
688 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
690 for (i = 0; i < ScreenCount(display); i++) {
691 /* Get the support check window and destroy it */
692 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
697 XDestroyWindow(display, id);
698 XDeleteProperty(display, screens[i].root, sup_check);
699 XDeleteProperty(display, screens[i].root, sup_list);
707 ewmh_autoquirk(struct ws_win *win)
710 unsigned long *data = NULL, n;
713 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
714 XA_ATOM, &n, NULL, (void *)&data);
721 for (i = 0; i < n; i++) {
723 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
725 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
726 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
727 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
729 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
732 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
733 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
735 win->quirks = SWM_Q_FLOAT;
743 #define SWM_EWMH_ACTION_COUNT_MAX (6)
744 #define EWMH_F_FULLSCREEN (1<<0)
745 #define EWMH_F_ABOVE (1<<1)
746 #define EWMH_F_HIDDEN (1<<2)
747 #define EWMH_F_SKIP_PAGER (1<<3)
748 #define EWMH_F_SKIP_TASKBAR (1<<4)
749 #define SWM_F_MANUAL (1<<5)
752 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
754 struct swm_geometry rg;
762 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
763 "fullscreen %s\n", win->id, YESNO(fs));
768 store_float_geom(win, win->ws->r);
772 if (win->g_floatvalid) {
773 /* refloat at last floating relative position */
774 X(win) = win->g_float.x - win->rg_float.x + rg.x;
775 Y(win) = win->g_float.y - win->rg_float.y + rg.y;
776 WIDTH(win) = win->g_float.w;
777 HEIGHT(win) = win->g_float.h;
785 ewmh_update_actions(struct ws_win *win)
787 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
793 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
796 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
797 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
800 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
801 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
804 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
805 #define _NET_WM_STATE_ADD 1 /* add/set property */
806 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
809 ewmh_update_win_state(struct ws_win *win, long state, long action)
811 unsigned int mask = 0;
812 unsigned int changed = 0;
813 unsigned int orig_flags;
818 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
819 mask = EWMH_F_FULLSCREEN;
820 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
822 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
824 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
825 mask = EWMH_F_SKIP_PAGER;
826 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
827 mask = EWMH_F_SKIP_TASKBAR;
830 orig_flags = win->ewmh_flags;
833 case _NET_WM_STATE_REMOVE:
834 win->ewmh_flags &= ~mask;
836 case _NET_WM_STATE_ADD:
837 win->ewmh_flags |= mask;
839 case _NET_WM_STATE_TOGGLE:
840 win->ewmh_flags ^= mask;
844 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
846 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
848 if (!floating_toggle_win(win))
849 win->ewmh_flags = orig_flags; /* revert */
850 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
852 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
853 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
855 if (!ewmh_set_win_fullscreen(win,
856 win->ewmh_flags & EWMH_F_FULLSCREEN))
857 win->ewmh_flags = orig_flags; /* revert */
859 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
861 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
862 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
863 XA_ATOM, 32, PropModeAppend,
864 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
865 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
866 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
867 XA_ATOM, 32, PropModeAppend,
868 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
869 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
870 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
871 XA_ATOM, 32, PropModeAppend,
872 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
873 if (win->ewmh_flags & EWMH_F_ABOVE)
874 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
875 XA_ATOM, 32, PropModeAppend,
876 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
877 if (win->ewmh_flags & SWM_F_MANUAL)
878 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
879 XA_ATOM, 32, PropModeAppend,
880 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
884 ewmh_get_win_state(struct ws_win *win)
895 win->ewmh_flags |= EWMH_F_ABOVE;
897 win->ewmh_flags |= SWM_F_MANUAL;
899 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
900 (~0L), XA_ATOM, &n, NULL, (void *)&states);
905 for (i = 0; i < n; i++)
906 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
914 geteventname(XEvent *e)
926 name = "ButtonPress";
929 name = "ButtonRelease";
932 name = "MotionNotify";
935 name = "EnterNotify";
938 name = "LeaveNotify";
947 name = "KeymapNotify";
953 name = "GraphicsExpose";
958 case VisibilityNotify:
959 name = "VisibilityNotify";
962 name = "CreateNotify";
965 name = "DestroyNotify";
968 name = "UnmapNotify";
977 name = "ReparentNotify";
979 case ConfigureNotify:
980 name = "ConfigureNotify";
982 case ConfigureRequest:
983 name = "ConfigureRequest";
986 name = "GravityNotify";
989 name = "ResizeRequest";
991 case CirculateNotify:
992 name = "CirculateNotify";
994 case CirculateRequest:
995 name = "CirculateRequest";
998 name = "PropertyNotify";
1000 case SelectionClear:
1001 name = "SelectionClear";
1003 case SelectionRequest:
1004 name = "SelectionRequest";
1006 case SelectionNotify:
1007 name = "SelectionNotify";
1009 case ColormapNotify:
1010 name = "ColormapNotify";
1013 name = "ClientMessage";
1016 name = "MappingNotify";
1026 xrandr_geteventname(XEvent *e)
1030 switch(e->type - xrandr_eventbase) {
1031 case RRScreenChangeNotify:
1032 name = "RRScreenChangeNotify";
1042 dumpwins(struct swm_region *r, union arg *args)
1046 XWindowAttributes wa;
1048 if (r->ws == NULL) {
1049 warnx("dumpwins: invalid workspace");
1053 warnx("=== managed window list ws %02d ===", r->ws->idx);
1055 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1056 state = getstate(win->id);
1057 if (!XGetWindowAttributes(display, win->id, &wa))
1058 warnx("window: 0x%lx, failed XGetWindowAttributes",
1060 warnx("window: 0x%lx, map_state: %d, state: %d, "
1061 "transient: 0x%lx", win->id, wa.map_state, state,
1065 warnx("===== unmanaged window list =====");
1066 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1067 state = getstate(win->id);
1068 if (!XGetWindowAttributes(display, win->id, &wa))
1069 warnx("window: 0x%lx, failed XGetWindowAttributes",
1071 warnx("window: 0x%lx, map_state: %d, state: %d, "
1072 "transient: 0x%lx", win->id, wa.map_state, state,
1076 warnx("=================================");
1080 dumpwins(struct swm_region *r, union arg *args)
1083 #endif /* SWM_DEBUG */
1085 void expose(XEvent *);
1086 void keypress(XEvent *);
1087 void buttonpress(XEvent *);
1088 void configurerequest(XEvent *);
1089 void configurenotify(XEvent *);
1090 void destroynotify(XEvent *);
1091 void enternotify(XEvent *);
1092 void focusevent(XEvent *);
1093 void mapnotify(XEvent *);
1094 void mappingnotify(XEvent *);
1095 void maprequest(XEvent *);
1096 void propertynotify(XEvent *);
1097 void unmapnotify(XEvent *);
1098 void visibilitynotify(XEvent *);
1099 void clientmessage(XEvent *);
1101 void (*handler[LASTEvent])(XEvent *) = {
1103 [KeyPress] = keypress,
1104 [ButtonPress] = buttonpress,
1105 [ConfigureRequest] = configurerequest,
1106 [ConfigureNotify] = configurenotify,
1107 [DestroyNotify] = destroynotify,
1108 [EnterNotify] = enternotify,
1109 [FocusIn] = focusevent,
1110 [FocusOut] = focusevent,
1111 [MapNotify] = mapnotify,
1112 [MappingNotify] = mappingnotify,
1113 [MapRequest] = maprequest,
1114 [PropertyNotify] = propertynotify,
1115 [UnmapNotify] = unmapnotify,
1116 [VisibilityNotify] = visibilitynotify,
1117 [ClientMessage] = clientmessage,
1123 int saved_errno, status;
1126 saved_errno = errno;
1130 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1135 if (errno != ECHILD)
1136 warn("sighdlr: waitpid");
1137 #endif /* SWM_DEBUG */
1140 if (pid == searchpid)
1144 if (WIFEXITED(status)) {
1145 if (WEXITSTATUS(status) != 0)
1146 warnx("sighdlr: child exit status: %d",
1147 WEXITSTATUS(status));
1149 warnx("sighdlr: child is terminated "
1151 #endif /* SWM_DEBUG */
1165 errno = saved_errno;
1171 struct pid_e *p = NULL;
1173 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1178 TAILQ_FOREACH(p, &pidlist, entry) {
1187 name_to_color(char *colorname)
1191 XColor screen_def, exact_def;
1192 unsigned long result = 0;
1193 char cname[32] = "#";
1195 cmap = DefaultColormap(display, screens[0].idx);
1196 status = XAllocNamedColor(display, cmap, colorname,
1197 &screen_def, &exact_def);
1199 strlcat(cname, colorname + 2, sizeof cname - 1);
1200 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1204 result = screen_def.pixel;
1206 warnx("color '%s' not found", colorname);
1212 setscreencolor(char *val, int i, int c)
1214 if (i > 0 && i <= ScreenCount(display)) {
1215 screens[i - 1].c[c].color = name_to_color(val);
1216 free(screens[i - 1].c[c].name);
1217 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1219 } else if (i == -1) {
1220 for (i = 0; i < ScreenCount(display); i++) {
1221 screens[i].c[c].color = name_to_color(val);
1222 free(screens[i].c[c].name);
1223 if ((screens[i].c[c].name = strdup(val)) == NULL)
1227 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1228 i, ScreenCount(display));
1232 fancy_stacker(struct workspace *ws)
1234 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1235 if (ws->cur_layout->l_stack == vertical_stack)
1236 snprintf(ws->stacker, sizeof ws->stacker,
1237 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1238 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1239 if (ws->cur_layout->l_stack == horizontal_stack)
1240 snprintf(ws->stacker, sizeof ws->stacker,
1241 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1242 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1246 plain_stacker(struct workspace *ws)
1248 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1249 if (ws->cur_layout->l_stack == vertical_stack)
1250 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1251 sizeof ws->stacker);
1252 if (ws->cur_layout->l_stack == horizontal_stack)
1253 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1254 sizeof ws->stacker);
1258 custom_region(char *val)
1260 unsigned int sidx, x, y, w, h;
1262 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1263 errx(1, "invalid custom region, "
1264 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1265 if (sidx < 1 || sidx > ScreenCount(display))
1266 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1267 sidx, ScreenCount(display));
1271 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1273 if (x > DisplayWidth(display, sidx) ||
1274 y > DisplayHeight(display, sidx) ||
1275 w + x > DisplayWidth(display, sidx) ||
1276 h + y > DisplayHeight(display, sidx)) {
1277 warnx("ignoring region %ux%u+%u+%u - not within screen "
1278 "boundaries (%ux%u)", w, h, x, y,
1279 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1283 new_region(&screens[sidx], x, y, w, h);
1287 socket_setnonblock(int fd)
1291 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1292 err(1, "fcntl F_GETFL");
1293 flags |= O_NONBLOCK;
1294 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1295 err(1, "fcntl F_SETFL");
1299 bar_print(struct swm_region *r, char *s)
1303 XRectangle ibox, lbox;
1305 XClearWindow(display, r->bar_window);
1308 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1310 switch (bar_justify) {
1311 case SWM_BAR_JUSTIFY_LEFT:
1314 case SWM_BAR_JUSTIFY_CENTER:
1315 x = (WIDTH(r) - lbox.width) / 2;
1317 case SWM_BAR_JUSTIFY_RIGHT:
1318 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1322 if (x < SWM_BAR_OFFSET)
1325 DRAWSTRING(display, r->bar_window, bar_fs, r->s->bar_gc,
1326 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1331 bar_extra_stop(void)
1335 bzero(bar_pipe, sizeof bar_pipe);
1338 kill(bar_pid, SIGTERM);
1341 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1346 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1348 int do_class, do_name;
1350 XClassHint *xch = NULL;
1352 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1353 cur_focus != NULL) {
1354 if ((xch = XAllocClassHint()) == NULL)
1356 status = XGetClassHint(display, cur_focus->id, xch);
1357 if (status == BadWindow || status == BadAlloc)
1359 do_class = (title_class_enabled && xch->res_class != NULL);
1360 do_name = (title_name_enabled && xch->res_name != NULL);
1362 strlcat(s, xch->res_class, sz);
1363 if (do_class && do_name)
1364 strlcat(s, ":", sz);
1366 strlcat(s, xch->res_name, sz);
1367 strlcat(s, " ", sz);
1371 XFree(xch->res_name);
1372 XFree(xch->res_class);
1378 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1380 unsigned char *title;
1382 if (window_name_enabled && cur_focus != NULL) {
1383 title = get_win_name(cur_focus->id);
1384 if (title != NULL) {
1385 DNPRINTF(SWM_D_BAR, "bar_window_name: title: %s\n",
1388 if (cur_focus->floating)
1389 strlcat(s, "(f) ", sz);
1390 strlcat(s, (char *)title, sz);
1391 strlcat(s, " ", sz);
1397 int urgent[SWM_WS_MAX];
1399 bar_urgent(char *s, ssize_t sz)
1401 XWMHints *wmh = NULL;
1406 if (urgent_enabled == 0)
1409 for (i = 0; i < workspace_limit; i++)
1412 for (i = 0; i < ScreenCount(display); i++)
1413 for (j = 0; j < workspace_limit; j++)
1414 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1415 wmh = XGetWMHints(display, win->id);
1419 if (wmh->flags & XUrgencyHint)
1424 strlcat(s, "* ", sz);
1425 for (i = 0; i < workspace_limit; i++) {
1427 snprintf(b, sizeof b, "%d ", i + 1);
1429 snprintf(b, sizeof b, "- ");
1432 strlcat(s, "* ", sz);
1440 struct swm_region *r;
1443 char ws[SWM_BAR_MAX];
1444 char s[SWM_BAR_MAX];
1445 unsigned char cn[SWM_BAR_MAX];
1446 char loc[SWM_BAR_MAX];
1447 char *b, *stack = "";
1449 if (bar_enabled == 0)
1451 if (bar_extra && bar_extra_running) {
1452 /* ignore short reads; it'll correct itself */
1453 while ((b = fgetln(stdin, &len)) != NULL)
1454 if (b && b[len - 1] == '\n') {
1456 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1458 if (b == NULL && errno != EAGAIN) {
1459 warn("bar_update: bar_extra failed");
1463 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1465 if (clock_enabled == 0)
1466 strlcpy(s, "", sizeof s);
1469 localtime_r(&tmt, &tm);
1470 len = strftime(s, sizeof s, clock_format, &tm);
1472 strlcat(s, " ", sizeof s);
1475 for (i = 0; i < ScreenCount(display); i++) {
1477 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1478 strlcpy((char *)cn, "", sizeof cn);
1479 strlcpy(ws, "", sizeof ws);
1481 bar_urgent((char *)cn, sizeof cn);
1482 bar_class_name((char *)cn, sizeof cn,
1484 bar_window_name((char *)cn, sizeof cn,
1487 snprintf(ws, sizeof ws, "<%s>",
1490 stack = r->ws->stacker;
1492 snprintf(loc, sizeof loc,
1493 "%d:%d %s %s %s%s %s %s",
1494 x++, r->ws->idx + 1, stack, ws, s, cn,
1495 bar_ext, bar_vertext);
1504 bar_check_opts(void)
1506 if (title_class_enabled || title_name_enabled || window_name_enabled)
1517 bar_toggle(struct swm_region *r, union arg *args)
1519 struct swm_region *tmpr;
1520 int i, sc = ScreenCount(display);
1522 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1525 for (i = 0; i < sc; i++)
1526 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1527 XUnmapWindow(display, tmpr->bar_window);
1529 for (i = 0; i < sc; i++)
1530 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1531 XMapRaised(display, tmpr->bar_window);
1533 bar_enabled = !bar_enabled;
1536 /* must be after stack */
1543 XSetWindowAttributes wa;
1544 struct swm_region *r;
1547 /* do this here because the conf file is in memory */
1548 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1549 /* launch external status app */
1550 bar_extra_running = 1;
1551 if (pipe(bar_pipe) == -1)
1552 err(1, "pipe error");
1553 socket_setnonblock(bar_pipe[0]);
1554 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1555 if (dup2(bar_pipe[0], 0) == -1)
1557 if (dup2(bar_pipe[1], 1) == -1)
1559 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1560 err(1, "could not disable SIGPIPE");
1561 switch (bar_pid = fork()) {
1563 err(1, "cannot fork");
1567 execvp(bar_argv[0], bar_argv);
1568 err(1, "%s external app failed", bar_argv[0]);
1570 default: /* parent */
1576 bzero(&wa, sizeof wa);
1577 for (i = 0; i < ScreenCount(display); i++)
1578 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1580 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1581 wa.background_pixel =
1582 screens[i].c[SWM_S_COLOR_BAR].color;
1583 XChangeWindowAttributes(display, r->bar_window,
1584 CWBackPixel | CWBorderPixel, &wa);
1590 bar_setup(struct swm_region *r)
1592 char *default_string;
1593 char **missing_charsets;
1594 int num_missing_charsets = 0;
1598 XFreeFontSet(display, bar_fs);
1603 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1605 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1606 &num_missing_charsets, &default_string);
1608 if (num_missing_charsets > 0) {
1609 warnx("Unable to load charset(s):");
1611 for (i = 0; i < num_missing_charsets; ++i)
1612 warnx("%s", missing_charsets[i]);
1614 XFreeStringList(missing_charsets);
1616 if (strcmp(default_string, ""))
1617 warnx("Glyphs from those sets will be replaced "
1618 "by '%s'.", default_string);
1620 warnx("Glyphs from those sets won't be drawn.");
1624 errx(1, "Error creating font set structure.");
1626 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1628 bar_height = bar_fs_extents->max_logical_extent.height +
1629 2 * bar_border_width;
1635 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1637 r->bar_window = XCreateSimpleWindow(display,
1638 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1639 bar_height - 2 * bar_border_width,
1640 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1641 r->s->c[SWM_S_COLOR_BAR].color);
1642 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1644 XMapRaised(display, r->bar_window);
1645 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1647 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1648 err(1, "could not install bar_signal");
1653 drain_enter_notify(void)
1658 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1661 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1665 set_win_state(struct ws_win *win, long state)
1667 long data[] = {state, None};
1669 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1674 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1675 (unsigned char *)data, 2);
1682 unsigned char *p = NULL;
1685 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1688 result = *((long *)p);
1694 version(struct swm_region *r, union arg *args)
1696 bar_version = !bar_version;
1698 snprintf(bar_vertext, sizeof bar_vertext,
1699 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1701 strlcpy(bar_vertext, "", sizeof bar_vertext);
1706 client_msg(struct ws_win *win, Atom a)
1708 XClientMessageEvent cm;
1713 bzero(&cm, sizeof cm);
1714 cm.type = ClientMessage;
1715 cm.window = win->id;
1716 cm.message_type = aprot;
1719 cm.data.l[1] = CurrentTime;
1720 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1723 /* synthetic response to a ConfigureRequest when not making a change */
1725 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1732 /* send notification of unchanged state. */
1733 ce.type = ConfigureNotify;
1736 ce.width = WIDTH(win);
1737 ce.height = HEIGHT(win);
1738 ce.override_redirect = False;
1742 ce.display = display;
1744 ce.window = win->id;
1745 ce.border_width = border_width;
1749 ce.display = ev->display;
1750 ce.event = ev->window;
1751 ce.window = ev->window;
1753 /* make response appear more WM_SIZE_HINTS-compliant */
1755 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1756 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1757 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1758 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1759 SH_INC_W(win), SH_INC_H(win));
1763 /* the hint may be set... to 0! */
1764 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1765 ce.width = SH_MIN_W(win);
1766 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1767 ce.height = SH_MIN_H(win);
1772 /* may also be advertized as 0 */
1773 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1774 ce.width = SH_MAX_W(win);
1775 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1776 ce.height = SH_MAX_H(win);
1779 /* resize increment. */
1781 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1782 ce.width -= (ce.width - SH_MIN_W(win)) %
1784 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1785 ce.height -= (ce.height - SH_MIN_H(win)) %
1789 /* adjust x and y for requested border_width. */
1790 ce.x += border_width - ev->border_width;
1791 ce.y += border_width - ev->border_width;
1792 ce.border_width = ev->border_width;
1793 ce.above = ev->above;
1796 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1797 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1798 ce.y, ce.width, ce.height, ce.border_width);
1800 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1804 count_win(struct workspace *ws, int count_transient)
1809 TAILQ_FOREACH(win, &ws->winlist, entry) {
1810 if (count_transient == 0 && win->floating)
1812 if (count_transient == 0 && win->transient)
1818 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1824 quit(struct swm_region *r, union arg *args)
1826 DNPRINTF(SWM_D_MISC, "quit\n");
1831 unmap_window(struct ws_win *win)
1836 /* don't unmap again */
1837 if (getstate(win->id) == IconicState)
1840 set_win_state(win, IconicState);
1842 XUnmapWindow(display, win->id);
1843 XSetWindowBorder(display, win->id,
1844 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1853 for (i = 0; i < ScreenCount(display); i++)
1854 for (j = 0; j < workspace_limit; j++)
1855 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1860 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1867 event.display = display; /* Ignored, but what the hell */
1868 event.window = win->id;
1869 event.root = win->s->root;
1870 event.subwindow = None;
1871 event.time = CurrentTime;
1876 event.same_screen = True;
1877 event.keycode = XKeysymToKeycode(display, keysym);
1878 event.state = modifiers;
1880 event.type = KeyPress;
1881 XSendEvent(event.display, event.window, True,
1882 KeyPressMask, (XEvent *)&event);
1884 event.type = KeyRelease;
1885 XSendEvent(event.display, event.window, True,
1886 KeyPressMask, (XEvent *)&event);
1891 restart(struct swm_region *r, union arg *args)
1893 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1895 /* disable alarm because the following code may not be interrupted */
1897 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1898 err(1, "can't disable alarm");
1903 XCloseDisplay(display);
1904 execvp(start_argv[0], start_argv);
1905 warn("execvp failed");
1910 root_to_region(Window root)
1912 struct swm_region *r = NULL;
1914 int i, x, y, wx, wy;
1917 for (i = 0; i < ScreenCount(display); i++)
1918 if (screens[i].root == root)
1921 if (XQueryPointer(display, screens[i].root,
1922 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1923 /* choose a region based on pointer location */
1924 TAILQ_FOREACH(r, &screens[i].rl, entry)
1925 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1926 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1931 r = TAILQ_FIRST(&screens[i].rl);
1937 find_unmanaged_window(Window id)
1942 for (i = 0; i < ScreenCount(display); i++)
1943 for (j = 0; j < workspace_limit; j++)
1944 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1952 find_window(Window id)
1955 Window wrr, wpr, *wcr = NULL;
1959 for (i = 0; i < ScreenCount(display); i++)
1960 for (j = 0; j < workspace_limit; j++)
1961 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1965 /* if we were looking for the parent return that window instead */
1966 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1971 /* ignore not found and root */
1972 if (wpr == 0 || wrr == wpr)
1975 /* look for parent */
1976 for (i = 0; i < ScreenCount(display); i++)
1977 for (j = 0; j < workspace_limit; j++)
1978 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1986 spawn(int ws_idx, union arg *args, int close_fd)
1991 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1994 close(ConnectionNumber(display));
1996 setenv("LD_PRELOAD", SWM_LIB, 1);
1998 if (asprintf(&ret, "%d", ws_idx) == -1) {
1999 warn("spawn: asprintf SWM_WS");
2002 setenv("_SWM_WS", ret, 1);
2006 if (asprintf(&ret, "%d", getpid()) == -1) {
2007 warn("spawn: asprintf _SWM_PID");
2010 setenv("_SWM_PID", ret, 1);
2014 if (setsid() == -1) {
2015 warn("spawn: setsid");
2021 * close stdin and stdout to prevent interaction between apps
2022 * and the baraction script
2023 * leave stderr open to record errors
2025 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2026 warn("spawn: open");
2029 dup2(fd, STDIN_FILENO);
2030 dup2(fd, STDOUT_FILENO);
2035 execvp(args->argv[0], args->argv);
2037 warn("spawn: execvp");
2042 spawnterm(struct swm_region *r, union arg *args)
2044 DNPRINTF(SWM_D_MISC, "spawnterm\n");
2048 setenv("_SWM_XTERM_FONTADJ", "", 1);
2049 spawn(r->ws->idx, args, 1);
2054 kill_refs(struct ws_win *win)
2057 struct swm_region *r;
2058 struct workspace *ws;
2063 for (i = 0; i < ScreenCount(display); i++)
2064 TAILQ_FOREACH(r, &screens[i].rl, entry)
2065 for (x = 0; x < workspace_limit; x++) {
2067 if (win == ws->focus)
2069 if (win == ws->focus_prev)
2070 ws->focus_prev = NULL;
2075 validate_win(struct ws_win *testwin)
2078 struct workspace *ws;
2079 struct swm_region *r;
2082 if (testwin == NULL)
2085 for (i = 0; i < ScreenCount(display); i++)
2086 TAILQ_FOREACH(r, &screens[i].rl, entry)
2087 for (x = 0; x < workspace_limit; x++) {
2089 TAILQ_FOREACH(win, &ws->winlist, entry)
2097 validate_ws(struct workspace *testws)
2099 struct swm_region *r;
2100 struct workspace *ws;
2103 /* validate all ws */
2104 for (i = 0; i < ScreenCount(display); i++)
2105 TAILQ_FOREACH(r, &screens[i].rl, entry)
2106 for (x = 0; x < workspace_limit; x++) {
2115 unfocus_win(struct ws_win *win)
2120 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2124 if (win->ws == NULL)
2127 if (validate_ws(win->ws))
2128 return; /* XXX this gets hit with thunderbird, needs fixing */
2130 if (win->ws->r == NULL)
2133 if (validate_win(win)) {
2138 if (win->ws->focus == win) {
2139 win->ws->focus = NULL;
2140 win->ws->focus_prev = win;
2143 if (validate_win(win->ws->focus)) {
2144 kill_refs(win->ws->focus);
2145 win->ws->focus = NULL;
2147 if (validate_win(win->ws->focus_prev)) {
2148 kill_refs(win->ws->focus_prev);
2149 win->ws->focus_prev = NULL;
2152 /* drain all previous unfocus events */
2153 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2156 grabbuttons(win, 0);
2157 XSetWindowBorder(display, win->id,
2158 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2160 XChangeProperty(display, win->s->root,
2161 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2162 PropModeReplace, (unsigned char *)&none,1);
2171 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2173 for (i = 0; i < ScreenCount(display); i++)
2174 for (j = 0; j < workspace_limit; j++)
2175 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2180 focus_win(struct ws_win *win)
2185 struct ws_win *cfw = NULL;
2188 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2192 if (win->ws == NULL)
2195 if (validate_ws(win->ws))
2196 return; /* XXX this gets hit with thunderbird, needs fixing */
2198 if (validate_win(win)) {
2203 if (validate_win(win)) {
2208 XGetInputFocus(display, &cur_focus, &rr);
2209 if ((cfw = find_window(cur_focus)) != NULL)
2212 /* use larger hammer since the window was killed somehow */
2213 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2214 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2215 XSetWindowBorder(display, cfw->id,
2216 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2219 win->ws->focus = win;
2221 if (win->ws->r != NULL) {
2222 /* drain all previous focus events */
2223 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2227 XSetInputFocus(display, win->id,
2228 RevertToParent, CurrentTime);
2229 grabbuttons(win, 1);
2230 XSetWindowBorder(display, win->id,
2231 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2232 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2233 win->ws->always_raise)
2234 XMapRaised(display, win->id);
2236 XChangeProperty(display, win->s->root,
2237 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2238 PropModeReplace, (unsigned char *)&win->id,1);
2245 switchws(struct swm_region *r, union arg *args)
2247 int wsid = args->id, unmap_old = 0;
2248 struct swm_region *this_r, *other_r;
2250 struct workspace *new_ws, *old_ws;
2256 if (wsid >= workspace_limit)
2260 old_ws = this_r->ws;
2261 new_ws = &this_r->s->ws[wsid];
2263 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2264 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2266 if (new_ws == NULL || old_ws == NULL)
2268 if (new_ws == old_ws)
2271 other_r = new_ws->r;
2272 if (other_r == NULL) {
2273 /* the other workspace is hidden, hide this one */
2277 /* the other ws is visible in another region, exchange them */
2278 other_r->ws_prior = new_ws;
2279 other_r->ws = old_ws;
2280 old_ws->r = other_r;
2282 this_r->ws_prior = old_ws;
2283 this_r->ws = new_ws;
2286 /* this is needed so that we can click on a window after a restart */
2290 a.id = SWM_ARG_ID_FOCUSCUR;
2291 focus(new_ws->r, &a);
2295 /* unmap old windows */
2297 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2300 if (focus_mode == SWM_FOCUS_DEFAULT)
2301 drain_enter_notify();
2305 cyclews(struct swm_region *r, union arg *args)
2308 struct swm_screen *s = r->s;
2311 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2312 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2317 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2320 case SWM_ARG_ID_CYCLEWS_UP:
2321 if (a.id < workspace_limit - 1)
2326 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2329 case SWM_ARG_ID_CYCLEWS_DOWN:
2333 a.id = workspace_limit - 1;
2340 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2342 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2346 } while (a.id != r->ws->idx);
2350 priorws(struct swm_region *r, union arg *args)
2354 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2355 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2357 if (r->ws_prior == NULL)
2360 a.id = r->ws_prior->idx;
2365 cyclescr(struct swm_region *r, union arg *args)
2367 struct swm_region *rr = NULL;
2371 /* do nothing if we don't have more than one screen */
2372 if (!(ScreenCount(display) > 1 || outputs > 1))
2377 case SWM_ARG_ID_CYCLESC_UP:
2378 rr = TAILQ_NEXT(r, entry);
2380 rr = TAILQ_FIRST(&screens[i].rl);
2382 case SWM_ARG_ID_CYCLESC_DOWN:
2383 rr = TAILQ_PREV(r, swm_region_list, entry);
2385 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2393 /* move mouse to region */
2395 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2396 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2398 a.id = SWM_ARG_ID_FOCUSCUR;
2401 if (rr->ws->focus) {
2402 /* move to focus window */
2403 x = X(rr->ws->focus) + 1;
2404 y = Y(rr->ws->focus) + 1;
2405 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2410 sort_windows(struct ws_win_list *wl)
2412 struct ws_win *win, *parent, *nxt;
2417 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2418 nxt = TAILQ_NEXT(win, entry);
2419 if (win->transient) {
2420 parent = find_window(win->transient);
2421 if (parent == NULL) {
2422 warnx("not possible bug");
2425 TAILQ_REMOVE(wl, win, entry);
2426 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2433 swapwin(struct swm_region *r, union arg *args)
2435 struct ws_win *target, *source;
2436 struct ws_win *cur_focus;
2437 struct ws_win_list *wl;
2440 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2441 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2443 cur_focus = r->ws->focus;
2444 if (cur_focus == NULL)
2448 wl = &source->ws->winlist;
2451 case SWM_ARG_ID_SWAPPREV:
2452 if (source->transient)
2453 source = find_window(source->transient);
2454 target = TAILQ_PREV(source, ws_win_list, entry);
2455 if (target && target->transient)
2456 target = find_window(target->transient);
2457 TAILQ_REMOVE(wl, source, entry);
2459 TAILQ_INSERT_TAIL(wl, source, entry);
2461 TAILQ_INSERT_BEFORE(target, source, entry);
2463 case SWM_ARG_ID_SWAPNEXT:
2464 target = TAILQ_NEXT(source, entry);
2465 /* move the parent and let the sort handle the move */
2466 if (source->transient)
2467 source = find_window(source->transient);
2468 TAILQ_REMOVE(wl, source, entry);
2470 TAILQ_INSERT_HEAD(wl, source, entry);
2472 TAILQ_INSERT_AFTER(wl, target, source, entry);
2474 case SWM_ARG_ID_SWAPMAIN:
2475 target = TAILQ_FIRST(wl);
2476 if (target == source) {
2477 if (source->ws->focus_prev != NULL &&
2478 source->ws->focus_prev != target)
2480 source = source->ws->focus_prev;
2484 if (target == NULL || source == NULL)
2486 source->ws->focus_prev = target;
2487 TAILQ_REMOVE(wl, target, entry);
2488 TAILQ_INSERT_BEFORE(source, target, entry);
2489 TAILQ_REMOVE(wl, source, entry);
2490 TAILQ_INSERT_HEAD(wl, source, entry);
2492 case SWM_ARG_ID_MOVELAST:
2493 TAILQ_REMOVE(wl, source, entry);
2494 TAILQ_INSERT_TAIL(wl, source, entry);
2497 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2507 focus_prev(struct ws_win *win)
2509 struct ws_win *winfocus = NULL;
2510 struct ws_win *cur_focus = NULL;
2511 struct ws_win_list *wl = NULL;
2512 struct workspace *ws = NULL;
2514 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx\n", WINID(win));
2516 if (!(win && win->ws))
2521 cur_focus = ws->focus;
2523 /* pickle, just focus on whatever */
2524 if (cur_focus == NULL) {
2525 /* use prev_focus if valid */
2526 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2527 find_window(WINID(ws->focus_prev)))
2528 winfocus = ws->focus_prev;
2529 if (winfocus == NULL)
2530 winfocus = TAILQ_FIRST(wl);
2534 /* if transient focus on parent */
2535 if (cur_focus->transient) {
2536 winfocus = find_window(cur_focus->transient);
2540 /* if in max_stack try harder */
2541 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2542 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2543 if (cur_focus != ws->focus_prev)
2544 winfocus = ws->focus_prev;
2546 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2551 if (cur_focus == win)
2552 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2553 if (winfocus == NULL)
2554 winfocus = TAILQ_LAST(wl, ws_win_list);
2555 if (winfocus == NULL || winfocus == win)
2556 winfocus = TAILQ_NEXT(cur_focus, entry);
2559 focus_magic(winfocus);
2563 focus(struct swm_region *r, union arg *args)
2565 struct ws_win *winfocus = NULL, *head;
2566 struct ws_win *cur_focus = NULL;
2567 struct ws_win_list *wl = NULL;
2568 struct workspace *ws = NULL;
2574 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2576 /* treat FOCUS_CUR special */
2577 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2578 if (r->ws->focus && r->ws->focus->iconic == 0)
2579 winfocus = r->ws->focus;
2580 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2581 winfocus = r->ws->focus_prev;
2583 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2584 if (winfocus->iconic == 0)
2587 focus_magic(winfocus);
2591 if ((cur_focus = r->ws->focus) == NULL)
2595 if (TAILQ_EMPTY(wl))
2597 /* make sure there is at least one uniconified window */
2599 TAILQ_FOREACH(winfocus, wl, entry)
2600 if (winfocus->iconic == 0) {
2608 case SWM_ARG_ID_FOCUSPREV:
2609 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2611 head = TAILQ_LAST(wl, ws_win_list);
2613 if (WINID(winfocus) == cur_focus->transient) {
2614 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2616 head = TAILQ_LAST(wl, ws_win_list);
2621 if (winfocus && winfocus->iconic) {
2622 while (winfocus != cur_focus) {
2623 if (winfocus == NULL)
2624 winfocus = TAILQ_LAST(wl, ws_win_list);
2625 if (winfocus->iconic == 0)
2627 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2633 case SWM_ARG_ID_FOCUSNEXT:
2634 head = TAILQ_NEXT(cur_focus, entry);
2636 head = TAILQ_FIRST(wl);
2640 if (winfocus && winfocus->iconic) {
2641 while (winfocus != cur_focus) {
2642 if (winfocus == NULL)
2643 winfocus = TAILQ_FIRST(wl);
2644 if (winfocus->iconic == 0)
2646 winfocus = TAILQ_NEXT(winfocus, entry);
2651 case SWM_ARG_ID_FOCUSMAIN:
2652 winfocus = TAILQ_FIRST(wl);
2653 if (winfocus == cur_focus)
2654 winfocus = cur_focus->ws->focus_prev;
2661 focus_magic(winfocus);
2665 cycle_layout(struct swm_region *r, union arg *args)
2667 struct workspace *ws = r->ws;
2670 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2673 if (ws->cur_layout->l_stack == NULL)
2674 ws->cur_layout = &layouts[0];
2677 if (focus_mode == SWM_FOCUS_DEFAULT)
2678 drain_enter_notify();
2679 a.id = SWM_ARG_ID_FOCUSCUR;
2685 stack_config(struct swm_region *r, union arg *args)
2687 struct workspace *ws = r->ws;
2689 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2692 if (ws->cur_layout->l_config != NULL)
2693 ws->cur_layout->l_config(ws, args->id);
2695 if (args->id != SWM_ARG_ID_STACKINIT)
2702 struct swm_geometry g;
2703 struct swm_region *r;
2709 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2711 for (i = 0; i < ScreenCount(display); i++) {
2715 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2716 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2717 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2719 /* start with screen geometry, adjust for bar */
2721 g.w -= 2 * border_width;
2722 g.h -= 2 * border_width;
2728 r->ws->cur_layout->l_stack(r->ws, &g);
2729 r->ws->cur_layout->l_string(r->ws);
2730 /* save r so we can track region changes */
2737 if (focus_mode == SWM_FOCUS_DEFAULT)
2738 drain_enter_notify();
2740 DNPRINTF(SWM_D_STACK, "stack: end\n");
2744 store_float_geom(struct ws_win *win, struct swm_region *r)
2746 /* retain window geom and region geom */
2747 win->g_float = win->g;
2748 win->rg_float = r->g;
2749 win->g_floatvalid = 1;
2753 stack_floater(struct ws_win *win, struct swm_region *r)
2761 bzero(&wc, sizeof wc);
2762 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2765 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2766 * geom on ws switches or return from max mode
2768 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2769 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2771 * use stored g and rg to set relative position and size
2772 * as in old region or before max stack mode
2774 X(win) = win->g_float.x - win->rg_float.x + X(r);
2775 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2776 WIDTH(win) = win->g_float.w;
2777 HEIGHT(win) = win->g_float.h;
2778 win->g_floatvalid = 0;
2781 win->floatmaxed = 0;
2783 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2784 (HEIGHT(win) >= HEIGHT(r)))
2785 wc.border_width = 0;
2787 wc.border_width = border_width;
2788 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2789 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
2790 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
2795 * floaters and transients are auto-centred unless moved
2798 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
2799 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
2802 /* win can be outside r if new r smaller than old r */
2803 /* Ensure top left corner inside r (move probs otherwise) */
2804 if (X(win) < X(r) - wc.border_width)
2805 X(win) = X(r) - wc.border_width;
2806 if (X(win) > X(r) + WIDTH(r) - 1)
2807 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
2808 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
2809 if (Y(win) < Y(r) - wc.border_width)
2810 Y(win) = Y(r) - wc.border_width;
2811 if (Y(win) > Y(r) + HEIGHT(r) - 1)
2812 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
2813 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
2817 wc.width = WIDTH(win);
2818 wc.height = HEIGHT(win);
2821 * Retain floater and transient geometry for correct positioning
2822 * when ws changes region
2824 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2825 store_float_geom(win, r);
2827 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2828 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
2830 XConfigureWindow(display, win->id, mask, &wc);
2834 * Send keystrokes to terminal to decrease/increase the font size as the
2835 * window size changes.
2838 adjust_font(struct ws_win *win)
2840 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2841 win->floating || win->transient)
2844 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2845 WIDTH(win) / win->sh.width_inc < term_width &&
2846 win->font_steps < SWM_MAX_FONT_STEPS) {
2847 win->font_size_boundary[win->font_steps] =
2848 (win->sh.width_inc * term_width) + win->sh.base_width;
2851 win->last_inc = win->sh.width_inc;
2852 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2853 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2854 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
2857 win->last_inc = win->sh.width_inc;
2858 fake_keypress(win, XK_KP_Add, ShiftMask);
2862 #define SWAPXY(g) do { \
2864 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2865 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2868 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2871 XWindowAttributes wa;
2872 struct swm_geometry win_g, r_g = *g;
2873 struct ws_win *win, *fs_win = 0;
2874 int i, j, s, stacks;
2875 int w_inc = 1, h_inc, w_base = 1, h_base;
2876 int hrh, extra = 0, h_slice, last_h = 0;
2877 int split, colno, winno, mwin, msize, mscale;
2878 int remain, missing, v_slice, reconfigure;
2881 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
2882 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
2884 winno = count_win(ws, 0);
2885 if (winno == 0 && count_win(ws, 1) == 0)
2888 TAILQ_FOREACH(win, &ws->winlist, entry)
2889 if (win->transient == 0 && win->floating == 0
2890 && win->iconic == 0)
2897 w_inc = win->sh.width_inc;
2898 w_base = win->sh.base_width;
2899 mwin = ws->l_state.horizontal_mwin;
2900 mscale = ws->l_state.horizontal_msize;
2901 stacks = ws->l_state.horizontal_stacks;
2904 w_inc = win->sh.height_inc;
2905 w_base = win->sh.base_height;
2906 mwin = ws->l_state.vertical_mwin;
2907 mscale = ws->l_state.vertical_msize;
2908 stacks = ws->l_state.vertical_stacks;
2912 if (stacks > winno - mwin)
2913 stacks = winno - mwin;
2917 h_slice = r_g.h / SWM_H_SLICE;
2918 if (mwin && winno > mwin) {
2919 v_slice = r_g.w / SWM_V_SLICE;
2923 win_g.w = v_slice * mscale;
2925 if (w_inc > 1 && w_inc < v_slice) {
2926 /* adjust for window's requested size increment */
2927 remain = (win_g.w - w_base) % w_inc;
2933 win_g.x += r_g.w - msize;
2936 colno = split = winno / stacks;
2937 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2938 2 * border_width) / stacks);
2940 hrh = r_g.h / colno;
2941 extra = r_g.h - (colno * hrh);
2942 win_g.h = hrh - 2 * border_width;
2944 /* stack all the tiled windows */
2945 i = j = 0, s = stacks;
2946 TAILQ_FOREACH(win, &ws->winlist, entry) {
2947 if (win->transient != 0 || win->floating != 0)
2949 if (win->iconic != 0)
2952 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2957 if (split && i == split) {
2958 colno = (winno - mwin) / stacks;
2959 if (s <= (winno - mwin) % stacks)
2961 split = split + colno;
2962 hrh = (r_g.h / colno);
2963 extra = r_g.h - (colno * hrh);
2967 win_g.x += win_g.w + 2 * border_width;
2968 win_g.w = (r_g.w - msize -
2969 (stacks * 2 * border_width)) / stacks;
2971 win_g.w += (r_g.w - msize -
2972 (stacks * 2 * border_width)) % stacks;
2976 win_g.h = hrh - 2 * border_width;
2978 h_inc = win->sh.width_inc;
2979 h_base = win->sh.base_width;
2981 h_inc = win->sh.height_inc;
2982 h_base = win->sh.base_height;
2984 if (j == colno - 1) {
2985 win_g.h = hrh + extra;
2986 } else if (h_inc > 1 && h_inc < h_slice) {
2987 /* adjust for window's requested size increment */
2988 remain = (win_g.h - h_base) % h_inc;
2989 missing = h_inc - remain;
2991 if (missing <= extra || j == 0) {
3003 win_g.y += last_h + 2 * border_width;
3005 bzero(&wc, sizeof wc);
3006 if (disable_border && bar_enabled == 0 && winno == 1){
3007 wc.border_width = 0;
3008 win_g.w += 2 * border_width;
3009 win_g.h += 2 * border_width;
3011 wc.border_width = border_width;
3014 if (X(win) != win_g.y || Y(win) != win_g.x ||
3015 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3017 X(win) = wc.x = win_g.y;
3018 Y(win) = wc.y = win_g.x;
3019 WIDTH(win) = wc.width = win_g.h;
3020 HEIGHT(win) = wc.height = win_g.w;
3023 if (X(win) != win_g.x || Y(win) != win_g.y ||
3024 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3026 X(win) = wc.x = win_g.x;
3027 Y(win) = wc.y = win_g.y;
3028 WIDTH(win) = wc.width = win_g.w;
3029 HEIGHT(win) = wc.height = win_g.h;
3034 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3035 XConfigureWindow(display, win->id, mask, &wc);
3038 if (XGetWindowAttributes(display, win->id, &wa))
3039 if (wa.map_state == IsUnmapped)
3040 XMapRaised(display, win->id);
3048 /* now, stack all the floaters and transients */
3049 TAILQ_FOREACH(win, &ws->winlist, entry) {
3050 if (win->transient == 0 && win->floating == 0)
3052 if (win->iconic == 1)
3054 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3059 stack_floater(win, ws->r);
3060 XMapRaised(display, win->id);
3064 stack_floater(fs_win, ws->r);
3065 XMapRaised(display, fs_win->id);
3070 vertical_config(struct workspace *ws, int id)
3072 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3076 case SWM_ARG_ID_STACKRESET:
3077 case SWM_ARG_ID_STACKINIT:
3078 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3079 ws->l_state.vertical_mwin = 1;
3080 ws->l_state.vertical_stacks = 1;
3082 case SWM_ARG_ID_MASTERSHRINK:
3083 if (ws->l_state.vertical_msize > 1)
3084 ws->l_state.vertical_msize--;
3086 case SWM_ARG_ID_MASTERGROW:
3087 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3088 ws->l_state.vertical_msize++;
3090 case SWM_ARG_ID_MASTERADD:
3091 ws->l_state.vertical_mwin++;
3093 case SWM_ARG_ID_MASTERDEL:
3094 if (ws->l_state.vertical_mwin > 0)
3095 ws->l_state.vertical_mwin--;
3097 case SWM_ARG_ID_STACKINC:
3098 ws->l_state.vertical_stacks++;
3100 case SWM_ARG_ID_STACKDEC:
3101 if (ws->l_state.vertical_stacks > 1)
3102 ws->l_state.vertical_stacks--;
3104 case SWM_ARG_ID_FLIPLAYOUT:
3105 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3113 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3115 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3117 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3121 horizontal_config(struct workspace *ws, int id)
3123 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3126 case SWM_ARG_ID_STACKRESET:
3127 case SWM_ARG_ID_STACKINIT:
3128 ws->l_state.horizontal_mwin = 1;
3129 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3130 ws->l_state.horizontal_stacks = 1;
3132 case SWM_ARG_ID_MASTERSHRINK:
3133 if (ws->l_state.horizontal_msize > 1)
3134 ws->l_state.horizontal_msize--;
3136 case SWM_ARG_ID_MASTERGROW:
3137 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3138 ws->l_state.horizontal_msize++;
3140 case SWM_ARG_ID_MASTERADD:
3141 ws->l_state.horizontal_mwin++;
3143 case SWM_ARG_ID_MASTERDEL:
3144 if (ws->l_state.horizontal_mwin > 0)
3145 ws->l_state.horizontal_mwin--;
3147 case SWM_ARG_ID_STACKINC:
3148 ws->l_state.horizontal_stacks++;
3150 case SWM_ARG_ID_STACKDEC:
3151 if (ws->l_state.horizontal_stacks > 1)
3152 ws->l_state.horizontal_stacks--;
3154 case SWM_ARG_ID_FLIPLAYOUT:
3155 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3163 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3165 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3167 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3170 /* fullscreen view */
3172 max_stack(struct workspace *ws, struct swm_geometry *g)
3175 struct swm_geometry gg = *g;
3176 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3180 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3185 winno = count_win(ws, 0);
3186 if (winno == 0 && count_win(ws, 1) == 0)
3189 TAILQ_FOREACH(win, &ws->winlist, entry) {
3190 if (win->transient) {
3192 parent = find_window(win->transient);
3196 if (win->floating && win->floatmaxed == 0 ) {
3198 * retain geometry for retrieval on exit from
3201 store_float_geom(win, ws->r);
3202 win->floatmaxed = 1;
3205 /* only reconfigure if necessary */
3206 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3207 HEIGHT(win) != gg.h) {
3208 bzero(&wc, sizeof wc);
3209 X(win) = wc.x = gg.x;
3210 Y(win) = wc.y = gg.y;
3212 wc.border_width = border_width;
3213 WIDTH(win) = wc.width = gg.w;
3214 HEIGHT(win) = wc.height = gg.h;
3216 wc.border_width = 0;
3217 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3218 HEIGHT(win) = wc.height = gg.h +
3221 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3222 XConfigureWindow(display, win->id, mask, &wc);
3224 /* unmap only if we don't have multi screen */
3225 if (win != ws->focus)
3226 if (!(ScreenCount(display) > 1 || outputs > 1))
3230 /* put the last transient on top */
3233 XMapRaised(display, parent->id);
3234 stack_floater(wintrans, ws->r);
3235 focus_magic(wintrans);
3240 send_to_ws(struct swm_region *r, union arg *args)
3242 int wsid = args->id;
3243 struct ws_win *win = NULL, *parent;
3244 struct workspace *ws, *nws;
3245 Atom ws_idx_atom = 0;
3246 unsigned char ws_idx_str[SWM_PROPLEN];
3249 if (wsid >= workspace_limit)
3252 if (r && r->ws && r->ws->focus)
3258 if (win->ws->idx == wsid)
3261 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3264 nws = &win->s->ws[wsid];
3266 a.id = SWM_ARG_ID_FOCUSPREV;
3268 if (win->transient) {
3269 parent = find_window(win->transient);
3271 unmap_window(parent);
3272 TAILQ_REMOVE(&ws->winlist, parent, entry);
3273 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3278 TAILQ_REMOVE(&ws->winlist, win, entry);
3279 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3280 if (TAILQ_EMPTY(&ws->winlist))
3281 r->ws->focus = NULL;
3284 /* Try to update the window's workspace property */
3285 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3287 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3289 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3291 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3292 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
3299 pressbutton(struct swm_region *r, union arg *args)
3301 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3302 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3306 raise_toggle(struct swm_region *r, union arg *args)
3308 if (r == NULL || r->ws == NULL)
3311 r->ws->always_raise = !r->ws->always_raise;
3313 /* bring floaters back to top */
3314 if (r->ws->always_raise == 0)
3319 iconify(struct swm_region *r, union arg *args)
3323 if (r->ws->focus == NULL)
3325 unmap_window(r->ws->focus);
3326 update_iconic(r->ws->focus, 1);
3328 if (focus_mode == SWM_FOCUS_DEFAULT)
3329 drain_enter_notify();
3330 r->ws->focus = NULL;
3331 a.id = SWM_ARG_ID_FOCUSCUR;
3336 get_win_name(Window win)
3338 unsigned char *prop = NULL;
3339 unsigned long nbytes, nitems;
3341 /* try _NET_WM_NAME first */
3342 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3345 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3346 &nitems, NULL, &prop))
3350 /* fallback to WM_NAME */
3351 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3354 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3361 uniconify(struct swm_region *r, union arg *args)
3365 unsigned char *name;
3368 DNPRINTF(SWM_D_MISC, "uniconify\n");
3370 if (r == NULL || r->ws == NULL)
3373 /* make sure we have anything to uniconify */
3374 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3375 if (win->ws == NULL)
3376 continue; /* should never happen */
3377 if (win->iconic == 0)
3385 search_resp_action = SWM_SEARCH_UNICONIFY;
3387 spawn_select(r, args, "search", &searchpid);
3389 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3392 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3393 if (win->ws == NULL)
3394 continue; /* should never happen */
3395 if (win->iconic == 0)
3398 name = get_win_name(win->id);
3401 fprintf(lfile, "%s.%lu\n", name, win->id);
3409 name_workspace(struct swm_region *r, union arg *args)
3413 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3419 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3421 spawn_select(r, args, "name_workspace", &searchpid);
3423 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3426 fprintf(lfile, "%s", "");
3431 search_workspace(struct swm_region *r, union arg *args)
3434 struct workspace *ws;
3437 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3443 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3445 spawn_select(r, args, "search", &searchpid);
3447 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3450 for (i = 0; i < workspace_limit; i++) {
3454 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3455 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3462 search_win_cleanup(void)
3464 struct search_window *sw = NULL;
3466 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3467 XDestroyWindow(display, sw->indicator);
3468 XFreeGC(display, sw->gc);
3469 TAILQ_REMOVE(&search_wl, sw, entry);
3475 search_win(struct swm_region *r, union arg *args)
3477 struct ws_win *win = NULL;
3478 struct search_window *sw = NULL;
3485 XRectangle ibox, lbox;
3487 DNPRINTF(SWM_D_MISC, "search_win\n");
3490 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3492 spawn_select(r, args, "search", &searchpid);
3494 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3497 TAILQ_INIT(&search_wl);
3500 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3501 if (win->iconic == 1)
3504 sw = calloc(1, sizeof(struct search_window));
3506 warn("search_win: calloc");
3508 search_win_cleanup();
3514 snprintf(s, sizeof s, "%d", i);
3517 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3519 w = XCreateSimpleWindow(display,
3520 win->id, 0, 0,lbox.width + 4,
3521 bar_fs_extents->max_logical_extent.height, 1,
3522 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3523 r->s->c[SWM_S_COLOR_FOCUS].color);
3526 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3528 sw->gc = XCreateGC(display, w, 0, &gcv);
3529 XMapRaised(display, w);
3530 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3532 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3533 (bar_fs_extents->max_logical_extent.height -
3534 lbox.height) / 2 - lbox.y, s, len);
3536 fprintf(lfile, "%d\n", i);
3544 search_resp_uniconify(char *resp, unsigned long len)
3546 unsigned char *name;
3550 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3552 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3553 if (win->iconic == 0)
3555 name = get_win_name(win->id);
3558 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3563 if (strncmp(s, resp, len) == 0) {
3564 /* XXX this should be a callback to generalize */
3565 update_iconic(win, 0);
3574 search_resp_name_workspace(char *resp, unsigned long len)
3576 struct workspace *ws;
3578 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3580 if (search_r->ws == NULL)
3585 free(search_r->ws->name);
3586 search_r->ws->name = NULL;
3590 ws->name = strdup(resp);
3591 if (ws->name == NULL) {
3592 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3593 "strdup: %s", strerror(errno));
3600 search_resp_search_workspace(char *resp, unsigned long len)
3607 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3611 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3618 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3620 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3627 switchws(search_r, &a);
3631 search_resp_search_window(char *resp, unsigned long len)
3636 struct search_window *sw;
3638 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3642 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3647 idx = strtonum(s, 1, INT_MAX, &errstr);
3649 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3656 TAILQ_FOREACH(sw, &search_wl, entry)
3657 if (idx == sw->idx) {
3663 #define MAX_RESP_LEN 1024
3666 search_do_resp(void)
3672 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3677 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3678 warn("search: calloc");
3682 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3684 warn("search: read error");
3687 resp[rbytes] = '\0';
3690 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3691 * newline, so work around that by sanitizing the resp now.
3693 resp[strcspn(resp, "\n")] = '\0';
3696 switch (search_resp_action) {
3697 case SWM_SEARCH_UNICONIFY:
3698 search_resp_uniconify(resp, len);
3700 case SWM_SEARCH_NAME_WORKSPACE:
3701 search_resp_name_workspace(resp, len);
3703 case SWM_SEARCH_SEARCH_WORKSPACE:
3704 search_resp_search_workspace(resp, len);
3706 case SWM_SEARCH_SEARCH_WINDOW:
3707 search_resp_search_window(resp, len);
3712 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3713 search_win_cleanup();
3715 search_resp_action = SWM_SEARCH_NONE;
3716 close(select_resp_pipe[0]);
3721 wkill(struct swm_region *r, union arg *args)
3723 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3725 if (r->ws->focus == NULL)
3728 if (args->id == SWM_ARG_ID_KILLWINDOW)
3729 XKillClient(display, r->ws->focus->id);
3731 if (r->ws->focus->can_delete)
3732 client_msg(r->ws->focus, adelete);
3737 floating_toggle_win(struct ws_win *win)
3739 struct swm_region *r;
3749 /* reject floating toggles in max stack mode */
3750 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3753 if (win->floating) {
3754 if (!win->floatmaxed) {
3755 /* retain position for refloat */
3756 store_float_geom(win, r);
3760 if (win->g_floatvalid) {
3761 /* refloat at last floating relative position */
3762 X(win) = win->g_float.x - win->rg_float.x + X(r);
3763 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3764 WIDTH(win) = win->g_float.w;
3765 HEIGHT(win) = win->g_float.h;
3770 ewmh_update_actions(win);
3776 floating_toggle(struct swm_region *r, union arg *args)
3778 struct ws_win *win = r->ws->focus;
3784 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3785 _NET_WM_STATE_TOGGLE);
3788 if (focus_mode == SWM_FOCUS_DEFAULT)
3789 drain_enter_notify();
3791 if (win == win->ws->focus) {
3792 a.id = SWM_ARG_ID_FOCUSCUR;
3793 focus(win->ws->r, &a);
3798 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3800 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
3802 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
3804 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
3807 if (X(win) < X(r) - border_width) {
3809 WIDTH(win) -= X(r) - X(win) - border_width;
3811 X(win) = X(r) - border_width;
3814 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
3816 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
3818 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
3821 if (Y(win) < Y(r) - border_width) {
3823 HEIGHT(win) -= Y(r) - Y(win) - border_width;
3825 Y(win) = Y(r) - border_width;
3830 if (HEIGHT(win) < 1)
3835 update_window(struct ws_win *win)
3840 bzero(&wc, sizeof wc);
3841 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
3842 wc.border_width = border_width;
3845 wc.width = WIDTH(win);
3846 wc.height = HEIGHT(win);
3848 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
3849 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3851 XConfigureWindow(display, win->id, mask, &wc);
3854 #define SWM_RESIZE_STEPS (50)
3857 resize(struct ws_win *win, union arg *args)
3861 struct swm_region *r = NULL;
3862 int resize_step = 0;
3866 struct swm_geometry g;
3867 int top = 0, left = 0;
3870 unsigned int shape; /* cursor style */
3876 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
3877 "transient: 0x%lx\n", win->id, YESNO(win->floating),
3880 if (!(win->transient != 0 || win->floating != 0))
3883 /* reject resizes in max mode for floaters (transient ok) */
3884 if (win->floatmaxed)
3888 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3894 case SWM_ARG_ID_WIDTHSHRINK:
3895 WIDTH(win) -= SWM_RESIZE_STEPS;
3898 case SWM_ARG_ID_WIDTHGROW:
3899 WIDTH(win) += SWM_RESIZE_STEPS;
3902 case SWM_ARG_ID_HEIGHTSHRINK:
3903 HEIGHT(win) -= SWM_RESIZE_STEPS;
3906 case SWM_ARG_ID_HEIGHTGROW:
3907 HEIGHT(win) += SWM_RESIZE_STEPS;
3914 constrain_window(win, r, 1);
3916 store_float_geom(win,r);
3920 if (focus_mode == SWM_FOCUS_DEFAULT)
3921 drain_enter_notify();
3923 /* get cursor offset from window root */
3924 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
3929 if (wx < WIDTH(win) / 2)
3932 if (wy < HEIGHT(win) / 2)
3935 if (args->id == SWM_ARG_ID_CENTER)
3938 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
3940 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
3942 cursor = XCreateFontCursor(display, shape);
3944 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3945 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
3946 XFreeCursor(display, cursor);
3951 XMaskEvent(display, MOUSEMASK | ExposureMask |
3952 SubstructureRedirectMask, &ev);
3954 case ConfigureRequest:
3957 handler[ev.type](&ev);
3960 /* cursor offset/delta from start of the operation */
3961 dx = ev.xmotion.x_root - x;
3962 dy = ev.xmotion.y_root - y;
3968 if (args->id == SWM_ARG_ID_CENTER) {
3969 if (g.h / 2 + dy < 1)
3973 HEIGHT(win) = g.h + 2 * dy;
3981 HEIGHT(win) = g.h + dy;
3988 if (args->id == SWM_ARG_ID_CENTER) {
3989 if (g.w / 2 + dx < 1)
3993 WIDTH(win) = g.w + 2 * dx;
4001 WIDTH(win) = g.w + dx;
4004 constrain_window(win, r, 1);
4006 /* not free, don't sync more than 120 times / second */
4007 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4008 time = ev.xmotion.time;
4009 XSync(display, False);
4014 } while (ev.type != ButtonRelease);
4016 XSync(display, False);
4019 store_float_geom(win,r);
4021 XUngrabPointer(display, CurrentTime);
4022 XFreeCursor(display, cursor);
4025 drain_enter_notify();
4029 resize_step(struct swm_region *r, union arg *args)
4031 struct ws_win *win = NULL;
4033 if (r && r->ws && r->ws->focus)
4041 #define SWM_MOVE_STEPS (50)
4044 move(struct ws_win *win, union arg *args)
4049 struct swm_region *r = NULL;
4059 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4060 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4062 /* in max_stack mode should only move transients */
4063 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4067 if (win->floating == 0 && !win->transient) {
4068 store_float_geom(win,r);
4069 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4072 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4079 case SWM_ARG_ID_MOVELEFT:
4080 X(win) -= (SWM_MOVE_STEPS - border_width);
4083 case SWM_ARG_ID_MOVERIGHT:
4084 X(win) += (SWM_MOVE_STEPS - border_width);
4087 case SWM_ARG_ID_MOVEUP:
4088 Y(win) -= (SWM_MOVE_STEPS - border_width);
4091 case SWM_ARG_ID_MOVEDOWN:
4092 Y(win) += (SWM_MOVE_STEPS - border_width);
4099 constrain_window(win, r, 0);
4101 store_float_geom(win, r);
4105 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4106 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4107 CurrentTime) != GrabSuccess)
4110 /* get cursor offset from window root */
4111 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4115 XMaskEvent(display, MOUSEMASK | ExposureMask |
4116 SubstructureRedirectMask, &ev);
4118 case ConfigureRequest:
4121 handler[ev.type](&ev);
4124 X(win) = ev.xmotion.x_root - wx - border_width;
4125 Y(win) = ev.xmotion.y_root - wy - border_width;
4127 constrain_window(win, r, 0);
4129 /* not free, don't sync more than 120 times / second */
4130 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4131 time = ev.xmotion.time;
4132 XSync(display, False);
4137 } while (ev.type != ButtonRelease);
4139 XSync(display, False);
4142 store_float_geom(win,r);
4143 XUngrabPointer(display, CurrentTime);
4146 drain_enter_notify();
4150 move_step(struct swm_region *r, union arg *args)
4152 struct ws_win *win = NULL;
4154 if (r && r->ws && r->ws->focus)
4159 if (!(win->transient != 0 || win->floating != 0))
4166 /* user/key callable function IDs */
4261 kf_search_workspace,
4263 kf_dumpwins, /* MUST BE LAST */
4267 /* key definitions */
4269 dummykeyfunc(struct swm_region *r, union arg *args)
4274 legacyfunc(struct swm_region *r, union arg *args)
4279 char name[SWM_FUNCNAME_LEN];
4280 void (*func)(struct swm_region *r, union arg *);
4282 } keyfuncs[kf_invalid + 1] = {
4283 /* name function argument */
4284 { "cycle_layout", cycle_layout, {0} },
4285 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4286 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4287 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4288 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4289 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4290 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4291 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4292 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4293 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4294 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4295 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4296 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4297 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4298 { "spawn_term", spawnterm, {.argv = spawn_term} },
4299 { "spawn_menu", legacyfunc, {0} },
4300 { "quit", quit, {0} },
4301 { "restart", restart, {0} },
4302 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4303 { "ws_1", switchws, {.id = 0} },
4304 { "ws_2", switchws, {.id = 1} },
4305 { "ws_3", switchws, {.id = 2} },
4306 { "ws_4", switchws, {.id = 3} },
4307 { "ws_5", switchws, {.id = 4} },
4308 { "ws_6", switchws, {.id = 5} },
4309 { "ws_7", switchws, {.id = 6} },
4310 { "ws_8", switchws, {.id = 7} },
4311 { "ws_9", switchws, {.id = 8} },
4312 { "ws_10", switchws, {.id = 9} },
4313 { "ws_11", switchws, {.id = 10} },
4314 { "ws_12", switchws, {.id = 11} },
4315 { "ws_13", switchws, {.id = 12} },
4316 { "ws_14", switchws, {.id = 13} },
4317 { "ws_15", switchws, {.id = 14} },
4318 { "ws_16", switchws, {.id = 15} },
4319 { "ws_17", switchws, {.id = 16} },
4320 { "ws_18", switchws, {.id = 17} },
4321 { "ws_19", switchws, {.id = 18} },
4322 { "ws_20", switchws, {.id = 19} },
4323 { "ws_21", switchws, {.id = 20} },
4324 { "ws_22", switchws, {.id = 21} },
4325 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4326 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4327 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4328 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4329 { "ws_prior", priorws, {0} },
4330 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4331 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4332 { "mvws_1", send_to_ws, {.id = 0} },
4333 { "mvws_2", send_to_ws, {.id = 1} },
4334 { "mvws_3", send_to_ws, {.id = 2} },
4335 { "mvws_4", send_to_ws, {.id = 3} },
4336 { "mvws_5", send_to_ws, {.id = 4} },
4337 { "mvws_6", send_to_ws, {.id = 5} },
4338 { "mvws_7", send_to_ws, {.id = 6} },
4339 { "mvws_8", send_to_ws, {.id = 7} },
4340 { "mvws_9", send_to_ws, {.id = 8} },
4341 { "mvws_10", send_to_ws, {.id = 9} },
4342 { "mvws_11", send_to_ws, {.id = 10} },
4343 { "mvws_12", send_to_ws, {.id = 11} },
4344 { "mvws_13", send_to_ws, {.id = 12} },
4345 { "mvws_14", send_to_ws, {.id = 13} },
4346 { "mvws_15", send_to_ws, {.id = 14} },
4347 { "mvws_16", send_to_ws, {.id = 15} },
4348 { "mvws_17", send_to_ws, {.id = 16} },
4349 { "mvws_18", send_to_ws, {.id = 17} },
4350 { "mvws_19", send_to_ws, {.id = 18} },
4351 { "mvws_20", send_to_ws, {.id = 19} },
4352 { "mvws_21", send_to_ws, {.id = 20} },
4353 { "mvws_22", send_to_ws, {.id = 21} },
4354 { "bar_toggle", bar_toggle, {0} },
4355 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4356 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4357 { "screenshot_all", legacyfunc, {0} },
4358 { "screenshot_wind", legacyfunc, {0} },
4359 { "float_toggle", floating_toggle,{0} },
4360 { "version", version, {0} },
4361 { "spawn_lock", legacyfunc, {0} },
4362 { "spawn_initscr", legacyfunc, {0} },
4363 { "spawn_custom", dummykeyfunc, {0} },
4364 { "iconify", iconify, {0} },
4365 { "uniconify", uniconify, {0} },
4366 { "raise_toggle", raise_toggle, {0} },
4367 { "button2", pressbutton, {2} },
4368 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4369 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4370 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4371 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4372 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4373 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4374 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4375 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4376 { "name_workspace", name_workspace, {0} },
4377 { "search_workspace", search_workspace, {0} },
4378 { "search_win", search_win, {0} },
4379 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4380 { "invalid key func", NULL, {0} },
4383 RB_ENTRY(key) entry;
4386 enum keyfuncid funcid;
4389 RB_HEAD(key_list, key);
4392 key_cmp(struct key *kp1, struct key *kp2)
4394 if (kp1->keysym < kp2->keysym)
4396 if (kp1->keysym > kp2->keysym)
4399 if (kp1->mod < kp2->mod)
4401 if (kp1->mod > kp2->mod)
4407 RB_GENERATE_STATIC(key_list, key, entry, key_cmp);
4408 struct key_list keys;
4411 enum { client_click, root_click };
4413 unsigned int action;
4415 unsigned int button;
4416 void (*func)(struct ws_win *, union arg *);
4419 /* action key mouse button func args */
4420 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4421 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4422 { client_click, MODKEY, Button1, move, {0} },
4426 update_modkey(unsigned int mod)
4432 RB_FOREACH(kp, key_list, &keys)
4433 if (kp->mod & ShiftMask)
4434 kp->mod = mod | ShiftMask;
4438 for (i = 0; i < LENGTH(buttons); i++)
4439 if (buttons[i].mask & ShiftMask)
4440 buttons[i].mask = mod | ShiftMask;
4442 buttons[i].mask = mod;
4447 TAILQ_ENTRY(spawn_prog) entry;
4452 TAILQ_HEAD(spawn_list, spawn_prog);
4453 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4456 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4459 struct spawn_prog *prog = NULL;
4461 char *ap, **real_args;
4463 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4466 TAILQ_FOREACH(prog, &spawns, entry) {
4467 if (!strcasecmp(spawn_name, prog->name))
4471 warnx("spawn_custom: program %s not found", spawn_name);
4475 /* make room for expanded args */
4476 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4477 err(1, "spawn_custom: calloc real_args");
4479 /* expand spawn_args into real_args */
4480 for (i = 0; i < prog->argc; i++) {
4482 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4483 if (!strcasecmp(ap, "$bar_border")) {
4485 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4487 err(1, "spawn_custom border color");
4488 } else if (!strcasecmp(ap, "$bar_color")) {
4490 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4492 err(1, "spawn_custom bar color");
4493 } else if (!strcasecmp(ap, "$bar_font")) {
4494 if ((real_args[i] = strdup(bar_fonts))
4496 err(1, "spawn_custom bar fonts");
4497 } else if (!strcasecmp(ap, "$bar_font_color")) {
4499 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4501 err(1, "spawn_custom color font");
4502 } else if (!strcasecmp(ap, "$color_focus")) {
4504 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4506 err(1, "spawn_custom color focus");
4507 } else if (!strcasecmp(ap, "$color_unfocus")) {
4509 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4511 err(1, "spawn_custom color unfocus");
4513 /* no match --> copy as is */
4514 if ((real_args[i] = strdup(ap)) == NULL)
4515 err(1, "spawn_custom strdup(ap)");
4517 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4522 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4523 for (i = 0; i < prog->argc; i++)
4524 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4525 DNPRINTF(SWM_D_SPAWN, "\n");
4527 *ret_args = real_args;
4528 return (prog->argc);
4532 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4538 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4542 spawn(r->ws->idx, &a, 1);
4544 for (i = 0; i < spawn_argc; i++)
4550 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4556 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4560 if (pipe(select_list_pipe) == -1)
4561 err(1, "pipe error");
4562 if (pipe(select_resp_pipe) == -1)
4563 err(1, "pipe error");
4565 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4566 err(1, "could not disable SIGPIPE");
4567 switch (*pid = fork()) {
4569 err(1, "cannot fork");
4572 if (dup2(select_list_pipe[0], 0) == -1)
4574 if (dup2(select_resp_pipe[1], 1) == -1)
4576 close(select_list_pipe[1]);
4577 close(select_resp_pipe[0]);
4578 spawn(r->ws->idx, &a, 0);
4580 default: /* parent */
4581 close(select_list_pipe[0]);
4582 close(select_resp_pipe[1]);
4586 for (i = 0; i < spawn_argc; i++)
4592 spawn_insert(char *name, char *args)
4594 char *arg, *cp, *ptr;
4595 struct spawn_prog *sp;
4597 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4599 if ((sp = calloc(1, sizeof *sp)) == NULL)
4600 err(1, "spawn_insert: malloc");
4601 if ((sp->name = strdup(name)) == NULL)
4602 err(1, "spawn_insert: strdup");
4604 /* convert the arguments to an argument list */
4605 if ((ptr = cp = strdup(args)) == NULL)
4606 err(1, "spawn_insert: strdup");
4607 while ((arg = strsep(&ptr, " \t")) != NULL) {
4608 /* empty field; skip it */
4613 if ((sp->argv = realloc(sp->argv, sp->argc *
4614 sizeof *sp->argv)) == NULL)
4615 err(1, "spawn_insert: realloc");
4616 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4617 err(1, "spawn_insert: strdup");
4621 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4622 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4626 spawn_remove(struct spawn_prog *sp)
4630 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4632 TAILQ_REMOVE(&spawns, sp, entry);
4633 for (i = 0; i < sp->argc; i++)
4639 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4643 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4645 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4648 spawn_insert(name, args);
4650 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4654 setspawn(char *name, char *args)
4656 struct spawn_prog *sp;
4658 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4663 TAILQ_FOREACH(sp, &spawns, entry) {
4664 if (!strcmp(sp->name, name)) {
4668 spawn_replace(sp, name, args);
4669 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4673 if (*args == '\0') {
4674 warnx("error: setspawn: cannot find program: %s", name);
4678 spawn_insert(name, args);
4679 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4683 setconfspawn(char *selector, char *value, int flags)
4685 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4687 setspawn(selector, value);
4689 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4696 setconfspawn("term", "xterm", 0);
4697 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4698 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4699 setconfspawn("lock", "xlock", 0);
4700 setconfspawn("initscr", "initscreen.sh", 0);
4701 setconfspawn("menu", "dmenu_run"
4704 " -nf $bar_font_color"
4706 " -sf $bar_color", 0);
4707 setconfspawn("search", "dmenu"
4711 " -nf $bar_font_color"
4713 " -sf $bar_color", 0);
4714 setconfspawn("name_workspace", "dmenu"
4718 " -nf $bar_font_color"
4720 " -sf $bar_color", 0);
4724 #define SWM_MODNAME_SIZE 32
4725 #define SWM_KEY_WS "\n+ \t"
4727 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4731 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4732 if (mod == NULL || ks == NULL) {
4733 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4736 if (keystr == NULL || strlen(keystr) == 0) {
4737 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4743 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4744 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4746 cp += (long)strspn(cp, SWM_KEY_WS);
4747 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4749 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4751 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4753 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4755 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4757 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4759 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4760 *mod |= ControlMask;
4762 *ks = XStringToKeysym(name);
4763 XConvertCase(*ks, ks, &uks);
4764 if (ks == NoSymbol) {
4766 "parsekeys: invalid key %s\n",
4772 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4777 strdupsafe(char *str)
4782 return (strdup(str));
4786 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
4790 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
4791 keyfuncs[kfid].name, spawn_name);
4793 if ((kp = malloc(sizeof *kp)) == NULL)
4794 err(1, "key_insert: malloc");
4799 kp->spawn_name = strdupsafe(spawn_name);
4800 RB_INSERT(key_list, &keys, kp);
4802 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
4806 key_lookup(unsigned int mod, KeySym ks)
4813 return (RB_FIND(key_list, &keys, &kp));
4817 key_remove(struct key *kp)
4819 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
4821 RB_REMOVE(key_list, &keys, kp);
4822 free(kp->spawn_name);
4825 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
4829 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
4832 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
4836 key_insert(mod, ks, kfid, spawn_name);
4838 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
4842 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4847 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4848 keyfuncs[kfid].name, spawn_name);
4850 if ((kp = key_lookup(mod, ks)) != NULL) {
4851 if (kfid == kf_invalid)
4854 key_replace(kp, mod, ks, kfid, spawn_name);
4855 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4858 if (kfid == kf_invalid) {
4859 warnx("error: setkeybinding: cannot find mod/key combination");
4860 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4864 key_insert(mod, ks, kfid, spawn_name);
4865 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4869 setconfbinding(char *selector, char *value, int flags)
4871 enum keyfuncid kfid;
4874 struct spawn_prog *sp;
4875 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4876 if (selector == NULL) {
4877 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4878 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4880 setkeybinding(mod, ks, kfid, NULL);
4885 /* search by key function name */
4886 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4887 if (strncasecmp(selector, keyfuncs[kfid].name,
4888 SWM_FUNCNAME_LEN) == 0) {
4889 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4891 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4892 setkeybinding(mod, ks, kfid, NULL);
4898 /* search by custom spawn name */
4899 TAILQ_FOREACH(sp, &spawns, entry) {
4900 if (strcasecmp(selector, sp->name) == 0) {
4901 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4903 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4904 setkeybinding(mod, ks, kf_spawn_custom,
4911 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4918 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4919 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
4920 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4921 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4922 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4923 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4924 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4925 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4926 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4927 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4928 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4929 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4930 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4931 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4932 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4933 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4934 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4935 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4936 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4937 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4938 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4939 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4940 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4941 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4942 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4943 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4944 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4945 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4946 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4947 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
4948 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
4949 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
4950 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
4951 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
4952 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
4953 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
4954 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
4955 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
4956 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
4957 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
4958 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
4959 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4960 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4961 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4962 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4963 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4964 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4965 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4966 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4967 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4968 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4969 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4970 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4971 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4972 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4973 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4974 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4975 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4976 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
4977 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
4978 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
4979 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
4980 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
4981 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
4982 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
4983 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
4984 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
4985 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
4986 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
4987 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
4988 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4989 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4990 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4991 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4992 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4993 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4994 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4995 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4996 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4997 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4998 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4999 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5000 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5001 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5002 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5003 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5004 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5005 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5006 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5007 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5008 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5009 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5010 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5011 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5012 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5013 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5015 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5024 while (RB_EMPTY(&keys) == 0) {
5025 kp = RB_ROOT(&keys);
5031 setkeymapping(char *selector, char *value, int flags)
5033 char keymapping_file[PATH_MAX];
5034 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5035 if (value[0] == '~')
5036 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5037 pwd->pw_dir, &value[1]);
5039 strlcpy(keymapping_file, value, sizeof keymapping_file);
5041 /* load new key bindings; if it fails, revert to default bindings */
5042 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5046 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5051 updatenumlockmask(void)
5054 XModifierKeymap *modmap;
5056 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5058 modmap = XGetModifierMapping(display);
5059 for (i = 0; i < 8; i++)
5060 for (j = 0; j < modmap->max_keypermod; j++)
5061 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5062 == XKeysymToKeycode(display, XK_Num_Lock))
5063 numlockmask = (1 << i);
5065 XFreeModifiermap(modmap);
5073 unsigned int modifiers[] =
5074 { 0, LockMask, numlockmask, numlockmask | LockMask };
5077 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5078 updatenumlockmask();
5080 for (k = 0; k < ScreenCount(display); k++) {
5081 if (TAILQ_EMPTY(&screens[k].rl))
5083 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
5084 RB_FOREACH(kp, key_list, &keys) {
5085 if ((code = XKeysymToKeycode(display, kp->keysym)))
5086 for (j = 0; j < LENGTH(modifiers); j++)
5087 XGrabKey(display, code,
5088 kp->mod | modifiers[j],
5089 screens[k].root, True,
5090 GrabModeAsync, GrabModeAsync);
5096 grabbuttons(struct ws_win *win, int focused)
5099 unsigned int modifiers[] =
5100 { 0, LockMask, numlockmask, numlockmask|LockMask };
5102 updatenumlockmask();
5103 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5105 for (i = 0; i < LENGTH(buttons); i++)
5106 if (buttons[i].action == client_click)
5107 for (j = 0; j < LENGTH(modifiers); j++)
5108 XGrabButton(display, buttons[i].button,
5109 buttons[i].mask | modifiers[j],
5110 win->id, False, BUTTONMASK,
5111 GrabModeAsync, GrabModeSync, None,
5114 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5115 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5118 const char *quirkname[] = {
5119 "NONE", /* config string for "no value" */
5128 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5129 #define SWM_Q_WS "\n|+ \t"
5131 parsequirks(char *qstr, unsigned long *quirk)
5141 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5143 cp += (long)strspn(cp, SWM_Q_WS);
5144 for (i = 0; i < LENGTH(quirkname); i++) {
5145 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5146 DNPRINTF(SWM_D_QUIRK,
5147 "parsequirks: %s\n", name);
5152 *quirk |= 1 << (i-1);
5156 if (i >= LENGTH(quirkname)) {
5157 DNPRINTF(SWM_D_QUIRK,
5158 "parsequirks: invalid quirk [%s]\n", name);
5166 quirk_insert(const char *class, const char *name, unsigned long quirk)
5170 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5173 if ((qp = malloc(sizeof *qp)) == NULL)
5174 err(1, "quirk_insert: malloc");
5175 if ((qp->class = strdup(class)) == NULL)
5176 err(1, "quirk_insert: strdup");
5177 if ((qp->name = strdup(name)) == NULL)
5178 err(1, "quirk_insert: strdup");
5181 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5183 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5187 quirk_remove(struct quirk *qp)
5189 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5190 qp->name, qp->quirk);
5192 TAILQ_REMOVE(&quirks, qp, entry);
5197 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5201 quirk_replace(struct quirk *qp, const char *class, const char *name,
5202 unsigned long quirk)
5204 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5205 qp->name, qp->quirk);
5208 quirk_insert(class, name, quirk);
5210 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5214 setquirk(const char *class, const char *name, unsigned long quirk)
5218 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5221 TAILQ_FOREACH(qp, &quirks, entry) {
5222 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5226 quirk_replace(qp, class, name, quirk);
5227 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5232 warnx("error: setquirk: cannot find class/name combination");
5236 quirk_insert(class, name, quirk);
5237 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5241 setconfquirk(char *selector, char *value, int flags)
5243 char *cp, *class, *name;
5245 unsigned long quirks;
5246 if (selector == NULL)
5248 if ((cp = strchr(selector, ':')) == NULL)
5253 if ((retval = parsequirks(value, &quirks)) == 0)
5254 setquirk(class, name, quirks);
5261 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5262 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5263 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5264 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5265 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5266 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5267 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5268 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5269 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5270 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5271 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5272 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5273 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5276 /* conf file stuff */
5277 #define SWM_CONF_FILE "spectrwm.conf"
5278 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5280 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5281 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5282 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_WORKSPACE_LIMIT,
5283 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5284 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5285 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5286 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5287 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5288 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5292 setconfvalue(char *selector, char *value, int flags)
5298 case SWM_S_BAR_DELAY:
5299 bar_delay = atoi(value);
5301 case SWM_S_BAR_ENABLED:
5302 bar_enabled = atoi(value);
5304 case SWM_S_BAR_BORDER_WIDTH:
5305 bar_border_width = atoi(value);
5307 case SWM_S_BAR_AT_BOTTOM:
5308 bar_at_bottom = atoi(value);
5310 case SWM_S_BAR_JUSTIFY:
5311 if (!strcmp(value, "left"))
5312 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5313 else if (!strcmp(value, "center"))
5314 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5315 else if (!strcmp(value, "right"))
5316 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5318 errx(1, "invalid bar_justify");
5320 case SWM_S_STACK_ENABLED:
5321 stack_enabled = atoi(value);
5323 case SWM_S_CLOCK_ENABLED:
5324 clock_enabled = atoi(value);
5326 case SWM_S_CLOCK_FORMAT:
5327 #ifndef SWM_DENY_CLOCK_FORMAT
5329 if ((clock_format = strdup(value)) == NULL)
5330 err(1, "setconfvalue: clock_format");
5333 case SWM_S_CYCLE_EMPTY:
5334 cycle_empty = atoi(value);
5336 case SWM_S_CYCLE_VISIBLE:
5337 cycle_visible = atoi(value);
5339 case SWM_S_WORKSPACE_LIMIT:
5340 workspace_limit = atoi(value);
5341 if (workspace_limit > SWM_WS_MAX)
5342 workspace_limit = SWM_WS_MAX;
5343 else if (workspace_limit < 1)
5344 workspace_limit = 1;
5346 case SWM_S_SS_ENABLED:
5347 ss_enabled = atoi(value);
5349 case SWM_S_TERM_WIDTH:
5350 term_width = atoi(value);
5352 case SWM_S_TITLE_CLASS_ENABLED:
5353 title_class_enabled = atoi(value);
5355 case SWM_S_WINDOW_NAME_ENABLED:
5356 window_name_enabled = atoi(value);
5358 case SWM_S_TITLE_NAME_ENABLED:
5359 title_name_enabled = atoi(value);
5361 case SWM_S_URGENT_ENABLED:
5362 urgent_enabled = atoi(value);
5364 case SWM_S_FOCUS_MODE:
5365 if (!strcmp(value, "default"))
5366 focus_mode = SWM_FOCUS_DEFAULT;
5367 else if (!strcmp(value, "follow_cursor"))
5368 focus_mode = SWM_FOCUS_FOLLOW;
5369 else if (!strcmp(value, "synergy"))
5370 focus_mode = SWM_FOCUS_SYNERGY;
5372 errx(1, "focus_mode");
5374 case SWM_S_DISABLE_BORDER:
5375 disable_border = atoi(value);
5377 case SWM_S_BORDER_WIDTH:
5378 border_width = atoi(value);
5380 case SWM_S_BAR_FONT:
5382 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5383 err(1, "setconfvalue: asprintf: failed to allocate "
5384 "memory for bar_fonts.");
5388 case SWM_S_BAR_ACTION:
5390 if ((bar_argv[0] = strdup(value)) == NULL)
5391 err(1, "setconfvalue: bar_action");
5393 case SWM_S_SPAWN_TERM:
5394 free(spawn_term[0]);
5395 if ((spawn_term[0] = strdup(value)) == NULL)
5396 err(1, "setconfvalue: spawn_term");
5400 case SWM_S_DIALOG_RATIO:
5401 dialog_ratio = atof(value);
5402 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5405 case SWM_S_VERBOSE_LAYOUT:
5406 verbose_layout = atoi(value);
5407 for (i = 0; layouts[i].l_stack != NULL; i++) {
5409 layouts[i].l_string = fancy_stacker;
5411 layouts[i].l_string = plain_stacker;
5421 setconfmodkey(char *selector, char *value, int flags)
5423 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5424 update_modkey(Mod1Mask);
5425 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5426 update_modkey(Mod2Mask);
5427 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5428 update_modkey(Mod3Mask);
5429 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5430 update_modkey(Mod4Mask);
5437 setconfcolor(char *selector, char *value, int flags)
5439 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5444 setconfregion(char *selector, char *value, int flags)
5446 custom_region(value);
5451 setautorun(char *selector, char *value, int flags)
5461 if (getenv("SWM_STARTED"))
5465 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5466 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5468 if (ws_id < 0 || ws_id >= workspace_limit)
5469 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5472 * This is a little intricate
5474 * If the pid already exists we simply reuse it because it means it was
5475 * used before AND not claimed by manage_window. We get away with
5476 * altering it in the parent after INSERT because this can not be a race
5479 while ((ap = strsep(&sp, " \t")) != NULL) {
5482 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5484 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5485 err(1, "setautorun: realloc");
5486 a.argv[argc - 1] = ap;
5489 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5490 err(1, "setautorun: realloc");
5491 a.argv[argc] = NULL;
5493 if ((pid = fork()) == 0) {
5494 spawn(ws_id, &a, 1);
5503 p = calloc(1, sizeof *p);
5506 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5516 setlayout(char *selector, char *value, int flags)
5518 int ws_id, i, x, mg, ma, si, raise;
5519 int st = SWM_V_STACK;
5521 struct workspace *ws;
5523 if (getenv("SWM_STARTED"))
5527 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5528 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5529 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5530 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5533 if (ws_id < 0 || ws_id >= workspace_limit)
5534 errx(1, "layout: invalid workspace %d", ws_id + 1);
5536 if (!strcasecmp(s, "vertical"))
5538 else if (!strcasecmp(s, "horizontal"))
5540 else if (!strcasecmp(s, "fullscreen"))
5543 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5544 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5547 for (i = 0; i < ScreenCount(display); i++) {
5548 ws = (struct workspace *)&screens[i].ws;
5549 ws[ws_id].cur_layout = &layouts[st];
5551 ws[ws_id].always_raise = raise;
5552 if (st == SWM_MAX_STACK)
5556 for (x = 0; x < abs(mg); x++) {
5557 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5558 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5559 SWM_ARG_ID_MASTERSHRINK);
5563 for (x = 0; x < abs(ma); x++) {
5564 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5565 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5566 SWM_ARG_ID_MASTERDEL);
5570 for (x = 0; x < abs(si); x++) {
5571 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5572 si >= 0 ? SWM_ARG_ID_STACKINC :
5573 SWM_ARG_ID_STACKDEC);
5581 /* config options */
5582 struct config_option {
5584 int (*func)(char*, char*, int);
5587 struct config_option configopt[] = {
5588 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5589 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5590 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5591 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5592 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5593 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5594 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5595 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5596 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5597 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5598 { "keyboard_mapping", setkeymapping, 0 },
5599 { "bind", setconfbinding, 0 },
5600 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5601 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5602 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5603 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5604 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5605 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5606 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5607 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
5608 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5609 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5610 { "modkey", setconfmodkey, 0 },
5611 { "program", setconfspawn, 0 },
5612 { "quirk", setconfquirk, 0 },
5613 { "region", setconfregion, 0 },
5614 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5615 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5616 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5617 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5618 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5619 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5620 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5621 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5622 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5623 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5624 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5625 { "autorun", setautorun, 0 },
5626 { "layout", setlayout, 0 },
5631 conf_load(char *filename, int keymapping)
5634 char *line, *cp, *optsub, *optval;
5635 size_t linelen, lineno = 0;
5636 int wordlen, i, optind;
5637 struct config_option *opt;
5639 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5641 if (filename == NULL) {
5642 warnx("conf_load: no filename");
5645 if ((config = fopen(filename, "r")) == NULL) {
5646 warn("conf_load: fopen: %s", filename);
5650 while (!feof(config)) {
5651 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5654 err(1, "%s", filename);
5659 cp += strspn(cp, " \t\n"); /* eat whitespace */
5660 if (cp[0] == '\0') {
5665 /* get config option */
5666 wordlen = strcspn(cp, "=[ \t\n");
5668 warnx("%s: line %zd: no option found",
5673 for (i = 0; i < LENGTH(configopt); i++) {
5674 opt = &configopt[i];
5675 if (!strncasecmp(cp, opt->optname, wordlen) &&
5676 strlen(opt->optname) == wordlen) {
5682 warnx("%s: line %zd: unknown option %.*s",
5683 filename, lineno, wordlen, cp);
5686 if (keymapping && strcmp(opt->optname, "bind")) {
5687 warnx("%s: line %zd: invalid option %.*s",
5688 filename, lineno, wordlen, cp);
5692 cp += strspn(cp, " \t\n"); /* eat whitespace */
5693 /* get [selector] if any */
5697 wordlen = strcspn(cp, "]");
5700 warnx("%s: line %zd: syntax error",
5705 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5707 warnx("%s: line %zd: unable to allocate"
5708 "memory for selector", filename,
5714 cp += strspn(cp, "] \t\n"); /* eat trailing */
5716 cp += strspn(cp, "= \t\n"); /* eat trailing */
5718 optval = strdup(cp);
5719 /* call function to deal with it all */
5720 if (configopt[optind].func(optsub, optval,
5721 configopt[optind].funcflags) != 0)
5722 errx(1, "%s: line %zd: invalid data for %s",
5723 filename, lineno, configopt[optind].optname);
5730 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5737 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
5743 set_child_transient(struct ws_win *win, Window *trans)
5745 struct ws_win *parent, *w;
5746 XWMHints *wmh = NULL;
5747 struct swm_region *r;
5748 struct workspace *ws;
5750 parent = find_window(win->transient);
5752 parent->child_trans = win;
5754 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5755 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
5757 if (win->hints == NULL) {
5758 warnx("no hints for 0x%lx", win->id);
5762 r = root_to_region(win->wa.root);
5764 /* parent doen't exist in our window list */
5765 TAILQ_FOREACH(w, &ws->winlist, entry) {
5769 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5770 warnx("can't get hints for 0x%lx", w->id);
5774 if (win->hints->window_group != wmh->window_group)
5777 w->child_trans = win;
5778 win->transient = w->id;
5780 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5781 "transient to 0x%lx\n", win->transient);
5791 window_get_pid(Window win)
5793 Atom actual_type_return;
5794 int actual_format_return = 0;
5795 unsigned long nitems_return = 0;
5796 unsigned long bytes_after_return = 0;
5800 unsigned char *prop = NULL;
5802 if (XGetWindowProperty(display, win,
5803 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5804 XA_CARDINAL, &actual_type_return, &actual_format_return,
5805 &nitems_return, &bytes_after_return,
5806 (unsigned char**)(void*)&pid) != Success)
5808 if (actual_type_return != XA_CARDINAL)
5819 if (XGetWindowProperty(display, win,
5820 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5821 XA_STRING, &actual_type_return, &actual_format_return,
5822 &nitems_return, &bytes_after_return, &prop) != Success)
5824 if (actual_type_return != XA_STRING)
5829 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5830 /* ignore error because strtonum returns 0 anyway */
5837 manage_window(Window id)
5840 struct workspace *ws;
5841 struct ws_win *win, *ww;
5842 int format, i, ws_idx, n, border_me = 0;
5843 unsigned long nitems, bytes;
5844 Atom ws_idx_atom = 0, type;
5845 Atom *prot = NULL, *pp;
5846 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5847 struct swm_region *r;
5854 if ((win = find_window(id)) != NULL)
5855 return (win); /* already being managed */
5857 /* see if we are on the unmanaged list */
5858 if ((win = find_unmanaged_window(id)) != NULL) {
5859 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
5860 "window: 0x%lx\n", win->id);
5861 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5862 if (win->transient) {
5863 set_child_transient(win, &trans);
5864 } if (trans && (ww = find_window(trans)))
5865 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5867 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5868 ewmh_update_actions(win);
5872 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5873 err(1, "manage_window: calloc: failed to allocate memory for "
5878 /* see if we need to override the workspace */
5879 p = find_pid(window_get_pid(id));
5881 /* Get all the window data in one shot */
5882 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5884 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5885 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5887 XGetWindowAttributes(display, id, &win->wa);
5888 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
5889 win->hints = XGetWMHints(display, id);
5890 XGetTransientForHint(display, id, &trans);
5892 win->transient = trans;
5893 set_child_transient(win, &trans);
5894 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
5895 "transient: 0x%lx\n", win->id, win->transient);
5898 /* get supported protocols */
5899 if (XGetWMProtocols(display, id, &prot, &n)) {
5900 for (i = 0, pp = prot; i < n; i++, pp++) {
5901 if (*pp == takefocus)
5902 win->take_focus = 1;
5904 win->can_delete = 1;
5910 win->iconic = get_iconic(win);
5913 * Figure out where to put the window. If it was previously assigned to
5914 * a workspace (either by spawn() or manually moving), and isn't
5915 * transient, * put it in the same workspace
5917 r = root_to_region(win->wa.root);
5919 ws = &r->s->ws[p->ws];
5920 TAILQ_REMOVE(&pidlist, p, entry);
5923 } else if (prop && win->transient == 0) {
5924 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
5925 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
5928 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
5931 ws = &r->s->ws[ws_idx];
5934 /* this should launch transients in the same ws as parent */
5936 if ((ww = find_window(trans)) != NULL)
5942 warnx("manage_window: fix this "
5948 /* set up the window layout */
5951 win->s = r->s; /* this never changes */
5952 if (trans && (ww = find_window(trans)))
5953 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5955 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5957 WIDTH(win) = win->wa.width;
5958 HEIGHT(win) = win->wa.height;
5961 win->g_floatvalid = 0;
5962 win->floatmaxed = 0;
5963 win->ewmh_flags = 0;
5965 /* Set window properties so we can remember this after reincarnation */
5966 if (ws_idx_atom && prop == NULL &&
5967 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5969 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
5971 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5972 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
5977 ewmh_autoquirk(win);
5979 if (XGetClassHint(display, win->id, &win->ch)) {
5980 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
5981 win->ch.res_class, win->ch.res_name);
5983 /* java is retarded so treat it special */
5984 if (strstr(win->ch.res_name, "sun-awt")) {
5989 TAILQ_FOREACH(qp, &quirks, entry) {
5990 if (!strcmp(win->ch.res_class, qp->class) &&
5991 !strcmp(win->ch.res_name, qp->name)) {
5992 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
5993 "class: %s, name: %s\n", win->ch.res_class,
5995 if (qp->quirk & SWM_Q_FLOAT) {
5999 win->quirks = qp->quirk;
6004 /* alter window position if quirky */
6005 if (win->quirks & SWM_Q_ANYWHERE) {
6006 win->manual = 1; /* don't center the quirky windows */
6007 bzero(&wc, sizeof wc);
6009 if (bar_enabled && Y(win) < bar_height) {
6010 Y(win) = wc.y = bar_height;
6013 if (WIDTH(win) + X(win) > WIDTH(r)) {
6014 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
6020 /* Reset font sizes (the bruteforce way; no default keybinding). */
6021 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6022 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6023 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6024 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6025 fake_keypress(win, XK_KP_Add, ShiftMask);
6028 ewmh_get_win_state(win);
6029 ewmh_update_actions(win);
6030 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6034 bzero(&wc, sizeof wc);
6035 wc.border_width = border_width;
6036 mask |= CWBorderWidth;
6037 XConfigureWindow(display, win->id, mask, &wc);
6040 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
6041 PropertyChangeMask | StructureNotifyMask);
6043 /* floaters need to be mapped if they are in the current workspace */
6044 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6045 XMapRaised(display, win->id);
6051 free_window(struct ws_win *win)
6053 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
6058 /* needed for restart wm */
6059 set_win_state(win, WithdrawnState);
6061 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6063 if (win->ch.res_class)
6064 XFree(win->ch.res_class);
6065 if (win->ch.res_name)
6066 XFree(win->ch.res_name);
6071 memset(win, 0xff, sizeof *win); /* XXX kill later */
6077 unmanage_window(struct ws_win *win)
6079 struct ws_win *parent;
6084 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
6086 if (win->transient) {
6087 parent = find_window(win->transient);
6089 parent->child_trans = NULL;
6092 /* focus on root just in case */
6093 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6102 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6103 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6109 focus_magic(struct ws_win *win)
6111 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6114 /* if there are no windows clear the status-bar */
6119 if (win->child_trans) {
6120 /* win = parent & has a transient so focus on that */
6122 focus_win(win->child_trans);
6123 if (win->child_trans->take_focus)
6124 client_msg(win, takefocus);
6126 /* make sure transient hasn't disappeared */
6127 if (validate_win(win->child_trans) == 0) {
6128 focus_win(win->child_trans);
6129 if (win->child_trans->take_focus)
6130 client_msg(win->child_trans, takefocus);
6132 win->child_trans = NULL;
6134 if (win->take_focus)
6135 client_msg(win, takefocus);
6141 if (win->take_focus)
6142 client_msg(win, takefocus);
6149 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6156 XKeyEvent *ev = &e->xkey;
6158 struct swm_region *r;
6160 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6161 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6163 if (keyfuncs[kp->funcid].func == NULL)
6166 r = root_to_region(ev->root);
6167 if (kp->funcid == kf_spawn_custom)
6168 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6170 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6174 buttonpress(XEvent *e)
6178 XButtonPressedEvent *ev = &e->xbutton;
6180 if ((win = find_window(ev->window)) == NULL)
6184 action = client_click;
6186 for (i = 0; i < LENGTH(buttons); i++)
6187 if (action == buttons[i].action && buttons[i].func &&
6188 buttons[i].button == ev->button &&
6189 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6190 buttons[i].func(win, &buttons[i].args);
6194 configurerequest(XEvent *e)
6196 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6201 if ((win = find_window(ev->window)) == NULL)
6202 if ((win = find_unmanaged_window(ev->window)) == NULL)
6205 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6206 ev->window, YESNO(new));
6209 bzero(&wc, sizeof wc);
6212 wc.width = ev->width;
6213 wc.height = ev->height;
6214 wc.border_width = ev->border_width;
6215 wc.sibling = ev->above;
6216 wc.stack_mode = ev->detail;
6217 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6219 config_win(win, ev);
6224 configurenotify(XEvent *e)
6228 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6229 e->xconfigure.window);
6231 win = find_window(e->xconfigure.window);
6233 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6237 if (focus_mode == SWM_FOCUS_DEFAULT)
6238 drain_enter_notify();
6243 destroynotify(XEvent *e)
6246 XDestroyWindowEvent *ev = &e->xdestroywindow;
6248 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6250 if ((win = find_window(ev->window)) == NULL) {
6251 if ((win = find_unmanaged_window(ev->window)) == NULL)
6257 /* make sure we focus on something */
6260 unmanage_window(win);
6262 if (focus_mode == SWM_FOCUS_DEFAULT)
6263 drain_enter_notify();
6268 enternotify(XEvent *e)
6270 XCrossingEvent *ev = &e->xcrossing;
6275 Window focus_return;
6276 int revert_to_return;
6278 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6279 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6280 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6281 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6283 if (ev->mode != NotifyNormal) {
6284 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6289 switch (focus_mode) {
6290 case SWM_FOCUS_DEFAULT:
6292 case SWM_FOCUS_FOLLOW:
6294 case SWM_FOCUS_SYNERGY:
6297 * all these checks need to be in this order because the
6298 * XCheckTypedWindowEvent relies on weeding out the previous events
6300 * making this code an option would enable a follow mouse for focus
6305 * state is set when we are switching workspaces and focus is set when
6306 * the window or a subwindow already has focus (occurs during restart).
6308 * Only honor the focus flag if last_focus_event is not FocusOut,
6309 * this allows spectrwm to continue to control focus when another
6310 * program is also playing with it.
6312 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6313 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6318 * happens when a window is created or destroyed and the border
6319 * crosses the mouse pointer and when switching ws
6321 * we need the subwindow test to see if we came from root in order
6322 * to give focus to floaters
6324 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6325 ev->subwindow == 0) {
6326 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6330 /* this window already has focus */
6331 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6332 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6336 /* this window is being deleted or moved to another ws */
6337 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6339 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6340 XPutBackEvent(display, &cne);
6344 if ((win = find_window(ev->window)) == NULL) {
6345 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6350 * In fullstack kill all enters unless they come from a different ws
6351 * (i.e. another region) or focus has been grabbed externally.
6353 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6354 last_focus_event != FocusOut) {
6355 XGetInputFocus(display, &focus_return, &revert_to_return);
6356 if ((w = find_window(focus_return)) == NULL ||
6358 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6366 if ((win = find_window(ev->window)) == NULL) {
6367 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6372 * if we have more enternotifies let them handle it in due time
6374 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6375 DNPRINTF(SWM_D_EVENT,
6376 "ignoring enternotify: got more enternotify\n");
6377 XPutBackEvent(display, &cne);
6384 /* lets us use one switch statement for arbitrary mode/detail combinations */
6385 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6388 focusevent(XEvent *e)
6392 u_int32_t mode_detail;
6393 XFocusChangeEvent *ev = &e->xfocus;
6395 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6396 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6397 ev->window, ev->mode, ev->detail);
6399 if (last_focus_event == ev->type) {
6400 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6404 last_focus_event = ev->type;
6405 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6407 switch (mode_detail) {
6408 /* synergy client focus operations */
6409 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6410 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6412 /* synergy server focus operations */
6413 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6415 /* Entering applications like rdesktop that mangle the pointer */
6416 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6418 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6419 XSetWindowBorder(display, win->id,
6420 win->ws->r->s->c[ev->type == FocusIn ?
6421 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6424 warnx("ignoring focusevent");
6425 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6432 mapnotify(XEvent *e)
6435 XMapEvent *ev = &e->xmap;
6437 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6439 win = manage_window(ev->window);
6441 set_win_state(win, NormalState);
6445 mappingnotify(XEvent *e)
6447 XMappingEvent *ev = &e->xmapping;
6449 XRefreshKeyboardMapping(ev);
6450 if (ev->request == MappingKeyboard)
6455 maprequest(XEvent *e)
6458 struct swm_region *r;
6459 XWindowAttributes wa;
6460 XMapRequestEvent *ev = &e->xmaprequest;
6462 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6463 e->xmaprequest.window);
6465 if (!XGetWindowAttributes(display, ev->window, &wa))
6467 if (wa.override_redirect)
6470 win = manage_window(e->xmaprequest.window);
6472 return; /* can't happen */
6476 /* make new win focused */
6477 r = root_to_region(win->wa.root);
6478 if (win->ws == r->ws)
6483 propertynotify(XEvent *e)
6486 XPropertyEvent *ev = &e->xproperty;
6489 name = XGetAtomName(display, ev->atom);
6490 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6495 win = find_window(ev->window);
6499 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6500 update_iconic(win, 0);
6501 XMapRaised(display, win->id);
6509 case XA_WM_NORMAL_HINTS:
6511 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6512 warnx("normal hints: flag 0x%x", win->sh.flags);
6513 if (win->sh.flags & PMinSize) {
6514 WIDTH(win) = win->sh.min_width;
6515 HEIGHT(win) = win->sh.min_height;
6516 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6518 XMoveResizeWindow(display, win->id,
6519 X(win), Y(win), WIDTH(win), HEIGHT(win));
6522 if (title_name_enabled || title_class_enabled)
6526 if (window_name_enabled)
6535 unmapnotify(XEvent *e)
6539 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6541 /* determine if we need to help unmanage this window */
6542 win = find_window(e->xunmap.window);
6546 if (getstate(e->xunmap.window) == NormalState) {
6547 unmanage_window(win);
6550 /* giant hack for apps that don't destroy transient windows */
6551 /* eat a bunch of events to prevent remanaging the window */
6553 while (XCheckWindowEvent(display, e->xunmap.window,
6554 EnterWindowMask, &cne))
6556 while (XCheckWindowEvent(display, e->xunmap.window,
6557 StructureNotifyMask, &cne))
6559 while (XCheckWindowEvent(display, e->xunmap.window,
6560 SubstructureNotifyMask, &cne))
6562 /* resend unmap because we ated it */
6563 XUnmapWindow(display, e->xunmap.window);
6566 if (focus_mode == SWM_FOCUS_DEFAULT)
6567 drain_enter_notify();
6571 visibilitynotify(XEvent *e)
6574 struct swm_region *r;
6576 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6577 e->xvisibility.window);
6578 if (e->xvisibility.state == VisibilityUnobscured)
6579 for (i = 0; i < ScreenCount(display); i++)
6580 TAILQ_FOREACH(r, &screens[i].rl, entry)
6581 if (e->xvisibility.window == r->bar_window)
6586 clientmessage(XEvent *e)
6588 XClientMessageEvent *ev;
6593 win = find_window(ev->window);
6597 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6598 ev->window, ev->message_type);
6600 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6601 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6604 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6605 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6606 if (win->can_delete)
6607 client_msg(win, adelete);
6609 XKillClient(display, win->id);
6611 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6612 DNPRINTF(SWM_D_EVENT,
6613 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6614 if (win->floating) {
6615 if (ev->data.l[0] & (1<<8)) /* x */
6616 X(win) = ev->data.l[1];
6617 if (ev->data.l[0] & (1<<9)) /* y */
6618 Y(win) = ev->data.l[2];
6619 if (ev->data.l[0] & (1<<10)) /* width */
6620 WIDTH(win) = ev->data.l[3];
6621 if (ev->data.l[0] & (1<<11)) /* height */
6622 HEIGHT(win) = ev->data.l[4];
6627 /* TODO: Change stack sizes */
6628 /* notify no change was made. */
6629 config_win(win, NULL);
6632 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6633 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6634 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6636 ewmh_update_win_state(win, ev->data.l[2],
6644 xerror_start(Display *d, XErrorEvent *ee)
6651 xerror(Display *d, XErrorEvent *ee)
6653 /* warnx("error: %p %p", display, ee); */
6661 xerrorxlib = XSetErrorHandler(xerror_start);
6663 /* this causes an error if some other window manager is running */
6664 XSelectInput(display, DefaultRootWindow(display),
6665 SubstructureRedirectMask);
6666 XSync(display, False);
6670 XSetErrorHandler(xerror);
6671 XSync(display, False);
6676 new_region(struct swm_screen *s, int x, int y, int w, int h)
6678 struct swm_region *r, *n;
6679 struct workspace *ws = NULL;
6682 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6683 s->idx, w, h, x, y);
6685 /* remove any conflicting regions */
6686 n = TAILQ_FIRST(&s->rl);
6689 n = TAILQ_NEXT(r, entry);
6690 if (X(r) < (x + w) &&
6691 (X(r) + WIDTH(r)) > x &&
6693 (Y(r) + HEIGHT(r)) > y) {
6694 if (r->ws->r != NULL)
6695 r->ws->old_r = r->ws->r;
6697 XDestroyWindow(display, r->bar_window);
6698 TAILQ_REMOVE(&s->rl, r, entry);
6699 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6703 /* search old regions for one to reuse */
6705 /* size + location match */
6706 TAILQ_FOREACH(r, &s->orl, entry)
6707 if (X(r) == x && Y(r) == y &&
6708 HEIGHT(r) == h && WIDTH(r) == w)
6712 TAILQ_FOREACH(r, &s->orl, entry)
6713 if (HEIGHT(r) == h && WIDTH(r) == w)
6717 TAILQ_REMOVE(&s->orl, r, entry);
6718 /* try to use old region's workspace */
6719 if (r->ws->r == NULL)
6722 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6723 err(1, "new_region: calloc: failed to allocate memory "
6726 /* if we don't have a workspace already, find one */
6728 for (i = 0; i < workspace_limit; i++)
6729 if (s->ws[i].r == NULL) {
6736 errx(1, "new_region: no free workspaces");
6747 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6753 #ifdef SWM_XRR_HAS_CRTC
6755 XRRScreenResources *sr;
6758 #endif /* SWM_XRR_HAS_CRTC */
6759 struct swm_region *r;
6762 if (i >= ScreenCount(display))
6763 errx(1, "scan_xrandr: invalid screen");
6765 /* remove any old regions */
6766 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6767 r->ws->old_r = r->ws->r = NULL;
6768 XDestroyWindow(display, r->bar_window);
6769 TAILQ_REMOVE(&screens[i].rl, r, entry);
6770 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6774 /* map virtual screens onto physical screens */
6775 #ifdef SWM_XRR_HAS_CRTC
6776 if (xrandr_support) {
6777 sr = XRRGetScreenResources(display, screens[i].root);
6779 new_region(&screens[i], 0, 0,
6780 DisplayWidth(display, i),
6781 DisplayHeight(display, i));
6785 for (c = 0, ci = NULL; c < ncrtc; c++) {
6786 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6787 if (ci->noutput == 0)
6790 if (ci != NULL && ci->mode == None)
6791 new_region(&screens[i], 0, 0,
6792 DisplayWidth(display, i),
6793 DisplayHeight(display, i));
6795 new_region(&screens[i],
6796 ci->x, ci->y, ci->width, ci->height);
6799 XRRFreeCrtcInfo(ci);
6800 XRRFreeScreenResources(sr);
6802 #endif /* SWM_XRR_HAS_CRTC */
6804 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6805 DisplayHeight(display, i));
6810 screenchange(XEvent *e) {
6811 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6812 struct swm_region *r;
6815 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
6817 if (!XRRUpdateConfiguration(e))
6820 /* silly event doesn't include the screen index */
6821 for (i = 0; i < ScreenCount(display); i++)
6822 if (screens[i].root == xe->root)
6824 if (i >= ScreenCount(display))
6825 errx(1, "screenchange: screen not found");
6827 /* brute force for now, just re-enumerate the regions */
6830 /* add bars to all regions */
6831 for (i = 0; i < ScreenCount(display); i++)
6832 TAILQ_FOREACH(r, &screens[i].rl, entry)
6835 if (focus_mode == SWM_FOCUS_DEFAULT)
6836 drain_enter_notify();
6842 Window d1, d2, *wins = NULL;
6843 XWindowAttributes wa;
6848 for (i = 0; i < ScreenCount(display); i++) {
6849 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6852 /* attach windows to a region */
6853 /* normal windows */
6854 for (j = 0; j < no; j++) {
6855 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6856 wa.override_redirect ||
6857 XGetTransientForHint(display, wins[j], &d1))
6860 state = getstate(wins[j]);
6861 manage = state == IconicState;
6862 if (wa.map_state == IsViewable || manage)
6863 manage_window(wins[j]);
6865 /* transient windows */
6866 for (j = 0; j < no; j++) {
6867 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6868 wa.override_redirect)
6871 state = getstate(wins[j]);
6872 manage = state == IconicState;
6873 if (XGetTransientForHint(display, wins[j], &d1) &&
6875 manage_window(wins[j]);
6888 int errorbase, major, minor;
6889 struct workspace *ws;
6892 if ((screens = calloc(ScreenCount(display),
6893 sizeof(struct swm_screen))) == NULL)
6894 err(1, "setup_screens: calloc: failed to allocate memory for "
6897 /* initial Xrandr setup */
6898 xrandr_support = XRRQueryExtension(display,
6899 &xrandr_eventbase, &errorbase);
6901 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6904 /* map physical screens */
6905 for (i = 0; i < ScreenCount(display); i++) {
6906 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
6908 TAILQ_INIT(&screens[i].rl);
6909 TAILQ_INIT(&screens[i].orl);
6910 screens[i].root = RootWindow(display, i);
6912 /* set default colors */
6913 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6914 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6915 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6916 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6917 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6919 /* create graphics context on screen with default font color */
6920 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
6923 XSetForeground(display, screens[i].bar_gc,
6924 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
6926 /* set default cursor */
6927 XDefineCursor(display, screens[i].root,
6928 XCreateFontCursor(display, XC_left_ptr));
6930 /* init all workspaces */
6931 /* XXX these should be dynamically allocated too */
6932 for (j = 0; j < SWM_WS_MAX; j++) {
6933 ws = &screens[i].ws[j];
6939 TAILQ_INIT(&ws->winlist);
6940 TAILQ_INIT(&ws->unmanagedlist);
6942 for (k = 0; layouts[k].l_stack != NULL; k++)
6943 if (layouts[k].l_config != NULL)
6944 layouts[k].l_config(ws,
6945 SWM_ARG_ID_STACKINIT);
6946 ws->cur_layout = &layouts[0];
6947 ws->cur_layout->l_string(ws);
6953 XRRSelectInput(display, screens[i].root,
6954 RRScreenChangeNotifyMask);
6961 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
6962 err(1, "setup_globals: strdup: failed to allocate memory.");
6964 if ((spawn_term[0] = strdup("xterm")) == NULL)
6965 err(1, "setup_globals: strdup: failed to allocate memory.");
6967 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6968 err(1, "setup_globals: strdup: failed to allocate memory.");
6975 Atom netwmcheck, netwmname, utf8_string;
6978 /* work around sun jdk bugs, code from wmname */
6979 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6980 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6981 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6982 for (i = 0; i < ScreenCount(display); i++) {
6983 root = screens[i].root;
6984 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6985 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6986 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6988 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6989 PropModeReplace, (unsigned char *)&win,1);
6990 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6991 PropModeReplace, (unsigned char *)&win,1);
6992 XChangeProperty(display, win, netwmname, utf8_string, 8,
6993 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6998 main(int argc, char *argv[])
7000 struct swm_region *r, *rr;
7001 struct ws_win *winfocus = NULL;
7004 char conf[PATH_MAX], *cfile = NULL;
7009 struct sigaction sact;
7012 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7013 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7015 warnx("no locale support");
7017 if (!X_HAVE_UTF8_STRING)
7018 warnx("no UTF-8 support");
7020 if (!(display = XOpenDisplay(0)))
7021 errx(1, "can not open display");
7024 errx(1, "other wm running");
7026 /* handle some signals */
7027 bzero(&sact, sizeof(sact));
7028 sigemptyset(&sact.sa_mask);
7030 sact.sa_handler = sighdlr;
7031 sigaction(SIGINT, &sact, NULL);
7032 sigaction(SIGQUIT, &sact, NULL);
7033 sigaction(SIGTERM, &sact, NULL);
7034 sigaction(SIGHUP, &sact, NULL);
7036 sact.sa_handler = sighdlr;
7037 sact.sa_flags = SA_NOCLDSTOP;
7038 sigaction(SIGCHLD, &sact, NULL);
7040 astate = XInternAtom(display, "WM_STATE", False);
7041 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
7042 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
7043 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
7044 a_wmname = XInternAtom(display, "WM_NAME", False);
7045 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7046 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
7047 a_string = XInternAtom(display, "STRING", False);
7048 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
7050 /* look for local and global conf file */
7051 pwd = getpwuid(getuid());
7053 errx(1, "invalid user: %d", getuid());
7062 for (i = 0; ; i++) {
7067 snprintf(conf, sizeof conf, "%s/.%s",
7068 pwd->pw_dir, SWM_CONF_FILE);
7072 snprintf(conf, sizeof conf, "/etc/%s",
7077 snprintf(conf, sizeof conf, "%s/.%s",
7078 pwd->pw_dir, SWM_CONF_FILE_OLD);
7082 snprintf(conf, sizeof conf, "/etc/%s",
7089 if (strlen(conf) && stat(conf, &sb) != -1)
7090 if (S_ISREG(sb.st_mode)) {
7097 /* load conf (if any) and refresh font color in bar graphics contexts */
7098 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
7099 for (i = 0; i < ScreenCount(display); ++i)
7100 XSetForeground(display, screens[i].bar_gc,
7101 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7104 /* set some values to work around bad programs */
7106 /* grab existing windows (before we build the bars) */
7109 if (getenv("SWM_STARTED") == NULL)
7110 setenv("SWM_STARTED", "YES", 1);
7112 /* setup all bars */
7113 for (i = 0; i < ScreenCount(display); i++)
7114 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7115 if (winfocus == NULL)
7116 winfocus = TAILQ_FIRST(&r->ws->winlist);
7124 if (focus_mode == SWM_FOCUS_DEFAULT)
7125 drain_enter_notify();
7127 xfd = ConnectionNumber(display);
7129 while (XPending(display)) {
7130 XNextEvent(display, &e);
7133 if (e.type < LASTEvent) {
7134 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7135 "window: 0x%lx, type: %s (%d), %d remaining"
7136 "\n", YESNO(handler[e.type]),
7137 e.xany.window, geteventname(&e),
7138 e.type, QLength(display));
7140 if (handler[e.type])
7141 handler[e.type](&e);
7143 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7144 "0x%lx, type: %s (%d)\n", e.xany.window,
7145 xrandr_geteventname(&e), e.type);
7147 switch (e.type - xrandr_eventbase) {
7148 case RRScreenChangeNotify:
7157 /* if we are being restarted go focus on first window */
7159 rr = winfocus->ws->r;
7161 /* not a visible window */
7165 /* move pointer to first screen if multi screen */
7166 if (ScreenCount(display) > 1 || outputs > 1)
7167 XWarpPointer(display, None, rr->s[0].root,
7169 Y(rr) + (bar_enabled ? bar_height : 0));
7171 a.id = SWM_ARG_ID_FOCUSCUR;
7181 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7183 DNPRINTF(SWM_D_MISC, "select failed");
7184 if (restart_wm == 1)
7185 restart(NULL, NULL);
7186 if (search_resp == 1)
7199 for (i = 0; i < ScreenCount(display); ++i)
7200 if (screens[i].bar_gc != NULL)
7201 XFreeGC(display, screens[i].bar_gc);
7203 XFreeFontSet(display, bar_fs);
7204 XCloseDisplay(display);