blob: a1bce643da19bc2337165f2127c360f32fc54ee3 [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
27#define __STDC_FORMAT_MACROS
28#include <cinttypes>
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070029#include <cstdlib>
Tim Kilbourn73475a42015-02-13 10:35:20 -080030#include <string>
31
32#include <utils/Log.h>
33#include <utils/Timers.h>
34
Tim Kilbourndbc8c162015-05-19 15:04:30 -070035#include "InputHost.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080036#include "InputHub.h"
Tim Kilbourn864984c2015-05-19 17:15:52 -070037#include "MouseInputMapper.h"
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070038#include "SwitchInputMapper.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080039
Tim Kilbourn73475a42015-02-13 10:35:20 -080040
41namespace android {
42
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070043static 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
57static 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
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070096EvdevDevice::EvdevDevice(InputHostInterface* host, const std::shared_ptr<InputDeviceNode>& node) :
97 mHost(host), mDeviceNode(node), mDeviceDefinition(mHost->createDeviceDefinition()) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070098
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070099 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
121void EvdevDevice::createMappers() {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700122 // See if this is a cursor device such as a trackball or mouse.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700123 if (mDeviceNode->hasKey(BTN_MOUSE)
124 && mDeviceNode->hasRelativeAxis(REL_X)
125 && mDeviceNode->hasRelativeAxis(REL_Y)) {
126 mClasses |= INPUT_DEVICE_CLASS_CURSOR;
Tim Kilbourn864984c2015-05-19 17:15:52 -0700127 mMappers.push_back(std::make_unique<MouseInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700128 }
129
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700130 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.
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700135 // Is this a new modern multi-touch driver?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700136 if (mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_X)
137 && mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_Y)) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700138 // 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.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700141 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>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700144 }
145 // Is this an old style single-touch driver?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700146 } 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>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700151 // Is this a BT stylus?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700152 } 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 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700173 }
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) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700180 uint32_t assumedClasses = mClasses | INPUT_DEVICE_CLASS_JOYSTICK;
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700181 for (int i = 0; i < ABS_CNT; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700182 if (mDeviceNode->hasAbsoluteAxis(i)
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700183 && getAbsAxisUsage(i, assumedClasses) == INPUT_DEVICE_CLASS_JOYSTICK) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700184 mClasses = assumedClasses;
185 //mMappers.push_back(std::make_unique<JoystickInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700186 break;
187 }
188 }
189 }
190
191 // Check whether this device has switches.
192 for (int i = 0; i < SW_CNT; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700193 if (mDeviceNode->hasSwitch(i)) {
194 mClasses |= INPUT_DEVICE_CLASS_SWITCH;
195 mMappers.push_back(std::make_unique<SwitchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700196 break;
197 }
198 }
199
200 // Check whether this device supports the vibrator.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700201 // 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>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700205 }
206
Michael Wrightfdd4d812015-11-25 16:00:28 +0000207 ALOGD("device %s classes=0x%x %zu mappers", mDeviceNode->getPath().c_str(), mClasses,
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700208 mMappers.size());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700209}
210
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700211void 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 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700219
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700220 reportDef = mHost->createOutputReportDefinition();
221 if (mapper->configureOutputReport(mDeviceNode.get(), reportDef)) {
222 mDeviceDefinition->addReport(reportDef);
223 } else {
224 mHost->freeReportDefinition(reportDef);
225 }
226 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700227}
Tim Kilbourn73475a42015-02-13 10:35:20 -0800228
229void EvdevDevice::processInput(InputEvent& event, nsecs_t currentTime) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700230#if DEBUG_INPUT_EVENTS
Tim Kilbourn73475a42015-02-13 10:35:20 -0800231 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");
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700237 ALOGD(log.c_str(), mDeviceNode->getPath().c_str(), event.when, event.type, event.code,
Tim Kilbourn73475a42015-02-13 10:35:20 -0800238 event.value);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700239#endif
Tim Kilbourn73475a42015-02-13 10:35:20 -0800240
Tim Kilbourn73475a42015-02-13 10:35:20 -0800241 // 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 }
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700272
273 for (size_t i = 0; i < mMappers.size(); ++i) {
274 mMappers[i]->process(event);
275 }
Tim Kilbourn73475a42015-02-13 10:35:20 -0800276}
277
278} // namespace android