/* * Copyright (C) 2021 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 #include #include #include #include #include #include "../Macros.h" #include "PeripheralController.h" namespace android { static inline int32_t getAlpha(int32_t color) { return (color >> 24) & 0xff; } static inline int32_t getRed(int32_t color) { return (color >> 16) & 0xff; } static inline int32_t getGreen(int32_t color) { return (color >> 8) & 0xff; } static inline int32_t getBlue(int32_t color) { return color & 0xff; } static inline int32_t toArgb(int32_t brightness, int32_t red, int32_t green, int32_t blue) { return (brightness & 0xff) << 24 | (red & 0xff) << 16 | (green & 0xff) << 8 | (blue & 0xff); } static inline bool isKeyboardBacklightCustomLevelsEnabled() { return sysprop::InputProperties::enable_keyboard_backlight_custom_levels().value_or(true); } /** * Input controller owned by InputReader device, implements the native API for querying input * lights, getting and setting the lights brightness and color, by interacting with EventHub * devices. */ PeripheralController::PeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) { configureBattries(); configureLights(); } PeripheralController::~PeripheralController() {} std::optional PeripheralController::Light::getRawLightBrightness(int32_t rawLightId) { std::optional rawInfoOpt = context.getRawLightInfo(rawLightId); if (!rawInfoOpt.has_value()) { return std::nullopt; } std::optional brightnessOpt = context.getLightBrightness(rawLightId); if (!brightnessOpt.has_value()) { return std::nullopt; } int brightness = brightnessOpt.value(); // If the light node doesn't have max brightness, use the default max brightness. int rawMaxBrightness = rawInfoOpt->maxBrightness.value_or(MAX_BRIGHTNESS); float ratio = static_cast(MAX_BRIGHTNESS) / rawMaxBrightness; // Scale the returned brightness in [0, rawMaxBrightness] to [0, 255] if (rawMaxBrightness != MAX_BRIGHTNESS) { brightness = brightness * ratio; } if (DEBUG_LIGHT_DETAILS) { ALOGD("getRawLightBrightness rawLightId %d brightness 0x%x ratio %.2f", rawLightId, brightness, ratio); } return brightness; } void PeripheralController::Light::setRawLightBrightness(int32_t rawLightId, int32_t brightness) { std::optional rawInfo = context.getRawLightInfo(rawLightId); if (!rawInfo.has_value()) { return; } // If the light node doesn't have max brightness, use the default max brightness. int rawMaxBrightness = rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS); float ratio = static_cast(MAX_BRIGHTNESS) / rawMaxBrightness; // Scale the requested brightness in [0, 255] to [0, rawMaxBrightness] if (rawMaxBrightness != MAX_BRIGHTNESS) { brightness = ceil(brightness / ratio); } if (DEBUG_LIGHT_DETAILS) { ALOGD("setRawLightBrightness rawLightId %d brightness 0x%x ratio %.2f", rawLightId, brightness, ratio); } context.setLightBrightness(rawLightId, brightness); } bool PeripheralController::MonoLight::setLightColor(int32_t color) { int32_t brightness = getAlpha(color); setRawLightBrightness(rawId, brightness); return true; } bool PeripheralController::RgbLight::setLightColor(int32_t color) { // Compose color value as per: // https://developer.android.com/reference/android/graphics/Color?hl=en // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff); // The alpha component is used to scale the R,G,B leds brightness, with the ratio to // MAX_BRIGHTNESS. brightness = getAlpha(color); int32_t red = 0; int32_t green = 0; int32_t blue = 0; if (brightness > 0) { float ratio = MAX_BRIGHTNESS / brightness; red = ceil(getRed(color) / ratio); green = ceil(getGreen(color) / ratio); blue = ceil(getBlue(color) / ratio); } setRawLightBrightness(rawRgbIds.at(LightColor::RED), red); setRawLightBrightness(rawRgbIds.at(LightColor::GREEN), green); setRawLightBrightness(rawRgbIds.at(LightColor::BLUE), blue); if (rawGlobalId.has_value()) { setRawLightBrightness(rawGlobalId.value(), brightness); } return true; } bool PeripheralController::MultiColorLight::setLightColor(int32_t color) { std::unordered_map intensities; intensities.emplace(LightColor::RED, getRed(color)); intensities.emplace(LightColor::GREEN, getGreen(color)); intensities.emplace(LightColor::BLUE, getBlue(color)); context.setLightIntensities(rawId, intensities); setRawLightBrightness(rawId, getAlpha(color)); return true; } std::optional PeripheralController::MonoLight::getLightColor() { std::optional brightness = getRawLightBrightness(rawId); if (!brightness.has_value()) { return std::nullopt; } return toArgb(brightness.value(), /*red=*/0, /*green=*/0, /*blue=*/0); } std::optional PeripheralController::RgbLight::getLightColor() { // If the Alpha component is zero, then return color 0. if (brightness == 0) { return 0; } // Compose color value as per: // https://developer.android.com/reference/android/graphics/Color?hl=en // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff); std::optional redOr = getRawLightBrightness(rawRgbIds.at(LightColor::RED)); std::optional greenOr = getRawLightBrightness(rawRgbIds.at(LightColor::GREEN)); std::optional blueOr = getRawLightBrightness(rawRgbIds.at(LightColor::BLUE)); // If we can't get brightness for any of the RGB light if (!redOr.has_value() || !greenOr.has_value() || !blueOr.has_value()) { return std::nullopt; } // Compose the ARGB format color. As the R,G,B color led brightness is scaled by Alpha // value, scale it back to return the nominal color value. float ratio = MAX_BRIGHTNESS / brightness; int32_t red = round(redOr.value() * ratio); int32_t green = round(greenOr.value() * ratio); int32_t blue = round(blueOr.value() * ratio); if (red > MAX_BRIGHTNESS || green > MAX_BRIGHTNESS || blue > MAX_BRIGHTNESS) { // Previously stored brightness isn't valid for current LED values, so just reset to max // brightness since an app couldn't have provided these values in the first place. red = redOr.value(); green = greenOr.value(); blue = blueOr.value(); brightness = MAX_BRIGHTNESS; } return toArgb(brightness, red, green, blue); } std::optional PeripheralController::MultiColorLight::getLightColor() { auto ret = context.getLightIntensities(rawId); if (!ret.has_value()) { return std::nullopt; } std::unordered_map intensities = ret.value(); // Get red, green, blue colors int32_t color = toArgb(/*brightness=*/0, intensities.at(LightColor::RED), intensities.at(LightColor::GREEN), intensities.at(LightColor::BLUE)); // Get brightness std::optional brightness = getRawLightBrightness(rawId); if (brightness.has_value()) { return toArgb(/*brightness=*/brightness.value(), 0, 0, 0) | color; } return std::nullopt; } bool PeripheralController::PlayerIdLight::setLightPlayerId(int32_t playerId) { if (rawLightIds.find(playerId) == rawLightIds.end()) { return false; } for (const auto& [id, rawId] : rawLightIds) { if (playerId == id) { setRawLightBrightness(rawId, MAX_BRIGHTNESS); } else { setRawLightBrightness(rawId, 0); } } return true; } std::optional PeripheralController::PlayerIdLight::getLightPlayerId() { for (const auto& [id, rawId] : rawLightIds) { std::optional brightness = getRawLightBrightness(rawId); if (brightness.has_value() && brightness.value() > 0) { return id; } } return std::nullopt; } void PeripheralController::MonoLight::dump(std::string& dump) { dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0)); } void PeripheralController::PlayerIdLight::dump(std::string& dump) { dump += StringPrintf(INDENT4 "PlayerId: %d\n", getLightPlayerId().value_or(-1)); dump += StringPrintf(INDENT4 "Raw Player ID LEDs:"); for (const auto& [id, rawId] : rawLightIds) { dump += StringPrintf("id %d -> %d ", id, rawId); } dump += "\n"; } void PeripheralController::RgbLight::dump(std::string& dump) { dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0)); dump += StringPrintf(INDENT4 "Raw RGB LEDs: [%d, %d, %d] ", rawRgbIds.at(LightColor::RED), rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE)); if (rawGlobalId.has_value()) { dump += StringPrintf(INDENT4 "Raw Global LED: [%d] ", rawGlobalId.value()); } dump += "\n"; } void PeripheralController::MultiColorLight::dump(std::string& dump) { dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0)); } void PeripheralController::populateDeviceInfo(InputDeviceInfo* deviceInfo) { // TODO: b/180733860 Remove this after enabling multi-battery if (!mBatteries.empty()) { deviceInfo->setHasBattery(true); } for (const auto& [batteryId, battery] : mBatteries) { InputDeviceBatteryInfo batteryInfo(battery->name, battery->id); deviceInfo->addBatteryInfo(batteryInfo); } for (const auto& [lightId, light] : mLights) { // Input device light doesn't support ordinal, always pass 1. InputDeviceLightInfo lightInfo(light->name, light->id, light->type, light->capabilityFlags, /*ordinal=*/1, getPreferredBrightnessLevels(light.get())); deviceInfo->addLightInfo(lightInfo); } } // TODO(b/281822656): Move to constructor and add as a parameter to avoid parsing repeatedly. // Need to change lifecycle of Peripheral controller so that Input device configuration map is // available at construction time before moving this logic to constructor. std::set PeripheralController::getPreferredBrightnessLevels( const Light* light) const { std::set levels; if (!isKeyboardBacklightCustomLevelsEnabled() || light->type != InputDeviceLightType::KEYBOARD_BACKLIGHT) { return levels; } std::optional keyboardBacklightLevels = mDeviceContext.getConfiguration().getString("keyboard.backlight.brightnessLevels"); if (!keyboardBacklightLevels) { return levels; } std::stringstream ss(*keyboardBacklightLevels); while (ss.good()) { std::string substr; std::getline(ss, substr, ','); char* end; int32_t value = static_cast(strtol(substr.c_str(), &end, 10)); if (*end != '\0' || value < 0 || value > 255) { ALOGE("Error parsing keyboard backlight brightness levels, provided levels = %s", keyboardBacklightLevels->c_str()); levels.clear(); break; } levels.insert(BrightnessLevel(value)); } return levels; } void PeripheralController::dump(std::string& dump) { dump += INDENT2 "Input Controller:\n"; if (!mLights.empty()) { dump += INDENT3 "Lights:\n"; for (const auto& [lightId, light] : mLights) { dump += StringPrintf(INDENT4 "Id: %d", lightId); dump += StringPrintf(INDENT4 "Name: %s", light->name.c_str()); dump += StringPrintf(INDENT4 "Type: %s", ftl::enum_string(light->type).c_str()); dump += StringPrintf(INDENT4 "Capability flags: %s", light->capabilityFlags.string().c_str()); light->dump(dump); } } // Dump raw lights dump += INDENT3 "RawLights:\n"; dump += INDENT4 "Id:\t Name:\t Flags:\t Max brightness:\t Brightness\n"; const std::vector rawLightIds = getDeviceContext().getRawLightIds(); // Map from raw light id to raw light info std::unordered_map rawInfos; for (const auto& rawId : rawLightIds) { std::optional rawInfo = getDeviceContext().getRawLightInfo(rawId); if (!rawInfo.has_value()) { continue; } dump += StringPrintf(INDENT4 "%d", rawId); dump += StringPrintf(INDENT4 "%s", rawInfo->name.c_str()); dump += StringPrintf(INDENT4 "%s", rawInfo->flags.string().c_str()); dump += StringPrintf(INDENT4 "%d", rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS)); dump += StringPrintf(INDENT4 "%d\n", getDeviceContext().getLightBrightness(rawId).value_or(-1)); } if (!mBatteries.empty()) { dump += INDENT3 "Batteries:\n"; for (const auto& [batteryId, battery] : mBatteries) { dump += StringPrintf(INDENT4 "Id: %d", batteryId); dump += StringPrintf(INDENT4 "Name: %s", battery->name.c_str()); dump += getBatteryCapacity(batteryId).has_value() ? StringPrintf(INDENT3 "Capacity: %d\n", getBatteryCapacity(batteryId).value()) : StringPrintf(INDENT3 "Capacity: Unknown"); std::string status; switch (getBatteryStatus(batteryId).value_or(BATTERY_STATUS_UNKNOWN)) { case BATTERY_STATUS_CHARGING: status = "Charging"; break; case BATTERY_STATUS_DISCHARGING: status = "Discharging"; break; case BATTERY_STATUS_NOT_CHARGING: status = "Not charging"; break; case BATTERY_STATUS_FULL: status = "Full"; break; default: status = "Unknown"; } dump += StringPrintf(INDENT3 "Status: %s\n", status.c_str()); } } } void PeripheralController::configureBattries() { // Check raw batteries const std::vector rawBatteryIds = getDeviceContext().getRawBatteryIds(); for (const auto& rawId : rawBatteryIds) { std::optional rawInfo = getDeviceContext().getRawBatteryInfo(rawId); if (!rawInfo.has_value()) { continue; } std::unique_ptr battery = std::make_unique(getDeviceContext(), rawInfo->name, rawInfo->id); mBatteries.insert_or_assign(rawId, std::move(battery)); } } void PeripheralController::configureLights() { bool hasRedLed = false; bool hasGreenLed = false; bool hasBlueLed = false; std::optional rawGlobalId = std::nullopt; // Player ID light common name string std::string playerIdName; // Raw RGB color to raw light ID std::unordered_map rawRgbIds; // Map from player Id to raw light Id std::unordered_map playerIdLightIds; // Set of Keyboard backlights std::set keyboardBacklightIds; // Check raw lights const std::vector rawLightIds = getDeviceContext().getRawLightIds(); // Map from raw light id to raw light info std::unordered_map rawInfos; for (const auto& rawId : rawLightIds) { std::optional rawInfo = getDeviceContext().getRawLightInfo(rawId); if (!rawInfo.has_value()) { continue; } rawInfos.insert_or_assign(rawId, rawInfo.value()); // Check if this is a group LEDs for player ID // The name for the light has already been parsed and is the `function` // value; for player ID lights the function is expected to be `player-#`. // However, the Sony driver will use `sony#` instead on SIXAXIS // gamepads. std::regex lightPattern("(player|sony)-?([0-9]+)"); std::smatch results; if (std::regex_match(rawInfo->name, results, lightPattern)) { std::string commonName = results[1].str(); int32_t playerId = std::stoi(results[2]); if (playerIdLightIds.empty()) { playerIdName = commonName; playerIdLightIds.insert_or_assign(playerId, rawId); } else { // Make sure the player ID leds have common string name if (playerIdName.compare(commonName) == 0 && playerIdLightIds.find(playerId) == playerIdLightIds.end()) { playerIdLightIds.insert_or_assign(playerId, rawId); } } } // Check if this is a Keyboard backlight if (rawInfo->flags.test(InputLightClass::KEYBOARD_BACKLIGHT)) { keyboardBacklightIds.insert(rawId); } // Check if this is an LED of RGB light if (rawInfo->flags.test(InputLightClass::RED)) { hasRedLed = true; rawRgbIds.emplace(LightColor::RED, rawId); } if (rawInfo->flags.test(InputLightClass::GREEN)) { hasGreenLed = true; rawRgbIds.emplace(LightColor::GREEN, rawId); } if (rawInfo->flags.test(InputLightClass::BLUE)) { hasBlueLed = true; rawRgbIds.emplace(LightColor::BLUE, rawId); } if (rawInfo->flags.test(InputLightClass::GLOBAL)) { rawGlobalId = rawId; } if (DEBUG_LIGHT_DETAILS) { ALOGD("Light rawId %d name %s max %d flags %s \n", rawInfo->id, rawInfo->name.c_str(), rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS), rawInfo->flags.string().c_str()); } } // Construct a player ID light if (playerIdLightIds.size() > 1) { std::unique_ptr light = std::make_unique(getDeviceContext(), playerIdName, ++mNextId, playerIdLightIds); mLights.insert_or_assign(light->id, std::move(light)); // Remove these raw lights from raw light info as they've been used to compose a // Player ID light, so we do not expose these raw lights as mono lights. for (const auto& [playerId, rawId] : playerIdLightIds) { rawInfos.erase(rawId); } } // Construct a RGB light for composed RGB light if (hasRedLed && hasGreenLed && hasBlueLed) { if (DEBUG_LIGHT_DETAILS) { ALOGD("Rgb light ids [%d, %d, %d] \n", rawRgbIds.at(LightColor::RED), rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE)); } bool isKeyboardBacklight = keyboardBacklightIds.find(rawRgbIds.at(LightColor::RED)) != keyboardBacklightIds.end() && keyboardBacklightIds.find(rawRgbIds.at(LightColor::GREEN)) != keyboardBacklightIds.end() && keyboardBacklightIds.find(rawRgbIds.at(LightColor::BLUE)) != keyboardBacklightIds.end() && (!rawGlobalId.has_value() || keyboardBacklightIds.find(rawGlobalId.value()) != keyboardBacklightIds.end()); std::unique_ptr light = std::make_unique(getDeviceContext(), ++mNextId, isKeyboardBacklight ? InputDeviceLightType::KEYBOARD_BACKLIGHT : InputDeviceLightType::INPUT, rawRgbIds, rawGlobalId); mLights.insert_or_assign(light->id, std::move(light)); // Remove from raw light info as they've been composed a RBG light. rawInfos.erase(rawRgbIds.at(LightColor::RED)); rawInfos.erase(rawRgbIds.at(LightColor::GREEN)); rawInfos.erase(rawRgbIds.at(LightColor::BLUE)); if (rawGlobalId.has_value()) { rawInfos.erase(rawGlobalId.value()); } } // Check the rest of raw light infos for (const auto& [rawId, rawInfo] : rawInfos) { InputDeviceLightType type; if (keyboardBacklightIds.find(rawId) != keyboardBacklightIds.end()) { type = InputDeviceLightType::KEYBOARD_BACKLIGHT; } else if (rawInfo.flags.test(InputLightClass::KEYBOARD_MIC_MUTE)) { type = InputDeviceLightType::KEYBOARD_MIC_MUTE; } else { type = InputDeviceLightType::INPUT; } // If the node is multi-color led, construct a MULTI_COLOR light if (rawInfo.flags.test(InputLightClass::MULTI_INDEX) && rawInfo.flags.test(InputLightClass::MULTI_INTENSITY)) { if (DEBUG_LIGHT_DETAILS) { ALOGD("Multicolor light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str()); } std::unique_ptr light = std::make_unique(getDeviceContext(), rawInfo.name, ++mNextId, type, rawInfo.id); mLights.insert_or_assign(light->id, std::move(light)); continue; } // Construct a Mono LED light if (DEBUG_LIGHT_DETAILS) { ALOGD("Mono light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str()); } std::unique_ptr light = std::make_unique(getDeviceContext(), rawInfo.name, ++mNextId, type, rawInfo.id); mLights.insert_or_assign(light->id, std::move(light)); } } std::optional PeripheralController::getBatteryCapacity(int batteryId) { return getDeviceContext().getBatteryCapacity(batteryId); } std::optional PeripheralController::getBatteryStatus(int batteryId) { return getDeviceContext().getBatteryStatus(batteryId); } bool PeripheralController::setLightColor(int32_t lightId, int32_t color) { auto it = mLights.find(lightId); if (it == mLights.end()) { return false; } auto& light = it->second; if (DEBUG_LIGHT_DETAILS) { ALOGD("setLightColor lightId %d type %s color 0x%x", lightId, ftl::enum_string(light->type).c_str(), color); } return light->setLightColor(color); } std::optional PeripheralController::getLightColor(int32_t lightId) { auto it = mLights.find(lightId); if (it == mLights.end()) { return std::nullopt; } auto& light = it->second; std::optional color = light->getLightColor(); if (DEBUG_LIGHT_DETAILS) { ALOGD("getLightColor lightId %d type %s color 0x%x", lightId, ftl::enum_string(light->type).c_str(), color.value_or(0)); } return color; } bool PeripheralController::setLightPlayerId(int32_t lightId, int32_t playerId) { auto it = mLights.find(lightId); if (it == mLights.end()) { return false; } auto& light = it->second; return light->setLightPlayerId(playerId); } std::optional PeripheralController::getLightPlayerId(int32_t lightId) { auto it = mLights.find(lightId); if (it == mLights.end()) { return std::nullopt; } auto& light = it->second; return light->getLightPlayerId(); } int32_t PeripheralController::getEventHubId() const { return getDeviceContext().getEventHubId(); } } // namespace android