2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// UIModeController.cpp
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Handling of application user interface modes (full, kiosk and kid).
|
|
|
|
// This includes switching the mode when the UI mode passkey is used.
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
|
2017-11-18 22:23:56 +00:00
|
|
|
#include "UIModeController.h"
|
|
|
|
|
|
|
|
#include "utils/StringUtil.h"
|
|
|
|
#include "views/ViewController.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "Window.h"
|
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
UIModeController * UIModeController::sInstance = nullptr;
|
2017-11-18 22:23:56 +00:00
|
|
|
|
|
|
|
UIModeController * UIModeController::getInstance()
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
if (sInstance == nullptr)
|
2020-06-21 12:25:28 +00:00
|
|
|
sInstance = new UIModeController();
|
2017-11-18 22:23:56 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return sInstance;
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UIModeController::UIModeController()
|
2020-06-21 12:25:28 +00:00
|
|
|
:mPassKeyCounter(0)
|
2017-11-18 22:23:56 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mPassKeySequence = Settings::getInstance()->getString("UIMode_passkey");
|
|
|
|
mCurrentUIMode = Settings::getInstance()->getString("UIMode");
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIModeController::monitorUIMode()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string uimode = Settings::getInstance()->getString("UIMode");
|
|
|
|
// UI mode was changed.
|
|
|
|
if (uimode != mCurrentUIMode) {
|
|
|
|
mCurrentUIMode = uimode;
|
|
|
|
ViewController::get()->ReloadAndGoToStart();
|
|
|
|
}
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIModeController::listen(InputConfig * config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Reads the current input to listen for the passkey sequence to unlock
|
|
|
|
// the UI mode. The progress is saved in mPassKeyCounter.
|
|
|
|
if (Settings::getInstance()->getBool("Debug"))
|
|
|
|
logInput(config, input);
|
|
|
|
|
|
|
|
if ((Settings::getInstance()->getString("UIMode") == "Full") || !isValidInput(config, input))
|
|
|
|
return false; // Already unlocked, or invalid input, nothing to do here.
|
|
|
|
|
|
|
|
if (!inputIsMatch(config, input))
|
|
|
|
mPassKeyCounter = 0; // Current input is incorrect, reset counter.
|
|
|
|
|
|
|
|
if (mPassKeyCounter == (int)mPassKeySequence.length()) {
|
|
|
|
unlockUIMode();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIModeController::inputIsMatch(InputConfig * config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
for (auto valstring : mInputVals) {
|
|
|
|
if (config->isMappedLike(valstring, input) &&
|
|
|
|
(mPassKeySequence[mPassKeyCounter] == valstring[0])) {
|
|
|
|
mPassKeyCounter++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// When we have reached the end of the list, trigger UI_mode unlock.
|
2017-11-18 22:23:56 +00:00
|
|
|
void UIModeController::unlockUIMode()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
LOG(LogDebug) <<
|
|
|
|
" UIModeController::listen(): Passkey sequence completed, switching UIMode to full";
|
|
|
|
Settings::getInstance()->setString("UIMode", "Full");
|
|
|
|
Settings::getInstance()->saveFile();
|
|
|
|
mPassKeyCounter = 0;
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIModeController::isUIModeFull()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return ((mCurrentUIMode == "Full") && !Settings::getInstance()->getBool("ForceKiosk"));
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIModeController::isUIModeKid()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return (Settings::getInstance()->getBool("ForceKid") ||
|
|
|
|
((mCurrentUIMode == "Kid") && !Settings::getInstance()->getBool("ForceKiosk")));
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 08:11:19 +00:00
|
|
|
bool UIModeController::isUIModeKiosk()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return (Settings::getInstance()->getBool("ForceKiosk") ||
|
|
|
|
((mCurrentUIMode == "Kiosk") && !Settings::getInstance()->getBool("ForceKid")));
|
2019-01-19 08:11:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 22:23:56 +00:00
|
|
|
std::string UIModeController::getFormattedPassKeyStr()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Supported sequence-inputs: u (up), d (down), l (left), r (right), a, b, x, y.
|
|
|
|
|
|
|
|
std::string out = "";
|
|
|
|
for (auto c : mPassKeySequence) {
|
|
|
|
out += (out == "") ? "" : ", "; // Add a comma after the first entry.
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'u':
|
|
|
|
out += Utils::String::unicode2Chars(0x2191); // Arrow pointing up.
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
out += Utils::String::unicode2Chars(0x2193); // Arrow pointing down.
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
out += Utils::String::unicode2Chars(0x2190); // Arrow pointing left.
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
out += Utils::String::unicode2Chars(0x2192); // Arrow pointing right.
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
out += "A";
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
out += "B";
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
out += "X";
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
out += "Y";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIModeController::logInput(InputConfig * config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string mapname = "";
|
|
|
|
std::vector<std::string> maps = config->getMappedTo(input);
|
2020-06-06 14:48:05 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
for (auto mn : maps) {
|
|
|
|
mapname += mn;
|
|
|
|
mapname += ", ";
|
|
|
|
}
|
2020-06-06 14:48:05 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
LOG(LogDebug) << "UIModeController::logInput( " << config->getDeviceName() <<
|
|
|
|
" ):" << input.string() << ", isMappedTo= " << mapname << ", value=" << input.value;
|
2017-11-18 22:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UIModeController::isValidInput(InputConfig * config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if ((config->getMappedTo(input).size() == 0) || // Not a mapped input, so ignore..
|
|
|
|
(input.type == TYPE_HAT) || // Ignore all hat inputs.
|
|
|
|
(!input.value)) // Not a key-down event.
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
2020-06-06 14:48:05 +00:00
|
|
|
}
|