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 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 23 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 45 | child = FindNode(node, "resource"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 46 | if (child) |
| 47 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 48 | mEmptyBar = LoadAttrImage(child, "empty"); |
| 49 | mFullBar = LoadAttrImage(child, "full"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 50 | } |
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 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 53 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 55 | // Load the data |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 56 | child = FindNode(node, "data"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | if (child) |
| 58 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 59 | mMinValVar = LoadAttrString(child, "min"); |
| 60 | mMaxValVar = LoadAttrString(child, "max"); |
| 61 | mCurValVar = LoadAttrString(child, "name"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 62 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 64 | mRenderW = mEmptyBar->GetWidth(); |
| 65 | mRenderH = mEmptyBar->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int GUIProgressBar::Render(void) |
| 69 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 70 | if(!isConditionTrue()) |
| 71 | return 0; |
| 72 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 73 | // This handles making sure timing updates occur |
| 74 | Update(); |
| 75 | return RenderInternal(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int GUIProgressBar::RenderInternal(void) |
| 79 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 80 | if (!mEmptyBar || !mEmptyBar->GetResource()) |
| 81 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 83 | if (!mFullBar || !mFullBar->GetResource()) |
| 84 | return -1; |
| 85 | |
| 86 | gr_blit(mEmptyBar->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 87 | gr_blit(mFullBar->GetResource(), 0, 0, mLastPos, mRenderH, mRenderX, mRenderY); |
| 88 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int GUIProgressBar::Update(void) |
| 92 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 93 | if(!isConditionTrue()) |
| 94 | return 0; |
| 95 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | std::string str; |
| 97 | int min, max, cur, pos; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | if (mMinValVar.empty()) |
| 100 | min = 0; |
| 101 | else |
| 102 | { |
| 103 | str.clear(); |
| 104 | if (atoi(mMinValVar.c_str()) != 0) |
| 105 | str = mMinValVar; |
| 106 | else |
| 107 | DataManager::GetValue(mMinValVar, str); |
| 108 | min = atoi(str.c_str()); |
| 109 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 110 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 111 | if (mMaxValVar.empty()) |
| 112 | max = 100; |
| 113 | else |
| 114 | { |
| 115 | str.clear(); |
| 116 | if (atoi(mMaxValVar.c_str()) != 0) |
| 117 | str = mMaxValVar; |
| 118 | else |
| 119 | DataManager::GetValue(mMaxValVar, str); |
| 120 | max = atoi(str.c_str()); |
| 121 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | str.clear(); |
| 124 | DataManager::GetValue(mCurValVar, str); |
| 125 | cur = atoi(str.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 127 | // Do slide, if needed |
| 128 | if (mSlideFrames) |
| 129 | { |
| 130 | mSlide += mSlideInc; |
| 131 | mSlideFrames--; |
| 132 | if (cur != (int) mSlide) |
| 133 | { |
| 134 | cur = (int) mSlide; |
| 135 | DataManager::SetValue(mCurValVar, cur); |
| 136 | } |
| 137 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 138 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 139 | // Normalize to 0 |
| 140 | max -= min; |
| 141 | cur -= min; |
| 142 | min = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 144 | if (cur < min) |
| 145 | cur = min; |
| 146 | if (cur > max) |
| 147 | cur = max; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 148 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 149 | if (max == 0) |
| 150 | pos = 0; |
| 151 | else |
| 152 | pos = (cur * mRenderW) / max; |
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 | if (pos == mLastPos) |
| 155 | return 0; |
| 156 | |
| 157 | mLastPos = pos; |
| 158 | |
| 159 | if (RenderInternal() != 0) |
| 160 | return -1; |
| 161 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 164 | int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 166 | GUIObject::NotifyVarChange(varName, value); |
| 167 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 168 | if(!isConditionTrue()) |
| 169 | return 0; |
| 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 | } |