blob: f08d68be3441f8c6b080cf581c0428357c648364 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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 bigbiff8a68c312013-02-10 14:28:30 -050036extern "C"
37{
Dees_Troy51a0e822012-09-05 15:24:24 -040038#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_Troy5bf43922012-09-07 16:07:55 -040050#include "../partitions.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050051#include "../twrp-functions.hpp"
52#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040053
Dees_Troy51a0e822012-09-05 15:24:24 -040054const static int CURTAIN_FADE = 32;
55
56using namespace rapidxml;
57
58// Global values
59static gr_surface gCurtain = NULL;
60static int gGuiInitialized = 0;
61static int gGuiConsoleRunning = 0;
62static int gGuiConsoleTerminate = 0;
63static int gForceRender = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050064pthread_mutex_t gForceRendermutex;
Dees_Troyc8b199c2012-09-24 11:55:07 -040065static int gNoAnimation = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +000066static int gGuiInputRunning = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050067blanktimer blankTimer;
Dees_Troy51a0e822012-09-05 15:24:24 -040068
69// Needed by pages.cpp too
70int gGuiRunning = 0;
71
72static int gRecorder = -1;
73
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050074extern "C" void gr_write_frame_to_file (int fd);
Dees_Troy51a0e822012-09-05 15:24:24 -040075
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050076void
77flip (void)
Dees_Troy51a0e822012-09-05 15:24:24 -040078{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050079 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_Troy51a0e822012-09-05 15:24:24 -040088}
89
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050090void
91rapidxml::parse_error_handler (const char *what, void *where)
Dees_Troy51a0e822012-09-05 15:24:24 -040092{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050093 fprintf (stderr, "Parser error: %s\n", what);
94 fprintf (stderr, " Start of string: %s\n", (char *) where);
95 abort ();
Dees_Troy51a0e822012-09-05 15:24:24 -040096}
97
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050098static void
99curtainSet ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400100{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500101 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_Troy51a0e822012-09-05 15:24:24 -0400107}
108
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500109static void
110curtainRaise (gr_surface surface)
Dees_Troy51a0e822012-09-05 15:24:24 -0400111{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500112 int sy = 0;
113 int h = gr_get_height (gCurtain) - 1;
114 int w = gr_get_width (gCurtain);
115 int fy = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500117 int msw = gr_get_width (surface);
118 int msh = gr_get_height (surface);
119 int CURTAIN_RATE = msh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500121 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_Troy51a0e822012-09-05 15:24:24 -0400133}
134
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500135void
136curtainClose ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400137{
138#if 0
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500139 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_Troy51a0e822012-09-05 15:24:24 -0400144
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500145 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_Troy51a0e822012-09-05 15:24:24 -0400155
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500156 if (gRecorder != -1)
157 close (gRecorder);
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500159 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_Troy51a0e822012-09-05 15:24:24 -0400171 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500172#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_Troy51a0e822012-09-05 15:24:24 -0400178}
179
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500180static void *
181input_thread (void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400182{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500183 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 bigbifff8e2f372013-02-27 20:50:43 -0500188 string seconds;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500190 //start screen timeout threads
191 blankTimer.setTimerThread();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500192 DataManager::GetValue("tw_screen_timeout_secs", seconds);
193 blankTimer.setTime(atoi(seconds.c_str()));
Dees_Troy51a0e822012-09-05 15:24:24 -0400194
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500195 for (;;)
196 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400197
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500198 // wait for the next event
199 struct input_event ev;
200 int state = 0, ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400201
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500202 ret = ev_get (&ev, dontwait);
Dees_Troy51a0e822012-09-05 15:24:24 -0400203
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500204 if (ret < 0)
205 {
206 struct timeval curTime;
207 gettimeofday (&curTime, NULL);
208 long mtime, seconds, useconds;
Dees_Troy51a0e822012-09-05 15:24:24 -0400209
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500210 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_Troy51a0e822012-09-05 15:24:24 -0400219#ifdef _EVENT_LOGGING
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500220 LOGE ("TOUCH_HOLD: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400221#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500222 PageManager::NotifyTouch (TOUCH_HOLD, x, y);
223 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500225 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_Troy51a0e822012-09-05 15:24:24 -0400256
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500257 x = ev.value >> 16;
258 y = ev.value & 0xFFFF;
Dees_Troy51a0e822012-09-05 15:24:24 -0400259
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500260 if (ev.code == 0)
261 {
262 if (state == 0)
263 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400264#ifdef _EVENT_LOGGING
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500265 LOGE ("TOUCH_RELEASE: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400266#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500267 PageManager::NotifyTouch (TOUCH_RELEASE, x, y);
268 blankTimer.resetTimerAndUnblank();
269 touch_and_hold = 0;
270 touch_repeat = 0;
271 if (!key_repeat)
Dees_Troy51a0e822012-09-05 15:24:24 -0400272 dontwait = 0;
273 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500274 state = 0;
275 drag = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400276 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500277 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_Troy51a0e822012-09-05 15:24:24 -0400348}
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 bigbiff8a68c312013-02-10 14:28:30 -0500353static void
354loopTimer (void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400355{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500356 static timespec lastCall;
357 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500359 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400360 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500361 clock_gettime (CLOCK_MONOTONIC, &lastCall);
362 initialized = 1;
363 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400364 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400365
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500366 do
367 {
368 timespec curTime;
369 clock_gettime (CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500371 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_Troy51a0e822012-09-05 15:24:24 -0400386}
387
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500388static int
389runPages (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_Troy6ef66352013-02-21 08:26:57 -0600429 if (DataManager::GetIntValue("tw_gui_done") != 0)
430 {
431 break;
432 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500433 }
434
435 gGuiRunning = 0;
436 return 0;
437}
438
439static int
440runPage (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
493int
494gui_forceRender (void)
495{
496 pthread_mutex_lock(&gForceRendermutex);
497 gForceRender = 1;
498 pthread_mutex_unlock(&gForceRendermutex);
499 return 0;
500}
501
502int
503gui_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
513int
514gui_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
523int
524gui_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
533std::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
570extern "C" int
571gui_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
590extern "C" int
591gui_loadResources ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400592{
Dees_Troy51a0e822012-09-05 15:24:24 -0400593// unlink("/sdcard/video.last");
594// rename("/sdcard/video.bin", "/sdcard/video.last");
595// gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY);
596
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500597 int check = 0;
598 DataManager::GetValue (TW_IS_ENCRYPTED, check);
599 if (check)
600 {
601 if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400602 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500603 LOGE ("Failed to load base packages.\n");
604 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400605 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500606 else
607 check = 1;
608 }
609 if (check == 0
610 && PageManager::LoadPackage ("TWRP", "/script/ui.xml", "main"))
611 {
612 std::string theme_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400613
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500614 theme_path = DataManager::GetSettingsStoragePath ();
615 if (!PartitionManager.Mount_Settings_Storage (false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500617 int retry_count = 5;
618 while (retry_count > 0
619 && !PartitionManager.Mount_Settings_Storage (false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400620 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500621 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_Troy51a0e822012-09-05 15:24:24 -0400639 }
640 }
641 }
642
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500643 // Set the default package
644 PageManager::SelectPackage ("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400645
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500646 gGuiInitialized = 1;
647 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400648
649error:
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500650 LOGE ("An internal error has occurred.\n");
651 gGuiInitialized = 0;
652 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400653}
654
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500655extern "C" int
656gui_start ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400657{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500658 if (!gGuiInitialized)
659 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400660
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500661 gGuiConsoleTerminate = 1;
662 while (gGuiConsoleRunning)
663 loopTimer ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400664
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500665 // Set the default package
666 PageManager::SelectPackage ("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400667
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500668 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_Troy4bc09ae2013-01-18 17:00:54 +0000674 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400675
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500676 return runPages ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400677}
678
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500679extern "C" int
680gui_startPage (const char *page_name)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000681{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500682 if (!gGuiInitialized)
683 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000684
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500685 gGuiConsoleTerminate = 1;
686 while (gGuiConsoleRunning)
687 loopTimer ();
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000688
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500689 // Set the default package
690 PageManager::SelectPackage ("TWRP");
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000691
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500692 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_Troy4bc09ae2013-01-18 17:00:54 +0000698 }
699
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500700 DataManager::SetValue ("tw_page_done", 0);
701 return runPage (page_name);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000702}
703
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500704static void *
705console_thread (void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400706{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500707 PageManager::SwitchToConsole ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400708
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500709 while (!gGuiConsoleTerminate)
710 {
711 loopTimer ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400712
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500713 if (!gForceRender)
714 {
715 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400716
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500717 ret = PageManager::Update ();
718 if (ret > 1)
719 PageManager::Render ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500721 if (ret > 0)
722 flip ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400723
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500724 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_Troy51a0e822012-09-05 15:24:24 -0400738}
739
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500740extern "C" int
741gui_console_only ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400742{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500743 if (!gGuiInitialized)
744 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500746 gGuiConsoleTerminate = 0;
747 gGuiConsoleRunning = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400748
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500749 // Start by spinning off an input handler.
750 pthread_t t;
751 pthread_create (&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400752
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500753 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400754}