Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 17 | #include <sys/wait.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | #include <sstream> |
| 21 | #include "../partitions.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 22 | #include "../twrp-functions.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 23 | #include "../openrecoveryscript.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 24 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 25 | #include "../ui.h" |
| 26 | #include "../adb_install.h" |
| 27 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 28 | extern "C" { |
| 29 | #include "../common.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 30 | #include "../minuitwrp/minui.h" |
| 31 | #include "../recovery_ui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 32 | #include "../variables.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 33 | #include "../twinstall.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 35 | #include "../minadbd/adb.h" |
| 36 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 37 | int TWinstall_zip(const char* path, int* wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | void 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | int gui_console_only(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | int gui_start(); |
| 41 | }; |
| 42 | |
| 43 | #include "rapidxml.hpp" |
| 44 | #include "objects.hpp" |
| 45 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 46 | extern RecoveryUI* ui; |
| 47 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | void curtainClose(void); |
| 49 | |
| 50 | GUIAction::GUIAction(xml_node<>* node) |
| 51 | : Conditional(node) |
| 52 | { |
| 53 | xml_node<>* child; |
| 54 | xml_node<>* actions; |
| 55 | xml_attribute<>* attr; |
| 56 | |
| 57 | mKey = 0; |
| 58 | |
| 59 | if (!node) return; |
| 60 | |
| 61 | // First, get the action |
| 62 | actions = node->first_node("actions"); |
| 63 | if (actions) child = actions->first_node("action"); |
| 64 | else child = node->first_node("action"); |
| 65 | |
| 66 | if (!child) return; |
| 67 | |
| 68 | while (child) |
| 69 | { |
| 70 | Action action; |
| 71 | |
| 72 | attr = child->first_attribute("function"); |
| 73 | if (!attr) return; |
| 74 | |
| 75 | action.mFunction = attr->value(); |
| 76 | action.mArg = child->value(); |
| 77 | mActions.push_back(action); |
| 78 | |
| 79 | child = child->next_sibling("action"); |
| 80 | } |
| 81 | |
| 82 | // Now, let's get either the key or region |
| 83 | child = node->first_node("touch"); |
| 84 | if (child) |
| 85 | { |
| 86 | attr = child->first_attribute("key"); |
| 87 | if (attr) |
| 88 | { |
| 89 | std::string key = attr->value(); |
| 90 | |
| 91 | mKey = getKeyByName(key); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | attr = child->first_attribute("x"); |
| 96 | if (!attr) return; |
| 97 | mActionX = atol(attr->value()); |
| 98 | attr = child->first_attribute("y"); |
| 99 | if (!attr) return; |
| 100 | mActionY = atol(attr->value()); |
| 101 | attr = child->first_attribute("w"); |
| 102 | if (!attr) return; |
| 103 | mActionW = atol(attr->value()); |
| 104 | attr = child->first_attribute("h"); |
| 105 | if (!attr) return; |
| 106 | mActionH = atol(attr->value()); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 112 | { |
| 113 | if (state == TOUCH_RELEASE) |
| 114 | doActions(); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int GUIAction::NotifyKey(int key) |
| 120 | { |
| 121 | if (!mKey || key != mKey) |
| 122 | return 1; |
| 123 | |
| 124 | doActions(); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int GUIAction::NotifyVarChange(std::string varName, std::string value) |
| 129 | { |
| 130 | if (varName.empty() && !isConditionValid() && !mKey && !mActionW) |
| 131 | doActions(); |
| 132 | |
| 133 | // This handles notifying the condition system of page start |
| 134 | if (varName.empty() && isConditionValid()) |
| 135 | NotifyPageSet(); |
| 136 | |
| 137 | if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue()) |
| 138 | doActions(); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | void GUIAction::simulate_progress_bar(void) |
| 144 | { |
| 145 | ui_print("Simulating actions...\n"); |
| 146 | for (int i = 0; i < 5; i++) |
| 147 | { |
| 148 | usleep(500000); |
| 149 | DataManager::SetValue("ui_progress", i * 20); |
| 150 | } |
| 151 | } |
| 152 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 153 | int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 154 | { |
| 155 | int ret_val = 0; |
| 156 | |
| 157 | DataManager::SetValue("ui_progress", 0); |
| 158 | |
| 159 | if (filename.empty()) |
| 160 | { |
| 161 | LOGE("No file specified.\n"); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | // We're going to jump to this page first, like a loading page |
| 166 | gui_changePage(pageName); |
| 167 | |
| 168 | int fd = -1; |
| 169 | ZipArchive zip; |
| 170 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 171 | if (!PartitionManager.Mount_By_Path(filename, true)) |
| 172 | return -1; |
| 173 | |
| 174 | if (mzOpenZipArchive(filename.c_str(), &zip)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | { |
| 176 | LOGE("Unable to open zip file.\n"); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | // Check the zip to see if it has a custom installer theme |
| 181 | const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip"); |
| 182 | if (twrp != NULL) |
| 183 | { |
| 184 | unlink("/tmp/twrp.zip"); |
| 185 | fd = creat("/tmp/twrp.zip", 0666); |
| 186 | } |
| 187 | if (fd >= 0 && twrp != NULL && |
| 188 | mzExtractZipEntryToFile(&zip, twrp, fd) && |
| 189 | !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main")) |
| 190 | { |
| 191 | mzCloseZipArchive(&zip); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 192 | PageManager::SelectPackage("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 193 | gui_changePage("main"); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | // In this case, we just use the default page |
| 198 | mzCloseZipArchive(&zip); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 199 | gui_changePage(pageName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 200 | } |
| 201 | if (fd >= 0) |
| 202 | close(fd); |
| 203 | |
| 204 | if (simulate) { |
| 205 | simulate_progress_bar(); |
| 206 | } else { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 207 | ret_val = TWinstall_zip(filename.c_str(), wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 208 | |
| 209 | // Now, check if we need to ensure TWRP remains installed... |
| 210 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 211 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | if (stat("/sbin/installTwrp", &st) == 0) |
| 213 | { |
| 214 | DataManager::SetValue("tw_operation", "Configuring TWRP"); |
| 215 | DataManager::SetValue("tw_partition", ""); |
| 216 | ui_print("Configuring TWRP...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 217 | if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 218 | { |
| 219 | ui_print("Unable to configure TWRP with this kernel.\n"); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Done |
| 225 | DataManager::SetValue("ui_progress", 100); |
| 226 | DataManager::SetValue("ui_progress", 0); |
| 227 | return ret_val; |
| 228 | } |
| 229 | |
| 230 | int GUIAction::doActions() |
| 231 | { |
| 232 | if (mActions.size() < 1) return -1; |
| 233 | if (mActions.size() == 1) |
| 234 | return doAction(mActions.at(0), 0); |
| 235 | |
| 236 | // For multi-action, we always use a thread |
| 237 | pthread_t t; |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 238 | pthread_attr_t tattr; |
| 239 | |
| 240 | if (pthread_attr_init(&tattr)) { |
| 241 | LOGE("Unable to pthread_attr_init\n"); |
| 242 | return -1; |
| 243 | } |
| 244 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
| 245 | LOGE("Error setting pthread_attr_setdetachstate\n"); |
| 246 | return -1; |
| 247 | } |
| 248 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
| 249 | LOGE("Error setting pthread_attr_setscope\n"); |
| 250 | return -1; |
| 251 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 252 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 253 | LOGE("Error setting pthread_attr_setstacksize\n"); |
| 254 | return -1; |
| 255 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 256 | */ |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 257 | int ret = pthread_create(&t, &tattr, thread_start, this); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 258 | if (ret) { |
| 259 | LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret); |
| 260 | thread_start(this); |
| 261 | } else { |
| 262 | if (pthread_join(t, NULL)) { |
| 263 | LOGE("Error joining threads\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | if (pthread_attr_destroy(&tattr)) { |
| 267 | LOGE("Failed to pthread_attr_destroy\n"); |
| 268 | return -1; |
| 269 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | void* GUIAction::thread_start(void *cookie) |
| 275 | { |
| 276 | GUIAction* ourThis = (GUIAction*) cookie; |
| 277 | |
| 278 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 279 | |
| 280 | if (ourThis->mActions.size() > 1) |
| 281 | { |
| 282 | std::vector<Action>::iterator iter; |
| 283 | for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++) |
| 284 | ourThis->doAction(*iter, 1); |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | ourThis->doAction(ourThis->mActions.at(0), 1); |
| 289 | } |
| 290 | int check = 0; |
| 291 | DataManager::GetValue("tw_background_thread_running", check); |
| 292 | if (check == 0) |
| 293 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | void GUIAction::operation_start(const string operation_name) |
| 298 | { |
| 299 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 300 | DataManager::SetValue("ui_progress", 0); |
| 301 | DataManager::SetValue("tw_operation", operation_name); |
| 302 | DataManager::SetValue("tw_operation_status", 0); |
| 303 | DataManager::SetValue("tw_operation_state", 0); |
| 304 | } |
| 305 | |
| 306 | void GUIAction::operation_end(const int operation_status, const int simulate) |
| 307 | { |
| 308 | int simulate_fail; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 309 | DataManager::SetValue("ui_progress", 100); |
| 310 | if (simulate) { |
| 311 | DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail); |
| 312 | if (simulate_fail != 0) |
| 313 | DataManager::SetValue("tw_operation_status", 1); |
| 314 | else |
| 315 | DataManager::SetValue("tw_operation_status", 0); |
| 316 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 317 | if (operation_status != 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 318 | DataManager::SetValue("tw_operation_status", 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 319 | } |
| 320 | else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 321 | DataManager::SetValue("tw_operation_status", 0); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 322 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 323 | } |
| 324 | DataManager::SetValue("tw_operation_state", 1); |
| 325 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 326 | } |
| 327 | |
| 328 | int GUIAction::doAction(Action action, int isThreaded /* = 0 */) |
| 329 | { |
| 330 | static string zip_queue[10]; |
| 331 | static int zip_queue_index; |
| 332 | static pthread_t terminal_command; |
| 333 | int simulate; |
| 334 | |
| 335 | std::string arg = gui_parse_text(action.mArg); |
| 336 | |
| 337 | std::string function = gui_parse_text(action.mFunction); |
| 338 | |
| 339 | DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); |
| 340 | |
| 341 | if (function == "reboot") |
| 342 | { |
| 343 | //curtainClose(); this sometimes causes a crash |
| 344 | |
| 345 | sync(); |
| 346 | |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 347 | if (arg == "recovery") |
| 348 | TWFunc::tw_reboot(rb_recovery); |
| 349 | else if (arg == "poweroff") |
| 350 | TWFunc::tw_reboot(rb_poweroff); |
| 351 | else if (arg == "bootloader") |
| 352 | TWFunc::tw_reboot(rb_bootloader); |
| 353 | else if (arg == "download") |
| 354 | TWFunc::tw_reboot(rb_download); |
| 355 | else |
| 356 | TWFunc::tw_reboot(rb_system); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 357 | |
| 358 | // This should never occur |
| 359 | return -1; |
| 360 | } |
| 361 | if (function == "home") |
| 362 | { |
| 363 | PageManager::SelectPackage("TWRP"); |
| 364 | gui_changePage("main"); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | if (function == "key") |
| 369 | { |
| 370 | PageManager::NotifyKey(getKeyByName(arg)); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | if (function == "page") { |
| 375 | std::string page_name = gui_parse_text(arg); |
| 376 | return gui_changePage(page_name); |
| 377 | } |
| 378 | |
| 379 | if (function == "reload") { |
| 380 | int check = 0, ret_val = 0; |
| 381 | std::string theme_path; |
| 382 | |
| 383 | operation_start("Reload Theme"); |
| 384 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 385 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
| 387 | check = 1; |
| 388 | } |
| 389 | |
| 390 | theme_path += "/TWRP/theme/ui.zip"; |
| 391 | if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 392 | { |
| 393 | // Loading the custom theme failed - try loading the stock theme |
| 394 | LOGI("Attempting to reload stock theme...\n"); |
| 395 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 396 | { |
| 397 | LOGE("Failed to load base packages.\n"); |
| 398 | ret_val = 1; |
| 399 | } |
| 400 | } |
| 401 | operation_end(ret_val, simulate); |
| 402 | } |
| 403 | |
| 404 | if (function == "readBackup") |
| 405 | { |
| 406 | string Restore_Name; |
| 407 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 408 | PartitionManager.Set_Restore_Files(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | if (function == "set") |
| 413 | { |
| 414 | if (arg.find('=') != string::npos) |
| 415 | { |
| 416 | string varName = arg.substr(0, arg.find('=')); |
| 417 | string value = arg.substr(arg.find('=') + 1, string::npos); |
| 418 | |
| 419 | DataManager::GetValue(value, value); |
| 420 | DataManager::SetValue(varName, value); |
| 421 | } |
| 422 | else |
| 423 | DataManager::SetValue(arg, "1"); |
| 424 | return 0; |
| 425 | } |
| 426 | if (function == "clear") |
| 427 | { |
| 428 | DataManager::SetValue(arg, "0"); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | if (function == "mount") |
| 433 | { |
| 434 | if (arg == "usb") |
| 435 | { |
| 436 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 437 | if (!simulate) |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 438 | PartitionManager.usb_storage_enable(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 439 | else |
| 440 | ui_print("Simulating actions...\n"); |
| 441 | } |
| 442 | else if (!simulate) |
| 443 | { |
| 444 | string cmd; |
| 445 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 446 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 447 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 448 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 449 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 450 | PartitionManager.Mount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 451 | } else |
| 452 | ui_print("Simulating actions...\n"); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | if (function == "umount" || function == "unmount") |
| 457 | { |
| 458 | if (arg == "usb") |
| 459 | { |
| 460 | if (!simulate) |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 461 | PartitionManager.usb_storage_disable(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 462 | else |
| 463 | ui_print("Simulating actions...\n"); |
| 464 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 465 | } |
| 466 | else if (!simulate) |
| 467 | { |
| 468 | string cmd; |
| 469 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 470 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 471 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 472 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 473 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 474 | PartitionManager.UnMount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 475 | } else |
| 476 | ui_print("Simulating actions...\n"); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | if (function == "restoredefaultsettings") |
| 481 | { |
| 482 | operation_start("Restore Defaults"); |
| 483 | if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting |
| 484 | ui_print("Simulating actions...\n"); |
| 485 | else { |
| 486 | DataManager::ResetDefaults(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 487 | PartitionManager.Update_System_Details(); |
| 488 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 489 | } |
| 490 | operation_end(0, simulate); |
| 491 | } |
| 492 | |
| 493 | if (function == "copylog") |
| 494 | { |
| 495 | operation_start("Copy Log"); |
| 496 | if (!simulate) |
| 497 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 498 | string dst; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 499 | PartitionManager.Mount_Current_Storage(true); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 500 | dst = DataManager::GetCurrentStoragePath() + "/recovery.log"; |
| 501 | TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 502 | sync(); |
| 503 | ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str()); |
| 504 | } else |
| 505 | simulate_progress_bar(); |
| 506 | operation_end(0, simulate); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | if (function == "compute" || function == "addsubtract") |
| 511 | { |
| 512 | if (arg.find("+") != string::npos) |
| 513 | { |
| 514 | string varName = arg.substr(0, arg.find('+')); |
| 515 | string string_to_add = arg.substr(arg.find('+') + 1, string::npos); |
| 516 | int amount_to_add = atoi(string_to_add.c_str()); |
| 517 | int value; |
| 518 | |
| 519 | DataManager::GetValue(varName, value); |
| 520 | DataManager::SetValue(varName, value + amount_to_add); |
| 521 | return 0; |
| 522 | } |
| 523 | if (arg.find("-") != string::npos) |
| 524 | { |
| 525 | string varName = arg.substr(0, arg.find('-')); |
| 526 | string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos); |
| 527 | int amount_to_subtract = atoi(string_to_subtract.c_str()); |
| 528 | int value; |
| 529 | |
| 530 | DataManager::GetValue(varName, value); |
| 531 | value -= amount_to_subtract; |
| 532 | if (value <= 0) |
| 533 | value = 0; |
| 534 | DataManager::SetValue(varName, value); |
| 535 | return 0; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (function == "setguitimezone") |
| 540 | { |
| 541 | string SelectedZone; |
| 542 | DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone |
| 543 | string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone |
| 544 | string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component |
| 545 | |
| 546 | int dst; |
| 547 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
| 548 | |
| 549 | string offset; |
| 550 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
| 551 | |
| 552 | string NewTimeZone = Zone; |
| 553 | if (offset != "0") |
| 554 | NewTimeZone += ":" + offset; |
| 555 | |
| 556 | if (dst != 0) |
| 557 | NewTimeZone += DSTZone; |
| 558 | |
| 559 | DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 560 | DataManager::update_tz_environment_variables(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | if (function == "togglestorage") { |
| 565 | if (arg == "internal") { |
| 566 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 567 | } else if (arg == "external") { |
| 568 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 569 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 570 | if (PartitionManager.Mount_Current_Storage(true)) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 571 | if (arg == "internal") { |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 572 | string zip_path, zip_root; |
| 573 | DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path); |
| 574 | zip_root = TWFunc::Get_Root_Path(zip_path); |
| 575 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 576 | #ifndef TW_EXTERNAL_STORAGE_PATH |
| 577 | if (zip_root != "/sdcard") |
| 578 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard"); |
| 579 | #else |
| 580 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 581 | if (zip_root != "/emmc") |
| 582 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc"); |
| 583 | } else { |
| 584 | if (zip_root != "/sdcard") |
| 585 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard"); |
| 586 | } |
| 587 | #endif |
| 588 | #else |
| 589 | if (zip_root != DataManager::GetCurrentStoragePath()) |
| 590 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath()); |
| 591 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 592 | // Save the current zip location to the external variable |
| 593 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 594 | // Change the current zip location to the internal variable |
| 595 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR)); |
| 596 | } else if (arg == "external") { |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 597 | string zip_path, zip_root; |
| 598 | DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path); |
| 599 | zip_root = TWFunc::Get_Root_Path(zip_path); |
| 600 | if (zip_root != DataManager::GetCurrentStoragePath()) { |
| 601 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath()); |
| 602 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 603 | // Save the current zip location to the internal variable |
| 604 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 605 | // Change the current zip location to the external variable |
| 606 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR)); |
| 607 | } |
| 608 | } else { |
| 609 | // We weren't able to toggle for some reason, restore original setting |
| 610 | if (arg == "internal") { |
| 611 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 612 | } else if (arg == "external") { |
| 613 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 614 | } |
| 615 | } |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | if (function == "overlay") |
| 620 | return gui_changeOverlay(arg); |
| 621 | |
| 622 | if (function == "queuezip") |
| 623 | { |
| 624 | if (zip_queue_index >= 10) { |
| 625 | ui_print("Maximum zip queue reached!\n"); |
| 626 | return 0; |
| 627 | } |
| 628 | DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]); |
| 629 | if (strlen(zip_queue[zip_queue_index].c_str()) > 0) { |
| 630 | zip_queue_index++; |
| 631 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | if (function == "cancelzip") |
| 637 | { |
| 638 | if (zip_queue_index <= 0) { |
| 639 | ui_print("Minimum zip queue reached!\n"); |
| 640 | return 0; |
| 641 | } else { |
| 642 | zip_queue_index--; |
| 643 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 644 | } |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | if (function == "queueclear") |
| 649 | { |
| 650 | zip_queue_index = 0; |
| 651 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | if (function == "sleep") |
| 656 | { |
| 657 | operation_start("Sleep"); |
| 658 | usleep(atoi(arg.c_str())); |
| 659 | operation_end(0, simulate); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | if (isThreaded) |
| 664 | { |
| 665 | if (function == "fileexists") |
| 666 | { |
| 667 | struct stat st; |
| 668 | string newpath = arg + "/."; |
| 669 | |
| 670 | operation_start("FileExists"); |
| 671 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 672 | operation_end(0, simulate); |
| 673 | else |
| 674 | operation_end(1, simulate); |
| 675 | } |
| 676 | |
| 677 | if (function == "flash") |
| 678 | { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 679 | int i, ret_val = 0, wipe_cache = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 680 | |
| 681 | for (i=0; i<zip_queue_index; i++) { |
| 682 | operation_start("Flashing"); |
| 683 | DataManager::SetValue("tw_filename", zip_queue[i]); |
| 684 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 685 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 686 | ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 687 | if (ret_val != 0) { |
| 688 | ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str()); |
| 689 | i = 10; // Error flashing zip - exit queue |
| 690 | ret_val = 1; |
| 691 | } |
| 692 | } |
| 693 | zip_queue_index = 0; |
| 694 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 695 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 696 | if (wipe_cache) |
| 697 | PartitionManager.Wipe_By_Path("/cache"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 698 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 699 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 700 | operation_start("ReinjectTWRP"); |
| 701 | ui_print("Injecting TWRP into boot image...\n"); |
| 702 | if (simulate) { |
| 703 | simulate_progress_bar(); |
| 704 | } else { |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 705 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 706 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 707 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 708 | else { |
| 709 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 710 | TWFunc::Exec_Cmd(injectcmd, result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 711 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 712 | ui_print("TWRP injection complete.\n"); |
| 713 | } |
| 714 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 715 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 716 | operation_end(ret_val, simulate); |
| 717 | return 0; |
| 718 | } |
| 719 | if (function == "wipe") |
| 720 | { |
| 721 | operation_start("Format"); |
| 722 | DataManager::SetValue("tw_partition", arg); |
| 723 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 724 | int ret_val = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 725 | |
| 726 | if (simulate) { |
| 727 | simulate_progress_bar(); |
| 728 | } else { |
| 729 | if (arg == "data") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 730 | ret_val = PartitionManager.Factory_Reset(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 731 | else if (arg == "battery") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 732 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 733 | else if (arg == "rotate") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 734 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 735 | else if (arg == "dalvik") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 736 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 737 | else if (arg == "DATAMEDIA") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 738 | ret_val = PartitionManager.Format_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 739 | } else if (arg == "INTERNAL") { |
| 740 | int has_datamedia, dual_storage; |
| 741 | |
| 742 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 743 | if (has_datamedia) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 744 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 745 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 746 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 747 | } |
| 748 | } else if (arg == "EXTERNAL") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 749 | string External_Path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 750 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 751 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 752 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 753 | } else if (arg == "ANDROIDSECURE") { |
| 754 | ret_val = PartitionManager.Wipe_Android_Secure(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 755 | } else |
| 756 | ret_val = PartitionManager.Wipe_By_Path(arg); |
| 757 | |
| 758 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 759 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 760 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
| 761 | |
| 762 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 763 | LOGI("Making TWRP folder and saving settings.\n"); |
| 764 | Storage_Path += "/TWRP"; |
| 765 | mkdir(Storage_Path.c_str(), 0777); |
| 766 | DataManager::Flush(); |
| 767 | } else { |
| 768 | LOGE("Unable to recreate TWRP folder and save settings.\n"); |
| 769 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 770 | } |
| 771 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 772 | PartitionManager.Update_System_Details(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 773 | if (ret_val) |
| 774 | ret_val = 0; // 0 is success |
| 775 | else |
| 776 | ret_val = 1; // 1 is failure |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 777 | operation_end(ret_val, simulate); |
| 778 | return 0; |
| 779 | } |
| 780 | if (function == "refreshsizes") |
| 781 | { |
| 782 | operation_start("Refreshing Sizes"); |
| 783 | if (simulate) { |
| 784 | simulate_progress_bar(); |
| 785 | } else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 786 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 787 | operation_end(0, simulate); |
| 788 | } |
| 789 | if (function == "nandroid") |
| 790 | { |
| 791 | operation_start("Nandroid"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 792 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 793 | |
| 794 | if (simulate) { |
| 795 | DataManager::SetValue("tw_partition", "Simulation"); |
| 796 | simulate_progress_bar(); |
| 797 | } else { |
| 798 | if (arg == "backup") { |
| 799 | string Backup_Name; |
| 800 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 801 | if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 802 | ret = PartitionManager.Run_Backup(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 803 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 804 | else { |
| 805 | operation_end(1, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 806 | return -1; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 807 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 808 | DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)"); |
| 809 | } else if (arg == "restore") { |
| 810 | string Restore_Name; |
| 811 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 812 | ret = PartitionManager.Run_Restore(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 813 | } else { |
| 814 | operation_end(1, simulate); |
| 815 | return -1; |
| 816 | } |
| 817 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 818 | if (ret == false) |
| 819 | ret = 1; // 1 for failure |
| 820 | else |
| 821 | ret = 0; // 0 for success |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 822 | operation_end(ret, simulate); |
| 823 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 824 | } |
| 825 | if (function == "fixpermissions") |
| 826 | { |
| 827 | operation_start("Fix Permissions"); |
| 828 | LOGI("fix permissions started!\n"); |
| 829 | if (simulate) { |
| 830 | simulate_progress_bar(); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 831 | } else { |
Dees_Troy | 6480ce0 | 2012-10-10 10:26:54 -0400 | [diff] [blame] | 832 | int op_status = PartitionManager.Fix_Permissions(); |
| 833 | if (op_status != 0) |
| 834 | op_status = 1; // failure |
| 835 | operation_end(op_status, simulate); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 836 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | if (function == "dd") |
| 840 | { |
| 841 | operation_start("imaging"); |
| 842 | |
| 843 | if (simulate) { |
| 844 | simulate_progress_bar(); |
| 845 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 846 | string result; |
| 847 | string cmd = "dd " + arg; |
| 848 | TWFunc::Exec_Cmd(cmd, result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 849 | } |
| 850 | operation_end(0, simulate); |
| 851 | return 0; |
| 852 | } |
| 853 | if (function == "partitionsd") |
| 854 | { |
| 855 | operation_start("Partition SD Card"); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 856 | int ret_val = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 857 | |
| 858 | if (simulate) { |
| 859 | simulate_progress_bar(); |
| 860 | } else { |
| 861 | int allow_partition; |
| 862 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 863 | if (allow_partition == 0) { |
| 864 | ui_print("This device does not have a real SD Card!\nAborting!\n"); |
| 865 | } else { |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 866 | if (!PartitionManager.Partition_SDCard()) |
| 867 | ret_val = 1; // failed |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 868 | } |
| 869 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 870 | operation_end(ret_val, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | if (function == "installhtcdumlock") |
| 874 | { |
| 875 | operation_start("Install HTC Dumlock"); |
| 876 | if (simulate) { |
| 877 | simulate_progress_bar(); |
| 878 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 879 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 880 | |
| 881 | operation_end(0, simulate); |
| 882 | return 0; |
| 883 | } |
| 884 | if (function == "htcdumlockrestoreboot") |
| 885 | { |
| 886 | operation_start("HTC Dumlock Restore Boot"); |
| 887 | if (simulate) { |
| 888 | simulate_progress_bar(); |
| 889 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 890 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 891 | |
| 892 | operation_end(0, simulate); |
| 893 | return 0; |
| 894 | } |
| 895 | if (function == "htcdumlockreflashrecovery") |
| 896 | { |
| 897 | operation_start("HTC Dumlock Reflash Recovery"); |
| 898 | if (simulate) { |
| 899 | simulate_progress_bar(); |
| 900 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 901 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 902 | |
| 903 | operation_end(0, simulate); |
| 904 | return 0; |
| 905 | } |
| 906 | if (function == "cmd") |
| 907 | { |
| 908 | int op_status = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 909 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 910 | |
| 911 | operation_start("Command"); |
| 912 | LOGI("Running command: '%s'\n", arg.c_str()); |
| 913 | if (simulate) { |
| 914 | simulate_progress_bar(); |
| 915 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 916 | op_status = TWFunc::Exec_Cmd(arg, result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 917 | if (op_status != 0) |
| 918 | op_status = 1; |
| 919 | } |
| 920 | |
| 921 | operation_end(op_status, simulate); |
| 922 | return 0; |
| 923 | } |
| 924 | if (function == "terminalcommand") |
| 925 | { |
| 926 | int op_status = 0; |
| 927 | string cmdpath, command; |
| 928 | |
| 929 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 930 | operation_start("CommandOutput"); |
| 931 | ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 932 | if (simulate) { |
| 933 | simulate_progress_bar(); |
| 934 | operation_end(op_status, simulate); |
| 935 | } else { |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 936 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 937 | LOGI("Actual command is: '%s'\n", command.c_str()); |
| 938 | DataManager::SetValue("tw_terminal_command_thread", command); |
| 939 | DataManager::SetValue("tw_terminal_state", 1); |
| 940 | DataManager::SetValue("tw_background_thread_running", 1); |
| 941 | op_status = pthread_create(&terminal_command, NULL, command_thread, NULL); |
| 942 | if (op_status != 0) { |
| 943 | LOGE("Error starting terminal command thread, %i.\n", op_status); |
| 944 | DataManager::SetValue("tw_terminal_state", 0); |
| 945 | DataManager::SetValue("tw_background_thread_running", 0); |
| 946 | operation_end(1, simulate); |
| 947 | } |
| 948 | } |
| 949 | return 0; |
| 950 | } |
| 951 | if (function == "killterminal") |
| 952 | { |
| 953 | int op_status = 0; |
| 954 | |
| 955 | LOGI("Sending kill command...\n"); |
| 956 | operation_start("KillCommand"); |
| 957 | DataManager::SetValue("tw_operation_status", 0); |
| 958 | DataManager::SetValue("tw_operation_state", 1); |
| 959 | DataManager::SetValue("tw_terminal_state", 0); |
| 960 | DataManager::SetValue("tw_background_thread_running", 0); |
| 961 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 962 | return 0; |
| 963 | } |
| 964 | if (function == "reinjecttwrp") |
| 965 | { |
| 966 | int op_status = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 967 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 968 | operation_start("ReinjectTWRP"); |
| 969 | ui_print("Injecting TWRP into boot image...\n"); |
| 970 | if (simulate) { |
| 971 | simulate_progress_bar(); |
| 972 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 973 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 974 | ui_print("TWRP injection complete.\n"); |
| 975 | } |
| 976 | |
| 977 | operation_end(op_status, simulate); |
| 978 | return 0; |
| 979 | } |
| 980 | if (function == "checkbackupname") |
| 981 | { |
| 982 | int op_status = 0; |
| 983 | |
| 984 | operation_start("CheckBackupName"); |
| 985 | if (simulate) { |
| 986 | simulate_progress_bar(); |
| 987 | } else { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 988 | op_status = PartitionManager.Check_Backup_Name(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 989 | if (op_status != 0) |
| 990 | op_status = 1; |
| 991 | } |
| 992 | |
| 993 | operation_end(op_status, simulate); |
| 994 | return 0; |
| 995 | } |
| 996 | if (function == "decrypt") |
| 997 | { |
| 998 | int op_status = 0; |
| 999 | |
| 1000 | operation_start("Decrypt"); |
| 1001 | if (simulate) { |
| 1002 | simulate_progress_bar(); |
| 1003 | } else { |
| 1004 | string Password; |
| 1005 | DataManager::GetValue("tw_crypto_password", Password); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1006 | op_status = PartitionManager.Decrypt_Device(Password); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1007 | if (op_status != 0) |
| 1008 | op_status = 1; |
| 1009 | else { |
| 1010 | int load_theme = 1; |
| 1011 | |
| 1012 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1013 | |
| 1014 | if (load_theme) { |
| 1015 | int has_datamedia; |
| 1016 | |
| 1017 | // Check for a custom theme and load it if exists |
| 1018 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1019 | if (has_datamedia != 0) { |
| 1020 | struct stat st; |
| 1021 | int check = 0; |
| 1022 | std::string theme_path; |
| 1023 | |
| 1024 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1025 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1026 | LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
| 1027 | check = 1; |
| 1028 | } |
| 1029 | |
| 1030 | theme_path += "/TWRP/theme/ui.zip"; |
| 1031 | if (check == 0 && stat(theme_path.c_str(), &st) == 0) { |
| 1032 | if (PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 1033 | { |
| 1034 | // Loading the custom theme failed - try loading the stock theme |
| 1035 | LOGI("Attempting to reload stock theme...\n"); |
| 1036 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 1037 | { |
| 1038 | LOGE("Failed to load base packages.\n"); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | operation_end(op_status, simulate); |
| 1048 | return 0; |
| 1049 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1050 | if (function == "adbsideload") |
| 1051 | { |
| 1052 | int ret = 0; |
| 1053 | |
| 1054 | operation_start("Sideload"); |
| 1055 | if (simulate) { |
| 1056 | simulate_progress_bar(); |
| 1057 | } else { |
| 1058 | int wipe_cache = 0; |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1059 | int wipe_dalvik = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1060 | string result, Sideload_File; |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1061 | |
| 1062 | if (!PartitionManager.Mount_Current_Storage(true)) { |
| 1063 | operation_end(1, simulate); |
| 1064 | return 0; |
| 1065 | } |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1066 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
| 1067 | if (TWFunc::Path_Exists(Sideload_File)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1068 | unlink(Sideload_File.c_str()); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1069 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1070 | ui_print("Starting ADB sideload feature...\n"); |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1071 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1072 | ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str()); |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1073 | if (ret != 0) { |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1074 | ret = 1; // failure |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1075 | } else { |
| 1076 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1077 | PartitionManager.Wipe_By_Path("/cache"); |
| 1078 | if (wipe_dalvik) |
| 1079 | PartitionManager.Wipe_Dalvik_Cache(); |
| 1080 | } |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1081 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 1082 | operation_start("ReinjectTWRP"); |
| 1083 | ui_print("Injecting TWRP into boot image...\n"); |
| 1084 | if (simulate) { |
| 1085 | simulate_progress_bar(); |
| 1086 | } else { |
| 1087 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1088 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1089 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1090 | else { |
| 1091 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1092 | TWFunc::Exec_Cmd(injectcmd, result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1093 | } |
| 1094 | ui_print("TWRP injection complete.\n"); |
| 1095 | } |
| 1096 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1097 | } |
| 1098 | operation_end(ret, simulate); |
| 1099 | return 0; |
| 1100 | } |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1101 | if (function == "adbsideloadcancel") |
| 1102 | { |
| 1103 | int child_pid; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1104 | string Sideload_File; |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1105 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1106 | unlink(Sideload_File.c_str()); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1107 | DataManager::GetValue("tw_child_pid", child_pid); |
| 1108 | ui_print("Cancelling ADB sideload...\n"); |
| 1109 | kill(child_pid, SIGTERM); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 1110 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1113 | if (function == "openrecoveryscript") { |
| 1114 | operation_start("OpenRecoveryScript"); |
| 1115 | if (simulate) { |
| 1116 | simulate_progress_bar(); |
| 1117 | } else { |
| 1118 | // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands |
| 1119 | // that we converted to ORS commands during boot in recovery.cpp. |
| 1120 | // Run those first. |
| 1121 | int reboot = 0; |
| 1122 | if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) { |
| 1123 | ui_print("Processing AOSP recovery commands...\n"); |
| 1124 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 1125 | reboot = 1; |
| 1126 | } |
| 1127 | } |
| 1128 | // Check for the ORS file in /cache and attempt to run those commands. |
| 1129 | if (OpenRecoveryScript::check_for_script_file()) { |
| 1130 | ui_print("Processing OpenRecoveryScript file...\n"); |
| 1131 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 1132 | reboot = 1; |
| 1133 | } |
| 1134 | } |
| 1135 | if (reboot) { |
| 1136 | usleep(2000000); // Sleep for 2 seconds before rebooting |
| 1137 | TWFunc::tw_reboot(rb_system); |
| 1138 | } else { |
| 1139 | DataManager::SetValue("tw_page_done", 1); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1143 | } |
| 1144 | else |
| 1145 | { |
| 1146 | pthread_t t; |
| 1147 | pthread_create(&t, NULL, thread_start, this); |
| 1148 | return 0; |
| 1149 | } |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
| 1153 | int GUIAction::getKeyByName(std::string key) |
| 1154 | { |
| 1155 | if (key == "home") return KEY_HOME; |
| 1156 | else if (key == "menu") return KEY_MENU; |
| 1157 | else if (key == "back") return KEY_BACK; |
| 1158 | else if (key == "search") return KEY_SEARCH; |
| 1159 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1160 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1161 | else if (key == "power") { |
| 1162 | int ret_val; |
| 1163 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1164 | if (!ret_val) |
| 1165 | return KEY_POWER; |
| 1166 | else |
| 1167 | return ret_val; |
| 1168 | } |
| 1169 | |
| 1170 | return atol(key.c_str()); |
| 1171 | } |
| 1172 | |
| 1173 | void* GUIAction::command_thread(void *cookie) |
| 1174 | { |
| 1175 | string command; |
| 1176 | FILE* fp; |
| 1177 | char line[512]; |
| 1178 | |
| 1179 | DataManager::GetValue("tw_terminal_command_thread", command); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1180 | fp = popen(command.c_str(), "r"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1181 | if (fp == NULL) { |
| 1182 | LOGE("Error opening command to run.\n"); |
| 1183 | } else { |
| 1184 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0; |
| 1185 | struct timeval timeout; |
| 1186 | fd_set fdset; |
| 1187 | |
| 1188 | while(keep_going) |
| 1189 | { |
| 1190 | FD_ZERO(&fdset); |
| 1191 | FD_SET(fd, &fdset); |
| 1192 | timeout.tv_sec = 0; |
| 1193 | timeout.tv_usec = 400000; |
| 1194 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1195 | if (has_data == 0) { |
| 1196 | // Timeout reached |
| 1197 | DataManager::GetValue("tw_terminal_state", check); |
| 1198 | if (check == 0) { |
| 1199 | keep_going = 0; |
| 1200 | } |
| 1201 | } else if (has_data < 0) { |
| 1202 | // End of execution |
| 1203 | keep_going = 0; |
| 1204 | } else { |
| 1205 | // Try to read output |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1206 | memset(line, 0, sizeof(line)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1207 | bytes_read = read(fd, line, sizeof(line)); |
| 1208 | if (bytes_read > 0) |
| 1209 | ui_print("%s", line); // Display output |
| 1210 | else |
| 1211 | keep_going = 0; // Done executing |
| 1212 | } |
| 1213 | } |
| 1214 | fclose(fp); |
| 1215 | } |
| 1216 | DataManager::SetValue("tw_operation_status", 0); |
| 1217 | DataManager::SetValue("tw_operation_state", 1); |
| 1218 | DataManager::SetValue("tw_terminal_state", 0); |
| 1219 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1220 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1221 | return NULL; |
| 1222 | } |