Improve max brightness handling

If we can detect the directory, why don't we detect maximum as well?

Additional patch by: Matt Mower
Replace clunky /nobrightness handling

Cleanup TWFunc::Set_Brightness() and blanktimer::getBrightness() a
bit, primarily for the purpose of relying on the value in
tw_has_brightnesss_file instead of checking for a phony brightness
path.

Change-Id: Ib22595df53cefa8db7a1172a581984c42ad461c6
This commit is contained in:
Tatsuyuki Ishi
2016-01-22 18:06:00 +01:00
committed by Dees Troy
parent 6069a793ea
commit 548a175182
4 changed files with 50 additions and 43 deletions
+36 -21
View File
@@ -55,10 +55,6 @@
#define DEVID_MAX 64
#define HWID_MAX 32
#ifndef TW_MAX_BRIGHTNESS
#define TW_MAX_BRIGHTNESS 255
#endif
extern "C"
{
#include "twcommon.h"
@@ -829,16 +825,15 @@ void DataManager::SetDefaultValues()
#endif
mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
#ifdef TW_BRIGHTNESS_PATH
string findbright;
if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
findbright = EXPAND(TW_BRIGHTNESS_PATH);
LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
if (!TWFunc::Path_Exists(findbright)) {
LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
findbright = "";
}
#ifdef TW_BRIGHTNESS_PATH
findbright = EXPAND(TW_BRIGHTNESS_PATH);
LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
if (!TWFunc::Path_Exists(findbright)) {
LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
findbright = "";
}
#endif
if (findbright.empty()) {
// Attempt to locate the brightness file
findbright = Find_File::Find("brightness", "/sys/class/backlight");
@@ -851,10 +846,33 @@ void DataManager::SetDefaultValues()
LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
mConstValues.insert(make_pair("tw_brightness_file", findbright));
string maxBrightness;
#ifdef TW_MAX_BRIGHTNESS
ostringstream maxVal;
maxVal << TW_MAX_BRIGHTNESS;
mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
maxBrightness = maxVal.str();
#else
// Attempt to locate the max_brightness file
string maxbrightpath = findbright.insert(findbright.rfind('/') + 1, "max_");
if (TWFunc::Path_Exists(maxbrightpath)) {
ifstream maxVal(maxbrightpath);
if(maxVal >> maxBrightness) {
LOGINFO("Got max brightness %s from '%s'\n", maxBrightness.c_str(), maxbrightpath.c_str());
} else {
// Something went wrong, set that to indicate error
maxBrightness = "-1";
}
}
if(stoi(maxBrightness) <= 0)
{
// Fallback into default
ostringstream maxVal;
maxVal << 255;
maxBrightness = maxVal.str();
}
#endif
mConstValues.insert(make_pair("tw_brightness_max", maxBrightness));
mValues.insert(make_pair("tw_brightness", make_pair(maxBrightness, 1)));
mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
#ifdef TW_SECONDARY_BRIGHTNESS_PATH
string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
@@ -867,7 +885,7 @@ void DataManager::SetDefaultValues()
#endif
#ifdef TW_DEFAULT_BRIGHTNESS
int defValInt = TW_DEFAULT_BRIGHTNESS;
int maxValInt = TW_MAX_BRIGHTNESS;
int maxValInt = stoi(maxBrightness);
// Deliberately int so the % is always a whole number
int defPctInt = ( ( (double)defValInt / maxValInt ) * 100 );
ostringstream defPct;
@@ -879,12 +897,11 @@ void DataManager::SetDefaultValues()
defVal << TW_DEFAULT_BRIGHTNESS;
mValues.erase("tw_brightness");
mValues.insert(make_pair("tw_brightness", make_pair(defVal.str(), 1)));
TWFunc::Set_Brightness(defVal.str());
TWFunc::Set_Brightness(defVal.str());
#else
TWFunc::Set_Brightness(maxVal.str());
TWFunc::Set_Brightness(maxBrightness);
#endif
}
#endif
mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
@@ -1096,9 +1113,7 @@ void DataManager::ReadSettingsFile(void)
PartitionManager.Mount_All_Storage();
update_tz_environment_variables();
#ifdef TW_MAX_BRIGHTNESS
if (GetStrValue("tw_brightness_path") != "/nobrightness") {
TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
}
TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
#endif
}