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 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 23 | SwitchInputMapper::SwitchInputMapper(InputDeviceContext& deviceContext, |
| 24 | const InputReaderConfiguration& readerConfig) |
| 25 | : InputMapper(deviceContext, readerConfig), mSwitchValues(0), mUpdatedSwitchMask(0) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 26 | |
| 27 | SwitchInputMapper::~SwitchInputMapper() {} |
| 28 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 29 | uint32_t SwitchInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 30 | return AINPUT_SOURCE_SWITCH; |
| 31 | } |
| 32 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 33 | std::list<NotifyArgs> SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 34 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 42 | out += sync(rawEvent->when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 45 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 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 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 59 | std::list<NotifyArgs> SwitchInputMapper::sync(nsecs_t when) { |
| 60 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 61 | if (mUpdatedSwitchMask) { |
| 62 | uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 63 | out.push_back(NotifySwitchArgs(getContext()->getNextId(), when, /*policyFlags=*/0, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 64 | updatedSwitchValues, mUpdatedSwitchMask)); |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 65 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 66 | mUpdatedSwitchMask = 0; |
| 67 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 68 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 72 | return getDeviceContext().getSwitchState(switchCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 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 |