Skip to content

Commit

Permalink
Windows: Detect DirectInput devices after launch.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Oct 9, 2018
1 parent 5a224d7 commit 52e2e07
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
11 changes: 4 additions & 7 deletions Windows/DinputDevice.cpp
Expand Up @@ -118,9 +118,9 @@ BOOL CALLBACK DinputDevice::DevicesCallback(
return DIENUM_CONTINUE;
}

void DinputDevice::getDevices()
void DinputDevice::getDevices(bool refresh)
{
if (devices.empty())
if (devices.empty() || refresh)
{
getPDI()->EnumDevices(DI8DEVCLASS_GAMECTRL, &DinputDevice::DevicesCallback, NULL, DIEDFL_ATTACHEDONLY);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ DinputDevice::DinputDevice(int devnum) {
return;
}

getDevices();
getDevices(false);
if ( (devnum >= (int)devices.size()) || FAILED(getPDI()->CreateDevice(devices.at(devnum).guidInstance, &pJoystick, NULL)))
{
return;
Expand Down Expand Up @@ -368,9 +368,6 @@ void DinputDevice::ApplyButtons(DIJOYSTATE2 &state) {

size_t DinputDevice::getNumPads()
{
if (devices.empty())
{
getDevices();
}
getDevices(true);
return devices.size();
}
2 changes: 1 addition & 1 deletion Windows/DinputDevice.h
Expand Up @@ -43,7 +43,7 @@ class DinputDevice :
//GUIDs of the plugged in devices. This function will only search for devices
//if none have been found yet and will only list plugged in devices
//also, it excludes the devices that are compatible with XInput
static void getDevices();
static void getDevices(bool refresh);
//callback for the WinAPI to call
static BOOL CALLBACK DevicesCallback(
LPCDIDEVICEINSTANCE lpddi,
Expand Down
53 changes: 33 additions & 20 deletions Windows/WindowsHost.cpp
Expand Up @@ -77,7 +77,7 @@ static BOOL PostDialogMessage(Dialog *dialog, UINT message, WPARAM wParam = 0, L
}

WindowsHost::WindowsHost(HINSTANCE hInstance, HWND mainWindow, HWND displayWindow)
: gfx_(nullptr), hInstance_(hInstance),
: hInstance_(hInstance),
mainWindow_(mainWindow),
displayWindow_(displayWindow)
{
Expand All @@ -87,8 +87,8 @@ WindowsHost::WindowsHost(HINSTANCE hInstance, HWND mainWindow, HWND displayWindo
//add first XInput device to respond
input.push_back(std::shared_ptr<InputDevice>(new XinputDevice()));
//find all connected DInput devices of class GamePad
size_t numDInputDevs = DinputDevice::getNumPads();
for (size_t i = 0; i < numDInputDevs; i++) {
numDinputDevices_ = DinputDevice::getNumPads();
for (size_t i = 0; i < numDinputDevices_; i++) {
input.push_back(std::shared_ptr<InputDevice>(new DinputDevice(static_cast<int>(i))));
}
keyboard = std::shared_ptr<KeyboardDevice>(new KeyboardDevice());
Expand Down Expand Up @@ -205,31 +205,44 @@ void WindowsHost::SetDebugMode(bool mode) {
}

void WindowsHost::PollControllers() {
static int checkCounter = 0;
static const int CHECK_FREQUENCY = 71;
if (checkCounter++ > CHECK_FREQUENCY) {
size_t newCount = DinputDevice::getNumPads();
if (newCount > numDinputDevices_) {
INFO_LOG(SYSTEM, "New controller device detected");
for (size_t i = numDinputDevices_; i < newCount; i++) {
input.push_back(std::shared_ptr<InputDevice>(new DinputDevice(static_cast<int>(i))));
}
numDinputDevices_ = newCount;
}

checkCounter = 0;
}

bool doPad = true;
for (auto iter = this->input.begin(); iter != this->input.end(); iter++)
{
auto device = *iter;
for (const auto &device : input) {
if (!doPad && device->IsPad())
continue;
if (device->UpdateState() == InputDevice::UPDATESTATE_SKIP_PAD)
doPad = false;
}

float scaleFactor_x = g_dpi_scale_x * 0.1 * g_Config.fMouseSensitivity;
float scaleFactor_y = g_dpi_scale_y * 0.1 * g_Config.fMouseSensitivity;

float mx = std::max(-1.0f, std::min(1.0f, g_mouseDeltaX * scaleFactor_x));
float my = std::max(-1.0f, std::min(1.0f, g_mouseDeltaY * scaleFactor_y));
AxisInput axisX, axisY;
axisX.axisId = JOYSTICK_AXIS_MOUSE_REL_X;
axisX.deviceId = DEVICE_ID_MOUSE;
axisX.value = mx;
axisY.axisId = JOYSTICK_AXIS_MOUSE_REL_Y;
axisY.deviceId = DEVICE_ID_MOUSE;
axisY.value = my;

// Disabled by default, needs a workaround to map to psp keys.
if (g_Config.bMouseControl){
if (g_Config.bMouseControl) {
float scaleFactor_x = g_dpi_scale_x * 0.1 * g_Config.fMouseSensitivity;
float scaleFactor_y = g_dpi_scale_y * 0.1 * g_Config.fMouseSensitivity;

float mx = std::max(-1.0f, std::min(1.0f, g_mouseDeltaX * scaleFactor_x));
float my = std::max(-1.0f, std::min(1.0f, g_mouseDeltaY * scaleFactor_y));
AxisInput axisX, axisY;
axisX.axisId = JOYSTICK_AXIS_MOUSE_REL_X;
axisX.deviceId = DEVICE_ID_MOUSE;
axisX.value = mx;
axisY.axisId = JOYSTICK_AXIS_MOUSE_REL_Y;
axisY.deviceId = DEVICE_ID_MOUSE;
axisY.value = my;

if (GetUIState() == UISTATE_INGAME || g_Config.bMapMouse) {
if (fabsf(mx) > 0.01f) NativeAxis(axisX);
if (fabsf(my) > 0.01f) NativeAxis(axisY);
Expand Down
3 changes: 2 additions & 1 deletion Windows/WindowsHost.h
Expand Up @@ -74,7 +74,8 @@ class WindowsHost : public Host {
HINSTANCE hInstance_;
HWND displayWindow_;
HWND mainWindow_;
GraphicsContext *gfx_;
GraphicsContext *gfx_ = nullptr;
size_t numDinputDevices_ = 0;

std::list<std::shared_ptr<InputDevice>> input;
};

0 comments on commit 52e2e07

Please sign in to comment.