blob: 61940ff9fd0a7e7a3220753a8497c633674b13a5 [file] [log] [blame]
Ethan Yonkera5db7122016-03-14 15:47:09 -05001/*
2 Copyright 2012 to 2016 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*/
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"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
46GUIText::GUIText(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010047 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040048{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020049 mFont = NULL;
50 mIsStatic = 1;
51 mVarChanged = 0;
52 mFontHeight = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040053 maxWidth = 0;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050054 scaleWidth = true;
Dees_Troy4d12f962012-10-19 13:13:15 -040055 isHighlighted = false;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050056 mText = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040057
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020058 if (!node)
59 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040060
thatf6ed8fc2015-02-14 20:23:16 +010061 // Load colors
62 mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255));
63 mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040064
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020065 // Load the font, and possibly override the color
Ethan Yonker21ff02a2015-02-18 14:35:00 -060066 mFont = LoadAttrFont(FindNode(node, "font"), "resource");
Ethan Yonkerfbb43532015-12-28 21:54:50 +010067 if (!mFont)
68 return;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060069 mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor);
70 mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040071
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060073 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040074
Ethan Yonker21ff02a2015-02-18 14:35:00 -060075 xml_node<>* child = FindNode(node, "text");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050078 child = FindNode(node, "noscaling");
79 if (child) {
80 scaleWidth = false;
81 } else {
82 if (mPlacement == TOP_LEFT || mPlacement == BOTTOM_LEFT) {
83 maxWidth = gr_fb_width() - mRenderX;
84 } else if (mPlacement == TOP_RIGHT || mPlacement == BOTTOM_RIGHT) {
85 maxWidth = mRenderX;
86 } else if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) {
87 if (mRenderX < gr_fb_width() / 2) {
88 maxWidth = mRenderX * 2;
89 } else {
90 maxWidth = (gr_fb_width() - mRenderX) * 2;
91 }
92 }
93 }
94
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 // Simple way to check for static state
thatb2e8f672015-03-05 20:25:39 +010096 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040098
thatf6ed8fc2015-02-14 20:23:16 +010099 mFontHeight = mFont->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -0400100}
101
102int GUIText::Render(void)
103{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 if (!isConditionTrue())
105 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 void* fontResource = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 if (mFont)
109 fontResource = mFont->GetResource();
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100110 else
111 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400112
thatb2e8f672015-03-05 20:25:39 +0100113 mLastValue = gui_parse_text(mText);
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
thatf6ed8fc2015-02-14 20:23:16 +0100117 if (isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400118 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
119 else
120 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
Ethan Yonkera5db7122016-03-14 15:47:09 -0500122 gr_textEx_scaleW(mRenderX, mRenderY, mLastValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500123
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125}
126
127int GUIText::Update(void)
128{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200129 if (!isConditionTrue())
130 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 // This hack just makes sure we update at least once a minute for things like clock and battery
135 if (updateCounter) updateCounter--;
136 else
137 {
138 mVarChanged = 1;
139 updateCounter = 3;
140 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 if (mIsStatic || !mVarChanged)
143 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
thatb2e8f672015-03-05 20:25:39 +0100145 std::string newValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500147 return 0;
148 else
149 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151}
152
153int GUIText::GetCurrentBounds(int& w, int& h)
154{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (mFont)
158 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 h = mFontHeight;
thatb2e8f672015-03-05 20:25:39 +0100161 mLastValue = gui_parse_text(mText);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100162 w = gr_ttf_measureEx(mLastValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164}
165
Vojtech Bocek07220562014-02-08 02:05:33 +0100166int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400167{
Vojtech Bocek07220562014-02-08 02:05:33 +0100168 GUIObject::NotifyVarChange(varName, value);
169
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200170 mVarChanged = 1;
171 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172}
173
174int GUIText::SetMaxWidth(unsigned width)
175{
176 maxWidth = width;
Ethan Yonkera5db7122016-03-14 15:47:09 -0500177 if (!maxWidth)
178 scaleWidth = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400179 mVarChanged = 1;
180 return 0;
181}
182
Ethan Yonkera5db7122016-03-14 15:47:09 -0500183void GUIText::SetText(string newtext)
Dees_Troy51a0e822012-09-05 15:24:24 -0400184{
Ethan Yonkera5db7122016-03-14 15:47:09 -0500185 mText = newtext;
Dees_Troy51a0e822012-09-05 15:24:24 -0400186}