blob: 7fd474a35347185436eb6ba0b402439a83ca0af5 [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 }
426 }
427
428 gGuiRunning = 0;
429 return 0;
430}
431
432static int
433runPage (const char *page_name)
434{
435 gui_changePage (page_name);
436
437 // Raise the curtain
438 if (gCurtain != NULL)
439 {
440 gr_surface surface;
441
442 PageManager::Render ();
443 gr_get_surface (&surface);
444 curtainRaise (surface);
445 gr_free_surface (surface);
446 }
447
448 gGuiRunning = 1;
449
450 DataManager::SetValue ("tw_loaded", 1);
451
452 for (;;)
453 {
454 loopTimer ();
455
456 if (!gForceRender)
457 {
458 int ret;
459
460 ret = PageManager::Update ();
461 if (ret > 1)
462 PageManager::Render ();
463
464 if (ret > 0)
465 flip ();
466 }
467 else
468 {
469 pthread_mutex_lock(&gForceRendermutex);
470 gForceRender = 0;
471 pthread_mutex_unlock(&gForceRendermutex);
472 PageManager::Render ();
473 flip ();
474 }
475 if (DataManager::GetIntValue ("tw_page_done") != 0)
476 {
477 gui_changePage ("main");
478 break;
479 }
480 }
481
482 gGuiRunning = 0;
483 return 0;
484}
485
486int
487gui_forceRender (void)
488{
489 pthread_mutex_lock(&gForceRendermutex);
490 gForceRender = 1;
491 pthread_mutex_unlock(&gForceRendermutex);
492 return 0;
493}
494
495int
496gui_changePage (std::string newPage)
497{
498 LOGI ("Set page: '%s'\n", newPage.c_str ());
499 PageManager::ChangePage (newPage);
500 pthread_mutex_lock(&gForceRendermutex);
501 gForceRender = 1;
502 pthread_mutex_unlock(&gForceRendermutex);
503 return 0;
504}
505
506int
507gui_changeOverlay (std::string overlay)
508{
509 PageManager::ChangeOverlay (overlay);
510 pthread_mutex_lock(&gForceRendermutex);
511 gForceRender = 1;
512 pthread_mutex_unlock(&gForceRendermutex);
513 return 0;
514}
515
516int
517gui_changePackage (std::string newPackage)
518{
519 PageManager::SelectPackage (newPackage);
520 pthread_mutex_lock(&gForceRendermutex);
521 gForceRender = 1;
522 pthread_mutex_unlock(&gForceRendermutex);
523 return 0;
524}
525
526std::string gui_parse_text (string inText)
527{
528 // Copied from std::string GUIText::parseText(void)
529 // This function parses text for DataManager values encompassed by %value% in the XML
530 static int counter = 0;
531 std::string str = inText;
532 size_t pos = 0;
533 size_t next = 0, end = 0;
534
535 while (1)
536 {
537 next = str.find ('%', pos);
538 if (next == std::string::npos)
539 return str;
540 end = str.find ('%', next + 1);
541 if (end == std::string::npos)
542 return str;
543
544 // We have a block of data
545 std::string var = str.substr (next + 1, (end - next) - 1);
546 str.erase (next, (end - next) + 1);
547
548 if (next + 1 == end)
549 {
550 str.insert (next, 1, '%');
551 }
552 else
553 {
554 std::string value;
555 if (DataManager::GetValue (var, value) == 0)
556 str.insert (next, value);
557 }
558
559 pos = next + 1;
560 }
561}
562
563extern "C" int
564gui_init ()
565{
566 int fd;
567
568 gr_init ();
569
570 if (res_create_surface ("/res/images/curtain.jpg", &gCurtain))
571 {
572 printf
573 ("Unable to locate '/res/images/curtain.jpg'\nDid you set a DEVICE_RESOLUTION in your config files?\n");
574 return -1;
575 }
576
577 curtainSet ();
578
579 ev_init ();
580 return 0;
581}
582
583extern "C" int
584gui_loadResources ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400585{
Dees_Troy51a0e822012-09-05 15:24:24 -0400586// unlink("/sdcard/video.last");
587// rename("/sdcard/video.bin", "/sdcard/video.last");
588// gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY);
589
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500590 int check = 0;
591 DataManager::GetValue (TW_IS_ENCRYPTED, check);
592 if (check)
593 {
594 if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400595 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500596 LOGE ("Failed to load base packages.\n");
597 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500599 else
600 check = 1;
601 }
602 if (check == 0
603 && PageManager::LoadPackage ("TWRP", "/script/ui.xml", "main"))
604 {
605 std::string theme_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400606
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500607 theme_path = DataManager::GetSettingsStoragePath ();
608 if (!PartitionManager.Mount_Settings_Storage (false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500610 int retry_count = 5;
611 while (retry_count > 0
612 && !PartitionManager.Mount_Settings_Storage (false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500614 usleep (500000);
615 retry_count--;
616 }
617 if (!PartitionManager.Mount_Settings_Storage (false))
618 {
619 LOGE ("Unable to mount %s during GUI startup.\n",
620 theme_path.c_str ());
621 check = 1;
622 }
623 }
624
625 theme_path += "/TWRP/theme/ui.zip";
626 if (check || PageManager::LoadPackage ("TWRP", theme_path, "main"))
627 {
628 if (PageManager::LoadPackage ("TWRP", "/res/ui.xml", "main"))
629 {
630 LOGE ("Failed to load base packages.\n");
631 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 }
633 }
634 }
635
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500636 // Set the default package
637 PageManager::SelectPackage ("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400638
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500639 gGuiInitialized = 1;
640 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400641
642error:
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500643 LOGE ("An internal error has occurred.\n");
644 gGuiInitialized = 0;
645 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400646}
647
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500648extern "C" int
649gui_start ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400650{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500651 if (!gGuiInitialized)
652 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400653
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500654 gGuiConsoleTerminate = 1;
655 while (gGuiConsoleRunning)
656 loopTimer ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400657
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500658 // Set the default package
659 PageManager::SelectPackage ("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400660
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500661 if (!gGuiInputRunning)
662 {
663 // Start by spinning off an input handler.
664 pthread_t t;
665 pthread_create (&t, NULL, input_thread, NULL);
666 gGuiInputRunning = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000667 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400668
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500669 return runPages ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400670}
671
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500672extern "C" int
673gui_startPage (const char *page_name)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000674{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500675 if (!gGuiInitialized)
676 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000677
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500678 gGuiConsoleTerminate = 1;
679 while (gGuiConsoleRunning)
680 loopTimer ();
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000681
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500682 // Set the default package
683 PageManager::SelectPackage ("TWRP");
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000684
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500685 if (!gGuiInputRunning)
686 {
687 // Start by spinning off an input handler.
688 pthread_t t;
689 pthread_create (&t, NULL, input_thread, NULL);
690 gGuiInputRunning = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000691 }
692
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500693 DataManager::SetValue ("tw_page_done", 0);
694 return runPage (page_name);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000695}
696
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500697static void *
698console_thread (void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400699{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500700 PageManager::SwitchToConsole ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400701
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500702 while (!gGuiConsoleTerminate)
703 {
704 loopTimer ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400705
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500706 if (!gForceRender)
707 {
708 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400709
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500710 ret = PageManager::Update ();
711 if (ret > 1)
712 PageManager::Render ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500714 if (ret > 0)
715 flip ();
Dees_Troy51a0e822012-09-05 15:24:24 -0400716
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500717 if (ret < 0)
718 LOGE ("An update request has failed.\n");
719 }
720 else
721 {
722 pthread_mutex_lock(&gForceRendermutex);
723 gForceRender = 0;
724 pthread_mutex_unlock(&gForceRendermutex);
725 PageManager::Render ();
726 flip ();
727 }
728 }
729 gGuiConsoleRunning = 0;
730 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400731}
732
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500733extern "C" int
734gui_console_only ()
Dees_Troy51a0e822012-09-05 15:24:24 -0400735{
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500736 if (!gGuiInitialized)
737 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400738
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500739 gGuiConsoleTerminate = 0;
740 gGuiConsoleRunning = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400741
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500742 // Start by spinning off an input handler.
743 pthread_t t;
744 pthread_create (&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400745
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500746 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400747}