blob: defd9517be37196b5548d7cd22cd1479dc2d5c3f [file] [log] [blame]
Matt Mowere04eee72016-12-31 00:38:57 -06001/*
2 Copyright 2017 TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
Dees_Troy51a0e822012-09-05 15:24:24 -040019// progressbar.cpp - GUIProgressBar object
20
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/reboot.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/mman.h>
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34#include <stdlib.h>
35
36#include <string>
37
38extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010041#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
Vojtech Bocekede51c52014-02-07 23:58:09 +010046GUIProgressBar::GUIProgressBar(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040047{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 mEmptyBar = NULL;
51 mFullBar = NULL;
52 mLastPos = 0;
53 mSlide = 0.0;
54 mSlideInc = 0.0;
Dees_Troy51a0e822012-09-05 15:24:24 -040055
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 if (!node)
57 {
58 LOGERR("GUIProgressBar created without XML node\n");
59 return;
60 }
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Ethan Yonker21ff02a2015-02-18 14:35:00 -060062 child = FindNode(node, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020063 if (child)
64 {
thatf6ed8fc2015-02-14 20:23:16 +010065 mEmptyBar = LoadAttrImage(child, "empty");
66 mFullBar = LoadAttrImage(child, "full");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 }
Dees_Troy51a0e822012-09-05 15:24:24 -040068
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020069 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060070 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -040071
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 // Load the data
Ethan Yonker21ff02a2015-02-18 14:35:00 -060073 child = FindNode(node, "data");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 if (child)
75 {
thatf6ed8fc2015-02-14 20:23:16 +010076 mMinValVar = LoadAttrString(child, "min");
77 mMaxValVar = LoadAttrString(child, "max");
78 mCurValVar = LoadAttrString(child, "name");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 }
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Ethan Yonker58f21322018-08-24 11:17:36 -050081 if (mEmptyBar && mEmptyBar->GetResource()) {
82 mRenderW = mEmptyBar->GetWidth();
83 mRenderH = mEmptyBar->GetHeight();
84 } else {
85 mRenderW = mRenderH = 0;
86 }
Dees_Troy51a0e822012-09-05 15:24:24 -040087}
88
89int GUIProgressBar::Render(void)
90{
Matt Mowera8a89d12016-12-30 18:10:37 -060091 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +010092 return 0;
93
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 // This handles making sure timing updates occur
95 Update();
96 return RenderInternal();
Dees_Troy51a0e822012-09-05 15:24:24 -040097}
98
99int GUIProgressBar::RenderInternal(void)
100{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 if (!mEmptyBar || !mEmptyBar->GetResource())
102 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400103
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 if (!mFullBar || !mFullBar->GetResource())
105 return -1;
106
107 gr_blit(mEmptyBar->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
108 gr_blit(mFullBar->GetResource(), 0, 0, mLastPos, mRenderH, mRenderX, mRenderY);
109 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400110}
111
112int GUIProgressBar::Update(void)
113{
Matt Mowera8a89d12016-12-30 18:10:37 -0600114 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100115 return 0;
116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 std::string str;
118 int min, max, cur, pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 if (mMinValVar.empty())
121 min = 0;
122 else
123 {
124 str.clear();
125 if (atoi(mMinValVar.c_str()) != 0)
126 str = mMinValVar;
127 else
128 DataManager::GetValue(mMinValVar, str);
129 min = atoi(str.c_str());
130 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (mMaxValVar.empty())
133 max = 100;
134 else
135 {
136 str.clear();
137 if (atoi(mMaxValVar.c_str()) != 0)
138 str = mMaxValVar;
139 else
140 DataManager::GetValue(mMaxValVar, str);
141 max = atoi(str.c_str());
142 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200144 str.clear();
145 DataManager::GetValue(mCurValVar, str);
146 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400147
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 // Do slide, if needed
149 if (mSlideFrames)
150 {
151 mSlide += mSlideInc;
152 mSlideFrames--;
153 if (cur != (int) mSlide)
154 {
155 cur = (int) mSlide;
156 DataManager::SetValue(mCurValVar, cur);
157 }
158 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 // Normalize to 0
161 max -= min;
162 cur -= min;
163 min = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500165 if (cur < min)
166 cur = min;
167 if (cur > max)
168 cur = max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400169
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500170 if (max == 0)
171 pos = 0;
172 else
173 pos = (cur * mRenderW) / max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400174
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 if (pos == mLastPos)
176 return 0;
177
178 mLastPos = pos;
179
180 if (RenderInternal() != 0)
181 return -1;
182 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400183}
184
Vojtech Bocek07220562014-02-08 02:05:33 +0100185int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400186{
Vojtech Bocek07220562014-02-08 02:05:33 +0100187 GUIObject::NotifyVarChange(varName, value);
188
Matt Mowera8a89d12016-12-30 18:10:37 -0600189 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100190 return 0;
191
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200192 static int nextPush = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 if (varName.empty())
195 {
196 nextPush = 0;
197 mLastPos = 0;
198 mSlide = 0.0;
199 mSlideInc = 0.0;
200 return 0;
201 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 if (varName == "ui_progress_portion" || varName == "ui_progress_frames")
204 {
205 std::string str;
206 int cur;
Dees_Troy51a0e822012-09-05 15:24:24 -0400207
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200208 if (mSlideFrames)
209 {
210 mSlide += (mSlideInc * mSlideFrames);
211 cur = (int) mSlide;
212 DataManager::SetValue(mCurValVar, cur);
213 mSlideFrames = 0;
214 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400215
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 if (nextPush)
217 {
218 mSlide += nextPush;
219 cur = (int) mSlide;
220 DataManager::SetValue(mCurValVar, cur);
221 nextPush = 0;
222 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400223
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200224 if (varName == "ui_progress_portion") mSlide = atof(value.c_str());
225 else
226 {
227 mSlideFrames = atol(value.c_str());
228 if (mSlideFrames == 0)
229 {
230 // We're just holding this progress until the next push
231 nextPush = mSlide;
232 }
233 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 if (mSlide > 0 && mSlideFrames > 0)
236 {
237 // Get the current position
238 str.clear();
239 DataManager::GetValue(mCurValVar, str);
240 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400241
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 mSlideInc = (float) mSlide / (float) mSlideFrames;
243 mSlide = cur;
244 }
245 }
246 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247}