Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 1 | /* |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 2 | Copyright 2012 to 2020 TeamWin |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 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 | // text.cpp - GUIText 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" |
| 42 | #include "minuitwrp/truetype.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
| 44 | #include "rapidxml.hpp" |
| 45 | #include "objects.hpp" |
| 46 | |
| 47 | GUIText::GUIText(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 48 | : GUIObject(node) |
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 | mFont = NULL; |
| 51 | mIsStatic = 1; |
| 52 | mVarChanged = 0; |
| 53 | mFontHeight = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | maxWidth = 0; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 55 | scaleWidth = true; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 56 | isHighlighted = false; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 57 | mText = ""; |
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 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 62 | // Load colors |
| 63 | mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255)); |
| 64 | mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor); |
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 | // Load the font, and possibly override the color |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 67 | mFont = LoadAttrFont(FindNode(node, "font"), "resource"); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 68 | if (!mFont || !mFont->GetResource()) |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 69 | return; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 70 | mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor); |
| 71 | mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor); |
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 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 74 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 76 | xml_node<>* child = FindNode(node, "text"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 77 | if (child) mText = child->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 79 | child = FindNode(node, "noscaling"); |
| 80 | if (child) { |
| 81 | scaleWidth = false; |
| 82 | } else { |
| 83 | if (mPlacement == TOP_LEFT || mPlacement == BOTTOM_LEFT) { |
| 84 | maxWidth = gr_fb_width() - mRenderX; |
| 85 | } else if (mPlacement == TOP_RIGHT || mPlacement == BOTTOM_RIGHT) { |
| 86 | maxWidth = mRenderX; |
| 87 | } else if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) { |
| 88 | if (mRenderX < gr_fb_width() / 2) { |
| 89 | maxWidth = mRenderX * 2; |
| 90 | } else { |
| 91 | maxWidth = (gr_fb_width() - mRenderX) * 2; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | // Simple way to check for static state |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 97 | mLastValue = gui_parse_text(mText); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 98 | if (mLastValue != mText) mIsStatic = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 99 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 100 | mFontHeight = mFont->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int GUIText::Render(void) |
| 104 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | if (!isConditionTrue()) |
| 106 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 108 | void* fontResource = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 109 | if (mFont) |
| 110 | fontResource = mFont->GetResource(); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 111 | else |
| 112 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 113 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 114 | mLastValue = gui_parse_text(mText); |
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 | mVarChanged = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 118 | if (isHighlighted) |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 119 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 120 | else |
| 121 | gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 123 | gr_textEx_scaleW(mRenderX, mRenderY, mLastValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 124 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int GUIText::Update(void) |
| 129 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 130 | if (!isConditionTrue()) |
| 131 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 132 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 133 | static int updateCounter = 3; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 135 | // This hack just makes sure we update at least once a minute for things like clock and battery |
| 136 | if (updateCounter) updateCounter--; |
| 137 | else |
| 138 | { |
| 139 | mVarChanged = 1; |
| 140 | updateCounter = 3; |
| 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 | if (mIsStatic || !mVarChanged) |
| 144 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 146 | std::string newValue = gui_parse_text(mText); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 147 | if (mLastValue == newValue) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 148 | return 0; |
| 149 | else |
| 150 | mLastValue = newValue; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int GUIText::GetCurrentBounds(int& w, int& h) |
| 155 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | void* fontResource = NULL; |
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 | if (mFont) |
| 159 | fontResource = mFont->GetResource(); |
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 | h = mFontHeight; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 162 | mLastValue = gui_parse_text(mText); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 163 | w = twrpTruetype::gr_ttf_measureEx(mLastValue.c_str(), fontResource); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 164 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 167 | int GUIText::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 168 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 169 | GUIObject::NotifyVarChange(varName, value); |
| 170 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | mVarChanged = 1; |
| 172 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | int GUIText::SetMaxWidth(unsigned width) |
| 176 | { |
| 177 | maxWidth = width; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 178 | if (!maxWidth) |
| 179 | scaleWidth = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 180 | mVarChanged = 1; |
| 181 | return 0; |
| 182 | } |
| 183 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 184 | void GUIText::SetText(string newtext) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | { |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 186 | mText = newtext; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 187 | } |