Skip to content

Instantly share code, notes, and snippets.

@Flashwalker
Last active June 29, 2024 15:58
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

Usage:

Cycle toggle all presets

$ switch-preset-mxmaster3s.sh

Toggle to specific preset

$ switch-preset-mxmaster3s.sh <PRESETNAME>

e.g.: switch-preset-mxmaster3s.sh copypaste

#!/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'
#######################################
## Store all arguments in a bash array:
array=("$@")
## Find out length of an array:
len="${#array[@]}"
## Get the last argument (the last one is preset name):
_last="${array[$len-1]}"
## Extract and store all arguments before last:
#args_="${array[@]:0:$len-1}"
## Extract and store all arguments except first & last:
#_args_="${array[@]:1:$len-2}"
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
echo Switching preset...
{
if (($pid)); then
# stop the current running preset
(($firstrun)) || input-remapper-control \
--config-dir "$configdir" \
--command stop \
--preset "$curpreset" \
--device "$device" 2>&1 >/dev/null
# start the next one in the list
input-remapper-control \
--config-dir "$configdir" \
--command start \
--preset "${_last:-$nextpreset}" \
--device "$device" 2>&1 >/dev/null
fi
} &
# write the actual current preset name to the file (curpreset)
[ -z "$_last" ] && echo "$nextpreset" | tee "$presetsocket"
notify-send "input-remapper" "${_last:-$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