fs.mitm: Use same override button semantics as loader.

This commit is contained in:
Michael Scire
2018-10-16 19:01:41 -07:00
parent 6711cd93a8
commit 55ad57a02d
9 changed files with 552 additions and 6 deletions

View File

@@ -18,14 +18,20 @@
#include <stratosphere.hpp>
#include <atomic>
#include <algorithm>
#include <strings.h>
#include "sm_mitm.h"
#include "debug.hpp"
#include "fsmitm_utils.hpp"
#include "ini.h"
static FsFileSystem g_sd_filesystem = {0};
static std::vector<u64> g_mitm_flagged_tids;
static std::atomic_bool g_has_initialized = false;
static std::atomic_bool g_has_hid_session = false;
static u64 g_override_key_combination = KEY_R;
static bool g_override_by_default = true;
static bool IsHexadecimal(const char *str) {
while (*str) {
@@ -77,15 +83,36 @@ void Utils::InitializeSdThreadFunc(void *args) {
fsDirClose(&titles_dir);
}
FsFile config_file;
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, "/atmosphere/loader.ini", FS_OPEN_READ, &config_file))) {
Utils::LoadConfiguration(&config_file);
fsFileClose(&config_file);
}
g_has_initialized = true;
svcExitThread();
}
void Utils::InitializeHidThreadFunc(void *args) {
while (R_FAILED(hidInitialize())) {
svcSleepThread(1000ULL);
}
g_has_hid_session = true;
svcExitThread();
}
bool Utils::IsSdInitialized() {
return g_has_initialized;
}
bool Utils::IsHidInitialized() {
return g_has_hid_session;
}
Result Utils::OpenSdFile(const char *fn, int flags, FsFile *out) {
if (!IsSdInitialized()) {
return 0xFA202;
@@ -189,3 +216,108 @@ bool Utils::HasSdMitMFlag(u64 tid) {
}
return false;
}
Result Utils::GetKeysDown(u64 *keys) {
if (!Utils::IsHidInitialized()) {
return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID);
}
hidScanInput();
*keys = hidKeysDown(CONTROLLER_P1_AUTO);
return 0x0;
}
bool Utils::HasOverrideButton(u64 tid) {
if (tid < 0x0100000000010000ULL) {
/* Disable button override disable for non-applications. */
return true;
}
u64 kDown = 0;
bool keys_triggered = (R_SUCCEEDED(GetKeysDown(&kDown)) && ((kDown & g_override_key_combination) != 0));
return IsSdInitialized() && (g_override_by_default ^ keys_triggered);
}
static int FsMitMIniHandler(void *user, const char *section, const char *name, const char *value) {
/* Taken and modified, with love, from Rajkosto's implementation. */
if (strcasecmp(section, "config") == 0) {
if (strcasecmp(name, "override_key") == 0) {
if (value[0] == '!') {
g_override_by_default = true;
value++;
} else {
g_override_by_default = false;
}
if (strcasecmp(value, "A") == 0) {
g_override_key_combination = KEY_A;
} else if (strcasecmp(value, "B") == 0) {
g_override_key_combination = KEY_B;
} else if (strcasecmp(value, "X") == 0) {
g_override_key_combination = KEY_X;
} else if (strcasecmp(value, "Y") == 0) {
g_override_key_combination = KEY_Y;
} else if (strcasecmp(value, "LS") == 0) {
g_override_key_combination = KEY_LSTICK;
} else if (strcasecmp(value, "RS") == 0) {
g_override_key_combination = KEY_RSTICK;
} else if (strcasecmp(value, "L") == 0) {
g_override_key_combination = KEY_L;
} else if (strcasecmp(value, "R") == 0) {
g_override_key_combination = KEY_R;
} else if (strcasecmp(value, "ZL") == 0) {
g_override_key_combination = KEY_ZL;
} else if (strcasecmp(value, "ZR") == 0) {
g_override_key_combination = KEY_ZR;
} else if (strcasecmp(value, "PLUS") == 0) {
g_override_key_combination = KEY_PLUS;
} else if (strcasecmp(value, "MINUS") == 0) {
g_override_key_combination = KEY_MINUS;
} else if (strcasecmp(value, "DLEFT") == 0) {
g_override_key_combination = KEY_DLEFT;
} else if (strcasecmp(value, "DUP") == 0) {
g_override_key_combination = KEY_DUP;
} else if (strcasecmp(value, "DRIGHT") == 0) {
g_override_key_combination = KEY_DRIGHT;
} else if (strcasecmp(value, "DDOWN") == 0) {
g_override_key_combination = KEY_DDOWN;
} else if (strcasecmp(value, "SL") == 0) {
g_override_key_combination = KEY_SL;
} else if (strcasecmp(value, "SR") == 0) {
g_override_key_combination = KEY_SR;
} else {
g_override_key_combination = 0;
}
}
} else {
return 0;
}
return 1;
}
void Utils::LoadConfiguration(FsFile *f) {
if (f == NULL) {
return;
}
u64 size;
if (R_FAILED(fsFileGetSize(f, &size))) {
return;
}
size = std::min(size, (decltype(size))0x7FF);
char *config_str = new char[0x800];
if (config_str != NULL) {
/* Read in string. */
std::fill(config_str, config_str + 0x800, 0);
size_t r_s;
fsFileRead(f, 0, config_str, size, &r_s);
ini_parse_string(config_str, FsMitMIniHandler, NULL);
delete[] config_str;
}
}