blob: 7c19f243adada8126ed947fe86903e78771a3f8f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.util.Log;
20import android.view.Display;
21import android.view.MotionEvent;
22import android.view.Surface;
23import android.view.WindowManagerPolicy;
24
25public class InputDevice {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070026 static final boolean DEBUG_POINTERS = false;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070027 static final boolean DEBUG_HACKS = false;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 /** Amount that trackball needs to move in order to generate a key event. */
30 static final int TRACKBALL_MOVEMENT_THRESHOLD = 6;
31
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070032 /** Maximum number of pointers we will track and report. */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070033 static final int MAX_POINTERS = 10;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 final int id;
36 final int classes;
37 final String name;
38 final AbsoluteInfo absX;
39 final AbsoluteInfo absY;
40 final AbsoluteInfo absPressure;
41 final AbsoluteInfo absSize;
42
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070043 long mKeyDownTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 int mMetaKeysState = 0;
45
Dianne Hackborn2a2b34432009-08-12 17:13:55 -070046 // For use by KeyInputQueue for keeping track of the current touch
47 // data in the old non-multi-touch protocol.
48 final int[] curTouchVals = new int[MotionEvent.NUM_SAMPLE_DATA * 2];
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 final MotionState mAbs = new MotionState(0, 0);
51 final MotionState mRel = new MotionState(TRACKBALL_MOVEMENT_THRESHOLD,
52 TRACKBALL_MOVEMENT_THRESHOLD);
53
54 static class MotionState {
55 int xPrecision;
56 int yPrecision;
57 float xMoveScale;
58 float yMoveScale;
59 MotionEvent currentMove = null;
60 boolean changed = false;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070061 long mDownTime = 0;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070062
63 // The currently assigned pointer IDs, corresponding to the last data.
64 int[] mPointerIds = new int[MAX_POINTERS];
65
66 // This is the last generated pointer data, ordered to match
67 // mPointerIds.
Dianne Hackborndc953722009-10-19 11:24:39 -070068 boolean mSkipLastPointers;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070069 int mLastNumPointers = 0;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070070 final int[] mLastData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070071
72 // This is the next set of pointer data being generated. It is not
73 // in any known order, and will be propagated in to mLastData
74 // as part of mapping it to the appropriate pointer IDs.
75 // Note that we have one extra sample of data here, to help clients
76 // avoid doing bounds checking.
77 int mNextNumPointers = 0;
78 final int[] mNextData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS)
79 + MotionEvent.NUM_SAMPLE_DATA];
80
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070081 // Used to determine whether we dropped bad data, to avoid doing
82 // it repeatedly.
83 final boolean[] mDroppedBadPoint = new boolean[MAX_POINTERS];
84
85 // Used to perform averaging of reported coordinates, to smooth
86 // the data and filter out transients during a release.
87 static final int HISTORY_SIZE = 5;
88 int[] mHistoryDataStart = new int[MAX_POINTERS];
89 int[] mHistoryDataEnd = new int[MAX_POINTERS];
90 final int[] mHistoryData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS)
91 * HISTORY_SIZE];
92 final int[] mAveragedData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
93
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070094 // Temporary data structures for doing the pointer ID mapping.
95 final int[] mLast2Next = new int[MAX_POINTERS];
96 final int[] mNext2Last = new int[MAX_POINTERS];
97 final long[] mNext2LastDistance = new long[MAX_POINTERS];
98
99 // Temporary data structure for generating the final motion data.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700100 final float[] mReportData = new float[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700102 // This is not used here, but can be used by callers for state tracking.
103 int mAddingPointerOffset = 0;
104 final boolean[] mDown = new boolean[MAX_POINTERS];
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 MotionState(int mx, int my) {
107 xPrecision = mx;
108 yPrecision = my;
109 xMoveScale = mx != 0 ? (1.0f/mx) : 1.0f;
110 yMoveScale = my != 0 ? (1.0f/my) : 1.0f;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700111 for (int i=0; i<MAX_POINTERS; i++) {
112 mPointerIds[i] = i;
113 }
114 }
115
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700116 /**
117 * Special hack for devices that have bad screen data: if one of the
118 * points has moved more than a screen height from the last position,
119 * then drop it.
120 */
121 void dropBadPoint(InputDevice dev) {
122 // We should always have absY, but let's be paranoid.
123 if (dev.absY == null) {
124 return;
125 }
126 // Don't do anything if a finger is going down or up. We run
127 // here before assigning pointer IDs, so there isn't a good
128 // way to do per-finger matching.
129 if (mNextNumPointers != mLastNumPointers) {
130 return;
131 }
132
133 // We consider a single movement across more than a 7/16 of
134 // the long size of the screen to be bad. This was a magic value
135 // determined by looking at the maximum distance it is feasible
136 // to actually move in one sample.
137 final int maxDy = ((dev.absY.maxValue-dev.absY.minValue)*7)/16;
138
139 // Look through all new points and see if any are farther than
140 // acceptable from all previous points.
141 for (int i=mNextNumPointers-1; i>=0; i--) {
142 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
143 //final int x = mNextData[ioff + MotionEvent.SAMPLE_X];
144 final int y = mNextData[ioff + MotionEvent.SAMPLE_Y];
145 if (DEBUG_HACKS) Log.v("InputDevice", "Looking at next point #" + i + ": y=" + y);
146 boolean dropped = false;
147 if (!mDroppedBadPoint[i] && mLastNumPointers > 0) {
148 dropped = true;
149 int closestDy = -1;
150 int closestY = -1;
151 // We will drop this new point if it is sufficiently
152 // far away from -all- last points.
153 for (int j=mLastNumPointers-1; j>=0; j--) {
154 final int joff = j * MotionEvent.NUM_SAMPLE_DATA;
155 //int dx = x - mLastData[joff + MotionEvent.SAMPLE_X];
156 int dy = y - mLastData[joff + MotionEvent.SAMPLE_Y];
157 //if (dx < 0) dx = -dx;
158 if (dy < 0) dy = -dy;
159 if (DEBUG_HACKS) Log.v("InputDevice", "Comparing with last point #" + j
160 + ": y=" + mLastData[joff] + " dy=" + dy);
161 if (dy < maxDy) {
162 dropped = false;
163 break;
164 } else if (closestDy < 0 || dy < closestDy) {
165 closestDy = dy;
166 closestY = mLastData[joff + MotionEvent.SAMPLE_Y];
167 }
168 }
169 if (dropped) {
170 dropped = true;
171 Log.i("InputDevice", "Dropping bad point #" + i
172 + ": newY=" + y + " closestDy=" + closestDy
173 + " maxDy=" + maxDy);
174 mNextData[ioff + MotionEvent.SAMPLE_Y] = closestY;
175 break;
176 }
177 }
178 mDroppedBadPoint[i] = dropped;
179 }
180 }
181
182 /**
183 * Special hack for devices that have bad screen data: aggregate and
184 * compute averages of the coordinate data, to reduce the amount of
185 * jitter seen by applications.
186 */
187 int[] generateAveragedData(int upOrDownPointer, int lastNumPointers,
188 int nextNumPointers) {
189 final int numPointers = mLastNumPointers;
190 final int[] rawData = mLastData;
191 if (DEBUG_HACKS) Log.v("InputDevice", "lastNumPointers=" + lastNumPointers
192 + " nextNumPointers=" + nextNumPointers
193 + " numPointers=" + numPointers);
194 for (int i=0; i<numPointers; i++) {
195 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
196 // We keep the average data in offsets based on the pointer
197 // ID, so we don't need to move it around as fingers are
198 // pressed and released.
199 final int p = mPointerIds[i];
200 final int poff = p * MotionEvent.NUM_SAMPLE_DATA * HISTORY_SIZE;
201 if (i == upOrDownPointer && lastNumPointers != nextNumPointers) {
202 if (lastNumPointers < nextNumPointers) {
203 // This pointer is going down. Clear its history
204 // and start fresh.
205 if (DEBUG_HACKS) Log.v("InputDevice", "Pointer down @ index "
206 + upOrDownPointer + " id " + mPointerIds[i]);
207 mHistoryDataStart[i] = 0;
208 mHistoryDataEnd[i] = 0;
209 System.arraycopy(rawData, ioff, mHistoryData, poff,
210 MotionEvent.NUM_SAMPLE_DATA);
211 System.arraycopy(rawData, ioff, mAveragedData, ioff,
212 MotionEvent.NUM_SAMPLE_DATA);
213 continue;
214 } else {
215 // The pointer is going up. Just fall through to
216 // recompute the last averaged point (and don't add
217 // it as a new point to include in the average).
218 if (DEBUG_HACKS) Log.v("InputDevice", "Pointer up @ index "
219 + upOrDownPointer + " id " + mPointerIds[i]);
220 }
221 } else {
222 int end = mHistoryDataEnd[i];
223 int eoff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
224 int oldX = mHistoryData[eoff + MotionEvent.SAMPLE_X];
225 int oldY = mHistoryData[eoff + MotionEvent.SAMPLE_Y];
226 int newX = rawData[ioff + MotionEvent.SAMPLE_X];
227 int newY = rawData[ioff + MotionEvent.SAMPLE_Y];
228 int dx = newX-oldX;
229 int dy = newY-oldY;
230 int delta = dx*dx + dy*dy;
231 if (DEBUG_HACKS) Log.v("InputDevice", "Delta from last: " + delta);
232 if (delta >= (75*75)) {
233 // Magic number, if moving farther than this, turn
234 // off filtering to avoid lag in response.
235 mHistoryDataStart[i] = 0;
236 mHistoryDataEnd[i] = 0;
237 System.arraycopy(rawData, ioff, mHistoryData, poff,
238 MotionEvent.NUM_SAMPLE_DATA);
239 System.arraycopy(rawData, ioff, mAveragedData, ioff,
240 MotionEvent.NUM_SAMPLE_DATA);
241 continue;
242 } else {
243 end++;
244 if (end >= HISTORY_SIZE) {
245 end -= HISTORY_SIZE;
246 }
247 mHistoryDataEnd[i] = end;
248 int noff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
249 mHistoryData[noff + MotionEvent.SAMPLE_X] = newX;
250 mHistoryData[noff + MotionEvent.SAMPLE_Y] = newY;
251 mHistoryData[noff + MotionEvent.SAMPLE_PRESSURE]
252 = rawData[ioff + MotionEvent.SAMPLE_PRESSURE];
253 int start = mHistoryDataStart[i];
254 if (end == start) {
255 start++;
256 if (start >= HISTORY_SIZE) {
257 start -= HISTORY_SIZE;
258 }
259 mHistoryDataStart[i] = start;
260 }
261 }
262 }
263
264 // Now compute the average.
265 int start = mHistoryDataStart[i];
266 int end = mHistoryDataEnd[i];
267 int x=0, y=0;
268 int totalPressure = 0;
269 while (start != end) {
270 int soff = poff + (start*MotionEvent.NUM_SAMPLE_DATA);
271 int pressure = mHistoryData[soff + MotionEvent.SAMPLE_PRESSURE];
Dianne Hackborn53cd5792009-10-13 19:50:51 -0700272 if (pressure <= 0) pressure = 1;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700273 x += mHistoryData[soff + MotionEvent.SAMPLE_X] * pressure;
274 y += mHistoryData[soff + MotionEvent.SAMPLE_Y] * pressure;
275 totalPressure += pressure;
276 start++;
277 if (start >= HISTORY_SIZE) start = 0;
278 }
279 int eoff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
280 int pressure = mHistoryData[eoff + MotionEvent.SAMPLE_PRESSURE];
Dianne Hackborn53cd5792009-10-13 19:50:51 -0700281 if (pressure <= 0) pressure = 1;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700282 x += mHistoryData[eoff + MotionEvent.SAMPLE_X] * pressure;
283 y += mHistoryData[eoff + MotionEvent.SAMPLE_Y] * pressure;
284 totalPressure += pressure;
285 x /= totalPressure;
286 y /= totalPressure;
287 if (DEBUG_HACKS) Log.v("InputDevice", "Averaging " + totalPressure
288 + " weight: (" + x + "," + y + ")");
289 mAveragedData[ioff + MotionEvent.SAMPLE_X] = x;
290 mAveragedData[ioff + MotionEvent.SAMPLE_Y] = y;
Dianne Hackborn05799982009-11-23 13:08:14 -0800291 mAveragedData[ioff + MotionEvent.SAMPLE_PRESSURE] =
292 rawData[ioff + MotionEvent.SAMPLE_PRESSURE];
293 mAveragedData[ioff + MotionEvent.SAMPLE_SIZE] =
294 rawData[ioff + MotionEvent.SAMPLE_SIZE];
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700295 }
296 return mAveragedData;
297 }
298
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700299 private boolean assignPointer(int nextIndex, boolean allowOverlap) {
300 final int lastNumPointers = mLastNumPointers;
301 final int[] next2Last = mNext2Last;
302 final long[] next2LastDistance = mNext2LastDistance;
303 final int[] last2Next = mLast2Next;
304 final int[] lastData = mLastData;
305 final int[] nextData = mNextData;
306 final int id = nextIndex * MotionEvent.NUM_SAMPLE_DATA;
307
308 if (DEBUG_POINTERS) Log.v("InputDevice", "assignPointer: nextIndex="
309 + nextIndex + " dataOff=" + id);
310 final int x1 = nextData[id + MotionEvent.SAMPLE_X];
311 final int y1 = nextData[id + MotionEvent.SAMPLE_Y];
312
313 long bestDistance = -1;
314 int bestIndex = -1;
315 for (int j=0; j<lastNumPointers; j++) {
316 if (!allowOverlap && last2Next[j] < 0) {
317 continue;
318 }
319 final int jd = j * MotionEvent.NUM_SAMPLE_DATA;
320 final int xd = lastData[jd + MotionEvent.SAMPLE_X] - x1;
321 final int yd = lastData[jd + MotionEvent.SAMPLE_Y] - y1;
322 final long distance = xd*(long)xd + yd*(long)yd;
323 if (j == 0 || distance < bestDistance) {
324 bestDistance = distance;
325 bestIndex = j;
326 }
327 }
328
329 if (DEBUG_POINTERS) Log.v("InputDevice", "New index " + nextIndex
330 + " best old index=" + bestIndex + " (distance="
331 + bestDistance + ")");
332 next2Last[nextIndex] = bestIndex;
333 next2LastDistance[nextIndex] = bestDistance;
334
335 if (bestIndex < 0) {
336 return true;
337 }
338
339 if (last2Next[bestIndex] == -1) {
340 last2Next[bestIndex] = nextIndex;
341 return false;
342 }
343
344 if (DEBUG_POINTERS) Log.v("InputDevice", "Old index " + bestIndex
345 + " has multiple best new pointers!");
346
347 last2Next[bestIndex] = -2;
348 return true;
349 }
350
351 private int updatePointerIdentifiers() {
352 final int[] lastData = mLastData;
353 final int[] nextData = mNextData;
354 final int nextNumPointers = mNextNumPointers;
355 final int lastNumPointers = mLastNumPointers;
356
357 if (nextNumPointers == 1 && lastNumPointers == 1) {
358 System.arraycopy(nextData, 0, lastData, 0,
359 MotionEvent.NUM_SAMPLE_DATA);
360 return -1;
361 }
362
363 // Clear our old state.
364 final int[] last2Next = mLast2Next;
365 for (int i=0; i<lastNumPointers; i++) {
366 last2Next[i] = -1;
367 }
368
369 if (DEBUG_POINTERS) Log.v("InputDevice",
370 "Update pointers: lastNumPointers=" + lastNumPointers
371 + " nextNumPointers=" + nextNumPointers);
372
373 // Figure out the closes new points to the previous points.
374 final int[] next2Last = mNext2Last;
375 final long[] next2LastDistance = mNext2LastDistance;
376 boolean conflicts = false;
377 for (int i=0; i<nextNumPointers; i++) {
378 conflicts |= assignPointer(i, true);
379 }
380
381 // Resolve ambiguities in pointer mappings, when two or more
382 // new pointer locations find their best previous location is
383 // the same.
384 if (conflicts) {
385 if (DEBUG_POINTERS) Log.v("InputDevice", "Resolving conflicts");
386
387 for (int i=0; i<lastNumPointers; i++) {
388 if (last2Next[i] != -2) {
389 continue;
390 }
391
392 // Note that this algorithm is far from perfect. Ideally
393 // we should do something like the one described at
394 // http://portal.acm.org/citation.cfm?id=997856
395
396 if (DEBUG_POINTERS) Log.v("InputDevice",
397 "Resolving last index #" + i);
398
399 int numFound;
400 do {
401 numFound = 0;
402 long worstDistance = 0;
403 int worstJ = -1;
404 for (int j=0; j<nextNumPointers; j++) {
405 if (next2Last[j] != i) {
406 continue;
407 }
408 numFound++;
409 if (worstDistance < next2LastDistance[j]) {
410 worstDistance = next2LastDistance[j];
411 worstJ = j;
412 }
413 }
414
415 if (worstJ >= 0) {
416 if (DEBUG_POINTERS) Log.v("InputDevice",
417 "Worst new pointer: " + worstJ
418 + " (distance=" + worstDistance + ")");
419 if (assignPointer(worstJ, false)) {
420 // In this case there is no last pointer
421 // remaining for this new one!
422 next2Last[worstJ] = -1;
423 }
424 }
425 } while (numFound > 2);
426 }
427 }
428
429 int retIndex = -1;
430
431 if (lastNumPointers < nextNumPointers) {
432 // We have one or more new pointers that are down. Create a
433 // new pointer identifier for one of them.
434 if (DEBUG_POINTERS) Log.v("InputDevice", "Adding new pointer");
435 int nextId = 0;
436 int i=0;
437 while (i < lastNumPointers) {
438 if (mPointerIds[i] > nextId) {
439 // Found a hole, insert the pointer here.
440 if (DEBUG_POINTERS) Log.v("InputDevice",
441 "Inserting new pointer at hole " + i);
442 System.arraycopy(mPointerIds, i, mPointerIds,
443 i+1, lastNumPointers-i);
444 System.arraycopy(lastData, i*MotionEvent.NUM_SAMPLE_DATA,
445 lastData, (i+1)*MotionEvent.NUM_SAMPLE_DATA,
446 (lastNumPointers-i)*MotionEvent.NUM_SAMPLE_DATA);
447 break;
448 }
449 i++;
450 nextId++;
451 }
452
453 if (DEBUG_POINTERS) Log.v("InputDevice",
454 "New pointer id " + nextId + " at index " + i);
455
456 mLastNumPointers++;
457 retIndex = i;
458 mPointerIds[i] = nextId;
459
460 // And assign this identifier to the first new pointer.
461 for (int j=0; j<nextNumPointers; j++) {
462 if (next2Last[j] < 0) {
463 if (DEBUG_POINTERS) Log.v("InputDevice",
464 "Assigning new id to new pointer index " + j);
465 next2Last[j] = i;
466 break;
467 }
468 }
469 }
470
471 // Propagate all of the current data into the appropriate
472 // location in the old data to match the pointer ID that was
473 // assigned to it.
474 for (int i=0; i<nextNumPointers; i++) {
475 int lastIndex = next2Last[i];
476 if (lastIndex >= 0) {
477 if (DEBUG_POINTERS) Log.v("InputDevice",
478 "Copying next pointer index " + i
479 + " to last index " + lastIndex);
480 System.arraycopy(nextData, i*MotionEvent.NUM_SAMPLE_DATA,
481 lastData, lastIndex*MotionEvent.NUM_SAMPLE_DATA,
482 MotionEvent.NUM_SAMPLE_DATA);
483 }
484 }
485
486 if (lastNumPointers > nextNumPointers) {
487 // One or more pointers has gone up. Find the first one,
488 // and adjust accordingly.
489 if (DEBUG_POINTERS) Log.v("InputDevice", "Removing old pointer");
490 for (int i=0; i<lastNumPointers; i++) {
491 if (last2Next[i] == -1) {
492 if (DEBUG_POINTERS) Log.v("InputDevice",
493 "Removing old pointer at index " + i);
494 retIndex = i;
495 break;
496 }
497 }
498 }
499
500 return retIndex;
501 }
502
503 void removeOldPointer(int index) {
504 final int lastNumPointers = mLastNumPointers;
505 if (index >= 0 && index < lastNumPointers) {
506 System.arraycopy(mPointerIds, index+1, mPointerIds,
507 index, lastNumPointers-index-1);
508 System.arraycopy(mLastData, (index+1)*MotionEvent.NUM_SAMPLE_DATA,
509 mLastData, (index)*MotionEvent.NUM_SAMPLE_DATA,
510 (lastNumPointers-index-1)*MotionEvent.NUM_SAMPLE_DATA);
511 mLastNumPointers--;
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700515 MotionEvent generateAbsMotion(InputDevice device, long curTime,
516 long curTimeNano, Display display, int orientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 int metaState) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700518
Dianne Hackborndc953722009-10-19 11:24:39 -0700519 if (mSkipLastPointers) {
520 mSkipLastPointers = false;
521 mLastNumPointers = 0;
522 }
523
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700524 if (mNextNumPointers <= 0 && mLastNumPointers <= 0) {
525 return null;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700526 }
527
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700528 final int lastNumPointers = mLastNumPointers;
529 final int nextNumPointers = mNextNumPointers;
530 if (mNextNumPointers > MAX_POINTERS) {
531 Log.w("InputDevice", "Number of pointers " + mNextNumPointers
532 + " exceeded maximum of " + MAX_POINTERS);
533 mNextNumPointers = MAX_POINTERS;
534 }
535
536 int upOrDownPointer = updatePointerIdentifiers();
537
538 final float[] reportData = mReportData;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700539 final int[] rawData;
540 if (KeyInputQueue.BAD_TOUCH_HACK) {
541 rawData = generateAveragedData(upOrDownPointer, lastNumPointers,
542 nextNumPointers);
543 } else {
544 rawData = mLastData;
545 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700546
547 final int numPointers = mLastNumPointers;
548
549 if (DEBUG_POINTERS) Log.v("InputDevice", "Processing "
550 + numPointers + " pointers (going from " + lastNumPointers
551 + " to " + nextNumPointers + ")");
552
553 for (int i=0; i<numPointers; i++) {
554 final int pos = i * MotionEvent.NUM_SAMPLE_DATA;
555 reportData[pos + MotionEvent.SAMPLE_X] = rawData[pos + MotionEvent.SAMPLE_X];
556 reportData[pos + MotionEvent.SAMPLE_Y] = rawData[pos + MotionEvent.SAMPLE_Y];
557 reportData[pos + MotionEvent.SAMPLE_PRESSURE] = rawData[pos + MotionEvent.SAMPLE_PRESSURE];
558 reportData[pos + MotionEvent.SAMPLE_SIZE] = rawData[pos + MotionEvent.SAMPLE_SIZE];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700559 }
560
561 int action;
562 int edgeFlags = 0;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700563 if (nextNumPointers != lastNumPointers) {
564 if (nextNumPointers > lastNumPointers) {
565 if (lastNumPointers == 0) {
566 action = MotionEvent.ACTION_DOWN;
567 mDownTime = curTime;
568 } else {
569 action = MotionEvent.ACTION_POINTER_DOWN
570 | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700571 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700572 } else {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700573 if (numPointers == 1) {
574 action = MotionEvent.ACTION_UP;
575 } else {
576 action = MotionEvent.ACTION_POINTER_UP
577 | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT);
578 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700579 }
580 currentMove = null;
581 } else {
582 action = MotionEvent.ACTION_MOVE;
583 }
584
585 final int dispW = display.getWidth()-1;
586 final int dispH = display.getHeight()-1;
587 int w = dispW;
588 int h = dispH;
589 if (orientation == Surface.ROTATION_90
590 || orientation == Surface.ROTATION_270) {
591 int tmp = w;
592 w = h;
593 h = tmp;
594 }
595
596 final AbsoluteInfo absX = device.absX;
597 final AbsoluteInfo absY = device.absY;
598 final AbsoluteInfo absPressure = device.absPressure;
599 final AbsoluteInfo absSize = device.absSize;
600 for (int i=0; i<numPointers; i++) {
601 final int j = i * MotionEvent.NUM_SAMPLE_DATA;
602
603 if (absX != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700604 reportData[j + MotionEvent.SAMPLE_X] =
605 ((reportData[j + MotionEvent.SAMPLE_X]-absX.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700606 / absX.range) * w;
607 }
608 if (absY != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700609 reportData[j + MotionEvent.SAMPLE_Y] =
610 ((reportData[j + MotionEvent.SAMPLE_Y]-absY.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700611 / absY.range) * h;
612 }
613 if (absPressure != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700614 reportData[j + MotionEvent.SAMPLE_PRESSURE] =
615 ((reportData[j + MotionEvent.SAMPLE_PRESSURE]-absPressure.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700616 / (float)absPressure.range);
617 }
618 if (absSize != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700619 reportData[j + MotionEvent.SAMPLE_SIZE] =
620 ((reportData[j + MotionEvent.SAMPLE_SIZE]-absSize.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700621 / (float)absSize.range);
622 }
623
624 switch (orientation) {
625 case Surface.ROTATION_90: {
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700626 final float temp = reportData[j + MotionEvent.SAMPLE_X];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700627 reportData[j + MotionEvent.SAMPLE_X] = reportData[j + MotionEvent.SAMPLE_Y];
628 reportData[j + MotionEvent.SAMPLE_Y] = w-temp;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700629 break;
630 }
631 case Surface.ROTATION_180: {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700632 reportData[j + MotionEvent.SAMPLE_X] = w-reportData[j + MotionEvent.SAMPLE_X];
633 reportData[j + MotionEvent.SAMPLE_Y] = h-reportData[j + MotionEvent.SAMPLE_Y];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700634 break;
635 }
636 case Surface.ROTATION_270: {
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700637 final float temp = reportData[j + MotionEvent.SAMPLE_X];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700638 reportData[j + MotionEvent.SAMPLE_X] = h-reportData[j + MotionEvent.SAMPLE_Y];
639 reportData[j + MotionEvent.SAMPLE_Y] = temp;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700640 break;
641 }
642 }
643 }
644
645 // We only consider the first pointer when computing the edge
646 // flags, since they are global to the event.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700647 if (action == MotionEvent.ACTION_DOWN) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700648 if (reportData[MotionEvent.SAMPLE_X] <= 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700649 edgeFlags |= MotionEvent.EDGE_LEFT;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700650 } else if (reportData[MotionEvent.SAMPLE_X] >= dispW) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700651 edgeFlags |= MotionEvent.EDGE_RIGHT;
652 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700653 if (reportData[MotionEvent.SAMPLE_Y] <= 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700654 edgeFlags |= MotionEvent.EDGE_TOP;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700655 } else if (reportData[MotionEvent.SAMPLE_Y] >= dispH) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700656 edgeFlags |= MotionEvent.EDGE_BOTTOM;
657 }
658 }
659
660 if (currentMove != null) {
661 if (false) Log.i("InputDevice", "Adding batch x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700662 + reportData[MotionEvent.SAMPLE_X]
663 + " y=" + reportData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700664 + " to " + currentMove);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700665 currentMove.addBatch(curTime, reportData, metaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700666 if (WindowManagerPolicy.WATCH_POINTER) {
667 Log.i("KeyInputQueue", "Updating: " + currentMove);
668 }
669 return null;
670 }
671
672 MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700673 curTimeNano, action, numPointers, mPointerIds, reportData,
674 metaState, xPrecision, yPrecision, device.id, edgeFlags);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700675 if (action == MotionEvent.ACTION_MOVE) {
676 currentMove = me;
677 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700678
679 if (nextNumPointers < lastNumPointers) {
680 removeOldPointer(upOrDownPointer);
681 }
682
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700683 return me;
684 }
685
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700686 boolean hasMore() {
687 return mLastNumPointers != mNextNumPointers;
688 }
689
690 void finish() {
691 mNextNumPointers = mAddingPointerOffset = 0;
692 mNextData[MotionEvent.SAMPLE_PRESSURE] = 0;
693 }
694
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700695 MotionEvent generateRelMotion(InputDevice device, long curTime,
696 long curTimeNano, int orientation, int metaState) {
697
698 final float[] scaled = mReportData;
699
700 // For now we only support 1 pointer with relative motions.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700701 scaled[MotionEvent.SAMPLE_X] = mNextData[MotionEvent.SAMPLE_X];
702 scaled[MotionEvent.SAMPLE_Y] = mNextData[MotionEvent.SAMPLE_Y];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700703 scaled[MotionEvent.SAMPLE_PRESSURE] = 1.0f;
704 scaled[MotionEvent.SAMPLE_SIZE] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 int edgeFlags = 0;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700706
707 int action;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700708 if (mNextNumPointers != mLastNumPointers) {
709 mNextData[MotionEvent.SAMPLE_X] =
710 mNextData[MotionEvent.SAMPLE_Y] = 0;
711 if (mNextNumPointers > 0 && mLastNumPointers == 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700712 action = MotionEvent.ACTION_DOWN;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700713 mDownTime = curTime;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700714 } else if (mNextNumPointers == 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700715 action = MotionEvent.ACTION_UP;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700716 } else {
717 action = MotionEvent.ACTION_MOVE;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700718 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700719 mLastNumPointers = mNextNumPointers;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700720 currentMove = null;
721 } else {
722 action = MotionEvent.ACTION_MOVE;
723 }
724
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700725 scaled[MotionEvent.SAMPLE_X] *= xMoveScale;
726 scaled[MotionEvent.SAMPLE_Y] *= yMoveScale;
727 switch (orientation) {
728 case Surface.ROTATION_90: {
729 final float temp = scaled[MotionEvent.SAMPLE_X];
730 scaled[MotionEvent.SAMPLE_X] = scaled[MotionEvent.SAMPLE_Y];
731 scaled[MotionEvent.SAMPLE_Y] = -temp;
732 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700734 case Surface.ROTATION_180: {
735 scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_X];
736 scaled[MotionEvent.SAMPLE_Y] = -scaled[MotionEvent.SAMPLE_Y];
737 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700739 case Surface.ROTATION_270: {
740 final float temp = scaled[MotionEvent.SAMPLE_X];
741 scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_Y];
742 scaled[MotionEvent.SAMPLE_Y] = temp;
743 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 }
746
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700747 if (currentMove != null) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700748 if (false) Log.i("InputDevice", "Adding batch x="
749 + scaled[MotionEvent.SAMPLE_X]
750 + " y=" + scaled[MotionEvent.SAMPLE_Y]
751 + " to " + currentMove);
752 currentMove.addBatch(curTime, scaled, metaState);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700753 if (WindowManagerPolicy.WATCH_POINTER) {
754 Log.i("KeyInputQueue", "Updating: " + currentMove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700756 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700758
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700759 MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700760 curTimeNano, action, 1, mPointerIds, scaled, metaState,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700761 xPrecision, yPrecision, device.id, edgeFlags);
762 if (action == MotionEvent.ACTION_MOVE) {
763 currentMove = me;
764 }
765 return me;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767 }
768
769 static class AbsoluteInfo {
770 int minValue;
771 int maxValue;
772 int range;
773 int flat;
774 int fuzz;
775 };
776
777 InputDevice(int _id, int _classes, String _name,
778 AbsoluteInfo _absX, AbsoluteInfo _absY,
779 AbsoluteInfo _absPressure, AbsoluteInfo _absSize) {
780 id = _id;
781 classes = _classes;
782 name = _name;
783 absX = _absX;
784 absY = _absY;
785 absPressure = _absPressure;
786 absSize = _absSize;
787 }
788};