Mesen-SX/Core/ControlManager.cpp

228 lines
5.6 KiB
C++
Raw Normal View History

2019-02-17 19:54:29 -05:00
#include "stdafx.h"
#include "ControlManager.h"
#include "Console.h"
2019-03-13 22:56:33 -04:00
#include "EmuSettings.h"
2019-02-17 19:54:29 -05:00
#include "MemoryManager.h"
#include "KeyManager.h"
#include "IKeyManager.h"
#include "IInputProvider.h"
#include "IInputRecorder.h"
#include "SystemActionManager.h"
2019-02-17 19:54:29 -05:00
#include "SnesController.h"
2019-03-15 14:16:27 -04:00
#include "SnesMouse.h"
2019-03-12 09:15:57 -04:00
#include "../Utilities/Serializer.h"
2019-02-17 19:54:29 -05:00
ControlManager::ControlManager(shared_ptr<Console> console)
{
_console = console;
2019-03-13 22:56:33 -04:00
_inputConfigVersion = -1;
2019-02-17 19:54:29 -05:00
_pollCounter = 0;
_systemActionManager.reset(new SystemActionManager(console));
2019-02-17 19:54:29 -05:00
UpdateControlDevices();
}
ControlManager::~ControlManager()
{
}
void ControlManager::RegisterInputProvider(IInputProvider* provider)
{
auto lock = _deviceLock.AcquireSafe();
_inputProviders.push_back(provider);
}
void ControlManager::UnregisterInputProvider(IInputProvider* provider)
{
auto lock = _deviceLock.AcquireSafe();
vector<IInputProvider*> &vec = _inputProviders;
vec.erase(std::remove(vec.begin(), vec.end(), provider), vec.end());
}
void ControlManager::RegisterInputRecorder(IInputRecorder* provider)
{
auto lock = _deviceLock.AcquireSafe();
_inputRecorders.push_back(provider);
}
void ControlManager::UnregisterInputRecorder(IInputRecorder* provider)
{
auto lock = _deviceLock.AcquireSafe();
vector<IInputRecorder*> &vec = _inputRecorders;
vec.erase(std::remove(vec.begin(), vec.end(), provider), vec.end());
}
vector<ControlDeviceState> ControlManager::GetPortStates()
{
auto lock = _deviceLock.AcquireSafe();
vector<ControlDeviceState> states;
for(int i = 0; i < 4; i++) {
shared_ptr<BaseControlDevice> device = GetControlDevice(i);
if(device) {
states.push_back(device->GetRawState());
} else {
states.push_back(ControlDeviceState());
}
}
return states;
}
shared_ptr<SystemActionManager> ControlManager::GetSystemActionManager()
{
return _systemActionManager;
}
2019-02-17 19:54:29 -05:00
shared_ptr<BaseControlDevice> ControlManager::GetControlDevice(uint8_t port)
{
auto lock = _deviceLock.AcquireSafe();
auto result = std::find_if(_controlDevices.begin(), _controlDevices.end(), [port](const shared_ptr<BaseControlDevice> control) { return control->GetPort() == port; });
if(result != _controlDevices.end()) {
return *result;
}
return nullptr;
}
vector<shared_ptr<BaseControlDevice>> ControlManager::GetControlDevices()
{
return _controlDevices;
}
void ControlManager::RegisterControlDevice(shared_ptr<BaseControlDevice> controlDevice)
{
_controlDevices.push_back(controlDevice);
}
ControllerType ControlManager::GetControllerType(uint8_t port)
{
2019-03-13 22:56:33 -04:00
return _console->GetSettings()->GetInputConfig().Controllers[port].Type;
2019-02-17 19:54:29 -05:00
}
shared_ptr<BaseControlDevice> ControlManager::CreateControllerDevice(ControllerType type, uint8_t port, shared_ptr<Console> console)
{
shared_ptr<BaseControlDevice> device;
2019-03-13 22:56:33 -04:00
InputConfig cfg = console->GetSettings()->GetInputConfig();
2019-02-17 19:54:29 -05:00
switch(type) {
case ControllerType::None: break;
2019-03-13 22:56:33 -04:00
case ControllerType::SnesController: device.reset(new SnesController(console, port, cfg.Controllers[port].Keys)); break;
2019-03-15 14:16:27 -04:00
case ControllerType::SnesMouse: device.reset(new SnesMouse(console, port)); break;
2019-03-13 22:56:33 -04:00
case ControllerType::SuperScope: break;
2019-02-17 19:54:29 -05:00
}
return device;
}
void ControlManager::UpdateControlDevices()
{
2019-03-13 22:56:33 -04:00
uint32_t version = _console->GetSettings()->GetInputConfigVersion();
if(_inputConfigVersion != version) {
_inputConfigVersion = version;
2019-02-17 19:54:29 -05:00
2019-03-13 22:56:33 -04:00
auto lock = _deviceLock.AcquireSafe();
_controlDevices.clear();
RegisterControlDevice(_systemActionManager);
2019-03-13 22:56:33 -04:00
for(int i = 0; i < 4; i++) {
shared_ptr<BaseControlDevice> device = CreateControllerDevice(GetControllerType(i), i, _console);
if(device) {
RegisterControlDevice(device);
}
2019-02-17 19:54:29 -05:00
}
}
_systemActionManager->ProcessSystemActions();
2019-02-17 19:54:29 -05:00
}
uint8_t ControlManager::GetOpenBusMask(uint8_t port)
{
2019-04-10 20:45:59 -04:00
if(port == 0x4016) {
return 0xFC;
} else {
return 0xE0;
}
2019-02-17 19:54:29 -05:00
}
void ControlManager::UpdateInputState()
{
KeyManager::RefreshKeyState();
2019-03-13 22:56:33 -04:00
auto lock = _deviceLock.AcquireSafe();
2019-02-17 19:54:29 -05:00
string log = "";
for(shared_ptr<BaseControlDevice> &device : _controlDevices) {
device->ClearState();
bool inputSet = false;
for(size_t i = 0; i < _inputProviders.size(); i++) {
IInputProvider* provider = _inputProviders[i];
if(provider->SetInput(device.get())) {
inputSet = true;
break;
}
}
if(!inputSet) {
device->SetStateFromInput();
}
device->OnAfterSetState();
//log += "|" + device->GetTextState();
}
//TODO
/*
shared_ptr<Debugger> debugger = _console->GetDebugger(false);
if(debugger) {
debugger->ProcessEvent(EventType::InputPolled);
}*/
for(IInputRecorder* recorder : _inputRecorders) {
recorder->RecordInput(_controlDevices);
}
//MessageManager::Log(log);
_pollCounter++;
}
uint32_t ControlManager::GetPollCounter()
{
return ControlManager::_pollCounter;
}
void ControlManager::SetPollCounter(uint32_t value)
{
_pollCounter = value;
}
uint8_t ControlManager::Read(uint16_t addr)
{
2019-04-10 20:45:59 -04:00
uint8_t value = _console->GetMemoryManager()->GetOpenBus() & GetOpenBusMask(addr - 0x4016);
2019-02-17 19:54:29 -05:00
for(shared_ptr<BaseControlDevice> &device : _controlDevices) {
value |= device->ReadRam(addr);
}
return value;
}
void ControlManager::Write(uint16_t addr, uint8_t value)
{
for(shared_ptr<BaseControlDevice> &device : _controlDevices) {
device->WriteRam(addr, value);
}
2019-03-12 09:15:57 -04:00
}
void ControlManager::Serialize(Serializer &s)
{
InputConfig cfg = _console->GetSettings()->GetInputConfig();
s.Stream(cfg.Controllers[0].Type, cfg.Controllers[1].Type, cfg.Controllers[2].Type, cfg.Controllers[3].Type);
if(!s.IsSaving()) {
_console->GetSettings()->SetInputConfig(cfg);
UpdateControlDevices();
}
2019-03-12 09:15:57 -04:00
for(shared_ptr<BaseControlDevice> &device : _controlDevices) {
s.Stream(device.get());
}
}