pragma Singleton

// Playing a short sound out of one chosen output.
//
// Deliberately not part of AudioDevices, which owns device state through the
// Quickshell PipeWire bindings and must not shell out -- doing so there would
// race the service that owns those same objects. This spawns a short-lived
// playback client instead: it creates its own stream and mutates no device, so
// there is nothing for it to race.
//
// It exists because nine outputs named after their chipsets cannot be told
// apart by reading. The only way to know which is which is to hear one.

import Quickshell
import Quickshell.Io
import QtQuick

Singleton {
    id: root

    // A short, unmistakable, front-and-centre sample that ships with the
    // freedesktop sound theme, so nothing has to be bundled.
    readonly property string sample:
        "/usr/share/sounds/freedesktop/stereo/audio-channel-front-center.oga"

    readonly property bool playing: player.running

    // Targeted by node name taken straight from the live node. pw-play falls
    // back to the default output for a target it cannot find, so a stale name
    // would play out of the wrong device and look like the test had worked.
    function play(node: var): void {
        const target = String(node?.name ?? "");
        if (target === "" || player.running)
            return;
        player.command = ["pw-play", "--target", target, root.sample];
        player.running = true;
    }

    Process { id: player }
}
