From 94b2c02e02f4b04db540711fef4509c5597b8536 Mon Sep 17 00:00:00 2001 From: cpasjuste Date: Tue, 22 Jun 2021 14:21:29 +0200 Subject: [PATCH] switch: add cmake support switch: update for latest sdl2 changes switch: update for latest libnx hid changes --- CMakeLists.switch | 86 --------------- CMakeLists.txt | 57 +++++++++- include/SDL_config.h.cmake | 7 ++ src/joystick/switch/SDL_sysjoystick.c | 152 +++++++++++++++----------- src/video/khronos/EGL/eglplatform.h | 2 +- src/video/switch/SDL_switchmouse.c | 97 ++++++++-------- src/video/switch/SDL_switchvideo.c | 21 ++-- 7 files changed, 207 insertions(+), 215 deletions(-) delete mode 100644 CMakeLists.switch diff --git a/CMakeLists.switch b/CMakeLists.switch deleted file mode 100644 index b6c3817d9..000000000 --- a/CMakeLists.switch +++ /dev/null @@ -1,86 +0,0 @@ -cmake_minimum_required(VERSION 3.0) -#set(CMAKE_VERBOSE_MAKEFILE ON) - -set(CMAKE_SYSTEM_NAME "Generic") - -set(DEVKITPRO $ENV{DEVKITPRO}) -set(CMAKE_SYSTEM_PROCESSOR "armv8-a") -set(CMAKE_C_COMPILER "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc") -set(CMAKE_CXX_COMPILER "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++") -set(CMAKE_ASM_COMPILER "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-as") -set(CMAKE_AR "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc-ar" CACHE STRING "") -set(CMAKE_RANLIB "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc-ranlib" CACHE STRING "") -set(CMAKE_C_FLAGS "-g -O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -ftls-model=local-exec -fPIC -I${DEVKITPRO}/libnx/include -I${DEVKITPRO}/portlibs/switch/include" CACHE STRING "C flags") -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fno-rtti -fno-exceptions" CACHE STRING "C++ flags") -set(CMAKE_FIND_ROOT_PATH ${DEVKITPRO} ${DEVKITPRO}/devkitA64 ${DEVKITPRO}/libnx ${DEVKITPRO}/portlibs/switch) -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) -set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Shared libs not available") - -project(SDL2) - -set(SRC_DIRS - src - src/atomic - src/audio - src/audio/switch - src/cpuinfo - src/events - src/file - src/filesystem/dummy - src/haptic - src/haptic/dummy - src/joystick - src/joystick/switch - src/joystick/dummy - src/loadso - src/loadso/dummy - src/libm - src/power - src/render - src/render/software - src/render/opengl - src/render/opengles - src/render/opengles2 - src/stdlib - src/thread - src/thread/switch - src/timer - src/timer/switch - src/video - src/video/yuv2rgb - src/video/switch - src/sensor - src/sensor/dummy - ) - -set(SRC_FILES ) -foreach (DIR ${SRC_DIRS}) - file(GLOB FILES ${DIR}/*.c*) - list(APPEND SRC_FILES ${FILES}) -endforeach (DIR) - -# SDL2 library -add_library(${PROJECT_NAME} STATIC ${SRC_FILES}) -target_include_directories(${PROJECT_NAME} PUBLIC ${SRC_DIRS} include) -target_compile_options(${PROJECT_NAME} PUBLIC -O3 -D__SWITCH__ -DSDL_VIDEO_STATIC_ANGLE) - -# SDL2 test -add_executable(${PROJECT_NAME}.elf test/testswitch.c) -target_include_directories(${PROJECT_NAME}.elf PRIVATE include) -target_compile_options(${PROJECT_NAME}.elf PRIVATE -O3 -D__SWITCH__) -target_link_libraries(${PROJECT_NAME}.elf - ${PROJECT_NAME} - ${DEVKITPRO}/portlibs/switch/lib/libEGL.a - ${DEVKITPRO}/portlibs/switch/lib/libglapi.a - ${DEVKITPRO}/portlibs/switch/lib/libdrm_nouveau.a - ${DEVKITPRO}/libnx/lib/libnx.a - stdc++ - m - ) -set_target_properties(${PROJECT_NAME}.elf PROPERTIES LINK_FLAGS "-specs=${DEVKITPRO}/libnx/switch.specs") -add_custom_target(${PROJECT_NAME}.nro - DEPENDS ${PROJECT_NAME}.elf - COMMAND elf2nro ${PROJECT_NAME}.elf ${PROJECT_NAME}.nro) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9b9eb620..a44c4b3b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,7 +164,7 @@ if(APPLE OR ARCH_64) set(OPT_DEF_SSEMATH ON) endif() endif() -if(UNIX OR MINGW OR MSYS) +if(UNIX OR MINGW OR MSYS OR NINTENDO_SWITCH) set(OPT_DEF_LIBC ON) endif() @@ -1988,6 +1988,61 @@ elseif(RISCOS) if(SDL_AUDIO) CheckOSS() endif() + +elseif(NINTENDO_SWITCH) + if(SDL_AUDIO) + set(SDL_AUDIO_DRIVER_SWITCH 1) + file(GLOB SWITCH_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/switch/*.c) + set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_AUDIO_SOURCES}) + set(HAVE_SDL_AUDIO TRUE) + endif() + #[[ + if(SDL_FILESYSTEM) + set(SDL_FILESYSTEM_SWITCH 1) + file(GLOB SWITCH_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/switch/*.c) + set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_FILESYSTEM_SOURCES}) + set(HAVE_SDL_FILESYSTEM TRUE) + endif() + ]] + if(SDL_JOYSTICK) + set(SDL_JOYSTICK_SWITCH 1) + file(GLOB SWITCH_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/switch/*.c) + set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_JOYSTICK_SOURCES}) + set(HAVE_SDL_JOYSTICK TRUE) + endif() + #[[ + if(SDL_POWER) + set(SDL_POWER_SWITCH 1) + file(GLOB SWITCH_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/switch/*.c) + set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_POWER_SOURCES}) + set(HAVE_SDL_POWER TRUE) + endif() + ]] + if(SDL_TIMERS) + set(SDL_TIMER_SWITCH 1) + file(GLOB SWITCH_TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/switch/*.c) + set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_TIMER_SOURCES}) + set(HAVE_SDL_TIMERS TRUE) + endif() + if(SDL_VIDEO) + set(SDL_VIDEO_DRIVER_SWITCH 1) + file(GLOB SWITCH_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/switch/*.c) + set(SOURCE_FILES ${SOURCE_FILES} ${SWITCH_VIDEO_SOURCES}) + set(HAVE_SDL_VIDEO TRUE) + set(SDL_VIDEO_OPENGL_EGL 1) + set(HAVE_VIDEO_OPENGLES TRUE) + set(SDL_VIDEO_OPENGL_ES2 1) + set(SDL_VIDEO_RENDER_OGL_ES2 1) + set(SDL_VIDEO_OPENGL 1) + set(HAVE_VIDEO_OPENGL TRUE) + endif() + set(SDL_STATIC_PIC ON) + list(APPEND EXTRA_LIBS EGL glapi drm_nouveau nx m) + add_definitions(-DSDL_VIDEO_STATIC_ANGLE) + set(HAVE_CLOCK_GETTIME 1) + set(PTHREADS ON) + set(PTHREADS_SEM ON) + CheckPTHREAD() endif() if(VIDEO_VULKAN) diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake index 754b57013..a3a8e1e87 100644 --- a/include/SDL_config.h.cmake +++ b/include/SDL_config.h.cmake @@ -287,6 +287,7 @@ #cmakedefine SDL_AUDIO_DRIVER_SUNAUDIO @SDL_AUDIO_DRIVER_SUNAUDIO@ #cmakedefine SDL_AUDIO_DRIVER_WASAPI @SDL_AUDIO_DRIVER_WASAPI@ #cmakedefine SDL_AUDIO_DRIVER_WINMM @SDL_AUDIO_DRIVER_WINMM@ +#cmakedefine SDL_AUDIO_DRIVER_SWITCH @SDL_AUDIO_DRIVER_SWITCH@ /* Enable various input drivers */ #cmakedefine SDL_INPUT_LINUXEV @SDL_INPUT_LINUXEV@ @@ -305,6 +306,7 @@ #cmakedefine SDL_JOYSTICK_HIDAPI @SDL_JOYSTICK_HIDAPI@ #cmakedefine SDL_JOYSTICK_EMSCRIPTEN @SDL_JOYSTICK_EMSCRIPTEN@ #cmakedefine SDL_JOYSTICK_VIRTUAL @SDL_JOYSTICK_VIRTUAL@ +#cmakedefine SDL_JOYSTICK_SWITCH @SDL_JOYSTICK_SWITCH@ #cmakedefine SDL_HAPTIC_DUMMY @SDL_HAPTIC_DUMMY@ #cmakedefine SDL_HAPTIC_LINUX @SDL_HAPTIC_LINUX@ #cmakedefine SDL_HAPTIC_IOKIT @SDL_HAPTIC_IOKIT@ @@ -330,6 +332,7 @@ #cmakedefine SDL_THREAD_PTHREAD_RECURSIVE_MUTEX @SDL_THREAD_PTHREAD_RECURSIVE_MUTEX@ #cmakedefine SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP @SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP@ #cmakedefine SDL_THREAD_WINDOWS @SDL_THREAD_WINDOWS@ +#cmakedefine SDL_THREAD_SWITCH @SDL_THREAD_SWITCH@ /* Enable various timer systems */ #cmakedefine SDL_TIMER_HAIKU @SDL_TIMER_HAIKU@ @@ -337,6 +340,7 @@ #cmakedefine SDL_TIMER_UNIX @SDL_TIMER_UNIX@ #cmakedefine SDL_TIMER_WINDOWS @SDL_TIMER_WINDOWS@ #cmakedefine SDL_TIMER_WINCE @SDL_TIMER_WINCE@ +#cmakedefine SDL_TIMER_SWITCH @SDL_TIMER_SWITCH@ /* Enable various video drivers */ #cmakedefine SDL_VIDEO_DRIVER_ANDROID @SDL_VIDEO_DRIVER_ANDROID@ @@ -386,6 +390,7 @@ #cmakedefine SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS @SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS@ #cmakedefine SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY @SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY@ #cmakedefine SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM @SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM@ +#cmakedefine SDL_VIDEO_DRIVER_SWITCH @SDL_VIDEO_DRIVER_SWITCH@ #cmakedefine SDL_VIDEO_RENDER_D3D @SDL_VIDEO_RENDER_D3D@ #cmakedefine SDL_VIDEO_RENDER_D3D11 @SDL_VIDEO_RENDER_D3D11@ @@ -423,6 +428,7 @@ #cmakedefine SDL_POWER_HAIKU @SDL_POWER_HAIKU@ #cmakedefine SDL_POWER_EMSCRIPTEN @SDL_POWER_EMSCRIPTEN@ #cmakedefine SDL_POWER_HARDWIRED @SDL_POWER_HARDWIRED@ +#cmakedefine SDL_POWER_SWITCH @SDL_POWER_SWITCH@ /* Enable system filesystem support */ #cmakedefine SDL_FILESYSTEM_ANDROID @SDL_FILESYSTEM_ANDROID@ @@ -432,6 +438,7 @@ #cmakedefine SDL_FILESYSTEM_UNIX @SDL_FILESYSTEM_UNIX@ #cmakedefine SDL_FILESYSTEM_WINDOWS @SDL_FILESYSTEM_WINDOWS@ #cmakedefine SDL_FILESYSTEM_EMSCRIPTEN @SDL_FILESYSTEM_EMSCRIPTEN@ +#cmakedefine SDL_FILESYSTEM_SWITCH @SDL_FILESYSTEM_SWITCH@ /* Enable assembly routines */ #cmakedefine SDL_ASSEMBLY_ROUTINES @SDL_ASSEMBLY_ROUTINES@ diff --git a/src/joystick/switch/SDL_sysjoystick.c b/src/joystick/switch/SDL_sysjoystick.c index b19128826..2d73a4424 100644 --- a/src/joystick/switch/SDL_sysjoystick.c +++ b/src/joystick/switch/SDL_sysjoystick.c @@ -31,36 +31,28 @@ #define JOYSTICK_COUNT 8 -typedef struct JoystickState +typedef struct SWITCHJoystickState { - HidControllerID id; - JoystickPosition l_pos; - JoystickPosition r_pos; + PadState pad; u64 buttons; - u32 vibrationDeviceHandles[2][2]; - HidVibrationValue vibrationValues[2]; -} JoystickState; + HidAnalogStickState stick_l; + HidAnalogStickState stick_r; + HidVibrationDeviceHandle vibrationDeviceHandles; + HidVibrationValue vibrationValues; +} SWITCHJoystickState; -/* Current pad state */ -static JoystickState pad[JOYSTICK_COUNT]; +static SWITCHJoystickState joystickState[JOYSTICK_COUNT]; -static HidControllerID pad_id[JOYSTICK_COUNT] = { - CONTROLLER_P1_AUTO, CONTROLLER_PLAYER_2, - CONTROLLER_PLAYER_3, CONTROLLER_PLAYER_4, - CONTROLLER_PLAYER_5, CONTROLLER_PLAYER_6, - CONTROLLER_PLAYER_7, CONTROLLER_PLAYER_8 -}; - -static const HidControllerKeys pad_mapping[] = { - KEY_A, KEY_B, KEY_X, KEY_Y, - KEY_LSTICK, KEY_RSTICK, - KEY_L, KEY_R, - KEY_ZL, KEY_ZR, - KEY_PLUS, KEY_MINUS, - KEY_DLEFT, KEY_DUP, KEY_DRIGHT, KEY_DDOWN, - KEY_LSTICK_LEFT, KEY_LSTICK_UP, KEY_LSTICK_RIGHT, KEY_LSTICK_DOWN, - KEY_RSTICK_LEFT, KEY_RSTICK_UP, KEY_RSTICK_RIGHT, KEY_RSTICK_DOWN, - KEY_SL_LEFT, KEY_SR_LEFT, KEY_SL_RIGHT, KEY_SR_RIGHT +static const HidNpadButton pad_mapping[] = { + HidNpadButton_A, HidNpadButton_B, HidNpadButton_X, HidNpadButton_Y, + HidNpadButton_StickL, HidNpadButton_StickR, + HidNpadButton_L, HidNpadButton_R, + HidNpadButton_ZL, HidNpadButton_ZR, + HidNpadButton_Plus, HidNpadButton_Minus, + HidNpadButton_Left, HidNpadButton_Up, HidNpadButton_Right, HidNpadButton_Down, + HidNpadButton_StickLLeft, HidNpadButton_StickLUp, HidNpadButton_StickLRight, HidNpadButton_StickLDown, + HidNpadButton_StickRLeft, HidNpadButton_StickRUp, HidNpadButton_StickRRight, HidNpadButton_StickRDown, + HidNpadButton_LeftSL, HidNpadButton_LeftSR, HidNpadButton_RightSL, HidNpadButton_RightSR }; /* Function to scan the system for joysticks. @@ -69,14 +61,18 @@ static const HidControllerKeys pad_mapping[] = { static int SWITCH_JoystickInit(void) { - for (int i = 0; i < JOYSTICK_COUNT; i++) { - pad[i].id = pad_id[i]; - hidInitializeVibrationDevices(pad[i].vibrationDeviceHandles[0], 2, CONTROLLER_HANDHELD, TYPE_HANDHELD); - if(pad[i].id == CONTROLLER_P1_AUTO) { - hidInitializeVibrationDevices(pad[i].vibrationDeviceHandles[1], 2, CONTROLLER_PLAYER_1, TYPE_HANDHELD); - } else { - hidInitializeVibrationDevices(pad[i].vibrationDeviceHandles[1], 2, pad[i].id, TYPE_JOYCON_PAIR); - } + padConfigureInput(JOYSTICK_COUNT, HidNpadStyleSet_NpadStandard); + + // initialize first pad to defaults + padInitializeDefault(&joystickState[0].pad); + padUpdate(&joystickState[0].pad); + + // initialize pad and vibrations for pad 1 to 7 + for (int i = 1; i < JOYSTICK_COUNT; i++) { + padInitialize(&joystickState[i].pad, HidNpadIdType_No1 + i); + padUpdate(&joystickState[i].pad); + hidInitializeVibrationDevices(&joystickState[i].vibrationDeviceHandles,1, + HidNpadIdType_No1 + i, padGetStyleSet(&joystickState[i].pad)); } return JOYSTICK_COUNT; @@ -148,23 +144,44 @@ SWITCH_JoystickOpen(SDL_Joystick *joystick, int device_index) static int SWITCH_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) { - int target_device = 0; int id = joystick->instance_id; - if (!hidGetHandheldMode()) { - target_device = 1; - } + joystickState[id].vibrationValues.amp_low = + joystickState[id].vibrationValues.amp_high = low_frequency_rumble == 0 ? 0.0f : 320.0f; + joystickState[id].vibrationValues.freq_low = + low_frequency_rumble == 0 ? 160.0f : (float) low_frequency_rumble / 204; + joystickState[id].vibrationValues.freq_high = + high_frequency_rumble == 0 ? 320.0f : (float) high_frequency_rumble / 204; - pad[id].vibrationValues[0].amp_low = pad[id].vibrationValues[0].amp_high = low_frequency_rumble == 0 ? 0.0f : 320.0f; - pad[id].vibrationValues[0].freq_low = low_frequency_rumble == 0 ? 160.0f : (float) low_frequency_rumble / 204; - pad[id].vibrationValues[0].freq_high = high_frequency_rumble == 0 ? 320.0f : (float) high_frequency_rumble / 204; - memcpy(&pad[id].vibrationValues[1], &pad[id].vibrationValues[0], sizeof(HidVibrationValue)); - - hidSendVibrationValues(pad[id].vibrationDeviceHandles[target_device], pad[id].vibrationValues, 2); + hidSendVibrationValues(&joystickState[id].vibrationDeviceHandles, &joystickState[id].vibrationValues, 1); return 0; } +static int +SWITCH_JoystickRumbleTriggers(SDL_Joystick * joystick, Uint16 left, Uint16 right) +{ + return SDL_Unsupported(); +} + +static SDL_bool +SWITCH_JoystickHasLED(SDL_Joystick * joystick) +{ + return SDL_FALSE; +} + +static int +SWITCH_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue) +{ + return 0; +} + +static int +SWITCH_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) +{ + return SDL_Unsupported(); +} + /* Function to update the state of a joystick - called as a device poll. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events @@ -174,44 +191,45 @@ static void SWITCH_JoystickUpdate(SDL_Joystick *joystick) { u64 changed; - static JoystickState pad_old[JOYSTICK_COUNT]; + static SWITCHJoystickState state_old[JOYSTICK_COUNT]; int index = (int) SDL_JoystickInstanceID(joystick); if (index > JOYSTICK_COUNT || SDL_IsTextInputActive()) { return; } - hidJoystickRead(&pad[index].l_pos, pad[index].id, JOYSTICK_LEFT); - hidJoystickRead(&pad[index].r_pos, pad[index].id, JOYSTICK_RIGHT); - pad[index].buttons = hidKeysHeld(pad[index].id); + padUpdate(&joystickState[index].pad); + joystickState[index].stick_l = padGetStickPos(&joystickState[index].pad, 0); + joystickState[index].stick_r = padGetStickPos(&joystickState[index].pad, 1); + joystickState[index].buttons = padGetButtons(&joystickState[index].pad); // Axes - if (pad_old[index].l_pos.dx != pad[index].l_pos.dx) { - SDL_PrivateJoystickAxis(joystick, 0, (Sint16) pad[index].l_pos.dx); - pad_old[index].l_pos.dx = pad[index].l_pos.dx; + if (state_old[index].stick_l.x != joystickState[index].stick_l.x) { + SDL_PrivateJoystickAxis(joystick, 0, (Sint16) joystickState[index].stick_l.x); + state_old[index].stick_l.x = joystickState[index].stick_l.x; } - if (pad_old[index].l_pos.dy != pad[index].l_pos.dy) { - SDL_PrivateJoystickAxis(joystick, 1, (Sint16) -pad[index].l_pos.dy); - pad_old[index].l_pos.dy = -pad[index].l_pos.dy; + if (state_old[index].stick_l.y != joystickState[index].stick_l.y) { + SDL_PrivateJoystickAxis(joystick, 1, (Sint16) -joystickState[index].stick_l.y); + state_old[index].stick_l.y = -joystickState[index].stick_l.y; } - if (pad_old[index].r_pos.dx != pad[index].r_pos.dx) { - SDL_PrivateJoystickAxis(joystick, 2, (Sint16) pad[index].r_pos.dx); - pad_old[index].r_pos.dx = pad[index].r_pos.dx; + if (state_old[index].stick_r.x != joystickState[index].stick_r.x) { + SDL_PrivateJoystickAxis(joystick, 2, (Sint16) joystickState[index].stick_r.x); + state_old[index].stick_r.x = joystickState[index].stick_r.x; } - if (pad_old[index].r_pos.dy != pad[index].r_pos.dy) { - SDL_PrivateJoystickAxis(joystick, 3, (Sint16) -pad[index].r_pos.dy); - pad_old[index].r_pos.dy = -pad[index].r_pos.dy; + if (state_old[index].stick_r.y != joystickState[index].stick_r.y) { + SDL_PrivateJoystickAxis(joystick, 3, (Sint16) -joystickState[index].stick_r.y); + state_old[index].stick_r.y = -joystickState[index].stick_r.y; } // Buttons - changed = pad_old[index].buttons ^ pad[index].buttons; - pad_old[index].buttons = pad[index].buttons; + changed = state_old[index].buttons ^ joystickState[index].buttons; + state_old[index].buttons = joystickState[index].buttons; if (changed) { for (int i = 0; i < joystick->nbuttons; i++) { if (changed & pad_mapping[i]) { - SDL_PrivateJoystickButton( - joystick, (Uint8) i, - (Uint8) ((pad[index].buttons & pad_mapping[i]) ? SDL_PRESSED : SDL_RELEASED)); + SDL_PrivateJoystickButton(joystick, (Uint8) i, + (Uint8) ((joystickState[index].buttons & pad_mapping[i]) ? + SDL_PRESSED : SDL_RELEASED)); } } } @@ -241,6 +259,10 @@ SDL_JoystickDriver SDL_SWITCH_JoystickDriver = SWITCH_JoystickGetDeviceInstanceID, SWITCH_JoystickOpen, SWITCH_JoystickRumble, + SWITCH_JoystickRumbleTriggers, + SWITCH_JoystickHasLED, + SWITCH_JoystickSetLED, + SWITCH_JoystickSetSensorsEnabled, SWITCH_JoystickUpdate, SWITCH_JoystickClose, SWITCH_JoystickQuit, diff --git a/src/video/khronos/EGL/eglplatform.h b/src/video/khronos/EGL/eglplatform.h index c77c3338d..c59e43bac 100644 --- a/src/video/khronos/EGL/eglplatform.h +++ b/src/video/khronos/EGL/eglplatform.h @@ -77,7 +77,7 @@ typedef HDC EGLNativeDisplayType; typedef HBITMAP EGLNativePixmapType; typedef HWND EGLNativeWindowType; -#elif defined(__APPLE__) || defined(__WINSCW__) || defined(__SYMBIAN32__) /* Symbian */ +#elif defined(__APPLE__) || defined(__WINSCW__) || defined(__SYMBIAN32__) || defined(__SWITCH__) typedef int EGLNativeDisplayType; typedef void *EGLNativeWindowType; diff --git a/src/video/switch/SDL_switchmouse.c b/src/video/switch/SDL_switchmouse.c index 02818a488..04c2db6b5 100644 --- a/src/video/switch/SDL_switchmouse.c +++ b/src/video/switch/SDL_switchmouse.c @@ -42,73 +42,74 @@ SWITCH_SetRelativeMouseMode(SDL_bool enabled) return 0; } -void +void SWITCH_InitMouse(void) { SDL_Mouse *mouse = SDL_GetMouse(); - mouse->SetRelativeMouseMode = SWITCH_SetRelativeMouseMode; + hidInitializeMouse(); } -void +void SWITCH_PollMouse(void) { - SDL_Window *window = SDL_GetFocusWindow(); - uint64_t buttons; - uint64_t changed_buttons; - MousePosition mouse_pos; - uint64_t timestamp; - int dx, dy; + SDL_Window *window = SDL_GetFocusWindow(); + HidMouseState mouse_state; + size_t state_count; + uint64_t changed_buttons; + uint64_t timestamp; + int dx, dy; - // We skip polling mouse if no window is created - if (window == NULL) - return; + // We skip polling mouse if no window is created + if (window == NULL) + return; - buttons = hidMouseButtonsHeld(); - changed_buttons = buttons ^ prev_buttons; + state_count = hidGetMouseStates(&mouse_state, 1); + changed_buttons = mouse_state.buttons ^ prev_buttons; - if (changed_buttons & MOUSE_LEFT) { - if (prev_buttons & MOUSE_LEFT) - SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_LEFT); - else - SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_LEFT); - } - if (changed_buttons & MOUSE_RIGHT) { - if (prev_buttons & MOUSE_RIGHT) - SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_RIGHT); - else - SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_RIGHT); - } - if (changed_buttons & MOUSE_MIDDLE) { - if (prev_buttons & MOUSE_MIDDLE) - SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_MIDDLE); - else - SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_MIDDLE); - } + if (changed_buttons & HidMouseButton_Left) { + if (prev_buttons & HidMouseButton_Left) + SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_LEFT); + else + SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_LEFT); + } + if (changed_buttons & HidMouseButton_Right) { + if (prev_buttons & HidMouseButton_Right) + SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_RIGHT); + else + SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_RIGHT); + } + if (changed_buttons & HidMouseButton_Middle) { + if (prev_buttons & HidMouseButton_Middle) + SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_MIDDLE); + else + SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_MIDDLE); + } - prev_buttons = buttons; + prev_buttons = mouse_state.buttons; - timestamp = SDL_GetTicks(); + timestamp = SDL_GetTicks(); - if (SDL_TICKS_PASSED(timestamp, last_timestamp + mouse_read_interval)) { - hidMouseRead(&mouse_pos); - // if hidMouseRead is called once per frame, a factor two on the velocities - // results in approximately the same mouse motion as reported by mouse_pos.x and mouse_pos.y - // but without the clamping to 1280 x 720 - dx = mouse_pos.velocityX * 2; - dy = mouse_pos.velocityY * 2; - if (dx || dy) { - SDL_SendMouseMotion(window, 0, 1, dx, dy); - } - last_timestamp = timestamp; - } + if (SDL_TICKS_PASSED(timestamp, last_timestamp + mouse_read_interval)) { + // if hidMouseRead is called once per frame, a factor two on the velocities + // results in approximately the same mouse motion as reported by mouse_pos.x and mouse_pos.y + // but without the clamping to 1280 x 720 + if(state_count > 0) { + dx = mouse_state.x * 2; + dy = mouse_state.y * 2; + if (dx || dy) { + SDL_SendMouseMotion(window, 0, 1, dx, dy); + } + } + last_timestamp = timestamp; + } } -void +void SWITCH_QuitMouse(void) { } #endif /* SDL_VIDEO_DRIVER_SWITCH */ -/* vi: set ts=4 sw=4 expandtab: */ +/* vi: set ts=4 sw=4 expandtab: */ \ No newline at end of file diff --git a/src/video/switch/SDL_switchvideo.c b/src/video/switch/SDL_switchvideo.c index 2bfa8cc14..45a62e3f6 100644 --- a/src/video/switch/SDL_switchvideo.c +++ b/src/video/switch/SDL_switchvideo.c @@ -40,12 +40,6 @@ static SDL_Window *switch_window = NULL; static AppletOperationMode operationMode; -static int -SWITCH_Available(void) -{ - return 1; -} - static void SWITCH_Destroy(SDL_VideoDevice *device) { @@ -92,7 +86,8 @@ SWITCH_CreateDevice(int devindex) device->MaximizeWindow = SWITCH_MaximizeWindow; device->MinimizeWindow = SWITCH_MinimizeWindow; device->RestoreWindow = SWITCH_RestoreWindow; - device->SetWindowGrab = SWITCH_SetWindowGrab; + //device->SetWindowMouseGrab = SWITCH_SetWindowGrab; // SDL 2.0.16 + //device->SetWindowKeyboardGrab = SWITCH_SetWindowGrab; // SDL 2.0.16 device->DestroyWindow = SWITCH_DestroyWindow; device->GL_LoadLibrary = SWITCH_GLES_LoadLibrary; @@ -117,10 +112,9 @@ SWITCH_CreateDevice(int devindex) } VideoBootStrap SWITCH_bootstrap = { - "Switch", - "SDL2 video driver for Nintendo Switch", - SWITCH_Available, - SWITCH_CreateDevice + "Switch", + "Nintendo Switch Video Driver", + SWITCH_CreateDevice }; /*****************************************************************************/ @@ -143,7 +137,7 @@ SWITCH_VideoInit(_THIS) display.desktop_mode = current_mode; display.current_mode = current_mode; display.driverdata = NULL; - SDL_AddVideoDisplay(&display); + SDL_AddVideoDisplay(&display, SDL_FALSE); // init touch SWITCH_InitTouch(); @@ -355,7 +349,6 @@ SWITCH_PumpEvents(_THIS) return; } - hidScanInput(); // we don't want other inputs overlapping with software keyboard if(!SDL_IsTextInputActive()) { SWITCH_PollTouch(); @@ -380,4 +373,4 @@ SWITCH_PumpEvents(_THIS) } } -#endif /* SDL_VIDEO_DRIVER_SWITCH */ +#endif /* SDL_VIDEO_DRIVER_SWITCH */ \ No newline at end of file