Last active
July 18, 2024 12:33
-
-
Save un-def/04714914ffbd8aa10b129c0b714ffd5b to your computer and use it in GitHub Desktop.
Multi–layout Switcher based on kbdd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### Constants | |
# the index of the first layout in the primary layer | |
PRIMARY_LAYER=0 | |
# the index of the first and only layout in the secondary layer | |
SECONDARY_LAYER=2 | |
### Functions | |
_call_kbdd() { | |
_method=${1}; shift | |
dbus-send \ | |
--print-reply=literal \ | |
--dest=ru.gentoo.KbddService \ | |
/ru/gentoo/KbddService \ | |
"ru.gentoo.kbdd.${_method}" "${@}" | |
} | |
get_layout() { | |
_call_kbdd getCurrentLayout | sed -r 's/ *uint32 +([[:digit:]])/\1/' | |
} | |
set_layout() { | |
_id=${1}; shift | |
_call_kbdd set_layout "uint32:${_id}" | |
} | |
prev_layout() { | |
_call_kbdd prev_layout | |
} | |
get_layout_name() { | |
_id=${1}; shift | |
_call_kbdd getLayoutName "uint32:${_id}" | sed -r 's/ *(.+)/\1/' | |
} | |
notify() { | |
_id=${1}; shift | |
_layout=$(get_layout_name "${_id}") | |
_icon="indicator-keyboard-$(echo "${_layout}" | cut -c 1-2 | sed s/Ge/Ka/)" | |
notify-send -t 300 -i "${_icon}" "${_layout}" | |
} | |
### Main | |
set_layout ${PRIMARY_LAYER} | |
toggle_layouts() { | |
_layout=$(get_layout) | |
if [ "${_layout}" -eq ${SECONDARY_LAYER} ]; then | |
prev_layout | |
_layout=$(get_layout) | |
else | |
_layout=$((_layout + 1)) | |
if [ "${_layout}" -ge ${SECONDARY_LAYER} ]; then | |
_layout=${PRIMARY_LAYER} | |
fi | |
set_layout "${_layout}" | |
fi | |
notify "${_layout}" | |
} | |
toggle_layers() { | |
_layout=$(get_layout) | |
if [ "${_layout}" -eq ${SECONDARY_LAYER} ]; then | |
prev_layout | |
_layout=$(get_layout) | |
else | |
set_layout ${SECONDARY_LAYER} | |
_layout=${SECONDARY_LAYER} | |
fi | |
notify "${_layout}" | |
} | |
trap toggle_layouts USR1 | |
trap toggle_layers USR2 | |
while true; do | |
sleep infinity & | |
wait | |
kill ${!} | |
done |
Author
un-def
commented
Jul 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment