summaryrefslogtreecommitdiff
path: root/services/inputflinger/reader/mapper/SwitchInputMapper.cpp
blob: 05338da146652296964c79734b9ab07ac3dce794 (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
/*
 * 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 "../Macros.h"

#include "SwitchInputMapper.h"

namespace android {

SwitchInputMapper::SwitchInputMapper(InputDeviceContext& deviceContext,
                                     const InputReaderConfiguration& readerConfig)
      : InputMapper(deviceContext, readerConfig), mSwitchValues(0), mUpdatedSwitchMask(0) {}

SwitchInputMapper::~SwitchInputMapper() {}

uint32_t SwitchInputMapper::getSources() const {
    return AINPUT_SOURCE_SWITCH;
}

std::list<NotifyArgs> SwitchInputMapper::process(const RawEvent* rawEvent) {
    std::list<NotifyArgs> out;
    switch (rawEvent->type) {
        case EV_SW:
            processSwitch(rawEvent->code, rawEvent->value);
            break;

        case EV_SYN:
            if (rawEvent->code == SYN_REPORT) {
                out += sync(rawEvent->when);
            }
    }
    return out;
}

void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
    if (switchCode >= 0 && switchCode < 32) {
        if (switchValue) {
            mSwitchValues |= 1 << switchCode;
        } else {
            mSwitchValues &= ~(1 << switchCode);
        }
        mUpdatedSwitchMask |= 1 << switchCode;
    }
}

std::list<NotifyArgs> SwitchInputMapper::sync(nsecs_t when) {
    std::list<NotifyArgs> out;
    if (mUpdatedSwitchMask) {
        uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask;
        out.push_back(NotifySwitchArgs(getContext()->getNextId(), when, /*policyFlags=*/0,
                                       updatedSwitchValues, mUpdatedSwitchMask));

        mUpdatedSwitchMask = 0;
    }
    return out;
}

int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
    return getDeviceContext().getSwitchState(switchCode);
}

void SwitchInputMapper::dump(std::string& dump) {
    dump += INDENT2 "Switch Input Mapper:\n";
    dump += StringPrintf(INDENT3 "SwitchValues: %x\n", mSwitchValues);
}

} // namespace android