blob: 76f9ec978ef2d87749f47131dbb5be03613e467c [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// The input reader.
5//
6#define LOG_TAG "InputReader"
7
8//#define LOG_NDEBUG 0
9
10// Log debug messages for each raw event received from the EventHub.
11#define DEBUG_RAW_EVENTS 0
12
13// Log debug messages about touch screen filtering hacks.
14#define DEBUG_HACKS 1
15
16// Log debug messages about virtual key processing.
17#define DEBUG_VIRTUAL_KEYS 1
18
19// Log debug messages about pointers.
20#define DEBUG_POINTERS 1
21
22#include <cutils/log.h>
23#include <ui/InputReader.h>
24
25#include <stddef.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <errno.h>
29#include <limits.h>
30
31namespace android {
32
33// --- Static Functions ---
34
35template<typename T>
36inline static T abs(const T& value) {
37 return value < 0 ? - value : value;
38}
39
40template<typename T>
41inline static T min(const T& a, const T& b) {
42 return a < b ? a : b;
43}
44
45int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
46 int32_t mask;
47 switch (keyCode) {
48 case KEYCODE_ALT_LEFT:
49 mask = META_ALT_LEFT_ON;
50 break;
51 case KEYCODE_ALT_RIGHT:
52 mask = META_ALT_RIGHT_ON;
53 break;
54 case KEYCODE_SHIFT_LEFT:
55 mask = META_SHIFT_LEFT_ON;
56 break;
57 case KEYCODE_SHIFT_RIGHT:
58 mask = META_SHIFT_RIGHT_ON;
59 break;
60 case KEYCODE_SYM:
61 mask = META_SYM_ON;
62 break;
63 default:
64 return oldMetaState;
65 }
66
67 int32_t newMetaState = down ? oldMetaState | mask : oldMetaState & ~ mask
68 & ~ (META_ALT_ON | META_SHIFT_ON);
69
70 if (newMetaState & (META_ALT_LEFT_ON | META_ALT_RIGHT_ON)) {
71 newMetaState |= META_ALT_ON;
72 }
73
74 if (newMetaState & (META_SHIFT_LEFT_ON | META_SHIFT_RIGHT_ON)) {
75 newMetaState |= META_SHIFT_ON;
76 }
77
78 return newMetaState;
79}
80
81static const int32_t keyCodeRotationMap[][4] = {
82 // key codes enumerated counter-clockwise with the original (unrotated) key first
83 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
84 { KEYCODE_DPAD_DOWN, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_UP, KEYCODE_DPAD_LEFT },
85 { KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_UP, KEYCODE_DPAD_LEFT, KEYCODE_DPAD_DOWN },
86 { KEYCODE_DPAD_UP, KEYCODE_DPAD_LEFT, KEYCODE_DPAD_DOWN, KEYCODE_DPAD_RIGHT },
87 { KEYCODE_DPAD_LEFT, KEYCODE_DPAD_DOWN, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_UP },
88};
89static const int keyCodeRotationMapSize =
90 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
91
92int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
93 if (orientation != InputDispatchPolicyInterface::ROTATION_0) {
94 for (int i = 0; i < keyCodeRotationMapSize; i++) {
95 if (keyCode == keyCodeRotationMap[i][0]) {
96 return keyCodeRotationMap[i][orientation];
97 }
98 }
99 }
100 return keyCode;
101}
102
103
104// --- InputDevice ---
105
106InputDevice::InputDevice(int32_t id, uint32_t classes, String8 name) :
107 id(id), classes(classes), name(name), ignored(false) {
108}
109
110void InputDevice::reset() {
111 if (isKeyboard()) {
112 keyboard.reset();
113 }
114
115 if (isTrackball()) {
116 trackball.reset();
117 }
118
119 if (isMultiTouchScreen()) {
120 multiTouchScreen.reset();
121 } else if (isSingleTouchScreen()) {
122 singleTouchScreen.reset();
123 }
124
125 if (isTouchScreen()) {
126 touchScreen.reset();
127 }
128}
129
130
131// --- InputDevice::TouchData ---
132
133void InputDevice::TouchData::copyFrom(const TouchData& other) {
134 pointerCount = other.pointerCount;
135 idBits = other.idBits;
136
137 for (uint32_t i = 0; i < pointerCount; i++) {
138 pointers[i] = other.pointers[i];
139 idToIndex[i] = other.idToIndex[i];
140 }
141}
142
143
144// --- InputDevice::KeyboardState ---
145
146void InputDevice::KeyboardState::reset() {
147 current.metaState = META_NONE;
148 current.downTime = 0;
149}
150
151
152// --- InputDevice::TrackballState ---
153
154void InputDevice::TrackballState::reset() {
155 accumulator.clear();
156 current.down = false;
157 current.downTime = 0;
158}
159
160
161// --- InputDevice::TouchScreenState ---
162
163void InputDevice::TouchScreenState::reset() {
164 lastTouch.clear();
165 downTime = 0;
166 currentVirtualKey.down = false;
167
168 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
169 averagingTouchFilter.historyStart[i] = 0;
170 averagingTouchFilter.historyEnd[i] = 0;
171 }
172
173 jumpyTouchFilter.jumpyPointsDropped = 0;
174}
175
176void InputDevice::TouchScreenState::calculatePointerIds() {
177 uint32_t currentPointerCount = currentTouch.pointerCount;
178 uint32_t lastPointerCount = lastTouch.pointerCount;
179
180 if (currentPointerCount == 0) {
181 // No pointers to assign.
182 currentTouch.idBits.clear();
183 } else if (lastPointerCount == 0) {
184 // All pointers are new.
185 currentTouch.idBits.clear();
186 for (uint32_t i = 0; i < currentPointerCount; i++) {
187 currentTouch.pointers[i].id = i;
188 currentTouch.idToIndex[i] = i;
189 currentTouch.idBits.markBit(i);
190 }
191 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
192 // Only one pointer and no change in count so it must have the same id as before.
193 uint32_t id = lastTouch.pointers[0].id;
194 currentTouch.pointers[0].id = id;
195 currentTouch.idToIndex[id] = 0;
196 currentTouch.idBits.value = BitSet32::valueForBit(id);
197 } else {
198 // General case.
199 // We build a heap of squared euclidean distances between current and last pointers
200 // associated with the current and last pointer indices. Then, we find the best
201 // match (by distance) for each current pointer.
202 struct {
203 uint32_t currentPointerIndex : 8;
204 uint32_t lastPointerIndex : 8;
205 uint64_t distance : 48; // squared distance
206 } heap[MAX_POINTERS * MAX_POINTERS];
207
208 uint32_t heapSize = 0;
209 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
210 currentPointerIndex++) {
211 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
212 lastPointerIndex++) {
213 int64_t deltaX = currentTouch.pointers[currentPointerIndex].x
214 - lastTouch.pointers[lastPointerIndex].x;
215 int64_t deltaY = currentTouch.pointers[currentPointerIndex].y
216 - lastTouch.pointers[lastPointerIndex].y;
217
218 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
219
220 // Insert new element into the heap (sift up).
221 heapSize += 1;
222 uint32_t insertionIndex = heapSize;
223 while (insertionIndex > 1) {
224 uint32_t parentIndex = (insertionIndex - 1) / 2;
225 if (distance < heap[parentIndex].distance) {
226 heap[insertionIndex] = heap[parentIndex];
227 insertionIndex = parentIndex;
228 } else {
229 break;
230 }
231 }
232 heap[insertionIndex].currentPointerIndex = currentPointerIndex;
233 heap[insertionIndex].lastPointerIndex = lastPointerIndex;
234 heap[insertionIndex].distance = distance;
235 }
236 }
237
238 // Pull matches out by increasing order of distance.
239 // To avoid reassigning pointers that have already been matched, the loop keeps track
240 // of which last and current pointers have been matched using the matchedXXXBits variables.
241 // It also tracks the used pointer id bits.
242 BitSet32 matchedLastBits(0);
243 BitSet32 matchedCurrentBits(0);
244 BitSet32 usedIdBits(0);
245 bool first = true;
246 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
247 for (;;) {
248 if (first) {
249 // The first time through the loop, we just consume the root element of
250 // the heap (the one with smalled distance).
251 first = false;
252 } else {
253 // Previous iterations consumed the root element of the heap.
254 // Pop root element off of the heap (sift down).
255 heapSize -= 1;
256 assert(heapSize > 0);
257
258 // Sift down to find where the element at index heapSize needs to be moved.
259 uint32_t rootIndex = 0;
260 for (;;) {
261 uint32_t childIndex = rootIndex * 2 + 1;
262 if (childIndex >= heapSize) {
263 break;
264 }
265
266 if (childIndex + 1 < heapSize
267 && heap[childIndex + 1].distance < heap[childIndex].distance) {
268 childIndex += 1;
269 }
270
271 if (heap[heapSize].distance < heap[childIndex].distance) {
272 break;
273 }
274
275 heap[rootIndex] = heap[childIndex];
276 rootIndex = childIndex;
277 }
278 heap[rootIndex] = heap[heapSize];
279 }
280
281 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
282 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
283
284 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
285 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
286
287 matchedCurrentBits.markBit(currentPointerIndex);
288 matchedLastBits.markBit(lastPointerIndex);
289
290 uint32_t id = lastTouch.pointers[lastPointerIndex].id;
291 currentTouch.pointers[currentPointerIndex].id = id;
292 currentTouch.idToIndex[id] = currentPointerIndex;
293 usedIdBits.markBit(id);
294 break;
295 }
296 }
297
298 // Assign fresh ids to new pointers.
299 if (currentPointerCount > lastPointerCount) {
300 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
301 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
302 uint32_t id = usedIdBits.firstUnmarkedBit();
303
304 currentTouch.pointers[currentPointerIndex].id = id;
305 currentTouch.idToIndex[id] = currentPointerIndex;
306 usedIdBits.markBit(id);
307
308 if (--i == 0) break; // done
309 matchedCurrentBits.markBit(currentPointerIndex);
310 }
311 }
312
313 // Fix id bits.
314 currentTouch.idBits = usedIdBits;
315 }
316}
317
318/* Special hack for devices that have bad screen data: if one of the
319 * points has moved more than a screen height from the last position,
320 * then drop it. */
321bool InputDevice::TouchScreenState::applyBadTouchFilter() {
322 uint32_t pointerCount = currentTouch.pointerCount;
323
324 // Nothing to do if there are no points.
325 if (pointerCount == 0) {
326 return false;
327 }
328
329 // Don't do anything if a finger is going down or up. We run
330 // here before assigning pointer IDs, so there isn't a good
331 // way to do per-finger matching.
332 if (pointerCount != lastTouch.pointerCount) {
333 return false;
334 }
335
336 // We consider a single movement across more than a 7/16 of
337 // the long size of the screen to be bad. This was a magic value
338 // determined by looking at the maximum distance it is feasible
339 // to actually move in one sample.
340 int32_t maxDeltaY = parameters.yAxis.range * 7 / 16;
341
342 // XXX The original code in InputDevice.java included commented out
343 // code for testing the X axis. Note that when we drop a point
344 // we don't actually restore the old X either. Strange.
345 // The old code also tries to track when bad points were previously
346 // detected but it turns out that due to the placement of a "break"
347 // at the end of the loop, we never set mDroppedBadPoint to true
348 // so it is effectively dead code.
349 // Need to figure out if the old code is busted or just overcomplicated
350 // but working as intended.
351
352 // Look through all new points and see if any are farther than
353 // acceptable from all previous points.
354 for (uint32_t i = pointerCount; i-- > 0; ) {
355 int32_t y = currentTouch.pointers[i].y;
356 int32_t closestY = INT_MAX;
357 int32_t closestDeltaY = 0;
358
359#if DEBUG_HACKS
360 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
361#endif
362
363 for (uint32_t j = pointerCount; j-- > 0; ) {
364 int32_t lastY = lastTouch.pointers[j].y;
365 int32_t deltaY = abs(y - lastY);
366
367#if DEBUG_HACKS
368 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
369 j, lastY, deltaY);
370#endif
371
372 if (deltaY < maxDeltaY) {
373 goto SkipSufficientlyClosePoint;
374 }
375 if (deltaY < closestDeltaY) {
376 closestDeltaY = deltaY;
377 closestY = lastY;
378 }
379 }
380
381 // Must not have found a close enough match.
382#if DEBUG_HACKS
383 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
384 i, y, closestY, closestDeltaY, maxDeltaY);
385#endif
386
387 currentTouch.pointers[i].y = closestY;
388 return true; // XXX original code only corrects one point
389
390 SkipSufficientlyClosePoint: ;
391 }
392
393 // No change.
394 return false;
395}
396
397/* Special hack for devices that have bad screen data: drop points where
398 * the coordinate value for one axis has jumped to the other pointer's location.
399 */
400bool InputDevice::TouchScreenState::applyJumpyTouchFilter() {
401 uint32_t pointerCount = currentTouch.pointerCount;
402 if (lastTouch.pointerCount != pointerCount) {
403#if DEBUG_HACKS
404 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
405 lastTouch.pointerCount, pointerCount);
406 for (uint32_t i = 0; i < pointerCount; i++) {
407 LOGD(" Pointer %d (%d, %d)", i,
408 currentTouch.pointers[i].x, currentTouch.pointers[i].y);
409 }
410#endif
411
412 if (jumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
413 if (lastTouch.pointerCount == 1 && pointerCount == 2) {
414 // Just drop the first few events going from 1 to 2 pointers.
415 // They're bad often enough that they're not worth considering.
416 currentTouch.pointerCount = 1;
417 jumpyTouchFilter.jumpyPointsDropped += 1;
418
419#if DEBUG_HACKS
420 LOGD("JumpyTouchFilter: Pointer 2 dropped");
421#endif
422 return true;
423 } else if (lastTouch.pointerCount == 2 && pointerCount == 1) {
424 // The event when we go from 2 -> 1 tends to be messed up too
425 currentTouch.pointerCount = 2;
426 currentTouch.pointers[0] = lastTouch.pointers[0];
427 currentTouch.pointers[1] = lastTouch.pointers[1];
428 jumpyTouchFilter.jumpyPointsDropped += 1;
429
430#if DEBUG_HACKS
431 for (int32_t i = 0; i < 2; i++) {
432 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
433 currentTouch.pointers[i].x, currentTouch.pointers[i].y);
434 }
435#endif
436 return true;
437 }
438 }
439 // Reset jumpy points dropped on other transitions or if limit exceeded.
440 jumpyTouchFilter.jumpyPointsDropped = 0;
441
442#if DEBUG_HACKS
443 LOGD("JumpyTouchFilter: Transition - drop limit reset");
444#endif
445 return false;
446 }
447
448 // We have the same number of pointers as last time.
449 // A 'jumpy' point is one where the coordinate value for one axis
450 // has jumped to the other pointer's location. No need to do anything
451 // else if we only have one pointer.
452 if (pointerCount < 2) {
453 return false;
454 }
455
456 if (jumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
457 int jumpyEpsilon = parameters.yAxis.range / JUMPY_EPSILON_DIVISOR;
458
459 // We only replace the single worst jumpy point as characterized by pointer distance
460 // in a single axis.
461 int32_t badPointerIndex = -1;
462 int32_t badPointerReplacementIndex = -1;
463 int32_t badPointerDistance = INT_MIN; // distance to be corrected
464
465 for (uint32_t i = pointerCount; i-- > 0; ) {
466 int32_t x = currentTouch.pointers[i].x;
467 int32_t y = currentTouch.pointers[i].y;
468
469#if DEBUG_HACKS
470 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
471#endif
472
473 // Check if a touch point is too close to another's coordinates
474 bool dropX = false, dropY = false;
475 for (uint32_t j = 0; j < pointerCount; j++) {
476 if (i == j) {
477 continue;
478 }
479
480 if (abs(x - currentTouch.pointers[j].x) <= jumpyEpsilon) {
481 dropX = true;
482 break;
483 }
484
485 if (abs(y - currentTouch.pointers[j].y) <= jumpyEpsilon) {
486 dropY = true;
487 break;
488 }
489 }
490 if (! dropX && ! dropY) {
491 continue; // not jumpy
492 }
493
494 // Find a replacement candidate by comparing with older points on the
495 // complementary (non-jumpy) axis.
496 int32_t distance = INT_MIN; // distance to be corrected
497 int32_t replacementIndex = -1;
498
499 if (dropX) {
500 // X looks too close. Find an older replacement point with a close Y.
501 int32_t smallestDeltaY = INT_MAX;
502 for (uint32_t j = 0; j < pointerCount; j++) {
503 int32_t deltaY = abs(y - lastTouch.pointers[j].y);
504 if (deltaY < smallestDeltaY) {
505 smallestDeltaY = deltaY;
506 replacementIndex = j;
507 }
508 }
509 distance = abs(x - lastTouch.pointers[replacementIndex].x);
510 } else {
511 // Y looks too close. Find an older replacement point with a close X.
512 int32_t smallestDeltaX = INT_MAX;
513 for (uint32_t j = 0; j < pointerCount; j++) {
514 int32_t deltaX = abs(x - lastTouch.pointers[j].x);
515 if (deltaX < smallestDeltaX) {
516 smallestDeltaX = deltaX;
517 replacementIndex = j;
518 }
519 }
520 distance = abs(y - lastTouch.pointers[replacementIndex].y);
521 }
522
523 // If replacing this pointer would correct a worse error than the previous ones
524 // considered, then use this replacement instead.
525 if (distance > badPointerDistance) {
526 badPointerIndex = i;
527 badPointerReplacementIndex = replacementIndex;
528 badPointerDistance = distance;
529 }
530 }
531
532 // Correct the jumpy pointer if one was found.
533 if (badPointerIndex >= 0) {
534#if DEBUG_HACKS
535 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
536 badPointerIndex,
537 lastTouch.pointers[badPointerReplacementIndex].x,
538 lastTouch.pointers[badPointerReplacementIndex].y);
539#endif
540
541 currentTouch.pointers[badPointerIndex].x =
542 lastTouch.pointers[badPointerReplacementIndex].x;
543 currentTouch.pointers[badPointerIndex].y =
544 lastTouch.pointers[badPointerReplacementIndex].y;
545 jumpyTouchFilter.jumpyPointsDropped += 1;
546 return true;
547 }
548 }
549
550 jumpyTouchFilter.jumpyPointsDropped = 0;
551 return false;
552}
553
554/* Special hack for devices that have bad screen data: aggregate and
555 * compute averages of the coordinate data, to reduce the amount of
556 * jitter seen by applications. */
557void InputDevice::TouchScreenState::applyAveragingTouchFilter() {
558 for (uint32_t currentIndex = 0; currentIndex < currentTouch.pointerCount; currentIndex++) {
559 uint32_t id = currentTouch.pointers[currentIndex].id;
560 int32_t x = currentTouch.pointers[currentIndex].x;
561 int32_t y = currentTouch.pointers[currentIndex].y;
562 int32_t pressure = currentTouch.pointers[currentIndex].pressure;
563
564 if (lastTouch.idBits.hasBit(id)) {
565 // Pointer still down compute average.
566 uint32_t start = averagingTouchFilter.historyStart[id];
567 uint32_t end = averagingTouchFilter.historyEnd[id];
568
569 int64_t deltaX = x - averagingTouchFilter.historyData[end].pointers[id].x;
570 int64_t deltaY = y - averagingTouchFilter.historyData[end].pointers[id].y;
571 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
572
573#if DEBUG_HACKS
574 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
575 id, distance);
576#endif
577
578 if (distance < AVERAGING_DISTANCE_LIMIT) {
579 end += 1;
580 if (end > AVERAGING_HISTORY_SIZE) {
581 end = 0;
582 }
583
584 if (end == start) {
585 start += 1;
586 if (start > AVERAGING_HISTORY_SIZE) {
587 start = 0;
588 }
589 }
590
591 averagingTouchFilter.historyStart[id] = start;
592 averagingTouchFilter.historyEnd[id] = end;
593 averagingTouchFilter.historyData[end].pointers[id].x = x;
594 averagingTouchFilter.historyData[end].pointers[id].y = y;
595 averagingTouchFilter.historyData[end].pointers[id].pressure = pressure;
596
597 int32_t averagedX = 0;
598 int32_t averagedY = 0;
599 int32_t totalPressure = 0;
600 for (;;) {
601 int32_t historicalX = averagingTouchFilter.historyData[start].pointers[id].x;
602 int32_t historicalY = averagingTouchFilter.historyData[start].pointers[id].x;
603 int32_t historicalPressure = averagingTouchFilter.historyData[start]
604 .pointers[id].pressure;
605
606 averagedX += historicalX;
607 averagedY += historicalY;
608 totalPressure += historicalPressure;
609
610 if (start == end) {
611 break;
612 }
613
614 start += 1;
615 if (start > AVERAGING_HISTORY_SIZE) {
616 start = 0;
617 }
618 }
619
620 averagedX /= totalPressure;
621 averagedY /= totalPressure;
622
623#if DEBUG_HACKS
624 LOGD("AveragingTouchFilter: Pointer id %d - "
625 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
626 averagedX, averagedY);
627#endif
628
629 currentTouch.pointers[currentIndex].x = averagedX;
630 currentTouch.pointers[currentIndex].y = averagedY;
631 } else {
632#if DEBUG_HACKS
633 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
634#endif
635 }
636 } else {
637#if DEBUG_HACKS
638 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
639#endif
640 }
641
642 // Reset pointer history.
643 averagingTouchFilter.historyStart[id] = 0;
644 averagingTouchFilter.historyEnd[id] = 0;
645 averagingTouchFilter.historyData[0].pointers[id].x = x;
646 averagingTouchFilter.historyData[0].pointers[id].y = y;
647 averagingTouchFilter.historyData[0].pointers[id].pressure = pressure;
648 }
649}
650
651bool InputDevice::TouchScreenState::isPointInsideDisplay(int32_t x, int32_t y) const {
652 return x >= parameters.xAxis.minValue
653 && x <= parameters.xAxis.maxValue
654 && y >= parameters.yAxis.minValue
655 && y <= parameters.yAxis.maxValue;
656}
657
658
659// --- InputDevice::SingleTouchScreenState ---
660
661void InputDevice::SingleTouchScreenState::reset() {
662 accumulator.clear();
663 current.down = false;
664 current.x = 0;
665 current.y = 0;
666 current.pressure = 0;
667 current.size = 0;
668}
669
670
671// --- InputDevice::MultiTouchScreenState ---
672
673void InputDevice::MultiTouchScreenState::reset() {
674 accumulator.clear();
675}
676
677
678// --- InputReader ---
679
680InputReader::InputReader(const sp<EventHubInterface>& eventHub,
681 const sp<InputDispatchPolicyInterface>& policy,
682 const sp<InputDispatcherInterface>& dispatcher) :
683 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher) {
684 resetGlobalMetaState();
685 resetDisplayProperties();
686 updateGlobalVirtualKeyState();
687}
688
689InputReader::~InputReader() {
690 for (size_t i = 0; i < mDevices.size(); i++) {
691 delete mDevices.valueAt(i);
692 }
693}
694
695void InputReader::loopOnce() {
696 RawEvent rawEvent;
697 mEventHub->getEvent(& rawEvent.deviceId, & rawEvent.type, & rawEvent.scanCode,
698 & rawEvent.keyCode, & rawEvent.flags, & rawEvent.value, & rawEvent.when);
699
700 // Replace the event timestamp so it is in same timebase as java.lang.System.nanoTime()
701 // and android.os.SystemClock.uptimeMillis() as expected by the rest of the system.
702 rawEvent.when = systemTime(SYSTEM_TIME_MONOTONIC);
703
704#if DEBUG_RAW_EVENTS
705 LOGD("Input event: device=0x%x type=0x%x scancode=%d keycode=%d value=%d",
706 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
707 rawEvent.value);
708#endif
709
710 process(& rawEvent);
711}
712
713void InputReader::process(const RawEvent* rawEvent) {
714 switch (rawEvent->type) {
715 case EventHubInterface::DEVICE_ADDED:
716 handleDeviceAdded(rawEvent);
717 break;
718
719 case EventHubInterface::DEVICE_REMOVED:
720 handleDeviceRemoved(rawEvent);
721 break;
722
723 case EV_SYN:
724 handleSync(rawEvent);
725 break;
726
727 case EV_KEY:
728 handleKey(rawEvent);
729 break;
730
731 case EV_REL:
732 handleRelativeMotion(rawEvent);
733 break;
734
735 case EV_ABS:
736 handleAbsoluteMotion(rawEvent);
737 break;
738
739 case EV_SW:
740 handleSwitch(rawEvent);
741 break;
742 }
743}
744
745void InputReader::handleDeviceAdded(const RawEvent* rawEvent) {
746 InputDevice* device = getDevice(rawEvent->deviceId);
747 if (device) {
748 LOGW("Ignoring spurious device added event for deviceId %d.", rawEvent->deviceId);
749 return;
750 }
751
752 addDevice(rawEvent->when, rawEvent->deviceId);
753}
754
755void InputReader::handleDeviceRemoved(const RawEvent* rawEvent) {
756 InputDevice* device = getDevice(rawEvent->deviceId);
757 if (! device) {
758 LOGW("Ignoring spurious device removed event for deviceId %d.", rawEvent->deviceId);
759 return;
760 }
761
762 removeDevice(rawEvent->when, device);
763}
764
765void InputReader::handleSync(const RawEvent* rawEvent) {
766 InputDevice* device = getNonIgnoredDevice(rawEvent->deviceId);
767 if (! device) return;
768
769 if (rawEvent->scanCode == SYN_MT_REPORT) {
770 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
771 // We drop pointers with pressure <= 0 since that indicates they are not down.
772 if (device->isMultiTouchScreen()) {
773 uint32_t pointerIndex = device->multiTouchScreen.accumulator.pointerCount;
774
775 if (device->multiTouchScreen.accumulator.pointers[pointerIndex].fields) {
776 if (pointerIndex == MAX_POINTERS) {
777 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
778 MAX_POINTERS);
779 } else {
780 pointerIndex += 1;
781 device->multiTouchScreen.accumulator.pointerCount = pointerIndex;
782 }
783 }
784
785 device->multiTouchScreen.accumulator.pointers[pointerIndex].clear();
786 }
787 } else if (rawEvent->scanCode == SYN_REPORT) {
788 // General Sync: The driver has returned all data for the current event update.
789 if (device->isMultiTouchScreen()) {
790 if (device->multiTouchScreen.accumulator.isDirty()) {
791 onMultiTouchScreenStateChanged(rawEvent->when, device);
792 device->multiTouchScreen.accumulator.clear();
793 }
794 } else if (device->isSingleTouchScreen()) {
795 if (device->singleTouchScreen.accumulator.isDirty()) {
796 onSingleTouchScreenStateChanged(rawEvent->when, device);
797 device->singleTouchScreen.accumulator.clear();
798 }
799 }
800
801 if (device->trackball.accumulator.isDirty()) {
802 onTrackballStateChanged(rawEvent->when, device);
803 device->trackball.accumulator.clear();
804 }
805 }
806}
807
808void InputReader::handleKey(const RawEvent* rawEvent) {
809 InputDevice* device = getNonIgnoredDevice(rawEvent->deviceId);
810 if (! device) return;
811
812 bool down = rawEvent->value != 0;
813 int32_t scanCode = rawEvent->scanCode;
814
815 if (device->isKeyboard() && (scanCode < BTN_FIRST || scanCode > BTN_LAST)) {
816 int32_t keyCode = rawEvent->keyCode;
817 onKey(rawEvent->when, device, down, keyCode, scanCode, rawEvent->flags);
818 } else if (device->isSingleTouchScreen()) {
819 switch (rawEvent->scanCode) {
820 case BTN_TOUCH:
821 device->singleTouchScreen.accumulator.fields |=
822 InputDevice::SingleTouchScreenState::Accumulator::FIELD_BTN_TOUCH;
823 device->singleTouchScreen.accumulator.btnTouch = down;
824 break;
825 }
826 } else if (device->isTrackball()) {
827 switch (rawEvent->scanCode) {
828 case BTN_MOUSE:
829 device->trackball.accumulator.fields |=
830 InputDevice::TrackballState::Accumulator::FIELD_BTN_MOUSE;
831 device->trackball.accumulator.btnMouse = down;
832
833 // send the down immediately
834 // XXX this emulates the old behavior of KeyInputQueue, unclear whether it is
835 // necessary or if we can wait until the next sync
836 onTrackballStateChanged(rawEvent->when, device);
837 device->trackball.accumulator.clear();
838 break;
839 }
840 }
841}
842
843void InputReader::handleRelativeMotion(const RawEvent* rawEvent) {
844 InputDevice* device = getNonIgnoredDevice(rawEvent->deviceId);
845 if (! device) return;
846
847 if (device->isTrackball()) {
848 switch (rawEvent->scanCode) {
849 case REL_X:
850 device->trackball.accumulator.fields |=
851 InputDevice::TrackballState::Accumulator::FIELD_REL_X;
852 device->trackball.accumulator.relX = rawEvent->value;
853 break;
854 case REL_Y:
855 device->trackball.accumulator.fields |=
856 InputDevice::TrackballState::Accumulator::FIELD_REL_Y;
857 device->trackball.accumulator.relY = rawEvent->value;
858 break;
859 }
860 }
861}
862
863void InputReader::handleAbsoluteMotion(const RawEvent* rawEvent) {
864 InputDevice* device = getNonIgnoredDevice(rawEvent->deviceId);
865 if (! device) return;
866
867 if (device->isMultiTouchScreen()) {
868 uint32_t pointerIndex = device->multiTouchScreen.accumulator.pointerCount;
869 InputDevice::MultiTouchScreenState::Accumulator::Pointer* pointer =
870 & device->multiTouchScreen.accumulator.pointers[pointerIndex];
871
872 switch (rawEvent->scanCode) {
873 case ABS_MT_POSITION_X:
874 pointer->fields |=
875 InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_POSITION_X;
876 pointer->absMTPositionX = rawEvent->value;
877 break;
878 case ABS_MT_POSITION_Y:
879 pointer->fields |=
880 InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_POSITION_Y;
881 pointer->absMTPositionY = rawEvent->value;
882 break;
883 case ABS_MT_TOUCH_MAJOR:
884 pointer->fields |=
885 InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
886 pointer->absMTTouchMajor = rawEvent->value;
887 break;
888 case ABS_MT_WIDTH_MAJOR:
889 pointer->fields |=
890 InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
891 pointer->absMTWidthMajor = rawEvent->value;
892 break;
893 case ABS_MT_TRACKING_ID:
894 pointer->fields |=
895 InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_TRACKING_ID;
896 pointer->absMTTrackingId = rawEvent->value;
897 break;
898 }
899 } else if (device->isSingleTouchScreen()) {
900 switch (rawEvent->scanCode) {
901 case ABS_X:
902 device->singleTouchScreen.accumulator.fields |=
903 InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_X;
904 device->singleTouchScreen.accumulator.absX = rawEvent->value;
905 break;
906 case ABS_Y:
907 device->singleTouchScreen.accumulator.fields |=
908 InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_Y;
909 device->singleTouchScreen.accumulator.absY = rawEvent->value;
910 break;
911 case ABS_PRESSURE:
912 device->singleTouchScreen.accumulator.fields |=
913 InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_PRESSURE;
914 device->singleTouchScreen.accumulator.absPressure = rawEvent->value;
915 break;
916 case ABS_TOOL_WIDTH:
917 device->singleTouchScreen.accumulator.fields |=
918 InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_TOOL_WIDTH;
919 device->singleTouchScreen.accumulator.absToolWidth = rawEvent->value;
920 break;
921 }
922 }
923}
924
925void InputReader::handleSwitch(const RawEvent* rawEvent) {
926 InputDevice* device = getNonIgnoredDevice(rawEvent->deviceId);
927 if (! device) return;
928
929 onSwitch(rawEvent->when, device, rawEvent->value != 0, rawEvent->scanCode);
930}
931
932void InputReader::onKey(nsecs_t when, InputDevice* device,
933 bool down, int32_t keyCode, int32_t scanCode, uint32_t policyFlags) {
934 /* Refresh display properties so we can rotate key codes according to display orientation */
935
936 if (! refreshDisplayProperties()) {
937 return;
938 }
939
940 /* Update device state */
941
942 int32_t oldMetaState = device->keyboard.current.metaState;
943 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
944 if (oldMetaState != newMetaState) {
945 device->keyboard.current.metaState = newMetaState;
946 resetGlobalMetaState();
947 }
948
949 // FIXME if we send a down event about a rotated key press we should ensure that we send
950 // a corresponding up event about the rotated key press even if the orientation
951 // has changed in the meantime
952 keyCode = rotateKeyCode(keyCode, mDisplayOrientation);
953
954 if (down) {
955 device->keyboard.current.downTime = when;
956 }
957
958 /* Apply policy */
959
960 int32_t policyActions = mPolicy->interceptKey(when, device->id,
961 down, keyCode, scanCode, policyFlags);
962
963 if (! applyStandardInputDispatchPolicyActions(when, policyActions, & policyFlags)) {
964 return; // event dropped
965 }
966
967 /* Enqueue key event for dispatch */
968
969 int32_t keyEventAction;
970 if (down) {
971 device->keyboard.current.downTime = when;
972 keyEventAction = KEY_EVENT_ACTION_DOWN;
973 } else {
974 keyEventAction = KEY_EVENT_ACTION_UP;
975 }
976
977 int32_t keyEventFlags = KEY_EVENT_FLAG_FROM_SYSTEM;
978 if (policyActions & InputDispatchPolicyInterface::ACTION_WOKE_HERE) {
979 keyEventFlags = keyEventFlags | KEY_EVENT_FLAG_WOKE_HERE;
980 }
981
982 mDispatcher->notifyKey(when, device->id, INPUT_EVENT_NATURE_KEY, policyFlags,
983 keyEventAction, keyEventFlags, keyCode, scanCode,
984 device->keyboard.current.metaState,
985 device->keyboard.current.downTime);
986}
987
988void InputReader::onSwitch(nsecs_t when, InputDevice* device, bool down,
989 int32_t code) {
990 switch (code) {
991 case SW_LID:
992 mDispatcher->notifyLidSwitchChanged(when, ! down);
993 }
994}
995
996void InputReader::onMultiTouchScreenStateChanged(nsecs_t when,
997 InputDevice* device) {
998 static const uint32_t REQUIRED_FIELDS =
999 InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_POSITION_X
1000 | InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_POSITION_Y
1001 | InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_TOUCH_MAJOR
1002 | InputDevice::MultiTouchScreenState::Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
1003
1004 /* Refresh display properties so we can map touch screen coords into display coords */
1005
1006 if (! refreshDisplayProperties()) {
1007 return;
1008 }
1009
1010 /* Update device state */
1011
1012 InputDevice::MultiTouchScreenState* in = & device->multiTouchScreen;
1013 InputDevice::TouchData* out = & device->touchScreen.currentTouch;
1014
1015 uint32_t inCount = in->accumulator.pointerCount;
1016 uint32_t outCount = 0;
1017 bool havePointerIds = true;
1018
1019 out->clear();
1020
1021 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
1022 uint32_t fields = in->accumulator.pointers[inIndex].fields;
1023
1024 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
1025#if DEBUG_POINTERS
1026 LOGD("Pointers: Missing required multitouch pointer fields: index=%d, fields=%d",
1027 inIndex, fields);
1028 continue;
1029#endif
1030 }
1031
1032 if (in->accumulator.pointers[inIndex].absMTTouchMajor <= 0) {
1033 // Pointer is not down. Drop it.
1034 continue;
1035 }
1036
1037 // FIXME assignment of pressure may be incorrect, probably better to let
1038 // pressure = touch / width. Later on we pass width to MotionEvent as a size, which
1039 // isn't quite right either. Should be using touch for that.
1040 out->pointers[outCount].x = in->accumulator.pointers[inIndex].absMTPositionX;
1041 out->pointers[outCount].y = in->accumulator.pointers[inIndex].absMTPositionY;
1042 out->pointers[outCount].pressure = in->accumulator.pointers[inIndex].absMTTouchMajor;
1043 out->pointers[outCount].size = in->accumulator.pointers[inIndex].absMTWidthMajor;
1044
1045 if (havePointerIds) {
1046 if (fields & InputDevice::MultiTouchScreenState::Accumulator::
1047 FIELD_ABS_MT_TRACKING_ID) {
1048 uint32_t id = uint32_t(in->accumulator.pointers[inIndex].absMTTrackingId);
1049
1050 if (id > MAX_POINTER_ID) {
1051#if DEBUG_POINTERS
1052 LOGD("Pointers: Ignoring driver provided pointer id %d because "
1053 "it is larger than max supported id %d for optimizations",
1054 id, MAX_POINTER_ID);
1055#endif
1056 havePointerIds = false;
1057 }
1058 else {
1059 out->pointers[outCount].id = id;
1060 out->idToIndex[id] = outCount;
1061 out->idBits.markBit(id);
1062 }
1063 } else {
1064 havePointerIds = false;
1065 }
1066 }
1067
1068 outCount += 1;
1069 }
1070
1071 out->pointerCount = outCount;
1072
1073 onTouchScreenChanged(when, device, havePointerIds);
1074}
1075
1076void InputReader::onSingleTouchScreenStateChanged(nsecs_t when,
1077 InputDevice* device) {
1078 static const uint32_t POSITION_FIELDS =
1079 InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_X
1080 | InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_Y
1081 | InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_PRESSURE
1082 | InputDevice::SingleTouchScreenState::Accumulator::FIELD_ABS_TOOL_WIDTH;
1083
1084 /* Refresh display properties so we can map touch screen coords into display coords */
1085
1086 if (! refreshDisplayProperties()) {
1087 return;
1088 }
1089
1090 /* Update device state */
1091
1092 InputDevice::SingleTouchScreenState* in = & device->singleTouchScreen;
1093 InputDevice::TouchData* out = & device->touchScreen.currentTouch;
1094
1095 uint32_t fields = in->accumulator.fields;
1096
1097 if (fields & InputDevice::SingleTouchScreenState::Accumulator::FIELD_BTN_TOUCH) {
1098 in->current.down = in->accumulator.btnTouch;
1099 }
1100
1101 if ((fields & POSITION_FIELDS) == POSITION_FIELDS) {
1102 in->current.x = in->accumulator.absX;
1103 in->current.y = in->accumulator.absY;
1104 in->current.pressure = in->accumulator.absPressure;
1105 in->current.size = in->accumulator.absToolWidth;
1106 }
1107
1108 out->clear();
1109
1110 if (in->current.down) {
1111 out->pointerCount = 1;
1112 out->pointers[0].id = 0;
1113 out->pointers[0].x = in->current.x;
1114 out->pointers[0].y = in->current.y;
1115 out->pointers[0].pressure = in->current.pressure;
1116 out->pointers[0].size = in->current.size;
1117 out->idToIndex[0] = 0;
1118 out->idBits.markBit(0);
1119 }
1120
1121 onTouchScreenChanged(when, device, true);
1122}
1123
1124void InputReader::onTouchScreenChanged(nsecs_t when,
1125 InputDevice* device, bool havePointerIds) {
1126 /* Apply policy */
1127
1128 int32_t policyActions = mPolicy->interceptTouch(when);
1129
1130 uint32_t policyFlags = 0;
1131 if (! applyStandardInputDispatchPolicyActions(when, policyActions, & policyFlags)) {
1132 device->touchScreen.lastTouch.clear();
1133 return; // event dropped
1134 }
1135
1136 /* Preprocess pointer data */
1137
1138 if (device->touchScreen.parameters.useBadTouchFilter) {
1139 if (device->touchScreen.applyBadTouchFilter()) {
1140 havePointerIds = false;
1141 }
1142 }
1143
1144 if (device->touchScreen.parameters.useJumpyTouchFilter) {
1145 if (device->touchScreen.applyJumpyTouchFilter()) {
1146 havePointerIds = false;
1147 }
1148 }
1149
1150 if (! havePointerIds) {
1151 device->touchScreen.calculatePointerIds();
1152 }
1153
1154 InputDevice::TouchData temp;
1155 InputDevice::TouchData* savedTouch;
1156 if (device->touchScreen.parameters.useAveragingTouchFilter) {
1157 temp.copyFrom(device->touchScreen.currentTouch);
1158 savedTouch = & temp;
1159
1160 device->touchScreen.applyAveragingTouchFilter();
1161 } else {
1162 savedTouch = & device->touchScreen.currentTouch;
1163 }
1164
1165 /* Process virtual keys or touches */
1166
1167 if (! consumeVirtualKeyTouches(when, device, policyFlags)) {
1168 dispatchTouches(when, device, policyFlags);
1169 }
1170
1171 // Copy current touch to last touch in preparation for the next cycle.
1172 device->touchScreen.lastTouch.copyFrom(*savedTouch);
1173}
1174
1175bool InputReader::consumeVirtualKeyTouches(nsecs_t when,
1176 InputDevice* device, uint32_t policyFlags) {
1177 if (device->touchScreen.currentVirtualKey.down) {
1178 if (device->touchScreen.currentTouch.pointerCount == 0) {
1179 // Pointer went up while virtual key was down. Send key up event.
1180 device->touchScreen.currentVirtualKey.down = false;
1181
1182#if DEBUG_VIRTUAL_KEYS
1183 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
1184 device->touchScreen.currentVirtualKey.keyCode,
1185 device->touchScreen.currentVirtualKey.scanCode);
1186#endif
1187
1188 dispatchVirtualKey(when, device, policyFlags, KEY_EVENT_ACTION_UP,
1189 KEY_EVENT_FLAG_FROM_SYSTEM | KEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1190 return true; // consumed
1191 }
1192
1193 int32_t x = device->touchScreen.currentTouch.pointers[0].x;
1194 int32_t y = device->touchScreen.currentTouch.pointers[0].y;
1195 if (device->touchScreen.isPointInsideDisplay(x, y)) {
1196 // Pointer moved inside the display area. Send key cancellation.
1197 device->touchScreen.currentVirtualKey.down = false;
1198
1199#if DEBUG_VIRTUAL_KEYS
1200 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
1201 device->touchScreen.currentVirtualKey.keyCode,
1202 device->touchScreen.currentVirtualKey.scanCode);
1203#endif
1204
1205 dispatchVirtualKey(when, device, policyFlags, KEY_EVENT_ACTION_UP,
1206 KEY_EVENT_FLAG_FROM_SYSTEM | KEY_EVENT_FLAG_VIRTUAL_HARD_KEY
1207 | KEY_EVENT_FLAG_CANCELED);
1208
1209 // Clear the last touch data so we will consider the pointer as having just been
1210 // pressed down when generating subsequent motion events.
1211 device->touchScreen.lastTouch.clear();
1212 return false; // not consumed
1213 }
1214 } else if (device->touchScreen.currentTouch.pointerCount > 0
1215 && device->touchScreen.lastTouch.pointerCount == 0) {
1216 int32_t x = device->touchScreen.currentTouch.pointers[0].x;
1217 int32_t y = device->touchScreen.currentTouch.pointers[0].y;
1218 for (size_t i = 0; i < device->touchScreen.virtualKeys.size(); i++) {
1219 const InputDevice::VirtualKey& virtualKey = device->touchScreen.virtualKeys[i];
1220
1221#if DEBUG_VIRTUAL_KEYS
1222 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
1223 "left=%d, top=%d, right=%d, bottom=%d",
1224 x, y,
1225 virtualKey.keyCode, virtualKey.scanCode,
1226 virtualKey.hitLeft, virtualKey.hitTop,
1227 virtualKey.hitRight, virtualKey.hitBottom);
1228#endif
1229
1230 if (virtualKey.isHit(x, y)) {
1231 device->touchScreen.currentVirtualKey.down = true;
1232 device->touchScreen.currentVirtualKey.downTime = when;
1233 device->touchScreen.currentVirtualKey.keyCode = virtualKey.keyCode;
1234 device->touchScreen.currentVirtualKey.scanCode = virtualKey.scanCode;
1235
1236#if DEBUG_VIRTUAL_KEYS
1237 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
1238 device->touchScreen.currentVirtualKey.keyCode,
1239 device->touchScreen.currentVirtualKey.scanCode);
1240#endif
1241
1242 dispatchVirtualKey(when, device, policyFlags, KEY_EVENT_ACTION_DOWN,
1243 KEY_EVENT_FLAG_FROM_SYSTEM | KEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
1244 return true; // consumed
1245 }
1246 }
1247 }
1248
1249 return false; // not consumed
1250}
1251
1252void InputReader::dispatchVirtualKey(nsecs_t when,
1253 InputDevice* device, uint32_t policyFlags,
1254 int32_t keyEventAction, int32_t keyEventFlags) {
1255 int32_t keyCode = device->touchScreen.currentVirtualKey.keyCode;
1256 int32_t scanCode = device->touchScreen.currentVirtualKey.scanCode;
1257 nsecs_t downTime = device->touchScreen.currentVirtualKey.downTime;
1258 int32_t metaState = globalMetaState();
1259
1260 updateGlobalVirtualKeyState();
1261
1262 mPolicy->virtualKeyFeedback(when, device->id, keyEventAction, keyEventFlags,
1263 keyCode, scanCode, metaState, downTime);
1264
1265 mDispatcher->notifyKey(when, device->id, INPUT_EVENT_NATURE_KEY, policyFlags,
1266 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
1267}
1268
1269void InputReader::dispatchTouches(nsecs_t when,
1270 InputDevice* device, uint32_t policyFlags) {
1271 uint32_t currentPointerCount = device->touchScreen.currentTouch.pointerCount;
1272 uint32_t lastPointerCount = device->touchScreen.lastTouch.pointerCount;
1273 if (currentPointerCount == 0 && lastPointerCount == 0) {
1274 return; // nothing to do!
1275 }
1276
1277 BitSet32 currentIdBits = device->touchScreen.currentTouch.idBits;
1278 BitSet32 lastIdBits = device->touchScreen.lastTouch.idBits;
1279
1280 if (currentIdBits == lastIdBits) {
1281 // No pointer id changes so this is a move event.
1282 // The dispatcher takes care of batching moves so we don't have to deal with that here.
1283 int32_t motionEventAction = MOTION_EVENT_ACTION_MOVE;
1284 dispatchTouch(when, device, policyFlags, & device->touchScreen.currentTouch,
1285 currentIdBits, motionEventAction);
1286 } else {
1287 // There may be pointers going up and pointers going down at the same time when pointer
1288 // ids are reported by the device driver.
1289 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
1290 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
1291 BitSet32 activeIdBits(lastIdBits.value);
1292
1293 while (! upIdBits.isEmpty()) {
1294 uint32_t upId = upIdBits.firstMarkedBit();
1295 upIdBits.clearBit(upId);
1296 BitSet32 oldActiveIdBits = activeIdBits;
1297 activeIdBits.clearBit(upId);
1298
1299 int32_t motionEventAction;
1300 if (activeIdBits.isEmpty()) {
1301 motionEventAction = MOTION_EVENT_ACTION_UP;
1302 } else {
1303 motionEventAction = MOTION_EVENT_ACTION_POINTER_UP
1304 | (upId << MOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
1305 }
1306
1307 dispatchTouch(when, device, policyFlags, & device->touchScreen.lastTouch,
1308 oldActiveIdBits, motionEventAction);
1309 }
1310
1311 while (! downIdBits.isEmpty()) {
1312 uint32_t downId = downIdBits.firstMarkedBit();
1313 downIdBits.clearBit(downId);
1314 BitSet32 oldActiveIdBits = activeIdBits;
1315 activeIdBits.markBit(downId);
1316
1317 int32_t motionEventAction;
1318 if (oldActiveIdBits.isEmpty()) {
1319 motionEventAction = MOTION_EVENT_ACTION_DOWN;
1320 device->touchScreen.downTime = when;
1321 } else {
1322 motionEventAction = MOTION_EVENT_ACTION_POINTER_DOWN
1323 | (downId << MOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
1324 }
1325
1326 dispatchTouch(when, device, policyFlags, & device->touchScreen.currentTouch,
1327 activeIdBits, motionEventAction);
1328 }
1329 }
1330}
1331
1332void InputReader::dispatchTouch(nsecs_t when, InputDevice* device, uint32_t policyFlags,
1333 InputDevice::TouchData* touch, BitSet32 idBits,
1334 int32_t motionEventAction) {
1335 int32_t orientedWidth, orientedHeight;
1336 switch (mDisplayOrientation) {
1337 case InputDispatchPolicyInterface::ROTATION_90:
1338 case InputDispatchPolicyInterface::ROTATION_270:
1339 orientedWidth = mDisplayHeight;
1340 orientedHeight = mDisplayWidth;
1341 break;
1342 default:
1343 orientedWidth = mDisplayWidth;
1344 orientedHeight = mDisplayHeight;
1345 break;
1346 }
1347
1348 uint32_t pointerCount = 0;
1349 int32_t pointerIds[MAX_POINTERS];
1350 PointerCoords pointerCoords[MAX_POINTERS];
1351
1352 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
1353 // display coordinates (PointerCoords) and adjust for display orientation.
1354 while (! idBits.isEmpty()) {
1355 uint32_t id = idBits.firstMarkedBit();
1356 idBits.clearBit(id);
1357 uint32_t index = touch->idToIndex[id];
1358
1359 float x = (float(touch->pointers[index].x)
1360 - device->touchScreen.parameters.xAxis.minValue)
1361 * device->touchScreen.precalculated.xScale;
1362 float y = (float(touch->pointers[index].y)
1363 - device->touchScreen.parameters.yAxis.minValue)
1364 * device->touchScreen.precalculated.yScale;
1365 float pressure = (float(touch->pointers[index].pressure)
1366 - device->touchScreen.parameters.pressureAxis.minValue)
1367 * device->touchScreen.precalculated.pressureScale;
1368 float size = (float(touch->pointers[index].size)
1369 - device->touchScreen.parameters.sizeAxis.minValue)
1370 * device->touchScreen.precalculated.sizeScale;
1371
1372 switch (mDisplayOrientation) {
1373 case InputDispatchPolicyInterface::ROTATION_90: {
1374 float xTemp = x;
1375 x = y;
1376 y = mDisplayHeight - xTemp;
1377 break;
1378 }
1379 case InputDispatchPolicyInterface::ROTATION_180: {
1380 x = mDisplayWidth - x;
1381 y = mDisplayHeight - y;
1382 break;
1383 }
1384 case InputDispatchPolicyInterface::ROTATION_270: {
1385 float xTemp = x;
1386 x = mDisplayWidth - y;
1387 y = xTemp;
1388 break;
1389 }
1390 }
1391
1392 pointerIds[pointerCount] = int32_t(id);
1393
1394 pointerCoords[pointerCount].x = x;
1395 pointerCoords[pointerCount].y = y;
1396 pointerCoords[pointerCount].pressure = pressure;
1397 pointerCoords[pointerCount].size = size;
1398
1399 pointerCount += 1;
1400 }
1401
1402 // Check edge flags by looking only at the first pointer since the flags are
1403 // global to the event.
1404 // XXX Maybe we should revise the edge flags API to work on a per-pointer basis.
1405 int32_t motionEventEdgeFlags = 0;
1406 if (motionEventAction == MOTION_EVENT_ACTION_DOWN) {
1407 if (pointerCoords[0].x <= 0) {
1408 motionEventEdgeFlags |= MOTION_EVENT_EDGE_FLAG_LEFT;
1409 } else if (pointerCoords[0].x >= orientedWidth) {
1410 motionEventEdgeFlags |= MOTION_EVENT_EDGE_FLAG_RIGHT;
1411 }
1412 if (pointerCoords[0].y <= 0) {
1413 motionEventEdgeFlags |= MOTION_EVENT_EDGE_FLAG_TOP;
1414 } else if (pointerCoords[0].y >= orientedHeight) {
1415 motionEventEdgeFlags |= MOTION_EVENT_EDGE_FLAG_BOTTOM;
1416 }
1417 }
1418
1419 nsecs_t downTime = device->touchScreen.downTime;
1420 mDispatcher->notifyMotion(when, device->id, INPUT_EVENT_NATURE_TOUCH, policyFlags,
1421 motionEventAction, globalMetaState(), motionEventEdgeFlags,
1422 pointerCount, pointerIds, pointerCoords,
1423 0, 0, downTime);
1424}
1425
1426void InputReader::onTrackballStateChanged(nsecs_t when,
1427 InputDevice* device) {
1428 static const uint32_t DELTA_FIELDS =
1429 InputDevice::TrackballState::Accumulator::FIELD_REL_X
1430 | InputDevice::TrackballState::Accumulator::FIELD_REL_Y;
1431
1432 /* Refresh display properties so we can trackball moves according to display orientation */
1433
1434 if (! refreshDisplayProperties()) {
1435 return;
1436 }
1437
1438 /* Update device state */
1439
1440 uint32_t fields = device->trackball.accumulator.fields;
1441 bool downChanged = fields & InputDevice::TrackballState::Accumulator::FIELD_BTN_MOUSE;
1442 bool deltaChanged = (fields & DELTA_FIELDS) == DELTA_FIELDS;
1443
1444 bool down;
1445 if (downChanged) {
1446 if (device->trackball.accumulator.btnMouse) {
1447 device->trackball.current.down = true;
1448 device->trackball.current.downTime = when;
1449 down = true;
1450 } else {
1451 device->trackball.current.down = false;
1452 down = false;
1453 }
1454 } else {
1455 down = device->trackball.current.down;
1456 }
1457
1458 /* Apply policy */
1459
1460 int32_t policyActions = mPolicy->interceptTrackball(when, downChanged, down, deltaChanged);
1461
1462 uint32_t policyFlags = 0;
1463 if (! applyStandardInputDispatchPolicyActions(when, policyActions, & policyFlags)) {
1464 return; // event dropped
1465 }
1466
1467 /* Enqueue motion event for dispatch */
1468
1469 int32_t motionEventAction;
1470 if (downChanged) {
1471 motionEventAction = down ? MOTION_EVENT_ACTION_DOWN : MOTION_EVENT_ACTION_UP;
1472 } else {
1473 motionEventAction = MOTION_EVENT_ACTION_MOVE;
1474 }
1475
1476 int32_t pointerId = 0;
1477 PointerCoords pointerCoords;
1478 pointerCoords.x = device->trackball.accumulator.relX
1479 * device->trackball.precalculated.xScale;
1480 pointerCoords.y = device->trackball.accumulator.relY
1481 * device->trackball.precalculated.yScale;
1482 pointerCoords.pressure = 1.0f; // XXX Consider making this 1.0f if down, 0 otherwise.
1483 pointerCoords.size = 0;
1484
1485 float temp;
1486 switch (mDisplayOrientation) {
1487 case InputDispatchPolicyInterface::ROTATION_90:
1488 temp = pointerCoords.x;
1489 pointerCoords.x = pointerCoords.y;
1490 pointerCoords.y = - temp;
1491 break;
1492
1493 case InputDispatchPolicyInterface::ROTATION_180:
1494 pointerCoords.x = - pointerCoords.x;
1495 pointerCoords.y = - pointerCoords.y;
1496 break;
1497
1498 case InputDispatchPolicyInterface::ROTATION_270:
1499 temp = pointerCoords.x;
1500 pointerCoords.x = - pointerCoords.y;
1501 pointerCoords.y = temp;
1502 break;
1503 }
1504
1505 mDispatcher->notifyMotion(when, device->id, INPUT_EVENT_NATURE_TRACKBALL, policyFlags,
1506 motionEventAction, globalMetaState(), MOTION_EVENT_EDGE_FLAG_NONE,
1507 1, & pointerId, & pointerCoords,
1508 device->trackball.precalculated.xPrecision,
1509 device->trackball.precalculated.yPrecision,
1510 device->trackball.current.downTime);
1511}
1512
1513void InputReader::onConfigurationChanged(nsecs_t when) {
1514 // Reset global meta state because it depends on the list of all configured devices.
1515 resetGlobalMetaState();
1516
1517 // Reset virtual keys, just in case.
1518 updateGlobalVirtualKeyState();
1519
1520 // Enqueue configuration changed.
1521 // XXX This stuff probably needs to be tracked elsewhere in an input device registry
1522 // of some kind that can be asynchronously updated and queried. (Same as above?)
1523 int32_t touchScreenConfig = InputDispatchPolicyInterface::TOUCHSCREEN_NOTOUCH;
1524 int32_t keyboardConfig = InputDispatchPolicyInterface::KEYBOARD_NOKEYS;
1525 int32_t navigationConfig = InputDispatchPolicyInterface::NAVIGATION_NONAV;
1526
1527 for (size_t i = 0; i < mDevices.size(); i++) {
1528 InputDevice* device = mDevices.valueAt(i);
1529 int32_t deviceClasses = device->classes;
1530
1531 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
1532 touchScreenConfig = InputDispatchPolicyInterface::TOUCHSCREEN_FINGER;
1533 }
1534 if (deviceClasses & INPUT_DEVICE_CLASS_ALPHAKEY) {
1535 keyboardConfig = InputDispatchPolicyInterface::KEYBOARD_QWERTY;
1536 }
1537 if (deviceClasses & INPUT_DEVICE_CLASS_TRACKBALL) {
1538 navigationConfig = InputDispatchPolicyInterface::NAVIGATION_TRACKBALL;
1539 } else if (deviceClasses & INPUT_DEVICE_CLASS_DPAD) {
1540 navigationConfig = InputDispatchPolicyInterface::NAVIGATION_DPAD;
1541 }
1542 }
1543
1544 mDispatcher->notifyConfigurationChanged(when, touchScreenConfig,
1545 keyboardConfig, navigationConfig);
1546}
1547
1548bool InputReader::applyStandardInputDispatchPolicyActions(nsecs_t when,
1549 int32_t policyActions, uint32_t* policyFlags) {
1550 if (policyActions & InputDispatchPolicyInterface::ACTION_APP_SWITCH_COMING) {
1551 mDispatcher->notifyAppSwitchComing(when);
1552 }
1553
1554 if (policyActions & InputDispatchPolicyInterface::ACTION_WOKE_HERE) {
1555 *policyFlags |= POLICY_FLAG_WOKE_HERE;
1556 }
1557
1558 if (policyActions & InputDispatchPolicyInterface::ACTION_BRIGHT_HERE) {
1559 *policyFlags |= POLICY_FLAG_BRIGHT_HERE;
1560 }
1561
1562 return policyActions & InputDispatchPolicyInterface::ACTION_DISPATCH;
1563}
1564
1565void InputReader::resetDisplayProperties() {
1566 mDisplayWidth = mDisplayHeight = -1;
1567 mDisplayOrientation = -1;
1568}
1569
1570bool InputReader::refreshDisplayProperties() {
1571 int32_t newWidth, newHeight, newOrientation;
1572 if (mPolicy->getDisplayInfo(0, & newWidth, & newHeight, & newOrientation)) {
1573 if (newWidth != mDisplayWidth || newHeight != mDisplayHeight) {
1574 LOGD("Display size changed from %dx%d to %dx%d, updating device configuration",
1575 mDisplayWidth, mDisplayHeight, newWidth, newHeight);
1576
1577 mDisplayWidth = newWidth;
1578 mDisplayHeight = newHeight;
1579
1580 for (size_t i = 0; i < mDevices.size(); i++) {
1581 configureDeviceForCurrentDisplaySize(mDevices.valueAt(i));
1582 }
1583 }
1584
1585 mDisplayOrientation = newOrientation;
1586 return true;
1587 } else {
1588 resetDisplayProperties();
1589 return false;
1590 }
1591}
1592
1593InputDevice* InputReader::getDevice(int32_t deviceId) {
1594 ssize_t index = mDevices.indexOfKey(deviceId);
1595 return index >= 0 ? mDevices.valueAt((size_t) index) : NULL;
1596}
1597
1598InputDevice* InputReader::getNonIgnoredDevice(int32_t deviceId) {
1599 InputDevice* device = getDevice(deviceId);
1600 return device && ! device->ignored ? device : NULL;
1601}
1602
1603void InputReader::addDevice(nsecs_t when, int32_t deviceId) {
1604 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
1605 String8 name = mEventHub->getDeviceName(deviceId);
1606 InputDevice* device = new InputDevice(deviceId, classes, name);
1607
1608 if (classes != 0) {
1609 LOGI("Device added: id=0x%x, name=%s, classes=%02x", device->id,
1610 device->name.string(), device->classes);
1611
1612 configureDevice(device);
1613 } else {
1614 LOGI("Device added: id=0x%x, name=%s (ignored non-input device)", device->id,
1615 device->name.string());
1616
1617 device->ignored = true;
1618 }
1619
1620 device->reset();
1621
1622 mDevices.add(deviceId, device);
1623
1624 if (! device->ignored) {
1625 onConfigurationChanged(when);
1626 }
1627}
1628
1629void InputReader::removeDevice(nsecs_t when, InputDevice* device) {
1630 mDevices.removeItem(device->id);
1631
1632 if (! device->ignored) {
1633 LOGI("Device removed: id=0x%x, name=%s, classes=%02x", device->id,
1634 device->name.string(), device->classes);
1635
1636 onConfigurationChanged(when);
1637 } else {
1638 LOGI("Device removed: id=0x%x, name=%s (ignored non-input device)", device->id,
1639 device->name.string());
1640 }
1641
1642 delete device;
1643}
1644
1645void InputReader::configureDevice(InputDevice* device) {
1646 if (device->isMultiTouchScreen()) {
1647 configureAbsoluteAxisInfo(device, ABS_MT_POSITION_X, "X",
1648 & device->touchScreen.parameters.xAxis);
1649 configureAbsoluteAxisInfo(device, ABS_MT_POSITION_Y, "Y",
1650 & device->touchScreen.parameters.yAxis);
1651 configureAbsoluteAxisInfo(device, ABS_MT_TOUCH_MAJOR, "Pressure",
1652 & device->touchScreen.parameters.pressureAxis);
1653 configureAbsoluteAxisInfo(device, ABS_MT_WIDTH_MAJOR, "Size",
1654 & device->touchScreen.parameters.sizeAxis);
1655 } else if (device->isSingleTouchScreen()) {
1656 configureAbsoluteAxisInfo(device, ABS_X, "X",
1657 & device->touchScreen.parameters.xAxis);
1658 configureAbsoluteAxisInfo(device, ABS_Y, "Y",
1659 & device->touchScreen.parameters.yAxis);
1660 configureAbsoluteAxisInfo(device, ABS_PRESSURE, "Pressure",
1661 & device->touchScreen.parameters.pressureAxis);
1662 configureAbsoluteAxisInfo(device, ABS_TOOL_WIDTH, "Size",
1663 & device->touchScreen.parameters.sizeAxis);
1664 }
1665
1666 if (device->isTouchScreen()) {
1667 device->touchScreen.parameters.useBadTouchFilter =
1668 mPolicy->filterTouchEvents();
1669 device->touchScreen.parameters.useAveragingTouchFilter =
1670 mPolicy->filterTouchEvents();
1671 device->touchScreen.parameters.useJumpyTouchFilter =
1672 mPolicy->filterJumpyTouchEvents();
1673
1674 device->touchScreen.precalculated.pressureScale =
1675 1.0f / device->touchScreen.parameters.pressureAxis.range;
1676 device->touchScreen.precalculated.sizeScale =
1677 1.0f / device->touchScreen.parameters.sizeAxis.range;
1678 }
1679
1680 if (device->isTrackball()) {
1681 device->trackball.precalculated.xPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1682 device->trackball.precalculated.yPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1683 device->trackball.precalculated.xScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1684 device->trackball.precalculated.yScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1685 }
1686
1687 configureDeviceForCurrentDisplaySize(device);
1688}
1689
1690void InputReader::configureDeviceForCurrentDisplaySize(InputDevice* device) {
1691 if (device->isTouchScreen()) {
1692 if (mDisplayWidth < 0) {
1693 LOGD("Skipping part of touch screen configuration since display size is unknown.");
1694 } else {
1695 LOGI("Device configured: id=0x%x, name=%s (display size was changed)", device->id,
1696 device->name.string());
1697 configureVirtualKeys(device);
1698
1699 device->touchScreen.precalculated.xScale =
1700 float(mDisplayWidth) / device->touchScreen.parameters.xAxis.range;
1701 device->touchScreen.precalculated.yScale =
1702 float(mDisplayHeight) / device->touchScreen.parameters.yAxis.range;
1703 }
1704 }
1705}
1706
1707void InputReader::configureVirtualKeys(InputDevice* device) {
1708 device->touchScreen.virtualKeys.clear();
1709
1710 Vector<InputDispatchPolicyInterface::VirtualKeyDefinition> virtualKeyDefinitions;
1711 mPolicy->getVirtualKeyDefinitions(device->name, virtualKeyDefinitions);
1712 if (virtualKeyDefinitions.size() == 0) {
1713 return;
1714 }
1715
1716 device->touchScreen.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1717
1718 int32_t touchScreenLeft = device->touchScreen.parameters.xAxis.minValue;
1719 int32_t touchScreenTop = device->touchScreen.parameters.yAxis.minValue;
1720 int32_t touchScreenWidth = device->touchScreen.parameters.xAxis.range;
1721 int32_t touchScreenHeight = device->touchScreen.parameters.yAxis.range;
1722
1723 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
1724 const InputDispatchPolicyInterface::VirtualKeyDefinition& virtualKeyDefinition =
1725 virtualKeyDefinitions[i];
1726
1727 device->touchScreen.virtualKeys.add();
1728 InputDevice::VirtualKey& virtualKey =
1729 device->touchScreen.virtualKeys.editTop();
1730
1731 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1732 int32_t keyCode;
1733 uint32_t flags;
1734 if (mEventHub->scancodeToKeycode(device->id, virtualKey.scanCode,
1735 & keyCode, & flags)) {
1736 LOGI(" VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode);
1737 device->touchScreen.virtualKeys.pop(); // drop the key
1738 continue;
1739 }
1740
1741 virtualKey.keyCode = keyCode;
1742 virtualKey.flags = flags;
1743
1744 // convert the key definition's display coordinates into touch coordinates for a hit box
1745 int32_t halfWidth = virtualKeyDefinition.width / 2;
1746 int32_t halfHeight = virtualKeyDefinition.height / 2;
1747
1748 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1749 * touchScreenWidth / mDisplayWidth + touchScreenLeft;
1750 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1751 * touchScreenWidth / mDisplayWidth + touchScreenLeft;
1752 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1753 * touchScreenHeight / mDisplayHeight + touchScreenTop;
1754 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1755 * touchScreenHeight / mDisplayHeight + touchScreenTop;
1756
1757 LOGI(" VirtualKey %d: keyCode=%d hitLeft=%d hitRight=%d hitTop=%d hitBottom=%d",
1758 virtualKey.scanCode, virtualKey.keyCode,
1759 virtualKey.hitLeft, virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom);
1760 }
1761}
1762
1763void InputReader::configureAbsoluteAxisInfo(InputDevice* device,
1764 int axis, const char* name, InputDevice::AbsoluteAxisInfo* out) {
1765 if (! mEventHub->getAbsoluteInfo(device->id, axis,
1766 & out->minValue, & out->maxValue, & out->flat, &out->fuzz)) {
1767 out->range = out->maxValue - out->minValue;
1768 if (out->range != 0) {
1769 LOGI(" %s: min=%d max=%d flat=%d fuzz=%d",
1770 name, out->minValue, out->maxValue, out->flat, out->fuzz);
1771 return;
1772 }
1773 }
1774
1775 out->minValue = 0;
1776 out->maxValue = 0;
1777 out->flat = 0;
1778 out->fuzz = 0;
1779 out->range = 0;
1780 LOGI(" %s: unknown axis values, setting to zero", name);
1781}
1782
1783void InputReader::resetGlobalMetaState() {
1784 mGlobalMetaState = -1;
1785}
1786
1787int32_t InputReader::globalMetaState() {
1788 if (mGlobalMetaState == -1) {
1789 mGlobalMetaState = 0;
1790 for (size_t i = 0; i < mDevices.size(); i++) {
1791 InputDevice* device = mDevices.valueAt(i);
1792 if (device->isKeyboard()) {
1793 mGlobalMetaState |= device->keyboard.current.metaState;
1794 }
1795 }
1796 }
1797 return mGlobalMetaState;
1798}
1799
1800void InputReader::updateGlobalVirtualKeyState() {
1801 int32_t keyCode = -1, scanCode = -1;
1802
1803 for (size_t i = 0; i < mDevices.size(); i++) {
1804 InputDevice* device = mDevices.valueAt(i);
1805 if (device->isTouchScreen()) {
1806 if (device->touchScreen.currentVirtualKey.down) {
1807 keyCode = device->touchScreen.currentVirtualKey.keyCode;
1808 scanCode = device->touchScreen.currentVirtualKey.scanCode;
1809 }
1810 }
1811 }
1812
1813 {
1814 AutoMutex _l(mExportedStateLock);
1815
1816 mGlobalVirtualKeyCode = keyCode;
1817 mGlobalVirtualScanCode = scanCode;
1818 }
1819}
1820
1821bool InputReader::getCurrentVirtualKey(int32_t* outKeyCode, int32_t* outScanCode) const {
1822 AutoMutex _l(mExportedStateLock);
1823
1824 *outKeyCode = mGlobalVirtualKeyCode;
1825 *outScanCode = mGlobalVirtualScanCode;
1826 return mGlobalVirtualKeyCode != -1;
1827}
1828
1829
1830// --- InputReaderThread ---
1831
1832InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
1833 Thread(/*canCallJava*/ true), mReader(reader) {
1834}
1835
1836InputReaderThread::~InputReaderThread() {
1837}
1838
1839bool InputReaderThread::threadLoop() {
1840 mReader->loopOnce();
1841 return true;
1842}
1843
1844} // namespace android