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> |
| 39 | |
| 40 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | #include "../minuitwrp/minui.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 | } |
| 47 | |
| 48 | #include "rapidxml.hpp" |
| 49 | #include "objects.hpp" |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 50 | #include "blanktimer.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
| 52 | extern int gGuiRunning; |
| 53 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 54 | // From ../twrp.cpp |
| 55 | extern bool datamedia; |
| 56 | |
| 57 | // From console.cpp |
| 58 | extern size_t last_message_count; |
| 59 | extern std::vector<std::string> gConsole; |
| 60 | extern std::vector<std::string> gConsoleColor; |
| 61 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | std::map<std::string, PageSet*> PageManager::mPageSets; |
| 63 | PageSet* PageManager::mCurrentSet; |
| 64 | PageSet* PageManager::mBaseSet = NULL; |
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 | } |
| 375 | else if (type == "button") |
| 376 | { |
| 377 | GUIButton* element = new GUIButton(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 378 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 379 | mRenders.push_back(element); |
| 380 | mActions.push_back(element); |
| 381 | } |
| 382 | else if (type == "checkbox") |
| 383 | { |
| 384 | GUICheckbox* element = new GUICheckbox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 385 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 386 | mRenders.push_back(element); |
| 387 | mActions.push_back(element); |
| 388 | } |
| 389 | else if (type == "fileselector") |
| 390 | { |
| 391 | GUIFileSelector* element = new GUIFileSelector(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 392 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 393 | mRenders.push_back(element); |
| 394 | mActions.push_back(element); |
| 395 | } |
| 396 | else if (type == "animation") |
| 397 | { |
| 398 | GUIAnimation* element = new GUIAnimation(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 399 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 400 | mRenders.push_back(element); |
| 401 | } |
| 402 | else if (type == "progressbar") |
| 403 | { |
| 404 | GUIProgressBar* element = new GUIProgressBar(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 405 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 406 | mRenders.push_back(element); |
| 407 | mActions.push_back(element); |
| 408 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | else if (type == "slider") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 410 | { |
| 411 | GUISlider* element = new GUISlider(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 412 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 413 | mRenders.push_back(element); |
| 414 | mActions.push_back(element); |
| 415 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 416 | else if (type == "slidervalue") |
| 417 | { |
| 418 | GUISliderValue *element = new GUISliderValue(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 419 | mObjects.push_back(element); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 420 | mRenders.push_back(element); |
| 421 | mActions.push_back(element); |
| 422 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 423 | else if (type == "listbox") |
| 424 | { |
| 425 | GUIListBox* element = new GUIListBox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 426 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 427 | mRenders.push_back(element); |
| 428 | mActions.push_back(element); |
| 429 | } |
| 430 | else if (type == "keyboard") |
| 431 | { |
| 432 | GUIKeyboard* element = new GUIKeyboard(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 433 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 434 | mRenders.push_back(element); |
| 435 | mActions.push_back(element); |
| 436 | } |
| 437 | else if (type == "input") |
| 438 | { |
| 439 | GUIInput* element = new GUIInput(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 440 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 441 | mRenders.push_back(element); |
| 442 | mActions.push_back(element); |
| 443 | mInputs.push_back(element); |
| 444 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 445 | else if (type == "partitionlist") |
| 446 | { |
| 447 | GUIPartitionList* element = new GUIPartitionList(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 448 | mObjects.push_back(element); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 449 | mRenders.push_back(element); |
| 450 | mActions.push_back(element); |
| 451 | } |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 452 | else if (type == "patternpassword") |
| 453 | { |
| 454 | GUIPatternPassword* element = new GUIPatternPassword(child); |
| 455 | mObjects.push_back(element); |
| 456 | mRenders.push_back(element); |
| 457 | mActions.push_back(element); |
| 458 | } |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 459 | else if (type == "textbox") |
| 460 | { |
| 461 | GUITextBox* element = new GUITextBox(child); |
| 462 | mObjects.push_back(element); |
| 463 | mRenders.push_back(element); |
| 464 | mActions.push_back(element); |
| 465 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 466 | else if (type == "template") |
| 467 | { |
| 468 | if (!templates || !child->first_attribute("name")) |
| 469 | { |
| 470 | LOGERR("Invalid template request.\n"); |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | std::string name = child->first_attribute("name")->value(); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 475 | xml_node<>* node; |
| 476 | bool node_found = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 477 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 478 | // We need to find the correct template |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 479 | for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) { |
| 480 | node = (*itr)->first_node("template"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 481 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 482 | while (node) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 483 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 484 | if (!node->first_attribute("name")) |
| 485 | continue; |
| 486 | |
| 487 | if (name == node->first_attribute("name")->value()) |
| 488 | { |
| 489 | if (!ProcessNode(node, templates, depth + 1)) |
| 490 | return false; |
| 491 | else { |
| 492 | node_found = true; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | if (node_found) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 497 | break; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 498 | node = node->next_sibling("template"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 499 | } |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 500 | // [check] why is there no if (node_found) here too? |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | else |
| 505 | { |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 506 | LOGERR("Unknown object type: %s.\n", type.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 507 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 508 | } |
| 509 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | int Page::Render(void) |
| 513 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 514 | // Render background |
| 515 | gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha); |
| 516 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 517 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 518 | // Render remaining objects |
| 519 | std::vector<RenderObject*>::iterator iter; |
| 520 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 521 | { |
| 522 | if ((*iter)->Render()) |
| 523 | LOGERR("A render request has failed.\n"); |
| 524 | } |
| 525 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | int Page::Update(void) |
| 529 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 530 | int retCode = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 531 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 532 | std::vector<RenderObject*>::iterator iter; |
| 533 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 534 | { |
| 535 | int ret = (*iter)->Update(); |
| 536 | if (ret < 0) |
| 537 | LOGERR("An update request has failed.\n"); |
| 538 | else if (ret > retCode) |
| 539 | retCode = ret; |
| 540 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 541 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 542 | return retCode; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | int Page::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 546 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 547 | // By default, return 1 to ignore further touches if nobody is listening |
| 548 | int ret = 1; |
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 | // Don't try to handle a lack of handlers |
| 551 | if (mActions.size() == 0) |
| 552 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 553 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 554 | // We record mTouchStart so we can pass all the touch stream to the same handler |
| 555 | if (state == TOUCH_START) |
| 556 | { |
| 557 | std::vector<ActionObject*>::reverse_iterator iter; |
| 558 | // We work backwards, from top-most element to bottom-most element |
| 559 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 560 | { |
| 561 | if ((*iter)->IsInRegion(x, y)) |
| 562 | { |
| 563 | mTouchStart = (*iter); |
| 564 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 565 | if (ret >= 0) |
| 566 | break; |
| 567 | mTouchStart = NULL; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | else if (state == TOUCH_RELEASE && mTouchStart != NULL) |
| 572 | { |
| 573 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 574 | mTouchStart = NULL; |
| 575 | } |
| 576 | else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL) |
| 577 | { |
| 578 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 579 | } |
| 580 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 581 | } |
| 582 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 583 | int Page::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 584 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 585 | std::vector<ActionObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 586 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 587 | // Don't try to handle a lack of handlers |
| 588 | if (mActions.size() == 0) |
| 589 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 590 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 591 | int ret = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 592 | // We work backwards, from top-most element to bottom-most element |
| 593 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 594 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 595 | ret = (*iter)->NotifyKey(key, down); |
| 596 | if (ret < 0) { |
| 597 | LOGERR("An action handler has returned an error\n"); |
| 598 | ret = 1; |
| 599 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 600 | } |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 601 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | int Page::NotifyKeyboard(int key) |
| 605 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 606 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 607 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 608 | // Don't try to handle a lack of handlers |
| 609 | if (mInputs.size() == 0) |
| 610 | return 1; |
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 | // We work backwards, from top-most element to bottom-most element |
| 613 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 614 | { |
| 615 | int ret = (*iter)->NotifyKeyboard(key); |
| 616 | if (ret == 0) |
| 617 | return 0; |
| 618 | else if (ret < 0) |
| 619 | LOGERR("A keyboard handler has returned an error"); |
| 620 | } |
| 621 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int Page::SetKeyBoardFocus(int inFocus) |
| 625 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 626 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 627 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 628 | // Don't try to handle a lack of handlers |
| 629 | if (mInputs.size() == 0) |
| 630 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 631 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 632 | // We work backwards, from top-most element to bottom-most element |
| 633 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 634 | { |
| 635 | int ret = (*iter)->SetInputFocus(inFocus); |
| 636 | if (ret == 0) |
| 637 | return 0; |
| 638 | else if (ret < 0) |
| 639 | LOGERR("An input focus handler has returned an error"); |
| 640 | } |
| 641 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | void Page::SetPageFocus(int inFocus) |
| 645 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 646 | // Render remaining objects |
| 647 | std::vector<RenderObject*>::iterator iter; |
| 648 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 649 | (*iter)->SetPageFocus(inFocus); |
| 650 | |
| 651 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | int Page::NotifyVarChange(std::string varName, std::string value) |
| 655 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 656 | std::vector<GUIObject*>::iterator iter; |
| 657 | for (iter = mObjects.begin(); iter != mObjects.end(); ++iter) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 658 | { |
| 659 | if ((*iter)->NotifyVarChange(varName, value)) |
| 660 | LOGERR("An action handler errored on NotifyVarChange.\n"); |
| 661 | } |
| 662 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 663 | } |
| 664 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 665 | PageSet::PageSet(const char* xmlFile) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 666 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 667 | mResources = new ResourceManager; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 668 | mCurrentPage = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 669 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 670 | if (!xmlFile) |
that | b63e2f9 | 2015-06-27 21:35:11 +0200 | [diff] [blame] | 671 | mCurrentPage = new Page(NULL, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | PageSet::~PageSet() |
| 675 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 676 | mOverlays.clear(); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 677 | for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr) |
| 678 | delete *itr; |
| 679 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 680 | delete mResources; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 681 | } |
| 682 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 683 | int PageSet::LoadLanguage(char* languageFile, ZipArchive* package) |
| 684 | { |
| 685 | xml_document<> lang; |
| 686 | xml_node<>* parent; |
| 687 | xml_node<>* child; |
| 688 | std::string resource_source; |
| 689 | |
| 690 | if (languageFile) { |
| 691 | printf("parsing languageFile\n"); |
| 692 | lang.parse<0>(languageFile); |
| 693 | printf("parsing languageFile done\n"); |
| 694 | } else { |
| 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | parent = lang.first_node("language"); |
| 699 | if (!parent) { |
| 700 | LOGERR("Unable to locate language node in language file.\n"); |
| 701 | lang.clear(); |
| 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | child = parent->first_node("display"); |
| 706 | if (child) { |
| 707 | DataManager::SetValue("tw_language_display", child->value()); |
| 708 | resource_source = child->value(); |
| 709 | } else { |
| 710 | LOGERR("language file does not have a display value set\n"); |
| 711 | DataManager::SetValue("tw_language_display", "Not Set"); |
| 712 | resource_source = languageFile; |
| 713 | } |
| 714 | |
| 715 | child = parent->first_node("resources"); |
| 716 | if (child) |
| 717 | mResources->LoadResources(child, package, resource_source); |
| 718 | else |
| 719 | return -1; |
| 720 | lang.clear(); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | int PageSet::Load(ZipArchive* package, char* xmlFile, char* languageFile) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 725 | { |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 726 | xml_document<> mDoc; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 727 | xml_node<>* parent; |
| 728 | xml_node<>* child; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 729 | xml_node<>* xmltemplate; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 730 | xml_node<>* xmlstyle; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 731 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 732 | mDoc.parse<0>(xmlFile); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 733 | parent = mDoc.first_node("recovery"); |
| 734 | if (!parent) |
| 735 | parent = mDoc.first_node("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 736 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 737 | set_scale_values(1, 1); // Reset any previous scaling values |
| 738 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 739 | // Now, let's parse the XML |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 740 | LOGINFO("Checking resolution...\n"); |
| 741 | child = parent->first_node("details"); |
| 742 | if (child) { |
| 743 | xml_node<>* resolution = child->first_node("resolution"); |
| 744 | if (resolution) { |
| 745 | xml_attribute<>* width_attr = resolution->first_attribute("width"); |
| 746 | xml_attribute<>* height_attr = resolution->first_attribute("height"); |
| 747 | xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling"); |
| 748 | if (width_attr && height_attr && !noscale_attr) { |
| 749 | int width = atoi(width_attr->value()); |
| 750 | int height = atoi(height_attr->value()); |
| 751 | int offx = 0, offy = 0; |
| 752 | #ifdef TW_ROUND_SCREEN |
| 753 | xml_node<>* roundscreen = child->first_node("roundscreen"); |
| 754 | if (roundscreen) { |
| 755 | LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n"); |
| 756 | xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x"); |
| 757 | xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y"); |
| 758 | if (offx_attr) { |
| 759 | offx = atoi(offx_attr->value()); |
| 760 | } |
| 761 | if (offy_attr) { |
| 762 | offy = atoi(offy_attr->value()); |
| 763 | } |
| 764 | } |
| 765 | #endif |
| 766 | if (width != 0 && height != 0) { |
| 767 | float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width; |
| 768 | float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height; |
| 769 | #ifdef TW_ROUND_SCREEN |
| 770 | float scale_off_w = (float)gr_fb_width() / (float)width; |
| 771 | float scale_off_h = (float)gr_fb_height() / (float)height; |
| 772 | tw_x_offset = offx * scale_off_w; |
| 773 | tw_y_offset = offy * scale_off_h; |
| 774 | #endif |
| 775 | if (scale_w != 1 || scale_h != 1) { |
| 776 | LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset); |
| 777 | set_scale_values(scale_w, scale_h); |
| 778 | } |
| 779 | } |
| 780 | } else { |
| 781 | LOGINFO("XML does not contain width and height, no scaling will be applied\n"); |
| 782 | } |
| 783 | } else { |
| 784 | LOGINFO("XML contains no resolution tag, no scaling will be applied.\n"); |
| 785 | } |
| 786 | } else { |
| 787 | LOGINFO("XML contains no details tag, no scaling will be applied.\n"); |
| 788 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 789 | |
| 790 | if (languageFile) |
| 791 | LoadLanguage(languageFile, package); |
| 792 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 793 | LOGINFO("Loading resources...\n"); |
| 794 | child = parent->first_node("resources"); |
| 795 | if (child) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 796 | mResources->LoadResources(child, package, "theme"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 797 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 798 | LOGINFO("Loading variables...\n"); |
| 799 | child = parent->first_node("variables"); |
| 800 | if (child) |
| 801 | LoadVariables(child); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 802 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 803 | LOGINFO("Loading mouse cursor...\n"); |
| 804 | child = parent->first_node("mousecursor"); |
| 805 | if(child) |
| 806 | PageManager::LoadCursorData(child); |
| 807 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 808 | LOGINFO("Loading pages...\n"); |
| 809 | // This may be NULL if no templates are present |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 810 | xmltemplate = parent->first_node("templates"); |
| 811 | if (xmltemplate) |
| 812 | templates.push_back(xmltemplate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 813 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 814 | // Load styles if present |
| 815 | xmlstyle = parent->first_node("styles"); |
| 816 | if (xmlstyle) |
| 817 | styles.push_back(xmlstyle); |
| 818 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 819 | child = parent->first_node("pages"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 820 | if (child) { |
| 821 | if (LoadPages(child)) { |
| 822 | LOGERR("PageSet::Load returning -1\n"); |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 823 | mDoc.clear(); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 824 | return -1; |
| 825 | } |
| 826 | } |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 827 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 828 | int ret = CheckInclude(package, &mDoc); |
| 829 | mDoc.clear(); |
| 830 | templates.clear(); |
| 831 | return ret; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 832 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 833 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 834 | int PageSet::CheckInclude(ZipArchive* package, xml_document<> *parentDoc) |
| 835 | { |
| 836 | xml_node<>* par; |
| 837 | xml_node<>* par2; |
| 838 | xml_node<>* chld; |
| 839 | xml_node<>* parent; |
| 840 | xml_node<>* child; |
| 841 | xml_node<>* xmltemplate; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 842 | xml_node<>* xmlstyle; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 843 | long len; |
| 844 | char* xmlFile = NULL; |
| 845 | string filename; |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 846 | xml_document<> *doc = NULL; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 847 | |
| 848 | par = parentDoc->first_node("recovery"); |
| 849 | if (!par) { |
| 850 | par = parentDoc->first_node("install"); |
| 851 | } |
| 852 | if (!par) { |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | par2 = par->first_node("include"); |
| 857 | if (!par2) |
| 858 | return 0; |
| 859 | chld = par2->first_node("xmlfile"); |
| 860 | while (chld != NULL) { |
| 861 | xml_attribute<>* attr = chld->first_attribute("name"); |
| 862 | if (!attr) |
| 863 | break; |
| 864 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 865 | if (!package) { |
| 866 | // We can try to load the XML directly... |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 867 | filename = TWRES; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 868 | filename += attr->value(); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 869 | } else { |
| 870 | filename += attr->value(); |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 871 | } |
| 872 | xmlFile = PageManager::LoadFileToBuffer(filename, package); |
| 873 | if (xmlFile == NULL) { |
| 874 | LOGERR("PageSet::CheckInclude unable to load '%s'\n", filename.c_str()); |
| 875 | return -1; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 876 | } |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 877 | |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 878 | doc = new xml_document<>(); |
| 879 | doc->parse<0>(xmlFile); |
| 880 | |
| 881 | parent = doc->first_node("recovery"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 882 | if (!parent) |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 883 | parent = doc->first_node("install"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 884 | |
| 885 | // Now, let's parse the XML |
| 886 | LOGINFO("Loading included resources...\n"); |
| 887 | child = parent->first_node("resources"); |
| 888 | if (child) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 889 | mResources->LoadResources(child, package, "theme"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 890 | |
| 891 | LOGINFO("Loading included variables...\n"); |
| 892 | child = parent->first_node("variables"); |
| 893 | if (child) |
| 894 | LoadVariables(child); |
| 895 | |
| 896 | LOGINFO("Loading mouse cursor...\n"); |
| 897 | child = parent->first_node("mousecursor"); |
| 898 | if(child) |
| 899 | PageManager::LoadCursorData(child); |
| 900 | |
| 901 | LOGINFO("Loading included pages...\n"); |
| 902 | // This may be NULL if no templates are present |
| 903 | xmltemplate = parent->first_node("templates"); |
| 904 | if (xmltemplate) |
| 905 | templates.push_back(xmltemplate); |
| 906 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 907 | // Load styles if present |
| 908 | xmlstyle = parent->first_node("styles"); |
| 909 | if (xmlstyle) |
| 910 | styles.push_back(xmlstyle); |
| 911 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 912 | child = parent->first_node("pages"); |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 913 | if (child && LoadPages(child)) |
| 914 | { |
| 915 | templates.pop_back(); |
| 916 | doc->clear(); |
| 917 | delete doc; |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 918 | free(xmlFile); |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 919 | return -1; |
| 920 | } |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 921 | |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 922 | if (CheckInclude(package, doc)) { |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 923 | doc->clear(); |
| 924 | delete doc; |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 925 | free(xmlFile); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 926 | return -1; |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 927 | } |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 928 | doc->clear(); |
| 929 | delete doc; |
| 930 | free(xmlFile); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 931 | |
| 932 | chld = chld->next_sibling("xmlfile"); |
| 933 | } |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 934 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 935 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | int PageSet::SetPage(std::string page) |
| 939 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 940 | Page* tmp = FindPage(page); |
| 941 | if (tmp) |
| 942 | { |
| 943 | if (mCurrentPage) mCurrentPage->SetPageFocus(0); |
| 944 | mCurrentPage = tmp; |
| 945 | mCurrentPage->SetPageFocus(1); |
| 946 | mCurrentPage->NotifyVarChange("", ""); |
| 947 | return 0; |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | LOGERR("Unable to locate page (%s)\n", page.c_str()); |
| 952 | } |
| 953 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | int PageSet::SetOverlay(Page* page) |
| 957 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 958 | if (page) { |
| 959 | if (mOverlays.size() >= 10) { |
| 960 | LOGERR("Too many overlays requested, max is 10.\n"); |
| 961 | return -1; |
| 962 | } |
Matt Mower | d411f8d | 2015-04-09 16:04:12 -0500 | [diff] [blame] | 963 | |
| 964 | std::vector<Page*>::iterator iter; |
| 965 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) { |
| 966 | if ((*iter)->GetName() == page->GetName()) { |
| 967 | mOverlays.erase(iter); |
| 968 | // SetOverlay() is (and should stay) the only function which |
| 969 | // adds to mOverlays. Then, each page can appear at most once. |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 974 | page->SetPageFocus(1); |
| 975 | page->NotifyVarChange("", ""); |
| 976 | |
| 977 | if (!mOverlays.empty()) |
| 978 | mOverlays.back()->SetPageFocus(0); |
| 979 | |
| 980 | mOverlays.push_back(page); |
| 981 | } else { |
| 982 | if (!mOverlays.empty()) { |
| 983 | mOverlays.back()->SetPageFocus(0); |
| 984 | mOverlays.pop_back(); |
| 985 | if (!mOverlays.empty()) |
| 986 | mOverlays.back()->SetPageFocus(1); |
| 987 | else if (mCurrentPage) |
| 988 | mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again |
| 989 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 990 | } |
| 991 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 992 | } |
| 993 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 994 | const ResourceManager* PageSet::GetResources() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 995 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 996 | return mResources; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | Page* PageSet::FindPage(std::string name) |
| 1000 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1001 | std::vector<Page*>::iterator iter; |
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 | for (iter = mPages.begin(); iter != mPages.end(); iter++) |
| 1004 | { |
| 1005 | if (name == (*iter)->GetName()) |
| 1006 | return (*iter); |
| 1007 | } |
| 1008 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | int PageSet::LoadVariables(xml_node<>* vars) |
| 1012 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1013 | xml_node<>* child; |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1014 | xml_attribute<> *name, *value, *persist; |
| 1015 | int p; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1016 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1017 | child = vars->first_node("variable"); |
| 1018 | while (child) |
| 1019 | { |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1020 | name = child->first_attribute("name"); |
| 1021 | value = child->first_attribute("value"); |
| 1022 | persist = child->first_attribute("persist"); |
| 1023 | if(name && value) |
| 1024 | { |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1025 | if (strcmp(name->value(), "tw_x_offset") == 0) { |
| 1026 | tw_x_offset = atoi(value->value()); |
| 1027 | child = child->next_sibling("variable"); |
| 1028 | continue; |
| 1029 | } |
| 1030 | if (strcmp(name->value(), "tw_y_offset") == 0) { |
| 1031 | tw_y_offset = atoi(value->value()); |
| 1032 | child = child->next_sibling("variable"); |
| 1033 | continue; |
| 1034 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1035 | p = persist ? atoi(persist->value()) : 0; |
Ethan Yonker | 96acb3d | 2014-08-05 09:20:30 -0500 | [diff] [blame] | 1036 | string temp = value->value(); |
| 1037 | string valstr = gui_parse_text(temp); |
| 1038 | |
| 1039 | if (valstr.find("+") != string::npos) { |
| 1040 | string val1str = valstr; |
| 1041 | val1str = val1str.substr(0, val1str.find('+')); |
| 1042 | string val2str = valstr; |
| 1043 | val2str = val2str.substr(val2str.find('+') + 1, string::npos); |
| 1044 | int val1 = atoi(val1str.c_str()); |
| 1045 | int val2 = atoi(val2str.c_str()); |
| 1046 | int val = val1 + val2; |
| 1047 | |
| 1048 | DataManager::SetValue(name->value(), val, p); |
| 1049 | } else if (valstr.find("-") != string::npos) { |
| 1050 | string val1str = valstr; |
| 1051 | val1str = val1str.substr(0, val1str.find('-')); |
| 1052 | string val2str = valstr; |
| 1053 | val2str = val2str.substr(val2str.find('-') + 1, string::npos); |
| 1054 | int val1 = atoi(val1str.c_str()); |
| 1055 | int val2 = atoi(val2str.c_str()); |
| 1056 | int val = val1 - val2; |
| 1057 | |
| 1058 | DataManager::SetValue(name->value(), val, p); |
| 1059 | } else { |
| 1060 | DataManager::SetValue(name->value(), valstr, p); |
| 1061 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 1062 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1063 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1064 | child = child->next_sibling("variable"); |
| 1065 | } |
| 1066 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 1069 | int PageSet::LoadPages(xml_node<>* pages) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1070 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1071 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1072 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1073 | if (!pages) |
| 1074 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1075 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1076 | child = pages->first_node("page"); |
| 1077 | while (child != NULL) |
| 1078 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 1079 | Page* page = new Page(child, &templates); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1080 | if (page->GetName().empty()) |
| 1081 | { |
| 1082 | LOGERR("Unable to process load page\n"); |
| 1083 | delete page; |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | mPages.push_back(page); |
| 1088 | } |
| 1089 | child = child->next_sibling("page"); |
| 1090 | } |
| 1091 | if (mPages.size() > 0) |
| 1092 | return 0; |
| 1093 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | int PageSet::IsCurrentPage(Page* page) |
| 1097 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1098 | return ((mCurrentPage && mCurrentPage == page) ? 1 : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1099 | } |
| 1100 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1101 | std::string PageSet::GetCurrentPage() const |
| 1102 | { |
| 1103 | return mCurrentPage ? mCurrentPage->GetName() : ""; |
| 1104 | } |
| 1105 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1106 | int PageSet::Render(void) |
| 1107 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1108 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1109 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1110 | ret = (mCurrentPage ? mCurrentPage->Render() : -1); |
| 1111 | if (ret < 0) |
| 1112 | return ret; |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1113 | |
| 1114 | std::vector<Page*>::iterator iter; |
| 1115 | |
| 1116 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) { |
| 1117 | ret = ((*iter) ? (*iter)->Render() : -1); |
| 1118 | if (ret < 0) |
| 1119 | return ret; |
| 1120 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1121 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | int PageSet::Update(void) |
| 1125 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1126 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1127 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1128 | ret = (mCurrentPage ? mCurrentPage->Update() : -1); |
| 1129 | if (ret < 0 || ret > 1) |
| 1130 | return ret; |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1131 | |
| 1132 | std::vector<Page*>::iterator iter; |
| 1133 | |
| 1134 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) { |
| 1135 | ret = ((*iter) ? (*iter)->Update() : -1); |
| 1136 | if (ret < 0) |
| 1137 | return ret; |
| 1138 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1139 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1143 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1144 | if (!mOverlays.empty()) |
| 1145 | return mOverlays.back()->NotifyTouch(state, x, y); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1146 | |
| 1147 | return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1148 | } |
| 1149 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1150 | int PageSet::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1151 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1152 | if (!mOverlays.empty()) |
| 1153 | return mOverlays.back()->NotifyKey(key, down); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1154 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1155 | return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | int PageSet::NotifyKeyboard(int key) |
| 1159 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1160 | if (!mOverlays.empty()) |
| 1161 | return mOverlays.back()->NotifyKeyboard(key); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1162 | |
| 1163 | return (mCurrentPage ? mCurrentPage->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | int PageSet::SetKeyBoardFocus(int inFocus) |
| 1167 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1168 | if (!mOverlays.empty()) |
| 1169 | return mOverlays.back()->SetKeyBoardFocus(inFocus); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1170 | |
| 1171 | return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | int PageSet::NotifyVarChange(std::string varName, std::string value) |
| 1175 | { |
Ethan Yonker | 1c27331 | 2015-03-16 12:18:56 -0500 | [diff] [blame] | 1176 | std::vector<Page*>::iterator iter; |
| 1177 | |
| 1178 | for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) |
| 1179 | (*iter)->NotifyVarChange(varName, value); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1180 | |
| 1181 | return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1184 | void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value) |
| 1185 | { |
| 1186 | mResources->AddStringResource(resource_source, resource_name, value); |
| 1187 | } |
| 1188 | |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1189 | char* PageManager::LoadFileToBuffer(std::string filename, ZipArchive* package) { |
| 1190 | size_t len; |
| 1191 | char* buffer = NULL; |
| 1192 | |
| 1193 | if (!package) { |
| 1194 | // We can try to load the XML directly... |
| 1195 | LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str()); |
| 1196 | struct stat st; |
| 1197 | if(stat(filename.c_str(),&st) != 0) { |
| 1198 | // This isn't always an error, sometimes we request files that don't exist. |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
| 1202 | len = (size_t)st.st_size; |
| 1203 | |
| 1204 | buffer = (char*) malloc(len + 1); |
| 1205 | if (!buffer) { |
| 1206 | LOGERR("PageManager::LoadFileToBuffer failed to malloc\n"); |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | |
| 1210 | int fd = open(filename.c_str(), O_RDONLY); |
| 1211 | if (fd == -1) { |
| 1212 | LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno)); |
| 1213 | free(buffer); |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | |
| 1217 | if (read(fd, buffer, len) < 0) { |
| 1218 | LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno)); |
| 1219 | free(buffer); |
| 1220 | close(fd); |
| 1221 | return NULL; |
| 1222 | } |
| 1223 | close(fd); |
| 1224 | } else { |
| 1225 | LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str()); |
| 1226 | const ZipEntry* zipentry = mzFindZipEntry(package, filename.c_str()); |
| 1227 | if (zipentry == NULL) { |
| 1228 | LOGERR("Unable to locate '%s' in zip file\n", filename.c_str()); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
| 1232 | // Allocate the buffer for the file |
| 1233 | len = mzGetZipEntryUncompLen(zipentry); |
| 1234 | buffer = (char*) malloc(len + 1); |
| 1235 | if (!buffer) |
| 1236 | return NULL; |
| 1237 | |
| 1238 | if (!mzExtractZipEntryToBuffer(package, zipentry, (unsigned char*) buffer)) { |
| 1239 | LOGERR("Unable to extract '%s'\n", filename.c_str()); |
| 1240 | free(buffer); |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | } |
| 1244 | // NULL-terminate the string |
| 1245 | buffer[len] = 0x00; |
| 1246 | return buffer; |
| 1247 | } |
| 1248 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1249 | void PageManager::LoadLanguageListDir(string dir) { |
| 1250 | if (!TWFunc::Path_Exists(dir)) { |
| 1251 | LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str()); |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | DIR *d = opendir(dir.c_str()); |
| 1256 | struct dirent *p; |
| 1257 | |
| 1258 | if (d == NULL) { |
| 1259 | LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno)); |
| 1260 | return; |
| 1261 | } |
| 1262 | |
| 1263 | while ((p = readdir(d))) { |
| 1264 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5) |
| 1265 | continue; |
| 1266 | |
| 1267 | string file = p->d_name; |
| 1268 | if (file.substr(strlen(p->d_name) - 4) != ".xml") |
| 1269 | continue; |
| 1270 | string path = dir + p->d_name; |
| 1271 | string file_no_extn = file.substr(0, strlen(p->d_name) - 4); |
| 1272 | struct language_struct language_entry; |
| 1273 | language_entry.filename = file_no_extn; |
| 1274 | char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL); |
| 1275 | if (xmlFile == NULL) { |
| 1276 | LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str()); |
| 1277 | continue; |
| 1278 | } |
| 1279 | xml_document<> *doc = new xml_document<>(); |
| 1280 | doc->parse<0>(xmlFile); |
| 1281 | |
| 1282 | xml_node<>* parent = doc->first_node("language"); |
| 1283 | if (!parent) { |
| 1284 | LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str()); |
| 1285 | } else { |
| 1286 | xml_node<>* child = parent->first_node("display"); |
| 1287 | if (child) { |
| 1288 | language_entry.displayvalue = child->value(); |
| 1289 | } else { |
| 1290 | LOGERR("No display value for '%s'\n", language_entry.filename.c_str()); |
| 1291 | language_entry.displayvalue = language_entry.filename; |
| 1292 | } |
| 1293 | Language_List.push_back(language_entry); |
| 1294 | } |
| 1295 | doc->clear(); |
| 1296 | delete doc; |
| 1297 | free(xmlFile); |
| 1298 | } |
| 1299 | closedir(d); |
| 1300 | } |
| 1301 | |
| 1302 | void PageManager::LoadLanguageList(ZipArchive* package) { |
| 1303 | Language_List.clear(); |
| 1304 | if (TWFunc::Path_Exists(TWRES "customlanguages")) |
| 1305 | TWFunc::removeDir(TWRES "customlanguages", true); |
| 1306 | if (package) { |
| 1307 | TWFunc::Recursive_Mkdir(TWRES "customlanguages"); |
| 1308 | struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
| 1309 | mzExtractRecursive(package, "languages", TWRES "customlanguages/", ×tamp, NULL, NULL, NULL); |
| 1310 | LoadLanguageListDir(TWRES "customlanguages/"); |
| 1311 | } else { |
| 1312 | LoadLanguageListDir(TWRES "languages/"); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | void PageManager::LoadLanguage(string filename) { |
| 1317 | string actual_filename; |
| 1318 | if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml")) |
| 1319 | actual_filename = TWRES "customlanguages/" + filename + ".xml"; |
| 1320 | else |
| 1321 | actual_filename = TWRES "languages/" + filename + ".xml"; |
| 1322 | char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL); |
| 1323 | if (xmlFile == NULL) |
| 1324 | LOGERR("Unable to load '%s'\n", actual_filename.c_str()); |
| 1325 | else { |
| 1326 | mCurrentSet->LoadLanguage(xmlFile, NULL); |
| 1327 | free(xmlFile); |
| 1328 | } |
| 1329 | PartitionManager.Translate_Partition_Display_Names(); |
| 1330 | } |
| 1331 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1332 | int PageManager::LoadPackage(std::string name, std::string package, std::string startpage) |
| 1333 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1334 | int fd; |
| 1335 | ZipArchive zip, *pZip = NULL; |
| 1336 | long len; |
| 1337 | char* xmlFile = NULL; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1338 | char* languageFile = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1339 | PageSet* pageSet = NULL; |
| 1340 | int ret; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1341 | MemMapping map; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1342 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1343 | mReloadTheme = false; |
| 1344 | mStartPage = startpage; |
| 1345 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1346 | // Open the XML file |
| 1347 | LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str()); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1348 | if (package.size() > 4 && package.substr(package.size() - 4) != ".zip") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1349 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1350 | LOGINFO("Load XML directly\n"); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1351 | tw_x_offset = TW_X_OFFSET; |
| 1352 | tw_y_offset = TW_Y_OFFSET; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1353 | LoadLanguageList(NULL); |
| 1354 | languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1355 | } |
| 1356 | else |
| 1357 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1358 | LOGINFO("Loading zip theme\n"); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1359 | tw_x_offset = 0; |
| 1360 | tw_y_offset = 0; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1361 | if (!TWFunc::Path_Exists(package)) |
| 1362 | return -1; |
| 1363 | if (sysMapFile(package.c_str(), &map) != 0) { |
| 1364 | LOGERR("Failed to map '%s'\n", package.c_str()); |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1365 | goto error; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1366 | } |
| 1367 | if (mzOpenZipArchive(map.addr, map.length, &zip)) { |
| 1368 | LOGERR("Unable to open zip archive '%s'\n", package.c_str()); |
| 1369 | sysReleaseMap(&map); |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1370 | goto error; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1371 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1372 | pZip = &zip; |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1373 | package = "ui.xml"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1374 | LoadLanguageList(pZip); |
| 1375 | languageFile = LoadFileToBuffer("languages/en.xml", pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1376 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1377 | |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1378 | xmlFile = LoadFileToBuffer(package, pZip); |
| 1379 | if (xmlFile == NULL) { |
| 1380 | goto error; |
| 1381 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1382 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1383 | // Before loading, mCurrentSet must be the loading package so we can find resources |
| 1384 | pageSet = mCurrentSet; |
| 1385 | mCurrentSet = new PageSet(xmlFile); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1386 | ret = mCurrentSet->Load(pZip, xmlFile, languageFile); |
| 1387 | if (languageFile) { |
| 1388 | free(languageFile); |
| 1389 | languageFile = NULL; |
| 1390 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1391 | if (ret == 0) |
| 1392 | { |
| 1393 | mCurrentSet->SetPage(startpage); |
| 1394 | mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet)); |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | LOGERR("Package %s failed to load.\n", name.c_str()); |
| 1399 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1400 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1401 | // The first successful package we loaded is the base |
| 1402 | if (mBaseSet == NULL) |
| 1403 | mBaseSet = mCurrentSet; |
| 1404 | |
| 1405 | mCurrentSet = pageSet; |
| 1406 | |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1407 | if (pZip) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1408 | mzCloseZipArchive(pZip); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1409 | sysReleaseMap(&map); |
| 1410 | } |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1411 | free(xmlFile); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1412 | if (languageFile) |
| 1413 | free(languageFile); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1414 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1415 | |
| 1416 | error: |
Ethan Yonker | 561c58d | 2015-10-05 08:48:22 -0500 | [diff] [blame] | 1417 | // Sometimes we get here without a real error |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1418 | if (pZip) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1419 | mzCloseZipArchive(pZip); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1420 | sysReleaseMap(&map); |
| 1421 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1422 | if (xmlFile) |
| 1423 | free(xmlFile); |
| 1424 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | PageSet* PageManager::FindPackage(std::string name) |
| 1428 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1429 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1430 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1431 | iter = mPageSets.find(name); |
| 1432 | if (iter != mPageSets.end()) |
| 1433 | return (*iter).second; |
| 1434 | |
| 1435 | LOGERR("Unable to locate package %s\n", name.c_str()); |
| 1436 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | PageSet* PageManager::SelectPackage(std::string name) |
| 1440 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1441 | LOGINFO("Switching packages (%s)\n", name.c_str()); |
| 1442 | PageSet* tmp; |
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 | tmp = FindPackage(name); |
| 1445 | if (tmp) |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1446 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1447 | mCurrentSet = tmp; |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1448 | mCurrentSet->NotifyVarChange("", ""); |
| 1449 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1450 | else |
| 1451 | LOGERR("Unable to find package.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1452 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1453 | return mCurrentSet; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | int PageManager::ReloadPackage(std::string name, std::string package) |
| 1457 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1458 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1459 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1460 | mReloadTheme = false; |
| 1461 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1462 | iter = mPageSets.find(name); |
| 1463 | if (iter == mPageSets.end()) |
| 1464 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1465 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1466 | if(mMouseCursor) |
| 1467 | mMouseCursor->ResetData(gr_fb_width(), gr_fb_height()); |
| 1468 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1469 | PageSet* set = (*iter).second; |
| 1470 | mPageSets.erase(iter); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1471 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1472 | if (LoadPackage(name, package, mStartPage) != 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1473 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1474 | LOGINFO("Failed to load package '%s'.\n", package.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1475 | mPageSets.insert(std::pair<std::string, PageSet*>(name, set)); |
| 1476 | return -1; |
| 1477 | } |
| 1478 | if (mCurrentSet == set) |
| 1479 | SelectPackage(name); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 1480 | if (mBaseSet == set) |
| 1481 | mBaseSet = mCurrentSet; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1482 | delete set; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1483 | GUIConsole::Translate_Now(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1484 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | void PageManager::ReleasePackage(std::string name) |
| 1488 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1489 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1490 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1491 | iter = mPageSets.find(name); |
| 1492 | if (iter == mPageSets.end()) |
| 1493 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1494 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1495 | PageSet* set = (*iter).second; |
| 1496 | mPageSets.erase(iter); |
| 1497 | delete set; |
| 1498 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1501 | int PageManager::RunReload() { |
| 1502 | int ret_val = 0; |
| 1503 | std::string theme_path; |
| 1504 | |
| 1505 | if (!mReloadTheme) |
| 1506 | return 0; |
| 1507 | |
| 1508 | mReloadTheme = false; |
| 1509 | theme_path = DataManager::GetSettingsStoragePath(); |
| 1510 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
| 1511 | LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str()); |
| 1512 | ret_val = 1; |
| 1513 | } |
| 1514 | |
| 1515 | theme_path += "/TWRP/theme/ui.zip"; |
| 1516 | if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0) |
| 1517 | { |
| 1518 | // Loading the custom theme failed - try loading the stock theme |
| 1519 | LOGINFO("Attempting to reload stock theme...\n"); |
| 1520 | if (ReloadPackage("TWRP", TWRES "ui.xml")) |
| 1521 | { |
| 1522 | LOGERR("Failed to load base packages.\n"); |
| 1523 | ret_val = 1; |
| 1524 | } |
| 1525 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1526 | if (ret_val == 0) { |
| 1527 | if (DataManager::GetStrValue("tw_language") != "en.xml") { |
| 1528 | LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str()); |
| 1529 | LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | // This makes the console re-translate |
| 1534 | last_message_count = 0; |
| 1535 | gConsole.clear(); |
| 1536 | gConsoleColor.clear(); |
| 1537 | |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1538 | return ret_val; |
| 1539 | } |
| 1540 | |
| 1541 | void PageManager::RequestReload() { |
| 1542 | mReloadTheme = true; |
| 1543 | } |
| 1544 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1545 | int PageManager::ChangePage(std::string name) |
| 1546 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1547 | DataManager::SetValue("tw_operation_state", 0); |
| 1548 | int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1); |
| 1549 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1550 | } |
| 1551 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1552 | std::string PageManager::GetCurrentPage() |
| 1553 | { |
| 1554 | return mCurrentSet ? mCurrentSet->GetCurrentPage() : ""; |
| 1555 | } |
| 1556 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1557 | int PageManager::ChangeOverlay(std::string name) |
| 1558 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1559 | if (name.empty()) |
| 1560 | return mCurrentSet->SetOverlay(NULL); |
| 1561 | else |
| 1562 | { |
| 1563 | Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL; |
| 1564 | return mCurrentSet->SetOverlay(page); |
| 1565 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 1568 | const ResourceManager* PageManager::GetResources() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1569 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 1570 | return (mCurrentSet ? mCurrentSet->GetResources() : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1571 | } |
| 1572 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1573 | int PageManager::IsCurrentPage(Page* page) |
| 1574 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1575 | return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | int PageManager::Render(void) |
| 1579 | { |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 1580 | if(blankTimer.isScreenOff()) |
| 1581 | return 0; |
| 1582 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1583 | int res = (mCurrentSet ? mCurrentSet->Render() : -1); |
| 1584 | if(mMouseCursor) |
| 1585 | mMouseCursor->Render(); |
| 1586 | return res; |
| 1587 | } |
| 1588 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1589 | HardwareKeyboard *PageManager::GetHardwareKeyboard() |
| 1590 | { |
| 1591 | if(!mHardwareKeyboard) |
| 1592 | mHardwareKeyboard = new HardwareKeyboard(); |
| 1593 | return mHardwareKeyboard; |
| 1594 | } |
| 1595 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 1596 | xml_node<>* PageManager::FindStyle(std::string name) |
| 1597 | { |
| 1598 | for (std::vector<xml_node<>*>::iterator itr = mCurrentSet->styles.begin(); itr != mCurrentSet->styles.end(); itr++) { |
| 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 | |
| 1657 | int PageManager::NotifyKeyboard(int key) |
| 1658 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1659 | return (mCurrentSet ? mCurrentSet->NotifyKeyboard(key) : -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 | } |