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