Skip to content

Commit

Permalink
Timezone detection, Y2k36 handling, code refactoring. (#18)
Browse files Browse the repository at this point in the history
* created upstream branch

--HG--
branch : upstream

* backported changes to upstream

--HG--
branch : upstream

* fixed a typo, now the sync option should work properly

--HG--
branch : upstream

* backported changes to upstream

--HG--
branch : upstream

* Backported to upstream.

--HG--
branch : upstream

* Backported to upstream.

--HG--
branch : upstream

---------

Co-authored-by: Daniel K. O. (dkosmari) <none@none>
  • Loading branch information
dkosmari and Daniel K. O. (dkosmari) authored Oct 20, 2023
1 parent 0a998b4 commit 3d5dd85
Show file tree
Hide file tree
Showing 26 changed files with 2,012 additions and 953 deletions.
24 changes: 17 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ BUILD := build
SOURCES := source source/wupsxx
DATA := data
INCLUDES := source
PLUGIN_NAME := "Wii U Time Sync"

# Be verbose by default.
V ?= 1
Expand All @@ -37,21 +38,31 @@ OPTFLAGS := -O2 -fipa-pta -ffunction-sections

CFLAGS := $(WARN_FLAGS) $(OPTFLAGS) $(MACHDEP)

CPPFLAGS := $(INCLUDE) -D__WIIU__ -D__WUT__ -D__WUPS__

CXXFLAGS := $(CFLAGS) -std=c++23

# Note: INCLUDE will be defined later, so CPPFLAGS has to be of the recursive flavor.
CPPFLAGS = $(INCLUDE) \
-D__WIIU__ \
-D__WUT__ \
-D__WUPS__ \
-DPLUGIN_NAME=\"$(PLUGIN_NAME)\"

ASFLAGS := -g $(ARCH)

LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) $(WUPSSPECS)
LDFLAGS = -g \
$(ARCH) \
$(RPXSPECS) \
$(WUPSSPECS) \
-Wl,-Map,$(notdir $*.map) \
$(OPTFLAGS)

LIBS := -lnotifications -lwups -lwut

#-------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level
# containing include and lib
#-------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(WUMS_ROOT) $(WUPS_ROOT) $(WUT_ROOT)
LIBDIRS := $(WUMS_ROOT) $(WUPS_ROOT) $(WUT_ROOT)

#-------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
Expand Down Expand Up @@ -93,12 +104,11 @@ export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))

export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
$(foreach dir,$(LIBDIRS),-I$(dir)/include)

export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

.PHONY: $(BUILD) clean all upload
.PHONY: $(BUILD) clean all

#-------------------------------------------------------------------------------
all: $(BUILD)
Expand Down
64 changes: 64 additions & 0 deletions source/cfg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT

#include "cfg.hpp"

#include "utc.hpp"
#include "wupsxx/storage.hpp"


namespace cfg {

namespace key {
const char* hours = "hours";
const char* minutes = "minutes";
const char* msg_duration = "msg_duration";
const char* notify = "notify";
const char* server = "server";
const char* sync = "sync";
const char* tolerance = "tolerance";
}


int hours = 0;
int minutes = 0;
int msg_duration = 5;
bool notify = true;
std::string server = "pool.ntp.org";
bool sync = false;
int tolerance = 250;


template<typename T>
void
load_or_init(const std::string& key,
T& variable)
{
auto val = wups::load<T>(key);
if (!val)
wups::store(key, variable);
else
variable = *val;
}


void
load()
{
load_or_init(key::hours, hours);
load_or_init(key::minutes, minutes);
load_or_init(key::msg_duration, msg_duration);
load_or_init(key::notify, notify);
load_or_init(key::server, server);
load_or_init(key::sync, sync);
load_or_init(key::tolerance, tolerance);
}


void
update_utc_offset()
{
double offset_seconds = (hours * 60.0 + minutes) * 60.0;
utc::timezone_offset = offset_seconds;
}

} // namespace cfg
38 changes: 38 additions & 0 deletions source/cfg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT

#ifndef CFG_HPP
#define CFG_HPP

#include <string>


namespace cfg {

namespace key {
extern const char* hours;
extern const char* minutes;
extern const char* msg_duration;
extern const char* notify;
extern const char* server;
extern const char* sync;
extern const char* tolerance;
}

extern int hours;
extern int minutes;
extern int msg_duration;
extern bool notify;
extern std::string server;
extern bool sync;
extern int tolerance;


void load();


// send the hours and minutes variables to the utc module
void update_utc_offset();

}

#endif
87 changes: 87 additions & 0 deletions source/config_screen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: MIT

#include <memory> // make_unique()

#include "wupsxx/bool_item.hpp"
#include "wupsxx/int_item.hpp"
#include "wupsxx/text_item.hpp"

#include "config_screen.hpp"

#include "cfg.hpp"
#include "http_client.hpp"
#include "nintendo_glyphs.hpp"
#include "utils.hpp"


using wups::bool_item;
using wups::int_item;
using wups::text_item;
using std::make_unique;

using namespace std::literals;


struct timezone_item : wups::text_item {

timezone_item() :
wups::text_item{"",
"Detect Timezone (press " NIN_GLYPH_BTN_A ")",
"Using http://ip-api.com"}
{}


void
on_button_pressed(WUPSConfigButtons buttons)
override
{
text_item::on_button_pressed(buttons);

if (buttons & WUPS_CONFIG_BUTTON_A)
query_timezone();
}


void
query_timezone()
try {
std::string tz = http::get("http://ip-api.com/line/?fields=timezone,offset");
auto tokens = utils::split(tz, " \r\n");
if (tokens.size() != 2)
throw std::runtime_error{"Could not parse response from \"ip-api.com\"."};

int tz_offset = std::stoi(tokens[1]);
text = tokens[0];

cfg::hours = tz_offset / (60 * 60);
cfg::minutes = tz_offset % (60 * 60) / 60;
if (cfg::minutes < 0) {
cfg::minutes += 60;
--cfg::hours;
}
}
catch (std::exception& e) {
text = "Error: "s + e.what();
}

};


config_screen::config_screen() :
wups::category{"Configuration"}
{
add(make_unique<bool_item>(cfg::key::sync, "Syncing Enabled", cfg::sync));
add(make_unique<bool_item>(cfg::key::notify, "Show Notifications", cfg::notify));
add(make_unique<int_item>(cfg::key::msg_duration, "Notification Duration (seconds)",
cfg::msg_duration, 0, 30));
add(make_unique<int_item>(cfg::key::hours, "Hours Offset", cfg::hours, -12, 14));
add(make_unique<int_item>(cfg::key::minutes, "Minutes Offset", cfg::minutes, 0, 59));

add(make_unique<timezone_item>());

add(make_unique<int_item>(cfg::key::tolerance, "Tolerance (milliseconds)",
cfg::tolerance, 0, 5000));

// show current NTP server address, no way to change it.
add(make_unique<wups::text_item>(cfg::key::server, "NTP servers", cfg::server));
}
15 changes: 15 additions & 0 deletions source/config_screen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT

#ifndef CONFIG_SCREEN_HPP
#define CONFIG_SCREEN_HPP

#include "wupsxx/category.hpp"


struct config_screen : wups::category {

config_screen();

};

#endif
Loading

0 comments on commit 3d5dd85

Please sign in to comment.