diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2015-10-02 08:52:18 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2015-10-02 08:52:18 +0200 |
commit | f1431409454f3b03c7e7ecb3a119c8b552c3443f (patch) | |
tree | f97038addf3283cfbb2015fcb123494d0f7fee73 /opentrack | |
parent | 4bc6c9e69170f2e4aa9bdcd908fda7027bcae044 (diff) | |
parent | 68d33b0c99cfac8f89c2224d457e1568232896f7 (diff) |
Merge branch 'unstable' into trackhat-ui
* unstable:
shortcuts: use dinput for consistency on win32
shortcuts: use our own timer, other one is thread-bound
shortcuts: implement dinput -> qt conversion
shortcuts: rework KeybindingWorker for win32
Diffstat (limited to 'opentrack')
-rw-r--r-- | opentrack/shortcuts.cpp | 96 | ||||
-rw-r--r-- | opentrack/shortcuts.h | 19 | ||||
-rw-r--r-- | opentrack/win32-shortcuts.cpp | 17 | ||||
-rw-r--r-- | opentrack/win32-shortcuts.h | 2 |
4 files changed, 83 insertions, 51 deletions
diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp index 63cd9088..e20fa1c0 100644 --- a/opentrack/shortcuts.cpp +++ b/opentrack/shortcuts.cpp @@ -10,18 +10,10 @@ #include <QMutexLocker> #if defined(_WIN32) +#include <functional> #include <windows.h> #include "win32-shortcuts.h" -void KeybindingWorker::set_keys(Key kCenter_, Key kToggle_, Key kZero_) -{ - QMutexLocker l(&mtx); - - kCenter = kCenter_; - kToggle = kToggle_; - kZero = kZero_; -} - KeybindingWorker::~KeybindingWorker() { should_quit = true; wait(); @@ -33,9 +25,11 @@ KeybindingWorker::~KeybindingWorker() { din->Release(); } -KeybindingWorker::KeybindingWorker(Key keyCenter, Key keyToggle, Key keyZero, WId handle, Shortcuts& sc) : - sc(sc), din(0), dinkeyboard(0), kCenter(keyCenter), kToggle(keyToggle), kZero(keyZero), should_quit(true) +KeybindingWorker::KeybindingWorker(std::function<void(Key&)> receiver, WId h) : + should_quit(true), receiver(receiver) { + HWND handle = reinterpret_cast<HWND>(h); + if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL) != DI_OK) { qDebug() << "setup DirectInput8 Creation failed!" << GetLastError(); return; @@ -74,26 +68,6 @@ KeybindingWorker::KeybindingWorker(Key keyCenter, Key keyToggle, Key keyZero, WI should_quit = false; } -static bool isKeyPressed( const Key *key, const BYTE *keystate ) { - bool shift; - bool ctrl; - bool alt; - - if (key->keycode != 0 && keystate[key->keycode] & 0x80) - { - shift = ( (keystate[DIK_LSHIFT] & 0x80) || (keystate[DIK_RSHIFT] & 0x80) ); - ctrl = ( (keystate[DIK_LCONTROL] & 0x80) || (keystate[DIK_RCONTROL] & 0x80) ); - alt = ( (keystate[DIK_LALT] & 0x80) || (keystate[DIK_RALT] & 0x80) ); - - if (key->shift && !shift) return false; - if (key->ctrl && !ctrl) return false; - if (key->alt && !alt) return false; - - return true; - } - return false; -} - void KeybindingWorker::run() { BYTE keystate[256]; @@ -107,14 +81,30 @@ void KeybindingWorker::run() { QMutexLocker l(&mtx); - if (isKeyPressed(&kCenter, keystate) && kCenter.should_process()) - emit sc.center(); - - if (isKeyPressed(&kToggle, keystate) && kToggle.should_process()) - emit sc.toggle(); - - if (isKeyPressed(&kZero, keystate) && kZero.should_process()) - emit sc.zero(); + for (int i = 0; i < 256; i++) + { + Key k; + if (keystate[i] & 0x80) + { + switch (i) + { + case DIK_LCONTROL: + case DIK_LSHIFT: + case DIK_LALT: + case DIK_RCONTROL: + case DIK_RSHIFT: + case DIK_RALT: + break; + default: + k.shift = !!(keystate[DIK_LSHIFT] & 0x80); + k.alt = !!(keystate[DIK_LALT] & 0x80); + k.ctrl = !!(keystate[DIK_LCONTROL] & 0x80); + k.keycode = i; + receiver(k); + break; + } + } + } // keypresses get dropped with high values Sleep(15); @@ -158,6 +148,30 @@ void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k) } #endif +#ifdef _WIN32 +void Shortcuts::receiver(Key &k) +{ + std::vector<K*> ks { &keyCenter, &keyToggle, &keyZero }; + for (auto& k_ : ks) + { + if (k.keycode != k_->keycode) + continue; + if (!k_->should_process()) + return; + if (k_->alt && !k.alt) return; + if (k_->ctrl && !k.ctrl) return; + if (k_->shift && !k.shift) return; + + if (k.keycode == keyCenter.keycode) + emit center(); + else if (k.keycode == keyToggle.keycode) + emit toggle(); + else if (k.keycode == keyZero.keycode) + emit zero(); + } +} +#endif + void Shortcuts::reload() { bind_keyboard_shortcut(keyCenter, s.center); bind_keyboard_shortcut(keyToggle, s.toggle); @@ -166,10 +180,8 @@ void Shortcuts::reload() { bool is_new = keybindingWorker == nullptr; if (is_new) { - keybindingWorker = std::make_shared<KeybindingWorker>(keyCenter, keyToggle, keyZero, handle, *this); + keybindingWorker = std::make_shared<KeybindingWorker>([&](Key& k) { receiver(k); }, handle); keybindingWorker->start(); } - else - keybindingWorker->set_keys(keyCenter, keyToggle, keyZero); #endif } diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index 2d7fec54..576b5384 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -9,7 +9,7 @@ #pragma once #include <QObject> #include <QWidget> -#include <QElapsedTimer> +#include "opentrack-compat/timer.hpp" #include <QThread> #include <QMessageBox> #include <QCheckBox> @@ -46,7 +46,7 @@ struct Key { bool shift; bool ctrl; bool alt; - QElapsedTimer timer; + Timer timer; public: Key() : keycode(0), shift(false), ctrl(false), alt(false) { @@ -54,7 +54,9 @@ public: bool should_process() { - return !timer.isValid() ? (timer.start(), true) : timer.restart() > 100; + bool ret = timer.elapsed_ms() > 100; + timer.start(); + return ret; } }; #else @@ -67,19 +69,15 @@ struct Shortcuts; struct KeybindingWorker : public QThread { #ifdef _WIN32 private: - Shortcuts& sc; LPDIRECTINPUT8 din; LPDIRECTINPUTDEVICE8 dinkeyboard; - Key kCenter; - Key kToggle; - Key kZero; QMutex mtx; public: volatile bool should_quit; + std::function<void(Key&)> receiver; ~KeybindingWorker(); - KeybindingWorker(Key keyCenter, Key keyToggle, Key keyZero, WId handle, Shortcuts& sc); + KeybindingWorker(std::function<void(Key&)> receiver, WId h); void run(); - void set_keys(Key kCenter, Key kToggle, Key kZero); #else public: KeybindingWorker(Key, Key, Key, WId) {} @@ -124,6 +122,9 @@ public: void reload(); private: void bind_keyboard_shortcut(K &key, key_opts& k); +#ifdef _WIN32 + void receiver(Key& k); +#endif signals: void center(); void toggle(); diff --git a/opentrack/win32-shortcuts.cpp b/opentrack/win32-shortcuts.cpp index d4107949..bd51ae88 100644 --- a/opentrack/win32-shortcuts.cpp +++ b/opentrack/win32-shortcuts.cpp @@ -111,6 +111,23 @@ QList<win_key> windows_key_sequences = win_key(DIK_SPACE, Qt::Key::Key_Space), }); +bool win_key::to_qt(const Key& k, QKeySequence& qt_, Qt::KeyboardModifiers &mods) +{ + for (auto& wk : windows_key_sequences) + { + if (wk.win == k.keycode) + { + qt_ = wk.qt; + mods = Qt::NoModifier; + if (k.ctrl) mods |= Qt::ControlModifier; + if (k.shift) mods |= Qt::ShiftModifier; + if (k.alt) mods |= Qt::AltModifier; + return true; + } + } + return false; +} + bool win_key::from_qt(QKeySequence qt_, int& dik, Qt::KeyboardModifiers& mods) { auto qt = static_cast<QVariant>(qt_).toInt(); diff --git a/opentrack/win32-shortcuts.h b/opentrack/win32-shortcuts.h index 3e824b89..fe92ae53 100644 --- a/opentrack/win32-shortcuts.h +++ b/opentrack/win32-shortcuts.h @@ -3,6 +3,7 @@ #ifdef _WIN32 #include <QKeySequence> +#include "opentrack/shortcuts.h" struct win_key; @@ -15,6 +16,7 @@ struct win_key int win; Qt::Key qt; static bool from_qt(QKeySequence qt_, int& dik, Qt::KeyboardModifiers &mods); + static bool to_qt(const Key& k, QKeySequence& qt_, Qt::KeyboardModifiers &mods); }; #endif |