blob: 3145c15f04df0163c597b2ab8c871231325b9042 [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}
bigbiffd81833a2021-01-17 11:06:57 -050050#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;
Mohd Faraz77bbeb02020-08-12 12:29:42 +000063GUITerminal* term = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040064
65std::map<std::string, PageSet*> PageManager::mPageSets;
66PageSet* PageManager::mCurrentSet;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010067MouseCursor *PageManager::mMouseCursor = NULL;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +010068HardwareKeyboard *PageManager::mHardwareKeyboard = NULL;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -050069bool PageManager::mReloadTheme = false;
70std::string PageManager::mStartPage = "main";
Ethan Yonker74db1572015-10-28 12:44:49 -050071std::vector<language_struct> Language_List;
Dees_Troy51a0e822012-09-05 15:24:24 -040072
Ethan Yonker751a85e2014-12-12 16:59:10 -060073int tw_x_offset = 0;
74int tw_y_offset = 0;
James Christopher Adduonodcd1e442016-11-06 13:17:34 -050075int tw_w_offset = 0;
76int tw_h_offset = 0;
Ethan Yonker751a85e2014-12-12 16:59:10 -060077
Dees_Troy51a0e822012-09-05 15:24:24 -040078// Helper routine to convert a string to a color declaration
79int ConvertStrToColor(std::string str, COLOR* color)
80{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 // Set the default, solid black
82 memset(color, 0, sizeof(COLOR));
83 color->alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 // Translate variables
86 DataManager::GetValue(str, str);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 // Look for some defaults
thatf6ed8fc2015-02-14 20:23:16 +010089 if (str == "black") return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 else if (str == "white") { color->red = color->green = color->blue = 255; return 0; }
91 else if (str == "red") { color->red = 255; return 0; }
92 else if (str == "green") { color->green = 255; return 0; }
93 else if (str == "blue") { color->blue = 255; return 0; }
Dees_Troy51a0e822012-09-05 15:24:24 -040094
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 // At this point, we require an RGB(A) color
96 if (str[0] != '#')
97 return -1;
98
99 str.erase(0, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Dees_Troy30b962e2012-10-19 20:48:59 -0400101 int result;
102 if (str.size() >= 8) {
103 // We have alpha channel
104 string alpha = str.substr(6, 2);
105 result = strtol(alpha.c_str(), NULL, 16);
106 color->alpha = result & 0x000000FF;
107 str.resize(6);
108 result = strtol(str.c_str(), NULL, 16);
109 color->red = (result >> 16) & 0x000000FF;
110 color->green = (result >> 8) & 0x000000FF;
111 color->blue = result & 0x000000FF;
112 } else {
113 result = strtol(str.c_str(), NULL, 16);
114 color->red = (result >> 16) & 0x000000FF;
115 color->green = (result >> 8) & 0x000000FF;
116 color->blue = result & 0x000000FF;
117 }
118 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119}
120
121// Helper APIs
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600122xml_node<>* FindNode(xml_node<>* parent, const char* nodename, int depth /* = 0 */)
123{
that8d46c092015-02-26 01:30:04 +0100124 if (!parent)
125 return NULL;
126
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600127 xml_node<>* child = parent->first_node(nodename);
128 if (child)
129 return child;
130
131 if (depth == 10) {
132 LOGERR("Too many style loops detected.\n");
133 return NULL;
134 }
135
136 xml_node<>* style = parent->first_node("style");
137 if (style) {
138 while (style) {
139 if (!style->first_attribute("name")) {
140 LOGERR("No name given for style.\n");
141 continue;
142 } else {
143 std::string name = style->first_attribute("name")->value();
144 xml_node<>* node = PageManager::FindStyle(name);
145
146 if (node) {
147 // We found the style that was named
148 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
149 if (stylenode)
150 return stylenode;
151 }
152 }
153 style = style->next_sibling("style");
154 }
155 } else {
that54e9c832015-11-04 21:46:01 +0100156 // Search for stylename in the parent node <object type="foo" style="foo2">
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600157 xml_attribute<>* attr = parent->first_attribute("style");
158 // If no style is found anywhere else and the node wasn't found in the object itself
159 // as a special case we will search for a style that uses the same style name as the
160 // object type, so <object type="button"> would search for a style named "button"
161 if (!attr)
162 attr = parent->first_attribute("type");
that54e9c832015-11-04 21:46:01 +0100163 // if there's no attribute type, the object type must be the element name
164 std::string stylename = attr ? attr->value() : parent->name();
165 xml_node<>* node = PageManager::FindStyle(stylename);
166 if (node) {
167 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
168 if (stylenode)
169 return stylenode;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600170 }
171 }
172 return NULL;
173}
174
thatf6ed8fc2015-02-14 20:23:16 +0100175std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue)
176{
177 if (!element)
178 return defaultvalue;
179
180 xml_attribute<>* attr = element->first_attribute(attrname);
181 return attr ? attr->value() : defaultvalue;
182}
183
184int LoadAttrInt(xml_node<>* element, const char* attrname, int defaultvalue)
185{
186 string value = LoadAttrString(element, attrname);
187 // resolve variables
188 DataManager::GetValue(value, value);
189 return value.empty() ? defaultvalue : atoi(value.c_str());
190}
191
192int LoadAttrIntScaleX(xml_node<>* element, const char* attrname, int defaultvalue)
193{
194 return scale_theme_x(LoadAttrInt(element, attrname, defaultvalue));
195}
196
197int LoadAttrIntScaleY(xml_node<>* element, const char* attrname, int defaultvalue)
198{
199 return scale_theme_y(LoadAttrInt(element, attrname, defaultvalue));
200}
201
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600202COLOR LoadAttrColor(xml_node<>* element, const char* attrname, bool* found_color, COLOR defaultvalue)
thatf6ed8fc2015-02-14 20:23:16 +0100203{
204 string value = LoadAttrString(element, attrname);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600205 *found_color = !value.empty();
thatf6ed8fc2015-02-14 20:23:16 +0100206 // resolve variables
207 DataManager::GetValue(value, value);
208 COLOR ret = defaultvalue;
209 if (ConvertStrToColor(value, &ret) == 0)
210 return ret;
211 else
212 return defaultvalue;
213}
214
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600215COLOR LoadAttrColor(xml_node<>* element, const char* attrname, COLOR defaultvalue)
216{
217 bool found_color = false;
218 return LoadAttrColor(element, attrname, &found_color, defaultvalue);
219}
220
thatf6ed8fc2015-02-14 20:23:16 +0100221FontResource* LoadAttrFont(xml_node<>* element, const char* attrname)
222{
223 std::string name = LoadAttrString(element, attrname, "");
224 if (name.empty())
225 return NULL;
226 else
that74ac6062015-03-04 22:39:34 +0100227 return PageManager::GetResources()->FindFont(name);
thatf6ed8fc2015-02-14 20:23:16 +0100228}
229
230ImageResource* LoadAttrImage(xml_node<>* element, const char* attrname)
231{
232 std::string name = LoadAttrString(element, attrname, "");
233 if (name.empty())
234 return NULL;
235 else
that74ac6062015-03-04 22:39:34 +0100236 return PageManager::GetResources()->FindImage(name);
thatf6ed8fc2015-02-14 20:23:16 +0100237}
238
239AnimationResource* LoadAttrAnimation(xml_node<>* element, const char* attrname)
240{
241 std::string name = LoadAttrString(element, attrname, "");
242 if (name.empty())
243 return NULL;
244 else
that74ac6062015-03-04 22:39:34 +0100245 return PageManager::GetResources()->FindAnimation(name);
thatf6ed8fc2015-02-14 20:23:16 +0100246}
247
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500248bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, Placement* placement /* = NULL */)
Dees_Troy51a0e822012-09-05 15:24:24 -0400249{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 if (!node)
251 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 if (node->first_attribute("x"))
thatf6ed8fc2015-02-14 20:23:16 +0100254 *x = LoadAttrIntScaleX(node, "x") + tw_x_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 if (node->first_attribute("y"))
thatf6ed8fc2015-02-14 20:23:16 +0100257 *y = LoadAttrIntScaleY(node, "y") + tw_y_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 if (w && node->first_attribute("w"))
thatf6ed8fc2015-02-14 20:23:16 +0100260 *w = LoadAttrIntScaleX(node, "w");
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 if (h && node->first_attribute("h"))
thatf6ed8fc2015-02-14 20:23:16 +0100263 *h = LoadAttrIntScaleY(node, "h");
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 if (placement && node->first_attribute("placement"))
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500266 *placement = (Placement) LoadAttrInt(node, "placement");
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400269}
270
271int ActionObject::SetActionPos(int x, int y, int w, int h)
272{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 if (x < 0 || y < 0)
274 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400275
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500276 mActionX = x;
277 mActionY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200278 if (w || h)
279 {
280 mActionW = w;
281 mActionH = h;
282 }
283 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400284}
285
thatb63e2f92015-06-27 21:35:11 +0200286Page::Page(xml_node<>* page, std::vector<xml_node<>*> *templates)
Dees_Troy51a0e822012-09-05 15:24:24 -0400287{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 mTouchStart = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 // We can memset the whole structure, because the alpha channel is ignored
291 memset(&mBackground, 0, sizeof(COLOR));
Dees_Troy51a0e822012-09-05 15:24:24 -0400292
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 // With NULL, we make a console-only display
294 if (!page)
295 {
296 mName = "console";
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 GUIConsole* element = new GUIConsole(NULL);
299 mRenders.push_back(element);
300 mActions.push_back(element);
301 return;
302 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400303
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200304 if (page->first_attribute("name"))
305 mName = page->first_attribute("name")->value();
306 else
307 {
308 LOGERR("No page name attribute found!\n");
309 return;
310 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 LOGINFO("Loading page %s\n", mName.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400313
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200314 // This is a recursive routine for template handling
thatb63e2f92015-06-27 21:35:11 +0200315 ProcessNode(page, templates, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400316}
317
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100318Page::~Page()
319{
320 for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr)
321 delete *itr;
322}
323
thatb63e2f92015-06-27 21:35:11 +0200324bool Page::ProcessNode(xml_node<>* page, std::vector<xml_node<>*> *templates, int depth)
Dees_Troy51a0e822012-09-05 15:24:24 -0400325{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200326 if (depth == 10)
327 {
328 LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n");
329 return false;
330 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400331
thatb63e2f92015-06-27 21:35:11 +0200332 for (xml_node<>* child = page->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 {
thatb63e2f92015-06-27 21:35:11 +0200334 std::string type = child->name();
335
336 if (type == "background") {
337 mBackground = LoadAttrColor(child, "color", COLOR(0,0,0,0));
338 continue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400340
thatb63e2f92015-06-27 21:35:11 +0200341 if (type == "object") {
342 // legacy format : <object type="...">
343 xml_attribute<>* attr = child->first_attribute("type");
344 type = attr ? attr->value() : "*unspecified*";
345 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400346
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 if (type == "text")
348 {
349 GUIText* element = new GUIText(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100350 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200351 mRenders.push_back(element);
352 mActions.push_back(element);
353 }
354 else if (type == "image")
355 {
356 GUIImage* element = new GUIImage(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100357 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200358 mRenders.push_back(element);
359 }
360 else if (type == "fill")
361 {
362 GUIFill* element = new GUIFill(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100363 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 mRenders.push_back(element);
365 }
366 else if (type == "action")
367 {
368 GUIAction* element = new GUIAction(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100369 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 mActions.push_back(element);
371 }
372 else if (type == "console")
373 {
374 GUIConsole* element = new GUIConsole(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100375 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 mRenders.push_back(element);
377 mActions.push_back(element);
378 }
that1964d192016-01-07 00:41:03 +0100379 else if (type == "terminal")
380 {
381 GUITerminal* element = new GUITerminal(child);
382 mObjects.push_back(element);
383 mRenders.push_back(element);
384 mActions.push_back(element);
385 mInputs.push_back(element);
Mohd Faraz77bbeb02020-08-12 12:29:42 +0000386 term = element;
that1964d192016-01-07 00:41:03 +0100387 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200388 else if (type == "button")
389 {
390 GUIButton* element = new GUIButton(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100391 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 mRenders.push_back(element);
393 mActions.push_back(element);
394 }
395 else if (type == "checkbox")
396 {
397 GUICheckbox* element = new GUICheckbox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100398 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200399 mRenders.push_back(element);
400 mActions.push_back(element);
401 }
402 else if (type == "fileselector")
403 {
404 GUIFileSelector* element = new GUIFileSelector(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100405 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200406 mRenders.push_back(element);
407 mActions.push_back(element);
408 }
409 else if (type == "animation")
410 {
411 GUIAnimation* element = new GUIAnimation(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100412 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200413 mRenders.push_back(element);
414 }
415 else if (type == "progressbar")
416 {
417 GUIProgressBar* element = new GUIProgressBar(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100418 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200419 mRenders.push_back(element);
420 mActions.push_back(element);
421 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 else if (type == "slider")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 {
424 GUISlider* element = new GUISlider(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100425 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200426 mRenders.push_back(element);
427 mActions.push_back(element);
428 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200429 else if (type == "slidervalue")
430 {
431 GUISliderValue *element = new GUISliderValue(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100432 mObjects.push_back(element);
Vojtech Bocek85932342013-04-01 22:11:33 +0200433 mRenders.push_back(element);
434 mActions.push_back(element);
435 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400436 else if (type == "listbox")
437 {
438 GUIListBox* element = new GUIListBox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100439 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 mRenders.push_back(element);
441 mActions.push_back(element);
442 }
443 else if (type == "keyboard")
444 {
445 GUIKeyboard* element = new GUIKeyboard(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100446 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 mRenders.push_back(element);
448 mActions.push_back(element);
449 }
450 else if (type == "input")
451 {
452 GUIInput* element = new GUIInput(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100453 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 mRenders.push_back(element);
455 mActions.push_back(element);
456 mInputs.push_back(element);
457 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 else if (type == "partitionlist")
459 {
460 GUIPartitionList* element = new GUIPartitionList(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100461 mObjects.push_back(element);
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 mRenders.push_back(element);
463 mActions.push_back(element);
464 }
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100465 else if (type == "patternpassword")
466 {
467 GUIPatternPassword* element = new GUIPatternPassword(child);
468 mObjects.push_back(element);
469 mRenders.push_back(element);
470 mActions.push_back(element);
471 }
Ethan Yonker44925ad2015-07-22 12:33:59 -0500472 else if (type == "textbox")
473 {
474 GUITextBox* element = new GUITextBox(child);
475 mObjects.push_back(element);
476 mRenders.push_back(element);
477 mActions.push_back(element);
478 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200479 else if (type == "template")
480 {
481 if (!templates || !child->first_attribute("name"))
482 {
483 LOGERR("Invalid template request.\n");
484 }
485 else
486 {
487 std::string name = child->first_attribute("name")->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500488 xml_node<>* node;
489 bool node_found = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400490
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200491 // We need to find the correct template
Ethan Yonker780cd392014-07-21 15:24:39 -0500492 for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) {
493 node = (*itr)->first_node("template");
Dees_Troy51a0e822012-09-05 15:24:24 -0400494
Ethan Yonker780cd392014-07-21 15:24:39 -0500495 while (node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200496 {
Ethan Yonker780cd392014-07-21 15:24:39 -0500497 if (!node->first_attribute("name"))
498 continue;
499
500 if (name == node->first_attribute("name")->value())
501 {
502 if (!ProcessNode(node, templates, depth + 1))
503 return false;
504 else {
505 node_found = true;
506 break;
507 }
508 }
509 if (node_found)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200510 break;
Ethan Yonker780cd392014-07-21 15:24:39 -0500511 node = node->next_sibling("template");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200512 }
thatb63e2f92015-06-27 21:35:11 +0200513 // [check] why is there no if (node_found) here too?
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200514 }
515 }
516 }
517 else
518 {
thatb63e2f92015-06-27 21:35:11 +0200519 LOGERR("Unknown object type: %s.\n", type.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200520 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200521 }
522 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400523}
524
525int Page::Render(void)
526{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200527 // Render background
528 gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha);
529 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Dees_Troy51a0e822012-09-05 15:24:24 -0400530
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200531 // Render remaining objects
532 std::vector<RenderObject*>::iterator iter;
533 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
534 {
535 if ((*iter)->Render())
536 LOGERR("A render request has failed.\n");
537 }
538 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400539}
540
541int Page::Update(void)
542{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200543 int retCode = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400544
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200545 std::vector<RenderObject*>::iterator iter;
546 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
547 {
548 int ret = (*iter)->Update();
549 if (ret < 0)
550 LOGERR("An update request has failed.\n");
551 else if (ret > retCode)
552 retCode = ret;
553 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400554
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200555 return retCode;
Dees_Troy51a0e822012-09-05 15:24:24 -0400556}
557
558int Page::NotifyTouch(TOUCH_STATE state, int x, int y)
559{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200560 // By default, return 1 to ignore further touches if nobody is listening
561 int ret = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400562
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200563 // Don't try to handle a lack of handlers
564 if (mActions.size() == 0)
565 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400566
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200567 // We record mTouchStart so we can pass all the touch stream to the same handler
568 if (state == TOUCH_START)
569 {
570 std::vector<ActionObject*>::reverse_iterator iter;
571 // We work backwards, from top-most element to bottom-most element
572 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
573 {
574 if ((*iter)->IsInRegion(x, y))
575 {
576 mTouchStart = (*iter);
577 ret = mTouchStart->NotifyTouch(state, x, y);
578 if (ret >= 0)
579 break;
580 mTouchStart = NULL;
581 }
582 }
583 }
584 else if (state == TOUCH_RELEASE && mTouchStart != NULL)
585 {
586 ret = mTouchStart->NotifyTouch(state, x, y);
587 mTouchStart = NULL;
588 }
589 else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL)
590 {
591 ret = mTouchStart->NotifyTouch(state, x, y);
592 }
593 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400594}
595
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100596int Page::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400597{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200598 std::vector<ActionObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400599
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100600 int ret = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 // We work backwards, from top-most element to bottom-most element
602 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
603 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100604 ret = (*iter)->NotifyKey(key, down);
that8834a0f2016-01-05 23:29:30 +0100605 if (ret == 0)
606 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100607 if (ret < 0) {
608 LOGERR("An action handler has returned an error\n");
609 ret = 1;
610 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200611 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100612 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400613}
614
that8834a0f2016-01-05 23:29:30 +0100615int Page::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -0400616{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200617 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400618
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200619 // We work backwards, from top-most element to bottom-most element
620 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
621 {
that8834a0f2016-01-05 23:29:30 +0100622 int ret = (*iter)->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200623 if (ret == 0)
624 return 0;
625 else if (ret < 0)
that6a894592016-03-13 17:51:28 +0100626 LOGERR("A char input handler has returned an error\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200627 }
628 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400629}
630
631int Page::SetKeyBoardFocus(int inFocus)
632{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200633 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400634
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200635 // We work backwards, from top-most element to bottom-most element
636 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
637 {
638 int ret = (*iter)->SetInputFocus(inFocus);
639 if (ret == 0)
640 return 0;
641 else if (ret < 0)
that6a894592016-03-13 17:51:28 +0100642 LOGERR("An input focus handler has returned an error\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200643 }
644 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400645}
646
647void Page::SetPageFocus(int inFocus)
648{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200649 // Render remaining objects
650 std::vector<RenderObject*>::iterator iter;
651 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
652 (*iter)->SetPageFocus(inFocus);
653
654 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400655}
656
657int Page::NotifyVarChange(std::string varName, std::string value)
658{
Vojtech Bocek07220562014-02-08 02:05:33 +0100659 std::vector<GUIObject*>::iterator iter;
660 for (iter = mObjects.begin(); iter != mObjects.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200661 {
662 if ((*iter)->NotifyVarChange(varName, value))
663 LOGERR("An action handler errored on NotifyVarChange.\n");
664 }
665 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400666}
667
that6a894592016-03-13 17:51:28 +0100668
669// transient data for loading themes
670struct LoadingContext
671{
bigbiff673c7ae2020-12-02 19:44:56 -0500672 ZipArchiveHandle zip; // zip to load theme from, or NULL for the stock theme
that6a894592016-03-13 17:51:28 +0100673 std::set<std::string> filenames; // to detect cyclic includes
674 std::string basepath; // if zip is NULL, base path to load includes from with trailing slash, otherwise empty
675 std::vector<xml_document<>*> xmldocs; // all loaded xml docs
676 std::vector<char*> xmlbuffers; // text buffers with xml content
677 std::vector<xml_node<>*> styles; // refer to <styles> nodes inside xmldocs
678 std::vector<xml_node<>*> templates; // refer to <templates> nodes inside xmldocs
679
680 LoadingContext()
681 {
682 zip = NULL;
683 }
684
685 ~LoadingContext()
686 {
687 // free all xml buffers
688 for (std::vector<char*>::iterator it = xmlbuffers.begin(); it != xmlbuffers.end(); ++it)
689 free(*it);
690 }
691
692};
693
694// for FindStyle
695LoadingContext* PageManager::currentLoadingContext = NULL;
696
697
698PageSet::PageSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400699{
that74ac6062015-03-04 22:39:34 +0100700 mResources = new ResourceManager;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200701 mCurrentPage = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400702
that6a894592016-03-13 17:51:28 +0100703 set_scale_values(1, 1); // Reset any previous scaling values
Dees_Troy51a0e822012-09-05 15:24:24 -0400704}
705
706PageSet::~PageSet()
707{
Ethan Yonker1c273312015-03-16 12:18:56 -0500708 mOverlays.clear();
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100709 for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
710 delete *itr;
711
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200712 delete mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400713}
714
that6a894592016-03-13 17:51:28 +0100715int PageSet::Load(LoadingContext& ctx, const std::string& filename)
716{
717 bool isMain = ctx.xmlbuffers.empty(); // if we have no files yet, remember that this is the main XML file
718
719 if (!ctx.filenames.insert(filename).second)
720 // ignore already loaded files to prevent crash with cyclic includes
721 return 0;
722
723 // load XML into buffer
724 char* xmlbuffer = PageManager::LoadFileToBuffer(filename, ctx.zip);
725 if (!xmlbuffer)
726 return -1; // error already displayed by LoadFileToBuffer
727 ctx.xmlbuffers.push_back(xmlbuffer);
728
729 // parse XML
730 xml_document<>* doc = new xml_document<>();
731 doc->parse<0>(xmlbuffer);
732 ctx.xmldocs.push_back(doc);
733
734 xml_node<>* root = doc->first_node("recovery");
735 if (!root)
736 root = doc->first_node("install");
737 if (!root) {
738 LOGERR("Unknown root element in %s\n", filename.c_str());
739 return -1;
740 }
741
742 if (isMain) {
743 int rc = LoadDetails(ctx, root);
744 if (rc != 0)
745 return rc;
746 }
747
748 LOGINFO("Loading resources...\n");
749 xml_node<>* child = root->first_node("resources");
750 if (child)
751 mResources->LoadResources(child, ctx.zip, "theme");
752
753 LOGINFO("Loading variables...\n");
754 child = root->first_node("variables");
755 if (child)
756 LoadVariables(child);
757
758 LOGINFO("Loading mouse cursor...\n");
759 child = root->first_node("mousecursor");
760 if (child)
761 PageManager::LoadCursorData(child);
762
763 LOGINFO("Loading pages...\n");
764 child = root->first_node("templates");
765 if (child)
766 ctx.templates.push_back(child);
767
768 child = root->first_node("styles");
769 if (child)
770 ctx.styles.push_back(child);
771
772 // Load pages
773 child = root->first_node("pages");
774 if (child) {
775 if (LoadPages(ctx, child)) {
776 LOGERR("PageSet::Load returning -1\n");
777 return -1;
778 }
779 }
780
781 // process includes recursively
782 child = root->first_node("include");
783 if (child) {
784 xml_node<>* include = child->first_node("xmlfile");
785 while (include != NULL) {
786 xml_attribute<>* attr = include->first_attribute("name");
787 if (!attr) {
788 LOGERR("Skipping include/xmlfile with no name\n");
789 continue;
790 }
791
792 string filename = ctx.basepath + attr->value();
793 LOGINFO("Including file: %s...\n", filename.c_str());
794 int rc = Load(ctx, filename);
795 if (rc != 0)
796 return rc;
797
798 include = include->next_sibling("xmlfile");
799 }
800 }
801
802 return 0;
803}
804
805void PageSet::MakeEmergencyConsoleIfNeeded()
806{
807 if (mPages.empty()) {
808 mCurrentPage = new Page(NULL, NULL); // fallback console page
809 // TODO: since removal of non-TTF fonts, the emergency console doesn't work without a font, which might be missing too
810 mPages.push_back(mCurrentPage);
811 }
812}
813
bigbiff673c7ae2020-12-02 19:44:56 -0500814int PageSet::LoadLanguage(char* languageFile, ZipArchiveHandle package)
Ethan Yonker74db1572015-10-28 12:44:49 -0500815{
816 xml_document<> lang;
817 xml_node<>* parent;
818 xml_node<>* child;
819 std::string resource_source;
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600820 int ret = 0;
Ethan Yonker74db1572015-10-28 12:44:49 -0500821
822 if (languageFile) {
823 printf("parsing languageFile\n");
824 lang.parse<0>(languageFile);
825 printf("parsing languageFile done\n");
826 } else {
827 return -1;
828 }
829
830 parent = lang.first_node("language");
831 if (!parent) {
832 LOGERR("Unable to locate language node in language file.\n");
833 lang.clear();
834 return -1;
835 }
836
837 child = parent->first_node("display");
838 if (child) {
839 DataManager::SetValue("tw_language_display", child->value());
840 resource_source = child->value();
841 } else {
842 LOGERR("language file does not have a display value set\n");
843 DataManager::SetValue("tw_language_display", "Not Set");
844 resource_source = languageFile;
845 }
846
847 child = parent->first_node("resources");
848 if (child)
849 mResources->LoadResources(child, package, resource_source);
850 else
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600851 ret = -1;
852 DataManager::SetValue("tw_backup_name", gui_lookup("auto_generate", "(Auto Generate)"));
Ethan Yonker74db1572015-10-28 12:44:49 -0500853 lang.clear();
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600854 return ret;
Ethan Yonker74db1572015-10-28 12:44:49 -0500855}
856
that6a894592016-03-13 17:51:28 +0100857int PageSet::LoadDetails(LoadingContext& ctx, xml_node<>* root)
Dees_Troy51a0e822012-09-05 15:24:24 -0400858{
that6a894592016-03-13 17:51:28 +0100859 xml_node<>* child = root->first_node("details");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600860 if (child) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600861 int theme_ver = 0;
862 xml_node<>* themeversion = child->first_node("themeversion");
863 if (themeversion && themeversion->value()) {
864 theme_ver = atoi(themeversion->value());
865 } else {
866 LOGINFO("No themeversion in theme.\n");
867 }
868 if (theme_ver != TW_THEME_VERSION) {
869 LOGINFO("theme version from xml: %i, expected %i\n", theme_ver, TW_THEME_VERSION);
that6a894592016-03-13 17:51:28 +0100870 if (ctx.zip) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600871 gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme.");
Ethan Yonker8e5692f2016-01-21 11:21:06 -0600872 return TW_THEME_VER_ERR;
Ethan Yonker1308d532016-01-14 22:21:49 -0600873 } else {
874 gui_print_color("warning", "Stock theme version does not match TWRP version.\n");
875 }
876 }
Ethan Yonker63e414f2015-02-06 15:44:39 -0600877 xml_node<>* resolution = child->first_node("resolution");
878 if (resolution) {
Ethan Yonker1308d532016-01-14 22:21:49 -0600879 LOGINFO("Checking resolution...\n");
Ethan Yonker63e414f2015-02-06 15:44:39 -0600880 xml_attribute<>* width_attr = resolution->first_attribute("width");
881 xml_attribute<>* height_attr = resolution->first_attribute("height");
882 xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
883 if (width_attr && height_attr && !noscale_attr) {
884 int width = atoi(width_attr->value());
885 int height = atoi(height_attr->value());
886 int offx = 0, offy = 0;
887#ifdef TW_ROUND_SCREEN
888 xml_node<>* roundscreen = child->first_node("roundscreen");
889 if (roundscreen) {
890 LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n");
891 xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x");
892 xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y");
893 if (offx_attr) {
894 offx = atoi(offx_attr->value());
895 }
896 if (offy_attr) {
897 offy = atoi(offy_attr->value());
898 }
899 }
900#endif
901 if (width != 0 && height != 0) {
James Christopher Adduonodcd1e442016-11-06 13:17:34 -0500902 float scale_w = (((float)gr_fb_width() + (float)tw_w_offset) - ((float)offx * 2.0)) / (float)width;
903 float scale_h = (((float)gr_fb_height() + (float)tw_h_offset) - ((float)offy * 2.0)) / (float)height;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600904#ifdef TW_ROUND_SCREEN
James Christopher Adduonodcd1e442016-11-06 13:17:34 -0500905 float scale_off_w = ((float)gr_fb_width() + (float)tw_w_offset) / (float)width;
906 float scale_off_h = ((float)gr_fb_height() + (float)tw_h_offset) / (float)height;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600907 tw_x_offset = offx * scale_off_w;
908 tw_y_offset = offy * scale_off_h;
909#endif
910 if (scale_w != 1 || scale_h != 1) {
James Christopher Adduonodcd1e442016-11-06 13:17:34 -0500911 LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i w: %i h: %i\n",
912 scale_w, scale_h, tw_x_offset, tw_y_offset, tw_w_offset, tw_h_offset);
Ethan Yonker63e414f2015-02-06 15:44:39 -0600913 set_scale_values(scale_w, scale_h);
914 }
915 }
916 } else {
917 LOGINFO("XML does not contain width and height, no scaling will be applied\n");
918 }
919 } else {
920 LOGINFO("XML contains no resolution tag, no scaling will be applied.\n");
921 }
922 } else {
923 LOGINFO("XML contains no details tag, no scaling will be applied.\n");
924 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500925
Ethan Yonker780cd392014-07-21 15:24:39 -0500926 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400927}
928
929int PageSet::SetPage(std::string page)
930{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200931 Page* tmp = FindPage(page);
932 if (tmp)
933 {
934 if (mCurrentPage) mCurrentPage->SetPageFocus(0);
935 mCurrentPage = tmp;
936 mCurrentPage->SetPageFocus(1);
937 mCurrentPage->NotifyVarChange("", "");
938 return 0;
939 }
940 else
941 {
942 LOGERR("Unable to locate page (%s)\n", page.c_str());
943 }
944 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400945}
946
947int PageSet::SetOverlay(Page* page)
948{
Ethan Yonker1c273312015-03-16 12:18:56 -0500949 if (page) {
950 if (mOverlays.size() >= 10) {
951 LOGERR("Too many overlays requested, max is 10.\n");
952 return -1;
953 }
Matt Mowerd411f8d2015-04-09 16:04:12 -0500954
955 std::vector<Page*>::iterator iter;
956 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
957 if ((*iter)->GetName() == page->GetName()) {
958 mOverlays.erase(iter);
959 // SetOverlay() is (and should stay) the only function which
960 // adds to mOverlays. Then, each page can appear at most once.
961 break;
962 }
963 }
964
Ethan Yonker1c273312015-03-16 12:18:56 -0500965 page->SetPageFocus(1);
966 page->NotifyVarChange("", "");
967
968 if (!mOverlays.empty())
969 mOverlays.back()->SetPageFocus(0);
970
971 mOverlays.push_back(page);
972 } else {
973 if (!mOverlays.empty()) {
974 mOverlays.back()->SetPageFocus(0);
975 mOverlays.pop_back();
976 if (!mOverlays.empty())
977 mOverlays.back()->SetPageFocus(1);
978 else if (mCurrentPage)
979 mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
980 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200981 }
982 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400983}
984
that74ac6062015-03-04 22:39:34 +0100985const ResourceManager* PageSet::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -0400986{
that74ac6062015-03-04 22:39:34 +0100987 return mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400988}
989
990Page* PageSet::FindPage(std::string name)
991{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200992 std::vector<Page*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400993
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200994 for (iter = mPages.begin(); iter != mPages.end(); iter++)
995 {
996 if (name == (*iter)->GetName())
997 return (*iter);
998 }
999 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001000}
1001
1002int PageSet::LoadVariables(xml_node<>* vars)
1003{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001004 xml_node<>* child;
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001005 xml_attribute<> *name, *value, *persist;
1006 int p;
Dees_Troy51a0e822012-09-05 15:24:24 -04001007
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001008 child = vars->first_node("variable");
1009 while (child)
1010 {
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001011 name = child->first_attribute("name");
1012 value = child->first_attribute("value");
1013 persist = child->first_attribute("persist");
Matt Mowera8a89d12016-12-30 18:10:37 -06001014 if (name && value)
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001015 {
Ethan Yonker751a85e2014-12-12 16:59:10 -06001016 if (strcmp(name->value(), "tw_x_offset") == 0) {
1017 tw_x_offset = atoi(value->value());
1018 child = child->next_sibling("variable");
1019 continue;
1020 }
1021 if (strcmp(name->value(), "tw_y_offset") == 0) {
1022 tw_y_offset = atoi(value->value());
1023 child = child->next_sibling("variable");
1024 continue;
1025 }
James Christopher Adduonodcd1e442016-11-06 13:17:34 -05001026 if (strcmp(name->value(), "tw_w_offset") == 0) {
1027 tw_w_offset = atoi(value->value());
1028 child = child->next_sibling("variable");
1029 continue;
1030 }
1031 if (strcmp(name->value(), "tw_h_offset") == 0) {
1032 tw_h_offset = atoi(value->value());
1033 child = child->next_sibling("variable");
1034 continue;
1035 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001036 p = persist ? atoi(persist->value()) : 0;
Ethan Yonker96acb3d2014-08-05 09:20:30 -05001037 string temp = value->value();
1038 string valstr = gui_parse_text(temp);
1039
1040 if (valstr.find("+") != string::npos) {
1041 string val1str = valstr;
1042 val1str = val1str.substr(0, val1str.find('+'));
1043 string val2str = valstr;
1044 val2str = val2str.substr(val2str.find('+') + 1, string::npos);
1045 int val1 = atoi(val1str.c_str());
1046 int val2 = atoi(val2str.c_str());
1047 int val = val1 + val2;
1048
1049 DataManager::SetValue(name->value(), val, p);
1050 } else if (valstr.find("-") != string::npos) {
1051 string val1str = valstr;
1052 val1str = val1str.substr(0, val1str.find('-'));
1053 string val2str = valstr;
1054 val2str = val2str.substr(val2str.find('-') + 1, string::npos);
1055 int val1 = atoi(val1str.c_str());
1056 int val2 = atoi(val2str.c_str());
1057 int val = val1 - val2;
1058
1059 DataManager::SetValue(name->value(), val, p);
1060 } else {
1061 DataManager::SetValue(name->value(), valstr, p);
1062 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001063 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001064
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 child = child->next_sibling("variable");
1066 }
1067 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001068}
1069
that6a894592016-03-13 17:51:28 +01001070int PageSet::LoadPages(LoadingContext& ctx, xml_node<>* pages)
Dees_Troy51a0e822012-09-05 15:24:24 -04001071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001072 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -04001073
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001074 if (!pages)
1075 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001076
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001077 child = pages->first_node("page");
1078 while (child != NULL)
1079 {
that6a894592016-03-13 17:51:28 +01001080 Page* page = new Page(child, &ctx.templates);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001081 if (page->GetName().empty())
1082 {
1083 LOGERR("Unable to process load page\n");
1084 delete page;
1085 }
1086 else
1087 {
1088 mPages.push_back(page);
1089 }
1090 child = child->next_sibling("page");
1091 }
1092 if (mPages.size() > 0)
1093 return 0;
1094 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001095}
1096
1097int PageSet::IsCurrentPage(Page* page)
1098{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001099 return ((mCurrentPage && mCurrentPage == page) ? 1 : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001100}
1101
that10ae24f2015-12-26 20:53:51 +01001102std::string PageSet::GetCurrentPage() const
1103{
1104 return mCurrentPage ? mCurrentPage->GetName() : "";
1105}
1106
Dees_Troy51a0e822012-09-05 15:24:24 -04001107int PageSet::Render(void)
1108{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001109 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001111 ret = (mCurrentPage ? mCurrentPage->Render() : -1);
1112 if (ret < 0)
1113 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001114
1115 std::vector<Page*>::iterator iter;
1116
1117 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1118 ret = ((*iter) ? (*iter)->Render() : -1);
1119 if (ret < 0)
1120 return ret;
1121 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001122 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001123}
1124
1125int PageSet::Update(void)
1126{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001127 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001128
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001129 ret = (mCurrentPage ? mCurrentPage->Update() : -1);
1130 if (ret < 0 || ret > 1)
1131 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001132
1133 std::vector<Page*>::iterator iter;
1134
1135 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1136 ret = ((*iter) ? (*iter)->Update() : -1);
1137 if (ret < 0)
1138 return ret;
1139 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001140 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001141}
1142
1143int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
1144{
Ethan Yonker1c273312015-03-16 12:18:56 -05001145 if (!mOverlays.empty())
1146 return mOverlays.back()->NotifyTouch(state, x, y);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001147
1148 return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001149}
1150
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001151int PageSet::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001152{
Ethan Yonker1c273312015-03-16 12:18:56 -05001153 if (!mOverlays.empty())
1154 return mOverlays.back()->NotifyKey(key, down);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001155
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001156 return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001157}
1158
that8834a0f2016-01-05 23:29:30 +01001159int PageSet::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001160{
Ethan Yonker1c273312015-03-16 12:18:56 -05001161 if (!mOverlays.empty())
that8834a0f2016-01-05 23:29:30 +01001162 return mOverlays.back()->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001163
that8834a0f2016-01-05 23:29:30 +01001164 return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001165}
1166
1167int PageSet::SetKeyBoardFocus(int inFocus)
1168{
Ethan Yonker1c273312015-03-16 12:18:56 -05001169 if (!mOverlays.empty())
1170 return mOverlays.back()->SetKeyBoardFocus(inFocus);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001171
1172 return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001173}
1174
1175int PageSet::NotifyVarChange(std::string varName, std::string value)
1176{
Ethan Yonker1c273312015-03-16 12:18:56 -05001177 std::vector<Page*>::iterator iter;
1178
1179 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
1180 (*iter)->NotifyVarChange(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001181
1182 return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001183}
1184
Ethan Yonker74db1572015-10-28 12:44:49 -05001185void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1186{
1187 mResources->AddStringResource(resource_source, resource_name, value);
1188}
1189
bigbiff673c7ae2020-12-02 19:44:56 -05001190char* PageManager::LoadFileToBuffer(std::string filename, ZipArchiveHandle package) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001191 size_t len;
1192 char* buffer = NULL;
1193
Mohd Faraz16c32042021-04-17 15:14:25 +02001194 if (!package) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001195 // We can try to load the XML directly...
1196 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str());
1197 struct stat st;
Matt Mowera8a89d12016-12-30 18:10:37 -06001198 if (stat(filename.c_str(),&st) != 0) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001199 // This isn't always an error, sometimes we request files that don't exist.
1200 return NULL;
1201 }
1202
1203 len = (size_t)st.st_size;
1204
1205 buffer = (char*) malloc(len + 1);
1206 if (!buffer) {
1207 LOGERR("PageManager::LoadFileToBuffer failed to malloc\n");
1208 return NULL;
1209 }
1210
1211 int fd = open(filename.c_str(), O_RDONLY);
1212 if (fd == -1) {
1213 LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno));
1214 free(buffer);
1215 return NULL;
1216 }
1217
1218 if (read(fd, buffer, len) < 0) {
1219 LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno));
1220 free(buffer);
1221 close(fd);
1222 return NULL;
1223 }
1224 close(fd);
1225 } else {
1226 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
bigbiff673c7ae2020-12-02 19:44:56 -05001227 ZipEntry binary_entry;
1228 if (FindEntry(package, filename, &binary_entry) == 0) {
1229 // if (!package->EntryExists(filename)) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001230 LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
1231 return NULL;
1232 }
1233
1234 // Allocate the buffer for the file
bigbiff673c7ae2020-12-02 19:44:56 -05001235 len = binary_entry.uncompressed_length;
1236 // len = package->GetUncompressedSize(filename);
Ethan Yonker561c58d2015-10-05 08:48:22 -05001237 buffer = (char*) malloc(len + 1);
1238 if (!buffer)
1239 return NULL;
1240
bigbiff673c7ae2020-12-02 19:44:56 -05001241 int32_t err =
1242 ExtractToMemory(package, &binary_entry, reinterpret_cast<uint8_t*>(buffer), len);
1243 if (err != 0) {
1244 // if (!package->ExtractToBuffer(filename, (unsigned char*) buffer)) {
Ethan Yonker561c58d2015-10-05 08:48:22 -05001245 LOGERR("Unable to extract '%s'\n", filename.c_str());
1246 free(buffer);
1247 return NULL;
1248 }
1249 }
1250 // NULL-terminate the string
1251 buffer[len] = 0x00;
1252 return buffer;
1253}
1254
Ethan Yonker74db1572015-10-28 12:44:49 -05001255void PageManager::LoadLanguageListDir(string dir) {
1256 if (!TWFunc::Path_Exists(dir)) {
1257 LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str());
1258 return;
1259 }
1260
1261 DIR *d = opendir(dir.c_str());
1262 struct dirent *p;
1263
1264 if (d == NULL) {
1265 LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno));
1266 return;
1267 }
1268
1269 while ((p = readdir(d))) {
1270 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5)
1271 continue;
1272
1273 string file = p->d_name;
1274 if (file.substr(strlen(p->d_name) - 4) != ".xml")
1275 continue;
1276 string path = dir + p->d_name;
1277 string file_no_extn = file.substr(0, strlen(p->d_name) - 4);
1278 struct language_struct language_entry;
1279 language_entry.filename = file_no_extn;
1280 char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL);
1281 if (xmlFile == NULL) {
1282 LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str());
1283 continue;
1284 }
1285 xml_document<> *doc = new xml_document<>();
1286 doc->parse<0>(xmlFile);
1287
1288 xml_node<>* parent = doc->first_node("language");
1289 if (!parent) {
1290 LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str());
1291 } else {
1292 xml_node<>* child = parent->first_node("display");
1293 if (child) {
1294 language_entry.displayvalue = child->value();
1295 } else {
1296 LOGERR("No display value for '%s'\n", language_entry.filename.c_str());
1297 language_entry.displayvalue = language_entry.filename;
1298 }
1299 Language_List.push_back(language_entry);
1300 }
1301 doc->clear();
1302 delete doc;
1303 free(xmlFile);
1304 }
1305 closedir(d);
1306}
1307
bigbiff673c7ae2020-12-02 19:44:56 -05001308void PageManager::LoadLanguageList(ZipArchiveHandle package) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001309 Language_List.clear();
1310 if (TWFunc::Path_Exists(TWRES "customlanguages"))
1311 TWFunc::removeDir(TWRES "customlanguages", true);
1312 if (package) {
1313 TWFunc::Recursive_Mkdir(TWRES "customlanguages");
bigbiff673c7ae2020-12-02 19:44:56 -05001314 ExtractPackageRecursive(package, "", TWRES "customlanguages", nullptr, nullptr);
1315
1316 // package->ExtractRecursive("languages", TWRES "customlanguages/");
Ethan Yonker74db1572015-10-28 12:44:49 -05001317 LoadLanguageListDir(TWRES "customlanguages/");
1318 } else {
1319 LoadLanguageListDir(TWRES "languages/");
1320 }
Xuefercac6ace2016-02-01 02:28:55 +08001321
1322 std::sort(Language_List.begin(), Language_List.end());
Ethan Yonker74db1572015-10-28 12:44:49 -05001323}
1324
1325void PageManager::LoadLanguage(string filename) {
1326 string actual_filename;
1327 if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml"))
1328 actual_filename = TWRES "customlanguages/" + filename + ".xml";
1329 else
1330 actual_filename = TWRES "languages/" + filename + ".xml";
1331 char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL);
1332 if (xmlFile == NULL)
1333 LOGERR("Unable to load '%s'\n", actual_filename.c_str());
1334 else {
1335 mCurrentSet->LoadLanguage(xmlFile, NULL);
1336 free(xmlFile);
1337 }
1338 PartitionManager.Translate_Partition_Display_Names();
1339}
1340
Dees_Troy51a0e822012-09-05 15:24:24 -04001341int PageManager::LoadPackage(std::string name, std::string package, std::string startpage)
1342{
that6a894592016-03-13 17:51:28 +01001343 std::string mainxmlfilename = package;
Ethan Yonker74db1572015-10-28 12:44:49 -05001344 char* languageFile = NULL;
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001345 char* baseLanguageFile = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001346 PageSet* pageSet = NULL;
1347 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001348
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001349 mReloadTheme = false;
1350 mStartPage = startpage;
1351
that6a894592016-03-13 17:51:28 +01001352 // init the loading context
1353 LoadingContext ctx;
1354
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001355 // Open the XML file
1356 LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str());
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001357 if (package.size() > 4 && package.substr(package.size() - 4) != ".zip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001358 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001359 LOGINFO("Load XML directly\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001360 tw_x_offset = TW_X_OFFSET;
1361 tw_y_offset = TW_Y_OFFSET;
James Christopher Adduonodcd1e442016-11-06 13:17:34 -05001362 tw_w_offset = TW_W_OFFSET;
1363 tw_h_offset = TW_H_OFFSET;
Ethan Yonker4f74d142016-03-31 08:10:37 -05001364 if (name != "splash") {
1365 LoadLanguageList(NULL);
1366 languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
1367 }
that6a894592016-03-13 17:51:28 +01001368 ctx.basepath = TWRES;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001369 }
1370 else
1371 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001372 LOGINFO("Loading zip theme\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001373 tw_x_offset = 0;
1374 tw_y_offset = 0;
James Christopher Adduonodcd1e442016-11-06 13:17:34 -05001375 tw_w_offset = 0;
1376 tw_h_offset = 0;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001377 if (!TWFunc::Path_Exists(package))
1378 return -1;
bigbiff673c7ae2020-12-02 19:44:56 -05001379
1380 ZipArchiveHandle Zip;
1381 int err = OpenArchive(package.c_str(), &Zip);
1382
1383 if (err != 0)
1384 return -1;
1385
1386 ctx.zip = Zip;
that6a894592016-03-13 17:51:28 +01001387 mainxmlfilename = "ui.xml";
1388 LoadLanguageList(ctx.zip);
1389 languageFile = LoadFileToBuffer("languages/en.xml", ctx.zip);
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001390 baseLanguageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001391 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001392
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001393 // Before loading, mCurrentSet must be the loading package so we can find resources
1394 pageSet = mCurrentSet;
that6a894592016-03-13 17:51:28 +01001395 mCurrentSet = new PageSet();
1396
1397 if (baseLanguageFile) {
1398 mCurrentSet->LoadLanguage(baseLanguageFile, NULL);
1399 free(baseLanguageFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001400 }
that6a894592016-03-13 17:51:28 +01001401
1402 if (languageFile) {
1403 mCurrentSet->LoadLanguage(languageFile, ctx.zip);
1404 free(languageFile);
1405 }
1406
1407 // Load and parse the XML and all includes
1408 currentLoadingContext = &ctx; // required to find styles
1409 ret = mCurrentSet->Load(ctx, mainxmlfilename);
1410 currentLoadingContext = NULL;
1411
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001412 if (ret == 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001413 mCurrentSet->SetPage(startpage);
1414 mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
Ethan Yonker8e5692f2016-01-21 11:21:06 -06001415 } else {
1416 if (ret != TW_THEME_VER_ERR)
1417 LOGERR("Package %s failed to load.\n", name.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001418 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001419
that6a894592016-03-13 17:51:28 +01001420 // reset to previous pageset
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001421 mCurrentSet = pageSet;
1422
that6a894592016-03-13 17:51:28 +01001423 if (ctx.zip) {
bigbiff673c7ae2020-12-02 19:44:56 -05001424 CloseArchive(ctx.zip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001425 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001426 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001427}
1428
1429PageSet* PageManager::FindPackage(std::string name)
1430{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001431 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001432
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001433 iter = mPageSets.find(name);
1434 if (iter != mPageSets.end())
1435 return (*iter).second;
1436
1437 LOGERR("Unable to locate package %s\n", name.c_str());
1438 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001439}
1440
1441PageSet* PageManager::SelectPackage(std::string name)
1442{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001443 LOGINFO("Switching packages (%s)\n", name.c_str());
1444 PageSet* tmp;
Dees_Troy51a0e822012-09-05 15:24:24 -04001445
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001446 tmp = FindPackage(name);
1447 if (tmp)
Vojtech Bocek07220562014-02-08 02:05:33 +01001448 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001449 mCurrentSet = tmp;
that6a894592016-03-13 17:51:28 +01001450 mCurrentSet->MakeEmergencyConsoleIfNeeded();
Vojtech Bocek07220562014-02-08 02:05:33 +01001451 mCurrentSet->NotifyVarChange("", "");
1452 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001453 else
1454 LOGERR("Unable to find package.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001455
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001456 return mCurrentSet;
Dees_Troy51a0e822012-09-05 15:24:24 -04001457}
1458
1459int PageManager::ReloadPackage(std::string name, std::string package)
1460{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001461 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001462
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001463 mReloadTheme = false;
1464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001465 iter = mPageSets.find(name);
1466 if (iter == mPageSets.end())
1467 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001468
Matt Mowera8a89d12016-12-30 18:10:37 -06001469 if (mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001470 mMouseCursor->ResetData(gr_fb_width(), gr_fb_height());
1471
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001472 PageSet* set = (*iter).second;
1473 mPageSets.erase(iter);
Dees_Troy51a0e822012-09-05 15:24:24 -04001474
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001475 if (LoadPackage(name, package, mStartPage) != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001476 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001477 LOGINFO("Failed to load package '%s'.\n", package.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001478 mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
1479 return -1;
1480 }
1481 if (mCurrentSet == set)
1482 SelectPackage(name);
1483 delete set;
Ethan Yonker74db1572015-10-28 12:44:49 -05001484 GUIConsole::Translate_Now();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001485 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001486}
1487
1488void PageManager::ReleasePackage(std::string name)
1489{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001490 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001491
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001492 iter = mPageSets.find(name);
1493 if (iter == mPageSets.end())
1494 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001495
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001496 PageSet* set = (*iter).second;
1497 mPageSets.erase(iter);
1498 delete set;
that235c6482016-01-24 21:59:00 +01001499 if (set == mCurrentSet)
1500 mCurrentSet = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001501 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001502}
1503
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001504int PageManager::RunReload() {
1505 int ret_val = 0;
1506 std::string theme_path;
1507
1508 if (!mReloadTheme)
1509 return 0;
1510
1511 mReloadTheme = false;
1512 theme_path = DataManager::GetSettingsStoragePath();
1513 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1514 LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
1515 ret_val = 1;
1516 }
1517
1518 theme_path += "/TWRP/theme/ui.zip";
1519 if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0)
1520 {
1521 // Loading the custom theme failed - try loading the stock theme
1522 LOGINFO("Attempting to reload stock theme...\n");
1523 if (ReloadPackage("TWRP", TWRES "ui.xml"))
1524 {
1525 LOGERR("Failed to load base packages.\n");
1526 ret_val = 1;
1527 }
1528 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 if (ret_val == 0) {
1530 if (DataManager::GetStrValue("tw_language") != "en.xml") {
1531 LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str());
1532 LoadLanguage(DataManager::GetStrValue("tw_language"));
1533 }
1534 }
1535
1536 // This makes the console re-translate
thata9dd9f02017-02-23 23:08:56 +01001537 GUIConsole::Clear_For_Retranslation();
Ethan Yonker74db1572015-10-28 12:44:49 -05001538
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001539 return ret_val;
1540}
1541
1542void PageManager::RequestReload() {
1543 mReloadTheme = true;
1544}
1545
Ethan Yonkerafde0982016-01-23 08:55:35 -06001546void PageManager::SetStartPage(const std::string& page_name) {
1547 mStartPage = page_name;
1548}
1549
Dees_Troy51a0e822012-09-05 15:24:24 -04001550int PageManager::ChangePage(std::string name)
1551{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001552 DataManager::SetValue("tw_operation_state", 0);
1553 int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1);
1554 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001555}
1556
that10ae24f2015-12-26 20:53:51 +01001557std::string PageManager::GetCurrentPage()
1558{
1559 return mCurrentSet ? mCurrentSet->GetCurrentPage() : "";
1560}
1561
Dees_Troy51a0e822012-09-05 15:24:24 -04001562int PageManager::ChangeOverlay(std::string name)
1563{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001564 if (name.empty())
1565 return mCurrentSet->SetOverlay(NULL);
1566 else
1567 {
Ethan Yonker1308d532016-01-14 22:21:49 -06001568 Page* page = mCurrentSet ? mCurrentSet->FindPage(name) : NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001569 return mCurrentSet->SetOverlay(page);
1570 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001571}
1572
that74ac6062015-03-04 22:39:34 +01001573const ResourceManager* PageManager::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001574{
that74ac6062015-03-04 22:39:34 +01001575 return (mCurrentSet ? mCurrentSet->GetResources() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001576}
1577
Dees_Troy51a0e822012-09-05 15:24:24 -04001578int PageManager::IsCurrentPage(Page* page)
1579{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001580 return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001581}
1582
1583int PageManager::Render(void)
1584{
Matt Mowera8a89d12016-12-30 18:10:37 -06001585 if (blankTimer.isScreenOff())
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001586 return 0;
1587
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001588 int res = (mCurrentSet ? mCurrentSet->Render() : -1);
Matt Mowera8a89d12016-12-30 18:10:37 -06001589 if (mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001590 mMouseCursor->Render();
1591 return res;
1592}
1593
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001594HardwareKeyboard *PageManager::GetHardwareKeyboard()
1595{
Matt Mowera8a89d12016-12-30 18:10:37 -06001596 if (!mHardwareKeyboard)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001597 mHardwareKeyboard = new HardwareKeyboard();
1598 return mHardwareKeyboard;
1599}
1600
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001601xml_node<>* PageManager::FindStyle(std::string name)
1602{
that6a894592016-03-13 17:51:28 +01001603 if (!currentLoadingContext)
1604 {
1605 LOGERR("FindStyle works only while loading a theme.\n");
1606 return NULL;
1607 }
1608
1609 for (std::vector<xml_node<>*>::iterator itr = currentLoadingContext->styles.begin(); itr != currentLoadingContext->styles.end(); itr++) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001610 xml_node<>* node = (*itr)->first_node("style");
1611
1612 while (node) {
1613 if (!node->first_attribute("name"))
1614 continue;
1615
1616 if (name == node->first_attribute("name")->value())
1617 return node;
1618 node = node->next_sibling("style");
1619 }
1620 }
1621 return NULL;
1622}
1623
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001624MouseCursor *PageManager::GetMouseCursor()
1625{
Matt Mowera8a89d12016-12-30 18:10:37 -06001626 if (!mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001627 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1628 return mMouseCursor;
1629}
1630
1631void PageManager::LoadCursorData(xml_node<>* node)
1632{
Matt Mowera8a89d12016-12-30 18:10:37 -06001633 if (!mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001634 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1635
1636 mMouseCursor->LoadData(node);
Dees_Troy51a0e822012-09-05 15:24:24 -04001637}
1638
1639int PageManager::Update(void)
1640{
Matt Mowera8a89d12016-12-30 18:10:37 -06001641 if (blankTimer.isScreenOff())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001642 return 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001643
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001644 if (RunReload())
1645 return -2;
1646
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001647 int res = (mCurrentSet ? mCurrentSet->Update() : -1);
1648
Matt Mowera8a89d12016-12-30 18:10:37 -06001649 if (mMouseCursor)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001650 {
1651 int c_res = mMouseCursor->Update();
Matt Mowera8a89d12016-12-30 18:10:37 -06001652 if (c_res > res)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001653 res = c_res;
1654 }
1655 return res;
Dees_Troy51a0e822012-09-05 15:24:24 -04001656}
1657
1658int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y)
1659{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001660 return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001661}
1662
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001663int PageManager::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001664{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001665 return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001666}
1667
that8834a0f2016-01-05 23:29:30 +01001668int PageManager::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001669{
that8834a0f2016-01-05 23:29:30 +01001670 return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001671}
1672
1673int PageManager::SetKeyBoardFocus(int inFocus)
1674{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001675 return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001676}
1677
1678int PageManager::NotifyVarChange(std::string varName, std::string value)
1679{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001680 return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001681}
1682
Ethan Yonker74db1572015-10-28 12:44:49 -05001683void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1684{
1685 if (mCurrentSet)
1686 mCurrentSet->AddStringResource(resource_source, resource_name, value);
1687}
1688
Dees_Troy51a0e822012-09-05 15:24:24 -04001689extern "C" void gui_notifyVarChange(const char *name, const char* value)
1690{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001691 if (!gGuiRunning)
1692 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001693
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001694 PageManager::NotifyVarChange(name, value);
Dees_Troy51a0e822012-09-05 15:24:24 -04001695}