input.cpp revision a95e4cb62f3642cb190d032dbf7dc40d9ecc6973
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
24#include <poll.h>
25
26using android::InputEvent;
27using android::KeyEvent;
28using android::MotionEvent;
29
30int32_t input_event_get_type(const input_event_t* event) {
31    return static_cast<const InputEvent*>(event)->getType();
32}
33
34int32_t input_event_get_device_id(const input_event_t* event) {
35    return static_cast<const InputEvent*>(event)->getDeviceId();
36}
37
38int32_t input_event_get_nature(const input_event_t* event) {
39    return static_cast<const InputEvent*>(event)->getNature();
40}
41
42int32_t key_event_get_action(const input_event_t* key_event) {
43    return static_cast<const KeyEvent*>(key_event)->getAction();
44}
45
46int32_t key_event_get_flags(const input_event_t* key_event) {
47    return static_cast<const KeyEvent*>(key_event)->getFlags();
48}
49
50int32_t key_event_get_key_code(const input_event_t* key_event) {
51    return static_cast<const KeyEvent*>(key_event)->getKeyCode();
52}
53
54int32_t key_event_get_scan_code(const input_event_t* key_event) {
55    return static_cast<const KeyEvent*>(key_event)->getScanCode();
56}
57
58int32_t key_event_get_meta_state(const input_event_t* key_event) {
59    return static_cast<const KeyEvent*>(key_event)->getMetaState();
60}
61int32_t key_event_get_repeat_count(const input_event_t* key_event) {
62    return static_cast<const KeyEvent*>(key_event)->getRepeatCount();
63}
64
65int64_t key_event_get_down_time(const input_event_t* key_event) {
66    return static_cast<const KeyEvent*>(key_event)->getDownTime();
67}
68
69int64_t key_event_get_event_time(const input_event_t* key_event) {
70    return static_cast<const KeyEvent*>(key_event)->getEventTime();
71}
72
73int32_t motion_event_get_action(const input_event_t* motion_event) {
74    return static_cast<const MotionEvent*>(motion_event)->getAction();
75}
76
77int32_t motion_event_get_meta_state(const input_event_t* motion_event) {
78    return static_cast<const MotionEvent*>(motion_event)->getMetaState();
79}
80
81int32_t motion_event_get_edge_flags(const input_event_t* motion_event) {
82    return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags();
83}
84
85int64_t motion_event_get_down_time(const input_event_t* motion_event) {
86    return static_cast<const MotionEvent*>(motion_event)->getDownTime();
87}
88
89int64_t motion_event_get_event_time(const input_event_t* motion_event) {
90    return static_cast<const MotionEvent*>(motion_event)->getEventTime();
91}
92
93float motion_event_get_x_offset(const input_event_t* motion_event) {
94    return static_cast<const MotionEvent*>(motion_event)->getXOffset();
95}
96
97float motion_event_get_y_offset(const input_event_t* motion_event) {
98    return static_cast<const MotionEvent*>(motion_event)->getYOffset();
99}
100
101float motion_event_get_x_precision(const input_event_t* motion_event) {
102    return static_cast<const MotionEvent*>(motion_event)->getXPrecision();
103}
104
105float motion_event_get_y_precision(const input_event_t* motion_event) {
106    return static_cast<const MotionEvent*>(motion_event)->getYPrecision();
107}
108
109size_t motion_event_get_pointer_count(const input_event_t* motion_event) {
110    return static_cast<const MotionEvent*>(motion_event)->getPointerCount();
111}
112
113int32_t motion_event_get_pointer_id(const input_event_t* motion_event, size_t pointer_index) {
114    return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index);
115}
116
117float motion_event_get_raw_x(const input_event_t* motion_event, size_t pointer_index) {
118    return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index);
119}
120
121float motion_event_get_raw_y(const input_event_t* motion_event, size_t pointer_index) {
122    return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index);
123}
124
125float motion_event_get_x(const input_event_t* motion_event, size_t pointer_index) {
126    return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index);
127}
128
129float motion_event_get_y(const input_event_t* motion_event, size_t pointer_index) {
130    return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index);
131}
132
133float motion_event_get_pressure(const input_event_t* motion_event, size_t pointer_index) {
134    return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index);
135}
136
137float motion_event_get_size(const input_event_t* motion_event, size_t pointer_index) {
138    return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index);
139}
140
141size_t motion_event_get_history_size(const input_event_t* motion_event) {
142    return static_cast<const MotionEvent*>(motion_event)->getHistorySize();
143}
144
145int64_t motion_event_get_historical_event_time(input_event_t* motion_event,
146        size_t history_index) {
147    return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime(
148            history_index);
149}
150
151float motion_event_get_historical_raw_x(input_event_t* motion_event, size_t pointer_index,
152        size_t history_index) {
153    return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX(
154            pointer_index, history_index);
155}
156
157float motion_event_get_historical_raw_y(input_event_t* motion_event, size_t pointer_index,
158        size_t history_index) {
159    return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY(
160            pointer_index, history_index);
161}
162
163float motion_event_get_historical_x(input_event_t* motion_event, size_t pointer_index,
164        size_t history_index) {
165    return static_cast<const MotionEvent*>(motion_event)->getHistoricalX(
166            pointer_index, history_index);
167}
168
169float motion_event_get_historical_y(input_event_t* motion_event, size_t pointer_index,
170        size_t history_index) {
171    return static_cast<const MotionEvent*>(motion_event)->getHistoricalY(
172            pointer_index, history_index);
173}
174
175float motion_event_get_historical_pressure(input_event_t* motion_event, size_t pointer_index,
176        size_t history_index) {
177    return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure(
178            pointer_index, history_index);
179}
180
181float motion_event_get_historical_size(input_event_t* motion_event, size_t pointer_index,
182        size_t history_index) {
183    return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize(
184            pointer_index, history_index);
185}
186
187int input_queue_get_fd(input_queue_t* queue) {
188    return queue->getConsumer().getChannel()->getReceivePipeFd();
189}
190
191int input_queue_has_events(input_queue_t* queue) {
192    struct pollfd pfd;
193
194    pfd.fd = queue->getConsumer().getChannel()->getReceivePipeFd();
195    pfd.events = POLLIN;
196    pfd.revents = 0;
197
198    int nfd = poll(&pfd, 1, 0);
199    if (nfd <= 0) return nfd;
200    return pfd.revents == POLLIN ? 1 : -1;
201}
202
203int32_t input_queue_get_event(input_queue_t* queue, input_event_t** outEvent) {
204    *outEvent = NULL;
205
206    int32_t res = queue->getConsumer().receiveDispatchSignal();
207    if (res != android::OK) {
208        LOGE("channel '%s' ~ Failed to receive dispatch signal.  status=%d",
209                queue->getConsumer().getChannel()->getName().string(), res);
210        return -1;
211    }
212
213    InputEvent* myEvent = NULL;
214    res = queue->consume(&myEvent);
215    if (res != android::OK) {
216        LOGW("channel '%s' ~ Failed to consume input event.  status=%d",
217                queue->getConsumer().getChannel()->getName().string(), res);
218        queue->getConsumer().sendFinishedSignal();
219        return -1;
220    }
221
222    *outEvent = myEvent;
223    return 0;
224}
225
226void input_queue_finish_event(input_queue_t* queue, input_event_t* event,
227        int handled) {
228    int32_t res = queue->getConsumer().sendFinishedSignal();
229    if (res != android::OK) {
230        LOGW("Failed to send finished signal on channel '%s'.  status=%d",
231                queue->getConsumer().getChannel()->getName().string(), res);
232    }
233}
234