Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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 | |
| 23 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 24 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | #include "rapidxml.hpp" |
| 29 | #include "objects.hpp" |
| 30 | |
| 31 | #define TMP_RESOURCE_NAME "/tmp/extract.bin" |
| 32 | |
| 33 | Resource::Resource(xml_node<>* node, ZipArchive* pZip) |
| 34 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 35 | if (node && node->first_attribute("name")) |
| 36 | mName = node->first_attribute("name")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 39 | int Resource::ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 41 | if (!pZip) |
| 42 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 44 | std::string src = folderName + "/" + fileName + fileExtn; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | |
| 46 | const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 47 | if (binary == NULL) { |
| 48 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 51 | unlink(destFile.c_str()); |
| 52 | int fd = creat(destFile.c_str(), 0666); |
| 53 | if (fd < 0) |
| 54 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 56 | int ret = 0; |
| 57 | if (!mzExtractZipEntryToFile(pZip, binary, fd)) |
| 58 | ret = -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | close(fd); |
| 61 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | FontResource::FontResource(xml_node<>* node, ZipArchive* pZip) |
| 65 | : Resource(node, pZip) |
| 66 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | std::string file; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 68 | xml_attribute<>* attr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 69 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 70 | mFont = NULL; |
| 71 | if (!node) |
| 72 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 74 | attr = node->first_attribute("filename"); |
| 75 | if (!attr) |
| 76 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 77 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 78 | file = attr->value(); |
| 79 | |
| 80 | #ifndef TW_DISABLE_TTF |
| 81 | if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 82 | { |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 83 | m_type = TYPE_TTF; |
| 84 | |
| 85 | attr = node->first_attribute("size"); |
| 86 | if(!attr) |
| 87 | return; |
| 88 | |
| 89 | int size = atoi(attr->value()); |
| 90 | int dpi = 300; |
| 91 | |
| 92 | attr = node->first_attribute("dpi"); |
| 93 | if(attr) |
| 94 | dpi = atoi(attr->value()); |
| 95 | |
| 96 | if (ExtractResource(pZip, "fonts", file, "", TMP_RESOURCE_NAME) == 0) |
| 97 | { |
| 98 | mFont = gr_ttf_loadFont(TMP_RESOURCE_NAME, size, dpi); |
| 99 | unlink(TMP_RESOURCE_NAME); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | file = std::string("/res/fonts/") + file; |
| 104 | mFont = gr_ttf_loadFont(file.c_str(), size, dpi); |
| 105 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | } |
| 107 | else |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 108 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 109 | { |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 110 | m_type = TYPE_TWRP; |
| 111 | |
| 112 | if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0) |
| 113 | { |
| 114 | attr = node->first_attribute("fallback"); |
| 115 | if (!attr) |
| 116 | return; |
| 117 | |
| 118 | file = attr->value(); |
| 119 | } |
| 120 | |
| 121 | if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) |
| 122 | { |
| 123 | mFont = gr_loadFont(TMP_RESOURCE_NAME); |
| 124 | unlink(TMP_RESOURCE_NAME); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | mFont = gr_loadFont(file.c_str()); |
| 129 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 130 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | FontResource::~FontResource() |
| 134 | { |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 135 | if(mFont) |
| 136 | { |
| 137 | #ifndef TW_DISABLE_TTF |
| 138 | if(m_type == TYPE_TTF) |
| 139 | gr_ttf_freeFont(mFont); |
| 140 | else |
| 141 | #endif |
| 142 | gr_freeFont(mFont); |
| 143 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip) |
| 147 | : Resource(node, pZip) |
| 148 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 149 | std::string file; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | mSurface = NULL; |
| 152 | if (!node) |
| 153 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 154 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | if (node->first_attribute("filename")) |
| 156 | file = node->first_attribute("filename")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) |
| 159 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 160 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 161 | unlink(TMP_RESOURCE_NAME); |
| 162 | } |
| 163 | else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) |
| 164 | { |
| 165 | // JPG includes the .jpg extension in the filename so extension should be blank |
| 166 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
| 167 | unlink(TMP_RESOURCE_NAME); |
| 168 | } |
| 169 | else |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 170 | res_create_surface(file.c_str(), &mSurface); |
| 171 | } |
| 172 | |
| 173 | ImageResource::~ImageResource() |
| 174 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 175 | if (mSurface) |
| 176 | res_free_surface(mSurface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip) |
| 180 | : Resource(node, pZip) |
| 181 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 182 | std::string file; |
| 183 | int fileNum = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 185 | if (!node) |
| 186 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 187 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 188 | if (node->first_attribute("filename")) |
| 189 | file = node->first_attribute("filename")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 190 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 191 | for (;;) |
| 192 | { |
| 193 | std::ostringstream fileName; |
| 194 | fileName << file << std::setfill ('0') << std::setw (3) << fileNum; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 195 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 196 | gr_surface surface; |
| 197 | if (pZip) |
| 198 | { |
| 199 | if (ExtractResource(pZip, "images", fileName.str(), ".png", TMP_RESOURCE_NAME) != 0) |
| 200 | break; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 201 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 202 | if (res_create_surface(TMP_RESOURCE_NAME, &surface)) |
| 203 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 204 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 205 | unlink(TMP_RESOURCE_NAME); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | if (res_create_surface(fileName.str().c_str(), &surface)) |
| 210 | break; |
| 211 | } |
| 212 | mSurfaces.push_back(surface); |
| 213 | fileNum++; |
| 214 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | AnimationResource::~AnimationResource() |
| 218 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 219 | std::vector<gr_surface>::iterator it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 220 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 221 | for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) |
| 222 | res_free_surface(*it); |
| 223 | |
| 224 | mSurfaces.clear(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | Resource* ResourceManager::FindResource(std::string name) |
| 228 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 229 | std::vector<Resource*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 230 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 231 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 232 | { |
| 233 | if (name == (*iter)->GetName()) |
| 234 | return (*iter); |
| 235 | } |
| 236 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip) |
| 240 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 241 | LoadResources(resList, pZip); |
| 242 | } |
| 243 | |
| 244 | void ResourceManager::LoadResources(xml_node<>* resList, ZipArchive* pZip) |
| 245 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 246 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 248 | if (!resList) |
| 249 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 250 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 251 | child = resList->first_node("resource"); |
| 252 | while (child != NULL) |
| 253 | { |
| 254 | xml_attribute<>* attr = child->first_attribute("type"); |
| 255 | if (!attr) |
| 256 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 257 | |
| 258 | std::string type = attr->value(); |
| 259 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 260 | if (type == "font") |
| 261 | { |
| 262 | FontResource* res = new FontResource(child, pZip); |
| 263 | if (res == NULL || res->GetResource() == NULL) |
| 264 | { |
| 265 | xml_attribute<>* attr_name = child->first_attribute("name"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 266 | |
| 267 | if (!attr_name) { |
| 268 | std::string res_name = attr_name->value(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 269 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 270 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 271 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 272 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 273 | delete res; |
| 274 | } |
| 275 | else |
| 276 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 277 | mResources.push_back((Resource*) res); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | else if (type == "image") |
| 281 | { |
| 282 | ImageResource* res = new ImageResource(child, pZip); |
| 283 | if (res == NULL || res->GetResource() == NULL) |
| 284 | { |
| 285 | xml_attribute<>* attr_name = child->first_attribute("name"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | |
| 287 | if (!attr_name) { |
| 288 | std::string res_name = attr_name->value(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 289 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 290 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 291 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 292 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 293 | delete res; |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | mResources.push_back((Resource*) res); |
| 298 | } |
| 299 | } |
| 300 | else if (type == "animation") |
| 301 | { |
| 302 | AnimationResource* res = new AnimationResource(child, pZip); |
| 303 | if (res == NULL || res->GetResource() == NULL) |
| 304 | { |
| 305 | xml_attribute<>* attr_name = child->first_attribute("name"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 306 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 307 | if (!attr_name) { |
| 308 | std::string res_name = attr_name->value(); |
| 309 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
| 310 | } else |
| 311 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
| 312 | |
| 313 | delete res; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | mResources.push_back((Resource*) res); |
| 318 | } |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | LOGERR("Resource type (%s) not supported.\n", type.c_str()); |
| 323 | } |
| 324 | |
| 325 | child = child->next_sibling("resource"); |
| 326 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | ResourceManager::~ResourceManager() |
| 330 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 331 | std::vector<Resource*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 332 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 333 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 334 | delete *iter; |
| 335 | |
| 336 | mResources.clear(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 337 | } |