blob: 49e27a10422609e5173c1fefb1efb94674332505 [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
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 int x = mRenderX, y = mRenderY;
Ethan Yonkera5db7122016-03-14 15:47:09 -0500118 int width = gr_ttf_measureEx(mLastValue.c_str(), fontResource);
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
thatf6ed8fc2015-02-14 20:23:16 +0100120 if (isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400121 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
122 else
123 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400124
Ethan Yonkera5db7122016-03-14 15:47:09 -0500125 gr_textEx_scaleW(mRenderX, mRenderY, mLastValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUIText::Update(void)
131{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (!isConditionTrue())
133 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 // This hack just makes sure we update at least once a minute for things like clock and battery
138 if (updateCounter) updateCounter--;
139 else
140 {
141 mVarChanged = 1;
142 updateCounter = 3;
143 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 if (mIsStatic || !mVarChanged)
146 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147
thatb2e8f672015-03-05 20:25:39 +0100148 std::string newValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500150 return 0;
151 else
152 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400154}
155
156int GUIText::GetCurrentBounds(int& w, int& h)
157{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 if (mFont)
161 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 h = mFontHeight;
thatb2e8f672015-03-05 20:25:39 +0100164 mLastValue = gui_parse_text(mText);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100165 w = gr_ttf_measureEx(mLastValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400167}
168
Vojtech Bocek07220562014-02-08 02:05:33 +0100169int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400170{
Vojtech Bocek07220562014-02-08 02:05:33 +0100171 GUIObject::NotifyVarChange(varName, value);
172
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173 mVarChanged = 1;
174 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400175}
176
177int GUIText::SetMaxWidth(unsigned width)
178{
179 maxWidth = width;
Ethan Yonkera5db7122016-03-14 15:47:09 -0500180 if (!maxWidth)
181 scaleWidth = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182 mVarChanged = 1;
183 return 0;
184}
185
Ethan Yonkera5db7122016-03-14 15:47:09 -0500186void GUIText::SetText(string newtext)
Dees_Troy51a0e822012-09-05 15:24:24 -0400187{
Ethan Yonkera5db7122016-03-14 15:47:09 -0500188 mText = newtext;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189}