blob: b2842d0aef8162ddb69719024b8ddc4bf390c01f [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) {
433 LOGD("channel '%s' publisher ~ Cannot append motion sample because the shared memory "
434 "buffer is full. Buffer size: %d bytes, pointers: %d, samples: %d",
435 mChannel->getName().string(),
436 mAshmemSize, mMotionEventPointerCount, mSharedMessage->motion.sampleCount);
437 return NO_MEMORY;
438 }
439
440 int result;
441 if (mWasDispatched) {
442 result = sem_trywait(& mSharedMessage->semaphore);
443 if (result < 0) {
444 if (errno == EAGAIN) {
445 // Only possible source of contention is the consumer having consumed (or being in the
446 // process of consuming) the message and left the semaphore count at 0.
447 LOGD("channel '%s' publisher ~ Cannot append motion sample because the message has "
448 "already been consumed.", mChannel->getName().string());
449 return FAILED_TRANSACTION;
450 } else {
451 LOGE("channel '%s' publisher ~ Error %d in sem_trywait.",
452 mChannel->getName().string(), errno);
453 return UNKNOWN_ERROR;
454 }
455 }
456 }
457
458 mMotionEventSampleDataTail->eventTime = eventTime;
459 for (size_t i = 0; i < mMotionEventPointerCount; i++) {
460 mMotionEventSampleDataTail->coords[i] = pointerCoords[i];
461 }
462 mMotionEventSampleDataTail = newTail;
463
464 mSharedMessage->motion.sampleCount += 1;
465
466 if (mWasDispatched) {
467 result = sem_post(& mSharedMessage->semaphore);
468 if (result < 0) {
469 LOGE("channel '%s' publisher ~ Error %d in sem_post.",
470 mChannel->getName().string(), errno);
471 return UNKNOWN_ERROR;
472 }
473 }
474 return OK;
475}
476
477status_t InputPublisher::sendDispatchSignal() {
478#if DEBUG_TRANSPORT_ACTIONS
479 LOGD("channel '%s' publisher ~ sendDispatchSignal",
480 mChannel->getName().string());
481#endif
482
483 mWasDispatched = true;
484 return mChannel->sendSignal(INPUT_SIGNAL_DISPATCH);
485}
486
487status_t InputPublisher::receiveFinishedSignal() {
488#if DEBUG_TRANSPORT_ACTIONS
489 LOGD("channel '%s' publisher ~ receiveFinishedSignal",
490 mChannel->getName().string());
491#endif
492
493 char signal;
494 status_t result = mChannel->receiveSignal(& signal);
495 if (result) {
496 return result;
497 }
498 if (signal != INPUT_SIGNAL_FINISHED) {
499 LOGE("channel '%s' publisher ~ Received unexpected signal '%c' from consumer",
500 mChannel->getName().string(), signal);
501 return UNKNOWN_ERROR;
502 }
503 return OK;
504}
505
506// --- InputConsumer ---
507
508InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
509 mChannel(channel), mSharedMessage(NULL) {
510}
511
512InputConsumer::~InputConsumer() {
513 if (mSharedMessage) {
514 munmap(mSharedMessage, mAshmemSize);
515 }
516}
517
518status_t InputConsumer::initialize() {
519#if DEBUG_TRANSPORT_ACTIONS
520 LOGD("channel '%s' consumer ~ initialize",
521 mChannel->getName().string());
522#endif
523
524 int ashmemFd = mChannel->getAshmemFd();
525 int result = ashmem_get_size_region(ashmemFd);
526 if (result < 0) {
527 LOGE("channel '%s' consumer ~ Error %d getting size of ashmem fd %d.",
528 mChannel->getName().string(), result, ashmemFd);
529 return UNKNOWN_ERROR;
530 }
531
532 mAshmemSize = (size_t) result;
533
534 mSharedMessage = static_cast<InputMessage*>(mmap(NULL, mAshmemSize,
535 PROT_READ | PROT_WRITE, MAP_SHARED, ashmemFd, 0));
536 if (! mSharedMessage) {
537 LOGE("channel '%s' consumer ~ mmap failed on ashmem fd %d.",
538 mChannel->getName().string(), ashmemFd);
539 return NO_MEMORY;
540 }
541
542 return OK;
543}
544
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700545status_t InputConsumer::consume(InputEventFactoryInterface* factory, InputEvent** outEvent) {
Jeff Browne839a582010-04-22 18:58:52 -0700546#if DEBUG_TRANSPORT_ACTIONS
547 LOGD("channel '%s' consumer ~ consume",
548 mChannel->getName().string());
549#endif
550
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700551 *outEvent = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700552
553 int ashmemFd = mChannel->getAshmemFd();
554 int result = ashmem_pin_region(ashmemFd, 0, 0);
555 if (result != ASHMEM_NOT_PURGED) {
556 if (result == ASHMEM_WAS_PURGED) {
557 LOGE("channel '%s' consumer ~ Error %d pinning ashmem fd %d because it was purged "
558 "which probably indicates that the publisher and consumer are out of sync.",
559 mChannel->getName().string(), result, ashmemFd);
560 return INVALID_OPERATION;
561 }
562
563 LOGE("channel '%s' consumer ~ Error %d pinning ashmem fd %d.",
564 mChannel->getName().string(), result, ashmemFd);
565 return UNKNOWN_ERROR;
566 }
567
568 if (mSharedMessage->consumed) {
569 LOGE("channel '%s' consumer ~ The current message has already been consumed.",
570 mChannel->getName().string());
571 return INVALID_OPERATION;
572 }
573
574 // Acquire but *never release* the semaphore. Contention on the semaphore is used to signal
575 // to the publisher that the message has been consumed (or is in the process of being
576 // consumed). Eventually the publisher will reinitialize the semaphore for the next message.
577 result = sem_wait(& mSharedMessage->semaphore);
578 if (result < 0) {
579 LOGE("channel '%s' consumer ~ Error %d in sem_wait.",
580 mChannel->getName().string(), errno);
581 return UNKNOWN_ERROR;
582 }
583
584 mSharedMessage->consumed = true;
585
586 switch (mSharedMessage->type) {
587 case INPUT_EVENT_TYPE_KEY: {
588 KeyEvent* keyEvent = factory->createKeyEvent();
589 if (! keyEvent) return NO_MEMORY;
590
591 populateKeyEvent(keyEvent);
592
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700593 *outEvent = keyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700594 break;
595 }
596
597 case INPUT_EVENT_TYPE_MOTION: {
598 MotionEvent* motionEvent = factory->createMotionEvent();
599 if (! motionEvent) return NO_MEMORY;
600
601 populateMotionEvent(motionEvent);
602
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700603 *outEvent = motionEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700604 break;
605 }
606
607 default:
608 LOGE("channel '%s' consumer ~ Received message of unknown type %d",
609 mChannel->getName().string(), mSharedMessage->type);
610 return UNKNOWN_ERROR;
611 }
612
613 return OK;
614}
615
616status_t InputConsumer::sendFinishedSignal() {
617#if DEBUG_TRANSPORT_ACTIONS
618 LOGD("channel '%s' consumer ~ sendFinishedSignal",
619 mChannel->getName().string());
620#endif
621
622 return mChannel->sendSignal(INPUT_SIGNAL_FINISHED);
623}
624
625status_t InputConsumer::receiveDispatchSignal() {
626#if DEBUG_TRANSPORT_ACTIONS
627 LOGD("channel '%s' consumer ~ receiveDispatchSignal",
628 mChannel->getName().string());
629#endif
630
631 char signal;
632 status_t result = mChannel->receiveSignal(& signal);
633 if (result) {
634 return result;
635 }
636 if (signal != INPUT_SIGNAL_DISPATCH) {
637 LOGE("channel '%s' consumer ~ Received unexpected signal '%c' from publisher",
638 mChannel->getName().string(), signal);
639 return UNKNOWN_ERROR;
640 }
641 return OK;
642}
643
644void InputConsumer::populateKeyEvent(KeyEvent* keyEvent) const {
645 keyEvent->initialize(
646 mSharedMessage->deviceId,
647 mSharedMessage->nature,
648 mSharedMessage->key.action,
649 mSharedMessage->key.flags,
650 mSharedMessage->key.keyCode,
651 mSharedMessage->key.scanCode,
652 mSharedMessage->key.metaState,
653 mSharedMessage->key.repeatCount,
654 mSharedMessage->key.downTime,
655 mSharedMessage->key.eventTime);
656}
657
658void InputConsumer::populateMotionEvent(MotionEvent* motionEvent) const {
659 motionEvent->initialize(
660 mSharedMessage->deviceId,
661 mSharedMessage->nature,
662 mSharedMessage->motion.action,
663 mSharedMessage->motion.edgeFlags,
664 mSharedMessage->motion.metaState,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700665 mSharedMessage->motion.xOffset,
666 mSharedMessage->motion.yOffset,
Jeff Browne839a582010-04-22 18:58:52 -0700667 mSharedMessage->motion.xPrecision,
668 mSharedMessage->motion.yPrecision,
669 mSharedMessage->motion.downTime,
670 mSharedMessage->motion.sampleData[0].eventTime,
671 mSharedMessage->motion.pointerCount,
672 mSharedMessage->motion.pointerIds,
673 mSharedMessage->motion.sampleData[0].coords);
674
675 size_t sampleCount = mSharedMessage->motion.sampleCount;
676 if (sampleCount > 1) {
677 InputMessage::SampleData* sampleData = mSharedMessage->motion.sampleData;
678 size_t sampleDataStride = InputMessage::sampleDataStride(
679 mSharedMessage->motion.pointerCount);
680
681 while (--sampleCount > 0) {
682 sampleData = InputMessage::sampleDataPtrIncrement(sampleData, sampleDataStride);
683 motionEvent->addSample(sampleData->eventTime, sampleData->coords);
684 }
685 }
Jeff Browne839a582010-04-22 18:58:52 -0700686}
687
688} // namespace android
Dianne Hackborn4d96bb62010-06-18 18:09:33 -0700689
690// --- input_queue_t ---
691
692using android::InputEvent;
693using android::InputChannel;
694using android::InputConsumer;
695using android::sp;
696using android::status_t;
697
698input_queue_t::input_queue_t(const sp<InputChannel>& channel) :
699 mConsumer(channel) {
700}
701
702input_queue_t::~input_queue_t() {
703}
704
705status_t input_queue_t::consume(InputEvent** event) {
706 return mConsumer.consume(&mInputEventFactory, event);
707}