blob: bd7c799599216871ec9f9c81e501ebb0e5449f22 [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 }
that1964d192016-01-07 00:41:03 +0100375 else if (type == "terminal")
376 {
377 GUITerminal* element = new GUITerminal(child);
378 mObjects.push_back(element);
379 mRenders.push_back(element);
380 mActions.push_back(element);
381 mInputs.push_back(element);
382 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200383 else if (type == "button")
384 {
385 GUIButton* element = new GUIButton(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100386 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 mRenders.push_back(element);
388 mActions.push_back(element);
389 }
390 else if (type == "checkbox")
391 {
392 GUICheckbox* element = new GUICheckbox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100393 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200394 mRenders.push_back(element);
395 mActions.push_back(element);
396 }
397 else if (type == "fileselector")
398 {
399 GUIFileSelector* element = new GUIFileSelector(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100400 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 mRenders.push_back(element);
402 mActions.push_back(element);
403 }
404 else if (type == "animation")
405 {
406 GUIAnimation* element = new GUIAnimation(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100407 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200408 mRenders.push_back(element);
409 }
410 else if (type == "progressbar")
411 {
412 GUIProgressBar* element = new GUIProgressBar(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100413 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200414 mRenders.push_back(element);
415 mActions.push_back(element);
416 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 else if (type == "slider")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 {
419 GUISlider* element = new GUISlider(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100420 mObjects.push_back(element);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200421 mRenders.push_back(element);
422 mActions.push_back(element);
423 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200424 else if (type == "slidervalue")
425 {
426 GUISliderValue *element = new GUISliderValue(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100427 mObjects.push_back(element);
Vojtech Bocek85932342013-04-01 22:11:33 +0200428 mRenders.push_back(element);
429 mActions.push_back(element);
430 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400431 else if (type == "listbox")
432 {
433 GUIListBox* element = new GUIListBox(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100434 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400435 mRenders.push_back(element);
436 mActions.push_back(element);
437 }
438 else if (type == "keyboard")
439 {
440 GUIKeyboard* element = new GUIKeyboard(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100441 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 mRenders.push_back(element);
443 mActions.push_back(element);
444 }
445 else if (type == "input")
446 {
447 GUIInput* element = new GUIInput(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100448 mObjects.push_back(element);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 mRenders.push_back(element);
450 mActions.push_back(element);
451 mInputs.push_back(element);
452 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500453 else if (type == "partitionlist")
454 {
455 GUIPartitionList* element = new GUIPartitionList(child);
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100456 mObjects.push_back(element);
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 mRenders.push_back(element);
458 mActions.push_back(element);
459 }
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100460 else if (type == "patternpassword")
461 {
462 GUIPatternPassword* element = new GUIPatternPassword(child);
463 mObjects.push_back(element);
464 mRenders.push_back(element);
465 mActions.push_back(element);
466 }
Ethan Yonker44925ad2015-07-22 12:33:59 -0500467 else if (type == "textbox")
468 {
469 GUITextBox* element = new GUITextBox(child);
470 mObjects.push_back(element);
471 mRenders.push_back(element);
472 mActions.push_back(element);
473 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200474 else if (type == "template")
475 {
476 if (!templates || !child->first_attribute("name"))
477 {
478 LOGERR("Invalid template request.\n");
479 }
480 else
481 {
482 std::string name = child->first_attribute("name")->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500483 xml_node<>* node;
484 bool node_found = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200486 // We need to find the correct template
Ethan Yonker780cd392014-07-21 15:24:39 -0500487 for (std::vector<xml_node<>*>::iterator itr = templates->begin(); itr != templates->end(); itr++) {
488 node = (*itr)->first_node("template");
Dees_Troy51a0e822012-09-05 15:24:24 -0400489
Ethan Yonker780cd392014-07-21 15:24:39 -0500490 while (node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200491 {
Ethan Yonker780cd392014-07-21 15:24:39 -0500492 if (!node->first_attribute("name"))
493 continue;
494
495 if (name == node->first_attribute("name")->value())
496 {
497 if (!ProcessNode(node, templates, depth + 1))
498 return false;
499 else {
500 node_found = true;
501 break;
502 }
503 }
504 if (node_found)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200505 break;
Ethan Yonker780cd392014-07-21 15:24:39 -0500506 node = node->next_sibling("template");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200507 }
thatb63e2f92015-06-27 21:35:11 +0200508 // [check] why is there no if (node_found) here too?
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200509 }
510 }
511 }
512 else
513 {
thatb63e2f92015-06-27 21:35:11 +0200514 LOGERR("Unknown object type: %s.\n", type.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200515 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200516 }
517 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400518}
519
520int Page::Render(void)
521{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200522 // Render background
523 gr_color(mBackground.red, mBackground.green, mBackground.blue, mBackground.alpha);
524 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Dees_Troy51a0e822012-09-05 15:24:24 -0400525
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526 // Render remaining objects
527 std::vector<RenderObject*>::iterator iter;
528 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
529 {
530 if ((*iter)->Render())
531 LOGERR("A render request has failed.\n");
532 }
533 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400534}
535
536int Page::Update(void)
537{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200538 int retCode = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400539
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 std::vector<RenderObject*>::iterator iter;
541 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
542 {
543 int ret = (*iter)->Update();
544 if (ret < 0)
545 LOGERR("An update request has failed.\n");
546 else if (ret > retCode)
547 retCode = ret;
548 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400549
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 return retCode;
Dees_Troy51a0e822012-09-05 15:24:24 -0400551}
552
553int Page::NotifyTouch(TOUCH_STATE state, int x, int y)
554{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200555 // By default, return 1 to ignore further touches if nobody is listening
556 int ret = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400557
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200558 // Don't try to handle a lack of handlers
559 if (mActions.size() == 0)
560 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400561
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200562 // We record mTouchStart so we can pass all the touch stream to the same handler
563 if (state == TOUCH_START)
564 {
565 std::vector<ActionObject*>::reverse_iterator iter;
566 // We work backwards, from top-most element to bottom-most element
567 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
568 {
569 if ((*iter)->IsInRegion(x, y))
570 {
571 mTouchStart = (*iter);
572 ret = mTouchStart->NotifyTouch(state, x, y);
573 if (ret >= 0)
574 break;
575 mTouchStart = NULL;
576 }
577 }
578 }
579 else if (state == TOUCH_RELEASE && mTouchStart != NULL)
580 {
581 ret = mTouchStart->NotifyTouch(state, x, y);
582 mTouchStart = NULL;
583 }
584 else if ((state == TOUCH_DRAG || state == TOUCH_HOLD || state == TOUCH_REPEAT) && mTouchStart != NULL)
585 {
586 ret = mTouchStart->NotifyTouch(state, x, y);
587 }
588 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400589}
590
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100591int Page::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400592{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200593 std::vector<ActionObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100595 int ret = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200596 // We work backwards, from top-most element to bottom-most element
597 for (iter = mActions.rbegin(); iter != mActions.rend(); iter++)
598 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100599 ret = (*iter)->NotifyKey(key, down);
that8834a0f2016-01-05 23:29:30 +0100600 if (ret == 0)
601 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100602 if (ret < 0) {
603 LOGERR("An action handler has returned an error\n");
604 ret = 1;
605 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200606 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100607 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400608}
609
that8834a0f2016-01-05 23:29:30 +0100610int Page::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -0400611{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200612 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400613
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200614 // We work backwards, from top-most element to bottom-most element
615 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
616 {
that8834a0f2016-01-05 23:29:30 +0100617 int ret = (*iter)->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200618 if (ret == 0)
619 return 0;
620 else if (ret < 0)
that8834a0f2016-01-05 23:29:30 +0100621 LOGERR("A char input handler has returned an error");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200622 }
623 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400624}
625
626int Page::SetKeyBoardFocus(int inFocus)
627{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200628 std::vector<InputObject*>::reverse_iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400629
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200630 // We work backwards, from top-most element to bottom-most element
631 for (iter = mInputs.rbegin(); iter != mInputs.rend(); iter++)
632 {
633 int ret = (*iter)->SetInputFocus(inFocus);
634 if (ret == 0)
635 return 0;
636 else if (ret < 0)
637 LOGERR("An input focus handler has returned an error");
638 }
639 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400640}
641
642void Page::SetPageFocus(int inFocus)
643{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200644 // Render remaining objects
645 std::vector<RenderObject*>::iterator iter;
646 for (iter = mRenders.begin(); iter != mRenders.end(); iter++)
647 (*iter)->SetPageFocus(inFocus);
648
649 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400650}
651
652int Page::NotifyVarChange(std::string varName, std::string value)
653{
Vojtech Bocek07220562014-02-08 02:05:33 +0100654 std::vector<GUIObject*>::iterator iter;
655 for (iter = mObjects.begin(); iter != mObjects.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200656 {
657 if ((*iter)->NotifyVarChange(varName, value))
658 LOGERR("An action handler errored on NotifyVarChange.\n");
659 }
660 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400661}
662
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500663PageSet::PageSet(const char* xmlFile)
Dees_Troy51a0e822012-09-05 15:24:24 -0400664{
that74ac6062015-03-04 22:39:34 +0100665 mResources = new ResourceManager;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200666 mCurrentPage = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400667
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500668 if (!xmlFile)
thatb63e2f92015-06-27 21:35:11 +0200669 mCurrentPage = new Page(NULL, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400670}
671
672PageSet::~PageSet()
673{
Ethan Yonker1c273312015-03-16 12:18:56 -0500674 mOverlays.clear();
Vojtech Bocekbfb63342014-02-08 00:32:31 +0100675 for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
676 delete *itr;
677
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200678 delete mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400679}
680
Ethan Yonker74db1572015-10-28 12:44:49 -0500681int PageSet::LoadLanguage(char* languageFile, ZipArchive* package)
682{
683 xml_document<> lang;
684 xml_node<>* parent;
685 xml_node<>* child;
686 std::string resource_source;
687
688 if (languageFile) {
689 printf("parsing languageFile\n");
690 lang.parse<0>(languageFile);
691 printf("parsing languageFile done\n");
692 } else {
693 return -1;
694 }
695
696 parent = lang.first_node("language");
697 if (!parent) {
698 LOGERR("Unable to locate language node in language file.\n");
699 lang.clear();
700 return -1;
701 }
702
703 child = parent->first_node("display");
704 if (child) {
705 DataManager::SetValue("tw_language_display", child->value());
706 resource_source = child->value();
707 } else {
708 LOGERR("language file does not have a display value set\n");
709 DataManager::SetValue("tw_language_display", "Not Set");
710 resource_source = languageFile;
711 }
712
713 child = parent->first_node("resources");
714 if (child)
715 mResources->LoadResources(child, package, resource_source);
716 else
717 return -1;
718 lang.clear();
719 return 0;
720}
721
722int PageSet::Load(ZipArchive* package, char* xmlFile, char* languageFile)
Dees_Troy51a0e822012-09-05 15:24:24 -0400723{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500724 xml_document<> mDoc;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200725 xml_node<>* parent;
726 xml_node<>* child;
Ethan Yonker780cd392014-07-21 15:24:39 -0500727 xml_node<>* xmltemplate;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600728 xml_node<>* xmlstyle;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500729
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500730 mDoc.parse<0>(xmlFile);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200731 parent = mDoc.first_node("recovery");
732 if (!parent)
733 parent = mDoc.first_node("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400734
Ethan Yonker63e414f2015-02-06 15:44:39 -0600735 set_scale_values(1, 1); // Reset any previous scaling values
736
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200737 // Now, let's parse the XML
Ethan Yonker63e414f2015-02-06 15:44:39 -0600738 LOGINFO("Checking resolution...\n");
739 child = parent->first_node("details");
740 if (child) {
741 xml_node<>* resolution = child->first_node("resolution");
742 if (resolution) {
743 xml_attribute<>* width_attr = resolution->first_attribute("width");
744 xml_attribute<>* height_attr = resolution->first_attribute("height");
745 xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
746 if (width_attr && height_attr && !noscale_attr) {
747 int width = atoi(width_attr->value());
748 int height = atoi(height_attr->value());
749 int offx = 0, offy = 0;
750#ifdef TW_ROUND_SCREEN
751 xml_node<>* roundscreen = child->first_node("roundscreen");
752 if (roundscreen) {
753 LOGINFO("TW_ROUND_SCREEN := true, using round screen XML settings.\n");
754 xml_attribute<>* offx_attr = roundscreen->first_attribute("offset_x");
755 xml_attribute<>* offy_attr = roundscreen->first_attribute("offset_y");
756 if (offx_attr) {
757 offx = atoi(offx_attr->value());
758 }
759 if (offy_attr) {
760 offy = atoi(offy_attr->value());
761 }
762 }
763#endif
764 if (width != 0 && height != 0) {
765 float scale_w = ((float)gr_fb_width() - ((float)offx * 2.0)) / (float)width;
766 float scale_h = ((float)gr_fb_height() - ((float)offy * 2.0)) / (float)height;
767#ifdef TW_ROUND_SCREEN
768 float scale_off_w = (float)gr_fb_width() / (float)width;
769 float scale_off_h = (float)gr_fb_height() / (float)height;
770 tw_x_offset = offx * scale_off_w;
771 tw_y_offset = offy * scale_off_h;
772#endif
773 if (scale_w != 1 || scale_h != 1) {
774 LOGINFO("Scaling theme width %fx and height %fx, offsets x: %i y: %i\n", scale_w, scale_h, tw_x_offset, tw_y_offset);
775 set_scale_values(scale_w, scale_h);
776 }
777 }
778 } else {
779 LOGINFO("XML does not contain width and height, no scaling will be applied\n");
780 }
781 } else {
782 LOGINFO("XML contains no resolution tag, no scaling will be applied.\n");
783 }
784 } else {
785 LOGINFO("XML contains no details tag, no scaling will be applied.\n");
786 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500787
788 if (languageFile)
789 LoadLanguage(languageFile, package);
790
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200791 LOGINFO("Loading resources...\n");
792 child = parent->first_node("resources");
793 if (child)
Ethan Yonker74db1572015-10-28 12:44:49 -0500794 mResources->LoadResources(child, package, "theme");
Dees_Troy51a0e822012-09-05 15:24:24 -0400795
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200796 LOGINFO("Loading variables...\n");
797 child = parent->first_node("variables");
798 if (child)
799 LoadVariables(child);
Dees_Troy51a0e822012-09-05 15:24:24 -0400800
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100801 LOGINFO("Loading mouse cursor...\n");
802 child = parent->first_node("mousecursor");
803 if(child)
804 PageManager::LoadCursorData(child);
805
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200806 LOGINFO("Loading pages...\n");
807 // This may be NULL if no templates are present
Ethan Yonker780cd392014-07-21 15:24:39 -0500808 xmltemplate = parent->first_node("templates");
809 if (xmltemplate)
810 templates.push_back(xmltemplate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400811
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600812 // Load styles if present
813 xmlstyle = parent->first_node("styles");
814 if (xmlstyle)
815 styles.push_back(xmlstyle);
816
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200817 child = parent->first_node("pages");
Ethan Yonker780cd392014-07-21 15:24:39 -0500818 if (child) {
819 if (LoadPages(child)) {
820 LOGERR("PageSet::Load returning -1\n");
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500821 mDoc.clear();
Ethan Yonker780cd392014-07-21 15:24:39 -0500822 return -1;
823 }
824 }
Vojtech Boceke979abd2015-01-12 18:29:12 +0100825
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500826 int ret = CheckInclude(package, &mDoc);
827 mDoc.clear();
828 templates.clear();
829 return ret;
Ethan Yonker780cd392014-07-21 15:24:39 -0500830}
Dees_Troy51a0e822012-09-05 15:24:24 -0400831
Ethan Yonker780cd392014-07-21 15:24:39 -0500832int PageSet::CheckInclude(ZipArchive* package, xml_document<> *parentDoc)
833{
834 xml_node<>* par;
835 xml_node<>* par2;
836 xml_node<>* chld;
837 xml_node<>* parent;
838 xml_node<>* child;
839 xml_node<>* xmltemplate;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600840 xml_node<>* xmlstyle;
Ethan Yonker780cd392014-07-21 15:24:39 -0500841 long len;
842 char* xmlFile = NULL;
843 string filename;
Vojtech Boceke979abd2015-01-12 18:29:12 +0100844 xml_document<> *doc = NULL;
Ethan Yonker780cd392014-07-21 15:24:39 -0500845
846 par = parentDoc->first_node("recovery");
847 if (!par) {
848 par = parentDoc->first_node("install");
849 }
850 if (!par) {
851 return 0;
852 }
853
854 par2 = par->first_node("include");
855 if (!par2)
856 return 0;
857 chld = par2->first_node("xmlfile");
858 while (chld != NULL) {
859 xml_attribute<>* attr = chld->first_attribute("name");
860 if (!attr)
861 break;
862
Ethan Yonker780cd392014-07-21 15:24:39 -0500863 if (!package) {
864 // We can try to load the XML directly...
Dees Troy3454ade2015-01-20 19:21:04 +0000865 filename = TWRES;
Ethan Yonker780cd392014-07-21 15:24:39 -0500866 filename += attr->value();
Ethan Yonker780cd392014-07-21 15:24:39 -0500867 } else {
868 filename += attr->value();
Ethan Yonker561c58d2015-10-05 08:48:22 -0500869 }
870 xmlFile = PageManager::LoadFileToBuffer(filename, package);
871 if (xmlFile == NULL) {
872 LOGERR("PageSet::CheckInclude unable to load '%s'\n", filename.c_str());
873 return -1;
Ethan Yonker780cd392014-07-21 15:24:39 -0500874 }
Ethan Yonker780cd392014-07-21 15:24:39 -0500875
Vojtech Boceke979abd2015-01-12 18:29:12 +0100876 doc = new xml_document<>();
877 doc->parse<0>(xmlFile);
878
879 parent = doc->first_node("recovery");
Ethan Yonker780cd392014-07-21 15:24:39 -0500880 if (!parent)
Vojtech Boceke979abd2015-01-12 18:29:12 +0100881 parent = doc->first_node("install");
Ethan Yonker780cd392014-07-21 15:24:39 -0500882
883 // Now, let's parse the XML
884 LOGINFO("Loading included resources...\n");
885 child = parent->first_node("resources");
886 if (child)
Ethan Yonker74db1572015-10-28 12:44:49 -0500887 mResources->LoadResources(child, package, "theme");
Ethan Yonker780cd392014-07-21 15:24:39 -0500888
889 LOGINFO("Loading included variables...\n");
890 child = parent->first_node("variables");
891 if (child)
892 LoadVariables(child);
893
894 LOGINFO("Loading mouse cursor...\n");
895 child = parent->first_node("mousecursor");
896 if(child)
897 PageManager::LoadCursorData(child);
898
899 LOGINFO("Loading included pages...\n");
900 // This may be NULL if no templates are present
901 xmltemplate = parent->first_node("templates");
902 if (xmltemplate)
903 templates.push_back(xmltemplate);
904
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600905 // Load styles if present
906 xmlstyle = parent->first_node("styles");
907 if (xmlstyle)
908 styles.push_back(xmlstyle);
909
Ethan Yonker780cd392014-07-21 15:24:39 -0500910 child = parent->first_node("pages");
Vojtech Boceke979abd2015-01-12 18:29:12 +0100911 if (child && LoadPages(child))
912 {
913 templates.pop_back();
914 doc->clear();
915 delete doc;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500916 free(xmlFile);
Vojtech Boceke979abd2015-01-12 18:29:12 +0100917 return -1;
918 }
Ethan Yonker780cd392014-07-21 15:24:39 -0500919
Ethan Yonker561c58d2015-10-05 08:48:22 -0500920 if (CheckInclude(package, doc)) {
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500921 doc->clear();
922 delete doc;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500923 free(xmlFile);
Ethan Yonker780cd392014-07-21 15:24:39 -0500924 return -1;
Ethan Yonker561c58d2015-10-05 08:48:22 -0500925 }
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500926 doc->clear();
927 delete doc;
928 free(xmlFile);
Ethan Yonker780cd392014-07-21 15:24:39 -0500929
930 chld = chld->next_sibling("xmlfile");
931 }
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500932
Ethan Yonker780cd392014-07-21 15:24:39 -0500933 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400934}
935
936int PageSet::SetPage(std::string page)
937{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200938 Page* tmp = FindPage(page);
939 if (tmp)
940 {
941 if (mCurrentPage) mCurrentPage->SetPageFocus(0);
942 mCurrentPage = tmp;
943 mCurrentPage->SetPageFocus(1);
944 mCurrentPage->NotifyVarChange("", "");
945 return 0;
946 }
947 else
948 {
949 LOGERR("Unable to locate page (%s)\n", page.c_str());
950 }
951 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400952}
953
954int PageSet::SetOverlay(Page* page)
955{
Ethan Yonker1c273312015-03-16 12:18:56 -0500956 if (page) {
957 if (mOverlays.size() >= 10) {
958 LOGERR("Too many overlays requested, max is 10.\n");
959 return -1;
960 }
Matt Mowerd411f8d2015-04-09 16:04:12 -0500961
962 std::vector<Page*>::iterator iter;
963 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
964 if ((*iter)->GetName() == page->GetName()) {
965 mOverlays.erase(iter);
966 // SetOverlay() is (and should stay) the only function which
967 // adds to mOverlays. Then, each page can appear at most once.
968 break;
969 }
970 }
971
Ethan Yonker1c273312015-03-16 12:18:56 -0500972 page->SetPageFocus(1);
973 page->NotifyVarChange("", "");
974
975 if (!mOverlays.empty())
976 mOverlays.back()->SetPageFocus(0);
977
978 mOverlays.push_back(page);
979 } else {
980 if (!mOverlays.empty()) {
981 mOverlays.back()->SetPageFocus(0);
982 mOverlays.pop_back();
983 if (!mOverlays.empty())
984 mOverlays.back()->SetPageFocus(1);
985 else if (mCurrentPage)
986 mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
987 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200988 }
989 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400990}
991
that74ac6062015-03-04 22:39:34 +0100992const ResourceManager* PageSet::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -0400993{
that74ac6062015-03-04 22:39:34 +0100994 return mResources;
Dees_Troy51a0e822012-09-05 15:24:24 -0400995}
996
997Page* PageSet::FindPage(std::string name)
998{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200999 std::vector<Page*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001000
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001001 for (iter = mPages.begin(); iter != mPages.end(); iter++)
1002 {
1003 if (name == (*iter)->GetName())
1004 return (*iter);
1005 }
1006 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001007}
1008
1009int PageSet::LoadVariables(xml_node<>* vars)
1010{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001011 xml_node<>* child;
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001012 xml_attribute<> *name, *value, *persist;
1013 int p;
Dees_Troy51a0e822012-09-05 15:24:24 -04001014
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001015 child = vars->first_node("variable");
1016 while (child)
1017 {
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001018 name = child->first_attribute("name");
1019 value = child->first_attribute("value");
1020 persist = child->first_attribute("persist");
1021 if(name && value)
1022 {
Ethan Yonker751a85e2014-12-12 16:59:10 -06001023 if (strcmp(name->value(), "tw_x_offset") == 0) {
1024 tw_x_offset = atoi(value->value());
1025 child = child->next_sibling("variable");
1026 continue;
1027 }
1028 if (strcmp(name->value(), "tw_y_offset") == 0) {
1029 tw_y_offset = atoi(value->value());
1030 child = child->next_sibling("variable");
1031 continue;
1032 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001033 p = persist ? atoi(persist->value()) : 0;
Ethan Yonker96acb3d2014-08-05 09:20:30 -05001034 string temp = value->value();
1035 string valstr = gui_parse_text(temp);
1036
1037 if (valstr.find("+") != string::npos) {
1038 string val1str = valstr;
1039 val1str = val1str.substr(0, val1str.find('+'));
1040 string val2str = valstr;
1041 val2str = val2str.substr(val2str.find('+') + 1, string::npos);
1042 int val1 = atoi(val1str.c_str());
1043 int val2 = atoi(val2str.c_str());
1044 int val = val1 + val2;
1045
1046 DataManager::SetValue(name->value(), val, p);
1047 } else if (valstr.find("-") != string::npos) {
1048 string val1str = valstr;
1049 val1str = val1str.substr(0, val1str.find('-'));
1050 string val2str = valstr;
1051 val2str = val2str.substr(val2str.find('-') + 1, string::npos);
1052 int val1 = atoi(val1str.c_str());
1053 int val2 = atoi(val2str.c_str());
1054 int val = val1 - val2;
1055
1056 DataManager::SetValue(name->value(), val, p);
1057 } else {
1058 DataManager::SetValue(name->value(), valstr, p);
1059 }
Vojtech Bocek81c29dc2013-12-07 23:02:09 +01001060 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001061
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001062 child = child->next_sibling("variable");
1063 }
1064 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
Ethan Yonker780cd392014-07-21 15:24:39 -05001067int PageSet::LoadPages(xml_node<>* pages)
Dees_Troy51a0e822012-09-05 15:24:24 -04001068{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001069 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -04001070
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001071 if (!pages)
1072 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001073
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001074 child = pages->first_node("page");
1075 while (child != NULL)
1076 {
Ethan Yonker780cd392014-07-21 15:24:39 -05001077 Page* page = new Page(child, &templates);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001078 if (page->GetName().empty())
1079 {
1080 LOGERR("Unable to process load page\n");
1081 delete page;
1082 }
1083 else
1084 {
1085 mPages.push_back(page);
1086 }
1087 child = child->next_sibling("page");
1088 }
1089 if (mPages.size() > 0)
1090 return 0;
1091 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001092}
1093
1094int PageSet::IsCurrentPage(Page* page)
1095{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001096 return ((mCurrentPage && mCurrentPage == page) ? 1 : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001097}
1098
that10ae24f2015-12-26 20:53:51 +01001099std::string PageSet::GetCurrentPage() const
1100{
1101 return mCurrentPage ? mCurrentPage->GetName() : "";
1102}
1103
Dees_Troy51a0e822012-09-05 15:24:24 -04001104int PageSet::Render(void)
1105{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001106 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001107
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001108 ret = (mCurrentPage ? mCurrentPage->Render() : -1);
1109 if (ret < 0)
1110 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001111
1112 std::vector<Page*>::iterator iter;
1113
1114 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1115 ret = ((*iter) ? (*iter)->Render() : -1);
1116 if (ret < 0)
1117 return ret;
1118 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001119 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001120}
1121
1122int PageSet::Update(void)
1123{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001124 int ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001125
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001126 ret = (mCurrentPage ? mCurrentPage->Update() : -1);
1127 if (ret < 0 || ret > 1)
1128 return ret;
Ethan Yonker1c273312015-03-16 12:18:56 -05001129
1130 std::vector<Page*>::iterator iter;
1131
1132 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
1133 ret = ((*iter) ? (*iter)->Update() : -1);
1134 if (ret < 0)
1135 return ret;
1136 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001137 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001138}
1139
1140int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
1141{
Ethan Yonker1c273312015-03-16 12:18:56 -05001142 if (!mOverlays.empty())
1143 return mOverlays.back()->NotifyTouch(state, x, y);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001144
1145 return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001146}
1147
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001148int PageSet::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001149{
Ethan Yonker1c273312015-03-16 12:18:56 -05001150 if (!mOverlays.empty())
1151 return mOverlays.back()->NotifyKey(key, down);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001152
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001153 return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001154}
1155
that8834a0f2016-01-05 23:29:30 +01001156int PageSet::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001157{
Ethan Yonker1c273312015-03-16 12:18:56 -05001158 if (!mOverlays.empty())
that8834a0f2016-01-05 23:29:30 +01001159 return mOverlays.back()->NotifyCharInput(ch);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001160
that8834a0f2016-01-05 23:29:30 +01001161 return (mCurrentPage ? mCurrentPage->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001162}
1163
1164int PageSet::SetKeyBoardFocus(int inFocus)
1165{
Ethan Yonker1c273312015-03-16 12:18:56 -05001166 if (!mOverlays.empty())
1167 return mOverlays.back()->SetKeyBoardFocus(inFocus);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001168
1169 return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001170}
1171
1172int PageSet::NotifyVarChange(std::string varName, std::string value)
1173{
Ethan Yonker1c273312015-03-16 12:18:56 -05001174 std::vector<Page*>::iterator iter;
1175
1176 for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
1177 (*iter)->NotifyVarChange(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001178
1179 return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001180}
1181
Ethan Yonker74db1572015-10-28 12:44:49 -05001182void PageSet::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1183{
1184 mResources->AddStringResource(resource_source, resource_name, value);
1185}
1186
Ethan Yonker561c58d2015-10-05 08:48:22 -05001187char* PageManager::LoadFileToBuffer(std::string filename, ZipArchive* package) {
1188 size_t len;
1189 char* buffer = NULL;
1190
1191 if (!package) {
1192 // We can try to load the XML directly...
1193 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' directly\n", filename.c_str());
1194 struct stat st;
1195 if(stat(filename.c_str(),&st) != 0) {
1196 // This isn't always an error, sometimes we request files that don't exist.
1197 return NULL;
1198 }
1199
1200 len = (size_t)st.st_size;
1201
1202 buffer = (char*) malloc(len + 1);
1203 if (!buffer) {
1204 LOGERR("PageManager::LoadFileToBuffer failed to malloc\n");
1205 return NULL;
1206 }
1207
1208 int fd = open(filename.c_str(), O_RDONLY);
1209 if (fd == -1) {
1210 LOGERR("PageManager::LoadFileToBuffer failed to open '%s' - (%s)\n", filename.c_str(), strerror(errno));
1211 free(buffer);
1212 return NULL;
1213 }
1214
1215 if (read(fd, buffer, len) < 0) {
1216 LOGERR("PageManager::LoadFileToBuffer failed to read '%s' - (%s)\n", filename.c_str(), strerror(errno));
1217 free(buffer);
1218 close(fd);
1219 return NULL;
1220 }
1221 close(fd);
1222 } else {
1223 LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
1224 const ZipEntry* zipentry = mzFindZipEntry(package, filename.c_str());
1225 if (zipentry == NULL) {
1226 LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
1227 return NULL;
1228 }
1229
1230 // Allocate the buffer for the file
1231 len = mzGetZipEntryUncompLen(zipentry);
1232 buffer = (char*) malloc(len + 1);
1233 if (!buffer)
1234 return NULL;
1235
1236 if (!mzExtractZipEntryToBuffer(package, zipentry, (unsigned char*) buffer)) {
1237 LOGERR("Unable to extract '%s'\n", filename.c_str());
1238 free(buffer);
1239 return NULL;
1240 }
1241 }
1242 // NULL-terminate the string
1243 buffer[len] = 0x00;
1244 return buffer;
1245}
1246
Ethan Yonker74db1572015-10-28 12:44:49 -05001247void PageManager::LoadLanguageListDir(string dir) {
1248 if (!TWFunc::Path_Exists(dir)) {
1249 LOGERR("LoadLanguageListDir '%s' path not found\n", dir.c_str());
1250 return;
1251 }
1252
1253 DIR *d = opendir(dir.c_str());
1254 struct dirent *p;
1255
1256 if (d == NULL) {
1257 LOGERR("LoadLanguageListDir error opening dir: '%s', %s\n", dir.c_str(), strerror(errno));
1258 return;
1259 }
1260
1261 while ((p = readdir(d))) {
1262 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..") || strlen(p->d_name) < 5)
1263 continue;
1264
1265 string file = p->d_name;
1266 if (file.substr(strlen(p->d_name) - 4) != ".xml")
1267 continue;
1268 string path = dir + p->d_name;
1269 string file_no_extn = file.substr(0, strlen(p->d_name) - 4);
1270 struct language_struct language_entry;
1271 language_entry.filename = file_no_extn;
1272 char* xmlFile = PageManager::LoadFileToBuffer(dir + p->d_name, NULL);
1273 if (xmlFile == NULL) {
1274 LOGERR("LoadLanguageListDir unable to load '%s'\n", language_entry.filename.c_str());
1275 continue;
1276 }
1277 xml_document<> *doc = new xml_document<>();
1278 doc->parse<0>(xmlFile);
1279
1280 xml_node<>* parent = doc->first_node("language");
1281 if (!parent) {
1282 LOGERR("Invalid language XML file '%s'\n", language_entry.filename.c_str());
1283 } else {
1284 xml_node<>* child = parent->first_node("display");
1285 if (child) {
1286 language_entry.displayvalue = child->value();
1287 } else {
1288 LOGERR("No display value for '%s'\n", language_entry.filename.c_str());
1289 language_entry.displayvalue = language_entry.filename;
1290 }
1291 Language_List.push_back(language_entry);
1292 }
1293 doc->clear();
1294 delete doc;
1295 free(xmlFile);
1296 }
1297 closedir(d);
1298}
1299
1300void PageManager::LoadLanguageList(ZipArchive* package) {
1301 Language_List.clear();
1302 if (TWFunc::Path_Exists(TWRES "customlanguages"))
1303 TWFunc::removeDir(TWRES "customlanguages", true);
1304 if (package) {
1305 TWFunc::Recursive_Mkdir(TWRES "customlanguages");
1306 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
1307 mzExtractRecursive(package, "languages", TWRES "customlanguages/", &timestamp, NULL, NULL, NULL);
1308 LoadLanguageListDir(TWRES "customlanguages/");
1309 } else {
1310 LoadLanguageListDir(TWRES "languages/");
1311 }
1312}
1313
1314void PageManager::LoadLanguage(string filename) {
1315 string actual_filename;
1316 if (TWFunc::Path_Exists(TWRES "customlanguages/" + filename + ".xml"))
1317 actual_filename = TWRES "customlanguages/" + filename + ".xml";
1318 else
1319 actual_filename = TWRES "languages/" + filename + ".xml";
1320 char* xmlFile = PageManager::LoadFileToBuffer(actual_filename, NULL);
1321 if (xmlFile == NULL)
1322 LOGERR("Unable to load '%s'\n", actual_filename.c_str());
1323 else {
1324 mCurrentSet->LoadLanguage(xmlFile, NULL);
1325 free(xmlFile);
1326 }
1327 PartitionManager.Translate_Partition_Display_Names();
1328}
1329
Dees_Troy51a0e822012-09-05 15:24:24 -04001330int PageManager::LoadPackage(std::string name, std::string package, std::string startpage)
1331{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001332 int fd;
1333 ZipArchive zip, *pZip = NULL;
1334 long len;
1335 char* xmlFile = NULL;
Ethan Yonker74db1572015-10-28 12:44:49 -05001336 char* languageFile = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001337 PageSet* pageSet = NULL;
1338 int ret;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001339 MemMapping map;
Dees_Troy51a0e822012-09-05 15:24:24 -04001340
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001341 mReloadTheme = false;
1342 mStartPage = startpage;
1343
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001344 // Open the XML file
1345 LOGINFO("Loading package: %s (%s)\n", name.c_str(), package.c_str());
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001346 if (package.size() > 4 && package.substr(package.size() - 4) != ".zip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001347 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001348 LOGINFO("Load XML directly\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001349 tw_x_offset = TW_X_OFFSET;
1350 tw_y_offset = TW_Y_OFFSET;
Ethan Yonker74db1572015-10-28 12:44:49 -05001351 LoadLanguageList(NULL);
1352 languageFile = LoadFileToBuffer(TWRES "languages/en.xml", NULL);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001353 }
1354 else
1355 {
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001356 LOGINFO("Loading zip theme\n");
Ethan Yonker751a85e2014-12-12 16:59:10 -06001357 tw_x_offset = 0;
1358 tw_y_offset = 0;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001359 if (!TWFunc::Path_Exists(package))
1360 return -1;
1361 if (sysMapFile(package.c_str(), &map) != 0) {
1362 LOGERR("Failed to map '%s'\n", package.c_str());
Ethan Yonker561c58d2015-10-05 08:48:22 -05001363 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001364 }
1365 if (mzOpenZipArchive(map.addr, map.length, &zip)) {
1366 LOGERR("Unable to open zip archive '%s'\n", package.c_str());
1367 sysReleaseMap(&map);
Ethan Yonker561c58d2015-10-05 08:48:22 -05001368 goto error;
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001369 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001370 pZip = &zip;
Ethan Yonker561c58d2015-10-05 08:48:22 -05001371 package = "ui.xml";
Ethan Yonker74db1572015-10-28 12:44:49 -05001372 LoadLanguageList(pZip);
1373 languageFile = LoadFileToBuffer("languages/en.xml", pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001374 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001375
Ethan Yonker561c58d2015-10-05 08:48:22 -05001376 xmlFile = LoadFileToBuffer(package, pZip);
1377 if (xmlFile == NULL) {
1378 goto error;
1379 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001380
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001381 // Before loading, mCurrentSet must be the loading package so we can find resources
1382 pageSet = mCurrentSet;
1383 mCurrentSet = new PageSet(xmlFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001384 ret = mCurrentSet->Load(pZip, xmlFile, languageFile);
1385 if (languageFile) {
1386 free(languageFile);
1387 languageFile = NULL;
1388 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001389 if (ret == 0)
1390 {
1391 mCurrentSet->SetPage(startpage);
1392 mPageSets.insert(std::pair<std::string, PageSet*>(name, mCurrentSet));
1393 }
1394 else
1395 {
1396 LOGERR("Package %s failed to load.\n", name.c_str());
1397 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001398
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001399 // The first successful package we loaded is the base
1400 if (mBaseSet == NULL)
1401 mBaseSet = mCurrentSet;
1402
1403 mCurrentSet = pageSet;
1404
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001405 if (pZip) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001406 mzCloseZipArchive(pZip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001407 sysReleaseMap(&map);
1408 }
Ethan Yonker561c58d2015-10-05 08:48:22 -05001409 free(xmlFile);
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 if (languageFile)
1411 free(languageFile);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001412 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001413
1414error:
Ethan Yonker561c58d2015-10-05 08:48:22 -05001415 // Sometimes we get here without a real error
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001416 if (pZip) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001417 mzCloseZipArchive(pZip);
Ethan Yonkera2dc2f22014-11-08 08:13:40 -06001418 sysReleaseMap(&map);
1419 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001420 if (xmlFile)
1421 free(xmlFile);
1422 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001423}
1424
1425PageSet* PageManager::FindPackage(std::string name)
1426{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001427 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001428
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001429 iter = mPageSets.find(name);
1430 if (iter != mPageSets.end())
1431 return (*iter).second;
1432
1433 LOGERR("Unable to locate package %s\n", name.c_str());
1434 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -04001435}
1436
1437PageSet* PageManager::SelectPackage(std::string name)
1438{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001439 LOGINFO("Switching packages (%s)\n", name.c_str());
1440 PageSet* tmp;
Dees_Troy51a0e822012-09-05 15:24:24 -04001441
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001442 tmp = FindPackage(name);
1443 if (tmp)
Vojtech Bocek07220562014-02-08 02:05:33 +01001444 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001445 mCurrentSet = tmp;
Vojtech Bocek07220562014-02-08 02:05:33 +01001446 mCurrentSet->NotifyVarChange("", "");
1447 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001448 else
1449 LOGERR("Unable to find package.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001450
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001451 return mCurrentSet;
Dees_Troy51a0e822012-09-05 15:24:24 -04001452}
1453
1454int PageManager::ReloadPackage(std::string name, std::string package)
1455{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001456 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001457
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001458 mReloadTheme = false;
1459
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001460 iter = mPageSets.find(name);
1461 if (iter == mPageSets.end())
1462 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001463
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001464 if(mMouseCursor)
1465 mMouseCursor->ResetData(gr_fb_width(), gr_fb_height());
1466
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001467 PageSet* set = (*iter).second;
1468 mPageSets.erase(iter);
Dees_Troy51a0e822012-09-05 15:24:24 -04001469
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001470 if (LoadPackage(name, package, mStartPage) != 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001471 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001472 LOGINFO("Failed to load package '%s'.\n", package.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001473 mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
1474 return -1;
1475 }
1476 if (mCurrentSet == set)
1477 SelectPackage(name);
Vojtech Bocekbfb63342014-02-08 00:32:31 +01001478 if (mBaseSet == set)
1479 mBaseSet = mCurrentSet;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001480 delete set;
Ethan Yonker74db1572015-10-28 12:44:49 -05001481 GUIConsole::Translate_Now();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001482 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001483}
1484
1485void PageManager::ReleasePackage(std::string name)
1486{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001487 std::map<std::string, PageSet*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -04001488
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001489 iter = mPageSets.find(name);
1490 if (iter == mPageSets.end())
1491 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001492
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001493 PageSet* set = (*iter).second;
1494 mPageSets.erase(iter);
1495 delete set;
1496 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001497}
1498
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001499int PageManager::RunReload() {
1500 int ret_val = 0;
1501 std::string theme_path;
1502
1503 if (!mReloadTheme)
1504 return 0;
1505
1506 mReloadTheme = false;
1507 theme_path = DataManager::GetSettingsStoragePath();
1508 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1509 LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
1510 ret_val = 1;
1511 }
1512
1513 theme_path += "/TWRP/theme/ui.zip";
1514 if (ret_val != 0 || ReloadPackage("TWRP", theme_path) != 0)
1515 {
1516 // Loading the custom theme failed - try loading the stock theme
1517 LOGINFO("Attempting to reload stock theme...\n");
1518 if (ReloadPackage("TWRP", TWRES "ui.xml"))
1519 {
1520 LOGERR("Failed to load base packages.\n");
1521 ret_val = 1;
1522 }
1523 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001524 if (ret_val == 0) {
1525 if (DataManager::GetStrValue("tw_language") != "en.xml") {
1526 LOGINFO("Loading language '%s'\n", DataManager::GetStrValue("tw_language").c_str());
1527 LoadLanguage(DataManager::GetStrValue("tw_language"));
1528 }
1529 }
1530
1531 // This makes the console re-translate
1532 last_message_count = 0;
1533 gConsole.clear();
1534 gConsoleColor.clear();
1535
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001536 return ret_val;
1537}
1538
1539void PageManager::RequestReload() {
1540 mReloadTheme = true;
1541}
1542
Dees_Troy51a0e822012-09-05 15:24:24 -04001543int PageManager::ChangePage(std::string name)
1544{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001545 DataManager::SetValue("tw_operation_state", 0);
1546 int ret = (mCurrentSet ? mCurrentSet->SetPage(name) : -1);
1547 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001548}
1549
that10ae24f2015-12-26 20:53:51 +01001550std::string PageManager::GetCurrentPage()
1551{
1552 return mCurrentSet ? mCurrentSet->GetCurrentPage() : "";
1553}
1554
Dees_Troy51a0e822012-09-05 15:24:24 -04001555int PageManager::ChangeOverlay(std::string name)
1556{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001557 if (name.empty())
1558 return mCurrentSet->SetOverlay(NULL);
1559 else
1560 {
1561 Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL;
1562 return mCurrentSet->SetOverlay(page);
1563 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001564}
1565
that74ac6062015-03-04 22:39:34 +01001566const ResourceManager* PageManager::GetResources()
Dees_Troy51a0e822012-09-05 15:24:24 -04001567{
that74ac6062015-03-04 22:39:34 +01001568 return (mCurrentSet ? mCurrentSet->GetResources() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -04001569}
1570
Dees_Troy51a0e822012-09-05 15:24:24 -04001571int PageManager::IsCurrentPage(Page* page)
1572{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001573 return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001574}
1575
1576int PageManager::Render(void)
1577{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001578 if(blankTimer.isScreenOff())
1579 return 0;
1580
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001581 int res = (mCurrentSet ? mCurrentSet->Render() : -1);
1582 if(mMouseCursor)
1583 mMouseCursor->Render();
1584 return res;
1585}
1586
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001587HardwareKeyboard *PageManager::GetHardwareKeyboard()
1588{
1589 if(!mHardwareKeyboard)
1590 mHardwareKeyboard = new HardwareKeyboard();
1591 return mHardwareKeyboard;
1592}
1593
Ethan Yonker21ff02a2015-02-18 14:35:00 -06001594xml_node<>* PageManager::FindStyle(std::string name)
1595{
1596 for (std::vector<xml_node<>*>::iterator itr = mCurrentSet->styles.begin(); itr != mCurrentSet->styles.end(); itr++) {
1597 xml_node<>* node = (*itr)->first_node("style");
1598
1599 while (node) {
1600 if (!node->first_attribute("name"))
1601 continue;
1602
1603 if (name == node->first_attribute("name")->value())
1604 return node;
1605 node = node->next_sibling("style");
1606 }
1607 }
1608 return NULL;
1609}
1610
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001611MouseCursor *PageManager::GetMouseCursor()
1612{
1613 if(!mMouseCursor)
1614 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1615 return mMouseCursor;
1616}
1617
1618void PageManager::LoadCursorData(xml_node<>* node)
1619{
1620 if(!mMouseCursor)
1621 mMouseCursor = new MouseCursor(gr_fb_width(), gr_fb_height());
1622
1623 mMouseCursor->LoadData(node);
Dees_Troy51a0e822012-09-05 15:24:24 -04001624}
1625
1626int PageManager::Update(void)
1627{
thatfb759d42015-01-11 12:16:53 +01001628 if(blankTimer.isScreenOff())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001629 return 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001630
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -05001631 if (RunReload())
1632 return -2;
1633
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001634 int res = (mCurrentSet ? mCurrentSet->Update() : -1);
1635
1636 if(mMouseCursor)
1637 {
1638 int c_res = mMouseCursor->Update();
1639 if(c_res > res)
1640 res = c_res;
1641 }
1642 return res;
Dees_Troy51a0e822012-09-05 15:24:24 -04001643}
1644
1645int PageManager::NotifyTouch(TOUCH_STATE state, int x, int y)
1646{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001647 return (mCurrentSet ? mCurrentSet->NotifyTouch(state, x, y) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001648}
1649
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001650int PageManager::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -04001651{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001652 return (mCurrentSet ? mCurrentSet->NotifyKey(key, down) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001653}
1654
that8834a0f2016-01-05 23:29:30 +01001655int PageManager::NotifyCharInput(int ch)
Dees_Troy51a0e822012-09-05 15:24:24 -04001656{
that8834a0f2016-01-05 23:29:30 +01001657 return (mCurrentSet ? mCurrentSet->NotifyCharInput(ch) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001658}
1659
1660int PageManager::SetKeyBoardFocus(int inFocus)
1661{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001662 return (mCurrentSet ? mCurrentSet->SetKeyBoardFocus(inFocus) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001663}
1664
1665int PageManager::NotifyVarChange(std::string varName, std::string value)
1666{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001667 return (mCurrentSet ? mCurrentSet->NotifyVarChange(varName, value) : -1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001668}
1669
Ethan Yonker74db1572015-10-28 12:44:49 -05001670void PageManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
1671{
1672 if (mCurrentSet)
1673 mCurrentSet->AddStringResource(resource_source, resource_name, value);
1674}
1675
Dees_Troy51a0e822012-09-05 15:24:24 -04001676extern "C" void gui_notifyVarChange(const char *name, const char* value)
1677{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001678 if (!gGuiRunning)
1679 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001680
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001681 PageManager::NotifyVarChange(name, value);
Dees_Troy51a0e822012-09-05 15:24:24 -04001682}