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