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