From 6e7af85c52e994fada7f989f4af2a6e793dedac9 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 10 Aug 2016 12:02:00 +0200 Subject: dinput: split from logic module For use in the joystick module. --- gui/keyboard.h | 2 +- opentrack-dinput/CMakeLists.txt | 4 + opentrack-dinput/dinput.cpp | 47 ++++ opentrack-dinput/dinput.hpp | 28 +++ opentrack-dinput/export.hpp | 28 +++ opentrack-dinput/keybinding-worker.cpp | 189 +++++++++++++++ opentrack-dinput/keybinding-worker.hpp | 82 +++++++ opentrack-dinput/win32-joystick.cpp | 362 +++++++++++++++++++++++++++++ opentrack-dinput/win32-joystick.hpp | 93 ++++++++ opentrack-logic/CMakeLists.txt | 2 +- opentrack-logic/dinput.cpp | 47 ---- opentrack-logic/dinput.hpp | 28 --- opentrack-logic/keybinding-worker.cpp | 189 --------------- opentrack-logic/keybinding-worker.hpp | 80 ------- opentrack-logic/shortcuts.h | 2 +- opentrack-logic/win32-joystick.cpp | 362 ----------------------------- opentrack-logic/win32-joystick.hpp | 93 -------- tracker-joystick/CMakeLists.txt | 2 +- tracker-joystick/ftnoir_tracker_joystick.h | 2 +- 19 files changed, 838 insertions(+), 804 deletions(-) create mode 100644 opentrack-dinput/CMakeLists.txt create mode 100644 opentrack-dinput/dinput.cpp create mode 100644 opentrack-dinput/dinput.hpp create mode 100644 opentrack-dinput/export.hpp create mode 100644 opentrack-dinput/keybinding-worker.cpp create mode 100644 opentrack-dinput/keybinding-worker.hpp create mode 100644 opentrack-dinput/win32-joystick.cpp create mode 100644 opentrack-dinput/win32-joystick.hpp delete mode 100644 opentrack-logic/dinput.cpp delete mode 100644 opentrack-logic/dinput.hpp delete mode 100644 opentrack-logic/keybinding-worker.cpp delete mode 100644 opentrack-logic/keybinding-worker.hpp delete mode 100644 opentrack-logic/win32-joystick.cpp delete mode 100644 opentrack-logic/win32-joystick.hpp diff --git a/gui/keyboard.h b/gui/keyboard.h index 07f326ea..27300690 100644 --- a/gui/keyboard.h +++ b/gui/keyboard.h @@ -2,7 +2,7 @@ #include "ui_keyboard_listener.h" #ifdef _WIN32 #include "opentrack-logic/win32-shortcuts.h" -#include "opentrack-logic/keybinding-worker.hpp" +#include "opentrack-dinput/keybinding-worker.hpp" #endif #include #include diff --git a/opentrack-dinput/CMakeLists.txt b/opentrack-dinput/CMakeLists.txt new file mode 100644 index 00000000..ed0b5990 --- /dev/null +++ b/opentrack-dinput/CMakeLists.txt @@ -0,0 +1,4 @@ +if(WIN32) + opentrack_boilerplate(opentrack-dinput BIN) + target_link_libraries(opentrack-dinput dinput) +endif() diff --git a/opentrack-dinput/dinput.cpp b/opentrack-dinput/dinput.cpp new file mode 100644 index 00000000..ce80fe0e --- /dev/null +++ b/opentrack-dinput/dinput.cpp @@ -0,0 +1,47 @@ +#ifdef _WIN32 + +#include "dinput.hpp" +#include + +dinput_handle dinput_handle::self; + +dinput_handle::dinput_handle() : handle(init_di()) +{ +} + +dinput_handle::~dinput_handle() +{ + if (handle) + { + handle->Release(); + handle = nullptr; + } +} + +dinput_handle::di_t dinput_handle::init_di() +{ + HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); + if (FAILED(hr)) + qDebug() << "dinput: failed CoInitializeEx" << hr << GetLastError(); + + static LPDIRECTINPUT8 di_ = nullptr; + if (di_ == nullptr) + { + if (SUCCEEDED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&di_, NULL))) + { + return di_; + } + else + { + return di_ = nullptr; + } + } + return di_; +} + +dinput_handle::di_t dinput_handle::make_di() +{ + return self.handle; +} + +#endif diff --git a/opentrack-dinput/dinput.hpp b/opentrack-dinput/dinput.hpp new file mode 100644 index 00000000..db901887 --- /dev/null +++ b/opentrack-dinput/dinput.hpp @@ -0,0 +1,28 @@ +#pragma once + +#ifdef _WIN32 + +#ifndef DIRECTINPUT_VERSION +# define DIRECTINPUT_VERSION 0x800 +#endif +#include "export.hpp" +#include +#include + +struct OPENTRACK_DINPUT_EXPORT dinput_handle final +{ + using di_t = LPDIRECTINPUT8; +private: + static dinput_handle self; + dinput_handle(); + ~dinput_handle(); + static di_t init_di(); + di_t handle; +public: + static di_t make_di(); + + dinput_handle(const dinput_handle&) = delete; + dinput_handle(dinput_handle&&) = delete; +}; + +#endif diff --git a/opentrack-dinput/export.hpp b/opentrack-dinput/export.hpp new file mode 100644 index 00000000..51ee4531 --- /dev/null +++ b/opentrack-dinput/export.hpp @@ -0,0 +1,28 @@ +#pragma once + +#ifdef BUILD_dinput +# ifdef _WIN32 +# define OPENTRACK_DINPUT_LINKAGE __declspec(dllexport) +# else +# define OPENTRACK_DINPUT_LINKAGE +# endif + +# ifndef _MSC_VER +# define OPENTRACK_DINPUT_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_DINPUT_LINKAGE +# else +# define OPENTRACK_DINPUT_EXPORT OPENTRACK_DINPUT_LINKAGE +# endif + +#else +#ifdef _WIN32 +# define OPENTRACK_DINPUT_LINKAGE __declspec(dllimport) +#else +# define OPENTRACK_DINPUT_LINKAGE +#endif + +#ifndef _MSC_VER +# define OPENTRACK_DINPUT_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_DINPUT_LINKAGE +#else +# define OPENTRACK_DINPUT_EXPORT OPENTRACK_DINPUT_LINKAGE +#endif +#endif diff --git a/opentrack-dinput/keybinding-worker.cpp b/opentrack-dinput/keybinding-worker.cpp new file mode 100644 index 00000000..a009d399 --- /dev/null +++ b/opentrack-dinput/keybinding-worker.cpp @@ -0,0 +1,189 @@ +/* Copyright (c) 2014-2015, Stanislaw Halik + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#ifdef _WIN32 + +#include "keybinding-worker.hpp" +#include +#include +#include +#include + +bool Key::should_process() +{ + if (!enabled || (keycode == 0 && guid == "")) + return false; + bool ret = timer.elapsed_ms() > 100; + timer.start(); + return ret; +} + +KeybindingWorker::~KeybindingWorker() +{ + should_quit = true; + wait(); + if (dinkeyboard) { + dinkeyboard->Unacquire(); + dinkeyboard->Release(); + } +} + +KeybindingWorker::KeybindingWorker() : should_quit(true) +{ + LPDIRECTINPUT8 din = dinput_handle::make_di(); + + if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL) != DI_OK) { + qDebug() << "setup CreateDevice function failed!" << GetLastError(); + return; + } + + if (dinkeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK) { + qDebug() << "setup SetDataFormat function failed!" << GetLastError(); + dinkeyboard->Release(); + dinkeyboard = 0; + return; + } + + if (dinkeyboard->SetCooperativeLevel((HWND) fake_main_window.winId(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { + dinkeyboard->Release(); + dinkeyboard = 0; + qDebug() << "setup SetCooperativeLevel function failed!" << GetLastError(); + return; + } + + if (dinkeyboard->Acquire() != DI_OK) + { + dinkeyboard->Release(); + dinkeyboard = 0; + qDebug() << "setup dinkeyboard Acquire failed!" << GetLastError(); + return; + } + + should_quit = false; + start(); +} + +KeybindingWorker& KeybindingWorker::make() +{ + static KeybindingWorker k; + return k; +} + +void KeybindingWorker::run() +{ + BYTE keystate[256] = {0}; + BYTE old_keystate[256] = {0}; + + while (!should_quit) + { + { + QMutexLocker l(&mtx); + + if (receivers.size()) + { + { + const HRESULT hr = dinkeyboard->GetDeviceState(256, (LPVOID)keystate); + + if (hr != DI_OK) { + qDebug() << "Tracker::run GetDeviceState function failed!" << GetLastError(); + Sleep(25); + continue; + } + } + + { + using joy_fn = std::function; + + joy_fn f = [&](const QString& guid, int idx, bool held) -> void { + Key k; + k.keycode = idx; + 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.guid = guid; + k.held = held; + + for (auto& r : receivers) + r->operator()(k); + }; + + joy_ctx.poll(f); + } + + for (int i = 0; i < 256; i++) + { + Key k; + if (old_keystate[i] != keystate[i]) + { + const bool held = 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; + k.held = held; + + for (auto& r : receivers) + r->operator()(k); + break; + } + } + old_keystate[i] = keystate[i]; + } + } + } + + // keypresses get dropped with high values + Sleep(4); + } +} + +KeybindingWorker::fun* KeybindingWorker::_add_receiver(fun& receiver) +{ + QMutexLocker l(&mtx); + receivers.push_back(std::unique_ptr(new fun(receiver))); + fun* f = receivers[receivers.size() - 1].get(); + //qDebug() << "add receiver" << (long) f; + joy_ctx.refresh(); + return f; +} + +void KeybindingWorker::remove_receiver(KeybindingWorker::fun* pos) +{ + QMutexLocker l(&mtx); + bool ok = false; + + using s = int; + + for (int i = s(receivers.size()) - 1; i >= 0; i--) + { + using u = unsigned; + if (receivers[u(i)].get() == pos) + { + ok = true; + //qDebug() << "remove receiver" << (long) pos; + receivers.erase(receivers.begin() + i); + break; + } + } + if (!ok) + { + qDebug() << "bad remove receiver" << (long) pos; + } +} + +#endif diff --git a/opentrack-dinput/keybinding-worker.hpp b/opentrack-dinput/keybinding-worker.hpp new file mode 100644 index 00000000..439d2066 --- /dev/null +++ b/opentrack-dinput/keybinding-worker.hpp @@ -0,0 +1,82 @@ +/* Copyright (c) 2014-2015, Stanislaw Halik + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#pragma once + +#include "export.hpp" + +#include "opentrack-compat/timer.hpp" +#include "win32-joystick.hpp" +#include "dinput.hpp" +#include +#include +#include +#include +#include +#include + +#undef DIRECTINPUT_VERSION +#define DIRECTINPUT_VERSION 0x0800 +#include +#include + +struct OPENTRACK_DINPUT_EXPORT Key +{ + BYTE keycode; + QString guid; + bool shift; + bool ctrl; + bool alt; + bool held; + bool enabled; + Timer timer; +public: + Key() : keycode(0), shift(false), ctrl(false), alt(false), held(true), enabled(true) {} + + bool should_process(); +}; + +struct OPENTRACK_DINPUT_EXPORT KeybindingWorker : private QThread +{ + using fun = std::function; + +private: + LPDIRECTINPUTDEVICE8 dinkeyboard; + win32_joy_ctx joy_ctx; + std::vector> receivers; + QMutex mtx; + QMainWindow fake_main_window; + volatile bool should_quit; + + void run() override; + KeybindingWorker(); + + static KeybindingWorker& make(); + fun* _add_receiver(fun &receiver); + void remove_receiver(fun* pos); + ~KeybindingWorker(); + + KeybindingWorker(const KeybindingWorker&) = delete; + KeybindingWorker& operator=(KeybindingWorker&) = delete; +public: + class Token + { + fun* pos; + Token(const Token&) = delete; + Token& operator=(Token&) = delete; + public: + ~Token() + { + make().remove_receiver(pos); + } + Token(fun receiver) + { + pos = make()._add_receiver(receiver); + } + }; +}; diff --git a/opentrack-dinput/win32-joystick.cpp b/opentrack-dinput/win32-joystick.cpp new file mode 100644 index 00000000..fa1a8060 --- /dev/null +++ b/opentrack-dinput/win32-joystick.cpp @@ -0,0 +1,362 @@ +#ifdef _WIN32 + +#undef NDEBUG +#include "win32-joystick.hpp" +#include "opentrack-compat/sleep.hpp" +#include +#include +#include +#include +#include + +QMutex win32_joy_ctx::enum_state::mtx; +win32_joy_ctx::enum_state win32_joy_ctx::enumerator; + +void win32_joy_ctx::poll(fn f) +{ + //refresh(false); + + QMutexLocker l(&enumerator.mtx); + + auto& joys = enumerator.get_joys(); + + for (auto& j : joys) + { + j.second->poll(f); + } +} + +bool win32_joy_ctx::poll_axis(const QString &guid, int* axes) +{ + QMutexLocker l(&enumerator.mtx); + + for (int k = 0; k < 10; k++) + { + if (k > 0) + enumerator.refresh(); + + const joys_t& joys = enumerator.get_joys(); + auto iter = joys.find(guid); + + if (iter == joys.end()) + return false; + + auto& j = iter->second; + + auto& joy_handle = j->joy_handle; + bool ok = false; + HRESULT hr; + + if (!FAILED(hr = joy_handle->Poll())) + { + ok = true; + } + + if (!ok && FAILED(hr = joy_handle->Acquire())) + { + //qDebug() << "joy acquire failed" << hr; + } + + if (!ok) + { + portable::sleep(25); + (void) joy_handle->Unacquire(); + continue; + } + + DIJOYSTATE2 js; + std::memset(&js, 0, sizeof(js)); + + if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js))) + { + qDebug() << "joy get state failed" << guid << hr; + continue; + } + + const int values[] = + { + js.lX, + js.lY, + js.lZ, + js.lRx, + js.lRy, + js.lRz, + js.rglSlider[0], + js.rglSlider[1] + }; + + for (int i = 0; i < 8; i++) + axes[i] = values[i]; + + return true; + } + + return false; +} + +std::vector win32_joy_ctx::get_joy_info() +{ + std::vector ret; + QMutexLocker l(&enumerator.mtx); + auto& joys = enumerator.get_joys(); + ret.reserve(joys.size()); + + for (auto& j : joys) + ret.push_back(joy_info { j.second->name, j.first }); + + std::sort(ret.begin(), ret.end(), [&](const joy_info& fst, const joy_info& snd) -> bool { return fst.name < snd.name; }); + + return ret; +} + +win32_joy_ctx::win32_joy_ctx() +{ + refresh(); +} + +void win32_joy_ctx::refresh() +{ + QMutexLocker l(&enumerator.mtx); + enumerator.refresh(); +} + +QString win32_joy_ctx::guid_to_string(const GUID guid) +{ + char buf[40] = {0}; + wchar_t szGuidW[40] = {0}; + + StringFromGUID2(guid, szGuidW, 40); + WideCharToMultiByte(0, 0, szGuidW, -1, buf, 40, NULL, NULL); + + return QString(buf); +} + +using fn = win32_joy_ctx::fn; + +void win32_joy_ctx::joy::release() +{ + if (joy_handle) + { + (void) joy_handle->Unacquire(); + joy_handle->Release(); + joy_handle = nullptr; + } +} + +bool win32_joy_ctx::joy::poll(fn f) +{ + HRESULT hr; + bool ok = false; + + (void) joy_handle->Acquire(); + + if (!FAILED(hr = joy_handle->Poll())) + ok = true; + + if (!ok) + { + qDebug() << "joy acquire failed" << guid << hr; + (void) joy_handle->Unacquire(); + return false; + } + + DIJOYSTATE2 js; + std::memset(&js, 0, sizeof(js)); + + if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js))) + { + qDebug() << "joy get state failed" << guid << hr; + return false; + } + + for (unsigned i = 0; i < 4; i++) + { + using std::round; + + unsigned char pos; + unsigned pos_ = js.rgdwPOV[i]; + if ((pos_ & 0xffff) == 0xffff) + pos = 0; + else if (pos_ == ~0u) + pos = 0; + else + { + using uc = unsigned char; + pos = uc(((pos_ / 9000u) % 4u) + 1u); + } + + const bool state[] = + { + pos == 1, + pos == 2, + pos == 3, + pos == 4 + }; + + unsigned idx = 128u + i * 4u; + + for (unsigned j = 0; j < 4; j++, idx++) + { + if (state[j] != pressed[idx]) + { + f(guid, int(idx), state[j]); + pressed[idx] = state[j]; + } + } + } + + for (int i = 0; i < 128; i++) + { + const bool state = !!(js.rgbButtons[i] & 0x80); + if (state != pressed[i]) + { + f(guid, i, state); + } + pressed[i] = state; + } + + return true; +} + +win32_joy_ctx::enum_state::enum_state() +{ +} + +win32_joy_ctx::enum_state::~enum_state() +{ + QMutexLocker l(&mtx); + + joys = std::unordered_map>(); +} + +void win32_joy_ctx::enum_state::refresh() +{ + all.clear(); + + di_t di = dinput_handle::make_di(); + if (!di) + { + qDebug() << "can't create dinput"; + return; + } + + HRESULT hr; + + if(FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, + EnumJoysticksCallback, + this, + DIEDFL_ATTACHEDONLY))) + { + qDebug() << "failed enum joysticks" << hr; + return; + } + + for (auto it = joys.begin(); it != joys.end(); ) + { + if (std::find_if(all.cbegin(), all.cend(), [&](const QString& guid2) -> bool { return it->second->guid == guid2; }) == all.end()) + { + it = joys.erase(it); + } + else + { + ++it; + } + } +} + +BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext) +{ + di_t di = dinput_handle::make_di(); + if (!di) + { + qDebug() << "can't create dinput"; + return DIENUM_STOP; + } + + enum_state& state = *reinterpret_cast(pContext); + const QString guid = guid_to_string(pdidInstance->guidInstance); + const QString name = QString(pdidInstance->tszInstanceName); + + const bool exists = state.joys.find(guid) != state.joys.end(); + + state.all.push_back(guid); + + if (exists) + goto end; + + { + HRESULT hr; + LPDIRECTINPUTDEVICE8 h; + if (FAILED(hr = di->CreateDevice(pdidInstance->guidInstance, &h, nullptr))) + { + qDebug() << "createdevice" << guid << hr; + goto end; + } + if (FAILED(h->SetDataFormat(&c_dfDIJoystick2))) + { + qDebug() << "format"; + h->Release(); + goto end; + } + + // not a static member - need main() to run for some time first + static const QWidget fake_window; + + if (FAILED(h->SetCooperativeLevel(reinterpret_cast(fake_window.winId()), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))) + { + qDebug() << "coop"; + h->Release(); + goto end; + } + if (FAILED(hr = h->EnumObjects(EnumObjectsCallback, h, DIDFT_ALL))) + { + qDebug() << "enum-objects"; + h->Release(); + goto end; + } + + state.joys[guid] = std::make_shared(h, guid, name); + } +end: + return DIENUM_CONTINUE; +} + +BOOL CALLBACK win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdidoi, void *ctx) +{ + if (pdidoi->dwType & DIDFT_AXIS) + { + DIPROPRANGE diprg; + std::memset(&diprg, 0, sizeof(diprg)); + diprg.diph.dwSize = sizeof( DIPROPRANGE ); + diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER ); + diprg.diph.dwHow = DIPH_BYID; + diprg.diph.dwObj = pdidoi->dwType; + diprg.lMax = joy_axis_size; + diprg.lMin = -joy_axis_size; + + HRESULT hr; + + if (FAILED(hr = reinterpret_cast(ctx)->SetProperty(DIPROP_RANGE, &diprg.diph))) + { + qDebug() << "DIPROP_RANGE" << hr; + return DIENUM_STOP; + } + } + + return DIENUM_CONTINUE; +} + +win32_joy_ctx::joy::joy(LPDIRECTINPUTDEVICE8 handle, const QString &guid, const QString &name) + : joy_handle(handle), guid(guid), name(name) +{ + qDebug() << "make joy" << guid << name << joy_handle; + std::memset(pressed, 0, sizeof(pressed)); +} + +win32_joy_ctx::joy::~joy() +{ + qDebug() << "nix joy" << guid; + release(); +} + +#endif diff --git a/opentrack-dinput/win32-joystick.hpp b/opentrack-dinput/win32-joystick.hpp new file mode 100644 index 00000000..c4fcdde2 --- /dev/null +++ b/opentrack-dinput/win32-joystick.hpp @@ -0,0 +1,93 @@ +#pragma once + +#ifdef _WIN32 + +#include "dinput.hpp" +#include "opentrack-compat/timer.hpp" +#include "export.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace std { +template<> +struct hash +{ + inline std::size_t operator()(const QString& value) const + { + return qHash(value); + } +}; +} + +struct OPENTRACK_DINPUT_EXPORT win32_joy_ctx +{ + using fn = std::function; + + struct joy + { + LPDIRECTINPUTDEVICE8 joy_handle; + QString guid, name; + bool pressed[128 + 4 * 4]; + Timer first_timer; + + joy(LPDIRECTINPUTDEVICE8 handle, const QString& guid, const QString& name); + ~joy(); + + void release(); + bool poll(fn f); + }; + + using joys_t = std::unordered_map>; + + static constexpr int joy_axis_size = 65535; + + struct joy_info + { + QString name, guid; + }; + + void poll(fn f); + bool poll_axis(const QString& guid, int* axes); + std::vector get_joy_info(); + + win32_joy_ctx(const win32_joy_ctx&) = delete; + win32_joy_ctx& operator=(const win32_joy_ctx&) = delete; + + win32_joy_ctx(); + void refresh(); + + using di_t = dinput_handle::di_t; + +private: + static QString guid_to_string(const GUID guid); + + class enum_state final + { + std::vector all; + joys_t joys; + + static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext); + static BOOL CALLBACK EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* ctx); + + public: + static QMutex mtx; + + enum_state(); + ~enum_state(); + void refresh(); + const joys_t& get_joys() const { return joys; } + }; + + static enum_state enumerator; +}; + +#endif diff --git a/opentrack-logic/CMakeLists.txt b/opentrack-logic/CMakeLists.txt index 87175454..88b0240f 100644 --- a/opentrack-logic/CMakeLists.txt +++ b/opentrack-logic/CMakeLists.txt @@ -3,5 +3,5 @@ target_link_libraries(opentrack-logic opentrack-spline-widget) if(NOT WIN32) target_link_libraries(opentrack-logic opentrack-qxt-mini) else() - target_link_libraries(opentrack-logic winmm) + target_link_libraries(opentrack-logic opentrack-dinput winmm) endif() diff --git a/opentrack-logic/dinput.cpp b/opentrack-logic/dinput.cpp deleted file mode 100644 index ce80fe0e..00000000 --- a/opentrack-logic/dinput.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#ifdef _WIN32 - -#include "dinput.hpp" -#include - -dinput_handle dinput_handle::self; - -dinput_handle::dinput_handle() : handle(init_di()) -{ -} - -dinput_handle::~dinput_handle() -{ - if (handle) - { - handle->Release(); - handle = nullptr; - } -} - -dinput_handle::di_t dinput_handle::init_di() -{ - HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); - if (FAILED(hr)) - qDebug() << "dinput: failed CoInitializeEx" << hr << GetLastError(); - - static LPDIRECTINPUT8 di_ = nullptr; - if (di_ == nullptr) - { - if (SUCCEEDED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&di_, NULL))) - { - return di_; - } - else - { - return di_ = nullptr; - } - } - return di_; -} - -dinput_handle::di_t dinput_handle::make_di() -{ - return self.handle; -} - -#endif diff --git a/opentrack-logic/dinput.hpp b/opentrack-logic/dinput.hpp deleted file mode 100644 index 205fc656..00000000 --- a/opentrack-logic/dinput.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#ifdef _WIN32 - -#ifndef DIRECTINPUT_VERSION -# define DIRECTINPUT_VERSION 0x800 -#endif -#include "export.hpp" -#include -#include - -struct OPENTRACK_LOGIC_EXPORT dinput_handle final -{ - using di_t = LPDIRECTINPUT8; -private: - static dinput_handle self; - dinput_handle(); - ~dinput_handle(); - static di_t init_di(); - di_t handle; -public: - static di_t make_di(); - - dinput_handle(const dinput_handle&) = delete; - dinput_handle(dinput_handle&&) = delete; -}; - -#endif diff --git a/opentrack-logic/keybinding-worker.cpp b/opentrack-logic/keybinding-worker.cpp deleted file mode 100644 index a009d399..00000000 --- a/opentrack-logic/keybinding-worker.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* Copyright (c) 2014-2015, Stanislaw Halik - - * Permission to use, copy, modify, and/or distribute this - * software for any purpose with or without fee is hereby granted, - * provided that the above copyright notice and this permission - * notice appear in all copies. - */ - -#ifdef _WIN32 - -#include "keybinding-worker.hpp" -#include -#include -#include -#include - -bool Key::should_process() -{ - if (!enabled || (keycode == 0 && guid == "")) - return false; - bool ret = timer.elapsed_ms() > 100; - timer.start(); - return ret; -} - -KeybindingWorker::~KeybindingWorker() -{ - should_quit = true; - wait(); - if (dinkeyboard) { - dinkeyboard->Unacquire(); - dinkeyboard->Release(); - } -} - -KeybindingWorker::KeybindingWorker() : should_quit(true) -{ - LPDIRECTINPUT8 din = dinput_handle::make_di(); - - if (din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL) != DI_OK) { - qDebug() << "setup CreateDevice function failed!" << GetLastError(); - return; - } - - if (dinkeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK) { - qDebug() << "setup SetDataFormat function failed!" << GetLastError(); - dinkeyboard->Release(); - dinkeyboard = 0; - return; - } - - if (dinkeyboard->SetCooperativeLevel((HWND) fake_main_window.winId(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { - dinkeyboard->Release(); - dinkeyboard = 0; - qDebug() << "setup SetCooperativeLevel function failed!" << GetLastError(); - return; - } - - if (dinkeyboard->Acquire() != DI_OK) - { - dinkeyboard->Release(); - dinkeyboard = 0; - qDebug() << "setup dinkeyboard Acquire failed!" << GetLastError(); - return; - } - - should_quit = false; - start(); -} - -KeybindingWorker& KeybindingWorker::make() -{ - static KeybindingWorker k; - return k; -} - -void KeybindingWorker::run() -{ - BYTE keystate[256] = {0}; - BYTE old_keystate[256] = {0}; - - while (!should_quit) - { - { - QMutexLocker l(&mtx); - - if (receivers.size()) - { - { - const HRESULT hr = dinkeyboard->GetDeviceState(256, (LPVOID)keystate); - - if (hr != DI_OK) { - qDebug() << "Tracker::run GetDeviceState function failed!" << GetLastError(); - Sleep(25); - continue; - } - } - - { - using joy_fn = std::function; - - joy_fn f = [&](const QString& guid, int idx, bool held) -> void { - Key k; - k.keycode = idx; - 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.guid = guid; - k.held = held; - - for (auto& r : receivers) - r->operator()(k); - }; - - joy_ctx.poll(f); - } - - for (int i = 0; i < 256; i++) - { - Key k; - if (old_keystate[i] != keystate[i]) - { - const bool held = 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; - k.held = held; - - for (auto& r : receivers) - r->operator()(k); - break; - } - } - old_keystate[i] = keystate[i]; - } - } - } - - // keypresses get dropped with high values - Sleep(4); - } -} - -KeybindingWorker::fun* KeybindingWorker::_add_receiver(fun& receiver) -{ - QMutexLocker l(&mtx); - receivers.push_back(std::unique_ptr(new fun(receiver))); - fun* f = receivers[receivers.size() - 1].get(); - //qDebug() << "add receiver" << (long) f; - joy_ctx.refresh(); - return f; -} - -void KeybindingWorker::remove_receiver(KeybindingWorker::fun* pos) -{ - QMutexLocker l(&mtx); - bool ok = false; - - using s = int; - - for (int i = s(receivers.size()) - 1; i >= 0; i--) - { - using u = unsigned; - if (receivers[u(i)].get() == pos) - { - ok = true; - //qDebug() << "remove receiver" << (long) pos; - receivers.erase(receivers.begin() + i); - break; - } - } - if (!ok) - { - qDebug() << "bad remove receiver" << (long) pos; - } -} - -#endif diff --git a/opentrack-logic/keybinding-worker.hpp b/opentrack-logic/keybinding-worker.hpp deleted file mode 100644 index e38e5046..00000000 --- a/opentrack-logic/keybinding-worker.hpp +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (c) 2014-2015, Stanislaw Halik - - * Permission to use, copy, modify, and/or distribute this - * software for any purpose with or without fee is hereby granted, - * provided that the above copyright notice and this permission - * notice appear in all copies. - */ - -#pragma once - -#include "export.hpp" - -#include "opentrack-compat/timer.hpp" -#include "win32-joystick.hpp" -#include "dinput.hpp" -#include -#include -#include -#include -#include -#include - -#undef DIRECTINPUT_VERSION -#define DIRECTINPUT_VERSION 0x0800 -#include -#include -struct Key { - BYTE keycode; - QString guid; - bool shift; - bool ctrl; - bool alt; - bool held; - bool enabled; - Timer timer; -public: - Key() : keycode(0), shift(false), ctrl(false), alt(false), held(true), enabled(true) {} - - bool should_process(); -}; - -struct OPENTRACK_LOGIC_EXPORT KeybindingWorker : private QThread -{ - using fun = std::function; - -private: - LPDIRECTINPUTDEVICE8 dinkeyboard; - win32_joy_ctx joy_ctx; - std::vector> receivers; - QMutex mtx; - QMainWindow fake_main_window; - volatile bool should_quit; - - void run() override; - KeybindingWorker(); - - static KeybindingWorker& make(); - fun* _add_receiver(fun &receiver); - void remove_receiver(fun* pos); - ~KeybindingWorker(); - - KeybindingWorker(const KeybindingWorker&) = delete; - KeybindingWorker& operator=(KeybindingWorker&) = delete; -public: - class Token - { - fun* pos; - Token(const Token&) = delete; - Token& operator=(Token&) = delete; - public: - ~Token() - { - make().remove_receiver(pos); - } - Token(fun receiver) - { - pos = make()._add_receiver(receiver); - } - }; -}; diff --git a/opentrack-logic/shortcuts.h b/opentrack-logic/shortcuts.h index d6a87672..b44864ba 100644 --- a/opentrack-logic/shortcuts.h +++ b/opentrack-logic/shortcuts.h @@ -19,7 +19,7 @@ #include "main-settings.hpp" #ifdef _WIN32 -# include "keybinding-worker.hpp" +# include "opentrack-dinput/keybinding-worker.hpp" #endif #if defined(__GNUC__) && !defined(_WIN32) diff --git a/opentrack-logic/win32-joystick.cpp b/opentrack-logic/win32-joystick.cpp deleted file mode 100644 index fa1a8060..00000000 --- a/opentrack-logic/win32-joystick.cpp +++ /dev/null @@ -1,362 +0,0 @@ -#ifdef _WIN32 - -#undef NDEBUG -#include "win32-joystick.hpp" -#include "opentrack-compat/sleep.hpp" -#include -#include -#include -#include -#include - -QMutex win32_joy_ctx::enum_state::mtx; -win32_joy_ctx::enum_state win32_joy_ctx::enumerator; - -void win32_joy_ctx::poll(fn f) -{ - //refresh(false); - - QMutexLocker l(&enumerator.mtx); - - auto& joys = enumerator.get_joys(); - - for (auto& j : joys) - { - j.second->poll(f); - } -} - -bool win32_joy_ctx::poll_axis(const QString &guid, int* axes) -{ - QMutexLocker l(&enumerator.mtx); - - for (int k = 0; k < 10; k++) - { - if (k > 0) - enumerator.refresh(); - - const joys_t& joys = enumerator.get_joys(); - auto iter = joys.find(guid); - - if (iter == joys.end()) - return false; - - auto& j = iter->second; - - auto& joy_handle = j->joy_handle; - bool ok = false; - HRESULT hr; - - if (!FAILED(hr = joy_handle->Poll())) - { - ok = true; - } - - if (!ok && FAILED(hr = joy_handle->Acquire())) - { - //qDebug() << "joy acquire failed" << hr; - } - - if (!ok) - { - portable::sleep(25); - (void) joy_handle->Unacquire(); - continue; - } - - DIJOYSTATE2 js; - std::memset(&js, 0, sizeof(js)); - - if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js))) - { - qDebug() << "joy get state failed" << guid << hr; - continue; - } - - const int values[] = - { - js.lX, - js.lY, - js.lZ, - js.lRx, - js.lRy, - js.lRz, - js.rglSlider[0], - js.rglSlider[1] - }; - - for (int i = 0; i < 8; i++) - axes[i] = values[i]; - - return true; - } - - return false; -} - -std::vector win32_joy_ctx::get_joy_info() -{ - std::vector ret; - QMutexLocker l(&enumerator.mtx); - auto& joys = enumerator.get_joys(); - ret.reserve(joys.size()); - - for (auto& j : joys) - ret.push_back(joy_info { j.second->name, j.first }); - - std::sort(ret.begin(), ret.end(), [&](const joy_info& fst, const joy_info& snd) -> bool { return fst.name < snd.name; }); - - return ret; -} - -win32_joy_ctx::win32_joy_ctx() -{ - refresh(); -} - -void win32_joy_ctx::refresh() -{ - QMutexLocker l(&enumerator.mtx); - enumerator.refresh(); -} - -QString win32_joy_ctx::guid_to_string(const GUID guid) -{ - char buf[40] = {0}; - wchar_t szGuidW[40] = {0}; - - StringFromGUID2(guid, szGuidW, 40); - WideCharToMultiByte(0, 0, szGuidW, -1, buf, 40, NULL, NULL); - - return QString(buf); -} - -using fn = win32_joy_ctx::fn; - -void win32_joy_ctx::joy::release() -{ - if (joy_handle) - { - (void) joy_handle->Unacquire(); - joy_handle->Release(); - joy_handle = nullptr; - } -} - -bool win32_joy_ctx::joy::poll(fn f) -{ - HRESULT hr; - bool ok = false; - - (void) joy_handle->Acquire(); - - if (!FAILED(hr = joy_handle->Poll())) - ok = true; - - if (!ok) - { - qDebug() << "joy acquire failed" << guid << hr; - (void) joy_handle->Unacquire(); - return false; - } - - DIJOYSTATE2 js; - std::memset(&js, 0, sizeof(js)); - - if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js))) - { - qDebug() << "joy get state failed" << guid << hr; - return false; - } - - for (unsigned i = 0; i < 4; i++) - { - using std::round; - - unsigned char pos; - unsigned pos_ = js.rgdwPOV[i]; - if ((pos_ & 0xffff) == 0xffff) - pos = 0; - else if (pos_ == ~0u) - pos = 0; - else - { - using uc = unsigned char; - pos = uc(((pos_ / 9000u) % 4u) + 1u); - } - - const bool state[] = - { - pos == 1, - pos == 2, - pos == 3, - pos == 4 - }; - - unsigned idx = 128u + i * 4u; - - for (unsigned j = 0; j < 4; j++, idx++) - { - if (state[j] != pressed[idx]) - { - f(guid, int(idx), state[j]); - pressed[idx] = state[j]; - } - } - } - - for (int i = 0; i < 128; i++) - { - const bool state = !!(js.rgbButtons[i] & 0x80); - if (state != pressed[i]) - { - f(guid, i, state); - } - pressed[i] = state; - } - - return true; -} - -win32_joy_ctx::enum_state::enum_state() -{ -} - -win32_joy_ctx::enum_state::~enum_state() -{ - QMutexLocker l(&mtx); - - joys = std::unordered_map>(); -} - -void win32_joy_ctx::enum_state::refresh() -{ - all.clear(); - - di_t di = dinput_handle::make_di(); - if (!di) - { - qDebug() << "can't create dinput"; - return; - } - - HRESULT hr; - - if(FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, - EnumJoysticksCallback, - this, - DIEDFL_ATTACHEDONLY))) - { - qDebug() << "failed enum joysticks" << hr; - return; - } - - for (auto it = joys.begin(); it != joys.end(); ) - { - if (std::find_if(all.cbegin(), all.cend(), [&](const QString& guid2) -> bool { return it->second->guid == guid2; }) == all.end()) - { - it = joys.erase(it); - } - else - { - ++it; - } - } -} - -BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext) -{ - di_t di = dinput_handle::make_di(); - if (!di) - { - qDebug() << "can't create dinput"; - return DIENUM_STOP; - } - - enum_state& state = *reinterpret_cast(pContext); - const QString guid = guid_to_string(pdidInstance->guidInstance); - const QString name = QString(pdidInstance->tszInstanceName); - - const bool exists = state.joys.find(guid) != state.joys.end(); - - state.all.push_back(guid); - - if (exists) - goto end; - - { - HRESULT hr; - LPDIRECTINPUTDEVICE8 h; - if (FAILED(hr = di->CreateDevice(pdidInstance->guidInstance, &h, nullptr))) - { - qDebug() << "createdevice" << guid << hr; - goto end; - } - if (FAILED(h->SetDataFormat(&c_dfDIJoystick2))) - { - qDebug() << "format"; - h->Release(); - goto end; - } - - // not a static member - need main() to run for some time first - static const QWidget fake_window; - - if (FAILED(h->SetCooperativeLevel(reinterpret_cast(fake_window.winId()), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))) - { - qDebug() << "coop"; - h->Release(); - goto end; - } - if (FAILED(hr = h->EnumObjects(EnumObjectsCallback, h, DIDFT_ALL))) - { - qDebug() << "enum-objects"; - h->Release(); - goto end; - } - - state.joys[guid] = std::make_shared(h, guid, name); - } -end: - return DIENUM_CONTINUE; -} - -BOOL CALLBACK win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdidoi, void *ctx) -{ - if (pdidoi->dwType & DIDFT_AXIS) - { - DIPROPRANGE diprg; - std::memset(&diprg, 0, sizeof(diprg)); - diprg.diph.dwSize = sizeof( DIPROPRANGE ); - diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER ); - diprg.diph.dwHow = DIPH_BYID; - diprg.diph.dwObj = pdidoi->dwType; - diprg.lMax = joy_axis_size; - diprg.lMin = -joy_axis_size; - - HRESULT hr; - - if (FAILED(hr = reinterpret_cast(ctx)->SetProperty(DIPROP_RANGE, &diprg.diph))) - { - qDebug() << "DIPROP_RANGE" << hr; - return DIENUM_STOP; - } - } - - return DIENUM_CONTINUE; -} - -win32_joy_ctx::joy::joy(LPDIRECTINPUTDEVICE8 handle, const QString &guid, const QString &name) - : joy_handle(handle), guid(guid), name(name) -{ - qDebug() << "make joy" << guid << name << joy_handle; - std::memset(pressed, 0, sizeof(pressed)); -} - -win32_joy_ctx::joy::~joy() -{ - qDebug() << "nix joy" << guid; - release(); -} - -#endif diff --git a/opentrack-logic/win32-joystick.hpp b/opentrack-logic/win32-joystick.hpp deleted file mode 100644 index f8c2e7e4..00000000 --- a/opentrack-logic/win32-joystick.hpp +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once - -#ifdef _WIN32 - -#include "dinput.hpp" -#include "opentrack-compat/timer.hpp" -#include "export.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace std { -template<> -struct hash -{ - inline std::size_t operator()(const QString& value) const - { - return qHash(value); - } -}; -} - -struct OPENTRACK_LOGIC_EXPORT win32_joy_ctx -{ - using fn = std::function; - - struct joy - { - LPDIRECTINPUTDEVICE8 joy_handle; - QString guid, name; - bool pressed[128 + 4 * 4]; - Timer first_timer; - - joy(LPDIRECTINPUTDEVICE8 handle, const QString& guid, const QString& name); - ~joy(); - - void release(); - bool poll(fn f); - }; - - using joys_t = std::unordered_map>; - - static constexpr int joy_axis_size = 65535; - - struct joy_info - { - QString name, guid; - }; - - void poll(fn f); - bool poll_axis(const QString& guid, int* axes); - std::vector get_joy_info(); - - win32_joy_ctx(const win32_joy_ctx&) = delete; - win32_joy_ctx& operator=(const win32_joy_ctx&) = delete; - - win32_joy_ctx(); - void refresh(); - - using di_t = dinput_handle::di_t; - -private: - static QString guid_to_string(const GUID guid); - - class enum_state final - { - std::vector all; - joys_t joys; - - static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext); - static BOOL CALLBACK EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* ctx); - - public: - static QMutex mtx; - - enum_state(); - ~enum_state(); - void refresh(); - const joys_t& get_joys() const { return joys; } - }; - - static enum_state enumerator; -}; - -#endif diff --git a/tracker-joystick/CMakeLists.txt b/tracker-joystick/CMakeLists.txt index e0b6f281..03f14b62 100644 --- a/tracker-joystick/CMakeLists.txt +++ b/tracker-joystick/CMakeLists.txt @@ -1,5 +1,5 @@ if(WIN32) opentrack_boilerplate(opentrack-tracker-joystick) - target_link_libraries(opentrack-tracker-joystick opentrack-logic) + target_link_libraries(opentrack-tracker-joystick opentrack-dinput) endif() diff --git a/tracker-joystick/ftnoir_tracker_joystick.h b/tracker-joystick/ftnoir_tracker_joystick.h index 31f1dee7..2d776ebf 100644 --- a/tracker-joystick/ftnoir_tracker_joystick.h +++ b/tracker-joystick/ftnoir_tracker_joystick.h @@ -17,7 +17,7 @@ #include #include "opentrack/plugin-api.hpp" -#include "opentrack-logic/win32-joystick.hpp" +#include "opentrack-dinput/win32-joystick.hpp" #include "opentrack-compat/options.hpp" using namespace options; -- cgit v1.2.3