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