• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <input/DisplayViewport.h>
21 #include <stdint.h>
22 
23 #include "EventHub.h"
24 #include "InputListener.h"
25 #include "InputReaderContext.h"
26 
27 namespace android {
28 
29 // --- Static Definitions ---
30 
getInverseRotation(int32_t orientation)31 static int32_t getInverseRotation(int32_t orientation) {
32     switch (orientation) {
33         case DISPLAY_ORIENTATION_90:
34             return DISPLAY_ORIENTATION_270;
35         case DISPLAY_ORIENTATION_270:
36             return DISPLAY_ORIENTATION_90;
37         default:
38             return orientation;
39     }
40 }
41 
rotateDelta(int32_t orientation,float * deltaX,float * deltaY)42 static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
43     float temp;
44     switch (orientation) {
45         case DISPLAY_ORIENTATION_90:
46             temp = *deltaX;
47             *deltaX = *deltaY;
48             *deltaY = -temp;
49             break;
50 
51         case DISPLAY_ORIENTATION_180:
52             *deltaX = -*deltaX;
53             *deltaY = -*deltaY;
54             break;
55 
56         case DISPLAY_ORIENTATION_270:
57             temp = *deltaX;
58             *deltaX = -*deltaY;
59             *deltaY = temp;
60             break;
61 
62         default:
63             break;
64     }
65 }
66 
67 // Returns true if the pointer should be reported as being down given the specified
68 // button states.  This determines whether the event is reported as a touch event.
isPointerDown(int32_t buttonState)69 static bool isPointerDown(int32_t buttonState) {
70     return buttonState &
71             (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
72              AMOTION_EVENT_BUTTON_TERTIARY);
73 }
74 
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)75 static void synthesizeButtonKey(InputReaderContext* context, int32_t action, nsecs_t when,
76                                 nsecs_t readTime, int32_t deviceId, uint32_t source,
77                                 int32_t displayId, uint32_t policyFlags, int32_t lastButtonState,
78                                 int32_t currentButtonState, int32_t buttonState, int32_t keyCode) {
79     if ((action == AKEY_EVENT_ACTION_DOWN && !(lastButtonState & buttonState) &&
80          (currentButtonState & buttonState)) ||
81         (action == AKEY_EVENT_ACTION_UP && (lastButtonState & buttonState) &&
82          !(currentButtonState & buttonState))) {
83         NotifyKeyArgs args(context->getNextId(), when, readTime, deviceId, source, displayId,
84                            policyFlags, action, 0, keyCode, 0, context->getGlobalMetaState(), when);
85         context->getListener().notifyKey(&args);
86     }
87 }
88 
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)89 static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, nsecs_t when,
90                                  nsecs_t readTime, int32_t deviceId, uint32_t source,
91                                  int32_t displayId, uint32_t policyFlags, int32_t lastButtonState,
92                                  int32_t currentButtonState) {
93     synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId, policyFlags,
94                         lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_BACK,
95                         AKEYCODE_BACK);
96     synthesizeButtonKey(context, action, when, readTime, deviceId, source, displayId, policyFlags,
97                         lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_FORWARD,
98                         AKEYCODE_FORWARD);
99 }
100 
101 } // namespace android
102 
103 #endif // _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
104