blob: a270e365f71d61f1221cc56131e867602f998c24 [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>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050037extern "C"
38{
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include <pixelflinger/pixelflinger.h>
41}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010042#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040043
44#include "rapidxml.hpp"
45#include "objects.hpp"
46#include "../data.hpp"
47#include "../variables.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040048#include "../partitions.hpp"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050049#include "../twrp-functions.hpp"
Ethan Yonker03a42f62014-08-08 11:03:51 -050050#include "../openrecoveryscript.hpp"
51#include "../orscmd/orscmd.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050052#include "blanktimer.hpp"
Ethan Yonker04536952015-01-27 08:41:28 -060053#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Boceke5ffcd12014-02-06 21:17:32 +010055// Enable to print render time of each frame to the log file
56//#define PRINT_RENDER_TIME 1
57
that9fa51532015-01-29 02:04:47 +010058#ifdef _EVENT_LOGGING
thatc5837f32015-02-01 01:59:43 +010059#define LOGEVENT(...) LOGERR(__VA_ARGS__)
that9fa51532015-01-29 02:04:47 +010060#else
61#define LOGEVENT(...) do {} while (0)
62#endif
63
Dees_Troy51a0e822012-09-05 15:24:24 -040064using namespace rapidxml;
65
66// Global values
Dees_Troy51a0e822012-09-05 15:24:24 -040067static int gGuiInitialized = 0;
Ethan Yonker04536952015-01-27 08:41:28 -060068static TWAtomicInt gForceRender;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050069blanktimer blankTimer;
Ethan Yonkerfd0439e2015-01-14 11:08:13 -060070int ors_read_fd = -1;
that10ae24f2015-12-26 20:53:51 +010071static FILE* orsout = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -060072static float scale_theme_w = 1;
73static float scale_theme_h = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040074
75// Needed by pages.cpp too
76int gGuiRunning = 0;
77
that1964d192016-01-07 00:41:03 +010078int g_pty_fd = -1; // set by terminal on init
79void terminal_pty_read();
80
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060081int select_fd = 0;
82
Dees_Troy51a0e822012-09-05 15:24:24 -040083static 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
Matt Mowerb68bff02017-01-11 15:46:26 -060087static void 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
that9fa51532015-01-29 02:04:47 +0100107class InputHandler
Dees_Troy51a0e822012-09-05 15:24:24 -0400108{
that9fa51532015-01-29 02:04:47 +0100109public:
110 void init()
thatfb759d42015-01-11 12:16:53 +0100111 {
that9fa51532015-01-29 02:04:47 +0100112 // these might be read from DataManager in the future
113 touch_hold_ms = 500;
114 touch_repeat_ms = 100;
115 key_hold_ms = 500;
116 key_repeat_ms = 100;
117 touch_status = TS_NONE;
118 key_status = KS_NONE;
119 state = AS_NO_ACTION;
120 x = y = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
that9fa51532015-01-29 02:04:47 +0100122#ifndef TW_NO_SCREEN_TIMEOUT
123 {
124 string seconds;
125 DataManager::GetValue("tw_screen_timeout_secs", seconds);
126 blankTimer.setTime(atoi(seconds.c_str()));
127 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600128 }
that9fa51532015-01-29 02:04:47 +0100129#else
130 LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n");
131#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600132 }
133
thatc5837f32015-02-01 01:59:43 +0100134 // process input events. returns true if any event was received.
thatde72b6d2015-02-08 08:55:00 +0100135 bool processInput(int timeout_ms);
thatc5837f32015-02-01 01:59:43 +0100136
that9fa51532015-01-29 02:04:47 +0100137 void handleDrag();
138
139private:
140 // timeouts for touch/key hold and repeat
141 int touch_hold_ms;
142 int touch_repeat_ms;
143 int key_hold_ms;
144 int key_repeat_ms;
145
146 enum touch_status_enum {
147 TS_NONE = 0,
148 TS_TOUCH_AND_HOLD = 1,
149 TS_TOUCH_REPEAT = 2,
150 };
151
152 enum key_status_enum {
153 KS_NONE = 0,
154 KS_KEY_PRESSED = 1,
155 KS_KEY_REPEAT = 2,
156 };
157
158 enum action_state_enum {
159 AS_IN_ACTION_AREA = 0, // we've touched a spot with an action
160 AS_NO_ACTION = 1, // we've touched in an empty area (no action) and ignore remaining events until touch release
161 };
162 touch_status_enum touch_status;
163 key_status_enum key_status;
164 action_state_enum state;
165 int x, y; // x and y coordinates of last touch
166 struct timeval touchStart; // used to track time for long press / key repeat
167
168 void processHoldAndRepeat();
169 void process_EV_REL(input_event& ev);
170 void process_EV_ABS(input_event& ev);
171 void process_EV_KEY(input_event& ev);
172
173 void doTouchStart();
174};
175
176InputHandler input_handler;
177
178
thatde72b6d2015-02-08 08:55:00 +0100179bool InputHandler::processInput(int timeout_ms)
that9fa51532015-01-29 02:04:47 +0100180{
181 input_event ev;
thatde72b6d2015-02-08 08:55:00 +0100182 int ret = ev_get(&ev, timeout_ms);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600183
184 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500185 {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600186 // This path means that we did not get any new touch data, but
187 // we do not get new touch data if you press and hold on either
188 // the screen or on a keyboard key or mouse button
that9fa51532015-01-29 02:04:47 +0100189 if (touch_status || key_status)
190 processHoldAndRepeat();
thatc5837f32015-02-01 01:59:43 +0100191 return (ret != -2); // -2 means no more events in the queue
Ethan Yonkere13fa632015-01-27 11:30:03 -0600192 }
that9fa51532015-01-29 02:04:47 +0100193
194 switch (ev.type)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600195 {
that9fa51532015-01-29 02:04:47 +0100196 case EV_ABS:
197 process_EV_ABS(ev);
198 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600199
that9fa51532015-01-29 02:04:47 +0100200 case EV_REL:
201 process_EV_REL(ev);
202 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600203
that9fa51532015-01-29 02:04:47 +0100204 case EV_KEY:
205 process_EV_KEY(ev);
206 break;
207 }
thatc5837f32015-02-01 01:59:43 +0100208
Matt Mower9472ba12016-01-20 18:12:47 -0600209 if (ev.code != KEY_POWER && ev.code > KEY_RESERVED)
210 blankTimer.resetTimerAndUnblank();
211
thatc5837f32015-02-01 01:59:43 +0100212 return true; // we got an event, so there might be more in the queue
that9fa51532015-01-29 02:04:47 +0100213}
214
215void InputHandler::processHoldAndRepeat()
216{
217 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
218
219 // touch and key repeat section
220 struct timeval curTime;
221 gettimeofday(&curTime, NULL);
222 long seconds = curTime.tv_sec - touchStart.tv_sec;
223 long useconds = curTime.tv_usec - touchStart.tv_usec;
224 long mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
225
226 if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms)
227 {
228 touch_status = TS_TOUCH_REPEAT;
229 gettimeofday(&touchStart, NULL);
230 LOGEVENT("TOUCH_HOLD: %d,%d\n", x, y);
231 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
that9fa51532015-01-29 02:04:47 +0100232 }
233 else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms)
234 {
235 LOGEVENT("TOUCH_REPEAT: %d,%d\n", x, y);
236 gettimeofday(&touchStart, NULL);
237 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
that9fa51532015-01-29 02:04:47 +0100238 }
239 else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms)
240 {
241 LOGEVENT("KEY_HOLD: %d,%d\n", x, y);
242 gettimeofday(&touchStart, NULL);
243 key_status = KS_KEY_REPEAT;
244 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100245 }
246 else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms)
247 {
248 LOGEVENT("KEY_REPEAT: %d,%d\n", x, y);
249 gettimeofday(&touchStart, NULL);
250 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100251 }
252}
253
254void InputHandler::doTouchStart()
255{
256 LOGEVENT("TOUCH_START: %d,%d\n", x, y);
257 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
258 state = AS_NO_ACTION;
259 else
260 state = AS_IN_ACTION_AREA;
261 touch_status = TS_TOUCH_AND_HOLD;
262 gettimeofday(&touchStart, NULL);
that9fa51532015-01-29 02:04:47 +0100263}
264
265void InputHandler::process_EV_ABS(input_event& ev)
266{
267 x = ev.value >> 16;
268 y = ev.value & 0xFFFF;
269
270 if (ev.code == 0)
271 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500272#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100273 if (state == AS_IN_ACTION_AREA)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500274 {
that9fa51532015-01-29 02:04:47 +0100275 LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y);
276 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
that9fa51532015-01-29 02:04:47 +0100277 }
278 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500279#endif
that9fa51532015-01-29 02:04:47 +0100280 }
281 else
282 {
283 if (!touch_status)
284 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500285#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100286 doTouchStart();
Ethan Yonker30fa3352015-03-09 13:57:21 -0500287#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600288 }
289 else
290 {
that9fa51532015-01-29 02:04:47 +0100291 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600292 {
that9fa51532015-01-29 02:04:47 +0100293 LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400294 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500295 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500296 }
that9fa51532015-01-29 02:04:47 +0100297}
298
299void InputHandler::process_EV_KEY(input_event& ev)
300{
301 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
302
303 // Handle key-press here
304 LOGEVENT("TOUCH_KEY: %d\n", ev.code);
305 // Left mouse button is treated as a touch
Matt Mowera8a89d12016-12-30 18:10:37 -0600306 if (ev.code == BTN_LEFT)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600307 {
that9fa51532015-01-29 02:04:47 +0100308 MouseCursor *cursor = PageManager::GetMouseCursor();
Matt Mowera8a89d12016-12-30 18:10:37 -0600309 if (ev.value == 1)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600310 {
that9fa51532015-01-29 02:04:47 +0100311 cursor->GetPos(x, y);
312 doTouchStart();
313 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600314 else if (touch_status)
that9fa51532015-01-29 02:04:47 +0100315 {
316 // Left mouse button was previously pressed and now is
317 // being released so send a TOUCH_RELEASE
318 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600319 {
320 cursor->GetPos(x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600321
that9fa51532015-01-29 02:04:47 +0100322 LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y);
323 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600324 }
that9fa51532015-01-29 02:04:47 +0100325 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600326 }
that9fa51532015-01-29 02:04:47 +0100327 }
328 // side mouse button, often used for "back" function
Matt Mowera8a89d12016-12-30 18:10:37 -0600329 else if (ev.code == BTN_SIDE)
that9fa51532015-01-29 02:04:47 +0100330 {
Matt Mowera8a89d12016-12-30 18:10:37 -0600331 if (ev.value == 1)
that9fa51532015-01-29 02:04:47 +0100332 kb->KeyDown(KEY_BACK);
333 else
334 kb->KeyUp(KEY_BACK);
335 } else if (ev.value != 0) {
336 // This is a key press
Ethan Yonker30fa3352015-03-09 13:57:21 -0500337#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
338 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
339 LOGEVENT("key code %i key press == touch start %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
340 doTouchStart();
341 return;
342 }
343#endif
that9fa51532015-01-29 02:04:47 +0100344 if (kb->KeyDown(ev.code)) {
345 // Key repeat is enabled for this key
346 key_status = KS_KEY_PRESSED;
347 touch_status = TS_NONE;
348 gettimeofday(&touchStart, NULL);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600349 } else {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600350 key_status = KS_NONE;
351 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600352 }
that9fa51532015-01-29 02:04:47 +0100353 } else {
354 // This is a key release
355 kb->KeyUp(ev.code);
356 key_status = KS_NONE;
357 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500358#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
359 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
360 LOGEVENT("key code %i key release == touch release %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
361 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
362 }
363#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600364 }
that9fa51532015-01-29 02:04:47 +0100365}
Ethan Yonkere13fa632015-01-27 11:30:03 -0600366
that9fa51532015-01-29 02:04:47 +0100367void InputHandler::process_EV_REL(input_event& ev)
368{
369 // Mouse movement
370 MouseCursor *cursor = PageManager::GetMouseCursor();
371 LOGEVENT("EV_REL %d %d\n", ev.code, ev.value);
Matt Mowera8a89d12016-12-30 18:10:37 -0600372 if (ev.code == REL_X)
that9fa51532015-01-29 02:04:47 +0100373 cursor->Move(ev.value, 0);
Matt Mowera8a89d12016-12-30 18:10:37 -0600374 else if (ev.code == REL_Y)
that9fa51532015-01-29 02:04:47 +0100375 cursor->Move(0, ev.value);
376
Matt Mowera8a89d12016-12-30 18:10:37 -0600377 if (touch_status) {
that9fa51532015-01-29 02:04:47 +0100378 cursor->GetPos(x, y);
379 LOGEVENT("Mouse TOUCH_DRAG: %d, %d\n", x, y);
380 key_status = KS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600381 }
that9fa51532015-01-29 02:04:47 +0100382}
383
384void InputHandler::handleDrag()
385{
386 // This allows us to only send one NotifyTouch event per render
387 // cycle to reduce overhead and perceived input latency.
388 static int prevx = 0, prevy = 0; // these track where the last drag notice was so that we don't send duplicate drag notices
389 if (touch_status && (x != prevx || y != prevy)) {
390 prevx = x;
391 prevy = y;
392 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
393 state = AS_NO_ACTION;
394 else
395 state = AS_IN_ACTION_AREA;
396 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400397}
398
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600399void set_select_fd() {
400 select_fd = ors_read_fd + 1;
401 if (g_pty_fd >= select_fd)
402 select_fd = g_pty_fd + 1;
403 if (PartitionManager.uevent_pfd.fd >= select_fd)
404 select_fd = PartitionManager.uevent_pfd.fd + 1;
405}
406
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600407static void setup_ors_command()
Ethan Yonker03a42f62014-08-08 11:03:51 -0500408{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600409 ors_read_fd = -1;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600410 set_select_fd();
Ethan Yonker03a42f62014-08-08 11:03:51 -0500411
412 unlink(ORS_INPUT_FILE);
413 if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
414 LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600415 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500416 }
417 unlink(ORS_OUTPUT_FILE);
418 if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
419 LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
420 unlink(ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600421 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500422 }
423
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600424 ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK);
425 if (ors_read_fd < 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500426 LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
427 unlink(ORS_INPUT_FILE);
428 unlink(ORS_OUTPUT_FILE);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500429 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600430 set_select_fd();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600431}
Ethan Yonker03a42f62014-08-08 11:03:51 -0500432
that10ae24f2015-12-26 20:53:51 +0100433// callback called after a CLI command was executed
434static void ors_command_done()
435{
436 gui_set_FILE(NULL);
437 fclose(orsout);
438 orsout = NULL;
439
440 if (DataManager::GetIntValue("tw_page_done") == 0) {
441 // The select function will return ready to read and the
442 // read function will return errno 19 no such device unless
443 // we set everything up all over again.
444 close(ors_read_fd);
445 setup_ors_command();
446 }
447}
448
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600449static void ors_command_read()
450{
that10ae24f2015-12-26 20:53:51 +0100451 char command[1024];
452 int read_ret = read(ors_read_fd, &command, sizeof(command));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500453
that10ae24f2015-12-26 20:53:51 +0100454 if (read_ret > 0) {
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600455 command[1022] = '\n';
456 command[1023] = '\0';
457 LOGINFO("Command '%s' received\n", command);
458 orsout = fopen(ORS_OUTPUT_FILE, "w");
459 if (!orsout) {
460 close(ors_read_fd);
461 ors_read_fd = -1;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600462 set_select_fd();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600463 LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
464 unlink(ORS_INPUT_FILE);
465 unlink(ORS_OUTPUT_FILE);
466 return;
467 }
468 if (DataManager::GetIntValue("tw_busy") != 0) {
that10ae24f2015-12-26 20:53:51 +0100469 fputs("Failed, operation in progress\n", orsout);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600470 LOGINFO("Command cannot be performed, operation in progress.\n");
that10ae24f2015-12-26 20:53:51 +0100471 fclose(orsout);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600472 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500473 if (strlen(command) == 11 && strncmp(command, "dumpstrings", 11) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500474 gui_set_FILE(orsout);
475 PageManager::GetResources()->DumpStrings();
that10ae24f2015-12-26 20:53:51 +0100476 ors_command_done();
477 } else {
478 // mirror output messages
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600479 gui_set_FILE(orsout);
that10ae24f2015-12-26 20:53:51 +0100480 // close orsout and restart listener after command is done
481 OpenRecoveryScript::Call_After_CLI_Command(ors_command_done);
482 // run the command in a threaded action...
483 DataManager::SetValue("tw_action", "twcmd");
484 DataManager::SetValue("tw_action_param", command);
485 // ...and switch back to the current page when finished
486 std::string currentPage = PageManager::GetCurrentPage();
487 DataManager::SetValue("tw_has_action2", "1");
488 DataManager::SetValue("tw_action2", "page");
489 DataManager::SetValue("tw_action2_param", currentPage);
490 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
491 DataManager::SetValue("tw_action_text2", "");
492 gui_changePage("singleaction_page");
493 // now immediately return to the GUI main loop (the action runs in the background thread)
494 // 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 -0500495 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500496 }
497 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500498}
499
that10ae24f2015-12-26 20:53:51 +0100500// Get and dispatch input events until it's time to draw the next frame
Dees_Troy51a0e822012-09-05 15:24:24 -0400501// This special function will return immediately the first time, but then
502// always returns 1/30th of a second (or immediately if called later) from
503// the last time it was called
thatde72b6d2015-02-08 08:55:00 +0100504static void loopTimer(int input_timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400505{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200506 static timespec lastCall;
507 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400508
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200509 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400510 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200511 clock_gettime(CLOCK_MONOTONIC, &lastCall);
512 initialized = 1;
513 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400514 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400515
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200516 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500517 {
thatde72b6d2015-02-08 08:55:00 +0100518 bool got_event = input_handler.processInput(input_timeout_ms); // get inputs but don't send drag notices
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200519 timespec curTime;
520 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400521
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200522 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500523
thatc5837f32015-02-01 01:59:43 +0100524 // This is really 2 or 30 times per second
525 // As long as we get events, increase the timeout so we can catch up with input
526 long timeout = got_event ? 500000000 : 33333333;
527
528 if (diff.tv_sec || diff.tv_nsec > timeout)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500529 {
thatc5837f32015-02-01 01:59:43 +0100530 // int32_t input_time = TWFunc::timespec_diff_ms(lastCall, curTime);
531 // LOGINFO("loopTimer(): %u ms, count: %u\n", input_time, count);
532
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200533 lastCall = curTime;
that9fa51532015-01-29 02:04:47 +0100534 input_handler.handleDrag(); // send only drag notices if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200535 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500536 }
537
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200538 // We need to sleep some period time microseconds
Ethan Yonkere13fa632015-01-27 11:30:03 -0600539 //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
540 //usleep(sleepTime); // removed so we can scan for input
thatde72b6d2015-02-08 08:55:00 +0100541 input_timeout_ms = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400543}
544
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600545static int runPages(const char *page_name, const int stop_on_page_done)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500546{
Ethan Yonker3f15be42015-02-09 09:39:47 -0600547 DataManager::SetValue("tw_page_done", 0);
548 DataManager::SetValue("tw_gui_done", 0);
549
Ethan Yonkerafde0982016-01-23 08:55:35 -0600550 if (page_name) {
551 PageManager::SetStartPage(page_name);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600552 gui_changePage(page_name);
Ethan Yonkerafde0982016-01-23 08:55:35 -0600553 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600554
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200555 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500556
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200557 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500558
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600559 struct timeval timeout;
560 fd_set fdset;
561 int has_data = 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100562
thatde72b6d2015-02-08 08:55:00 +0100563 int input_timeout_ms = 0;
564 int idle_frames = 0;
565
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200566 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500567 {
thatde72b6d2015-02-08 08:55:00 +0100568 loopTimer(input_timeout_ms);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600569 FD_ZERO(&fdset);
570 timeout.tv_sec = 0;
571 timeout.tv_usec = 1;
that1964d192016-01-07 00:41:03 +0100572 if (g_pty_fd > 0) {
that1964d192016-01-07 00:41:03 +0100573 FD_SET(g_pty_fd, &fdset);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600574 }
575 if (PartitionManager.uevent_pfd.fd > 0) {
576 FD_SET(PartitionManager.uevent_pfd.fd, &fdset);
that1964d192016-01-07 00:41:03 +0100577 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600578#ifndef TW_OEM_BUILD
that10ae24f2015-12-26 20:53:51 +0100579 if (ors_read_fd > 0 && !orsout) { // orsout is non-NULL if a command is still running
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600580 FD_SET(ors_read_fd, &fdset);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600581 }
582#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600583 // TODO: combine this select with the poll done by input handling
584 has_data = select(select_fd, &fdset, NULL, NULL, &timeout);
585 if (has_data > 0) {
586 if (g_pty_fd > 0 && FD_ISSET(g_pty_fd, &fdset))
587 terminal_pty_read();
588 if (PartitionManager.uevent_pfd.fd > 0 && FD_ISSET(PartitionManager.uevent_pfd.fd, &fdset))
589 PartitionManager.read_uevent();
590 if (ors_read_fd > 0 && !orsout && FD_ISSET(ors_read_fd, &fdset))
591 ors_command_read();
592 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500593
Ethan Yonker04536952015-01-27 08:41:28 -0600594 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500595 {
thatde72b6d2015-02-08 08:55:00 +0100596 int ret = PageManager::Update();
597 if (ret == 0)
598 ++idle_frames;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500599 else if (ret == -2)
600 break; // Theme reload failure
thatde72b6d2015-02-08 08:55:00 +0100601 else
602 idle_frames = 0;
603 // due to possible animation objects, we need to delay activating the input timeout
604 input_timeout_ms = idle_frames > 15 ? 1000 : 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100605
606#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200607 if (ret > 1)
608 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500609
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200610 if (ret > 0)
611 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100612#else
613 if (ret > 1)
614 {
that9fa51532015-01-29 02:04:47 +0100615 timespec start, end;
616 int32_t render_t, flip_t;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100617 clock_gettime(CLOCK_MONOTONIC, &start);
618 PageManager::Render();
619 clock_gettime(CLOCK_MONOTONIC, &end);
620 render_t = TWFunc::timespec_diff_ms(start, end);
621
622 flip();
623 clock_gettime(CLOCK_MONOTONIC, &start);
624 flip_t = TWFunc::timespec_diff_ms(end, start);
625
626 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
627 }
thatde72b6d2015-02-08 08:55:00 +0100628 else if (ret > 0)
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100629 flip();
630#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500631 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200632 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500633 {
Ethan Yonker04536952015-01-27 08:41:28 -0600634 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200635 PageManager::Render();
636 flip();
thatde72b6d2015-02-08 08:55:00 +0100637 input_timeout_ms = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500638 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200639
thatfb759d42015-01-11 12:16:53 +0100640 blankTimer.checkForTimeout();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600641 if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200642 {
643 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600644 break;
645 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600646 if (DataManager::GetIntValue("tw_gui_done") != 0)
647 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500648 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600649 if (ors_read_fd > 0)
650 close(ors_read_fd);
651 ors_read_fd = -1;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600652 set_select_fd();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200653 gGuiRunning = 0;
654 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500655}
656
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200657int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500658{
Ethan Yonker04536952015-01-27 08:41:28 -0600659 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200660 return 0;
661}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500662
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200663int gui_changePage(std::string newPage)
664{
665 LOGINFO("Set page: '%s'\n", newPage.c_str());
666 PageManager::ChangePage(newPage);
Ethan Yonker04536952015-01-27 08:41:28 -0600667 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200668 return 0;
669}
670
671int gui_changeOverlay(std::string overlay)
672{
Ethan Yonker1c273312015-03-16 12:18:56 -0500673 LOGINFO("Set overlay: '%s'\n", overlay.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200674 PageManager::ChangeOverlay(overlay);
Ethan Yonker04536952015-01-27 08:41:28 -0600675 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200676 return 0;
677}
678
thatb2e8f672015-03-05 20:25:39 +0100679std::string gui_parse_text(std::string str)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200680{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200681 // This function parses text for DataManager values encompassed by %value% in the XML
thatb2e8f672015-03-05 20:25:39 +0100682 // and string resources (%@resource_name%)
Ethan Yonker74db1572015-10-28 12:44:49 -0500683 size_t pos = 0, next, end;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200684
685 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500686 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500687 next = str.find("{@", pos);
688 if (next == std::string::npos)
689 break;
690
691 end = str.find('}', next + 1);
692 if (end == std::string::npos)
693 break;
694
695 std::string var = str.substr(next + 2, (end - next) - 2);
696 str.erase(next, (end - next) + 1);
697
698 size_t default_loc = var.find('=', 0);
699 std::string lookup;
700 if (default_loc == std::string::npos) {
701 str.insert(next, PageManager::GetResources()->FindString(var));
702 } else {
703 lookup = var.substr(0, default_loc);
704 std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1);
705 str.insert(next, PageManager::GetResources()->FindString(lookup, default_string));
706 }
707 }
708 pos = 0;
709 while (1)
710 {
711 next = str.find('%', pos);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200712 if (next == std::string::npos)
713 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500714
Ethan Yonker74db1572015-10-28 12:44:49 -0500715 end = str.find('%', next + 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200716 if (end == std::string::npos)
717 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500718
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200719 // We have a block of data
thatb2e8f672015-03-05 20:25:39 +0100720 std::string var = str.substr(next + 1, (end - next) - 1);
721 str.erase(next, (end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500722
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200723 if (next + 1 == end)
724 str.insert(next, 1, '%');
725 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500726 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200727 std::string value;
thatb2e8f672015-03-05 20:25:39 +0100728 if (var.size() > 0 && var[0] == '@') {
729 // this is a string resource ("%@string_name%")
730 value = PageManager::GetResources()->FindString(var.substr(1));
731 str.insert(next, value);
732 }
733 else if (DataManager::GetValue(var, value) == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500735 }
736
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200737 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500738 }
739}
740
Ethan Yonker74db1572015-10-28 12:44:49 -0500741std::string gui_lookup(const std::string& resource_name, const std::string& default_value) {
742 return PageManager::GetResources()->FindString(resource_name, default_value);
743}
744
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200745extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500746{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200747 gr_init();
Matt Mowerb26c1162016-02-03 21:05:57 -0600748 TWFunc::Set_Brightness(DataManager::GetStrValue("tw_brightness"));
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500749
that235c6482016-01-24 21:59:00 +0100750 // load and show splash screen
751 if (PageManager::LoadPackage("splash", TWRES "splash.xml", "splash")) {
752 LOGERR("Failed to load splash screen XML.\n");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500753 }
that235c6482016-01-24 21:59:00 +0100754 else {
755 PageManager::SelectPackage("splash");
756 PageManager::Render();
757 flip();
758 PageManager::ReleasePackage("splash");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600759 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500760
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200761 ev_init();
762 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500763}
764
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200765extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400766{
Ethan Yonker83e82572014-04-04 10:59:28 -0500767#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200768 int check = 0;
769 DataManager::GetValue(TW_IS_ENCRYPTED, check);
Dees Troy3454ade2015-01-20 19:21:04 +0000770
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200771 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500772 {
Dees Troy3454ade2015-01-20 19:21:04 +0000773 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400774 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500775 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200776 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200778 else
779 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500780 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400781
Ethan Yonker86404bd2016-03-31 08:01:05 -0500782 if (check == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200783 {
784 std::string theme_path;
785
786 theme_path = DataManager::GetSettingsStoragePath();
787 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400788 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200789 int retry_count = 5;
790 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200792 usleep(500000);
793 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500794 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200795
Ethan Yonker74db1572015-10-28 12:44:49 -0500796 if (!PartitionManager.Mount_Settings_Storage(true))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500797 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500798 LOGINFO("Unable to mount %s during GUI startup.\n", theme_path.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200799 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500800 }
801 }
802
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200803 theme_path += "/TWRP/theme/ui.zip";
804 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500805 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500806#endif // ifndef TW_OEM_BUILD
Dees Troy3454ade2015-01-20 19:21:04 +0000807 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500808 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500809 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200810 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500812#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400813 }
814 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500815#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200816 // Set the default package
817 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400818
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200819 gGuiInitialized = 1;
820 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400821
822error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500823 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 gGuiInitialized = 0;
825 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826}
827
Ethan Yonkercf50da52015-01-12 21:59:07 -0600828extern "C" int gui_loadCustomResources(void)
829{
830#ifndef TW_OEM_BUILD
831 if (!PartitionManager.Mount_Settings_Storage(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500832 LOGINFO("Unable to mount settings storage during GUI startup.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600833 return -1;
834 }
835
836 std::string theme_path = DataManager::GetSettingsStoragePath();
837 theme_path += "/TWRP/theme/ui.zip";
838 // Check for a custom theme
839 if (TWFunc::Path_Exists(theme_path)) {
840 // There is a custom theme, try to load it
841 if (PageManager::ReloadPackage("TWRP", theme_path)) {
842 // Custom theme failed to load, try to load stock theme
Dees Troy3454ade2015-01-20 19:21:04 +0000843 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500844 gui_err("base_pkg_err=Failed to load base packages.");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600845 goto error;
846 }
847 }
848 }
849 // Set the default package
850 PageManager::SelectPackage("TWRP");
851#endif
852 return 0;
853
854error:
855 LOGERR("An internal error has occurred: unable to load theme.\n");
856 gGuiInitialized = 0;
857 return -1;
858}
859
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200860extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400861{
Ethan Yonkerafde0982016-01-23 08:55:35 -0600862 return gui_startPage("main", 1, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400863}
864
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600865extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000866{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200867 if (!gGuiInitialized)
868 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000869
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200870 // Set the default package
871 PageManager::SelectPackage("TWRP");
872
that9fa51532015-01-29 02:04:47 +0100873 input_handler.init();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600874#ifndef TW_OEM_BUILD
875 if (allow_commands)
876 {
877 if (ors_read_fd < 0)
878 setup_ors_command();
879 } else {
880 if (ors_read_fd >= 0) {
881 close(ors_read_fd);
882 ors_read_fd = -1;
883 }
884 }
885#endif
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600886 return runPages(page_name, stop_on_page_done);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000887}
888
Ethan Yonker63e414f2015-02-06 15:44:39 -0600889
890extern "C" void set_scale_values(float w, float h)
891{
892 scale_theme_w = w;
893 scale_theme_h = h;
894}
895
896extern "C" int scale_theme_x(int initial_x)
897{
898 if (scale_theme_w != 1) {
thatba75a0e2015-02-14 21:20:10 +0100899 int scaled = (float)initial_x * scale_theme_w;
900 if (scaled == 0 && initial_x > 0)
901 return 1;
902 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600903 }
904 return initial_x;
905}
906
907extern "C" int scale_theme_y(int initial_y)
908{
909 if (scale_theme_h != 1) {
thatba75a0e2015-02-14 21:20:10 +0100910 int scaled = (float)initial_y * scale_theme_h;
911 if (scaled == 0 && initial_y > 0)
912 return 1;
913 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600914 }
915 return initial_y;
916}
917
918extern "C" int scale_theme_min(int initial_value)
919{
920 if (scale_theme_w != 1 || scale_theme_h != 1) {
921 if (scale_theme_w < scale_theme_h)
922 return scale_theme_x(initial_value);
923 else
924 return scale_theme_y(initial_value);
925 }
926 return initial_value;
927}
928
929extern "C" float get_scale_w()
930{
931 return scale_theme_w;
932}
933
934extern "C" float get_scale_h()
935{
936 return scale_theme_h;
937}