//! system/services/input-test — the input service's client and test oracle, the input
//! counterpart of vfs-test. It subscribes to *all* device classes and loops receiving the
//! events a source broadcasts, logging each with its class. It emits the success marker
//! `"input-test: ok"` only **after it has received at least one of each class** (keyboard,
//! mouse, and joystick), then heartbeats it. So the in-kernel `input` test case seeing that
//! marker proves not just that IPC delivery works but that the service *routed* all three
//! device classes to one subscription — source -> service -> subscriber, per device.

const std = @import("std");
const runtime = @import("runtime");
const input = runtime.input;
const system = runtime.system;

fn writeLine(comptime fmt: []const u8, arguments: anytype) void {
    var line: [128]u8 = undefined;
    _ = system.write(std.fmt.bufPrint(&line, fmt, arguments) catch return);
}

pub fn main() void {
    var listener = input.subscribeAll() orelse {
        _ = system.write("input-test: could not subscribe\n");
        return;
    };
    _ = system.write("input-test: subscribed\n");

    var seen_keyboard = false;
    var seen_mouse = false;
    var seen_joystick = false;
    while (true) {
        const event = listener.next() orelse continue;
        // Decode the class-specific payload from the tagged envelope and note the class.
        if (event.asKeyboard()) |key| {
            seen_keyboard = true;
            writeLine("input-test: got keyboard code={d} char={d}\n", .{ key.keycode, key.character });
        } else if (event.asMouse()) |mouse| {
            seen_mouse = true;
            writeLine("input-test: got mouse dx={d} dy={d} buttons={d}\n", .{ mouse.dx, mouse.dy, mouse.buttons });
        } else if (event.asJoystick()) |joystick| {
            seen_joystick = true;
            writeLine("input-test: got joystick control={d} value={d}\n", .{ joystick.control, joystick.value });
        } else {
            writeLine("input-test: got device={d}\n", .{event.device});
        }

        // The success marker: only once every class has been routed here does this appear,
        // and then it heartbeats. Seeing "input-test: ok" proves per-device fan-out works.
        if (seen_keyboard and seen_mouse and seen_joystick) {
            _ = system.write("input-test: ok all classes received (keyboard, mouse, joystick)\n");
        }
    }
}
