blob: 56ce48091d14ff16f38cb1e7fab75a488e42023b [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_attribute<>* attr;
49 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 mEmptyBar = NULL;
52 mFullBar = NULL;
53 mLastPos = 0;
54 mSlide = 0.0;
55 mSlideInc = 0.0;
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 if (!node)
58 {
59 LOGERR("GUIProgressBar created without XML node\n");
60 return;
61 }
Dees_Troy51a0e822012-09-05 15:24:24 -040062
Ethan Yonker21ff02a2015-02-18 14:35:00 -060063 child = FindNode(node, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020064 if (child)
65 {
thatf6ed8fc2015-02-14 20:23:16 +010066 mEmptyBar = LoadAttrImage(child, "empty");
67 mFullBar = LoadAttrImage(child, "full");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 }
Dees_Troy51a0e822012-09-05 15:24:24 -040069
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060071 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -040072
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 // Load the data
Ethan Yonker21ff02a2015-02-18 14:35:00 -060074 child = FindNode(node, "data");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020075 if (child)
76 {
thatf6ed8fc2015-02-14 20:23:16 +010077 mMinValVar = LoadAttrString(child, "min");
78 mMaxValVar = LoadAttrString(child, "max");
79 mCurValVar = LoadAttrString(child, "name");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 }
Dees_Troy51a0e822012-09-05 15:24:24 -040081
thatf6ed8fc2015-02-14 20:23:16 +010082 mRenderW = mEmptyBar->GetWidth();
83 mRenderH = mEmptyBar->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040084}
85
86int GUIProgressBar::Render(void)
87{
Matt Mowera8a89d12016-12-30 18:10:37 -060088 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +010089 return 0;
90
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 // This handles making sure timing updates occur
92 Update();
93 return RenderInternal();
Dees_Troy51a0e822012-09-05 15:24:24 -040094}
95
96int GUIProgressBar::RenderInternal(void)
97{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 if (!mEmptyBar || !mEmptyBar->GetResource())
99 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 if (!mFullBar || !mFullBar->GetResource())
102 return -1;
103
104 gr_blit(mEmptyBar->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
105 gr_blit(mFullBar->GetResource(), 0, 0, mLastPos, mRenderH, mRenderX, mRenderY);
106 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400107}
108
109int GUIProgressBar::Update(void)
110{
Matt Mowera8a89d12016-12-30 18:10:37 -0600111 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100112 return 0;
113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 std::string str;
115 int min, max, cur, pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 if (mMinValVar.empty())
118 min = 0;
119 else
120 {
121 str.clear();
122 if (atoi(mMinValVar.c_str()) != 0)
123 str = mMinValVar;
124 else
125 DataManager::GetValue(mMinValVar, str);
126 min = atoi(str.c_str());
127 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400128
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200129 if (mMaxValVar.empty())
130 max = 100;
131 else
132 {
133 str.clear();
134 if (atoi(mMaxValVar.c_str()) != 0)
135 str = mMaxValVar;
136 else
137 DataManager::GetValue(mMaxValVar, str);
138 max = atoi(str.c_str());
139 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 str.clear();
142 DataManager::GetValue(mCurValVar, str);
143 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 // Do slide, if needed
146 if (mSlideFrames)
147 {
148 mSlide += mSlideInc;
149 mSlideFrames--;
150 if (cur != (int) mSlide)
151 {
152 cur = (int) mSlide;
153 DataManager::SetValue(mCurValVar, cur);
154 }
155 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 // Normalize to 0
158 max -= min;
159 cur -= min;
160 min = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500162 if (cur < min)
163 cur = min;
164 if (cur > max)
165 cur = max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400166
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500167 if (max == 0)
168 pos = 0;
169 else
170 pos = (cur * mRenderW) / max;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 if (pos == mLastPos)
173 return 0;
174
175 mLastPos = pos;
176
177 if (RenderInternal() != 0)
178 return -1;
179 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400180}
181
Vojtech Bocek07220562014-02-08 02:05:33 +0100182int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400183{
Vojtech Bocek07220562014-02-08 02:05:33 +0100184 GUIObject::NotifyVarChange(varName, value);
185
Matt Mowera8a89d12016-12-30 18:10:37 -0600186 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100187 return 0;
188
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200189 static int nextPush = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 if (varName.empty())
192 {
193 nextPush = 0;
194 mLastPos = 0;
195 mSlide = 0.0;
196 mSlideInc = 0.0;
197 return 0;
198 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400199
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200200 if (varName == "ui_progress_portion" || varName == "ui_progress_frames")
201 {
202 std::string str;
203 int cur;
Dees_Troy51a0e822012-09-05 15:24:24 -0400204
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 if (mSlideFrames)
206 {
207 mSlide += (mSlideInc * mSlideFrames);
208 cur = (int) mSlide;
209 DataManager::SetValue(mCurValVar, cur);
210 mSlideFrames = 0;
211 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 if (nextPush)
214 {
215 mSlide += nextPush;
216 cur = (int) mSlide;
217 DataManager::SetValue(mCurValVar, cur);
218 nextPush = 0;
219 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400220
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 if (varName == "ui_progress_portion") mSlide = atof(value.c_str());
222 else
223 {
224 mSlideFrames = atol(value.c_str());
225 if (mSlideFrames == 0)
226 {
227 // We're just holding this progress until the next push
228 nextPush = mSlide;
229 }
230 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 if (mSlide > 0 && mSlideFrames > 0)
233 {
234 // Get the current position
235 str.clear();
236 DataManager::GetValue(mCurValVar, str);
237 cur = atoi(str.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 mSlideInc = (float) mSlide / (float) mSlideFrames;
240 mSlide = cur;
241 }
242 }
243 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400244}