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 | |
| 28 | GUIProgressBar::GUIProgressBar(xml_node<>* node) |
| 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 | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | // This handles making sure timing updates occur |
| 86 | Update(); |
| 87 | return RenderInternal(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int GUIProgressBar::RenderInternal(void) |
| 91 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | if (!mEmptyBar || !mEmptyBar->GetResource()) |
| 93 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 94 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 95 | if (!mFullBar || !mFullBar->GetResource()) |
| 96 | return -1; |
| 97 | |
| 98 | gr_blit(mEmptyBar->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 99 | gr_blit(mFullBar->GetResource(), 0, 0, mLastPos, mRenderH, mRenderX, mRenderY); |
| 100 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int GUIProgressBar::Update(void) |
| 104 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | std::string str; |
| 106 | int min, max, cur, pos; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 108 | if (mMinValVar.empty()) |
| 109 | min = 0; |
| 110 | else |
| 111 | { |
| 112 | str.clear(); |
| 113 | if (atoi(mMinValVar.c_str()) != 0) |
| 114 | str = mMinValVar; |
| 115 | else |
| 116 | DataManager::GetValue(mMinValVar, str); |
| 117 | min = atoi(str.c_str()); |
| 118 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | if (mMaxValVar.empty()) |
| 121 | max = 100; |
| 122 | else |
| 123 | { |
| 124 | str.clear(); |
| 125 | if (atoi(mMaxValVar.c_str()) != 0) |
| 126 | str = mMaxValVar; |
| 127 | else |
| 128 | DataManager::GetValue(mMaxValVar, str); |
| 129 | max = atoi(str.c_str()); |
| 130 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | str.clear(); |
| 133 | DataManager::GetValue(mCurValVar, str); |
| 134 | cur = atoi(str.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 135 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 136 | // Do slide, if needed |
| 137 | if (mSlideFrames) |
| 138 | { |
| 139 | mSlide += mSlideInc; |
| 140 | mSlideFrames--; |
| 141 | if (cur != (int) mSlide) |
| 142 | { |
| 143 | cur = (int) mSlide; |
| 144 | DataManager::SetValue(mCurValVar, cur); |
| 145 | } |
| 146 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 147 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 148 | // Normalize to 0 |
| 149 | max -= min; |
| 150 | cur -= min; |
| 151 | min = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 153 | if (cur < min) cur = min; |
| 154 | if (cur > max) cur = max; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 155 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | if (max == 0) pos = 0; |
| 157 | else pos = (cur * mRenderW) / max; |
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 (pos == mLastPos) |
| 160 | return 0; |
| 161 | |
| 162 | mLastPos = pos; |
| 163 | |
| 164 | if (RenderInternal() != 0) |
| 165 | return -1; |
| 166 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int GUIProgressBar::NotifyVarChange(std::string varName, std::string value) |
| 170 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | static int nextPush = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 172 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 173 | if (varName.empty()) |
| 174 | { |
| 175 | nextPush = 0; |
| 176 | mLastPos = 0; |
| 177 | mSlide = 0.0; |
| 178 | mSlideInc = 0.0; |
| 179 | return 0; |
| 180 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 181 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 182 | if (varName == "ui_progress_portion" || varName == "ui_progress_frames") |
| 183 | { |
| 184 | std::string str; |
| 185 | int cur; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 186 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 187 | if (mSlideFrames) |
| 188 | { |
| 189 | mSlide += (mSlideInc * mSlideFrames); |
| 190 | cur = (int) mSlide; |
| 191 | DataManager::SetValue(mCurValVar, cur); |
| 192 | mSlideFrames = 0; |
| 193 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 195 | if (nextPush) |
| 196 | { |
| 197 | mSlide += nextPush; |
| 198 | cur = (int) mSlide; |
| 199 | DataManager::SetValue(mCurValVar, cur); |
| 200 | nextPush = 0; |
| 201 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 203 | if (varName == "ui_progress_portion") mSlide = atof(value.c_str()); |
| 204 | else |
| 205 | { |
| 206 | mSlideFrames = atol(value.c_str()); |
| 207 | if (mSlideFrames == 0) |
| 208 | { |
| 209 | // We're just holding this progress until the next push |
| 210 | nextPush = mSlide; |
| 211 | } |
| 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 (mSlide > 0 && mSlideFrames > 0) |
| 215 | { |
| 216 | // Get the current position |
| 217 | str.clear(); |
| 218 | DataManager::GetValue(mCurValVar, str); |
| 219 | cur = atoi(str.c_str()); |
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 | mSlideInc = (float) mSlide / (float) mSlideFrames; |
| 222 | mSlide = cur; |
| 223 | } |
| 224 | } |
| 225 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 226 | } |