#ifdef _WIN32 #undef NDEBUG #include #include #include "win32-joystick.hpp" #include "opentrack-compat/sleep.hpp" 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; 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::di_t& win32_joy_ctx::make_di() { 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_; } 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; 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() { } win32_joy_ctx::enum_state::~enum_state() { QMutexLocker l(&mtx); di_t& di = make_di(); if (!di) { qDebug() << "can't create dinput"; return; } joys = std::unordered_map>(); di->Release(); di = nullptr; } void win32_joy_ctx::enum_state::refresh() { all.clear(); di_t di = 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 = 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; 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; 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