Add a hint watch callback for SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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() */