blob: d53fdc19f65ba72b29fee6323f17b23687710f3c [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
28GUIProgressBar::GUIProgressBar(xml_node<>* node)
29{
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 Bocekfafb0c52013-07-25 22:53:02 +020085 // This handles making sure timing updates occur
86 Update();
87 return RenderInternal();
Dees_Troy51a0e822012-09-05 15:24:24 -040088}
89
90int GUIProgressBar::RenderInternal(void)
91{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 if (!mEmptyBar || !mEmptyBar->GetResource())
93 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040094
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 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_Troy51a0e822012-09-05 15:24:24 -0400101}
102
103int GUIProgressBar::Update(void)
104{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 std::string str;
106 int min, max, cur, pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400107
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 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_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 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_Troy51a0e822012-09-05 15:24:24 -0400131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 str.clear();
133 DataManager::GetValue(mCurValVar, str);
134 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 // 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_Troy51a0e822012-09-05 15:24:24 -0400147
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 // Normalize to 0
149 max -= min;
150 cur -= min;
151 min = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 if (cur < min) cur = min;
154 if (cur > max) cur = max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (max == 0) pos = 0;
157 else pos = (cur * mRenderW) / max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 if (pos == mLastPos)
160 return 0;
161
162 mLastPos = pos;
163
164 if (RenderInternal() != 0)
165 return -1;
166 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400167}
168
169int GUIProgressBar::NotifyVarChange(std::string varName, std::string value)
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}