Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // progressbar.cpp - GUIProgressBar object |
| 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 | |
| 20 | extern "C" { |
| 21 | #include "../common.h" |
| 22 | #include "../minuitwrp/minui.h" |
| 23 | #include "../recovery_ui.h" |
| 24 | } |
| 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | |
| 29 | GUIProgressBar::GUIProgressBar(xml_node<>* node) |
| 30 | { |
| 31 | xml_attribute<>* attr; |
| 32 | xml_node<>* child; |
| 33 | |
| 34 | mEmptyBar = NULL; |
| 35 | mFullBar = NULL; |
| 36 | mLastPos = 0; |
| 37 | mSlide = 0.0; |
| 38 | mSlideInc = 0.0; |
| 39 | |
| 40 | if (!node) |
| 41 | { |
| 42 | LOGE("GUIProgressBar created without XML node\n"); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | child = node->first_node("resource"); |
| 47 | if (child) |
| 48 | { |
| 49 | attr = child->first_attribute("empty"); |
| 50 | if (attr) |
| 51 | mEmptyBar = PageManager::FindResource(attr->value()); |
| 52 | |
| 53 | attr = child->first_attribute("full"); |
| 54 | if (attr) |
| 55 | mFullBar = PageManager::FindResource(attr->value()); |
| 56 | } |
| 57 | |
| 58 | // Load the placement |
| 59 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY); |
| 60 | |
| 61 | // Load the data |
| 62 | child = node->first_node("data"); |
| 63 | if (child) |
| 64 | { |
| 65 | attr = child->first_attribute("min"); |
| 66 | if (attr) mMinValVar = attr->value(); |
| 67 | |
| 68 | attr = child->first_attribute("max"); |
| 69 | if (attr) mMaxValVar = attr->value(); |
| 70 | |
| 71 | attr = child->first_attribute("name"); |
| 72 | if (attr) mCurValVar = attr->value(); |
| 73 | } |
| 74 | |
| 75 | if (mEmptyBar && mEmptyBar->GetResource()) |
| 76 | { |
| 77 | mRenderW = gr_get_width(mEmptyBar->GetResource()); |
| 78 | mRenderH = gr_get_height(mEmptyBar->GetResource()); |
| 79 | } |
| 80 | |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | int GUIProgressBar::Render(void) |
| 85 | { |
| 86 | // This handles making sure timing updates occur |
| 87 | Update(); |
| 88 | return RenderInternal(); |
| 89 | } |
| 90 | |
| 91 | int GUIProgressBar::RenderInternal(void) |
| 92 | { |
| 93 | if (!mEmptyBar || !mEmptyBar->GetResource()) return -1; |
| 94 | if (!mFullBar || !mFullBar->GetResource()) return -1; |
| 95 | |
| 96 | gr_blit(mEmptyBar->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 97 | gr_blit(mFullBar->GetResource(), 0, 0, mLastPos, mRenderH, mRenderX, mRenderY); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int GUIProgressBar::Update(void) |
| 102 | { |
| 103 | std::string str; |
| 104 | int min, max, cur, pos; |
| 105 | |
| 106 | if (mMinValVar.empty()) min = 0; |
| 107 | else |
| 108 | { |
| 109 | str.clear(); |
| 110 | if (atoi(mMinValVar.c_str()) != 0) str = mMinValVar; |
| 111 | else DataManager::GetValue(mMinValVar, str); |
| 112 | min = atoi(str.c_str()); |
| 113 | } |
| 114 | |
| 115 | if (mMaxValVar.empty()) max = 100; |
| 116 | else |
| 117 | { |
| 118 | str.clear(); |
| 119 | if (atoi(mMaxValVar.c_str()) != 0) str = mMaxValVar; |
| 120 | else DataManager::GetValue(mMaxValVar, str); |
| 121 | max = atoi(str.c_str()); |
| 122 | } |
| 123 | |
| 124 | str.clear(); |
| 125 | DataManager::GetValue(mCurValVar, str); |
| 126 | cur = atoi(str.c_str()); |
| 127 | |
| 128 | // Do slide, if needed |
| 129 | if (mSlideFrames) |
| 130 | { |
| 131 | mSlide += mSlideInc; |
| 132 | mSlideFrames--; |
| 133 | if (cur != (int) mSlide) |
| 134 | { |
| 135 | cur = (int) mSlide; |
| 136 | DataManager::SetValue(mCurValVar, cur); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Normalize to 0 |
| 141 | max -= min; |
| 142 | cur -= min; |
| 143 | min = 0; |
| 144 | |
| 145 | if (cur < min) cur = min; |
| 146 | if (cur > max) cur = max; |
| 147 | |
| 148 | if (max == 0) pos = 0; |
| 149 | else pos = (cur * mRenderW) / max; |
| 150 | |
| 151 | if (pos == mLastPos) return 0; |
| 152 | mLastPos = pos; |
| 153 | if (RenderInternal() != 0) return -1; |
| 154 | return 2; |
| 155 | } |
| 156 | |
| 157 | int GUIProgressBar::NotifyVarChange(std::string varName, std::string value) |
| 158 | { |
| 159 | static int nextPush = 0; |
| 160 | |
| 161 | if (varName.empty()) |
| 162 | { |
| 163 | nextPush = 0; |
| 164 | mLastPos = 0; |
| 165 | mSlide = 0.0; |
| 166 | mSlideInc = 0.0; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | if (varName == "ui_progress_portion" || varName == "ui_progress_frames") |
| 171 | { |
| 172 | std::string str; |
| 173 | int cur; |
| 174 | |
| 175 | if (mSlideFrames) |
| 176 | { |
| 177 | mSlide += (mSlideInc * mSlideFrames); |
| 178 | cur = (int) mSlide; |
| 179 | DataManager::SetValue(mCurValVar, cur); |
| 180 | mSlideFrames = 0; |
| 181 | } |
| 182 | |
| 183 | if (nextPush) |
| 184 | { |
| 185 | mSlide += nextPush; |
| 186 | cur = (int) mSlide; |
| 187 | DataManager::SetValue(mCurValVar, cur); |
| 188 | nextPush = 0; |
| 189 | } |
| 190 | |
| 191 | if (varName == "ui_progress_portion") mSlide = atof(value.c_str()); |
| 192 | else |
| 193 | { |
| 194 | mSlideFrames = atol(value.c_str()); |
| 195 | if (mSlideFrames == 0) |
| 196 | { |
| 197 | // We're just holding this progress until the next push |
| 198 | nextPush = mSlide; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (mSlide > 0 && mSlideFrames > 0) |
| 203 | { |
| 204 | // Get the current position |
| 205 | str.clear(); |
| 206 | DataManager::GetValue(mCurValVar, str); |
| 207 | cur = atoi(str.c_str()); |
| 208 | |
| 209 | mSlideInc = (float) mSlide / (float) mSlideFrames; |
| 210 | mSlide = cur; |
| 211 | } |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |