From 2cced5bdac9077147eacb0044f9ce2a16bfffc0b Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Sat, 2 May 2020 01:41:43 +0200 Subject: [PATCH] Ajust some gfx functions for joycon compatibility --- source/hid/hid.c | 44 +++++++++++++++++++++++++---- source/hid/hid.h | 6 +++- source/tegraexplorer/fs/filemenu.c | 23 ++++++++------- source/tegraexplorer/gfx/gfxutils.c | 22 +++++++-------- source/tegraexplorer/gfx/gfxutils.h | 4 +-- source/tegraexplorer/gfx/menu.c | 2 +- source/tegraexplorer/mainmenu.c | 8 +++--- 7 files changed, 73 insertions(+), 36 deletions(-) diff --git a/source/hid/hid.c b/source/hid/hid.c index f2df35e..c600fc3 100644 --- a/source/hid/hid.c +++ b/source/hid/hid.c @@ -1,6 +1,8 @@ #include "hid.h" #include "joycon.h" #include "../utils/btn.h" +#include "../gfx/gfx.h" +#include "../utils/types.h" static Inputs inputs = {0}; u16 LbaseX = 0, LbaseY = 0, RbaseX = 0, RbaseY = 0; @@ -11,6 +13,12 @@ void hidInit(){ Inputs *hidRead(){ jc_gamepad_rpt_t *controller = joycon_poll(); + static bool errPrint = false; + + u8 btn = btn_read(); + inputs.volp = (btn & BTN_VOL_UP) ? 1 : 0; + inputs.volm = (btn & BTN_VOL_DOWN) ? 1 : 0; + inputs.pow = (btn & BTN_POWER) ? 1 : 0; inputs.a = controller->a; inputs.b = controller->b; @@ -25,6 +33,18 @@ Inputs *hidRead(){ inputs.home = controller->home; inputs.cap = controller->cap; + if (controller->conn_l && controller->conn_r){ + if (errPrint){ + gfx_boxGrey(1008, 703, 1279, 719, 0xFF); + errPrint = false; + } + } + else { + gfx_con_setpos(1008, 703); + gfx_printf("%k%K%s %s MISS%k%K", COLOR_DEFAULT, COLOR_WHITE, (controller->conn_l) ? " " : "JOYL", (controller->conn_r) ? " " : "JOYR", COLOR_WHITE, COLOR_DEFAULT); + errPrint = true; + } + if (controller->conn_l){ if ((LbaseX == 0 || LbaseY == 0) || controller->l3){ LbaseX = controller->lstick_x; @@ -36,6 +56,10 @@ Inputs *hidRead(){ inputs.Lleft = (controller->left || (controller->lstick_x < LbaseX - 500)) ? 1 : 0; inputs.Lright = (controller->right || (controller->lstick_x > LbaseX + 500)) ? 1 : 0; } + else { + inputs.Lup = inputs.volp; + inputs.Ldown = inputs.volm; + } if (controller->conn_r){ if ((RbaseX == 0 || RbaseY == 0) || controller->r3){ @@ -48,19 +72,27 @@ Inputs *hidRead(){ inputs.Rleft = (controller->rstick_x < RbaseX - 500) ? 1 : 0; inputs.Rright = (controller->rstick_x > RbaseX + 500) ? 1 : 0; } - - u8 btn = btn_read(); - inputs.volp = (btn & BTN_VOL_UP) ? 1 : 0; - inputs.volm = (btn & BTN_VOL_DOWN) ? 1 : 0; - inputs.pow = (btn & BTN_POWER) ? 1 : 0; + else + inputs.a = inputs.pow; return &inputs; } -Inputs *hidWaitForButton(u32 mask){ +Inputs *hidWaitMask(u32 mask){ Inputs *in = hidRead(); while ((in->buttons & mask) == 0){ in = hidRead(); } return in; +} + +Inputs *hidWait(){ + Inputs *in = hidRead(); + + while (in->buttons) + hidRead(); + + while (!(in->buttons)) + hidRead(); + return in; } \ No newline at end of file diff --git a/source/hid/hid.h b/source/hid/hid.h index c4d0f59..179bb1b 100644 --- a/source/hid/hid.h +++ b/source/hid/hid.h @@ -8,6 +8,9 @@ #define KEY_LDOWN BIT(17) #define KEY_RUP BIT(7) #define KEY_RDOWN BIT(6) +#define KEY_VOLP BIT(14) +#define KEY_VOLM BIT(15) +#define KEY_POW BIT(16) typedef struct _inputs { union { @@ -47,4 +50,5 @@ typedef struct _inputs { void hidInit(); Inputs *hidRead(); -Inputs *hidWaitForButton(u32 mask); \ No newline at end of file +Inputs *hidWait(); +Inputs *hidWaitMask(u32 mask); \ No newline at end of file diff --git a/source/tegraexplorer/fs/filemenu.c b/source/tegraexplorer/fs/filemenu.c index 3d35915..7649c6c 100644 --- a/source/tegraexplorer/fs/filemenu.c +++ b/source/tegraexplorer/fs/filemenu.c @@ -15,6 +15,7 @@ #include "../../utils/sprintf.h" #include "../script/parser.h" #include "../emmc/emmcoperations.h" +#include "../../hid/hid.h" extern char *currentpath; extern char *clipboard; @@ -36,23 +37,25 @@ int delfile(const char *path, const char *filename){ void viewbytes(char *path){ FIL in; - u8 print[2048]; + u8 print[1024]; u32 size; QWORD offset = 0; int res; + Inputs *input = hidRead(); + + while (input->buttons & (KEY_POW | KEY_B)); gfx_clearscreen(); + if ((res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING))){ gfx_errDisplay("viewbytes", res, 1); return; } - while (btn_read() & BTN_POWER); - while (1){ f_lseek(&in, offset * 16); - if ((res = f_read(&in, &print, 2048 * sizeof(u8), &size))){ + if ((res = f_read(&in, &print, 1024 * sizeof(u8), &size))){ gfx_errDisplay("viewbytes", res, 2); return; } @@ -60,16 +63,16 @@ void viewbytes(char *path){ gfx_con_setpos(0, 31); gfx_hexdump(offset * 16, print, size * sizeof(u8)); - res = btn_read(); + input = hidRead(); - if (!res) - res = btn_wait(); + if (!(input->buttons)) + input = hidWait(); - if (res & BTN_VOL_DOWN && 2048 * sizeof(u8) == size) + if (input->Ldown && 1024 * sizeof(u8) == size) offset++; - if (res & BTN_VOL_UP && offset > 0) + if (input->Lup && offset > 0) offset--; - if (res & BTN_POWER) + if (input->buttons & (KEY_POW | KEY_B)) break; } f_close(&in); diff --git a/source/tegraexplorer/gfx/gfxutils.c b/source/tegraexplorer/gfx/gfxutils.c index 6706ac7..ca30aee 100644 --- a/source/tegraexplorer/gfx/gfxutils.c +++ b/source/tegraexplorer/gfx/gfxutils.c @@ -8,6 +8,7 @@ #include "../../utils/util.h" #include "../../mem/heap.h" #include "../common/common.h" +#include "../../hid/hid.h" int printerrors = true; @@ -22,12 +23,12 @@ void gfx_clearscreen(){ gfx_boxGrey(0, 703, 1279, 719, 0xFF); gfx_boxGrey(0, 0, 1279, 15, 0xFF); gfx_con_setpos(0, 0); - gfx_printf("Tegraexplorer v1.5.2 | Battery: %3d%%", battery >> 8); + gfx_printf("Tegraexplorer v1.5.2 | Battery: %3d%%\n", battery >> 8); RESETCOLOR; } -int gfx_message(u32 color, const char* message, ...){ +u32 gfx_message(u32 color, const char* message, ...){ va_list ap; va_start(ap, message); @@ -37,10 +38,10 @@ int gfx_message(u32 color, const char* message, ...){ gfx_vprintf(message, ap); va_end(ap); - return btn_wait(); + return hidWait()->buttons; } -int gfx_errDisplay(char *src_func, int err, int loc){ +u32 gfx_errDisplay(char *src_func, int err, int loc){ if (!printerrors) return 0; @@ -61,27 +62,24 @@ int gfx_errDisplay(char *src_func, int err, int loc){ RESETCOLOR; - while (btn_read() != 0); - - return btn_wait(); + return hidWait()->buttons; } int gfx_makewaitmenu(char *hiddenmessage, int timer){ int res; u32 start = get_tmr_s(); - - while (btn_read() != 0); + Inputs *input = NULL; while(1){ - res = btn_read(); + input = hidRead(); - if (res & BTN_VOL_DOWN || res & BTN_VOL_UP) + if (input->buttons & (KEY_VOLM | KEY_VOLP | KEY_B)) return 0; if (start + timer > get_tmr_s()) gfx_printf("\r ", timer + start - get_tmr_s()); - else if (res & BTN_POWER) + else if (input->a) return 1; else diff --git a/source/tegraexplorer/gfx/gfxutils.h b/source/tegraexplorer/gfx/gfxutils.h index d360ca9..55f0642 100644 --- a/source/tegraexplorer/gfx/gfxutils.h +++ b/source/tegraexplorer/gfx/gfxutils.h @@ -7,8 +7,8 @@ #define RESETCOLOR gfx_printf("%k%K", COLOR_WHITE, COLOR_DEFAULT) void gfx_clearscreen(); -int gfx_message(u32 color, const char* message, ...); -int gfx_errDisplay(char *src_func, int err, int loc); +u32 gfx_message(u32 color, const char* message, ...); +u32 gfx_errDisplay(char *src_func, int err, int loc); int gfx_makewaitmenu(char *hiddenmessage, int timer); void gfx_printlength(int size, char *toprint); void gfx_printandclear(char *in, int length, int endX); diff --git a/source/tegraexplorer/gfx/menu.c b/source/tegraexplorer/gfx/menu.c index 9309105..70c9521 100644 --- a/source/tegraexplorer/gfx/menu.c +++ b/source/tegraexplorer/gfx/menu.c @@ -153,7 +153,7 @@ int menu_make(menu_entry *entries, int amount, char *toptext){ gfx_printf("Type: %s", (entries[currentpos].property & ISDIR) ? "Dir " : "File"); } else - gfx_boxGrey(800, 223, 1279, 271, 0x1B); + gfx_boxGrey(800, 144, 1279, 190, 0x1B); gfx_con_setpos(0, 703); SWAPCOLOR(COLOR_DEFAULT); diff --git a/source/tegraexplorer/mainmenu.c b/source/tegraexplorer/mainmenu.c index 3ff7a83..5a70324 100644 --- a/source/tegraexplorer/mainmenu.c +++ b/source/tegraexplorer/mainmenu.c @@ -37,8 +37,8 @@ void MainMenu_SDCard(){ void MainMenu_EMMC(){ gfx_clearscreen(); - gfx_printf("You're about to enter EMMC\nModifying anything here\n can result in a BRICK!\n\nPlease only continue\n if you know what you're doing\n\nPress Vol+/- to return\n"); - if (gfx_makewaitmenu("Press Power to enter", 4)){ + gfx_printf("You're about to enter EMMC\nModifying anything here\n can result in a BRICK!\n\nPlease only continue\n if you know what you're doing\n\nPress B to return\n"); + if (gfx_makewaitmenu("Press A to enter", 4)){ /* connect_mmc(SYSMMC); @@ -92,8 +92,8 @@ void MainMenu_SDFormat(){ if (res > 0){ gfx_clearscreen(); - gfx_printf("Are you sure you want to format your sd?\nThis will delete everything on your SD card\nThis action is irreversible!\n\nPress Vol+/- to cancel\n"); - if(gfx_makewaitmenu("Press Power to continue", 10)){ + gfx_printf("Are you sure you want to format your sd?\nThis will delete everything on your SD card\nThis action is irreversible!\n\nPress B to cancel\n"); + if(gfx_makewaitmenu("Press A to continue", 10)){ if (format(res)){ sd_unmount(); }