Files
simconnect-sdk-rs/simconnect-sdk/src/simconnect/events.rs
Mihai Dinculescu d89fa524c2 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.
2023-05-01 19:13:30 +01:00

136 lines
4.0 KiB
Rust

use crate::{
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 {
/// 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(),
event as u32,
event.into_c_char(),
)
})?;
success!(unsafe {
bindings::SimConnect_AddClientEventToNotificationGroup(
self.handle.as_ptr(),
NOTIFICATION_GROUP_ID,
event as u32,
0,
)
})?;
success!(unsafe {
bindings::SimConnect_SetNotificationGroupPriority(
self.handle.as_ptr(),
NOTIFICATION_GROUP_ID,
bindings::SIMCONNECT_GROUP_PRIORITY_HIGHEST,
)
})?;
Ok(())
}
/// 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::unsubscribe_from_client_event",
level = "debug",
skip(self)
)]
pub fn unsubscribe_from_client_event(
&mut self,
event: ClientEventRequest,
) -> Result<(), SimConnectError> {
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 any client event.
#[tracing::instrument(
name = "SimConnect::unsubscribe_from_all_client_events",
level = "debug",
skip(self)
)]
pub fn unsubscribe_from_all_client_events(&mut self) -> Result<(), SimConnectError> {
success!(unsafe {
bindings::SimConnect_ClearNotificationGroup(self.handle.as_ptr(), NOTIFICATION_GROUP_ID)
})?;
self.client_event_register.clear();
Ok(())
}
}