blob: 17ed108fce5c3e95c7d27f3e3e61394f95093fad [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 <pixelflinger/pixelflinger.h>
42}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010043#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040044
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
thatc5837f32015-02-01 01:59:43 +010060#define LOGEVENT(...) LOGERR(__VA_ARGS__)
that9fa51532015-01-29 02:04:47 +010061#else
62#define LOGEVENT(...) do {} while (0)
63#endif
64
Dees_Troy51a0e822012-09-05 15:24:24 -040065using namespace rapidxml;
66
67// Global values
Dees_Troy51a0e822012-09-05 15:24:24 -040068static int gGuiInitialized = 0;
Ethan Yonker04536952015-01-27 08:41:28 -060069static TWAtomicInt gForceRender;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050070blanktimer blankTimer;
Ethan Yonkerfd0439e2015-01-14 11:08:13 -060071int ors_read_fd = -1;
that10ae24f2015-12-26 20:53:51 +010072static FILE* orsout = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -060073static float scale_theme_w = 1;
74static float scale_theme_h = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
76// Needed by pages.cpp too
77int gGuiRunning = 0;
78
that1964d192016-01-07 00:41:03 +010079int g_pty_fd = -1; // set by terminal on init
80void terminal_pty_read();
81
Dees_Troy51a0e822012-09-05 15:24:24 -040082static 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
that9fa51532015-01-29 02:04:47 +0100106class InputHandler
Dees_Troy51a0e822012-09-05 15:24:24 -0400107{
that9fa51532015-01-29 02:04:47 +0100108public:
109 void init()
thatfb759d42015-01-11 12:16:53 +0100110 {
that9fa51532015-01-29 02:04:47 +0100111 // these might be read from DataManager in the future
112 touch_hold_ms = 500;
113 touch_repeat_ms = 100;
114 key_hold_ms = 500;
115 key_repeat_ms = 100;
116 touch_status = TS_NONE;
117 key_status = KS_NONE;
118 state = AS_NO_ACTION;
119 x = y = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
that9fa51532015-01-29 02:04:47 +0100121#ifndef TW_NO_SCREEN_TIMEOUT
122 {
123 string seconds;
124 DataManager::GetValue("tw_screen_timeout_secs", seconds);
125 blankTimer.setTime(atoi(seconds.c_str()));
126 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600127 }
that9fa51532015-01-29 02:04:47 +0100128#else
129 LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n");
130#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600131 }
132
thatc5837f32015-02-01 01:59:43 +0100133 // process input events. returns true if any event was received.
thatde72b6d2015-02-08 08:55:00 +0100134 bool processInput(int timeout_ms);
thatc5837f32015-02-01 01:59:43 +0100135
that9fa51532015-01-29 02:04:47 +0100136 void handleDrag();
137
138private:
139 // timeouts for touch/key hold and repeat
140 int touch_hold_ms;
141 int touch_repeat_ms;
142 int key_hold_ms;
143 int key_repeat_ms;
144
145 enum touch_status_enum {
146 TS_NONE = 0,
147 TS_TOUCH_AND_HOLD = 1,
148 TS_TOUCH_REPEAT = 2,
149 };
150
151 enum key_status_enum {
152 KS_NONE = 0,
153 KS_KEY_PRESSED = 1,
154 KS_KEY_REPEAT = 2,
155 };
156
157 enum action_state_enum {
158 AS_IN_ACTION_AREA = 0, // we've touched a spot with an action
159 AS_NO_ACTION = 1, // we've touched in an empty area (no action) and ignore remaining events until touch release
160 };
161 touch_status_enum touch_status;
162 key_status_enum key_status;
163 action_state_enum state;
164 int x, y; // x and y coordinates of last touch
165 struct timeval touchStart; // used to track time for long press / key repeat
166
167 void processHoldAndRepeat();
168 void process_EV_REL(input_event& ev);
169 void process_EV_ABS(input_event& ev);
170 void process_EV_KEY(input_event& ev);
171
172 void doTouchStart();
173};
174
175InputHandler input_handler;
176
177
thatde72b6d2015-02-08 08:55:00 +0100178bool InputHandler::processInput(int timeout_ms)
that9fa51532015-01-29 02:04:47 +0100179{
180 input_event ev;
thatde72b6d2015-02-08 08:55:00 +0100181 int ret = ev_get(&ev, timeout_ms);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600182
183 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500184 {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600185 // This path means that we did not get any new touch data, but
186 // we do not get new touch data if you press and hold on either
187 // the screen or on a keyboard key or mouse button
that9fa51532015-01-29 02:04:47 +0100188 if (touch_status || key_status)
189 processHoldAndRepeat();
thatc5837f32015-02-01 01:59:43 +0100190 return (ret != -2); // -2 means no more events in the queue
Ethan Yonkere13fa632015-01-27 11:30:03 -0600191 }
that9fa51532015-01-29 02:04:47 +0100192
193 switch (ev.type)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600194 {
that9fa51532015-01-29 02:04:47 +0100195 case EV_ABS:
196 process_EV_ABS(ev);
197 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600198
that9fa51532015-01-29 02:04:47 +0100199 case EV_REL:
200 process_EV_REL(ev);
201 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600202
that9fa51532015-01-29 02:04:47 +0100203 case EV_KEY:
204 process_EV_KEY(ev);
205 break;
206 }
thatc5837f32015-02-01 01:59:43 +0100207
208 blankTimer.resetTimerAndUnblank();
209 return true; // we got an event, so there might be more in the queue
that9fa51532015-01-29 02:04:47 +0100210}
211
212void InputHandler::processHoldAndRepeat()
213{
214 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
215
216 // touch and key repeat section
217 struct timeval curTime;
218 gettimeofday(&curTime, NULL);
219 long seconds = curTime.tv_sec - touchStart.tv_sec;
220 long useconds = curTime.tv_usec - touchStart.tv_usec;
221 long mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
222
223 if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms)
224 {
225 touch_status = TS_TOUCH_REPEAT;
226 gettimeofday(&touchStart, NULL);
227 LOGEVENT("TOUCH_HOLD: %d,%d\n", x, y);
228 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
that9fa51532015-01-29 02:04:47 +0100229 }
230 else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms)
231 {
232 LOGEVENT("TOUCH_REPEAT: %d,%d\n", x, y);
233 gettimeofday(&touchStart, NULL);
234 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
that9fa51532015-01-29 02:04:47 +0100235 }
236 else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms)
237 {
238 LOGEVENT("KEY_HOLD: %d,%d\n", x, y);
239 gettimeofday(&touchStart, NULL);
240 key_status = KS_KEY_REPEAT;
241 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100242 }
243 else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms)
244 {
245 LOGEVENT("KEY_REPEAT: %d,%d\n", x, y);
246 gettimeofday(&touchStart, NULL);
247 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100248 }
249}
250
251void InputHandler::doTouchStart()
252{
253 LOGEVENT("TOUCH_START: %d,%d\n", x, y);
254 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
255 state = AS_NO_ACTION;
256 else
257 state = AS_IN_ACTION_AREA;
258 touch_status = TS_TOUCH_AND_HOLD;
259 gettimeofday(&touchStart, NULL);
that9fa51532015-01-29 02:04:47 +0100260}
261
262void InputHandler::process_EV_ABS(input_event& ev)
263{
264 x = ev.value >> 16;
265 y = ev.value & 0xFFFF;
266
267 if (ev.code == 0)
268 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500269#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100270 if (state == AS_IN_ACTION_AREA)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500271 {
that9fa51532015-01-29 02:04:47 +0100272 LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y);
273 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
that9fa51532015-01-29 02:04:47 +0100274 }
275 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500276#endif
that9fa51532015-01-29 02:04:47 +0100277 }
278 else
279 {
280 if (!touch_status)
281 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500282#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100283 doTouchStart();
Ethan Yonker30fa3352015-03-09 13:57:21 -0500284#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600285 }
286 else
287 {
that9fa51532015-01-29 02:04:47 +0100288 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600289 {
that9fa51532015-01-29 02:04:47 +0100290 LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500292 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500293 }
that9fa51532015-01-29 02:04:47 +0100294}
295
296void InputHandler::process_EV_KEY(input_event& ev)
297{
298 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
299
300 // Handle key-press here
301 LOGEVENT("TOUCH_KEY: %d\n", ev.code);
302 // Left mouse button is treated as a touch
Matt Mowera8a89d12016-12-30 18:10:37 -0600303 if (ev.code == BTN_LEFT)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600304 {
that9fa51532015-01-29 02:04:47 +0100305 MouseCursor *cursor = PageManager::GetMouseCursor();
Matt Mowera8a89d12016-12-30 18:10:37 -0600306 if (ev.value == 1)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600307 {
that9fa51532015-01-29 02:04:47 +0100308 cursor->GetPos(x, y);
309 doTouchStart();
310 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600311 else if (touch_status)
that9fa51532015-01-29 02:04:47 +0100312 {
313 // Left mouse button was previously pressed and now is
314 // being released so send a TOUCH_RELEASE
315 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600316 {
317 cursor->GetPos(x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600318
that9fa51532015-01-29 02:04:47 +0100319 LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y);
320 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600321 }
that9fa51532015-01-29 02:04:47 +0100322 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600323 }
that9fa51532015-01-29 02:04:47 +0100324 }
325 // side mouse button, often used for "back" function
Matt Mowera8a89d12016-12-30 18:10:37 -0600326 else if (ev.code == BTN_SIDE)
that9fa51532015-01-29 02:04:47 +0100327 {
Matt Mowera8a89d12016-12-30 18:10:37 -0600328 if (ev.value == 1)
that9fa51532015-01-29 02:04:47 +0100329 kb->KeyDown(KEY_BACK);
330 else
331 kb->KeyUp(KEY_BACK);
332 } else if (ev.value != 0) {
333 // This is a key press
Ethan Yonker30fa3352015-03-09 13:57:21 -0500334#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
335 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
336 LOGEVENT("key code %i key press == touch start %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
337 doTouchStart();
338 return;
339 }
340#endif
that9fa51532015-01-29 02:04:47 +0100341 if (kb->KeyDown(ev.code)) {
342 // Key repeat is enabled for this key
343 key_status = KS_KEY_PRESSED;
344 touch_status = TS_NONE;
345 gettimeofday(&touchStart, NULL);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600346 } else {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600347 key_status = KS_NONE;
348 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600349 }
that9fa51532015-01-29 02:04:47 +0100350 } else {
351 // This is a key release
352 kb->KeyUp(ev.code);
353 key_status = KS_NONE;
354 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500355#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
356 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
357 LOGEVENT("key code %i key release == touch release %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
358 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
359 }
360#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600361 }
that9fa51532015-01-29 02:04:47 +0100362}
Ethan Yonkere13fa632015-01-27 11:30:03 -0600363
that9fa51532015-01-29 02:04:47 +0100364void InputHandler::process_EV_REL(input_event& ev)
365{
366 // Mouse movement
367 MouseCursor *cursor = PageManager::GetMouseCursor();
368 LOGEVENT("EV_REL %d %d\n", ev.code, ev.value);
Matt Mowera8a89d12016-12-30 18:10:37 -0600369 if (ev.code == REL_X)
that9fa51532015-01-29 02:04:47 +0100370 cursor->Move(ev.value, 0);
Matt Mowera8a89d12016-12-30 18:10:37 -0600371 else if (ev.code == REL_Y)
that9fa51532015-01-29 02:04:47 +0100372 cursor->Move(0, ev.value);
373
Matt Mowera8a89d12016-12-30 18:10:37 -0600374 if (touch_status) {
that9fa51532015-01-29 02:04:47 +0100375 cursor->GetPos(x, y);
376 LOGEVENT("Mouse TOUCH_DRAG: %d, %d\n", x, y);
377 key_status = KS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600378 }
that9fa51532015-01-29 02:04:47 +0100379}
380
381void InputHandler::handleDrag()
382{
383 // This allows us to only send one NotifyTouch event per render
384 // cycle to reduce overhead and perceived input latency.
385 static int prevx = 0, prevy = 0; // these track where the last drag notice was so that we don't send duplicate drag notices
386 if (touch_status && (x != prevx || y != prevy)) {
387 prevx = x;
388 prevy = y;
389 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
390 state = AS_NO_ACTION;
391 else
392 state = AS_IN_ACTION_AREA;
393 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}
395
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600396static void setup_ors_command()
Ethan Yonker03a42f62014-08-08 11:03:51 -0500397{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600398 ors_read_fd = -1;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500399
400 unlink(ORS_INPUT_FILE);
401 if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
402 LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600403 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500404 }
405 unlink(ORS_OUTPUT_FILE);
406 if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
407 LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
408 unlink(ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600409 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500410 }
411
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600412 ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK);
413 if (ors_read_fd < 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500414 LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
415 unlink(ORS_INPUT_FILE);
416 unlink(ORS_OUTPUT_FILE);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500417 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600418}
Ethan Yonker03a42f62014-08-08 11:03:51 -0500419
that10ae24f2015-12-26 20:53:51 +0100420// callback called after a CLI command was executed
421static void ors_command_done()
422{
423 gui_set_FILE(NULL);
424 fclose(orsout);
425 orsout = NULL;
426
427 if (DataManager::GetIntValue("tw_page_done") == 0) {
428 // The select function will return ready to read and the
429 // read function will return errno 19 no such device unless
430 // we set everything up all over again.
431 close(ors_read_fd);
432 setup_ors_command();
433 }
434}
435
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600436static void ors_command_read()
437{
that10ae24f2015-12-26 20:53:51 +0100438 char command[1024];
439 int read_ret = read(ors_read_fd, &command, sizeof(command));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500440
that10ae24f2015-12-26 20:53:51 +0100441 if (read_ret > 0) {
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600442 command[1022] = '\n';
443 command[1023] = '\0';
444 LOGINFO("Command '%s' received\n", command);
445 orsout = fopen(ORS_OUTPUT_FILE, "w");
446 if (!orsout) {
447 close(ors_read_fd);
448 ors_read_fd = -1;
449 LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
450 unlink(ORS_INPUT_FILE);
451 unlink(ORS_OUTPUT_FILE);
452 return;
453 }
454 if (DataManager::GetIntValue("tw_busy") != 0) {
that10ae24f2015-12-26 20:53:51 +0100455 fputs("Failed, operation in progress\n", orsout);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600456 LOGINFO("Command cannot be performed, operation in progress.\n");
that10ae24f2015-12-26 20:53:51 +0100457 fclose(orsout);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600458 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500459 if (strlen(command) == 11 && strncmp(command, "dumpstrings", 11) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500460 gui_set_FILE(orsout);
461 PageManager::GetResources()->DumpStrings();
that10ae24f2015-12-26 20:53:51 +0100462 ors_command_done();
bigbiffce8f83c2015-12-12 18:30:21 -0500463 //check to see if we should show backup page for parsing adbbackup partitions
464 } else if (strlen(command) == 23 && strncmp(command, "adbbackup", 9) == 0) {
465 gui_set_FILE(orsout);
466 DataManager::SetValue("tw_action", "twcmd");
467 DataManager::SetValue("tw_action_param", command);
468 DataManager::SetValue("tw_enable_adb_backup", 1);
469 gui_changePage("backup");
470 ors_command_done();
that10ae24f2015-12-26 20:53:51 +0100471 } else {
472 // mirror output messages
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600473 gui_set_FILE(orsout);
that10ae24f2015-12-26 20:53:51 +0100474 // close orsout and restart listener after command is done
475 OpenRecoveryScript::Call_After_CLI_Command(ors_command_done);
476 // run the command in a threaded action...
477 DataManager::SetValue("tw_action", "twcmd");
478 DataManager::SetValue("tw_action_param", command);
479 // ...and switch back to the current page when finished
480 std::string currentPage = PageManager::GetCurrentPage();
481 DataManager::SetValue("tw_has_action2", "1");
482 DataManager::SetValue("tw_action2", "page");
483 DataManager::SetValue("tw_action2_param", currentPage);
484 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
485 DataManager::SetValue("tw_action_text2", "");
486 gui_changePage("singleaction_page");
487 // now immediately return to the GUI main loop (the action runs in the background thread)
488 // put all things that need to be done after the command is finished into ors_command_done, not here
Ethan Yonker03a42f62014-08-08 11:03:51 -0500489 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500490 }
491 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500492}
493
that10ae24f2015-12-26 20:53:51 +0100494// Get and dispatch input events until it's time to draw the next frame
Dees_Troy51a0e822012-09-05 15:24:24 -0400495// This special function will return immediately the first time, but then
496// always returns 1/30th of a second (or immediately if called later) from
497// the last time it was called
thatde72b6d2015-02-08 08:55:00 +0100498static void loopTimer(int input_timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400499{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200500 static timespec lastCall;
501 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400502
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200503 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400504 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200505 clock_gettime(CLOCK_MONOTONIC, &lastCall);
506 initialized = 1;
507 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400508 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400509
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200510 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500511 {
thatde72b6d2015-02-08 08:55:00 +0100512 bool got_event = input_handler.processInput(input_timeout_ms); // get inputs but don't send drag notices
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200513 timespec curTime;
514 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200516 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500517
thatc5837f32015-02-01 01:59:43 +0100518 // This is really 2 or 30 times per second
519 // As long as we get events, increase the timeout so we can catch up with input
520 long timeout = got_event ? 500000000 : 33333333;
521
522 if (diff.tv_sec || diff.tv_nsec > timeout)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500523 {
thatc5837f32015-02-01 01:59:43 +0100524 // int32_t input_time = TWFunc::timespec_diff_ms(lastCall, curTime);
525 // LOGINFO("loopTimer(): %u ms, count: %u\n", input_time, count);
526
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200527 lastCall = curTime;
that9fa51532015-01-29 02:04:47 +0100528 input_handler.handleDrag(); // send only drag notices if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500530 }
531
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200532 // We need to sleep some period time microseconds
Ethan Yonkere13fa632015-01-27 11:30:03 -0600533 //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
534 //usleep(sleepTime); // removed so we can scan for input
thatde72b6d2015-02-08 08:55:00 +0100535 input_timeout_ms = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200536 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400537}
538
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600539static int runPages(const char *page_name, const int stop_on_page_done)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500540{
Ethan Yonker3f15be42015-02-09 09:39:47 -0600541 DataManager::SetValue("tw_page_done", 0);
542 DataManager::SetValue("tw_gui_done", 0);
543
Ethan Yonkerafde0982016-01-23 08:55:35 -0600544 if (page_name) {
545 PageManager::SetStartPage(page_name);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600546 gui_changePage(page_name);
Ethan Yonkerafde0982016-01-23 08:55:35 -0600547 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600548
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200549 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500550
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200551 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500552
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600553 struct timeval timeout;
554 fd_set fdset;
555 int has_data = 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100556
thatde72b6d2015-02-08 08:55:00 +0100557 int input_timeout_ms = 0;
558 int idle_frames = 0;
559
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200560 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500561 {
thatde72b6d2015-02-08 08:55:00 +0100562 loopTimer(input_timeout_ms);
that1964d192016-01-07 00:41:03 +0100563 if (g_pty_fd > 0) {
564 // TODO: this is not nice, we should have one central select for input, pty, and ors
565 FD_ZERO(&fdset);
566 FD_SET(g_pty_fd, &fdset);
567 timeout.tv_sec = 0;
568 timeout.tv_usec = 1;
569 has_data = select(g_pty_fd+1, &fdset, NULL, NULL, &timeout);
570 if (has_data > 0) {
571 terminal_pty_read();
572 }
573 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600574#ifndef TW_OEM_BUILD
that10ae24f2015-12-26 20:53:51 +0100575 if (ors_read_fd > 0 && !orsout) { // orsout is non-NULL if a command is still running
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600576 FD_ZERO(&fdset);
577 FD_SET(ors_read_fd, &fdset);
578 timeout.tv_sec = 0;
579 timeout.tv_usec = 1;
580 has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout);
581 if (has_data > 0) {
582 ors_command_read();
583 }
584 }
585#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500586
Ethan Yonker04536952015-01-27 08:41:28 -0600587 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500588 {
thatde72b6d2015-02-08 08:55:00 +0100589 int ret = PageManager::Update();
590 if (ret == 0)
591 ++idle_frames;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500592 else if (ret == -2)
593 break; // Theme reload failure
thatde72b6d2015-02-08 08:55:00 +0100594 else
595 idle_frames = 0;
596 // due to possible animation objects, we need to delay activating the input timeout
597 input_timeout_ms = idle_frames > 15 ? 1000 : 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100598
599#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200600 if (ret > 1)
601 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500602
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200603 if (ret > 0)
604 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100605#else
606 if (ret > 1)
607 {
that9fa51532015-01-29 02:04:47 +0100608 timespec start, end;
609 int32_t render_t, flip_t;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100610 clock_gettime(CLOCK_MONOTONIC, &start);
611 PageManager::Render();
612 clock_gettime(CLOCK_MONOTONIC, &end);
613 render_t = TWFunc::timespec_diff_ms(start, end);
614
615 flip();
616 clock_gettime(CLOCK_MONOTONIC, &start);
617 flip_t = TWFunc::timespec_diff_ms(end, start);
618
619 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
620 }
thatde72b6d2015-02-08 08:55:00 +0100621 else if (ret > 0)
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100622 flip();
623#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500624 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200625 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500626 {
Ethan Yonker04536952015-01-27 08:41:28 -0600627 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200628 PageManager::Render();
629 flip();
thatde72b6d2015-02-08 08:55:00 +0100630 input_timeout_ms = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500631 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200632
thatfb759d42015-01-11 12:16:53 +0100633 blankTimer.checkForTimeout();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600634 if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200635 {
636 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600637 break;
638 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600639 if (DataManager::GetIntValue("tw_gui_done") != 0)
640 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500641 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600642 if (ors_read_fd > 0)
643 close(ors_read_fd);
644 ors_read_fd = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200645 gGuiRunning = 0;
646 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500647}
648
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200649int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500650{
Ethan Yonker04536952015-01-27 08:41:28 -0600651 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200652 return 0;
653}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500654
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200655int gui_changePage(std::string newPage)
656{
657 LOGINFO("Set page: '%s'\n", newPage.c_str());
658 PageManager::ChangePage(newPage);
Ethan Yonker04536952015-01-27 08:41:28 -0600659 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200660 return 0;
661}
662
663int gui_changeOverlay(std::string overlay)
664{
Ethan Yonker1c273312015-03-16 12:18:56 -0500665 LOGINFO("Set overlay: '%s'\n", overlay.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200666 PageManager::ChangeOverlay(overlay);
Ethan Yonker04536952015-01-27 08:41:28 -0600667 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200668 return 0;
669}
670
thatb2e8f672015-03-05 20:25:39 +0100671std::string gui_parse_text(std::string str)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200672{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200673 // This function parses text for DataManager values encompassed by %value% in the XML
thatb2e8f672015-03-05 20:25:39 +0100674 // and string resources (%@resource_name%)
Ethan Yonker74db1572015-10-28 12:44:49 -0500675 size_t pos = 0, next, end;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200676
677 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500678 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500679 next = str.find("{@", pos);
680 if (next == std::string::npos)
681 break;
682
683 end = str.find('}', next + 1);
684 if (end == std::string::npos)
685 break;
686
687 std::string var = str.substr(next + 2, (end - next) - 2);
688 str.erase(next, (end - next) + 1);
689
690 size_t default_loc = var.find('=', 0);
691 std::string lookup;
692 if (default_loc == std::string::npos) {
693 str.insert(next, PageManager::GetResources()->FindString(var));
694 } else {
695 lookup = var.substr(0, default_loc);
696 std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1);
697 str.insert(next, PageManager::GetResources()->FindString(lookup, default_string));
698 }
699 }
700 pos = 0;
701 while (1)
702 {
703 next = str.find('%', pos);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200704 if (next == std::string::npos)
705 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500706
Ethan Yonker74db1572015-10-28 12:44:49 -0500707 end = str.find('%', next + 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200708 if (end == std::string::npos)
709 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500710
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200711 // We have a block of data
thatb2e8f672015-03-05 20:25:39 +0100712 std::string var = str.substr(next + 1, (end - next) - 1);
713 str.erase(next, (end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500714
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200715 if (next + 1 == end)
716 str.insert(next, 1, '%');
717 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500718 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200719 std::string value;
thatb2e8f672015-03-05 20:25:39 +0100720 if (var.size() > 0 && var[0] == '@') {
721 // this is a string resource ("%@string_name%")
722 value = PageManager::GetResources()->FindString(var.substr(1));
723 str.insert(next, value);
724 }
725 else if (DataManager::GetValue(var, value) == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200726 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500727 }
728
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200729 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500730 }
731}
732
Ethan Yonker74db1572015-10-28 12:44:49 -0500733std::string gui_lookup(const std::string& resource_name, const std::string& default_value) {
734 return PageManager::GetResources()->FindString(resource_name, default_value);
735}
736
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200737extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500738{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200739 gr_init();
Matt Mowerb26c1162016-02-03 21:05:57 -0600740 TWFunc::Set_Brightness(DataManager::GetStrValue("tw_brightness"));
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500741
that235c6482016-01-24 21:59:00 +0100742 // load and show splash screen
743 if (PageManager::LoadPackage("splash", TWRES "splash.xml", "splash")) {
744 LOGERR("Failed to load splash screen XML.\n");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500745 }
that235c6482016-01-24 21:59:00 +0100746 else {
747 PageManager::SelectPackage("splash");
748 PageManager::Render();
749 flip();
750 PageManager::ReleasePackage("splash");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600751 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500752
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200753 ev_init();
754 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500755}
756
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200757extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400758{
Ethan Yonker83e82572014-04-04 10:59:28 -0500759#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200760 int check = 0;
761 DataManager::GetValue(TW_IS_ENCRYPTED, check);
Dees Troy3454ade2015-01-20 19:21:04 +0000762
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200763 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500764 {
Dees Troy3454ade2015-01-20 19:21:04 +0000765 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400766 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500767 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200768 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200770 else
771 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500772 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400773
Ethan Yonker86404bd2016-03-31 08:01:05 -0500774 if (check == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200775 {
776 std::string theme_path;
777
778 theme_path = DataManager::GetSettingsStoragePath();
779 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200781 int retry_count = 5;
782 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400783 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200784 usleep(500000);
785 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500786 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200787
Ethan Yonker74db1572015-10-28 12:44:49 -0500788 if (!PartitionManager.Mount_Settings_Storage(true))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500789 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500790 LOGINFO("Unable to mount %s during GUI startup.\n", theme_path.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200791 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500792 }
793 }
794
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200795 theme_path += "/TWRP/theme/ui.zip";
796 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500797 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500798#endif // ifndef TW_OEM_BUILD
Dees Troy3454ade2015-01-20 19:21:04 +0000799 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500800 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500801 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200802 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400803 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500804#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 }
806 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500807#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200808 // Set the default package
809 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400810
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200811 gGuiInitialized = 1;
812 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400813
814error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500815 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200816 gGuiInitialized = 0;
817 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400818}
819
Ethan Yonkercf50da52015-01-12 21:59:07 -0600820extern "C" int gui_loadCustomResources(void)
821{
822#ifndef TW_OEM_BUILD
823 if (!PartitionManager.Mount_Settings_Storage(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500824 LOGINFO("Unable to mount settings storage during GUI startup.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600825 return -1;
826 }
827
828 std::string theme_path = DataManager::GetSettingsStoragePath();
829 theme_path += "/TWRP/theme/ui.zip";
830 // Check for a custom theme
831 if (TWFunc::Path_Exists(theme_path)) {
832 // There is a custom theme, try to load it
833 if (PageManager::ReloadPackage("TWRP", theme_path)) {
834 // Custom theme failed to load, try to load stock theme
Dees Troy3454ade2015-01-20 19:21:04 +0000835 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500836 gui_err("base_pkg_err=Failed to load base packages.");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600837 goto error;
838 }
839 }
840 }
841 // Set the default package
842 PageManager::SelectPackage("TWRP");
843#endif
844 return 0;
845
846error:
847 LOGERR("An internal error has occurred: unable to load theme.\n");
848 gGuiInitialized = 0;
849 return -1;
850}
851
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200852extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400853{
Ethan Yonkerafde0982016-01-23 08:55:35 -0600854 return gui_startPage("main", 1, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400855}
856
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600857extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000858{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200859 if (!gGuiInitialized)
860 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000861
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200862 // Set the default package
863 PageManager::SelectPackage("TWRP");
864
that9fa51532015-01-29 02:04:47 +0100865 input_handler.init();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600866#ifndef TW_OEM_BUILD
867 if (allow_commands)
868 {
869 if (ors_read_fd < 0)
870 setup_ors_command();
871 } else {
872 if (ors_read_fd >= 0) {
873 close(ors_read_fd);
874 ors_read_fd = -1;
875 }
876 }
877#endif
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600878 return runPages(page_name, stop_on_page_done);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000879}
880
Ethan Yonker63e414f2015-02-06 15:44:39 -0600881
882extern "C" void set_scale_values(float w, float h)
883{
884 scale_theme_w = w;
885 scale_theme_h = h;
886}
887
888extern "C" int scale_theme_x(int initial_x)
889{
890 if (scale_theme_w != 1) {
thatba75a0e2015-02-14 21:20:10 +0100891 int scaled = (float)initial_x * scale_theme_w;
892 if (scaled == 0 && initial_x > 0)
893 return 1;
894 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600895 }
896 return initial_x;
897}
898
899extern "C" int scale_theme_y(int initial_y)
900{
901 if (scale_theme_h != 1) {
thatba75a0e2015-02-14 21:20:10 +0100902 int scaled = (float)initial_y * scale_theme_h;
903 if (scaled == 0 && initial_y > 0)
904 return 1;
905 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600906 }
907 return initial_y;
908}
909
910extern "C" int scale_theme_min(int initial_value)
911{
912 if (scale_theme_w != 1 || scale_theme_h != 1) {
913 if (scale_theme_w < scale_theme_h)
914 return scale_theme_x(initial_value);
915 else
916 return scale_theme_y(initial_value);
917 }
918 return initial_value;
919}
920
921extern "C" float get_scale_w()
922{
923 return scale_theme_w;
924}
925
926extern "C" float get_scale_h()
927{
928 return scale_theme_h;
929}