much simpler design for RTC stuff

This commit is contained in:
Arisotura 2023-10-28 13:06:23 +02:00
parent 40b4692ee0
commit 29f3a9f040
10 changed files with 64 additions and 166 deletions

View File

@ -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

View File

@ -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:

View File

@ -19,6 +19,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#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<int>(entry->Default); break;
case 1: *(bool*)entry->Value = std::get<bool>(entry->Default); break;
case 2: *(std::string*)entry->Value = std::get<std::string>(entry->Default); break;
case 3: *(int64_t*)entry->Value = std::get<int64_t>(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;
}
}

View File

@ -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<int, bool, std::string> Default;
std::variant<int, bool, std::string, int64_t> 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;

View File

@ -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()
{
//
}

View File

@ -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;
};

View File

@ -7,40 +7,17 @@
<x>0</x>
<y>0</y>
<width>357</width>
<height>259</height>
<height>161</height>
</rect>
</property>
<property name="windowTitle">
<string>Date and time - melonDS</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>General settings</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="1" column="0" colspan="2">
<widget class="QRadioButton" name="rbCustomTime">
<property name="text">
<string>Use custom date and time</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QRadioButton" name="rbSystemTime">
<property name="text">
<string>Always use system date and time</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Custom date and time</string>
<string>Date and time</string>
</property>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
@ -64,28 +41,14 @@
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<item row="2" column="0">
<widget class="QCheckBox" name="chkChangeTime">
<property name="text">
<string>Change</string>
<string>Change to:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>New value:</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="label_5">
<property name="text">
<string>The new value will be applied on the next boot.</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QDateTimeEdit" name="txtNewCustomTime">
<property name="dateTime">
<datetime>
@ -125,6 +88,13 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="chkResetTime">
<property name="text">
<string>Reset to system date and time</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>

View File

@ -23,6 +23,7 @@
#include <string>
#include <QStandardPaths>
#include <QString>
#include <QDateTime>
#include <QDir>
#include <QThread>
#include <QSemaphore>
@ -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();

View File

@ -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()

View File

@ -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();