Initial Commit
This commit is contained in:
188
TegraExplorer/source/utils/utils.c
Normal file
188
TegraExplorer/source/utils/utils.c
Normal file
@@ -0,0 +1,188 @@
|
||||
#include "utils.h"
|
||||
#include <string.h>
|
||||
#include <utils/types.h>
|
||||
#include <mem/heap.h>
|
||||
#include <utils/util.h>
|
||||
#include "vector.h"
|
||||
#include "../gfx/gfxutils.h"
|
||||
#include "../gfx/gfx.h"
|
||||
#include "../gfx/menu.h"
|
||||
#include "../hid/hid.h"
|
||||
#include "../fs/fsutils.h"
|
||||
#include "../config.h"
|
||||
|
||||
extern hekate_config h_cfg;
|
||||
|
||||
extern int launch_payload(char *path);
|
||||
|
||||
void ALWAYS_INLINE power_off(){
|
||||
power_set_state(POWER_OFF_RESET);
|
||||
}
|
||||
|
||||
void ALWAYS_INLINE reboot_rcm(){
|
||||
power_set_state(REBOOT_RCM);
|
||||
}
|
||||
|
||||
void ALWAYS_INLINE reboot_normal(){
|
||||
power_set_state((h_cfg.t210b01) ? REBOOT_BYPASS_FUSES : POWER_OFF_REBOOT);
|
||||
}
|
||||
|
||||
void RebootToPayloadOrRcm(){
|
||||
if (FileExists("sd:/atmosphere/reboot_payload.bin"))
|
||||
launch_payload("sd:/atmosphere/reboot_payload.bin");
|
||||
reboot_rcm();
|
||||
}
|
||||
|
||||
char *CpyStr(const char* in){
|
||||
int len = strlen(in);
|
||||
char *out = malloc(len + 1);
|
||||
out[len] = 0;
|
||||
memcpy(out, in, len);
|
||||
return out;
|
||||
}
|
||||
|
||||
void MaskIn(char *mod, u32 bitstream, char mask){
|
||||
u32 len = strlen(mod);
|
||||
for (int i = 0; i < len; i++){
|
||||
if (!(bitstream & 1))
|
||||
*mod = mask;
|
||||
|
||||
bitstream >>= 1;
|
||||
mod++;
|
||||
}
|
||||
}
|
||||
|
||||
// non-zero is yes, zero is no
|
||||
bool StrEndsWith(char *begin, char *end){
|
||||
begin = strrchr(begin, *end);
|
||||
if (begin != NULL)
|
||||
return !strcmp(begin, end);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WaitFor(u32 ms){
|
||||
u32 a = get_tmr_ms();
|
||||
while (a + ms > get_tmr_ms());
|
||||
}
|
||||
|
||||
char *lines[] = {
|
||||
"1234567890*", // 0 - 10
|
||||
"qwertyuiop~", // 11 - 21
|
||||
"asdfghjkl.+", // 22 - 32
|
||||
"^zxcvbnm_<>" // 33 - 43
|
||||
};
|
||||
|
||||
char *ShowKeyboard(const char *toEdit, u8 alwaysRet){
|
||||
char *ret = CpyStr(toEdit);
|
||||
int pos = 0;
|
||||
int posOnWord = 0;
|
||||
bool shift = 0;
|
||||
|
||||
gfx_printf("* = exit | ~ = backspace | ^(left) = shift | + = add char\n\n");
|
||||
|
||||
u32 x, y;
|
||||
gfx_con_getpos(&x, &y);
|
||||
|
||||
while (1){
|
||||
gfx_con_setpos(x, y);
|
||||
|
||||
for (int i = 0; i < strlen(ret); i++){
|
||||
(i == posOnWord) ? SETCOLOR(COLOR_WHITE, COLOR_VIOLET) : SETCOLOR(COLOR_WHITE, COLOR_DEFAULT);
|
||||
gfx_putc(ret[i]);
|
||||
}
|
||||
|
||||
RESETCOLOR;
|
||||
gfx_putc(' ');
|
||||
|
||||
for (int a = 0; a < 4; a++){
|
||||
for (int b = 0; b < 11; b++){
|
||||
(pos == ((b % 11) + (a * 11))) ? SETCOLOR(COLOR_DEFAULT, COLOR_WHITE) : SETCOLOR(COLOR_WHITE, COLOR_DEFAULT);
|
||||
gfx_con_setpos(x + 16 + (b * 2 * 16), y + a * 16 * 2 + 32);
|
||||
if (shift && lines[a][b] >= 'a' && lines[a][b] <= 'z')
|
||||
gfx_putc(lines[a][b] & ~BIT(5));
|
||||
else
|
||||
gfx_putc(lines[a][b]);
|
||||
}
|
||||
}
|
||||
|
||||
Input_t *input = hidWait();
|
||||
if (input->buttons & (JoyA | JoyLB | JoyRB)){
|
||||
if (pos == 42 || input->l){
|
||||
if (posOnWord > 0)
|
||||
posOnWord--;
|
||||
}
|
||||
else if (pos == 43 || input->r){
|
||||
if (strlen(ret) - 1 > posOnWord)
|
||||
posOnWord++;
|
||||
}
|
||||
else if (pos == 10){
|
||||
break;
|
||||
}
|
||||
else if (pos == 21){
|
||||
u32 wordLen = strlen(ret);
|
||||
if (!wordLen)
|
||||
continue;
|
||||
|
||||
for (int i = posOnWord; i < wordLen - 1; i++){
|
||||
ret[i] = ret[i + 1];
|
||||
}
|
||||
ret[wordLen - 1] = 0;
|
||||
if (posOnWord > wordLen - 2)
|
||||
posOnWord--;
|
||||
}
|
||||
else if (pos == 32){
|
||||
u32 wordLen = strlen(ret);
|
||||
if (wordLen >= 79)
|
||||
continue;
|
||||
|
||||
char *copy = calloc(wordLen + 2, 1);
|
||||
memcpy(copy, ret, wordLen);
|
||||
copy[wordLen] = 'a';
|
||||
free(ret);
|
||||
ret = copy;
|
||||
}
|
||||
else if (pos == 33){
|
||||
shift = !shift;
|
||||
}
|
||||
else {
|
||||
char toPut = lines[pos / 11][pos % 11];
|
||||
if (shift)
|
||||
toPut &= ~BIT(5);
|
||||
ret[posOnWord] = toPut;
|
||||
|
||||
if (strlen(ret) - 1 > posOnWord)
|
||||
posOnWord++;
|
||||
}
|
||||
}
|
||||
int val = (input->up || input->down) ? 11 : 1;
|
||||
|
||||
if (input->volm) {
|
||||
if (pos > 0)
|
||||
pos -= 1;
|
||||
}
|
||||
else if (input->volp) {
|
||||
if (pos < 43)
|
||||
pos += 1;
|
||||
}
|
||||
else if (input->buttons & (JoyLLeft | JoyLUp)){
|
||||
if (pos > -1 + val)
|
||||
pos -= val;
|
||||
}
|
||||
else if (input->buttons & (JoyLRight | JoyLDown)){
|
||||
if (pos < 44 - val)
|
||||
pos += val;
|
||||
}
|
||||
|
||||
if (input->b){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(ret, toEdit) && !alwaysRet){
|
||||
free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
15
TegraExplorer/source/utils/utils.h
Normal file
15
TegraExplorer/source/utils/utils.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include <utils/types.h>
|
||||
|
||||
char *CpyStr(const char* in);
|
||||
void MaskIn(char *mod, u32 bitstream, char mask);
|
||||
bool StrEndsWith(char *begin, char *end);
|
||||
void WaitFor(u32 ms);
|
||||
void RebootToPayloadOrRcm();
|
||||
|
||||
#define FREE(x) free(x); x = NULL;
|
||||
char *ShowKeyboard(const char *toEdit, u8 alwaysRet);
|
||||
|
||||
void power_off();
|
||||
void reboot_rcm();
|
||||
void reboot_normal();
|
||||
104
TegraExplorer/source/utils/vector.c
Normal file
104
TegraExplorer/source/utils/vector.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "vector.h"
|
||||
#include "../gfx/gfx.h"
|
||||
#include <string.h>
|
||||
#include <mem/heap.h>
|
||||
|
||||
Vector_t newVec(u8 typesz, u32 preallocate) {
|
||||
if (preallocate) {
|
||||
Vector_t res = {
|
||||
.data = calloc(preallocate, typesz),
|
||||
.capacity = preallocate * typesz,
|
||||
.count = 0,
|
||||
.elemSz = typesz
|
||||
};
|
||||
|
||||
// check .data != null;
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
Vector_t res = {
|
||||
.data = NULL,
|
||||
.capacity = 1 * typesz,
|
||||
.count = 0,
|
||||
.elemSz = typesz
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Vector_t vecFromArray(void* array, u32 count, u32 typesz)
|
||||
{
|
||||
Vector_t res = {
|
||||
.data = array,
|
||||
.capacity = count * typesz,
|
||||
.count = count,
|
||||
.elemSz = typesz
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
int _vecAdd(Vector_t* v, void* elem, u8 sz) {
|
||||
if (!v || !elem || v->elemSz != sz)
|
||||
return 0;
|
||||
|
||||
if (v->data == NULL) {
|
||||
v->data = calloc(1, v->elemSz);
|
||||
}
|
||||
u32 usedbytes = v->count * (u32)v->elemSz;
|
||||
if (usedbytes >= v->capacity)
|
||||
{
|
||||
v->capacity *= 2;
|
||||
void* buff = malloc(v->capacity);
|
||||
if (!buff)
|
||||
return 0;
|
||||
memcpy(buff, v->data, v->capacity / 2);
|
||||
free(v->data);
|
||||
v->data = buff;
|
||||
}
|
||||
memcpy(((u8*)v->data) + usedbytes, elem, v->elemSz);
|
||||
v->count++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Vector_t vecCopyOffset(Vector_t* orig, u32 offset) {
|
||||
Vector_t dst = newVec(orig->elemSz, orig->count - offset);
|
||||
memcpy(dst.data, ((u8*)orig->data + orig->elemSz * offset), (orig->count - offset) * orig->elemSz);
|
||||
dst.count = orig->count - offset;
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vector_t vecCopy(Vector_t* orig) {
|
||||
return vecCopyOffset(orig, 0);
|
||||
}
|
||||
|
||||
|
||||
void* getStackEntry(Vector_t *stack) {
|
||||
if (stack->count <= 0)
|
||||
return NULL;
|
||||
|
||||
return ((u8*)stack->data + (stack->elemSz * (stack->count - 1)));
|
||||
}
|
||||
|
||||
// This will stay valid until the queue is modified
|
||||
void* popStackEntry(Vector_t* stack) {
|
||||
if (stack->count <= 0)
|
||||
return NULL;
|
||||
|
||||
void* a = getStackEntry(stack);
|
||||
stack->count--;
|
||||
return a;
|
||||
}
|
||||
|
||||
void vecRem(Vector_t *vec, int idx) {
|
||||
if (vec->count <= 0 || idx >= vec->count)
|
||||
return;
|
||||
|
||||
if (idx == (vec->count - 1)) {
|
||||
vec->count--;
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy((u8*)vec->data + (vec->elemSz * idx), (u8*)vec->data + (vec->elemSz * (idx + 1)), (vec->count - idx - 1) * vec->elemSz);
|
||||
vec->count--;
|
||||
}
|
||||
41
TegraExplorer/source/utils/vector.h
Normal file
41
TegraExplorer/source/utils/vector.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <utils/types.h>
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
typedef struct {
|
||||
void* data;
|
||||
u32 capacity;
|
||||
u32 count;
|
||||
u8 elemSz;
|
||||
// u32 typeTag;
|
||||
} Vector_t;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
#define FREE(x) free(x); x = NULL;
|
||||
|
||||
#define vecAddElem(v, elem) _vecAdd(v, &elem, sizeof(elem))
|
||||
#define vecAddElement(v, elem) _vecAdd(v, &elem, sizeof(elem))
|
||||
#define vecAdd(vec, element) _vecAdd(vec, &element, sizeof(element))
|
||||
#define vecDefArray(type, varName, vec) type varName = (type)((vec).data)
|
||||
#define vecGetArray(type, vec) (type)((vec).data)
|
||||
#define vecPDefArray(type, varName, vec) type varName = (type)((vec)->data)
|
||||
#define vecPGetArray(type, vec) (type)((vec)->data)
|
||||
#define vecFreePtr(vec) FREE(vec->data)
|
||||
#define vecFree(vec) FREE(vec.data)
|
||||
#define vecGetCapacity(vec) (vec.capacity / vec.elemSz)
|
||||
|
||||
#define vecGetArrayPtr(vec, type) (type)((vec)->data)
|
||||
|
||||
#define vecForEach(type, varname, vecPtr) for (type varname = vecPtr->data; ((u8*)varname - (u8*)vecPtr->data) < (vecPtr->count * vecPtr->elemSz); varname++)
|
||||
|
||||
Vector_t newVec(u8 typesz, u32 preallocate);
|
||||
Vector_t vecFromArray(void* array, u32 count, u32 typesz);
|
||||
bool _vecAdd(Vector_t* v, void* elem, u8 sz);
|
||||
Vector_t vecCopy(Vector_t* orig);
|
||||
Vector_t vecCopyOffset(Vector_t* orig, u32 offset);
|
||||
|
||||
void* getStackEntry(Vector_t* stack);
|
||||
void* popStackEntry(Vector_t* stack);
|
||||
void vecRem(Vector_t * vec, int idx);
|
||||
Reference in New Issue
Block a user