1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <linux/input.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
extern "C" {
#include "../common.h"
#include "../roots.h"
#include "../minuitwrp/minui.h"
#include "../recovery_ui.h"
#include "../minzip/Zip.h"
#include <pixelflinger/pixelflinger.h>
}
#include "rapidxml.hpp"
#include "objects.hpp"
#include "../data.hpp"
#include "../variables.h"
#include "../partitions.hpp"
const static int CURTAIN_FADE = 32;
using namespace rapidxml;
// Global values
static gr_surface gCurtain = NULL;
static int gGuiInitialized = 0;
static int gGuiConsoleRunning = 0;
static int gGuiConsoleTerminate = 0;
static int gForceRender = 0;
static int gNoAnimation = 1;
static int gGuiInputRunning = 0;
// Needed by pages.cpp too
int gGuiRunning = 0;
static int gRecorder = -1;
extern "C" void gr_write_frame_to_file(int fd);
void flip(void)
{
if (gRecorder != -1)
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
write(gRecorder, &time, sizeof(timespec));
gr_write_frame_to_file(gRecorder);
}
gr_flip();
return;
}
void rapidxml::parse_error_handler(const char *what, void *where)
{
fprintf(stderr, "Parser error: %s\n", what);
fprintf(stderr, " Start of string: %s\n", (char*) where);
abort();
}
static void curtainSet()
{
gr_color(0, 0, 0, 255);
gr_fill(0, 0, gr_fb_width(), gr_fb_height());
gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
gr_flip();
return;
}
static void curtainRaise(gr_surface surface)
{
int sy = 0;
int h = gr_get_height(gCurtain) - 1;
int w = gr_get_width(gCurtain);
int fy = 1;
int msw = gr_get_width(surface);
int msh = gr_get_height(surface);
int CURTAIN_RATE = msh / 30;
if (gNoAnimation == 0)
{
for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE)
{
gr_blit(surface, 0, 0, msw, msh, 0, 0);
gr_blit(gCurtain, 0, sy, w, h, 0, 0);
gr_flip();
}
}
gr_blit(surface, 0, 0, msw, msh, 0, 0);
flip();
return;
}
void curtainClose()
{
#if 0
int w = gr_get_width(gCurtain);
int h = 1;
int sy = gr_get_height(gCurtain) - 1;
int fbh = gr_fb_height();
int CURTAIN_RATE = fbh / 30;
if (gNoAnimation == 0)
{
for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE)
{
gr_blit(gCurtain, 0, sy, w, h, 0, 0);
gr_flip();
}
gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
gr_flip();
if (gRecorder != -1)
close(gRecorder);
int fade;
for (fade = 16; fade < 255; fade += CURTAIN_FADE)
{
gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
gr_color(0, 0, 0, fade);
gr_fill(0, 0, gr_fb_width(), gr_fb_height());
gr_flip();
}
gr_color(0, 0, 0, 255);
gr_fill(0, 0, gr_fb_width(), gr_fb_height());
gr_flip();
}
#else
gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0);
gr_flip();
#endif
return;
}
timespec timespec_diff(timespec& start, timespec& end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
static void *input_thread(void *cookie)
{
int drag = 0;
static int touch_and_hold = 0, dontwait = 0, touch_repeat = 0, x = 0, y = 0, lshift = 0, rshift = 0, key_repeat = 0;
static struct timeval touchStart;
HardwareKeyboard kb;
for (;;) {
// wait for the next event
struct input_event ev;
int state = 0, ret = 0;
ret = ev_get(&ev, dontwait);
if (ret < 0) {
struct timeval curTime;
gettimeofday(&curTime, NULL);
long mtime, seconds, useconds;
seconds = curTime.tv_sec - touchStart.tv_sec;
useconds = curTime.tv_usec - touchStart.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
if (touch_and_hold && mtime > 500) {
touch_and_hold = 0;
touch_repeat = 1;
gettimeofday(&touchStart, NULL);
#ifdef _EVENT_LOGGING
LOGE("TOUCH_HOLD: %d,%d\n", x, y);
#endif
PageManager::NotifyTouch(TOUCH_HOLD, x, y);
} else if (touch_repeat && mtime > 100) {
#ifdef _EVENT_LOGGING
LOGE("TOUCH_REPEAT: %d,%d\n", x, y);
#endif
gettimeofday(&touchStart, NULL);
PageManager::NotifyTouch(TOUCH_REPEAT, x, y);
} else if (key_repeat == 1 && mtime > 500) {
#ifdef _EVENT_LOGGING
LOGE("KEY_HOLD: %d,%d\n", x, y);
#endif
gettimeofday(&touchStart, NULL);
key_repeat = 2;
kb.KeyRepeat();
} else if (key_repeat == 2 && mtime > 100) {
#ifdef _EVENT_LOGGING
LOGE("KEY_REPEAT: %d,%d\n", x, y);
#endif
gettimeofday(&touchStart, NULL);
kb.KeyRepeat();
}
} else if (ev.type == EV_ABS) {
x = ev.value >> 16;
y = ev.value & 0xFFFF;
if (ev.code == 0)
{
if (state == 0)
{
#ifdef _EVENT_LOGGING
LOGE("TOUCH_RELEASE: %d,%d\n", x, y);
#endif
PageManager::NotifyTouch(TOUCH_RELEASE, x, y);
touch_and_hold = 0;
touch_repeat = 0;
if (!key_repeat)
dontwait = 0;
}
state = 0;
drag = 0;
}
else
{
if (!drag)
{
#ifdef _EVENT_LOGGING
LOGE("TOUCH_START: %d,%d\n", x, y);
#endif
if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0)
state = 1;
drag = 1;
touch_and_hold = 1;
dontwait = 1;
key_repeat = 0;
gettimeofday(&touchStart, NULL);
}
else
{
if (state == 0)
{
#ifdef _EVENT_LOGGING
LOGE("TOUCH_DRAG: %d,%d\n", x, y);
#endif
if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0)
state = 1;
key_repeat = 0;
}
}
}
}
else if (ev.type == EV_KEY)
{
// Handle key-press here
#ifdef _EVENT_LOGGING
LOGE("TOUCH_KEY: %d\n", ev.code);
#endif
if (ev.value != 0) {
// This is a key press
if (kb.KeyDown(ev.code)) {
key_repeat = 1;
touch_and_hold = 0;
touch_repeat = 0;
dontwait = 1;
gettimeofday(&touchStart, NULL);
} else {
key_repeat = 0;
touch_and_hold = 0;
touch_repeat = 0;
dontwait = 0;
}
} else {
// This is a key release
kb.KeyUp(ev.code);
key_repeat = 0;
touch_and_hold = 0;
touch_repeat = 0;
dontwait = 0;
}
}
}
return NULL;
}
// This special function will return immediately the first time, but then
// always returns 1/30th of a second (or immediately if called later) from
// the last time it was called
static void loopTimer(void)
{
static timespec lastCall;
static int initialized = 0;
if (!initialized)
{
clock_gettime(CLOCK_MONOTONIC, &lastCall);
initialized = 1;
return;
}
do
{
timespec curTime;
clock_gettime(CLOCK_MONOTONIC, &curTime);
timespec diff = timespec_diff(lastCall, curTime);
// This is really 30 times per second
if (diff.tv_sec || diff.tv_nsec > 33333333)
{
lastCall = curTime;
return;
}
// We need to sleep some period time microseconds
unsigned int sleepTime = 33333 - (diff.tv_nsec / 1000);
usleep(sleepTime);
} while(1);
return;
}
static int runPages(void)
{
// Raise the curtain
if (gCurtain != NULL)
{
gr_surface surface;
PageManager::Render();
gr_get_surface(&surface);
curtainRaise(surface);
gr_free_surface(surface);
}
gGuiRunning = 1;
DataManager::SetValue("tw_loaded", 1);
for (;;)
{
loopTimer();
if (!gForceRender)
{
int ret;
ret = PageManager::Update();
if (ret > 1)
PageManager::Render();
if (ret > 0)
flip();
}
else
{
gForceRender = 0;
PageManager::Render();
flip();
}
}
gGuiRunning = 0;
return 0;
}
static int runPage(const char* page_name)
{
gui_changePage(page_name);
// Raise the curtain
if (gCurtain != NULL)
{
gr_surface surface;
PageManager::Render();
gr_get_surface(&surface);
curtainRaise(surface);
gr_free_surface(surface);
}
gGuiRunning = 1;
DataManager::SetValue("tw_loaded", 1);
for (;;)
{
loopTimer();
if (!gForceRender)
{
int ret;
ret = PageManager::Update();
if (ret > 1)
PageManager::Render();
if (ret > 0)
flip();
}
else
{
gForceRender = 0;
PageManager::Render();
flip();
}
if (DataManager::GetIntValue("tw_page_done") != 0) {
gui_changePage("main");
break;
}
}
gGuiRunning = 0;
return 0;
}
int gui_forceRender(void)
{
gForceRender = 1;
return 0;
}
int gui_changePage(std::string newPage)
{
LOGI("Set page: '%s'\n", newPage.c_str());
PageManager::ChangePage(newPage);
gForceRender = 1;
return 0;
}
int gui_changeOverlay(std::string overlay)
{
PageManager::ChangeOverlay(overlay);
gForceRender = 1;
return 0;
}
int gui_changePackage(std::string newPackage)
{
PageManager::SelectPackage(newPackage);
gForceRender = 1;
return 0;
}
std::string gui_parse_text(string inText)
{
// Copied from std::string GUIText::parseText(void)
// This function parses text for DataManager values encompassed by %value% in the XML
static int counter = 0;
std::string str = inText;
size_t pos = 0;
size_t next = 0, end = 0;
while (1)
{
next = str.find('%', pos);
if (next == std::string::npos) return str;
end = str.find('%', next + 1);
if (end == std::string::npos) return str;
// We have a block of data
std::string var = str.substr(next + 1, (end - next) - 1);
str.erase(next, (end - next) + 1);
if (next + 1 == end)
{
str.insert(next, 1, '%');
}
else
{
std::string value;
if (DataManager::GetValue(var, value) == 0)
str.insert(next, value);
}
pos = next + 1;
}
}
extern "C" int gui_init()
{
int fd;
gr_init();
if (res_create_surface("/res/images/curtain.jpg", &gCurtain))
{
printf("Unable to locate '/res/images/curtain.jpg'\nDid you set a DEVICE_RESOLUTION in your config files?\n");
return -1;
}
curtainSet();
ev_init();
return 0;
}
extern "C" int gui_loadResources()
{
// unlink("/sdcard/video.last");
// rename("/sdcard/video.bin", "/sdcard/video.last");
// gRecorder = open("/sdcard/video.bin", O_CREAT | O_WRONLY);
int check = 0;
DataManager::GetValue(TW_IS_ENCRYPTED, check);
if (check) {
if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "decrypt"))
{
LOGE("Failed to load base packages.\n");
goto error;
} else
check = 1;
}
if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main")) {
std::string theme_path;
theme_path = DataManager::GetSettingsStoragePath();
if (!PartitionManager.Mount_Settings_Storage(false)) {
int retry_count = 5;
while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false)) {
usleep(500000);
retry_count--;
}
if (!PartitionManager.Mount_Settings_Storage(false)) {
LOGE("Unable to mount %s during GUI startup.\n", theme_path.c_str());
check = 1;
}
}
theme_path += "/TWRP/theme/ui.zip";
if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
{
if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "main"))
{
LOGE("Failed to load base packages.\n");
goto error;
}
}
}
// Set the default package
PageManager::SelectPackage("TWRP");
gGuiInitialized = 1;
return 0;
error:
LOGE("An internal error has occurred.\n");
gGuiInitialized = 0;
return -1;
}
extern "C" int gui_start()
{
if (!gGuiInitialized) return -1;
gGuiConsoleTerminate = 1;
while (gGuiConsoleRunning) loopTimer();
// Set the default package
PageManager::SelectPackage("TWRP");
if (!gGuiInputRunning) {
// Start by spinning off an input handler.
pthread_t t;
pthread_create(&t, NULL, input_thread, NULL);
gGuiInputRunning = 1;
}
return runPages();
}
extern "C" int gui_startPage(const char* page_name)
{
if (!gGuiInitialized) return -1;
gGuiConsoleTerminate = 1;
while (gGuiConsoleRunning) loopTimer();
// Set the default package
PageManager::SelectPackage("TWRP");
if (!gGuiInputRunning) {
// Start by spinning off an input handler.
pthread_t t;
pthread_create(&t, NULL, input_thread, NULL);
gGuiInputRunning = 1;
}
return runPage(page_name);
}
static void *console_thread(void *cookie)
{
PageManager::SwitchToConsole();
while (!gGuiConsoleTerminate)
{
loopTimer();
if (!gForceRender)
{
int ret;
ret = PageManager::Update();
if (ret > 1)
PageManager::Render();
if (ret > 0)
flip();
if (ret < 0)
LOGE("An update request has failed.\n");
}
else
{
gForceRender = 0;
PageManager::Render();
flip();
}
}
gGuiConsoleRunning = 0;
return NULL;
}
extern "C" int gui_console_only()
{
if (!gGuiInitialized) return -1;
gGuiConsoleTerminate = 0;
gGuiConsoleRunning = 1;
// Start by spinning off an input handler.
pthread_t t;
pthread_create(&t, NULL, console_thread, NULL);
return 0;
}
|