diff --git a/src/Platform.h b/src/Platform.h index a379d853..144fce19 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -337,6 +337,9 @@ void WriteGBASave(const u8* savedata, u32 savelen, u32 writeoffset, u32 writelen /// @param writelen The number of bytes that were written to firmware. void WriteFirmware(const SPI_Firmware::Firmware& firmware, u32 writeoffset, u32 writelen); +// called when the RTC date/time is changed and the frontend might need to take it into account +void WriteDateTime(int year, int month, int day, int hour, int minute, int second); + // local multiplayer comm interface // packet type: DS-style TX header (12 bytes) + original 802.11 frame diff --git a/src/RTC.cpp b/src/RTC.cpp index 8d3fb401..3272275d 100644 --- a/src/RTC.cpp +++ b/src/RTC.cpp @@ -455,6 +455,13 @@ void WriteDateTime(int num, u8 val) } } +void SaveDateTime() +{ + int y, m, d, h, i, s; + GetDateTime(y, m, d, h, i, s); + Platform::WriteDateTime(y, m, d, h, i, s); +} + void CmdRead() { if ((CurCmd & 0x0F) == 0x06) @@ -607,11 +614,15 @@ void CmdWrite(u8 val) case 0x20: if (InputPos <= 7) WriteDateTime(InputPos, val); + if (InputPos == 7) + SaveDateTime(); break; case 0x60: if (InputPos <= 3) WriteDateTime(InputPos+4, val); + if (InputPos == 3) + SaveDateTime(); break; case 0x10: diff --git a/src/frontend/qt_sdl/Config.cpp b/src/frontend/qt_sdl/Config.cpp index ba91068d..cfd4b1c0 100644 --- a/src/frontend/qt_sdl/Config.cpp +++ b/src/frontend/qt_sdl/Config.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "Platform.h" #include "Config.h" @@ -140,10 +141,7 @@ int MouseHideSeconds; bool PauseLostFocus; -int RTCMode; -std::string RTCLastTime; -std::string RTCLastHostTime; -std::string RTCNewTime; +int64_t RTCOffset; bool DSBatteryLevelOkay; int DSiBatteryLevel; @@ -344,10 +342,7 @@ ConfigEntry ConfigFile[] = {"MouseHideSeconds", 0, &MouseHideSeconds, 5, false}, {"PauseLostFocus", 1, &PauseLostFocus, false, false}, - {"RTCMode", 0, &RTCMode, 0, true}, - {"RTCLastTime", 2, &RTCLastTime, (std::string)"", true}, - {"RTCLastHostTime", 2, &RTCLastHostTime, (std::string)"", true}, - {"RTCNewTime", 2, &RTCNewTime, (std::string)"", true}, + {"RTCOffset", 3, &RTCOffset, 0LL, true}, {"DSBatteryLevelOkay", 1, &DSBatteryLevelOkay, true, true}, {"DSiBatteryLevel", 0, &DSiBatteryLevel, 0xF, true}, @@ -416,6 +411,7 @@ void LoadFile(int inst) case 0: *(int*)entry->Value = strtol(entryval, NULL, 10); break; case 1: *(bool*)entry->Value = strtol(entryval, NULL, 10) ? true:false; break; case 2: *(std::string*)entry->Value = entryval; break; + case 3: *(int64_t*)entry->Value = strtoll(entryval, NULL, 10); break; } break; @@ -436,6 +432,7 @@ void Load() case 0: *(int*)entry->Value = std::get(entry->Default); break; case 1: *(bool*)entry->Value = std::get(entry->Default); break; case 2: *(std::string*)entry->Value = std::get(entry->Default); break; + case 3: *(int64_t*)entry->Value = std::get(entry->Default); break; } } @@ -472,6 +469,7 @@ void Save() case 0: Platform::FileWriteFormatted(f, "%s=%d\r\n", entry->Name, *(int*)entry->Value); break; case 1: Platform::FileWriteFormatted(f, "%s=%d\r\n", entry->Name, *(bool*)entry->Value ? 1:0); break; case 2: Platform::FileWriteFormatted(f, "%s=%s\r\n", entry->Name, (*(std::string*)entry->Value).c_str()); break; + case 3: Platform::FileWriteFormatted(f, "%s=%"PRId64"\r\n", entry->Name, *(int64_t*)entry->Value); break; } } diff --git a/src/frontend/qt_sdl/Config.h b/src/frontend/qt_sdl/Config.h index daa69c8b..fba9bfb0 100644 --- a/src/frontend/qt_sdl/Config.h +++ b/src/frontend/qt_sdl/Config.h @@ -57,9 +57,9 @@ namespace Config struct ConfigEntry { char Name[32]; - int Type; // 0=int 1=bool 2=string + int Type; // 0=int 1=bool 2=string 3=64bit int void* Value; // pointer to the value variable - std::variant Default; + std::variant Default; bool InstanceUnique; // whether the setting can exist individually for each instance in multiplayer }; @@ -185,10 +185,7 @@ extern bool MouseHide; extern int MouseHideSeconds; extern bool PauseLostFocus; -extern int RTCMode; -extern std::string RTCLastTime; -extern std::string RTCLastHostTime; -extern std::string RTCNewTime; +extern int64_t RTCOffset; extern bool DSBatteryLevelOkay; extern int DSiBatteryLevel; diff --git a/src/frontend/qt_sdl/DateTimeDialog.cpp b/src/frontend/qt_sdl/DateTimeDialog.cpp index 49ef5545..2ef96881 100644 --- a/src/frontend/qt_sdl/DateTimeDialog.cpp +++ b/src/frontend/qt_sdl/DateTimeDialog.cpp @@ -32,46 +32,16 @@ DateTimeDialog::DateTimeDialog(QWidget* parent) : QDialog(parent), ui(new Ui::Da ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); - grpTimeMode = new QButtonGroup(this); - grpTimeMode->addButton(ui->rbSystemTime, 0); - grpTimeMode->addButton(ui->rbCustomTime, 1); - connect(grpTimeMode, SIGNAL(buttonClicked(int)), this, SLOT(onChangeTimeMode(int))); - grpTimeMode->button(Config::RTCMode)->setChecked(true); - QDateTime now = QDateTime::currentDateTime(); - customTime = now; + customTime = now.addSecs(Config::RTCOffset); ui->chkChangeTime->setChecked(false); - - if (Config::RTCNewTime != "") - { - QDateTime newtime = QDateTime::fromString(QString::fromStdString(Config::RTCNewTime), Qt::ISODate); - - if (newtime.isValid()) - { - ui->chkChangeTime->setChecked(true); - ui->txtNewCustomTime->setDateTime(newtime); - } - } - - if (Config::RTCLastTime != "" && Config::RTCLastHostTime != "") - { - QDateTime lasttime = QDateTime::fromString(QString::fromStdString(Config::RTCLastTime), Qt::ISODate); - QDateTime lasthost = QDateTime::fromString(QString::fromStdString(Config::RTCLastHostTime), Qt::ISODate); - - if (lasttime.isValid() && lasthost.isValid()) - { - qint64 offset = lasthost.secsTo(now); - customTime = lasttime.addSecs(offset); - } - } + ui->chkResetTime->setChecked(false); ui->lblCustomTime->setText(customTime.toString(ui->txtNewCustomTime->displayFormat())); startTimer(1000); - bool iscustom = (Config::RTCMode == 1); - ui->chkChangeTime->setEnabled(iscustom); - ui->txtNewCustomTime->setEnabled(iscustom && ui->chkChangeTime->isChecked()); + ui->txtNewCustomTime->setEnabled(ui->chkChangeTime->isChecked()); } DateTimeDialog::~DateTimeDialog() @@ -89,12 +59,13 @@ void DateTimeDialog::done(int r) { if (r == QDialog::Accepted) { - Config::RTCMode = grpTimeMode->checkedId(); - if (ui->chkChangeTime->isChecked()) - Config::RTCNewTime = ui->txtNewCustomTime->dateTime().toString(Qt::ISODate).toStdString(); - else - Config::RTCNewTime = ""; + { + QDateTime now = QDateTime::currentDateTime(); + Config::RTCOffset = now.secsTo(ui->txtNewCustomTime->dateTime()); + } + else if (ui->chkResetTime->isChecked()) + Config::RTCOffset = 0; Config::Save(); } @@ -106,21 +77,15 @@ void DateTimeDialog::done(int r) void DateTimeDialog::on_chkChangeTime_clicked(bool checked) { - bool iscustom = (grpTimeMode->checkedId() == 1); - - ui->txtNewCustomTime->setEnabled(iscustom && checked); + if (checked) ui->chkResetTime->setChecked(false); + ui->txtNewCustomTime->setEnabled(checked); } -void DateTimeDialog::onChangeTimeMode(int mode) +void DateTimeDialog::on_chkResetTime_clicked(bool checked) { - bool iscustom = (mode == 1); - - ui->chkChangeTime->setEnabled(iscustom); - ui->txtNewCustomTime->setEnabled(iscustom && ui->chkChangeTime->isChecked()); + if (checked) + { + ui->chkChangeTime->setChecked(false); + ui->txtNewCustomTime->setEnabled(false); + } } - -void setCustomTimeLabel() -{ - // -} - diff --git a/src/frontend/qt_sdl/DateTimeDialog.h b/src/frontend/qt_sdl/DateTimeDialog.h index 3e48f380..f13dbd09 100644 --- a/src/frontend/qt_sdl/DateTimeDialog.h +++ b/src/frontend/qt_sdl/DateTimeDialog.h @@ -59,13 +59,11 @@ private slots: void done(int r); void on_chkChangeTime_clicked(bool checked); - void onChangeTimeMode(int mode); + void on_chkResetTime_clicked(bool checked); private: Ui::DateTimeDialog* ui; - QButtonGroup* grpTimeMode; - QDateTime customTime; }; diff --git a/src/frontend/qt_sdl/DateTimeDialog.ui b/src/frontend/qt_sdl/DateTimeDialog.ui index f4b6eb8b..59eb677e 100644 --- a/src/frontend/qt_sdl/DateTimeDialog.ui +++ b/src/frontend/qt_sdl/DateTimeDialog.ui @@ -7,40 +7,17 @@ 0 0 357 - 259 + 161 Date and time - melonDS - - - - General settings - - - - - - Use custom date and time - - - - - - - Always use system date and time - - - - - - - Custom date and time + Date and time @@ -64,28 +41,14 @@ - + - Change + Change to: - - - - New value: - - - - - - - The new value will be applied on the next boot. - - - - + @@ -125,6 +88,13 @@ + + + + Reset to system date and time + + + diff --git a/src/frontend/qt_sdl/Platform.cpp b/src/frontend/qt_sdl/Platform.cpp index 0cb95740..252594c7 100644 --- a/src/frontend/qt_sdl/Platform.cpp +++ b/src/frontend/qt_sdl/Platform.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -610,6 +611,15 @@ void WriteFirmware(const SPI_Firmware::Firmware& firmware, u32 writeoffset, u32 } +void WriteDateTime(int year, int month, int day, int hour, int minute, int second) +{ + QDateTime hosttime = QDateTime::currentDateTime(); + QDateTime time = QDateTime(QDate(year, month, day), QTime(hour, minute, second)); + + Config::RTCOffset = hosttime.secsTo(time); + Config::Save(); +} + bool MP_Init() { return LocalMP::Init(); diff --git a/src/frontend/qt_sdl/ROMManager.cpp b/src/frontend/qt_sdl/ROMManager.cpp index e7c55504..648ab676 100644 --- a/src/frontend/qt_sdl/ROMManager.cpp +++ b/src/frontend/qt_sdl/ROMManager.cpp @@ -595,49 +595,10 @@ void SetBatteryLevels() void SetDateTime() { QDateTime hosttime = QDateTime::currentDateTime(); - QDateTime time; - - if (Config::RTCMode == 0) - { - // use current system time - - time = hosttime; - } - else - { - // use custom time - - if (Config::RTCNewTime != "") - { - // assign a new custom time - - time = QDateTime::fromString(QString::fromStdString(Config::RTCNewTime), Qt::ISODate); - if (!time.isValid()) - return; - } - else if (Config::RTCLastTime != "" && Config::RTCLastHostTime != "") - { - // deduce the custom time based on the last saved times - - QDateTime lasttime = QDateTime::fromString(QString::fromStdString(Config::RTCLastTime), Qt::ISODate); - QDateTime lasthost = QDateTime::fromString(QString::fromStdString(Config::RTCLastHostTime), Qt::ISODate); - - if (lasttime.isValid() && lasthost.isValid()) - { - qint64 offset = lasthost.secsTo(hosttime); - time = lasttime.addSecs(offset); - } - else - return; - } - } + QDateTime time = hosttime.addSecs(Config::RTCOffset); RTC::SetDateTime(time.date().year(), time.date().month(), time.date().day(), time.time().hour(), time.time().minute(), time.time().second()); - - Config::RTCLastTime = time.toString(Qt::ISODate).toStdString(); - Config::RTCLastHostTime = hosttime.toString(Qt::ISODate).toStdString(); - Config::RTCNewTime = ""; } void Reset() diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index 7088f303..f6704779 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -316,7 +316,6 @@ void EmuThread::run() { u32 mainScreenPos[3]; Platform::FileHandle* file; - bool hasrun = false; NDS::Init(); @@ -441,8 +440,6 @@ void EmuThread::run() if (EmuRunning == emuStatus_Running || EmuRunning == emuStatus_FrameStep) { - hasrun = true; - EmuStatus = emuStatus_Running; if (EmuRunning == emuStatus_FrameStep) EmuRunning = emuStatus_Paused; @@ -673,17 +670,6 @@ void EmuThread::run() Platform::FileWrite(&state, sizeof(state), 1, file); Platform::CloseFile(file); } - if (hasrun) - { - int y, m, d, h, i, s; - RTC::GetDateTime(y, m, d, h, i, s); - - QDateTime hosttime = QDateTime::currentDateTime(); - QDateTime time = QDateTime(QDate(y, m, d), QTime(h, i, s)); - - Config::RTCLastTime = time.toString(Qt::ISODate).toStdString(); - Config::RTCLastHostTime = hosttime.toString(Qt::ISODate).toStdString(); - } EmuStatus = emuStatus_Exit; @@ -3339,7 +3325,6 @@ int main(int argc, char** argv) SANITIZE(Config::ScreenSizing, 0, (int)Frontend::screenSizing_MAX); SANITIZE(Config::ScreenAspectTop, 0, AspectRatiosNum); SANITIZE(Config::ScreenAspectBot, 0, AspectRatiosNum); - SANITIZE(Config::RTCMode, 0, 1); #undef SANITIZE AudioInOut::Init();