Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 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 | |
| 26 | using android::InputEvent; |
| 27 | using android::KeyEvent; |
| 28 | using android::MotionEvent; |
| 29 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 30 | int32_t AInputEvent_getType(const AInputEvent* event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 31 | return static_cast<const InputEvent*>(event)->getType(); |
| 32 | } |
| 33 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 34 | int32_t AInputEvent_getDeviceId(const AInputEvent* event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 35 | return static_cast<const InputEvent*>(event)->getDeviceId(); |
| 36 | } |
| 37 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 38 | int32_t AInputEvent_getNature(const AInputEvent* event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 39 | return static_cast<const InputEvent*>(event)->getNature(); |
| 40 | } |
| 41 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 42 | int32_t AKeyEvent_getAction(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 43 | return static_cast<const KeyEvent*>(key_event)->getAction(); |
| 44 | } |
| 45 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 46 | int32_t AKeyEvent_getFlags(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 47 | return static_cast<const KeyEvent*>(key_event)->getFlags(); |
| 48 | } |
| 49 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 50 | int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 51 | return static_cast<const KeyEvent*>(key_event)->getKeyCode(); |
| 52 | } |
| 53 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 54 | int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 55 | return static_cast<const KeyEvent*>(key_event)->getScanCode(); |
| 56 | } |
| 57 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 58 | int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 59 | return static_cast<const KeyEvent*>(key_event)->getMetaState(); |
| 60 | } |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 61 | int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 62 | return static_cast<const KeyEvent*>(key_event)->getRepeatCount(); |
| 63 | } |
| 64 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 65 | int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 66 | return static_cast<const KeyEvent*>(key_event)->getDownTime(); |
| 67 | } |
| 68 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 69 | int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 70 | return static_cast<const KeyEvent*>(key_event)->getEventTime(); |
| 71 | } |
| 72 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 73 | int32_t AMotionEvent_getAction(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 74 | return static_cast<const MotionEvent*>(motion_event)->getAction(); |
| 75 | } |
| 76 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 77 | int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 78 | return static_cast<const MotionEvent*>(motion_event)->getMetaState(); |
| 79 | } |
| 80 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 81 | int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 82 | return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags(); |
| 83 | } |
| 84 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 85 | int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 86 | return static_cast<const MotionEvent*>(motion_event)->getDownTime(); |
| 87 | } |
| 88 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 89 | int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 90 | return static_cast<const MotionEvent*>(motion_event)->getEventTime(); |
| 91 | } |
| 92 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 93 | float AMotionEvent_getXOffset(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 94 | return static_cast<const MotionEvent*>(motion_event)->getXOffset(); |
| 95 | } |
| 96 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 97 | float AMotionEvent_getYOffset(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 98 | return static_cast<const MotionEvent*>(motion_event)->getYOffset(); |
| 99 | } |
| 100 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 101 | float AMotionEvent_getXPrecision(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 102 | return static_cast<const MotionEvent*>(motion_event)->getXPrecision(); |
| 103 | } |
| 104 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 105 | float AMotionEvent_getYPrecision(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 106 | return static_cast<const MotionEvent*>(motion_event)->getYPrecision(); |
| 107 | } |
| 108 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 109 | size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 110 | return static_cast<const MotionEvent*>(motion_event)->getPointerCount(); |
| 111 | } |
| 112 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 113 | int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 114 | return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index); |
| 115 | } |
| 116 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 117 | float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 118 | return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index); |
| 119 | } |
| 120 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 121 | float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 122 | return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index); |
| 123 | } |
| 124 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 125 | float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 126 | return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index); |
| 127 | } |
| 128 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 129 | float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 130 | return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index); |
| 131 | } |
| 132 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 133 | float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 134 | return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index); |
| 135 | } |
| 136 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 137 | float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 138 | return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index); |
| 139 | } |
| 140 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 141 | size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 142 | return static_cast<const MotionEvent*>(motion_event)->getHistorySize(); |
| 143 | } |
| 144 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 145 | int64_t AMotionEvent_getHistoricalEventTime(AInputEvent* motion_event, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 146 | size_t history_index) { |
| 147 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime( |
| 148 | history_index); |
| 149 | } |
| 150 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 151 | float AMotionEvent_getHistoricalRawX(AInputEvent* motion_event, size_t pointer_index, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 152 | size_t history_index) { |
| 153 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX( |
| 154 | pointer_index, history_index); |
| 155 | } |
| 156 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 157 | float AMotionEvent_getHistoricalRawY(AInputEvent* motion_event, size_t pointer_index, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 158 | size_t history_index) { |
| 159 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY( |
| 160 | pointer_index, history_index); |
| 161 | } |
| 162 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 163 | float AMotionEvent_getHistoricalX(AInputEvent* motion_event, size_t pointer_index, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 164 | size_t history_index) { |
| 165 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalX( |
| 166 | pointer_index, history_index); |
| 167 | } |
| 168 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 169 | float AMotionEvent_getHistoricalY(AInputEvent* motion_event, size_t pointer_index, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 170 | size_t history_index) { |
| 171 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalY( |
| 172 | pointer_index, history_index); |
| 173 | } |
| 174 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 175 | float AMotionEvent_getHistoricalPressure(AInputEvent* motion_event, size_t pointer_index, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 176 | size_t history_index) { |
| 177 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure( |
| 178 | pointer_index, history_index); |
| 179 | } |
| 180 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 181 | float AMotionEvent_getHistoricalSize(AInputEvent* motion_event, size_t pointer_index, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 182 | size_t history_index) { |
| 183 | return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize( |
| 184 | pointer_index, history_index); |
| 185 | } |
| 186 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 187 | int AInputQueue_getFd(AInputQueue* queue) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 188 | return queue->getConsumer().getChannel()->getReceivePipeFd(); |
| 189 | } |
| 190 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 191 | int AInputQueue_hasEvents(AInputQueue* queue) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 203 | int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) { |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame^] | 226 | void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 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 | } |