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