diff options
Diffstat (limited to 'opentrack-dinput/dinput.cpp')
-rw-r--r-- | opentrack-dinput/dinput.cpp | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/opentrack-dinput/dinput.cpp b/opentrack-dinput/dinput.cpp index 217df6a7..ae132e70 100644 --- a/opentrack-dinput/dinput.cpp +++ b/opentrack-dinput/dinput.cpp @@ -34,31 +34,46 @@ dinput_handle::di_t dinput_handle::make_di() return di_t(ret); } -#endif +void dinput_handle::di_t::free_di() +{ + if (handle && *handle) + (*handle)->Release(); + *handle = nullptr; + handle = nullptr; +} -dinput_handle::di_t::di_t(LPDIRECTINPUT8& handle) : handle(handle) +void dinput_handle::di_t::ref_di() { while (init_lock.test_and_set()) { /* busy loop */ } - refcnt++; + const int refcnt_ = refcnt.fetch_add(1) + 1; + qDebug() << "start: dinput refcount now" << (refcnt_); init_lock.clear(); } -void dinput_handle::di_t::free_di() +dinput_handle::di_t& dinput_handle::di_t::operator=(const di_t& new_di) { if (handle) - handle->Release(); - handle = nullptr; + unref_di(); + + handle = new_di.handle; + + if (handle) + ref_di(); + + return *this; } -dinput_handle::di_t::~di_t() +void dinput_handle::di_t::unref_di() { while (init_lock.test_and_set()) { /* busy loop */ } - int refcnt_ = refcnt.fetch_sub(1); + const int refcnt_ = refcnt.fetch_sub(1) - 1; - if (refcnt_ == 1) + qDebug() << "exit: dinput refcount now" << refcnt_; + + if (refcnt_ == 0) { qDebug() << "exit: deleting di handle"; free_di(); @@ -66,3 +81,18 @@ dinput_handle::di_t::~di_t() init_lock.clear(); } + +dinput_handle::di_t::di_t(LPDIRECTINPUT8& handle) : handle(&handle) +{ + ref_di(); +} + +dinput_handle::di_t::di_t() : handle(nullptr) {} + +dinput_handle::di_t::~di_t() +{ + if (handle) + unref_di(); +} + +#endif |