shadPS4/src/Emulator/Host/controller.cpp

53 lines
1.3 KiB
C++
Raw Normal View History

2023-10-13 08:40:59 +02:00
#include "controller.h"
2023-10-31 07:47:58 +01:00
#include <core/hle/libraries/libkernel/time_management.h>
2023-10-13 08:40:59 +02:00
namespace Emulator::Host::Controller {
GameController::GameController() { m_states_num = 0;
m_last_state = State();
}
void GameController::readState(State* state, bool* isConnected, int* connectedCount) {
std::scoped_lock lock{m_mutex};
2023-10-13 08:40:59 +02:00
*isConnected = m_connected;
*connectedCount = m_connected_count;
*state = getLastState();
}
State GameController::getLastState() const {
if (m_states_num == 0) {
return m_last_state;
}
auto last = (m_first_state + m_states_num - 1) % MAX_STATES;
return m_states[last];
}
void GameController::addState(const State& state) {
if (m_states_num >= MAX_STATES) {
m_states_num = MAX_STATES - 1;
m_first_state = (m_first_state + 1) % MAX_STATES;
}
auto index = (m_first_state + m_states_num) % MAX_STATES;
m_states[index] = state;
m_last_state = state;
m_states_num++;
}
void GameController::checKButton(int id, u32 button, bool isPressed) {
std::scoped_lock lock{m_mutex};
2023-10-13 08:40:59 +02:00
auto state = getLastState();
2023-10-30 11:43:39 +01:00
state.time = Core::Libraries::LibKernel::sceKernelGetProcessTime();
2023-10-13 08:40:59 +02:00
if (isPressed) {
state.buttonsState |= button;
} else {
state.buttonsState &= ~button;
}
addState(state);
}
} // namespace Emulator::Host::Controller