summaryrefslogtreecommitdiff
path: root/modules/input/evdev/SwitchInputMapper.cpp
blob: bb79d01ec933a814beca6a2d2a6273860ae8910c (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
/*
 * Copyright (C) 2015 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.
 */

#define LOG_TAG "SwitchInputMapper"
//#define LOG_NDEBUG 0

#include "SwitchInputMapper.h"

#include <inttypes.h>
#include <linux/input.h>
#include <hardware/input.h>
#include <utils/Log.h>

#include "InputHost.h"
#include "InputHub.h"

namespace android {

static struct {
    int32_t scancode;
    InputUsage usage;
} codeMap[] = {
    {SW_LID, INPUT_USAGE_SWITCH_LID},
    {SW_TABLET_MODE, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_HEADPHONE_INSERT, INPUT_USAGE_SWITCH_HEADPHONE_INSERT},
    {SW_RFKILL_ALL, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_MICROPHONE_INSERT, INPUT_USAGE_SWITCH_MICROPHONE_INSERT},
    {SW_DOCK, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_LINEOUT_INSERT, INPUT_USAGE_SWITCH_LINEOUT_INSERT},
    {SW_JACK_PHYSICAL_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_VIDEOOUT_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_CAMERA_LENS_COVER, INPUT_USAGE_SWITCH_CAMERA_LENS_COVER},
    {SW_KEYPAD_SLIDE, INPUT_USAGE_SWITCH_KEYPAD_SLIDE},
    {SW_FRONT_PROXIMITY, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_ROTATE_LOCK, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_LINEIN_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
    {0x0e /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
    {SW_MAX, INPUT_USAGE_SWITCH_UNKNOWN},
};

SwitchInputMapper::SwitchInputMapper()
    : InputMapper() {
    static_assert(SW_CNT <= 32, "More than 32 switches defined in linux/input.h");
}

bool SwitchInputMapper::configureInputReport(InputDeviceNode* devNode,
        InputReportDefinition* report) {
    InputUsage usages[SW_CNT];
    int numUsages = 0;
    for (int32_t i = 0; i < SW_CNT; ++i) {
        if (devNode->hasSwitch(codeMap[i].scancode)) {
            usages[numUsages++] = codeMap[i].usage;
        }
    }
    if (numUsages == 0) {
        ALOGE("SwitchInputMapper found no switches for %s!", devNode->getPath().c_str());
        return false;
    }
    setInputReportDefinition(report);
    getInputReportDefinition()->addCollection(INPUT_COLLECTION_ID_SWITCH, 1);
    getInputReportDefinition()->declareUsages(INPUT_COLLECTION_ID_SWITCH, usages, numUsages);
    return true;
}

void SwitchInputMapper::process(const InputEvent& event) {
    switch (event.type) {
        case EV_SW:
            processSwitch(event.code, event.value);
            break;
        case EV_SYN:
            if (event.code == SYN_REPORT) {
                sync(event.when);
            }
            break;
        default:
            ALOGV("unknown switch event type: %d", event.type);
    }
}

void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
    ALOGV("processing switch event. code=%" PRId32 ", value=%" PRId32, switchCode, switchValue);
    if (switchCode >= 0 && switchCode < SW_CNT) {
        if (switchValue) {
            mSwitchValues.markBit(switchCode);
        } else {
            mSwitchValues.clearBit(switchCode);
        }
        mUpdatedSwitchMask.markBit(switchCode);
    }
}

void SwitchInputMapper::sync(nsecs_t when) {
    if (mUpdatedSwitchMask.isEmpty()) {
        // Clear the values just in case.
        mSwitchValues.clear();
        return;
    }

    while (!mUpdatedSwitchMask.isEmpty()) {
        auto bit = mUpdatedSwitchMask.firstMarkedBit();
        getInputReport()->setBoolUsage(INPUT_COLLECTION_ID_SWITCH, codeMap[bit].usage,
                mSwitchValues.hasBit(bit), 0);
        mUpdatedSwitchMask.clearBit(bit);
    }
    getInputReport()->reportEvent(getDeviceHandle());
    mUpdatedSwitchMask.clear();
    mSwitchValues.clear();
}

}  // namespace android