Add initial API for stylus and mouse buttons.

Added the concept of pointer properties in a MotionEvent.
This is currently used to track the pointer tool type to enable
applications to distinguish finger touches from a stylus.

Button states are also reported to application as part of touch events.

There are no new actions for detecting changes in button states.
The application should instead query the button state from the
MotionEvent and take appropriate action as needed.

A good time to check the button state is on ACTION_DOWN.

As a side-effect, applications that do not support multiple buttons
will treat primary, secondary and tertiary buttons identically
for all touch events.

The back button on the mouse is mapped to KEYCODE_BACK
and the forward button is mapped to KEYCODE_FORWARD.

Added basic plumbing for the secondary mouse button to invoke
the context menu, particularly in lists.

Added clamp and split methods on MotionEvent to take care of
common filtering operations so we don't have them scattered
in multiple places across the framework.

Bug: 4260011
Change-Id: Ie992b4d4e00c8f2e76b961da0a902145b27f6d83
diff --git a/native/include/android/input.h b/native/include/android/input.h
index f1738c6..26cac50 100644
--- a/native/include/android/input.h
+++ b/native/include/android/input.h
@@ -372,6 +372,7 @@
     AMOTION_EVENT_AXIS_WHEEL = 21,
     AMOTION_EVENT_AXIS_GAS = 22,
     AMOTION_EVENT_AXIS_BRAKE = 23,
+    AMOTION_EVENT_AXIS_DISTANCE = 24,
     AMOTION_EVENT_AXIS_GENERIC_1 = 32,
     AMOTION_EVENT_AXIS_GENERIC_2 = 33,
     AMOTION_EVENT_AXIS_GENERIC_3 = 34,
@@ -394,6 +395,32 @@
 };
 
 /*
+ * Constants that identify buttons that are associated with motion events.
+ * Refer to the documentation on the MotionEvent class for descriptions of each button.
+ */
+enum {
+    AMOTION_EVENT_BUTTON_PRIMARY = 1 << 0,
+    AMOTION_EVENT_BUTTON_SECONDARY = 1 << 1,
+    AMOTION_EVENT_BUTTON_TERTIARY = 1 << 2,
+    AMOTION_EVENT_BUTTON_BACK = 1 << 3,
+    AMOTION_EVENT_BUTTON_FORWARD = 1 << 4,
+    AMOTION_EVENT_BUTTON_ERASER = 1 << 5,
+};
+
+/*
+ * Constants that identify tool types.
+ * Refer to the documentation on the MotionEvent class for descriptions of each tool type.
+ */
+enum {
+    AMOTION_EVENT_TOOL_TYPE_UNKNOWN = 0,
+    AMOTION_EVENT_TOOL_TYPE_FINGER = 1,
+    AMOTION_EVENT_TOOL_TYPE_STYLUS = 2,
+    AMOTION_EVENT_TOOL_TYPE_MOUSE = 3,
+    AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER = 4,
+    AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS = 5,
+};
+
+/*
  * Input sources.
  *
  * Refer to the documentation on android.view.InputDevice for more details about input sources
@@ -417,6 +444,7 @@
     AINPUT_SOURCE_GAMEPAD = 0x00000400 | AINPUT_SOURCE_CLASS_BUTTON,
     AINPUT_SOURCE_TOUCHSCREEN = 0x00001000 | AINPUT_SOURCE_CLASS_POINTER,
     AINPUT_SOURCE_MOUSE = 0x00002000 | AINPUT_SOURCE_CLASS_POINTER,
+    AINPUT_SOURCE_STYLUS = 0x00004000 | AINPUT_SOURCE_CLASS_POINTER,
     AINPUT_SOURCE_TRACKBALL = 0x00010000 | AINPUT_SOURCE_CLASS_NAVIGATION,
     AINPUT_SOURCE_TOUCHPAD = 0x00100000 | AINPUT_SOURCE_CLASS_POSITION,
     AINPUT_SOURCE_JOYSTICK = 0x01000000 | AINPUT_SOURCE_CLASS_JOYSTICK,
@@ -532,6 +560,9 @@
  * event was generated. */
 int32_t AMotionEvent_getMetaState(const AInputEvent* motion_event);
 
+/* Get the button state of all buttons that are pressed. */
+int32_t AMotionEvent_getButtonState(const AInputEvent* motion_event);
+
 /* Get a bitfield indicating which edges, if any, were touched by this motion event.
  * For touch events, clients can use this to determine if the user's finger was
  * touching the edge of the display. */
@@ -572,11 +603,16 @@
 size_t AMotionEvent_getPointerCount(const AInputEvent* motion_event);
 
 /* Get the pointer identifier associated with a particular pointer
- * data index is this event.  The identifier tells you the actual pointer
+ * data index in this event.  The identifier tells you the actual pointer
  * number associated with the data, accounting for individual pointers
  * going up and down since the start of the current gesture. */
 int32_t AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index);
 
+/* Get the tool type of a pointer for the given pointer index.
+ * The tool type indicates the type of tool used to make contact such as a
+ * finger or stylus, if known. */
+int32_t AMotionEvent_getToolType(const AInputEvent* motion_event, size_t pointer_index);
+
 /* Get the original raw X coordinate of this event.
  * For touch events on the screen, this is the original location of the event
  * on the screen, before it had been adjusted for the containing window