blob: 9d7e482cae99988a69874695b5c403f3bae189cc [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 mKey = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 // First, get the action
81 actions = node->first_node("actions");
82 if (actions) child = actions->first_node("action");
83 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 while (child)
88 {
89 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 attr = child->first_attribute("function");
92 if (!attr) return;
93
94 action.mFunction = attr->value();
95 action.mArg = child->value();
96 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 child = child->next_sibling("action");
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // Now, let's get either the key or region
102 child = node->first_node("touch");
103 if (child)
104 {
105 attr = child->first_attribute("key");
106 if (attr)
107 {
108 std::string key = attr->value();
109
110 mKey = getKeyByName(key);
111 }
112 else
113 {
114 attr = child->first_attribute("x");
115 if (!attr) return;
116 mActionX = atol(attr->value());
117 attr = child->first_attribute("y");
118 if (!attr) return;
119 mActionY = atol(attr->value());
120 attr = child->first_attribute("w");
121 if (!attr) return;
122 mActionW = atol(attr->value());
123 attr = child->first_attribute("h");
124 if (!attr) return;
125 mActionH = atol(attr->value());
126 }
127 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
131{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (state == TOUCH_RELEASE)
133 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136}
137
138int GUIAction::NotifyKey(int key)
139{
140 if (!mKey || key != mKey)
141 return 1;
142
143 doActions();
144 return 0;
145}
146
Vojtech Bocek07220562014-02-08 02:05:33 +0100147int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400148{
Vojtech Bocek07220562014-02-08 02:05:33 +0100149 GUIObject::NotifyVarChange(varName, value);
150
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
152 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100153 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157}
158
159void GUIAction::simulate_progress_bar(void)
160{
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400162 for (int i = 0; i < 5; i++)
163 {
164 usleep(500000);
165 DataManager::SetValue("ui_progress", i * 20);
166 }
167}
168
Dees_Troy657c3092012-09-10 20:32:10 -0400169int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400170{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172
173 DataManager::SetValue("ui_progress", 0);
174
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 if (filename.empty())
176 {
177 LOGERR("No file specified.\n");
178 return -1;
179 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400180
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181 // We're going to jump to this page first, like a loading page
182 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 int fd = -1;
185 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400186
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400188 return -1;
189
190 if (mzOpenZipArchive(filename.c_str(), &zip))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 {
192 LOGERR("Unable to open zip file.\n");
193 return -1;
194 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400195
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200196 // Check the zip to see if it has a custom installer theme
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200198 if (twrp != NULL)
199 {
200 unlink("/tmp/twrp.zip");
201 fd = creat("/tmp/twrp.zip", 0666);
202 }
203 if (fd >= 0 && twrp != NULL &&
204 mzExtractZipEntryToFile(&zip, twrp, fd) &&
205 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
206 {
207 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400208 PageManager::SelectPackage("install");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 gui_changePage("main");
210 }
211 else
212 {
213 // In this case, we just use the default page
214 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400215 gui_changePage(pageName);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 }
217 if (fd >= 0)
218 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
220 if (simulate) {
221 simulate_progress_bar();
222 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400223 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400224
225 // Now, check if we need to ensure TWRP remains installed...
226 struct stat st;
227 if (stat("/sbin/installTwrp", &st) == 0)
228 {
229 DataManager::SetValue("tw_operation", "Configuring TWRP");
230 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200232 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400233 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400235 }
236 }
237 }
238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 // Done
240 DataManager::SetValue("ui_progress", 100);
241 DataManager::SetValue("ui_progress", 0);
242 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243}
244
245int GUIAction::doActions()
246{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 if (mActions.size() < 1) return -1;
248 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 return doAction(mActions.at(0), 0);
250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 // For multi-action, we always use a thread
252 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000253 pthread_attr_t tattr;
254
255 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000257 return -1;
258 }
259 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000261 return -1;
262 }
263 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000265 return -1;
266 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500267 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000268 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000269 return -1;
270 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500271 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000272 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000275 thread_start(this);
276 } else {
277 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000278 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000279 }
280 }
281 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000283 return -1;
284 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400285
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400287}
288
289void* GUIAction::thread_start(void *cookie)
290{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292
293 DataManager::SetValue(TW_ACTION_BUSY, 1);
294
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (ourThis->mActions.size() > 1)
296 {
297 std::vector<Action>::iterator iter;
298 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
299 ourThis->doAction(*iter, 1);
300 }
301 else
302 {
303 ourThis->doAction(ourThis->mActions.at(0), 1);
304 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400305 int check = 0;
306 DataManager::GetValue("tw_background_thread_running", check);
307 if (check == 0)
308 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400310}
311
312void GUIAction::operation_start(const string operation_name)
313{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000314 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400315 DataManager::SetValue(TW_ACTION_BUSY, 1);
316 DataManager::SetValue("ui_progress", 0);
317 DataManager::SetValue("tw_operation", operation_name);
318 DataManager::SetValue("tw_operation_status", 0);
319 DataManager::SetValue("tw_operation_state", 0);
320}
321
322void GUIAction::operation_end(const int operation_status, const int simulate)
323{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000324 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400325 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400326 DataManager::SetValue("ui_progress", 100);
327 if (simulate) {
328 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
329 if (simulate_fail != 0)
330 DataManager::SetValue("tw_operation_status", 1);
331 else
332 DataManager::SetValue("tw_operation_status", 0);
333 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400335 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500336 }
337 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400338 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500339 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400340 }
341 DataManager::SetValue("tw_operation_state", 1);
342 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700343#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500344 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700345#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000346 time(&Stop);
347 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600348 DataManager::Vibrate("tw_action_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
351int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
352{
353 static string zip_queue[10];
354 static int zip_queue_index;
355 static pthread_t terminal_command;
356 int simulate;
357
358 std::string arg = gui_parse_text(action.mArg);
359
360 std::string function = gui_parse_text(action.mFunction);
361
362 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
363
Dees_Troy6ef66352013-02-21 08:26:57 -0600364 if (function == "reboot")
365 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
Dees_Troy6ef66352013-02-21 08:26:57 -0600368 sync();
369 DataManager::SetValue("tw_gui_done", 1);
370 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Dees_Troy6ef66352013-02-21 08:26:57 -0600372 return 0;
373 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374 if (function == "home")
375 {
376 PageManager::SelectPackage("TWRP");
377 gui_changePage("main");
378 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 }
380
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 if (function == "key")
382 {
383 PageManager::NotifyKey(getKeyByName(arg));
384 return 0;
385 }
386
387 if (function == "page") {
388 std::string page_name = gui_parse_text(arg);
389 return gui_changePage(page_name);
390 }
391
392 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400393 int check = 0, ret_val = 0;
394 std::string theme_path;
395
396 operation_start("Reload Theme");
397 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400398 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000399 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 check = 1;
401 }
402
403 theme_path += "/TWRP/theme/ui.zip";
404 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
405 {
406 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000407 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
409 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000410 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 ret_val = 1;
412 }
413 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200414 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000415 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416 }
417
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 if (function == "readBackup")
419 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 string Restore_Name;
421 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400422 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 return 0;
424 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400425
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200426 if (function == "set")
427 {
428 if (arg.find('=') != string::npos)
429 {
430 string varName = arg.substr(0, arg.find('='));
431 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400432
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200433 DataManager::GetValue(value, value);
434 DataManager::SetValue(varName, value);
435 }
436 else
437 DataManager::SetValue(arg, "1");
438 return 0;
439 }
440 if (function == "clear")
441 {
442 DataManager::SetValue(arg, "0");
443 return 0;
444 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400445
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600446 if (function == "mount") {
447 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200448 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400450 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000452 gui_print("Simulating actions...\n");
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600453 } else if (!simulate) {
454 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200455 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000456 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200457 return 0;
458 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600460 if (function == "umount" || function == "unmount") {
461 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200462 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400463 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000465 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600467 } else if (!simulate) {
468 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200469 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000470 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200471 return 0;
472 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400473
474 if (function == "restoredefaultsettings")
475 {
476 operation_start("Restore Defaults");
477 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000478 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 else {
480 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 PartitionManager.Update_System_Details();
482 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 }
484 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000485 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 }
487
488 if (function == "copylog")
489 {
490 operation_start("Copy Log");
491 if (!simulate)
492 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500493 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400494 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500495 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
496 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000498 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 } else
500 simulate_progress_bar();
501 operation_end(0, simulate);
502 return 0;
503 }
504
505 if (function == "compute" || function == "addsubtract")
506 {
507 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508 {
509 string varName = arg.substr(0, arg.find('+'));
510 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 int amount_to_add = atoi(string_to_add.c_str());
512 int value;
513
514 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200515 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200517 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200519 {
520 string varName = arg.substr(0, arg.find('-'));
521 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522 int amount_to_subtract = atoi(string_to_subtract.c_str());
523 int value;
524
525 DataManager::GetValue(varName, value);
526 value -= amount_to_subtract;
527 if (value <= 0)
528 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200531 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200532 if (arg.find("*") != string::npos)
533 {
534 string varName = arg.substr(0, arg.find('*'));
535 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
536 int multiply_by = atoi(multiply_by_str.c_str());
537 int value;
538
539 DataManager::GetValue(varName, value);
540 DataManager::SetValue(varName, value*multiply_by);
541 return 0;
542 }
543 if (arg.find("/") != string::npos)
544 {
545 string varName = arg.substr(0, arg.find('/'));
546 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
547 int divide_by = atoi(divide_by_str.c_str());
548 int value;
549
550 if(divide_by != 0)
551 {
552 DataManager::GetValue(varName, value);
553 DataManager::SetValue(varName, value/divide_by);
554 }
555 return 0;
556 }
557 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
558 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400559 }
560
561 if (function == "setguitimezone")
562 {
563 string SelectedZone;
564 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
565 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
566 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
567
568 int dst;
569 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
570
571 string offset;
572 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
573
574 string NewTimeZone = Zone;
575 if (offset != "0")
576 NewTimeZone += ":" + offset;
577
578 if (dst != 0)
579 NewTimeZone += DSTZone;
580
581 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400582 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400583 return 0;
584 }
585
586 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000587 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400588 return 0;
589 }
590
591 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200592 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
594 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200595 {
596 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000597 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 return 0;
599 }
600 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
601 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
602 zip_queue_index++;
603 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
604 }
605 return 0;
606 }
607
608 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200609 {
610 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000611 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400612 return 0;
613 } else {
614 zip_queue_index--;
615 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
616 }
617 return 0;
618 }
619
620 if (function == "queueclear")
621 {
622 zip_queue_index = 0;
623 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
624 return 0;
625 }
626
627 if (function == "sleep")
628 {
629 operation_start("Sleep");
630 usleep(atoi(arg.c_str()));
631 operation_end(0, simulate);
632 return 0;
633 }
634
Dees Troyb21cc642013-09-10 17:36:41 +0000635 if (function == "appenddatetobackupname")
636 {
637 operation_start("AppendDateToBackupName");
638 string Backup_Name;
639 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
640 Backup_Name += TWFunc::Get_Current_Date();
641 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
642 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
643 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
644 operation_end(0, simulate);
645 return 0;
646 }
647
648 if (function == "generatebackupname")
649 {
650 operation_start("GenerateBackupName");
651 TWFunc::Auto_Generate_Backup_Name();
652 operation_end(0, simulate);
653 return 0;
654 }
655
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200656 if (isThreaded)
657 {
658 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 {
660 struct stat st;
661 string newpath = arg + "/.";
662
663 operation_start("FileExists");
664 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
665 operation_end(0, simulate);
666 else
667 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000668 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 }
670
671 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200672 {
Dees_Troy657c3092012-09-10 20:32:10 -0400673 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400674
675 for (i=0; i<zip_queue_index; i++) {
676 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200677 DataManager::SetValue("tw_filename", zip_queue[i]);
678 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400679
Dees_Troy657c3092012-09-10 20:32:10 -0400680 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000682 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400683 i = 10; // Error flashing zip - exit queue
684 ret_val = 1;
685 }
686 }
687 zip_queue_index = 0;
688 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
689
Dees_Troy657c3092012-09-10 20:32:10 -0400690 if (wipe_cache)
691 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200692
Dees_Troy51a0e822012-09-05 15:24:24 -0400693 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
694 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000695 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 if (simulate) {
697 simulate_progress_bar();
698 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400699 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
700 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200701 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400702 else {
703 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 +0200704 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400705 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000706 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 }
708 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400709 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200711 return 0;
712 }
713 if (function == "wipe")
714 {
715 operation_start("Format");
716 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400717
Dees_Troy38bd7602012-09-14 13:33:53 -0400718 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400719
720 if (simulate) {
721 simulate_progress_bar();
722 } else {
723 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400724 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400726 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400727 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400728 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400729 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400730 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400731 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400732 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 } else if (arg == "INTERNAL") {
734 int has_datamedia, dual_storage;
735
736 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
737 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400740 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400741 }
742 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400743 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400744
Dees_Troy38bd7602012-09-14 13:33:53 -0400745 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
746 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400747 } else if (arg == "ANDROIDSECURE") {
748 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500749 } else if (arg == "LIST") {
750 string Wipe_List, wipe_path;
751 bool skip = false;
752 ret_val = true;
753 TWPartition* wipe_part = NULL;
754
755 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000756 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500757 if (!Wipe_List.empty()) {
758 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
759 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
760 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500762 if (wipe_path == "/and-sec") {
763 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000764 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500765 ret_val = false;
766 break;
767 } else {
768 skip = true;
769 }
770 } else if (wipe_path == "DALVIK") {
771 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000772 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500773 ret_val = false;
774 break;
775 } else {
776 skip = true;
777 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000778 } else if (wipe_path == "INTERNAL") {
779 if (!PartitionManager.Wipe_Media_From_Data()) {
780 ret_val = false;
781 break;
782 } else {
783 skip = true;
784 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500785 }
786 if (!skip) {
787 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500789 ret_val = false;
790 break;
791 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
792 arg = wipe_path;
793 }
794 } else {
795 skip = false;
796 }
797 start_pos = end_pos + 1;
798 end_pos = Wipe_List.find(";", start_pos);
799 }
800 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400801 } else
802 ret_val = PartitionManager.Wipe_By_Path(arg);
Ethan Yonker83e82572014-04-04 10:59:28 -0500803#ifdef TW_OEM_BUILD
Dees_Troy38bd7602012-09-14 13:33:53 -0400804 if (arg == DataManager::GetSettingsStoragePath()) {
805 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
806 string Storage_Path = DataManager::GetSettingsStoragePath();
807
808 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000809 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400810 Storage_Path += "/TWRP";
811 mkdir(Storage_Path.c_str(), 0777);
812 DataManager::Flush();
813 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000814 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400815 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400816 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500817#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400818 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400819 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400820 if (ret_val)
821 ret_val = 0; // 0 is success
822 else
823 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 operation_end(ret_val, simulate);
825 return 0;
826 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400827 if (function == "refreshsizes")
828 {
829 operation_start("Refreshing Sizes");
830 if (simulate) {
831 simulate_progress_bar();
832 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400833 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400834 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000835 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400836 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200837 if (function == "nandroid")
838 {
839 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400840 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400841
842 if (simulate) {
843 DataManager::SetValue("tw_partition", "Simulation");
844 simulate_progress_bar();
845 } else {
846 if (arg == "backup") {
847 string Backup_Name;
848 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000849 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 -0400850 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500851 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400852 else {
853 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400854 return -1;
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000855
Dees_Troy43d8b002012-09-17 16:00:01 -0400856 }
Dees Troyb21cc642013-09-10 17:36:41 +0000857 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -0400858 } else if (arg == "restore") {
859 string Restore_Name;
860 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400861 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400862 } else {
863 operation_end(1, simulate);
864 return -1;
865 }
866 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000867 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400868 if (ret == false)
869 ret = 1; // 1 for failure
870 else
871 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200872 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000873 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200874 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400875 if (function == "fixpermissions")
876 {
877 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200878 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400879 if (simulate) {
880 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400881 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400882 int op_status = PartitionManager.Fix_Permissions();
883 if (op_status != 0)
884 op_status = 1; // failure
885 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400886 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400887 return 0;
888 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200889 if (function == "dd")
890 {
891 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -0400892
893 if (simulate) {
894 simulate_progress_bar();
895 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500896 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +0200897 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400898 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200899 operation_end(0, simulate);
900 return 0;
901 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400902 if (function == "partitionsd")
903 {
904 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400905 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400906
907 if (simulate) {
908 simulate_progress_bar();
909 } else {
910 int allow_partition;
911 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
912 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000913 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400914 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400915 if (!PartitionManager.Partition_SDCard())
916 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400917 }
918 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400919 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400920 return 0;
921 }
922 if (function == "installhtcdumlock")
923 {
924 operation_start("Install HTC Dumlock");
925 if (simulate) {
926 simulate_progress_bar();
927 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400928 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400929
930 operation_end(0, simulate);
931 return 0;
932 }
933 if (function == "htcdumlockrestoreboot")
934 {
935 operation_start("HTC Dumlock Restore Boot");
936 if (simulate) {
937 simulate_progress_bar();
938 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400939 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400940
941 operation_end(0, simulate);
942 return 0;
943 }
944 if (function == "htcdumlockreflashrecovery")
945 {
946 operation_start("HTC Dumlock Reflash Recovery");
947 if (simulate) {
948 simulate_progress_bar();
949 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400950 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400951
952 operation_end(0, simulate);
953 return 0;
954 }
955 if (function == "cmd")
956 {
957 int op_status = 0;
958
959 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400961 if (simulate) {
962 simulate_progress_bar();
963 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +0200964 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400965 if (op_status != 0)
966 op_status = 1;
967 }
968
969 operation_end(op_status, simulate);
970 return 0;
971 }
972 if (function == "terminalcommand")
973 {
974 int op_status = 0;
975 string cmdpath, command;
976
977 DataManager::GetValue("tw_terminal_location", cmdpath);
978 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +0000979 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400980 if (simulate) {
981 simulate_progress_bar();
982 operation_end(op_status, simulate);
983 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400984 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +0000985 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400986 DataManager::SetValue("tw_terminal_command_thread", command);
987 DataManager::SetValue("tw_terminal_state", 1);
988 DataManager::SetValue("tw_background_thread_running", 1);
989 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
990 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000991 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400992 DataManager::SetValue("tw_terminal_state", 0);
993 DataManager::SetValue("tw_background_thread_running", 0);
994 operation_end(1, simulate);
995 }
996 }
997 return 0;
998 }
999 if (function == "killterminal")
1000 {
1001 int op_status = 0;
1002
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001004 operation_start("KillCommand");
1005 DataManager::SetValue("tw_operation_status", 0);
1006 DataManager::SetValue("tw_operation_state", 1);
1007 DataManager::SetValue("tw_terminal_state", 0);
1008 DataManager::SetValue("tw_background_thread_running", 0);
1009 DataManager::SetValue(TW_ACTION_BUSY, 0);
1010 return 0;
1011 }
1012 if (function == "reinjecttwrp")
1013 {
1014 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001015 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001016 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001017 if (simulate) {
1018 simulate_progress_bar();
1019 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001020 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001022 }
1023
1024 operation_end(op_status, simulate);
1025 return 0;
1026 }
1027 if (function == "checkbackupname")
1028 {
1029 int op_status = 0;
1030
1031 operation_start("CheckBackupName");
1032 if (simulate) {
1033 simulate_progress_bar();
1034 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001035 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001036 if (op_status != 0)
1037 op_status = 1;
1038 }
1039
1040 operation_end(op_status, simulate);
1041 return 0;
1042 }
1043 if (function == "decrypt")
1044 {
1045 int op_status = 0;
1046
1047 operation_start("Decrypt");
1048 if (simulate) {
1049 simulate_progress_bar();
1050 } else {
1051 string Password;
1052 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001053 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001054 if (op_status != 0)
1055 op_status = 1;
1056 else {
1057 int load_theme = 1;
1058
1059 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001060
1061 if (load_theme) {
1062 int has_datamedia;
1063
1064 // Check for a custom theme and load it if exists
1065 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1066 if (has_datamedia != 0) {
1067 struct stat st;
1068 int check = 0;
1069 std::string theme_path;
1070
1071 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001072 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001074 check = 1;
1075 }
1076
1077 theme_path += "/TWRP/theme/ui.zip";
1078 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1079 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1080 {
1081 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001083 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1084 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001086 }
1087 }
1088 }
1089 }
1090 }
1091 }
1092 }
1093
1094 operation_end(op_status, simulate);
1095 return 0;
1096 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001097 if (function == "adbsideload")
1098 {
1099 int ret = 0;
1100
1101 operation_start("Sideload");
1102 if (simulate) {
1103 simulate_progress_bar();
1104 } else {
1105 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001106 int wipe_dalvik = 0;
Vojtech Bocek05534202013-09-11 08:11:56 +02001107 string Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001108
Ethan Yonker45312e52014-02-26 09:23:53 -06001109 if (!PartitionManager.Mount_Current_Storage(false)) {
1110 gui_print("Using RAM for sideload storage.\n");
1111 Sideload_File = "/tmp/sideload.zip";
1112 } else {
1113 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
Dees_Troycfb63ae2012-09-19 14:30:17 -04001114 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001115 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001116 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001117 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001118 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001119 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001120 ret = apply_from_adb(Sideload_File.c_str());
1121 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 -05001122 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001123 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001124 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001125 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1126 PartitionManager.Wipe_By_Path("/cache");
1127 if (wipe_dalvik)
1128 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 } else {
1130 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001131 }
Dees Troy8c0f06b2013-11-26 21:21:29 +00001132 PartitionManager.Update_System_Details();
Dees_Troy06b4fe92012-10-16 11:43:20 -04001133 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1134 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001136 if (simulate) {
1137 simulate_progress_bar();
1138 } else {
1139 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1140 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001141 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001142 else {
1143 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 +02001144 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001145 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001147 }
1148 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001149 }
1150 operation_end(ret, simulate);
1151 return 0;
1152 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001153 if (function == "adbsideloadcancel")
1154 {
1155 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001156 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001157 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001158 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001159 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001160 property_get("tw_child_pid", child_prop, "error");
1161 if (strcmp(child_prop, "error") == 0) {
1162 LOGERR("Unable to get child ID from prop\n");
1163 return 0;
1164 }
1165 child_pid = atoi(child_prop);
1166 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001167 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001168 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001169 return 0;
1170 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001171 if (function == "openrecoveryscript") {
1172 operation_start("OpenRecoveryScript");
1173 if (simulate) {
1174 simulate_progress_bar();
1175 } else {
1176 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1177 // that we converted to ORS commands during boot in recovery.cpp.
1178 // Run those first.
1179 int reboot = 0;
1180 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001181 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001182 if (OpenRecoveryScript::run_script_file() == 0) {
1183 reboot = 1;
1184 }
1185 }
1186 // Check for the ORS file in /cache and attempt to run those commands.
1187 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001189 if (OpenRecoveryScript::run_script_file() == 0) {
1190 reboot = 1;
1191 }
1192 }
1193 if (reboot) {
1194 usleep(2000000); // Sleep for 2 seconds before rebooting
1195 TWFunc::tw_reboot(rb_system);
1196 } else {
1197 DataManager::SetValue("tw_page_done", 1);
1198 }
1199 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001200 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001201 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001202 if (function == "installsu")
1203 {
1204 int op_status = 0;
1205
1206 operation_start("Install SuperSU");
1207 if (simulate) {
1208 simulate_progress_bar();
1209 } else {
1210 if (!TWFunc::Install_SuperSU())
1211 op_status = 1;
1212 }
1213
1214 operation_end(op_status, simulate);
1215 return 0;
1216 }
1217 if (function == "fixsu")
1218 {
1219 int op_status = 0;
1220
1221 operation_start("Fixing Superuser Permissions");
1222 if (simulate) {
1223 simulate_progress_bar();
1224 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001225 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1226 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001227 }
1228
1229 operation_end(op_status, simulate);
1230 return 0;
1231 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001232 if (function == "decrypt_backup")
1233 {
1234 int op_status = 0;
1235
1236 operation_start("Try Restore Decrypt");
1237 if (simulate) {
1238 simulate_progress_bar();
1239 } else {
1240 string Restore_Path, Filename, Password;
1241 DataManager::GetValue("tw_restore", Restore_Path);
1242 Restore_Path += "/";
1243 DataManager::GetValue("tw_restore_password", Password);
1244 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1245 op_status = 0; // success
1246 else
1247 op_status = 1; // fail
1248 }
1249
1250 operation_end(op_status, simulate);
1251 return 0;
1252 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001253 }
1254 else
1255 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001256 pthread_t t;
1257 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001258 return 0;
1259 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001260 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001261 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001262}
1263
1264int GUIAction::getKeyByName(std::string key)
1265{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001266 if (key == "home") return KEY_HOME;
1267 else if (key == "menu") return KEY_MENU;
1268 else if (key == "back") return KEY_BACK;
1269 else if (key == "search") return KEY_SEARCH;
1270 else if (key == "voldown") return KEY_VOLUMEDOWN;
1271 else if (key == "volup") return KEY_VOLUMEUP;
1272 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001273 int ret_val;
1274 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1275 if (!ret_val)
1276 return KEY_POWER;
1277 else
1278 return ret_val;
1279 }
1280
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001281 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001282}
1283
1284void* GUIAction::command_thread(void *cookie)
1285{
1286 string command;
1287 FILE* fp;
1288 char line[512];
1289
1290 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001291 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001292 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001294 } else {
1295 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1296 struct timeval timeout;
1297 fd_set fdset;
1298
1299 while(keep_going)
1300 {
1301 FD_ZERO(&fdset);
1302 FD_SET(fd, &fdset);
1303 timeout.tv_sec = 0;
1304 timeout.tv_usec = 400000;
1305 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1306 if (has_data == 0) {
1307 // Timeout reached
1308 DataManager::GetValue("tw_terminal_state", check);
1309 if (check == 0) {
1310 keep_going = 0;
1311 }
1312 } else if (has_data < 0) {
1313 // End of execution
1314 keep_going = 0;
1315 } else {
1316 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001317 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001318 bytes_read = read(fd, line, sizeof(line));
1319 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001321 else
1322 keep_going = 0; // Done executing
1323 }
1324 }
1325 fclose(fp);
1326 }
1327 DataManager::SetValue("tw_operation_status", 0);
1328 DataManager::SetValue("tw_operation_state", 1);
1329 DataManager::SetValue("tw_terminal_state", 0);
1330 DataManager::SetValue("tw_background_thread_running", 0);
1331 DataManager::SetValue(TW_ACTION_BUSY, 0);
1332 return NULL;
1333}