ark-api-ffi 0.16.0

Ark low-level Wasm FFI API
Documentation
define_api_id!(0xbb0f_0dfe_ff53_2a55, "applet-v0");

use bytemuck::CheckedBitPattern;
use bytemuck::NoUninit;
use bytemuck::Pod;
use bytemuck::Zeroable;
use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;

/// Symbolic name for a keyboard key
#[allow(missing_docs)]
#[derive(
    Copy, Clone, Debug, Hash, Eq, PartialEq, TryFromPrimitive, NoUninit, CheckedBitPattern,
)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "with_speedy", derive(speedy::Writable, speedy::Readable))]
#[repr(u32)]
pub enum VirtualKeyCode {
    /// The '1' key over the letters.
    Key1 = 0,
    /// The '2' key over the letters.
    Key2 = 1,
    /// The '3' key over the letters.
    Key3 = 2,
    /// The '4' key over the letters.
    Key4 = 3,
    /// The '5' key over the letters.
    Key5 = 4,
    /// The '6' key over the letters.
    Key6 = 5,
    /// The '7' key over the letters.
    Key7 = 6,
    /// The '8' key over the letters.
    Key8 = 7,
    /// The '9' key over the letters.
    Key9 = 8,
    /// The '0' key over the 'O' and 'P' keys.
    Key0 = 9,

    A = 10,
    B = 11,
    C = 12,
    D = 13,
    E = 14,
    F = 15,
    G = 16,
    H = 17,
    I = 18,
    J = 19,
    K = 20,
    L = 21,
    M = 22,
    N = 23,
    O = 24,
    P = 25,
    Q = 26,
    R = 27,
    S = 28,
    T = 29,
    U = 30,
    V = 31,
    W = 32,
    X = 33,
    Y = 34,
    Z = 35,

    /// The Escape key, next to F1.
    Escape = 36,

    /// `Insert`, next to Backspace.
    Insert = 64,
    Home = 65,
    Delete = 66,
    End = 67,
    PageDown = 68,
    PageUp = 69,

    Left = 70,
    Up = 71,
    Right = 72,
    Down = 73,

    /// The Backspace key, right over Enter.
    // TODO: rename
    Back = 74,
    /// The Enter key.
    Return = 75,
    /// The space bar.
    Space = 76,

    Add = 92,
    Apostrophe = 93,
    Backslash = 97,
    Colon = 100,
    Comma = 101,
    Divide = 104,
    Equals = 105,
    LAlt = 109,
    LBracket = 110,
    LControl = 111,
    LShift = 112,
    Period = 129,
    RAlt = 133,
    RBracket = 134,
    RControl = 135,
    RShift = 136,
    Semicolon = 138,
    Slash = 139,
    Tab = 144,

    /// On Windows and Linux, LControl/RControl input events will
    /// generate an additional LCommand/RCommand input event to
    /// avoid platform specifics.
    LCommand = 161, // Left Command key on Mac. Left Control key on Windows + Linux.
    RCommand = 162, // Right Command key on Mac. Right Control key on Windows + Linux.
}

/// Key event type.
#[repr(u32)]
#[derive(
    Copy,
    Clone,
    Debug,
    Hash,
    Eq,
    PartialEq,
    IntoPrimitive,
    TryFromPrimitive,
    NoUninit,
    CheckedBitPattern,
)]
pub enum KeyEventType {
    KeyPress = 0,
    KeyRelease = 1,
    Text = 2,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, NoUninit, CheckedBitPattern)]
pub struct KeyInput {
    pub event_type: KeyEventType,
    pub code: VirtualKeyCode,
    pub text: char, // Unicode (UTF-32, char in Rust is 32-bit)
}

crate::impl_checked_bit_pattern_for_transparent_pad!(KeyInput);

/// Mouse event type enumeration.
#[repr(u32)]
#[derive(
    Copy,
    Clone,
    Debug,
    Hash,
    Eq,
    PartialEq,
    IntoPrimitive,
    TryFromPrimitive,
    NoUninit,
    CheckedBitPattern,
)]
#[non_exhaustive]
pub enum MouseEventType {
    Move = 0,
    ButtonPress = 1,
    ButtonRelease = 2,
    Scroll = 3,
    RelativeMove = 4,
    /// TODO: deprecate as it is no longer supported by the higher level API
    Still = 5,
}

#[repr(u32)]
#[derive(
    Copy,
    Clone,
    Debug,
    Hash,
    Eq,
    PartialEq,
    IntoPrimitive,
    TryFromPrimitive,
    NoUninit,
    CheckedBitPattern,
)]
pub enum MouseButton {
    Primary = 1, // Left by default
    Secondary = 2,
    Middle = 3,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, NoUninit, CheckedBitPattern)]
pub struct MouseInput {
    pub event_type: MouseEventType,
    /// If event_type == `ButtonPress` or `ButtonRelease`, indicates which button was pressed.
    pub button: MouseButton,
    /// Position, wheel or relative movement on the X axis. For positions, values are in logical pixels.
    pub x: f32,
    /// Position, wheel or relative movement on the Y axis. For positions, values are in logical pixels.
    pub y: f32,
    /// Ray origin (world space). Applicable for moves and button presses.
    pub ray_origin: [f32; 3],
    /// Ray direction (world space). Applicable for moves and button presses.
    pub ray_dir: [f32; 3],
}

/// Touch event type.
#[repr(u32)]
#[derive(
    Copy, Clone, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive, NoUninit, CheckedBitPattern,
)]
pub enum TouchEventType {
    /// Every time a user touches the screen a new `Started` event is generated
    Started = 0,
    /// After a `Started` event has been emitted, there may be zero or more `Move`
    /// events when the finger is moved.
    Move = 1,
    /// Relative move for touch is deprecated - relative events mainly make sense for mouse
    /// (since they can still happen when the mouse hits the screen edge - no such thing
    /// for touch).
    RelativeMove = 2,
    /// When the finger is lifted, an `Ended` event is generated
    Ended = 3,
    /// TODO: deprecate as it is no longer supported by the higher level API
    Still = 4,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, NoUninit, CheckedBitPattern)]
pub struct TouchInput {
    /// Represents a touch event
    pub event_type: TouchEventType,
    /// Unique identifier of a finger.
    pub id: u32,
    /// Position or relative movement on the X axis. For positions, values are in logical pixels.
    pub x: f32,
    /// Position or relative movement on the Y axis. For positions, values are in logical pixels.
    pub y: f32,
    /// Ray origin (world space). Applicable for moves and button presses.
    pub ray_origin: [f32; 3],
    /// Ray direction (world space). Applicable for moves and button presses.
    pub ray_dir: [f32; 3],
}

/// Contains information about the window
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Pod, Zeroable)]
pub struct WindowState {
    /// Viewport index
    pub viewport_index: u32,
    /// Window width in logical pixels
    pub width: f32,
    /// Window height in logical pixels
    pub height: f32,
    /// DPI scale factor. Multiply logical pixel coordinate with `dpi_scale` to get physical pixels.
    /// Divide to convert the other way.
    pub dpi_factor: f32,
}

#[ark_api_macros::ark_bindgen(imports = "ark-applet-v0")]
mod applet {
    /// [`EventType`] maps to a specific type
    /// `Key` => `KeyInput`
    #[repr(u32)]
    #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
    #[non_exhaustive]
    pub enum EventType {
        Key = 0,
        Mouse = 2,
        Window = 3,
        //Hmd = 4,
        Touch = 5,
        Players = 6,
    }

    #[repr(u32)]
    #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
    #[non_exhaustive]
    #[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
    #[cfg_attr(feature = "with_speedy", derive(speedy::Writable, speedy::Readable))]
    #[cfg_attr(feature = "with_arbitrary", derive(arbitrary::Arbitrary))]
    pub enum CursorMode {
        None = 0,
        Grab = 1,
        Hide = 2,
        GrabHide = 3,
    }

    extern "C" {
        /// Returns the time between the current and last frame
        #[deprecated_infallible]
        pub fn delta_time() -> f32;

        /// The real time that has passed since the module was started, in seconds.
        ///
        /// Ignores things like pausing, computer falling asleep, etc.
        /// Also know as [_wall time_](https://en.wikipedia.org/wiki/Elapsed_real_time).
        #[deprecated_infallible]
        pub fn absolute_time() -> f64;

        /// The size of the allocation in bytes for the a given [`EventType`].
        #[deprecated_infallible]
        pub fn event_size(ty: EventType) -> u32;

        /// Writes all events for a given [`EventType`] into `out_events`.
        ///
        /// `ty`: The [`EventType`] of the event
        /// `out_events`: The ptr where the events are written to
        #[deprecated_infallible]
        pub fn retrieve_events(ty: EventType, out_events: &mut [u8]);

        #[deprecated_infallible]
        pub fn set_cursor_mode(mode: CursorMode);
    }
}

pub use applet::*;