From 494101f4cc8d49baf0cb7bb47618f57c97452148 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 20 Aug 2023 08:55:53 +1000 Subject: [PATCH] Vita: Support touch input kinda --- src/Makefile_vita | 2 +- src/Platform_PSVita.c | 12 +++------- src/Program.c | 2 +- src/Window_PSVita.c | 54 +++++++++++++++++++++++++++++++++++++++---- src/_GLShared.h | 3 --- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/Makefile_vita b/src/Makefile_vita index 107795119..24e73f6c6 100644 --- a/src/Makefile_vita +++ b/src/Makefile_vita @@ -9,7 +9,7 @@ CFILES :=$(wildcard src/*.c) OBJS := $(addprefix $(BUILD_DIR)/, $(CFILES:src/%.c=%.o)) # Needed by psvDebugScreenPrintf -LIBS += -lm -lSceDisplay_stub -lSceCtrl_stub -lSceGxm_stub +LIBS += -lm -lSceDisplay_stub -lSceCtrl_stub -lSceTouch_stub -lSceGxm_stub #--------------------------------------------------------------------------------- diff --git a/src/Platform_PSVita.c b/src/Platform_PSVita.c index b5213a5b0..d88578465 100644 --- a/src/Platform_PSVita.c +++ b/src/Platform_PSVita.c @@ -21,6 +21,7 @@ const cc_result ReturnCode_SocketWouldBlock = SCE_NET_ERROR_EWOULDBLOCK; const cc_result ReturnCode_DirectoryExists = EEXIST; const char* Platform_AppNameSuffix = " PS Vita"; static int epoll_id; +static int stdout_fd; /*########################################################################################################################* @@ -32,15 +33,8 @@ cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { } void Platform_Log(const char* msg, int len) { - int fd = sceKernelGetStdout(); - sceIoWrite(fd, msg, len); - sceIoWrite(fd, "\n", 1); - - //sceIoDevctl("emulator:", 2, msg, len, NULL, 0); - //cc_string str = String_Init(msg, len, len); - //cc_file file = 0; - //File_Open(&file, &str); - //File_Close(file); + if (!stdout_fd) stdout_fd = sceKernelGetStdout(); + sceIoWrite(stdout_fd, msg, len); } #define UnixTime_TotalMS(time) ((cc_uint64)time.sec * 1000 + UNIX_EPOCH + (time.usec / 1000)) diff --git a/src/Program.c b/src/Program.c index 7e40eda0d..e16043dbb 100644 --- a/src/Program.c +++ b/src/Program.c @@ -138,7 +138,7 @@ void android_main(void) { SetupProgram(0, NULL); for (;;) { RunProgram(0, NULL); } } -#elif defined CC_BUILD_3DS || defined CC_BUILD_PSP || defined CC_BUILD_GCWII || defined CC_BUILD_DREAMCAST +#elif defined CC_BUILD_3DS || defined CC_BUILD_PSP || defined CC_BUILD_GCWII || defined CC_BUILD_DREAMCAST || defined CC_BUILD_XBOX || defined CC_BUILD_PSVITA int main(int argc, char** argv) { SetupProgram(argc, argv); while (WindowInfo.Exists) { diff --git a/src/Window_PSVita.c b/src/Window_PSVita.c index c3228d094..c22a07e86 100644 --- a/src/Window_PSVita.c +++ b/src/Window_PSVita.c @@ -13,6 +13,7 @@ #include "Logger.h" #include static cc_bool launcherMode; +static SceTouchPanelInfo frontPanel; struct _DisplayData DisplayInfo; struct _WinData WindowInfo; @@ -38,6 +39,10 @@ void Window_Init(void) { Input.GamepadSource = true; sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG); + sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START); + sceTouchSetSamplingState(SCE_TOUCH_PORT_BACK, SCE_TOUCH_SAMPLING_STATE_START); + + sceTouchGetPanelInfo(SCE_TOUCH_PORT_FRONT, &frontPanel); } void Window_Create2D(int width, int height) { launcherMode = true; } @@ -104,12 +109,45 @@ static void ProcessCircleInput(SceCtrlData* pad, double delta) { Event_RaiseRawMove(&PointerEvents.RawMoved, dx * scale, dy * scale); } -void Window_ProcessEvents(double delta) { - SceCtrlData pad; - /* TODO implement */ - sceCtrlReadBufferPositive(0, &pad, 1); - int mods = pad.buttons; + +static void ProcessTouchPress(int x, int y) { + if (!frontPanel.maxDispX || !frontPanel.maxDispY) { + // TODO: Shouldn't ever happen? need to check + Pointer_SetPosition(0, x, y); + return; + } + // rescale from touch range to screen range + x = (x - frontPanel.minDispX) * SCREEN_WIDTH / frontPanel.maxDispX; + y = (y - frontPanel.minDispY) * SCREEN_HEIGHT / frontPanel.maxDispY; + Pointer_SetPosition(0, x, y); +} + +static void ProcessTouchInput(void) { + SceTouchData touch; + + // sceTouchRead is blocking (seems to block until vblank), and don't want that + int res = sceTouchPeek(SCE_TOUCH_PORT_FRONT, &touch, 1); + if (res == 0) return; // no data available yet + if (res < 0) return; // error occurred + + if (touch.reportNum > 0) { + int x = touch.report[0].x; + int y = touch.report[0].y; + ProcessTouchPress(X, Y); + } + Input_SetNonRepeatable(CCMOUSE_L, touch.reportNum > 0); +} + +static void ProcessPadInput(double delta) { + SceCtrlData pad; + + // sceCtrlReadBufferPositive is blocking (seems to block until vblank), and don't want that + int res = sceCtrlPeekBufferPositive(0, &pad, 1); + if (res == 0) return; // no data available yet + if (res < 0) return; // error occurred + + int mods = pad.buttons; if (launcherMode) { HandleButtons_Launcher(mods); } else { @@ -121,6 +159,12 @@ void Window_ProcessEvents(double delta) { } } +void Window_ProcessEvents(double delta) { + /* TODO implement */ + ProcessPadInput(delta); + ProcessTouchInput(); +} + void Cursor_SetPosition(int x, int y) { } // Makes no sense for PS Vita void Window_EnableRawMouse(void) { Input.RawMode = true; } diff --git a/src/_GLShared.h b/src/_GLShared.h index dce8e524d..68c7b6a6b 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -319,10 +319,7 @@ void Gfx_EndFrame(void) { } void Gfx_OnWindowResize(void) { - /* TODO: Eliminate this nasty hack.. */ - Game_UpdateDimensions(); glViewport(0, 0, Game.Width, Game.Height); - /* With cocoa backend, in some cases [NSOpenGLContext update] will actually */ /* call glViewport with the size of the window framebuffer */ /* https://github.com/glfw/glfw/issues/80 */