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 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 159 | if (cur < min) |
| 160 | cur = min; |
| 161 | if (cur > max) |
| 162 | cur = max; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 163 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 164 | if (max == 0) |
| 165 | pos = 0; |
| 166 | else |
| 167 | pos = (cur * mRenderW) / max; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 168 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 169 | if (pos == mLastPos) |
| 170 | return 0; |
| 171 | |
| 172 | mLastPos = pos; |
| 173 | |
| 174 | if (RenderInternal() != 0) |
| 175 | return -1; |
| 176 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 179 | int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 180 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 181 | GUIObject::NotifyVarChange(varName, value); |
| 182 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 183 | if(!isConditionTrue()) |
| 184 | return 0; |
| 185 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 186 | static int nextPush = 0; |
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 (varName.empty()) |
| 189 | { |
| 190 | nextPush = 0; |
| 191 | mLastPos = 0; |
| 192 | mSlide = 0.0; |
| 193 | mSlideInc = 0.0; |
| 194 | return 0; |
| 195 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 196 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 197 | if (varName == "ui_progress_portion" || varName == "ui_progress_frames") |
| 198 | { |
| 199 | std::string str; |
| 200 | int cur; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 201 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 202 | if (mSlideFrames) |
| 203 | { |
| 204 | mSlide += (mSlideInc * mSlideFrames); |
| 205 | cur = (int) mSlide; |
| 206 | DataManager::SetValue(mCurValVar, cur); |
| 207 | mSlideFrames = 0; |
| 208 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 210 | if (nextPush) |
| 211 | { |
| 212 | mSlide += nextPush; |
| 213 | cur = (int) mSlide; |
| 214 | DataManager::SetValue(mCurValVar, cur); |
| 215 | nextPush = 0; |
| 216 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 218 | if (varName == "ui_progress_portion") mSlide = atof(value.c_str()); |
| 219 | else |
| 220 | { |
| 221 | mSlideFrames = atol(value.c_str()); |
| 222 | if (mSlideFrames == 0) |
| 223 | { |
| 224 | // We're just holding this progress until the next push |
| 225 | nextPush = mSlide; |
| 226 | } |
| 227 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 228 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 229 | if (mSlide > 0 && mSlideFrames > 0) |
| 230 | { |
| 231 | // Get the current position |
| 232 | str.clear(); |
| 233 | DataManager::GetValue(mCurValVar, str); |
| 234 | cur = atoi(str.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 235 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 236 | mSlideInc = (float) mSlide / (float) mSlideFrames; |
| 237 | mSlide = cur; |
| 238 | } |
| 239 | } |
| 240 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 241 | } |