summaryrefslogtreecommitdiff
path: root/services/inputflinger/reader/mapper/accumulator/MultiTouchMotionAccumulator.h
blob: 5b55e3d599ea9612b524cc61a8fb1862595a03a4 (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
/*
 * Copyright (C) 2022 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.
 */

#pragma once

#include <linux/input-event-codes.h>
#include <stdint.h>
#include <vector>

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

namespace android {

/* Keeps track of the state of multi-touch protocol. */
class MultiTouchMotionAccumulator {
public:
    class Slot {
    public:
        inline bool isInUse() const { return mInUse; }
        inline int32_t getX() const { return mAbsMtPositionX; }
        inline int32_t getY() const { return mAbsMtPositionY; }
        inline int32_t getTouchMajor() const { return mAbsMtTouchMajor; }
        inline int32_t getTouchMinor() const {
            return mHaveAbsMtTouchMinor ? mAbsMtTouchMinor : mAbsMtTouchMajor;
        }
        inline int32_t getToolMajor() const { return mAbsMtWidthMajor; }
        inline int32_t getToolMinor() const {
            return mHaveAbsMtWidthMinor ? mAbsMtWidthMinor : mAbsMtWidthMajor;
        }
        inline int32_t getOrientation() const { return mAbsMtOrientation; }
        inline int32_t getTrackingId() const { return mAbsMtTrackingId; }
        inline int32_t getPressure() const { return mAbsMtPressure; }
        inline int32_t getDistance() const { return mAbsMtDistance; }
        ToolType getToolType() const;

    private:
        friend class MultiTouchMotionAccumulator;

        bool mInUse = false;
        bool mHaveAbsMtTouchMinor = false;
        bool mHaveAbsMtWidthMinor = false;
        bool mHaveAbsMtToolType = false;

        int32_t mAbsMtPositionX = 0;
        int32_t mAbsMtPositionY = 0;
        int32_t mAbsMtTouchMajor = 0;
        int32_t mAbsMtTouchMinor = 0;
        int32_t mAbsMtWidthMajor = 0;
        int32_t mAbsMtWidthMinor = 0;
        int32_t mAbsMtOrientation = 0;
        int32_t mAbsMtTrackingId = -1;
        int32_t mAbsMtPressure = 0;
        int32_t mAbsMtDistance = 0;
        int32_t mAbsMtToolType = 0;

        void clear() { *this = Slot(); }
    };

    MultiTouchMotionAccumulator();

    void configure(const InputDeviceContext& deviceContext, size_t slotCount,
                   bool usingSlotsProtocol);
    void process(const RawEvent* rawEvent);
    void finishSync();

    size_t getActiveSlotsCount() const;
    inline size_t getSlotCount() const { return mSlots.size(); }
    inline const Slot& getSlot(size_t index) const {
        LOG_ALWAYS_FATAL_IF(index < 0 || index >= mSlots.size(), "Invalid index: %zu", index);
        return mSlots[index];
    }
    void reset(const InputDeviceContext& deviceContext);

private:
    int32_t mCurrentSlot;
    std::vector<Slot> mSlots;
    bool mUsingSlotsProtocol;

    void resetSlots();
    void warnIfNotInUse(const RawEvent& event, const Slot& slot);
};

} // namespace android