blob: 41655c376168ba1e2f354fea1c4038c9c59f46f1 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// resource.cpp - Source to manage GUI resources
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -04007#include <unistd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -04008
9#include <string>
10#include <sstream>
11#include <iostream>
12#include <iomanip>
13
thatb2e8f672015-03-05 20:25:39 +010014#include "../minzip/Zip.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040015extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000016#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040017#include "../minuitwrp/minui.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060018#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040019}
20
21#include "rapidxml.hpp"
22#include "objects.hpp"
23
24#define TMP_RESOURCE_NAME "/tmp/extract.bin"
25
26Resource::Resource(xml_node<>* node, ZipArchive* pZip)
27{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020028 if (node && node->first_attribute("name"))
29 mName = node->first_attribute("name")->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040030}
31
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020032int Resource::ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile)
Dees_Troy51a0e822012-09-05 15:24:24 -040033{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 if (!pZip)
35 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040036
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020037 std::string src = folderName + "/" + fileName + fileExtn;
Dees_Troy51a0e822012-09-05 15:24:24 -040038
39 const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020040 if (binary == NULL) {
41 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040042 }
43
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020044 unlink(destFile.c_str());
45 int fd = creat(destFile.c_str(), 0666);
46 if (fd < 0)
47 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040048
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020049 int ret = 0;
50 if (!mzExtractZipEntryToFile(pZip, binary, fd))
51 ret = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 close(fd);
54 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -040055}
56
Ethan Yonker63e414f2015-02-06 15:44:39 -060057void Resource::LoadImage(ZipArchive* pZip, std::string file, gr_surface* source)
58{
59 if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0)
60 {
61 res_create_surface(TMP_RESOURCE_NAME, source);
62 unlink(TMP_RESOURCE_NAME);
63 }
64 else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0)
65 {
66 // JPG includes the .jpg extension in the filename so extension should be blank
67 res_create_surface(TMP_RESOURCE_NAME, source);
68 unlink(TMP_RESOURCE_NAME);
69 }
70 else if (!pZip)
71 {
72 // File name in xml may have included .png so try without adding .png
73 res_create_surface(file.c_str(), source);
74 }
75}
76
77void Resource::CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect)
78{
79 if (!source) {
80 *destination = NULL;
81 return;
82 }
83 if (get_scale_w() != 0 && get_scale_h() != 0) {
84 float scale_w = get_scale_w(), scale_h = get_scale_h();
85 if (retain_aspect) {
86 if (scale_w < scale_h)
87 scale_h = scale_w;
88 else
89 scale_w = scale_h;
90 }
91 if (res_scale_surface(source, destination, scale_w, scale_h)) {
92 LOGINFO("Error scaling image, using regular size.\n");
93 *destination = source;
94 }
95 } else {
96 *destination = source;
97 }
98}
99
Dees_Troy51a0e822012-09-05 15:24:24 -0400100FontResource::FontResource(xml_node<>* node, ZipArchive* pZip)
101 : Resource(node, pZip)
102{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200103 std::string file;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200104 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 mFont = NULL;
107 if (!node)
108 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400109
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200110 attr = node->first_attribute("filename");
111 if (!attr)
112 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200114 file = attr->value();
115
116#ifndef TW_DISABLE_TTF
117 if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 {
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200119 m_type = TYPE_TTF;
120
121 attr = node->first_attribute("size");
122 if(!attr)
123 return;
124
Ethan Yonker63e414f2015-02-06 15:44:39 -0600125 int size = scale_theme_min(atoi(attr->value()));
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200126 int dpi = 300;
127
128 attr = node->first_attribute("dpi");
129 if(attr)
130 dpi = atoi(attr->value());
131
132 if (ExtractResource(pZip, "fonts", file, "", TMP_RESOURCE_NAME) == 0)
133 {
134 mFont = gr_ttf_loadFont(TMP_RESOURCE_NAME, size, dpi);
135 unlink(TMP_RESOURCE_NAME);
136 }
137 else
138 {
Dees Troy3454ade2015-01-20 19:21:04 +0000139 file = std::string(TWRES "fonts/") + file;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200140 mFont = gr_ttf_loadFont(file.c_str(), size, dpi);
141 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 }
143 else
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200144#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 {
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200146 m_type = TYPE_TWRP;
147
148 if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0)
149 {
150 attr = node->first_attribute("fallback");
151 if (!attr)
152 return;
153
154 file = attr->value();
155 }
156
157 if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0)
158 {
159 mFont = gr_loadFont(TMP_RESOURCE_NAME);
160 unlink(TMP_RESOURCE_NAME);
161 }
162 else
163 {
164 mFont = gr_loadFont(file.c_str());
165 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400167}
168
169FontResource::~FontResource()
170{
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200171 if(mFont)
172 {
173#ifndef TW_DISABLE_TTF
174 if(m_type == TYPE_TTF)
175 gr_ttf_freeFont(mFont);
176 else
177#endif
178 gr_freeFont(mFont);
179 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400180}
181
that5267a212015-05-06 23:45:57 +0200182ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400183 : Resource(node, pZip)
184{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 std::string file;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600186 gr_surface temp_surface = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 mSurface = NULL;
Ethan Yonker619a7212014-12-03 16:47:37 -0600189 if (!node) {
190 LOGERR("ImageResource node is NULL\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 return;
Ethan Yonker619a7212014-12-03 16:47:37 -0600192 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400193
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 if (node->first_attribute("filename"))
195 file = node->first_attribute("filename")->value();
Ethan Yonker63e414f2015-02-06 15:44:39 -0600196 else {
197 LOGERR("No filename specified for image resource.\n");
198 return;
199 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400200
that5267a212015-05-06 23:45:57 +0200201 bool retain_aspect = (node->first_attribute("retainaspect") != NULL);
202 // the value does not matter, if retainaspect is present, we assume that we want to retain it
Ethan Yonker63e414f2015-02-06 15:44:39 -0600203 LoadImage(pZip, file, &temp_surface);
204 CheckAndScaleImage(temp_surface, &mSurface, retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -0400205}
206
207ImageResource::~ImageResource()
208{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 if (mSurface)
210 res_free_surface(mSurface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400211}
212
that5267a212015-05-06 23:45:57 +0200213AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 : Resource(node, pZip)
215{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 std::string file;
217 int fileNum = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 if (!node)
220 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400221
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200222 if (node->first_attribute("filename"))
223 file = node->first_attribute("filename")->value();
Ethan Yonker63e414f2015-02-06 15:44:39 -0600224 else {
225 LOGERR("No filename specified for image resource.\n");
226 return;
227 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400228
that5267a212015-05-06 23:45:57 +0200229 bool retain_aspect = (node->first_attribute("retainaspect") != NULL);
230 // the value does not matter, if retainaspect is present, we assume that we want to retain it
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 for (;;)
232 {
233 std::ostringstream fileName;
234 fileName << file << std::setfill ('0') << std::setw (3) << fileNum;
Dees_Troy51a0e822012-09-05 15:24:24 -0400235
Ethan Yonker63e414f2015-02-06 15:44:39 -0600236 gr_surface surface, temp_surface = NULL;
237 LoadImage(pZip, fileName.str(), &temp_surface);
238 CheckAndScaleImage(temp_surface, &surface, retain_aspect);
239 if (surface) {
240 mSurfaces.push_back(surface);
241 fileNum++;
242 } else
243 break; // Done loading animation images
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400245}
246
247AnimationResource::~AnimationResource()
248{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 std::vector<gr_surface>::iterator it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it)
252 res_free_surface(*it);
253
254 mSurfaces.clear();
Dees_Troy51a0e822012-09-05 15:24:24 -0400255}
256
that74ac6062015-03-04 22:39:34 +0100257FontResource* ResourceManager::FindFont(const std::string& name) const
Dees_Troy51a0e822012-09-05 15:24:24 -0400258{
that74ac6062015-03-04 22:39:34 +0100259 for (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it)
260 if (name == (*it)->GetName())
261 return *it;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400263}
264
that74ac6062015-03-04 22:39:34 +0100265ImageResource* ResourceManager::FindImage(const std::string& name) const
Dees_Troy51a0e822012-09-05 15:24:24 -0400266{
that74ac6062015-03-04 22:39:34 +0100267 for (std::vector<ImageResource*>::const_iterator it = mImages.begin(); it != mImages.end(); ++it)
268 if (name == (*it)->GetName())
269 return *it;
270 return NULL;
271}
272
273AnimationResource* ResourceManager::FindAnimation(const std::string& name) const
274{
275 for (std::vector<AnimationResource*>::const_iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
276 if (name == (*it)->GetName())
277 return *it;
278 return NULL;
279}
280
thatb2e8f672015-03-05 20:25:39 +0100281std::string ResourceManager::FindString(const std::string& name) const
282{
283 std::map<std::string, std::string>::const_iterator it = mStrings.find(name);
284 if (it != mStrings.end())
285 return it->second;
286 return "[" + name + ("]");
287}
288
that74ac6062015-03-04 22:39:34 +0100289ResourceManager::ResourceManager()
290{
Ethan Yonker780cd392014-07-21 15:24:39 -0500291}
292
293void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
294{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (!resList)
296 return;
that0f425062015-03-04 23:05:00 +0100297
298 for (xml_node<>* child = resList->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 {
that0f425062015-03-04 23:05:00 +0100300 std::string type = child->name();
301 if (type == "resource") {
302 // legacy format : <resource type="...">
303 xml_attribute<>* attr = child->first_attribute("type");
304 type = attr ? attr->value() : "*unspecified*";
305 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400306
that74ac6062015-03-04 22:39:34 +0100307 bool error = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 if (type == "font")
309 {
that74ac6062015-03-04 22:39:34 +0100310 FontResource* res = new FontResource(child, pZip);
311 if (res->GetResource())
312 mFonts.push_back(res);
313 else {
314 error = true;
315 delete res;
316 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 }
318 else if (type == "image")
319 {
that5267a212015-05-06 23:45:57 +0200320 ImageResource* res = new ImageResource(child, pZip);
that74ac6062015-03-04 22:39:34 +0100321 if (res->GetResource())
322 mImages.push_back(res);
323 else {
324 error = true;
325 delete res;
326 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200327 }
328 else if (type == "animation")
329 {
that5267a212015-05-06 23:45:57 +0200330 AnimationResource* res = new AnimationResource(child, pZip);
that74ac6062015-03-04 22:39:34 +0100331 if (res->GetResourceCount())
332 mAnimations.push_back(res);
333 else {
334 error = true;
335 delete res;
336 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 }
thatb2e8f672015-03-05 20:25:39 +0100338 else if (type == "string")
339 {
340 if (xml_attribute<>* attr = child->first_attribute("name"))
341 mStrings[attr->value()] = child->value();
342 else
343 error = true;
344 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 else
346 {
347 LOGERR("Resource type (%s) not supported.\n", type.c_str());
that74ac6062015-03-04 22:39:34 +0100348 error = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200349 }
thatf6ed8fc2015-02-14 20:23:16 +0100350
that74ac6062015-03-04 22:39:34 +0100351 if (error)
thatf74ac872015-01-18 12:00:02 +0100352 {
353 std::string res_name;
354 if (child->first_attribute("name"))
355 res_name = child->first_attribute("name")->value();
356 if (res_name.empty() && child->first_attribute("filename"))
357 res_name = child->first_attribute("filename")->value();
358
359 if (!res_name.empty()) {
360 LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
361 } else
362 LOGERR("Resource type (%s) failed to load\n", type.c_str());
thatf74ac872015-01-18 12:00:02 +0100363 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400365}
366
367ResourceManager::~ResourceManager()
368{
that74ac6062015-03-04 22:39:34 +0100369 for (std::vector<FontResource*>::iterator it = mFonts.begin(); it != mFonts.end(); ++it)
370 delete *it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
that74ac6062015-03-04 22:39:34 +0100372 for (std::vector<ImageResource*>::iterator it = mImages.begin(); it != mImages.end(); ++it)
373 delete *it;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374
that74ac6062015-03-04 22:39:34 +0100375 for (std::vector<AnimationResource*>::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
376 delete *it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400377}