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 "TouchButtonAccumulator.h"
18 
19 #include "EventHub.h"
20 #include "InputDevice.h"
21 
22 namespace android {
23 
TouchButtonAccumulator()24 TouchButtonAccumulator::TouchButtonAccumulator() : mHaveBtnTouch(false), mHaveStylus(false) {
25     clearButtons();
26 }
27 
configure(InputDeviceContext & deviceContext)28 void TouchButtonAccumulator::configure(InputDeviceContext& deviceContext) {
29     mHaveBtnTouch = deviceContext.hasScanCode(BTN_TOUCH);
30     mHaveStylus = deviceContext.hasScanCode(BTN_TOOL_PEN) ||
31             deviceContext.hasScanCode(BTN_TOOL_RUBBER) ||
32             deviceContext.hasScanCode(BTN_TOOL_BRUSH) ||
33             deviceContext.hasScanCode(BTN_TOOL_PENCIL) ||
34             deviceContext.hasScanCode(BTN_TOOL_AIRBRUSH);
35 }
36 
reset(InputDeviceContext & deviceContext)37 void TouchButtonAccumulator::reset(InputDeviceContext& deviceContext) {
38     mBtnTouch = deviceContext.isKeyPressed(BTN_TOUCH);
39     mBtnStylus = deviceContext.isKeyPressed(BTN_STYLUS);
40     // BTN_0 is what gets mapped for the HID usage Digitizers.SecondaryBarrelSwitch
41     mBtnStylus2 = deviceContext.isKeyPressed(BTN_STYLUS2) || deviceContext.isKeyPressed(BTN_0);
42     mBtnToolFinger = deviceContext.isKeyPressed(BTN_TOOL_FINGER);
43     mBtnToolPen = deviceContext.isKeyPressed(BTN_TOOL_PEN);
44     mBtnToolRubber = deviceContext.isKeyPressed(BTN_TOOL_RUBBER);
45     mBtnToolBrush = deviceContext.isKeyPressed(BTN_TOOL_BRUSH);
46     mBtnToolPencil = deviceContext.isKeyPressed(BTN_TOOL_PENCIL);
47     mBtnToolAirbrush = deviceContext.isKeyPressed(BTN_TOOL_AIRBRUSH);
48     mBtnToolMouse = deviceContext.isKeyPressed(BTN_TOOL_MOUSE);
49     mBtnToolLens = deviceContext.isKeyPressed(BTN_TOOL_LENS);
50     mBtnToolDoubleTap = deviceContext.isKeyPressed(BTN_TOOL_DOUBLETAP);
51     mBtnToolTripleTap = deviceContext.isKeyPressed(BTN_TOOL_TRIPLETAP);
52     mBtnToolQuadTap = deviceContext.isKeyPressed(BTN_TOOL_QUADTAP);
53 }
54 
clearButtons()55 void TouchButtonAccumulator::clearButtons() {
56     mBtnTouch = 0;
57     mBtnStylus = 0;
58     mBtnStylus2 = 0;
59     mBtnToolFinger = 0;
60     mBtnToolPen = 0;
61     mBtnToolRubber = 0;
62     mBtnToolBrush = 0;
63     mBtnToolPencil = 0;
64     mBtnToolAirbrush = 0;
65     mBtnToolMouse = 0;
66     mBtnToolLens = 0;
67     mBtnToolDoubleTap = 0;
68     mBtnToolTripleTap = 0;
69     mBtnToolQuadTap = 0;
70 }
71 
process(const RawEvent * rawEvent)72 void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
73     if (rawEvent->type == EV_KEY) {
74         switch (rawEvent->code) {
75             case BTN_TOUCH:
76                 mBtnTouch = rawEvent->value;
77                 break;
78             case BTN_STYLUS:
79                 mBtnStylus = rawEvent->value;
80                 break;
81             case BTN_STYLUS2:
82             case BTN_0: // BTN_0 is what gets mapped for the HID usage
83                         // Digitizers.SecondaryBarrelSwitch
84                 mBtnStylus2 = rawEvent->value;
85                 break;
86             case BTN_TOOL_FINGER:
87                 mBtnToolFinger = rawEvent->value;
88                 break;
89             case BTN_TOOL_PEN:
90                 mBtnToolPen = rawEvent->value;
91                 break;
92             case BTN_TOOL_RUBBER:
93                 mBtnToolRubber = rawEvent->value;
94                 break;
95             case BTN_TOOL_BRUSH:
96                 mBtnToolBrush = rawEvent->value;
97                 break;
98             case BTN_TOOL_PENCIL:
99                 mBtnToolPencil = rawEvent->value;
100                 break;
101             case BTN_TOOL_AIRBRUSH:
102                 mBtnToolAirbrush = rawEvent->value;
103                 break;
104             case BTN_TOOL_MOUSE:
105                 mBtnToolMouse = rawEvent->value;
106                 break;
107             case BTN_TOOL_LENS:
108                 mBtnToolLens = rawEvent->value;
109                 break;
110             case BTN_TOOL_DOUBLETAP:
111                 mBtnToolDoubleTap = rawEvent->value;
112                 break;
113             case BTN_TOOL_TRIPLETAP:
114                 mBtnToolTripleTap = rawEvent->value;
115                 break;
116             case BTN_TOOL_QUADTAP:
117                 mBtnToolQuadTap = rawEvent->value;
118                 break;
119         }
120     }
121 }
122 
getButtonState() const123 uint32_t TouchButtonAccumulator::getButtonState() const {
124     uint32_t result = 0;
125     if (mBtnStylus) {
126         result |= AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
127     }
128     if (mBtnStylus2) {
129         result |= AMOTION_EVENT_BUTTON_STYLUS_SECONDARY;
130     }
131     return result;
132 }
133 
getToolType() const134 int32_t TouchButtonAccumulator::getToolType() const {
135     if (mBtnToolMouse || mBtnToolLens) {
136         return AMOTION_EVENT_TOOL_TYPE_MOUSE;
137     }
138     if (mBtnToolRubber) {
139         return AMOTION_EVENT_TOOL_TYPE_ERASER;
140     }
141     if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
142         return AMOTION_EVENT_TOOL_TYPE_STYLUS;
143     }
144     if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
145         return AMOTION_EVENT_TOOL_TYPE_FINGER;
146     }
147     return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
148 }
149 
isToolActive() const150 bool TouchButtonAccumulator::isToolActive() const {
151     return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber || mBtnToolBrush ||
152             mBtnToolPencil || mBtnToolAirbrush || mBtnToolMouse || mBtnToolLens ||
153             mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
154 }
155 
isHovering() const156 bool TouchButtonAccumulator::isHovering() const {
157     return mHaveBtnTouch && !mBtnTouch;
158 }
159 
hasStylus() const160 bool TouchButtonAccumulator::hasStylus() const {
161     return mHaveStylus;
162 }
163 
164 } // namespace android
165