diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2014-10-20 09:33:56 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2014-10-20 09:33:56 +0200 |
commit | ba0cd4a1fd743a7d0d025ca76c211c8f466e6879 (patch) | |
tree | 2bbd5f1939a7413e76cbe782e3da7b7c844cea8c | |
parent | 7f0d0d1b9ef7711a4f1835b0120f014799b71e90 (diff) |
refactor 2/n
-rw-r--r-- | facetracknoir/facetracknoir.cpp | 19 | ||||
-rw-r--r-- | opentrack/shortcuts.cpp | 17 | ||||
-rw-r--r-- | opentrack/shortcuts.h | 36 | ||||
-rw-r--r-- | opentrack/work.hpp | 9 |
4 files changed, 33 insertions, 48 deletions
diff --git a/facetracknoir/facetracknoir.cpp b/facetracknoir/facetracknoir.cpp index c547f608..5170498c 100644 --- a/facetracknoir/facetracknoir.cpp +++ b/facetracknoir/facetracknoir.cpp @@ -199,7 +199,7 @@ void FaceTrackNoIR::updateButtonState(bool running, bool inertialp) void FaceTrackNoIR::bindKeyboardShortcuts() { if (work) - work->sc = std::make_shared<Shortcuts>(); + work->sc = std::make_shared<Shortcuts>(winId()); } void FaceTrackNoIR::startTracker( ) { @@ -211,7 +211,7 @@ void FaceTrackNoIR::startTracker( ) { work = nullptr; libs = SelectedLibraries(ui.video_frame, current_tracker(), current_protocol(), current_filter()); - work = std::make_shared<Work>(s, pose, libs, this); + work = std::make_shared<Work>(s, pose, libs, this, winId()); { double p[6] = {0,0,0, 0,0,0}; @@ -227,11 +227,6 @@ void FaceTrackNoIR::startTracker( ) { return; } -#if defined(_WIN32) - keybindingWorker = new KeybindingWorker(*this, keyCenter, keyToggle); - keybindingWorker->start(); -#endif - ui.video_frame->show(); timUpdateHeadPose.start(50); @@ -243,15 +238,7 @@ void FaceTrackNoIR::startTracker( ) { void FaceTrackNoIR::stopTracker( ) { ui.game_name->setText("Not connected"); -#if defined(_WIN32) - if (keybindingWorker) - { - keybindingWorker->should_quit = true; - keybindingWorker->wait(); - delete keybindingWorker; - keybindingWorker = NULL; - } -#endif + timUpdateHeadPose.stop(); ui.pose_display->rotateBy(0, 0, 0); diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp index 18a3a312..0a3d7e0d 100644 --- a/opentrack/shortcuts.cpp +++ b/opentrack/shortcuts.cpp @@ -37,7 +37,7 @@ void KeyboardShortcutDialog::doCancel() { #if defined(_WIN32) #include <windows.h> -KeybindingWorkerImpl::~KeybindingWorkerImpl() { +KeybindingWorker::~KeybindingWorker() { should_quit = true; wait(); if (dinkeyboard) { @@ -48,8 +48,8 @@ KeybindingWorkerImpl::~KeybindingWorkerImpl() { din->Release(); } -KeybindingWorkerImpl::KeybindingWorkerImpl(Key keyCenter, Key keyToggle) -: din(0), dinkeyboard(0), kCenter(keyCenter), kToggle(keyToggle), window(w), should_quit(true) +KeybindingWorker::KeybindingWorker(Key keyCenter, Key keyToggle, WId handle) : + din(0), dinkeyboard(0), kCenter(keyCenter), kToggle(keyToggle), should_quit(true) { if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL) != DI_OK) { qDebug() << "setup DirectInput8 Creation failed!" << GetLastError(); @@ -69,8 +69,7 @@ KeybindingWorkerImpl::KeybindingWorkerImpl(Key keyCenter, Key keyToggle) din = 0; return; } - - if (dinkeyboard->SetCooperativeLevel((HWND) window.winId(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { + if (dinkeyboard->SetCooperativeLevel((HWND) handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { dinkeyboard->Release(); din->Release(); din = 0; @@ -111,9 +110,9 @@ static bool isKeyPressed( const Key *key, const BYTE *keystate ) { #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(); + emit s; -void KeybindingWorkerImpl::run() { +void KeybindingWorker::run() { BYTE keystate[256]; while (!should_quit) { @@ -123,8 +122,8 @@ void KeybindingWorkerImpl::run() { continue; } - PROCESS_KEY(kCenter, shortcutRecentered); - PROCESS_KEY(kToggle, shortcutToggled); + PROCESS_KEY(kCenter, center()); + PROCESS_KEY(kToggle, toggle()); Sleep(25); } diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index 2962f4fd..54305a52 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -51,8 +51,9 @@ typedef unsigned char BYTE; struct Key { int foo; }; #endif -#if defined(_WIN32) -class KeybindingWorkerImpl { +struct KeybindingWorker : public QThread { + Q_OBJECT +#ifdef _WIN32 private: LPDIRECTINPUT8 din; LPDIRECTINPUTDEVICE8 dinkeyboard; @@ -60,26 +61,17 @@ private: Key kToggle; public: volatile bool should_quit; - ~KeybindingWorkerImpl(); - KeybindingWorkerImpl(Key keyCenter, Key keyToggle); + ~KeybindingWorker(); + KeybindingWorker(Key keyCenter, Key keyToggle, WId handle); void run(); -}; #else -class KeybindingWorkerImpl { public: - KeybindingWorkerImpl(Key keyCenter, Key keyToggle); + KeybindingWorker(Key, Key, WId) {} 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(); - } +signals: + void center(); + void toggle(); }; @@ -108,14 +100,18 @@ struct Shortcuts { {} } s; - Shortcuts() +#ifdef _WIN32 + Shortcuts(WId handle) +#else + Shortcuts(WId) +#endif { 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(); + keybindingWorker = std::make_shared<KeybindingWorker>(keyCenter, keyToggle, handle); + keybindingWorker->start(); #endif } private: diff --git a/opentrack/work.hpp b/opentrack/work.hpp index bd69f7ba..c0962b9a 100644 --- a/opentrack/work.hpp +++ b/opentrack/work.hpp @@ -16,14 +16,17 @@ struct Work ptr<Tracker> tracker; ptr<Shortcuts> sc; - Work(main_settings& s, Mappings& m, SelectedLibraries& libs, QObject* recv) : + Work(main_settings& s, Mappings& m, SelectedLibraries& libs, QObject* recv, WId handle) : s(s), libs(libs), tracker(std::make_shared<Tracker>(s, m, libs)), - sc(std::make_shared<Shortcuts>()) + sc(std::make_shared<Shortcuts>(handle)) { #ifndef _WIN32 QObject::connect(&sc->keyCenter, SIGNAL(activated()), recv, SLOT(shortcutRecentered())); QObject::connect(&sc->keyToggle, SIGNAL(activated()), recv, SLOT(shortcutToggled())); +#else + QObject::connect(sc->keybindingWorker.get(), SIGNAL(center()), recv, SLOT(shortcutRecentered())); + QObject::connect(sc->keybindingWorker.get(), SIGNAL(toggle()), recv, SLOT(shortcutToggled())); #endif tracker->start(); } @@ -34,4 +37,4 @@ struct Work tracker = nullptr; libs = SelectedLibraries(); } -};
\ No newline at end of file +}; |