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:
76
examples/src/subscribe_to_system_events.rs
Normal file
76
examples/src/subscribe_to_system_events.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use simconnect_sdk::{Notification, SimConnect, SystemEvent, SystemEventRequest};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = SimConnect::new("System Events example");
|
||||
|
||||
match client {
|
||||
Ok(mut client) => loop {
|
||||
let notification = client.get_next_dispatch()?;
|
||||
|
||||
match notification {
|
||||
Some(Notification::Open) => {
|
||||
println!("Connection opened.");
|
||||
|
||||
// After the connection is successfully open
|
||||
// We request the system events we're interested in
|
||||
client.subscribe_to_system_event(SystemEventRequest::FourSeconds)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::AircraftLoaded)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::Crashed)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::FlightLoaded)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::FlightPlanActivated)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::FlightPlanDeactivated)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::Pause)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::Sim)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::Sound)?;
|
||||
client.subscribe_to_system_event(SystemEventRequest::View)?;
|
||||
}
|
||||
Some(Notification::SystemEvent(event)) => match event {
|
||||
SystemEvent::FourSeconds => {
|
||||
println!("FourSeconds ping received.");
|
||||
|
||||
// After we have receive one FourSeconds event notification, we unsubscribe from it
|
||||
client.unsubscribe_from_system_event(SystemEventRequest::FourSeconds)?;
|
||||
println!("FourSeconds subscription stopped.");
|
||||
}
|
||||
SystemEvent::AircraftLoaded { file_name } => {
|
||||
println!("AircraftLoaded: {file_name}.");
|
||||
}
|
||||
SystemEvent::Crashed => {
|
||||
println!("Crashed.");
|
||||
}
|
||||
SystemEvent::FlightLoaded { file_name } => {
|
||||
println!("FlightLoaded: {file_name}.");
|
||||
}
|
||||
SystemEvent::FlightPlanActivated { file_name } => {
|
||||
println!("FlightPlanActivated: {file_name}.");
|
||||
}
|
||||
SystemEvent::FlightPlanDeactivated => {
|
||||
println!("FlightPlanDeactivated.");
|
||||
}
|
||||
SystemEvent::Pause { state } => {
|
||||
println!("Pause: {state}.");
|
||||
}
|
||||
SystemEvent::Sim { state } => {
|
||||
println!("Sim: {state}.");
|
||||
}
|
||||
SystemEvent::Sound { state } => {
|
||||
println!("Sound: {state}.");
|
||||
}
|
||||
SystemEvent::View { view } => {
|
||||
println!("View: {view:?}.");
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// sleep for about a frame to reduce CPU usage
|
||||
std::thread::sleep(std::time::Duration::from_millis(16));
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Error: {e:?}")
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user