removed useless stack clearing and whitespace

This commit is contained in:
Wolfvak 2019-05-26 15:06:13 -03:00 committed by d0k3
parent ca7944ce04
commit f179caaef3
2 changed files with 84 additions and 87 deletions

View File

@ -235,7 +235,7 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
if (!BuildKeyboard(swkbd_numpad, keys_numpad, layout_numpad)) return false; if (!BuildKeyboard(swkbd_numpad, keys_numpad, layout_numpad)) return false;
// (instructional) text // (instructional) text
char str[512] = { 0 }; // arbitrary limit, should be more than enough char str[512]; // arbitrary limit, should be more than enough
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, 512, format, va); vsnprintf(str, 512, format, va);

View File

@ -254,7 +254,7 @@ void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcol
void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...) void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...)
{ {
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
@ -266,7 +266,7 @@ void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *
void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...) void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...)
{ {
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
@ -365,7 +365,7 @@ void FormatNumber(char* str, u64 number) { // str should be 32 byte in size
void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just to be safe void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just to be safe
const char* units[] = {" Byte", " kB", " MB", " GB"}; const char* units[] = {" Byte", " kB", " MB", " GB"};
if (bytes == (u64) -1) snprintf(str, 32, "INVALID"); if (bytes == (u64) -1) snprintf(str, 32, "INVALID");
else if (bytes < 1024) snprintf(str, 32, "%llu%s", bytes, units[0]); else if (bytes < 1024) snprintf(str, 32, "%llu%s", bytes, units[0]);
else { else {
@ -379,12 +379,12 @@ void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just
void ShowString(const char *format, ...) void ShowString(const char *format, ...)
{ {
if (format && *format) { // only if there is something in there if (format && *format) { // only if there is something in there
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
} else ClearScreenF(true, false, COLOR_STD_BG); } else ClearScreenF(true, false, COLOR_STD_BG);
@ -395,16 +395,16 @@ void ShowIconString(u16* icon, int w, int h, const char *format, ...)
static const u32 icon_offset = 10; static const u32 icon_offset = 10;
u32 str_width, str_height, tot_height; u32 str_width, str_height, tot_height;
u32 x_str, y_str, x_bmp, y_bmp; u32 x_str, y_str, x_bmp, y_bmp;
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
if (!format || !*format) return; // only if there is something in there if (!format || !*format) return; // only if there is something in there
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
str_width = GetDrawStringWidth(str); str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str); str_height = GetDrawStringHeight(str);
tot_height = h + icon_offset + str_height; tot_height = h + icon_offset + str_height;
@ -412,7 +412,7 @@ void ShowIconString(u16* icon, int w, int h, const char *format, ...)
y_str = (str_height >= SCREEN_HEIGHT) ? 0 : h + icon_offset + (SCREEN_HEIGHT - tot_height) / 2; y_str = (str_height >= SCREEN_HEIGHT) ? 0 : h + icon_offset + (SCREEN_HEIGHT - tot_height) / 2;
x_bmp = (w >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - w) / 2; x_bmp = (w >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - w) / 2;
y_bmp = (tot_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - tot_height) / 2; y_bmp = (tot_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - tot_height) / 2;
DrawBitmap(MAIN_SCREEN, x_bmp, y_bmp, w, h, icon); DrawBitmap(MAIN_SCREEN, x_bmp, y_bmp, w, h, icon);
DrawStringF(MAIN_SCREEN, x_str, y_str, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x_str, y_str, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
} }
@ -421,7 +421,7 @@ bool ShowPrompt(bool ask, const char *format, ...)
{ {
bool ret = true; bool ret = true;
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
@ -458,38 +458,37 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
u32 color_off = COLOR_GREY; u32 color_off = COLOR_GREY;
u32 color_on = seqcolors[seqlvl]; u32 color_on = seqcolors[seqlvl];
u32 lvl = 0; u32 lvl = 0;
u32 str_width, str_height; u32 str_width, str_height;
u32 x, y; u32 x, y;
char str[STRBUF_SIZE];
char str[STRBUF_SIZE] = { 0 };
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
str_width = GetDrawStringWidth(str); str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str) + (4*line_height); str_height = GetDrawStringHeight(str) + (4*line_height);
if (str_width < 24 * font_width) str_width = 24 * font_width; if (str_width < 24 * font_width) str_width = 24 * font_width;
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2; x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2; y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
if (seqlvl >= 6) { // special handling if (seqlvl >= 6) { // special handling
color_bg = seqcolors[seqlvl]; color_bg = seqcolors[seqlvl];
color_font = COLOR_BLACK; color_font = COLOR_BLACK;
color_off = COLOR_BLACK; color_off = COLOR_BLACK;
color_on = COLOR_DARKGREY; color_on = COLOR_DARKGREY;
} }
ClearScreenF(true, false, color_bg); ClearScreenF(true, false, color_bg);
DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str); DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str);
#ifndef TIMER_UNLOCK #ifndef TIMER_UNLOCK
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, enter this:"); DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, enter this:");
// generate sequence // generate sequence
const char dpad_symbols[] = { '\x1A', '\x1B', '\x18', '\x19' }; // R L U D const char dpad_symbols[] = { '\x1A', '\x1B', '\x18', '\x19' }; // R L U D
u32 sequence[seqlen_max]; u32 sequence[seqlen_max];
char seqsymbols[seqlen_max]; char seqsymbols[seqlen_max];
u32 lastlsh = (u32) -1; u32 lastlsh = (u32) -1;
@ -502,8 +501,8 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
} }
sequence[seqlen-1] = BUTTON_A; sequence[seqlen-1] = BUTTON_A;
seqsymbols[seqlen-1] = 'A'; seqsymbols[seqlen-1] = 'A';
while (true) { while (true) {
for (u32 n = 0; n < seqlen; n++) { for (u32 n = 0; n < seqlen; n++) {
DrawStringF(MAIN_SCREEN, x + (n*4*FONT_WIDTH_EXT), y + str_height - 28 + line_height, DrawStringF(MAIN_SCREEN, x + (n*4*FONT_WIDTH_EXT), y + str_height - 28 + line_height,
@ -523,7 +522,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
} }
#else #else
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, hold <X>:"); DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, hold <X>:");
while (!CheckButton(BUTTON_B)) { while (!CheckButton(BUTTON_B)) {
for (u32 n = 0; n < seqlen; n++) { for (u32 n = 0; n < seqlen; n++) {
DrawStringF(MAIN_SCREEN, x + (n*4*FONT_WIDTH_EXT), y + str_height - 18, DrawStringF(MAIN_SCREEN, x + (n*4*FONT_WIDTH_EXT), y + str_height - 18,
@ -531,21 +530,20 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
} }
if (lvl == seqlen) if (lvl == seqlen)
break; break;
u32 prev_lvl = lvl; u32 prev_lvl = lvl;
while (lvl == prev_lvl) { while (lvl == prev_lvl) {
u64 timer = timer_start(); u64 timer = timer_start();
while ((lvl != 0) && CheckButton(BUTTON_X) && (timer_msec(timer) < 400)); while ((lvl != 0) && CheckButton(BUTTON_X) && (timer_msec(timer) < 400));
if (CheckButton(BUTTON_B)) break; if (CheckButton(BUTTON_B)) break;
else if (CheckButton(BUTTON_X)) lvl++; else if (CheckButton(BUTTON_X)) lvl++;
else lvl = 0; else lvl = 0;
} }
} }
#endif #endif
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
return (lvl >= seqlen); return (lvl >= seqlen);
} }
#endif #endif
@ -554,16 +552,16 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
u32 str_width, str_height; u32 str_width, str_height;
u32 x, y, yopt; u32 x, y, yopt;
u32 sel = 0; u32 sel = 0;
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
if (n == 0) return 0; // check for low number of options if (n == 0) return 0; // check for low number of options
// else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0; // else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0;
str_width = GetDrawStringWidth(str); str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str) + (n * (line_height + 2)) + (3 * line_height); str_height = GetDrawStringHeight(str) + (n * (line_height + 2)) + (3 * line_height);
if (str_width < 24 * font_width) str_width = 24 * font_width; if (str_width < 24 * font_width) str_width = 24 * font_width;
@ -572,7 +570,7 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2; x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2; y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
yopt = y + GetDrawStringHeight(str) + 8; yopt = y + GetDrawStringHeight(str) + 8;
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
DrawStringF(MAIN_SCREEN, x, yopt + (n*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)"); DrawStringF(MAIN_SCREEN, x, yopt + (n*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)");
@ -590,9 +588,9 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
break; break;
} }
} }
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
return (sel >= n) ? 0 : sel + 1; return (sel >= n) ? 0 : sel + 1;
} }
@ -602,22 +600,22 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40; const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40;
int sel = 0, scroll = 0; int sel = 0, scroll = 0;
u32 n_show = min(n, 10); u32 n_show = min(n, 10);
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
if (n == 0) return 0; // check for low number of options if (n == 0) return 0; // check for low number of options
// else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0; // else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0;
str_height = GetDrawStringHeight(str) + (n_show * (line_height + 2)) + (4 * line_height); str_height = GetDrawStringHeight(str) + (n_show * (line_height + 2)) + (4 * line_height);
x = (SCREEN_WIDTH_MAIN - item_width) / 2; x = (SCREEN_WIDTH_MAIN - item_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2; y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
yopt = y + GetDrawStringHeight(str) + 8; yopt = y + GetDrawStringHeight(str) + 8;
fname_len = min(64, item_width / FONT_WIDTH_EXT - 14); fname_len = min(64, item_width / FONT_WIDTH_EXT - 14);
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)"); DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)");
@ -625,16 +623,16 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
for (u32 i = scroll; i < scroll+n_show; i++) { for (u32 i = scroll; i < scroll+n_show; i++) {
char bytestr[16]; char bytestr[16];
FormatBytes(bytestr, options[i]->size); FormatBytes(bytestr, options[i]->size);
char content_str[64 + 1]; char content_str[64 + 1];
char temp_str[256]; char temp_str[256];
strncpy(temp_str, options[i]->name, 255); strncpy(temp_str, options[i]->name, 255);
char* dot = strrchr(temp_str, '.'); char* dot = strrchr(temp_str, '.');
if (hide_ext && dot) *dot = '\0'; if (hide_ext && dot) *dot = '\0';
ResizeString(content_str, temp_str, fname_len, 8, false); ResizeString(content_str, temp_str, fname_len, 8, false);
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)), DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)),
(sel == (int)i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s", (sel == (int)i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s",
(sel == (int)i) ? "->" : "", content_str); (sel == (int)i) ? "->" : "", content_str);
@ -658,12 +656,12 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
u32 bar_height = (n_show * flist_height) / n; u32 bar_height = (n_show * flist_height) / n;
if (bar_height < bar_height_min) bar_height = bar_height_min; if (bar_height < bar_height_min) bar_height = bar_height_min;
const u32 bar_y = ((u64) scroll * (flist_height - bar_height)) / (n - n_show) + yopt; const u32 bar_y = ((u64) scroll * (flist_height - bar_height)) / (n - n_show) + yopt;
DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, (bar_y - yopt), COLOR_STD_BG); DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, (bar_y - yopt), COLOR_STD_BG);
DrawRectangle(MAIN_SCREEN, bar_x, bar_y + bar_height, bar_width, SCREEN_HEIGHT - (bar_y + bar_height), COLOR_STD_BG); DrawRectangle(MAIN_SCREEN, bar_x, bar_y + bar_height, bar_width, SCREEN_HEIGHT - (bar_y + bar_height), COLOR_STD_BG);
DrawRectangle(MAIN_SCREEN, bar_x, bar_y, bar_width, bar_height, COLOR_SIDE_BAR); DrawRectangle(MAIN_SCREEN, bar_x, bar_y, bar_width, bar_height, COLOR_SIDE_BAR);
} else DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, flist_height, COLOR_STD_BG); } else DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, flist_height, COLOR_STD_BG);
u32 pad_state = InputWait(0); u32 pad_state = InputWait(0);
if (pad_state & BUTTON_DOWN) sel++; if (pad_state & BUTTON_DOWN) sel++;
else if (pad_state & BUTTON_UP) sel--; else if (pad_state & BUTTON_UP) sel--;
@ -680,20 +678,20 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
else if (sel == (int) n-1 && sel >= (int)(scroll + n_show - 1)) scroll = sel - n_show + 1; else if (sel == (int) n-1 && sel >= (int)(scroll + n_show - 1)) scroll = sel - n_show + 1;
else if (sel >= (int)(scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1; else if (sel >= (int)(scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1;
} }
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
return (sel >= (int)n) ? 0 : sel + 1; return (sel >= (int)n) ? 0 : sel + 1;
} }
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...) { u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...) {
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
char* ptr = str; char* ptr = str;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
ptr += vsnprintf(ptr, STRBUF_SIZE, format, va); ptr += vsnprintf(ptr, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n "); ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n ");
for (u32 i = 0; i < n; i++) { for (u32 i = 0; i < n; i++) {
char buttonstr[16]; char buttonstr[16];
@ -725,20 +723,20 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
const u32 alphabet_size = strnlen(alphabet, 256); const u32 alphabet_size = strnlen(alphabet, 256);
const u32 input_shown = 22; const u32 input_shown = 22;
const u32 fast_scroll = 4; const u32 fast_scroll = 4;
u32 str_width, str_height; u32 str_width, str_height;
u32 x, y; u32 x, y;
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
// check / fix up the input string if required // check / fix up the input string if required
if (max_size < 2) return false; // catching this, too if (max_size < 2) return false; // catching this, too
if ((*inputstr == '\0') || (resize && (strnlen(inputstr, max_size - 1) % resize))) { if ((*inputstr == '\0') || (resize && (strnlen(inputstr, max_size - 1) % resize))) {
memset(inputstr, alphabet[0], resize); // set the string if it is not set or invalid memset(inputstr, alphabet[0], resize); // set the string if it is not set or invalid
inputstr[resize] = '\0'; inputstr[resize] = '\0';
} }
str_width = GetDrawStringWidth(str); str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str) + 88; str_height = GetDrawStringHeight(str) + 88;
if (str_width < (24 * font_width)) str_width = 24 * font_width; if (str_width < (24 * font_width)) str_width = 24 * font_width;
@ -749,13 +747,13 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
DrawStringF(MAIN_SCREEN, x + 8, y + str_height - 40, COLOR_STD_FONT, COLOR_STD_BG, DrawStringF(MAIN_SCREEN, x + 8, y + str_height - 40, COLOR_STD_FONT, COLOR_STD_BG,
"R - (\x18\x19) fast scroll\nL - clear data%s", resize ? "\nX - remove char\nY - insert char" : ""); "R - (\x18\x19) fast scroll\nL - clear data%s", resize ? "\nX - remove char\nY - insert char" : "");
int cursor_a = -1; int cursor_a = -1;
u32 cursor_s = 0; u32 cursor_s = 0;
u32 scroll = 0; u32 scroll = 0;
bool aprv = false; bool aprv = false;
bool ret = false; bool ret = false;
while (true) { while (true) {
u32 inputstr_size = strnlen(inputstr, max_size - 1); u32 inputstr_size = strnlen(inputstr, max_size - 1);
if (cursor_s < scroll) scroll = cursor_s; if (cursor_s < scroll) scroll = cursor_s;
@ -780,7 +778,7 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
if (cursor_a < 0) { if (cursor_a < 0) {
for (cursor_a = alphabet_size - 1; (cursor_a > 0) && (alphabet[cursor_a] != inputstr[cursor_s]); cursor_a--); for (cursor_a = alphabet_size - 1; (cursor_a > 0) && (alphabet[cursor_a] != inputstr[cursor_s]); cursor_a--);
} }
// alphabet preview // alphabet preview
if (alphabet_size > (SCREEN_WIDTH(MAIN_SCREEN) / font_width)) { if (alphabet_size > (SCREEN_WIDTH(MAIN_SCREEN) / font_width)) {
const u32 aprv_y = y + str_height - 60; const u32 aprv_y = y + str_height - 60;
@ -799,7 +797,7 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
} }
} else DrawRectangle(MAIN_SCREEN, 0, aprv_y, SCREEN_WIDTH(MAIN_SCREEN), font_height, COLOR_STD_BG); } else DrawRectangle(MAIN_SCREEN, 0, aprv_y, SCREEN_WIDTH(MAIN_SCREEN), font_height, COLOR_STD_BG);
} }
u32 pad_state = InputWait(3); u32 pad_state = InputWait(3);
aprv = (pad_state & (BUTTON_UP|BUTTON_DOWN|BUTTON_R1)) && !(pad_state & (BUTTON_RIGHT|BUTTON_LEFT)); aprv = (pad_state & (BUTTON_UP|BUTTON_DOWN|BUTTON_R1)) && !(pad_state & (BUTTON_RIGHT|BUTTON_LEFT));
if (pad_state & BUTTON_A) { if (pad_state & BUTTON_A) {
@ -859,9 +857,9 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
// remove any trailing spaces // remove any trailing spaces
for (char* cc = inputstr + strnlen(inputstr, max_size) - 1; for (char* cc = inputstr + strnlen(inputstr, max_size) - 1;
(*cc == ' ') && (cc > inputstr); *(cc--) = '\0'); (*cc == ' ') && (cc > inputstr); *(cc--) = '\0');
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
return ret; return ret;
} }
@ -869,17 +867,17 @@ bool ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...) {
const char* alphabet = " aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ(){}[]'`^,~*?!@#$%&0123456789=+-_."; const char* alphabet = " aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ(){}[]'`^,~*?!@#$%&0123456789=+-_.";
bool ret = false; bool ret = false;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
ret = ShowInputPrompt(inputstr, max_size, 1, alphabet, format, va); ret = ShowInputPrompt(inputstr, max_size, 1, alphabet, format, va);
va_end(va); va_end(va);
return ret; return ret;
} }
u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...) { u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...) {
const char* alphabet = "0123456789ABCDEF"; const char* alphabet = "0123456789ABCDEF";
char inputstr[16 + 1] = { 0 }; char inputstr[16 + 1];
u64 ret = 0; u64 ret = 0;
va_list va; va_list va;
@ -897,36 +895,36 @@ u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...) {
u64 ShowNumberPrompt(u64 start_val, const char *format, ...) { u64 ShowNumberPrompt(u64 start_val, const char *format, ...) {
const char* alphabet = "0123456789"; const char* alphabet = "0123456789";
char inputstr[20 + 1] = { 0 }; char inputstr[20 + 1];
u64 ret = 0; u64 ret = 0;
va_list va; va_list va;
snprintf(inputstr, 20 + 1, "%llu", start_val); snprintf(inputstr, 20 + 1, "%llu", start_val);
va_start(va, format); va_start(va, format);
if (ShowInputPrompt(inputstr, 20 + 1, 1, alphabet, format, va)) { if (ShowInputPrompt(inputstr, 20 + 1, 1, alphabet, format, va)) {
sscanf(inputstr, "%llu", &ret); sscanf(inputstr, "%llu", &ret);
} else ret = (u64) -1; } else ret = (u64) -1;
va_end(va); va_end(va);
return ret; return ret;
} }
bool ShowDataPrompt(u8* data, u32* size, const char *format, ...) { bool ShowDataPrompt(u8* data, u32* size, const char *format, ...) {
const char* alphabet = "0123456789ABCDEF"; const char* alphabet = "0123456789ABCDEF";
char inputstr[128 + 1] = { 0 }; // maximum size of data: 64 byte char inputstr[128 + 1]; // maximum size of data: 64 byte
bool ret = false; bool ret = false;
va_list va; va_list va;
if (*size > 64) *size = 64; if (*size > 64) *size = 64;
for (u32 i = 0; i < *size; i++) for (u32 i = 0; i < *size; i++)
snprintf(inputstr + (2*i), 128 + 1 - (2*i), "%02X", (unsigned int) data[i]); snprintf(inputstr + (2*i), 128 + 1 - (2*i), "%02X", (unsigned int) data[i]);
va_start(va, format); va_start(va, format);
if (ShowInputPrompt(inputstr, 128 + 1, 2, alphabet, format, va)) { if (ShowInputPrompt(inputstr, 128 + 1, 2, alphabet, format, va)) {
*size = strnlen(inputstr, 128) / 2; *size = strnlen(inputstr, 128) / 2;
for (u32 i = 0; i < *size; i++) { for (u32 i = 0; i < *size; i++) {
char bytestr[2 + 1] = { 0 }; char bytestr[2 + 1];
unsigned int byte; unsigned int byte;
strncpy(bytestr, inputstr + (2*i), 2); strncpy(bytestr, inputstr + (2*i), 2);
sscanf(bytestr, "%02X", &byte); sscanf(bytestr, "%02X", &byte);
@ -935,7 +933,7 @@ bool ShowDataPrompt(u8* data, u32* size, const char *format, ...) {
ret = true; ret = true;
} }
va_end(va); va_end(va);
return ret; return ret;
} }
@ -944,29 +942,29 @@ bool ShowRtcSetterPrompt(void* time, const char *format, ...) {
DsTime* dstime = (DsTime*) time; DsTime* dstime = (DsTime*) time;
u32 str_width, str_height; u32 str_width, str_height;
u32 x, y; u32 x, y;
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
// check the initial time // check the initial time
if (!is_valid_dstime(dstime)) { if (!is_valid_dstime(dstime)) {
dstime->bcd_h = dstime->bcd_m = dstime->bcd_s = 0x00; dstime->bcd_h = dstime->bcd_m = dstime->bcd_s = 0x00;
dstime->bcd_D = dstime->bcd_M = 0x01; dstime->bcd_D = dstime->bcd_M = 0x01;
dstime->bcd_Y = 0x00; dstime->bcd_Y = 0x00;
} }
str_width = GetDrawStringWidth(str); str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str) + (4*line_height); str_height = GetDrawStringHeight(str) + (4*line_height);
if (str_width < (19 * font_width)) str_width = 19 * font_width; if (str_width < (19 * font_width)) str_width = 19 * font_width;
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2; x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2; y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str); DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
int cursor = 0; int cursor = 0;
bool ret = false; bool ret = false;
while (true) { while (true) {
@ -980,7 +978,7 @@ bool ShowRtcSetterPrompt(void* time, const char *format, ...) {
DrawStringF(MAIN_SCREEN, x, y + str_height - 18, COLOR_STD_FONT, COLOR_STD_BG, "20%02lX-%02lX-%02lX %02lX:%02lX:%02lX\n %-*.*s^^%-*.*s", DrawStringF(MAIN_SCREEN, x, y + str_height - 18, COLOR_STD_FONT, COLOR_STD_BG, "20%02lX-%02lX-%02lX %02lX:%02lX:%02lX\n %-*.*s^^%-*.*s",
(u32) dstime->bcd_Y, (u32) dstime->bcd_M, (u32) dstime->bcd_D, (u32) dstime->bcd_h, (u32) dstime->bcd_m, (u32) dstime->bcd_s, (u32) dstime->bcd_Y, (u32) dstime->bcd_M, (u32) dstime->bcd_D, (u32) dstime->bcd_h, (u32) dstime->bcd_m, (u32) dstime->bcd_s,
cursor * 3, cursor * 3, "", 17 - 2 - (cursor * 3), 17 - 2 - (cursor * 3), ""); cursor * 3, cursor * 3, "", 17 - 2 - (cursor * 3), 17 - 2 - (cursor * 3), "");
// user input // user input
u32 pad_state = InputWait(0); u32 pad_state = InputWait(0);
if ((pad_state & BUTTON_A) && is_valid_dstime(dstime)) { if ((pad_state & BUTTON_A) && is_valid_dstime(dstime)) {
@ -999,13 +997,12 @@ bool ShowRtcSetterPrompt(void* time, const char *format, ...) {
} else if (pad_state & BUTTON_RIGHT) { } else if (pad_state & BUTTON_RIGHT) {
if (++cursor > 5) cursor = 0; if (++cursor > 5) cursor = 0;
} }
// update bcd // update bcd
*bcd = NUM2BCD(val); *bcd = NUM2BCD(val);
} }
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
return ret; return ret;
} }
@ -1022,7 +1019,7 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0; u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0;
char tempstr[64]; char tempstr[64];
char progstr[64]; char progstr[64];
static u64 last_msec_elapsed = 0; static u64 last_msec_elapsed = 0;
static u64 last_sec_remain = 0; static u64 last_sec_remain = 0;
if (!current) { if (!current) {
@ -1035,7 +1032,7 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
u64 sec_remain = (!last_sec_remain) ? (sec_total - sec_elapsed) : ((last_sec_remain + (sec_total - sec_elapsed) + 1) / 2); u64 sec_remain = (!last_sec_remain) ? (sec_total - sec_elapsed) : ((last_sec_remain + (sec_total - sec_elapsed) + 1) / 2);
if (sec_remain >= 60 * 60) sec_remain = 60 * 60 - 1; if (sec_remain >= 60 * 60) sec_remain = 60 * 60 - 1;
last_sec_remain = sec_remain; last_sec_remain = sec_remain;
if (!current || last_prog_width > prog_width) { if (!current || last_prog_width > prog_width) {
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawRectangle(MAIN_SCREEN, bar_pos_x, bar_pos_y, bar_width, bar_height, COLOR_STD_FONT); DrawRectangle(MAIN_SCREEN, bar_pos_x, bar_pos_y, bar_width, bar_height, COLOR_STD_FONT);
@ -1043,7 +1040,7 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
} }
DrawRectangle(MAIN_SCREEN, bar_pos_x + 2, bar_pos_y + 2, prog_width, bar_height - 4, COLOR_STD_FONT); DrawRectangle(MAIN_SCREEN, bar_pos_x + 2, bar_pos_y + 2, prog_width, bar_height - 4, COLOR_STD_FONT);
DrawRectangle(MAIN_SCREEN, bar_pos_x + 2 + prog_width, bar_pos_y + 2, (bar_width-4) - prog_width, bar_height - 4, COLOR_STD_BG); DrawRectangle(MAIN_SCREEN, bar_pos_x + 2 + prog_width, bar_pos_y + 2, (bar_width-4) - prog_width, bar_height - 4, COLOR_STD_BG);
TruncateString(progstr, opstr, min(63, (bar_width / FONT_WIDTH_EXT) - 7), 8); TruncateString(progstr, opstr, min(63, (bar_width / FONT_WIDTH_EXT) - 7), 8);
snprintf(tempstr, 64, "%s (%lu%%)", progstr, prog_percent); snprintf(tempstr, 64, "%s (%lu%%)", progstr, prog_percent);
ResizeString(progstr, tempstr, bar_width / FONT_WIDTH_EXT, 8, false); ResizeString(progstr, tempstr, bar_width / FONT_WIDTH_EXT, 8, false);
@ -1055,7 +1052,7 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
bar_pos_y - line_height - 1, COLOR_STD_FONT, COLOR_STD_BG, true); bar_pos_y - line_height - 1, COLOR_STD_FONT, COLOR_STD_BG, true);
} }
DrawString(MAIN_SCREEN, "(hold B to cancel)", bar_pos_x + 2, text_pos_y + 14, COLOR_STD_FONT, COLOR_STD_BG, false); DrawString(MAIN_SCREEN, "(hold B to cancel)", bar_pos_x + 2, text_pos_y + 14, COLOR_STD_FONT, COLOR_STD_BG, false);
last_prog_width = prog_width; last_prog_width = prog_width;
return !CheckButton(BUTTON_B); return !CheckButton(BUTTON_B);