Extract HID usage accumulation logic from KeyboardInputMapper
Extract the logic into a HidUsageAccumulator class so that when HID
usage is used in more than one place, the usage code accumulation logic
maintains the same behavior.
There should be no behavior change in this CL.
Bug: 246394583
Test: atest inputflinger_tests
Change-Id: Ia5f9bcac145f07c0d0dca643755dcff1b725d568
diff --git a/services/inputflinger/reader/Android.bp b/services/inputflinger/reader/Android.bp
index c5e1f0c..a53fcd7 100644
--- a/services/inputflinger/reader/Android.bp
+++ b/services/inputflinger/reader/Android.bp
@@ -54,6 +54,7 @@
"mapper/VibratorInputMapper.cpp",
"mapper/accumulator/CursorButtonAccumulator.cpp",
"mapper/accumulator/CursorScrollAccumulator.cpp",
+ "mapper/accumulator/HidUsageAccumulator.cpp",
"mapper/accumulator/SingleTouchMotionAccumulator.cpp",
"mapper/accumulator/TouchButtonAccumulator.cpp",
],
diff --git a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
index 33b8a1b..da9413e 100644
--- a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
@@ -180,7 +180,7 @@
std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
std::list<NotifyArgs> out = cancelAllDownKeys(when);
- mCurrentHidUsage = 0;
+ mHidUsageAccumulator.reset();
resetLedState();
@@ -190,29 +190,17 @@
std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
std::list<NotifyArgs> out;
+ mHidUsageAccumulator.process(*rawEvent);
switch (rawEvent->type) {
case EV_KEY: {
int32_t scanCode = rawEvent->code;
- int32_t usageCode = mCurrentHidUsage;
- mCurrentHidUsage = 0;
if (isSupportedScanCode(scanCode)) {
out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
- scanCode, usageCode);
+ scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
}
break;
}
- case EV_MSC: {
- if (rawEvent->code == MSC_SCAN) {
- mCurrentHidUsage = rawEvent->value;
- }
- break;
- }
- case EV_SYN: {
- if (rawEvent->code == SYN_REPORT) {
- mCurrentHidUsage = 0;
- }
- }
}
return out;
}
diff --git a/services/inputflinger/reader/mapper/KeyboardInputMapper.h b/services/inputflinger/reader/mapper/KeyboardInputMapper.h
index 3dd570d..11d5ad2 100644
--- a/services/inputflinger/reader/mapper/KeyboardInputMapper.h
+++ b/services/inputflinger/reader/mapper/KeyboardInputMapper.h
@@ -16,6 +16,7 @@
#pragma once
+#include "HidUsageAccumulator.h"
#include "InputMapper.h"
namespace android {
@@ -61,7 +62,7 @@
std::vector<KeyDown> mKeyDowns{}; // keys that are down
int32_t mMetaState{};
- int32_t mCurrentHidUsage{}; // most recent HID usage seen this packet, or 0 if none
+ HidUsageAccumulator mHidUsageAccumulator;
struct LedState {
bool avail{}; // led is available
diff --git a/services/inputflinger/reader/mapper/accumulator/HidUsageAccumulator.cpp b/services/inputflinger/reader/mapper/accumulator/HidUsageAccumulator.cpp
new file mode 100644
index 0000000..2da1d81
--- /dev/null
+++ b/services/inputflinger/reader/mapper/accumulator/HidUsageAccumulator.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "HidUsageAccumulator.h"
+
+namespace android {
+
+void HidUsageAccumulator::process(const RawEvent& rawEvent) {
+ if (rawEvent.type == EV_MSC && rawEvent.code == MSC_SCAN) {
+ mCurrentHidUsage = rawEvent.value;
+ return;
+ }
+
+ if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) {
+ reset();
+ return;
+ }
+}
+
+int32_t HidUsageAccumulator::consumeCurrentHidUsage() {
+ const int32_t currentHidUsage = mCurrentHidUsage;
+ reset();
+ return currentHidUsage;
+}
+
+} // namespace android
diff --git a/services/inputflinger/reader/mapper/accumulator/HidUsageAccumulator.h b/services/inputflinger/reader/mapper/accumulator/HidUsageAccumulator.h
new file mode 100644
index 0000000..740a710
--- /dev/null
+++ b/services/inputflinger/reader/mapper/accumulator/HidUsageAccumulator.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "EventHub.h"
+
+#include <cstdint>
+
+namespace android {
+
+/* Keeps track of the state of currently reported HID usage code. */
+class HidUsageAccumulator {
+public:
+ explicit HidUsageAccumulator() = default;
+ inline void reset() { *this = HidUsageAccumulator(); }
+
+ void process(const RawEvent& rawEvent);
+
+ /* This must be called when processing the `EV_KEY` event. Returns 0 if invalid. */
+ int32_t consumeCurrentHidUsage();
+
+private:
+ int32_t mCurrentHidUsage{};
+};
+
+} // namespace android