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" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | |
| 37 | #include <string> |
| 38 | |
| 39 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | #include "../minuitwrp/minui.h" |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 42 | #include "../minzip/SysUtil.h" |
| 43 | #include "../minzip/Zip.h" |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 44 | #include "gui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | #include "rapidxml.hpp" |
| 48 | #include "objects.hpp" |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 49 | #include "blanktimer.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 50 | |
| 51 | extern int gGuiRunning; |
| 52 | |
| 53 | std::map<std::string, PageSet*> PageManager::mPageSets; |
| 54 | PageSet* PageManager::mCurrentSet; |
| 55 | PageSet* PageManager::mBaseSet = NULL; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 56 | MouseCursor *PageManager::mMouseCursor = NULL; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 57 | HardwareKeyboard *PageManager::mHardwareKeyboard = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 59 | int tw_x_offset = 0; |
| 60 | int tw_y_offset = 0; |
| 61 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | // Helper routine to convert a string to a color declaration |
| 63 | int ConvertStrToColor(std::string str, COLOR* color) |
| 64 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | // Set the default, solid black |
| 66 | memset(color, 0, sizeof(COLOR)); |
| 67 | color->alpha = 255; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 69 | // Translate variables |
| 70 | DataManager::GetValue(str, str); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 71 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 72 | // Look for some defaults |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 73 | if (str == "black") return 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 74 | else if (str == "white") { color->red = color->green = color->blue = 255; return 0; } |
| 75 | else if (str == "red") { color->red = 255; return 0; } |
| 76 | else if (str == "green") { color->green = 255; return 0; } |
| 77 | else if (str == "blue") { color->blue = 255; return 0; } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 79 | // At this point, we require an RGB(A) color |
| 80 | if (str[0] != '#') |
| 81 | return -1; |
| 82 | |
| 83 | str.erase(0, 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 84 | |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 85 | int result; |
| 86 | if (str.size() >= 8) { |
| 87 | // We have alpha channel |
| 88 | string alpha = str.substr(6, 2); |
| 89 | result = strtol(alpha.c_str(), NULL, 16); |
| 90 | color->alpha = result & 0x000000FF; |
| 91 | str.resize(6); |
| 92 | result = strtol(str.c_str(), NULL, 16); |
| 93 | color->red = (result >> 16) & 0x000000FF; |
| 94 | color->green = (result >> 8) & 0x000000FF; |
| 95 | color->blue = result & 0x000000FF; |
| 96 | } else { |
| 97 | result = strtol(str.c_str(), NULL, 16); |
| 98 | color->red = (result >> 16) & 0x000000FF; |
| 99 | color->green = (result >> 8) & 0x000000FF; |
| 100 | color->blue = result & 0x000000FF; |
| 101 | } |
| 102 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Helper APIs |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 106 | std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue) |
| 107 | { |
| 108 | if (!element) |
| 109 | return defaultvalue; |
| 110 | |
| 111 | xml_attribute<>* attr = element->first_attribute(attrname); |
| 112 | return attr ? attr->value() : defaultvalue; |
| 113 | } |
| 114 | |
| 115 | int LoadAttrInt(xml_node<>* element, const char* attrname, int defaultvalue) |
| 116 | { |
| 117 | string value = LoadAttrString(element, attrname); |
| 118 | // resolve variables |
| 119 | DataManager::GetValue(value, value); |
| 120 | return value.empty() ? defaultvalue : atoi(value.c_str()); |
| 121 | } |
| 122 | |
| 123 | int LoadAttrIntScaleX(xml_node<>* element, const char* attrname, int defaultvalue) |
| 124 | { |
| 125 | return scale_theme_x(LoadAttrInt(element, attrname, defaultvalue)); |
| 126 | } |
| 127 | |
| 128 | int LoadAttrIntScaleY(xml_node<>* element, const char* attrname, int defaultvalue) |
| 129 | { |
| 130 | return scale_theme_y(LoadAttrInt(element, attrname, defaultvalue)); |
| 131 | } |
| 132 | |
| 133 | COLOR LoadAttrColor(xml_node<>* element, const char* attrname, COLOR defaultvalue) |
| 134 | { |
| 135 | string value = LoadAttrString(element, attrname); |
| 136 | // resolve variables |
| 137 | DataManager::GetValue(value, value); |
| 138 | COLOR ret = defaultvalue; |
| 139 | if (ConvertStrToColor(value, &ret) == 0) |
| 140 | return ret; |
| 141 | else |
| 142 | return defaultvalue; |
| 143 | } |
| 144 | |
| 145 | FontResource* LoadAttrFont(xml_node<>* element, const char* attrname) |
| 146 | { |
| 147 | std::string name = LoadAttrString(element, attrname, ""); |
| 148 | if (name.empty()) |
| 149 | return NULL; |
| 150 | else |
| 151 | return (FontResource*) PageManager::FindResource(name); |
| 152 | // TODO: make resource lookup type-safe |
| 153 | } |
| 154 | |
| 155 | ImageResource* LoadAttrImage(xml_node<>* element, const char* attrname) |
| 156 | { |
| 157 | std::string name = LoadAttrString(element, attrname, ""); |
| 158 | if (name.empty()) |
| 159 | return NULL; |
| 160 | else |
| 161 | return (ImageResource*) PageManager::FindResource(name); |
| 162 | // TODO: make resource lookup type-safe |
| 163 | } |
| 164 | |
| 165 | AnimationResource* LoadAttrAnimation(xml_node<>* element, const char* attrname) |
| 166 | { |
| 167 | std::string name = LoadAttrString(element, attrname, ""); |
| 168 | if (name.empty()) |
| 169 | return NULL; |
| 170 | else |
| 171 | return (AnimationResource*) PageManager::FindResource(name); |
| 172 | // TODO: make resource lookup type-safe |
| 173 | } |
| 174 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, RenderObject::Placement* placement /* = NULL */) |
| 176 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 177 | if (!node) |
| 178 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 179 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 180 | if (node->first_attribute("x")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 181 | *x = LoadAttrIntScaleX(node, "x") + tw_x_offset; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 183 | if (node->first_attribute("y")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 184 | *y = LoadAttrIntScaleY(node, "y") + tw_y_offset; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 186 | if (w && node->first_attribute("w")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 187 | *w = LoadAttrIntScaleX(node, "w"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 188 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 189 | if (h && node->first_attribute("h")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 190 | *h = LoadAttrIntScaleY(node, "h"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 191 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 192 | if (placement && node->first_attribute("placement")) |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 193 | *placement = (RenderObject::Placement) LoadAttrInt(node, "placement"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 195 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | int ActionObject::SetActionPos(int x, int y, int w, int h) |
| 199 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 200 | if (x < 0 || y < 0) |
| 201 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 203 | mActionX = x; |
| 204 | mActionY = y; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 205 | if (w || h) |
| 206 | { |
| 207 | mActionW = w; |
| 208 | mActionH = h; |
| 209 | } |
| 210 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 213 | Page::Page(xml_node<>* page, std::vector<xml_node<>*> *templates /* = NULL */) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 214 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 215 | mTouchStart = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 217 | // We can memset the whole structure, because the alpha channel is ignored |
| 218 | memset(&mBackground, 0, sizeof(COLOR)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 219 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 220 | // With NULL, we make a console-only display |
| 221 | if (!page) |
| 222 | { |
| 223 | mName = "console"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 225 | GUIConsole* element = new GUIConsole(NULL); |
| 226 | mRenders.push_back(element); |
| 227 | mActions.push_back(element); |
| 228 | return; |
| 229 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 230 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 231 | if (page->first_attribute("name")) |
| 232 | mName = page->first_attribute("name")->value(); |
| 233 | else |
| 234 | { |
| 235 | LOGERR("No page name attribute found!\n"); |
| 236 | return; |
| 237 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 238 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 239 | LOGINFO("Loading page %s\n", mName.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 240 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 241 | // This is a recursive routine for template handling |
| 242 | ProcessNode(page, templates); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 243 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 244 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 247 | Page::~Page() |
| 248 | { |
| 249 | for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr) |
| 250 | delete *itr; |
| 251 | } |
| 252 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 253 | bool Page::ProcessNode(xml_node<>* page, std::vector<xml_node<>*> *templates /* = NULL */, int depth /* = 0 */) |
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 (depth == 10) |
| 256 | { |
| 257 | LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n"); |
| 258 | return false; |
| 259 | } |
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 | // Let's retrieve the background value, if any |
| 262 | xml_node<>* bg = page->first_node("background"); |
| 263 | if (bg) |
| 264 | { |
| 265 | xml_attribute<>* attr = bg->first_attribute("color"); |
| 266 | if (attr) |
| 267 | { |
| 268 | std::string color = attr->value(); |
| 269 | ConvertStrToColor(color, &mBackground); |
| 270 | } |
| 271 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 272 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 273 | xml_node<>* child; |
| 274 | child = page->first_node("object"); |
| 275 | while (child) |
| 276 | { |
| 277 | if (!child->first_attribute("type")) |
| 278 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 279 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 280 | std::string type = child->first_attribute("type")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 281 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 282 | if (type == "text") |
| 283 | { |
| 284 | GUIText* element = new GUIText(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 285 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 286 | mRenders.push_back(element); |
| 287 | mActions.push_back(element); |
| 288 | } |
| 289 | else if (type == "image") |
| 290 | { |
| 291 | GUIImage* element = new GUIImage(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 292 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 293 | mRenders.push_back(element); |
| 294 | } |
| 295 | else if (type == "fill") |
| 296 | { |
| 297 | GUIFill* element = new GUIFill(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 298 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 299 | mRenders.push_back(element); |
| 300 | } |
| 301 | else if (type == "action") |
| 302 | { |
| 303 | GUIAction* element = new GUIAction(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 304 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 305 | mActions.push_back(element); |
| 306 | } |
| 307 | else if (type == "console") |
| 308 | { |
| 309 | GUIConsole* element = new GUIConsole(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 310 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 311 | mRenders.push_back(element); |
| 312 | mActions.push_back(element); |
| 313 | } |
| 314 | else if (type == "button") |
| 315 | { |
| 316 | GUIButton* element = new GUIButton(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 317 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 318 | mRenders.push_back(element); |
| 319 | mActions.push_back(element); |
| 320 | } |
| 321 | else if (type == "checkbox") |
| 322 | { |
| 323 | GUICheckbox* element = new GUICheckbox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 324 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 325 | mRenders.push_back(element); |
| 326 | mActions.push_back(element); |
| 327 | } |
| 328 | else if (type == "fileselector") |
| 329 | { |
| 330 | GUIFileSelector* element = new GUIFileSelector(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 331 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 332 | mRenders.push_back(element); |
| 333 | mActions.push_back(element); |
| 334 | } |
| 335 | else if (type == "animation") |
| 336 | { |
| 337 | GUIAnimation* element = new GUIAnimation(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 338 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 339 | mRenders.push_back(element); |
| 340 | } |
| 341 | else if (type == "progressbar") |
| 342 | { |
| 343 | GUIProgressBar* element = new GUIProgressBar(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 344 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 345 | mRenders.push_back(element); |
| 346 | mActions.push_back(element); |
| 347 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 348 | else if (type == "slider") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 349 | { |
| 350 | GUISlider* element = new GUISlider(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 351 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 352 | mRenders.push_back(element); |
| 353 | mActions.push_back(element); |
| 354 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 355 | else if (type == "slidervalue") |
| 356 | { |
| 357 | GUISliderValue *element = new GUISliderValue(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 358 | mObjects.push_back(element); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 359 | mRenders.push_back(element); |
| 360 | mActions.push_back(element); |
| 361 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 362 | else if (type == "listbox") |
| 363 | { |
| 364 | GUIListBox* element = new GUIListBox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 365 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 366 | mRenders.push_back(element); |
| 367 | mActions.push_back(element); |
| 368 | } |
| 369 | else if (type == "keyboard") |
| 370 | { |
| 371 | GUIKeyboard* element = new GUIKeyboard(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 372 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 373 | mRenders.push_back(element); |
| 374 | mActions.push_back(element); |
| 375 | } |
| 376 | else if (type == "input") |
| 377 | { |
| 378 | GUIInput* element = new GUIInput(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 379 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 380 | mRenders.push_back(element); |
| 381 | mActions.push_back(element); |
| 382 | mInputs.push_back(element); |
| 383 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 384 | else if (type == "partitionlist") |
| 385 | { |
| 386 | GUIPartitionList* element = new GUIPartitionList(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 387 | mObjects.push_back(element); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 388 | mRenders.push_back(element); |
| 389 | mActions.push_back(element); |
| 390 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 391 | else if (type == "template") |
| 392 | { |
| 393 | if (!templates || !child->first_attribute("name")) |
| 394 | { |
| 395 | LOGERR("Invalid template request.\n"); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | std::string name = child->first_attribute("name")->value(); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 400 | xml_node<>* node; |
| 401 | bool node_found = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 402 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 403 | // We need to find the correct template |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 404 | for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) { |
| 405 | node = (*itr)->first_node("template"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 406 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 407 | while (node) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 408 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 409 | if (!node->first_attribute("name")) |
| 410 | continue; |
| 411 | |
| 412 | if (name == node->first_attribute("name")->value()) |
| 413 | { |
| 414 | if (!ProcessNode(node, templates, depth + 1)) |
| 415 | return false; |
| 416 | else { |
| 417 | node_found = true; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | if (node_found) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 422 | break; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 423 | node = node->next_sibling("template"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 424 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | LOGERR("Unknown object type.\n"); |
| 431 | } |
| 432 | child = child->next_sibling("object"); |
| 433 | } |
| 434 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | int Page::Render(void) |
| 438 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 439 | // Render background |
| 440 | gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha); |
| 441 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 442 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 443 | // Render remaining objects |
| 444 | std::vector<RenderObject*>::iterator iter; |
| 445 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 446 | { |
| 447 | if ((*iter)->Render()) |
| 448 | LOGERR("A render request has failed.\n"); |
| 449 | } |
| 450 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | int Page::Update(void) |
| 454 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 455 | int retCode = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 456 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 457 | std::vector<RenderObject*>::iterator iter; |
| 458 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 459 | { |
| 460 | int ret = (*iter)->Update(); |
| 461 | if (ret < 0) |
| 462 | LOGERR("An update request has failed.\n"); |
| 463 | else if (ret > retCode) |
| 464 | retCode = ret; |
| 465 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 466 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 467 | return retCode; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int Page::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 471 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 472 | // By default, return 1 to ignore further touches if nobody is listening |
| 473 | int ret = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 474 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 475 | // Don't try to handle a lack of handlers |
| 476 | if (mActions.size() == 0) |
| 477 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 478 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 479 | // We record mTouchStart so we can pass all the touch stream to the same handler |
| 480 | if (state == TOUCH_START) |
| 481 | { |
| 482 | std::vector<ActionObject*>::reverse_iterator iter; |
| 483 | // We work backwards, from top-most element to bottom-most element |
| 484 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 485 | { |
| 486 | if ((*iter)->IsInRegion(x, y)) |
| 487 | { |
| 488 | mTouchStart = (*iter); |
| 489 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 490 | if (ret >= 0) |
| 491 | break; |
| 492 | mTouchStart = NULL; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | else if (state == TOUCH_RELEASE && mTouchStart != NULL) |
| 497 | { |
| 498 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 499 | mTouchStart = NULL; |
| 500 | } |
| 501 | else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL) |
| 502 | { |
| 503 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 504 | } |
| 505 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 508 | int Page::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 509 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 510 | std::vector<ActionObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 511 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 512 | // Don't try to handle a lack of handlers |
| 513 | if (mActions.size() == 0) |
| 514 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 515 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 516 | int ret = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 517 | // We work backwards, from top-most element to bottom-most element |
| 518 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 519 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 520 | ret = (*iter)->NotifyKey(key, down); |
| 521 | if (ret < 0) { |
| 522 | LOGERR("An action handler has returned an error\n"); |
| 523 | ret = 1; |
| 524 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 525 | } |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 526 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | int Page::NotifyKeyboard(int key) |
| 530 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 531 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 532 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 533 | // Don't try to handle a lack of handlers |
| 534 | if (mInputs.size() == 0) |
| 535 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 536 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 537 | // We work backwards, from top-most element to bottom-most element |
| 538 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 539 | { |
| 540 | int ret = (*iter)->NotifyKeyboard(key); |
| 541 | if (ret == 0) |
| 542 | return 0; |
| 543 | else if (ret < 0) |
| 544 | LOGERR("A keyboard handler has returned an error"); |
| 545 | } |
| 546 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | int Page::SetKeyBoardFocus(int inFocus) |
| 550 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 551 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 552 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 553 | // Don't try to handle a lack of handlers |
| 554 | if (mInputs.size() == 0) |
| 555 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 556 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 557 | // We work backwards, from top-most element to bottom-most element |
| 558 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 559 | { |
| 560 | int ret = (*iter)->SetInputFocus(inFocus); |
| 561 | if (ret == 0) |
| 562 | return 0; |
| 563 | else if (ret < 0) |
| 564 | LOGERR("An input focus handler has returned an error"); |
| 565 | } |
| 566 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void Page::SetPageFocus(int inFocus) |
| 570 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 571 | // Render remaining objects |
| 572 | std::vector<RenderObject*>::iterator iter; |
| 573 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 574 | (*iter)->SetPageFocus(inFocus); |
| 575 | |
| 576 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | int Page::NotifyVarChange(std::string varName, std::string value) |
| 580 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 581 | std::vector<GUIObject*>::iterator iter; |
| 582 | for (iter = mObjects.begin(); iter != mObjects.end(); ++iter) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 583 | { |
| 584 | if ((*iter)->NotifyVarChange(varName, value)) |
| 585 | LOGERR("An action handler errored on NotifyVarChange.\n"); |
| 586 | } |
| 587 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | PageSet::PageSet(char* xmlFile) |
| 591 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 592 | mResources = NULL; |
| 593 | mCurrentPage = NULL; |
| 594 | mOverlayPage = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 595 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 596 | mXmlFile = xmlFile; |
| 597 | if (xmlFile) |
| 598 | mDoc.parse<0>(mXmlFile); |
| 599 | else |
| 600 | mCurrentPage = new Page(NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | PageSet::~PageSet() |
| 604 | { |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 605 | for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr) |
| 606 | delete *itr; |
| 607 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 608 | delete mResources; |
| 609 | free(mXmlFile); |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 610 | |
| 611 | mDoc.clear(); |
| 612 | |
| 613 | for (std::vector<xml_document<>*>::iterator itr = mIncludedDocs.begin(); itr != mIncludedDocs.end(); ++itr) { |
| 614 | (*itr)->clear(); |
| 615 | delete *itr; |
| 616 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | int PageSet::Load(ZipArchive* package) |
| 620 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 621 | xml_node<>* parent; |
| 622 | xml_node<>* child; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 623 | xml_node<>* xmltemplate; |
| 624 | xml_node<>* blank_templates; |
| 625 | int pages_loaded = -1, ret; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 626 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 627 | parent = mDoc.first_node("recovery"); |
| 628 | if (!parent) |
| 629 | parent = mDoc.first_node("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 630 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 631 | set_scale_values(1, 1); // Reset any previous scaling values |
| 632 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 633 | // Now, let's parse the XML |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 634 | LOGINFO("Checking resolution...\n"); |
| 635 | child = parent->first_node("details"); |
| 636 | if (child) { |
| 637 | xml_node<>* resolution = child->first_node("resolution"); |
| 638 | if (resolution) { |
| 639 | xml_attribute<>* width_attr = resolution->first_attribute("width"); |
| 640 | xml_attribute<>* height_attr = resolution->first_attribute("height"); |
| 641 | xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling"); |
| 642 | if (width_attr && height_attr && !noscale_attr) { |
| 643 | int width = atoi(width_attr->value()); |
| 644 | int height = atoi(height_attr->value()); |
| 645 | int offx = 0, offy = 0; |
| 646 | #ifdef TW_ROUND_SCREEN |
| 647 | xml_node<>* roundscreen = child->first_node("roundscreen"); |
| 648 | if (roundscreen) { |
| 649 | LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n"); |
| 650 | xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x"); |
| 651 | xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y"); |
| 652 | if (offx_attr) { |
| 653 | offx = atoi(offx_attr->value()); |
| 654 | } |
| 655 | if (offy_attr) { |
| 656 | offy = atoi(offy_attr->value()); |
| 657 | } |
| 658 | } |
| 659 | #endif |
| 660 | if (width != 0 && height != 0) { |
| 661 | float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width; |
| 662 | float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height; |
| 663 | #ifdef TW_ROUND_SCREEN |
| 664 | float scale_off_w = (float)gr_fb_width() / (float)width; |
| 665 | float scale_off_h = (float)gr_fb_height() / (float)height; |
| 666 | tw_x_offset = offx * scale_off_w; |
| 667 | tw_y_offset = offy * scale_off_h; |
| 668 | #endif |
| 669 | if (scale_w != 1 || scale_h != 1) { |
| 670 | LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset); |
| 671 | set_scale_values(scale_w, scale_h); |
| 672 | } |
| 673 | } |
| 674 | } else { |
| 675 | LOGINFO("XML does not contain width and height, no scaling will be applied\n"); |
| 676 | } |
| 677 | } else { |
| 678 | LOGINFO("XML contains no resolution tag, no scaling will be applied.\n"); |
| 679 | } |
| 680 | } else { |
| 681 | LOGINFO("XML contains no details tag, no scaling will be applied.\n"); |
| 682 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 683 | LOGINFO("Loading resources...\n"); |
| 684 | child = parent->first_node("resources"); |
| 685 | if (child) |
| 686 | mResources = new ResourceManager(child, package); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 687 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 688 | LOGINFO("Loading variables...\n"); |
| 689 | child = parent->first_node("variables"); |
| 690 | if (child) |
| 691 | LoadVariables(child); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 692 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 693 | LOGINFO("Loading mouse cursor...\n"); |
| 694 | child = parent->first_node("mousecursor"); |
| 695 | if(child) |
| 696 | PageManager::LoadCursorData(child); |
| 697 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 698 | LOGINFO("Loading pages...\n"); |
| 699 | // This may be NULL if no templates are present |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 700 | xmltemplate = parent->first_node("templates"); |
| 701 | if (xmltemplate) |
| 702 | templates.push_back(xmltemplate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 703 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 704 | child = parent->first_node("pages"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 705 | if (child) { |
| 706 | if (LoadPages(child)) { |
| 707 | LOGERR("PageSet::Load returning -1\n"); |
| 708 | return -1; |
| 709 | } |
| 710 | } |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 711 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 712 | return CheckInclude(package, &mDoc); |
| 713 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 714 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 715 | int PageSet::CheckInclude(ZipArchive* package, xml_document<> *parentDoc) |
| 716 | { |
| 717 | xml_node<>* par; |
| 718 | xml_node<>* par2; |
| 719 | xml_node<>* chld; |
| 720 | xml_node<>* parent; |
| 721 | xml_node<>* child; |
| 722 | xml_node<>* xmltemplate; |
| 723 | long len; |
| 724 | char* xmlFile = NULL; |
| 725 | string filename; |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 726 | xml_document<> *doc = NULL; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 727 | |
| 728 | par = parentDoc->first_node("recovery"); |
| 729 | if (!par) { |
| 730 | par = parentDoc->first_node("install"); |
| 731 | } |
| 732 | if (!par) { |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | par2 = par->first_node("include"); |
| 737 | if (!par2) |
| 738 | return 0; |
| 739 | chld = par2->first_node("xmlfile"); |
| 740 | while (chld != NULL) { |
| 741 | xml_attribute<>* attr = chld->first_attribute("name"); |
| 742 | if (!attr) |
| 743 | break; |
| 744 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 745 | if (!package) { |
| 746 | // We can try to load the XML directly... |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 747 | filename = TWRES; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 748 | filename += attr->value(); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 749 | LOGINFO("PageSet::CheckInclude loading filename: '%s'\n", filename.c_str()); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 750 | struct stat st; |
| 751 | if(stat(filename.c_str(),&st) != 0) { |
| 752 | LOGERR("Unable to locate '%s'\n", filename.c_str()); |
| 753 | return -1; |
| 754 | } |
| 755 | |
| 756 | len = st.st_size; |
| 757 | xmlFile = (char*) malloc(len + 1); |
| 758 | if (!xmlFile) |
| 759 | return -1; |
| 760 | |
| 761 | int fd = open(filename.c_str(), O_RDONLY); |
| 762 | if (fd == -1) |
| 763 | return -1; |
| 764 | |
| 765 | read(fd, xmlFile, len); |
| 766 | close(fd); |
| 767 | } else { |
| 768 | filename += attr->value(); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 769 | LOGINFO("PageSet::CheckInclude loading filename: '%s'\n", filename.c_str()); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 770 | const ZipEntry* ui_xml = mzFindZipEntry(package, filename.c_str()); |
| 771 | if (ui_xml == NULL) |
| 772 | { |
| 773 | LOGERR("Unable to locate '%s' in zip file\n", filename.c_str()); |
| 774 | return -1; |
| 775 | } |
| 776 | |
| 777 | // Allocate the buffer for the file |
| 778 | len = mzGetZipEntryUncompLen(ui_xml); |
| 779 | xmlFile = (char*) malloc(len + 1); |
| 780 | if (!xmlFile) |
| 781 | return -1; |
| 782 | |
| 783 | if (!mzExtractZipEntryToBuffer(package, ui_xml, (unsigned char*) xmlFile)) |
| 784 | { |
| 785 | LOGERR("Unable to extract '%s'\n", filename.c_str()); |
| 786 | return -1; |
| 787 | } |
| 788 | } |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 789 | |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 790 | xmlFile[len] = '\0'; |
| 791 | doc = new xml_document<>(); |
| 792 | doc->parse<0>(xmlFile); |
| 793 | |
| 794 | parent = doc->first_node("recovery"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 795 | if (!parent) |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 796 | parent = doc->first_node("install"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 797 | |
| 798 | // Now, let's parse the XML |
| 799 | LOGINFO("Loading included resources...\n"); |
| 800 | child = parent->first_node("resources"); |
| 801 | if (child) |
| 802 | mResources->LoadResources(child, package); |
| 803 | |
| 804 | LOGINFO("Loading included variables...\n"); |
| 805 | child = parent->first_node("variables"); |
| 806 | if (child) |
| 807 | LoadVariables(child); |
| 808 | |
| 809 | LOGINFO("Loading mouse cursor...\n"); |
| 810 | child = parent->first_node("mousecursor"); |
| 811 | if(child) |
| 812 | PageManager::LoadCursorData(child); |
| 813 | |
| 814 | LOGINFO("Loading included pages...\n"); |
| 815 | // This may be NULL if no templates are present |
| 816 | xmltemplate = parent->first_node("templates"); |
| 817 | if (xmltemplate) |
| 818 | templates.push_back(xmltemplate); |
| 819 | |
| 820 | child = parent->first_node("pages"); |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 821 | if (child && LoadPages(child)) |
| 822 | { |
| 823 | templates.pop_back(); |
| 824 | doc->clear(); |
| 825 | delete doc; |
| 826 | return -1; |
| 827 | } |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 828 | |
Vojtech Bocek | e979abd | 2015-01-12 18:29:12 +0100 | [diff] [blame] | 829 | mIncludedDocs.push_back(doc); |
| 830 | |
| 831 | if (CheckInclude(package, doc)) |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 832 | return -1; |
| 833 | |
| 834 | chld = chld->next_sibling("xmlfile"); |
| 835 | } |
| 836 | |
| 837 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | int PageSet::SetPage(std::string page) |
| 841 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 842 | Page* tmp = FindPage(page); |
| 843 | if (tmp) |
| 844 | { |
| 845 | if (mCurrentPage) mCurrentPage->SetPageFocus(0); |
| 846 | mCurrentPage = tmp; |
| 847 | mCurrentPage->SetPageFocus(1); |
| 848 | mCurrentPage->NotifyVarChange("", ""); |
| 849 | return 0; |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | LOGERR("Unable to locate page (%s)\n", page.c_str()); |
| 854 | } |
| 855 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | int PageSet::SetOverlay(Page* page) |
| 859 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 860 | if (mOverlayPage) mOverlayPage->SetPageFocus(0); |
| 861 | mOverlayPage = page; |
| 862 | if (mOverlayPage) |
| 863 | { |
| 864 | mOverlayPage->SetPageFocus(1); |
| 865 | mOverlayPage->NotifyVarChange("", ""); |
| 866 | } |
| 867 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | Resource* PageSet::FindResource(std::string name) |
| 871 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 872 | return mResources ? mResources->FindResource(name) : NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | Page* PageSet::FindPage(std::string name) |
| 876 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 877 | std::vector<Page*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 878 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 879 | for (iter = mPages.begin(); iter != mPages.end(); iter++) |
| 880 | { |
| 881 | if (name == (*iter)->GetName()) |
| 882 | return (*iter); |
| 883 | } |
| 884 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int PageSet::LoadVariables(xml_node<>* vars) |
| 888 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 889 | xml_node<>* child; |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 890 | xml_attribute<> *name, *value, *persist; |
| 891 | int p; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 892 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 893 | child = vars->first_node("variable"); |
| 894 | while (child) |
| 895 | { |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 896 | name = child->first_attribute("name"); |
| 897 | value = child->first_attribute("value"); |
| 898 | persist = child->first_attribute("persist"); |
| 899 | if(name && value) |
| 900 | { |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 901 | if (strcmp(name->value(), "tw_x_offset") == 0) { |
| 902 | tw_x_offset = atoi(value->value()); |
| 903 | child = child->next_sibling("variable"); |
| 904 | continue; |
| 905 | } |
| 906 | if (strcmp(name->value(), "tw_y_offset") == 0) { |
| 907 | tw_y_offset = atoi(value->value()); |
| 908 | child = child->next_sibling("variable"); |
| 909 | continue; |
| 910 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 911 | p = persist ? atoi(persist->value()) : 0; |
Ethan Yonker | 96acb3d | 2014-08-05 09:20:30 -0500 | [diff] [blame] | 912 | string temp = value->value(); |
| 913 | string valstr = gui_parse_text(temp); |
| 914 | |
| 915 | if (valstr.find("+") != string::npos) { |
| 916 | string val1str = valstr; |
| 917 | val1str = val1str.substr(0, val1str.find('+')); |
| 918 | string val2str = valstr; |
| 919 | val2str = val2str.substr(val2str.find('+') + 1, string::npos); |
| 920 | int val1 = atoi(val1str.c_str()); |
| 921 | int val2 = atoi(val2str.c_str()); |
| 922 | int val = val1 + val2; |
| 923 | |
| 924 | DataManager::SetValue(name->value(), val, p); |
| 925 | } else if (valstr.find("-") != string::npos) { |
| 926 | string val1str = valstr; |
| 927 | val1str = val1str.substr(0, val1str.find('-')); |
| 928 | string val2str = valstr; |
| 929 | val2str = val2str.substr(val2str.find('-') + 1, string::npos); |
| 930 | int val1 = atoi(val1str.c_str()); |
| 931 | int val2 = atoi(val2str.c_str()); |
| 932 | int val = val1 - val2; |
| 933 | |
| 934 | DataManager::SetValue(name->value(), val, p); |
| 935 | } else { |
| 936 | DataManager::SetValue(name->value(), valstr, p); |
| 937 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 938 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 939 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 940 | child = child->next_sibling("variable"); |
| 941 | } |
| 942 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 943 | } |
| 944 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 945 | int PageSet::LoadPages(xml_node<>* pages) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 946 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 947 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 948 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 949 | if (!pages) |
| 950 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 951 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 952 | child = pages->first_node("page"); |
| 953 | while (child != NULL) |
| 954 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 955 | Page* page = new Page(child, &templates); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 956 | if (page->GetName().empty()) |
| 957 | { |
| 958 | LOGERR("Unable to process load page\n"); |
| 959 | delete page; |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | mPages.push_back(page); |
| 964 | } |
| 965 | child = child->next_sibling("page"); |
| 966 | } |
| 967 | if (mPages.size() > 0) |
| 968 | return 0; |
| 969 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | int PageSet::IsCurrentPage(Page* page) |
| 973 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 974 | return ((mCurrentPage && mCurrentPage == page) ? 1 : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | int PageSet::Render(void) |
| 978 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 979 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 980 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 981 | ret = (mCurrentPage ? mCurrentPage->Render() : -1); |
| 982 | if (ret < 0) |
| 983 | return ret; |
| 984 | ret = (mOverlayPage ? mOverlayPage->Render() : -1); |
| 985 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | int PageSet::Update(void) |
| 989 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 990 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 991 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 992 | ret = (mCurrentPage ? mCurrentPage->Update() : -1); |
| 993 | if (ret < 0 || ret > 1) |
| 994 | return ret; |
| 995 | ret = (mOverlayPage ? mOverlayPage->Update() : -1); |
| 996 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1000 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1001 | if (mOverlayPage) |
| 1002 | return (mOverlayPage->NotifyTouch(state, x, y)); |
| 1003 | |
| 1004 | return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1007 | int PageSet::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1008 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1009 | if (mOverlayPage) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1010 | return (mOverlayPage->NotifyKey(key, down)); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1011 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1012 | return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | int PageSet::NotifyKeyboard(int key) |
| 1016 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1017 | if (mOverlayPage) |
| 1018 | return (mOverlayPage->NotifyKeyboard(key)); |
| 1019 | |
| 1020 | return (mCurrentPage ? mCurrentPage->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | int PageSet::SetKeyBoardFocus(int inFocus) |
| 1024 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1025 | if (mOverlayPage) |
| 1026 | return (mOverlayPage->SetKeyBoardFocus(inFocus)); |
| 1027 | |
| 1028 | return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | int PageSet::NotifyVarChange(std::string varName, std::string value) |
| 1032 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1033 | if (mOverlayPage) |
| 1034 | mOverlayPage->NotifyVarChange(varName, value); |
| 1035 | |
| 1036 | return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | int PageManager::LoadPackage(std::string name, std::string package, std::string startpage) |
| 1040 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1041 | int fd; |
| 1042 | ZipArchive zip, *pZip = NULL; |
| 1043 | long len; |
| 1044 | char* xmlFile = NULL; |
| 1045 | PageSet* pageSet = NULL; |
| 1046 | int ret; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1047 | MemMapping map; |
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 | // Open the XML file |
| 1050 | LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str()); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1051 | if (package.size() > 4 && package.substr(package.size() - 4) != ".zip") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1052 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1053 | LOGINFO("Load XML directly\n"); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1054 | tw_x_offset = TW_X_OFFSET; |
| 1055 | tw_y_offset = TW_Y_OFFSET; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1056 | // We can try to load the XML directly... |
| 1057 | struct stat st; |
| 1058 | if(stat(package.c_str(),&st) != 0) |
| 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 | len = st.st_size; |
| 1062 | xmlFile = (char*) malloc(len + 1); |
| 1063 | if (!xmlFile) |
| 1064 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1065 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1066 | fd = open(package.c_str(), O_RDONLY); |
| 1067 | if (fd == -1) |
| 1068 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1069 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1070 | read(fd, xmlFile, len); |
| 1071 | close(fd); |
| 1072 | } |
| 1073 | else |
| 1074 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1075 | LOGINFO("Loading zip theme\n"); |
Ethan Yonker | 751a85e | 2014-12-12 16:59:10 -0600 | [diff] [blame] | 1076 | tw_x_offset = 0; |
| 1077 | tw_y_offset = 0; |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1078 | if (!TWFunc::Path_Exists(package)) |
| 1079 | return -1; |
| 1080 | if (sysMapFile(package.c_str(), &map) != 0) { |
| 1081 | LOGERR("Failed to map '%s'\n", package.c_str()); |
| 1082 | return -1; |
| 1083 | } |
| 1084 | if (mzOpenZipArchive(map.addr, map.length, &zip)) { |
| 1085 | LOGERR("Unable to open zip archive '%s'\n", package.c_str()); |
| 1086 | sysReleaseMap(&map); |
| 1087 | return -1; |
| 1088 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1089 | pZip = &zip; |
| 1090 | const ZipEntry* ui_xml = mzFindZipEntry(&zip, "ui.xml"); |
| 1091 | if (ui_xml == NULL) |
| 1092 | { |
| 1093 | LOGERR("Unable to locate ui.xml in zip file\n"); |
| 1094 | goto error; |
| 1095 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1096 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1097 | // Allocate the buffer for the file |
| 1098 | len = mzGetZipEntryUncompLen(ui_xml); |
| 1099 | xmlFile = (char*) malloc(len + 1); |
| 1100 | if (!xmlFile) |
| 1101 | goto error; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1102 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1103 | if (!mzExtractZipEntryToBuffer(&zip, ui_xml, (unsigned char*) xmlFile)) |
| 1104 | { |
| 1105 | LOGERR("Unable to extract ui.xml\n"); |
| 1106 | goto error; |
| 1107 | } |
| 1108 | } |
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 | // NULL-terminate the string |
| 1111 | xmlFile[len] = 0x00; |
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 | // Before loading, mCurrentSet must be the loading package so we can find resources |
| 1114 | pageSet = mCurrentSet; |
| 1115 | mCurrentSet = new PageSet(xmlFile); |
| 1116 | |
| 1117 | ret = mCurrentSet->Load(pZip); |
| 1118 | if (ret == 0) |
| 1119 | { |
| 1120 | mCurrentSet->SetPage(startpage); |
| 1121 | mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet)); |
| 1122 | } |
| 1123 | else |
| 1124 | { |
| 1125 | LOGERR("Package %s failed to load.\n", name.c_str()); |
| 1126 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1127 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1128 | // The first successful package we loaded is the base |
| 1129 | if (mBaseSet == NULL) |
| 1130 | mBaseSet = mCurrentSet; |
| 1131 | |
| 1132 | mCurrentSet = pageSet; |
| 1133 | |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1134 | if (pZip) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1135 | mzCloseZipArchive(pZip); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1136 | sysReleaseMap(&map); |
| 1137 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1138 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1139 | |
| 1140 | error: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1141 | LOGERR("An internal error has occurred.\n"); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1142 | if (pZip) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1143 | mzCloseZipArchive(pZip); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1144 | sysReleaseMap(&map); |
| 1145 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1146 | if (xmlFile) |
| 1147 | free(xmlFile); |
| 1148 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | PageSet* PageManager::FindPackage(std::string name) |
| 1152 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1153 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1154 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1155 | iter = mPageSets.find(name); |
| 1156 | if (iter != mPageSets.end()) |
| 1157 | return (*iter).second; |
| 1158 | |
| 1159 | LOGERR("Unable to locate package %s\n", name.c_str()); |
| 1160 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | PageSet* PageManager::SelectPackage(std::string name) |
| 1164 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1165 | LOGINFO("Switching packages (%s)\n", name.c_str()); |
| 1166 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1167 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1168 | tmp = FindPackage(name); |
| 1169 | if (tmp) |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1170 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1171 | mCurrentSet = tmp; |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1172 | mCurrentSet->NotifyVarChange("", ""); |
| 1173 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1174 | else |
| 1175 | LOGERR("Unable to find package.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1176 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1177 | return mCurrentSet; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | int PageManager::ReloadPackage(std::string name, std::string package) |
| 1181 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1182 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1183 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1184 | iter = mPageSets.find(name); |
| 1185 | if (iter == mPageSets.end()) |
| 1186 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1187 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1188 | if(mMouseCursor) |
| 1189 | mMouseCursor->ResetData(gr_fb_width(), gr_fb_height()); |
| 1190 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1191 | PageSet* set = (*iter).second; |
| 1192 | mPageSets.erase(iter); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1193 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1194 | if (LoadPackage(name, package, "main") != 0) |
| 1195 | { |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 1196 | LOGERR("Failed to load package '%s'.\n", package.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1197 | mPageSets.insert(std::pair<std::string, PageSet*>(name, set)); |
| 1198 | return -1; |
| 1199 | } |
| 1200 | if (mCurrentSet == set) |
| 1201 | SelectPackage(name); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 1202 | if (mBaseSet == set) |
| 1203 | mBaseSet = mCurrentSet; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1204 | delete set; |
| 1205 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | void PageManager::ReleasePackage(std::string name) |
| 1209 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1210 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1211 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1212 | iter = mPageSets.find(name); |
| 1213 | if (iter == mPageSets.end()) |
| 1214 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1215 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1216 | PageSet* set = (*iter).second; |
| 1217 | mPageSets.erase(iter); |
| 1218 | delete set; |
| 1219 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | int PageManager::ChangePage(std::string name) |
| 1223 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1224 | DataManager::SetValue("tw_operation_state", 0); |
| 1225 | int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1); |
| 1226 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | int PageManager::ChangeOverlay(std::string name) |
| 1230 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1231 | if (name.empty()) |
| 1232 | return mCurrentSet->SetOverlay(NULL); |
| 1233 | else |
| 1234 | { |
| 1235 | Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL; |
| 1236 | return mCurrentSet->SetOverlay(page); |
| 1237 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | Resource* PageManager::FindResource(std::string name) |
| 1241 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1242 | return (mCurrentSet ? mCurrentSet->FindResource(name) : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | Resource* PageManager::FindResource(std::string package, std::string name) |
| 1246 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1247 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1248 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1249 | tmp = FindPackage(name); |
| 1250 | return (tmp ? tmp->FindResource(name) : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | int PageManager::SwitchToConsole(void) |
| 1254 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1255 | PageSet* console = new PageSet(NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1256 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1257 | mCurrentSet = console; |
| 1258 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 1261 | int PageManager::EndConsole(void) |
| 1262 | { |
| 1263 | if (mCurrentSet && mBaseSet) { |
| 1264 | delete mCurrentSet; |
| 1265 | mCurrentSet = mBaseSet; |
| 1266 | return 0; |
| 1267 | } |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1271 | int PageManager::IsCurrentPage(Page* page) |
| 1272 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1273 | return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | int PageManager::Render(void) |
| 1277 | { |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1278 | int res = (mCurrentSet ? mCurrentSet->Render() : -1); |
| 1279 | if(mMouseCursor) |
| 1280 | mMouseCursor->Render(); |
| 1281 | return res; |
| 1282 | } |
| 1283 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1284 | HardwareKeyboard *PageManager::GetHardwareKeyboard() |
| 1285 | { |
| 1286 | if(!mHardwareKeyboard) |
| 1287 | mHardwareKeyboard = new HardwareKeyboard(); |
| 1288 | return mHardwareKeyboard; |
| 1289 | } |
| 1290 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1291 | MouseCursor *PageManager::GetMouseCursor() |
| 1292 | { |
| 1293 | if(!mMouseCursor) |
| 1294 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 1295 | return mMouseCursor; |
| 1296 | } |
| 1297 | |
| 1298 | void PageManager::LoadCursorData(xml_node<>* node) |
| 1299 | { |
| 1300 | if(!mMouseCursor) |
| 1301 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 1302 | |
| 1303 | mMouseCursor->LoadData(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | int PageManager::Update(void) |
| 1307 | { |
that | fb759d4 | 2015-01-11 12:16:53 +0100 | [diff] [blame] | 1308 | if(blankTimer.isScreenOff()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1309 | return 0; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1310 | |
| 1311 | int res = (mCurrentSet ? mCurrentSet->Update() : -1); |
| 1312 | |
| 1313 | if(mMouseCursor) |
| 1314 | { |
| 1315 | int c_res = mMouseCursor->Update(); |
| 1316 | if(c_res > res) |
| 1317 | res = c_res; |
| 1318 | } |
| 1319 | return res; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1323 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1324 | return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1325 | } |
| 1326 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1327 | int PageManager::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1328 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1329 | return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | int PageManager::NotifyKeyboard(int key) |
| 1333 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1334 | return (mCurrentSet ? mCurrentSet->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | int PageManager::SetKeyBoardFocus(int inFocus) |
| 1338 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1339 | return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | int PageManager::NotifyVarChange(std::string varName, std::string value) |
| 1343 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1344 | return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | extern "C" void gui_notifyVarChange(const char *name, const char* value) |
| 1348 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1349 | if (!gGuiRunning) |
| 1350 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1351 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1352 | PageManager::NotifyVarChange(name, value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1353 | } |