diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-07-07 14:15:18 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-07-07 14:15:18 +0200 |
commit | 5f5ca66c58ad30d0c218b2da904aa3f722e18ebe (patch) | |
tree | 8e8d5bae3ca7edf6c96c7f0e0da87f347486b045 /opentrack-logic/dinput.cpp | |
parent | c418644838e99416fb168066bacc28a4cbc36a04 (diff) |
logic/dinput: only ever use a single handle to DIRECTINPUT8 COM object
Diffstat (limited to 'opentrack-logic/dinput.cpp')
-rw-r--r-- | opentrack-logic/dinput.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
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 <QDebug> + +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 |