xref: /aosp_15_r20/hardware/libhardware/modules/input/evdev/MouseInputMapper.cpp (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #define LOG_TAG "MouseInputMapper"
18*e01b6f76SAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
19*e01b6f76SAndroid Build Coastguard Worker 
20*e01b6f76SAndroid Build Coastguard Worker #include "MouseInputMapper.h"
21*e01b6f76SAndroid Build Coastguard Worker 
22*e01b6f76SAndroid Build Coastguard Worker #include <linux/input.h>
23*e01b6f76SAndroid Build Coastguard Worker #include <hardware/input.h>
24*e01b6f76SAndroid Build Coastguard Worker #include <utils/Log.h>
25*e01b6f76SAndroid Build Coastguard Worker #include <utils/misc.h>
26*e01b6f76SAndroid Build Coastguard Worker 
27*e01b6f76SAndroid Build Coastguard Worker #include "InputHost.h"
28*e01b6f76SAndroid Build Coastguard Worker #include "InputHub.h"
29*e01b6f76SAndroid Build Coastguard Worker 
30*e01b6f76SAndroid Build Coastguard Worker 
31*e01b6f76SAndroid Build Coastguard Worker namespace android {
32*e01b6f76SAndroid Build Coastguard Worker 
33*e01b6f76SAndroid Build Coastguard Worker // Map scancodes to input HAL usages.
34*e01b6f76SAndroid Build Coastguard Worker // The order of these definitions MUST remain in sync with the order they are
35*e01b6f76SAndroid Build Coastguard Worker // defined in linux/input.h.
36*e01b6f76SAndroid Build Coastguard Worker static struct {
37*e01b6f76SAndroid Build Coastguard Worker     int32_t scancode;
38*e01b6f76SAndroid Build Coastguard Worker     InputUsage usage;
39*e01b6f76SAndroid Build Coastguard Worker } codeMap[] = {
40*e01b6f76SAndroid Build Coastguard Worker     {BTN_LEFT, INPUT_USAGE_BUTTON_PRIMARY},
41*e01b6f76SAndroid Build Coastguard Worker     {BTN_RIGHT, INPUT_USAGE_BUTTON_SECONDARY},
42*e01b6f76SAndroid Build Coastguard Worker     {BTN_MIDDLE, INPUT_USAGE_BUTTON_TERTIARY},
43*e01b6f76SAndroid Build Coastguard Worker     {BTN_SIDE, INPUT_USAGE_BUTTON_UNKNOWN},
44*e01b6f76SAndroid Build Coastguard Worker     {BTN_EXTRA, INPUT_USAGE_BUTTON_UNKNOWN},
45*e01b6f76SAndroid Build Coastguard Worker     {BTN_FORWARD, INPUT_USAGE_BUTTON_FORWARD},
46*e01b6f76SAndroid Build Coastguard Worker     {BTN_BACK, INPUT_USAGE_BUTTON_BACK},
47*e01b6f76SAndroid Build Coastguard Worker     {BTN_TASK, INPUT_USAGE_BUTTON_UNKNOWN},
48*e01b6f76SAndroid Build Coastguard Worker };
49*e01b6f76SAndroid Build Coastguard Worker 
50*e01b6f76SAndroid Build Coastguard Worker 
configureInputReport(InputDeviceNode * devNode,InputReportDefinition * report)51*e01b6f76SAndroid Build Coastguard Worker bool MouseInputMapper::configureInputReport(InputDeviceNode* devNode,
52*e01b6f76SAndroid Build Coastguard Worker         InputReportDefinition* report) {
53*e01b6f76SAndroid Build Coastguard Worker     setInputReportDefinition(report);
54*e01b6f76SAndroid Build Coastguard Worker     getInputReportDefinition()->addCollection(INPUT_COLLECTION_ID_MOUSE, 1);
55*e01b6f76SAndroid Build Coastguard Worker 
56*e01b6f76SAndroid Build Coastguard Worker     // Configure mouse axes
57*e01b6f76SAndroid Build Coastguard Worker     if (!devNode->hasRelativeAxis(REL_X) || !devNode->hasRelativeAxis(REL_Y)) {
58*e01b6f76SAndroid Build Coastguard Worker         ALOGE("Device %s is missing a relative x or y axis. Device cannot be configured.",
59*e01b6f76SAndroid Build Coastguard Worker                 devNode->getPath().c_str());
60*e01b6f76SAndroid Build Coastguard Worker         return false;
61*e01b6f76SAndroid Build Coastguard Worker     }
62*e01b6f76SAndroid Build Coastguard Worker     getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_X,
63*e01b6f76SAndroid Build Coastguard Worker             INT32_MIN, INT32_MAX, 1.0f);
64*e01b6f76SAndroid Build Coastguard Worker     getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_Y,
65*e01b6f76SAndroid Build Coastguard Worker             INT32_MIN, INT32_MAX, 1.0f);
66*e01b6f76SAndroid Build Coastguard Worker     if (devNode->hasRelativeAxis(REL_WHEEL)) {
67*e01b6f76SAndroid Build Coastguard Worker         getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE,
68*e01b6f76SAndroid Build Coastguard Worker                 INPUT_USAGE_AXIS_VSCROLL, -1, 1, 0.0f);
69*e01b6f76SAndroid Build Coastguard Worker     }
70*e01b6f76SAndroid Build Coastguard Worker     if (devNode->hasRelativeAxis(REL_HWHEEL)) {
71*e01b6f76SAndroid Build Coastguard Worker         getInputReportDefinition()->declareUsage(INPUT_COLLECTION_ID_MOUSE,
72*e01b6f76SAndroid Build Coastguard Worker                 INPUT_USAGE_AXIS_HSCROLL, -1, 1, 0.0f);
73*e01b6f76SAndroid Build Coastguard Worker     }
74*e01b6f76SAndroid Build Coastguard Worker 
75*e01b6f76SAndroid Build Coastguard Worker     // Configure mouse buttons
76*e01b6f76SAndroid Build Coastguard Worker     InputUsage usages[NELEM(codeMap)];
77*e01b6f76SAndroid Build Coastguard Worker     int numUsages = 0;
78*e01b6f76SAndroid Build Coastguard Worker     for (int32_t i = 0; i < NELEM(codeMap); ++i) {
79*e01b6f76SAndroid Build Coastguard Worker         if (devNode->hasKey(codeMap[i].scancode)) {
80*e01b6f76SAndroid Build Coastguard Worker             usages[numUsages++] = codeMap[i].usage;
81*e01b6f76SAndroid Build Coastguard Worker         }
82*e01b6f76SAndroid Build Coastguard Worker     }
83*e01b6f76SAndroid Build Coastguard Worker     if (numUsages == 0) {
84*e01b6f76SAndroid Build Coastguard Worker         ALOGW("MouseInputMapper found no buttons for %s", devNode->getPath().c_str());
85*e01b6f76SAndroid Build Coastguard Worker     }
86*e01b6f76SAndroid Build Coastguard Worker     getInputReportDefinition()->declareUsages(INPUT_COLLECTION_ID_MOUSE, usages, numUsages);
87*e01b6f76SAndroid Build Coastguard Worker     return true;
88*e01b6f76SAndroid Build Coastguard Worker }
89*e01b6f76SAndroid Build Coastguard Worker 
process(const InputEvent & event)90*e01b6f76SAndroid Build Coastguard Worker void MouseInputMapper::process(const InputEvent& event) {
91*e01b6f76SAndroid Build Coastguard Worker     ALOGV("processing mouse event. type=%d code=%d value=%d",
92*e01b6f76SAndroid Build Coastguard Worker             event.type, event.code, event.value);
93*e01b6f76SAndroid Build Coastguard Worker     switch (event.type) {
94*e01b6f76SAndroid Build Coastguard Worker         case EV_KEY:
95*e01b6f76SAndroid Build Coastguard Worker             processButton(event.code, event.value);
96*e01b6f76SAndroid Build Coastguard Worker             break;
97*e01b6f76SAndroid Build Coastguard Worker         case EV_REL:
98*e01b6f76SAndroid Build Coastguard Worker             processMotion(event.code, event.value);
99*e01b6f76SAndroid Build Coastguard Worker             break;
100*e01b6f76SAndroid Build Coastguard Worker         case EV_SYN:
101*e01b6f76SAndroid Build Coastguard Worker             if (event.code == SYN_REPORT) {
102*e01b6f76SAndroid Build Coastguard Worker                 sync(event.when);
103*e01b6f76SAndroid Build Coastguard Worker             }
104*e01b6f76SAndroid Build Coastguard Worker             break;
105*e01b6f76SAndroid Build Coastguard Worker         default:
106*e01b6f76SAndroid Build Coastguard Worker             ALOGV("unknown mouse event type: %d", event.type);
107*e01b6f76SAndroid Build Coastguard Worker     }
108*e01b6f76SAndroid Build Coastguard Worker }
109*e01b6f76SAndroid Build Coastguard Worker 
processMotion(int32_t code,int32_t value)110*e01b6f76SAndroid Build Coastguard Worker void MouseInputMapper::processMotion(int32_t code, int32_t value) {
111*e01b6f76SAndroid Build Coastguard Worker     switch (code) {
112*e01b6f76SAndroid Build Coastguard Worker         case REL_X:
113*e01b6f76SAndroid Build Coastguard Worker             mRelX = value;
114*e01b6f76SAndroid Build Coastguard Worker             break;
115*e01b6f76SAndroid Build Coastguard Worker         case REL_Y:
116*e01b6f76SAndroid Build Coastguard Worker             mRelY = value;
117*e01b6f76SAndroid Build Coastguard Worker             break;
118*e01b6f76SAndroid Build Coastguard Worker         case REL_WHEEL:
119*e01b6f76SAndroid Build Coastguard Worker             mRelWheel = value;
120*e01b6f76SAndroid Build Coastguard Worker             break;
121*e01b6f76SAndroid Build Coastguard Worker         case REL_HWHEEL:
122*e01b6f76SAndroid Build Coastguard Worker             mRelHWheel = value;
123*e01b6f76SAndroid Build Coastguard Worker             break;
124*e01b6f76SAndroid Build Coastguard Worker         default:
125*e01b6f76SAndroid Build Coastguard Worker             // Unknown code. Ignore.
126*e01b6f76SAndroid Build Coastguard Worker             break;
127*e01b6f76SAndroid Build Coastguard Worker     }
128*e01b6f76SAndroid Build Coastguard Worker }
129*e01b6f76SAndroid Build Coastguard Worker 
130*e01b6f76SAndroid Build Coastguard Worker // Map evdev button codes to bit indices. This function assumes code >=
131*e01b6f76SAndroid Build Coastguard Worker // BTN_MOUSE.
buttonToBit(int32_t code)132*e01b6f76SAndroid Build Coastguard Worker uint32_t buttonToBit(int32_t code) {
133*e01b6f76SAndroid Build Coastguard Worker     return static_cast<uint32_t>(code - BTN_MOUSE);
134*e01b6f76SAndroid Build Coastguard Worker }
135*e01b6f76SAndroid Build Coastguard Worker 
processButton(int32_t code,int32_t value)136*e01b6f76SAndroid Build Coastguard Worker void MouseInputMapper::processButton(int32_t code, int32_t value) {
137*e01b6f76SAndroid Build Coastguard Worker     // Mouse buttons start at BTN_MOUSE and end before BTN_JOYSTICK. There isn't
138*e01b6f76SAndroid Build Coastguard Worker     // really enough room after the mouse buttons for another button class, so
139*e01b6f76SAndroid Build Coastguard Worker     // the risk of a button type being inserted after mouse is low.
140*e01b6f76SAndroid Build Coastguard Worker     if (code >= BTN_MOUSE && code < BTN_JOYSTICK) {
141*e01b6f76SAndroid Build Coastguard Worker         if (value) {
142*e01b6f76SAndroid Build Coastguard Worker             mButtonValues.markBit(buttonToBit(code));
143*e01b6f76SAndroid Build Coastguard Worker         } else {
144*e01b6f76SAndroid Build Coastguard Worker             mButtonValues.clearBit(buttonToBit(code));
145*e01b6f76SAndroid Build Coastguard Worker         }
146*e01b6f76SAndroid Build Coastguard Worker         mUpdatedButtonMask.markBit(buttonToBit(code));
147*e01b6f76SAndroid Build Coastguard Worker     }
148*e01b6f76SAndroid Build Coastguard Worker }
149*e01b6f76SAndroid Build Coastguard Worker 
sync(nsecs_t when)150*e01b6f76SAndroid Build Coastguard Worker void MouseInputMapper::sync(nsecs_t when) {
151*e01b6f76SAndroid Build Coastguard Worker     // Process updated button states.
152*e01b6f76SAndroid Build Coastguard Worker     while (!mUpdatedButtonMask.isEmpty()) {
153*e01b6f76SAndroid Build Coastguard Worker         auto bit = mUpdatedButtonMask.clearFirstMarkedBit();
154*e01b6f76SAndroid Build Coastguard Worker         getInputReport()->setBoolUsage(INPUT_COLLECTION_ID_MOUSE, codeMap[bit].usage,
155*e01b6f76SAndroid Build Coastguard Worker                 mButtonValues.hasBit(bit), 0);
156*e01b6f76SAndroid Build Coastguard Worker     }
157*e01b6f76SAndroid Build Coastguard Worker 
158*e01b6f76SAndroid Build Coastguard Worker     // Process motion and scroll changes.
159*e01b6f76SAndroid Build Coastguard Worker     if (mRelX != 0) {
160*e01b6f76SAndroid Build Coastguard Worker         getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_X, mRelX, 0);
161*e01b6f76SAndroid Build Coastguard Worker     }
162*e01b6f76SAndroid Build Coastguard Worker     if (mRelY != 0) {
163*e01b6f76SAndroid Build Coastguard Worker         getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_Y, mRelY, 0);
164*e01b6f76SAndroid Build Coastguard Worker     }
165*e01b6f76SAndroid Build Coastguard Worker     if (mRelWheel != 0) {
166*e01b6f76SAndroid Build Coastguard Worker         getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_VSCROLL,
167*e01b6f76SAndroid Build Coastguard Worker                 mRelWheel, 0);
168*e01b6f76SAndroid Build Coastguard Worker     }
169*e01b6f76SAndroid Build Coastguard Worker     if (mRelHWheel != 0) {
170*e01b6f76SAndroid Build Coastguard Worker         getInputReport()->setIntUsage(INPUT_COLLECTION_ID_MOUSE, INPUT_USAGE_AXIS_HSCROLL,
171*e01b6f76SAndroid Build Coastguard Worker                 mRelHWheel, 0);
172*e01b6f76SAndroid Build Coastguard Worker     }
173*e01b6f76SAndroid Build Coastguard Worker 
174*e01b6f76SAndroid Build Coastguard Worker     // Report and reset.
175*e01b6f76SAndroid Build Coastguard Worker     getInputReport()->reportEvent(getDeviceHandle());
176*e01b6f76SAndroid Build Coastguard Worker     mUpdatedButtonMask.clear();
177*e01b6f76SAndroid Build Coastguard Worker     mButtonValues.clear();
178*e01b6f76SAndroid Build Coastguard Worker     mRelX = 0;
179*e01b6f76SAndroid Build Coastguard Worker     mRelY = 0;
180*e01b6f76SAndroid Build Coastguard Worker     mRelWheel = 0;
181*e01b6f76SAndroid Build Coastguard Worker     mRelHWheel = 0;
182*e01b6f76SAndroid Build Coastguard Worker }
183*e01b6f76SAndroid Build Coastguard Worker 
184*e01b6f76SAndroid Build Coastguard Worker }  // namespace android
185