blob: 932be6b326d5a9aad10977cc519fe866f80e93fa [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
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 Troy3be70a82013-10-22 14:25:12 +000018
Dees_Troya13d74f2013-03-24 08:54:55 -050019// pages.cpp - Source to manage GUI base objects
Dees_Troy51a0e822012-09-05 15:24:24 -040020
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/reboot.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/mman.h>
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34#include <stdlib.h>
Ethan Yonkera2dc2f22014-11-08 08:13:40 -060035#include "../twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050036#include "../partitions.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040037
38#include <string>
39
40extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "../minuitwrp/minui.h"
Ethan Yonkera2dc2f22014-11-08 08:13:40 -060043#include "../minzip/SysUtil.h"
44#include "../minzip/Zip.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060045#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040046}
47
48#include "rapidxml.hpp"
49#include "objects.hpp"
gordon13370d9133d2013-06-08 14:17:07 +020050#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Ethan Yonker1308d532016-01-14 22:21:49 -060052#define TW_THEME_VERSION 1
Ethan Yonker8e5692f2016-01-21 11:21:06 -060053#define TW_THEME_VER_ERR -2
Ethan Yonker1308d532016-01-14 22:21:49 -060054
Dees_Troy51a0e822012-09-05 15:24:24 -040055extern int gGuiRunning;
56
Ethan Yonker74db1572015-10-28 12:44:49 -050057// From ../twrp.cpp
58extern bool datamedia;
59
60// From console.cpp
61extern size_t last_message_count;
62extern std::vector<std::string> gConsole;
63extern std::vector<std::string> gConsoleColor;
64
Dees_Troy51a0e822012-09-05 15:24:24 -040065std::map<std::string, PageSet*> PageManager::mPageSets;
66PageSet* PageManager::mCurrentSet;
67PageSet* PageManager::mBaseSet = NULL;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010068MouseCursor *PageManager::mMouseCursor = NULL;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +010069HardwareKeyboard *PageManager::mHardwareKeyboard = NULL;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -050070bool PageManager::mReloadTheme = false;
71std::string PageManager::mStartPage = "main";
Ethan Yonker74db1572015-10-28 12:44:49 -050072std::vector<language_struct> Language_List;
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Ethan Yonker751a85e2014-12-12 16:59:10 -060074int tw_x_offset = 0;
75int tw_y_offset = 0;
76
Dees_Troy51a0e822012-09-05 15:24:24 -040077// Helper routine to convert a string to a color declaration
78int ConvertStrToColor(std::string str, COLOR* color)
79{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 // Set the default, solid black
81 memset(color, 0, sizeof(COLOR));
82 color->alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 // Translate variables
85 DataManager::GetValue(str, str);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 // Look for some defaults
thatf6ed8fc2015-02-14 20:23:16 +010088 if (str == "black") return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 else if (str == "white") { color->red = color->green = color->blue = 255; return 0; }
90 else if (str == "red") { color->red = 255; return 0; }
91 else if (str == "green") { color->green = 255; return 0; }
92 else if (str == "blue") { color->blue = 255; return 0; }
Dees_Troy51a0e822012-09-05 15:24:24 -040093
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 // At this point, we require an RGB(A) color
95 if (str[0] != '#')
96 return -1;
97
98 str.erase(0, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -040099
Dees_Troy30b962e2012-10-19 20:48:59 -0400100 int result;
101 if (str.size() >= 8) {
102 // We have alpha channel
103 string alpha = str.substr(6, 2);
104 result = strtol(alpha.c_str(), NULL, 16);
105 color->alpha = result & 0x000000FF;
106 str.resize(6);
107 result = strtol(str.c_str(), NULL, 16);
108 color->red = (result >> 16) & 0x000000FF;
109 color->green = (result >> 8) & 0x000000FF;
110 color->blue = result & 0x000000FF;
111 } else {
112 result = strtol(str.c_str(), NULL, 16);
113 color->red = (result >> 16) & 0x000000FF;
114 color->green = (result >> 8) & 0x000000FF;
115 color->blue = result & 0x000000FF;
116 }
117 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118}
119
120// Helper APIs
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600121xml_node<>* FindNode(xml_node<>* parent, const char* nodename, int depth /* = 0 */)
122{
that8d46c092015-02-26 01:30:04 +0100123 if (!parent)
124 return NULL;
125
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600126 xml_node<>* child = parent->first_node(nodename);
127 if (child)
128 return child;
129
130 if (depth == 10) {
131 LOGERR("Too many style loops detected.\n");
132 return NULL;
133 }
134
135 xml_node<>* style = parent->first_node("style");
136 if (style) {
137 while (style) {
138 if (!style->first_attribute("name")) {
139 LOGERR("No name given for style.\n");
140 continue;
141 } else {
142 std::string name = style->first_attribute("name")->value();
143 xml_node<>* node = PageManager::FindStyle(name);
144
145 if (node) {
146 // We found the style that was named
147 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
148 if (stylenode)
149 return stylenode;
150 }
151 }
152 style = style->next_sibling("style");
153 }
154 } else {
that54e9c832015-11-04 21:46:01 +0100155 // Search for stylename in the parent node <object type="foo" style="foo2">
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600156 xml_attribute<>* attr = parent->first_attribute("style");
157 // If no style is found anywhere else and the node wasn't found in the object itself
158 // as a special case we will search for a style that uses the same style name as the
159 // object type, so <object type="button"> would search for a style named "button"
160 if (!attr)
161 attr = parent->first_attribute("type");
that54e9c832015-11-04 21:46:01 +0100162 // if there's no attribute type, the object type must be the element name
163 std::string stylename = attr ? attr->value() : parent->name();
164 xml_node<>* node = PageManager::FindStyle(stylename);
165 if (node) {
166 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
167 if (stylenode)
168 return stylenode;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600169 }
170 }
171 return NULL;
172}
173
thatf6ed8fc2015-02-14 20:23:16 +0100174std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue)
175{
176 if (!element)
177 return defaultvalue;
178
179 xml_attribute<>* attr = element->first_attribute(attrname);
180 return attr ? attr->value() : defaultvalue;
181}
182
183int LoadAttrInt(xml_node<>* element, const char* attrname, int defaultvalue)
184{
185 string value = LoadAttrString(element, attrname);
186 // resolve variables
187 DataManager::GetValue(value, value);
188 return value.empty() ? defaultvalue : atoi(value.c_str());
189}
190
191int LoadAttrIntScaleX(xml_node<>* element, const char* attrname, int defaultvalue)
192{
193 return scale_theme_x(LoadAttrInt(element, attrname, defaultvalue));
194}
195
196int LoadAttrIntScaleY(xml_node<>* element, const char* attrname, int defaultvalue)
197{
198 return scale_theme_y(LoadAttrInt(element, attrname, defaultvalue));
199}
200
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600201COLOR LoadAttrColor(xml_node<>* element, const char* attrname, bool* found_color, COLOR defaultvalue)
thatf6ed8fc2015-02-14 20:23:16 +0100202{
203 string value = LoadAttrString(element, attrname);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600204 *found_color = !value.empty();
thatf6ed8fc2015-02-14 20:23:16 +0100205 // resolve variables
206 DataManager::GetValue(value, value);
207 COLOR ret = defaultvalue;
208 if (ConvertStrToColor(value, &ret) == 0)
209 return ret;
210 else
211 return defaultvalue;
212}
213
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600214COLOR LoadAttrColor(xml_node<>* element, const char* attrname, COLOR defaultvalue)
215{
216 bool found_color = false;
217 return LoadAttrColor(element, attrname, &found_color, defaultvalue);
218}
219
thatf6ed8fc2015-02-14 20:23:16 +0100220FontResource* LoadAttrFont(xml_node<>* element, const char* attrname)
221{
222 std::string name = LoadAttrString(element, attrname, "");
223 if (name.empty())
224 return NULL;
225 else
that74ac6062015-03-04 22:39:34 +0100226 return PageManager::GetResources()->FindFont(name);
thatf6ed8fc2015-02-14 20:23:16 +0100227}
228
229ImageResource* LoadAttrImage(xml_node<>* element, const char* attrname)
230{
231 std::string name = LoadAttrString(element, attrname, "");
232 if (name.empty())
233 return NULL;
234 else
that74ac6062015-03-04 22:39:34 +0100235 return PageManager::GetResources()->FindImage(name);
thatf6ed8fc2015-02-14 20:23:16 +0100236}
237
238AnimationResource* LoadAttrAnimation(xml_node<>* element, const char* attrname)
239{
240 std::string name = LoadAttrString(element, attrname, "");
241 if (name.empty())
242 return NULL;
243 else
that74ac6062015-03-04 22:39:34 +0100244 return PageManager::GetResources()->FindAnimation(name);
thatf6ed8fc2015-02-14 20:23:16 +0100245}
246
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500247bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, Placement* placement /* = NULL */)
Dees_Troy51a0e822012-09-05 15:24:24 -0400248{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 if (!node)
250 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 if (node->first_attribute("x"))
thatf6ed8fc2015-02-14 20:23:16 +0100253 *x = LoadAttrIntScaleX(node, "x") + tw_x_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (node->first_attribute("y"))
thatf6ed8fc2015-02-14 20:23:16 +0100256 *y = LoadAttrIntScaleY(node, "y") + tw_y_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (w && node->first_attribute("w"))
thatf6ed8fc2015-02-14 20:23:16 +0100259 *w = LoadAttrIntScaleX(node, "w");
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 if (h && node->first_attribute("h"))
thatf6ed8fc2015-02-14 20:23:16 +0100262 *h = LoadAttrIntScaleY(node, "h");
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 if (placement && node->first_attribute("placement"))
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500265 *placement = (Placement) LoadAttrInt(node, "placement");
Dees_Troy51a0e822012-09-05 15:24:24 -0400266
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268}
269
270int ActionObject::SetActionPos(int x, int y, int w, int h)
271{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200272 if (x < 0 || y < 0)
273 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400274
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500275 mActionX = x;
276 mActionY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 if (w || h)
278 {
279 mActionW = w;
280 mActionH = h;
281 }
282 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400283}
284
thatb63e2f92015-06-27 21:35:11 +0200285Page::Page(xml_node<>* page, std::vector<xml_node<>*> *templates)
Dees_Troy51a0e822012-09-05 15:24:24 -0400286{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 mTouchStart = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 // We can memset the whole structure, because the alpha channel is ignored
290 memset(&mBackground, 0, sizeof(COLOR));
Dees_Troy51a0e822012-09-05 15:24:24 -0400291
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 // With NULL, we make a console-only display
293 if (!page)
294 {
295 mName = "console";
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200297 GUIConsole* element = new GUIConsole(NULL);
298 mRenders.push_back(element);
299 mActions.push_back(element);
300 return;
301 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400302
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 if (page->first_attribute("name"))
304 mName = page->first_attribute("name")->value();
305 else
306 {
307 LOGERR("No page name attribute found!\n");
308 return;
309 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400310
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200311 LOGINFO("Loading page %s\n", mName.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400312
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200313 // This is a recursive routine for template handling
thatb63e2f92015-06-27 21:35:11 +0200314 ProcessNode(page, templates, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400315}
316
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100317Page::~Page()
318{
319 for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr)
320 delete *itr;
321}
322
thatb63e2f92015-06-27 21:35:11 +0200323bool Page::ProcessNode(xml_node<>* page, std::vector<xml_node<>*> *templates, int depth)
Dees_Troy51a0e822012-09-05 15:24:24 -0400324{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 if (depth == 10)
326 {
327 LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n");
328 return false;
329 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400330
thatb63e2f92015-06-27 21:35:11 +0200331 for (xml_node<>* child = page->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200332 {
thatb63e2f92015-06-27 21:35:11 +0200333 std::string type = child->name();
334
335 if (type == "background") {
336 mBackground = LoadAttrColor(child, "color", COLOR(0,0,0,0));
337 continue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
thatb63e2f92015-06-27 21:35:11 +0200340 if (type == "object") {
341 // legacy format : <object type="...">
342 xml_attribute<>* attr = child->first_attribute("type");
343 type = attr ? attr->value() : "*unspecified*";
344 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400345
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200346 if (type == "text")
347 {
348 GUIText* element = new GUIText(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100349 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200350 mRenders.push_back(element);
351 mActions.push_back(element);
352 }
353 else if (type == "image")
354 {
355 GUIImage* element = new GUIImage(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100356 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 mRenders.push_back(element);
358 }
359 else if (type == "fill")
360 {
361 GUIFill* element = new GUIFill(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100362 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 mRenders.push_back(element);
364 }
365 else if (type == "action")
366 {
367 GUIAction* element = new GUIAction(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100368 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 mActions.push_back(element);
370 }
371 else if (type == "console")
372 {
373 GUIConsole* element = new GUIConsole(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100374 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200375 mRenders.push_back(element);
376 mActions.push_back(element);
377 }
that1964d192016-01-07 00:41:03 +0100378 else if (type == "terminal")
379 {
380 GUITerminal* element = new GUITerminal(child);
381 mObjects.push_back(element);
382 mRenders.push_back(element);
383 mActions.push_back(element);
384 mInputs.push_back(element);
385 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 else if (type == "button")
387 {
388 GUIButton* element = new GUIButton(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100389 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200390 mRenders.push_back(element);
391 mActions.push_back(element);
392 }
393 else if (type == "checkbox")
394 {
395 GUICheckbox* element = new GUICheckbox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100396 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 mRenders.push_back(element);
398 mActions.push_back(element);
399 }
400 else if (type == "fileselector")
401 {
402 GUIFileSelector* element = new GUIFileSelector(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100403 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 mRenders.push_back(element);
405 mActions.push_back(element);
406 }
407 else if (type == "animation")
408 {
409 GUIAnimation* element = new GUIAnimation(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100410 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200411 mRenders.push_back(element);
412 }
413 else if (type == "progressbar")
414 {
415 GUIProgressBar* element = new GUIProgressBar(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100416 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200417 mRenders.push_back(element);
418 mActions.push_back(element);
419 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 else if (type == "slider")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200421 {
422 GUISlider* element = new GUISlider(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100423 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200424 mRenders.push_back(element);
425 mActions.push_back(element);
426 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200427 else if (type == "slidervalue")
428 {
429 GUISliderValue *element = new GUISliderValue(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100430 mObjects.push_back(element);
Vojtech Bocek85932342013-04-01 22:11:33 +0200431 mRenders.push_back(element);
432 mActions.push_back(element);
433 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400434 else if (type == "listbox")
435 {
436 GUIListBox* element = new GUIListBox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100437 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400438 mRenders.push_back(element);
439 mActions.push_back(element);
440 }
441 else if (type == "keyboard")
442 {
443 GUIKeyboard* element = new GUIKeyboard(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100444 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 mRenders.push_back(element);
446 mActions.push_back(element);
447 }
448 else if (type == "input")
449 {
450 GUIInput* element = new GUIInput(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100451 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 mRenders.push_back(element);
453 mActions.push_back(element);
454 mInputs.push_back(element);
455 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 else if (type == "partitionlist")
457 {
458 GUIPartitionList* element = new GUIPartitionList(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100459 mObjects.push_back(element);
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 mRenders.push_back(element);
461 mActions.push_back(element);
462 }
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100463 else if (type == "patternpassword")
464 {
465 GUIPatternPassword* element = new GUIPatternPassword(child);
466 mObjects.push_back(element);
467 mRenders.push_back(element);
468 mActions.push_back(element);
469 }
Ethan Yonker44925ad2015-07-22 12:33:59 -0500470 else if (type == "textbox")
471 {
472 GUITextBox* element = new GUITextBox(child);
473 mObjects.push_back(element);
474 mRenders.push_back(element);
475 mActions.push_back(element);
476 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200477 else if (type == "template")
478 {
479 if (!templates || !child->first_attribute("name"))
480 {
481 LOGERR("Invalid template request.\n");
482 }
483 else
484 {
485 std::string name = child->first_attribute("name")->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500486 xml_node<>* node;
487 bool node_found = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200489 // We need to find the correct template
Ethan Yonker780cd392014-07-21 15:24:39 -0500490 for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) {
491 node = (*itr)->first_node("template");
Dees_Troy51a0e822012-09-05 15:24:24 -0400492
Ethan Yonker780cd392014-07-21 15:24:39 -0500493 while (node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200494 {
Ethan Yonker780cd392014-07-21 15:24:39 -0500495 if (!node->first_attribute("name"))
496 continue;
497
498 if (name == node->first_attribute("name")->value())
499 {
500 if (!ProcessNode(node, templates, depth + 1))
501 return false;
502 else {
503 node_found = true;
504 break;
505 }
506 }
507 if (node_found)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508 break;
Ethan Yonker780cd392014-07-21 15:24:39 -0500509 node = node->next_sibling("template");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200510 }
thatb63e2f92015-06-27 21:35:11 +0200511 // [check] why is there no if (node_found) here too?
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200512 }
513 }
514 }
515 else
516 {
thatb63e2f92015-06-27 21:35:11 +0200517 LOGERR("Unknown object type: %s.\n", type.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200518 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200519 }
520 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400521}
522
523int Page::Render(void)
524{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200525 // Render background
526 gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha);
527 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 // Render remaining objects
530 std::vector<RenderObject*>::iterator iter;
531 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
532 {
533 if ((*iter)->Render())
534 LOGERR("A render request has failed.\n");
535 }
536 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400537}
538
539int Page::Update(void)
540{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200541 int retCode = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400542
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200543 std::vector<RenderObject*>::iterator iter;
544 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
545 {
546 int ret = (*iter)->Update();
547 if (ret < 0)
548 LOGERR("An update request has failed.\n");
549 else if (ret > retCode)
550 retCode = ret;
551 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400552
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200553 return retCode;
Dees_Troy51a0e822012-09-05 15:24:24 -0400554}
555
556int Page::NotifyTouch(TOUCH_STATE state, int x, int y)
557{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200558 // By default, return 1 to ignore further touches if nobody is listening
559 int ret = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400560
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200561 // Don't try to handle a lack of handlers
562 if (mActions.size() == 0)
563 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400564
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200565 // We record mTouchStart so we can pass all the touch stream to the same handler
566 if (state == TOUCH_START)
567 {
568 std::vector<ActionObject*>::reverse_iterator iter;
569 // We work backwards, from top-most element to bottom-most element
570 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
571 {
572 if ((*iter)->IsInRegion(x, y))
573 {
574 mTouchStart = (*iter);
575 ret = mTouchStart->NotifyTouch(state, x, y);
576 if (ret >= 0)
577 break;
578 mTouchStart = NULL;
579 }
580 }
581 }
582 else if (state == TOUCH_RELEASE && mTouchStart != NULL)
583 {
584 ret = mTouchStart->NotifyTouch(state, x, y);
585 mTouchStart = NULL;
586 }
587 else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL)
588 {
589 ret = mTouchStart->NotifyTouch(state, x, y);
590 }
591 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400592}
593
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100594int Page::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400595{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200596 std::vector<ActionObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400597
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100598 int ret = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 // We work backwards, from top-most element to bottom-most element
600 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
601 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100602 ret = (*iter)->NotifyKey(key, down);
that8834a0f2016-01-05 23:29:30 +0100603 if (ret == 0)
604 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100605 if (ret < 0) {
606 LOGERR("An action handler has returned an error\n");
607 ret = 1;
608 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200609 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100610 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400611}
612
that8834a0f2016-01-05 23:29:30 +0100613int Page::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -0400614{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200615 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400616
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200617 // We work backwards, from top-most element to bottom-most element
618 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
619 {
that8834a0f2016-01-05 23:29:30 +0100620 int ret = (*iter)->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200621 if (ret == 0)
622 return 0;
623 else if (ret < 0)
that8834a0f2016-01-05 23:29:30 +0100624 LOGERR("A char input handler has returned an error");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200625 }
626 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400627}
628
629int Page::SetKeyBoardFocus(int inFocus)
630{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200631 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400632
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200633 // We work backwards, from top-most element to bottom-most element
634 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
635 {
636 int ret = (*iter)->SetInputFocus(inFocus);
637 if (ret == 0)
638 return 0;
639 else if (ret < 0)
640 LOGERR("An input focus handler has returned an error");
641 }
642 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400643}
644
645void Page::SetPageFocus(int inFocus)
646{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200647 // Render remaining objects
648 std::vector<RenderObject*>::iterator iter;
649 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
650 (*iter)->SetPageFocus(inFocus);
651
652 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400653}
654
655int Page::NotifyVarChange(std::string varName, std::string value)
656{
Vojtech Bocek07220562014-02-08 02:05:33 +0100657 std::vector<GUIObject*>::iterator iter;
658 for (iter = mObjects.begin(); iter != mObjects.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200659 {
660 if ((*iter)->NotifyVarChange(varName, value))
661 LOGERR("An action handler errored on NotifyVarChange.\n");
662 }
663 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400664}
665
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500666PageSet::PageSet(const char* xmlFile)
Dees_Troy51a0e822012-09-05 15:24:24 -0400667{
that74ac6062015-03-04 22:39:34 +0100668 mResources = new ResourceManager;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200669 mCurrentPage = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400670
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500671 if (!xmlFile)
thatb63e2f92015-06-27 21:35:11 +0200672 mCurrentPage = new Page(NULL, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400673}
674
675PageSet::~PageSet()
676{
Ethan Yonker1c273312015-03-16 12:18:56 -0500677 mOverlays.clear();
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100678 for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
679 delete *itr;
680
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200681 delete mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400682}
683
Ethan Yonker74db1572015-10-28 12:44:49 -0500684int PageSet::LoadLanguage(char* languageFile, ZipArchive* package)
685{
686 xml_document<> lang;
687 xml_node<>* parent;
688 xml_node<>* child;
689 std::string resource_source;
690
691 if (languageFile) {
692 printf("parsing languageFile\n");
693 lang.parse<0>(languageFile);
694 printf("parsing languageFile done\n");
695 } else {
696 return -1;
697 }
698
699 parent = lang.first_node("language");
700 if (!parent) {
701 LOGERR("Unable to locate language node in language file.\n");
702 lang.clear();
703 return -1;
704 }
705
706 child = parent->first_node("display");
707 if (child) {
708 DataManager::SetValue("tw_language_display", child->value());
709 resource_source = child->value();
710 } else {
711 LOGERR("language file does not have a display value set\n");
712 DataManager::SetValue("tw_language_display", "Not Set");
713 resource_source = languageFile;
714 }
715
716 child = parent->first_node("resources");
717 if (child)
718 mResources->LoadResources(child, package, resource_source);
719 else
720 return -1;
721 lang.clear();
722 return 0;
723}
724
Ethan Yonker8e5692f2016-01-21 11:21:06 -0600725int PageSet::Load(ZipArchive* package, char* xmlFile, char* languageFile, char* baseLanguageFile)
Dees_Troy51a0e822012-09-05 15:24:24 -0400726{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500727 xml_document<> mDoc;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200728 xml_node<>* parent;
729 xml_node<>* child;
Ethan Yonker780cd392014-07-21 15:24:39 -0500730 xml_node<>* xmltemplate;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600731 xml_node<>* xmlstyle;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500732
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500733 mDoc.parse<0>(xmlFile);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734 parent = mDoc.first_node("recovery");
735 if (!parent)
736 parent = mDoc.first_node("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400737
Ethan Yonker63e414f2015-02-06 15:44:39 -0600738 set_scale_values(1, 1); // Reset any previous scaling values
739
Ethan Yonker8e5692f2016-01-21 11:21:06 -0600740 if (baseLanguageFile)
741 LoadLanguage(baseLanguageFile, NULL);
742
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200743 // Now, let's parse the XML
Ethan Yonker63e414f2015-02-06 15:44:39 -0600744 child = parent->first_node("details");
745 if (child) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600746 int theme_ver = 0;
747 xml_node<>* themeversion = child->first_node("themeversion");
748 if (themeversion && themeversion->value()) {
749 theme_ver = atoi(themeversion->value());
750 } else {
751 LOGINFO("No themeversion in theme.\n");
752 }
753 if (theme_ver != TW_THEME_VERSION) {
754 LOGINFO("theme version from xml: %i, expected %i\n", theme_ver, TW_THEME_VERSION);
755 if (package) {
756 gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme.");
757 mDoc.clear();
Ethan Yonker8e5692f2016-01-21 11:21:06 -0600758 return TW_THEME_VER_ERR;
Ethan Yonker1308d532016-01-14 22:21:49 -0600759 } else {
760 gui_print_color("warning", "Stock theme version does not match TWRP version.\n");
761 }
762 }
Ethan Yonker63e414f2015-02-06 15:44:39 -0600763 xml_node<>* resolution = child->first_node("resolution");
764 if (resolution) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600765 LOGINFO("Checking resolution...\n");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600766 xml_attribute<>* width_attr = resolution->first_attribute("width");
767 xml_attribute<>* height_attr = resolution->first_attribute("height");
768 xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
769 if (width_attr && height_attr && !noscale_attr) {
770 int width = atoi(width_attr->value());
771 int height = atoi(height_attr->value());
772 int offx = 0, offy = 0;
773#ifdef TW_ROUND_SCREEN
774 xml_node<>* roundscreen = child->first_node("roundscreen");
775 if (roundscreen) {
776 LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n");
777 xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x");
778 xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y");
779 if (offx_attr) {
780 offx = atoi(offx_attr->value());
781 }
782 if (offy_attr) {
783 offy = atoi(offy_attr->value());
784 }
785 }
786#endif
787 if (width != 0 && height != 0) {
788 float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width;
789 float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height;
790#ifdef TW_ROUND_SCREEN
791 float scale_off_w = (float)gr_fb_width() / (float)width;
792 float scale_off_h = (float)gr_fb_height() / (float)height;
793 tw_x_offset = offx * scale_off_w;
794 tw_y_offset = offy * scale_off_h;
795#endif
796 if (scale_w != 1 || scale_h != 1) {
797 LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset);
798 set_scale_values(scale_w, scale_h);
799 }
800 }
801 } else {
802 LOGINFO("XML does not contain width and height, no scaling will be applied\n");
803 }
804 } else {
805 LOGINFO("XML contains no resolution tag, no scaling will be applied.\n");
806 }
807 } else {
808 LOGINFO("XML contains no details tag, no scaling will be applied.\n");
809 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500810
811 if (languageFile)
812 LoadLanguage(languageFile, package);
813
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200814 LOGINFO("Loading resources...\n");
815 child = parent->first_node("resources");
816 if (child)
Ethan Yonker74db1572015-10-28 12:44:49 -0500817 mResources->LoadResources(child, package, "theme");
Dees_Troy51a0e822012-09-05 15:24:24 -0400818
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200819 LOGINFO("Loading variables...\n");
820 child = parent->first_node("variables");
821 if (child)
822 LoadVariables(child);
Dees_Troy51a0e822012-09-05 15:24:24 -0400823
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100824 LOGINFO("Loading mouse cursor...\n");
825 child = parent->first_node("mousecursor");
826 if(child)
827 PageManager::LoadCursorData(child);
828
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200829 LOGINFO("Loading pages...\n");
830 // This may be NULL if no templates are present
Ethan Yonker780cd392014-07-21 15:24:39 -0500831 xmltemplate = parent->first_node("templates");
832 if (xmltemplate)
833 templates.push_back(xmltemplate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600835 // Load styles if present
836 xmlstyle = parent->first_node("styles");
837 if (xmlstyle)
838 styles.push_back(xmlstyle);
839
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200840 child = parent->first_node("pages");
Ethan Yonker780cd392014-07-21 15:24:39 -0500841 if (child) {
842 if (LoadPages(child)) {
843 LOGERR("PageSet::Load returning -1\n");
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500844 mDoc.clear();
Ethan Yonker780cd392014-07-21 15:24:39 -0500845 return -1;
846 }
847 }
Vojtech Boceke979abd2015-01-12 18:29:12 +0100848
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500849 int ret = CheckInclude(package, &mDoc);
850 mDoc.clear();
851 templates.clear();
852 return ret;
Ethan Yonker780cd392014-07-21 15:24:39 -0500853}
Dees_Troy51a0e822012-09-05 15:24:24 -0400854
Ethan Yonker780cd392014-07-21 15:24:39 -0500855int PageSet::CheckInclude(ZipArchive* package, xml_document<> *parentDoc)
856{
857 xml_node<>* par;
858 xml_node<>* par2;
859 xml_node<>* chld;
860 xml_node<>* parent;
861 xml_node<>* child;
862 xml_node<>* xmltemplate;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600863 xml_node<>* xmlstyle;
Ethan Yonker780cd392014-07-21 15:24:39 -0500864 long len;
865 char* xmlFile = NULL;
866 string filename;
Vojtech Boceke979abd2015-01-12 18:29:12 +0100867 xml_document<> *doc = NULL;
Ethan Yonker780cd392014-07-21 15:24:39 -0500868
869 par = parentDoc->first_node("recovery");
870 if (!par) {
871 par = parentDoc->first_node("install");
872 }
873 if (!par) {
874 return 0;
875 }
876
877 par2 = par->first_node("include");
878 if (!par2)
879 return 0;
880 chld = par2->first_node("xmlfile");
881 while (chld != NULL) {
882 xml_attribute<>* attr = chld->first_attribute("name");
883 if (!attr)
884 break;
885
Ethan Yonker780cd392014-07-21 15:24:39 -0500886 if (!package) {
887 // We can try to load the XML directly...
Dees Troy3454ade2015-01-20 19:21:04 +0000888 filename = TWRES;
Ethan Yonker780cd392014-07-21 15:24:39 -0500889 filename += attr->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500890 } else {
891 filename += attr->value();
Ethan Yonker561c58d2015-10-05 08:48:22 -0500892 }
893 xmlFile = PageManager::LoadFileToBuffer(filename, package);
894 if (xmlFile == NULL) {
895 LOGERR("PageSet::CheckInclude unable to load '%s'\n", filename.c_str());
896 return -1;
Ethan Yonker780cd392014-07-21 15:24:39 -0500897 }
Ethan Yonker780cd392014-07-21 15:24:39 -0500898
Vojtech Boceke979abd2015-01-12 18:29:12 +0100899 doc = new xml_document<>();
900 doc->parse<0>(xmlFile);
901
902 parent = doc->first_node("recovery");
Ethan Yonker780cd392014-07-21 15:24:39 -0500903 if (!parent)
Vojtech Boceke979abd2015-01-12 18:29:12 +0100904 parent = doc->first_node("install");
Ethan Yonker780cd392014-07-21 15:24:39 -0500905
906 // Now, let's parse the XML
907 LOGINFO("Loading included resources...\n");
908 child = parent->first_node("resources");
909 if (child)
Ethan Yonker74db1572015-10-28 12:44:49 -0500910 mResources->LoadResources(child, package, "theme");
Ethan Yonker780cd392014-07-21 15:24:39 -0500911
912 LOGINFO("Loading included variables...\n");
913 child = parent->first_node("variables");
914 if (child)
915 LoadVariables(child);
916
917 LOGINFO("Loading mouse cursor...\n");
918 child = parent->first_node("mousecursor");
919 if(child)
920 PageManager::LoadCursorData(child);
921
922 LOGINFO("Loading included pages...\n");
923 // This may be NULL if no templates are present
924 xmltemplate = parent->first_node("templates");
925 if (xmltemplate)
926 templates.push_back(xmltemplate);
927
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600928 // Load styles if present
929 xmlstyle = parent->first_node("styles");
930 if (xmlstyle)
931 styles.push_back(xmlstyle);
932
Ethan Yonker780cd392014-07-21 15:24:39 -0500933 child = parent->first_node("pages");
Vojtech Boceke979abd2015-01-12 18:29:12 +0100934 if (child && LoadPages(child))
935 {
936 templates.pop_back();
937 doc->clear();
938 delete doc;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500939 free(xmlFile);
Vojtech Boceke979abd2015-01-12 18:29:12 +0100940 return -1;
941 }
Ethan Yonker780cd392014-07-21 15:24:39 -0500942
Ethan Yonker561c58d2015-10-05 08:48:22 -0500943 if (CheckInclude(package, doc)) {
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500944 doc->clear();
945 delete doc;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500946 free(xmlFile);
Ethan Yonker780cd392014-07-21 15:24:39 -0500947 return -1;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500948 }
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500949 doc->clear();
950 delete doc;
951 free(xmlFile);
Ethan Yonker780cd392014-07-21 15:24:39 -0500952
953 chld = chld->next_sibling("xmlfile");
954 }
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500955
Ethan Yonker780cd392014-07-21 15:24:39 -0500956 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400957}
958
959int PageSet::SetPage(std::string page)
960{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200961 Page* tmp = FindPage(page);
962 if (tmp)
963 {
964 if (mCurrentPage) mCurrentPage->SetPageFocus(0);
965 mCurrentPage = tmp;
966 mCurrentPage->SetPageFocus(1);
967 mCurrentPage->NotifyVarChange("", "");
968 return 0;
969 }
970 else
971 {
972 LOGERR("Unable to locate page (%s)\n", page.c_str());
973 }
974 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400975}
976
977int PageSet::SetOverlay(Page* page)
978{
Ethan Yonker1c273312015-03-16 12:18:56 -0500979 if (page) {
980 if (mOverlays.size() >= 10) {
981 LOGERR("Too many overlays requested, max is 10.\n");
982 return -1;
983 }
Matt Mowerd411f8d2015-04-09 16:04:12 -0500984
985 std::vector<Page*>::iterator iter;
986 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
987 if ((*iter)->GetName() == page->GetName()) {
988 mOverlays.erase(iter);
989 // SetOverlay() is (and should stay) the only function which
990 // adds to mOverlays. Then, each page can appear at most once.
991 break;
992 }
993 }
994
Ethan Yonker1c273312015-03-16 12:18:56 -0500995 page->SetPageFocus(1);
996 page->NotifyVarChange("", "");
997
998 if (!mOverlays.empty())
999 mOverlays.back()->SetPageFocus(0);
1000
1001 mOverlays.push_back(page);
1002 } else {
1003 if (!mOverlays.empty()) {
1004 mOverlays.back()->SetPageFocus(0);
1005 mOverlays.pop_back();
1006 if (!mOverlays.empty())
1007 mOverlays.back()->SetPageFocus(1);
1008 else if (mCurrentPage)
1009 mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
1010 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001011 }
1012 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001013}
1014
that74ac6062015-03-04 22:39:34 +01001015const ResourceManager* PageSet::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001016{
that74ac6062015-03-04 22:39:34 +01001017 return mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -04001018}
1019
1020Page* PageSet::FindPage(std::string name)
1021{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001022 std::vector<Page*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001023
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001024 for (iter = mPages.begin(); iter != mPages.end(); iter++)
1025 {
1026 if (name == (*iter)->GetName())
1027 return (*iter);
1028 }
1029 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001030}
1031
1032int PageSet::LoadVariables(xml_node<>* vars)
1033{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001034 xml_node<>* child;
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001035 xml_attribute<> *name, *value, *persist;
1036 int p;
Dees_Troy51a0e822012-09-05 15:24:24 -04001037
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001038 child = vars->first_node("variable");
1039 while (child)
1040 {
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001041 name = child->first_attribute("name");
1042 value = child->first_attribute("value");
1043 persist = child->first_attribute("persist");
1044 if(name && value)
1045 {
Ethan Yonker751a85e2014-12-12 16:59:10 -06001046 if (strcmp(name->value(), "tw_x_offset") == 0) {
1047 tw_x_offset = atoi(value->value());
1048 child = child->next_sibling("variable");
1049 continue;
1050 }
1051 if (strcmp(name->value(), "tw_y_offset") == 0) {
1052 tw_y_offset = atoi(value->value());
1053 child = child->next_sibling("variable");
1054 continue;
1055 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001056 p = persist ? atoi(persist->value()) : 0;
Ethan Yonker96acb3d2014-08-05 09:20:30 -05001057 string temp = value->value();
1058 string valstr = gui_parse_text(temp);
1059
1060 if (valstr.find("+") != string::npos) {
1061 string val1str = valstr;
1062 val1str = val1str.substr(0, val1str.find('+'));
1063 string val2str = valstr;
1064 val2str = val2str.substr(val2str.find('+') + 1, string::npos);
1065 int val1 = atoi(val1str.c_str());
1066 int val2 = atoi(val2str.c_str());
1067 int val = val1 + val2;
1068
1069 DataManager::SetValue(name->value(), val, p);
1070 } else if (valstr.find("-") != string::npos) {
1071 string val1str = valstr;
1072 val1str = val1str.substr(0, val1str.find('-'));
1073 string val2str = valstr;
1074 val2str = val2str.substr(val2str.find('-') + 1, string::npos);
1075 int val1 = atoi(val1str.c_str());
1076 int val2 = atoi(val2str.c_str());
1077 int val = val1 - val2;
1078
1079 DataManager::SetValue(name->value(), val, p);
1080 } else {
1081 DataManager::SetValue(name->value(), valstr, p);
1082 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001083 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001085 child = child->next_sibling("variable");
1086 }
1087 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001088}
1089
Ethan Yonker780cd392014-07-21 15:24:39 -05001090int PageSet::LoadPages(xml_node<>* pages)
Dees_Troy51a0e822012-09-05 15:24:24 -04001091{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001092 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001094 if (!pages)
1095 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001096
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001097 child = pages->first_node("page");
1098 while (child != NULL)
1099 {
Ethan Yonker780cd392014-07-21 15:24:39 -05001100 Page* page = new Page(child, &templates);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001101 if (page->GetName().empty())
1102 {
1103 LOGERR("Unable to process load page\n");
1104 delete page;
1105 }
1106 else
1107 {
1108 mPages.push_back(page);
1109 }
1110 child = child->next_sibling("page");
1111 }
1112 if (mPages.size() > 0)
1113 return 0;
1114 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001115}
1116
1117int PageSet::IsCurrentPage(Page* page)
1118{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001119 return ((mCurrentPage && mCurrentPage == page) ? 1 : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001120}
1121
that10ae24f2015-12-26 20:53:51 +01001122std::string PageSet::GetCurrentPage() const
1123{
1124 return mCurrentPage ? mCurrentPage->GetName() : "";
1125}
1126
Dees_Troy51a0e822012-09-05 15:24:24 -04001127int PageSet::Render(void)
1128{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001129 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001130
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001131 ret = (mCurrentPage ? mCurrentPage->Render() : -1);
1132 if (ret < 0)
1133 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001134
1135 std::vector<Page*>::iterator iter;
1136
1137 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1138 ret = ((*iter) ? (*iter)->Render() : -1);
1139 if (ret < 0)
1140 return ret;
1141 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001142 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001143}
1144
1145int PageSet::Update(void)
1146{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001147 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001148
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001149 ret = (mCurrentPage ? mCurrentPage->Update() : -1);
1150 if (ret < 0 || ret > 1)
1151 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001152
1153 std::vector<Page*>::iterator iter;
1154
1155 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1156 ret = ((*iter) ? (*iter)->Update() : -1);
1157 if (ret < 0)
1158 return ret;
1159 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001160 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001161}
1162
1163int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
1164{
Ethan Yonker1c273312015-03-16 12:18:56 -05001165 if (!mOverlays.empty())
1166 return mOverlays.back()->NotifyTouch(state, x, y);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001167
1168 return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001169}
1170
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001171int PageSet::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001172{
Ethan Yonker1c273312015-03-16 12:18:56 -05001173 if (!mOverlays.empty())
1174 return mOverlays.back()->NotifyKey(key, down);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001175
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001176 return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
that8834a0f2016-01-05 23:29:30 +01001179int PageSet::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001180{
Ethan Yonker1c273312015-03-16 12:18:56 -05001181 if (!mOverlays.empty())
that8834a0f2016-01-05 23:29:30 +01001182 return mOverlays.back()->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001183
that8834a0f2016-01-05 23:29:30 +01001184 return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001185}
1186
1187int PageSet::SetKeyBoardFocus(int inFocus)
1188{
Ethan Yonker1c273312015-03-16 12:18:56 -05001189 if (!mOverlays.empty())
1190 return mOverlays.back()->SetKeyBoardFocus(inFocus);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001191
1192 return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001193}
1194
1195int PageSet::NotifyVarChange(std::string varName, std::string value)
1196{
Ethan Yonker1c273312015-03-16 12:18:56 -05001197 std::vector<Page*>::iterator iter;
1198
1199 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
1200 (*iter)->NotifyVarChange(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001201
1202 return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001203}
1204
Ethan Yonker74db1572015-10-28 12:44:49 -05001205void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1206{
1207 mResources->AddStringResource(resource_source, resource_name, value);
1208}
1209
Ethan Yonker561c58d2015-10-05 08:48:22 -05001210char* PageManager::LoadFileToBuffer(std::string filename, ZipArchive* package) {
1211 size_t len;
1212 char* buffer = NULL;
1213
1214 if (!package) {
1215 // We can try to load the XML directly...
1216 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str());
1217 struct stat st;
1218 if(stat(filename.c_str(),&st) != 0) {
1219 // This isn't always an error, sometimes we request files that don't exist.
1220 return NULL;
1221 }
1222
1223 len = (size_t)st.st_size;
1224
1225 buffer = (char*) malloc(len + 1);
1226 if (!buffer) {
1227 LOGERR("PageManager::LoadFileToBuffer failed to malloc\n");
1228 return NULL;
1229 }
1230
1231 int fd = open(filename.c_str(), O_RDONLY);
1232 if (fd == -1) {
1233 LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno));
1234 free(buffer);
1235 return NULL;
1236 }
1237
1238 if (read(fd, buffer, len) < 0) {
1239 LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno));
1240 free(buffer);
1241 close(fd);
1242 return NULL;
1243 }
1244 close(fd);
1245 } else {
1246 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
1247 const ZipEntry* zipentry = mzFindZipEntry(package, filename.c_str());
1248 if (zipentry == NULL) {
1249 LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
1250 return NULL;
1251 }
1252
1253 // Allocate the buffer for the file
1254 len = mzGetZipEntryUncompLen(zipentry);
1255 buffer = (char*) malloc(len + 1);
1256 if (!buffer)
1257 return NULL;
1258
1259 if (!mzExtractZipEntryToBuffer(package, zipentry, (unsigned char*) buffer)) {
1260 LOGERR("Unable to extract '%s'\n", filename.c_str());
1261 free(buffer);
1262 return NULL;
1263 }
1264 }
1265 // NULL-terminate the string
1266 buffer[len] = 0x00;
1267 return buffer;
1268}
1269
Ethan Yonker74db1572015-10-28 12:44:49 -05001270void PageManager::LoadLanguageListDir(string dir) {
1271 if (!TWFunc::Path_Exists(dir)) {
1272 LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str());
1273 return;
1274 }
1275
1276 DIR *d = opendir(dir.c_str());
1277 struct dirent *p;
1278
1279 if (d == NULL) {
1280 LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno));
1281 return;
1282 }
1283
1284 while ((p = readdir(d))) {
1285 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5)
1286 continue;
1287
1288 string file = p->d_name;
1289 if (file.substr(strlen(p->d_name) - 4) != ".xml")
1290 continue;
1291 string path = dir + p->d_name;
1292 string file_no_extn = file.substr(0, strlen(p->d_name) - 4);
1293 struct language_struct language_entry;
1294 language_entry.filename = file_no_extn;
1295 char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL);
1296 if (xmlFile == NULL) {
1297 LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str());
1298 continue;
1299 }
1300 xml_document<> *doc = new xml_document<>();
1301 doc->parse<0>(xmlFile);
1302
1303 xml_node<>* parent = doc->first_node("language");
1304 if (!parent) {
1305 LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str());
1306 } else {
1307 xml_node<>* child = parent->first_node("display");
1308 if (child) {
1309 language_entry.displayvalue = child->value();
1310 } else {
1311 LOGERR("No display value for '%s'\n", language_entry.filename.c_str());
1312 language_entry.displayvalue = language_entry.filename;
1313 }
1314 Language_List.push_back(language_entry);
1315 }
1316 doc->clear();
1317 delete doc;
1318 free(xmlFile);
1319 }
1320 closedir(d);
1321}
1322
1323void PageManager::LoadLanguageList(ZipArchive* package) {
1324 Language_List.clear();
1325 if (TWFunc::Path_Exists(TWRES "customlanguages"))
1326 TWFunc::removeDir(TWRES "customlanguages", true);
1327 if (package) {
1328 TWFunc::Recursive_Mkdir(TWRES "customlanguages");
1329 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
1330 mzExtractRecursive(package, "languages", TWRES "customlanguages/", &timestamp, NULL, NULL, NULL);
1331 LoadLanguageListDir(TWRES "customlanguages/");
1332 } else {
1333 LoadLanguageListDir(TWRES "languages/");
1334 }
1335}
1336
1337void PageManager::LoadLanguage(string filename) {
1338 string actual_filename;
1339 if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml"))
1340 actual_filename = TWRES "customlanguages/" + filename + ".xml";
1341 else
1342 actual_filename = TWRES "languages/" + filename + ".xml";
1343 char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL);
1344 if (xmlFile == NULL)
1345 LOGERR("Unable to load '%s'\n", actual_filename.c_str());
1346 else {
1347 mCurrentSet->LoadLanguage(xmlFile, NULL);
1348 free(xmlFile);
1349 }
1350 PartitionManager.Translate_Partition_Display_Names();
1351}
1352
Dees_Troy51a0e822012-09-05 15:24:24 -04001353int PageManager::LoadPackage(std::string name, std::string package, std::string startpage)
1354{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001355 int fd;
1356 ZipArchive zip, *pZip = NULL;
1357 long len;
1358 char* xmlFile = NULL;
Ethan Yonker74db1572015-10-28 12:44:49 -05001359 char* languageFile = NULL;
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001360 char* baseLanguageFile = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001361 PageSet* pageSet = NULL;
1362 int ret;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001363 MemMapping map;
Dees_Troy51a0e822012-09-05 15:24:24 -04001364
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001365 mReloadTheme = false;
1366 mStartPage = startpage;
1367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001368 // Open the XML file
1369 LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str());
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001370 if (package.size() > 4 && package.substr(package.size() - 4) != ".zip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001371 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001372 LOGINFO("Load XML directly\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001373 tw_x_offset = TW_X_OFFSET;
1374 tw_y_offset = TW_Y_OFFSET;
Ethan Yonker74db1572015-10-28 12:44:49 -05001375 LoadLanguageList(NULL);
1376 languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001377 }
1378 else
1379 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001380 LOGINFO("Loading zip theme\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001381 tw_x_offset = 0;
1382 tw_y_offset = 0;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001383 if (!TWFunc::Path_Exists(package))
1384 return -1;
1385 if (sysMapFile(package.c_str(), &map) != 0) {
1386 LOGERR("Failed to map '%s'\n", package.c_str());
Ethan Yonker561c58d2015-10-05 08:48:22 -05001387 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001388 }
1389 if (mzOpenZipArchive(map.addr, map.length, &zip)) {
1390 LOGERR("Unable to open zip archive '%s'\n", package.c_str());
1391 sysReleaseMap(&map);
Ethan Yonker561c58d2015-10-05 08:48:22 -05001392 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001393 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001394 pZip = &zip;
Ethan Yonker561c58d2015-10-05 08:48:22 -05001395 package = "ui.xml";
Ethan Yonker74db1572015-10-28 12:44:49 -05001396 LoadLanguageList(pZip);
1397 languageFile = LoadFileToBuffer("languages/en.xml", pZip);
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001398 baseLanguageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001399 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001400
Ethan Yonker561c58d2015-10-05 08:48:22 -05001401 xmlFile = LoadFileToBuffer(package, pZip);
1402 if (xmlFile == NULL) {
1403 goto error;
1404 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001405
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001406 // Before loading, mCurrentSet must be the loading package so we can find resources
1407 pageSet = mCurrentSet;
1408 mCurrentSet = new PageSet(xmlFile);
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001409 ret = mCurrentSet->Load(pZip, xmlFile, languageFile, baseLanguageFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 if (languageFile) {
1411 free(languageFile);
1412 languageFile = NULL;
1413 }
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001414 if (ret == 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001415 mCurrentSet->SetPage(startpage);
1416 mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001417 } else {
1418 if (ret != TW_THEME_VER_ERR)
1419 LOGERR("Package %s failed to load.\n", name.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001420 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001421
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001422 // The first successful package we loaded is the base
1423 if (mBaseSet == NULL)
1424 mBaseSet = mCurrentSet;
1425
1426 mCurrentSet = pageSet;
1427
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001428 if (pZip) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001429 mzCloseZipArchive(pZip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001430 sysReleaseMap(&map);
1431 }
Ethan Yonker561c58d2015-10-05 08:48:22 -05001432 free(xmlFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001433 if (languageFile)
1434 free(languageFile);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001435 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001436
1437error:
Ethan Yonker561c58d2015-10-05 08:48:22 -05001438 // Sometimes we get here without a real error
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001439 if (pZip) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001440 mzCloseZipArchive(pZip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001441 sysReleaseMap(&map);
1442 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001443 if (xmlFile)
1444 free(xmlFile);
1445 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001446}
1447
1448PageSet* PageManager::FindPackage(std::string name)
1449{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001450 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001452 iter = mPageSets.find(name);
1453 if (iter != mPageSets.end())
1454 return (*iter).second;
1455
1456 LOGERR("Unable to locate package %s\n", name.c_str());
1457 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001458}
1459
1460PageSet* PageManager::SelectPackage(std::string name)
1461{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001462 LOGINFO("Switching packages (%s)\n", name.c_str());
1463 PageSet* tmp;
Dees_Troy51a0e822012-09-05 15:24:24 -04001464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001465 tmp = FindPackage(name);
1466 if (tmp)
Vojtech Bocek07220562014-02-08 02:05:33 +01001467 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001468 mCurrentSet = tmp;
Vojtech Bocek07220562014-02-08 02:05:33 +01001469 mCurrentSet->NotifyVarChange("", "");
1470 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001471 else
1472 LOGERR("Unable to find package.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001473
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001474 return mCurrentSet;
Dees_Troy51a0e822012-09-05 15:24:24 -04001475}
1476
1477int PageManager::ReloadPackage(std::string name, std::string package)
1478{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001479 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001480
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001481 mReloadTheme = false;
1482
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001483 iter = mPageSets.find(name);
1484 if (iter == mPageSets.end())
1485 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001486
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001487 if(mMouseCursor)
1488 mMouseCursor->ResetData(gr_fb_width(), gr_fb_height());
1489
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001490 PageSet* set = (*iter).second;
1491 mPageSets.erase(iter);
Dees_Troy51a0e822012-09-05 15:24:24 -04001492
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001493 if (LoadPackage(name, package, mStartPage) != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001494 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001495 LOGINFO("Failed to load package '%s'.\n", package.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001496 mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
1497 return -1;
1498 }
1499 if (mCurrentSet == set)
1500 SelectPackage(name);
Vojtech Bocekbfb63342014-02-08 00:32:31 +01001501 if (mBaseSet == set)
1502 mBaseSet = mCurrentSet;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001503 delete set;
Ethan Yonker74db1572015-10-28 12:44:49 -05001504 GUIConsole::Translate_Now();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001505 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001506}
1507
1508void PageManager::ReleasePackage(std::string name)
1509{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001510 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001511
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001512 iter = mPageSets.find(name);
1513 if (iter == mPageSets.end())
1514 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001515
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001516 PageSet* set = (*iter).second;
1517 mPageSets.erase(iter);
1518 delete set;
1519 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001520}
1521
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001522int PageManager::RunReload() {
1523 int ret_val = 0;
1524 std::string theme_path;
1525
1526 if (!mReloadTheme)
1527 return 0;
1528
1529 mReloadTheme = false;
1530 theme_path = DataManager::GetSettingsStoragePath();
1531 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1532 LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
1533 ret_val = 1;
1534 }
1535
1536 theme_path += "/TWRP/theme/ui.zip";
1537 if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0)
1538 {
1539 // Loading the custom theme failed - try loading the stock theme
1540 LOGINFO("Attempting to reload stock theme...\n");
1541 if (ReloadPackage("TWRP", TWRES "ui.xml"))
1542 {
1543 LOGERR("Failed to load base packages.\n");
1544 ret_val = 1;
1545 }
1546 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001547 if (ret_val == 0) {
1548 if (DataManager::GetStrValue("tw_language") != "en.xml") {
1549 LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str());
1550 LoadLanguage(DataManager::GetStrValue("tw_language"));
1551 }
1552 }
1553
1554 // This makes the console re-translate
1555 last_message_count = 0;
1556 gConsole.clear();
1557 gConsoleColor.clear();
1558
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001559 return ret_val;
1560}
1561
1562void PageManager::RequestReload() {
1563 mReloadTheme = true;
1564}
1565
Dees_Troy51a0e822012-09-05 15:24:24 -04001566int PageManager::ChangePage(std::string name)
1567{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001568 DataManager::SetValue("tw_operation_state", 0);
1569 int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1);
1570 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001571}
1572
that10ae24f2015-12-26 20:53:51 +01001573std::string PageManager::GetCurrentPage()
1574{
1575 return mCurrentSet ? mCurrentSet->GetCurrentPage() : "";
1576}
1577
Dees_Troy51a0e822012-09-05 15:24:24 -04001578int PageManager::ChangeOverlay(std::string name)
1579{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001580 if (name.empty())
1581 return mCurrentSet->SetOverlay(NULL);
1582 else
1583 {
Ethan Yonker1308d532016-01-14 22:21:49 -06001584 Page* page = mCurrentSet ? mCurrentSet->FindPage(name) : NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001585 return mCurrentSet->SetOverlay(page);
1586 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001587}
1588
that74ac6062015-03-04 22:39:34 +01001589const ResourceManager* PageManager::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001590{
that74ac6062015-03-04 22:39:34 +01001591 return (mCurrentSet ? mCurrentSet->GetResources() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001592}
1593
Dees_Troy51a0e822012-09-05 15:24:24 -04001594int PageManager::IsCurrentPage(Page* page)
1595{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001596 return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001597}
1598
1599int PageManager::Render(void)
1600{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001601 if(blankTimer.isScreenOff())
1602 return 0;
1603
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001604 int res = (mCurrentSet ? mCurrentSet->Render() : -1);
1605 if(mMouseCursor)
1606 mMouseCursor->Render();
1607 return res;
1608}
1609
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001610HardwareKeyboard *PageManager::GetHardwareKeyboard()
1611{
1612 if(!mHardwareKeyboard)
1613 mHardwareKeyboard = new HardwareKeyboard();
1614 return mHardwareKeyboard;
1615}
1616
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001617xml_node<>* PageManager::FindStyle(std::string name)
1618{
1619 for (std::vector<xml_node<>*>::iterator itr = mCurrentSet->styles.begin(); itr != mCurrentSet->styles.end(); itr++) {
1620 xml_node<>* node = (*itr)->first_node("style");
1621
1622 while (node) {
1623 if (!node->first_attribute("name"))
1624 continue;
1625
1626 if (name == node->first_attribute("name")->value())
1627 return node;
1628 node = node->next_sibling("style");
1629 }
1630 }
1631 return NULL;
1632}
1633
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001634MouseCursor *PageManager::GetMouseCursor()
1635{
1636 if(!mMouseCursor)
1637 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1638 return mMouseCursor;
1639}
1640
1641void PageManager::LoadCursorData(xml_node<>* node)
1642{
1643 if(!mMouseCursor)
1644 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1645
1646 mMouseCursor->LoadData(node);
Dees_Troy51a0e822012-09-05 15:24:24 -04001647}
1648
1649int PageManager::Update(void)
1650{
thatfb759d42015-01-11 12:16:53 +01001651 if(blankTimer.isScreenOff())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001652 return 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001653
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001654 if (RunReload())
1655 return -2;
1656
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001657 int res = (mCurrentSet ? mCurrentSet->Update() : -1);
1658
1659 if(mMouseCursor)
1660 {
1661 int c_res = mMouseCursor->Update();
1662 if(c_res > res)
1663 res = c_res;
1664 }
1665 return res;
Dees_Troy51a0e822012-09-05 15:24:24 -04001666}
1667
1668int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y)
1669{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001670 return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001671}
1672
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001673int PageManager::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001674{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001675 return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001676}
1677
that8834a0f2016-01-05 23:29:30 +01001678int PageManager::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001679{
that8834a0f2016-01-05 23:29:30 +01001680 return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001681}
1682
1683int PageManager::SetKeyBoardFocus(int inFocus)
1684{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001685 return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001686}
1687
1688int PageManager::NotifyVarChange(std::string varName, std::string value)
1689{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001690 return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001691}
1692
Ethan Yonker74db1572015-10-28 12:44:49 -05001693void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1694{
1695 if (mCurrentSet)
1696 mCurrentSet->AddStringResource(resource_source, resource_name, value);
1697}
1698
Dees_Troy51a0e822012-09-05 15:24:24 -04001699extern "C" void gui_notifyVarChange(const char *name, const char* value)
1700{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001701 if (!gGuiRunning)
1702 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001703
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001704 PageManager::NotifyVarChange(name, value);
Dees_Troy51a0e822012-09-05 15:24:24 -04001705}