blob: 253d438f45b943ce16a1d70e682076c0b32aac90 [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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -050026Resource::Resource(xml_node<>* node, ZipArchive* pZip __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -040027{
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
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200116 if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 {
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200118 m_type = TYPE_TTF;
119
120 attr = node->first_attribute("size");
121 if(!attr)
122 return;
123
Ethan Yonker63e414f2015-02-06 15:44:39 -0600124 int size = scale_theme_min(atoi(attr->value()));
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200125 int dpi = 300;
126
127 attr = node->first_attribute("dpi");
128 if(attr)
129 dpi = atoi(attr->value());
130
131 if (ExtractResource(pZip, "fonts", file, "", TMP_RESOURCE_NAME) == 0)
132 {
133 mFont = gr_ttf_loadFont(TMP_RESOURCE_NAME, size, dpi);
134 unlink(TMP_RESOURCE_NAME);
135 }
136 else
137 {
Dees Troy3454ade2015-01-20 19:21:04 +0000138 file = std::string(TWRES "fonts/") + file;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200139 mFont = gr_ttf_loadFont(file.c_str(), size, dpi);
140 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 }
142 else
143 {
Ethan Yonker88037f42015-10-04 22:09:08 -0500144 LOGERR("Non-TTF fonts are no longer supported.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400146}
147
148FontResource::~FontResource()
149{
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200150 if(mFont)
151 {
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200152 if(m_type == TYPE_TTF)
153 gr_ttf_freeFont(mFont);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200154 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400155}
156
that5267a212015-05-06 23:45:57 +0200157ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 : Resource(node, pZip)
159{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 std::string file;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600161 gr_surface temp_surface = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 mSurface = NULL;
Ethan Yonker619a7212014-12-03 16:47:37 -0600164 if (!node) {
165 LOGERR("ImageResource node is NULL\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 return;
Ethan Yonker619a7212014-12-03 16:47:37 -0600167 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400168
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 if (node->first_attribute("filename"))
170 file = node->first_attribute("filename")->value();
Ethan Yonker63e414f2015-02-06 15:44:39 -0600171 else {
172 LOGERR("No filename specified for image resource.\n");
173 return;
174 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400175
that5267a212015-05-06 23:45:57 +0200176 bool retain_aspect = (node->first_attribute("retainaspect") != NULL);
177 // the value does not matter, if retainaspect is present, we assume that we want to retain it
Ethan Yonker63e414f2015-02-06 15:44:39 -0600178 LoadImage(pZip, file, &temp_surface);
179 CheckAndScaleImage(temp_surface, &mSurface, retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -0400180}
181
182ImageResource::~ImageResource()
183{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 if (mSurface)
185 res_free_surface(mSurface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400186}
187
that5267a212015-05-06 23:45:57 +0200188AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 : Resource(node, pZip)
190{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 std::string file;
192 int fileNum = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 if (!node)
195 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400196
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200197 if (node->first_attribute("filename"))
198 file = node->first_attribute("filename")->value();
Ethan Yonker63e414f2015-02-06 15:44:39 -0600199 else {
200 LOGERR("No filename specified for image resource.\n");
201 return;
202 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400203
that5267a212015-05-06 23:45:57 +0200204 bool retain_aspect = (node->first_attribute("retainaspect") != NULL);
205 // the value does not matter, if retainaspect is present, we assume that we want to retain it
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 for (;;)
207 {
208 std::ostringstream fileName;
209 fileName << file << std::setfill ('0') << std::setw (3) << fileNum;
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
Ethan Yonker63e414f2015-02-06 15:44:39 -0600211 gr_surface surface, temp_surface = NULL;
212 LoadImage(pZip, fileName.str(), &temp_surface);
213 CheckAndScaleImage(temp_surface, &surface, retain_aspect);
214 if (surface) {
215 mSurfaces.push_back(surface);
216 fileNum++;
217 } else
218 break; // Done loading animation images
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400220}
221
222AnimationResource::~AnimationResource()
223{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200224 std::vector<gr_surface>::iterator it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400225
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it)
227 res_free_surface(*it);
228
229 mSurfaces.clear();
Dees_Troy51a0e822012-09-05 15:24:24 -0400230}
231
that74ac6062015-03-04 22:39:34 +0100232FontResource* ResourceManager::FindFont(const std::string& name) const
Dees_Troy51a0e822012-09-05 15:24:24 -0400233{
that74ac6062015-03-04 22:39:34 +0100234 for (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it)
235 if (name == (*it)->GetName())
236 return *it;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400238}
239
that74ac6062015-03-04 22:39:34 +0100240ImageResource* ResourceManager::FindImage(const std::string& name) const
Dees_Troy51a0e822012-09-05 15:24:24 -0400241{
that74ac6062015-03-04 22:39:34 +0100242 for (std::vector<ImageResource*>::const_iterator it = mImages.begin(); it != mImages.end(); ++it)
243 if (name == (*it)->GetName())
244 return *it;
245 return NULL;
246}
247
248AnimationResource* ResourceManager::FindAnimation(const std::string& name) const
249{
250 for (std::vector<AnimationResource*>::const_iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
251 if (name == (*it)->GetName())
252 return *it;
253 return NULL;
254}
255
thatb2e8f672015-03-05 20:25:39 +0100256std::string ResourceManager::FindString(const std::string& name) const
257{
258 std::map<std::string, std::string>::const_iterator it = mStrings.find(name);
259 if (it != mStrings.end())
260 return it->second;
261 return "[" + name + ("]");
262}
263
that74ac6062015-03-04 22:39:34 +0100264ResourceManager::ResourceManager()
265{
Ethan Yonker780cd392014-07-21 15:24:39 -0500266}
267
268void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip)
269{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 if (!resList)
271 return;
that0f425062015-03-04 23:05:00 +0100272
273 for (xml_node<>* child = resList->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 {
that0f425062015-03-04 23:05:00 +0100275 std::string type = child->name();
276 if (type == "resource") {
277 // legacy format : <resource type="...">
278 xml_attribute<>* attr = child->first_attribute("type");
279 type = attr ? attr->value() : "*unspecified*";
280 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400281
that74ac6062015-03-04 22:39:34 +0100282 bool error = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200283 if (type == "font")
284 {
that74ac6062015-03-04 22:39:34 +0100285 FontResource* res = new FontResource(child, pZip);
286 if (res->GetResource())
287 mFonts.push_back(res);
288 else {
289 error = true;
290 delete res;
291 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 }
293 else if (type == "image")
294 {
that5267a212015-05-06 23:45:57 +0200295 ImageResource* res = new ImageResource(child, pZip);
that74ac6062015-03-04 22:39:34 +0100296 if (res->GetResource())
297 mImages.push_back(res);
298 else {
299 error = true;
300 delete res;
301 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 }
303 else if (type == "animation")
304 {
that5267a212015-05-06 23:45:57 +0200305 AnimationResource* res = new AnimationResource(child, pZip);
that74ac6062015-03-04 22:39:34 +0100306 if (res->GetResourceCount())
307 mAnimations.push_back(res);
308 else {
309 error = true;
310 delete res;
311 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 }
thatb2e8f672015-03-05 20:25:39 +0100313 else if (type == "string")
314 {
315 if (xml_attribute<>* attr = child->first_attribute("name"))
316 mStrings[attr->value()] = child->value();
317 else
318 error = true;
319 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200320 else
321 {
322 LOGERR("Resource type (%s) not supported.\n", type.c_str());
that74ac6062015-03-04 22:39:34 +0100323 error = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200324 }
thatf6ed8fc2015-02-14 20:23:16 +0100325
that74ac6062015-03-04 22:39:34 +0100326 if (error)
thatf74ac872015-01-18 12:00:02 +0100327 {
328 std::string res_name;
329 if (child->first_attribute("name"))
330 res_name = child->first_attribute("name")->value();
331 if (res_name.empty() && child->first_attribute("filename"))
332 res_name = child->first_attribute("filename")->value();
333
334 if (!res_name.empty()) {
335 LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
336 } else
337 LOGERR("Resource type (%s) failed to load\n", type.c_str());
thatf74ac872015-01-18 12:00:02 +0100338 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342ResourceManager::~ResourceManager()
343{
that74ac6062015-03-04 22:39:34 +0100344 for (std::vector<FontResource*>::iterator it = mFonts.begin(); it != mFonts.end(); ++it)
345 delete *it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400346
that74ac6062015-03-04 22:39:34 +0100347 for (std::vector<ImageResource*>::iterator it = mImages.begin(); it != mImages.end(); ++it)
348 delete *it;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200349
that74ac6062015-03-04 22:39:34 +0100350 for (std::vector<AnimationResource*>::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
351 delete *it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400352}