summaryrefslogtreecommitdiffhomepage
path: root/dinput/win32-joystick.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dinput/win32-joystick.cpp')
-rw-r--r--dinput/win32-joystick.cpp82
1 files changed, 28 insertions, 54 deletions
diff --git a/dinput/win32-joystick.cpp b/dinput/win32-joystick.cpp
index 4da5c57d..0f2687fe 100644
--- a/dinput/win32-joystick.cpp
+++ b/dinput/win32-joystick.cpp
@@ -3,19 +3,12 @@
#include "win32-joystick.hpp"
#include "compat/macros.hpp"
-// doesn't play well with Qt Creator clang code model
-#if defined Q_CREATOR_RUN && defined _MSC_VER
-# undef offsetof
-# define offsetof(type, member) __builtin_offsetof(type, member)
-#else
-# include <cstddef>
-#endif
-
+#include <cstddef>
#include <algorithm>
#include <cmath>
+#include <iterator>
#include <QWidget>
-
#include <QDebug>
#include <objbase.h>
@@ -54,33 +47,15 @@ bool win32_joy_ctx::poll_axis(const QString &guid, int* axes)
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()))
- {
- (void)0;
- //qDebug() << "joy acquire failed" << hr;
- }
+ DIJOYSTATE2 js = {};
- if (!ok)
- {
- (void)joy_handle->Unacquire();
- Sleep(100);
+ if (!di_t::poll_device(joy_handle))
continue;
- }
-
- DIJOYSTATE2 js = {};
- if (FAILED(hr = joy_handle->GetDeviceState(sizeof(js), &js)))
+ if (FAILED(joy_handle->GetDeviceState(sizeof(js), &js)))
{
- //qDebug() << "joy get state failed" << guid << hr;
- Sleep(500);
+ //qDebug() << "joy get state failed" << guid;
continue;
}
@@ -96,7 +71,7 @@ bool win32_joy_ctx::poll_axis(const QString &guid, int* axes)
js.rglSlider[1]
};
- for (int i = 0; i < 8; i++)
+ for (unsigned i = 0; i < std::size(values); i++)
axes[i] = values[i];
return true;
@@ -133,11 +108,12 @@ void win32_joy_ctx::refresh()
QString win32_joy_ctx::guid_to_string(const GUID& guid)
{
- char buf[40] = {0};
- wchar_t szGuidW[40] = {0};
+ char buf[40] = {};
+ wchar_t szGuidW[40] = {};
- StringFromGUID2(guid, szGuidW, 40);
- WideCharToMultiByte(0, 0, szGuidW, -1, buf, 40, nullptr, nullptr);
+ StringFromGUID2(guid, szGuidW, sizeof(buf));
+ WideCharToMultiByte(0, 0, szGuidW, -1, buf, sizeof(buf), nullptr, nullptr);
+ buf[sizeof(buf)-1] = 0;
return QString(buf);
}
@@ -158,19 +134,18 @@ bool win32_joy_ctx::joy::poll(fn const& f)
{
HRESULT hr;
- (void) joy_handle->Acquire();
-
- if (FAILED(hr = joy_handle->Poll()))
+ if (!di_t::poll_device(joy_handle))
{
- //qDebug() << "joy acquire failed" << guid << hr;
- (void) joy_handle->Unacquire();
+ eval_once(qDebug() << "joy poll failed" << guid << (void*)hr);
+ //(void)joy_handle->Unacquire();
+ //Sleep(0);
return false;
}
DWORD sz = num_buffers;
if (FAILED(hr = joy_handle->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), keystate_buffers, &sz, 0)))
{
- eval_once(qDebug() << "joy get state failed" << guid << hr);
+ eval_once(qDebug() << "joy GetDeviceData failed" << guid << (void*)hr);
return false;
}
@@ -195,7 +170,7 @@ bool win32_joy_ctx::joy::poll(fn const& f)
default:
if (event.dwOfs >= BUTTON_OFFSET(0) && event.dwOfs <= BUTTON_OFFSET(max_buttons - 1))
{
- i = event.dwOfs - BUTTON_OFFSET(0);
+ i = int(event.dwOfs - BUTTON_OFFSET(0));
i /= sizeof(DIJOYSTATE2().rgbButtons[0]);
i %= max_buttons; // defensive programming
}
@@ -254,7 +229,7 @@ void win32_joy_ctx::enum_state::refresh()
if (!di)
{
- qDebug() << "can't create dinput";
+ qDebug() << "dinput: can't create dinput";
return;
}
@@ -265,7 +240,7 @@ void win32_joy_ctx::enum_state::refresh()
this,
DIEDFL_ATTACHEDONLY)))
{
- eval_once(qDebug() << "failed enum joysticks" << hr);
+ eval_once(qDebug() << "dinput: failed enum joysticks" << (void*)hr);
return;
}
@@ -298,12 +273,12 @@ BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINS
LPDIRECTINPUTDEVICE8 h;
if (FAILED(hr = state.di->CreateDevice(pdidInstance->guidInstance, &h, nullptr)))
{
- qDebug() << "createdevice" << guid << hr;
+ qDebug() << "dinput: failed joystick CreateDevice" << guid << (void*)hr;
goto end;
}
- if (FAILED(h->SetDataFormat(&c_dfDIJoystick2)))
+ if (FAILED(hr = h->SetDataFormat(&c_dfDIJoystick2)))
{
- qDebug() << "format";
+ qDebug() << "dinput: failed joystick SetDataFormat" << (void*)hr;
h->Release();
goto end;
}
@@ -328,7 +303,7 @@ BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINS
if (h->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph) != DI_OK)
{
- qDebug() << "setup joystick buffer mode failed!";
+ qDebug() << "dinput: joystick DIPROP_BUFFERSIZE";
h->Release();
goto end;
}
@@ -336,7 +311,7 @@ BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINS
if (FAILED(hr = h->EnumObjects(EnumObjectsCallback, h, DIDFT_ALL)))
{
- qDebug() << "enum-objects";
+ qDebug() << "dinput: joystick EnumObjects";
h->Release();
goto end;
}
@@ -352,8 +327,8 @@ BOOL CALLBACK win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJEC
if (pdidoi->dwType & DIDFT_AXIS)
{
DIPROPRANGE diprg = {};
- diprg.diph.dwSize = sizeof( DIPROPRANGE );
- diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER );
+ 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;
@@ -363,7 +338,7 @@ BOOL CALLBACK win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJEC
if (FAILED(hr = reinterpret_cast<LPDIRECTINPUTDEVICE8>(ctx)->SetProperty(DIPROP_RANGE, &diprg.diph)))
{
- qDebug() << "DIPROP_RANGE" << hr;
+ qDebug() << "dinput: failed joystick DIPROP_RANGE" << (void*)hr;
return DIENUM_STOP;
}
}
@@ -384,5 +359,4 @@ win32_joy_ctx::joy::~joy()
}
} // ns win32_joy_impl
-
#endif