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>
93 #include <X11/Xlib-xcb.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 BORDER(w) (w->bordered ? border_width : 0)
177 #define MAX_X(r) ((r)->g.x + (r)->g.w)
178 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
179 #define SH_MIN(w) (w)->sh_mask & PMinSize
180 #define SH_MIN_W(w) (w)->sh.min_width
181 #define SH_MIN_H(w) (w)->sh.min_height
182 #define SH_MAX(w) (w)->sh_mask & PMaxSize
183 #define SH_MAX_W(w) (w)->sh.max_width
184 #define SH_MAX_H(w) (w)->sh.max_height
185 #define SH_INC(w) (w)->sh_mask & PResizeInc
186 #define SH_INC_W(w) (w)->sh.width_inc
187 #define SH_INC_H(w) (w)->sh.height_inc
188 #define SWM_MAX_FONT_STEPS (3)
189 #define WINID(w) ((w) ? (w)->id : 0)
190 #define YESNO(x) ((x) ? "yes" : "no")
192 #define SWM_FOCUS_DEFAULT (0)
193 #define SWM_FOCUS_SYNERGY (1)
194 #define SWM_FOCUS_FOLLOW (2)
196 #define SWM_CONF_DEFAULT (0)
197 #define SWM_CONF_KEYMAPPING (1)
200 #define SWM_LIB "/usr/local/lib/libswmhack.so"
213 volatile sig_atomic_t running = 1;
214 volatile sig_atomic_t restart_wm = 0;
216 int last_focus_event = FocusOut;
217 int (*xerrorxlib)(Display *, XErrorEvent *);
221 int xrandr_eventbase;
222 unsigned int numlockmask = 0;
224 xcb_connection_t *conn;
227 int cycle_visible = 0;
229 int font_adjusted = 0;
230 unsigned int mod_key = MODKEY;
233 struct swm_region *search_r;
234 int select_list_pipe[2];
235 int select_resp_pipe[2];
237 volatile sig_atomic_t search_resp;
238 int search_resp_action;
240 struct search_window {
241 TAILQ_ENTRY(search_window) entry;
247 TAILQ_HEAD(search_winlist, search_window);
248 struct search_winlist search_wl;
253 SWM_SEARCH_UNICONIFY,
254 SWM_SEARCH_NAME_WORKSPACE,
255 SWM_SEARCH_SEARCH_WORKSPACE,
256 SWM_SEARCH_SEARCH_WINDOW
259 #define SWM_STACK_TOP (0)
260 #define SWM_STACK_BOTTOM (1)
261 #define SWM_STACK_ABOVE (2)
262 #define SWM_STACK_BELOW (3)
265 double dialog_ratio = 0.6;
267 #define SWM_BAR_MAX (256)
268 #define SWM_BAR_JUSTIFY_LEFT (0)
269 #define SWM_BAR_JUSTIFY_CENTER (1)
270 #define SWM_BAR_JUSTIFY_RIGHT (2)
271 #define SWM_BAR_OFFSET (4)
272 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
273 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
274 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
275 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
276 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
278 #ifdef X_HAVE_UTF8_STRING
279 #define DRAWSTRING(x...) Xutf8DrawString(x)
281 #define DRAWSTRING(x...) XmbDrawString(x)
284 char *bar_argv[] = { NULL, NULL };
286 unsigned char bar_ext[SWM_BAR_MAX];
287 char bar_vertext[SWM_BAR_MAX];
289 sig_atomic_t bar_alarm = 0;
292 int bar_border_width = 1;
293 int bar_at_bottom = 0;
295 int bar_extra_running = 0;
298 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
299 char *bar_format = NULL;
300 int stack_enabled = 1;
301 int clock_enabled = 1;
302 int urgent_enabled = 0;
303 char *clock_format = NULL;
304 int title_name_enabled = 0;
305 int title_class_enabled = 0;
306 int window_name_enabled = 0;
307 int focus_mode = SWM_FOCUS_DEFAULT;
308 int focus_close = SWM_STACK_BELOW;
309 int focus_close_wrap = 1;
310 int focus_default = SWM_STACK_TOP;
311 int spawn_position = SWM_STACK_TOP;
312 int disable_border = 0;
313 int border_width = 1;
314 int verbose_layout = 0;
317 XFontSetExtents *bar_fs_extents;
321 #define SWM_MENU_FN (2)
322 #define SWM_MENU_NB (4)
323 #define SWM_MENU_NF (6)
324 #define SWM_MENU_SB (8)
325 #define SWM_MENU_SF (10)
327 /* layout manager data */
328 struct swm_geometry {
341 struct swm_geometry g;
344 /* virtual "screens" */
346 TAILQ_ENTRY(swm_region) entry;
347 struct swm_geometry g;
348 struct workspace *ws; /* current workspace on this region */
349 struct workspace *ws_prior; /* prior workspace on this region */
350 struct swm_screen *s; /* screen idx */
353 TAILQ_HEAD(swm_region_list, swm_region);
356 TAILQ_ENTRY(ws_win) entry;
359 struct ws_win *child_trans; /* transient child window */
360 struct swm_geometry g; /* current geometry */
361 struct swm_geometry g_float; /* region coordinates */
362 int g_floatvalid; /* g_float geometry validity */
363 int floatmaxed; /* whether maxed by max_stack */
368 unsigned int ewmh_flags;
369 int font_size_boundary[SWM_MAX_FONT_STEPS];
375 unsigned long quirks;
376 struct workspace *ws; /* always valid */
377 struct swm_screen *s; /* always valid, never changes */
378 XWindowAttributes wa;
384 TAILQ_HEAD(ws_win_list, ws_win);
388 TAILQ_ENTRY(pid_e) entry;
392 TAILQ_HEAD(pid_list, pid_e);
393 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
395 /* layout handlers */
397 void vertical_config(struct workspace *, int);
398 void vertical_stack(struct workspace *, struct swm_geometry *);
399 void horizontal_config(struct workspace *, int);
400 void horizontal_stack(struct workspace *, struct swm_geometry *);
401 void max_stack(struct workspace *, struct swm_geometry *);
402 void plain_stacker(struct workspace *);
403 void fancy_stacker(struct workspace *);
405 struct ws_win *find_window(Window);
407 void grabbuttons(struct ws_win *, int);
408 void new_region(struct swm_screen *, int, int, int, int);
409 void unmanage_window(struct ws_win *);
410 long getstate(Window);
412 int conf_load(char *, int);
415 void (*l_stack)(struct workspace *, struct swm_geometry *);
416 void (*l_config)(struct workspace *, int);
418 #define SWM_L_FOCUSPREV (1<<0)
419 #define SWM_L_MAPONFOCUS (1<<1)
420 void (*l_string)(struct workspace *);
422 /* stack, configure */
423 { vertical_stack, vertical_config, 0, plain_stacker },
424 { horizontal_stack, horizontal_config, 0, plain_stacker },
426 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
427 { NULL, NULL, 0, NULL },
430 /* position of max_stack mode in the layouts array, index into layouts! */
431 #define SWM_V_STACK (0)
432 #define SWM_H_STACK (1)
433 #define SWM_MAX_STACK (2)
435 #define SWM_H_SLICE (32)
436 #define SWM_V_SLICE (32)
438 /* define work spaces */
440 int idx; /* workspace index */
441 char *name; /* workspace name */
442 int always_raise; /* raise windows on focus */
443 struct layout *cur_layout; /* current layout handlers */
444 struct ws_win *focus; /* may be NULL */
445 struct ws_win *focus_prev; /* may be NULL */
446 struct swm_region *r; /* may be NULL */
447 struct swm_region *old_r; /* may be NULL */
448 struct ws_win_list winlist; /* list of windows in ws */
449 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
450 char stacker[10]; /* display stacker and layout */
454 int horizontal_msize;
456 int horizontal_stacks;
467 SWM_S_COLOR_BAR_BORDER,
468 SWM_S_COLOR_BAR_FONT,
474 /* physical screen mapping */
475 #define SWM_WS_MAX (22) /* hard limit */
476 int workspace_limit = 10; /* soft limit */
479 int idx; /* screen index */
480 struct swm_region_list rl; /* list of regions on this screen */
481 struct swm_region_list orl; /* list of old regions */
483 struct workspace ws[SWM_WS_MAX];
489 } c[SWM_S_COLOR_MAX];
493 struct swm_screen *screens;
495 /* args to functions */
498 #define SWM_ARG_ID_FOCUSNEXT (0)
499 #define SWM_ARG_ID_FOCUSPREV (1)
500 #define SWM_ARG_ID_FOCUSMAIN (2)
501 #define SWM_ARG_ID_FOCUSCUR (4)
502 #define SWM_ARG_ID_SWAPNEXT (10)
503 #define SWM_ARG_ID_SWAPPREV (11)
504 #define SWM_ARG_ID_SWAPMAIN (12)
505 #define SWM_ARG_ID_MOVELAST (13)
506 #define SWM_ARG_ID_MASTERSHRINK (20)
507 #define SWM_ARG_ID_MASTERGROW (21)
508 #define SWM_ARG_ID_MASTERADD (22)
509 #define SWM_ARG_ID_MASTERDEL (23)
510 #define SWM_ARG_ID_FLIPLAYOUT (24)
511 #define SWM_ARG_ID_STACKRESET (30)
512 #define SWM_ARG_ID_STACKINIT (31)
513 #define SWM_ARG_ID_CYCLEWS_UP (40)
514 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
515 #define SWM_ARG_ID_CYCLESC_UP (42)
516 #define SWM_ARG_ID_CYCLESC_DOWN (43)
517 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
518 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
519 #define SWM_ARG_ID_STACKINC (50)
520 #define SWM_ARG_ID_STACKDEC (51)
521 #define SWM_ARG_ID_SS_ALL (60)
522 #define SWM_ARG_ID_SS_WINDOW (61)
523 #define SWM_ARG_ID_DONTCENTER (70)
524 #define SWM_ARG_ID_CENTER (71)
525 #define SWM_ARG_ID_KILLWINDOW (80)
526 #define SWM_ARG_ID_DELETEWINDOW (81)
527 #define SWM_ARG_ID_WIDTHGROW (90)
528 #define SWM_ARG_ID_WIDTHSHRINK (91)
529 #define SWM_ARG_ID_HEIGHTGROW (92)
530 #define SWM_ARG_ID_HEIGHTSHRINK (93)
531 #define SWM_ARG_ID_MOVEUP (100)
532 #define SWM_ARG_ID_MOVEDOWN (101)
533 #define SWM_ARG_ID_MOVELEFT (102)
534 #define SWM_ARG_ID_MOVERIGHT (103)
538 void focus(struct swm_region *, union arg *);
539 void focus_magic(struct ws_win *);
543 TAILQ_ENTRY(quirk) entry;
547 #define SWM_Q_FLOAT (1<<0) /* float this window */
548 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
549 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
550 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
551 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
552 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
554 TAILQ_HEAD(quirk_list, quirk);
555 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
558 * Supported EWMH hints should be added to
559 * both the enum and the ewmh array
564 _NET_MOVERESIZE_WINDOW,
565 _NET_WM_ACTION_CLOSE,
566 _NET_WM_ACTION_FULLSCREEN,
568 _NET_WM_ACTION_RESIZE,
569 _NET_WM_ALLOWED_ACTIONS,
572 _NET_WM_STATE_FULLSCREEN,
573 _NET_WM_STATE_HIDDEN,
574 _NET_WM_STATE_MAXIMIZED_HORZ,
575 _NET_WM_STATE_MAXIMIZED_VERT,
576 _NET_WM_STATE_SKIP_PAGER,
577 _NET_WM_STATE_SKIP_TASKBAR,
579 _NET_WM_WINDOW_TYPE_DIALOG,
580 _NET_WM_WINDOW_TYPE_DOCK,
581 _NET_WM_WINDOW_TYPE_NORMAL,
582 _NET_WM_WINDOW_TYPE_SPLASH,
583 _NET_WM_WINDOW_TYPE_TOOLBAR,
584 _NET_WM_WINDOW_TYPE_UTILITY,
585 _SWM_WM_STATE_MANUAL,
592 } ewmh[SWM_EWMH_HINT_MAX] = {
593 /* must be in same order as in the enum */
594 {"_NET_ACTIVE_WINDOW", None},
595 {"_NET_CLOSE_WINDOW", None},
596 {"_NET_MOVERESIZE_WINDOW", None},
597 {"_NET_WM_ACTION_CLOSE", None},
598 {"_NET_WM_ACTION_FULLSCREEN", None},
599 {"_NET_WM_ACTION_MOVE", None},
600 {"_NET_WM_ACTION_RESIZE", None},
601 {"_NET_WM_ALLOWED_ACTIONS", None},
602 {"_NET_WM_STATE", None},
603 {"_NET_WM_STATE_ABOVE", None},
604 {"_NET_WM_STATE_FULLSCREEN", None},
605 {"_NET_WM_STATE_HIDDEN", None},
606 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
607 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
608 {"_NET_WM_STATE_SKIP_PAGER", None},
609 {"_NET_WM_STATE_SKIP_TASKBAR", None},
610 {"_NET_WM_WINDOW_TYPE", None},
611 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
612 {"_NET_WM_WINDOW_TYPE_DOCK", None},
613 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
614 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
615 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
616 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
617 {"_SWM_WM_STATE_MANUAL", None},
620 void store_float_geom(struct ws_win *, struct swm_region *);
621 int floating_toggle_win(struct ws_win *);
622 void constrain_window(struct ws_win *, struct swm_region *, int);
623 void update_window(struct ws_win *);
624 void spawn_select(struct swm_region *, union arg *, char *, int *);
625 unsigned char *get_win_name(Window);
628 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
629 unsigned long *nbytes, unsigned char **data)
632 unsigned long *nbytes_ret, *nitems_ret;
633 unsigned long nbytes_tmp, nitems_tmp;
636 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
637 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
639 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
640 &real, &format, nitems_ret, nbytes_ret, data);
642 if (status != Success)
651 update_iconic(struct ws_win *win, int newv)
655 xcb_intern_atom_cookie_t c;
656 xcb_intern_atom_reply_t *r;
660 c = xcb_intern_atom(conn, False, strlen("_SWM_ICONIC"), "_SWM_ICONIC");
661 r = xcb_intern_atom_reply(conn, c, NULL);
669 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
670 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
672 xcb_delete_property(conn, win->id, iprop);
676 get_iconic(struct ws_win *win)
678 int32_t v = 0, *vtmp;
680 xcb_intern_atom_cookie_t c;
681 xcb_intern_atom_reply_t *r;
682 xcb_get_property_cookie_t pc;
683 xcb_get_property_reply_t *pr;
685 c = xcb_intern_atom(conn, False, strlen("_SWM_ICONIC"), "_SWM_ICONIC");
686 r = xcb_intern_atom_reply(conn, c, NULL);
693 pc = xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
695 pr = xcb_get_property_reply(conn, pc, NULL);
698 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
700 vtmp = xcb_get_property_value(pr);
712 xcb_intern_atom_cookie_t c;
713 xcb_intern_atom_reply_t *r;
714 int i, j, num_screens;
716 c = xcb_intern_atom(conn, False, strlen("_NET_SUPPORTED"),
718 r = xcb_intern_atom_reply(conn, c, NULL);
724 for (i = 0; i < LENGTH(ewmh); i++) {
725 c = xcb_intern_atom(conn, False, strlen(ewmh[i].name),
727 r = xcb_intern_atom_reply(conn, c, NULL);
729 ewmh[i].atom = r->atom;
734 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
735 for (i = 0; i < num_screens; i++) {
736 /* Support check window will be created by workaround(). */
738 /* Report supported atoms */
739 xcb_delete_property(conn, screens[i].root, sup_list);
740 for (j = 0; j < LENGTH(ewmh); j++)
741 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
742 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
751 unsigned char *data = NULL;
753 Atom sup_check, sup_list;
756 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
757 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
759 for (i = 0; i < ScreenCount(display); i++) {
760 /* Get the support check window and destroy it */
761 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
766 XDestroyWindow(display, id);
767 XDeleteProperty(display, screens[i].root, sup_check);
768 XDeleteProperty(display, screens[i].root, sup_list);
776 ewmh_autoquirk(struct ws_win *win)
779 unsigned long *data = NULL, n;
782 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
783 XA_ATOM, &n, NULL, (void *)&data);
790 for (i = 0; i < n; i++) {
792 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
794 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
795 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
796 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
798 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
801 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
802 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
804 win->quirks = SWM_Q_FLOAT;
812 #define SWM_EWMH_ACTION_COUNT_MAX (6)
813 #define EWMH_F_FULLSCREEN (1<<0)
814 #define EWMH_F_ABOVE (1<<1)
815 #define EWMH_F_HIDDEN (1<<2)
816 #define EWMH_F_SKIP_PAGER (1<<3)
817 #define EWMH_F_SKIP_TASKBAR (1<<4)
818 #define SWM_F_MANUAL (1<<5)
821 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
829 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
830 "fullscreen %s\n", win->id, YESNO(fs));
833 if (!win->g_floatvalid)
834 store_float_geom(win, win->ws->r);
836 win->g = win->ws->r->g;
839 if (win->g_floatvalid) {
840 /* refloat at last floating relative position */
841 win->g = win->g_float;
842 X(win) += X(win->ws->r);
843 Y(win) += Y(win->ws->r);
851 ewmh_update_actions(struct ws_win *win)
853 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
859 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
862 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
863 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
866 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
867 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
870 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
871 #define _NET_WM_STATE_ADD 1 /* add/set property */
872 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
875 ewmh_update_win_state(struct ws_win *win, long state, long action)
877 unsigned int mask = 0;
878 unsigned int changed = 0;
879 unsigned int orig_flags;
884 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
885 mask = EWMH_F_FULLSCREEN;
886 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
888 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
890 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
891 mask = EWMH_F_SKIP_PAGER;
892 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
893 mask = EWMH_F_SKIP_TASKBAR;
896 orig_flags = win->ewmh_flags;
899 case _NET_WM_STATE_REMOVE:
900 win->ewmh_flags &= ~mask;
902 case _NET_WM_STATE_ADD:
903 win->ewmh_flags |= mask;
905 case _NET_WM_STATE_TOGGLE:
906 win->ewmh_flags ^= mask;
910 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
912 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
914 if (!floating_toggle_win(win))
915 win->ewmh_flags = orig_flags; /* revert */
916 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
918 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
919 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
921 if (!ewmh_set_win_fullscreen(win,
922 win->ewmh_flags & EWMH_F_FULLSCREEN))
923 win->ewmh_flags = orig_flags; /* revert */
925 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
927 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
928 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
929 XA_ATOM, 32, PropModeAppend,
930 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
931 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
932 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
933 XA_ATOM, 32, PropModeAppend,
934 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
935 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
936 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
937 XA_ATOM, 32, PropModeAppend,
938 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
939 if (win->ewmh_flags & EWMH_F_ABOVE)
940 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
941 XA_ATOM, 32, PropModeAppend,
942 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
943 if (win->ewmh_flags & SWM_F_MANUAL)
944 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
945 XA_ATOM, 32, PropModeAppend,
946 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
950 ewmh_get_win_state(struct ws_win *win)
961 win->ewmh_flags |= EWMH_F_ABOVE;
963 win->ewmh_flags |= SWM_F_MANUAL;
965 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
966 (~0L), XA_ATOM, &n, NULL, (void *)&states);
971 for (i = 0; i < n; i++)
972 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
980 geteventname(XEvent *e)
992 name = "ButtonPress";
995 name = "ButtonRelease";
998 name = "MotionNotify";
1001 name = "EnterNotify";
1004 name = "LeaveNotify";
1013 name = "KeymapNotify";
1018 case GraphicsExpose:
1019 name = "GraphicsExpose";
1024 case VisibilityNotify:
1025 name = "VisibilityNotify";
1028 name = "CreateNotify";
1031 name = "DestroyNotify";
1034 name = "UnmapNotify";
1040 name = "MapRequest";
1042 case ReparentNotify:
1043 name = "ReparentNotify";
1045 case ConfigureNotify:
1046 name = "ConfigureNotify";
1048 case ConfigureRequest:
1049 name = "ConfigureRequest";
1052 name = "GravityNotify";
1055 name = "ResizeRequest";
1057 case CirculateNotify:
1058 name = "CirculateNotify";
1060 case CirculateRequest:
1061 name = "CirculateRequest";
1063 case PropertyNotify:
1064 name = "PropertyNotify";
1066 case SelectionClear:
1067 name = "SelectionClear";
1069 case SelectionRequest:
1070 name = "SelectionRequest";
1072 case SelectionNotify:
1073 name = "SelectionNotify";
1075 case ColormapNotify:
1076 name = "ColormapNotify";
1079 name = "ClientMessage";
1082 name = "MappingNotify";
1092 xrandr_geteventname(XEvent *e)
1096 switch(e->type - xrandr_eventbase) {
1097 case RRScreenChangeNotify:
1098 name = "RRScreenChangeNotify";
1108 dumpwins(struct swm_region *r, union arg *args)
1112 XWindowAttributes wa;
1114 if (r->ws == NULL) {
1115 warnx("dumpwins: invalid workspace");
1119 warnx("=== managed window list ws %02d ===", r->ws->idx);
1121 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1122 state = getstate(win->id);
1123 if (!XGetWindowAttributes(display, win->id, &wa))
1124 warnx("window: 0x%lx, failed XGetWindowAttributes",
1126 warnx("window: 0x%lx, map_state: %d, state: %d, "
1127 "transient: 0x%lx", win->id, wa.map_state, state,
1131 warnx("===== unmanaged window list =====");
1132 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1133 state = getstate(win->id);
1134 if (!XGetWindowAttributes(display, win->id, &wa))
1135 warnx("window: 0x%lx, failed XGetWindowAttributes",
1137 warnx("window: 0x%lx, map_state: %d, state: %d, "
1138 "transient: 0x%lx", win->id, wa.map_state, state,
1142 warnx("=================================");
1146 dumpwins(struct swm_region *r, union arg *args)
1149 #endif /* SWM_DEBUG */
1151 void expose(XEvent *);
1152 void keypress(XEvent *);
1153 void buttonpress(XEvent *);
1154 void configurerequest(XEvent *);
1155 void configurenotify(XEvent *);
1156 void destroynotify(XEvent *);
1157 void enternotify(XEvent *);
1158 void focusevent(XEvent *);
1159 void mapnotify(XEvent *);
1160 void mappingnotify(XEvent *);
1161 void maprequest(XEvent *);
1162 void propertynotify(XEvent *);
1163 void unmapnotify(XEvent *);
1164 void visibilitynotify(XEvent *);
1165 void clientmessage(XEvent *);
1167 void (*handler[LASTEvent])(XEvent *) = {
1169 [KeyPress] = keypress,
1170 [ButtonPress] = buttonpress,
1171 [ConfigureRequest] = configurerequest,
1172 [ConfigureNotify] = configurenotify,
1173 [DestroyNotify] = destroynotify,
1174 [EnterNotify] = enternotify,
1175 [FocusIn] = focusevent,
1176 [FocusOut] = focusevent,
1177 [MapNotify] = mapnotify,
1178 [MappingNotify] = mappingnotify,
1179 [MapRequest] = maprequest,
1180 [PropertyNotify] = propertynotify,
1181 [UnmapNotify] = unmapnotify,
1182 [VisibilityNotify] = visibilitynotify,
1183 [ClientMessage] = clientmessage,
1189 int saved_errno, status;
1192 saved_errno = errno;
1196 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1201 if (errno != ECHILD)
1202 warn("sighdlr: waitpid");
1203 #endif /* SWM_DEBUG */
1206 if (pid == searchpid)
1210 if (WIFEXITED(status)) {
1211 if (WEXITSTATUS(status) != 0)
1212 warnx("sighdlr: child exit status: %d",
1213 WEXITSTATUS(status));
1215 warnx("sighdlr: child is terminated "
1217 #endif /* SWM_DEBUG */
1231 errno = saved_errno;
1237 struct pid_e *p = NULL;
1239 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1244 TAILQ_FOREACH(p, &pidlist, entry) {
1253 name_to_color(char *colorname)
1257 XColor screen_def, exact_def;
1258 unsigned long result = 0;
1259 char cname[32] = "#";
1261 cmap = DefaultColormap(display, screens[0].idx);
1262 status = XAllocNamedColor(display, cmap, colorname,
1263 &screen_def, &exact_def);
1265 strlcat(cname, colorname + 2, sizeof cname - 1);
1266 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1270 result = screen_def.pixel;
1272 warnx("color '%s' not found", colorname);
1278 setscreencolor(char *val, int i, int c)
1282 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1283 if (i > 0 && i <= num_screens) {
1284 screens[i - 1].c[c].color = name_to_color(val);
1285 free(screens[i - 1].c[c].name);
1286 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1288 } else if (i == -1) {
1289 for (i = 0; i < num_screens; i++) {
1290 screens[i].c[c].color = name_to_color(val);
1291 free(screens[i].c[c].name);
1292 if ((screens[i].c[c].name = strdup(val)) == NULL)
1296 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1301 fancy_stacker(struct workspace *ws)
1303 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1304 if (ws->cur_layout->l_stack == vertical_stack)
1305 snprintf(ws->stacker, sizeof ws->stacker,
1306 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1307 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1308 if (ws->cur_layout->l_stack == horizontal_stack)
1309 snprintf(ws->stacker, sizeof ws->stacker,
1310 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1311 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1315 plain_stacker(struct workspace *ws)
1317 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1318 if (ws->cur_layout->l_stack == vertical_stack)
1319 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1320 sizeof ws->stacker);
1321 if (ws->cur_layout->l_stack == horizontal_stack)
1322 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1323 sizeof ws->stacker);
1327 custom_region(char *val)
1329 unsigned int sidx, x, y, w, h;
1331 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1332 errx(1, "invalid custom region, "
1333 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1334 if (sidx < 1 || sidx > ScreenCount(display))
1335 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1336 sidx, ScreenCount(display));
1340 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1342 if (x > DisplayWidth(display, sidx) ||
1343 y > DisplayHeight(display, sidx) ||
1344 w + x > DisplayWidth(display, sidx) ||
1345 h + y > DisplayHeight(display, sidx)) {
1346 warnx("ignoring region %ux%u+%u+%u - not within screen "
1347 "boundaries (%ux%u)", w, h, x, y,
1348 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1352 new_region(&screens[sidx], x, y, w, h);
1356 socket_setnonblock(int fd)
1360 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1361 err(1, "fcntl F_GETFL");
1362 flags |= O_NONBLOCK;
1363 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1364 err(1, "fcntl F_SETFL");
1368 bar_print(struct swm_region *r, const char *s)
1372 XRectangle ibox, lbox;
1375 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1377 switch (bar_justify) {
1378 case SWM_BAR_JUSTIFY_LEFT:
1381 case SWM_BAR_JUSTIFY_CENTER:
1382 x = (WIDTH(r) - lbox.width) / 2;
1384 case SWM_BAR_JUSTIFY_RIGHT:
1385 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1389 if (x < SWM_BAR_OFFSET)
1392 /* clear back buffer */
1393 XSetForeground(display, r->s->bar_gc, r->s->c[SWM_S_COLOR_BAR].color);
1394 XFillRectangle(display, r->bar->buffer, r->s->bar_gc, 0, 0,
1395 WIDTH(r->bar), HEIGHT(r->bar));
1397 /* draw back buffer */
1398 XSetForeground(display, r->s->bar_gc,
1399 r->s->c[SWM_S_COLOR_BAR_FONT].color);
1400 DRAWSTRING(display, r->bar->buffer, bar_fs, r->s->bar_gc,
1401 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1405 XCopyArea(display, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1406 WIDTH(r->bar), HEIGHT(r->bar), 0, 0);
1410 bar_extra_stop(void)
1414 bzero(bar_pipe, sizeof bar_pipe);
1417 kill(bar_pid, SIGTERM);
1420 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1425 bar_class_name(char *s, size_t sz, struct swm_region *r)
1427 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1429 if (r->ws->focus->ch.res_class != NULL)
1430 strlcat(s, r->ws->focus->ch.res_class, sz);
1434 bar_title_name(char *s, size_t sz, struct swm_region *r)
1436 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1438 if (r->ws->focus->ch.res_name != NULL)
1439 strlcat(s, r->ws->focus->ch.res_name, sz);
1443 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1445 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1448 bar_class_name(s, sz, r);
1449 strlcat(s, ":", sz);
1450 bar_title_name(s, sz, r);
1454 bar_window_float(char *s, size_t sz, struct swm_region *r)
1456 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1458 if (r->ws->focus->floating)
1459 strlcat(s, "(f)", sz);
1463 bar_window_name(char *s, size_t sz, struct swm_region *r)
1465 unsigned char *title;
1467 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1469 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1472 strlcat(s, (char *)title, sz);
1476 int urgent[SWM_WS_MAX];
1478 bar_urgent(char *s, size_t sz)
1480 XWMHints *wmh = NULL;
1485 for (i = 0; i < workspace_limit; i++)
1488 for (i = 0; i < ScreenCount(display); i++)
1489 for (j = 0; j < workspace_limit; j++)
1490 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1491 wmh = XGetWMHints(display, win->id);
1495 if (wmh->flags & XUrgencyHint)
1500 for (i = 0; i < workspace_limit; i++) {
1502 snprintf(b, sizeof b, "%d ", i + 1);
1504 snprintf(b, sizeof b, "- ");
1510 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1512 if (r == NULL || r->ws == NULL)
1514 if (r->ws->name != NULL)
1515 strlcat(s, r->ws->name, sz);
1518 /* build the default bar format according to the defined enabled options */
1520 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1522 /* if format provided, just copy the buffers */
1523 if (bar_format != NULL) {
1524 strlcpy(fmtnew, fmtexp, sz);
1528 /* reset the output buffer */
1531 strlcat(fmtnew, "+N:+I ", sz);
1533 strlcat(fmtnew, "+S", sz);
1534 strlcat(fmtnew, " ", sz);
1536 /* only show the workspace name if there's actually one */
1537 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1538 strlcat(fmtnew, "<+D>", sz);
1539 strlcat(fmtnew, "+3<", sz);
1541 if (clock_enabled) {
1542 strlcat(fmtnew, fmtexp, sz);
1543 strlcat(fmtnew, "+4<", sz);
1546 /* bar_urgent already adds the space before the last asterisk */
1548 strlcat(fmtnew, "* +U*+4<", sz);
1550 if (title_class_enabled) {
1551 strlcat(fmtnew, "+C", sz);
1552 if (title_name_enabled == 0)
1553 strlcat(fmtnew, "+4<", sz);
1556 /* checks needed by the colon and floating strlcat(3) calls below */
1557 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1558 if (title_name_enabled) {
1559 if (title_class_enabled)
1560 strlcat(fmtnew, ":", sz);
1561 strlcat(fmtnew, "+T+4<", sz);
1563 if (window_name_enabled) {
1564 if (r->ws->focus->floating)
1565 strlcat(fmtnew, "+F ", sz);
1566 strlcat(fmtnew, "+64W ", sz);
1570 /* finally add the action script output and the version */
1571 strlcat(fmtnew, "+4<+A+4<+V", sz);
1575 bar_replace_pad(char *tmp, int *limit, size_t sz)
1577 /* special case; no limit given, pad one space, instead */
1578 if (*limit == sz - 1)
1580 snprintf(tmp, sz, "%*s", *limit, " ");
1583 /* replaces the bar format character sequences (like in tmux(1)) */
1585 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1589 char tmp[SWM_BAR_MAX];
1593 /* reset strlcat(3) buffer */
1596 /* get number, if any */
1599 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1600 limit = sizeof tmp - 1;
1601 if (limit <= 0 || limit >= sizeof tmp)
1602 limit = sizeof tmp - 1;
1604 /* there is nothing to replace (ie EOL) */
1611 bar_replace_pad(tmp, &limit, sizeof tmp);
1614 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1617 bar_class_name(tmp, sizeof tmp, r);
1620 bar_workspace_name(tmp, sizeof tmp, r);
1623 bar_window_float(tmp, sizeof tmp, r);
1626 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1629 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1632 bar_class_title_name(tmp, sizeof tmp, r);
1635 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1638 bar_title_name(tmp, sizeof tmp, r);
1641 bar_urgent(tmp, sizeof tmp);
1644 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1647 bar_window_name(tmp, sizeof tmp, r);
1650 /* unknown character sequence; copy as-is */
1651 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1659 while (limit-- > 0) {
1660 if (*offrep >= sz - 1)
1662 fmtrep[(*offrep)++] = *ptr++;
1670 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1675 while (*fmt != '\0') {
1677 /* skip ordinary characters */
1680 fmtrep[off++] = *fmt++;
1684 /* character sequence found; replace it */
1685 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1694 bar_fmt_expand(char *fmtexp, size_t sz)
1701 /* start by grabbing the current time and date */
1703 localtime_r(&tmt, &tm);
1705 /* figure out what to expand */
1706 if (bar_format != NULL)
1708 else if (bar_format == NULL && clock_enabled)
1710 /* if nothing to expand bail out */
1716 /* copy as-is, just in case the format shouldn't be expanded below */
1717 strlcpy(fmtexp, fmt, sz);
1718 /* finally pass the string through strftime(3) */
1719 #ifndef SWM_DENY_CLOCK_FORMAT
1720 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1721 warnx("format too long");
1729 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1730 char fmtrep[SWM_BAR_MAX];
1732 struct swm_region *r;
1734 /* expand the format by first passing it through strftime(3) */
1735 bar_fmt_expand(fmtexp, sizeof fmtexp);
1737 for (i = 0; i < ScreenCount(display); i++) {
1738 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1741 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1742 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1743 bar_print(r, fmtrep);
1754 if (bar_enabled == 0)
1756 if (bar_extra && bar_extra_running) {
1757 /* ignore short reads; it'll correct itself */
1758 while ((b = fgetln(stdin, &len)) != NULL)
1759 if (b && b[len - 1] == '\n') {
1761 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1763 if (b == NULL && errno != EAGAIN) {
1764 warn("bar_update: bar_extra failed");
1768 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1781 bar_toggle(struct swm_region *r, union arg *args)
1783 struct swm_region *tmpr;
1784 int i, sc = ScreenCount(display);
1786 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1789 for (i = 0; i < sc; i++)
1790 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1792 XUnmapWindow(display, tmpr->bar->id);
1794 for (i = 0; i < sc; i++)
1795 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1797 XMapRaised(display, tmpr->bar->id);
1800 bar_enabled = !bar_enabled;
1803 /* must be after stack */
1810 XSetWindowAttributes wa;
1811 struct swm_region *r;
1814 /* do this here because the conf file is in memory */
1815 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1816 /* launch external status app */
1817 bar_extra_running = 1;
1818 if (pipe(bar_pipe) == -1)
1819 err(1, "pipe error");
1820 socket_setnonblock(bar_pipe[0]);
1821 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1822 if (dup2(bar_pipe[0], 0) == -1)
1824 if (dup2(bar_pipe[1], 1) == -1)
1826 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1827 err(1, "could not disable SIGPIPE");
1828 switch (bar_pid = fork()) {
1830 err(1, "cannot fork");
1834 execvp(bar_argv[0], bar_argv);
1835 err(1, "%s external app failed", bar_argv[0]);
1837 default: /* parent */
1843 bzero(&wa, sizeof wa);
1844 for (i = 0; i < ScreenCount(display); i++)
1845 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1849 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1850 wa.background_pixel =
1851 screens[i].c[SWM_S_COLOR_BAR].color;
1852 XChangeWindowAttributes(display, r->bar->id,
1853 CWBackPixel | CWBorderPixel, &wa);
1859 bar_setup(struct swm_region *r)
1861 char *default_string;
1862 char **missing_charsets;
1863 int num_missing_charsets = 0;
1867 XFreeFontSet(display, bar_fs);
1871 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1872 err(1, "bar_setup: calloc: failed to allocate memory.");
1874 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1876 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1877 &num_missing_charsets, &default_string);
1879 if (num_missing_charsets > 0) {
1880 warnx("Unable to load charset(s):");
1882 for (i = 0; i < num_missing_charsets; ++i)
1883 warnx("%s", missing_charsets[i]);
1885 XFreeStringList(missing_charsets);
1887 if (strcmp(default_string, ""))
1888 warnx("Glyphs from those sets will be replaced "
1889 "by '%s'.", default_string);
1891 warnx("Glyphs from those sets won't be drawn.");
1895 errx(1, "Error creating font set structure.");
1897 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1899 bar_height = bar_fs_extents->max_logical_extent.height +
1900 2 * bar_border_width;
1906 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1907 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1908 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1910 r->bar->id = XCreateSimpleWindow(display,
1911 r->s->root, X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1912 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1913 r->s->c[SWM_S_COLOR_BAR].color);
1915 r->bar->buffer = XCreatePixmap(display, r->bar->id, WIDTH(r->bar),
1916 HEIGHT(r->bar), DefaultDepth(display, r->s->idx));
1918 XSelectInput(display, r->bar->id, VisibilityChangeMask);
1921 XMapRaised(display, r->bar->id);
1923 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%lx, (x,y) w x h: (%d,%d) "
1924 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1927 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1928 err(1, "could not install bar_signal");
1933 bar_cleanup(struct swm_region *r)
1937 XDestroyWindow(display, r->bar->id);
1938 XFreePixmap(display, r->bar->buffer);
1944 drain_enter_notify(void)
1949 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1952 DNPRINTF(SWM_D_EVENT, "drain_enter_notify: drained: %d\n", i);
1956 set_win_state(struct ws_win *win, long state)
1958 long data[] = {state, None};
1960 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1965 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1966 (unsigned char *)data, 2);
1973 unsigned char *p = NULL;
1976 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1979 result = *((long *)p);
1985 version(struct swm_region *r, union arg *args)
1987 bar_version = !bar_version;
1989 snprintf(bar_vertext, sizeof bar_vertext,
1990 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1992 strlcpy(bar_vertext, "", sizeof bar_vertext);
1997 client_msg(struct ws_win *win, Atom a)
1999 XClientMessageEvent cm;
2004 bzero(&cm, sizeof cm);
2005 cm.type = ClientMessage;
2006 cm.window = win->id;
2007 cm.message_type = aprot;
2010 cm.data.l[1] = CurrentTime;
2011 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
2014 /* synthetic response to a ConfigureRequest when not making a change */
2016 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
2023 /* send notification of unchanged state. */
2024 ce.type = ConfigureNotify;
2027 ce.width = WIDTH(win);
2028 ce.height = HEIGHT(win);
2029 ce.override_redirect = False;
2033 ce.display = display;
2035 ce.window = win->id;
2036 ce.border_width = BORDER(win);
2040 ce.display = ev->display;
2041 ce.event = ev->window;
2042 ce.window = ev->window;
2044 /* make response appear more WM_SIZE_HINTS-compliant */
2046 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
2047 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2048 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
2049 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2050 SH_INC_W(win), SH_INC_H(win));
2054 /* the hint may be set... to 0! */
2055 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2056 ce.width = SH_MIN_W(win);
2057 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2058 ce.height = SH_MIN_H(win);
2063 /* may also be advertized as 0 */
2064 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2065 ce.width = SH_MAX_W(win);
2066 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2067 ce.height = SH_MAX_H(win);
2070 /* resize increment. */
2072 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2073 ce.width -= (ce.width - SH_MIN_W(win)) %
2075 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2076 ce.height -= (ce.height - SH_MIN_H(win)) %
2080 /* adjust x and y for requested border_width. */
2081 ce.x += BORDER(win) - ev->border_width;
2082 ce.y += BORDER(win) - ev->border_width;
2083 ce.border_width = ev->border_width;
2084 ce.above = ev->above;
2087 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2088 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2089 ce.y, ce.width, ce.height, ce.border_width);
2091 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
2095 count_win(struct workspace *ws, int count_transient)
2100 TAILQ_FOREACH(win, &ws->winlist, entry) {
2101 if (count_transient == 0 && win->floating)
2103 if (count_transient == 0 && win->transient)
2109 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2115 quit(struct swm_region *r, union arg *args)
2117 DNPRINTF(SWM_D_MISC, "quit\n");
2122 unmap_window(struct ws_win *win)
2127 /* don't unmap again */
2128 if (getstate(win->id) == IconicState)
2131 set_win_state(win, IconicState);
2133 XUnmapWindow(display, win->id);
2134 XSetWindowBorder(display, win->id,
2135 win->s->c[SWM_S_COLOR_UNFOCUS].color);
2144 for (i = 0; i < ScreenCount(display); i++)
2145 for (j = 0; j < workspace_limit; j++)
2146 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2151 fake_keypress(struct ws_win *win, int keysym, int modifiers)
2158 event.display = display; /* Ignored, but what the hell */
2159 event.window = win->id;
2160 event.root = win->s->root;
2161 event.subwindow = None;
2162 event.time = CurrentTime;
2167 event.same_screen = True;
2168 event.keycode = XKeysymToKeycode(display, keysym);
2169 event.state = modifiers;
2171 event.type = KeyPress;
2172 XSendEvent(event.display, event.window, True,
2173 KeyPressMask, (XEvent *)&event);
2175 event.type = KeyRelease;
2176 XSendEvent(event.display, event.window, True,
2177 KeyPressMask, (XEvent *)&event);
2182 restart(struct swm_region *r, union arg *args)
2184 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2186 /* disable alarm because the following code may not be interrupted */
2188 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2189 err(1, "can't disable alarm");
2194 xcb_disconnect(conn);
2195 XCloseDisplay(display);
2196 execvp(start_argv[0], start_argv);
2197 warn("execvp failed");
2202 root_to_region(Window root)
2204 struct swm_region *r = NULL;
2206 int i, x, y, wx, wy;
2209 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%lx\n", root);
2211 for (i = 0; i < ScreenCount(display); i++)
2212 if (screens[i].root == root)
2215 if (XQueryPointer(display, screens[i].root,
2216 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
2217 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2219 /* choose a region based on pointer location */
2220 TAILQ_FOREACH(r, &screens[i].rl, entry)
2221 if (X(r) <= x && x < MAX_X(r) &&
2222 Y(r) <= y && y < MAX_Y(r))
2227 r = TAILQ_FIRST(&screens[i].rl);
2233 find_unmanaged_window(Window id)
2238 for (i = 0; i < ScreenCount(display); i++)
2239 for (j = 0; j < workspace_limit; j++)
2240 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2248 find_window(Window id)
2251 Window wrr, wpr, *wcr = NULL;
2255 for (i = 0; i < ScreenCount(display); i++)
2256 for (j = 0; j < workspace_limit; j++)
2257 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2261 /* if we were looking for the parent return that window instead */
2262 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
2267 /* ignore not found and root */
2268 if (wpr == 0 || wrr == wpr)
2271 /* look for parent */
2272 for (i = 0; i < ScreenCount(display); i++)
2273 for (j = 0; j < workspace_limit; j++)
2274 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2282 spawn(int ws_idx, union arg *args, int close_fd)
2287 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2290 close(ConnectionNumber(display));
2292 setenv("LD_PRELOAD", SWM_LIB, 1);
2294 if (asprintf(&ret, "%d", ws_idx) == -1) {
2295 warn("spawn: asprintf SWM_WS");
2298 setenv("_SWM_WS", ret, 1);
2302 if (asprintf(&ret, "%d", getpid()) == -1) {
2303 warn("spawn: asprintf _SWM_PID");
2306 setenv("_SWM_PID", ret, 1);
2310 if (setsid() == -1) {
2311 warn("spawn: setsid");
2317 * close stdin and stdout to prevent interaction between apps
2318 * and the baraction script
2319 * leave stderr open to record errors
2321 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2322 warn("spawn: open");
2325 dup2(fd, STDIN_FILENO);
2326 dup2(fd, STDOUT_FILENO);
2331 execvp(args->argv[0], args->argv);
2333 warn("spawn: execvp");
2338 kill_refs(struct ws_win *win)
2341 struct swm_region *r;
2342 struct workspace *ws;
2347 for (i = 0; i < ScreenCount(display); i++)
2348 TAILQ_FOREACH(r, &screens[i].rl, entry)
2349 for (x = 0; x < workspace_limit; x++) {
2351 if (win == ws->focus)
2353 if (win == ws->focus_prev)
2354 ws->focus_prev = NULL;
2359 validate_win(struct ws_win *testwin)
2362 struct workspace *ws;
2363 struct swm_region *r;
2366 if (testwin == NULL)
2369 for (i = 0; i < ScreenCount(display); i++)
2370 TAILQ_FOREACH(r, &screens[i].rl, entry)
2371 for (x = 0; x < workspace_limit; x++) {
2373 TAILQ_FOREACH(win, &ws->winlist, entry)
2381 validate_ws(struct workspace *testws)
2383 struct swm_region *r;
2384 struct workspace *ws;
2387 /* validate all ws */
2388 for (i = 0; i < ScreenCount(display); i++)
2389 TAILQ_FOREACH(r, &screens[i].rl, entry)
2390 for (x = 0; x < workspace_limit; x++) {
2399 unfocus_win(struct ws_win *win)
2404 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2408 if (win->ws == NULL)
2411 if (validate_ws(win->ws))
2412 return; /* XXX this gets hit with thunderbird, needs fixing */
2414 if (win->ws->r == NULL)
2417 if (validate_win(win)) {
2422 if (win->ws->focus == win) {
2423 win->ws->focus = NULL;
2424 win->ws->focus_prev = win;
2427 if (validate_win(win->ws->focus)) {
2428 kill_refs(win->ws->focus);
2429 win->ws->focus = NULL;
2431 if (validate_win(win->ws->focus_prev)) {
2432 kill_refs(win->ws->focus_prev);
2433 win->ws->focus_prev = NULL;
2436 /* drain all previous unfocus events */
2437 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2440 grabbuttons(win, 0);
2441 XSetWindowBorder(display, win->id,
2442 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2444 XChangeProperty(display, win->s->root,
2445 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2446 PropModeReplace, (unsigned char *)&none, 1);
2455 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2457 for (i = 0; i < ScreenCount(display); i++)
2458 for (j = 0; j < workspace_limit; j++)
2459 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2464 focus_win(struct ws_win *win)
2469 struct ws_win *cfw = NULL;
2472 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2476 if (win->ws == NULL)
2479 if (validate_ws(win->ws))
2480 return; /* XXX this gets hit with thunderbird, needs fixing */
2482 if (validate_win(win)) {
2487 if (validate_win(win)) {
2492 XGetInputFocus(display, &cur_focus, &rr);
2493 if ((cfw = find_window(cur_focus)) != NULL)
2496 /* use larger hammer since the window was killed somehow */
2497 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2498 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2499 XSetWindowBorder(display, cfw->id,
2500 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2503 win->ws->focus = win;
2505 if (win->ws->r != NULL) {
2506 /* drain all previous focus events */
2507 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2511 XSetInputFocus(display, win->id,
2512 RevertToParent, CurrentTime);
2513 grabbuttons(win, 1);
2514 XSetWindowBorder(display, win->id,
2515 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2516 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2517 win->ws->always_raise)
2518 XMapRaised(display, win->id);
2520 XChangeProperty(display, win->s->root,
2521 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2522 PropModeReplace, (unsigned char *)&win->id, 1);
2529 switchws(struct swm_region *r, union arg *args)
2531 int wsid = args->id, unmap_old = 0;
2532 struct swm_region *this_r, *other_r;
2534 struct workspace *new_ws, *old_ws;
2540 if (wsid >= workspace_limit)
2544 old_ws = this_r->ws;
2545 new_ws = &this_r->s->ws[wsid];
2547 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2548 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2550 if (new_ws == NULL || old_ws == NULL)
2552 if (new_ws == old_ws)
2555 other_r = new_ws->r;
2556 if (other_r == NULL) {
2557 /* the other workspace is hidden, hide this one */
2561 /* the other ws is visible in another region, exchange them */
2562 other_r->ws_prior = new_ws;
2563 other_r->ws = old_ws;
2564 old_ws->r = other_r;
2566 this_r->ws_prior = old_ws;
2567 this_r->ws = new_ws;
2570 /* this is needed so that we can click on a window after a restart */
2574 a.id = SWM_ARG_ID_FOCUSCUR;
2575 focus(new_ws->r, &a);
2577 /* unmap old windows */
2579 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2582 if (focus_mode == SWM_FOCUS_DEFAULT)
2583 drain_enter_notify();
2587 cyclews(struct swm_region *r, union arg *args)
2590 struct swm_screen *s = r->s;
2593 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2594 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2599 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2602 case SWM_ARG_ID_CYCLEWS_UP:
2603 if (a.id < workspace_limit - 1)
2608 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2611 case SWM_ARG_ID_CYCLEWS_DOWN:
2615 a.id = workspace_limit - 1;
2622 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2624 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2628 } while (a.id != r->ws->idx);
2632 priorws(struct swm_region *r, union arg *args)
2636 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2637 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2639 if (r->ws_prior == NULL)
2642 a.id = r->ws_prior->idx;
2647 cyclescr(struct swm_region *r, union arg *args)
2649 struct swm_region *rr = NULL;
2653 /* do nothing if we don't have more than one screen */
2654 if (!(ScreenCount(display) > 1 || outputs > 1))
2659 case SWM_ARG_ID_CYCLESC_UP:
2660 rr = TAILQ_NEXT(r, entry);
2662 rr = TAILQ_FIRST(&screens[i].rl);
2664 case SWM_ARG_ID_CYCLESC_DOWN:
2665 rr = TAILQ_PREV(r, swm_region_list, entry);
2667 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2675 /* move mouse to region */
2677 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2678 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2680 a.id = SWM_ARG_ID_FOCUSCUR;
2683 if (rr->ws->focus) {
2684 /* move to focus window */
2685 x = X(rr->ws->focus) + 1;
2686 y = Y(rr->ws->focus) + 1;
2687 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2692 sort_windows(struct ws_win_list *wl)
2694 struct ws_win *win, *parent, *nxt;
2699 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2700 nxt = TAILQ_NEXT(win, entry);
2701 if (win->transient) {
2702 parent = find_window(win->transient);
2703 if (parent == NULL) {
2704 warnx("not possible bug");
2707 TAILQ_REMOVE(wl, win, entry);
2708 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2715 swapwin(struct swm_region *r, union arg *args)
2717 struct ws_win *target, *source;
2718 struct ws_win *cur_focus;
2719 struct ws_win_list *wl;
2722 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2723 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2725 cur_focus = r->ws->focus;
2726 if (cur_focus == NULL)
2730 wl = &source->ws->winlist;
2733 case SWM_ARG_ID_SWAPPREV:
2734 if (source->transient)
2735 source = find_window(source->transient);
2736 target = TAILQ_PREV(source, ws_win_list, entry);
2737 if (target && target->transient)
2738 target = find_window(target->transient);
2739 TAILQ_REMOVE(wl, source, entry);
2741 TAILQ_INSERT_TAIL(wl, source, entry);
2743 TAILQ_INSERT_BEFORE(target, source, entry);
2745 case SWM_ARG_ID_SWAPNEXT:
2746 target = TAILQ_NEXT(source, entry);
2747 /* move the parent and let the sort handle the move */
2748 if (source->transient)
2749 source = find_window(source->transient);
2750 TAILQ_REMOVE(wl, source, entry);
2752 TAILQ_INSERT_HEAD(wl, source, entry);
2754 TAILQ_INSERT_AFTER(wl, target, source, entry);
2756 case SWM_ARG_ID_SWAPMAIN:
2757 target = TAILQ_FIRST(wl);
2758 if (target == source) {
2759 if (source->ws->focus_prev != NULL &&
2760 source->ws->focus_prev != target)
2761 source = source->ws->focus_prev;
2765 if (target == NULL || source == NULL)
2767 source->ws->focus_prev = target;
2768 TAILQ_REMOVE(wl, target, entry);
2769 TAILQ_INSERT_BEFORE(source, target, entry);
2770 TAILQ_REMOVE(wl, source, entry);
2771 TAILQ_INSERT_HEAD(wl, source, entry);
2773 case SWM_ARG_ID_MOVELAST:
2774 TAILQ_REMOVE(wl, source, entry);
2775 TAILQ_INSERT_TAIL(wl, source, entry);
2778 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2788 focus_prev(struct ws_win *win)
2790 struct ws_win *winfocus = NULL;
2791 struct ws_win *cur_focus = NULL;
2792 struct ws_win_list *wl = NULL;
2793 struct workspace *ws = NULL;
2795 if (!(win && win->ws))
2800 cur_focus = ws->focus;
2802 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2803 WINID(win), WINID(cur_focus));
2805 /* pickle, just focus on whatever */
2806 if (cur_focus == NULL) {
2807 /* use prev_focus if valid */
2808 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2809 find_window(WINID(ws->focus_prev)))
2810 winfocus = ws->focus_prev;
2814 /* if transient focus on parent */
2815 if (cur_focus->transient) {
2816 winfocus = find_window(cur_focus->transient);
2820 /* if in max_stack try harder */
2821 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2822 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2823 if (cur_focus != ws->focus_prev)
2824 winfocus = ws->focus_prev;
2826 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2831 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2833 if (winfocus == NULL || winfocus == win) {
2834 switch (focus_close) {
2835 case SWM_STACK_BOTTOM:
2836 winfocus = TAILQ_FIRST(wl);
2839 winfocus = TAILQ_LAST(wl, ws_win_list);
2841 case SWM_STACK_ABOVE:
2842 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2843 if (focus_close_wrap)
2844 winfocus = TAILQ_FIRST(wl);
2846 winfocus = TAILQ_PREV(cur_focus,
2847 ws_win_list, entry);
2850 case SWM_STACK_BELOW:
2851 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2853 if (focus_close_wrap)
2854 winfocus = TAILQ_LAST(wl, ws_win_list);
2856 winfocus = TAILQ_NEXT(cur_focus, entry);
2862 if (winfocus == NULL) {
2863 if (focus_default == SWM_STACK_TOP)
2864 winfocus = TAILQ_LAST(wl, ws_win_list);
2866 winfocus = TAILQ_FIRST(wl);
2870 focus_magic(winfocus);
2874 focus(struct swm_region *r, union arg *args)
2876 struct ws_win *winfocus = NULL, *head;
2877 struct ws_win *cur_focus = NULL;
2878 struct ws_win_list *wl = NULL;
2879 struct workspace *ws = NULL;
2885 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2887 /* treat FOCUS_CUR special */
2888 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2889 if (r->ws->focus && r->ws->focus->iconic == 0)
2890 winfocus = r->ws->focus;
2891 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2892 winfocus = r->ws->focus_prev;
2894 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2895 if (winfocus->iconic == 0)
2898 focus_magic(winfocus);
2902 if ((cur_focus = r->ws->focus) == NULL)
2906 if (TAILQ_EMPTY(wl))
2908 /* make sure there is at least one uniconified window */
2910 TAILQ_FOREACH(winfocus, wl, entry)
2911 if (winfocus->iconic == 0) {
2919 case SWM_ARG_ID_FOCUSPREV:
2920 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2922 head = TAILQ_LAST(wl, ws_win_list);
2924 if (WINID(winfocus) == cur_focus->transient) {
2925 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2927 head = TAILQ_LAST(wl, ws_win_list);
2932 if (winfocus && winfocus->iconic) {
2933 while (winfocus != cur_focus) {
2934 if (winfocus == NULL)
2935 winfocus = TAILQ_LAST(wl, ws_win_list);
2936 if (winfocus->iconic == 0)
2938 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2944 case SWM_ARG_ID_FOCUSNEXT:
2945 head = TAILQ_NEXT(cur_focus, entry);
2947 head = TAILQ_FIRST(wl);
2951 if (winfocus && winfocus->iconic) {
2952 while (winfocus != cur_focus) {
2953 if (winfocus == NULL)
2954 winfocus = TAILQ_FIRST(wl);
2955 if (winfocus->iconic == 0)
2957 winfocus = TAILQ_NEXT(winfocus, entry);
2962 case SWM_ARG_ID_FOCUSMAIN:
2963 winfocus = TAILQ_FIRST(wl);
2964 if (winfocus == cur_focus)
2965 winfocus = cur_focus->ws->focus_prev;
2972 focus_magic(winfocus);
2976 cycle_layout(struct swm_region *r, union arg *args)
2978 struct workspace *ws = r->ws;
2981 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2984 if (ws->cur_layout->l_stack == NULL)
2985 ws->cur_layout = &layouts[0];
2988 if (focus_mode == SWM_FOCUS_DEFAULT)
2989 drain_enter_notify();
2990 a.id = SWM_ARG_ID_FOCUSCUR;
2995 stack_config(struct swm_region *r, union arg *args)
2997 struct workspace *ws = r->ws;
2999 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3002 if (ws->cur_layout->l_config != NULL)
3003 ws->cur_layout->l_config(ws, args->id);
3005 if (args->id != SWM_ARG_ID_STACKINIT)
3012 struct swm_geometry g;
3013 struct swm_region *r;
3019 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3021 for (i = 0; i < ScreenCount(display); i++) {
3025 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3026 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3027 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3029 /* start with screen geometry, adjust for bar */
3031 g.w -= 2 * border_width;
3032 g.h -= 2 * border_width;
3038 r->ws->cur_layout->l_stack(r->ws, &g);
3039 r->ws->cur_layout->l_string(r->ws);
3040 /* save r so we can track region changes */
3047 if (focus_mode == SWM_FOCUS_DEFAULT)
3048 drain_enter_notify();
3050 DNPRINTF(SWM_D_STACK, "stack: end\n");
3054 store_float_geom(struct ws_win *win, struct swm_region *r)
3056 /* retain window geom and region geom */
3057 win->g_float = win->g;
3058 win->g_float.x -= X(r);
3059 win->g_float.y -= Y(r);
3060 win->g_floatvalid = 1;
3061 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3062 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3063 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3064 win->g_float.w, win->g_float.h);
3068 stack_floater(struct ws_win *win, struct swm_region *r)
3073 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%lx\n", win->id);
3076 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3077 * geom on ws switches or return from max mode
3079 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3080 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3081 /* refloat at last floating relative position */
3082 win->g = win->g_float;
3087 win->floatmaxed = 0;
3090 * if set to fullscreen mode, configure window to maximum size.
3092 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3093 if (!win->g_floatvalid)
3094 store_float_geom(win, win->ws->r);
3096 win->g = win->ws->r->g;
3100 * remove border on fullscreen floater when in fullscreen mode or when
3101 * the quirk is present.
3103 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3104 ((win->quirks & SWM_Q_FULLSCREEN) &&
3105 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3106 if (win->bordered) {
3108 X(win) += border_width;
3109 Y(win) += border_width;
3111 } else if (!win->bordered) {
3113 X(win) -= border_width;
3114 Y(win) -= border_width;
3117 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3118 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3119 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3124 * floaters and transients are auto-centred unless moved
3127 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3128 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3131 /* keep window within region bounds */
3132 constrain_window(win, r, 0);
3138 * Send keystrokes to terminal to decrease/increase the font size as the
3139 * window size changes.
3142 adjust_font(struct ws_win *win)
3144 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3145 win->floating || win->transient)
3148 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3149 WIDTH(win) / win->sh.width_inc < term_width &&
3150 win->font_steps < SWM_MAX_FONT_STEPS) {
3151 win->font_size_boundary[win->font_steps] =
3152 (win->sh.width_inc * term_width) + win->sh.base_width;
3155 win->last_inc = win->sh.width_inc;
3156 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3157 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3158 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3161 win->last_inc = win->sh.width_inc;
3162 fake_keypress(win, XK_KP_Add, ShiftMask);
3166 #define SWAPXY(g) do { \
3168 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3169 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3172 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3174 XWindowAttributes wa;
3175 struct swm_geometry win_g, r_g = *g;
3176 struct ws_win *win, *fs_win = NULL;
3177 int i, j, s, stacks;
3178 int w_inc = 1, h_inc, w_base = 1, h_base;
3179 int hrh, extra = 0, h_slice, last_h = 0;
3180 int split, colno, winno, mwin, msize, mscale;
3181 int remain, missing, v_slice, reconfigure;
3184 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3185 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3187 winno = count_win(ws, 0);
3188 if (winno == 0 && count_win(ws, 1) == 0)
3191 TAILQ_FOREACH(win, &ws->winlist, entry)
3192 if (win->transient == 0 && win->floating == 0
3193 && win->iconic == 0)
3200 w_inc = win->sh.width_inc;
3201 w_base = win->sh.base_width;
3202 mwin = ws->l_state.horizontal_mwin;
3203 mscale = ws->l_state.horizontal_msize;
3204 stacks = ws->l_state.horizontal_stacks;
3207 w_inc = win->sh.height_inc;
3208 w_base = win->sh.base_height;
3209 mwin = ws->l_state.vertical_mwin;
3210 mscale = ws->l_state.vertical_msize;
3211 stacks = ws->l_state.vertical_stacks;
3215 if (stacks > winno - mwin)
3216 stacks = winno - mwin;
3220 h_slice = r_g.h / SWM_H_SLICE;
3221 if (mwin && winno > mwin) {
3222 v_slice = r_g.w / SWM_V_SLICE;
3226 win_g.w = v_slice * mscale;
3228 if (w_inc > 1 && w_inc < v_slice) {
3229 /* adjust for window's requested size increment */
3230 remain = (win_g.w - w_base) % w_inc;
3236 win_g.x += r_g.w - msize;
3239 colno = split = winno / stacks;
3240 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3241 2 * border_width) / stacks);
3243 hrh = r_g.h / colno;
3244 extra = r_g.h - (colno * hrh);
3245 win_g.h = hrh - 2 * border_width;
3247 /* stack all the tiled windows */
3248 i = j = 0, s = stacks;
3249 TAILQ_FOREACH(win, &ws->winlist, entry) {
3250 if (win->transient != 0 || win->floating != 0)
3252 if (win->iconic != 0)
3255 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3260 if (split && i == split) {
3261 colno = (winno - mwin) / stacks;
3262 if (s <= (winno - mwin) % stacks)
3264 split = split + colno;
3265 hrh = (r_g.h / colno);
3266 extra = r_g.h - (colno * hrh);
3270 win_g.x += win_g.w + 2 * border_width;
3271 win_g.w = (r_g.w - msize -
3272 (stacks * 2 * border_width)) / stacks;
3274 win_g.w += (r_g.w - msize -
3275 (stacks * 2 * border_width)) % stacks;
3279 win_g.h = hrh - 2 * border_width;
3281 h_inc = win->sh.width_inc;
3282 h_base = win->sh.base_width;
3284 h_inc = win->sh.height_inc;
3285 h_base = win->sh.base_height;
3287 if (j == colno - 1) {
3288 win_g.h = hrh + extra;
3289 } else if (h_inc > 1 && h_inc < h_slice) {
3290 /* adjust for window's requested size increment */
3291 remain = (win_g.h - h_base) % h_inc;
3292 missing = h_inc - remain;
3294 if (missing <= extra || j == 0) {
3306 win_g.y += last_h + 2 * border_width;
3308 if (disable_border && bar_enabled == 0 && winno == 1){
3310 win_g.w += 2 * border_width;
3311 win_g.h += 2 * border_width;
3316 if (X(win) != win_g.y || Y(win) != win_g.x ||
3317 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3321 WIDTH(win) = win_g.h;
3322 HEIGHT(win) = win_g.w;
3325 if (X(win) != win_g.x || Y(win) != win_g.y ||
3326 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3330 WIDTH(win) = win_g.w;
3331 HEIGHT(win) = win_g.h;
3335 if (bordered != win->bordered) {
3337 win->bordered = bordered;
3345 if (XGetWindowAttributes(display, win->id, &wa))
3346 if (wa.map_state == IsUnmapped)
3347 XMapRaised(display, win->id);
3355 /* now, stack all the floaters and transients */
3356 TAILQ_FOREACH(win, &ws->winlist, entry) {
3357 if (win->transient == 0 && win->floating == 0)
3359 if (win->iconic == 1)
3361 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3366 stack_floater(win, ws->r);
3367 XMapRaised(display, win->id);
3371 stack_floater(fs_win, ws->r);
3372 XMapRaised(display, fs_win->id);
3377 vertical_config(struct workspace *ws, int id)
3379 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3383 case SWM_ARG_ID_STACKRESET:
3384 case SWM_ARG_ID_STACKINIT:
3385 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3386 ws->l_state.vertical_mwin = 1;
3387 ws->l_state.vertical_stacks = 1;
3389 case SWM_ARG_ID_MASTERSHRINK:
3390 if (ws->l_state.vertical_msize > 1)
3391 ws->l_state.vertical_msize--;
3393 case SWM_ARG_ID_MASTERGROW:
3394 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3395 ws->l_state.vertical_msize++;
3397 case SWM_ARG_ID_MASTERADD:
3398 ws->l_state.vertical_mwin++;
3400 case SWM_ARG_ID_MASTERDEL:
3401 if (ws->l_state.vertical_mwin > 0)
3402 ws->l_state.vertical_mwin--;
3404 case SWM_ARG_ID_STACKINC:
3405 ws->l_state.vertical_stacks++;
3407 case SWM_ARG_ID_STACKDEC:
3408 if (ws->l_state.vertical_stacks > 1)
3409 ws->l_state.vertical_stacks--;
3411 case SWM_ARG_ID_FLIPLAYOUT:
3412 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3420 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3422 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3424 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3428 horizontal_config(struct workspace *ws, int id)
3430 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3433 case SWM_ARG_ID_STACKRESET:
3434 case SWM_ARG_ID_STACKINIT:
3435 ws->l_state.horizontal_mwin = 1;
3436 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3437 ws->l_state.horizontal_stacks = 1;
3439 case SWM_ARG_ID_MASTERSHRINK:
3440 if (ws->l_state.horizontal_msize > 1)
3441 ws->l_state.horizontal_msize--;
3443 case SWM_ARG_ID_MASTERGROW:
3444 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3445 ws->l_state.horizontal_msize++;
3447 case SWM_ARG_ID_MASTERADD:
3448 ws->l_state.horizontal_mwin++;
3450 case SWM_ARG_ID_MASTERDEL:
3451 if (ws->l_state.horizontal_mwin > 0)
3452 ws->l_state.horizontal_mwin--;
3454 case SWM_ARG_ID_STACKINC:
3455 ws->l_state.horizontal_stacks++;
3457 case SWM_ARG_ID_STACKDEC:
3458 if (ws->l_state.horizontal_stacks > 1)
3459 ws->l_state.horizontal_stacks--;
3461 case SWM_ARG_ID_FLIPLAYOUT:
3462 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3470 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3472 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3474 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3477 /* fullscreen view */
3479 max_stack(struct workspace *ws, struct swm_geometry *g)
3481 struct swm_geometry gg = *g;
3482 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3485 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3490 winno = count_win(ws, 0);
3491 if (winno == 0 && count_win(ws, 1) == 0)
3494 TAILQ_FOREACH(win, &ws->winlist, entry) {
3495 if (win->transient) {
3497 parent = find_window(win->transient);
3501 if (win->floating && win->floatmaxed == 0 ) {
3503 * retain geometry for retrieval on exit from
3506 store_float_geom(win, ws->r);
3507 win->floatmaxed = 1;
3510 /* only reconfigure if necessary */
3511 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3512 HEIGHT(win) != gg.h) {
3518 WIDTH(win) += 2 * border_width;
3519 HEIGHT(win) += 2 * border_width;
3524 /* unmap only if we don't have multi screen */
3525 if (win != ws->focus)
3526 if (!(ScreenCount(display) > 1 || outputs > 1))
3530 /* put the last transient on top */
3533 XMapRaised(display, parent->id);
3534 stack_floater(wintrans, ws->r);
3535 focus_magic(wintrans);
3540 send_to_ws(struct swm_region *r, union arg *args)
3542 int wsid = args->id;
3543 struct ws_win *win = NULL, *parent;
3544 struct workspace *ws, *nws;
3545 Atom ws_idx_atom = 0;
3546 unsigned char ws_idx_str[SWM_PROPLEN];
3549 if (wsid >= workspace_limit)
3552 if (r && r->ws && r->ws->focus)
3558 if (win->ws->idx == wsid)
3561 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3564 nws = &win->s->ws[wsid];
3566 a.id = SWM_ARG_ID_FOCUSPREV;
3568 if (win->transient) {
3569 parent = find_window(win->transient);
3571 unmap_window(parent);
3572 TAILQ_REMOVE(&ws->winlist, parent, entry);
3573 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3578 TAILQ_REMOVE(&ws->winlist, win, entry);
3579 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3580 if (TAILQ_EMPTY(&ws->winlist))
3581 r->ws->focus = NULL;
3584 /* Try to update the window's workspace property */
3585 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3587 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3589 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3591 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3592 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
3600 pressbutton(struct swm_region *r, union arg *args)
3602 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3603 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3607 raise_toggle(struct swm_region *r, union arg *args)
3609 if (r == NULL || r->ws == NULL)
3612 r->ws->always_raise = !r->ws->always_raise;
3614 /* bring floaters back to top */
3615 if (r->ws->always_raise == 0)
3620 iconify(struct swm_region *r, union arg *args)
3624 if (r->ws->focus == NULL)
3626 unmap_window(r->ws->focus);
3627 update_iconic(r->ws->focus, 1);
3629 if (focus_mode == SWM_FOCUS_DEFAULT)
3630 drain_enter_notify();
3631 r->ws->focus = NULL;
3632 a.id = SWM_ARG_ID_FOCUSCUR;
3637 get_win_name(Window win)
3639 unsigned char *prop = NULL;
3640 unsigned long nbytes, nitems;
3642 /* try _NET_WM_NAME first */
3643 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3646 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3647 &nitems, NULL, &prop))
3651 /* fallback to WM_NAME */
3652 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3655 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3662 uniconify(struct swm_region *r, union arg *args)
3666 unsigned char *name;
3669 DNPRINTF(SWM_D_MISC, "uniconify\n");
3671 if (r == NULL || r->ws == NULL)
3674 /* make sure we have anything to uniconify */
3675 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3676 if (win->ws == NULL)
3677 continue; /* should never happen */
3678 if (win->iconic == 0)
3686 search_resp_action = SWM_SEARCH_UNICONIFY;
3688 spawn_select(r, args, "search", &searchpid);
3690 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3693 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3694 if (win->ws == NULL)
3695 continue; /* should never happen */
3696 if (win->iconic == 0)
3699 name = get_win_name(win->id);
3702 fprintf(lfile, "%s.%lu\n", name, win->id);
3710 name_workspace(struct swm_region *r, union arg *args)
3714 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3720 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3722 spawn_select(r, args, "name_workspace", &searchpid);
3724 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3727 fprintf(lfile, "%s", "");
3732 search_workspace(struct swm_region *r, union arg *args)
3735 struct workspace *ws;
3738 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3744 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3746 spawn_select(r, args, "search", &searchpid);
3748 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3751 for (i = 0; i < workspace_limit; i++) {
3755 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3756 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3763 search_win_cleanup(void)
3765 struct search_window *sw = NULL;
3767 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3768 XDestroyWindow(display, sw->indicator);
3769 XFreeGC(display, sw->gc);
3770 TAILQ_REMOVE(&search_wl, sw, entry);
3776 search_win(struct swm_region *r, union arg *args)
3778 struct ws_win *win = NULL;
3779 struct search_window *sw = NULL;
3786 XRectangle ibox, lbox;
3788 DNPRINTF(SWM_D_MISC, "search_win\n");
3791 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3793 spawn_select(r, args, "search", &searchpid);
3795 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3798 TAILQ_INIT(&search_wl);
3801 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3802 if (win->iconic == 1)
3805 sw = calloc(1, sizeof(struct search_window));
3807 warn("search_win: calloc");
3809 search_win_cleanup();
3815 snprintf(s, sizeof s, "%d", i);
3818 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3820 w = XCreateSimpleWindow(display,
3821 win->id, 0, 0,lbox.width + 4,
3822 bar_fs_extents->max_logical_extent.height, 1,
3823 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3824 r->s->c[SWM_S_COLOR_FOCUS].color);
3827 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3829 sw->gc = XCreateGC(display, w, 0, &gcv);
3830 XMapRaised(display, w);
3831 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3833 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3834 (bar_fs_extents->max_logical_extent.height -
3835 lbox.height) / 2 - lbox.y, s, len);
3837 fprintf(lfile, "%d\n", i);
3845 search_resp_uniconify(char *resp, unsigned long len)
3847 unsigned char *name;
3851 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3853 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3854 if (win->iconic == 0)
3856 name = get_win_name(win->id);
3859 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3864 if (strncmp(s, resp, len) == 0) {
3865 /* XXX this should be a callback to generalize */
3866 update_iconic(win, 0);
3875 search_resp_name_workspace(char *resp, unsigned long len)
3877 struct workspace *ws;
3879 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3881 if (search_r->ws == NULL)
3886 free(search_r->ws->name);
3887 search_r->ws->name = NULL;
3891 ws->name = strdup(resp);
3892 if (ws->name == NULL) {
3893 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3894 "strdup: %s", strerror(errno));
3901 search_resp_search_workspace(char *resp, unsigned long len)
3908 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3912 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3919 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3921 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3928 switchws(search_r, &a);
3932 search_resp_search_window(char *resp, unsigned long len)
3937 struct search_window *sw;
3939 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3943 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3948 idx = strtonum(s, 1, INT_MAX, &errstr);
3950 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3957 TAILQ_FOREACH(sw, &search_wl, entry)
3958 if (idx == sw->idx) {
3964 #define MAX_RESP_LEN 1024
3967 search_do_resp(void)
3973 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3978 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3979 warn("search: calloc");
3983 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3985 warn("search: read error");
3988 resp[rbytes] = '\0';
3991 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3992 * newline, so work around that by sanitizing the resp now.
3994 resp[strcspn(resp, "\n")] = '\0';
3997 switch (search_resp_action) {
3998 case SWM_SEARCH_UNICONIFY:
3999 search_resp_uniconify(resp, len);
4001 case SWM_SEARCH_NAME_WORKSPACE:
4002 search_resp_name_workspace(resp, len);
4004 case SWM_SEARCH_SEARCH_WORKSPACE:
4005 search_resp_search_workspace(resp, len);
4007 case SWM_SEARCH_SEARCH_WINDOW:
4008 search_resp_search_window(resp, len);
4013 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4014 search_win_cleanup();
4016 search_resp_action = SWM_SEARCH_NONE;
4017 close(select_resp_pipe[0]);
4022 wkill(struct swm_region *r, union arg *args)
4024 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4026 if (r->ws->focus == NULL)
4029 if (args->id == SWM_ARG_ID_KILLWINDOW)
4030 XKillClient(display, r->ws->focus->id);
4032 if (r->ws->focus->can_delete)
4033 client_msg(r->ws->focus, adelete);
4038 floating_toggle_win(struct ws_win *win)
4040 struct swm_region *r;
4050 /* reject floating toggles in max stack mode */
4051 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4054 if (win->floating) {
4055 if (!win->floatmaxed) {
4056 /* retain position for refloat */
4057 store_float_geom(win, r);
4061 if (win->g_floatvalid) {
4062 /* refloat at last floating relative position */
4063 X(win) = win->g_float.x + X(r);
4064 Y(win) = win->g_float.y + Y(r);
4065 WIDTH(win) = win->g_float.w;
4066 HEIGHT(win) = win->g_float.h;
4071 ewmh_update_actions(win);
4077 floating_toggle(struct swm_region *r, union arg *args)
4079 struct ws_win *win = r->ws->focus;
4085 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4086 _NET_WM_STATE_TOGGLE);
4089 if (focus_mode == SWM_FOCUS_DEFAULT)
4090 drain_enter_notify();
4092 if (win == win->ws->focus) {
4093 a.id = SWM_ARG_ID_FOCUSCUR;
4094 focus(win->ws->r, &a);
4099 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4101 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4103 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4105 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4108 if (X(win) + BORDER(win) < X(r)) {
4110 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4112 X(win) = X(r) - BORDER(win);
4115 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4117 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4119 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4122 if (Y(win) + BORDER(win) < Y(r)) {
4124 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4126 Y(win) = Y(r) - BORDER(win);
4132 if (HEIGHT(win) < 1)
4138 update_window(struct ws_win *win)
4143 bzero(&wc, sizeof wc);
4144 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
4146 wc.border_width = BORDER(win);
4149 wc.width = WIDTH(win);
4150 wc.height = HEIGHT(win);
4152 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%lx, (x,y) w x h: "
4153 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc.x, wc.y, wc.width,
4154 wc.height, YESNO(win->bordered));
4156 XConfigureWindow(display, win->id, mask, &wc);
4159 #define SWM_RESIZE_STEPS (50)
4162 resize(struct ws_win *win, union arg *args)
4166 struct swm_region *r = NULL;
4167 int resize_step = 0;
4171 struct swm_geometry g;
4172 int top = 0, left = 0;
4175 unsigned int shape; /* cursor style */
4181 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4184 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
4185 "transient: 0x%lx\n", win->id, YESNO(win->floating),
4188 if (!(win->transient != 0 || win->floating != 0))
4191 /* reject resizes in max mode for floaters (transient ok) */
4192 if (win->floatmaxed)
4196 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4202 case SWM_ARG_ID_WIDTHSHRINK:
4203 WIDTH(win) -= SWM_RESIZE_STEPS;
4206 case SWM_ARG_ID_WIDTHGROW:
4207 WIDTH(win) += SWM_RESIZE_STEPS;
4210 case SWM_ARG_ID_HEIGHTSHRINK:
4211 HEIGHT(win) -= SWM_RESIZE_STEPS;
4214 case SWM_ARG_ID_HEIGHTGROW:
4215 HEIGHT(win) += SWM_RESIZE_STEPS;
4222 constrain_window(win, r, 1);
4224 store_float_geom(win,r);
4228 if (focus_mode == SWM_FOCUS_DEFAULT)
4229 drain_enter_notify();
4231 /* get cursor offset from window root */
4232 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4237 if (wx < WIDTH(win) / 2)
4240 if (wy < HEIGHT(win) / 2)
4243 if (args->id == SWM_ARG_ID_CENTER)
4246 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4248 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4250 cursor = XCreateFontCursor(display, shape);
4252 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4253 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
4254 XFreeCursor(display, cursor);
4259 XMaskEvent(display, MOUSEMASK | ExposureMask |
4260 SubstructureRedirectMask, &ev);
4262 case ConfigureRequest:
4265 handler[ev.type](&ev);
4268 /* cursor offset/delta from start of the operation */
4269 dx = ev.xmotion.x_root - x;
4270 dy = ev.xmotion.y_root - y;
4276 if (args->id == SWM_ARG_ID_CENTER) {
4277 if (g.h / 2 + dy < 1)
4281 HEIGHT(win) = g.h + 2 * dy;
4289 HEIGHT(win) = g.h + dy;
4296 if (args->id == SWM_ARG_ID_CENTER) {
4297 if (g.w / 2 + dx < 1)
4301 WIDTH(win) = g.w + 2 * dx;
4309 WIDTH(win) = g.w + dx;
4312 constrain_window(win, r, 1);
4314 /* not free, don't sync more than 120 times / second */
4315 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4316 time = ev.xmotion.time;
4317 XSync(display, False);
4322 } while (ev.type != ButtonRelease);
4324 XSync(display, False);
4327 store_float_geom(win,r);
4329 XUngrabPointer(display, CurrentTime);
4330 XFreeCursor(display, cursor);
4333 drain_enter_notify();
4337 resize_step(struct swm_region *r, union arg *args)
4339 struct ws_win *win = NULL;
4341 if (r && r->ws && r->ws->focus)
4349 #define SWM_MOVE_STEPS (50)
4352 move(struct ws_win *win, union arg *args)
4357 struct swm_region *r = NULL;
4367 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4370 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4371 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4373 /* in max_stack mode should only move transients */
4374 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4378 if (win->floating == 0 && !win->transient) {
4379 store_float_geom(win, r);
4380 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4383 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4390 case SWM_ARG_ID_MOVELEFT:
4391 X(win) -= (SWM_MOVE_STEPS - border_width);
4394 case SWM_ARG_ID_MOVERIGHT:
4395 X(win) += (SWM_MOVE_STEPS - border_width);
4398 case SWM_ARG_ID_MOVEUP:
4399 Y(win) -= (SWM_MOVE_STEPS - border_width);
4402 case SWM_ARG_ID_MOVEDOWN:
4403 Y(win) += (SWM_MOVE_STEPS - border_width);
4410 constrain_window(win, r, 0);
4412 store_float_geom(win, r);
4416 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4417 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4418 CurrentTime) != GrabSuccess)
4421 /* get cursor offset from window root */
4422 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4426 XMaskEvent(display, MOUSEMASK | ExposureMask |
4427 SubstructureRedirectMask, &ev);
4429 case ConfigureRequest:
4432 handler[ev.type](&ev);
4435 X(win) = ev.xmotion.x_root - wx - border_width;
4436 Y(win) = ev.xmotion.y_root - wy - border_width;
4438 constrain_window(win, r, 0);
4440 /* not free, don't sync more than 120 times / second */
4441 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4442 time = ev.xmotion.time;
4443 XSync(display, False);
4448 } while (ev.type != ButtonRelease);
4450 XSync(display, False);
4453 store_float_geom(win, r);
4454 XUngrabPointer(display, CurrentTime);
4457 drain_enter_notify();
4461 move_step(struct swm_region *r, union arg *args)
4463 struct ws_win *win = NULL;
4465 if (r && r->ws && r->ws->focus)
4470 if (!(win->transient != 0 || win->floating != 0))
4477 /* user/key callable function IDs */
4566 kf_search_workspace,
4568 kf_dumpwins, /* MUST BE LAST */
4572 /* key definitions */
4574 dummykeyfunc(struct swm_region *r, union arg *args)
4579 char name[SWM_FUNCNAME_LEN];
4580 void (*func)(struct swm_region *r, union arg *);
4582 } keyfuncs[kf_invalid + 1] = {
4583 /* name function argument */
4584 { "cycle_layout", cycle_layout, {0} },
4585 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4586 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4587 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4588 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4589 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4590 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4591 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4592 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4593 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4594 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4595 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4596 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4597 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4598 { "quit", quit, {0} },
4599 { "restart", restart, {0} },
4600 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4601 { "ws_1", switchws, {.id = 0} },
4602 { "ws_2", switchws, {.id = 1} },
4603 { "ws_3", switchws, {.id = 2} },
4604 { "ws_4", switchws, {.id = 3} },
4605 { "ws_5", switchws, {.id = 4} },
4606 { "ws_6", switchws, {.id = 5} },
4607 { "ws_7", switchws, {.id = 6} },
4608 { "ws_8", switchws, {.id = 7} },
4609 { "ws_9", switchws, {.id = 8} },
4610 { "ws_10", switchws, {.id = 9} },
4611 { "ws_11", switchws, {.id = 10} },
4612 { "ws_12", switchws, {.id = 11} },
4613 { "ws_13", switchws, {.id = 12} },
4614 { "ws_14", switchws, {.id = 13} },
4615 { "ws_15", switchws, {.id = 14} },
4616 { "ws_16", switchws, {.id = 15} },
4617 { "ws_17", switchws, {.id = 16} },
4618 { "ws_18", switchws, {.id = 17} },
4619 { "ws_19", switchws, {.id = 18} },
4620 { "ws_20", switchws, {.id = 19} },
4621 { "ws_21", switchws, {.id = 20} },
4622 { "ws_22", switchws, {.id = 21} },
4623 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4624 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4625 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4626 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4627 { "ws_prior", priorws, {0} },
4628 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4629 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4630 { "mvws_1", send_to_ws, {.id = 0} },
4631 { "mvws_2", send_to_ws, {.id = 1} },
4632 { "mvws_3", send_to_ws, {.id = 2} },
4633 { "mvws_4", send_to_ws, {.id = 3} },
4634 { "mvws_5", send_to_ws, {.id = 4} },
4635 { "mvws_6", send_to_ws, {.id = 5} },
4636 { "mvws_7", send_to_ws, {.id = 6} },
4637 { "mvws_8", send_to_ws, {.id = 7} },
4638 { "mvws_9", send_to_ws, {.id = 8} },
4639 { "mvws_10", send_to_ws, {.id = 9} },
4640 { "mvws_11", send_to_ws, {.id = 10} },
4641 { "mvws_12", send_to_ws, {.id = 11} },
4642 { "mvws_13", send_to_ws, {.id = 12} },
4643 { "mvws_14", send_to_ws, {.id = 13} },
4644 { "mvws_15", send_to_ws, {.id = 14} },
4645 { "mvws_16", send_to_ws, {.id = 15} },
4646 { "mvws_17", send_to_ws, {.id = 16} },
4647 { "mvws_18", send_to_ws, {.id = 17} },
4648 { "mvws_19", send_to_ws, {.id = 18} },
4649 { "mvws_20", send_to_ws, {.id = 19} },
4650 { "mvws_21", send_to_ws, {.id = 20} },
4651 { "mvws_22", send_to_ws, {.id = 21} },
4652 { "bar_toggle", bar_toggle, {0} },
4653 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4654 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4655 { "float_toggle", floating_toggle,{0} },
4656 { "version", version, {0} },
4657 { "spawn_custom", dummykeyfunc, {0} },
4658 { "iconify", iconify, {0} },
4659 { "uniconify", uniconify, {0} },
4660 { "raise_toggle", raise_toggle, {0} },
4661 { "button2", pressbutton, {2} },
4662 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4663 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4664 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4665 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4666 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4667 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4668 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4669 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4670 { "name_workspace", name_workspace, {0} },
4671 { "search_workspace", search_workspace, {0} },
4672 { "search_win", search_win, {0} },
4673 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4674 { "invalid key func", NULL, {0} },
4677 RB_ENTRY(key) entry;
4680 enum keyfuncid funcid;
4683 RB_HEAD(key_tree, key);
4686 key_cmp(struct key *kp1, struct key *kp2)
4688 if (kp1->keysym < kp2->keysym)
4690 if (kp1->keysym > kp2->keysym)
4693 if (kp1->mod < kp2->mod)
4695 if (kp1->mod > kp2->mod)
4701 RB_GENERATE(key_tree, key, entry, key_cmp);
4702 struct key_tree keys;
4705 enum { client_click, root_click };
4707 unsigned int action;
4709 unsigned int button;
4710 void (*func)(struct ws_win *, union arg *);
4713 /* action key mouse button func args */
4714 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4715 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4716 { client_click, MODKEY, Button1, move, {0} },
4720 update_modkey(unsigned int mod)
4726 RB_FOREACH(kp, key_tree, &keys)
4727 if (kp->mod & ShiftMask)
4728 kp->mod = mod | ShiftMask;
4732 for (i = 0; i < LENGTH(buttons); i++)
4733 if (buttons[i].mask & ShiftMask)
4734 buttons[i].mask = mod | ShiftMask;
4736 buttons[i].mask = mod;
4741 TAILQ_ENTRY(spawn_prog) entry;
4746 TAILQ_HEAD(spawn_list, spawn_prog);
4747 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4750 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4753 struct spawn_prog *prog = NULL;
4755 char *ap, **real_args;
4757 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4760 TAILQ_FOREACH(prog, &spawns, entry) {
4761 if (!strcasecmp(spawn_name, prog->name))
4765 warnx("spawn_custom: program %s not found", spawn_name);
4769 /* make room for expanded args */
4770 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4771 err(1, "spawn_custom: calloc real_args");
4773 /* expand spawn_args into real_args */
4774 for (i = 0; i < prog->argc; i++) {
4776 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4777 if (!strcasecmp(ap, "$bar_border")) {
4779 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4781 err(1, "spawn_custom border color");
4782 } else if (!strcasecmp(ap, "$bar_color")) {
4784 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4786 err(1, "spawn_custom bar color");
4787 } else if (!strcasecmp(ap, "$bar_font")) {
4788 if ((real_args[i] = strdup(bar_fonts))
4790 err(1, "spawn_custom bar fonts");
4791 } else if (!strcasecmp(ap, "$bar_font_color")) {
4793 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4795 err(1, "spawn_custom color font");
4796 } else if (!strcasecmp(ap, "$color_focus")) {
4798 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4800 err(1, "spawn_custom color focus");
4801 } else if (!strcasecmp(ap, "$color_unfocus")) {
4803 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4805 err(1, "spawn_custom color unfocus");
4807 /* no match --> copy as is */
4808 if ((real_args[i] = strdup(ap)) == NULL)
4809 err(1, "spawn_custom strdup(ap)");
4811 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4816 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4817 for (i = 0; i < prog->argc; i++)
4818 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4819 DNPRINTF(SWM_D_SPAWN, "\n");
4821 *ret_args = real_args;
4822 return (prog->argc);
4826 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4832 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4836 spawn(r->ws->idx, &a, 1);
4838 for (i = 0; i < spawn_argc; i++)
4844 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4850 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4854 if (pipe(select_list_pipe) == -1)
4855 err(1, "pipe error");
4856 if (pipe(select_resp_pipe) == -1)
4857 err(1, "pipe error");
4859 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4860 err(1, "could not disable SIGPIPE");
4861 switch (*pid = fork()) {
4863 err(1, "cannot fork");
4866 if (dup2(select_list_pipe[0], 0) == -1)
4868 if (dup2(select_resp_pipe[1], 1) == -1)
4870 close(select_list_pipe[1]);
4871 close(select_resp_pipe[0]);
4872 spawn(r->ws->idx, &a, 0);
4874 default: /* parent */
4875 close(select_list_pipe[0]);
4876 close(select_resp_pipe[1]);
4880 for (i = 0; i < spawn_argc; i++)
4886 spawn_insert(char *name, char *args)
4888 char *arg, *cp, *ptr;
4889 struct spawn_prog *sp;
4891 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4893 if ((sp = calloc(1, sizeof *sp)) == NULL)
4894 err(1, "spawn_insert: malloc");
4895 if ((sp->name = strdup(name)) == NULL)
4896 err(1, "spawn_insert: strdup");
4898 /* convert the arguments to an argument list */
4899 if ((ptr = cp = strdup(args)) == NULL)
4900 err(1, "spawn_insert: strdup");
4901 while ((arg = strsep(&ptr, " \t")) != NULL) {
4902 /* empty field; skip it */
4907 if ((sp->argv = realloc(sp->argv, sp->argc *
4908 sizeof *sp->argv)) == NULL)
4909 err(1, "spawn_insert: realloc");
4910 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4911 err(1, "spawn_insert: strdup");
4915 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4916 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4920 spawn_remove(struct spawn_prog *sp)
4924 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4926 TAILQ_REMOVE(&spawns, sp, entry);
4927 for (i = 0; i < sp->argc; i++)
4933 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4937 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4939 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4942 spawn_insert(name, args);
4944 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4948 setspawn(char *name, char *args)
4950 struct spawn_prog *sp;
4952 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4957 TAILQ_FOREACH(sp, &spawns, entry) {
4958 if (!strcmp(sp->name, name)) {
4962 spawn_replace(sp, name, args);
4963 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4967 if (*args == '\0') {
4968 warnx("error: setspawn: cannot find program: %s", name);
4972 spawn_insert(name, args);
4973 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4977 setconfspawn(char *selector, char *value, int flags)
4979 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4981 setspawn(selector, value);
4983 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4990 setconfspawn("term", "xterm", 0);
4991 setconfspawn("spawn_term", "xterm", 0);
4992 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4993 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4994 setconfspawn("lock", "xlock", 0);
4995 setconfspawn("initscr", "initscreen.sh", 0);
4996 setconfspawn("menu", "dmenu_run"
4999 " -nf $bar_font_color"
5001 " -sf $bar_color", 0);
5002 setconfspawn("search", "dmenu"
5006 " -nf $bar_font_color"
5008 " -sf $bar_color", 0);
5009 setconfspawn("name_workspace", "dmenu"
5013 " -nf $bar_font_color"
5015 " -sf $bar_color", 0);
5019 #define SWM_MODNAME_SIZE 32
5020 #define SWM_KEY_WS "\n+ \t"
5022 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5026 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5027 if (mod == NULL || ks == NULL) {
5028 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5031 if (keystr == NULL || strlen(keystr) == 0) {
5032 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5038 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5039 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5041 cp += (long)strspn(cp, SWM_KEY_WS);
5042 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5044 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5046 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5048 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5050 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5052 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5054 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5055 *mod |= ControlMask;
5057 *ks = XStringToKeysym(name);
5058 XConvertCase(*ks, ks, &uks);
5059 if (ks == NoSymbol) {
5061 "parsekeys: invalid key %s\n",
5067 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5072 strdupsafe(char *str)
5077 return (strdup(str));
5081 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5085 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5086 keyfuncs[kfid].name, spawn_name);
5088 if ((kp = malloc(sizeof *kp)) == NULL)
5089 err(1, "key_insert: malloc");
5094 kp->spawn_name = strdupsafe(spawn_name);
5095 RB_INSERT(key_tree, &keys, kp);
5097 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5101 key_lookup(unsigned int mod, KeySym ks)
5108 return (RB_FIND(key_tree, &keys, &kp));
5112 key_remove(struct key *kp)
5114 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5116 RB_REMOVE(key_tree, &keys, kp);
5117 free(kp->spawn_name);
5120 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5124 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5127 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5131 key_insert(mod, ks, kfid, spawn_name);
5133 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5137 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5142 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5143 keyfuncs[kfid].name, spawn_name);
5145 if ((kp = key_lookup(mod, ks)) != NULL) {
5146 if (kfid == kf_invalid)
5149 key_replace(kp, mod, ks, kfid, spawn_name);
5150 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5153 if (kfid == kf_invalid) {
5154 warnx("error: setkeybinding: cannot find mod/key combination");
5155 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5159 key_insert(mod, ks, kfid, spawn_name);
5160 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5164 setconfbinding(char *selector, char *value, int flags)
5166 enum keyfuncid kfid;
5169 struct spawn_prog *sp;
5170 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5171 if (selector == NULL) {
5172 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5173 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5175 setkeybinding(mod, ks, kfid, NULL);
5180 /* search by key function name */
5181 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5182 if (strncasecmp(selector, keyfuncs[kfid].name,
5183 SWM_FUNCNAME_LEN) == 0) {
5184 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5186 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5187 setkeybinding(mod, ks, kfid, NULL);
5193 /* search by custom spawn name */
5194 TAILQ_FOREACH(sp, &spawns, entry) {
5195 if (strcasecmp(selector, sp->name) == 0) {
5196 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5198 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5199 setkeybinding(mod, ks, kf_spawn_custom,
5206 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5213 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5214 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5215 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5216 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5217 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5218 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5219 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5220 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5221 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5222 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5223 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5224 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5225 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5226 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5227 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5228 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5229 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5230 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5231 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5232 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5233 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5234 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5235 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5236 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5237 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5238 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5239 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5240 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5241 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5242 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5243 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5244 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5245 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5246 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5247 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5248 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5249 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5250 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5251 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5252 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5253 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5254 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5255 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5256 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5257 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5258 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5259 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5260 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5261 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5262 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5263 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5264 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5265 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5266 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5267 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5268 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5269 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5270 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5271 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5272 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5273 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5274 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5275 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5276 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5277 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5278 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5279 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5280 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5281 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5282 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5283 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5284 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5285 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5286 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5287 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5288 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5289 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5290 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5291 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5292 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5293 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5294 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5295 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5296 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5297 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5298 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5299 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5300 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5301 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5302 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5303 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5304 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5305 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5306 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5307 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5308 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5310 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5319 while (RB_EMPTY(&keys) == 0) {
5320 kp = RB_ROOT(&keys);
5326 setkeymapping(char *selector, char *value, int flags)
5328 char keymapping_file[PATH_MAX];
5329 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5330 if (value[0] == '~')
5331 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5332 pwd->pw_dir, &value[1]);
5334 strlcpy(keymapping_file, value, sizeof keymapping_file);
5336 /* load new key bindings; if it fails, revert to default bindings */
5337 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5341 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5346 updatenumlockmask(void)
5349 XModifierKeymap *modmap;
5351 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5353 modmap = XGetModifierMapping(display);
5354 for (i = 0; i < 8; i++)
5355 for (j = 0; j < modmap->max_keypermod; j++)
5356 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5357 == XKeysymToKeycode(display, XK_Num_Lock))
5358 numlockmask = (1 << i);
5360 XFreeModifiermap(modmap);
5368 unsigned int modifiers[] =
5369 { 0, LockMask, numlockmask, numlockmask | LockMask };
5372 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5373 updatenumlockmask();
5375 for (k = 0; k < ScreenCount(display); k++) {
5376 if (TAILQ_EMPTY(&screens[k].rl))
5378 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
5379 RB_FOREACH(kp, key_tree, &keys) {
5380 if ((code = XKeysymToKeycode(display, kp->keysym)))
5381 for (j = 0; j < LENGTH(modifiers); j++)
5382 XGrabKey(display, code,
5383 kp->mod | modifiers[j],
5384 screens[k].root, True,
5385 GrabModeAsync, GrabModeAsync);
5391 grabbuttons(struct ws_win *win, int focused)
5394 unsigned int modifiers[] =
5395 { 0, LockMask, numlockmask, numlockmask|LockMask };
5397 updatenumlockmask();
5398 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5400 for (i = 0; i < LENGTH(buttons); i++)
5401 if (buttons[i].action == client_click)
5402 for (j = 0; j < LENGTH(modifiers); j++)
5403 XGrabButton(display, buttons[i].button,
5404 buttons[i].mask | modifiers[j],
5405 win->id, False, BUTTONMASK,
5406 GrabModeAsync, GrabModeSync, None,
5409 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5410 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5413 const char *quirkname[] = {
5414 "NONE", /* config string for "no value" */
5423 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5424 #define SWM_Q_WS "\n|+ \t"
5426 parsequirks(char *qstr, unsigned long *quirk)
5436 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5438 cp += (long)strspn(cp, SWM_Q_WS);
5439 for (i = 0; i < LENGTH(quirkname); i++) {
5440 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5441 DNPRINTF(SWM_D_QUIRK,
5442 "parsequirks: %s\n", name);
5447 *quirk |= 1 << (i-1);
5451 if (i >= LENGTH(quirkname)) {
5452 DNPRINTF(SWM_D_QUIRK,
5453 "parsequirks: invalid quirk [%s]\n", name);
5461 quirk_insert(const char *class, const char *name, unsigned long quirk)
5465 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5468 if ((qp = malloc(sizeof *qp)) == NULL)
5469 err(1, "quirk_insert: malloc");
5470 if ((qp->class = strdup(class)) == NULL)
5471 err(1, "quirk_insert: strdup");
5472 if ((qp->name = strdup(name)) == NULL)
5473 err(1, "quirk_insert: strdup");
5476 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5478 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5482 quirk_remove(struct quirk *qp)
5484 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5485 qp->name, qp->quirk);
5487 TAILQ_REMOVE(&quirks, qp, entry);
5492 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5496 quirk_replace(struct quirk *qp, const char *class, const char *name,
5497 unsigned long quirk)
5499 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5500 qp->name, qp->quirk);
5503 quirk_insert(class, name, quirk);
5505 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5509 setquirk(const char *class, const char *name, unsigned long quirk)
5513 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5516 TAILQ_FOREACH(qp, &quirks, entry) {
5517 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5521 quirk_replace(qp, class, name, quirk);
5522 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5527 warnx("error: setquirk: cannot find class/name combination");
5531 quirk_insert(class, name, quirk);
5532 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5536 setconfquirk(char *selector, char *value, int flags)
5538 char *cp, *class, *name;
5540 unsigned long quirks;
5541 if (selector == NULL)
5543 if ((cp = strchr(selector, ':')) == NULL)
5548 if ((retval = parsequirks(value, &quirks)) == 0)
5549 setquirk(class, name, quirks);
5556 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5557 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5558 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5559 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5560 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5561 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5562 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5563 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5564 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5565 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5566 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5567 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5568 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5571 /* conf file stuff */
5572 #define SWM_CONF_FILE "spectrwm.conf"
5573 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5577 SWM_S_BAR_AT_BOTTOM,
5578 SWM_S_BAR_BORDER_WIDTH,
5585 SWM_S_CLOCK_ENABLED,
5588 SWM_S_CYCLE_VISIBLE,
5590 SWM_S_DISABLE_BORDER,
5592 SWM_S_FOCUS_CLOSE_WRAP,
5593 SWM_S_FOCUS_DEFAULT,
5599 SWM_S_STACK_ENABLED,
5601 SWM_S_TITLE_CLASS_ENABLED,
5602 SWM_S_TITLE_NAME_ENABLED,
5603 SWM_S_URGENT_ENABLED,
5604 SWM_S_VERBOSE_LAYOUT,
5605 SWM_S_WINDOW_NAME_ENABLED,
5606 SWM_S_WORKSPACE_LIMIT
5610 setconfvalue(char *selector, char *value, int flags)
5616 case SWM_S_BAR_ACTION:
5618 if ((bar_argv[0] = strdup(value)) == NULL)
5619 err(1, "setconfvalue: bar_action");
5621 case SWM_S_BAR_AT_BOTTOM:
5622 bar_at_bottom = atoi(value);
5624 case SWM_S_BAR_BORDER_WIDTH:
5625 bar_border_width = atoi(value);
5626 if (bar_border_width < 0)
5627 bar_border_width = 0;
5629 case SWM_S_BAR_DELAY:
5630 bar_delay = atoi(value);
5632 case SWM_S_BAR_ENABLED:
5633 bar_enabled = atoi(value);
5635 case SWM_S_BAR_FONT:
5637 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5638 err(1, "setconfvalue: asprintf: failed to allocate "
5639 "memory for bar_fonts.");
5643 case SWM_S_BAR_FORMAT:
5645 if ((bar_format = strdup(value)) == NULL)
5646 err(1, "setconfvalue: bar_format");
5648 case SWM_S_BAR_JUSTIFY:
5649 if (!strcmp(value, "left"))
5650 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5651 else if (!strcmp(value, "center"))
5652 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5653 else if (!strcmp(value, "right"))
5654 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5656 errx(1, "invalid bar_justify");
5658 case SWM_S_BORDER_WIDTH:
5659 border_width = atoi(value);
5660 if (border_width < 0)
5663 case SWM_S_CLOCK_ENABLED:
5664 clock_enabled = atoi(value);
5666 case SWM_S_CLOCK_FORMAT:
5667 #ifndef SWM_DENY_CLOCK_FORMAT
5669 if ((clock_format = strdup(value)) == NULL)
5670 err(1, "setconfvalue: clock_format");
5673 case SWM_S_CYCLE_EMPTY:
5674 cycle_empty = atoi(value);
5676 case SWM_S_CYCLE_VISIBLE:
5677 cycle_visible = atoi(value);
5679 case SWM_S_DIALOG_RATIO:
5680 dialog_ratio = atof(value);
5681 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5684 case SWM_S_DISABLE_BORDER:
5685 disable_border = atoi(value);
5687 case SWM_S_FOCUS_CLOSE:
5688 if (!strcmp(value, "first"))
5689 focus_close = SWM_STACK_BOTTOM;
5690 else if (!strcmp(value, "last"))
5691 focus_close = SWM_STACK_TOP;
5692 else if (!strcmp(value, "next"))
5693 focus_close = SWM_STACK_ABOVE;
5694 else if (!strcmp(value, "previous"))
5695 focus_close = SWM_STACK_BELOW;
5697 errx(1, "focus_close");
5699 case SWM_S_FOCUS_CLOSE_WRAP:
5700 focus_close_wrap = atoi(value);
5702 case SWM_S_FOCUS_DEFAULT:
5703 if (!strcmp(value, "last"))
5704 focus_default = SWM_STACK_TOP;
5705 else if (!strcmp(value, "first"))
5706 focus_default = SWM_STACK_BOTTOM;
5708 errx(1, "focus_default");
5710 case SWM_S_FOCUS_MODE:
5711 if (!strcmp(value, "default"))
5712 focus_mode = SWM_FOCUS_DEFAULT;
5713 else if (!strcmp(value, "follow_cursor"))
5714 focus_mode = SWM_FOCUS_FOLLOW;
5715 else if (!strcmp(value, "synergy"))
5716 focus_mode = SWM_FOCUS_SYNERGY;
5718 errx(1, "focus_mode");
5720 case SWM_S_SPAWN_ORDER:
5721 if (!strcmp(value, "first"))
5722 spawn_position = SWM_STACK_BOTTOM;
5723 else if (!strcmp(value, "last"))
5724 spawn_position = SWM_STACK_TOP;
5725 else if (!strcmp(value, "next"))
5726 spawn_position = SWM_STACK_ABOVE;
5727 else if (!strcmp(value, "previous"))
5728 spawn_position = SWM_STACK_BELOW;
5730 errx(1, "spawn_position");
5732 case SWM_S_SPAWN_TERM:
5733 setconfspawn("term", value, 0);
5734 setconfspawn("spawn_term", value, 0);
5738 case SWM_S_SS_ENABLED:
5739 ss_enabled = atoi(value);
5741 case SWM_S_STACK_ENABLED:
5742 stack_enabled = atoi(value);
5744 case SWM_S_TERM_WIDTH:
5745 term_width = atoi(value);
5749 case SWM_S_TITLE_CLASS_ENABLED:
5750 title_class_enabled = atoi(value);
5752 case SWM_S_TITLE_NAME_ENABLED:
5753 title_name_enabled = atoi(value);
5755 case SWM_S_URGENT_ENABLED:
5756 urgent_enabled = atoi(value);
5758 case SWM_S_VERBOSE_LAYOUT:
5759 verbose_layout = atoi(value);
5760 for (i = 0; layouts[i].l_stack != NULL; i++) {
5762 layouts[i].l_string = fancy_stacker;
5764 layouts[i].l_string = plain_stacker;
5767 case SWM_S_WINDOW_NAME_ENABLED:
5768 window_name_enabled = atoi(value);
5770 case SWM_S_WORKSPACE_LIMIT:
5771 workspace_limit = atoi(value);
5772 if (workspace_limit > SWM_WS_MAX)
5773 workspace_limit = SWM_WS_MAX;
5774 else if (workspace_limit < 1)
5775 workspace_limit = 1;
5784 setconfmodkey(char *selector, char *value, int flags)
5786 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5787 update_modkey(Mod1Mask);
5788 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5789 update_modkey(Mod2Mask);
5790 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5791 update_modkey(Mod3Mask);
5792 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5793 update_modkey(Mod4Mask);
5800 setconfcolor(char *selector, char *value, int flags)
5802 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5807 setconfregion(char *selector, char *value, int flags)
5809 custom_region(value);
5814 setautorun(char *selector, char *value, int flags)
5824 if (getenv("SWM_STARTED"))
5828 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5829 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5831 if (ws_id < 0 || ws_id >= workspace_limit)
5832 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5835 * This is a little intricate
5837 * If the pid already exists we simply reuse it because it means it was
5838 * used before AND not claimed by manage_window. We get away with
5839 * altering it in the parent after INSERT because this can not be a race
5842 while ((ap = strsep(&sp, " \t")) != NULL) {
5845 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5847 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5848 err(1, "setautorun: realloc");
5849 a.argv[argc - 1] = ap;
5852 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5853 err(1, "setautorun: realloc");
5854 a.argv[argc] = NULL;
5856 if ((pid = fork()) == 0) {
5857 spawn(ws_id, &a, 1);
5866 p = calloc(1, sizeof *p);
5869 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5879 setlayout(char *selector, char *value, int flags)
5881 int ws_id, i, x, mg, ma, si, raise, f = 0;
5882 int st = SWM_V_STACK;
5884 struct workspace *ws;
5886 if (getenv("SWM_STARTED"))
5890 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5891 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5892 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5893 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5896 if (ws_id < 0 || ws_id >= workspace_limit)
5897 errx(1, "layout: invalid workspace %d", ws_id + 1);
5899 if (!strcasecmp(s, "vertical"))
5901 else if (!strcasecmp(s, "vertical_flip")) {
5904 } else if (!strcasecmp(s, "horizontal"))
5906 else if (!strcasecmp(s, "horizontal_flip")) {
5909 } else if (!strcasecmp(s, "fullscreen"))
5912 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5913 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5916 for (i = 0; i < ScreenCount(display); i++) {
5917 ws = (struct workspace *)&screens[i].ws;
5918 ws[ws_id].cur_layout = &layouts[st];
5920 ws[ws_id].always_raise = raise;
5921 if (st == SWM_MAX_STACK)
5925 for (x = 0; x < abs(mg); x++) {
5926 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5927 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5928 SWM_ARG_ID_MASTERSHRINK);
5932 for (x = 0; x < abs(ma); x++) {
5933 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5934 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5935 SWM_ARG_ID_MASTERDEL);
5939 for (x = 0; x < abs(si); x++) {
5940 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5941 si >= 0 ? SWM_ARG_ID_STACKINC :
5942 SWM_ARG_ID_STACKDEC);
5947 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5948 SWM_ARG_ID_FLIPLAYOUT);
5956 /* config options */
5957 struct config_option {
5959 int (*func)(char*, char*, int);
5962 struct config_option configopt[] = {
5963 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5964 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5965 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5966 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5967 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5968 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5969 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5970 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5971 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5972 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5973 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
5974 { "keyboard_mapping", setkeymapping, 0 },
5975 { "bind", setconfbinding, 0 },
5976 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5977 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5978 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5979 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5980 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5981 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5982 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5983 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
5984 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5985 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5986 { "modkey", setconfmodkey, 0 },
5987 { "program", setconfspawn, 0 },
5988 { "quirk", setconfquirk, 0 },
5989 { "region", setconfregion, 0 },
5990 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5991 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5992 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5993 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5994 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5995 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5996 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5997 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5998 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5999 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6000 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6001 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6002 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6003 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6004 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6005 { "autorun", setautorun, 0 },
6006 { "layout", setlayout, 0 },
6011 conf_load(char *filename, int keymapping)
6014 char *line, *cp, *optsub, *optval;
6015 size_t linelen, lineno = 0;
6016 int wordlen, i, optind;
6017 struct config_option *opt;
6019 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6021 if (filename == NULL) {
6022 warnx("conf_load: no filename");
6025 if ((config = fopen(filename, "r")) == NULL) {
6026 warn("conf_load: fopen: %s", filename);
6030 while (!feof(config)) {
6031 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6034 err(1, "%s", filename);
6039 cp += strspn(cp, " \t\n"); /* eat whitespace */
6040 if (cp[0] == '\0') {
6045 /* get config option */
6046 wordlen = strcspn(cp, "=[ \t\n");
6048 warnx("%s: line %zd: no option found",
6053 for (i = 0; i < LENGTH(configopt); i++) {
6054 opt = &configopt[i];
6055 if (!strncasecmp(cp, opt->optname, wordlen) &&
6056 strlen(opt->optname) == wordlen) {
6062 warnx("%s: line %zd: unknown option %.*s",
6063 filename, lineno, wordlen, cp);
6066 if (keymapping && strcmp(opt->optname, "bind")) {
6067 warnx("%s: line %zd: invalid option %.*s",
6068 filename, lineno, wordlen, cp);
6072 cp += strspn(cp, " \t\n"); /* eat whitespace */
6073 /* get [selector] if any */
6077 wordlen = strcspn(cp, "]");
6080 warnx("%s: line %zd: syntax error",
6085 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6087 warnx("%s: line %zd: unable to allocate"
6088 "memory for selector", filename,
6094 cp += strspn(cp, "] \t\n"); /* eat trailing */
6096 cp += strspn(cp, "= \t\n"); /* eat trailing */
6098 optval = strdup(cp);
6099 /* call function to deal with it all */
6100 if (configopt[optind].func(optsub, optval,
6101 configopt[optind].funcflags) != 0)
6102 errx(1, "%s: line %zd: invalid data for %s",
6103 filename, lineno, configopt[optind].optname);
6110 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6117 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6123 set_child_transient(struct ws_win *win, Window *trans)
6125 struct ws_win *parent, *w;
6126 XWMHints *wmh = NULL;
6127 struct swm_region *r;
6128 struct workspace *ws;
6130 parent = find_window(win->transient);
6132 parent->child_trans = win;
6134 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6135 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
6137 if (win->hints == NULL) {
6138 warnx("no hints for 0x%lx", win->id);
6142 r = root_to_region(win->wa.root);
6144 /* parent doen't exist in our window list */
6145 TAILQ_FOREACH(w, &ws->winlist, entry) {
6149 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
6150 warnx("can't get hints for 0x%lx", w->id);
6154 if (win->hints->window_group != wmh->window_group)
6157 w->child_trans = win;
6158 win->transient = w->id;
6160 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
6161 "transient to 0x%lx\n", win->transient);
6171 window_get_pid(Window win)
6173 Atom actual_type_return;
6174 int actual_format_return = 0;
6175 unsigned long nitems_return = 0;
6176 unsigned long bytes_after_return = 0;
6180 unsigned char *prop = NULL;
6182 if (XGetWindowProperty(display, win,
6183 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
6184 XA_CARDINAL, &actual_type_return, &actual_format_return,
6185 &nitems_return, &bytes_after_return,
6186 (unsigned char**)(void*)&pid) != Success)
6188 if (actual_type_return != XA_CARDINAL)
6199 if (XGetWindowProperty(display, win,
6200 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
6201 XA_STRING, &actual_type_return, &actual_format_return,
6202 &nitems_return, &bytes_after_return, &prop) != Success)
6204 if (actual_type_return != XA_STRING)
6209 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
6210 /* ignore error because strtonum returns 0 anyway */
6217 manage_window(Window id)
6220 struct workspace *ws;
6221 struct ws_win *win, *ww;
6222 int format, i, ws_idx, n, border_me = 0;
6223 unsigned long nitems, bytes;
6224 Atom ws_idx_atom = 0, type;
6225 Atom *prot = NULL, *pp;
6226 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6227 struct swm_region *r;
6232 if ((win = find_window(id)) != NULL)
6233 return (win); /* already being managed */
6235 /* see if we are on the unmanaged list */
6236 if ((win = find_unmanaged_window(id)) != NULL) {
6237 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6238 "window: 0x%lx\n", win->id);
6239 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6241 set_child_transient(win, &trans);
6243 if (trans && (ww = find_window(trans)))
6244 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6245 else if ((ww = win->ws->focus) &&
6246 spawn_position == SWM_STACK_ABOVE)
6247 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6249 else if (ww && spawn_position == SWM_STACK_BELOW)
6250 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6251 else switch (spawn_position) {
6254 case SWM_STACK_ABOVE:
6255 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6257 case SWM_STACK_BOTTOM:
6258 case SWM_STACK_BELOW:
6259 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6262 ewmh_update_actions(win);
6266 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6267 err(1, "manage_window: calloc: failed to allocate memory for "
6273 /* see if we need to override the workspace */
6274 p = find_pid(window_get_pid(id));
6276 /* Get all the window data in one shot */
6277 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
6279 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
6280 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
6282 XGetWindowAttributes(display, id, &win->wa);
6283 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6284 win->hints = XGetWMHints(display, id);
6285 XGetTransientForHint(display, id, &trans);
6287 win->transient = trans;
6288 set_child_transient(win, &trans);
6289 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
6290 "transient: 0x%lx\n", win->id, win->transient);
6293 /* get supported protocols */
6294 if (XGetWMProtocols(display, id, &prot, &n)) {
6295 for (i = 0, pp = prot; i < n; i++, pp++) {
6296 if (*pp == takefocus)
6297 win->take_focus = 1;
6299 win->can_delete = 1;
6305 win->iconic = get_iconic(win);
6308 * Figure out where to put the window. If it was previously assigned to
6309 * a workspace (either by spawn() or manually moving), and isn't
6310 * transient, * put it in the same workspace
6312 r = root_to_region(win->wa.root);
6314 ws = &r->s->ws[p->ws];
6315 TAILQ_REMOVE(&pidlist, p, entry);
6318 } else if (prop && win->transient == 0) {
6319 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6320 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
6323 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6326 ws = &r->s->ws[ws_idx];
6329 /* this should launch transients in the same ws as parent */
6331 if ((ww = find_window(trans)) != NULL)
6337 warnx("manage_window: fix this "
6343 /* set up the window layout */
6346 win->s = r->s; /* this never changes */
6347 if (trans && (ww = find_window(trans)))
6348 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6349 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6350 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6352 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6353 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6354 else switch (spawn_position) {
6357 case SWM_STACK_ABOVE:
6358 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6360 case SWM_STACK_BOTTOM:
6361 case SWM_STACK_BELOW:
6362 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6365 /* ignore window border if there is one. */
6366 WIDTH(win) = win->wa.width;
6367 HEIGHT(win) = win->wa.height;
6368 X(win) = win->wa.x + win->wa.border_width;
6369 Y(win) = win->wa.y + win->wa.border_width;
6371 win->g_floatvalid = 0;
6372 win->floatmaxed = 0;
6373 win->ewmh_flags = 0;
6375 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, (x,y) w x h: "
6376 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6377 HEIGHT(win), ws->idx);
6379 constrain_window(win, r, 0);
6381 /* Set window properties so we can remember this after reincarnation */
6382 if (ws_idx_atom && prop == NULL &&
6383 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6385 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6387 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
6388 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
6393 ewmh_autoquirk(win);
6395 if (XGetClassHint(display, win->id, &win->ch)) {
6396 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6397 win->ch.res_class, win->ch.res_name);
6399 /* java is retarded so treat it special */
6400 if (strstr(win->ch.res_name, "sun-awt")) {
6405 TAILQ_FOREACH(qp, &quirks, entry) {
6406 if (!strcmp(win->ch.res_class, qp->class) &&
6407 !strcmp(win->ch.res_name, qp->name)) {
6408 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6409 "class: %s, name: %s\n", win->ch.res_class,
6411 if (qp->quirk & SWM_Q_FLOAT) {
6415 win->quirks = qp->quirk;
6420 /* alter window position if quirky */
6421 if (win->quirks & SWM_Q_ANYWHERE) {
6422 win->manual = 1; /* don't center the quirky windows */
6423 if (bar_enabled && Y(win) < bar_height)
6424 Y(win) = bar_height;
6425 if (WIDTH(win) + X(win) > WIDTH(r))
6426 X(win) = WIDTH(r) - WIDTH(win) - 2;
6430 /* Reset font sizes (the bruteforce way; no default keybinding). */
6431 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6432 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6433 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6434 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6435 fake_keypress(win, XK_KP_Add, ShiftMask);
6438 ewmh_get_win_state(win);
6439 ewmh_update_actions(win);
6440 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6445 X(win) -= border_width;
6446 Y(win) -= border_width;
6450 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
6451 PropertyChangeMask | StructureNotifyMask);
6453 /* floaters need to be mapped if they are in the current workspace */
6454 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6455 XMapRaised(display, win->id);
6461 free_window(struct ws_win *win)
6463 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
6468 /* needed for restart wm */
6469 set_win_state(win, WithdrawnState);
6471 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6473 if (win->ch.res_class)
6474 XFree(win->ch.res_class);
6475 if (win->ch.res_name)
6476 XFree(win->ch.res_name);
6481 memset(win, 0xff, sizeof *win); /* XXX kill later */
6487 unmanage_window(struct ws_win *win)
6489 struct ws_win *parent;
6494 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
6496 if (win->transient) {
6497 parent = find_window(win->transient);
6499 parent->child_trans = NULL;
6502 /* focus on root just in case */
6503 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6512 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6513 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6517 focus_magic(struct ws_win *win)
6519 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6522 /* if there are no windows clear the status-bar */
6527 if (win->child_trans) {
6528 /* win = parent & has a transient so focus on that */
6530 focus_win(win->child_trans);
6531 if (win->child_trans->take_focus)
6532 client_msg(win, takefocus);
6534 /* make sure transient hasn't disappeared */
6535 if (validate_win(win->child_trans) == 0) {
6536 focus_win(win->child_trans);
6537 if (win->child_trans->take_focus)
6538 client_msg(win->child_trans, takefocus);
6540 win->child_trans = NULL;
6542 if (win->take_focus)
6543 client_msg(win, takefocus);
6549 if (win->take_focus)
6550 client_msg(win, takefocus);
6557 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6564 XKeyEvent *ev = &e->xkey;
6566 struct swm_region *r;
6568 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6569 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6571 if (keyfuncs[kp->funcid].func == NULL)
6574 r = root_to_region(ev->root);
6575 if (kp->funcid == kf_spawn_custom)
6576 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6578 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6582 buttonpress(XEvent *e)
6586 XButtonPressedEvent *ev = &e->xbutton;
6588 if ((win = find_window(ev->window)) == NULL)
6592 action = client_click;
6594 for (i = 0; i < LENGTH(buttons); i++)
6595 if (action == buttons[i].action && buttons[i].func &&
6596 buttons[i].button == ev->button &&
6597 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6598 buttons[i].func(win, &buttons[i].args);
6602 configurerequest(XEvent *e)
6604 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6609 if ((win = find_window(ev->window)) == NULL)
6610 if ((win = find_unmanaged_window(ev->window)) == NULL)
6614 bzero(&wc, sizeof wc);
6617 wc.width = ev->width;
6618 wc.height = ev->height;
6619 wc.border_width = ev->border_width;
6620 wc.sibling = ev->above;
6621 wc.stack_mode = ev->detail;
6623 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%lx, "
6624 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev->window,
6625 YESNO(new), wc.x, wc.y, wc.width, wc.height);
6627 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6628 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6629 !(win->sh_mask & EWMH_F_FULLSCREEN)) {
6630 win->g_float.x = ev->x - X(win->ws->r);
6631 win->g_float.y = ev->y - Y(win->ws->r);
6632 win->g_float.w = ev->width;
6633 win->g_float.h = ev->height;
6634 win->g_floatvalid = 1;
6636 if (win->floating) {
6637 win->g = win->g_float;
6638 win->g.x += X(win->ws->r);
6639 win->g.y += Y(win->ws->r);
6642 config_win(win, ev);
6645 config_win(win, ev);
6650 configurenotify(XEvent *e)
6654 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6655 e->xconfigure.window);
6657 win = find_window(e->xconfigure.window);
6659 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6663 if (focus_mode == SWM_FOCUS_DEFAULT)
6664 drain_enter_notify();
6669 destroynotify(XEvent *e)
6672 XDestroyWindowEvent *ev = &e->xdestroywindow;
6674 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6676 if ((win = find_window(ev->window)) == NULL) {
6677 if ((win = find_unmanaged_window(ev->window)) == NULL)
6683 /* make sure we focus on something */
6686 unmanage_window(win);
6688 if (focus_mode == SWM_FOCUS_DEFAULT)
6689 drain_enter_notify();
6694 enternotify(XEvent *e)
6696 XCrossingEvent *ev = &e->xcrossing;
6701 Window focus_return;
6702 int revert_to_return;
6704 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6705 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6706 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6707 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6709 if (ev->mode != NotifyNormal) {
6710 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6715 switch (focus_mode) {
6716 case SWM_FOCUS_DEFAULT:
6718 case SWM_FOCUS_FOLLOW:
6720 case SWM_FOCUS_SYNERGY:
6723 * all these checks need to be in this order because the
6724 * XCheckTypedWindowEvent relies on weeding out the previous events
6726 * making this code an option would enable a follow mouse for focus
6731 * state is set when we are switching workspaces and focus is set when
6732 * the window or a subwindow already has focus (occurs during restart).
6734 * Only honor the focus flag if last_focus_event is not FocusOut,
6735 * this allows spectrwm to continue to control focus when another
6736 * program is also playing with it.
6738 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6739 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6744 * happens when a window is created or destroyed and the border
6745 * crosses the mouse pointer and when switching ws
6747 * we need the subwindow test to see if we came from root in order
6748 * to give focus to floaters
6750 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6751 ev->subwindow == 0) {
6752 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6756 /* this window already has focus */
6757 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6758 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6762 /* this window is being deleted or moved to another ws */
6763 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6765 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6766 XPutBackEvent(display, &cne);
6770 if ((win = find_window(ev->window)) == NULL) {
6771 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6776 * In fullstack kill all enters unless they come from a different ws
6777 * (i.e. another region) or focus has been grabbed externally.
6779 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6780 last_focus_event != FocusOut) {
6781 XGetInputFocus(display, &focus_return, &revert_to_return);
6782 if ((w = find_window(focus_return)) == NULL ||
6784 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6792 if ((win = find_window(ev->window)) == NULL) {
6793 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6798 * if we have more enternotifies let them handle it in due time
6800 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6801 DNPRINTF(SWM_D_EVENT,
6802 "ignoring enternotify: got more enternotify\n");
6803 XPutBackEvent(display, &cne);
6810 /* lets us use one switch statement for arbitrary mode/detail combinations */
6811 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6814 focusevent(XEvent *e)
6818 u_int32_t mode_detail;
6819 XFocusChangeEvent *ev = &e->xfocus;
6821 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6822 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6823 ev->window, ev->mode, ev->detail);
6825 if (last_focus_event == ev->type) {
6826 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6830 last_focus_event = ev->type;
6831 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6833 switch (mode_detail) {
6834 /* synergy client focus operations */
6835 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6836 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6838 /* synergy server focus operations */
6839 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6841 /* Entering applications like rdesktop that mangle the pointer */
6842 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6844 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6845 XSetWindowBorder(display, win->id,
6846 win->ws->r->s->c[ev->type == FocusIn ?
6847 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6850 warnx("ignoring focusevent");
6851 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6858 mapnotify(XEvent *e)
6861 XMapEvent *ev = &e->xmap;
6863 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6865 win = manage_window(ev->window);
6867 set_win_state(win, NormalState);
6870 * focus_win can only set input focus on a mapped window.
6871 * make sure the window really has focus since it is just being mapped.
6873 if (win->ws->focus == win)
6878 mappingnotify(XEvent *e)
6880 XMappingEvent *ev = &e->xmapping;
6882 XRefreshKeyboardMapping(ev);
6883 if (ev->request == MappingKeyboard)
6888 maprequest(XEvent *e)
6891 struct swm_region *r;
6892 XWindowAttributes wa;
6893 XMapRequestEvent *ev = &e->xmaprequest;
6895 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6896 e->xmaprequest.window);
6898 if (!XGetWindowAttributes(display, ev->window, &wa))
6900 if (wa.override_redirect)
6903 win = manage_window(e->xmaprequest.window);
6905 return; /* can't happen */
6909 /* make new win focused */
6910 r = root_to_region(win->wa.root);
6911 if (win->ws == r->ws)
6916 propertynotify(XEvent *e)
6919 XPropertyEvent *ev = &e->xproperty;
6922 name = XGetAtomName(display, ev->atom);
6923 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6928 win = find_window(ev->window);
6932 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6933 update_iconic(win, 0);
6934 XMapRaised(display, win->id);
6942 case XA_WM_NORMAL_HINTS:
6944 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6945 warnx("normal hints: flag 0x%x", win->sh.flags);
6946 if (win->sh.flags & PMinSize) {
6947 WIDTH(win) = win->sh.min_width;
6948 HEIGHT(win) = win->sh.min_height;
6949 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6951 XMoveResizeWindow(display, win->id,
6952 X(win), Y(win), WIDTH(win), HEIGHT(win));
6964 unmapnotify(XEvent *e)
6968 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6970 /* determine if we need to help unmanage this window */
6971 win = find_window(e->xunmap.window);
6975 if (getstate(e->xunmap.window) == NormalState) {
6976 unmanage_window(win);
6979 /* giant hack for apps that don't destroy transient windows */
6980 /* eat a bunch of events to prevent remanaging the window */
6982 while (XCheckWindowEvent(display, e->xunmap.window,
6983 EnterWindowMask, &cne))
6985 while (XCheckWindowEvent(display, e->xunmap.window,
6986 StructureNotifyMask, &cne))
6988 while (XCheckWindowEvent(display, e->xunmap.window,
6989 SubstructureNotifyMask, &cne))
6991 /* resend unmap because we ated it */
6992 XUnmapWindow(display, e->xunmap.window);
6995 if (focus_mode == SWM_FOCUS_DEFAULT)
6996 drain_enter_notify();
7000 visibilitynotify(XEvent *e)
7003 struct swm_region *r;
7005 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
7006 e->xvisibility.window);
7007 if (e->xvisibility.state == VisibilityUnobscured)
7008 for (i = 0; i < ScreenCount(display); i++)
7009 TAILQ_FOREACH(r, &screens[i].rl, entry)
7010 if (e->xvisibility.window == WINID(r->bar))
7015 clientmessage(XEvent *e)
7017 XClientMessageEvent *ev;
7022 win = find_window(ev->window);
7024 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7025 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7026 "unmanaged window.\n");
7027 e->xmaprequest.window = ev->window;
7033 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
7034 ev->window, ev->message_type);
7036 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7037 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7040 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7041 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7042 if (win->can_delete)
7043 client_msg(win, adelete);
7045 XKillClient(display, win->id);
7047 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7048 DNPRINTF(SWM_D_EVENT,
7049 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7050 if (win->floating) {
7051 if (ev->data.l[0] & (1<<8)) /* x */
7052 X(win) = ev->data.l[1];
7053 if (ev->data.l[0] & (1<<9)) /* y */
7054 Y(win) = ev->data.l[2];
7055 if (ev->data.l[0] & (1<<10)) /* width */
7056 WIDTH(win) = ev->data.l[3];
7057 if (ev->data.l[0] & (1<<11)) /* height */
7058 HEIGHT(win) = ev->data.l[4];
7063 /* TODO: Change stack sizes */
7064 /* notify no change was made. */
7065 config_win(win, NULL);
7068 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
7069 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7070 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
7072 ewmh_update_win_state(win, ev->data.l[2],
7080 xerror_start(Display *d, XErrorEvent *ee)
7087 xerror(Display *d, XErrorEvent *ee)
7089 /* warnx("error: %p %p", display, ee); */
7097 xerrorxlib = XSetErrorHandler(xerror_start);
7099 /* this causes an error if some other window manager is running */
7100 XSelectInput(display, DefaultRootWindow(display),
7101 SubstructureRedirectMask);
7102 XSync(display, False);
7106 XSetErrorHandler(xerror);
7107 XSync(display, False);
7112 new_region(struct swm_screen *s, int x, int y, int w, int h)
7114 struct swm_region *r, *n;
7115 struct workspace *ws = NULL;
7118 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7119 s->idx, w, h, x, y);
7121 /* remove any conflicting regions */
7122 n = TAILQ_FIRST(&s->rl);
7125 n = TAILQ_NEXT(r, entry);
7126 if (X(r) < (x + w) &&
7127 (X(r) + WIDTH(r)) > x &&
7129 (Y(r) + HEIGHT(r)) > y) {
7130 if (r->ws->r != NULL)
7131 r->ws->old_r = r->ws->r;
7134 TAILQ_REMOVE(&s->rl, r, entry);
7135 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7139 /* search old regions for one to reuse */
7141 /* size + location match */
7142 TAILQ_FOREACH(r, &s->orl, entry)
7143 if (X(r) == x && Y(r) == y &&
7144 HEIGHT(r) == h && WIDTH(r) == w)
7148 TAILQ_FOREACH(r, &s->orl, entry)
7149 if (HEIGHT(r) == h && WIDTH(r) == w)
7153 TAILQ_REMOVE(&s->orl, r, entry);
7154 /* try to use old region's workspace */
7155 if (r->ws->r == NULL)
7158 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7159 err(1, "new_region: calloc: failed to allocate memory "
7162 /* if we don't have a workspace already, find one */
7164 for (i = 0; i < workspace_limit; i++)
7165 if (s->ws[i].r == NULL) {
7172 errx(1, "new_region: no free workspaces");
7183 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7189 #ifdef SWM_XRR_HAS_CRTC
7191 XRRScreenResources *sr;
7194 #endif /* SWM_XRR_HAS_CRTC */
7195 struct swm_region *r;
7198 if (i >= ScreenCount(display))
7199 errx(1, "scan_xrandr: invalid screen");
7201 /* remove any old regions */
7202 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7203 r->ws->old_r = r->ws->r = NULL;
7205 TAILQ_REMOVE(&screens[i].rl, r, entry);
7206 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7210 /* map virtual screens onto physical screens */
7211 #ifdef SWM_XRR_HAS_CRTC
7212 if (xrandr_support) {
7213 sr = XRRGetScreenResourcesCurrent(display, screens[i].root);
7215 new_region(&screens[i], 0, 0,
7216 DisplayWidth(display, i),
7217 DisplayHeight(display, i));
7221 for (c = 0, ci = NULL; c < ncrtc; c++) {
7222 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
7223 if (ci->noutput == 0)
7226 if (ci != NULL && ci->mode == None)
7227 new_region(&screens[i], 0, 0,
7228 DisplayWidth(display, i),
7229 DisplayHeight(display, i));
7231 new_region(&screens[i],
7232 ci->x, ci->y, ci->width, ci->height);
7235 XRRFreeCrtcInfo(ci);
7236 XRRFreeScreenResources(sr);
7238 #endif /* SWM_XRR_HAS_CRTC */
7240 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
7241 DisplayHeight(display, i));
7246 screenchange(XEvent *e) {
7247 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7248 struct swm_region *r;
7251 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7253 if (!XRRUpdateConfiguration(e))
7256 /* silly event doesn't include the screen index */
7257 for (i = 0; i < ScreenCount(display); i++)
7258 if (screens[i].root == xe->root)
7260 if (i >= ScreenCount(display))
7261 errx(1, "screenchange: screen not found");
7263 /* brute force for now, just re-enumerate the regions */
7266 /* add bars to all regions */
7267 for (i = 0; i < ScreenCount(display); i++)
7268 TAILQ_FOREACH(r, &screens[i].rl, entry)
7271 if (focus_mode == SWM_FOCUS_DEFAULT)
7272 drain_enter_notify();
7278 Window d1, d2, *wins = NULL;
7279 XWindowAttributes wa;
7284 for (i = 0; i < ScreenCount(display); i++) {
7285 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
7288 /* attach windows to a region */
7289 /* normal windows */
7290 for (j = 0; j < no; j++) {
7291 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7292 wa.override_redirect ||
7293 XGetTransientForHint(display, wins[j], &d1))
7296 state = getstate(wins[j]);
7297 manage = state == IconicState;
7298 if (wa.map_state == IsViewable || manage)
7299 manage_window(wins[j]);
7301 /* transient windows */
7302 for (j = 0; j < no; j++) {
7303 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7304 wa.override_redirect)
7307 state = getstate(wins[j]);
7308 manage = state == IconicState;
7309 if (XGetTransientForHint(display, wins[j], &d1) &&
7311 manage_window(wins[j]);
7323 int i, j, k, num_screens;
7324 int errorbase, major, minor;
7325 struct workspace *ws;
7328 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7329 if ((screens = calloc(num_screens,
7330 sizeof(struct swm_screen))) == NULL)
7331 err(1, "setup_screens: calloc: failed to allocate memory for "
7334 /* initial Xrandr setup */
7335 xrandr_support = XRRQueryExtension(display,
7336 &xrandr_eventbase, &errorbase);
7338 if (XRRQueryVersion(display, &major, &minor) && major < 1)
7341 /* map physical screens */
7342 for (i = 0; i < num_screens; i++) {
7343 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7345 TAILQ_INIT(&screens[i].rl);
7346 TAILQ_INIT(&screens[i].orl);
7347 screens[i].root = RootWindow(display, i);
7349 /* set default colors */
7350 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7351 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7352 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7353 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7354 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7356 /* create graphics context on screen */
7357 gcv.graphics_exposures = 0;
7358 screens[i].bar_gc = XCreateGC(display, screens[i].root,
7359 GCGraphicsExposures, &gcv);
7361 /* set default cursor */
7362 XDefineCursor(display, screens[i].root,
7363 XCreateFontCursor(display, XC_left_ptr));
7365 /* init all workspaces */
7366 /* XXX these should be dynamically allocated too */
7367 for (j = 0; j < SWM_WS_MAX; j++) {
7368 ws = &screens[i].ws[j];
7374 TAILQ_INIT(&ws->winlist);
7375 TAILQ_INIT(&ws->unmanagedlist);
7377 for (k = 0; layouts[k].l_stack != NULL; k++)
7378 if (layouts[k].l_config != NULL)
7379 layouts[k].l_config(ws,
7380 SWM_ARG_ID_STACKINIT);
7381 ws->cur_layout = &layouts[0];
7382 ws->cur_layout->l_string(ws);
7388 XRRSelectInput(display, screens[i].root,
7389 RRScreenChangeNotifyMask);
7396 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7397 err(1, "setup_globals: strdup: failed to allocate memory.");
7399 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7400 err(1, "setup_globals: strdup: failed to allocate memory.");
7407 Atom netwmcheck, netwmname, utf8_string;
7410 /* work around sun jdk bugs, code from wmname */
7411 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
7412 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7413 utf8_string = XInternAtom(display, "UTF8_STRING", False);
7414 for (i = 0; i < ScreenCount(display); i++) {
7415 root = screens[i].root;
7416 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
7417 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
7418 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
7420 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
7421 PropModeReplace, (unsigned char *)&win, 1);
7422 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
7423 PropModeReplace, (unsigned char *)&win, 1);
7424 XChangeProperty(display, win, netwmname, utf8_string, 8,
7425 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
7430 main(int argc, char *argv[])
7432 struct swm_region *r, *rr;
7433 struct ws_win *winfocus = NULL;
7436 char conf[PATH_MAX], *cfile = NULL;
7441 struct sigaction sact;
7444 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7445 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7447 warnx("no locale support");
7449 if (!X_HAVE_UTF8_STRING)
7450 warnx("no UTF-8 support");
7452 if (!(display = XOpenDisplay(0)))
7453 errx(1, "can not open display");
7455 if (!(conn = XGetXCBConnection(display)))
7456 errx(1, "can not get XCB connection");
7459 errx(1, "other wm running");
7461 /* handle some signals */
7462 bzero(&sact, sizeof(sact));
7463 sigemptyset(&sact.sa_mask);
7465 sact.sa_handler = sighdlr;
7466 sigaction(SIGINT, &sact, NULL);
7467 sigaction(SIGQUIT, &sact, NULL);
7468 sigaction(SIGTERM, &sact, NULL);
7469 sigaction(SIGHUP, &sact, NULL);
7471 sact.sa_handler = sighdlr;
7472 sact.sa_flags = SA_NOCLDSTOP;
7473 sigaction(SIGCHLD, &sact, NULL);
7475 astate = XInternAtom(display, "WM_STATE", False);
7476 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
7477 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
7478 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
7479 a_wmname = XInternAtom(display, "WM_NAME", False);
7480 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7481 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
7482 a_string = XInternAtom(display, "STRING", False);
7483 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
7485 /* look for local and global conf file */
7486 pwd = getpwuid(getuid());
7488 errx(1, "invalid user: %d", getuid());
7497 for (i = 0; ; i++) {
7502 snprintf(conf, sizeof conf, "%s/.%s",
7503 pwd->pw_dir, SWM_CONF_FILE);
7507 snprintf(conf, sizeof conf, "/etc/%s",
7512 snprintf(conf, sizeof conf, "%s/.%s",
7513 pwd->pw_dir, SWM_CONF_FILE_OLD);
7517 snprintf(conf, sizeof conf, "/etc/%s",
7524 if (strlen(conf) && stat(conf, &sb) != -1)
7525 if (S_ISREG(sb.st_mode)) {
7532 /* load conf (if any) */
7534 conf_load(cfile, SWM_CONF_DEFAULT);
7537 /* set some values to work around bad programs */
7539 /* grab existing windows (before we build the bars) */
7542 if (getenv("SWM_STARTED") == NULL)
7543 setenv("SWM_STARTED", "YES", 1);
7545 /* setup all bars */
7546 for (i = 0; i < ScreenCount(display); i++)
7547 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7548 if (winfocus == NULL)
7549 winfocus = TAILQ_FIRST(&r->ws->winlist);
7557 if (focus_mode == SWM_FOCUS_DEFAULT)
7558 drain_enter_notify();
7560 xfd = ConnectionNumber(display);
7562 while (XPending(display)) {
7563 XNextEvent(display, &e);
7566 if (e.type < LASTEvent) {
7567 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7568 "window: 0x%lx, type: %s (%d), %d remaining"
7569 "\n", YESNO(handler[e.type]),
7570 e.xany.window, geteventname(&e),
7571 e.type, QLength(display));
7573 if (handler[e.type])
7574 handler[e.type](&e);
7576 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7577 "0x%lx, type: %s (%d)\n", e.xany.window,
7578 xrandr_geteventname(&e), e.type);
7580 switch (e.type - xrandr_eventbase) {
7581 case RRScreenChangeNotify:
7590 /* if we are being restarted go focus on first window */
7592 rr = winfocus->ws->r;
7594 /* not a visible window */
7598 /* move pointer to first screen if multi screen */
7599 if (ScreenCount(display) > 1 || outputs > 1)
7600 XWarpPointer(display, None, rr->s[0].root,
7602 Y(rr) + (bar_enabled ? bar_height : 0));
7604 a.id = SWM_ARG_ID_FOCUSCUR;
7614 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7616 DNPRINTF(SWM_D_MISC, "select failed");
7617 if (restart_wm == 1)
7618 restart(NULL, NULL);
7619 if (search_resp == 1)
7632 for (i = 0; i < ScreenCount(display); ++i)
7633 if (screens[i].bar_gc != NULL)
7634 XFreeGC(display, screens[i].bar_gc);
7636 XFreeFontSet(display, bar_fs);
7637 xcb_disconnect(conn);
7638 XCloseDisplay(display);