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