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 | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 616 | if (page_name) |
| 617 | gui_changePage(page_name); |
| 618 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 619 | // Raise the curtain |
| 620 | if (gCurtain != NULL) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 621 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 622 | gr_surface surface; |
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 | PageManager::Render(); |
| 625 | gr_get_surface(&surface); |
| 626 | curtainRaise(surface); |
| 627 | gr_free_surface(surface); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 628 | } |
| 629 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 630 | gGuiRunning = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 631 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 632 | DataManager::SetValue("tw_loaded", 1); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 633 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 634 | #ifndef TW_OEM_BUILD |
| 635 | struct timeval timeout; |
| 636 | fd_set fdset; |
| 637 | int has_data = 0; |
| 638 | #endif |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 639 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 640 | int input_timeout_ms = 0; |
| 641 | int idle_frames = 0; |
| 642 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 643 | for (;;) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 644 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 645 | loopTimer(input_timeout_ms); |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 646 | if (g_pty_fd > 0) { |
| 647 | // TODO: this is not nice, we should have one central select for input, pty, and ors |
| 648 | FD_ZERO(&fdset); |
| 649 | FD_SET(g_pty_fd, &fdset); |
| 650 | timeout.tv_sec = 0; |
| 651 | timeout.tv_usec = 1; |
| 652 | has_data = select(g_pty_fd+1, &fdset, NULL, NULL, &timeout); |
| 653 | if (has_data > 0) { |
| 654 | terminal_pty_read(); |
| 655 | } |
| 656 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 657 | #ifndef TW_OEM_BUILD |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 658 | 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] | 659 | FD_ZERO(&fdset); |
| 660 | FD_SET(ors_read_fd, &fdset); |
| 661 | timeout.tv_sec = 0; |
| 662 | timeout.tv_usec = 1; |
| 663 | has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout); |
| 664 | if (has_data > 0) { |
| 665 | ors_command_read(); |
| 666 | } |
| 667 | } |
| 668 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 669 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 670 | if (!gForceRender.get_value()) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 671 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 672 | int ret = PageManager::Update(); |
| 673 | if (ret == 0) |
| 674 | ++idle_frames; |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 675 | else if (ret == -2) |
| 676 | break; // Theme reload failure |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 677 | else |
| 678 | idle_frames = 0; |
| 679 | // due to possible animation objects, we need to delay activating the input timeout |
| 680 | input_timeout_ms = idle_frames > 15 ? 1000 : 0; |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 681 | |
| 682 | #ifndef PRINT_RENDER_TIME |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 683 | if (ret > 1) |
| 684 | PageManager::Render(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 685 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 686 | if (ret > 0) |
| 687 | flip(); |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 688 | #else |
| 689 | if (ret > 1) |
| 690 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 691 | timespec start, end; |
| 692 | int32_t render_t, flip_t; |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 693 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 694 | PageManager::Render(); |
| 695 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 696 | render_t = TWFunc::timespec_diff_ms(start, end); |
| 697 | |
| 698 | flip(); |
| 699 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 700 | flip_t = TWFunc::timespec_diff_ms(end, start); |
| 701 | |
| 702 | LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t); |
| 703 | } |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 704 | else if (ret > 0) |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 705 | flip(); |
| 706 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 707 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 708 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 709 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 710 | gForceRender.set_value(0); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 711 | PageManager::Render(); |
| 712 | flip(); |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 713 | input_timeout_ms = 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 714 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 715 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 716 | blankTimer.checkForTimeout(); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 717 | if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 718 | { |
| 719 | gui_changePage("main"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 720 | break; |
| 721 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 722 | if (DataManager::GetIntValue("tw_gui_done") != 0) |
| 723 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 724 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 725 | if (ors_read_fd > 0) |
| 726 | close(ors_read_fd); |
| 727 | ors_read_fd = -1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 728 | gGuiRunning = 0; |
| 729 | return 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 730 | } |
| 731 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 732 | int gui_forceRender(void) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 733 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 734 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 735 | return 0; |
| 736 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 737 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 738 | int gui_changePage(std::string newPage) |
| 739 | { |
| 740 | LOGINFO("Set page: '%s'\n", newPage.c_str()); |
| 741 | PageManager::ChangePage(newPage); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 742 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | int gui_changeOverlay(std::string overlay) |
| 747 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 748 | LOGINFO("Set overlay: '%s'\n", overlay.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 749 | PageManager::ChangeOverlay(overlay); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 750 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 754 | std::string gui_parse_text(std::string str) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 755 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 756 | // This function parses text for DataManager values encompassed by %value% in the XML |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 757 | // and string resources (%@resource_name%) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 758 | size_t pos = 0, next, end; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 759 | |
| 760 | while (1) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 761 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 762 | next = str.find("{@", pos); |
| 763 | if (next == std::string::npos) |
| 764 | break; |
| 765 | |
| 766 | end = str.find('}', next + 1); |
| 767 | if (end == std::string::npos) |
| 768 | break; |
| 769 | |
| 770 | std::string var = str.substr(next + 2, (end - next) - 2); |
| 771 | str.erase(next, (end - next) + 1); |
| 772 | |
| 773 | size_t default_loc = var.find('=', 0); |
| 774 | std::string lookup; |
| 775 | if (default_loc == std::string::npos) { |
| 776 | str.insert(next, PageManager::GetResources()->FindString(var)); |
| 777 | } else { |
| 778 | lookup = var.substr(0, default_loc); |
| 779 | std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1); |
| 780 | str.insert(next, PageManager::GetResources()->FindString(lookup, default_string)); |
| 781 | } |
| 782 | } |
| 783 | pos = 0; |
| 784 | while (1) |
| 785 | { |
| 786 | next = str.find('%', pos); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 787 | if (next == std::string::npos) |
| 788 | return str; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 789 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 790 | end = str.find('%', next + 1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 791 | if (end == std::string::npos) |
| 792 | return str; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 793 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 794 | // We have a block of data |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 795 | std::string var = str.substr(next + 1, (end - next) - 1); |
| 796 | str.erase(next, (end - next) + 1); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 797 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 798 | if (next + 1 == end) |
| 799 | str.insert(next, 1, '%'); |
| 800 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 801 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 802 | std::string value; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 803 | if (var.size() > 0 && var[0] == '@') { |
| 804 | // this is a string resource ("%@string_name%") |
| 805 | value = PageManager::GetResources()->FindString(var.substr(1)); |
| 806 | str.insert(next, value); |
| 807 | } |
| 808 | else if (DataManager::GetValue(var, value) == 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 809 | str.insert(next, value); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 812 | pos = next + 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 816 | std::string gui_lookup(const std::string& resource_name, const std::string& default_value) { |
| 817 | return PageManager::GetResources()->FindString(resource_name, default_value); |
| 818 | } |
| 819 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 820 | extern "C" int gui_init(void) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 821 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 822 | gr_init(); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 823 | std::string curtain_path = TWRES "images/curtain.jpg"; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 824 | gr_surface source_Surface = NULL; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 825 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 826 | if (res_create_surface(curtain_path.c_str(), &source_Surface)) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 827 | { |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 828 | 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] | 829 | return -1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 830 | } |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 831 | 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] | 832 | // We need to scale the curtain to fit the screen |
| 833 | float scale_w = (float)gr_fb_width() / (float)gr_get_width(source_Surface); |
| 834 | float scale_h = (float)gr_fb_height() / (float)gr_get_height(source_Surface); |
| 835 | if (res_scale_surface(source_Surface, &gCurtain, scale_w, scale_h)) { |
| 836 | LOGINFO("Failed to scale curtain\n"); |
| 837 | gCurtain = source_Surface; |
| 838 | } else { |
| 839 | LOGINFO("Scaling the curtain width %fx and height %fx\n", scale_w, scale_h); |
| 840 | } |
| 841 | } else { |
| 842 | gCurtain = source_Surface; |
| 843 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 844 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 845 | curtainSet(); |
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 | ev_init(); |
| 848 | return 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 849 | } |
| 850 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 851 | extern "C" int gui_loadResources(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 852 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 853 | #ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 854 | int check = 0; |
| 855 | DataManager::GetValue(TW_IS_ENCRYPTED, check); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 856 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 857 | if (check) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 858 | { |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 859 | if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt")) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 860 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 861 | gui_err("base_pkg_err=Failed to load base packages."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 862 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 863 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 864 | else |
| 865 | check = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 866 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 867 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 868 | if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main")) |
| 869 | { |
| 870 | std::string theme_path; |
| 871 | |
| 872 | theme_path = DataManager::GetSettingsStoragePath(); |
| 873 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 874 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 875 | int retry_count = 5; |
| 876 | while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 877 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 878 | usleep(500000); |
| 879 | retry_count--; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 880 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 881 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 882 | if (!PartitionManager.Mount_Settings_Storage(true)) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 883 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 884 | 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] | 885 | check = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 889 | theme_path += "/TWRP/theme/ui.zip"; |
| 890 | if (check || PageManager::LoadPackage("TWRP", theme_path, "main")) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 891 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 892 | #endif // ifndef TW_OEM_BUILD |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 893 | if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main")) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 894 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 895 | gui_err("base_pkg_err=Failed to load base packages."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 896 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 897 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 898 | #ifndef TW_OEM_BUILD |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 899 | } |
| 900 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 901 | #endif // ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 902 | // Set the default package |
| 903 | PageManager::SelectPackage("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 904 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 905 | gGuiInitialized = 1; |
| 906 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 907 | |
| 908 | error: |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 909 | LOGERR("An internal error has occurred: unable to load theme.\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 910 | gGuiInitialized = 0; |
| 911 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 912 | } |
| 913 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 914 | extern "C" int gui_loadCustomResources(void) |
| 915 | { |
| 916 | #ifndef TW_OEM_BUILD |
| 917 | if (!PartitionManager.Mount_Settings_Storage(false)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 918 | LOGINFO("Unable to mount settings storage during GUI startup.\n"); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | std::string theme_path = DataManager::GetSettingsStoragePath(); |
| 923 | theme_path += "/TWRP/theme/ui.zip"; |
| 924 | // Check for a custom theme |
| 925 | if (TWFunc::Path_Exists(theme_path)) { |
| 926 | // There is a custom theme, try to load it |
| 927 | if (PageManager::ReloadPackage("TWRP", theme_path)) { |
| 928 | // Custom theme failed to load, try to load stock theme |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 929 | if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 930 | gui_err("base_pkg_err=Failed to load base packages."); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 931 | goto error; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | // Set the default package |
| 936 | PageManager::SelectPackage("TWRP"); |
| 937 | #endif |
| 938 | return 0; |
| 939 | |
| 940 | error: |
| 941 | LOGERR("An internal error has occurred: unable to load theme.\n"); |
| 942 | gGuiInitialized = 0; |
| 943 | return -1; |
| 944 | } |
| 945 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 946 | extern "C" int gui_start(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 947 | { |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 948 | return gui_startPage(NULL, 1, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 951 | 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] | 952 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 953 | if (!gGuiInitialized) |
| 954 | return -1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 955 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 956 | // Set the default package |
| 957 | PageManager::SelectPackage("TWRP"); |
| 958 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 959 | input_handler.init(); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 960 | #ifndef TW_OEM_BUILD |
| 961 | if (allow_commands) |
| 962 | { |
| 963 | if (ors_read_fd < 0) |
| 964 | setup_ors_command(); |
| 965 | } else { |
| 966 | if (ors_read_fd >= 0) { |
| 967 | close(ors_read_fd); |
| 968 | ors_read_fd = -1; |
| 969 | } |
| 970 | } |
| 971 | #endif |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 972 | return runPages(page_name, stop_on_page_done); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 975 | |
| 976 | extern "C" void set_scale_values(float w, float h) |
| 977 | { |
| 978 | scale_theme_w = w; |
| 979 | scale_theme_h = h; |
| 980 | } |
| 981 | |
| 982 | extern "C" int scale_theme_x(int initial_x) |
| 983 | { |
| 984 | if (scale_theme_w != 1) { |
that | ba75a0e | 2015-02-14 21:20:10 +0100 | [diff] [blame] | 985 | int scaled = (float)initial_x * scale_theme_w; |
| 986 | if (scaled == 0 && initial_x > 0) |
| 987 | return 1; |
| 988 | return scaled; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 989 | } |
| 990 | return initial_x; |
| 991 | } |
| 992 | |
| 993 | extern "C" int scale_theme_y(int initial_y) |
| 994 | { |
| 995 | if (scale_theme_h != 1) { |
that | ba75a0e | 2015-02-14 21:20:10 +0100 | [diff] [blame] | 996 | int scaled = (float)initial_y * scale_theme_h; |
| 997 | if (scaled == 0 && initial_y > 0) |
| 998 | return 1; |
| 999 | return scaled; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 1000 | } |
| 1001 | return initial_y; |
| 1002 | } |
| 1003 | |
| 1004 | extern "C" int scale_theme_min(int initial_value) |
| 1005 | { |
| 1006 | if (scale_theme_w != 1 || scale_theme_h != 1) { |
| 1007 | if (scale_theme_w < scale_theme_h) |
| 1008 | return scale_theme_x(initial_value); |
| 1009 | else |
| 1010 | return scale_theme_y(initial_value); |
| 1011 | } |
| 1012 | return initial_value; |
| 1013 | } |
| 1014 | |
| 1015 | extern "C" float get_scale_w() |
| 1016 | { |
| 1017 | return scale_theme_w; |
| 1018 | } |
| 1019 | |
| 1020 | extern "C" float get_scale_h() |
| 1021 | { |
| 1022 | return scale_theme_h; |
| 1023 | } |