blob: c274c4b5b8cb825debebe003932a764bdddcb456 [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 Yonker3fdcda42016-11-30 12:29:37 -060035#include <dirent.h>
Ethan Yonkera2dc2f22014-11-08 08:13:40 -060036#include "../twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050037#include "../partitions.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040038
39#include <string>
Xuefercac6ace2016-02-01 02:28:55 +080040#include <algorithm>
Dees_Troy51a0e822012-09-05 15:24:24 -040041
bigbiff673c7ae2020-12-02 19:44:56 -050042
bigbiffd58ba182020-03-23 10:02:29 -040043#include <ziparchive/zip_archive.h>
bigbiff673c7ae2020-12-02 19:44:56 -050044#include "ZipUtil.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050045
Dees_Troy51a0e822012-09-05 15:24:24 -040046extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000047#include "../twcommon.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060048#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010050#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051
52#include "rapidxml.hpp"
53#include "objects.hpp"
gordon13370d9133d2013-06-08 14:17:07 +020054#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040055
that74bff7f2017-01-18 22:32:36 +010056// version 2 requires theme to handle power button as action togglebacklight
Captain Throwback3b339542020-10-22 09:33:29 -040057// version 4 adds fastbootd support
58#define TW_THEME_VERSION 4
that74bff7f2017-01-18 22:32:36 +010059
Ethan Yonker8e5692f2016-01-21 11:21:06 -060060#define TW_THEME_VER_ERR -2
Ethan Yonker1308d532016-01-14 22:21:49 -060061
Dees_Troy51a0e822012-09-05 15:24:24 -040062extern int gGuiRunning;
63
64std::map<std::string, PageSet*> PageManager::mPageSets;
65PageSet* PageManager::mCurrentSet;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010066MouseCursor *PageManager::mMouseCursor = NULL;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +010067HardwareKeyboard *PageManager::mHardwareKeyboard = NULL;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -050068bool PageManager::mReloadTheme = false;
69std::string PageManager::mStartPage = "main";
Ethan Yonker74db1572015-10-28 12:44:49 -050070std::vector<language_struct> Language_List;
Dees_Troy51a0e822012-09-05 15:24:24 -040071
Ethan Yonker751a85e2014-12-12 16:59:10 -060072int tw_x_offset = 0;
73int tw_y_offset = 0;
James Christopher Adduonodcd1e442016-11-06 13:17:34 -050074int tw_w_offset = 0;
75int tw_h_offset = 0;
Ethan Yonker751a85e2014-12-12 16:59:10 -060076
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)
that6a894592016-03-13 17:51:28 +0100624 LOGERR("A char input handler has returned an error\n");
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)
that6a894592016-03-13 17:51:28 +0100640 LOGERR("An input focus handler has returned an error\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200641 }
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
that6a894592016-03-13 17:51:28 +0100666
667// transient data for loading themes
668struct LoadingContext
669{
bigbiff673c7ae2020-12-02 19:44:56 -0500670 ZipArchiveHandle zip; // zip to load theme from, or NULL for the stock theme
that6a894592016-03-13 17:51:28 +0100671 std::set<std::string> filenames; // to detect cyclic includes
672 std::string basepath; // if zip is NULL, base path to load includes from with trailing slash, otherwise empty
673 std::vector<xml_document<>*> xmldocs; // all loaded xml docs
674 std::vector<char*> xmlbuffers; // text buffers with xml content
675 std::vector<xml_node<>*> styles; // refer to <styles> nodes inside xmldocs
676 std::vector<xml_node<>*> templates; // refer to <templates> nodes inside xmldocs
677
678 LoadingContext()
679 {
680 zip = NULL;
681 }
682
683 ~LoadingContext()
684 {
685 // free all xml buffers
686 for (std::vector<char*>::iterator it = xmlbuffers.begin(); it != xmlbuffers.end(); ++it)
687 free(*it);
688 }
689
690};
691
692// for FindStyle
693LoadingContext* PageManager::currentLoadingContext = NULL;
694
695
696PageSet::PageSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400697{
that74ac6062015-03-04 22:39:34 +0100698 mResources = new ResourceManager;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200699 mCurrentPage = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400700
that6a894592016-03-13 17:51:28 +0100701 set_scale_values(1, 1); // Reset any previous scaling values
Dees_Troy51a0e822012-09-05 15:24:24 -0400702}
703
704PageSet::~PageSet()
705{
Ethan Yonker1c273312015-03-16 12:18:56 -0500706 mOverlays.clear();
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100707 for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
708 delete *itr;
709
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200710 delete mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400711}
712
that6a894592016-03-13 17:51:28 +0100713int PageSet::Load(LoadingContext& ctx, const std::string& filename)
714{
715 bool isMain = ctx.xmlbuffers.empty(); // if we have no files yet, remember that this is the main XML file
716
717 if (!ctx.filenames.insert(filename).second)
718 // ignore already loaded files to prevent crash with cyclic includes
719 return 0;
720
721 // load XML into buffer
722 char* xmlbuffer = PageManager::LoadFileToBuffer(filename, ctx.zip);
723 if (!xmlbuffer)
724 return -1; // error already displayed by LoadFileToBuffer
725 ctx.xmlbuffers.push_back(xmlbuffer);
726
727 // parse XML
728 xml_document<>* doc = new xml_document<>();
729 doc->parse<0>(xmlbuffer);
730 ctx.xmldocs.push_back(doc);
731
732 xml_node<>* root = doc->first_node("recovery");
733 if (!root)
734 root = doc->first_node("install");
735 if (!root) {
736 LOGERR("Unknown root element in %s\n", filename.c_str());
737 return -1;
738 }
739
740 if (isMain) {
741 int rc = LoadDetails(ctx, root);
742 if (rc != 0)
743 return rc;
744 }
745
746 LOGINFO("Loading resources...\n");
747 xml_node<>* child = root->first_node("resources");
748 if (child)
749 mResources->LoadResources(child, ctx.zip, "theme");
750
751 LOGINFO("Loading variables...\n");
752 child = root->first_node("variables");
753 if (child)
754 LoadVariables(child);
755
756 LOGINFO("Loading mouse cursor...\n");
757 child = root->first_node("mousecursor");
758 if (child)
759 PageManager::LoadCursorData(child);
760
761 LOGINFO("Loading pages...\n");
762 child = root->first_node("templates");
763 if (child)
764 ctx.templates.push_back(child);
765
766 child = root->first_node("styles");
767 if (child)
768 ctx.styles.push_back(child);
769
770 // Load pages
771 child = root->first_node("pages");
772 if (child) {
773 if (LoadPages(ctx, child)) {
774 LOGERR("PageSet::Load returning -1\n");
775 return -1;
776 }
777 }
778
779 // process includes recursively
780 child = root->first_node("include");
781 if (child) {
782 xml_node<>* include = child->first_node("xmlfile");
783 while (include != NULL) {
784 xml_attribute<>* attr = include->first_attribute("name");
785 if (!attr) {
786 LOGERR("Skipping include/xmlfile with no name\n");
787 continue;
788 }
789
790 string filename = ctx.basepath + attr->value();
791 LOGINFO("Including file: %s...\n", filename.c_str());
792 int rc = Load(ctx, filename);
793 if (rc != 0)
794 return rc;
795
796 include = include->next_sibling("xmlfile");
797 }
798 }
799
800 return 0;
801}
802
803void PageSet::MakeEmergencyConsoleIfNeeded()
804{
805 if (mPages.empty()) {
806 mCurrentPage = new Page(NULL, NULL); // fallback console page
807 // TODO: since removal of non-TTF fonts, the emergency console doesn't work without a font, which might be missing too
808 mPages.push_back(mCurrentPage);
809 }
810}
811
bigbiff673c7ae2020-12-02 19:44:56 -0500812int PageSet::LoadLanguage(char* languageFile, ZipArchiveHandle package)
Ethan Yonker74db1572015-10-28 12:44:49 -0500813{
814 xml_document<> lang;
815 xml_node<>* parent;
816 xml_node<>* child;
817 std::string resource_source;
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600818 int ret = 0;
Ethan Yonker74db1572015-10-28 12:44:49 -0500819
820 if (languageFile) {
821 printf("parsing languageFile\n");
822 lang.parse<0>(languageFile);
823 printf("parsing languageFile done\n");
824 } else {
825 return -1;
826 }
827
828 parent = lang.first_node("language");
829 if (!parent) {
830 LOGERR("Unable to locate language node in language file.\n");
831 lang.clear();
832 return -1;
833 }
834
835 child = parent->first_node("display");
836 if (child) {
837 DataManager::SetValue("tw_language_display", child->value());
838 resource_source = child->value();
839 } else {
840 LOGERR("language file does not have a display value set\n");
841 DataManager::SetValue("tw_language_display", "Not Set");
842 resource_source = languageFile;
843 }
844
845 child = parent->first_node("resources");
846 if (child)
847 mResources->LoadResources(child, package, resource_source);
848 else
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600849 ret = -1;
850 DataManager::SetValue("tw_backup_name", gui_lookup("auto_generate", "(Auto Generate)"));
Ethan Yonker74db1572015-10-28 12:44:49 -0500851 lang.clear();
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600852 return ret;
Ethan Yonker74db1572015-10-28 12:44:49 -0500853}
854
that6a894592016-03-13 17:51:28 +0100855int PageSet::LoadDetails(LoadingContext& ctx, xml_node<>* root)
Dees_Troy51a0e822012-09-05 15:24:24 -0400856{
that6a894592016-03-13 17:51:28 +0100857 xml_node<>* child = root->first_node("details");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600858 if (child) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600859 int theme_ver = 0;
860 xml_node<>* themeversion = child->first_node("themeversion");
861 if (themeversion && themeversion->value()) {
862 theme_ver = atoi(themeversion->value());
863 } else {
864 LOGINFO("No themeversion in theme.\n");
865 }
866 if (theme_ver != TW_THEME_VERSION) {
867 LOGINFO("theme version from xml: %i, expected %i\n", theme_ver, TW_THEME_VERSION);
that6a894592016-03-13 17:51:28 +0100868 if (ctx.zip) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600869 gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme.");
Ethan Yonker8e5692f2016-01-21 11:21:06 -0600870 return TW_THEME_VER_ERR;
Ethan Yonker1308d532016-01-14 22:21:49 -0600871 } else {
872 gui_print_color("warning", "Stock theme version does not match TWRP version.\n");
873 }
874 }
Ethan Yonker63e414f2015-02-06 15:44:39 -0600875 xml_node<>* resolution = child->first_node("resolution");
876 if (resolution) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600877 LOGINFO("Checking resolution...\n");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600878 xml_attribute<>* width_attr = resolution->first_attribute("width");
879 xml_attribute<>* height_attr = resolution->first_attribute("height");
880 xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
881 if (width_attr && height_attr && !noscale_attr) {
882 int width = atoi(width_attr->value());
883 int height = atoi(height_attr->value());
884 int offx = 0, offy = 0;
885#ifdef TW_ROUND_SCREEN
886 xml_node<>* roundscreen = child->first_node("roundscreen");
887 if (roundscreen) {
888 LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n");
889 xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x");
890 xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y");
891 if (offx_attr) {
892 offx = atoi(offx_attr->value());
893 }
894 if (offy_attr) {
895 offy = atoi(offy_attr->value());
896 }
897 }
898#endif
899 if (width != 0 && height != 0) {
James Christopher Adduonodcd1e442016-11-06 13:17:34 -0500900 float scale_w = (((float)gr_fb_width() + (float)tw_w_offset) - ((float)offx * 2.0)) / (float)width;
901 float scale_h = (((float)gr_fb_height() + (float)tw_h_offset) - ((float)offy * 2.0)) / (float)height;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600902#ifdef TW_ROUND_SCREEN
James Christopher Adduonodcd1e442016-11-06 13:17:34 -0500903 float scale_off_w = ((float)gr_fb_width() + (float)tw_w_offset) / (float)width;
904 float scale_off_h = ((float)gr_fb_height() + (float)tw_h_offset) / (float)height;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600905 tw_x_offset = offx * scale_off_w;
906 tw_y_offset = offy * scale_off_h;
907#endif
908 if (scale_w != 1 || scale_h != 1) {
James Christopher Adduonodcd1e442016-11-06 13:17:34 -0500909 LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i w: %i h: %i\n",
910 scale_w, scale_h, tw_x_offset, tw_y_offset, tw_w_offset, tw_h_offset);
Ethan Yonker63e414f2015-02-06 15:44:39 -0600911 set_scale_values(scale_w, scale_h);
912 }
913 }
914 } else {
915 LOGINFO("XML does not contain width and height, no scaling will be applied\n");
916 }
917 } else {
918 LOGINFO("XML contains no resolution tag, no scaling will be applied.\n");
919 }
920 } else {
921 LOGINFO("XML contains no details tag, no scaling will be applied.\n");
922 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500923
Ethan Yonker780cd392014-07-21 15:24:39 -0500924 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400925}
926
927int PageSet::SetPage(std::string page)
928{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200929 Page* tmp = FindPage(page);
930 if (tmp)
931 {
932 if (mCurrentPage) mCurrentPage->SetPageFocus(0);
933 mCurrentPage = tmp;
934 mCurrentPage->SetPageFocus(1);
935 mCurrentPage->NotifyVarChange("", "");
936 return 0;
937 }
938 else
939 {
940 LOGERR("Unable to locate page (%s)\n", page.c_str());
941 }
942 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400943}
944
945int PageSet::SetOverlay(Page* page)
946{
Ethan Yonker1c273312015-03-16 12:18:56 -0500947 if (page) {
948 if (mOverlays.size() >= 10) {
949 LOGERR("Too many overlays requested, max is 10.\n");
950 return -1;
951 }
Matt Mowerd411f8d2015-04-09 16:04:12 -0500952
953 std::vector<Page*>::iterator iter;
954 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
955 if ((*iter)->GetName() == page->GetName()) {
956 mOverlays.erase(iter);
957 // SetOverlay() is (and should stay) the only function which
958 // adds to mOverlays. Then, each page can appear at most once.
959 break;
960 }
961 }
962
Ethan Yonker1c273312015-03-16 12:18:56 -0500963 page->SetPageFocus(1);
964 page->NotifyVarChange("", "");
965
966 if (!mOverlays.empty())
967 mOverlays.back()->SetPageFocus(0);
968
969 mOverlays.push_back(page);
970 } else {
971 if (!mOverlays.empty()) {
972 mOverlays.back()->SetPageFocus(0);
973 mOverlays.pop_back();
974 if (!mOverlays.empty())
975 mOverlays.back()->SetPageFocus(1);
976 else if (mCurrentPage)
977 mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
978 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200979 }
980 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400981}
982
that74ac6062015-03-04 22:39:34 +0100983const ResourceManager* PageSet::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -0400984{
that74ac6062015-03-04 22:39:34 +0100985 return mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400986}
987
988Page* PageSet::FindPage(std::string name)
989{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200990 std::vector<Page*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400991
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200992 for (iter = mPages.begin(); iter != mPages.end(); iter++)
993 {
994 if (name == (*iter)->GetName())
995 return (*iter);
996 }
997 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400998}
999
1000int PageSet::LoadVariables(xml_node<>* vars)
1001{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001002 xml_node<>* child;
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001003 xml_attribute<> *name, *value, *persist;
1004 int p;
Dees_Troy51a0e822012-09-05 15:24:24 -04001005
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001006 child = vars->first_node("variable");
1007 while (child)
1008 {
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001009 name = child->first_attribute("name");
1010 value = child->first_attribute("value");
1011 persist = child->first_attribute("persist");
Matt Mowera8a89d12016-12-30 18:10:37 -06001012 if (name && value)
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001013 {
Ethan Yonker751a85e2014-12-12 16:59:10 -06001014 if (strcmp(name->value(), "tw_x_offset") == 0) {
1015 tw_x_offset = atoi(value->value());
1016 child = child->next_sibling("variable");
1017 continue;
1018 }
1019 if (strcmp(name->value(), "tw_y_offset") == 0) {
1020 tw_y_offset = atoi(value->value());
1021 child = child->next_sibling("variable");
1022 continue;
1023 }
James Christopher Adduonodcd1e442016-11-06 13:17:34 -05001024 if (strcmp(name->value(), "tw_w_offset") == 0) {
1025 tw_w_offset = atoi(value->value());
1026 child = child->next_sibling("variable");
1027 continue;
1028 }
1029 if (strcmp(name->value(), "tw_h_offset") == 0) {
1030 tw_h_offset = atoi(value->value());
1031 child = child->next_sibling("variable");
1032 continue;
1033 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001034 p = persist ? atoi(persist->value()) : 0;
Ethan Yonker96acb3d2014-08-05 09:20:30 -05001035 string temp = value->value();
1036 string valstr = gui_parse_text(temp);
1037
1038 if (valstr.find("+") != string::npos) {
1039 string val1str = valstr;
1040 val1str = val1str.substr(0, val1str.find('+'));
1041 string val2str = valstr;
1042 val2str = val2str.substr(val2str.find('+') + 1, string::npos);
1043 int val1 = atoi(val1str.c_str());
1044 int val2 = atoi(val2str.c_str());
1045 int val = val1 + val2;
1046
1047 DataManager::SetValue(name->value(), val, p);
1048 } else if (valstr.find("-") != string::npos) {
1049 string val1str = valstr;
1050 val1str = val1str.substr(0, val1str.find('-'));
1051 string val2str = valstr;
1052 val2str = val2str.substr(val2str.find('-') + 1, string::npos);
1053 int val1 = atoi(val1str.c_str());
1054 int val2 = atoi(val2str.c_str());
1055 int val = val1 - val2;
1056
1057 DataManager::SetValue(name->value(), val, p);
1058 } else {
1059 DataManager::SetValue(name->value(), valstr, p);
1060 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001061 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001062
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001063 child = child->next_sibling("variable");
1064 }
1065 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
that6a894592016-03-13 17:51:28 +01001068int PageSet::LoadPages(LoadingContext& ctx, xml_node<>* pages)
Dees_Troy51a0e822012-09-05 15:24:24 -04001069{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001070 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -04001071
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001072 if (!pages)
1073 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001074
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001075 child = pages->first_node("page");
1076 while (child != NULL)
1077 {
that6a894592016-03-13 17:51:28 +01001078 Page* page = new Page(child, &ctx.templates);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001079 if (page->GetName().empty())
1080 {
1081 LOGERR("Unable to process load page\n");
1082 delete page;
1083 }
1084 else
1085 {
1086 mPages.push_back(page);
1087 }
1088 child = child->next_sibling("page");
1089 }
1090 if (mPages.size() > 0)
1091 return 0;
1092 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093}
1094
1095int PageSet::IsCurrentPage(Page* page)
1096{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001097 return ((mCurrentPage && mCurrentPage == page) ? 1 : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001098}
1099
that10ae24f2015-12-26 20:53:51 +01001100std::string PageSet::GetCurrentPage() const
1101{
1102 return mCurrentPage ? mCurrentPage->GetName() : "";
1103}
1104
Dees_Troy51a0e822012-09-05 15:24:24 -04001105int PageSet::Render(void)
1106{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001107 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001109 ret = (mCurrentPage ? mCurrentPage->Render() : -1);
1110 if (ret < 0)
1111 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001112
1113 std::vector<Page*>::iterator iter;
1114
1115 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1116 ret = ((*iter) ? (*iter)->Render() : -1);
1117 if (ret < 0)
1118 return ret;
1119 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001120 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001121}
1122
1123int PageSet::Update(void)
1124{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001125 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001127 ret = (mCurrentPage ? mCurrentPage->Update() : -1);
1128 if (ret < 0 || ret > 1)
1129 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001130
1131 std::vector<Page*>::iterator iter;
1132
1133 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1134 ret = ((*iter) ? (*iter)->Update() : -1);
1135 if (ret < 0)
1136 return ret;
1137 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001138 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001139}
1140
1141int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
1142{
Ethan Yonker1c273312015-03-16 12:18:56 -05001143 if (!mOverlays.empty())
1144 return mOverlays.back()->NotifyTouch(state, x, y);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001145
1146 return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001147}
1148
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001149int PageSet::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001150{
Ethan Yonker1c273312015-03-16 12:18:56 -05001151 if (!mOverlays.empty())
1152 return mOverlays.back()->NotifyKey(key, down);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001153
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001154 return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001155}
1156
that8834a0f2016-01-05 23:29:30 +01001157int PageSet::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001158{
Ethan Yonker1c273312015-03-16 12:18:56 -05001159 if (!mOverlays.empty())
that8834a0f2016-01-05 23:29:30 +01001160 return mOverlays.back()->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001161
that8834a0f2016-01-05 23:29:30 +01001162 return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001163}
1164
1165int PageSet::SetKeyBoardFocus(int inFocus)
1166{
Ethan Yonker1c273312015-03-16 12:18:56 -05001167 if (!mOverlays.empty())
1168 return mOverlays.back()->SetKeyBoardFocus(inFocus);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001169
1170 return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001171}
1172
1173int PageSet::NotifyVarChange(std::string varName, std::string value)
1174{
Ethan Yonker1c273312015-03-16 12:18:56 -05001175 std::vector<Page*>::iterator iter;
1176
1177 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
1178 (*iter)->NotifyVarChange(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001179
1180 return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001181}
1182
Ethan Yonker74db1572015-10-28 12:44:49 -05001183void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1184{
1185 mResources->AddStringResource(resource_source, resource_name, value);
1186}
1187
bigbiff673c7ae2020-12-02 19:44:56 -05001188char* PageManager::LoadFileToBuffer(std::string filename, ZipArchiveHandle package) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001189 size_t len;
1190 char* buffer = NULL;
1191
bigbiff673c7ae2020-12-02 19:44:56 -05001192 if (package) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001193 // We can try to load the XML directly...
1194 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str());
1195 struct stat st;
Matt Mowera8a89d12016-12-30 18:10:37 -06001196 if (stat(filename.c_str(),&st) != 0) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001197 // This isn't always an error, sometimes we request files that don't exist.
1198 return NULL;
1199 }
1200
1201 len = (size_t)st.st_size;
1202
1203 buffer = (char*) malloc(len + 1);
1204 if (!buffer) {
1205 LOGERR("PageManager::LoadFileToBuffer failed to malloc\n");
1206 return NULL;
1207 }
1208
1209 int fd = open(filename.c_str(), O_RDONLY);
1210 if (fd == -1) {
1211 LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno));
1212 free(buffer);
1213 return NULL;
1214 }
1215
1216 if (read(fd, buffer, len) < 0) {
1217 LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno));
1218 free(buffer);
1219 close(fd);
1220 return NULL;
1221 }
1222 close(fd);
1223 } else {
1224 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
bigbiff673c7ae2020-12-02 19:44:56 -05001225 ZipEntry binary_entry;
1226 if (FindEntry(package, filename, &binary_entry) == 0) {
1227 // if (!package->EntryExists(filename)) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001228 LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
1229 return NULL;
1230 }
1231
1232 // Allocate the buffer for the file
bigbiff673c7ae2020-12-02 19:44:56 -05001233 len = binary_entry.uncompressed_length;
1234 // len = package->GetUncompressedSize(filename);
Ethan Yonker561c58d2015-10-05 08:48:22 -05001235 buffer = (char*) malloc(len + 1);
1236 if (!buffer)
1237 return NULL;
1238
bigbiff673c7ae2020-12-02 19:44:56 -05001239 int32_t err =
1240 ExtractToMemory(package, &binary_entry, reinterpret_cast<uint8_t*>(buffer), len);
1241 if (err != 0) {
1242 // if (!package->ExtractToBuffer(filename, (unsigned char*) buffer)) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001243 LOGERR("Unable to extract '%s'\n", filename.c_str());
1244 free(buffer);
1245 return NULL;
1246 }
1247 }
1248 // NULL-terminate the string
1249 buffer[len] = 0x00;
1250 return buffer;
1251}
1252
Ethan Yonker74db1572015-10-28 12:44:49 -05001253void PageManager::LoadLanguageListDir(string dir) {
1254 if (!TWFunc::Path_Exists(dir)) {
1255 LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str());
1256 return;
1257 }
1258
1259 DIR *d = opendir(dir.c_str());
1260 struct dirent *p;
1261
1262 if (d == NULL) {
1263 LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno));
1264 return;
1265 }
1266
1267 while ((p = readdir(d))) {
1268 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5)
1269 continue;
1270
1271 string file = p->d_name;
1272 if (file.substr(strlen(p->d_name) - 4) != ".xml")
1273 continue;
1274 string path = dir + p->d_name;
1275 string file_no_extn = file.substr(0, strlen(p->d_name) - 4);
1276 struct language_struct language_entry;
1277 language_entry.filename = file_no_extn;
1278 char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL);
1279 if (xmlFile == NULL) {
1280 LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str());
1281 continue;
1282 }
1283 xml_document<> *doc = new xml_document<>();
1284 doc->parse<0>(xmlFile);
1285
1286 xml_node<>* parent = doc->first_node("language");
1287 if (!parent) {
1288 LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str());
1289 } else {
1290 xml_node<>* child = parent->first_node("display");
1291 if (child) {
1292 language_entry.displayvalue = child->value();
1293 } else {
1294 LOGERR("No display value for '%s'\n", language_entry.filename.c_str());
1295 language_entry.displayvalue = language_entry.filename;
1296 }
1297 Language_List.push_back(language_entry);
1298 }
1299 doc->clear();
1300 delete doc;
1301 free(xmlFile);
1302 }
1303 closedir(d);
1304}
1305
bigbiff673c7ae2020-12-02 19:44:56 -05001306void PageManager::LoadLanguageList(ZipArchiveHandle package) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001307 Language_List.clear();
1308 if (TWFunc::Path_Exists(TWRES "customlanguages"))
1309 TWFunc::removeDir(TWRES "customlanguages", true);
1310 if (package) {
1311 TWFunc::Recursive_Mkdir(TWRES "customlanguages");
bigbiff673c7ae2020-12-02 19:44:56 -05001312 ExtractPackageRecursive(package, "", TWRES "customlanguages", nullptr, nullptr);
1313
1314 // package->ExtractRecursive("languages", TWRES "customlanguages/");
Ethan Yonker74db1572015-10-28 12:44:49 -05001315 LoadLanguageListDir(TWRES "customlanguages/");
1316 } else {
1317 LoadLanguageListDir(TWRES "languages/");
1318 }
Xuefercac6ace2016-02-01 02:28:55 +08001319
1320 std::sort(Language_List.begin(), Language_List.end());
Ethan Yonker74db1572015-10-28 12:44:49 -05001321}
1322
1323void PageManager::LoadLanguage(string filename) {
1324 string actual_filename;
1325 if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml"))
1326 actual_filename = TWRES "customlanguages/" + filename + ".xml";
1327 else
1328 actual_filename = TWRES "languages/" + filename + ".xml";
1329 char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL);
1330 if (xmlFile == NULL)
1331 LOGERR("Unable to load '%s'\n", actual_filename.c_str());
1332 else {
1333 mCurrentSet->LoadLanguage(xmlFile, NULL);
1334 free(xmlFile);
1335 }
1336 PartitionManager.Translate_Partition_Display_Names();
1337}
1338
Dees_Troy51a0e822012-09-05 15:24:24 -04001339int PageManager::LoadPackage(std::string name, std::string package, std::string startpage)
1340{
that6a894592016-03-13 17:51:28 +01001341 std::string mainxmlfilename = package;
Ethan Yonker74db1572015-10-28 12:44:49 -05001342 char* languageFile = NULL;
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001343 char* baseLanguageFile = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001344 PageSet* pageSet = NULL;
1345 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001346
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001347 mReloadTheme = false;
1348 mStartPage = startpage;
1349
that6a894592016-03-13 17:51:28 +01001350 // init the loading context
1351 LoadingContext ctx;
1352
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001353 // Open the XML file
1354 LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str());
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001355 if (package.size() > 4 && package.substr(package.size() - 4) != ".zip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001356 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001357 LOGINFO("Load XML directly\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001358 tw_x_offset = TW_X_OFFSET;
1359 tw_y_offset = TW_Y_OFFSET;
James Christopher Adduonodcd1e442016-11-06 13:17:34 -05001360 tw_w_offset = TW_W_OFFSET;
1361 tw_h_offset = TW_H_OFFSET;
Ethan Yonker4f74d142016-03-31 08:10:37 -05001362 if (name != "splash") {
1363 LoadLanguageList(NULL);
1364 languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
1365 }
that6a894592016-03-13 17:51:28 +01001366 ctx.basepath = TWRES;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001367 }
1368 else
1369 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001370 LOGINFO("Loading zip theme\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001371 tw_x_offset = 0;
1372 tw_y_offset = 0;
James Christopher Adduonodcd1e442016-11-06 13:17:34 -05001373 tw_w_offset = 0;
1374 tw_h_offset = 0;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001375 if (!TWFunc::Path_Exists(package))
1376 return -1;
bigbiff673c7ae2020-12-02 19:44:56 -05001377
1378 ZipArchiveHandle Zip;
1379 int err = OpenArchive(package.c_str(), &Zip);
1380
1381 if (err != 0)
1382 return -1;
1383
1384 ctx.zip = Zip;
that6a894592016-03-13 17:51:28 +01001385 mainxmlfilename = "ui.xml";
1386 LoadLanguageList(ctx.zip);
1387 languageFile = LoadFileToBuffer("languages/en.xml", ctx.zip);
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001388 baseLanguageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001389 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001390
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001391 // Before loading, mCurrentSet must be the loading package so we can find resources
1392 pageSet = mCurrentSet;
that6a894592016-03-13 17:51:28 +01001393 mCurrentSet = new PageSet();
1394
1395 if (baseLanguageFile) {
1396 mCurrentSet->LoadLanguage(baseLanguageFile, NULL);
1397 free(baseLanguageFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001398 }
that6a894592016-03-13 17:51:28 +01001399
1400 if (languageFile) {
1401 mCurrentSet->LoadLanguage(languageFile, ctx.zip);
1402 free(languageFile);
1403 }
1404
1405 // Load and parse the XML and all includes
1406 currentLoadingContext = &ctx; // required to find styles
1407 ret = mCurrentSet->Load(ctx, mainxmlfilename);
1408 currentLoadingContext = NULL;
1409
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001410 if (ret == 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001411 mCurrentSet->SetPage(startpage);
1412 mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001413 } else {
1414 if (ret != TW_THEME_VER_ERR)
1415 LOGERR("Package %s failed to load.\n", name.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001416 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001417
that6a894592016-03-13 17:51:28 +01001418 // reset to previous pageset
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001419 mCurrentSet = pageSet;
1420
that6a894592016-03-13 17:51:28 +01001421 if (ctx.zip) {
bigbiff673c7ae2020-12-02 19:44:56 -05001422 CloseArchive(ctx.zip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001423 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001424 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001425}
1426
1427PageSet* PageManager::FindPackage(std::string name)
1428{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001429 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001430
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001431 iter = mPageSets.find(name);
1432 if (iter != mPageSets.end())
1433 return (*iter).second;
1434
1435 LOGERR("Unable to locate package %s\n", name.c_str());
1436 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001437}
1438
1439PageSet* PageManager::SelectPackage(std::string name)
1440{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001441 LOGINFO("Switching packages (%s)\n", name.c_str());
1442 PageSet* tmp;
Dees_Troy51a0e822012-09-05 15:24:24 -04001443
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001444 tmp = FindPackage(name);
1445 if (tmp)
Vojtech Bocek07220562014-02-08 02:05:33 +01001446 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001447 mCurrentSet = tmp;
that6a894592016-03-13 17:51:28 +01001448 mCurrentSet->MakeEmergencyConsoleIfNeeded();
Vojtech Bocek07220562014-02-08 02:05:33 +01001449 mCurrentSet->NotifyVarChange("", "");
1450 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001451 else
1452 LOGERR("Unable to find package.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001453
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001454 return mCurrentSet;
Dees_Troy51a0e822012-09-05 15:24:24 -04001455}
1456
1457int PageManager::ReloadPackage(std::string name, std::string package)
1458{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001459 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001460
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001461 mReloadTheme = false;
1462
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001463 iter = mPageSets.find(name);
1464 if (iter == mPageSets.end())
1465 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001466
Matt Mowera8a89d12016-12-30 18:10:37 -06001467 if (mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001468 mMouseCursor->ResetData(gr_fb_width(), gr_fb_height());
1469
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001470 PageSet* set = (*iter).second;
1471 mPageSets.erase(iter);
Dees_Troy51a0e822012-09-05 15:24:24 -04001472
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001473 if (LoadPackage(name, package, mStartPage) != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001474 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001475 LOGINFO("Failed to load package '%s'.\n", package.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001476 mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
1477 return -1;
1478 }
1479 if (mCurrentSet == set)
1480 SelectPackage(name);
1481 delete set;
Ethan Yonker74db1572015-10-28 12:44:49 -05001482 GUIConsole::Translate_Now();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001483 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001484}
1485
1486void PageManager::ReleasePackage(std::string name)
1487{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001488 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001489
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001490 iter = mPageSets.find(name);
1491 if (iter == mPageSets.end())
1492 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001493
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001494 PageSet* set = (*iter).second;
1495 mPageSets.erase(iter);
1496 delete set;
that235c6482016-01-24 21:59:00 +01001497 if (set == mCurrentSet)
1498 mCurrentSet = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001499 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001500}
1501
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001502int PageManager::RunReload() {
1503 int ret_val = 0;
1504 std::string theme_path;
1505
1506 if (!mReloadTheme)
1507 return 0;
1508
1509 mReloadTheme = false;
1510 theme_path = DataManager::GetSettingsStoragePath();
1511 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1512 LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
1513 ret_val = 1;
1514 }
1515
1516 theme_path += "/TWRP/theme/ui.zip";
1517 if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0)
1518 {
1519 // Loading the custom theme failed - try loading the stock theme
1520 LOGINFO("Attempting to reload stock theme...\n");
1521 if (ReloadPackage("TWRP", TWRES "ui.xml"))
1522 {
1523 LOGERR("Failed to load base packages.\n");
1524 ret_val = 1;
1525 }
1526 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001527 if (ret_val == 0) {
1528 if (DataManager::GetStrValue("tw_language") != "en.xml") {
1529 LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str());
1530 LoadLanguage(DataManager::GetStrValue("tw_language"));
1531 }
1532 }
1533
1534 // This makes the console re-translate
thata9dd9f02017-02-23 23:08:56 +01001535 GUIConsole::Clear_For_Retranslation();
Ethan Yonker74db1572015-10-28 12:44:49 -05001536
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001537 return ret_val;
1538}
1539
1540void PageManager::RequestReload() {
1541 mReloadTheme = true;
1542}
1543
Ethan Yonkerafde0982016-01-23 08:55:35 -06001544void PageManager::SetStartPage(const std::string& page_name) {
1545 mStartPage = page_name;
1546}
1547
Dees_Troy51a0e822012-09-05 15:24:24 -04001548int PageManager::ChangePage(std::string name)
1549{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001550 DataManager::SetValue("tw_operation_state", 0);
1551 int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1);
1552 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001553}
1554
that10ae24f2015-12-26 20:53:51 +01001555std::string PageManager::GetCurrentPage()
1556{
1557 return mCurrentSet ? mCurrentSet->GetCurrentPage() : "";
1558}
1559
Dees_Troy51a0e822012-09-05 15:24:24 -04001560int PageManager::ChangeOverlay(std::string name)
1561{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001562 if (name.empty())
1563 return mCurrentSet->SetOverlay(NULL);
1564 else
1565 {
Ethan Yonker1308d532016-01-14 22:21:49 -06001566 Page* page = mCurrentSet ? mCurrentSet->FindPage(name) : NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001567 return mCurrentSet->SetOverlay(page);
1568 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001569}
1570
that74ac6062015-03-04 22:39:34 +01001571const ResourceManager* PageManager::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001572{
that74ac6062015-03-04 22:39:34 +01001573 return (mCurrentSet ? mCurrentSet->GetResources() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001574}
1575
Dees_Troy51a0e822012-09-05 15:24:24 -04001576int PageManager::IsCurrentPage(Page* page)
1577{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001578 return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001579}
1580
1581int PageManager::Render(void)
1582{
Matt Mowera8a89d12016-12-30 18:10:37 -06001583 if (blankTimer.isScreenOff())
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001584 return 0;
1585
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001586 int res = (mCurrentSet ? mCurrentSet->Render() : -1);
Matt Mowera8a89d12016-12-30 18:10:37 -06001587 if (mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001588 mMouseCursor->Render();
1589 return res;
1590}
1591
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001592HardwareKeyboard *PageManager::GetHardwareKeyboard()
1593{
Matt Mowera8a89d12016-12-30 18:10:37 -06001594 if (!mHardwareKeyboard)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001595 mHardwareKeyboard = new HardwareKeyboard();
1596 return mHardwareKeyboard;
1597}
1598
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001599xml_node<>* PageManager::FindStyle(std::string name)
1600{
that6a894592016-03-13 17:51:28 +01001601 if (!currentLoadingContext)
1602 {
1603 LOGERR("FindStyle works only while loading a theme.\n");
1604 return NULL;
1605 }
1606
1607 for (std::vector<xml_node<>*>::iterator itr = currentLoadingContext->styles.begin(); itr != currentLoadingContext->styles.end(); itr++) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001608 xml_node<>* node = (*itr)->first_node("style");
1609
1610 while (node) {
1611 if (!node->first_attribute("name"))
1612 continue;
1613
1614 if (name == node->first_attribute("name")->value())
1615 return node;
1616 node = node->next_sibling("style");
1617 }
1618 }
1619 return NULL;
1620}
1621
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001622MouseCursor *PageManager::GetMouseCursor()
1623{
Matt Mowera8a89d12016-12-30 18:10:37 -06001624 if (!mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001625 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1626 return mMouseCursor;
1627}
1628
1629void PageManager::LoadCursorData(xml_node<>* node)
1630{
Matt Mowera8a89d12016-12-30 18:10:37 -06001631 if (!mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001632 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1633
1634 mMouseCursor->LoadData(node);
Dees_Troy51a0e822012-09-05 15:24:24 -04001635}
1636
1637int PageManager::Update(void)
1638{
Matt Mowera8a89d12016-12-30 18:10:37 -06001639 if (blankTimer.isScreenOff())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001640 return 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001641
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001642 if (RunReload())
1643 return -2;
1644
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001645 int res = (mCurrentSet ? mCurrentSet->Update() : -1);
1646
Matt Mowera8a89d12016-12-30 18:10:37 -06001647 if (mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001648 {
1649 int c_res = mMouseCursor->Update();
Matt Mowera8a89d12016-12-30 18:10:37 -06001650 if (c_res > res)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001651 res = c_res;
1652 }
1653 return res;
Dees_Troy51a0e822012-09-05 15:24:24 -04001654}
1655
1656int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y)
1657{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001658 return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001659}
1660
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001661int PageManager::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001662{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001663 return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001664}
1665
that8834a0f2016-01-05 23:29:30 +01001666int PageManager::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001667{
that8834a0f2016-01-05 23:29:30 +01001668 return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
1671int PageManager::SetKeyBoardFocus(int inFocus)
1672{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001673 return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001674}
1675
1676int PageManager::NotifyVarChange(std::string varName, std::string value)
1677{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001678 return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001679}
1680
Ethan Yonker74db1572015-10-28 12:44:49 -05001681void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1682{
1683 if (mCurrentSet)
1684 mCurrentSet->AddStringResource(resource_source, resource_name, value);
1685}
1686
Dees_Troy51a0e822012-09-05 15:24:24 -04001687extern "C" void gui_notifyVarChange(const char *name, const char* value)
1688{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001689 if (!gGuiRunning)
1690 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001691
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001692 PageManager::NotifyVarChange(name, value);
Dees_Troy51a0e822012-09-05 15:24:24 -04001693}