blob: 08178fc17d4bc952ffd7bedde840eebddb9f8e42 [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 gForceRender;
73const int gNoAnimation = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050074blanktimer blankTimer;
Ethan Yonkerfd0439e2015-01-14 11:08:13 -060075int ors_read_fd = -1;
that10ae24f2015-12-26 20:53:51 +010076static FILE* orsout = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -060077static float scale_theme_w = 1;
78static float scale_theme_h = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
80// Needed by pages.cpp too
81int gGuiRunning = 0;
82
that1964d192016-01-07 00:41:03 +010083int g_pty_fd = -1; // set by terminal on init
84void terminal_pty_read();
85
Dees_Troy51a0e822012-09-05 15:24:24 -040086static int gRecorder = -1;
87
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088extern "C" void gr_write_frame_to_file(int fd);
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090void flip(void)
Dees_Troy51a0e822012-09-05 15:24:24 -040091{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 if (gRecorder != -1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050093 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 timespec time;
95 clock_gettime(CLOCK_MONOTONIC, &time);
96 write(gRecorder, &time, sizeof(timespec));
97 gr_write_frame_to_file(gRecorder);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050098 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400100}
101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102void rapidxml::parse_error_handler(const char *what, void *where)
Dees_Troy51a0e822012-09-05 15:24:24 -0400103{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 fprintf(stderr, "Parser error: %s\n", what);
105 fprintf(stderr, " Start of string: %s\n",(char *) where);
106 LOGERR("Error parsing XML file.\n");
107 //abort();
Dees_Troy51a0e822012-09-05 15:24:24 -0400108}
109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110static void curtainSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400111{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 gr_color(0, 0, 0, 255);
113 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Ethan Yonker751a85e2014-12-12 16:59:10 -0600114 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 +0200115 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400116}
117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118static void curtainRaise(gr_surface surface)
Dees_Troy51a0e822012-09-05 15:24:24 -0400119{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 int sy = 0;
121 int h = gr_get_height(gCurtain) - 1;
122 int w = gr_get_width(gCurtain);
123 int fy = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 int msw = gr_get_width(surface);
126 int msh = gr_get_height(surface);
127 int CURTAIN_RATE = msh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200129 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500130 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500132 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 gr_blit(surface, 0, 0, msw, msh, 0, 0);
134 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
135 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500136 }
137 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 gr_blit(surface, 0, 0, msw, msh, 0, 0);
139 flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400140}
141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142void curtainClose()
Dees_Troy51a0e822012-09-05 15:24:24 -0400143{
144#if 0
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 int w = gr_get_width(gCurtain);
146 int h = 1;
147 int sy = gr_get_height(gCurtain) - 1;
148 int fbh = gr_fb_height();
149 int CURTAIN_RATE = fbh / 30;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 if (gNoAnimation == 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500152 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500154 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 gr_blit(gCurtain, 0, sy, w, h, 0, 0);
156 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500157 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
159 gr_get_height(gCurtain), 0, 0);
160 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 if (gRecorder != -1)
163 close(gRecorder);
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 int fade;
166 for (fade = 16; fade < 255; fade += CURTAIN_FADE)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500167 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain),
169 gr_get_height(gCurtain), 0, 0);
170 gr_color(0, 0, 0, fade);
171 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
172 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500173 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 gr_color(0, 0, 0, 255);
175 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
176 gr_flip();
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500178#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200179 gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
180 gr_flip();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500181#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400182}
183
that9fa51532015-01-29 02:04:47 +0100184class InputHandler
Dees_Troy51a0e822012-09-05 15:24:24 -0400185{
that9fa51532015-01-29 02:04:47 +0100186public:
187 void init()
thatfb759d42015-01-11 12:16:53 +0100188 {
that9fa51532015-01-29 02:04:47 +0100189 // these might be read from DataManager in the future
190 touch_hold_ms = 500;
191 touch_repeat_ms = 100;
192 key_hold_ms = 500;
193 key_repeat_ms = 100;
194 touch_status = TS_NONE;
195 key_status = KS_NONE;
196 state = AS_NO_ACTION;
197 x = y = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400198
that9fa51532015-01-29 02:04:47 +0100199#ifndef TW_NO_SCREEN_TIMEOUT
200 {
201 string seconds;
202 DataManager::GetValue("tw_screen_timeout_secs", seconds);
203 blankTimer.setTime(atoi(seconds.c_str()));
204 blankTimer.resetTimerAndUnblank();
Ethan Yonkere13fa632015-01-27 11:30:03 -0600205 }
that9fa51532015-01-29 02:04:47 +0100206#else
207 LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n");
208#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600209 }
210
thatc5837f32015-02-01 01:59:43 +0100211 // process input events. returns true if any event was received.
thatde72b6d2015-02-08 08:55:00 +0100212 bool processInput(int timeout_ms);
thatc5837f32015-02-01 01:59:43 +0100213
that9fa51532015-01-29 02:04:47 +0100214 void handleDrag();
215
216private:
217 // timeouts for touch/key hold and repeat
218 int touch_hold_ms;
219 int touch_repeat_ms;
220 int key_hold_ms;
221 int key_repeat_ms;
222
223 enum touch_status_enum {
224 TS_NONE = 0,
225 TS_TOUCH_AND_HOLD = 1,
226 TS_TOUCH_REPEAT = 2,
227 };
228
229 enum key_status_enum {
230 KS_NONE = 0,
231 KS_KEY_PRESSED = 1,
232 KS_KEY_REPEAT = 2,
233 };
234
235 enum action_state_enum {
236 AS_IN_ACTION_AREA = 0, // we've touched a spot with an action
237 AS_NO_ACTION = 1, // we've touched in an empty area (no action) and ignore remaining events until touch release
238 };
239 touch_status_enum touch_status;
240 key_status_enum key_status;
241 action_state_enum state;
242 int x, y; // x and y coordinates of last touch
243 struct timeval touchStart; // used to track time for long press / key repeat
244
245 void processHoldAndRepeat();
246 void process_EV_REL(input_event& ev);
247 void process_EV_ABS(input_event& ev);
248 void process_EV_KEY(input_event& ev);
249
250 void doTouchStart();
251};
252
253InputHandler input_handler;
254
255
thatde72b6d2015-02-08 08:55:00 +0100256bool InputHandler::processInput(int timeout_ms)
that9fa51532015-01-29 02:04:47 +0100257{
258 input_event ev;
thatde72b6d2015-02-08 08:55:00 +0100259 int ret = ev_get(&ev, timeout_ms);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600260
261 if (ret < 0)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500262 {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600263 // This path means that we did not get any new touch data, but
264 // we do not get new touch data if you press and hold on either
265 // the screen or on a keyboard key or mouse button
that9fa51532015-01-29 02:04:47 +0100266 if (touch_status || key_status)
267 processHoldAndRepeat();
thatc5837f32015-02-01 01:59:43 +0100268 return (ret != -2); // -2 means no more events in the queue
Ethan Yonkere13fa632015-01-27 11:30:03 -0600269 }
that9fa51532015-01-29 02:04:47 +0100270
271 switch (ev.type)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600272 {
that9fa51532015-01-29 02:04:47 +0100273 case EV_ABS:
274 process_EV_ABS(ev);
275 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600276
that9fa51532015-01-29 02:04:47 +0100277 case EV_REL:
278 process_EV_REL(ev);
279 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600280
that9fa51532015-01-29 02:04:47 +0100281 case EV_KEY:
282 process_EV_KEY(ev);
283 break;
284 }
thatc5837f32015-02-01 01:59:43 +0100285
286 blankTimer.resetTimerAndUnblank();
287 return true; // we got an event, so there might be more in the queue
that9fa51532015-01-29 02:04:47 +0100288}
289
290void InputHandler::processHoldAndRepeat()
291{
292 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
293
294 // touch and key repeat section
295 struct timeval curTime;
296 gettimeofday(&curTime, NULL);
297 long seconds = curTime.tv_sec - touchStart.tv_sec;
298 long useconds = curTime.tv_usec - touchStart.tv_usec;
299 long mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
300
301 if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms)
302 {
303 touch_status = TS_TOUCH_REPEAT;
304 gettimeofday(&touchStart, NULL);
305 LOGEVENT("TOUCH_HOLD: %d,%d\n", x, y);
306 PageManager::NotifyTouch(TOUCH_HOLD, x, y);
that9fa51532015-01-29 02:04:47 +0100307 }
308 else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms)
309 {
310 LOGEVENT("TOUCH_REPEAT: %d,%d\n", x, y);
311 gettimeofday(&touchStart, NULL);
312 PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
that9fa51532015-01-29 02:04:47 +0100313 }
314 else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms)
315 {
316 LOGEVENT("KEY_HOLD: %d,%d\n", x, y);
317 gettimeofday(&touchStart, NULL);
318 key_status = KS_KEY_REPEAT;
319 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100320 }
321 else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms)
322 {
323 LOGEVENT("KEY_REPEAT: %d,%d\n", x, y);
324 gettimeofday(&touchStart, NULL);
325 kb->KeyRepeat();
that9fa51532015-01-29 02:04:47 +0100326 }
327}
328
329void InputHandler::doTouchStart()
330{
331 LOGEVENT("TOUCH_START: %d,%d\n", x, y);
332 if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
333 state = AS_NO_ACTION;
334 else
335 state = AS_IN_ACTION_AREA;
336 touch_status = TS_TOUCH_AND_HOLD;
337 gettimeofday(&touchStart, NULL);
that9fa51532015-01-29 02:04:47 +0100338}
339
340void InputHandler::process_EV_ABS(input_event& ev)
341{
342 x = ev.value >> 16;
343 y = ev.value & 0xFFFF;
344
345 if (ev.code == 0)
346 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500347#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100348 if (state == AS_IN_ACTION_AREA)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500349 {
that9fa51532015-01-29 02:04:47 +0100350 LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y);
351 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
that9fa51532015-01-29 02:04:47 +0100352 }
353 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500354#endif
that9fa51532015-01-29 02:04:47 +0100355 }
356 else
357 {
358 if (!touch_status)
359 {
Ethan Yonker30fa3352015-03-09 13:57:21 -0500360#ifndef TW_USE_KEY_CODE_TOUCH_SYNC
that9fa51532015-01-29 02:04:47 +0100361 doTouchStart();
Ethan Yonker30fa3352015-03-09 13:57:21 -0500362#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600363 }
364 else
365 {
that9fa51532015-01-29 02:04:47 +0100366 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600367 {
that9fa51532015-01-29 02:04:47 +0100368 LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400369 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500370 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500371 }
that9fa51532015-01-29 02:04:47 +0100372}
373
374void InputHandler::process_EV_KEY(input_event& ev)
375{
376 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
377
378 // Handle key-press here
379 LOGEVENT("TOUCH_KEY: %d\n", ev.code);
380 // Left mouse button is treated as a touch
381 if(ev.code == BTN_LEFT)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600382 {
that9fa51532015-01-29 02:04:47 +0100383 MouseCursor *cursor = PageManager::GetMouseCursor();
384 if(ev.value == 1)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600385 {
that9fa51532015-01-29 02:04:47 +0100386 cursor->GetPos(x, y);
387 doTouchStart();
388 }
389 else if(touch_status)
390 {
391 // Left mouse button was previously pressed and now is
392 // being released so send a TOUCH_RELEASE
393 if (state == AS_IN_ACTION_AREA)
Ethan Yonkere13fa632015-01-27 11:30:03 -0600394 {
395 cursor->GetPos(x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600396
that9fa51532015-01-29 02:04:47 +0100397 LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y);
398 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600399 }
that9fa51532015-01-29 02:04:47 +0100400 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600401 }
that9fa51532015-01-29 02:04:47 +0100402 }
403 // side mouse button, often used for "back" function
404 else if(ev.code == BTN_SIDE)
405 {
406 if(ev.value == 1)
407 kb->KeyDown(KEY_BACK);
408 else
409 kb->KeyUp(KEY_BACK);
410 } else if (ev.value != 0) {
411 // This is a key press
Ethan Yonker30fa3352015-03-09 13:57:21 -0500412#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
413 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
414 LOGEVENT("key code %i key press == touch start %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
415 doTouchStart();
416 return;
417 }
418#endif
that9fa51532015-01-29 02:04:47 +0100419 if (kb->KeyDown(ev.code)) {
420 // Key repeat is enabled for this key
421 key_status = KS_KEY_PRESSED;
422 touch_status = TS_NONE;
423 gettimeofday(&touchStart, NULL);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600424 } else {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600425 key_status = KS_NONE;
426 touch_status = TS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600427 }
that9fa51532015-01-29 02:04:47 +0100428 } else {
429 // This is a key release
430 kb->KeyUp(ev.code);
431 key_status = KS_NONE;
432 touch_status = TS_NONE;
Ethan Yonker30fa3352015-03-09 13:57:21 -0500433#ifdef TW_USE_KEY_CODE_TOUCH_SYNC
434 if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) {
435 LOGEVENT("key code %i key release == touch release %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y);
436 PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
437 }
438#endif
Ethan Yonkere13fa632015-01-27 11:30:03 -0600439 }
that9fa51532015-01-29 02:04:47 +0100440}
Ethan Yonkere13fa632015-01-27 11:30:03 -0600441
that9fa51532015-01-29 02:04:47 +0100442void InputHandler::process_EV_REL(input_event& ev)
443{
444 // Mouse movement
445 MouseCursor *cursor = PageManager::GetMouseCursor();
446 LOGEVENT("EV_REL %d %d\n", ev.code, ev.value);
447 if(ev.code == REL_X)
448 cursor->Move(ev.value, 0);
449 else if(ev.code == REL_Y)
450 cursor->Move(0, ev.value);
451
452 if(touch_status) {
453 cursor->GetPos(x, y);
454 LOGEVENT("Mouse TOUCH_DRAG: %d, %d\n", x, y);
455 key_status = KS_NONE;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600456 }
that9fa51532015-01-29 02:04:47 +0100457}
458
459void InputHandler::handleDrag()
460{
461 // This allows us to only send one NotifyTouch event per render
462 // cycle to reduce overhead and perceived input latency.
463 static int prevx = 0, prevy = 0; // these track where the last drag notice was so that we don't send duplicate drag notices
464 if (touch_status && (x != prevx || y != prevy)) {
465 prevx = x;
466 prevy = y;
467 if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
468 state = AS_NO_ACTION;
469 else
470 state = AS_IN_ACTION_AREA;
471 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400472}
473
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600474static void setup_ors_command()
Ethan Yonker03a42f62014-08-08 11:03:51 -0500475{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600476 ors_read_fd = -1;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500477
478 unlink(ORS_INPUT_FILE);
479 if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
480 LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600481 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500482 }
483 unlink(ORS_OUTPUT_FILE);
484 if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
485 LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
486 unlink(ORS_INPUT_FILE);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600487 return;
Ethan Yonker03a42f62014-08-08 11:03:51 -0500488 }
489
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600490 ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK);
491 if (ors_read_fd < 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500492 LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
493 unlink(ORS_INPUT_FILE);
494 unlink(ORS_OUTPUT_FILE);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500495 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600496}
Ethan Yonker03a42f62014-08-08 11:03:51 -0500497
that10ae24f2015-12-26 20:53:51 +0100498// callback called after a CLI command was executed
499static void ors_command_done()
500{
501 gui_set_FILE(NULL);
502 fclose(orsout);
503 orsout = NULL;
504
505 if (DataManager::GetIntValue("tw_page_done") == 0) {
506 // The select function will return ready to read and the
507 // read function will return errno 19 no such device unless
508 // we set everything up all over again.
509 close(ors_read_fd);
510 setup_ors_command();
511 }
512}
513
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600514static void ors_command_read()
515{
that10ae24f2015-12-26 20:53:51 +0100516 char command[1024];
517 int read_ret = read(ors_read_fd, &command, sizeof(command));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500518
that10ae24f2015-12-26 20:53:51 +0100519 if (read_ret > 0) {
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600520 command[1022] = '\n';
521 command[1023] = '\0';
522 LOGINFO("Command '%s' received\n", command);
523 orsout = fopen(ORS_OUTPUT_FILE, "w");
524 if (!orsout) {
525 close(ors_read_fd);
526 ors_read_fd = -1;
527 LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
528 unlink(ORS_INPUT_FILE);
529 unlink(ORS_OUTPUT_FILE);
530 return;
531 }
532 if (DataManager::GetIntValue("tw_busy") != 0) {
that10ae24f2015-12-26 20:53:51 +0100533 fputs("Failed, operation in progress\n", orsout);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600534 LOGINFO("Command cannot be performed, operation in progress.\n");
that10ae24f2015-12-26 20:53:51 +0100535 fclose(orsout);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600536 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500537 if (strlen(command) == 11 && strncmp(command, "dumpstrings", 11) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500538 gui_set_FILE(orsout);
539 PageManager::GetResources()->DumpStrings();
that10ae24f2015-12-26 20:53:51 +0100540 ors_command_done();
541 } else {
542 // mirror output messages
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600543 gui_set_FILE(orsout);
that10ae24f2015-12-26 20:53:51 +0100544 // close orsout and restart listener after command is done
545 OpenRecoveryScript::Call_After_CLI_Command(ors_command_done);
546 // run the command in a threaded action...
547 DataManager::SetValue("tw_action", "twcmd");
548 DataManager::SetValue("tw_action_param", command);
549 // ...and switch back to the current page when finished
550 std::string currentPage = PageManager::GetCurrentPage();
551 DataManager::SetValue("tw_has_action2", "1");
552 DataManager::SetValue("tw_action2", "page");
553 DataManager::SetValue("tw_action2_param", currentPage);
554 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
555 DataManager::SetValue("tw_action_text2", "");
556 gui_changePage("singleaction_page");
557 // now immediately return to the GUI main loop (the action runs in the background thread)
558 // 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 -0500559 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500560 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600561 } else {
562 LOGINFO("ORS command line read returned an error: %i, %i, %s\n", read_ret, errno, strerror(errno));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500563 }
Ethan Yonker03a42f62014-08-08 11:03:51 -0500564}
565
that10ae24f2015-12-26 20:53:51 +0100566// Get and dispatch input events until it's time to draw the next frame
Dees_Troy51a0e822012-09-05 15:24:24 -0400567// This special function will return immediately the first time, but then
568// always returns 1/30th of a second (or immediately if called later) from
569// the last time it was called
thatde72b6d2015-02-08 08:55:00 +0100570static void loopTimer(int input_timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400571{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200572 static timespec lastCall;
573 static int initialized = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400574
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200575 if (!initialized)
Dees_Troyc8b199c2012-09-24 11:55:07 -0400576 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200577 clock_gettime(CLOCK_MONOTONIC, &lastCall);
578 initialized = 1;
579 return;
Dees_Troyc8b199c2012-09-24 11:55:07 -0400580 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400581
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200582 do
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500583 {
thatde72b6d2015-02-08 08:55:00 +0100584 bool got_event = input_handler.processInput(input_timeout_ms); // get inputs but don't send drag notices
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200585 timespec curTime;
586 clock_gettime(CLOCK_MONOTONIC, &curTime);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 timespec diff = TWFunc::timespec_diff(lastCall, curTime);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500589
thatc5837f32015-02-01 01:59:43 +0100590 // This is really 2 or 30 times per second
591 // As long as we get events, increase the timeout so we can catch up with input
592 long timeout = got_event ? 500000000 : 33333333;
593
594 if (diff.tv_sec || diff.tv_nsec > timeout)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500595 {
thatc5837f32015-02-01 01:59:43 +0100596 // int32_t input_time = TWFunc::timespec_diff_ms(lastCall, curTime);
597 // LOGINFO("loopTimer(): %u ms, count: %u\n", input_time, count);
598
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 lastCall = curTime;
that9fa51532015-01-29 02:04:47 +0100600 input_handler.handleDrag(); // send only drag notices if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 return;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500602 }
603
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200604 // We need to sleep some period time microseconds
Ethan Yonkere13fa632015-01-27 11:30:03 -0600605 //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
606 //usleep(sleepTime); // removed so we can scan for input
thatde72b6d2015-02-08 08:55:00 +0100607 input_timeout_ms = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200608 } while (1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400609}
610
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600611static int runPages(const char *page_name, const int stop_on_page_done)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500612{
Ethan Yonker3f15be42015-02-09 09:39:47 -0600613 DataManager::SetValue("tw_page_done", 0);
614 DataManager::SetValue("tw_gui_done", 0);
615
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600616 if (page_name)
617 gui_changePage(page_name);
618
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200619 // Raise the curtain
620 if (gCurtain != NULL)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500621 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200622 gr_surface surface;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500623
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200624 PageManager::Render();
625 gr_get_surface(&surface);
626 curtainRaise(surface);
627 gr_free_surface(surface);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500628 }
629
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200630 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500631
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200632 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500633
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600634#ifndef TW_OEM_BUILD
635 struct timeval timeout;
636 fd_set fdset;
637 int has_data = 0;
638#endif
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100639
thatde72b6d2015-02-08 08:55:00 +0100640 int input_timeout_ms = 0;
641 int idle_frames = 0;
642
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200643 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500644 {
thatde72b6d2015-02-08 08:55:00 +0100645 loopTimer(input_timeout_ms);
that1964d192016-01-07 00:41:03 +0100646 if (g_pty_fd > 0) {
647 // TODO: this is not nice, we should have one central select for input, pty, and ors
648 FD_ZERO(&fdset);
649 FD_SET(g_pty_fd, &fdset);
650 timeout.tv_sec = 0;
651 timeout.tv_usec = 1;
652 has_data = select(g_pty_fd+1, &fdset, NULL, NULL, &timeout);
653 if (has_data > 0) {
654 terminal_pty_read();
655 }
656 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600657#ifndef TW_OEM_BUILD
that10ae24f2015-12-26 20:53:51 +0100658 if (ors_read_fd > 0 && !orsout) { // orsout is non-NULL if a command is still running
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600659 FD_ZERO(&fdset);
660 FD_SET(ors_read_fd, &fdset);
661 timeout.tv_sec = 0;
662 timeout.tv_usec = 1;
663 has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout);
664 if (has_data > 0) {
665 ors_command_read();
666 }
667 }
668#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500669
Ethan Yonker04536952015-01-27 08:41:28 -0600670 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500671 {
thatde72b6d2015-02-08 08:55:00 +0100672 int ret = PageManager::Update();
673 if (ret == 0)
674 ++idle_frames;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500675 else if (ret == -2)
676 break; // Theme reload failure
thatde72b6d2015-02-08 08:55:00 +0100677 else
678 idle_frames = 0;
679 // due to possible animation objects, we need to delay activating the input timeout
680 input_timeout_ms = idle_frames > 15 ? 1000 : 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100681
682#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200683 if (ret > 1)
684 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500685
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200686 if (ret > 0)
687 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100688#else
689 if (ret > 1)
690 {
that9fa51532015-01-29 02:04:47 +0100691 timespec start, end;
692 int32_t render_t, flip_t;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100693 clock_gettime(CLOCK_MONOTONIC, &start);
694 PageManager::Render();
695 clock_gettime(CLOCK_MONOTONIC, &end);
696 render_t = TWFunc::timespec_diff_ms(start, end);
697
698 flip();
699 clock_gettime(CLOCK_MONOTONIC, &start);
700 flip_t = TWFunc::timespec_diff_ms(end, start);
701
702 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
703 }
thatde72b6d2015-02-08 08:55:00 +0100704 else if (ret > 0)
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100705 flip();
706#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500707 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200708 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500709 {
Ethan Yonker04536952015-01-27 08:41:28 -0600710 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200711 PageManager::Render();
712 flip();
thatde72b6d2015-02-08 08:55:00 +0100713 input_timeout_ms = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500714 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200715
thatfb759d42015-01-11 12:16:53 +0100716 blankTimer.checkForTimeout();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600717 if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200718 {
719 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600720 break;
721 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600722 if (DataManager::GetIntValue("tw_gui_done") != 0)
723 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500724 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600725 if (ors_read_fd > 0)
726 close(ors_read_fd);
727 ors_read_fd = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200728 gGuiRunning = 0;
729 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500730}
731
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200732int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500733{
Ethan Yonker04536952015-01-27 08:41:28 -0600734 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200735 return 0;
736}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500737
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200738int gui_changePage(std::string newPage)
739{
740 LOGINFO("Set page: '%s'\n", newPage.c_str());
741 PageManager::ChangePage(newPage);
Ethan Yonker04536952015-01-27 08:41:28 -0600742 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200743 return 0;
744}
745
746int gui_changeOverlay(std::string overlay)
747{
Ethan Yonker1c273312015-03-16 12:18:56 -0500748 LOGINFO("Set overlay: '%s'\n", overlay.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200749 PageManager::ChangeOverlay(overlay);
Ethan Yonker04536952015-01-27 08:41:28 -0600750 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200751 return 0;
752}
753
thatb2e8f672015-03-05 20:25:39 +0100754std::string gui_parse_text(std::string str)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200755{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200756 // This function parses text for DataManager values encompassed by %value% in the XML
thatb2e8f672015-03-05 20:25:39 +0100757 // and string resources (%@resource_name%)
Ethan Yonker74db1572015-10-28 12:44:49 -0500758 size_t pos = 0, next, end;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200759
760 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500761 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500762 next = str.find("{@", pos);
763 if (next == std::string::npos)
764 break;
765
766 end = str.find('}', next + 1);
767 if (end == std::string::npos)
768 break;
769
770 std::string var = str.substr(next + 2, (end - next) - 2);
771 str.erase(next, (end - next) + 1);
772
773 size_t default_loc = var.find('=', 0);
774 std::string lookup;
775 if (default_loc == std::string::npos) {
776 str.insert(next, PageManager::GetResources()->FindString(var));
777 } else {
778 lookup = var.substr(0, default_loc);
779 std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1);
780 str.insert(next, PageManager::GetResources()->FindString(lookup, default_string));
781 }
782 }
783 pos = 0;
784 while (1)
785 {
786 next = str.find('%', pos);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200787 if (next == std::string::npos)
788 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500789
Ethan Yonker74db1572015-10-28 12:44:49 -0500790 end = str.find('%', next + 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200791 if (end == std::string::npos)
792 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500793
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200794 // We have a block of data
thatb2e8f672015-03-05 20:25:39 +0100795 std::string var = str.substr(next + 1, (end - next) - 1);
796 str.erase(next, (end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500797
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200798 if (next + 1 == end)
799 str.insert(next, 1, '%');
800 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500801 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200802 std::string value;
thatb2e8f672015-03-05 20:25:39 +0100803 if (var.size() > 0 && var[0] == '@') {
804 // this is a string resource ("%@string_name%")
805 value = PageManager::GetResources()->FindString(var.substr(1));
806 str.insert(next, value);
807 }
808 else if (DataManager::GetValue(var, value) == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200809 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500810 }
811
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200812 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500813 }
814}
815
Ethan Yonker74db1572015-10-28 12:44:49 -0500816std::string gui_lookup(const std::string& resource_name, const std::string& default_value) {
817 return PageManager::GetResources()->FindString(resource_name, default_value);
818}
819
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200820extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500821{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200822 gr_init();
Dees Troy3454ade2015-01-20 19:21:04 +0000823 std::string curtain_path = TWRES "images/curtain.jpg";
Ethan Yonker63e414f2015-02-06 15:44:39 -0600824 gr_surface source_Surface = NULL;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500825
Ethan Yonker63e414f2015-02-06 15:44:39 -0600826 if (res_create_surface(curtain_path.c_str(), &source_Surface))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500827 {
Ethan Yonker591b9202015-03-11 11:17:15 -0500828 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 +0200829 return -1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500830 }
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500831 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 -0600832 // We need to scale the curtain to fit the screen
833 float scale_w = (float)gr_fb_width() / (float)gr_get_width(source_Surface);
834 float scale_h = (float)gr_fb_height() / (float)gr_get_height(source_Surface);
835 if (res_scale_surface(source_Surface, &gCurtain, scale_w, scale_h)) {
836 LOGINFO("Failed to scale curtain\n");
837 gCurtain = source_Surface;
838 } else {
839 LOGINFO("Scaling the curtain width %fx and height %fx\n", scale_w, scale_h);
840 }
841 } else {
842 gCurtain = source_Surface;
843 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500844
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200845 curtainSet();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500846
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200847 ev_init();
848 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500849}
850
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200851extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400852{
Ethan Yonker83e82572014-04-04 10:59:28 -0500853#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200854 int check = 0;
855 DataManager::GetValue(TW_IS_ENCRYPTED, check);
Dees Troy3454ade2015-01-20 19:21:04 +0000856
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200857 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500858 {
Dees Troy3454ade2015-01-20 19:21:04 +0000859 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400860 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500861 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200862 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400863 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200864 else
865 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500866 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400867
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200868 if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main"))
869 {
870 std::string theme_path;
871
872 theme_path = DataManager::GetSettingsStoragePath();
873 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400874 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200875 int retry_count = 5;
876 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400877 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200878 usleep(500000);
879 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500880 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200881
Ethan Yonker74db1572015-10-28 12:44:49 -0500882 if (!PartitionManager.Mount_Settings_Storage(true))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500883 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500884 LOGINFO("Unable to mount %s during GUI startup.\n", theme_path.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200885 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500886 }
887 }
888
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200889 theme_path += "/TWRP/theme/ui.zip";
890 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500891 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500892#endif // ifndef TW_OEM_BUILD
Dees Troy3454ade2015-01-20 19:21:04 +0000893 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500894 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500895 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200896 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400897 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500898#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400899 }
900 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500901#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200902 // Set the default package
903 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400904
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200905 gGuiInitialized = 1;
906 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400907
908error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500909 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200910 gGuiInitialized = 0;
911 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400912}
913
Ethan Yonkercf50da52015-01-12 21:59:07 -0600914extern "C" int gui_loadCustomResources(void)
915{
916#ifndef TW_OEM_BUILD
917 if (!PartitionManager.Mount_Settings_Storage(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500918 LOGINFO("Unable to mount settings storage during GUI startup.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600919 return -1;
920 }
921
922 std::string theme_path = DataManager::GetSettingsStoragePath();
923 theme_path += "/TWRP/theme/ui.zip";
924 // Check for a custom theme
925 if (TWFunc::Path_Exists(theme_path)) {
926 // There is a custom theme, try to load it
927 if (PageManager::ReloadPackage("TWRP", theme_path)) {
928 // Custom theme failed to load, try to load stock theme
Dees Troy3454ade2015-01-20 19:21:04 +0000929 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500930 gui_err("base_pkg_err=Failed to load base packages.");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600931 goto error;
932 }
933 }
934 }
935 // Set the default package
936 PageManager::SelectPackage("TWRP");
937#endif
938 return 0;
939
940error:
941 LOGERR("An internal error has occurred: unable to load theme.\n");
942 gGuiInitialized = 0;
943 return -1;
944}
945
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200946extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400947{
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600948 return gui_startPage(NULL, 1, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400949}
950
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600951extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000952{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200953 if (!gGuiInitialized)
954 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000955
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200956 // Set the default package
957 PageManager::SelectPackage("TWRP");
958
that9fa51532015-01-29 02:04:47 +0100959 input_handler.init();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600960#ifndef TW_OEM_BUILD
961 if (allow_commands)
962 {
963 if (ors_read_fd < 0)
964 setup_ors_command();
965 } else {
966 if (ors_read_fd >= 0) {
967 close(ors_read_fd);
968 ors_read_fd = -1;
969 }
970 }
971#endif
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600972 return runPages(page_name, stop_on_page_done);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000973}
974
Ethan Yonker63e414f2015-02-06 15:44:39 -0600975
976extern "C" void set_scale_values(float w, float h)
977{
978 scale_theme_w = w;
979 scale_theme_h = h;
980}
981
982extern "C" int scale_theme_x(int initial_x)
983{
984 if (scale_theme_w != 1) {
thatba75a0e2015-02-14 21:20:10 +0100985 int scaled = (float)initial_x * scale_theme_w;
986 if (scaled == 0 && initial_x > 0)
987 return 1;
988 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600989 }
990 return initial_x;
991}
992
993extern "C" int scale_theme_y(int initial_y)
994{
995 if (scale_theme_h != 1) {
thatba75a0e2015-02-14 21:20:10 +0100996 int scaled = (float)initial_y * scale_theme_h;
997 if (scaled == 0 && initial_y > 0)
998 return 1;
999 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -06001000 }
1001 return initial_y;
1002}
1003
1004extern "C" int scale_theme_min(int initial_value)
1005{
1006 if (scale_theme_w != 1 || scale_theme_h != 1) {
1007 if (scale_theme_w < scale_theme_h)
1008 return scale_theme_x(initial_value);
1009 else
1010 return scale_theme_y(initial_value);
1011 }
1012 return initial_value;
1013}
1014
1015extern "C" float get_scale_w()
1016{
1017 return scale_theme_w;
1018}
1019
1020extern "C" float get_scale_h()
1021{
1022 return scale_theme_h;
1023}