summaryrefslogtreecommitdiffhomepage
path: root/opentrack/keybinding-worker.hpp
blob: 5b669331a966709654df9030a801a12db7f4704d (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
/* 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.
 */

#pragma once

#ifdef BUILD_api
#   include "opentrack-compat/export.hpp"
#else
#   include "opentrack-compat/import.hpp"
#endif

#include "opentrack-compat/timer.hpp"
#include "opentrack/win32-joystick.hpp"
#include <QThread>
#include <QMutex>
#include <QWidget>
#include <functional>
#include <vector>

#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0800
#include <windows.h>
#include <dinput.h>
struct Key {
    BYTE keycode;
    QString guid;
    bool shift;
    bool ctrl;
    bool alt;
    bool held;
    Timer timer;
public:
    Key() : keycode(0), shift(false), ctrl(false), alt(false), held(true) {}

    bool should_process();
};

struct OPENTRACK_EXPORT KeybindingWorker : private QThread
{
private:
    LPDIRECTINPUT8 din;
    LPDIRECTINPUTDEVICE8 dinkeyboard;
    win32_joy_ctx& joy_ctx;
    volatile bool should_quit;
    using fun = std::function<void(Key&)>;
    std::vector<fun> receivers;
    QMutex mtx;

    void run() override;
    KeybindingWorker();

    KeybindingWorker(const KeybindingWorker&) = delete;
    KeybindingWorker& operator=(KeybindingWorker&) = delete;
    static KeybindingWorker& make();
    fun* _add_receiver(fun receiver);
    void remove_receiver(fun* pos);
    ~KeybindingWorker();
public:
    class Token
    {
        fun* pos;
        //Token(const Token&) = delete;
        Token& operator=(Token&) = delete;
    public:
        Token(fun receiver)
        {
            pos = make()._add_receiver(receiver);
        }
        ~Token()
        {
            make().remove_receiver(pos);
        }
    };
    static Token add_receiver(fun receiver)
    {
        return Token(receiver);
    }
};