MouseInputMapper

Maps relative movements of a mouse or scroll wheel and mouse button
presses. All other logic from CursorInputMapper will be handled in the
host.

Change-Id: I00a966a6194d4810b7b6ac5cc815287ecf7dfb84
diff --git a/modules/input/evdev/Android.mk b/modules/input/evdev/Android.mk
index 1f5e12f..9a5d092 100644
--- a/modules/input/evdev/Android.mk
+++ b/modules/input/evdev/Android.mk
@@ -24,6 +24,7 @@
     InputDeviceManager.cpp \
     InputHost.cpp \
     InputMapper.cpp \
+    MouseInputMapper.cpp \
     SwitchInputMapper.cpp
 
 LOCAL_SHARED_LIBRARIES := \
diff --git a/modules/input/evdev/InputDevice.cpp b/modules/input/evdev/InputDevice.cpp
index ea90068..b575117 100644
--- a/modules/input/evdev/InputDevice.cpp
+++ b/modules/input/evdev/InputDevice.cpp
@@ -34,6 +34,7 @@
 
 #include "InputHost.h"
 #include "InputHub.h"
+#include "MouseInputMapper.h"
 #include "SwitchInputMapper.h"
 
 #define MSC_ANDROID_TIME_SEC  0x6
@@ -138,7 +139,7 @@
             && mDeviceNode->hasRelativeAxis(REL_X)
             && mDeviceNode->hasRelativeAxis(REL_Y)) {
         mClasses |= INPUT_DEVICE_CLASS_CURSOR;
-        //mMappers.push_back(std::make_unique<CursorInputMapper>());
+        mMappers.push_back(std::make_unique<MouseInputMapper>());
     }
 
     bool isStylus = false;
diff --git a/modules/input/evdev/MouseInputMapper.cpp b/modules/input/evdev/MouseInputMapper.cpp
new file mode 100644
index 0000000..453edde
--- /dev/null
+++ b/modules/input/evdev/MouseInputMapper.cpp
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "MouseInputMapper"
+//#define LOG_NDEBUG 0
+
+#include "MouseInputMapper.h"
+
+#include <linux/input.h>
+#include <hardware/input.h>
+#include <utils/Log.h>
+#include <utils/misc.h>
+
+#include "InputHost.h"
+#include "InputHub.h"
+
+
+namespace android {
+
+// Map scancodes to input HAL usages.
+// The order of these definitions MUST remain in sync with the order they are
+// defined in linux/input.h.
+static struct {
+    int32_t scancode;
+    InputUsage usage;
+} codeMap[] = {
+    {BTN_LEFT, INPUT_USAGE_BUTTON_PRIMARY},
+    {BTN_RIGHT, INPUT_USAGE_BUTTON_SECONDARY},
+    {BTN_MIDDLE, INPUT_USAGE_BUTTON_TERTIARY},
+    {BTN_SIDE, INPUT_USAGE_BUTTON_UNKNOWN},
+    {BTN_EXTRA, INPUT_USAGE_BUTTON_UNKNOWN},
+    {BTN_FORWARD, INPUT_USAGE_BUTTON_FORWARD},
+    {BTN_BACK, INPUT_USAGE_BUTTON_BACK},
+    {BTN_TASK, INPUT_USAGE_BUTTON_UNKNOWN},
+};
+
+
+bool MouseInputMapper::configureInputReport(InputDeviceNode* devNode,
+        InputReportDefinition* report) {
+    setInputReportDefinition(report);
+    getInputReportDefinition()->addCollection(INPUT_COLLECTION_ID_MOUSE, 1);
+
+    // Configure mouse axes
+    if (!devNode->hasRelativeAxis(REL_X) || !devNode->hasRelativeAxis(REL_Y)) {
+        ALOGE("Device %s is missing a relative x or y axis. Device cannot be configured.",
+                devNode->getPath().c_str());
+        return false;
+    }
+    getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_X,
+            INT32_MIN, INT32_MAX, 1.0f);
+    getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_Y,
+            INT32_MIN, INT32_MAX, 1.0f);
+    if (devNode->hasRelativeAxis(REL_WHEEL)) {
+        getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE,
+                INPUT_USAGE_AXIS_VSCROLL, -1, 1, 0.0f);
+    }
+    if (devNode->hasRelativeAxis(REL_HWHEEL)) {
+        getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE,
+                INPUT_USAGE_AXIS_HSCROLL, -1, 1, 0.0f);
+    }
+
+    // Configure mouse buttons
+    InputUsage usages[NELEM(codeMap)];
+    int numUsages = 0;
+    for (int32_t i = 0; i < NELEM(codeMap); ++i) {
+        if (devNode->hasKey(codeMap[i].scancode)) {
+            usages[numUsages++] = codeMap[i].usage;
+        }
+    }
+    if (numUsages == 0) {
+        ALOGW("MouseInputMapper found no buttons for %s", devNode->getPath().c_str());
+    }
+    getInputReportDefinition()->declareUsages(INPUT_COLLECTION_ID_MOUSE, usages, numUsages);
+    return true;
+}
+
+void MouseInputMapper::process(const InputEvent& event) {
+    ALOGD("processing mouse event. type=%d code=%d value=%d",
+            event.type, event.code, event.value);
+    switch (event.type) {
+        case EV_KEY:
+            processButton(event.code, event.value);
+            break;
+        case EV_REL:
+            processMotion(event.code, event.value);
+            break;
+        case EV_SYN:
+            if (event.code == SYN_REPORT) {
+                sync(event.when);
+            }
+            break;
+        default:
+            ALOGD("unknown mouse event type: %d", event.type);
+    }
+}
+
+void MouseInputMapper::processMotion(int32_t code, int32_t value) {
+    switch (code) {
+        case REL_X:
+            mRelX = value;
+            break;
+        case REL_Y:
+            mRelY = value;
+            break;
+        case REL_WHEEL:
+            mRelWheel = value;
+            break;
+        case REL_HWHEEL:
+            mRelHWheel = value;
+            break;
+        default:
+            // Unknown code. Ignore.
+            break;
+    }
+}
+
+// Map evdev button codes to bit indices. This function assumes code >=
+// BTN_MOUSE.
+uint32_t buttonToBit(int32_t code) {
+    return static_cast<uint32_t>(code - BTN_MOUSE);
+}
+
+void MouseInputMapper::processButton(int32_t code, int32_t value) {
+    // Mouse buttons start at BTN_MOUSE and end before BTN_JOYSTICK. There isn't
+    // really enough room after the mouse buttons for another button class, so
+    // the risk of a button type being inserted after mouse is low.
+    if (code >= BTN_MOUSE && code < BTN_JOYSTICK) {
+        if (value) {
+            mButtonValues.markBit(buttonToBit(code));
+        } else {
+            mButtonValues.clearBit(buttonToBit(code));
+        }
+        mUpdatedButtonMask.markBit(buttonToBit(code));
+    }
+}
+
+void MouseInputMapper::sync(nsecs_t when) {
+    // Process updated button states.
+    while (!mUpdatedButtonMask.isEmpty()) {
+        auto bit = mUpdatedButtonMask.clearFirstMarkedBit();
+        getInputReport()->setBoolUsage(INPUT_COLLECTION_ID_MOUSE, codeMap[bit].usage,
+                mButtonValues.hasBit(bit), 0);
+    }
+
+    // Process motion and scroll changes.
+    if (mRelX != 0) {
+        getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_X, mRelX, 0);
+    }
+    if (mRelY != 0) {
+        getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_Y, mRelY, 0);
+    }
+    if (mRelWheel != 0) {
+        getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_VSCROLL,
+                mRelWheel, 0);
+    }
+    if (mRelHWheel != 0) {
+        getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_HSCROLL,
+                mRelHWheel, 0);
+    }
+
+    // Report and reset.
+    getInputReport()->reportEvent(getDeviceHandle());
+    mUpdatedButtonMask.clear();
+    mButtonValues.clear();
+    mRelX = 0;
+    mRelY = 0;
+    mRelWheel = 0;
+    mRelHWheel = 0;
+}
+
+}  // namespace android
diff --git a/modules/input/evdev/MouseInputMapper.h b/modules/input/evdev/MouseInputMapper.h
new file mode 100644
index 0000000..1f8bc06
--- /dev/null
+++ b/modules/input/evdev/MouseInputMapper.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_MOUSE_INPUT_MAPPER_H_
+#define ANDROID_MOUSE_INPUT_MAPPER_H_
+
+#include <cstdint>
+
+#include <utils/BitSet.h>
+#include <utils/Timers.h>
+
+#include "InputMapper.h"
+
+namespace android {
+
+class MouseInputMapper : public InputMapper {
+public:
+    virtual ~MouseInputMapper() = default;
+
+    virtual bool configureInputReport(InputDeviceNode* devNode,
+            InputReportDefinition* report) override;
+    virtual void process(const InputEvent& event) override;
+
+private:
+    void processMotion(int32_t code, int32_t value);
+    void processButton(int32_t code, int32_t value);
+    void sync(nsecs_t when);
+
+    BitSet32 mButtonValues;
+    BitSet32 mUpdatedButtonMask;
+
+    int32_t mRelX = 0;
+    int32_t mRelY = 0;
+
+    bool mHaveRelWheel = false;
+    bool mHaveRelHWheel = false;
+    int32_t mRelWheel = 0;
+    int32_t mRelHWheel = 0;
+};
+
+}  // namespace android
+
+#endif  // ANDROID_MOUSE_INPUT_MAPPER_H_
diff --git a/modules/input/evdev/SwitchInputMapper.h b/modules/input/evdev/SwitchInputMapper.h
index b3a1c05..e25c3a5 100644
--- a/modules/input/evdev/SwitchInputMapper.h
+++ b/modules/input/evdev/SwitchInputMapper.h
@@ -26,10 +26,6 @@
 
 namespace android {
 
-class InputDeviceNode;
-class InputReportDefinition;
-struct InputEvent;
-
 class SwitchInputMapper : public InputMapper {
 public:
     SwitchInputMapper();