summaryrefslogtreecommitdiff
path: root/services/inputflinger/reader/mapper/accumulator/TouchButtonAccumulator.cpp
blob: 8c4bed32677597c84eb5cd3e80e352ae8c95d64c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "TouchButtonAccumulator.h"

#include "EventHub.h"
#include "InputDevice.h"

namespace android {

void TouchButtonAccumulator::configure() {
    mHaveBtnTouch = mDeviceContext.hasScanCode(BTN_TOUCH);
    mHaveStylus = mDeviceContext.hasScanCode(BTN_TOOL_PEN) ||
            mDeviceContext.hasScanCode(BTN_TOOL_RUBBER) ||
            mDeviceContext.hasScanCode(BTN_TOOL_BRUSH) ||
            mDeviceContext.hasScanCode(BTN_TOOL_PENCIL) ||
            mDeviceContext.hasScanCode(BTN_TOOL_AIRBRUSH);
}

void TouchButtonAccumulator::reset() {
    mBtnTouch = mDeviceContext.isKeyPressed(BTN_TOUCH);
    mBtnStylus = mDeviceContext.isKeyPressed(BTN_STYLUS) ||
            mDeviceContext.isKeyCodePressed(AKEYCODE_STYLUS_BUTTON_PRIMARY);
    // BTN_0 is what gets mapped for the HID usage Digitizers.SecondaryBarrelSwitch
    mBtnStylus2 = mDeviceContext.isKeyPressed(BTN_STYLUS2) || mDeviceContext.isKeyPressed(BTN_0) ||
            mDeviceContext.isKeyCodePressed(AKEYCODE_STYLUS_BUTTON_SECONDARY);
    mBtnToolFinger = mDeviceContext.isKeyPressed(BTN_TOOL_FINGER);
    mBtnToolPen = mDeviceContext.isKeyPressed(BTN_TOOL_PEN);
    mBtnToolRubber = mDeviceContext.isKeyPressed(BTN_TOOL_RUBBER);
    mBtnToolBrush = mDeviceContext.isKeyPressed(BTN_TOOL_BRUSH);
    mBtnToolPencil = mDeviceContext.isKeyPressed(BTN_TOOL_PENCIL);
    mBtnToolAirbrush = mDeviceContext.isKeyPressed(BTN_TOOL_AIRBRUSH);
    mBtnToolMouse = mDeviceContext.isKeyPressed(BTN_TOOL_MOUSE);
    mBtnToolLens = mDeviceContext.isKeyPressed(BTN_TOOL_LENS);
    mBtnToolDoubleTap = mDeviceContext.isKeyPressed(BTN_TOOL_DOUBLETAP);
    mBtnToolTripleTap = mDeviceContext.isKeyPressed(BTN_TOOL_TRIPLETAP);
    mBtnToolQuadTap = mDeviceContext.isKeyPressed(BTN_TOOL_QUADTAP);
    mBtnToolQuintTap = mDeviceContext.isKeyPressed(BTN_TOOL_QUINTTAP);
    mHidUsageAccumulator.reset();
}

void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
    mHidUsageAccumulator.process(*rawEvent);

    if (rawEvent->type == EV_KEY) {
        switch (rawEvent->code) {
            case BTN_TOUCH:
                mBtnTouch = rawEvent->value;
                break;
            case BTN_STYLUS:
                mBtnStylus = rawEvent->value;
                break;
            case BTN_STYLUS2:
            case BTN_0: // BTN_0 is what gets mapped for the HID usage
                        // Digitizers.SecondaryBarrelSwitch
                mBtnStylus2 = rawEvent->value;
                break;
            case BTN_TOOL_FINGER:
                mBtnToolFinger = rawEvent->value;
                break;
            case BTN_TOOL_PEN:
                mBtnToolPen = rawEvent->value;
                break;
            case BTN_TOOL_RUBBER:
                mBtnToolRubber = rawEvent->value;
                break;
            case BTN_TOOL_BRUSH:
                mBtnToolBrush = rawEvent->value;
                break;
            case BTN_TOOL_PENCIL:
                mBtnToolPencil = rawEvent->value;
                break;
            case BTN_TOOL_AIRBRUSH:
                mBtnToolAirbrush = rawEvent->value;
                break;
            case BTN_TOOL_MOUSE:
                mBtnToolMouse = rawEvent->value;
                break;
            case BTN_TOOL_LENS:
                mBtnToolLens = rawEvent->value;
                break;
            case BTN_TOOL_DOUBLETAP:
                mBtnToolDoubleTap = rawEvent->value;
                break;
            case BTN_TOOL_TRIPLETAP:
                mBtnToolTripleTap = rawEvent->value;
                break;
            case BTN_TOOL_QUADTAP:
                mBtnToolQuadTap = rawEvent->value;
                break;
            case BTN_TOOL_QUINTTAP:
                mBtnToolQuintTap = rawEvent->value;
                break;
            default:
                processMappedKey(rawEvent->code, rawEvent->value);
        }
        return;
    }
}

void TouchButtonAccumulator::processMappedKey(int32_t scanCode, bool down) {
    int32_t keyCode, metaState;
    uint32_t flags;
    if (mDeviceContext.mapKey(scanCode, mHidUsageAccumulator.consumeCurrentHidUsage(),
                              /*metaState=*/0, &keyCode, &metaState, &flags) != OK) {
        return;
    }
    switch (keyCode) {
        case AKEYCODE_STYLUS_BUTTON_PRIMARY:
            mBtnStylus = down;
            break;
        case AKEYCODE_STYLUS_BUTTON_SECONDARY:
            mBtnStylus2 = down;
            break;
        default:
            break;
    }
}

uint32_t TouchButtonAccumulator::getButtonState() const {
    uint32_t result = 0;
    if (mBtnStylus) {
        result |= AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
    }
    if (mBtnStylus2) {
        result |= AMOTION_EVENT_BUTTON_STYLUS_SECONDARY;
    }
    return result;
}

ToolType TouchButtonAccumulator::getToolType() const {
    if (mBtnToolMouse || mBtnToolLens) {
        return ToolType::MOUSE;
    }
    if (mBtnToolRubber) {
        return ToolType::ERASER;
    }
    if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
        return ToolType::STYLUS;
    }
    if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap ||
        mBtnToolQuintTap) {
        return ToolType::FINGER;
    }
    return ToolType::UNKNOWN;
}

bool TouchButtonAccumulator::isToolActive() const {
    return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber || mBtnToolBrush ||
            mBtnToolPencil || mBtnToolAirbrush || mBtnToolMouse || mBtnToolLens ||
            mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap || mBtnToolQuintTap;
}

bool TouchButtonAccumulator::isHovering() const {
    return mHaveBtnTouch && !mBtnTouch;
}

bool TouchButtonAccumulator::hasStylus() const {
    return mHaveStylus;
}

bool TouchButtonAccumulator::hasButtonTouch() const {
    return mHaveBtnTouch;
}

int TouchButtonAccumulator::getTouchCount() const {
    if (mBtnTouch) {
        if (mBtnToolQuintTap) return 5;
        if (mBtnToolQuadTap) return 4;
        if (mBtnToolTripleTap) return 3;
        if (mBtnToolDoubleTap) return 2;
        if (mBtnToolFinger) return 1;
    }
    return 0;
}

} // namespace android