#!/bin/sh

# This configuration file describes a minimal example for a Catacomb config.
#
# The following applications should be installed or replaced:
# - tremor: Used for vibration effects (https://git.sr.ht/~undeadleech/tremor)
# - epitaph: Panel (https://git.sr.ht/~undeadleech/epitaph)
# - tzompantli: Application launcher (https://git.sr.ht/~undeadleech/tzompantli)
# - squeekboard: Virtual Keyboard (https://gitlab.gnome.org/World/Phosh/squeekboard)
# - swayidle: Automatic idle timeouts (https://github.com/swaywm/sway)
# - grim: Screenshots (https://gitlab.freedesktop.org/emersion/grim)
# - slurp: Screenshot region selection (https://github.com/emersion/slurp)

# Import display environment variable into systemd
systemctl --user import-environment WAYLAND_DISPLAY

# Key Bindings

## Ignore power-button press, to allow binding to it
if command -v systemd-inhibit; then
    systemd-inhibit --what handle-power-key sh -c "while true; do sleep 86400; done" &
else
    elogind-inhibit --what handle-power-key sh -c "while true; do sleep 86400; done" &
fi

## Launch `tzompantli` when holding the power button
catacomb msg bind-key --trigger press '*' XF86PowerOff bash -c \
    "sleep 0 0 0 0 0 0 0 0.5 &&
        if [ \"\$(catacomb msg dpms)\" == \"on\" ]; then
            (tremor 150 0 1; tzompantli)
        fi"

## Toggle display when pressing the power button
catacomb msg bind-key --trigger release '*' XF86PowerOff bash -c \
    "if pkill -SIGINT -xf \"sleep 0 0 0 0 0 0 0 0.5\"; then
        if [ \"\$(catacomb msg dpms)\" == \"on\" ]; then
            catacomb msg dpms off;
        else
            catacomb msg dpms on;
        fi
    fi"

## Take a screenshot on `power + volume down` and save it to /tmp/screenshot.png
catacomb msg bind-key --trigger press '*' XF86PowerOff+XF86AudioLowerVolume bash -c \
    "pkill -SIGINT -xf \"sleep 0 0 0 0 0 0 0 0.5\";
    tremor 150 0 1;
    geometry=\$(slurp 2>&1);
    if [[ \$geometry == \"selection cancelled\" ]]; then
        grim /tmp/screenshot.png;
    else
        grim -g \"\$geometry\" /tmp/screenshot.png;
    fi"

## Allow manually toggling the virtual keyboard
catacomb msg bind-key '*' EnableVirtualKeyboard \
    busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b true
catacomb msg bind-key '*' AutoVirtualKeyboard \
    busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b false

## Bind global volume keys
catacomb msg bind-key --trigger repeat '*' XF86AudioRaiseVolume \
    pactl set-sink-volume 0 +5%
catacomb msg bind-key --trigger repeat '*' XF86AudioLowerVolume \
    pactl set-sink-volume 0 -5%

# Turn off display after 3 minutes, suspend 30s later
systemctl="systemctl"
if ! command -v systemctl; then
    systemctl="loginctl"
fi
swayidle -w \
    timeout 180 "catacomb msg dpms off" \
    timeout 210 "$systemctl suspend" \
    before-sleep "catacomb msg dpms off" \
    after-resume \
        "# Resuspend after 30 seconds on wakeup with no activity.
        swayidle -w timeout 30 \"$systemctl suspend\" &
        sleep 30.1;
        kill \$!" &

# Ensure the virtual keyboard is enabled
gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true

# Spawn background apps
squeekboard &
epitaph &

# Wait for completion
wait
