diff --git a/dot_config/gamemode/executable_gamemode-end.sh b/dot_config/gamemode/executable_gamemode-end.sh index 64a13ee..a7e3795 100644 --- a/dot_config/gamemode/executable_gamemode-end.sh +++ b/dot_config/gamemode/executable_gamemode-end.sh @@ -1,8 +1,37 @@ #!/bin/sh -# Restore llama-swap only if GameMode stopped a previously running service. -marker="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/llama-swap-gamemode.was-active" -if [ -f "$marker" ]; then - rm -f "$marker" - /usr/bin/systemctl --user start llama-swap.service +PID_FILE="${XDG_RUNTIME_DIR:-/tmp}/gamemode-inhibit.pid" +LLAMA_STATE="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/gamemode-llama-services" +LEGACY_LLAMA_MARKER="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/llama-swap-gamemode.was-active" + +if [ -f "$PID_FILE" ]; then + pid=$(cat "$PID_FILE" 2>/dev/null) + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + kill "$pid" >/dev/null 2>&1 || true + fi + rm -f "$PID_FILE" +fi + +/home/misti/.cargo/bin/wp mode random >/dev/null 2>&1 || true +swaync-client -df -sw >/dev/null 2>&1 || true + +# Restore only the router that GameMode stopped. Accept the old marker once so +# an upgrade performed during an active game still restores llama-swap on exit. +if [ -f "$LEGACY_LLAMA_MARKER" ]; then + rm -f "$LEGACY_LLAMA_MARKER" + if [ ! -f "$LLAMA_STATE" ]; then + printf '%s\n' llama-swap.service > "$LLAMA_STATE" + fi +fi + +if [ -f "$LLAMA_STATE" ]; then + services=$(cat "$LLAMA_STATE") + rm -f "$LLAMA_STATE" + for service in $services; do + case "$service" in + llama-swap.service|llama-server.service) + /usr/bin/systemctl --user start "$service" >/dev/null 2>&1 || true + ;; + esac + done fi diff --git a/dot_config/gamemode/executable_gamemode-start.sh b/dot_config/gamemode/executable_gamemode-start.sh index 5057b24..6d9c45b 100644 --- a/dot_config/gamemode/executable_gamemode-start.sh +++ b/dot_config/gamemode/executable_gamemode-start.sh @@ -1,10 +1,45 @@ #!/bin/sh -# Stop the router itself so background clients cannot reload models while gaming. -marker="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/llama-swap-gamemode.was-active" - -if /usr/bin/systemctl --user is-active --quiet llama-swap.service; then - : > "$marker" - /usr/bin/systemctl --user stop llama-swap.service -else - rm -f "$marker" + +PID_FILE="${XDG_RUNTIME_DIR:-/tmp}/gamemode-inhibit.pid" +LLAMA_STATE="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/gamemode-llama-services" + +if [ -f "$PID_FILE" ]; then + pid=$(cat "$PID_FILE" 2>/dev/null) + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + : + else + rm -f "$PID_FILE" + fi +fi + +if [ ! -f "$PID_FILE" ] && command -v systemd-inhibit >/dev/null 2>&1; then + systemd-inhibit --what=idle:sleep --who="gamemode" --why="GameMode active" /usr/bin/sleep infinity >/dev/null 2>&1 & + pid=$! + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + printf '%s\n' "$pid" > "$PID_FILE" + fi fi + +/home/misti/.cargo/bin/wp mode static >/dev/null 2>&1 || true +swaync-client -dn -sw >/dev/null 2>&1 || true + +# Remember the router that was running before GameMode, then stop every local +# llama router. Stopping (rather than freezing) unloads child models and frees VRAM. +if [ ! -f "$LLAMA_STATE" ]; then + state_tmp="${LLAMA_STATE}.tmp" + rm -f "$state_tmp" + for service in llama-swap.service llama-server.service; do + if /usr/bin/systemctl --user is-active --quiet "$service"; then + printf '%s\n' "$service" >> "$state_tmp" + fi + done + if [ -s "$state_tmp" ]; then + mv "$state_tmp" "$LLAMA_STATE" + else + rm -f "$state_tmp" + fi +fi + +# The preset watcher can restart llama-server when models.ini changes. +/usr/bin/systemctl --user stop llama-server-models.path >/dev/null 2>&1 || true +/usr/bin/systemctl --user stop llama-server.service llama-swap.service >/dev/null 2>&1 || true diff --git a/dot_config/systemd/user/symlink_llama-server-models.path b/dot_config/systemd/user/symlink_llama-server-models.path new file mode 100644 index 0000000..b555872 --- /dev/null +++ b/dot_config/systemd/user/symlink_llama-server-models.path @@ -0,0 +1 @@ +/dev/null diff --git a/dot_config/systemd/user/symlink_llama-server.service b/dot_config/systemd/user/symlink_llama-server.service new file mode 100644 index 0000000..b555872 --- /dev/null +++ b/dot_config/systemd/user/symlink_llama-server.service @@ -0,0 +1 @@ +/dev/null