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; |
| 113 | if (mButtonImg) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 114 | mButtonImg->GetRenderPos(x, y, w, h); |
| 115 | } else if (hasFill) { |
| 116 | LoadPlacement(node->first_node("placement"), &x, &y, &w, &h); |
| 117 | } |
| 118 | SetRenderPos(x, y, w, h); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 119 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | GUIButton::~GUIButton() |
| 123 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 124 | if (mButtonImg) delete mButtonImg; |
| 125 | if (mButtonLabel) delete mButtonLabel; |
| 126 | if (mAction) delete mAction; |
| 127 | if (mButtonIcon) delete mButtonIcon; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int GUIButton::Render(void) |
| 131 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | if (!isConditionTrue()) |
| 133 | { |
| 134 | mRendered = false; |
| 135 | return 0; |
| 136 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 138 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 139 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 140 | if (mButtonImg) ret = mButtonImg->Render(); |
| 141 | if (ret < 0) return ret; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 142 | if (hasFill) { |
| 143 | gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha); |
| 144 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 145 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 147 | gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY); |
| 148 | if (mButtonLabel) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 149 | int w, h; |
| 150 | mButtonLabel->GetCurrentBounds(w, h); |
| 151 | if (w != mTextW) { |
| 152 | mTextW = w; |
| 153 | // As a special case, we'll allow large text which automatically moves it to the right. |
| 154 | if (mTextW > mRenderW) |
| 155 | { |
| 156 | mTextX = mRenderW + mRenderX + 5; |
| 157 | mRenderW += mTextW + 5; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | mTextX = mRenderX + ((mRenderW - mTextW) / 2); |
| 162 | } |
| 163 | mButtonLabel->SetRenderPos(mTextX, mTextY); |
| 164 | } |
| 165 | ret = mButtonLabel->Render(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 166 | if (ret < 0) return ret; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 167 | } |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 168 | if (renderHighlight && hasHighlightColor) { |
| 169 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 170 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 171 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 172 | mRendered = true; |
| 173 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int GUIButton::Update(void) |
| 177 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 178 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 179 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 180 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 181 | int ret = 0, ret2 = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 183 | if (mButtonImg) ret = mButtonImg->Update(); |
| 184 | if (ret < 0) return ret; |
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 (ret == 0) |
| 187 | { |
| 188 | if (mButtonLabel) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 189 | ret2 = mButtonLabel->Update(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 190 | if (ret2 < 0) return ret2; |
| 191 | if (ret2 > ret) ret = ret2; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 192 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 193 | } |
| 194 | else if (ret == 1) |
| 195 | { |
| 196 | // The button re-rendered, so everyone else is a render |
| 197 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 198 | gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY); |
| 199 | if (mButtonLabel) ret = mButtonLabel->Render(); |
| 200 | if (ret < 0) return ret; |
| 201 | ret = 1; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | // Aparently, the button needs a background update |
| 206 | ret = 2; |
| 207 | } |
| 208 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int GUIButton::SetRenderPos(int x, int y, int w, int h) |
| 212 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 213 | mRenderX = x; |
| 214 | mRenderY = y; |
| 215 | if (w || h) |
| 216 | { |
| 217 | mRenderW = w; |
| 218 | mRenderH = h; |
| 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 | mIconW = 0; mIconH = 0; |
| 222 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 223 | { |
| 224 | mIconW = gr_get_width(mButtonIcon->GetResource()); |
| 225 | mIconH = gr_get_height(mButtonIcon->GetResource()); |
| 226 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 227 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 228 | mTextH = 0; |
| 229 | mTextW = 0; |
| 230 | mIconX = mRenderX + ((mRenderW - mIconW) / 2); |
| 231 | if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH); |
| 232 | if (mTextW) |
| 233 | { |
| 234 | // As a special case, we'll allow large text which automatically moves it to the right. |
| 235 | if (mTextW > mRenderW) |
| 236 | { |
| 237 | mTextX = mRenderW + mRenderX + 5; |
| 238 | mRenderW += mTextW + 5; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | mTextX = mRenderX + ((mRenderW - mTextW) / 2); |
| 243 | } |
| 244 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 245 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 246 | if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH) |
| 247 | { |
| 248 | mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2); |
| 249 | mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | int divisor = mRenderH - (mIconH + mTextH); |
| 254 | mIconY = mRenderY + (divisor / 3); |
| 255 | mTextY = mRenderY + (divisor * 2 / 3) + mIconH; |
| 256 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 257 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 258 | if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY); |
| 259 | if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 260 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 261 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 265 | { |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 266 | static int last_state = 0; |
| 267 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 268 | if (!isConditionTrue()) return -1; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 269 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) { |
| 270 | if (last_state == 1) { |
| 271 | last_state = 0; |
| 272 | if (mButtonLabel != NULL) |
| 273 | mButtonLabel->isHighlighted = false; |
| 274 | if (mButtonImg != NULL) |
| 275 | mButtonImg->isHighlighted = false; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 276 | renderHighlight = false; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 277 | mRendered = false; |
| 278 | } |
| 279 | } else { |
| 280 | if (last_state == 0) { |
| 281 | last_state = 1; |
| 282 | if (mButtonLabel != NULL) |
| 283 | mButtonLabel->isHighlighted = true; |
| 284 | if (mButtonImg != NULL) |
| 285 | mButtonImg->isHighlighted = true; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 286 | renderHighlight = true; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 287 | mRendered = false; |
| 288 | } |
| 289 | } |
| 290 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) |
| 291 | return 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 292 | return (mAction ? mAction->NotifyTouch(state, x, y) : 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 293 | } |