Commit 1270247ccdeeaa3edfc26d73a6e733690d4a3152

Ryan C. Gordon 2015-03-25T10:59:10

Add a hint watch callback for SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH.

diff --git a/src/video/android/SDL_androidtouch.c b/src/video/android/SDL_androidtouch.c
index 1ad78dd..27b7859 100644
--- a/src/video/android/SDL_androidtouch.c
+++ b/src/video/android/SDL_androidtouch.c
@@ -50,11 +50,24 @@ static void Android_GetWindowCoordinates(float x, float y,
     *window_y = (int)(y * window_h);
 }
 
+static volatile SDL_bool separate_mouse_and_touch = SDL_FALSE;
+
+static void
+SeparateEventsHintWatcher(void *userdata, const char *name,
+                          const char *oldValue, const char *newValue)
+{
+    separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0));
+}
+
 void Android_InitTouch(void)
 {
     int i;
     int* ids;
-    int number = Android_JNI_GetTouchDeviceIds(&ids);
+    const int number = Android_JNI_GetTouchDeviceIds(&ids);
+
+    SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
+                        SeparateEventsHintWatcher, NULL);
+
     if (0 < number) {
         for (i = 0; i < number; ++i) {
             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
@@ -63,12 +76,18 @@ void Android_InitTouch(void)
     }
 }
 
+void Android_QuitTouch(void)
+{
+    SDL_DelHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
+                        SeparateEventsHintWatcher, NULL);
+    separate_mouse_and_touch = SDL_FALSE;
+}
+
 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
 {
     SDL_TouchID touchDeviceId = 0;
     SDL_FingerID fingerId = 0;
     int window_x, window_y;
-    const char * hint;
     static SDL_FingerID pointerFingerID = 0;
 
     if (!Android_Window) {
@@ -81,12 +100,11 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
     }
 
     fingerId = (SDL_FingerID)pointer_finger_id_in;
-    hint = SDL_GetHint("SDL_ANDROID_SEPARATE_MOUSE_AND_TOUCH");
     switch (action) {
         case ACTION_DOWN:
             /* Primary pointer down */
             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
-            if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
+            if (!separate_mouse_and_touch) {
                 /* send moved event */
                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
                 /* send mouse down event */
@@ -101,8 +119,7 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
         case ACTION_MOVE:
             if (!pointerFingerID) {
                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
-
-                if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
+                if (!separate_mouse_and_touch) {
                     /* send moved event */
                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
                 }
@@ -112,7 +129,7 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
 
         case ACTION_UP:
             /* Primary pointer up */
-            if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
+            if (!separate_mouse_and_touch) {
                 /* send mouse up */
                 pointerFingerID = (SDL_FingerID) 0;
                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
diff --git a/src/video/android/SDL_androidtouch.h b/src/video/android/SDL_androidtouch.h
index 81a0cb5..904e0d2 100644
--- a/src/video/android/SDL_androidtouch.h
+++ b/src/video/android/SDL_androidtouch.h
@@ -23,6 +23,7 @@
 #include "SDL_androidvideo.h"
 
 extern void Android_InitTouch(void);
+extern void Android_QuitTouch(void);
 extern void Android_OnTouch( int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p);
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index 5e03461..66384b2 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -188,6 +188,7 @@ Android_VideoInit(_THIS)
 void
 Android_VideoQuit(_THIS)
 {
+    Android_QuitTouch();
 }
 
 /* This function gets called before VideoInit() */