#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();
}
bool noSleep = false;
while (runThread_.load(std::memory_order_relaxed)) {
if (focused_.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused) {
System_Notify(SystemNotification::POLL_CONTROLLERS);
for (const auto &device : devices_) {
int state = device->UpdateState();
if (state == InputDevice::UPDATESTATE_SKIP_PAD)
break;
if (state == InputDevice::UPDATESTATE_NO_SLEEP) {
noSleep = true;
}
}
}
if (!noSleep)
Sleep(4);
}
for (auto &device : devices_) {
device->Shutdown();
}
}
void InputManager::BeginPolling() {
runThread_.store(true, std::memory_order_relaxed);
inputThread_ = std::thread([this]() {
#if !PPSSPP_PLATFORM(UWP)
AddDevice(new XinputDevice());
AddDevice(new DInputMetaDevice());
AddDevice(new HidInputDevice());
#endif
InputThread();
});
}
void InputManager::StopPolling() {
runThread_.store(false, std::memory_order_relaxed);
inputThread_.join();
}
bool InputManager::AnyAccelerometer() const {
for (const auto &device : devices_) {
if (device->HasAccelerometer()) {
return true;
}
}
return false;
}