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