Rework events and add proper support for client events
Added - Client events are now implemented through `SimConnect::subscribe_to_client_event`, `SimConnect::unsubscribe_from_client_event` and `SimConnect::unsubscribe_from_all_client_events`. - `subscribe_to_client_events.rs` example has been added. - `SimConnectError::EventAlreadySubscribedTo` and `SimConnectError::EventNotSubscribedTo` error variants have been added. Changed - A second call to `SimConnect::subscribe_to_system_event` for the same event will now return an error of type `SimConnectError::EventAlreadySubscribedTo` instead of `SimConnectError::SimConnectException`. - The call to `SimConnect::unsubscribe_from_system_event` is now a NOOP when the system event is not subscribed to. - `SimConnectError::UnimplementedMessageType` has been renamed to `SimConnectError::UnimplementedNotification`. Removed - `SimConnect::register_event` has been replaced by the new client event functions. - `NotificationGroup` has been removed in favor of an internally managed notification group.
This commit is contained in:
@@ -1,18 +1,68 @@
|
||||
use crate::{
|
||||
bindings, success, ClientEvent, NotificationGroup, SimConnect, SimConnectError,
|
||||
SystemEventRequest,
|
||||
bindings, success, ClientEventRequest, SimConnect, SimConnectError, SystemEventRequest,
|
||||
};
|
||||
|
||||
// In order to simplify the usage we're using a single notification group for all client events.
|
||||
const NOTIFICATION_GROUP_ID: u32 = 0;
|
||||
|
||||
impl SimConnect {
|
||||
/// Associates a client defined event with a Microsoft Flight Simulator event name.
|
||||
///
|
||||
/// WIP
|
||||
#[tracing::instrument(name = "SimConnect::register_event", level = "debug", skip(self))]
|
||||
pub fn register_event(
|
||||
&self,
|
||||
event: ClientEvent,
|
||||
notification_group: NotificationGroup,
|
||||
/// Request that a specific system event is notified.
|
||||
#[tracing::instrument(
|
||||
name = "SimConnect::subscribe_to_system_event",
|
||||
level = "debug",
|
||||
skip(self)
|
||||
)]
|
||||
pub fn subscribe_to_system_event(
|
||||
&mut self,
|
||||
event: SystemEventRequest,
|
||||
) -> Result<(), SimConnectError> {
|
||||
self.system_event_register.register(event)?;
|
||||
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_SubscribeToSystemEvent(
|
||||
self.handle.as_ptr(),
|
||||
event as u32,
|
||||
event.into_c_char(),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Request that notifications are no longer received for the specified system event.
|
||||
/// If the system event is not subscribed to, this function does nothing.
|
||||
#[tracing::instrument(
|
||||
name = "SimConnect::unsubscribe_from_system_event",
|
||||
level = "debug",
|
||||
skip(self)
|
||||
)]
|
||||
pub fn unsubscribe_from_system_event(
|
||||
&mut self,
|
||||
event: SystemEventRequest,
|
||||
) -> Result<(), SimConnectError> {
|
||||
if self.system_event_register.is_registered(event) {
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_UnsubscribeFromSystemEvent(self.handle.as_ptr(), event as u32)
|
||||
})?;
|
||||
|
||||
self.system_event_register.clear();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Request that a specific client event is notified.
|
||||
#[tracing::instrument(
|
||||
name = "SimConnect::subscribe_to_client_event",
|
||||
level = "debug",
|
||||
skip(self)
|
||||
)]
|
||||
pub fn subscribe_to_client_event(
|
||||
&mut self,
|
||||
event: ClientEventRequest,
|
||||
) -> Result<(), SimConnectError> {
|
||||
self.client_event_register.register(event)?;
|
||||
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_MapClientEventToSimEvent(
|
||||
self.handle.as_ptr(),
|
||||
@@ -24,7 +74,7 @@ impl SimConnect {
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_AddClientEventToNotificationGroup(
|
||||
self.handle.as_ptr(),
|
||||
notification_group as u32,
|
||||
NOTIFICATION_GROUP_ID,
|
||||
event as u32,
|
||||
0,
|
||||
)
|
||||
@@ -33,43 +83,53 @@ impl SimConnect {
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_SetNotificationGroupPriority(
|
||||
self.handle.as_ptr(),
|
||||
notification_group as u32,
|
||||
1,
|
||||
NOTIFICATION_GROUP_ID,
|
||||
bindings::SIMCONNECT_GROUP_PRIORITY_HIGHEST,
|
||||
)
|
||||
})
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Request that a specific system event is notified to the client.
|
||||
/// Request that notifications are no longer received for the specified client event.
|
||||
/// If the client event is not subscribed to, this function does nothing.
|
||||
#[tracing::instrument(
|
||||
name = "SimConnect::subscribe_to_system_event",
|
||||
name = "SimConnect::unsubscribe_from_client_event",
|
||||
level = "debug",
|
||||
skip(self)
|
||||
)]
|
||||
pub fn subscribe_to_system_event(
|
||||
pub fn unsubscribe_from_client_event(
|
||||
&mut self,
|
||||
event: SystemEventRequest,
|
||||
event: ClientEventRequest,
|
||||
) -> Result<(), SimConnectError> {
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_SubscribeToSystemEvent(
|
||||
self.handle.as_ptr(),
|
||||
event as u32,
|
||||
event.into_c_char(),
|
||||
)
|
||||
})
|
||||
if self.client_event_register.is_registered(event) {
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_RemoveClientEvent(
|
||||
self.handle.as_ptr(),
|
||||
NOTIFICATION_GROUP_ID,
|
||||
event as u32,
|
||||
)
|
||||
})?;
|
||||
|
||||
self.client_event_register.unregister(event)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Request that notifications are no longer received for the specified system event.
|
||||
/// Request that notifications are no longer received for any client event.
|
||||
#[tracing::instrument(
|
||||
name = "SimConnect::unsubscribe_from_system_event",
|
||||
name = "SimConnect::unsubscribe_from_all_client_events",
|
||||
level = "debug",
|
||||
skip(self)
|
||||
)]
|
||||
pub fn unsubscribe_from_system_event(
|
||||
&mut self,
|
||||
event: SystemEventRequest,
|
||||
) -> Result<(), SimConnectError> {
|
||||
pub fn unsubscribe_from_all_client_events(&mut self) -> Result<(), SimConnectError> {
|
||||
success!(unsafe {
|
||||
bindings::SimConnect_UnsubscribeFromSystemEvent(self.handle.as_ptr(), event as u32)
|
||||
})
|
||||
bindings::SimConnect_ClearNotificationGroup(self.handle.as_ptr(), NOTIFICATION_GROUP_ID)
|
||||
})?;
|
||||
|
||||
self.client_event_register.clear();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user