From 6c5a33fa5cc08264aa75841495ebee96ac40e06e Mon Sep 17 00:00:00 2001
From: Benau <Benau@users.noreply.github.com>
Date: Thu, 14 May 2020 10:48:03 +0800
Subject: [PATCH] Fix frozen STK when connecting USB hidapi gamepad

---
 .../source/Irrlicht/stk_android_native_app_glue.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/lib/irrlicht/source/Irrlicht/stk_android_native_app_glue.c b/lib/irrlicht/source/Irrlicht/stk_android_native_app_glue.c
index a25a56af9..be253b4fb 100644
--- a/lib/irrlicht/source/Irrlicht/stk_android_native_app_glue.c
+++ b/lib/irrlicht/source/Irrlicht/stk_android_native_app_glue.c
@@ -336,11 +336,19 @@ static void onStart(ANativeActivity* activity) {
     android_app_set_activity_state(app, APP_CMD_START);
 }
 
+// From SDL2 hid.cpp: request permission dialog in usbmanager call onPause which
+// blocks the stk thread (sdl thread too), so if this return true we use
+// android_app_write_cmd directly
+extern int isNonBlockingEnabled();
+
 static void onResume(ANativeActivity* activity) {
     LOGV("Resume: %p\n", activity);
     struct android_app* app = (struct android_app*)activity->instance;
     if (app->onAppCmdDirect != NULL) app->onAppCmdDirect(activity, APP_CMD_RESUME);
-    android_app_set_activity_state(app, APP_CMD_RESUME);
+    if (isNonBlockingEnabled() == 1)
+        android_app_write_cmd(app, APP_CMD_RESUME);
+    else
+        android_app_set_activity_state(app, APP_CMD_RESUME);
 }
 
 static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
@@ -372,7 +380,10 @@ static void onPause(ANativeActivity* activity) {
     LOGV("Pause: %p\n", activity);
     struct android_app* app = (struct android_app*)activity->instance;
     if (app->onAppCmdDirect != NULL) app->onAppCmdDirect(activity, APP_CMD_PAUSE);
-    android_app_set_activity_state(app, APP_CMD_PAUSE);
+    if (isNonBlockingEnabled() == 1)
+        android_app_write_cmd(app, APP_CMD_PAUSE);
+    else
+        android_app_set_activity_state(app, APP_CMD_PAUSE);
 }
 
 static void onStop(ANativeActivity* activity) {