Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy 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 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/reboot.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <time.h> |
| 31 | #include <unistd.h> |
| 32 | #include <stdlib.h> |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 33 | #include "../data.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
| 35 | #include <string> |
| 36 | |
| 37 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 38 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | #include "rapidxml.hpp" |
| 43 | #include "objects.hpp" |
| 44 | |
| 45 | GUIButton::GUIButton(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 46 | : 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 | mButtonImg = NULL; |
| 52 | mButtonIcon = NULL; |
| 53 | mButtonLabel = NULL; |
| 54 | mAction = NULL; |
| 55 | mRendered = false; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 56 | hasHighlightColor = false; |
| 57 | renderHighlight = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 58 | hasFill = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | if (!node) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
Dees Troy | 4168a07 | 2013-11-29 05:01:51 +0000 | [diff] [blame] | 62 | // These can be loaded directly from the node |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 63 | mButtonLabel = new GUIText(node); |
| 64 | mAction = new GUIAction(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | |
Dees Troy | 4168a07 | 2013-11-29 05:01:51 +0000 | [diff] [blame] | 66 | child = node->first_node("image"); |
| 67 | if (child) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 68 | { |
Dees Troy | 4168a07 | 2013-11-29 05:01:51 +0000 | [diff] [blame] | 69 | mButtonImg = new GUIImage(node); |
| 70 | if (mButtonImg->Render() < 0) |
| 71 | { |
| 72 | delete mButtonImg; |
| 73 | mButtonImg = NULL; |
| 74 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 75 | } |
| 76 | if (mButtonLabel->Render() < 0) |
| 77 | { |
| 78 | delete mButtonLabel; |
| 79 | mButtonLabel = NULL; |
| 80 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 81 | // Load fill if it exists |
| 82 | memset(&mFillColor, 0, sizeof(COLOR)); |
| 83 | child = node->first_node("fill"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 84 | if (child) |
| 85 | { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 86 | attr = child->first_attribute("color"); |
| 87 | if (attr) { |
| 88 | hasFill = true; |
| 89 | std::string color = attr->value(); |
| 90 | ConvertStrToColor(color, &mFillColor); |
| 91 | } |
| 92 | } |
| 93 | if (!hasFill && mButtonImg == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 94 | LOGERR("No image resource or fill specified for button.\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 95 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 96 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | // The icon is a special case |
| 98 | child = node->first_node("icon"); |
| 99 | if (child) |
| 100 | { |
| 101 | attr = child->first_attribute("resource"); |
| 102 | if (attr) |
| 103 | mButtonIcon = PageManager::FindResource(attr->value()); |
| 104 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 106 | memset(&mHighlightColor, 0, sizeof(COLOR)); |
| 107 | child = node->first_node("highlight"); |
| 108 | if (child) { |
| 109 | attr = child->first_attribute("color"); |
| 110 | if (attr) { |
| 111 | hasHighlightColor = true; |
| 112 | std::string color = attr->value(); |
| 113 | ConvertStrToColor(color, &mHighlightColor); |
| 114 | } |
| 115 | } |
| 116 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 117 | int x, y, w, h; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 118 | TextPlacement = TOP_LEFT; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 119 | if (mButtonImg) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 120 | mButtonImg->GetRenderPos(x, y, w, h); |
| 121 | } else if (hasFill) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 122 | LoadPlacement(node->first_node("placement"), &x, &y, &w, &h, &TextPlacement); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 123 | } |
| 124 | SetRenderPos(x, y, w, h); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | GUIButton::~GUIButton() |
| 129 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 130 | if (mButtonImg) delete mButtonImg; |
| 131 | if (mButtonLabel) delete mButtonLabel; |
| 132 | if (mAction) delete mAction; |
| 133 | if (mButtonIcon) delete mButtonIcon; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int GUIButton::Render(void) |
| 137 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 138 | if (!isConditionTrue()) |
| 139 | { |
| 140 | mRendered = false; |
| 141 | return 0; |
| 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 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | if (mButtonImg) ret = mButtonImg->Render(); |
| 147 | if (ret < 0) return ret; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 148 | if (hasFill) { |
| 149 | gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha); |
| 150 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 151 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 152 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 153 | gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY); |
| 154 | if (mButtonLabel) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 155 | int w, h; |
| 156 | mButtonLabel->GetCurrentBounds(w, h); |
| 157 | if (w != mTextW) { |
| 158 | mTextW = w; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 159 | if (TextPlacement == CENTER_X_ONLY) { |
| 160 | mTextX = ((mRenderW - mRenderX) / 2); |
| 161 | } else if (mTextW > mRenderW) { // As a special case, we'll allow large text which automatically moves it to the right. |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 162 | mTextX = mRenderW + mRenderX + 5; |
| 163 | mRenderW += mTextW + 5; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 164 | } else { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 165 | mTextX = mRenderX + ((mRenderW - mTextW) / 2); |
| 166 | } |
| 167 | mButtonLabel->SetRenderPos(mTextX, mTextY); |
| 168 | } |
| 169 | ret = mButtonLabel->Render(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 170 | if (ret < 0) return ret; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 171 | } |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 172 | if (renderHighlight && hasHighlightColor) { |
| 173 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 174 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 175 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 176 | mRendered = true; |
| 177 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int GUIButton::Update(void) |
| 181 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 182 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 183 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 185 | int ret = 0, ret2 = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 186 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 187 | if (mButtonImg) ret = mButtonImg->Update(); |
| 188 | if (ret < 0) return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 190 | if (ret == 0) |
| 191 | { |
| 192 | if (mButtonLabel) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 193 | ret2 = mButtonLabel->Update(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 194 | if (ret2 < 0) return ret2; |
| 195 | if (ret2 > ret) ret = ret2; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 196 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 197 | } |
| 198 | else if (ret == 1) |
| 199 | { |
| 200 | // The button re-rendered, so everyone else is a render |
| 201 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 202 | gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY); |
| 203 | if (mButtonLabel) ret = mButtonLabel->Render(); |
| 204 | if (ret < 0) return ret; |
| 205 | ret = 1; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | // Aparently, the button needs a background update |
| 210 | ret = 2; |
| 211 | } |
| 212 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int GUIButton::SetRenderPos(int x, int y, int w, int h) |
| 216 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 217 | mRenderX = x; |
| 218 | mRenderY = y; |
| 219 | if (w || h) |
| 220 | { |
| 221 | mRenderW = w; |
| 222 | mRenderH = h; |
| 223 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 225 | mIconW = 0; mIconH = 0; |
| 226 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 227 | { |
| 228 | mIconW = gr_get_width(mButtonIcon->GetResource()); |
| 229 | mIconH = gr_get_height(mButtonIcon->GetResource()); |
| 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 | mTextH = 0; |
| 233 | mTextW = 0; |
| 234 | mIconX = mRenderX + ((mRenderW - mIconW) / 2); |
| 235 | if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH); |
| 236 | if (mTextW) |
| 237 | { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 238 | if (TextPlacement == CENTER_X_ONLY) { |
| 239 | mTextX = ((mRenderW - mRenderX) / 2); |
| 240 | } else if (mTextW > mRenderW) { // As a special case, we'll allow large text which automatically moves it to the right. |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 241 | mTextX = mRenderW + mRenderX + 5; |
| 242 | mRenderW += mTextW + 5; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 243 | } else { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 244 | mTextX = mRenderX + ((mRenderW - mTextW) / 2); |
| 245 | } |
| 246 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 248 | if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH) |
| 249 | { |
| 250 | mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2); |
| 251 | mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | int divisor = mRenderH - (mIconH + mTextH); |
| 256 | mIconY = mRenderY + (divisor / 3); |
| 257 | mTextY = mRenderY + (divisor * 2 / 3) + mIconH; |
| 258 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 259 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 260 | if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY); |
| 261 | if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 262 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 263 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 267 | { |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 268 | static int last_state = 0; |
| 269 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 270 | if (!isConditionTrue()) return -1; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 271 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) { |
| 272 | if (last_state == 1) { |
| 273 | last_state = 0; |
| 274 | if (mButtonLabel != NULL) |
| 275 | mButtonLabel->isHighlighted = false; |
| 276 | if (mButtonImg != NULL) |
| 277 | mButtonImg->isHighlighted = false; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 278 | renderHighlight = false; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 279 | mRendered = false; |
| 280 | } |
| 281 | } else { |
| 282 | if (last_state == 0) { |
| 283 | last_state = 1; |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 284 | DataManager::Vibrate("tw_button_vibrate"); |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 285 | if (mButtonLabel != NULL) |
| 286 | mButtonLabel->isHighlighted = true; |
| 287 | if (mButtonImg != NULL) |
| 288 | mButtonImg->isHighlighted = true; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 289 | renderHighlight = true; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 290 | mRendered = false; |
| 291 | } |
| 292 | } |
| 293 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) |
| 294 | return 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 295 | return (mAction ? mAction->NotifyTouch(state, x, y) : 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 296 | } |