1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "InputDevice"
18 //#define LOG_NDEBUG 0
19
20 // Enables debug output for processing input events
21 #define DEBUG_INPUT_EVENTS 0
22
23 #include "InputDevice.h"
24
25 #include <linux/input.h>
26
27 #define __STDC_FORMAT_MACROS
28 #include <cinttypes>
29 #include <cstdlib>
30 #include <string>
31
32 #include <utils/Log.h>
33 #include <utils/Timers.h>
34
35 #include "InputHost.h"
36 #include "InputHub.h"
37 #include "MouseInputMapper.h"
38 #include "SwitchInputMapper.h"
39
40
41 namespace android {
42
getInputBus(const std::shared_ptr<InputDeviceNode> & node)43 static InputBus getInputBus(const std::shared_ptr<InputDeviceNode>& node) {
44 switch (node->getBusType()) {
45 case BUS_USB:
46 return INPUT_BUS_USB;
47 case BUS_BLUETOOTH:
48 return INPUT_BUS_BT;
49 case BUS_RS232:
50 return INPUT_BUS_SERIAL;
51 default:
52 // TODO: check for other linux bus types that might not be built-in
53 return INPUT_BUS_BUILTIN;
54 }
55 }
56
getAbsAxisUsage(int32_t axis,uint32_t deviceClasses)57 static uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
58 // Touch devices get dibs on touch-related axes.
59 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
60 switch (axis) {
61 case ABS_X:
62 case ABS_Y:
63 case ABS_PRESSURE:
64 case ABS_TOOL_WIDTH:
65 case ABS_DISTANCE:
66 case ABS_TILT_X:
67 case ABS_TILT_Y:
68 case ABS_MT_SLOT:
69 case ABS_MT_TOUCH_MAJOR:
70 case ABS_MT_TOUCH_MINOR:
71 case ABS_MT_WIDTH_MAJOR:
72 case ABS_MT_WIDTH_MINOR:
73 case ABS_MT_ORIENTATION:
74 case ABS_MT_POSITION_X:
75 case ABS_MT_POSITION_Y:
76 case ABS_MT_TOOL_TYPE:
77 case ABS_MT_BLOB_ID:
78 case ABS_MT_TRACKING_ID:
79 case ABS_MT_PRESSURE:
80 case ABS_MT_DISTANCE:
81 return INPUT_DEVICE_CLASS_TOUCH;
82 }
83 }
84
85 // External stylus gets the pressure axis
86 if (deviceClasses & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
87 if (axis == ABS_PRESSURE) {
88 return INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
89 }
90 }
91
92 // Joystick devices get the rest.
93 return INPUT_DEVICE_CLASS_JOYSTICK;
94 }
95
EvdevDevice(InputHostInterface * host,const std::shared_ptr<InputDeviceNode> & node)96 EvdevDevice::EvdevDevice(InputHostInterface* host, const std::shared_ptr<InputDeviceNode>& node) :
97 mHost(host), mDeviceNode(node), mDeviceDefinition(mHost->createDeviceDefinition()) {
98
99 InputBus bus = getInputBus(node);
100 mInputId = mHost->createDeviceIdentifier(
101 node->getName().c_str(),
102 node->getProductId(),
103 node->getVendorId(),
104 bus,
105 node->getUniqueId().c_str());
106
107 createMappers();
108 configureDevice();
109
110 // If we found a need for at least one mapper, register the device with the
111 // host. If there were no mappers, this device is effectively ignored, as
112 // the host won't know about it.
113 if (mMappers.size() > 0) {
114 mDeviceHandle = mHost->registerDevice(mInputId, mDeviceDefinition);
115 for (const auto& mapper : mMappers) {
116 mapper->setDeviceHandle(mDeviceHandle);
117 }
118 }
119 }
120
createMappers()121 void EvdevDevice::createMappers() {
122 // See if this is a cursor device such as a trackball or mouse.
123 if (mDeviceNode->hasKey(BTN_MOUSE)
124 && mDeviceNode->hasRelativeAxis(REL_X)
125 && mDeviceNode->hasRelativeAxis(REL_Y)) {
126 mClasses |= INPUT_DEVICE_CLASS_CURSOR;
127 mMappers.push_back(std::make_unique<MouseInputMapper>());
128 }
129
130 bool isStylus = false;
131 bool haveGamepadButtons = mDeviceNode->hasKeyInRange(BTN_MISC, BTN_MOUSE) ||
132 mDeviceNode->hasKeyInRange(BTN_JOYSTICK, BTN_DIGI);
133
134 // See if this is a touch pad or stylus.
135 // Is this a new modern multi-touch driver?
136 if (mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_X)
137 && mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_Y)) {
138 // Some joysticks such as the PS3 controller report axes that conflict
139 // with the ABS_MT range. Try to confirm that the device really is a
140 // touch screen.
141 if (mDeviceNode->hasKey(BTN_TOUCH) || !haveGamepadButtons) {
142 mClasses |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
143 //mMappers.push_back(std::make_unique<MultiTouchInputMapper>());
144 }
145 // Is this an old style single-touch driver?
146 } else if (mDeviceNode->hasKey(BTN_TOUCH)
147 && mDeviceNode->hasAbsoluteAxis(ABS_X)
148 && mDeviceNode->hasAbsoluteAxis(ABS_Y)) {
149 mClasses |= INPUT_DEVICE_CLASS_TOUCH;
150 //mMappers.push_back(std::make_unique<SingleTouchInputMapper>());
151 // Is this a BT stylus?
152 } else if ((mDeviceNode->hasAbsoluteAxis(ABS_PRESSURE) || mDeviceNode->hasKey(BTN_TOUCH))
153 && !mDeviceNode->hasAbsoluteAxis(ABS_X) && !mDeviceNode->hasAbsoluteAxis(ABS_Y)) {
154 mClasses |= INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
155 //mMappers.push_back(std::make_unique<ExternalStylusInputMapper>());
156 isStylus = true;
157 mClasses &= ~INPUT_DEVICE_CLASS_KEYBOARD;
158 }
159
160 // See if this is a keyboard. Ignore everything in the button range except
161 // for joystick and gamepad buttons which are handled like keyboards for the
162 // most part.
163 // Keyboard will try to claim some of the stylus buttons but we really want
164 // to reserve those so we can fuse it with the touch screen data. Note this
165 // means an external stylus cannot also be a keyboard device.
166 if (!isStylus) {
167 bool haveKeyboardKeys = mDeviceNode->hasKeyInRange(0, BTN_MISC) ||
168 mDeviceNode->hasKeyInRange(KEY_OK, KEY_CNT);
169 if (haveKeyboardKeys || haveGamepadButtons) {
170 mClasses |= INPUT_DEVICE_CLASS_KEYBOARD;
171 //mMappers.push_back(std::make_unique<KeyboardInputMapper>());
172 }
173 }
174
175 // See if this device is a joystick.
176 // Assumes that joysticks always have gamepad buttons in order to
177 // distinguish them from other devices such as accelerometers that also have
178 // absolute axes.
179 if (haveGamepadButtons) {
180 uint32_t assumedClasses = mClasses | INPUT_DEVICE_CLASS_JOYSTICK;
181 for (int i = 0; i < ABS_CNT; ++i) {
182 if (mDeviceNode->hasAbsoluteAxis(i)
183 && getAbsAxisUsage(i, assumedClasses) == INPUT_DEVICE_CLASS_JOYSTICK) {
184 mClasses = assumedClasses;
185 //mMappers.push_back(std::make_unique<JoystickInputMapper>());
186 break;
187 }
188 }
189 }
190
191 // Check whether this device has switches.
192 for (int i = 0; i < SW_CNT; ++i) {
193 if (mDeviceNode->hasSwitch(i)) {
194 mClasses |= INPUT_DEVICE_CLASS_SWITCH;
195 mMappers.push_back(std::make_unique<SwitchInputMapper>());
196 break;
197 }
198 }
199
200 // Check whether this device supports the vibrator.
201 // TODO: decide if this is necessary.
202 if (mDeviceNode->hasForceFeedback(FF_RUMBLE)) {
203 mClasses |= INPUT_DEVICE_CLASS_VIBRATOR;
204 //mMappers.push_back(std::make_unique<VibratorInputMapper>());
205 }
206
207 ALOGD("device %s classes=0x%x %zu mappers", mDeviceNode->getPath().c_str(), mClasses,
208 mMappers.size());
209 }
210
configureDevice()211 void EvdevDevice::configureDevice() {
212 for (const auto& mapper : mMappers) {
213 auto reportDef = mHost->createInputReportDefinition();
214 if (mapper->configureInputReport(mDeviceNode.get(), reportDef)) {
215 mDeviceDefinition->addReport(reportDef);
216 } else {
217 mHost->freeReportDefinition(reportDef);
218 }
219
220 reportDef = mHost->createOutputReportDefinition();
221 if (mapper->configureOutputReport(mDeviceNode.get(), reportDef)) {
222 mDeviceDefinition->addReport(reportDef);
223 } else {
224 mHost->freeReportDefinition(reportDef);
225 }
226 }
227 }
228
processInput(InputEvent & event,nsecs_t currentTime)229 void EvdevDevice::processInput(InputEvent& event, nsecs_t currentTime) {
230 #if DEBUG_INPUT_EVENTS
231 std::string log;
232 log.append("---InputEvent for device %s---\n");
233 log.append(" when: %" PRId64 "\n");
234 log.append(" type: %d\n");
235 log.append(" code: %d\n");
236 log.append(" value: %d\n");
237 ALOGD(log.c_str(), mDeviceNode->getPath().c_str(), event.when, event.type, event.code,
238 event.value);
239 #endif
240
241 // Bug 7291243: Add a guard in case the kernel generates timestamps
242 // that appear to be far into the future because they were generated
243 // using the wrong clock source.
244 //
245 // This can happen because when the input device is initially opened
246 // it has a default clock source of CLOCK_REALTIME. Any input events
247 // enqueued right after the device is opened will have timestamps
248 // generated using CLOCK_REALTIME. We later set the clock source
249 // to CLOCK_MONOTONIC but it is already too late.
250 //
251 // Invalid input event timestamps can result in ANRs, crashes and
252 // and other issues that are hard to track down. We must not let them
253 // propagate through the system.
254 //
255 // Log a warning so that we notice the problem and recover gracefully.
256 if (event.when >= currentTime + s2ns(10)) {
257 // Double-check. Time may have moved on.
258 auto time = systemTime(SYSTEM_TIME_MONOTONIC);
259 if (event.when > time) {
260 ALOGW("An input event from %s has a timestamp that appears to have "
261 "been generated using the wrong clock source (expected "
262 "CLOCK_MONOTONIC): event time %" PRId64 ", current time %" PRId64
263 ", call time %" PRId64 ". Using current time instead.",
264 mDeviceNode->getPath().c_str(), event.when, time, currentTime);
265 event.when = time;
266 } else {
267 ALOGV("Event time is ok but failed the fast path and required an extra "
268 "call to systemTime: event time %" PRId64 ", current time %" PRId64
269 ", call time %" PRId64 ".", event.when, time, currentTime);
270 }
271 }
272
273 for (size_t i = 0; i < mMappers.size(); ++i) {
274 mMappers[i]->process(event);
275 }
276 }
277
278 } // namespace android
279