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> |
| 35 | |
| 36 | #include <string> |
| 37 | |
| 38 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 39 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | #include "rapidxml.hpp" |
| 44 | #include "objects.hpp" |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 45 | #ifndef TW_NO_SCREEN_TIMEOUT |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 46 | #include "blanktimer.hpp" |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 47 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | |
| 49 | extern int gGuiRunning; |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 50 | #ifndef TW_NO_SCREEN_TIMEOUT |
gordon1337 | 0d9133d | 2013-06-08 14:17:07 +0200 | [diff] [blame] | 51 | extern blanktimer blankTimer; |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 52 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
| 54 | std::map<std::string, PageSet*> PageManager::mPageSets; |
| 55 | PageSet* PageManager::mCurrentSet; |
| 56 | PageSet* PageManager::mBaseSet = NULL; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 57 | MouseCursor *PageManager::mMouseCursor = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | // Helper routine to convert a string to a color declaration |
| 60 | int ConvertStrToColor(std::string str, COLOR* color) |
| 61 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 62 | // Set the default, solid black |
| 63 | memset(color, 0, sizeof(COLOR)); |
| 64 | color->alpha = 255; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | // Translate variables |
| 67 | DataManager::GetValue(str, str); |
| 68 | |
| 69 | // Look for some defaults |
| 70 | if (str == "black") return 0; |
| 71 | else if (str == "white") { color->red = color->green = color->blue = 255; return 0; } |
| 72 | else if (str == "red") { color->red = 255; return 0; } |
| 73 | else if (str == "green") { color->green = 255; return 0; } |
| 74 | else if (str == "blue") { color->blue = 255; return 0; } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 76 | // At this point, we require an RGB(A) color |
| 77 | if (str[0] != '#') |
| 78 | return -1; |
| 79 | |
| 80 | str.erase(0, 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 82 | int result; |
| 83 | if (str.size() >= 8) { |
| 84 | // We have alpha channel |
| 85 | string alpha = str.substr(6, 2); |
| 86 | result = strtol(alpha.c_str(), NULL, 16); |
| 87 | color->alpha = result & 0x000000FF; |
| 88 | str.resize(6); |
| 89 | result = strtol(str.c_str(), NULL, 16); |
| 90 | color->red = (result >> 16) & 0x000000FF; |
| 91 | color->green = (result >> 8) & 0x000000FF; |
| 92 | color->blue = result & 0x000000FF; |
| 93 | } else { |
| 94 | result = strtol(str.c_str(), NULL, 16); |
| 95 | color->red = (result >> 16) & 0x000000FF; |
| 96 | color->green = (result >> 8) & 0x000000FF; |
| 97 | color->blue = result & 0x000000FF; |
| 98 | } |
| 99 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Helper APIs |
| 103 | bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, RenderObject::Placement* placement /* = NULL */) |
| 104 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | if (!node) |
| 106 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 108 | std::string value; |
| 109 | if (node->first_attribute("x")) |
| 110 | { |
| 111 | value = node->first_attribute("x")->value(); |
| 112 | DataManager::GetValue(value, value); |
| 113 | *x = atol(value.c_str()); |
| 114 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | if (node->first_attribute("y")) |
| 117 | { |
| 118 | value = node->first_attribute("y")->value(); |
| 119 | DataManager::GetValue(value, value); |
| 120 | *y = atol(value.c_str()); |
| 121 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | if (w && node->first_attribute("w")) |
| 124 | { |
| 125 | value = node->first_attribute("w")->value(); |
| 126 | DataManager::GetValue(value, value); |
| 127 | *w = atol(value.c_str()); |
| 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 (h && node->first_attribute("h")) |
| 131 | { |
| 132 | value = node->first_attribute("h")->value(); |
| 133 | DataManager::GetValue(value, value); |
| 134 | *h = atol(value.c_str()); |
| 135 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 136 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 137 | if (placement && node->first_attribute("placement")) |
| 138 | { |
| 139 | value = node->first_attribute("placement")->value(); |
| 140 | DataManager::GetValue(value, value); |
| 141 | *placement = (RenderObject::Placement) atol(value.c_str()); |
| 142 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 144 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | int ActionObject::SetActionPos(int x, int y, int w, int h) |
| 148 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 149 | if (x < 0 || y < 0) |
| 150 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 151 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 152 | mActionX = x; |
| 153 | mActionY = y; |
| 154 | if (w || h) |
| 155 | { |
| 156 | mActionW = w; |
| 157 | mActionH = h; |
| 158 | } |
| 159 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Page::Page(xml_node<>* page, xml_node<>* templates /* = NULL */) |
| 163 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 164 | mTouchStart = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 166 | // We can memset the whole structure, because the alpha channel is ignored |
| 167 | memset(&mBackground, 0, sizeof(COLOR)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 168 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 169 | // With NULL, we make a console-only display |
| 170 | if (!page) |
| 171 | { |
| 172 | mName = "console"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 174 | GUIConsole* element = new GUIConsole(NULL); |
| 175 | mRenders.push_back(element); |
| 176 | mActions.push_back(element); |
| 177 | return; |
| 178 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 179 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 180 | if (page->first_attribute("name")) |
| 181 | mName = page->first_attribute("name")->value(); |
| 182 | else |
| 183 | { |
| 184 | LOGERR("No page name attribute found!\n"); |
| 185 | return; |
| 186 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 187 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 188 | LOGINFO("Loading page %s\n", mName.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 190 | // This is a recursive routine for template handling |
| 191 | ProcessNode(page, templates); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 192 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 193 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 196 | Page::~Page() |
| 197 | { |
| 198 | for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr) |
| 199 | delete *itr; |
| 200 | } |
| 201 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | bool Page::ProcessNode(xml_node<>* page, xml_node<>* templates /* = NULL */, int depth /* = 0 */) |
| 203 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 204 | if (depth == 10) |
| 205 | { |
| 206 | LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n"); |
| 207 | return false; |
| 208 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 210 | // Let's retrieve the background value, if any |
| 211 | xml_node<>* bg = page->first_node("background"); |
| 212 | if (bg) |
| 213 | { |
| 214 | xml_attribute<>* attr = bg->first_attribute("color"); |
| 215 | if (attr) |
| 216 | { |
| 217 | std::string color = attr->value(); |
| 218 | ConvertStrToColor(color, &mBackground); |
| 219 | } |
| 220 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 221 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 222 | xml_node<>* child; |
| 223 | child = page->first_node("object"); |
| 224 | while (child) |
| 225 | { |
| 226 | if (!child->first_attribute("type")) |
| 227 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 228 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 229 | std::string type = child->first_attribute("type")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 230 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 231 | if (type == "text") |
| 232 | { |
| 233 | GUIText* element = new GUIText(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 234 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 235 | mRenders.push_back(element); |
| 236 | mActions.push_back(element); |
| 237 | } |
| 238 | else if (type == "image") |
| 239 | { |
| 240 | GUIImage* element = new GUIImage(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 241 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 242 | mRenders.push_back(element); |
| 243 | } |
| 244 | else if (type == "fill") |
| 245 | { |
| 246 | GUIFill* element = new GUIFill(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 247 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 248 | mRenders.push_back(element); |
| 249 | } |
| 250 | else if (type == "action") |
| 251 | { |
| 252 | GUIAction* element = new GUIAction(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 253 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 254 | mActions.push_back(element); |
| 255 | } |
| 256 | else if (type == "console") |
| 257 | { |
| 258 | GUIConsole* element = new GUIConsole(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 259 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 260 | mRenders.push_back(element); |
| 261 | mActions.push_back(element); |
| 262 | } |
| 263 | else if (type == "button") |
| 264 | { |
| 265 | GUIButton* element = new GUIButton(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 266 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 267 | mRenders.push_back(element); |
| 268 | mActions.push_back(element); |
| 269 | } |
| 270 | else if (type == "checkbox") |
| 271 | { |
| 272 | GUICheckbox* element = new GUICheckbox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 273 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 274 | mRenders.push_back(element); |
| 275 | mActions.push_back(element); |
| 276 | } |
| 277 | else if (type == "fileselector") |
| 278 | { |
| 279 | GUIFileSelector* element = new GUIFileSelector(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 280 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 281 | mRenders.push_back(element); |
| 282 | mActions.push_back(element); |
| 283 | } |
| 284 | else if (type == "animation") |
| 285 | { |
| 286 | GUIAnimation* element = new GUIAnimation(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 287 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 288 | mRenders.push_back(element); |
| 289 | } |
| 290 | else if (type == "progressbar") |
| 291 | { |
| 292 | GUIProgressBar* element = new GUIProgressBar(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 293 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 294 | mRenders.push_back(element); |
| 295 | mActions.push_back(element); |
| 296 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 297 | else if (type == "slider") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 298 | { |
| 299 | GUISlider* element = new GUISlider(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 300 | mObjects.push_back(element); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 301 | mRenders.push_back(element); |
| 302 | mActions.push_back(element); |
| 303 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 304 | else if (type == "slidervalue") |
| 305 | { |
| 306 | GUISliderValue *element = new GUISliderValue(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 307 | mObjects.push_back(element); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 308 | mRenders.push_back(element); |
| 309 | mActions.push_back(element); |
| 310 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 311 | else if (type == "listbox") |
| 312 | { |
| 313 | GUIListBox* element = new GUIListBox(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 314 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 315 | mRenders.push_back(element); |
| 316 | mActions.push_back(element); |
| 317 | } |
| 318 | else if (type == "keyboard") |
| 319 | { |
| 320 | GUIKeyboard* element = new GUIKeyboard(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 321 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 322 | mRenders.push_back(element); |
| 323 | mActions.push_back(element); |
| 324 | } |
| 325 | else if (type == "input") |
| 326 | { |
| 327 | GUIInput* element = new GUIInput(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 328 | mObjects.push_back(element); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 329 | mRenders.push_back(element); |
| 330 | mActions.push_back(element); |
| 331 | mInputs.push_back(element); |
| 332 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 333 | else if (type == "partitionlist") |
| 334 | { |
| 335 | GUIPartitionList* element = new GUIPartitionList(child); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 336 | mObjects.push_back(element); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 337 | mRenders.push_back(element); |
| 338 | mActions.push_back(element); |
| 339 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 340 | else if (type == "template") |
| 341 | { |
| 342 | if (!templates || !child->first_attribute("name")) |
| 343 | { |
| 344 | LOGERR("Invalid template request.\n"); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | std::string name = child->first_attribute("name")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 349 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 350 | // We need to find the correct template |
| 351 | xml_node<>* node; |
| 352 | node = templates->first_node("template"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 353 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 354 | while (node) |
| 355 | { |
| 356 | if (!node->first_attribute("name")) |
| 357 | continue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 359 | if (name == node->first_attribute("name")->value()) |
| 360 | { |
| 361 | if (!ProcessNode(node, templates, depth + 1)) |
| 362 | return false; |
| 363 | else |
| 364 | break; |
| 365 | } |
| 366 | node = node->next_sibling("template"); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | LOGERR("Unknown object type.\n"); |
| 373 | } |
| 374 | child = child->next_sibling("object"); |
| 375 | } |
| 376 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | int Page::Render(void) |
| 380 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 381 | // Render background |
| 382 | gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha); |
| 383 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 384 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 385 | // Render remaining objects |
| 386 | std::vector<RenderObject*>::iterator iter; |
| 387 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 388 | { |
| 389 | if ((*iter)->Render()) |
| 390 | LOGERR("A render request has failed.\n"); |
| 391 | } |
| 392 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | int Page::Update(void) |
| 396 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 397 | int retCode = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 398 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 399 | std::vector<RenderObject*>::iterator iter; |
| 400 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 401 | { |
| 402 | int ret = (*iter)->Update(); |
| 403 | if (ret < 0) |
| 404 | LOGERR("An update request has failed.\n"); |
| 405 | else if (ret > retCode) |
| 406 | retCode = ret; |
| 407 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 408 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 409 | return retCode; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | int Page::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 413 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 414 | // By default, return 1 to ignore further touches if nobody is listening |
| 415 | int ret = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 416 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 417 | // Don't try to handle a lack of handlers |
| 418 | if (mActions.size() == 0) |
| 419 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 420 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 421 | // We record mTouchStart so we can pass all the touch stream to the same handler |
| 422 | if (state == TOUCH_START) |
| 423 | { |
| 424 | std::vector<ActionObject*>::reverse_iterator iter; |
| 425 | // We work backwards, from top-most element to bottom-most element |
| 426 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 427 | { |
| 428 | if ((*iter)->IsInRegion(x, y)) |
| 429 | { |
| 430 | mTouchStart = (*iter); |
| 431 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 432 | if (ret >= 0) |
| 433 | break; |
| 434 | mTouchStart = NULL; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | else if (state == TOUCH_RELEASE && mTouchStart != NULL) |
| 439 | { |
| 440 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 441 | mTouchStart = NULL; |
| 442 | } |
| 443 | else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL) |
| 444 | { |
| 445 | ret = mTouchStart->NotifyTouch(state, x, y); |
| 446 | } |
| 447 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | int Page::NotifyKey(int key) |
| 451 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 452 | std::vector<ActionObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 453 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 454 | // Don't try to handle a lack of handlers |
| 455 | if (mActions.size() == 0) |
| 456 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 457 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 458 | // We work backwards, from top-most element to bottom-most element |
| 459 | for (iter = mActions.rbegin(); iter != mActions.rend(); iter++) |
| 460 | { |
| 461 | int ret = (*iter)->NotifyKey(key); |
| 462 | if (ret == 0) |
| 463 | return 0; |
| 464 | else if (ret < 0) |
| 465 | LOGERR("An action handler has returned an error"); |
| 466 | } |
| 467 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int Page::NotifyKeyboard(int key) |
| 471 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 472 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 473 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 474 | // Don't try to handle a lack of handlers |
| 475 | if (mInputs.size() == 0) |
| 476 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 477 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 478 | // We work backwards, from top-most element to bottom-most element |
| 479 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 480 | { |
| 481 | int ret = (*iter)->NotifyKeyboard(key); |
| 482 | if (ret == 0) |
| 483 | return 0; |
| 484 | else if (ret < 0) |
| 485 | LOGERR("A keyboard handler has returned an error"); |
| 486 | } |
| 487 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | int Page::SetKeyBoardFocus(int inFocus) |
| 491 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 492 | std::vector<InputObject*>::reverse_iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 493 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 494 | // Don't try to handle a lack of handlers |
| 495 | if (mInputs.size() == 0) |
| 496 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 497 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 498 | // We work backwards, from top-most element to bottom-most element |
| 499 | for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++) |
| 500 | { |
| 501 | int ret = (*iter)->SetInputFocus(inFocus); |
| 502 | if (ret == 0) |
| 503 | return 0; |
| 504 | else if (ret < 0) |
| 505 | LOGERR("An input focus handler has returned an error"); |
| 506 | } |
| 507 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void Page::SetPageFocus(int inFocus) |
| 511 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 512 | // Render remaining objects |
| 513 | std::vector<RenderObject*>::iterator iter; |
| 514 | for (iter = mRenders.begin(); iter != mRenders.end(); iter++) |
| 515 | (*iter)->SetPageFocus(inFocus); |
| 516 | |
| 517 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | int Page::NotifyVarChange(std::string varName, std::string value) |
| 521 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 522 | std::vector<GUIObject*>::iterator iter; |
| 523 | for (iter = mObjects.begin(); iter != mObjects.end(); ++iter) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 524 | { |
| 525 | if ((*iter)->NotifyVarChange(varName, value)) |
| 526 | LOGERR("An action handler errored on NotifyVarChange.\n"); |
| 527 | } |
| 528 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | PageSet::PageSet(char* xmlFile) |
| 532 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 533 | mResources = NULL; |
| 534 | mCurrentPage = NULL; |
| 535 | mOverlayPage = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 536 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 537 | mXmlFile = xmlFile; |
| 538 | if (xmlFile) |
| 539 | mDoc.parse<0>(mXmlFile); |
| 540 | else |
| 541 | mCurrentPage = new Page(NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | PageSet::~PageSet() |
| 545 | { |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 546 | for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr) |
| 547 | delete *itr; |
| 548 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 549 | delete mResources; |
| 550 | free(mXmlFile); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | int PageSet::Load(ZipArchive* package) |
| 554 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 555 | xml_node<>* parent; |
| 556 | xml_node<>* child; |
| 557 | xml_node<>* templates; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 558 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 559 | parent = mDoc.first_node("recovery"); |
| 560 | if (!parent) |
| 561 | parent = mDoc.first_node("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 562 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 563 | // Now, let's parse the XML |
| 564 | LOGINFO("Loading resources...\n"); |
| 565 | child = parent->first_node("resources"); |
| 566 | if (child) |
| 567 | mResources = new ResourceManager(child, package); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 568 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 569 | LOGINFO("Loading variables...\n"); |
| 570 | child = parent->first_node("variables"); |
| 571 | if (child) |
| 572 | LoadVariables(child); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 573 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 574 | LOGINFO("Loading mouse cursor...\n"); |
| 575 | child = parent->first_node("mousecursor"); |
| 576 | if(child) |
| 577 | PageManager::LoadCursorData(child); |
| 578 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 579 | LOGINFO("Loading pages...\n"); |
| 580 | // This may be NULL if no templates are present |
| 581 | templates = parent->first_node("templates"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 582 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 583 | child = parent->first_node("pages"); |
| 584 | if (!child) |
| 585 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 586 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 587 | return LoadPages(child, templates); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | int PageSet::SetPage(std::string page) |
| 591 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 592 | Page* tmp = FindPage(page); |
| 593 | if (tmp) |
| 594 | { |
| 595 | if (mCurrentPage) mCurrentPage->SetPageFocus(0); |
| 596 | mCurrentPage = tmp; |
| 597 | mCurrentPage->SetPageFocus(1); |
| 598 | mCurrentPage->NotifyVarChange("", ""); |
| 599 | return 0; |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | LOGERR("Unable to locate page (%s)\n", page.c_str()); |
| 604 | } |
| 605 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | int PageSet::SetOverlay(Page* page) |
| 609 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 610 | if (mOverlayPage) mOverlayPage->SetPageFocus(0); |
| 611 | mOverlayPage = page; |
| 612 | if (mOverlayPage) |
| 613 | { |
| 614 | mOverlayPage->SetPageFocus(1); |
| 615 | mOverlayPage->NotifyVarChange("", ""); |
| 616 | } |
| 617 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | Resource* PageSet::FindResource(std::string name) |
| 621 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 622 | return mResources ? mResources->FindResource(name) : NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | Page* PageSet::FindPage(std::string name) |
| 626 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 627 | std::vector<Page*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 628 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 629 | for (iter = mPages.begin(); iter != mPages.end(); iter++) |
| 630 | { |
| 631 | if (name == (*iter)->GetName()) |
| 632 | return (*iter); |
| 633 | } |
| 634 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | int PageSet::LoadVariables(xml_node<>* vars) |
| 638 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 639 | xml_node<>* child; |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 640 | xml_attribute<> *name, *value, *persist; |
| 641 | int p; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 642 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 643 | child = vars->first_node("variable"); |
| 644 | while (child) |
| 645 | { |
Vojtech Bocek | 81c29dc | 2013-12-07 23:02:09 +0100 | [diff] [blame] | 646 | name = child->first_attribute("name"); |
| 647 | value = child->first_attribute("value"); |
| 648 | persist = child->first_attribute("persist"); |
| 649 | if(name && value) |
| 650 | { |
| 651 | p = persist ? atoi(persist->value()) : 0; |
| 652 | DataManager::SetValue(name->value(), value->value(), p); |
| 653 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 654 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 655 | child = child->next_sibling("variable"); |
| 656 | } |
| 657 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | int PageSet::LoadPages(xml_node<>* pages, xml_node<>* templates /* = NULL */) |
| 661 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 662 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 663 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 664 | if (!pages) |
| 665 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 666 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 667 | child = pages->first_node("page"); |
| 668 | while (child != NULL) |
| 669 | { |
| 670 | Page* page = new Page(child, templates); |
| 671 | if (page->GetName().empty()) |
| 672 | { |
| 673 | LOGERR("Unable to process load page\n"); |
| 674 | delete page; |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | mPages.push_back(page); |
| 679 | } |
| 680 | child = child->next_sibling("page"); |
| 681 | } |
| 682 | if (mPages.size() > 0) |
| 683 | return 0; |
| 684 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | int PageSet::IsCurrentPage(Page* page) |
| 688 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 689 | return ((mCurrentPage && mCurrentPage == page) ? 1 : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | int PageSet::Render(void) |
| 693 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 694 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 695 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 696 | ret = (mCurrentPage ? mCurrentPage->Render() : -1); |
| 697 | if (ret < 0) |
| 698 | return ret; |
| 699 | ret = (mOverlayPage ? mOverlayPage->Render() : -1); |
| 700 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | int PageSet::Update(void) |
| 704 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 705 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 706 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 707 | ret = (mCurrentPage ? mCurrentPage->Update() : -1); |
| 708 | if (ret < 0 || ret > 1) |
| 709 | return ret; |
| 710 | ret = (mOverlayPage ? mOverlayPage->Update() : -1); |
| 711 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 715 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 716 | if (mOverlayPage) |
| 717 | return (mOverlayPage->NotifyTouch(state, x, y)); |
| 718 | |
| 719 | return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | int PageSet::NotifyKey(int key) |
| 723 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 724 | if (mOverlayPage) |
| 725 | return (mOverlayPage->NotifyKey(key)); |
| 726 | |
| 727 | return (mCurrentPage ? mCurrentPage->NotifyKey(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | int PageSet::NotifyKeyboard(int key) |
| 731 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 732 | if (mOverlayPage) |
| 733 | return (mOverlayPage->NotifyKeyboard(key)); |
| 734 | |
| 735 | return (mCurrentPage ? mCurrentPage->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | int PageSet::SetKeyBoardFocus(int inFocus) |
| 739 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 740 | if (mOverlayPage) |
| 741 | return (mOverlayPage->SetKeyBoardFocus(inFocus)); |
| 742 | |
| 743 | return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | int PageSet::NotifyVarChange(std::string varName, std::string value) |
| 747 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 748 | if (mOverlayPage) |
| 749 | mOverlayPage->NotifyVarChange(varName, value); |
| 750 | |
| 751 | return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | int PageManager::LoadPackage(std::string name, std::string package, std::string startpage) |
| 755 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 756 | int fd; |
| 757 | ZipArchive zip, *pZip = NULL; |
| 758 | long len; |
| 759 | char* xmlFile = NULL; |
| 760 | PageSet* pageSet = NULL; |
| 761 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 762 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 763 | // Open the XML file |
| 764 | LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str()); |
| 765 | if (mzOpenZipArchive(package.c_str(), &zip)) |
| 766 | { |
| 767 | // We can try to load the XML directly... |
| 768 | struct stat st; |
| 769 | if(stat(package.c_str(),&st) != 0) |
| 770 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 771 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 772 | len = st.st_size; |
| 773 | xmlFile = (char*) malloc(len + 1); |
| 774 | if (!xmlFile) |
| 775 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 776 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 777 | fd = open(package.c_str(), O_RDONLY); |
| 778 | if (fd == -1) |
| 779 | goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 780 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 781 | read(fd, xmlFile, len); |
| 782 | close(fd); |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | pZip = &zip; |
| 787 | const ZipEntry* ui_xml = mzFindZipEntry(&zip, "ui.xml"); |
| 788 | if (ui_xml == NULL) |
| 789 | { |
| 790 | LOGERR("Unable to locate ui.xml in zip file\n"); |
| 791 | goto error; |
| 792 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 793 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 794 | // Allocate the buffer for the file |
| 795 | len = mzGetZipEntryUncompLen(ui_xml); |
| 796 | xmlFile = (char*) malloc(len + 1); |
| 797 | if (!xmlFile) |
| 798 | goto error; |
| 799 | |
| 800 | if (!mzExtractZipEntryToBuffer(&zip, ui_xml, (unsigned char*) xmlFile)) |
| 801 | { |
| 802 | LOGERR("Unable to extract ui.xml\n"); |
| 803 | goto error; |
| 804 | } |
| 805 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 806 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 807 | // NULL-terminate the string |
| 808 | xmlFile[len] = 0x00; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 809 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 810 | // Before loading, mCurrentSet must be the loading package so we can find resources |
| 811 | pageSet = mCurrentSet; |
| 812 | mCurrentSet = new PageSet(xmlFile); |
| 813 | |
| 814 | ret = mCurrentSet->Load(pZip); |
| 815 | if (ret == 0) |
| 816 | { |
| 817 | mCurrentSet->SetPage(startpage); |
| 818 | mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet)); |
| 819 | } |
| 820 | else |
| 821 | { |
| 822 | LOGERR("Package %s failed to load.\n", name.c_str()); |
| 823 | } |
| 824 | |
| 825 | // The first successful package we loaded is the base |
| 826 | if (mBaseSet == NULL) |
| 827 | mBaseSet = mCurrentSet; |
| 828 | |
| 829 | mCurrentSet = pageSet; |
| 830 | |
| 831 | if (pZip) |
| 832 | mzCloseZipArchive(pZip); |
| 833 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 834 | |
| 835 | error: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 836 | LOGERR("An internal error has occurred.\n"); |
| 837 | if (pZip) |
| 838 | mzCloseZipArchive(pZip); |
| 839 | if (xmlFile) |
| 840 | free(xmlFile); |
| 841 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | PageSet* PageManager::FindPackage(std::string name) |
| 845 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 846 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 847 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 848 | iter = mPageSets.find(name); |
| 849 | if (iter != mPageSets.end()) |
| 850 | return (*iter).second; |
| 851 | |
| 852 | LOGERR("Unable to locate package %s\n", name.c_str()); |
| 853 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | PageSet* PageManager::SelectPackage(std::string name) |
| 857 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 858 | LOGINFO("Switching packages (%s)\n", name.c_str()); |
| 859 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 860 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 861 | tmp = FindPackage(name); |
| 862 | if (tmp) |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 863 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 864 | mCurrentSet = tmp; |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 865 | mCurrentSet->NotifyVarChange("", ""); |
| 866 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 867 | else |
| 868 | LOGERR("Unable to find package.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 869 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 870 | return mCurrentSet; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | int PageManager::ReloadPackage(std::string name, std::string package) |
| 874 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 875 | std::map<std::string, PageSet*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 876 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 877 | iter = mPageSets.find(name); |
| 878 | if (iter == mPageSets.end()) |
| 879 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 880 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 881 | if(mMouseCursor) |
| 882 | mMouseCursor->ResetData(gr_fb_width(), gr_fb_height()); |
| 883 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 884 | PageSet* set = (*iter).second; |
| 885 | mPageSets.erase(iter); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 886 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 887 | if (LoadPackage(name, package, "main") != 0) |
| 888 | { |
| 889 | LOGERR("Failed to load package.\n"); |
| 890 | mPageSets.insert(std::pair<std::string, PageSet*>(name, set)); |
| 891 | return -1; |
| 892 | } |
| 893 | if (mCurrentSet == set) |
| 894 | SelectPackage(name); |
Vojtech Bocek | bfb6334 | 2014-02-08 00:32:31 +0100 | [diff] [blame] | 895 | if (mBaseSet == set) |
| 896 | mBaseSet = mCurrentSet; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 897 | delete set; |
| 898 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | void PageManager::ReleasePackage(std::string name) |
| 902 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 903 | std::map<std::string, PageSet*>::iterator iter; |
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 | iter = mPageSets.find(name); |
| 906 | if (iter == mPageSets.end()) |
| 907 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 908 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 909 | PageSet* set = (*iter).second; |
| 910 | mPageSets.erase(iter); |
| 911 | delete set; |
| 912 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | int PageManager::ChangePage(std::string name) |
| 916 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 917 | DataManager::SetValue("tw_operation_state", 0); |
| 918 | int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1); |
| 919 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | int PageManager::ChangeOverlay(std::string name) |
| 923 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 924 | if (name.empty()) |
| 925 | return mCurrentSet->SetOverlay(NULL); |
| 926 | else |
| 927 | { |
| 928 | Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL; |
| 929 | return mCurrentSet->SetOverlay(page); |
| 930 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | Resource* PageManager::FindResource(std::string name) |
| 934 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 935 | return (mCurrentSet ? mCurrentSet->FindResource(name) : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | Resource* PageManager::FindResource(std::string package, std::string name) |
| 939 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 940 | PageSet* tmp; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 941 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 942 | tmp = FindPackage(name); |
| 943 | return (tmp ? tmp->FindResource(name) : NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | int PageManager::SwitchToConsole(void) |
| 947 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 948 | PageSet* console = new PageSet(NULL); |
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 | mCurrentSet = console; |
| 951 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | int PageManager::IsCurrentPage(Page* page) |
| 955 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 956 | return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | int PageManager::Render(void) |
| 960 | { |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 961 | int res = (mCurrentSet ? mCurrentSet->Render() : -1); |
| 962 | if(mMouseCursor) |
| 963 | mMouseCursor->Render(); |
| 964 | return res; |
| 965 | } |
| 966 | |
| 967 | MouseCursor *PageManager::GetMouseCursor() |
| 968 | { |
| 969 | if(!mMouseCursor) |
| 970 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 971 | return mMouseCursor; |
| 972 | } |
| 973 | |
| 974 | void PageManager::LoadCursorData(xml_node<>* node) |
| 975 | { |
| 976 | if(!mMouseCursor) |
| 977 | mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height()); |
| 978 | |
| 979 | mMouseCursor->LoadData(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | int PageManager::Update(void) |
| 983 | { |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 984 | #ifndef TW_NO_SCREEN_TIMEOUT |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 985 | if(blankTimer.IsScreenOff()) |
| 986 | return 0; |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 987 | #endif |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 988 | |
| 989 | int res = (mCurrentSet ? mCurrentSet->Update() : -1); |
| 990 | |
| 991 | if(mMouseCursor) |
| 992 | { |
| 993 | int c_res = mMouseCursor->Update(); |
| 994 | if(c_res > res) |
| 995 | res = c_res; |
| 996 | } |
| 997 | return res; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 1001 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1002 | return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | int PageManager::NotifyKey(int key) |
| 1006 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1007 | return (mCurrentSet ? mCurrentSet->NotifyKey(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | int PageManager::NotifyKeyboard(int key) |
| 1011 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1012 | return (mCurrentSet ? mCurrentSet->NotifyKeyboard(key) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | int PageManager::SetKeyBoardFocus(int inFocus) |
| 1016 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1017 | return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | int PageManager::NotifyVarChange(std::string varName, std::string value) |
| 1021 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1022 | return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | extern "C" void gui_notifyVarChange(const char *name, const char* value) |
| 1026 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1027 | if (!gGuiRunning) |
| 1028 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1029 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1030 | PageManager::NotifyVarChange(name, value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1031 | } |