From c26423c613619a40df81a3cd30870e4ef2784007 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 9 Feb 2019 10:39:39 +0100 Subject: proto/simconnect: cleanup Issue: #804 - don't call simconnect_close without a valid handle - remove magic numbers - fix event handle logic. only use it for checking whether we're timing out. - add more informative stderr output - don't hardcode QFunctionPointer type alias - remove awful VirtPos/Rot... --- proto-simconnect/ftnoir_protocol_sc.cpp | 106 ++++++++++++++++---------------- proto-simconnect/ftnoir_protocol_sc.h | 33 +++++----- proto-simconnect/lang/nl_NL.ts | 4 +- proto-simconnect/lang/ru_RU.ts | 4 +- proto-simconnect/lang/stub.ts | 4 +- proto-simconnect/lang/zh_CN.ts | 4 +- 6 files changed, 76 insertions(+), 79 deletions(-) (limited to 'proto-simconnect') diff --git a/proto-simconnect/ftnoir_protocol_sc.cpp b/proto-simconnect/ftnoir_protocol_sc.cpp index 734e10ba..938a0a26 100644 --- a/proto-simconnect/ftnoir_protocol_sc.cpp +++ b/proto-simconnect/ftnoir_protocol_sc.cpp @@ -27,61 +27,57 @@ void simconnect::run() if (event == nullptr) { - qDebug() << "simconnect: event create" << GetLastError(); + qDebug() << "fsx: create event failed, error code" << GetLastError(); return; } + constexpr unsigned sleep_time = 5; + while (!isInterruptionRequested()) { HRESULT hr; + reconnect = false; + handle = nullptr; - if (SUCCEEDED(hr = simconnect_open(&handle, "opentrack", nullptr, 0, event, 0))) + if (!SUCCEEDED(hr = simconnect_open(&handle, "opentrack", nullptr, 0, event, 0))) + qDebug() << "fsx: connect failed, retry in" << sleep_time << "seconds..."; + else { - if (!SUCCEEDED(hr = simconnect_subscribetosystemevent(handle, 0, "Frame"))) - { - qDebug() << "simconnect: can't subscribe to frame event:" << hr; - } - Timer tm; - reconnect = false; - if (SUCCEEDED(hr)) + if (!SUCCEEDED(hr = simconnect_subscribe(handle, 0, "Frame"))) + qDebug() << "fsx: can't subscribe to frame event:" << (void*)hr; + else + { while (!isInterruptionRequested()) { - if (reconnect) - break; + constexpr int max_idle_seconds = 2; - if (WaitForSingleObject(event, 100) == WAIT_OBJECT_0) - { + if (WaitForSingleObject(event, 0) == WAIT_OBJECT_0) tm.start(); - if (!SUCCEEDED(hr = simconnect_calldispatch(handle, processNextSimconnectEvent, reinterpret_cast(this)))) - { - qDebug() << "simconnect: calldispatch failed:" << hr; - break; - } + if ((int)tm.elapsed_seconds() > max_idle_seconds) + { + qDebug() << "fsx: timeout reached, reconnecting"; + break; } if (reconnect) break; - else + if (!SUCCEEDED(hr = simconnect_calldispatch(handle, event_handler, (void*)this))) { - const int idle_seconds = (int)tm.elapsed_seconds(); - constexpr int max_idle_seconds = 2; - - if (idle_seconds >= max_idle_seconds) - break; + qDebug() << "fsx: calldispatch failed:" << (void*)hr; + break; } - } + } + } - (void) simconnect_close(handle); + (void)simconnect_close(handle); } - else - qDebug() << "simconnect: can't open handle:" << (void*)hr; if (!isInterruptionRequested()) - Sleep(3000); + Sleep(sleep_time * 1000); } qDebug() << "simconnect: exit"; @@ -89,26 +85,27 @@ void simconnect::run() CloseHandle(event); } -void simconnect::pose( const double *headpose ) +void simconnect::pose(const double* pose) { - // euler degrees - virtSCRotX = float(-headpose[Pitch]); - virtSCRotY = float(headpose[Yaw]); - virtSCRotZ = float(headpose[Roll]); - - // cm to meters - virtSCPosX = float(-headpose[TX]/100); - virtSCPosY = float(headpose[TY]/100); - virtSCPosZ = float(-headpose[TZ]/100); -} + QMutexLocker l(&mtx); + + data[Pitch] = (float)-pose[Pitch]; + data[Yaw] = (float)pose[Yaw]; + data[Roll] = (float)pose[Roll]; + constexpr float to_meters = 1e-2f; + data[TX] = (float)-pose[TX] * to_meters; + data[TY] = (float)pose[TY] * to_meters; + data[TZ] = (float)-pose[TZ] * to_meters; } module_status simconnect::initialize() { if (!library.isLoaded()) { - activation_context ctx("opentrack-proto-simconnect" "." OPENTRACK_LIBRARY_EXTENSION, 142 + s.sxs_manifest); + constexpr int resource_offset = 142; + activation_context ctx("opentrack-proto-simconnect" "." OPENTRACK_LIBRARY_EXTENSION, + resource_offset + s.sxs_manifest); if (ctx) { @@ -119,25 +116,25 @@ module_status simconnect::initialize() } else // XXX TODO add instructions for fsx and p3d -sh 20190128 - return error(tr("install FSX SDK")); + return error(tr("Install FSX/Prepar3D SimConnect SDK.")); } - using ptr = void(*)(); + using ptr = decltype(library.resolve("")); struct { const char* name; ptr& place; } list[] = { - { "SimConnect_Open", (ptr&)simconnect_open }, - { "SimConnect_CameraSetRelative6DOF", (ptr&)simconnect_set6DOF }, - { "SimConnect_Close", (ptr&)simconnect_close }, - { "SimConnect_CallDispatch", (ptr&)simconnect_calldispatch }, - { "SimConnect_SubscribeToSystemEvent", (ptr&)simconnect_subscribetosystemevent }, + { "SimConnect_Open", (ptr&)simconnect_open }, + { "SimConnect_CameraSetRelative6DOF", (ptr&)simconnect_set6DOF }, + { "SimConnect_Close", (ptr&)simconnect_close }, + { "SimConnect_CallDispatch", (ptr&)simconnect_calldispatch }, + { "SimConnect_SubscribeToSystemEvent", (ptr&)simconnect_subscribe }, }; for (auto& x : list) { - x.place = (ptr)library.resolve(x.name); + x.place = library.resolve(x.name); if (!x.place) return error(tr("can't import %1: %2").arg(x.name, library.errorString())); } @@ -149,10 +146,13 @@ module_status simconnect::initialize() void simconnect::handler() { - (void) simconnect_set6DOF(handle, virtSCPosX, virtSCPosY, virtSCPosZ, virtSCRotX, virtSCRotZ, virtSCRotY); + QMutexLocker l(&mtx); + (void)simconnect_set6DOF(handle, + data[TX], data[TY], data[TZ], + data[Pitch], data[Roll], data[Yaw]); } -void CALLBACK simconnect::processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWORD, void *self_) +void simconnect::event_handler(SIMCONNECT_RECV* pData, DWORD, void* self_) { simconnect& self = *reinterpret_cast(self_); @@ -165,7 +165,7 @@ void CALLBACK simconnect::processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWO self.reconnect = true; break; case SIMCONNECT_RECV_ID_QUIT: - qDebug() << "simconnect: got quit event"; + qDebug() << "fsx: got quit event"; self.reconnect = true; break; case SIMCONNECT_RECV_ID_EVENT_FRAME: @@ -176,7 +176,7 @@ void CALLBACK simconnect::processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWO QString simconnect::game_name() { - return tr("FS2004/FSX"); + return tr("FSX / Prepar3D"); } OPENTRACK_DECLARE_PROTOCOL(simconnect, simconnect_ui, simconnect_metadata) diff --git a/proto-simconnect/ftnoir_protocol_sc.h b/proto-simconnect/ftnoir_protocol_sc.h index dfe80ef7..a51d1518 100644 --- a/proto-simconnect/ftnoir_protocol_sc.h +++ b/proto-simconnect/ftnoir_protocol_sc.h @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -61,30 +62,26 @@ private: DWORD dwID; }; - typedef void (CALLBACK *DispatchProc)(SIMCONNECT_RECV*, DWORD, void*); + typedef void (CALLBACK *SC_DispatchProc)(SIMCONNECT_RECV*, DWORD, void*); - typedef HRESULT (WINAPI *importSimConnect_Open)(HANDLE * phSimConnect, LPCSTR szName, HWND hWnd, DWORD UserEventWin32, HANDLE hEventHandle, DWORD ConfigIndex); - typedef HRESULT (WINAPI *importSimConnect_Close)(HANDLE hSimConnect); - typedef HRESULT (WINAPI *importSimConnect_CameraSetRelative6DOF)(HANDLE hSimConnect, float fDeltaX, float fDeltaY, float fDeltaZ, float fPitchDeg, float fBankDeg, float fHeadingDeg); - typedef HRESULT (WINAPI *importSimConnect_CallDispatch)(HANDLE hSimConnect, DispatchProc pfcnDispatch, void * pContext); - typedef HRESULT (WINAPI *importSimConnect_SubscribeToSystemEvent)(HANDLE hSimConnect, DWORD EventID, const char * SystemEventName); + typedef HRESULT (WINAPI *SC_Open)(HANDLE* phSimConnect, LPCSTR szName, HWND hWnd, DWORD UserEventWin32, HANDLE hEventHandle, DWORD ConfigIndex); + typedef HRESULT (WINAPI *SC_Close)(HANDLE hSimConnect); + typedef HRESULT (WINAPI *SC_CameraSetRelative6DOF)(HANDLE hSimConnect, float fDeltaX, float fDeltaY, float fDeltaZ, float fPitchDeg, float fBankDeg, float fHeadingDeg); + typedef HRESULT (WINAPI *SC_CallDispatch)(HANDLE hSimConnect, SC_DispatchProc pfcnDispatch, void * pContext); + typedef HRESULT (WINAPI *SC_SubscribeToSystemEvent)(HANDLE hSimConnect, DWORD EventID, const char * SystemEventName); - std::atomic virtSCPosX {0}; - std::atomic virtSCPosY {0}; - std::atomic virtSCPosZ {0}; - std::atomic virtSCRotX {0}; - std::atomic virtSCRotY {0}; - std::atomic virtSCRotZ {0}; + float data[6] {}; + QMutex mtx; - importSimConnect_Open simconnect_open = nullptr; - importSimConnect_Close simconnect_close = nullptr; - importSimConnect_CameraSetRelative6DOF simconnect_set6DOF = nullptr; - importSimConnect_CallDispatch simconnect_calldispatch = nullptr; - importSimConnect_SubscribeToSystemEvent simconnect_subscribetosystemevent = nullptr; + SC_Open simconnect_open = nullptr; + SC_Close simconnect_close = nullptr; + SC_CameraSetRelative6DOF simconnect_set6DOF = nullptr; + SC_CallDispatch simconnect_calldispatch = nullptr; + SC_SubscribeToSystemEvent simconnect_subscribe = nullptr; HANDLE handle = nullptr; std::atomic reconnect = false; - static void CALLBACK processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext); + static void CALLBACK event_handler(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext); settings s; QLibrary library; }; diff --git a/proto-simconnect/lang/nl_NL.ts b/proto-simconnect/lang/nl_NL.ts index 8841c3ae..22233fd3 100644 --- a/proto-simconnect/lang/nl_NL.ts +++ b/proto-simconnect/lang/nl_NL.ts @@ -51,11 +51,11 @@ - install FSX SDK + can't import %1: %2 - can't import %1: %2 + Install FSX/Prepar3D SimConnect SDK. diff --git a/proto-simconnect/lang/ru_RU.ts b/proto-simconnect/lang/ru_RU.ts index 9a9c7c46..9b7cd214 100644 --- a/proto-simconnect/lang/ru_RU.ts +++ b/proto-simconnect/lang/ru_RU.ts @@ -51,11 +51,11 @@ - install FSX SDK + can't import %1: %2 - can't import %1: %2 + Install FSX/Prepar3D SimConnect SDK. diff --git a/proto-simconnect/lang/stub.ts b/proto-simconnect/lang/stub.ts index a221cf39..61f9acd8 100644 --- a/proto-simconnect/lang/stub.ts +++ b/proto-simconnect/lang/stub.ts @@ -51,11 +51,11 @@ - install FSX SDK + can't import %1: %2 - can't import %1: %2 + Install FSX/Prepar3D SimConnect SDK. diff --git a/proto-simconnect/lang/zh_CN.ts b/proto-simconnect/lang/zh_CN.ts index a221cf39..61f9acd8 100644 --- a/proto-simconnect/lang/zh_CN.ts +++ b/proto-simconnect/lang/zh_CN.ts @@ -51,11 +51,11 @@ - install FSX SDK + can't import %1: %2 - can't import %1: %2 + Install FSX/Prepar3D SimConnect SDK. -- cgit v1.2.3