blob: ed7eed07d726705ca31fb8cd2e70e6c8b9f4217b [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
Joe Onorato8a9b2202010-02-26 18:56:32 -080019import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.view.Display;
21import android.view.MotionEvent;
22import android.view.Surface;
23import android.view.WindowManagerPolicy;
24
Dianne Hackborna2e92262010-03-02 17:19:29 -080025import java.io.PrintWriter;
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027public class InputDevice {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070028 static final boolean DEBUG_POINTERS = false;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070029 static final boolean DEBUG_HACKS = false;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 /** Amount that trackball needs to move in order to generate a key event. */
32 static final int TRACKBALL_MOVEMENT_THRESHOLD = 6;
33
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070034 /** Maximum number of pointers we will track and report. */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070035 static final int MAX_POINTERS = 10;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070036
Adam Powellf5bcc6a2010-03-02 10:42:16 -080037 /**
38 * Slop distance for jumpy pointer detection.
39 * This is in touchscreen coordinates, not pixels or dips.
40 */
41 private static final int JUMPY_EPSILON = 30;
42
43 /** Number of jumpy points to drop for touchscreens that need it. */
44 private static final int JUMPY_TRANSITION_DROPS = 3;
45 private static final int JUMPY_DROP_LIMIT = 3;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 final int id;
48 final int classes;
49 final String name;
50 final AbsoluteInfo absX;
51 final AbsoluteInfo absY;
52 final AbsoluteInfo absPressure;
53 final AbsoluteInfo absSize;
54
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070055 long mKeyDownTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 int mMetaKeysState = 0;
57
Dianne Hackborn2a2b34432009-08-12 17:13:55 -070058 // For use by KeyInputQueue for keeping track of the current touch
59 // data in the old non-multi-touch protocol.
60 final int[] curTouchVals = new int[MotionEvent.NUM_SAMPLE_DATA * 2];
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 final MotionState mAbs = new MotionState(0, 0);
63 final MotionState mRel = new MotionState(TRACKBALL_MOVEMENT_THRESHOLD,
64 TRACKBALL_MOVEMENT_THRESHOLD);
65
66 static class MotionState {
67 int xPrecision;
68 int yPrecision;
69 float xMoveScale;
70 float yMoveScale;
71 MotionEvent currentMove = null;
72 boolean changed = false;
Dianne Hackborna2e92262010-03-02 17:19:29 -080073 boolean everChanged = false;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070074 long mDownTime = 0;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070075
76 // The currently assigned pointer IDs, corresponding to the last data.
77 int[] mPointerIds = new int[MAX_POINTERS];
78
79 // This is the last generated pointer data, ordered to match
80 // mPointerIds.
Dianne Hackborndc953722009-10-19 11:24:39 -070081 boolean mSkipLastPointers;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070082 int mLastNumPointers = 0;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070083 final int[] mLastData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070084
85 // This is the next set of pointer data being generated. It is not
86 // in any known order, and will be propagated in to mLastData
87 // as part of mapping it to the appropriate pointer IDs.
88 // Note that we have one extra sample of data here, to help clients
89 // avoid doing bounds checking.
90 int mNextNumPointers = 0;
91 final int[] mNextData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS)
92 + MotionEvent.NUM_SAMPLE_DATA];
93
Dianne Hackborn1411d1c2009-10-12 23:21:18 -070094 // Used to determine whether we dropped bad data, to avoid doing
95 // it repeatedly.
96 final boolean[] mDroppedBadPoint = new boolean[MAX_POINTERS];
Adam Powellf5bcc6a2010-03-02 10:42:16 -080097
98 // Used to count the number of jumpy points dropped.
99 private int mJumpyPointsDropped = 0;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700100
101 // Used to perform averaging of reported coordinates, to smooth
102 // the data and filter out transients during a release.
103 static final int HISTORY_SIZE = 5;
104 int[] mHistoryDataStart = new int[MAX_POINTERS];
105 int[] mHistoryDataEnd = new int[MAX_POINTERS];
106 final int[] mHistoryData = new int[(MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS)
107 * HISTORY_SIZE];
108 final int[] mAveragedData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
109
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700110 // Temporary data structures for doing the pointer ID mapping.
111 final int[] mLast2Next = new int[MAX_POINTERS];
112 final int[] mNext2Last = new int[MAX_POINTERS];
113 final long[] mNext2LastDistance = new long[MAX_POINTERS];
114
115 // Temporary data structure for generating the final motion data.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700116 final float[] mReportData = new float[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700118 // This is not used here, but can be used by callers for state tracking.
119 int mAddingPointerOffset = 0;
120 final boolean[] mDown = new boolean[MAX_POINTERS];
121
Dianne Hackborna2e92262010-03-02 17:19:29 -0800122 void dumpIntArray(PrintWriter pw, int[] array) {
123 pw.print("[");
124 for (int i=0; i<array.length; i++) {
125 if (i > 0) pw.print(", ");
126 pw.print(array[i]);
127 }
128 pw.print("]");
129 }
130
131 void dumpBooleanArray(PrintWriter pw, boolean[] array) {
132 pw.print("[");
133 for (int i=0; i<array.length; i++) {
134 if (i > 0) pw.print(", ");
135 pw.print(array[i] ? "true" : "false");
136 }
137 pw.print("]");
138 }
139
140 void dump(PrintWriter pw, String prefix) {
141 pw.print(prefix); pw.print("xPrecision="); pw.print(xPrecision);
142 pw.print(" yPrecision="); pw.println(yPrecision);
143 pw.print(prefix); pw.print("xMoveScale="); pw.print(xMoveScale);
144 pw.print(" yMoveScale="); pw.println(yMoveScale);
145 if (currentMove != null) {
146 pw.print(prefix); pw.print("currentMove="); pw.println(currentMove);
147 }
148 if (changed || mDownTime != 0) {
149 pw.print(prefix); pw.print("changed="); pw.print(changed);
150 pw.print(" mDownTime="); pw.println(mDownTime);
151 }
152 pw.print(prefix); pw.print("mPointerIds="); dumpIntArray(pw, mPointerIds);
153 pw.println("");
154 if (mSkipLastPointers || mLastNumPointers != 0) {
155 pw.print(prefix); pw.print("mSkipLastPointers="); pw.print(mSkipLastPointers);
156 pw.print(" mLastNumPointers="); pw.println(mLastNumPointers);
157 pw.print(prefix); pw.print("mLastData="); dumpIntArray(pw, mLastData);
158 pw.println("");
159 }
160 if (mNextNumPointers != 0) {
161 pw.print(prefix); pw.print("mNextNumPointers="); pw.println(mNextNumPointers);
162 pw.print(prefix); pw.print("mNextData="); dumpIntArray(pw, mNextData);
163 pw.println("");
164 }
165 pw.print(prefix); pw.print("mDroppedBadPoint=");
166 dumpBooleanArray(pw, mDroppedBadPoint); pw.println("");
167 pw.print(prefix); pw.print("mAddingPointerOffset="); pw.println(mAddingPointerOffset);
168 pw.print(prefix); pw.print("mDown=");
169 dumpBooleanArray(pw, mDown); pw.println("");
170 }
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 MotionState(int mx, int my) {
173 xPrecision = mx;
174 yPrecision = my;
175 xMoveScale = mx != 0 ? (1.0f/mx) : 1.0f;
176 yMoveScale = my != 0 ? (1.0f/my) : 1.0f;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700177 for (int i=0; i<MAX_POINTERS; i++) {
178 mPointerIds[i] = i;
179 }
180 }
181
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700182 /**
183 * Special hack for devices that have bad screen data: if one of the
184 * points has moved more than a screen height from the last position,
185 * then drop it.
186 */
187 void dropBadPoint(InputDevice dev) {
188 // We should always have absY, but let's be paranoid.
189 if (dev.absY == null) {
190 return;
191 }
192 // Don't do anything if a finger is going down or up. We run
193 // here before assigning pointer IDs, so there isn't a good
194 // way to do per-finger matching.
195 if (mNextNumPointers != mLastNumPointers) {
196 return;
197 }
198
199 // We consider a single movement across more than a 7/16 of
200 // the long size of the screen to be bad. This was a magic value
201 // determined by looking at the maximum distance it is feasible
202 // to actually move in one sample.
203 final int maxDy = ((dev.absY.maxValue-dev.absY.minValue)*7)/16;
204
205 // Look through all new points and see if any are farther than
206 // acceptable from all previous points.
207 for (int i=mNextNumPointers-1; i>=0; i--) {
208 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
209 //final int x = mNextData[ioff + MotionEvent.SAMPLE_X];
210 final int y = mNextData[ioff + MotionEvent.SAMPLE_Y];
Joe Onorato8a9b2202010-02-26 18:56:32 -0800211 if (DEBUG_HACKS) Slog.v("InputDevice", "Looking at next point #" + i + ": y=" + y);
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700212 boolean dropped = false;
213 if (!mDroppedBadPoint[i] && mLastNumPointers > 0) {
214 dropped = true;
215 int closestDy = -1;
216 int closestY = -1;
217 // We will drop this new point if it is sufficiently
218 // far away from -all- last points.
219 for (int j=mLastNumPointers-1; j>=0; j--) {
220 final int joff = j * MotionEvent.NUM_SAMPLE_DATA;
221 //int dx = x - mLastData[joff + MotionEvent.SAMPLE_X];
222 int dy = y - mLastData[joff + MotionEvent.SAMPLE_Y];
223 //if (dx < 0) dx = -dx;
224 if (dy < 0) dy = -dy;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800225 if (DEBUG_HACKS) Slog.v("InputDevice", "Comparing with last point #" + j
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700226 + ": y=" + mLastData[joff] + " dy=" + dy);
227 if (dy < maxDy) {
228 dropped = false;
229 break;
230 } else if (closestDy < 0 || dy < closestDy) {
231 closestDy = dy;
232 closestY = mLastData[joff + MotionEvent.SAMPLE_Y];
233 }
234 }
235 if (dropped) {
236 dropped = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800237 Slog.i("InputDevice", "Dropping bad point #" + i
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700238 + ": newY=" + y + " closestDy=" + closestDy
239 + " maxDy=" + maxDy);
240 mNextData[ioff + MotionEvent.SAMPLE_Y] = closestY;
241 break;
242 }
243 }
244 mDroppedBadPoint[i] = dropped;
245 }
246 }
247
Adam Powellf5bcc6a2010-03-02 10:42:16 -0800248 void dropJumpyPoint(InputDevice dev) {
249 final int nextNumPointers = mNextNumPointers;
250 final int lastNumPointers = mLastNumPointers;
251 final int[] nextData = mNextData;
252 final int[] lastData = mLastData;
253
254 if (nextNumPointers != mLastNumPointers) {
255 if (DEBUG_HACKS) {
256 Slog.d("InputDevice", "Different pointer count " + lastNumPointers +
257 " -> " + nextNumPointers);
258 for (int i = 0; i < nextNumPointers; i++) {
259 int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
260 Slog.d("InputDevice", "Pointer " + i + " (" +
261 mNextData[ioff + MotionEvent.SAMPLE_X] + ", " +
262 mNextData[ioff + MotionEvent.SAMPLE_Y] + ")");
263 }
264 }
265
266 // Just drop the first few events going from 1 to 2 pointers.
267 // They're bad often enough that they're not worth considering.
268 if (lastNumPointers == 1 && nextNumPointers == 2
269 && mJumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
270 mNextNumPointers = 1;
271 mJumpyPointsDropped++;
272 } else if (lastNumPointers == 2 && nextNumPointers == 1
273 && mJumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
274 // The event when we go from 2 -> 1 tends to be messed up too
275 System.arraycopy(lastData, 0, nextData, 0,
276 lastNumPointers * MotionEvent.NUM_SAMPLE_DATA);
277 mNextNumPointers = lastNumPointers;
278 mJumpyPointsDropped++;
279
280 if (DEBUG_HACKS) {
281 for (int i = 0; i < mNextNumPointers; i++) {
282 int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
283 Slog.d("InputDevice", "Pointer " + i + " replaced (" +
284 mNextData[ioff + MotionEvent.SAMPLE_X] + ", " +
285 mNextData[ioff + MotionEvent.SAMPLE_Y] + ")");
286 }
287 }
288 } else {
289 mJumpyPointsDropped = 0;
290
291 if (DEBUG_HACKS) {
292 Slog.d("InputDevice", "Transition - drop limit reset");
293 }
294 }
295 return;
296 }
297
298 // A 'jumpy' point is one where the coordinate value for one axis
299 // has jumped to the other pointer's location. No need to do anything
300 // else if we only have one pointer.
301 if (nextNumPointers < 2) {
302 return;
303 }
304
305 int badPointerIndex = -1;
306 int badPointerReplaceXWith = 0;
307 int badPointerReplaceYWith = 0;
308 int badPointerDistance = Integer.MIN_VALUE;
309 for (int i = nextNumPointers - 1; i >= 0; i--) {
310 boolean dropx = false;
311 boolean dropy = false;
312
313 // Limit how many times a jumpy point can get dropped.
314 if (mJumpyPointsDropped < JUMPY_DROP_LIMIT) {
315 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
316 final int x = nextData[ioff + MotionEvent.SAMPLE_X];
317 final int y = nextData[ioff + MotionEvent.SAMPLE_Y];
318
319 if (DEBUG_HACKS) {
320 Slog.d("InputDevice", "Point " + i + " (" + x + ", " + y + ")");
321 }
322
323 // Check if a touch point is too close to another's coordinates
324 for (int j = 0; j < nextNumPointers && !dropx && !dropy; j++) {
325 if (j == i) {
326 continue;
327 }
328
329 final int joff = j * MotionEvent.NUM_SAMPLE_DATA;
330 final int xOther = nextData[joff + MotionEvent.SAMPLE_X];
331 final int yOther = nextData[joff + MotionEvent.SAMPLE_Y];
332
333 dropx = Math.abs(x - xOther) <= JUMPY_EPSILON;
334 dropy = Math.abs(y - yOther) <= JUMPY_EPSILON;
335 }
336
337 if (dropx) {
338 int xreplace = lastData[MotionEvent.SAMPLE_X];
339 int yreplace = lastData[MotionEvent.SAMPLE_Y];
340 int distance = Math.abs(yreplace - y);
341 for (int j = 1; j < lastNumPointers; j++) {
342 final int joff = j * MotionEvent.NUM_SAMPLE_DATA;
343 int lasty = lastData[joff + MotionEvent.SAMPLE_Y];
344 int currDist = Math.abs(lasty - y);
345 if (currDist < distance) {
346 xreplace = lastData[joff + MotionEvent.SAMPLE_X];
347 yreplace = lasty;
348 distance = currDist;
349 }
350 }
351
352 int badXDelta = Math.abs(xreplace - x);
353 if (badXDelta > badPointerDistance) {
354 badPointerDistance = badXDelta;
355 badPointerIndex = i;
356 badPointerReplaceXWith = xreplace;
357 badPointerReplaceYWith = yreplace;
358 }
359 } else if (dropy) {
360 int xreplace = lastData[MotionEvent.SAMPLE_X];
361 int yreplace = lastData[MotionEvent.SAMPLE_Y];
362 int distance = Math.abs(xreplace - x);
363 for (int j = 1; j < lastNumPointers; j++) {
364 final int joff = j * MotionEvent.NUM_SAMPLE_DATA;
365 int lastx = lastData[joff + MotionEvent.SAMPLE_X];
366 int currDist = Math.abs(lastx - x);
367 if (currDist < distance) {
368 xreplace = lastx;
369 yreplace = lastData[joff + MotionEvent.SAMPLE_Y];
370 distance = currDist;
371 }
372 }
373
374 int badYDelta = Math.abs(yreplace - y);
375 if (badYDelta > badPointerDistance) {
376 badPointerDistance = badYDelta;
377 badPointerIndex = i;
378 badPointerReplaceXWith = xreplace;
379 badPointerReplaceYWith = yreplace;
380 }
381 }
382 }
383 }
384 if (badPointerIndex >= 0) {
385 if (DEBUG_HACKS) {
386 Slog.d("InputDevice", "Replacing bad pointer " + badPointerIndex +
387 " with (" + badPointerReplaceXWith + ", " + badPointerReplaceYWith +
388 ")");
389 }
390
391 final int offset = badPointerIndex * MotionEvent.NUM_SAMPLE_DATA;
392 nextData[offset + MotionEvent.SAMPLE_X] = badPointerReplaceXWith;
393 nextData[offset + MotionEvent.SAMPLE_Y] = badPointerReplaceYWith;
394 mJumpyPointsDropped++;
395 } else {
396 mJumpyPointsDropped = 0;
397 }
398 }
399
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700400 /**
401 * Special hack for devices that have bad screen data: aggregate and
402 * compute averages of the coordinate data, to reduce the amount of
403 * jitter seen by applications.
404 */
405 int[] generateAveragedData(int upOrDownPointer, int lastNumPointers,
406 int nextNumPointers) {
407 final int numPointers = mLastNumPointers;
408 final int[] rawData = mLastData;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800409 if (DEBUG_HACKS) Slog.v("InputDevice", "lastNumPointers=" + lastNumPointers
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700410 + " nextNumPointers=" + nextNumPointers
411 + " numPointers=" + numPointers);
412 for (int i=0; i<numPointers; i++) {
413 final int ioff = i * MotionEvent.NUM_SAMPLE_DATA;
414 // We keep the average data in offsets based on the pointer
415 // ID, so we don't need to move it around as fingers are
416 // pressed and released.
417 final int p = mPointerIds[i];
418 final int poff = p * MotionEvent.NUM_SAMPLE_DATA * HISTORY_SIZE;
419 if (i == upOrDownPointer && lastNumPointers != nextNumPointers) {
420 if (lastNumPointers < nextNumPointers) {
421 // This pointer is going down. Clear its history
422 // and start fresh.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800423 if (DEBUG_HACKS) Slog.v("InputDevice", "Pointer down @ index "
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700424 + upOrDownPointer + " id " + mPointerIds[i]);
425 mHistoryDataStart[i] = 0;
426 mHistoryDataEnd[i] = 0;
427 System.arraycopy(rawData, ioff, mHistoryData, poff,
428 MotionEvent.NUM_SAMPLE_DATA);
429 System.arraycopy(rawData, ioff, mAveragedData, ioff,
430 MotionEvent.NUM_SAMPLE_DATA);
431 continue;
432 } else {
433 // The pointer is going up. Just fall through to
434 // recompute the last averaged point (and don't add
435 // it as a new point to include in the average).
Joe Onorato8a9b2202010-02-26 18:56:32 -0800436 if (DEBUG_HACKS) Slog.v("InputDevice", "Pointer up @ index "
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700437 + upOrDownPointer + " id " + mPointerIds[i]);
438 }
439 } else {
440 int end = mHistoryDataEnd[i];
441 int eoff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
442 int oldX = mHistoryData[eoff + MotionEvent.SAMPLE_X];
443 int oldY = mHistoryData[eoff + MotionEvent.SAMPLE_Y];
444 int newX = rawData[ioff + MotionEvent.SAMPLE_X];
445 int newY = rawData[ioff + MotionEvent.SAMPLE_Y];
446 int dx = newX-oldX;
447 int dy = newY-oldY;
448 int delta = dx*dx + dy*dy;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800449 if (DEBUG_HACKS) Slog.v("InputDevice", "Delta from last: " + delta);
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700450 if (delta >= (75*75)) {
451 // Magic number, if moving farther than this, turn
452 // off filtering to avoid lag in response.
453 mHistoryDataStart[i] = 0;
454 mHistoryDataEnd[i] = 0;
455 System.arraycopy(rawData, ioff, mHistoryData, poff,
456 MotionEvent.NUM_SAMPLE_DATA);
457 System.arraycopy(rawData, ioff, mAveragedData, ioff,
458 MotionEvent.NUM_SAMPLE_DATA);
459 continue;
460 } else {
461 end++;
462 if (end >= HISTORY_SIZE) {
463 end -= HISTORY_SIZE;
464 }
465 mHistoryDataEnd[i] = end;
466 int noff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
467 mHistoryData[noff + MotionEvent.SAMPLE_X] = newX;
468 mHistoryData[noff + MotionEvent.SAMPLE_Y] = newY;
469 mHistoryData[noff + MotionEvent.SAMPLE_PRESSURE]
470 = rawData[ioff + MotionEvent.SAMPLE_PRESSURE];
471 int start = mHistoryDataStart[i];
472 if (end == start) {
473 start++;
474 if (start >= HISTORY_SIZE) {
475 start -= HISTORY_SIZE;
476 }
477 mHistoryDataStart[i] = start;
478 }
479 }
480 }
481
482 // Now compute the average.
483 int start = mHistoryDataStart[i];
484 int end = mHistoryDataEnd[i];
485 int x=0, y=0;
486 int totalPressure = 0;
487 while (start != end) {
488 int soff = poff + (start*MotionEvent.NUM_SAMPLE_DATA);
489 int pressure = mHistoryData[soff + MotionEvent.SAMPLE_PRESSURE];
Dianne Hackborn53cd5792009-10-13 19:50:51 -0700490 if (pressure <= 0) pressure = 1;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700491 x += mHistoryData[soff + MotionEvent.SAMPLE_X] * pressure;
492 y += mHistoryData[soff + MotionEvent.SAMPLE_Y] * pressure;
493 totalPressure += pressure;
494 start++;
495 if (start >= HISTORY_SIZE) start = 0;
496 }
497 int eoff = poff + (end*MotionEvent.NUM_SAMPLE_DATA);
498 int pressure = mHistoryData[eoff + MotionEvent.SAMPLE_PRESSURE];
Dianne Hackborn53cd5792009-10-13 19:50:51 -0700499 if (pressure <= 0) pressure = 1;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700500 x += mHistoryData[eoff + MotionEvent.SAMPLE_X] * pressure;
501 y += mHistoryData[eoff + MotionEvent.SAMPLE_Y] * pressure;
502 totalPressure += pressure;
503 x /= totalPressure;
504 y /= totalPressure;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800505 if (DEBUG_HACKS) Slog.v("InputDevice", "Averaging " + totalPressure
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700506 + " weight: (" + x + "," + y + ")");
507 mAveragedData[ioff + MotionEvent.SAMPLE_X] = x;
508 mAveragedData[ioff + MotionEvent.SAMPLE_Y] = y;
Dianne Hackborn05799982009-11-23 13:08:14 -0800509 mAveragedData[ioff + MotionEvent.SAMPLE_PRESSURE] =
510 rawData[ioff + MotionEvent.SAMPLE_PRESSURE];
511 mAveragedData[ioff + MotionEvent.SAMPLE_SIZE] =
512 rawData[ioff + MotionEvent.SAMPLE_SIZE];
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700513 }
514 return mAveragedData;
515 }
516
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700517 private boolean assignPointer(int nextIndex, boolean allowOverlap) {
518 final int lastNumPointers = mLastNumPointers;
519 final int[] next2Last = mNext2Last;
520 final long[] next2LastDistance = mNext2LastDistance;
521 final int[] last2Next = mLast2Next;
522 final int[] lastData = mLastData;
523 final int[] nextData = mNextData;
524 final int id = nextIndex * MotionEvent.NUM_SAMPLE_DATA;
525
Joe Onorato8a9b2202010-02-26 18:56:32 -0800526 if (DEBUG_POINTERS) Slog.v("InputDevice", "assignPointer: nextIndex="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700527 + nextIndex + " dataOff=" + id);
528 final int x1 = nextData[id + MotionEvent.SAMPLE_X];
529 final int y1 = nextData[id + MotionEvent.SAMPLE_Y];
530
531 long bestDistance = -1;
532 int bestIndex = -1;
533 for (int j=0; j<lastNumPointers; j++) {
Dianne Hackborn709d6db2009-12-02 18:42:39 -0800534 // If we are not allowing multiple new points to be assigned
535 // to the same old pointer, then skip this one if it is already
536 // detected as a conflict (-2).
537 if (!allowOverlap && last2Next[j] < -1) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700538 continue;
539 }
540 final int jd = j * MotionEvent.NUM_SAMPLE_DATA;
541 final int xd = lastData[jd + MotionEvent.SAMPLE_X] - x1;
542 final int yd = lastData[jd + MotionEvent.SAMPLE_Y] - y1;
543 final long distance = xd*(long)xd + yd*(long)yd;
Dianne Hackborn709d6db2009-12-02 18:42:39 -0800544 if (bestDistance == -1 || distance < bestDistance) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700545 bestDistance = distance;
546 bestIndex = j;
547 }
548 }
549
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 if (DEBUG_POINTERS) Slog.v("InputDevice", "New index " + nextIndex
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700551 + " best old index=" + bestIndex + " (distance="
552 + bestDistance + ")");
553 next2Last[nextIndex] = bestIndex;
554 next2LastDistance[nextIndex] = bestDistance;
555
556 if (bestIndex < 0) {
557 return true;
558 }
559
560 if (last2Next[bestIndex] == -1) {
561 last2Next[bestIndex] = nextIndex;
562 return false;
563 }
564
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 if (DEBUG_POINTERS) Slog.v("InputDevice", "Old index " + bestIndex
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700566 + " has multiple best new pointers!");
567
568 last2Next[bestIndex] = -2;
569 return true;
570 }
571
572 private int updatePointerIdentifiers() {
573 final int[] lastData = mLastData;
574 final int[] nextData = mNextData;
575 final int nextNumPointers = mNextNumPointers;
576 final int lastNumPointers = mLastNumPointers;
577
578 if (nextNumPointers == 1 && lastNumPointers == 1) {
579 System.arraycopy(nextData, 0, lastData, 0,
580 MotionEvent.NUM_SAMPLE_DATA);
581 return -1;
582 }
583
584 // Clear our old state.
585 final int[] last2Next = mLast2Next;
586 for (int i=0; i<lastNumPointers; i++) {
587 last2Next[i] = -1;
588 }
589
Joe Onorato8a9b2202010-02-26 18:56:32 -0800590 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700591 "Update pointers: lastNumPointers=" + lastNumPointers
592 + " nextNumPointers=" + nextNumPointers);
593
594 // Figure out the closes new points to the previous points.
595 final int[] next2Last = mNext2Last;
596 final long[] next2LastDistance = mNext2LastDistance;
597 boolean conflicts = false;
598 for (int i=0; i<nextNumPointers; i++) {
599 conflicts |= assignPointer(i, true);
600 }
601
602 // Resolve ambiguities in pointer mappings, when two or more
603 // new pointer locations find their best previous location is
604 // the same.
605 if (conflicts) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800606 if (DEBUG_POINTERS) Slog.v("InputDevice", "Resolving conflicts");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700607
608 for (int i=0; i<lastNumPointers; i++) {
609 if (last2Next[i] != -2) {
610 continue;
611 }
612
613 // Note that this algorithm is far from perfect. Ideally
614 // we should do something like the one described at
615 // http://portal.acm.org/citation.cfm?id=997856
616
Joe Onorato8a9b2202010-02-26 18:56:32 -0800617 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700618 "Resolving last index #" + i);
619
620 int numFound;
621 do {
622 numFound = 0;
623 long worstDistance = 0;
624 int worstJ = -1;
625 for (int j=0; j<nextNumPointers; j++) {
626 if (next2Last[j] != i) {
627 continue;
628 }
629 numFound++;
630 if (worstDistance < next2LastDistance[j]) {
631 worstDistance = next2LastDistance[j];
632 worstJ = j;
633 }
634 }
635
636 if (worstJ >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700638 "Worst new pointer: " + worstJ
639 + " (distance=" + worstDistance + ")");
640 if (assignPointer(worstJ, false)) {
641 // In this case there is no last pointer
642 // remaining for this new one!
643 next2Last[worstJ] = -1;
644 }
645 }
646 } while (numFound > 2);
647 }
648 }
649
650 int retIndex = -1;
651
652 if (lastNumPointers < nextNumPointers) {
653 // We have one or more new pointers that are down. Create a
654 // new pointer identifier for one of them.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800655 if (DEBUG_POINTERS) Slog.v("InputDevice", "Adding new pointer");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700656 int nextId = 0;
657 int i=0;
658 while (i < lastNumPointers) {
659 if (mPointerIds[i] > nextId) {
660 // Found a hole, insert the pointer here.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800661 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700662 "Inserting new pointer at hole " + i);
663 System.arraycopy(mPointerIds, i, mPointerIds,
664 i+1, lastNumPointers-i);
665 System.arraycopy(lastData, i*MotionEvent.NUM_SAMPLE_DATA,
666 lastData, (i+1)*MotionEvent.NUM_SAMPLE_DATA,
667 (lastNumPointers-i)*MotionEvent.NUM_SAMPLE_DATA);
668 break;
669 }
670 i++;
671 nextId++;
672 }
673
Joe Onorato8a9b2202010-02-26 18:56:32 -0800674 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700675 "New pointer id " + nextId + " at index " + i);
676
677 mLastNumPointers++;
678 retIndex = i;
679 mPointerIds[i] = nextId;
680
681 // And assign this identifier to the first new pointer.
682 for (int j=0; j<nextNumPointers; j++) {
683 if (next2Last[j] < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800684 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700685 "Assigning new id to new pointer index " + j);
686 next2Last[j] = i;
687 break;
688 }
689 }
690 }
691
692 // Propagate all of the current data into the appropriate
693 // location in the old data to match the pointer ID that was
694 // assigned to it.
695 for (int i=0; i<nextNumPointers; i++) {
696 int lastIndex = next2Last[i];
697 if (lastIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700699 "Copying next pointer index " + i
700 + " to last index " + lastIndex);
701 System.arraycopy(nextData, i*MotionEvent.NUM_SAMPLE_DATA,
702 lastData, lastIndex*MotionEvent.NUM_SAMPLE_DATA,
703 MotionEvent.NUM_SAMPLE_DATA);
704 }
705 }
706
707 if (lastNumPointers > nextNumPointers) {
708 // One or more pointers has gone up. Find the first one,
709 // and adjust accordingly.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 if (DEBUG_POINTERS) Slog.v("InputDevice", "Removing old pointer");
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700711 for (int i=0; i<lastNumPointers; i++) {
712 if (last2Next[i] == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 if (DEBUG_POINTERS) Slog.v("InputDevice",
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700714 "Removing old pointer at index " + i);
715 retIndex = i;
716 break;
717 }
718 }
719 }
720
721 return retIndex;
722 }
723
724 void removeOldPointer(int index) {
725 final int lastNumPointers = mLastNumPointers;
726 if (index >= 0 && index < lastNumPointers) {
727 System.arraycopy(mPointerIds, index+1, mPointerIds,
728 index, lastNumPointers-index-1);
729 System.arraycopy(mLastData, (index+1)*MotionEvent.NUM_SAMPLE_DATA,
730 mLastData, (index)*MotionEvent.NUM_SAMPLE_DATA,
731 (lastNumPointers-index-1)*MotionEvent.NUM_SAMPLE_DATA);
732 mLastNumPointers--;
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700736 MotionEvent generateAbsMotion(InputDevice device, long curTime,
737 long curTimeNano, Display display, int orientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 int metaState) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700739
Dianne Hackborndc953722009-10-19 11:24:39 -0700740 if (mSkipLastPointers) {
741 mSkipLastPointers = false;
742 mLastNumPointers = 0;
743 }
744
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700745 if (mNextNumPointers <= 0 && mLastNumPointers <= 0) {
746 return null;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700747 }
748
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700749 final int lastNumPointers = mLastNumPointers;
750 final int nextNumPointers = mNextNumPointers;
751 if (mNextNumPointers > MAX_POINTERS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800752 Slog.w("InputDevice", "Number of pointers " + mNextNumPointers
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700753 + " exceeded maximum of " + MAX_POINTERS);
754 mNextNumPointers = MAX_POINTERS;
755 }
756
757 int upOrDownPointer = updatePointerIdentifiers();
758
759 final float[] reportData = mReportData;
Dianne Hackborn1411d1c2009-10-12 23:21:18 -0700760 final int[] rawData;
761 if (KeyInputQueue.BAD_TOUCH_HACK) {
762 rawData = generateAveragedData(upOrDownPointer, lastNumPointers,
763 nextNumPointers);
764 } else {
765 rawData = mLastData;
766 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700767
768 final int numPointers = mLastNumPointers;
769
Joe Onorato8a9b2202010-02-26 18:56:32 -0800770 if (DEBUG_POINTERS) Slog.v("InputDevice", "Processing "
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700771 + numPointers + " pointers (going from " + lastNumPointers
772 + " to " + nextNumPointers + ")");
773
774 for (int i=0; i<numPointers; i++) {
775 final int pos = i * MotionEvent.NUM_SAMPLE_DATA;
776 reportData[pos + MotionEvent.SAMPLE_X] = rawData[pos + MotionEvent.SAMPLE_X];
777 reportData[pos + MotionEvent.SAMPLE_Y] = rawData[pos + MotionEvent.SAMPLE_Y];
778 reportData[pos + MotionEvent.SAMPLE_PRESSURE] = rawData[pos + MotionEvent.SAMPLE_PRESSURE];
779 reportData[pos + MotionEvent.SAMPLE_SIZE] = rawData[pos + MotionEvent.SAMPLE_SIZE];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700780 }
781
782 int action;
783 int edgeFlags = 0;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700784 if (nextNumPointers != lastNumPointers) {
785 if (nextNumPointers > lastNumPointers) {
786 if (lastNumPointers == 0) {
787 action = MotionEvent.ACTION_DOWN;
788 mDownTime = curTime;
789 } else {
790 action = MotionEvent.ACTION_POINTER_DOWN
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800791 | (upOrDownPointer << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700792 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700793 } else {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700794 if (numPointers == 1) {
795 action = MotionEvent.ACTION_UP;
796 } else {
797 action = MotionEvent.ACTION_POINTER_UP
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800798 | (upOrDownPointer << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700799 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700800 }
801 currentMove = null;
802 } else {
803 action = MotionEvent.ACTION_MOVE;
804 }
805
806 final int dispW = display.getWidth()-1;
807 final int dispH = display.getHeight()-1;
808 int w = dispW;
809 int h = dispH;
810 if (orientation == Surface.ROTATION_90
811 || orientation == Surface.ROTATION_270) {
812 int tmp = w;
813 w = h;
814 h = tmp;
815 }
816
817 final AbsoluteInfo absX = device.absX;
818 final AbsoluteInfo absY = device.absY;
819 final AbsoluteInfo absPressure = device.absPressure;
820 final AbsoluteInfo absSize = device.absSize;
821 for (int i=0; i<numPointers; i++) {
822 final int j = i * MotionEvent.NUM_SAMPLE_DATA;
823
824 if (absX != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700825 reportData[j + MotionEvent.SAMPLE_X] =
826 ((reportData[j + MotionEvent.SAMPLE_X]-absX.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700827 / absX.range) * w;
828 }
829 if (absY != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700830 reportData[j + MotionEvent.SAMPLE_Y] =
831 ((reportData[j + MotionEvent.SAMPLE_Y]-absY.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700832 / absY.range) * h;
833 }
834 if (absPressure != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700835 reportData[j + MotionEvent.SAMPLE_PRESSURE] =
836 ((reportData[j + MotionEvent.SAMPLE_PRESSURE]-absPressure.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700837 / (float)absPressure.range);
838 }
839 if (absSize != null) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700840 reportData[j + MotionEvent.SAMPLE_SIZE] =
841 ((reportData[j + MotionEvent.SAMPLE_SIZE]-absSize.minValue)
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700842 / (float)absSize.range);
843 }
844
845 switch (orientation) {
846 case Surface.ROTATION_90: {
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700847 final float temp = reportData[j + MotionEvent.SAMPLE_X];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700848 reportData[j + MotionEvent.SAMPLE_X] = reportData[j + MotionEvent.SAMPLE_Y];
849 reportData[j + MotionEvent.SAMPLE_Y] = w-temp;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700850 break;
851 }
852 case Surface.ROTATION_180: {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700853 reportData[j + MotionEvent.SAMPLE_X] = w-reportData[j + MotionEvent.SAMPLE_X];
854 reportData[j + MotionEvent.SAMPLE_Y] = h-reportData[j + MotionEvent.SAMPLE_Y];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700855 break;
856 }
857 case Surface.ROTATION_270: {
Dianne Hackborn2a2b34432009-08-12 17:13:55 -0700858 final float temp = reportData[j + MotionEvent.SAMPLE_X];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700859 reportData[j + MotionEvent.SAMPLE_X] = h-reportData[j + MotionEvent.SAMPLE_Y];
860 reportData[j + MotionEvent.SAMPLE_Y] = temp;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700861 break;
862 }
863 }
864 }
865
866 // We only consider the first pointer when computing the edge
867 // flags, since they are global to the event.
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700868 if (action == MotionEvent.ACTION_DOWN) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700869 if (reportData[MotionEvent.SAMPLE_X] <= 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700870 edgeFlags |= MotionEvent.EDGE_LEFT;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700871 } else if (reportData[MotionEvent.SAMPLE_X] >= dispW) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700872 edgeFlags |= MotionEvent.EDGE_RIGHT;
873 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700874 if (reportData[MotionEvent.SAMPLE_Y] <= 0) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700875 edgeFlags |= MotionEvent.EDGE_TOP;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700876 } else if (reportData[MotionEvent.SAMPLE_Y] >= dispH) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700877 edgeFlags |= MotionEvent.EDGE_BOTTOM;
878 }
879 }
880
881 if (currentMove != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800882 if (false) Slog.i("InputDevice", "Adding batch x="
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700883 + reportData[MotionEvent.SAMPLE_X]
884 + " y=" + reportData[MotionEvent.SAMPLE_Y]
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700885 + " to " + currentMove);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700886 currentMove.addBatch(curTime, reportData, metaState);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700887 if (WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800888 Slog.i("KeyInputQueue", "Updating: " + currentMove);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700889 }
890 return null;
891 }
892
893 MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700894 curTimeNano, action, numPointers, mPointerIds, reportData,
895 metaState, xPrecision, yPrecision, device.id, edgeFlags);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700896 if (action == MotionEvent.ACTION_MOVE) {
897 currentMove = me;
898 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700899
900 if (nextNumPointers < lastNumPointers) {
901 removeOldPointer(upOrDownPointer);
902 }
903
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700904 return me;
905 }
906
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700907 boolean hasMore() {
908 return mLastNumPointers != mNextNumPointers;
909 }
910
911 void finish() {
912 mNextNumPointers = mAddingPointerOffset = 0;
913 mNextData[MotionEvent.SAMPLE_PRESSURE] = 0;
914 }
915
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700916 MotionEvent generateRelMotion(InputDevice device, long curTime,
917 long curTimeNano, int orientation, int metaState) {
918
919 final float[] scaled = mReportData;
920
921 // For now we only support 1 pointer with relative motions.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700922 scaled[MotionEvent.SAMPLE_X] = mNextData[MotionEvent.SAMPLE_X];
923 scaled[MotionEvent.SAMPLE_Y] = mNextData[MotionEvent.SAMPLE_Y];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700924 scaled[MotionEvent.SAMPLE_PRESSURE] = 1.0f;
925 scaled[MotionEvent.SAMPLE_SIZE] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 int edgeFlags = 0;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700927
928 int action;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700929 if (mNextNumPointers != mLastNumPointers) {
930 mNextData[MotionEvent.SAMPLE_X] =
931 mNextData[MotionEvent.SAMPLE_Y] = 0;
932 if (mNextNumPointers > 0 && mLastNumPointers == 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700933 action = MotionEvent.ACTION_DOWN;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700934 mDownTime = curTime;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700935 } else if (mNextNumPointers == 0) {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700936 action = MotionEvent.ACTION_UP;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700937 } else {
938 action = MotionEvent.ACTION_MOVE;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700939 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700940 mLastNumPointers = mNextNumPointers;
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700941 currentMove = null;
942 } else {
943 action = MotionEvent.ACTION_MOVE;
944 }
945
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700946 scaled[MotionEvent.SAMPLE_X] *= xMoveScale;
947 scaled[MotionEvent.SAMPLE_Y] *= yMoveScale;
948 switch (orientation) {
949 case Surface.ROTATION_90: {
950 final float temp = scaled[MotionEvent.SAMPLE_X];
951 scaled[MotionEvent.SAMPLE_X] = scaled[MotionEvent.SAMPLE_Y];
952 scaled[MotionEvent.SAMPLE_Y] = -temp;
953 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700955 case Surface.ROTATION_180: {
956 scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_X];
957 scaled[MotionEvent.SAMPLE_Y] = -scaled[MotionEvent.SAMPLE_Y];
958 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700960 case Surface.ROTATION_270: {
961 final float temp = scaled[MotionEvent.SAMPLE_X];
962 scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_Y];
963 scaled[MotionEvent.SAMPLE_Y] = temp;
964 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
966 }
967
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700968 if (currentMove != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800969 if (false) Slog.i("InputDevice", "Adding batch x="
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700970 + scaled[MotionEvent.SAMPLE_X]
971 + " y=" + scaled[MotionEvent.SAMPLE_Y]
972 + " to " + currentMove);
973 currentMove.addBatch(curTime, scaled, metaState);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700974 if (WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800975 Slog.i("KeyInputQueue", "Updating: " + currentMove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700977 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700979
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700980 MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700981 curTimeNano, action, 1, mPointerIds, scaled, metaState,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700982 xPrecision, yPrecision, device.id, edgeFlags);
983 if (action == MotionEvent.ACTION_MOVE) {
984 currentMove = me;
985 }
986 return me;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988 }
989
990 static class AbsoluteInfo {
991 int minValue;
992 int maxValue;
993 int range;
994 int flat;
995 int fuzz;
Dianne Hackborna2e92262010-03-02 17:19:29 -0800996
997 final void dump(PrintWriter pw) {
998 pw.print("minValue="); pw.print(minValue);
999 pw.print(" maxValue="); pw.print(maxValue);
1000 pw.print(" range="); pw.print(range);
1001 pw.print(" flat="); pw.print(flat);
1002 pw.print(" fuzz="); pw.print(fuzz);
1003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 };
1005
1006 InputDevice(int _id, int _classes, String _name,
1007 AbsoluteInfo _absX, AbsoluteInfo _absY,
1008 AbsoluteInfo _absPressure, AbsoluteInfo _absSize) {
1009 id = _id;
1010 classes = _classes;
1011 name = _name;
1012 absX = _absX;
1013 absY = _absY;
1014 absPressure = _absPressure;
1015 absSize = _absSize;
1016 }
1017};