#!/bin/bash
# Pocket DS — toggle the Onboard on-screen keyboard.
#
# Onboard runs under XWayland (reliable XInput touch) with the uinput
# injection backend (global). Under XWayland it only renders at full
# DSI-2 size on a FRESH map, so we SHOW by starting a fresh instance and
# HIDE by killing it.
#
# Show goes through Onboard's D-Bus service, whose Exec (patched in our
# onboard package) sets the same env as /usr/bin/pocketds-onboard — so a not-
# running Onboard is activated with the correct XWayland+uinput env, and
# a running one just gets Show. No launch/registration race.
#
# When the bottom-screen companion runs, Onboard lives in its dock, so the
# trigger toggles the dock instead.
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"

if dbus-send --session --print-reply --dest=io.github.mmogr.FlipCompanion \
        /io/github/mmogr/FlipCompanion io.github.mmogr.FlipCompanion.Shell.ToggleKeyboard \
        >/dev/null 2>&1; then
    exit 0
fi

if pgrep -x onboard >/dev/null 2>&1; then
    pkill -x onboard
else
    dbus-send --type=method_call --dest=org.onboard.Onboard \
        /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Show 2>/dev/null
fi
