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