Skip to content

Commit

Permalink
refactor global and add 10-min change
Browse files Browse the repository at this point in the history
  • Loading branch information
3096 committed Aug 12, 2024
1 parent abd29de commit e624ff9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
25 changes: 25 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurations": [
{
"name": "DevkitPro Aarch64 Linux",
"includePath": [
"/opt/devkitpro/devkitA64/aarch64-none-elf/include",
"/opt/devkitpro/devkitA64/lib/gcc/aarch64-none-elf/*/include",
"/opt/devkitpro/devkitA64/lib/gcc/aarch64-none-elf/*/include-fixed",
"/opt/devkitpro/libnx/include"
],
"defines": [
"SWITCH",
"__SWITCH__",
"__aarch64__",
"ATMOSPHER_OS_HORIZON"
],
"compilerPath": "/opt/devkitpro/devkitA64/bin/aarch64-none-elf-gcc",
"cStandard": "c11",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-arm64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"files.associations": {
"ntp.h": "c",
"switch.h": "c",
"counter.h": "c",
"nro.h": "c",
"fatal.h": "c",
"time.h": "c",
"usbhs.h": "c",
"hidbus.h": "c",
"hid.h": "c",
"usbds.h": "c"
}
}
42 changes: 23 additions & 19 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ For more information, please refer to <https://unlicense.org> */

#include "ntp.h"

PadState pad;

bool setsysInternetTimeSyncIsOn() {
Result rs = setsysInitialize();
if (R_FAILED(rs)) {
Expand Down Expand Up @@ -86,12 +84,12 @@ bool setNetworkSystemClock(time_t time) {
return true;
}

int consoleExitWithMsg(char* msg) {
int consoleExitWithMsg(char* msg, PadState* pad) {
printf("%s\n\nPress + to quit...", msg);

while (appletMainLoop()) {
padUpdate(&pad);
u64 kDown = padGetButtonsDown(&pad);
padUpdate(pad);
u64 kDown = padGetButtonsDown(pad);

if (kDown & HidNpadButton_Plus) {
consoleExit(NULL);
Expand All @@ -104,7 +102,7 @@ int consoleExitWithMsg(char* msg) {
return 0;
}

bool toggleHBMenuPath(char* curPath) {
bool toggleHBMenuPath(char* curPath, PadState* pad) {
const char* HB_MENU_NRO_PATH = "sdmc:/hbmenu.nro";
const char* HB_MENU_BAK_PATH = "sdmc:/hbmenu.nro.bak";
const char* DEFAULT_RESTORE_PATH = "sdmc:/switch/switch-time.nro";
Expand All @@ -117,35 +115,35 @@ bool toggleHBMenuPath(char* curPath) {
rs = access(HB_MENU_BAK_PATH, F_OK);
if (R_FAILED(rs)) {
printf("could not find %s to restore. failed: 0x%x", HB_MENU_BAK_PATH, rs);
consoleExitWithMsg("");
consoleExitWithMsg("", pad);
return false;
}

rs = rename(curPath, DEFAULT_RESTORE_PATH);
if (R_FAILED(rs)) {
printf("fsFsRenameFile(%s, %s) failed: 0x%x", curPath, DEFAULT_RESTORE_PATH, rs);
consoleExitWithMsg("");
consoleExitWithMsg("", pad);
return false;
}
rs = rename(HB_MENU_BAK_PATH, HB_MENU_NRO_PATH);
if (R_FAILED(rs)) {
printf("fsFsRenameFile(%s, %s) failed: 0x%x", HB_MENU_BAK_PATH, HB_MENU_NRO_PATH, rs);
consoleExitWithMsg("");
consoleExitWithMsg("", pad);
return false;
}
} else {
// replace hbmenu
rs = rename(HB_MENU_NRO_PATH, HB_MENU_BAK_PATH);
if (R_FAILED(rs)) {
printf("fsFsRenameFile(%s, %s) failed: 0x%x", HB_MENU_NRO_PATH, HB_MENU_BAK_PATH, rs);
consoleExitWithMsg("");
consoleExitWithMsg("", pad);
return false;
}
rs = rename(curPath, HB_MENU_NRO_PATH);
if (R_FAILED(rs)) {
printf("fsFsRenameFile(%s, %s) failed: 0x%x", curPath, HB_MENU_NRO_PATH, rs);
rename(HB_MENU_BAK_PATH, HB_MENU_NRO_PATH); // hbmenu already moved, try to move it back
consoleExitWithMsg("");
consoleExitWithMsg("", pad);
return false;
}
}
Expand All @@ -157,30 +155,32 @@ bool toggleHBMenuPath(char* curPath) {

int main(int argc, char* argv[]) {
consoleInit(NULL);
printf("SwitchTime v0.1.3\n\n");

padConfigureInput(8, HidNpadStyleSet_NpadStandard);
PadState pad;
padInitializeAny(&pad);
printf("SwitchTime v0.1.3\n\n");

if (!setsysInternetTimeSyncIsOn()) {
// printf("Trying setsysSetUserSystemClockAutomaticCorrectionEnabled...\n");
// if (R_FAILED(enableSetsysInternetTimeSync())) {
// return consoleExitWithMsg("Internet time sync is not enabled. Please enable it in System Settings.");
// }
// doesn't work without rebooting? not worth it
return consoleExitWithMsg("Internet time sync is not enabled. Please enable it in System Settings.");
return consoleExitWithMsg("Internet time sync is not enabled. Please enable it in System Settings.", &pad);
}

// Main loop
while (appletMainLoop()) {
printf(
"\n\n"
"Press:\n\n"
"UP/DOWN to change hour | LEFT/RIGHT to change day | R/ZR to change year\n"
"UP/DOWN to change hour | LEFT/RIGHT to change day | X/B to 10 minutes\n"
"L/ZL to change month | R/ZR to change year\n"
"A to confirm time | Y to reset to current time (Cloudflare time server)\n"
" | + to quit\n\n\n");

int dayChange = 0, hourChange = 0, monthChange = 0,yearsChange = 0;
int yearsChange = 0, monthChange = 0, dayChange = 0, hourChange = 0, tenMinChange = 0;
while (appletMainLoop()) {
padUpdate(&pad);
u64 kDown = padGetButtonsDown(&pad);
Expand All @@ -190,7 +190,7 @@ int main(int argc, char* argv[]) {
return 0; // return to hbmenu
}
if (kDown & HidNpadButton_Minus) {
if (!toggleHBMenuPath(argv[0])) {
if (!toggleHBMenuPath(argv[0], &pad)) {
return 0;
}
}
Expand All @@ -199,15 +199,15 @@ int main(int argc, char* argv[]) {
Result rs = timeGetCurrentTime(TimeType_UserSystemClock, (u64*)&currentTime);
if (R_FAILED(rs)) {
printf("timeGetCurrentTime failed with %x", rs);
return consoleExitWithMsg("");
return consoleExitWithMsg("", &pad);
}

struct tm* p_tm_timeToSet = localtime(&currentTime);
p_tm_timeToSet->tm_year += yearsChange;
p_tm_timeToSet->tm_mon += monthChange;
p_tm_timeToSet->tm_mday += dayChange;
p_tm_timeToSet->tm_hour += hourChange;

p_tm_timeToSet->tm_min += tenMinChange * 10;
time_t timeToSet = mktime(p_tm_timeToSet);

if (kDown & HidNpadButton_A) {
Expand Down Expand Up @@ -241,6 +241,10 @@ int main(int argc, char* argv[]) {
monthChange--;
} else if (kDown & HidNpadButton_ZL) {
monthChange++;
} else if (kDown & HidNpadButton_B) {
tenMinChange--;
} else if (kDown & HidNpadButton_X) {
tenMinChange++;
}

char timeToSetStr[25];
Expand All @@ -249,4 +253,4 @@ int main(int argc, char* argv[]) {
consoleUpdate(NULL);
}
}
}
}

0 comments on commit e624ff9

Please sign in to comment.