From 5f5ca66c58ad30d0c218b2da904aa3f722e18ebe Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 7 Jul 2016 14:15:18 +0200 Subject: logic/dinput: only ever use a single handle to DIRECTINPUT8 COM object --- opentrack-logic/dinput.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 opentrack-logic/dinput.cpp (limited to 'opentrack-logic/dinput.cpp') diff --git a/opentrack-logic/dinput.cpp b/opentrack-logic/dinput.cpp new file mode 100644 index 00000000..4958e862 --- /dev/null +++ b/opentrack-logic/dinput.cpp @@ -0,0 +1,48 @@ +#ifdef _WIN32 + +#include "dinput.hpp" +#include + +dinput_handle dinput_handle::self; + +dinput_handle::dinput_handle() : handle(init_di()) +{ +} + +dinput_handle::~dinput_handle() +{ + if (handle) + { + handle->Release(); + handle = nullptr; + } +} + +dinput_handle::di_t dinput_handle::init_di() +{ + HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + if (FAILED(hr)) + qDebug() << "dinput: failed CoInitializeEx" << hr << GetLastError(); + + static LPDIRECTINPUT8 di_ = nullptr; + if (di_ == nullptr) + { + if (SUCCEEDED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&di_, NULL))) + { + qDebug() << "made dinput8 handle"; + return di_; + } + else + { + return di_ = nullptr; + } + } + return di_; +} + +dinput_handle::di_t dinput_handle::make_di() +{ + return self.handle; +} + +#endif -- cgit v1.2.3