• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::InputQueue;
37 using android::KeyEvent;
38 using android::Looper;
39 using android::MotionEvent;
40 using android::sp;
41 using android::Vector;
42 
AInputEvent_getType(const AInputEvent * event)43 int32_t AInputEvent_getType(const AInputEvent* event) {
44     return static_cast<const InputEvent*>(event)->getType();
45 }
46 
AInputEvent_getDeviceId(const AInputEvent * event)47 int32_t AInputEvent_getDeviceId(const AInputEvent* event) {
48     return static_cast<const InputEvent*>(event)->getDeviceId();
49 }
50 
AInputEvent_getSource(const AInputEvent * event)51 int32_t AInputEvent_getSource(const AInputEvent* event) {
52     return static_cast<const InputEvent*>(event)->getSource();
53 }
54 
AInputEvent_release(const AInputEvent * event)55 void AInputEvent_release(const AInputEvent* event) {
56     delete event;
57 }
58 
AKeyEvent_getAction(const AInputEvent * key_event)59 int32_t AKeyEvent_getAction(const AInputEvent* key_event) {
60     return static_cast<const KeyEvent*>(key_event)->getAction();
61 }
62 
AKeyEvent_getFlags(const AInputEvent * key_event)63 int32_t AKeyEvent_getFlags(const AInputEvent* key_event) {
64     return static_cast<const KeyEvent*>(key_event)->getFlags();
65 }
66 
AKeyEvent_getKeyCode(const AInputEvent * key_event)67 int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) {
68     return static_cast<const KeyEvent*>(key_event)->getKeyCode();
69 }
70 
AKeyEvent_getScanCode(const AInputEvent * key_event)71 int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) {
72     return static_cast<const KeyEvent*>(key_event)->getScanCode();
73 }
74 
AKeyEvent_getMetaState(const AInputEvent * key_event)75 int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) {
76     return static_cast<const KeyEvent*>(key_event)->getMetaState();
77 }
AKeyEvent_getRepeatCount(const AInputEvent * key_event)78 int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) {
79     return static_cast<const KeyEvent*>(key_event)->getRepeatCount();
80 }
81 
AKeyEvent_getDownTime(const AInputEvent * key_event)82 int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) {
83     return static_cast<const KeyEvent*>(key_event)->getDownTime();
84 }
85 
AKeyEvent_fromJava(JNIEnv * env,jobject keyEvent)86 const AInputEvent* AKeyEvent_fromJava(JNIEnv* env, jobject keyEvent) {
87     std::unique_ptr<KeyEvent> event = std::make_unique<KeyEvent>();
88     android::status_t ret = android::android_view_KeyEvent_toNative(env, keyEvent, event.get());
89     if (ret == android::OK) {
90         return event.release();
91     }
92     return nullptr;
93 }
94 
AKeyEvent_getEventTime(const AInputEvent * key_event)95 int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) {
96     return static_cast<const KeyEvent*>(key_event)->getEventTime();
97 }
98 
AMotionEvent_getAction(const AInputEvent * motion_event)99 int32_t AMotionEvent_getAction(const AInputEvent* motion_event) {
100     return static_cast<const MotionEvent*>(motion_event)->getAction();
101 }
102 
AMotionEvent_getFlags(const AInputEvent * motion_event)103 int32_t AMotionEvent_getFlags(const AInputEvent* motion_event) {
104     return static_cast<const MotionEvent*>(motion_event)->getFlags();
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)->getXOffset();
129 }
130 
AMotionEvent_getYOffset(const AInputEvent * motion_event)131 float AMotionEvent_getYOffset(const AInputEvent* motion_event) {
132     return static_cast<const MotionEvent*>(motion_event)->getYOffset();
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     return static_cast<const MotionEvent*>(motion_event)->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_fromJava(JNIEnv * env,jobject motionEvent)286 const AInputEvent* AMotionEvent_fromJava(JNIEnv* env, jobject motionEvent) {
287     MotionEvent* eventSrc = android::android_view_MotionEvent_getNativePtr(env, motionEvent);
288     if (eventSrc == nullptr) {
289         return nullptr;
290     }
291     MotionEvent* event = new MotionEvent();
292     event->copyFrom(eventSrc, true);
293     return event;
294 }
295 
AInputQueue_attachLooper(AInputQueue * queue,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)296 void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
297         int ident, ALooper_callbackFunc callback, void* data) {
298     InputQueue* iq = static_cast<InputQueue*>(queue);
299     Looper* l = reinterpret_cast<Looper*>(looper);
300     iq->attachLooper(l, ident, callback, data);
301 }
302 
AInputQueue_detachLooper(AInputQueue * queue)303 void AInputQueue_detachLooper(AInputQueue* queue) {
304     InputQueue* iq = static_cast<InputQueue*>(queue);
305     iq->detachLooper();
306 }
307 
AInputQueue_hasEvents(AInputQueue * queue)308 int32_t AInputQueue_hasEvents(AInputQueue* queue) {
309     InputQueue* iq = static_cast<InputQueue*>(queue);
310     return iq->hasEvents();
311 }
312 
AInputQueue_getEvent(AInputQueue * queue,AInputEvent ** outEvent)313 int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
314     InputQueue* iq = static_cast<InputQueue*>(queue);
315     InputEvent* event;
316     int32_t res = iq->getEvent(&event);
317     *outEvent = event;
318     return res;
319 }
320 
AInputQueue_preDispatchEvent(AInputQueue * queue,AInputEvent * event)321 int32_t AInputQueue_preDispatchEvent(AInputQueue* queue, AInputEvent* event) {
322     InputQueue* iq = static_cast<InputQueue*>(queue);
323     InputEvent* e = static_cast<InputEvent*>(event);
324     return iq->preDispatchEvent(e) ? 1 : 0;
325 }
326 
AInputQueue_finishEvent(AInputQueue * queue,AInputEvent * event,int handled)327 void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, int handled) {
328     InputQueue* iq = static_cast<InputQueue*>(queue);
329     InputEvent* e = static_cast<InputEvent*>(event);
330     iq->finishEvent(e, handled != 0);
331 }
332