blob: 88bc86adae0b8f54fd15dc5ff016aee19461f044 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
Dees Troy3be70a82013-10-22 14:25:12 +00002 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <time.h>
32#include <unistd.h>
33#include <stdlib.h>
34
35#include <string>
36#include <utility>
37#include <map>
38#include <fstream>
39#include <sstream>
40
41#include "variables.h"
42#include "data.hpp"
43#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040044#include "twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060046#include "gui/blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070047#endif
Ethan Yonker00028b42014-04-09 14:29:02 -050048#include "find_file.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060049#include "set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070051#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 #include "cutils/properties.h"
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070053#endif
54
Ethan Yonkera18f1082014-07-07 15:07:58 -050055#ifndef TW_MAX_BRIGHTNESS
56#define TW_MAX_BRIGHTNESS 255
57#endif
58
Dees_Troy51a0e822012-09-05 15:24:24 -040059extern "C"
60{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020061 #include "twcommon.h"
62 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040063 #include "gui/pages.h"
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000064 #include "minuitwrp/minui.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040065 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040066}
67
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068#define FILE_VERSION 0x00010001
Dees_Troy51a0e822012-09-05 15:24:24 -040069
70using namespace std;
71
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070072map<string, DataManager::TStrIntPair> DataManager::mValues;
73map<string, string> DataManager::mConstValues;
74string DataManager::mBackingFile;
75int DataManager::mInitialized = 0;
Jenkins1710bf22014-10-02 20:22:21 -040076
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070077#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060078extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070079#endif
Ethan Yonker6277c792014-09-15 14:54:30 -050080extern bool datamedia;
Dees_Troy51a0e822012-09-05 15:24:24 -040081
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040082// Device ID functions
83void DataManager::sanitize_device_id(char* device_id) {
84 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
85 char str[50];
86 char* c = str;
87
88 strcpy(str, device_id);
89 memset(device_id, 0, sizeof(device_id));
90 while (*c) {
91 if (strchr(whitelist, *c))
92 strncat(device_id, c, 1);
93 c++;
94 }
95 return;
96}
97
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098#define CMDLINE_SERIALNO "androidboot.serialno="
99#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
100#define CPUINFO_SERIALNO "Serial"
101#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
102#define CPUINFO_HARDWARE "Hardware"
103#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400104
105void DataManager::get_device_id(void) {
106 FILE *fp;
107 char line[2048];
108 char hardware_id[32], device_id[64];
109 char* token;
110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 // Assign a blank device_id to start with
112 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700113
114#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
115 // Now we'll use product_model_hardwareid as device id
116 char model_id[PROPERTY_VALUE_MAX];
117 property_get("ro.product.model", model_id, "error");
118 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000119 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700120 // Replace spaces with underscores
121 for(int i = 0; i < strlen(model_id); i++) {
122 if(model_id[i] == ' ')
123 model_id[i] = '_';
124 }
125 strcpy(device_id, model_id);
126 if (hardware_id[0] != 0) {
127 strcat(device_id, "_");
128 strcat(device_id, hardware_id);
129 }
130 sanitize_device_id((char *)device_id);
131 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700133 return;
134 }
135#endif
136
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400137#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 // First, try the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400139 fp = fopen("/proc/cmdline", "rt");
140 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 {
142 // First step, read the line. For cmdline, it's one long line
143 fgets(line, sizeof(line), fp);
144 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 // Now, let's tokenize the string
147 token = strtok(line, " ");
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400148
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
150 while (token)
151 {
152 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
153 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
154 {
155 // We found the serial number!
156 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400157 sanitize_device_id((char *)device_id);
158 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 return;
160 }
161 token = strtok(NULL, " ");
162 }
163 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400164#endif
165 // Now we'll try cpuinfo for a serial number
166 fp = fopen("/proc/cpuinfo", "rt");
167 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 {
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400169 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
170 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
171 {
172 // We found the serial number!
173 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
174 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
175 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400177 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
178 memset(device_id, 0, sizeof(device_id));
179 strncpy(device_id, token, strlen(token) - 1);
180 } else {
181 strcpy(device_id, token);
182 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000183 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400184 fclose(fp);
185 sanitize_device_id((char *)device_id);
186 mConstValues.insert(make_pair("device_id", device_id));
187 return;
188 }
189 } else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
190 // We found the hardware ID
191 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
192 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
193 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400195 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200196 memset(hardware_id, 0, sizeof(hardware_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400197 strncpy(hardware_id, token, strlen(token) - 1);
198 } else {
199 strcpy(hardware_id, token);
200 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000201 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400202 }
203 }
204 }
205 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400207
208 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400210 strcpy(device_id, hardware_id);
211 sanitize_device_id((char *)device_id);
212 mConstValues.insert(make_pair("device_id", device_id));
213 return;
214 }
215
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000217 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400218 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400220}
221
Dees_Troy51a0e822012-09-05 15:24:24 -0400222int DataManager::ResetDefaults()
223{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200224 mValues.clear();
225 mConstValues.clear();
226 SetDefaultValues();
227 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400228}
229
230int DataManager::LoadValues(const string filename)
231{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400233
234 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400238 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 // Read in the file, if possible
242 FILE* in = fopen(filename.c_str(), "rb");
243 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 return 0;
246 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 int file_version;
251 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
252 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 while (!feof(in))
255 {
256 string Name;
257 string Value;
258 unsigned short length;
259 char array[512];
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
262 if (length >= 512) goto error;
263 if (fread(array, 1, length, in) != length) goto error;
264 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400265
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
267 if (length >= 512) goto error;
268 if (fread(array, 1, length, in) != length) goto error;
269 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400270
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200271 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 pos = mValues.find(Name);
274 if (pos != mValues.end())
275 {
276 pos->second.first = Value;
277 pos->second.second = 1;
278 }
279 else
280 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700281#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500282 if (Name == "tw_screen_timeout_secs")
283 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700284#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400286error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500288 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500289 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200290 if(!Part)
291 Part = PartitionManager.Get_Default_Storage_Partition();
292 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500293 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
294 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500295 } else {
296 SetBackupFolder();
297 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400299}
300
301int DataManager::Flush()
302{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400304}
305
306int DataManager::SaveValues()
307{
Ethan Yonker83e82572014-04-04 10:59:28 -0500308#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 if (mBackingFile.empty())
310 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
312 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400314
315 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 if (!out)
317 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400318
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200319 int file_version = FILE_VERSION;
320 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400321
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200322 map<string, TStrIntPair>::iterator iter;
323 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
324 {
325 // Save only the persisted data
326 if (iter->second.second != 0)
327 {
328 unsigned short length = (unsigned short) iter->first.length() + 1;
329 fwrite(&length, 1, sizeof(unsigned short), out);
330 fwrite(iter->first.c_str(), 1, length, out);
331 length = (unsigned short) iter->second.first.length() + 1;
332 fwrite(&length, 1, sizeof(unsigned short), out);
333 fwrite(iter->second.first.c_str(), 1, length, out);
334 }
335 }
336 fclose(out);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600337 tw_set_default_metadata(mBackingFile.c_str());
Ethan Yonker83e82572014-04-04 10:59:28 -0500338#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342int DataManager::GetValue(const string varName, string& value)
343{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200346 if (!mInitialized)
347 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400348
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200349 // Strip off leading and trailing '%' if provided
350 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
351 {
352 localStr.erase(0, 1);
353 localStr.erase(localStr.length() - 1, 1);
354 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 // Handle magic values
357 if (GetMagicValue(localStr, value) == 0)
358 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 map<string, string>::iterator constPos;
360 constPos = mConstValues.find(localStr);
361 if (constPos != mConstValues.end())
362 {
363 value = constPos->second;
364 return 0;
365 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400366
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 map<string, TStrIntPair>::iterator pos;
368 pos = mValues.find(localStr);
369 if (pos == mValues.end())
370 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200372 value = pos->second.first;
373 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400374}
375
376int DataManager::GetValue(const string varName, int& value)
377{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200378 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 if (GetValue(varName,data) != 0)
381 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200383 value = atoi(data.c_str());
384 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385}
386
Dees_Troy2673cec2013-04-02 20:22:16 +0000387int DataManager::GetValue(const string varName, float& value)
388{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200389 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000390
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200391 if (GetValue(varName,data) != 0)
392 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000393
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200394 value = atof(data.c_str());
395 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000396}
397
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500398unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
399{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500401
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200402 if (GetValue(varName,data) != 0)
403 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500404
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 value = strtoull(data.c_str(), NULL, 10);
406 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500407}
408
Dees_Troy51a0e822012-09-05 15:24:24 -0400409// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
410string& DataManager::GetValueRef(const string varName)
411{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 if (!mInitialized)
413 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400414
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200415 map<string, string>::iterator constPos;
416 constPos = mConstValues.find(varName);
417 if (constPos != mConstValues.end())
418 return constPos->second;
Dees_Troy51a0e822012-09-05 15:24:24 -0400419
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200420 map<string, TStrIntPair>::iterator pos;
421 pos = mValues.find(varName);
422 if (pos == mValues.end())
423 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400424
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200425 return pos->second.first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426}
427
428// This function will return an empty string if the value doesn't exist
429string DataManager::GetStrValue(const string varName)
430{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200431 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200433 GetValue(varName, retVal);
434 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400435}
436
437// This function will return 0 if the value doesn't exist
438int DataManager::GetIntValue(const string varName)
439{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200440 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400441
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200442 GetValue(varName, retVal);
443 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400444}
445
446int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
447{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200448 if (!mInitialized)
449 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400450
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200451 // Don't allow empty values or numerical starting values
452 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
453 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400454
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200455 map<string, string>::iterator constChk;
456 constChk = mConstValues.find(varName);
457 if (constChk != mConstValues.end())
458 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200460 map<string, TStrIntPair>::iterator pos;
461 pos = mValues.find(varName);
462 if (pos == mValues.end())
463 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
464 else
465 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400466
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200467 if (pos->second.second != 0)
468 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700469
470#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500471 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600472 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700473 } else
474#endif
475 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500477 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200479 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
482int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
483{
484 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200485 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 if (varName == "tw_use_external_storage") {
487 string str;
488
489 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
490 if (value == 0) {
491 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 } else {
493 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 }
495 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
496 str = GetStrValue(TW_INTERNAL_PATH);
497 else
498 str = GetStrValue(TW_EXTERNAL_PATH);
499
Dees_Troya13d74f2013-03-24 08:54:55 -0500500 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200502 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400503}
504
505int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
506{
507 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508 valStr << value;
509 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400510}
511
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500512int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
513{
514 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200515 valStr << value;
516 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500517}
518
Dees_Troy2673cec2013-04-02 20:22:16 +0000519int DataManager::SetProgress(float Fraction) {
520 return SetValue("ui_progress", (float) (Fraction * 100.0));
521}
522
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200523int DataManager::ShowProgress(float Portion, float Seconds)
524{
Dees_Troy2673cec2013-04-02 20:22:16 +0000525 float Starting_Portion;
526 GetValue("ui_progress_portion", Starting_Portion);
527 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
528 return -1;
529 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
530 return -1;
531 return 0;
532}
533
Dees_Troy51a0e822012-09-05 15:24:24 -0400534void DataManager::DumpValues()
535{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200536 map<string, TStrIntPair>::iterator iter;
537 gui_print("Data Manager dump - Values with leading X are persisted.\n");
538 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
539 gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400540}
541
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542void DataManager::update_tz_environment_variables(void)
543{
Dees_Troy8170a922012-09-18 15:40:25 -0400544 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200545 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400546}
547
Dees_Troy16b74352012-11-14 22:27:31 +0000548void DataManager::SetBackupFolder()
549{
550 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500551 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000552 str += "/TWRP/BACKUPS/";
553
554 string dev_id;
555 GetValue("device_id", dev_id);
556
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200557 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000558 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000559 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500560 if (partition != NULL) {
561 SetValue("tw_storage_display_name", partition->Storage_Name);
562 char free_space[255];
563 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
564 SetValue("tw_storage_free_size", free_space);
565 string zip_path, zip_root, storage_path;
566 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
567 if (partition->Has_Data_Media)
568 storage_path = partition->Symlink_Mount_Point;
569 else
570 storage_path = partition->Storage_Path;
571 if (zip_path.size() < storage_path.size()) {
572 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
573 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000574 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500575 if (zip_root != storage_path) {
576 LOGINFO("DataManager::SetBackupFolder zip path was %s changing to %s, %s\n", zip_path.c_str(), storage_path.c_str(), zip_root.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500577 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500578 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500579 }
580 } else {
581 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000582 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500583 }
Dees_Troy16b74352012-11-14 22:27:31 +0000584}
585
Dees_Troy51a0e822012-09-05 15:24:24 -0400586void DataManager::SetDefaultValues()
587{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200590 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400591
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200592 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594 mConstValues.insert(make_pair("true", "1"));
595 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400596
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200597 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Ethan Yonker03db3262014-02-05 08:02:06 -0600598 mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1)));
599 mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1)));
600 mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400601
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200602 TWPartition *store = PartitionManager.Get_Default_Storage_Partition();
603 if(store)
604 mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1)));
605 else
606 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
607
Dees_Troyf4499812013-01-23 19:07:38 +0000608#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
609 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
610#endif
611
Dees_Troy51a0e822012-09-05 15:24:24 -0400612#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000613 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200614 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400615#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200616 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400617#endif
618
619#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000620 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
622#else
623 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
624#endif
625
Dees_Troy51a0e822012-09-05 15:24:24 -0400626 str = GetCurrentStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -0400627 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400629
630 string dev_id;
631 GetValue("device_id", dev_id);
632
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200633 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400634 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
635
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200636 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400637#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100638 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400639 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
640#else
Dees_Troya58bead2012-09-27 09:49:29 -0400641 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400642#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200643 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400644#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100645 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400646 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
647#else
Dees_Troya58bead2012-09-27 09:49:29 -0400648 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400649#endif
650#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000651 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400652 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
Ethan Yonker6277c792014-09-15 14:54:30 -0500653 mConstValues.insert(make_pair("tw_has_internal", "1"));
654 datamedia = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400655#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500656 mValues.insert(make_pair(TW_HAS_DATA_MEDIA, make_pair("0", 0)));
657 mValues.insert(make_pair("tw_has_internal", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400658#endif
659#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000660 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
662#else
663 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
664#endif
Jenkins1710bf22014-10-02 20:22:21 -0400665#ifdef TW_NO_CPU_TEMP
666 printf("TW_NO_CPU_TEMP := true\n");
667 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
668#else
669 string cpu_temp_file;
670#ifdef TW_CUSTOM_CPU_TEMP_PATH
671 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
672#else
673 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
674#endif
675 if (TWFunc::Path_Exists(cpu_temp_file)) {
676 mConstValues.insert(make_pair("tw_no_cpu_temp", "0"));
677 } else {
678 LOGINFO("CPU temperature file '%s' not found, disabling CPU temp.\n", cpu_temp_file.c_str());
679 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
680 }
681#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400682#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000683 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400684 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
685#else
686 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
687#endif
688#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000689 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
691#endif
692#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000693 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
695#else
696 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
697#endif
698#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000699 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
701#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000702 char lun_file[255];
703 string Lun_File_str = CUSTOM_LUN_FILE;
704 size_t found = Lun_File_str.find("%");
705 if (found != string::npos) {
706 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
707 Lun_File_str = lun_file;
708 }
709 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000711 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
712 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000713 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000714 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
715 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400716#endif
717#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000718 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
720 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
721#else
722 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
723 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
724#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400725#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000726 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400727 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
728#endif
729#ifdef TW_INCLUDE_CRYPTO
730 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000731 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400732#endif
733#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000734 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
736#else
737 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
738#endif
739
Dees_Troya13d74f2013-03-24 08:54:55 -0500740#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500741 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500742#else
Dees_Troyf100c942013-06-21 08:15:31 -0500743 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500744#endif
745 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000746 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500747
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200748 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
749 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
750 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
751 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
752 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
753 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
754 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
755 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
756 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
757 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
758 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
759 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
760 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
761 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
762 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
763 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
764 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
765 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
766 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200767 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
768 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
770 SetValue(TW_HAS_USB_STORAGE, 0, 0);
771 else
772 SetValue(TW_HAS_USB_STORAGE, 1, 0);
773 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
774 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
775 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
776 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
777 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
778 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
779 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
780 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
781 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
782 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
783 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
784 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500785 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700786#ifdef TW_NO_SCREEN_TIMEOUT
787 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
788 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
789#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600790 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700791 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
792#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600793 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000794 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200795#ifdef TW_BRIGHTNESS_PATH
Ethan Yonker00028b42014-04-09 14:29:02 -0500796 string findbright;
Dees_Troy2f9117a2013-02-17 19:52:09 -0600797 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Ethan Yonker00028b42014-04-09 14:29:02 -0500798 findbright = EXPAND(TW_BRIGHTNESS_PATH);
799 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
800 if (!TWFunc::Path_Exists(findbright)) {
801 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
802 findbright = "";
803 }
804 }
805 if (findbright.empty()) {
806 // Attempt to locate the brightness file
807 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500808 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500809 }
810 if (findbright.empty()) {
811 LOGINFO("Unable to locate brightness file\n");
812 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
813 } else {
814 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600815 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500816 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Vojtech Bocek85932342013-04-01 22:11:33 +0200817 ostringstream maxVal;
818 maxVal << TW_MAX_BRIGHTNESS;
819 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
820 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
821 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
xNUTxe85f02d2014-07-18 01:30:58 +0200822#ifdef TW_SECONDARY_BRIGHTNESS_PATH
823 string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
824 if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
825 LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
826 mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
827 } else {
828 LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
829 }
830#endif
Ethan Yonkera18f1082014-07-07 15:07:58 -0500831 string max_bright = maxVal.str();
xNUTxe85f02d2014-07-18 01:30:58 +0200832 TWFunc::Set_Brightness(max_bright);
Dees_Troy2f9117a2013-02-17 19:52:09 -0600833 }
834#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500835 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000836#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
837 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
838#else
839 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
840 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
841#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400842#ifdef TW_HAS_MTP
843 mConstValues.insert(make_pair("tw_has_mtp", "1"));
844 mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1)));
Ethan Yonkerc8743cf2014-09-03 21:16:40 -0500845 mValues.insert(make_pair("tw_mtp_debug", make_pair("0", 1)));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400846#else
847 LOGINFO("TW_EXCLUDE_MTP := true\n");
848 mConstValues.insert(make_pair("tw_has_mtp", "0"));
849 mConstValues.insert(make_pair("tw_mtp_enabled", "0"));
850#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400851}
852
853// Magic Values
854int DataManager::GetMagicValue(const string varName, string& value)
855{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200856 // Handle special dynamic cases
857 if (varName == "tw_time")
858 {
859 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400860
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200861 struct tm *current;
862 time_t now;
863 int tw_military_time;
864 now = time(0);
865 current = localtime(&now);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500866 GetValue(TW_MILITARY_TIME, tw_military_time);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200867 if (current->tm_hour >= 12)
868 {
869 if (tw_military_time == 1)
870 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
871 else
872 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500873 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500874 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200875 {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500876 if (tw_military_time == 1)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200877 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
878 else
879 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
880 }
881 value = tmp;
882 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500883 }
Jenkins1710bf22014-10-02 20:22:21 -0400884 else if (varName == "tw_cpu_temp")
885 {
886 string cpu_temp_file;
887 static unsigned long convert_temp = 0;
888 static time_t cpuSecCheck = 0;
889 int divisor = 0;
890 struct timeval curTime;
891 string results;
892
893 gettimeofday(&curTime, NULL);
894 if (curTime.tv_sec > cpuSecCheck)
895 {
896#ifdef TW_CUSTOM_CPU_TEMP_PATH
897 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
898 if (TWFunc::read_file(cpu_temp_file, results) != 0)
899 return -1;
900#else
901 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
902 if (TWFunc::read_file(cpu_temp_file, results) != 0)
903 return -1;
904#endif
905 convert_temp = strtoul(results.c_str(), NULL, 0) / 1000;
906 if (convert_temp <= 0)
907 convert_temp = strtoul(results.c_str(), NULL, 0);
HandyMennyb6033452014-10-15 21:39:12 +0200908 if (convert_temp >= 150)
909 convert_temp = strtoul(results.c_str(), NULL, 0) / 10;
910 cpuSecCheck = curTime.tv_sec + 5;
Jenkins1710bf22014-10-02 20:22:21 -0400911 }
912 value = TWFunc::to_string(convert_temp);
913 return 0;
914 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200915 else if (varName == "tw_battery")
916 {
917 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400918 static char charging = ' ';
919 static int lastVal = -1;
920 static time_t nextSecCheck = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 struct timeval curTime;
922 gettimeofday(&curTime, NULL);
923 if (curTime.tv_sec > nextSecCheck)
924 {
925 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000926#ifdef TW_CUSTOM_BATTERY_PATH
927 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
928 capacity_file += "/capacity";
929 FILE * cap = fopen(capacity_file.c_str(),"rt");
930#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400931 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000932#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400933 if (cap){
934 fgets(cap_s, 4, cap);
935 fclose(cap);
936 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200937 if (lastVal > 100) lastVal = 101;
938 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400939 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000940#ifdef TW_CUSTOM_BATTERY_PATH
941 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
942 status_file += "/status";
943 cap = fopen(status_file.c_str(),"rt");
944#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400945 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000946#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400947 if (cap) {
948 fgets(cap_s, 2, cap);
949 fclose(cap);
950 if (cap_s[0] == 'C')
951 charging = '+';
952 else
953 charging = ' ';
954 }
955 nextSecCheck = curTime.tv_sec + 60;
956 }
957
958 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200959 value = tmp;
960 return 0;
961 }
962 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400963}
964
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200965void DataManager::Output_Version(void)
966{
Dees_Troy1c1ac442013-01-17 21:42:14 +0000967 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400968 char version[255];
969
Dees_Troy1c1ac442013-01-17 21:42:14 +0000970 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000971 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400972 return;
973 }
Dees_Troy1c1ac442013-01-17 21:42:14 +0000974 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000975 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +0000976 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000977 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +0000978 return;
979 }
980 }
981 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400982 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500983 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400984 }
985 FILE *fp = fopen(Path.c_str(), "w");
986 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000987 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400988 return;
989 }
990 strcpy(version, TW_VERSION_STR);
991 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
992 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +0000993 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
994 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400995 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400997}
998
Dees_Troy51a0e822012-09-05 15:24:24 -0400999void DataManager::ReadSettingsFile(void)
1000{
Ethan Yonker83e82572014-04-04 10:59:28 -05001001#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001002 // Load up the values for TWRP - Sleep to let the card be ready
1003 char mkdir_path[255], settings_file[255];
1004 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1005
1006 GetValue(TW_IS_ENCRYPTED, is_enc);
1007 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1008 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001009 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 return;
1011 }
1012
1013 memset(mkdir_path, 0, sizeof(mkdir_path));
1014 memset(settings_file, 0, sizeof(settings_file));
1015 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1016 sprintf(settings_file, "%s/.twrps", mkdir_path);
1017
Dees_Troy5bf43922012-09-07 16:07:55 -04001018 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001019 {
1020 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001021 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001023 }
1024
1025 mkdir(mkdir_path, 0777);
1026
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001028 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001029 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001030#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001031 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001032 update_tz_environment_variables();
xNUTxe85f02d2014-07-18 01:30:58 +02001033#ifdef TW_MAX_BRIGHTNESS
1034 if (GetStrValue("tw_brightness_path") != "/nobrightness") {
1035 TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
Dees_Troy2f9117a2013-02-17 19:52:09 -06001036 }
xNUTxe85f02d2014-07-18 01:30:58 +02001037#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001038}
1039
1040string DataManager::GetCurrentStoragePath(void)
1041{
Dees_Troya13d74f2013-03-24 08:54:55 -05001042 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
1045string& DataManager::CGetCurrentStoragePath()
1046{
Dees_Troya13d74f2013-03-24 08:54:55 -05001047 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001048}
1049
1050string DataManager::GetSettingsStoragePath(void)
1051{
Dees_Troya13d74f2013-03-24 08:54:55 -05001052 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001053}
1054
1055string& DataManager::CGetSettingsStoragePath()
1056{
Dees_Troya13d74f2013-03-24 08:54:55 -05001057 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001058}
1059
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001060extern "C" int DataManager_ResetDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001061{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001062 return DataManager::ResetDefaults();
Dees_Troy51a0e822012-09-05 15:24:24 -04001063}
1064
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065extern "C" void DataManager_LoadDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001066{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001067 return DataManager::SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001068}
1069
1070extern "C" int DataManager_LoadValues(const char* filename)
1071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001072 return DataManager::LoadValues(filename);
Dees_Troy51a0e822012-09-05 15:24:24 -04001073}
1074
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001075extern "C" int DataManager_Flush(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001076{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001077 return DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001078}
1079
1080extern "C" int DataManager_GetValue(const char* varName, char* value)
1081{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001082 int ret;
1083 string str;
Dees_Troy51a0e822012-09-05 15:24:24 -04001084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001085 ret = DataManager::GetValue(varName, str);
1086 if (ret == 0)
1087 strcpy(value, str.c_str());
1088 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001089}
1090
1091extern "C" const char* DataManager_GetStrValue(const char* varName)
1092{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001093 string& str = DataManager::GetValueRef(varName);
1094 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001095}
1096
1097extern "C" const char* DataManager_GetCurrentStoragePath(void)
1098{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001099 string& str = DataManager::CGetCurrentStoragePath();
1100 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001101}
1102
1103extern "C" const char* DataManager_GetSettingsStoragePath(void)
1104{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001105 string& str = DataManager::CGetSettingsStoragePath();
1106 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001107}
1108
Dees_Troy51a0e822012-09-05 15:24:24 -04001109extern "C" int DataManager_GetIntValue(const char* varName)
1110{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001111 return DataManager::GetIntValue(varName);
Dees_Troy51a0e822012-09-05 15:24:24 -04001112}
1113
1114extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1115{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001116 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001117}
1118
1119extern "C" int DataManager_SetIntValue(const char* varName, int value)
1120{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001121 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001122}
1123
1124extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1125{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001126 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001127}
1128
1129extern "C" int DataManager_ToggleIntValue(const char* varName)
1130{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001131 if (DataManager::GetIntValue(varName))
1132 return DataManager::SetValue(varName, 0);
1133 else
1134 return DataManager::SetValue(varName, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001135}
1136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001137extern "C" void DataManager_DumpValues(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001138{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001139 return DataManager::DumpValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001140}
1141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001142extern "C" void DataManager_ReadSettingsFile(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001143{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001144 return DataManager::ReadSettingsFile();
Dees_Troy51a0e822012-09-05 15:24:24 -04001145}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001146void DataManager::Vibrate(const string varName)
1147{
1148 int vib_value = 0;
1149 GetValue(varName, vib_value);
1150 if (vib_value) {
1151 vibrate(vib_value);
1152 }
1153}