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