summaryrefslogtreecommitdiffhomepage
path: root/dinput/dinput.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2019-03-21 10:31:45 +0100
committerStanislaw Halik <sthalik@misaki.pl>2019-03-21 10:31:45 +0100
commit8553a64aa9eddfa9c7ed166ecb220f1a8c115b78 (patch)
tree970843c27d6079d71be15715adb984e375870e58 /dinput/dinput.cpp
parent89d5870138a4ad26c00cddb209ebe5312fe85dc3 (diff)
dinput: get rid of refcounting
Diffstat (limited to 'dinput/dinput.cpp')
-rw-r--r--dinput/dinput.cpp69
1 files changed, 35 insertions, 34 deletions
diff --git a/dinput/dinput.cpp b/dinput/dinput.cpp
index 22781a32..02b56683 100644
--- a/dinput/dinput.cpp
+++ b/dinput/dinput.cpp
@@ -1,68 +1,69 @@
+#undef NDEBUG
+
#include "dinput.hpp"
#include "compat/macros.hpp"
+#include <cassert>
+#include <cstdlib>
+
#include <QDebug>
-int di_t::refcnt{0};
diptr di_t::handle;
QMutex di_t::lock;
-diptr di_t::init_di_()
+diptr di_t::init_di()
{
CoInitialize(nullptr);
- diptr di = nullptr;
- 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!";
- std::abort();
- }
+ if (!handle)
+ handle = init_di_();
- return di;
+ return handle;
}
-di_t::di_t()
+diptr di_t::operator->() const
{
- ref_di();
+ QMutexLocker l(&lock);
+ return init_di();
}
-void di_t::ref_di()
+di_t::operator bool() const
{
QMutexLocker l(&lock);
-
- if (!handle)
- handle = init_di_();
-
- ++refcnt;
+ return !!init_di();
}
-void di_t::unref_di()
+di_t::operator diptr() const
{
- QMutexLocker l(&lock);
-
- const int refcnt_ = --refcnt;
+ return init_di();
+}
- if (refcnt_ == 0)
+diptr di_t::init_di_()
+{
+ diptr di = nullptr;
+ HRESULT hr = DirectInput8Create(GetModuleHandle(nullptr),
+ DIRECTINPUT_VERSION,
+ IID_IDirectInput8,
+ (void**)&di,
+ nullptr);
+ if (!SUCCEEDED(hr))
{
- qDebug() << "exit: di handle";
- handle->Release();
+ qDebug() << "can't make dinput:" << (void*)(LONG_PTR)hr;
+ qDebug() << "crashing!";
+ std::abort();
}
-}
-di_t::~di_t()
-{
- unref_di();
+ qDebug() << "dinput: initialized";
+
+ return di;
}
+di_t::di_t() = default;
+
bool di_t::poll_device(LPDIRECTINPUTDEVICE8 dev)
{
HRESULT hr;
+ assert(handle);
switch (dev->Poll())
{