Skip to content

Commit b3e025e

Browse files
Sync runtime control through GNOME layout DBus
1 parent fd1dfe5 commit b3e025e

1 file changed

Lines changed: 57 additions & 3 deletions

File tree

scripts/lay-runtime-control.sh

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
set -euo pipefail
33

44
CONFIG_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/lay/config.json"
5+
LAY_DBUS_DEST="org.gnome.Shell"
6+
LAY_DBUS_PATH="/io/github/radislabus_star/LayDaemon"
7+
LAY_DBUS_IFACE="io.github.radislabus_star.LayDaemon"
58

69
text_backend() {
710
python3 - "$CONFIG_PATH" <<'PY'
@@ -21,12 +24,61 @@ select_xkb() {
2124
|| true
2225
}
2326

27+
activate_gnome_layout() {
28+
local layout="${1:?layout required}"
29+
timeout 2s gdbus call \
30+
--session \
31+
--dest "$LAY_DBUS_DEST" \
32+
--object-path "$LAY_DBUS_PATH" \
33+
--method "$LAY_DBUS_IFACE.ActivateLayout" \
34+
"$layout" >/dev/null
35+
}
36+
37+
current_gnome_layout() {
38+
timeout 2s gdbus call \
39+
--session \
40+
--dest "$LAY_DBUS_DEST" \
41+
--object-path "$LAY_DBUS_PATH" \
42+
--method "$LAY_DBUS_IFACE.CurrentLayout" \
43+
2>/dev/null \
44+
| sed -n "s/^('\\(.*\\)',)$/\\1/p"
45+
}
46+
47+
sync_ibus_engine() {
48+
local layout="${1:?layout required}"
49+
local attempt
50+
for attempt in 1 2 3 4 5; do
51+
if timeout 2s ibus engine "$layout" >/dev/null 2>&1; then
52+
local engine
53+
engine="$(timeout 1s ibus engine 2>/dev/null || true)"
54+
if [ "$engine" = "$layout" ]; then
55+
return 0
56+
fi
57+
fi
58+
sleep 0.15
59+
done
60+
return 1
61+
}
62+
63+
select_lay_ime() {
64+
local layout="${1:?layout required}"
65+
if activate_gnome_layout "$layout"; then
66+
sync_ibus_engine "$layout" || true
67+
local current engine
68+
current="$(current_gnome_layout || true)"
69+
engine="$(timeout 1s ibus engine 2>/dev/null || true)"
70+
if [ "$current" = "$layout" ] && [ "$engine" = "$layout" ]; then
71+
return 0
72+
fi
73+
fi
74+
sync_ibus_engine "$layout"
75+
}
76+
2477
start_ime() {
2578
systemctl --user stop lay-ibus-engine.service >/dev/null 2>&1 || true
2679
pkill -x lay-ibus-engine || true
27-
timeout 2s ibus engine lay-ime-ru \
28-
|| timeout 2s ibus engine lay-ime-ru \
29-
|| timeout 2s ibus engine lay-ime-us \
80+
select_lay_ime lay-ime-ru \
81+
|| select_lay_ime lay-ime-us \
3082
|| true
3183
}
3284

@@ -75,6 +127,8 @@ case "${1:-status}" in
75127
systemctl --user is-active lay-daemon.service || true
76128
printf 'ime_processes='
77129
pgrep -c -x lay-ibus-engine || true
130+
printf 'gnome_layout='
131+
current_gnome_layout || true
78132
printf 'ibus_engine='
79133
timeout 1s ibus engine || true
80134
;;

0 commit comments

Comments
 (0)