Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
| 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // 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 | |
| 38 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 39 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 41 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
| 43 | #include "rapidxml.hpp" |
| 44 | #include "objects.hpp" |
| 45 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 46 | GUIProgressBar::GUIProgressBar(xml_node<>* node) : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 48 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 50 | mEmptyBar = NULL; |
| 51 | mFullBar = NULL; |
| 52 | mLastPos = 0; |
| 53 | mSlide = 0.0; |
| 54 | mSlideInc = 0.0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 56 | if (!node) |
| 57 | { |
| 58 | LOGERR("GUIProgressBar created without XML node\n"); |
| 59 | return; |
| 60 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 62 | child = FindNode(node, "resource"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 63 | if (child) |
| 64 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 65 | mEmptyBar = LoadAttrImage(child, "empty"); |
| 66 | mFullBar = LoadAttrImage(child, "full"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 69 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 70 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 71 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 72 | // Load the data |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 73 | child = FindNode(node, "data"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 74 | if (child) |
| 75 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 76 | mMinValVar = LoadAttrString(child, "min"); |
| 77 | mMaxValVar = LoadAttrString(child, "max"); |
| 78 | mCurValVar = LoadAttrString(child, "name"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 79 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 80 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 81 | if (mEmptyBar && mEmptyBar->GetResource()) { |
| 82 | mRenderW = mEmptyBar->GetWidth(); |
| 83 | mRenderH = mEmptyBar->GetHeight(); |
| 84 | } else { |
| 85 | mRenderW = mRenderH = 0; |
| 86 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int GUIProgressBar::Render(void) |
| 90 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 91 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 92 | return 0; |
| 93 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 94 | // This handles making sure timing updates occur |
| 95 | Update(); |
| 96 | return RenderInternal(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int GUIProgressBar::RenderInternal(void) |
| 100 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 101 | if (!mEmptyBar || !mEmptyBar->GetResource()) |
| 102 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 104 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int GUIProgressBar::Update(void) |
| 113 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 114 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 115 | return 0; |
| 116 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 117 | std::string str; |
| 118 | int min, max, cur, pos; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 144 | str.clear(); |
| 145 | DataManager::GetValue(mCurValVar, str); |
| 146 | cur = atoi(str.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 147 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 148 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 160 | // Normalize to 0 |
| 161 | max -= min; |
| 162 | cur -= min; |
| 163 | min = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 165 | if (cur < min) |
| 166 | cur = min; |
| 167 | if (cur > max) |
| 168 | cur = max; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 169 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 170 | if (max == 0) |
| 171 | pos = 0; |
| 172 | else |
| 173 | pos = (cur * mRenderW) / max; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 174 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 175 | if (pos == mLastPos) |
| 176 | return 0; |
| 177 | |
| 178 | mLastPos = pos; |
| 179 | |
| 180 | if (RenderInternal() != 0) |
| 181 | return -1; |
| 182 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 185 | int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 186 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 187 | GUIObject::NotifyVarChange(varName, value); |
| 188 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 189 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 190 | return 0; |
| 191 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 192 | static int nextPush = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 193 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 194 | if (varName.empty()) |
| 195 | { |
| 196 | nextPush = 0; |
| 197 | mLastPos = 0; |
| 198 | mSlide = 0.0; |
| 199 | mSlideInc = 0.0; |
| 200 | return 0; |
| 201 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 203 | if (varName == "ui_progress_portion" || varName == "ui_progress_frames") |
| 204 | { |
| 205 | std::string str; |
| 206 | int cur; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 208 | if (mSlideFrames) |
| 209 | { |
| 210 | mSlide += (mSlideInc * mSlideFrames); |
| 211 | cur = (int) mSlide; |
| 212 | DataManager::SetValue(mCurValVar, cur); |
| 213 | mSlideFrames = 0; |
| 214 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 215 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 216 | if (nextPush) |
| 217 | { |
| 218 | mSlide += nextPush; |
| 219 | cur = (int) mSlide; |
| 220 | DataManager::SetValue(mCurValVar, cur); |
| 221 | nextPush = 0; |
| 222 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 224 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 234 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 235 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 241 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 242 | mSlideInc = (float) mSlide / (float) mSlideFrames; |
| 243 | mSlide = cur; |
| 244 | } |
| 245 | } |
| 246 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | } |