//! system/services/input-source — a hardware-free synthetic input source, used to exercise
//! the input service end to end without a real PS/2 controller (the `input` test case, and
//! any bring-up where there is no hardware). It stands in for a driver: it connects to the
//! input service and publishes a rolling stream that cycles through all device classes —
//! keyboard, mouse, and joystick/gamepad — which the service routes to interested
//! subscribers.
//!
//! It stays silent after startup (no per-event logging) so it can share the boot serial
//! transcript with a subscriber whose output is the test's success marker. The real
//! keyboard and mouse drivers publish their own synthetic streams today; swapping in
//! decoded hardware is a follow-up (see docs/input.md).

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

pub fn main(init: runtime.process.Init) void {
    var source = input.connectSource() orelse {
        _ = system.write("input-source: input service unavailable\n");
        return;
    };

    // "mouse" mode publishes a steady stream of pure motion (dx=dy=+1), for driving a
    // cursor (the `display-cursor` test). The default "rotate" mode cycles all device
    // classes to exercise the service's per-device routing (the `input` test).
    const mode = init.arguments.get(1) orelse "rotate";
    if (std.mem.eql(u8, mode, "mouse")) {
        _ = system.write("input-source: publishing synthetic mouse motion\n");
        while (true) {
            _ = source.publishMouseEvent(.{
                .kind = @intFromEnum(input.MouseEventKind.motion),
                .button = 0,
                .dx = 1,
                .dy = 1,
                .scroll_x = 0,
                .scroll_y = 0,
                .buttons = 0,
            });
            system.sleep(20); // ~50 events/sec: moves the cursor briskly
        }
    }

    _ = system.write("input-source: publishing synthetic input events\n");
    var step: usize = 0;
    while (true) : (step +%= 1) {
        // Rotate across the device classes so every publish path (and the service's
        // per-device routing) is exercised.
        switch (step % 3) {
            0 => _ = source.publishKeyboardEvent(input.syntheticKeyEvent(step)),
            1 => _ = source.publishMouseEvent(input.syntheticMouseEvent(step)),
            else => _ = source.publishJoystickEvent(input.syntheticJoystickEvent(step)),
        }
        system.sleep(200);
    }
}
