From feb12bd0eecc9f09ef7a1ab7fc60858ea519edbe Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 20 Oct 2014 08:09:08 +0200 Subject: refactor 1/2 (?) --- facetracknoir/facetracknoir.cpp | 212 ++++++++++-------------------- facetracknoir/facetracknoir.h | 44 ++----- facetracknoir/ftnoir_keyboardshortcuts.ui | 125 +++++++++--------- facetracknoir/shortcuts.cpp | 144 -------------------- facetracknoir/shortcuts.h | 83 ------------ opentrack/main-settings.hpp | 20 +-- opentrack/shortcuts.cpp | 172 ++++++++++++++++++++++++ opentrack/shortcuts.h | 139 ++++++++++++++++++++ opentrack/tracker.h | 1 - opentrack/work.hpp | 63 +++++++++ 10 files changed, 518 insertions(+), 485 deletions(-) delete mode 100644 facetracknoir/shortcuts.cpp delete mode 100644 facetracknoir/shortcuts.h create mode 100644 opentrack/shortcuts.cpp create mode 100644 opentrack/shortcuts.h create mode 100644 opentrack/work.hpp diff --git a/facetracknoir/facetracknoir.cpp b/facetracknoir/facetracknoir.cpp index 005e85f5..4c074e6e 100644 --- a/facetracknoir/facetracknoir.cpp +++ b/facetracknoir/facetracknoir.cpp @@ -28,23 +28,16 @@ FaceTrackNoIR::FaceTrackNoIR() : QMainWindow(nullptr), b(bundle("opentrack-ui")), s(b), - #if defined(_WIN32) - keybindingWorker(NULL), - #else - keyCenter(this), - keyToggle(this), - #endif pose(std::vector{&s.a_x, &s.a_y, &s.a_z, &s.a_yaw, &s.a_pitch, &s.a_roll}), timUpdateHeadPose(this), kbd_quit(QKeySequence("Ctrl+Q"), this), - no_feed_pixmap(":/uielements/no-feed.png"), - module_list(dylib::enum_libraries()) + no_feed_pixmap(":/uielements/no-feed.png") { ui.setupUi(this); + setFixedSize(size()); - ui.video_frame_label->setPixmap(no_feed_pixmap); updateButtonState(false, false); - + ui.video_frame_label->setPixmap(no_feed_pixmap); QDir::setCurrent(QCoreApplication::applicationDirPath()); connect(ui.btnLoad, SIGNAL(clicked()), this, SLOT(open())); @@ -57,12 +50,17 @@ FaceTrackNoIR::FaceTrackNoIR() : QMainWindow(nullptr), connect(ui.btnShowServerControls, SIGNAL(clicked()), this, SLOT(showServerControls())); connect(ui.btnShowFilterControls, SIGNAL(clicked()), this, SLOT(showFilterControls())); - filter_modules.push_back(nullptr); + modules.filters().push_back(nullptr); ui.iconcomboFilter->addItem(QIcon(), ""); - fill_combobox(dylib::Tracker, module_list, tracker_modules, ui.iconcomboTrackerSource); - fill_combobox(dylib::Protocol, module_list, protocol_modules, ui.iconcomboProtocol); - fill_combobox(dylib::Filter, module_list, filter_modules, ui.iconcomboFilter); + for (auto x : modules.trackers()) + ui.iconcomboTrackerSource->addItem(x->icon, x->name); + + for (auto x : modules.protocols()) + ui.iconcomboProtocol->addItem(x->icon, x->name); + + for (auto x : modules.filters()) + ui.iconcomboFilter->addItem(x->icon, x->name); fill_profile_combobox(); @@ -72,15 +70,9 @@ FaceTrackNoIR::FaceTrackNoIR() : QMainWindow(nullptr), connect(ui.btnStartTracker, SIGNAL(clicked()), this, SLOT(startTracker())); connect(ui.btnStopTracker, SIGNAL(clicked()), this, SLOT(stopTracker())); - connect(ui.iconcomboProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(profileSelected(int))); + connect(&timUpdateHeadPose, SIGNAL(timeout()), this, SLOT(showHeadPose())); - -#ifndef _WIN32 - connect(&keyCenter, SIGNAL(activated()), this, SLOT(shortcutRecentered())); - connect(&keyToggle, SIGNAL(activated()), this, SLOT(shortcutToggled())); -#endif - connect(&kbd_quit, SIGNAL(activated()), this, SLOT(exit())); kbd_quit.setEnabled(true); } @@ -91,25 +83,6 @@ FaceTrackNoIR::~FaceTrackNoIR() save(); } -void FaceTrackNoIR::fill_combobox(dylib::Type t, - QList> list, - QList>& out_list, - QComboBox* cbx) -{ - for (auto x : list) - { - if (t == x->type) - { - cbx->addItem(x->icon, x->name); - out_list.append(x); - } - } -} - -QFrame* FaceTrackNoIR::video_frame() { - return ui.video_frame; -} - void FaceTrackNoIR::open() { QFileDialog dialog(this); dialog.setFileMode(QFileDialog::ExistingFile); @@ -226,15 +199,27 @@ void FaceTrackNoIR::updateButtonState(bool running, bool inertialp) ui.video_frame_label->setVisible(not_running || inertialp); } +void FaceTrackNoIR::bindKeyboardShortcuts() +{ + if (work) + work->sc = std::make_shared(); +} + void FaceTrackNoIR::startTracker( ) { b->save(); loadSettings(); bindKeyboardShortcuts(); - // Tracker dtor needs run first - tracker = nullptr; + // tracker dtor needs run first + work = nullptr; - libs = SelectedLibraries(video_frame(), current_tracker(), current_protocol(), current_filter()); + libs = SelectedLibraries(ui.video_frame, current_tracker(), current_protocol(), current_filter()); + work = std::make_shared(s, pose, libs, this); + + { + double p[6] = {0,0,0, 0,0,0}; + display_pose(p, p); + } if (!libs.correct) { @@ -252,8 +237,6 @@ void FaceTrackNoIR::startTracker( ) { ui.video_frame->show(); timUpdateHeadPose.start(50); - tracker = std::make_shared(s, pose, libs); - tracker->start(); // NB check valid since SelectedLibraries ctor called // trackers take care of layout state updates @@ -293,41 +276,53 @@ void FaceTrackNoIR::stopTracker( ) { pFilterDialog = nullptr; } - tracker = nullptr; + work = nullptr; libs = SelectedLibraries(); + + { + double p[6] = {0,0,0, 0,0,0}; + display_pose(p, p); + } updateButtonState(false, false); } -void FaceTrackNoIR::showHeadPose() +void FaceTrackNoIR::display_pose(const double *mapped, const double *raw) { - double mapped[6], raw[6]; - - tracker->get_raw_and_mapped_poses(mapped, raw); - ui.pose_display->rotateBy(mapped[Yaw], mapped[Roll], mapped[Pitch]); if (mapping_widget) mapping_widget->update(); + double mapped_[6], raw_[6]; + for (int i = 0; i < 6; i++) { - mapped[i] = (int) mapped[i]; - raw[i] = (int) raw[i]; + mapped_[i] = (int) mapped[i]; + raw_[i] = (int) raw[i]; } - ui.lcdNumX->display(raw[TX]); - ui.lcdNumY->display(raw[TY]); - ui.lcdNumZ->display(raw[TZ]); - ui.lcdNumRotX->display(raw[Yaw]); - ui.lcdNumRotY->display(raw[Pitch]); - ui.lcdNumRotZ->display(raw[Roll]); + ui.lcdNumX->display(raw_[TX]); + ui.lcdNumY->display(raw_[TY]); + ui.lcdNumZ->display(raw_[TZ]); + ui.lcdNumRotX->display(raw_[Yaw]); + ui.lcdNumRotY->display(raw_[Pitch]); + ui.lcdNumRotZ->display(raw_[Roll]); + + ui.lcdNumOutputPosX->display(mapped_[TX]); + ui.lcdNumOutputPosY->display(mapped_[TY]); + ui.lcdNumOutputPosZ->display(mapped_[TZ]); + ui.lcdNumOutputRotX->display(mapped_[Yaw]); + ui.lcdNumOutputRotY->display(mapped_[Pitch]); + ui.lcdNumOutputRotZ->display(mapped_[Roll]); +} + +void FaceTrackNoIR::showHeadPose() +{ + double mapped[6], raw[6]; + + work->tracker->get_raw_and_mapped_poses(mapped, raw); - ui.lcdNumOutputPosX->display(mapped[TX]); - ui.lcdNumOutputPosY->display(mapped[TY]); - ui.lcdNumOutputPosZ->display(mapped[TZ]); - ui.lcdNumOutputRotX->display(mapped[Yaw]); - ui.lcdNumOutputRotY->display(mapped[Pitch]); - ui.lcdNumOutputRotZ->display(mapped[Roll]); + display_pose(mapped, raw); if (libs.pProtocol) { @@ -338,7 +333,7 @@ void FaceTrackNoIR::showHeadPose() void FaceTrackNoIR::showTrackerSettings() { - ptr lib = tracker_modules.value(ui.iconcomboTrackerSource->currentIndex(), nullptr); + ptr lib = modules.trackers().value(ui.iconcomboTrackerSource->currentIndex(), nullptr); if (lib) { pTrackerDialog = ptr(reinterpret_cast(lib->Dialog())); @@ -349,7 +344,7 @@ void FaceTrackNoIR::showTrackerSettings() } void FaceTrackNoIR::showServerControls() { - ptr lib = protocol_modules.value(ui.iconcomboProtocol->currentIndex(), nullptr); + ptr lib = modules.protocols().value(ui.iconcomboProtocol->currentIndex(), nullptr); if (lib) { pProtocolDialog = ptr(reinterpret_cast(lib->Dialog())); @@ -359,7 +354,7 @@ void FaceTrackNoIR::showServerControls() { } void FaceTrackNoIR::showFilterControls() { - ptr lib = filter_modules.value(ui.iconcomboFilter->currentIndex(), nullptr); + ptr lib = modules.filters().value(ui.iconcomboFilter->currentIndex(), nullptr); if (lib) { pFilterDialog = ptr(reinterpret_cast(lib->Dialog())); @@ -369,9 +364,10 @@ void FaceTrackNoIR::showFilterControls() { } } void FaceTrackNoIR::showKeyboardShortcuts() { - shortcuts_widget = std::make_shared( this, this ); + shortcuts_widget = std::make_shared(); shortcuts_widget->show(); shortcuts_widget->raise(); + connect(shortcuts_widget.get(), SIGNAL(reload()), this, SLOT(bindKeyboardShortcuts())); } void FaceTrackNoIR::showCurveConfiguration() { mapping_widget = std::make_shared(pose, s, this); @@ -394,86 +390,16 @@ void FaceTrackNoIR::profileSelected(int index) loadSettings(); } -#if !defined(_WIN32) -void FaceTrackNoIR::bind_keyboard_shortcut(QxtGlobalShortcut& key, key_opts& k) -{ - 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 -static void bind_keyboard_shortcut(Key& key, key_opts& k) -{ - 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 - -void FaceTrackNoIR::bindKeyboardShortcuts() -{ -#if !defined(_WIN32) - bind_keyboard_shortcut(keyCenter, s.center_key); - bind_keyboard_shortcut(keyToggle, s.toggle_key); -#else - bind_keyboard_shortcut(keyCenter, s.center_key); - bind_keyboard_shortcut(keyToggle, s.toggle_key); -#endif - if (tracker) /* running already */ - { -#if defined(_WIN32) - if (keybindingWorker) - { - keybindingWorker->should_quit = true; - keybindingWorker->wait(); - delete keybindingWorker; - keybindingWorker = NULL; - } - keybindingWorker = new KeybindingWorker(*this, keyCenter, keyToggle); - keybindingWorker->start(); -#endif - } -} - void FaceTrackNoIR::shortcutRecentered() { qDebug() << "Center"; - if (s.dingp) - QApplication::beep(); - if (tracker) - tracker->center(); + if (work) + work->tracker->center(); } void FaceTrackNoIR::shortcutToggled() { qDebug() << "Toggle"; - if (s.dingp) - QApplication::beep(); - if (tracker) - tracker->toggle_enabled(); + if (work) + work->tracker->toggle_enabled(); } diff --git a/facetracknoir/facetracknoir.h b/facetracknoir/facetracknoir.h index 32bf8239..13c2bb59 100644 --- a/facetracknoir/facetracknoir.h +++ b/facetracknoir/facetracknoir.h @@ -48,7 +48,8 @@ #include "opentrack/main-settings.hpp" #include "opentrack/plugin-support.h" #include "opentrack/tracker.h" -#include "shortcuts.h" +#include "opentrack/shortcuts.h" +#include "opentrack/work.hpp" #include "curve-config.h" using namespace options; @@ -59,70 +60,54 @@ class FaceTrackNoIR : public QMainWindow public: pbundle b; main_settings s; - ptr tracker; - - // XXX move kbd handling into class its own -sh 20141019 -#ifndef _WIN32 - void bind_keyboard_shortcut(QxtGlobalShortcut&, key_opts& k); -#endif private: -#if defined(_WIN32) - Key keyCenter; - Key keyToggle; - KeybindingWorker* keybindingWorker; -#else - QxtGlobalShortcut keyCenter; - QxtGlobalShortcut keyToggle; -#endif // XXX move the shit outta the _widget_, establish a class // for running tracker state, etc -sh 20141014 Mappings pose; Ui::OpentrackUI ui; + QTimer timUpdateHeadPose; SelectedLibraries libs; - ptr pTrackerDialog; - ptr pProtocolDialog; - ptr pFilterDialog; + ptr work; ptr shortcuts_widget; ptr mapping_widget; QShortcut kbd_quit; QPixmap no_feed_pixmap; - - QList> filter_modules; - QList> tracker_modules; - QList> protocol_modules; - - QList> module_list; + ptr pFilterDialog; + ptr pProtocolDialog; + ptr pTrackerDialog; + Modules modules; // XXX this shit stinks -sh 20141004 // TODO move to separate class representing running tracker state ptr current_tracker() { - return tracker_modules.value(ui.iconcomboTrackerSource->currentIndex(), nullptr); + return modules.trackers().value(ui.iconcomboTrackerSource->currentIndex(), nullptr); } ptr current_protocol() { - return protocol_modules.value(ui.iconcomboProtocol->currentIndex(), nullptr); + return modules.protocols().value(ui.iconcomboProtocol->currentIndex(), nullptr); } ptr current_filter() { - return filter_modules.value(ui.iconcomboFilter->currentIndex(), nullptr); + return modules.filters().value(ui.iconcomboFilter->currentIndex(), nullptr); } void createIconGroupBox(); void loadSettings(); void updateButtonState(bool running, bool inertialp); - void fill_combobox(dylib::Type t, QList> list, QList > &out_list, QComboBox* cbx); void fill_profile_combobox(); - QFrame *video_frame(); + void display_pose(const double* mapped, const double* raw); + public slots: void shortcutRecentered(); void shortcutToggled(); + void bindKeyboardShortcuts(); private slots: void open(); void save(); @@ -146,5 +131,4 @@ public: ~FaceTrackNoIR(); void save_mappings(); void load_mappings(); - void bindKeyboardShortcuts(); }; diff --git a/facetracknoir/ftnoir_keyboardshortcuts.ui b/facetracknoir/ftnoir_keyboardshortcuts.ui index 245b503a..f576d8fb 100644 --- a/facetracknoir/ftnoir_keyboardshortcuts.ui +++ b/facetracknoir/ftnoir_keyboardshortcuts.ui @@ -6,8 +6,8 @@ 0 0 - 371 - 125 + 415 + 143 @@ -30,6 +30,38 @@ false + + + + + 0 + 0 + + + + Toggle + + + false + + + + + + + + 0 + 0 + + + + Center + + + false + + + @@ -59,22 +91,6 @@ - - - - - 0 - 0 - - - - Toggle - - - false - - - @@ -88,8 +104,8 @@ - - + + 50 @@ -101,22 +117,6 @@ - - - - - 90 - 0 - - - - Select Number - - - QComboBox::InsertAlphabetically - - - @@ -136,15 +136,8 @@ - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - + + 50 @@ -152,28 +145,19 @@ - Ctrl + Alt - - - - - 0 - 0 - - - - Center - - - false + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - + + 50 @@ -181,7 +165,7 @@ - Alt + Ctrl @@ -198,10 +182,19 @@ - - - - Ding! + + + + + 90 + 0 + + + + Select Number + + + QComboBox::InsertAlphabetically diff --git a/facetracknoir/shortcuts.cpp b/facetracknoir/shortcuts.cpp deleted file mode 100644 index 53d67957..00000000 --- a/facetracknoir/shortcuts.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#include "facetracknoir.h" -#include "shortcuts.h" - -// XXX todo remove ref to ui class -sh 20141019 - -KeyboardShortcutDialog::KeyboardShortcutDialog( FaceTrackNoIR *ftnoir, QWidget *parent ) - : QWidget( parent, Qt::Dialog) -{ - ui.setupUi( this ); - - QPoint offsetpos(100, 100); - this->move(parent->pos() + offsetpos); - - mainApp = ftnoir; - - 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(mainApp->s.center_key.key_index, ui.cbxCenterKey); - tie_setting(mainApp->s.center_key.alt, ui.chkCenterAlt); - tie_setting(mainApp->s.center_key.shift, ui.chkCenterShift); - tie_setting(mainApp->s.center_key.ctrl, ui.chkCenterCtrl); - - tie_setting(mainApp->s.toggle_key.key_index, ui.cbxToggleKey); - tie_setting(mainApp->s.toggle_key.alt, ui.chkToggleAlt); - tie_setting(mainApp->s.toggle_key.shift, ui.chkToggleShift); - tie_setting(mainApp->s.toggle_key.ctrl, ui.chkToggleCtrl); - - tie_setting(mainApp->s.dingp, ui.ding); -} - -void KeyboardShortcutDialog::doOK() { - mainApp->b->save(); - this->close(); - if (mainApp->tracker) - mainApp->bindKeyboardShortcuts(); -} - -void KeyboardShortcutDialog::doCancel() { - mainApp->s.b->reload(); - close(); -} - -#if defined(_WIN32) -#include - -KeybindingWorkerImpl::~KeybindingWorkerImpl() { - if (dinkeyboard) { - dinkeyboard->Unacquire(); - dinkeyboard->Release(); - } - if (din) - din->Release(); -} - -KeybindingWorkerImpl::KeybindingWorkerImpl(FaceTrackNoIR& w, 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 diff --git a/facetracknoir/shortcuts.h b/facetracknoir/shortcuts.h deleted file mode 100644 index 8e665211..00000000 --- a/facetracknoir/shortcuts.h +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include "ui_ftnoir_keyboardshortcuts.h" - -class FaceTrackNoIR; - -class KeyboardShortcutDialog: public QWidget -{ - Q_OBJECT -public: - KeyboardShortcutDialog( FaceTrackNoIR *ftnoir, QWidget *parent ); -private: - Ui::UICKeyboardShortcutDialog ui; - FaceTrackNoIR *mainApp; -private slots: - void doOK(); - void doCancel(); -}; - -extern QList global_key_sequences; - -#if defined(_WIN32) -extern QList global_windows_key_sequences; -# undef DIRECTINPUT_VERSION -# define DIRECTINPUT_VERSION 0x0800 -# include -# include - -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; - FaceTrackNoIR& window; -public: - volatile bool should_quit; - ~KeybindingWorkerImpl(); - KeybindingWorkerImpl(FaceTrackNoIR& w, Key keyCenter, Key keyToggle); - void run(); -}; -#else -class KeybindingWorkerImpl { -public: - KeybindingWorkerImpl(FaceTrackNoIR& w, Key keyCenter, Key keyToggle); - void run() {} -}; -#endif - -class KeybindingWorker : public QThread, public KeybindingWorkerImpl { - Q_OBJECT -public: - KeybindingWorker(FaceTrackNoIR& w, Key keyCenter, Key keyToggle) : KeybindingWorkerImpl(w, keyCenter, keyToggle) - { - } - void run() { - KeybindingWorkerImpl::run(); - } -}; 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 key_index; - value 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 zero; value invert, altp; @@ -35,15 +24,11 @@ private: struct main_settings { pbundle b; - key_opts center_key; - key_opts toggle_key; value tracker_dll, tracker2_dll, filter_dll, protocol_dll; axis_opts a_x, a_y, a_z, a_yaw, a_pitch, a_roll; - value tcomp_p, tcomp_tz, dingp; + value 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 + +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 +#include +#include +#include +#include +#include +#include + +#include "qxt-mini/QxtGlobalShortcut" +#include "opentrack/plugin-support.h" +#include "opentrack/options.hpp" +#include "ui_ftnoir_keyboardshortcuts.h" + +using namespace options; + +extern QList global_key_sequences; + +struct key_opts { + value key_index; + value 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 global_windows_key_sequences; +# undef DIRECTINPUT_VERSION +# define DIRECTINPUT_VERSION 0x0800 +# include +# include + +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 +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; +#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(*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 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 +#include +#include + +struct Modules { + Modules() : + module_list(dylib::enum_libraries()), + filter_modules(filter(dylib::Filter)), + tracker_modules(filter(dylib::Tracker)), + protocol_modules(filter(dylib::Protocol)) + {} + QList>& filters() { return filter_modules; } + QList>& trackers() { return tracker_modules; } + QList>& protocols() { return protocol_modules; } +private: + QList> module_list; + QList> filter_modules; + QList> tracker_modules; + QList> protocol_modules; + + QList> filter(dylib::Type t) + { + QList> 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; + ptr sc; + + Work(main_settings& s, Mappings& m, SelectedLibraries& libs, QObject* recv) : + s(s), libs(libs), + tracker(std::make_shared(s, m, libs)), + sc(std::make_shared()) + { +#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 -- cgit v1.2.3