Permalink
Fetching contributors…
Cannot retrieve contributors at this time
12258 lines (10504 sloc) 301 KB
/*
* Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
* Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
* Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
* Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
* Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
* Copyright (c) 2011 Jason L. Wright <jason@thought.net>
* Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
* Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
* Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
* Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
* Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* kernel includes */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef __OSX__
#include "queue.h"
#else
#include <sys/queue.h>
#endif
#include <sys/param.h>
#include <sys/select.h>
#if defined(__linux__)
#include "tree.h"
#elif defined(__OpenBSD__)
#include <sys/tree.h>
#elif defined(__FreeBSD__)
#include <sys/tree.h>
#else
#include "tree.h"
#endif
/* /usr/includes */
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <locale.h>
#include <paths.h>
#include <pwd.h>
#include <regex.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <util.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xrandr.h>
#include <X11/Xcursor/Xcursor.h>
#include <X11/Xft/Xft.h>
#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_keysyms.h>
#include <xcb/xtest.h>
#include <xcb/randr.h>
/* local includes */
#include "version.h"
#ifdef __OSX__
#include <osx.h>
#endif
#ifdef SPECTRWM_BUILDSTR
static const char *buildstr = SPECTRWM_BUILDSTR;
#else
static const char *buildstr = SPECTRWM_VERSION;
#endif
#if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
# if RANDR_MAJOR < 1
# error RandR versions less than 1.0 are not supported
#endif
# if RANDR_MAJOR >= 1
# if RANDR_MINOR >= 2
# define SWM_XRR_HAS_CRTC
# endif
# endif
#endif /* __CYGWIN__ */
#ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
#define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
#define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
#define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
#define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
#define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
#define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
#define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
#define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
#define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
#define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
#define xcb_icccm_get_wm_class xcb_get_wm_class
#define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
#define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
#define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
#define xcb_icccm_get_wm_hints xcb_get_wm_hints
#define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
#define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
#define xcb_icccm_get_wm_name xcb_get_wm_name
#define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
#define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
#define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
#define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
#define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
#define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
#define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
#define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
#define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
#define xcb_icccm_wm_hints_t xcb_wm_hints_t
#endif
/*#define SWM_DEBUG*/
#ifdef SWM_DEBUG
#define DPRINTF(x...) do { \
if (swm_debug) \
fprintf(stderr, x); \
} while (0)
#define DNPRINTF(n,x...) do { \
if (swm_debug & n) { \
fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
fprintf(stderr, x); \
} \
} while (0)
#define SWM_D_MISC 0x0001
#define SWM_D_EVENT 0x0002
#define SWM_D_WS 0x0004
#define SWM_D_FOCUS 0x0008
#define SWM_D_MOVE 0x0010
#define SWM_D_STACK 0x0020
#define SWM_D_MOUSE 0x0040
#define SWM_D_PROP 0x0080
#define SWM_D_CLASS 0x0100
#define SWM_D_KEY 0x0200
#define SWM_D_QUIRK 0x0400
#define SWM_D_SPAWN 0x0800
#define SWM_D_EVENTQ 0x1000
#define SWM_D_CONF 0x2000
#define SWM_D_BAR 0x4000
#define SWM_D_INIT 0x8000
uint32_t swm_debug = 0
| SWM_D_MISC
| SWM_D_EVENT
| SWM_D_WS
| SWM_D_FOCUS
| SWM_D_MOVE
| SWM_D_STACK
| SWM_D_MOUSE
| SWM_D_PROP
| SWM_D_CLASS
| SWM_D_KEY
| SWM_D_QUIRK
| SWM_D_SPAWN
| SWM_D_EVENTQ
| SWM_D_CONF
| SWM_D_BAR
| SWM_D_INIT
;
#else
#define DPRINTF(x...)
#define DNPRINTF(n,x...)
#endif
#define SWM_EWMH_ACTION_COUNT_MAX (8)
#define EWMH_F_FULLSCREEN (0x001)
#define EWMH_F_ABOVE (0x002)
#define EWMH_F_HIDDEN (0x004)
#define EWMH_F_MAXIMIZED_VERT (0x008)
#define EWMH_F_MAXIMIZED_HORZ (0x010)
#define EWMH_F_SKIP_PAGER (0x020)
#define EWMH_F_SKIP_TASKBAR (0x040)
#define SWM_F_MANUAL (0x080)
#define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
/* convert 8-bit to 16-bit */
#define RGB_8_TO_16(col) (((col) << 8) + (col))
#define PIXEL_TO_XRENDERCOLOR(px, xrc) \
xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
xrc.blue = RGB_8_TO_16((px) & 0xff); \
xrc.alpha = 0xffff;
#define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
#define MODKEY XCB_MOD_MASK_1
#define ANYMOD XCB_MOD_MASK_ANY
#define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
#define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
XCB_EVENT_MASK_BUTTON_RELEASE)
#define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
#define SWM_PROPLEN (16)
#define SWM_FUNCNAME_LEN (32)
#define SWM_QUIRK_LEN (64)
#define X(r) ((r)->g.x)
#define Y(r) ((r)->g.y)
#define WIDTH(r) ((r)->g.w)
#define HEIGHT(r) ((r)->g.h)
#define MAX_X(r) ((r)->g.x + (r)->g.w)
#define MAX_Y(r) ((r)->g.y + (r)->g.h)
#define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
#define SH_MIN_W(w) ((w)->sh.min_width)
#define SH_MIN_H(w) ((w)->sh.min_height)
#define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
#define SH_MAX_W(w) ((w)->sh.max_width)
#define SH_MAX_H(w) ((w)->sh.max_height)
#define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
#define SH_INC_W(w) ((w)->sh.width_inc)
#define SH_INC_H(w) ((w)->sh.height_inc)
#define SWM_MAX_FONT_STEPS (3)
#define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
#define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
|| ((w)->hints.input))
#define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
#define YESNO(x) ((x) ? "yes" : "no")
#define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
#define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
#define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
#define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
#define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
#define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
#define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
#define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
#define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
MAXIMIZED(w))
/* Constrain Window flags */
#define SWM_CW_RESIZABLE (0x01)
#define SWM_CW_SOFTBOUNDARY (0x02)
#define SWM_CW_HARDBOUNDARY (0x04)
#define SWM_CW_RIGHT (0x10)
#define SWM_CW_LEFT (0x20)
#define SWM_CW_BOTTOM (0x40)
#define SWM_CW_TOP (0x80)
#define SWM_CW_ALLSIDES (0xf0)
#define SWM_FOCUS_DEFAULT (0)
#define SWM_FOCUS_FOLLOW (1)
#define SWM_FOCUS_MANUAL (2)
#define SWM_CK_ALL (0xf)
#define SWM_CK_FOCUS (0x1)
#define SWM_CK_POINTER (0x2)
#define SWM_CK_FALLBACK (0x4)
#define SWM_CK_REGION (0x8)
#define SWM_CONF_DEFAULT (0)
#define SWM_CONF_KEYMAPPING (1)
#ifndef SWM_LIB
#define SWM_LIB "/usr/local/lib/libswmhack.so"
#endif
char **start_argv;
xcb_atom_t a_state;
xcb_atom_t a_prot;
xcb_atom_t a_delete;
xcb_atom_t a_net_frame_extents;
xcb_atom_t a_net_wm_check;
xcb_atom_t a_net_supported;
xcb_atom_t a_takefocus;
xcb_atom_t a_utf8_string;
xcb_atom_t a_swm_ws;
volatile sig_atomic_t running = 1;
volatile sig_atomic_t restart_wm = 0;
xcb_timestamp_t last_event_time = 0;
int outputs = 0;
bool randr_support;
int randr_eventbase;
unsigned int numlockmask = 0;
Display *display;
xcb_connection_t *conn;
xcb_key_symbols_t *syms;
int boundary_width = 50;
bool cycle_empty = false;
bool cycle_visible = false;
int term_width = 0;
int font_adjusted = 0;
uint16_t mod_key = MODKEY;
bool warp_focus = false;
bool warp_pointer = false;
bool workspace_clamp = false;
/* dmenu search */
struct swm_region *search_r;
int select_list_pipe[2];
int select_resp_pipe[2];
pid_t searchpid;
volatile sig_atomic_t search_resp;
int search_resp_action;
struct search_window {
TAILQ_ENTRY(search_window) entry;
int idx;
struct ws_win *win;
xcb_gcontext_t gc;
xcb_window_t indicator;
};
TAILQ_HEAD(search_winlist, search_window);
struct search_winlist search_wl;
/* search actions */
enum {
SWM_SEARCH_NONE,
SWM_SEARCH_UNICONIFY,
SWM_SEARCH_NAME_WORKSPACE,
SWM_SEARCH_SEARCH_WORKSPACE,
SWM_SEARCH_SEARCH_WINDOW
};
#define SWM_STACK_TOP (0)
#define SWM_STACK_BOTTOM (1)
#define SWM_STACK_ABOVE (2)
#define SWM_STACK_BELOW (3)
/* dialog windows */
double dialog_ratio = 0.6;
/* status bar */
#define SWM_BAR_MAX (256)
#define SWM_BAR_JUSTIFY_LEFT (0)
#define SWM_BAR_JUSTIFY_CENTER (1)
#define SWM_BAR_JUSTIFY_RIGHT (2)
#define SWM_BAR_OFFSET (4)
#define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
"-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
"-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
"-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
"-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
#ifdef X_HAVE_UTF8_STRING
#define DRAWSTRING(x...) Xutf8DrawString(x)
#else
#define DRAWSTRING(x...) XmbDrawString(x)
#endif
char *bar_argv[] = { NULL, NULL };
int bar_pipe[2];
char bar_ext[SWM_BAR_MAX];
char bar_ext_buf[SWM_BAR_MAX];
char bar_vertext[SWM_BAR_MAX];
bool bar_version = false;
bool bar_enabled = true;
int bar_border_width = 1;
bool bar_at_bottom = false;
bool bar_extra = false;
int bar_height = 0;
int bar_justify = SWM_BAR_JUSTIFY_LEFT;
char *bar_format = NULL;
bool stack_enabled = true;
bool clock_enabled = true;
bool iconic_enabled = false;
bool maximize_hide_bar = false;
bool urgent_enabled = false;
bool urgent_collapse = false;
char *clock_format = NULL;
bool window_class_enabled = false;
bool window_instance_enabled = false;
bool window_name_enabled = false;
int focus_mode = SWM_FOCUS_DEFAULT;
int focus_close = SWM_STACK_BELOW;
bool focus_close_wrap = true;
int focus_default = SWM_STACK_TOP;
int spawn_position = SWM_STACK_TOP;
bool disable_border = false;
int border_width = 1;
int region_padding = 0;
int tile_gap = 0;
bool java_workaround = true;
bool verbose_layout = false;
#ifdef SWM_DEBUG
bool debug_enabled;
time_t time_started;
#endif
pid_t bar_pid;
XFontSet bar_fs = NULL;
XFontSetExtents *bar_fs_extents;
XftFont *bar_font = NULL;
bool bar_font_legacy = true;
char *bar_fonts = NULL;
XftColor bar_font_color;
XftColor search_font_color;
char *startup_exception = NULL;
unsigned int nr_exceptions = 0;
/* layout manager data */
struct swm_geometry {
int x;
int y;
int w;
int h;
};
struct swm_screen;
struct workspace;
struct swm_bar {
xcb_window_t id;
xcb_pixmap_t buffer;
struct swm_geometry g;
struct swm_region *r; /* Associated region. */
};
/* virtual "screens" */
struct swm_region {
TAILQ_ENTRY(swm_region) entry;
xcb_window_t id;
struct swm_geometry g;
struct workspace *ws; /* current workspace on this region */
struct workspace *ws_prior; /* prior workspace on this region */
struct swm_screen *s; /* screen idx */
struct swm_bar *bar;
};
TAILQ_HEAD(swm_region_list, swm_region);
enum {
SWM_WIN_STATE_REPARENTING,
SWM_WIN_STATE_REPARENTED,
SWM_WIN_STATE_UNPARENTING,
SWM_WIN_STATE_UNPARENTED,
};
struct ws_win {
TAILQ_ENTRY(ws_win) entry;
TAILQ_ENTRY(ws_win) stack_entry;
xcb_window_t id;
xcb_window_t frame;
xcb_window_t transient;
struct ws_win *focus_child; /* focus on child transient */
struct swm_geometry g; /* current geometry */
struct swm_geometry g_prev; /* prev configured geometry */
struct swm_geometry g_float; /* region coordinates */
bool g_floatvalid; /* g_float geometry validity */
bool mapped;
uint8_t state;
bool bordered;
uint32_t ewmh_flags;
int font_size_boundary[SWM_MAX_FONT_STEPS];
int font_steps;
int last_inc;
bool can_delete;
bool take_focus;
bool java;
uint32_t quirks;
struct workspace *ws; /* always valid */
struct swm_screen *s; /* always valid, never changes */
xcb_size_hints_t sh;
xcb_icccm_get_wm_class_reply_t ch;
xcb_icccm_wm_hints_t hints;
#ifdef SWM_DEBUG
xcb_window_t debug;
#endif
};
TAILQ_HEAD(ws_win_list, ws_win);
TAILQ_HEAD(ws_win_stack, ws_win);
/* pid goo */
struct pid_e {
TAILQ_ENTRY(pid_e) entry;
pid_t pid;
int ws;
};
TAILQ_HEAD(pid_list, pid_e);
struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
/* layout handlers */
void stack(struct swm_region *);
void vertical_config(struct workspace *, int);
void vertical_stack(struct workspace *, struct swm_geometry *);
void horizontal_config(struct workspace *, int);
void horizontal_stack(struct workspace *, struct swm_geometry *);
void max_stack(struct workspace *, struct swm_geometry *);
void plain_stacker(struct workspace *);
void fancy_stacker(struct workspace *);
struct layout {
void (*l_stack)(struct workspace *, struct swm_geometry *);
void (*l_config)(struct workspace *, int);
uint32_t flags;
#define SWM_L_FOCUSPREV (1<<0)
#define SWM_L_MAPONFOCUS (1<<1)
void (*l_string)(struct workspace *);
} layouts[] = {
/* stack, configure */
{ vertical_stack, vertical_config, 0, plain_stacker },
{ horizontal_stack, horizontal_config, 0, plain_stacker },
{ max_stack, NULL,
SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
{ NULL, NULL, 0, NULL },
};
/* position of max_stack mode in the layouts array, index into layouts! */
#define SWM_V_STACK (0)
#define SWM_H_STACK (1)
#define SWM_MAX_STACK (2)
#define SWM_H_SLICE (32)
#define SWM_V_SLICE (32)
/* define work spaces */
struct workspace {
int idx; /* workspace index */
char *name; /* workspace name */
bool always_raise; /* raise windows on focus */
bool bar_enabled; /* bar visibility */
struct layout *cur_layout; /* current layout handlers */
struct ws_win *focus; /* may be NULL */
struct ws_win *focus_prev; /* may be NULL */
struct ws_win *focus_pending; /* may be NULL */
struct ws_win *focus_raise; /* may be NULL */
struct swm_region *r; /* may be NULL */
struct swm_region *old_r; /* may be NULL */
struct ws_win_list winlist; /* list of windows in ws */
struct ws_win_list unmanagedlist; /* list of dead windows in ws */
struct ws_win_stack stack; /* stacking order */
int state; /* mapping state */
char stacker[10]; /* display stacker and layout */
/* stacker state */
struct {
int horizontal_msize;
int horizontal_mwin;
int horizontal_stacks;
bool horizontal_flip;
int vertical_msize;
int vertical_mwin;
int vertical_stacks;
bool vertical_flip;
} l_state;
};
enum {
SWM_WS_STATE_HIDDEN,
SWM_WS_STATE_MAPPING,
SWM_WS_STATE_MAPPED,
};
enum {
SWM_S_COLOR_BAR,
SWM_S_COLOR_BAR_BORDER,
SWM_S_COLOR_BAR_BORDER_UNFOCUS,
SWM_S_COLOR_BAR_FONT,
SWM_S_COLOR_FOCUS,
SWM_S_COLOR_FOCUS_MAXIMIZED,
SWM_S_COLOR_UNFOCUS,
SWM_S_COLOR_UNFOCUS_MAXIMIZED,
SWM_S_COLOR_MAX
};
/* physical screen mapping */
#define SWM_WS_MAX (22) /* hard limit */
int workspace_limit = 10; /* soft limit */
struct swm_screen {
int idx; /* screen index */
struct swm_region_list rl; /* list of regions on this screen */
struct swm_region_list orl; /* list of old regions */
xcb_window_t root;
struct workspace ws[SWM_WS_MAX];
struct swm_region *r_focus;
/* colors */
struct {
uint32_t pixel;
char *name;
int manual;
} c[SWM_S_COLOR_MAX];
xcb_gcontext_t bar_gc;
GC bar_gc_legacy;
};
struct swm_screen *screens;
/* args to functions */
union arg {
int id;
#define SWM_ARG_ID_FOCUSNEXT (0)
#define SWM_ARG_ID_FOCUSPREV (1)
#define SWM_ARG_ID_FOCUSMAIN (2)
#define SWM_ARG_ID_FOCUSURGENT (3)
#define SWM_ARG_ID_SWAPNEXT (10)
#define SWM_ARG_ID_SWAPPREV (11)
#define SWM_ARG_ID_SWAPMAIN (12)
#define SWM_ARG_ID_MOVELAST (13)
#define SWM_ARG_ID_MASTERSHRINK (20)
#define SWM_ARG_ID_MASTERGROW (21)
#define SWM_ARG_ID_MASTERADD (22)
#define SWM_ARG_ID_MASTERDEL (23)
#define SWM_ARG_ID_FLIPLAYOUT (24)
#define SWM_ARG_ID_STACKRESET (30)
#define SWM_ARG_ID_STACKINIT (31)
#define SWM_ARG_ID_STACKBALANCE (32)
#define SWM_ARG_ID_CYCLEWS_UP (40)
#define SWM_ARG_ID_CYCLEWS_DOWN (41)
#define SWM_ARG_ID_CYCLERG_UP (42)
#define SWM_ARG_ID_CYCLERG_DOWN (43)
#define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
#define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
#define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
#define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
#define SWM_ARG_ID_STACKINC (50)
#define SWM_ARG_ID_STACKDEC (51)
#define SWM_ARG_ID_DONTCENTER (70)
#define SWM_ARG_ID_CENTER (71)
#define SWM_ARG_ID_KILLWINDOW (80)
#define SWM_ARG_ID_DELETEWINDOW (81)
#define SWM_ARG_ID_WIDTHGROW (90)
#define SWM_ARG_ID_WIDTHSHRINK (91)
#define SWM_ARG_ID_HEIGHTGROW (92)
#define SWM_ARG_ID_HEIGHTSHRINK (93)
#define SWM_ARG_ID_MOVEUP (100)
#define SWM_ARG_ID_MOVEDOWN (101)
#define SWM_ARG_ID_MOVELEFT (102)
#define SWM_ARG_ID_MOVERIGHT (103)
#define SWM_ARG_ID_BAR_TOGGLE (110)
#define SWM_ARG_ID_BAR_TOGGLE_WS (111)
#define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
#define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
char **argv;
};
/* quirks */
struct quirk {
TAILQ_ENTRY(quirk) entry;
char *class; /* WM_CLASS:class */
char *instance; /* WM_CLASS:instance */
char *name; /* WM_NAME */
regex_t regex_class;
regex_t regex_instance;
regex_t regex_name;
uint32_t quirk;
int ws; /* Initial workspace. */
#define SWM_Q_FLOAT (1<<0) /* Float this window. */
#define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
#define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
#define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
#define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
#define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
#define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
#define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
#define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
#define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
#define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
#define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
#define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
};
TAILQ_HEAD(quirk_list, quirk);
struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
/*
* Supported EWMH hints should be added to
* both the enum and the ewmh array
*/
enum {
_NET_ACTIVE_WINDOW,
_NET_CLIENT_LIST,
_NET_CLOSE_WINDOW,
_NET_CURRENT_DESKTOP,
_NET_DESKTOP_GEOMETRY,
_NET_DESKTOP_NAMES,
_NET_DESKTOP_VIEWPORT,
_NET_MOVERESIZE_WINDOW,
_NET_NUMBER_OF_DESKTOPS,
_NET_REQUEST_FRAME_EXTENTS,
_NET_RESTACK_WINDOW,
_NET_WM_ACTION_ABOVE,
_NET_WM_ACTION_CLOSE,
_NET_WM_ACTION_FULLSCREEN,
_NET_WM_ACTION_MOVE,
_NET_WM_ACTION_RESIZE,
_NET_WM_ALLOWED_ACTIONS,
_NET_WM_DESKTOP,
_NET_WM_FULL_PLACEMENT,
_NET_WM_NAME,
_NET_WM_STATE,
_NET_WM_STATE_ABOVE,
_NET_WM_STATE_FULLSCREEN,
_NET_WM_STATE_HIDDEN,
_NET_WM_STATE_MAXIMIZED_VERT,
_NET_WM_STATE_MAXIMIZED_HORZ,
_NET_WM_STATE_SKIP_PAGER,
_NET_WM_STATE_SKIP_TASKBAR,
_NET_WM_WINDOW_TYPE,
_NET_WM_WINDOW_TYPE_DIALOG,
_NET_WM_WINDOW_TYPE_DOCK,
_NET_WM_WINDOW_TYPE_NORMAL,
_NET_WM_WINDOW_TYPE_SPLASH,
_NET_WM_WINDOW_TYPE_TOOLBAR,
_NET_WM_WINDOW_TYPE_UTILITY,
_SWM_WM_STATE_MANUAL,
SWM_EWMH_HINT_MAX
};
struct ewmh_hint {
char *name;
xcb_atom_t atom;
} ewmh[SWM_EWMH_HINT_MAX] = {
/* must be in same order as in the enum */
{"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
{"_NET_CLIENT_LIST", XCB_ATOM_NONE},
{"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
{"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
{"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
{"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
{"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
{"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
{"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
{"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE},
{"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
{"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
{"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
{"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
{"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
{"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
{"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
{"_NET_WM_DESKTOP", XCB_ATOM_NONE},
{"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
{"_NET_WM_NAME", XCB_ATOM_NONE},
{"_NET_WM_STATE", XCB_ATOM_NONE},
{"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
{"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
{"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
{"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
{"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
{"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
{"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
{"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
{"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
};
/* EWMH source type */
enum {
EWMH_SOURCE_TYPE_NONE = 0,
EWMH_SOURCE_TYPE_NORMAL = 1,
EWMH_SOURCE_TYPE_OTHER = 2,
};
/* Cursors */
enum {
XC_FLEUR,
XC_LEFT_PTR,
XC_BOTTOM_LEFT_CORNER,
XC_BOTTOM_RIGHT_CORNER,
XC_SIZING,
XC_TOP_LEFT_CORNER,
XC_TOP_RIGHT_CORNER,
XC_MAX
};
struct cursors {
char *name; /* Name used by Xcursor .*/
uint8_t cf_char; /* cursorfont index. */
xcb_cursor_t cid;
} cursors[XC_MAX] = {
{"fleur", XC_fleur, XCB_CURSOR_NONE},
{"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
{"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
{"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
{"sizing", XC_sizing, XCB_CURSOR_NONE},
{"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
{"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
};
#define SWM_SPAWN_OPTIONAL 0x1
/* spawn */
struct spawn_prog {
TAILQ_ENTRY(spawn_prog) entry;
char *name;
int argc;
char **argv;
int flags;
};
TAILQ_HEAD(spawn_list, spawn_prog);
struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
enum {
FN_F_NOREPLAY = 0x1,
};
/* User callable function IDs. */
enum actionid {
FN_BAR_TOGGLE,
FN_BAR_TOGGLE_WS,
FN_BUTTON2,
FN_CYCLE_LAYOUT,
FN_FLIP_LAYOUT,
FN_FLOAT_TOGGLE,
FN_FOCUS,
FN_FOCUS_MAIN,
FN_FOCUS_NEXT,
FN_FOCUS_PREV,
FN_FOCUS_URGENT,
FN_FULLSCREEN_TOGGLE,
FN_MAXIMIZE_TOGGLE,
FN_HEIGHT_GROW,
FN_HEIGHT_SHRINK,
FN_ICONIFY,
FN_MASTER_SHRINK,
FN_MASTER_GROW,
FN_MASTER_ADD,
FN_MASTER_DEL,
FN_MOVE,
FN_MOVE_DOWN,
FN_MOVE_LEFT,
FN_MOVE_RIGHT,
FN_MOVE_UP,
FN_MVRG_1,
FN_MVRG_2,
FN_MVRG_3,
FN_MVRG_4,
FN_MVRG_5,
FN_MVRG_6,
FN_MVRG_7,
FN_MVRG_8,
FN_MVRG_9,
KF_MVRG_NEXT,
KF_MVRG_PREV,
FN_MVWS_1,
FN_MVWS_2,
FN_MVWS_3,
FN_MVWS_4,
FN_MVWS_5,
FN_MVWS_6,
FN_MVWS_7,
FN_MVWS_8,
FN_MVWS_9,
FN_MVWS_10,
FN_MVWS_11,
FN_MVWS_12,
FN_MVWS_13,
FN_MVWS_14,
FN_MVWS_15,
FN_MVWS_16,
FN_MVWS_17,
FN_MVWS_18,
FN_MVWS_19,
FN_MVWS_20,
FN_MVWS_21,
FN_MVWS_22,
FN_NAME_WORKSPACE,
FN_QUIT,
FN_RAISE,
FN_RAISE_TOGGLE,
FN_RESIZE,
FN_RESIZE_CENTERED,
FN_RESTART,
FN_RG_1,
FN_RG_2,
FN_RG_3,
FN_RG_4,
FN_RG_5,
FN_RG_6,
FN_RG_7,
FN_RG_8,
FN_RG_9,
FN_RG_MOVE_NEXT,
FN_RG_MOVE_PREV,
FN_RG_NEXT,
FN_RG_PREV,
FN_SCREEN_NEXT,
FN_SCREEN_PREV,
FN_SEARCH_WIN,
FN_SEARCH_WORKSPACE,
FN_SPAWN_CUSTOM,
FN_STACK_BALANCE,
FN_STACK_INC,
FN_STACK_DEC,
FN_STACK_RESET,
FN_SWAP_MAIN,
FN_SWAP_NEXT,
FN_SWAP_PREV,
FN_UNICONIFY,
FN_VERSION,
FN_WIDTH_GROW,
FN_WIDTH_SHRINK,
FN_WIND_DEL,
FN_WIND_KILL,
FN_WS_1,
FN_WS_2,
FN_WS_3,
FN_WS_4,
FN_WS_5,
FN_WS_6,
FN_WS_7,
FN_WS_8,
FN_WS_9,
FN_WS_10,
FN_WS_11,
FN_WS_12,
FN_WS_13,
FN_WS_14,
FN_WS_15,
FN_WS_16,
FN_WS_17,
FN_WS_18,
FN_WS_19,
FN_WS_20,
FN_WS_21,
FN_WS_22,
FN_WS_NEXT,
FN_WS_NEXT_ALL,
FN_WS_NEXT_MOVE,
FN_WS_PREV,
FN_WS_PREV_ALL,
FN_WS_PREV_MOVE,
FN_WS_PRIOR,
/* SWM_DEBUG actions MUST be here: */
FN_DEBUG_TOGGLE,
FN_DUMPWINS,
/* ALWAYS last: */
FN_INVALID
};
enum binding_type {
KEYBIND,
BTNBIND
};
enum {
BINDING_F_REPLAY = 0x1,
};
struct binding {
RB_ENTRY(binding) entry;
uint16_t mod; /* Modifier Mask. */
enum binding_type type; /* Key or Button. */
uint32_t value; /* KeySym or Button Index. */
enum actionid action; /* Action Identifier. */
uint32_t flags;
char *spawn_name;
};
RB_HEAD(binding_tree, binding);
/* function prototypes */
void adjust_font(struct ws_win *);
char *argsep(char **);
void bar_cleanup(struct swm_region *);
void bar_extra_setup(void);
void bar_extra_stop(void);
int bar_extra_update(void);
void bar_fmt(const char *, char *, struct swm_region *, size_t);
void bar_fmt_expand(char *, size_t);
void bar_draw(struct swm_bar *);
void bar_print(struct swm_region *, const char *);
void bar_print_legacy(struct swm_region *, const char *);
void bar_replace(char *, char *, struct swm_region *, size_t);
void bar_replace_pad(char *, int *, size_t);
char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
void bar_setup(struct swm_region *);
void bar_toggle(struct binding *, struct swm_region *, union arg *);
void bar_urgent(char *, size_t);
void bar_window_class(char *, size_t, struct swm_region *);
void bar_window_class_instance(char *, size_t, struct swm_region *);
void bar_window_float(char *, size_t, struct swm_region *);
void bar_window_instance(char *, size_t, struct swm_region *);
void bar_window_name(char *, size_t, struct swm_region *);
void bar_window_state(char *, size_t, struct swm_region *);
void bar_workspace_name(char *, size_t, struct swm_region *);
int binding_cmp(struct binding *, struct binding *);
void binding_insert(uint16_t, enum binding_type, uint32_t, enum actionid,
uint32_t, const char *);
struct binding *binding_lookup(uint16_t, enum binding_type, uint32_t);
void binding_remove(struct binding *);
void buttonpress(xcb_button_press_event_t *);
void buttonrelease(xcb_button_release_event_t *);
void center_pointer(struct swm_region *);
void check_conn(void);
void clear_bindings(void);
void clear_keybindings(void);
int clear_maximized(struct workspace *);
void clear_quirks(void);
void clear_spawns(void);
void clientmessage(xcb_client_message_event_t *);
void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
int conf_load(const char *, int);
void configurenotify(xcb_configure_notify_event_t *);
void configurerequest(xcb_configure_request_event_t *);
void config_win(struct ws_win *, xcb_configure_request_event_t *);
void constrain_window(struct ws_win *, struct swm_geometry *, int *);
int count_win(struct workspace *, bool);
void cursors_cleanup(void);
void cursors_load(void);
void custom_region(const char *);
void cycle_layout(struct binding *, struct swm_region *, union arg *);
void cyclerg(struct binding *, struct swm_region *, union arg *);
void cyclews(struct binding *, struct swm_region *, union arg *);
#ifdef SWM_DEBUG
void debug_refresh(struct ws_win *);
#endif
void debug_toggle(struct binding *, struct swm_region *, union arg *);
void destroynotify(xcb_destroy_notify_event_t *);
void dumpwins(struct binding *, struct swm_region *, union arg *);
int enable_wm(void);
void enternotify(xcb_enter_notify_event_t *);
void event_drain(uint8_t);
void event_error(xcb_generic_error_t *);
void event_handle(xcb_generic_event_t *);
void ewmh_apply_flags(struct ws_win *, uint32_t);
void ewmh_autoquirk(struct ws_win *);
void ewmh_get_desktop_names(void);
void ewmh_get_wm_state(struct ws_win *);
void ewmh_update_actions(struct ws_win *);
void ewmh_update_client_list(void);
void ewmh_update_current_desktop(void);
void ewmh_update_desktop_names(void);
void ewmh_update_desktops(void);
void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
void ewmh_update_wm_state(struct ws_win *);
char *expand_tilde(const char *);
void expose(xcb_expose_event_t *);
void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
struct swm_bar *find_bar(xcb_window_t);
struct ws_win *find_frame_window(xcb_window_t);
struct pid_e *find_pid(pid_t);
struct swm_region *find_region(xcb_window_t);
struct ws_win *find_unmanaged_window(xcb_window_t);
struct ws_win *find_window(xcb_window_t);
void floating_toggle(struct binding *, struct swm_region *, union arg *);
void focus(struct binding *, struct swm_region *, union arg *);
void focus_flush(void);
void focus_pointer(struct binding *, struct swm_region *, union arg *);
void focus_region(struct swm_region *);
void focus_win(struct ws_win *);
void focusin(xcb_focus_in_event_t *);
#ifdef SWM_DEBUG
void focusout(xcb_focus_out_event_t *);
#endif
void focusrg(struct binding *, struct swm_region *, union arg *);
void fontset_init(void);
void free_window(struct ws_win *);
void fullscreen_toggle(struct binding *, struct swm_region *, union arg *);
xcb_atom_t get_atom_from_string(const char *);
#ifdef SWM_DEBUG
char *get_atom_name(xcb_atom_t);
#endif
struct ws_win *get_focus_magic(struct ws_win *);
struct ws_win *get_focus_prev(struct ws_win *);
xcb_generic_event_t *get_next_event(bool);
#ifdef SWM_DEBUG
char *get_notify_detail_label(uint8_t);
char *get_notify_mode_label(uint8_t);
#endif
struct ws_win *get_pointer_win(xcb_window_t);
struct ws_win *get_region_focus(struct swm_region *);
int get_region_index(struct swm_region *);
xcb_screen_t *get_screen(int);
int get_screen_count(void);
#ifdef SWM_DEBUG
char *get_source_type_label(uint32_t);
char *get_stack_mode_name(uint8_t);
char *get_state_mask_label(uint16_t);
#endif
int32_t get_swm_ws(xcb_window_t);
bool get_urgent(struct ws_win *);
#ifdef SWM_DEBUG
char *get_win_input_model(struct ws_win *);
#endif
char *get_win_name(xcb_window_t);
uint8_t get_win_state(xcb_window_t);
void get_wm_protocols(struct ws_win *);
int get_ws_idx(struct ws_win *);
void grab_windows(void);
void grabbuttons(void);
void grabkeys(void);
void iconify(struct binding *, struct swm_region *, union arg *);
bool isxlfd(char *);
bool keybindreleased(struct binding *, xcb_key_release_event_t *);
void keypress(xcb_key_press_event_t *);
void keyrelease(xcb_key_release_event_t *);
bool keyrepeating(xcb_key_release_event_t *);
void kill_bar_extra_atexit(void);
void kill_refs(struct ws_win *);
#ifdef SWM_DEBUG
void leavenotify(xcb_leave_notify_event_t *);
#endif
void load_float_geom(struct ws_win *);
void lower_window(struct ws_win *);
struct ws_win *manage_window(xcb_window_t, int, bool);
void map_window(struct ws_win *);
void mapnotify(xcb_map_notify_event_t *);
void mappingnotify(xcb_mapping_notify_event_t *);
void maprequest(xcb_map_request_event_t *);
void maximize_toggle(struct binding *, struct swm_region *, union arg *);
void motionnotify(xcb_motion_notify_event_t *);
void move(struct binding *, struct swm_region *, union arg *);
void move_win(struct ws_win *, struct binding *, int);
uint32_t name_to_pixel(int, const char *);
void name_workspace(struct binding *, struct swm_region *, union arg *);
void new_region(struct swm_screen *, int, int, int, int);
int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
int parsebinding(const char *, uint16_t *, enum binding_type *, uint32_t *,
uint32_t *);
int parsequirks(const char *, uint32_t *, int *);
void pressbutton(struct binding *, struct swm_region *, union arg *);
void priorws(struct binding *, struct swm_region *, union arg *);
#ifdef SWM_DEBUG
void print_win_geom(xcb_window_t);
#endif
void propertynotify(xcb_property_notify_event_t *);
void put_back_event(xcb_generic_event_t *);
void quirk_free(struct quirk *);
void quirk_insert(const char *, const char *, const char *, uint32_t, int);
void quirk_remove(struct quirk *);
void quirk_replace(struct quirk *, const char *, const char *, const char *,
uint32_t, int);
void quit(struct binding *, struct swm_region *, union arg *);
void raise_focus(struct binding *, struct swm_region *, union arg *);
void raise_toggle(struct binding *, struct swm_region *, union arg *);
void raise_window(struct ws_win *);
void region_containment(struct ws_win *, struct swm_region *, int);
struct swm_region *region_under(struct swm_screen *, int, int);
void regionize(struct ws_win *, int, int);
void reparent_window(struct ws_win *);
void reparentnotify(xcb_reparent_notify_event_t *);
void resize(struct binding *, struct swm_region *, union arg *);
void resize_win(struct ws_win *, struct binding *, int);
void restart(struct binding *, struct swm_region *, union arg *);
struct swm_region *root_to_region(xcb_window_t, int);
void screenchange(xcb_randr_screen_change_notify_event_t *);
void scan_randr(int);
void search_do_resp(void);
void search_resp_name_workspace(const char *, size_t);
void search_resp_search_window(const char *);
void search_resp_search_workspace(const char *);
void search_resp_uniconify(const char *, size_t);
void search_win(struct binding *, struct swm_region *, union arg *);
void search_win_cleanup(void);
void search_workspace(struct binding *, struct swm_region *, union arg *);
void send_to_rg(struct binding *, struct swm_region *, union arg *);
void send_to_rg_relative(struct binding *, struct swm_region *, union arg *);
void send_to_ws(struct binding *, struct swm_region *, union arg *);
void set_region(struct swm_region *);
int setautorun(const char *, const char *, int);
void setbinding(uint16_t, enum binding_type, uint32_t, enum actionid,
uint32_t, const char *);
int setconfbinding(const char *, const char *, int);
int setconfcolor(const char *, const char *, int);
int setconfmodkey(const char *, const char *, int);
int setconfquirk(const char *, const char *, int);
int setconfregion(const char *, const char *, int);
int setconfspawn(const char *, const char *, int);
int setconfvalue(const char *, const char *, int);
int setkeymapping(const char *, const char *, int);
int setlayout(const char *, const char *, int);
void setquirk(const char *, const char *, const char *, uint32_t, int);
void setscreencolor(const char *, int, int);
void setspawn(const char *, const char *, int);
void setup_btnbindings(void);
void setup_ewmh(void);
void setup_globals(void);
void setup_keybindings(void);
void setup_quirks(void);
void setup_screens(void);
void setup_spawn(void);
void set_child_transient(struct ws_win *, xcb_window_t *);
void set_win_state(struct ws_win *, uint8_t);
void shutdown_cleanup(void);
void sighdlr(int);
void socket_setnonblock(int);
void sort_windows(struct ws_win_list *);
void spawn(int, union arg *, bool);
void spawn_custom(struct swm_region *, union arg *, const char *);
int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
void spawn_insert(const char *, const char *, int);
struct spawn_prog *spawn_find(const char *);
void spawn_remove(struct spawn_prog *);
void spawn_replace(struct spawn_prog *, const char *, const char *, int);
void spawn_select(struct swm_region *, union arg *, const char *, int *);
void stack_config(struct binding *, struct swm_region *, union arg *);
void stack_master(struct workspace *, struct swm_geometry *, int, bool);
void store_float_geom(struct ws_win *);
char *strdupsafe(const char *);
void swapwin(struct binding *, struct swm_region *, union arg *);
void switchws(struct binding *, struct swm_region *, union arg *);
void teardown_ewmh(void);
void unescape_selector(char *);
void unfocus_win(struct ws_win *);
void uniconify(struct binding *, struct swm_region *, union arg *);
void unmanage_window(struct ws_win *);
void unmap_all(void);
void unmap_window(struct ws_win *);
void unmapnotify(xcb_unmap_notify_event_t *);
void unparent_window(struct ws_win *);
void update_floater(struct ws_win *);
void update_modkey(uint16_t);
void update_win_stacking(struct ws_win *);
void update_window(struct ws_win *);
void draw_frame(struct ws_win *);
void update_wm_state(struct ws_win *win);
void updatenumlockmask(void);
void validate_spawns(void);
int validate_win(struct ws_win *);
int validate_ws(struct workspace *);
void version(struct binding *, struct swm_region *, union arg *);
void win_to_ws(struct ws_win *, int, bool);
pid_t window_get_pid(xcb_window_t);
void wkill(struct binding *, struct swm_region *, union arg *);
void update_ws_stack(struct workspace *);
void xft_init(struct swm_region *);
void _add_startup_exception(const char *, va_list);
void add_startup_exception(const char *, ...);
RB_PROTOTYPE(binding_tree, binding, entry, binding_cmp);
RB_GENERATE(binding_tree, binding, entry, binding_cmp);
struct binding_tree bindings;
void
cursors_load(void)
{
xcb_font_t cf = XCB_NONE;
int i;
for (i = 0; i < LENGTH(cursors); ++i) {
/* try to load Xcursor first. */
cursors[i].cid = XcursorLibraryLoadCursor(display,
cursors[i].name);
/* fallback to cursorfont. */
if (cursors[i].cid == XCB_CURSOR_NONE) {
if (cf == XCB_NONE) {
cf = xcb_generate_id(conn);
xcb_open_font(conn, cf, strlen("cursor"),
"cursor");
}
cursors[i].cid = xcb_generate_id(conn);
xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
0xffff, 0xffff, 0xffff);
}
}
if (cf != XCB_NONE)
xcb_close_font(conn, cf);
}
void
cursors_cleanup(void)
{
int i;
for (i = 0; i < LENGTH(cursors); ++i)
xcb_free_cursor(conn, cursors[i].cid);
}
char *
expand_tilde(const char *s)
{
struct passwd *ppwd;
int i;
long max;
char *user;
const char *sc = s;
char *result;
if (s == NULL)
errx(1, "expand_tilde: NULL string.");
if (s[0] != '~') {
result = strdup(sc);
goto out;
}
++s;
if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
errx(1, "expand_tilde: sysconf");
if ((user = calloc(1, max + 1)) == NULL)
errx(1, "expand_tilde: calloc");
for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
user[i] = s[i];
user[i] = '\0';
s = &s[i];
ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
free(user);
if (ppwd == NULL)
result = strdup(sc);
else
if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
result = NULL;
out:
if (result == NULL)
errx(1, "expand_tilde: failed to allocate memory.");
return result;
}
int
parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
{
unsigned int tmpr, tmpg, tmpb;
if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
return (-1);
*rr = RGB_8_TO_16(tmpr);
*gg = RGB_8_TO_16(tmpg);
*bb = RGB_8_TO_16(tmpb);
return (0);
}
xcb_screen_t *
get_screen(int screen)
{
const xcb_setup_t *r;
xcb_screen_iterator_t iter;
if ((r = xcb_get_setup(conn)) == NULL) {
DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
check_conn();
}
iter = xcb_setup_roots_iterator(r);
for (; iter.rem; --screen, xcb_screen_next(&iter))
if (screen == 0)
return (iter.data);
return (NULL);
}
int
get_screen_count(void)
{
const xcb_setup_t *r;
if ((r = xcb_get_setup(conn)) == NULL) {
DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
check_conn();
}
return xcb_setup_roots_length(r);
}
int
get_region_index(struct swm_region *r)
{
struct swm_region *rr;
int ridx = 0;
if (r == NULL)
return -1;
TAILQ_FOREACH(rr, &r->s->rl, entry) {
if (rr == r)
break;
++ridx;
}
if (rr == NULL)
return -1;
return ridx;
}
void
focus_flush(void)
{
if (focus_mode == SWM_FOCUS_DEFAULT)
event_drain(XCB_ENTER_NOTIFY);
else
xcb_flush(conn);
}
xcb_atom_t
get_atom_from_string(const char *str)
{
xcb_intern_atom_cookie_t c;
xcb_intern_atom_reply_t *r;
xcb_atom_t atom;
c = xcb_intern_atom(conn, 0, strlen(str), str);
r = xcb_intern_atom_reply(conn, c, NULL);
if (r) {
atom = r->atom;
free(r);
return (atom);
}
return (XCB_ATOM_NONE);
}
void
get_wm_protocols(struct ws_win *win) {
int i;
xcb_icccm_get_wm_protocols_reply_t wpr;
if (xcb_icccm_get_wm_protocols_reply(conn,
xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
&wpr, NULL)) {
for (i = 0; i < (int)wpr.atoms_len; i++) {
if (wpr.atoms[i] == a_takefocus)
win->take_focus = true;
if (wpr.atoms[i] == a_delete)
win->can_delete = true;
}
xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
}
}
void
setup_ewmh(void)
{
xcb_window_t root, win;
int i, j, num_screens;
for (i = 0; i < LENGTH(ewmh); i++)
ewmh[i].atom = get_atom_from_string(ewmh[i].name);
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++) {
root = screens[i].root;
/* Set up _NET_SUPPORTING_WM_CHECK. */
win = xcb_generate_id(conn);
xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
XCB_COPY_FROM_PARENT, 0, NULL);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
ewmh[_NET_WM_NAME].atom, a_utf8_string,
8, strlen("spectrwm"), "spectrwm");
/* Report supported atoms */
xcb_delete_property(conn, root, a_net_supported);
for (j = 0; j < LENGTH(ewmh); j++)
xcb_change_property(conn, XCB_PROP_MODE_APPEND, root,
a_net_supported, XCB_ATOM_ATOM, 32, 1,
&ewmh[j].atom);
}
ewmh_update_desktops();
ewmh_get_desktop_names();
}
void
teardown_ewmh(void)
{
int i, num_screens;
xcb_window_t id;
xcb_get_property_cookie_t pc;
xcb_get_property_reply_t *pr;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++) {
/* Get the support check window and destroy it */
pc = xcb_get_property(conn, 0, screens[i].root, a_net_wm_check,
XCB_ATOM_WINDOW, 0, 1);
pr = xcb_get_property_reply(conn, pc, NULL);
if (pr == NULL)
continue;
if (pr->format == a_net_wm_check) {
id = *((xcb_window_t *)xcb_get_property_value(pr));
xcb_destroy_window(conn, id);
xcb_delete_property(conn, screens[i].root,
a_net_wm_check);
xcb_delete_property(conn, screens[i].root,
a_net_supported);
}
free(pr);
}
}
void
ewmh_autoquirk(struct ws_win *win)
{
xcb_get_property_reply_t *r;
xcb_get_property_cookie_t c;
xcb_atom_t *type;
int i, n;
c = xcb_get_property(conn, 0, win->id,
ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
r = xcb_get_property_reply(conn, c, NULL);
if (r == NULL)
return;
type = xcb_get_property_value(r);
n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
for (i = 0; i < n; i++) {
if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
break;
if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
break;
}
if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
win->quirks = SWM_Q_FLOAT;
break;
}
}
free(r);
}
void
ewmh_update_actions(struct ws_win *win)
{
xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
int n = 0;
if (win == NULL)
return;
actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
if (ABOVE(win)) {
actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
}
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
}
#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
#define _NET_WM_STATE_ADD 1 /* add/set property */
#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
void
ewmh_change_wm_state(struct ws_win *win, xcb_atom_t state, long action)
{
uint32_t flag = 0;
uint32_t new_flags;
#ifdef SWM_DEBUG
char *name;
name = get_atom_name(state);
DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: win %#x, state: %s, "
"action: %ld\n", WINID(win), name, action);
free(name);
#endif
if (win == NULL)
goto out;
if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
flag = EWMH_F_FULLSCREEN;
else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
flag = EWMH_F_ABOVE;
else if (state == ewmh[_NET_WM_STATE_HIDDEN].atom)
flag = EWMH_F_HIDDEN;
else if (state == ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom ||
state == ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom)
flag = EWMH_F_MAXIMIZED;
else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
flag = SWM_F_MANUAL;
else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
flag = EWMH_F_SKIP_PAGER;
else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
flag = EWMH_F_SKIP_TASKBAR;
/* Disallow unfloating transients. */
if (TRANS(win) && flag == EWMH_F_ABOVE)
goto out;
new_flags = win->ewmh_flags;
switch (action) {
case _NET_WM_STATE_REMOVE:
new_flags &= ~flag;
break;
case _NET_WM_STATE_ADD:
new_flags |= flag;
break;
case _NET_WM_STATE_TOGGLE:
new_flags ^= flag;
break;
}
ewmh_apply_flags(win, new_flags);
out:
DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: done.\n");
}
void
ewmh_apply_flags(struct ws_win *win, uint32_t pending)
{
struct workspace *ws;
uint32_t changed;
changed = win->ewmh_flags ^ pending;
if (changed == 0)
return;
DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: pending: %d\n", pending);
win->ewmh_flags = pending;
ws = win->ws;
if (changed & EWMH_F_HIDDEN) {
if (ICONIC(win)) {
if (focus_mode != SWM_FOCUS_FOLLOW)
ws->focus_pending = get_focus_prev(win);
unfocus_win(win);
unmap_window(win);
} else {
/* Reload floating geometry in case region changed. */
if (FLOATING(win))
load_float_geom(win);
/* The window is no longer iconic, prepare focus. */
if (focus_mode != SWM_FOCUS_FOLLOW)
ws->focus_pending = get_focus_magic(win);
raise_window(win);
}
}
if (changed & EWMH_F_ABOVE) {
if (ws->cur_layout != &layouts[SWM_MAX_STACK]) {
if (ABOVE(win))
load_float_geom(win);
else if (!MAXIMIZED(win))
store_float_geom(win);
win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
changed &= ~EWMH_F_MAXIMIZED;
raise_window(win);
} else {
/* Revert. */
win->ewmh_flags ^= EWMH_F_ABOVE & pending;
}
}
if (changed & EWMH_F_MAXIMIZED) {
/* VERT and/or HORZ changed. */
if (ABOVE(win)) {
if (!MAXIMIZED(win))
load_float_geom(win);
else
store_float_geom(win);
}
if (MAXIMIZED(win)) {
if (focus_mode != SWM_FOCUS_FOLLOW &&
ws->cur_layout != &layouts[SWM_MAX_STACK]) {
if (WS_FOCUSED(ws))
focus_win(win);
else
ws->focus_pending = win;
}
}
draw_frame(win);
raise_window(win);
}
if (changed & EWMH_F_FULLSCREEN) {
if (FULLSCREEN(win)) {
if (focus_mode != SWM_FOCUS_FOLLOW) {
if (WS_FOCUSED(ws))
focus_win(win);
else
ws->focus_pending = win;
}
} else {
load_float_geom(win);
}
win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
raise_window(win);
}
DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: done.\n");
}
void
ewmh_update_wm_state(struct ws_win *win) {
xcb_atom_t vals[SWM_EWMH_ACTION_COUNT_MAX];
int n = 0;
if (ICONIC(win))
vals[n++] = ewmh[_NET_WM_STATE_HIDDEN].atom;
if (FULLSCREEN(win))
vals[n++] = ewmh[_NET_WM_STATE_FULLSCREEN].atom;
if (MAXIMIZED_VERT(win))
vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom;
if (MAXIMIZED_HORZ(win))
vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom;
if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
vals[n++] = ewmh[_NET_WM_STATE_SKIP_PAGER].atom;
if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
vals[n++] = ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom;
if (win->ewmh_flags & EWMH_F_ABOVE)
vals[n++] = ewmh[_NET_WM_STATE_ABOVE].atom;
if (win->ewmh_flags & SWM_F_MANUAL)
vals[n++] = ewmh[_SWM_WM_STATE_MANUAL].atom;
if (n > 0)
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, n, vals);
else
xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
}
void
ewmh_get_wm_state(struct ws_win *win)
{
xcb_atom_t *states;
xcb_get_property_cookie_t c;
xcb_get_property_reply_t *r;
int i, n;
if (win == NULL)
return;
win->ewmh_flags = 0;
c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
XCB_ATOM_ATOM, 0, UINT32_MAX);
r = xcb_get_property_reply(conn, c, NULL);
if (r == NULL)
return;
states = xcb_get_property_value(r);
n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
for (i = 0; i < n; i++)
ewmh_change_wm_state(win, states[i], _NET_WM_STATE_ADD);
free(r);
}
/* events */
#ifdef SWM_DEBUG
void
dumpwins(struct binding *bp, struct swm_region *r, union arg *args)
{
struct ws_win *w;
uint32_t state;
xcb_get_window_attributes_cookie_t c;
xcb_get_window_attributes_reply_t *wa;
/* suppress unused warning since var is needed */
(void)bp;
(void)args;
if (r->ws == NULL) {
DPRINTF("dumpwins: invalid workspace\n");
return;
}
DPRINTF("=== managed window list ws %02d ===\n", r->ws->idx);
TAILQ_FOREACH(w, &r->ws->winlist, entry) {
state = get_win_state(w->id);
c = xcb_get_window_attributes(conn, w->id);
wa = xcb_get_window_attributes_reply(conn, c, NULL);
if (wa) {
DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
"transient: %#x\n", w->frame, w->id, wa->map_state,
state, w->transient);
free(wa);
} else
DPRINTF("win %#x, failed xcb_get_window_attributes\n",
w->id);
}
DPRINTF("=== stacking order (top down) === \n");
TAILQ_FOREACH(w, &r->ws->stack, stack_entry) {
DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
"iconic: %s\n", w->frame, w->id, YESNO(FULLSCREEN(w)),
YESNO(MAXIMIZED(w)), YESNO(ABOVE(w)), YESNO(ICONIC(w)));
}
DPRINTF("===== unmanaged window list =====\n");
TAILQ_FOREACH(w, &r->ws->unmanagedlist, entry) {
state = get_win_state(w->id);
c = xcb_get_window_attributes(conn, w->id);
wa = xcb_get_window_attributes_reply(conn, c, NULL);
if (wa) {
DPRINTF("win %#x, map_state: %d, state: %u, "
"transient: %#x\n", w->id, wa->map_state,
state, w->transient);
free(wa);
} else
DPRINTF("win %#x, failed xcb_get_window_attributes\n",
w->id);
}
DPRINTF("=================================\n");
}
void
debug_toggle(struct binding *b, struct swm_region *r, union arg *s)
{
struct ws_win *win;
int num_screens, i, j;
/* Suppress warnings. */
(void)b;
(void)r;
(void)s;
DNPRINTF(SWM_D_MISC, "debug_toggle\n");
debug_enabled = !debug_enabled;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
for (j = 0; j < workspace_limit; j++)
TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
debug_refresh(win);
xcb_flush(conn);
}
void
debug_refresh(struct ws_win *win)
{
struct ws_win *w;
XftDraw *draw;
XGlyphInfo info;
GC l_draw;
XGCValues l_gcv;
XRectangle l_ibox, l_lbox;
xcb_rectangle_t rect;
size_t len;
uint32_t wc[4], mask, width, height, gcv[1];
int widx, sidx;
char *s;
xcb_screen_t *screen;
if (debug_enabled) {
/* Create debug window if it doesn't exist. */
if (win->debug == XCB_WINDOW_NONE) {
if ((screen = get_screen(win->s->idx)) == NULL)
errx(1, "ERROR: can't get screen %d.",
win->s->idx);
win->debug = xcb_generate_id(conn);
wc[0] = win->s->c[SWM_S_COLOR_BAR].pixel;
wc[1] = win->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
wc[2] = screen->default_colormap;
xcb_create_window(conn, screen->root_depth, win->debug,
win->frame, 0, 0, 10, 10, 1,
XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual,
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL |
XCB_CW_COLORMAP, wc);
xcb_map_window(conn, win->debug);
}
/* Determine workspace window list index. */
widx = 0;
TAILQ_FOREACH(w, &win->ws->winlist, entry) {
++widx;
if (w == win)
break;
}
/* Determine stacking index (top down). */
sidx = 0;
TAILQ_FOREACH(w, &win->ws->stack, stack_entry) {
++sidx;
if (w == win)
break;
}
if (asprintf(&s, "%#x f:%#x wl:%d s:%d im:%s", win->id,
win->frame, widx, sidx, get_win_input_model(win)) == -1)
return;
len = strlen(s);
/* Update window to an appropriate dimension. */
if (bar_font_legacy) {
XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
width = l_lbox.width + 4;
height = bar_fs_extents->max_logical_extent.height + 4;
} else {
XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
&info);
width = info.width + 4;
height = bar_font->height + 4;
}
mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
if (win->bordered)
wc[0] = wc[1] = border_width;
else
wc[0] = wc[1] = 0;
wc[2] = width;
wc[3] = height;
xcb_configure_window(conn, win->debug, mask, wc);
/* Draw a filled rectangle to 'clear' window. */
rect.x = 0;
rect.y = 0;
rect.width = width;
rect.height = height;
gcv[0] = win->s->c[SWM_S_COLOR_BAR].pixel;
xcb_change_gc(conn, win->s->bar_gc, XCB_GC_FOREGROUND, gcv);
xcb_poly_fill_rectangle(conn, win->debug, win->s->bar_gc, 1,
&rect);
/* Draw text. */
if (bar_font_legacy) {
l_gcv.graphics_exposures = 0;
l_draw = XCreateGC(display, win->debug, 0, &l_gcv);
XSetForeground(display, l_draw,
win->s->c[SWM_S_COLOR_BAR_FONT].pixel);
DRAWSTRING(display, win->debug, bar_fs, l_draw, 2,
(bar_fs_extents->max_logical_extent.height -
l_lbox.height) / 2 - l_lbox.y, s, len);
XFreeGC(display, l_draw);
} else {
draw = XftDrawCreate(display, win->debug,
DefaultVisual(display, win->s->idx),
DefaultColormap(display, win->s->idx));
XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
(bar_height + bar_font->height) / 2 -
bar_font->descent, (FcChar8 *)s, len);
XftDrawDestroy(draw);
}
free(s);
} else if (win->debug != XCB_WINDOW_NONE) {
xcb_destroy_window(conn, win->debug);
win->debug = XCB_WINDOW_NONE;
}
}
#else
void
dumpwins(struct binding *b, struct swm_region *r, union arg *s)
{
(void)b;
(void)r;
(void)s;
}
void
debug_toggle(struct binding *b, struct swm_region *r, union arg *s)
{
(void)b;
(void)r;
(void)s;
}
#endif /* SWM_DEBUG */
void
sighdlr(int sig)
{
int saved_errno, status;
pid_t pid;
saved_errno = errno;
switch (sig) {
case SIGCHLD:
while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
if (pid == -1) {
if (errno == EINTR)
continue;
#ifdef SWM_DEBUG
if (errno != ECHILD)
warn("sighdlr: waitpid");
#endif /* SWM_DEBUG */
break;
}
if (pid == searchpid)
search_resp = 1;
#ifdef SWM_DEBUG
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
warnx("sighdlr: child exit status: %d",
WEXITSTATUS(status));
} else
warnx("sighdlr: child is terminated "
"abnormally");
#endif /* SWM_DEBUG */
}
break;
case SIGHUP:
restart_wm = 1;
break;
case SIGINT:
case SIGTERM:
case SIGQUIT:
running = 0;
break;
}
errno = saved_errno;
}
struct pid_e *
find_pid(pid_t pid)
{
struct pid_e *p = NULL;
DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
if (pid == 0)
return (NULL);
TAILQ_FOREACH(p, &pidlist, entry) {
if (p->pid == pid)
return (p);
}
return (NULL);
}
uint32_t
name_to_pixel(int sidx, const char *colorname)
{
uint32_t result = 0;
char cname[32] = "#";
xcb_screen_t *screen;
xcb_colormap_t cmap;
xcb_alloc_color_reply_t *cr;
xcb_alloc_named_color_reply_t *nr;
uint16_t rr, gg, bb;
screen = get_screen(sidx);
cmap = screen->default_colormap;
/* color is in format rgb://rr/gg/bb */
if (strncmp(colorname, "rgb:", 4) == 0) {
if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
warnx("could not parse rgb %s", colorname);
else {
cr = xcb_alloc_color_reply(conn,
xcb_alloc_color(conn, cmap, rr, gg, bb),
NULL);
if (cr) {
result = cr->pixel;
free(cr);
} else
warnx("color '%s' not found", colorname);
}
} else {
nr = xcb_alloc_named_color_reply(conn,
xcb_alloc_named_color(conn, cmap, strlen(colorname),
colorname), NULL);
if (nr == NULL) {
strlcat(cname, colorname + 2, sizeof cname - 1);
nr = xcb_alloc_named_color_reply(conn,
xcb_alloc_named_color(conn, cmap, strlen(cname),
cname), NULL);
}
if (nr) {
result = nr->pixel;
free(nr);
} else
warnx("color '%s' not found", colorname);
}
return (result);
}
void
setscreencolor(const char *val, int i, int c)
{
if (i < 0 || i >= get_screen_count())
return;
screens[i].c[c].pixel = name_to_pixel(i, val);
free(screens[i].c[c].name);
if ((screens[i].c[c].name = strdup(val)) == NULL)
err(1, "strdup");
}
void
fancy_stacker(struct workspace *ws)
{
strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
if (ws->cur_layout->l_stack == vertical_stack)
snprintf(ws->stacker, sizeof ws->stacker,
ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
else if (ws->cur_layout->l_stack == horizontal_stack)
snprintf(ws->stacker, sizeof ws->stacker,
ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
}
void
plain_stacker(struct workspace *ws)
{
strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
if (ws->cur_layout->l_stack == vertical_stack)
strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
sizeof ws->stacker);
else if (ws->cur_layout->l_stack == horizontal_stack)
strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
sizeof ws->stacker);
}
void
custom_region(const char *val)
{
unsigned int x, y, w, h;
int sidx, num_screens;
xcb_screen_t *screen;
DNPRINTF(SWM_D_CONF, "custom_region: %s\n", val);
num_screens = get_screen_count();
if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
errx(1, "invalid custom region, "
"should be 'screen[<n>]:<n>x<n>+<n>+<n>");
if (sidx < 1 || sidx > num_screens)
errx(1, "invalid screen index: %d out of bounds (maximum %d)",
sidx, num_screens);
sidx--;
if ((screen = get_screen(sidx)) == NULL)
errx(1, "ERROR: can't get screen %d.", sidx);
if (w < 1 || h < 1)
errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
if (x > screen->width_in_pixels ||
y > screen->height_in_pixels ||
w + x > screen->width_in_pixels ||
h + y > screen->height_in_pixels) {
warnx("ignoring region %ux%u+%u+%u - not within screen "
"boundaries (%ux%u)", w, h, x, y,
screen->width_in_pixels, screen->height_in_pixels);
return;
}
new_region(&screens[sidx], x, y, w, h);
}
void
socket_setnonblock(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
err(1, "fcntl F_GETFL");
flags |= O_NONBLOCK;
if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
err(1, "fcntl F_SETFL");
}
void
bar_print_legacy(struct swm_region *r, const char *s)
{
xcb_rectangle_t rect;
uint32_t gcv[1];
XGCValues gcvd;
int x = 0;
size_t len;
XRectangle ibox, lbox;
GC draw;
len = strlen(s);
XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
switch (bar_justify) {
case SWM_BAR_JUSTIFY_LEFT:
x = SWM_BAR_OFFSET;
break;
case SWM_BAR_JUSTIFY_CENTER:
x = (WIDTH(r) - lbox.width) / 2;
break;
case SWM_BAR_JUSTIFY_RIGHT:
x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
break;
}
if (x < SWM_BAR_OFFSET)
x = SWM_BAR_OFFSET;
rect.x = 0;
rect.y = 0;
rect.width = WIDTH(r->bar);
rect.height = HEIGHT(r->bar);
/* clear back buffer */
gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
/* draw back buffer */
gcvd.graphics_exposures = 0;
draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
lbox.y, s, len);
XFreeGC(display, draw);
/* blt */
xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
0, 0, WIDTH(r->bar), HEIGHT(r->bar));
}
void
bar_print(struct swm_region *r, const char *s)
{
size_t len;
xcb_rectangle_t rect;
uint32_t gcv[1];
int32_t x = 0;
XGlyphInfo info;
XftDraw *draw;
len = strlen(s);
XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
switch (bar_justify) {
case SWM_BAR_JUSTIFY_LEFT:
x = SWM_BAR_OFFSET;
break;
case SWM_BAR_JUSTIFY_CENTER:
x = (WIDTH(r) - info.width) / 2;
break;
case SWM_BAR_JUSTIFY_RIGHT:
x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
break;
}
if (x < SWM_BAR_OFFSET)
x = SWM_BAR_OFFSET;
rect.x = 0;
rect.y = 0;
rect.width = WIDTH(r->bar);
rect.height = HEIGHT(r->bar);
/* clear back buffer */
gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
/* draw back buffer */
draw = XftDrawCreate(display, r->bar->buffer,
DefaultVisual(display, r->s->idx),
DefaultColormap(display, r->s->idx));
XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
(HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
(FcChar8 *)s, len);
XftDrawDestroy(draw);
/* blt */
xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
0, 0, WIDTH(r->bar), HEIGHT(r->bar));
}
void
bar_extra_stop(void)
{
if (bar_pipe[0]) {
close(bar_pipe[0]);
bzero(bar_pipe, sizeof bar_pipe);
}
if (bar_pid) {
kill(bar_pid, SIGTERM);
bar_pid = 0;
}
strlcpy(bar_ext, "", sizeof bar_ext);
bar_extra = false;
}
void
bar_window_class(char *s, size_t sz, struct swm_region *r)
{
if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
return;
if (r->ws->focus->ch.class_name != NULL)
strlcat(s, r->ws->focus->ch.class_name, sz);
}
void
bar_window_instance(char *s, size_t sz, struct swm_region *r)
{
if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
return;
if (r->ws->focus->ch.instance_name != NULL)
strlcat(s, r->ws->focus->ch.instance_name, sz);
}
void
bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
{
if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
return;
bar_window_class(s, sz, r);
strlcat(s, ":", sz);
bar_window_instance(s, sz, r);
}
void
bar_window_state(char *s, size_t sz, struct swm_region *r)
{
if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
return;
if (MAXIMIZED(r->ws->focus))
strlcat(s, "(m)", sz);
else if (ABOVE(r->ws->focus))
strlcat(s, "(f)", sz);
}
void
bar_window_name(char *s, size_t sz, struct swm_region *r)
{
char *title;
if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
return;
title = get_win_name(r->ws->focus->id);
strlcat(s, title, sz);
free(title);
}
bool
get_urgent(struct ws_win *win)
{
xcb_icccm_wm_hints_t hints;
xcb_get_property_cookie_t c;
bool urgent = false;
if (win) {
c = xcb_icccm_get_wm_hints(conn, win->id);
if (xcb_icccm_get_wm_hints_reply(conn, c, &hints, NULL))
urgent = xcb_icccm_wm_hints_get_urgency(&hints);
}
return urgent;
}
void
bar_urgent(char *s, size_t sz)
{
struct ws_win *win;
int i, j, num_screens;
bool urgent[SWM_WS_MAX];
char b[8];
for (i = 0; i < workspace_limit; i++)
urgent[i] = false;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
for (j = 0; j < workspace_limit; j++)
TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
if (get_urgent(win))
urgent[j] = true;
for (i = 0; i < workspace_limit; i++) {
if (urgent[i]) {
snprintf(b, sizeof b, "%d ", i + 1);
strlcat(s, b, sz);
} else if (!urgent_collapse) {
strlcat(s, "- ", sz);
}
}
if (urgent_collapse && s[0])
s[strlen(s) - 1] = 0;
}
void
bar_workspace_name(char *s, size_t sz, struct swm_region *r)
{
if (r == NULL || r->ws == NULL)
return;
if (r->ws->name != NULL)
strlcat(s, r->ws->name, sz);
}
/* build the default bar format according to the defined enabled options */
void
bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
{
struct ws_win *w;
/* if format provided, just copy the buffers */
if (bar_format != NULL) {
strlcpy(fmtnew, fmtexp, sz);
return;
}
/* reset the output buffer */
*fmtnew = '\0';
strlcat(fmtnew, "+N:+I ", sz);
if (stack_enabled)
strlcat(fmtnew, "+S", sz);
strlcat(fmtnew, " ", sz);
/* only show the workspace name if there's actually one */
if (r != NULL && r->ws != NULL && r->ws->name != NULL)
strlcat(fmtnew, "<+D>", sz);
/* If enabled, only show the iconic count if there are iconic wins. */
if (iconic_enabled && r != NULL && r->ws != NULL)
TAILQ_FOREACH(w, &r->ws->winlist, entry)
if (ICONIC(w)) {
strlcat(fmtnew, "{+M}", sz);
break;
}
strlcat(fmtnew, "+3<", sz);
if (clock_enabled) {
strlcat(fmtnew, fmtexp, sz);
strlcat(fmtnew, "+4<", sz);
}
/* bar_urgent already adds the space before the last asterisk */
if (urgent_enabled)
strlcat(fmtnew, (urgent_collapse ? "*+U*+4<" : "* +U*+4<"), sz);
if (window_class_enabled) {
strlcat(fmtnew, "+C", sz);
if (!window_instance_enabled)
strlcat(fmtnew, "+4<", sz);
}
/* checks needed by the colon and floating strlcat(3) calls below */
if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
if (window_instance_enabled) {
if (window_class_enabled)
strlcat(fmtnew, ":", sz);
strlcat(fmtnew, "+T+4<", sz);
}
if (window_name_enabled) {
if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
strlcat(fmtnew, "+F ", sz);
strlcat(fmtnew, "+64W ", sz);
}
}
/* finally add the action script output and the version */
strlcat(fmtnew, "+4<+A+4<+V", sz);
}
void
bar_replace_pad(char *tmp, int *limit, size_t sz)
{
/* special case; no limit given, pad one space, instead */
if (*limit == (int)sz - 1)
*limit = 1;
snprintf(tmp, sz, "%*s", *limit, " ");
}
/* replaces the bar format character sequences (like in tmux(1)) */
char *
bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
size_t sz)
{
struct ws_win *w;
char *ptr;
char tmp[SWM_BAR_MAX];
int limit, size, count;
size_t len;
/* reset strlcat(3) buffer */
*tmp = '\0';
/* get number, if any */
fmt++;
size = 0;
if (sscanf(fmt, "%d%n", &limit, &size) != 1)
limit = sizeof tmp - 1;
if (limit <= 0 || limit >= (int)sizeof tmp)
limit = sizeof tmp - 1;
/* there is nothing to replace (ie EOL) */
fmt += size;
if (*fmt == '\0')
return (fmt);
switch (*fmt) {
case '<':
bar_replace_pad(tmp, &limit, sizeof tmp);
break;
case 'A':
snprintf(tmp, sizeof tmp, "%s", bar_ext);
break;
case 'C':
bar_window_class(tmp, sizeof tmp, r);
break;
case 'D':
bar_workspace_name(tmp, sizeof tmp, r);
break;
case 'F':
bar_window_state(tmp, sizeof tmp, r);
break;
case 'I':
snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
break;
case 'M':
count = 0;
TAILQ_FOREACH(w, &r->ws->winlist, entry)
if (ICONIC(w))
++count;
snprintf(tmp, sizeof tmp, "%d", count);
break;
case 'N':
snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
break;
case 'P':
bar_window_class_instance(tmp, sizeof tmp, r);
break;
case 'S':
snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
break;
case 'T':
bar_window_instance(tmp, sizeof tmp, r);
break;
case 'U':
bar_urgent(tmp, sizeof tmp);
break;
case 'V':
snprintf(tmp, sizeof tmp, "%s", bar_vertext);
break;
case 'W':
bar_window_name(tmp, sizeof tmp, r);
break;
default:
/* unknown character sequence; copy as-is */
snprintf(tmp, sizeof tmp, "+%c", *fmt);
break;
}
len = strlen(tmp);
ptr = tmp;
if ((int)len < limit)
limit = len;
while (limit-- > 0) {
if (*offrep >= sz - 1)
break;
fmtrep[(*offrep)++] = *ptr++;
}
fmt++;
return (fmt);
}
void
bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
{
size_t off;
off = 0;
while (*fmt != '\0') {
if (*fmt != '+') {
/* skip ordinary characters */
if (off >= sz - 1)
break;
fmtrep[off++] = *fmt++;
continue;
}
/* character sequence found; replace it */
fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
if (off >= sz - 1)
break;
}
fmtrep[off] = '\0';
}
void
bar_fmt_expand(char *fmtexp, size_t sz)
{
char *fmt = NULL;
size_t len;
struct tm tm;
time_t tmt;
/* start by grabbing the current time and date */
time(&tmt);
localtime_r(&tmt, &tm);
/* figure out what to expand */
if (bar_format != NULL)
fmt = bar_format;
else if (bar_format == NULL && clock_enabled)
fmt = clock_format;
/* if nothing to expand bail out */
if (fmt == NULL) {
*fmtexp = '\0';
return;
}
/* copy as-is, just in case the format shouldn't be expanded below */
strlcpy(fmtexp, fmt, sz);
/* finally pass the string through strftime(3) */
#ifndef SWM_DENY_CLOCK_FORMAT
if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
warnx("format too long");
fmtexp[len] = '\0';
#endif
}
/* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
void
bar_draw(struct swm_bar *bar)
{
struct swm_region *r;
char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
char fmtrep[SWM_BAR_MAX];
/* expand the format by first passing it through strftime(3) */
bar_fmt_expand(fmtexp, sizeof fmtexp);
if (bar == NULL)
return;
r = bar->r;
if (bar_enabled && r->ws->bar_enabled)
xcb_map_window(conn, bar->id);
else {
xcb_unmap_window(conn, bar->id);
return;
}
if (startup_exception)
snprintf(fmtrep, sizeof fmtrep, "total "
"exceptions: %d, first exception: %s",
nr_exceptions,
startup_exception);
else {
bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
}
if (bar_font_legacy)
bar_print_legacy(r, fmtrep);
else
bar_print(r, fmtrep);
}
/*
* Reads external script output; call when stdin is readable.
* Returns 1 if bar_ext was updated; otherwise 0.
*/
int
bar_extra_update(void)
{
size_t len;
char b[SWM_BAR_MAX];
bool changed = false;
if (!bar_extra)
return changed;
while (fgets(b, sizeof(b), stdin) != NULL) {
if (bar_enabled) {
len = strlen(b);
if (b[len - 1] == '\n') {
/* Remove newline. */
b[--len] = '\0';
/* "Clear" bar_ext. */
bar_ext[0] = '\0';
/* Flush buffered output. */
strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
bar_ext_buf[0] = '\0';
/* Append new output to bar. */
strlcat(bar_ext, b, sizeof(bar_ext));
changed = true;
} else {
/* Buffer output. */
strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
}
}
}
if (errno != EAGAIN) {
warn("bar_action failed");
bar_extra_stop();
changed = true;
}
return changed;
}
void
bar_toggle(struct binding *bp, struct swm_region *r, union arg *args)
{
struct swm_region *tmpr;
int i, num_screens;
/* suppress unused warnings since vars are needed */
(void)bp;
(void)r;
(void)args;
DNPRINTF(SWM_D_BAR, "bar_toggle\n");
switch (args->id) {
case SWM_ARG_ID_BAR_TOGGLE_WS:
/* Only change if master switch is enabled. */
if (bar_enabled)
r->ws->bar_enabled = !r->ws->bar_enabled;
else
bar_enabled = r->ws->bar_enabled = true;
break;
case SWM_ARG_ID_BAR_TOGGLE:
bar_enabled = !bar_enabled;
break;
}
/* update bars as necessary */
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
if (tmpr->bar) {
if (bar_enabled && tmpr->ws->bar_enabled)
xcb_map_window(conn, tmpr->bar->id);
else
xcb_unmap_window(conn, tmpr->bar->id);
}
/* Restack all regions and redraw bar. */
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(tmpr, &screens[i].rl, entry) {
stack(tmpr);
bar_draw(tmpr->bar);
}
focus_flush();
}
void
bar_extra_setup(void)
{
/* do this here because the conf file is in memory */
if (!bar_extra && bar_argv[0]) {
/* launch external status app */
bar_extra = true;
if (pipe(bar_pipe) == -1)
err(1, "pipe error");
socket_setnonblock(bar_pipe[0]);
socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
/* Set stdin to read from the pipe. */
if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
err(1, "dup2");
/* Set stdout to write to the pipe. */
if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
err(1, "dup2");
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
err(1, "could not disable SIGPIPE");
switch (bar_pid = fork()) {
case -1:
err(1, "cannot fork");
break;
case 0: /* child */
close(bar_pipe[0]);
execvp(bar_argv[0], bar_argv);
err(1, "%s external app failed", bar_argv[0]);
break;
default: /* parent */
close(bar_pipe[1]);
break;
}
atexit(kill_bar_extra_atexit);
}
}
void
kill_bar_extra_atexit(void)
{
if (bar_pid)
kill(bar_pid, SIGTERM);
}
bool
isxlfd(char *s)
{
int count = 0;
while ((s = index(s, '-'))) {
++count;
++s;
}
return (count == 14);
}
void
fontset_init(void)
{
char *default_string;
char **missing_charsets;
int num_missing_charsets = 0;
int i;
if (bar_fs) {
XFreeFontSet(display, bar_fs);
bar_fs = NULL;
}
DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n",
bar_fonts);
bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
&num_missing_charsets, &default_string);
if (num_missing_charsets > 0) {
warnx("Unable to load charset(s):");
for (i = 0; i < num_missing_charsets; ++i)
warnx("%s", missing_charsets[i]);
XFreeStringList(missing_charsets);
if (strcmp(default_string, ""))
warnx("Glyphs from those sets will be replaced "
"by '%s'.", default_string);
else
warnx("Glyphs from those sets won't be drawn.");
}
if (bar_fs == NULL)
errx(1, "Error creating font set structure.");
bar_fs_extents = XExtentsOfFontSet(bar_fs);
bar_height = bar_fs_extents->max_logical_extent.height +
2 * bar_border_width;
if (bar_height < 1)
bar_height = 1;
}
void
xft_init(struct swm_region *r)
{
char *font, *str, *search;
XRenderColor color;
if (bar_font == NULL) {
if ((search = str = strdup(bar_fonts)) == NULL)
errx(1, "insufficient memory.");
while ((font = strsep(&search, ",")) != NULL) {
if (*font == '\0')
continue;
DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
if (isxlfd(font)) {
bar_font = XftFontOpenXlfd(display, r->s->idx,
font);
} else {
bar_font = XftFontOpenName(display, r->s->idx,
font);
}
if (bar_font == NULL) {
warnx("unable to load font %s", font);
continue;
} else {
DNPRINTF(SWM_D_INIT, "successfully opened "
"font %s\n", font);
break;
}
}
free(str);
}
if (bar_font == NULL)
errx(1, "unable to open a font");
PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
DefaultColormap(display, r->s->idx), &color, &bar_font_color))
warn("Xft error: unable to allocate color.");
PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR].pixel, color);
if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
DefaultColormap(display, r->s->idx), &color, &search_font_color))
warn("Xft error: unable to allocate color.");
bar_height = bar_font->height + 2 * bar_border_width;
if (bar_height < 1)
bar_height = 1;
}
void
bar_setup(struct swm_region *r)
{
xcb_screen_t *screen;
uint32_t wa[3];
DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
r->s->idx);
if ((screen = get_screen(r->s->idx)) == NULL)
errx(1, "ERROR: can't get screen %d.", r->s->idx);
if (r->bar != NULL)
return;
if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
err(1, "bar_setup: calloc: failed to allocate memory.");
if (bar_font_legacy)
fontset_init();
else
xft_init(r);
r->bar->r = r;
X(r->bar) = X(r);
Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
/* Assume region is unfocused when we create the bar. */
r->bar->id = xcb_generate_id(conn);
wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
XCB_EVENT_MASK_POINTER_MOTION_HINT;
xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
| XCB_CW_EVENT_MASK, wa);
/* Stack bar window above region window to start. */
wa[0] = r->id;
wa[1] = XCB_STACK_MODE_ABOVE;
xcb_configure_window(conn, r->bar->id, XCB_CONFIG_WINDOW_SIBLING |
XCB_CONFIG_WINDOW_STACK_MODE, wa);
r->bar->buffer = xcb_generate_id(conn);
xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
WIDTH(r->bar), HEIGHT(r->bar));
if (randr_support)
xcb_randr_select_input(conn, r->bar->id,
XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
if (bar_enabled)
xcb_map_window(conn, r->bar->id);
DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
"%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
HEIGHT(r->bar));
bar_extra_setup();
}
void
bar_cleanup(struct swm_region *r)
{
if (r->bar == NULL)
return;
xcb_destroy_window(conn, r->bar->id);
xcb_free_pixmap(conn, r->bar->buffer);
free(r->bar);
r->bar = NULL;
}
void
set_win_state(struct ws_win *win, uint8_t state)
{
uint32_t data[2] = { state, XCB_ATOM_NONE };
DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
WINID(win), state);
if (win == NULL)
return;
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
a_state, 32, 2, data);
}
uint8_t
get_win_state(xcb_window_t w)
{
xcb_get_property_reply_t *r;
xcb_get_property_cookie_t c;
uint32_t result = 0;
c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
r = xcb_get_property_reply(conn, c, NULL);
if (r) {
if (r->type == a_state && r->format == 32 && r->length == 2)
result = *((uint32_t *)xcb_get_property_value(r));
free(r);
}
DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
result);
return (result);
}
void
version(struct binding *bp, struct swm_region *r, union arg *args)
{
struct swm_region *tmpr;
int i, num_screens;
/* suppress unused warnings since vars are needed */
(void)bp;
(void)r;
(void)args;
bar_version = !bar_version;
if (bar_version)
snprintf(bar_vertext, sizeof bar_vertext,
"Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
else
strlcpy(bar_vertext, "", sizeof bar_vertext);
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++) {
TAILQ_FOREACH(tmpr, &screens[i].rl, entry) {
bar_draw(tmpr->bar);
xcb_flush(conn);
}
}
}
void
client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
{
xcb_client_message_event_t ev;
#ifdef SWM_DEBUG
char *name;
#endif
if (win == NULL)
return;
#ifdef SWM_DEBUG
name = get_atom_name(a);
DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
"time: %#x\n",
win->id, name, a, t);
free(name);
#endif
bzero(&ev, sizeof ev);
ev.response_type = XCB_CLIENT_MESSAGE;
ev.window = win->id;
ev.type = a_prot;
ev.format = 32;
ev.data.data32[0] = a;
ev.data.data32[1] = t;
xcb_send_event(conn, 0, win->id,
XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
}
/* synthetic response to a ConfigureRequest when not making a change */
void
config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
{
xcb_configure_notify_event_t ce;
if (win == NULL)
return;
/* send notification of unchanged state. */
bzero(&ce, sizeof(ce));
ce.response_type = XCB_CONFIGURE_NOTIFY;
ce.x = X(win);
ce.y = Y(win);
ce.width = WIDTH(win);
ce.height = HEIGHT(win);
ce.border_width = 0;
ce.override_redirect = 0;
if (ev == NULL) {
/* EWMH */
ce.event = win->id;
ce.window = win->id;
ce.above_sibling = XCB_WINDOW_NONE;
} else {
/* normal */
ce.event = ev->window;
ce.window = ev->window;
/* make response appear more WM_SIZE_HINTS-compliant */
if (win->sh.flags) {
DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
" sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
"%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
SH_INC_W(win), SH_INC_H(win));
}
/* min size */
if (SH_MIN(win)) {
/* the hint may be set... to 0! */
if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
ce.width = SH_MIN_W(win);
if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
ce.height = SH_MIN_H(win);
}
/* max size */
if (SH_MAX(win)) {
/* may also be advertized as 0 */
if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
ce.width = SH_MAX_W(win);
if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
ce.height = SH_MAX_H(win);
}
/* resize increment. */
if (SH_INC(win)) {
if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
ce.width -= (ce.width - SH_MIN_W(win)) %
SH_INC_W(win);
if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
ce.height -= (ce.height - SH_MIN_H(win)) %
SH_INC_H(win);
}
/* adjust x and y for requested border_width. */
ce.x += ev->border_width;
ce.y += ev->border_width;
ce.above_sibling = ev->sibling;
}
DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
"(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
ce.y, ce.width, ce.height, ce.border_width);
xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
(char *)&ce);
}
int
count_win(struct workspace *ws, bool count_transient)
{
struct ws_win *win;
int count = 0;
TAILQ_FOREACH(win, &ws->winlist, entry) {
if (!count_transient && FLOATING(win))
continue;
if (ICONIC(win))
continue;
count++;
}
DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
return (count);
}
void
quit(struct binding *bp, struct swm_region *r, union arg *args)
{
/* suppress unused warnings since vars are needed */
(void)bp;
(void)r;
(void)args;
DNPRINTF(SWM_D_MISC, "quit\n");
running = 0;
}
void
lower_window(struct ws_win *win)
{
struct ws_win *target = NULL;
struct workspace *ws;
DNPRINTF(SWM_D_EVENT, "lower_window: win %#x\n", WINID(win));
if (win == NULL)
return;
ws = win->ws;
TAILQ_FOREACH(target, &ws->stack, stack_entry) {
if (target == win || ICONIC(target))
continue;
if (ws->cur_layout == &layouts[SWM_MAX_STACK])
break;
if (TRANS(win)) {
if (win->transient == target->transient)
continue;
if (win->transient == target->id)
break;
}
if (FULLSCREEN(target))
continue;
if (FULLSCREEN(win))
break;
if (MAXIMIZED(target))
continue;
if (MAXIMIZED(win))
break;
if (ABOVE(target) || TRANS(target))
continue;
if (ABOVE(win) || TRANS(win))
break;
}
/* Change stack position. */
TAILQ_REMOVE(&ws->stack, win, stack_entry);
if (target)
TAILQ_INSERT_BEFORE(target, win, stack_entry);
else
TAILQ_INSERT_TAIL(&ws->stack, win, stack_entry);
update_win_stacking(win);
#ifdef SWM_DEBUG
if (swm_debug & SWM_D_STACK) {
DPRINTF("=== stacking order (top down) === \n");
TAILQ_FOREACH(target, &ws->stack, stack_entry) {
DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
"iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
YESNO(ICONIC(target)));
}
}
#endif
DNPRINTF(SWM_D_EVENT, "lower_window: done\n");
}
void
raise_window(struct ws_win *win)
{
struct ws_win *target = NULL;
struct workspace *ws;
DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", WINID(win));
if (win == NULL)
return;
ws = win->ws;
TAILQ_FOREACH(target, &ws->stack, stack_entry) {
if (target == win || ICONIC(target))
continue;
if (ws->cur_layout == &layouts[SWM_MAX_STACK])
break;
if (TRANS(win) && (win->transient == target->transient ||
win->transient == target->id))
break;
if (FULLSCREEN(win))
break;
if (FULLSCREEN(target))
continue;
if (MAXIMIZED(win))
break;
if (MAXIMIZED(target))
continue;
if (ABOVE(win) || TRANS(win) ||
(win->ws->focus == win && ws->always_raise))
break;
if (!ABOVE(target) && !TRANS(target))
break;
}
TAILQ_REMOVE(&ws->stack, win, stack_entry);
if (target)
TAILQ_INSERT_BEFORE(target, win, stack_entry);
else
TAILQ_INSERT_TAIL(&ws->stack, win, stack_entry);
update_win_stacking(win);
#ifdef SWM_DEBUG
if (swm_debug & SWM_D_STACK) {
DPRINTF("=== stacking order (top down) === \n");
TAILQ_FOREACH(target, &ws->stack, stack_entry) {
DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
"iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
YESNO(ICONIC(target)));
}
}
#endif
DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
}
void
update_win_stacking(struct ws_win *win)
{
struct ws_win *sibling;
#ifdef SWM_DEBUG
struct ws_win *w;
#endif
struct swm_region *r;
uint32_t val[2];
if (win == NULL || (r = win->ws->r) == NULL)
return;
if (win->frame == XCB_WINDOW_NONE) {
DNPRINTF(SWM_D_EVENT, "update_window_stacking: win %#x not "
"reparented.\n", win->id);
return;
}
sibling = TAILQ_NEXT(win, stack_entry);
if (sibling != NULL && (FLOATING(win) == FLOATING(sibling) ||
(win->ws->always_raise && win->ws->focus == win)))
val[0] = sibling->frame;
else if (FLOATING(win) || (win->ws->always_raise &&
win->ws->focus == win))
val[0] = r->bar->id;
else
val[0] = r->id;
DNPRINTF(SWM_D_EVENT, "update_win_stacking: win %#x (%#x), "
"sibling %#x\n", win->frame, win->id, val[0]);
val[1] = XCB_STACK_MODE_ABOVE;
xcb_configure_window(conn, win->frame, XCB_CONFIG_WINDOW_SIBLING |
XCB_CONFIG_WINDOW_STACK_MODE, val);
#ifdef SWM_DEBUG
TAILQ_FOREACH(w, &win->ws->winlist, entry)
debug_refresh(w);
#endif
}
void
map_window(struct ws_win *win)
{
if (win == NULL)
return;
DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
win->id, YESNO(win->mapped));
if (win->mapped)
return;
xcb_map_window(conn, win->frame);
xcb_map_window(conn, win->id);
set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
win->mapped = true;
}
void
unmap_window(struct ws_win *win)
{
if (win == NULL)
return;
DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
YESNO(win->mapped));
if (!win->mapped)
return;
xcb_unmap_window(conn, win->id);
xcb_unmap_window(conn, win->frame);
set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
win->mapped = false;
}
void
unmap_all(void)
{
struct ws_win *win;
int i, j, num_screens;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
for (j = 0; j < workspace_limit; j++)
TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
unmap_window(win);
}
void
fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
{
xcb_key_press_event_t event;
xcb_keycode_t *keycode;
if (win == NULL)
return;
keycode = xcb_key_symbols_get_keycode(syms, keysym);
DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
win->id, *keycode);
bzero(&event, sizeof(event));
event.event = win->id;
event.root = win->s->root;
event.child = XCB_WINDOW_NONE;
event.time = XCB_CURRENT_TIME;
event.event_x = X(win);
event.event_y = Y(win);
event.root_x = 1;
event.root_y = 1;
event.same_screen = 1;
event.detail = *keycode;
event.state = modifiers;
event.response_type = XCB_KEY_PRESS;
xcb_send_event(conn, 1, win->id,
XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
event.response_type = XCB_KEY_RELEASE;
xcb_send_event(conn, 1, win->id,
XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
free(keycode);
}
void
restart(struct binding *bp, struct swm_region *r, union arg *args)
{
/* suppress unused warning since var is needed */
(void)bp;
(void)r;
(void)args;
DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
shutdown_cleanup();
execvp(start_argv[0], start_argv);
warn("execvp failed");
quit(NULL, NULL, NULL);
}
struct ws_win *
get_pointer_win(xcb_window_t root)
{
struct ws_win *win = NULL;
xcb_query_pointer_reply_t *r;
DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
if (r) {
win = find_window(r->child);
if (win) {
DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
win->id);
} else {
DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
}
free(r);
}
return win;
}
void
center_pointer(struct swm_region *r)
{
struct ws_win *win;
if (!warp_pointer || r == NULL)
return;
win = r->ws->focus;
DNPRINTF(SWM_D_EVENT, "center_pointer: win %#x.\n", WINID(win));
if (win && win->mapped)
xcb_warp_pointer(conn, XCB_NONE, win->frame, 0, 0, 0, 0,
WIDTH(win) / 2, HEIGHT(win) / 2);
else
xcb_warp_pointer(conn, XCB_NONE, r->id, 0, 0, 0, 0,
WIDTH(r) / 2, HEIGHT(r) / 2);
}
struct swm_region *
root_to_region(xcb_window_t root, int check)
{
struct ws_win *cfw;
struct swm_region *r = NULL;
int i, num_screens;
xcb_query_pointer_reply_t *qpr;
xcb_get_input_focus_reply_t *gifr;
DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
if (screens[i].root == root)
break;
if (check & SWM_CK_REGION)
r = screens[i].r_focus;
if (r == NULL && check & SWM_CK_FOCUS) {
/* Try to find an actively focused window */
gifr = xcb_get_input_focus_reply(conn,
xcb_get_input_focus(conn), NULL);
if (gifr) {
cfw = find_window(gifr->focus);
if (cfw && cfw->ws->r)
r = cfw->ws->r;
free(gifr);
}
}
if (r == NULL && check & SWM_CK_POINTER) {
/* No region with an active focus; try to use pointer. */
qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
screens[i].root), NULL);
if (qpr) {
DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
"(%d,%d)\n", qpr->root_x, qpr->root_y);
TAILQ_FOREACH(r, &screens[i].rl, entry)
if (X(r) <= qpr->root_x &&
qpr->root_x < MAX_X(r) &&
Y(r) <= qpr->root_y &&
qpr->root_y < MAX_Y(r))
break;
free(qpr);
}
}
/* Last resort. */
if (r == NULL && check & SWM_CK_FALLBACK)
r = TAILQ_FIRST(&screens[i].rl);
DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
return (r);
}
struct swm_region *
find_region(xcb_window_t id)
{
struct swm_region *r;
int i, num_screens;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(r, &screens[i].rl, entry)
if (r->id == id)
return r;
return NULL;
}
struct swm_bar *
find_bar(xcb_window_t id)
{
struct swm_region *r;
int i, num_screens;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(r, &screens[i].rl, entry)
if (r->bar && r->bar->id == id)
return r->bar;
return NULL;
}
struct ws_win *
find_frame_window(xcb_window_t id) {
struct swm_region *r;
struct ws_win *w;
int i, num_screens;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(r, &screens[i].rl, entry)
TAILQ_FOREACH(w, &r->ws->winlist, entry)
if (w->frame == id)
return w;
return NULL;
}
struct ws_win *
find_window(xcb_window_t id)
{
struct ws_win *win = NULL;
int i, j, num_screens;
xcb_query_tree_reply_t *qtr;
DNPRINTF(SWM_D_MISC, "find_window: id: %#x\n", id);
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
for (j = 0; j < workspace_limit; j++)
TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
if (id == win->id || id == win->frame)
return (win);
/* If window isn't top-level, try to find managed ancestor. */
qtr = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
if (qtr) {
if (qtr->parent != XCB_WINDOW_NONE && qtr->parent != qtr->root)
win = find_window(qtr->parent);
#ifdef SWM_DEBUG
if (win)
DNPRINTF(SWM_D_MISC, "find_window: found child %#x "
"of %#x.\n", win->id, qtr->parent);
#endif
free(qtr);
}
return (win);
}
struct ws_win *
find_unmanaged_window(xcb_window_t id)
{
struct ws_win *win;
int i, j, num_screens;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
for (j = 0; j < workspace_limit; j++)
TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
entry)
if (id == win->id)
return (win);
return (NULL);
}
void
spawn(int ws_idx, union arg *args, bool close_fd)
{
int fd;
char *ret = NULL;
DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
close(xcb_get_file_descriptor(conn));
if ((ret = getenv("LD_PRELOAD"))) {
if (asprintf(&ret, "%s:%s", SWM_LIB, ret) == -1) {
warn("spawn: asprintf LD_PRELOAD");
_exit(1);
}
setenv("LD_PRELOAD", ret, 1);
free(ret);
} else {
setenv("LD_PRELOAD", SWM_LIB, 1);
}
if (asprintf(&ret, "%d", ws_idx) == -1) {
warn("spawn: asprintf SWM_WS");
_exit(1);
}
setenv("_SWM_WS", ret, 1);
free(ret);
ret = NULL;
if (asprintf(&ret, "%d", getpid()) == -1) {
warn("spawn: asprintf _SWM_PID");
_exit(1);
}
setenv("_SWM_PID", ret, 1);
free(ret);
ret = NULL;
if (setsid() == -1) {
warn("spawn: setsid");
_exit(1);
}
if (close_fd) {
/*
* close stdin and stdout to prevent interaction between apps
* and the baraction script
* leave stderr open to record errors
*/
if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
warn("spawn: open");
_exit(1);
}
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
if (fd > 2)
close(fd);
}
if (signal(SIGPIPE, SIG_DFL) == SIG_ERR)
err(1, "could not reset SIGPIPE");
execvp(args->argv[0], args->argv);
warn("spawn: execvp");
_exit(1);
}
void
kill_refs(struct ws_win *win)
{
struct workspace *ws;
struct ws_win *w;
int i, j, num_screens;
if (win == NULL)
return;
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++) {
for (j = 0; j < workspace_limit; j++) {
ws = &screens[i].ws[j];
if (win == ws->focus)
ws->focus = NULL;
if (win == ws->focus_prev)
ws->focus_prev = NULL;
if (win == ws->focus_pending)
ws->focus_pending = NULL;
if (win == ws->focus_raise)
ws->focus_raise = NULL;
if (TRANS(win))
TAILQ_FOREACH(w, &ws->winlist, entry)
if (win == w->focus_child)
w->focus_child = NULL;
}
}
}
int
validate_win(struct ws_win *testwin)
{
struct ws_win *win;
struct workspace *ws;
struct swm_region *r;
int i, x, num_screens;
if (testwin == NULL)
return (0);
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(r, &screens[i].rl, entry)
for (x = 0; x < workspace_limit; x++) {
ws = &r->s->ws[x];
TAILQ_FOREACH(win, &ws->winlist, entry)
if (win == testwin)
return (0);
}
return (1);
}
int
validate_ws(struct workspace *testws)
{
struct swm_region *r;
struct workspace *ws;
int i, x, num_screens;
/* validate all ws */
num_screens = get_screen_count();
for (i = 0; i < num_screens; i++)
TAILQ_FOREACH(r, &screens[i].rl, entry)
for (x = 0; x < workspace_limit; x++) {
ws = &r->s->ws[x];
if (ws == testws)
return (0);
}
return (1);
}
void
unfocus_win(struct ws_win *win)
{
xcb_window_t none = XCB_WINDOW_NONE;
DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
if (win == NULL)
return;
if (win->ws == NULL) {
DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
return;
}
if (validate_ws(win->ws)) {
DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
return;
}
if (win->ws->r == NULL) {
DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
return;
}
if (validate_win(win)) {
DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
kill_refs(win);
return;
}
if (win->ws->focus == win) {
win->ws->focus = NULL;
win->ws->focus_prev = win;
if (win->ws->focus_raise == win && !FLOATING(win)) {
update_win_stacking(win);
}
}
if (validate_win(win->ws->focus)) {
kill_refs(win->ws->focus);
win->ws->focus = NULL;
}
if (validate_win(win->ws->focus_prev)) {
kill_refs(win->ws->focus_prev);
win->ws->focus_prev = NULL;
}
draw_frame(win);
/* Raise window to "top unfocused position." */
if (win->ws->always_raise)
raise_window(win);
/* Update border width */
if (win->bordered && (win->quirks & SWM_Q_MINIMALBORDER) &&
FLOATING(win)) {
win->bordered = 0;
X(win) += border_width;
Y(win) += border_width;
update_window(win);
}
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
}
void
focus_win(struct ws_win *win)
{
struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
struct workspace *ws;
xcb_get_input_focus_reply_t *gifr = NULL;
xcb_get_window_attributes_reply_t *war = NULL;
DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
if (win == NULL || win->ws == NULL || !win->mapped)
goto out;
ws = win->ws;
if (validate_ws(ws))
goto out;
if (validate_win(win)) {
kill_refs(win);
goto out;
}
gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
if (gifr) {
DNPRINTF(SWM_D_FOCUS, "focus_win: cur focus: %#x\n",
gifr->focus);
cfw = find_window(gifr->focus);
if (cfw) {
if (cfw != win) {
if (cfw->ws != ws && cfw->ws->r != NULL &&
cfw->frame != XCB_WINDOW_NONE) {
draw_frame(cfw);
} else {
unfocus_win(cfw);
}
}
} else {
war = xcb_get_window_attributes_reply(conn,
xcb_get_window_attributes(conn, gifr->focus), NULL);
if (war && war->override_redirect && ws->focus == win) {
DNPRINTF(SWM_D_FOCUS, "focus_win: skip refocus "
"from override_redirect.\n");
goto out;