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"
|
2019-03-16 12:20:18 -04:00
|
|
|
#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-08-09 11:45:20 -04:00
|
|
|
#include "Multitap.h"
|
2019-08-09 20:47:12 -04:00
|
|
|
#include "SuperScope.h"
|
2019-05-12 21:18:05 -04:00
|
|
|
#include "EventType.h"
|
2019-03-12 09:15:57 -04:00
|
|
|
#include "../Utilities/Serializer.h"
|
2019-02-17 19:54:29 -05:00
|
|
|
|
2019-07-12 23:34:19 -04:00
|
|
|
ControlManager::ControlManager(Console* console)
|
2019-02-17 19:54:29 -05:00
|
|
|
{
|
|
|
|
_console = console;
|
2019-03-13 22:56:33 -04:00
|
|
|
_inputConfigVersion = -1;
|
2019-02-17 19:54:29 -05:00
|
|
|
_pollCounter = 0;
|
2019-03-16 12:20:18 -04:00
|
|
|
_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());
|
|
|
|
}
|
|
|
|
|
2019-10-18 17:05:44 -04:00
|
|
|
vector<ControllerData> ControlManager::GetPortStates()
|
2019-02-17 19:54:29 -05:00
|
|
|
{
|
|
|
|
auto lock = _deviceLock.AcquireSafe();
|
|
|
|
|
2019-10-18 17:05:44 -04:00
|
|
|
vector<ControllerData> states;
|
|
|
|
for(int i = 0; i < 2; i++) {
|
2019-02-17 19:54:29 -05:00
|
|
|
shared_ptr<BaseControlDevice> device = GetControlDevice(i);
|
|
|
|
if(device) {
|
2019-10-18 17:05:44 -04:00
|
|
|
states.push_back({ device->GetControllerType(), device->GetRawState() });
|
2019-02-17 19:54:29 -05:00
|
|
|
} else {
|
2019-10-18 17:05:44 -04:00
|
|
|
states.push_back({ ControllerType::None, ControlDeviceState()});
|
2019-02-17 19:54:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return states;
|
|
|
|
}
|
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-12 23:34:19 -04:00
|
|
|
shared_ptr<BaseControlDevice> ControlManager::CreateControllerDevice(ControllerType type, uint8_t port, Console* console)
|
2019-02-17 19:54:29 -05:00
|
|
|
{
|
|
|
|
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-08-09 20:47:12 -04:00
|
|
|
case ControllerType::SuperScope: device.reset(new SuperScope(console, port, cfg.Controllers[port].Keys)); break;
|
2019-08-09 11:45:20 -04:00
|
|
|
case ControllerType::Multitap: device.reset(new Multitap(console, port, cfg.Controllers[port].Keys, cfg.Controllers[2].Keys, cfg.Controllers[3].Keys, cfg.Controllers[4].Keys)); 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();
|
2019-03-16 12:20:18 -04:00
|
|
|
RegisterControlDevice(_systemActionManager);
|
2019-08-09 16:25:59 -04:00
|
|
|
for(int i = 0; i < 2; i++) {
|
2019-03-13 22:56:33 -04:00
|
|
|
shared_ptr<BaseControlDevice> device = CreateControllerDevice(GetControllerType(i), i, _console);
|
|
|
|
if(device) {
|
|
|
|
RegisterControlDevice(device);
|
|
|
|
}
|
2019-02-17 19:54:29 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-16 12:20:18 -04:00
|
|
|
_systemActionManager->ProcessSystemActions();
|
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
|
|
|
|
2019-10-16 20:22:45 -04:00
|
|
|
//string log = "F: " + std::to_string(_console->GetPpu()->GetFrameCount()) + " C:" + std::to_string(_pollCounter) + " ";
|
2019-02-17 19:54:29 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Debugger> debugger = _console->GetDebugger(false);
|
|
|
|
if(debugger) {
|
|
|
|
debugger->ProcessEvent(EventType::InputPolled);
|
2019-05-12 21:18:05 -04:00
|
|
|
}
|
2019-02-17 19:54:29 -05:00
|
|
|
|
|
|
|
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-12 17:49:58 -04:00
|
|
|
uint8_t value = _console->GetMemoryManager()->GetOpenBus() & (addr == 0x4016 ? 0xFC : 0xE0);
|
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)
|
|
|
|
{
|
2019-03-31 16:20:28 -04:00
|
|
|
InputConfig cfg = _console->GetSettings()->GetInputConfig();
|
2019-08-09 16:25:59 -04:00
|
|
|
s.Stream(cfg.Controllers[0].Type, cfg.Controllers[1].Type, cfg.Controllers[2].Type, cfg.Controllers[3].Type, cfg.Controllers[4].Type);
|
2019-03-31 16:20:28 -04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|