blob: b518e77dcb9aa381877e504947278773f40d13bc [file] [log] [blame]
Ethan Yonker74db1572015-10-28 12:44:49 -05001/*
2 Copyright 2015 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// console.cpp - GUIConsole 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"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui.hpp"
46#include "twmsg.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Ethan Yonker74db1572015-10-28 12:44:49 -050048#define GUI_CONSOLE_BUFFER_SIZE 512
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Ethan Yonker74db1572015-10-28 12:44:49 -050050size_t last_message_count = 0;
51std::vector<Message> gMessages;
52
53std::vector<std::string> gConsole;
54std::vector<std::string> gConsoleColor;
Ethan Yonker03a42f62014-08-08 11:03:51 -050055static FILE* ors_file;
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050057extern "C" void __gui_print(const char *color, char *buf)
Dees_Troy51a0e822012-09-05 15:24:24 -040058{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 char *start, *next;
Dees_Troy51a0e822012-09-05 15:24:24 -040060
61 if (buf[0] == '\n' && strlen(buf) < 2) {
62 // This prevents the double lines bug seen in the console during zip installs
63 return;
64 }
65
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010066 for (start = next = buf; *next != '\0';)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 {
68 if (*next == '\n')
69 {
70 *next = '\0';
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010071 gConsole.push_back(start);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050072 gConsoleColor.push_back(color);
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010074 start = ++next;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020075 }
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010076 else
77 ++next;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 }
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010080 // The text after last \n (or whole string if there is no \n)
Matt Mowera8a89d12016-12-30 18:10:37 -060081 if (*start) {
Vojtech Bocekd5b26d62014-03-06 22:56:13 +010082 gConsole.push_back(start);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050083 gConsoleColor.push_back(color);
84 }
85}
86
87extern "C" void gui_print(const char *fmt, ...)
88{
Ethan Yonker74db1572015-10-28 12:44:49 -050089 char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050090
91 va_list ap;
92 va_start(ap, fmt);
Ethan Yonker74db1572015-10-28 12:44:49 -050093 vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -050094 va_end(ap);
95
96 fputs(buf, stdout);
Ethan Yonker74db1572015-10-28 12:44:49 -050097 if (ors_file) {
98 fprintf(ors_file, "%s", buf);
99 fflush(ors_file);
100 }
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500101
102 __gui_print("normal", buf);
103 return;
104}
105
106extern "C" void gui_print_color(const char *color, const char *fmt, ...)
107{
Ethan Yonker74db1572015-10-28 12:44:49 -0500108 char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500109
110 va_list ap;
111 va_start(ap, fmt);
Ethan Yonker74db1572015-10-28 12:44:49 -0500112 vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500113 va_end(ap);
114
115 fputs(buf, stdout);
Ethan Yonker74db1572015-10-28 12:44:49 -0500116 if (ors_file) {
117 fprintf(ors_file, "%s", buf);
118 fflush(ors_file);
119 }
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500120
121 __gui_print(color, buf);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123}
124
Ethan Yonker03a42f62014-08-08 11:03:51 -0500125extern "C" void gui_set_FILE(FILE* f)
126{
127 ors_file = f;
128}
129
Ethan Yonker74db1572015-10-28 12:44:49 -0500130void gui_msg(const char* text)
131{
132 if (text) {
133 Message msg = Msg(text);
134 gui_msg(msg);
135 }
136}
137
138void gui_warn(const char* text)
139{
140 if (text) {
141 Message msg = Msg(msg::kWarning, text);
142 gui_msg(msg);
143 }
144}
145
146void gui_err(const char* text)
147{
148 if (text) {
149 Message msg = Msg(msg::kError, text);
150 gui_msg(msg);
151 }
152}
153
154void gui_highlight(const char* text)
155{
156 if (text) {
157 Message msg = Msg(msg::kHighlight, text);
158 gui_msg(msg);
159 }
160}
161
162void gui_msg(Message msg)
163{
164 std::string output = msg;
165 output += "\n";
166 fputs(output.c_str(), stdout);
167 if (ors_file) {
168 fprintf(ors_file, "%s", output.c_str());
169 fflush(ors_file);
170 }
171 gMessages.push_back(msg);
172}
173
174void GUIConsole::Translate_Now() {
175 size_t message_count = gMessages.size();
176 if (message_count <= last_message_count)
177 return;
178
179 for (size_t m = last_message_count; m < message_count; m++) {
180 std::string message = gMessages[m];
181 std::string color = "normal";
182 if (gMessages[m].GetKind() == msg::kError)
183 color = "error";
184 else if (gMessages[m].GetKind() == msg::kHighlight)
185 color = "highlight";
186 else if (gMessages[m].GetKind() == msg::kWarning)
187 color = "warning";
188 gConsole.push_back(message);
189 gConsoleColor.push_back(color);
190 }
191 last_message_count = message_count;
192}
193
that8d46c092015-02-26 01:30:04 +0100194GUIConsole::GUIConsole(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400195{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200196 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -0400197
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200198 mLastCount = 0;
that8d46c092015-02-26 01:30:04 +0100199 scrollToEnd = true;
200 mSlideoutX = mSlideoutY = mSlideoutW = mSlideoutH = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 mSlideout = 0;
that8d46c092015-02-26 01:30:04 +0100202 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400203
that8d46c092015-02-26 01:30:04 +0100204 allowSelection = false; // console doesn't support list item selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 if (!node)
207 {
Matt Mowera8a89d12016-12-30 18:10:37 -0600208 mRenderX = 0;
209 mRenderY = 0;
210 mRenderW = gr_fb_width();
211 mRenderH = gr_fb_height();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 }
213 else
214 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600215 child = FindNode(node, "color");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 if (child)
217 {
that8d46c092015-02-26 01:30:04 +0100218 mFontColor = LoadAttrColor(child, "foreground", mFontColor);
thatf6ed8fc2015-02-14 20:23:16 +0100219 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
that8d46c092015-02-26 01:30:04 +0100220 //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400222
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600223 child = FindNode(node, "slideout");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200224 if (child)
225 {
226 mSlideout = 1;
that8d46c092015-02-26 01:30:04 +0100227 mSlideoutState = hidden;
Ethan Yonker591b9202015-03-11 11:17:15 -0500228 LoadPlacement(child, &mSlideoutX, &mSlideoutY, &mSlideoutW, &mSlideoutH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -0400229
thatf6ed8fc2015-02-14 20:23:16 +0100230 mSlideoutImage = LoadAttrImage(child, "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -0400231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 if (mSlideoutImage && mSlideoutImage->GetResource())
233 {
thatf6ed8fc2015-02-14 20:23:16 +0100234 mSlideoutW = mSlideoutImage->GetWidth();
235 mSlideoutH = mSlideoutImage->GetHeight();
Ethan Yonker591b9202015-03-11 11:17:15 -0500236 if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) {
237 mSlideoutX = mSlideoutX - (mSlideoutW / 2);
238 if (mPlacement == CENTER) {
239 mSlideoutY = mSlideoutY - (mSlideoutH / 2);
240 }
241 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 }
243 }
244 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400245}
246
247int GUIConsole::RenderSlideout(void)
248{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 if (!mSlideoutImage || !mSlideoutImage->GetResource())
250 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
253 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254}
255
that8d46c092015-02-26 01:30:04 +0100256int GUIConsole::RenderConsole(void)
257{
Ethan Yonker74db1572015-10-28 12:44:49 -0500258 Translate_Now();
Ethan Yonker44925ad2015-07-22 12:33:59 -0500259 AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor);
that8d46c092015-02-26 01:30:04 +0100260 GUIScrollList::Render();
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
that8d46c092015-02-26 01:30:04 +0100262 // if last line is fully visible, keep tracking the last line when new lines are added
263 int bottom_offset = GetDisplayRemainder() - actualItemHeight;
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500264 bool isAtBottom = firstDisplayedItem == (int)GetItemCount() - GetDisplayItemCount() - (bottom_offset != 0) && y_offset == bottom_offset;
that8d46c092015-02-26 01:30:04 +0100265 if (isAtBottom)
266 scrollToEnd = true;
267#if 0
268 // debug - show if we are tracking the last line
269 if (scrollToEnd) {
270 gr_color(0,255,0,255);
271 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
272 } else {
273 gr_color(255,0,0,255);
274 gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 }
that8d46c092015-02-26 01:30:04 +0100276#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 return (mSlideout ? RenderSlideout() : 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400278}
279
280int GUIConsole::Render(void)
281{
Matt Mowera8a89d12016-12-30 18:10:37 -0600282 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100283 return 0;
284
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 if (mSlideout && mSlideoutState == hidden)
286 return RenderSlideout();
287
288 return RenderConsole();
Dees_Troy51a0e822012-09-05 15:24:24 -0400289}
290
291int GUIConsole::Update(void)
292{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 if (mSlideout && mSlideoutState != visible)
294 {
295 if (mSlideoutState == hidden)
296 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 if (mSlideoutState == request_hide)
299 mSlideoutState = hidden;
Dees_Troy51a0e822012-09-05 15:24:24 -0400300
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200301 if (mSlideoutState == request_show)
302 mSlideoutState = visible;
Dees_Troy51a0e822012-09-05 15:24:24 -0400303
that8d46c092015-02-26 01:30:04 +0100304 // Any time we activate the console, we reset the position
305 SetVisibleListLocation(rConsole.size() - 1);
306 mUpdate = 1;
307 scrollToEnd = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400309
Ethan Yonker44925ad2015-07-22 12:33:59 -0500310 if (AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor)) {
that8d46c092015-02-26 01:30:04 +0100311 // someone added new text
312 // at least the scrollbar must be updated, even if the new lines are currently not visible
313 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200314 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400315
that8d46c092015-02-26 01:30:04 +0100316 if (scrollToEnd) {
317 // keep the last line in view
318 SetVisibleListLocation(rConsole.size() - 1);
319 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400320
that8d46c092015-02-26 01:30:04 +0100321 GUIScrollList::Update();
322
323 if (mUpdate) {
324 mUpdate = 0;
325 if (Render() == 0)
326 return 2;
327 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200328 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400329}
330
331// IsInRegion - Checks if the request is handled by this object
thatf8194e22015-01-29 01:05:01 +0100332// Return 1 if this object handles the request, 0 if not
Dees_Troy51a0e822012-09-05 15:24:24 -0400333int GUIConsole::IsInRegion(int x, int y)
334{
that8d46c092015-02-26 01:30:04 +0100335 if (mSlideout) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 // Check if they tapped the slideout button
that8d46c092015-02-26 01:30:04 +0100337 if (x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 // If we're only rendering the slideout, bail now
341 if (mSlideoutState == hidden)
342 return 0;
343 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400344
that8d46c092015-02-26 01:30:04 +0100345 return GUIScrollList::IsInRegion(x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400346}
347
348// NotifyTouch - Notify of a touch event
349// Return 0 on success, >0 to ignore remainder of touch, and <0 on error
350int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
351{
Matt Mowera8a89d12016-12-30 18:10:37 -0600352 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100353 return -1;
354
that8d46c092015-02-26 01:30:04 +0100355 if (mSlideout && x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) {
356 if (state == TOUCH_START) {
357 if (mSlideoutState == hidden)
358 mSlideoutState = request_show;
359 else if (mSlideoutState == visible)
360 mSlideoutState = request_hide;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200361 }
that8d46c092015-02-26 01:30:04 +0100362 return 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 }
that8d46c092015-02-26 01:30:04 +0100364 scrollToEnd = false;
365 return GUIScrollList::NotifyTouch(state, x, y);
366}
367
368size_t GUIConsole::GetItemCount()
369{
370 return rConsole.size();
371}
372
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500373void GUIConsole::RenderItem(size_t itemindex, int yPos, bool selected __unused)
that8d46c092015-02-26 01:30:04 +0100374{
375 // Set the color for the font
376 if (rConsoleColor[itemindex] == "normal") {
377 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
378 } else {
379 COLOR FontColor;
380 std::string color = rConsoleColor[itemindex];
381 ConvertStrToColor(color, &FontColor);
382 FontColor.alpha = 255;
383 gr_color(FontColor.red, FontColor.green, FontColor.blue, FontColor.alpha);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200384 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400385
that8d46c092015-02-26 01:30:04 +0100386 // render text
387 const char* text = rConsole[itemindex].c_str();
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500388 gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0);
that8d46c092015-02-26 01:30:04 +0100389}
Dees_Troy51a0e822012-09-05 15:24:24 -0400390
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500391void GUIConsole::NotifySelect(size_t item_selected __unused)
that8d46c092015-02-26 01:30:04 +0100392{
393 // do nothing - console ignores selections
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}