Jeff Brown | 46b9ac0 | 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 | |
| 10 | // Log debug messages about channel signalling (send signal, receive signal) |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 11 | #define DEBUG_CHANNEL_SIGNALS 0 |
Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 15 | |
| 16 | // Log debug messages about transport actions (initialize, reset, publish, ...) |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 17 | #define DEBUG_TRANSPORT_ACTIONS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | #include <cutils/ashmem.h> |
| 21 | #include <cutils/log.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <ui/InputTransport.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 30 | #define ROUND_UP(value, boundary) (((value) + (boundary) - 1) & ~((boundary) - 1)) |
| 31 | #define MIN_HISTORY_DEPTH 20 |
| 32 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | // Must be at least sizeof(InputMessage) + sufficient space for pointer data |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 34 | static const int DEFAULT_MESSAGE_BUFFER_SIZE = ROUND_UP( |
| 35 | sizeof(InputMessage) + MIN_HISTORY_DEPTH |
| 36 | * (sizeof(InputMessage::SampleData) + MAX_POINTERS * sizeof(PointerCoords)), |
| 37 | 4096); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | |
| 39 | // Signal sent by the producer to the consumer to inform it that a new message is |
| 40 | // available to be consumed in the shared memory buffer. |
| 41 | static const char INPUT_SIGNAL_DISPATCH = 'D'; |
| 42 | |
| 43 | // Signal sent by the consumer to the producer to inform it that it has finished |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 44 | // consuming the most recent message and it handled it. |
| 45 | static const char INPUT_SIGNAL_FINISHED_HANDLED = 'f'; |
| 46 | |
| 47 | // Signal sent by the consumer to the producer to inform it that it has finished |
| 48 | // consuming the most recent message but it did not handle it. |
| 49 | static const char INPUT_SIGNAL_FINISHED_UNHANDLED = 'u'; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 50 | |
| 51 | |
| 52 | // --- InputChannel --- |
| 53 | |
| 54 | InputChannel::InputChannel(const String8& name, int32_t ashmemFd, int32_t receivePipeFd, |
| 55 | int32_t sendPipeFd) : |
| 56 | mName(name), mAshmemFd(ashmemFd), mReceivePipeFd(receivePipeFd), mSendPipeFd(sendPipeFd) { |
| 57 | #if DEBUG_CHANNEL_LIFECYCLE |
| 58 | LOGD("Input channel constructed: name='%s', ashmemFd=%d, receivePipeFd=%d, sendPipeFd=%d", |
| 59 | mName.string(), ashmemFd, receivePipeFd, sendPipeFd); |
| 60 | #endif |
| 61 | |
| 62 | int result = fcntl(mReceivePipeFd, F_SETFL, O_NONBLOCK); |
| 63 | LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make receive pipe " |
| 64 | "non-blocking. errno=%d", mName.string(), errno); |
| 65 | |
| 66 | result = fcntl(mSendPipeFd, F_SETFL, O_NONBLOCK); |
| 67 | LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make send pipe " |
| 68 | "non-blocking. errno=%d", mName.string(), errno); |
| 69 | } |
| 70 | |
| 71 | InputChannel::~InputChannel() { |
| 72 | #if DEBUG_CHANNEL_LIFECYCLE |
| 73 | LOGD("Input channel destroyed: name='%s', ashmemFd=%d, receivePipeFd=%d, sendPipeFd=%d", |
| 74 | mName.string(), mAshmemFd, mReceivePipeFd, mSendPipeFd); |
| 75 | #endif |
| 76 | |
| 77 | ::close(mAshmemFd); |
| 78 | ::close(mReceivePipeFd); |
| 79 | ::close(mSendPipeFd); |
| 80 | } |
| 81 | |
| 82 | status_t InputChannel::openInputChannelPair(const String8& name, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 83 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 84 | status_t result; |
| 85 | |
| 86 | int serverAshmemFd = ashmem_create_region(name.string(), DEFAULT_MESSAGE_BUFFER_SIZE); |
| 87 | if (serverAshmemFd < 0) { |
| 88 | result = -errno; |
| 89 | LOGE("channel '%s' ~ Could not create shared memory region. errno=%d", |
| 90 | name.string(), errno); |
| 91 | } else { |
| 92 | result = ashmem_set_prot_region(serverAshmemFd, PROT_READ | PROT_WRITE); |
| 93 | if (result < 0) { |
| 94 | LOGE("channel '%s' ~ Error %d trying to set protection of ashmem fd %d.", |
| 95 | name.string(), result, serverAshmemFd); |
| 96 | } else { |
| 97 | // Dup the file descriptor because the server and client input channel objects that |
| 98 | // are returned may have different lifetimes but they share the same shared memory region. |
| 99 | int clientAshmemFd; |
| 100 | clientAshmemFd = dup(serverAshmemFd); |
| 101 | if (clientAshmemFd < 0) { |
| 102 | result = -errno; |
| 103 | LOGE("channel '%s' ~ Could not dup() shared memory region fd. errno=%d", |
| 104 | name.string(), errno); |
| 105 | } else { |
| 106 | int forward[2]; |
| 107 | if (pipe(forward)) { |
| 108 | result = -errno; |
| 109 | LOGE("channel '%s' ~ Could not create forward pipe. errno=%d", |
| 110 | name.string(), errno); |
| 111 | } else { |
| 112 | int reverse[2]; |
| 113 | if (pipe(reverse)) { |
| 114 | result = -errno; |
| 115 | LOGE("channel '%s' ~ Could not create reverse pipe. errno=%d", |
| 116 | name.string(), errno); |
| 117 | } else { |
| 118 | String8 serverChannelName = name; |
| 119 | serverChannelName.append(" (server)"); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 120 | outServerChannel = new InputChannel(serverChannelName, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | serverAshmemFd, reverse[0], forward[1]); |
| 122 | |
| 123 | String8 clientChannelName = name; |
| 124 | clientChannelName.append(" (client)"); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 125 | outClientChannel = new InputChannel(clientChannelName, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 126 | clientAshmemFd, forward[0], reverse[1]); |
| 127 | return OK; |
| 128 | } |
| 129 | ::close(forward[0]); |
| 130 | ::close(forward[1]); |
| 131 | } |
| 132 | ::close(clientAshmemFd); |
| 133 | } |
| 134 | } |
| 135 | ::close(serverAshmemFd); |
| 136 | } |
| 137 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 138 | outServerChannel.clear(); |
| 139 | outClientChannel.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 140 | return result; |
| 141 | } |
| 142 | |
| 143 | status_t InputChannel::sendSignal(char signal) { |
Jeff Brown | 7dae0e4 | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 144 | ssize_t nWrite; |
| 145 | do { |
| 146 | nWrite = ::write(mSendPipeFd, & signal, 1); |
| 147 | } while (nWrite == -1 && errno == EINTR); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 148 | |
| 149 | if (nWrite == 1) { |
| 150 | #if DEBUG_CHANNEL_SIGNALS |
| 151 | LOGD("channel '%s' ~ sent signal '%c'", mName.string(), signal); |
| 152 | #endif |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | #if DEBUG_CHANNEL_SIGNALS |
| 157 | LOGD("channel '%s' ~ error sending signal '%c', errno=%d", mName.string(), signal, errno); |
| 158 | #endif |
| 159 | return -errno; |
| 160 | } |
| 161 | |
| 162 | status_t InputChannel::receiveSignal(char* outSignal) { |
Jeff Brown | 7dae0e4 | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 163 | ssize_t nRead; |
| 164 | do { |
| 165 | nRead = ::read(mReceivePipeFd, outSignal, 1); |
| 166 | } while (nRead == -1 && errno == EINTR); |
| 167 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 168 | if (nRead == 1) { |
| 169 | #if DEBUG_CHANNEL_SIGNALS |
| 170 | LOGD("channel '%s' ~ received signal '%c'", mName.string(), *outSignal); |
| 171 | #endif |
| 172 | return OK; |
| 173 | } |
| 174 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 175 | if (nRead == 0) { // check for EOF |
| 176 | #if DEBUG_CHANNEL_SIGNALS |
| 177 | LOGD("channel '%s' ~ receive signal failed because peer was closed", mName.string()); |
| 178 | #endif |
| 179 | return DEAD_OBJECT; |
| 180 | } |
| 181 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | if (errno == EAGAIN) { |
| 183 | #if DEBUG_CHANNEL_SIGNALS |
| 184 | LOGD("channel '%s' ~ receive signal failed because no signal available", mName.string()); |
| 185 | #endif |
| 186 | return WOULD_BLOCK; |
| 187 | } |
| 188 | |
| 189 | #if DEBUG_CHANNEL_SIGNALS |
| 190 | LOGD("channel '%s' ~ receive signal failed, errno=%d", mName.string(), errno); |
| 191 | #endif |
| 192 | return -errno; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | // --- InputPublisher --- |
| 197 | |
| 198 | InputPublisher::InputPublisher(const sp<InputChannel>& channel) : |
| 199 | mChannel(channel), mSharedMessage(NULL), |
| 200 | mPinned(false), mSemaphoreInitialized(false), mWasDispatched(false), |
| 201 | mMotionEventSampleDataTail(NULL) { |
| 202 | } |
| 203 | |
| 204 | InputPublisher::~InputPublisher() { |
| 205 | reset(); |
| 206 | |
| 207 | if (mSharedMessage) { |
| 208 | munmap(mSharedMessage, mAshmemSize); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | status_t InputPublisher::initialize() { |
| 213 | #if DEBUG_TRANSPORT_ACTIONS |
| 214 | LOGD("channel '%s' publisher ~ initialize", |
| 215 | mChannel->getName().string()); |
| 216 | #endif |
| 217 | |
| 218 | int ashmemFd = mChannel->getAshmemFd(); |
| 219 | int result = ashmem_get_size_region(ashmemFd); |
| 220 | if (result < 0) { |
| 221 | LOGE("channel '%s' publisher ~ Error %d getting size of ashmem fd %d.", |
| 222 | mChannel->getName().string(), result, ashmemFd); |
| 223 | return UNKNOWN_ERROR; |
| 224 | } |
| 225 | mAshmemSize = (size_t) result; |
| 226 | |
| 227 | mSharedMessage = static_cast<InputMessage*>(mmap(NULL, mAshmemSize, |
| 228 | PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0)); |
| 229 | if (! mSharedMessage) { |
| 230 | LOGE("channel '%s' publisher ~ mmap failed on ashmem fd %d.", |
| 231 | mChannel->getName().string(), ashmemFd); |
| 232 | return NO_MEMORY; |
| 233 | } |
| 234 | |
| 235 | mPinned = true; |
| 236 | mSharedMessage->consumed = false; |
| 237 | |
| 238 | return reset(); |
| 239 | } |
| 240 | |
| 241 | status_t InputPublisher::reset() { |
| 242 | #if DEBUG_TRANSPORT_ACTIONS |
| 243 | LOGD("channel '%s' publisher ~ reset", |
| 244 | mChannel->getName().string()); |
| 245 | #endif |
| 246 | |
| 247 | if (mPinned) { |
| 248 | // Destroy the semaphore since we are about to unpin the memory region that contains it. |
| 249 | int result; |
| 250 | if (mSemaphoreInitialized) { |
| 251 | if (mSharedMessage->consumed) { |
| 252 | result = sem_post(& mSharedMessage->semaphore); |
| 253 | if (result < 0) { |
| 254 | LOGE("channel '%s' publisher ~ Error %d in sem_post.", |
| 255 | mChannel->getName().string(), errno); |
| 256 | return UNKNOWN_ERROR; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | result = sem_destroy(& mSharedMessage->semaphore); |
| 261 | if (result < 0) { |
| 262 | LOGE("channel '%s' publisher ~ Error %d in sem_destroy.", |
| 263 | mChannel->getName().string(), errno); |
| 264 | return UNKNOWN_ERROR; |
| 265 | } |
| 266 | |
| 267 | mSemaphoreInitialized = false; |
| 268 | } |
| 269 | |
| 270 | // Unpin the region since we no longer care about its contents. |
| 271 | int ashmemFd = mChannel->getAshmemFd(); |
| 272 | result = ashmem_unpin_region(ashmemFd, 0, 0); |
| 273 | if (result < 0) { |
| 274 | LOGE("channel '%s' publisher ~ Error %d unpinning ashmem fd %d.", |
| 275 | mChannel->getName().string(), result, ashmemFd); |
| 276 | return UNKNOWN_ERROR; |
| 277 | } |
| 278 | |
| 279 | mPinned = false; |
| 280 | } |
| 281 | |
| 282 | mMotionEventSampleDataTail = NULL; |
| 283 | mWasDispatched = false; |
| 284 | return OK; |
| 285 | } |
| 286 | |
| 287 | status_t InputPublisher::publishInputEvent( |
| 288 | int32_t type, |
| 289 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 290 | int32_t source) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 291 | if (mPinned) { |
| 292 | LOGE("channel '%s' publisher ~ Attempted to publish a new event but publisher has " |
| 293 | "not yet been reset.", mChannel->getName().string()); |
| 294 | return INVALID_OPERATION; |
| 295 | } |
| 296 | |
| 297 | // Pin the region. |
| 298 | // We do not check for ASHMEM_NOT_PURGED because we don't care about the previous |
| 299 | // contents of the buffer so it does not matter whether it was purged in the meantime. |
| 300 | int ashmemFd = mChannel->getAshmemFd(); |
| 301 | int result = ashmem_pin_region(ashmemFd, 0, 0); |
| 302 | if (result < 0) { |
| 303 | LOGE("channel '%s' publisher ~ Error %d pinning ashmem fd %d.", |
| 304 | mChannel->getName().string(), result, ashmemFd); |
| 305 | return UNKNOWN_ERROR; |
| 306 | } |
| 307 | |
| 308 | mPinned = true; |
| 309 | |
| 310 | result = sem_init(& mSharedMessage->semaphore, 1, 1); |
| 311 | if (result < 0) { |
| 312 | LOGE("channel '%s' publisher ~ Error %d in sem_init.", |
| 313 | mChannel->getName().string(), errno); |
| 314 | return UNKNOWN_ERROR; |
| 315 | } |
| 316 | |
| 317 | mSemaphoreInitialized = true; |
| 318 | |
| 319 | mSharedMessage->consumed = false; |
| 320 | mSharedMessage->type = type; |
| 321 | mSharedMessage->deviceId = deviceId; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 322 | mSharedMessage->source = source; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 323 | return OK; |
| 324 | } |
| 325 | |
| 326 | status_t InputPublisher::publishKeyEvent( |
| 327 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 328 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 329 | int32_t action, |
| 330 | int32_t flags, |
| 331 | int32_t keyCode, |
| 332 | int32_t scanCode, |
| 333 | int32_t metaState, |
| 334 | int32_t repeatCount, |
| 335 | nsecs_t downTime, |
| 336 | nsecs_t eventTime) { |
| 337 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 338 | LOGD("channel '%s' publisher ~ publishKeyEvent: deviceId=%d, source=0x%x, " |
| 339 | "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d," |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 340 | "downTime=%lld, eventTime=%lld", |
| 341 | mChannel->getName().string(), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 342 | deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 343 | downTime, eventTime); |
| 344 | #endif |
| 345 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 346 | status_t result = publishInputEvent(AINPUT_EVENT_TYPE_KEY, deviceId, source); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 347 | if (result < 0) { |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | mSharedMessage->key.action = action; |
| 352 | mSharedMessage->key.flags = flags; |
| 353 | mSharedMessage->key.keyCode = keyCode; |
| 354 | mSharedMessage->key.scanCode = scanCode; |
| 355 | mSharedMessage->key.metaState = metaState; |
| 356 | mSharedMessage->key.repeatCount = repeatCount; |
| 357 | mSharedMessage->key.downTime = downTime; |
| 358 | mSharedMessage->key.eventTime = eventTime; |
| 359 | return OK; |
| 360 | } |
| 361 | |
| 362 | status_t InputPublisher::publishMotionEvent( |
| 363 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 364 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 365 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 366 | int32_t flags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 367 | int32_t edgeFlags, |
| 368 | int32_t metaState, |
| 369 | float xOffset, |
| 370 | float yOffset, |
| 371 | float xPrecision, |
| 372 | float yPrecision, |
| 373 | nsecs_t downTime, |
| 374 | nsecs_t eventTime, |
| 375 | size_t pointerCount, |
| 376 | const int32_t* pointerIds, |
| 377 | const PointerCoords* pointerCoords) { |
| 378 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 379 | LOGD("channel '%s' publisher ~ publishMotionEvent: deviceId=%d, source=0x%x, " |
| 380 | "action=0x%x, flags=0x%x, edgeFlags=0x%x, metaState=0x%x, xOffset=%f, yOffset=%f, " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 381 | "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, " |
| 382 | "pointerCount=%d", |
| 383 | mChannel->getName().string(), |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 384 | deviceId, source, action, flags, edgeFlags, metaState, xOffset, yOffset, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 385 | xPrecision, yPrecision, downTime, eventTime, pointerCount); |
| 386 | #endif |
| 387 | |
| 388 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
| 389 | LOGE("channel '%s' publisher ~ Invalid number of pointers provided: %d.", |
| 390 | mChannel->getName().string(), pointerCount); |
| 391 | return BAD_VALUE; |
| 392 | } |
| 393 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 394 | status_t result = publishInputEvent(AINPUT_EVENT_TYPE_MOTION, deviceId, source); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 395 | if (result < 0) { |
| 396 | return result; |
| 397 | } |
| 398 | |
| 399 | mSharedMessage->motion.action = action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 400 | mSharedMessage->motion.flags = flags; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 401 | mSharedMessage->motion.edgeFlags = edgeFlags; |
| 402 | mSharedMessage->motion.metaState = metaState; |
| 403 | mSharedMessage->motion.xOffset = xOffset; |
| 404 | mSharedMessage->motion.yOffset = yOffset; |
| 405 | mSharedMessage->motion.xPrecision = xPrecision; |
| 406 | mSharedMessage->motion.yPrecision = yPrecision; |
| 407 | mSharedMessage->motion.downTime = downTime; |
| 408 | mSharedMessage->motion.pointerCount = pointerCount; |
| 409 | |
| 410 | mSharedMessage->motion.sampleCount = 1; |
| 411 | mSharedMessage->motion.sampleData[0].eventTime = eventTime; |
| 412 | |
| 413 | for (size_t i = 0; i < pointerCount; i++) { |
| 414 | mSharedMessage->motion.pointerIds[i] = pointerIds[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 415 | mSharedMessage->motion.sampleData[0].coords[i].copyFrom(pointerCoords[i]); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Cache essential information about the motion event to ensure that a malicious consumer |
| 419 | // cannot confuse the publisher by modifying the contents of the shared memory buffer while |
| 420 | // it is being updated. |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 421 | if (action == AMOTION_EVENT_ACTION_MOVE |
| 422 | || action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 423 | mMotionEventPointerCount = pointerCount; |
| 424 | mMotionEventSampleDataStride = InputMessage::sampleDataStride(pointerCount); |
| 425 | mMotionEventSampleDataTail = InputMessage::sampleDataPtrIncrement( |
| 426 | mSharedMessage->motion.sampleData, mMotionEventSampleDataStride); |
| 427 | } else { |
| 428 | mMotionEventSampleDataTail = NULL; |
| 429 | } |
| 430 | return OK; |
| 431 | } |
| 432 | |
| 433 | status_t InputPublisher::appendMotionSample( |
| 434 | nsecs_t eventTime, |
| 435 | const PointerCoords* pointerCoords) { |
| 436 | #if DEBUG_TRANSPORT_ACTIONS |
| 437 | LOGD("channel '%s' publisher ~ appendMotionSample: eventTime=%lld", |
| 438 | mChannel->getName().string(), eventTime); |
| 439 | #endif |
| 440 | |
| 441 | if (! mPinned || ! mMotionEventSampleDataTail) { |
| 442 | LOGE("channel '%s' publisher ~ Cannot append motion sample because there is no current " |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 443 | "AMOTION_EVENT_ACTION_MOVE event.", mChannel->getName().string()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 444 | return INVALID_OPERATION; |
| 445 | } |
| 446 | |
| 447 | InputMessage::SampleData* newTail = InputMessage::sampleDataPtrIncrement( |
| 448 | mMotionEventSampleDataTail, mMotionEventSampleDataStride); |
| 449 | size_t newBytesUsed = reinterpret_cast<char*>(newTail) - |
| 450 | reinterpret_cast<char*>(mSharedMessage); |
| 451 | |
| 452 | if (newBytesUsed > mAshmemSize) { |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 453 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 454 | LOGD("channel '%s' publisher ~ Cannot append motion sample because the shared memory " |
| 455 | "buffer is full. Buffer size: %d bytes, pointers: %d, samples: %d", |
| 456 | mChannel->getName().string(), |
| 457 | mAshmemSize, mMotionEventPointerCount, mSharedMessage->motion.sampleCount); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 458 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 459 | return NO_MEMORY; |
| 460 | } |
| 461 | |
| 462 | int result; |
| 463 | if (mWasDispatched) { |
| 464 | result = sem_trywait(& mSharedMessage->semaphore); |
| 465 | if (result < 0) { |
| 466 | if (errno == EAGAIN) { |
| 467 | // Only possible source of contention is the consumer having consumed (or being in the |
| 468 | // process of consuming) the message and left the semaphore count at 0. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 469 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 470 | LOGD("channel '%s' publisher ~ Cannot append motion sample because the message has " |
| 471 | "already been consumed.", mChannel->getName().string()); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 472 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 473 | return FAILED_TRANSACTION; |
| 474 | } else { |
| 475 | LOGE("channel '%s' publisher ~ Error %d in sem_trywait.", |
| 476 | mChannel->getName().string(), errno); |
| 477 | return UNKNOWN_ERROR; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | mMotionEventSampleDataTail->eventTime = eventTime; |
| 483 | for (size_t i = 0; i < mMotionEventPointerCount; i++) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 484 | mMotionEventSampleDataTail->coords[i].copyFrom(pointerCoords[i]); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 485 | } |
| 486 | mMotionEventSampleDataTail = newTail; |
| 487 | |
| 488 | mSharedMessage->motion.sampleCount += 1; |
| 489 | |
| 490 | if (mWasDispatched) { |
| 491 | result = sem_post(& mSharedMessage->semaphore); |
| 492 | if (result < 0) { |
| 493 | LOGE("channel '%s' publisher ~ Error %d in sem_post.", |
| 494 | mChannel->getName().string(), errno); |
| 495 | return UNKNOWN_ERROR; |
| 496 | } |
| 497 | } |
| 498 | return OK; |
| 499 | } |
| 500 | |
| 501 | status_t InputPublisher::sendDispatchSignal() { |
| 502 | #if DEBUG_TRANSPORT_ACTIONS |
| 503 | LOGD("channel '%s' publisher ~ sendDispatchSignal", |
| 504 | mChannel->getName().string()); |
| 505 | #endif |
| 506 | |
| 507 | mWasDispatched = true; |
| 508 | return mChannel->sendSignal(INPUT_SIGNAL_DISPATCH); |
| 509 | } |
| 510 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 511 | status_t InputPublisher::receiveFinishedSignal(bool* outHandled) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 512 | #if DEBUG_TRANSPORT_ACTIONS |
| 513 | LOGD("channel '%s' publisher ~ receiveFinishedSignal", |
| 514 | mChannel->getName().string()); |
| 515 | #endif |
| 516 | |
| 517 | char signal; |
| 518 | status_t result = mChannel->receiveSignal(& signal); |
| 519 | if (result) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 520 | *outHandled = false; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 521 | return result; |
| 522 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 523 | if (signal == INPUT_SIGNAL_FINISHED_HANDLED) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 524 | *outHandled = true; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 525 | } else if (signal == INPUT_SIGNAL_FINISHED_UNHANDLED) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 526 | *outHandled = false; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 527 | } else { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 528 | LOGE("channel '%s' publisher ~ Received unexpected signal '%c' from consumer", |
| 529 | mChannel->getName().string(), signal); |
| 530 | return UNKNOWN_ERROR; |
| 531 | } |
| 532 | return OK; |
| 533 | } |
| 534 | |
| 535 | // --- InputConsumer --- |
| 536 | |
| 537 | InputConsumer::InputConsumer(const sp<InputChannel>& channel) : |
| 538 | mChannel(channel), mSharedMessage(NULL) { |
| 539 | } |
| 540 | |
| 541 | InputConsumer::~InputConsumer() { |
| 542 | if (mSharedMessage) { |
| 543 | munmap(mSharedMessage, mAshmemSize); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | status_t InputConsumer::initialize() { |
| 548 | #if DEBUG_TRANSPORT_ACTIONS |
| 549 | LOGD("channel '%s' consumer ~ initialize", |
| 550 | mChannel->getName().string()); |
| 551 | #endif |
| 552 | |
| 553 | int ashmemFd = mChannel->getAshmemFd(); |
| 554 | int result = ashmem_get_size_region(ashmemFd); |
| 555 | if (result < 0) { |
| 556 | LOGE("channel '%s' consumer ~ Error %d getting size of ashmem fd %d.", |
| 557 | mChannel->getName().string(), result, ashmemFd); |
| 558 | return UNKNOWN_ERROR; |
| 559 | } |
| 560 | |
| 561 | mAshmemSize = (size_t) result; |
| 562 | |
| 563 | mSharedMessage = static_cast<InputMessage*>(mmap(NULL, mAshmemSize, |
| 564 | PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0)); |
| 565 | if (! mSharedMessage) { |
| 566 | LOGE("channel '%s' consumer ~ mmap failed on ashmem fd %d.", |
| 567 | mChannel->getName().string(), ashmemFd); |
| 568 | return NO_MEMORY; |
| 569 | } |
| 570 | |
| 571 | return OK; |
| 572 | } |
| 573 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 574 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, InputEvent** outEvent) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 575 | #if DEBUG_TRANSPORT_ACTIONS |
| 576 | LOGD("channel '%s' consumer ~ consume", |
| 577 | mChannel->getName().string()); |
| 578 | #endif |
| 579 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 580 | *outEvent = NULL; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 581 | |
| 582 | int ashmemFd = mChannel->getAshmemFd(); |
| 583 | int result = ashmem_pin_region(ashmemFd, 0, 0); |
| 584 | if (result != ASHMEM_NOT_PURGED) { |
| 585 | if (result == ASHMEM_WAS_PURGED) { |
| 586 | LOGE("channel '%s' consumer ~ Error %d pinning ashmem fd %d because it was purged " |
| 587 | "which probably indicates that the publisher and consumer are out of sync.", |
| 588 | mChannel->getName().string(), result, ashmemFd); |
| 589 | return INVALID_OPERATION; |
| 590 | } |
| 591 | |
| 592 | LOGE("channel '%s' consumer ~ Error %d pinning ashmem fd %d.", |
| 593 | mChannel->getName().string(), result, ashmemFd); |
| 594 | return UNKNOWN_ERROR; |
| 595 | } |
| 596 | |
| 597 | if (mSharedMessage->consumed) { |
| 598 | LOGE("channel '%s' consumer ~ The current message has already been consumed.", |
| 599 | mChannel->getName().string()); |
| 600 | return INVALID_OPERATION; |
| 601 | } |
| 602 | |
| 603 | // Acquire but *never release* the semaphore. Contention on the semaphore is used to signal |
| 604 | // to the publisher that the message has been consumed (or is in the process of being |
| 605 | // consumed). Eventually the publisher will reinitialize the semaphore for the next message. |
| 606 | result = sem_wait(& mSharedMessage->semaphore); |
| 607 | if (result < 0) { |
| 608 | LOGE("channel '%s' consumer ~ Error %d in sem_wait.", |
| 609 | mChannel->getName().string(), errno); |
| 610 | return UNKNOWN_ERROR; |
| 611 | } |
| 612 | |
| 613 | mSharedMessage->consumed = true; |
| 614 | |
| 615 | switch (mSharedMessage->type) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 616 | case AINPUT_EVENT_TYPE_KEY: { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 617 | KeyEvent* keyEvent = factory->createKeyEvent(); |
| 618 | if (! keyEvent) return NO_MEMORY; |
| 619 | |
| 620 | populateKeyEvent(keyEvent); |
| 621 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 622 | *outEvent = keyEvent; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 626 | case AINPUT_EVENT_TYPE_MOTION: { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 627 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 628 | if (! motionEvent) return NO_MEMORY; |
| 629 | |
| 630 | populateMotionEvent(motionEvent); |
| 631 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 632 | *outEvent = motionEvent; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | |
| 636 | default: |
| 637 | LOGE("channel '%s' consumer ~ Received message of unknown type %d", |
| 638 | mChannel->getName().string(), mSharedMessage->type); |
| 639 | return UNKNOWN_ERROR; |
| 640 | } |
| 641 | |
| 642 | return OK; |
| 643 | } |
| 644 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 645 | status_t InputConsumer::sendFinishedSignal(bool handled) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 646 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 647 | LOGD("channel '%s' consumer ~ sendFinishedSignal: handled=%d", |
| 648 | mChannel->getName().string(), handled); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 649 | #endif |
| 650 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 651 | return mChannel->sendSignal(handled |
| 652 | ? INPUT_SIGNAL_FINISHED_HANDLED |
| 653 | : INPUT_SIGNAL_FINISHED_UNHANDLED); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | status_t InputConsumer::receiveDispatchSignal() { |
| 657 | #if DEBUG_TRANSPORT_ACTIONS |
| 658 | LOGD("channel '%s' consumer ~ receiveDispatchSignal", |
| 659 | mChannel->getName().string()); |
| 660 | #endif |
| 661 | |
| 662 | char signal; |
| 663 | status_t result = mChannel->receiveSignal(& signal); |
| 664 | if (result) { |
| 665 | return result; |
| 666 | } |
| 667 | if (signal != INPUT_SIGNAL_DISPATCH) { |
| 668 | LOGE("channel '%s' consumer ~ Received unexpected signal '%c' from publisher", |
| 669 | mChannel->getName().string(), signal); |
| 670 | return UNKNOWN_ERROR; |
| 671 | } |
| 672 | return OK; |
| 673 | } |
| 674 | |
| 675 | void InputConsumer::populateKeyEvent(KeyEvent* keyEvent) const { |
| 676 | keyEvent->initialize( |
| 677 | mSharedMessage->deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 678 | mSharedMessage->source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 679 | mSharedMessage->key.action, |
| 680 | mSharedMessage->key.flags, |
| 681 | mSharedMessage->key.keyCode, |
| 682 | mSharedMessage->key.scanCode, |
| 683 | mSharedMessage->key.metaState, |
| 684 | mSharedMessage->key.repeatCount, |
| 685 | mSharedMessage->key.downTime, |
| 686 | mSharedMessage->key.eventTime); |
| 687 | } |
| 688 | |
| 689 | void InputConsumer::populateMotionEvent(MotionEvent* motionEvent) const { |
| 690 | motionEvent->initialize( |
| 691 | mSharedMessage->deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 692 | mSharedMessage->source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 693 | mSharedMessage->motion.action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 694 | mSharedMessage->motion.flags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 695 | mSharedMessage->motion.edgeFlags, |
| 696 | mSharedMessage->motion.metaState, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 697 | mSharedMessage->motion.xOffset, |
| 698 | mSharedMessage->motion.yOffset, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 699 | mSharedMessage->motion.xPrecision, |
| 700 | mSharedMessage->motion.yPrecision, |
| 701 | mSharedMessage->motion.downTime, |
| 702 | mSharedMessage->motion.sampleData[0].eventTime, |
| 703 | mSharedMessage->motion.pointerCount, |
| 704 | mSharedMessage->motion.pointerIds, |
| 705 | mSharedMessage->motion.sampleData[0].coords); |
| 706 | |
| 707 | size_t sampleCount = mSharedMessage->motion.sampleCount; |
| 708 | if (sampleCount > 1) { |
| 709 | InputMessage::SampleData* sampleData = mSharedMessage->motion.sampleData; |
| 710 | size_t sampleDataStride = InputMessage::sampleDataStride( |
| 711 | mSharedMessage->motion.pointerCount); |
| 712 | |
| 713 | while (--sampleCount > 0) { |
| 714 | sampleData = InputMessage::sampleDataPtrIncrement(sampleData, sampleDataStride); |
| 715 | motionEvent->addSample(sampleData->eventTime, sampleData->coords); |
| 716 | } |
| 717 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | } // namespace android |