SDL  2.0
SDL_androidtouch.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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 
49  *window_x = (int)(x * window_w);
50  *window_y = (int)(y * window_h);
51 }
52 
53 static SDL_bool separate_mouse_and_touch = SDL_FALSE;
54 
55 static void SDLCALL
56 SeparateEventsHintWatcher(void *userdata, const char *name,
57  const char *oldValue, const char *newValue)
58 {
59  separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0));
60 
61  Android_JNI_SetSeparateMouseAndTouch(separate_mouse_and_touch);
62 }
63 
64 void Android_InitTouch(void)
65 {
66  int i;
67  int* ids;
68  const int number = Android_JNI_GetTouchDeviceIds(&ids);
69 
71  SeparateEventsHintWatcher, NULL);
72 
73  if (0 < number) {
74  for (i = 0; i < number; ++i) {
75  SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
76  }
77  SDL_free(ids);
78  }
79 }
80 
81 void Android_QuitTouch(void)
82 {
84  SeparateEventsHintWatcher, NULL);
85  separate_mouse_and_touch = SDL_FALSE;
86 }
87 
88 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
89 {
90  SDL_TouchID touchDeviceId = 0;
91  SDL_FingerID fingerId = 0;
92  int window_x, window_y;
93  static SDL_FingerID pointerFingerID = 0;
94 
95  if (!Android_Window) {
96  return;
97  }
98 
99  touchDeviceId = (SDL_TouchID)touch_device_id_in;
100  if (SDL_AddTouch(touchDeviceId, "") < 0) {
101  SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
102  }
103 
104  fingerId = (SDL_FingerID)pointer_finger_id_in;
105  switch (action) {
106  case ACTION_DOWN:
107  /* Primary pointer down */
108  if (!separate_mouse_and_touch) {
109  Android_GetWindowCoordinates(x, y, &window_x, &window_y);
110  /* send moved event */
111  SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
112  /* send mouse down event */
114  }
115  pointerFingerID = fingerId;
116  case ACTION_POINTER_DOWN:
117  /* Non primary pointer down */
118  SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
119  break;
120 
121  case ACTION_MOVE:
122  if (!pointerFingerID) {
123  if (!separate_mouse_and_touch) {
124  Android_GetWindowCoordinates(x, y, &window_x, &window_y);
125  /* send moved event */
126  SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
127  }
128  }
129  SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
130  break;
131 
132  case ACTION_UP:
133  /* Primary pointer up */
134  if (!separate_mouse_and_touch) {
135  /* send mouse up */
137  }
138  pointerFingerID = (SDL_FingerID) 0;
139  case ACTION_POINTER_UP:
140  /* Non primary pointer up */
141  SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
142  break;
143 
144  default:
145  break;
146  }
147 }
148 
149 #endif /* SDL_VIDEO_DRIVER_ANDROID */
150 
151 /* vi: set ts=4 sw=4 expandtab: */
SDL_events.h
Android_JNI_SetSeparateMouseAndTouch
void Android_JNI_SetSeparateMouseAndTouch(SDL_bool new_value)
window_w
int window_w
Definition: testoverlay2.c:143
NULL
#define NULL
Definition: begin_code.h:164
Android_OnTouch
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
SDL_log.h
SDL_FingerID
Sint64 SDL_FingerID
Definition: SDL_touch.h:42
SDLCALL
#define SDLCALL
Definition: SDL_internal.h:45
Android_JNI_GetTouchDeviceIds
int Android_JNI_GetTouchDeviceIds(int **ids)
SDL_SendTouch
int SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, float x, float y, float pressure)
Definition: SDL_touch.c:222
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
SDL_SendTouchMotion
int SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, float x, float y, float pressure)
Definition: SDL_touch.c:284
SDL_TOUCH_MOUSEID
#define SDL_TOUCH_MOUSEID
Definition: SDL_touch.h:53
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
SDL_BUTTON_LEFT
#define SDL_BUTTON_LEFT
Definition: SDL_mouse.h:282
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
window_h
int window_h
Definition: testoverlay2.c:144
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_SendMouseMotion
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:263
SDL_AddTouch
int SDL_AddTouch(SDL_TouchID touchID, const char *name)
Definition: SDL_touch.c:136
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
Android_QuitTouch
void Android_QuitTouch(void)
SDL_TouchID
Sint64 SDL_TouchID
Definition: SDL_touch.h:41
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_androidtouch.h
SDL_AddHintCallback
#define SDL_AddHintCallback
Definition: SDL_dynapi_overrides.h:192
SDL_hints.h
SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH
#define SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH
A variable to control whether mouse and touch events are to be treated together or separately.
Definition: SDL_hints.h:849
Android_InitTouch
void Android_InitTouch(void)
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_DelHintCallback
#define SDL_DelHintCallback
Definition: SDL_dynapi_overrides.h:193
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_strcmp
#define SDL_strcmp
Definition: SDL_dynapi_overrides.h:417
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
Android_Window
SDL_Window * Android_Window
ids
GLuint * ids
Definition: SDL_opengl_glext.h:525
SDL_SendMouseButton
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:532