2014-03-24 05:18:54 +00:00
|
|
|
// 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/.
|
|
|
|
|
2020-08-12 18:15:32 +00:00
|
|
|
#include "stdafx.h"
|
2017-02-27 19:51:36 +00:00
|
|
|
#include <thread>
|
2019-06-28 23:19:49 +00:00
|
|
|
#include <atomic>
|
2017-02-27 20:57:46 +00:00
|
|
|
|
2020-10-01 07:36:43 +00:00
|
|
|
#include "Common/Input/InputState.h"
|
2020-10-01 07:27:25 +00:00
|
|
|
#include "Common/Thread/ThreadUtil.h"
|
2015-02-28 22:02:03 +00:00
|
|
|
#include "Core/Config.h"
|
2014-03-24 05:18:54 +00:00
|
|
|
#include "Core/Host.h"
|
|
|
|
#include "Windows/InputDevice.h"
|
|
|
|
|
2019-06-28 23:19:49 +00:00
|
|
|
static std::atomic_flag threadRunningFlag;
|
|
|
|
static std::thread inputThread;
|
2020-08-13 14:48:48 +00:00
|
|
|
static std::atomic_bool focused = ATOMIC_VAR_INIT(true);
|
2014-03-24 05:18:54 +00:00
|
|
|
|
|
|
|
inline static void ExecuteInputPoll() {
|
2019-06-28 23:19:49 +00:00
|
|
|
if (host && (focused.load(std::memory_order_relaxed) || !g_Config.bGamepadOnlyFocused)) {
|
2017-03-15 05:01:18 +00:00
|
|
|
host->PollControllers();
|
2014-03-24 05:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RunInputThread() {
|
2020-11-30 23:46:26 +00:00
|
|
|
SetCurrentThreadName("Input");
|
2014-03-24 05:18:54 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2019-06-28 23:19:49 +00:00
|
|
|
while (threadRunningFlag.test_and_set(std::memory_order_relaxed)) {
|
2014-03-24 05:18:54 +00:00
|
|
|
ExecuteInputPoll();
|
|
|
|
|
2014-06-29 10:53:03 +00:00
|
|
|
// Try to update 250 times per second.
|
2014-03-24 05:18:54 +00:00
|
|
|
Sleep(4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::BeginPolling() {
|
2019-06-28 23:19:49 +00:00
|
|
|
threadRunningFlag.test_and_set(std::memory_order_relaxed);
|
|
|
|
inputThread = std::thread(&RunInputThread);
|
2014-03-24 05:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::StopPolling() {
|
2019-06-28 23:19:49 +00:00
|
|
|
threadRunningFlag.clear(std::memory_order_relaxed);
|
2012-11-12 17:32:35 +00:00
|
|
|
|
2019-06-28 23:19:49 +00:00
|
|
|
inputThread.join();
|
2014-03-24 05:18:54 +00:00
|
|
|
}
|
2015-02-28 22:02:03 +00:00
|
|
|
|
|
|
|
void InputDevice::GainFocus() {
|
2019-06-28 23:19:49 +00:00
|
|
|
focused.store(true, std::memory_order_relaxed);
|
2015-02-28 22:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::LoseFocus() {
|
2019-06-28 23:19:49 +00:00
|
|
|
focused.store(false, std::memory_order_relaxed);
|
2015-02-28 22:02:03 +00:00
|
|
|
}
|