blob: d636934409f0efbc5860fb8630b4733b6f61d06e [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// image.cpp - GUIImage object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/time.h>
10#include <sys/mman.h>
11#include <sys/types.h>
12#include <sys/ioctl.h>
13#include <linux/input.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040017#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <string>
20#include <sstream>
21#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040022#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040023#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
Dees_Troy43d8b002012-09-17 16:00:01 -040025#include "../ui.h"
26#include "../adb_install.h"
27
Dees_Troy51a0e822012-09-05 15:24:24 -040028extern "C" {
29#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040030#include "../tw_reboot.h"
31#include "../minuitwrp/minui.h"
32#include "../recovery_ui.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040033#include "../extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040035#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036
Dees_Troy43d8b002012-09-17 16:00:01 -040037#include "../minadbd/adb.h"
38
Dees_Troy32c8eb82012-09-11 15:28:06 -040039int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040040int check_backup_name(int show_error);
Dees_Troy51a0e822012-09-05 15:24:24 -040041void 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 -040042int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040043int gui_start();
44};
45
46#include "rapidxml.hpp"
47#include "objects.hpp"
48
Dees_Troy43d8b002012-09-17 16:00:01 -040049extern RecoveryUI* ui;
50
Dees_Troy51a0e822012-09-05 15:24:24 -040051void curtainClose(void);
52
53GUIAction::GUIAction(xml_node<>* node)
54 : Conditional(node)
55{
56 xml_node<>* child;
57 xml_node<>* actions;
58 xml_attribute<>* attr;
59
60 mKey = 0;
61
62 if (!node) return;
63
64 // First, get the action
65 actions = node->first_node("actions");
66 if (actions) child = actions->first_node("action");
67 else child = node->first_node("action");
68
69 if (!child) return;
70
71 while (child)
72 {
73 Action action;
74
75 attr = child->first_attribute("function");
76 if (!attr) return;
77
78 action.mFunction = attr->value();
79 action.mArg = child->value();
80 mActions.push_back(action);
81
82 child = child->next_sibling("action");
83 }
84
85 // Now, let's get either the key or region
86 child = node->first_node("touch");
87 if (child)
88 {
89 attr = child->first_attribute("key");
90 if (attr)
91 {
92 std::string key = attr->value();
93
94 mKey = getKeyByName(key);
95 }
96 else
97 {
98 attr = child->first_attribute("x");
99 if (!attr) return;
100 mActionX = atol(attr->value());
101 attr = child->first_attribute("y");
102 if (!attr) return;
103 mActionY = atol(attr->value());
104 attr = child->first_attribute("w");
105 if (!attr) return;
106 mActionW = atol(attr->value());
107 attr = child->first_attribute("h");
108 if (!attr) return;
109 mActionH = atol(attr->value());
110 }
111 }
112}
113
114int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
115{
116 if (state == TOUCH_RELEASE)
117 doActions();
118
119 return 0;
120}
121
122int GUIAction::NotifyKey(int key)
123{
124 if (!mKey || key != mKey)
125 return 1;
126
127 doActions();
128 return 0;
129}
130
131int GUIAction::NotifyVarChange(std::string varName, std::string value)
132{
133 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
134 doActions();
135
136 // This handles notifying the condition system of page start
137 if (varName.empty() && isConditionValid())
138 NotifyPageSet();
139
140 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
141 doActions();
142
143 return 0;
144}
145
146void GUIAction::simulate_progress_bar(void)
147{
148 ui_print("Simulating actions...\n");
149 for (int i = 0; i < 5; i++)
150 {
151 usleep(500000);
152 DataManager::SetValue("ui_progress", i * 20);
153 }
154}
155
Dees_Troy657c3092012-09-10 20:32:10 -0400156int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400157{
158 int ret_val = 0;
159
160 DataManager::SetValue("ui_progress", 0);
161
162 if (filename.empty())
163 {
164 LOGE("No file specified.\n");
165 return -1;
166 }
167
168 // We're going to jump to this page first, like a loading page
169 gui_changePage(pageName);
170
171 int fd = -1;
172 ZipArchive zip;
173
Dees_Troy657c3092012-09-10 20:32:10 -0400174 if (!PartitionManager.Mount_By_Path(filename, true))
175 return -1;
176
177 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400178 {
179 LOGE("Unable to open zip file.\n");
180 return -1;
181 }
182
183 // Check the zip to see if it has a custom installer theme
184 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
185 if (twrp != NULL)
186 {
187 unlink("/tmp/twrp.zip");
188 fd = creat("/tmp/twrp.zip", 0666);
189 }
190 if (fd >= 0 && twrp != NULL &&
191 mzExtractZipEntryToFile(&zip, twrp, fd) &&
192 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
193 {
194 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400195 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400196 gui_changePage("main");
197 }
198 else
199 {
200 // In this case, we just use the default page
201 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400202 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 }
204 if (fd >= 0)
205 close(fd);
206
207 if (simulate) {
208 simulate_progress_bar();
209 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400210 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
212 // Now, check if we need to ensure TWRP remains installed...
213 struct stat st;
214 if (stat("/sbin/installTwrp", &st) == 0)
215 {
216 DataManager::SetValue("tw_operation", "Configuring TWRP");
217 DataManager::SetValue("tw_partition", "");
218 ui_print("Configuring TWRP...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400219 if (system("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 {
221 ui_print("Unable to configure TWRP with this kernel.\n");
222 }
223 }
224 }
225
226 // Done
227 DataManager::SetValue("ui_progress", 100);
228 DataManager::SetValue("ui_progress", 0);
229 return ret_val;
230}
231
232int GUIAction::doActions()
233{
234 if (mActions.size() < 1) return -1;
235 if (mActions.size() == 1)
236 return doAction(mActions.at(0), 0);
237
238 // For multi-action, we always use a thread
239 pthread_t t;
240 pthread_create(&t, NULL, thread_start, this);
241
242 return 0;
243}
244
245void* GUIAction::thread_start(void *cookie)
246{
247 GUIAction* ourThis = (GUIAction*) cookie;
248
249 DataManager::SetValue(TW_ACTION_BUSY, 1);
250
251 if (ourThis->mActions.size() > 1)
252 {
253 std::vector<Action>::iterator iter;
254 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
255 ourThis->doAction(*iter, 1);
256 }
257 else
258 {
259 ourThis->doAction(ourThis->mActions.at(0), 1);
260 }
261 int check = 0;
262 DataManager::GetValue("tw_background_thread_running", check);
263 if (check == 0)
264 DataManager::SetValue(TW_ACTION_BUSY, 0);
265 return NULL;
266}
267
268void GUIAction::operation_start(const string operation_name)
269{
270 DataManager::SetValue(TW_ACTION_BUSY, 1);
271 DataManager::SetValue("ui_progress", 0);
272 DataManager::SetValue("tw_operation", operation_name);
273 DataManager::SetValue("tw_operation_status", 0);
274 DataManager::SetValue("tw_operation_state", 0);
275}
276
277void GUIAction::operation_end(const int operation_status, const int simulate)
278{
279 int simulate_fail;
280
281 DataManager::SetValue("ui_progress", 100);
282 if (simulate) {
283 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
284 if (simulate_fail != 0)
285 DataManager::SetValue("tw_operation_status", 1);
286 else
287 DataManager::SetValue("tw_operation_status", 0);
288 } else {
289 if (operation_status != 0)
290 DataManager::SetValue("tw_operation_status", 1);
291 else
292 DataManager::SetValue("tw_operation_status", 0);
293 }
294 DataManager::SetValue("tw_operation_state", 1);
295 DataManager::SetValue(TW_ACTION_BUSY, 0);
296}
297
298int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
299{
300 static string zip_queue[10];
301 static int zip_queue_index;
302 static pthread_t terminal_command;
303 int simulate;
304
305 std::string arg = gui_parse_text(action.mArg);
306
307 std::string function = gui_parse_text(action.mFunction);
308
309 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
310
311 if (function == "reboot")
312 {
313 //curtainClose(); this sometimes causes a crash
314
315 sync();
316
317 if (arg == "recovery")
318 tw_reboot(rb_recovery);
319 else if (arg == "poweroff")
320 tw_reboot(rb_poweroff);
321 else if (arg == "bootloader")
322 tw_reboot(rb_bootloader);
323 else if (arg == "download")
324 tw_reboot(rb_download);
325 else
326 tw_reboot(rb_system);
327
328 // This should never occur
329 return -1;
330 }
331 if (function == "home")
332 {
333 PageManager::SelectPackage("TWRP");
334 gui_changePage("main");
335 return 0;
336 }
337
338 if (function == "key")
339 {
340 PageManager::NotifyKey(getKeyByName(arg));
341 return 0;
342 }
343
344 if (function == "page") {
345 std::string page_name = gui_parse_text(arg);
346 return gui_changePage(page_name);
347 }
348
349 if (function == "reload") {
350 int check = 0, ret_val = 0;
351 std::string theme_path;
352
353 operation_start("Reload Theme");
354 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
357 check = 1;
358 }
359
360 theme_path += "/TWRP/theme/ui.zip";
361 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
362 {
363 // Loading the custom theme failed - try loading the stock theme
364 LOGI("Attempting to reload stock theme...\n");
365 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
366 {
367 LOGE("Failed to load base packages.\n");
368 ret_val = 1;
369 }
370 }
371 operation_end(ret_val, simulate);
372 }
373
374 if (function == "readBackup")
375 {
376 string Restore_Name;
377 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 return 0;
380 }
381
382 if (function == "set")
383 {
384 if (arg.find('=') != string::npos)
385 {
386 string varName = arg.substr(0, arg.find('='));
387 string value = arg.substr(arg.find('=') + 1, string::npos);
388
389 DataManager::GetValue(value, value);
390 DataManager::SetValue(varName, value);
391 }
392 else
393 DataManager::SetValue(arg, "1");
394 return 0;
395 }
396 if (function == "clear")
397 {
398 DataManager::SetValue(arg, "0");
399 return 0;
400 }
401
402 if (function == "mount")
403 {
404 if (arg == "usb")
405 {
406 DataManager::SetValue(TW_ACTION_BUSY, 1);
407 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400408 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 else
410 ui_print("Simulating actions...\n");
411 }
412 else if (!simulate)
413 {
414 string cmd;
415 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400416 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400418 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 else
Dees_Troy51127312012-09-08 13:08:49 -0400420 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 } else
422 ui_print("Simulating actions...\n");
423 return 0;
424 }
425
426 if (function == "umount" || function == "unmount")
427 {
428 if (arg == "usb")
429 {
430 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400431 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400432 else
433 ui_print("Simulating actions...\n");
434 DataManager::SetValue(TW_ACTION_BUSY, 0);
435 }
436 else if (!simulate)
437 {
438 string cmd;
439 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400440 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400442 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 else
Dees_Troy51127312012-09-08 13:08:49 -0400444 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 } else
446 ui_print("Simulating actions...\n");
447 return 0;
448 }
449
450 if (function == "restoredefaultsettings")
451 {
452 operation_start("Restore Defaults");
453 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
454 ui_print("Simulating actions...\n");
455 else {
456 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 PartitionManager.Update_System_Details();
458 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 }
460 operation_end(0, simulate);
461 }
462
463 if (function == "copylog")
464 {
465 operation_start("Copy Log");
466 if (!simulate)
467 {
468 char command[255];
469
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400472 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 sync();
474 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
475 } else
476 simulate_progress_bar();
477 operation_end(0, simulate);
478 return 0;
479 }
480
481 if (function == "compute" || function == "addsubtract")
482 {
483 if (arg.find("+") != string::npos)
484 {
485 string varName = arg.substr(0, arg.find('+'));
486 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
487 int amount_to_add = atoi(string_to_add.c_str());
488 int value;
489
490 DataManager::GetValue(varName, value);
491 DataManager::SetValue(varName, value + amount_to_add);
492 return 0;
493 }
494 if (arg.find("-") != string::npos)
495 {
496 string varName = arg.substr(0, arg.find('-'));
497 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
498 int amount_to_subtract = atoi(string_to_subtract.c_str());
499 int value;
500
501 DataManager::GetValue(varName, value);
502 value -= amount_to_subtract;
503 if (value <= 0)
504 value = 0;
505 DataManager::SetValue(varName, value);
506 return 0;
507 }
508 }
509
510 if (function == "setguitimezone")
511 {
512 string SelectedZone;
513 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
514 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
515 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
516
517 int dst;
518 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
519
520 string offset;
521 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
522
523 string NewTimeZone = Zone;
524 if (offset != "0")
525 NewTimeZone += ":" + offset;
526
527 if (dst != 0)
528 NewTimeZone += DSTZone;
529
530 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400531 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 return 0;
533 }
534
535 if (function == "togglestorage") {
536 if (arg == "internal") {
537 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
538 } else if (arg == "external") {
539 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
540 }
Dees_Troy51127312012-09-08 13:08:49 -0400541 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400542 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400543 string zip_path, zip_root;
544 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
545 zip_root = TWFunc::Get_Root_Path(zip_path);
546#ifdef RECOVERY_SDCARD_ON_DATA
547 #ifndef TW_EXTERNAL_STORAGE_PATH
548 if (zip_root != "/sdcard")
549 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
550 #else
551 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
552 if (zip_root != "/emmc")
553 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
554 } else {
555 if (zip_root != "/sdcard")
556 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
557 }
558 #endif
559#else
560 if (zip_root != DataManager::GetCurrentStoragePath())
561 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
562#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 // Save the current zip location to the external variable
564 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
565 // Change the current zip location to the internal variable
566 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
567 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400568 string zip_path, zip_root;
569 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
570 zip_root = TWFunc::Get_Root_Path(zip_path);
571 if (zip_root != DataManager::GetCurrentStoragePath()) {
572 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
573 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400574 // Save the current zip location to the internal variable
575 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
576 // Change the current zip location to the external variable
577 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
578 }
579 } else {
580 // We weren't able to toggle for some reason, restore original setting
581 if (arg == "internal") {
582 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
583 } else if (arg == "external") {
584 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
585 }
586 }
587 return 0;
588 }
589
590 if (function == "overlay")
591 return gui_changeOverlay(arg);
592
593 if (function == "queuezip")
594 {
595 if (zip_queue_index >= 10) {
596 ui_print("Maximum zip queue reached!\n");
597 return 0;
598 }
599 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
600 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
601 zip_queue_index++;
602 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
603 }
604 return 0;
605 }
606
607 if (function == "cancelzip")
608 {
609 if (zip_queue_index <= 0) {
610 ui_print("Minimum zip queue reached!\n");
611 return 0;
612 } else {
613 zip_queue_index--;
614 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
615 }
616 return 0;
617 }
618
619 if (function == "queueclear")
620 {
621 zip_queue_index = 0;
622 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
623 return 0;
624 }
625
626 if (function == "sleep")
627 {
628 operation_start("Sleep");
629 usleep(atoi(arg.c_str()));
630 operation_end(0, simulate);
631 return 0;
632 }
633
634 if (isThreaded)
635 {
636 if (function == "fileexists")
637 {
638 struct stat st;
639 string newpath = arg + "/.";
640
641 operation_start("FileExists");
642 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
643 operation_end(0, simulate);
644 else
645 operation_end(1, simulate);
646 }
647
648 if (function == "flash")
649 {
Dees_Troy657c3092012-09-10 20:32:10 -0400650 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400651
652 for (i=0; i<zip_queue_index; i++) {
653 operation_start("Flashing");
654 DataManager::SetValue("tw_filename", zip_queue[i]);
655 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
656
Dees_Troy657c3092012-09-10 20:32:10 -0400657 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400658 if (ret_val != 0) {
659 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
660 i = 10; // Error flashing zip - exit queue
661 ret_val = 1;
662 }
663 }
664 zip_queue_index = 0;
665 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
666
Dees_Troy657c3092012-09-10 20:32:10 -0400667 if (wipe_cache)
668 PartitionManager.Wipe_By_Path("/cache");
669
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
671 operation_start("ReinjectTWRP");
672 ui_print("Injecting TWRP into boot image...\n");
673 if (simulate) {
674 simulate_progress_bar();
675 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400676 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 ui_print("TWRP injection complete.\n");
678 }
679 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400680 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 operation_end(ret_val, simulate);
682 return 0;
683 }
684 if (function == "wipe")
685 {
686 operation_start("Format");
687 DataManager::SetValue("tw_partition", arg);
688
Dees_Troy38bd7602012-09-14 13:33:53 -0400689 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400690
691 if (simulate) {
692 simulate_progress_bar();
693 } else {
694 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400695 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400697 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400699 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400701 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400703 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 } else if (arg == "INTERNAL") {
705 int has_datamedia, dual_storage;
706
707 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
708 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400709 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400711 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 }
713 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400714 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400715
Dees_Troy38bd7602012-09-14 13:33:53 -0400716 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
717 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400718 } else if (arg == "ANDROIDSECURE") {
719 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400720 } else
721 ret_val = PartitionManager.Wipe_By_Path(arg);
722
723 if (arg == DataManager::GetSettingsStoragePath()) {
724 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
725 string Storage_Path = DataManager::GetSettingsStoragePath();
726
727 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
728 LOGI("Making TWRP folder and saving settings.\n");
729 Storage_Path += "/TWRP";
730 mkdir(Storage_Path.c_str(), 0777);
731 DataManager::Flush();
732 } else {
733 LOGE("Unable to recreate TWRP folder and save settings.\n");
734 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 }
736 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400737 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 if (ret_val)
739 ret_val = 0; // 0 is success
740 else
741 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 operation_end(ret_val, simulate);
743 return 0;
744 }
745 if (function == "refreshsizes")
746 {
747 operation_start("Refreshing Sizes");
748 if (simulate) {
749 simulate_progress_bar();
750 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400751 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400752 operation_end(0, simulate);
753 }
754 if (function == "nandroid")
755 {
756 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400757 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400758
759 if (simulate) {
760 DataManager::SetValue("tw_partition", "Simulation");
761 simulate_progress_bar();
762 } else {
763 if (arg == "backup") {
764 string Backup_Name;
765 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400766 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1) == 0)
767 ret = PartitionManager.Run_Backup();
768 else {
769 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
773 } else if (arg == "restore") {
774 string Restore_Name;
775 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400776 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 } else {
778 operation_end(1, simulate);
779 return -1;
780 }
781 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400782 if (ret == false)
783 ret = 1; // 1 for failure
784 else
785 ret = 0; // 0 for success
786 operation_end(ret, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400787 return 0;
788 }
789 if (function == "fixpermissions")
790 {
791 operation_start("Fix Permissions");
792 LOGI("fix permissions started!\n");
793 if (simulate) {
794 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400795 } else {
796 int op_status;
797 if (!PartitionManager.Mount_By_Path("/data", true) || !PartitionManager.Mount_By_Path("/system", true))
798 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400799
Dees_Troy4be841b2012-09-26 14:07:15 -0400800 DataManager::SetValue("tw_terminal_command_thread", "./sbin/fix_permissions.sh");
801 DataManager::SetValue("tw_terminal_state", 1);
802 DataManager::SetValue("tw_background_thread_running", 1);
803 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
804 if (op_status != 0) {
805 LOGE("Error starting terminal command thread, %i.\n", op_status);
806 DataManager::SetValue("tw_terminal_state", 0);
807 DataManager::SetValue("tw_background_thread_running", 0);
808 operation_end(1, simulate);
809 }
810 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 return 0;
812 }
813 if (function == "dd")
814 {
815 operation_start("imaging");
816
817 if (simulate) {
818 simulate_progress_bar();
819 } else {
820 char cmd[512];
821 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400822 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400823 }
824 operation_end(0, simulate);
825 return 0;
826 }
827 if (function == "partitionsd")
828 {
829 operation_start("Partition SD Card");
830
831 if (simulate) {
832 simulate_progress_bar();
833 } else {
834 int allow_partition;
835 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
836 if (allow_partition == 0) {
837 ui_print("This device does not have a real SD Card!\nAborting!\n");
838 } else {
839 // Below seen in Koush's recovery
840 char sddevice[256];
841 char mkdir_path[255];
Dees_Troya2ac96a2012-09-26 13:07:22 -0400842#ifdef TW_EXTERNAL_STORAGE_PATH
843 TWPartition* SDCard = PartitionManager.Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
844#else
845 TWPartition* SDCard = PartitionManager.Find_Partition_By_Path("/sdcard");
846#endif
847 if (SDCard == NULL) {
848 LOGE("Unable to locate device to partition.\n");
849 operation_end(1, simulate);
850 return 0;
851 }
852 strcpy(sddevice, SDCard->Actual_Block_Device.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 // Just need block not whole partition
Dees_Troy8170a922012-09-18 15:40:25 -0400854 sddevice[strlen("/dev/block/mmcblkX")] = '\0';
Dees_Troy51a0e822012-09-05 15:24:24 -0400855
856 char es[64];
857 std::string ext_format, sd_path;
858 int ext, swap;
859 DataManager::GetValue("tw_sdext_size", ext);
860 DataManager::GetValue("tw_swap_size", swap);
861 DataManager::GetValue("tw_sdpart_file_system", ext_format);
862 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
863 LOGI("\nrunning script: %s\n", es);
864 run_script("\nContinue partitioning?",
865 "\nPartitioning sdcard : ",
866 es,
867 "\nunable to execute parted!\n(%s)\n",
868 "\nOops... something went wrong!\nPlease check the recovery log!\n",
869 "\nPartitioning complete!\n\n",
870 "\nPartitioning aborted!\n\n", 0);
871
872 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
873#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy5bf43922012-09-07 16:07:55 -0400874 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400875 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
876 memset(mkdir_path, 0, sizeof(mkdir_path));
877 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
878#else
Dees_Troy5bf43922012-09-07 16:07:55 -0400879 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400880 strcpy(mkdir_path, "/sdcard/TWRP");
881#endif
882 mkdir(mkdir_path, 0777);
883 DataManager::Flush();
884#ifdef TW_EXTERNAL_STORAGE_PATH
885 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
886 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
887 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
888#else
889 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
890 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
891 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
892#endif
893 // This is sometimes needed to make a healthy ext4 partition
894 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
895 char command[256];
896 LOGE("Fix this format command!\n");
897 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
898 ui_print("Formatting sd-ext as ext4...\n");
899 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
Dees_Troy8170a922012-09-18 15:40:25 -0400900 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400901 ui_print("DONE\n");
902 }
903
Dees_Troy5bf43922012-09-07 16:07:55 -0400904 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400905 }
906 }
907 operation_end(0, simulate);
908 return 0;
909 }
910 if (function == "installhtcdumlock")
911 {
912 operation_start("Install HTC Dumlock");
913 if (simulate) {
914 simulate_progress_bar();
915 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400916 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400917
918 operation_end(0, simulate);
919 return 0;
920 }
921 if (function == "htcdumlockrestoreboot")
922 {
923 operation_start("HTC Dumlock Restore Boot");
924 if (simulate) {
925 simulate_progress_bar();
926 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400927 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400928
929 operation_end(0, simulate);
930 return 0;
931 }
932 if (function == "htcdumlockreflashrecovery")
933 {
934 operation_start("HTC Dumlock Reflash Recovery");
935 if (simulate) {
936 simulate_progress_bar();
937 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400938 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400939
940 operation_end(0, simulate);
941 return 0;
942 }
943 if (function == "cmd")
944 {
945 int op_status = 0;
946
947 operation_start("Command");
948 LOGI("Running command: '%s'\n", arg.c_str());
949 if (simulate) {
950 simulate_progress_bar();
951 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400952 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400953 if (op_status != 0)
954 op_status = 1;
955 }
956
957 operation_end(op_status, simulate);
958 return 0;
959 }
960 if (function == "terminalcommand")
961 {
962 int op_status = 0;
963 string cmdpath, command;
964
965 DataManager::GetValue("tw_terminal_location", cmdpath);
966 operation_start("CommandOutput");
967 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
968 if (simulate) {
969 simulate_progress_bar();
970 operation_end(op_status, simulate);
971 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400972 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400973 LOGI("Actual command is: '%s'\n", command.c_str());
974 DataManager::SetValue("tw_terminal_command_thread", command);
975 DataManager::SetValue("tw_terminal_state", 1);
976 DataManager::SetValue("tw_background_thread_running", 1);
977 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
978 if (op_status != 0) {
979 LOGE("Error starting terminal command thread, %i.\n", op_status);
980 DataManager::SetValue("tw_terminal_state", 0);
981 DataManager::SetValue("tw_background_thread_running", 0);
982 operation_end(1, simulate);
983 }
984 }
985 return 0;
986 }
987 if (function == "killterminal")
988 {
989 int op_status = 0;
990
991 LOGI("Sending kill command...\n");
992 operation_start("KillCommand");
993 DataManager::SetValue("tw_operation_status", 0);
994 DataManager::SetValue("tw_operation_state", 1);
995 DataManager::SetValue("tw_terminal_state", 0);
996 DataManager::SetValue("tw_background_thread_running", 0);
997 DataManager::SetValue(TW_ACTION_BUSY, 0);
998 return 0;
999 }
1000 if (function == "reinjecttwrp")
1001 {
1002 int op_status = 0;
1003
1004 operation_start("ReinjectTWRP");
1005 ui_print("Injecting TWRP into boot image...\n");
1006 if (simulate) {
1007 simulate_progress_bar();
1008 } else {
Dees_Troy8170a922012-09-18 15:40:25 -04001009 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 ui_print("TWRP injection complete.\n");
1011 }
1012
1013 operation_end(op_status, simulate);
1014 return 0;
1015 }
1016 if (function == "checkbackupname")
1017 {
1018 int op_status = 0;
1019
1020 operation_start("CheckBackupName");
1021 if (simulate) {
1022 simulate_progress_bar();
1023 } else {
1024 op_status = check_backup_name(1);
1025 if (op_status != 0)
1026 op_status = 1;
1027 }
1028
1029 operation_end(op_status, simulate);
1030 return 0;
1031 }
1032 if (function == "decrypt")
1033 {
1034 int op_status = 0;
1035
1036 operation_start("Decrypt");
1037 if (simulate) {
1038 simulate_progress_bar();
1039 } else {
1040 string Password;
1041 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001042 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 if (op_status != 0)
1044 op_status = 1;
1045 else {
1046 int load_theme = 1;
1047
1048 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1049 DataManager::ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -04001050 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001051 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -04001052 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 usleep(2000000); // Sleep for 2 seconds before rebooting
1054 tw_reboot(rb_system);
1055 load_theme = 0;
1056 }
1057 }
1058
1059 if (load_theme) {
1060 int has_datamedia;
1061
1062 // Check for a custom theme and load it if exists
1063 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1064 if (has_datamedia != 0) {
1065 struct stat st;
1066 int check = 0;
1067 std::string theme_path;
1068
1069 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001070 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001071 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1072 check = 1;
1073 }
1074
1075 theme_path += "/TWRP/theme/ui.zip";
1076 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1077 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1078 {
1079 // Loading the custom theme failed - try loading the stock theme
1080 LOGI("Attempting to reload stock theme...\n");
1081 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1082 {
1083 LOGE("Failed to load base packages.\n");
1084 }
1085 }
1086 }
1087 }
1088 }
1089 }
1090 }
1091
1092 operation_end(op_status, simulate);
1093 return 0;
1094 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001095 if (function == "adbsideload")
1096 {
1097 int ret = 0;
1098
1099 operation_start("Sideload");
1100 if (simulate) {
1101 simulate_progress_bar();
1102 } else {
1103 int wipe_cache = 0;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001104 string Command, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001105
1106 if (!PartitionManager.Mount_Current_Storage(true)) {
1107 operation_end(1, simulate);
1108 return 0;
1109 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001110 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1111 if (TWFunc::Path_Exists(Sideload_File)) {
1112 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001113 system(Command.c_str());
1114 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001115 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001116 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001117 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001118 ret = 1; // failure
1119 else if (wipe_cache)
1120 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy43d8b002012-09-17 16:00:01 -04001121 }
1122 operation_end(ret, simulate);
1123 return 0;
1124 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001125 if (function == "adbsideloadcancel")
1126 {
1127 int child_pid;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001128 string Command, Sideload_File;
1129 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1130 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001131 system(Command.c_str());
1132 DataManager::GetValue("tw_child_pid", child_pid);
1133 ui_print("Cancelling ADB sideload...\n");
1134 kill(child_pid, SIGTERM);
1135 return 0;
1136 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001137 }
1138 else
1139 {
1140 pthread_t t;
1141 pthread_create(&t, NULL, thread_start, this);
1142 return 0;
1143 }
1144 return -1;
1145}
1146
1147int GUIAction::getKeyByName(std::string key)
1148{
1149 if (key == "home") return KEY_HOME;
1150 else if (key == "menu") return KEY_MENU;
1151 else if (key == "back") return KEY_BACK;
1152 else if (key == "search") return KEY_SEARCH;
1153 else if (key == "voldown") return KEY_VOLUMEDOWN;
1154 else if (key == "volup") return KEY_VOLUMEUP;
1155 else if (key == "power") {
1156 int ret_val;
1157 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1158 if (!ret_val)
1159 return KEY_POWER;
1160 else
1161 return ret_val;
1162 }
1163
1164 return atol(key.c_str());
1165}
1166
1167void* GUIAction::command_thread(void *cookie)
1168{
1169 string command;
1170 FILE* fp;
1171 char line[512];
1172
1173 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001174 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001175 if (fp == NULL) {
1176 LOGE("Error opening command to run.\n");
1177 } else {
1178 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1179 struct timeval timeout;
1180 fd_set fdset;
1181
1182 while(keep_going)
1183 {
1184 FD_ZERO(&fdset);
1185 FD_SET(fd, &fdset);
1186 timeout.tv_sec = 0;
1187 timeout.tv_usec = 400000;
1188 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1189 if (has_data == 0) {
1190 // Timeout reached
1191 DataManager::GetValue("tw_terminal_state", check);
1192 if (check == 0) {
1193 keep_going = 0;
1194 }
1195 } else if (has_data < 0) {
1196 // End of execution
1197 keep_going = 0;
1198 } else {
1199 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001200 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001201 bytes_read = read(fd, line, sizeof(line));
1202 if (bytes_read > 0)
1203 ui_print("%s", line); // Display output
1204 else
1205 keep_going = 0; // Done executing
1206 }
1207 }
1208 fclose(fp);
1209 }
1210 DataManager::SetValue("tw_operation_status", 0);
1211 DataManager::SetValue("tw_operation_state", 1);
1212 DataManager::SetValue("tw_terminal_state", 0);
1213 DataManager::SetValue("tw_background_thread_running", 0);
1214 DataManager::SetValue(TW_ACTION_BUSY, 0);
1215 return NULL;
1216}