From b5e5199eeb8b20c3d3f23b7226edbeea30b684db Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Tue, 21 Nov 2017 16:10:58 +0000 Subject: [PATCH] Move all configurations to a separate file. Still not ideal as it is not straightforward to add options to places like the Video class. Signed-off-by: Andrea Odetti --- source/frontends/qapple/CMakeLists.txt | 1 + source/frontends/qapple/configuration.cpp | 222 ++++++++++++++++++++++ source/frontends/qapple/configuration.h | 15 ++ source/frontends/qapple/preferences.cpp | 6 +- source/frontends/qapple/qapple.cpp | 197 +------------------ source/frontends/qapple/qapple.pro | 6 +- 6 files changed, 249 insertions(+), 198 deletions(-) create mode 100644 source/frontends/qapple/configuration.cpp create mode 100644 source/frontends/qapple/configuration.h diff --git a/source/frontends/qapple/CMakeLists.txt b/source/frontends/qapple/CMakeLists.txt index 057c26c7..1abc1518 100644 --- a/source/frontends/qapple/CMakeLists.txt +++ b/source/frontends/qapple/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(qapple gamepadpaddle.cpp video.cpp settings.cpp + configuration.cpp commands.cpp chunks.cpp diff --git a/source/frontends/qapple/configuration.cpp b/source/frontends/qapple/configuration.cpp new file mode 100644 index 00000000..fe41804b --- /dev/null +++ b/source/frontends/qapple/configuration.cpp @@ -0,0 +1,222 @@ +#include "configuration.h" + +#include "StdAfx.h" +#include "Common.h" +#include "Applewin.h" +#include "Disk.h" +#include "Harddisk.h" +#include "Registry.h" +#include "SaveState.h" +#include "CPU.h" + +#include "linux/paddle.h" + +#include "gamepadpaddle.h" + +#include +#include + +namespace +{ + const std::vector diskIDs = {DRIVE_1, DRIVE_2}; + const std::vector hdIDs = {HARDDISK_1, HARDDISK_2}; + + void insertDisk(const QString & filename, const int disk) + { + if (filename.isEmpty()) + { + DiskEject(disk); + } + else + { + const bool createMissingDisk = true; + const ImageError_e result = DiskInsert(disk, filename.toStdString().c_str(), IMAGE_USE_FILES_WRITE_PROTECT_STATUS, createMissingDisk); + if (result != eIMAGE_ERROR_NONE) + { + const QString message = QString("Error [%1] inserting '%2'").arg(QString::number(result), filename); + QMessageBox::warning(NULL, "Disk error", message); + } + } + } + + void insertHD(const QString & filename, const int disk) + { + if (filename.isEmpty()) + { + HD_Unplug(disk); + } + else + { + if (!HD_Insert(disk, filename.toStdString().c_str())) + { + const QString message = QString("Error inserting '%1'").arg(filename); + QMessageBox::warning(NULL, "Hard Disk error", message); + } + } + } + + void setSlot4(const SS_CARDTYPE newCardType) + { + g_Slot4 = newCardType; + REGSAVE(TEXT(REGVALUE_SLOT4), (DWORD)g_Slot4); + } + + void setSlot5(const SS_CARDTYPE newCardType) + { + g_Slot5 = newCardType; + REGSAVE(TEXT(REGVALUE_SLOT5), (DWORD)g_Slot5); + } + + const std::vector computerTypes = {A2TYPE_APPLE2, A2TYPE_APPLE2PLUS, A2TYPE_APPLE2E, A2TYPE_APPLE2EENHANCED, + A2TYPE_PRAVETS82, A2TYPE_PRAVETS8M, A2TYPE_PRAVETS8A, A2TYPE_TK30002E}; + + int getApple2ComputerType() + { + const eApple2Type type = GetApple2Type(); + const auto it = std::find(computerTypes.begin(), computerTypes.end(), type); + if (it != computerTypes.end()) + { + return std::distance(computerTypes.begin(), it); + } + else + { + // default to A2E + return 2; + } + } + + void setScreenshotTemplate(const QString & filenameTemplate) + { + QSettings().setValue("QApple/Screenshot Template", filenameTemplate); + } + +} + +QString getScreenshotTemplate() +{ + const QString filenameTemplate = QSettings().value("QApple/Screenshot Template", "/tmp/qapple_%1.png").toString(); + return filenameTemplate; +} + +Preferences::Data getCurrentOptions(const std::shared_ptr & gamepad) +{ + Preferences::Data currentOptions; + + currentOptions.disks.resize(diskIDs.size()); + for (size_t i = 0; i < diskIDs.size(); ++i) + { + const char * diskName = DiskGetFullName(diskIDs[i]); + if (diskName) + { + currentOptions.disks[i] = diskName; + } + } + + currentOptions.hds.resize(hdIDs.size()); + for (size_t i = 0; i < hdIDs.size(); ++i) + { + const char * diskName = HD_GetFullName(hdIDs[i]); + if (diskName) + { + currentOptions.hds[i] = diskName; + } + } + + currentOptions.mouseInSlot4 = g_Slot4 == CT_MouseInterface; + currentOptions.cpmInSlot5 = g_Slot5 == CT_Z80; + currentOptions.hdInSlot7 = HD_CardIsEnabled(); + + currentOptions.apple2Type = getApple2ComputerType(); + + if (gamepad) + { + currentOptions.joystick = gamepad->name(); + currentOptions.joystickId = gamepad->deviceId(); + } + else + { + currentOptions.joystickId = 0; + } + + const char* saveState = Snapshot_GetFilename(); + if (saveState) + { + currentOptions.saveState = QString::fromUtf8(saveState);; + } + + currentOptions.screenshotTemplate = getScreenshotTemplate(); + + return currentOptions; +} + +void setNewOptions(const Preferences::Data & currentOptions, const Preferences::Data & newOptions, std::shared_ptr & gamepad) +{ + if (currentOptions.apple2Type != newOptions.apple2Type) + { + const eApple2Type type = computerTypes[newOptions.apple2Type]; + SetApple2Type(type); + REGSAVE(TEXT(REGVALUE_APPLE2_TYPE), type); + const eCpuType cpu = ProbeMainCpuDefault(type); + SetMainCpu(cpu); + REGSAVE(TEXT(REGVALUE_CPU_TYPE), cpu); + } + + if (currentOptions.mouseInSlot4 != newOptions.mouseInSlot4) + { + const SS_CARDTYPE card = newOptions.mouseInSlot4 ? CT_MouseInterface : CT_Empty; + setSlot4(card); + } + if (currentOptions.cpmInSlot5 != newOptions.cpmInSlot5) + { + const SS_CARDTYPE card = newOptions.cpmInSlot5 ? CT_Z80 : CT_Empty; + setSlot5(card); + } + if (currentOptions.hdInSlot7 != newOptions.hdInSlot7) + { + REGSAVE(TEXT(REGVALUE_HDD_ENABLED), newOptions.hdInSlot7 ? 1 : 0); + HD_SetEnabled(newOptions.hdInSlot7); + } + + if (newOptions.joystick.isEmpty()) + { + gamepad.reset(); + Paddle::instance() = std::make_shared(); + } + else + { + if (newOptions.joystickId != currentOptions.joystickId) + { + gamepad.reset(new QGamepad(newOptions.joystickId)); + Paddle::instance() = std::make_shared(gamepad); + } + } + + for (size_t i = 0; i < diskIDs.size(); ++i) + { + if (currentOptions.disks[i] != newOptions.disks[i]) + { + insertDisk(newOptions.disks[i], diskIDs[i]); + } + } + + for (size_t i = 0; i < hdIDs.size(); ++i) + { + if (currentOptions.hds[i] != newOptions.hds[i]) + { + insertHD(newOptions.hds[i], hdIDs[i]); + } + } + + if (currentOptions.saveState != newOptions.saveState) + { + const std::string name = newOptions.saveState.toStdString(); + Snapshot_SetFilename(name); + RegSaveString(TEXT(REG_CONFIG), REGVALUE_SAVESTATE_FILENAME, 1, name.c_str()); + } + + if (currentOptions.screenshotTemplate != newOptions.screenshotTemplate) + { + setScreenshotTemplate(newOptions.screenshotTemplate); + } + +} diff --git a/source/frontends/qapple/configuration.h b/source/frontends/qapple/configuration.h new file mode 100644 index 00000000..af0f4e9e --- /dev/null +++ b/source/frontends/qapple/configuration.h @@ -0,0 +1,15 @@ +#ifndef CONFIGURATION_H +#define CONFIGURATION_H + +#include + +#include "preferences.h" + +class QGamepad; + +QString getScreenshotTemplate(); +Preferences::Data getCurrentOptions(const std::shared_ptr & gamepad); +void setNewOptions(const Preferences::Data & currentOptions, const Preferences::Data & newOptions, + std::shared_ptr & gamepad); + +#endif // CONFIGURATION_H diff --git a/source/frontends/qapple/preferences.cpp b/source/frontends/qapple/preferences.cpp index e5cddc92..035f05ca 100644 --- a/source/frontends/qapple/preferences.cpp +++ b/source/frontends/qapple/preferences.cpp @@ -132,9 +132,9 @@ void Preferences::setup(const Data & data, QSettings & settings) void Preferences::populateJoysticks() { joystick->clear(); - const QList gamepads = QGamepadManager::instance()->connectedGamepads(); + joystick->addItem("None"); // index = 0 - joystick->addItem("None"); + const QList gamepads = QGamepadManager::instance()->connectedGamepads(); for (int id : gamepads) { @@ -194,6 +194,7 @@ Preferences::Data Preferences::getData() const data.cpmInSlot5 = cpm_5->isChecked(); data.hdInSlot7 = hd_7->isChecked(); + // because index = 0 is None if (joystick->currentIndex() >= 1) { const QVariant & device = joystick->itemData(joystick->currentIndex()); @@ -216,6 +217,7 @@ void Preferences::browseDisk(const std::vector & disks, const size_ QFileDialog diskFileDialog(this); diskFileDialog.setFileMode(QFileDialog::AnyFile); + // because index = 0 is None if (disks[id]->currentIndex() >= 1) { diskFileDialog.selectFile(disks[id]->currentText()); diff --git a/source/frontends/qapple/qapple.cpp b/source/frontends/qapple/qapple.cpp index af42bb2d..736e0250 100644 --- a/source/frontends/qapple/qapple.cpp +++ b/source/frontends/qapple/qapple.cpp @@ -12,16 +12,13 @@ #include "ParallelPrinter.h" #include "Video.h" #include "SaveState.h" -#include "Registry.h" #include "linux/data.h" #include "linux/benchmark.h" -#include "linux/paddle.h" #include "emulator.h" #include "memorycontainer.h" -#include "gamepadpaddle.h" -#include "settings.h" +#include "configuration.h" #include #include @@ -71,80 +68,6 @@ namespace DiskDestroy(); } - void insertDisk(const QString & filename, const int disk) - { - if (filename.isEmpty()) - { - DiskEject(disk); - } - else - { - const bool createMissingDisk = true; - const ImageError_e result = DiskInsert(disk, filename.toStdString().c_str(), IMAGE_USE_FILES_WRITE_PROTECT_STATUS, createMissingDisk); - if (result != eIMAGE_ERROR_NONE) - { - const QString message = QString("Error [%1] inserting '%2'").arg(QString::number(result), filename); - QMessageBox::warning(NULL, "Disk error", message); - } - } - } - - void insertHD(const QString & filename, const int disk) - { - if (filename.isEmpty()) - { - HD_Unplug(disk); - } - else - { - if (!HD_Insert(disk, filename.toStdString().c_str())) - { - const QString message = QString("Error inserting '%1'").arg(filename); - QMessageBox::warning(NULL, "Hard Disk error", message); - } - } - } - - void setSlot4(const SS_CARDTYPE newCardType) - { - g_Slot4 = newCardType; - REGSAVE(TEXT(REGVALUE_SLOT4), (DWORD)g_Slot4); - } - - void setSlot5(const SS_CARDTYPE newCardType) - { - g_Slot5 = newCardType; - REGSAVE(TEXT(REGVALUE_SLOT5), (DWORD)g_Slot5); - } - - const std::vector computerTypes = {A2TYPE_APPLE2, A2TYPE_APPLE2PLUS, A2TYPE_APPLE2E, A2TYPE_APPLE2EENHANCED, - A2TYPE_PRAVETS82, A2TYPE_PRAVETS8M, A2TYPE_PRAVETS8A, A2TYPE_TK30002E}; - - int getApple2ComputerType() - { - const eApple2Type type = GetApple2Type(); - const auto it = std::find(computerTypes.begin(), computerTypes.end(), type); - if (it != computerTypes.end()) - { - return std::distance(computerTypes.begin(), it); - } - else - { - // default to A2E - return 2; - } - } - - QString getScreenshotTemplate() - { - const QString filenameTemplate = QSettings().value("QApple/Screenshot Template", "/tmp/qapple_%1.png").toString(); - return filenameTemplate; - } - - void setScreenshotTemplate(const QString & filenameTemplate) - { - QSettings().setValue("QApple/Screenshot Template", filenameTemplate); - } } void FrameDrawDiskLEDS(HDC) @@ -334,53 +257,7 @@ void QApple::on_actionMemory_triggered() void QApple::on_actionOptions_triggered() { - Preferences::Data currentOptions; - - const std::vector diskIDs = {DRIVE_1, DRIVE_2}; - currentOptions.disks.resize(diskIDs.size()); - for (size_t i = 0; i < diskIDs.size(); ++i) - { - const char * diskName = DiskGetFullName(diskIDs[i]); - if (diskName) - { - currentOptions.disks[i] = diskName; - } - } - - const std::vector hdIDs = {HARDDISK_1, HARDDISK_2}; - currentOptions.hds.resize(hdIDs.size()); - for (size_t i = 0; i < hdIDs.size(); ++i) - { - const char * diskName = HD_GetFullName(hdIDs[i]); - if (diskName) - { - currentOptions.hds[i] = diskName; - } - } - - currentOptions.mouseInSlot4 = g_Slot4 == CT_MouseInterface; - currentOptions.cpmInSlot5 = g_Slot5 == CT_Z80; - currentOptions.hdInSlot7 = HD_CardIsEnabled(); - - currentOptions.apple2Type = getApple2ComputerType(); - - if (myGamepad) - { - currentOptions.joystick = myGamepad->name(); - currentOptions.joystickId = myGamepad->deviceId(); - } - else - { - currentOptions.joystickId = 0; - } - - const char* saveState = Snapshot_GetFilename(); - if (saveState) - { - currentOptions.saveState = QString::fromUtf8(saveState);; - } - - currentOptions.screenshotTemplate = getScreenshotTemplate(); + const Preferences::Data currentOptions = getCurrentOptions(myGamepad); QSettings settings; // the function will "modify" it myPreferences.setup(currentOptions, settings); @@ -388,76 +265,8 @@ void QApple::on_actionOptions_triggered() if (myPreferences.exec()) { const Preferences::Data newOptions = myPreferences.getData(); - - if (currentOptions.apple2Type != newOptions.apple2Type) - { - const eApple2Type type = computerTypes[newOptions.apple2Type]; - SetApple2Type(type); - REGSAVE(TEXT(REGVALUE_APPLE2_TYPE), type); - const eCpuType cpu = ProbeMainCpuDefault(type); - SetMainCpu(cpu); - REGSAVE(TEXT(REGVALUE_CPU_TYPE), cpu); - } - - if (currentOptions.mouseInSlot4 != newOptions.mouseInSlot4) - { - const SS_CARDTYPE card = newOptions.mouseInSlot4 ? CT_MouseInterface : CT_Empty; - setSlot4(card); - } - if (currentOptions.cpmInSlot5 != newOptions.cpmInSlot5) - { - const SS_CARDTYPE card = newOptions.cpmInSlot5 ? CT_Z80 : CT_Empty; - setSlot5(card); - } - if (currentOptions.hdInSlot7 != newOptions.hdInSlot7) - { - REGSAVE(TEXT(REGVALUE_HDD_ENABLED), newOptions.hdInSlot7 ? 1 : 0); - HD_SetEnabled(newOptions.hdInSlot7); - } - - if (newOptions.joystick.isEmpty()) - { - myGamepad.reset(); - Paddle::instance() = std::make_shared(); - } - else - { - if (newOptions.joystickId != currentOptions.joystickId) - { - myGamepad.reset(new QGamepad(newOptions.joystickId)); - Paddle::instance() = std::make_shared(myGamepad); - } - } - - for (size_t i = 0; i < diskIDs.size(); ++i) - { - if (currentOptions.disks[i] != newOptions.disks[i]) - { - insertDisk(newOptions.disks[i], diskIDs[i]); - } - } - - for (size_t i = 0; i < hdIDs.size(); ++i) - { - if (currentOptions.hds[i] != newOptions.hds[i]) - { - insertHD(newOptions.hds[i], hdIDs[i]); - } - } - - if (currentOptions.saveState != newOptions.saveState) - { - const std::string name = newOptions.saveState.toStdString(); - Snapshot_SetFilename(name); - RegSaveString(TEXT(REG_CONFIG), REGVALUE_SAVESTATE_FILENAME, 1, name.c_str()); - } - - if (currentOptions.screenshotTemplate != newOptions.screenshotTemplate) - { - setScreenshotTemplate(newOptions.screenshotTemplate); - } + setNewOptions(currentOptions, newOptions, myGamepad); } - } void QApple::on_actionSave_state_triggered() diff --git a/source/frontends/qapple/qapple.pro b/source/frontends/qapple/qapple.pro index 0613777c..e29a373d 100644 --- a/source/frontends/qapple/qapple.pro +++ b/source/frontends/qapple/qapple.pro @@ -24,7 +24,8 @@ SOURCES += main.cpp\ memorycontainer.cpp \ preferences.cpp \ gamepadpaddle.cpp \ - settings.cpp + settings.cpp \ + configuration.cpp HEADERS += qapple.h \ emulator.h \ @@ -36,7 +37,8 @@ HEADERS += qapple.h \ memorycontainer.h \ preferences.h \ gamepadpaddle.h \ - settings.h + settings.h \ + configuration.h FORMS += qapple.ui \ emulator.ui \