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 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 40 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | mButtonImg = NULL; |
| 49 | mButtonIcon = NULL; |
| 50 | mButtonLabel = NULL; |
| 51 | mAction = NULL; |
| 52 | mRendered = false; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 53 | hasHighlightColor = false; |
| 54 | renderHighlight = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 55 | hasFill = false; |
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) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Dees Troy | 4168a07 | 2013-11-29 05:01:51 +0000 | [diff] [blame] | 59 | // These can be loaded directly from the node |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | mButtonLabel = new GUIText(node); |
| 61 | mAction = new GUIAction(node); |
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 | mButtonImg = new GUIImage(node); |
| 64 | if (mButtonImg->Render() < 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 66 | delete mButtonImg; |
| 67 | mButtonImg = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 68 | } |
| 69 | if (mButtonLabel->Render() < 0) |
| 70 | { |
| 71 | delete mButtonLabel; |
| 72 | mButtonLabel = NULL; |
| 73 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 74 | // Load fill if it exists |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 75 | mFillColor = LoadAttrColor(FindNode(node, "fill"), "color", &hasFill); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 76 | if (!hasFill && mButtonImg == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 77 | LOGERR("No image resource or fill specified for button.\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 78 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 80 | // The icon is a special case |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 81 | mButtonIcon = LoadAttrImage(FindNode(node, "icon"), "resource"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 83 | mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlightColor); |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 84 | |
Matt Mower | c39b1b4 | 2016-01-01 18:38:43 -0600 | [diff] [blame] | 85 | int x = 0, y = 0, w = 0, h = 0; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 86 | TextPlacement = TOP_LEFT; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 87 | if (mButtonImg) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 88 | mButtonImg->GetRenderPos(x, y, w, h); |
| 89 | } else if (hasFill) { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 90 | LoadPlacement(FindNode(node, "placement"), &x, &y, &w, &h, &TextPlacement); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 91 | } |
| 92 | SetRenderPos(x, y, w, h); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 93 | if (mButtonLabel) { |
| 94 | TextPlacement = (Placement)LoadAttrInt(FindNode(node, "placement"), "textplacement", TOP_LEFT); |
| 95 | if (TextPlacement != TEXT_ONLY_RIGHT) { |
| 96 | mButtonLabel->scaleWidth = 1; |
| 97 | mButtonLabel->SetMaxWidth(w); |
| 98 | mButtonLabel->SetPlacement(CENTER); |
| 99 | mTextX = ((mRenderW / 2) + mRenderX); |
| 100 | mTextY = mRenderY + (mRenderH / 2); |
| 101 | mButtonLabel->SetRenderPos(mTextX, mTextY); |
| 102 | } else { |
| 103 | mTextX = mRenderW + mRenderX + 5; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 104 | mButtonLabel->GetCurrentBounds(mTextW, mTextH); |
Ethan Yonker | fd52b98 | 2015-11-06 15:24:26 -0600 | [diff] [blame] | 105 | mRenderW += mTextW + 5; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 106 | mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2); |
| 107 | mButtonLabel->SetRenderPos(mTextX, mTextY); |
Ethan Yonker | fd52b98 | 2015-11-06 15:24:26 -0600 | [diff] [blame] | 108 | if (mAction) |
| 109 | mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 110 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 111 | } |
| 112 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | GUIButton::~GUIButton() |
| 116 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 117 | delete mButtonImg; |
| 118 | delete mButtonLabel; |
| 119 | delete mAction; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int GUIButton::Render(void) |
| 123 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 124 | if (!isConditionTrue()) |
| 125 | { |
| 126 | mRendered = false; |
| 127 | return 0; |
| 128 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 129 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 130 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | if (mButtonImg) ret = mButtonImg->Render(); |
| 133 | if (ret < 0) return ret; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 134 | if (hasFill) { |
| 135 | gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha); |
| 136 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 137 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 138 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 139 | gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY); |
| 140 | if (mButtonLabel) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 141 | int w, h; |
| 142 | mButtonLabel->GetCurrentBounds(w, h); |
| 143 | if (w != mTextW) { |
| 144 | mTextW = w; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 145 | } |
| 146 | ret = mButtonLabel->Render(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 147 | if (ret < 0) return ret; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 148 | } |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 149 | if (renderHighlight && hasHighlightColor) { |
| 150 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 151 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 152 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 153 | mRendered = true; |
| 154 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int GUIButton::Update(void) |
| 158 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 159 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 160 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 161 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 162 | int ret = 0, ret2 = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 163 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 164 | if (mButtonImg) ret = mButtonImg->Update(); |
| 165 | if (ret < 0) return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 166 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 167 | if (ret == 0) |
| 168 | { |
| 169 | if (mButtonLabel) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 170 | ret2 = mButtonLabel->Update(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | if (ret2 < 0) return ret2; |
| 172 | if (ret2 > ret) ret = ret2; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 173 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 174 | } |
| 175 | else if (ret == 1) |
| 176 | { |
| 177 | // The button re-rendered, so everyone else is a render |
| 178 | if (mButtonIcon && mButtonIcon->GetResource()) |
| 179 | gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY); |
| 180 | if (mButtonLabel) ret = mButtonLabel->Render(); |
| 181 | if (ret < 0) return ret; |
| 182 | ret = 1; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | // Aparently, the button needs a background update |
| 187 | ret = 2; |
| 188 | } |
| 189 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int GUIButton::SetRenderPos(int x, int y, int w, int h) |
| 193 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 194 | mRenderX = x; |
| 195 | mRenderY = y; |
| 196 | if (w || h) |
| 197 | { |
| 198 | mRenderW = w; |
| 199 | mRenderH = h; |
| 200 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 201 | mIconW = mIconH = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 203 | if (mButtonIcon && mButtonIcon->GetResource()) { |
| 204 | mIconW = mButtonIcon->GetWidth(); |
| 205 | mIconH = mButtonIcon->GetHeight(); |
| 206 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 208 | mTextH = 0; |
| 209 | mTextW = 0; |
| 210 | mIconX = mRenderX + ((mRenderW - mIconW) / 2); |
| 211 | if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 212 | if (mTextW && TextPlacement == TEXT_ONLY_RIGHT) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 213 | { |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 214 | mRenderW += mTextW + 5; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 215 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 217 | if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH) |
| 218 | { |
| 219 | mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 220 | } |
| 221 | else |
| 222 | { |
| 223 | int divisor = mRenderH - (mIconH + mTextH); |
| 224 | mIconY = mRenderY + (divisor / 3); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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 | if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY); |
| 228 | if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 229 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 230 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 234 | { |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 235 | static int last_state = 0; |
| 236 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 237 | if (!isConditionTrue()) return -1; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 238 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) { |
| 239 | if (last_state == 1) { |
| 240 | last_state = 0; |
| 241 | if (mButtonLabel != NULL) |
| 242 | mButtonLabel->isHighlighted = false; |
| 243 | if (mButtonImg != NULL) |
| 244 | mButtonImg->isHighlighted = false; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 245 | renderHighlight = false; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 246 | mRendered = false; |
| 247 | } |
| 248 | } else { |
| 249 | if (last_state == 0) { |
| 250 | last_state = 1; |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 251 | DataManager::Vibrate("tw_button_vibrate"); |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 252 | if (mButtonLabel != NULL) |
| 253 | mButtonLabel->isHighlighted = true; |
| 254 | if (mButtonImg != NULL) |
| 255 | mButtonImg->isHighlighted = true; |
Dees_Troy | 1a7a667 | 2013-02-15 09:39:07 -0600 | [diff] [blame] | 256 | renderHighlight = true; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 257 | mRendered = false; |
| 258 | } |
| 259 | } |
| 260 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) |
| 261 | return 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 262 | return (mAction ? mAction->NotifyTouch(state, x, y) : 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 263 | } |