1 /*
2 * Copyright (C) 2019 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 #ifndef _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
18 #define _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
19
20 #include <android-base/properties.h>
21 #include <input/DisplayViewport.h>
22 #include <stdint.h>
23
24 #include "EventHub.h"
25 #include "InputListener.h"
26 #include "InputReaderContext.h"
27
28 namespace android {
29
30 // --- Static Definitions ---
31
32 // When per-window input rotation is enabled, display transformations such as rotation and
33 // projection are part of the input window's transform. This means InputReader should work in the
34 // un-rotated coordinate space.
isPerWindowInputRotationEnabled()35 static bool isPerWindowInputRotationEnabled() {
36 static const bool PER_WINDOW_INPUT_ROTATION =
37 base::GetBoolProperty("persist.debug.per_window_input_rotation", false);
38 return PER_WINDOW_INPUT_ROTATION;
39 }
40
getInverseRotation(int32_t orientation)41 static int32_t getInverseRotation(int32_t orientation) {
42 switch (orientation) {
43 case DISPLAY_ORIENTATION_90:
44 return DISPLAY_ORIENTATION_270;
45 case DISPLAY_ORIENTATION_270:
46 return DISPLAY_ORIENTATION_90;
47 default:
48 return orientation;
49 }
50 }
51
rotateDelta(int32_t orientation,float * deltaX,float * deltaY)52 static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
53 float temp;
54 switch (orientation) {
55 case DISPLAY_ORIENTATION_90:
56 temp = *deltaX;
57 *deltaX = *deltaY;
58 *deltaY = -temp;
59 break;
60
61 case DISPLAY_ORIENTATION_180:
62 *deltaX = -*deltaX;
63 *deltaY = -*deltaY;
64 break;
65
66 case DISPLAY_ORIENTATION_270:
67 temp = *deltaX;
68 *deltaX = -*deltaY;
69 *deltaY = temp;
70 break;
71
72 default:
73 break;
74 }
75 }
76
77 // Rotates the given point (x, y) by the supplied orientation. The width and height are the
78 // dimensions of the surface prior to this rotation being applied.
rotatePoint(int32_t orientation,float & x,float & y,int32_t width,int32_t height)79 static void rotatePoint(int32_t orientation, float& x, float& y, int32_t width, int32_t height) {
80 rotateDelta(orientation, &x, &y);
81 switch (orientation) {
82 case DISPLAY_ORIENTATION_90:
83 y += width;
84 break;
85 case DISPLAY_ORIENTATION_180:
86 x += width;
87 y += height;
88 break;
89 case DISPLAY_ORIENTATION_270:
90 x += height;
91 break;
92 default:
93 break;
94 }
95 }
96
97 // Returns true if the pointer should be reported as being down given the specified
98 // button states. This determines whether the event is reported as a touch event.
isPointerDown(int32_t buttonState)99 static bool isPointerDown(int32_t buttonState) {
100 return buttonState &
101 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
102 AMOTION_EVENT_BUTTON_TERTIARY);
103 }
104
synthesizeButtonKey(InputReaderContext * context,int32_t action,nsecs_t when,nsecs_t readTime,int32_t deviceId,uint32_t source,int32_t displayId,uint32_t policyFlags,int32_t lastButtonState,int32_t currentButtonState,int32_t buttonState,int32_t keyCode)105 static void synthesizeButtonKey(InputReaderContext* context, int32_t action, nsecs_t when,
106 nsecs_t readTime, int32_t deviceId, uint32_t source,
107 int32_t displayId, uint32_t policyFlags, int32_t lastButtonState,
108 int32_t currentButtonState, int32_t buttonState, int32_t keyCode) {
109 if ((action == AKEY_EVENT_ACTION_DOWN && !(lastButtonState & buttonState) &&
110 (currentButtonState & buttonState)) ||
111 (action == AKEY_EVENT_ACTION_UP && (lastButtonState & buttonState) &&
112 !(currentButtonState & buttonState))) {
113 NotifyKeyArgs args(context->getNextId(), when, readTime, deviceId, source, displayId,
114 policyFlags, action, 0, keyCode, 0, context->getGlobalMetaState(), when);
115 context->getListener()->notifyKey(&args);
116 }
117 }
118
synthesizeButtonKeys(InputReaderContext * context,int32_t action,nsecs_t when,nsecs_t readTime,int32_t deviceId,uint32_t source,int32_t displayId,uint32_t policyFlags,int32_t lastButtonState,int32_t currentButtonState)119 static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, nsecs_t when,
120 nsecs_t readTime, int32_t deviceId, uint32_t source,
121 int32_t displayId, uint32_t policyFlags, int32_t lastButtonState,
122 int32_t currentButtonState) {
123 synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId, policyFlags,
124 lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_BACK,
125 AKEYCODE_BACK);
126 synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId, policyFlags,
127 lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_FORWARD,
128 AKEYCODE_FORWARD);
129 }
130
131 } // namespace android
132
133 #endif // _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
134