From 33c1683b37265001eb91024290f7137c5b3c5411 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 12 Aug 2016 17:06:14 +0200 Subject: dinput: hold dinput reference through entire lifetime --- opentrack-dinput/keybinding-worker.cpp | 18 +++++++++++++++--- opentrack-dinput/keybinding-worker.hpp | 3 +++ opentrack-dinput/win32-joystick.cpp | 18 +++++++----------- opentrack-dinput/win32-joystick.hpp | 7 ++++--- 4 files changed, 29 insertions(+), 17 deletions(-) (limited to 'opentrack-dinput') diff --git a/opentrack-dinput/keybinding-worker.cpp b/opentrack-dinput/keybinding-worker.cpp index a009d399..32bfc6f0 100644 --- a/opentrack-dinput/keybinding-worker.cpp +++ b/opentrack-dinput/keybinding-worker.cpp @@ -25,6 +25,8 @@ bool Key::should_process() KeybindingWorker::~KeybindingWorker() { + qDebug() << "exit: destroying keybinding worker"; + should_quit = true; wait(); if (dinkeyboard) { @@ -33,9 +35,15 @@ KeybindingWorker::~KeybindingWorker() } } -KeybindingWorker::KeybindingWorker() : should_quit(true) +void KeybindingWorker::init() { - LPDIRECTINPUT8 din = dinput_handle::make_di(); + din = dinput_handle::make_di(); + + if (!din) + { + qDebug() << "can't create dinput handle"; + return; + } if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL) != DI_OK) { qDebug() << "setup CreateDevice function failed!" << GetLastError(); @@ -63,8 +71,10 @@ KeybindingWorker::KeybindingWorker() : should_quit(true) qDebug() << "setup dinkeyboard Acquire failed!" << GetLastError(); return; } +} - should_quit = false; +KeybindingWorker::KeybindingWorker() : should_quit(false) +{ start(); } @@ -76,6 +86,8 @@ KeybindingWorker& KeybindingWorker::make() void KeybindingWorker::run() { + init(); + BYTE keystate[256] = {0}; BYTE old_keystate[256] = {0}; diff --git a/opentrack-dinput/keybinding-worker.hpp b/opentrack-dinput/keybinding-worker.hpp index c0d61c2f..623875ac 100644 --- a/opentrack-dinput/keybinding-worker.hpp +++ b/opentrack-dinput/keybinding-worker.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -46,9 +47,11 @@ private: std::vector> receivers; QMutex mtx; QMainWindow fake_main_window; + dinput_handle::di_t din; volatile bool should_quit; void run() override; + void init(); KeybindingWorker(); static KeybindingWorker& make(); diff --git a/opentrack-dinput/win32-joystick.cpp b/opentrack-dinput/win32-joystick.cpp index fa1a8060..4765d4e9 100644 --- a/opentrack-dinput/win32-joystick.cpp +++ b/opentrack-dinput/win32-joystick.cpp @@ -9,6 +9,8 @@ #include #include +#include + QMutex win32_joy_ctx::enum_state::mtx; win32_joy_ctx::enum_state win32_joy_ctx::enumerator; @@ -120,7 +122,7 @@ void win32_joy_ctx::refresh() enumerator.refresh(); } -QString win32_joy_ctx::guid_to_string(const GUID guid) +QString win32_joy_ctx::guid_to_string(const GUID& guid) { char buf[40] = {0}; wchar_t szGuidW[40] = {0}; @@ -218,7 +220,7 @@ bool win32_joy_ctx::joy::poll(fn f) return true; } -win32_joy_ctx::enum_state::enum_state() +win32_joy_ctx::enum_state::enum_state() : di(dinput_handle::make_di()) { } @@ -233,7 +235,6 @@ void win32_joy_ctx::enum_state::refresh() { all.clear(); - di_t di = dinput_handle::make_di(); if (!di) { qDebug() << "can't create dinput"; @@ -264,15 +265,10 @@ void win32_joy_ctx::enum_state::refresh() } } +const win32_joy_ctx::joys_t& win32_joy_ctx::enum_state::get_joys() const { return joys; } + BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext) { - di_t di = dinput_handle::make_di(); - if (!di) - { - qDebug() << "can't create dinput"; - return DIENUM_STOP; - } - enum_state& state = *reinterpret_cast(pContext); const QString guid = guid_to_string(pdidInstance->guidInstance); const QString name = QString(pdidInstance->tszInstanceName); @@ -287,7 +283,7 @@ BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINS { HRESULT hr; LPDIRECTINPUTDEVICE8 h; - if (FAILED(hr = di->CreateDevice(pdidInstance->guidInstance, &h, nullptr))) + if (FAILED(hr = state.di->CreateDevice(pdidInstance->guidInstance, &h, nullptr))) { qDebug() << "createdevice" << guid << hr; goto end; diff --git a/opentrack-dinput/win32-joystick.hpp b/opentrack-dinput/win32-joystick.hpp index a69ec765..ef8f59b0 100644 --- a/opentrack-dinput/win32-joystick.hpp +++ b/opentrack-dinput/win32-joystick.hpp @@ -75,12 +75,13 @@ struct OPENTRACK_DINPUT_EXPORT win32_joy_ctx using di_t = dinput_handle::di_t; private: - static QString guid_to_string(const GUID guid); + static QString guid_to_string(const GUID& guid); - class enum_state final + class OPENTRACK_DINPUT_EXPORT enum_state final { std::vector all; joys_t joys; + dinput_handle::di_t di; static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext); static BOOL CALLBACK EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* ctx); @@ -91,7 +92,7 @@ private: enum_state(); ~enum_state(); void refresh(); - const joys_t& get_joys() const { return joys; } + const joys_t& get_joys() const; }; static enum_state enumerator; -- cgit v1.2.3