blob: 7a50a899b1702eb97a20f7cced3e26656b1af8f0 [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>
7#include <fcntl.h>
8#include <sys/reboot.h>
9#include <sys/stat.h>
10#include <sys/time.h>
11#include <sys/mman.h>
12#include <sys/types.h>
13#include <sys/ioctl.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
17
18#include <string>
19#include <sstream>
20#include <iostream>
21#include <iomanip>
22
23extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000024#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040026}
27
28#include "rapidxml.hpp"
29#include "objects.hpp"
30
31#define TMP_RESOURCE_NAME "/tmp/extract.bin"
32
33Resource::Resource(xml_node<>* node, ZipArchive* pZip)
34{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020035 if (node && node->first_attribute("name"))
36 mName = node->first_attribute("name")->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040037}
38
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020039int Resource::ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile)
Dees_Troy51a0e822012-09-05 15:24:24 -040040{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020041 if (!pZip)
42 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040043
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020044 std::string src = folderName + "/" + fileName + fileExtn;
Dees_Troy51a0e822012-09-05 15:24:24 -040045
46 const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020047 if (binary == NULL) {
48 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040049 }
50
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 unlink(destFile.c_str());
52 int fd = creat(destFile.c_str(), 0666);
53 if (fd < 0)
54 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040055
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 int ret = 0;
57 if (!mzExtractZipEntryToFile(pZip, binary, fd))
58 ret = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 close(fd);
61 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -040062}
63
64FontResource::FontResource(xml_node<>* node, ZipArchive* pZip)
65 : Resource(node, pZip)
66{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 std::string file;
Dees_Troy51a0e822012-09-05 15:24:24 -040068
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020069 mFont = NULL;
70 if (!node)
71 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040072
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 if (node->first_attribute("filename"))
74 file = node->first_attribute("filename")->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0)
77 {
78 mFont = gr_loadFont(TMP_RESOURCE_NAME);
79 unlink(TMP_RESOURCE_NAME);
80 }
81 else
82 {
83 mFont = gr_loadFont(file.c_str());
84 }
Dees_Troy51a0e822012-09-05 15:24:24 -040085}
86
87FontResource::~FontResource()
88{
89}
90
91ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip)
92 : Resource(node, pZip)
93{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 std::string file;
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 mSurface = NULL;
97 if (!node)
98 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040099
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100 if (node->first_attribute("filename"))
101 file = node->first_attribute("filename")->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400102
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200103 if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0)
104 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400105 res_create_surface(TMP_RESOURCE_NAME, &mSurface);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 unlink(TMP_RESOURCE_NAME);
107 }
108 else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0)
109 {
110 // JPG includes the .jpg extension in the filename so extension should be blank
111 res_create_surface(TMP_RESOURCE_NAME, &mSurface);
112 unlink(TMP_RESOURCE_NAME);
113 }
114 else
Dees_Troy51a0e822012-09-05 15:24:24 -0400115 res_create_surface(file.c_str(), &mSurface);
116}
117
118ImageResource::~ImageResource()
119{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 if (mSurface)
121 res_free_surface(mSurface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400122}
123
124AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip)
125 : Resource(node, pZip)
126{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 std::string file;
128 int fileNum = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400129
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 if (!node)
131 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400132
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 if (node->first_attribute("filename"))
134 file = node->first_attribute("filename")->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 for (;;)
137 {
138 std::ostringstream fileName;
139 fileName << file << std::setfill ('0') << std::setw (3) << fileNum;
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 gr_surface surface;
142 if (pZip)
143 {
144 if (ExtractResource(pZip, "images", fileName.str(), ".png", TMP_RESOURCE_NAME) != 0)
145 break;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500146
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 if (res_create_surface(TMP_RESOURCE_NAME, &surface))
148 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 unlink(TMP_RESOURCE_NAME);
151 }
152 else
153 {
154 if (res_create_surface(fileName.str().c_str(), &surface))
155 break;
156 }
157 mSurfaces.push_back(surface);
158 fileNum++;
159 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400160}
161
162AnimationResource::~AnimationResource()
163{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200164 std::vector<gr_surface>::iterator it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400165
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it)
167 res_free_surface(*it);
168
169 mSurfaces.clear();
Dees_Troy51a0e822012-09-05 15:24:24 -0400170}
171
172Resource* ResourceManager::FindResource(std::string name)
173{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 std::vector<Resource*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400175
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 for (iter = mResources.begin(); iter != mResources.end(); iter++)
177 {
178 if (name == (*iter)->GetName())
179 return (*iter);
180 }
181 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182}
183
184ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip)
185{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 if (!resList)
189 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 child = resList->first_node("resource");
192 while (child != NULL)
193 {
194 xml_attribute<>* attr = child->first_attribute("type");
195 if (!attr)
196 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400197
198 std::string type = attr->value();
199
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200200 if (type == "font")
201 {
202 FontResource* res = new FontResource(child, pZip);
203 if (res == NULL || res->GetResource() == NULL)
204 {
205 xml_attribute<>* attr_name = child->first_attribute("name");
Dees_Troy51a0e822012-09-05 15:24:24 -0400206
207 if (!attr_name) {
208 std::string res_name = attr_name->value();
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000211 LOGERR("Resource type (%s) failed to load\n", type.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 delete res;
214 }
215 else
216 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 mResources.push_back((Resource*) res);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 }
219 }
220 else if (type == "image")
221 {
222 ImageResource* res = new ImageResource(child, pZip);
223 if (res == NULL || res->GetResource() == NULL)
224 {
225 xml_attribute<>* attr_name = child->first_attribute("name");
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
227 if (!attr_name) {
228 std::string res_name = attr_name->value();
Dees_Troy2673cec2013-04-02 20:22:16 +0000229 LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 LOGERR("Resource type (%s) failed to load\n", type.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400232
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200233 delete res;
234 }
235 else
236 {
237 mResources.push_back((Resource*) res);
238 }
239 }
240 else if (type == "animation")
241 {
242 AnimationResource* res = new AnimationResource(child, pZip);
243 if (res == NULL || res->GetResource() == NULL)
244 {
245 xml_attribute<>* attr_name = child->first_attribute("name");
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 if (!attr_name) {
248 std::string res_name = attr_name->value();
249 LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
250 } else
251 LOGERR("Resource type (%s) failed to load\n", type.c_str());
252
253 delete res;
254 }
255 else
256 {
257 mResources.push_back((Resource*) res);
258 }
259 }
260 else
261 {
262 LOGERR("Resource type (%s) not supported.\n", type.c_str());
263 }
264
265 child = child->next_sibling("resource");
266 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400267}
268
269ResourceManager::~ResourceManager()
270{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200271 std::vector<Resource*>::iterator iter;
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 for (iter = mResources.begin(); iter != mResources.end(); iter++)
274 delete *iter;
275
276 mResources.clear();
Dees_Troy51a0e822012-09-05 15:24:24 -0400277}