Skip to content

Commit

Permalink
feat: Support the extended version of simconnect events (#18)
Browse files Browse the repository at this point in the history
Support the extended version of Simconnect and Gauges events to have
more than only one argument per event.

---------

Co-authored-by: snek <[email protected]>
  • Loading branch information
Gurgel100 and devsnek authored Nov 24, 2024
1 parent b438d3e commit c24ffc0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
14 changes: 14 additions & 0 deletions msfs/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ pub fn trigger_key_event(event_id: sys::ID32, value: sys::UINT32) {
}
}

/// trigger_key_event_EX1
pub fn trigger_key_event_ex1(
event_id: sys::ID32,
value0: sys::UINT32,
value1: sys::UINT32,
value2: sys::UINT32,
value3: sys::UINT32,
value4: sys::UINT32,
) {
unsafe {
sys::trigger_key_event_EX1(event_id, value0, value1, value2, value3, value4);
}
}

#[doc(hidden)]
pub trait ExecuteCalculatorCodeImpl {
fn execute(code: &std::ffi::CStr) -> Option<Self>
Expand Down
50 changes: 48 additions & 2 deletions msfs/src/sim_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub trait ClientDataDefinition: 'static {
}

/// Rusty HRESULT wrapper.
#[allow(dead_code)]
#[derive(Debug)]
pub struct HResult(sys::HRESULT);
impl std::fmt::Display for HResult {
Expand Down Expand Up @@ -148,7 +149,7 @@ impl<'a> SimConnect<'a> {

// 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;
let mut padding = usize::MAX;
for (offset, size, epsilon) in T::get_definitions() {
padding = padding.min(std::mem::size_of::<T>() - (offset + size));
unsafe {
Expand All @@ -162,7 +163,7 @@ impl<'a> SimConnect<'a> {
))?;
}
}
if padding > 0 && padding != std::usize::MAX {
if padding > 0 && padding != usize::MAX {
unsafe {
map_err(sys::SimConnect_AddToClientDataDefinition(
handle,
Expand Down Expand Up @@ -311,6 +312,28 @@ impl<'a> SimConnect<'a> {
}
}

pub fn transmit_client_event_ex1(
&mut self,
object_id: sys::SIMCONNECT_OBJECT_ID,
event_id: sys::DWORD,
data: [sys::DWORD; 5],
) -> Result<()> {
unsafe {
map_err(sys::SimConnect_TransmitClientEvent_EX1(
self.handle,
object_id,
event_id,
0,
0,
data[0],
data[1],
data[2],
data[3],
data[4],
))
}
}

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;
Expand Down Expand Up @@ -544,6 +567,11 @@ macro_rules! recv {
SIMCONNECT_RECV_EVENT,
Event
),
(
SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_EVENT_EX1,
SIMCONNECT_RECV_EVENT_EX1,
EventEx1
),
(
SIMCONNECT_RECV_ID_SIMCONNECT_RECV_ID_SIMOBJECT_DATA,
SIMCONNECT_RECV_SIMOBJECT_DATA,
Expand Down Expand Up @@ -618,6 +646,24 @@ impl sys::SIMCONNECT_RECV_EVENT {
}
}

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

/// The data for this event.
pub fn data(&self) -> [sys::DWORD; 5] {
[
self.dwData0,
self.dwData1,
self.dwData2,
self.dwData3,
self.dwData4,
]
}
}

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

0 comments on commit c24ffc0

Please sign in to comment.