Input device protocol enhancements.

Added support for Linux multitouch protocol B (slots).

Added support for using the device's input properties as a hint
to determine the intended usage of a touch device.

Added support for the ABS_MT_DISTANCE axis.

Fixed a bug reporting the presence of the orientation axis.

Change-Id: Icf7b5a5a0f1a9cdf6ad2b35be8ea0c1a35815d48
diff --git a/services/input/InputReader.h b/services/input/InputReader.h
index 62ac4b2..85338b6 100644
--- a/services/input/InputReader.h
+++ b/services/input/InputReader.h
@@ -331,6 +331,7 @@
     String8 mName;
     uint32_t mSources;
     bool mIsExternal;
+    bool mDropUntilNextSync;
 
     typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
     int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
@@ -602,6 +603,7 @@
         int32_t toolMajor;
         int32_t toolMinor;
         int32_t orientation;
+        int32_t distance;
         bool isStylus;
 
         inline bool operator== (const PointerData& other) const {
@@ -613,7 +615,8 @@
                     && touchMinor == other.touchMinor
                     && toolMajor == other.toolMajor
                     && toolMinor == other.toolMinor
-                    && orientation == other.orientation;
+                    && orientation == other.orientation
+                    && distance == other.distance;
         }
         inline bool operator!= (const PointerData& other) const {
             return !(*this == other);
@@ -759,6 +762,17 @@
         };
 
         OrientationCalibration orientationCalibration;
+
+        // Distance
+        enum DistanceCalibration {
+            DISTANCE_CALIBRATION_DEFAULT,
+            DISTANCE_CALIBRATION_NONE,
+            DISTANCE_CALIBRATION_SCALED,
+        };
+
+        DistanceCalibration distanceCalibration;
+        bool haveDistanceScale;
+        float distanceScale;
     } mCalibration;
 
     // Raw axis information from the driver.
@@ -771,6 +785,9 @@
         RawAbsoluteAxisInfo toolMajor;
         RawAbsoluteAxisInfo toolMinor;
         RawAbsoluteAxisInfo orientation;
+        RawAbsoluteAxisInfo distance;
+        RawAbsoluteAxisInfo trackingId;
+        RawAbsoluteAxisInfo slot;
     } mRawAxes;
 
     // Current and previous touch sample data.
@@ -819,6 +836,8 @@
 
         float orientationScale;
 
+        float distanceScale;
+
         // Oriented motion ranges for input device info.
         struct OrientedRanges {
             InputDeviceInfo::MotionRange x;
@@ -840,6 +859,9 @@
 
             bool haveOrientation;
             InputDeviceInfo::MotionRange orientation;
+
+            bool haveDistance;
+            InputDeviceInfo::MotionRange distance;
         } orientedRanges;
 
         // Oriented dimensions and precision.
@@ -1146,7 +1168,7 @@
     int32_t mToolWidth;
     int32_t mButtonState;
 
-    void initialize();
+    void clearState();
 
     void sync(nsecs_t when);
 };
@@ -1166,20 +1188,21 @@
 private:
     struct Accumulator {
         enum {
-            FIELD_ABS_MT_POSITION_X = 1,
-            FIELD_ABS_MT_POSITION_Y = 2,
-            FIELD_ABS_MT_TOUCH_MAJOR = 4,
-            FIELD_ABS_MT_TOUCH_MINOR = 8,
-            FIELD_ABS_MT_WIDTH_MAJOR = 16,
-            FIELD_ABS_MT_WIDTH_MINOR = 32,
-            FIELD_ABS_MT_ORIENTATION = 64,
-            FIELD_ABS_MT_TRACKING_ID = 128,
-            FIELD_ABS_MT_PRESSURE = 256,
+            FIELD_ABS_MT_POSITION_X = 1 << 0,
+            FIELD_ABS_MT_POSITION_Y = 1 << 1,
+            FIELD_ABS_MT_TOUCH_MAJOR = 1 << 2,
+            FIELD_ABS_MT_TOUCH_MINOR = 1 << 3,
+            FIELD_ABS_MT_WIDTH_MAJOR = 1 << 4,
+            FIELD_ABS_MT_WIDTH_MINOR = 1 << 5,
+            FIELD_ABS_MT_ORIENTATION = 1 << 6,
+            FIELD_ABS_MT_TRACKING_ID = 1 << 7,
+            FIELD_ABS_MT_PRESSURE = 1 << 8,
+            FIELD_ABS_MT_TOOL_TYPE = 1 << 9,
+            FIELD_ABS_MT_DISTANCE = 1 << 10,
         };
 
-        uint32_t pointerCount;
-        struct Pointer {
-            uint32_t fields;
+        struct Slot {
+            uint32_t fields; // 0 if slot is unused
 
             int32_t absMTPositionX;
             int32_t absMTPositionY;
@@ -1190,27 +1213,56 @@
             int32_t absMTOrientation;
             int32_t absMTTrackingId;
             int32_t absMTPressure;
+            int32_t absMTToolType;
+            int32_t absMTDistance;
+
+            inline Slot() {
+                clear();
+            }
 
             inline void clear() {
                 fields = 0;
             }
-        } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
+        };
+
+        // Current slot index.
+        int32_t currentSlot;
+
+        // Array of slots.
+        Slot* slots;
 
         // Bitfield of buttons that went down or up.
         uint32_t buttonDown;
         uint32_t buttonUp;
 
-        inline void clear() {
-            pointerCount = 0;
-            pointers[0].clear();
+        Accumulator() : slots(NULL) {
+            clear(false);
+        }
+
+        ~Accumulator() {
+            delete[] slots;
+        }
+
+        void allocateSlots(size_t slotCount) {
+            slots = new Slot[slotCount];
+        }
+
+        void clear(size_t slotCount) {
+            for (size_t i = 0; i < slotCount; i++) {
+                slots[i].clear();
+            }
+            currentSlot = 0;
             buttonDown = 0;
             buttonUp = 0;
         }
     } mAccumulator;
 
+    size_t mSlotCount;
+    bool mUsingSlotsProtocol;
+
     int32_t mButtonState;
 
-    void initialize();
+    void clearState();
 
     void sync(nsecs_t when);
 };