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_obtainAsCopy(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 ~AMOTION_EVENT_PRIVATE_FLAG_MASK;
105 }
106
AMotionEvent_getMetaState(const AInputEvent * motion_event)107 int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) {
108 return static_cast<const MotionEvent*>(motion_event)->getMetaState();
109 }
110
AMotionEvent_getButtonState(const AInputEvent * motion_event)111 int32_t AMotionEvent_getButtonState(const AInputEvent* motion_event) {
112 return static_cast<const MotionEvent*>(motion_event)->getButtonState();
113 }
114
AMotionEvent_getEdgeFlags(const AInputEvent * motion_event)115 int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) {
116 return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags();
117 }
118
AMotionEvent_getDownTime(const AInputEvent * motion_event)119 int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) {
120 return static_cast<const MotionEvent*>(motion_event)->getDownTime();
121 }
122
AMotionEvent_getEventTime(const AInputEvent * motion_event)123 int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) {
124 return static_cast<const MotionEvent*>(motion_event)->getEventTime();
125 }
126
AMotionEvent_getXOffset(const AInputEvent * motion_event)127 float AMotionEvent_getXOffset(const AInputEvent* motion_event) {
128 return static_cast<const MotionEvent*>(motion_event)->getRawXOffset();
129 }
130
AMotionEvent_getYOffset(const AInputEvent * motion_event)131 float AMotionEvent_getYOffset(const AInputEvent* motion_event) {
132 return static_cast<const MotionEvent*>(motion_event)->getRawYOffset();
133 }
134
AMotionEvent_getXPrecision(const AInputEvent * motion_event)135 float AMotionEvent_getXPrecision(const AInputEvent* motion_event) {
136 return static_cast<const MotionEvent*>(motion_event)->getXPrecision();
137 }
138
AMotionEvent_getYPrecision(const AInputEvent * motion_event)139 float AMotionEvent_getYPrecision(const AInputEvent* motion_event) {
140 return static_cast<const MotionEvent*>(motion_event)->getYPrecision();
141 }
142
AMotionEvent_getPointerCount(const AInputEvent * motion_event)143 size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) {
144 return static_cast<const MotionEvent*>(motion_event)->getPointerCount();
145 }
146
AMotionEvent_getPointerId(const AInputEvent * motion_event,size_t pointer_index)147 int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) {
148 return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index);
149 }
150
AMotionEvent_getToolType(const AInputEvent * motion_event,size_t pointer_index)151 int32_t AMotionEvent_getToolType(const AInputEvent* motion_event, size_t pointer_index) {
152 const MotionEvent& motion = static_cast<const MotionEvent&>(*motion_event);
153 return static_cast<int32_t>(motion.getToolType(pointer_index));
154 }
155
AMotionEvent_getRawX(const AInputEvent * motion_event,size_t pointer_index)156 float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) {
157 return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index);
158 }
159
AMotionEvent_getRawY(const AInputEvent * motion_event,size_t pointer_index)160 float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) {
161 return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index);
162 }
163
AMotionEvent_getX(const AInputEvent * motion_event,size_t pointer_index)164 float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) {
165 return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index);
166 }
167
AMotionEvent_getY(const AInputEvent * motion_event,size_t pointer_index)168 float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) {
169 return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index);
170 }
171
AMotionEvent_getPressure(const AInputEvent * motion_event,size_t pointer_index)172 float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) {
173 return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index);
174 }
175
AMotionEvent_getSize(const AInputEvent * motion_event,size_t pointer_index)176 float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) {
177 return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index);
178 }
179
AMotionEvent_getTouchMajor(const AInputEvent * motion_event,size_t pointer_index)180 float AMotionEvent_getTouchMajor(const AInputEvent* motion_event, size_t pointer_index) {
181 return static_cast<const MotionEvent*>(motion_event)->getTouchMajor(pointer_index);
182 }
183
AMotionEvent_getTouchMinor(const AInputEvent * motion_event,size_t pointer_index)184 float AMotionEvent_getTouchMinor(const AInputEvent* motion_event, size_t pointer_index) {
185 return static_cast<const MotionEvent*>(motion_event)->getTouchMinor(pointer_index);
186 }
187
AMotionEvent_getToolMajor(const AInputEvent * motion_event,size_t pointer_index)188 float AMotionEvent_getToolMajor(const AInputEvent* motion_event, size_t pointer_index) {
189 return static_cast<const MotionEvent*>(motion_event)->getToolMajor(pointer_index);
190 }
191
AMotionEvent_getToolMinor(const AInputEvent * motion_event,size_t pointer_index)192 float AMotionEvent_getToolMinor(const AInputEvent* motion_event, size_t pointer_index) {
193 return static_cast<const MotionEvent*>(motion_event)->getToolMinor(pointer_index);
194 }
195
AMotionEvent_getOrientation(const AInputEvent * motion_event,size_t pointer_index)196 float AMotionEvent_getOrientation(const AInputEvent* motion_event, size_t pointer_index) {
197 return static_cast<const MotionEvent*>(motion_event)->getOrientation(pointer_index);
198 }
199
AMotionEvent_getAxisValue(const AInputEvent * motion_event,int32_t axis,size_t pointer_index)200 float AMotionEvent_getAxisValue(const AInputEvent* motion_event,
201 int32_t axis, size_t pointer_index) {
202 return static_cast<const MotionEvent*>(motion_event)->getAxisValue(axis, pointer_index);
203 }
204
AMotionEvent_getHistorySize(const AInputEvent * motion_event)205 size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) {
206 return static_cast<const MotionEvent*>(motion_event)->getHistorySize();
207 }
208
AMotionEvent_getHistoricalEventTime(const AInputEvent * motion_event,size_t history_index)209 int64_t AMotionEvent_getHistoricalEventTime(const AInputEvent* motion_event,
210 size_t history_index) {
211 return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime(
212 history_index);
213 }
214
AMotionEvent_getHistoricalRawX(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)215 float AMotionEvent_getHistoricalRawX(const AInputEvent* motion_event, size_t pointer_index,
216 size_t history_index) {
217 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX(
218 pointer_index, history_index);
219 }
220
AMotionEvent_getHistoricalRawY(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)221 float AMotionEvent_getHistoricalRawY(const AInputEvent* motion_event, size_t pointer_index,
222 size_t history_index) {
223 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY(
224 pointer_index, history_index);
225 }
226
AMotionEvent_getHistoricalX(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)227 float AMotionEvent_getHistoricalX(const AInputEvent* motion_event, size_t pointer_index,
228 size_t history_index) {
229 return static_cast<const MotionEvent*>(motion_event)->getHistoricalX(
230 pointer_index, history_index);
231 }
232
AMotionEvent_getHistoricalY(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)233 float AMotionEvent_getHistoricalY(const AInputEvent* motion_event, size_t pointer_index,
234 size_t history_index) {
235 return static_cast<const MotionEvent*>(motion_event)->getHistoricalY(
236 pointer_index, history_index);
237 }
238
AMotionEvent_getHistoricalPressure(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)239 float AMotionEvent_getHistoricalPressure(const AInputEvent* motion_event, size_t pointer_index,
240 size_t history_index) {
241 return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure(
242 pointer_index, history_index);
243 }
244
AMotionEvent_getHistoricalSize(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)245 float AMotionEvent_getHistoricalSize(const AInputEvent* motion_event, size_t pointer_index,
246 size_t history_index) {
247 return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize(
248 pointer_index, history_index);
249 }
250
AMotionEvent_getHistoricalTouchMajor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)251 float AMotionEvent_getHistoricalTouchMajor(const AInputEvent* motion_event, size_t pointer_index,
252 size_t history_index) {
253 return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMajor(
254 pointer_index, history_index);
255 }
256
AMotionEvent_getHistoricalTouchMinor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)257 float AMotionEvent_getHistoricalTouchMinor(const AInputEvent* motion_event, size_t pointer_index,
258 size_t history_index) {
259 return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMinor(
260 pointer_index, history_index);
261 }
262
AMotionEvent_getHistoricalToolMajor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)263 float AMotionEvent_getHistoricalToolMajor(const AInputEvent* motion_event, size_t pointer_index,
264 size_t history_index) {
265 return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMajor(
266 pointer_index, history_index);
267 }
268
AMotionEvent_getHistoricalToolMinor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)269 float AMotionEvent_getHistoricalToolMinor(const AInputEvent* motion_event, size_t pointer_index,
270 size_t history_index) {
271 return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMinor(
272 pointer_index, history_index);
273 }
274
AMotionEvent_getHistoricalOrientation(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)275 float AMotionEvent_getHistoricalOrientation(const AInputEvent* motion_event, size_t pointer_index,
276 size_t history_index) {
277 return static_cast<const MotionEvent*>(motion_event)->getHistoricalOrientation(
278 pointer_index, history_index);
279 }
280
AMotionEvent_getHistoricalAxisValue(const AInputEvent * motion_event,int32_t axis,size_t pointer_index,size_t history_index)281 float AMotionEvent_getHistoricalAxisValue(const AInputEvent* motion_event,
282 int32_t axis, size_t pointer_index, size_t history_index) {
283 return static_cast<const MotionEvent*>(motion_event)->getHistoricalAxisValue(
284 axis, pointer_index, history_index);
285 }
286
AMotionEvent_getActionButton(const AInputEvent * motion_event)287 int32_t AMotionEvent_getActionButton(const AInputEvent* motion_event) {
288 return static_cast<const MotionEvent*>(motion_event)->getActionButton();
289 }
290
AMotionEvent_getClassification(const AInputEvent * motion_event)291 int32_t AMotionEvent_getClassification(const AInputEvent* motion_event) {
292 switch (static_cast<const MotionEvent*>(motion_event)->getClassification()) {
293 case android::MotionClassification::NONE:
294 return AMOTION_EVENT_CLASSIFICATION_NONE;
295 case android::MotionClassification::AMBIGUOUS_GESTURE:
296 return AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE;
297 case android::MotionClassification::DEEP_PRESS:
298 return AMOTION_EVENT_CLASSIFICATION_DEEP_PRESS;
299 case android::MotionClassification::TWO_FINGER_SWIPE:
300 return AMOTION_EVENT_CLASSIFICATION_TWO_FINGER_SWIPE;
301 case android::MotionClassification::MULTI_FINGER_SWIPE:
302 return AMOTION_EVENT_CLASSIFICATION_MULTI_FINGER_SWIPE;
303 case android::MotionClassification::PINCH:
304 return AMOTION_EVENT_CLASSIFICATION_PINCH;
305 }
306 }
307
AMotionEvent_fromJava(JNIEnv * env,jobject motionEvent)308 const AInputEvent* AMotionEvent_fromJava(JNIEnv* env, jobject motionEvent) {
309 MotionEvent* eventSrc = android::android_view_MotionEvent_getNativePtr(env, motionEvent);
310 if (eventSrc == nullptr) {
311 return nullptr;
312 }
313 MotionEvent* event = new MotionEvent();
314 event->copyFrom(eventSrc, true);
315 return event;
316 }
317
AInputEvent_toJava(JNIEnv * env,const AInputEvent * aInputEvent)318 jobject AInputEvent_toJava(JNIEnv* env, const AInputEvent* aInputEvent) {
319 LOG_ALWAYS_FATAL_IF(aInputEvent == nullptr, "Expected aInputEvent to be non-null");
320 const int32_t eventType = AInputEvent_getType(aInputEvent);
321 switch (eventType) {
322 case AINPUT_EVENT_TYPE_MOTION:
323 return android::android_view_MotionEvent_obtainAsCopy(env,
324 static_cast<const MotionEvent&>(
325 *aInputEvent))
326 .release();
327 case AINPUT_EVENT_TYPE_KEY:
328 return android::android_view_KeyEvent_obtainAsCopy(env,
329 static_cast<const KeyEvent&>(
330 *aInputEvent))
331 .release();
332 default:
333 LOG_ALWAYS_FATAL("Unexpected event type %d in AInputEvent_toJava.", eventType);
334 }
335 }
336
AInputQueue_attachLooper(AInputQueue * queue,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)337 void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
338 int ident, ALooper_callbackFunc callback, void* data) {
339 InputQueue* iq = static_cast<InputQueue*>(queue);
340 Looper* l = reinterpret_cast<Looper*>(looper);
341 iq->attachLooper(l, ident, callback, data);
342 }
343
AInputQueue_detachLooper(AInputQueue * queue)344 void AInputQueue_detachLooper(AInputQueue* queue) {
345 InputQueue* iq = static_cast<InputQueue*>(queue);
346 iq->detachLooper();
347 }
348
AInputQueue_hasEvents(AInputQueue * queue)349 int32_t AInputQueue_hasEvents(AInputQueue* queue) {
350 InputQueue* iq = static_cast<InputQueue*>(queue);
351 return iq->hasEvents();
352 }
353
AInputQueue_getEvent(AInputQueue * queue,AInputEvent ** outEvent)354 int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
355 InputQueue* iq = static_cast<InputQueue*>(queue);
356 InputEvent* event;
357 int32_t res = iq->getEvent(&event);
358 *outEvent = event;
359 return res;
360 }
361
AInputQueue_preDispatchEvent(AInputQueue * queue,AInputEvent * event)362 int32_t AInputQueue_preDispatchEvent(AInputQueue* queue, AInputEvent* event) {
363 InputQueue* iq = static_cast<InputQueue*>(queue);
364 InputEvent* e = static_cast<InputEvent*>(event);
365 return iq->preDispatchEvent(e) ? 1 : 0;
366 }
367
AInputQueue_finishEvent(AInputQueue * queue,AInputEvent * event,int handled)368 void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, int handled) {
369 InputQueue* iq = static_cast<InputQueue*>(queue);
370 InputEvent* e = static_cast<InputEvent*>(event);
371 iq->finishEvent(e, handled != 0);
372 }
373
AInputQueue_fromJava(JNIEnv * env,jobject inputQueue)374 AInputQueue* AInputQueue_fromJava(JNIEnv* env, jobject inputQueue) {
375 return android::android_view_InputQueue_getNativePtr(env, inputQueue);
376 }
377