diff options
author | Anthony Wang | 2024-11-22 22:33:23 -0500 |
---|---|---|
committer | Anthony Wang | 2024-11-22 22:33:23 -0500 |
commit | b5a71f5df399bdc6f24b11cfc0d50598c7b25ff8 (patch) | |
tree | 520e3df94c358e3f4035a9b51590c2e4e770415f /content | |
parent | 61ae6d38d1545dacacb73fa4e6c6fb0323e8fca6 (diff) |
Add Fish and systemd versions of switchvirtualkeyboard scripts
Diffstat (limited to 'content')
-rw-r--r-- | content/posts/switch-virtual-keyboard.md | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/content/posts/switch-virtual-keyboard.md b/content/posts/switch-virtual-keyboard.md index a31de97..1c97305 100644 --- a/content/posts/switch-virtual-keyboard.md +++ b/content/posts/switch-virtual-keyboard.md @@ -25,7 +25,7 @@ signal time=1727303907.040280 sender=:1.403 -> destination=(null destination) se Basically, that particular D-Bus signal tells KWin to peak at the new value of `InputMethod`. That translates into the command `busctl --user emit /kwinrc org.kde.kconfig.notify ConfigChanged "a{saay}" 1 Wayland 1 11 73 110 112 117 116 77 101 116 104 111 100`. The weird numbers are just `InputMethod` in ASCII as an array of bytes. Did you know D-Bus has its own [type and serialization system](https://dbus.freedesktop.org/doc/dbus-specification.html#type-system)? Well, I had to learn it for that command. It's also possible to detect entering and exiting tablet mode using D-Bus. -Alright, so now time for the actual script, in `~/.config/autostart/switchvirtualkeyboard.sh`: +Alright, so now time for a Bash script, in `~/.config/autostart/switchvirtualkeyboard.sh`: ```bash #!/bin/bash @@ -45,7 +45,27 @@ while read -r line; do done ``` -And a file `~/.config/autostart/switchvirtualkeyboard.desktop` to make Plasma start it at login: +Or alternatively, a Fish version of the same script: +```fish +function setvirtualkeyboard + echo "Starting $argv[1]" + kwriteconfig6 --file kwinrc --group Wayland --key InputMethod "$argv[1]" + busctl --user emit /kwinrc org.kde.kconfig.notify ConfigChanged "a{saay}" 1 Wayland 1 11 73 110 112 117 116 77 101 116 104 111 100 +end + +function switchvirtualkeyboard + busctl --user monitor --match "type='signal',interface='org.kde.KWin.TabletModeManager',member='tabletModeChanged'" | \ + while read -l line + if string match -q "*true*" $line + setvirtualkeyboard /usr/share/applications/com.github.maliit.keyboard.desktop + else if string match -q "*false*" $line + setvirtualkeyboard /usr/share/applications/fcitx5-wayland-launcher.desktop + end + end +end +``` + +To make Plasma start the Bash script at login, add a file `~/.config/autostart/switchvirtualkeyboard.desktop`. Internally, systemd-xdg-autostart-generator translates the `.desktop` file into a systemd service. ```ini [Desktop Entry] Exec=bash ~/.config/autostart/switchvirtualkeyboard.sh @@ -53,6 +73,18 @@ Name=switchvirtualkeyboard Type=Application ``` -Internally, systemd-xdg-autostart-generator translates the `.desktop` file into a systemd service. As you might expect from all these obscure Linux components, there's not much documentation about this stuff and the script was really tricky to debug. It works now, but it took me way more time to write this than to the amount of time it would hypothetically take me to manually switch the virtual keyboard in the settings app every time in the future. But hey, I got to learn some cool things about D-Bus and systemd! +For the Fish version, you can directly write a systemd service and start and enable it: +```ini +[Unit] +Description=Switch the virtual keyboard when entering and exiting tablet mode + +[Service] +ExecStart=fish -c switchvirtualkeyboard + +[Install] +WantedBy=default.target +``` + +As you might expect from all these obscure Linux components, there's not much documentation about this stuff and the script was really tricky to debug. It works now, but it took me way more time to write this than to the amount of time it would hypothetically take me to manually switch the virtual keyboard in the settings app every time in the future. But hey, I got to learn some cool things about D-Bus and systemd! Lastly, this is just one way that I found to accomplish this task, but I bet there are better ways. For instance, I considered writing a KWin script instead, but the API seemed a bit limited, since for instance, `callDBus` can only call D-Bus methods and can't send D-Bus signals. If you have any ideas for improvement, I'd love to hear it! |