input.cpp revision 6d0fec2de3601821f4f44eeb7d7deedebb2b7117
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 <ui/Input.h>
22#include <ui/InputTransport.h>
23#include <utils/PollLoop.h>
24#include <utils/RefBase.h>
25#include <utils/Vector.h>
26
27#include <android_runtime/android_app_NativeActivity.h>
28
29#include <poll.h>
30#include <errno.h>
31
32using android::InputEvent;
33using android::KeyEvent;
34using android::MotionEvent;
35using android::InputDeviceInfo;
36using android::InputDeviceProxy;
37using android::sp;
38using android::Vector;
39
40int32_t AInputEvent_getType(const AInputEvent* event) {
41    return static_cast<const InputEvent*>(event)->getType();
42}
43
44int32_t AInputEvent_getDeviceId(const AInputEvent* event) {
45    return static_cast<const InputEvent*>(event)->getDeviceId();
46}
47
48int32_t AInputEvent_getSource(const AInputEvent* event) {
49    return static_cast<const InputEvent*>(event)->getSource();
50}
51
52int32_t AKeyEvent_getAction(const AInputEvent* key_event) {
53    return static_cast<const KeyEvent*>(key_event)->getAction();
54}
55
56int32_t AKeyEvent_getFlags(const AInputEvent* key_event) {
57    return static_cast<const KeyEvent*>(key_event)->getFlags();
58}
59
60int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) {
61    return static_cast<const KeyEvent*>(key_event)->getKeyCode();
62}
63
64int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) {
65    return static_cast<const KeyEvent*>(key_event)->getScanCode();
66}
67
68int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) {
69    return static_cast<const KeyEvent*>(key_event)->getMetaState();
70}
71int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) {
72    return static_cast<const KeyEvent*>(key_event)->getRepeatCount();
73}
74
75int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) {
76    return static_cast<const KeyEvent*>(key_event)->getDownTime();
77}
78
79
80int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) {
81    return static_cast<const KeyEvent*>(key_event)->getEventTime();
82}
83
84int32_t AMotionEvent_getAction(const AInputEvent* motion_event) {
85    return static_cast<const MotionEvent*>(motion_event)->getAction();
86}
87
88int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) {
89    return static_cast<const MotionEvent*>(motion_event)->getMetaState();
90}
91
92int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) {
93    return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags();
94}
95
96int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) {
97    return static_cast<const MotionEvent*>(motion_event)->getDownTime();
98}
99
100int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) {
101    return static_cast<const MotionEvent*>(motion_event)->getEventTime();
102}
103
104float AMotionEvent_getXOffset(const AInputEvent* motion_event) {
105    return static_cast<const MotionEvent*>(motion_event)->getXOffset();
106}
107
108float AMotionEvent_getYOffset(const AInputEvent* motion_event) {
109    return static_cast<const MotionEvent*>(motion_event)->getYOffset();
110}
111
112float AMotionEvent_getXPrecision(const AInputEvent* motion_event) {
113    return static_cast<const MotionEvent*>(motion_event)->getXPrecision();
114}
115
116float AMotionEvent_getYPrecision(const AInputEvent* motion_event) {
117    return static_cast<const MotionEvent*>(motion_event)->getYPrecision();
118}
119
120size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) {
121    return static_cast<const MotionEvent*>(motion_event)->getPointerCount();
122}
123
124int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) {
125    return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index);
126}
127
128float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) {
129    return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index);
130}
131
132float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) {
133    return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index);
134}
135
136float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) {
137    return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index);
138}
139
140float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) {
141    return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index);
142}
143
144float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) {
145    return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index);
146}
147
148float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) {
149    return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index);
150}
151
152float AMotionEvent_getTouchMajor(const AInputEvent* motion_event, size_t pointer_index) {
153    return static_cast<const MotionEvent*>(motion_event)->getTouchMajor(pointer_index);
154}
155
156float AMotionEvent_getTouchMinor(const AInputEvent* motion_event, size_t pointer_index) {
157    return static_cast<const MotionEvent*>(motion_event)->getTouchMinor(pointer_index);
158}
159
160float AMotionEvent_getToolMajor(const AInputEvent* motion_event, size_t pointer_index) {
161    return static_cast<const MotionEvent*>(motion_event)->getToolMajor(pointer_index);
162}
163
164float AMotionEvent_getToolMinor(const AInputEvent* motion_event, size_t pointer_index) {
165    return static_cast<const MotionEvent*>(motion_event)->getToolMinor(pointer_index);
166}
167
168float AMotionEvent_getOrientation(const AInputEvent* motion_event, size_t pointer_index) {
169    return static_cast<const MotionEvent*>(motion_event)->getOrientation(pointer_index);
170}
171
172size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) {
173    return static_cast<const MotionEvent*>(motion_event)->getHistorySize();
174}
175
176int64_t AMotionEvent_getHistoricalEventTime(AInputEvent* motion_event,
177        size_t history_index) {
178    return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime(
179            history_index);
180}
181
182float AMotionEvent_getHistoricalRawX(AInputEvent* motion_event, size_t pointer_index,
183        size_t history_index) {
184    return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX(
185            pointer_index, history_index);
186}
187
188float AMotionEvent_getHistoricalRawY(AInputEvent* motion_event, size_t pointer_index,
189        size_t history_index) {
190    return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY(
191            pointer_index, history_index);
192}
193
194float AMotionEvent_getHistoricalX(AInputEvent* motion_event, size_t pointer_index,
195        size_t history_index) {
196    return static_cast<const MotionEvent*>(motion_event)->getHistoricalX(
197            pointer_index, history_index);
198}
199
200float AMotionEvent_getHistoricalY(AInputEvent* motion_event, size_t pointer_index,
201        size_t history_index) {
202    return static_cast<const MotionEvent*>(motion_event)->getHistoricalY(
203            pointer_index, history_index);
204}
205
206float AMotionEvent_getHistoricalPressure(AInputEvent* motion_event, size_t pointer_index,
207        size_t history_index) {
208    return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure(
209            pointer_index, history_index);
210}
211
212float AMotionEvent_getHistoricalSize(AInputEvent* motion_event, size_t pointer_index,
213        size_t history_index) {
214    return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize(
215            pointer_index, history_index);
216}
217
218float AMotionEvent_getHistoricalTouchMajor(AInputEvent* motion_event, size_t pointer_index,
219        size_t history_index) {
220    return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMajor(
221            pointer_index, history_index);
222}
223
224float AMotionEvent_getHistoricalTouchMinor(AInputEvent* motion_event, size_t pointer_index,
225        size_t history_index) {
226    return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMinor(
227            pointer_index, history_index);
228}
229
230float AMotionEvent_getHistoricalToolMajor(AInputEvent* motion_event, size_t pointer_index,
231        size_t history_index) {
232    return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMajor(
233            pointer_index, history_index);
234}
235
236float AMotionEvent_getHistoricalToolMinor(AInputEvent* motion_event, size_t pointer_index,
237        size_t history_index) {
238    return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMinor(
239            pointer_index, history_index);
240}
241
242float AMotionEvent_getHistoricalOrientation(AInputEvent* motion_event, size_t pointer_index,
243        size_t history_index) {
244    return static_cast<const MotionEvent*>(motion_event)->getHistoricalOrientation(
245            pointer_index, history_index);
246}
247
248
249void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
250        ALooper_callbackFunc* callback, void* data) {
251    queue->attachLooper(looper, callback, data);
252}
253
254void AInputQueue_detachLooper(AInputQueue* queue) {
255    queue->detachLooper();
256}
257
258int32_t AInputQueue_hasEvents(AInputQueue* queue) {
259    return queue->hasEvents();
260}
261
262int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
263    return queue->getEvent(outEvent);
264}
265
266int32_t AInputQueue_preDispatchEvent(AInputQueue* queue, AInputEvent* event) {
267    return queue->preDispatchEvent(event) ? 1 : 0;
268}
269
270void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, int handled) {
271    queue->finishEvent(event, handled != 0);
272}
273
274
275int32_t AInputDevice_getDeviceIds(int32_t* idBuf, size_t nMax, size_t* nActual) {
276    Vector<int32_t> ids;
277    InputDeviceProxy::getDeviceIds(ids);
278
279    if (nActual) {
280        *nActual = ids.size();
281    }
282
283    if (idBuf && ids.size() < nMax) {
284        memcpy(idBuf, ids.array(), ids.size() * sizeof(int32_t));
285        return 0;
286    }
287
288    return -ENOMEM;
289}
290
291AInputDevice* AInputDevice_acquire(int32_t deviceId) {
292    sp<InputDeviceProxy> proxy(InputDeviceProxy::getDevice(deviceId));
293    if (proxy == NULL) {
294        return NULL;
295    }
296    proxy->incStrong((void*)AInputDevice_acquire);
297    return static_cast<AInputDevice*>(proxy.get());
298}
299
300void AInputDevice_release(AInputDevice* device) {
301    if (device) {
302        InputDeviceProxy* proxy = static_cast<InputDeviceProxy*>(device);
303        proxy->decStrong((void*)AInputDevice_acquire);
304    }
305}
306
307const char* AInputDevice_getName(AInputDevice* device) {
308    InputDeviceProxy* proxy = static_cast<InputDeviceProxy*>(device);
309    return proxy->getInfo()->getName().string();
310}
311
312uint32_t AInputDevice_getSources(AInputDevice* device) {
313    InputDeviceProxy* proxy = static_cast<InputDeviceProxy*>(device);
314    return proxy->getInfo()->getSources();
315}
316
317int32_t AInputDevice_getKeyboardType(AInputDevice* device) {
318    InputDeviceProxy* proxy = static_cast<InputDeviceProxy*>(device);
319    return proxy->getInfo()->getKeyboardType();
320}
321
322int32_t AInputDevice_getMotionRange(AInputDevice* device, int32_t rangeType,
323        float* outMin, float* outMax, float* outFlat, float* outFuzz) {
324    InputDeviceProxy* proxy = static_cast<InputDeviceProxy*>(device);
325    const InputDeviceInfo::MotionRange* range = proxy->getInfo()->getMotionRange(rangeType);
326    if (range) {
327        if (outMin) {
328            *outMin = range->min;
329        }
330        if (outMax) {
331            *outMax = range->max;
332        }
333        if (outFlat) {
334            *outFlat = range->flat;
335        }
336        if (outFuzz) {
337            *outFuzz = range->fuzz;
338        }
339        return 0;
340    } else {
341        return -ENOTSUP;
342    }
343}
344