CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/InputDevice.cpp
Views: 1401
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
static std::atomic_flag threadRunningFlag;
29
static std::thread inputThread;
30
static std::atomic_bool focused = ATOMIC_VAR_INIT(true);
31
32
inline static void ExecuteInputPoll() {
33
if (focused.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused) {
34
System_Notify(SystemNotification::POLL_CONTROLLERS);
35
}
36
}
37
38
static void RunInputThread() {
39
SetCurrentThreadName("Input");
40
41
// NOTE: The keyboard and mouse buttons are handled via raw input, not here.
42
// This is mainly for controllers which need to be polled, instead of generating events.
43
44
while (threadRunningFlag.test_and_set(std::memory_order_relaxed)) {
45
ExecuteInputPoll();
46
47
// Try to update 250 times per second.
48
Sleep(4);
49
}
50
}
51
52
void InputDevice::BeginPolling() {
53
threadRunningFlag.test_and_set(std::memory_order_relaxed);
54
inputThread = std::thread(&RunInputThread);
55
}
56
57
void InputDevice::StopPolling() {
58
threadRunningFlag.clear(std::memory_order_relaxed);
59
60
inputThread.join();
61
}
62
63
void InputDevice::GainFocus() {
64
focused.store(true, std::memory_order_relaxed);
65
}
66
67
void InputDevice::LoseFocus() {
68
focused.store(false, std::memory_order_relaxed);
69
}
70
71