input.cpp revision 85448bbecd4e0909eecfab15b7c3605f114d0427
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 25#include <poll.h> 26 27using android::InputEvent; 28using android::KeyEvent; 29using android::MotionEvent; 30 31int32_t AInputEvent_getType(const AInputEvent* event) { 32 return static_cast<const InputEvent*>(event)->getType(); 33} 34 35int32_t AInputEvent_getDeviceId(const AInputEvent* event) { 36 return static_cast<const InputEvent*>(event)->getDeviceId(); 37} 38 39int32_t AInputEvent_getNature(const AInputEvent* event) { 40 return static_cast<const InputEvent*>(event)->getNature(); 41} 42 43int32_t AKeyEvent_getAction(const AInputEvent* key_event) { 44 return static_cast<const KeyEvent*>(key_event)->getAction(); 45} 46 47int32_t AKeyEvent_getFlags(const AInputEvent* key_event) { 48 return static_cast<const KeyEvent*>(key_event)->getFlags(); 49} 50 51int32_t AKeyEvent_getKeyCode(const AInputEvent* key_event) { 52 return static_cast<const KeyEvent*>(key_event)->getKeyCode(); 53} 54 55int32_t AKeyEvent_getScanCode(const AInputEvent* key_event) { 56 return static_cast<const KeyEvent*>(key_event)->getScanCode(); 57} 58 59int32_t AKeyEvent_getMetaState(const AInputEvent* key_event) { 60 return static_cast<const KeyEvent*>(key_event)->getMetaState(); 61} 62int32_t AKeyEvent_getRepeatCount(const AInputEvent* key_event) { 63 return static_cast<const KeyEvent*>(key_event)->getRepeatCount(); 64} 65 66int64_t AKeyEvent_getDownTime(const AInputEvent* key_event) { 67 return static_cast<const KeyEvent*>(key_event)->getDownTime(); 68} 69 70int64_t AKeyEvent_getEventTime(const AInputEvent* key_event) { 71 return static_cast<const KeyEvent*>(key_event)->getEventTime(); 72} 73 74int32_t AMotionEvent_getAction(const AInputEvent* motion_event) { 75 return static_cast<const MotionEvent*>(motion_event)->getAction(); 76} 77 78int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event) { 79 return static_cast<const MotionEvent*>(motion_event)->getMetaState(); 80} 81 82int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event) { 83 return reinterpret_cast<const MotionEvent*>(motion_event)->getEdgeFlags(); 84} 85 86int64_t AMotionEvent_getDownTime(const AInputEvent* motion_event) { 87 return static_cast<const MotionEvent*>(motion_event)->getDownTime(); 88} 89 90int64_t AMotionEvent_getEventTime(const AInputEvent* motion_event) { 91 return static_cast<const MotionEvent*>(motion_event)->getEventTime(); 92} 93 94float AMotionEvent_getXOffset(const AInputEvent* motion_event) { 95 return static_cast<const MotionEvent*>(motion_event)->getXOffset(); 96} 97 98float AMotionEvent_getYOffset(const AInputEvent* motion_event) { 99 return static_cast<const MotionEvent*>(motion_event)->getYOffset(); 100} 101 102float AMotionEvent_getXPrecision(const AInputEvent* motion_event) { 103 return static_cast<const MotionEvent*>(motion_event)->getXPrecision(); 104} 105 106float AMotionEvent_getYPrecision(const AInputEvent* motion_event) { 107 return static_cast<const MotionEvent*>(motion_event)->getYPrecision(); 108} 109 110size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event) { 111 return static_cast<const MotionEvent*>(motion_event)->getPointerCount(); 112} 113 114int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index) { 115 return static_cast<const MotionEvent*>(motion_event)->getPointerId(pointer_index); 116} 117 118float AMotionEvent_getRawX(const AInputEvent* motion_event, size_t pointer_index) { 119 return static_cast<const MotionEvent*>(motion_event)->getRawX(pointer_index); 120} 121 122float AMotionEvent_getRawY(const AInputEvent* motion_event, size_t pointer_index) { 123 return static_cast<const MotionEvent*>(motion_event)->getRawY(pointer_index); 124} 125 126float AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) { 127 return static_cast<const MotionEvent*>(motion_event)->getX(pointer_index); 128} 129 130float AMotionEvent_getY(const AInputEvent* motion_event, size_t pointer_index) { 131 return static_cast<const MotionEvent*>(motion_event)->getY(pointer_index); 132} 133 134float AMotionEvent_getPressure(const AInputEvent* motion_event, size_t pointer_index) { 135 return static_cast<const MotionEvent*>(motion_event)->getPressure(pointer_index); 136} 137 138float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index) { 139 return static_cast<const MotionEvent*>(motion_event)->getSize(pointer_index); 140} 141 142size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event) { 143 return static_cast<const MotionEvent*>(motion_event)->getHistorySize(); 144} 145 146int64_t AMotionEvent_getHistoricalEventTime(AInputEvent* motion_event, 147 size_t history_index) { 148 return static_cast<const MotionEvent*>(motion_event)->getHistoricalEventTime( 149 history_index); 150} 151 152float AMotionEvent_getHistoricalRawX(AInputEvent* motion_event, size_t pointer_index, 153 size_t history_index) { 154 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawX( 155 pointer_index, history_index); 156} 157 158float AMotionEvent_getHistoricalRawY(AInputEvent* motion_event, size_t pointer_index, 159 size_t history_index) { 160 return static_cast<const MotionEvent*>(motion_event)->getHistoricalRawY( 161 pointer_index, history_index); 162} 163 164float AMotionEvent_getHistoricalX(AInputEvent* motion_event, size_t pointer_index, 165 size_t history_index) { 166 return static_cast<const MotionEvent*>(motion_event)->getHistoricalX( 167 pointer_index, history_index); 168} 169 170float AMotionEvent_getHistoricalY(AInputEvent* motion_event, size_t pointer_index, 171 size_t history_index) { 172 return static_cast<const MotionEvent*>(motion_event)->getHistoricalY( 173 pointer_index, history_index); 174} 175 176float AMotionEvent_getHistoricalPressure(AInputEvent* motion_event, size_t pointer_index, 177 size_t history_index) { 178 return static_cast<const MotionEvent*>(motion_event)->getHistoricalPressure( 179 pointer_index, history_index); 180} 181 182float AMotionEvent_getHistoricalSize(AInputEvent* motion_event, size_t pointer_index, 183 size_t history_index) { 184 return static_cast<const MotionEvent*>(motion_event)->getHistoricalSize( 185 pointer_index, history_index); 186} 187 188void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper, 189 ALooper_callbackFunc* callback, void* data) { 190 queue->setPollLoop(static_cast<android::PollLoop*>(looper)); 191 ALooper_addFd(looper, queue->getConsumer().getChannel()->getReceivePipeFd(), 192 POLLIN, callback, data); 193} 194 195void AInputQueue_detachLooper(AInputQueue* queue) { 196 queue->getPollLoop()->removeCallback( 197 queue->getConsumer().getChannel()->getReceivePipeFd()); 198} 199 200int AInputQueue_hasEvents(AInputQueue* queue) { 201 struct pollfd pfd; 202 203 pfd.fd = queue->getConsumer().getChannel()->getReceivePipeFd(); 204 pfd.events = POLLIN; 205 pfd.revents = 0; 206 207 int nfd = poll(&pfd, 1, 0); 208 if (nfd <= 0) return nfd; 209 return pfd.revents == POLLIN ? 1 : -1; 210} 211 212int32_t AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) { 213 *outEvent = NULL; 214 215 int32_t res = queue->getConsumer().receiveDispatchSignal(); 216 if (res != android::OK) { 217 LOGE("channel '%s' ~ Failed to receive dispatch signal. status=%d", 218 queue->getConsumer().getChannel()->getName().string(), res); 219 return -1; 220 } 221 222 InputEvent* myEvent = NULL; 223 res = queue->consume(&myEvent); 224 if (res != android::OK) { 225 LOGW("channel '%s' ~ Failed to consume input event. status=%d", 226 queue->getConsumer().getChannel()->getName().string(), res); 227 queue->getConsumer().sendFinishedSignal(); 228 return -1; 229 } 230 231 *outEvent = myEvent; 232 return 0; 233} 234 235void AInputQueue_finishEvent(AInputQueue* queue, AInputEvent* event, 236 int handled) { 237 if (!handled && ((InputEvent*)event)->getType() == INPUT_EVENT_TYPE_KEY 238 && ((KeyEvent*)event)->hasDefaultAction()) { 239 // The app didn't handle this, but it may have a default action 240 // associated with it. We need to hand this back to Java to be 241 // executed. 242 queue->doDefaultKey((KeyEvent*)event); 243 return; 244 } 245 246 int32_t res = queue->getConsumer().sendFinishedSignal(); 247 if (res != android::OK) { 248 LOGW("Failed to send finished signal on channel '%s'. status=%d", 249 queue->getConsumer().getChannel()->getName().string(), res); 250 } 251} 252