From 9b03a0c87aaed26f71302c45979bc30764564f2b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 8 Aug 2023 20:27:50 +1000 Subject: [PATCH] More broken Xbox stuff --- src/Platform_Xbox.c | 2 + src/Window_Xbox.c | 104 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/Platform_Xbox.c b/src/Platform_Xbox.c index 53c79f58c..99c4051d2 100644 --- a/src/Platform_Xbox.c +++ b/src/Platform_Xbox.c @@ -13,6 +13,7 @@ #include #include #include +#include static HANDLE heap; const cc_result ReturnCode_FileShareViolation = ERROR_SHARING_VIOLATION; @@ -520,6 +521,7 @@ cc_bool DynamicLib_DescribeError(cc_string* dst) { void Platform_Init(void) { Platform_SingleProcess = true; Platform_InitStopwatch(); + nxNetInit(NULL); } void Platform_Free(void) { diff --git a/src/Window_Xbox.c b/src/Window_Xbox.c index ffd0052ed..d8957d7cc 100644 --- a/src/Window_Xbox.c +++ b/src/Window_Xbox.c @@ -11,8 +11,12 @@ #include "Errors.h" #include "ExtMath.h" #include +#include +#include static cc_bool launcherMode; +static xid_dev_t* xid_ctrl; +static xid_gamepad_in gp_state; struct _DisplayData DisplayInfo; struct _WinData WindowInfo; @@ -20,6 +24,41 @@ struct _WinData WindowInfo; int Display_ScaleX(int x) { return x; } int Display_ScaleY(int y) { return y; } +// TODO No idea if this even works +static void OnDataReceived(UTR_T* utr) { + xid_dev_t* xid_dev = (xid_dev_t*)utr->context; + + if (utr->status < 0 || !xid_ctrl || xid_dev != xid_ctrl) return; + + int len = min(utr->xfer_len, sizeof(gp_state)); + Mem_Copy(&gp_state, utr->buff, len); + int mods = gp_state.dButtons; + Platform_Log1("MODS: %i", &mods); + + // queue USB transfer again + utr->xfer_len = 0; + utr->bIsTransferDone = 0; + usbh_int_xfer(utr); +} + +static void OnDeviceChanged(xid_dev_t *xid_dev__, int status__) { + xid_dev_t* xid_dev = usbh_xid_get_device_list(); + + Platform_LogConst("SCANNING DEVICES.."); + while (xid_dev) + { + int DEV = xid_dev->xid_desc.bType; + Platform_Log1("DEV: %i", &DEV); + if (xid_dev->xid_desc.bType != XID_TYPE_GAMECONTROLLER) + continue; + + xid_ctrl = xid_dev; + usbh_xid_read(xid_ctrl, 0, OnDataReceived); + return; + } + xid_ctrl = NULL; +} + void Window_Init(void) { XVideoSetMode(640, 480, 32, REFRESH_DEFAULT); // TODO not call VIDEO_MODE mode = XVideoGetMode(); @@ -34,6 +73,12 @@ void Window_Init(void) { WindowInfo.Height = mode.height; WindowInfo.Focused = true; WindowInfo.Exists = true; + + usbh_core_init(); + usbh_xid_init(); + // TODO will this produce wrong stuff when device disc/conn + usbh_install_xid_conn_callback(OnDeviceChanged, OnDeviceChanged); + OnDeviceChanged(NULL, 0); } void Window_Create2D(int width, int height) { launcherMode = true; } @@ -59,8 +104,61 @@ void Window_Close(void) { /*########################################################################################################################* *----------------------------------------------------Input processing-----------------------------------------------------* *#########################################################################################################################*/ -void Window_ProcessEvents(double delta) { +// https://docs.microsoft.com/en-us/windows/win32/api/xinput/ns-xinput-xinput_gamepad +#define XINPUT_GAMEPAD_DPAD_UP 0x0001 +#define XINPUT_GAMEPAD_DPAD_DOWN 0x0002 +#define XINPUT_GAMEPAD_DPAD_LEFT 0x0004 +#define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008 +#define XINPUT_GAMEPAD_START 0x0010 +#define XINPUT_GAMEPAD_BACK 0x0020 +#define XINPUT_GAMEPAD_LEFT_THUMB 0x0040 +#define XINPUT_GAMEPAD_RIGHT_THUMB 0x0080 +#define XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100 +#define XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200 +#define XINPUT_GAMEPAD_A 0x1000 +#define XINPUT_GAMEPAD_B 0x2000 +#define XINPUT_GAMEPAD_X 0x4000 +#define XINPUT_GAMEPAD_Y 0x8000 + +static void HandleButtons_Game(int mods) { + Input_SetNonRepeatable(CCPAD_L, mods & XINPUT_GAMEPAD_LEFT_THUMB); + Input_SetNonRepeatable(CCPAD_R, mods & XINPUT_GAMEPAD_RIGHT_THUMB); + Input_SetNonRepeatable(CCPAD_A, mods & XINPUT_GAMEPAD_A); + Input_SetNonRepeatable(CCPAD_B, mods & XINPUT_GAMEPAD_B); + Input_SetNonRepeatable(CCPAD_X, mods & XINPUT_GAMEPAD_X); + Input_SetNonRepeatable(CCPAD_Y, mods & XINPUT_GAMEPAD_Y); + + Input_SetNonRepeatable(CCPAD_START, mods & XINPUT_GAMEPAD_START); + Input_SetNonRepeatable(CCPAD_SELECT, mods & XINPUT_GAMEPAD_BACK); + + Input_SetNonRepeatable(CCPAD_LEFT, mods & XINPUT_GAMEPAD_DPAD_LEFT); + Input_SetNonRepeatable(CCPAD_RIGHT, mods & XINPUT_GAMEPAD_DPAD_RIGHT); + Input_SetNonRepeatable(CCPAD_UP, mods & XINPUT_GAMEPAD_DPAD_UP); + Input_SetNonRepeatable(CCPAD_DOWN, mods & XINPUT_GAMEPAD_DPAD_DOWN); +} + +static void HandleButtons_Launcher(int mods) { + Input_SetNonRepeatable(CCKEY_ENTER, mods & XINPUT_GAMEPAD_A); + Input_SetNonRepeatable(CCKEY_ESCAPE, mods & XINPUT_GAMEPAD_B); + // fake tab with down for Launcher + //Input_SetNonRepeatable(CCKEY_TAB, mods & KEY_DDOWN); + + Input_SetNonRepeatable(CCPAD_LEFT, mods & XINPUT_GAMEPAD_DPAD_LEFT); + Input_SetNonRepeatable(CCPAD_RIGHT, mods & XINPUT_GAMEPAD_DPAD_RIGHT); + Input_SetNonRepeatable(CCPAD_UP, mods & XINPUT_GAMEPAD_DPAD_UP); + Input_SetNonRepeatable(CCPAD_DOWN, mods & XINPUT_GAMEPAD_DPAD_DOWN); +} + +void Window_ProcessEvents(double delta) { + if (!xid_ctrl) return; + int mods = gp_state.dButtons; + + if (launcherMode) { + HandleButtons_Launcher(mods); + } else { + HandleButtons_Game(mods); + } } void Cursor_SetPosition(int x, int y) { } // Makes no sense for 3DS @@ -86,7 +184,8 @@ void Window_DrawFramebuffer(Rect2D r) { cc_uint32* src = (cc_uint32*)fb_bmp.scan0 + r.X; cc_uint32* dst = (cc_uint32*)fb + r.X; - for (int y = r.Y; y < r.Y + r.Height; y++) { + for (int y = r.Y; y < r.Y + r.Height; y++) + { Mem_Copy(dst + y * fb_bmp.width, src + y * fb_bmp.width, r.Width * 4); } } @@ -118,7 +217,6 @@ cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) { } cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { - return ERR_NOT_SUPPORTED; } #endif