// Copyright (c) 2014- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "stdafx.h"18#include <thread>19#include <atomic>2021#include "Common/Input/InputState.h"22#include "Common/System/System.h"23#include "Common/Thread/ThreadUtil.h"24#include "Core/Config.h"25#include "Windows/InputDevice.h"2627InputManager g_InputManager;2829void InputManager::InputThread() {30SetCurrentThreadName("Input");3132for (auto &device : devices_) {33device->Init();34}3536// NOTE: The keyboard and mouse buttons are handled via raw input, not here.37// This is mainly for controllers which need to be polled, instead of generating events.38bool noSleep = false;39while (runThread_.load(std::memory_order_relaxed)) {40if (focused_.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused) {41System_Notify(SystemNotification::POLL_CONTROLLERS);42for (const auto &device : devices_) {43int state = device->UpdateState();44if (state == InputDevice::UPDATESTATE_SKIP_PAD)45break;46if (state == InputDevice::UPDATESTATE_NO_SLEEP) {47// Sleep was handled automatically.48noSleep = true;49}50}51}5253// Try to update 250 times per second.54if (!noSleep)55Sleep(4);56}5758for (auto &device : devices_) {59device->Shutdown();60}61}6263void InputManager::BeginPolling() {64runThread_.store(true, std::memory_order_relaxed);65inputThread_ = std::thread([this]() {66InputThread();67});68}6970void InputManager::StopPolling() {71runThread_.store(false, std::memory_order_relaxed);72inputThread_.join();73}747576