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 <InputFlingerProperties.sysprop.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     return sysprop::InputFlingerProperties::per_window_input_rotation().value_or(false);
37 }
38 
getInverseRotation(int32_t orientation)39 static int32_t getInverseRotation(int32_t orientation) {
40     switch (orientation) {
41         case DISPLAY_ORIENTATION_90:
42             return DISPLAY_ORIENTATION_270;
43         case DISPLAY_ORIENTATION_270:
44             return DISPLAY_ORIENTATION_90;
45         default:
46             return orientation;
47     }
48 }
49 
rotateDelta(int32_t orientation,float * deltaX,float * deltaY)50 static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
51     float temp;
52     switch (orientation) {
53         case DISPLAY_ORIENTATION_90:
54             temp = *deltaX;
55             *deltaX = *deltaY;
56             *deltaY = -temp;
57             break;
58 
59         case DISPLAY_ORIENTATION_180:
60             *deltaX = -*deltaX;
61             *deltaY = -*deltaY;
62             break;
63 
64         case DISPLAY_ORIENTATION_270:
65             temp = *deltaX;
66             *deltaX = -*deltaY;
67             *deltaY = temp;
68             break;
69 
70         default:
71             break;
72     }
73 }
74 
75 // Rotates the given point (x, y) by the supplied orientation. The width and height are the
76 // dimensions of the surface prior to this rotation being applied.
rotatePoint(int32_t orientation,float & x,float & y,int32_t width,int32_t height)77 static void rotatePoint(int32_t orientation, float& x, float& y, int32_t width, int32_t height) {
78     rotateDelta(orientation, &x, &y);
79     switch (orientation) {
80         case DISPLAY_ORIENTATION_90:
81             y += width;
82             break;
83         case DISPLAY_ORIENTATION_180:
84             x += width;
85             y += height;
86             break;
87         case DISPLAY_ORIENTATION_270:
88             x += height;
89             break;
90         default:
91             break;
92     }
93 }
94 
95 // Returns true if the pointer should be reported as being down given the specified
96 // button states.  This determines whether the event is reported as a touch event.
isPointerDown(int32_t buttonState)97 static bool isPointerDown(int32_t buttonState) {
98     return buttonState &
99             (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
100              AMOTION_EVENT_BUTTON_TERTIARY);
101 }
102 
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)103 static void synthesizeButtonKey(InputReaderContext* context, int32_t action, nsecs_t when,
104                                 nsecs_t readTime, int32_t deviceId, uint32_t source,
105                                 int32_t displayId, uint32_t policyFlags, int32_t lastButtonState,
106                                 int32_t currentButtonState, int32_t buttonState, int32_t keyCode) {
107     if ((action == AKEY_EVENT_ACTION_DOWN && !(lastButtonState & buttonState) &&
108          (currentButtonState & buttonState)) ||
109         (action == AKEY_EVENT_ACTION_UP && (lastButtonState & buttonState) &&
110          !(currentButtonState & buttonState))) {
111         NotifyKeyArgs args(context->getNextId(), when, readTime, deviceId, source, displayId,
112                            policyFlags, action, 0, keyCode, 0, context->getGlobalMetaState(), when);
113         context->getListener()->notifyKey(&args);
114     }
115 }
116 
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)117 static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, nsecs_t when,
118                                  nsecs_t readTime, int32_t deviceId, uint32_t source,
119                                  int32_t displayId, uint32_t policyFlags, int32_t lastButtonState,
120                                  int32_t currentButtonState) {
121     synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId, policyFlags,
122                         lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_BACK,
123                         AKEYCODE_BACK);
124     synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId, policyFlags,
125                         lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_FORWARD,
126                         AKEYCODE_FORWARD);
127 }
128 
129 } // namespace android
130 
131 #endif // _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
132