diff options
| -rw-r--r-- | opentrack-dinput/dinput.cpp | 48 | ||||
| -rw-r--r-- | opentrack-dinput/dinput.hpp | 13 | 
2 files changed, 47 insertions, 14 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 diff --git a/opentrack-dinput/dinput.hpp b/opentrack-dinput/dinput.hpp index 37a6d723..53f1c4af 100644 --- a/opentrack-dinput/dinput.hpp +++ b/opentrack-dinput/dinput.hpp @@ -35,16 +35,19 @@ public:      {          friend class dinput_handle; -        LPDIRECTINPUT8& handle; +        LPDIRECTINPUT8* handle;          di_t(LPDIRECTINPUT8& handle);          void free_di(); +        void unref_di(); +        void ref_di();      public: -        LPDIRECTINPUT8 operator->() { return handle; } -        operator LPDIRECTINPUT8() { return handle; } -        LPDIRECTINPUT8 di() { return handle; } - +        LPDIRECTINPUT8 operator->() { return *handle; } +        operator LPDIRECTINPUT8() { return *handle; } +        LPDIRECTINPUT8 di() { return *handle; } +        di_t& operator=(const di_t& new_di); +        di_t();          ~di_t();      };  | 
