Support reading since_epoch and secondary brightness files

TWFunc::Fixup_Time_On_Boot: Will now try to read
/sys/class/rtc/rtc0/since_epoch to correct time, if that fails
it will try to use the ats files to correct time.

TWFunc::Set_Brightness: One single function to set brightness
from both the automated functions and the gui actions. It is
able to set a second brightness path if present, it will set
them both the same value. Many Sony devices have 2 brightness
files in the sysfs that must be set to properly set the
brightness.

Change-Id: I7ca582109085dfbcb46b8de73ad031e4b7903fca
This commit is contained in:
xNUTx
2014-07-18 01:30:58 +02:00
committed by Ethan Yonker
parent cd56f8cb29
commit e85f02dd91
10 changed files with 110 additions and 44 deletions
+3
View File
@@ -261,6 +261,9 @@ endif
ifneq ($(TW_BRIGHTNESS_PATH),)
LOCAL_CFLAGS += -DTW_BRIGHTNESS_PATH=$(TW_BRIGHTNESS_PATH)
endif
ifneq ($(TW_SECONDARY_BRIGHTNESS_PATH),)
LOCAL_CFLAGS += -DTW_SECONDARY_BRIGHTNESS_PATH=$(TW_SECONDARY_BRIGHTNESS_PATH)
endif
ifneq ($(TW_MAX_BRIGHTNESS),)
LOCAL_CFLAGS += -DTW_MAX_BRIGHTNESS=$(TW_MAX_BRIGHTNESS)
endif
+14 -6
View File
@@ -976,8 +976,17 @@ void DataManager::SetDefaultValues()
mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
#ifdef TW_SECONDARY_BRIGHTNESS_PATH
string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
} else {
LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
}
#endif
string max_bright = maxVal.str();
TWFunc::write_file(findbright, max_bright);
TWFunc::Set_Brightness(max_bright);
}
#endif
mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
@@ -1139,12 +1148,11 @@ void DataManager::ReadSettingsFile(void)
#endif // ifdef TW_OEM_BUILD
PartitionManager.Mount_All_Storage();
update_tz_environment_variables();
string brightness_path = GetStrValue("tw_brightness_file");
if (!brightness_path.empty() && brightness_path != "/nobrightness" && TWFunc::Path_Exists(brightness_path)) {
string brightness_value = GetStrValue("tw_brightness");
TWFunc::write_file(brightness_path, brightness_value);
#ifdef TW_MAX_BRIGHTNESS
if (GetStrValue("tw_brightness_path") != "/nobrightness") {
TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
}
#endif
}
string DataManager::GetCurrentStoragePath(void)
+5
View File
@@ -824,6 +824,11 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
return 0;
}
if (function == "setbrightness")
{
return TWFunc::Set_Brightness(arg);
}
if (isThreaded)
{
if (function == "fileexists")
+11 -29
View File
@@ -97,11 +97,11 @@ int blanktimer::setClockTimer(void) {
if (sleepTimer > 2 && diff.tv_sec > (sleepTimer - 2) && conblank == 0) {
orig_brightness = getBrightness();
setConBlank(1);
setBrightness(5);
TWFunc::Set_Brightness("5");
}
if (sleepTimer && diff.tv_sec > sleepTimer && conblank < 2) {
setConBlank(2);
setBrightness(0);
TWFunc::Set_Brightness("0");
screenoff = true;
TWFunc::check_and_run_script("/sbin/postscreenblank.sh", "blank");
PageManager::ChangeOverlay("lock");
@@ -115,39 +115,20 @@ int blanktimer::setClockTimer(void) {
return -1; //shouldn't get here
}
int blanktimer::getBrightness(void) {
string results;
string blanktimer::getBrightness(void) {
string result;
string brightness_path;
DataManager::GetValue("tw_brightness_file", brightness_path);
if ((TWFunc::read_file(brightness_path, results)) != 0)
return -1;
int result = atoi(results.c_str());
if (result == 0) {
int tw_brightness;
DataManager::GetValue("tw_brightness", tw_brightness);
if (tw_brightness) {
result = tw_brightness;
} else {
result = 255;
}
if (brightness_path == "/nobrightness")
return brightness_path;
DataManager::GetValue("tw_brightness", result);
if (result == "") {
result = "255";
}
return result;
}
int blanktimer::setBrightness(int brightness) {
string brightness_path;
string bstring;
char buff[100];
DataManager::GetValue("tw_brightness_file", brightness_path);
sprintf(buff, "%d", brightness);
bstring = buff;
if ((TWFunc::write_file(brightness_path, bstring)) != 0)
return -1;
gui_forceRender();
return 0;
}
void blanktimer::resetTimerAndUnblank(void) {
setTimer();
switch (conblank) {
@@ -165,7 +146,8 @@ void blanktimer::resetTimerAndUnblank(void) {
screenoff = false;
// No break here, we want to keep going
case 1:
setBrightness(orig_brightness);
if (orig_brightness != "/nobrightness")
TWFunc::Set_Brightness(orig_brightness);
setConBlank(0);
break;
}
+2 -2
View File
@@ -41,7 +41,7 @@ private:
void setConBlank(int blank);
void setTimer(void);
timespec getTimer(void);
int getBrightness(void);
string getBrightness(void);
int setBrightness(int brightness);
int setBlankTimer(void);
int setClockTimer(void);
@@ -51,7 +51,7 @@ private:
int conblank;
timespec btimer;
unsigned long long sleepTimer;
int orig_brightness;
string orig_brightness;
bool screenoff;
};
+1 -1
View File
@@ -2589,7 +2589,7 @@
<action function="set">tw_brightness=%tw_brightness_max%</action>
<action function="compute">tw_brightness*%tw_brightness_pct%</action>
<action function="compute">tw_brightness/100</action>
<action function="cmd">echo %tw_brightness% > &quot;%tw_brightness_file%&quot;</action>
<action function="setbrightness">%tw_brightness%</action>
</actions>
</object>
+1 -1
View File
@@ -2628,7 +2628,7 @@
<action function="set">tw_brightness=%tw_brightness_max%</action>
<action function="compute">tw_brightness*%tw_brightness_pct%</action>
<action function="compute">tw_brightness/100</action>
<action function="cmd">echo %tw_brightness% > &quot;%tw_brightness_file%&quot;</action>
<action function="setbrightness">%tw_brightness%</action>
</actions>
</object>
+1 -1
View File
@@ -2592,7 +2592,7 @@
<action function="set">tw_brightness=%tw_brightness_max%</action>
<action function="compute">tw_brightness*%tw_brightness_pct%</action>
<action function="compute">tw_brightness/100</action>
<action function="cmd">echo %tw_brightness% > &quot;%tw_brightness_file%&quot;</action>
<action function="setbrightness">%tw_brightness%</action>
</actions>
</object>
+70 -4
View File
@@ -659,6 +659,20 @@ int TWFunc::read_file(string fn, vector<string>& results) {
return -1;
}
int TWFunc::read_file(string fn, uint64_t& results) {
ifstream file;
file.open(fn.c_str(), ios::in);
if (file.is_open()) {
file >> results;
file.close();
return 0;
}
LOGINFO("Cannot find file %s\n", fn.c_str());
return -1;
}
int TWFunc::write_file(string fn, string& line) {
FILE *file;
file = fopen(fn.c_str(), "w");
@@ -1076,6 +1090,38 @@ void TWFunc::Auto_Generate_Backup_Name() {
void TWFunc::Fixup_Time_On_Boot()
{
#ifdef QCOM_RTC_FIX
LOGINFO("TWFunc::Fixup_Time: Pre-fix date and time: %s\n", TWFunc::Get_Current_Date().c_str());
struct timeval tv;
uint64_t offset = 0;
std::string sepoch = "/sys/class/rtc/rtc0/since_epoch";
if (TWFunc::read_file(sepoch, offset) == 0) {
LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s\n", sepoch.c_str());
tv.tv_sec = offset;
tv.tv_usec = 0;
settimeofday(&tv, NULL);
gettimeofday(&tv, NULL);
if (tv.tv_sec > 1405209403) { // Anything older then 12 Jul 2014 23:56:43 GMT will do nicely thank you ;)
LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str());
return;
}
} else {
LOGINFO("TWFunc::Fixup_Time: opening %s failed\n", sepoch.c_str());
}
LOGINFO("TWFunc::Fixup_Time: will attempt to use the ats files now.\n", sepoch.c_str());
// Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC.
// They never set it, it just ticks forward from 1970-01-01 00:00,
// and then they have files /data/system/time/ats_* with 64bit offset
@@ -1087,10 +1133,9 @@ void TWFunc::Fixup_Time_On_Boot()
static const char *paths[] = { "/data/system/time/", "/data/time/" };
DIR *d;
FILE *f;
uint64_t offset = 0;
struct timeval tv;
DIR *d;
offset = 0;
struct dirent *dt;
std::string ats_path;
@@ -1120,7 +1165,7 @@ void TWFunc::Fixup_Time_On_Boot()
if(ats_path.empty())
{
LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving time as-is!\n");
LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving untouched!\n");
return;
}
@@ -1153,6 +1198,9 @@ void TWFunc::Fixup_Time_On_Boot()
}
settimeofday(&tv, NULL);
LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str());
#endif
}
@@ -1194,4 +1242,22 @@ bool TWFunc::Create_Dir_Recursive(const std::string& path, mode_t mode, uid_t ui
return true;
}
int TWFunc::Set_Brightness(std::string brightness_value)
{
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");
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());
TWFunc::write_file(secondary_brightness_file, brightness_value);
}
return result;
}
return -1;
}
#endif // ndef BUILD_TWRPTAR_MAIN
+2
View File
@@ -70,6 +70,7 @@ public:
static int32_t timespec_diff_ms(timespec& start, timespec& end); // Returns diff in ms
static int read_file(string fn, vector<string>& results); //read from file
static int read_file(string fn, string& results); //read from file
static int read_file(string fn, uint64_t& results); //read from file
static int write_file(string fn, string& line); //write from file
static int drop_caches(void); //drop linux cache memory
static int Check_su_Perms(void); // check perms and owner of su binary in various locations
@@ -83,6 +84,7 @@ public:
static void Fixup_Time_On_Boot(); // Fixes time on devices which need it
static std::vector<std::string> Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty = true); // Splits string by delimiter
static bool Create_Dir_Recursive(const std::string& path, mode_t mode = 0755, uid_t uid = -1, gid_t gid = -1); // Create directory and it's parents, if they don't exist. mode, uid and gid are set to all _newly_ created folders. If whole path exists, do nothing.
static int Set_Brightness(std::string brightness_value); // Well, you can read, it does what it says, passing return int from TWFunc::Write_File ;)
private:
static void Copy_Log(string Source, string Destination);