Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 2 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 38 | extern "C" |
| 39 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | #include <pixelflinger/pixelflinger.h> |
| 43 | } |
| 44 | |
| 45 | #include "rapidxml.hpp" |
| 46 | #include "objects.hpp" |
| 47 | #include "../data.hpp" |
| 48 | #include "../variables.h" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 49 | #include "../partitions.hpp" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 50 | #include "../twrp-functions.hpp" |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 51 | #include "../openrecoveryscript.hpp" |
| 52 | #include "../orscmd/orscmd.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 53 | #include "blanktimer.hpp" |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 54 | #include "../tw_atomic.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 56 | // Enable to print render time of each frame to the log file |
| 57 | //#define PRINT_RENDER_TIME 1 |
| 58 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 59 | #ifdef _EVENT_LOGGING |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 60 | #define LOGEVENT(...) LOGERR(__VA_ARGS__) |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 61 | #else |
| 62 | #define LOGEVENT(...) do {} while (0) |
| 63 | #endif |
| 64 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | const static int CURTAIN_FADE = 32; |
| 66 | |
| 67 | using namespace rapidxml; |
| 68 | |
| 69 | // Global values |
| 70 | static gr_surface gCurtain = NULL; |
| 71 | static int gGuiInitialized = 0; |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 72 | static TWAtomicInt gForceRender; |
| 73 | const int gNoAnimation = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 74 | blanktimer blankTimer; |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 75 | int ors_read_fd = -1; |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 76 | static FILE* orsout = NULL; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 77 | static float scale_theme_w = 1; |
| 78 | static float scale_theme_h = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
| 80 | // Needed by pages.cpp too |
| 81 | int gGuiRunning = 0; |
| 82 | |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 83 | int g_pty_fd = -1; // set by terminal on init |
| 84 | void terminal_pty_read(); |
| 85 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 86 | static int gRecorder = -1; |
| 87 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | extern "C" void gr_write_frame_to_file(int fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 90 | void flip(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | if (gRecorder != -1) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 93 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 94 | timespec time; |
| 95 | clock_gettime(CLOCK_MONOTONIC, &time); |
| 96 | write(gRecorder, &time, sizeof(timespec)); |
| 97 | gr_write_frame_to_file(gRecorder); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 98 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 102 | void rapidxml::parse_error_handler(const char *what, void *where) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 104 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 110 | static void curtainSet() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 112 | gr_color(0, 0, 0, 255); |
| 113 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 114 | gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), TW_X_OFFSET, TW_Y_OFFSET); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 115 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | static void curtainRaise(gr_surface surface) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | int sy = 0; |
| 121 | int h = gr_get_height(gCurtain) - 1; |
| 122 | int w = gr_get_width(gCurtain); |
| 123 | int fy = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | int msw = gr_get_width(surface); |
| 126 | int msh = gr_get_height(surface); |
| 127 | int CURTAIN_RATE = msh / 30; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 129 | if (gNoAnimation == 0) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 130 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 131 | for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 132 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 133 | gr_blit(surface, 0, 0, msw, msh, 0, 0); |
| 134 | gr_blit(gCurtain, 0, sy, w, h, 0, 0); |
| 135 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 138 | gr_blit(surface, 0, 0, msw, msh, 0, 0); |
| 139 | flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 142 | void curtainClose() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | { |
| 144 | #if 0 |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 145 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | if (gNoAnimation == 0) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 152 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 153 | for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 154 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | gr_blit(gCurtain, 0, sy, w, h, 0, 0); |
| 156 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 157 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), |
| 159 | gr_get_height(gCurtain), 0, 0); |
| 160 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 161 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 162 | if (gRecorder != -1) |
| 163 | close(gRecorder); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 165 | int fade; |
| 166 | for (fade = 16; fade < 255; fade += CURTAIN_FADE) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 167 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 168 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 173 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 174 | gr_color(0, 0, 0, 255); |
| 175 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 176 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 178 | #else |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 179 | gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0); |
| 180 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 181 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | } |
| 183 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 184 | class InputHandler |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 186 | public: |
| 187 | void init() |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 188 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 189 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 199 | #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 Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 205 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 206 | #else |
| 207 | LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n"); |
| 208 | #endif |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 209 | } |
| 210 | |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 211 | // process input events. returns true if any event was received. |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 212 | bool processInput(int timeout_ms); |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 213 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 214 | void handleDrag(); |
| 215 | |
| 216 | private: |
| 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 | |
| 253 | InputHandler input_handler; |
| 254 | |
| 255 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 256 | bool InputHandler::processInput(int timeout_ms) |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 257 | { |
| 258 | input_event ev; |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 259 | int ret = ev_get(&ev, timeout_ms); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 260 | |
| 261 | if (ret < 0) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 262 | { |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 263 | // 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 |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 266 | if (touch_status || key_status) |
| 267 | processHoldAndRepeat(); |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 268 | return (ret != -2); // -2 means no more events in the queue |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 269 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 270 | |
| 271 | switch (ev.type) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 272 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 273 | case EV_ABS: |
| 274 | process_EV_ABS(ev); |
| 275 | break; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 276 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 277 | case EV_REL: |
| 278 | process_EV_REL(ev); |
| 279 | break; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 280 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 281 | case EV_KEY: |
| 282 | process_EV_KEY(ev); |
| 283 | break; |
| 284 | } |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 285 | |
| 286 | blankTimer.resetTimerAndUnblank(); |
| 287 | return true; // we got an event, so there might be more in the queue |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void 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); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 307 | } |
| 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); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 313 | } |
| 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(); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 320 | } |
| 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(); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | void 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); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void 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 Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 347 | #ifndef TW_USE_KEY_CODE_TOUCH_SYNC |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 348 | if (state == AS_IN_ACTION_AREA) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 349 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 350 | LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y); |
| 351 | PageManager::NotifyTouch(TOUCH_RELEASE, x, y); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 352 | } |
| 353 | touch_status = TS_NONE; |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 354 | #endif |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 355 | } |
| 356 | else |
| 357 | { |
| 358 | if (!touch_status) |
| 359 | { |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 360 | #ifndef TW_USE_KEY_CODE_TOUCH_SYNC |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 361 | doTouchStart(); |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 362 | #endif |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 366 | if (state == AS_IN_ACTION_AREA) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 367 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 368 | LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 369 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 370 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 371 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void 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 Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 382 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 383 | MouseCursor *cursor = PageManager::GetMouseCursor(); |
| 384 | if(ev.value == 1) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 385 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 386 | 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 Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 394 | { |
| 395 | cursor->GetPos(x, y); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 396 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 397 | LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y); |
| 398 | PageManager::NotifyTouch(TOUCH_RELEASE, x, y); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 399 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 400 | touch_status = TS_NONE; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 401 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 402 | } |
| 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 Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 412 | #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 |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 419 | 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 Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 424 | } else { |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 425 | key_status = KS_NONE; |
| 426 | touch_status = TS_NONE; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 427 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 428 | } else { |
| 429 | // This is a key release |
| 430 | kb->KeyUp(ev.code); |
| 431 | key_status = KS_NONE; |
| 432 | touch_status = TS_NONE; |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 433 | #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 Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 439 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 440 | } |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 441 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 442 | void 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 Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 456 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 474 | static void setup_ors_command() |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 475 | { |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 476 | ors_read_fd = -1; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 477 | |
| 478 | unlink(ORS_INPUT_FILE); |
| 479 | if (mkfifo(ORS_INPUT_FILE, 06660) != 0) { |
| 480 | LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 481 | return; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 482 | } |
| 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 Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 487 | return; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 490 | ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK); |
| 491 | if (ors_read_fd < 0) { |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 492 | LOGINFO("Unable to open %s\n", ORS_INPUT_FILE); |
| 493 | unlink(ORS_INPUT_FILE); |
| 494 | unlink(ORS_OUTPUT_FILE); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 495 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 496 | } |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 497 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 498 | // callback called after a CLI command was executed |
| 499 | static 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 Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 514 | static void ors_command_read() |
| 515 | { |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 516 | char command[1024]; |
| 517 | int read_ret = read(ors_read_fd, &command, sizeof(command)); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 518 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 519 | if (read_ret > 0) { |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 520 | 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) { |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 533 | fputs("Failed, operation in progress\n", orsout); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 534 | LOGINFO("Command cannot be performed, operation in progress.\n"); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 535 | fclose(orsout); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 536 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 537 | if (strlen(command) == 11 && strncmp(command, "dumpstrings", 11) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 538 | gui_set_FILE(orsout); |
| 539 | PageManager::GetResources()->DumpStrings(); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 540 | ors_command_done(); |
| 541 | } else { |
| 542 | // mirror output messages |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 543 | gui_set_FILE(orsout); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 544 | // 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 Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 559 | } |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 560 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 561 | } else { |
| 562 | LOGINFO("ORS command line read returned an error: %i, %i, %s\n", read_ret, errno, strerror(errno)); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 563 | } |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 564 | } |
| 565 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 566 | // Get and dispatch input events until it's time to draw the next frame |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 567 | // 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 |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 570 | static void loopTimer(int input_timeout_ms) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 571 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 572 | static timespec lastCall; |
| 573 | static int initialized = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 574 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 575 | if (!initialized) |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 576 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 577 | clock_gettime(CLOCK_MONOTONIC, &lastCall); |
| 578 | initialized = 1; |
| 579 | return; |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 580 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 581 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 582 | do |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 583 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 584 | bool got_event = input_handler.processInput(input_timeout_ms); // get inputs but don't send drag notices |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 585 | timespec curTime; |
| 586 | clock_gettime(CLOCK_MONOTONIC, &curTime); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 587 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 588 | timespec diff = TWFunc::timespec_diff(lastCall, curTime); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 589 | |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 590 | // 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 595 | { |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 596 | // int32_t input_time = TWFunc::timespec_diff_ms(lastCall, curTime); |
| 597 | // LOGINFO("loopTimer(): %u ms, count: %u\n", input_time, count); |
| 598 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 599 | lastCall = curTime; |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 600 | input_handler.handleDrag(); // send only drag notices if needed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 601 | return; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 602 | } |
| 603 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 604 | // We need to sleep some period time microseconds |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 605 | //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000); |
| 606 | //usleep(sleepTime); // removed so we can scan for input |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 607 | input_timeout_ms = 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 608 | } while (1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 609 | } |
| 610 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 611 | static int runPages(const char *page_name, const int stop_on_page_done) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 612 | { |
Ethan Yonker | 3f15be4 | 2015-02-09 09:39:47 -0600 | [diff] [blame] | 613 | DataManager::SetValue("tw_page_done", 0); |
| 614 | DataManager::SetValue("tw_gui_done", 0); |
| 615 | |
Ethan Yonker | afde098 | 2016-01-23 08:55:35 -0600 | [diff] [blame^] | 616 | if (page_name) { |
| 617 | PageManager::SetStartPage(page_name); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 618 | gui_changePage(page_name); |
Ethan Yonker | afde098 | 2016-01-23 08:55:35 -0600 | [diff] [blame^] | 619 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 620 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 621 | // Raise the curtain |
| 622 | if (gCurtain != NULL) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 623 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 624 | gr_surface surface; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 625 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 626 | PageManager::Render(); |
| 627 | gr_get_surface(&surface); |
| 628 | curtainRaise(surface); |
| 629 | gr_free_surface(surface); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 630 | } |
| 631 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 632 | gGuiRunning = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 633 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 634 | DataManager::SetValue("tw_loaded", 1); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 635 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 636 | #ifndef TW_OEM_BUILD |
| 637 | struct timeval timeout; |
| 638 | fd_set fdset; |
| 639 | int has_data = 0; |
| 640 | #endif |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 641 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 642 | int input_timeout_ms = 0; |
| 643 | int idle_frames = 0; |
| 644 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 645 | for (;;) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 646 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 647 | loopTimer(input_timeout_ms); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 648 | 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 Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 659 | #ifndef TW_OEM_BUILD |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 660 | if (ors_read_fd > 0 && !orsout) { // orsout is non-NULL if a command is still running |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 661 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 671 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 672 | if (!gForceRender.get_value()) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 673 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 674 | int ret = PageManager::Update(); |
| 675 | if (ret == 0) |
| 676 | ++idle_frames; |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 677 | else if (ret == -2) |
| 678 | break; // Theme reload failure |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 679 | 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 Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 683 | |
| 684 | #ifndef PRINT_RENDER_TIME |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 685 | if (ret > 1) |
| 686 | PageManager::Render(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 687 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 688 | if (ret > 0) |
| 689 | flip(); |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 690 | #else |
| 691 | if (ret > 1) |
| 692 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 693 | timespec start, end; |
| 694 | int32_t render_t, flip_t; |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 695 | 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 | } |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 706 | else if (ret > 0) |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 707 | flip(); |
| 708 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 709 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 710 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 711 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 712 | gForceRender.set_value(0); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 713 | PageManager::Render(); |
| 714 | flip(); |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 715 | input_timeout_ms = 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 716 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 717 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 718 | blankTimer.checkForTimeout(); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 719 | if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 720 | { |
| 721 | gui_changePage("main"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 722 | break; |
| 723 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 724 | if (DataManager::GetIntValue("tw_gui_done") != 0) |
| 725 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 726 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 727 | if (ors_read_fd > 0) |
| 728 | close(ors_read_fd); |
| 729 | ors_read_fd = -1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 730 | gGuiRunning = 0; |
| 731 | return 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 732 | } |
| 733 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 734 | int gui_forceRender(void) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 735 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 736 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 737 | return 0; |
| 738 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 739 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 740 | int gui_changePage(std::string newPage) |
| 741 | { |
| 742 | LOGINFO("Set page: '%s'\n", newPage.c_str()); |
| 743 | PageManager::ChangePage(newPage); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 744 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | int gui_changeOverlay(std::string overlay) |
| 749 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 750 | LOGINFO("Set overlay: '%s'\n", overlay.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 751 | PageManager::ChangeOverlay(overlay); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 752 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 756 | std::string gui_parse_text(std::string str) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 757 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 758 | // This function parses text for DataManager values encompassed by %value% in the XML |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 759 | // and string resources (%@resource_name%) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 760 | size_t pos = 0, next, end; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 761 | |
| 762 | while (1) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 763 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 764 | 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 789 | if (next == std::string::npos) |
| 790 | return str; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 791 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 792 | end = str.find('%', next + 1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 793 | if (end == std::string::npos) |
| 794 | return str; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 795 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 796 | // We have a block of data |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 797 | std::string var = str.substr(next + 1, (end - next) - 1); |
| 798 | str.erase(next, (end - next) + 1); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 799 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 800 | if (next + 1 == end) |
| 801 | str.insert(next, 1, '%'); |
| 802 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 803 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 804 | std::string value; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 805 | 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 811 | str.insert(next, value); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 812 | } |
| 813 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 814 | pos = next + 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 818 | std::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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 822 | extern "C" int gui_init(void) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 823 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 824 | gr_init(); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 825 | std::string curtain_path = TWRES "images/curtain.jpg"; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 826 | gr_surface source_Surface = NULL; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 827 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 828 | if (res_create_surface(curtain_path.c_str(), &source_Surface)) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 829 | { |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 830 | printf("Unable to locate '%s'\nDid you set a TW_THEME in your config files?\n", curtain_path.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 831 | return -1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 832 | } |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 833 | if (gr_get_width(source_Surface) != (unsigned)gr_fb_width() || gr_get_height(source_Surface) != (unsigned)gr_fb_height()) { |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 834 | // 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 846 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 847 | curtainSet(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 848 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 849 | ev_init(); |
| 850 | return 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 851 | } |
| 852 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 853 | extern "C" int gui_loadResources(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 854 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 855 | #ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 856 | int check = 0; |
| 857 | DataManager::GetValue(TW_IS_ENCRYPTED, check); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 858 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 859 | if (check) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 860 | { |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 861 | if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt")) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 862 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 863 | gui_err("base_pkg_err=Failed to load base packages."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 864 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 865 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 866 | else |
| 867 | check = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 868 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 869 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 870 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 876 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 877 | int retry_count = 5; |
| 878 | while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 879 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 880 | usleep(500000); |
| 881 | retry_count--; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 882 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 883 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 884 | if (!PartitionManager.Mount_Settings_Storage(true)) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 885 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 886 | LOGINFO("Unable to mount %s during GUI startup.\n", theme_path.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 887 | check = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 891 | theme_path += "/TWRP/theme/ui.zip"; |
| 892 | if (check || PageManager::LoadPackage("TWRP", theme_path, "main")) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 893 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 894 | #endif // ifndef TW_OEM_BUILD |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 895 | if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main")) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 896 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 897 | gui_err("base_pkg_err=Failed to load base packages."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 898 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 899 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 900 | #ifndef TW_OEM_BUILD |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 901 | } |
| 902 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 903 | #endif // ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 904 | // Set the default package |
| 905 | PageManager::SelectPackage("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 906 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 907 | gGuiInitialized = 1; |
| 908 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 909 | |
| 910 | error: |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 911 | LOGERR("An internal error has occurred: unable to load theme.\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 912 | gGuiInitialized = 0; |
| 913 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 914 | } |
| 915 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 916 | extern "C" int gui_loadCustomResources(void) |
| 917 | { |
| 918 | #ifndef TW_OEM_BUILD |
| 919 | if (!PartitionManager.Mount_Settings_Storage(false)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 920 | LOGINFO("Unable to mount settings storage during GUI startup.\n"); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 921 | 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 Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 931 | if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 932 | gui_err("base_pkg_err=Failed to load base packages."); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 933 | goto error; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | // Set the default package |
| 938 | PageManager::SelectPackage("TWRP"); |
| 939 | #endif |
| 940 | return 0; |
| 941 | |
| 942 | error: |
| 943 | LOGERR("An internal error has occurred: unable to load theme.\n"); |
| 944 | gGuiInitialized = 0; |
| 945 | return -1; |
| 946 | } |
| 947 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 948 | extern "C" int gui_start(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 949 | { |
Ethan Yonker | afde098 | 2016-01-23 08:55:35 -0600 | [diff] [blame^] | 950 | return gui_startPage("main", 1, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 951 | } |
| 952 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 953 | extern "C" int gui_startPage(const char *page_name, const int allow_commands, int stop_on_page_done) |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 954 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 955 | if (!gGuiInitialized) |
| 956 | return -1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 957 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 958 | // Set the default package |
| 959 | PageManager::SelectPackage("TWRP"); |
| 960 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 961 | input_handler.init(); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 962 | #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 Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 974 | return runPages(page_name, stop_on_page_done); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 977 | |
| 978 | extern "C" void set_scale_values(float w, float h) |
| 979 | { |
| 980 | scale_theme_w = w; |
| 981 | scale_theme_h = h; |
| 982 | } |
| 983 | |
| 984 | extern "C" int scale_theme_x(int initial_x) |
| 985 | { |
| 986 | if (scale_theme_w != 1) { |
that | ba75a0e | 2015-02-14 21:20:10 +0100 | [diff] [blame] | 987 | int scaled = (float)initial_x * scale_theme_w; |
| 988 | if (scaled == 0 && initial_x > 0) |
| 989 | return 1; |
| 990 | return scaled; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 991 | } |
| 992 | return initial_x; |
| 993 | } |
| 994 | |
| 995 | extern "C" int scale_theme_y(int initial_y) |
| 996 | { |
| 997 | if (scale_theme_h != 1) { |
that | ba75a0e | 2015-02-14 21:20:10 +0100 | [diff] [blame] | 998 | int scaled = (float)initial_y * scale_theme_h; |
| 999 | if (scaled == 0 && initial_y > 0) |
| 1000 | return 1; |
| 1001 | return scaled; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 1002 | } |
| 1003 | return initial_y; |
| 1004 | } |
| 1005 | |
| 1006 | extern "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 | |
| 1017 | extern "C" float get_scale_w() |
| 1018 | { |
| 1019 | return scale_theme_w; |
| 1020 | } |
| 1021 | |
| 1022 | extern "C" float get_scale_h() |
| 1023 | { |
| 1024 | return scale_theme_h; |
| 1025 | } |