blob: 4bf01ecfbbb41d6349aaea9d9c7ad12d0b9aff69 [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"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050053#include "blanktimer.hpp"
Ethan Yonker04536952015-01-27 08:41:28 -060054#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040055
Vojtech Boceke5ffcd12014-02-06 21:17:32 +010056// Enable to print render time of each frame to the log file
57//#define PRINT_RENDER_TIME 1
58
that9fa51532015-01-29 02:04:47 +010059#ifdef _EVENT_LOGGING
60#define LOGEVENT(...) LOGERR(__VA_ARGS)
61#else
62#define LOGEVENT(...) do {} while (0)
63#endif
64
Dees_Troy51a0e822012-09-05 15:24:24 -040065const static int CURTAIN_FADE = 32;
66
67using namespace rapidxml;
68
69// Global values
70static gr_surface gCurtain = NULL;
71static int gGuiInitialized = 0;
Ethan Yonker04536952015-01-27 08:41:28 -060072static TWAtomicInt gGuiConsoleRunning;
73static TWAtomicInt gGuiConsoleTerminate;
74static TWAtomicInt gForceRender;
75const int gNoAnimation = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050076blanktimer blankTimer;
Ethan Yonkerfd0439e2015-01-14 11:08:13 -060077int ors_read_fd = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040078
79// Needed by pages.cpp too
80int gGuiRunning = 0;
81
82static int gRecorder = -1;
83
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084extern "C" void gr_write_frame_to_file(int fd);
Dees_Troy51a0e822012-09-05 15:24:24 -040085
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086void flip(void)
Dees_Troy51a0e822012-09-05 15:24:24 -040087{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 if (gRecorder != -1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050089 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 timespec time;
91 clock_gettime(CLOCK_MONOTONIC, &time);
92 write(gRecorder, &time, sizeof(timespec));
93 gr_write_frame_to_file(gRecorder);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050094 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -040096}
97
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098void rapidxml::parse_error_handler(const char *what, void *where)
Dees_Troy51a0e822012-09-05 15:24:24 -040099{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100 fprintf(stderr, "Parser error: %s\n", what);
101 fprintf(stderr, " Start of string: %s\n",(char *) where);
102 LOGERR("Error parsing XML file.\n");
103 //abort();
Dees_Troy51a0e822012-09-05 15:24:24 -0400104}
105
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106static void curtainSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400107{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 gr_color(0, 0, 0, 255);
109 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Ethan Yonker751a85e2014-12-12 16:59:10 -0600110 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), TW_X_OFFSET, TW_Y_OFFSET);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400112}
113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114static void curtainRaise(gr_surface surface)
Dees_Troy51a0e822012-09-05 15:24:24 -0400115{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 int sy = 0;
117 int h = gr_get_height(gCurtain) - 1;
118 int w = gr_get_width(gCurtain);
119 int fy = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 int msw = gr_get_width(surface);
122 int msh = gr_get_height(surface);
123 int CURTAIN_RATE = msh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500126 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500128 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200129 gr_blit(surface, 0, 0, msw, msh, 0, 0);
130 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
131 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500132 }
133 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 gr_blit(surface, 0, 0, msw, msh, 0, 0);
135 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400136}
137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138void curtainClose()
Dees_Troy51a0e822012-09-05 15:24:24 -0400139{
140#if 0
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 int w = gr_get_width(gCurtain);
142 int h = 1;
143 int sy = gr_get_height(gCurtain) - 1;
144 int fbh = gr_fb_height();
145 int CURTAIN_RATE = fbh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500148 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500150 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
152 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500153 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
155 gr_get_height(gCurtain), 0, 0);
156 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 if (gRecorder != -1)
159 close(gRecorder);
Dees_Troy51a0e822012-09-05 15:24:24 -0400160
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 int fade;
162 for (fade = 16; fade < 255; fade += CURTAIN_FADE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500163 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200164 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
165 gr_get_height(gCurtain), 0, 0);
166 gr_color(0, 0, 0, fade);
167 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
168 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500169 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200170 gr_color(0, 0, 0, 255);
171 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
172 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400173 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500174#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
176 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500177#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400178}
179
that9fa51532015-01-29 02:04:47 +0100180class InputHandler
Dees_Troy51a0e822012-09-05 15:24:24 -0400181{
that9fa51532015-01-29 02:04:47 +0100182public:
183 void init()
thatfb759d42015-01-11 12:16:53 +0100184 {
that9fa51532015-01-29 02:04:47 +0100185 // these might be read from DataManager in the future
186 touch_hold_ms = 500;
187 touch_repeat_ms = 100;
188 key_hold_ms = 500;
189 key_repeat_ms = 100;
190 touch_status = TS_NONE;
191 key_status = KS_NONE;
192 state = AS_NO_ACTION;
193 x = y = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400194
that9fa51532015-01-29 02:04:47 +0100195#ifndef TW_NO_SCREEN_TIMEOUT
196 {
197 string seconds;
198 DataManager::GetValue("tw_screen_timeout_secs", seconds);
199 blankTimer.setTime(atoi(seconds.c_str()));
200 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600201 }
that9fa51532015-01-29 02:04:47 +0100202#else
203 LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n");
204#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600205 }
206
that9fa51532015-01-29 02:04:47 +0100207 void processInput();
208 void handleDrag();
209
210private:
211 // timeouts for touch/key hold and repeat
212 int touch_hold_ms;
213 int touch_repeat_ms;
214 int key_hold_ms;
215 int key_repeat_ms;
216
217 enum touch_status_enum {
218 TS_NONE = 0,
219 TS_TOUCH_AND_HOLD = 1,
220 TS_TOUCH_REPEAT = 2,
221 };
222
223 enum key_status_enum {
224 KS_NONE = 0,
225 KS_KEY_PRESSED = 1,
226 KS_KEY_REPEAT = 2,
227 };
228
229 enum action_state_enum {
230 AS_IN_ACTION_AREA = 0, // we've touched a spot with an action
231 AS_NO_ACTION = 1, // we've touched in an empty area (no action) and ignore remaining events until touch release
232 };
233 touch_status_enum touch_status;
234 key_status_enum key_status;
235 action_state_enum state;
236 int x, y; // x and y coordinates of last touch
237 struct timeval touchStart; // used to track time for long press / key repeat
238
239 void processHoldAndRepeat();
240 void process_EV_REL(input_event& ev);
241 void process_EV_ABS(input_event& ev);
242 void process_EV_KEY(input_event& ev);
243
244 void doTouchStart();
245};
246
247InputHandler input_handler;
248
249
250void InputHandler::processInput()
251{
252 input_event ev;
253 int ret = ev_get(&ev);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600254
255 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500256 {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600257 // This path means that we did not get any new touch data, but
258 // we do not get new touch data if you press and hold on either
259 // the screen or on a keyboard key or mouse button
that9fa51532015-01-29 02:04:47 +0100260 if (touch_status || key_status)
261 processHoldAndRepeat();
262 return;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600263 }
that9fa51532015-01-29 02:04:47 +0100264
265 switch (ev.type)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600266 {
that9fa51532015-01-29 02:04:47 +0100267 case EV_ABS:
268 process_EV_ABS(ev);
269 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600270
that9fa51532015-01-29 02:04:47 +0100271 case EV_REL:
272 process_EV_REL(ev);
273 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600274
that9fa51532015-01-29 02:04:47 +0100275 case EV_KEY:
276 process_EV_KEY(ev);
277 break;
278 }
279}
280
281void InputHandler::processHoldAndRepeat()
282{
283 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
284
285 // touch and key repeat section
286 struct timeval curTime;
287 gettimeofday(&curTime, NULL);
288 long seconds = curTime.tv_sec - touchStart.tv_sec;
289 long useconds = curTime.tv_usec - touchStart.tv_usec;
290 long mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
291
292 if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms)
293 {
294 touch_status = TS_TOUCH_REPEAT;
295 gettimeofday(&touchStart, NULL);
296 LOGEVENT("TOUCH_HOLD: %d,%d\n", x, y);
297 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
298 blankTimer.resetTimerAndUnblank();
299 }
300 else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms)
301 {
302 LOGEVENT("TOUCH_REPEAT: %d,%d\n", x, y);
303 gettimeofday(&touchStart, NULL);
304 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
305 blankTimer.resetTimerAndUnblank();
306 }
307 else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms)
308 {
309 LOGEVENT("KEY_HOLD: %d,%d\n", x, y);
310 gettimeofday(&touchStart, NULL);
311 key_status = KS_KEY_REPEAT;
312 kb->KeyRepeat();
313 blankTimer.resetTimerAndUnblank();
314
315 }
316 else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms)
317 {
318 LOGEVENT("KEY_REPEAT: %d,%d\n", x, y);
319 gettimeofday(&touchStart, NULL);
320 kb->KeyRepeat();
321 blankTimer.resetTimerAndUnblank();
322 }
323}
324
325void InputHandler::doTouchStart()
326{
327 LOGEVENT("TOUCH_START: %d,%d\n", x, y);
328 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
329 state = AS_NO_ACTION;
330 else
331 state = AS_IN_ACTION_AREA;
332 touch_status = TS_TOUCH_AND_HOLD;
333 gettimeofday(&touchStart, NULL);
334 blankTimer.resetTimerAndUnblank();
335}
336
337void InputHandler::process_EV_ABS(input_event& ev)
338{
339 x = ev.value >> 16;
340 y = ev.value & 0xFFFF;
341
342 if (ev.code == 0)
343 {
344 if (state == AS_IN_ACTION_AREA)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500345 {
that9fa51532015-01-29 02:04:47 +0100346 LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y);
347 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
348 blankTimer.resetTimerAndUnblank();
349 }
350 touch_status = TS_NONE;
351 }
352 else
353 {
354 if (!touch_status)
355 {
356 doTouchStart();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600357 }
358 else
359 {
that9fa51532015-01-29 02:04:47 +0100360 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600361 {
that9fa51532015-01-29 02:04:47 +0100362 LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600363 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500365 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500366 }
that9fa51532015-01-29 02:04:47 +0100367}
368
369void InputHandler::process_EV_KEY(input_event& ev)
370{
371 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
372
373 // Handle key-press here
374 LOGEVENT("TOUCH_KEY: %d\n", ev.code);
375 // Left mouse button is treated as a touch
376 if(ev.code == BTN_LEFT)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600377 {
that9fa51532015-01-29 02:04:47 +0100378 MouseCursor *cursor = PageManager::GetMouseCursor();
379 if(ev.value == 1)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600380 {
that9fa51532015-01-29 02:04:47 +0100381 cursor->GetPos(x, y);
382 doTouchStart();
383 }
384 else if(touch_status)
385 {
386 // Left mouse button was previously pressed and now is
387 // being released so send a TOUCH_RELEASE
388 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600389 {
390 cursor->GetPos(x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600391
that9fa51532015-01-29 02:04:47 +0100392 LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y);
393 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600394 }
that9fa51532015-01-29 02:04:47 +0100395 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600396 }
that9fa51532015-01-29 02:04:47 +0100397 }
398 // side mouse button, often used for "back" function
399 else if(ev.code == BTN_SIDE)
400 {
401 if(ev.value == 1)
402 kb->KeyDown(KEY_BACK);
403 else
404 kb->KeyUp(KEY_BACK);
405 } else if (ev.value != 0) {
406 // This is a key press
407 if (kb->KeyDown(ev.code)) {
408 // Key repeat is enabled for this key
409 key_status = KS_KEY_PRESSED;
410 touch_status = TS_NONE;
411 gettimeofday(&touchStart, NULL);
412 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600413 } else {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600414 key_status = KS_NONE;
415 touch_status = TS_NONE;
416 blankTimer.resetTimerAndUnblank();
417 }
that9fa51532015-01-29 02:04:47 +0100418 } else {
419 // This is a key release
420 kb->KeyUp(ev.code);
421 key_status = KS_NONE;
422 touch_status = TS_NONE;
423 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600424 }
that9fa51532015-01-29 02:04:47 +0100425}
Ethan Yonkere13fa632015-01-27 11:30:03 -0600426
that9fa51532015-01-29 02:04:47 +0100427void InputHandler::process_EV_REL(input_event& ev)
428{
429 // Mouse movement
430 MouseCursor *cursor = PageManager::GetMouseCursor();
431 LOGEVENT("EV_REL %d %d\n", ev.code, ev.value);
432 if(ev.code == REL_X)
433 cursor->Move(ev.value, 0);
434 else if(ev.code == REL_Y)
435 cursor->Move(0, ev.value);
436
437 if(touch_status) {
438 cursor->GetPos(x, y);
439 LOGEVENT("Mouse TOUCH_DRAG: %d, %d\n", x, y);
440 key_status = KS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600441 }
that9fa51532015-01-29 02:04:47 +0100442}
443
444void InputHandler::handleDrag()
445{
446 // This allows us to only send one NotifyTouch event per render
447 // cycle to reduce overhead and perceived input latency.
448 static int prevx = 0, prevy = 0; // these track where the last drag notice was so that we don't send duplicate drag notices
449 if (touch_status && (x != prevx || y != prevy)) {
450 prevx = x;
451 prevy = y;
452 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
453 state = AS_NO_ACTION;
454 else
455 state = AS_IN_ACTION_AREA;
456 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400457}
458
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600459static void setup_ors_command()
Ethan Yonker03a42f62014-08-08 11:03:51 -0500460{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600461 ors_read_fd = -1;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500462
463 unlink(ORS_INPUT_FILE);
464 if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
465 LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600466 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500467 }
468 unlink(ORS_OUTPUT_FILE);
469 if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
470 LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
471 unlink(ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600472 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500473 }
474
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600475 ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK);
476 if (ors_read_fd < 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500477 LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
478 unlink(ORS_INPUT_FILE);
479 unlink(ORS_OUTPUT_FILE);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500480 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600481}
Ethan Yonker03a42f62014-08-08 11:03:51 -0500482
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600483static void ors_command_read()
484{
485 FILE* orsout;
486 char command[1024], result[512];
487 int set_page_done = 0, read_ret = 0;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500488
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600489 if ((read_ret = read(ors_read_fd, &command, sizeof(command))) > 0) {
490 command[1022] = '\n';
491 command[1023] = '\0';
492 LOGINFO("Command '%s' received\n", command);
493 orsout = fopen(ORS_OUTPUT_FILE, "w");
494 if (!orsout) {
495 close(ors_read_fd);
496 ors_read_fd = -1;
497 LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
498 unlink(ORS_INPUT_FILE);
499 unlink(ORS_OUTPUT_FILE);
500 return;
501 }
502 if (DataManager::GetIntValue("tw_busy") != 0) {
503 strcpy(result, "Failed, operation in progress\n");
504 fprintf(orsout, "%s", result);
505 LOGINFO("Command cannot be performed, operation in progress.\n");
506 } else {
507 if (gui_console_only() == 0) {
508 LOGINFO("Console started successfully\n");
509 gui_set_FILE(orsout);
510 if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) {
511 char* filename = command + 11;
512 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
513 LOGERR("Unable to copy script file\n");
514 } else {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500515 OpenRecoveryScript::run_script_file();
516 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600517 } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) {
518 char* varname = command + 4;
519 string temp;
520 DataManager::GetValue(varname, temp);
521 gui_print("%s = %s\n", varname, temp.c_str());
522 } else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) {
523 char* pass = command + 8;
524 gui_print("Attempting to decrypt data partition via command line.\n");
525 if (PartitionManager.Decrypt_Device(pass) == 0) {
526 set_page_done = 1;
527 }
528 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
529 OpenRecoveryScript::run_script_file();
Ethan Yonker03a42f62014-08-08 11:03:51 -0500530 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600531 gui_set_FILE(NULL);
Ethan Yonker04536952015-01-27 08:41:28 -0600532 gGuiConsoleTerminate.set_value(1);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500533 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500534 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600535 fclose(orsout);
536 LOGINFO("Done reading ORS command from command line\n");
537 if (set_page_done) {
538 DataManager::SetValue("tw_page_done", 1);
539 } else {
540 // The select function will return ready to read and the
541 // read function will return errno 19 no such device unless
542 // we set everything up all over again.
543 close(ors_read_fd);
544 setup_ors_command();
545 }
546 } else {
547 LOGINFO("ORS command line read returned an error: %i, %i, %s\n", read_ret, errno, strerror(errno));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500548 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600549 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500550}
551
Dees_Troy51a0e822012-09-05 15:24:24 -0400552// This special function will return immediately the first time, but then
553// always returns 1/30th of a second (or immediately if called later) from
554// the last time it was called
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200555static void loopTimer(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400556{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200557 static timespec lastCall;
558 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400559
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200560 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400561 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200562 clock_gettime(CLOCK_MONOTONIC, &lastCall);
563 initialized = 1;
564 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400565 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400566
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200567 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500568 {
that9fa51532015-01-29 02:04:47 +0100569 input_handler.processInput(); // get inputs but don't send drag notices
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200570 timespec curTime;
571 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400572
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200573 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500574
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200575 // This is really 30 times per second
576 if (diff.tv_sec || diff.tv_nsec > 33333333)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500577 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200578 lastCall = curTime;
that9fa51532015-01-29 02:04:47 +0100579 input_handler.handleDrag(); // send only drag notices if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200580 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500581 }
582
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 // We need to sleep some period time microseconds
Ethan Yonkere13fa632015-01-27 11:30:03 -0600584 //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
585 //usleep(sleepTime); // removed so we can scan for input
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200586 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587}
588
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600589static int runPages(const char *page_name, const int stop_on_page_done)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500590{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600591 if (page_name)
592 gui_changePage(page_name);
593
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594 // Raise the curtain
595 if (gCurtain != NULL)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500596 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200597 gr_surface surface;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500598
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 PageManager::Render();
600 gr_get_surface(&surface);
601 curtainRaise(surface);
602 gr_free_surface(surface);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500603 }
604
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200605 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500606
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200607 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500608
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600609#ifndef TW_OEM_BUILD
610 struct timeval timeout;
611 fd_set fdset;
612 int has_data = 0;
613#endif
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100614
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200615 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500616 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200617 loopTimer();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600618#ifndef TW_OEM_BUILD
619 if (ors_read_fd > 0) {
620 FD_ZERO(&fdset);
621 FD_SET(ors_read_fd, &fdset);
622 timeout.tv_sec = 0;
623 timeout.tv_usec = 1;
624 has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout);
625 if (has_data > 0) {
626 ors_command_read();
627 }
628 }
629#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500630
Ethan Yonker04536952015-01-27 08:41:28 -0600631 if (gGuiConsoleRunning.get_value()) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500632 continue;
633 }
634
Ethan Yonker04536952015-01-27 08:41:28 -0600635 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500636 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200637 int ret;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500638
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200639 ret = PageManager::Update();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100640
641#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200642 if (ret > 1)
643 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500644
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200645 if (ret > 0)
646 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100647#else
648 if (ret > 1)
649 {
that9fa51532015-01-29 02:04:47 +0100650 timespec start, end;
651 int32_t render_t, flip_t;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100652 clock_gettime(CLOCK_MONOTONIC, &start);
653 PageManager::Render();
654 clock_gettime(CLOCK_MONOTONIC, &end);
655 render_t = TWFunc::timespec_diff_ms(start, end);
656
657 flip();
658 clock_gettime(CLOCK_MONOTONIC, &start);
659 flip_t = TWFunc::timespec_diff_ms(end, start);
660
661 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
662 }
663 else if(ret == 1)
664 flip();
665#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500666 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200667 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500668 {
Ethan Yonker04536952015-01-27 08:41:28 -0600669 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200670 PageManager::Render();
671 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500672 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200673
thatfb759d42015-01-11 12:16:53 +0100674 blankTimer.checkForTimeout();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600675 if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200676 {
677 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600678 break;
679 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600680 if (DataManager::GetIntValue("tw_gui_done") != 0)
681 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500682 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600683 if (ors_read_fd > 0)
684 close(ors_read_fd);
685 ors_read_fd = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200686 gGuiRunning = 0;
687 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500688}
689
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200690int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500691{
Ethan Yonker04536952015-01-27 08:41:28 -0600692 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200693 return 0;
694}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500695
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200696int gui_changePage(std::string newPage)
697{
698 LOGINFO("Set page: '%s'\n", newPage.c_str());
699 PageManager::ChangePage(newPage);
Ethan Yonker04536952015-01-27 08:41:28 -0600700 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200701 return 0;
702}
703
704int gui_changeOverlay(std::string overlay)
705{
706 PageManager::ChangeOverlay(overlay);
Ethan Yonker04536952015-01-27 08:41:28 -0600707 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200708 return 0;
709}
710
711int gui_changePackage(std::string newPackage)
712{
713 PageManager::SelectPackage(newPackage);
Ethan Yonker04536952015-01-27 08:41:28 -0600714 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200715 return 0;
716}
717
718std::string gui_parse_text(string inText)
719{
720 // Copied from std::string GUIText::parseText(void)
721 // This function parses text for DataManager values encompassed by %value% in the XML
722 static int counter = 0;
723 std::string str = inText;
724 size_t pos = 0;
725 size_t next = 0, end = 0;
726
727 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500728 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200729 next = str.find('%', pos);
730 if (next == std::string::npos)
731 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500732
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200733 end = str.find('%', next + 1);
734 if (end == std::string::npos)
735 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500736
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200737 // We have a block of data
738 std::string var = str.substr(next + 1,(end - next) - 1);
739 str.erase(next,(end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500740
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200741 if (next + 1 == end)
742 str.insert(next, 1, '%');
743 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500744 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200745 std::string value;
746 if (DataManager::GetValue(var, value) == 0)
747 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500748 }
749
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200750 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500751 }
752}
753
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200754extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500755{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200756 gr_init();
Dees Troy3454ade2015-01-20 19:21:04 +0000757 std::string curtain_path = TWRES "images/curtain.jpg";
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500758
Dees Troy3454ade2015-01-20 19:21:04 +0000759 if (res_create_surface(curtain_path.c_str(), &gCurtain))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500760 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200761 printf
Dees Troy3454ade2015-01-20 19:21:04 +0000762 ("Unable to locate '%s'\nDid you set a DEVICE_RESOLUTION in your config files?\n", curtain_path.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200763 return -1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500764 }
765
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200766 curtainSet();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500767
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200768 ev_init();
769 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500770}
771
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200772extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400773{
Ethan Yonker83e82572014-04-04 10:59:28 -0500774#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200775 int check = 0;
776 DataManager::GetValue(TW_IS_ENCRYPTED, check);
Dees Troy3454ade2015-01-20 19:21:04 +0000777
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200778 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500779 {
Dees Troy3454ade2015-01-20 19:21:04 +0000780 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400781 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200782 LOGERR("Failed to load base packages.\n");
783 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200785 else
786 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500787 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400788
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200789 if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main"))
790 {
791 std::string theme_path;
792
793 theme_path = DataManager::GetSettingsStoragePath();
794 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200796 int retry_count = 5;
797 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200799 usleep(500000);
800 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500801 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200802
803 if (!PartitionManager.Mount_Settings_Storage(false))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500804 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200805 LOGERR("Unable to mount %s during GUI startup.\n",
806 theme_path.c_str());
807 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500808 }
809 }
810
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200811 theme_path += "/TWRP/theme/ui.zip";
812 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500813 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500814#endif // ifndef TW_OEM_BUILD
Dees Troy3454ade2015-01-20 19:21:04 +0000815 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500816 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200817 LOGERR("Failed to load base packages.\n");
818 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400819 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500820#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400821 }
822 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500823#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 // Set the default package
825 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400826
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200827 gGuiInitialized = 1;
828 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400829
830error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500831 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200832 gGuiInitialized = 0;
833 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400834}
835
Ethan Yonkercf50da52015-01-12 21:59:07 -0600836extern "C" int gui_loadCustomResources(void)
837{
838#ifndef TW_OEM_BUILD
839 if (!PartitionManager.Mount_Settings_Storage(false)) {
840 LOGERR("Unable to mount settings storage during GUI startup.\n");
841 return -1;
842 }
843
844 std::string theme_path = DataManager::GetSettingsStoragePath();
845 theme_path += "/TWRP/theme/ui.zip";
846 // Check for a custom theme
847 if (TWFunc::Path_Exists(theme_path)) {
848 // There is a custom theme, try to load it
849 if (PageManager::ReloadPackage("TWRP", theme_path)) {
850 // Custom theme failed to load, try to load stock theme
Dees Troy3454ade2015-01-20 19:21:04 +0000851 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
Ethan Yonkercf50da52015-01-12 21:59:07 -0600852 LOGERR("Failed to load base packages.\n");
853 goto error;
854 }
855 }
856 }
857 // Set the default package
858 PageManager::SelectPackage("TWRP");
859#endif
860 return 0;
861
862error:
863 LOGERR("An internal error has occurred: unable to load theme.\n");
864 gGuiInitialized = 0;
865 return -1;
866}
867
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200868extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400869{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600870 return gui_startPage(NULL, 1, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400871}
872
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600873extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000874{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200875 if (!gGuiInitialized)
876 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000877
Ethan Yonker04536952015-01-27 08:41:28 -0600878 gGuiConsoleTerminate.set_value(1);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000879
Ethan Yonker04536952015-01-27 08:41:28 -0600880 while (gGuiConsoleRunning.get_value())
Ethan Yonkere13fa632015-01-27 11:30:03 -0600881 usleep(10000);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000882
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200883 // Set the default package
884 PageManager::SelectPackage("TWRP");
885
that9fa51532015-01-29 02:04:47 +0100886 input_handler.init();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600887#ifndef TW_OEM_BUILD
888 if (allow_commands)
889 {
890 if (ors_read_fd < 0)
891 setup_ors_command();
892 } else {
893 if (ors_read_fd >= 0) {
894 close(ors_read_fd);
895 ors_read_fd = -1;
896 }
897 }
898#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200899 DataManager::SetValue("tw_page_done", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600900 return runPages(page_name, stop_on_page_done);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000901}
902
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200903static void * console_thread(void *cookie)
Dees_Troy51a0e822012-09-05 15:24:24 -0400904{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200905 PageManager::SwitchToConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400906
Ethan Yonker04536952015-01-27 08:41:28 -0600907 while (!gGuiConsoleTerminate.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500908 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200909 loopTimer();
Dees_Troy51a0e822012-09-05 15:24:24 -0400910
Ethan Yonker04536952015-01-27 08:41:28 -0600911 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500912 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200913 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400914
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200915 ret = PageManager::Update();
916 if (ret > 1)
917 PageManager::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400918
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200919 if (ret > 0)
920 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400921
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200922 if (ret < 0)
923 LOGERR("An update request has failed.\n");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500924 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200925 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500926 {
Ethan Yonker04536952015-01-27 08:41:28 -0600927 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200928 PageManager::Render();
929 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500930 }
931 }
Ethan Yonker04536952015-01-27 08:41:28 -0600932 gGuiConsoleRunning.set_value(0);
933 gForceRender.set_value(1); // this will kickstart the GUI to render again
Ethan Yonker03a42f62014-08-08 11:03:51 -0500934 PageManager::EndConsole();
935 LOGINFO("Console stopping\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200936 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400937}
938
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200939extern "C" int gui_console_only(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400940{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200941 if (!gGuiInitialized)
942 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400943
Ethan Yonker04536952015-01-27 08:41:28 -0600944 gGuiConsoleTerminate.set_value(0);
Ethan Yonkerffbd6ff2014-10-22 10:40:40 -0500945
Ethan Yonker04536952015-01-27 08:41:28 -0600946 if (gGuiConsoleRunning.get_value())
Ethan Yonkerffbd6ff2014-10-22 10:40:40 -0500947 return 0;
948
Ethan Yonker04536952015-01-27 08:41:28 -0600949 gGuiConsoleRunning.set_value(1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400950
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200951 // Start by spinning off an input handler.
952 pthread_t t;
953 pthread_create(&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400954
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200955 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400956}