2020-05-26 09:46:53 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2020-05-26 12:55:06 +00:00
|
|
|
# output is to stdout unless explicitly set through -o or env var
|
|
|
|
OUTPUTF="$SXHKD_OUTPUTF"
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
# set fifo input file, according (somewhat) to xdg
|
2020-05-26 12:55:06 +00:00
|
|
|
if [ -n "$SXHKD_FIFO" ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
FIFO="$SXHKD_FIFO"
|
2020-05-26 12:55:06 +00:00
|
|
|
elif [ -p "${XDG_RUNTIME_DIR}"/sxhkd_fifo ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
FIFO="${XDG_RUNTIME_DIR}"/sxhkd_fifo
|
2020-05-26 12:55:06 +00:00
|
|
|
elif [ -p "${XDG_CACHE_HOME:-$HOME/.cache}"/sxhkd_fifo ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
FIFO="${XDG_CACHE_HOME:-$HOME/.cache}"/sxhkd_fifo
|
2020-05-26 12:55:06 +00:00
|
|
|
elif [ -p "$HOME/.sxhkd_fifo" ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
FIFO="$HOME/.sxhkd_fifo"
|
2020-05-26 12:55:06 +00:00
|
|
|
fi
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
# set label config file, according (somewhat) to xdg
|
2020-05-26 12:55:06 +00:00
|
|
|
if [ -n "$SXHKD_LABELCONFIG" ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
LABELCONFIG="$SXHKD_LABELCONFIG"
|
2020-05-26 12:55:06 +00:00
|
|
|
elif [ -f "${XDG_CONFIG_HOME:-$HOME/.config}"/sxhkd/chain-labels.conf ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
LABELCONFIG="${XDG_CONFIG_HOME:-$HOME/.config}"/sxhkd/chain-labels.conf
|
2020-05-26 12:55:06 +00:00
|
|
|
elif [ -f "$HOME/.chain-labels.conf" ]; then
|
2021-07-10 15:57:08 +00:00
|
|
|
LABELCONFIG="$HOME/.chain-labels.conf"
|
2020-05-26 12:55:06 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
SXHKDRC_FILE="$XDG_CONFIG_HOME"/sxhkd/sxhkdrc
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
main() {
|
2021-07-10 15:57:08 +00:00
|
|
|
while read -r event; do
|
|
|
|
detect_event "$event"
|
|
|
|
done <"$FIFO"
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
detect_event() {
|
2021-07-10 15:57:08 +00:00
|
|
|
ev="$(echo "$1" | sed -e 's/^H.*$/hotkey/;s/^C.*$/command/;s/^BBegin chain.*$/chainstart/;s/^EEnd chain.*$/chainend/;')"
|
|
|
|
|
|
|
|
case $ev in
|
|
|
|
hotkey) ev_hotkey "$(echo "$1" | sed -e 's/^H//')" ;;
|
|
|
|
# command) ev_command "$(echo "$1" | sed -e 's/^C//')" ;;
|
|
|
|
chainstart) ev_chainstart "$(echo "$1" | sed -e 's/^B//')" ;;
|
|
|
|
chainend) ev_chainend "$(echo "$1" | sed -e 's/^E//')" ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
send_msg() {
|
2021-07-10 15:57:08 +00:00
|
|
|
if [ -n "$OUTPUTF" ]; then
|
|
|
|
echo "$1" >"$OUTPUTF"
|
|
|
|
else
|
|
|
|
echo "$1"
|
|
|
|
fi
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ev_hotkey() {
|
2021-07-10 15:57:08 +00:00
|
|
|
LAST_HOTKEY="$1"
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# compare labels to last hotkey, return mode name
|
|
|
|
ev_chainstart() {
|
2021-07-10 15:57:08 +00:00
|
|
|
[ -z "$LAST_HOTKEY" ] && return 1
|
|
|
|
found=$(echo "$LABELS" | sed -e "/$LAST_HOTKEY/!d;s/^\(.\+\):.*$/\1/")
|
|
|
|
send_msg "$found"
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ev_chainend() {
|
2021-07-10 15:57:08 +00:00
|
|
|
send_msg ""
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# TODO add option to also display last command done in chain
|
|
|
|
# ev_command() {
|
|
|
|
# send_msg "command: $1"
|
|
|
|
# }
|
|
|
|
|
|
|
|
# read config from file, remove comments (lines starting with #) and empty lines
|
|
|
|
read_config() {
|
2021-07-10 15:57:08 +00:00
|
|
|
[ ! -f "$1" ] && return 1
|
2020-05-26 12:55:06 +00:00
|
|
|
|
2021-07-10 15:57:08 +00:00
|
|
|
parse_labels "$(cat "$1")"
|
2020-05-26 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# parse sxhkdrc for mode compatible comments
|
|
|
|
read_sxhkdrc() {
|
2021-07-10 15:57:08 +00:00
|
|
|
[ ! -f "$1" ] && return 1
|
2020-05-26 12:55:06 +00:00
|
|
|
|
2021-07-10 15:57:08 +00:00
|
|
|
_sxhkdrc_content="$(sed -e '/^# mode:/!d;s/^# mode://' <"$1")"
|
|
|
|
parse_labels "$_sxhkdrc_content"
|
2020-05-26 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# append
|
|
|
|
parse_labels() {
|
2021-07-10 15:57:08 +00:00
|
|
|
LABELS="${LABELS}$(echo "$1" | sed -e '/^#/d;/^[[:blank:]]*$/d')"
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_help() {
|
2021-07-10 15:57:08 +00:00
|
|
|
printf \
|
|
|
|
"Usage: sxhkd-chain-labels [-c config file][-o output file][-s input pipe]
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
By default will take the input from the input pipe (at XDG_RUNTIME_DIR/sxhkd_fifo) and
|
|
|
|
print the current sxhkd chain mode to stdout. That means, sxhkd needs to be started
|
|
|
|
with a fifo pipe running, ideally to the XDG_RUNTIME_DIR:
|
|
|
|
|
|
|
|
mkfifo \$XDG_RUNTIME_DIR/sxhkd_fifo && sxhkd -s \$XDG_RUNTIME_DIR/sxhkd_fifo
|
|
|
|
|
|
|
|
When given a key combination which maps to a specific mode, it will print out the name
|
|
|
|
of the mode instead. These maps can be passed with -c flag, specifying an options file.
|
|
|
|
By default it will look in \$XDG_CONFIG_HOME/sxhkd/sxhkd-chain-labels.conf
|
|
|
|
|
|
|
|
When passed the -o flag it will replace the contents of the file passed in with the
|
|
|
|
current sxhkd chain mode, emptying the file when no mode is active.
|
|
|
|
|
|
|
|
Default lookup places for files, in descending order:
|
|
|
|
:input fifo
|
|
|
|
\$SXHKD_FIFO (env variable)
|
|
|
|
\$XDG_RUNTIME_DIR/sxhkd_fifo
|
|
|
|
\$XDG_CACHE_HOME/sxhkd_fifo
|
|
|
|
~/.cache/sxhkd_fifo
|
|
|
|
~/.sxhkd_fifo
|
|
|
|
|
|
|
|
:label configuration
|
|
|
|
\$SXHKD_LABELCONFIG (env variable)
|
|
|
|
\$XDG_CONFIG_HOME/sxhkd/chain-labels.conf
|
|
|
|
~/.config/sxhkd/chain-labels.conf
|
|
|
|
~/.chain-labels.conf
|
2020-05-26 12:55:06 +00:00
|
|
|
\$XDG_CONFIG_HOME/sxhkd/sxhkdrc (parsing)
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
The label configuration file uses the following format:
|
2020-05-26 12:55:06 +00:00
|
|
|
mode name:key chain
|
2020-05-26 09:46:53 +00:00
|
|
|
|
2020-05-26 12:55:06 +00:00
|
|
|
Lines beginning with a # will be ignored. Whitespace is important, sxhkd will, by
|
|
|
|
default, put a single space between any component of the key combination.
|
2020-05-26 09:46:53 +00:00
|
|
|
|
2020-05-26 12:55:06 +00:00
|
|
|
An example file chain-labels.conf:
|
2020-05-26 09:46:53 +00:00
|
|
|
media:super + alt + m
|
|
|
|
system:super + backspace
|
|
|
|
|
2020-05-26 12:55:06 +00:00
|
|
|
Instead of using an explicit configuration file, you can put the chain mode
|
|
|
|
information into the regular sxhkdrc as comments. They need to follow this exact format:
|
2020-09-22 20:34:50 +00:00
|
|
|
# mode:mode-name:key-chain
|
2020-05-26 12:55:06 +00:00
|
|
|
|
2021-07-10 15:57:08 +00:00
|
|
|
They can occur anywhere in the file. The space between # and mode is necessary, and #
|
|
|
|
needs to be the first character on the line.
|
|
|
|
The above example file as written into the sxhkdrc:
|
2020-05-26 12:55:06 +00:00
|
|
|
# mode:media:super + alt + m
|
|
|
|
# mode:system:super + backspace
|
|
|
|
|
|
|
|
If an explicit configuration file exists, it will supersede any mode information in the
|
|
|
|
sxhkdrc file.
|
2020-05-26 09:46:53 +00:00
|
|
|
\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_version() {
|
2021-07-10 15:57:08 +00:00
|
|
|
printf \
|
|
|
|
"%s: 0.3
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
fifo input pipe:
|
2021-07-10 15:57:08 +00:00
|
|
|
%s %s
|
2020-05-26 09:46:53 +00:00
|
|
|
label configuration file:
|
2021-07-10 15:57:08 +00:00
|
|
|
%s %s
|
2020-05-26 09:46:53 +00:00
|
|
|
output:
|
|
|
|
%s \n" \
|
2021-07-10 15:57:08 +00:00
|
|
|
"$0" \
|
|
|
|
"$FIFO" \
|
|
|
|
"$([ -p "$FIFO" ] && echo "(found)" || echo "(NOT FOUND)")" \
|
|
|
|
"${LABELCONFIG:-"$SXHKDRC_FILE"}" \
|
|
|
|
"$([ -f "${LABELCONFIG:-"$SXHKDRC_FILE"}" ] && echo "(found)" || echo "(NOT FOUND)")" \
|
|
|
|
"${OUTPUTF:-stdout}"
|
2020-05-26 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while getopts "vho:s:c:" opt; do
|
2021-07-10 15:57:08 +00:00
|
|
|
case "$opt" in
|
|
|
|
\?)
|
|
|
|
printf "Usage: sxhkd-chain-labels [-c config file][-o output file][-s input pipe]\n"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
get_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
get_version
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
o) OUTPUTF="$OPTARG" ;;
|
|
|
|
s) FIFO="$OPTARG" ;;
|
|
|
|
c) LABELCONFIG="$OPTARG" ;;
|
|
|
|
esac
|
2020-05-26 09:46:53 +00:00
|
|
|
done
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
|
|
|
[ "${1:-}" = "--" ] && shift
|
|
|
|
|
2020-05-26 12:55:06 +00:00
|
|
|
# look for default label config, prefer config file to parsing sxhkdrc
|
2020-05-26 09:46:53 +00:00
|
|
|
[ -z "$LABELS" ] && read_config "$LABELCONFIG"
|
2020-05-26 12:55:06 +00:00
|
|
|
[ -z "$LABELS" ] && read_sxhkdrc "$SXHKDRC_FILE"
|
2020-05-26 09:46:53 +00:00
|
|
|
|
|
|
|
main "$@"
|