blob: a4d1329326b6f3bfd5ba0b69cdeadc8334c7f605 [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
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500159 if (cur < min)
160 cur = min;
161 if (cur > max)
162 cur = max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400163
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500164 if (max == 0)
165 pos = 0;
166 else
167 pos = (cur * mRenderW) / max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400168
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 if (pos == mLastPos)
170 return 0;
171
172 mLastPos = pos;
173
174 if (RenderInternal() != 0)
175 return -1;
176 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400177}
178
Vojtech Bocek07220562014-02-08 02:05:33 +0100179int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400180{
Vojtech Bocek07220562014-02-08 02:05:33 +0100181 GUIObject::NotifyVarChange(varName, value);
182
Vojtech Bocekede51c52014-02-07 23:58:09 +0100183 if(!isConditionTrue())
184 return 0;
185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 static int nextPush = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 if (varName.empty())
189 {
190 nextPush = 0;
191 mLastPos = 0;
192 mSlide = 0.0;
193 mSlideInc = 0.0;
194 return 0;
195 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400196
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200197 if (varName == "ui_progress_portion" || varName == "ui_progress_frames")
198 {
199 std::string str;
200 int cur;
Dees_Troy51a0e822012-09-05 15:24:24 -0400201
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200202 if (mSlideFrames)
203 {
204 mSlide += (mSlideInc * mSlideFrames);
205 cur = (int) mSlide;
206 DataManager::SetValue(mCurValVar, cur);
207 mSlideFrames = 0;
208 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400209
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200210 if (nextPush)
211 {
212 mSlide += nextPush;
213 cur = (int) mSlide;
214 DataManager::SetValue(mCurValVar, cur);
215 nextPush = 0;
216 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400217
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 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_Troy51a0e822012-09-05 15:24:24 -0400228
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 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_Troy51a0e822012-09-05 15:24:24 -0400235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 mSlideInc = (float) mSlide / (float) mSlideFrames;
237 mSlide = cur;
238 }
239 }
240 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241}