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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#![allow(clippy::too_many_arguments)]

use crate::sys;
use std::any::TypeId;
use std::collections::HashMap;
use std::pin::Pin;

pub use sys::SIMCONNECT_OBJECT_ID_USER;

pub use msfs_derive::sim_connect_client_data_definition as client_data_definition;
pub use msfs_derive::sim_connect_data_definition as data_definition;

pub type DataXYZ = sys::SIMCONNECT_DATA_XYZ;
pub type InitPosition = sys::SIMCONNECT_DATA_INITPOSITION;

/// A trait implemented by the `data_definition` attribute.
pub trait DataDefinition: 'static {
    #[doc(hidden)]
    const DEFINITIONS: &'static [(&'static str, &'static str, f32, sys::SIMCONNECT_DATATYPE)];
}

/// A trait implemented by the `client_data_definition` attribute.
pub trait ClientDataDefinition: 'static {
    #[doc(hidden)]
    fn get_definitions() -> Vec<(usize, usize, f32)>;
}

/// Rusty HRESULT wrapper.
#[derive(Debug)]
pub struct HResult(sys::HRESULT);
impl std::fmt::Display for HResult {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        std::fmt::Debug::fmt(self, fmt)
    }
}
impl std::error::Error for HResult {}

pub type Result<T> = std::result::Result<T, HResult>;
#[inline(always)]
fn map_err(result: sys::HRESULT) -> Result<()> {
    if result >= 0 {
        Ok(())
    } else {
        Err(HResult(result))
    }
}

type SimConnectCallback<'a> = dyn FnMut(&mut SimConnect, SimConnectRecv) + 'a;

/// A SimConnect session. This provides access to data within the MSFS sim.
pub struct SimConnect<'a> {
    handle: sys::HANDLE,
    callback: Box<SimConnectCallback<'a>>,
    data_definitions: HashMap<TypeId, sys::SIMCONNECT_DATA_DEFINITION_ID>,
    client_data_definitions: HashMap<TypeId, sys::SIMCONNECT_CLIENT_DATA_DEFINITION_ID>,
    event_id_counter: sys::DWORD,
    client_data_id_counter: sys::DWORD,
}

impl<'a> std::fmt::Debug for SimConnect<'a> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_struct("SimConnect").finish()
    }
}

impl<'a> SimConnect<'a> {
    /// Send a request to the Microsoft Flight Simulator server to open up communications with a new client.
    pub fn open<F>(name: &str, callback: F) -> Result<Pin<Box<SimConnect<'a>>>>
    where
        F: FnMut(&mut SimConnect, SimConnectRecv) + 'a,
    {
        unsafe {
            let mut handle = 0;
            let name = std::ffi::CString::new(name).unwrap();
            map_err(sys::SimConnect_Open(
                &mut handle,
                name.as_ptr(),
                std::ptr::null_mut(),
                0,
                0,
                0,
            ))?;
            debug_assert!(handle != 0);
            let mut sim = Box::pin(SimConnect {
                handle,
                callback: Box::new(callback),
                data_definitions: HashMap::new(),
                client_data_definitions: HashMap::new(),
                event_id_counter: 0,
                client_data_id_counter: 0,
            });
            sim.call_dispatch()?;
            Ok(sim)
        }
    }

    /// Used to process the next SimConnect message received. Only needed when not using the gauge API.
    pub fn call_dispatch(&mut self) -> Result<()> {
        unsafe {
            map_err(sys::SimConnect_CallDispatch(
                self.handle,
                Some(dispatch_cb),
                self as *mut SimConnect as *mut std::ffi::c_void,
            ))
        }
    }

    fn get_define_id<T: DataDefinition>(&mut self) -> Result<sys::SIMCONNECT_DATA_DEFINITION_ID> {
        let handle = self.handle;
        SimConnect::get_id::<T, _, _>(
            &mut self.data_definitions,
            |define_id: sys::SIMCONNECT_DATA_DEFINITION_ID| {
                /*
                unsafe {
                    map_err(sys::SimConnect_ClearDataDefinition(handle, define_id))?;
                }
                */
                for (datum_name, units_type, epsilon, datatype) in T::DEFINITIONS {
                    let datum_name = std::ffi::CString::new(*datum_name).unwrap();
                    let units_type = std::ffi::CString::new(*units_type).unwrap();
                    unsafe {
                        map_err(sys::SimConnect_AddToDataDefinition(
                            handle,
                            define_id,
                            datum_name.as_ptr(),
                            units_type.as_ptr(),
                            *datatype,
                            *epsilon,
                            sys::SIMCONNECT_UNUSED,
                        ))?;
                    }
                }
                Ok(())
            },
        )
    }

    fn get_client_data_define_id<T: ClientDataDefinition>(
        &mut self,
    ) -> Result<sys::SIMCONNECT_CLIENT_DATA_DEFINITION_ID> {
        let handle = self.handle;
        SimConnect::get_id::<T, _, _>(&mut self.client_data_definitions, |define_id| {
            /*
            unsafe {
                map_err(sys::SimConnect_ClearClientDataDefinition(handle, define_id))?;
            }
            */

            // Rust may reorder fields, so padding has to be calculated as min of
            // all fields instead of the last field.
            let mut padding = std::usize::MAX;
            for (offset, size, epsilon) in T::get_definitions() {
                padding = padding.min(std::mem::size_of::<T>() - (offset + size));
                unsafe {
                    map_err(sys::SimConnect_AddToClientDataDefinition(
                        handle,
                        define_id,
                        offset as sys::DWORD,
                        size as sys::DWORD,
                        epsilon,
                        sys::SIMCONNECT_UNUSED,
                    ))?;
                }
            }
            if padding > 0 && padding != std::usize::MAX {
                unsafe {
                    map_err(sys::SimConnect_AddToClientDataDefinition(
                        handle,
                        define_id,
                        (std::mem::size_of::<T>() - padding) as sys::DWORD,
                        padding as sys::DWORD,
                        0.0,
                        sys::SIMCONNECT_UNUSED,
                    ))?;
                }
            }
            Ok(())
        })
    }

    fn get_id<T: 'static, U: std::convert::TryFrom<usize> + Copy, F: Fn(U) -> Result<()>>(
        map: &mut HashMap<TypeId, U>,
        insert_fn: F,
    ) -> Result<U> {
        let key = TypeId::of::<T>();
        let maybe_id = U::try_from(map.len()).unwrap_or_else(|_| unreachable!());
        match map.entry(key) {
            std::collections::hash_map::Entry::Vacant(entry) => {
                insert_fn(maybe_id)?;
                entry.insert(maybe_id);
                Ok(maybe_id)
            }
            std::collections::hash_map::Entry::Occupied(entry) => Ok(*entry.get()),
        }
    }

    /// Make changes to the data properties of an object.
    pub fn set_data_on_sim_object<T: DataDefinition>(
        &mut self,
        object_id: sys::SIMCONNECT_OBJECT_ID,
        data: &T,
    ) -> Result<()> {
        let define_id = self.get_define_id::<T>()?;
        unsafe {
            map_err(sys::SimConnect_SetDataOnSimObject(
                self.handle,
                define_id,
                object_id,
                0,
                0,
                std::mem::size_of_val(data) as sys::DWORD,
                data as *const T as *mut std::ffi::c_void,
            ))
        }
    }

    /// Retrieve information about simulation objects of a given type that are
    /// within a specified radius of the user's aircraft.
    pub fn request_data_on_sim_object_type<T: DataDefinition>(
        &mut self,
        request_id: sys::SIMCONNECT_DATA_REQUEST_ID,
        radius: sys::DWORD,
        r#type: sys::SIMCONNECT_SIMOBJECT_TYPE,
    ) -> Result<()> {
        let define_id = self.get_define_id::<T>()?;
        unsafe {
            map_err(sys::SimConnect_RequestDataOnSimObjectType(
                self.handle,
                request_id,
                define_id,
                radius,
                r#type,
            ))
        }
    }

    /// Request when the SimConnect client is to receive data values for a specific object
    pub fn request_data_on_sim_object<T: DataDefinition>(
        &mut self,
        request_id: sys::SIMCONNECT_DATA_REQUEST_ID,
        object_id: sys::SIMCONNECT_OBJECT_ID,
        period: Period,
    ) -> Result<()> {
        let define_id = self.get_define_id::<T>()?;

        unsafe {
            map_err(sys::SimConnect_RequestDataOnSimObject(
                self.handle,
                request_id,
                define_id,
                object_id,
                period as sys::SIMCONNECT_PERIOD,
                sys::SIMCONNECT_DATA_REQUEST_FLAG_CHANGED,
                0,
                0,
                0,
            ))
        }
    }

    /// Map a Prepar3D event to a specific ID. If `mask` is true, the sim itself
    /// will ignore the event, and only this SimConnect instance will receive it.
    pub fn map_client_event_to_sim_event(
        &mut self,
        event_name: &str,
        mask: bool,
    ) -> Result<sys::DWORD> {
        let event_id = self.event_id_counter;
        self.event_id_counter += 1;
        let event_name = std::ffi::CString::new(event_name).unwrap();

        unsafe {
            map_err(sys::SimConnect_MapClientEventToSimEvent(
                self.handle,
                event_id,
                event_name.as_ptr(),
            ))?;

            map_err(sys::SimConnect_AddClientEventToNotificationGroup(
                self.handle,
                0,
                event_id,
                mask.into(),
            ))?;

            map_err(sys::SimConnect_SetNotificationGroupPriority(
                self.handle,
                0,
                sys::SIMCONNECT_GROUP_PRIORITY_HIGHEST_MASKABLE,
            ))?;
        }
        Ok(event_id)
    }

    /// Trigger an event, previously mapped with `map_client_event_to_sim_event`
    pub fn transmit_client_event(
        &mut self,
        object_id: sys::SIMCONNECT_OBJECT_ID,
        event_id: sys::DWORD,
        data: sys::DWORD,
    ) -> Result<()> {
        unsafe {
            map_err(sys::SimConnect_TransmitClientEvent(
                self.handle,
                object_id,
                event_id,
                data,
                0,
                0,
            ))
        }
    }

    fn get_client_data_id(&mut self, name: &str) -> Result<sys::SIMCONNECT_CLIENT_DATA_ID> {
        let client_id = self.client_data_id_counter;
        self.client_data_id_counter += 1;
        let name = std::ffi::CString::new(name).unwrap();

        unsafe {
            map_err(sys::SimConnect_MapClientDataNameToID(
                self.handle,
                name.as_ptr(),
                client_id,
            ))?;
        }
        Ok(client_id)
    }

    /// Allocate a region of memory in the sim with the given `name`. Other
    /// SimConnect modules can use the `name` to read data from this memory
    /// using `request_client_data`. This memory cannot be deallocated.
    pub fn create_client_data<T: ClientDataDefinition>(
        &mut self,
        name: &str,
    ) -> Result<ClientDataArea<T>> {
        let client_id = self.get_client_data_id(name)?;
        unsafe {
            map_err(sys::SimConnect_CreateClientData(
                self.handle,
                client_id,
                std::mem::size_of::<T>() as sys::DWORD,
                0,
            ))?;
        }
        Ok(ClientDataArea {
            client_id,
            phantom: std::marker::PhantomData,
        })
    }

    /// Create a handle to a region of memory allocated by another module with
    /// the given `name`.
    pub fn get_client_area<T: ClientDataDefinition>(
        &mut self,
        name: &str,
    ) -> Result<ClientDataArea<T>> {
        let client_id = self.get_client_data_id(name)?;
        Ok(ClientDataArea {
            client_id,
            phantom: std::marker::PhantomData,
        })
    }

    /// Request a pre-allocated region of memory from the sim with the given
    /// `name`. A module must have already used `create_client_data` to
    /// allocate this memory.
    pub fn request_client_data<T: ClientDataDefinition>(
        &mut self,
        request_id: sys::SIMCONNECT_DATA_REQUEST_ID,
        name: &str,
    ) -> Result<()> {
        let define_id = self.get_client_data_define_id::<T>()?;
        let client_id = self.get_client_data_id(name)?;
        unsafe {
            map_err(sys::SimConnect_RequestClientData(
                self.handle,
                client_id,
                request_id,
                define_id,
                sys::SIMCONNECT_CLIENT_DATA_PERIOD_SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET,
                sys::SIMCONNECT_CLIENT_DATA_REQUEST_FLAG_CHANGED,
                0,
                0,
                0,
            ))?;
        }
        Ok(())
    }

    /// Set the data of an area acquired by `create_client_data` or
    /// `get_client_data`.
    pub fn set_client_data<T: ClientDataDefinition>(
        &mut self,
        area: &ClientDataArea<T>,
        data: &T,
    ) -> Result<()> {
        unsafe {
            map_err(sys::SimConnect_SetClientData(
                self.handle,
                area.client_id,
                self.get_client_data_define_id::<T>()?,
                0,
                0,
                std::mem::size_of::<T>() as sys::DWORD,
                data as *const _ as *mut std::ffi::c_void,
            ))?;
        }
        Ok(())
    }

    pub fn ai_create_non_atc_aircraft(
        &mut self,
        container_title: &str,
        tail_number: &str,
        init_position: sys::SIMCONNECT_DATA_INITPOSITION,
        request_id: sys::SIMCONNECT_DATA_REQUEST_ID,
    ) -> Result<()> {
        let container_title = std::ffi::CString::new(container_title).unwrap();
        let tail_number = std::ffi::CString::new(tail_number).unwrap();

        unsafe {
            map_err(sys::SimConnect_AICreateNonATCAircraft(
                self.handle,
                container_title.as_ptr(),
                tail_number.as_ptr(),
                init_position,
                request_id,
            ))?;
        }
        Ok(())
    }

    pub fn ai_create_parked_atc_aircraft(
        &mut self,
        container_title: &str,
        tail_number: &str,
        icao: &str,
        request_id: sys::SIMCONNECT_DATA_REQUEST_ID,
    ) -> Result<()> {
        let container_title = std::ffi::CString::new(container_title).unwrap();
        let tail_number = std::ffi::CString::new(tail_number).unwrap();
        let icao = std::ffi::CString::new(icao).unwrap();

        unsafe {
            map_err(sys::SimConnect_AICreateParkedATCAircraft(
                self.handle,
                container_title.as_ptr(),
                tail_number.as_ptr(),
                icao.as_ptr(),
                request_id,
            ))?;
        }
        Ok(())
    }

    pub fn ai_remove_object(
        &mut self,
        object_id: sys::SIMCONNECT_OBJECT_ID,
        request_id: sys::SIMCONNECT_DATA_REQUEST_ID,
    ) -> Result<()> {
        unsafe {
            map_err(sys::SimConnect_AIRemoveObject(
                self.handle,
                object_id,
                request_id,
            ))?;
        }
        Ok(())
    }

    pub fn subscribe_to_system_event(&mut self, system_event_name: &str) -> Result<sys::DWORD> {
        let event_id = self.event_id_counter;
        self.event_id_counter += 1;
        let system_event_name = std::ffi::CString::new(system_event_name).unwrap();

        unsafe {
            map_err(sys::SimConnect_SubscribeToSystemEvent(
                self.handle,
                event_id,
                system_event_name.as_ptr(),
            ))?;
        }
        Ok(event_id)
    }

    pub fn unsubscribe_from_system_event(
        &mut self,
        event_id: sys::SIMCONNECT_CLIENT_EVENT_ID,
    ) -> Result<()> {
        unsafe {
            map_err(sys::SimConnect_UnsubscribeFromSystemEvent(
                self.handle,
                event_id,
            ))?;
        }
        Ok(())
    }

    pub fn set_system_event_state(
        &mut self,
        event_id: sys::SIMCONNECT_CLIENT_EVENT_ID,
        on: bool,
    ) -> Result<()> {
        let state = on.into();
        unsafe {
            map_err(sys::SimConnect_SetSystemEventState(
                self.handle,
                event_id,
                state,
            ))?;
        }
        Ok(())
    }
}

impl<'a> Drop for SimConnect<'a> {
    fn drop(&mut self) {
        unsafe {
            map_err(sys::SimConnect_Close(self.handle)).expect("SimConnect_Close");
        }
    }
}

macro_rules! recv {
    ($V:ident) => {
        $V! {
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_EXCEPTION,
                SIMCONNECT_RECV_EXCEPTION,
                Exception
            ),
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_OPEN,
                SIMCONNECT_RECV_OPEN,
                Open
            ),
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_QUIT,
                SIMCONNECT_RECV_QUIT,
                Quit
            ),
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_EVENT,
                SIMCONNECT_RECV_EVENT,
                Event
            ),
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_SIMOBJECT_DATA,
                SIMCONNECT_RECV_SIMOBJECT_DATA,
                SimObjectData
            ),
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_CLIENT_DATA,
                SIMCONNECT_RECV_CLIENT_DATA,
                ClientData
            ),
            (
                SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_ASSIGNED_OBJECT_ID,
                SIMCONNECT_RECV_ASSIGNED_OBJECT_ID,
                AssignedObjectId
            ),
        }
    };
}

extern "C" fn dispatch_cb(
    recv: *mut sys::SIMCONNECT_RECV,
    _cb_data: sys::DWORD,
    p_context: *mut std::ffi::c_void,
) {
    macro_rules! recv_cb {
        ($( ($ID:ident, $T:ident, $E:ident), )*) => {
            unsafe {
                match (*recv).dwID as sys::SIMCONNECT_RECV_ID {
                    sys::SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_NULL => Some(SimConnectRecv::Null),
                    $(
                        sys::$ID => Some(SimConnectRecv::$E(&*(recv as *mut sys::$T))),
                    )*
                    sys::SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE => {
                        Some(SimConnectRecv::SimObjectData(&*(recv as *mut sys::SIMCONNECT_RECV_SIMOBJECT_DATA)))
                    }
                    _ => None,
                }
            }
        }
    }
    let recv = recv!(recv_cb);

    if let Some(recv) = recv {
        let sim = unsafe { &mut *(p_context as *mut SimConnect) };
        (sim.callback)(unsafe { &mut *(p_context as *mut SimConnect) }, recv);
    }
}

macro_rules! recv_enum {
    ($( ($ID:ident, $T:ident, $E:ident), )*) => {
        /// Message received from SimConnect.
        #[derive(Debug)]
        pub enum SimConnectRecv<'a> {
            Null,
            $(
                $E(&'a sys::$T),
            )*
        }
    }
}
recv!(recv_enum);

impl sys::SIMCONNECT_RECV_EVENT {
    /// The ID for this event.
    pub fn id(&self) -> sys::DWORD {
        self.uEventID
    }

    /// The data for this event.
    pub fn data(&self) -> sys::DWORD {
        self.dwData
    }
}

impl sys::SIMCONNECT_RECV_ASSIGNED_OBJECT_ID {
    pub fn id(&self) -> sys::DWORD {
        self.dwRequestID
    }

    pub fn object_id(&self) -> sys::DWORD {
        self.dwObjectID
    }
}

impl sys::SIMCONNECT_RECV_SIMOBJECT_DATA {
    /// The ID for this data.
    pub fn id(&self) -> sys::DWORD {
        self.dwRequestID
    }

    /// Convert a SimObjectData event into the data it contains.
    pub fn into<T: DataDefinition>(&self, sim: &SimConnect) -> Option<&T> {
        let define_id = sim.data_definitions[&TypeId::of::<T>()];
        if define_id == self.dwDefineID {
            // UB: creates unaligned reference
            Some(unsafe { &*(std::ptr::addr_of!(self.dwData) as *const T) })
        } else {
            None
        }
    }
}

impl sys::SIMCONNECT_RECV_CLIENT_DATA {
    /// The ID for this data.
    pub fn id(&self) -> sys::DWORD {
        self._base.dwRequestID
    }

    /// Convert a ClientData event into the data it contains.
    pub fn into<T: ClientDataDefinition>(&self, sim: &SimConnect) -> Option<&T> {
        let define_id = sim.client_data_definitions[&TypeId::of::<T>()];
        if define_id == self._base.dwDefineID {
            // UB: creates unaligned reference
            Some(unsafe { &*(std::ptr::addr_of!(self._base.dwData) as *const T) })
        } else {
            None
        }
    }
}

/// Specify how often data is to be sent to the client.
#[derive(Debug)]
pub enum Period {
    /// Specifies that the data is not to be sent
    Never = sys::SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_NEVER as isize,
    /// Specifies that the data should be sent once only. Note that this is not
    /// an efficient way of receiving data frequently, use one of the other
    /// periods if there is a regular frequency to the data request.
    Once = sys::SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_ONCE as isize,
    /// Specifies that the data should be sent every visual (rendered) frame.
    VisualFrame = sys::SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_VISUAL_FRAME as isize,
    /// Specifies that the data should be sent every simulated frame, whether that frame is
    /// rendered or not.
    SimFrame = sys::SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_SIM_FRAME as isize,
    /// Specifies that the data should be sent once every second.
    Second = sys::SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_SECOND as isize,
}

/// An allocated client data memory region. Dropping this struct will not
/// deallocate the memory which has been allocated in the sim.
pub struct ClientDataArea<T: ClientDataDefinition> {
    client_id: sys::SIMCONNECT_CLIENT_DATA_ID,
    phantom: std::marker::PhantomData<T>,
}