a bit controller detection refactoring

This commit is contained in:
thrust26 2019-02-16 11:46:42 +01:00
parent 9580385f21
commit 3c6b8339e1
6 changed files with 105 additions and 66 deletions

View File

@ -852,10 +852,10 @@ void Console::setControllers(const string& rommd5)
// try to detect controllers
if(image != nullptr || size != 0)
{
left = ControllerDetector::detect(image, size, left,
left = ControllerDetector::detectType(image, size, left,
!swappedPorts ? Controller::Left : Controller::Right,
myOSystem.settings());
right = ControllerDetector::detect(image, size, right,
right = ControllerDetector::detectType(image, size, right,
!swappedPorts ? Controller::Right : Controller::Left,
myOSystem.settings());
}

View File

@ -20,7 +20,7 @@
#include "ControllerDetector.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerDetector::detect(const uInt8* image, uInt32 size,
string ControllerDetector::detectType(const uInt8* image, uInt32 size,
const string& controller, const Controller::Jack port,
const Settings& settings)
{
@ -41,6 +41,14 @@ string ControllerDetector::detect(const uInt8* image, uInt32 size,
return type;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerDetector::detectName(const uInt8* image, uInt32 size,
const string& controller, const Controller::Jack port,
const Settings& settings)
{
return getControllerName(detectType(image, size, controller, port, settings));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerDetector::autodetectPort(const uInt8* image, uInt32 size,
Controller::Jack port, const Settings& settings)
@ -595,3 +603,44 @@ bool ControllerDetector::isProbablySaveKey(const uInt8* image, uInt32 size, Cont
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string ControllerDetector::getControllerName(const string& controller)
{
// auto detected:
if(BSPF::equalsIgnoreCase(controller, "JOYSTICK"))
return "Joystick";
if(BSPF::equalsIgnoreCase(controller, "SAVEKEY"))
return "SaveKey";
if(BSPF::equalsIgnoreCase(controller, "TRAKBALL"))
return "TrakBall";
if(BSPF::equalsIgnoreCase(controller, "ATARIMOUSE"))
return "AtariMouse";
if(BSPF::equalsIgnoreCase(controller, "AMIGAMOUSE"))
return "AmigaMouse";
if(BSPF::equalsIgnoreCase(controller, "KEYBOARD"))
return "Keyboard";
if(BSPF::equalsIgnoreCase(controller, "GENESIS"))
return "Sega Genesis";
if(BSPF::equalsIgnoreCase(controller, "PADDLES"))
return "Paddles";
// not auto detected:
if(BSPF::equalsIgnoreCase(controller, "BOOSTERGRIP"))
return "BoosterGrip";
if(BSPF::equalsIgnoreCase(controller, "DRIVING"))
return "Driving";
if(BSPF::equalsIgnoreCase(controller, "MINDLINK"))
return "MindLink";
if(BSPF::equalsIgnoreCase(controller, "ATARIVOX"))
return "AtariVox";
if(BSPF::equalsIgnoreCase(controller, "PADDLES_IAXIS"))
return "Paddles IAxis";
if(BSPF::equalsIgnoreCase(controller, "PADDLES_IAXDR"))
return "Paddles IAxDr";
if(BSPF::equalsIgnoreCase(controller, "COMPUMATE"))
return "CompuMate";
if(BSPF::equalsIgnoreCase(controller, "KIDVID"))
return "KidVid";
return controller;
}

View File

@ -31,28 +31,54 @@ class ControllerDetector
{
public:
/**
Detects the controller at the given port if no controller is provided.
Detects the controller type at the given port if no controller is provided.
@param image A pointer to the ROM image
@param size The size of the ROM image
@param controller The provided controller type of the ROM image
@param port The port to be checked
@param settings A reference to the various settings (read-only)
@return The detected controller name
@return The detected controller type
*/
static string detect(const uInt8* image, uInt32 size,
static string detectType(const uInt8* image, uInt32 size,
const string& controller, const Controller::Jack port,
const Settings& settings);
/**
Detects the controller type at the given port if no controller is provided
and returns its name.
@param image A pointer to the ROM image
@param size The size of the ROM image
@param controller The provided controller type of the ROM image
@param port The port to be checked
@param settings A reference to the various settings (read-only)
@return The (detected) controller name
*/
static string detectName(const uInt8* image, uInt32 size,
const string& controller, const Controller::Jack port,
const Settings& settings);
/**
Returns a nicer formatted name for the given controller.
@param controller The provided controller type of the ROM image
@return The controller name
*/
static const string getControllerName(const string& controller);
private:
/**
Detects the controller at the given port.
Detects the controller type at the given port.
@param image A pointer to the ROM image
@param size The size of the ROM image
@param port The port to be checked
@param settings A reference to the various settings (read-only)
@return The detected controller name
@return The detected controller type
*/
static string autodetectPort(const uInt8* image, uInt32 size, Controller::Jack port,
const Settings& settings);

View File

@ -434,18 +434,12 @@ void GameInfoDialog::loadControllerProperties(const Properties& props)
if(myLeftPort->getSelectedTag().toString() == "AUTO")
{
if(instance().hasConsole())
{
label = (!swapPorts ? instance().console().leftController().name()
: instance().console().rightController().name())
+ " detected";
}
: instance().console().rightController().name()) + " detected";
else if(autoDetect)
{
controller = ControllerDetector::detect(image.get(), size, controller,
label = ControllerDetector::detectName(image.get(), size, controller,
!swapPorts ? Controller::Jack::Left : Controller::Jack::Right,
instance().settings());
label = getControllerName(controller) + " detected";
}
instance().settings()) + " detected";
}
myLeftPortDetected->setLabel(label);
@ -456,18 +450,12 @@ void GameInfoDialog::loadControllerProperties(const Properties& props)
if(myRightPort->getSelectedTag().toString() == "AUTO")
{
if(instance().hasConsole())
{
label = (!swapPorts ? instance().console().rightController().name()
: instance().console().leftController().name())
+ " detected";
}
: instance().console().leftController().name()) + " detected";
else if(autoDetect)
{
controller = ControllerDetector::detect(image.get(), size, controller,
label = ControllerDetector::detectName(image.get(), size, controller,
!swapPorts ? Controller::Jack::Right : Controller::Jack::Left,
instance().settings());
label = getControllerName(controller) + " detected";
}
instance().settings()) + " detected";
}
myRightPortDetected->setLabel(label);
@ -651,29 +639,6 @@ void GameInfoDialog::updateControllerStates()
myEraseEEPROMInfo->setEnabled(enableEEEraseButton);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string GameInfoDialog::getControllerName(const string& type)
{
if(BSPF::equalsIgnoreCase(type, "JOYSTICK"))
return "Joystick";
if(BSPF::equalsIgnoreCase(type, "SAVEKEY"))
return "SaveKey";
if(BSPF::equalsIgnoreCase(type, "TRAKBALL"))
return "TrakBall";
if(BSPF::equalsIgnoreCase(type, "ATA RIMOUSE"))
return "AtariMouse";
if(BSPF::equalsIgnoreCase(type, "AMIGAMOUSE"))
return "AmigaMouse";
if(BSPF::equalsIgnoreCase(type, "KEYBOARD"))
return "Keyboard";
if(BSPF::equalsIgnoreCase(type, "GENESIS"))
return "Genesis";
if(BSPF::equalsIgnoreCase(type, "PADDLES"))
return "Paddles";
return type;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void GameInfoDialog::eraseEEPROM()
{

View File

@ -55,7 +55,6 @@ class GameInfoDialog : public Dialog, public CommandSender
void loadCartridgeProperties(const Properties& props);
void updateControllerStates();
const string getControllerName(const string& type);
void eraseEEPROM();
private:

View File

@ -136,10 +136,10 @@ void RomInfoWidget::parseProperties(const FilesystemNode& node)
if(node.exists() && !node.isDirectory() && (image = instance().openROM(node, md5, size)) != nullptr)
{
left = ControllerDetector::detect(image.get(), size, left,
left = ControllerDetector::detectName(image.get(), size, left,
!swappedPorts ? Controller::Jack::Left : Controller::Jack::Right,
instance().settings());
right = ControllerDetector::detect(image.get(), size, right,
right = ControllerDetector::detectName(image.get(), size, right,
!swappedPorts ? Controller::Jack::Right : Controller::Jack::Left,
instance().settings());
}