From 42c8e7e1752be542295b2c80829d76558c823740 Mon Sep 17 00:00:00 2001 From: cpasjuste Date: Fri, 16 Mar 2018 19:19:48 +0100 Subject: [PATCH] SWITCH: bitmask joystick buttons to fit HidControllerKeys SWITCH: improve and comment test --- src/joystick/switch/SDL_sysjoystick.c | 3 +- test/testswitch.c | 41 ++++++++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/joystick/switch/SDL_sysjoystick.c b/src/joystick/switch/SDL_sysjoystick.c index 5bcf264db..d2b9303a1 100644 --- a/src/joystick/switch/SDL_sysjoystick.c +++ b/src/joystick/switch/SDL_sysjoystick.c @@ -181,7 +181,8 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) for (int i = 0; i < sizeof(pad_mapping) / sizeof(*pad_mapping); i++) { if (changed & pad_mapping[i]) { SDL_PrivateJoystickButton( - joystick, (Uint8) i, (Uint8) ((pad[index].buttons & pad_mapping[i]) ? SDL_PRESSED : SDL_RELEASED)); + joystick, (Uint8) BIT(i), + (Uint8) ((pad[index].buttons & pad_mapping[i]) ? SDL_PRESSED : SDL_RELEASED)); } } } diff --git a/test/testswitch.c b/test/testswitch.c index 18e38d099..885ec7c19 100644 --- a/test/testswitch.c +++ b/test/testswitch.c @@ -19,30 +19,44 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 -SDL_Renderer *renderer = NULL; - -int done = 0; - int main(int argc, char *argv[]) { + SDL_Event event; + SDL_Window *window; + SDL_Renderer *renderer; + int done = 0; + + // redirect stdout to emulators consoleDebugInit(debugDevice_SVC); stdout = stderr; - SDL_Window *window; - - if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) { - printf("SDL_CreateWindowAndRenderer: %s\n", SDL_GetError()); + // mandatory at least on switch, else gfx is not properly closed + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { + printf("SDL_Init: %s\n", SDL_GetError()); return -1; } - SDL_Event event; + // switch only support software renderer for now + if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) { + printf("SDL_CreateWindowAndRenderer: %s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } - printf("entering main loop\n"); + // open CONTROLLER_P1_AUTO + // https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L45 + if (SDL_JoystickOpen(0) == NULL) { + printf("SDL_JoystickOpen: %s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } while (!done) { while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) { + // seek for (B) button press + // https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L51 + if (event.type == SDL_JOYBUTTONDOWN && event.jbutton.button == KEY_B) { done = 1; } } @@ -50,14 +64,17 @@ int main(int argc, char *argv[]) SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); + // R SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_Rect r = {0, 0, 64, 64}; SDL_RenderFillRect(renderer, &r); + // G SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); SDL_Rect g = {64, 0, 64, 64}; SDL_RenderFillRect(renderer, &g); + // B SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); SDL_Rect b = {128, 0, 64, 64}; SDL_RenderFillRect(renderer, &b); @@ -65,5 +82,7 @@ int main(int argc, char *argv[]) SDL_RenderPresent(renderer); } + SDL_Quit(); + return 0; }