blob: 115d6b35dfa7de7757878afcabd95d340940924c [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>
Xuefercac6ace2016-02-01 02:28:55 +080039#include <algorithm>
Dees_Troy51a0e822012-09-05 15:24:24 -040040
41extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000042#include "../twcommon.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}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010047#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048
49#include "rapidxml.hpp"
50#include "objects.hpp"
gordon13370d9133d2013-06-08 14:17:07 +020051#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Ethan Yonker1308d532016-01-14 22:21:49 -060053#define TW_THEME_VERSION 1
Ethan Yonker8e5692f2016-01-21 11:21:06 -060054#define TW_THEME_VER_ERR -2
Ethan Yonker1308d532016-01-14 22:21:49 -060055
Dees_Troy51a0e822012-09-05 15:24:24 -040056extern int gGuiRunning;
57
Ethan Yonker74db1572015-10-28 12:44:49 -050058// From console.cpp
59extern size_t last_message_count;
60extern std::vector<std::string> gConsole;
61extern std::vector<std::string> gConsoleColor;
62
Dees_Troy51a0e822012-09-05 15:24:24 -040063std::map<std::string, PageSet*> PageManager::mPageSets;
64PageSet* PageManager::mCurrentSet;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010065MouseCursor *PageManager::mMouseCursor = NULL;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +010066HardwareKeyboard *PageManager::mHardwareKeyboard = NULL;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -050067bool PageManager::mReloadTheme = false;
68std::string PageManager::mStartPage = "main";
Ethan Yonker74db1572015-10-28 12:44:49 -050069std::vector<language_struct> Language_List;
Dees_Troy51a0e822012-09-05 15:24:24 -040070
Ethan Yonker751a85e2014-12-12 16:59:10 -060071int tw_x_offset = 0;
72int tw_y_offset = 0;
73
Dees_Troy51a0e822012-09-05 15:24:24 -040074// Helper routine to convert a string to a color declaration
75int ConvertStrToColor(std::string str, COLOR* color)
76{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 // Set the default, solid black
78 memset(color, 0, sizeof(COLOR));
79 color->alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 // Translate variables
82 DataManager::GetValue(str, str);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 // Look for some defaults
thatf6ed8fc2015-02-14 20:23:16 +010085 if (str == "black") return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086 else if (str == "white") { color->red = color->green = color->blue = 255; return 0; }
87 else if (str == "red") { color->red = 255; return 0; }
88 else if (str == "green") { color->green = 255; return 0; }
89 else if (str == "blue") { color->blue = 255; return 0; }
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 // At this point, we require an RGB(A) color
92 if (str[0] != '#')
93 return -1;
94
95 str.erase(0, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -040096
Dees_Troy30b962e2012-10-19 20:48:59 -040097 int result;
98 if (str.size() >= 8) {
99 // We have alpha channel
100 string alpha = str.substr(6, 2);
101 result = strtol(alpha.c_str(), NULL, 16);
102 color->alpha = result & 0x000000FF;
103 str.resize(6);
104 result = strtol(str.c_str(), NULL, 16);
105 color->red = (result >> 16) & 0x000000FF;
106 color->green = (result >> 8) & 0x000000FF;
107 color->blue = result & 0x000000FF;
108 } else {
109 result = strtol(str.c_str(), NULL, 16);
110 color->red = (result >> 16) & 0x000000FF;
111 color->green = (result >> 8) & 0x000000FF;
112 color->blue = result & 0x000000FF;
113 }
114 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115}
116
117// Helper APIs
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600118xml_node<>* FindNode(xml_node<>* parent, const char* nodename, int depth /* = 0 */)
119{
that8d46c092015-02-26 01:30:04 +0100120 if (!parent)
121 return NULL;
122
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600123 xml_node<>* child = parent->first_node(nodename);
124 if (child)
125 return child;
126
127 if (depth == 10) {
128 LOGERR("Too many style loops detected.\n");
129 return NULL;
130 }
131
132 xml_node<>* style = parent->first_node("style");
133 if (style) {
134 while (style) {
135 if (!style->first_attribute("name")) {
136 LOGERR("No name given for style.\n");
137 continue;
138 } else {
139 std::string name = style->first_attribute("name")->value();
140 xml_node<>* node = PageManager::FindStyle(name);
141
142 if (node) {
143 // We found the style that was named
144 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
145 if (stylenode)
146 return stylenode;
147 }
148 }
149 style = style->next_sibling("style");
150 }
151 } else {
that54e9c832015-11-04 21:46:01 +0100152 // Search for stylename in the parent node <object type="foo" style="foo2">
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600153 xml_attribute<>* attr = parent->first_attribute("style");
154 // If no style is found anywhere else and the node wasn't found in the object itself
155 // as a special case we will search for a style that uses the same style name as the
156 // object type, so <object type="button"> would search for a style named "button"
157 if (!attr)
158 attr = parent->first_attribute("type");
that54e9c832015-11-04 21:46:01 +0100159 // if there's no attribute type, the object type must be the element name
160 std::string stylename = attr ? attr->value() : parent->name();
161 xml_node<>* node = PageManager::FindStyle(stylename);
162 if (node) {
163 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
164 if (stylenode)
165 return stylenode;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600166 }
167 }
168 return NULL;
169}
170
thatf6ed8fc2015-02-14 20:23:16 +0100171std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue)
172{
173 if (!element)
174 return defaultvalue;
175
176 xml_attribute<>* attr = element->first_attribute(attrname);
177 return attr ? attr->value() : defaultvalue;
178}
179
180int LoadAttrInt(xml_node<>* element, const char* attrname, int defaultvalue)
181{
182 string value = LoadAttrString(element, attrname);
183 // resolve variables
184 DataManager::GetValue(value, value);
185 return value.empty() ? defaultvalue : atoi(value.c_str());
186}
187
188int LoadAttrIntScaleX(xml_node<>* element, const char* attrname, int defaultvalue)
189{
190 return scale_theme_x(LoadAttrInt(element, attrname, defaultvalue));
191}
192
193int LoadAttrIntScaleY(xml_node<>* element, const char* attrname, int defaultvalue)
194{
195 return scale_theme_y(LoadAttrInt(element, attrname, defaultvalue));
196}
197
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600198COLOR LoadAttrColor(xml_node<>* element, const char* attrname, bool* found_color, COLOR defaultvalue)
thatf6ed8fc2015-02-14 20:23:16 +0100199{
200 string value = LoadAttrString(element, attrname);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600201 *found_color = !value.empty();
thatf6ed8fc2015-02-14 20:23:16 +0100202 // resolve variables
203 DataManager::GetValue(value, value);
204 COLOR ret = defaultvalue;
205 if (ConvertStrToColor(value, &ret) == 0)
206 return ret;
207 else
208 return defaultvalue;
209}
210
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600211COLOR LoadAttrColor(xml_node<>* element, const char* attrname, COLOR defaultvalue)
212{
213 bool found_color = false;
214 return LoadAttrColor(element, attrname, &found_color, defaultvalue);
215}
216
thatf6ed8fc2015-02-14 20:23:16 +0100217FontResource* LoadAttrFont(xml_node<>* element, const char* attrname)
218{
219 std::string name = LoadAttrString(element, attrname, "");
220 if (name.empty())
221 return NULL;
222 else
that74ac6062015-03-04 22:39:34 +0100223 return PageManager::GetResources()->FindFont(name);
thatf6ed8fc2015-02-14 20:23:16 +0100224}
225
226ImageResource* LoadAttrImage(xml_node<>* element, const char* attrname)
227{
228 std::string name = LoadAttrString(element, attrname, "");
229 if (name.empty())
230 return NULL;
231 else
that74ac6062015-03-04 22:39:34 +0100232 return PageManager::GetResources()->FindImage(name);
thatf6ed8fc2015-02-14 20:23:16 +0100233}
234
235AnimationResource* LoadAttrAnimation(xml_node<>* element, const char* attrname)
236{
237 std::string name = LoadAttrString(element, attrname, "");
238 if (name.empty())
239 return NULL;
240 else
that74ac6062015-03-04 22:39:34 +0100241 return PageManager::GetResources()->FindAnimation(name);
thatf6ed8fc2015-02-14 20:23:16 +0100242}
243
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500244bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, Placement* placement /* = NULL */)
Dees_Troy51a0e822012-09-05 15:24:24 -0400245{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (!node)
247 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 if (node->first_attribute("x"))
thatf6ed8fc2015-02-14 20:23:16 +0100250 *x = LoadAttrIntScaleX(node, "x") + tw_x_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 if (node->first_attribute("y"))
thatf6ed8fc2015-02-14 20:23:16 +0100253 *y = LoadAttrIntScaleY(node, "y") + tw_y_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (w && node->first_attribute("w"))
thatf6ed8fc2015-02-14 20:23:16 +0100256 *w = LoadAttrIntScaleX(node, "w");
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (h && node->first_attribute("h"))
thatf6ed8fc2015-02-14 20:23:16 +0100259 *h = LoadAttrIntScaleY(node, "h");
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 if (placement && node->first_attribute("placement"))
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500262 *placement = (Placement) LoadAttrInt(node, "placement");
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400265}
266
267int ActionObject::SetActionPos(int x, int y, int w, int h)
268{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 if (x < 0 || y < 0)
270 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400271
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500272 mActionX = x;
273 mActionY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 if (w || h)
275 {
276 mActionW = w;
277 mActionH = h;
278 }
279 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400280}
281
thatb63e2f92015-06-27 21:35:11 +0200282Page::Page(xml_node<>* page, std::vector<xml_node<>*> *templates)
Dees_Troy51a0e822012-09-05 15:24:24 -0400283{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 mTouchStart = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400285
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 // We can memset the whole structure, because the alpha channel is ignored
287 memset(&mBackground, 0, sizeof(COLOR));
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 // With NULL, we make a console-only display
290 if (!page)
291 {
292 mName = "console";
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 GUIConsole* element = new GUIConsole(NULL);
295 mRenders.push_back(element);
296 mActions.push_back(element);
297 return;
298 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400299
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200300 if (page->first_attribute("name"))
301 mName = page->first_attribute("name")->value();
302 else
303 {
304 LOGERR("No page name attribute found!\n");
305 return;
306 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 LOGINFO("Loading page %s\n", mName.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400309
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200310 // This is a recursive routine for template handling
thatb63e2f92015-06-27 21:35:11 +0200311 ProcessNode(page, templates, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400312}
313
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100314Page::~Page()
315{
316 for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr)
317 delete *itr;
318}
319
thatb63e2f92015-06-27 21:35:11 +0200320bool Page::ProcessNode(xml_node<>* page, std::vector<xml_node<>*> *templates, int depth)
Dees_Troy51a0e822012-09-05 15:24:24 -0400321{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200322 if (depth == 10)
323 {
324 LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n");
325 return false;
326 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400327
thatb63e2f92015-06-27 21:35:11 +0200328 for (xml_node<>* child = page->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200329 {
thatb63e2f92015-06-27 21:35:11 +0200330 std::string type = child->name();
331
332 if (type == "background") {
333 mBackground = LoadAttrColor(child, "color", COLOR(0,0,0,0));
334 continue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
thatb63e2f92015-06-27 21:35:11 +0200337 if (type == "object") {
338 // legacy format : <object type="...">
339 xml_attribute<>* attr = child->first_attribute("type");
340 type = attr ? attr->value() : "*unspecified*";
341 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400342
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200343 if (type == "text")
344 {
345 GUIText* element = new GUIText(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100346 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 mRenders.push_back(element);
348 mActions.push_back(element);
349 }
350 else if (type == "image")
351 {
352 GUIImage* element = new GUIImage(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100353 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200354 mRenders.push_back(element);
355 }
356 else if (type == "fill")
357 {
358 GUIFill* element = new GUIFill(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100359 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 mRenders.push_back(element);
361 }
362 else if (type == "action")
363 {
364 GUIAction* element = new GUIAction(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100365 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 mActions.push_back(element);
367 }
368 else if (type == "console")
369 {
370 GUIConsole* element = new GUIConsole(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100371 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200372 mRenders.push_back(element);
373 mActions.push_back(element);
374 }
that1964d192016-01-07 00:41:03 +0100375 else if (type == "terminal")
376 {
377 GUITerminal* element = new GUITerminal(child);
378 mObjects.push_back(element);
379 mRenders.push_back(element);
380 mActions.push_back(element);
381 mInputs.push_back(element);
382 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200383 else if (type == "button")
384 {
385 GUIButton* element = new GUIButton(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100386 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 mRenders.push_back(element);
388 mActions.push_back(element);
389 }
390 else if (type == "checkbox")
391 {
392 GUICheckbox* element = new GUICheckbox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100393 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200394 mRenders.push_back(element);
395 mActions.push_back(element);
396 }
397 else if (type == "fileselector")
398 {
399 GUIFileSelector* element = new GUIFileSelector(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100400 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 mRenders.push_back(element);
402 mActions.push_back(element);
403 }
404 else if (type == "animation")
405 {
406 GUIAnimation* element = new GUIAnimation(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100407 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200408 mRenders.push_back(element);
409 }
410 else if (type == "progressbar")
411 {
412 GUIProgressBar* element = new GUIProgressBar(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100413 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200414 mRenders.push_back(element);
415 mActions.push_back(element);
416 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 else if (type == "slider")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 {
419 GUISlider* element = new GUISlider(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100420 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200421 mRenders.push_back(element);
422 mActions.push_back(element);
423 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200424 else if (type == "slidervalue")
425 {
426 GUISliderValue *element = new GUISliderValue(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100427 mObjects.push_back(element);
Vojtech Bocek85932342013-04-01 22:11:33 +0200428 mRenders.push_back(element);
429 mActions.push_back(element);
430 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400431 else if (type == "listbox")
432 {
433 GUIListBox* element = new GUIListBox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100434 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400435 mRenders.push_back(element);
436 mActions.push_back(element);
437 }
438 else if (type == "keyboard")
439 {
440 GUIKeyboard* element = new GUIKeyboard(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100441 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 mRenders.push_back(element);
443 mActions.push_back(element);
444 }
445 else if (type == "input")
446 {
447 GUIInput* element = new GUIInput(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100448 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 mRenders.push_back(element);
450 mActions.push_back(element);
451 mInputs.push_back(element);
452 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500453 else if (type == "partitionlist")
454 {
455 GUIPartitionList* element = new GUIPartitionList(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100456 mObjects.push_back(element);
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 mRenders.push_back(element);
458 mActions.push_back(element);
459 }
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100460 else if (type == "patternpassword")
461 {
462 GUIPatternPassword* element = new GUIPatternPassword(child);
463 mObjects.push_back(element);
464 mRenders.push_back(element);
465 mActions.push_back(element);
466 }
Ethan Yonker44925ad2015-07-22 12:33:59 -0500467 else if (type == "textbox")
468 {
469 GUITextBox* element = new GUITextBox(child);
470 mObjects.push_back(element);
471 mRenders.push_back(element);
472 mActions.push_back(element);
473 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200474 else if (type == "template")
475 {
476 if (!templates || !child->first_attribute("name"))
477 {
478 LOGERR("Invalid template request.\n");
479 }
480 else
481 {
482 std::string name = child->first_attribute("name")->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500483 xml_node<>* node;
484 bool node_found = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200486 // We need to find the correct template
Ethan Yonker780cd392014-07-21 15:24:39 -0500487 for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) {
488 node = (*itr)->first_node("template");
Dees_Troy51a0e822012-09-05 15:24:24 -0400489
Ethan Yonker780cd392014-07-21 15:24:39 -0500490 while (node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200491 {
Ethan Yonker780cd392014-07-21 15:24:39 -0500492 if (!node->first_attribute("name"))
493 continue;
494
495 if (name == node->first_attribute("name")->value())
496 {
497 if (!ProcessNode(node, templates, depth + 1))
498 return false;
499 else {
500 node_found = true;
501 break;
502 }
503 }
504 if (node_found)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200505 break;
Ethan Yonker780cd392014-07-21 15:24:39 -0500506 node = node->next_sibling("template");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200507 }
thatb63e2f92015-06-27 21:35:11 +0200508 // [check] why is there no if (node_found) here too?
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200509 }
510 }
511 }
512 else
513 {
thatb63e2f92015-06-27 21:35:11 +0200514 LOGERR("Unknown object type: %s.\n", type.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200515 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200516 }
517 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400518}
519
520int Page::Render(void)
521{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200522 // Render background
523 gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha);
524 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Dees_Troy51a0e822012-09-05 15:24:24 -0400525
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526 // Render remaining objects
527 std::vector<RenderObject*>::iterator iter;
528 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
529 {
530 if ((*iter)->Render())
531 LOGERR("A render request has failed.\n");
532 }
533 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400534}
535
536int Page::Update(void)
537{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200538 int retCode = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400539
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 std::vector<RenderObject*>::iterator iter;
541 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
542 {
543 int ret = (*iter)->Update();
544 if (ret < 0)
545 LOGERR("An update request has failed.\n");
546 else if (ret > retCode)
547 retCode = ret;
548 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400549
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 return retCode;
Dees_Troy51a0e822012-09-05 15:24:24 -0400551}
552
553int Page::NotifyTouch(TOUCH_STATE state, int x, int y)
554{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200555 // By default, return 1 to ignore further touches if nobody is listening
556 int ret = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400557
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200558 // Don't try to handle a lack of handlers
559 if (mActions.size() == 0)
560 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400561
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200562 // We record mTouchStart so we can pass all the touch stream to the same handler
563 if (state == TOUCH_START)
564 {
565 std::vector<ActionObject*>::reverse_iterator iter;
566 // We work backwards, from top-most element to bottom-most element
567 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
568 {
569 if ((*iter)->IsInRegion(x, y))
570 {
571 mTouchStart = (*iter);
572 ret = mTouchStart->NotifyTouch(state, x, y);
573 if (ret >= 0)
574 break;
575 mTouchStart = NULL;
576 }
577 }
578 }
579 else if (state == TOUCH_RELEASE && mTouchStart != NULL)
580 {
581 ret = mTouchStart->NotifyTouch(state, x, y);
582 mTouchStart = NULL;
583 }
584 else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL)
585 {
586 ret = mTouchStart->NotifyTouch(state, x, y);
587 }
588 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400589}
590
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100591int Page::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400592{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200593 std::vector<ActionObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100595 int ret = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200596 // We work backwards, from top-most element to bottom-most element
597 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
598 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100599 ret = (*iter)->NotifyKey(key, down);
that8834a0f2016-01-05 23:29:30 +0100600 if (ret == 0)
601 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100602 if (ret < 0) {
603 LOGERR("An action handler has returned an error\n");
604 ret = 1;
605 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200606 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100607 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400608}
609
that8834a0f2016-01-05 23:29:30 +0100610int Page::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -0400611{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200612 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400613
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200614 // We work backwards, from top-most element to bottom-most element
615 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
616 {
that8834a0f2016-01-05 23:29:30 +0100617 int ret = (*iter)->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200618 if (ret == 0)
619 return 0;
620 else if (ret < 0)
that6a894592016-03-13 17:51:28 +0100621 LOGERR("A char input handler has returned an error\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200622 }
623 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400624}
625
626int Page::SetKeyBoardFocus(int inFocus)
627{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200628 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400629
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200630 // We work backwards, from top-most element to bottom-most element
631 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
632 {
633 int ret = (*iter)->SetInputFocus(inFocus);
634 if (ret == 0)
635 return 0;
636 else if (ret < 0)
that6a894592016-03-13 17:51:28 +0100637 LOGERR("An input focus handler has returned an error\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200638 }
639 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400640}
641
642void Page::SetPageFocus(int inFocus)
643{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200644 // Render remaining objects
645 std::vector<RenderObject*>::iterator iter;
646 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
647 (*iter)->SetPageFocus(inFocus);
648
649 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400650}
651
652int Page::NotifyVarChange(std::string varName, std::string value)
653{
Vojtech Bocek07220562014-02-08 02:05:33 +0100654 std::vector<GUIObject*>::iterator iter;
655 for (iter = mObjects.begin(); iter != mObjects.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200656 {
657 if ((*iter)->NotifyVarChange(varName, value))
658 LOGERR("An action handler errored on NotifyVarChange.\n");
659 }
660 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400661}
662
that6a894592016-03-13 17:51:28 +0100663
664// transient data for loading themes
665struct LoadingContext
666{
667 ZipArchive* zip; // zip to load theme from, or NULL for the stock theme
668 std::set<std::string> filenames; // to detect cyclic includes
669 std::string basepath; // if zip is NULL, base path to load includes from with trailing slash, otherwise empty
670 std::vector<xml_document<>*> xmldocs; // all loaded xml docs
671 std::vector<char*> xmlbuffers; // text buffers with xml content
672 std::vector<xml_node<>*> styles; // refer to <styles> nodes inside xmldocs
673 std::vector<xml_node<>*> templates; // refer to <templates> nodes inside xmldocs
674
675 LoadingContext()
676 {
677 zip = NULL;
678 }
679
680 ~LoadingContext()
681 {
682 // free all xml buffers
683 for (std::vector<char*>::iterator it = xmlbuffers.begin(); it != xmlbuffers.end(); ++it)
684 free(*it);
685 }
686
687};
688
689// for FindStyle
690LoadingContext* PageManager::currentLoadingContext = NULL;
691
692
693PageSet::PageSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400694{
that74ac6062015-03-04 22:39:34 +0100695 mResources = new ResourceManager;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200696 mCurrentPage = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400697
that6a894592016-03-13 17:51:28 +0100698 set_scale_values(1, 1); // Reset any previous scaling values
Dees_Troy51a0e822012-09-05 15:24:24 -0400699}
700
701PageSet::~PageSet()
702{
Ethan Yonker1c273312015-03-16 12:18:56 -0500703 mOverlays.clear();
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100704 for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
705 delete *itr;
706
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200707 delete mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400708}
709
that6a894592016-03-13 17:51:28 +0100710int PageSet::Load(LoadingContext& ctx, const std::string& filename)
711{
712 bool isMain = ctx.xmlbuffers.empty(); // if we have no files yet, remember that this is the main XML file
713
714 if (!ctx.filenames.insert(filename).second)
715 // ignore already loaded files to prevent crash with cyclic includes
716 return 0;
717
718 // load XML into buffer
719 char* xmlbuffer = PageManager::LoadFileToBuffer(filename, ctx.zip);
720 if (!xmlbuffer)
721 return -1; // error already displayed by LoadFileToBuffer
722 ctx.xmlbuffers.push_back(xmlbuffer);
723
724 // parse XML
725 xml_document<>* doc = new xml_document<>();
726 doc->parse<0>(xmlbuffer);
727 ctx.xmldocs.push_back(doc);
728
729 xml_node<>* root = doc->first_node("recovery");
730 if (!root)
731 root = doc->first_node("install");
732 if (!root) {
733 LOGERR("Unknown root element in %s\n", filename.c_str());
734 return -1;
735 }
736
737 if (isMain) {
738 int rc = LoadDetails(ctx, root);
739 if (rc != 0)
740 return rc;
741 }
742
743 LOGINFO("Loading resources...\n");
744 xml_node<>* child = root->first_node("resources");
745 if (child)
746 mResources->LoadResources(child, ctx.zip, "theme");
747
748 LOGINFO("Loading variables...\n");
749 child = root->first_node("variables");
750 if (child)
751 LoadVariables(child);
752
753 LOGINFO("Loading mouse cursor...\n");
754 child = root->first_node("mousecursor");
755 if (child)
756 PageManager::LoadCursorData(child);
757
758 LOGINFO("Loading pages...\n");
759 child = root->first_node("templates");
760 if (child)
761 ctx.templates.push_back(child);
762
763 child = root->first_node("styles");
764 if (child)
765 ctx.styles.push_back(child);
766
767 // Load pages
768 child = root->first_node("pages");
769 if (child) {
770 if (LoadPages(ctx, child)) {
771 LOGERR("PageSet::Load returning -1\n");
772 return -1;
773 }
774 }
775
776 // process includes recursively
777 child = root->first_node("include");
778 if (child) {
779 xml_node<>* include = child->first_node("xmlfile");
780 while (include != NULL) {
781 xml_attribute<>* attr = include->first_attribute("name");
782 if (!attr) {
783 LOGERR("Skipping include/xmlfile with no name\n");
784 continue;
785 }
786
787 string filename = ctx.basepath + attr->value();
788 LOGINFO("Including file: %s...\n", filename.c_str());
789 int rc = Load(ctx, filename);
790 if (rc != 0)
791 return rc;
792
793 include = include->next_sibling("xmlfile");
794 }
795 }
796
797 return 0;
798}
799
800void PageSet::MakeEmergencyConsoleIfNeeded()
801{
802 if (mPages.empty()) {
803 mCurrentPage = new Page(NULL, NULL); // fallback console page
804 // TODO: since removal of non-TTF fonts, the emergency console doesn't work without a font, which might be missing too
805 mPages.push_back(mCurrentPage);
806 }
807}
808
Ethan Yonker74db1572015-10-28 12:44:49 -0500809int PageSet::LoadLanguage(char* languageFile, ZipArchive* package)
810{
811 xml_document<> lang;
812 xml_node<>* parent;
813 xml_node<>* child;
814 std::string resource_source;
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600815 int ret = 0;
Ethan Yonker74db1572015-10-28 12:44:49 -0500816
817 if (languageFile) {
818 printf("parsing languageFile\n");
819 lang.parse<0>(languageFile);
820 printf("parsing languageFile done\n");
821 } else {
822 return -1;
823 }
824
825 parent = lang.first_node("language");
826 if (!parent) {
827 LOGERR("Unable to locate language node in language file.\n");
828 lang.clear();
829 return -1;
830 }
831
832 child = parent->first_node("display");
833 if (child) {
834 DataManager::SetValue("tw_language_display", child->value());
835 resource_source = child->value();
836 } else {
837 LOGERR("language file does not have a display value set\n");
838 DataManager::SetValue("tw_language_display", "Not Set");
839 resource_source = languageFile;
840 }
841
842 child = parent->first_node("resources");
843 if (child)
844 mResources->LoadResources(child, package, resource_source);
845 else
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600846 ret = -1;
847 DataManager::SetValue("tw_backup_name", gui_lookup("auto_generate", "(Auto Generate)"));
Ethan Yonker74db1572015-10-28 12:44:49 -0500848 lang.clear();
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600849 return ret;
Ethan Yonker74db1572015-10-28 12:44:49 -0500850}
851
that6a894592016-03-13 17:51:28 +0100852int PageSet::LoadDetails(LoadingContext& ctx, xml_node<>* root)
Dees_Troy51a0e822012-09-05 15:24:24 -0400853{
that6a894592016-03-13 17:51:28 +0100854 xml_node<>* child = root->first_node("details");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600855 if (child) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600856 int theme_ver = 0;
857 xml_node<>* themeversion = child->first_node("themeversion");
858 if (themeversion && themeversion->value()) {
859 theme_ver = atoi(themeversion->value());
860 } else {
861 LOGINFO("No themeversion in theme.\n");
862 }
863 if (theme_ver != TW_THEME_VERSION) {
864 LOGINFO("theme version from xml: %i, expected %i\n", theme_ver, TW_THEME_VERSION);
that6a894592016-03-13 17:51:28 +0100865 if (ctx.zip) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600866 gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme.");
Ethan Yonker8e5692f2016-01-21 11:21:06 -0600867 return TW_THEME_VER_ERR;
Ethan Yonker1308d532016-01-14 22:21:49 -0600868 } else {
869 gui_print_color("warning", "Stock theme version does not match TWRP version.\n");
870 }
871 }
Ethan Yonker63e414f2015-02-06 15:44:39 -0600872 xml_node<>* resolution = child->first_node("resolution");
873 if (resolution) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600874 LOGINFO("Checking resolution...\n");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600875 xml_attribute<>* width_attr = resolution->first_attribute("width");
876 xml_attribute<>* height_attr = resolution->first_attribute("height");
877 xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
878 if (width_attr && height_attr && !noscale_attr) {
879 int width = atoi(width_attr->value());
880 int height = atoi(height_attr->value());
881 int offx = 0, offy = 0;
882#ifdef TW_ROUND_SCREEN
883 xml_node<>* roundscreen = child->first_node("roundscreen");
884 if (roundscreen) {
885 LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n");
886 xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x");
887 xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y");
888 if (offx_attr) {
889 offx = atoi(offx_attr->value());
890 }
891 if (offy_attr) {
892 offy = atoi(offy_attr->value());
893 }
894 }
895#endif
896 if (width != 0 && height != 0) {
897 float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width;
898 float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height;
899#ifdef TW_ROUND_SCREEN
900 float scale_off_w = (float)gr_fb_width() / (float)width;
901 float scale_off_h = (float)gr_fb_height() / (float)height;
902 tw_x_offset = offx * scale_off_w;
903 tw_y_offset = offy * scale_off_h;
904#endif
905 if (scale_w != 1 || scale_h != 1) {
906 LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset);
907 set_scale_values(scale_w, scale_h);
908 }
909 }
910 } else {
911 LOGINFO("XML does not contain width and height, no scaling will be applied\n");
912 }
913 } else {
914 LOGINFO("XML contains no resolution tag, no scaling will be applied.\n");
915 }
916 } else {
917 LOGINFO("XML contains no details tag, no scaling will be applied.\n");
918 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500919
Ethan Yonker780cd392014-07-21 15:24:39 -0500920 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400921}
922
923int PageSet::SetPage(std::string page)
924{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200925 Page* tmp = FindPage(page);
926 if (tmp)
927 {
928 if (mCurrentPage) mCurrentPage->SetPageFocus(0);
929 mCurrentPage = tmp;
930 mCurrentPage->SetPageFocus(1);
931 mCurrentPage->NotifyVarChange("", "");
932 return 0;
933 }
934 else
935 {
936 LOGERR("Unable to locate page (%s)\n", page.c_str());
937 }
938 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400939}
940
941int PageSet::SetOverlay(Page* page)
942{
Ethan Yonker1c273312015-03-16 12:18:56 -0500943 if (page) {
944 if (mOverlays.size() >= 10) {
945 LOGERR("Too many overlays requested, max is 10.\n");
946 return -1;
947 }
Matt Mowerd411f8d2015-04-09 16:04:12 -0500948
949 std::vector<Page*>::iterator iter;
950 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
951 if ((*iter)->GetName() == page->GetName()) {
952 mOverlays.erase(iter);
953 // SetOverlay() is (and should stay) the only function which
954 // adds to mOverlays. Then, each page can appear at most once.
955 break;
956 }
957 }
958
Ethan Yonker1c273312015-03-16 12:18:56 -0500959 page->SetPageFocus(1);
960 page->NotifyVarChange("", "");
961
962 if (!mOverlays.empty())
963 mOverlays.back()->SetPageFocus(0);
964
965 mOverlays.push_back(page);
966 } else {
967 if (!mOverlays.empty()) {
968 mOverlays.back()->SetPageFocus(0);
969 mOverlays.pop_back();
970 if (!mOverlays.empty())
971 mOverlays.back()->SetPageFocus(1);
972 else if (mCurrentPage)
973 mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
974 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200975 }
976 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400977}
978
that74ac6062015-03-04 22:39:34 +0100979const ResourceManager* PageSet::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -0400980{
that74ac6062015-03-04 22:39:34 +0100981 return mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400982}
983
984Page* PageSet::FindPage(std::string name)
985{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200986 std::vector<Page*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400987
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200988 for (iter = mPages.begin(); iter != mPages.end(); iter++)
989 {
990 if (name == (*iter)->GetName())
991 return (*iter);
992 }
993 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400994}
995
996int PageSet::LoadVariables(xml_node<>* vars)
997{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200998 xml_node<>* child;
Vojtech Bocek81c29dc2013-12-07 23:02:09 +0100999 xml_attribute<> *name, *value, *persist;
1000 int p;
Dees_Troy51a0e822012-09-05 15:24:24 -04001001
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001002 child = vars->first_node("variable");
1003 while (child)
1004 {
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001005 name = child->first_attribute("name");
1006 value = child->first_attribute("value");
1007 persist = child->first_attribute("persist");
1008 if(name && value)
1009 {
Ethan Yonker751a85e2014-12-12 16:59:10 -06001010 if (strcmp(name->value(), "tw_x_offset") == 0) {
1011 tw_x_offset = atoi(value->value());
1012 child = child->next_sibling("variable");
1013 continue;
1014 }
1015 if (strcmp(name->value(), "tw_y_offset") == 0) {
1016 tw_y_offset = atoi(value->value());
1017 child = child->next_sibling("variable");
1018 continue;
1019 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001020 p = persist ? atoi(persist->value()) : 0;
Ethan Yonker96acb3d2014-08-05 09:20:30 -05001021 string temp = value->value();
1022 string valstr = gui_parse_text(temp);
1023
1024 if (valstr.find("+") != string::npos) {
1025 string val1str = valstr;
1026 val1str = val1str.substr(0, val1str.find('+'));
1027 string val2str = valstr;
1028 val2str = val2str.substr(val2str.find('+') + 1, string::npos);
1029 int val1 = atoi(val1str.c_str());
1030 int val2 = atoi(val2str.c_str());
1031 int val = val1 + val2;
1032
1033 DataManager::SetValue(name->value(), val, p);
1034 } else if (valstr.find("-") != string::npos) {
1035 string val1str = valstr;
1036 val1str = val1str.substr(0, val1str.find('-'));
1037 string val2str = valstr;
1038 val2str = val2str.substr(val2str.find('-') + 1, string::npos);
1039 int val1 = atoi(val1str.c_str());
1040 int val2 = atoi(val2str.c_str());
1041 int val = val1 - val2;
1042
1043 DataManager::SetValue(name->value(), val, p);
1044 } else {
1045 DataManager::SetValue(name->value(), valstr, p);
1046 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001047 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001048
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001049 child = child->next_sibling("variable");
1050 }
1051 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001052}
1053
that6a894592016-03-13 17:51:28 +01001054int PageSet::LoadPages(LoadingContext& ctx, xml_node<>* pages)
Dees_Troy51a0e822012-09-05 15:24:24 -04001055{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001056 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -04001057
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001058 if (!pages)
1059 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001060
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001061 child = pages->first_node("page");
1062 while (child != NULL)
1063 {
that6a894592016-03-13 17:51:28 +01001064 Page* page = new Page(child, &ctx.templates);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 if (page->GetName().empty())
1066 {
1067 LOGERR("Unable to process load page\n");
1068 delete page;
1069 }
1070 else
1071 {
1072 mPages.push_back(page);
1073 }
1074 child = child->next_sibling("page");
1075 }
1076 if (mPages.size() > 0)
1077 return 0;
1078 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001079}
1080
1081int PageSet::IsCurrentPage(Page* page)
1082{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001083 return ((mCurrentPage && mCurrentPage == page) ? 1 : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001084}
1085
that10ae24f2015-12-26 20:53:51 +01001086std::string PageSet::GetCurrentPage() const
1087{
1088 return mCurrentPage ? mCurrentPage->GetName() : "";
1089}
1090
Dees_Troy51a0e822012-09-05 15:24:24 -04001091int PageSet::Render(void)
1092{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001093 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001094
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001095 ret = (mCurrentPage ? mCurrentPage->Render() : -1);
1096 if (ret < 0)
1097 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001098
1099 std::vector<Page*>::iterator iter;
1100
1101 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1102 ret = ((*iter) ? (*iter)->Render() : -1);
1103 if (ret < 0)
1104 return ret;
1105 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001106 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001107}
1108
1109int PageSet::Update(void)
1110{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001111 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001112
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001113 ret = (mCurrentPage ? mCurrentPage->Update() : -1);
1114 if (ret < 0 || ret > 1)
1115 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001116
1117 std::vector<Page*>::iterator iter;
1118
1119 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1120 ret = ((*iter) ? (*iter)->Update() : -1);
1121 if (ret < 0)
1122 return ret;
1123 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001124 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001125}
1126
1127int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
1128{
Ethan Yonker1c273312015-03-16 12:18:56 -05001129 if (!mOverlays.empty())
1130 return mOverlays.back()->NotifyTouch(state, x, y);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001131
1132 return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001133}
1134
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001135int PageSet::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001136{
Ethan Yonker1c273312015-03-16 12:18:56 -05001137 if (!mOverlays.empty())
1138 return mOverlays.back()->NotifyKey(key, down);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001139
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001140 return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001141}
1142
that8834a0f2016-01-05 23:29:30 +01001143int PageSet::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001144{
Ethan Yonker1c273312015-03-16 12:18:56 -05001145 if (!mOverlays.empty())
that8834a0f2016-01-05 23:29:30 +01001146 return mOverlays.back()->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001147
that8834a0f2016-01-05 23:29:30 +01001148 return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001149}
1150
1151int PageSet::SetKeyBoardFocus(int inFocus)
1152{
Ethan Yonker1c273312015-03-16 12:18:56 -05001153 if (!mOverlays.empty())
1154 return mOverlays.back()->SetKeyBoardFocus(inFocus);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001155
1156 return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001157}
1158
1159int PageSet::NotifyVarChange(std::string varName, std::string value)
1160{
Ethan Yonker1c273312015-03-16 12:18:56 -05001161 std::vector<Page*>::iterator iter;
1162
1163 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
1164 (*iter)->NotifyVarChange(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001165
1166 return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001167}
1168
Ethan Yonker74db1572015-10-28 12:44:49 -05001169void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1170{
1171 mResources->AddStringResource(resource_source, resource_name, value);
1172}
1173
Ethan Yonker561c58d2015-10-05 08:48:22 -05001174char* PageManager::LoadFileToBuffer(std::string filename, ZipArchive* package) {
1175 size_t len;
1176 char* buffer = NULL;
1177
1178 if (!package) {
1179 // We can try to load the XML directly...
1180 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str());
1181 struct stat st;
1182 if(stat(filename.c_str(),&st) != 0) {
1183 // This isn't always an error, sometimes we request files that don't exist.
1184 return NULL;
1185 }
1186
1187 len = (size_t)st.st_size;
1188
1189 buffer = (char*) malloc(len + 1);
1190 if (!buffer) {
1191 LOGERR("PageManager::LoadFileToBuffer failed to malloc\n");
1192 return NULL;
1193 }
1194
1195 int fd = open(filename.c_str(), O_RDONLY);
1196 if (fd == -1) {
1197 LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno));
1198 free(buffer);
1199 return NULL;
1200 }
1201
1202 if (read(fd, buffer, len) < 0) {
1203 LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno));
1204 free(buffer);
1205 close(fd);
1206 return NULL;
1207 }
1208 close(fd);
1209 } else {
1210 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
1211 const ZipEntry* zipentry = mzFindZipEntry(package, filename.c_str());
1212 if (zipentry == NULL) {
1213 LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
1214 return NULL;
1215 }
1216
1217 // Allocate the buffer for the file
1218 len = mzGetZipEntryUncompLen(zipentry);
1219 buffer = (char*) malloc(len + 1);
1220 if (!buffer)
1221 return NULL;
1222
1223 if (!mzExtractZipEntryToBuffer(package, zipentry, (unsigned char*) buffer)) {
1224 LOGERR("Unable to extract '%s'\n", filename.c_str());
1225 free(buffer);
1226 return NULL;
1227 }
1228 }
1229 // NULL-terminate the string
1230 buffer[len] = 0x00;
1231 return buffer;
1232}
1233
Ethan Yonker74db1572015-10-28 12:44:49 -05001234void PageManager::LoadLanguageListDir(string dir) {
1235 if (!TWFunc::Path_Exists(dir)) {
1236 LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str());
1237 return;
1238 }
1239
1240 DIR *d = opendir(dir.c_str());
1241 struct dirent *p;
1242
1243 if (d == NULL) {
1244 LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno));
1245 return;
1246 }
1247
1248 while ((p = readdir(d))) {
1249 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5)
1250 continue;
1251
1252 string file = p->d_name;
1253 if (file.substr(strlen(p->d_name) - 4) != ".xml")
1254 continue;
1255 string path = dir + p->d_name;
1256 string file_no_extn = file.substr(0, strlen(p->d_name) - 4);
1257 struct language_struct language_entry;
1258 language_entry.filename = file_no_extn;
1259 char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL);
1260 if (xmlFile == NULL) {
1261 LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str());
1262 continue;
1263 }
1264 xml_document<> *doc = new xml_document<>();
1265 doc->parse<0>(xmlFile);
1266
1267 xml_node<>* parent = doc->first_node("language");
1268 if (!parent) {
1269 LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str());
1270 } else {
1271 xml_node<>* child = parent->first_node("display");
1272 if (child) {
1273 language_entry.displayvalue = child->value();
1274 } else {
1275 LOGERR("No display value for '%s'\n", language_entry.filename.c_str());
1276 language_entry.displayvalue = language_entry.filename;
1277 }
1278 Language_List.push_back(language_entry);
1279 }
1280 doc->clear();
1281 delete doc;
1282 free(xmlFile);
1283 }
1284 closedir(d);
1285}
1286
1287void PageManager::LoadLanguageList(ZipArchive* package) {
1288 Language_List.clear();
1289 if (TWFunc::Path_Exists(TWRES "customlanguages"))
1290 TWFunc::removeDir(TWRES "customlanguages", true);
1291 if (package) {
1292 TWFunc::Recursive_Mkdir(TWRES "customlanguages");
1293 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
1294 mzExtractRecursive(package, "languages", TWRES "customlanguages/", &timestamp, NULL, NULL, NULL);
1295 LoadLanguageListDir(TWRES "customlanguages/");
1296 } else {
1297 LoadLanguageListDir(TWRES "languages/");
1298 }
Xuefercac6ace2016-02-01 02:28:55 +08001299
1300 std::sort(Language_List.begin(), Language_List.end());
Ethan Yonker74db1572015-10-28 12:44:49 -05001301}
1302
1303void PageManager::LoadLanguage(string filename) {
1304 string actual_filename;
1305 if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml"))
1306 actual_filename = TWRES "customlanguages/" + filename + ".xml";
1307 else
1308 actual_filename = TWRES "languages/" + filename + ".xml";
1309 char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL);
1310 if (xmlFile == NULL)
1311 LOGERR("Unable to load '%s'\n", actual_filename.c_str());
1312 else {
1313 mCurrentSet->LoadLanguage(xmlFile, NULL);
1314 free(xmlFile);
1315 }
1316 PartitionManager.Translate_Partition_Display_Names();
1317}
1318
Dees_Troy51a0e822012-09-05 15:24:24 -04001319int PageManager::LoadPackage(std::string name, std::string package, std::string startpage)
1320{
that6a894592016-03-13 17:51:28 +01001321 std::string mainxmlfilename = package;
1322 ZipArchive zip;
Ethan Yonker74db1572015-10-28 12:44:49 -05001323 char* languageFile = NULL;
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001324 char* baseLanguageFile = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001325 PageSet* pageSet = NULL;
1326 int ret;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001327 MemMapping map;
Dees_Troy51a0e822012-09-05 15:24:24 -04001328
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001329 mReloadTheme = false;
1330 mStartPage = startpage;
1331
that6a894592016-03-13 17:51:28 +01001332 // init the loading context
1333 LoadingContext ctx;
1334
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001335 // Open the XML file
1336 LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str());
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001337 if (package.size() > 4 && package.substr(package.size() - 4) != ".zip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001338 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001339 LOGINFO("Load XML directly\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001340 tw_x_offset = TW_X_OFFSET;
1341 tw_y_offset = TW_Y_OFFSET;
Ethan Yonker4f74d142016-03-31 08:10:37 -05001342 if (name != "splash") {
1343 LoadLanguageList(NULL);
1344 languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
1345 }
that6a894592016-03-13 17:51:28 +01001346 ctx.basepath = TWRES;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001347 }
1348 else
1349 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001350 LOGINFO("Loading zip theme\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001351 tw_x_offset = 0;
1352 tw_y_offset = 0;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001353 if (!TWFunc::Path_Exists(package))
1354 return -1;
1355 if (sysMapFile(package.c_str(), &map) != 0) {
1356 LOGERR("Failed to map '%s'\n", package.c_str());
Ethan Yonker561c58d2015-10-05 08:48:22 -05001357 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001358 }
1359 if (mzOpenZipArchive(map.addr, map.length, &zip)) {
1360 LOGERR("Unable to open zip archive '%s'\n", package.c_str());
1361 sysReleaseMap(&map);
Ethan Yonker561c58d2015-10-05 08:48:22 -05001362 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001363 }
that6a894592016-03-13 17:51:28 +01001364 ctx.zip = &zip;
1365 mainxmlfilename = "ui.xml";
1366 LoadLanguageList(ctx.zip);
1367 languageFile = LoadFileToBuffer("languages/en.xml", ctx.zip);
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001368 baseLanguageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001369 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001370
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001371 // Before loading, mCurrentSet must be the loading package so we can find resources
1372 pageSet = mCurrentSet;
that6a894592016-03-13 17:51:28 +01001373 mCurrentSet = new PageSet();
1374
1375 if (baseLanguageFile) {
1376 mCurrentSet->LoadLanguage(baseLanguageFile, NULL);
1377 free(baseLanguageFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001378 }
that6a894592016-03-13 17:51:28 +01001379
1380 if (languageFile) {
1381 mCurrentSet->LoadLanguage(languageFile, ctx.zip);
1382 free(languageFile);
1383 }
1384
1385 // Load and parse the XML and all includes
1386 currentLoadingContext = &ctx; // required to find styles
1387 ret = mCurrentSet->Load(ctx, mainxmlfilename);
1388 currentLoadingContext = NULL;
1389
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001390 if (ret == 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001391 mCurrentSet->SetPage(startpage);
1392 mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001393 } else {
1394 if (ret != TW_THEME_VER_ERR)
1395 LOGERR("Package %s failed to load.\n", name.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001396 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001397
that6a894592016-03-13 17:51:28 +01001398 // reset to previous pageset
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001399 mCurrentSet = pageSet;
1400
that6a894592016-03-13 17:51:28 +01001401 if (ctx.zip) {
1402 mzCloseZipArchive(ctx.zip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001403 sysReleaseMap(&map);
1404 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001405 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001406
1407error:
Ethan Yonker561c58d2015-10-05 08:48:22 -05001408 // Sometimes we get here without a real error
that6a894592016-03-13 17:51:28 +01001409 if (ctx.zip) {
1410 mzCloseZipArchive(ctx.zip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001411 sysReleaseMap(&map);
1412 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001413 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001414}
1415
1416PageSet* PageManager::FindPackage(std::string name)
1417{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001418 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001419
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001420 iter = mPageSets.find(name);
1421 if (iter != mPageSets.end())
1422 return (*iter).second;
1423
1424 LOGERR("Unable to locate package %s\n", name.c_str());
1425 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001426}
1427
1428PageSet* PageManager::SelectPackage(std::string name)
1429{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001430 LOGINFO("Switching packages (%s)\n", name.c_str());
1431 PageSet* tmp;
Dees_Troy51a0e822012-09-05 15:24:24 -04001432
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001433 tmp = FindPackage(name);
1434 if (tmp)
Vojtech Bocek07220562014-02-08 02:05:33 +01001435 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001436 mCurrentSet = tmp;
that6a894592016-03-13 17:51:28 +01001437 mCurrentSet->MakeEmergencyConsoleIfNeeded();
Vojtech Bocek07220562014-02-08 02:05:33 +01001438 mCurrentSet->NotifyVarChange("", "");
1439 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001440 else
1441 LOGERR("Unable to find package.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001442
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001443 return mCurrentSet;
Dees_Troy51a0e822012-09-05 15:24:24 -04001444}
1445
1446int PageManager::ReloadPackage(std::string name, std::string package)
1447{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001448 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001449
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001450 mReloadTheme = false;
1451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001452 iter = mPageSets.find(name);
1453 if (iter == mPageSets.end())
1454 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001455
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001456 if(mMouseCursor)
1457 mMouseCursor->ResetData(gr_fb_width(), gr_fb_height());
1458
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001459 PageSet* set = (*iter).second;
1460 mPageSets.erase(iter);
Dees_Troy51a0e822012-09-05 15:24:24 -04001461
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001462 if (LoadPackage(name, package, mStartPage) != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001463 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001464 LOGINFO("Failed to load package '%s'.\n", package.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001465 mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
1466 return -1;
1467 }
1468 if (mCurrentSet == set)
1469 SelectPackage(name);
1470 delete set;
Ethan Yonker74db1572015-10-28 12:44:49 -05001471 GUIConsole::Translate_Now();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001472 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001473}
1474
1475void PageManager::ReleasePackage(std::string name)
1476{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001477 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001478
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001479 iter = mPageSets.find(name);
1480 if (iter == mPageSets.end())
1481 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001482
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001483 PageSet* set = (*iter).second;
1484 mPageSets.erase(iter);
1485 delete set;
that235c6482016-01-24 21:59:00 +01001486 if (set == mCurrentSet)
1487 mCurrentSet = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001488 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001489}
1490
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001491int PageManager::RunReload() {
1492 int ret_val = 0;
1493 std::string theme_path;
1494
1495 if (!mReloadTheme)
1496 return 0;
1497
1498 mReloadTheme = false;
1499 theme_path = DataManager::GetSettingsStoragePath();
1500 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1501 LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
1502 ret_val = 1;
1503 }
1504
1505 theme_path += "/TWRP/theme/ui.zip";
1506 if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0)
1507 {
1508 // Loading the custom theme failed - try loading the stock theme
1509 LOGINFO("Attempting to reload stock theme...\n");
1510 if (ReloadPackage("TWRP", TWRES "ui.xml"))
1511 {
1512 LOGERR("Failed to load base packages.\n");
1513 ret_val = 1;
1514 }
1515 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 if (ret_val == 0) {
1517 if (DataManager::GetStrValue("tw_language") != "en.xml") {
1518 LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str());
1519 LoadLanguage(DataManager::GetStrValue("tw_language"));
1520 }
1521 }
1522
1523 // This makes the console re-translate
1524 last_message_count = 0;
1525 gConsole.clear();
1526 gConsoleColor.clear();
1527
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001528 return ret_val;
1529}
1530
1531void PageManager::RequestReload() {
1532 mReloadTheme = true;
1533}
1534
Ethan Yonkerafde0982016-01-23 08:55:35 -06001535void PageManager::SetStartPage(const std::string& page_name) {
1536 mStartPage = page_name;
1537}
1538
Dees_Troy51a0e822012-09-05 15:24:24 -04001539int PageManager::ChangePage(std::string name)
1540{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001541 DataManager::SetValue("tw_operation_state", 0);
1542 int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1);
1543 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001544}
1545
that10ae24f2015-12-26 20:53:51 +01001546std::string PageManager::GetCurrentPage()
1547{
1548 return mCurrentSet ? mCurrentSet->GetCurrentPage() : "";
1549}
1550
Dees_Troy51a0e822012-09-05 15:24:24 -04001551int PageManager::ChangeOverlay(std::string name)
1552{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001553 if (name.empty())
1554 return mCurrentSet->SetOverlay(NULL);
1555 else
1556 {
Ethan Yonker1308d532016-01-14 22:21:49 -06001557 Page* page = mCurrentSet ? mCurrentSet->FindPage(name) : NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001558 return mCurrentSet->SetOverlay(page);
1559 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001560}
1561
that74ac6062015-03-04 22:39:34 +01001562const ResourceManager* PageManager::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001563{
that74ac6062015-03-04 22:39:34 +01001564 return (mCurrentSet ? mCurrentSet->GetResources() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001565}
1566
Dees_Troy51a0e822012-09-05 15:24:24 -04001567int PageManager::IsCurrentPage(Page* page)
1568{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001569 return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001570}
1571
1572int PageManager::Render(void)
1573{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001574 if(blankTimer.isScreenOff())
1575 return 0;
1576
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001577 int res = (mCurrentSet ? mCurrentSet->Render() : -1);
1578 if(mMouseCursor)
1579 mMouseCursor->Render();
1580 return res;
1581}
1582
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001583HardwareKeyboard *PageManager::GetHardwareKeyboard()
1584{
1585 if(!mHardwareKeyboard)
1586 mHardwareKeyboard = new HardwareKeyboard();
1587 return mHardwareKeyboard;
1588}
1589
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001590xml_node<>* PageManager::FindStyle(std::string name)
1591{
that6a894592016-03-13 17:51:28 +01001592 if (!currentLoadingContext)
1593 {
1594 LOGERR("FindStyle works only while loading a theme.\n");
1595 return NULL;
1596 }
1597
1598 for (std::vector<xml_node<>*>::iterator itr = currentLoadingContext->styles.begin(); itr != currentLoadingContext->styles.end(); itr++) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001599 xml_node<>* node = (*itr)->first_node("style");
1600
1601 while (node) {
1602 if (!node->first_attribute("name"))
1603 continue;
1604
1605 if (name == node->first_attribute("name")->value())
1606 return node;
1607 node = node->next_sibling("style");
1608 }
1609 }
1610 return NULL;
1611}
1612
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001613MouseCursor *PageManager::GetMouseCursor()
1614{
1615 if(!mMouseCursor)
1616 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1617 return mMouseCursor;
1618}
1619
1620void PageManager::LoadCursorData(xml_node<>* node)
1621{
1622 if(!mMouseCursor)
1623 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1624
1625 mMouseCursor->LoadData(node);
Dees_Troy51a0e822012-09-05 15:24:24 -04001626}
1627
1628int PageManager::Update(void)
1629{
thatfb759d42015-01-11 12:16:53 +01001630 if(blankTimer.isScreenOff())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001631 return 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001632
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001633 if (RunReload())
1634 return -2;
1635
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001636 int res = (mCurrentSet ? mCurrentSet->Update() : -1);
1637
1638 if(mMouseCursor)
1639 {
1640 int c_res = mMouseCursor->Update();
1641 if(c_res > res)
1642 res = c_res;
1643 }
1644 return res;
Dees_Troy51a0e822012-09-05 15:24:24 -04001645}
1646
1647int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y)
1648{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001649 return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001650}
1651
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001652int PageManager::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001653{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001654 return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001655}
1656
that8834a0f2016-01-05 23:29:30 +01001657int PageManager::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001658{
that8834a0f2016-01-05 23:29:30 +01001659 return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001660}
1661
1662int PageManager::SetKeyBoardFocus(int inFocus)
1663{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001664 return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001665}
1666
1667int PageManager::NotifyVarChange(std::string varName, std::string value)
1668{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001669 return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001670}
1671
Ethan Yonker74db1572015-10-28 12:44:49 -05001672void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1673{
1674 if (mCurrentSet)
1675 mCurrentSet->AddStringResource(resource_source, resource_name, value);
1676}
1677
Dees_Troy51a0e822012-09-05 15:24:24 -04001678extern "C" void gui_notifyVarChange(const char *name, const char* value)
1679{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001680 if (!gGuiRunning)
1681 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001682
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001683 PageManager::NotifyVarChange(name, value);
Dees_Troy51a0e822012-09-05 15:24:24 -04001684}