summaryrefslogtreecommitdiffhomepage
path: root/opentrack/win32-joystick.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/win32-joystick.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/win32-joystick.cpp')
-rw-r--r--opentrack/win32-joystick.cpp299
1 files changed, 299 insertions, 0 deletions
diff --git a/opentrack/win32-joystick.cpp b/opentrack/win32-joystick.cpp
new file mode 100644
index 00000000..5e6f2011
--- /dev/null
+++ b/opentrack/win32-joystick.cpp
@@ -0,0 +1,299 @@
+#undef NDEBUG
+#include <cassert>
+#include "win32-joystick.hpp"
+
+#ifdef _WIN32
+
+void win32_joy_ctx::poll(fn f)
+{
+ //refresh(false);
+
+ QMutexLocker l(&mtx);
+
+ for (auto& j : joys)
+ {
+ j.second->poll(f);
+ }
+}
+
+bool win32_joy_ctx::poll_axis(const QString &guid, int axes[])
+{
+ //refresh(false);
+
+ QMutexLocker l(&mtx);
+
+ 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;
+
+ (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;
+ memset(&js, 0, sizeof(js));
+
+ if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js)))
+ {
+ qDebug() << "joy get state failed" << guid << hr;
+ return false;
+ }
+
+ 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;
+}
+
+win32_joy_ctx::~win32_joy_ctx()
+{
+ release();
+}
+
+std::vector<win32_joy_ctx::joy_info> win32_joy_ctx::get_joy_info()
+{
+ QMutexLocker l(&mtx);
+
+ std::vector<joy_info> ret;
+
+ for (auto& j : joys)
+ ret.push_back(joy_info { j.second->name, j.first });
+
+ return ret;
+}
+
+win32_joy_ctx::win32_joy_ctx()
+{
+ if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&di, NULL) != DI_OK) {
+ qDebug() << "setup DirectInput8 Creation failed!" << GetLastError();
+ assert(!"direct input handle can't be created");
+ }
+ refresh();
+}
+
+void win32_joy_ctx::release()
+{
+ joys = std::unordered_map<QString, std::shared_ptr<joy>>();
+ di->Release();
+ di = nullptr;
+}
+
+void win32_joy_ctx::refresh()
+{
+ QMutexLocker l(&mtx);
+
+ qDebug() << "joy list refresh";
+ enum_state st(joys, fake_main_window, di);
+}
+
+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;
+ memset(&js, 0, sizeof(js));
+
+ if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js)))
+ {
+ qDebug() << "joy get state failed" << guid << hr;
+ return false;
+ }
+
+ for (int i = 0; i < 128; i++)
+ {
+ const bool state = !!(js.rgbButtons[i] & 0x80) && js.rgbButtons[i] != js_old.rgbButtons[i];
+ if (state != pressed[i])
+ {
+ f(guid, i, state);
+ qDebug() << "btn" << guid << i << state;
+ }
+ pressed[i] = state;
+ }
+
+ js_old = js;
+
+ return true;
+}
+
+win32_joy_ctx::enum_state::enum_state(std::unordered_map<QString, std::shared_ptr<joy>> &joys,
+ QMainWindow &fake_main_window,
+ LPDIRECTINPUT8 di) :
+ fake_main_window(fake_main_window),
+ di(di)
+{
+ this->joys = joys;
+
+ HRESULT hr;
+
+ if(FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL,
+ EnumJoysticksCallback,
+ this,
+ DIEDFL_ATTACHEDONLY)))
+ {
+ qDebug() << "failed enum joysticks" << hr;
+ return;
+ }
+
+ auto& js = this->joys;
+
+ for (auto it = js.begin(); it != js.end(); )
+ {
+ if (std::find_if(all.cbegin(), all.cend(), [&](const QString& guid2) -> bool { return it->second->guid == guid2; }) == all.cend())
+ it = js.erase(it);
+ else
+ it++;
+ }
+
+ joys = this->joys;
+}
+
+win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext)
+{
+ enum_state& state = *reinterpret_cast<enum_state*>(pContext);
+ const QString guid = guid_to_string(pdidInstance->guidInstance);
+ const QString name = QString(pdidInstance->tszInstanceName);
+
+ auto it = state.joys.find(guid);
+ const bool exists = it != state.joys.end();
+
+ state.all.push_back(guid);
+
+ if (!exists)
+ {
+ HRESULT hr;
+ LPDIRECTINPUTDEVICE8 h;
+ if (FAILED(hr = state.di->CreateDevice(pdidInstance->guidInstance, &h, nullptr)))
+ {
+ qDebug() << "createdevice" << guid << hr;
+ goto end;
+ }
+ if (FAILED(h->SetDataFormat(&c_dfDIJoystick2)))
+ {
+ qDebug() << "format";
+ h->Release();
+ goto end;
+ }
+
+ if (FAILED(h->SetCooperativeLevel((HWND) state.fake_main_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;
+ }
+
+ qDebug() << "add joy" << guid;
+ state.joys[guid] = std::make_shared<joy>(h, guid, name);
+ }
+end:
+ return DIENUM_CONTINUE;
+}
+
+win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdidoi, void *ctx)
+{
+ if (pdidoi->dwType & DIDFT_AXIS)
+ {
+ DIPROPRANGE diprg;
+ 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<LPDIRECTINPUTDEVICE8>(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() << "got joy" << guid;
+ for (int i = 0; i < 128; i++)
+ pressed[i] = false;
+ memset(&js_old, 0, sizeof(js_old));
+}
+
+win32_joy_ctx::joy::~joy()
+{
+ qDebug() << "nix joy" << guid;
+ release();
+}
+
+#endif