blob: 2dc45b53df5994d3b2ce914d0c452ae489b47c1f [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.
68 int mLastNumPointers = 0;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070069 final int[] mLastData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070070
71 // This is the next set of pointer data being generated. It is not
72 // in any known order, and will be propagated in to mLastData
73 // as part of mapping it to the appropriate pointer IDs.
74 // Note that we have one extra sample of data here, to help clients
75 // avoid doing bounds checking.
76 int mNextNumPointers = 0;
77 final int[] mNextData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS)
78 + MotionEvent.NUM_SAMPLE_DATA];
79
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070080 // Used to determine whether we dropped bad data, to avoid doing
81 // it repeatedly.
82 final boolean[] mDroppedBadPoint = new boolean[MAX_POINTERS];
83
84 // Used to perform averaging of reported coordinates, to smooth
85 // the data and filter out transients during a release.
86 static final int HISTORY_SIZE = 5;
87 int[] mHistoryDataStart = new int[MAX_POINTERS];
88 int[] mHistoryDataEnd = new int[MAX_POINTERS];
89 final int[] mHistoryData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS)
90 * HISTORY_SIZE];
91 final int[] mAveragedData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
92
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070093 // Temporary data structures for doing the pointer ID mapping.
94 final int[] mLast2Next = new int[MAX_POINTERS];
95 final int[] mNext2Last = new int[MAX_POINTERS];
96 final long[] mNext2LastDistance = new long[MAX_POINTERS];
97
98 // Temporary data structure for generating the final motion data.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070099 final float[] mReportData = new float[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700101 // This is not used here, but can be used by callers for state tracking.
102 int mAddingPointerOffset = 0;
103 final boolean[] mDown = new boolean[MAX_POINTERS];
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 MotionState(int mx, int my) {
106 xPrecision = mx;
107 yPrecision = my;
108 xMoveScale = mx != 0 ? (1.0f/mx) : 1.0f;
109 yMoveScale = my != 0 ? (1.0f/my) : 1.0f;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700110 for (int i=0; i<MAX_POINTERS; i++) {
111 mPointerIds[i] = i;
112 }
113 }
114
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700115 /**
116 * Special hack for devices that have bad screen data: if one of the
117 * points has moved more than a screen height from the last position,
118 * then drop it.
119 */
120 void dropBadPoint(InputDevice dev) {
121 // We should always have absY, but let's be paranoid.
122 if (dev.absY == null) {
123 return;
124 }
125 // Don't do anything if a finger is going down or up. We run
126 // here before assigning pointer IDs, so there isn't a good
127 // way to do per-finger matching.
128 if (mNextNumPointers != mLastNumPointers) {
129 return;
130 }
131
132 // We consider a single movement across more than a 7/16 of
133 // the long size of the screen to be bad. This was a magic value
134 // determined by looking at the maximum distance it is feasible
135 // to actually move in one sample.
136 final int maxDy = ((dev.absY.maxValue-dev.absY.minValue)*7)/16;
137
138 // Look through all new points and see if any are farther than
139 // acceptable from all previous points.
140 for (int i=mNextNumPointers-1; i>=0; i--) {
141 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
142 //final int x = mNextData[ioff + MotionEvent.SAMPLE_X];
143 final int y = mNextData[ioff + MotionEvent.SAMPLE_Y];
144 if (DEBUG_HACKS) Log.v("InputDevice", "Looking at next point #" + i + ": y=" + y);
145 boolean dropped = false;
146 if (!mDroppedBadPoint[i] && mLastNumPointers > 0) {
147 dropped = true;
148 int closestDy = -1;
149 int closestY = -1;
150 // We will drop this new point if it is sufficiently
151 // far away from -all- last points.
152 for (int j=mLastNumPointers-1; j>=0; j--) {
153 final int joff = j * MotionEvent.NUM_SAMPLE_DATA;
154 //int dx = x - mLastData[joff + MotionEvent.SAMPLE_X];
155 int dy = y - mLastData[joff + MotionEvent.SAMPLE_Y];
156 //if (dx < 0) dx = -dx;
157 if (dy < 0) dy = -dy;
158 if (DEBUG_HACKS) Log.v("InputDevice", "Comparing with last point #" + j
159 + ": y=" + mLastData[joff] + " dy=" + dy);
160 if (dy < maxDy) {
161 dropped = false;
162 break;
163 } else if (closestDy < 0 || dy < closestDy) {
164 closestDy = dy;
165 closestY = mLastData[joff + MotionEvent.SAMPLE_Y];
166 }
167 }
168 if (dropped) {
169 dropped = true;
170 Log.i("InputDevice", "Dropping bad point #" + i
171 + ": newY=" + y + " closestDy=" + closestDy
172 + " maxDy=" + maxDy);
173 mNextData[ioff + MotionEvent.SAMPLE_Y] = closestY;
174 break;
175 }
176 }
177 mDroppedBadPoint[i] = dropped;
178 }
179 }
180
181 /**
182 * Special hack for devices that have bad screen data: aggregate and
183 * compute averages of the coordinate data, to reduce the amount of
184 * jitter seen by applications.
185 */
186 int[] generateAveragedData(int upOrDownPointer, int lastNumPointers,
187 int nextNumPointers) {
188 final int numPointers = mLastNumPointers;
189 final int[] rawData = mLastData;
190 if (DEBUG_HACKS) Log.v("InputDevice", "lastNumPointers=" + lastNumPointers
191 + " nextNumPointers=" + nextNumPointers
192 + " numPointers=" + numPointers);
193 for (int i=0; i<numPointers; i++) {
194 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
195 // We keep the average data in offsets based on the pointer
196 // ID, so we don't need to move it around as fingers are
197 // pressed and released.
198 final int p = mPointerIds[i];
199 final int poff = p * MotionEvent.NUM_SAMPLE_DATA * HISTORY_SIZE;
200 if (i == upOrDownPointer && lastNumPointers != nextNumPointers) {
201 if (lastNumPointers < nextNumPointers) {
202 // This pointer is going down. Clear its history
203 // and start fresh.
204 if (DEBUG_HACKS) Log.v("InputDevice", "Pointer down @ index "
205 + upOrDownPointer + " id " + mPointerIds[i]);
206 mHistoryDataStart[i] = 0;
207 mHistoryDataEnd[i] = 0;
208 System.arraycopy(rawData, ioff, mHistoryData, poff,
209 MotionEvent.NUM_SAMPLE_DATA);
210 System.arraycopy(rawData, ioff, mAveragedData, ioff,
211 MotionEvent.NUM_SAMPLE_DATA);
212 continue;
213 } else {
214 // The pointer is going up. Just fall through to
215 // recompute the last averaged point (and don't add
216 // it as a new point to include in the average).
217 if (DEBUG_HACKS) Log.v("InputDevice", "Pointer up @ index "
218 + upOrDownPointer + " id " + mPointerIds[i]);
219 }
220 } else {
221 int end = mHistoryDataEnd[i];
222 int eoff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
223 int oldX = mHistoryData[eoff + MotionEvent.SAMPLE_X];
224 int oldY = mHistoryData[eoff + MotionEvent.SAMPLE_Y];
225 int newX = rawData[ioff + MotionEvent.SAMPLE_X];
226 int newY = rawData[ioff + MotionEvent.SAMPLE_Y];
227 int dx = newX-oldX;
228 int dy = newY-oldY;
229 int delta = dx*dx + dy*dy;
230 if (DEBUG_HACKS) Log.v("InputDevice", "Delta from last: " + delta);
231 if (delta >= (75*75)) {
232 // Magic number, if moving farther than this, turn
233 // off filtering to avoid lag in response.
234 mHistoryDataStart[i] = 0;
235 mHistoryDataEnd[i] = 0;
236 System.arraycopy(rawData, ioff, mHistoryData, poff,
237 MotionEvent.NUM_SAMPLE_DATA);
238 System.arraycopy(rawData, ioff, mAveragedData, ioff,
239 MotionEvent.NUM_SAMPLE_DATA);
240 continue;
241 } else {
242 end++;
243 if (end >= HISTORY_SIZE) {
244 end -= HISTORY_SIZE;
245 }
246 mHistoryDataEnd[i] = end;
247 int noff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
248 mHistoryData[noff + MotionEvent.SAMPLE_X] = newX;
249 mHistoryData[noff + MotionEvent.SAMPLE_Y] = newY;
250 mHistoryData[noff + MotionEvent.SAMPLE_PRESSURE]
251 = rawData[ioff + MotionEvent.SAMPLE_PRESSURE];
252 int start = mHistoryDataStart[i];
253 if (end == start) {
254 start++;
255 if (start >= HISTORY_SIZE) {
256 start -= HISTORY_SIZE;
257 }
258 mHistoryDataStart[i] = start;
259 }
260 }
261 }
262
263 // Now compute the average.
264 int start = mHistoryDataStart[i];
265 int end = mHistoryDataEnd[i];
266 int x=0, y=0;
267 int totalPressure = 0;
268 while (start != end) {
269 int soff = poff + (start*MotionEvent.NUM_SAMPLE_DATA);
270 int pressure = mHistoryData[soff + MotionEvent.SAMPLE_PRESSURE];
271 x += mHistoryData[soff + MotionEvent.SAMPLE_X] * pressure;
272 y += mHistoryData[soff + MotionEvent.SAMPLE_Y] * pressure;
273 totalPressure += pressure;
274 start++;
275 if (start >= HISTORY_SIZE) start = 0;
276 }
277 int eoff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
278 int pressure = mHistoryData[eoff + MotionEvent.SAMPLE_PRESSURE];
279 x += mHistoryData[eoff + MotionEvent.SAMPLE_X] * pressure;
280 y += mHistoryData[eoff + MotionEvent.SAMPLE_Y] * pressure;
281 totalPressure += pressure;
282 x /= totalPressure;
283 y /= totalPressure;
284 if (DEBUG_HACKS) Log.v("InputDevice", "Averaging " + totalPressure
285 + " weight: (" + x + "," + y + ")");
286 mAveragedData[ioff + MotionEvent.SAMPLE_X] = x;
287 mAveragedData[ioff + MotionEvent.SAMPLE_Y] = y;
288 }
289 return mAveragedData;
290 }
291
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700292 private boolean assignPointer(int nextIndex, boolean allowOverlap) {
293 final int lastNumPointers = mLastNumPointers;
294 final int[] next2Last = mNext2Last;
295 final long[] next2LastDistance = mNext2LastDistance;
296 final int[] last2Next = mLast2Next;
297 final int[] lastData = mLastData;
298 final int[] nextData = mNextData;
299 final int id = nextIndex * MotionEvent.NUM_SAMPLE_DATA;
300
301 if (DEBUG_POINTERS) Log.v("InputDevice", "assignPointer: nextIndex="
302 + nextIndex + " dataOff=" + id);
303 final int x1 = nextData[id + MotionEvent.SAMPLE_X];
304 final int y1 = nextData[id + MotionEvent.SAMPLE_Y];
305
306 long bestDistance = -1;
307 int bestIndex = -1;
308 for (int j=0; j<lastNumPointers; j++) {
309 if (!allowOverlap && last2Next[j] < 0) {
310 continue;
311 }
312 final int jd = j * MotionEvent.NUM_SAMPLE_DATA;
313 final int xd = lastData[jd + MotionEvent.SAMPLE_X] - x1;
314 final int yd = lastData[jd + MotionEvent.SAMPLE_Y] - y1;
315 final long distance = xd*(long)xd + yd*(long)yd;
316 if (j == 0 || distance < bestDistance) {
317 bestDistance = distance;
318 bestIndex = j;
319 }
320 }
321
322 if (DEBUG_POINTERS) Log.v("InputDevice", "New index " + nextIndex
323 + " best old index=" + bestIndex + " (distance="
324 + bestDistance + ")");
325 next2Last[nextIndex] = bestIndex;
326 next2LastDistance[nextIndex] = bestDistance;
327
328 if (bestIndex < 0) {
329 return true;
330 }
331
332 if (last2Next[bestIndex] == -1) {
333 last2Next[bestIndex] = nextIndex;
334 return false;
335 }
336
337 if (DEBUG_POINTERS) Log.v("InputDevice", "Old index " + bestIndex
338 + " has multiple best new pointers!");
339
340 last2Next[bestIndex] = -2;
341 return true;
342 }
343
344 private int updatePointerIdentifiers() {
345 final int[] lastData = mLastData;
346 final int[] nextData = mNextData;
347 final int nextNumPointers = mNextNumPointers;
348 final int lastNumPointers = mLastNumPointers;
349
350 if (nextNumPointers == 1 && lastNumPointers == 1) {
351 System.arraycopy(nextData, 0, lastData, 0,
352 MotionEvent.NUM_SAMPLE_DATA);
353 return -1;
354 }
355
356 // Clear our old state.
357 final int[] last2Next = mLast2Next;
358 for (int i=0; i<lastNumPointers; i++) {
359 last2Next[i] = -1;
360 }
361
362 if (DEBUG_POINTERS) Log.v("InputDevice",
363 "Update pointers: lastNumPointers=" + lastNumPointers
364 + " nextNumPointers=" + nextNumPointers);
365
366 // Figure out the closes new points to the previous points.
367 final int[] next2Last = mNext2Last;
368 final long[] next2LastDistance = mNext2LastDistance;
369 boolean conflicts = false;
370 for (int i=0; i<nextNumPointers; i++) {
371 conflicts |= assignPointer(i, true);
372 }
373
374 // Resolve ambiguities in pointer mappings, when two or more
375 // new pointer locations find their best previous location is
376 // the same.
377 if (conflicts) {
378 if (DEBUG_POINTERS) Log.v("InputDevice", "Resolving conflicts");
379
380 for (int i=0; i<lastNumPointers; i++) {
381 if (last2Next[i] != -2) {
382 continue;
383 }
384
385 // Note that this algorithm is far from perfect. Ideally
386 // we should do something like the one described at
387 // http://portal.acm.org/citation.cfm?id=997856
388
389 if (DEBUG_POINTERS) Log.v("InputDevice",
390 "Resolving last index #" + i);
391
392 int numFound;
393 do {
394 numFound = 0;
395 long worstDistance = 0;
396 int worstJ = -1;
397 for (int j=0; j<nextNumPointers; j++) {
398 if (next2Last[j] != i) {
399 continue;
400 }
401 numFound++;
402 if (worstDistance < next2LastDistance[j]) {
403 worstDistance = next2LastDistance[j];
404 worstJ = j;
405 }
406 }
407
408 if (worstJ >= 0) {
409 if (DEBUG_POINTERS) Log.v("InputDevice",
410 "Worst new pointer: " + worstJ
411 + " (distance=" + worstDistance + ")");
412 if (assignPointer(worstJ, false)) {
413 // In this case there is no last pointer
414 // remaining for this new one!
415 next2Last[worstJ] = -1;
416 }
417 }
418 } while (numFound > 2);
419 }
420 }
421
422 int retIndex = -1;
423
424 if (lastNumPointers < nextNumPointers) {
425 // We have one or more new pointers that are down. Create a
426 // new pointer identifier for one of them.
427 if (DEBUG_POINTERS) Log.v("InputDevice", "Adding new pointer");
428 int nextId = 0;
429 int i=0;
430 while (i < lastNumPointers) {
431 if (mPointerIds[i] > nextId) {
432 // Found a hole, insert the pointer here.
433 if (DEBUG_POINTERS) Log.v("InputDevice",
434 "Inserting new pointer at hole " + i);
435 System.arraycopy(mPointerIds, i, mPointerIds,
436 i+1, lastNumPointers-i);
437 System.arraycopy(lastData, i*MotionEvent.NUM_SAMPLE_DATA,
438 lastData, (i+1)*MotionEvent.NUM_SAMPLE_DATA,
439 (lastNumPointers-i)*MotionEvent.NUM_SAMPLE_DATA);
440 break;
441 }
442 i++;
443 nextId++;
444 }
445
446 if (DEBUG_POINTERS) Log.v("InputDevice",
447 "New pointer id " + nextId + " at index " + i);
448
449 mLastNumPointers++;
450 retIndex = i;
451 mPointerIds[i] = nextId;
452
453 // And assign this identifier to the first new pointer.
454 for (int j=0; j<nextNumPointers; j++) {
455 if (next2Last[j] < 0) {
456 if (DEBUG_POINTERS) Log.v("InputDevice",
457 "Assigning new id to new pointer index " + j);
458 next2Last[j] = i;
459 break;
460 }
461 }
462 }
463
464 // Propagate all of the current data into the appropriate
465 // location in the old data to match the pointer ID that was
466 // assigned to it.
467 for (int i=0; i<nextNumPointers; i++) {
468 int lastIndex = next2Last[i];
469 if (lastIndex >= 0) {
470 if (DEBUG_POINTERS) Log.v("InputDevice",
471 "Copying next pointer index " + i
472 + " to last index " + lastIndex);
473 System.arraycopy(nextData, i*MotionEvent.NUM_SAMPLE_DATA,
474 lastData, lastIndex*MotionEvent.NUM_SAMPLE_DATA,
475 MotionEvent.NUM_SAMPLE_DATA);
476 }
477 }
478
479 if (lastNumPointers > nextNumPointers) {
480 // One or more pointers has gone up. Find the first one,
481 // and adjust accordingly.
482 if (DEBUG_POINTERS) Log.v("InputDevice", "Removing old pointer");
483 for (int i=0; i<lastNumPointers; i++) {
484 if (last2Next[i] == -1) {
485 if (DEBUG_POINTERS) Log.v("InputDevice",
486 "Removing old pointer at index " + i);
487 retIndex = i;
488 break;
489 }
490 }
491 }
492
493 return retIndex;
494 }
495
496 void removeOldPointer(int index) {
497 final int lastNumPointers = mLastNumPointers;
498 if (index >= 0 && index < lastNumPointers) {
499 System.arraycopy(mPointerIds, index+1, mPointerIds,
500 index, lastNumPointers-index-1);
501 System.arraycopy(mLastData, (index+1)*MotionEvent.NUM_SAMPLE_DATA,
502 mLastData, (index)*MotionEvent.NUM_SAMPLE_DATA,
503 (lastNumPointers-index-1)*MotionEvent.NUM_SAMPLE_DATA);
504 mLastNumPointers--;
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700508 MotionEvent generateAbsMotion(InputDevice device, long curTime,
509 long curTimeNano, Display display, int orientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 int metaState) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700511
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700512 if (mNextNumPointers <= 0 && mLastNumPointers <= 0) {
513 return null;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700514 }
515
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700516 final int lastNumPointers = mLastNumPointers;
517 final int nextNumPointers = mNextNumPointers;
518 if (mNextNumPointers > MAX_POINTERS) {
519 Log.w("InputDevice", "Number of pointers " + mNextNumPointers
520 + " exceeded maximum of " + MAX_POINTERS);
521 mNextNumPointers = MAX_POINTERS;
522 }
523
524 int upOrDownPointer = updatePointerIdentifiers();
525
526 final float[] reportData = mReportData;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700527 final int[] rawData;
528 if (KeyInputQueue.BAD_TOUCH_HACK) {
529 rawData = generateAveragedData(upOrDownPointer, lastNumPointers,
530 nextNumPointers);
531 } else {
532 rawData = mLastData;
533 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700534
535 final int numPointers = mLastNumPointers;
536
537 if (DEBUG_POINTERS) Log.v("InputDevice", "Processing "
538 + numPointers + " pointers (going from " + lastNumPointers
539 + " to " + nextNumPointers + ")");
540
541 for (int i=0; i<numPointers; i++) {
542 final int pos = i * MotionEvent.NUM_SAMPLE_DATA;
543 reportData[pos + MotionEvent.SAMPLE_X] = rawData[pos + MotionEvent.SAMPLE_X];
544 reportData[pos + MotionEvent.SAMPLE_Y] = rawData[pos + MotionEvent.SAMPLE_Y];
545 reportData[pos + MotionEvent.SAMPLE_PRESSURE] = rawData[pos + MotionEvent.SAMPLE_PRESSURE];
546 reportData[pos + MotionEvent.SAMPLE_SIZE] = rawData[pos + MotionEvent.SAMPLE_SIZE];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700547 }
548
549 int action;
550 int edgeFlags = 0;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700551 if (nextNumPointers != lastNumPointers) {
552 if (nextNumPointers > lastNumPointers) {
553 if (lastNumPointers == 0) {
554 action = MotionEvent.ACTION_DOWN;
555 mDownTime = curTime;
556 } else {
557 action = MotionEvent.ACTION_POINTER_DOWN
558 | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700559 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700560 } else {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700561 if (numPointers == 1) {
562 action = MotionEvent.ACTION_UP;
563 } else {
564 action = MotionEvent.ACTION_POINTER_UP
565 | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT);
566 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700567 }
568 currentMove = null;
569 } else {
570 action = MotionEvent.ACTION_MOVE;
571 }
572
573 final int dispW = display.getWidth()-1;
574 final int dispH = display.getHeight()-1;
575 int w = dispW;
576 int h = dispH;
577 if (orientation == Surface.ROTATION_90
578 || orientation == Surface.ROTATION_270) {
579 int tmp = w;
580 w = h;
581 h = tmp;
582 }
583
584 final AbsoluteInfo absX = device.absX;
585 final AbsoluteInfo absY = device.absY;
586 final AbsoluteInfo absPressure = device.absPressure;
587 final AbsoluteInfo absSize = device.absSize;
588 for (int i=0; i<numPointers; i++) {
589 final int j = i * MotionEvent.NUM_SAMPLE_DATA;
590
591 if (absX != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700592 reportData[j + MotionEvent.SAMPLE_X] =
593 ((reportData[j + MotionEvent.SAMPLE_X]-absX.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700594 / absX.range) * w;
595 }
596 if (absY != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700597 reportData[j + MotionEvent.SAMPLE_Y] =
598 ((reportData[j + MotionEvent.SAMPLE_Y]-absY.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700599 / absY.range) * h;
600 }
601 if (absPressure != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700602 reportData[j + MotionEvent.SAMPLE_PRESSURE] =
603 ((reportData[j + MotionEvent.SAMPLE_PRESSURE]-absPressure.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700604 / (float)absPressure.range);
605 }
606 if (absSize != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700607 reportData[j + MotionEvent.SAMPLE_SIZE] =
608 ((reportData[j + MotionEvent.SAMPLE_SIZE]-absSize.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700609 / (float)absSize.range);
610 }
611
612 switch (orientation) {
613 case Surface.ROTATION_90: {
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700614 final float temp = reportData[j + MotionEvent.SAMPLE_X];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700615 reportData[j + MotionEvent.SAMPLE_X] = reportData[j + MotionEvent.SAMPLE_Y];
616 reportData[j + MotionEvent.SAMPLE_Y] = w-temp;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700617 break;
618 }
619 case Surface.ROTATION_180: {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700620 reportData[j + MotionEvent.SAMPLE_X] = w-reportData[j + MotionEvent.SAMPLE_X];
621 reportData[j + MotionEvent.SAMPLE_Y] = h-reportData[j + MotionEvent.SAMPLE_Y];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700622 break;
623 }
624 case Surface.ROTATION_270: {
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700625 final float temp = reportData[j + MotionEvent.SAMPLE_X];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700626 reportData[j + MotionEvent.SAMPLE_X] = h-reportData[j + MotionEvent.SAMPLE_Y];
627 reportData[j + MotionEvent.SAMPLE_Y] = temp;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700628 break;
629 }
630 }
631 }
632
633 // We only consider the first pointer when computing the edge
634 // flags, since they are global to the event.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700635 if (action == MotionEvent.ACTION_DOWN) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700636 if (reportData[MotionEvent.SAMPLE_X] <= 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700637 edgeFlags |= MotionEvent.EDGE_LEFT;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700638 } else if (reportData[MotionEvent.SAMPLE_X] >= dispW) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700639 edgeFlags |= MotionEvent.EDGE_RIGHT;
640 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700641 if (reportData[MotionEvent.SAMPLE_Y] <= 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700642 edgeFlags |= MotionEvent.EDGE_TOP;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700643 } else if (reportData[MotionEvent.SAMPLE_Y] >= dispH) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700644 edgeFlags |= MotionEvent.EDGE_BOTTOM;
645 }
646 }
647
648 if (currentMove != null) {
649 if (false) Log.i("InputDevice", "Adding batch x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700650 + reportData[MotionEvent.SAMPLE_X]
651 + " y=" + reportData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700652 + " to " + currentMove);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700653 currentMove.addBatch(curTime, reportData, metaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700654 if (WindowManagerPolicy.WATCH_POINTER) {
655 Log.i("KeyInputQueue", "Updating: " + currentMove);
656 }
657 return null;
658 }
659
660 MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700661 curTimeNano, action, numPointers, mPointerIds, reportData,
662 metaState, xPrecision, yPrecision, device.id, edgeFlags);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700663 if (action == MotionEvent.ACTION_MOVE) {
664 currentMove = me;
665 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700666
667 if (nextNumPointers < lastNumPointers) {
668 removeOldPointer(upOrDownPointer);
669 }
670
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700671 return me;
672 }
673
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700674 boolean hasMore() {
675 return mLastNumPointers != mNextNumPointers;
676 }
677
678 void finish() {
679 mNextNumPointers = mAddingPointerOffset = 0;
680 mNextData[MotionEvent.SAMPLE_PRESSURE] = 0;
681 }
682
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700683 MotionEvent generateRelMotion(InputDevice device, long curTime,
684 long curTimeNano, int orientation, int metaState) {
685
686 final float[] scaled = mReportData;
687
688 // For now we only support 1 pointer with relative motions.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700689 scaled[MotionEvent.SAMPLE_X] = mNextData[MotionEvent.SAMPLE_X];
690 scaled[MotionEvent.SAMPLE_Y] = mNextData[MotionEvent.SAMPLE_Y];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700691 scaled[MotionEvent.SAMPLE_PRESSURE] = 1.0f;
692 scaled[MotionEvent.SAMPLE_SIZE] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 int edgeFlags = 0;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700694
695 int action;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700696 if (mNextNumPointers != mLastNumPointers) {
697 mNextData[MotionEvent.SAMPLE_X] =
698 mNextData[MotionEvent.SAMPLE_Y] = 0;
699 if (mNextNumPointers > 0 && mLastNumPointers == 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700700 action = MotionEvent.ACTION_DOWN;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700701 mDownTime = curTime;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700702 } else if (mNextNumPointers == 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700703 action = MotionEvent.ACTION_UP;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700704 } else {
705 action = MotionEvent.ACTION_MOVE;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700706 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700707 mLastNumPointers = mNextNumPointers;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700708 currentMove = null;
709 } else {
710 action = MotionEvent.ACTION_MOVE;
711 }
712
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700713 scaled[MotionEvent.SAMPLE_X] *= xMoveScale;
714 scaled[MotionEvent.SAMPLE_Y] *= yMoveScale;
715 switch (orientation) {
716 case Surface.ROTATION_90: {
717 final float temp = scaled[MotionEvent.SAMPLE_X];
718 scaled[MotionEvent.SAMPLE_X] = scaled[MotionEvent.SAMPLE_Y];
719 scaled[MotionEvent.SAMPLE_Y] = -temp;
720 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700722 case Surface.ROTATION_180: {
723 scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_X];
724 scaled[MotionEvent.SAMPLE_Y] = -scaled[MotionEvent.SAMPLE_Y];
725 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700727 case Surface.ROTATION_270: {
728 final float temp = scaled[MotionEvent.SAMPLE_X];
729 scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_Y];
730 scaled[MotionEvent.SAMPLE_Y] = temp;
731 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733 }
734
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700735 if (currentMove != null) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700736 if (false) Log.i("InputDevice", "Adding batch x="
737 + scaled[MotionEvent.SAMPLE_X]
738 + " y=" + scaled[MotionEvent.SAMPLE_Y]
739 + " to " + currentMove);
740 currentMove.addBatch(curTime, scaled, metaState);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700741 if (WindowManagerPolicy.WATCH_POINTER) {
742 Log.i("KeyInputQueue", "Updating: " + currentMove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700744 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700746
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700747 MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700748 curTimeNano, action, 1, mPointerIds, scaled, metaState,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700749 xPrecision, yPrecision, device.id, edgeFlags);
750 if (action == MotionEvent.ACTION_MOVE) {
751 currentMove = me;
752 }
753 return me;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
755 }
756
757 static class AbsoluteInfo {
758 int minValue;
759 int maxValue;
760 int range;
761 int flat;
762 int fuzz;
763 };
764
765 InputDevice(int _id, int _classes, String _name,
766 AbsoluteInfo _absX, AbsoluteInfo _absY,
767 AbsoluteInfo _absPressure, AbsoluteInfo _absSize) {
768 id = _id;
769 classes = _classes;
770 name = _name;
771 absX = _absX;
772 absY = _absY;
773 absPressure = _absPressure;
774 absSize = _absSize;
775 }
776};