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 | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 42 | #if (ANDROID_VERSION >= 5) |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 43 | #include "../minzip/SysUtil.h" |
| 44 | #include "../minzip/Zip.h" |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 45 | #else |
| 46 | #include "../minzipold/SysUtil.h" |
| 47 | #include "../minzipold/Zip.h" |
| 48 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | #include "rapidxml.hpp" |
| 52 | #include "objects.hpp" |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 53 | #ifndef TW_NO_SCREEN_TIMEOUT |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 54 | #include "blanktimer.hpp" |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 55 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | |
| 57 | extern int gGuiRunning; |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 58 | #ifndef TW_NO_SCREEN_TIMEOUT |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 59 | extern blanktimer blankTimer; |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 60 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
| 62 | std::map<std::string, PageSet*> PageManager::mPageSets; |
| 63 | PageSet* PageManager::mCurrentSet; |
| 64 | PageSet* PageManager::mBaseSet = NULL; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 65 | MouseCursor *PageManager::mMouseCursor = NULL; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 66 | HardwareKeyboard *PageManager::mHardwareKeyboard = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 67 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | // Helper routine to convert a string to a color declaration |
| 69 | int ConvertStrToColor(std::string str, COLOR* color) |
| 70 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 71 | // Set the default, solid black |
| 72 | memset(color, 0, sizeof(COLOR)); |
| 73 | color->alpha = 255; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 75 | // Translate variables |
| 76 | DataManager::GetValue(str, str); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 77 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 78 | // Look for some defaults |
| 79 | if (str == "black") return 0; |
| 80 | else if (str == "white") { color->red = color->green = color->blue = 255; return 0; } |
| 81 | else if (str == "red") { color->red = 255; return 0; } |
| 82 | else if (str == "green") { color->green = 255; return 0; } |
| 83 | else if (str == "blue") { color->blue = 255; return 0; } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 84 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | // At this point, we require an RGB(A) color |
| 86 | if (str[0] != '#') |
| 87 | return -1; |
| 88 | |
| 89 | str.erase(0, 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 90 | |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 91 | int result; |
| 92 | if (str.size() >= 8) { |
| 93 | // We have alpha channel |
| 94 | string alpha = str.substr(6, 2); |
| 95 | result = strtol(alpha.c_str(), NULL, 16); |
| 96 | color->alpha = result & 0x000000FF; |
| 97 | str.resize(6); |
| 98 | result = strtol(str.c_str(), NULL, 16); |
| 99 | color->red = (result >> 16) & 0x000000FF; |
| 100 | color->green = (result >> 8) & 0x000000FF; |
| 101 | color->blue = result & 0x000000FF; |
| 102 | } else { |
| 103 | result = strtol(str.c_str(), NULL, 16); |
| 104 | color->red = (result >> 16) & 0x000000FF; |
| 105 | color->green = (result >> 8) & 0x000000FF; |
| 106 | color->blue = result & 0x000000FF; |
| 107 | } |
| 108 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Helper APIs |
| 112 | bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, RenderObject::Placement* placement /* = NULL */) |
| 113 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 114 | if (!node) |
| 115 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 117 | std::string value; |
| 118 | if (node->first_attribute("x")) |
| 119 | { |
| 120 | value = node->first_attribute("x")->value(); |
| 121 | DataManager::GetValue(value, value); |
| 122 | *x = atol(value.c_str()); |
| 123 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | if (node->first_attribute("y")) |
| 126 | { |
| 127 | value = node->first_attribute("y")->value(); |
| 128 | DataManager::GetValue(value, value); |
| 129 | *y = atol(value.c_str()); |
| 130 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | if (w && node->first_attribute("w")) |
| 133 | { |
| 134 | value = node->first_attribute("w")->value(); |
| 135 | DataManager::GetValue(value, value); |
| 136 | *w = atol(value.c_str()); |
| 137 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 138 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 139 | if (h && node->first_attribute("h")) |
| 140 | { |
| 141 | value = node->first_attribute("h")->value(); |
| 142 | DataManager::GetValue(value, value); |
| 143 | *h = atol(value.c_str()); |
| 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; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 565 | for (std::vector<xml_node<>*>::iterator itr2 = templates.begin(); itr2 != templates.end(); ++itr2) |
| 566 | delete *itr2; |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 567 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 568 | delete mResources; |
| 569 | free(mXmlFile); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | int PageSet::Load(ZipArchive* package) |
| 573 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 574 | xml_node<>* parent; |
| 575 | xml_node<>* child; |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 576 | xml_node<>* xmltemplate; |
| 577 | xml_node<>* blank_templates; |
| 578 | int pages_loaded = -1, ret; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 579 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 580 | parent = mDoc.first_node("recovery"); |
| 581 | if (!parent) |
| 582 | parent = mDoc.first_node("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 583 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 584 | // Now, let's parse the XML |
| 585 | LOGINFO("Loading resources...\n"); |
| 586 | child = parent->first_node("resources"); |
| 587 | if (child) |
| 588 | mResources = new ResourceManager(child, package); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 589 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 590 | LOGINFO("Loading variables...\n"); |
| 591 | child = parent->first_node("variables"); |
| 592 | if (child) |
| 593 | LoadVariables(child); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 594 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 595 | LOGINFO("Loading mouse cursor...\n"); |
| 596 | child = parent->first_node("mousecursor"); |
| 597 | if(child) |
| 598 | PageManager::LoadCursorData(child); |
| 599 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 600 | LOGINFO("Loading pages...\n"); |
| 601 | // This may be NULL if no templates are present |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 602 | xmltemplate = parent->first_node("templates"); |
| 603 | if (xmltemplate) |
| 604 | templates.push_back(xmltemplate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 605 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 606 | child = parent->first_node("pages"); |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 607 | if (child) { |
| 608 | if (LoadPages(child)) { |
| 609 | LOGERR("PageSet::Load returning -1\n"); |
| 610 | return -1; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return CheckInclude(package, &mDoc); |
| 615 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 616 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 617 | int PageSet::CheckInclude(ZipArchive* package, xml_document<> *parentDoc) |
| 618 | { |
| 619 | xml_node<>* par; |
| 620 | xml_node<>* par2; |
| 621 | xml_node<>* chld; |
| 622 | xml_node<>* parent; |
| 623 | xml_node<>* child; |
| 624 | xml_node<>* xmltemplate; |
| 625 | long len; |
| 626 | char* xmlFile = NULL; |
| 627 | string filename; |
| 628 | xml_document<> doc; |
| 629 | |
| 630 | par = parentDoc->first_node("recovery"); |
| 631 | if (!par) { |
| 632 | par = parentDoc->first_node("install"); |
| 633 | } |
| 634 | if (!par) { |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | par2 = par->first_node("include"); |
| 639 | if (!par2) |
| 640 | return 0; |
| 641 | chld = par2->first_node("xmlfile"); |
| 642 | while (chld != NULL) { |
| 643 | xml_attribute<>* attr = chld->first_attribute("name"); |
| 644 | if (!attr) |
| 645 | break; |
| 646 | |
| 647 | LOGINFO("PageSet::CheckInclude loading filename: '%s'\n", filename.c_str()); |
| 648 | if (!package) { |
| 649 | // We can try to load the XML directly... |
| 650 | filename = "/res/"; |
| 651 | filename += attr->value(); |
| 652 | struct stat st; |
| 653 | if(stat(filename.c_str(),&st) != 0) { |
| 654 | LOGERR("Unable to locate '%s'\n", filename.c_str()); |
| 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | len = st.st_size; |
| 659 | xmlFile = (char*) malloc(len + 1); |
| 660 | if (!xmlFile) |
| 661 | return -1; |
| 662 | |
| 663 | int fd = open(filename.c_str(), O_RDONLY); |
| 664 | if (fd == -1) |
| 665 | return -1; |
| 666 | |
| 667 | read(fd, xmlFile, len); |
| 668 | close(fd); |
| 669 | } else { |
| 670 | filename += attr->value(); |
| 671 | const ZipEntry* ui_xml = mzFindZipEntry(package, filename.c_str()); |
| 672 | if (ui_xml == NULL) |
| 673 | { |
| 674 | LOGERR("Unable to locate '%s' in zip file\n", filename.c_str()); |
| 675 | return -1; |
| 676 | } |
| 677 | |
| 678 | // Allocate the buffer for the file |
| 679 | len = mzGetZipEntryUncompLen(ui_xml); |
| 680 | xmlFile = (char*) malloc(len + 1); |
| 681 | if (!xmlFile) |
| 682 | return -1; |
| 683 | |
| 684 | if (!mzExtractZipEntryToBuffer(package, ui_xml, (unsigned char*) xmlFile)) |
| 685 | { |
| 686 | LOGERR("Unable to extract '%s'\n", filename.c_str()); |
| 687 | return -1; |
| 688 | } |
| 689 | } |
| 690 | doc.parse<0>(xmlFile); |
| 691 | |
| 692 | parent = doc.first_node("recovery"); |
| 693 | if (!parent) |
| 694 | parent = doc.first_node("install"); |
| 695 | |
| 696 | // Now, let's parse the XML |
| 697 | LOGINFO("Loading included resources...\n"); |
| 698 | child = parent->first_node("resources"); |
| 699 | if (child) |
| 700 | mResources->LoadResources(child, package); |
| 701 | |
| 702 | LOGINFO("Loading included variables...\n"); |
| 703 | child = parent->first_node("variables"); |
| 704 | if (child) |
| 705 | LoadVariables(child); |
| 706 | |
| 707 | LOGINFO("Loading mouse cursor...\n"); |
| 708 | child = parent->first_node("mousecursor"); |
| 709 | if(child) |
| 710 | PageManager::LoadCursorData(child); |
| 711 | |
| 712 | LOGINFO("Loading included pages...\n"); |
| 713 | // This may be NULL if no templates are present |
| 714 | xmltemplate = parent->first_node("templates"); |
| 715 | if (xmltemplate) |
| 716 | templates.push_back(xmltemplate); |
| 717 | |
| 718 | child = parent->first_node("pages"); |
| 719 | if (child) |
| 720 | if (LoadPages(child)) |
| 721 | return -1; |
| 722 | |
| 723 | if (CheckInclude(package, &doc)) |
| 724 | return -1; |
| 725 | |
| 726 | chld = chld->next_sibling("xmlfile"); |
| 727 | } |
| 728 | |
| 729 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | int PageSet::SetPage(std::string page) |
| 733 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 734 | Page* tmp = FindPage(page); |
| 735 | if (tmp) |
| 736 | { |
| 737 | if (mCurrentPage) mCurrentPage->SetPageFocus(0); |
| 738 | mCurrentPage = tmp; |
| 739 | mCurrentPage->SetPageFocus(1); |
| 740 | mCurrentPage->NotifyVarChange("", ""); |
| 741 | return 0; |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | LOGERR("Unable to locate page (%s)\n", page.c_str()); |
| 746 | } |
| 747 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | int PageSet::SetOverlay(Page* page) |
| 751 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 752 | if (mOverlayPage) mOverlayPage->SetPageFocus(0); |
| 753 | mOverlayPage = page; |
| 754 | if (mOverlayPage) |
| 755 | { |
| 756 | mOverlayPage->SetPageFocus(1); |
| 757 | mOverlayPage->NotifyVarChange("", ""); |
| 758 | } |
| 759 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | Resource* PageSet::FindResource(std::string name) |
| 763 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 764 | return mResources ? mResources->FindResource(name) : NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | Page* PageSet::FindPage(std::string name) |
| 768 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 769 | std::vector<Page*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 770 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 771 | for (iter = mPages.begin(); iter != mPages.end(); iter++) |
| 772 | { |
| 773 | if (name == (*iter)->GetName()) |
| 774 | return (*iter); |
| 775 | } |
| 776 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | int PageSet::LoadVariables(xml_node<>* vars) |
| 780 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 781 | xml_node<>* child; |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 782 | xml_attribute<> *name, *value, *persist; |
| 783 | int p; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 784 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 785 | child = vars->first_node("variable"); |
| 786 | while (child) |
| 787 | { |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 788 | name = child->first_attribute("name"); |
| 789 | value = child->first_attribute("value"); |
| 790 | persist = child->first_attribute("persist"); |
| 791 | if(name && value) |
| 792 | { |
| 793 | p = persist ? atoi(persist->value()) : 0; |
Ethan Yonker | 96acb3d | 2014-08-05 09:20:30 -0500 | [diff] [blame] | 794 | string temp = value->value(); |
| 795 | string valstr = gui_parse_text(temp); |
| 796 | |
| 797 | if (valstr.find("+") != string::npos) { |
| 798 | string val1str = valstr; |
| 799 | val1str = val1str.substr(0, val1str.find('+')); |
| 800 | string val2str = valstr; |
| 801 | val2str = val2str.substr(val2str.find('+') + 1, string::npos); |
| 802 | int val1 = atoi(val1str.c_str()); |
| 803 | int val2 = atoi(val2str.c_str()); |
| 804 | int val = val1 + val2; |
| 805 | |
| 806 | DataManager::SetValue(name->value(), val, p); |
| 807 | } else if (valstr.find("-") != string::npos) { |
| 808 | string val1str = valstr; |
| 809 | val1str = val1str.substr(0, val1str.find('-')); |
| 810 | string val2str = valstr; |
| 811 | val2str = val2str.substr(val2str.find('-') + 1, string::npos); |
| 812 | int val1 = atoi(val1str.c_str()); |
| 813 | int val2 = atoi(val2str.c_str()); |
| 814 | int val = val1 - val2; |
| 815 | |
| 816 | DataManager::SetValue(name->value(), val, p); |
| 817 | } else { |
| 818 | DataManager::SetValue(name->value(), valstr, p); |
| 819 | } |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 820 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 821 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 822 | child = child->next_sibling("variable"); |
| 823 | } |
| 824 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 825 | } |
| 826 | |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 827 | int PageSet::LoadPages(xml_node<>* pages) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 828 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 829 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 830 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 831 | if (!pages) |
| 832 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 833 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 834 | child = pages->first_node("page"); |
| 835 | while (child != NULL) |
| 836 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 837 | Page* page = new Page(child, &templates); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 838 | if (page->GetName().empty()) |
| 839 | { |
| 840 | LOGERR("Unable to process load page\n"); |
| 841 | delete page; |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | mPages.push_back(page); |
| 846 | } |
| 847 | child = child->next_sibling("page"); |
| 848 | } |
| 849 | if (mPages.size() > 0) |
| 850 | return 0; |
| 851 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | int PageSet::IsCurrentPage(Page* page) |
| 855 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 856 | return ((mCurrentPage && mCurrentPage == page) ? 1 : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | int PageSet::Render(void) |
| 860 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 861 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 862 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 863 | ret = (mCurrentPage ? mCurrentPage->Render() : -1); |
| 864 | if (ret < 0) |
| 865 | return ret; |
| 866 | ret = (mOverlayPage ? mOverlayPage->Render() : -1); |
| 867 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | int PageSet::Update(void) |
| 871 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 872 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 873 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 874 | ret = (mCurrentPage ? mCurrentPage->Update() : -1); |
| 875 | if (ret < 0 || ret > 1) |
| 876 | return ret; |
| 877 | ret = (mOverlayPage ? mOverlayPage->Update() : -1); |
| 878 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 882 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 883 | if (mOverlayPage) |
| 884 | return (mOverlayPage->NotifyTouch(state, x, y)); |
| 885 | |
| 886 | return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 889 | int PageSet::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 890 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 891 | if (mOverlayPage) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 892 | return (mOverlayPage->NotifyKey(key, down)); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 893 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 894 | return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | int PageSet::NotifyKeyboard(int key) |
| 898 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 899 | if (mOverlayPage) |
| 900 | return (mOverlayPage->NotifyKeyboard(key)); |
| 901 | |
| 902 | return (mCurrentPage ? mCurrentPage->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | int PageSet::SetKeyBoardFocus(int inFocus) |
| 906 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 907 | if (mOverlayPage) |
| 908 | return (mOverlayPage->SetKeyBoardFocus(inFocus)); |
| 909 | |
| 910 | return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | int PageSet::NotifyVarChange(std::string varName, std::string value) |
| 914 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 915 | if (mOverlayPage) |
| 916 | mOverlayPage->NotifyVarChange(varName, value); |
| 917 | |
| 918 | return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | int PageManager::LoadPackage(std::string name, std::string package, std::string startpage) |
| 922 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 923 | int fd; |
| 924 | ZipArchive zip, *pZip = NULL; |
| 925 | long len; |
| 926 | char* xmlFile = NULL; |
| 927 | PageSet* pageSet = NULL; |
| 928 | int ret; |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 929 | #if (ANDROID_VERSION >= 5) |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 930 | MemMapping map; |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 931 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 932 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 933 | // Open the XML file |
| 934 | LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str()); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 935 | if (package.size() > 4 && package.substr(package.size() - 4) != ".zip") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 936 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 937 | LOGINFO("Load XML directly\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 938 | // We can try to load the XML directly... |
| 939 | struct stat st; |
| 940 | if(stat(package.c_str(),&st) != 0) |
| 941 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 942 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 943 | len = st.st_size; |
| 944 | xmlFile = (char*) malloc(len + 1); |
| 945 | if (!xmlFile) |
| 946 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 947 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 948 | fd = open(package.c_str(), O_RDONLY); |
| 949 | if (fd == -1) |
| 950 | goto error; |
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 | read(fd, xmlFile, len); |
| 953 | close(fd); |
| 954 | } |
| 955 | else |
| 956 | { |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 957 | LOGINFO("Loading zip theme\n"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 958 | #if (ANDROID_VERSION >= 5) |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 959 | if (!TWFunc::Path_Exists(package)) |
| 960 | return -1; |
| 961 | if (sysMapFile(package.c_str(), &map) != 0) { |
| 962 | LOGERR("Failed to map '%s'\n", package.c_str()); |
| 963 | return -1; |
| 964 | } |
| 965 | if (mzOpenZipArchive(map.addr, map.length, &zip)) { |
| 966 | LOGERR("Unable to open zip archive '%s'\n", package.c_str()); |
| 967 | sysReleaseMap(&map); |
| 968 | return -1; |
| 969 | } |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 970 | #else |
| 971 | if (mzOpenZipArchive(package.c_str(), &zip)) { |
| 972 | LOGERR("Failed to open theme zip.\n"); |
| 973 | return -1; |
| 974 | } |
| 975 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 976 | pZip = &zip; |
| 977 | const ZipEntry* ui_xml = mzFindZipEntry(&zip, "ui.xml"); |
| 978 | if (ui_xml == NULL) |
| 979 | { |
| 980 | LOGERR("Unable to locate ui.xml in zip file\n"); |
| 981 | goto error; |
| 982 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 983 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 984 | // Allocate the buffer for the file |
| 985 | len = mzGetZipEntryUncompLen(ui_xml); |
| 986 | xmlFile = (char*) malloc(len + 1); |
| 987 | if (!xmlFile) |
| 988 | goto error; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 989 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 990 | if (!mzExtractZipEntryToBuffer(&zip, ui_xml, (unsigned char*) xmlFile)) |
| 991 | { |
| 992 | LOGERR("Unable to extract ui.xml\n"); |
| 993 | goto error; |
| 994 | } |
| 995 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 996 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 997 | // NULL-terminate the string |
| 998 | xmlFile[len] = 0x00; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 999 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1000 | // Before loading, mCurrentSet must be the loading package so we can find resources |
| 1001 | pageSet = mCurrentSet; |
| 1002 | mCurrentSet = new PageSet(xmlFile); |
| 1003 | |
| 1004 | ret = mCurrentSet->Load(pZip); |
| 1005 | if (ret == 0) |
| 1006 | { |
| 1007 | mCurrentSet->SetPage(startpage); |
| 1008 | mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet)); |
| 1009 | } |
| 1010 | else |
| 1011 | { |
| 1012 | LOGERR("Package %s failed to load.\n", name.c_str()); |
| 1013 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1014 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1015 | // The first successful package we loaded is the base |
| 1016 | if (mBaseSet == NULL) |
| 1017 | mBaseSet = mCurrentSet; |
| 1018 | |
| 1019 | mCurrentSet = pageSet; |
| 1020 | |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1021 | if (pZip) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1022 | mzCloseZipArchive(pZip); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 1023 | #if (ANDROID_VERSION >= 5) |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1024 | sysReleaseMap(&map); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 1025 | #endif |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1026 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1027 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1028 | |
| 1029 | error: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1030 | LOGERR("An internal error has occurred.\n"); |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1031 | if (pZip) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1032 | mzCloseZipArchive(pZip); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 1033 | #if (ANDROID_VERSION >= 5) |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1034 | sysReleaseMap(&map); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 1035 | #endif |
Ethan Yonker | a2dc2f2 | 2014-11-08 08:13:40 -0600 | [diff] [blame] | 1036 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1037 | if (xmlFile) |
| 1038 | free(xmlFile); |
| 1039 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | PageSet* PageManager::FindPackage(std::string name) |
| 1043 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1044 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1045 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1046 | iter = mPageSets.find(name); |
| 1047 | if (iter != mPageSets.end()) |
| 1048 | return (*iter).second; |
| 1049 | |
| 1050 | LOGERR("Unable to locate package %s\n", name.c_str()); |
| 1051 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | PageSet* PageManager::SelectPackage(std::string name) |
| 1055 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1056 | LOGINFO("Switching packages (%s)\n", name.c_str()); |
| 1057 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1058 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1059 | tmp = FindPackage(name); |
| 1060 | if (tmp) |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1061 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1062 | mCurrentSet = tmp; |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 1063 | mCurrentSet->NotifyVarChange("", ""); |
| 1064 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1065 | else |
| 1066 | LOGERR("Unable to find package.\n"); |
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 | return mCurrentSet; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | int PageManager::ReloadPackage(std::string name, std::string package) |
| 1072 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1073 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1074 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1075 | iter = mPageSets.find(name); |
| 1076 | if (iter == mPageSets.end()) |
| 1077 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1078 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1079 | if(mMouseCursor) |
| 1080 | mMouseCursor->ResetData(gr_fb_width(), gr_fb_height()); |
| 1081 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1082 | PageSet* set = (*iter).second; |
| 1083 | mPageSets.erase(iter); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1084 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1085 | if (LoadPackage(name, package, "main") != 0) |
| 1086 | { |
| 1087 | LOGERR("Failed to load package.\n"); |
| 1088 | mPageSets.insert(std::pair<std::string, PageSet*>(name, set)); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | if (mCurrentSet == set) |
| 1092 | SelectPackage(name); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 1093 | if (mBaseSet == set) |
| 1094 | mBaseSet = mCurrentSet; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1095 | delete set; |
| 1096 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | void PageManager::ReleasePackage(std::string name) |
| 1100 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1101 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1102 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1103 | iter = mPageSets.find(name); |
| 1104 | if (iter == mPageSets.end()) |
| 1105 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1106 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1107 | PageSet* set = (*iter).second; |
| 1108 | mPageSets.erase(iter); |
| 1109 | delete set; |
| 1110 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | int PageManager::ChangePage(std::string name) |
| 1114 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1115 | DataManager::SetValue("tw_operation_state", 0); |
| 1116 | int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1); |
| 1117 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | int PageManager::ChangeOverlay(std::string name) |
| 1121 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1122 | if (name.empty()) |
| 1123 | return mCurrentSet->SetOverlay(NULL); |
| 1124 | else |
| 1125 | { |
| 1126 | Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL; |
| 1127 | return mCurrentSet->SetOverlay(page); |
| 1128 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | Resource* PageManager::FindResource(std::string name) |
| 1132 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1133 | return (mCurrentSet ? mCurrentSet->FindResource(name) : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | Resource* PageManager::FindResource(std::string package, std::string name) |
| 1137 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1138 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1139 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1140 | tmp = FindPackage(name); |
| 1141 | return (tmp ? tmp->FindResource(name) : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | int PageManager::SwitchToConsole(void) |
| 1145 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1146 | PageSet* console = new PageSet(NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1147 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1148 | mCurrentSet = console; |
| 1149 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1150 | } |
| 1151 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 1152 | int PageManager::EndConsole(void) |
| 1153 | { |
| 1154 | if (mCurrentSet && mBaseSet) { |
| 1155 | delete mCurrentSet; |
| 1156 | mCurrentSet = mBaseSet; |
| 1157 | return 0; |
| 1158 | } |
| 1159 | return -1; |
| 1160 | } |
| 1161 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1162 | int PageManager::IsCurrentPage(Page* page) |
| 1163 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1164 | return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | int PageManager::Render(void) |
| 1168 | { |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1169 | int res = (mCurrentSet ? mCurrentSet->Render() : -1); |
| 1170 | if(mMouseCursor) |
| 1171 | mMouseCursor->Render(); |
| 1172 | return res; |
| 1173 | } |
| 1174 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1175 | HardwareKeyboard *PageManager::GetHardwareKeyboard() |
| 1176 | { |
| 1177 | if(!mHardwareKeyboard) |
| 1178 | mHardwareKeyboard = new HardwareKeyboard(); |
| 1179 | return mHardwareKeyboard; |
| 1180 | } |
| 1181 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1182 | MouseCursor *PageManager::GetMouseCursor() |
| 1183 | { |
| 1184 | if(!mMouseCursor) |
| 1185 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 1186 | return mMouseCursor; |
| 1187 | } |
| 1188 | |
| 1189 | void PageManager::LoadCursorData(xml_node<>* node) |
| 1190 | { |
| 1191 | if(!mMouseCursor) |
| 1192 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 1193 | |
| 1194 | mMouseCursor->LoadData(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | int PageManager::Update(void) |
| 1198 | { |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 1199 | #ifndef TW_NO_SCREEN_TIMEOUT |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1200 | if(blankTimer.IsScreenOff()) |
| 1201 | return 0; |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 1202 | #endif |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1203 | |
| 1204 | int res = (mCurrentSet ? mCurrentSet->Update() : -1); |
| 1205 | |
| 1206 | if(mMouseCursor) |
| 1207 | { |
| 1208 | int c_res = mMouseCursor->Update(); |
| 1209 | if(c_res > res) |
| 1210 | res = c_res; |
| 1211 | } |
| 1212 | return res; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1216 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1217 | return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1218 | } |
| 1219 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1220 | int PageManager::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1221 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1222 | return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | int PageManager::NotifyKeyboard(int key) |
| 1226 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1227 | return (mCurrentSet ? mCurrentSet->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | int PageManager::SetKeyBoardFocus(int inFocus) |
| 1231 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1232 | return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | int PageManager::NotifyVarChange(std::string varName, std::string value) |
| 1236 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1237 | return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | extern "C" void gui_notifyVarChange(const char *name, const char* value) |
| 1241 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1242 | if (!gGuiRunning) |
| 1243 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1244 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1245 | PageManager::NotifyVarChange(name, value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1246 | } |