xplane 0.1.0-alpha.1

High-level interfaces to the X-Plane plugin SDK
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// SPDX-FileCopyrightText: 2024 Julia DeMille <me@jdemille.com>
//
// SPDX-License-Identifier: MPL-2.0

use std::ffi::{c_int, c_void};
use std::{
    ffi::{CString, NulError},
    marker::PhantomData,
};

use snafu::prelude::*;

use xplane_sys::{
    XPLMCommandBegin, XPLMCommandEnd, XPLMCommandOnce, XPLMCommandPhase, XPLMCommandRef,
    XPLMCreateCommand, XPLMFindCommand, XPLMRegisterCommandHandler, XPLMUnregisterCommandHandler,
};

use crate::{make_x, NoSendSync, XPAPI};

/// Struct to access X-Plane's command API.
pub struct CommandApi {
    pub(crate) _phantom: NoSendSync,
}

impl CommandApi {
    /// Make a new command.
    /// # Errors
    /// Returns an error if a matching command already exists.
    pub fn try_new(
        &mut self,
        name: &str,
        description: &str,
    ) -> Result<Command, CommandCreateError> {
        Command::try_new(name, description)
    }
    /// Finds a command
    ///
    /// The command should have already been created by X-Plane or another plugin.
    /// # Errors
    /// Errors if command could not be found.
    pub fn try_find(&mut self, name: &str) -> Result<Command, CommandFindError> {
        Command::try_find(name)
    }
}

/// A command created by X-Plane or another plugin, that can be triggered
#[derive(Debug)]
pub struct Command {
    /// The command reference
    id: XPLMCommandRef,
    _phantom: NoSendSync,
}

impl Command {
    fn try_new(name: &str, description: &str) -> Result<Self, CommandCreateError> {
        let name_c = CString::new(name)?;
        let description_c = CString::new(description)?;

        let existing = unsafe { XPLMFindCommand(name_c.as_ptr()) };
        if existing.is_null() {
            let command_id = unsafe { XPLMCreateCommand(name_c.as_ptr(), description_c.as_ptr()) };
            Ok(Command {
                id: command_id,
                _phantom: PhantomData,
            })
        } else {
            Err(CommandCreateError::Exists {
                existing_command: Command {
                    id: existing,
                    _phantom: PhantomData,
                },
            })
        }
    }
    fn try_find(name: &str) -> Result<Self, CommandFindError> {
        let name_c = CString::new(name)?;
        let command_ref = unsafe { XPLMFindCommand(name_c.as_ptr()) };
        if command_ref.is_null() {
            Err(CommandFindError::NotFound)
        } else {
            Ok(Command {
                id: command_ref,
                _phantom: PhantomData,
            })
        }
    }

    /// Triggers a command once
    ///
    /// This is equivalent to pressing a button down and immediately releasing it.
    pub fn trigger(&mut self) {
        unsafe {
            XPLMCommandOnce(self.id);
        }
    }

    /// Starts holding down this command
    ///
    /// The command will be released when the returned hold object is dropped.
    pub fn hold_down(&'_ mut self) -> CommandHold<'_> {
        unsafe {
            XPLMCommandBegin(self.id);
        }
        CommandHold {
            command: self,
            _phantom: PhantomData,
        }
    }

    /// Releases this command
    fn release(&mut self) {
        unsafe {
            XPLMCommandEnd(self.id);
        }
    }
    /// Register a [`CommandHandler`] for this command.
    ///
    /// If `before` is `true`, then this handler will be run before X-Plane executes the command.
    pub fn handle(
        &mut self,
        handler: impl CommandHandler,
        before: bool,
    ) -> RegisteredCommandHandler {
        RegisteredCommandHandler::new(self, handler, before)
    }
}

/// An RAII lock that keeps a command held down.
///
/// The command will be released when this object is dropped.
#[derive(Debug)]
pub struct CommandHold<'a> {
    /// The command being held
    command: &'a mut Command,
    _phantom: NoSendSync,
}

impl<'a> Drop for CommandHold<'a> {
    fn drop(&mut self) {
        self.command.release();
    }
}

/// Errors that can occur when finding a command
#[derive(Snafu, Debug)]
#[snafu(module)]
pub enum CommandFindError {
    /// The provided command name contained a NUL byte
    #[snafu(display("Null byte in command name."))]
    #[snafu(context(false))]
    Nul {
        /// The source of this error.
        source: NulError,
    },

    /// The Command could not be found
    #[snafu(display("Command not found."))]
    NotFound,
}

/// Enum returned from all functions of a [`CommandHandler`].
pub enum CommandHandlerResult {
    /// If handling before X-Plane, prevent X-Plane from running its own handler on this command.
    DisallowXPlaneProcessing,
    /// If handling before X-Plane, allow X-Plane to run its own handler on this command.
    AllowXPlaneProcessing,
    /// Return this if handling a command after X-Plane.
    Irrelevant,
}

impl From<CommandHandlerResult> for c_int {
    fn from(value: CommandHandlerResult) -> Self {
        match value {
            CommandHandlerResult::DisallowXPlaneProcessing => 0,
            CommandHandlerResult::AllowXPlaneProcessing | CommandHandlerResult::Irrelevant => 1,
        }
    }
}

/// Trait for things that can handle [`Commands`](Command).
/// Store your state data within the struct implementing this.
pub trait CommandHandler: 'static {
    /// Called when the command begins (corresponds to a button being pressed down)
    fn command_begin(&mut self, x: &mut XPAPI) -> CommandHandlerResult;
    /// Called frequently while the command button is held down
    fn command_continue(&mut self, x: &mut XPAPI) -> CommandHandlerResult;
    /// Called when the command ends (corresponds to a button being released)
    fn command_end(&mut self, x: &mut XPAPI) -> CommandHandlerResult;
}

/// A command created by this plugin that can be triggered by other components
pub struct RegisteredCommandHandler {
    /// The heap-allocated data
    data: *mut CommandHandlerData,
}

impl RegisteredCommandHandler {
    fn new(command: &Command, handler: impl CommandHandler, before: bool) -> Self {
        let data = Box::into_raw(Box::new(CommandHandlerData::new(command, handler, before)));
        unsafe {
            XPLMRegisterCommandHandler(
                (*data).command_ref,
                Some(command_handler),
                c_int::from(before),
                data.cast::<c_void>(),
            );
        }
        RegisteredCommandHandler { data }
    }
}

impl Drop for RegisteredCommandHandler {
    fn drop(&mut self) {
        unsafe {
            XPLMUnregisterCommandHandler(
                (*self.data).command_ref,
                Some(command_handler),
                (*self.data).before.into(),
                self.data.cast::<c_void>(),
            );
            let _ = Box::from_raw(self.data);
        }
    }
}

/// Data for an owned command, used as a refcon
struct CommandHandlerData {
    /// The command reference
    command_ref: XPLMCommandRef,
    /// The handler
    handler: *mut dyn CommandHandler,
    /// Whether this handler runs before others.
    before: bool,
}

impl CommandHandlerData {
    fn new(command: &Command, handler: impl CommandHandler, before: bool) -> Self {
        CommandHandlerData {
            command_ref: command.id,
            handler: Box::into_raw(Box::new(handler)),
            before,
        }
    }
}

impl Drop for CommandHandlerData {
    fn drop(&mut self) {
        let _ = unsafe { Box::from_raw(self.handler) };
    }
}

/// Command handler callback
unsafe extern "C-unwind" fn command_handler(
    _: XPLMCommandRef,
    phase: XPLMCommandPhase,
    refcon: *mut c_void,
) -> c_int {
    let data = refcon.cast::<CommandHandlerData>();
    let handler = unsafe { (*data).handler };
    let mut x = make_x();
    if phase == XPLMCommandPhase::Begin {
        unsafe { (*handler).command_begin(&mut x).into() }
    } else if phase == XPLMCommandPhase::Continue {
        unsafe { (*handler).command_continue(&mut x).into() }
    } else if phase == XPLMCommandPhase::End {
        unsafe { (*handler).command_end(&mut x).into() }
    } else {
        1 // If we've somehow achieved this, just let someone else deal with it.
    }
}

/// Errors that can occur when creating a Command
#[derive(Snafu, Debug)]
#[snafu(module)]
pub enum CommandCreateError {
    #[snafu(display("Null byte in Command name or description."))]
    #[snafu(context(false))]
    /// The provided Command name contained a NUL byte
    Nul {
        /// The source of this error.
        source: NulError,
    },

    #[snafu(display("Command exists already."))]
    /// The [`Command`] exists already
    Exists {
        /// The already existing command.
        existing_command: Command,
    },
}

#[cfg(test)]
mod tests {

    use std::{
        cell::RefCell,
        ffi::CStr,
        ptr::{self, NonNull},
        rc::Rc,
    };

    use super::*;

    #[test]
    #[allow(clippy::too_many_lines)] // This function has to set up several mocks.
    fn test_command_create_and_handling() {
        struct TestCommandHandler {
            internal_data: i32,
        }
        impl CommandHandler for TestCommandHandler {
            fn command_begin(&mut self, _x: &mut XPAPI) -> CommandHandlerResult {
                println!("Command begin! internal: {}", self.internal_data);
                self.internal_data = 32;
                CommandHandlerResult::AllowXPlaneProcessing
            }
            fn command_continue(&mut self, _x: &mut XPAPI) -> CommandHandlerResult {
                println!("Command continue! internal: {}", self.internal_data);
                self.internal_data = 64;
                CommandHandlerResult::DisallowXPlaneProcessing
            }
            fn command_end(&mut self, _x: &mut XPAPI) -> CommandHandlerResult {
                println!("Command end! internal: {}", self.internal_data);
                self.internal_data = 16;
                CommandHandlerResult::AllowXPlaneProcessing
            }
        }
        let refcon_cell = Rc::new(RefCell::new(NonNull::<c_void>::dangling().as_ptr()));
        let find_command_context = xplane_sys::XPLMFindCommand_context();
        find_command_context
            .expect()
            .withf(|cmd_c| {
                let cmd_c = unsafe { CStr::from_ptr(*cmd_c) };
                cmd_c == CString::new("xplane_rs/test/command").unwrap().as_c_str()
                // This contains no NUL bytes, and so should construct a C-string.
            })
            .once()
            .return_once_st(|_| std::ptr::null_mut());
        let create_command_context = xplane_sys::XPLMCreateCommand_context();
        let expected_ptr = NonNull::<c_void>::dangling().as_ptr();
        create_command_context
            .expect()
            .withf(|cmd_c, desc_c| {
                let cmd_c = unsafe { CStr::from_ptr(*cmd_c) };
                let desc_c = unsafe { CStr::from_ptr(*desc_c) };
                (cmd_c == CString::new("xplane_rs/test/command").unwrap().as_c_str())
                    && (desc_c
                        == CString::new("A test command for rust-xplane unit tests.")
                            .unwrap()
                            .as_c_str())
            })
            .once()
            .return_once_st(move |_, _| expected_ptr);
        let register_handler_ctx = xplane_sys::XPLMRegisterCommandHandler_context();
        let refcon_cell_1 = refcon_cell.clone();
        register_handler_ctx.expect().once().return_once_st(
            move |cmd_ref, handler, before, refcon| {
                assert_eq!(cmd_ref, expected_ptr);
                assert!(handler == Some(command_handler));
                assert_eq!(before, 1);
                *refcon_cell_1.borrow_mut() = refcon;
            },
        );
        let unregister_handler_ctx = xplane_sys::XPLMUnregisterCommandHandler_context();
        let refcon_cell_1 = refcon_cell.clone();
        unregister_handler_ctx.expect().once().return_once_st(
            move |cmd_ref, handler, before, refcon| {
                assert_eq!(cmd_ref, expected_ptr);
                assert!(handler == Some(command_handler));
                assert_eq!(before, 1);
                assert_eq!(refcon, *refcon_cell_1.borrow());
            },
        );
        let command_once_ctx = xplane_sys::XPLMCommandOnce_context();
        let refcon_cell_1 = refcon_cell.clone();
        command_once_ctx
            .expect()
            .once()
            .return_once_st(move |cmd_ref| {
                assert!(cmd_ref == expected_ptr);
                let res = unsafe {
                    command_handler(cmd_ref, XPLMCommandPhase::Begin, *refcon_cell_1.borrow())
                };
                assert_eq!(res, 1);
                let res = unsafe {
                    command_handler(cmd_ref, XPLMCommandPhase::End, *refcon_cell_1.borrow())
                };
                assert_eq!(res, 1);
            });
        let command_begin_ctx = xplane_sys::XPLMCommandBegin_context();
        let refcon_cell_1 = refcon_cell.clone();
        command_begin_ctx
            .expect()
            .once()
            .return_once_st(move |cmd_ref| {
                assert!(cmd_ref == expected_ptr);
                let res = unsafe {
                    command_handler(cmd_ref, XPLMCommandPhase::Begin, *refcon_cell_1.borrow())
                };
                assert_eq!(res, 1);
                let res = unsafe {
                    command_handler(cmd_ref, XPLMCommandPhase::Continue, *refcon_cell_1.borrow())
                };
                assert_eq!(res, 0);
            });
        let command_end_ctx = xplane_sys::XPLMCommandEnd_context();
        let refcon_cell_1 = refcon_cell.clone();
        command_end_ctx
            .expect()
            .once()
            .return_once_st(move |cmd_ref| {
                assert!(cmd_ref == expected_ptr);
                let res = unsafe {
                    command_handler(cmd_ref, XPLMCommandPhase::End, *refcon_cell_1.borrow())
                };
                assert_eq!(res, 1);
            });
        let mut x = make_x();
        let mut cmd = x
            .command
            .try_new(
                "xplane_rs/test/command",
                "A test command for rust-xplane unit tests.",
            )
            .unwrap(); // This should succeed.
        let _reg_handler = cmd.handle(TestCommandHandler { internal_data: 0 }, true);
        cmd.trigger();
        {
            let _ = cmd.hold_down();
        }
    }

    #[test]
    fn test_command_exists() {
        let expected_ptr = NonNull::<c_void>::dangling().as_ptr();
        let find_command_context = xplane_sys::XPLMFindCommand_context();
        find_command_context
            .expect()
            .withf(|cmd_c| {
                let cmd_c = unsafe { CStr::from_ptr(*cmd_c) };
                cmd_c == CString::new("xplane_rs/test/command").unwrap().as_c_str()
                // This contains no NUL bytes, and so should construct a C-string.
            })
            .once()
            .return_once_st(move |_| expected_ptr);
        let mut x = make_x();
        let cmd_result = x
            .command
            .try_new("xplane_rs/test/command", "Test command.")
            .unwrap_err();
        let CommandCreateError::Exists { existing_command } = cmd_result else {
            unreachable!("This code should be unreachable!")
        };
        assert_eq!(existing_command.id, expected_ptr);
    }

    #[test]
    fn test_command_found() {
        let expected_ptr = NonNull::<c_void>::dangling().as_ptr();
        let find_command_context = xplane_sys::XPLMFindCommand_context();
        find_command_context
            .expect()
            .withf(|cmd_c| {
                let cmd_c = unsafe { CStr::from_ptr(*cmd_c) };
                cmd_c == CString::new("xplane_rs/test/command").unwrap().as_c_str()
                // This contains no NUL bytes, and so should construct a C-string.
            })
            .once()
            .return_once_st(move |_| expected_ptr);
        let mut x = make_x();
        let cmd = x.command.try_find("xplane_rs/test/command").unwrap();
        assert_eq!(cmd.id, expected_ptr);
    }

    #[test]
    fn test_command_not_found() {
        let find_command_context = xplane_sys::XPLMFindCommand_context();
        find_command_context
            .expect()
            .withf(|cmd_c| {
                let cmd_c = unsafe { CStr::from_ptr(*cmd_c) };
                cmd_c == CString::new("xplane_rs/test/command").unwrap().as_c_str()
                // This contains no NUL bytes, and so should construct a C-string.
            })
            .once()
            .return_once_st(|_| ptr::null_mut());
        let mut x = make_x();
        let cmd_result = x.command.try_find("xplane_rs/test/command");
        assert!(matches!(cmd_result, Err(CommandFindError::NotFound)));
    }
}