First stab at poly-finger support.

The MotionEvent API should be fairly solid, but there is still a lot of
work to do in the input device code.  In particular, right now we are
really stupid about watching how fingers change -- we just take whatever
the driver reports as down and dump that directly into the motion event.

The big remaning work is to assign pointer IDs so that applications have
help in determine which fingers go up and down, and adding support for
the official multi-touch driver protocol.
diff --git a/services/java/com/android/server/InputDevice.java b/services/java/com/android/server/InputDevice.java
index a71c39a..3e98132 100644
--- a/services/java/com/android/server/InputDevice.java
+++ b/services/java/com/android/server/InputDevice.java
@@ -26,6 +26,9 @@
     /** Amount that trackball needs to move in order to generate a key event. */
     static final int TRACKBALL_MOVEMENT_THRESHOLD = 6;
 
+    /** Maximum number of pointers we will track and report. */
+    static final int MAX_POINTERS = 2;
+    
     final int id;
     final int classes;
     final String name;
@@ -34,7 +37,7 @@
     final AbsoluteInfo absPressure;
     final AbsoluteInfo absSize;
     
-    long mDownTime = 0;
+    long mKeyDownTime = 0;
     int mMetaKeysState = 0;
     
     final MotionState mAbs = new MotionState(0, 0);
@@ -48,13 +51,13 @@
         float yMoveScale;
         MotionEvent currentMove = null;
         boolean changed = false;
-        boolean down = false;
-        boolean lastDown = false;
-        long downTime = 0;
-        int x = 0;
-        int y = 0;
-        int pressure = 1;
-        int size = 0;
+        boolean mLastAnyDown = false;
+        long mDownTime = 0;
+        final boolean[] mLastDown = new boolean[MAX_POINTERS];
+        final boolean[] mDown = new boolean[MAX_POINTERS];
+        final int[] mLastData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
+        final int[] mCurData = new int[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
+        final float[] mReportData = new float[MotionEvent.NUM_SAMPLE_DATA * MAX_POINTERS];
         
         MotionState(int mx, int my) {
             xPrecision = mx;
@@ -63,46 +66,207 @@
             yMoveScale = my != 0 ? (1.0f/my) : 1.0f;
         }
         
-        MotionEvent generateMotion(InputDevice device, long curTime, long curTimeNano,
-                boolean isAbs, Display display, int orientation,
+        MotionEvent generateAbsMotion(InputDevice device, long curTime,
+                long curTimeNano, Display display, int orientation,
                 int metaState) {
-            float scaledX = x;
-            float scaledY = y;
-            float temp;
-            float scaledPressure = 1.0f;
-            float scaledSize = 0;
+            
+            final float[] scaled = mReportData;
+            final int[] cur = mCurData;
+            
+            boolean anyDown = false;
+            int firstDownChanged = -1;
+            int numPointers = 0;
+            for (int i=0; i<MAX_POINTERS; i++) {
+                boolean d = mDown[i];
+                anyDown |= d;
+                if (d != mLastDown[i] && firstDownChanged < 0) {
+                    firstDownChanged = i;
+                    mLastDown[i] = mDown[i];
+                    d = true;
+                }
+                
+                if (d) {
+                    final int src = i * MotionEvent.NUM_SAMPLE_DATA;
+                    final int dest = numPointers * MotionEvent.NUM_SAMPLE_DATA;
+                    numPointers++;
+                    scaled[dest + MotionEvent.SAMPLE_X] = cur[src + MotionEvent.SAMPLE_X];
+                    scaled[dest + MotionEvent.SAMPLE_Y] = cur[src + MotionEvent.SAMPLE_Y];
+                    scaled[dest + MotionEvent.SAMPLE_PRESSURE] = cur[src + MotionEvent.SAMPLE_PRESSURE];
+                    scaled[dest + MotionEvent.SAMPLE_SIZE] = cur[src + MotionEvent.SAMPLE_SIZE];
+                }
+            }
+            
+            if (numPointers <= 0) {
+                return null;
+            }
+            
+            int action;
+            int edgeFlags = 0;
+            if (anyDown != mLastAnyDown) {
+                final AbsoluteInfo absX = device.absX;
+                final AbsoluteInfo absY = device.absY;
+                if (anyDown && absX != null && absY != null) {
+                    // We don't let downs start unless we are
+                    // inside of the screen.  There are two reasons for
+                    // this: to avoid spurious touches when holding
+                    // the edges of the device near the touchscreen,
+                    // and to avoid reporting events if there are virtual
+                    // keys on the touchscreen outside of the display
+                    // area.
+                    // Note that we are only looking at the first pointer,
+                    // since what we are handling here is the first pointer
+                    // going down, and this is the coordinate that will be
+                    // used to dispatch the event.
+                    if (cur[MotionEvent.SAMPLE_X] < absX.minValue
+                            || cur[MotionEvent.SAMPLE_X] > absX.maxValue
+                            || cur[MotionEvent.SAMPLE_Y] < absY.minValue
+                            || cur[MotionEvent.SAMPLE_Y] > absY.maxValue) {
+                        if (false) Log.v("InputDevice", "Rejecting ("
+                                + cur[MotionEvent.SAMPLE_X] + ","
+                                + cur[MotionEvent.SAMPLE_Y] + "): outside of ("
+                                + absX.minValue + "," + absY.minValue
+                                + ")-(" + absX.maxValue + ","
+                                + absY.maxValue + ")");
+                        return null;
+                    }
+                }
+                mLastAnyDown = anyDown;
+                if (anyDown) {
+                    action = MotionEvent.ACTION_DOWN;
+                    mDownTime = curTime;
+                } else {
+                    action = MotionEvent.ACTION_UP;
+                }
+                currentMove = null;
+            } else if (firstDownChanged >= 0) {
+                if (mDown[firstDownChanged]) {
+                    action = MotionEvent.ACTION_POINTER_DOWN
+                            | (firstDownChanged << MotionEvent.ACTION_POINTER_SHIFT);
+                } else {
+                    action = MotionEvent.ACTION_POINTER_UP
+                            | (firstDownChanged << MotionEvent.ACTION_POINTER_SHIFT);
+                }
+                currentMove = null;
+            } else {
+                action = MotionEvent.ACTION_MOVE;
+            }
+            
+            final int dispW = display.getWidth()-1;
+            final int dispH = display.getHeight()-1;
+            int w = dispW;
+            int h = dispH;
+            if (orientation == Surface.ROTATION_90
+                    || orientation == Surface.ROTATION_270) {
+                int tmp = w;
+                w = h;
+                h = tmp;
+            }
+            
+            final AbsoluteInfo absX = device.absX;
+            final AbsoluteInfo absY = device.absY;
+            final AbsoluteInfo absPressure = device.absPressure;
+            final AbsoluteInfo absSize = device.absSize;
+            for (int i=0; i<numPointers; i++) {
+                final int j = i * MotionEvent.NUM_SAMPLE_DATA;
+            
+                if (absX != null) {
+                    scaled[j + MotionEvent.SAMPLE_X] =
+                            ((scaled[j + MotionEvent.SAMPLE_X]-absX.minValue)
+                                / absX.range) * w;
+                }
+                if (absY != null) {
+                    scaled[j + MotionEvent.SAMPLE_Y] =
+                            ((scaled[j + MotionEvent.SAMPLE_Y]-absY.minValue)
+                                / absY.range) * h;
+                }
+                if (absPressure != null) {
+                    scaled[j + MotionEvent.SAMPLE_PRESSURE] = 
+                            ((scaled[j + MotionEvent.SAMPLE_PRESSURE]-absPressure.minValue)
+                                / (float)absPressure.range);
+                }
+                if (absSize != null) {
+                    scaled[j + MotionEvent.SAMPLE_SIZE] = 
+                            ((scaled[j + MotionEvent.SAMPLE_SIZE]-absSize.minValue)
+                                / (float)absSize.range);
+                }
+                
+                switch (orientation) {
+                    case Surface.ROTATION_90: {
+                        final float temp = scaled[MotionEvent.SAMPLE_X];
+                        scaled[j + MotionEvent.SAMPLE_X] = scaled[j + MotionEvent.SAMPLE_Y];
+                        scaled[j + MotionEvent.SAMPLE_Y] = w-temp;
+                        break;
+                    }
+                    case Surface.ROTATION_180: {
+                        scaled[j + MotionEvent.SAMPLE_X] = w-scaled[j + MotionEvent.SAMPLE_X];
+                        scaled[j + MotionEvent.SAMPLE_Y] = h-scaled[j + MotionEvent.SAMPLE_Y];
+                        break;
+                    }
+                    case Surface.ROTATION_270: {
+                        final float temp = scaled[i + MotionEvent.SAMPLE_X];
+                        scaled[j + MotionEvent.SAMPLE_X] = h-scaled[j + MotionEvent.SAMPLE_Y];
+                        scaled[j + MotionEvent.SAMPLE_Y] = temp;
+                        break;
+                    }
+                }
+            }
+            
+            // We only consider the first pointer when computing the edge
+            // flags, since they are global to the event.
+            if (action != MotionEvent.ACTION_DOWN) {
+                if (scaled[MotionEvent.SAMPLE_X] <= 0) {
+                    edgeFlags |= MotionEvent.EDGE_LEFT;
+                } else if (scaled[MotionEvent.SAMPLE_X] >= dispW) {
+                    edgeFlags |= MotionEvent.EDGE_RIGHT;
+                }
+                if (scaled[MotionEvent.SAMPLE_Y] <= 0) {
+                    edgeFlags |= MotionEvent.EDGE_TOP;
+                } else if (scaled[MotionEvent.SAMPLE_Y] >= dispH) {
+                    edgeFlags |= MotionEvent.EDGE_BOTTOM;
+                }
+            }
+            
+            if (currentMove != null) {
+                if (false) Log.i("InputDevice", "Adding batch x="
+                        + scaled[MotionEvent.SAMPLE_X]
+                        + " y=" + scaled[MotionEvent.SAMPLE_Y]
+                        + " to " + currentMove);
+                currentMove.addBatch(curTime, scaled, metaState);
+                if (WindowManagerPolicy.WATCH_POINTER) {
+                    Log.i("KeyInputQueue", "Updating: " + currentMove);
+                }
+                return null;
+            }
+            
+            MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
+                    curTimeNano, action, numPointers, scaled, metaState,
+                    xPrecision, yPrecision, device.id, edgeFlags);
+            if (action == MotionEvent.ACTION_MOVE) {
+                currentMove = me;
+            }
+            return me;
+        }
+        
+        MotionEvent generateRelMotion(InputDevice device, long curTime,
+                long curTimeNano, int orientation, int metaState) {
+            
+            final float[] scaled = mReportData;
+            
+            // For now we only support 1 pointer with relative motions.
+            scaled[MotionEvent.SAMPLE_X] = mCurData[MotionEvent.SAMPLE_X];
+            scaled[MotionEvent.SAMPLE_Y] = mCurData[MotionEvent.SAMPLE_Y];
+            scaled[MotionEvent.SAMPLE_PRESSURE] = 1.0f;
+            scaled[MotionEvent.SAMPLE_SIZE] = 0;
             int edgeFlags = 0;
             
             int action;
-            if (down != lastDown) {
-                if (isAbs) {
-                    final AbsoluteInfo absX = device.absX;
-                    final AbsoluteInfo absY = device.absY;
-                    if (down && absX != null && absY != null) {
-                        // We don't let downs start unless we are
-                        // inside of the screen.  There are two reasons for
-                        // this: to avoid spurious touches when holding
-                        // the edges of the device near the touchscreen,
-                        // and to avoid reporting events if there are virtual
-                        // keys on the touchscreen outside of the display
-                        // area.
-                        if (scaledX < absX.minValue || scaledX > absX.maxValue
-                                || scaledY < absY.minValue || scaledY > absY.maxValue) {
-                            if (false) Log.v("InputDevice", "Rejecting (" + scaledX + ","
-                                    + scaledY + "): outside of ("
-                                    + absX.minValue + "," + absY.minValue
-                                    + ")-(" + absX.maxValue + ","
-                                    + absY.maxValue + ")");
-                            return null;
-                        }
-                    }
-                } else {
-                    x = y = 0;
-                }
-                lastDown = down;
-                if (down) {
+            if (mDown[0] != mLastDown[0]) {
+                mCurData[MotionEvent.SAMPLE_X] =
+                        mCurData[MotionEvent.SAMPLE_Y] = 0;
+                mLastDown[0] = mDown[0];
+                if (mDown[0]) {
                     action = MotionEvent.ACTION_DOWN;
-                    downTime = curTime;
+                    mDownTime = curTime;
                 } else {
                     action = MotionEvent.ACTION_UP;
                 }
@@ -111,100 +275,42 @@
                 action = MotionEvent.ACTION_MOVE;
             }
             
-            if (isAbs) {
-                final int dispW = display.getWidth()-1;
-                final int dispH = display.getHeight()-1;
-                int w = dispW;
-                int h = dispH;
-                if (orientation == Surface.ROTATION_90
-                        || orientation == Surface.ROTATION_270) {
-                    int tmp = w;
-                    w = h;
-                    h = tmp;
+            scaled[MotionEvent.SAMPLE_X] *= xMoveScale;
+            scaled[MotionEvent.SAMPLE_Y] *= yMoveScale;
+            switch (orientation) {
+                case Surface.ROTATION_90: {
+                    final float temp = scaled[MotionEvent.SAMPLE_X];
+                    scaled[MotionEvent.SAMPLE_X] = scaled[MotionEvent.SAMPLE_Y];
+                    scaled[MotionEvent.SAMPLE_Y] = -temp;
+                    break;
                 }
-                if (device.absX != null) {
-                    scaledX = ((scaledX-device.absX.minValue)
-                                / device.absX.range) * w;
+                case Surface.ROTATION_180: {
+                    scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_X];
+                    scaled[MotionEvent.SAMPLE_Y] = -scaled[MotionEvent.SAMPLE_Y];
+                    break;
                 }
-                if (device.absY != null) {
-                    scaledY = ((scaledY-device.absY.minValue)
-                                / device.absY.range) * h;
-                }
-                if (device.absPressure != null) {
-                    scaledPressure = 
-                        ((pressure-device.absPressure.minValue)
-                                / (float)device.absPressure.range);
-                }
-                if (device.absSize != null) {
-                    scaledSize = 
-                        ((size-device.absSize.minValue)
-                                / (float)device.absSize.range);
-                }
-                switch (orientation) {
-                    case Surface.ROTATION_90:
-                        temp = scaledX;
-                        scaledX = scaledY;
-                        scaledY = w-temp;
-                        break;
-                    case Surface.ROTATION_180:
-                        scaledX = w-scaledX;
-                        scaledY = h-scaledY;
-                        break;
-                    case Surface.ROTATION_270:
-                        temp = scaledX;
-                        scaledX = h-scaledY;
-                        scaledY = temp;
-                        break;
-                }
-
-                if (action != MotionEvent.ACTION_DOWN) {
-                    if (scaledX <= 0) {
-                        edgeFlags += MotionEvent.EDGE_LEFT;
-                    } else if (scaledX >= dispW) {
-                        edgeFlags += MotionEvent.EDGE_RIGHT;
-                    }
-                    if (scaledY <= 0) {
-                        edgeFlags += MotionEvent.EDGE_TOP;
-                    } else if (scaledY >= dispH) {
-                        edgeFlags += MotionEvent.EDGE_BOTTOM;
-                    }
-                }
-                
-            } else {
-                scaledX *= xMoveScale;
-                scaledY *= yMoveScale;
-                switch (orientation) {
-                    case Surface.ROTATION_90:
-                        temp = scaledX;
-                        scaledX = scaledY;
-                        scaledY = -temp;
-                        break;
-                    case Surface.ROTATION_180:
-                        scaledX = -scaledX;
-                        scaledY = -scaledY;
-                        break;
-                    case Surface.ROTATION_270:
-                        temp = scaledX;
-                        scaledX = -scaledY;
-                        scaledY = temp;
-                        break;
+                case Surface.ROTATION_270: {
+                    final float temp = scaled[MotionEvent.SAMPLE_X];
+                    scaled[MotionEvent.SAMPLE_X] = -scaled[MotionEvent.SAMPLE_Y];
+                    scaled[MotionEvent.SAMPLE_Y] = temp;
+                    break;
                 }
             }
             
             if (currentMove != null) {
-                if (false) Log.i("InputDevice", "Adding batch x=" + scaledX
-                        + " y=" + scaledY + " to " + currentMove);
-                currentMove.addBatch(curTime, scaledX, scaledY,
-                        scaledPressure, scaledSize, metaState);
+                if (false) Log.i("InputDevice", "Adding batch x="
+                        + scaled[MotionEvent.SAMPLE_X]
+                        + " y=" + scaled[MotionEvent.SAMPLE_Y]
+                        + " to " + currentMove);
+                currentMove.addBatch(curTime, scaled, metaState);
                 if (WindowManagerPolicy.WATCH_POINTER) {
                     Log.i("KeyInputQueue", "Updating: " + currentMove);
                 }
                 return null;
             }
             
-            MotionEvent me = MotionEvent.obtainNano(downTime, curTime,
-                    curTimeNano, action, scaledX, scaledY,
-                    scaledPressure, scaledSize, metaState,
+            MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,
+                    curTimeNano, action, 1, scaled, metaState,
                     xPrecision, yPrecision, device.id, edgeFlags);
             if (action == MotionEvent.ACTION_MOVE) {
                 currentMove = me;