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 
30 #include <poll.h>
31 #include <errno.h>
32 
33 using android::InputEvent;
34 using android::InputQueue;
35 using android::KeyEvent;
36 using android::Looper;
37 using android::MotionEvent;
38 using android::sp;
39 using android::Vector;
40 
AInputEvent_getType(const AInputEvent * event)41 int32_t AInputEvent_getType(const AInputEvent* event) {
42     return static_cast<const InputEvent*>(event)->getType();
43 }
44 
AInputEvent_getDeviceId(const AInputEvent * event)45 int32_t AInputEvent_getDeviceId(const AInputEvent* event) {
46     return static_cast<const InputEvent*>(event)->getDeviceId();
47 }
48 
AInputEvent_getSource(const AInputEvent * event)49 int32_t AInputEvent_getSource(const AInputEvent* event) {
50     return static_cast<const InputEvent*>(event)->getSource();
51 }
52 
AKeyEvent_getAction(const AInputEvent * key_event)53 int32_t AKeyEvent_getAction(const AInputEvent* key_event) {
54     return static_cast<const KeyEvent*>(key_event)->getAction();
55 }
56 
AKeyEvent_getFlags(const AInputEvent * key_event)57 int32_t AKeyEvent_getFlags(const AInputEvent* key_event) {
58     return static_cast<const KeyEvent*>(key_event)->getFlags();
59 }
60 
AKeyEvent_getKeyCode(const AInputEvent * key_event)61 int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) {
62     return static_cast<const KeyEvent*>(key_event)->getKeyCode();
63 }
64 
AKeyEvent_getScanCode(const AInputEvent * key_event)65 int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) {
66     return static_cast<const KeyEvent*>(key_event)->getScanCode();
67 }
68 
AKeyEvent_getMetaState(const AInputEvent * key_event)69 int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) {
70     return static_cast<const KeyEvent*>(key_event)->getMetaState();
71 }
AKeyEvent_getRepeatCount(const AInputEvent * key_event)72 int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) {
73     return static_cast<const KeyEvent*>(key_event)->getRepeatCount();
74 }
75 
AKeyEvent_getDownTime(const AInputEvent * key_event)76 int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) {
77     return static_cast<const KeyEvent*>(key_event)->getDownTime();
78 }
79 
80 
AKeyEvent_getEventTime(const AInputEvent * key_event)81 int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) {
82     return static_cast<const KeyEvent*>(key_event)->getEventTime();
83 }
84 
AMotionEvent_getAction(const AInputEvent * motion_event)85 int32_t AMotionEvent_getAction(const AInputEvent* motion_event) {
86     return static_cast<const MotionEvent*>(motion_event)->getAction();
87 }
88 
AMotionEvent_getFlags(const AInputEvent * motion_event)89 int32_t AMotionEvent_getFlags(const AInputEvent* motion_event) {
90     return static_cast<const MotionEvent*>(motion_event)->getFlags();
91 }
92 
AMotionEvent_getMetaState(const AInputEvent * motion_event)93 int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) {
94     return static_cast<const MotionEvent*>(motion_event)->getMetaState();
95 }
96 
AMotionEvent_getButtonState(const AInputEvent * motion_event)97 int32_t AMotionEvent_getButtonState(const AInputEvent* motion_event) {
98     return static_cast<const MotionEvent*>(motion_event)->getButtonState();
99 }
100 
AMotionEvent_getEdgeFlags(const AInputEvent * motion_event)101 int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) {
102     return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags();
103 }
104 
AMotionEvent_getDownTime(const AInputEvent * motion_event)105 int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) {
106     return static_cast<const MotionEvent*>(motion_event)->getDownTime();
107 }
108 
AMotionEvent_getEventTime(const AInputEvent * motion_event)109 int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) {
110     return static_cast<const MotionEvent*>(motion_event)->getEventTime();
111 }
112 
AMotionEvent_getXOffset(const AInputEvent * motion_event)113 float AMotionEvent_getXOffset(const AInputEvent* motion_event) {
114     return static_cast<const MotionEvent*>(motion_event)->getXOffset();
115 }
116 
AMotionEvent_getYOffset(const AInputEvent * motion_event)117 float AMotionEvent_getYOffset(const AInputEvent* motion_event) {
118     return static_cast<const MotionEvent*>(motion_event)->getYOffset();
119 }
120 
AMotionEvent_getXPrecision(const AInputEvent * motion_event)121 float AMotionEvent_getXPrecision(const AInputEvent* motion_event) {
122     return static_cast<const MotionEvent*>(motion_event)->getXPrecision();
123 }
124 
AMotionEvent_getYPrecision(const AInputEvent * motion_event)125 float AMotionEvent_getYPrecision(const AInputEvent* motion_event) {
126     return static_cast<const MotionEvent*>(motion_event)->getYPrecision();
127 }
128 
AMotionEvent_getPointerCount(const AInputEvent * motion_event)129 size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) {
130     return static_cast<const MotionEvent*>(motion_event)->getPointerCount();
131 }
132 
AMotionEvent_getPointerId(const AInputEvent * motion_event,size_t pointer_index)133 int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) {
134     return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index);
135 }
136 
AMotionEvent_getToolType(const AInputEvent * motion_event,size_t pointer_index)137 int32_t AMotionEvent_getToolType(const AInputEvent* motion_event, size_t pointer_index) {
138     return static_cast<const MotionEvent*>(motion_event)->getToolType(pointer_index);
139 }
140 
AMotionEvent_getRawX(const AInputEvent * motion_event,size_t pointer_index)141 float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) {
142     return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index);
143 }
144 
AMotionEvent_getRawY(const AInputEvent * motion_event,size_t pointer_index)145 float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) {
146     return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index);
147 }
148 
AMotionEvent_getX(const AInputEvent * motion_event,size_t pointer_index)149 float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) {
150     return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index);
151 }
152 
AMotionEvent_getY(const AInputEvent * motion_event,size_t pointer_index)153 float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) {
154     return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index);
155 }
156 
AMotionEvent_getPressure(const AInputEvent * motion_event,size_t pointer_index)157 float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) {
158     return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index);
159 }
160 
AMotionEvent_getSize(const AInputEvent * motion_event,size_t pointer_index)161 float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) {
162     return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index);
163 }
164 
AMotionEvent_getTouchMajor(const AInputEvent * motion_event,size_t pointer_index)165 float AMotionEvent_getTouchMajor(const AInputEvent* motion_event, size_t pointer_index) {
166     return static_cast<const MotionEvent*>(motion_event)->getTouchMajor(pointer_index);
167 }
168 
AMotionEvent_getTouchMinor(const AInputEvent * motion_event,size_t pointer_index)169 float AMotionEvent_getTouchMinor(const AInputEvent* motion_event, size_t pointer_index) {
170     return static_cast<const MotionEvent*>(motion_event)->getTouchMinor(pointer_index);
171 }
172 
AMotionEvent_getToolMajor(const AInputEvent * motion_event,size_t pointer_index)173 float AMotionEvent_getToolMajor(const AInputEvent* motion_event, size_t pointer_index) {
174     return static_cast<const MotionEvent*>(motion_event)->getToolMajor(pointer_index);
175 }
176 
AMotionEvent_getToolMinor(const AInputEvent * motion_event,size_t pointer_index)177 float AMotionEvent_getToolMinor(const AInputEvent* motion_event, size_t pointer_index) {
178     return static_cast<const MotionEvent*>(motion_event)->getToolMinor(pointer_index);
179 }
180 
AMotionEvent_getOrientation(const AInputEvent * motion_event,size_t pointer_index)181 float AMotionEvent_getOrientation(const AInputEvent* motion_event, size_t pointer_index) {
182     return static_cast<const MotionEvent*>(motion_event)->getOrientation(pointer_index);
183 }
184 
AMotionEvent_getAxisValue(const AInputEvent * motion_event,int32_t axis,size_t pointer_index)185 float AMotionEvent_getAxisValue(const AInputEvent* motion_event,
186         int32_t axis, size_t pointer_index) {
187     return static_cast<const MotionEvent*>(motion_event)->getAxisValue(axis, pointer_index);
188 }
189 
AMotionEvent_getHistorySize(const AInputEvent * motion_event)190 size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) {
191     return static_cast<const MotionEvent*>(motion_event)->getHistorySize();
192 }
193 
AMotionEvent_getHistoricalEventTime(const AInputEvent * motion_event,size_t history_index)194 int64_t AMotionEvent_getHistoricalEventTime(const AInputEvent* motion_event,
195         size_t history_index) {
196     return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime(
197             history_index);
198 }
199 
AMotionEvent_getHistoricalRawX(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)200 float AMotionEvent_getHistoricalRawX(const AInputEvent* motion_event, size_t pointer_index,
201         size_t history_index) {
202     return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX(
203             pointer_index, history_index);
204 }
205 
AMotionEvent_getHistoricalRawY(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)206 float AMotionEvent_getHistoricalRawY(const AInputEvent* motion_event, size_t pointer_index,
207         size_t history_index) {
208     return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY(
209             pointer_index, history_index);
210 }
211 
AMotionEvent_getHistoricalX(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)212 float AMotionEvent_getHistoricalX(const AInputEvent* motion_event, size_t pointer_index,
213         size_t history_index) {
214     return static_cast<const MotionEvent*>(motion_event)->getHistoricalX(
215             pointer_index, history_index);
216 }
217 
AMotionEvent_getHistoricalY(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)218 float AMotionEvent_getHistoricalY(const AInputEvent* motion_event, size_t pointer_index,
219         size_t history_index) {
220     return static_cast<const MotionEvent*>(motion_event)->getHistoricalY(
221             pointer_index, history_index);
222 }
223 
AMotionEvent_getHistoricalPressure(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)224 float AMotionEvent_getHistoricalPressure(const AInputEvent* motion_event, size_t pointer_index,
225         size_t history_index) {
226     return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure(
227             pointer_index, history_index);
228 }
229 
AMotionEvent_getHistoricalSize(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)230 float AMotionEvent_getHistoricalSize(const AInputEvent* motion_event, size_t pointer_index,
231         size_t history_index) {
232     return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize(
233             pointer_index, history_index);
234 }
235 
AMotionEvent_getHistoricalTouchMajor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)236 float AMotionEvent_getHistoricalTouchMajor(const AInputEvent* motion_event, size_t pointer_index,
237         size_t history_index) {
238     return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMajor(
239             pointer_index, history_index);
240 }
241 
AMotionEvent_getHistoricalTouchMinor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)242 float AMotionEvent_getHistoricalTouchMinor(const AInputEvent* motion_event, size_t pointer_index,
243         size_t history_index) {
244     return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMinor(
245             pointer_index, history_index);
246 }
247 
AMotionEvent_getHistoricalToolMajor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)248 float AMotionEvent_getHistoricalToolMajor(const AInputEvent* motion_event, size_t pointer_index,
249         size_t history_index) {
250     return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMajor(
251             pointer_index, history_index);
252 }
253 
AMotionEvent_getHistoricalToolMinor(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)254 float AMotionEvent_getHistoricalToolMinor(const AInputEvent* motion_event, size_t pointer_index,
255         size_t history_index) {
256     return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMinor(
257             pointer_index, history_index);
258 }
259 
AMotionEvent_getHistoricalOrientation(const AInputEvent * motion_event,size_t pointer_index,size_t history_index)260 float AMotionEvent_getHistoricalOrientation(const AInputEvent* motion_event, size_t pointer_index,
261         size_t history_index) {
262     return static_cast<const MotionEvent*>(motion_event)->getHistoricalOrientation(
263             pointer_index, history_index);
264 }
265 
AMotionEvent_getHistoricalAxisValue(const AInputEvent * motion_event,int32_t axis,size_t pointer_index,size_t history_index)266 float AMotionEvent_getHistoricalAxisValue(const AInputEvent* motion_event,
267         int32_t axis, size_t pointer_index, size_t history_index) {
268     return static_cast<const MotionEvent*>(motion_event)->getHistoricalAxisValue(
269             axis, pointer_index, history_index);
270 }
271 
272 
AInputQueue_attachLooper(AInputQueue * queue,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)273 void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
274         int ident, ALooper_callbackFunc callback, void* data) {
275     InputQueue* iq = static_cast<InputQueue*>(queue);
276     Looper* l = reinterpret_cast<Looper*>(looper);
277     iq->attachLooper(l, ident, callback, data);
278 }
279 
AInputQueue_detachLooper(AInputQueue * queue)280 void AInputQueue_detachLooper(AInputQueue* queue) {
281     InputQueue* iq = static_cast<InputQueue*>(queue);
282     iq->detachLooper();
283 }
284 
AInputQueue_hasEvents(AInputQueue * queue)285 int32_t AInputQueue_hasEvents(AInputQueue* queue) {
286     InputQueue* iq = static_cast<InputQueue*>(queue);
287     return iq->hasEvents();
288 }
289 
AInputQueue_getEvent(AInputQueue * queue,AInputEvent ** outEvent)290 int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
291     InputQueue* iq = static_cast<InputQueue*>(queue);
292     InputEvent* event;
293     int32_t res = iq->getEvent(&event);
294     *outEvent = event;
295     return res;
296 }
297 
AInputQueue_preDispatchEvent(AInputQueue * queue,AInputEvent * event)298 int32_t AInputQueue_preDispatchEvent(AInputQueue* queue, AInputEvent* event) {
299     InputQueue* iq = static_cast<InputQueue*>(queue);
300     InputEvent* e = static_cast<InputEvent*>(event);
301     return iq->preDispatchEvent(e) ? 1 : 0;
302 }
303 
AInputQueue_finishEvent(AInputQueue * queue,AInputEvent * event,int handled)304 void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, int handled) {
305     InputQueue* iq = static_cast<InputQueue*>(queue);
306     InputEvent* e = static_cast<InputEvent*>(event);
307     iq->finishEvent(e, handled != 0);
308 }
309