blob: d07c49ff44be796f55edd1ba2c637f5df85d3789 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
Dees Troy3be70a82013-10-22 14:25:12 +00002 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#include <fcntl.h>
27#include <sys/reboot.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/mman.h>
31#include <sys/types.h>
32#include <sys/ioctl.h>
33#include <sys/mount.h>
34#include <time.h>
35#include <unistd.h>
36#include <stdlib.h>
37
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050038extern "C"
39{
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include <pixelflinger/pixelflinger.h>
43}
44
45#include "rapidxml.hpp"
46#include "objects.hpp"
47#include "../data.hpp"
48#include "../variables.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040049#include "../partitions.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050050#include "../twrp-functions.hpp"
Ethan Yonker03a42f62014-08-08 11:03:51 -050051#include "../openrecoveryscript.hpp"
52#include "../orscmd/orscmd.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070053#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050054#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070055#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Boceke5ffcd12014-02-06 21:17:32 +010057// Enable to print render time of each frame to the log file
58//#define PRINT_RENDER_TIME 1
59
Dees_Troy51a0e822012-09-05 15:24:24 -040060const static int CURTAIN_FADE = 32;
61
62using namespace rapidxml;
63
64// Global values
65static gr_surface gCurtain = NULL;
66static int gGuiInitialized = 0;
67static int gGuiConsoleRunning = 0;
68static int gGuiConsoleTerminate = 0;
69static int gForceRender = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050070pthread_mutex_t gForceRendermutex;
Dees_Troyc8b199c2012-09-24 11:55:07 -040071static int gNoAnimation = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +000072static int gGuiInputRunning = 0;
Ethan Yonker03a42f62014-08-08 11:03:51 -050073static int gCmdLineRunning = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070074#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050075blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070076#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040077
78// Needed by pages.cpp too
79int gGuiRunning = 0;
80
81static int gRecorder = -1;
82
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083extern "C" void gr_write_frame_to_file(int fd);
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085void flip(void)
Dees_Troy51a0e822012-09-05 15:24:24 -040086{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 if (gRecorder != -1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050088 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 timespec time;
90 clock_gettime(CLOCK_MONOTONIC, &time);
91 write(gRecorder, &time, sizeof(timespec));
92 gr_write_frame_to_file(gRecorder);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050093 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -040095}
96
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097void rapidxml::parse_error_handler(const char *what, void *where)
Dees_Troy51a0e822012-09-05 15:24:24 -040098{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 fprintf(stderr, "Parser error: %s\n", what);
100 fprintf(stderr, " Start of string: %s\n",(char *) where);
101 LOGERR("Error parsing XML file.\n");
102 //abort();
Dees_Troy51a0e822012-09-05 15:24:24 -0400103}
104
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105static void curtainSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400106{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 gr_color(0, 0, 0, 255);
108 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
109 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
110 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113static void curtainRaise(gr_surface surface)
Dees_Troy51a0e822012-09-05 15:24:24 -0400114{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 int sy = 0;
116 int h = gr_get_height(gCurtain) - 1;
117 int w = gr_get_width(gCurtain);
118 int fy = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 int msw = gr_get_width(surface);
121 int msh = gr_get_height(surface);
122 int CURTAIN_RATE = msh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500125 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500127 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200128 gr_blit(surface, 0, 0, msw, msh, 0, 0);
129 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
130 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500131 }
132 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 gr_blit(surface, 0, 0, msw, msh, 0, 0);
134 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400135}
136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137void curtainClose()
Dees_Troy51a0e822012-09-05 15:24:24 -0400138{
139#if 0
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 int w = gr_get_width(gCurtain);
141 int h = 1;
142 int sy = gr_get_height(gCurtain) - 1;
143 int fbh = gr_fb_height();
144 int CURTAIN_RATE = fbh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500147 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500149 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
151 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500152 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
154 gr_get_height(gCurtain), 0, 0);
155 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (gRecorder != -1)
158 close(gRecorder);
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 int fade;
161 for (fade = 16; fade < 255; fade += CURTAIN_FADE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500162 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
164 gr_get_height(gCurtain), 0, 0);
165 gr_color(0, 0, 0, fade);
166 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
167 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500168 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 gr_color(0, 0, 0, 255);
170 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
171 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400172 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500173#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
175 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500176#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400177}
178
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200179static void * input_thread(void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400180{
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700181
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 int drag = 0;
183 static int touch_and_hold = 0, dontwait = 0;
184 static int touch_repeat = 0, key_repeat = 0;
185 static int x = 0, y = 0;
186 static int lshift = 0, rshift = 0;
187 static struct timeval touchStart;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 string seconds;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100189 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100190 MouseCursor *cursor = PageManager::GetMouseCursor();
Dees_Troy51a0e822012-09-05 15:24:24 -0400191
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700192#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 //start screen timeout threads
194 blankTimer.setTimerThread();
195 DataManager::GetValue("tw_screen_timeout_secs", seconds);
196 blankTimer.setTime(atoi(seconds.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700197#else
198 LOGINFO("Skipping screen timeout threads: TW_NO_SCREEN_TIMEOUT is set\n");
199#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400200
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500202 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 // wait for the next event
204 struct input_event ev;
205 int state = 0, ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400206
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200207 ret = ev_get(&ev, dontwait);
Dees_Troy51a0e822012-09-05 15:24:24 -0400208
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500210 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200211 struct timeval curTime;
212 gettimeofday(&curTime, NULL);
213 long mtime, seconds, useconds;
Dees_Troy51a0e822012-09-05 15:24:24 -0400214
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200215 seconds = curTime.tv_sec - touchStart.tv_sec;
216 useconds = curTime.tv_usec - touchStart.tv_usec;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500217
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
219 if (touch_and_hold && mtime > 500)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500220 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 touch_and_hold = 0;
222 touch_repeat = 1;
223 gettimeofday(&touchStart, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400224#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 LOGERR("TOUCH_HOLD: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400226#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700228#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700230#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400231 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 else if (touch_repeat && mtime > 100)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500233 {
234#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 LOGERR("TOUCH_REPEAT: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500236#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 gettimeofday(&touchStart, NULL);
238 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700239#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700241#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500242 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 else if (key_repeat == 1 && mtime > 500)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500244 {
245#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 LOGERR("KEY_HOLD: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500247#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 gettimeofday(&touchStart, NULL);
249 key_repeat = 2;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100250 kb->KeyRepeat();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700251#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700253#endif
254
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500255 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 else if (key_repeat == 2 && mtime > 100)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500257 {
258#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 LOGERR("KEY_REPEAT: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500260#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 gettimeofday(&touchStart, NULL);
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100262 kb->KeyRepeat();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700263#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700265#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500266 }
267 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 else if (ev.type == EV_ABS)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500269 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400270
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200271 x = ev.value >> 16;
272 y = ev.value & 0xFFFF;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 if (ev.code == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500275 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200276 if (state == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500277 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400278#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200279 LOGERR("TOUCH_RELEASE: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400280#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700282#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200283 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700284#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 touch_and_hold = 0;
286 touch_repeat = 0;
287 if (!key_repeat)
288 dontwait = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400289 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 state = 0;
291 drag = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500294 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (!drag)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500296 {
Dees Troy1eba56f2014-04-12 13:09:00 +0000297 if (x != 0 && y != 0) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500298#ifdef _EVENT_LOGGING
Dees Troy1eba56f2014-04-12 13:09:00 +0000299 LOGERR("TOUCH_START: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500300#endif
Dees Troy1eba56f2014-04-12 13:09:00 +0000301 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
302 state = 1;
303 drag = 1;
304 touch_and_hold = 1;
305 dontwait = 1;
306 key_repeat = 0;
307 gettimeofday(&touchStart, NULL);
308 }
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700309#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200310 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700311#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500312 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200313 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500314 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200315 if (state == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500316 {
317#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200318 LOGERR("TOUCH_DRAG: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500319#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200320 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
321 state = 1;
322 key_repeat = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700323#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200324 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700325#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500326 }
327 }
328 }
329 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200330 else if (ev.type == EV_KEY)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500331 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200332 // Handle key-press here
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500333#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 LOGERR("TOUCH_KEY: %d\n", ev.code);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500335#endif
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100336 // Left mouse button
337 if(ev.code == BTN_LEFT)
338 {
339 if(ev.value == 1)
340 {
341 cursor->GetPos(x, y);
342
343 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
344 state = 1;
345 drag = 1;
346 touch_and_hold = 1;
347 dontwait = 1;
348 key_repeat = 0;
349 gettimeofday(&touchStart, NULL);
350 }
351 else if(drag == 1)
352 {
353 if (state == 0)
354 {
355 cursor->GetPos(x, y);
356
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100357#ifdef _EVENT_LOGGING
358 LOGERR("TOUCH_RELEASE: %d,%d\n", x, y);
359#endif
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100360 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
361
362 touch_and_hold = 0;
363 touch_repeat = 0;
364 if (!key_repeat)
365 dontwait = 0;
366 }
367 state = 0;
368 drag = 0;
369 }
370 }
371 // side mouse button, often used for "back" function
372 else if(ev.code == BTN_SIDE)
373 {
374 if(ev.value == 1)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100375 kb->KeyDown(KEY_BACK);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100376 else
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100377 kb->KeyUp(KEY_BACK);
378 } else if (ev.value != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 // This is a key press
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100380 if (kb->KeyDown(ev.code)) {
381 // Key repeat is enabled for this key
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200382 key_repeat = 1;
383 touch_and_hold = 0;
384 touch_repeat = 0;
385 dontwait = 1;
386 gettimeofday(&touchStart, NULL);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700387#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200388 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700389#endif
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100390 } else {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200391 key_repeat = 0;
392 touch_and_hold = 0;
393 touch_repeat = 0;
394 dontwait = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700395#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200396 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700397#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500398 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100399 } else {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 // This is a key release
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100401 kb->KeyUp(ev.code);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200402 key_repeat = 0;
403 touch_and_hold = 0;
404 touch_repeat = 0;
405 dontwait = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700406#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200407 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700408#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500409 }
410 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100411 else if(ev.type == EV_REL)
412 {
413#ifdef _EVENT_LOGGING
414 LOGERR("EV_REL %d %d\n", ev.code, ev.value);
415#endif
416 if(ev.code == REL_X)
417 cursor->Move(ev.value, 0);
418 else if(ev.code == REL_Y)
419 cursor->Move(0, ev.value);
420
421 if(drag == 1) {
422 cursor->GetPos(x, y);
423#ifdef _EVENT_LOGGING
424 LOGERR("TOUCH_DRAG: %d, %d\n", x, y);
425#endif
426 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
427 state = 1;
428 key_repeat = 0;
429 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500430 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500431 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200432 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400433}
434
Ethan Yonker03a42f62014-08-08 11:03:51 -0500435static void * command_thread(void *cookie)
436{
437 int read_fd;
438 FILE* orsout;
439 char command[1024], result[512];
440
441 LOGINFO("Starting command line thread\n");
442
443 unlink(ORS_INPUT_FILE);
444 if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
445 LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
446 return 0;
447 }
448 unlink(ORS_OUTPUT_FILE);
449 if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
450 LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
451 unlink(ORS_INPUT_FILE);
452 return 0;
453 }
454
455 read_fd = open(ORS_INPUT_FILE, O_RDONLY);
456 if (read_fd < 0) {
457 LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
458 unlink(ORS_INPUT_FILE);
459 unlink(ORS_OUTPUT_FILE);
460 return 0;
461 }
462
463 while (!gGuiRunning)
464 sleep(1);
465
466 for (;;) {
467 while (read(read_fd, &command, sizeof(command)) > 0) {
468 command[1022] = '\n';
469 command[1023] = '\0';
470 LOGINFO("Command '%s' received\n", command);
471 orsout = fopen(ORS_OUTPUT_FILE, "w");
472 if (!orsout) {
473 close(read_fd);
474 LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
475 unlink(ORS_INPUT_FILE);
476 unlink(ORS_OUTPUT_FILE);
477 return 0;
478 }
479 if (DataManager::GetIntValue("tw_busy") != 0) {
480 strcpy(result, "Failed, operation in progress\n");
481 fprintf(orsout, "%s", result);
482 LOGINFO("Command cannot be performed, operation in progress.\n");
483 } else {
484 if (gui_console_only() == 0) {
485 LOGINFO("Console started successfully\n");
486 gui_set_FILE(orsout);
487 if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) {
488 char* filename = command + 11;
489 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
490 LOGERR("Unable to copy script file\n");
491 } else {
492 OpenRecoveryScript::run_script_file();
493 }
494 } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) {
495 char* varname = command + 4;
496 string temp;
497 DataManager::GetValue(varname, temp);
498 gui_print("%s = %s\n", varname, temp.c_str());
499 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
500 OpenRecoveryScript::run_script_file();
501 }
502 gui_set_FILE(NULL);
503 gGuiConsoleTerminate = 1;
504 }
505 }
506 fclose(orsout);
507 }
508 }
509 close(read_fd);
510 LOGINFO("Command thread exiting\n");
511 return 0;
512}
513
Dees_Troy51a0e822012-09-05 15:24:24 -0400514// This special function will return immediately the first time, but then
515// always returns 1/30th of a second (or immediately if called later) from
516// the last time it was called
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200517static void loopTimer(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400518{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200519 static timespec lastCall;
520 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400521
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200522 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400523 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200524 clock_gettime(CLOCK_MONOTONIC, &lastCall);
525 initialized = 1;
526 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400527 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500530 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200531 timespec curTime;
532 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400533
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200534 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500535
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200536 // This is really 30 times per second
537 if (diff.tv_sec || diff.tv_nsec > 33333333)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500538 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200539 lastCall = curTime;
540 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500541 }
542
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200543 // We need to sleep some period time microseconds
544 unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
545 usleep(sleepTime);
546 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400547}
548
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200549static int runPages(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500550{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200551 // Raise the curtain
552 if (gCurtain != NULL)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500553 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200554 gr_surface surface;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500555
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200556 PageManager::Render();
557 gr_get_surface(&surface);
558 curtainRaise(surface);
559 gr_free_surface(surface);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500560 }
561
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200562 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500563
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200564 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500565
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100566#ifdef PRINT_RENDER_TIME
567 timespec start, end;
568 int32_t render_t, flip_t;
569#endif
570
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200571 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500572 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200573 loopTimer();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500574
Ethan Yonker03a42f62014-08-08 11:03:51 -0500575 if (gGuiConsoleRunning) {
576 continue;
577 }
578
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200579 if (!gForceRender)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500580 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200581 int ret;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500582
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 ret = PageManager::Update();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100584
585#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200586 if (ret > 1)
587 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500588
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200589 if (ret > 0)
590 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100591#else
592 if (ret > 1)
593 {
594 clock_gettime(CLOCK_MONOTONIC, &start);
595 PageManager::Render();
596 clock_gettime(CLOCK_MONOTONIC, &end);
597 render_t = TWFunc::timespec_diff_ms(start, end);
598
599 flip();
600 clock_gettime(CLOCK_MONOTONIC, &start);
601 flip_t = TWFunc::timespec_diff_ms(end, start);
602
603 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
604 }
605 else if(ret == 1)
606 flip();
607#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500608 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200609 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500610 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200611 pthread_mutex_lock(&gForceRendermutex);
612 gForceRender = 0;
613 pthread_mutex_unlock(&gForceRendermutex);
614 PageManager::Render();
615 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500616 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200617
Dees_Troy6ef66352013-02-21 08:26:57 -0600618 if (DataManager::GetIntValue("tw_gui_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200619 break;
620 }
621
622 gGuiRunning = 0;
623 return 0;
624}
625
626static int runPage(const char *page_name)
627{
628 gui_changePage(page_name);
629
630 // Raise the curtain
631 if (gCurtain != NULL)
632 {
633 gr_surface surface;
634
635 PageManager::Render();
636 gr_get_surface(&surface);
637 curtainRaise(surface);
638 gr_free_surface(surface);
639 }
640
641 gGuiRunning = 1;
642
643 DataManager::SetValue("tw_loaded", 1);
644
645 for (;;)
646 {
647 loopTimer();
648
649 if (!gForceRender)
Dees_Troy6ef66352013-02-21 08:26:57 -0600650 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200651 int ret;
652
653 ret = PageManager::Update();
654 if (ret > 1)
655 PageManager::Render();
656
657 if (ret > 0)
658 flip();
659 }
660 else
661 {
662 pthread_mutex_lock(&gForceRendermutex);
663 gForceRender = 0;
664 pthread_mutex_unlock(&gForceRendermutex);
665 PageManager::Render();
666 flip();
667 }
668 if (DataManager::GetIntValue("tw_page_done") != 0)
669 {
670 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600671 break;
672 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500673 }
674
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200675 gGuiRunning = 0;
676 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500677}
678
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200679int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500680{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200681 pthread_mutex_lock(&gForceRendermutex);
682 gForceRender = 1;
683 pthread_mutex_unlock(&gForceRendermutex);
684 return 0;
685}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500686
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200687int gui_changePage(std::string newPage)
688{
689 LOGINFO("Set page: '%s'\n", newPage.c_str());
690 PageManager::ChangePage(newPage);
691 pthread_mutex_lock(&gForceRendermutex);
692 gForceRender = 1;
693 pthread_mutex_unlock(&gForceRendermutex);
694 return 0;
695}
696
697int gui_changeOverlay(std::string overlay)
698{
699 PageManager::ChangeOverlay(overlay);
700 pthread_mutex_lock(&gForceRendermutex);
701 gForceRender = 1;
702 pthread_mutex_unlock(&gForceRendermutex);
703 return 0;
704}
705
706int gui_changePackage(std::string newPackage)
707{
708 PageManager::SelectPackage(newPackage);
709 pthread_mutex_lock(&gForceRendermutex);
710 gForceRender = 1;
711 pthread_mutex_unlock(&gForceRendermutex);
712 return 0;
713}
714
715std::string gui_parse_text(string inText)
716{
717 // Copied from std::string GUIText::parseText(void)
718 // This function parses text for DataManager values encompassed by %value% in the XML
719 static int counter = 0;
720 std::string str = inText;
721 size_t pos = 0;
722 size_t next = 0, end = 0;
723
724 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500725 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200726 next = str.find('%', pos);
727 if (next == std::string::npos)
728 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500729
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200730 end = str.find('%', next + 1);
731 if (end == std::string::npos)
732 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500733
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734 // We have a block of data
735 std::string var = str.substr(next + 1,(end - next) - 1);
736 str.erase(next,(end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500737
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200738 if (next + 1 == end)
739 str.insert(next, 1, '%');
740 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500741 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200742 std::string value;
743 if (DataManager::GetValue(var, value) == 0)
744 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500745 }
746
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200747 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500748 }
749}
750
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200751extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500752{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200753 int fd;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500754
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200755 gr_init();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500756
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200757 if (res_create_surface("/res/images/curtain.jpg", &gCurtain))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500758 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200759 printf
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500760 ("Unable to locate '/res/images/curtain.jpg'\nDid you set a DEVICE_RESOLUTION in your config files?\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200761 return -1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500762 }
763
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200764 curtainSet();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500765
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200766 ev_init();
767 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500768}
769
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200770extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400771{
Ethan Yonker83e82572014-04-04 10:59:28 -0500772#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200773 int check = 0;
774 DataManager::GetValue(TW_IS_ENCRYPTED, check);
775 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500776 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200777 if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400778 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200779 LOGERR("Failed to load base packages.\n");
780 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400781 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200782 else
783 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500784 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400785
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200786 if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main"))
787 {
788 std::string theme_path;
789
790 theme_path = DataManager::GetSettingsStoragePath();
791 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400792 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200793 int retry_count = 5;
794 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200796 usleep(500000);
797 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500798 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200799
800 if (!PartitionManager.Mount_Settings_Storage(false))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500801 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200802 LOGERR("Unable to mount %s during GUI startup.\n",
803 theme_path.c_str());
804 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500805 }
806 }
807
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200808 theme_path += "/TWRP/theme/ui.zip";
809 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500810 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500811#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200812 if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500813 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200814 LOGERR("Failed to load base packages.\n");
815 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400816 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500817#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400818 }
819 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500820#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200821 // Set the default package
822 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400823
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 gGuiInitialized = 1;
825 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826
827error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500828 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200829 gGuiInitialized = 0;
830 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400831}
832
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200833extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400834{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200835 if (!gGuiInitialized)
836 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400837
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200838 gGuiConsoleTerminate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400839
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200840 while (gGuiConsoleRunning)
841 loopTimer();
Dees_Troy51a0e822012-09-05 15:24:24 -0400842
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200843 // Set the default package
844 PageManager::SelectPackage("TWRP");
845
846 if (!gGuiInputRunning)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500847 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200848 // Start by spinning off an input handler.
849 pthread_t t;
850 pthread_create(&t, NULL, input_thread, NULL);
851 gGuiInputRunning = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000852 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500853#ifndef TW_OEM_BUILD
854 if (!gCmdLineRunning)
855 {
856 // Start by spinning off an input handler.
857 pthread_t t;
858 pthread_create(&t, NULL, command_thread, NULL);
859 gCmdLineRunning = 1;
860 }
861#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200862 return runPages();
Dees_Troy51a0e822012-09-05 15:24:24 -0400863}
864
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200865extern "C" int gui_startPage(const char *page_name)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000866{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200867 if (!gGuiInitialized)
868 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000869
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200870 gGuiConsoleTerminate = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000871
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200872 while (gGuiConsoleRunning)
873 loopTimer();
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000874
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200875 // Set the default package
876 PageManager::SelectPackage("TWRP");
877
878 if (!gGuiInputRunning)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500879 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200880 // Start by spinning off an input handler.
881 pthread_t t;
882 pthread_create(&t, NULL, input_thread, NULL);
883 gGuiInputRunning = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000884 }
885
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200886 DataManager::SetValue("tw_page_done", 0);
887 return runPage(page_name);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000888}
889
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200890static void * console_thread(void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400891{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200892 PageManager::SwitchToConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400893
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200894 while (!gGuiConsoleTerminate)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500895 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200896 loopTimer();
Dees_Troy51a0e822012-09-05 15:24:24 -0400897
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200898 if (!gForceRender)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500899 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200900 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400901
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200902 ret = PageManager::Update();
903 if (ret > 1)
904 PageManager::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400905
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200906 if (ret > 0)
907 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400908
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200909 if (ret < 0)
910 LOGERR("An update request has failed.\n");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500911 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200912 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500913 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200914 pthread_mutex_lock(&gForceRendermutex);
915 gForceRender = 0;
916 pthread_mutex_unlock(&gForceRendermutex);
917 PageManager::Render();
918 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500919 }
920 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200921 gGuiConsoleRunning = 0;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500922 gForceRender = 1; // this will kickstart the GUI to render again
923 PageManager::EndConsole();
924 LOGINFO("Console stopping\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200925 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400926}
927
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200928extern "C" int gui_console_only(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400929{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200930 if (!gGuiInitialized)
931 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400932
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200933 gGuiConsoleTerminate = 0;
Ethan Yonkerffbd6ff2014-10-22 10:40:40 -0500934
935 if (gGuiConsoleRunning)
936 return 0;
937
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200938 gGuiConsoleRunning = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400939
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200940 // Start by spinning off an input handler.
941 pthread_t t;
942 pthread_create(&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400943
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200944 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400945}