1 /*
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
22 #include <inttypes.h>
23 #include <linux/input.h>
24 #include <hardware/input.h>
25 #include <utils/Log.h>
26
27 #include "InputHost.h"
28 #include "InputHub.h"
29
30 namespace android {
31
32 static 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},
50 {SW_MUTE_DEVICE, INPUT_USAGE_SWITCH_UNKNOWN},
51 {SW_PEN_INSERTED, INPUT_USAGE_SWITCH_UNKNOWN},
52 {SW_MACHINE_COVER, INPUT_USAGE_SWITCH_UNKNOWN},
53 {0x11 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
54 {0x12 /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
55 {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},
62 {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},
69 };
70
71 static_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.
75 static constexpr int32_t kMaxNumInputCodes = 0x21;
76
SwitchInputMapper()77 SwitchInputMapper::SwitchInputMapper()
78 : InputMapper() {
79 // 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");
82 }
83
configureInputReport(InputDeviceNode * devNode,InputReportDefinition * report)84 bool SwitchInputMapper::configureInputReport(InputDeviceNode* devNode,
85 InputReportDefinition* report) {
86 InputUsage usages[kMaxNumInputCodes];
87 int numUsages = 0;
88 for (int32_t i = 0; i < kMaxNumInputCodes; ++i) {
89 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
process(const InputEvent & event)103 void SwitchInputMapper::process(const InputEvent& event) {
104 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:
114 ALOGV("unknown switch event type: %d", event.type);
115 }
116 }
117
processSwitch(int32_t switchCode,int32_t switchValue)118 void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
119 ALOGV("processing switch event. code=%" PRId32 ", value=%" PRId32, switchCode, switchValue);
120 if (switchCode >= 0 && switchCode < kMaxNumInputCodes) {
121 if (switchValue) {
122 mSwitchValues.markBit(switchCode);
123 } else {
124 mSwitchValues.clearBit(switchCode);
125 }
126 mUpdatedSwitchMask.markBit(switchCode);
127 }
128 }
129
sync(nsecs_t when)130 void 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
149