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