mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-07-23 02:24:38 +00:00
91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "Emulator/Host/controller.h"
|
|
#include "core/hle/libraries/libkernel/time_management.h"
|
|
|
|
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};
|
|
|
|
*isConnected = m_connected;
|
|
*connectedCount = m_connected_count;
|
|
*state = getLastState();
|
|
}
|
|
|
|
int GameController::ReadStates(State* states, int states_num, bool* isConnected,
|
|
int* connectedCount) {
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
*isConnected = m_connected;
|
|
*connectedCount = m_connected_count;
|
|
|
|
int ret_num = 0;
|
|
|
|
if (m_connected) {
|
|
if (m_states_num == 0) {
|
|
ret_num = 1;
|
|
states[0] = m_last_state;
|
|
} else {
|
|
for (uint32_t i = 0; i < m_states_num; i++) {
|
|
if (ret_num >= states_num) {
|
|
break;
|
|
}
|
|
auto index = (m_first_state + i) % MAX_STATES;
|
|
if (!m_private[index].obtained) {
|
|
m_private[index].obtained = true;
|
|
|
|
states[ret_num++] = m_states[index];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret_num;
|
|
}
|
|
|
|
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_private[index].obtained = false;
|
|
m_states_num++;
|
|
}
|
|
|
|
void GameController::checKButton(int id, u32 button, bool isPressed) {
|
|
std::scoped_lock lock{m_mutex};
|
|
auto state = getLastState();
|
|
state.time = Core::Libraries::LibKernel::sceKernelGetProcessTime();
|
|
if (isPressed) {
|
|
state.buttonsState |= button;
|
|
} else {
|
|
state.buttonsState &= ~button;
|
|
}
|
|
|
|
addState(state);
|
|
}
|
|
|
|
} // namespace Emulator::Host::Controller
|