diff options
author | Jesse Talavera-Greenberg <jesse@jesse.tg> | 2023-11-28 17:16:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-28 23:16:41 +0100 |
commit | e973236203292637eb7bd009a4cfbd6fd785181f (patch) | |
tree | 4c348a9927bfa6f8f37cc943291174a1096434b3 /src/frontend/qt_sdl/PowerManagement | |
parent | c84cb174628c5a2e8e6cc0179e16de3eab47864a (diff) |
Refactor `NDS` and `DSi` to be objects (#1893)
* First crack at refactoring NDS and DSi into objects
- Remove all global/`static` variables in `NDS` and related classes
- Rely more on virtual dispatch when we need to pick methods at runtime
- Pass `NDS&` or `DSi&` to its constituent components where necessary
- Introduce some headers or move some definitions to break `#include` cycles
* Refactor the frontend to accommodate the core's changes
* Move up `SchedList`'s declaration
- Move it to before the components are initialized so the `map`s inside are initialized
- Fields in C++ are initialized in the order they're declared
* Fix a crash when allocating memory
* Fix JIT-free builds
* Fix GDB-free builds
* Fix Linux builds
- Explicitly qualify some member types in NDS, since they share the same name as their classes
* Remove an unnecessary template argument
- This was causing the build to fail on macOS
* Fix ARM and Android builds
* Rename `Constants.h` to `MemConstants.h`
* Add `NDS::IsRunning()`
* Use an `#include` guard instead of `#pragma once`
Diffstat (limited to 'src/frontend/qt_sdl/PowerManagement')
-rw-r--r-- | src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.cpp | 83 | ||||
-rw-r--r-- | src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.h | 8 |
2 files changed, 59 insertions, 32 deletions
diff --git a/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.cpp b/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.cpp index 14ccd51..3d47c45 100644 --- a/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.cpp +++ b/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.cpp @@ -29,45 +29,59 @@ #include "types.h" #include <QtDebug> +#include "main.h" using namespace melonDS; PowerManagementDialog* PowerManagementDialog::currentDlg = nullptr; -PowerManagementDialog::PowerManagementDialog(QWidget* parent) : QDialog(parent), ui(new Ui::PowerManagementDialog) +PowerManagementDialog::PowerManagementDialog(QWidget* parent, EmuThread* emuThread) : QDialog(parent), emuThread(emuThread), ui(new Ui::PowerManagementDialog) { inited = false; ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); - if (NDS::ConsoleType == 1) + if (emuThread->NDS->ConsoleType == 1) { ui->grpDSBattery->setEnabled(false); - oldDSiBatteryLevel = DSi::I2C->GetBPTWL()->GetBatteryLevel(); - oldDSiBatteryCharging = DSi::I2C->GetBPTWL()->GetBatteryCharging(); + auto& dsi = static_cast<DSi&>(*emuThread->NDS); + oldDSiBatteryLevel = dsi.I2C.GetBPTWL()->GetBatteryLevel(); + oldDSiBatteryCharging = dsi.I2C.GetBPTWL()->GetBatteryCharging(); } else { ui->grpDSiBattery->setEnabled(false); - oldDSBatteryLevel = NDS::SPI->GetPowerMan()->GetBatteryLevelOkay(); + oldDSBatteryLevel = emuThread->NDS->SPI.GetPowerMan()->GetBatteryLevelOkay(); } updateDSBatteryLevelControls(); - ui->cbDSiBatteryCharging->setChecked(DSi::I2C->GetBPTWL()->GetBatteryCharging()); - int dsiBatterySliderPos; - switch (DSi::I2C->GetBPTWL()->GetBatteryLevel()) + bool defaultDSiBatteryCharging = (emuThread->NDS->ConsoleType == 1) ? Config::DSiBatteryCharging : false; + + if (emuThread->NDS->ConsoleType == 1) { + auto& dsi = static_cast<DSi&>(*emuThread->NDS); + ui->cbDSiBatteryCharging->setChecked(dsi.I2C.GetBPTWL()->GetBatteryCharging()); + int dsiBatterySliderPos = 4; + switch (dsi.I2C.GetBPTWL()->GetBatteryLevel()) + { case DSi_BPTWL::batteryLevel_AlmostEmpty: dsiBatterySliderPos = 0; break; case DSi_BPTWL::batteryLevel_Low: dsiBatterySliderPos = 1; break; case DSi_BPTWL::batteryLevel_Half: dsiBatterySliderPos = 2; break; case DSi_BPTWL::batteryLevel_ThreeQuarters: dsiBatterySliderPos = 3; break; case DSi_BPTWL::batteryLevel_Full: dsiBatterySliderPos = 4; break; + } + ui->sliderDSiBatteryLevel->setValue(dsiBatterySliderPos); + } + else + { + ui->cbDSiBatteryCharging->setChecked(Config::DSiBatteryCharging); + ui->sliderDSiBatteryLevel->setValue(Config::DSiBatteryLevel); } - ui->sliderDSiBatteryLevel->setValue(dsiBatterySliderPos); + int inst = Platform::InstanceID(); if (inst > 0) @@ -87,26 +101,28 @@ void PowerManagementDialog::done(int r) { if (r == QDialog::Accepted) { - if (NDS::ConsoleType == 1) + if (emuThread->NDS->ConsoleType == 1) { - Config::DSiBatteryLevel = DSi::I2C->GetBPTWL()->GetBatteryLevel(); - Config::DSiBatteryCharging = DSi::I2C->GetBPTWL()->GetBatteryCharging(); + auto& dsi = static_cast<DSi&>(*emuThread->NDS); + Config::DSiBatteryLevel = dsi.I2C.GetBPTWL()->GetBatteryLevel(); + Config::DSiBatteryCharging = dsi.I2C.GetBPTWL()->GetBatteryCharging(); } else { - Config::DSBatteryLevelOkay = NDS::SPI->GetPowerMan()->GetBatteryLevelOkay(); + Config::DSBatteryLevelOkay = emuThread->NDS->SPI.GetPowerMan()->GetBatteryLevelOkay(); } } else { - if (NDS::ConsoleType == 1) + if (emuThread->NDS->ConsoleType == 1) { - DSi::I2C->GetBPTWL()->SetBatteryLevel(oldDSiBatteryLevel); - DSi::I2C->GetBPTWL()->SetBatteryCharging(oldDSiBatteryCharging); + auto& dsi = static_cast<DSi&>(*emuThread->NDS); + dsi.I2C.GetBPTWL()->SetBatteryLevel(oldDSiBatteryLevel); + dsi.I2C.GetBPTWL()->SetBatteryCharging(oldDSiBatteryCharging); } else { - NDS::SPI->GetPowerMan()->SetBatteryLevelOkay(oldDSBatteryLevel); + emuThread->NDS->SPI.GetPowerMan()->SetBatteryLevelOkay(oldDSBatteryLevel); } } @@ -117,17 +133,17 @@ void PowerManagementDialog::done(int r) void PowerManagementDialog::on_rbDSBatteryLow_clicked() { - NDS::SPI->GetPowerMan()->SetBatteryLevelOkay(false); + emuThread->NDS->SPI.GetPowerMan()->SetBatteryLevelOkay(false); } void PowerManagementDialog::on_rbDSBatteryOkay_clicked() { - NDS::SPI->GetPowerMan()->SetBatteryLevelOkay(true); + emuThread->NDS->SPI.GetPowerMan()->SetBatteryLevelOkay(true); } void PowerManagementDialog::updateDSBatteryLevelControls() { - if (NDS::SPI->GetPowerMan()->GetBatteryLevelOkay()) + if (emuThread->NDS->SPI.GetPowerMan()->GetBatteryLevelOkay()) ui->rbDSBatteryOkay->setChecked(true); else ui->rbDSBatteryLow->setChecked(true); @@ -135,23 +151,32 @@ void PowerManagementDialog::updateDSBatteryLevelControls() void PowerManagementDialog::on_cbDSiBatteryCharging_toggled() { - DSi::I2C->GetBPTWL()->SetBatteryCharging(ui->cbDSiBatteryCharging->isChecked()); + if (emuThread->NDS->ConsoleType == 1) + { + auto& dsi = static_cast<DSi&>(*emuThread->NDS); + dsi.I2C.GetBPTWL()->SetBatteryCharging(ui->cbDSiBatteryCharging->isChecked()); + } } void PowerManagementDialog::on_sliderDSiBatteryLevel_valueChanged(int value) { if (!inited) return; - u8 newBatteryLevel; - switch (value) + if (emuThread->NDS->ConsoleType == 1) { - case 0: newBatteryLevel = DSi::I2C->GetBPTWL()->batteryLevel_AlmostEmpty; break; - case 1: newBatteryLevel = DSi::I2C->GetBPTWL()->batteryLevel_Low; break; - case 2: newBatteryLevel = DSi::I2C->GetBPTWL()->batteryLevel_Half; break; - case 3: newBatteryLevel = DSi::I2C->GetBPTWL()->batteryLevel_ThreeQuarters; break; - case 4: newBatteryLevel = DSi::I2C->GetBPTWL()->batteryLevel_Full; break; + auto& dsi = static_cast<DSi&>(*emuThread->NDS); + u8 newBatteryLevel = DSi_BPTWL::batteryLevel_Full; + switch (value) + { + case 0: newBatteryLevel = DSi_BPTWL::batteryLevel_AlmostEmpty; break; + case 1: newBatteryLevel = DSi_BPTWL::batteryLevel_Low; break; + case 2: newBatteryLevel = DSi_BPTWL::batteryLevel_Half; break; + case 3: newBatteryLevel = DSi_BPTWL::batteryLevel_ThreeQuarters; break; + case 4: newBatteryLevel = DSi_BPTWL::batteryLevel_Full; break; + } + dsi.I2C.GetBPTWL()->SetBatteryLevel(newBatteryLevel); } - DSi::I2C->GetBPTWL()->SetBatteryLevel(newBatteryLevel); + updateDSBatteryLevelControls(); } diff --git a/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.h b/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.h index cd2954a..bc2abc3 100644 --- a/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.h +++ b/src/frontend/qt_sdl/PowerManagement/PowerManagementDialog.h @@ -25,6 +25,7 @@ #include "types.h" namespace Ui { class PowerManagementDialog; } +class EmuThread; class PowerManagementDialog; class PowerManagementDialog : public QDialog @@ -32,11 +33,11 @@ class PowerManagementDialog : public QDialog Q_OBJECT public: - explicit PowerManagementDialog(QWidget* parent); + explicit PowerManagementDialog(QWidget* parent, EmuThread* emu_thread); ~PowerManagementDialog(); static PowerManagementDialog* currentDlg; - static PowerManagementDialog* openDlg(QWidget* parent) + static PowerManagementDialog* openDlg(QWidget* parent, EmuThread* emu_thread) { if (currentDlg) { @@ -44,7 +45,7 @@ public: return currentDlg; } - currentDlg = new PowerManagementDialog(parent); + currentDlg = new PowerManagementDialog(parent, emu_thread); currentDlg->open(); return currentDlg; } @@ -64,6 +65,7 @@ private slots: private: Ui::PowerManagementDialog* ui; + EmuThread* emuThread; bool inited; bool oldDSBatteryLevel; |