blob: b72dd047c9b8a5966f3ffe3bc995180ccd801ada [file] [log] [blame]
Ethan Yonkera5db7122016-03-14 15:47:09 -05001/*
bigbiffd58ba182020-03-23 10:02:29 -04002 Copyright 2012 to 2020 TeamWin
Ethan Yonkera5db7122016-03-14 15:47:09 -05003 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_Troy51a0e822012-09-05 15:24:24 -040019// 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
38extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010041#include "../minuitwrp/minui.h"
bigbiffd58ba182020-03-23 10:02:29 -040042#include "../minuitwrp/truetype.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040043
44#include "rapidxml.hpp"
45#include "objects.hpp"
46
47GUIText::GUIText(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010048 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040049{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 mFont = NULL;
51 mIsStatic = 1;
52 mVarChanged = 0;
53 mFontHeight = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040054 maxWidth = 0;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050055 scaleWidth = true;
Dees_Troy4d12f962012-10-19 13:13:15 -040056 isHighlighted = false;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050057 mText = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (!node)
60 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040061
thatf6ed8fc2015-02-14 20:23:16 +010062 // Load colors
63 mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255));
64 mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 // Load the font, and possibly override the color
Ethan Yonker21ff02a2015-02-18 14:35:00 -060067 mFont = LoadAttrFont(FindNode(node, "font"), "resource");
Ethan Yonker58f21322018-08-24 11:17:36 -050068 if (!mFont || !mFont->GetResource())
Ethan Yonkerfbb43532015-12-28 21:54:50 +010069 return;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060070 mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor);
71 mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040072
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060074 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Ethan Yonker21ff02a2015-02-18 14:35:00 -060076 xml_node<>* child = FindNode(node, "text");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050079 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 Bocekfafb0c52013-07-25 22:53:02 +020096 // Simple way to check for static state
thatb2e8f672015-03-05 20:25:39 +010097 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040099
thatf6ed8fc2015-02-14 20:23:16 +0100100 mFontHeight = mFont->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -0400101}
102
103int GUIText::Render(void)
104{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 if (!isConditionTrue())
106 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400107
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 void* fontResource = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 if (mFont)
110 fontResource = mFont->GetResource();
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100111 else
112 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
thatb2e8f672015-03-05 20:25:39 +0100114 mLastValue = gui_parse_text(mText);
Dees_Troy51a0e822012-09-05 15:24:24 -0400115
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
thatf6ed8fc2015-02-14 20:23:16 +0100118 if (isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400119 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
120 else
121 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
Ethan Yonkera5db7122016-03-14 15:47:09 -0500123 gr_textEx_scaleW(mRenderX, mRenderY, mLastValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500124
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400126}
127
128int GUIText::Update(void)
129{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 if (!isConditionTrue())
131 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400132
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 // 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_Troy51a0e822012-09-05 15:24:24 -0400142
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 if (mIsStatic || !mVarChanged)
144 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
thatb2e8f672015-03-05 20:25:39 +0100146 std::string newValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500148 return 0;
149 else
150 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152}
153
154int GUIText::GetCurrentBounds(int& w, int& h)
155{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 if (mFont)
159 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400160
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 h = mFontHeight;
thatb2e8f672015-03-05 20:25:39 +0100162 mLastValue = gui_parse_text(mText);
bigbiffd58ba182020-03-23 10:02:29 -0400163 w = twrpTruetype::gr_ttf_measureEx(mLastValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200164 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400165}
166
Vojtech Bocek07220562014-02-08 02:05:33 +0100167int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400168{
Vojtech Bocek07220562014-02-08 02:05:33 +0100169 GUIObject::NotifyVarChange(varName, value);
170
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 mVarChanged = 1;
172 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400173}
174
175int GUIText::SetMaxWidth(unsigned width)
176{
177 maxWidth = width;
Ethan Yonkera5db7122016-03-14 15:47:09 -0500178 if (!maxWidth)
179 scaleWidth = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400180 mVarChanged = 1;
181 return 0;
182}
183
Ethan Yonkera5db7122016-03-14 15:47:09 -0500184void GUIText::SetText(string newtext)
Dees_Troy51a0e822012-09-05 15:24:24 -0400185{
Ethan Yonkera5db7122016-03-14 15:47:09 -0500186 mText = newtext;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187}