1 /*
2  * Copyright (C) 2019 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 #include "../Macros.h"
18 
19 #include "SwitchInputMapper.h"
20 
21 namespace android {
22 
SwitchInputMapper(InputDeviceContext & deviceContext,const InputReaderConfiguration & readerConfig)23 SwitchInputMapper::SwitchInputMapper(InputDeviceContext& deviceContext,
24                                      const InputReaderConfiguration& readerConfig)
25       : InputMapper(deviceContext, readerConfig), mSwitchValues(0), mUpdatedSwitchMask(0) {}
26 
~SwitchInputMapper()27 SwitchInputMapper::~SwitchInputMapper() {}
28 
getSources() const29 uint32_t SwitchInputMapper::getSources() const {
30     return AINPUT_SOURCE_SWITCH;
31 }
32 
process(const RawEvent & rawEvent)33 std::list<NotifyArgs> SwitchInputMapper::process(const RawEvent& rawEvent) {
34     std::list<NotifyArgs> out;
35     switch (rawEvent.type) {
36         case EV_SW:
37             processSwitch(rawEvent.code, rawEvent.value);
38             break;
39 
40         case EV_SYN:
41             if (rawEvent.code == SYN_REPORT) {
42                 out += sync(rawEvent.when);
43             }
44     }
45     return out;
46 }
47 
processSwitch(int32_t switchCode,int32_t switchValue)48 void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
49     if (switchCode >= 0 && switchCode < 32) {
50         if (switchValue) {
51             mSwitchValues |= 1 << switchCode;
52         } else {
53             mSwitchValues &= ~(1 << switchCode);
54         }
55         mUpdatedSwitchMask |= 1 << switchCode;
56     }
57 }
58 
sync(nsecs_t when)59 std::list<NotifyArgs> SwitchInputMapper::sync(nsecs_t when) {
60     std::list<NotifyArgs> out;
61     if (mUpdatedSwitchMask) {
62         uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask;
63         out.push_back(NotifySwitchArgs(getContext()->getNextId(), when, /*policyFlags=*/0,
64                                        updatedSwitchValues, mUpdatedSwitchMask));
65 
66         mUpdatedSwitchMask = 0;
67     }
68     return out;
69 }
70 
getSwitchState(uint32_t sourceMask,int32_t switchCode)71 int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
72     return getDeviceContext().getSwitchState(switchCode);
73 }
74 
dump(std::string & dump)75 void SwitchInputMapper::dump(std::string& dump) {
76     dump += INDENT2 "Switch Input Mapper:\n";
77     dump += StringPrintf(INDENT3 "SwitchValues: %x\n", mSwitchValues);
78 }
79 
80 } // namespace android
81