blob: 1e3da589f002464fdaf2f3a37386129ec4d385c7 [file] [log] [blame]
Tim Kilbourn73475a42015-02-13 10:35:20 -08001/*
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"
Michael Wrightfdd4d812015-11-25 16:00:28 +000018//#define LOG_NDEBUG 0
Tim Kilbourn73475a42015-02-13 10:35:20 -080019
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070020// Enables debug output for processing input events
21#define DEBUG_INPUT_EVENTS 0
22
Tim Kilbourndbc8c162015-05-19 15:04:30 -070023#include "InputDevice.h"
24
Tim Kilbourn73475a42015-02-13 10:35:20 -080025#include <linux/input.h>
26
Tim Kilbourn73475a42015-02-13 10:35:20 -080027#include <cinttypes>
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070028#include <cstdlib>
Tim Kilbourn73475a42015-02-13 10:35:20 -080029#include <string>
30
31#include <utils/Log.h>
32#include <utils/Timers.h>
33
Tim Kilbourndbc8c162015-05-19 15:04:30 -070034#include "InputHost.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080035#include "InputHub.h"
Tim Kilbourn864984c2015-05-19 17:15:52 -070036#include "MouseInputMapper.h"
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070037#include "SwitchInputMapper.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080038
Tim Kilbourn73475a42015-02-13 10:35:20 -080039
40namespace android {
41
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070042static InputBus getInputBus(const std::shared_ptr<InputDeviceNode>& node) {
43 switch (node->getBusType()) {
44 case BUS_USB:
45 return INPUT_BUS_USB;
46 case BUS_BLUETOOTH:
47 return INPUT_BUS_BT;
48 case BUS_RS232:
49 return INPUT_BUS_SERIAL;
50 default:
51 // TODO: check for other linux bus types that might not be built-in
52 return INPUT_BUS_BUILTIN;
53 }
54}
55
56static uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
57 // Touch devices get dibs on touch-related axes.
58 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
59 switch (axis) {
60 case ABS_X:
61 case ABS_Y:
62 case ABS_PRESSURE:
63 case ABS_TOOL_WIDTH:
64 case ABS_DISTANCE:
65 case ABS_TILT_X:
66 case ABS_TILT_Y:
67 case ABS_MT_SLOT:
68 case ABS_MT_TOUCH_MAJOR:
69 case ABS_MT_TOUCH_MINOR:
70 case ABS_MT_WIDTH_MAJOR:
71 case ABS_MT_WIDTH_MINOR:
72 case ABS_MT_ORIENTATION:
73 case ABS_MT_POSITION_X:
74 case ABS_MT_POSITION_Y:
75 case ABS_MT_TOOL_TYPE:
76 case ABS_MT_BLOB_ID:
77 case ABS_MT_TRACKING_ID:
78 case ABS_MT_PRESSURE:
79 case ABS_MT_DISTANCE:
80 return INPUT_DEVICE_CLASS_TOUCH;
81 }
82 }
83
84 // External stylus gets the pressure axis
85 if (deviceClasses & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
86 if (axis == ABS_PRESSURE) {
87 return INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
88 }
89 }
90
91 // Joystick devices get the rest.
92 return INPUT_DEVICE_CLASS_JOYSTICK;
93}
94
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070095EvdevDevice::EvdevDevice(InputHostInterface* host, const std::shared_ptr<InputDeviceNode>& node) :
96 mHost(host), mDeviceNode(node), mDeviceDefinition(mHost->createDeviceDefinition()) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070097
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070098 InputBus bus = getInputBus(node);
99 mInputId = mHost->createDeviceIdentifier(
100 node->getName().c_str(),
101 node->getProductId(),
102 node->getVendorId(),
103 bus,
104 node->getUniqueId().c_str());
105
106 createMappers();
107 configureDevice();
108
109 // If we found a need for at least one mapper, register the device with the
110 // host. If there were no mappers, this device is effectively ignored, as
111 // the host won't know about it.
112 if (mMappers.size() > 0) {
113 mDeviceHandle = mHost->registerDevice(mInputId, mDeviceDefinition);
114 for (const auto& mapper : mMappers) {
115 mapper->setDeviceHandle(mDeviceHandle);
116 }
117 }
118}
119
120void EvdevDevice::createMappers() {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700121 // See if this is a cursor device such as a trackball or mouse.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700122 if (mDeviceNode->hasKey(BTN_MOUSE)
123 && mDeviceNode->hasRelativeAxis(REL_X)
124 && mDeviceNode->hasRelativeAxis(REL_Y)) {
125 mClasses |= INPUT_DEVICE_CLASS_CURSOR;
Tim Kilbourn864984c2015-05-19 17:15:52 -0700126 mMappers.push_back(std::make_unique<MouseInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700127 }
128
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700129 bool isStylus = false;
130 bool haveGamepadButtons = mDeviceNode->hasKeyInRange(BTN_MISC, BTN_MOUSE) ||
131 mDeviceNode->hasKeyInRange(BTN_JOYSTICK, BTN_DIGI);
132
133 // See if this is a touch pad or stylus.
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700134 // Is this a new modern multi-touch driver?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700135 if (mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_X)
136 && mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_Y)) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700137 // Some joysticks such as the PS3 controller report axes that conflict
138 // with the ABS_MT range. Try to confirm that the device really is a
139 // touch screen.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700140 if (mDeviceNode->hasKey(BTN_TOUCH) || !haveGamepadButtons) {
141 mClasses |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
142 //mMappers.push_back(std::make_unique<MultiTouchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700143 }
144 // Is this an old style single-touch driver?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700145 } else if (mDeviceNode->hasKey(BTN_TOUCH)
146 && mDeviceNode->hasAbsoluteAxis(ABS_X)
147 && mDeviceNode->hasAbsoluteAxis(ABS_Y)) {
148 mClasses |= INPUT_DEVICE_CLASS_TOUCH;
149 //mMappers.push_back(std::make_unique<SingleTouchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700150 // Is this a BT stylus?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700151 } else if ((mDeviceNode->hasAbsoluteAxis(ABS_PRESSURE) || mDeviceNode->hasKey(BTN_TOUCH))
152 && !mDeviceNode->hasAbsoluteAxis(ABS_X) && !mDeviceNode->hasAbsoluteAxis(ABS_Y)) {
153 mClasses |= INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
154 //mMappers.push_back(std::make_unique<ExternalStylusInputMapper>());
155 isStylus = true;
156 mClasses &= ~INPUT_DEVICE_CLASS_KEYBOARD;
157 }
158
159 // See if this is a keyboard. Ignore everything in the button range except
160 // for joystick and gamepad buttons which are handled like keyboards for the
161 // most part.
162 // Keyboard will try to claim some of the stylus buttons but we really want
163 // to reserve those so we can fuse it with the touch screen data. Note this
164 // means an external stylus cannot also be a keyboard device.
165 if (!isStylus) {
166 bool haveKeyboardKeys = mDeviceNode->hasKeyInRange(0, BTN_MISC) ||
167 mDeviceNode->hasKeyInRange(KEY_OK, KEY_CNT);
168 if (haveKeyboardKeys || haveGamepadButtons) {
169 mClasses |= INPUT_DEVICE_CLASS_KEYBOARD;
170 //mMappers.push_back(std::make_unique<KeyboardInputMapper>());
171 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700172 }
173
174 // See if this device is a joystick.
175 // Assumes that joysticks always have gamepad buttons in order to
176 // distinguish them from other devices such as accelerometers that also have
177 // absolute axes.
178 if (haveGamepadButtons) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700179 uint32_t assumedClasses = mClasses | INPUT_DEVICE_CLASS_JOYSTICK;
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700180 for (int i = 0; i < ABS_CNT; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700181 if (mDeviceNode->hasAbsoluteAxis(i)
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700182 && getAbsAxisUsage(i, assumedClasses) == INPUT_DEVICE_CLASS_JOYSTICK) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700183 mClasses = assumedClasses;
184 //mMappers.push_back(std::make_unique<JoystickInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700185 break;
186 }
187 }
188 }
189
190 // Check whether this device has switches.
191 for (int i = 0; i < SW_CNT; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700192 if (mDeviceNode->hasSwitch(i)) {
193 mClasses |= INPUT_DEVICE_CLASS_SWITCH;
194 mMappers.push_back(std::make_unique<SwitchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700195 break;
196 }
197 }
198
199 // Check whether this device supports the vibrator.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700200 // TODO: decide if this is necessary.
201 if (mDeviceNode->hasForceFeedback(FF_RUMBLE)) {
202 mClasses |= INPUT_DEVICE_CLASS_VIBRATOR;
203 //mMappers.push_back(std::make_unique<VibratorInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700204 }
205
Michael Wrightfdd4d812015-11-25 16:00:28 +0000206 ALOGD("device %s classes=0x%x %zu mappers", mDeviceNode->getPath().c_str(), mClasses,
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700207 mMappers.size());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700208}
209
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700210void EvdevDevice::configureDevice() {
211 for (const auto& mapper : mMappers) {
212 auto reportDef = mHost->createInputReportDefinition();
213 if (mapper->configureInputReport(mDeviceNode.get(), reportDef)) {
214 mDeviceDefinition->addReport(reportDef);
215 } else {
216 mHost->freeReportDefinition(reportDef);
217 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700218
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700219 reportDef = mHost->createOutputReportDefinition();
220 if (mapper->configureOutputReport(mDeviceNode.get(), reportDef)) {
221 mDeviceDefinition->addReport(reportDef);
222 } else {
223 mHost->freeReportDefinition(reportDef);
224 }
225 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700226}
Tim Kilbourn73475a42015-02-13 10:35:20 -0800227
228void EvdevDevice::processInput(InputEvent& event, nsecs_t currentTime) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700229#if DEBUG_INPUT_EVENTS
Tim Kilbourn73475a42015-02-13 10:35:20 -0800230 std::string log;
231 log.append("---InputEvent for device %s---\n");
232 log.append(" when: %" PRId64 "\n");
233 log.append(" type: %d\n");
234 log.append(" code: %d\n");
235 log.append(" value: %d\n");
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700236 ALOGD(log.c_str(), mDeviceNode->getPath().c_str(), event.when, event.type, event.code,
Tim Kilbourn73475a42015-02-13 10:35:20 -0800237 event.value);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700238#endif
Tim Kilbourn73475a42015-02-13 10:35:20 -0800239
Tim Kilbourn73475a42015-02-13 10:35:20 -0800240 // Bug 7291243: Add a guard in case the kernel generates timestamps
241 // that appear to be far into the future because they were generated
242 // using the wrong clock source.
243 //
244 // This can happen because when the input device is initially opened
245 // it has a default clock source of CLOCK_REALTIME. Any input events
246 // enqueued right after the device is opened will have timestamps
247 // generated using CLOCK_REALTIME. We later set the clock source
248 // to CLOCK_MONOTONIC but it is already too late.
249 //
250 // Invalid input event timestamps can result in ANRs, crashes and
251 // and other issues that are hard to track down. We must not let them
252 // propagate through the system.
253 //
254 // Log a warning so that we notice the problem and recover gracefully.
255 if (event.when >= currentTime + s2ns(10)) {
256 // Double-check. Time may have moved on.
257 auto time = systemTime(SYSTEM_TIME_MONOTONIC);
258 if (event.when > time) {
259 ALOGW("An input event from %s has a timestamp that appears to have "
260 "been generated using the wrong clock source (expected "
261 "CLOCK_MONOTONIC): event time %" PRId64 ", current time %" PRId64
262 ", call time %" PRId64 ". Using current time instead.",
263 mDeviceNode->getPath().c_str(), event.when, time, currentTime);
264 event.when = time;
265 } else {
266 ALOGV("Event time is ok but failed the fast path and required an extra "
267 "call to systemTime: event time %" PRId64 ", current time %" PRId64
268 ", call time %" PRId64 ".", event.when, time, currentTime);
269 }
270 }
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700271
272 for (size_t i = 0; i < mMappers.size(); ++i) {
273 mMappers[i]->process(event);
274 }
Tim Kilbourn73475a42015-02-13 10:35:20 -0800275}
276
277} // namespace android