blob: 8eeb9555c51c5350d83b95c4326e6d404836b723 [file] [log] [blame]
Dianne Hackborna95e4cb2010-06-18 18:09:33 -07001/*
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>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070021#include <input/Input.h>
22#include <input/InputTransport.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070023#include <utils/Looper.h>
Jeff Brown6d0fec22010-07-23 21:28:06 -070024#include <utils/RefBase.h>
25#include <utils/Vector.h>
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070026
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070027#include <android_runtime/android_app_NativeActivity.h>
Michael Wrighta44dd262013-04-10 21:12:00 -070028#include <android_runtime/android_view_InputQueue.h>
Chris Ye3da1d202020-04-07 19:39:38 -070029#include <android_view_MotionEvent.h>
30#include <android_view_KeyEvent.h>
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070031
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070032#include <poll.h>
Jeff Brown6d0fec22010-07-23 21:28:06 -070033#include <errno.h>
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070034
35using android::InputEvent;
Michael Wrighta44dd262013-04-10 21:12:00 -070036using android::InputQueue;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070037using android::KeyEvent;
Michael Wrighta44dd262013-04-10 21:12:00 -070038using android::Looper;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070039using android::MotionEvent;
Jeff Brown6d0fec22010-07-23 21:28:06 -070040using android::sp;
41using android::Vector;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070042
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070043int32_t AInputEvent_getType(const AInputEvent* event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070044 return static_cast<const InputEvent*>(event)->getType();
45}
46
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070047int32_t AInputEvent_getDeviceId(const AInputEvent* event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070048 return static_cast<const InputEvent*>(event)->getDeviceId();
49}
50
Jeff Brownc5ed5912010-07-14 18:48:53 -070051int32_t AInputEvent_getSource(const AInputEvent* event) {
52 return static_cast<const InputEvent*>(event)->getSource();
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070053}
54
Chris Ye3da1d202020-04-07 19:39:38 -070055void AInputEvent_release(const AInputEvent* event) {
56 delete event;
57}
58
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070059int32_t AKeyEvent_getAction(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070060 return static_cast<const KeyEvent*>(key_event)->getAction();
61}
62
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070063int32_t AKeyEvent_getFlags(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070064 return static_cast<const KeyEvent*>(key_event)->getFlags();
65}
66
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070067int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070068 return static_cast<const KeyEvent*>(key_event)->getKeyCode();
69}
70
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070071int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070072 return static_cast<const KeyEvent*>(key_event)->getScanCode();
73}
74
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070075int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070076 return static_cast<const KeyEvent*>(key_event)->getMetaState();
77}
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070078int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070079 return static_cast<const KeyEvent*>(key_event)->getRepeatCount();
80}
81
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070082int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070083 return static_cast<const KeyEvent*>(key_event)->getDownTime();
84}
85
Chris Ye3da1d202020-04-07 19:39:38 -070086const 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}
Jeff Brownc5ed5912010-07-14 18:48:53 -070094
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070095int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070096 return static_cast<const KeyEvent*>(key_event)->getEventTime();
97}
98
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070099int32_t AMotionEvent_getAction(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700100 return static_cast<const MotionEvent*>(motion_event)->getAction();
101}
102
Jeff Brown85a31762010-09-01 17:01:00 -0700103int32_t AMotionEvent_getFlags(const AInputEvent* motion_event) {
104 return static_cast<const MotionEvent*>(motion_event)->getFlags();
105}
106
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700107int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700108 return static_cast<const MotionEvent*>(motion_event)->getMetaState();
109}
110
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700111int32_t AMotionEvent_getButtonState(const AInputEvent* motion_event) {
112 return static_cast<const MotionEvent*>(motion_event)->getButtonState();
113}
114
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700115int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700116 return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags();
117}
118
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700119int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700120 return static_cast<const MotionEvent*>(motion_event)->getDownTime();
121}
122
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700123int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700124 return static_cast<const MotionEvent*>(motion_event)->getEventTime();
125}
126
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700127float AMotionEvent_getXOffset(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700128 return static_cast<const MotionEvent*>(motion_event)->getXOffset();
129}
130
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700131float AMotionEvent_getYOffset(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700132 return static_cast<const MotionEvent*>(motion_event)->getYOffset();
133}
134
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700135float AMotionEvent_getXPrecision(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700136 return static_cast<const MotionEvent*>(motion_event)->getXPrecision();
137}
138
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700139float AMotionEvent_getYPrecision(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700140 return static_cast<const MotionEvent*>(motion_event)->getYPrecision();
141}
142
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700143size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700144 return static_cast<const MotionEvent*>(motion_event)->getPointerCount();
145}
146
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700147int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700148 return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index);
149}
150
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700151int32_t AMotionEvent_getToolType(const AInputEvent* motion_event, size_t pointer_index) {
152 return static_cast<const MotionEvent*>(motion_event)->getToolType(pointer_index);
153}
154
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700155float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700156 return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index);
157}
158
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700159float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700160 return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index);
161}
162
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700163float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700164 return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index);
165}
166
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700167float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700168 return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index);
169}
170
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700171float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700172 return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index);
173}
174
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700175float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700176 return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index);
177}
178
Jeff Brownc5ed5912010-07-14 18:48:53 -0700179float AMotionEvent_getTouchMajor(const AInputEvent* motion_event, size_t pointer_index) {
180 return static_cast<const MotionEvent*>(motion_event)->getTouchMajor(pointer_index);
181}
182
183float AMotionEvent_getTouchMinor(const AInputEvent* motion_event, size_t pointer_index) {
184 return static_cast<const MotionEvent*>(motion_event)->getTouchMinor(pointer_index);
185}
186
187float AMotionEvent_getToolMajor(const AInputEvent* motion_event, size_t pointer_index) {
188 return static_cast<const MotionEvent*>(motion_event)->getToolMajor(pointer_index);
189}
190
191float AMotionEvent_getToolMinor(const AInputEvent* motion_event, size_t pointer_index) {
192 return static_cast<const MotionEvent*>(motion_event)->getToolMinor(pointer_index);
193}
194
195float AMotionEvent_getOrientation(const AInputEvent* motion_event, size_t pointer_index) {
196 return static_cast<const MotionEvent*>(motion_event)->getOrientation(pointer_index);
197}
198
Jeff Brown91c69ab2011-02-14 17:03:18 -0800199float 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
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700204size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) {
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700205 return static_cast<const MotionEvent*>(motion_event)->getHistorySize();
206}
207
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800208int64_t AMotionEvent_getHistoricalEventTime(const AInputEvent* motion_event,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700209 size_t history_index) {
210 return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime(
211 history_index);
212}
213
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800214float AMotionEvent_getHistoricalRawX(const AInputEvent* motion_event, size_t pointer_index,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700215 size_t history_index) {
216 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX(
217 pointer_index, history_index);
218}
219
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800220float AMotionEvent_getHistoricalRawY(const AInputEvent* motion_event, size_t pointer_index,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700221 size_t history_index) {
222 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY(
223 pointer_index, history_index);
224}
225
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800226float AMotionEvent_getHistoricalX(const AInputEvent* motion_event, size_t pointer_index,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700227 size_t history_index) {
228 return static_cast<const MotionEvent*>(motion_event)->getHistoricalX(
229 pointer_index, history_index);
230}
231
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800232float AMotionEvent_getHistoricalY(const AInputEvent* motion_event, size_t pointer_index,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700233 size_t history_index) {
234 return static_cast<const MotionEvent*>(motion_event)->getHistoricalY(
235 pointer_index, history_index);
236}
237
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800238float AMotionEvent_getHistoricalPressure(const AInputEvent* motion_event, size_t pointer_index,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700239 size_t history_index) {
240 return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure(
241 pointer_index, history_index);
242}
243
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800244float AMotionEvent_getHistoricalSize(const AInputEvent* motion_event, size_t pointer_index,
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700245 size_t history_index) {
246 return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize(
247 pointer_index, history_index);
248}
249
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800250float AMotionEvent_getHistoricalTouchMajor(const AInputEvent* motion_event, size_t pointer_index,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700251 size_t history_index) {
252 return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMajor(
253 pointer_index, history_index);
254}
255
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800256float AMotionEvent_getHistoricalTouchMinor(const AInputEvent* motion_event, size_t pointer_index,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700257 size_t history_index) {
258 return static_cast<const MotionEvent*>(motion_event)->getHistoricalTouchMinor(
259 pointer_index, history_index);
260}
261
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800262float AMotionEvent_getHistoricalToolMajor(const AInputEvent* motion_event, size_t pointer_index,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700263 size_t history_index) {
264 return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMajor(
265 pointer_index, history_index);
266}
267
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800268float AMotionEvent_getHistoricalToolMinor(const AInputEvent* motion_event, size_t pointer_index,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700269 size_t history_index) {
270 return static_cast<const MotionEvent*>(motion_event)->getHistoricalToolMinor(
271 pointer_index, history_index);
272}
273
Andrew Hsiehc01e1ed2013-05-27 12:27:10 +0800274float AMotionEvent_getHistoricalOrientation(const AInputEvent* motion_event, size_t pointer_index,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700275 size_t history_index) {
276 return static_cast<const MotionEvent*>(motion_event)->getHistoricalOrientation(
277 pointer_index, history_index);
278}
279
Jeff Brown91c69ab2011-02-14 17:03:18 -0800280float 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
Chris Ye3da1d202020-04-07 19:39:38 -0700286const 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}
Jeff Brownc5ed5912010-07-14 18:48:53 -0700295
Dianne Hackborn68267412010-07-02 18:52:01 -0700296void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700297 int ident, ALooper_callbackFunc callback, void* data) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700298 InputQueue* iq = static_cast<InputQueue*>(queue);
Brian Carlstrom82b007d2013-12-12 23:12:55 -0800299 Looper* l = reinterpret_cast<Looper*>(looper);
Michael Wrighta44dd262013-04-10 21:12:00 -0700300 iq->attachLooper(l, ident, callback, data);
Dianne Hackborn68267412010-07-02 18:52:01 -0700301}
302
303void AInputQueue_detachLooper(AInputQueue* queue) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700304 InputQueue* iq = static_cast<InputQueue*>(queue);
305 iq->detachLooper();
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700306}
307
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700308int32_t AInputQueue_hasEvents(AInputQueue* queue) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700309 InputQueue* iq = static_cast<InputQueue*>(queue);
310 return iq->hasEvents();
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700311}
312
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700313int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700314 InputQueue* iq = static_cast<InputQueue*>(queue);
315 InputEvent* event;
316 int32_t res = iq->getEvent(&event);
317 *outEvent = event;
318 return res;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700319}
320
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700321int32_t AInputQueue_preDispatchEvent(AInputQueue* queue, AInputEvent* event) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700322 InputQueue* iq = static_cast<InputQueue*>(queue);
323 InputEvent* e = static_cast<InputEvent*>(event);
324 return iq->preDispatchEvent(e) ? 1 : 0;
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700325}
326
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700327void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, int handled) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700328 InputQueue* iq = static_cast<InputQueue*>(queue);
329 InputEvent* e = static_cast<InputEvent*>(event);
330 iq->finishEvent(e, handled != 0);
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700331}