blob: adc2f63bb3fb02d785fd2b0f3ba9886b013e5975 [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
22#include <linux/input.h>
23#include <hardware/input.h>
24#include <utils/Log.h>
25
26#include "InputHost.h"
27#include "InputHub.h"
28
29namespace android {
30
31static struct {
32 int32_t scancode;
33 InputUsage usage;
34} codeMap[] = {
35 {SW_LID, INPUT_USAGE_SWITCH_LID},
36 {SW_TABLET_MODE, INPUT_USAGE_SWITCH_UNKNOWN},
37 {SW_HEADPHONE_INSERT, INPUT_USAGE_SWITCH_HEADPHONE_INSERT},
38 {SW_RFKILL_ALL, INPUT_USAGE_SWITCH_UNKNOWN},
39 {SW_MICROPHONE_INSERT, INPUT_USAGE_SWITCH_MICROPHONE_INSERT},
40 {SW_DOCK, INPUT_USAGE_SWITCH_UNKNOWN},
41 {SW_LINEOUT_INSERT, INPUT_USAGE_SWITCH_LINEOUT_INSERT},
42 {SW_JACK_PHYSICAL_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
43 {SW_VIDEOOUT_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
44 {SW_CAMERA_LENS_COVER, INPUT_USAGE_SWITCH_CAMERA_LENS_COVER},
45 {SW_KEYPAD_SLIDE, INPUT_USAGE_SWITCH_KEYPAD_SLIDE},
46 {SW_FRONT_PROXIMITY, INPUT_USAGE_SWITCH_UNKNOWN},
47 {SW_ROTATE_LOCK, INPUT_USAGE_SWITCH_UNKNOWN},
48 {SW_LINEIN_INSERT, INPUT_USAGE_SWITCH_UNKNOWN},
49 {0x0e /* unused */, INPUT_USAGE_SWITCH_UNKNOWN},
50 {SW_MAX, INPUT_USAGE_SWITCH_UNKNOWN},
51};
52
53SwitchInputMapper::SwitchInputMapper()
54 : InputMapper() {
55 static_assert(SW_CNT <= 32, "More than 32 switches defined in linux/input.h");
56}
57
58bool SwitchInputMapper::configureInputReport(InputDeviceNode* devNode,
59 InputReportDefinition* report) {
60 InputUsage usages[SW_CNT];
61 int numUsages = 0;
62 for (int32_t i = 0; i < SW_CNT; ++i) {
63 if (devNode->hasSwitch(codeMap[i].scancode)) {
64 usages[numUsages++] = codeMap[i].usage;
65 }
66 }
67 if (numUsages == 0) {
68 ALOGE("SwitchInputMapper found no switches for %s!", devNode->getPath().c_str());
69 return false;
70 }
71 setInputReportDefinition(report);
72 getInputReportDefinition()->addCollection(INPUT_COLLECTION_ID_SWITCH, 1);
73 getInputReportDefinition()->declareUsages(INPUT_COLLECTION_ID_SWITCH, usages, numUsages);
74 return true;
75}
76
77void SwitchInputMapper::process(const InputEvent& event) {
78 ALOGD("processing switch event. type=%d code=%d value=%d",
79 event.type, event.code, event.value);
80 switch (event.type) {
81 case EV_SW:
82 processSwitch(event.code, event.value);
83 break;
84 case EV_SYN:
85 if (event.code == SYN_REPORT) {
86 sync(event.when);
87 }
88 break;
89 default:
90 ALOGD("unknown switch event type: %d", event.type);
91 }
92}
93
94void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
95 if (switchCode >= 0 && switchCode < SW_CNT) {
96 if (switchValue) {
97 mSwitchValues.markBit(switchCode);
98 } else {
99 mSwitchValues.clearBit(switchCode);
100 }
101 mUpdatedSwitchMask.markBit(switchCode);
102 }
103}
104
105void SwitchInputMapper::sync(nsecs_t when) {
106 if (mUpdatedSwitchMask.isEmpty()) {
107 // Clear the values just in case.
108 mSwitchValues.clear();
109 return;
110 }
111
112 while (!mUpdatedSwitchMask.isEmpty()) {
113 auto bit = mUpdatedSwitchMask.firstMarkedBit();
114 getInputReport()->setBoolUsage(INPUT_COLLECTION_ID_SWITCH, codeMap[bit].usage,
115 mSwitchValues.hasBit(bit), 0);
116 mUpdatedSwitchMask.clearBit(bit);
117 }
118 getInputReport()->reportEvent(getDeviceHandle());
119 mUpdatedSwitchMask.clear();
120 mSwitchValues.clear();
121}
122
123} // namespace android