blob: 028fc725c1d5df260ccda11a36b6bf54c46e11ae [file] [log] [blame]
Tim Kilbourn4f3145d2015-05-04 17:26:30 -07001/*
2 * Copyright (C) 2015 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#define LOG_TAG "SwitchInputMapper"
18//#define LOG_NDEBUG 0
19
20#include "SwitchInputMapper.h"
21
Michael Wrightfdd4d812015-11-25 16:00:28 +000022#include <inttypes.h>
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070023#include <linux/input.h>
24#include <hardware/input.h>
25#include <utils/Log.h>
26
27#include "InputHost.h"
28#include "InputHub.h"
29
30namespace android {
31
32static struct {
33 int32_t scancode;
34 InputUsage usage;
35} codeMap[] = {
36 {SW_LID, INPUT_USAGE_SWITCH_LID},
37 {SW_TABLET_MODE, INPUT_USAGE_SWITCH_UNKNOWN},
38 {SW_HEADPHONE_INSERT, INPUT_USAGE_SWITCH_HEADPHONE_INSERT},
39 {SW_RFKILL_ALL, INPUT_USAGE_SWITCH_UNKNOWN},
40 {SW_MICROPHONE_INSERT, INPUT_USAGE_SWITCH_MICROPHONE_INSERT},
41 {SW_DOCK, INPUT_USAGE_SWITCH_UNKNOWN},
42 {SW_LINEOUT_INSERT, INPUT_USAGE_SWITCH_LINEOUT_INSERT},
43 {SW_JACK_PHYSICAL_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
44 {SW_VIDEOOUT_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
45 {SW_CAMERA_LENS_COVER, INPUT_USAGE_SWITCH_CAMERA_LENS_COVER},
46 {SW_KEYPAD_SLIDE, INPUT_USAGE_SWITCH_KEYPAD_SLIDE},
47 {SW_FRONT_PROXIMITY, INPUT_USAGE_SWITCH_UNKNOWN},
48 {SW_ROTATE_LOCK, INPUT_USAGE_SWITCH_UNKNOWN},
49 {SW_LINEIN_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
Christopher Ferris8f102062019-12-10 12:31:21 -080050 {SW_MUTE_DEVICE, INPUT_USAGE_SWITCH_UNKNOWN},
51 {SW_PEN_INSERTED, INPUT_USAGE_SWITCH_UNKNOWN},
Christopher Ferris82dca312020-08-04 18:37:39 -070052 {SW_MACHINE_COVER, INPUT_USAGE_SWITCH_UNKNOWN},
53 {0x11 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
54 {0x12 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
Christopher Ferris8f102062019-12-10 12:31:21 -080055 {0x13 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
56 {0x14 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
57 {0x15 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
58 {0x16 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
59 {0x17 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
60 {0x18 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
61 {0x19 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
Christopher Ferris82dca312020-08-04 18:37:39 -070062 {0x1a /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
63 {0x1b /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
64 {0x1c /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
65 {0x1d /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
66 {0x1e /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
67 {0x1f /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
68 {0x20 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070069};
70
Christopher Ferris82dca312020-08-04 18:37:39 -070071static_assert(SW_MAX == SW_MACHINE_COVER, "SW_MAX is not SW_MACHINE_COVER");
72
73// This is the max value that any kernel has ever used. The v5.4 kernels
74// increased SW_MAX to 0x20, while v5.8 decreased the value to 0x10.
75static constexpr int32_t kMaxNumInputCodes = 0x21;
76
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070077SwitchInputMapper::SwitchInputMapper()
78 : InputMapper() {
Christopher Ferris8f102062019-12-10 12:31:21 -080079 // If this gets larger than 64, then the mSwitchValues and mUpdatedSwitchMask
80 // variables need to be changed to support more than 64 bits.
81 static_assert(SW_CNT <= 64, "More than 64 switches defined in linux/input.h");
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070082}
83
84bool SwitchInputMapper::configureInputReport(InputDeviceNode* devNode,
85 InputReportDefinition* report) {
Christopher Ferris82dca312020-08-04 18:37:39 -070086 InputUsage usages[kMaxNumInputCodes];
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070087 int numUsages = 0;
Christopher Ferris82dca312020-08-04 18:37:39 -070088 for (int32_t i = 0; i < kMaxNumInputCodes; ++i) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070089 if (devNode->hasSwitch(codeMap[i].scancode)) {
90 usages[numUsages++] = codeMap[i].usage;
91 }
92 }
93 if (numUsages == 0) {
94 ALOGE("SwitchInputMapper found no switches for %s!", devNode->getPath().c_str());
95 return false;
96 }
97 setInputReportDefinition(report);
98 getInputReportDefinition()->addCollection(INPUT_COLLECTION_ID_SWITCH, 1);
99 getInputReportDefinition()->declareUsages(INPUT_COLLECTION_ID_SWITCH, usages, numUsages);
100 return true;
101}
102
103void SwitchInputMapper::process(const InputEvent& event) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700104 switch (event.type) {
105 case EV_SW:
106 processSwitch(event.code, event.value);
107 break;
108 case EV_SYN:
109 if (event.code == SYN_REPORT) {
110 sync(event.when);
111 }
112 break;
113 default:
Michael Wrightfdd4d812015-11-25 16:00:28 +0000114 ALOGV("unknown switch event type: %d", event.type);
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700115 }
116}
117
118void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
Michael Wrightfdd4d812015-11-25 16:00:28 +0000119 ALOGV("processing switch event. code=%" PRId32 ", value=%" PRId32, switchCode, switchValue);
Christopher Ferris82dca312020-08-04 18:37:39 -0700120 if (switchCode >= 0 && switchCode < kMaxNumInputCodes) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700121 if (switchValue) {
122 mSwitchValues.markBit(switchCode);
123 } else {
124 mSwitchValues.clearBit(switchCode);
125 }
126 mUpdatedSwitchMask.markBit(switchCode);
127 }
128}
129
130void SwitchInputMapper::sync(nsecs_t when) {
131 if (mUpdatedSwitchMask.isEmpty()) {
132 // Clear the values just in case.
133 mSwitchValues.clear();
134 return;
135 }
136
137 while (!mUpdatedSwitchMask.isEmpty()) {
138 auto bit = mUpdatedSwitchMask.firstMarkedBit();
139 getInputReport()->setBoolUsage(INPUT_COLLECTION_ID_SWITCH, codeMap[bit].usage,
140 mSwitchValues.hasBit(bit), 0);
141 mUpdatedSwitchMask.clearBit(bit);
142 }
143 getInputReport()->reportEvent(getDeviceHandle());
144 mUpdatedSwitchMask.clear();
145 mSwitchValues.clear();
146}
147
148} // namespace android