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/.
|
|
|
|
|
2012-11-12 17:32:35 +00:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
2014-03-24 05:18:54 +00:00
|
|
|
#include "base/mutex.h"
|
|
|
|
#include "input/input_state.h"
|
|
|
|
#include "thread/thread.h"
|
|
|
|
#include "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"
|
|
|
|
#include "Windows/XinputDevice.h"
|
|
|
|
#include "Windows/DinputDevice.h"
|
|
|
|
#include "Windows/KeyboardDevice.h"
|
|
|
|
#include "Windows/WindowsHost.h"
|
|
|
|
|
|
|
|
static volatile bool inputThreadStatus = false;
|
|
|
|
static volatile bool inputThreadEnabled = false;
|
|
|
|
static std::thread *inputThread = NULL;
|
|
|
|
static recursive_mutex inputMutex;
|
|
|
|
static condition_variable inputEndCond;
|
2015-02-28 22:02:03 +00:00
|
|
|
static bool focused = true;
|
2014-03-24 05:18:54 +00:00
|
|
|
|
|
|
|
extern InputState input_state;
|
|
|
|
|
|
|
|
inline static void ExecuteInputPoll() {
|
2014-06-29 10:53:03 +00:00
|
|
|
// Hm, we may hold the input_state lock for quite a while (time it takes to poll all devices)...
|
|
|
|
// If that becomes an issue, maybe should poll to a copy of inputstate and only hold the lock while
|
|
|
|
// copying that one to the real one?
|
2014-03-24 05:18:54 +00:00
|
|
|
lock_guard guard(input_state.lock);
|
|
|
|
input_state.pad_buttons = 0;
|
|
|
|
input_state.pad_lstick_x = 0;
|
|
|
|
input_state.pad_lstick_y = 0;
|
|
|
|
input_state.pad_rstick_x = 0;
|
|
|
|
input_state.pad_rstick_y = 0;
|
2015-02-28 22:02:03 +00:00
|
|
|
if (host && (focused || !g_Config.bGamepadOnlyFocused)) {
|
2014-03-24 05:18:54 +00:00
|
|
|
host->PollControllers(input_state);
|
|
|
|
}
|
|
|
|
UpdateInputState(&input_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RunInputThread() {
|
2014-06-29 10:53:03 +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.
|
|
|
|
|
|
|
|
while (inputThreadEnabled) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
lock_guard guard(inputMutex);
|
|
|
|
inputThreadStatus = false;
|
|
|
|
inputEndCond.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::BeginPolling() {
|
|
|
|
lock_guard guard(inputMutex);
|
|
|
|
inputThreadEnabled = true;
|
|
|
|
inputThread = new std::thread(&RunInputThread);
|
|
|
|
inputThread->detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::StopPolling() {
|
|
|
|
inputThreadEnabled = false;
|
2012-11-12 17:32:35 +00:00
|
|
|
|
2014-03-24 05:18:54 +00:00
|
|
|
lock_guard guard(inputMutex);
|
|
|
|
if (inputThreadStatus) {
|
|
|
|
inputEndCond.wait(inputMutex);
|
|
|
|
}
|
|
|
|
delete inputThread;
|
|
|
|
inputThread = NULL;
|
|
|
|
}
|
2015-02-28 22:02:03 +00:00
|
|
|
|
|
|
|
void InputDevice::GainFocus() {
|
|
|
|
focused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::LoseFocus() {
|
|
|
|
focused = false;
|
|
|
|
}
|