blob: fc2a4976cb91acaf959a8342cf2e8e4f2b5f0cd6 [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}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010023#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
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
Ethan Yonker21ff02a2015-02-18 14:35:00 -060045 child = FindNode(node, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 if (child)
47 {
thatf6ed8fc2015-02-14 20:23:16 +010048 mEmptyBar = LoadAttrImage(child, "empty");
49 mFullBar = LoadAttrImage(child, "full");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 }
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060053 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 // Load the data
Ethan Yonker21ff02a2015-02-18 14:35:00 -060056 child = FindNode(node, "data");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 if (child)
58 {
thatf6ed8fc2015-02-14 20:23:16 +010059 mMinValVar = LoadAttrString(child, "min");
60 mMaxValVar = LoadAttrString(child, "max");
61 mCurValVar = LoadAttrString(child, "name");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 }
Dees_Troy51a0e822012-09-05 15:24:24 -040063
thatf6ed8fc2015-02-14 20:23:16 +010064 mRenderW = mEmptyBar->GetWidth();
65 mRenderH = mEmptyBar->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040066}
67
68int GUIProgressBar::Render(void)
69{
Vojtech Bocekede51c52014-02-07 23:58:09 +010070 if(!isConditionTrue())
71 return 0;
72
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 // This handles making sure timing updates occur
74 Update();
75 return RenderInternal();
Dees_Troy51a0e822012-09-05 15:24:24 -040076}
77
78int GUIProgressBar::RenderInternal(void)
79{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 if (!mEmptyBar || !mEmptyBar->GetResource())
81 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 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_Troy51a0e822012-09-05 15:24:24 -040089}
90
91int GUIProgressBar::Update(void)
92{
Vojtech Bocekede51c52014-02-07 23:58:09 +010093 if(!isConditionTrue())
94 return 0;
95
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 std::string str;
97 int min, max, cur, pos;
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 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_Troy51a0e822012-09-05 15:24:24 -0400110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 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_Troy51a0e822012-09-05 15:24:24 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 str.clear();
124 DataManager::GetValue(mCurValVar, str);
125 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 // 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_Troy51a0e822012-09-05 15:24:24 -0400138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 // Normalize to 0
140 max -= min;
141 cur -= min;
142 min = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500144 if (cur < min)
145 cur = min;
146 if (cur > max)
147 cur = max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500149 if (max == 0)
150 pos = 0;
151 else
152 pos = (cur * mRenderW) / max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 if (pos == mLastPos)
155 return 0;
156
157 mLastPos = pos;
158
159 if (RenderInternal() != 0)
160 return -1;
161 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400162}
163
Vojtech Bocek07220562014-02-08 02:05:33 +0100164int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400165{
Vojtech Bocek07220562014-02-08 02:05:33 +0100166 GUIObject::NotifyVarChange(varName, value);
167
Vojtech Bocekede51c52014-02-07 23:58:09 +0100168 if(!isConditionTrue())
169 return 0;
170
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 static int nextPush = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173 if (varName.empty())
174 {
175 nextPush = 0;
176 mLastPos = 0;
177 mSlide = 0.0;
178 mSlideInc = 0.0;
179 return 0;
180 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400181
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 if (varName == "ui_progress_portion" || varName == "ui_progress_frames")
183 {
184 std::string str;
185 int cur;
Dees_Troy51a0e822012-09-05 15:24:24 -0400186
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 if (mSlideFrames)
188 {
189 mSlide += (mSlideInc * mSlideFrames);
190 cur = (int) mSlide;
191 DataManager::SetValue(mCurValVar, cur);
192 mSlideFrames = 0;
193 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400194
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200195 if (nextPush)
196 {
197 mSlide += nextPush;
198 cur = (int) mSlide;
199 DataManager::SetValue(mCurValVar, cur);
200 nextPush = 0;
201 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 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_Troy51a0e822012-09-05 15:24:24 -0400213
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 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_Troy51a0e822012-09-05 15:24:24 -0400220
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 mSlideInc = (float) mSlide / (float) mSlideFrames;
222 mSlide = cur;
223 }
224 }
225 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400226}