blob: 94acf188c53f078432f631fbecf3160a9bf12844 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 Copyright 2013 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 <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040035
36#include <string>
37#include <sstream>
38#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040040#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "../adb_install.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070043#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050044#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040046extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000047#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040052#include "../minadbd/adb.h"
53
Dees_Troy32c8eb82012-09-11 15:28:06 -040054int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040055void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
Dees_Troy51a0e822012-09-05 15:24:24 -040056int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040057int gui_start();
58};
59
60#include "rapidxml.hpp"
61#include "objects.hpp"
62
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070063#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050064extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070065#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040066
Dees_Troy51a0e822012-09-05 15:24:24 -040067void curtainClose(void);
68
69GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010070 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 xml_node<>* child;
73 xml_node<>* actions;
74 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 // First, get the action
79 actions = node->first_node("actions");
80 if (actions) child = actions->first_node("action");
81 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 while (child)
86 {
87 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040088
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 attr = child->first_attribute("function");
90 if (!attr) return;
91
92 action.mFunction = attr->value();
93 action.mArg = child->value();
94 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 child = child->next_sibling("action");
97 }
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 // Now, let's get either the key or region
100 child = node->first_node("touch");
101 if (child)
102 {
103 attr = child->first_attribute("key");
104 if (attr)
105 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100106 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
107 for(size_t i = 0; i < keys.size(); ++i)
108 {
109 const int key = getKeyByName(keys[i]);
110 mKeys[key] = false;
111 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 }
113 else
114 {
115 attr = child->first_attribute("x");
116 if (!attr) return;
117 mActionX = atol(attr->value());
118 attr = child->first_attribute("y");
119 if (!attr) return;
120 mActionY = atol(attr->value());
121 attr = child->first_attribute("w");
122 if (!attr) return;
123 mActionW = atol(attr->value());
124 attr = child->first_attribute("h");
125 if (!attr) return;
126 mActionH = atol(attr->value());
127 }
128 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400129}
130
131int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
132{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 if (state == TOUCH_RELEASE)
134 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100139int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400140{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100141 if (mKeys.empty())
142 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100144 std::map<int, bool>::iterator itr = mKeys.find(key);
145 if(itr == mKeys.end())
146 return 0;
147
148 bool prevState = itr->second;
149 itr->second = down;
150
151 // If there is only one key for this action, wait for key up so it
152 // doesn't trigger with multi-key actions.
153 // Else, check if all buttons are pressed, then consume their release events
154 // so they don't trigger one-button actions and reset mKeys pressed status
155 if(mKeys.size() == 1) {
156 if(!down && prevState)
157 doActions();
158 } else if(down) {
159 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
160 if(!itr->second)
161 return 0;
162 }
163
164 // Passed, all req buttons are pressed, reset them and consume release events
165 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
166 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
167 kb->ConsumeKeyRelease(itr->first);
168 itr->second = false;
169 }
170
171 doActions();
172 }
173
Dees_Troy51a0e822012-09-05 15:24:24 -0400174 return 0;
175}
176
Vojtech Bocek07220562014-02-08 02:05:33 +0100177int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400178{
Vojtech Bocek07220562014-02-08 02:05:33 +0100179 GUIObject::NotifyVarChange(varName, value);
180
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100181 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100183 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187}
188
189void GUIAction::simulate_progress_bar(void)
190{
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 for (int i = 0; i < 5; i++)
193 {
194 usleep(500000);
195 DataManager::SetValue("ui_progress", i * 20);
196 }
197}
198
Dees_Troy657c3092012-09-10 20:32:10 -0400199int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400200{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
203 DataManager::SetValue("ui_progress", 0);
204
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 if (filename.empty())
206 {
207 LOGERR("No file specified.\n");
208 return -1;
209 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200211 // We're going to jump to this page first, like a loading page
212 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400213
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 int fd = -1;
215 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400218 return -1;
219
220 if (mzOpenZipArchive(filename.c_str(), &zip))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 {
222 LOGERR("Unable to open zip file.\n");
223 return -1;
224 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400225
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 // Check the zip to see if it has a custom installer theme
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 if (twrp != NULL)
229 {
230 unlink("/tmp/twrp.zip");
231 fd = creat("/tmp/twrp.zip", 0666);
232 }
233 if (fd >= 0 && twrp != NULL &&
234 mzExtractZipEntryToFile(&zip, twrp, fd) &&
235 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
236 {
237 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400238 PageManager::SelectPackage("install");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 gui_changePage("main");
240 }
241 else
242 {
243 // In this case, we just use the default page
244 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400245 gui_changePage(pageName);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 }
247 if (fd >= 0)
248 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
250 if (simulate) {
251 simulate_progress_bar();
252 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400253 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
255 // Now, check if we need to ensure TWRP remains installed...
256 struct stat st;
257 if (stat("/sbin/installTwrp", &st) == 0)
258 {
259 DataManager::SetValue("tw_operation", "Configuring TWRP");
260 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200262 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400263 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400265 }
266 }
267 }
268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 // Done
270 DataManager::SetValue("ui_progress", 100);
271 DataManager::SetValue("ui_progress", 0);
272 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273}
274
275int GUIAction::doActions()
276{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 if (mActions.size() < 1) return -1;
278 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400279 return doAction(mActions.at(0), 0);
280
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 // For multi-action, we always use a thread
282 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000283 pthread_attr_t tattr;
284
285 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000287 return -1;
288 }
289 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000291 return -1;
292 }
293 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000294 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000295 return -1;
296 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500297 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000298 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000299 return -1;
300 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500301 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000302 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000305 thread_start(this);
306 } else {
307 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000308 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000309 }
310 }
311 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000312 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000313 return -1;
314 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400315
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319void* GUIAction::thread_start(void *cookie)
320{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200321 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400322
323 DataManager::SetValue(TW_ACTION_BUSY, 1);
324
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 if (ourThis->mActions.size() > 1)
326 {
327 std::vector<Action>::iterator iter;
328 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
329 ourThis->doAction(*iter, 1);
330 }
331 else
332 {
333 ourThis->doAction(ourThis->mActions.at(0), 1);
334 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400335 int check = 0;
336 DataManager::GetValue("tw_background_thread_running", check);
337 if (check == 0)
338 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342void GUIAction::operation_start(const string operation_name)
343{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000344 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 DataManager::SetValue(TW_ACTION_BUSY, 1);
346 DataManager::SetValue("ui_progress", 0);
347 DataManager::SetValue("tw_operation", operation_name);
348 DataManager::SetValue("tw_operation_status", 0);
349 DataManager::SetValue("tw_operation_state", 0);
350}
351
352void GUIAction::operation_end(const int operation_status, const int simulate)
353{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000354 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 DataManager::SetValue("ui_progress", 100);
357 if (simulate) {
358 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
359 if (simulate_fail != 0)
360 DataManager::SetValue("tw_operation_status", 1);
361 else
362 DataManager::SetValue("tw_operation_status", 0);
363 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500364 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400365 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500366 }
367 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400368 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370 }
371 DataManager::SetValue("tw_operation_state", 1);
372 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700373#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500374 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700375#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000376 time(&Stop);
377 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600378 DataManager::Vibrate("tw_action_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400379}
380
381int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
382{
383 static string zip_queue[10];
384 static int zip_queue_index;
385 static pthread_t terminal_command;
386 int simulate;
387
388 std::string arg = gui_parse_text(action.mArg);
389
390 std::string function = gui_parse_text(action.mFunction);
391
392 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
393
Dees_Troy6ef66352013-02-21 08:26:57 -0600394 if (function == "reboot")
395 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200396 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
Dees_Troy6ef66352013-02-21 08:26:57 -0600398 sync();
399 DataManager::SetValue("tw_gui_done", 1);
400 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400401
Dees_Troy6ef66352013-02-21 08:26:57 -0600402 return 0;
403 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 if (function == "home")
405 {
406 PageManager::SelectPackage("TWRP");
407 gui_changePage("main");
408 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 }
410
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200411 if (function == "key")
412 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100413 const int key = getKeyByName(arg);
414 PageManager::NotifyKey(key, true);
415 PageManager::NotifyKey(key, false);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 return 0;
417 }
418
419 if (function == "page") {
420 std::string page_name = gui_parse_text(arg);
421 return gui_changePage(page_name);
422 }
423
424 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 int check = 0, ret_val = 0;
426 std::string theme_path;
427
428 operation_start("Reload Theme");
429 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400432 check = 1;
433 }
434
435 theme_path += "/TWRP/theme/ui.zip";
436 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
437 {
438 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000439 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
441 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 ret_val = 1;
444 }
445 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200446 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000447 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 }
449
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200450 if (function == "readBackup")
451 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 string Restore_Name;
453 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200455 return 0;
456 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200458 if (function == "set")
459 {
460 if (arg.find('=') != string::npos)
461 {
462 string varName = arg.substr(0, arg.find('='));
463 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 DataManager::GetValue(value, value);
466 DataManager::SetValue(varName, value);
467 }
468 else
469 DataManager::SetValue(arg, "1");
470 return 0;
471 }
472 if (function == "clear")
473 {
474 DataManager::SetValue(arg, "0");
475 return 0;
476 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400477
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600478 if (function == "mount") {
479 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400482 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000484 gui_print("Simulating actions...\n");
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600485 } else if (!simulate) {
486 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000488 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200489 return 0;
490 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400491
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600492 if (function == "umount" || function == "unmount") {
493 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200494 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400495 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000497 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600499 } else if (!simulate) {
500 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200501 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000502 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200503 return 0;
504 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400505
506 if (function == "restoredefaultsettings")
507 {
508 operation_start("Restore Defaults");
509 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 else {
512 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400513 PartitionManager.Update_System_Details();
514 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 }
516 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000517 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 }
519
520 if (function == "copylog")
521 {
522 operation_start("Copy Log");
523 if (!simulate)
524 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500525 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400526 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500527 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
528 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 } else
532 simulate_progress_bar();
533 operation_end(0, simulate);
534 return 0;
535 }
536
537 if (function == "compute" || function == "addsubtract")
538 {
539 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 {
541 string varName = arg.substr(0, arg.find('+'));
542 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400543 int amount_to_add = atoi(string_to_add.c_str());
544 int value;
545
546 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200547 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400548 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200549 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400550 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200551 {
552 string varName = arg.substr(0, arg.find('-'));
553 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400554 int amount_to_subtract = atoi(string_to_subtract.c_str());
555 int value;
556
557 DataManager::GetValue(varName, value);
558 value -= amount_to_subtract;
559 if (value <= 0)
560 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200561 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400562 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200563 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200564 if (arg.find("*") != string::npos)
565 {
566 string varName = arg.substr(0, arg.find('*'));
567 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
568 int multiply_by = atoi(multiply_by_str.c_str());
569 int value;
570
571 DataManager::GetValue(varName, value);
572 DataManager::SetValue(varName, value*multiply_by);
573 return 0;
574 }
575 if (arg.find("/") != string::npos)
576 {
577 string varName = arg.substr(0, arg.find('/'));
578 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
579 int divide_by = atoi(divide_by_str.c_str());
580 int value;
581
582 if(divide_by != 0)
583 {
584 DataManager::GetValue(varName, value);
585 DataManager::SetValue(varName, value/divide_by);
586 }
587 return 0;
588 }
589 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
590 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400591 }
592
593 if (function == "setguitimezone")
594 {
595 string SelectedZone;
596 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
597 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
598 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
599
600 int dst;
601 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
602
603 string offset;
604 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
605
606 string NewTimeZone = Zone;
607 if (offset != "0")
608 NewTimeZone += ":" + offset;
609
610 if (dst != 0)
611 NewTimeZone += DSTZone;
612
613 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400614 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400615 return 0;
616 }
617
618 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000619 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400620 return 0;
621 }
622
623 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200624 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400625
626 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200627 {
628 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000629 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400630 return 0;
631 }
632 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
633 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
634 zip_queue_index++;
635 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
636 }
637 return 0;
638 }
639
640 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200641 {
642 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000643 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 return 0;
645 } else {
646 zip_queue_index--;
647 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
648 }
649 return 0;
650 }
651
652 if (function == "queueclear")
653 {
654 zip_queue_index = 0;
655 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
656 return 0;
657 }
658
659 if (function == "sleep")
660 {
661 operation_start("Sleep");
662 usleep(atoi(arg.c_str()));
663 operation_end(0, simulate);
664 return 0;
665 }
666
Dees Troyb21cc642013-09-10 17:36:41 +0000667 if (function == "appenddatetobackupname")
668 {
669 operation_start("AppendDateToBackupName");
670 string Backup_Name;
671 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
672 Backup_Name += TWFunc::Get_Current_Date();
673 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
674 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
675 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
676 operation_end(0, simulate);
677 return 0;
678 }
679
680 if (function == "generatebackupname")
681 {
682 operation_start("GenerateBackupName");
683 TWFunc::Auto_Generate_Backup_Name();
684 operation_end(0, simulate);
685 return 0;
686 }
687
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200688 if (isThreaded)
689 {
690 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400691 {
692 struct stat st;
693 string newpath = arg + "/.";
694
695 operation_start("FileExists");
696 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
697 operation_end(0, simulate);
698 else
699 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000700 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 }
702
703 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200704 {
Dees_Troy657c3092012-09-10 20:32:10 -0400705 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400706
707 for (i=0; i<zip_queue_index; i++) {
708 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200709 DataManager::SetValue("tw_filename", zip_queue[i]);
710 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400711
Dees_Troy657c3092012-09-10 20:32:10 -0400712 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000714 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 i = 10; // Error flashing zip - exit queue
716 ret_val = 1;
717 }
718 }
719 zip_queue_index = 0;
720 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
721
Dees_Troy657c3092012-09-10 20:32:10 -0400722 if (wipe_cache)
723 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200724
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
726 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000727 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 if (simulate) {
729 simulate_progress_bar();
730 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400731 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
732 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200733 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400734 else {
735 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200736 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400737 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 }
740 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400741 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200743 return 0;
744 }
745 if (function == "wipe")
746 {
747 operation_start("Format");
748 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400749
Dees_Troy38bd7602012-09-14 13:33:53 -0400750 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400751
752 if (simulate) {
753 simulate_progress_bar();
754 } else {
755 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400756 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400757 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400759 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400760 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400761 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400762 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400764 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400765 } else if (arg == "INTERNAL") {
766 int has_datamedia, dual_storage;
767
768 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
769 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400770 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400771 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400772 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 }
774 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400776
Dees_Troy38bd7602012-09-14 13:33:53 -0400777 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
778 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400779 } else if (arg == "ANDROIDSECURE") {
780 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500781 } else if (arg == "LIST") {
782 string Wipe_List, wipe_path;
783 bool skip = false;
784 ret_val = true;
785 TWPartition* wipe_part = NULL;
786
787 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500789 if (!Wipe_List.empty()) {
790 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
791 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
792 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500794 if (wipe_path == "/and-sec") {
795 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000796 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500797 ret_val = false;
798 break;
799 } else {
800 skip = true;
801 }
802 } else if (wipe_path == "DALVIK") {
803 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000804 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500805 ret_val = false;
806 break;
807 } else {
808 skip = true;
809 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000810 } else if (wipe_path == "INTERNAL") {
811 if (!PartitionManager.Wipe_Media_From_Data()) {
812 ret_val = false;
813 break;
814 } else {
815 skip = true;
816 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500817 }
818 if (!skip) {
819 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500821 ret_val = false;
822 break;
823 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
824 arg = wipe_path;
825 }
826 } else {
827 skip = false;
828 }
829 start_pos = end_pos + 1;
830 end_pos = Wipe_List.find(";", start_pos);
831 }
832 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400833 } else
834 ret_val = PartitionManager.Wipe_By_Path(arg);
Ethan Yonker83e82572014-04-04 10:59:28 -0500835#ifdef TW_OEM_BUILD
Dees_Troy38bd7602012-09-14 13:33:53 -0400836 if (arg == DataManager::GetSettingsStoragePath()) {
837 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
838 string Storage_Path = DataManager::GetSettingsStoragePath();
839
840 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400842 Storage_Path += "/TWRP";
843 mkdir(Storage_Path.c_str(), 0777);
844 DataManager::Flush();
845 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000846 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400847 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400848 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500849#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400850 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400851 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400852 if (ret_val)
853 ret_val = 0; // 0 is success
854 else
855 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200856 operation_end(ret_val, simulate);
857 return 0;
858 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400859 if (function == "refreshsizes")
860 {
861 operation_start("Refreshing Sizes");
862 if (simulate) {
863 simulate_progress_bar();
864 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400865 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000867 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400868 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200869 if (function == "nandroid")
870 {
871 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400872 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400873
874 if (simulate) {
875 DataManager::SetValue("tw_partition", "Simulation");
876 simulate_progress_bar();
877 } else {
878 if (arg == "backup") {
879 string Backup_Name;
880 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000881 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400882 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500883 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400884 else {
885 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400886 return -1;
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000887
Dees_Troy43d8b002012-09-17 16:00:01 -0400888 }
Dees Troyb21cc642013-09-10 17:36:41 +0000889 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -0400890 } else if (arg == "restore") {
891 string Restore_Name;
892 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400893 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400894 } else {
895 operation_end(1, simulate);
896 return -1;
897 }
898 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000899 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400900 if (ret == false)
901 ret = 1; // 1 for failure
902 else
903 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200904 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000905 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200906 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400907 if (function == "fixpermissions")
908 {
909 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200910 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400911 if (simulate) {
912 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400913 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400914 int op_status = PartitionManager.Fix_Permissions();
915 if (op_status != 0)
916 op_status = 1; // failure
917 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400918 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400919 return 0;
920 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200921 if (function == "dd")
922 {
923 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -0400924
925 if (simulate) {
926 simulate_progress_bar();
927 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500928 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +0200929 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400930 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200931 operation_end(0, simulate);
932 return 0;
933 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400934 if (function == "partitionsd")
935 {
936 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400937 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400938
939 if (simulate) {
940 simulate_progress_bar();
941 } else {
942 int allow_partition;
943 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
944 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000945 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400946 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400947 if (!PartitionManager.Partition_SDCard())
948 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400949 }
950 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400951 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400952 return 0;
953 }
954 if (function == "installhtcdumlock")
955 {
956 operation_start("Install HTC Dumlock");
957 if (simulate) {
958 simulate_progress_bar();
959 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400960 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400961
962 operation_end(0, simulate);
963 return 0;
964 }
965 if (function == "htcdumlockrestoreboot")
966 {
967 operation_start("HTC Dumlock Restore Boot");
968 if (simulate) {
969 simulate_progress_bar();
970 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400971 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400972
973 operation_end(0, simulate);
974 return 0;
975 }
976 if (function == "htcdumlockreflashrecovery")
977 {
978 operation_start("HTC Dumlock Reflash Recovery");
979 if (simulate) {
980 simulate_progress_bar();
981 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400982 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400983
984 operation_end(0, simulate);
985 return 0;
986 }
987 if (function == "cmd")
988 {
989 int op_status = 0;
990
991 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +0000992 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400993 if (simulate) {
994 simulate_progress_bar();
995 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +0200996 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400997 if (op_status != 0)
998 op_status = 1;
999 }
1000
1001 operation_end(op_status, simulate);
1002 return 0;
1003 }
1004 if (function == "terminalcommand")
1005 {
1006 int op_status = 0;
1007 string cmdpath, command;
1008
1009 DataManager::GetValue("tw_terminal_location", cmdpath);
1010 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001011 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001012 if (simulate) {
1013 simulate_progress_bar();
1014 operation_end(op_status, simulate);
1015 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001016 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001018 DataManager::SetValue("tw_terminal_command_thread", command);
1019 DataManager::SetValue("tw_terminal_state", 1);
1020 DataManager::SetValue("tw_background_thread_running", 1);
1021 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1022 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001024 DataManager::SetValue("tw_terminal_state", 0);
1025 DataManager::SetValue("tw_background_thread_running", 0);
1026 operation_end(1, simulate);
1027 }
1028 }
1029 return 0;
1030 }
1031 if (function == "killterminal")
1032 {
1033 int op_status = 0;
1034
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001036 operation_start("KillCommand");
1037 DataManager::SetValue("tw_operation_status", 0);
1038 DataManager::SetValue("tw_operation_state", 1);
1039 DataManager::SetValue("tw_terminal_state", 0);
1040 DataManager::SetValue("tw_background_thread_running", 0);
1041 DataManager::SetValue(TW_ACTION_BUSY, 0);
1042 return 0;
1043 }
1044 if (function == "reinjecttwrp")
1045 {
1046 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001047 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001048 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001049 if (simulate) {
1050 simulate_progress_bar();
1051 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001052 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001053 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001054 }
1055
1056 operation_end(op_status, simulate);
1057 return 0;
1058 }
1059 if (function == "checkbackupname")
1060 {
1061 int op_status = 0;
1062
1063 operation_start("CheckBackupName");
1064 if (simulate) {
1065 simulate_progress_bar();
1066 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001067 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001068 if (op_status != 0)
1069 op_status = 1;
1070 }
1071
1072 operation_end(op_status, simulate);
1073 return 0;
1074 }
1075 if (function == "decrypt")
1076 {
1077 int op_status = 0;
1078
1079 operation_start("Decrypt");
1080 if (simulate) {
1081 simulate_progress_bar();
1082 } else {
1083 string Password;
1084 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001085 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001086 if (op_status != 0)
1087 op_status = 1;
1088 else {
1089 int load_theme = 1;
1090
1091 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001092
1093 if (load_theme) {
1094 int has_datamedia;
1095
1096 // Check for a custom theme and load it if exists
1097 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1098 if (has_datamedia != 0) {
1099 struct stat st;
1100 int check = 0;
1101 std::string theme_path;
1102
1103 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001104 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001105 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001106 check = 1;
1107 }
1108
1109 theme_path += "/TWRP/theme/ui.zip";
1110 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1111 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1112 {
1113 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001114 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1116 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001117 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001118 }
1119 }
1120 }
1121 }
1122 }
1123 }
1124 }
1125
1126 operation_end(op_status, simulate);
1127 return 0;
1128 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001129 if (function == "adbsideload")
1130 {
1131 int ret = 0;
1132
1133 operation_start("Sideload");
1134 if (simulate) {
1135 simulate_progress_bar();
1136 } else {
1137 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001138 int wipe_dalvik = 0;
Vojtech Bocek05534202013-09-11 08:11:56 +02001139 string Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001140
Ethan Yonker45312e52014-02-26 09:23:53 -06001141 if (!PartitionManager.Mount_Current_Storage(false)) {
1142 gui_print("Using RAM for sideload storage.\n");
1143 Sideload_File = "/tmp/sideload.zip";
1144 } else {
1145 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
Dees_Troycfb63ae2012-09-19 14:30:17 -04001146 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001147 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001148 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001149 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001151 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001152 ret = apply_from_adb(Sideload_File.c_str());
1153 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001154 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001155 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001156 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001157 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1158 PartitionManager.Wipe_By_Path("/cache");
1159 if (wipe_dalvik)
1160 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 } else {
1162 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001163 }
Dees Troy8c0f06b2013-11-26 21:21:29 +00001164 PartitionManager.Update_System_Details();
Dees_Troy06b4fe92012-10-16 11:43:20 -04001165 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1166 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001167 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001168 if (simulate) {
1169 simulate_progress_bar();
1170 } else {
1171 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1172 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001173 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001174 else {
1175 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001176 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001177 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001178 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001179 }
1180 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001181 }
1182 operation_end(ret, simulate);
1183 return 0;
1184 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001185 if (function == "adbsideloadcancel")
1186 {
1187 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001189 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001190 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001191 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001192 property_get("tw_child_pid", child_prop, "error");
1193 if (strcmp(child_prop, "error") == 0) {
1194 LOGERR("Unable to get child ID from prop\n");
1195 return 0;
1196 }
1197 child_pid = atoi(child_prop);
1198 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001199 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001200 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001201 return 0;
1202 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001203 if (function == "openrecoveryscript") {
1204 operation_start("OpenRecoveryScript");
1205 if (simulate) {
1206 simulate_progress_bar();
1207 } else {
1208 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1209 // that we converted to ORS commands during boot in recovery.cpp.
1210 // Run those first.
1211 int reboot = 0;
1212 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001213 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001214 if (OpenRecoveryScript::run_script_file() == 0) {
1215 reboot = 1;
1216 }
1217 }
1218 // Check for the ORS file in /cache and attempt to run those commands.
1219 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001221 if (OpenRecoveryScript::run_script_file() == 0) {
1222 reboot = 1;
1223 }
1224 }
1225 if (reboot) {
1226 usleep(2000000); // Sleep for 2 seconds before rebooting
1227 TWFunc::tw_reboot(rb_system);
1228 } else {
1229 DataManager::SetValue("tw_page_done", 1);
1230 }
1231 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001232 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001233 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001234 if (function == "installsu")
1235 {
1236 int op_status = 0;
1237
1238 operation_start("Install SuperSU");
1239 if (simulate) {
1240 simulate_progress_bar();
1241 } else {
1242 if (!TWFunc::Install_SuperSU())
1243 op_status = 1;
1244 }
1245
1246 operation_end(op_status, simulate);
1247 return 0;
1248 }
1249 if (function == "fixsu")
1250 {
1251 int op_status = 0;
1252
1253 operation_start("Fixing Superuser Permissions");
1254 if (simulate) {
1255 simulate_progress_bar();
1256 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001257 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1258 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001259 }
1260
1261 operation_end(op_status, simulate);
1262 return 0;
1263 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001264 if (function == "decrypt_backup")
1265 {
1266 int op_status = 0;
1267
1268 operation_start("Try Restore Decrypt");
1269 if (simulate) {
1270 simulate_progress_bar();
1271 } else {
1272 string Restore_Path, Filename, Password;
1273 DataManager::GetValue("tw_restore", Restore_Path);
1274 Restore_Path += "/";
1275 DataManager::GetValue("tw_restore_password", Password);
1276 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1277 op_status = 0; // success
1278 else
1279 op_status = 1; // fail
1280 }
1281
1282 operation_end(op_status, simulate);
1283 return 0;
1284 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001285 }
1286 else
1287 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001288 pthread_t t;
1289 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001290 return 0;
1291 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001292 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001293 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001294}
1295
1296int GUIAction::getKeyByName(std::string key)
1297{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001298 if (key == "home") return KEY_HOME;
1299 else if (key == "menu") return KEY_MENU;
1300 else if (key == "back") return KEY_BACK;
1301 else if (key == "search") return KEY_SEARCH;
1302 else if (key == "voldown") return KEY_VOLUMEDOWN;
1303 else if (key == "volup") return KEY_VOLUMEUP;
1304 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001305 int ret_val;
1306 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1307 if (!ret_val)
1308 return KEY_POWER;
1309 else
1310 return ret_val;
1311 }
1312
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001313 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001314}
1315
1316void* GUIAction::command_thread(void *cookie)
1317{
1318 string command;
1319 FILE* fp;
1320 char line[512];
1321
1322 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001323 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001324 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001326 } else {
1327 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1328 struct timeval timeout;
1329 fd_set fdset;
1330
1331 while(keep_going)
1332 {
1333 FD_ZERO(&fdset);
1334 FD_SET(fd, &fdset);
1335 timeout.tv_sec = 0;
1336 timeout.tv_usec = 400000;
1337 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1338 if (has_data == 0) {
1339 // Timeout reached
1340 DataManager::GetValue("tw_terminal_state", check);
1341 if (check == 0) {
1342 keep_going = 0;
1343 }
1344 } else if (has_data < 0) {
1345 // End of execution
1346 keep_going = 0;
1347 } else {
1348 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001349 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001350 bytes_read = read(fd, line, sizeof(line));
1351 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001352 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001353 else
1354 keep_going = 0; // Done executing
1355 }
1356 }
1357 fclose(fp);
1358 }
1359 DataManager::SetValue("tw_operation_status", 0);
1360 DataManager::SetValue("tw_operation_state", 1);
1361 DataManager::SetValue("tw_terminal_state", 0);
1362 DataManager::SetValue("tw_background_thread_running", 0);
1363 DataManager::SetValue(TW_ACTION_BUSY, 0);
1364 return NULL;
1365}