SDL: Fixed bug #5221 - Add SDL_AndroidSendMessage()

From fe2ed6cf6f355b821bae157abd98a11dce09d38e Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Tue, 1 Feb 2022 11:30:43 +0100
Subject: [PATCH] Fixed bug #5221 - Add SDL_AndroidSendMessage()

---
 include/SDL_system.h              | 9 +++++++++
 src/core/android/SDL_android.c    | 9 +++++++++
 src/dynapi/SDL_dynapi_overrides.h | 1 +
 src/dynapi/SDL_dynapi_procs.h     | 1 +
 4 files changed, 20 insertions(+)

diff --git a/include/SDL_system.h b/include/SDL_system.h
index e2fa7b5fe39..2257643cf27 100644
--- a/include/SDL_system.h
+++ b/include/SDL_system.h
@@ -425,6 +425,15 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AndroidRequestPermission(const char *permis
  */
 extern DECLSPEC int SDLCALL SDL_AndroidShowToast(const char* message, int duration, int gravity, int xoffset, int yoffset);
 
+/**
+ * Send a user command to SDLActivity
+ * override "boolean onUnhandledMessage(Message msg)" to handle the message
+ *
+ * \param command user command that must be greater or equal to 0x8000
+ * \param param user parameter
+ */
+extern DECLSPEC int SDLCALL SDL_AndroidSendMessage(Uint32 command, int param);
+
 #endif /* __ANDROID__ */
 
 /* Platform specific functions for WinRT */
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 4236835cfcc..c3815d1c5f5 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -2126,6 +2126,15 @@ void Android_JNI_HapticStop(int device_id)
 /* See SDLActivity.java for constants. */
 #define COMMAND_SET_KEEP_SCREEN_ON    5
 
+
+int SDL_AndroidSendMessage(Uint32 command, int param)
+{
+    if (command >= 0x8000) {
+        return Android_JNI_SendMessage(command, param);
+    }
+    return -1;
+}
+
 /* sends message to be handled on the UI event dispatch thread */
 int Android_JNI_SendMessage(int command, int param)
 {
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 53bb4d4f64c..2eec056e401 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -855,3 +855,4 @@
 #define SDL_GameControllerHasRumbleTriggers SDL_GameControllerHasRumbleTriggers_REAL
 #define SDL_hid_ble_scan SDL_hid_ble_scan_REAL
 #define SDL_PremultiplyAlpha SDL_PremultiplyAlpha_REAL
+#define SDL_AndroidSendMessage SDL_AndroidSendMessage_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 000cde80379..2d870ec7320 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -924,3 +924,4 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GameControllerHasRumble,(SDL_GameController *a),(a)
 SDL_DYNAPI_PROC(SDL_bool,SDL_GameControllerHasRumbleTriggers,(SDL_GameController *a),(a),return)
 SDL_DYNAPI_PROC(void,SDL_hid_ble_scan,(SDL_bool a),(a),)
 SDL_DYNAPI_PROC(int,SDL_PremultiplyAlpha,(int a, int b, Uint32 c, const void *d, int e, Uint32 f, void *g, int h),(a,b,c,d,e,f,g,h),return)
+SDL_DYNAPI_PROC(int,SDL_AndroidSendMessage,(Uint32 a, int b),(a,b),return)