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