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 #include "CursorButtonAccumulator.h"
18 
19 #include "EventHub.h"
20 #include "InputDevice.h"
21 
22 namespace android {
23 
CursorButtonAccumulator()24 CursorButtonAccumulator::CursorButtonAccumulator() {
25     clearButtons();
26 }
27 
reset(const InputDeviceContext & deviceContext)28 void CursorButtonAccumulator::reset(const InputDeviceContext& deviceContext) {
29     mBtnLeft = deviceContext.isKeyPressed(BTN_LEFT);
30     mBtnRight = deviceContext.isKeyPressed(BTN_RIGHT);
31     mBtnMiddle = deviceContext.isKeyPressed(BTN_MIDDLE);
32     mBtnBack = deviceContext.isKeyPressed(BTN_BACK);
33     mBtnSide = deviceContext.isKeyPressed(BTN_SIDE);
34     mBtnForward = deviceContext.isKeyPressed(BTN_FORWARD);
35     mBtnExtra = deviceContext.isKeyPressed(BTN_EXTRA);
36     mBtnTask = deviceContext.isKeyPressed(BTN_TASK);
37 }
38 
clearButtons()39 void CursorButtonAccumulator::clearButtons() {
40     mBtnLeft = 0;
41     mBtnRight = 0;
42     mBtnMiddle = 0;
43     mBtnBack = 0;
44     mBtnSide = 0;
45     mBtnForward = 0;
46     mBtnExtra = 0;
47     mBtnTask = 0;
48 }
49 
setSwapLeftRightButtons(bool shouldSwap)50 void CursorButtonAccumulator::setSwapLeftRightButtons(bool shouldSwap) {
51     mSwapLeftRightButtons = shouldSwap;
52 }
53 
process(const RawEvent & rawEvent)54 void CursorButtonAccumulator::process(const RawEvent& rawEvent) {
55     if (rawEvent.type == EV_KEY) {
56         switch (rawEvent.code) {
57             case BTN_LEFT:
58                 mBtnLeft = rawEvent.value;
59                 break;
60             case BTN_RIGHT:
61                 mBtnRight = rawEvent.value;
62                 break;
63             case BTN_MIDDLE:
64                 mBtnMiddle = rawEvent.value;
65                 break;
66             case BTN_BACK:
67                 mBtnBack = rawEvent.value;
68                 break;
69             case BTN_SIDE:
70                 mBtnSide = rawEvent.value;
71                 break;
72             case BTN_FORWARD:
73                 mBtnForward = rawEvent.value;
74                 break;
75             case BTN_EXTRA:
76                 mBtnExtra = rawEvent.value;
77                 break;
78             case BTN_TASK:
79                 mBtnTask = rawEvent.value;
80                 break;
81         }
82     }
83 }
84 
getButtonState() const85 uint32_t CursorButtonAccumulator::getButtonState() const {
86     uint32_t result = 0;
87     if (mBtnLeft) {
88         result |= mSwapLeftRightButtons ? AMOTION_EVENT_BUTTON_SECONDARY
89                                         : AMOTION_EVENT_BUTTON_PRIMARY;
90     }
91     if (mBtnRight) {
92         result |= mSwapLeftRightButtons ? AMOTION_EVENT_BUTTON_PRIMARY
93                                         : AMOTION_EVENT_BUTTON_SECONDARY;
94     }
95     if (mBtnMiddle) {
96         result |= AMOTION_EVENT_BUTTON_TERTIARY;
97     }
98     if (mBtnBack || mBtnSide) {
99         result |= AMOTION_EVENT_BUTTON_BACK;
100     }
101     if (mBtnForward || mBtnExtra) {
102         result |= AMOTION_EVENT_BUTTON_FORWARD;
103     }
104     return result;
105 }
106 
107 } // namespace android
108