SDL  2.0
SDL_androidtouch.c
Go to the documentation of this file.
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 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 
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 {
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 */
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 */
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: */
GLuint * ids
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
Sint64 SDL_FingerID
Definition: SDL_touch.h:42
GLfloat GLfloat p
int SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, float x, float y, float pressure)
Definition: SDL_touch.c:216
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
GLuint const GLchar * name
#define SDL_TOUCH_MOUSEID
Definition: SDL_touch.h:53
void Android_InitTouch(void)
int SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, float x, float y, float pressure)
Definition: SDL_touch.c:278
#define SDL_GetWindowSize
#define SDL_Log
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:188
#define SDL_BUTTON_LEFT
Definition: SDL_mouse.h:282
void SDL_free(void *mem)
SDL_Window * Android_Window
Sint64 SDL_TouchID
Definition: SDL_touch.h:41
int SDL_AddTouch(SDL_TouchID touchID, const char *name)
Definition: SDL_touch.c:130
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
#define NULL
Definition: begin_code.h:143
SDL_bool
Definition: SDL_stdinc.h:130
jclass Android_JNI_GetActivityClass(void)
#define SDL_AddHintCallback
#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:635
#define SDL_DelHintCallback
int window_h
Definition: testoverlay2.c:146
#define SDL_strcmp
#define SDL_PRESSED
Definition: SDL_events.h:50
int Android_JNI_GetTouchDeviceIds(int **ids)
#define SDL_RELEASED
Definition: SDL_events.h:49
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF base if bpp PF set rept prefetch_distance PF set OFFSET endr endif endm macro preload_leading_step2 base if bpp ifc DST PF PF else if bpp lsl PF PF lsl PF PF lsl PF PF PF else PF lsl PF lsl PF lsl PF endif SIZE macro preload_middle scratch_holds_offset if bpp if else PF PF endif endif endif endm macro preload_trailing base if bpp if bpp *pix_per_block PF PF lsl PF PF PF PF PF else PF lsl PF lsl PF PF PF PF PF base if bpp if narrow_case &&bpp<=dst_w_bpp) PF bic, WK0, base, #31 PF pld, [WK0] PF add, WK1, base, X, LSL #bpp_shift PF sub, WK1, WK1, #1 PF bic, WK1, WK1, #31 PF cmp, WK1, WK0 PF beq, 90f PF pld, [WK1]90:.else PF bic, WK0, base, #31 PF pld, [WK0] PF add, WK1, base, X, lsl #bpp_shift PF sub, WK1, WK1, #1 PF bic, WK1, WK1, #31 PF cmp, WK1, WK0 PF beq, 92f91:PF add, WK0, WK0, #32 PF cmp, WK0, WK1 PF pld, [WK0] PF bne, 91b92:.endif .endif.endm.macro conditional_process1_helper cond, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx process_head cond, numbytes, firstreg, unaligned_src, unaligned_mask, 0 .if decrementx sub &cond X, X, #8 *numbytes/dst_w_bpp .endif process_tail cond, numbytes, firstreg .if !((flags) &FLAG_PROCESS_DOES_STORE) pixst cond, numbytes, firstreg, DST .endif.endm.macro conditional_process1 cond, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx .if(flags) &FLAG_BRANCH_OVER .ifc cond, mi bpl 100f .endif .ifc cond, cs bcc 100f .endif .ifc cond, ne beq 100f .endif conditional_process1_helper, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx100:.else conditional_process1_helper cond, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx .endif.endm.macro conditional_process2 test, cond1, cond2, process_head, process_tail, numbytes1, numbytes2, firstreg1, firstreg2, unaligned_src, unaligned_mask, decrementx .if(flags) &(FLAG_DST_READWRITE|FLAG_BRANCH_OVER|FLAG_PROCESS_CORRUPTS_PSR|FLAG_PROCESS_DOES_STORE) test conditional_process1 cond1, process_head, process_tail, numbytes1, firstreg1, unaligned_src, unaligned_mask, decrementx .if(flags) &FLAG_PROCESS_CORRUPTS_PSR test .endif conditional_process1 cond2, process_head, process_tail, numbytes2, firstreg2, unaligned_src, unaligned_mask, decrementx .else test process_head cond1, numbytes1, firstreg1, unaligned_src, unaligned_mask, 0 process_head cond2, numbytes2, firstreg2, unaligned_src, unaligned_mask, 0 .if decrementx sub &cond1 X, X, #8 *numbytes1/dst_w_bpp sub &cond2 X, X, #8 *numbytes2/dst_w_bpp .endif process_tail cond1, numbytes1, firstreg1 process_tail cond2, numbytes2, firstreg2 pixst cond1, numbytes1, firstreg1, DST pixst cond2, numbytes2, firstreg2, DST .endif.endm.macro test_bits_1_0_ptr .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 movs SCRATCH, X, lsl #32-1 .else movs SCRATCH, WK0, lsl #32-1 .endif.endm.macro test_bits_3_2_ptr .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 movs SCRATCH, X, lsl #32-3 .else movs SCRATCH, WK0, lsl #32-3 .endif.endm.macro leading_15bytes process_head, process_tail .set DECREMENT_X, 1 .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 .set DECREMENT_X, 0 sub X, X, WK0, lsr #dst_bpp_shift str X, [sp, #LINE_SAVED_REG_COUNT *4] mov X, WK0 .endif .if dst_w_bpp==8 conditional_process2 test_bits_1_0_ptr, mi, cs, process_head, process_tail, 1, 2, 1, 2, 1, 1, DECREMENT_X .elseif dst_w_bpp==16 test_bits_1_0_ptr conditional_process1 cs, process_head, process_tail, 2, 2, 1, 1, DECREMENT_X .endif conditional_process2 test_bits_3_2_ptr, mi, cs, process_head, process_tail, 4, 8, 1, 2, 1, 1, DECREMENT_X .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 ldr X, [sp, #LINE_SAVED_REG_COUNT *4] .endif.endm.macro test_bits_3_2_pix movs SCRATCH, X, lsl #dst_bpp_shift+32-3.endm.macro test_bits_1_0_pix .if dst_w_bpp==8 movs SCRATCH, X, lsl #dst_bpp_shift+32-1 .else movs SCRATCH, X, lsr #1 .endif.endm.macro trailing_15bytes process_head, process_tail, unaligned_src, unaligned_mask conditional_process2 test_bits_3_2_pix, cs, mi, process_head, process_tail, 8, 4, 0, 2, unaligned_src, unaligned_mask, 0 .if dst_w_bpp==16 test_bits_1_0_pix conditional_process1 cs, process_head, process_tail, 2, 0, unaligned_src, unaligned_mask, 0 .elseif dst_w_bpp==8 conditional_process2 test_bits_1_0_pix, cs, mi, process_head, process_tail, 2, 1, 0, 1, unaligned_src, unaligned_mask, 0 .endif.endm.macro wide_case_inner_loop process_head, process_tail, unaligned_src, unaligned_mask, dst_alignment110:.set SUBBLOCK, 0 .rept pix_per_block *dst_w_bpp/128 process_head, 16, 0, unaligned_src, unaligned_mask, 1 .if(src_bpp > 0) &&(mask_bpp==0) &&((flags) &FLAG_PROCESS_PRESERVES_SCRATCH) preload_middle src_bpp, SRC, 1 .elseif(src_bpp==0) &&(mask_bpp > 0) &&((flags) &FLAG_PROCESS_PRESERVES_SCRATCH) preload_middle mask_bpp, MASK, 1 .else preload_middle src_bpp, SRC, 0 preload_middle mask_bpp, MASK, 0 .endif .if(dst_r_bpp > 0) &&((SUBBLOCK % 2)==0) &&(((flags) &FLAG_NO_PRELOAD_DST)==0) PF pld, [DST, #32 *prefetch_distance - dst_alignment] .endif process_tail, 16, 0 .if !((flags) &FLAG_PROCESS_DOES_STORE) pixst, 16, 0, DST .endif .set SUBBLOCK, SUBBLOCK+1 .endr subs X, X, #pix_per_block bhs 110b.endm.macro wide_case_inner_loop_and_trailing_pixels process_head, process_tail, process_inner_loop, exit_label, unaligned_src, unaligned_mask .if dst_r_bpp > tst bne process_inner_loop DST_PRELOAD_BIAS endif preload_trailing SRC preload_trailing MASK DST endif add medium_case_inner_loop_and_trailing_pixels unaligned_mask endm macro medium_case_inner_loop_and_trailing_pixels DST endif subs bhs tst beq exit_label trailing_15bytes unaligned_mask endm macro narrow_case_inner_loop_and_trailing_pixels unaligned_mask tst conditional_process1 trailing_15bytes unaligned_mask endm macro switch_on_alignment action
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:414
JNIEnv * Android_JNI_GetEnv(void)
int window_w
Definition: testoverlay2.c:145
void Android_QuitTouch(void)