VitaShell/settings.c

384 lines
12 KiB
C
Raw Normal View History

2016-10-31 18:35:25 +00:00
/*
2017-10-13 10:42:36 +00:00
VitaShell
2017-12-30 10:43:37 +00:00
Copyright (C) 2015-2018, TheFloW
2016-10-31 18:35:25 +00:00
2017-10-13 10:42:36 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2016-10-31 18:35:25 +00:00
2017-10-13 10:42:36 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2016-10-31 18:35:25 +00:00
2017-10-13 10:42:36 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-10-31 18:35:25 +00:00
*/
#include "main.h"
2016-11-02 18:54:40 +00:00
#include "config.h"
2016-10-31 18:35:25 +00:00
#include "init.h"
#include "theme.h"
#include "language.h"
#include "settings.h"
2016-11-01 15:34:15 +00:00
#include "message_dialog.h"
2016-10-31 18:35:25 +00:00
#include "ime_dialog.h"
#include "utils.h"
2017-03-13 20:45:04 +00:00
static void restartShell();
2017-02-12 15:27:43 +00:00
static void rebootDevice();
static void shutdownDevice();
static void suspendDevice();
2016-11-01 17:25:42 +00:00
2016-11-02 18:54:40 +00:00
static int changed = 0;
2017-03-13 20:45:04 +00:00
static int theme = 0;
2016-11-02 18:54:40 +00:00
2016-11-01 17:25:42 +00:00
static char spoofed_version[6];
2016-10-31 18:35:25 +00:00
2016-10-31 19:31:44 +00:00
static SettingsMenuEntry *settings_menu_entries = NULL;
static int n_settings_entries = 0;
2016-10-31 18:35:25 +00:00
static char *usbdevice_options[4];
2017-01-12 16:45:52 +00:00
static char *select_button_options[2];
2017-03-13 20:45:04 +00:00
static char **theme_options = NULL;
static int theme_count = 0;
static char *theme_name = NULL;
2016-11-02 18:54:40 +00:00
static ConfigEntry settings_entries[] = {
2017-10-13 10:42:36 +00:00
{ "USBDEVICE", CONFIG_TYPE_DECIMAL, (int *)&vitashell_config.usbdevice },
{ "SELECT_BUTTON", CONFIG_TYPE_DECIMAL, (int *)&vitashell_config.select_button },
{ "DISABLE_AUTOUPDATE", CONFIG_TYPE_BOOLEAN, (int *)&vitashell_config.disable_autoupdate },
2016-11-02 18:54:40 +00:00
};
2017-03-13 20:45:04 +00:00
static ConfigEntry theme_entries[] = {
2017-10-13 10:42:36 +00:00
{ "THEME_NAME", CONFIG_TYPE_STRING, (void *)&theme_name },
2017-03-13 20:45:04 +00:00
};
2016-11-01 15:49:27 +00:00
SettingsMenuOption main_settings[] = {
2017-10-13 10:42:36 +00:00
// { VITASHELL_SETTINGS_LANGUAGE, SETTINGS_OPTION_TYPE_BOOLEAN, NULL, NULL, 0, NULL, 0, &language },
{ VITASHELL_SETTINGS_THEME, SETTINGS_OPTION_TYPE_OPTIONS, NULL, NULL, 0, NULL, 0, NULL },
{ VITASHELL_SETTINGS_USBDEVICE, SETTINGS_OPTION_TYPE_OPTIONS, NULL, NULL, 0,
usbdevice_options, sizeof(usbdevice_options) / sizeof(char **), &vitashell_config.usbdevice },
{ VITASHELL_SETTINGS_SELECT_BUTTON, SETTINGS_OPTION_TYPE_OPTIONS, NULL, NULL, 0,
select_button_options, sizeof(select_button_options) / sizeof(char **), &vitashell_config.select_button },
{ VITASHELL_SETTINGS_NO_AUTO_UPDATE, SETTINGS_OPTION_TYPE_BOOLEAN, NULL, NULL, 0, NULL, 0, &vitashell_config.disable_autoupdate },
{ VITASHELL_SETTINGS_RESTART_SHELL, SETTINGS_OPTION_TYPE_CALLBACK, (void *)restartShell, NULL, 0, NULL, 0, NULL },
2016-11-01 17:25:42 +00:00
};
SettingsMenuOption power_settings[] = {
2017-10-13 10:42:36 +00:00
{ VITASHELL_SETTINGS_REBOOT, SETTINGS_OPTION_TYPE_CALLBACK, (void *)rebootDevice, NULL, 0, NULL, 0, NULL },
{ VITASHELL_SETTINGS_POWEROFF, SETTINGS_OPTION_TYPE_CALLBACK, (void *)shutdownDevice, NULL, 0, NULL, 0, NULL },
{ VITASHELL_SETTINGS_STANDBY, SETTINGS_OPTION_TYPE_CALLBACK, (void *)suspendDevice, NULL, 0, NULL, 0, NULL },
2016-10-31 19:31:44 +00:00
};
SettingsMenuEntry vitashell_settings_menu_entries[] = {
2017-10-13 10:42:36 +00:00
{ VITASHELL_SETTINGS_MAIN, main_settings, sizeof(main_settings) / sizeof(SettingsMenuOption) },
{ VITASHELL_SETTINGS_POWER, power_settings, sizeof(power_settings) / sizeof(SettingsMenuOption) },
2016-10-31 19:31:44 +00:00
};
2016-10-31 18:35:25 +00:00
2016-10-31 19:31:44 +00:00
static SettingsMenu settings_menu;
2016-10-31 18:35:25 +00:00
2016-11-02 18:54:40 +00:00
void loadSettingsConfig() {
2017-10-13 10:42:36 +00:00
// Load settings config file
memset(&vitashell_config, 0, sizeof(VitaShellConfig));
readConfig("ux0:VitaShell/settings.txt", settings_entries, sizeof(settings_entries) / sizeof(ConfigEntry));
2016-11-02 18:54:40 +00:00
}
void saveSettingsConfig() {
2017-10-13 10:42:36 +00:00
// Save settings config file
writeConfig("ux0:VitaShell/settings.txt", settings_entries, sizeof(settings_entries) / sizeof(ConfigEntry));
2017-09-16 20:34:06 +00:00
2017-10-13 10:42:36 +00:00
if (sceKernelGetModel() == SCE_KERNEL_MODEL_VITATV) {
vitashell_config.select_button = SELECT_BUTTON_MODE_FTP;
}
2016-11-02 18:54:40 +00:00
}
2017-03-13 20:45:04 +00:00
static void restartShell() {
2017-10-13 10:42:36 +00:00
closeSettingsMenu();
sceAppMgrLoadExec("app0:eboot.bin", NULL, NULL);
2017-03-13 20:45:04 +00:00
}
2017-02-12 15:27:43 +00:00
static void rebootDevice() {
2017-10-13 10:42:36 +00:00
closeSettingsMenu();
scePowerRequestColdReset();
2016-11-01 17:25:42 +00:00
}
2017-02-12 15:27:43 +00:00
static void shutdownDevice() {
2017-10-13 10:42:36 +00:00
closeSettingsMenu();
scePowerRequestStandby();
2016-11-01 17:25:42 +00:00
}
2017-02-12 15:27:43 +00:00
static void suspendDevice() {
2017-10-13 10:42:36 +00:00
closeSettingsMenu();
scePowerRequestSuspend();
2016-11-01 17:25:42 +00:00
}
2016-10-31 18:35:25 +00:00
void initSettingsMenu() {
2017-10-13 10:42:36 +00:00
int i;
2017-03-13 20:45:04 +00:00
2017-10-13 10:42:36 +00:00
memset(&settings_menu, 0, sizeof(SettingsMenu));
settings_menu.status = SETTINGS_MENU_CLOSED;
2016-10-31 18:35:25 +00:00
2017-10-13 10:42:36 +00:00
n_settings_entries = sizeof(vitashell_settings_menu_entries) / sizeof(SettingsMenuEntry);
settings_menu_entries = vitashell_settings_menu_entries;
2016-10-31 19:31:44 +00:00
2017-10-13 10:42:36 +00:00
for (i = 0; i < n_settings_entries; i++)
settings_menu.n_options += settings_menu_entries[i].n_options;
2017-01-12 16:45:52 +00:00
2017-10-13 10:42:36 +00:00
usbdevice_options[0] = language_container[VITASHELL_SETTINGS_USB_MEMORY_CARD];
usbdevice_options[1] = language_container[VITASHELL_SETTINGS_USB_GAME_CARD];
usbdevice_options[2] = language_container[VITASHELL_SETTINGS_USB_SD2VITA];
usbdevice_options[3] = language_container[VITASHELL_SETTINGS_USB_PSVSD];
2017-03-05 11:32:53 +00:00
2017-10-13 10:42:36 +00:00
select_button_options[0] = language_container[VITASHELL_SETTINGS_SELECT_BUTTON_USB];
select_button_options[1] = language_container[VITASHELL_SETTINGS_SELECT_BUTTON_FTP];
theme_options = malloc(MAX_THEMES * sizeof(char *));
for (i = 0; i < MAX_THEMES; i++)
theme_options[i] = malloc(MAX_THEME_LENGTH);
2016-10-31 18:35:25 +00:00
}
void openSettingsMenu() {
2017-10-13 10:42:36 +00:00
settings_menu.status = SETTINGS_MENU_OPENING;
settings_menu.entry_sel = 0;
settings_menu.option_sel = 0;
// Get current theme
if (theme_name)
free(theme_name);
readConfig("ux0:VitaShell/theme/theme.txt", theme_entries, sizeof(theme_entries) / sizeof(ConfigEntry));
// Get theme index in main tab
int theme_index = -1;
int i;
for (i = 0; i < (sizeof(main_settings) / sizeof(SettingsMenuOption)); i++) {
if (main_settings[i].name == VITASHELL_SETTINGS_THEME) {
theme_index = i;
break;
}
}
// Find all themes
if (theme_index >= 0) {
SceUID dfd = sceIoDopen("ux0:VitaShell/theme");
if (dfd >= 0) {
theme_count = 0;
theme = 0;
int res = 0;
do {
SceIoDirent dir;
memset(&dir, 0, sizeof(SceIoDirent));
res = sceIoDread(dfd, &dir);
if (res > 0) {
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
if (theme_name && strcasecmp(dir.d_name, theme_name) == 0)
2017-10-13 10:42:36 +00:00
theme = theme_count;
strncpy(theme_options[theme_count], dir.d_name, MAX_THEME_LENGTH);
theme_count++;
}
}
} while (res > 0 && theme_count < MAX_THEMES);
sceIoDclose(dfd);
main_settings[theme_index].options = theme_options;
main_settings[theme_index].n_options = theme_count;
main_settings[theme_index].value = &theme;
}
}
changed = 0;
2016-10-31 18:35:25 +00:00
}
void closeSettingsMenu() {
2017-10-13 10:42:36 +00:00
settings_menu.status = SETTINGS_MENU_CLOSING;
// Save settings
if (changed) {
saveSettingsConfig();
// Save theme config file
theme_entries[0].value = &theme_options[theme];
writeConfig("ux0:VitaShell/theme/theme.txt", theme_entries, sizeof(theme_entries) / sizeof(ConfigEntry));
theme_entries[0].value = (void *)&theme_name;
}
2016-10-31 18:35:25 +00:00
}
int getSettingsMenuStatus() {
2017-10-13 10:42:36 +00:00
return settings_menu.status;
2016-10-31 18:35:25 +00:00
}
void drawSettingsMenu() {
2017-10-13 10:42:36 +00:00
if (settings_menu.status == SETTINGS_MENU_CLOSED)
return;
// Closing settings menu
if (settings_menu.status == SETTINGS_MENU_CLOSING) {
if (settings_menu.cur_pos > 0.0f) {
settings_menu.cur_pos -= easeOut(0.0f, settings_menu.cur_pos, 0.25f, 0.01f);
} else {
settings_menu.status = SETTINGS_MENU_CLOSED;
}
}
// Opening settings menu
if (settings_menu.status == SETTINGS_MENU_OPENING) {
if (settings_menu.cur_pos < SCREEN_HEIGHT) {
settings_menu.cur_pos += easeOut(settings_menu.cur_pos, SCREEN_HEIGHT, 0.25f, 0.01f);
} else {
settings_menu.status = SETTINGS_MENU_OPENED;
}
}
// Draw settings menu
vita2d_draw_texture(settings_image, 0.0f, SCREEN_HEIGHT - settings_menu.cur_pos);
float y = SCREEN_HEIGHT - settings_menu.cur_pos + START_Y;
int i;
for (i = 0; i < n_settings_entries; i++) {
// Title
2017-12-29 23:23:08 +00:00
float x = pgf_text_width(language_container[settings_menu_entries[i].name]);
pgf_draw_text(ALIGN_CENTER(SCREEN_WIDTH, x), y, SETTINGS_MENU_TITLE_COLOR, language_container[settings_menu_entries[i].name]);
2017-10-13 10:42:36 +00:00
y += FONT_Y_SPACE;
SettingsMenuOption *options = settings_menu_entries[i].options;
int j;
for (j = 0; j < settings_menu_entries[i].n_options; j++) {
// Focus
if (settings_menu.entry_sel == i && settings_menu.option_sel == j)
vita2d_draw_rectangle(SHELL_MARGIN_X, y + 3.0f, MARK_WIDTH, FONT_Y_SPACE, SETTINGS_MENU_FOCUS_COLOR);
if (options[j].type == SETTINGS_OPTION_TYPE_CALLBACK) {
// Item
2017-12-29 23:23:08 +00:00
float x = pgf_text_width(language_container[options[j].name]);
pgf_draw_text(ALIGN_CENTER(SCREEN_WIDTH, x), y, SETTINGS_MENU_ITEM_COLOR, language_container[options[j].name]);
2017-10-13 10:42:36 +00:00
} else {
// Item
2017-12-29 23:23:08 +00:00
float x = pgf_text_width(language_container[options[j].name]);
pgf_draw_text(ALIGN_RIGHT(SCREEN_HALF_WIDTH - 10.0f, x), y, SETTINGS_MENU_ITEM_COLOR, language_container[options[j].name]);
2017-10-13 10:42:36 +00:00
// Option
switch (options[j].type) {
case SETTINGS_OPTION_TYPE_BOOLEAN:
2017-12-29 23:23:08 +00:00
pgf_draw_text(SCREEN_HALF_WIDTH + 10.0f, y, SETTINGS_MENU_OPTION_COLOR,
2017-10-13 10:42:36 +00:00
(options[j].value && *(options[j].value)) ? language_container[ON] : language_container[OFF]);
break;
case SETTINGS_OPTION_TYPE_STRING:
2017-12-29 23:23:08 +00:00
pgf_draw_text(SCREEN_HALF_WIDTH + 10.0f, y, SETTINGS_MENU_OPTION_COLOR, options[j].string);
2017-10-13 10:42:36 +00:00
break;
case SETTINGS_OPTION_TYPE_OPTIONS:
{
int value = 0;
if (options[j].value)
value = *(options[j].value);
2017-12-29 23:23:08 +00:00
pgf_draw_text(SCREEN_HALF_WIDTH + 10.0f, y, SETTINGS_MENU_OPTION_COLOR, options[j].options ? options[j].options[value] : "");
2017-10-13 10:42:36 +00:00
break;
}
}
}
y += FONT_Y_SPACE;
}
y += FONT_Y_SPACE;
}
2016-10-31 18:35:25 +00:00
}
2016-11-01 15:34:15 +00:00
static int agreement = SETTINGS_AGREEMENT_NONE;
void settingsAgree() {
2017-10-13 10:42:36 +00:00
agreement = SETTINGS_AGREEMENT_AGREE;
2016-11-01 15:34:15 +00:00
}
void settingsDisagree() {
2017-10-13 10:42:36 +00:00
agreement = SETTINGS_AGREEMENT_DISAGREE;
2016-11-01 15:34:15 +00:00
}
2016-10-31 18:35:25 +00:00
void settingsMenuCtrl() {
2017-10-13 10:42:36 +00:00
SettingsMenuOption *option = &settings_menu_entries[settings_menu.entry_sel].options[settings_menu.option_sel];
// Agreement
if (agreement != SETTINGS_AGREEMENT_NONE) {
agreement = SETTINGS_AGREEMENT_NONE;
}
// Change options
if (pressed_buttons & (SCE_CTRL_ENTER | SCE_CTRL_LEFT | SCE_CTRL_RIGHT)) {
changed = 1;
switch (option->type) {
case SETTINGS_OPTION_TYPE_BOOLEAN:
if (option->value)
*(option->value) = !*(option->value);
break;
case SETTINGS_OPTION_TYPE_STRING:
initImeDialog(language_container[option->name], option->string, option->size_string, SCE_IME_TYPE_EXTENDED_NUMBER, 0);
setDialogStep(DIALOG_STEP_SETTINGS_STRING);
break;
case SETTINGS_OPTION_TYPE_CALLBACK:
if (option->callback)
option->callback(&option);
break;
case SETTINGS_OPTION_TYPE_OPTIONS:
{
if (option->value) {
if (pressed_buttons & SCE_CTRL_LEFT) {
if (*(option->value) > 0)
(*(option->value))--;
else
*(option->value) = option->n_options - 1;
} else if (pressed_buttons & (SCE_CTRL_ENTER | SCE_CTRL_RIGHT)) {
if (*(option->value) < option->n_options - 1)
(*(option->value))++;
else
*(option->value) = 0;
}
}
break;
}
}
}
// Move
if (hold_buttons & SCE_CTRL_UP || hold2_buttons & SCE_CTRL_LEFT_ANALOG_UP) {
if (settings_menu.option_sel > 0) {
settings_menu.option_sel--;
} else if (settings_menu.entry_sel > 0) {
settings_menu.entry_sel--;
settings_menu.option_sel = settings_menu_entries[settings_menu.entry_sel].n_options - 1;
}
} else if (hold_buttons & SCE_CTRL_DOWN || hold2_buttons & SCE_CTRL_LEFT_ANALOG_DOWN) {
if (settings_menu.option_sel < settings_menu_entries[settings_menu.entry_sel].n_options - 1) {
settings_menu.option_sel++;
} else if (settings_menu.entry_sel < n_settings_entries - 1) {
settings_menu.entry_sel++;
settings_menu.option_sel = 0;
}
}
// Close
if (pressed_buttons & (SCE_CTRL_CANCEL | SCE_CTRL_START)) {
closeSettingsMenu();
}
2016-10-31 18:35:25 +00:00
}