Add a hint watch callback for SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 25 Mar 2015 10:59:10 -0400
changeset 9441 fc7522030729
parent 9440 a72c15020efa
child 9442 6073a89fc0eb
Add a hint watch callback for SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH.
src/video/android/SDL_androidtouch.c
src/video/android/SDL_androidtouch.h
src/video/android/SDL_androidvideo.c
--- a/src/video/android/SDL_androidtouch.c	Wed Mar 25 10:48:59 2015 -0400
+++ b/src/video/android/SDL_androidtouch.c	Wed Mar 25 10:59:10 2015 -0400
@@ -50,11 +50,24 @@
     *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_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 @@
     }
 
     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 @@
         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 @@
 
         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);
--- a/src/video/android/SDL_androidtouch.h	Wed Mar 25 10:48:59 2015 -0400
+++ b/src/video/android/SDL_androidtouch.h	Wed Mar 25 10:59:10 2015 -0400
@@ -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: */
--- a/src/video/android/SDL_androidvideo.c	Wed Mar 25 10:48:59 2015 -0400
+++ b/src/video/android/SDL_androidvideo.c	Wed Mar 25 10:59:10 2015 -0400
@@ -188,6 +188,7 @@
 void
 Android_VideoQuit(_THIS)
 {
+    Android_QuitTouch();
 }
 
 /* This function gets called before VideoInit() */