Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010 The Android Open Source Project |
| 3 | // |
| 4 | // Provides a shared memory transport for input events. |
| 5 | // |
| 6 | #define LOG_TAG "InputTransport" |
| 7 | |
| 8 | //#define LOG_NDEBUG 0 |
| 9 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 10 | // Log debug messages about channel messages (send message, receive message) |
| 11 | #define DEBUG_CHANNEL_MESSAGES 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 12 | |
| 13 | // Log debug messages whenever InputChannel objects are created/destroyed |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 14 | #define DEBUG_CHANNEL_LIFECYCLE 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 15 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 16 | #define DEBUG_TRANSPORT_ACTIONS 0 |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 17 | // Log debug messages about transport actions |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
| 19 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 20 | #include <cutils/log.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 23 | #include <ui/InputTransport.h> |
| 24 | #include <unistd.h> |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/socket.h> |
| 27 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 31 | // --- InputMessage --- |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 32 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 33 | bool InputMessage::isValid(size_t actualSize) const { |
| 34 | if (size() == actualSize) { |
| 35 | switch (header.type) { |
| 36 | case TYPE_KEY: |
| 37 | return true; |
| 38 | case TYPE_MOTION: |
| 39 | return body.motion.pointerCount > 0 |
| 40 | && body.motion.pointerCount <= MAX_POINTERS; |
| 41 | case TYPE_FINISHED: |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 48 | size_t InputMessage::size() const { |
| 49 | switch (header.type) { |
| 50 | case TYPE_KEY: |
| 51 | return sizeof(Header) + body.key.size(); |
| 52 | case TYPE_MOTION: |
| 53 | return sizeof(Header) + body.motion.size(); |
| 54 | case TYPE_FINISHED: |
| 55 | return sizeof(Header) + body.finished.size(); |
| 56 | } |
| 57 | return sizeof(Header); |
| 58 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | // --- InputChannel --- |
| 62 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 63 | InputChannel::InputChannel(const String8& name, int fd) : |
| 64 | mName(name), mFd(fd) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 65 | #if DEBUG_CHANNEL_LIFECYCLE |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 66 | ALOGD("Input channel constructed: name='%s', fd=%d", |
| 67 | mName.string(), fd); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 70 | int result = fcntl(mFd, F_SETFL, O_NONBLOCK); |
| 71 | LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make socket " |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 72 | "non-blocking. errno=%d", mName.string(), errno); |
| 73 | } |
| 74 | |
| 75 | InputChannel::~InputChannel() { |
| 76 | #if DEBUG_CHANNEL_LIFECYCLE |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 77 | ALOGD("Input channel destroyed: name='%s', fd=%d", |
| 78 | mName.string(), mFd); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 79 | #endif |
| 80 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 81 | ::close(mFd); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | status_t InputChannel::openInputChannelPair(const String8& name, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 85 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 86 | int sockets[2]; |
| 87 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) { |
| 88 | status_t result = -errno; |
| 89 | ALOGE("channel '%s' ~ Could not create socket pair. errno=%d", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 90 | name.string(), errno); |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 91 | outServerChannel.clear(); |
| 92 | outClientChannel.clear(); |
| 93 | return result; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 96 | String8 serverChannelName = name; |
| 97 | serverChannelName.append(" (server)"); |
| 98 | outServerChannel = new InputChannel(serverChannelName, sockets[0]); |
| 99 | |
| 100 | String8 clientChannelName = name; |
| 101 | clientChannelName.append(" (client)"); |
| 102 | outClientChannel = new InputChannel(clientChannelName, sockets[1]); |
| 103 | return OK; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 106 | status_t InputChannel::sendMessage(const InputMessage* msg) { |
| 107 | size_t msgLength = msg->size(); |
Jeff Brown | 7dae0e4 | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 108 | ssize_t nWrite; |
| 109 | do { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 110 | nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL); |
Jeff Brown | 7dae0e4 | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 111 | } while (nWrite == -1 && errno == EINTR); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 113 | if (nWrite < 0) { |
| 114 | int error = errno; |
| 115 | #if DEBUG_CHANNEL_MESSAGES |
| 116 | ALOGD("channel '%s' ~ error sending message of type %d, errno=%d", mName.string(), |
| 117 | msg->header.type, error); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 118 | #endif |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 119 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 120 | return WOULD_BLOCK; |
| 121 | } |
| 122 | if (error == EPIPE || error == ENOTCONN) { |
| 123 | return DEAD_OBJECT; |
| 124 | } |
| 125 | return -error; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 128 | if (size_t(nWrite) != msgLength) { |
| 129 | #if DEBUG_CHANNEL_MESSAGES |
| 130 | ALOGD("channel '%s' ~ error sending message type %d, send was incomplete", |
| 131 | mName.string(), msg->header.type); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 132 | #endif |
| 133 | return DEAD_OBJECT; |
| 134 | } |
| 135 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 136 | #if DEBUG_CHANNEL_MESSAGES |
| 137 | ALOGD("channel '%s' ~ sent message of type %d", mName.string(), msg->header.type); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 138 | #endif |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 139 | return OK; |
| 140 | } |
| 141 | |
| 142 | status_t InputChannel::receiveMessage(InputMessage* msg) { |
| 143 | ssize_t nRead; |
| 144 | do { |
| 145 | nRead = ::recv(mFd, msg, sizeof(InputMessage), MSG_DONTWAIT); |
| 146 | } while (nRead == -1 && errno == EINTR); |
| 147 | |
| 148 | if (nRead < 0) { |
| 149 | int error = errno; |
| 150 | #if DEBUG_CHANNEL_MESSAGES |
| 151 | ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.string(), errno); |
| 152 | #endif |
| 153 | if (error == EAGAIN || error == EWOULDBLOCK) { |
| 154 | return WOULD_BLOCK; |
| 155 | } |
| 156 | if (error == EPIPE || error == ENOTCONN) { |
| 157 | return DEAD_OBJECT; |
| 158 | } |
| 159 | return -error; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 162 | if (nRead == 0) { // check for EOF |
| 163 | #if DEBUG_CHANNEL_MESSAGES |
| 164 | ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.string()); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 165 | #endif |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 166 | return DEAD_OBJECT; |
| 167 | } |
| 168 | |
| 169 | if (!msg->isValid(nRead)) { |
| 170 | #if DEBUG_CHANNEL_MESSAGES |
| 171 | ALOGD("channel '%s' ~ received invalid message", mName.string()); |
| 172 | #endif |
| 173 | return BAD_VALUE; |
| 174 | } |
| 175 | |
| 176 | #if DEBUG_CHANNEL_MESSAGES |
| 177 | ALOGD("channel '%s' ~ received message of type %d", mName.string(), msg->header.type); |
| 178 | #endif |
| 179 | return OK; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | // --- InputPublisher --- |
| 184 | |
| 185 | InputPublisher::InputPublisher(const sp<InputChannel>& channel) : |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 186 | mChannel(channel) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | InputPublisher::~InputPublisher() { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | status_t InputPublisher::publishKeyEvent( |
| 193 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 194 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 195 | int32_t action, |
| 196 | int32_t flags, |
| 197 | int32_t keyCode, |
| 198 | int32_t scanCode, |
| 199 | int32_t metaState, |
| 200 | int32_t repeatCount, |
| 201 | nsecs_t downTime, |
| 202 | nsecs_t eventTime) { |
| 203 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 204 | ALOGD("channel '%s' publisher ~ publishKeyEvent: deviceId=%d, source=0x%x, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 205 | "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d," |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 206 | "downTime=%lld, eventTime=%lld", |
| 207 | mChannel->getName().string(), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 208 | deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 209 | downTime, eventTime); |
| 210 | #endif |
| 211 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 212 | InputMessage msg; |
| 213 | msg.header.type = InputMessage::TYPE_KEY; |
| 214 | msg.body.key.deviceId = deviceId; |
| 215 | msg.body.key.source = source; |
| 216 | msg.body.key.action = action; |
| 217 | msg.body.key.flags = flags; |
| 218 | msg.body.key.keyCode = keyCode; |
| 219 | msg.body.key.scanCode = scanCode; |
| 220 | msg.body.key.metaState = metaState; |
| 221 | msg.body.key.repeatCount = repeatCount; |
| 222 | msg.body.key.downTime = downTime; |
| 223 | msg.body.key.eventTime = eventTime; |
| 224 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | status_t InputPublisher::publishMotionEvent( |
| 228 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 229 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 230 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 231 | int32_t flags, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 232 | int32_t edgeFlags, |
| 233 | int32_t metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 234 | int32_t buttonState, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 235 | float xOffset, |
| 236 | float yOffset, |
| 237 | float xPrecision, |
| 238 | float yPrecision, |
| 239 | nsecs_t downTime, |
| 240 | nsecs_t eventTime, |
| 241 | size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 242 | const PointerProperties* pointerProperties, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 243 | const PointerCoords* pointerCoords) { |
| 244 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 245 | ALOGD("channel '%s' publisher ~ publishMotionEvent: deviceId=%d, source=0x%x, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 246 | "action=0x%x, flags=0x%x, edgeFlags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 247 | "xOffset=%f, yOffset=%f, " |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 248 | "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, " |
| 249 | "pointerCount=%d", |
| 250 | mChannel->getName().string(), |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 251 | deviceId, source, action, flags, edgeFlags, metaState, buttonState, |
| 252 | xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 253 | #endif |
| 254 | |
| 255 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 256 | ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %d.", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 257 | mChannel->getName().string(), pointerCount); |
| 258 | return BAD_VALUE; |
| 259 | } |
| 260 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 261 | InputMessage msg; |
| 262 | msg.header.type = InputMessage::TYPE_MOTION; |
| 263 | msg.body.motion.deviceId = deviceId; |
| 264 | msg.body.motion.source = source; |
| 265 | msg.body.motion.action = action; |
| 266 | msg.body.motion.flags = flags; |
| 267 | msg.body.motion.edgeFlags = edgeFlags; |
| 268 | msg.body.motion.metaState = metaState; |
| 269 | msg.body.motion.buttonState = buttonState; |
| 270 | msg.body.motion.xOffset = xOffset; |
| 271 | msg.body.motion.yOffset = yOffset; |
| 272 | msg.body.motion.xPrecision = xPrecision; |
| 273 | msg.body.motion.yPrecision = yPrecision; |
| 274 | msg.body.motion.downTime = downTime; |
| 275 | msg.body.motion.eventTime = eventTime; |
| 276 | msg.body.motion.pointerCount = pointerCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 277 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 278 | msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]); |
| 279 | msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 280 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 281 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 284 | status_t InputPublisher::receiveFinishedSignal(bool* outHandled) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 285 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 286 | ALOGD("channel '%s' publisher ~ receiveFinishedSignal", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 287 | mChannel->getName().string()); |
| 288 | #endif |
| 289 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 290 | InputMessage msg; |
| 291 | status_t result = mChannel->receiveMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 292 | if (result) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 293 | *outHandled = false; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 294 | return result; |
| 295 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 296 | if (msg.header.type != InputMessage::TYPE_FINISHED) { |
| 297 | ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer", |
| 298 | mChannel->getName().string(), msg.header.type); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 299 | return UNKNOWN_ERROR; |
| 300 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 301 | *outHandled = msg.body.finished.handled; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 302 | return OK; |
| 303 | } |
| 304 | |
| 305 | // --- InputConsumer --- |
| 306 | |
| 307 | InputConsumer::InputConsumer(const sp<InputChannel>& channel) : |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 308 | mChannel(channel) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | InputConsumer::~InputConsumer() { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 314 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, InputEvent** outEvent) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 315 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 316 | ALOGD("channel '%s' consumer ~ consume", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 317 | mChannel->getName().string()); |
| 318 | #endif |
| 319 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 320 | *outEvent = NULL; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 321 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 322 | InputMessage msg; |
| 323 | status_t result = mChannel->receiveMessage(&msg); |
| 324 | if (result) { |
| 325 | return result; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 328 | switch (msg.header.type) { |
| 329 | case InputMessage::TYPE_KEY: { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 330 | KeyEvent* keyEvent = factory->createKeyEvent(); |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 331 | if (!keyEvent) return NO_MEMORY; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 332 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 333 | keyEvent->initialize( |
| 334 | msg.body.key.deviceId, |
| 335 | msg.body.key.source, |
| 336 | msg.body.key.action, |
| 337 | msg.body.key.flags, |
| 338 | msg.body.key.keyCode, |
| 339 | msg.body.key.scanCode, |
| 340 | msg.body.key.metaState, |
| 341 | msg.body.key.repeatCount, |
| 342 | msg.body.key.downTime, |
| 343 | msg.body.key.eventTime); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 344 | *outEvent = keyEvent; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 345 | break; |
| 346 | } |
| 347 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 348 | case AINPUT_EVENT_TYPE_MOTION: { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 349 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 350 | if (! motionEvent) return NO_MEMORY; |
| 351 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 352 | size_t pointerCount = msg.body.motion.pointerCount; |
| 353 | PointerProperties pointerProperties[pointerCount]; |
| 354 | PointerCoords pointerCoords[pointerCount]; |
| 355 | for (size_t i = 0; i < pointerCount; i++) { |
| 356 | pointerProperties[i].copyFrom(msg.body.motion.pointers[i].properties); |
| 357 | pointerCoords[i].copyFrom(msg.body.motion.pointers[i].coords); |
| 358 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 359 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 360 | motionEvent->initialize( |
| 361 | msg.body.motion.deviceId, |
| 362 | msg.body.motion.source, |
| 363 | msg.body.motion.action, |
| 364 | msg.body.motion.flags, |
| 365 | msg.body.motion.edgeFlags, |
| 366 | msg.body.motion.metaState, |
| 367 | msg.body.motion.buttonState, |
| 368 | msg.body.motion.xOffset, |
| 369 | msg.body.motion.yOffset, |
| 370 | msg.body.motion.xPrecision, |
| 371 | msg.body.motion.yPrecision, |
| 372 | msg.body.motion.downTime, |
| 373 | msg.body.motion.eventTime, |
| 374 | pointerCount, |
| 375 | pointerProperties, |
| 376 | pointerCoords); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 377 | *outEvent = motionEvent; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | |
| 381 | default: |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 382 | ALOGE("channel '%s' consumer ~ Received unexpected message of type %d", |
| 383 | mChannel->getName().string(), msg.header.type); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 384 | return UNKNOWN_ERROR; |
| 385 | } |
| 386 | |
| 387 | return OK; |
| 388 | } |
| 389 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 390 | status_t InputConsumer::sendFinishedSignal(bool handled) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 391 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 392 | ALOGD("channel '%s' consumer ~ sendFinishedSignal: handled=%d", |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 393 | mChannel->getName().string(), handled); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 394 | #endif |
| 395 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 396 | InputMessage msg; |
| 397 | msg.header.type = InputMessage::TYPE_FINISHED; |
| 398 | msg.body.finished.handled = handled; |
| 399 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | } // namespace android |