| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | |
| 5 | if ! command -v ibus-daemon >/dev/null || ! /usr/bin/python3 -c 'import gi; gi.require_version("IBus", "1.0")' 2>/dev/null; then |
| 6 | exec "$@" |
| 7 | fi |
| 8 | umask 077 |
| 9 | input_root="$HOME/.cache/cage-input" |
| 10 | if [ "${CAGE_PHONE_DESKTOP_INPUT:-}" = 1 ]; then input_root=/run/cage-phone-desktop/input; fi |
| 11 | mkdir -p "$input_root" |
| 12 | chmod 700 "$input_root" |
| 13 | export IBUS_ADDRESS="unix:path=$input_root/bus" |
| 14 | export GTK_IM_MODULE=ibus QT_IM_MODULE=ibus XMODIFIERS=@im=ibus |
| 15 | bus_pid='' bridge_pid='' app_pid='' session_pid='' |
| 16 | cleanup() { |
| 17 | if [ -n "$app_pid" ]; then |
| 18 | kill -TERM "$app_pid" 2>/dev/null || true |
| 19 | |
| 20 | wait "$app_pid" 2>/dev/null || true |
| 21 | fi |
| 22 | for pid in "$bridge_pid" "$bus_pid" "$session_pid"; do |
| 23 | if [ -n "$pid" ]; then kill -TERM "$pid" 2>/dev/null || true; fi |
| 24 | done |
| 25 | rm -f "$input_root/ready" "$input_root/input.sock" |
| 26 | } |
| 27 | trap cleanup EXIT |
| 28 | trap 'exit 143' TERM |
| 29 | trap 'exit 130' INT |
| 30 | |
| 31 | |
| 32 | |
| 33 | if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]; then |
| 34 | mkdir -p "$input_root/services" |
| 35 | cp /usr/share/dbus-1/services/org.freedesktop.portal.IBus.service "$input_root/services/" |
| 36 | cat > "$input_root/session.conf" <<CONFIG |
| 37 | <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> |
| 38 | <busconfig> |
| 39 | <type>session</type><listen>unix:path=$input_root/session-bus</listen><auth>EXTERNAL</auth> |
| 40 | <servicedir>$input_root/services</servicedir> |
| 41 | <policy context="default"><allow send_destination="*"/><allow receive_sender="*"/><allow own="*"/></policy> |
| 42 | </busconfig> |
| 43 | CONFIG |
| 44 | rm -f "$input_root/session-bus" |
| 45 | dbus-daemon --nofork --config-file="$input_root/session.conf" >/dev/null 2>&1 & |
| 46 | session_pid=$! |
| 47 | for i in $(seq 1 100); do |
| 48 | kill -0 "$session_pid" |
| 49 | if [ -S "$input_root/session-bus" ]; then break; fi |
| 50 | sleep .05 |
| 51 | done |
| 52 | test -S "$input_root/session-bus" |
| 53 | export DBUS_SESSION_BUS_ADDRESS="unix:path=$input_root/session-bus" |
| 54 | fi |
| 55 | rm -f "$input_root/ready" "$input_root/bus" "$input_root/input.sock" |
| 56 | ibus-daemon --single --panel=disable --config=disable --emoji-extension=disable --cache=none --address="$IBUS_ADDRESS" & |
| 57 | bus_pid=$! |
| 58 | for i in $(seq 1 100); do |
| 59 | kill -0 "$bus_pid" |
| 60 | if [ -S "$input_root/bus" ]; then break; fi |
| 61 | sleep .05 |
| 62 | done |
| 63 | /usr/local/bin/cage-native-input & |
| 64 | bridge_pid=$! |
| 65 | for i in $(seq 1 100); do |
| 66 | kill -0 "$bridge_pid" |
| 67 | if [ -f "$input_root/ready" ]; then break; fi |
| 68 | sleep .05 |
| 69 | done |
| 70 | test -f "$input_root/ready" |
| 71 | "$@" & |
| 72 | app_pid=$! |
| 73 | wait "$app_pid" |