blob: bbcca23f5e446d01c699eedfafef007cd19859b1 [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_Troy51a0e822012-09-05 15:24:24 -040023
Dees_Troy43d8b002012-09-17 16:00:01 -040024#include "../ui.h"
25#include "../adb_install.h"
26
Dees_Troy51a0e822012-09-05 15:24:24 -040027extern "C" {
28#include "../common.h"
29#include "../roots.h"
30#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);
718 } else
719 ret_val = PartitionManager.Wipe_By_Path(arg);
720
721 if (arg == DataManager::GetSettingsStoragePath()) {
722 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
723 string Storage_Path = DataManager::GetSettingsStoragePath();
724
725 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
726 LOGI("Making TWRP folder and saving settings.\n");
727 Storage_Path += "/TWRP";
728 mkdir(Storage_Path.c_str(), 0777);
729 DataManager::Flush();
730 } else {
731 LOGE("Unable to recreate TWRP folder and save settings.\n");
732 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 }
734 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400735 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400736 if (ret_val)
737 ret_val = 0; // 0 is success
738 else
739 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 operation_end(ret_val, simulate);
741 return 0;
742 }
743 if (function == "refreshsizes")
744 {
745 operation_start("Refreshing Sizes");
746 if (simulate) {
747 simulate_progress_bar();
748 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400749 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 operation_end(0, simulate);
751 }
752 if (function == "nandroid")
753 {
754 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400755 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400756
757 if (simulate) {
758 DataManager::SetValue("tw_partition", "Simulation");
759 simulate_progress_bar();
760 } else {
761 if (arg == "backup") {
762 string Backup_Name;
763 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400764 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1) == 0)
765 ret = PartitionManager.Run_Backup();
766 else {
767 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400769 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
771 } else if (arg == "restore") {
772 string Restore_Name;
773 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400774 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400775 } else {
776 operation_end(1, simulate);
777 return -1;
778 }
779 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400780 if (ret == false)
781 ret = 1; // 1 for failure
782 else
783 ret = 0; // 0 for success
784 operation_end(ret, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400785 return 0;
786 }
787 if (function == "fixpermissions")
788 {
789 operation_start("Fix Permissions");
790 LOGI("fix permissions started!\n");
791 if (simulate) {
792 simulate_progress_bar();
793 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400794 PartitionManager.Fix_Permissions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400795
796 LOGI("fix permissions DONE!\n");
797 operation_end(0, simulate);
798 return 0;
799 }
800 if (function == "dd")
801 {
802 operation_start("imaging");
803
804 if (simulate) {
805 simulate_progress_bar();
806 } else {
807 char cmd[512];
808 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400809 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 }
811 operation_end(0, simulate);
812 return 0;
813 }
814 if (function == "partitionsd")
815 {
816 operation_start("Partition SD Card");
817
818 if (simulate) {
819 simulate_progress_bar();
820 } else {
821 int allow_partition;
822 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
823 if (allow_partition == 0) {
824 ui_print("This device does not have a real SD Card!\nAborting!\n");
825 } else {
826 // Below seen in Koush's recovery
827 char sddevice[256];
828 char mkdir_path[255];
829 Volume *vol = volume_for_path("/sdcard");
830 strcpy(sddevice, vol->device);
831 // Just need block not whole partition
Dees_Troy8170a922012-09-18 15:40:25 -0400832 sddevice[strlen("/dev/block/mmcblkX")] = '\0';
Dees_Troy51a0e822012-09-05 15:24:24 -0400833
834 char es[64];
835 std::string ext_format, sd_path;
836 int ext, swap;
837 DataManager::GetValue("tw_sdext_size", ext);
838 DataManager::GetValue("tw_swap_size", swap);
839 DataManager::GetValue("tw_sdpart_file_system", ext_format);
840 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
841 LOGI("\nrunning script: %s\n", es);
842 run_script("\nContinue partitioning?",
843 "\nPartitioning sdcard : ",
844 es,
845 "\nunable to execute parted!\n(%s)\n",
846 "\nOops... something went wrong!\nPlease check the recovery log!\n",
847 "\nPartitioning complete!\n\n",
848 "\nPartitioning aborted!\n\n", 0);
849
850 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
851#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy5bf43922012-09-07 16:07:55 -0400852 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
854 memset(mkdir_path, 0, sizeof(mkdir_path));
855 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
856#else
Dees_Troy5bf43922012-09-07 16:07:55 -0400857 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400858 strcpy(mkdir_path, "/sdcard/TWRP");
859#endif
860 mkdir(mkdir_path, 0777);
861 DataManager::Flush();
862#ifdef TW_EXTERNAL_STORAGE_PATH
863 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
864 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
865 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
866#else
867 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
868 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
869 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
870#endif
871 // This is sometimes needed to make a healthy ext4 partition
872 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
873 char command[256];
874 LOGE("Fix this format command!\n");
875 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
876 ui_print("Formatting sd-ext as ext4...\n");
877 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
Dees_Troy8170a922012-09-18 15:40:25 -0400878 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400879 ui_print("DONE\n");
880 }
881
Dees_Troy5bf43922012-09-07 16:07:55 -0400882 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400883 }
884 }
885 operation_end(0, simulate);
886 return 0;
887 }
888 if (function == "installhtcdumlock")
889 {
890 operation_start("Install HTC Dumlock");
891 if (simulate) {
892 simulate_progress_bar();
893 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400895
896 operation_end(0, simulate);
897 return 0;
898 }
899 if (function == "htcdumlockrestoreboot")
900 {
901 operation_start("HTC Dumlock Restore Boot");
902 if (simulate) {
903 simulate_progress_bar();
904 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400905 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400906
907 operation_end(0, simulate);
908 return 0;
909 }
910 if (function == "htcdumlockreflashrecovery")
911 {
912 operation_start("HTC Dumlock Reflash Recovery");
913 if (simulate) {
914 simulate_progress_bar();
915 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400916 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400917
918 operation_end(0, simulate);
919 return 0;
920 }
921 if (function == "cmd")
922 {
923 int op_status = 0;
924
925 operation_start("Command");
926 LOGI("Running command: '%s'\n", arg.c_str());
927 if (simulate) {
928 simulate_progress_bar();
929 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400930 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400931 if (op_status != 0)
932 op_status = 1;
933 }
934
935 operation_end(op_status, simulate);
936 return 0;
937 }
938 if (function == "terminalcommand")
939 {
940 int op_status = 0;
941 string cmdpath, command;
942
943 DataManager::GetValue("tw_terminal_location", cmdpath);
944 operation_start("CommandOutput");
945 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
946 if (simulate) {
947 simulate_progress_bar();
948 operation_end(op_status, simulate);
949 } else {
950 command = "cd \"";
951 command += cmdpath;
952 command += "\" && ";
953 command += arg;
954 LOGI("Actual command is: '%s'\n", command.c_str());
955 DataManager::SetValue("tw_terminal_command_thread", command);
956 DataManager::SetValue("tw_terminal_state", 1);
957 DataManager::SetValue("tw_background_thread_running", 1);
958 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
959 if (op_status != 0) {
960 LOGE("Error starting terminal command thread, %i.\n", op_status);
961 DataManager::SetValue("tw_terminal_state", 0);
962 DataManager::SetValue("tw_background_thread_running", 0);
963 operation_end(1, simulate);
964 }
965 }
966 return 0;
967 }
968 if (function == "killterminal")
969 {
970 int op_status = 0;
971
972 LOGI("Sending kill command...\n");
973 operation_start("KillCommand");
974 DataManager::SetValue("tw_operation_status", 0);
975 DataManager::SetValue("tw_operation_state", 1);
976 DataManager::SetValue("tw_terminal_state", 0);
977 DataManager::SetValue("tw_background_thread_running", 0);
978 DataManager::SetValue(TW_ACTION_BUSY, 0);
979 return 0;
980 }
981 if (function == "reinjecttwrp")
982 {
983 int op_status = 0;
984
985 operation_start("ReinjectTWRP");
986 ui_print("Injecting TWRP into boot image...\n");
987 if (simulate) {
988 simulate_progress_bar();
989 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400990 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400991 ui_print("TWRP injection complete.\n");
992 }
993
994 operation_end(op_status, simulate);
995 return 0;
996 }
997 if (function == "checkbackupname")
998 {
999 int op_status = 0;
1000
1001 operation_start("CheckBackupName");
1002 if (simulate) {
1003 simulate_progress_bar();
1004 } else {
1005 op_status = check_backup_name(1);
1006 if (op_status != 0)
1007 op_status = 1;
1008 }
1009
1010 operation_end(op_status, simulate);
1011 return 0;
1012 }
1013 if (function == "decrypt")
1014 {
1015 int op_status = 0;
1016
1017 operation_start("Decrypt");
1018 if (simulate) {
1019 simulate_progress_bar();
1020 } else {
1021 string Password;
1022 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001023 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001024 if (op_status != 0)
1025 op_status = 1;
1026 else {
1027 int load_theme = 1;
1028
1029 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1030 DataManager::ReadSettingsFile();
Dees_Troy7d15c252012-09-05 20:47:21 -04001031LOGE("TODO: Implement ORS support\n");
1032 if (0/*check_for_script_file()*/) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001033 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy7d15c252012-09-05 20:47:21 -04001034 if (/*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}