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
This commit is contained in:
Tim Kilbourn 2015-05-19 17:15:52 -07:00
parent d780659bde
commit 864984c3b1
10 changed files with 386 additions and 13 deletions

View file

@ -318,6 +318,12 @@ typedef enum {
// axes
INPUT_USAGE_AXIS_X,
INPUT_USAGE_AXIS_Y,
INPUT_USAGE_AXIS_Z,
INPUT_USAGE_AXIS_RX,
INPUT_USAGE_AXIS_RY,
INPUT_USAGE_AXIS_RZ,
INPUT_USAGE_AXIS_HAT_X,
INPUT_USAGE_AXIS_HAT_Y,
INPUT_USAGE_AXIS_PRESSURE,
INPUT_USAGE_AXIS_SIZE,
INPUT_USAGE_AXIS_TOUCH_MAJOR,
@ -327,12 +333,6 @@ typedef enum {
INPUT_USAGE_AXIS_ORIENTATION,
INPUT_USAGE_AXIS_VSCROLL,
INPUT_USAGE_AXIS_HSCROLL,
INPUT_USAGE_AXIS_Z,
INPUT_USAGE_AXIS_RX,
INPUT_USAGE_AXIS_RY,
INPUT_USAGE_AXIS_RZ,
INPUT_USAGE_AXIS_HAT_X,
INPUT_USAGE_AXIS_HAT_Y,
INPUT_USAGE_AXIS_LTRIGGER,
INPUT_USAGE_AXIS_RTRIGGER,
INPUT_USAGE_AXIS_THROTTLE,
@ -384,6 +384,15 @@ typedef enum {
INPUT_USAGE_SWITCH_MICROPHONE_INSERT,
INPUT_USAGE_SWITCH_LINEOUT_INSERT,
INPUT_USAGE_SWITCH_CAMERA_LENS_COVER,
// mouse buttons
// (see android.view.MotionEvent)
INPUT_USAGE_BUTTON_UNKNOWN,
INPUT_USAGE_BUTTON_PRIMARY, // left
INPUT_USAGE_BUTTON_SECONDARY, // right
INPUT_USAGE_BUTTON_TERTIARY, // middle
INPUT_USAGE_BUTTON_FORWARD,
INPUT_USAGE_BUTTON_BACK,
} input_usage_t;
typedef enum input_collection_id {

View file

@ -24,6 +24,7 @@ LOCAL_SRC_FILES := \
InputDeviceManager.cpp \
InputHost.cpp \
InputMapper.cpp \
MouseInputMapper.cpp \
SwitchInputMapper.cpp
LOCAL_SHARED_LIBRARIES := \

View file

@ -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 @@ void EvdevDevice::createMappers() {
&& 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;

View file

@ -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

View file

@ -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_

View file

@ -26,10 +26,6 @@
namespace android {
class InputDeviceNode;
class InputReportDefinition;
struct InputEvent;
class SwitchInputMapper : public InputMapper {
public:
SwitchInputMapper();

View file

@ -9,6 +9,7 @@ LOCAL_SRC_FILES:= \
InputDevice_test.cpp \
InputHub_test.cpp \
InputMocks.cpp \
MouseInputMapper_test.cpp \
SwitchInputMapper_test.cpp \
TestHelpers.cpp

View file

@ -146,6 +146,8 @@ TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
// only the SwitchInputMapper has been implemented.
// TODO: move this expectation out to a common function
EXPECT_CALL(mHost, createInputReportDefinition());
EXPECT_CALL(mHost, createOutputReportDefinition());
EXPECT_CALL(mHost, freeReportDefinition(_));
EXPECT_CALL(mHost, registerDevice(_, _));
auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getHeadsetJack());

View file

@ -0,0 +1,123 @@
/*
* 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.
*/
#include <memory>
#include <linux/input.h>
#include <gtest/gtest.h>
#include "InputMocks.h"
#include "MockInputHost.h"
#include "MouseInputMapper.h"
using ::testing::_;
using ::testing::Args;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
namespace android {
namespace tests {
class MouseInputMapperTest : public ::testing::Test {
protected:
virtual void SetUp() override {
mMapper = std::make_unique<MouseInputMapper>();
}
MockInputHost mHost;
std::unique_ptr<MouseInputMapper> mMapper;
};
TEST_F(MouseInputMapperTest, testConfigureDevice) {
MockInputReportDefinition reportDef;
MockInputDeviceNode deviceNode;
deviceNode.addKeys(BTN_LEFT, BTN_RIGHT, BTN_MIDDLE);
deviceNode.addRelAxis(REL_X);
deviceNode.addRelAxis(REL_Y);
const auto id = INPUT_COLLECTION_ID_MOUSE;
EXPECT_CALL(reportDef, addCollection(id, 1));
EXPECT_CALL(reportDef, declareUsage(id, INPUT_USAGE_AXIS_X, _, _, _));
EXPECT_CALL(reportDef, declareUsage(id, INPUT_USAGE_AXIS_Y, _, _, _));
EXPECT_CALL(reportDef, declareUsages(id, _, 3))
.With(Args<1,2>(UnorderedElementsAre(
INPUT_USAGE_BUTTON_PRIMARY,
INPUT_USAGE_BUTTON_SECONDARY,
INPUT_USAGE_BUTTON_TERTIARY)));
EXPECT_TRUE(mMapper->configureInputReport(&deviceNode, &reportDef));
}
TEST_F(MouseInputMapperTest, testConfigureDevice_noXAxis) {
MockInputReportDefinition reportDef;
MockInputDeviceNode deviceNode;
EXPECT_CALL(reportDef, addCollection(INPUT_COLLECTION_ID_MOUSE, 1));
EXPECT_CALL(reportDef, declareUsage(_, _, _, _, _)).Times(0);
EXPECT_CALL(reportDef, declareUsages(_, _, _)).Times(0);
EXPECT_FALSE(mMapper->configureInputReport(&deviceNode, &reportDef));
}
TEST_F(MouseInputMapperTest, testProcessInput) {
MockInputReportDefinition reportDef;
MockInputDeviceNode deviceNode;
deviceNode.addKeys(BTN_LEFT, BTN_RIGHT, BTN_MIDDLE);
deviceNode.addRelAxis(REL_X);
deviceNode.addRelAxis(REL_Y);
EXPECT_CALL(reportDef, addCollection(_, _));
EXPECT_CALL(reportDef, declareUsage(_, _, _, _, _)).Times(2);
EXPECT_CALL(reportDef, declareUsages(_, _, 3));
mMapper->configureInputReport(&deviceNode, &reportDef);
MockInputReport report;
EXPECT_CALL(reportDef, allocateReport())
.WillOnce(Return(&report));
{
// Test two switch events in order
InSequence s;
const auto id = INPUT_COLLECTION_ID_MOUSE;
EXPECT_CALL(report, setIntUsage(id, INPUT_USAGE_AXIS_X, 5, 0));
EXPECT_CALL(report, setIntUsage(id, INPUT_USAGE_AXIS_Y, -3, 0));
EXPECT_CALL(report, reportEvent(_));
EXPECT_CALL(report, setBoolUsage(id, INPUT_USAGE_BUTTON_PRIMARY, 1, 0));
EXPECT_CALL(report, reportEvent(_));
EXPECT_CALL(report, setBoolUsage(id, INPUT_USAGE_BUTTON_PRIMARY, 0, 0));
EXPECT_CALL(report, reportEvent(_));
}
InputEvent events[] = {
{0, EV_REL, REL_X, 5},
{1, EV_REL, REL_Y, -3},
{2, EV_SYN, SYN_REPORT, 0},
{0, EV_KEY, BTN_LEFT, 1},
{1, EV_SYN, SYN_REPORT, 0},
{2, EV_KEY, BTN_LEFT, 0},
{3, EV_SYN, SYN_REPORT, 0},
};
for (auto e : events) {
mMapper->process(e);
}
}
} // namespace tests
} // namespace android

View file

@ -54,7 +54,7 @@ TEST_F(SwitchInputMapperTest, testConfigureDevice) {
.With(Args<1,2>(UnorderedElementsAre(INPUT_USAGE_SWITCH_LID,
INPUT_USAGE_SWITCH_CAMERA_LENS_COVER)));
mMapper->configureInputReport(&deviceNode, &reportDef);
EXPECT_TRUE(mMapper->configureInputReport(&deviceNode, &reportDef));
}
TEST_F(SwitchInputMapperTest, testConfigureDevice_noSwitches) {
@ -64,7 +64,7 @@ TEST_F(SwitchInputMapperTest, testConfigureDevice_noSwitches) {
EXPECT_CALL(reportDef, addCollection(_, _)).Times(0);
EXPECT_CALL(reportDef, declareUsages(_, _, _)).Times(0);
mMapper->configureInputReport(&deviceNode, &reportDef);
EXPECT_FALSE(mMapper->configureInputReport(&deviceNode, &reportDef));
}
TEST_F(SwitchInputMapperTest, testProcessInput) {