summaryrefslogtreecommitdiffhomepage
path: root/dinput/keybinding-worker.cpp
blob: 91b02428b3fd4bfac6a7b44c7a6c3ac3c073c499 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* Copyright (c) 2014-2015, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#ifdef _WIN32

#include "keybinding-worker.hpp"
#include "compat/macros.hpp"
#include "compat/thread-name.hpp"

#include <QDebug>
#include <QMutexLocker>

#include <windows.h>

Key::Key() = default;

bool Key::should_process()
{
    if (!enabled || (keycode == 0 && guid == ""))
        return false;
    bool ret = !held || timer.elapsed_ms() > 100;
    timer.start();
    return ret;
}

KeybindingWorker::~KeybindingWorker()
{
    qDebug() << "exit: keybinding worker";

    requestInterruption();
    wait();

    if (dinkeyboard)
    {
        dinkeyboard->Unacquire();
        dinkeyboard->Release();
    }
}

bool KeybindingWorker::init()
{
    if (dinkeyboard)
        return true;

    if (!din)
    {
        qDebug() << "can't create dinput handle";
        goto fail;
    }

    if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, nullptr) != DI_OK)
    {
        qDebug() << "dinput: create keyboard failed" << GetLastError();
        goto fail;
    }

    if (dinkeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK)
    {
        qDebug() << "dinput: keyboard SetDataFormat" << GetLastError();
        goto fail;
    }

    if (dinkeyboard->SetCooperativeLevel((HWND) fake_main_window.winId(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK)
    {
        qDebug() << "dinput: keyboard SetCooperativeLevel" << GetLastError();
        goto fail;
    }

    {
        DIPROPDWORD dipdw;
        dipdw.dwData = num_keyboard_states;
        dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
        dipdw.diph.dwHow = DIPH_DEVICE;
        dipdw.diph.dwObj = 0;
        dipdw.diph.dwSize = sizeof(dipdw);

        if (dinkeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph) != DI_OK)
        {
            qDebug() << "dinput: DIPROP_BUFFERSIZE";
            goto fail;
        }
    }

    return true;

fail:
    if (dinkeyboard)
    {
        dinkeyboard->Release();
        dinkeyboard = nullptr;
    }
    return false;
}

KeybindingWorker::KeybindingWorker()
{
    fake_main_window.setAttribute(Qt::WA_NativeWindow);

    if (init())
        start(QThread::HighPriority);
}

KeybindingWorker& KeybindingWorker::make()
{
    static KeybindingWorker k;
    return k;
}

void KeybindingWorker::run()
{
    portable::set_curthread_name("keybinding worker");

    while (!isInterruptionRequested())
    {
        {
            QMutexLocker l(&mtx);

            if (!receivers.empty())
            {
                bool ok = true;

                ok &= run_keyboard_nolock();
                ok &= run_joystick_nolock();

                if (!ok)
                    Sleep(500);
            }
        }

        Sleep(25);
    }
}

bool KeybindingWorker::run_keyboard_nolock()
{
    /* There are some problems reported on various forums
     * with regard to key-up events. But that's what I dug up:
     *
     * https://www.gamedev.net/forums/topic/633011-keyboard-getdevicedata-buffered-never-releases-keys/
     *
     * "Over in the xna forums (http://xboxforums.create.msdn.com/forums/p/108722/642144.aspx#642144)
     *  we discovered this behavior is caused by calling Unacquire in your event processing loop.
     *  Funnily enough only the keyboard seems to be affected."
     *
     * Key-up events work on my end.
     */

    if (!di_t::poll_device(dinkeyboard))
        eval_once(qDebug() << "dinput: keyboard poll failed");

    DWORD sz = num_keyboard_states;
    HRESULT hr = dinkeyboard->GetDeviceData(sizeof(*keyboard_states), keyboard_states, &sz, 0);

    if (FAILED(hr))
    {
        eval_once(qDebug() << "dinput: keyboard GetDeviceData failed" << (void*)hr);
        return false;
    }

    for (unsigned k = 0; k < sz; k++)
    {
        const unsigned idx = keyboard_states[k].dwOfs & 0xff; // defensive programming
        const bool held = !!(keyboard_states[k].dwData & 0x80);

        switch (idx)
        {
        case DIK_LCONTROL:
        case DIK_LSHIFT:
        case DIK_LALT:
        case DIK_RCONTROL:
        case DIK_RSHIFT:
        case DIK_RALT:
        case DIK_LWIN:
        case DIK_RWIN:
            break;
        default:
        {
            Key k;
            k.shift = keystate[DIK_LSHIFT] | keystate[DIK_RSHIFT];
            k.alt = keystate[DIK_LALT] | keystate[DIK_RALT];
            k.ctrl = keystate[DIK_LCONTROL] | keystate[DIK_RCONTROL];
            k.keycode = idx;
            k.held = held;

            for (auto& r : receivers)
                (*r)(k);
        }
            break;
        }

        keystate[idx] = held;
    }

    return true;
}

bool KeybindingWorker::run_joystick_nolock()
{
    using joy_fn = std::function<void(const QString& guid, int idx, bool held)>;

    joy_fn f = [&](const QString& guid, int idx, bool held) {
        Key k;
        k.keycode = idx;
        k.shift = keystate[DIK_LSHIFT] | keystate[DIK_RSHIFT];
        k.alt = keystate[DIK_LALT] | keystate[DIK_RALT];
        k.ctrl = keystate[DIK_LCONTROL] | keystate[DIK_RCONTROL];
        k.guid = guid;
        k.held = held;

        for (auto& r : receivers)
            (*r)(k);
    };

    joy_ctx.poll(f);

    return true;
}

KeybindingWorker::fun* KeybindingWorker::add_receiver(fun& receiver)
{
    QMutexLocker l(&mtx);
    receivers.push_back(std::make_unique<fun>(receiver));
    fun* f = &*receivers[receivers.size() - 1];
    //qDebug() << "add receiver" << (long) f;
    joy_ctx.refresh();
    return f;
}

void KeybindingWorker::remove_receiver(KeybindingWorker::fun* pos)
{
    QMutexLocker l(&mtx);
    bool ok = false;

    using s = int;

    for (int i = s(receivers.size()) - 1; i >= 0; i--)
    {
        using u = unsigned;
        if (&*receivers[u(i)] == pos)
        {
            ok = true;
            //qDebug() << "remove receiver" << (long) pos;
            receivers.erase(receivers.begin() + i);
            break;
        }
    }
    if (!ok)
    {
        qDebug() << "bad remove receiver" << (void*) pos;
    }
}

#endif