blob: 4788fe0dfb95f20d5cd65362a7e0fe6c1669e5c4 [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;
Dees_Troy51a0e822012-09-05 15:24:24 -0400188
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500189 //start screen timeout threads
190 blankTimer.setTimerThread();
Dees_Troy51a0e822012-09-05 15:24:24 -0400191
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500192 for (;;)
193 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400194
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500195 // wait for the next event
196 struct input_event ev;
197 int state = 0, ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400198
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500199 ret = ev_get (&ev, dontwait);
Dees_Troy51a0e822012-09-05 15:24:24 -0400200
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500201 if (ret < 0)
202 {
203 struct timeval curTime;
204 gettimeofday (&curTime, NULL);
205 long mtime, seconds, useconds;
Dees_Troy51a0e822012-09-05 15:24:24 -0400206
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500207 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_Troy51a0e822012-09-05 15:24:24 -0400216#ifdef _EVENT_LOGGING
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500217 LOGE ("TOUCH_HOLD: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400218#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500219 PageManager::NotifyTouch (TOUCH_HOLD, x, y);
220 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400221 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500222 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_Troy51a0e822012-09-05 15:24:24 -0400253
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500254 x = ev.value >> 16;
255 y = ev.value & 0xFFFF;
Dees_Troy51a0e822012-09-05 15:24:24 -0400256
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500257 if (ev.code == 0)
258 {
259 if (state == 0)
260 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400261#ifdef _EVENT_LOGGING
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500262 LOGE ("TOUCH_RELEASE: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400263#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500264 PageManager::NotifyTouch (TOUCH_RELEASE, x, y);
265 blankTimer.resetTimerAndUnblank();
266 touch_and_hold = 0;
267 touch_repeat = 0;
268 if (!key_repeat)
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 dontwait = 0;
270 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500271 state = 0;
272 drag = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500274 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_Troy51a0e822012-09-05 15:24:24 -0400345}
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 bigbiff8a68c312013-02-10 14:28:30 -0500350static void
351loopTimer (void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400352{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500353 static timespec lastCall;
354 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500356 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400357 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500358 clock_gettime (CLOCK_MONOTONIC, &lastCall);
359 initialized = 1;
360 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400361 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400362
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500363 do
364 {
365 timespec curTime;
366 clock_gettime (CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500368 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_Troy51a0e822012-09-05 15:24:24 -0400383}
384
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500385static int
386runPages (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_Troy6ef66352013-02-21 08:26:57 -0600426 if (DataManager::GetIntValue("tw_gui_done") != 0)
427 {
428 break;
429 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500430 }
431
432 gGuiRunning = 0;
433 return 0;
434}
435
436static int
437runPage (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
490int
491gui_forceRender (void)
492{
493 pthread_mutex_lock(&gForceRendermutex);
494 gForceRender = 1;
495 pthread_mutex_unlock(&gForceRendermutex);
496 return 0;
497}
498
499int
500gui_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
510int
511gui_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
520int
521gui_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
530std::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
567extern "C" int
568gui_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
587extern "C" int
588gui_loadResources ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400589{
Dees_Troy51a0e822012-09-05 15:24:24 -0400590// unlink("/sdcard/video.last");
591// rename("/sdcard/video.bin", "/sdcard/video.last");
592// gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY);
593
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500594 int check = 0;
595 DataManager::GetValue (TW_IS_ENCRYPTED, check);
596 if (check)
597 {
598 if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400599 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500600 LOGE ("Failed to load base packages.\n");
601 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400602 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500603 else
604 check = 1;
605 }
606 if (check == 0
607 && PageManager::LoadPackage ("TWRP", "/script/ui.xml", "main"))
608 {
609 std::string theme_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400610
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500611 theme_path = DataManager::GetSettingsStoragePath ();
612 if (!PartitionManager.Mount_Settings_Storage (false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500614 int retry_count = 5;
615 while (retry_count > 0
616 && !PartitionManager.Mount_Settings_Storage (false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400617 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500618 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_Troy51a0e822012-09-05 15:24:24 -0400636 }
637 }
638 }
639
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500640 // Set the default package
641 PageManager::SelectPackage ("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400642
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500643 gGuiInitialized = 1;
644 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400645
646error:
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500647 LOGE ("An internal error has occurred.\n");
648 gGuiInitialized = 0;
649 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400650}
651
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500652extern "C" int
653gui_start ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400654{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500655 if (!gGuiInitialized)
656 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400657
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500658 gGuiConsoleTerminate = 1;
659 while (gGuiConsoleRunning)
660 loopTimer ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400661
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500662 // Set the default package
663 PageManager::SelectPackage ("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400664
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500665 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_Troy4bc09ae2013-01-18 17:00:54 +0000671 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400672
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500673 return runPages ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400674}
675
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500676extern "C" int
677gui_startPage (const char *page_name)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000678{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500679 if (!gGuiInitialized)
680 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000681
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500682 gGuiConsoleTerminate = 1;
683 while (gGuiConsoleRunning)
684 loopTimer ();
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000685
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500686 // Set the default package
687 PageManager::SelectPackage ("TWRP");
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000688
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500689 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_Troy4bc09ae2013-01-18 17:00:54 +0000695 }
696
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500697 DataManager::SetValue ("tw_page_done", 0);
698 return runPage (page_name);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000699}
700
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500701static void *
702console_thread (void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400703{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500704 PageManager::SwitchToConsole ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400705
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500706 while (!gGuiConsoleTerminate)
707 {
708 loopTimer ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400709
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500710 if (!gForceRender)
711 {
712 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500714 ret = PageManager::Update ();
715 if (ret > 1)
716 PageManager::Render ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400717
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500718 if (ret > 0)
719 flip ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500721 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_Troy51a0e822012-09-05 15:24:24 -0400735}
736
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500737extern "C" int
738gui_console_only ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400739{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500740 if (!gGuiInitialized)
741 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400742
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500743 gGuiConsoleTerminate = 0;
744 gGuiConsoleRunning = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500746 // Start by spinning off an input handler.
747 pthread_t t;
748 pthread_create (&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400749
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500750 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400751}