Skip to content

Instantly share code, notes, and snippets.

@Flashwalker
Last active May 19, 2024 19:26
Show Gist options
  • Save Flashwalker/e5179d7bd79362154d6ae2ccc1f6e856 to your computer and use it in GitHub Desktop.
Save Flashwalker/e5179d7bd79362154d6ae2ccc1f6e856 to your computer and use it in GitHub Desktop.
Switch input-remapper presets
#!/bin/bash
# Cycle through input-remapper presets
# https://github.com/sezanzeb/input-remapper
## First create presets in input-remapper GUI. Do not use spaces in presets names.
## Edit your device name and default preset name here:
######### Customize this config #######
device='Logitech USB Receiver' # or e.g. 'MX Anywhere 3S Mouse' # See your device name in the GUI
configdir=~/'.config/input-remapper-2'
defaultpreset='copypaste'
#######################################
presetsocket="${configdir}/presets/${device}/curpreset"
presets=($(cd "${configdir}/presets/${device}/"; find -P -maxdepth 1 -type f -name "*.json" -printf '%f\n'))
curpreset=
nextpreset=
if [ ${#presets[@]} -gt 1 ]; then
# Define a current preset
if ! [ -f "$presetsocket" ]; then
firstrun=1
nextpreset="$defaultpreset"
curpreset="$nextpreset"
echo "$curpreset" > "$presetsocket"
else
curpreset="$(cat "$presetsocket" | head -1)"
fi
for i in "${!presets[@]}"; do
# match the current preset in list of the presets and set the nextpreset to the next one
case "${presets[$i]%.json}" in
"$curpreset")
j=$(($i+1))
# if we rich the end of the list of presets set the nextpreset to the first preset in the list
if [ $j -eq ${#presets[@]} ]; then
nextpreset="${presets[0]%.json}"
# or set the nextpreset to the next one in the list
else
nextpreset="${presets[$j]%.json}"
fi
;;
esac
done
# Start the input-remapper daemon if it's not running
pid="$(pgrep input-remapper | head -1)"
if ! (($pid)); then
pkexec input-remapper-control \
--command start-reader-service
pkexec input-remapper-control \
--command start-daemon
sleep 0.3s
pid="$(pgrep input-remapper | head -1)"
fi
# Switch the preset
{
if (($pid)); then
# stop the current running preset
(($firstrun)) || input-remapper-control \
--config-dir "$configdir" \
--command stop \
--preset "$curpreset" \
--device "$device" >/dev/null 2>&1
# start the next one in the list
input-remapper-control \
--config-dir "$configdir" \
--command start \
--preset "$nextpreset" \
--device "$device" >/dev/null 2>&1
fi
} &
# write the actual current preset name to the file (curpreset)
echo "$nextpreset" | tee "$presetsocket"
notify-send "input-remapper" "$nextpreset" -a "input-remapper" -i "input-mouse"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment