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 
process(const RawEvent & rawEvent)50 void CursorButtonAccumulator::process(const RawEvent& rawEvent) {
51     if (rawEvent.type == EV_KEY) {
52         switch (rawEvent.code) {
53             case BTN_LEFT:
54                 mBtnLeft = rawEvent.value;
55                 break;
56             case BTN_RIGHT:
57                 mBtnRight = rawEvent.value;
58                 break;
59             case BTN_MIDDLE:
60                 mBtnMiddle = rawEvent.value;
61                 break;
62             case BTN_BACK:
63                 mBtnBack = rawEvent.value;
64                 break;
65             case BTN_SIDE:
66                 mBtnSide = rawEvent.value;
67                 break;
68             case BTN_FORWARD:
69                 mBtnForward = rawEvent.value;
70                 break;
71             case BTN_EXTRA:
72                 mBtnExtra = rawEvent.value;
73                 break;
74             case BTN_TASK:
75                 mBtnTask = rawEvent.value;
76                 break;
77         }
78     }
79 }
80 
getButtonState() const81 uint32_t CursorButtonAccumulator::getButtonState() const {
82     uint32_t result = 0;
83     if (mBtnLeft) {
84         result |= AMOTION_EVENT_BUTTON_PRIMARY;
85     }
86     if (mBtnRight) {
87         result |= AMOTION_EVENT_BUTTON_SECONDARY;
88     }
89     if (mBtnMiddle) {
90         result |= AMOTION_EVENT_BUTTON_TERTIARY;
91     }
92     if (mBtnBack || mBtnSide) {
93         result |= AMOTION_EVENT_BUTTON_BACK;
94     }
95     if (mBtnForward || mBtnExtra) {
96         result |= AMOTION_EVENT_BUTTON_FORWARD;
97     }
98     return result;
99 }
100 
101 } // namespace android
102