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