blob: c4564a4380ab6001624861c7c2c019d370b902c5 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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 Pradhan9244aea2020-02-05 20:31:40 -080017#include "../Macros.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
19#include "SwitchInputMapper.h"
20
21namespace android {
22
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080023SwitchInputMapper::SwitchInputMapper(InputDeviceContext& deviceContext)
24 : InputMapper(deviceContext), mSwitchValues(0), mUpdatedSwitchMask(0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025
26SwitchInputMapper::~SwitchInputMapper() {}
27
Philip Junker4af3b3d2021-12-14 10:36:55 +010028uint32_t SwitchInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029 return AINPUT_SOURCE_SWITCH;
30}
31
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070032std::list<NotifyArgs> SwitchInputMapper::process(const RawEvent* rawEvent) {
33 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070034 switch (rawEvent->type) {
35 case EV_SW:
36 processSwitch(rawEvent->code, rawEvent->value);
37 break;
38
39 case EV_SYN:
40 if (rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070041 out += sync(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042 }
43 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070044 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070045}
46
47void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
48 if (switchCode >= 0 && switchCode < 32) {
49 if (switchValue) {
50 mSwitchValues |= 1 << switchCode;
51 } else {
52 mSwitchValues &= ~(1 << switchCode);
53 }
54 mUpdatedSwitchMask |= 1 << switchCode;
55 }
56}
57
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070058std::list<NotifyArgs> SwitchInputMapper::sync(nsecs_t when) {
59 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060 if (mUpdatedSwitchMask) {
61 uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask;
Harry Cutts33476232023-01-30 19:57:29 +000062 out.push_back(NotifySwitchArgs(getContext()->getNextId(), when, /*policyFlags=*/0,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070063 updatedSwitchValues, mUpdatedSwitchMask));
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +000064
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065 mUpdatedSwitchMask = 0;
66 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070067 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068}
69
70int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080071 return getDeviceContext().getSwitchState(switchCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072}
73
74void SwitchInputMapper::dump(std::string& dump) {
75 dump += INDENT2 "Switch Input Mapper:\n";
76 dump += StringPrintf(INDENT3 "SwitchValues: %x\n", mSwitchValues);
77}
78
79} // namespace android