//! Fixed queries over the logical-frame input snapshot. No endpoint or selector //! crosses the guest boundary; names below identify operations, not value types. use crate::builtins::{AbiType, BuiltinScalarType, IntrinsicExecutionError, IntrinsicMeta}; use prometeu_bytecode::Value; use prometeu_hal::{HostContext, InputPlatform, button::Button}; const BOOL: &[AbiType] = &[AbiType::Scalar(BuiltinScalarType::Bool)]; const INT: &[AbiType] = &[AbiType::Scalar(BuiltinScalarType::Int64)]; const fn query( id: u32, owner: &'static str, name: &'static str, ret_layout: &'static [AbiType], implementation: crate::IntrinsicHostImplementation, ) -> IntrinsicMeta { IntrinsicMeta { id, owner, name, version: 1, arg_layout: &[], ret_layout, parameter_names: &[], cost_cycles: 1, cost_model: crate::IntrinsicCostModel::Fixed, attempt_cost_cycles: 0, max_attempts: 0, mutates_guest_state: false, deterministic: true, may_allocate: false, implementation: crate::IntrinsicImplementation::Host(implementation), } } fn snapshot<'a>( args: &[Value], ctx: &'a HostContext<'_>, ) -> Result<&'a dyn InputPlatform, IntrinsicExecutionError> { if !args.is_empty() { return Err(IntrinsicExecutionError::ArityMismatch { expected: 0, got: args.len() }); } let platform = ctx.platform.as_deref().ok_or(IntrinsicExecutionError::HardwareUnavailable)?; Ok(platform.input()) } fn read_button(button: &Button) -> Vec { vec![match PROPERTY { 0 => Value::Boolean(button.pressed), 1 => Value::Boolean(button.released), 2 => Value::Boolean(button.down), 3 => Value::Int64(i64::from(button.hold_frames)), _ => unreachable!("private input property outside the fixed catalogue"), }] } // This single mapping defines IDs, canonical names and their snapshot readers. macro_rules! input_queries { ($(($base:literal, $prefix:literal, $reader:ident, $device:ident, $button:ident)),* $(,)?) => { pub(crate) const INPUT_INTRINSICS: [IntrinsicMeta; 54] = [ $( query($base, $prefix, "pressed", BOOL, $reader::<0>), query($base + 1, $prefix, "released", BOOL, $reader::<1>), query($base + 2, $prefix, "down", BOOL, $reader::<2>), query($base + 3, $prefix, "hold", INT, $reader::<3>), )* query(0x2134, "input.touch", "x", INT, touch_x), query(0x2135, "input.touch", "y", INT, touch_y), ]; $( fn $reader( args: &[Value], ctx: &mut HostContext<'_>, ) -> Result, IntrinsicExecutionError> { Ok(read_button::(snapshot(args, ctx)?.$device().$button())) } )* } } input_queries!( (0x2100, "input.pad.up", up, pad, up), (0x2104, "input.pad.down", down, pad, down), (0x2108, "input.pad.left", left, pad, left), (0x210C, "input.pad.right", right, pad, right), (0x2110, "input.pad.a", a, pad, a), (0x2114, "input.pad.b", b, pad, b), (0x2118, "input.pad.x", x, pad, x), (0x211C, "input.pad.y", y, pad, y), (0x2120, "input.pad.l", l, pad, l), (0x2124, "input.pad.r", r, pad, r), (0x2128, "input.pad.start", start, pad, start), (0x212C, "input.pad.select", select, pad, select), (0x2130, "input.touch.button", touch_button, touch, f), ); fn touch_x( args: &[Value], ctx: &mut HostContext<'_>, ) -> Result, IntrinsicExecutionError> { Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().x()))]) } fn touch_y( args: &[Value], ctx: &mut HostContext<'_>, ) -> Result, IntrinsicExecutionError> { Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().y()))]) }