a6e0b896a74f5dc44f006e96b0396a6ca29fa8a5
[platform/upstream/SDL.git] / src / video / android / SDL_androidtouch.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22
23 #if SDL_VIDEO_DRIVER_ANDROID
24
25 #include <android/log.h>
26
27 #include "SDL_hints.h"
28 #include "SDL_events.h"
29 #include "SDL_log.h"
30 #include "SDL_androidtouch.h"
31 #include "../../events/SDL_mouse_c.h"
32 #include "../../events/SDL_touch_c.h"
33 #include "../../core/android/SDL_android.h"
34
35 #define ACTION_DOWN 0
36 #define ACTION_UP 1
37 #define ACTION_MOVE 2
38 #define ACTION_CANCEL 3
39 #define ACTION_OUTSIDE 4
40 #define ACTION_POINTER_DOWN 5
41 #define ACTION_POINTER_UP 6
42
43 static void Android_GetWindowCoordinates(float x, float y,
44                                          int *window_x, int *window_y)
45 {
46     int window_w, window_h;
47
48     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
49     *window_x = (int)(x * window_w);
50     *window_y = (int)(y * window_h);
51 }
52
53 static volatile SDL_bool separate_mouse_and_touch = SDL_FALSE;
54
55 static void
56 SeparateEventsHintWatcher(void *userdata, const char *name,
57                           const char *oldValue, const char *newValue)
58 {
59     jclass mActivityClass = Android_JNI_GetActivityClass();
60     JNIEnv *env = Android_JNI_GetEnv();
61     jfieldID fid = (*env)->GetStaticFieldID(env, mActivityClass, "mSeparateMouseAndTouch", "Z");
62
63     separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0));
64     (*env)->SetStaticBooleanField(env, mActivityClass, fid, separate_mouse_and_touch ? JNI_TRUE : JNI_FALSE);
65 }
66
67 void Android_InitTouch(void)
68 {
69     int i;
70     int* ids;
71     const int number = Android_JNI_GetTouchDeviceIds(&ids);
72
73     SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
74                         SeparateEventsHintWatcher, NULL);
75
76     if (0 < number) {
77         for (i = 0; i < number; ++i) {
78             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
79         }
80         SDL_free(ids);
81     }
82 }
83
84 void Android_QuitTouch(void)
85 {
86     SDL_DelHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
87                         SeparateEventsHintWatcher, NULL);
88     separate_mouse_and_touch = SDL_FALSE;
89 }
90
91 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
92 {
93     SDL_TouchID touchDeviceId = 0;
94     SDL_FingerID fingerId = 0;
95     int window_x, window_y;
96     static SDL_FingerID pointerFingerID = 0;
97
98     if (!Android_Window) {
99         return;
100     }
101
102     touchDeviceId = (SDL_TouchID)touch_device_id_in;
103     if (SDL_AddTouch(touchDeviceId, "") < 0) {
104         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
105     }
106
107     fingerId = (SDL_FingerID)pointer_finger_id_in;
108     switch (action) {
109         case ACTION_DOWN:
110             /* Primary pointer down */
111             if (!separate_mouse_and_touch) {
112                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
113                 /* send moved event */
114                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
115                 /* send mouse down event */
116                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
117             }
118             pointerFingerID = fingerId;
119         case ACTION_POINTER_DOWN:
120             /* Non primary pointer down */
121             SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
122             break;
123
124         case ACTION_MOVE:
125             if (!pointerFingerID) {
126                 if (!separate_mouse_and_touch) {
127                     Android_GetWindowCoordinates(x, y, &window_x, &window_y);
128                     /* send moved event */
129                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
130                 }
131             }
132             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
133             break;
134
135         case ACTION_UP:
136             /* Primary pointer up */
137             if (!separate_mouse_and_touch) {
138                 /* send mouse up */
139                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
140             }
141             pointerFingerID = (SDL_FingerID) 0;
142         case ACTION_POINTER_UP:
143             /* Non primary pointer up */
144             SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
145             break;
146
147         default:
148             break;
149     }
150 }
151
152 #endif /* SDL_VIDEO_DRIVER_ANDROID */
153
154 /* vi: set ts=4 sw=4 expandtab: */