Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/input.h> |
| 18 | #include <pthread.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <errno.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/reboot.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <sys/mount.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
| 35 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 36 | extern "C" |
| 37 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | #include "../common.h" |
| 39 | #include "../roots.h" |
| 40 | #include "../minuitwrp/minui.h" |
| 41 | #include "../recovery_ui.h" |
| 42 | #include "../minzip/Zip.h" |
| 43 | #include <pixelflinger/pixelflinger.h> |
| 44 | } |
| 45 | |
| 46 | #include "rapidxml.hpp" |
| 47 | #include "objects.hpp" |
| 48 | #include "../data.hpp" |
| 49 | #include "../variables.h" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 50 | #include "../partitions.hpp" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 51 | #include "../twrp-functions.hpp" |
| 52 | #include "blanktimer.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | const static int CURTAIN_FADE = 32; |
| 55 | |
| 56 | using namespace rapidxml; |
| 57 | |
| 58 | // Global values |
| 59 | static gr_surface gCurtain = NULL; |
| 60 | static int gGuiInitialized = 0; |
| 61 | static int gGuiConsoleRunning = 0; |
| 62 | static int gGuiConsoleTerminate = 0; |
| 63 | static int gForceRender = 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 64 | pthread_mutex_t gForceRendermutex; |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 65 | static int gNoAnimation = 1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 66 | static int gGuiInputRunning = 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 67 | blanktimer blankTimer; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
| 69 | // Needed by pages.cpp too |
| 70 | int gGuiRunning = 0; |
| 71 | |
| 72 | static int gRecorder = -1; |
| 73 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 74 | extern "C" void gr_write_frame_to_file (int fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 76 | void |
| 77 | flip (void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 79 | if (gRecorder != -1) |
| 80 | { |
| 81 | timespec time; |
| 82 | clock_gettime (CLOCK_MONOTONIC, &time); |
| 83 | write (gRecorder, &time, sizeof (timespec)); |
| 84 | gr_write_frame_to_file (gRecorder); |
| 85 | } |
| 86 | gr_flip (); |
| 87 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 88 | } |
| 89 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 90 | void |
| 91 | rapidxml::parse_error_handler (const char *what, void *where) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 92 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 93 | fprintf (stderr, "Parser error: %s\n", what); |
| 94 | fprintf (stderr, " Start of string: %s\n", (char *) where); |
| 95 | abort (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 96 | } |
| 97 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 98 | static void |
| 99 | curtainSet () |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 101 | gr_color (0, 0, 0, 255); |
| 102 | gr_fill (0, 0, gr_fb_width (), gr_fb_height ()); |
| 103 | gr_blit (gCurtain, 0, 0, gr_get_width (gCurtain), gr_get_height (gCurtain), |
| 104 | 0, 0); |
| 105 | gr_flip (); |
| 106 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | } |
| 108 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 109 | static void |
| 110 | curtainRaise (gr_surface surface) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 112 | int sy = 0; |
| 113 | int h = gr_get_height (gCurtain) - 1; |
| 114 | int w = gr_get_width (gCurtain); |
| 115 | int fy = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 117 | int msw = gr_get_width (surface); |
| 118 | int msh = gr_get_height (surface); |
| 119 | int CURTAIN_RATE = msh / 30; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 121 | if (gNoAnimation == 0) |
| 122 | { |
| 123 | for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE) |
| 124 | { |
| 125 | gr_blit (surface, 0, 0, msw, msh, 0, 0); |
| 126 | gr_blit (gCurtain, 0, sy, w, h, 0, 0); |
| 127 | gr_flip (); |
| 128 | } |
| 129 | } |
| 130 | gr_blit (surface, 0, 0, msw, msh, 0, 0); |
| 131 | flip (); |
| 132 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 133 | } |
| 134 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 135 | void |
| 136 | curtainClose () |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | { |
| 138 | #if 0 |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 139 | int w = gr_get_width (gCurtain); |
| 140 | int h = 1; |
| 141 | int sy = gr_get_height (gCurtain) - 1; |
| 142 | int fbh = gr_fb_height (); |
| 143 | int CURTAIN_RATE = fbh / 30; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 145 | if (gNoAnimation == 0) |
| 146 | { |
| 147 | for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE) |
| 148 | { |
| 149 | gr_blit (gCurtain, 0, sy, w, h, 0, 0); |
| 150 | gr_flip (); |
| 151 | } |
| 152 | gr_blit (gCurtain, 0, 0, gr_get_width (gCurtain), |
| 153 | gr_get_height (gCurtain), 0, 0); |
| 154 | gr_flip (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 155 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 156 | if (gRecorder != -1) |
| 157 | close (gRecorder); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 158 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 159 | int fade; |
| 160 | for (fade = 16; fade < 255; fade += CURTAIN_FADE) |
| 161 | { |
| 162 | gr_blit (gCurtain, 0, 0, gr_get_width (gCurtain), |
| 163 | gr_get_height (gCurtain), 0, 0); |
| 164 | gr_color (0, 0, 0, fade); |
| 165 | gr_fill (0, 0, gr_fb_width (), gr_fb_height ()); |
| 166 | gr_flip (); |
| 167 | } |
| 168 | gr_color (0, 0, 0, 255); |
| 169 | gr_fill (0, 0, gr_fb_width (), gr_fb_height ()); |
| 170 | gr_flip (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 171 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 172 | #else |
| 173 | gr_blit (gCurtain, 0, 0, gr_get_width (gCurtain), gr_get_height (gCurtain), |
| 174 | 0, 0); |
| 175 | gr_flip (); |
| 176 | #endif |
| 177 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 178 | } |
| 179 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 180 | static void * |
| 181 | input_thread (void *cookie) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 183 | int drag = 0; |
| 184 | static int touch_and_hold = 0, dontwait = 0, touch_repeat = 0, x = 0, y = |
| 185 | 0, lshift = 0, rshift = 0, key_repeat = 0; |
| 186 | static struct timeval touchStart; |
| 187 | HardwareKeyboard kb; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 188 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 189 | //start screen timeout threads |
| 190 | blankTimer.setTimerThread(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 191 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 192 | for (;;) |
| 193 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 195 | // wait for the next event |
| 196 | struct input_event ev; |
| 197 | int state = 0, ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 199 | ret = ev_get (&ev, dontwait); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 200 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 201 | if (ret < 0) |
| 202 | { |
| 203 | struct timeval curTime; |
| 204 | gettimeofday (&curTime, NULL); |
| 205 | long mtime, seconds, useconds; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 206 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 207 | seconds = curTime.tv_sec - touchStart.tv_sec; |
| 208 | useconds = curTime.tv_usec - touchStart.tv_usec; |
| 209 | |
| 210 | mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5; |
| 211 | if (touch_and_hold && mtime > 500) |
| 212 | { |
| 213 | touch_and_hold = 0; |
| 214 | touch_repeat = 1; |
| 215 | gettimeofday (&touchStart, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | #ifdef _EVENT_LOGGING |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 217 | LOGE ("TOUCH_HOLD: %d,%d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 218 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 219 | PageManager::NotifyTouch (TOUCH_HOLD, x, y); |
| 220 | blankTimer.resetTimerAndUnblank(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 221 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 222 | else if (touch_repeat && mtime > 100) |
| 223 | { |
| 224 | #ifdef _EVENT_LOGGING |
| 225 | LOGE ("TOUCH_REPEAT: %d,%d\n", x, y); |
| 226 | #endif |
| 227 | gettimeofday (&touchStart, NULL); |
| 228 | PageManager::NotifyTouch (TOUCH_REPEAT, x, y); |
| 229 | blankTimer.resetTimerAndUnblank(); |
| 230 | } |
| 231 | else if (key_repeat == 1 && mtime > 500) |
| 232 | { |
| 233 | #ifdef _EVENT_LOGGING |
| 234 | LOGE ("KEY_HOLD: %d,%d\n", x, y); |
| 235 | #endif |
| 236 | gettimeofday (&touchStart, NULL); |
| 237 | key_repeat = 2; |
| 238 | kb.KeyRepeat (); |
| 239 | blankTimer.resetTimerAndUnblank(); |
| 240 | } |
| 241 | else if (key_repeat == 2 && mtime > 100) |
| 242 | { |
| 243 | #ifdef _EVENT_LOGGING |
| 244 | LOGE ("KEY_REPEAT: %d,%d\n", x, y); |
| 245 | #endif |
| 246 | gettimeofday (&touchStart, NULL); |
| 247 | kb.KeyRepeat (); |
| 248 | blankTimer.resetTimerAndUnblank(); |
| 249 | } |
| 250 | } |
| 251 | else if (ev.type == EV_ABS) |
| 252 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 253 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 254 | x = ev.value >> 16; |
| 255 | y = ev.value & 0xFFFF; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 256 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 257 | if (ev.code == 0) |
| 258 | { |
| 259 | if (state == 0) |
| 260 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 261 | #ifdef _EVENT_LOGGING |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 262 | LOGE ("TOUCH_RELEASE: %d,%d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 263 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 264 | PageManager::NotifyTouch (TOUCH_RELEASE, x, y); |
| 265 | blankTimer.resetTimerAndUnblank(); |
| 266 | touch_and_hold = 0; |
| 267 | touch_repeat = 0; |
| 268 | if (!key_repeat) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 269 | dontwait = 0; |
| 270 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 271 | state = 0; |
| 272 | drag = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 273 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 274 | else |
| 275 | { |
| 276 | if (!drag) |
| 277 | { |
| 278 | #ifdef _EVENT_LOGGING |
| 279 | LOGE ("TOUCH_START: %d,%d\n", x, y); |
| 280 | #endif |
| 281 | if (PageManager::NotifyTouch (TOUCH_START, x, y) > 0) |
| 282 | state = 1; |
| 283 | drag = 1; |
| 284 | touch_and_hold = 1; |
| 285 | dontwait = 1; |
| 286 | key_repeat = 0; |
| 287 | gettimeofday (&touchStart, NULL); |
| 288 | blankTimer.resetTimerAndUnblank(); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | if (state == 0) |
| 293 | { |
| 294 | #ifdef _EVENT_LOGGING |
| 295 | LOGE ("TOUCH_DRAG: %d,%d\n", x, y); |
| 296 | #endif |
| 297 | if (PageManager::NotifyTouch (TOUCH_DRAG, x, y) > 0) |
| 298 | state = 1; |
| 299 | key_repeat = 0; |
| 300 | blankTimer.resetTimerAndUnblank(); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | else if (ev.type == EV_KEY) |
| 306 | { |
| 307 | // Handle key-press here |
| 308 | #ifdef _EVENT_LOGGING |
| 309 | LOGE ("TOUCH_KEY: %d\n", ev.code); |
| 310 | #endif |
| 311 | if (ev.value != 0) |
| 312 | { |
| 313 | // This is a key press |
| 314 | if (kb.KeyDown (ev.code)) |
| 315 | { |
| 316 | key_repeat = 1; |
| 317 | touch_and_hold = 0; |
| 318 | touch_repeat = 0; |
| 319 | dontwait = 1; |
| 320 | gettimeofday (&touchStart, NULL); |
| 321 | blankTimer.resetTimerAndUnblank(); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | key_repeat = 0; |
| 326 | touch_and_hold = 0; |
| 327 | touch_repeat = 0; |
| 328 | dontwait = 0; |
| 329 | blankTimer.resetTimerAndUnblank(); |
| 330 | } |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | // This is a key release |
| 335 | kb.KeyUp (ev.code); |
| 336 | key_repeat = 0; |
| 337 | touch_and_hold = 0; |
| 338 | touch_repeat = 0; |
| 339 | dontwait = 0; |
| 340 | blankTimer.resetTimerAndUnblank(); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // This special function will return immediately the first time, but then |
| 348 | // always returns 1/30th of a second (or immediately if called later) from |
| 349 | // the last time it was called |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 350 | static void |
| 351 | loopTimer (void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 352 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 353 | static timespec lastCall; |
| 354 | static int initialized = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 355 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 356 | if (!initialized) |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 357 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 358 | clock_gettime (CLOCK_MONOTONIC, &lastCall); |
| 359 | initialized = 1; |
| 360 | return; |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 361 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 362 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 363 | do |
| 364 | { |
| 365 | timespec curTime; |
| 366 | clock_gettime (CLOCK_MONOTONIC, &curTime); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 368 | timespec diff = TWFunc::timespec_diff (lastCall, curTime); |
| 369 | |
| 370 | // This is really 30 times per second |
| 371 | if (diff.tv_sec || diff.tv_nsec > 33333333) |
| 372 | { |
| 373 | lastCall = curTime; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | // We need to sleep some period time microseconds |
| 378 | unsigned int sleepTime = 33333 - (diff.tv_nsec / 1000); |
| 379 | usleep (sleepTime); |
| 380 | } |
| 381 | while (1); |
| 382 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 383 | } |
| 384 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 385 | static int |
| 386 | runPages (void) |
| 387 | { |
| 388 | // Raise the curtain |
| 389 | if (gCurtain != NULL) |
| 390 | { |
| 391 | gr_surface surface; |
| 392 | |
| 393 | PageManager::Render (); |
| 394 | gr_get_surface (&surface); |
| 395 | curtainRaise (surface); |
| 396 | gr_free_surface (surface); |
| 397 | } |
| 398 | |
| 399 | gGuiRunning = 1; |
| 400 | |
| 401 | DataManager::SetValue ("tw_loaded", 1); |
| 402 | |
| 403 | for (;;) |
| 404 | { |
| 405 | loopTimer (); |
| 406 | |
| 407 | if (!gForceRender) |
| 408 | { |
| 409 | int ret; |
| 410 | |
| 411 | ret = PageManager::Update (); |
| 412 | if (ret > 1) |
| 413 | PageManager::Render (); |
| 414 | |
| 415 | if (ret > 0) |
| 416 | flip (); |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | pthread_mutex_lock(&gForceRendermutex); |
| 421 | gForceRender = 0; |
| 422 | pthread_mutex_unlock(&gForceRendermutex); |
| 423 | PageManager::Render (); |
| 424 | flip (); |
| 425 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 426 | if (DataManager::GetIntValue("tw_gui_done") != 0) |
| 427 | { |
| 428 | break; |
| 429 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | gGuiRunning = 0; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int |
| 437 | runPage (const char *page_name) |
| 438 | { |
| 439 | gui_changePage (page_name); |
| 440 | |
| 441 | // Raise the curtain |
| 442 | if (gCurtain != NULL) |
| 443 | { |
| 444 | gr_surface surface; |
| 445 | |
| 446 | PageManager::Render (); |
| 447 | gr_get_surface (&surface); |
| 448 | curtainRaise (surface); |
| 449 | gr_free_surface (surface); |
| 450 | } |
| 451 | |
| 452 | gGuiRunning = 1; |
| 453 | |
| 454 | DataManager::SetValue ("tw_loaded", 1); |
| 455 | |
| 456 | for (;;) |
| 457 | { |
| 458 | loopTimer (); |
| 459 | |
| 460 | if (!gForceRender) |
| 461 | { |
| 462 | int ret; |
| 463 | |
| 464 | ret = PageManager::Update (); |
| 465 | if (ret > 1) |
| 466 | PageManager::Render (); |
| 467 | |
| 468 | if (ret > 0) |
| 469 | flip (); |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | pthread_mutex_lock(&gForceRendermutex); |
| 474 | gForceRender = 0; |
| 475 | pthread_mutex_unlock(&gForceRendermutex); |
| 476 | PageManager::Render (); |
| 477 | flip (); |
| 478 | } |
| 479 | if (DataManager::GetIntValue ("tw_page_done") != 0) |
| 480 | { |
| 481 | gui_changePage ("main"); |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | gGuiRunning = 0; |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | int |
| 491 | gui_forceRender (void) |
| 492 | { |
| 493 | pthread_mutex_lock(&gForceRendermutex); |
| 494 | gForceRender = 1; |
| 495 | pthread_mutex_unlock(&gForceRendermutex); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | int |
| 500 | gui_changePage (std::string newPage) |
| 501 | { |
| 502 | LOGI ("Set page: '%s'\n", newPage.c_str ()); |
| 503 | PageManager::ChangePage (newPage); |
| 504 | pthread_mutex_lock(&gForceRendermutex); |
| 505 | gForceRender = 1; |
| 506 | pthread_mutex_unlock(&gForceRendermutex); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | int |
| 511 | gui_changeOverlay (std::string overlay) |
| 512 | { |
| 513 | PageManager::ChangeOverlay (overlay); |
| 514 | pthread_mutex_lock(&gForceRendermutex); |
| 515 | gForceRender = 1; |
| 516 | pthread_mutex_unlock(&gForceRendermutex); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | int |
| 521 | gui_changePackage (std::string newPackage) |
| 522 | { |
| 523 | PageManager::SelectPackage (newPackage); |
| 524 | pthread_mutex_lock(&gForceRendermutex); |
| 525 | gForceRender = 1; |
| 526 | pthread_mutex_unlock(&gForceRendermutex); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | std::string gui_parse_text (string inText) |
| 531 | { |
| 532 | // Copied from std::string GUIText::parseText(void) |
| 533 | // This function parses text for DataManager values encompassed by %value% in the XML |
| 534 | static int counter = 0; |
| 535 | std::string str = inText; |
| 536 | size_t pos = 0; |
| 537 | size_t next = 0, end = 0; |
| 538 | |
| 539 | while (1) |
| 540 | { |
| 541 | next = str.find ('%', pos); |
| 542 | if (next == std::string::npos) |
| 543 | return str; |
| 544 | end = str.find ('%', next + 1); |
| 545 | if (end == std::string::npos) |
| 546 | return str; |
| 547 | |
| 548 | // We have a block of data |
| 549 | std::string var = str.substr (next + 1, (end - next) - 1); |
| 550 | str.erase (next, (end - next) + 1); |
| 551 | |
| 552 | if (next + 1 == end) |
| 553 | { |
| 554 | str.insert (next, 1, '%'); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | std::string value; |
| 559 | if (DataManager::GetValue (var, value) == 0) |
| 560 | str.insert (next, value); |
| 561 | } |
| 562 | |
| 563 | pos = next + 1; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | extern "C" int |
| 568 | gui_init () |
| 569 | { |
| 570 | int fd; |
| 571 | |
| 572 | gr_init (); |
| 573 | |
| 574 | if (res_create_surface ("/res/images/curtain.jpg", &gCurtain)) |
| 575 | { |
| 576 | printf |
| 577 | ("Unable to locate '/res/images/curtain.jpg'\nDid you set a DEVICE_RESOLUTION in your config files?\n"); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | curtainSet (); |
| 582 | |
| 583 | ev_init (); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | extern "C" int |
| 588 | gui_loadResources () |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 589 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 590 | // unlink("/sdcard/video.last"); |
| 591 | // rename("/sdcard/video.bin", "/sdcard/video.last"); |
| 592 | // gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY); |
| 593 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 594 | int check = 0; |
| 595 | DataManager::GetValue (TW_IS_ENCRYPTED, check); |
| 596 | if (check) |
| 597 | { |
| 598 | if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "decrypt")) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 599 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 600 | LOGE ("Failed to load base packages.\n"); |
| 601 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 602 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 603 | else |
| 604 | check = 1; |
| 605 | } |
| 606 | if (check == 0 |
| 607 | && PageManager::LoadPackage ("TWRP", "/script/ui.xml", "main")) |
| 608 | { |
| 609 | std::string theme_path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 610 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 611 | theme_path = DataManager::GetSettingsStoragePath (); |
| 612 | if (!PartitionManager.Mount_Settings_Storage (false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 613 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 614 | int retry_count = 5; |
| 615 | while (retry_count > 0 |
| 616 | && !PartitionManager.Mount_Settings_Storage (false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 617 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 618 | usleep (500000); |
| 619 | retry_count--; |
| 620 | } |
| 621 | if (!PartitionManager.Mount_Settings_Storage (false)) |
| 622 | { |
| 623 | LOGE ("Unable to mount %s during GUI startup.\n", |
| 624 | theme_path.c_str ()); |
| 625 | check = 1; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | theme_path += "/TWRP/theme/ui.zip"; |
| 630 | if (check || PageManager::LoadPackage ("TWRP", theme_path, "main")) |
| 631 | { |
| 632 | if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "main")) |
| 633 | { |
| 634 | LOGE ("Failed to load base packages.\n"); |
| 635 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 640 | // Set the default package |
| 641 | PageManager::SelectPackage ("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 642 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 643 | gGuiInitialized = 1; |
| 644 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 645 | |
| 646 | error: |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 647 | LOGE ("An internal error has occurred.\n"); |
| 648 | gGuiInitialized = 0; |
| 649 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 650 | } |
| 651 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 652 | extern "C" int |
| 653 | gui_start () |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 654 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 655 | if (!gGuiInitialized) |
| 656 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 657 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 658 | gGuiConsoleTerminate = 1; |
| 659 | while (gGuiConsoleRunning) |
| 660 | loopTimer (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 661 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 662 | // Set the default package |
| 663 | PageManager::SelectPackage ("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 664 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 665 | if (!gGuiInputRunning) |
| 666 | { |
| 667 | // Start by spinning off an input handler. |
| 668 | pthread_t t; |
| 669 | pthread_create (&t, NULL, input_thread, NULL); |
| 670 | gGuiInputRunning = 1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 671 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 672 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 673 | return runPages (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 674 | } |
| 675 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 676 | extern "C" int |
| 677 | gui_startPage (const char *page_name) |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 678 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 679 | if (!gGuiInitialized) |
| 680 | return -1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 681 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 682 | gGuiConsoleTerminate = 1; |
| 683 | while (gGuiConsoleRunning) |
| 684 | loopTimer (); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 685 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 686 | // Set the default package |
| 687 | PageManager::SelectPackage ("TWRP"); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 688 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 689 | if (!gGuiInputRunning) |
| 690 | { |
| 691 | // Start by spinning off an input handler. |
| 692 | pthread_t t; |
| 693 | pthread_create (&t, NULL, input_thread, NULL); |
| 694 | gGuiInputRunning = 1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 695 | } |
| 696 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 697 | DataManager::SetValue ("tw_page_done", 0); |
| 698 | return runPage (page_name); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 699 | } |
| 700 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 701 | static void * |
| 702 | console_thread (void *cookie) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 703 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 704 | PageManager::SwitchToConsole (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 705 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 706 | while (!gGuiConsoleTerminate) |
| 707 | { |
| 708 | loopTimer (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 709 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 710 | if (!gForceRender) |
| 711 | { |
| 712 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 713 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 714 | ret = PageManager::Update (); |
| 715 | if (ret > 1) |
| 716 | PageManager::Render (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 717 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 718 | if (ret > 0) |
| 719 | flip (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 720 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 721 | if (ret < 0) |
| 722 | LOGE ("An update request has failed.\n"); |
| 723 | } |
| 724 | else |
| 725 | { |
| 726 | pthread_mutex_lock(&gForceRendermutex); |
| 727 | gForceRender = 0; |
| 728 | pthread_mutex_unlock(&gForceRendermutex); |
| 729 | PageManager::Render (); |
| 730 | flip (); |
| 731 | } |
| 732 | } |
| 733 | gGuiConsoleRunning = 0; |
| 734 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 735 | } |
| 736 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 737 | extern "C" int |
| 738 | gui_console_only () |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 739 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 740 | if (!gGuiInitialized) |
| 741 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 742 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 743 | gGuiConsoleTerminate = 0; |
| 744 | gGuiConsoleRunning = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 745 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 746 | // Start by spinning off an input handler. |
| 747 | pthread_t t; |
| 748 | pthread_create (&t, NULL, console_thread, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 749 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 750 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 751 | } |