diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2014-10-20 08:09:08 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2014-10-20 08:09:08 +0200 |
commit | feb12bd0eecc9f09ef7a1ab7fc60858ea519edbe (patch) | |
tree | 5e2132290e27490d6878f442a6762b406934d503 /opentrack | |
parent | f493c94a8d8e44efa9bfbccba81ae8016efb9fee (diff) |
refactor 1/2 (?)
Diffstat (limited to 'opentrack')
-rw-r--r-- | opentrack/main-settings.hpp | 20 | ||||
-rw-r--r-- | opentrack/shortcuts.cpp | 172 | ||||
-rw-r--r-- | opentrack/shortcuts.h | 139 | ||||
-rw-r--r-- | opentrack/tracker.h | 1 | ||||
-rw-r--r-- | opentrack/work.hpp | 63 |
5 files changed, 376 insertions, 19 deletions
diff --git a/opentrack/main-settings.hpp b/opentrack/main-settings.hpp index 841d3bca..e41a23c9 100644 --- a/opentrack/main-settings.hpp +++ b/opentrack/main-settings.hpp @@ -6,17 +6,6 @@ using namespace options; -struct key_opts { - value<int> key_index; - value<bool> ctrl, alt, shift; - key_opts(pbundle b, const QString& name) : - key_index(b, QString("key-index-%1").arg(name), 0), - ctrl(b, QString("key-ctrl-%1").arg(name), 0), - alt(b, QString("key-alt-%1").arg(name), 0), - shift(b, QString("key-shift-%1").arg(name), 0) - {} -}; - struct axis_opts { value<double> zero; value<bool> invert, altp; @@ -35,15 +24,11 @@ private: struct main_settings { pbundle b; - key_opts center_key; - key_opts toggle_key; value<QString> tracker_dll, tracker2_dll, filter_dll, protocol_dll; axis_opts a_x, a_y, a_z, a_yaw, a_pitch, a_roll; - value<bool> tcomp_p, tcomp_tz, dingp; + value<bool> tcomp_p, tcomp_tz; main_settings(pbundle b) : b(b), - center_key(b, "center"), - toggle_key(b, "toggle"), tracker_dll(b, "tracker-dll", ""), tracker2_dll(b, "tracker2-dll", ""), filter_dll(b, "filter-dll", ""), @@ -55,7 +40,6 @@ struct main_settings { a_pitch(b, "pitch", Pitch), a_roll(b, "roll", Roll), tcomp_p(b, "compensate-translation", true), - tcomp_tz(b, "compensate-translation-disable-z-axis", false), - dingp(b, "ding", true) + tcomp_tz(b, "compensate-translation-disable-z-axis", false) {} }; diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp new file mode 100644 index 00000000..18a3a312 --- /dev/null +++ b/opentrack/shortcuts.cpp @@ -0,0 +1,172 @@ +#include "shortcuts.h" + +KeyboardShortcutDialog::KeyboardShortcutDialog() +{ + ui.setupUi( this ); + + connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK())); + connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel())); + + for ( int i = 0; i < global_key_sequences.size(); i++) { + ui.cbxCenterKey->addItem(global_key_sequences.at(i)); + ui.cbxToggleKey->addItem(global_key_sequences.at(i)); + } + + tie_setting(s.center.key_index, ui.cbxCenterKey); + tie_setting(s.center.alt, ui.chkCenterAlt); + tie_setting(s.center.shift, ui.chkCenterShift); + tie_setting(s.center.ctrl, ui.chkCenterCtrl); + + tie_setting(s.toggle.key_index, ui.cbxToggleKey); + tie_setting(s.toggle.alt, ui.chkToggleAlt); + tie_setting(s.toggle.shift, ui.chkToggleShift); + tie_setting(s.toggle.ctrl, ui.chkToggleCtrl); +} + +void KeyboardShortcutDialog::doOK() { + s.b->save(); + this->close(); + emit reload(); +} + +void KeyboardShortcutDialog::doCancel() { + s.b->reload(); + close(); +} + +#if defined(_WIN32) +#include <windows.h> + +KeybindingWorkerImpl::~KeybindingWorkerImpl() { + should_quit = true; + wait(); + if (dinkeyboard) { + dinkeyboard->Unacquire(); + dinkeyboard->Release(); + } + if (din) + din->Release(); +} + +KeybindingWorkerImpl::KeybindingWorkerImpl(Key keyCenter, Key keyToggle) +: din(0), dinkeyboard(0), kCenter(keyCenter), kToggle(keyToggle), window(w), should_quit(true) +{ + 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) window.winId(), 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; +} + +static bool isKeyPressed( const Key *key, const BYTE *keystate ) { + bool shift; + bool ctrl; + bool alt; + + if (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; +} + +#define PROCESS_KEY(k, s) \ + if (isKeyPressed(&k, keystate) && (!k.ever_pressed ? (k.timer.start(), k.ever_pressed = true) : k.timer.restart() > 100)) \ + window.s(); + +void KeybindingWorkerImpl::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; + } + + PROCESS_KEY(kCenter, shortcutRecentered); + PROCESS_KEY(kToggle, shortcutToggled); + + Sleep(25); + } +} + +#else +#endif + +void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k) +{ +#if !defined(_WIN32) + key.setShortcut(QKeySequence::fromString("")); + key.setDisabled(); + const int idx = k.key_index; + if (idx > 0) + { + QString seq(global_key_sequences.value(idx, "")); + if (!seq.isEmpty()) + { + if (k.shift) + seq = "Shift+" + seq; + if (k.alt) + seq = "Alt+" + seq; + if (k.ctrl) + seq = "Ctrl+" + seq; + key.setShortcut(QKeySequence::fromString(seq, QKeySequence::PortableText)); + key.setEnabled(); + } else { + key.setDisabled(); + } + } +#else + int idx = k.key_index; + if (idx > 0) + { + key.keycode = 0; + key.shift = key.alt = key.ctrl = 0; + if (idx < global_windows_key_sequences.size()) + key.keycode = global_windows_key_sequences[idx]; + key.shift = k.shift; + key.alt = k.alt; + key.ctrl = k.ctrl; + } +#endif +}
\ No newline at end of file diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h new file mode 100644 index 00000000..2962f4fd --- /dev/null +++ b/opentrack/shortcuts.h @@ -0,0 +1,139 @@ +#pragma once +#include <QWidget> +#include <QElapsedTimer> +#include <QThread> +#include <QMessageBox> +#include <QCheckBox> +#include <QComboBox> +#include <QSettings> + +#include "qxt-mini/QxtGlobalShortcut" +#include "opentrack/plugin-support.h" +#include "opentrack/options.hpp" +#include "ui_ftnoir_keyboardshortcuts.h" + +using namespace options; + +extern QList<QString> global_key_sequences; + +struct key_opts { + value<int> key_index; + value<bool> ctrl, alt, shift; + key_opts(pbundle b, const QString& name) : + key_index(b, QString("key-index-%1").arg(name), 0), + ctrl(b, QString("key-ctrl-%1").arg(name), 0), + alt(b, QString("key-alt-%1").arg(name), 0), + shift(b, QString("key-shift-%1").arg(name), 0) + {} +}; + +#if defined(_WIN32) +extern QList<int> global_windows_key_sequences; +# undef DIRECTINPUT_VERSION +# define DIRECTINPUT_VERSION 0x0800 +# include <windows.h> +# include <dinput.h> + +struct Key { + BYTE keycode; + bool shift; + bool ctrl; + bool alt; + bool ever_pressed; + QElapsedTimer timer; +public: + Key() : keycode(0), shift(false), ctrl(false), alt(false), ever_pressed(false) + { + } +}; +#else +typedef unsigned char BYTE; +struct Key { int foo; }; +#endif + +#if defined(_WIN32) +class KeybindingWorkerImpl { +private: + LPDIRECTINPUT8 din; + LPDIRECTINPUTDEVICE8 dinkeyboard; + Key kCenter; + Key kToggle; +public: + volatile bool should_quit; + ~KeybindingWorkerImpl(); + KeybindingWorkerImpl(Key keyCenter, Key keyToggle); + void run(); +}; +#else +class KeybindingWorkerImpl { +public: + KeybindingWorkerImpl(Key keyCenter, Key keyToggle); + void run() {} +}; +#endif + +template<typename t_self> +struct KeybindingWorker : public QThread, public KeybindingWorkerImpl { + KeybindingWorker(Key keyCenter, Key keyToggle) : KeybindingWorkerImpl(keyCenter, keyToggle) + { + } + void run() { + KeybindingWorkerImpl::run(); + } +}; + + +struct Shortcuts { + using K = +#ifndef _WIN32 + QxtGlobalShortcut +#else + Key +#endif + ; + + K keyCenter; + K keyToggle; +#ifdef _WIN32 + ptr<KeybindingWorker> keybindingWorker; +#endif + + struct settings { + pbundle b; + key_opts center, toggle; + settings() : + b(bundle("keyboard-shortcuts")), + center(b, "center"), + toggle(b, "toggle") + {} + } s; + + Shortcuts() + { + bind_keyboard_shortcut(keyCenter, s.center); + bind_keyboard_shortcut(keyToggle, s.toggle); +#ifdef _WIN32 + keybindingWorker = nullptr; + keybindingWorker = std::make_shared<KeybindingWorker>(*this, keyCenter, keyToggle); + keybindingWorker.start(); +#endif + } +private: + void bind_keyboard_shortcut(K& key, key_opts& k); +}; + +class KeyboardShortcutDialog: public QWidget +{ + Q_OBJECT +public: + KeyboardShortcutDialog(); +private: + Ui::UICKeyboardShortcutDialog ui; + Shortcuts::settings s; + ptr<Shortcuts> sc; +signals: + void reload(); +private slots: + void doOK(); + void doCancel(); +};
\ No newline at end of file diff --git a/opentrack/tracker.h b/opentrack/tracker.h index 02d6bee2..5ec44968 100644 --- a/opentrack/tracker.h +++ b/opentrack/tracker.h @@ -20,7 +20,6 @@ class Tracker : private QThread { private: QMutex mtx; main_settings& s; - // XXX can be const-cast when functionconfig const-correct -sh 20141004 Mappings& m; Timer t; diff --git a/opentrack/work.hpp b/opentrack/work.hpp new file mode 100644 index 00000000..3e51b480 --- /dev/null +++ b/opentrack/work.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include "opentrack/main-settings.hpp" +#include "opentrack/plugin-support.h" +#include "opentrack/tracker.h" +#include "opentrack/shortcuts.h" + +#include <QObject> +#include <QFrame> +#include <memory> + +struct Modules { + Modules() : + module_list(dylib::enum_libraries()), + filter_modules(filter(dylib::Filter)), + tracker_modules(filter(dylib::Tracker)), + protocol_modules(filter(dylib::Protocol)) + {} + QList<ptr<dylib>>& filters() { return filter_modules; } + QList<ptr<dylib>>& trackers() { return tracker_modules; } + QList<ptr<dylib>>& protocols() { return protocol_modules; } +private: + QList<ptr<dylib>> module_list; + QList<ptr<dylib>> filter_modules; + QList<ptr<dylib>> tracker_modules; + QList<ptr<dylib>> protocol_modules; + + QList<ptr<dylib>> filter(dylib::Type t) + { + QList<ptr<dylib>> ret; + for (auto x : module_list) + if (x->type == t) + ret.push_back(x); + return ret; + } +}; + +struct Work +{ + main_settings& s; + SelectedLibraries libs; + ptr<Tracker> tracker; + ptr<Shortcuts> sc; + + Work(main_settings& s, Mappings& m, SelectedLibraries& libs, QObject* recv) : + s(s), libs(libs), + tracker(std::make_shared<Tracker>(s, m, libs)), + sc(std::make_shared<Shortcuts>()) + { +#ifndef _WIN32 + QObject::connect(&sc->keyCenter, SIGNAL(activated()), recv, SLOT(shortcutRecentered())); + QObject::connect(&sc->keyToggle, SIGNAL(activated()), recv, SLOT(shortcutToggled())); +#endif + tracker->start(); + } + + ~Work() + { + // order matters, otherwise use-after-free -sh + tracker = nullptr; + libs = SelectedLibraries(); + } +};
\ No newline at end of file |