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" { |
| 24 | #include "../common.h" |
| 25 | #include "../minuitwrp/minui.h" |
| 26 | #include "../recovery_ui.h" |
| 27 | } |
| 28 | |
| 29 | #include "rapidxml.hpp" |
| 30 | #include "objects.hpp" |
| 31 | |
| 32 | #define TMP_RESOURCE_NAME "/tmp/extract.bin" |
| 33 | |
| 34 | Resource::Resource(xml_node<>* node, ZipArchive* pZip) |
| 35 | { |
| 36 | if (node && node->first_attribute("name")) |
| 37 | mName = node->first_attribute("name")->value(); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | int Resource::ExtractResource(ZipArchive* pZip, |
| 42 | std::string folderName, |
| 43 | std::string fileName, |
| 44 | std::string fileExtn, |
| 45 | std::string destFile) |
| 46 | { |
| 47 | if (!pZip) return -1; |
| 48 | |
| 49 | std::string src = folderName + "/" + fileName + fileExtn; |
| 50 | |
| 51 | const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str()); |
| 52 | if (binary == NULL) { |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | unlink(destFile.c_str()); |
| 57 | int fd = creat(destFile.c_str(), 0666); |
| 58 | if (fd < 0) |
| 59 | return -1; |
| 60 | |
| 61 | int ret = 0; |
| 62 | if (!mzExtractZipEntryToFile(pZip, binary, fd)) |
| 63 | ret = -1; |
| 64 | |
| 65 | close(fd); |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | FontResource::FontResource(xml_node<>* node, ZipArchive* pZip) |
| 70 | : Resource(node, pZip) |
| 71 | { |
| 72 | std::string file; |
| 73 | |
| 74 | mFont = NULL; |
| 75 | if (!node) return; |
| 76 | |
| 77 | if (node->first_attribute("filename")) |
| 78 | file = node->first_attribute("filename")->value(); |
| 79 | |
| 80 | if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) |
| 81 | { |
| 82 | mFont = gr_loadFont(TMP_RESOURCE_NAME); |
| 83 | unlink(TMP_RESOURCE_NAME); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | mFont = gr_loadFont(file.c_str()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | FontResource::~FontResource() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip) |
| 96 | : Resource(node, pZip) |
| 97 | { |
| 98 | std::string file; |
| 99 | |
| 100 | mSurface = NULL; |
| 101 | if (!node) return; |
| 102 | |
| 103 | if (node->first_attribute("filename")) |
| 104 | file = node->first_attribute("filename")->value(); |
| 105 | |
| 106 | if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) |
| 107 | { |
| 108 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
| 109 | unlink(TMP_RESOURCE_NAME); |
| 110 | } else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) |
| 111 | { |
| 112 | // JPG includes the .jpg extension in the filename so extension should be blank |
| 113 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
| 114 | unlink(TMP_RESOURCE_NAME); |
| 115 | } |
| 116 | else |
| 117 | res_create_surface(file.c_str(), &mSurface); |
| 118 | } |
| 119 | |
| 120 | ImageResource::~ImageResource() |
| 121 | { |
| 122 | if (mSurface) |
| 123 | res_free_surface(mSurface); |
| 124 | } |
| 125 | |
| 126 | AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip) |
| 127 | : Resource(node, pZip) |
| 128 | { |
| 129 | std::string file; |
| 130 | int fileNum = 1; |
| 131 | |
| 132 | if (!node) return; |
| 133 | |
| 134 | if (node->first_attribute("filename")) |
| 135 | file = node->first_attribute("filename")->value(); |
| 136 | |
| 137 | for ( ; ; ) |
| 138 | { |
| 139 | std::ostringstream fileName; |
| 140 | fileName << file << std::setfill ('0') << std::setw (3) << fileNum; |
| 141 | |
| 142 | gr_surface surface; |
| 143 | if (pZip) |
| 144 | { |
| 145 | if (ExtractResource(pZip, "images", fileName.str(), ".png", TMP_RESOURCE_NAME) != 0) |
| 146 | break; |
| 147 | |
| 148 | if (res_create_surface(TMP_RESOURCE_NAME, &surface)) |
| 149 | break; |
| 150 | |
| 151 | unlink(TMP_RESOURCE_NAME); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | if (res_create_surface(fileName.str().c_str(), &surface)) |
| 156 | break; |
| 157 | } |
| 158 | mSurfaces.push_back(surface); |
| 159 | fileNum++; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | AnimationResource::~AnimationResource() |
| 164 | { |
| 165 | std::vector<gr_surface>::iterator it; |
| 166 | |
| 167 | for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) |
| 168 | { |
| 169 | res_free_surface(*it); |
| 170 | } |
| 171 | mSurfaces.clear(); |
| 172 | } |
| 173 | |
| 174 | Resource* ResourceManager::FindResource(std::string name) |
| 175 | { |
| 176 | std::vector<Resource*>::iterator iter; |
| 177 | |
| 178 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 179 | { |
| 180 | if (name == (*iter)->GetName()) |
| 181 | return (*iter); |
| 182 | } |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip) |
| 187 | { |
| 188 | xml_node<>* child; |
| 189 | |
| 190 | if (!resList) return; |
| 191 | |
| 192 | child = resList->first_node("resource"); |
| 193 | while (child != NULL) |
| 194 | { |
| 195 | xml_attribute<>* attr = child->first_attribute("type"); |
| 196 | if (!attr) |
| 197 | break; |
| 198 | |
| 199 | std::string type = attr->value(); |
| 200 | |
| 201 | if (type == "font") |
| 202 | { |
| 203 | FontResource* res = new FontResource(child, pZip); |
| 204 | if (res == NULL || res->GetResource() == NULL) |
| 205 | { |
| 206 | xml_attribute<>* attr_name = child->first_attribute("name"); |
| 207 | |
| 208 | if (!attr_name) { |
| 209 | std::string res_name = attr_name->value(); |
| 210 | LOGE("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
| 211 | } else |
| 212 | LOGE("Resource type (%s) failed to load\n", type.c_str()); |
| 213 | |
| 214 | delete res; |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | mResources.push_back((Resource*) res); |
| 219 | } |
| 220 | } |
| 221 | else if (type == "image") |
| 222 | { |
| 223 | ImageResource* res = new ImageResource(child, pZip); |
| 224 | if (res == NULL || res->GetResource() == NULL) |
| 225 | { |
| 226 | xml_attribute<>* attr_name = child->first_attribute("name"); |
| 227 | |
| 228 | if (!attr_name) { |
| 229 | std::string res_name = attr_name->value(); |
| 230 | LOGE("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
| 231 | } else |
| 232 | LOGE("Resource type (%s) failed to load\n", type.c_str()); |
| 233 | |
| 234 | delete res; |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | mResources.push_back((Resource*) res); |
| 239 | } |
| 240 | } |
| 241 | else if (type == "animation") |
| 242 | { |
| 243 | AnimationResource* res = new AnimationResource(child, pZip); |
| 244 | if (res == NULL || res->GetResource() == NULL) |
| 245 | { |
| 246 | xml_attribute<>* attr_name = child->first_attribute("name"); |
| 247 | |
| 248 | if (!attr_name) { |
| 249 | std::string res_name = attr_name->value(); |
| 250 | LOGE("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
| 251 | } else |
| 252 | LOGE("Resource type (%s) failed to load\n", type.c_str()); |
| 253 | |
| 254 | delete res; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | mResources.push_back((Resource*) res); |
| 259 | } |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | LOGE("Resource type (%s) not supported.\n", type.c_str()); |
| 264 | } |
| 265 | |
| 266 | child = child->next_sibling("resource"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | ResourceManager::~ResourceManager() |
| 271 | { |
| 272 | std::vector<Resource*>::iterator iter; |
| 273 | |
| 274 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 275 | { |
| 276 | delete *iter; |
| 277 | } |
| 278 | mResources.clear(); |
| 279 | } |
| 280 | |