summaryrefslogtreecommitdiffhomepage
path: root/opentrack/shortcuts.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-01-06 20:07:13 +0100
committerStanislaw Halik <sthalik@misaki.pl>2016-01-06 20:16:18 +0100
commit82f3d7373234cc0db79a22d476cb54b5eda7a0ea (patch)
tree65ee0194ad064cc470f95f7ca8efd533b089ca96 /opentrack/shortcuts.cpp
parent7e3807d048c5e0a8e0aa64fb49807bf5dfd11fc1 (diff)
parentf02baa0868f219076a641634625f7c032d3a9eef (diff)
Merge branch 'unstable' into trackhat
* unstable: (140 commits) tracker/pt: increase auto threshold bucket size again tracker/pt: limit max amount of extracted blobs gui: don't update main window if we're minimized tracker/pt: only show widget if the frame is visible tracker/pt: don't resize twice in widget freetrack/games: regen contrib/make-csv: perl sort isn't stable, don't ignore case tracker/pt: avoid widget temp QImage allocation spline-widget: oops, pass by reference tracker/pt: don't allocate temporary dynamic size arrays tracker/pt: don't copy points array needlessly tracker/pt: don't allocate temporary frame tracker/pt: cv::Mat::at<T> is slow, use cv::Mat::ptr tracker/pt: avoid widget malloc when able tracker/pt: optimize widget tracker/pt: update video widget at 40 -> 50 ms cmake/mingw-w64: update tracker/pt: reduce mutex contention gui: fix left margin tracker/pt: remove krap tracker/pt: move ctor out of the loop tracker/pt: nix unused tracker/pt: don't fill mask on frame pose-widget: also bilinear interpolation of alpha value ui: adjust margin ui: make more compact glwidget: use transparent octopus background api/mat: fix typos/breakage api/joy: refresh only manually on certain events pt: histogram more granular 6 -> 8 cmake/api: link with strmiids.lib on win32 tracker/pt: reduce auto thresholding histogram bucket size api/keys: prevent idempotent keys api/joy: move from header api/joy: prevent idempotent keypressed passed to receiver compat/options: get rid of std::string usage compat/options: move from header gui/settings: set parent, otherwise not modal gui/settings: don't forget to show a modal dialog before executing gui/main: don't raise a new window, it's enough to set visible api/joy: speed up poll_axis path api/joy: nix static, now that we're not a singleton tracker/joy: adapt to non-singleton joy worker joystick: no longer singleton, use fake window handle api/keys: use a fake window for DirectInput handle gui/keys: allow for pausing global keystrokes for options dialog api/keys: nix tautological #ifdef contrib/aruco: oops, right extension contrib/aruco: use @frost555's marker image api/camera-names: move to compat/ ...
Diffstat (limited to 'opentrack/shortcuts.cpp')
-rw-r--r--opentrack/shortcuts.cpp194
1 files changed, 54 insertions, 140 deletions
diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp
index a33cf088..bab1283a 100644
--- a/opentrack/shortcuts.cpp
+++ b/opentrack/shortcuts.cpp
@@ -7,112 +7,9 @@
*/
#include "shortcuts.h"
-#include <QMutexLocker>
-
-#if defined(_WIN32)
-#include <functional>
-#include <windows.h>
#include "win32-shortcuts.h"
-KeybindingWorker::~KeybindingWorker() {
- should_quit = true;
- wait();
- if (dinkeyboard) {
- dinkeyboard->Unacquire();
- dinkeyboard->Release();
- }
- if (din)
- din->Release();
-}
-
-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;
- }
- if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL) != DI_OK) {
- din->Release();
- din = 0;
- qDebug() << "setup CreateDevice function failed!" << GetLastError();
- return;
- }
- if (dinkeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK) {
- qDebug() << "setup SetDataFormat function failed!" << GetLastError();
- dinkeyboard->Release();
- dinkeyboard = 0;
- din->Release();
- din = 0;
- return;
- }
- if (dinkeyboard->SetCooperativeLevel((HWND) handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) {
- dinkeyboard->Release();
- din->Release();
- din = 0;
- dinkeyboard = 0;
- qDebug() << "setup SetCooperativeLevel function failed!" << GetLastError();
- return;
- }
- if (dinkeyboard->Acquire() != DI_OK)
- {
- dinkeyboard->Release();
- din->Release();
- din = 0;
- dinkeyboard = 0;
- qDebug() << "setup dinkeyboard Acquire failed!" << GetLastError();
- return;
- }
- should_quit = false;
-}
-
-void KeybindingWorker::run() {
- BYTE keystate[256];
-
- while (!should_quit)
- {
- if (dinkeyboard->GetDeviceState(256, (LPVOID)keystate) != DI_OK) {
- qDebug() << "Tracker::run GetDeviceState function failed!" << GetLastError();
- Sleep(25);
- continue;
- }
-
- QMutexLocker l(&mtx);
-
- 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) || !!(keystate[DIK_RSHIFT] & 0x80);
- k.alt = !!(keystate[DIK_LALT] & 0x80) || !!(keystate[DIK_RALT] & 0x80);
- k.ctrl = !!(keystate[DIK_LCONTROL] & 0x80) || !!(keystate[DIK_RCONTROL] & 0x80);
- k.keycode = i;
- receiver(k);
- break;
- }
- }
- }
-
- // keypresses get dropped with high values
- Sleep(4);
- }
-}
-#endif
-
-void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k)
+void Shortcuts::bind_keyboard_shortcut(K &key, const key_opts& k)
{
#if !defined(_WIN32)
using sh = QxtGlobalShortcut;
@@ -136,56 +33,73 @@ void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k)
key = K();
int idx = 0;
QKeySequence code;
-
- if (k.keycode == "")
- code = QKeySequence(Qt::Key_unknown);
+
+ if (k.guid != "")
+ {
+ key.guid = k.guid;
+ key.keycode = k.button & ~Qt::KeyboardModifierMask;
+ key.ctrl = !!(k.button & Qt::ControlModifier);
+ key.alt = !!(k.button & Qt::AltModifier);
+ key.shift = !!(k.button & Qt::ShiftModifier);
+ }
else
- code = QKeySequence::fromString(k.keycode, QKeySequence::PortableText);
-
- Qt::KeyboardModifiers mods = Qt::NoModifier;
- if (code != Qt::Key_unknown)
- win_key::from_qt(code, idx, mods);
- key.shift = !!(mods & Qt::ShiftModifier);
- key.alt = !!(mods & Qt::AltModifier);
- key.ctrl = !!(mods & Qt::ControlModifier);
- key.keycode = idx;
+ {
+ if (k.keycode == "")
+ code = QKeySequence(Qt::Key_unknown);
+ else
+ code = QKeySequence::fromString(k.keycode, QKeySequence::PortableText);
+
+ Qt::KeyboardModifiers mods = Qt::NoModifier;
+ if (code != Qt::Key_unknown)
+ win_key::from_qt(code, idx, mods);
+ key.shift = !!(mods & Qt::ShiftModifier);
+ key.alt = !!(mods & Qt::AltModifier);
+ key.ctrl = !!(mods & Qt::ControlModifier);
+ key.keycode = idx;
+ }
}
#endif
#ifdef _WIN32
-void Shortcuts::receiver(Key &k)
+void Shortcuts::receiver(const Key& k)
{
- std::vector<K*> ks { &keyCenter, &keyToggle, &keyZero };
- for (K* k_ : ks)
+ const int sz = keys.size();
+ for (int i = 0; i < sz; i++)
{
- if (k.keycode != k_->keycode)
+ K& k_ = std::get<0>(keys[i]);
+ auto& fun = std::get<1>(keys[i]);
+ if (k.guid != k_.guid)
+ continue;
+ if (k.keycode != k_.keycode)
+ continue;
+ if (!k.held)
continue;
- if (!k_->should_process())
+ if (!k_.should_process())
continue;
- if (k_->alt && !k.alt) continue;
- if (k_->ctrl && !k.ctrl) continue;
- if (k_->shift && !k.shift) continue;
+ if (k_.alt && !k.alt) continue;
+ if (k_.ctrl && !k.ctrl) continue;
+ if (k_.shift && !k.shift) continue;
- if (k_ == &keyCenter)
- emit center();
- else if (k_ == &keyToggle)
- emit toggle();
- else if (k_ == &keyZero)
- emit zero();
+ fun();
}
}
#endif
-void Shortcuts::reload() {
- bind_keyboard_shortcut(keyCenter, s.center);
- bind_keyboard_shortcut(keyToggle, s.toggle);
- bind_keyboard_shortcut(keyZero, s.zero);
-#ifdef _WIN32
- bool is_new = keybindingWorker == nullptr;
- if (is_new)
+void Shortcuts::reload(const std::vector<std::tuple<key_opts&, fun> > &keys_)
+{
+ const int sz = keys_.size();
+ keys = std::vector<tt>();
+
+ for (int i = 0; i < sz; i++)
{
- keybindingWorker = std::make_shared<KeybindingWorker>([&](Key& k) { receiver(k); }, handle);
- keybindingWorker->start();
- }
+ const auto& kk = keys_[i];
+ const key_opts& opts = std::get<0>(kk);
+ auto& fun = std::get<1>(kk);
+ K k;
+ bind_keyboard_shortcut(k, opts);
+ keys.push_back(std::tuple<K, Shortcuts::fun>(k, fun));
+#ifndef _WIN32
+ connect(k.get(), &QxtGlobalShortcut::activated, fun);
#endif
+ }
}