summaryrefslogtreecommitdiffhomepage
path: root/opentrack-dinput
diff options
context:
space:
mode:
Diffstat (limited to 'opentrack-dinput')
-rw-r--r--opentrack-dinput/keybinding-worker.cpp18
-rw-r--r--opentrack-dinput/keybinding-worker.hpp3
-rw-r--r--opentrack-dinput/win32-joystick.cpp18
-rw-r--r--opentrack-dinput/win32-joystick.hpp7
4 files changed, 29 insertions, 17 deletions
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 <QMutex>
#include <QWidget>
#include <QMainWindow>
+#include <QDebug>
#include <functional>
#include <vector>
@@ -46,9 +47,11 @@ private:
std::vector<std::unique_ptr<fun>> 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 <cmath>
#include <objbase.h>
+#include <QDebug>
+
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<enum_state*>(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<QString> 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;