blob: 31b61be6db593ef0ebfe5d76e2ba25d0c5f276b7 [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
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 -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;
Ethan Yonker63e414f2015-02-06 15:44:39 -060078static float scale_theme_w = 1;
79static float scale_theme_h = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
81// Needed by pages.cpp too
82int gGuiRunning = 0;
83
84static int gRecorder = -1;
85
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086extern "C" void gr_write_frame_to_file(int fd);
Dees_Troy51a0e822012-09-05 15:24:24 -040087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088void flip(void)
Dees_Troy51a0e822012-09-05 15:24:24 -040089{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 if (gRecorder != -1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050091 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 timespec time;
93 clock_gettime(CLOCK_MONOTONIC, &time);
94 write(gRecorder, &time, sizeof(timespec));
95 gr_write_frame_to_file(gRecorder);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050096 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -040098}
99
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100void rapidxml::parse_error_handler(const char *what, void *where)
Dees_Troy51a0e822012-09-05 15:24:24 -0400101{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 fprintf(stderr, "Parser error: %s\n", what);
103 fprintf(stderr, " Start of string: %s\n",(char *) where);
104 LOGERR("Error parsing XML file.\n");
105 //abort();
Dees_Troy51a0e822012-09-05 15:24:24 -0400106}
107
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108static void curtainSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400109{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 gr_color(0, 0, 0, 255);
111 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Ethan Yonker751a85e2014-12-12 16:59:10 -0600112 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 +0200113 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400114}
115
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116static void curtainRaise(gr_surface surface)
Dees_Troy51a0e822012-09-05 15:24:24 -0400117{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 int sy = 0;
119 int h = gr_get_height(gCurtain) - 1;
120 int w = gr_get_width(gCurtain);
121 int fy = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 int msw = gr_get_width(surface);
124 int msh = gr_get_height(surface);
125 int CURTAIN_RATE = msh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500128 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200129 for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500130 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 gr_blit(surface, 0, 0, msw, msh, 0, 0);
132 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
133 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500134 }
135 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 gr_blit(surface, 0, 0, msw, msh, 0, 0);
137 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400138}
139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140void curtainClose()
Dees_Troy51a0e822012-09-05 15:24:24 -0400141{
142#if 0
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 int w = gr_get_width(gCurtain);
144 int h = 1;
145 int sy = gr_get_height(gCurtain) - 1;
146 int fbh = gr_fb_height();
147 int CURTAIN_RATE = fbh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500150 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500152 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
154 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500155 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
157 gr_get_height(gCurtain), 0, 0);
158 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 if (gRecorder != -1)
161 close(gRecorder);
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 int fade;
164 for (fade = 16; fade < 255; fade += CURTAIN_FADE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500165 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
167 gr_get_height(gCurtain), 0, 0);
168 gr_color(0, 0, 0, fade);
169 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
170 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500171 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 gr_color(0, 0, 0, 255);
173 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
174 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500176#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200177 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
178 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500179#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400180}
181
that9fa51532015-01-29 02:04:47 +0100182class InputHandler
Dees_Troy51a0e822012-09-05 15:24:24 -0400183{
that9fa51532015-01-29 02:04:47 +0100184public:
185 void init()
thatfb759d42015-01-11 12:16:53 +0100186 {
that9fa51532015-01-29 02:04:47 +0100187 // these might be read from DataManager in the future
188 touch_hold_ms = 500;
189 touch_repeat_ms = 100;
190 key_hold_ms = 500;
191 key_repeat_ms = 100;
192 touch_status = TS_NONE;
193 key_status = KS_NONE;
194 state = AS_NO_ACTION;
195 x = y = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400196
that9fa51532015-01-29 02:04:47 +0100197#ifndef TW_NO_SCREEN_TIMEOUT
198 {
199 string seconds;
200 DataManager::GetValue("tw_screen_timeout_secs", seconds);
201 blankTimer.setTime(atoi(seconds.c_str()));
202 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600203 }
that9fa51532015-01-29 02:04:47 +0100204#else
205 LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n");
206#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600207 }
208
thatc5837f32015-02-01 01:59:43 +0100209 // process input events. returns true if any event was received.
thatde72b6d2015-02-08 08:55:00 +0100210 bool processInput(int timeout_ms);
thatc5837f32015-02-01 01:59:43 +0100211
that9fa51532015-01-29 02:04:47 +0100212 void handleDrag();
213
214private:
215 // timeouts for touch/key hold and repeat
216 int touch_hold_ms;
217 int touch_repeat_ms;
218 int key_hold_ms;
219 int key_repeat_ms;
220
221 enum touch_status_enum {
222 TS_NONE = 0,
223 TS_TOUCH_AND_HOLD = 1,
224 TS_TOUCH_REPEAT = 2,
225 };
226
227 enum key_status_enum {
228 KS_NONE = 0,
229 KS_KEY_PRESSED = 1,
230 KS_KEY_REPEAT = 2,
231 };
232
233 enum action_state_enum {
234 AS_IN_ACTION_AREA = 0, // we've touched a spot with an action
235 AS_NO_ACTION = 1, // we've touched in an empty area (no action) and ignore remaining events until touch release
236 };
237 touch_status_enum touch_status;
238 key_status_enum key_status;
239 action_state_enum state;
240 int x, y; // x and y coordinates of last touch
241 struct timeval touchStart; // used to track time for long press / key repeat
242
243 void processHoldAndRepeat();
244 void process_EV_REL(input_event& ev);
245 void process_EV_ABS(input_event& ev);
246 void process_EV_KEY(input_event& ev);
247
248 void doTouchStart();
249};
250
251InputHandler input_handler;
252
253
thatde72b6d2015-02-08 08:55:00 +0100254bool InputHandler::processInput(int timeout_ms)
that9fa51532015-01-29 02:04:47 +0100255{
256 input_event ev;
thatde72b6d2015-02-08 08:55:00 +0100257 int ret = ev_get(&ev, timeout_ms);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600258
259 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500260 {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600261 // This path means that we did not get any new touch data, but
262 // we do not get new touch data if you press and hold on either
263 // the screen or on a keyboard key or mouse button
that9fa51532015-01-29 02:04:47 +0100264 if (touch_status || key_status)
265 processHoldAndRepeat();
thatc5837f32015-02-01 01:59:43 +0100266 return (ret != -2); // -2 means no more events in the queue
Ethan Yonkere13fa632015-01-27 11:30:03 -0600267 }
that9fa51532015-01-29 02:04:47 +0100268
269 switch (ev.type)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600270 {
that9fa51532015-01-29 02:04:47 +0100271 case EV_ABS:
272 process_EV_ABS(ev);
273 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600274
that9fa51532015-01-29 02:04:47 +0100275 case EV_REL:
276 process_EV_REL(ev);
277 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600278
that9fa51532015-01-29 02:04:47 +0100279 case EV_KEY:
280 process_EV_KEY(ev);
281 break;
282 }
thatc5837f32015-02-01 01:59:43 +0100283
284 blankTimer.resetTimerAndUnblank();
285 return true; // we got an event, so there might be more in the queue
that9fa51532015-01-29 02:04:47 +0100286}
287
288void InputHandler::processHoldAndRepeat()
289{
290 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
291
292 // touch and key repeat section
293 struct timeval curTime;
294 gettimeofday(&curTime, NULL);
295 long seconds = curTime.tv_sec - touchStart.tv_sec;
296 long useconds = curTime.tv_usec - touchStart.tv_usec;
297 long mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
298
299 if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms)
300 {
301 touch_status = TS_TOUCH_REPEAT;
302 gettimeofday(&touchStart, NULL);
303 LOGEVENT("TOUCH_HOLD: %d,%d\n", x, y);
304 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
that9fa51532015-01-29 02:04:47 +0100305 }
306 else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms)
307 {
308 LOGEVENT("TOUCH_REPEAT: %d,%d\n", x, y);
309 gettimeofday(&touchStart, NULL);
310 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
that9fa51532015-01-29 02:04:47 +0100311 }
312 else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms)
313 {
314 LOGEVENT("KEY_HOLD: %d,%d\n", x, y);
315 gettimeofday(&touchStart, NULL);
316 key_status = KS_KEY_REPEAT;
317 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100318 }
319 else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms)
320 {
321 LOGEVENT("KEY_REPEAT: %d,%d\n", x, y);
322 gettimeofday(&touchStart, NULL);
323 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100324 }
325}
326
327void InputHandler::doTouchStart()
328{
329 LOGEVENT("TOUCH_START: %d,%d\n", x, y);
330 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
331 state = AS_NO_ACTION;
332 else
333 state = AS_IN_ACTION_AREA;
334 touch_status = TS_TOUCH_AND_HOLD;
335 gettimeofday(&touchStart, NULL);
that9fa51532015-01-29 02:04:47 +0100336}
337
338void InputHandler::process_EV_ABS(input_event& ev)
339{
340 x = ev.value >> 16;
341 y = ev.value & 0xFFFF;
342
343 if (ev.code == 0)
344 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500345#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100346 if (state == AS_IN_ACTION_AREA)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500347 {
that9fa51532015-01-29 02:04:47 +0100348 LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y);
349 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
that9fa51532015-01-29 02:04:47 +0100350 }
351 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500352#endif
that9fa51532015-01-29 02:04:47 +0100353 }
354 else
355 {
356 if (!touch_status)
357 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500358#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100359 doTouchStart();
Ethan Yonker30fa3352015-03-09 13:57:21 -0500360#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600361 }
362 else
363 {
that9fa51532015-01-29 02:04:47 +0100364 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600365 {
that9fa51532015-01-29 02:04:47 +0100366 LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400367 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500368 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500369 }
that9fa51532015-01-29 02:04:47 +0100370}
371
372void InputHandler::process_EV_KEY(input_event& ev)
373{
374 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
375
376 // Handle key-press here
377 LOGEVENT("TOUCH_KEY: %d\n", ev.code);
378 // Left mouse button is treated as a touch
379 if(ev.code == BTN_LEFT)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600380 {
that9fa51532015-01-29 02:04:47 +0100381 MouseCursor *cursor = PageManager::GetMouseCursor();
382 if(ev.value == 1)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600383 {
that9fa51532015-01-29 02:04:47 +0100384 cursor->GetPos(x, y);
385 doTouchStart();
386 }
387 else if(touch_status)
388 {
389 // Left mouse button was previously pressed and now is
390 // being released so send a TOUCH_RELEASE
391 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600392 {
393 cursor->GetPos(x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600394
that9fa51532015-01-29 02:04:47 +0100395 LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y);
396 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600397 }
that9fa51532015-01-29 02:04:47 +0100398 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600399 }
that9fa51532015-01-29 02:04:47 +0100400 }
401 // side mouse button, often used for "back" function
402 else if(ev.code == BTN_SIDE)
403 {
404 if(ev.value == 1)
405 kb->KeyDown(KEY_BACK);
406 else
407 kb->KeyUp(KEY_BACK);
408 } else if (ev.value != 0) {
409 // This is a key press
Ethan Yonker30fa3352015-03-09 13:57:21 -0500410#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
411 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
412 LOGEVENT("key code %i key press == touch start %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
413 doTouchStart();
414 return;
415 }
416#endif
that9fa51532015-01-29 02:04:47 +0100417 if (kb->KeyDown(ev.code)) {
418 // Key repeat is enabled for this key
419 key_status = KS_KEY_PRESSED;
420 touch_status = TS_NONE;
421 gettimeofday(&touchStart, NULL);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600422 } else {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600423 key_status = KS_NONE;
424 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600425 }
that9fa51532015-01-29 02:04:47 +0100426 } else {
427 // This is a key release
428 kb->KeyUp(ev.code);
429 key_status = KS_NONE;
430 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500431#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
432 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
433 LOGEVENT("key code %i key release == touch release %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
434 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
435 }
436#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600437 }
that9fa51532015-01-29 02:04:47 +0100438}
Ethan Yonkere13fa632015-01-27 11:30:03 -0600439
that9fa51532015-01-29 02:04:47 +0100440void InputHandler::process_EV_REL(input_event& ev)
441{
442 // Mouse movement
443 MouseCursor *cursor = PageManager::GetMouseCursor();
444 LOGEVENT("EV_REL %d %d\n", ev.code, ev.value);
445 if(ev.code == REL_X)
446 cursor->Move(ev.value, 0);
447 else if(ev.code == REL_Y)
448 cursor->Move(0, ev.value);
449
450 if(touch_status) {
451 cursor->GetPos(x, y);
452 LOGEVENT("Mouse TOUCH_DRAG: %d, %d\n", x, y);
453 key_status = KS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600454 }
that9fa51532015-01-29 02:04:47 +0100455}
456
457void InputHandler::handleDrag()
458{
459 // This allows us to only send one NotifyTouch event per render
460 // cycle to reduce overhead and perceived input latency.
461 static int prevx = 0, prevy = 0; // these track where the last drag notice was so that we don't send duplicate drag notices
462 if (touch_status && (x != prevx || y != prevy)) {
463 prevx = x;
464 prevy = y;
465 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
466 state = AS_NO_ACTION;
467 else
468 state = AS_IN_ACTION_AREA;
469 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400470}
471
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600472static void setup_ors_command()
Ethan Yonker03a42f62014-08-08 11:03:51 -0500473{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600474 ors_read_fd = -1;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500475
476 unlink(ORS_INPUT_FILE);
477 if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
478 LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600479 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500480 }
481 unlink(ORS_OUTPUT_FILE);
482 if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
483 LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
484 unlink(ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600485 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500486 }
487
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600488 ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK);
489 if (ors_read_fd < 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500490 LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
491 unlink(ORS_INPUT_FILE);
492 unlink(ORS_OUTPUT_FILE);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500493 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600494}
Ethan Yonker03a42f62014-08-08 11:03:51 -0500495
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600496static void ors_command_read()
497{
498 FILE* orsout;
499 char command[1024], result[512];
500 int set_page_done = 0, read_ret = 0;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500501
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600502 if ((read_ret = read(ors_read_fd, &command, sizeof(command))) > 0) {
503 command[1022] = '\n';
504 command[1023] = '\0';
505 LOGINFO("Command '%s' received\n", command);
506 orsout = fopen(ORS_OUTPUT_FILE, "w");
507 if (!orsout) {
508 close(ors_read_fd);
509 ors_read_fd = -1;
510 LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
511 unlink(ORS_INPUT_FILE);
512 unlink(ORS_OUTPUT_FILE);
513 return;
514 }
515 if (DataManager::GetIntValue("tw_busy") != 0) {
516 strcpy(result, "Failed, operation in progress\n");
517 fprintf(orsout, "%s", result);
518 LOGINFO("Command cannot be performed, operation in progress.\n");
519 } else {
520 if (gui_console_only() == 0) {
521 LOGINFO("Console started successfully\n");
522 gui_set_FILE(orsout);
523 if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) {
524 char* filename = command + 11;
525 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
526 LOGERR("Unable to copy script file\n");
527 } else {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500528 OpenRecoveryScript::run_script_file();
529 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600530 } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) {
531 char* varname = command + 4;
532 string temp;
533 DataManager::GetValue(varname, temp);
534 gui_print("%s = %s\n", varname, temp.c_str());
535 } else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) {
536 char* pass = command + 8;
537 gui_print("Attempting to decrypt data partition via command line.\n");
538 if (PartitionManager.Decrypt_Device(pass) == 0) {
539 set_page_done = 1;
540 }
541 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
542 OpenRecoveryScript::run_script_file();
Ethan Yonker03a42f62014-08-08 11:03:51 -0500543 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600544 gui_set_FILE(NULL);
Ethan Yonker04536952015-01-27 08:41:28 -0600545 gGuiConsoleTerminate.set_value(1);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500546 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500547 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600548 fclose(orsout);
549 LOGINFO("Done reading ORS command from command line\n");
550 if (set_page_done) {
551 DataManager::SetValue("tw_page_done", 1);
552 } else {
553 // The select function will return ready to read and the
554 // read function will return errno 19 no such device unless
555 // we set everything up all over again.
556 close(ors_read_fd);
557 setup_ors_command();
558 }
559 } else {
560 LOGINFO("ORS command line read returned an error: %i, %i, %s\n", read_ret, errno, strerror(errno));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500561 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600562 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500563}
564
Dees_Troy51a0e822012-09-05 15:24:24 -0400565// This special function will return immediately the first time, but then
566// always returns 1/30th of a second (or immediately if called later) from
567// the last time it was called
thatde72b6d2015-02-08 08:55:00 +0100568static void loopTimer(int input_timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400569{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200570 static timespec lastCall;
571 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400572
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200573 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400574 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200575 clock_gettime(CLOCK_MONOTONIC, &lastCall);
576 initialized = 1;
577 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400578 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400579
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200580 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500581 {
thatde72b6d2015-02-08 08:55:00 +0100582 bool got_event = input_handler.processInput(input_timeout_ms); // get inputs but don't send drag notices
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 timespec curTime;
584 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200586 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500587
thatc5837f32015-02-01 01:59:43 +0100588 // This is really 2 or 30 times per second
589 // As long as we get events, increase the timeout so we can catch up with input
590 long timeout = got_event ? 500000000 : 33333333;
591
592 if (diff.tv_sec || diff.tv_nsec > timeout)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500593 {
thatc5837f32015-02-01 01:59:43 +0100594 // int32_t input_time = TWFunc::timespec_diff_ms(lastCall, curTime);
595 // LOGINFO("loopTimer(): %u ms, count: %u\n", input_time, count);
596
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200597 lastCall = curTime;
that9fa51532015-01-29 02:04:47 +0100598 input_handler.handleDrag(); // send only drag notices if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500600 }
601
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200602 // We need to sleep some period time microseconds
Ethan Yonkere13fa632015-01-27 11:30:03 -0600603 //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
604 //usleep(sleepTime); // removed so we can scan for input
thatde72b6d2015-02-08 08:55:00 +0100605 input_timeout_ms = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200606 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400607}
608
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600609static int runPages(const char *page_name, const int stop_on_page_done)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500610{
Ethan Yonker3f15be42015-02-09 09:39:47 -0600611 DataManager::SetValue("tw_page_done", 0);
612 DataManager::SetValue("tw_gui_done", 0);
613
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600614 if (page_name)
615 gui_changePage(page_name);
616
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200617 // Raise the curtain
618 if (gCurtain != NULL)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500619 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200620 gr_surface surface;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500621
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200622 PageManager::Render();
623 gr_get_surface(&surface);
624 curtainRaise(surface);
625 gr_free_surface(surface);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500626 }
627
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200628 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500629
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200630 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500631
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600632#ifndef TW_OEM_BUILD
633 struct timeval timeout;
634 fd_set fdset;
635 int has_data = 0;
636#endif
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100637
thatde72b6d2015-02-08 08:55:00 +0100638 int input_timeout_ms = 0;
639 int idle_frames = 0;
640
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200641 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500642 {
thatde72b6d2015-02-08 08:55:00 +0100643 loopTimer(input_timeout_ms);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600644#ifndef TW_OEM_BUILD
645 if (ors_read_fd > 0) {
646 FD_ZERO(&fdset);
647 FD_SET(ors_read_fd, &fdset);
648 timeout.tv_sec = 0;
649 timeout.tv_usec = 1;
650 has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout);
651 if (has_data > 0) {
652 ors_command_read();
653 }
654 }
655#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500656
Ethan Yonker04536952015-01-27 08:41:28 -0600657 if (gGuiConsoleRunning.get_value()) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500658 continue;
659 }
660
Ethan Yonker04536952015-01-27 08:41:28 -0600661 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500662 {
thatde72b6d2015-02-08 08:55:00 +0100663 int ret = PageManager::Update();
664 if (ret == 0)
665 ++idle_frames;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500666 else if (ret == -2)
667 break; // Theme reload failure
thatde72b6d2015-02-08 08:55:00 +0100668 else
669 idle_frames = 0;
670 // due to possible animation objects, we need to delay activating the input timeout
671 input_timeout_ms = idle_frames > 15 ? 1000 : 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100672
673#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200674 if (ret > 1)
675 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500676
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200677 if (ret > 0)
678 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100679#else
680 if (ret > 1)
681 {
that9fa51532015-01-29 02:04:47 +0100682 timespec start, end;
683 int32_t render_t, flip_t;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100684 clock_gettime(CLOCK_MONOTONIC, &start);
685 PageManager::Render();
686 clock_gettime(CLOCK_MONOTONIC, &end);
687 render_t = TWFunc::timespec_diff_ms(start, end);
688
689 flip();
690 clock_gettime(CLOCK_MONOTONIC, &start);
691 flip_t = TWFunc::timespec_diff_ms(end, start);
692
693 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
694 }
thatde72b6d2015-02-08 08:55:00 +0100695 else if (ret > 0)
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100696 flip();
697#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500698 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200699 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500700 {
Ethan Yonker04536952015-01-27 08:41:28 -0600701 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200702 PageManager::Render();
703 flip();
thatde72b6d2015-02-08 08:55:00 +0100704 input_timeout_ms = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500705 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200706
thatfb759d42015-01-11 12:16:53 +0100707 blankTimer.checkForTimeout();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600708 if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200709 {
710 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600711 break;
712 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600713 if (DataManager::GetIntValue("tw_gui_done") != 0)
714 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500715 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600716 if (ors_read_fd > 0)
717 close(ors_read_fd);
718 ors_read_fd = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200719 gGuiRunning = 0;
720 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500721}
722
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200723int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500724{
Ethan Yonker04536952015-01-27 08:41:28 -0600725 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200726 return 0;
727}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500728
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200729int gui_changePage(std::string newPage)
730{
731 LOGINFO("Set page: '%s'\n", newPage.c_str());
732 PageManager::ChangePage(newPage);
Ethan Yonker04536952015-01-27 08:41:28 -0600733 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734 return 0;
735}
736
737int gui_changeOverlay(std::string overlay)
738{
Ethan Yonker1c273312015-03-16 12:18:56 -0500739 LOGINFO("Set overlay: '%s'\n", overlay.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200740 PageManager::ChangeOverlay(overlay);
Ethan Yonker04536952015-01-27 08:41:28 -0600741 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200742 return 0;
743}
744
745int gui_changePackage(std::string newPackage)
746{
747 PageManager::SelectPackage(newPackage);
Ethan Yonker04536952015-01-27 08:41:28 -0600748 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200749 return 0;
750}
751
thatb2e8f672015-03-05 20:25:39 +0100752std::string gui_parse_text(std::string str)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200753{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200754 // This function parses text for DataManager values encompassed by %value% in the XML
thatb2e8f672015-03-05 20:25:39 +0100755 // and string resources (%@resource_name%)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200756 size_t pos = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200757
758 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500759 {
thatb2e8f672015-03-05 20:25:39 +0100760 size_t next = str.find('%', pos);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200761 if (next == std::string::npos)
762 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500763
thatb2e8f672015-03-05 20:25:39 +0100764 size_t end = str.find('%', next + 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200765 if (end == std::string::npos)
766 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500767
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200768 // We have a block of data
thatb2e8f672015-03-05 20:25:39 +0100769 std::string var = str.substr(next + 1, (end - next) - 1);
770 str.erase(next, (end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500771
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200772 if (next + 1 == end)
773 str.insert(next, 1, '%');
774 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500775 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200776 std::string value;
thatb2e8f672015-03-05 20:25:39 +0100777 if (var.size() > 0 && var[0] == '@') {
778 // this is a string resource ("%@string_name%")
779 value = PageManager::GetResources()->FindString(var.substr(1));
780 str.insert(next, value);
781 }
782 else if (DataManager::GetValue(var, value) == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200783 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500784 }
785
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200786 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500787 }
788}
789
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200790extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500791{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200792 gr_init();
Dees Troy3454ade2015-01-20 19:21:04 +0000793 std::string curtain_path = TWRES "images/curtain.jpg";
Ethan Yonker63e414f2015-02-06 15:44:39 -0600794 gr_surface source_Surface = NULL;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500795
Ethan Yonker63e414f2015-02-06 15:44:39 -0600796 if (res_create_surface(curtain_path.c_str(), &source_Surface))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500797 {
Ethan Yonker591b9202015-03-11 11:17:15 -0500798 printf("Unable to locate '%s'\nDid you set a TW_THEME in your config files?\n", curtain_path.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200799 return -1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500800 }
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500801 if (gr_get_width(source_Surface) != (unsigned)gr_fb_width() || gr_get_height(source_Surface) != (unsigned)gr_fb_height()) {
Ethan Yonker63e414f2015-02-06 15:44:39 -0600802 // We need to scale the curtain to fit the screen
803 float scale_w = (float)gr_fb_width() / (float)gr_get_width(source_Surface);
804 float scale_h = (float)gr_fb_height() / (float)gr_get_height(source_Surface);
805 if (res_scale_surface(source_Surface, &gCurtain, scale_w, scale_h)) {
806 LOGINFO("Failed to scale curtain\n");
807 gCurtain = source_Surface;
808 } else {
809 LOGINFO("Scaling the curtain width %fx and height %fx\n", scale_w, scale_h);
810 }
811 } else {
812 gCurtain = source_Surface;
813 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500814
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200815 curtainSet();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500816
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200817 ev_init();
818 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500819}
820
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200821extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400822{
Ethan Yonker83e82572014-04-04 10:59:28 -0500823#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 int check = 0;
825 DataManager::GetValue(TW_IS_ENCRYPTED, check);
Dees Troy3454ade2015-01-20 19:21:04 +0000826
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200827 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500828 {
Dees Troy3454ade2015-01-20 19:21:04 +0000829 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400830 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200831 LOGERR("Failed to load base packages.\n");
832 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400833 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200834 else
835 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500836 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400837
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200838 if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main"))
839 {
840 std::string theme_path;
841
842 theme_path = DataManager::GetSettingsStoragePath();
843 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400844 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200845 int retry_count = 5;
846 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400847 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200848 usleep(500000);
849 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500850 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200851
852 if (!PartitionManager.Mount_Settings_Storage(false))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500853 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200854 LOGERR("Unable to mount %s during GUI startup.\n",
855 theme_path.c_str());
856 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500857 }
858 }
859
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200860 theme_path += "/TWRP/theme/ui.zip";
861 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500862 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500863#endif // ifndef TW_OEM_BUILD
Dees Troy3454ade2015-01-20 19:21:04 +0000864 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500865 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200866 LOGERR("Failed to load base packages.\n");
867 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400868 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500869#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400870 }
871 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500872#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200873 // Set the default package
874 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400875
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200876 gGuiInitialized = 1;
877 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400878
879error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500880 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200881 gGuiInitialized = 0;
882 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400883}
884
Ethan Yonkercf50da52015-01-12 21:59:07 -0600885extern "C" int gui_loadCustomResources(void)
886{
887#ifndef TW_OEM_BUILD
888 if (!PartitionManager.Mount_Settings_Storage(false)) {
889 LOGERR("Unable to mount settings storage during GUI startup.\n");
890 return -1;
891 }
892
893 std::string theme_path = DataManager::GetSettingsStoragePath();
894 theme_path += "/TWRP/theme/ui.zip";
895 // Check for a custom theme
896 if (TWFunc::Path_Exists(theme_path)) {
897 // There is a custom theme, try to load it
898 if (PageManager::ReloadPackage("TWRP", theme_path)) {
899 // Custom theme failed to load, try to load stock theme
Dees Troy3454ade2015-01-20 19:21:04 +0000900 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
Ethan Yonkercf50da52015-01-12 21:59:07 -0600901 LOGERR("Failed to load base packages.\n");
902 goto error;
903 }
904 }
905 }
906 // Set the default package
907 PageManager::SelectPackage("TWRP");
908#endif
909 return 0;
910
911error:
912 LOGERR("An internal error has occurred: unable to load theme.\n");
913 gGuiInitialized = 0;
914 return -1;
915}
916
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200917extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400918{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600919 return gui_startPage(NULL, 1, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400920}
921
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600922extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000923{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200924 if (!gGuiInitialized)
925 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000926
Ethan Yonker04536952015-01-27 08:41:28 -0600927 gGuiConsoleTerminate.set_value(1);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000928
Ethan Yonker04536952015-01-27 08:41:28 -0600929 while (gGuiConsoleRunning.get_value())
Ethan Yonkere13fa632015-01-27 11:30:03 -0600930 usleep(10000);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000931
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200932 // Set the default package
933 PageManager::SelectPackage("TWRP");
934
that9fa51532015-01-29 02:04:47 +0100935 input_handler.init();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600936#ifndef TW_OEM_BUILD
937 if (allow_commands)
938 {
939 if (ors_read_fd < 0)
940 setup_ors_command();
941 } else {
942 if (ors_read_fd >= 0) {
943 close(ors_read_fd);
944 ors_read_fd = -1;
945 }
946 }
947#endif
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600948 return runPages(page_name, stop_on_page_done);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000949}
950
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500951static void * console_thread(void *cookie __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400952{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200953 PageManager::SwitchToConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400954
Ethan Yonker04536952015-01-27 08:41:28 -0600955 while (!gGuiConsoleTerminate.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500956 {
thatde72b6d2015-02-08 08:55:00 +0100957 loopTimer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400958
Ethan Yonker04536952015-01-27 08:41:28 -0600959 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500960 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200961 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400962
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200963 ret = PageManager::Update();
964 if (ret > 1)
965 PageManager::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400966
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200967 if (ret > 0)
968 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400969
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200970 if (ret < 0)
971 LOGERR("An update request has failed.\n");
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500972 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200973 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500974 {
Ethan Yonker04536952015-01-27 08:41:28 -0600975 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200976 PageManager::Render();
977 flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500978 }
979 }
Ethan Yonker04536952015-01-27 08:41:28 -0600980 gGuiConsoleRunning.set_value(0);
981 gForceRender.set_value(1); // this will kickstart the GUI to render again
Ethan Yonker03a42f62014-08-08 11:03:51 -0500982 PageManager::EndConsole();
983 LOGINFO("Console stopping\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200984 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400985}
986
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200987extern "C" int gui_console_only(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400988{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200989 if (!gGuiInitialized)
990 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400991
Ethan Yonker04536952015-01-27 08:41:28 -0600992 gGuiConsoleTerminate.set_value(0);
Ethan Yonkerffbd6ff2014-10-22 10:40:40 -0500993
Ethan Yonker04536952015-01-27 08:41:28 -0600994 if (gGuiConsoleRunning.get_value())
Ethan Yonkerffbd6ff2014-10-22 10:40:40 -0500995 return 0;
996
Ethan Yonker04536952015-01-27 08:41:28 -0600997 gGuiConsoleRunning.set_value(1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400998
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200999 // Start by spinning off an input handler.
1000 pthread_t t;
1001 pthread_create(&t, NULL, console_thread, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001002
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001003 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001004}
Ethan Yonker63e414f2015-02-06 15:44:39 -06001005
1006extern "C" void set_scale_values(float w, float h)
1007{
1008 scale_theme_w = w;
1009 scale_theme_h = h;
1010}
1011
1012extern "C" int scale_theme_x(int initial_x)
1013{
1014 if (scale_theme_w != 1) {
thatba75a0e2015-02-14 21:20:10 +01001015 int scaled = (float)initial_x * scale_theme_w;
1016 if (scaled == 0 && initial_x > 0)
1017 return 1;
1018 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -06001019 }
1020 return initial_x;
1021}
1022
1023extern "C" int scale_theme_y(int initial_y)
1024{
1025 if (scale_theme_h != 1) {
thatba75a0e2015-02-14 21:20:10 +01001026 int scaled = (float)initial_y * scale_theme_h;
1027 if (scaled == 0 && initial_y > 0)
1028 return 1;
1029 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -06001030 }
1031 return initial_y;
1032}
1033
1034extern "C" int scale_theme_min(int initial_value)
1035{
1036 if (scale_theme_w != 1 || scale_theme_h != 1) {
1037 if (scale_theme_w < scale_theme_h)
1038 return scale_theme_x(initial_value);
1039 else
1040 return scale_theme_y(initial_value);
1041 }
1042 return initial_value;
1043}
1044
1045extern "C" float get_scale_w()
1046{
1047 return scale_theme_w;
1048}
1049
1050extern "C" float get_scale_h()
1051{
1052 return scale_theme_h;
1053}