sdm660-common: Adapt AIDL Light HAL for xiaomi_sdm660

* Re-apply old HIDL Light HAL changes
* Bring back set lcd backlight support, our devices doesn't support to set lcd backlight via hwcomposer

Co-authored-by: dianlujitao <dianlujitao@lineageos.org>
Signed-off-by: Ratoriku <a1063021545@gmail.com>
Change-Id: I1561545a2bc54c6e1b49af6d09b56db506afba5c
Signed-off-by: pix106 <sbordenave@gmail.com>
This commit is contained in:
Ratoriku 2021-09-24 20:56:19 +08:00 committed by pix106
parent 6cca34ef40
commit 8d896b11cf
3 changed files with 93 additions and 53 deletions

View file

@ -1,55 +1,53 @@
/* /*
* Copyright (C) 2019 The Android Open Source Project * Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020-2021 The LineageOS Project * Copyright (C) 2020 The LineageOS Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * SPDX-License-Identifier: Apache-2.0
* 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.
*/ */
// Author := dev_harsh1998, Isaac Chen
#define LOG_TAG "android.hardware.lights-service.xiaomi_sdm660" #define LOG_TAG "android.hardware.lights-service.xiaomi_sdm660"
/* #define LOG_NDEBUG 0 */
#include "Lights.h" #include "Lights.h"
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/logging.h> #include <android-base/logging.h>
#include <unistd.h>
namespace { namespace {
/* clang-format off */
#define PPCAT_NX(A, B) A/B #define PPCAT_NX(A, B) A/B
#define PPCAT(A, B) PPCAT_NX(A, B) #define PPCAT(A, B) PPCAT_NX(A, B)
#define STRINGIFY_INNER(x) #x #define STRINGIFY_INNER(x) #x
#define STRINGIFY(x) STRINGIFY_INNER(x) #define STRINGIFY(x) STRINGIFY_INNER(x)
#define LEDS(x) PPCAT(/sys/class/leds, x) #define LEDS(x) PPCAT(/sys/class/leds, x)
#define BLUE_ATTR(x) STRINGIFY(PPCAT(LEDS(blue), x)) #define LCD_ATTR(x) STRINGIFY(PPCAT(LEDS(lcd-backlight), x))
#define GREEN_ATTR(x) STRINGIFY(PPCAT(LEDS(green), x))
#define RED_ATTR(x) STRINGIFY(PPCAT(LEDS(red), x))
#define WHITE_ATTR(x) STRINGIFY(PPCAT(LEDS(white), x)) #define WHITE_ATTR(x) STRINGIFY(PPCAT(LEDS(white), x))
/* clang-format on */ #define BUTTON_ATTR(x) STRINGIFY(PPCAT(LEDS(button-backlight), x))
#define BUTTON1_ATTR(x) STRINGIFY(PPCAT(LEDS(button-backlight1), x))
using ::android::base::ReadFileToString; using ::android::base::ReadFileToString;
using ::android::base::WriteStringToFile; using ::android::base::WriteStringToFile;
// Default max brightness // Default max brightness
constexpr auto kDefaultMaxLedBrightness = 255; constexpr auto kDefaultMaxLedBrightness = 255;
constexpr auto kDefaultMaxScreenBrightness = 4095;
// Each step will stay on for 70ms by default. // Each step will stay on for 50ms by default.
constexpr auto kRampStepDurationDefault = 70; constexpr auto kRampStepDurationDefault = 50;
// Write value to path and close file. // Write value to path and close file.
bool WriteToFile(const std::string& path, uint32_t content) { bool WriteToFile(const std::string& path, uint32_t content) {
return WriteStringToFile(std::to_string(content), path); return WriteStringToFile(std::to_string(content), path);
} }
bool WriteToFile(const std::string& path, const std::string& content) {
return WriteStringToFile(content, path);
}
uint32_t RgbaToBrightness(uint32_t color) { uint32_t RgbaToBrightness(uint32_t color) {
// Extract brightness from AARRGGBB. // Extract brightness from AARRGGBB.
uint32_t alpha = (color >> 24) & 0xFF; uint32_t alpha = (color >> 24) & 0xFF;
@ -85,11 +83,11 @@ namespace hardware {
namespace light { namespace light {
Lights::Lights() { Lights::Lights() {
std::map<int, std::function<void(int id, const HwLightState&)>> lights_{ std::map<int, std::function<void(int id, const HwLightState&)>> lights_{
{(int)LightType::NOTIFICATIONS, {(int)LightType::NOTIFICATIONS,
[this](auto&&... args) { setLightNotification(args...); }}, [this](auto&&... args) { setLightNotification(args...); }},
{(int)LightType::BATTERY, [this](auto&&... args) { setLightNotification(args...); }}, {(int)LightType::BATTERY, [this](auto&&... args) { setLightNotification(args...); }},
{(int)LightType::BACKLIGHT, {}}}; {(int)LightType::BACKLIGHT, [this](auto&&... args) { setLightBacklight(args...); }}};
std::vector<HwLight> availableLights; std::vector<HwLight> availableLights;
for (auto const& pair : lights_) { for (auto const& pair : lights_) {
@ -103,14 +101,38 @@ Lights::Lights() {
std::string buf; std::string buf;
if (ReadFileToString(BLUE_ATTR(max_brightness), &buf) || if (ReadFileToString(LCD_ATTR(max_brightness), &buf)) {
ReadFileToString(RED_ATTR(max_brightness), &buf) || max_screen_brightness_ = std::stoi(buf);
ReadFileToString(WHITE_ATTR(max_brightness), &buf)) { } else {
max_screen_brightness_ = kDefaultMaxScreenBrightness;
LOG(ERROR) << "Failed to read max screen brightness, fallback to "
<< kDefaultMaxScreenBrightness;
}
if (ReadFileToString(WHITE_ATTR(max_brightness), &buf)) {
max_led_brightness_ = std::stoi(buf); max_led_brightness_ = std::stoi(buf);
} else { } else {
max_led_brightness_ = kDefaultMaxLedBrightness; max_led_brightness_ = kDefaultMaxLedBrightness;
LOG(ERROR) << "Failed to read max LED brightness, fallback to " << kDefaultMaxLedBrightness; LOG(ERROR) << "Failed to read max LED brightness, fallback to " << kDefaultMaxLedBrightness;
} }
if (!access(BUTTON_ATTR(brightness), W_OK)) {
lights_.emplace(std::make_pair((int)LightType::BUTTONS,
[this](auto&&... args) { setLightButtons(args...); }));
buttons_.emplace_back(BUTTON_ATTR(brightness));
if (!access(BUTTON1_ATTR(brightness), W_OK)) {
buttons_.emplace_back(BUTTON1_ATTR(brightness));
}
if (ReadFileToString(BUTTON_ATTR(max_brightness), &buf)) {
max_button_brightness_ = std::stoi(buf);
} else {
max_button_brightness_ = kDefaultMaxLedBrightness;
LOG(ERROR) << "Failed to read max button brightness, fallback to "
<< kDefaultMaxLedBrightness;
}
}
} }
ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) { ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) {
@ -132,6 +154,18 @@ ndk::ScopedAStatus Lights::getLights(std::vector<HwLight>* lights) {
return ndk::ScopedAStatus::ok(); return ndk::ScopedAStatus::ok();
} }
void Lights::setLightBacklight(int /*id*/, const HwLightState& state) {
uint32_t brightness = RgbaToBrightness(state.color, max_screen_brightness_);
WriteToFile(LCD_ATTR(brightness), brightness);
}
void Lights::setLightButtons(int /*id*/, const HwLightState& state) {
uint32_t brightness = RgbaToBrightness(state.color, max_button_brightness_);
for (auto&& button : buttons_) {
WriteToFile(button, brightness);
}
}
void Lights::setLightNotification(int id, const HwLightState& state) { void Lights::setLightNotification(int id, const HwLightState& state) {
bool found = false; bool found = false;
for (auto&& [cur_id, cur_state] : notif_states_) { for (auto&& [cur_id, cur_state] : notif_states_) {
@ -149,30 +183,20 @@ void Lights::setLightNotification(int id, const HwLightState& state) {
} }
void Lights::applyNotificationState(const HwLightState& state) { void Lights::applyNotificationState(const HwLightState& state) {
std::map<std::string, int> colorValues; uint32_t white_brightness = RgbaToBrightness(state.color, max_led_brightness_);
colorValues["red"] = colorValues["green"] = colorValues["blue"] = colorValues["white"] =
RgbaToBrightness(state.color, max_led_brightness_);
auto makeLedPath = [](const std::string& led, const std::string& op) -> std::string { // Turn off the leds (initially)
return "/sys/class/leds/" + led + "/" + op; WriteToFile(WHITE_ATTR(blink), 0);
};
for (const auto& entry : colorValues) { if (state.flashMode == FlashMode::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) {
// Turn off the leds (initially) WriteToFile(WHITE_ATTR(ramp_step_ms),
WriteToFile(makeLedPath(entry.first, "breath"), 0); static_cast<uint32_t>(kRampStepDurationDefault)),
if (state.flashMode == FlashMode::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) { // White
WriteToFile(makeLedPath(entry.first, "step_ms"), WriteToFile(WHITE_ATTR(start_idx), 0);
static_cast<uint32_t>(kRampStepDurationDefault)), WriteToFile(WHITE_ATTR(pause_lo), static_cast<uint32_t>(state.flashOffMs));
WriteToFile(makeLedPath(entry.first, "pause_lo_count"), 30), WriteToFile(WHITE_ATTR(blink), 1);
WriteToFile(makeLedPath(entry.first, "lo_idx"), 0); } else {
WriteToFile(makeLedPath(entry.first, "lux_pattern"), 0); WriteToFile(WHITE_ATTR(brightness), white_brightness);
WriteToFile(makeLedPath(entry.first, "delay_on"),
static_cast<uint32_t>(state.flashOnMs));
WriteToFile(makeLedPath(entry.first, "delay_off"),
static_cast<uint32_t>(state.flashOffMs));
WriteToFile(makeLedPath(entry.first, "breath"), 1);
} else {
WriteToFile(makeLedPath(entry.first, "brightness"), entry.second);
}
} }
} }

View file

@ -35,10 +35,14 @@ class Lights : public BnLights {
ndk::ScopedAStatus getLights(std::vector<HwLight>* types) override; ndk::ScopedAStatus getLights(std::vector<HwLight>* types) override;
private: private:
void setLightBacklight(int id, const HwLightState& state);
void setLightButtons(int id, const HwLightState& state);
void setLightNotification(int id, const HwLightState& state); void setLightNotification(int id, const HwLightState& state);
void applyNotificationState(const HwLightState& state); void applyNotificationState(const HwLightState& state);
uint32_t max_button_brightness_;
uint32_t max_led_brightness_; uint32_t max_led_brightness_;
uint32_t max_screen_brightness_;
std::map<int, std::function<void(int id, const HwLightState&)>> mLights; std::map<int, std::function<void(int id, const HwLightState&)>> mLights;
std::vector<HwLight> mAvailableLights; std::vector<HwLight> mAvailableLights;
@ -48,6 +52,8 @@ class Lights : public BnLights {
{(int)LightType::NOTIFICATIONS, {}}, {(int)LightType::NOTIFICATIONS, {}},
{(int)LightType::BATTERY, {}}, {(int)LightType::BATTERY, {}},
}}; }};
std::vector<std::string> buttons_;
}; };
} // namespace light } // namespace light

View file

@ -1,9 +1,19 @@
on boot on boot
# Backlight # Notification LED
chown system system /sys/class/backlight/panel0-backlight/brightness chown system system /sys/class/leds/white/blink
chown system system /sys/class/backlight/panel0-backlight/max_brightness chown system system /sys/class/leds/white/brightness
chmod 0644 /sys/class/backlight/panel0-backlight/brightness chown system system /sys/class/leds/white/duty_pcts
chmod 0644 /sys/class/backlight/panel0-backlight/max_brightness chown system system /sys/class/leds/white/max_brightness
chown system system /sys/class/leds/white/pause_hi
chown system system /sys/class/leds/white/pause_lo
chown system system /sys/class/leds/white/ramp_step_ms
chown system system /sys/class/leds/white/start_idx
chown system system /sys/class/leds/button-backlight/max_brightness
chown system system /sys/class/leds/button-backlight1/brightness
chown system system /sys/class/leds/button-backlight1/max_brightness
chown system system /sys/class/leds/lcd-backlight/max_brightness
service vendor.light /vendor/bin/hw/android.hardware.lights-service.xiaomi_sdm660 service vendor.light /vendor/bin/hw/android.hardware.lights-service.xiaomi_sdm660
class hal class hal