blob: 83de4d13b93846fb613e38ec6268ca86512aa95a [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// 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
20extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000021#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040022#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040023}
24
25#include "rapidxml.hpp"
26#include "objects.hpp"
27
Vojtech Bocekede51c52014-02-07 23:58:09 +010028GUIProgressBar::GUIProgressBar(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040029{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020030 xml_attribute<>* attr;
31 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020033 mEmptyBar = NULL;
34 mFullBar = NULL;
35 mLastPos = 0;
36 mSlide = 0.0;
37 mSlideInc = 0.0;
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020039 if (!node)
40 {
41 LOGERR("GUIProgressBar created without XML node\n");
42 return;
43 }
Dees_Troy51a0e822012-09-05 15:24:24 -040044
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020045 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_Troy51a0e822012-09-05 15:24:24 -040051
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 attr = child->first_attribute("full");
53 if (attr)
54 mFullBar = PageManager::FindResource(attr->value());
55 }
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 // Load the placement
58 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 // 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_Troy51a0e822012-09-05 15:24:24 -040066
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 attr = child->first_attribute("max");
68 if (attr) mMaxValVar = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040069
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 attr = child->first_attribute("name");
71 if (attr) mCurValVar = attr->value();
72 }
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 if (mEmptyBar && mEmptyBar->GetResource())
75 {
76 mRenderW = gr_get_width(mEmptyBar->GetResource());
77 mRenderH = gr_get_height(mEmptyBar->GetResource());
78 }
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040081}
82
83int GUIProgressBar::Render(void)
84{
Vojtech Bocekede51c52014-02-07 23:58:09 +010085 if(!isConditionTrue())
86 return 0;
87
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 // This handles making sure timing updates occur
89 Update();
90 return RenderInternal();
Dees_Troy51a0e822012-09-05 15:24:24 -040091}
92
93int GUIProgressBar::RenderInternal(void)
94{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 if (!mEmptyBar || !mEmptyBar->GetResource())
96 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 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_Troy51a0e822012-09-05 15:24:24 -0400104}
105
106int GUIProgressBar::Update(void)
107{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100108 if(!isConditionTrue())
109 return 0;
110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 std::string str;
112 int min, max, cur, pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 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_Troy51a0e822012-09-05 15:24:24 -0400125
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 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_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 str.clear();
139 DataManager::GetValue(mCurValVar, str);
140 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 // 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_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 // Normalize to 0
155 max -= min;
156 cur -= min;
157 min = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 if (cur < min) cur = min;
160 if (cur > max) cur = max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 if (max == 0) pos = 0;
163 else pos = (cur * mRenderW) / max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 if (pos == mLastPos)
166 return 0;
167
168 mLastPos = pos;
169
170 if (RenderInternal() != 0)
171 return -1;
172 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400173}
174
Vojtech Bocek07220562014-02-08 02:05:33 +0100175int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400176{
Vojtech Bocek07220562014-02-08 02:05:33 +0100177 GUIObject::NotifyVarChange(varName, value);
178
Vojtech Bocekede51c52014-02-07 23:58:09 +0100179 if(!isConditionTrue())
180 return 0;
181
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 static int nextPush = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 if (varName.empty())
185 {
186 nextPush = 0;
187 mLastPos = 0;
188 mSlide = 0.0;
189 mSlideInc = 0.0;
190 return 0;
191 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400192
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 if (varName == "ui_progress_portion" || varName == "ui_progress_frames")
194 {
195 std::string str;
196 int cur;
Dees_Troy51a0e822012-09-05 15:24:24 -0400197
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200198 if (mSlideFrames)
199 {
200 mSlide += (mSlideInc * mSlideFrames);
201 cur = (int) mSlide;
202 DataManager::SetValue(mCurValVar, cur);
203 mSlideFrames = 0;
204 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 if (nextPush)
207 {
208 mSlide += nextPush;
209 cur = (int) mSlide;
210 DataManager::SetValue(mCurValVar, cur);
211 nextPush = 0;
212 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400213
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 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_Troy51a0e822012-09-05 15:24:24 -0400224
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 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_Troy51a0e822012-09-05 15:24:24 -0400231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 mSlideInc = (float) mSlide / (float) mSlideFrames;
233 mSlide = cur;
234 }
235 }
236 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237}