summaryrefslogtreecommitdiffhomepage
path: root/dinput/dinput.cpp
blob: 1b0cf09d519d874292be5e1601669baa16d56bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifdef _WIN32

#include "dinput.hpp"
#include "compat/win32-com.hpp"
#include <QDebug>

std::atomic<int> dinput_handle::refcnt;
std::atomic_flag dinput_handle::init_lock = ATOMIC_FLAG_INIT;

LPDIRECTINPUT8& dinput_handle::init_di()
{
    init_com_threading(com_multithreaded);

    static LPDIRECTINPUT8 di_ = nullptr;
    if (di_ == nullptr)
    {
        if (!SUCCEEDED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&di_, NULL)))
        {
            di_ = nullptr;
        }
    }
    return di_;
}

dinput_handle::di_t dinput_handle::make_di()
{
    while (init_lock.test_and_set()) { /* busy loop */ }

    LPDIRECTINPUT8& ret = init_di();

    init_lock.clear();

    return di_t(ret);
}

void dinput_handle::di_t::free_di()
{
    if (handle && *handle)
        (*handle)->Release();
    *handle = nullptr;
    handle = nullptr;
}

void dinput_handle::di_t::ref_di()
{
    while (init_lock.test_and_set()) { /* busy loop */ }

    const int refcnt_ = refcnt.fetch_add(1) + 1;
    qDebug() << "start: dinput refcount now" << (refcnt_);

    init_lock.clear();
}

dinput_handle::di_t& dinput_handle::di_t::operator=(const di_t& new_di)
{
    if (handle)
        unref_di();

    handle = new_di.handle;

    if (handle)
        ref_di();

    return *this;
}

void dinput_handle::di_t::unref_di()
{
    while (init_lock.test_and_set()) { /* busy loop */ }

    const int refcnt_ = refcnt.fetch_sub(1) - 1;

    qDebug() << "exit: dinput refcount now" << refcnt_;

    if (refcnt_ == 0)
    {
        qDebug() << "exit: deleting di handle";
        free_di();
    }

    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