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 gGuiConsoleRunning; |
| 73 | static TWAtomicInt gGuiConsoleTerminate; |
| 74 | static TWAtomicInt gForceRender; |
| 75 | const int gNoAnimation = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 76 | blanktimer blankTimer; |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 77 | int ors_read_fd = -1; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 78 | static float scale_theme_w = 1; |
| 79 | static float scale_theme_h = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 80 | |
| 81 | // Needed by pages.cpp too |
| 82 | int gGuiRunning = 0; |
| 83 | |
| 84 | static int gRecorder = -1; |
| 85 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 86 | extern "C" void gr_write_frame_to_file(int fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | void flip(void) |
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 | if (gRecorder != -1) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 91 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | timespec time; |
| 93 | clock_gettime(CLOCK_MONOTONIC, &time); |
| 94 | write(gRecorder, &time, sizeof(timespec)); |
| 95 | gr_write_frame_to_file(gRecorder); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 96 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 100 | void rapidxml::parse_error_handler(const char *what, void *where) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 102 | fprintf(stderr, "Parser error: %s\n", what); |
| 103 | fprintf(stderr, " Start of string: %s\n",(char *) where); |
| 104 | LOGERR("Error parsing XML file.\n"); |
| 105 | //abort(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 108 | static void curtainSet() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 109 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 110 | gr_color(0, 0, 0, 255); |
| 111 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 112 | 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] | 113 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | static void curtainRaise(gr_surface surface) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | int sy = 0; |
| 119 | int h = gr_get_height(gCurtain) - 1; |
| 120 | int w = gr_get_width(gCurtain); |
| 121 | int fy = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | int msw = gr_get_width(surface); |
| 124 | int msh = gr_get_height(surface); |
| 125 | int CURTAIN_RATE = msh / 30; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 127 | if (gNoAnimation == 0) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 128 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 129 | for (; h > 0; h -= CURTAIN_RATE, sy += CURTAIN_RATE, fy += CURTAIN_RATE) |
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 | gr_blit(surface, 0, 0, msw, msh, 0, 0); |
| 132 | gr_blit(gCurtain, 0, sy, w, h, 0, 0); |
| 133 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 136 | gr_blit(surface, 0, 0, msw, msh, 0, 0); |
| 137 | flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 140 | void curtainClose() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 141 | { |
| 142 | #if 0 |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 143 | int w = gr_get_width(gCurtain); |
| 144 | int h = 1; |
| 145 | int sy = gr_get_height(gCurtain) - 1; |
| 146 | int fbh = gr_fb_height(); |
| 147 | int CURTAIN_RATE = fbh / 30; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 148 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 149 | if (gNoAnimation == 0) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 150 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | for (; h < fbh; h += CURTAIN_RATE, sy -= CURTAIN_RATE) |
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 | gr_blit(gCurtain, 0, sy, w, h, 0, 0); |
| 154 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 155 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), |
| 157 | gr_get_height(gCurtain), 0, 0); |
| 158 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 160 | if (gRecorder != -1) |
| 161 | close(gRecorder); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 162 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 163 | int fade; |
| 164 | for (fade = 16; fade < 255; fade += CURTAIN_FADE) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 165 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 166 | gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), |
| 167 | gr_get_height(gCurtain), 0, 0); |
| 168 | gr_color(0, 0, 0, fade); |
| 169 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 170 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 171 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 172 | gr_color(0, 0, 0, 255); |
| 173 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 174 | gr_flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 176 | #else |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 177 | gr_blit(gCurtain, 0, 0, gr_get_width(gCurtain), gr_get_height(gCurtain), 0, 0); |
| 178 | gr_flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 179 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 180 | } |
| 181 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 182 | class InputHandler |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 183 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 184 | public: |
| 185 | void init() |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 186 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 187 | // these might be read from DataManager in the future |
| 188 | touch_hold_ms = 500; |
| 189 | touch_repeat_ms = 100; |
| 190 | key_hold_ms = 500; |
| 191 | key_repeat_ms = 100; |
| 192 | touch_status = TS_NONE; |
| 193 | key_status = KS_NONE; |
| 194 | state = AS_NO_ACTION; |
| 195 | x = y = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 196 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 197 | #ifndef TW_NO_SCREEN_TIMEOUT |
| 198 | { |
| 199 | string seconds; |
| 200 | DataManager::GetValue("tw_screen_timeout_secs", seconds); |
| 201 | blankTimer.setTime(atoi(seconds.c_str())); |
| 202 | blankTimer.resetTimerAndUnblank(); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 203 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 204 | #else |
| 205 | LOGINFO("Skipping screen timeout: TW_NO_SCREEN_TIMEOUT is set\n"); |
| 206 | #endif |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 207 | } |
| 208 | |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 209 | // process input events. returns true if any event was received. |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 210 | bool processInput(int timeout_ms); |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 211 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 212 | void handleDrag(); |
| 213 | |
| 214 | private: |
| 215 | // timeouts for touch/key hold and repeat |
| 216 | int touch_hold_ms; |
| 217 | int touch_repeat_ms; |
| 218 | int key_hold_ms; |
| 219 | int key_repeat_ms; |
| 220 | |
| 221 | enum touch_status_enum { |
| 222 | TS_NONE = 0, |
| 223 | TS_TOUCH_AND_HOLD = 1, |
| 224 | TS_TOUCH_REPEAT = 2, |
| 225 | }; |
| 226 | |
| 227 | enum key_status_enum { |
| 228 | KS_NONE = 0, |
| 229 | KS_KEY_PRESSED = 1, |
| 230 | KS_KEY_REPEAT = 2, |
| 231 | }; |
| 232 | |
| 233 | enum action_state_enum { |
| 234 | AS_IN_ACTION_AREA = 0, // we've touched a spot with an action |
| 235 | AS_NO_ACTION = 1, // we've touched in an empty area (no action) and ignore remaining events until touch release |
| 236 | }; |
| 237 | touch_status_enum touch_status; |
| 238 | key_status_enum key_status; |
| 239 | action_state_enum state; |
| 240 | int x, y; // x and y coordinates of last touch |
| 241 | struct timeval touchStart; // used to track time for long press / key repeat |
| 242 | |
| 243 | void processHoldAndRepeat(); |
| 244 | void process_EV_REL(input_event& ev); |
| 245 | void process_EV_ABS(input_event& ev); |
| 246 | void process_EV_KEY(input_event& ev); |
| 247 | |
| 248 | void doTouchStart(); |
| 249 | }; |
| 250 | |
| 251 | InputHandler input_handler; |
| 252 | |
| 253 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 254 | bool InputHandler::processInput(int timeout_ms) |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 255 | { |
| 256 | input_event ev; |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 257 | int ret = ev_get(&ev, timeout_ms); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 258 | |
| 259 | if (ret < 0) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 260 | { |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 261 | // This path means that we did not get any new touch data, but |
| 262 | // we do not get new touch data if you press and hold on either |
| 263 | // the screen or on a keyboard key or mouse button |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 264 | if (touch_status || key_status) |
| 265 | processHoldAndRepeat(); |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 266 | return (ret != -2); // -2 means no more events in the queue |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 267 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 268 | |
| 269 | switch (ev.type) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 270 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 271 | case EV_ABS: |
| 272 | process_EV_ABS(ev); |
| 273 | break; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 274 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 275 | case EV_REL: |
| 276 | process_EV_REL(ev); |
| 277 | break; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 278 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 279 | case EV_KEY: |
| 280 | process_EV_KEY(ev); |
| 281 | break; |
| 282 | } |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 283 | |
| 284 | blankTimer.resetTimerAndUnblank(); |
| 285 | 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] | 286 | } |
| 287 | |
| 288 | void InputHandler::processHoldAndRepeat() |
| 289 | { |
| 290 | HardwareKeyboard *kb = PageManager::GetHardwareKeyboard(); |
| 291 | |
| 292 | // touch and key repeat section |
| 293 | struct timeval curTime; |
| 294 | gettimeofday(&curTime, NULL); |
| 295 | long seconds = curTime.tv_sec - touchStart.tv_sec; |
| 296 | long useconds = curTime.tv_usec - touchStart.tv_usec; |
| 297 | long mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5; |
| 298 | |
| 299 | if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms) |
| 300 | { |
| 301 | touch_status = TS_TOUCH_REPEAT; |
| 302 | gettimeofday(&touchStart, NULL); |
| 303 | LOGEVENT("TOUCH_HOLD: %d,%d\n", x, y); |
| 304 | PageManager::NotifyTouch(TOUCH_HOLD, x, y); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 305 | } |
| 306 | else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms) |
| 307 | { |
| 308 | LOGEVENT("TOUCH_REPEAT: %d,%d\n", x, y); |
| 309 | gettimeofday(&touchStart, NULL); |
| 310 | PageManager::NotifyTouch(TOUCH_REPEAT, x, y); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 311 | } |
| 312 | else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms) |
| 313 | { |
| 314 | LOGEVENT("KEY_HOLD: %d,%d\n", x, y); |
| 315 | gettimeofday(&touchStart, NULL); |
| 316 | key_status = KS_KEY_REPEAT; |
| 317 | kb->KeyRepeat(); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 318 | } |
| 319 | else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms) |
| 320 | { |
| 321 | LOGEVENT("KEY_REPEAT: %d,%d\n", x, y); |
| 322 | gettimeofday(&touchStart, NULL); |
| 323 | kb->KeyRepeat(); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | void InputHandler::doTouchStart() |
| 328 | { |
| 329 | LOGEVENT("TOUCH_START: %d,%d\n", x, y); |
| 330 | if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0) |
| 331 | state = AS_NO_ACTION; |
| 332 | else |
| 333 | state = AS_IN_ACTION_AREA; |
| 334 | touch_status = TS_TOUCH_AND_HOLD; |
| 335 | gettimeofday(&touchStart, NULL); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void InputHandler::process_EV_ABS(input_event& ev) |
| 339 | { |
| 340 | x = ev.value >> 16; |
| 341 | y = ev.value & 0xFFFF; |
| 342 | |
| 343 | if (ev.code == 0) |
| 344 | { |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 345 | #ifndef TW_USE_KEY_CODE_TOUCH_SYNC |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 346 | if (state == AS_IN_ACTION_AREA) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 347 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 348 | LOGEVENT("TOUCH_RELEASE: %d,%d\n", x, y); |
| 349 | PageManager::NotifyTouch(TOUCH_RELEASE, x, y); |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 350 | } |
| 351 | touch_status = TS_NONE; |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 352 | #endif |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 353 | } |
| 354 | else |
| 355 | { |
| 356 | if (!touch_status) |
| 357 | { |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 358 | #ifndef TW_USE_KEY_CODE_TOUCH_SYNC |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 359 | doTouchStart(); |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 360 | #endif |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 361 | } |
| 362 | else |
| 363 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 364 | if (state == AS_IN_ACTION_AREA) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 365 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 366 | LOGEVENT("TOUCH_DRAG: %d,%d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 368 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 369 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void InputHandler::process_EV_KEY(input_event& ev) |
| 373 | { |
| 374 | HardwareKeyboard *kb = PageManager::GetHardwareKeyboard(); |
| 375 | |
| 376 | // Handle key-press here |
| 377 | LOGEVENT("TOUCH_KEY: %d\n", ev.code); |
| 378 | // Left mouse button is treated as a touch |
| 379 | if(ev.code == BTN_LEFT) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 380 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 381 | MouseCursor *cursor = PageManager::GetMouseCursor(); |
| 382 | if(ev.value == 1) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 383 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 384 | cursor->GetPos(x, y); |
| 385 | doTouchStart(); |
| 386 | } |
| 387 | else if(touch_status) |
| 388 | { |
| 389 | // Left mouse button was previously pressed and now is |
| 390 | // being released so send a TOUCH_RELEASE |
| 391 | if (state == AS_IN_ACTION_AREA) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 392 | { |
| 393 | cursor->GetPos(x, y); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 394 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 395 | LOGEVENT("Mouse TOUCH_RELEASE: %d,%d\n", x, y); |
| 396 | PageManager::NotifyTouch(TOUCH_RELEASE, x, y); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 397 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 398 | touch_status = TS_NONE; |
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 | } |
| 401 | // side mouse button, often used for "back" function |
| 402 | else if(ev.code == BTN_SIDE) |
| 403 | { |
| 404 | if(ev.value == 1) |
| 405 | kb->KeyDown(KEY_BACK); |
| 406 | else |
| 407 | kb->KeyUp(KEY_BACK); |
| 408 | } else if (ev.value != 0) { |
| 409 | // This is a key press |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 410 | #ifdef TW_USE_KEY_CODE_TOUCH_SYNC |
| 411 | if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) { |
| 412 | LOGEVENT("key code %i key press == touch start %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y); |
| 413 | doTouchStart(); |
| 414 | return; |
| 415 | } |
| 416 | #endif |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 417 | if (kb->KeyDown(ev.code)) { |
| 418 | // Key repeat is enabled for this key |
| 419 | key_status = KS_KEY_PRESSED; |
| 420 | touch_status = TS_NONE; |
| 421 | gettimeofday(&touchStart, NULL); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 422 | } else { |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 423 | key_status = KS_NONE; |
| 424 | touch_status = TS_NONE; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 425 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 426 | } else { |
| 427 | // This is a key release |
| 428 | kb->KeyUp(ev.code); |
| 429 | key_status = KS_NONE; |
| 430 | touch_status = TS_NONE; |
Ethan Yonker | 30fa335 | 2015-03-09 13:57:21 -0500 | [diff] [blame] | 431 | #ifdef TW_USE_KEY_CODE_TOUCH_SYNC |
| 432 | if (ev.code == TW_USE_KEY_CODE_TOUCH_SYNC) { |
| 433 | LOGEVENT("key code %i key release == touch release %i %i\n", TW_USE_KEY_CODE_TOUCH_SYNC, x, y); |
| 434 | PageManager::NotifyTouch(TOUCH_RELEASE, x, y); |
| 435 | } |
| 436 | #endif |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 437 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 438 | } |
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 | void InputHandler::process_EV_REL(input_event& ev) |
| 441 | { |
| 442 | // Mouse movement |
| 443 | MouseCursor *cursor = PageManager::GetMouseCursor(); |
| 444 | LOGEVENT("EV_REL %d %d\n", ev.code, ev.value); |
| 445 | if(ev.code == REL_X) |
| 446 | cursor->Move(ev.value, 0); |
| 447 | else if(ev.code == REL_Y) |
| 448 | cursor->Move(0, ev.value); |
| 449 | |
| 450 | if(touch_status) { |
| 451 | cursor->GetPos(x, y); |
| 452 | LOGEVENT("Mouse TOUCH_DRAG: %d, %d\n", x, y); |
| 453 | key_status = KS_NONE; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 454 | } |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void InputHandler::handleDrag() |
| 458 | { |
| 459 | // This allows us to only send one NotifyTouch event per render |
| 460 | // cycle to reduce overhead and perceived input latency. |
| 461 | static int prevx = 0, prevy = 0; // these track where the last drag notice was so that we don't send duplicate drag notices |
| 462 | if (touch_status && (x != prevx || y != prevy)) { |
| 463 | prevx = x; |
| 464 | prevy = y; |
| 465 | if (PageManager::NotifyTouch(TOUCH_DRAG, x, y) > 0) |
| 466 | state = AS_NO_ACTION; |
| 467 | else |
| 468 | state = AS_IN_ACTION_AREA; |
| 469 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 470 | } |
| 471 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 472 | static void setup_ors_command() |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 473 | { |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 474 | ors_read_fd = -1; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 475 | |
| 476 | unlink(ORS_INPUT_FILE); |
| 477 | if (mkfifo(ORS_INPUT_FILE, 06660) != 0) { |
| 478 | LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 479 | return; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 480 | } |
| 481 | unlink(ORS_OUTPUT_FILE); |
| 482 | if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) { |
| 483 | LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE); |
| 484 | unlink(ORS_INPUT_FILE); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 485 | return; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 486 | } |
| 487 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 488 | ors_read_fd = open(ORS_INPUT_FILE, O_RDONLY | O_NONBLOCK); |
| 489 | if (ors_read_fd < 0) { |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 490 | LOGINFO("Unable to open %s\n", ORS_INPUT_FILE); |
| 491 | unlink(ORS_INPUT_FILE); |
| 492 | unlink(ORS_OUTPUT_FILE); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 493 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 494 | } |
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 | static void ors_command_read() |
| 497 | { |
| 498 | FILE* orsout; |
| 499 | char command[1024], result[512]; |
| 500 | int set_page_done = 0, read_ret = 0; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 501 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 502 | if ((read_ret = read(ors_read_fd, &command, sizeof(command))) > 0) { |
| 503 | command[1022] = '\n'; |
| 504 | command[1023] = '\0'; |
| 505 | LOGINFO("Command '%s' received\n", command); |
| 506 | orsout = fopen(ORS_OUTPUT_FILE, "w"); |
| 507 | if (!orsout) { |
| 508 | close(ors_read_fd); |
| 509 | ors_read_fd = -1; |
| 510 | LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE); |
| 511 | unlink(ORS_INPUT_FILE); |
| 512 | unlink(ORS_OUTPUT_FILE); |
| 513 | return; |
| 514 | } |
| 515 | if (DataManager::GetIntValue("tw_busy") != 0) { |
| 516 | strcpy(result, "Failed, operation in progress\n"); |
| 517 | fprintf(orsout, "%s", result); |
| 518 | LOGINFO("Command cannot be performed, operation in progress.\n"); |
| 519 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 520 | if (strlen(command) == 11 && strncmp(command, "dumpstrings", 11) == 0) { |
| 521 | // This cannot be done safely with gui_console_only because gui_console_only updates mCurrentSet |
| 522 | // which makes the resources that we are trying to read unreachable. |
| 523 | gui_set_FILE(orsout); |
| 524 | PageManager::GetResources()->DumpStrings(); |
| 525 | gui_set_FILE(NULL); |
| 526 | } else if (gui_console_only() == 0) { |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 527 | LOGINFO("Console started successfully\n"); |
| 528 | gui_set_FILE(orsout); |
| 529 | if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) { |
| 530 | char* filename = command + 11; |
| 531 | if (OpenRecoveryScript::copy_script_file(filename) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 532 | LOGINFO("Unable to copy script file\n"); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 533 | } else { |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 534 | OpenRecoveryScript::run_script_file(); |
| 535 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 536 | } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) { |
| 537 | char* varname = command + 4; |
| 538 | string temp; |
| 539 | DataManager::GetValue(varname, temp); |
| 540 | gui_print("%s = %s\n", varname, temp.c_str()); |
| 541 | } else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) { |
| 542 | char* pass = command + 8; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 543 | gui_msg("decrypt_cmd=Attempting to decrypt data partition via command line."); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 544 | if (PartitionManager.Decrypt_Device(pass) == 0) { |
| 545 | set_page_done = 1; |
| 546 | } |
| 547 | } else if (OpenRecoveryScript::Insert_ORS_Command(command)) { |
| 548 | OpenRecoveryScript::run_script_file(); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 549 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 550 | gui_set_FILE(NULL); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 551 | gGuiConsoleTerminate.set_value(1); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 552 | } |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 553 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 554 | fclose(orsout); |
| 555 | LOGINFO("Done reading ORS command from command line\n"); |
| 556 | if (set_page_done) { |
| 557 | DataManager::SetValue("tw_page_done", 1); |
| 558 | } else { |
| 559 | // The select function will return ready to read and the |
| 560 | // read function will return errno 19 no such device unless |
| 561 | // we set everything up all over again. |
| 562 | close(ors_read_fd); |
| 563 | setup_ors_command(); |
| 564 | } |
| 565 | } else { |
| 566 | 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] | 567 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 568 | return; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 571 | // This special function will return immediately the first time, but then |
| 572 | // always returns 1/30th of a second (or immediately if called later) from |
| 573 | // the last time it was called |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 574 | static void loopTimer(int input_timeout_ms) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 575 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 576 | static timespec lastCall; |
| 577 | static int initialized = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 578 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 579 | if (!initialized) |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 580 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 581 | clock_gettime(CLOCK_MONOTONIC, &lastCall); |
| 582 | initialized = 1; |
| 583 | return; |
Dees_Troy | c8b199c | 2012-09-24 11:55:07 -0400 | [diff] [blame] | 584 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 585 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 586 | do |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 587 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 588 | 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] | 589 | timespec curTime; |
| 590 | clock_gettime(CLOCK_MONOTONIC, &curTime); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 591 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 592 | timespec diff = TWFunc::timespec_diff(lastCall, curTime); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 593 | |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 594 | // This is really 2 or 30 times per second |
| 595 | // As long as we get events, increase the timeout so we can catch up with input |
| 596 | long timeout = got_event ? 500000000 : 33333333; |
| 597 | |
| 598 | if (diff.tv_sec || diff.tv_nsec > timeout) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 599 | { |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 600 | // int32_t input_time = TWFunc::timespec_diff_ms(lastCall, curTime); |
| 601 | // LOGINFO("loopTimer(): %u ms, count: %u\n", input_time, count); |
| 602 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 603 | lastCall = curTime; |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 604 | input_handler.handleDrag(); // send only drag notices if needed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 605 | return; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 606 | } |
| 607 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 608 | // We need to sleep some period time microseconds |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 609 | //unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000); |
| 610 | //usleep(sleepTime); // removed so we can scan for input |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 611 | input_timeout_ms = 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 612 | } while (1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 613 | } |
| 614 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 615 | 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] | 616 | { |
Ethan Yonker | 3f15be4 | 2015-02-09 09:39:47 -0600 | [diff] [blame] | 617 | DataManager::SetValue("tw_page_done", 0); |
| 618 | DataManager::SetValue("tw_gui_done", 0); |
| 619 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 620 | if (page_name) |
| 621 | gui_changePage(page_name); |
| 622 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 623 | // Raise the curtain |
| 624 | if (gCurtain != NULL) |
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 | gr_surface surface; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 627 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 628 | PageManager::Render(); |
| 629 | gr_get_surface(&surface); |
| 630 | curtainRaise(surface); |
| 631 | gr_free_surface(surface); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 632 | } |
| 633 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 634 | gGuiRunning = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 635 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 636 | DataManager::SetValue("tw_loaded", 1); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 637 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 638 | #ifndef TW_OEM_BUILD |
| 639 | struct timeval timeout; |
| 640 | fd_set fdset; |
| 641 | int has_data = 0; |
| 642 | #endif |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 643 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 644 | int input_timeout_ms = 0; |
| 645 | int idle_frames = 0; |
| 646 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 647 | for (;;) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 648 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 649 | loopTimer(input_timeout_ms); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 650 | #ifndef TW_OEM_BUILD |
| 651 | if (ors_read_fd > 0) { |
| 652 | FD_ZERO(&fdset); |
| 653 | FD_SET(ors_read_fd, &fdset); |
| 654 | timeout.tv_sec = 0; |
| 655 | timeout.tv_usec = 1; |
| 656 | has_data = select(ors_read_fd+1, &fdset, NULL, NULL, &timeout); |
| 657 | if (has_data > 0) { |
| 658 | ors_command_read(); |
| 659 | } |
| 660 | } |
| 661 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 662 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 663 | if (gGuiConsoleRunning.get_value()) { |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 664 | continue; |
| 665 | } |
| 666 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 667 | if (!gForceRender.get_value()) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 668 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 669 | int ret = PageManager::Update(); |
| 670 | if (ret == 0) |
| 671 | ++idle_frames; |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 672 | else if (ret == -2) |
| 673 | break; // Theme reload failure |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 674 | else |
| 675 | idle_frames = 0; |
| 676 | // due to possible animation objects, we need to delay activating the input timeout |
| 677 | input_timeout_ms = idle_frames > 15 ? 1000 : 0; |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 678 | |
| 679 | #ifndef PRINT_RENDER_TIME |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 680 | if (ret > 1) |
| 681 | PageManager::Render(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 682 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 683 | if (ret > 0) |
| 684 | flip(); |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 685 | #else |
| 686 | if (ret > 1) |
| 687 | { |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 688 | timespec start, end; |
| 689 | int32_t render_t, flip_t; |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 690 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 691 | PageManager::Render(); |
| 692 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 693 | render_t = TWFunc::timespec_diff_ms(start, end); |
| 694 | |
| 695 | flip(); |
| 696 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 697 | flip_t = TWFunc::timespec_diff_ms(end, start); |
| 698 | |
| 699 | LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t); |
| 700 | } |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 701 | else if (ret > 0) |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 702 | flip(); |
| 703 | #endif |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 704 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 705 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 706 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 707 | gForceRender.set_value(0); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 708 | PageManager::Render(); |
| 709 | flip(); |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 710 | input_timeout_ms = 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 711 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 712 | |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 713 | blankTimer.checkForTimeout(); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 714 | if (stop_on_page_done && DataManager::GetIntValue("tw_page_done") != 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 715 | { |
| 716 | gui_changePage("main"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 717 | break; |
| 718 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 719 | if (DataManager::GetIntValue("tw_gui_done") != 0) |
| 720 | break; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 721 | } |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 722 | if (ors_read_fd > 0) |
| 723 | close(ors_read_fd); |
| 724 | ors_read_fd = -1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 725 | gGuiRunning = 0; |
| 726 | return 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 729 | int gui_forceRender(void) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 730 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 731 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 732 | return 0; |
| 733 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 734 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 735 | int gui_changePage(std::string newPage) |
| 736 | { |
| 737 | LOGINFO("Set page: '%s'\n", newPage.c_str()); |
| 738 | PageManager::ChangePage(newPage); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 739 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | int gui_changeOverlay(std::string overlay) |
| 744 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 745 | LOGINFO("Set overlay: '%s'\n", overlay.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 746 | PageManager::ChangeOverlay(overlay); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 747 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | int gui_changePackage(std::string newPackage) |
| 752 | { |
| 753 | PageManager::SelectPackage(newPackage); |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 754 | gForceRender.set_value(1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 755 | return 0; |
| 756 | } |
| 757 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 758 | std::string gui_parse_text(std::string str) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 759 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 760 | // This function parses text for DataManager values encompassed by %value% in the XML |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 761 | // and string resources (%@resource_name%) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 762 | size_t pos = 0, next, end; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 763 | |
| 764 | while (1) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 765 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 766 | next = str.find("{@", pos); |
| 767 | if (next == std::string::npos) |
| 768 | break; |
| 769 | |
| 770 | end = str.find('}', next + 1); |
| 771 | if (end == std::string::npos) |
| 772 | break; |
| 773 | |
| 774 | std::string var = str.substr(next + 2, (end - next) - 2); |
| 775 | str.erase(next, (end - next) + 1); |
| 776 | |
| 777 | size_t default_loc = var.find('=', 0); |
| 778 | std::string lookup; |
| 779 | if (default_loc == std::string::npos) { |
| 780 | str.insert(next, PageManager::GetResources()->FindString(var)); |
| 781 | } else { |
| 782 | lookup = var.substr(0, default_loc); |
| 783 | std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1); |
| 784 | str.insert(next, PageManager::GetResources()->FindString(lookup, default_string)); |
| 785 | } |
| 786 | } |
| 787 | pos = 0; |
| 788 | while (1) |
| 789 | { |
| 790 | next = str.find('%', pos); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 791 | if (next == std::string::npos) |
| 792 | return str; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 793 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 794 | end = str.find('%', next + 1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 795 | if (end == std::string::npos) |
| 796 | return str; |
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 | // We have a block of data |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 799 | std::string var = str.substr(next + 1, (end - next) - 1); |
| 800 | str.erase(next, (end - next) + 1); |
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 | if (next + 1 == end) |
| 803 | str.insert(next, 1, '%'); |
| 804 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 805 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 806 | std::string value; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 807 | if (var.size() > 0 && var[0] == '@') { |
| 808 | // this is a string resource ("%@string_name%") |
| 809 | value = PageManager::GetResources()->FindString(var.substr(1)); |
| 810 | str.insert(next, value); |
| 811 | } |
| 812 | else if (DataManager::GetValue(var, value) == 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 813 | str.insert(next, value); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 814 | } |
| 815 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 816 | pos = next + 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 820 | std::string gui_lookup(const std::string& resource_name, const std::string& default_value) { |
| 821 | return PageManager::GetResources()->FindString(resource_name, default_value); |
| 822 | } |
| 823 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 824 | extern "C" int gui_init(void) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 825 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 826 | gr_init(); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 827 | std::string curtain_path = TWRES "images/curtain.jpg"; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 828 | gr_surface source_Surface = NULL; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 829 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 830 | if (res_create_surface(curtain_path.c_str(), &source_Surface)) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 831 | { |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 832 | 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] | 833 | return -1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 834 | } |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 835 | 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] | 836 | // We need to scale the curtain to fit the screen |
| 837 | float scale_w = (float)gr_fb_width() / (float)gr_get_width(source_Surface); |
| 838 | float scale_h = (float)gr_fb_height() / (float)gr_get_height(source_Surface); |
| 839 | if (res_scale_surface(source_Surface, &gCurtain, scale_w, scale_h)) { |
| 840 | LOGINFO("Failed to scale curtain\n"); |
| 841 | gCurtain = source_Surface; |
| 842 | } else { |
| 843 | LOGINFO("Scaling the curtain width %fx and height %fx\n", scale_w, scale_h); |
| 844 | } |
| 845 | } else { |
| 846 | gCurtain = source_Surface; |
| 847 | } |
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 | curtainSet(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 850 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 851 | ev_init(); |
| 852 | return 0; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 853 | } |
| 854 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 855 | extern "C" int gui_loadResources(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 856 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 857 | #ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 858 | int check = 0; |
| 859 | DataManager::GetValue(TW_IS_ENCRYPTED, check); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 860 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 861 | if (check) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 862 | { |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 863 | if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt")) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 864 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 865 | gui_err("base_pkg_err=Failed to load base packages."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 866 | goto error; |
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 | else |
| 869 | check = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 870 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 871 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 872 | if (check == 0 && PageManager::LoadPackage("TWRP", "/script/ui.xml", "main")) |
| 873 | { |
| 874 | std::string theme_path; |
| 875 | |
| 876 | theme_path = DataManager::GetSettingsStoragePath(); |
| 877 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 878 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 879 | int retry_count = 5; |
| 880 | while (retry_count > 0 && !PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 881 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 882 | usleep(500000); |
| 883 | retry_count--; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 884 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 885 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 886 | if (!PartitionManager.Mount_Settings_Storage(true)) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 887 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 888 | 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] | 889 | check = 1; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 893 | theme_path += "/TWRP/theme/ui.zip"; |
| 894 | if (check || PageManager::LoadPackage("TWRP", theme_path, "main")) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 895 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 896 | #endif // ifndef TW_OEM_BUILD |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 897 | if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main")) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 898 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 899 | gui_err("base_pkg_err=Failed to load base packages."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 900 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 901 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 902 | #ifndef TW_OEM_BUILD |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 903 | } |
| 904 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 905 | #endif // ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 906 | // Set the default package |
| 907 | PageManager::SelectPackage("TWRP"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 908 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 909 | gGuiInitialized = 1; |
| 910 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 911 | |
| 912 | error: |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 913 | LOGERR("An internal error has occurred: unable to load theme.\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 914 | gGuiInitialized = 0; |
| 915 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 916 | } |
| 917 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 918 | extern "C" int gui_loadCustomResources(void) |
| 919 | { |
| 920 | #ifndef TW_OEM_BUILD |
| 921 | if (!PartitionManager.Mount_Settings_Storage(false)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 922 | LOGINFO("Unable to mount settings storage during GUI startup.\n"); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 923 | return -1; |
| 924 | } |
| 925 | |
| 926 | std::string theme_path = DataManager::GetSettingsStoragePath(); |
| 927 | theme_path += "/TWRP/theme/ui.zip"; |
| 928 | // Check for a custom theme |
| 929 | if (TWFunc::Path_Exists(theme_path)) { |
| 930 | // There is a custom theme, try to load it |
| 931 | if (PageManager::ReloadPackage("TWRP", theme_path)) { |
| 932 | // Custom theme failed to load, try to load stock theme |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 933 | if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame^] | 934 | gui_err("base_pkg_err=Failed to load base packages."); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 935 | goto error; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | // Set the default package |
| 940 | PageManager::SelectPackage("TWRP"); |
| 941 | #endif |
| 942 | return 0; |
| 943 | |
| 944 | error: |
| 945 | LOGERR("An internal error has occurred: unable to load theme.\n"); |
| 946 | gGuiInitialized = 0; |
| 947 | return -1; |
| 948 | } |
| 949 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 950 | extern "C" int gui_start(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 951 | { |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 952 | return gui_startPage(NULL, 1, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 953 | } |
| 954 | |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 955 | 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] | 956 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 957 | if (!gGuiInitialized) |
| 958 | return -1; |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 959 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 960 | gGuiConsoleTerminate.set_value(1); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 961 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 962 | while (gGuiConsoleRunning.get_value()) |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 963 | usleep(10000); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 964 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 965 | // Set the default package |
| 966 | PageManager::SelectPackage("TWRP"); |
| 967 | |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 968 | input_handler.init(); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 969 | #ifndef TW_OEM_BUILD |
| 970 | if (allow_commands) |
| 971 | { |
| 972 | if (ors_read_fd < 0) |
| 973 | setup_ors_command(); |
| 974 | } else { |
| 975 | if (ors_read_fd >= 0) { |
| 976 | close(ors_read_fd); |
| 977 | ors_read_fd = -1; |
| 978 | } |
| 979 | } |
| 980 | #endif |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 981 | return runPages(page_name, stop_on_page_done); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 984 | static void * console_thread(void *cookie __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 985 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 986 | PageManager::SwitchToConsole(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 987 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 988 | while (!gGuiConsoleTerminate.get_value()) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 989 | { |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 990 | loopTimer(0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 991 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 992 | if (!gForceRender.get_value()) |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 993 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 994 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 995 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 996 | ret = PageManager::Update(); |
| 997 | if (ret > 1) |
| 998 | PageManager::Render(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 999 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1000 | if (ret > 0) |
| 1001 | flip(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1002 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1003 | if (ret < 0) |
| 1004 | LOGERR("An update request has failed.\n"); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 1005 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1006 | else |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 1007 | { |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 1008 | gForceRender.set_value(0); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1009 | PageManager::Render(); |
| 1010 | flip(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 1011 | } |
| 1012 | } |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 1013 | gGuiConsoleRunning.set_value(0); |
| 1014 | gForceRender.set_value(1); // this will kickstart the GUI to render again |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 1015 | PageManager::EndConsole(); |
| 1016 | LOGINFO("Console stopping\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1017 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1020 | extern "C" int gui_console_only(void) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1021 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1022 | if (!gGuiInitialized) |
| 1023 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1024 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 1025 | gGuiConsoleTerminate.set_value(0); |
Ethan Yonker | ffbd6ff | 2014-10-22 10:40:40 -0500 | [diff] [blame] | 1026 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 1027 | if (gGuiConsoleRunning.get_value()) |
Ethan Yonker | ffbd6ff | 2014-10-22 10:40:40 -0500 | [diff] [blame] | 1028 | return 0; |
| 1029 | |
Ethan Yonker | 0453695 | 2015-01-27 08:41:28 -0600 | [diff] [blame] | 1030 | gGuiConsoleRunning.set_value(1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1031 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1032 | // Start by spinning off an input handler. |
| 1033 | pthread_t t; |
| 1034 | pthread_create(&t, NULL, console_thread, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1035 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1036 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1037 | } |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 1038 | |
| 1039 | extern "C" void set_scale_values(float w, float h) |
| 1040 | { |
| 1041 | scale_theme_w = w; |
| 1042 | scale_theme_h = h; |
| 1043 | } |
| 1044 | |
| 1045 | extern "C" int scale_theme_x(int initial_x) |
| 1046 | { |
| 1047 | if (scale_theme_w != 1) { |
that | ba75a0e | 2015-02-14 21:20:10 +0100 | [diff] [blame] | 1048 | int scaled = (float)initial_x * scale_theme_w; |
| 1049 | if (scaled == 0 && initial_x > 0) |
| 1050 | return 1; |
| 1051 | return scaled; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 1052 | } |
| 1053 | return initial_x; |
| 1054 | } |
| 1055 | |
| 1056 | extern "C" int scale_theme_y(int initial_y) |
| 1057 | { |
| 1058 | if (scale_theme_h != 1) { |
that | ba75a0e | 2015-02-14 21:20:10 +0100 | [diff] [blame] | 1059 | int scaled = (float)initial_y * scale_theme_h; |
| 1060 | if (scaled == 0 && initial_y > 0) |
| 1061 | return 1; |
| 1062 | return scaled; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 1063 | } |
| 1064 | return initial_y; |
| 1065 | } |
| 1066 | |
| 1067 | extern "C" int scale_theme_min(int initial_value) |
| 1068 | { |
| 1069 | if (scale_theme_w != 1 || scale_theme_h != 1) { |
| 1070 | if (scale_theme_w < scale_theme_h) |
| 1071 | return scale_theme_x(initial_value); |
| 1072 | else |
| 1073 | return scale_theme_y(initial_value); |
| 1074 | } |
| 1075 | return initial_value; |
| 1076 | } |
| 1077 | |
| 1078 | extern "C" float get_scale_w() |
| 1079 | { |
| 1080 | return scale_theme_w; |
| 1081 | } |
| 1082 | |
| 1083 | extern "C" float get_scale_h() |
| 1084 | { |
| 1085 | return scale_theme_h; |
| 1086 | } |