Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2013 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 | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 18 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 19 | // pages.cpp - Source to manage GUI base objects |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/reboot.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 35 | #include <dirent.h> |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 36 | #include "../twrp-functions.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 37 | #include "../partitions.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | |
| 39 | #include <string> |
Xuefer | cac6ace | 2016-02-01 02:28:55 +0800 | [diff] [blame] | 40 | #include <algorithm> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
| 42 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 43 | #include "../twcommon.h" |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 44 | #include "../minzip/SysUtil.h" |
| 45 | #include "../minzip/Zip.h" |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 46 | #include "gui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 48 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | |
| 50 | #include "rapidxml.hpp" |
| 51 | #include "objects.hpp" |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 52 | #include "blanktimer.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 54 | #define TW_THEME_VERSION 1 |
Ethan Yonker | 8e5692f | 2016-01-21 11:21:06 -0600 | [diff] [blame] | 55 | #define TW_THEME_VER_ERR -2 |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 56 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | extern int gGuiRunning; |
| 58 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 59 | // From console.cpp |
| 60 | extern size_t last_message_count; |
| 61 | extern std::vector<std::string> gConsole; |
| 62 | extern std::vector<std::string> gConsoleColor; |
| 63 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | std::map<std::string, PageSet*> PageManager::mPageSets; |
| 65 | PageSet* PageManager::mCurrentSet; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 66 | MouseCursor *PageManager::mMouseCursor = NULL; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 67 | HardwareKeyboard *PageManager::mHardwareKeyboard = NULL; |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 68 | bool PageManager::mReloadTheme = false; |
| 69 | std::string PageManager::mStartPage = "main"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 70 | std::vector<language_struct> Language_List; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 71 | |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 72 | int tw_x_offset = 0; |
| 73 | int tw_y_offset = 0; |
| 74 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | // Helper routine to convert a string to a color declaration |
| 76 | int ConvertStrToColor(std::string str, COLOR* color) |
| 77 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 78 | // Set the default, solid black |
| 79 | memset(color, 0, sizeof(COLOR)); |
| 80 | color->alpha = 255; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 82 | // Translate variables |
| 83 | DataManager::GetValue(str, str); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 84 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | // Look for some defaults |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 86 | if (str == "black") return 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 87 | else if (str == "white") { color->red = color->green = color->blue = 255; return 0; } |
| 88 | else if (str == "red") { color->red = 255; return 0; } |
| 89 | else if (str == "green") { color->green = 255; return 0; } |
| 90 | else if (str == "blue") { color->blue = 255; return 0; } |
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 | // At this point, we require an RGB(A) color |
| 93 | if (str[0] != '#') |
| 94 | return -1; |
| 95 | |
| 96 | str.erase(0, 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 97 | |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 98 | int result; |
| 99 | if (str.size() >= 8) { |
| 100 | // We have alpha channel |
| 101 | string alpha = str.substr(6, 2); |
| 102 | result = strtol(alpha.c_str(), NULL, 16); |
| 103 | color->alpha = result & 0x000000FF; |
| 104 | str.resize(6); |
| 105 | result = strtol(str.c_str(), NULL, 16); |
| 106 | color->red = (result >> 16) & 0x000000FF; |
| 107 | color->green = (result >> 8) & 0x000000FF; |
| 108 | color->blue = result & 0x000000FF; |
| 109 | } else { |
| 110 | result = strtol(str.c_str(), NULL, 16); |
| 111 | color->red = (result >> 16) & 0x000000FF; |
| 112 | color->green = (result >> 8) & 0x000000FF; |
| 113 | color->blue = result & 0x000000FF; |
| 114 | } |
| 115 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Helper APIs |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 119 | xml_node<>* FindNode(xml_node<>* parent, const char* nodename, int depth /* = 0 */) |
| 120 | { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 121 | if (!parent) |
| 122 | return NULL; |
| 123 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 124 | xml_node<>* child = parent->first_node(nodename); |
| 125 | if (child) |
| 126 | return child; |
| 127 | |
| 128 | if (depth == 10) { |
| 129 | LOGERR("Too many style loops detected.\n"); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | xml_node<>* style = parent->first_node("style"); |
| 134 | if (style) { |
| 135 | while (style) { |
| 136 | if (!style->first_attribute("name")) { |
| 137 | LOGERR("No name given for style.\n"); |
| 138 | continue; |
| 139 | } else { |
| 140 | std::string name = style->first_attribute("name")->value(); |
| 141 | xml_node<>* node = PageManager::FindStyle(name); |
| 142 | |
| 143 | if (node) { |
| 144 | // We found the style that was named |
| 145 | xml_node<>* stylenode = FindNode(node, nodename, depth + 1); |
| 146 | if (stylenode) |
| 147 | return stylenode; |
| 148 | } |
| 149 | } |
| 150 | style = style->next_sibling("style"); |
| 151 | } |
| 152 | } else { |
that | 54e9c83 | 2015-11-04 21:46:01 +0100 | [diff] [blame] | 153 | // Search for stylename in the parent node <object type="foo" style="foo2"> |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 154 | xml_attribute<>* attr = parent->first_attribute("style"); |
| 155 | // If no style is found anywhere else and the node wasn't found in the object itself |
| 156 | // as a special case we will search for a style that uses the same style name as the |
| 157 | // object type, so <object type="button"> would search for a style named "button" |
| 158 | if (!attr) |
| 159 | attr = parent->first_attribute("type"); |
that | 54e9c83 | 2015-11-04 21:46:01 +0100 | [diff] [blame] | 160 | // if there's no attribute type, the object type must be the element name |
| 161 | std::string stylename = attr ? attr->value() : parent->name(); |
| 162 | xml_node<>* node = PageManager::FindStyle(stylename); |
| 163 | if (node) { |
| 164 | xml_node<>* stylenode = FindNode(node, nodename, depth + 1); |
| 165 | if (stylenode) |
| 166 | return stylenode; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 172 | std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue) |
| 173 | { |
| 174 | if (!element) |
| 175 | return defaultvalue; |
| 176 | |
| 177 | xml_attribute<>* attr = element->first_attribute(attrname); |
| 178 | return attr ? attr->value() : defaultvalue; |
| 179 | } |
| 180 | |
| 181 | int LoadAttrInt(xml_node<>* element, const char* attrname, int defaultvalue) |
| 182 | { |
| 183 | string value = LoadAttrString(element, attrname); |
| 184 | // resolve variables |
| 185 | DataManager::GetValue(value, value); |
| 186 | return value.empty() ? defaultvalue : atoi(value.c_str()); |
| 187 | } |
| 188 | |
| 189 | int LoadAttrIntScaleX(xml_node<>* element, const char* attrname, int defaultvalue) |
| 190 | { |
| 191 | return scale_theme_x(LoadAttrInt(element, attrname, defaultvalue)); |
| 192 | } |
| 193 | |
| 194 | int LoadAttrIntScaleY(xml_node<>* element, const char* attrname, int defaultvalue) |
| 195 | { |
| 196 | return scale_theme_y(LoadAttrInt(element, attrname, defaultvalue)); |
| 197 | } |
| 198 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 199 | COLOR LoadAttrColor(xml_node<>* element, const char* attrname, bool* found_color, COLOR defaultvalue) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 200 | { |
| 201 | string value = LoadAttrString(element, attrname); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 202 | *found_color = !value.empty(); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 203 | // resolve variables |
| 204 | DataManager::GetValue(value, value); |
| 205 | COLOR ret = defaultvalue; |
| 206 | if (ConvertStrToColor(value, &ret) == 0) |
| 207 | return ret; |
| 208 | else |
| 209 | return defaultvalue; |
| 210 | } |
| 211 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 212 | COLOR LoadAttrColor(xml_node<>* element, const char* attrname, COLOR defaultvalue) |
| 213 | { |
| 214 | bool found_color = false; |
| 215 | return LoadAttrColor(element, attrname, &found_color, defaultvalue); |
| 216 | } |
| 217 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 218 | FontResource* LoadAttrFont(xml_node<>* element, const char* attrname) |
| 219 | { |
| 220 | std::string name = LoadAttrString(element, attrname, ""); |
| 221 | if (name.empty()) |
| 222 | return NULL; |
| 223 | else |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 224 | return PageManager::GetResources()->FindFont(name); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | ImageResource* LoadAttrImage(xml_node<>* element, const char* attrname) |
| 228 | { |
| 229 | std::string name = LoadAttrString(element, attrname, ""); |
| 230 | if (name.empty()) |
| 231 | return NULL; |
| 232 | else |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 233 | return PageManager::GetResources()->FindImage(name); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | AnimationResource* LoadAttrAnimation(xml_node<>* element, const char* attrname) |
| 237 | { |
| 238 | std::string name = LoadAttrString(element, attrname, ""); |
| 239 | if (name.empty()) |
| 240 | return NULL; |
| 241 | else |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 242 | return PageManager::GetResources()->FindAnimation(name); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 245 | bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, Placement* placement /* = NULL */) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 246 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 247 | if (!node) |
| 248 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 249 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 250 | if (node->first_attribute("x")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 251 | *x = LoadAttrIntScaleX(node, "x") + tw_x_offset; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 252 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 253 | if (node->first_attribute("y")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 254 | *y = LoadAttrIntScaleY(node, "y") + tw_y_offset; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 255 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 256 | if (w && node->first_attribute("w")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 257 | *w = LoadAttrIntScaleX(node, "w"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 258 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 259 | if (h && node->first_attribute("h")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 260 | *h = LoadAttrIntScaleY(node, "h"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 261 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 262 | if (placement && node->first_attribute("placement")) |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 263 | *placement = (Placement) LoadAttrInt(node, "placement"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 264 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 265 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int ActionObject::SetActionPos(int x, int y, int w, int h) |
| 269 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 270 | if (x < 0 || y < 0) |
| 271 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 272 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 273 | mActionX = x; |
| 274 | mActionY = y; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 275 | if (w || h) |
| 276 | { |
| 277 | mActionW = w; |
| 278 | mActionH = h; |
| 279 | } |
| 280 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 281 | } |
| 282 | |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 283 | Page::Page(xml_node<>* page, std::vector<xml_node<>*> *templates) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 284 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 285 | mTouchStart = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 287 | // We can memset the whole structure, because the alpha channel is ignored |
| 288 | memset(&mBackground, 0, sizeof(COLOR)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 289 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 290 | // With NULL, we make a console-only display |
| 291 | if (!page) |
| 292 | { |
| 293 | mName = "console"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 294 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 295 | GUIConsole* element = new GUIConsole(NULL); |
| 296 | mRenders.push_back(element); |
| 297 | mActions.push_back(element); |
| 298 | return; |
| 299 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 300 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 301 | if (page->first_attribute("name")) |
| 302 | mName = page->first_attribute("name")->value(); |
| 303 | else |
| 304 | { |
| 305 | LOGERR("No page name attribute found!\n"); |
| 306 | return; |
| 307 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 308 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 309 | LOGINFO("Loading page %s\n", mName.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 310 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 311 | // This is a recursive routine for template handling |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 312 | ProcessNode(page, templates, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 315 | Page::~Page() |
| 316 | { |
| 317 | for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr) |
| 318 | delete *itr; |
| 319 | } |
| 320 | |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 321 | bool Page::ProcessNode(xml_node<>* page, std::vector<xml_node<>*> *templates, int depth) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 322 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 323 | if (depth == 10) |
| 324 | { |
| 325 | LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n"); |
| 326 | return false; |
| 327 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 328 | |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 329 | for (xml_node<>* child = page->first_node(); child; child = child->next_sibling()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 330 | { |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 331 | std::string type = child->name(); |
| 332 | |
| 333 | if (type == "background") { |
| 334 | mBackground = LoadAttrColor(child, "color", COLOR(0,0,0,0)); |
| 335 | continue; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 336 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 337 | |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 338 | if (type == "object") { |
| 339 | // legacy format : <object type="..."> |
| 340 | xml_attribute<>* attr = child->first_attribute("type"); |
| 341 | type = attr ? attr->value() : "*unspecified*"; |
| 342 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 343 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 344 | if (type == "text") |
| 345 | { |
| 346 | GUIText* element = new GUIText(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 347 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 348 | mRenders.push_back(element); |
| 349 | mActions.push_back(element); |
| 350 | } |
| 351 | else if (type == "image") |
| 352 | { |
| 353 | GUIImage* element = new GUIImage(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 354 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 355 | mRenders.push_back(element); |
| 356 | } |
| 357 | else if (type == "fill") |
| 358 | { |
| 359 | GUIFill* element = new GUIFill(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 360 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 361 | mRenders.push_back(element); |
| 362 | } |
| 363 | else if (type == "action") |
| 364 | { |
| 365 | GUIAction* element = new GUIAction(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 366 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 367 | mActions.push_back(element); |
| 368 | } |
| 369 | else if (type == "console") |
| 370 | { |
| 371 | GUIConsole* element = new GUIConsole(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 372 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 373 | mRenders.push_back(element); |
| 374 | mActions.push_back(element); |
| 375 | } |
that | 1964d19 | 2016-01-07 00:41:03 +0100 | [diff] [blame] | 376 | else if (type == "terminal") |
| 377 | { |
| 378 | GUITerminal* element = new GUITerminal(child); |
| 379 | mObjects.push_back(element); |
| 380 | mRenders.push_back(element); |
| 381 | mActions.push_back(element); |
| 382 | mInputs.push_back(element); |
| 383 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 384 | else if (type == "button") |
| 385 | { |
| 386 | GUIButton* element = new GUIButton(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 387 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 388 | mRenders.push_back(element); |
| 389 | mActions.push_back(element); |
| 390 | } |
| 391 | else if (type == "checkbox") |
| 392 | { |
| 393 | GUICheckbox* element = new GUICheckbox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 394 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 395 | mRenders.push_back(element); |
| 396 | mActions.push_back(element); |
| 397 | } |
| 398 | else if (type == "fileselector") |
| 399 | { |
| 400 | GUIFileSelector* element = new GUIFileSelector(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 401 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 402 | mRenders.push_back(element); |
| 403 | mActions.push_back(element); |
| 404 | } |
| 405 | else if (type == "animation") |
| 406 | { |
| 407 | GUIAnimation* element = new GUIAnimation(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 408 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 409 | mRenders.push_back(element); |
| 410 | } |
| 411 | else if (type == "progressbar") |
| 412 | { |
| 413 | GUIProgressBar* element = new GUIProgressBar(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 414 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 415 | mRenders.push_back(element); |
| 416 | mActions.push_back(element); |
| 417 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 418 | else if (type == "slider") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 419 | { |
| 420 | GUISlider* element = new GUISlider(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 421 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 422 | mRenders.push_back(element); |
| 423 | mActions.push_back(element); |
| 424 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 425 | else if (type == "slidervalue") |
| 426 | { |
| 427 | GUISliderValue *element = new GUISliderValue(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 428 | mObjects.push_back(element); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 429 | mRenders.push_back(element); |
| 430 | mActions.push_back(element); |
| 431 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 432 | else if (type == "listbox") |
| 433 | { |
| 434 | GUIListBox* element = new GUIListBox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 435 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 436 | mRenders.push_back(element); |
| 437 | mActions.push_back(element); |
| 438 | } |
| 439 | else if (type == "keyboard") |
| 440 | { |
| 441 | GUIKeyboard* element = new GUIKeyboard(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 442 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 443 | mRenders.push_back(element); |
| 444 | mActions.push_back(element); |
| 445 | } |
| 446 | else if (type == "input") |
| 447 | { |
| 448 | GUIInput* element = new GUIInput(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 449 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 450 | mRenders.push_back(element); |
| 451 | mActions.push_back(element); |
| 452 | mInputs.push_back(element); |
| 453 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 454 | else if (type == "partitionlist") |
| 455 | { |
| 456 | GUIPartitionList* element = new GUIPartitionList(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 457 | mObjects.push_back(element); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 458 | mRenders.push_back(element); |
| 459 | mActions.push_back(element); |
| 460 | } |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 461 | else if (type == "patternpassword") |
| 462 | { |
| 463 | GUIPatternPassword* element = new GUIPatternPassword(child); |
| 464 | mObjects.push_back(element); |
| 465 | mRenders.push_back(element); |
| 466 | mActions.push_back(element); |
| 467 | } |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 468 | else if (type == "textbox") |
| 469 | { |
| 470 | GUITextBox* element = new GUITextBox(child); |
| 471 | mObjects.push_back(element); |
| 472 | mRenders.push_back(element); |
| 473 | mActions.push_back(element); |
| 474 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 475 | else if (type == "template") |
| 476 | { |
| 477 | if (!templates || !child->first_attribute("name")) |
| 478 | { |
| 479 | LOGERR("Invalid template request.\n"); |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | std::string name = child->first_attribute("name")->value(); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 484 | xml_node<>* node; |
| 485 | bool node_found = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 486 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 487 | // We need to find the correct template |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 488 | for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) { |
| 489 | node = (*itr)->first_node("template"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 490 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 491 | while (node) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 492 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 493 | if (!node->first_attribute("name")) |
| 494 | continue; |
| 495 | |
| 496 | if (name == node->first_attribute("name")->value()) |
| 497 | { |
| 498 | if (!ProcessNode(node, templates, depth + 1)) |
| 499 | return false; |
| 500 | else { |
| 501 | node_found = true; |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | if (node_found) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 506 | break; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 507 | node = node->next_sibling("template"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 508 | } |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 509 | // [check] why is there no if (node_found) here too? |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 515 | LOGERR("Unknown object type: %s.\n", type.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 516 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 517 | } |
| 518 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | int Page::Render(void) |
| 522 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 523 | // Render background |
| 524 | gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha); |
| 525 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 526 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 527 | // Render remaining objects |
| 528 | std::vector<RenderObject*>::iterator iter; |
| 529 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 530 | { |
| 531 | if ((*iter)->Render()) |
| 532 | LOGERR("A render request has failed.\n"); |
| 533 | } |
| 534 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | int Page::Update(void) |
| 538 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 539 | int retCode = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 540 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 541 | std::vector<RenderObject*>::iterator iter; |
| 542 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 543 | { |
| 544 | int ret = (*iter)->Update(); |
| 545 | if (ret < 0) |
| 546 | LOGERR("An update request has failed.\n"); |
| 547 | else if (ret > retCode) |
| 548 | retCode = ret; |
| 549 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 550 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 551 | return retCode; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | int Page::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 555 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 556 | // By default, return 1 to ignore further touches if nobody is listening |
| 557 | int ret = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 558 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 559 | // Don't try to handle a lack of handlers |
| 560 | if (mActions.size() == 0) |
| 561 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 562 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 563 | // We record mTouchStart so we can pass all the touch stream to the same handler |
| 564 | if (state == TOUCH_START) |
| 565 | { |
| 566 | std::vector<ActionObject*>::reverse_iterator iter; |
| 567 | // We work backwards, from top-most element to bottom-most element |
| 568 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 569 | { |
| 570 | if ((*iter)->IsInRegion(x, y)) |
| 571 | { |
| 572 | mTouchStart = (*iter); |
| 573 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 574 | if (ret >= 0) |
| 575 | break; |
| 576 | mTouchStart = NULL; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | else if (state == TOUCH_RELEASE && mTouchStart != NULL) |
| 581 | { |
| 582 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 583 | mTouchStart = NULL; |
| 584 | } |
| 585 | else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL) |
| 586 | { |
| 587 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 588 | } |
| 589 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 592 | int Page::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 593 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 594 | std::vector<ActionObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 595 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 596 | int ret = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 597 | // We work backwards, from top-most element to bottom-most element |
| 598 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 599 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 600 | ret = (*iter)->NotifyKey(key, down); |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 601 | if (ret == 0) |
| 602 | return 0; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 603 | if (ret < 0) { |
| 604 | LOGERR("An action handler has returned an error\n"); |
| 605 | ret = 1; |
| 606 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 607 | } |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 608 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 609 | } |
| 610 | |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 611 | int Page::NotifyCharInput(int ch) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 612 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 613 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 614 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 615 | // We work backwards, from top-most element to bottom-most element |
| 616 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 617 | { |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 618 | int ret = (*iter)->NotifyCharInput(ch); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 619 | if (ret == 0) |
| 620 | return 0; |
| 621 | else if (ret < 0) |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 622 | LOGERR("A char input handler has returned an error\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 623 | } |
| 624 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | int Page::SetKeyBoardFocus(int inFocus) |
| 628 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 629 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 630 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 631 | // We work backwards, from top-most element to bottom-most element |
| 632 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 633 | { |
| 634 | int ret = (*iter)->SetInputFocus(inFocus); |
| 635 | if (ret == 0) |
| 636 | return 0; |
| 637 | else if (ret < 0) |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 638 | LOGERR("An input focus handler has returned an error\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 639 | } |
| 640 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | void Page::SetPageFocus(int inFocus) |
| 644 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 645 | // Render remaining objects |
| 646 | std::vector<RenderObject*>::iterator iter; |
| 647 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 648 | (*iter)->SetPageFocus(inFocus); |
| 649 | |
| 650 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | int Page::NotifyVarChange(std::string varName, std::string value) |
| 654 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 655 | std::vector<GUIObject*>::iterator iter; |
| 656 | for (iter = mObjects.begin(); iter != mObjects.end(); ++iter) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 657 | { |
| 658 | if ((*iter)->NotifyVarChange(varName, value)) |
| 659 | LOGERR("An action handler errored on NotifyVarChange.\n"); |
| 660 | } |
| 661 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 662 | } |
| 663 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 664 | |
| 665 | // transient data for loading themes |
| 666 | struct LoadingContext |
| 667 | { |
| 668 | ZipArchive* zip; // zip to load theme from, or NULL for the stock theme |
| 669 | std::set<std::string> filenames; // to detect cyclic includes |
| 670 | std::string basepath; // if zip is NULL, base path to load includes from with trailing slash, otherwise empty |
| 671 | std::vector<xml_document<>*> xmldocs; // all loaded xml docs |
| 672 | std::vector<char*> xmlbuffers; // text buffers with xml content |
| 673 | std::vector<xml_node<>*> styles; // refer to <styles> nodes inside xmldocs |
| 674 | std::vector<xml_node<>*> templates; // refer to <templates> nodes inside xmldocs |
| 675 | |
| 676 | LoadingContext() |
| 677 | { |
| 678 | zip = NULL; |
| 679 | } |
| 680 | |
| 681 | ~LoadingContext() |
| 682 | { |
| 683 | // free all xml buffers |
| 684 | for (std::vector<char*>::iterator it = xmlbuffers.begin(); it != xmlbuffers.end(); ++it) |
| 685 | free(*it); |
| 686 | } |
| 687 | |
| 688 | }; |
| 689 | |
| 690 | // for FindStyle |
| 691 | LoadingContext* PageManager::currentLoadingContext = NULL; |
| 692 | |
| 693 | |
| 694 | PageSet::PageSet() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 695 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 696 | mResources = new ResourceManager; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 697 | mCurrentPage = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 698 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 699 | set_scale_values(1, 1); // Reset any previous scaling values |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | PageSet::~PageSet() |
| 703 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 704 | mOverlays.clear(); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 705 | for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr) |
| 706 | delete *itr; |
| 707 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 708 | delete mResources; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 709 | } |
| 710 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 711 | int PageSet::Load(LoadingContext& ctx, const std::string& filename) |
| 712 | { |
| 713 | bool isMain = ctx.xmlbuffers.empty(); // if we have no files yet, remember that this is the main XML file |
| 714 | |
| 715 | if (!ctx.filenames.insert(filename).second) |
| 716 | // ignore already loaded files to prevent crash with cyclic includes |
| 717 | return 0; |
| 718 | |
| 719 | // load XML into buffer |
| 720 | char* xmlbuffer = PageManager::LoadFileToBuffer(filename, ctx.zip); |
| 721 | if (!xmlbuffer) |
| 722 | return -1; // error already displayed by LoadFileToBuffer |
| 723 | ctx.xmlbuffers.push_back(xmlbuffer); |
| 724 | |
| 725 | // parse XML |
| 726 | xml_document<>* doc = new xml_document<>(); |
| 727 | doc->parse<0>(xmlbuffer); |
| 728 | ctx.xmldocs.push_back(doc); |
| 729 | |
| 730 | xml_node<>* root = doc->first_node("recovery"); |
| 731 | if (!root) |
| 732 | root = doc->first_node("install"); |
| 733 | if (!root) { |
| 734 | LOGERR("Unknown root element in %s\n", filename.c_str()); |
| 735 | return -1; |
| 736 | } |
| 737 | |
| 738 | if (isMain) { |
| 739 | int rc = LoadDetails(ctx, root); |
| 740 | if (rc != 0) |
| 741 | return rc; |
| 742 | } |
| 743 | |
| 744 | LOGINFO("Loading resources...\n"); |
| 745 | xml_node<>* child = root->first_node("resources"); |
| 746 | if (child) |
| 747 | mResources->LoadResources(child, ctx.zip, "theme"); |
| 748 | |
| 749 | LOGINFO("Loading variables...\n"); |
| 750 | child = root->first_node("variables"); |
| 751 | if (child) |
| 752 | LoadVariables(child); |
| 753 | |
| 754 | LOGINFO("Loading mouse cursor...\n"); |
| 755 | child = root->first_node("mousecursor"); |
| 756 | if (child) |
| 757 | PageManager::LoadCursorData(child); |
| 758 | |
| 759 | LOGINFO("Loading pages...\n"); |
| 760 | child = root->first_node("templates"); |
| 761 | if (child) |
| 762 | ctx.templates.push_back(child); |
| 763 | |
| 764 | child = root->first_node("styles"); |
| 765 | if (child) |
| 766 | ctx.styles.push_back(child); |
| 767 | |
| 768 | // Load pages |
| 769 | child = root->first_node("pages"); |
| 770 | if (child) { |
| 771 | if (LoadPages(ctx, child)) { |
| 772 | LOGERR("PageSet::Load returning -1\n"); |
| 773 | return -1; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | // process includes recursively |
| 778 | child = root->first_node("include"); |
| 779 | if (child) { |
| 780 | xml_node<>* include = child->first_node("xmlfile"); |
| 781 | while (include != NULL) { |
| 782 | xml_attribute<>* attr = include->first_attribute("name"); |
| 783 | if (!attr) { |
| 784 | LOGERR("Skipping include/xmlfile with no name\n"); |
| 785 | continue; |
| 786 | } |
| 787 | |
| 788 | string filename = ctx.basepath + attr->value(); |
| 789 | LOGINFO("Including file: %s...\n", filename.c_str()); |
| 790 | int rc = Load(ctx, filename); |
| 791 | if (rc != 0) |
| 792 | return rc; |
| 793 | |
| 794 | include = include->next_sibling("xmlfile"); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | void PageSet::MakeEmergencyConsoleIfNeeded() |
| 802 | { |
| 803 | if (mPages.empty()) { |
| 804 | mCurrentPage = new Page(NULL, NULL); // fallback console page |
| 805 | // TODO: since removal of non-TTF fonts, the emergency console doesn't work without a font, which might be missing too |
| 806 | mPages.push_back(mCurrentPage); |
| 807 | } |
| 808 | } |
| 809 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 810 | int PageSet::LoadLanguage(char* languageFile, ZipArchive* package) |
| 811 | { |
| 812 | xml_document<> lang; |
| 813 | xml_node<>* parent; |
| 814 | xml_node<>* child; |
| 815 | std::string resource_source; |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 816 | int ret = 0; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 817 | |
| 818 | if (languageFile) { |
| 819 | printf("parsing languageFile\n"); |
| 820 | lang.parse<0>(languageFile); |
| 821 | printf("parsing languageFile done\n"); |
| 822 | } else { |
| 823 | return -1; |
| 824 | } |
| 825 | |
| 826 | parent = lang.first_node("language"); |
| 827 | if (!parent) { |
| 828 | LOGERR("Unable to locate language node in language file.\n"); |
| 829 | lang.clear(); |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | child = parent->first_node("display"); |
| 834 | if (child) { |
| 835 | DataManager::SetValue("tw_language_display", child->value()); |
| 836 | resource_source = child->value(); |
| 837 | } else { |
| 838 | LOGERR("language file does not have a display value set\n"); |
| 839 | DataManager::SetValue("tw_language_display", "Not Set"); |
| 840 | resource_source = languageFile; |
| 841 | } |
| 842 | |
| 843 | child = parent->first_node("resources"); |
| 844 | if (child) |
| 845 | mResources->LoadResources(child, package, resource_source); |
| 846 | else |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 847 | ret = -1; |
| 848 | DataManager::SetValue("tw_backup_name", gui_lookup("auto_generate", "(Auto Generate)")); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 849 | lang.clear(); |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 850 | return ret; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 851 | } |
| 852 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 853 | int PageSet::LoadDetails(LoadingContext& ctx, xml_node<>* root) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 854 | { |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 855 | xml_node<>* child = root->first_node("details"); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 856 | if (child) { |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 857 | int theme_ver = 0; |
| 858 | xml_node<>* themeversion = child->first_node("themeversion"); |
| 859 | if (themeversion && themeversion->value()) { |
| 860 | theme_ver = atoi(themeversion->value()); |
| 861 | } else { |
| 862 | LOGINFO("No themeversion in theme.\n"); |
| 863 | } |
| 864 | if (theme_ver != TW_THEME_VERSION) { |
| 865 | LOGINFO("theme version from xml: %i, expected %i\n", theme_ver, TW_THEME_VERSION); |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 866 | if (ctx.zip) { |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 867 | gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme."); |
Ethan Yonker | 8e5692f | 2016-01-21 11:21:06 -0600 | [diff] [blame] | 868 | return TW_THEME_VER_ERR; |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 869 | } else { |
| 870 | gui_print_color("warning", "Stock theme version does not match TWRP version.\n"); |
| 871 | } |
| 872 | } |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 873 | xml_node<>* resolution = child->first_node("resolution"); |
| 874 | if (resolution) { |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 875 | LOGINFO("Checking resolution...\n"); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 876 | xml_attribute<>* width_attr = resolution->first_attribute("width"); |
| 877 | xml_attribute<>* height_attr = resolution->first_attribute("height"); |
| 878 | xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling"); |
| 879 | if (width_attr && height_attr && !noscale_attr) { |
| 880 | int width = atoi(width_attr->value()); |
| 881 | int height = atoi(height_attr->value()); |
| 882 | int offx = 0, offy = 0; |
| 883 | #ifdef TW_ROUND_SCREEN |
| 884 | xml_node<>* roundscreen = child->first_node("roundscreen"); |
| 885 | if (roundscreen) { |
| 886 | LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n"); |
| 887 | xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x"); |
| 888 | xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y"); |
| 889 | if (offx_attr) { |
| 890 | offx = atoi(offx_attr->value()); |
| 891 | } |
| 892 | if (offy_attr) { |
| 893 | offy = atoi(offy_attr->value()); |
| 894 | } |
| 895 | } |
| 896 | #endif |
| 897 | if (width != 0 && height != 0) { |
| 898 | float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width; |
| 899 | float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height; |
| 900 | #ifdef TW_ROUND_SCREEN |
| 901 | float scale_off_w = (float)gr_fb_width() / (float)width; |
| 902 | float scale_off_h = (float)gr_fb_height() / (float)height; |
| 903 | tw_x_offset = offx * scale_off_w; |
| 904 | tw_y_offset = offy * scale_off_h; |
| 905 | #endif |
| 906 | if (scale_w != 1 || scale_h != 1) { |
| 907 | LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset); |
| 908 | set_scale_values(scale_w, scale_h); |
| 909 | } |
| 910 | } |
| 911 | } else { |
| 912 | LOGINFO("XML does not contain width and height, no scaling will be applied\n"); |
| 913 | } |
| 914 | } else { |
| 915 | LOGINFO("XML contains no resolution tag, no scaling will be applied.\n"); |
| 916 | } |
| 917 | } else { |
| 918 | LOGINFO("XML contains no details tag, no scaling will be applied.\n"); |
| 919 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 920 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 921 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | int PageSet::SetPage(std::string page) |
| 925 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 926 | Page* tmp = FindPage(page); |
| 927 | if (tmp) |
| 928 | { |
| 929 | if (mCurrentPage) mCurrentPage->SetPageFocus(0); |
| 930 | mCurrentPage = tmp; |
| 931 | mCurrentPage->SetPageFocus(1); |
| 932 | mCurrentPage->NotifyVarChange("", ""); |
| 933 | return 0; |
| 934 | } |
| 935 | else |
| 936 | { |
| 937 | LOGERR("Unable to locate page (%s)\n", page.c_str()); |
| 938 | } |
| 939 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | int PageSet::SetOverlay(Page* page) |
| 943 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 944 | if (page) { |
| 945 | if (mOverlays.size() >= 10) { |
| 946 | LOGERR("Too many overlays requested, max is 10.\n"); |
| 947 | return -1; |
| 948 | } |
Matt Mower | d411f8d | 2015-04-09 16:04:12 -0500 | [diff] [blame] | 949 | |
| 950 | std::vector<Page*>::iterator iter; |
| 951 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) { |
| 952 | if ((*iter)->GetName() == page->GetName()) { |
| 953 | mOverlays.erase(iter); |
| 954 | // SetOverlay() is (and should stay) the only function which |
| 955 | // adds to mOverlays. Then, each page can appear at most once. |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 960 | page->SetPageFocus(1); |
| 961 | page->NotifyVarChange("", ""); |
| 962 | |
| 963 | if (!mOverlays.empty()) |
| 964 | mOverlays.back()->SetPageFocus(0); |
| 965 | |
| 966 | mOverlays.push_back(page); |
| 967 | } else { |
| 968 | if (!mOverlays.empty()) { |
| 969 | mOverlays.back()->SetPageFocus(0); |
| 970 | mOverlays.pop_back(); |
| 971 | if (!mOverlays.empty()) |
| 972 | mOverlays.back()->SetPageFocus(1); |
| 973 | else if (mCurrentPage) |
| 974 | mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again |
| 975 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 976 | } |
| 977 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 978 | } |
| 979 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 980 | const ResourceManager* PageSet::GetResources() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 981 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 982 | return mResources; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | Page* PageSet::FindPage(std::string name) |
| 986 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 987 | std::vector<Page*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 988 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 989 | for (iter = mPages.begin(); iter != mPages.end(); iter++) |
| 990 | { |
| 991 | if (name == (*iter)->GetName()) |
| 992 | return (*iter); |
| 993 | } |
| 994 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | int PageSet::LoadVariables(xml_node<>* vars) |
| 998 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 999 | xml_node<>* child; |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1000 | xml_attribute<> *name, *value, *persist; |
| 1001 | int p; |
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 | child = vars->first_node("variable"); |
| 1004 | while (child) |
| 1005 | { |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1006 | name = child->first_attribute("name"); |
| 1007 | value = child->first_attribute("value"); |
| 1008 | persist = child->first_attribute("persist"); |
| 1009 | if(name && value) |
| 1010 | { |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1011 | if (strcmp(name->value(), "tw_x_offset") == 0) { |
| 1012 | tw_x_offset = atoi(value->value()); |
| 1013 | child = child->next_sibling("variable"); |
| 1014 | continue; |
| 1015 | } |
| 1016 | if (strcmp(name->value(), "tw_y_offset") == 0) { |
| 1017 | tw_y_offset = atoi(value->value()); |
| 1018 | child = child->next_sibling("variable"); |
| 1019 | continue; |
| 1020 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1021 | p = persist ? atoi(persist->value()) : 0; |
Ethan Yonker | 96acb3d | 2014-08-05 09:20:30 -0500 | [diff] [blame] | 1022 | string temp = value->value(); |
| 1023 | string valstr = gui_parse_text(temp); |
| 1024 | |
| 1025 | if (valstr.find("+") != string::npos) { |
| 1026 | string val1str = valstr; |
| 1027 | val1str = val1str.substr(0, val1str.find('+')); |
| 1028 | string val2str = valstr; |
| 1029 | val2str = val2str.substr(val2str.find('+') + 1, string::npos); |
| 1030 | int val1 = atoi(val1str.c_str()); |
| 1031 | int val2 = atoi(val2str.c_str()); |
| 1032 | int val = val1 + val2; |
| 1033 | |
| 1034 | DataManager::SetValue(name->value(), val, p); |
| 1035 | } else if (valstr.find("-") != string::npos) { |
| 1036 | string val1str = valstr; |
| 1037 | val1str = val1str.substr(0, val1str.find('-')); |
| 1038 | string val2str = valstr; |
| 1039 | val2str = val2str.substr(val2str.find('-') + 1, string::npos); |
| 1040 | int val1 = atoi(val1str.c_str()); |
| 1041 | int val2 = atoi(val2str.c_str()); |
| 1042 | int val = val1 - val2; |
| 1043 | |
| 1044 | DataManager::SetValue(name->value(), val, p); |
| 1045 | } else { |
| 1046 | DataManager::SetValue(name->value(), valstr, p); |
| 1047 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1048 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1049 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1050 | child = child->next_sibling("variable"); |
| 1051 | } |
| 1052 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1055 | int PageSet::LoadPages(LoadingContext& ctx, xml_node<>* pages) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1056 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1057 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1058 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1059 | if (!pages) |
| 1060 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1061 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1062 | child = pages->first_node("page"); |
| 1063 | while (child != NULL) |
| 1064 | { |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1065 | Page* page = new Page(child, &ctx.templates); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1066 | if (page->GetName().empty()) |
| 1067 | { |
| 1068 | LOGERR("Unable to process load page\n"); |
| 1069 | delete page; |
| 1070 | } |
| 1071 | else |
| 1072 | { |
| 1073 | mPages.push_back(page); |
| 1074 | } |
| 1075 | child = child->next_sibling("page"); |
| 1076 | } |
| 1077 | if (mPages.size() > 0) |
| 1078 | return 0; |
| 1079 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | int PageSet::IsCurrentPage(Page* page) |
| 1083 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1084 | return ((mCurrentPage && mCurrentPage == page) ? 1 : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1085 | } |
| 1086 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1087 | std::string PageSet::GetCurrentPage() const |
| 1088 | { |
| 1089 | return mCurrentPage ? mCurrentPage->GetName() : ""; |
| 1090 | } |
| 1091 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1092 | int PageSet::Render(void) |
| 1093 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1094 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1095 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1096 | ret = (mCurrentPage ? mCurrentPage->Render() : -1); |
| 1097 | if (ret < 0) |
| 1098 | return ret; |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1099 | |
| 1100 | std::vector<Page*>::iterator iter; |
| 1101 | |
| 1102 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) { |
| 1103 | ret = ((*iter) ? (*iter)->Render() : -1); |
| 1104 | if (ret < 0) |
| 1105 | return ret; |
| 1106 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1107 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | int PageSet::Update(void) |
| 1111 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1112 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1113 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1114 | ret = (mCurrentPage ? mCurrentPage->Update() : -1); |
| 1115 | if (ret < 0 || ret > 1) |
| 1116 | return ret; |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1117 | |
| 1118 | std::vector<Page*>::iterator iter; |
| 1119 | |
| 1120 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) { |
| 1121 | ret = ((*iter) ? (*iter)->Update() : -1); |
| 1122 | if (ret < 0) |
| 1123 | return ret; |
| 1124 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1125 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1129 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1130 | if (!mOverlays.empty()) |
| 1131 | return mOverlays.back()->NotifyTouch(state, x, y); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1132 | |
| 1133 | return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1136 | int PageSet::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1137 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1138 | if (!mOverlays.empty()) |
| 1139 | return mOverlays.back()->NotifyKey(key, down); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1140 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1141 | return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1142 | } |
| 1143 | |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1144 | int PageSet::NotifyCharInput(int ch) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1145 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1146 | if (!mOverlays.empty()) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1147 | return mOverlays.back()->NotifyCharInput(ch); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1148 | |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1149 | return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | int PageSet::SetKeyBoardFocus(int inFocus) |
| 1153 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1154 | if (!mOverlays.empty()) |
| 1155 | return mOverlays.back()->SetKeyBoardFocus(inFocus); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1156 | |
| 1157 | return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | int PageSet::NotifyVarChange(std::string varName, std::string value) |
| 1161 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1162 | std::vector<Page*>::iterator iter; |
| 1163 | |
| 1164 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) |
| 1165 | (*iter)->NotifyVarChange(varName, value); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1166 | |
| 1167 | return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1170 | void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value) |
| 1171 | { |
| 1172 | mResources->AddStringResource(resource_source, resource_name, value); |
| 1173 | } |
| 1174 | |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1175 | char* PageManager::LoadFileToBuffer(std::string filename, ZipArchive* package) { |
| 1176 | size_t len; |
| 1177 | char* buffer = NULL; |
| 1178 | |
| 1179 | if (!package) { |
| 1180 | // We can try to load the XML directly... |
| 1181 | LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str()); |
| 1182 | struct stat st; |
| 1183 | if(stat(filename.c_str(),&st) != 0) { |
| 1184 | // This isn't always an error, sometimes we request files that don't exist. |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | |
| 1188 | len = (size_t)st.st_size; |
| 1189 | |
| 1190 | buffer = (char*) malloc(len + 1); |
| 1191 | if (!buffer) { |
| 1192 | LOGERR("PageManager::LoadFileToBuffer failed to malloc\n"); |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | |
| 1196 | int fd = open(filename.c_str(), O_RDONLY); |
| 1197 | if (fd == -1) { |
| 1198 | LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno)); |
| 1199 | free(buffer); |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | |
| 1203 | if (read(fd, buffer, len) < 0) { |
| 1204 | LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno)); |
| 1205 | free(buffer); |
| 1206 | close(fd); |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | close(fd); |
| 1210 | } else { |
| 1211 | LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str()); |
| 1212 | const ZipEntry* zipentry = mzFindZipEntry(package, filename.c_str()); |
| 1213 | if (zipentry == NULL) { |
| 1214 | LOGERR("Unable to locate '%s' in zip file\n", filename.c_str()); |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | |
| 1218 | // Allocate the buffer for the file |
| 1219 | len = mzGetZipEntryUncompLen(zipentry); |
| 1220 | buffer = (char*) malloc(len + 1); |
| 1221 | if (!buffer) |
| 1222 | return NULL; |
| 1223 | |
| 1224 | if (!mzExtractZipEntryToBuffer(package, zipentry, (unsigned char*) buffer)) { |
| 1225 | LOGERR("Unable to extract '%s'\n", filename.c_str()); |
| 1226 | free(buffer); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | } |
| 1230 | // NULL-terminate the string |
| 1231 | buffer[len] = 0x00; |
| 1232 | return buffer; |
| 1233 | } |
| 1234 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1235 | void PageManager::LoadLanguageListDir(string dir) { |
| 1236 | if (!TWFunc::Path_Exists(dir)) { |
| 1237 | LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str()); |
| 1238 | return; |
| 1239 | } |
| 1240 | |
| 1241 | DIR *d = opendir(dir.c_str()); |
| 1242 | struct dirent *p; |
| 1243 | |
| 1244 | if (d == NULL) { |
| 1245 | LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno)); |
| 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | while ((p = readdir(d))) { |
| 1250 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5) |
| 1251 | continue; |
| 1252 | |
| 1253 | string file = p->d_name; |
| 1254 | if (file.substr(strlen(p->d_name) - 4) != ".xml") |
| 1255 | continue; |
| 1256 | string path = dir + p->d_name; |
| 1257 | string file_no_extn = file.substr(0, strlen(p->d_name) - 4); |
| 1258 | struct language_struct language_entry; |
| 1259 | language_entry.filename = file_no_extn; |
| 1260 | char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL); |
| 1261 | if (xmlFile == NULL) { |
| 1262 | LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str()); |
| 1263 | continue; |
| 1264 | } |
| 1265 | xml_document<> *doc = new xml_document<>(); |
| 1266 | doc->parse<0>(xmlFile); |
| 1267 | |
| 1268 | xml_node<>* parent = doc->first_node("language"); |
| 1269 | if (!parent) { |
| 1270 | LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str()); |
| 1271 | } else { |
| 1272 | xml_node<>* child = parent->first_node("display"); |
| 1273 | if (child) { |
| 1274 | language_entry.displayvalue = child->value(); |
| 1275 | } else { |
| 1276 | LOGERR("No display value for '%s'\n", language_entry.filename.c_str()); |
| 1277 | language_entry.displayvalue = language_entry.filename; |
| 1278 | } |
| 1279 | Language_List.push_back(language_entry); |
| 1280 | } |
| 1281 | doc->clear(); |
| 1282 | delete doc; |
| 1283 | free(xmlFile); |
| 1284 | } |
| 1285 | closedir(d); |
| 1286 | } |
| 1287 | |
| 1288 | void PageManager::LoadLanguageList(ZipArchive* package) { |
| 1289 | Language_List.clear(); |
| 1290 | if (TWFunc::Path_Exists(TWRES "customlanguages")) |
| 1291 | TWFunc::removeDir(TWRES "customlanguages", true); |
| 1292 | if (package) { |
| 1293 | TWFunc::Recursive_Mkdir(TWRES "customlanguages"); |
| 1294 | struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
| 1295 | mzExtractRecursive(package, "languages", TWRES "customlanguages/", ×tamp, NULL, NULL, NULL); |
| 1296 | LoadLanguageListDir(TWRES "customlanguages/"); |
| 1297 | } else { |
| 1298 | LoadLanguageListDir(TWRES "languages/"); |
| 1299 | } |
Xuefer | cac6ace | 2016-02-01 02:28:55 +0800 | [diff] [blame] | 1300 | |
| 1301 | std::sort(Language_List.begin(), Language_List.end()); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | void PageManager::LoadLanguage(string filename) { |
| 1305 | string actual_filename; |
| 1306 | if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml")) |
| 1307 | actual_filename = TWRES "customlanguages/" + filename + ".xml"; |
| 1308 | else |
| 1309 | actual_filename = TWRES "languages/" + filename + ".xml"; |
| 1310 | char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL); |
| 1311 | if (xmlFile == NULL) |
| 1312 | LOGERR("Unable to load '%s'\n", actual_filename.c_str()); |
| 1313 | else { |
| 1314 | mCurrentSet->LoadLanguage(xmlFile, NULL); |
| 1315 | free(xmlFile); |
| 1316 | } |
| 1317 | PartitionManager.Translate_Partition_Display_Names(); |
| 1318 | } |
| 1319 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1320 | int PageManager::LoadPackage(std::string name, std::string package, std::string startpage) |
| 1321 | { |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1322 | std::string mainxmlfilename = package; |
| 1323 | ZipArchive zip; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1324 | char* languageFile = NULL; |
Ethan Yonker | 8e5692f | 2016-01-21 11:21:06 -0600 | [diff] [blame] | 1325 | char* baseLanguageFile = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1326 | PageSet* pageSet = NULL; |
| 1327 | int ret; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1328 | MemMapping map; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1329 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1330 | mReloadTheme = false; |
| 1331 | mStartPage = startpage; |
| 1332 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1333 | // init the loading context |
| 1334 | LoadingContext ctx; |
| 1335 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1336 | // Open the XML file |
| 1337 | LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str()); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1338 | if (package.size() > 4 && package.substr(package.size() - 4) != ".zip") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1339 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1340 | LOGINFO("Load XML directly\n"); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1341 | tw_x_offset = TW_X_OFFSET; |
| 1342 | tw_y_offset = TW_Y_OFFSET; |
Ethan Yonker | 4f74d14 | 2016-03-31 08:10:37 -0500 | [diff] [blame] | 1343 | if (name != "splash") { |
| 1344 | LoadLanguageList(NULL); |
| 1345 | languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL); |
| 1346 | } |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1347 | ctx.basepath = TWRES; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1348 | } |
| 1349 | else |
| 1350 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1351 | LOGINFO("Loading zip theme\n"); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1352 | tw_x_offset = 0; |
| 1353 | tw_y_offset = 0; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1354 | if (!TWFunc::Path_Exists(package)) |
| 1355 | return -1; |
| 1356 | if (sysMapFile(package.c_str(), &map) != 0) { |
| 1357 | LOGERR("Failed to map '%s'\n", package.c_str()); |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1358 | goto error; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1359 | } |
| 1360 | if (mzOpenZipArchive(map.addr, map.length, &zip)) { |
| 1361 | LOGERR("Unable to open zip archive '%s'\n", package.c_str()); |
| 1362 | sysReleaseMap(&map); |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1363 | goto error; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1364 | } |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1365 | ctx.zip = &zip; |
| 1366 | mainxmlfilename = "ui.xml"; |
| 1367 | LoadLanguageList(ctx.zip); |
| 1368 | languageFile = LoadFileToBuffer("languages/en.xml", ctx.zip); |
Ethan Yonker | 8e5692f | 2016-01-21 11:21:06 -0600 | [diff] [blame] | 1369 | baseLanguageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1370 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1371 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1372 | // Before loading, mCurrentSet must be the loading package so we can find resources |
| 1373 | pageSet = mCurrentSet; |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1374 | mCurrentSet = new PageSet(); |
| 1375 | |
| 1376 | if (baseLanguageFile) { |
| 1377 | mCurrentSet->LoadLanguage(baseLanguageFile, NULL); |
| 1378 | free(baseLanguageFile); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1379 | } |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1380 | |
| 1381 | if (languageFile) { |
| 1382 | mCurrentSet->LoadLanguage(languageFile, ctx.zip); |
| 1383 | free(languageFile); |
| 1384 | } |
| 1385 | |
| 1386 | // Load and parse the XML and all includes |
| 1387 | currentLoadingContext = &ctx; // required to find styles |
| 1388 | ret = mCurrentSet->Load(ctx, mainxmlfilename); |
| 1389 | currentLoadingContext = NULL; |
| 1390 | |
Ethan Yonker | 8e5692f | 2016-01-21 11:21:06 -0600 | [diff] [blame] | 1391 | if (ret == 0) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1392 | mCurrentSet->SetPage(startpage); |
| 1393 | mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet)); |
Ethan Yonker | 8e5692f | 2016-01-21 11:21:06 -0600 | [diff] [blame] | 1394 | } else { |
| 1395 | if (ret != TW_THEME_VER_ERR) |
| 1396 | LOGERR("Package %s failed to load.\n", name.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1397 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1398 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1399 | // reset to previous pageset |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1400 | mCurrentSet = pageSet; |
| 1401 | |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1402 | if (ctx.zip) { |
| 1403 | mzCloseZipArchive(ctx.zip); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1404 | sysReleaseMap(&map); |
| 1405 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1406 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1407 | |
| 1408 | error: |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1409 | // Sometimes we get here without a real error |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1410 | if (ctx.zip) { |
| 1411 | mzCloseZipArchive(ctx.zip); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1412 | sysReleaseMap(&map); |
| 1413 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1414 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | PageSet* PageManager::FindPackage(std::string name) |
| 1418 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1419 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1420 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1421 | iter = mPageSets.find(name); |
| 1422 | if (iter != mPageSets.end()) |
| 1423 | return (*iter).second; |
| 1424 | |
| 1425 | LOGERR("Unable to locate package %s\n", name.c_str()); |
| 1426 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | PageSet* PageManager::SelectPackage(std::string name) |
| 1430 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1431 | LOGINFO("Switching packages (%s)\n", name.c_str()); |
| 1432 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1433 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1434 | tmp = FindPackage(name); |
| 1435 | if (tmp) |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1436 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1437 | mCurrentSet = tmp; |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1438 | mCurrentSet->MakeEmergencyConsoleIfNeeded(); |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1439 | mCurrentSet->NotifyVarChange("", ""); |
| 1440 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1441 | else |
| 1442 | LOGERR("Unable to find package.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1443 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1444 | return mCurrentSet; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | int PageManager::ReloadPackage(std::string name, std::string package) |
| 1448 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1449 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1450 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1451 | mReloadTheme = false; |
| 1452 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1453 | iter = mPageSets.find(name); |
| 1454 | if (iter == mPageSets.end()) |
| 1455 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1456 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1457 | if(mMouseCursor) |
| 1458 | mMouseCursor->ResetData(gr_fb_width(), gr_fb_height()); |
| 1459 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1460 | PageSet* set = (*iter).second; |
| 1461 | mPageSets.erase(iter); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1462 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1463 | if (LoadPackage(name, package, mStartPage) != 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1464 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1465 | LOGINFO("Failed to load package '%s'.\n", package.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1466 | mPageSets.insert(std::pair<std::string, PageSet*>(name, set)); |
| 1467 | return -1; |
| 1468 | } |
| 1469 | if (mCurrentSet == set) |
| 1470 | SelectPackage(name); |
| 1471 | delete set; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1472 | GUIConsole::Translate_Now(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1473 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | void PageManager::ReleasePackage(std::string name) |
| 1477 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1478 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1479 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1480 | iter = mPageSets.find(name); |
| 1481 | if (iter == mPageSets.end()) |
| 1482 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1483 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1484 | PageSet* set = (*iter).second; |
| 1485 | mPageSets.erase(iter); |
| 1486 | delete set; |
that | 235c648 | 2016-01-24 21:59:00 +0100 | [diff] [blame] | 1487 | if (set == mCurrentSet) |
| 1488 | mCurrentSet = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1489 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1492 | int PageManager::RunReload() { |
| 1493 | int ret_val = 0; |
| 1494 | std::string theme_path; |
| 1495 | |
| 1496 | if (!mReloadTheme) |
| 1497 | return 0; |
| 1498 | |
| 1499 | mReloadTheme = false; |
| 1500 | theme_path = DataManager::GetSettingsStoragePath(); |
| 1501 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
| 1502 | LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str()); |
| 1503 | ret_val = 1; |
| 1504 | } |
| 1505 | |
| 1506 | theme_path += "/TWRP/theme/ui.zip"; |
| 1507 | if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0) |
| 1508 | { |
| 1509 | // Loading the custom theme failed - try loading the stock theme |
| 1510 | LOGINFO("Attempting to reload stock theme...\n"); |
| 1511 | if (ReloadPackage("TWRP", TWRES "ui.xml")) |
| 1512 | { |
| 1513 | LOGERR("Failed to load base packages.\n"); |
| 1514 | ret_val = 1; |
| 1515 | } |
| 1516 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1517 | if (ret_val == 0) { |
| 1518 | if (DataManager::GetStrValue("tw_language") != "en.xml") { |
| 1519 | LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str()); |
| 1520 | LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | // This makes the console re-translate |
| 1525 | last_message_count = 0; |
| 1526 | gConsole.clear(); |
| 1527 | gConsoleColor.clear(); |
| 1528 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1529 | return ret_val; |
| 1530 | } |
| 1531 | |
| 1532 | void PageManager::RequestReload() { |
| 1533 | mReloadTheme = true; |
| 1534 | } |
| 1535 | |
Ethan Yonker | afde098 | 2016-01-23 08:55:35 -0600 | [diff] [blame] | 1536 | void PageManager::SetStartPage(const std::string& page_name) { |
| 1537 | mStartPage = page_name; |
| 1538 | } |
| 1539 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1540 | int PageManager::ChangePage(std::string name) |
| 1541 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1542 | DataManager::SetValue("tw_operation_state", 0); |
| 1543 | int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1); |
| 1544 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1545 | } |
| 1546 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1547 | std::string PageManager::GetCurrentPage() |
| 1548 | { |
| 1549 | return mCurrentSet ? mCurrentSet->GetCurrentPage() : ""; |
| 1550 | } |
| 1551 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1552 | int PageManager::ChangeOverlay(std::string name) |
| 1553 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1554 | if (name.empty()) |
| 1555 | return mCurrentSet->SetOverlay(NULL); |
| 1556 | else |
| 1557 | { |
Ethan Yonker | 1308d53 | 2016-01-14 22:21:49 -0600 | [diff] [blame] | 1558 | Page* page = mCurrentSet ? mCurrentSet->FindPage(name) : NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1559 | return mCurrentSet->SetOverlay(page); |
| 1560 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1561 | } |
| 1562 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 1563 | const ResourceManager* PageManager::GetResources() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1564 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 1565 | return (mCurrentSet ? mCurrentSet->GetResources() : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1568 | int PageManager::IsCurrentPage(Page* page) |
| 1569 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1570 | return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | int PageManager::Render(void) |
| 1574 | { |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1575 | if(blankTimer.isScreenOff()) |
| 1576 | return 0; |
| 1577 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1578 | int res = (mCurrentSet ? mCurrentSet->Render() : -1); |
| 1579 | if(mMouseCursor) |
| 1580 | mMouseCursor->Render(); |
| 1581 | return res; |
| 1582 | } |
| 1583 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1584 | HardwareKeyboard *PageManager::GetHardwareKeyboard() |
| 1585 | { |
| 1586 | if(!mHardwareKeyboard) |
| 1587 | mHardwareKeyboard = new HardwareKeyboard(); |
| 1588 | return mHardwareKeyboard; |
| 1589 | } |
| 1590 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 1591 | xml_node<>* PageManager::FindStyle(std::string name) |
| 1592 | { |
that | 6a89459 | 2016-03-13 17:51:28 +0100 | [diff] [blame] | 1593 | if (!currentLoadingContext) |
| 1594 | { |
| 1595 | LOGERR("FindStyle works only while loading a theme.\n"); |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | |
| 1599 | for (std::vector<xml_node<>*>::iterator itr = currentLoadingContext->styles.begin(); itr != currentLoadingContext->styles.end(); itr++) { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 1600 | xml_node<>* node = (*itr)->first_node("style"); |
| 1601 | |
| 1602 | while (node) { |
| 1603 | if (!node->first_attribute("name")) |
| 1604 | continue; |
| 1605 | |
| 1606 | if (name == node->first_attribute("name")->value()) |
| 1607 | return node; |
| 1608 | node = node->next_sibling("style"); |
| 1609 | } |
| 1610 | } |
| 1611 | return NULL; |
| 1612 | } |
| 1613 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1614 | MouseCursor *PageManager::GetMouseCursor() |
| 1615 | { |
| 1616 | if(!mMouseCursor) |
| 1617 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 1618 | return mMouseCursor; |
| 1619 | } |
| 1620 | |
| 1621 | void PageManager::LoadCursorData(xml_node<>* node) |
| 1622 | { |
| 1623 | if(!mMouseCursor) |
| 1624 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 1625 | |
| 1626 | mMouseCursor->LoadData(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | int PageManager::Update(void) |
| 1630 | { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 1631 | if(blankTimer.isScreenOff()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1632 | return 0; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1633 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1634 | if (RunReload()) |
| 1635 | return -2; |
| 1636 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1637 | int res = (mCurrentSet ? mCurrentSet->Update() : -1); |
| 1638 | |
| 1639 | if(mMouseCursor) |
| 1640 | { |
| 1641 | int c_res = mMouseCursor->Update(); |
| 1642 | if(c_res > res) |
| 1643 | res = c_res; |
| 1644 | } |
| 1645 | return res; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1649 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1650 | return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1651 | } |
| 1652 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1653 | int PageManager::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1654 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1655 | return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1656 | } |
| 1657 | |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1658 | int PageManager::NotifyCharInput(int ch) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1659 | { |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1660 | return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | int PageManager::SetKeyBoardFocus(int inFocus) |
| 1664 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1665 | return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | int PageManager::NotifyVarChange(std::string varName, std::string value) |
| 1669 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1670 | return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1671 | } |
| 1672 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1673 | void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value) |
| 1674 | { |
| 1675 | if (mCurrentSet) |
| 1676 | mCurrentSet->AddStringResource(resource_source, resource_name, value); |
| 1677 | } |
| 1678 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1679 | extern "C" void gui_notifyVarChange(const char *name, const char* value) |
| 1680 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1681 | if (!gGuiRunning) |
| 1682 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1683 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1684 | PageManager::NotifyVarChange(name, value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1685 | } |