From 28f266302fbeea2d75d5a62de651d464ab380798 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Sun, 4 Jul 2021 18:52:48 +0200 Subject: Add Nintendo Switch initial support --- src/skel/crossplatform.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++- src/skel/events.cpp | 2 + src/skel/glfw/glfw.cpp | 12 +++- 3 files changed, 156 insertions(+), 4 deletions(-) (limited to 'src/skel') diff --git a/src/skel/crossplatform.cpp b/src/skel/crossplatform.cpp index 577983b6..758125e9 100644 --- a/src/skel/crossplatform.cpp +++ b/src/skel/crossplatform.cpp @@ -198,6 +198,20 @@ char* casepath(char const* path, bool checkPathFirst) size_t rl = 0; DIR* d; + char* c; + + #if defined(GTA_SWITCH) || defined(GTA_VITA) + if( (c = strstr(p, ":/")) != NULL) // scheme used by some environments, eg. switch, vita + { + size_t deviceNameOffset = c - p + 3; + char* deviceNamePath = (char*)alloca(deviceNameOffset + 1); + strlcpy(deviceNamePath, p, deviceNameOffset); + deviceNamePath[deviceNameOffset] = 0; + d = opendir(deviceNamePath); + p = c + 1; + } + else + #endif if (p[0] == '/' || p[0] == '\\') { d = opendir("/"); @@ -212,7 +226,7 @@ char* casepath(char const* path, bool checkPathFirst) bool cantProceed = false; // just convert slashes in what's left in string, don't correct case of letters(because we can't) bool mayBeTrailingSlash = false; - char* c; + while (c = strsep(&p, "/\\")) { // May be trailing slash(allow), slash at the start(avoid), or multiple slashes(avoid) @@ -279,3 +293,133 @@ char* casepath(char const* path, bool checkPathFirst) return out; } #endif + +#ifdef GTA_SWITCH +/* Taken from glibc */ +char *realpath(const char *name, char *resolved) +{ + char *rpath, *dest = NULL; + const char *start, *end, *rpath_limit; + long int path_max; + + /* As per Single Unix Specification V2 we must return an error if + either parameter is a null pointer. We extend this to allow + the RESOLVED parameter to be NULL in case the we are expected to + allocate the room for the return value. */ + if (!name) + return NULL; + + /* As per Single Unix Specification V2 we must return an error if + the name argument points to an empty string. */ + if (name[0] == '\0') + return NULL; + +#ifdef PATH_MAX + path_max = PATH_MAX; +#else + path_max = pathconf(name, _PC_PATH_MAX); + if (path_max <= 0) + path_max = 1024; +#endif + + if (!resolved) + { + rpath = (char*)malloc(path_max); + if (!rpath) + return NULL; + } + else + rpath = resolved; + rpath_limit = rpath + path_max; + + if (name[0] != '/') + { + if (!getcwd(rpath, path_max)) + { + rpath[0] = '\0'; + goto error; + } + dest = (char*)memchr(rpath, '\0', path_max); + } + else + { + rpath[0] = '/'; + dest = rpath + 1; + } + + for (start = end = name; *start; start = end) + { + /* Skip sequence of multiple path-separators. */ + while (*start == '/') + ++start; + + /* Find end of path component. */ + for (end = start; *end && *end != '/'; ++end) + /* Nothing. */; + + if (end - start == 0) + break; + else if (end - start == 1 && start[0] == '.') + /* nothing */; + else if (end - start == 2 && start[0] == '.' && start[1] == '.') + { + /* Back up to previous component, ignore if at root already. */ + if (dest > rpath + 1) + while ((--dest)[-1] != '/') + ; + } + else + { + size_t new_size; + + if (dest[-1] != '/') + *dest++ = '/'; + + if (dest + (end - start) >= rpath_limit) + { + ptrdiff_t dest_offset = dest - rpath; + char *new_rpath; + + if (resolved) + { + if (dest > rpath + 1) + dest--; + *dest = '\0'; + goto error; + } + new_size = rpath_limit - rpath; + if (end - start + 1 > path_max) + new_size += end - start + 1; + else + new_size += path_max; + new_rpath = (char *)realloc(rpath, new_size); + if (!new_rpath) + goto error; + rpath = new_rpath; + rpath_limit = rpath + new_size; + + dest = rpath + dest_offset; + } + + dest = (char*)memcpy(dest, start, end - start); + *dest = '\0'; + } + } + if (dest > rpath + 1 && dest[-1] == '/') + --dest; + *dest = '\0'; + + return rpath; + +error: + if (!resolved) + free(rpath); + return NULL; +} + +ssize_t readlink (const char * __path, char * __buf, size_t __buflen) +{ + errno = ENOSYS; + return -1; +} +#endif diff --git a/src/skel/events.cpp b/src/skel/events.cpp index 3e1e95b3..87447819 100644 --- a/src/skel/events.cpp +++ b/src/skel/events.cpp @@ -821,7 +821,9 @@ PadHandler(RsEvent event, void *param) RwBool AttachInputDevices(void) { +#ifndef IGNORE_MOUSE_KEYBOARD RsInputDeviceAttach(rsKEYBOARD, KeyboardHandler); +#endif RsInputDeviceAttach(rsPAD, PadHandler); diff --git a/src/skel/glfw/glfw.cpp b/src/skel/glfw/glfw.cpp index 8d3fc7d7..c7f92d34 100644 --- a/src/skel/glfw/glfw.cpp +++ b/src/skel/glfw/glfw.cpp @@ -12,12 +12,14 @@ DWORD _dwOperatingSystemVersion; #include "resource.h" #else long _dwOperatingSystemVersion; +#ifndef GTA_SWITCH #ifndef __APPLE__ #include #else #include #include #endif +#endif #include #include #include @@ -51,7 +53,7 @@ long _dwOperatingSystemVersion; #include "MemoryMgr.h" // We found out that GLFW's keyboard input handling is still pretty delayed/not stable, so now we fetch input from X11 directly on Linux. -#if !defined _WIN32 && !defined __APPLE__ && !defined __SWITCH__ // && !defined WAYLAND +#if !defined _WIN32 && !defined __APPLE__ && !defined GTA_SWITCH // && !defined WAYLAND #define GET_KEYBOARD_INPUT_FROM_X11 #endif @@ -485,11 +487,13 @@ psInitialize(void) debug("Physical memory size %llu\n", _dwMemAvailPhys); debug("Available physical memory %llu\n", size); #else +#ifndef GTA_SWITCH struct sysinfo systemInfo; sysinfo(&systemInfo); _dwMemAvailPhys = systemInfo.freeram; debug("Physical memory size %u\n", systemInfo.totalram); debug("Available physical memory %u\n", systemInfo.freeram); +#endif #endif TheText.Unload(); @@ -949,13 +953,15 @@ void psPostRWinit(void) RwVideoMode vm; RwEngineGetVideoModeInfo(&vm, GcurSelVM); + glfwSetFramebufferSizeCallback(PSGLOBAL(window), resizeCB); +#ifndef IGNORE_MOUSE_KEYBOARD #ifndef GET_KEYBOARD_INPUT_FROM_X11 glfwSetKeyCallback(PSGLOBAL(window), keypressCB); #endif - glfwSetFramebufferSizeCallback(PSGLOBAL(window), resizeCB); glfwSetScrollCallback(PSGLOBAL(window), scrollCB); glfwSetCursorPosCallback(PSGLOBAL(window), cursorCB); glfwSetCursorEnterCallback(PSGLOBAL(window), cursorEnterCB); +#endif glfwSetWindowIconifyCallback(PSGLOBAL(window), windowIconifyCB); glfwSetWindowFocusCallback(PSGLOBAL(window), windowFocusCB); glfwSetJoystickCallback(joysChangeCB); @@ -1791,7 +1797,7 @@ main(int argc, char *argv[]) InitMemoryMgr(); #endif -#ifndef _WIN32 +#if !defined(_WIN32) && !defined(GTA_SWITCH) struct sigaction act; act.sa_sigaction = terminateHandler; act.sa_flags = SA_SIGINFO; -- cgit v1.2.3 From 02ec56e8f70ab0fda5092d3455f76dcbb8825cff Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Sun, 4 Jul 2021 20:51:16 +0200 Subject: Add platform specific functions for glfw vibration With implementation for Switch vibration through libnx hid/pad API --- src/skel/glfw/glfw.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'src/skel') diff --git a/src/skel/glfw/glfw.cpp b/src/skel/glfw/glfw.cpp index c7f92d34..754c43be 100644 --- a/src/skel/glfw/glfw.cpp +++ b/src/skel/glfw/glfw.cpp @@ -326,6 +326,78 @@ psNativeTextureSupport(void) #define CMDSTR LPSTR #endif +/* + ***************************************************************************** + */ + +#ifdef GTA_SWITCH + +static HidVibrationValue SwitchVibrationValues[2]; +static HidVibrationDeviceHandle SwitchVibrationDeviceHandles[2][2]; +static HidVibrationDeviceHandle SwitchVibrationDeviceGC; + +static PadState SwitchPad; + +Result HidInitializationResult[2]; +Result HidInitializationGCResult; + +void _psInitializeVibration() +{ + HidInitializationResult[0] = hidInitializeVibrationDevices(SwitchVibrationDeviceHandles[0], 2, HidNpadIdType_Handheld, HidNpadStyleTag_NpadHandheld); + if(R_FAILED(HidInitializationResult[0])) { + printf("Failed to initialize VibrationDevice for Handheld Mode\n"); + } + HidInitializationResult[1] = hidInitializeVibrationDevices(SwitchVibrationDeviceHandles[1], 2, HidNpadIdType_No1, HidNpadStyleSet_NpadFullCtrl); + if(R_FAILED(HidInitializationResult[1])) { + printf("Failed to initialize VibrationDevice for Detached Mode\n"); + } + HidInitializationGCResult = hidInitializeVibrationDevices(&SwitchVibrationDeviceGC, 1, HidNpadIdType_No1, HidNpadStyleTag_NpadGc); + if(R_FAILED(HidInitializationResult[1])) { + printf("Failed to initialize VibrationDevice for GC Mode\n"); + } + + SwitchVibrationValues[0].freq_low = 160.0f; + SwitchVibrationValues[0].freq_high = 320.0f; + + padConfigureInput(1, HidNpadStyleSet_NpadFullCtrl); + padInitializeDefault(&SwitchPad); +} + +void _psHandleVibration() +{ + padUpdate(&SwitchPad); + + uint8 target_device = padIsHandheld(&SwitchPad) ? 0 : 1; + + if(R_SUCCEEDED(HidInitializationResult[target_device])) { + CPad* pad = CPad::GetPad(0); + + // value conversion based on SDL2 switch port + SwitchVibrationValues[0].amp_high = SwitchVibrationValues[0].amp_low = pad->ShakeFreq == 0 ? 0.0f : 320.0f; + SwitchVibrationValues[0].freq_low = pad->ShakeFreq == 0.0 ? 160.0f : (float)pad->ShakeFreq * 1.26f; + SwitchVibrationValues[0].freq_high = pad->ShakeFreq == 0.0 ? 320.0f : (float)pad->ShakeFreq * 1.26f; + + if (pad->ShakeDur < CTimer::GetTimeStepInMilliseconds()) + pad->ShakeDur = 0; + else + pad->ShakeDur -= CTimer::GetTimeStepInMilliseconds(); + if (pad->ShakeDur == 0) pad->ShakeFreq = 0; + + + if(target_device == 1 && R_SUCCEEDED(HidInitializationGCResult)) { + // gamecube rumble + hidSendVibrationGcErmCommand(SwitchVibrationDeviceGC, pad->ShakeFreq > 0 ? HidVibrationGcErmCommand_Start : HidVibrationGcErmCommand_Stop); + } + + memcpy(&SwitchVibrationValues[1], &SwitchVibrationValues[0], sizeof(HidVibrationValue)); + hidSendVibrationValues(SwitchVibrationDeviceHandles[target_device], SwitchVibrationValues, 2); + } +} +#else +void _psInitializeVibration() {} +void _psHandleVibration() {} +#endif + /* ***************************************************************************** */ @@ -409,6 +481,8 @@ psInitialize(void) #endif #endif + + _psInitializeVibration(); gGameState = GS_START_UP; TRACE("gGameState = GS_START_UP"); @@ -2459,6 +2533,8 @@ void CapturePad(RwInt32 padID) if ( Abs(rightStickPos.y) > 0.3f ) pad->PCTempJoyState.RightStickY = (int32)(rightStickPos.y * 128.0f); } + + _psHandleVibration(); return; } -- cgit v1.2.3 From ca5d3c881133d1fb3c2d9af16de81b63304bc88f Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Mon, 5 Jul 2021 20:02:44 +0200 Subject: Replace GTA_SWITCH macro and use GAMEPAD_MENU * Replace GTA_SWITCH macro with __SWITCH__ for platform specific stuff and GTA_HANDHELD for the rest (which could be used by other ports) * Use GAMEPAD_MENU on GTA_HANDHELD, which will replace the usual controller setup (which contains some useless features for handhelds) * Fix some identation inconsistencies * Disable PC_PLAYER_CONTROLS on GTA_HANDHELD builds --- src/skel/crossplatform.cpp | 4 ++-- src/skel/glfw/glfw.cpp | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/skel') diff --git a/src/skel/crossplatform.cpp b/src/skel/crossplatform.cpp index 758125e9..e69c22e1 100644 --- a/src/skel/crossplatform.cpp +++ b/src/skel/crossplatform.cpp @@ -200,7 +200,7 @@ char* casepath(char const* path, bool checkPathFirst) DIR* d; char* c; - #if defined(GTA_SWITCH) || defined(GTA_VITA) + #if defined(__SWITCH__) || defined(PSP2) if( (c = strstr(p, ":/")) != NULL) // scheme used by some environments, eg. switch, vita { size_t deviceNameOffset = c - p + 3; @@ -294,7 +294,7 @@ char* casepath(char const* path, bool checkPathFirst) } #endif -#ifdef GTA_SWITCH +#ifdef __SWITCH__ /* Taken from glibc */ char *realpath(const char *name, char *resolved) { diff --git a/src/skel/glfw/glfw.cpp b/src/skel/glfw/glfw.cpp index 754c43be..767d1bbd 100644 --- a/src/skel/glfw/glfw.cpp +++ b/src/skel/glfw/glfw.cpp @@ -12,7 +12,7 @@ DWORD _dwOperatingSystemVersion; #include "resource.h" #else long _dwOperatingSystemVersion; -#ifndef GTA_SWITCH +#ifndef __SWITCH__ #ifndef __APPLE__ #include #else @@ -53,7 +53,7 @@ long _dwOperatingSystemVersion; #include "MemoryMgr.h" // We found out that GLFW's keyboard input handling is still pretty delayed/not stable, so now we fetch input from X11 directly on Linux. -#if !defined _WIN32 && !defined __APPLE__ && !defined GTA_SWITCH // && !defined WAYLAND +#if !defined _WIN32 && !defined __APPLE__ && !defined GTA_HANDHELD // && !defined WAYLAND #define GET_KEYBOARD_INPUT_FROM_X11 #endif @@ -330,7 +330,7 @@ psNativeTextureSupport(void) ***************************************************************************** */ -#ifdef GTA_SWITCH +#ifdef __SWITCH__ static HidVibrationValue SwitchVibrationValues[2]; static HidVibrationDeviceHandle SwitchVibrationDeviceHandles[2][2]; @@ -338,10 +338,10 @@ static HidVibrationDeviceHandle SwitchVibrationDeviceGC; static PadState SwitchPad; -Result HidInitializationResult[2]; -Result HidInitializationGCResult; +static Result HidInitializationResult[2]; +static Result HidInitializationGCResult; -void _psInitializeVibration() +static void _psInitializeVibration() { HidInitializationResult[0] = hidInitializeVibrationDevices(SwitchVibrationDeviceHandles[0], 2, HidNpadIdType_Handheld, HidNpadStyleTag_NpadHandheld); if(R_FAILED(HidInitializationResult[0])) { @@ -360,10 +360,10 @@ void _psInitializeVibration() SwitchVibrationValues[0].freq_high = 320.0f; padConfigureInput(1, HidNpadStyleSet_NpadFullCtrl); - padInitializeDefault(&SwitchPad); + padInitializeDefault(&SwitchPad); } -void _psHandleVibration() +static void _psHandleVibration() { padUpdate(&SwitchPad); @@ -394,8 +394,8 @@ void _psHandleVibration() } } #else -void _psInitializeVibration() {} -void _psHandleVibration() {} +static void _psInitializeVibration() {} +static void _psHandleVibration() {} #endif /* @@ -561,7 +561,7 @@ psInitialize(void) debug("Physical memory size %llu\n", _dwMemAvailPhys); debug("Available physical memory %llu\n", size); #else -#ifndef GTA_SWITCH +#ifndef __SWITCH__ struct sysinfo systemInfo; sysinfo(&systemInfo); _dwMemAvailPhys = systemInfo.freeram; @@ -1871,7 +1871,7 @@ main(int argc, char *argv[]) InitMemoryMgr(); #endif -#if !defined(_WIN32) && !defined(GTA_SWITCH) +#if !defined(_WIN32) && !defined(__SWITCH__) struct sigaction act; act.sa_sigaction = terminateHandler; act.sa_flags = SA_SIGINFO; -- cgit v1.2.3 From 3bde84f6c8480217b2fcd7ec29c28f62672a3279 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Mon, 12 Jul 2021 01:26:11 +0200 Subject: Move assets to dedicated folder and redefine GLFW mappings for Switch --- src/skel/crossplatform.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/skel') diff --git a/src/skel/crossplatform.h b/src/skel/crossplatform.h index aa90ce5a..67bb4282 100644 --- a/src/skel/crossplatform.h +++ b/src/skel/crossplatform.h @@ -157,3 +157,28 @@ bool FindNextFile(HANDLE, WIN32_FIND_DATA*); void FileTimeToSystemTime(time_t*, SYSTEMTIME*); void GetDateFormat(int, int, SYSTEMTIME*, int, char*, int); #endif + +#ifdef __SWITCH__ + +// tweak glfw values for switch to match expected pc bindings +#ifdef GLFW_GAMEPAD_BUTTON_A + #undef GLFW_GAMEPAD_BUTTON_A +#endif +#define GLFW_GAMEPAD_BUTTON_A 1 + +#ifdef GLFW_GAMEPAD_BUTTON_B + #undef GLFW_GAMEPAD_BUTTON_B +#endif +#define GLFW_GAMEPAD_BUTTON_B 0 + +#ifdef GLFW_GAMEPAD_BUTTON_X + #undef GLFW_GAMEPAD_BUTTON_X +#endif +#define GLFW_GAMEPAD_BUTTON_X 3 + +#ifdef GLFW_GAMEPAD_BUTTON_Y + #undef GLFW_GAMEPAD_BUTTON_Y +#endif +#define GLFW_GAMEPAD_BUTTON_Y 2 + +#endif -- cgit v1.2.3 From a7b38e1b34210c9c78c16a32140ba40e47152fa8 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Mon, 12 Jul 2021 20:58:34 +0200 Subject: Save current usable memory for Switch application --- src/skel/glfw/glfw.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/skel') diff --git a/src/skel/glfw/glfw.cpp b/src/skel/glfw/glfw.cpp index 767d1bbd..6d4cade9 100644 --- a/src/skel/glfw/glfw.cpp +++ b/src/skel/glfw/glfw.cpp @@ -560,14 +560,15 @@ psInitialize(void) _dwMemAvailPhys = (uint64_t)(vm_stat.free_count * page_size); debug("Physical memory size %llu\n", _dwMemAvailPhys); debug("Available physical memory %llu\n", size); +#elif defined (__SWITCH__) + svcGetInfo(&_dwMemAvailPhys, 7, 0xffff8001, 0); + debug("Physical memory size %llu\n", _dwMemAvailPhys); #else -#ifndef __SWITCH__ struct sysinfo systemInfo; sysinfo(&systemInfo); _dwMemAvailPhys = systemInfo.freeram; debug("Physical memory size %u\n", systemInfo.totalram); debug("Available physical memory %u\n", systemInfo.freeram); -#endif #endif TheText.Unload(); -- cgit v1.2.3 From 207378c94716f8c97600f39f63dff526de293a50 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Tue, 13 Jul 2021 00:41:56 +0200 Subject: Use svcGetInfo definitions from libnx Co-authored-by: Anonymous Maarten --- src/skel/glfw/glfw.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/skel') diff --git a/src/skel/glfw/glfw.cpp b/src/skel/glfw/glfw.cpp index 6d4cade9..2282a040 100644 --- a/src/skel/glfw/glfw.cpp +++ b/src/skel/glfw/glfw.cpp @@ -561,7 +561,7 @@ psInitialize(void) debug("Physical memory size %llu\n", _dwMemAvailPhys); debug("Available physical memory %llu\n", size); #elif defined (__SWITCH__) - svcGetInfo(&_dwMemAvailPhys, 7, 0xffff8001, 0); + svcGetInfo(&_dwMemAvailPhys, InfoType_UsedMemorySize, CUR_PROCESS_HANDLE, 0); debug("Physical memory size %llu\n", _dwMemAvailPhys); #else struct sysinfo systemInfo; -- cgit v1.2.3