Skip to content

Commit

Permalink
Rework events and add proper support for client events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mihai-dinculescu committed May 1, 2023
1 parent 85e5341 commit d89fa52
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 141 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ file. This change log follows the conventions of

## [Unreleased]

### 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.

## [v0.2.2] - 2023-02-22

### Changed
Expand Down
76 changes: 38 additions & 38 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@

## General

| Feature | Status | Comment |
| --------------------------------------- | ------- | ------- |
| DispatchProc | | |
| SimConnect_Open | ✓ | |
| SimConnect_Close | ✓ | |
| SimConnect_CallDispatch | | |
| SimConnect_GetNextDispatch | ✓ | |
| SimConnect_RequestSystemState | | |
| SimConnect_MapClientEventToSimEvent | - | WIP |
| SimConnect_SubscribeToSystemEvent | ✓ | |
| SimConnect_SetSystemEventState | | |
| SimConnect_UnsubscribeFromSystemEvent | ✓ | |
| SimConnect_SetNotificationGroupPriority | - | WIP |
| Feature | Status | Comment |
| --------------------------------------- | ------- | -------------------------------------------- |
| DispatchProc | | |
| SimConnect_Open | ✓ | |
| SimConnect_Close | ✓ | |
| SimConnect_CallDispatch | | |
| SimConnect_GetNextDispatch | ✓ | |
| SimConnect_RequestSystemState | | |
| SimConnect_MapClientEventToSimEvent | ✓ | Encapsulated by `subscribe_to_client_event`. |
| SimConnect_SubscribeToSystemEvent | ✓ | |
| SimConnect_SetSystemEventState | | |
| SimConnect_UnsubscribeFromSystemEvent | ✓ | |
| SimConnect_SetNotificationGroupPriority | ✓ | Encapsulated by `subscribe_to_client_event`. |

## Events And Data

| Feature | Status | Comment |
| -------------------------------------------- | ------- | ----------------------------------- |
| SimConnect_RequestDataOnSimObject | ✓ | Only for SIMCONNECT_OBJECT_ID_USER |
| SimConnect_RequestDataOnSimObjectType | | |
| SimConnect_AddClientEventToNotificationGroup | - | WIP |
| SimConnect_RemoveClientEvent | | |
| SimConnect_TransmitClientEvent | | |
| SimConnect_TransmitClientEvent_EX1 | | |
| SimConnect_MapClientDataNameToID | | |
| SimConnect_RequestClientData | | |
| SimConnect_CreateClientData | | |
| SimConnect_AddToClientDataDefinition | | |
| SimConnect_AddToDataDefinition | ✓ | Supports `f64`, `bool` and `String` |
| SimConnect_SetClientData | | |
| SimConnect_SetDataOnSimObject | | |
| SimConnect_ClearClientDataDefinition | | |
| SimConnect_ClearDataDefinition | ✓ | |
| SimConnect_MapInputEventToClientEvent | | |
| SimConnect_RequestNotificationGroup | | |
| SimConnect_ClearInputGroup | | |
| SimConnect_ClearNotificationGroup | | |
| SimConnect_RequestReservedKey | | |
| SimConnect_SetInputGroupPriority | | |
| SimConnect_SetInputGroupState | | |
| SimConnect_RemoveInputEvent | | |
| Feature | Status | Comment |
| -------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------- |
| SimConnect_RequestDataOnSimObject | ✓ | Only for `SIMCONNECT_OBJECT_ID_USER`. |
| SimConnect_RequestDataOnSimObjectType | | |
| SimConnect_AddClientEventToNotificationGroup | ✓ | Encapsulated by `subscribe_to_client_event`. |
| SimConnect_RemoveClientEvent | ✓ | |
| SimConnect_TransmitClientEvent | | |
| SimConnect_TransmitClientEvent_EX1 | | |
| SimConnect_MapClientDataNameToID | | |
| SimConnect_RequestClientData | | |
| SimConnect_CreateClientData | | |
| SimConnect_AddToClientDataDefinition | | |
| SimConnect_AddToDataDefinition | ✓ | Encapsulated by `register_object` and the `simconnect` macro. Supports `f64`, `bool` and `String`. |
| SimConnect_SetClientData | | |
| SimConnect_SetDataOnSimObject | | |
| SimConnect_ClearClientDataDefinition | | |
| SimConnect_ClearDataDefinition | ✓ | |
| SimConnect_MapInputEventToClientEvent | | |
| SimConnect_RequestNotificationGroup | | |
| SimConnect_ClearInputGroup | | |
| SimConnect_ClearNotificationGroup | ✓ | Implemented by `unsubscribe_from_all_client_events`. |
| SimConnect_RequestReservedKey | | |
| SimConnect_SetInputGroupPriority | | |
| SimConnect_SetInputGroupState | | |
| SimConnect_RemoveInputEvent | | |

## AI Objects

Expand Down
8 changes: 6 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ name = "facilities"
path = "src/facilities.rs"

[[bin]]
name = "system_events"
path = "src/system_events.rs"
name = "subscribe_to_client_events"
path = "src/subscribe_to_client_events.rs"

[[bin]]
name = "subscribe_to_system_events"
path = "src/subscribe_to_system_events.rs"

[dependencies]
tracing = "0.1"
Expand Down
10 changes: 8 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ cargo run --bin data_multiple_objects
cargo run --bin facilities
```

## Receiving system events
## Subscribe to client events

```bash
cargo run --bin system_events
cargo run --bin subscribe_to_client_events
```

## Subscribe to system events

```bash
cargo run --bin subscribe_to_system_events
```
62 changes: 62 additions & 0 deletions examples/src/subscribe_to_client_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use simconnect_sdk::{ClientEvent, ClientEventRequest, Notification, SimConnect};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Client Events example");

let mut throttle_events_received = 0;
let mut elevator_events_received = 0;

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 subscribe to the client events we're interested in
client.subscribe_to_client_event(ClientEventRequest::Throttle1Set)?;
client.subscribe_to_client_event(ClientEventRequest::AxisElevatorSet)?;
}
Some(Notification::ClientEvent(event)) => match event {
ClientEvent::Throttle1Set { value } => {
println!("Throttle1Set: {value}");

throttle_events_received += 1;
if throttle_events_received >= 9 {
// We unsubscribe from the client event after we receive 10 of them
// This might run multiple times if there are more events queued up
println!("Unsubscribing from Throttle1Set...");
client
.unsubscribe_from_client_event(ClientEventRequest::Throttle1Set)?;
}
}
ClientEvent::AxisElevatorSet { value } => {
println!("AxisElevatorSet: {value}");

elevator_events_received += 1;
if elevator_events_received >= 9 {
// We unsubscribe from the client event after we receive 10 of them
// This might run multiple times if there are more events queued up
println!("Unsubscribing from AxisElevatorSet...");
client.unsubscribe_from_client_event(
ClientEventRequest::AxisElevatorSet,
)?;
}
}
_ => {}
},
_ => (),
}

// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
Err(e) => {
println!("Error: {e:?}")
}
}

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simconnect_sdk::{Notification, SimConnect, SystemEvent, SystemEventRequest};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("System events example");
let client = SimConnect::new("System Events example");

match client {
Ok(mut client) => loop {
Expand Down
7 changes: 7 additions & 0 deletions simconnect-sdk/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ fn main() {
.allowlist_function("SimConnect_AddToDataDefinition")
.allowlist_function("SimConnect_CallDispatch")
.allowlist_function("SimConnect_ClearDataDefinition")
.allowlist_function("SimConnect_ClearNotificationGroup")
.allowlist_function("SimConnect_Close")
.allowlist_function("SimConnect_GetNextDispatch")
.allowlist_function("SimConnect_MapClientEventToSimEvent")
.allowlist_function("SimConnect_Open")
.allowlist_function("SimConnect_RemoveClientEvent")
.allowlist_function("SimConnect_RequestDataOnSimObject")
.allowlist_function("SimConnect_RequestFacilitiesList")
.allowlist_function("SimConnect_SetNotificationGroupPriority")
Expand All @@ -52,6 +54,11 @@ fn main() {
.allowlist_type("SIMCONNECT_RECV_WAYPOINT_LIST")
.allowlist_type("SIMCONNECT_RECV")
.allowlist_var("SIMCONNECT_DATA_REQUEST_FLAG_CHANGED")
.allowlist_var("SIMCONNECT_GROUP_PRIORITY_DEFAULT")
.allowlist_var("SIMCONNECT_GROUP_PRIORITY_HIGHEST_MASKABLE")
.allowlist_var("SIMCONNECT_GROUP_PRIORITY_HIGHEST")
.allowlist_var("SIMCONNECT_GROUP_PRIORITY_LOWEST")
.allowlist_var("SIMCONNECT_GROUP_PRIORITY_STANDARD")
.allowlist_var("SIMCONNECT_OBJECT_ID_USER")
.allowlist_var("SIMCONNECT_RECV_ID_VOR_LIST_HAS_DME")
.allowlist_var("SIMCONNECT_RECV_ID_VOR_LIST_HAS_GLIDE_SLOPE")
Expand Down
Loading

0 comments on commit d89fa52

Please sign in to comment.