1 /*
2  * Copyright (C) 2022 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 #include <input/DisplayViewport.h>
18 #include <stdint.h>
19 #include <ui/Rotation.h>
20 
21 #include "EventHub.h"
22 #include "InputListener.h"
23 #include "InputReaderContext.h"
24 
25 namespace android {
26 
27 namespace {
28 
synthesizeButtonKey(InputReaderContext * context,int32_t action,nsecs_t when,nsecs_t readTime,int32_t deviceId,uint32_t source,ui::LogicalDisplayId displayId,uint32_t policyFlags,int32_t lastButtonState,int32_t currentButtonState,int32_t buttonState,int32_t keyCode)29 [[nodiscard]] std::list<NotifyArgs> synthesizeButtonKey(
30         InputReaderContext* context, int32_t action, nsecs_t when, nsecs_t readTime,
31         int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
32         int32_t lastButtonState, int32_t currentButtonState, int32_t buttonState, int32_t keyCode) {
33     std::list<NotifyArgs> out;
34     if ((action == AKEY_EVENT_ACTION_DOWN && !(lastButtonState & buttonState) &&
35          (currentButtonState & buttonState)) ||
36         (action == AKEY_EVENT_ACTION_UP && (lastButtonState & buttonState) &&
37          !(currentButtonState & buttonState))) {
38         out.push_back(NotifyKeyArgs(context->getNextId(), when, readTime, deviceId, source,
39                                     displayId, policyFlags, action, 0, keyCode, 0,
40                                     context->getGlobalMetaState(), when));
41     }
42     return out;
43 }
44 
45 } // namespace
46 
getInverseRotation(ui::Rotation orientation)47 ui::Rotation getInverseRotation(ui::Rotation orientation) {
48     switch (orientation) {
49         case ui::ROTATION_90:
50             return ui::ROTATION_270;
51         case ui::ROTATION_270:
52             return ui::ROTATION_90;
53         default:
54             return orientation;
55     }
56 }
57 
rotateDelta(ui::Rotation orientation,float * deltaX,float * deltaY)58 void rotateDelta(ui::Rotation orientation, float* deltaX, float* deltaY) {
59     float temp;
60     switch (orientation) {
61         case ui::ROTATION_90:
62             temp = *deltaX;
63             *deltaX = *deltaY;
64             *deltaY = -temp;
65             break;
66 
67         case ui::ROTATION_180:
68             *deltaX = -*deltaX;
69             *deltaY = -*deltaY;
70             break;
71 
72         case ui::ROTATION_270:
73             temp = *deltaX;
74             *deltaX = -*deltaY;
75             *deltaY = temp;
76             break;
77 
78         default:
79             break;
80     }
81 }
82 
isPointerDown(int32_t buttonState)83 bool isPointerDown(int32_t buttonState) {
84     return buttonState &
85             (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
86              AMOTION_EVENT_BUTTON_TERTIARY);
87 }
88 
synthesizeButtonKeys(InputReaderContext * context,int32_t action,nsecs_t when,nsecs_t readTime,int32_t deviceId,uint32_t source,ui::LogicalDisplayId displayId,uint32_t policyFlags,int32_t lastButtonState,int32_t currentButtonState)89 [[nodiscard]] std::list<NotifyArgs> synthesizeButtonKeys(
90         InputReaderContext* context, int32_t action, nsecs_t when, nsecs_t readTime,
91         int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
92         int32_t lastButtonState, int32_t currentButtonState) {
93     std::list<NotifyArgs> out;
94     out += synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId,
95                                policyFlags, lastButtonState, currentButtonState,
96                                AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
97     out += synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId,
98                                policyFlags, lastButtonState, currentButtonState,
99                                AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
100     return out;
101 }
102 
applyBluetoothTimestampSmoothening(const InputDeviceIdentifier & identifier,nsecs_t currentEventTime,nsecs_t readTime,nsecs_t lastEventTime)103 std::tuple<nsecs_t /*eventTime*/, nsecs_t /*readTime*/> applyBluetoothTimestampSmoothening(
104         const InputDeviceIdentifier& identifier, nsecs_t currentEventTime, nsecs_t readTime,
105         nsecs_t lastEventTime) {
106     if (identifier.bus != BUS_BLUETOOTH) {
107         return {currentEventTime, readTime};
108     }
109 
110     // Assume the fastest rate at which a Bluetooth touch device can report input events is one
111     // every 4 milliseconds, or 250 Hz. Timestamps for successive events from a Bluetooth device
112     // will be separated by at least this amount.
113     constexpr static nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
114     // We define a maximum smoothing time delta so that we don't generate events too far into the
115     // future.
116     constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
117     const nsecs_t smoothenedEventTime =
118             std::min(std::max(currentEventTime, lastEventTime + MIN_BLUETOOTH_TIMESTAMP_DELTA),
119                      currentEventTime + MAX_BLUETOOTH_SMOOTHING_DELTA);
120     // If we are modifying the event time, treat this event as a synthetically generated event for
121     // latency tracking purposes and use the event time as the read time (zero read latency).
122     const nsecs_t smoothenedReadTime =
123             smoothenedEventTime != currentEventTime ? currentEventTime : readTime;
124     return {smoothenedEventTime, smoothenedReadTime};
125 }
126 
127 } // namespace android
128