infinity-rs 0.2.0

Safe, idiomatic Rust bindings for the MSFS 2024 WASM SDK.
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
//! Safe wrapper around the MSFS `fsPlannedRoute*` API.
//!
//! Three primitives:
//!
//! * [`current_efb_route`] — borrow the route currently held by the EFB, if
//!   one is set.
//! * [`subscribe_broadcast`] — listen for route updates pushed by the EFB
//!   or other planners.
//! * [`subscribe_requests`] — receive [`RouteRequest`] handles when a
//!   consumer asks "what is the current planned route?"; respond by handing
//!   back any [`PlannedRoute`] borrow.
//!
//! Routes are host-owned. The wrappers here only borrow them — there is no
//! free/destroy entry point in the SDK header, so we never call one.

use crate::sys;

use std::cell::RefCell;
use std::ffi::CStr;
use std::os::raw::c_void;
use std::ptr;

#[derive(Debug)]
pub enum PlannedRouteError {
    RegistrationFailed,
    UnregisterFailed,
}

impl std::fmt::Display for PlannedRouteError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::RegistrationFailed => f.write_str("planned route registration failed"),
            Self::UnregisterFailed => f.write_str("planned route unregistration failed"),
        }
    }
}

impl std::error::Error for PlannedRouteError {}

pub type PlannedRouteResult<T> = Result<T, PlannedRouteError>;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum FlightAltitudeType {
    None = 0,
    Feet = 1,
    FlightLevel = 2,
}

impl FlightAltitudeType {
    fn from_raw(raw: sys::FsFlightAltitudeType) -> Self {
        match raw as u32 {
            1 => Self::Feet,
            2 => Self::FlightLevel,
            _ => Self::None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum EnrouteLegType {
    Normal = 0,
    LatLon = 1,
    PointBearingDistance = 2,
}

impl EnrouteLegType {
    fn from_raw(raw: sys::FsEnrouteLegType) -> Self {
        match raw as u32 {
            1 => Self::LatLon,
            2 => Self::PointBearingDistance,
            _ => Self::Normal,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FlightAltitude {
    pub kind: FlightAltitudeType,
    /// Altitude value, interpreted per `kind` (feet or flight level).
    pub value: i32,
}

impl FlightAltitude {
    fn from_raw(raw: &sys::FsFlightAltitude) -> Self {
        Self {
            kind: FlightAltitudeType::from_raw(raw.type_),
            value: raw.altitude,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VisualPattern {
    pub pattern: i32,
    pub is_left_traffic: bool,
    pub distance: f32,
    pub altitude: f32,
}

impl VisualPattern {
    fn from_raw(raw: &sys::FsVisualPattern) -> Self {
        Self {
            pattern: raw.pattern,
            is_left_traffic: raw.isLeftTraffic,
            distance: raw.distance,
            altitude: raw.altitude,
        }
    }
}

/// Borrowed view over a single enroute leg.
#[derive(Clone, Copy)]
pub struct EnrouteLegRef<'a> {
    raw: &'a sys::FsEnrouteLeg,
}

impl<'a> EnrouteLegRef<'a> {
    pub fn kind(self) -> EnrouteLegType {
        EnrouteLegType::from_raw(self.raw.type_)
    }

    pub fn fix_icao(self) -> &'a sys::FsIcao {
        &self.raw.fixIcao
    }

    pub fn via(self) -> &'a str {
        c_array_to_str(&self.raw.via)
    }

    pub fn name(self) -> &'a str {
        unsafe { c_str_to_borrowed(self.raw.name) }
    }

    pub fn altitude(self) -> FlightAltitude {
        FlightAltitude::from_raw(&self.raw.altitude)
    }

    pub fn lat(self) -> f64 {
        self.raw.lat
    }

    pub fn lon(self) -> f64 {
        self.raw.lon
    }

    pub fn pbd_reference_icao(self) -> &'a sys::FsIcao {
        &self.raw.pbdReferenceIcao
    }

    pub fn bearing(self) -> f64 {
        self.raw.bearing
    }

    pub fn distance(self) -> f64 {
        self.raw.distance
    }
}

/// Borrowed view over a host-owned `FsPlannedRoute`.
///
/// Holds a raw pointer plus a lifetime so the borrow can't outlive the
/// callback (or, in the case of [`current_efb_route`], the next host poke).
#[derive(Clone, Copy)]
pub struct PlannedRoute<'a> {
    raw: *const sys::FsPlannedRoute,
    _life: std::marker::PhantomData<&'a sys::FsPlannedRoute>,
}

unsafe impl Send for PlannedRoute<'_> {}

impl<'a> PlannedRoute<'a> {
    /// # Safety
    /// `raw` must be a valid `FsPlannedRoute*` for the duration `'a`.
    pub unsafe fn from_raw(raw: *const sys::FsPlannedRoute) -> Self {
        Self {
            raw,
            _life: std::marker::PhantomData,
        }
    }

    /// Underlying pointer. Useful to relay the route into
    /// [`RouteRequest::respond`].
    #[inline]
    pub fn as_ptr(&self) -> *const sys::FsPlannedRoute {
        self.raw
    }

    fn r(&self) -> &'a sys::FsPlannedRoute {
        unsafe { &*self.raw }
    }

    pub fn departure_airport(&self) -> &'a sys::FsIcao {
        &self.r().departureAirport
    }

    pub fn departure_runway(&self) -> sys::FsRunwayIdentifier {
        // FsPlannedRoute is `#pragma pack(1)`, so inner struct fields can't
        // be borrowed at their natural alignment — copy out instead.
        unsafe { ptr::read_unaligned(ptr::addr_of!(self.r().departureRunway)) }
    }

    pub fn departure(&self) -> &'a str {
        c_array_to_str(&self.r().departure)
    }

    pub fn departure_transition(&self) -> &'a str {
        c_array_to_str(&self.r().departureTransition)
    }

    pub fn departure_visual_pattern(&self) -> VisualPattern {
        VisualPattern::from_raw(&self.r().departureVisualPattern)
    }

    pub fn destination_airport(&self) -> &'a sys::FsIcao {
        &self.r().destinationAirport
    }

    pub fn destination_runway(&self) -> sys::FsRunwayIdentifier {
        unsafe { ptr::read_unaligned(ptr::addr_of!(self.r().destinationRunway)) }
    }

    pub fn arrival(&self) -> &'a str {
        c_array_to_str(&self.r().arrival)
    }

    pub fn arrival_transition(&self) -> &'a str {
        c_array_to_str(&self.r().arrivalTransition)
    }

    pub fn approach(&self) -> sys::FsApproachIdentifier {
        unsafe { ptr::read_unaligned(ptr::addr_of!(self.r().approach)) }
    }

    pub fn approach_transition(&self) -> &'a str {
        c_array_to_str(&self.r().approachTransition)
    }

    pub fn approach_visual_pattern(&self) -> VisualPattern {
        VisualPattern::from_raw(&self.r().approachVisualPattern)
    }

    pub fn cruise_altitude(&self) -> FlightAltitude {
        FlightAltitude::from_raw(&self.r().cruiseAltitude)
    }

    pub fn is_vfr(&self) -> bool {
        self.r().isVfr
    }

    pub fn enroute_legs(&self) -> &'a [sys::FsEnrouteLeg] {
        let r = self.r();
        unsafe {
            if r.enrouteLegs.is_null() || r.numEnrouteLegs <= 0 {
                &[]
            } else {
                std::slice::from_raw_parts(r.enrouteLegs, r.numEnrouteLegs as usize)
            }
        }
    }

    pub fn iter_enroute_legs(&self) -> impl Iterator<Item = EnrouteLegRef<'a>> {
        self.enroute_legs().iter().map(|l| EnrouteLegRef { raw: l })
    }
}

/// Fetch the EFB's current planned route, if one is set.
///
/// The returned borrow is tied to `'static` for ergonomics; treat it as
/// short-lived and re-fetch each frame rather than caching across host
/// callbacks (the EFB may swap routes at any time).
pub fn current_efb_route() -> Option<PlannedRoute<'static>> {
    let raw = unsafe { sys::fsPlannedRouteGetEfbRoute() };
    if raw.is_null() {
        None
    } else {
        Some(unsafe { PlannedRoute::from_raw(raw) })
    }
}

// -- Subscriptions -------------------------------------------------------------

type BroadcastHandler = Box<dyn FnMut(PlannedRoute<'_>)>;
type RequestHandler = Box<dyn FnMut(RouteRequest)>;

struct BroadcastSlot {
    id: u64,
    handler: BroadcastHandler,
}

struct RequestSlot {
    id: u64,
    handler: RequestHandler,
}

#[derive(Default)]
struct Registry {
    broadcast_registered: bool,
    request_registered: bool,
    next_id: u64,
    broadcast: Vec<BroadcastSlot>,
    requests: Vec<RequestSlot>,
}

thread_local! {
    static REGISTRY: RefCell<Registry> = RefCell::new(Registry::default());
}

extern "C" fn broadcast_trampoline(route: *const sys::FsPlannedRoute, _ctx: *mut c_void) {
    if route.is_null() {
        return;
    }
    let view = unsafe { PlannedRoute::from_raw(route) };
    REGISTRY.with(|registry| {
        let mut r = registry.borrow_mut();
        for slot in &mut r.broadcast {
            (slot.handler)(view);
        }
    });
}

extern "C" fn request_trampoline(id: sys::FsRouteRequestId, _ctx: *mut c_void) {
    REGISTRY.with(|registry| {
        let mut r = registry.borrow_mut();
        for slot in &mut r.requests {
            (slot.handler)(RouteRequest { id });
        }
    });
}

/// Handle issued by the host when a consumer asks for the current route.
///
/// Forward whatever route you have via [`RouteRequest::respond`]. The handle
/// can be ignored if you have nothing to send — the host will time the
/// requester out.
pub struct RouteRequest {
    id: sys::FsRouteRequestId,
}

impl RouteRequest {
    #[inline]
    pub fn id(&self) -> sys::FsRouteRequestId {
        self.id
    }

    /// Reply with a planned route. The host copies what it needs before
    /// returning, so a borrowed view (e.g. from [`current_efb_route`]) is
    /// fine.
    pub fn respond(self, route: PlannedRoute<'_>) -> bool {
        unsafe {
            sys::fsPlannedRouteRespondToRequest(self.id, route.as_ptr() as *mut _)
        }
    }
}

pub struct BroadcastSubscription {
    id: u64,
}

impl BroadcastSubscription {
    pub fn register(handler: impl FnMut(PlannedRoute<'_>) + 'static) -> PlannedRouteResult<Self> {
        REGISTRY.with(|registry| {
            let mut r = registry.borrow_mut();
            if !r.broadcast_registered {
                let ok = unsafe {
                    sys::fsPlannedRouteRegisterForBroadcast(
                        Some(broadcast_trampoline),
                        ptr::null_mut(),
                    )
                };
                if !ok {
                    return Err(PlannedRouteError::RegistrationFailed);
                }
                r.broadcast_registered = true;
            }
            let id = r.next_id;
            r.next_id = r.next_id.wrapping_add(1);
            r.broadcast.push(BroadcastSlot {
                id,
                handler: Box::new(handler),
            });
            Ok(Self { id })
        })
    }
}

impl Drop for BroadcastSubscription {
    fn drop(&mut self) {
        REGISTRY.with(|registry| {
            let mut r = registry.borrow_mut();
            r.broadcast.retain(|s| s.id != self.id);
            if r.broadcast.is_empty() && r.broadcast_registered {
                unsafe {
                    let _ = sys::fsPlannedRouteUnregisterForBroadcast(Some(broadcast_trampoline));
                };
                r.broadcast_registered = false;
            }
        });
    }
}

pub struct RequestSubscription {
    id: u64,
}

impl RequestSubscription {
    pub fn register(handler: impl FnMut(RouteRequest) + 'static) -> PlannedRouteResult<Self> {
        REGISTRY.with(|registry| {
            let mut r = registry.borrow_mut();
            if !r.request_registered {
                let ok = unsafe {
                    sys::fsPlannedRouteRegisterForRequest(
                        Some(request_trampoline),
                        ptr::null_mut(),
                    )
                };
                if !ok {
                    return Err(PlannedRouteError::RegistrationFailed);
                }
                r.request_registered = true;
            }
            let id = r.next_id;
            r.next_id = r.next_id.wrapping_add(1);
            r.requests.push(RequestSlot {
                id,
                handler: Box::new(handler),
            });
            Ok(Self { id })
        })
    }
}

impl Drop for RequestSubscription {
    fn drop(&mut self) {
        REGISTRY.with(|registry| {
            let mut r = registry.borrow_mut();
            r.requests.retain(|s| s.id != self.id);
            if r.requests.is_empty() && r.request_registered {
                unsafe {
                    let _ = sys::fsPlannedRouteUnregisterForRequest(Some(request_trampoline));
                };
                r.request_registered = false;
            }
        });
    }
}

pub fn subscribe_broadcast(
    handler: impl FnMut(PlannedRoute<'_>) + 'static,
) -> PlannedRouteResult<BroadcastSubscription> {
    BroadcastSubscription::register(handler)
}

pub fn subscribe_requests(
    handler: impl FnMut(RouteRequest) + 'static,
) -> PlannedRouteResult<RequestSubscription> {
    RequestSubscription::register(handler)
}

// -- helpers ------------------------------------------------------------------

fn c_array_to_str(buf: &[std::os::raw::c_char]) -> &str {
    // Trim at the first NUL, then decode as UTF-8.
    let bytes: &[u8] = unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len()) };
    let end = bytes.iter().position(|b| *b == 0).unwrap_or(bytes.len());
    std::str::from_utf8(&bytes[..end]).unwrap_or("")
}

unsafe fn c_str_to_borrowed<'a>(p: *const std::os::raw::c_char) -> &'a str {
    if p.is_null() {
        ""
    } else {
        unsafe { CStr::from_ptr(p) }.to_str().unwrap_or("")
    }
}