Loading include/input/PropertyMap.h +9 −8 Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ #include <android-base/result.h> #include <utils/Tokenizer.h> #include <optional> #include <string> #include <unordered_map> #include <unordered_set> Loading Loading @@ -63,15 +64,15 @@ public: /* Returns a set of all property keys starting with the given prefix. */ std::unordered_set<std::string> getKeysWithPrefix(const std::string& prefix) const; /* Gets the value of a property and parses it. * Returns true and sets outValue if the key was found and its value was parsed successfully. * Otherwise returns false and does not modify outValue. (Also logs a warning.) /* Gets the value of a property and parses it. Returns nullopt if the key wasn't found or * couldn't be parsed as the requested type. (Warnings are also logged in the case of parsing * failures.) */ bool tryGetProperty(const std::string& key, std::string& outValue) const; bool tryGetProperty(const std::string& key, bool& outValue) const; bool tryGetProperty(const std::string& key, int32_t& outValue) const; bool tryGetProperty(const std::string& key, float& outValue) const; bool tryGetProperty(const std::string& key, double& outValue) const; std::optional<std::string> getString(const std::string& key) const; std::optional<bool> getBool(const std::string& key) const; std::optional<int32_t> getInt(const std::string& key) const; std::optional<float> getFloat(const std::string& key) const; std::optional<double> getDouble(const std::string& key) const; /* Adds all values from the specified property map. */ void addAll(const PropertyMap* map); Loading libs/input/Keyboard.cpp +14 −14 Original line number Diff line number Diff line Loading @@ -16,9 +16,10 @@ #define LOG_TAG "Keyboard" #include <limits.h> #include <stdlib.h> #include <unistd.h> #include <limits.h> #include <optional> #include <input/InputDevice.h> #include <input/InputEventLabels.h> Loading Loading @@ -49,23 +50,25 @@ status_t KeyMap::load(const InputDeviceIdentifier& deviceIdentifier, const PropertyMap* deviceConfiguration) { // Use the configured key layout if available. if (deviceConfiguration) { std::string keyLayoutName; if (deviceConfiguration->tryGetProperty("keyboard.layout", keyLayoutName)) { status_t status = loadKeyLayout(deviceIdentifier, keyLayoutName.c_str()); std::optional<std::string> keyLayoutName = deviceConfiguration->getString("keyboard.layout"); if (keyLayoutName.has_value()) { status_t status = loadKeyLayout(deviceIdentifier, *keyLayoutName); if (status == NAME_NOT_FOUND) { ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but " "it was not found.", deviceIdentifier.name.c_str(), keyLayoutName.c_str()); deviceIdentifier.name.c_str(), keyLayoutName->c_str()); } } std::string keyCharacterMapName; if (deviceConfiguration->tryGetProperty("keyboard.characterMap", keyCharacterMapName)) { status_t status = loadKeyCharacterMap(deviceIdentifier, keyCharacterMapName.c_str()); std::optional<std::string> keyCharacterMapName = deviceConfiguration->getString("keyboard.characterMap"); if (keyCharacterMapName.has_value()) { status_t status = loadKeyCharacterMap(deviceIdentifier, *keyCharacterMapName); if (status == NAME_NOT_FOUND) { ALOGE("Configuration for keyboard device '%s' requested keyboard character " "map '%s' but it was not found.", deviceIdentifier.name.c_str(), keyCharacterMapName.c_str()); deviceIdentifier.name.c_str(), keyCharacterMapName->c_str()); } } Loading Loading @@ -162,9 +165,7 @@ bool isKeyboardSpecialFunction(const PropertyMap* config) { if (config == nullptr) { return false; } bool isSpecialFunction = false; config->tryGetProperty("keyboard.specialFunction", isSpecialFunction); return isSpecialFunction; return config->getBool("keyboard.specialFunction").value_or(false); } bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier, Loading @@ -177,8 +178,7 @@ bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier, } if (deviceConfiguration) { bool builtIn = false; if (deviceConfiguration->tryGetProperty("keyboard.builtIn", builtIn) && builtIn) { if (deviceConfiguration->getBool("keyboard.builtIn").value_or(false)) { return true; } } Loading libs/input/PropertyMap.cpp +29 −42 Original line number Diff line number Diff line Loading @@ -60,75 +60,62 @@ bool PropertyMap::hasProperty(const std::string& key) const { return mProperties.find(key) != mProperties.end(); } bool PropertyMap::tryGetProperty(const std::string& key, std::string& outValue) const { std::optional<std::string> PropertyMap::getString(const std::string& key) const { auto it = mProperties.find(key); if (it == mProperties.end()) { return false; return it != mProperties.end() ? std::make_optional(it->second) : std::nullopt; } outValue = it->second; return true; std::optional<bool> PropertyMap::getBool(const std::string& key) const { std::optional<int32_t> intValue = getInt(key); return intValue.has_value() ? std::make_optional(*intValue != 0) : std::nullopt; } bool PropertyMap::tryGetProperty(const std::string& key, bool& outValue) const { int32_t intValue; if (!tryGetProperty(key, intValue)) { return false; } outValue = intValue; return true; } bool PropertyMap::tryGetProperty(const std::string& key, int32_t& outValue) const { std::string stringValue; if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { return false; std::optional<int32_t> PropertyMap::getInt(const std::string& key) const { std::optional<std::string> stringValue = getString(key); if (!stringValue.has_value() || stringValue->length() == 0) { return std::nullopt; } char* end; int32_t value = static_cast<int32_t>(strtol(stringValue.c_str(), &end, 10)); int32_t value = static_cast<int32_t>(strtol(stringValue->c_str(), &end, 10)); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(), stringValue.c_str()); return false; stringValue->c_str()); return std::nullopt; } outValue = value; return true; return value; } bool PropertyMap::tryGetProperty(const std::string& key, float& outValue) const { std::string stringValue; if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { return false; std::optional<float> PropertyMap::getFloat(const std::string& key) const { std::optional<std::string> stringValue = getString(key); if (!stringValue.has_value() || stringValue->length() == 0) { return std::nullopt; } char* end; float value = strtof(stringValue.c_str(), &end); float value = strtof(stringValue->c_str(), &end); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(), stringValue.c_str()); return false; stringValue->c_str()); return std::nullopt; } outValue = value; return true; return value; } bool PropertyMap::tryGetProperty(const std::string& key, double& outValue) const { std::string stringValue; if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { return false; std::optional<double> PropertyMap::getDouble(const std::string& key) const { std::optional<std::string> stringValue = getString(key); if (!stringValue.has_value() || stringValue->length() == 0) { return std::nullopt; } char* end; double value = strtod(stringValue.c_str(), &end); double value = strtod(stringValue->c_str(), &end); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected a double.", key.c_str(), stringValue.c_str()); return false; stringValue->c_str()); return std::nullopt; } outValue = value; return true; return value; } void PropertyMap::addAll(const PropertyMap* map) { Loading libs/input/PropertyMap_fuzz.cpp 100755 → 100644 +1 −2 Original line number Diff line number Diff line Loading @@ -29,8 +29,7 @@ static const std::vector<std::function<void(FuzzedDataProvider*, android::Proper }, [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void { std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN); std::string out; propertyMap.tryGetProperty(key, out); propertyMap.getString(key); }, [](FuzzedDataProvider* dataProvider, android::PropertyMap& /*unused*/) -> void { TemporaryFile tf; Loading services/inputflinger/host/InputDriver.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -242,13 +242,13 @@ input_property_map_t* InputDriver::inputGetDevicePropertyMap(input_device_identi input_property_t* InputDriver::inputGetDeviceProperty(input_property_map_t* map, const char* key) { if (map != nullptr) { std::string value; auto prop = std::make_unique<input_property_t>(); if (!map->propertyMap->tryGetProperty(key, value)) { std::optional<std::string> value = map->propertyMap->getString(key); if (!value.has_value()) { return nullptr; } auto prop = std::make_unique<input_property_t>(); prop->key = key; prop->value = value.c_str(); prop->value = value->c_str(); return prop.release(); } return nullptr; Loading Loading
include/input/PropertyMap.h +9 −8 Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ #include <android-base/result.h> #include <utils/Tokenizer.h> #include <optional> #include <string> #include <unordered_map> #include <unordered_set> Loading Loading @@ -63,15 +64,15 @@ public: /* Returns a set of all property keys starting with the given prefix. */ std::unordered_set<std::string> getKeysWithPrefix(const std::string& prefix) const; /* Gets the value of a property and parses it. * Returns true and sets outValue if the key was found and its value was parsed successfully. * Otherwise returns false and does not modify outValue. (Also logs a warning.) /* Gets the value of a property and parses it. Returns nullopt if the key wasn't found or * couldn't be parsed as the requested type. (Warnings are also logged in the case of parsing * failures.) */ bool tryGetProperty(const std::string& key, std::string& outValue) const; bool tryGetProperty(const std::string& key, bool& outValue) const; bool tryGetProperty(const std::string& key, int32_t& outValue) const; bool tryGetProperty(const std::string& key, float& outValue) const; bool tryGetProperty(const std::string& key, double& outValue) const; std::optional<std::string> getString(const std::string& key) const; std::optional<bool> getBool(const std::string& key) const; std::optional<int32_t> getInt(const std::string& key) const; std::optional<float> getFloat(const std::string& key) const; std::optional<double> getDouble(const std::string& key) const; /* Adds all values from the specified property map. */ void addAll(const PropertyMap* map); Loading
libs/input/Keyboard.cpp +14 −14 Original line number Diff line number Diff line Loading @@ -16,9 +16,10 @@ #define LOG_TAG "Keyboard" #include <limits.h> #include <stdlib.h> #include <unistd.h> #include <limits.h> #include <optional> #include <input/InputDevice.h> #include <input/InputEventLabels.h> Loading Loading @@ -49,23 +50,25 @@ status_t KeyMap::load(const InputDeviceIdentifier& deviceIdentifier, const PropertyMap* deviceConfiguration) { // Use the configured key layout if available. if (deviceConfiguration) { std::string keyLayoutName; if (deviceConfiguration->tryGetProperty("keyboard.layout", keyLayoutName)) { status_t status = loadKeyLayout(deviceIdentifier, keyLayoutName.c_str()); std::optional<std::string> keyLayoutName = deviceConfiguration->getString("keyboard.layout"); if (keyLayoutName.has_value()) { status_t status = loadKeyLayout(deviceIdentifier, *keyLayoutName); if (status == NAME_NOT_FOUND) { ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but " "it was not found.", deviceIdentifier.name.c_str(), keyLayoutName.c_str()); deviceIdentifier.name.c_str(), keyLayoutName->c_str()); } } std::string keyCharacterMapName; if (deviceConfiguration->tryGetProperty("keyboard.characterMap", keyCharacterMapName)) { status_t status = loadKeyCharacterMap(deviceIdentifier, keyCharacterMapName.c_str()); std::optional<std::string> keyCharacterMapName = deviceConfiguration->getString("keyboard.characterMap"); if (keyCharacterMapName.has_value()) { status_t status = loadKeyCharacterMap(deviceIdentifier, *keyCharacterMapName); if (status == NAME_NOT_FOUND) { ALOGE("Configuration for keyboard device '%s' requested keyboard character " "map '%s' but it was not found.", deviceIdentifier.name.c_str(), keyCharacterMapName.c_str()); deviceIdentifier.name.c_str(), keyCharacterMapName->c_str()); } } Loading Loading @@ -162,9 +165,7 @@ bool isKeyboardSpecialFunction(const PropertyMap* config) { if (config == nullptr) { return false; } bool isSpecialFunction = false; config->tryGetProperty("keyboard.specialFunction", isSpecialFunction); return isSpecialFunction; return config->getBool("keyboard.specialFunction").value_or(false); } bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier, Loading @@ -177,8 +178,7 @@ bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier, } if (deviceConfiguration) { bool builtIn = false; if (deviceConfiguration->tryGetProperty("keyboard.builtIn", builtIn) && builtIn) { if (deviceConfiguration->getBool("keyboard.builtIn").value_or(false)) { return true; } } Loading
libs/input/PropertyMap.cpp +29 −42 Original line number Diff line number Diff line Loading @@ -60,75 +60,62 @@ bool PropertyMap::hasProperty(const std::string& key) const { return mProperties.find(key) != mProperties.end(); } bool PropertyMap::tryGetProperty(const std::string& key, std::string& outValue) const { std::optional<std::string> PropertyMap::getString(const std::string& key) const { auto it = mProperties.find(key); if (it == mProperties.end()) { return false; return it != mProperties.end() ? std::make_optional(it->second) : std::nullopt; } outValue = it->second; return true; std::optional<bool> PropertyMap::getBool(const std::string& key) const { std::optional<int32_t> intValue = getInt(key); return intValue.has_value() ? std::make_optional(*intValue != 0) : std::nullopt; } bool PropertyMap::tryGetProperty(const std::string& key, bool& outValue) const { int32_t intValue; if (!tryGetProperty(key, intValue)) { return false; } outValue = intValue; return true; } bool PropertyMap::tryGetProperty(const std::string& key, int32_t& outValue) const { std::string stringValue; if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { return false; std::optional<int32_t> PropertyMap::getInt(const std::string& key) const { std::optional<std::string> stringValue = getString(key); if (!stringValue.has_value() || stringValue->length() == 0) { return std::nullopt; } char* end; int32_t value = static_cast<int32_t>(strtol(stringValue.c_str(), &end, 10)); int32_t value = static_cast<int32_t>(strtol(stringValue->c_str(), &end, 10)); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(), stringValue.c_str()); return false; stringValue->c_str()); return std::nullopt; } outValue = value; return true; return value; } bool PropertyMap::tryGetProperty(const std::string& key, float& outValue) const { std::string stringValue; if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { return false; std::optional<float> PropertyMap::getFloat(const std::string& key) const { std::optional<std::string> stringValue = getString(key); if (!stringValue.has_value() || stringValue->length() == 0) { return std::nullopt; } char* end; float value = strtof(stringValue.c_str(), &end); float value = strtof(stringValue->c_str(), &end); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(), stringValue.c_str()); return false; stringValue->c_str()); return std::nullopt; } outValue = value; return true; return value; } bool PropertyMap::tryGetProperty(const std::string& key, double& outValue) const { std::string stringValue; if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { return false; std::optional<double> PropertyMap::getDouble(const std::string& key) const { std::optional<std::string> stringValue = getString(key); if (!stringValue.has_value() || stringValue->length() == 0) { return std::nullopt; } char* end; double value = strtod(stringValue.c_str(), &end); double value = strtod(stringValue->c_str(), &end); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected a double.", key.c_str(), stringValue.c_str()); return false; stringValue->c_str()); return std::nullopt; } outValue = value; return true; return value; } void PropertyMap::addAll(const PropertyMap* map) { Loading
libs/input/PropertyMap_fuzz.cpp 100755 → 100644 +1 −2 Original line number Diff line number Diff line Loading @@ -29,8 +29,7 @@ static const std::vector<std::function<void(FuzzedDataProvider*, android::Proper }, [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void { std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN); std::string out; propertyMap.tryGetProperty(key, out); propertyMap.getString(key); }, [](FuzzedDataProvider* dataProvider, android::PropertyMap& /*unused*/) -> void { TemporaryFile tf; Loading
services/inputflinger/host/InputDriver.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -242,13 +242,13 @@ input_property_map_t* InputDriver::inputGetDevicePropertyMap(input_device_identi input_property_t* InputDriver::inputGetDeviceProperty(input_property_map_t* map, const char* key) { if (map != nullptr) { std::string value; auto prop = std::make_unique<input_property_t>(); if (!map->propertyMap->tryGetProperty(key, value)) { std::optional<std::string> value = map->propertyMap->getString(key); if (!value.has_value()) { return nullptr; } auto prop = std::make_unique<input_property_t>(); prop->key = key; prop->value = value.c_str(); prop->value = value->c_str(); return prop.release(); } return nullptr; Loading