From e81326eefb73dd423bbdf9da373cd153ef8cde26 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Fri, 15 Feb 2019 23:46:09 +0100 Subject: [PATCH] auto detect controller in GameInfoDialog when started from Launcher --- src/gui/GameInfoDialog.cxx | 94 ++++++++++++++++++++++++++++++-------- src/gui/LauncherDialog.cxx | 10 ++++ 2 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/gui/GameInfoDialog.cxx b/src/gui/GameInfoDialog.cxx index 27de81589..e39f04b89 100644 --- a/src/gui/GameInfoDialog.cxx +++ b/src/gui/GameInfoDialog.cxx @@ -24,6 +24,7 @@ #include "RadioButtonWidget.hxx" #include "Launcher.hxx" #include "OSystem.hxx" +#include "ControllerDetector.hxx" #include "PopUpWidget.hxx" #include "Props.hxx" #include "PropsSet.hxx" @@ -415,28 +416,60 @@ void GameInfoDialog::loadConsoleProperties(const Properties& props) void GameInfoDialog::loadControllerProperties(const Properties& props) { bool swapPorts = props.get(Console_SwapPorts) == "YES"; + bool autoDetect = false; + BytePtr image; + string md5 = props.get(Cartridge_MD5); + uInt32 size = 0; + const FilesystemNode& node = FilesystemNode(instance().launcher().selectedRom()); - myLeftPort->setSelected(props.get(Controller_Left), "AUTO"); - if(instance().hasConsole() && myLeftPort->getSelectedTag().toString() == "AUTO") - { - myLeftPortDetected->setLabel((!swapPorts - ? instance().console().leftController().name() - : instance().console().rightController().name()) - + " detected"); - } - else - myLeftPortDetected->setLabel(""); + // try to load the image for auto detection + if(!instance().hasConsole() && + node.exists() && !node.isDirectory() && (image = instance().openROM(node, md5, size)) != nullptr) + autoDetect = true; - myRightPort->setSelected(props.get(Controller_Right), "AUTO"); - if(instance().hasConsole() && myRightPort->getSelectedTag().toString() == "AUTO") + string label = ""; + string controller = props.get(Controller_Left); + + myLeftPort->setSelected(controller, "AUTO"); + if(myLeftPort->getSelectedTag().toString() == "AUTO") { - myRightPortDetected->setLabel((!swapPorts - ? instance().console().rightController().name() - : instance().console().leftController().name()) - + " detected"); + if(instance().hasConsole()) + { + label = (!swapPorts ? instance().console().leftController().name() + : instance().console().rightController().name()) + + " detected"; + } + else if(autoDetect) + { + controller = ControllerDetector::detect(image.get(), size, controller, + !swapPorts ? Controller::Jack::Left : Controller::Jack::Right, + instance().settings()); + label = getControllerName(controller) + " detected"; + } } - else - myRightPortDetected->setLabel(""); + myLeftPortDetected->setLabel(label); + + label = ""; + controller = props.get(Controller_Right); + + myRightPort->setSelected(controller, "AUTO"); + if(myRightPort->getSelectedTag().toString() == "AUTO") + { + if(instance().hasConsole()) + { + label = (!swapPorts ? instance().console().rightController().name() + : instance().console().leftController().name()) + + " detected"; + } + else if(autoDetect) + { + controller = ControllerDetector::detect(image.get(), size, controller, + !swapPorts ? Controller::Jack::Right : Controller::Jack::Left, + instance().settings()); + label = getControllerName(controller) + " detected"; + } + } + myRightPortDetected->setLabel(label); mySwapPorts->setState(props.get(Console_SwapPorts) == "YES"); mySwapPaddles->setState(props.get(Controller_SwapPaddles) == "YES"); @@ -618,6 +651,29 @@ 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() { @@ -698,4 +754,4 @@ void GameInfoDialog::handleCommand(CommandSender* sender, int cmd, Dialog::handleCommand(sender, cmd, data, 0); break; } -} +} \ No newline at end of file diff --git a/src/gui/LauncherDialog.cxx b/src/gui/LauncherDialog.cxx index 55a503d16..d1b32899c 100644 --- a/src/gui/LauncherDialog.cxx +++ b/src/gui/LauncherDialog.cxx @@ -208,6 +208,16 @@ LauncherDialog::LauncherDialog(OSystem& osystem, DialogContainer& parent, myAllFiles->setState(!onlyROMs); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +const string& LauncherDialog::selectedRom() +{ + int item = myList->getSelected(); + if(item < 0) + return EmptyString; + + return myGameList->path(item); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - const string& LauncherDialog::selectedRomMD5() {