Files
ppsspp/Windows/InputDevice.cpp
T
Henrik RydgårdandClaude Opus 5.5 2968debfe7 Windows: Don't hang on exit when a HID pad stops sending reports
DualShock, DualSense and Switch Pro pads are read with a blocking
ReadFile on the input thread, and WM_DESTROY joins that thread. If the
pad stops sending reports the read never returns, so the window is gone
but the process stays. Cancel the thread's I/O until it stops, retrying
in case a cancel lands just before a read starts. This also covers the
synchronous writes the pads' shutdown does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 13:51:56 +02:00

113 lines
3.5 KiB
C++

// Copyright (c) 2014- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "stdafx.h"
#include <thread>
#include <atomic>
#include "Common/Input/InputState.h"
#include "Common/System/System.h"
#include "Common/Thread/ThreadUtil.h"
#include "Core/Config.h"
#include "Windows/InputDevice.h"
#if !PPSSPP_PLATFORM(UWP)
#include "Windows/DinputDevice.h"
#include "Windows/Hid/HidInputDevice.h"
#include "Windows/XinputDevice.h"
#endif
InputManager g_InputManager;
void InputManager::InputThread() {
SetCurrentThreadName("Input");
for (auto &device : devices_) {
device->Init();
}
// NOTE: The keyboard and mouse buttons are handled via raw input, not here.
// This is mainly for controllers which need to be polled, instead of generating events.
while (runThread_.load(std::memory_order_relaxed)) {
bool noSleep = false;
if (focused_.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused) {
System_Notify(SystemNotification::POLL_CONTROLLERS);
for (const auto &device : devices_) {
const int state = device->UpdateState();
if (state == InputDevice::UPDATESTATE_SKIP_PAD)
break;
if (state == InputDevice::UPDATESTATE_NO_SLEEP) {
// Sleep was handled automatically.
noSleep = true;
}
}
}
// Try to update 250 times per second.
if (!noSleep)
Sleep(4);
}
for (auto &device : devices_) {
device->Shutdown();
}
}
void InputManager::BeginPolling() {
runThread_.store(true, std::memory_order_relaxed);
inputThread_ = std::thread([this]() {
// In UWP, we add the devices from the main thread, before launching the thread.
// This is a bit awkward but worth the startup speed boost on non-UWP until we refactor it.
#if !PPSSPP_PLATFORM(UWP)
//add first XInput device to respond
AddDevice(new XinputDevice());
AddDevice(new DInputMetaDevice());
AddDevice(new HidInputDevice());
#endif
InputThread();
});
}
void InputManager::StopPolling() {
INFO_LOG(Log::System, "Stopping the input thread");
runThread_.store(false, std::memory_order_relaxed);
#if !PPSSPP_PLATFORM(UWP)
// The HID pads are read with a blocking ReadFile, which never returns if the pad stops sending
// reports, and then the join below waits forever with the window already gone. Cancel whatever
// I/O the thread is blocked in. Repeat it, since a cancel that lands just before a read starts
// does nothing.
HANDLE thread = (HANDLE)inputThread_.native_handle();
for (int tries = 0; WaitForSingleObject(thread, 100) == WAIT_TIMEOUT; tries++) {
if (tries == 0) {
WARN_LOG(Log::System, "Input thread didn't stop, cancelling its I/O");
}
CancelSynchronousIo(thread);
}
#endif
inputThread_.join();
INFO_LOG(Log::System, "Input thread stopped");
}
bool InputManager::AnyAccelerometer() const {
for (const auto &device : devices_) {
if (device->HasAccelerometer()) {
return true;
}
}
return false;
}