SDL: Fix SDL2main on PSP (#5076)

From da5dbb4763cb9dd6f473ee57bcfa4328415addc4 Mon Sep 17 00:00:00 2001
From: Wouter Wijsman <[EMAIL REDACTED]>
Date: Tue, 7 Dec 2021 16:43:50 +0100
Subject: [PATCH] Fix SDL2main on PSP (#5076)

- Fix SDL2main on PSP
  SDL2main was not working for PSP, because it wasn't being activated and
  it wasn't unsetting the main. Besides that a debug screen being started
  was causing issues with joystick input and the sceKernelExitGame calli
  is no longer needed with the current PSPDEV SDK.
- Clean up imports in PSP main
- Set PSP GPU and user modes in main
- Fix exit callback in PSP main
---
 include/SDL_main.h          |  9 +++++++++
 src/main/psp/SDL_psp_main.c | 16 +++++++---------
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/include/SDL_main.h b/include/SDL_main.h
index ec78e41495a..43aac974f14 100644
--- a/include/SDL_main.h
+++ b/include/SDL_main.h
@@ -83,6 +83,15 @@
 */
 #define SDL_MAIN_NEEDED
 
+#elif defined(__PSP__)
+/* On PSP SDL provides a main function that sets the module info,
+   activates the GPU and starts the thread required to be able to exit
+   the software.
+
+   If you provide this yourself, you may define SDL_MAIN_HANDLED
+ */
+#define SDL_MAIN_AVAILABLE
+
 #endif
 #endif /* SDL_MAIN_HANDLED */
 
diff --git a/src/main/psp/SDL_psp_main.c b/src/main/psp/SDL_psp_main.c
index 2ca8e446b43..fb3dd447487 100644
--- a/src/main/psp/SDL_psp_main.c
+++ b/src/main/psp/SDL_psp_main.c
@@ -7,11 +7,12 @@
 
 #include "SDL_main.h"
 #include <pspkernel.h>
-#include <pspdebug.h>
-#include <pspsdk.h>
 #include <pspthreadman.h>
 #include <stdlib.h>
-#include <stdio.h>
+
+#ifdef main
+    #undef main
+#endif
 
 /* If application's main() is redefined as SDL_main, and libSDLmain is
    linked, then this file will create the standard exit callback,
@@ -23,11 +24,12 @@
    PSP_MAIN_THREAD_STACK_SIZE, etc.
 */
 
-PSP_MODULE_INFO("SDL App", 0, 1, 1);
+PSP_MODULE_INFO("SDL App", 0, 1, 0);
+PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
 
 int sdl_psp_exit_callback(int arg1, int arg2, void *common)
 {
-    exit(0);
+    sceKernelExitGame();
     return 0;
 }
 
@@ -53,12 +55,8 @@ int sdl_psp_setup_callbacks(void)
 
 int main(int argc, char *argv[])
 {
-    pspDebugScreenInit();
     sdl_psp_setup_callbacks();
 
-    /* Register sceKernelExitGame() to be called when we exit */
-    atexit(sceKernelExitGame);
-
     SDL_SetMainReady();
 
     (void)SDL_main(argc, argv);