Matt Mower | 37a7ab6 | 2017-01-18 20:06:08 -0600 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 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 | */ |
| 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // checkbox.cpp - GUICheckbox object |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/reboot.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include <string> |
| 37 | |
| 38 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 39 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | } |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 41 | #include "minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
| 43 | #include "rapidxml.hpp" |
| 44 | #include "objects.hpp" |
| 45 | |
| 46 | GUICheckbox::GUICheckbox(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 47 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 49 | xml_attribute<>* attr; |
| 50 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 52 | mChecked = NULL; |
| 53 | mUnchecked = NULL; |
| 54 | mLabel = NULL; |
| 55 | mRendered = 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 | mLastState = 0; |
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) |
| 60 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 62 | // The label can be loaded directly |
| 63 | mLabel = new GUIText(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | // Read the check states |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 66 | child = FindNode(node, "image"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | if (child) |
| 68 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 69 | mChecked = LoadAttrImage(child, "checked"); |
| 70 | mUnchecked = LoadAttrImage(child, "unchecked"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 71 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 72 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 73 | // Get the variable data |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 74 | child = FindNode(node, "data"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 75 | if (child) |
| 76 | { |
| 77 | attr = child->first_attribute("variable"); |
| 78 | if (attr) |
| 79 | mVarName = attr->value(); |
| 80 | attr = child->first_attribute("default"); |
Ethan Yonker | b150276 | 2019-03-25 15:38:34 -0500 | [diff] [blame] | 81 | if (attr) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 82 | DataManager::SetValue(mVarName, attr->value()); |
Ethan Yonker | b150276 | 2019-03-25 15:38:34 -0500 | [diff] [blame] | 83 | } else { |
| 84 | int val; |
| 85 | if (DataManager::GetValue(mVarName, val) != 0) |
| 86 | DataManager::SetValue(mVarName, 0); // Prevents check boxes from having to be tapped twice the first time |
| 87 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 90 | mCheckW = mCheckH = 0; |
| 91 | if (mChecked && mChecked->GetResource()) { |
| 92 | mCheckW = mChecked->GetWidth(); |
| 93 | mCheckH = mChecked->GetHeight(); |
| 94 | } else if (mUnchecked && mUnchecked->GetResource()) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 95 | mCheckW = mUnchecked->GetWidth(); |
| 96 | mCheckH = mUnchecked->GetHeight(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | int x, y, w, h; |
| 100 | mLabel->GetRenderPos(x, y, w, h); |
| 101 | SetRenderPos(x, y, 0, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | GUICheckbox::~GUICheckbox() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | int GUICheckbox::Render(void) |
| 109 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 110 | if (!isConditionTrue()) |
| 111 | { |
| 112 | mRendered = false; |
| 113 | return 0; |
| 114 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | int ret = 0; |
| 117 | int lastState = 0; |
| 118 | DataManager::GetValue(mVarName, lastState); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | if (lastState) |
| 121 | { |
| 122 | if (mChecked && mChecked->GetResource()) |
| 123 | gr_blit(mChecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | if (mUnchecked && mUnchecked->GetResource()) |
| 128 | gr_blit(mUnchecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY); |
| 129 | } |
| 130 | if (mLabel) |
| 131 | ret = mLabel->Render(); |
| 132 | mLastState = lastState; |
| 133 | mRendered = true; |
| 134 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int GUICheckbox::Update(void) |
| 138 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 139 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 140 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 141 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 142 | int lastState = 0; |
| 143 | DataManager::GetValue(mVarName, lastState); |
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 (lastState != mLastState) |
| 146 | return 2; |
| 147 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int GUICheckbox::SetRenderPos(int x, int y, int w, int h) |
| 151 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 152 | mRenderX = x; |
| 153 | mRenderY = y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 154 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | if (w || h) |
| 156 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | int textW, textH; |
| 159 | mLabel->GetCurrentBounds(textW, textH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 160 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 161 | w = textW + mCheckW + 5; |
| 162 | mRenderW = w; |
| 163 | mRenderH = mCheckH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 165 | mTextX = mRenderX + mCheckW + 5; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 166 | mTextY = mRenderY + (mCheckH / 2); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 168 | mLabel->SetRenderPos(mTextX, mTextY, 0, 0); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 169 | mLabel->SetPlacement(TEXT_ONLY_RIGHT); |
| 170 | mLabel->SetMaxWidth(gr_fb_width() - mTextX); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 172 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 175 | int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 176 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 177 | if (!isConditionTrue()) |
| 178 | return -1; |
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 | if (state == TOUCH_RELEASE) |
| 181 | { |
| 182 | int lastState; |
| 183 | DataManager::GetValue(mVarName, lastState); |
| 184 | lastState = (lastState == 0) ? 1 : 0; |
| 185 | DataManager::SetValue(mVarName, lastState); |
Vojtech Bocek | 5af8f3f | 2014-02-08 02:21:23 +0100 | [diff] [blame] | 186 | |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 187 | #ifndef TW_NO_HAPTICS |
Vojtech Bocek | 5af8f3f | 2014-02-08 02:21:23 +0100 | [diff] [blame] | 188 | DataManager::Vibrate("tw_button_vibrate"); |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 189 | #endif |
| 190 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 191 | } |
| 192 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 193 | } |
| 194 | |