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