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
diff --git a/data.cpp b/data.cpp
index c945f2a..e38f366 100644
--- a/data.cpp
+++ b/data.cpp
@@ -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 @@
 #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 @@
 		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 @@
 #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 @@
 		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 @@
 	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
 }
 
diff --git a/gui/blanktimer.cpp b/gui/blanktimer.cpp
index 95b6c47..06208e2 100644
--- a/gui/blanktimer.cpp
+++ b/gui/blanktimer.cpp
@@ -81,16 +81,13 @@
 
 string blanktimer::getBrightness(void) {
 	string result;
-	string brightness_path;
-	DataManager::GetValue("tw_brightness_file", brightness_path);
-	if (brightness_path == "/nobrightness")
-		return brightness_path;
-	DataManager::GetValue("tw_brightness", result);
-	if (result == "") {
-		result = "255";
+
+	if (DataManager::GetIntValue("tw_has_brightnesss_file")) {
+		DataManager::GetValue("tw_brightness", result);
+		if (result.empty())
+			result = "255";
 	}
 	return result;
-
 }
 
 void blanktimer::resetTimerAndUnblank(void) {
@@ -112,7 +109,7 @@
 			gui_forceRender();
 			// No break here, we want to keep going
 		case kDim:
-			if (orig_brightness != "/nobrightness")
+			if (!orig_brightness.empty())
 				TWFunc::Set_Brightness(orig_brightness);
 			state = kOn;
 		case kOn:
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index e73a52d..63ed0a6 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -981,20 +981,19 @@
 
 int TWFunc::Set_Brightness(std::string brightness_value)
 {
+	int result = -1;
+	std::string secondary_brightness_file;
 
-	std::string brightness_file = DataManager::GetStrValue("tw_brightness_file");;
-
-	if (brightness_file.compare("/nobrightness") != 0) {
-		std::string secondary_brightness_file = DataManager::GetStrValue("tw_secondary_brightness_file");
+	if (DataManager::GetIntValue("tw_has_brightnesss_file")) {
 		LOGINFO("TWFunc::Set_Brightness: Setting brightness control to %s\n", brightness_value.c_str());
-		int result = TWFunc::write_file(brightness_file, brightness_value);
-		if (secondary_brightness_file != "") {
-			LOGINFO("TWFunc::Set_Brightness: Setting SECONDARY brightness control to %s\n", brightness_value.c_str());
+		result = TWFunc::write_file(DataManager::GetStrValue("tw_brightness_file"), brightness_value);
+		DataManager::GetValue("tw_secondary_brightness_file", secondary_brightness_file);
+		if (!secondary_brightness_file.empty()) {
+			LOGINFO("TWFunc::Set_Brightness: Setting secondary brightness control to %s\n", brightness_value.c_str());
 			TWFunc::write_file(secondary_brightness_file, brightness_value);
 		}
-		return result;
 	}
-	return -1;
+	return result;
 }
 
 bool TWFunc::Toggle_MTP(bool enable) {
diff --git a/variables.h b/variables.h
index f7304b4..e66f8c3 100644
--- a/variables.h
+++ b/variables.h
@@ -172,10 +172,6 @@
 #define CUSTOM_LUN_FILE "/sys/class/android_usb/android0/f_mass_storage/lun%d/file"
 #endif
 
-#ifndef TW_BRIGHTNESS_PATH
-#define TW_BRIGHTNESS_PATH /nobrightness
-#endif
-
 // For OpenRecoveryScript
 #define SCRIPT_FILE_CACHE "/cache/recovery/openrecoveryscript"
 #define SCRIPT_FILE_TMP "/tmp/openrecoveryscript"