blob: c097c39bdedd7fdfc34e96ce03ab337c9055dc06 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees Troy3be70a82013-10-22 14:25:12 +000018
Dees_Troya13d74f2013-03-24 08:54:55 -050019// pages.cpp - Source to manage GUI base objects
Dees_Troy51a0e822012-09-05 15:24:24 -040020
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/reboot.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/mman.h>
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34#include <stdlib.h>
Ethan Yonkera2dc2f22014-11-08 08:13:40 -060035#include "../twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050036#include "../partitions.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040037
38#include <string>
39
40extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "../minuitwrp/minui.h"
Ethan Yonkera2dc2f22014-11-08 08:13:40 -060043#include "../minzip/SysUtil.h"
44#include "../minzip/Zip.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060045#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040046}
47
48#include "rapidxml.hpp"
49#include "objects.hpp"
gordon13370d9133d2013-06-08 14:17:07 +020050#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040051
52extern int gGuiRunning;
53
Ethan Yonker74db1572015-10-28 12:44:49 -050054// From ../twrp.cpp
55extern bool datamedia;
56
57// From console.cpp
58extern size_t last_message_count;
59extern std::vector<std::string> gConsole;
60extern std::vector<std::string> gConsoleColor;
61
Dees_Troy51a0e822012-09-05 15:24:24 -040062std::map<std::string, PageSet*> PageManager::mPageSets;
63PageSet* PageManager::mCurrentSet;
64PageSet* PageManager::mBaseSet = NULL;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010065MouseCursor *PageManager::mMouseCursor = NULL;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +010066HardwareKeyboard *PageManager::mHardwareKeyboard = NULL;
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -050067bool PageManager::mReloadTheme = false;
68std::string PageManager::mStartPage = "main";
Ethan Yonker74db1572015-10-28 12:44:49 -050069std::vector<language_struct> Language_List;
Dees_Troy51a0e822012-09-05 15:24:24 -040070
Ethan Yonker751a85e2014-12-12 16:59:10 -060071int tw_x_offset = 0;
72int tw_y_offset = 0;
73
Dees_Troy51a0e822012-09-05 15:24:24 -040074// Helper routine to convert a string to a color declaration
75int ConvertStrToColor(std::string str, COLOR* color)
76{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 // Set the default, solid black
78 memset(color, 0, sizeof(COLOR));
79 color->alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 // Translate variables
82 DataManager::GetValue(str, str);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 // Look for some defaults
thatf6ed8fc2015-02-14 20:23:16 +010085 if (str == "black") return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086 else if (str == "white") { color->red = color->green = color->blue = 255; return 0; }
87 else if (str == "red") { color->red = 255; return 0; }
88 else if (str == "green") { color->green = 255; return 0; }
89 else if (str == "blue") { color->blue = 255; return 0; }
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 // At this point, we require an RGB(A) color
92 if (str[0] != '#')
93 return -1;
94
95 str.erase(0, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -040096
Dees_Troy30b962e2012-10-19 20:48:59 -040097 int result;
98 if (str.size() >= 8) {
99 // We have alpha channel
100 string alpha = str.substr(6, 2);
101 result = strtol(alpha.c_str(), NULL, 16);
102 color->alpha = result & 0x000000FF;
103 str.resize(6);
104 result = strtol(str.c_str(), NULL, 16);
105 color->red = (result >> 16) & 0x000000FF;
106 color->green = (result >> 8) & 0x000000FF;
107 color->blue = result & 0x000000FF;
108 } else {
109 result = strtol(str.c_str(), NULL, 16);
110 color->red = (result >> 16) & 0x000000FF;
111 color->green = (result >> 8) & 0x000000FF;
112 color->blue = result & 0x000000FF;
113 }
114 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115}
116
117// Helper APIs
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600118xml_node<>* FindNode(xml_node<>* parent, const char* nodename, int depth /* = 0 */)
119{
that8d46c092015-02-26 01:30:04 +0100120 if (!parent)
121 return NULL;
122
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600123 xml_node<>* child = parent->first_node(nodename);
124 if (child)
125 return child;
126
127 if (depth == 10) {
128 LOGERR("Too many style loops detected.\n");
129 return NULL;
130 }
131
132 xml_node<>* style = parent->first_node("style");
133 if (style) {
134 while (style) {
135 if (!style->first_attribute("name")) {
136 LOGERR("No name given for style.\n");
137 continue;
138 } else {
139 std::string name = style->first_attribute("name")->value();
140 xml_node<>* node = PageManager::FindStyle(name);
141
142 if (node) {
143 // We found the style that was named
144 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
145 if (stylenode)
146 return stylenode;
147 }
148 }
149 style = style->next_sibling("style");
150 }
151 } else {
that54e9c832015-11-04 21:46:01 +0100152 // Search for stylename in the parent node <object type="foo" style="foo2">
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600153 xml_attribute<>* attr = parent->first_attribute("style");
154 // If no style is found anywhere else and the node wasn't found in the object itself
155 // as a special case we will search for a style that uses the same style name as the
156 // object type, so <object type="button"> would search for a style named "button"
157 if (!attr)
158 attr = parent->first_attribute("type");
that54e9c832015-11-04 21:46:01 +0100159 // if there's no attribute type, the object type must be the element name
160 std::string stylename = attr ? attr->value() : parent->name();
161 xml_node<>* node = PageManager::FindStyle(stylename);
162 if (node) {
163 xml_node<>* stylenode = FindNode(node, nodename, depth + 1);
164 if (stylenode)
165 return stylenode;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600166 }
167 }
168 return NULL;
169}
170
thatf6ed8fc2015-02-14 20:23:16 +0100171std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue)
172{
173 if (!element)
174 return defaultvalue;
175
176 xml_attribute<>* attr = element->first_attribute(attrname);
177 return attr ? attr->value() : defaultvalue;
178}
179
180int LoadAttrInt(xml_node<>* element, const char* attrname, int defaultvalue)
181{
182 string value = LoadAttrString(element, attrname);
183 // resolve variables
184 DataManager::GetValue(value, value);
185 return value.empty() ? defaultvalue : atoi(value.c_str());
186}
187
188int LoadAttrIntScaleX(xml_node<>* element, const char* attrname, int defaultvalue)
189{
190 return scale_theme_x(LoadAttrInt(element, attrname, defaultvalue));
191}
192
193int LoadAttrIntScaleY(xml_node<>* element, const char* attrname, int defaultvalue)
194{
195 return scale_theme_y(LoadAttrInt(element, attrname, defaultvalue));
196}
197
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600198COLOR LoadAttrColor(xml_node<>* element, const char* attrname, bool* found_color, COLOR defaultvalue)
thatf6ed8fc2015-02-14 20:23:16 +0100199{
200 string value = LoadAttrString(element, attrname);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600201 *found_color = !value.empty();
thatf6ed8fc2015-02-14 20:23:16 +0100202 // resolve variables
203 DataManager::GetValue(value, value);
204 COLOR ret = defaultvalue;
205 if (ConvertStrToColor(value, &ret) == 0)
206 return ret;
207 else
208 return defaultvalue;
209}
210
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600211COLOR LoadAttrColor(xml_node<>* element, const char* attrname, COLOR defaultvalue)
212{
213 bool found_color = false;
214 return LoadAttrColor(element, attrname, &found_color, defaultvalue);
215}
216
thatf6ed8fc2015-02-14 20:23:16 +0100217FontResource* LoadAttrFont(xml_node<>* element, const char* attrname)
218{
219 std::string name = LoadAttrString(element, attrname, "");
220 if (name.empty())
221 return NULL;
222 else
that74ac6062015-03-04 22:39:34 +0100223 return PageManager::GetResources()->FindFont(name);
thatf6ed8fc2015-02-14 20:23:16 +0100224}
225
226ImageResource* LoadAttrImage(xml_node<>* element, const char* attrname)
227{
228 std::string name = LoadAttrString(element, attrname, "");
229 if (name.empty())
230 return NULL;
231 else
that74ac6062015-03-04 22:39:34 +0100232 return PageManager::GetResources()->FindImage(name);
thatf6ed8fc2015-02-14 20:23:16 +0100233}
234
235AnimationResource* LoadAttrAnimation(xml_node<>* element, const char* attrname)
236{
237 std::string name = LoadAttrString(element, attrname, "");
238 if (name.empty())
239 return NULL;
240 else
that74ac6062015-03-04 22:39:34 +0100241 return PageManager::GetResources()->FindAnimation(name);
thatf6ed8fc2015-02-14 20:23:16 +0100242}
243
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500244bool LoadPlacement(xml_node<>* node, int* x, int* y, int* w /* = NULL */, int* h /* = NULL */, Placement* placement /* = NULL */)
Dees_Troy51a0e822012-09-05 15:24:24 -0400245{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (!node)
247 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 if (node->first_attribute("x"))
thatf6ed8fc2015-02-14 20:23:16 +0100250 *x = LoadAttrIntScaleX(node, "x") + tw_x_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 if (node->first_attribute("y"))
thatf6ed8fc2015-02-14 20:23:16 +0100253 *y = LoadAttrIntScaleY(node, "y") + tw_y_offset;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (w && node->first_attribute("w"))
thatf6ed8fc2015-02-14 20:23:16 +0100256 *w = LoadAttrIntScaleX(node, "w");
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (h && node->first_attribute("h"))
thatf6ed8fc2015-02-14 20:23:16 +0100259 *h = LoadAttrIntScaleY(node, "h");
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 if (placement && node->first_attribute("placement"))
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500262 *placement = (Placement) LoadAttrInt(node, "placement");
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400265}
266
267int ActionObject::SetActionPos(int x, int y, int w, int h)
268{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 if (x < 0 || y < 0)
270 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400271
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500272 mActionX = x;
273 mActionY = y;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 if (w || h)
275 {
276 mActionW = w;
277 mActionH = h;
278 }
279 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400280}
281
thatb63e2f92015-06-27 21:35:11 +0200282Page::Page(xml_node<>* page, std::vector<xml_node<>*> *templates)
Dees_Troy51a0e822012-09-05 15:24:24 -0400283{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 mTouchStart = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400285
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 // We can memset the whole structure, because the alpha channel is ignored
287 memset(&mBackground, 0, sizeof(COLOR));
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 // With NULL, we make a console-only display
290 if (!page)
291 {
292 mName = "console";
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 GUIConsole* element = new GUIConsole(NULL);
295 mRenders.push_back(element);
296 mActions.push_back(element);
297 return;
298 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400299
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200300 if (page->first_attribute("name"))
301 mName = page->first_attribute("name")->value();
302 else
303 {
304 LOGERR("No page name attribute found!\n");
305 return;
306 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 LOGINFO("Loading page %s\n", mName.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400309
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200310 // This is a recursive routine for template handling
thatb63e2f92015-06-27 21:35:11 +0200311 ProcessNode(page, templates, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400312}
313
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100314Page::~Page()
315{
316 for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr)
317 delete *itr;
318}
319
thatb63e2f92015-06-27 21:35:11 +0200320bool Page::ProcessNode(xml_node<>* page, std::vector<xml_node<>*> *templates, int depth)
Dees_Troy51a0e822012-09-05 15:24:24 -0400321{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200322 if (depth == 10)
323 {
324 LOGERR("Page processing depth has exceeded 10. Failing out. This is likely a recursive template.\n");
325 return false;
326 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400327
thatb63e2f92015-06-27 21:35:11 +0200328 for (xml_node<>* child = page->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200329 {
thatb63e2f92015-06-27 21:35:11 +0200330 std::string type = child->name();
331
332 if (type == "background") {
333 mBackground = LoadAttrColor(child, "color", COLOR(0,0,0,0));
334 continue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
thatb63e2f92015-06-27 21:35:11 +0200337 if (type == "object") {
338 // legacy format : <object type="...">
339 xml_attribute<>* attr = child->first_attribute("type");
340 type = attr ? attr->value() : "*unspecified*";
341 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400342
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200343 if (type == "text")
344 {
345 GUIText* element = new GUIText(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100346 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 mRenders.push_back(element);
348 mActions.push_back(element);
349 }
350 else if (type == "image")
351 {
352 GUIImage* element = new GUIImage(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100353 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200354 mRenders.push_back(element);
355 }
356 else if (type == "fill")
357 {
358 GUIFill* element = new GUIFill(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100359 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 mRenders.push_back(element);
361 }
362 else if (type == "action")
363 {
364 GUIAction* element = new GUIAction(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100365 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 mActions.push_back(element);
367 }
368 else if (type == "console")
369 {
370 GUIConsole* element = new GUIConsole(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100371 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200372 mRenders.push_back(element);
373 mActions.push_back(element);
374 }
375 else if (type == "button")
376 {
377 GUIButton* element = new GUIButton(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100378 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 mRenders.push_back(element);
380 mActions.push_back(element);
381 }
382 else if (type == "checkbox")
383 {
384 GUICheckbox* element = new GUICheckbox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100385 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 mRenders.push_back(element);
387 mActions.push_back(element);
388 }
389 else if (type == "fileselector")
390 {
391 GUIFileSelector* element = new GUIFileSelector(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100392 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 mRenders.push_back(element);
394 mActions.push_back(element);
395 }
396 else if (type == "animation")
397 {
398 GUIAnimation* element = new GUIAnimation(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100399 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 mRenders.push_back(element);
401 }
402 else if (type == "progressbar")
403 {
404 GUIProgressBar* element = new GUIProgressBar(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 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 else if (type == "slider")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200410 {
411 GUISlider* element = new GUISlider(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 mActions.push_back(element);
415 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200416 else if (type == "slidervalue")
417 {
418 GUISliderValue *element = new GUISliderValue(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100419 mObjects.push_back(element);
Vojtech Bocek85932342013-04-01 22:11:33 +0200420 mRenders.push_back(element);
421 mActions.push_back(element);
422 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 else if (type == "listbox")
424 {
425 GUIListBox* element = new GUIListBox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100426 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400427 mRenders.push_back(element);
428 mActions.push_back(element);
429 }
430 else if (type == "keyboard")
431 {
432 GUIKeyboard* element = new GUIKeyboard(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100433 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400434 mRenders.push_back(element);
435 mActions.push_back(element);
436 }
437 else if (type == "input")
438 {
439 GUIInput* element = new GUIInput(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100440 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 mRenders.push_back(element);
442 mActions.push_back(element);
443 mInputs.push_back(element);
444 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500445 else if (type == "partitionlist")
446 {
447 GUIPartitionList* element = new GUIPartitionList(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100448 mObjects.push_back(element);
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 mRenders.push_back(element);
450 mActions.push_back(element);
451 }
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100452 else if (type == "patternpassword")
453 {
454 GUIPatternPassword* element = new GUIPatternPassword(child);
455 mObjects.push_back(element);
456 mRenders.push_back(element);
457 mActions.push_back(element);
458 }
Ethan Yonker44925ad2015-07-22 12:33:59 -0500459 else if (type == "textbox")
460 {
461 GUITextBox* element = new GUITextBox(child);
462 mObjects.push_back(element);
463 mRenders.push_back(element);
464 mActions.push_back(element);
465 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200466 else if (type == "template")
467 {
468 if (!templates || !child->first_attribute("name"))
469 {
470 LOGERR("Invalid template request.\n");
471 }
472 else
473 {
474 std::string name = child->first_attribute("name")->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500475 xml_node<>* node;
476 bool node_found = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400477
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 // We need to find the correct template
Ethan Yonker780cd392014-07-21 15:24:39 -0500479 for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) {
480 node = (*itr)->first_node("template");
Dees_Troy51a0e822012-09-05 15:24:24 -0400481
Ethan Yonker780cd392014-07-21 15:24:39 -0500482 while (node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200483 {
Ethan Yonker780cd392014-07-21 15:24:39 -0500484 if (!node->first_attribute("name"))
485 continue;
486
487 if (name == node->first_attribute("name")->value())
488 {
489 if (!ProcessNode(node, templates, depth + 1))
490 return false;
491 else {
492 node_found = true;
493 break;
494 }
495 }
496 if (node_found)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200497 break;
Ethan Yonker780cd392014-07-21 15:24:39 -0500498 node = node->next_sibling("template");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200499 }
thatb63e2f92015-06-27 21:35:11 +0200500 // [check] why is there no if (node_found) here too?
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200501 }
502 }
503 }
504 else
505 {
thatb63e2f92015-06-27 21:35:11 +0200506 LOGERR("Unknown object type: %s.\n", type.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200507 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508 }
509 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400510}
511
512int Page::Render(void)
513{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200514 // Render background
515 gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha);
516 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Dees_Troy51a0e822012-09-05 15:24:24 -0400517
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200518 // Render remaining objects
519 std::vector<RenderObject*>::iterator iter;
520 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
521 {
522 if ((*iter)->Render())
523 LOGERR("A render request has failed.\n");
524 }
525 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400526}
527
528int Page::Update(void)
529{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200530 int retCode = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400531
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200532 std::vector<RenderObject*>::iterator iter;
533 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
534 {
535 int ret = (*iter)->Update();
536 if (ret < 0)
537 LOGERR("An update request has failed.\n");
538 else if (ret > retCode)
539 retCode = ret;
540 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400541
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 return retCode;
Dees_Troy51a0e822012-09-05 15:24:24 -0400543}
544
545int Page::NotifyTouch(TOUCH_STATE state, int x, int y)
546{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200547 // By default, return 1 to ignore further touches if nobody is listening
548 int ret = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400549
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 // Don't try to handle a lack of handlers
551 if (mActions.size() == 0)
552 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400553
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200554 // We record mTouchStart so we can pass all the touch stream to the same handler
555 if (state == TOUCH_START)
556 {
557 std::vector<ActionObject*>::reverse_iterator iter;
558 // We work backwards, from top-most element to bottom-most element
559 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
560 {
561 if ((*iter)->IsInRegion(x, y))
562 {
563 mTouchStart = (*iter);
564 ret = mTouchStart->NotifyTouch(state, x, y);
565 if (ret >= 0)
566 break;
567 mTouchStart = NULL;
568 }
569 }
570 }
571 else if (state == TOUCH_RELEASE && mTouchStart != NULL)
572 {
573 ret = mTouchStart->NotifyTouch(state, x, y);
574 mTouchStart = NULL;
575 }
576 else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL)
577 {
578 ret = mTouchStart->NotifyTouch(state, x, y);
579 }
580 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400581}
582
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100583int Page::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400584{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200585 std::vector<ActionObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400586
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100587 int ret = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 // We work backwards, from top-most element to bottom-most element
589 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
590 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100591 ret = (*iter)->NotifyKey(key, down);
that8834a0f2016-01-05 23:29:30 +0100592 if (ret == 0)
593 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100594 if (ret < 0) {
595 LOGERR("An action handler has returned an error\n");
596 ret = 1;
597 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200598 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100599 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400600}
601
that8834a0f2016-01-05 23:29:30 +0100602int Page::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -0400603{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200604 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400605
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200606 // We work backwards, from top-most element to bottom-most element
607 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
608 {
that8834a0f2016-01-05 23:29:30 +0100609 int ret = (*iter)->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200610 if (ret == 0)
611 return 0;
612 else if (ret < 0)
that8834a0f2016-01-05 23:29:30 +0100613 LOGERR("A char input handler has returned an error");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200614 }
615 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400616}
617
618int Page::SetKeyBoardFocus(int inFocus)
619{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200620 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400621
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200622 // We work backwards, from top-most element to bottom-most element
623 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
624 {
625 int ret = (*iter)->SetInputFocus(inFocus);
626 if (ret == 0)
627 return 0;
628 else if (ret < 0)
629 LOGERR("An input focus handler has returned an error");
630 }
631 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400632}
633
634void Page::SetPageFocus(int inFocus)
635{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200636 // Render remaining objects
637 std::vector<RenderObject*>::iterator iter;
638 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
639 (*iter)->SetPageFocus(inFocus);
640
641 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400642}
643
644int Page::NotifyVarChange(std::string varName, std::string value)
645{
Vojtech Bocek07220562014-02-08 02:05:33 +0100646 std::vector<GUIObject*>::iterator iter;
647 for (iter = mObjects.begin(); iter != mObjects.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200648 {
649 if ((*iter)->NotifyVarChange(varName, value))
650 LOGERR("An action handler errored on NotifyVarChange.\n");
651 }
652 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400653}
654
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500655PageSet::PageSet(const char* xmlFile)
Dees_Troy51a0e822012-09-05 15:24:24 -0400656{
that74ac6062015-03-04 22:39:34 +0100657 mResources = new ResourceManager;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200658 mCurrentPage = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400659
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500660 if (!xmlFile)
thatb63e2f92015-06-27 21:35:11 +0200661 mCurrentPage = new Page(NULL, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400662}
663
664PageSet::~PageSet()
665{
Ethan Yonker1c273312015-03-16 12:18:56 -0500666 mOverlays.clear();
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100667 for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
668 delete *itr;
669
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200670 delete mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400671}
672
Ethan Yonker74db1572015-10-28 12:44:49 -0500673int PageSet::LoadLanguage(char* languageFile, ZipArchive* package)
674{
675 xml_document<> lang;
676 xml_node<>* parent;
677 xml_node<>* child;
678 std::string resource_source;
679
680 if (languageFile) {
681 printf("parsing languageFile\n");
682 lang.parse<0>(languageFile);
683 printf("parsing languageFile done\n");
684 } else {
685 return -1;
686 }
687
688 parent = lang.first_node("language");
689 if (!parent) {
690 LOGERR("Unable to locate language node in language file.\n");
691 lang.clear();
692 return -1;
693 }
694
695 child = parent->first_node("display");
696 if (child) {
697 DataManager::SetValue("tw_language_display", child->value());
698 resource_source = child->value();
699 } else {
700 LOGERR("language file does not have a display value set\n");
701 DataManager::SetValue("tw_language_display", "Not Set");
702 resource_source = languageFile;
703 }
704
705 child = parent->first_node("resources");
706 if (child)
707 mResources->LoadResources(child, package, resource_source);
708 else
709 return -1;
710 lang.clear();
711 return 0;
712}
713
714int PageSet::Load(ZipArchive* package, char* xmlFile, char* languageFile)
Dees_Troy51a0e822012-09-05 15:24:24 -0400715{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500716 xml_document<> mDoc;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200717 xml_node<>* parent;
718 xml_node<>* child;
Ethan Yonker780cd392014-07-21 15:24:39 -0500719 xml_node<>* xmltemplate;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600720 xml_node<>* xmlstyle;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500721
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500722 mDoc.parse<0>(xmlFile);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200723 parent = mDoc.first_node("recovery");
724 if (!parent)
725 parent = mDoc.first_node("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400726
Ethan Yonker63e414f2015-02-06 15:44:39 -0600727 set_scale_values(1, 1); // Reset any previous scaling values
728
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200729 // Now, let's parse the XML
Ethan Yonker63e414f2015-02-06 15:44:39 -0600730 LOGINFO("Checking resolution...\n");
731 child = parent->first_node("details");
732 if (child) {
733 xml_node<>* resolution = child->first_node("resolution");
734 if (resolution) {
735 xml_attribute<>* width_attr = resolution->first_attribute("width");
736 xml_attribute<>* height_attr = resolution->first_attribute("height");
737 xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
738 if (width_attr && height_attr && !noscale_attr) {
739 int width = atoi(width_attr->value());
740 int height = atoi(height_attr->value());
741 int offx = 0, offy = 0;
742#ifdef TW_ROUND_SCREEN
743 xml_node<>* roundscreen = child->first_node("roundscreen");
744 if (roundscreen) {
745 LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n");
746 xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x");
747 xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y");
748 if (offx_attr) {
749 offx = atoi(offx_attr->value());
750 }
751 if (offy_attr) {
752 offy = atoi(offy_attr->value());
753 }
754 }
755#endif
756 if (width != 0 && height != 0) {
757 float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width;
758 float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height;
759#ifdef TW_ROUND_SCREEN
760 float scale_off_w = (float)gr_fb_width() / (float)width;
761 float scale_off_h = (float)gr_fb_height() / (float)height;
762 tw_x_offset = offx * scale_off_w;
763 tw_y_offset = offy * scale_off_h;
764#endif
765 if (scale_w != 1 || scale_h != 1) {
766 LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset);
767 set_scale_values(scale_w, scale_h);
768 }
769 }
770 } else {
771 LOGINFO("XML does not contain width and height, no scaling will be applied\n");
772 }
773 } else {
774 LOGINFO("XML contains no resolution tag, no scaling will be applied.\n");
775 }
776 } else {
777 LOGINFO("XML contains no details tag, no scaling will be applied.\n");
778 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500779
780 if (languageFile)
781 LoadLanguage(languageFile, package);
782
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200783 LOGINFO("Loading resources...\n");
784 child = parent->first_node("resources");
785 if (child)
Ethan Yonker74db1572015-10-28 12:44:49 -0500786 mResources->LoadResources(child, package, "theme");
Dees_Troy51a0e822012-09-05 15:24:24 -0400787
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200788 LOGINFO("Loading variables...\n");
789 child = parent->first_node("variables");
790 if (child)
791 LoadVariables(child);
Dees_Troy51a0e822012-09-05 15:24:24 -0400792
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100793 LOGINFO("Loading mouse cursor...\n");
794 child = parent->first_node("mousecursor");
795 if(child)
796 PageManager::LoadCursorData(child);
797
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200798 LOGINFO("Loading pages...\n");
799 // This may be NULL if no templates are present
Ethan Yonker780cd392014-07-21 15:24:39 -0500800 xmltemplate = parent->first_node("templates");
801 if (xmltemplate)
802 templates.push_back(xmltemplate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400803
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600804 // Load styles if present
805 xmlstyle = parent->first_node("styles");
806 if (xmlstyle)
807 styles.push_back(xmlstyle);
808
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200809 child = parent->first_node("pages");
Ethan Yonker780cd392014-07-21 15:24:39 -0500810 if (child) {
811 if (LoadPages(child)) {
812 LOGERR("PageSet::Load returning -1\n");
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500813 mDoc.clear();
Ethan Yonker780cd392014-07-21 15:24:39 -0500814 return -1;
815 }
816 }
Vojtech Boceke979abd2015-01-12 18:29:12 +0100817
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500818 int ret = CheckInclude(package, &mDoc);
819 mDoc.clear();
820 templates.clear();
821 return ret;
Ethan Yonker780cd392014-07-21 15:24:39 -0500822}
Dees_Troy51a0e822012-09-05 15:24:24 -0400823
Ethan Yonker780cd392014-07-21 15:24:39 -0500824int PageSet::CheckInclude(ZipArchive* package, xml_document<> *parentDoc)
825{
826 xml_node<>* par;
827 xml_node<>* par2;
828 xml_node<>* chld;
829 xml_node<>* parent;
830 xml_node<>* child;
831 xml_node<>* xmltemplate;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600832 xml_node<>* xmlstyle;
Ethan Yonker780cd392014-07-21 15:24:39 -0500833 long len;
834 char* xmlFile = NULL;
835 string filename;
Vojtech Boceke979abd2015-01-12 18:29:12 +0100836 xml_document<> *doc = NULL;
Ethan Yonker780cd392014-07-21 15:24:39 -0500837
838 par = parentDoc->first_node("recovery");
839 if (!par) {
840 par = parentDoc->first_node("install");
841 }
842 if (!par) {
843 return 0;
844 }
845
846 par2 = par->first_node("include");
847 if (!par2)
848 return 0;
849 chld = par2->first_node("xmlfile");
850 while (chld != NULL) {
851 xml_attribute<>* attr = chld->first_attribute("name");
852 if (!attr)
853 break;
854
Ethan Yonker780cd392014-07-21 15:24:39 -0500855 if (!package) {
856 // We can try to load the XML directly...
Dees Troy3454ade2015-01-20 19:21:04 +0000857 filename = TWRES;
Ethan Yonker780cd392014-07-21 15:24:39 -0500858 filename += attr->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500859 } else {
860 filename += attr->value();
Ethan Yonker561c58d2015-10-05 08:48:22 -0500861 }
862 xmlFile = PageManager::LoadFileToBuffer(filename, package);
863 if (xmlFile == NULL) {
864 LOGERR("PageSet::CheckInclude unable to load '%s'\n", filename.c_str());
865 return -1;
Ethan Yonker780cd392014-07-21 15:24:39 -0500866 }
Ethan Yonker780cd392014-07-21 15:24:39 -0500867
Vojtech Boceke979abd2015-01-12 18:29:12 +0100868 doc = new xml_document<>();
869 doc->parse<0>(xmlFile);
870
871 parent = doc->first_node("recovery");
Ethan Yonker780cd392014-07-21 15:24:39 -0500872 if (!parent)
Vojtech Boceke979abd2015-01-12 18:29:12 +0100873 parent = doc->first_node("install");
Ethan Yonker780cd392014-07-21 15:24:39 -0500874
875 // Now, let's parse the XML
876 LOGINFO("Loading included resources...\n");
877 child = parent->first_node("resources");
878 if (child)
Ethan Yonker74db1572015-10-28 12:44:49 -0500879 mResources->LoadResources(child, package, "theme");
Ethan Yonker780cd392014-07-21 15:24:39 -0500880
881 LOGINFO("Loading included variables...\n");
882 child = parent->first_node("variables");
883 if (child)
884 LoadVariables(child);
885
886 LOGINFO("Loading mouse cursor...\n");
887 child = parent->first_node("mousecursor");
888 if(child)
889 PageManager::LoadCursorData(child);
890
891 LOGINFO("Loading included pages...\n");
892 // This may be NULL if no templates are present
893 xmltemplate = parent->first_node("templates");
894 if (xmltemplate)
895 templates.push_back(xmltemplate);
896
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600897 // Load styles if present
898 xmlstyle = parent->first_node("styles");
899 if (xmlstyle)
900 styles.push_back(xmlstyle);
901
Ethan Yonker780cd392014-07-21 15:24:39 -0500902 child = parent->first_node("pages");
Vojtech Boceke979abd2015-01-12 18:29:12 +0100903 if (child && LoadPages(child))
904 {
905 templates.pop_back();
906 doc->clear();
907 delete doc;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500908 free(xmlFile);
Vojtech Boceke979abd2015-01-12 18:29:12 +0100909 return -1;
910 }
Ethan Yonker780cd392014-07-21 15:24:39 -0500911
Ethan Yonker561c58d2015-10-05 08:48:22 -0500912 if (CheckInclude(package, doc)) {
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500913 doc->clear();
914 delete doc;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500915 free(xmlFile);
Ethan Yonker780cd392014-07-21 15:24:39 -0500916 return -1;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500917 }
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500918 doc->clear();
919 delete doc;
920 free(xmlFile);
Ethan Yonker780cd392014-07-21 15:24:39 -0500921
922 chld = chld->next_sibling("xmlfile");
923 }
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500924
Ethan Yonker780cd392014-07-21 15:24:39 -0500925 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400926}
927
928int PageSet::SetPage(std::string page)
929{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200930 Page* tmp = FindPage(page);
931 if (tmp)
932 {
933 if (mCurrentPage) mCurrentPage->SetPageFocus(0);
934 mCurrentPage = tmp;
935 mCurrentPage->SetPageFocus(1);
936 mCurrentPage->NotifyVarChange("", "");
937 return 0;
938 }
939 else
940 {
941 LOGERR("Unable to locate page (%s)\n", page.c_str());
942 }
943 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400944}
945
946int PageSet::SetOverlay(Page* page)
947{
Ethan Yonker1c273312015-03-16 12:18:56 -0500948 if (page) {
949 if (mOverlays.size() >= 10) {
950 LOGERR("Too many overlays requested, max is 10.\n");
951 return -1;
952 }
Matt Mowerd411f8d2015-04-09 16:04:12 -0500953
954 std::vector<Page*>::iterator iter;
955 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
956 if ((*iter)->GetName() == page->GetName()) {
957 mOverlays.erase(iter);
958 // SetOverlay() is (and should stay) the only function which
959 // adds to mOverlays. Then, each page can appear at most once.
960 break;
961 }
962 }
963
Ethan Yonker1c273312015-03-16 12:18:56 -0500964 page->SetPageFocus(1);
965 page->NotifyVarChange("", "");
966
967 if (!mOverlays.empty())
968 mOverlays.back()->SetPageFocus(0);
969
970 mOverlays.push_back(page);
971 } else {
972 if (!mOverlays.empty()) {
973 mOverlays.back()->SetPageFocus(0);
974 mOverlays.pop_back();
975 if (!mOverlays.empty())
976 mOverlays.back()->SetPageFocus(1);
977 else if (mCurrentPage)
978 mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
979 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200980 }
981 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400982}
983
that74ac6062015-03-04 22:39:34 +0100984const ResourceManager* PageSet::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -0400985{
that74ac6062015-03-04 22:39:34 +0100986 return mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400987}
988
989Page* PageSet::FindPage(std::string name)
990{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200991 std::vector<Page*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400992
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200993 for (iter = mPages.begin(); iter != mPages.end(); iter++)
994 {
995 if (name == (*iter)->GetName())
996 return (*iter);
997 }
998 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400999}
1000
1001int PageSet::LoadVariables(xml_node<>* vars)
1002{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001003 xml_node<>* child;
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001004 xml_attribute<> *name, *value, *persist;
1005 int p;
Dees_Troy51a0e822012-09-05 15:24:24 -04001006
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001007 child = vars->first_node("variable");
1008 while (child)
1009 {
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001010 name = child->first_attribute("name");
1011 value = child->first_attribute("value");
1012 persist = child->first_attribute("persist");
1013 if(name && value)
1014 {
Ethan Yonker751a85e2014-12-12 16:59:10 -06001015 if (strcmp(name->value(), "tw_x_offset") == 0) {
1016 tw_x_offset = atoi(value->value());
1017 child = child->next_sibling("variable");
1018 continue;
1019 }
1020 if (strcmp(name->value(), "tw_y_offset") == 0) {
1021 tw_y_offset = atoi(value->value());
1022 child = child->next_sibling("variable");
1023 continue;
1024 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001025 p = persist ? atoi(persist->value()) : 0;
Ethan Yonker96acb3d2014-08-05 09:20:30 -05001026 string temp = value->value();
1027 string valstr = gui_parse_text(temp);
1028
1029 if (valstr.find("+") != string::npos) {
1030 string val1str = valstr;
1031 val1str = val1str.substr(0, val1str.find('+'));
1032 string val2str = valstr;
1033 val2str = val2str.substr(val2str.find('+') + 1, string::npos);
1034 int val1 = atoi(val1str.c_str());
1035 int val2 = atoi(val2str.c_str());
1036 int val = val1 + val2;
1037
1038 DataManager::SetValue(name->value(), val, p);
1039 } else if (valstr.find("-") != string::npos) {
1040 string val1str = valstr;
1041 val1str = val1str.substr(0, val1str.find('-'));
1042 string val2str = valstr;
1043 val2str = val2str.substr(val2str.find('-') + 1, string::npos);
1044 int val1 = atoi(val1str.c_str());
1045 int val2 = atoi(val2str.c_str());
1046 int val = val1 - val2;
1047
1048 DataManager::SetValue(name->value(), val, p);
1049 } else {
1050 DataManager::SetValue(name->value(), valstr, p);
1051 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001052 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001053
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001054 child = child->next_sibling("variable");
1055 }
1056 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001057}
1058
Ethan Yonker780cd392014-07-21 15:24:39 -05001059int PageSet::LoadPages(xml_node<>* pages)
Dees_Troy51a0e822012-09-05 15:24:24 -04001060{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001061 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -04001062
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001063 if (!pages)
1064 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001066 child = pages->first_node("page");
1067 while (child != NULL)
1068 {
Ethan Yonker780cd392014-07-21 15:24:39 -05001069 Page* page = new Page(child, &templates);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001070 if (page->GetName().empty())
1071 {
1072 LOGERR("Unable to process load page\n");
1073 delete page;
1074 }
1075 else
1076 {
1077 mPages.push_back(page);
1078 }
1079 child = child->next_sibling("page");
1080 }
1081 if (mPages.size() > 0)
1082 return 0;
1083 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001084}
1085
1086int PageSet::IsCurrentPage(Page* page)
1087{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001088 return ((mCurrentPage && mCurrentPage == page) ? 1 : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001089}
1090
that10ae24f2015-12-26 20:53:51 +01001091std::string PageSet::GetCurrentPage() const
1092{
1093 return mCurrentPage ? mCurrentPage->GetName() : "";
1094}
1095
Dees_Troy51a0e822012-09-05 15:24:24 -04001096int PageSet::Render(void)
1097{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001098 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001099
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001100 ret = (mCurrentPage ? mCurrentPage->Render() : -1);
1101 if (ret < 0)
1102 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001103
1104 std::vector<Page*>::iterator iter;
1105
1106 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1107 ret = ((*iter) ? (*iter)->Render() : -1);
1108 if (ret < 0)
1109 return ret;
1110 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001111 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001112}
1113
1114int PageSet::Update(void)
1115{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001116 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001118 ret = (mCurrentPage ? mCurrentPage->Update() : -1);
1119 if (ret < 0 || ret > 1)
1120 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001121
1122 std::vector<Page*>::iterator iter;
1123
1124 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1125 ret = ((*iter) ? (*iter)->Update() : -1);
1126 if (ret < 0)
1127 return ret;
1128 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001129 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001130}
1131
1132int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
1133{
Ethan Yonker1c273312015-03-16 12:18:56 -05001134 if (!mOverlays.empty())
1135 return mOverlays.back()->NotifyTouch(state, x, y);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001136
1137 return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001138}
1139
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001140int PageSet::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001141{
Ethan Yonker1c273312015-03-16 12:18:56 -05001142 if (!mOverlays.empty())
1143 return mOverlays.back()->NotifyKey(key, down);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001144
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001145 return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001146}
1147
that8834a0f2016-01-05 23:29:30 +01001148int PageSet::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001149{
Ethan Yonker1c273312015-03-16 12:18:56 -05001150 if (!mOverlays.empty())
that8834a0f2016-01-05 23:29:30 +01001151 return mOverlays.back()->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001152
that8834a0f2016-01-05 23:29:30 +01001153 return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001154}
1155
1156int PageSet::SetKeyBoardFocus(int inFocus)
1157{
Ethan Yonker1c273312015-03-16 12:18:56 -05001158 if (!mOverlays.empty())
1159 return mOverlays.back()->SetKeyBoardFocus(inFocus);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001160
1161 return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001162}
1163
1164int PageSet::NotifyVarChange(std::string varName, std::string value)
1165{
Ethan Yonker1c273312015-03-16 12:18:56 -05001166 std::vector<Page*>::iterator iter;
1167
1168 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
1169 (*iter)->NotifyVarChange(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001170
1171 return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001172}
1173
Ethan Yonker74db1572015-10-28 12:44:49 -05001174void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1175{
1176 mResources->AddStringResource(resource_source, resource_name, value);
1177}
1178
Ethan Yonker561c58d2015-10-05 08:48:22 -05001179char* PageManager::LoadFileToBuffer(std::string filename, ZipArchive* package) {
1180 size_t len;
1181 char* buffer = NULL;
1182
1183 if (!package) {
1184 // We can try to load the XML directly...
1185 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str());
1186 struct stat st;
1187 if(stat(filename.c_str(),&st) != 0) {
1188 // This isn't always an error, sometimes we request files that don't exist.
1189 return NULL;
1190 }
1191
1192 len = (size_t)st.st_size;
1193
1194 buffer = (char*) malloc(len + 1);
1195 if (!buffer) {
1196 LOGERR("PageManager::LoadFileToBuffer failed to malloc\n");
1197 return NULL;
1198 }
1199
1200 int fd = open(filename.c_str(), O_RDONLY);
1201 if (fd == -1) {
1202 LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno));
1203 free(buffer);
1204 return NULL;
1205 }
1206
1207 if (read(fd, buffer, len) < 0) {
1208 LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno));
1209 free(buffer);
1210 close(fd);
1211 return NULL;
1212 }
1213 close(fd);
1214 } else {
1215 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
1216 const ZipEntry* zipentry = mzFindZipEntry(package, filename.c_str());
1217 if (zipentry == NULL) {
1218 LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
1219 return NULL;
1220 }
1221
1222 // Allocate the buffer for the file
1223 len = mzGetZipEntryUncompLen(zipentry);
1224 buffer = (char*) malloc(len + 1);
1225 if (!buffer)
1226 return NULL;
1227
1228 if (!mzExtractZipEntryToBuffer(package, zipentry, (unsigned char*) buffer)) {
1229 LOGERR("Unable to extract '%s'\n", filename.c_str());
1230 free(buffer);
1231 return NULL;
1232 }
1233 }
1234 // NULL-terminate the string
1235 buffer[len] = 0x00;
1236 return buffer;
1237}
1238
Ethan Yonker74db1572015-10-28 12:44:49 -05001239void PageManager::LoadLanguageListDir(string dir) {
1240 if (!TWFunc::Path_Exists(dir)) {
1241 LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str());
1242 return;
1243 }
1244
1245 DIR *d = opendir(dir.c_str());
1246 struct dirent *p;
1247
1248 if (d == NULL) {
1249 LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno));
1250 return;
1251 }
1252
1253 while ((p = readdir(d))) {
1254 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5)
1255 continue;
1256
1257 string file = p->d_name;
1258 if (file.substr(strlen(p->d_name) - 4) != ".xml")
1259 continue;
1260 string path = dir + p->d_name;
1261 string file_no_extn = file.substr(0, strlen(p->d_name) - 4);
1262 struct language_struct language_entry;
1263 language_entry.filename = file_no_extn;
1264 char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL);
1265 if (xmlFile == NULL) {
1266 LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str());
1267 continue;
1268 }
1269 xml_document<> *doc = new xml_document<>();
1270 doc->parse<0>(xmlFile);
1271
1272 xml_node<>* parent = doc->first_node("language");
1273 if (!parent) {
1274 LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str());
1275 } else {
1276 xml_node<>* child = parent->first_node("display");
1277 if (child) {
1278 language_entry.displayvalue = child->value();
1279 } else {
1280 LOGERR("No display value for '%s'\n", language_entry.filename.c_str());
1281 language_entry.displayvalue = language_entry.filename;
1282 }
1283 Language_List.push_back(language_entry);
1284 }
1285 doc->clear();
1286 delete doc;
1287 free(xmlFile);
1288 }
1289 closedir(d);
1290}
1291
1292void PageManager::LoadLanguageList(ZipArchive* package) {
1293 Language_List.clear();
1294 if (TWFunc::Path_Exists(TWRES "customlanguages"))
1295 TWFunc::removeDir(TWRES "customlanguages", true);
1296 if (package) {
1297 TWFunc::Recursive_Mkdir(TWRES "customlanguages");
1298 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
1299 mzExtractRecursive(package, "languages", TWRES "customlanguages/", &timestamp, NULL, NULL, NULL);
1300 LoadLanguageListDir(TWRES "customlanguages/");
1301 } else {
1302 LoadLanguageListDir(TWRES "languages/");
1303 }
1304}
1305
1306void PageManager::LoadLanguage(string filename) {
1307 string actual_filename;
1308 if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml"))
1309 actual_filename = TWRES "customlanguages/" + filename + ".xml";
1310 else
1311 actual_filename = TWRES "languages/" + filename + ".xml";
1312 char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL);
1313 if (xmlFile == NULL)
1314 LOGERR("Unable to load '%s'\n", actual_filename.c_str());
1315 else {
1316 mCurrentSet->LoadLanguage(xmlFile, NULL);
1317 free(xmlFile);
1318 }
1319 PartitionManager.Translate_Partition_Display_Names();
1320}
1321
Dees_Troy51a0e822012-09-05 15:24:24 -04001322int PageManager::LoadPackage(std::string name, std::string package, std::string startpage)
1323{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001324 int fd;
1325 ZipArchive zip, *pZip = NULL;
1326 long len;
1327 char* xmlFile = NULL;
Ethan Yonker74db1572015-10-28 12:44:49 -05001328 char* languageFile = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001329 PageSet* pageSet = NULL;
1330 int ret;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001331 MemMapping map;
Dees_Troy51a0e822012-09-05 15:24:24 -04001332
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001333 mReloadTheme = false;
1334 mStartPage = startpage;
1335
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001336 // Open the XML file
1337 LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str());
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001338 if (package.size() > 4 && package.substr(package.size() - 4) != ".zip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001339 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001340 LOGINFO("Load XML directly\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001341 tw_x_offset = TW_X_OFFSET;
1342 tw_y_offset = TW_Y_OFFSET;
Ethan Yonker74db1572015-10-28 12:44:49 -05001343 LoadLanguageList(NULL);
1344 languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001345 }
1346 else
1347 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001348 LOGINFO("Loading zip theme\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001349 tw_x_offset = 0;
1350 tw_y_offset = 0;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001351 if (!TWFunc::Path_Exists(package))
1352 return -1;
1353 if (sysMapFile(package.c_str(), &map) != 0) {
1354 LOGERR("Failed to map '%s'\n", package.c_str());
Ethan Yonker561c58d2015-10-05 08:48:22 -05001355 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001356 }
1357 if (mzOpenZipArchive(map.addr, map.length, &zip)) {
1358 LOGERR("Unable to open zip archive '%s'\n", package.c_str());
1359 sysReleaseMap(&map);
Ethan Yonker561c58d2015-10-05 08:48:22 -05001360 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001361 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001362 pZip = &zip;
Ethan Yonker561c58d2015-10-05 08:48:22 -05001363 package = "ui.xml";
Ethan Yonker74db1572015-10-28 12:44:49 -05001364 LoadLanguageList(pZip);
1365 languageFile = LoadFileToBuffer("languages/en.xml", pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001366 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001367
Ethan Yonker561c58d2015-10-05 08:48:22 -05001368 xmlFile = LoadFileToBuffer(package, pZip);
1369 if (xmlFile == NULL) {
1370 goto error;
1371 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001372
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001373 // Before loading, mCurrentSet must be the loading package so we can find resources
1374 pageSet = mCurrentSet;
1375 mCurrentSet = new PageSet(xmlFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001376 ret = mCurrentSet->Load(pZip, xmlFile, languageFile);
1377 if (languageFile) {
1378 free(languageFile);
1379 languageFile = NULL;
1380 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001381 if (ret == 0)
1382 {
1383 mCurrentSet->SetPage(startpage);
1384 mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
1385 }
1386 else
1387 {
1388 LOGERR("Package %s failed to load.\n", name.c_str());
1389 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001390
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001391 // The first successful package we loaded is the base
1392 if (mBaseSet == NULL)
1393 mBaseSet = mCurrentSet;
1394
1395 mCurrentSet = pageSet;
1396
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001397 if (pZip) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001398 mzCloseZipArchive(pZip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001399 sysReleaseMap(&map);
1400 }
Ethan Yonker561c58d2015-10-05 08:48:22 -05001401 free(xmlFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001402 if (languageFile)
1403 free(languageFile);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001404 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001405
1406error:
Ethan Yonker561c58d2015-10-05 08:48:22 -05001407 // Sometimes we get here without a real error
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001408 if (pZip) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001409 mzCloseZipArchive(pZip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001410 sysReleaseMap(&map);
1411 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001412 if (xmlFile)
1413 free(xmlFile);
1414 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001415}
1416
1417PageSet* PageManager::FindPackage(std::string name)
1418{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001419 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001420
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001421 iter = mPageSets.find(name);
1422 if (iter != mPageSets.end())
1423 return (*iter).second;
1424
1425 LOGERR("Unable to locate package %s\n", name.c_str());
1426 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001427}
1428
1429PageSet* PageManager::SelectPackage(std::string name)
1430{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001431 LOGINFO("Switching packages (%s)\n", name.c_str());
1432 PageSet* tmp;
Dees_Troy51a0e822012-09-05 15:24:24 -04001433
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001434 tmp = FindPackage(name);
1435 if (tmp)
Vojtech Bocek07220562014-02-08 02:05:33 +01001436 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001437 mCurrentSet = tmp;
Vojtech Bocek07220562014-02-08 02:05:33 +01001438 mCurrentSet->NotifyVarChange("", "");
1439 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001440 else
1441 LOGERR("Unable to find package.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001442
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001443 return mCurrentSet;
Dees_Troy51a0e822012-09-05 15:24:24 -04001444}
1445
1446int PageManager::ReloadPackage(std::string name, std::string package)
1447{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001448 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001449
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001450 mReloadTheme = false;
1451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001452 iter = mPageSets.find(name);
1453 if (iter == mPageSets.end())
1454 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001455
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001456 if(mMouseCursor)
1457 mMouseCursor->ResetData(gr_fb_width(), gr_fb_height());
1458
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001459 PageSet* set = (*iter).second;
1460 mPageSets.erase(iter);
Dees_Troy51a0e822012-09-05 15:24:24 -04001461
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001462 if (LoadPackage(name, package, mStartPage) != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001463 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001464 LOGINFO("Failed to load package '%s'.\n", package.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001465 mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
1466 return -1;
1467 }
1468 if (mCurrentSet == set)
1469 SelectPackage(name);
Vojtech Bocekbfb63342014-02-08 00:32:31 +01001470 if (mBaseSet == set)
1471 mBaseSet = mCurrentSet;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001472 delete set;
Ethan Yonker74db1572015-10-28 12:44:49 -05001473 GUIConsole::Translate_Now();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001474 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001475}
1476
1477void PageManager::ReleasePackage(std::string name)
1478{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001479 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001480
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001481 iter = mPageSets.find(name);
1482 if (iter == mPageSets.end())
1483 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001484
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001485 PageSet* set = (*iter).second;
1486 mPageSets.erase(iter);
1487 delete set;
1488 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001489}
1490
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001491int PageManager::RunReload() {
1492 int ret_val = 0;
1493 std::string theme_path;
1494
1495 if (!mReloadTheme)
1496 return 0;
1497
1498 mReloadTheme = false;
1499 theme_path = DataManager::GetSettingsStoragePath();
1500 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1501 LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
1502 ret_val = 1;
1503 }
1504
1505 theme_path += "/TWRP/theme/ui.zip";
1506 if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0)
1507 {
1508 // Loading the custom theme failed - try loading the stock theme
1509 LOGINFO("Attempting to reload stock theme...\n");
1510 if (ReloadPackage("TWRP", TWRES "ui.xml"))
1511 {
1512 LOGERR("Failed to load base packages.\n");
1513 ret_val = 1;
1514 }
1515 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 if (ret_val == 0) {
1517 if (DataManager::GetStrValue("tw_language") != "en.xml") {
1518 LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str());
1519 LoadLanguage(DataManager::GetStrValue("tw_language"));
1520 }
1521 }
1522
1523 // This makes the console re-translate
1524 last_message_count = 0;
1525 gConsole.clear();
1526 gConsoleColor.clear();
1527
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001528 return ret_val;
1529}
1530
1531void PageManager::RequestReload() {
1532 mReloadTheme = true;
1533}
1534
Dees_Troy51a0e822012-09-05 15:24:24 -04001535int PageManager::ChangePage(std::string name)
1536{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001537 DataManager::SetValue("tw_operation_state", 0);
1538 int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1);
1539 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001540}
1541
that10ae24f2015-12-26 20:53:51 +01001542std::string PageManager::GetCurrentPage()
1543{
1544 return mCurrentSet ? mCurrentSet->GetCurrentPage() : "";
1545}
1546
Dees_Troy51a0e822012-09-05 15:24:24 -04001547int PageManager::ChangeOverlay(std::string name)
1548{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001549 if (name.empty())
1550 return mCurrentSet->SetOverlay(NULL);
1551 else
1552 {
1553 Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL;
1554 return mCurrentSet->SetOverlay(page);
1555 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001556}
1557
that74ac6062015-03-04 22:39:34 +01001558const ResourceManager* PageManager::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001559{
that74ac6062015-03-04 22:39:34 +01001560 return (mCurrentSet ? mCurrentSet->GetResources() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001561}
1562
Dees_Troy51a0e822012-09-05 15:24:24 -04001563int PageManager::IsCurrentPage(Page* page)
1564{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001565 return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001566}
1567
1568int PageManager::Render(void)
1569{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001570 if(blankTimer.isScreenOff())
1571 return 0;
1572
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001573 int res = (mCurrentSet ? mCurrentSet->Render() : -1);
1574 if(mMouseCursor)
1575 mMouseCursor->Render();
1576 return res;
1577}
1578
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001579HardwareKeyboard *PageManager::GetHardwareKeyboard()
1580{
1581 if(!mHardwareKeyboard)
1582 mHardwareKeyboard = new HardwareKeyboard();
1583 return mHardwareKeyboard;
1584}
1585
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001586xml_node<>* PageManager::FindStyle(std::string name)
1587{
1588 for (std::vector<xml_node<>*>::iterator itr = mCurrentSet->styles.begin(); itr != mCurrentSet->styles.end(); itr++) {
1589 xml_node<>* node = (*itr)->first_node("style");
1590
1591 while (node) {
1592 if (!node->first_attribute("name"))
1593 continue;
1594
1595 if (name == node->first_attribute("name")->value())
1596 return node;
1597 node = node->next_sibling("style");
1598 }
1599 }
1600 return NULL;
1601}
1602
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001603MouseCursor *PageManager::GetMouseCursor()
1604{
1605 if(!mMouseCursor)
1606 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1607 return mMouseCursor;
1608}
1609
1610void PageManager::LoadCursorData(xml_node<>* node)
1611{
1612 if(!mMouseCursor)
1613 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1614
1615 mMouseCursor->LoadData(node);
Dees_Troy51a0e822012-09-05 15:24:24 -04001616}
1617
1618int PageManager::Update(void)
1619{
thatfb759d42015-01-11 12:16:53 +01001620 if(blankTimer.isScreenOff())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001621 return 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001622
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001623 if (RunReload())
1624 return -2;
1625
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001626 int res = (mCurrentSet ? mCurrentSet->Update() : -1);
1627
1628 if(mMouseCursor)
1629 {
1630 int c_res = mMouseCursor->Update();
1631 if(c_res > res)
1632 res = c_res;
1633 }
1634 return res;
Dees_Troy51a0e822012-09-05 15:24:24 -04001635}
1636
1637int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y)
1638{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001639 return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001640}
1641
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001642int PageManager::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001643{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001644 return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001645}
1646
that8834a0f2016-01-05 23:29:30 +01001647int PageManager::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001648{
that8834a0f2016-01-05 23:29:30 +01001649 return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001650}
1651
1652int PageManager::SetKeyBoardFocus(int inFocus)
1653{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001654 return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001655}
1656
1657int PageManager::NotifyVarChange(std::string varName, std::string value)
1658{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001659 return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001660}
1661
Ethan Yonker74db1572015-10-28 12:44:49 -05001662void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1663{
1664 if (mCurrentSet)
1665 mCurrentSet->AddStringResource(resource_source, resource_name, value);
1666}
1667
Dees_Troy51a0e822012-09-05 15:24:24 -04001668extern "C" void gui_notifyVarChange(const char *name, const char* value)
1669{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001670 if (!gGuiRunning)
1671 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001672
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001673 PageManager::NotifyVarChange(name, value);
Dees_Troy51a0e822012-09-05 15:24:24 -04001674}