From 1388d95ecf71d517d24ccb6713641220a5553e4a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 6 Jul 2015 09:09:18 +0200 Subject: simconnect: use event handle without calldispatch in a loop We aren't supposed to calldispatch in a loop, since it locks up FSX. Use calldispatch only if FSX signals us that an event has been received. This might fix slowness reported in #174. --- CMakeLists.txt | 2 +- ftnoir_protocol_sc/ftnoir_protocol_sc.cpp | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 448375f3..c45163dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,7 +304,7 @@ endif() if(SDK_SIMCONNECT) opentrack_library(opentrack-proto-simconnect ftnoir_protocol_sc) - target_link_libraries(opentrack-proto-simconnect ${SDK_SIMCONNECT}/lib/SimConnect.lib winmm) + target_link_libraries(opentrack-proto-simconnect ${SDK_SIMCONNECT}/lib/SimConnect.lib) include_directories(opentrack-proto-simconnect SYSTEM PUBLIC ${SDK_SIMCONNECT}/inc) endif() diff --git a/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp b/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp index c0e2fc02..5f70b093 100644 --- a/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp +++ b/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp @@ -41,19 +41,27 @@ FTNoIR_Protocol::~FTNoIR_Protocol() void FTNoIR_Protocol::run() { - (void) timeBeginPeriod(1); + HANDLE event = CreateEvent(NULL, FALSE, FALSE, nullptr); + + if (event == nullptr) + { + qDebug() << "simconnect: event create" << GetLastError(); + return; + } while (!should_stop) { - if (SUCCEEDED(simconnect_open(&hSimConnect, "opentrack", NULL, 0, 0, 0))) + if (SUCCEEDED(simconnect_open(&hSimConnect, "opentrack", NULL, 0, event, 0))) { simconnect_subscribetosystemevent(hSimConnect, 0, "Frame"); while (!should_stop) { - if (FAILED(simconnect_calldispatch(hSimConnect, processNextSimconnectEvent, reinterpret_cast(this)))) - break; - Sleep(16); + if (WaitForSingleObject(event, 10) == WAIT_OBJECT_0) + { + if (FAILED(simconnect_calldispatch(hSimConnect, processNextSimconnectEvent, reinterpret_cast(this)))) + break; + } } (void) simconnect_close(hSimConnect); @@ -63,7 +71,7 @@ void FTNoIR_Protocol::run() Sleep(100); } - (void) timeEndPeriod(1); + CloseHandle(event); } void FTNoIR_Protocol::pose( const double *headpose ) { @@ -169,8 +177,6 @@ bool FTNoIR_Protocol::correct() return false; } - qDebug() << "FTNoIR_Protocol::correct() says: SimConnect functions resolved in DLL!"; - start(); return true; -- cgit v1.2.3