blob: 83653861c6f93c24e43241d5aca5252382efb87e [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"
18#define LOG_NDEBUG 0
19
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070020// Enables debug output for processing input events
21#define DEBUG_INPUT_EVENTS 0
22
Tim Kilbourn73475a42015-02-13 10:35:20 -080023#include <linux/input.h>
24
25#define __STDC_FORMAT_MACROS
26#include <cinttypes>
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070027#include <cstdlib>
Tim Kilbourn73475a42015-02-13 10:35:20 -080028#include <string>
29
30#include <utils/Log.h>
31#include <utils/Timers.h>
32
33#include "InputHub.h"
34#include "InputDevice.h"
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070035#include "InputMapper.h"
36#include "SwitchInputMapper.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080037
38#define MSC_ANDROID_TIME_SEC 0x6
39#define MSC_ANDROID_TIME_USEC 0x7
40
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
96static bool getBooleanProperty(const InputProperty& prop) {
97 const char* propValue = prop.getValue();
98 if (propValue == nullptr) return false;
99
100 char* end;
101 int value = std::strtol(propValue, &end, 10);
102 if (*end != '\0') {
103 ALOGW("Expected boolean for property %s; value=%s", prop.getKey(), propValue);
104 return false;
105 }
106 return value;
107}
108
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700109EvdevDevice::EvdevDevice(InputHostInterface* host, const std::shared_ptr<InputDeviceNode>& node) :
110 mHost(host), mDeviceNode(node), mDeviceDefinition(mHost->createDeviceDefinition()) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700111
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700112 InputBus bus = getInputBus(node);
113 mInputId = mHost->createDeviceIdentifier(
114 node->getName().c_str(),
115 node->getProductId(),
116 node->getVendorId(),
117 bus,
118 node->getUniqueId().c_str());
119
120 createMappers();
121 configureDevice();
122
123 // If we found a need for at least one mapper, register the device with the
124 // host. If there were no mappers, this device is effectively ignored, as
125 // the host won't know about it.
126 if (mMappers.size() > 0) {
127 mDeviceHandle = mHost->registerDevice(mInputId, mDeviceDefinition);
128 for (const auto& mapper : mMappers) {
129 mapper->setDeviceHandle(mDeviceHandle);
130 }
131 }
132}
133
134void EvdevDevice::createMappers() {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700135 // See if this is a cursor device such as a trackball or mouse.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700136 if (mDeviceNode->hasKey(BTN_MOUSE)
137 && mDeviceNode->hasRelativeAxis(REL_X)
138 && mDeviceNode->hasRelativeAxis(REL_Y)) {
139 mClasses |= INPUT_DEVICE_CLASS_CURSOR;
140 //mMappers.push_back(std::make_unique<CursorInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700141 }
142
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700143 bool isStylus = false;
144 bool haveGamepadButtons = mDeviceNode->hasKeyInRange(BTN_MISC, BTN_MOUSE) ||
145 mDeviceNode->hasKeyInRange(BTN_JOYSTICK, BTN_DIGI);
146
147 // See if this is a touch pad or stylus.
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700148 // Is this a new modern multi-touch driver?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700149 if (mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_X)
150 && mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_Y)) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700151 // Some joysticks such as the PS3 controller report axes that conflict
152 // with the ABS_MT range. Try to confirm that the device really is a
153 // touch screen.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700154 if (mDeviceNode->hasKey(BTN_TOUCH) || !haveGamepadButtons) {
155 mClasses |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
156 //mMappers.push_back(std::make_unique<MultiTouchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700157 }
158 // Is this an old style single-touch driver?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700159 } else if (mDeviceNode->hasKey(BTN_TOUCH)
160 && mDeviceNode->hasAbsoluteAxis(ABS_X)
161 && mDeviceNode->hasAbsoluteAxis(ABS_Y)) {
162 mClasses |= INPUT_DEVICE_CLASS_TOUCH;
163 //mMappers.push_back(std::make_unique<SingleTouchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700164 // Is this a BT stylus?
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700165 } else if ((mDeviceNode->hasAbsoluteAxis(ABS_PRESSURE) || mDeviceNode->hasKey(BTN_TOUCH))
166 && !mDeviceNode->hasAbsoluteAxis(ABS_X) && !mDeviceNode->hasAbsoluteAxis(ABS_Y)) {
167 mClasses |= INPUT_DEVICE_CLASS_EXTERNAL_STYLUS;
168 //mMappers.push_back(std::make_unique<ExternalStylusInputMapper>());
169 isStylus = true;
170 mClasses &= ~INPUT_DEVICE_CLASS_KEYBOARD;
171 }
172
173 // See if this is a keyboard. Ignore everything in the button range except
174 // for joystick and gamepad buttons which are handled like keyboards for the
175 // most part.
176 // Keyboard will try to claim some of the stylus buttons but we really want
177 // to reserve those so we can fuse it with the touch screen data. Note this
178 // means an external stylus cannot also be a keyboard device.
179 if (!isStylus) {
180 bool haveKeyboardKeys = mDeviceNode->hasKeyInRange(0, BTN_MISC) ||
181 mDeviceNode->hasKeyInRange(KEY_OK, KEY_CNT);
182 if (haveKeyboardKeys || haveGamepadButtons) {
183 mClasses |= INPUT_DEVICE_CLASS_KEYBOARD;
184 //mMappers.push_back(std::make_unique<KeyboardInputMapper>());
185 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700186 }
187
188 // See if this device is a joystick.
189 // Assumes that joysticks always have gamepad buttons in order to
190 // distinguish them from other devices such as accelerometers that also have
191 // absolute axes.
192 if (haveGamepadButtons) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700193 uint32_t assumedClasses = mClasses | INPUT_DEVICE_CLASS_JOYSTICK;
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700194 for (int i = 0; i < ABS_CNT; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700195 if (mDeviceNode->hasAbsoluteAxis(i)
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700196 && getAbsAxisUsage(i, assumedClasses) == INPUT_DEVICE_CLASS_JOYSTICK) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700197 mClasses = assumedClasses;
198 //mMappers.push_back(std::make_unique<JoystickInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700199 break;
200 }
201 }
202 }
203
204 // Check whether this device has switches.
205 for (int i = 0; i < SW_CNT; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700206 if (mDeviceNode->hasSwitch(i)) {
207 mClasses |= INPUT_DEVICE_CLASS_SWITCH;
208 mMappers.push_back(std::make_unique<SwitchInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700209 break;
210 }
211 }
212
213 // Check whether this device supports the vibrator.
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700214 // TODO: decide if this is necessary.
215 if (mDeviceNode->hasForceFeedback(FF_RUMBLE)) {
216 mClasses |= INPUT_DEVICE_CLASS_VIBRATOR;
217 //mMappers.push_back(std::make_unique<VibratorInputMapper>());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700218 }
219
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700220 ALOGD("device %s classes=0x%x %d mappers", mDeviceNode->getPath().c_str(), mClasses,
221 mMappers.size());
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700222}
223
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700224void EvdevDevice::configureDevice() {
225 for (const auto& mapper : mMappers) {
226 auto reportDef = mHost->createInputReportDefinition();
227 if (mapper->configureInputReport(mDeviceNode.get(), reportDef)) {
228 mDeviceDefinition->addReport(reportDef);
229 } else {
230 mHost->freeReportDefinition(reportDef);
231 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700232
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700233 reportDef = mHost->createOutputReportDefinition();
234 if (mapper->configureOutputReport(mDeviceNode.get(), reportDef)) {
235 mDeviceDefinition->addReport(reportDef);
236 } else {
237 mHost->freeReportDefinition(reportDef);
238 }
239 }
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700240}
Tim Kilbourn73475a42015-02-13 10:35:20 -0800241
242void EvdevDevice::processInput(InputEvent& event, nsecs_t currentTime) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700243#if DEBUG_INPUT_EVENTS
Tim Kilbourn73475a42015-02-13 10:35:20 -0800244 std::string log;
245 log.append("---InputEvent for device %s---\n");
246 log.append(" when: %" PRId64 "\n");
247 log.append(" type: %d\n");
248 log.append(" code: %d\n");
249 log.append(" value: %d\n");
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700250 ALOGD(log.c_str(), mDeviceNode->getPath().c_str(), event.when, event.type, event.code,
Tim Kilbourn73475a42015-02-13 10:35:20 -0800251 event.value);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700252#endif
Tim Kilbourn73475a42015-02-13 10:35:20 -0800253
254 if (event.type == EV_MSC) {
255 if (event.code == MSC_ANDROID_TIME_SEC) {
256 mOverrideSec = event.value;
257 } else if (event.code == MSC_ANDROID_TIME_USEC) {
258 mOverrideUsec = event.value;
259 }
260 return;
261 }
262
263 if (mOverrideSec || mOverrideUsec) {
264 event.when = s2ns(mOverrideSec) + us2ns(mOverrideUsec);
265 ALOGV("applied override time %d.%06d", mOverrideSec, mOverrideUsec);
266
267 if (event.type == EV_SYN && event.code == SYN_REPORT) {
268 mOverrideSec = 0;
269 mOverrideUsec = 0;
270 }
271 }
272
273 // Bug 7291243: Add a guard in case the kernel generates timestamps
274 // that appear to be far into the future because they were generated
275 // using the wrong clock source.
276 //
277 // This can happen because when the input device is initially opened
278 // it has a default clock source of CLOCK_REALTIME. Any input events
279 // enqueued right after the device is opened will have timestamps
280 // generated using CLOCK_REALTIME. We later set the clock source
281 // to CLOCK_MONOTONIC but it is already too late.
282 //
283 // Invalid input event timestamps can result in ANRs, crashes and
284 // and other issues that are hard to track down. We must not let them
285 // propagate through the system.
286 //
287 // Log a warning so that we notice the problem and recover gracefully.
288 if (event.when >= currentTime + s2ns(10)) {
289 // Double-check. Time may have moved on.
290 auto time = systemTime(SYSTEM_TIME_MONOTONIC);
291 if (event.when > time) {
292 ALOGW("An input event from %s has a timestamp that appears to have "
293 "been generated using the wrong clock source (expected "
294 "CLOCK_MONOTONIC): event time %" PRId64 ", current time %" PRId64
295 ", call time %" PRId64 ". Using current time instead.",
296 mDeviceNode->getPath().c_str(), event.when, time, currentTime);
297 event.when = time;
298 } else {
299 ALOGV("Event time is ok but failed the fast path and required an extra "
300 "call to systemTime: event time %" PRId64 ", current time %" PRId64
301 ", call time %" PRId64 ".", event.when, time, currentTime);
302 }
303 }
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700304
305 for (size_t i = 0; i < mMappers.size(); ++i) {
306 mMappers[i]->process(event);
307 }
Tim Kilbourn73475a42015-02-13 10:35:20 -0800308}
309
310} // namespace android