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; |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 188 | string seconds; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 190 | //start screen timeout threads |
| 191 | blankTimer.setTimerThread(); |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 192 | DataManager::GetValue("tw_screen_timeout_secs", seconds); |
| 193 | blankTimer.setTime(atoi(seconds.c_str())); |
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 | for (;;) |
| 196 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 197 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 198 | // wait for the next event |
| 199 | struct input_event ev; |
| 200 | int state = 0, ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 201 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 202 | ret = ev_get (&ev, dontwait); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 203 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 204 | if (ret < 0) |
| 205 | { |
| 206 | struct timeval curTime; |
| 207 | gettimeofday (&curTime, NULL); |
| 208 | long mtime, seconds, useconds; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 210 | seconds = curTime.tv_sec - touchStart.tv_sec; |
| 211 | useconds = curTime.tv_usec - touchStart.tv_usec; |
| 212 | |
| 213 | mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5; |
| 214 | if (touch_and_hold && mtime > 500) |
| 215 | { |
| 216 | touch_and_hold = 0; |
| 217 | touch_repeat = 1; |
| 218 | gettimeofday (&touchStart, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 219 | #ifdef _EVENT_LOGGING |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 220 | LOGE ("TOUCH_HOLD: %d,%d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 221 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 222 | PageManager::NotifyTouch (TOUCH_HOLD, x, y); |
| 223 | blankTimer.resetTimerAndUnblank(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 225 | else if (touch_repeat && mtime > 100) |
| 226 | { |
| 227 | #ifdef _EVENT_LOGGING |
| 228 | LOGE ("TOUCH_REPEAT: %d,%d\n", x, y); |
| 229 | #endif |
| 230 | gettimeofday (&touchStart, NULL); |
| 231 | PageManager::NotifyTouch (TOUCH_REPEAT, x, y); |
| 232 | blankTimer.resetTimerAndUnblank(); |
| 233 | } |
| 234 | else if (key_repeat == 1 && mtime > 500) |
| 235 | { |
| 236 | #ifdef _EVENT_LOGGING |
| 237 | LOGE ("KEY_HOLD: %d,%d\n", x, y); |
| 238 | #endif |
| 239 | gettimeofday (&touchStart, NULL); |
| 240 | key_repeat = 2; |
| 241 | kb.KeyRepeat (); |
| 242 | blankTimer.resetTimerAndUnblank(); |
| 243 | } |
| 244 | else if (key_repeat == 2 && mtime > 100) |
| 245 | { |
| 246 | #ifdef _EVENT_LOGGING |
| 247 | LOGE ("KEY_REPEAT: %d,%d\n", x, y); |
| 248 | #endif |
| 249 | gettimeofday (&touchStart, NULL); |
| 250 | kb.KeyRepeat (); |
| 251 | blankTimer.resetTimerAndUnblank(); |
| 252 | } |
| 253 | } |
| 254 | else if (ev.type == EV_ABS) |
| 255 | { |
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 | x = ev.value >> 16; |
| 258 | y = ev.value & 0xFFFF; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 259 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 260 | if (ev.code == 0) |
| 261 | { |
| 262 | if (state == 0) |
| 263 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 264 | #ifdef _EVENT_LOGGING |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 265 | LOGE ("TOUCH_RELEASE: %d,%d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 266 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 267 | PageManager::NotifyTouch (TOUCH_RELEASE, x, y); |
| 268 | blankTimer.resetTimerAndUnblank(); |
| 269 | touch_and_hold = 0; |
| 270 | touch_repeat = 0; |
| 271 | if (!key_repeat) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 272 | dontwait = 0; |
| 273 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 274 | state = 0; |
| 275 | drag = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 276 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 277 | else |
| 278 | { |
| 279 | if (!drag) |
| 280 | { |
| 281 | #ifdef _EVENT_LOGGING |
| 282 | LOGE ("TOUCH_START: %d,%d\n", x, y); |
| 283 | #endif |
| 284 | if (PageManager::NotifyTouch (TOUCH_START, x, y) > 0) |
| 285 | state = 1; |
| 286 | drag = 1; |
| 287 | touch_and_hold = 1; |
| 288 | dontwait = 1; |
| 289 | key_repeat = 0; |
| 290 | gettimeofday (&touchStart, NULL); |
| 291 | blankTimer.resetTimerAndUnblank(); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | if (state == 0) |
| 296 | { |
| 297 | #ifdef _EVENT_LOGGING |
| 298 | LOGE ("TOUCH_DRAG: %d,%d\n", x, y); |
| 299 | #endif |
| 300 | if (PageManager::NotifyTouch (TOUCH_DRAG, x, y) > 0) |
| 301 | state = 1; |
| 302 | key_repeat = 0; |
| 303 | blankTimer.resetTimerAndUnblank(); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | else if (ev.type == EV_KEY) |
| 309 | { |
| 310 | // Handle key-press here |
| 311 | #ifdef _EVENT_LOGGING |
| 312 | LOGE ("TOUCH_KEY: %d\n", ev.code); |
| 313 | #endif |
| 314 | if (ev.value != 0) |
| 315 | { |
| 316 | // This is a key press |
| 317 | if (kb.KeyDown (ev.code)) |
| 318 | { |
| 319 | key_repeat = 1; |
| 320 | touch_and_hold = 0; |
| 321 | touch_repeat = 0; |
| 322 | dontwait = 1; |
| 323 | gettimeofday (&touchStart, NULL); |
| 324 | blankTimer.resetTimerAndUnblank(); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | key_repeat = 0; |
| 329 | touch_and_hold = 0; |
| 330 | touch_repeat = 0; |
| 331 | dontwait = 0; |
| 332 | blankTimer.resetTimerAndUnblank(); |
| 333 | } |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | // This is a key release |
| 338 | kb.KeyUp (ev.code); |
| 339 | key_repeat = 0; |
| 340 | touch_and_hold = 0; |
| 341 | touch_repeat = 0; |
| 342 | dontwait = 0; |
| 343 | blankTimer.resetTimerAndUnblank(); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // This special function will return immediately the first time, but then |
| 351 | // always returns 1/30th of a second (or immediately if called later) from |
| 352 | // the last time it was called |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 353 | static void |
| 354 | loopTimer (void) |
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 | static timespec lastCall; |
| 357 | static int initialized = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 359 | if (!initialized) |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 360 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 361 | clock_gettime (CLOCK_MONOTONIC, &lastCall); |
| 362 | initialized = 1; |
| 363 | return; |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 364 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 365 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 366 | do |
| 367 | { |
| 368 | timespec curTime; |
| 369 | clock_gettime (CLOCK_MONOTONIC, &curTime); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 370 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 371 | timespec diff = TWFunc::timespec_diff (lastCall, curTime); |
| 372 | |
| 373 | // This is really 30 times per second |
| 374 | if (diff.tv_sec || diff.tv_nsec > 33333333) |
| 375 | { |
| 376 | lastCall = curTime; |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // We need to sleep some period time microseconds |
| 381 | unsigned int sleepTime = 33333 - (diff.tv_nsec / 1000); |
| 382 | usleep (sleepTime); |
| 383 | } |
| 384 | while (1); |
| 385 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | } |
| 387 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 388 | static int |
| 389 | runPages (void) |
| 390 | { |
| 391 | // Raise the curtain |
| 392 | if (gCurtain != NULL) |
| 393 | { |
| 394 | gr_surface surface; |
| 395 | |
| 396 | PageManager::Render (); |
| 397 | gr_get_surface (&surface); |
| 398 | curtainRaise (surface); |
| 399 | gr_free_surface (surface); |
| 400 | } |
| 401 | |
| 402 | gGuiRunning = 1; |
| 403 | |
| 404 | DataManager::SetValue ("tw_loaded", 1); |
| 405 | |
| 406 | for (;;) |
| 407 | { |
| 408 | loopTimer (); |
| 409 | |
| 410 | if (!gForceRender) |
| 411 | { |
| 412 | int ret; |
| 413 | |
| 414 | ret = PageManager::Update (); |
| 415 | if (ret > 1) |
| 416 | PageManager::Render (); |
| 417 | |
| 418 | if (ret > 0) |
| 419 | flip (); |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | pthread_mutex_lock(&gForceRendermutex); |
| 424 | gForceRender = 0; |
| 425 | pthread_mutex_unlock(&gForceRendermutex); |
| 426 | PageManager::Render (); |
| 427 | flip (); |
| 428 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 429 | if (DataManager::GetIntValue("tw_gui_done") != 0) |
| 430 | { |
| 431 | break; |
| 432 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | gGuiRunning = 0; |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | runPage (const char *page_name) |
| 441 | { |
| 442 | gui_changePage (page_name); |
| 443 | |
| 444 | // Raise the curtain |
| 445 | if (gCurtain != NULL) |
| 446 | { |
| 447 | gr_surface surface; |
| 448 | |
| 449 | PageManager::Render (); |
| 450 | gr_get_surface (&surface); |
| 451 | curtainRaise (surface); |
| 452 | gr_free_surface (surface); |
| 453 | } |
| 454 | |
| 455 | gGuiRunning = 1; |
| 456 | |
| 457 | DataManager::SetValue ("tw_loaded", 1); |
| 458 | |
| 459 | for (;;) |
| 460 | { |
| 461 | loopTimer (); |
| 462 | |
| 463 | if (!gForceRender) |
| 464 | { |
| 465 | int ret; |
| 466 | |
| 467 | ret = PageManager::Update (); |
| 468 | if (ret > 1) |
| 469 | PageManager::Render (); |
| 470 | |
| 471 | if (ret > 0) |
| 472 | flip (); |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | pthread_mutex_lock(&gForceRendermutex); |
| 477 | gForceRender = 0; |
| 478 | pthread_mutex_unlock(&gForceRendermutex); |
| 479 | PageManager::Render (); |
| 480 | flip (); |
| 481 | } |
| 482 | if (DataManager::GetIntValue ("tw_page_done") != 0) |
| 483 | { |
| 484 | gui_changePage ("main"); |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | gGuiRunning = 0; |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | int |
| 494 | gui_forceRender (void) |
| 495 | { |
| 496 | pthread_mutex_lock(&gForceRendermutex); |
| 497 | gForceRender = 1; |
| 498 | pthread_mutex_unlock(&gForceRendermutex); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | int |
| 503 | gui_changePage (std::string newPage) |
| 504 | { |
| 505 | LOGI ("Set page: '%s'\n", newPage.c_str ()); |
| 506 | PageManager::ChangePage (newPage); |
| 507 | pthread_mutex_lock(&gForceRendermutex); |
| 508 | gForceRender = 1; |
| 509 | pthread_mutex_unlock(&gForceRendermutex); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int |
| 514 | gui_changeOverlay (std::string overlay) |
| 515 | { |
| 516 | PageManager::ChangeOverlay (overlay); |
| 517 | pthread_mutex_lock(&gForceRendermutex); |
| 518 | gForceRender = 1; |
| 519 | pthread_mutex_unlock(&gForceRendermutex); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | int |
| 524 | gui_changePackage (std::string newPackage) |
| 525 | { |
| 526 | PageManager::SelectPackage (newPackage); |
| 527 | pthread_mutex_lock(&gForceRendermutex); |
| 528 | gForceRender = 1; |
| 529 | pthread_mutex_unlock(&gForceRendermutex); |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | std::string gui_parse_text (string inText) |
| 534 | { |
| 535 | // Copied from std::string GUIText::parseText(void) |
| 536 | // This function parses text for DataManager values encompassed by %value% in the XML |
| 537 | static int counter = 0; |
| 538 | std::string str = inText; |
| 539 | size_t pos = 0; |
| 540 | size_t next = 0, end = 0; |
| 541 | |
| 542 | while (1) |
| 543 | { |
| 544 | next = str.find ('%', pos); |
| 545 | if (next == std::string::npos) |
| 546 | return str; |
| 547 | end = str.find ('%', next + 1); |
| 548 | if (end == std::string::npos) |
| 549 | return str; |
| 550 | |
| 551 | // We have a block of data |
| 552 | std::string var = str.substr (next + 1, (end - next) - 1); |
| 553 | str.erase (next, (end - next) + 1); |
| 554 | |
| 555 | if (next + 1 == end) |
| 556 | { |
| 557 | str.insert (next, 1, '%'); |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | std::string value; |
| 562 | if (DataManager::GetValue (var, value) == 0) |
| 563 | str.insert (next, value); |
| 564 | } |
| 565 | |
| 566 | pos = next + 1; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | extern "C" int |
| 571 | gui_init () |
| 572 | { |
| 573 | int fd; |
| 574 | |
| 575 | gr_init (); |
| 576 | |
| 577 | if (res_create_surface ("/res/images/curtain.jpg", &gCurtain)) |
| 578 | { |
| 579 | printf |
| 580 | ("Unable to locate '/res/images/curtain.jpg'\nDid you set a DEVICE_RESOLUTION in your config files?\n"); |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | curtainSet (); |
| 585 | |
| 586 | ev_init (); |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | extern "C" int |
| 591 | gui_loadResources () |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 592 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 593 | // unlink("/sdcard/video.last"); |
| 594 | // rename("/sdcard/video.bin", "/sdcard/video.last"); |
| 595 | // gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY); |
| 596 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 597 | int check = 0; |
| 598 | DataManager::GetValue (TW_IS_ENCRYPTED, check); |
| 599 | if (check) |
| 600 | { |
| 601 | if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "decrypt")) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 602 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 603 | LOGE ("Failed to load base packages.\n"); |
| 604 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 605 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 606 | else |
| 607 | check = 1; |
| 608 | } |
| 609 | if (check == 0 |
| 610 | && PageManager::LoadPackage ("TWRP", "/script/ui.xml", "main")) |
| 611 | { |
| 612 | std::string theme_path; |
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 | theme_path = DataManager::GetSettingsStoragePath (); |
| 615 | if (!PartitionManager.Mount_Settings_Storage (false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 616 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 617 | int retry_count = 5; |
| 618 | while (retry_count > 0 |
| 619 | && !PartitionManager.Mount_Settings_Storage (false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 620 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 621 | usleep (500000); |
| 622 | retry_count--; |
| 623 | } |
| 624 | if (!PartitionManager.Mount_Settings_Storage (false)) |
| 625 | { |
| 626 | LOGE ("Unable to mount %s during GUI startup.\n", |
| 627 | theme_path.c_str ()); |
| 628 | check = 1; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | theme_path += "/TWRP/theme/ui.zip"; |
| 633 | if (check || PageManager::LoadPackage ("TWRP", theme_path, "main")) |
| 634 | { |
| 635 | if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "main")) |
| 636 | { |
| 637 | LOGE ("Failed to load base packages.\n"); |
| 638 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 643 | // Set the default package |
| 644 | PageManager::SelectPackage ("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 645 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 646 | gGuiInitialized = 1; |
| 647 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 648 | |
| 649 | error: |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 650 | LOGE ("An internal error has occurred.\n"); |
| 651 | gGuiInitialized = 0; |
| 652 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 653 | } |
| 654 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 655 | extern "C" int |
| 656 | gui_start () |
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 | if (!gGuiInitialized) |
| 659 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 660 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 661 | gGuiConsoleTerminate = 1; |
| 662 | while (gGuiConsoleRunning) |
| 663 | loopTimer (); |
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 | // Set the default package |
| 666 | PageManager::SelectPackage ("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 667 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 668 | if (!gGuiInputRunning) |
| 669 | { |
| 670 | // Start by spinning off an input handler. |
| 671 | pthread_t t; |
| 672 | pthread_create (&t, NULL, input_thread, NULL); |
| 673 | gGuiInputRunning = 1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 674 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 675 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 676 | return runPages (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 677 | } |
| 678 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 679 | extern "C" int |
| 680 | gui_startPage (const char *page_name) |
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 | if (!gGuiInitialized) |
| 683 | return -1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 684 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 685 | gGuiConsoleTerminate = 1; |
| 686 | while (gGuiConsoleRunning) |
| 687 | loopTimer (); |
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 | // Set the default package |
| 690 | PageManager::SelectPackage ("TWRP"); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 691 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 692 | if (!gGuiInputRunning) |
| 693 | { |
| 694 | // Start by spinning off an input handler. |
| 695 | pthread_t t; |
| 696 | pthread_create (&t, NULL, input_thread, NULL); |
| 697 | gGuiInputRunning = 1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 698 | } |
| 699 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 700 | DataManager::SetValue ("tw_page_done", 0); |
| 701 | return runPage (page_name); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 702 | } |
| 703 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 704 | static void * |
| 705 | console_thread (void *cookie) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 706 | { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 707 | PageManager::SwitchToConsole (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 708 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 709 | while (!gGuiConsoleTerminate) |
| 710 | { |
| 711 | loopTimer (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 712 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 713 | if (!gForceRender) |
| 714 | { |
| 715 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 716 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 717 | ret = PageManager::Update (); |
| 718 | if (ret > 1) |
| 719 | PageManager::Render (); |
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 | flip (); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 723 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 724 | if (ret < 0) |
| 725 | LOGE ("An update request has failed.\n"); |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | pthread_mutex_lock(&gForceRendermutex); |
| 730 | gForceRender = 0; |
| 731 | pthread_mutex_unlock(&gForceRendermutex); |
| 732 | PageManager::Render (); |
| 733 | flip (); |
| 734 | } |
| 735 | } |
| 736 | gGuiConsoleRunning = 0; |
| 737 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 738 | } |
| 739 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 740 | extern "C" int |
| 741 | gui_console_only () |
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 | if (!gGuiInitialized) |
| 744 | return -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 | gGuiConsoleTerminate = 0; |
| 747 | gGuiConsoleRunning = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 748 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 749 | // Start by spinning off an input handler. |
| 750 | pthread_t t; |
| 751 | pthread_create (&t, NULL, console_thread, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 752 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 753 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 754 | } |