summaryrefslogtreecommitdiffhomepage
path: root/opentrack/win32-joystick.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'opentrack/win32-joystick.cpp')
-rw-r--r--opentrack/win32-joystick.cpp92
1 files changed, 45 insertions, 47 deletions
diff --git a/opentrack/win32-joystick.cpp b/opentrack/win32-joystick.cpp
index 5e6f2011..bf919f4a 100644
--- a/opentrack/win32-joystick.cpp
+++ b/opentrack/win32-joystick.cpp
@@ -7,9 +7,9 @@
void win32_joy_ctx::poll(fn f)
{
//refresh(false);
-
+
QMutexLocker l(&mtx);
-
+
for (auto& j : joys)
{
j.second->poll(f);
@@ -19,41 +19,41 @@ void win32_joy_ctx::poll(fn 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,
@@ -64,10 +64,10 @@ bool win32_joy_ctx::poll_axis(const QString &guid, int axes[])
js.rglSlider[0],
js.rglSlider[1]
};
-
+
for (int i = 0; i < 8; i++)
axes[i] = values[i];
-
+
return true;
}
@@ -79,12 +79,12 @@ win32_joy_ctx::~win32_joy_ctx()
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;
}
@@ -107,7 +107,7 @@ void win32_joy_ctx::release()
void win32_joy_ctx::refresh()
{
QMutexLocker l(&mtx);
-
+
qDebug() << "joy list refresh";
enum_state st(joys, fake_main_window, di);
}
@@ -116,10 +116,10 @@ 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);
}
@@ -139,28 +139,28 @@ 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];
@@ -171,20 +171,20 @@ bool win32_joy_ctx::joy::poll(fn f)
}
pressed[i] = state;
}
-
+
js_old = js;
-
+
return true;
}
-win32_joy_ctx::enum_state::enum_state(std::unordered_map<QString, std::shared_ptr<joy>> &joys,
+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;
-
+ joys = joys_;
+
HRESULT hr;
if(FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL,
@@ -195,18 +195,16 @@ win32_joy_ctx::enum_state::enum_state(std::unordered_map<QString, std::shared_pt
qDebug() << "failed enum joysticks" << hr;
return;
}
-
- auto& js = this->joys;
-
- for (auto it = js.begin(); it != js.end(); )
+
+ 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.cend())
- it = js.erase(it);
+ it = joys.erase(it);
else
it++;
}
-
- joys = this->joys;
+
+ joys_ = joys;
}
win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext)
@@ -214,12 +212,12 @@ win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidIns
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;
@@ -235,7 +233,7 @@ win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidIns
h->Release();
goto end;
}
-
+
if (FAILED(h->SetCooperativeLevel((HWND) state.fake_main_window.winId(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
{
qDebug() << "coop";
@@ -248,7 +246,7 @@ win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidIns
h->Release();
goto end;
}
-
+
qDebug() << "add joy" << guid;
state.joys[guid] = std::make_shared<joy>(h, guid, name);
}
@@ -268,16 +266,16 @@ win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdi
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;
}