Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/InputDevice.cpp
5654 views
1
// Copyright (c) 2014- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "stdafx.h"
19
#include <thread>
20
#include <atomic>
21
22
#include "Common/Input/InputState.h"
23
#include "Common/System/System.h"
24
#include "Common/Thread/ThreadUtil.h"
25
#include "Core/Config.h"
26
#include "Windows/InputDevice.h"
27
28
#if !PPSSPP_PLATFORM(UWP)
29
#include "Windows/DinputDevice.h"
30
#include "Windows/Hid/HidInputDevice.h"
31
#include "Windows/XinputDevice.h"
32
#endif
33
34
InputManager g_InputManager;
35
36
void InputManager::InputThread() {
37
SetCurrentThreadName("Input");
38
39
for (auto &device : devices_) {
40
device->Init();
41
}
42
43
// NOTE: The keyboard and mouse buttons are handled via raw input, not here.
44
// This is mainly for controllers which need to be polled, instead of generating events.
45
bool noSleep = false;
46
while (runThread_.load(std::memory_order_relaxed)) {
47
if (focused_.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused) {
48
System_Notify(SystemNotification::POLL_CONTROLLERS);
49
for (const auto &device : devices_) {
50
int state = device->UpdateState();
51
if (state == InputDevice::UPDATESTATE_SKIP_PAD)
52
break;
53
if (state == InputDevice::UPDATESTATE_NO_SLEEP) {
54
// Sleep was handled automatically.
55
noSleep = true;
56
}
57
}
58
}
59
60
// Try to update 250 times per second.
61
if (!noSleep)
62
Sleep(4);
63
}
64
65
for (auto &device : devices_) {
66
device->Shutdown();
67
}
68
}
69
70
void InputManager::BeginPolling() {
71
runThread_.store(true, std::memory_order_relaxed);
72
inputThread_ = std::thread([this]() {
73
// In UWP, we add the devices from the main thread, before launching the thread.
74
// This is a bit awkward but worth the startup speed boost on non-UWP until we refactor it.
75
#if !PPSSPP_PLATFORM(UWP)
76
//add first XInput device to respond
77
AddDevice(new XinputDevice());
78
AddDevice(new DInputMetaDevice());
79
AddDevice(new HidInputDevice());
80
#endif
81
InputThread();
82
});
83
}
84
85
void InputManager::StopPolling() {
86
runThread_.store(false, std::memory_order_relaxed);
87
inputThread_.join();
88
}
89
90
bool InputManager::AnyAccelerometer() const {
91
for (const auto &device : devices_) {
92
if (device->HasAccelerometer()) {
93
return true;
94
}
95
}
96
return false;
97
}
98
99