VitaShell/utils.c

427 lines
11 KiB
C
Raw Normal View History

2016-08-06 06:59:41 +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-08-06 06:59:41 +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-08-06 06:59:41 +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-08-06 06:59:41 +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-08-06 06:59:41 +00:00
*/
#include "main.h"
2017-03-18 09:56:46 +00:00
#include "init.h"
2016-08-06 06:59:41 +00:00
#include "file.h"
#include "message_dialog.h"
2016-08-28 14:36:27 +00:00
#include "uncommon_dialog.h"
#include "theme.h"
2016-08-06 06:59:41 +00:00
#include "language.h"
#include "utils.h"
#include "bm.h"
2016-08-06 06:59:41 +00:00
SceCtrlData pad;
2018-01-25 21:22:07 +00:00
Pad old_pad, current_pad, pressed_pad, released_pad, hold_pad, hold2_pad;
Pad hold_count, hold2_count;
2016-08-06 06:59:41 +00:00
static int netdbg_sock = -1;
static void *net_memory = NULL;
static int net_init = -1;
static int lock_power = 0;
2017-02-12 17:53:00 +00:00
float easeOut(float x0, float x1, float a, float b) {
2017-10-13 10:42:36 +00:00
float dx = (x1 - x0);
return ((dx * a) > b) ? (dx * a) : dx;
2017-02-12 17:53:00 +00:00
}
2016-09-03 20:14:07 +00:00
void startDrawing(vita2d_texture *bg) {
2017-10-13 10:42:36 +00:00
vita2d_start_drawing();
vita2d_set_clear_color(BACKGROUND_COLOR);
vita2d_clear_screen();
if (wallpaper_image) {
vita2d_draw_texture(wallpaper_image, 0.0f, 0.0f);
} else if (bg) {
vita2d_draw_texture(bg, 0.0f, 0.0f);
}
2016-08-28 14:36:27 +00:00
}
void endDrawing() {
2017-10-13 10:42:36 +00:00
drawUncommonDialog();
vita2d_end_drawing();
vita2d_common_dialog_update();
vita2d_swap_buffers();
sceDisplayWaitVblankStart();
2016-08-28 14:36:27 +00:00
}
2016-09-12 20:34:03 +00:00
void closeWaitDialog() {
2017-10-13 10:42:36 +00:00
sceMsgDialogClose();
2016-09-12 20:34:03 +00:00
2017-10-13 10:42:36 +00:00
while (updateMessageDialog() != MESSAGE_DIALOG_RESULT_NONE) {
sceKernelDelayThread(1000);
}
2016-09-12 20:34:03 +00:00
}
2016-08-06 06:59:41 +00:00
void errorDialog(int error) {
2017-10-13 10:42:36 +00:00
if (error < 0) {
initMessageDialog(SCE_MSG_DIALOG_BUTTON_TYPE_OK, language_container[ERROR], error);
setDialogStep(DIALOG_STEP_ERROR);
}
2016-08-06 06:59:41 +00:00
}
2017-02-12 15:27:43 +00:00
void infoDialog(const char *msg, ...) {
2017-10-13 10:42:36 +00:00
va_list list;
char string[512];
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
va_start(list, msg);
vsnprintf(string, sizeof(string), msg, list);
va_end(list);
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
initMessageDialog(SCE_MSG_DIALOG_BUTTON_TYPE_OK, string);
setDialogStep(DIALOG_STEP_INFO);
2016-08-06 06:59:41 +00:00
}
2017-03-18 09:56:46 +00:00
int checkMemoryCardFreeSpace(const char *path, uint64_t size) {
2017-10-13 10:42:36 +00:00
char device[8];
uint64_t free_size = 0, max_size = 0, extra_space = 0;
char *p = strchr(path, ':');
if (p) {
2018-01-25 21:22:07 +00:00
strncpy(device, path, p - path + 1);
device[p - path + 1] = '\0';
2017-10-13 10:42:36 +00:00
}
if (strcmp(device, "ux0:") == 0) {
extra_space = 40 * 1024 * 1024;
}
if (is_safe_mode) {
sceAppMgrGetDevInfo(device, &max_size, &free_size);
} else {
SceIoDevInfo info;
memset(&info, 0, sizeof(SceIoDevInfo));
sceIoDevctl(device, 0x3001, NULL, 0, &info, sizeof(SceIoDevInfo));
free_size = info.free_size;
max_size = info.max_size;
}
if (size >= (free_size + extra_space)) {
closeWaitDialog();
char size_string[16];
getSizeString(size_string, size - (free_size + extra_space));
infoDialog(language_container[NO_SPACE_ERROR], size_string);
return 1;
}
return 0;
2016-09-12 20:34:03 +00:00
}
2017-02-12 15:27:43 +00:00
static int power_tick_thread(SceSize args, void *argp) {
2017-10-13 10:42:36 +00:00
while (1) {
if (lock_power > 0) {
sceKernelPowerTick(SCE_KERNEL_POWER_TICK_DISABLE_AUTO_SUSPEND);
}
2017-10-13 10:42:36 +00:00
sceKernelDelayThread(10 * 1000 * 1000);
}
2017-10-13 10:42:36 +00:00
return 0;
}
void initPowerTickThread() {
2017-10-13 10:42:36 +00:00
SceUID thid = sceKernelCreateThread("power_tick_thread", power_tick_thread, 0x10000100, 0x40000, 0, 0, NULL);
if (thid >= 0)
sceKernelStartThread(thid, 0, NULL);
}
void powerLock() {
2017-10-13 10:42:36 +00:00
if (!lock_power)
sceShellUtilLock(SCE_SHELL_UTIL_LOCK_TYPE_PS_BTN);
2016-11-03 15:57:08 +00:00
2017-10-13 10:42:36 +00:00
lock_power++;
}
void powerUnlock() {
2017-10-13 10:42:36 +00:00
if (lock_power)
sceShellUtilUnlock(SCE_SHELL_UTIL_LOCK_TYPE_PS_BTN);
2016-11-03 15:57:08 +00:00
2017-10-13 10:42:36 +00:00
lock_power--;
if (lock_power < 0)
lock_power = 0;
2016-08-06 06:59:41 +00:00
}
2018-01-25 21:22:07 +00:00
void readPad() {
2017-10-13 10:42:36 +00:00
memset(&pad, 0, sizeof(SceCtrlData));
sceCtrlPeekBufferPositive(0, &pad, 1);
2018-01-25 21:22:07 +00:00
memcpy(&old_pad, current_pad, sizeof(Pad));
memset(&current_pad, 0, sizeof(Pad));
if (pad.buttons & SCE_CTRL_UP)
current_pad[PAD_UP] = 1;
if (pad.buttons & SCE_CTRL_DOWN)
current_pad[PAD_DOWN] = 1;
if (pad.buttons & SCE_CTRL_LEFT)
current_pad[PAD_LEFT] = 1;
if (pad.buttons & SCE_CTRL_RIGHT)
current_pad[PAD_RIGHT] = 1;
if (pad.buttons & SCE_CTRL_LTRIGGER)
current_pad[PAD_LTRIGGER] = 1;
if (pad.buttons & SCE_CTRL_RTRIGGER)
current_pad[PAD_RTRIGGER] = 1;
if (pad.buttons & SCE_CTRL_TRIANGLE)
current_pad[PAD_TRIANGLE] = 1;
if (pad.buttons & SCE_CTRL_CIRCLE)
current_pad[PAD_CIRCLE] = 1;
if (pad.buttons & SCE_CTRL_CROSS)
current_pad[PAD_CROSS] = 1;
if (pad.buttons & SCE_CTRL_SQUARE)
current_pad[PAD_SQUARE] = 1;
if (pad.buttons & SCE_CTRL_START)
current_pad[PAD_START] = 1;
if (pad.buttons & SCE_CTRL_SELECT)
current_pad[PAD_SELECT] = 1;
2018-01-03 21:37:11 +00:00
if (pad.ly < ANALOG_CENTER - ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_LEFT_ANALOG_UP] = 1;
2018-01-03 21:37:11 +00:00
} else if (pad.ly > ANALOG_CENTER + ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_LEFT_ANALOG_DOWN] = 1;
2017-10-13 10:42:36 +00:00
}
2018-01-03 21:37:11 +00:00
if (pad.lx < ANALOG_CENTER - ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_LEFT_ANALOG_LEFT] = 1;
2018-01-03 21:37:11 +00:00
} else if (pad.lx > ANALOG_CENTER + ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_LEFT_ANALOG_RIGHT] = 1;
2017-10-13 10:42:36 +00:00
}
2018-01-03 21:37:11 +00:00
if (pad.ry < ANALOG_CENTER - ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_RIGHT_ANALOG_UP] = 1;
2018-01-03 21:37:11 +00:00
} else if (pad.ry > ANALOG_CENTER + ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_RIGHT_ANALOG_DOWN] = 1;
2017-10-13 10:42:36 +00:00
}
2018-01-03 21:37:11 +00:00
if (pad.rx < ANALOG_CENTER - ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_RIGHT_ANALOG_LEFT] = 1;
2018-01-03 21:37:11 +00:00
} else if (pad.rx > ANALOG_CENTER + ANALOG_THRESHOLD) {
2018-01-25 21:22:07 +00:00
current_pad[PAD_RIGHT_ANALOG_RIGHT] = 1;
2017-10-13 10:42:36 +00:00
}
2018-01-25 21:22:07 +00:00
int i;
for (i = 0; i < PAD_N_BUTTONS; i++) {
pressed_pad[i] = current_pad[i] & ~old_pad[i];
released_pad[i] = ~current_pad[i] & old_pad[i];
hold_pad[i] = pressed_pad[i];
hold2_pad[i] = pressed_pad[i];
if (current_pad[i]) {
if (hold_count[i] >= 10) {
hold_pad[i] = 1;
hold_count[i] = 6;
}
2017-10-13 10:42:36 +00:00
2018-01-25 21:22:07 +00:00
if (hold2_count[i] >= 10) {
hold2_pad[i] = 1;
hold2_count[i] = 10;
}
2017-10-13 10:42:36 +00:00
2018-01-25 21:22:07 +00:00
hold_count[i]++;
hold2_count[i]++;
} else {
hold_count[i] = 0;
hold2_count[i] = 0;
2017-10-13 10:42:36 +00:00
}
2018-01-25 21:22:07 +00:00
}
2018-01-27 14:28:51 +00:00
if (enter_button == SCE_SYSTEM_PARAM_ENTER_BUTTON_CIRCLE) {
2018-01-25 21:22:07 +00:00
old_pad[PAD_ENTER] = old_pad[PAD_CIRCLE];
current_pad[PAD_ENTER] = current_pad[PAD_CIRCLE];
pressed_pad[PAD_ENTER] = pressed_pad[PAD_CIRCLE];
released_pad[PAD_ENTER] = released_pad[PAD_CIRCLE];
hold_pad[PAD_ENTER] = hold_pad[PAD_CIRCLE];
hold2_pad[PAD_ENTER] = hold2_pad[PAD_CIRCLE];
old_pad[PAD_CANCEL] = old_pad[PAD_CROSS];
current_pad[PAD_CANCEL] = current_pad[PAD_CROSS];
pressed_pad[PAD_CANCEL] = pressed_pad[PAD_CROSS];
released_pad[PAD_CANCEL] = released_pad[PAD_CROSS];
hold_pad[PAD_CANCEL] = hold_pad[PAD_CROSS];
hold2_pad[PAD_CANCEL] = hold2_pad[PAD_CROSS];
2017-10-13 10:42:36 +00:00
} else {
2018-01-25 21:22:07 +00:00
old_pad[PAD_ENTER] = old_pad[PAD_CROSS];
current_pad[PAD_ENTER] = current_pad[PAD_CROSS];
pressed_pad[PAD_ENTER] = pressed_pad[PAD_CROSS];
released_pad[PAD_ENTER] = released_pad[PAD_CROSS];
hold_pad[PAD_ENTER] = hold_pad[PAD_CROSS];
hold2_pad[PAD_ENTER] = hold2_pad[PAD_CROSS];
old_pad[PAD_CANCEL] = old_pad[PAD_CIRCLE];
current_pad[PAD_CANCEL] = current_pad[PAD_CIRCLE];
pressed_pad[PAD_CANCEL] = pressed_pad[PAD_CIRCLE];
released_pad[PAD_CANCEL] = released_pad[PAD_CIRCLE];
hold_pad[PAD_CANCEL] = hold_pad[PAD_CIRCLE];
hold2_pad[PAD_CANCEL] = hold2_pad[PAD_CIRCLE];
2017-10-13 10:42:36 +00:00
}
2016-08-06 06:59:41 +00:00
}
int holdButtons(SceCtrlData *pad, uint32_t buttons, uint64_t time) {
2017-10-13 10:42:36 +00:00
if ((pad->buttons & buttons) == buttons) {
uint64_t time_start = sceKernelGetProcessTimeWide();
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
while ((pad->buttons & buttons) == buttons) {
sceKernelDelayThread(10 * 1000);
sceCtrlPeekBufferPositive(0, pad, 1);
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
if ((sceKernelGetProcessTimeWide() - time_start) >= time) {
return 1;
}
}
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
return 0;
2016-08-06 06:59:41 +00:00
}
2017-02-12 15:27:43 +00:00
int hasEndSlash(const char *path) {
2017-10-13 10:42:36 +00:00
return path[strlen(path) - 1] == '/';
2016-09-06 19:57:04 +00:00
}
2016-08-06 06:59:41 +00:00
int removeEndSlash(char *path) {
2017-10-13 10:42:36 +00:00
int len = strlen(path);
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
if (path[len - 1] == '/') {
path[len - 1] = '\0';
return 1;
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
return 0;
2016-08-06 06:59:41 +00:00
}
int addEndSlash(char *path) {
2017-10-13 10:42:36 +00:00
int len = strlen(path);
if (len < MAX_PATH_LENGTH - 2) {
if (path[len - 1] != '/') {
2018-01-03 21:37:11 +00:00
path[len] = '/';
path[len + 1] = '\0';
2017-10-13 10:42:36 +00:00
return 1;
}
}
return 0;
2016-08-06 06:59:41 +00:00
}
2017-02-25 09:27:52 +00:00
void getSizeString(char string[16], uint64_t size) {
2017-10-13 10:42:36 +00:00
double double_size = (double)size;
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
int i = 0;
static char *units[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
while (double_size >= 1024.0) {
double_size /= 1024.0;
i++;
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
snprintf(string, 16, "%.*f %s", (i == 0) ? 0 : 2, double_size, units[i]);
2016-08-06 06:59:41 +00:00
}
2016-11-01 08:53:35 +00:00
void convertUtcToLocalTime(SceDateTime *time_local, SceDateTime *time_utc) {
2017-10-13 10:42:36 +00:00
SceRtcTick tick;
sceRtcGetTick(time_utc, &tick);
sceRtcConvertUtcToLocalTime(&tick, &tick);
sceRtcSetTick(time_local, &tick);
2016-11-01 08:53:35 +00:00
}
void convertLocalTimeToUtc(SceDateTime *time_utc, SceDateTime *time_local) {
2017-10-13 10:42:36 +00:00
SceRtcTick tick;
sceRtcGetTick(time_local, &tick);
sceRtcConvertLocalTimeToUtc(&tick, &tick);
sceRtcSetTick(time_utc, &tick);
2016-11-01 08:53:35 +00:00
}
2017-02-25 09:27:52 +00:00
void getDateString(char string[24], int date_format, SceDateTime *time) {
2017-10-13 10:42:36 +00:00
SceDateTime time_local;
convertUtcToLocalTime(&time_local, time);
switch (date_format) {
case SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD:
snprintf(string, 24, "%04d/%02d/%02d", time_local.year, time_local.month, time_local.day);
break;
case SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY:
snprintf(string, 24, "%02d/%02d/%04d", time_local.day, time_local.month, time_local.year);
break;
case SCE_SYSTEM_PARAM_DATE_FORMAT_MMDDYYYY:
snprintf(string, 24, "%02d/%02d/%04d", time_local.month, time_local.day, time_local.year);
break;
}
2016-08-06 06:59:41 +00:00
}
2017-02-25 09:27:52 +00:00
void getTimeString(char string[16], int time_format, SceDateTime *time) {
2017-10-13 10:42:36 +00:00
SceDateTime time_local;
convertUtcToLocalTime(&time_local, time);
switch(time_format) {
case SCE_SYSTEM_PARAM_TIME_FORMAT_12HR:
2018-01-03 21:37:11 +00:00
snprintf(string, 16, "%02d:%02d %s", (time_local.hour > 12) ? (time_local.hour - 12) : ((time_local.hour == 0) ? 12 : time_local.hour), time_local.minute, time_local.hour >= 12 ? "PM" : "AM");
2017-10-13 10:42:36 +00:00
break;
case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR:
snprintf(string, 16, "%02d:%02d", time_local.hour, time_local.minute);
break;
}
2016-08-06 06:59:41 +00:00
}
2017-02-12 15:27:43 +00:00
int debugPrintf(const char *text, ...) {
2017-10-13 10:42:36 +00:00
va_list list;
char string[512];
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
va_start(list, text);
vsnprintf(string, sizeof(string), text, list);
va_end(list);
2016-08-06 06:59:41 +00:00
2018-01-03 21:37:11 +00:00
SceUID fd = sceIoOpen("ux0:data/vitashell_log.txt", SCE_O_WRONLY | SCE_O_CREAT | SCE_O_APPEND, 0777);
2017-10-13 10:42:36 +00:00
if (fd >= 0) {
sceIoWrite(fd, string, strlen(string));
sceIoClose(fd);
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
return 0;
2016-09-04 10:55:15 +00:00
}
2017-02-12 15:27:43 +00:00
int launchAppByUriExit(const char *titleid) {
2017-10-13 10:42:36 +00:00
char uri[32];
snprintf(uri, sizeof(uri), "psgm:play?titleid=%s", titleid);
2016-09-04 10:55:15 +00:00
2017-10-13 10:42:36 +00:00
sceAppMgrLaunchAppByUri(0xFFFFF, uri);
sceKernelExitProcess(0);
2016-09-04 10:55:15 +00:00
2017-10-13 10:42:36 +00:00
return 0;
}
char *strcasestr(const char *haystack, const char *needle) {
2017-10-13 10:42:36 +00:00
return boyer_moore(haystack, needle);
2016-10-18 12:35:35 +00:00
}
2017-01-12 16:45:52 +00:00
2017-02-13 10:06:23 +00:00
int vshIoMount(int id, const char *path, int permission, int a4, int a5, int a6) {
2017-10-13 10:42:36 +00:00
uint32_t buf[3];
2017-01-12 16:45:52 +00:00
2017-10-13 10:42:36 +00:00
buf[0] = a4;
buf[1] = a5;
buf[2] = a6;
2017-01-12 16:45:52 +00:00
2017-10-13 10:42:36 +00:00
return _vshIoMount(id, path, permission, buf);
2017-01-12 16:45:52 +00:00
}
2017-02-24 17:42:34 +00:00
void remount(int id) {
2017-10-13 10:42:36 +00:00
vshIoUmount(id, 0, 0, 0);
vshIoUmount(id, 1, 0, 0);
vshIoMount(id, NULL, 0, 0, 0, 0);
2017-01-12 16:45:52 +00:00
}