• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cinttypes>
28 #include <cstdlib>
29 #include <string>
30 
31 #include <utils/Log.h>
32 #include <utils/Timers.h>
33 
34 #include "InputHost.h"
35 #include "InputHub.h"
36 #include "MouseInputMapper.h"
37 #include "SwitchInputMapper.h"
38 
39 
40 namespace android {
41 
getInputBus(const std::shared_ptr<InputDeviceNode> & node)42 static 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 
getAbsAxisUsage(int32_t axis,uint32_t deviceClasses)56 static 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 
EvdevDevice(InputHostInterface * host,const std::shared_ptr<InputDeviceNode> & node)95 EvdevDevice::EvdevDevice(InputHostInterface* host, const std::shared_ptr<InputDeviceNode>& node) :
96     mHost(host), mDeviceNode(node), mDeviceDefinition(mHost->createDeviceDefinition()) {
97 
98     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 
createMappers()120 void EvdevDevice::createMappers() {
121     // See if this is a cursor device such as a trackball or mouse.
122     if (mDeviceNode->hasKey(BTN_MOUSE)
123             && mDeviceNode->hasRelativeAxis(REL_X)
124             && mDeviceNode->hasRelativeAxis(REL_Y)) {
125         mClasses |= INPUT_DEVICE_CLASS_CURSOR;
126         mMappers.push_back(std::make_unique<MouseInputMapper>());
127     }
128 
129     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.
134     // Is this a new modern multi-touch driver?
135     if (mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_X)
136             && mDeviceNode->hasAbsoluteAxis(ABS_MT_POSITION_Y)) {
137         // 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.
140         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>());
143         }
144     // Is this an old style single-touch driver?
145     } 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>());
150     // Is this a BT stylus?
151     } 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         }
172     }
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) {
179         uint32_t assumedClasses = mClasses | INPUT_DEVICE_CLASS_JOYSTICK;
180         for (int i = 0; i < ABS_CNT; ++i) {
181             if (mDeviceNode->hasAbsoluteAxis(i)
182                     && getAbsAxisUsage(i, assumedClasses) == INPUT_DEVICE_CLASS_JOYSTICK) {
183                 mClasses = assumedClasses;
184                 //mMappers.push_back(std::make_unique<JoystickInputMapper>());
185                 break;
186             }
187         }
188     }
189 
190     // Check whether this device has switches.
191     for (int i = 0; i < SW_CNT; ++i) {
192         if (mDeviceNode->hasSwitch(i)) {
193             mClasses |= INPUT_DEVICE_CLASS_SWITCH;
194             mMappers.push_back(std::make_unique<SwitchInputMapper>());
195             break;
196         }
197     }
198 
199     // Check whether this device supports the vibrator.
200     // 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>());
204     }
205 
206     ALOGD("device %s classes=0x%x %zu mappers", mDeviceNode->getPath().c_str(), mClasses,
207             mMappers.size());
208 }
209 
configureDevice()210 void 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         }
218 
219         reportDef = mHost->createOutputReportDefinition();
220         if (mapper->configureOutputReport(mDeviceNode.get(), reportDef)) {
221             mDeviceDefinition->addReport(reportDef);
222         } else {
223             mHost->freeReportDefinition(reportDef);
224         }
225     }
226 }
227 
processInput(InputEvent & event,nsecs_t currentTime)228 void EvdevDevice::processInput(InputEvent& event, nsecs_t currentTime) {
229 #if DEBUG_INPUT_EVENTS
230     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");
236     ALOGD(log.c_str(), mDeviceNode->getPath().c_str(), event.when, event.type, event.code,
237             event.value);
238 #endif
239 
240     // 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     }
271 
272     for (size_t i = 0; i < mMappers.size(); ++i) {
273         mMappers[i]->process(event);
274     }
275 }
276 
277 }  // namespace android
278