blob: 80a582c6cf7b43d9ddb1b817949404772cd6b706 [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"
30#include "../roots.h"
31#include "../tw_reboot.h"
32#include "../minuitwrp/minui.h"
33#include "../recovery_ui.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040034#include "../extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040036#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037
Dees_Troy43d8b002012-09-17 16:00:01 -040038#include "../minadbd/adb.h"
39
Dees_Troy32c8eb82012-09-11 15:28:06 -040040int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040041int check_backup_name(int show_error);
Dees_Troy51a0e822012-09-05 15:24:24 -040042void 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 -040043int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040044int gui_start();
45};
46
47#include "rapidxml.hpp"
48#include "objects.hpp"
49
Dees_Troy43d8b002012-09-17 16:00:01 -040050extern RecoveryUI* ui;
51
Dees_Troy51a0e822012-09-05 15:24:24 -040052void curtainClose(void);
53
54GUIAction::GUIAction(xml_node<>* node)
55 : Conditional(node)
56{
57 xml_node<>* child;
58 xml_node<>* actions;
59 xml_attribute<>* attr;
60
61 mKey = 0;
62
63 if (!node) return;
64
65 // First, get the action
66 actions = node->first_node("actions");
67 if (actions) child = actions->first_node("action");
68 else child = node->first_node("action");
69
70 if (!child) return;
71
72 while (child)
73 {
74 Action action;
75
76 attr = child->first_attribute("function");
77 if (!attr) return;
78
79 action.mFunction = attr->value();
80 action.mArg = child->value();
81 mActions.push_back(action);
82
83 child = child->next_sibling("action");
84 }
85
86 // Now, let's get either the key or region
87 child = node->first_node("touch");
88 if (child)
89 {
90 attr = child->first_attribute("key");
91 if (attr)
92 {
93 std::string key = attr->value();
94
95 mKey = getKeyByName(key);
96 }
97 else
98 {
99 attr = child->first_attribute("x");
100 if (!attr) return;
101 mActionX = atol(attr->value());
102 attr = child->first_attribute("y");
103 if (!attr) return;
104 mActionY = atol(attr->value());
105 attr = child->first_attribute("w");
106 if (!attr) return;
107 mActionW = atol(attr->value());
108 attr = child->first_attribute("h");
109 if (!attr) return;
110 mActionH = atol(attr->value());
111 }
112 }
113}
114
115int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
116{
117 if (state == TOUCH_RELEASE)
118 doActions();
119
120 return 0;
121}
122
123int GUIAction::NotifyKey(int key)
124{
125 if (!mKey || key != mKey)
126 return 1;
127
128 doActions();
129 return 0;
130}
131
132int GUIAction::NotifyVarChange(std::string varName, std::string value)
133{
134 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
135 doActions();
136
137 // This handles notifying the condition system of page start
138 if (varName.empty() && isConditionValid())
139 NotifyPageSet();
140
141 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
142 doActions();
143
144 return 0;
145}
146
147void GUIAction::simulate_progress_bar(void)
148{
149 ui_print("Simulating actions...\n");
150 for (int i = 0; i < 5; i++)
151 {
152 usleep(500000);
153 DataManager::SetValue("ui_progress", i * 20);
154 }
155}
156
Dees_Troy657c3092012-09-10 20:32:10 -0400157int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400158{
159 int ret_val = 0;
160
161 DataManager::SetValue("ui_progress", 0);
162
163 if (filename.empty())
164 {
165 LOGE("No file specified.\n");
166 return -1;
167 }
168
169 // We're going to jump to this page first, like a loading page
170 gui_changePage(pageName);
171
172 int fd = -1;
173 ZipArchive zip;
174
Dees_Troy657c3092012-09-10 20:32:10 -0400175 if (!PartitionManager.Mount_By_Path(filename, true))
176 return -1;
177
178 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400179 {
180 LOGE("Unable to open zip file.\n");
181 return -1;
182 }
183
184 // Check the zip to see if it has a custom installer theme
185 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
186 if (twrp != NULL)
187 {
188 unlink("/tmp/twrp.zip");
189 fd = creat("/tmp/twrp.zip", 0666);
190 }
191 if (fd >= 0 && twrp != NULL &&
192 mzExtractZipEntryToFile(&zip, twrp, fd) &&
193 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
194 {
195 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400196 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 gui_changePage("main");
198 }
199 else
200 {
201 // In this case, we just use the default page
202 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400203 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400204 }
205 if (fd >= 0)
206 close(fd);
207
208 if (simulate) {
209 simulate_progress_bar();
210 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400211 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
213 // Now, check if we need to ensure TWRP remains installed...
214 struct stat st;
215 if (stat("/sbin/installTwrp", &st) == 0)
216 {
217 DataManager::SetValue("tw_operation", "Configuring TWRP");
218 DataManager::SetValue("tw_partition", "");
219 ui_print("Configuring TWRP...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400220 if (system("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400221 {
222 ui_print("Unable to configure TWRP with this kernel.\n");
223 }
224 }
225 }
226
227 // Done
228 DataManager::SetValue("ui_progress", 100);
229 DataManager::SetValue("ui_progress", 0);
230 return ret_val;
231}
232
233int GUIAction::doActions()
234{
235 if (mActions.size() < 1) return -1;
236 if (mActions.size() == 1)
237 return doAction(mActions.at(0), 0);
238
239 // For multi-action, we always use a thread
240 pthread_t t;
241 pthread_create(&t, NULL, thread_start, this);
242
243 return 0;
244}
245
246void* GUIAction::thread_start(void *cookie)
247{
248 GUIAction* ourThis = (GUIAction*) cookie;
249
250 DataManager::SetValue(TW_ACTION_BUSY, 1);
251
252 if (ourThis->mActions.size() > 1)
253 {
254 std::vector<Action>::iterator iter;
255 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
256 ourThis->doAction(*iter, 1);
257 }
258 else
259 {
260 ourThis->doAction(ourThis->mActions.at(0), 1);
261 }
262 int check = 0;
263 DataManager::GetValue("tw_background_thread_running", check);
264 if (check == 0)
265 DataManager::SetValue(TW_ACTION_BUSY, 0);
266 return NULL;
267}
268
269void GUIAction::operation_start(const string operation_name)
270{
271 DataManager::SetValue(TW_ACTION_BUSY, 1);
272 DataManager::SetValue("ui_progress", 0);
273 DataManager::SetValue("tw_operation", operation_name);
274 DataManager::SetValue("tw_operation_status", 0);
275 DataManager::SetValue("tw_operation_state", 0);
276}
277
278void GUIAction::operation_end(const int operation_status, const int simulate)
279{
280 int simulate_fail;
281
282 DataManager::SetValue("ui_progress", 100);
283 if (simulate) {
284 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
285 if (simulate_fail != 0)
286 DataManager::SetValue("tw_operation_status", 1);
287 else
288 DataManager::SetValue("tw_operation_status", 0);
289 } else {
290 if (operation_status != 0)
291 DataManager::SetValue("tw_operation_status", 1);
292 else
293 DataManager::SetValue("tw_operation_status", 0);
294 }
295 DataManager::SetValue("tw_operation_state", 1);
296 DataManager::SetValue(TW_ACTION_BUSY, 0);
297}
298
299int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
300{
301 static string zip_queue[10];
302 static int zip_queue_index;
303 static pthread_t terminal_command;
304 int simulate;
305
306 std::string arg = gui_parse_text(action.mArg);
307
308 std::string function = gui_parse_text(action.mFunction);
309
310 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
311
312 if (function == "reboot")
313 {
314 //curtainClose(); this sometimes causes a crash
315
316 sync();
317
318 if (arg == "recovery")
319 tw_reboot(rb_recovery);
320 else if (arg == "poweroff")
321 tw_reboot(rb_poweroff);
322 else if (arg == "bootloader")
323 tw_reboot(rb_bootloader);
324 else if (arg == "download")
325 tw_reboot(rb_download);
326 else
327 tw_reboot(rb_system);
328
329 // This should never occur
330 return -1;
331 }
332 if (function == "home")
333 {
334 PageManager::SelectPackage("TWRP");
335 gui_changePage("main");
336 return 0;
337 }
338
339 if (function == "key")
340 {
341 PageManager::NotifyKey(getKeyByName(arg));
342 return 0;
343 }
344
345 if (function == "page") {
346 std::string page_name = gui_parse_text(arg);
347 return gui_changePage(page_name);
348 }
349
350 if (function == "reload") {
351 int check = 0, ret_val = 0;
352 std::string theme_path;
353
354 operation_start("Reload Theme");
355 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
358 check = 1;
359 }
360
361 theme_path += "/TWRP/theme/ui.zip";
362 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
363 {
364 // Loading the custom theme failed - try loading the stock theme
365 LOGI("Attempting to reload stock theme...\n");
366 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
367 {
368 LOGE("Failed to load base packages.\n");
369 ret_val = 1;
370 }
371 }
372 operation_end(ret_val, simulate);
373 }
374
375 if (function == "readBackup")
376 {
377 string Restore_Name;
378 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400380 return 0;
381 }
382
383 if (function == "set")
384 {
385 if (arg.find('=') != string::npos)
386 {
387 string varName = arg.substr(0, arg.find('='));
388 string value = arg.substr(arg.find('=') + 1, string::npos);
389
390 DataManager::GetValue(value, value);
391 DataManager::SetValue(varName, value);
392 }
393 else
394 DataManager::SetValue(arg, "1");
395 return 0;
396 }
397 if (function == "clear")
398 {
399 DataManager::SetValue(arg, "0");
400 return 0;
401 }
402
403 if (function == "mount")
404 {
405 if (arg == "usb")
406 {
407 DataManager::SetValue(TW_ACTION_BUSY, 1);
408 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400409 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 else
411 ui_print("Simulating actions...\n");
412 }
413 else if (!simulate)
414 {
415 string cmd;
416 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400417 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400419 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 else
Dees_Troy51127312012-09-08 13:08:49 -0400421 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 } else
423 ui_print("Simulating actions...\n");
424 return 0;
425 }
426
427 if (function == "umount" || function == "unmount")
428 {
429 if (arg == "usb")
430 {
431 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400432 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400433 else
434 ui_print("Simulating actions...\n");
435 DataManager::SetValue(TW_ACTION_BUSY, 0);
436 }
437 else if (!simulate)
438 {
439 string cmd;
440 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400441 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400443 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 else
Dees_Troy51127312012-09-08 13:08:49 -0400445 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 } else
447 ui_print("Simulating actions...\n");
448 return 0;
449 }
450
451 if (function == "restoredefaultsettings")
452 {
453 operation_start("Restore Defaults");
454 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
455 ui_print("Simulating actions...\n");
456 else {
457 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 PartitionManager.Update_System_Details();
459 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460 }
461 operation_end(0, simulate);
462 }
463
464 if (function == "copylog")
465 {
466 operation_start("Copy Log");
467 if (!simulate)
468 {
469 char command[255];
470
Dees_Troy5bf43922012-09-07 16:07:55 -0400471 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400472 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400473 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 sync();
475 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
476 } else
477 simulate_progress_bar();
478 operation_end(0, simulate);
479 return 0;
480 }
481
482 if (function == "compute" || function == "addsubtract")
483 {
484 if (arg.find("+") != string::npos)
485 {
486 string varName = arg.substr(0, arg.find('+'));
487 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
488 int amount_to_add = atoi(string_to_add.c_str());
489 int value;
490
491 DataManager::GetValue(varName, value);
492 DataManager::SetValue(varName, value + amount_to_add);
493 return 0;
494 }
495 if (arg.find("-") != string::npos)
496 {
497 string varName = arg.substr(0, arg.find('-'));
498 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
499 int amount_to_subtract = atoi(string_to_subtract.c_str());
500 int value;
501
502 DataManager::GetValue(varName, value);
503 value -= amount_to_subtract;
504 if (value <= 0)
505 value = 0;
506 DataManager::SetValue(varName, value);
507 return 0;
508 }
509 }
510
511 if (function == "setguitimezone")
512 {
513 string SelectedZone;
514 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
515 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
516 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
517
518 int dst;
519 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
520
521 string offset;
522 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
523
524 string NewTimeZone = Zone;
525 if (offset != "0")
526 NewTimeZone += ":" + offset;
527
528 if (dst != 0)
529 NewTimeZone += DSTZone;
530
531 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400532 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400533 return 0;
534 }
535
536 if (function == "togglestorage") {
537 if (arg == "internal") {
538 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
539 } else if (arg == "external") {
540 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
541 }
Dees_Troy51127312012-09-08 13:08:49 -0400542 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400543 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400544 string zip_path, zip_root;
545 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
546 zip_root = TWFunc::Get_Root_Path(zip_path);
547#ifdef RECOVERY_SDCARD_ON_DATA
548 #ifndef TW_EXTERNAL_STORAGE_PATH
549 if (zip_root != "/sdcard")
550 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
551 #else
552 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
553 if (zip_root != "/emmc")
554 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
555 } else {
556 if (zip_root != "/sdcard")
557 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
558 }
559 #endif
560#else
561 if (zip_root != DataManager::GetCurrentStoragePath())
562 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
563#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 // Save the current zip location to the external variable
565 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
566 // Change the current zip location to the internal variable
567 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
568 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400569 string zip_path, zip_root;
570 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
571 zip_root = TWFunc::Get_Root_Path(zip_path);
572 if (zip_root != DataManager::GetCurrentStoragePath()) {
573 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
574 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400575 // Save the current zip location to the internal variable
576 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
577 // Change the current zip location to the external variable
578 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
579 }
580 } else {
581 // We weren't able to toggle for some reason, restore original setting
582 if (arg == "internal") {
583 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
584 } else if (arg == "external") {
585 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
586 }
587 }
588 return 0;
589 }
590
591 if (function == "overlay")
592 return gui_changeOverlay(arg);
593
594 if (function == "queuezip")
595 {
596 if (zip_queue_index >= 10) {
597 ui_print("Maximum zip queue reached!\n");
598 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")
609 {
610 if (zip_queue_index <= 0) {
611 ui_print("Minimum zip queue reached!\n");
612 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
635 if (isThreaded)
636 {
637 if (function == "fileexists")
638 {
639 struct stat st;
640 string newpath = arg + "/.";
641
642 operation_start("FileExists");
643 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
644 operation_end(0, simulate);
645 else
646 operation_end(1, simulate);
647 }
648
649 if (function == "flash")
650 {
Dees_Troy657c3092012-09-10 20:32:10 -0400651 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400652
653 for (i=0; i<zip_queue_index; i++) {
654 operation_start("Flashing");
655 DataManager::SetValue("tw_filename", zip_queue[i]);
656 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
657
Dees_Troy657c3092012-09-10 20:32:10 -0400658 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 if (ret_val != 0) {
660 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
661 i = 10; // Error flashing zip - exit queue
662 ret_val = 1;
663 }
664 }
665 zip_queue_index = 0;
666 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
667
Dees_Troy657c3092012-09-10 20:32:10 -0400668 if (wipe_cache)
669 PartitionManager.Wipe_By_Path("/cache");
670
Dees_Troy51a0e822012-09-05 15:24:24 -0400671 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
672 operation_start("ReinjectTWRP");
673 ui_print("Injecting TWRP into boot image...\n");
674 if (simulate) {
675 simulate_progress_bar();
676 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400677 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400678 ui_print("TWRP injection complete.\n");
679 }
680 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400681 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400682 operation_end(ret_val, simulate);
683 return 0;
684 }
685 if (function == "wipe")
686 {
687 operation_start("Format");
688 DataManager::SetValue("tw_partition", arg);
689
Dees_Troy38bd7602012-09-14 13:33:53 -0400690 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400691
692 if (simulate) {
693 simulate_progress_bar();
694 } else {
695 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400696 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400698 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400700 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400702 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400704 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 } else if (arg == "INTERNAL") {
706 int has_datamedia, dual_storage;
707
708 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
709 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400710 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400712 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 }
714 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400715 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400716
Dees_Troy38bd7602012-09-14 13:33:53 -0400717 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
718 ret_val = PartitionManager.Wipe_By_Path(External_Path);
719 } else
720 ret_val = PartitionManager.Wipe_By_Path(arg);
721
722 if (arg == DataManager::GetSettingsStoragePath()) {
723 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
724 string Storage_Path = DataManager::GetSettingsStoragePath();
725
726 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
727 LOGI("Making TWRP folder and saving settings.\n");
728 Storage_Path += "/TWRP";
729 mkdir(Storage_Path.c_str(), 0777);
730 DataManager::Flush();
731 } else {
732 LOGE("Unable to recreate TWRP folder and save settings.\n");
733 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400734 }
735 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400736 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 if (ret_val)
738 ret_val = 0; // 0 is success
739 else
740 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400741 operation_end(ret_val, simulate);
742 return 0;
743 }
744 if (function == "refreshsizes")
745 {
746 operation_start("Refreshing Sizes");
747 if (simulate) {
748 simulate_progress_bar();
749 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400750 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 operation_end(0, simulate);
752 }
753 if (function == "nandroid")
754 {
755 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400756 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400757
758 if (simulate) {
759 DataManager::SetValue("tw_partition", "Simulation");
760 simulate_progress_bar();
761 } else {
762 if (arg == "backup") {
763 string Backup_Name;
764 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400765 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1) == 0)
766 ret = PartitionManager.Run_Backup();
767 else {
768 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400770 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400771 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
772 } else if (arg == "restore") {
773 string Restore_Name;
774 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400775 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 } else {
777 operation_end(1, simulate);
778 return -1;
779 }
780 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400781 if (ret == false)
782 ret = 1; // 1 for failure
783 else
784 ret = 0; // 0 for success
785 operation_end(ret, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 return 0;
787 }
788 if (function == "fixpermissions")
789 {
790 operation_start("Fix Permissions");
791 LOGI("fix permissions started!\n");
792 if (simulate) {
793 simulate_progress_bar();
794 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400795 PartitionManager.Fix_Permissions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400796
797 LOGI("fix permissions DONE!\n");
798 operation_end(0, simulate);
799 return 0;
800 }
801 if (function == "dd")
802 {
803 operation_start("imaging");
804
805 if (simulate) {
806 simulate_progress_bar();
807 } else {
808 char cmd[512];
809 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400810 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 }
812 operation_end(0, simulate);
813 return 0;
814 }
815 if (function == "partitionsd")
816 {
817 operation_start("Partition SD Card");
818
819 if (simulate) {
820 simulate_progress_bar();
821 } else {
822 int allow_partition;
823 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
824 if (allow_partition == 0) {
825 ui_print("This device does not have a real SD Card!\nAborting!\n");
826 } else {
827 // Below seen in Koush's recovery
828 char sddevice[256];
829 char mkdir_path[255];
830 Volume *vol = volume_for_path("/sdcard");
831 strcpy(sddevice, vol->device);
832 // Just need block not whole partition
Dees_Troy8170a922012-09-18 15:40:25 -0400833 sddevice[strlen("/dev/block/mmcblkX")] = '\0';
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
835 char es[64];
836 std::string ext_format, sd_path;
837 int ext, swap;
838 DataManager::GetValue("tw_sdext_size", ext);
839 DataManager::GetValue("tw_swap_size", swap);
840 DataManager::GetValue("tw_sdpart_file_system", ext_format);
841 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
842 LOGI("\nrunning script: %s\n", es);
843 run_script("\nContinue partitioning?",
844 "\nPartitioning sdcard : ",
845 es,
846 "\nunable to execute parted!\n(%s)\n",
847 "\nOops... something went wrong!\nPlease check the recovery log!\n",
848 "\nPartitioning complete!\n\n",
849 "\nPartitioning aborted!\n\n", 0);
850
851 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
852#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy5bf43922012-09-07 16:07:55 -0400853 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400854 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
855 memset(mkdir_path, 0, sizeof(mkdir_path));
856 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
857#else
Dees_Troy5bf43922012-09-07 16:07:55 -0400858 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400859 strcpy(mkdir_path, "/sdcard/TWRP");
860#endif
861 mkdir(mkdir_path, 0777);
862 DataManager::Flush();
863#ifdef TW_EXTERNAL_STORAGE_PATH
864 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
865 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
866 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
867#else
868 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
869 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
870 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
871#endif
872 // This is sometimes needed to make a healthy ext4 partition
873 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
874 char command[256];
875 LOGE("Fix this format command!\n");
876 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
877 ui_print("Formatting sd-ext as ext4...\n");
878 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
Dees_Troy8170a922012-09-18 15:40:25 -0400879 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400880 ui_print("DONE\n");
881 }
882
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400884 }
885 }
886 operation_end(0, simulate);
887 return 0;
888 }
889 if (function == "installhtcdumlock")
890 {
891 operation_start("Install HTC Dumlock");
892 if (simulate) {
893 simulate_progress_bar();
894 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400895 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400896
897 operation_end(0, simulate);
898 return 0;
899 }
900 if (function == "htcdumlockrestoreboot")
901 {
902 operation_start("HTC Dumlock Restore Boot");
903 if (simulate) {
904 simulate_progress_bar();
905 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400906 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400907
908 operation_end(0, simulate);
909 return 0;
910 }
911 if (function == "htcdumlockreflashrecovery")
912 {
913 operation_start("HTC Dumlock Reflash Recovery");
914 if (simulate) {
915 simulate_progress_bar();
916 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400917 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400918
919 operation_end(0, simulate);
920 return 0;
921 }
922 if (function == "cmd")
923 {
924 int op_status = 0;
925
926 operation_start("Command");
927 LOGI("Running command: '%s'\n", arg.c_str());
928 if (simulate) {
929 simulate_progress_bar();
930 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400931 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400932 if (op_status != 0)
933 op_status = 1;
934 }
935
936 operation_end(op_status, simulate);
937 return 0;
938 }
939 if (function == "terminalcommand")
940 {
941 int op_status = 0;
942 string cmdpath, command;
943
944 DataManager::GetValue("tw_terminal_location", cmdpath);
945 operation_start("CommandOutput");
946 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
947 if (simulate) {
948 simulate_progress_bar();
949 operation_end(op_status, simulate);
950 } else {
951 command = "cd \"";
952 command += cmdpath;
953 command += "\" && ";
954 command += arg;
955 LOGI("Actual command is: '%s'\n", command.c_str());
956 DataManager::SetValue("tw_terminal_command_thread", command);
957 DataManager::SetValue("tw_terminal_state", 1);
958 DataManager::SetValue("tw_background_thread_running", 1);
959 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
960 if (op_status != 0) {
961 LOGE("Error starting terminal command thread, %i.\n", op_status);
962 DataManager::SetValue("tw_terminal_state", 0);
963 DataManager::SetValue("tw_background_thread_running", 0);
964 operation_end(1, simulate);
965 }
966 }
967 return 0;
968 }
969 if (function == "killterminal")
970 {
971 int op_status = 0;
972
973 LOGI("Sending kill command...\n");
974 operation_start("KillCommand");
975 DataManager::SetValue("tw_operation_status", 0);
976 DataManager::SetValue("tw_operation_state", 1);
977 DataManager::SetValue("tw_terminal_state", 0);
978 DataManager::SetValue("tw_background_thread_running", 0);
979 DataManager::SetValue(TW_ACTION_BUSY, 0);
980 return 0;
981 }
982 if (function == "reinjecttwrp")
983 {
984 int op_status = 0;
985
986 operation_start("ReinjectTWRP");
987 ui_print("Injecting TWRP into boot image...\n");
988 if (simulate) {
989 simulate_progress_bar();
990 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400991 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400992 ui_print("TWRP injection complete.\n");
993 }
994
995 operation_end(op_status, simulate);
996 return 0;
997 }
998 if (function == "checkbackupname")
999 {
1000 int op_status = 0;
1001
1002 operation_start("CheckBackupName");
1003 if (simulate) {
1004 simulate_progress_bar();
1005 } else {
1006 op_status = check_backup_name(1);
1007 if (op_status != 0)
1008 op_status = 1;
1009 }
1010
1011 operation_end(op_status, simulate);
1012 return 0;
1013 }
1014 if (function == "decrypt")
1015 {
1016 int op_status = 0;
1017
1018 operation_start("Decrypt");
1019 if (simulate) {
1020 simulate_progress_bar();
1021 } else {
1022 string Password;
1023 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001024 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001025 if (op_status != 0)
1026 op_status = 1;
1027 else {
1028 int load_theme = 1;
1029
1030 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1031 DataManager::ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -04001032 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001033 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -04001034 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001035 usleep(2000000); // Sleep for 2 seconds before rebooting
1036 tw_reboot(rb_system);
1037 load_theme = 0;
1038 }
1039 }
1040
1041 if (load_theme) {
1042 int has_datamedia;
1043
1044 // Check for a custom theme and load it if exists
1045 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1046 if (has_datamedia != 0) {
1047 struct stat st;
1048 int check = 0;
1049 std::string theme_path;
1050
1051 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001052 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1054 check = 1;
1055 }
1056
1057 theme_path += "/TWRP/theme/ui.zip";
1058 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1059 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1060 {
1061 // Loading the custom theme failed - try loading the stock theme
1062 LOGI("Attempting to reload stock theme...\n");
1063 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1064 {
1065 LOGE("Failed to load base packages.\n");
1066 }
1067 }
1068 }
1069 }
1070 }
1071 }
1072 }
1073
1074 operation_end(op_status, simulate);
1075 return 0;
1076 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001077 if (function == "adbsideload")
1078 {
1079 int ret = 0;
1080
1081 operation_start("Sideload");
1082 if (simulate) {
1083 simulate_progress_bar();
1084 } else {
1085 int wipe_cache = 0;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001086 string Command, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001087
1088 if (!PartitionManager.Mount_Current_Storage(true)) {
1089 operation_end(1, simulate);
1090 return 0;
1091 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001092 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1093 if (TWFunc::Path_Exists(Sideload_File)) {
1094 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001095 system(Command.c_str());
1096 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001097 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001098 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001099 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001100 ret = 1; // failure
1101 else if (wipe_cache)
1102 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy43d8b002012-09-17 16:00:01 -04001103 }
1104 operation_end(ret, simulate);
1105 return 0;
1106 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001107 if (function == "adbsideloadcancel")
1108 {
1109 int child_pid;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001110 string Command, Sideload_File;
1111 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1112 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001113 system(Command.c_str());
1114 DataManager::GetValue("tw_child_pid", child_pid);
1115 ui_print("Cancelling ADB sideload...\n");
1116 kill(child_pid, SIGTERM);
1117 return 0;
1118 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001119 }
1120 else
1121 {
1122 pthread_t t;
1123 pthread_create(&t, NULL, thread_start, this);
1124 return 0;
1125 }
1126 return -1;
1127}
1128
1129int GUIAction::getKeyByName(std::string key)
1130{
1131 if (key == "home") return KEY_HOME;
1132 else if (key == "menu") return KEY_MENU;
1133 else if (key == "back") return KEY_BACK;
1134 else if (key == "search") return KEY_SEARCH;
1135 else if (key == "voldown") return KEY_VOLUMEDOWN;
1136 else if (key == "volup") return KEY_VOLUMEUP;
1137 else if (key == "power") {
1138 int ret_val;
1139 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1140 if (!ret_val)
1141 return KEY_POWER;
1142 else
1143 return ret_val;
1144 }
1145
1146 return atol(key.c_str());
1147}
1148
1149void* GUIAction::command_thread(void *cookie)
1150{
1151 string command;
1152 FILE* fp;
1153 char line[512];
1154
1155 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001156 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001157 if (fp == NULL) {
1158 LOGE("Error opening command to run.\n");
1159 } else {
1160 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1161 struct timeval timeout;
1162 fd_set fdset;
1163
1164 while(keep_going)
1165 {
1166 FD_ZERO(&fdset);
1167 FD_SET(fd, &fdset);
1168 timeout.tv_sec = 0;
1169 timeout.tv_usec = 400000;
1170 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1171 if (has_data == 0) {
1172 // Timeout reached
1173 DataManager::GetValue("tw_terminal_state", check);
1174 if (check == 0) {
1175 keep_going = 0;
1176 }
1177 } else if (has_data < 0) {
1178 // End of execution
1179 keep_going = 0;
1180 } else {
1181 // Try to read output
1182 bytes_read = read(fd, line, sizeof(line));
1183 if (bytes_read > 0)
1184 ui_print("%s", line); // Display output
1185 else
1186 keep_going = 0; // Done executing
1187 }
1188 }
1189 fclose(fp);
1190 }
1191 DataManager::SetValue("tw_operation_status", 0);
1192 DataManager::SetValue("tw_operation_state", 1);
1193 DataManager::SetValue("tw_terminal_state", 0);
1194 DataManager::SetValue("tw_background_thread_running", 0);
1195 DataManager::SetValue(TW_ACTION_BUSY, 0);
1196 return NULL;
1197}