1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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::safe as safe_v0;
pub use applet::CursorMode;
pub use applet::EventType;
#[cfg(not(target_arch = "wasm32"))]
pub use applet::HostShim as HostShim_v0;