diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2019-02-09 10:39:39 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2019-02-09 12:13:36 +0100 |
commit | c26423c613619a40df81a3cd30870e4ef2784007 (patch) | |
tree | 37f68cd5f017d085f2b3eb2da4c448d5f7921cb4 | |
parent | e80d210dcc7e2b45656dfe29cb42e4363a088b05 (diff) |
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...
-rw-r--r-- | proto-simconnect/ftnoir_protocol_sc.cpp | 106 | ||||
-rw-r--r-- | proto-simconnect/ftnoir_protocol_sc.h | 33 | ||||
-rw-r--r-- | proto-simconnect/lang/nl_NL.ts | 4 | ||||
-rw-r--r-- | proto-simconnect/lang/ru_RU.ts | 4 | ||||
-rw-r--r-- | proto-simconnect/lang/stub.ts | 4 | ||||
-rw-r--r-- | proto-simconnect/lang/zh_CN.ts | 4 |
6 files changed, 76 insertions, 79 deletions
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<void*>(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<simconnect*>(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 <atomic> #include <QThread> +#include <QMutex> #include <QMessageBox> #include <QSettings> #include <QLibrary> @@ -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<float> virtSCPosX {0}; - std::atomic<float> virtSCPosY {0}; - std::atomic<float> virtSCPosZ {0}; - std::atomic<float> virtSCRotX {0}; - std::atomic<float> virtSCRotY {0}; - std::atomic<float> 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<bool> 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 @@ <translation type="unfinished"></translation> </message> <message> - <source>install FSX SDK</source> + <source>can't import %1: %2</source> <translation type="unfinished"></translation> </message> <message> - <source>can't import %1: %2</source> + <source>Install FSX/Prepar3D SimConnect SDK.</source> <translation type="unfinished"></translation> </message> </context> 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 @@ <translation type="unfinished"></translation> </message> <message> - <source>install FSX SDK</source> + <source>can't import %1: %2</source> <translation type="unfinished"></translation> </message> <message> - <source>can't import %1: %2</source> + <source>Install FSX/Prepar3D SimConnect SDK.</source> <translation type="unfinished"></translation> </message> </context> 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 @@ <translation type="unfinished"></translation> </message> <message> - <source>install FSX SDK</source> + <source>can't import %1: %2</source> <translation type="unfinished"></translation> </message> <message> - <source>can't import %1: %2</source> + <source>Install FSX/Prepar3D SimConnect SDK.</source> <translation type="unfinished"></translation> </message> </context> 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 @@ <translation type="unfinished"></translation> </message> <message> - <source>install FSX SDK</source> + <source>can't import %1: %2</source> <translation type="unfinished"></translation> </message> <message> - <source>can't import %1: %2</source> + <source>Install FSX/Prepar3D SimConnect SDK.</source> <translation type="unfinished"></translation> </message> </context> |