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
// SPDX-FileCopyrightText: 2024 Julia DeMille <me@jdemille.com>
//
// SPDX-License-Identifier: MPL-2.0
/// Creates an X-Plane plugin
///
/// Provide the name of your plugin struct. The callbacks that X-Plane uses will be created.
///
/// Creating a plugin involves three steps:
///
/// 1. Create a struct for your plugin
/// 2. Implement Plugin for your plugin struct
/// 3. Place `xplane_plugin!(YourPluginStruct)` in a file, not in any function
///
#[macro_export]
macro_rules! xplane_plugin {
($plugin_type: ty) => {
struct _UncheckedSyncUnsafeCell<T>(::std::cell::UnsafeCell<T>);
/// Safety: dereferencing the pointer from `UnsafeCell::get` must involve external synchronization.
unsafe impl Sync
for _UncheckedSyncUnsafeCell<$crate::plugin::internal::PluginData<$plugin_type>>
{
}
// The plugin
static PLUGIN: _UncheckedSyncUnsafeCell<
$crate::plugin::internal::PluginData<$plugin_type>,
> = _UncheckedSyncUnsafeCell(::std::cell::UnsafeCell::new(
$crate::plugin::internal::PluginData {
plugin: ::std::ptr::null_mut(),
},
));
#[allow(non_snake_case)]
#[no_mangle]
/// Shim around `xplane::plugin::internal::xplugin_start`.
pub unsafe extern "C-unwind" fn XPluginStart(
name: *mut ::std::ffi::c_char,
signature: *mut ::std::ffi::c_char,
description: *mut ::std::ffi::c_char,
) -> ::std::ffi::c_int {
unsafe {
$crate::plugin::internal::xplugin_start(
PLUGIN
.0
.get()
.as_mut()
.expect("The contents of PLUGIN should never be NULL."),
name,
signature,
description,
)
}
}
#[allow(non_snake_case)]
#[no_mangle]
/// Shim around `xplane::plugin::internal::xplugin_stop`.
pub unsafe extern "C-unwind" fn XPluginStop() {
unsafe {
$crate::plugin::internal::xplugin_stop(
PLUGIN
.0
.get()
.as_mut()
.expect("The contents of PLUGIN should never be NULL."),
)
}
}
#[allow(non_snake_case)]
#[no_mangle]
/// Shim around `xplane::plugin::internal::xplugin_enable`.
pub unsafe extern "C-unwind" fn XPluginEnable() -> ::std::ffi::c_int {
unsafe {
$crate::plugin::internal::xplugin_enable(
PLUGIN
.0
.get()
.as_mut()
.expect("The contents of PLUGIN should never be NULL."),
)
}
}
#[allow(non_snake_case)]
#[no_mangle]
/// Shim around `xplane::plugin::internal::xplugin_disable`.
pub unsafe extern "C-unwind" fn XPluginDisable() {
unsafe {
$crate::plugin::internal::xplugin_disable(
PLUGIN
.0
.get()
.as_mut()
.expect("The contents of PLUGIN should never be NULL."),
)
}
}
#[allow(non_snake_case)]
#[no_mangle]
/// Shim around `xplane::plugin::internal::xplugin_receive_message`.
pub unsafe extern "C-unwind" fn XPluginReceiveMessage(
from: ::std::ffi::c_int,
message: ::std::ffi::c_int,
param: *mut ::std::ffi::c_void,
) {
unsafe {
$crate::plugin::internal::xplugin_receive_message(
PLUGIN
.0
.get()
.as_mut()
.expect("The contents of PLUGIN should never be NULL."),
from,
message,
param,
)
}
}
};
}