1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "input"
18 #include <utils/Log.h>
19
20 #include <android/input.h>
21 #include <input/Input.h>
22 #include <input/InputTransport.h>
23 #include <utils/Looper.h>
24 #include <utils/RefBase.h>
25 #include <utils/Vector.h>
26
27 #include <android_runtime/android_app_NativeActivity.h>
28 #include <android_runtime/android_view_InputQueue.h>
29 #include <android_view_MotionEvent.h>
30 #include <android_view_KeyEvent.h>
31
32 #include <poll.h>
33 #include <errno.h>
34
35 using android::InputEvent;
36 using android::InputEventType;
37 using android::InputQueue;
38 using android::KeyEvent;
39 using android::Looper;
40 using android::MotionEvent;
41 using android::sp;
42 using android::Vector;
43
AInputEvent_getType(const AInputEvent * event)44 int32_t AInputEvent_getType(const AInputEvent* event) {
45 const InputEventType eventType = static_cast<const InputEvent*>(event)->getType();
46 return static_cast<int32_t>(eventType);
47 }
48
AInputEvent_getDeviceId(const AInputEvent * event)49 int32_t AInputEvent_getDeviceId(const AInputEvent* event) {
50 return static_cast<const InputEvent*>(event)->getDeviceId();
51 }
52
AInputEvent_getSource(const AInputEvent * event)53 int32_t AInputEvent_getSource(const AInputEvent* event) {
54 return static_cast<const InputEvent*>(event)->getSource();
55 }
56
AInputEvent_release(const AInputEvent * event)57 void AInputEvent_release(const AInputEvent* event) {
58 delete event;
59 }
60
AKeyEvent_getAction(const AInputEvent * key_event)61 int32_t AKeyEvent_getAction(const AInputEvent* key_event) {
62 return static_cast<const KeyEvent*>(key_event)->getAction();
63 }
64
AKeyEvent_getFlags(const AInputEvent * key_event)65 int32_t AKeyEvent_getFlags(const AInputEvent* key_event) {
66 return static_cast<const KeyEvent*>(key_event)->getFlags();
67 }
68
AKeyEvent_getKeyCode(const AInputEvent * key_event)69 int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) {
70 return static_cast<const KeyEvent*>(key_event)->getKeyCode();
71 }
72
AKeyEvent_getScanCode(const AInputEvent * key_event)73 int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) {
74 return static_cast<const KeyEvent*>(key_event)->getScanCode();
75 }
76
AKeyEvent_getMetaState(const AInputEvent * key_event)77 int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) {
78 return static_cast<const KeyEvent*>(key_event)->getMetaState();
79 }
AKeyEvent_getRepeatCount(const AInputEvent * key_event)80 int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) {
81 return static_cast<const KeyEvent*>(key_event)->getRepeatCount();
82 }
83
AKeyEvent_getDownTime(const AInputEvent * key_event)84 int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) {
85 return static_cast<const KeyEvent*>(key_event)->getDownTime();
86 }
87
AKeyEvent_fromJava(JNIEnv * env,jobject keyEvent)88 const AInputEvent* AKeyEvent_fromJava(JNIEnv* env, jobject keyEvent) {
89 std::unique_ptr<KeyEvent> event = std::make_unique<KeyEvent>();
90 *event = android::android_view_KeyEvent_toNative(env, keyEvent);
91 return event.release();
92 }
93
AKeyEvent_getEventTime(const AInputEvent * key_event)94 int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) {
95 return static_cast<const KeyEvent*>(key_event)->getEventTime();
96 }
97
AMotionEvent_getAction(const AInputEvent * motion_event)98 int32_t AMotionEvent_getAction(const AInputEvent* motion_event) {
99 return static_cast<const MotionEvent*>(motion_event)->getAction();
100 }
101
AMotionEvent_getFlags(const AInputEvent * motion_event)102 int32_t AMotionEvent_getFlags(const AInputEvent* motion_event) {
103 return static_cast<const MotionEvent*>(motion_event)->getFlags();
104 }
105
AMotionEvent_getMetaState(const AInputEvent * motion_event)106 int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) {
107 return static_cast<const MotionEvent*>(motion_event)->getMetaState();
108 }
109
AMotionEvent_getButtonState(const AInputEvent * motion_event)110 int32_t AMotionEvent_getButtonState(const AInputEvent* motion_event) {
111 return static_cast<const MotionEvent*>(motion_event)->getButtonState();
112 }
113
AMotionEvent_getEdgeFlags(const AInputEvent * motion_event)114 int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) {
115 return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags();
116 }
117
AMotionEvent_getDownTime(const AInputEvent * motion_event)118 int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) {
119 return static_cast<const MotionEvent*>(motion_event)->getDownTime();
120 }
121
AMotionEvent_getEventTime(const AInputEvent * motion_event)122 int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) {
123 return static_cast<const MotionEvent*>(motion_event)->getEventTime();
124 }
125
AMotionEvent_getXOffset(const AInputEvent * motion_event)126 float AMotionEvent_getXOffset(const AInputEvent* motion_event) {
127 return static_cast<const MotionEvent*>(motion_event)->getXOffset();
128 }
129
AMotionEvent_getYOffset(const AInputEvent * motion_event)130 float AMotionEvent_getYOffset(const AInputEvent* motion_event) {
131 return static_cast<const MotionEvent*>(motion_event)->getYOffset();
132 }
133
AMotionEvent_getXPrecision(const AInputEvent * motion_event)134 float AMotionEvent_getXPrecision(const AInputEvent* motion_event) {
135 return static_cast<const MotionEvent*>(motion_event)->getXPrecision();
136 }
137
AMotionEvent_getYPrecision(const AInputEvent * motion_event)138 float AMotionEvent_getYPrecision(const AInputEvent* motion_event) {
139 return static_cast<const MotionEvent*>(motion_event)->getYPrecision();
140 }
141
AMotionEvent_getPointerCount(const AInputEvent * motion_event)142 size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) {
143 return static_cast<const MotionEvent*>(motion_event)->getPointerCount();
144 }
145
AMotionEvent_getPointerId(const AInputEvent * motion_event,size_t pointer_index)146 int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) {
147 return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index);
148 }
149
AMotionEvent_getToolType(const AInputEvent * motion_event,size_t pointer_index)150 int32_t AMotionEvent_getToolType(const AInputEvent* motion_event, size_t pointer_index) {
151 const MotionEvent& motion = static_cast<const MotionEvent&>(*motion_event);
152 return static_cast<int32_t>(motion.getToolType(pointer_index));
153 }
154
AMotionEvent_getRawX(const AInputEvent * motion_event,size_t pointer_index)155 float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) {
156 return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index);
157 }
158
AMotionEvent_getRawY(const AInputEvent * motion_event,size_t pointer_index)159 float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) {
160 return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index);
161 }
162
AMotionEvent_getX(const AInputEvent * motion_event,size_t pointer_index)163 float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) {
164 return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index);
165 }
166
AMotionEvent_getY(const AInputEvent * motion_event,size_t pointer_index)167 float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) {
168 return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index);
169 }
170
AMotionEvent_getPressure(const AInputEvent * motion_event,size_t pointer_index)171 float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) {
172 return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index);
173 }
174
AMotionEvent_getSize(const AInputEvent * motion_event,size_t pointer_index)175 float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) {
176 return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index);
177 }
178
AMotionEvent_getTouchMajor(const AInputEvent * motion_event,size_t pointer_index)179 float AMotionEvent_getTouchMajor(const AInputEvent* motion_event, size_t pointer_index) {
180 return static_cast<const MotionEvent*>(motion_event)->getTouchMajor(pointer_index);
181 }
182
AMotionEvent_getTouchMinor(const AInputEvent * motion_event,size_t pointer_index)183 float AMotionEvent_getTouchMinor(const AInputEvent* motion_event, size_t pointer_index) {
184 return static_cast<const MotionEvent*>(motion_event)->getTouchMinor(pointer_index);
185 }
186
AMotionEvent_getToolMajor(const AInputEvent * motion_event,size_t pointer_index)187 float AMotionEvent_getToolMajor(const AInputEvent* motion_event, size_t pointer_index) {
188 return static_cast<const MotionEvent*>(motion_event)->getToolMajor(pointer_index);
189 }
190
AMotionEvent_getToolMinor(const AInputEvent * motion_event,size_t pointer_index)191 float AMotionEvent_getToolMinor(const AInputEvent* motion_event, size_t pointer_index) {
192 return static_cast<const MotionEvent*>(motion_event)->getToolMinor(pointer_index);
193 }
194
AMotionEvent_getOrientation(const AInputEvent * motion_event,size_t pointer_index)195 float AMotionEvent_getOrientation(const AInputEvent* motion_event, size_t pointer_index) {
196 return static_cast<const MotionEvent*>(motion_event)->getOrientation(pointer_index);
197 }
198
AMotionEvent_getAxisValue(const AInputEvent * motion_event,int32_t axis,size_t pointer_index)199 float AMotionEvent_getAxisValue(const AInputEvent* motion_event,
200 int32_t axis, size_t pointer_index) {
201 return static_cast<const MotionEvent*>(motion_event)->getAxisValue(axis, pointer_index);
202 }
203
AMotionEvent_getHistorySize(const AInputEvent * motion_event)204 size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) {
205 return static_cast<const MotionEvent*>(motion_event)->getHistorySize();
206 }
207
AMotionEvent_getHistoricalEventTime(const AInputEvent * motion_event,size_t history_index)208 int64_t AMotionEvent_getHistoricalEventTime(const AInputEvent* motion_event,
209 size_t history_index) {
210 return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime(
211 history_index);
212 }
213
AMotionEvent_getHistoricalRawX(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)214 float AMotionEvent_getHistoricalRawX(const AInputEvent* motion_event, size_t pointer_index,
215 size_t history_index) {
216 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX(
217 pointer_index, history_index);
218 }
219
AMotionEvent_getHistoricalRawY(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)220 float AMotionEvent_getHistoricalRawY(const AInputEvent* motion_event, size_t pointer_index,
221 size_t history_index) {
222 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY(
223 pointer_index, history_index);
224 }
225
AMotionEvent_getHistoricalX(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)226 float AMotionEvent_getHistoricalX(const AInputEvent* motion_event, size_t pointer_index,
227 size_t history_index) {
228 return static_cast<const MotionEvent*>(motion_event)->getHistoricalX(
229 pointer_index, history_index);
230 }
231
AMotionEvent_getHistoricalY(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)232 float AMotionEvent_getHistoricalY(const AInputEvent* motion_event, size_t pointer_index,
233 size_t history_index) {
234 return static_cast<const MotionEvent*>(motion_event)->getHistoricalY(
235 pointer_index, history_index);
236 }
237
AMotionEvent_getHistoricalPressure(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)238 float AMotionEvent_getHistoricalPressure(const AInputEvent* motion_event, size_t pointer_index,
239 size_t history_index) {
240 return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure(
241 pointer_index, history_index);
242 }
243
AMotionEvent_getHistoricalSize(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)244 float AMotionEvent_getHistoricalSize(const AInputEvent* motion_event, size_t pointer_index,
245 size_t history_index) {
246 return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize(
247 pointer_index, history_index);
248 }
249
AMotionEvent_getHistoricalTouchMajor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)250 float AMotionEvent_getHistoricalTouchMajor(const AInputEvent* motion_event, size_t pointer_index,
251 size_t history_index) {
252 return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMajor(
253 pointer_index, history_index);
254 }
255
AMotionEvent_getHistoricalTouchMinor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)256 float AMotionEvent_getHistoricalTouchMinor(const AInputEvent* motion_event, size_t pointer_index,
257 size_t history_index) {
258 return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMinor(
259 pointer_index, history_index);
260 }
261
AMotionEvent_getHistoricalToolMajor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)262 float AMotionEvent_getHistoricalToolMajor(const AInputEvent* motion_event, size_t pointer_index,
263 size_t history_index) {
264 return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMajor(
265 pointer_index, history_index);
266 }
267
AMotionEvent_getHistoricalToolMinor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)268 float AMotionEvent_getHistoricalToolMinor(const AInputEvent* motion_event, size_t pointer_index,
269 size_t history_index) {
270 return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMinor(
271 pointer_index, history_index);
272 }
273
AMotionEvent_getHistoricalOrientation(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)274 float AMotionEvent_getHistoricalOrientation(const AInputEvent* motion_event, size_t pointer_index,
275 size_t history_index) {
276 return static_cast<const MotionEvent*>(motion_event)->getHistoricalOrientation(
277 pointer_index, history_index);
278 }
279
AMotionEvent_getHistoricalAxisValue(const AInputEvent * motion_event,int32_t axis,size_t pointer_index,size_t history_index)280 float AMotionEvent_getHistoricalAxisValue(const AInputEvent* motion_event,
281 int32_t axis, size_t pointer_index, size_t history_index) {
282 return static_cast<const MotionEvent*>(motion_event)->getHistoricalAxisValue(
283 axis, pointer_index, history_index);
284 }
285
AMotionEvent_getActionButton(const AInputEvent * motion_event)286 int32_t AMotionEvent_getActionButton(const AInputEvent* motion_event) {
287 return static_cast<const MotionEvent*>(motion_event)->getActionButton();
288 }
289
AMotionEvent_getClassification(const AInputEvent * motion_event)290 int32_t AMotionEvent_getClassification(const AInputEvent* motion_event) {
291 switch (static_cast<const MotionEvent*>(motion_event)->getClassification()) {
292 case android::MotionClassification::NONE:
293 return AMOTION_EVENT_CLASSIFICATION_NONE;
294 case android::MotionClassification::AMBIGUOUS_GESTURE:
295 return AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE;
296 case android::MotionClassification::DEEP_PRESS:
297 return AMOTION_EVENT_CLASSIFICATION_DEEP_PRESS;
298 case android::MotionClassification::TWO_FINGER_SWIPE:
299 return AMOTION_EVENT_CLASSIFICATION_TWO_FINGER_SWIPE;
300 case android::MotionClassification::MULTI_FINGER_SWIPE:
301 return AMOTION_EVENT_CLASSIFICATION_MULTI_FINGER_SWIPE;
302 case android::MotionClassification::PINCH:
303 return AMOTION_EVENT_CLASSIFICATION_PINCH;
304 }
305 }
306
AMotionEvent_fromJava(JNIEnv * env,jobject motionEvent)307 const AInputEvent* AMotionEvent_fromJava(JNIEnv* env, jobject motionEvent) {
308 MotionEvent* eventSrc = android::android_view_MotionEvent_getNativePtr(env, motionEvent);
309 if (eventSrc == nullptr) {
310 return nullptr;
311 }
312 MotionEvent* event = new MotionEvent();
313 event->copyFrom(eventSrc, true);
314 return event;
315 }
316
AInputQueue_attachLooper(AInputQueue * queue,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)317 void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
318 int ident, ALooper_callbackFunc callback, void* data) {
319 InputQueue* iq = static_cast<InputQueue*>(queue);
320 Looper* l = reinterpret_cast<Looper*>(looper);
321 iq->attachLooper(l, ident, callback, data);
322 }
323
AInputQueue_detachLooper(AInputQueue * queue)324 void AInputQueue_detachLooper(AInputQueue* queue) {
325 InputQueue* iq = static_cast<InputQueue*>(queue);
326 iq->detachLooper();
327 }
328
AInputQueue_hasEvents(AInputQueue * queue)329 int32_t AInputQueue_hasEvents(AInputQueue* queue) {
330 InputQueue* iq = static_cast<InputQueue*>(queue);
331 return iq->hasEvents();
332 }
333
AInputQueue_getEvent(AInputQueue * queue,AInputEvent ** outEvent)334 int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
335 InputQueue* iq = static_cast<InputQueue*>(queue);
336 InputEvent* event;
337 int32_t res = iq->getEvent(&event);
338 *outEvent = event;
339 return res;
340 }
341
AInputQueue_preDispatchEvent(AInputQueue * queue,AInputEvent * event)342 int32_t AInputQueue_preDispatchEvent(AInputQueue* queue, AInputEvent* event) {
343 InputQueue* iq = static_cast<InputQueue*>(queue);
344 InputEvent* e = static_cast<InputEvent*>(event);
345 return iq->preDispatchEvent(e) ? 1 : 0;
346 }
347
AInputQueue_finishEvent(AInputQueue * queue,AInputEvent * event,int handled)348 void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, int handled) {
349 InputQueue* iq = static_cast<InputQueue*>(queue);
350 InputEvent* e = static_cast<InputEvent*>(event);
351 iq->finishEvent(e, handled != 0);
352 }
353
AInputQueue_fromJava(JNIEnv * env,jobject inputQueue)354 AInputQueue* AInputQueue_fromJava(JNIEnv* env, jobject inputQueue) {
355 return android::android_view_InputQueue_getNativePtr(env, inputQueue);
356 }
357