blob: 912899f07ddb94f9607219002d62104c16633add [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 Troyb7ae0982013-09-10 20:47:35 +000042#ifdef HAVE_SELINUX
Dees_Troy51a0e822012-09-05 15:24:24 -040043#include "../minzip/Zip.h"
Dees Troyb7ae0982013-09-10 20:47:35 +000044#else
45#include "../minzipold/Zip.h"
46#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040047#include <pixelflinger/pixelflinger.h>
48}
49
50#include "rapidxml.hpp"
51#include "objects.hpp"
52#include "../data.hpp"
53#include "../variables.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040054#include "../partitions.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050055#include "../twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070056#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050057#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070058#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Vojtech Boceke5ffcd12014-02-06 21:17:32 +010060// Enable to print render time of each frame to the log file
61//#define PRINT_RENDER_TIME 1
62
Dees_Troy51a0e822012-09-05 15:24:24 -040063const static int CURTAIN_FADE = 32;
64
65using namespace rapidxml;
66
67// Global values
68static gr_surface gCurtain = NULL;
69static int gGuiInitialized = 0;
70static int gGuiConsoleRunning = 0;
71static int gGuiConsoleTerminate = 0;
72static int gForceRender = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050073pthread_mutex_t gForceRendermutex;
Dees_Troyc8b199c2012-09-24 11:55:07 -040074static int gNoAnimation = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +000075static int gGuiInputRunning = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070076#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050077blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070078#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040079
80// Needed by pages.cpp too
81int gGuiRunning = 0;
82
83static int gRecorder = -1;
84
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085extern "C" void gr_write_frame_to_file(int fd);
Dees_Troy51a0e822012-09-05 15:24:24 -040086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087void flip(void)
Dees_Troy51a0e822012-09-05 15:24:24 -040088{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 if (gRecorder != -1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050090 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 timespec time;
92 clock_gettime(CLOCK_MONOTONIC, &time);
93 write(gRecorder, &time, sizeof(timespec));
94 gr_write_frame_to_file(gRecorder);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050095 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -040097}
98
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099void rapidxml::parse_error_handler(const char *what, void *where)
Dees_Troy51a0e822012-09-05 15:24:24 -0400100{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 fprintf(stderr, "Parser error: %s\n", what);
102 fprintf(stderr, " Start of string: %s\n",(char *) where);
103 LOGERR("Error parsing XML file.\n");
104 //abort();
Dees_Troy51a0e822012-09-05 15:24:24 -0400105}
106
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107static void curtainSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400108{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 gr_color(0, 0, 0, 255);
110 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
111 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
112 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400113}
114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115static void curtainRaise(gr_surface surface)
Dees_Troy51a0e822012-09-05 15:24:24 -0400116{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 int sy = 0;
118 int h = gr_get_height(gCurtain) - 1;
119 int w = gr_get_width(gCurtain);
120 int fy = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 int msw = gr_get_width(surface);
123 int msh = gr_get_height(surface);
124 int CURTAIN_RATE = msh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500127 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200128 for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500129 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 gr_blit(surface, 0, 0, msw, msh, 0, 0);
131 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
132 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500133 }
134 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 gr_blit(surface, 0, 0, msw, msh, 0, 0);
136 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139void curtainClose()
Dees_Troy51a0e822012-09-05 15:24:24 -0400140{
141#if 0
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 int w = gr_get_width(gCurtain);
143 int h = 1;
144 int sy = gr_get_height(gCurtain) - 1;
145 int fbh = gr_fb_height();
146 int CURTAIN_RATE = fbh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500149 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500151 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
153 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500154 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
156 gr_get_height(gCurtain), 0, 0);
157 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 if (gRecorder != -1)
160 close(gRecorder);
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 int fade;
163 for (fade = 16; fade < 255; fade += CURTAIN_FADE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500164 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
166 gr_get_height(gCurtain), 0, 0);
167 gr_color(0, 0, 0, fade);
168 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
169 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500170 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 gr_color(0, 0, 0, 255);
172 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
173 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400174 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500175#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
177 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500178#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400179}
180
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181static void * input_thread(void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400182{
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 int drag = 0;
185 static int touch_and_hold = 0, dontwait = 0;
186 static int touch_repeat = 0, key_repeat = 0;
187 static int x = 0, y = 0;
188 static int lshift = 0, rshift = 0;
189 static struct timeval touchStart;
190 HardwareKeyboard kb;
191 string seconds;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100192 MouseCursor *cursor = PageManager::GetMouseCursor();
Dees_Troy51a0e822012-09-05 15:24:24 -0400193
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700194#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200195 //start screen timeout threads
196 blankTimer.setTimerThread();
197 DataManager::GetValue("tw_screen_timeout_secs", seconds);
198 blankTimer.setTime(atoi(seconds.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700199#else
200 LOGINFO("Skipping screen timeout threads: TW_NO_SCREEN_TIMEOUT is set\n");
201#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500204 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 // wait for the next event
206 struct input_event ev;
207 int state = 0, ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400208
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 ret = ev_get(&ev, dontwait);
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200211 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500212 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 struct timeval curTime;
214 gettimeofday(&curTime, NULL);
215 long mtime, seconds, useconds;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 seconds = curTime.tv_sec - touchStart.tv_sec;
218 useconds = curTime.tv_usec - touchStart.tv_usec;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500219
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
221 if (touch_and_hold && mtime > 500)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500222 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200223 touch_and_hold = 0;
224 touch_repeat = 1;
225 gettimeofday(&touchStart, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400226#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 LOGERR("TOUCH_HOLD: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400228#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700230#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700232#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400233 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 else if (touch_repeat && mtime > 100)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500235 {
236#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 LOGERR("TOUCH_REPEAT: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500238#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 gettimeofday(&touchStart, NULL);
240 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700241#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700243#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500244 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 else if (key_repeat == 1 && mtime > 500)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500246 {
247#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 LOGERR("KEY_HOLD: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500249#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 gettimeofday(&touchStart, NULL);
251 key_repeat = 2;
252 kb.KeyRepeat();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700253#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700255#endif
256
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500257 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 else if (key_repeat == 2 && mtime > 100)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500259 {
260#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 LOGERR("KEY_REPEAT: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500262#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 gettimeofday(&touchStart, NULL);
264 kb.KeyRepeat();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700265#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700267#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500268 }
269 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 else if (ev.type == EV_ABS)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500271 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 x = ev.value >> 16;
274 y = ev.value & 0xFFFF;
Dees_Troy51a0e822012-09-05 15:24:24 -0400275
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200276 if (ev.code == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500277 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200278 if (state == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500279 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400280#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 LOGERR("TOUCH_RELEASE: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400282#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200283 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700284#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700286#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 touch_and_hold = 0;
288 touch_repeat = 0;
289 if (!key_repeat)
290 dontwait = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 state = 0;
293 drag = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400294 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500296 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200297 if (!drag)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500298 {
299#ifdef _EVENT_LOGGING
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200300 LOGERR("TOUCH_START: %d,%d\n", x, y);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500301#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
303 state = 1;
304 drag = 1;
305 touch_and_hold = 1;
306 dontwait = 1;
307 key_repeat = 0;
308 gettimeofday(&touchStart, NULL);
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
357 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
358
359 touch_and_hold = 0;
360 touch_repeat = 0;
361 if (!key_repeat)
362 dontwait = 0;
363 }
364 state = 0;
365 drag = 0;
366 }
367 }
368 // side mouse button, often used for "back" function
369 else if(ev.code == BTN_SIDE)
370 {
371 if(ev.value == 1)
372 kb.KeyDown(KEY_BACK);
373 else
374 kb.KeyUp(KEY_BACK);
375 }
376 else if (ev.value != 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500377 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200378 // This is a key press
379 if (kb.KeyDown(ev.code))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500380 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 key_repeat = 1;
382 touch_and_hold = 0;
383 touch_repeat = 0;
384 dontwait = 1;
385 gettimeofday(&touchStart, NULL);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700386#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700388#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500389 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200390 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500391 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 key_repeat = 0;
393 touch_and_hold = 0;
394 touch_repeat = 0;
395 dontwait = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700396#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700398#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500399 }
400 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500402 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200403 // This is a key release
404 kb.KeyUp(ev.code);
405 key_repeat = 0;
406 touch_and_hold = 0;
407 touch_repeat = 0;
408 dontwait = 0;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700409#ifndef TW_NO_SCREEN_TIMEOUT
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200410 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700411#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500412 }
413 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100414 else if(ev.type == EV_REL)
415 {
416#ifdef _EVENT_LOGGING
417 LOGERR("EV_REL %d %d\n", ev.code, ev.value);
418#endif
419 if(ev.code == REL_X)
420 cursor->Move(ev.value, 0);
421 else if(ev.code == REL_Y)
422 cursor->Move(0, ev.value);
423
424 if(drag == 1) {
425 cursor->GetPos(x, y);
426#ifdef _EVENT_LOGGING
427 LOGERR("TOUCH_DRAG: %d, %d\n", x, y);
428#endif
429 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
430 state = 1;
431 key_repeat = 0;
432 }
433 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500434 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200435 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400436}
437
438// This special function will return immediately the first time, but then
439// always returns 1/30th of a second (or immediately if called later) from
440// the last time it was called
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200441static void loopTimer(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400442{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200443 static timespec lastCall;
444 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400445
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200446 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400447 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200448 clock_gettime(CLOCK_MONOTONIC, &lastCall);
449 initialized = 1;
450 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400451 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400452
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200453 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500454 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200455 timespec curTime;
456 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200458 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500459
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200460 // This is really 30 times per second
461 if (diff.tv_sec || diff.tv_nsec > 33333333)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500462 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200463 lastCall = curTime;
464 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500465 }
466
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200467 // We need to sleep some period time microseconds
468 unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
469 usleep(sleepTime);
470 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471}
472
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200473static int runPages(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500474{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200475 // Raise the curtain
476 if (gCurtain != NULL)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500477 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 gr_surface surface;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500479
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 PageManager::Render();
481 gr_get_surface(&surface);
482 curtainRaise(surface);
483 gr_free_surface(surface);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500484 }
485
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200486 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500487
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200488 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500489
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100490#ifdef PRINT_RENDER_TIME
491 timespec start, end;
492 int32_t render_t, flip_t;
493#endif
494
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200495 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500496 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200497 loopTimer();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500498
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200499 if (!gForceRender)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500500 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200501 int ret;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500502
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200503 ret = PageManager::Update();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100504
505#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200506 if (ret > 1)
507 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500508
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200509 if (ret > 0)
510 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100511#else
512 if (ret > 1)
513 {
514 clock_gettime(CLOCK_MONOTONIC, &start);
515 PageManager::Render();
516 clock_gettime(CLOCK_MONOTONIC, &end);
517 render_t = TWFunc::timespec_diff_ms(start, end);
518
519 flip();
520 clock_gettime(CLOCK_MONOTONIC, &start);
521 flip_t = TWFunc::timespec_diff_ms(end, start);
522
523 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
524 }
525 else if(ret == 1)
526 flip();
527#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500528 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500530 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200531 pthread_mutex_lock(&gForceRendermutex);
532 gForceRender = 0;
533 pthread_mutex_unlock(&gForceRendermutex);
534 PageManager::Render();
535 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500536 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200537
Dees_Troy6ef66352013-02-21 08:26:57 -0600538 if (DataManager::GetIntValue("tw_gui_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200539 break;
540 }
541
542 gGuiRunning = 0;
543 return 0;
544}
545
546static int runPage(const char *page_name)
547{
548 gui_changePage(page_name);
549
550 // Raise the curtain
551 if (gCurtain != NULL)
552 {
553 gr_surface surface;
554
555 PageManager::Render();
556 gr_get_surface(&surface);
557 curtainRaise(surface);
558 gr_free_surface(surface);
559 }
560
561 gGuiRunning = 1;
562
563 DataManager::SetValue("tw_loaded", 1);
564
565 for (;;)
566 {
567 loopTimer();
568
569 if (!gForceRender)
Dees_Troy6ef66352013-02-21 08:26:57 -0600570 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200571 int ret;
572
573 ret = PageManager::Update();
574 if (ret > 1)
575 PageManager::Render();
576
577 if (ret > 0)
578 flip();
579 }
580 else
581 {
582 pthread_mutex_lock(&gForceRendermutex);
583 gForceRender = 0;
584 pthread_mutex_unlock(&gForceRendermutex);
585 PageManager::Render();
586 flip();
587 }
588 if (DataManager::GetIntValue("tw_page_done") != 0)
589 {
590 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600591 break;
592 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500593 }
594
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200595 gGuiRunning = 0;
596 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500597}
598
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500600{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 pthread_mutex_lock(&gForceRendermutex);
602 gForceRender = 1;
603 pthread_mutex_unlock(&gForceRendermutex);
604 return 0;
605}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500606
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200607int gui_changePage(std::string newPage)
608{
609 LOGINFO("Set page: '%s'\n", newPage.c_str());
610 PageManager::ChangePage(newPage);
611 pthread_mutex_lock(&gForceRendermutex);
612 gForceRender = 1;
613 pthread_mutex_unlock(&gForceRendermutex);
614 return 0;
615}
616
617int gui_changeOverlay(std::string overlay)
618{
619 PageManager::ChangeOverlay(overlay);
620 pthread_mutex_lock(&gForceRendermutex);
621 gForceRender = 1;
622 pthread_mutex_unlock(&gForceRendermutex);
623 return 0;
624}
625
626int gui_changePackage(std::string newPackage)
627{
628 PageManager::SelectPackage(newPackage);
629 pthread_mutex_lock(&gForceRendermutex);
630 gForceRender = 1;
631 pthread_mutex_unlock(&gForceRendermutex);
632 return 0;
633}
634
635std::string gui_parse_text(string inText)
636{
637 // Copied from std::string GUIText::parseText(void)
638 // This function parses text for DataManager values encompassed by %value% in the XML
639 static int counter = 0;
640 std::string str = inText;
641 size_t pos = 0;
642 size_t next = 0, end = 0;
643
644 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500645 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200646 next = str.find('%', pos);
647 if (next == std::string::npos)
648 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500649
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200650 end = str.find('%', next + 1);
651 if (end == std::string::npos)
652 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500653
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200654 // We have a block of data
655 std::string var = str.substr(next + 1,(end - next) - 1);
656 str.erase(next,(end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500657
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200658 if (next + 1 == end)
659 str.insert(next, 1, '%');
660 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500661 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200662 std::string value;
663 if (DataManager::GetValue(var, value) == 0)
664 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500665 }
666
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200667 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500668 }
669}
670
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200671extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500672{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200673 int fd;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500674
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200675 gr_init();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500676
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200677 if (res_create_surface("/res/images/curtain.jpg", &gCurtain))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500678 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200679 printf
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500680 ("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 +0200681 return -1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500682 }
683
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200684 curtainSet();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500685
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200686 ev_init();
687 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500688}
689
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200690extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400691{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200692 // unlink("/sdcard/video.last");
693 // rename("/sdcard/video.bin", "/sdcard/video.last");
694 // gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400695
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200696 int check = 0;
697 DataManager::GetValue(TW_IS_ENCRYPTED, check);
698 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500699 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200700 if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400701 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200702 LOGERR("Failed to load base packages.\n");
703 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200705 else
706 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500707 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400708
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200709 if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main"))
710 {
711 std::string theme_path;
712
713 theme_path = DataManager::GetSettingsStoragePath();
714 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200716 int retry_count = 5;
717 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400718 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200719 usleep(500000);
720 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500721 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200722
723 if (!PartitionManager.Mount_Settings_Storage(false))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500724 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200725 LOGERR("Unable to mount %s during GUI startup.\n",
726 theme_path.c_str());
727 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500728 }
729 }
730
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200731 theme_path += "/TWRP/theme/ui.zip";
732 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500733 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734 if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500735 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200736 LOGERR("Failed to load base packages.\n");
737 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 }
739 }
740 }
741
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200742 // Set the default package
743 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400744
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200745 gGuiInitialized = 1;
746 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400747
748error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200749 LOGERR("An internal error has occurred.\n");
750 gGuiInitialized = 0;
751 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400752}
753
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200754extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400755{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200756 if (!gGuiInitialized)
757 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400758
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200759 gGuiConsoleTerminate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400760
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200761 while (gGuiConsoleRunning)
762 loopTimer();
Dees_Troy51a0e822012-09-05 15:24:24 -0400763
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200764 // Set the default package
765 PageManager::SelectPackage("TWRP");
766
767 if (!gGuiInputRunning)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500768 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200769 // Start by spinning off an input handler.
770 pthread_t t;
771 pthread_create(&t, NULL, input_thread, NULL);
772 gGuiInputRunning = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000773 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400774
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200775 return runPages();
Dees_Troy51a0e822012-09-05 15:24:24 -0400776}
777
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200778extern "C" int gui_startPage(const char *page_name)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000779{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200780 if (!gGuiInitialized)
781 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000782
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200783 gGuiConsoleTerminate = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000784
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200785 while (gGuiConsoleRunning)
786 loopTimer();
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000787
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200788 // Set the default package
789 PageManager::SelectPackage("TWRP");
790
791 if (!gGuiInputRunning)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500792 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200793 // Start by spinning off an input handler.
794 pthread_t t;
795 pthread_create(&t, NULL, input_thread, NULL);
796 gGuiInputRunning = 1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000797 }
798
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200799 DataManager::SetValue("tw_page_done", 0);
800 return runPage(page_name);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000801}
802
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200803static void * console_thread(void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400804{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200805 PageManager::SwitchToConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400806
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200807 while (!gGuiConsoleTerminate)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500808 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200809 loopTimer();
Dees_Troy51a0e822012-09-05 15:24:24 -0400810
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200811 if (!gForceRender)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500812 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200813 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400814
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200815 ret = PageManager::Update();
816 if (ret > 1)
817 PageManager::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400818
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200819 if (ret > 0)
820 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400821
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200822 if (ret < 0)
823 LOGERR("An update request has failed.\n");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500824 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200825 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500826 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200827 pthread_mutex_lock(&gForceRendermutex);
828 gForceRender = 0;
829 pthread_mutex_unlock(&gForceRendermutex);
830 PageManager::Render();
831 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500832 }
833 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200834 gGuiConsoleRunning = 0;
835 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400836}
837
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200838extern "C" int gui_console_only(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400839{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200840 if (!gGuiInitialized)
841 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400842
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200843 gGuiConsoleTerminate = 0;
844 gGuiConsoleRunning = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400845
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200846 // Start by spinning off an input handler.
847 pthread_t t;
848 pthread_create(&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400849
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200850 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400851}