Skip to content

Commit

Permalink
Added System Event mapping, completed ClientEvent and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkopp committed Mar 3, 2023
1 parent 2d8856a commit 959fc99
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 70 deletions.
90 changes: 49 additions & 41 deletions fbw-a32nx/src/wasm/extra-backend/src/Example/ExampleModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ bool ExampleModule::initialize() {
<< std::endl;
std::cout << std::endl;
});
// if (!metaDataPtr->requestPeriodicDataFromSim(SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET)) {
// LOG_ERROR("Failed to request periodic data from sim");
// }
if (!metaDataPtr->requestPeriodicDataFromSim(SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET)) {
LOG_ERROR("Failed to request periodic data from sim");
}

// ClientDataBufferedAreaVariable test
hugeClientDataPtr = dataManager->make_clientdatabufferedarea_var<BYTE, SIMCONNECT_CLIENTDATA_MAX_SIZE>("HUGE CLIENT DATA");
Expand All @@ -176,9 +176,9 @@ bool ExampleModule::initialize() {
<< "]" << std::endl;
std::cout << std::endl;
});
// if (!SUCCEEDED(hugeClientDataPtr->requestPeriodicDataFromSim(SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET))) {
// LOG_ERROR("Failed to request periodic data from sim");
// }
if (!SUCCEEDED(hugeClientDataPtr->requestPeriodicDataFromSim(SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET))) {
LOG_ERROR("Failed to request periodic data from sim");
}

// Key event tests
// Test callback a member method using this->
Expand All @@ -197,10 +197,9 @@ bool ExampleModule::initialize() {
// ======================
// Client Event tests

// Simple client event - no mappings
// Simple custom client event - no mappings
clientEventPtr = dataManager->make_client_event("A32NX.MY_CUSTOM_EVENT");
clientEventPtr->addClientEventToNotificationGroup(NOTIFICATION_GROUP_1);

clientEventCallbackId = clientEventPtr->addCallback(
[&, this](const int number, const DWORD param0, const DWORD param1, const DWORD param2, const DWORD param3, const DWORD param4) {
std::cout << "--- CALLBACK: A32NX.MY_CUSTOM_EVENT" << std::endl;
Expand All @@ -210,11 +209,26 @@ bool ExampleModule::initialize() {
<< " param3 = " << param3 << " param4 = " << param4 << std::endl;
std::cout << std::endl;
});
clientEventPtr->mapInputDownEvent("VK_COMMA", INPUT_GROUP_1);
clientEventPtr->mapInputUpEvent("VK_COMMA", INPUT_GROUP_1);
clientEventPtr->mapInputDownEvent("joystick:1:button:7", INPUT_GROUP_1);
clientEventPtr->mapInputUpEvent("joystick:1:button:7", INPUT_GROUP_1);
clientEventPtr->setInputGroupState(0, SIMCONNECT_STATE_ON);
clientEventPtr->mapInputDownUpEvent("VK_COMMA", INPUT_GROUP_1);
clientEventPtr->mapInputDownUpEvent("joystick:1:button:7", INPUT_GROUP_1);
clientEventPtr->setInputGroupState(INPUT_GROUP_1, SIMCONNECT_STATE_ON);

// System Events
// Create the client event with the registerToSim flag set to false, so we can add
// client event to system event. When this is set to true (default) the client event
// will be registered to the sim either as a custom event or a mapped event (if the event name exists) and an
// error will be thrown if you try to register the system event to the sim.
systemEventPtr = dataManager->make_client_event("A32NX.SYSTEM_EVENT_VIEW", false);
systemEventCallbackId = systemEventPtr->addCallback(
[&](const int number, const DWORD param0, const DWORD param1, const DWORD param2, const DWORD param3, const DWORD param4) {
std::cout << "--- CALLBACK: A32NX.SYSTEM_EVENT_VIEW" << std::endl;
std::cout << systemEventPtr->str() << std::endl;
std::cout << "A32NX.SYSTEM_EVENT_VIEW"
<< " number = " << number << " param0 = " << param0 << " param1 = " << param1 << " param2 = " << param2
<< " param3 = " << param3 << " param4 = " << param4 << std::endl;
std::cout << std::endl;
});
systemEventPtr->subscribeToSimSystemEvent("View");

isInitialized = true;
LOG_INFO("ExampleModule initialized");
Expand Down Expand Up @@ -262,33 +276,32 @@ bool ExampleModule::update([[maybe_unused]] sGaugeDrawData* pData) {
[[maybe_unused]] const FLOAT64 timeStamp = msfsHandler.getTimeStamp();
[[maybe_unused]] const UINT64 tickCounter = msfsHandler.getTickCounter();

std::cout << "==== tickCounter = " << tickCounter << " timeStamp = " << timeStamp << " ==================================" << std::endl;
std::cout << "==== tickCounter = " << tickCounter << " timeStamp = " << timeStamp << " =============================" << std::endl;

// ======================
// Client Event Tests

if (tickCounter % 2000 == 1000) {
clientEventPtr->removeCallback(clientEventCallbackId);
clientEventPtr->removeClientEventFromNotificationGroup(NOTIFICATION_GROUP_1);
}
if (tickCounter % 2000 == 0) {
clientEventPtr->mapToSimEvent();
clientEventPtr->addClientEventToNotificationGroup(NOTIFICATION_GROUP_1);
clientEventCallbackId = clientEventPtr->addCallback(
[&, this](const int number, const DWORD param0, const DWORD param1, const DWORD param2, const DWORD param3, const DWORD param4) {
std::cout << "--- CALLBACK: A32NX.MY_CUSTOM_EVENT" << std::endl;
std::cout << clientEventPtr->str() << std::endl;
std::cout << "CUSTOM_EVENT "
<< " number = " << number << " param0 = " << param0 << " param1 = " << param1 << " param2 = " << param2
<< " param3 = " << param3 << " param4 = " << param4 << std::endl;
std::cout << std::endl;
});
// clientEventPtr->mapInputEvent(0, "VK_COMMA", clientEventPtr);
// clientEventPtr->mapInputEvent(0, "joystick:1:button:7", clientEventPtr);
// clientEventPtr->mapInputEvent(0, "joystick:1:button:8", clientEventPtr);
}
if (tickCounter == 4000) {
}
/* if (tickCounter % 2000 == 1000) {
clientEventPtr->unmapInputEvent("VK_COMMA", INPUT_GROUP_1);
clientEventPtr->unmapInputEvent("joystick:1:button:7", INPUT_GROUP_1);
clientEventPtr->removeCallback(clientEventCallbackId);
clientEventPtr->removeClientEventFromNotificationGroup(NOTIFICATION_GROUP_1);
}
if (tickCounter % 2000 == 0) {
clientEventPtr->mapToSimEvent();
clientEventPtr->addClientEventToNotificationGroup(NOTIFICATION_GROUP_1);
clientEventCallbackId = clientEventPtr->addCallback(
[&, this](const int number, const DWORD param0, const DWORD param1, const DWORD param2, const DWORD param3, const DWORD
param4) { std::cout << "--- CALLBACK: A32NX.MY_CUSTOM_EVENT" << std::endl; std::cout << clientEventPtr->str() << std::endl; std::cout
<< "CUSTOM_EVENT "
<< " number = " << number << " param0 = " << param0 << " param1 = " << param1 << " param2 = " << param2
<< " param3 = " << param3 << " param4 = " << param4 << std::endl;
std::cout << std::endl;
});
clientEventPtr->mapInputDownUpEvent("VK_COMMA", INPUT_GROUP_1);
clientEventPtr->mapInputDownUpEvent("joystick:1:button:7", INPUT_GROUP_1);
clientEventPtr->setInputGroupState(INPUT_GROUP_1, SIMCONNECT_STATE_ON);
}*/
// clientEventPtr->trigger(999);

// difference if using different units
Expand Down Expand Up @@ -478,9 +491,4 @@ bool ExampleModule::shutdown() {
return true;
}

void ExampleModule::keyEventTest(DWORD param0, DWORD param1, DWORD param2, DWORD param3, DWORD param4) {
std::cout << "ExampleModule::keyEventTest() - param0 = " << param0 << " param1 = " << param1 << " param2 = " << param2
<< " param3 = " << param3 << " param4 = " << param4 << std::endl;
}

#endif
10 changes: 8 additions & 2 deletions fbw-a32nx/src/wasm/extra-backend/src/Example/ExampleModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class ExampleModule : public Module {
ClientEventPtr clientEventPtr;
[[maybe_unused]] CallbackID clientEventCallbackId{};

// System Events
ClientEventPtr systemEventPtr;
[[maybe_unused]] CallbackID systemEventCallbackId{};

public:
ExampleModule() = delete;

Expand All @@ -123,9 +127,11 @@ class ExampleModule : public Module {
bool shutdown() override;

private:

// key event test function
void keyEventTest(DWORD param0, DWORD param1, DWORD param2, DWORD param3, DWORD param4);
void keyEventTest(DWORD param0, DWORD param1, DWORD param2, DWORD param3, DWORD param4) {
std::cout << "ExampleModule::keyEventTest() - param0 = " << param0 << " param1 = " << param1 << " param2 = " << param2
<< " param3 = " << param3 << " param4 = " << param4 << std::endl;
}

// Fowler-Noll-Vo hash function
uint64_t fingerPrintFVN(std::vector<BYTE>& data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ ClientEventPtr DataManager::make_client_event(const std::string& clientEventName
}
// create a new event instance
ClientEventPtr clientEvent =
std::shared_ptr<ClientEvent>(new ClientEvent(hSimConnect, clientEventIDGen.getNextId(), std::move(clientEventName), registerToSim));
std::shared_ptr<ClientEvent>(new ClientEvent(hSimConnect, clientEventIDGen.getNextId(), std::move(clientEventName)));
if (registerToSim) {
clientEvent->mapToSimEvent();
}
clientEvents[clientEvent->getClientEventId()] = clientEvent;
if (notificationGroupId != SIMCONNECT_UNUSED) {
clientEvent->addClientEventToNotificationGroup(notificationGroupId);
Expand Down
8 changes: 5 additions & 3 deletions fbw-common/src/wasm/extra-backend/MsfsHandler/DataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,14 @@ class DataManager {
* Flight Simulator events include periods. If no entry is made for this
* parameter, the event is private to the client.
* @param registerToSim Flag to indicate if the event should be registered to the sim immediately.
* This is required to be false for example when using SimConnect_SubscribeToSystemEvent
* as this function does the registering itself (default=true).
* A custom event will be registered and a sim event will be mapped if a
* sim event with this name exists. Otherwise SimConnect will throw an error.
* This must be false when it is intended to map the client event
* to a system event with ClientEvent::subscribeToSystemEvent() afterwards.
* @param notificationGroupId Specifies the notification group to which the event is added. If no
* entry is made for this parameter, the event is not added to a
* notification group.
* @return A shared pointer to the CLientEvent
* @return A shared pointer to the ClientEvent
*/
ClientEventPtr make_client_event(const std::string& clientEventName,
bool registerToSim = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
#include "ClientEvent.h"
#include "logging.h"

ClientEvent::ClientEvent(HANDLE hSimConnect,
SIMCONNECT_CLIENT_EVENT_ID clientEventId,
const std::string& clientEventName,
bool registerToSim)
: hSimConnect(hSimConnect), clientEventId(clientEventId), clientEventName(std::move(clientEventName)) {
if (registerToSim) {
mapToSimEvent();
}
}
ClientEvent::ClientEvent(HANDLE hSimConnect, SIMCONNECT_CLIENT_EVENT_ID clientEventId, const std::string& clientEventName)
: hSimConnect(hSimConnect), clientEventId(clientEventId), clientEventName(std::move(clientEventName)) {}

ClientEvent::~ClientEvent() {
callbacks.clear();
}

// =================================================================================================
// Registration / Mapping of the Client Event to Sim Events
// =================================================================================================

void ClientEvent::mapToSimEvent() {
if (!SUCCEEDED(SimConnect_MapClientEventToSimEvent(hSimConnect, clientEventId, clientEventName.c_str()))) {
LOG_ERROR("Failed to map event client ID " + std::to_string(clientEventId) + " to sim event " + clientEventName);
Expand All @@ -30,6 +27,38 @@ void ClientEvent::mapToSimEvent() {
isRegisteredToSim = true;
}

void ClientEvent::subscribeToSimSystemEvent(const std::string& eventName) {
if (!SUCCEEDED(SimConnect_SubscribeToSystemEvent(hSimConnect, getClientEventId(), eventName.c_str()))) {
LOG_ERROR("Failed to map client event " + clientEventName + " with client ID " + std::to_string(clientEventId) +
" to sim system event " + eventName);
return;
}
LOG_DEBUG("Mapped client event " + clientEventName + " with client ID " + std::to_string(clientEventId) + " to sim system event " +
eventName);
isRegisteredToSim = true;
}

void ClientEvent::unsubscribeFromSimSystemEvent() {
if (!SUCCEEDED(SimConnect_UnsubscribeFromSystemEvent(hSimConnect, getClientEventId()))) {
LOG_ERROR("Failed to unsubscribe client event " + clientEventName + " with client ID " + std::to_string(clientEventId) +
" from sim system event: " + clientEventName);
return;
}
LOG_DEBUG("Unsubscribed client event " + clientEventName + " with client ID " + std::to_string(clientEventId) +
" from sim system event: " + clientEventName);
isRegisteredToSim = false;
}

void ClientEvent::setSystemEventState(SIMCONNECT_STATE state) {
if (!SUCCEEDED(SimConnect_SetSystemEventState(hSimConnect, getClientEventId(), state))) {
LOG_ERROR("Failed to set system event state " + std::to_string(state) + " for client event " + clientEventName + " with client ID " +
std::to_string(clientEventId));
return;
}
LOG_DEBUG("Set system event state " + std::to_string(state) + " for client event " + clientEventName + " with client ID " +
std::to_string(clientEventId));
}

// =================================================================================================
// Triggering Events
// =================================================================================================
Expand Down
Loading

0 comments on commit 959fc99

Please sign in to comment.