summaryrefslogtreecommitdiffhomepage
path: root/dinput/dinput.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dinput/dinput.cpp')
-rw-r--r--dinput/dinput.cpp62
1 files changed, 47 insertions, 15 deletions
diff --git a/dinput/dinput.cpp b/dinput/dinput.cpp
index 8a3d0f4a..22781a32 100644
--- a/dinput/dinput.cpp
+++ b/dinput/dinput.cpp
@@ -1,21 +1,23 @@
#include "dinput.hpp"
+#include "compat/macros.hpp"
+
#include <QDebug>
-std::atomic<int> di_t::refcnt;
-std::atomic_flag di_t::init_lock = ATOMIC_FLAG_INIT;
+int di_t::refcnt{0};
diptr di_t::handle;
+QMutex di_t::lock;
diptr di_t::init_di_()
{
CoInitialize(nullptr);
diptr di = nullptr;
- if (HRESULT hr = DirectInput8Create(GetModuleHandle(nullptr),
- DIRECTINPUT_VERSION,
- IID_IDirectInput8,
- (void**)&di,
- nullptr);
- !SUCCEEDED(hr))
+ HRESULT hr = DirectInput8Create(GetModuleHandle(nullptr),
+ DIRECTINPUT_VERSION,
+ IID_IDirectInput8,
+ (void**)&di,
+ nullptr);
+ if (!SUCCEEDED(hr))
{
qDebug() << "can't make dinput:" << (void*)(LONG_PTR)hr;
qDebug() << "crashing!";
@@ -32,28 +34,24 @@ di_t::di_t()
void di_t::ref_di()
{
- while (init_lock.test_and_set()) { /* busy loop */ }
+ QMutexLocker l(&lock);
if (!handle)
handle = init_di_();
++refcnt;
-
- init_lock.clear();
}
void di_t::unref_di()
{
+ QMutexLocker l(&lock);
+
const int refcnt_ = --refcnt;
if (refcnt_ == 0)
{
- while (init_lock.test_and_set()) { /* busy loop */ }
-
qDebug() << "exit: di handle";
handle->Release();
-
- init_lock.clear();
}
}
@@ -62,3 +60,37 @@ di_t::~di_t()
unref_di();
}
+bool di_t::poll_device(LPDIRECTINPUTDEVICE8 dev)
+{
+ HRESULT hr;
+
+ switch (dev->Poll())
+ {
+ case DI_OK:
+ case DI_NOEFFECT:
+ return true;
+ default:
+ break;
+ }
+
+ switch (hr = dev->Acquire())
+ {
+ default:
+ break;
+ case DI_OK:
+ case S_FALSE:
+ switch (hr = dev->Poll())
+ {
+ case DI_OK:
+ case DI_NOEFFECT:
+ return true;
+ default:
+ break;
+ }
+ break;
+ }
+
+ eval_once(qDebug() << "dinput: device poll failed:" << (void*)hr);
+
+ return false;
+}