summaryrefslogtreecommitdiffhomepage
path: root/opentrack/keybinding-worker.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-11-22 15:06:30 +0100
committerStanislaw Halik <sthalik@misaki.pl>2015-11-22 15:55:39 +0100
commita9860e951757bf454b83b13ce7eff865f0b6644e (patch)
tree88d13f20a8066e0a2aaf0f94944a03733279db66 /opentrack/keybinding-worker.hpp
parent763da906a83b80eadb736008dbb558b809552505 (diff)
api/keyboard: implement a central worker
DirectInput dies when two LPDIRECTINPUT8 handles are obtained. Implement a singleton worker providing keypress events.
Diffstat (limited to 'opentrack/keybinding-worker.hpp')
-rw-r--r--opentrack/keybinding-worker.hpp48
1 files changed, 37 insertions, 11 deletions
diff --git a/opentrack/keybinding-worker.hpp b/opentrack/keybinding-worker.hpp
index b5d63fac..5a73b1b1 100644
--- a/opentrack/keybinding-worker.hpp
+++ b/opentrack/keybinding-worker.hpp
@@ -20,6 +20,7 @@
#include <QMutex>
#include <QWidget>
#include <functional>
+#include <vector>
#ifdef _WIN32
# undef DIRECTINPUT_VERSION
@@ -53,21 +54,46 @@ typedef unsigned char BYTE;
struct Key { int foo; };
#endif
-struct OPENTRACK_EXPORT KeybindingWorker : public QThread {
-#ifdef _WIN32
+struct OPENTRACK_EXPORT KeybindingWorker : private QThread
+{
private:
LPDIRECTINPUT8 din;
LPDIRECTINPUTDEVICE8 dinkeyboard;
win32_joy_ctx joy_ctx;
-public:
volatile bool should_quit;
- std::function<void(Key&)> receiver;
+ 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();
- KeybindingWorker(std::function<void(Key&)> receiver, WId h);
- void run();
-#else
public:
- KeybindingWorker(Key, Key, Key, WId) {}
- void run() {}
-#endif
-}; \ No newline at end of file
+ class Token
+ {
+ friend class KeybindingWorker;
+ fun* pos;
+ //Token(const Token&) = delete;
+ Token& operator=(Token&) = delete;
+ public:
+ Token(fun receiver)
+ {
+ pos = make()._add_receiver(receiver);
+ }
+ ~Token()
+ {
+ make().remove_receiver(pos);
+ }
+ };
+ friend class Token;
+ static Token add_receiver(fun receiver)
+ {
+ return Token(receiver);
+ }
+};