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,12 +852,12 @@ void Console::setControllers(const string& rommd5)
// try to detect controllers // try to detect controllers
if(image != nullptr || size != 0) if(image != nullptr || size != 0)
{ {
left = ControllerDetector::detect(image, size, left, left = ControllerDetector::detectType(image, size, left,
!swappedPorts ? Controller::Left : Controller::Right, !swappedPorts ? Controller::Left : Controller::Right,
myOSystem.settings()); myOSystem.settings());
right = ControllerDetector::detect(image, size, right, right = ControllerDetector::detectType(image, size, right,
!swappedPorts ? Controller::Right : Controller::Left, !swappedPorts ? Controller::Right : Controller::Left,
myOSystem.settings()); myOSystem.settings());
} }
unique_ptr<Controller> leftC = getControllerPort(rommd5, left, Controller::Left), unique_ptr<Controller> leftC = getControllerPort(rommd5, left, Controller::Left),

View File

@ -20,9 +20,9 @@
#include "ControllerDetector.hxx" #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 string& controller, const Controller::Jack port,
const Settings& settings) const Settings& settings)
{ {
string type(controller); string type(controller);
@ -41,6 +41,14 @@ string ControllerDetector::detect(const uInt8* image, uInt32 size,
return type; 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, string ControllerDetector::autodetectPort(const uInt8* image, uInt32 size,
Controller::Jack port, const Settings& settings) Controller::Jack port, const Settings& settings)
@ -595,3 +603,44 @@ bool ControllerDetector::isProbablySaveKey(const uInt8* image, uInt32 size, Cont
return false; 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: 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 image A pointer to the ROM image
@param size The size of the ROM image @param size The size of the ROM image
@param controller The provided controller type of the ROM image @param controller The provided controller type of the ROM image
@param port The port to be checked @param port The port to be checked
@param settings A reference to the various settings (read-only) @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 string& controller, const Controller::Jack port,
const Settings& settings); 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: 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 image A pointer to the ROM image
@param size The size of the ROM image @param size The size of the ROM image
@param port The port to be checked @param port The port to be checked
@param settings A reference to the various settings (read-only) @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, static string autodetectPort(const uInt8* image, uInt32 size, Controller::Jack port,
const Settings& settings); const Settings& settings);

View File

@ -434,18 +434,12 @@ void GameInfoDialog::loadControllerProperties(const Properties& props)
if(myLeftPort->getSelectedTag().toString() == "AUTO") if(myLeftPort->getSelectedTag().toString() == "AUTO")
{ {
if(instance().hasConsole()) if(instance().hasConsole())
{
label = (!swapPorts ? instance().console().leftController().name() label = (!swapPorts ? instance().console().leftController().name()
: instance().console().rightController().name()) : instance().console().rightController().name()) + " detected";
+ " detected";
}
else if(autoDetect) else if(autoDetect)
{ label = ControllerDetector::detectName(image.get(), size, controller,
controller = ControllerDetector::detect(image.get(), size, controller, !swapPorts ? Controller::Jack::Left : Controller::Jack::Right,
!swapPorts ? Controller::Jack::Left : Controller::Jack::Right, instance().settings()) + " detected";
instance().settings());
label = getControllerName(controller) + " detected";
}
} }
myLeftPortDetected->setLabel(label); myLeftPortDetected->setLabel(label);
@ -456,18 +450,12 @@ void GameInfoDialog::loadControllerProperties(const Properties& props)
if(myRightPort->getSelectedTag().toString() == "AUTO") if(myRightPort->getSelectedTag().toString() == "AUTO")
{ {
if(instance().hasConsole()) if(instance().hasConsole())
{
label = (!swapPorts ? instance().console().rightController().name() label = (!swapPorts ? instance().console().rightController().name()
: instance().console().leftController().name()) : instance().console().leftController().name()) + " detected";
+ " detected";
}
else if(autoDetect) else if(autoDetect)
{ label = ControllerDetector::detectName(image.get(), size, controller,
controller = ControllerDetector::detect(image.get(), size, controller, !swapPorts ? Controller::Jack::Right : Controller::Jack::Left,
!swapPorts ? Controller::Jack::Right : Controller::Jack::Left, instance().settings()) + " detected";
instance().settings());
label = getControllerName(controller) + " detected";
}
} }
myRightPortDetected->setLabel(label); myRightPortDetected->setLabel(label);
@ -651,29 +639,6 @@ void GameInfoDialog::updateControllerStates()
myEraseEEPROMInfo->setEnabled(enableEEEraseButton); 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() void GameInfoDialog::eraseEEPROM()
{ {

View File

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

View File

@ -136,12 +136,12 @@ void RomInfoWidget::parseProperties(const FilesystemNode& node)
if(node.exists() && !node.isDirectory() && (image = instance().openROM(node, md5, size)) != nullptr) 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, !swappedPorts ? Controller::Jack::Left : Controller::Jack::Right,
instance().settings()); instance().settings());
right = ControllerDetector::detect(image.get(), size, right, right = ControllerDetector::detectName(image.get(), size, right,
!swappedPorts ? Controller::Jack::Right : Controller::Jack::Left, !swappedPorts ? Controller::Jack::Right : Controller::Jack::Left,
instance().settings()); instance().settings());
} }
myRomInfo.push_back("Controllers: " + (left + " (left), " + right + " (right)")); myRomInfo.push_back("Controllers: " + (left + " (left), " + right + " (right)"));