From 8743f18757a16e4c107d36c21b1dca3b4a844d82 Mon Sep 17 00:00:00 2001 From: Harry Cutts Date: Wed, 17 May 2023 12:03:49 +0000 Subject: GestureConverter: add finger count axis for multi-finger swipes Consumers of multi-finger swipes (i.e. SysUI) previously had to wait until the first MOVE event to find out how many fingers were swiping, even though in the framework we knew this when sending the DOWN event. Adding this as an axis on the DOWN event should let SysUI simplify their code. Bug: 283093437 Test: atest inputflinger_tests:GestureConverterTest Test: modify InputDispatcher to send multi-finger swipes to apps too, then check reported values in a test app Change-Id: I34d1fdf096c49d7eb9b5d8ebd64427eb4e5db1f4 --- libs/input/InputEventLabels.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libs/input/InputEventLabels.cpp') diff --git a/libs/input/InputEventLabels.cpp b/libs/input/InputEventLabels.cpp index f99a7d640e..1c7cc129bd 100644 --- a/libs/input/InputEventLabels.cpp +++ b/libs/input/InputEventLabels.cpp @@ -404,7 +404,8 @@ namespace android { DEFINE_AXIS(GESTURE_Y_OFFSET), \ DEFINE_AXIS(GESTURE_SCROLL_X_DISTANCE), \ DEFINE_AXIS(GESTURE_SCROLL_Y_DISTANCE), \ - DEFINE_AXIS(GESTURE_PINCH_SCALE_FACTOR) + DEFINE_AXIS(GESTURE_PINCH_SCALE_FACTOR), \ + DEFINE_AXIS(GESTURE_SWIPE_FINGER_COUNT) // NOTE: If you add new LEDs here, you must also add them to Input.h #define LEDS_SEQUENCE \ -- cgit v1.2.3-59-g8ed1b From ffe62933bc0c0b2340b3d69dc202deaea2317ab0 Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Thu, 15 Jun 2023 19:46:30 -0700 Subject: Initialize InputEventLookup in a static function Currently, the InputEventLookup class uses static fields, which means that they are getting initialized early. This causes issues with address sanitizers. The sanitizers report a memory leak with the purely static approach. It's not clear whether this is an actual issue, or just a bug elsewhere in the system. To get around these problems, create a singleton instance of InputEventLookup. This singleton will be created upon the first access to any of its static functions. This helps resolves the sanitizer issues. Bug: 271455682 Test: FUZZER=inputflinger_input_dispatcher_fuzzer; m $FUZZER && out/host/linux-x86/fuzz/x86_64/$FUZZER/$FUZZER Note: test requires more CLs that are upcoming Change-Id: Id655554db9c706f5d45eaebbee55e12519dcfdc8 --- include/input/InputEventLabels.h | 28 ++++++++++++++++++++++------ libs/input/InputEventLabels.cpp | 37 ++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 23 deletions(-) (limited to 'libs/input/InputEventLabels.cpp') diff --git a/include/input/InputEventLabels.h b/include/input/InputEventLabels.h index 9dedd2b2da..909bf087dd 100644 --- a/include/input/InputEventLabels.h +++ b/include/input/InputEventLabels.h @@ -40,7 +40,16 @@ struct EvdevEventLabel { // then you must add it to InputEventLabels.cpp. class InputEventLookup { + /** + * This class is not purely static, but uses a singleton pattern in order to delay the + * initialization of the maps that it contains. If it were purely static, the maps could be + * created early, and would cause sanitizers to report memory leaks. + */ public: + InputEventLookup(InputEventLookup& other) = delete; + + void operator=(const InputEventLookup&) = delete; + static std::optional lookupValueByLabel(const std::unordered_map& map, const char* literal); @@ -61,17 +70,24 @@ public: static EvdevEventLabel getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value); private: - static const std::unordered_map KEYCODES; + InputEventLookup(); + + static const InputEventLookup& get() { + static InputEventLookup sLookup; + return sLookup; + } + + const std::unordered_map KEYCODES; - static const std::vector KEY_NAMES; + const std::vector KEY_NAMES; - static const std::unordered_map AXES; + const std::unordered_map AXES; - static const std::vector AXES_NAMES; + const std::vector AXES_NAMES; - static const std::unordered_map LEDS; + const std::unordered_map LEDS; - static const std::unordered_map FLAGS; + const std::unordered_map FLAGS; }; } // namespace android diff --git a/libs/input/InputEventLabels.cpp b/libs/input/InputEventLabels.cpp index 1c7cc129bd..bade68629d 100644 --- a/libs/input/InputEventLabels.cpp +++ b/libs/input/InputEventLabels.cpp @@ -434,17 +434,14 @@ namespace android { // clang-format on // --- InputEventLookup --- -const std::unordered_map InputEventLookup::KEYCODES = {KEYCODES_SEQUENCE}; -const std::vector InputEventLookup::KEY_NAMES = {KEYCODES_SEQUENCE}; - -const std::unordered_map InputEventLookup::AXES = {AXES_SEQUENCE}; - -const std::vector InputEventLookup::AXES_NAMES = {AXES_SEQUENCE}; - -const std::unordered_map InputEventLookup::LEDS = {LEDS_SEQUENCE}; - -const std::unordered_map InputEventLookup::FLAGS = {FLAGS_SEQUENCE}; +InputEventLookup::InputEventLookup() + : KEYCODES({KEYCODES_SEQUENCE}), + KEY_NAMES({KEYCODES_SEQUENCE}), + AXES({AXES_SEQUENCE}), + AXES_NAMES({AXES_SEQUENCE}), + LEDS({LEDS_SEQUENCE}), + FLAGS({FLAGS_SEQUENCE}) {} std::optional InputEventLookup::lookupValueByLabel( const std::unordered_map& map, const char* literal) { @@ -462,30 +459,36 @@ const char* InputEventLookup::lookupLabelByValue(const std::vector InputEventLookup::getKeyCodeByLabel(const char* label) { - return lookupValueByLabel(KEYCODES, label); + const auto& self = get(); + return self.lookupValueByLabel(self.KEYCODES, label); } const char* InputEventLookup::getLabelByKeyCode(int32_t keyCode) { - if (keyCode >= 0 && static_cast(keyCode) < KEYCODES.size()) { - return lookupLabelByValue(KEY_NAMES, keyCode); + const auto& self = get(); + if (keyCode >= 0 && static_cast(keyCode) < self.KEYCODES.size()) { + return get().lookupLabelByValue(self.KEY_NAMES, keyCode); } return nullptr; } std::optional InputEventLookup::getKeyFlagByLabel(const char* label) { - return lookupValueByLabel(FLAGS, label); + const auto& self = get(); + return lookupValueByLabel(self.FLAGS, label); } std::optional InputEventLookup::getAxisByLabel(const char* label) { - return lookupValueByLabel(AXES, label); + const auto& self = get(); + return lookupValueByLabel(self.AXES, label); } const char* InputEventLookup::getAxisLabel(int32_t axisId) { - return lookupLabelByValue(AXES_NAMES, axisId); + const auto& self = get(); + return lookupLabelByValue(self.AXES_NAMES, axisId); } std::optional InputEventLookup::getLedByLabel(const char* label) { - return lookupValueByLabel(LEDS, label); + const auto& self = get(); + return lookupValueByLabel(self.LEDS, label); } namespace { -- cgit v1.2.3-59-g8ed1b