| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 |  | 
| Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 17 | #include "../Macros.h" | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include "SwitchInputMapper.h" | 
|  | 20 |  | 
|  | 21 | namespace android { | 
|  | 22 |  | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 23 | SwitchInputMapper::SwitchInputMapper(InputDeviceContext& deviceContext) | 
|  | 24 | : InputMapper(deviceContext), mSwitchValues(0), mUpdatedSwitchMask(0) {} | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | SwitchInputMapper::~SwitchInputMapper() {} | 
|  | 27 |  | 
|  | 28 | uint32_t SwitchInputMapper::getSources() { | 
|  | 29 | return AINPUT_SOURCE_SWITCH; | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | void SwitchInputMapper::process(const RawEvent* rawEvent) { | 
|  | 33 | switch (rawEvent->type) { | 
|  | 34 | case EV_SW: | 
|  | 35 | processSwitch(rawEvent->code, rawEvent->value); | 
|  | 36 | break; | 
|  | 37 |  | 
|  | 38 | case EV_SYN: | 
|  | 39 | if (rawEvent->code == SYN_REPORT) { | 
|  | 40 | sync(rawEvent->when); | 
|  | 41 | } | 
|  | 42 | } | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) { | 
|  | 46 | if (switchCode >= 0 && switchCode < 32) { | 
|  | 47 | if (switchValue) { | 
|  | 48 | mSwitchValues |= 1 << switchCode; | 
|  | 49 | } else { | 
|  | 50 | mSwitchValues &= ~(1 << switchCode); | 
|  | 51 | } | 
|  | 52 | mUpdatedSwitchMask |= 1 << switchCode; | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | void SwitchInputMapper::sync(nsecs_t when) { | 
|  | 57 | if (mUpdatedSwitchMask) { | 
|  | 58 | uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask; | 
| Siarhei Vishniakou | cec3f6a | 2020-11-10 15:42:39 -0600 | [diff] [blame] | 59 | getContext()->notifySwitch(when, updatedSwitchValues, mUpdatedSwitchMask); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 60 | mUpdatedSwitchMask = 0; | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 65 | return getDeviceContext().getSwitchState(switchCode); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
|  | 68 | void SwitchInputMapper::dump(std::string& dump) { | 
|  | 69 | dump += INDENT2 "Switch Input Mapper:\n"; | 
|  | 70 | dump += StringPrintf(INDENT3 "SwitchValues: %x\n", mSwitchValues); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | } // namespace android |