-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timezone detection, Y2k36 handling, code refactoring. (#18)
* 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
Showing
26 changed files
with
2,012 additions
and
953 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.