blob: d99764aea2bc5bbaeec6e59724c5ec9299a6fccf [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 Yonkerafde0982016-01-23 08:55:35 -0600616 if (page_name) {
617 PageManager::SetStartPage(page_name);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600618 gui_changePage(page_name);
Ethan Yonkerafde0982016-01-23 08:55:35 -0600619 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600620
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200621 // Raise the curtain
622 if (gCurtain != NULL)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500623 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200624 gr_surface surface;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500625
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200626 PageManager::Render();
627 gr_get_surface(&surface);
628 curtainRaise(surface);
629 gr_free_surface(surface);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500630 }
631
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200632 gGuiRunning = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500633
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200634 DataManager::SetValue("tw_loaded", 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500635
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600636#ifndef TW_OEM_BUILD
637 struct timeval timeout;
638 fd_set fdset;
639 int has_data = 0;
640#endif
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100641
thatde72b6d2015-02-08 08:55:00 +0100642 int input_timeout_ms = 0;
643 int idle_frames = 0;
644
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200645 for (;;)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500646 {
thatde72b6d2015-02-08 08:55:00 +0100647 loopTimer(input_timeout_ms);
that1964d192016-01-07 00:41:03 +0100648 if (g_pty_fd > 0) {
649 // TODO: this is not nice, we should have one central select for input, pty, and ors
650 FD_ZERO(&fdset);
651 FD_SET(g_pty_fd, &fdset);
652 timeout.tv_sec = 0;
653 timeout.tv_usec = 1;
654 has_data = select(g_pty_fd+1, &fdset, NULL, NULL, &timeout);
655 if (has_data > 0) {
656 terminal_pty_read();
657 }
658 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600659#ifndef TW_OEM_BUILD
that10ae24f2015-12-26 20:53:51 +0100660 if (ors_read_fd > 0 && !orsout) { // orsout is non-NULL if a command is still running
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600661 FD_ZERO(&fdset);
662 FD_SET(ors_read_fd, &fdset);
663 timeout.tv_sec = 0;
664 timeout.tv_usec = 1;
665 has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout);
666 if (has_data > 0) {
667 ors_command_read();
668 }
669 }
670#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500671
Ethan Yonker04536952015-01-27 08:41:28 -0600672 if (!gForceRender.get_value())
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500673 {
thatde72b6d2015-02-08 08:55:00 +0100674 int ret = PageManager::Update();
675 if (ret == 0)
676 ++idle_frames;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500677 else if (ret == -2)
678 break; // Theme reload failure
thatde72b6d2015-02-08 08:55:00 +0100679 else
680 idle_frames = 0;
681 // due to possible animation objects, we need to delay activating the input timeout
682 input_timeout_ms = idle_frames > 15 ? 1000 : 0;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100683
684#ifndef PRINT_RENDER_TIME
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200685 if (ret > 1)
686 PageManager::Render();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500687
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200688 if (ret > 0)
689 flip();
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100690#else
691 if (ret > 1)
692 {
that9fa51532015-01-29 02:04:47 +0100693 timespec start, end;
694 int32_t render_t, flip_t;
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100695 clock_gettime(CLOCK_MONOTONIC, &start);
696 PageManager::Render();
697 clock_gettime(CLOCK_MONOTONIC, &end);
698 render_t = TWFunc::timespec_diff_ms(start, end);
699
700 flip();
701 clock_gettime(CLOCK_MONOTONIC, &start);
702 flip_t = TWFunc::timespec_diff_ms(end, start);
703
704 LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
705 }
thatde72b6d2015-02-08 08:55:00 +0100706 else if (ret > 0)
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100707 flip();
708#endif
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500709 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200710 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500711 {
Ethan Yonker04536952015-01-27 08:41:28 -0600712 gForceRender.set_value(0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200713 PageManager::Render();
714 flip();
thatde72b6d2015-02-08 08:55:00 +0100715 input_timeout_ms = 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500716 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200717
thatfb759d42015-01-11 12:16:53 +0100718 blankTimer.checkForTimeout();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600719 if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200720 {
721 gui_changePage("main");
Dees_Troy6ef66352013-02-21 08:26:57 -0600722 break;
723 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600724 if (DataManager::GetIntValue("tw_gui_done") != 0)
725 break;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500726 }
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600727 if (ors_read_fd > 0)
728 close(ors_read_fd);
729 ors_read_fd = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200730 gGuiRunning = 0;
731 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500732}
733
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734int gui_forceRender(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500735{
Ethan Yonker04536952015-01-27 08:41:28 -0600736 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200737 return 0;
738}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500739
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200740int gui_changePage(std::string newPage)
741{
742 LOGINFO("Set page: '%s'\n", newPage.c_str());
743 PageManager::ChangePage(newPage);
Ethan Yonker04536952015-01-27 08:41:28 -0600744 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200745 return 0;
746}
747
748int gui_changeOverlay(std::string overlay)
749{
Ethan Yonker1c273312015-03-16 12:18:56 -0500750 LOGINFO("Set overlay: '%s'\n", overlay.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200751 PageManager::ChangeOverlay(overlay);
Ethan Yonker04536952015-01-27 08:41:28 -0600752 gForceRender.set_value(1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200753 return 0;
754}
755
thatb2e8f672015-03-05 20:25:39 +0100756std::string gui_parse_text(std::string str)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200757{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200758 // This function parses text for DataManager values encompassed by %value% in the XML
thatb2e8f672015-03-05 20:25:39 +0100759 // and string resources (%@resource_name%)
Ethan Yonker74db1572015-10-28 12:44:49 -0500760 size_t pos = 0, next, end;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200761
762 while (1)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500763 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500764 next = str.find("{@", pos);
765 if (next == std::string::npos)
766 break;
767
768 end = str.find('}', next + 1);
769 if (end == std::string::npos)
770 break;
771
772 std::string var = str.substr(next + 2, (end - next) - 2);
773 str.erase(next, (end - next) + 1);
774
775 size_t default_loc = var.find('=', 0);
776 std::string lookup;
777 if (default_loc == std::string::npos) {
778 str.insert(next, PageManager::GetResources()->FindString(var));
779 } else {
780 lookup = var.substr(0, default_loc);
781 std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1);
782 str.insert(next, PageManager::GetResources()->FindString(lookup, default_string));
783 }
784 }
785 pos = 0;
786 while (1)
787 {
788 next = str.find('%', pos);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200789 if (next == std::string::npos)
790 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500791
Ethan Yonker74db1572015-10-28 12:44:49 -0500792 end = str.find('%', next + 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200793 if (end == std::string::npos)
794 return str;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500795
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200796 // We have a block of data
thatb2e8f672015-03-05 20:25:39 +0100797 std::string var = str.substr(next + 1, (end - next) - 1);
798 str.erase(next, (end - next) + 1);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500799
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200800 if (next + 1 == end)
801 str.insert(next, 1, '%');
802 else
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500803 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200804 std::string value;
thatb2e8f672015-03-05 20:25:39 +0100805 if (var.size() > 0 && var[0] == '@') {
806 // this is a string resource ("%@string_name%")
807 value = PageManager::GetResources()->FindString(var.substr(1));
808 str.insert(next, value);
809 }
810 else if (DataManager::GetValue(var, value) == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200811 str.insert(next, value);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500812 }
813
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200814 pos = next + 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500815 }
816}
817
Ethan Yonker74db1572015-10-28 12:44:49 -0500818std::string gui_lookup(const std::string& resource_name, const std::string& default_value) {
819 return PageManager::GetResources()->FindString(resource_name, default_value);
820}
821
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200822extern "C" int gui_init(void)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500823{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 gr_init();
Dees Troy3454ade2015-01-20 19:21:04 +0000825 std::string curtain_path = TWRES "images/curtain.jpg";
Ethan Yonker63e414f2015-02-06 15:44:39 -0600826 gr_surface source_Surface = NULL;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500827
Ethan Yonker63e414f2015-02-06 15:44:39 -0600828 if (res_create_surface(curtain_path.c_str(), &source_Surface))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500829 {
Ethan Yonker591b9202015-03-11 11:17:15 -0500830 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 +0200831 return -1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500832 }
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500833 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 -0600834 // We need to scale the curtain to fit the screen
835 float scale_w = (float)gr_fb_width() / (float)gr_get_width(source_Surface);
836 float scale_h = (float)gr_fb_height() / (float)gr_get_height(source_Surface);
837 if (res_scale_surface(source_Surface, &gCurtain, scale_w, scale_h)) {
838 LOGINFO("Failed to scale curtain\n");
839 gCurtain = source_Surface;
840 } else {
841 LOGINFO("Scaling the curtain width %fx and height %fx\n", scale_w, scale_h);
842 }
843 } else {
844 gCurtain = source_Surface;
845 }
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500846
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200847 curtainSet();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500848
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200849 ev_init();
850 return 0;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500851}
852
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200853extern "C" int gui_loadResources(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400854{
Ethan Yonker83e82572014-04-04 10:59:28 -0500855#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200856 int check = 0;
857 DataManager::GetValue(TW_IS_ENCRYPTED, check);
Dees Troy3454ade2015-01-20 19:21:04 +0000858
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200859 if (check)
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500860 {
Dees Troy3454ade2015-01-20 19:21:04 +0000861 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500863 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200864 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400865 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200866 else
867 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500868 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400869
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200870 if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main"))
871 {
872 std::string theme_path;
873
874 theme_path = DataManager::GetSettingsStoragePath();
875 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400876 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200877 int retry_count = 5;
878 while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400879 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200880 usleep(500000);
881 retry_count--;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500882 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200883
Ethan Yonker74db1572015-10-28 12:44:49 -0500884 if (!PartitionManager.Mount_Settings_Storage(true))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500885 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500886 LOGINFO("Unable to mount %s during GUI startup.\n", theme_path.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200887 check = 1;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500888 }
889 }
890
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200891 theme_path += "/TWRP/theme/ui.zip";
892 if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500893 {
Ethan Yonker83e82572014-04-04 10:59:28 -0500894#endif // ifndef TW_OEM_BUILD
Dees Troy3454ade2015-01-20 19:21:04 +0000895 if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500896 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500897 gui_err("base_pkg_err=Failed to load base packages.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200898 goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400899 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500900#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -0400901 }
902 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500903#endif // ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200904 // Set the default package
905 PageManager::SelectPackage("TWRP");
Dees_Troy51a0e822012-09-05 15:24:24 -0400906
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200907 gGuiInitialized = 1;
908 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400909
910error:
Ethan Yonker83e82572014-04-04 10:59:28 -0500911 LOGERR("An internal error has occurred: unable to load theme.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200912 gGuiInitialized = 0;
913 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400914}
915
Ethan Yonkercf50da52015-01-12 21:59:07 -0600916extern "C" int gui_loadCustomResources(void)
917{
918#ifndef TW_OEM_BUILD
919 if (!PartitionManager.Mount_Settings_Storage(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500920 LOGINFO("Unable to mount settings storage during GUI startup.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600921 return -1;
922 }
923
924 std::string theme_path = DataManager::GetSettingsStoragePath();
925 theme_path += "/TWRP/theme/ui.zip";
926 // Check for a custom theme
927 if (TWFunc::Path_Exists(theme_path)) {
928 // There is a custom theme, try to load it
929 if (PageManager::ReloadPackage("TWRP", theme_path)) {
930 // Custom theme failed to load, try to load stock theme
Dees Troy3454ade2015-01-20 19:21:04 +0000931 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500932 gui_err("base_pkg_err=Failed to load base packages.");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600933 goto error;
934 }
935 }
936 }
937 // Set the default package
938 PageManager::SelectPackage("TWRP");
939#endif
940 return 0;
941
942error:
943 LOGERR("An internal error has occurred: unable to load theme.\n");
944 gGuiInitialized = 0;
945 return -1;
946}
947
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200948extern "C" int gui_start(void)
Dees_Troy51a0e822012-09-05 15:24:24 -0400949{
Ethan Yonkerafde0982016-01-23 08:55:35 -0600950 return gui_startPage("main", 1, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400951}
952
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600953extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done)
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000954{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200955 if (!gGuiInitialized)
956 return -1;
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000957
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200958 // Set the default package
959 PageManager::SelectPackage("TWRP");
960
that9fa51532015-01-29 02:04:47 +0100961 input_handler.init();
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600962#ifndef TW_OEM_BUILD
963 if (allow_commands)
964 {
965 if (ors_read_fd < 0)
966 setup_ors_command();
967 } else {
968 if (ors_read_fd >= 0) {
969 close(ors_read_fd);
970 ors_read_fd = -1;
971 }
972 }
973#endif
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600974 return runPages(page_name, stop_on_page_done);
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000975}
976
Ethan Yonker63e414f2015-02-06 15:44:39 -0600977
978extern "C" void set_scale_values(float w, float h)
979{
980 scale_theme_w = w;
981 scale_theme_h = h;
982}
983
984extern "C" int scale_theme_x(int initial_x)
985{
986 if (scale_theme_w != 1) {
thatba75a0e2015-02-14 21:20:10 +0100987 int scaled = (float)initial_x * scale_theme_w;
988 if (scaled == 0 && initial_x > 0)
989 return 1;
990 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600991 }
992 return initial_x;
993}
994
995extern "C" int scale_theme_y(int initial_y)
996{
997 if (scale_theme_h != 1) {
thatba75a0e2015-02-14 21:20:10 +0100998 int scaled = (float)initial_y * scale_theme_h;
999 if (scaled == 0 && initial_y > 0)
1000 return 1;
1001 return scaled;
Ethan Yonker63e414f2015-02-06 15:44:39 -06001002 }
1003 return initial_y;
1004}
1005
1006extern "C" int scale_theme_min(int initial_value)
1007{
1008 if (scale_theme_w != 1 || scale_theme_h != 1) {
1009 if (scale_theme_w < scale_theme_h)
1010 return scale_theme_x(initial_value);
1011 else
1012 return scale_theme_y(initial_value);
1013 }
1014 return initial_value;
1015}
1016
1017extern "C" float get_scale_w()
1018{
1019 return scale_theme_w;
1020}
1021
1022extern "C" float get_scale_h()
1023{
1024 return scale_theme_h;
1025}